gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
4c2df51b 71
c906108c
SS
72#include <fcntl.h>
73#include "gdb_string.h"
4bdf3d34 74#include "gdb_assert.h"
c906108c 75#include <sys/types.h>
d8151005 76
34eaf542
TT
77typedef struct symbol *symbolp;
78DEF_VEC_P (symbolp);
79
45cfd468
DE
80/* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82static int dwarf2_read_debug = 0;
83
d97bc12b 84/* When non-zero, dump DIEs after they are read in. */
ccce17b0 85static unsigned int dwarf2_die_debug = 0;
d97bc12b 86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3 90/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 91static int use_deprecated_index_sections = 0;
481860b3 92
6502dd73
DJ
93static const struct objfile_data *dwarf2_objfile_data_key;
94
dce234bc
PP
95struct dwarf2_section_info
96{
97 asection *asection;
98 gdb_byte *buffer;
99 bfd_size_type size;
be391dca
TT
100 /* True if we have tried to read this section. */
101 int readin;
dce234bc
PP
102};
103
8b70b953
TT
104typedef struct dwarf2_section_info dwarf2_section_info_def;
105DEF_VEC_O (dwarf2_section_info_def);
106
9291a0cd
TT
107/* All offsets in the index are of this type. It must be
108 architecture-independent. */
109typedef uint32_t offset_type;
110
111DEF_VEC_I (offset_type);
112
156942c7
DE
113/* Ensure only legit values are used. */
114#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
115 do { \
116 gdb_assert ((unsigned int) (value) <= 1); \
117 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
118 } while (0)
119
120/* Ensure only legit values are used. */
121#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
124 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
125 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128/* Ensure we don't use more than the alloted nuber of bits for the CU. */
129#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
132 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
9291a0cd
TT
135/* A description of the mapped index. The file format is described in
136 a comment by the code that writes the index. */
137struct mapped_index
138{
559a7a62
JK
139 /* Index data format version. */
140 int version;
141
9291a0cd
TT
142 /* The total length of the buffer. */
143 off_t total_size;
b11b1f88 144
9291a0cd
TT
145 /* A pointer to the address table data. */
146 const gdb_byte *address_table;
b11b1f88 147
9291a0cd
TT
148 /* Size of the address table data in bytes. */
149 offset_type address_table_size;
b11b1f88 150
3876f04e
DE
151 /* The symbol table, implemented as a hash table. */
152 const offset_type *symbol_table;
b11b1f88 153
9291a0cd 154 /* Size in slots, each slot is 2 offset_types. */
3876f04e 155 offset_type symbol_table_slots;
b11b1f88 156
9291a0cd
TT
157 /* A pointer to the constant pool. */
158 const char *constant_pool;
159};
160
95554aad
TT
161typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
162DEF_VEC_P (dwarf2_per_cu_ptr);
163
9cdd5dbd
DE
164/* Collection of data recorded per objfile.
165 This hangs off of dwarf2_objfile_data_key. */
166
6502dd73
DJ
167struct dwarf2_per_objfile
168{
dce234bc
PP
169 struct dwarf2_section_info info;
170 struct dwarf2_section_info abbrev;
171 struct dwarf2_section_info line;
dce234bc
PP
172 struct dwarf2_section_info loc;
173 struct dwarf2_section_info macinfo;
cf2c3c16 174 struct dwarf2_section_info macro;
dce234bc
PP
175 struct dwarf2_section_info str;
176 struct dwarf2_section_info ranges;
3019eac3 177 struct dwarf2_section_info addr;
dce234bc
PP
178 struct dwarf2_section_info frame;
179 struct dwarf2_section_info eh_frame;
9291a0cd 180 struct dwarf2_section_info gdb_index;
ae038cb0 181
8b70b953
TT
182 VEC (dwarf2_section_info_def) *types;
183
be391dca
TT
184 /* Back link. */
185 struct objfile *objfile;
186
d467dd73 187 /* Table of all the compilation units. This is used to locate
10b3939b 188 the target compilation unit of a particular reference. */
ae038cb0
DJ
189 struct dwarf2_per_cu_data **all_comp_units;
190
191 /* The number of compilation units in ALL_COMP_UNITS. */
192 int n_comp_units;
193
1fd400ff 194 /* The number of .debug_types-related CUs. */
d467dd73 195 int n_type_units;
1fd400ff 196
d467dd73 197 /* The .debug_types-related CUs (TUs). */
b4dd5633 198 struct signatured_type **all_type_units;
1fd400ff 199
f4dc4d17
DE
200 /* The number of entries in all_type_unit_groups. */
201 int n_type_unit_groups;
202
203 /* Table of type unit groups.
204 This exists to make it easy to iterate over all CUs and TU groups. */
205 struct type_unit_group **all_type_unit_groups;
206
207 /* Table of struct type_unit_group objects.
208 The hash key is the DW_AT_stmt_list value. */
209 htab_t type_unit_groups;
72dca2f5 210
348e048f
DE
211 /* A table mapping .debug_types signatures to its signatured_type entry.
212 This is NULL if the .debug_types section hasn't been read in yet. */
213 htab_t signatured_types;
214
f4dc4d17
DE
215 /* Type unit statistics, to see how well the scaling improvements
216 are doing. */
217 struct tu_stats
218 {
219 int nr_uniq_abbrev_tables;
220 int nr_symtabs;
221 int nr_symtab_sharers;
222 int nr_stmt_less_type_units;
223 } tu_stats;
224
225 /* A chain of compilation units that are currently read in, so that
226 they can be freed later. */
227 struct dwarf2_per_cu_data *read_in_chain;
228
3019eac3
DE
229 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
230 This is NULL if the table hasn't been allocated yet. */
231 htab_t dwo_files;
232
80626a55
DE
233 /* Non-zero if we've check for whether there is a DWP file. */
234 int dwp_checked;
235
236 /* The DWP file if there is one, or NULL. */
237 struct dwp_file *dwp_file;
238
36586728
TT
239 /* The shared '.dwz' file, if one exists. This is used when the
240 original data was compressed using 'dwz -m'. */
241 struct dwz_file *dwz_file;
242
72dca2f5
FR
243 /* A flag indicating wether this objfile has a section loaded at a
244 VMA of 0. */
245 int has_section_at_zero;
9291a0cd 246
ae2de4f8
DE
247 /* True if we are using the mapped index,
248 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
249 unsigned char using_index;
250
ae2de4f8 251 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 252 struct mapped_index *index_table;
98bfdba5 253
7b9f3c50 254 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
255 TUs typically share line table entries with a CU, so we maintain a
256 separate table of all line table entries to support the sharing.
257 Note that while there can be way more TUs than CUs, we've already
258 sorted all the TUs into "type unit groups", grouped by their
259 DW_AT_stmt_list value. Therefore the only sharing done here is with a
260 CU and its associated TU group if there is one. */
7b9f3c50
DE
261 htab_t quick_file_names_table;
262
98bfdba5
PA
263 /* Set during partial symbol reading, to prevent queueing of full
264 symbols. */
265 int reading_partial_symbols;
673bfd45 266
dee91e82 267 /* Table mapping type DIEs to their struct type *.
673bfd45 268 This is NULL if not allocated yet.
dee91e82
DE
269 The mapping is done via (CU/TU signature + DIE offset) -> type. */
270 htab_t die_type_hash;
95554aad
TT
271
272 /* The CUs we recently read. */
273 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
274};
275
276static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 277
251d32d9 278/* Default names of the debugging sections. */
c906108c 279
233a11ab
CS
280/* Note that if the debugging section has been compressed, it might
281 have a name like .zdebug_info. */
282
9cdd5dbd
DE
283static const struct dwarf2_debug_sections dwarf2_elf_names =
284{
251d32d9
TG
285 { ".debug_info", ".zdebug_info" },
286 { ".debug_abbrev", ".zdebug_abbrev" },
287 { ".debug_line", ".zdebug_line" },
288 { ".debug_loc", ".zdebug_loc" },
289 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 290 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
291 { ".debug_str", ".zdebug_str" },
292 { ".debug_ranges", ".zdebug_ranges" },
293 { ".debug_types", ".zdebug_types" },
3019eac3 294 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
24d3216f
TT
297 { ".gdb_index", ".zgdb_index" },
298 23
251d32d9 299};
c906108c 300
80626a55 301/* List of DWO/DWP sections. */
3019eac3 302
80626a55 303static const struct dwop_section_names
3019eac3
DE
304{
305 struct dwarf2_section_names abbrev_dwo;
306 struct dwarf2_section_names info_dwo;
307 struct dwarf2_section_names line_dwo;
308 struct dwarf2_section_names loc_dwo;
09262596
DE
309 struct dwarf2_section_names macinfo_dwo;
310 struct dwarf2_section_names macro_dwo;
3019eac3
DE
311 struct dwarf2_section_names str_dwo;
312 struct dwarf2_section_names str_offsets_dwo;
313 struct dwarf2_section_names types_dwo;
80626a55
DE
314 struct dwarf2_section_names cu_index;
315 struct dwarf2_section_names tu_index;
3019eac3 316}
80626a55 317dwop_section_names =
3019eac3
DE
318{
319 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
320 { ".debug_info.dwo", ".zdebug_info.dwo" },
321 { ".debug_line.dwo", ".zdebug_line.dwo" },
322 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
323 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
324 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
325 { ".debug_str.dwo", ".zdebug_str.dwo" },
326 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
327 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
328 { ".debug_cu_index", ".zdebug_cu_index" },
329 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
330};
331
c906108c
SS
332/* local data types */
333
107d2387
AC
334/* The data in a compilation unit header, after target2host
335 translation, looks like this. */
c906108c 336struct comp_unit_head
a738430d 337{
c764a876 338 unsigned int length;
a738430d 339 short version;
a738430d
MK
340 unsigned char addr_size;
341 unsigned char signed_addr_p;
b64f50a1 342 sect_offset abbrev_offset;
57349743 343
a738430d
MK
344 /* Size of file offsets; either 4 or 8. */
345 unsigned int offset_size;
57349743 346
a738430d
MK
347 /* Size of the length field; either 4 or 12. */
348 unsigned int initial_length_size;
57349743 349
a738430d
MK
350 /* Offset to the first byte of this compilation unit header in the
351 .debug_info section, for resolving relative reference dies. */
b64f50a1 352 sect_offset offset;
57349743 353
d00adf39
DE
354 /* Offset to first die in this cu from the start of the cu.
355 This will be the first byte following the compilation unit header. */
b64f50a1 356 cu_offset first_die_offset;
a738430d 357};
c906108c 358
3da10d80
KS
359/* Type used for delaying computation of method physnames.
360 See comments for compute_delayed_physnames. */
361struct delayed_method_info
362{
363 /* The type to which the method is attached, i.e., its parent class. */
364 struct type *type;
365
366 /* The index of the method in the type's function fieldlists. */
367 int fnfield_index;
368
369 /* The index of the method in the fieldlist. */
370 int index;
371
372 /* The name of the DIE. */
373 const char *name;
374
375 /* The DIE associated with this method. */
376 struct die_info *die;
377};
378
379typedef struct delayed_method_info delayed_method_info;
380DEF_VEC_O (delayed_method_info);
381
e7c27a73
DJ
382/* Internal state when decoding a particular compilation unit. */
383struct dwarf2_cu
384{
385 /* The objfile containing this compilation unit. */
386 struct objfile *objfile;
387
d00adf39 388 /* The header of the compilation unit. */
e7c27a73 389 struct comp_unit_head header;
e142c38c 390
d00adf39
DE
391 /* Base address of this compilation unit. */
392 CORE_ADDR base_address;
393
394 /* Non-zero if base_address has been set. */
395 int base_known;
396
e142c38c
DJ
397 /* The language we are debugging. */
398 enum language language;
399 const struct language_defn *language_defn;
400
b0f35d58
DL
401 const char *producer;
402
e142c38c
DJ
403 /* The generic symbol table building routines have separate lists for
404 file scope symbols and all all other scopes (local scopes). So
405 we need to select the right one to pass to add_symbol_to_list().
406 We do it by keeping a pointer to the correct list in list_in_scope.
407
408 FIXME: The original dwarf code just treated the file scope as the
409 first local scope, and all other local scopes as nested local
410 scopes, and worked fine. Check to see if we really need to
411 distinguish these in buildsym.c. */
412 struct pending **list_in_scope;
413
433df2d4
DE
414 /* The abbrev table for this CU.
415 Normally this points to the abbrev table in the objfile.
416 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
417 struct abbrev_table *abbrev_table;
72bf9492 418
b64f50a1
JK
419 /* Hash table holding all the loaded partial DIEs
420 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
421 htab_t partial_dies;
422
423 /* Storage for things with the same lifetime as this read-in compilation
424 unit, including partial DIEs. */
425 struct obstack comp_unit_obstack;
426
ae038cb0
DJ
427 /* When multiple dwarf2_cu structures are living in memory, this field
428 chains them all together, so that they can be released efficiently.
429 We will probably also want a generation counter so that most-recently-used
430 compilation units are cached... */
431 struct dwarf2_per_cu_data *read_in_chain;
432
433 /* Backchain to our per_cu entry if the tree has been built. */
434 struct dwarf2_per_cu_data *per_cu;
435
436 /* How many compilation units ago was this CU last referenced? */
437 int last_used;
438
b64f50a1
JK
439 /* A hash table of DIE cu_offset for following references with
440 die_info->offset.sect_off as hash. */
51545339 441 htab_t die_hash;
10b3939b
DJ
442
443 /* Full DIEs if read in. */
444 struct die_info *dies;
445
446 /* A set of pointers to dwarf2_per_cu_data objects for compilation
447 units referenced by this one. Only set during full symbol processing;
448 partial symbol tables do not have dependencies. */
449 htab_t dependencies;
450
cb1df416
DJ
451 /* Header data from the line table, during full symbol processing. */
452 struct line_header *line_header;
453
3da10d80
KS
454 /* A list of methods which need to have physnames computed
455 after all type information has been read. */
456 VEC (delayed_method_info) *method_list;
457
96408a79
SA
458 /* To be copied to symtab->call_site_htab. */
459 htab_t call_site_htab;
460
034e5797
DE
461 /* Non-NULL if this CU came from a DWO file.
462 There is an invariant here that is important to remember:
463 Except for attributes copied from the top level DIE in the "main"
464 (or "stub") file in preparation for reading the DWO file
465 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
466 Either there isn't a DWO file (in which case this is NULL and the point
467 is moot), or there is and either we're not going to read it (in which
468 case this is NULL) or there is and we are reading it (in which case this
469 is non-NULL). */
3019eac3
DE
470 struct dwo_unit *dwo_unit;
471
472 /* The DW_AT_addr_base attribute if present, zero otherwise
473 (zero is a valid value though).
474 Note this value comes from the stub CU/TU's DIE. */
475 ULONGEST addr_base;
476
2e3cf129
DE
477 /* The DW_AT_ranges_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE.
480 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
481 be used without needing to know whether DWO files are in use or not.
482 N.B. This does not apply to DW_AT_ranges appearing in
483 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
484 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
485 DW_AT_ranges_base *would* have to be applied, and we'd have to care
486 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
487 ULONGEST ranges_base;
488
ae038cb0
DJ
489 /* Mark used when releasing cached dies. */
490 unsigned int mark : 1;
491
8be455d7
JK
492 /* This CU references .debug_loc. See the symtab->locations_valid field.
493 This test is imperfect as there may exist optimized debug code not using
494 any location list and still facing inlining issues if handled as
495 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 496 unsigned int has_loclist : 1;
ba919b58 497
1b80a9fa
JK
498 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
499 if all the producer_is_* fields are valid. This information is cached
500 because profiling CU expansion showed excessive time spent in
501 producer_is_gxx_lt_4_6. */
ba919b58
TT
502 unsigned int checked_producer : 1;
503 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 504 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 505 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
506
507 /* When set, the file that we're processing is known to have
508 debugging info for C++ namespaces. GCC 3.3.x did not produce
509 this information, but later versions do. */
510
511 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
512};
513
10b3939b
DJ
514/* Persistent data held for a compilation unit, even when not
515 processing it. We put a pointer to this structure in the
28dee7f5 516 read_symtab_private field of the psymtab. */
10b3939b 517
ae038cb0
DJ
518struct dwarf2_per_cu_data
519{
36586728 520 /* The start offset and length of this compilation unit.
45452591 521 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
522 initial_length_size.
523 If the DIE refers to a DWO file, this is always of the original die,
524 not the DWO file. */
b64f50a1 525 sect_offset offset;
36586728 526 unsigned int length;
ae038cb0
DJ
527
528 /* Flag indicating this compilation unit will be read in before
529 any of the current compilation units are processed. */
c764a876 530 unsigned int queued : 1;
ae038cb0 531
0d99eb77
DE
532 /* This flag will be set when reading partial DIEs if we need to load
533 absolutely all DIEs for this compilation unit, instead of just the ones
534 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
535 hash table and don't find it. */
536 unsigned int load_all_dies : 1;
537
3019eac3
DE
538 /* Non-zero if this CU is from .debug_types. */
539 unsigned int is_debug_types : 1;
540
36586728
TT
541 /* Non-zero if this CU is from the .dwz file. */
542 unsigned int is_dwz : 1;
543
3019eac3
DE
544 /* The section this CU/TU lives in.
545 If the DIE refers to a DWO file, this is always the original die,
546 not the DWO file. */
547 struct dwarf2_section_info *info_or_types_section;
348e048f 548
17ea53c3
JK
549 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
550 of the CU cache it gets reset to NULL again. */
ae038cb0 551 struct dwarf2_cu *cu;
1c379e20 552
9cdd5dbd
DE
553 /* The corresponding objfile.
554 Normally we can get the objfile from dwarf2_per_objfile.
555 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
556 struct objfile *objfile;
557
558 /* When using partial symbol tables, the 'psymtab' field is active.
559 Otherwise the 'quick' field is active. */
560 union
561 {
562 /* The partial symbol table associated with this compilation unit,
95554aad 563 or NULL for unread partial units. */
9291a0cd
TT
564 struct partial_symtab *psymtab;
565
566 /* Data needed by the "quick" functions. */
567 struct dwarf2_per_cu_quick_data *quick;
568 } v;
95554aad 569
796a7ff8
DE
570 /* The CUs we import using DW_TAG_imported_unit. This is filled in
571 while reading psymtabs, used to compute the psymtab dependencies,
572 and then cleared. Then it is filled in again while reading full
573 symbols, and only deleted when the objfile is destroyed.
574
575 This is also used to work around a difference between the way gold
576 generates .gdb_index version <=7 and the way gdb does. Arguably this
577 is a gold bug. For symbols coming from TUs, gold records in the index
578 the CU that includes the TU instead of the TU itself. This breaks
579 dw2_lookup_symbol: It assumes that if the index says symbol X lives
580 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
581 will find X. Alas TUs live in their own symtab, so after expanding CU Y
582 we need to look in TU Z to find X. Fortunately, this is akin to
583 DW_TAG_imported_unit, so we just use the same mechanism: For
584 .gdb_index version <=7 this also records the TUs that the CU referred
585 to. Concurrently with this change gdb was modified to emit version 8
586 indices so we only pay a price for gold generated indices. */
587 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
588
589 /* Type units are grouped by their DW_AT_stmt_list entry so that they
590 can share them. If this is a TU, this points to the containing
591 symtab. */
592 struct type_unit_group *type_unit_group;
ae038cb0
DJ
593};
594
348e048f
DE
595/* Entry in the signatured_types hash table. */
596
597struct signatured_type
598{
42e7ad6c
DE
599 /* The "per_cu" object of this type.
600 N.B.: This is the first member so that it's easy to convert pointers
601 between them. */
602 struct dwarf2_per_cu_data per_cu;
603
3019eac3 604 /* The type's signature. */
348e048f
DE
605 ULONGEST signature;
606
3019eac3
DE
607 /* Offset in the TU of the type's DIE, as read from the TU header.
608 If the definition lives in a DWO file, this value is unusable. */
609 cu_offset type_offset_in_tu;
610
611 /* Offset in the section of the type's DIE.
612 If the definition lives in a DWO file, this is the offset in the
613 .debug_types.dwo section.
614 The value is zero until the actual value is known.
615 Zero is otherwise not a valid section offset. */
616 sect_offset type_offset_in_section;
348e048f
DE
617};
618
094b34ac
DE
619/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
620 This includes type_unit_group and quick_file_names. */
621
622struct stmt_list_hash
623{
624 /* The DWO unit this table is from or NULL if there is none. */
625 struct dwo_unit *dwo_unit;
626
627 /* Offset in .debug_line or .debug_line.dwo. */
628 sect_offset line_offset;
629};
630
f4dc4d17
DE
631/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
632 an object of this type. */
633
634struct type_unit_group
635{
636 /* dwarf2read.c's main "handle" on the symtab.
637 To simplify things we create an artificial CU that "includes" all the
638 type units using this stmt_list so that the rest of the code still has
639 a "per_cu" handle on the symtab.
640 This PER_CU is recognized by having no section. */
641#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
642 struct dwarf2_per_cu_data per_cu;
643
644 union
645 {
646 /* The TUs that share this DW_AT_stmt_list entry.
647 This is added to while parsing type units to build partial symtabs,
648 and is deleted afterwards and not used again. */
649 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 650
094b34ac
DE
651 /* When reading the line table in "quick" functions, we need a real TU.
652 Any will do, we know they all share the same DW_AT_stmt_list entry.
653 For simplicity's sake, we pick the first one. */
654 struct dwarf2_per_cu_data *first_tu;
655 } t;
f4dc4d17
DE
656
657 /* The primary symtab.
094b34ac
DE
658 Type units in a group needn't all be defined in the same source file,
659 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
660 struct symtab *primary_symtab;
661
094b34ac
DE
662 /* The data used to construct the hash key. */
663 struct stmt_list_hash hash;
f4dc4d17
DE
664
665 /* The number of symtabs from the line header.
666 The value here must match line_header.num_file_names. */
667 unsigned int num_symtabs;
668
669 /* The symbol tables for this TU (obtained from the files listed in
670 DW_AT_stmt_list).
671 WARNING: The order of entries here must match the order of entries
672 in the line header. After the first TU using this type_unit_group, the
673 line header for the subsequent TUs is recreated from this. This is done
674 because we need to use the same symtabs for each TU using the same
675 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
676 there's no guarantee the line header doesn't have duplicate entries. */
677 struct symtab **symtabs;
678};
679
80626a55 680/* These sections are what may appear in a DWO file. */
3019eac3
DE
681
682struct dwo_sections
683{
684 struct dwarf2_section_info abbrev;
3019eac3
DE
685 struct dwarf2_section_info line;
686 struct dwarf2_section_info loc;
09262596
DE
687 struct dwarf2_section_info macinfo;
688 struct dwarf2_section_info macro;
3019eac3
DE
689 struct dwarf2_section_info str;
690 struct dwarf2_section_info str_offsets;
80626a55
DE
691 /* In the case of a virtual DWO file, these two are unused. */
692 struct dwarf2_section_info info;
3019eac3
DE
693 VEC (dwarf2_section_info_def) *types;
694};
695
696/* Common bits of DWO CUs/TUs. */
697
698struct dwo_unit
699{
700 /* Backlink to the containing struct dwo_file. */
701 struct dwo_file *dwo_file;
702
703 /* The "id" that distinguishes this CU/TU.
704 .debug_info calls this "dwo_id", .debug_types calls this "signature".
705 Since signatures came first, we stick with it for consistency. */
706 ULONGEST signature;
707
708 /* The section this CU/TU lives in, in the DWO file. */
709 struct dwarf2_section_info *info_or_types_section;
710
711 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
712 sect_offset offset;
713 unsigned int length;
714
715 /* For types, offset in the type's DIE of the type defined by this TU. */
716 cu_offset type_offset_in_tu;
717};
718
80626a55
DE
719/* Data for one DWO file.
720 This includes virtual DWO files that have been packaged into a
721 DWP file. */
3019eac3
DE
722
723struct dwo_file
724{
80626a55
DE
725 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
726 For virtual DWO files the name is constructed from the section offsets
727 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
728 from related CU+TUs. */
729 const char *name;
3019eac3 730
80626a55
DE
731 /* The bfd, when the file is open. Otherwise this is NULL.
732 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
733 bfd *dbfd;
3019eac3
DE
734
735 /* Section info for this file. */
736 struct dwo_sections sections;
737
738 /* Table of CUs in the file.
739 Each element is a struct dwo_unit. */
740 htab_t cus;
741
742 /* Table of TUs in the file.
743 Each element is a struct dwo_unit. */
744 htab_t tus;
745};
746
80626a55
DE
747/* These sections are what may appear in a DWP file. */
748
749struct dwp_sections
750{
751 struct dwarf2_section_info str;
752 struct dwarf2_section_info cu_index;
753 struct dwarf2_section_info tu_index;
754 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
755 by section number. We don't need to record them here. */
756};
757
758/* These sections are what may appear in a virtual DWO file. */
759
760struct virtual_dwo_sections
761{
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info line;
764 struct dwarf2_section_info loc;
765 struct dwarf2_section_info macinfo;
766 struct dwarf2_section_info macro;
767 struct dwarf2_section_info str_offsets;
768 /* Each DWP hash table entry records one CU or one TU.
769 That is recorded here, and copied to dwo_unit.info_or_types_section. */
770 struct dwarf2_section_info info_or_types;
771};
772
773/* Contents of DWP hash tables. */
774
775struct dwp_hash_table
776{
777 uint32_t nr_units, nr_slots;
778 const gdb_byte *hash_table, *unit_table, *section_pool;
779};
780
781/* Data for one DWP file. */
782
783struct dwp_file
784{
785 /* Name of the file. */
786 const char *name;
787
788 /* The bfd, when the file is open. Otherwise this is NULL. */
789 bfd *dbfd;
790
791 /* Section info for this file. */
792 struct dwp_sections sections;
793
794 /* Table of CUs in the file. */
795 const struct dwp_hash_table *cus;
796
797 /* Table of TUs in the file. */
798 const struct dwp_hash_table *tus;
799
800 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
801 htab_t loaded_cutus;
802
803 /* Table to map ELF section numbers to their sections. */
804 unsigned int num_sections;
805 asection **elf_sections;
806};
807
36586728
TT
808/* This represents a '.dwz' file. */
809
810struct dwz_file
811{
812 /* A dwz file can only contain a few sections. */
813 struct dwarf2_section_info abbrev;
814 struct dwarf2_section_info info;
815 struct dwarf2_section_info str;
816 struct dwarf2_section_info line;
817 struct dwarf2_section_info macro;
2ec9a5e0 818 struct dwarf2_section_info gdb_index;
36586728
TT
819
820 /* The dwz's BFD. */
821 bfd *dwz_bfd;
822};
823
0963b4bd
MS
824/* Struct used to pass misc. parameters to read_die_and_children, et
825 al. which are used for both .debug_info and .debug_types dies.
826 All parameters here are unchanging for the life of the call. This
dee91e82 827 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
828
829struct die_reader_specs
830{
dee91e82 831 /* die_section->asection->owner. */
93311388
DE
832 bfd* abfd;
833
834 /* The CU of the DIE we are parsing. */
835 struct dwarf2_cu *cu;
836
80626a55 837 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
838 struct dwo_file *dwo_file;
839
dee91e82 840 /* The section the die comes from.
3019eac3 841 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
842 struct dwarf2_section_info *die_section;
843
844 /* die_section->buffer. */
845 gdb_byte *buffer;
f664829e
DE
846
847 /* The end of the buffer. */
848 const gdb_byte *buffer_end;
93311388
DE
849};
850
fd820528 851/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
852typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
853 gdb_byte *info_ptr,
854 struct die_info *comp_unit_die,
855 int has_children,
856 void *data);
857
debd256d
JB
858/* The line number information for a compilation unit (found in the
859 .debug_line section) begins with a "statement program header",
860 which contains the following information. */
861struct line_header
862{
863 unsigned int total_length;
864 unsigned short version;
865 unsigned int header_length;
866 unsigned char minimum_instruction_length;
2dc7f7b3 867 unsigned char maximum_ops_per_instruction;
debd256d
JB
868 unsigned char default_is_stmt;
869 int line_base;
870 unsigned char line_range;
871 unsigned char opcode_base;
872
873 /* standard_opcode_lengths[i] is the number of operands for the
874 standard opcode whose value is i. This means that
875 standard_opcode_lengths[0] is unused, and the last meaningful
876 element is standard_opcode_lengths[opcode_base - 1]. */
877 unsigned char *standard_opcode_lengths;
878
879 /* The include_directories table. NOTE! These strings are not
880 allocated with xmalloc; instead, they are pointers into
881 debug_line_buffer. If you try to free them, `free' will get
882 indigestion. */
883 unsigned int num_include_dirs, include_dirs_size;
884 char **include_dirs;
885
886 /* The file_names table. NOTE! These strings are not allocated
887 with xmalloc; instead, they are pointers into debug_line_buffer.
888 Don't try to free them directly. */
889 unsigned int num_file_names, file_names_size;
890 struct file_entry
c906108c 891 {
debd256d
JB
892 char *name;
893 unsigned int dir_index;
894 unsigned int mod_time;
895 unsigned int length;
aaa75496 896 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 897 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
898 } *file_names;
899
900 /* The start and end of the statement program following this
6502dd73 901 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 902 gdb_byte *statement_program_start, *statement_program_end;
debd256d 903};
c906108c
SS
904
905/* When we construct a partial symbol table entry we only
0963b4bd 906 need this much information. */
c906108c
SS
907struct partial_die_info
908 {
72bf9492 909 /* Offset of this DIE. */
b64f50a1 910 sect_offset offset;
72bf9492
DJ
911
912 /* DWARF-2 tag for this DIE. */
913 ENUM_BITFIELD(dwarf_tag) tag : 16;
914
72bf9492
DJ
915 /* Assorted flags describing the data found in this DIE. */
916 unsigned int has_children : 1;
917 unsigned int is_external : 1;
918 unsigned int is_declaration : 1;
919 unsigned int has_type : 1;
920 unsigned int has_specification : 1;
921 unsigned int has_pc_info : 1;
481860b3 922 unsigned int may_be_inlined : 1;
72bf9492
DJ
923
924 /* Flag set if the SCOPE field of this structure has been
925 computed. */
926 unsigned int scope_set : 1;
927
fa4028e9
JB
928 /* Flag set if the DIE has a byte_size attribute. */
929 unsigned int has_byte_size : 1;
930
98bfdba5
PA
931 /* Flag set if any of the DIE's children are template arguments. */
932 unsigned int has_template_arguments : 1;
933
abc72ce4
DE
934 /* Flag set if fixup_partial_die has been called on this die. */
935 unsigned int fixup_called : 1;
936
36586728
TT
937 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
938 unsigned int is_dwz : 1;
939
940 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
941 unsigned int spec_is_dwz : 1;
942
72bf9492 943 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 944 sometimes a default name for unnamed DIEs. */
15d034d0 945 const char *name;
72bf9492 946
abc72ce4
DE
947 /* The linkage name, if present. */
948 const char *linkage_name;
949
72bf9492
DJ
950 /* The scope to prepend to our children. This is generally
951 allocated on the comp_unit_obstack, so will disappear
952 when this compilation unit leaves the cache. */
15d034d0 953 const char *scope;
72bf9492 954
95554aad
TT
955 /* Some data associated with the partial DIE. The tag determines
956 which field is live. */
957 union
958 {
959 /* The location description associated with this DIE, if any. */
960 struct dwarf_block *locdesc;
961 /* The offset of an import, for DW_TAG_imported_unit. */
962 sect_offset offset;
963 } d;
72bf9492
DJ
964
965 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
966 CORE_ADDR lowpc;
967 CORE_ADDR highpc;
72bf9492 968
93311388 969 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 970 DW_AT_sibling, if any. */
abc72ce4
DE
971 /* NOTE: This member isn't strictly necessary, read_partial_die could
972 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 973 gdb_byte *sibling;
72bf9492
DJ
974
975 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
976 DW_AT_specification (or DW_AT_abstract_origin or
977 DW_AT_extension). */
b64f50a1 978 sect_offset spec_offset;
72bf9492
DJ
979
980 /* Pointers to this DIE's parent, first child, and next sibling,
981 if any. */
982 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
983 };
984
0963b4bd 985/* This data structure holds the information of an abbrev. */
c906108c
SS
986struct abbrev_info
987 {
988 unsigned int number; /* number identifying abbrev */
989 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
990 unsigned short has_children; /* boolean */
991 unsigned short num_attrs; /* number of attributes */
c906108c
SS
992 struct attr_abbrev *attrs; /* an array of attribute descriptions */
993 struct abbrev_info *next; /* next in chain */
994 };
995
996struct attr_abbrev
997 {
9d25dd43
DE
998 ENUM_BITFIELD(dwarf_attribute) name : 16;
999 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1000 };
1001
433df2d4
DE
1002/* Size of abbrev_table.abbrev_hash_table. */
1003#define ABBREV_HASH_SIZE 121
1004
1005/* Top level data structure to contain an abbreviation table. */
1006
1007struct abbrev_table
1008{
f4dc4d17
DE
1009 /* Where the abbrev table came from.
1010 This is used as a sanity check when the table is used. */
433df2d4
DE
1011 sect_offset offset;
1012
1013 /* Storage for the abbrev table. */
1014 struct obstack abbrev_obstack;
1015
1016 /* Hash table of abbrevs.
1017 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1018 It could be statically allocated, but the previous code didn't so we
1019 don't either. */
1020 struct abbrev_info **abbrevs;
1021};
1022
0963b4bd 1023/* Attributes have a name and a value. */
b60c80d6
DJ
1024struct attribute
1025 {
9d25dd43 1026 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1027 ENUM_BITFIELD(dwarf_form) form : 15;
1028
1029 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1030 field should be in u.str (existing only for DW_STRING) but it is kept
1031 here for better struct attribute alignment. */
1032 unsigned int string_is_canonical : 1;
1033
b60c80d6
DJ
1034 union
1035 {
15d034d0 1036 const char *str;
b60c80d6 1037 struct dwarf_block *blk;
43bbcdc2
PH
1038 ULONGEST unsnd;
1039 LONGEST snd;
b60c80d6 1040 CORE_ADDR addr;
348e048f 1041 struct signatured_type *signatured_type;
b60c80d6
DJ
1042 }
1043 u;
1044 };
1045
0963b4bd 1046/* This data structure holds a complete die structure. */
c906108c
SS
1047struct die_info
1048 {
76815b17
DE
1049 /* DWARF-2 tag for this DIE. */
1050 ENUM_BITFIELD(dwarf_tag) tag : 16;
1051
1052 /* Number of attributes */
98bfdba5
PA
1053 unsigned char num_attrs;
1054
1055 /* True if we're presently building the full type name for the
1056 type derived from this DIE. */
1057 unsigned char building_fullname : 1;
76815b17
DE
1058
1059 /* Abbrev number */
1060 unsigned int abbrev;
1061
93311388 1062 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1063 sect_offset offset;
78ba4af6
JB
1064
1065 /* The dies in a compilation unit form an n-ary tree. PARENT
1066 points to this die's parent; CHILD points to the first child of
1067 this node; and all the children of a given node are chained
4950bc1c 1068 together via their SIBLING fields. */
639d11d3
DC
1069 struct die_info *child; /* Its first child, if any. */
1070 struct die_info *sibling; /* Its next sibling, if any. */
1071 struct die_info *parent; /* Its parent, if any. */
c906108c 1072
b60c80d6
DJ
1073 /* An array of attributes, with NUM_ATTRS elements. There may be
1074 zero, but it's not common and zero-sized arrays are not
1075 sufficiently portable C. */
1076 struct attribute attrs[1];
c906108c
SS
1077 };
1078
0963b4bd 1079/* Get at parts of an attribute structure. */
c906108c
SS
1080
1081#define DW_STRING(attr) ((attr)->u.str)
8285870a 1082#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1083#define DW_UNSND(attr) ((attr)->u.unsnd)
1084#define DW_BLOCK(attr) ((attr)->u.blk)
1085#define DW_SND(attr) ((attr)->u.snd)
1086#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1087#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1088
0963b4bd 1089/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1090struct dwarf_block
1091 {
56eb65bd 1092 size_t size;
1d6edc3c
JK
1093
1094 /* Valid only if SIZE is not zero. */
fe1b8b76 1095 gdb_byte *data;
c906108c
SS
1096 };
1097
c906108c
SS
1098#ifndef ATTR_ALLOC_CHUNK
1099#define ATTR_ALLOC_CHUNK 4
1100#endif
1101
c906108c
SS
1102/* Allocate fields for structs, unions and enums in this size. */
1103#ifndef DW_FIELD_ALLOC_CHUNK
1104#define DW_FIELD_ALLOC_CHUNK 4
1105#endif
1106
c906108c
SS
1107/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1108 but this would require a corresponding change in unpack_field_as_long
1109 and friends. */
1110static int bits_per_byte = 8;
1111
1112/* The routines that read and process dies for a C struct or C++ class
1113 pass lists of data member fields and lists of member function fields
1114 in an instance of a field_info structure, as defined below. */
1115struct field_info
c5aa993b 1116 {
0963b4bd 1117 /* List of data member and baseclasses fields. */
c5aa993b
JM
1118 struct nextfield
1119 {
1120 struct nextfield *next;
1121 int accessibility;
1122 int virtuality;
1123 struct field field;
1124 }
7d0ccb61 1125 *fields, *baseclasses;
c906108c 1126
7d0ccb61 1127 /* Number of fields (including baseclasses). */
c5aa993b 1128 int nfields;
c906108c 1129
c5aa993b
JM
1130 /* Number of baseclasses. */
1131 int nbaseclasses;
c906108c 1132
c5aa993b
JM
1133 /* Set if the accesibility of one of the fields is not public. */
1134 int non_public_fields;
c906108c 1135
c5aa993b
JM
1136 /* Member function fields array, entries are allocated in the order they
1137 are encountered in the object file. */
1138 struct nextfnfield
1139 {
1140 struct nextfnfield *next;
1141 struct fn_field fnfield;
1142 }
1143 *fnfields;
c906108c 1144
c5aa993b
JM
1145 /* Member function fieldlist array, contains name of possibly overloaded
1146 member function, number of overloaded member functions and a pointer
1147 to the head of the member function field chain. */
1148 struct fnfieldlist
1149 {
15d034d0 1150 const char *name;
c5aa993b
JM
1151 int length;
1152 struct nextfnfield *head;
1153 }
1154 *fnfieldlists;
c906108c 1155
c5aa993b
JM
1156 /* Number of entries in the fnfieldlists array. */
1157 int nfnfields;
98751a41
JK
1158
1159 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1160 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1161 struct typedef_field_list
1162 {
1163 struct typedef_field field;
1164 struct typedef_field_list *next;
1165 }
1166 *typedef_field_list;
1167 unsigned typedef_field_list_count;
c5aa993b 1168 };
c906108c 1169
10b3939b
DJ
1170/* One item on the queue of compilation units to read in full symbols
1171 for. */
1172struct dwarf2_queue_item
1173{
1174 struct dwarf2_per_cu_data *per_cu;
95554aad 1175 enum language pretend_language;
10b3939b
DJ
1176 struct dwarf2_queue_item *next;
1177};
1178
1179/* The current queue. */
1180static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1181
ae038cb0
DJ
1182/* Loaded secondary compilation units are kept in memory until they
1183 have not been referenced for the processing of this many
1184 compilation units. Set this to zero to disable caching. Cache
1185 sizes of up to at least twenty will improve startup time for
1186 typical inter-CU-reference binaries, at an obvious memory cost. */
1187static int dwarf2_max_cache_age = 5;
920d2a44
AC
1188static void
1189show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1190 struct cmd_list_element *c, const char *value)
1191{
3e43a32a
MS
1192 fprintf_filtered (file, _("The upper bound on the age of cached "
1193 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1194 value);
1195}
1196
ae038cb0 1197
0963b4bd 1198/* Various complaints about symbol reading that don't abort the process. */
c906108c 1199
4d3c2250
KB
1200static void
1201dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1202{
4d3c2250 1203 complaint (&symfile_complaints,
e2e0b3e5 1204 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1205}
1206
25e43795
DJ
1207static void
1208dwarf2_debug_line_missing_file_complaint (void)
1209{
1210 complaint (&symfile_complaints,
1211 _(".debug_line section has line data without a file"));
1212}
1213
59205f5a
JB
1214static void
1215dwarf2_debug_line_missing_end_sequence_complaint (void)
1216{
1217 complaint (&symfile_complaints,
3e43a32a
MS
1218 _(".debug_line section has line "
1219 "program sequence without an end"));
59205f5a
JB
1220}
1221
4d3c2250
KB
1222static void
1223dwarf2_complex_location_expr_complaint (void)
2e276125 1224{
e2e0b3e5 1225 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1226}
1227
4d3c2250
KB
1228static void
1229dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1230 int arg3)
2e276125 1231{
4d3c2250 1232 complaint (&symfile_complaints,
3e43a32a
MS
1233 _("const value length mismatch for '%s', got %d, expected %d"),
1234 arg1, arg2, arg3);
4d3c2250
KB
1235}
1236
1237static void
f664829e 1238dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1239{
4d3c2250 1240 complaint (&symfile_complaints,
f664829e
DE
1241 _("debug info runs off end of %s section"
1242 " [in module %s]"),
1243 section->asection->name,
1244 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1245}
1246
1247static void
1248dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1249{
4d3c2250 1250 complaint (&symfile_complaints,
3e43a32a
MS
1251 _("macro debug info contains a "
1252 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1253 arg1);
1254}
1255
1256static void
1257dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1258{
4d3c2250 1259 complaint (&symfile_complaints,
3e43a32a
MS
1260 _("invalid attribute class or form for '%s' in '%s'"),
1261 arg1, arg2);
4d3c2250 1262}
c906108c 1263
c906108c
SS
1264/* local function prototypes */
1265
4efb68b1 1266static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1267
aaa75496
JB
1268static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1269 struct objfile *);
1270
918dd910
JK
1271static void dwarf2_find_base_address (struct die_info *die,
1272 struct dwarf2_cu *cu);
1273
c67a9c90 1274static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1275
72bf9492
DJ
1276static void scan_partial_symbols (struct partial_die_info *,
1277 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1278 int, struct dwarf2_cu *);
c906108c 1279
72bf9492
DJ
1280static void add_partial_symbol (struct partial_die_info *,
1281 struct dwarf2_cu *);
63d06c5c 1282
72bf9492
DJ
1283static void add_partial_namespace (struct partial_die_info *pdi,
1284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1285 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1286
5d7cb8df
JK
1287static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1288 CORE_ADDR *highpc, int need_pc,
1289 struct dwarf2_cu *cu);
1290
72bf9492
DJ
1291static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1292 struct dwarf2_cu *cu);
91c24f0a 1293
bc30ff58
JB
1294static void add_partial_subprogram (struct partial_die_info *pdi,
1295 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1296 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1297
257e7a09
YQ
1298static void dwarf2_read_symtab (struct partial_symtab *,
1299 struct objfile *);
c906108c 1300
a14ed312 1301static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1302
433df2d4
DE
1303static struct abbrev_info *abbrev_table_lookup_abbrev
1304 (const struct abbrev_table *, unsigned int);
1305
1306static struct abbrev_table *abbrev_table_read_table
1307 (struct dwarf2_section_info *, sect_offset);
1308
1309static void abbrev_table_free (struct abbrev_table *);
1310
f4dc4d17
DE
1311static void abbrev_table_free_cleanup (void *);
1312
dee91e82
DE
1313static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1314 struct dwarf2_section_info *);
c906108c 1315
f3dd6933 1316static void dwarf2_free_abbrev_table (void *);
c906108c 1317
6caca83c
CC
1318static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1319
dee91e82
DE
1320static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1322
dee91e82
DE
1323static gdb_byte *read_partial_die (const struct die_reader_specs *,
1324 struct partial_die_info *,
1325 struct abbrev_info *,
1326 unsigned int,
1327 gdb_byte *);
c906108c 1328
36586728 1329static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1330 struct dwarf2_cu *);
72bf9492
DJ
1331
1332static void fixup_partial_die (struct partial_die_info *,
1333 struct dwarf2_cu *);
1334
dee91e82
DE
1335static gdb_byte *read_attribute (const struct die_reader_specs *,
1336 struct attribute *, struct attr_abbrev *,
1337 gdb_byte *);
a8329558 1338
a1855c1d 1339static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1340
a1855c1d 1341static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1342
a1855c1d 1343static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1344
a1855c1d 1345static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1346
a1855c1d 1347static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1348
fe1b8b76 1349static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1350 unsigned int *);
c906108c 1351
c764a876
DE
1352static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1353
1354static LONGEST read_checked_initial_length_and_offset
1355 (bfd *, gdb_byte *, const struct comp_unit_head *,
1356 unsigned int *, unsigned int *);
613e1657 1357
fe1b8b76 1358static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1359 unsigned int *);
1360
1361static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1362
f4dc4d17
DE
1363static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1364 sect_offset);
1365
fe1b8b76 1366static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1367
9b1c24c8 1368static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1369
fe1b8b76
JB
1370static char *read_indirect_string (bfd *, gdb_byte *,
1371 const struct comp_unit_head *,
1372 unsigned int *);
4bdf3d34 1373
36586728
TT
1374static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1375
12df843f 1376static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1377
12df843f 1378static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1379
3019eac3
DE
1380static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1381 unsigned int *);
1382
1383static char *read_str_index (const struct die_reader_specs *reader,
1384 struct dwarf2_cu *cu, ULONGEST str_index);
1385
e142c38c 1386static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1387
e142c38c
DJ
1388static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1389 struct dwarf2_cu *);
c906108c 1390
348e048f 1391static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1392 unsigned int);
348e048f 1393
05cf31d1
JB
1394static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1395 struct dwarf2_cu *cu);
1396
e142c38c 1397static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1398
e142c38c 1399static struct die_info *die_specification (struct die_info *die,
f2f0e013 1400 struct dwarf2_cu **);
63d06c5c 1401
debd256d
JB
1402static void free_line_header (struct line_header *lh);
1403
aaa75496
JB
1404static void add_file_name (struct line_header *, char *, unsigned int,
1405 unsigned int, unsigned int);
1406
3019eac3
DE
1407static struct line_header *dwarf_decode_line_header (unsigned int offset,
1408 struct dwarf2_cu *cu);
debd256d 1409
f3f5162e
DE
1410static void dwarf_decode_lines (struct line_header *, const char *,
1411 struct dwarf2_cu *, struct partial_symtab *,
1412 int);
c906108c 1413
72b9f47f 1414static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1415
f4dc4d17 1416static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1417 const char *, const char *, CORE_ADDR);
f4dc4d17 1418
a14ed312 1419static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1420 struct dwarf2_cu *);
c906108c 1421
34eaf542
TT
1422static struct symbol *new_symbol_full (struct die_info *, struct type *,
1423 struct dwarf2_cu *, struct symbol *);
1424
a14ed312 1425static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1426 struct dwarf2_cu *);
c906108c 1427
98bfdba5
PA
1428static void dwarf2_const_value_attr (struct attribute *attr,
1429 struct type *type,
1430 const char *name,
1431 struct obstack *obstack,
12df843f 1432 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1433 gdb_byte **bytes,
1434 struct dwarf2_locexpr_baton **baton);
2df3850c 1435
e7c27a73 1436static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1437
b4ba55a1
JB
1438static int need_gnat_info (struct dwarf2_cu *);
1439
3e43a32a
MS
1440static struct type *die_descriptive_type (struct die_info *,
1441 struct dwarf2_cu *);
b4ba55a1
JB
1442
1443static void set_descriptive_type (struct type *, struct die_info *,
1444 struct dwarf2_cu *);
1445
e7c27a73
DJ
1446static struct type *die_containing_type (struct die_info *,
1447 struct dwarf2_cu *);
c906108c 1448
673bfd45
DE
1449static struct type *lookup_die_type (struct die_info *, struct attribute *,
1450 struct dwarf2_cu *);
c906108c 1451
f792889a 1452static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1453
673bfd45
DE
1454static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1455
0d5cff50 1456static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1457
6e70227d 1458static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1459 const char *suffix, int physname,
1460 struct dwarf2_cu *cu);
63d06c5c 1461
e7c27a73 1462static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1463
348e048f
DE
1464static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1465
e7c27a73 1466static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1467
e7c27a73 1468static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1469
96408a79
SA
1470static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1471
ff013f42
JK
1472static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1473 struct dwarf2_cu *, struct partial_symtab *);
1474
a14ed312 1475static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1476 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1477 struct partial_symtab *);
c906108c 1478
fae299cd
DC
1479static void get_scope_pc_bounds (struct die_info *,
1480 CORE_ADDR *, CORE_ADDR *,
1481 struct dwarf2_cu *);
1482
801e3a5b
JB
1483static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1484 CORE_ADDR, struct dwarf2_cu *);
1485
a14ed312 1486static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1487 struct dwarf2_cu *);
c906108c 1488
a14ed312 1489static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1490 struct type *, struct dwarf2_cu *);
c906108c 1491
a14ed312 1492static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1493 struct die_info *, struct type *,
e7c27a73 1494 struct dwarf2_cu *);
c906108c 1495
a14ed312 1496static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1497 struct type *,
1498 struct dwarf2_cu *);
c906108c 1499
134d01f1 1500static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1501
e7c27a73 1502static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1503
e7c27a73 1504static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1505
5d7cb8df
JK
1506static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1507
27aa8d6a
SW
1508static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1509
f55ee35c
JK
1510static struct type *read_module_type (struct die_info *die,
1511 struct dwarf2_cu *cu);
1512
38d518c9 1513static const char *namespace_name (struct die_info *die,
e142c38c 1514 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1515
134d01f1 1516static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1517
e7c27a73 1518static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1519
6e70227d 1520static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1521 struct dwarf2_cu *);
1522
dee91e82 1523static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1524 gdb_byte *info_ptr,
fe1b8b76 1525 gdb_byte **new_info_ptr,
639d11d3
DC
1526 struct die_info *parent);
1527
dee91e82 1528static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1529 gdb_byte *info_ptr,
fe1b8b76 1530 gdb_byte **new_info_ptr,
639d11d3
DC
1531 struct die_info *parent);
1532
3019eac3
DE
1533static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1534 struct die_info **, gdb_byte *, int *, int);
1535
dee91e82
DE
1536static gdb_byte *read_full_die (const struct die_reader_specs *,
1537 struct die_info **, gdb_byte *, int *);
93311388 1538
e7c27a73 1539static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1540
15d034d0
TT
1541static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1542 struct obstack *);
71c25dea 1543
15d034d0 1544static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1545
15d034d0 1546static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1547 struct die_info *die,
1548 struct dwarf2_cu *cu);
1549
e142c38c 1550static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1551 struct dwarf2_cu **);
9219021c 1552
f39c6ffd 1553static const char *dwarf_tag_name (unsigned int);
c906108c 1554
f39c6ffd 1555static const char *dwarf_attr_name (unsigned int);
c906108c 1556
f39c6ffd 1557static const char *dwarf_form_name (unsigned int);
c906108c 1558
a14ed312 1559static char *dwarf_bool_name (unsigned int);
c906108c 1560
f39c6ffd 1561static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1562
f9aca02d 1563static struct die_info *sibling_die (struct die_info *);
c906108c 1564
d97bc12b
DE
1565static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1566
1567static void dump_die_for_error (struct die_info *);
1568
1569static void dump_die_1 (struct ui_file *, int level, int max_level,
1570 struct die_info *);
c906108c 1571
d97bc12b 1572/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1573
51545339 1574static void store_in_ref_table (struct die_info *,
10b3939b 1575 struct dwarf2_cu *);
c906108c 1576
93311388
DE
1577static int is_ref_attr (struct attribute *);
1578
b64f50a1 1579static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1580
43bbcdc2 1581static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1582
348e048f
DE
1583static struct die_info *follow_die_ref_or_sig (struct die_info *,
1584 struct attribute *,
1585 struct dwarf2_cu **);
1586
10b3939b
DJ
1587static struct die_info *follow_die_ref (struct die_info *,
1588 struct attribute *,
f2f0e013 1589 struct dwarf2_cu **);
c906108c 1590
348e048f
DE
1591static struct die_info *follow_die_sig (struct die_info *,
1592 struct attribute *,
1593 struct dwarf2_cu **);
1594
6c83ed52
TT
1595static struct signatured_type *lookup_signatured_type_at_offset
1596 (struct objfile *objfile,
b64f50a1 1597 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1598
e5fe5e75 1599static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1600
52dc124a 1601static void read_signatured_type (struct signatured_type *);
348e048f 1602
f4dc4d17 1603static struct type_unit_group *get_type_unit_group
094b34ac 1604 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1605
1606static void build_type_unit_groups (die_reader_func_ftype *, void *);
1607
c906108c
SS
1608/* memory allocation interface */
1609
7b5a2f43 1610static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1611
b60c80d6 1612static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1613
09262596 1614static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1615 const char *, int);
2e276125 1616
8e19ed76
PS
1617static int attr_form_is_block (struct attribute *);
1618
3690dd37
JB
1619static int attr_form_is_section_offset (struct attribute *);
1620
1621static int attr_form_is_constant (struct attribute *);
1622
8cf6f0b1
TT
1623static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1624 struct dwarf2_loclist_baton *baton,
1625 struct attribute *attr);
1626
93e7bd98
DJ
1627static void dwarf2_symbol_mark_computed (struct attribute *attr,
1628 struct symbol *sym,
1629 struct dwarf2_cu *cu);
4c2df51b 1630
dee91e82
DE
1631static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1632 gdb_byte *info_ptr,
1633 struct abbrev_info *abbrev);
4bb7a0a7 1634
72bf9492
DJ
1635static void free_stack_comp_unit (void *);
1636
72bf9492
DJ
1637static hashval_t partial_die_hash (const void *item);
1638
1639static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1640
ae038cb0 1641static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1642 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1643
9816fde3 1644static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1645 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1646
1647static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1648 struct die_info *comp_unit_die,
1649 enum language pretend_language);
93311388 1650
68dc6402 1651static void free_heap_comp_unit (void *);
ae038cb0
DJ
1652
1653static void free_cached_comp_units (void *);
1654
1655static void age_cached_comp_units (void);
1656
dee91e82 1657static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1658
f792889a
DJ
1659static struct type *set_die_type (struct die_info *, struct type *,
1660 struct dwarf2_cu *);
1c379e20 1661
ae038cb0
DJ
1662static void create_all_comp_units (struct objfile *);
1663
0e50663e 1664static int create_all_type_units (struct objfile *);
1fd400ff 1665
95554aad
TT
1666static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1667 enum language);
10b3939b 1668
95554aad
TT
1669static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1670 enum language);
10b3939b 1671
f4dc4d17
DE
1672static void process_full_type_unit (struct dwarf2_per_cu_data *,
1673 enum language);
1674
10b3939b
DJ
1675static void dwarf2_add_dependence (struct dwarf2_cu *,
1676 struct dwarf2_per_cu_data *);
1677
ae038cb0
DJ
1678static void dwarf2_mark (struct dwarf2_cu *);
1679
1680static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1681
b64f50a1 1682static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1683 struct dwarf2_per_cu_data *per_cu);
1684
f792889a 1685static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1686
9291a0cd
TT
1687static void dwarf2_release_queue (void *dummy);
1688
95554aad
TT
1689static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1690 enum language pretend_language);
1691
1692static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1693 struct dwarf2_per_cu_data *per_cu,
1694 enum language pretend_language);
9291a0cd 1695
a0f42c21 1696static void process_queue (void);
9291a0cd
TT
1697
1698static void find_file_and_directory (struct die_info *die,
1699 struct dwarf2_cu *cu,
15d034d0 1700 const char **name, const char **comp_dir);
9291a0cd
TT
1701
1702static char *file_full_name (int file, struct line_header *lh,
1703 const char *comp_dir);
1704
36586728
TT
1705static gdb_byte *read_and_check_comp_unit_head
1706 (struct comp_unit_head *header,
1707 struct dwarf2_section_info *section,
1708 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1709 int is_debug_types_section);
1710
fd820528 1711static void init_cutu_and_read_dies
f4dc4d17
DE
1712 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1713 int use_existing_cu, int keep,
3019eac3
DE
1714 die_reader_func_ftype *die_reader_func, void *data);
1715
dee91e82
DE
1716static void init_cutu_and_read_dies_simple
1717 (struct dwarf2_per_cu_data *this_cu,
1718 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1719
673bfd45 1720static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1721
3019eac3
DE
1722static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1723
1724static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1725 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1726
1727static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1728 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1729
1730static void free_dwo_file_cleanup (void *);
1731
95554aad
TT
1732static void process_cu_includes (void);
1733
1b80a9fa
JK
1734static void check_producer (struct dwarf2_cu *cu);
1735
9291a0cd
TT
1736#if WORDS_BIGENDIAN
1737
1738/* Convert VALUE between big- and little-endian. */
1739static offset_type
1740byte_swap (offset_type value)
1741{
1742 offset_type result;
1743
1744 result = (value & 0xff) << 24;
1745 result |= (value & 0xff00) << 8;
1746 result |= (value & 0xff0000) >> 8;
1747 result |= (value & 0xff000000) >> 24;
1748 return result;
1749}
1750
1751#define MAYBE_SWAP(V) byte_swap (V)
1752
1753#else
1754#define MAYBE_SWAP(V) (V)
1755#endif /* WORDS_BIGENDIAN */
1756
1757/* The suffix for an index file. */
1758#define INDEX_SUFFIX ".gdb-index"
1759
15d034d0 1760static const char *dwarf2_physname (const char *name, struct die_info *die,
3da10d80
KS
1761 struct dwarf2_cu *cu);
1762
c906108c 1763/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1764 information and return true if we have enough to do something.
1765 NAMES points to the dwarf2 section names, or is NULL if the standard
1766 ELF names are used. */
c906108c
SS
1767
1768int
251d32d9
TG
1769dwarf2_has_info (struct objfile *objfile,
1770 const struct dwarf2_debug_sections *names)
c906108c 1771{
be391dca
TT
1772 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1773 if (!dwarf2_per_objfile)
1774 {
1775 /* Initialize per-objfile state. */
1776 struct dwarf2_per_objfile *data
1777 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1778
be391dca
TT
1779 memset (data, 0, sizeof (*data));
1780 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1781 dwarf2_per_objfile = data;
6502dd73 1782
251d32d9
TG
1783 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1784 (void *) names);
be391dca
TT
1785 dwarf2_per_objfile->objfile = objfile;
1786 }
1787 return (dwarf2_per_objfile->info.asection != NULL
1788 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1789}
1790
251d32d9
TG
1791/* When loading sections, we look either for uncompressed section or for
1792 compressed section names. */
233a11ab
CS
1793
1794static int
251d32d9
TG
1795section_is_p (const char *section_name,
1796 const struct dwarf2_section_names *names)
233a11ab 1797{
251d32d9
TG
1798 if (names->normal != NULL
1799 && strcmp (section_name, names->normal) == 0)
1800 return 1;
1801 if (names->compressed != NULL
1802 && strcmp (section_name, names->compressed) == 0)
1803 return 1;
1804 return 0;
233a11ab
CS
1805}
1806
c906108c
SS
1807/* This function is mapped across the sections and remembers the
1808 offset and size of each of the debugging sections we are interested
1809 in. */
1810
1811static void
251d32d9 1812dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1813{
251d32d9 1814 const struct dwarf2_debug_sections *names;
dc7650b8 1815 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1816
1817 if (vnames == NULL)
1818 names = &dwarf2_elf_names;
1819 else
1820 names = (const struct dwarf2_debug_sections *) vnames;
1821
dc7650b8
JK
1822 if ((aflag & SEC_HAS_CONTENTS) == 0)
1823 {
1824 }
1825 else if (section_is_p (sectp->name, &names->info))
c906108c 1826 {
dce234bc
PP
1827 dwarf2_per_objfile->info.asection = sectp;
1828 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1829 }
251d32d9 1830 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1831 {
dce234bc
PP
1832 dwarf2_per_objfile->abbrev.asection = sectp;
1833 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1834 }
251d32d9 1835 else if (section_is_p (sectp->name, &names->line))
c906108c 1836 {
dce234bc
PP
1837 dwarf2_per_objfile->line.asection = sectp;
1838 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1839 }
251d32d9 1840 else if (section_is_p (sectp->name, &names->loc))
c906108c 1841 {
dce234bc
PP
1842 dwarf2_per_objfile->loc.asection = sectp;
1843 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1844 }
251d32d9 1845 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1846 {
dce234bc
PP
1847 dwarf2_per_objfile->macinfo.asection = sectp;
1848 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1849 }
cf2c3c16
TT
1850 else if (section_is_p (sectp->name, &names->macro))
1851 {
1852 dwarf2_per_objfile->macro.asection = sectp;
1853 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1854 }
251d32d9 1855 else if (section_is_p (sectp->name, &names->str))
c906108c 1856 {
dce234bc
PP
1857 dwarf2_per_objfile->str.asection = sectp;
1858 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1859 }
3019eac3
DE
1860 else if (section_is_p (sectp->name, &names->addr))
1861 {
1862 dwarf2_per_objfile->addr.asection = sectp;
1863 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1864 }
251d32d9 1865 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1866 {
dce234bc
PP
1867 dwarf2_per_objfile->frame.asection = sectp;
1868 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1869 }
251d32d9 1870 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1871 {
dc7650b8
JK
1872 dwarf2_per_objfile->eh_frame.asection = sectp;
1873 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1874 }
251d32d9 1875 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1876 {
dce234bc
PP
1877 dwarf2_per_objfile->ranges.asection = sectp;
1878 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1879 }
251d32d9 1880 else if (section_is_p (sectp->name, &names->types))
348e048f 1881 {
8b70b953
TT
1882 struct dwarf2_section_info type_section;
1883
1884 memset (&type_section, 0, sizeof (type_section));
1885 type_section.asection = sectp;
1886 type_section.size = bfd_get_section_size (sectp);
1887
1888 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1889 &type_section);
348e048f 1890 }
251d32d9 1891 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1892 {
1893 dwarf2_per_objfile->gdb_index.asection = sectp;
1894 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1895 }
dce234bc 1896
72dca2f5
FR
1897 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1898 && bfd_section_vma (abfd, sectp) == 0)
1899 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1900}
1901
fceca515
DE
1902/* A helper function that decides whether a section is empty,
1903 or not present. */
9e0ac564
TT
1904
1905static int
1906dwarf2_section_empty_p (struct dwarf2_section_info *info)
1907{
1908 return info->asection == NULL || info->size == 0;
1909}
1910
3019eac3
DE
1911/* Read the contents of the section INFO.
1912 OBJFILE is the main object file, but not necessarily the file where
1913 the section comes from. E.g., for DWO files INFO->asection->owner
1914 is the bfd of the DWO file.
dce234bc 1915 If the section is compressed, uncompress it before returning. */
c906108c 1916
dce234bc
PP
1917static void
1918dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1919{
dce234bc 1920 asection *sectp = info->asection;
3019eac3 1921 bfd *abfd;
dce234bc
PP
1922 gdb_byte *buf, *retbuf;
1923 unsigned char header[4];
c906108c 1924
be391dca
TT
1925 if (info->readin)
1926 return;
dce234bc 1927 info->buffer = NULL;
be391dca 1928 info->readin = 1;
188dd5d6 1929
9e0ac564 1930 if (dwarf2_section_empty_p (info))
dce234bc 1931 return;
c906108c 1932
3019eac3
DE
1933 abfd = sectp->owner;
1934
4bf44c1c
TT
1935 /* If the section has relocations, we must read it ourselves.
1936 Otherwise we attach it to the BFD. */
1937 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1938 {
4bf44c1c 1939 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1940
4bf44c1c
TT
1941 /* We have to cast away const here for historical reasons.
1942 Fixing dwarf2read to be const-correct would be quite nice. */
1943 info->buffer = (gdb_byte *) bytes;
1944 return;
dce234bc 1945 }
dce234bc 1946
4bf44c1c
TT
1947 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1948 info->buffer = buf;
dce234bc
PP
1949
1950 /* When debugging .o files, we may need to apply relocations; see
1951 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1952 We never compress sections in .o files, so we only need to
1953 try this when the section is not compressed. */
ac8035ab 1954 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1955 if (retbuf != NULL)
1956 {
1957 info->buffer = retbuf;
1958 return;
1959 }
1960
1961 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1962 || bfd_bread (buf, info->size, abfd) != info->size)
1963 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1964 bfd_get_filename (abfd));
1965}
1966
9e0ac564
TT
1967/* A helper function that returns the size of a section in a safe way.
1968 If you are positive that the section has been read before using the
1969 size, then it is safe to refer to the dwarf2_section_info object's
1970 "size" field directly. In other cases, you must call this
1971 function, because for compressed sections the size field is not set
1972 correctly until the section has been read. */
1973
1974static bfd_size_type
1975dwarf2_section_size (struct objfile *objfile,
1976 struct dwarf2_section_info *info)
1977{
1978 if (!info->readin)
1979 dwarf2_read_section (objfile, info);
1980 return info->size;
1981}
1982
dce234bc 1983/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1984 SECTION_NAME. */
af34e669 1985
dce234bc 1986void
3017a003
TG
1987dwarf2_get_section_info (struct objfile *objfile,
1988 enum dwarf2_section_enum sect,
dce234bc
PP
1989 asection **sectp, gdb_byte **bufp,
1990 bfd_size_type *sizep)
1991{
1992 struct dwarf2_per_objfile *data
1993 = objfile_data (objfile, dwarf2_objfile_data_key);
1994 struct dwarf2_section_info *info;
a3b2a86b
TT
1995
1996 /* We may see an objfile without any DWARF, in which case we just
1997 return nothing. */
1998 if (data == NULL)
1999 {
2000 *sectp = NULL;
2001 *bufp = NULL;
2002 *sizep = 0;
2003 return;
2004 }
3017a003
TG
2005 switch (sect)
2006 {
2007 case DWARF2_DEBUG_FRAME:
2008 info = &data->frame;
2009 break;
2010 case DWARF2_EH_FRAME:
2011 info = &data->eh_frame;
2012 break;
2013 default:
2014 gdb_assert_not_reached ("unexpected section");
2015 }
dce234bc 2016
9e0ac564 2017 dwarf2_read_section (objfile, info);
dce234bc
PP
2018
2019 *sectp = info->asection;
2020 *bufp = info->buffer;
2021 *sizep = info->size;
2022}
2023
36586728
TT
2024/* A helper function to find the sections for a .dwz file. */
2025
2026static void
2027locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2028{
2029 struct dwz_file *dwz_file = arg;
2030
2031 /* Note that we only support the standard ELF names, because .dwz
2032 is ELF-only (at the time of writing). */
2033 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2034 {
2035 dwz_file->abbrev.asection = sectp;
2036 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2037 }
2038 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2039 {
2040 dwz_file->info.asection = sectp;
2041 dwz_file->info.size = bfd_get_section_size (sectp);
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2044 {
2045 dwz_file->str.asection = sectp;
2046 dwz_file->str.size = bfd_get_section_size (sectp);
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2049 {
2050 dwz_file->line.asection = sectp;
2051 dwz_file->line.size = bfd_get_section_size (sectp);
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2054 {
2055 dwz_file->macro.asection = sectp;
2056 dwz_file->macro.size = bfd_get_section_size (sectp);
2057 }
2ec9a5e0
TT
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2059 {
2060 dwz_file->gdb_index.asection = sectp;
2061 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2062 }
36586728
TT
2063}
2064
2065/* Open the separate '.dwz' debug file, if needed. Error if the file
2066 cannot be found. */
2067
2068static struct dwz_file *
2069dwarf2_get_dwz_file (void)
2070{
2071 bfd *abfd, *dwz_bfd;
2072 asection *section;
2073 gdb_byte *data;
2074 struct cleanup *cleanup;
2075 const char *filename;
2076 struct dwz_file *result;
2077
2078 if (dwarf2_per_objfile->dwz_file != NULL)
2079 return dwarf2_per_objfile->dwz_file;
2080
2081 abfd = dwarf2_per_objfile->objfile->obfd;
2082 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2083 if (section == NULL)
2084 error (_("could not find '.gnu_debugaltlink' section"));
2085 if (!bfd_malloc_and_get_section (abfd, section, &data))
2086 error (_("could not read '.gnu_debugaltlink' section: %s"),
2087 bfd_errmsg (bfd_get_error ()));
2088 cleanup = make_cleanup (xfree, data);
2089
2090 filename = data;
2091 if (!IS_ABSOLUTE_PATH (filename))
2092 {
2093 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2094 char *rel;
2095
2096 make_cleanup (xfree, abs);
2097 abs = ldirname (abs);
2098 make_cleanup (xfree, abs);
2099
2100 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2101 make_cleanup (xfree, rel);
2102 filename = rel;
2103 }
2104
2105 /* The format is just a NUL-terminated file name, followed by the
2106 build-id. For now, though, we ignore the build-id. */
2107 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2108 if (dwz_bfd == NULL)
2109 error (_("could not read '%s': %s"), filename,
2110 bfd_errmsg (bfd_get_error ()));
2111
2112 if (!bfd_check_format (dwz_bfd, bfd_object))
2113 {
2114 gdb_bfd_unref (dwz_bfd);
2115 error (_("file '%s' was not usable: %s"), filename,
2116 bfd_errmsg (bfd_get_error ()));
2117 }
2118
2119 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2120 struct dwz_file);
2121 result->dwz_bfd = dwz_bfd;
2122
2123 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2124
2125 do_cleanups (cleanup);
2126
8d2cc612 2127 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2128 return result;
2129}
9291a0cd 2130\f
7b9f3c50
DE
2131/* DWARF quick_symbols_functions support. */
2132
2133/* TUs can share .debug_line entries, and there can be a lot more TUs than
2134 unique line tables, so we maintain a separate table of all .debug_line
2135 derived entries to support the sharing.
2136 All the quick functions need is the list of file names. We discard the
2137 line_header when we're done and don't need to record it here. */
2138struct quick_file_names
2139{
094b34ac
DE
2140 /* The data used to construct the hash key. */
2141 struct stmt_list_hash hash;
7b9f3c50
DE
2142
2143 /* The number of entries in file_names, real_names. */
2144 unsigned int num_file_names;
2145
2146 /* The file names from the line table, after being run through
2147 file_full_name. */
2148 const char **file_names;
2149
2150 /* The file names from the line table after being run through
2151 gdb_realpath. These are computed lazily. */
2152 const char **real_names;
2153};
2154
2155/* When using the index (and thus not using psymtabs), each CU has an
2156 object of this type. This is used to hold information needed by
2157 the various "quick" methods. */
2158struct dwarf2_per_cu_quick_data
2159{
2160 /* The file table. This can be NULL if there was no file table
2161 or it's currently not read in.
2162 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2163 struct quick_file_names *file_names;
2164
2165 /* The corresponding symbol table. This is NULL if symbols for this
2166 CU have not yet been read. */
2167 struct symtab *symtab;
2168
2169 /* A temporary mark bit used when iterating over all CUs in
2170 expand_symtabs_matching. */
2171 unsigned int mark : 1;
2172
2173 /* True if we've tried to read the file table and found there isn't one.
2174 There will be no point in trying to read it again next time. */
2175 unsigned int no_file_data : 1;
2176};
2177
094b34ac
DE
2178/* Utility hash function for a stmt_list_hash. */
2179
2180static hashval_t
2181hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2182{
2183 hashval_t v = 0;
2184
2185 if (stmt_list_hash->dwo_unit != NULL)
2186 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2187 v += stmt_list_hash->line_offset.sect_off;
2188 return v;
2189}
2190
2191/* Utility equality function for a stmt_list_hash. */
2192
2193static int
2194eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2195 const struct stmt_list_hash *rhs)
2196{
2197 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2198 return 0;
2199 if (lhs->dwo_unit != NULL
2200 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2201 return 0;
2202
2203 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2204}
2205
7b9f3c50
DE
2206/* Hash function for a quick_file_names. */
2207
2208static hashval_t
2209hash_file_name_entry (const void *e)
2210{
2211 const struct quick_file_names *file_data = e;
2212
094b34ac 2213 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2214}
2215
2216/* Equality function for a quick_file_names. */
2217
2218static int
2219eq_file_name_entry (const void *a, const void *b)
2220{
2221 const struct quick_file_names *ea = a;
2222 const struct quick_file_names *eb = b;
2223
094b34ac 2224 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2225}
2226
2227/* Delete function for a quick_file_names. */
2228
2229static void
2230delete_file_name_entry (void *e)
2231{
2232 struct quick_file_names *file_data = e;
2233 int i;
2234
2235 for (i = 0; i < file_data->num_file_names; ++i)
2236 {
2237 xfree ((void*) file_data->file_names[i]);
2238 if (file_data->real_names)
2239 xfree ((void*) file_data->real_names[i]);
2240 }
2241
2242 /* The space for the struct itself lives on objfile_obstack,
2243 so we don't free it here. */
2244}
2245
2246/* Create a quick_file_names hash table. */
2247
2248static htab_t
2249create_quick_file_names_table (unsigned int nr_initial_entries)
2250{
2251 return htab_create_alloc (nr_initial_entries,
2252 hash_file_name_entry, eq_file_name_entry,
2253 delete_file_name_entry, xcalloc, xfree);
2254}
9291a0cd 2255
918dd910
JK
2256/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2257 have to be created afterwards. You should call age_cached_comp_units after
2258 processing PER_CU->CU. dw2_setup must have been already called. */
2259
2260static void
2261load_cu (struct dwarf2_per_cu_data *per_cu)
2262{
3019eac3 2263 if (per_cu->is_debug_types)
e5fe5e75 2264 load_full_type_unit (per_cu);
918dd910 2265 else
95554aad 2266 load_full_comp_unit (per_cu, language_minimal);
918dd910 2267
918dd910 2268 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2269
2270 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2271}
2272
a0f42c21 2273/* Read in the symbols for PER_CU. */
2fdf6df6 2274
9291a0cd 2275static void
a0f42c21 2276dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2277{
2278 struct cleanup *back_to;
2279
f4dc4d17
DE
2280 /* Skip type_unit_groups, reading the type units they contain
2281 is handled elsewhere. */
2282 if (IS_TYPE_UNIT_GROUP (per_cu))
2283 return;
2284
9291a0cd
TT
2285 back_to = make_cleanup (dwarf2_release_queue, NULL);
2286
95554aad
TT
2287 if (dwarf2_per_objfile->using_index
2288 ? per_cu->v.quick->symtab == NULL
2289 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2290 {
2291 queue_comp_unit (per_cu, language_minimal);
2292 load_cu (per_cu);
2293 }
9291a0cd 2294
a0f42c21 2295 process_queue ();
9291a0cd
TT
2296
2297 /* Age the cache, releasing compilation units that have not
2298 been used recently. */
2299 age_cached_comp_units ();
2300
2301 do_cleanups (back_to);
2302}
2303
2304/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2305 the objfile from which this CU came. Returns the resulting symbol
2306 table. */
2fdf6df6 2307
9291a0cd 2308static struct symtab *
a0f42c21 2309dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2310{
95554aad 2311 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2312 if (!per_cu->v.quick->symtab)
2313 {
2314 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2315 increment_reading_symtab ();
a0f42c21 2316 dw2_do_instantiate_symtab (per_cu);
95554aad 2317 process_cu_includes ();
9291a0cd
TT
2318 do_cleanups (back_to);
2319 }
2320 return per_cu->v.quick->symtab;
2321}
2322
f4dc4d17
DE
2323/* Return the CU given its index.
2324
2325 This is intended for loops like:
2326
2327 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2328 + dwarf2_per_objfile->n_type_units); ++i)
2329 {
2330 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2331
2332 ...;
2333 }
2334*/
2fdf6df6 2335
1fd400ff
TT
2336static struct dwarf2_per_cu_data *
2337dw2_get_cu (int index)
2338{
2339 if (index >= dwarf2_per_objfile->n_comp_units)
2340 {
f4dc4d17 2341 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2342 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2343 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2344 }
2345
2346 return dwarf2_per_objfile->all_comp_units[index];
2347}
2348
2349/* Return the primary CU given its index.
2350 The difference between this function and dw2_get_cu is in the handling
2351 of type units (TUs). Here we return the type_unit_group object.
2352
2353 This is intended for loops like:
2354
2355 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2356 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2357 {
2358 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2359
2360 ...;
2361 }
2362*/
2363
2364static struct dwarf2_per_cu_data *
2365dw2_get_primary_cu (int index)
2366{
2367 if (index >= dwarf2_per_objfile->n_comp_units)
2368 {
1fd400ff 2369 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2370 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2371 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2372 }
f4dc4d17 2373
1fd400ff
TT
2374 return dwarf2_per_objfile->all_comp_units[index];
2375}
2376
2ec9a5e0
TT
2377/* A helper for create_cus_from_index that handles a given list of
2378 CUs. */
2fdf6df6 2379
74a0d9f6 2380static void
2ec9a5e0
TT
2381create_cus_from_index_list (struct objfile *objfile,
2382 const gdb_byte *cu_list, offset_type n_elements,
2383 struct dwarf2_section_info *section,
2384 int is_dwz,
2385 int base_offset)
9291a0cd
TT
2386{
2387 offset_type i;
9291a0cd 2388
2ec9a5e0 2389 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2390 {
2391 struct dwarf2_per_cu_data *the_cu;
2392 ULONGEST offset, length;
2393
74a0d9f6
JK
2394 gdb_static_assert (sizeof (ULONGEST) >= 8);
2395 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2396 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2397 cu_list += 2 * 8;
2398
2399 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2400 struct dwarf2_per_cu_data);
b64f50a1 2401 the_cu->offset.sect_off = offset;
9291a0cd
TT
2402 the_cu->length = length;
2403 the_cu->objfile = objfile;
2ec9a5e0 2404 the_cu->info_or_types_section = section;
9291a0cd
TT
2405 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2406 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2407 the_cu->is_dwz = is_dwz;
2408 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2409 }
9291a0cd
TT
2410}
2411
2ec9a5e0 2412/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2413 the CU objects for this objfile. */
2ec9a5e0 2414
74a0d9f6 2415static void
2ec9a5e0
TT
2416create_cus_from_index (struct objfile *objfile,
2417 const gdb_byte *cu_list, offset_type cu_list_elements,
2418 const gdb_byte *dwz_list, offset_type dwz_elements)
2419{
2420 struct dwz_file *dwz;
2421
2422 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2423 dwarf2_per_objfile->all_comp_units
2424 = obstack_alloc (&objfile->objfile_obstack,
2425 dwarf2_per_objfile->n_comp_units
2426 * sizeof (struct dwarf2_per_cu_data *));
2427
74a0d9f6
JK
2428 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2429 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2430
2431 if (dwz_elements == 0)
74a0d9f6 2432 return;
2ec9a5e0
TT
2433
2434 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2435 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2436 cu_list_elements / 2);
2ec9a5e0
TT
2437}
2438
1fd400ff 2439/* Create the signatured type hash table from the index. */
673bfd45 2440
74a0d9f6 2441static void
673bfd45 2442create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2443 struct dwarf2_section_info *section,
673bfd45
DE
2444 const gdb_byte *bytes,
2445 offset_type elements)
1fd400ff
TT
2446{
2447 offset_type i;
673bfd45 2448 htab_t sig_types_hash;
1fd400ff 2449
d467dd73
DE
2450 dwarf2_per_objfile->n_type_units = elements / 3;
2451 dwarf2_per_objfile->all_type_units
1fd400ff 2452 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2453 dwarf2_per_objfile->n_type_units
b4dd5633 2454 * sizeof (struct signatured_type *));
1fd400ff 2455
673bfd45 2456 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2457
2458 for (i = 0; i < elements; i += 3)
2459 {
52dc124a
DE
2460 struct signatured_type *sig_type;
2461 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2462 void **slot;
2463
74a0d9f6
JK
2464 gdb_static_assert (sizeof (ULONGEST) >= 8);
2465 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2466 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2467 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2468 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2469 bytes += 3 * 8;
2470
52dc124a 2471 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2472 struct signatured_type);
52dc124a 2473 sig_type->signature = signature;
3019eac3
DE
2474 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2475 sig_type->per_cu.is_debug_types = 1;
2476 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2477 sig_type->per_cu.offset.sect_off = offset;
2478 sig_type->per_cu.objfile = objfile;
2479 sig_type->per_cu.v.quick
1fd400ff
TT
2480 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2481 struct dwarf2_per_cu_quick_data);
2482
52dc124a
DE
2483 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2484 *slot = sig_type;
1fd400ff 2485
b4dd5633 2486 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2487 }
2488
673bfd45 2489 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2490}
2491
9291a0cd
TT
2492/* Read the address map data from the mapped index, and use it to
2493 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2494
9291a0cd
TT
2495static void
2496create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2497{
2498 const gdb_byte *iter, *end;
2499 struct obstack temp_obstack;
2500 struct addrmap *mutable_map;
2501 struct cleanup *cleanup;
2502 CORE_ADDR baseaddr;
2503
2504 obstack_init (&temp_obstack);
2505 cleanup = make_cleanup_obstack_free (&temp_obstack);
2506 mutable_map = addrmap_create_mutable (&temp_obstack);
2507
2508 iter = index->address_table;
2509 end = iter + index->address_table_size;
2510
2511 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2512
2513 while (iter < end)
2514 {
2515 ULONGEST hi, lo, cu_index;
2516 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2517 iter += 8;
2518 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2519 iter += 8;
2520 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2521 iter += 4;
2522
2523 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2524 dw2_get_cu (cu_index));
9291a0cd
TT
2525 }
2526
2527 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2528 &objfile->objfile_obstack);
2529 do_cleanups (cleanup);
2530}
2531
59d7bcaf
JK
2532/* The hash function for strings in the mapped index. This is the same as
2533 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2534 implementation. This is necessary because the hash function is tied to the
2535 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2536 SYMBOL_HASH_NEXT.
2537
2538 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2539
9291a0cd 2540static hashval_t
559a7a62 2541mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2542{
2543 const unsigned char *str = (const unsigned char *) p;
2544 hashval_t r = 0;
2545 unsigned char c;
2546
2547 while ((c = *str++) != 0)
559a7a62
JK
2548 {
2549 if (index_version >= 5)
2550 c = tolower (c);
2551 r = r * 67 + c - 113;
2552 }
9291a0cd
TT
2553
2554 return r;
2555}
2556
2557/* Find a slot in the mapped index INDEX for the object named NAME.
2558 If NAME is found, set *VEC_OUT to point to the CU vector in the
2559 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2560
9291a0cd
TT
2561static int
2562find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2563 offset_type **vec_out)
2564{
0cf03b49
JK
2565 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2566 offset_type hash;
9291a0cd 2567 offset_type slot, step;
559a7a62 2568 int (*cmp) (const char *, const char *);
9291a0cd 2569
0cf03b49
JK
2570 if (current_language->la_language == language_cplus
2571 || current_language->la_language == language_java
2572 || current_language->la_language == language_fortran)
2573 {
2574 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2575 not contain any. */
2576 const char *paren = strchr (name, '(');
2577
2578 if (paren)
2579 {
2580 char *dup;
2581
2582 dup = xmalloc (paren - name + 1);
2583 memcpy (dup, name, paren - name);
2584 dup[paren - name] = 0;
2585
2586 make_cleanup (xfree, dup);
2587 name = dup;
2588 }
2589 }
2590
559a7a62 2591 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2592 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2593 simulate our NAME being searched is also lowercased. */
2594 hash = mapped_index_string_hash ((index->version == 4
2595 && case_sensitivity == case_sensitive_off
2596 ? 5 : index->version),
2597 name);
2598
3876f04e
DE
2599 slot = hash & (index->symbol_table_slots - 1);
2600 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2601 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2602
2603 for (;;)
2604 {
2605 /* Convert a slot number to an offset into the table. */
2606 offset_type i = 2 * slot;
2607 const char *str;
3876f04e 2608 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2609 {
2610 do_cleanups (back_to);
2611 return 0;
2612 }
9291a0cd 2613
3876f04e 2614 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2615 if (!cmp (name, str))
9291a0cd
TT
2616 {
2617 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2618 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2619 do_cleanups (back_to);
9291a0cd
TT
2620 return 1;
2621 }
2622
3876f04e 2623 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2624 }
2625}
2626
2ec9a5e0
TT
2627/* A helper function that reads the .gdb_index from SECTION and fills
2628 in MAP. FILENAME is the name of the file containing the section;
2629 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2630 ok to use deprecated sections.
2631
2632 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2633 out parameters that are filled in with information about the CU and
2634 TU lists in the section.
2635
2636 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2637
9291a0cd 2638static int
2ec9a5e0
TT
2639read_index_from_section (struct objfile *objfile,
2640 const char *filename,
2641 int deprecated_ok,
2642 struct dwarf2_section_info *section,
2643 struct mapped_index *map,
2644 const gdb_byte **cu_list,
2645 offset_type *cu_list_elements,
2646 const gdb_byte **types_list,
2647 offset_type *types_list_elements)
9291a0cd 2648{
9291a0cd 2649 char *addr;
2ec9a5e0 2650 offset_type version;
b3b272e1 2651 offset_type *metadata;
1fd400ff 2652 int i;
9291a0cd 2653
2ec9a5e0 2654 if (dwarf2_section_empty_p (section))
9291a0cd 2655 return 0;
82430852
JK
2656
2657 /* Older elfutils strip versions could keep the section in the main
2658 executable while splitting it for the separate debug info file. */
2ec9a5e0 2659 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2660 return 0;
2661
2ec9a5e0 2662 dwarf2_read_section (objfile, section);
9291a0cd 2663
2ec9a5e0 2664 addr = section->buffer;
9291a0cd 2665 /* Version check. */
1fd400ff 2666 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2667 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2668 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2669 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2670 indices. */
831adc1f 2671 if (version < 4)
481860b3
GB
2672 {
2673 static int warning_printed = 0;
2674 if (!warning_printed)
2675 {
2676 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2677 filename);
481860b3
GB
2678 warning_printed = 1;
2679 }
2680 return 0;
2681 }
2682 /* Index version 4 uses a different hash function than index version
2683 5 and later.
2684
2685 Versions earlier than 6 did not emit psymbols for inlined
2686 functions. Using these files will cause GDB not to be able to
2687 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2688 indices unless the user has done
2689 "set use-deprecated-index-sections on". */
2ec9a5e0 2690 if (version < 6 && !deprecated_ok)
481860b3
GB
2691 {
2692 static int warning_printed = 0;
2693 if (!warning_printed)
2694 {
e615022a
DE
2695 warning (_("\
2696Skipping deprecated .gdb_index section in %s.\n\
2697Do \"set use-deprecated-index-sections on\" before the file is read\n\
2698to use the section anyway."),
2ec9a5e0 2699 filename);
481860b3
GB
2700 warning_printed = 1;
2701 }
2702 return 0;
2703 }
796a7ff8
DE
2704 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2705 of the TU (for symbols coming from TUs). It's just a performance bug, and
2706 we can't distinguish gdb-generated indices from gold-generated ones, so
2707 nothing to do here. */
2708
481860b3 2709 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2710 longer backward compatible. */
796a7ff8 2711 if (version > 8)
594e8718 2712 return 0;
9291a0cd 2713
559a7a62 2714 map->version = version;
2ec9a5e0 2715 map->total_size = section->size;
9291a0cd
TT
2716
2717 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2718
2719 i = 0;
2ec9a5e0
TT
2720 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2721 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2722 / 8);
1fd400ff
TT
2723 ++i;
2724
2ec9a5e0
TT
2725 *types_list = addr + MAYBE_SWAP (metadata[i]);
2726 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2727 - MAYBE_SWAP (metadata[i]))
2728 / 8);
987d643c 2729 ++i;
1fd400ff
TT
2730
2731 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2732 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2733 - MAYBE_SWAP (metadata[i]));
2734 ++i;
2735
3876f04e
DE
2736 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2737 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2738 - MAYBE_SWAP (metadata[i]))
2739 / (2 * sizeof (offset_type)));
1fd400ff 2740 ++i;
9291a0cd 2741
1fd400ff
TT
2742 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2743
2ec9a5e0
TT
2744 return 1;
2745}
2746
2747
2748/* Read the index file. If everything went ok, initialize the "quick"
2749 elements of all the CUs and return 1. Otherwise, return 0. */
2750
2751static int
2752dwarf2_read_index (struct objfile *objfile)
2753{
2754 struct mapped_index local_map, *map;
2755 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2756 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2757
2758 if (!read_index_from_section (objfile, objfile->name,
2759 use_deprecated_index_sections,
2760 &dwarf2_per_objfile->gdb_index, &local_map,
2761 &cu_list, &cu_list_elements,
2762 &types_list, &types_list_elements))
2763 return 0;
2764
0fefef59 2765 /* Don't use the index if it's empty. */
2ec9a5e0 2766 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2767 return 0;
2768
2ec9a5e0
TT
2769 /* If there is a .dwz file, read it so we can get its CU list as
2770 well. */
2771 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2772 {
2773 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2774 struct mapped_index dwz_map;
2775 const gdb_byte *dwz_types_ignore;
2776 offset_type dwz_types_elements_ignore;
2777
2778 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2779 1,
2780 &dwz->gdb_index, &dwz_map,
2781 &dwz_list, &dwz_list_elements,
2782 &dwz_types_ignore,
2783 &dwz_types_elements_ignore))
2784 {
2785 warning (_("could not read '.gdb_index' section from %s; skipping"),
2786 bfd_get_filename (dwz->dwz_bfd));
2787 return 0;
2788 }
2789 }
2790
74a0d9f6
JK
2791 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2792 dwz_list_elements);
1fd400ff 2793
8b70b953
TT
2794 if (types_list_elements)
2795 {
2796 struct dwarf2_section_info *section;
2797
2798 /* We can only handle a single .debug_types when we have an
2799 index. */
2800 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2801 return 0;
2802
2803 section = VEC_index (dwarf2_section_info_def,
2804 dwarf2_per_objfile->types, 0);
2805
74a0d9f6
JK
2806 create_signatured_type_table_from_index (objfile, section, types_list,
2807 types_list_elements);
8b70b953 2808 }
9291a0cd 2809
2ec9a5e0
TT
2810 create_addrmap_from_index (objfile, &local_map);
2811
2812 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2813 *map = local_map;
9291a0cd
TT
2814
2815 dwarf2_per_objfile->index_table = map;
2816 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2817 dwarf2_per_objfile->quick_file_names_table =
2818 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2819
2820 return 1;
2821}
2822
2823/* A helper for the "quick" functions which sets the global
2824 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2825
9291a0cd
TT
2826static void
2827dw2_setup (struct objfile *objfile)
2828{
2829 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2830 gdb_assert (dwarf2_per_objfile);
2831}
2832
dee91e82 2833/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2834
dee91e82
DE
2835static void
2836dw2_get_file_names_reader (const struct die_reader_specs *reader,
2837 gdb_byte *info_ptr,
2838 struct die_info *comp_unit_die,
2839 int has_children,
2840 void *data)
9291a0cd 2841{
dee91e82
DE
2842 struct dwarf2_cu *cu = reader->cu;
2843 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2844 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2845 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2846 struct line_header *lh;
9291a0cd 2847 struct attribute *attr;
dee91e82 2848 int i;
15d034d0 2849 const char *name, *comp_dir;
7b9f3c50
DE
2850 void **slot;
2851 struct quick_file_names *qfn;
2852 unsigned int line_offset;
9291a0cd 2853
07261596
TT
2854 /* Our callers never want to match partial units -- instead they
2855 will match the enclosing full CU. */
2856 if (comp_unit_die->tag == DW_TAG_partial_unit)
2857 {
2858 this_cu->v.quick->no_file_data = 1;
2859 return;
2860 }
2861
094b34ac
DE
2862 /* If we're reading the line header for TUs, store it in the "per_cu"
2863 for tu_group. */
2864 if (this_cu->is_debug_types)
2865 {
2866 struct type_unit_group *tu_group = data;
2867
2868 gdb_assert (tu_group != NULL);
2869 lh_cu = &tu_group->per_cu;
2870 }
2871 else
2872 lh_cu = this_cu;
2873
7b9f3c50
DE
2874 lh = NULL;
2875 slot = NULL;
2876 line_offset = 0;
dee91e82
DE
2877
2878 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2879 if (attr)
2880 {
7b9f3c50
DE
2881 struct quick_file_names find_entry;
2882
2883 line_offset = DW_UNSND (attr);
2884
2885 /* We may have already read in this line header (TU line header sharing).
2886 If we have we're done. */
094b34ac
DE
2887 find_entry.hash.dwo_unit = cu->dwo_unit;
2888 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2889 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2890 &find_entry, INSERT);
2891 if (*slot != NULL)
2892 {
094b34ac 2893 lh_cu->v.quick->file_names = *slot;
dee91e82 2894 return;
7b9f3c50
DE
2895 }
2896
3019eac3 2897 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2898 }
2899 if (lh == NULL)
2900 {
094b34ac 2901 lh_cu->v.quick->no_file_data = 1;
dee91e82 2902 return;
9291a0cd
TT
2903 }
2904
7b9f3c50 2905 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2906 qfn->hash.dwo_unit = cu->dwo_unit;
2907 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2908 gdb_assert (slot != NULL);
2909 *slot = qfn;
9291a0cd 2910
dee91e82 2911 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2912
7b9f3c50
DE
2913 qfn->num_file_names = lh->num_file_names;
2914 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2915 lh->num_file_names * sizeof (char *));
9291a0cd 2916 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2917 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2918 qfn->real_names = NULL;
9291a0cd 2919
7b9f3c50 2920 free_line_header (lh);
7b9f3c50 2921
094b34ac 2922 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2923}
2924
2925/* A helper for the "quick" functions which attempts to read the line
2926 table for THIS_CU. */
2927
2928static struct quick_file_names *
2929dw2_get_file_names (struct objfile *objfile,
2930 struct dwarf2_per_cu_data *this_cu)
2931{
f4dc4d17
DE
2932 /* For TUs this should only be called on the parent group. */
2933 if (this_cu->is_debug_types)
2934 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2935
dee91e82
DE
2936 if (this_cu->v.quick->file_names != NULL)
2937 return this_cu->v.quick->file_names;
2938 /* If we know there is no line data, no point in looking again. */
2939 if (this_cu->v.quick->no_file_data)
2940 return NULL;
2941
3019eac3
DE
2942 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2943 in the stub for CUs, there's is no need to lookup the DWO file.
2944 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2945 DWO file. */
2946 if (this_cu->is_debug_types)
094b34ac 2947 {
796a7ff8 2948 struct type_unit_group *tu_group = this_cu->type_unit_group;
094b34ac
DE
2949
2950 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2951 dw2_get_file_names_reader, tu_group);
2952 }
3019eac3
DE
2953 else
2954 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2955
2956 if (this_cu->v.quick->no_file_data)
2957 return NULL;
2958 return this_cu->v.quick->file_names;
9291a0cd
TT
2959}
2960
2961/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2962 real path for a given file name from the line table. */
2fdf6df6 2963
9291a0cd 2964static const char *
7b9f3c50
DE
2965dw2_get_real_path (struct objfile *objfile,
2966 struct quick_file_names *qfn, int index)
9291a0cd 2967{
7b9f3c50
DE
2968 if (qfn->real_names == NULL)
2969 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2970 qfn->num_file_names, sizeof (char *));
9291a0cd 2971
7b9f3c50
DE
2972 if (qfn->real_names[index] == NULL)
2973 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2974
7b9f3c50 2975 return qfn->real_names[index];
9291a0cd
TT
2976}
2977
2978static struct symtab *
2979dw2_find_last_source_symtab (struct objfile *objfile)
2980{
2981 int index;
ae2de4f8 2982
9291a0cd
TT
2983 dw2_setup (objfile);
2984 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2985 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2986}
2987
7b9f3c50
DE
2988/* Traversal function for dw2_forget_cached_source_info. */
2989
2990static int
2991dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2992{
7b9f3c50 2993 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2994
7b9f3c50 2995 if (file_data->real_names)
9291a0cd 2996 {
7b9f3c50 2997 int i;
9291a0cd 2998
7b9f3c50 2999 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3000 {
7b9f3c50
DE
3001 xfree ((void*) file_data->real_names[i]);
3002 file_data->real_names[i] = NULL;
9291a0cd
TT
3003 }
3004 }
7b9f3c50
DE
3005
3006 return 1;
3007}
3008
3009static void
3010dw2_forget_cached_source_info (struct objfile *objfile)
3011{
3012 dw2_setup (objfile);
3013
3014 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3015 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3016}
3017
f8eba3c6
TT
3018/* Helper function for dw2_map_symtabs_matching_filename that expands
3019 the symtabs and calls the iterator. */
3020
3021static int
3022dw2_map_expand_apply (struct objfile *objfile,
3023 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3024 const char *name, const char *real_path,
f8eba3c6
TT
3025 int (*callback) (struct symtab *, void *),
3026 void *data)
3027{
3028 struct symtab *last_made = objfile->symtabs;
3029
3030 /* Don't visit already-expanded CUs. */
3031 if (per_cu->v.quick->symtab)
3032 return 0;
3033
3034 /* This may expand more than one symtab, and we want to iterate over
3035 all of them. */
a0f42c21 3036 dw2_instantiate_symtab (per_cu);
f8eba3c6 3037
f5b95b50 3038 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3039 objfile->symtabs, last_made);
3040}
3041
3042/* Implementation of the map_symtabs_matching_filename method. */
3043
9291a0cd 3044static int
f8eba3c6 3045dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3046 const char *real_path,
f8eba3c6
TT
3047 int (*callback) (struct symtab *, void *),
3048 void *data)
9291a0cd
TT
3049{
3050 int i;
c011a4f4 3051 const char *name_basename = lbasename (name);
9291a0cd
TT
3052
3053 dw2_setup (objfile);
ae2de4f8 3054
848e3e78
DE
3055 /* The rule is CUs specify all the files, including those used by
3056 any TU, so there's no need to scan TUs here. */
f4dc4d17 3057
848e3e78 3058 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3059 {
3060 int j;
f4dc4d17 3061 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3062 struct quick_file_names *file_data;
9291a0cd 3063
3d7bb9d9 3064 /* We only need to look at symtabs not already expanded. */
e254ef6a 3065 if (per_cu->v.quick->symtab)
9291a0cd
TT
3066 continue;
3067
7b9f3c50
DE
3068 file_data = dw2_get_file_names (objfile, per_cu);
3069 if (file_data == NULL)
9291a0cd
TT
3070 continue;
3071
7b9f3c50 3072 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3073 {
7b9f3c50 3074 const char *this_name = file_data->file_names[j];
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;
288e77a7 3082 continue;
4aac40c8 3083 }
9291a0cd 3084
c011a4f4
DE
3085 /* Before we invoke realpath, which can get expensive when many
3086 files are involved, do a quick comparison of the basenames. */
3087 if (! basenames_may_differ
3088 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3089 continue;
3090
da235a7c
JK
3091 this_real_name = dw2_get_real_path (objfile, file_data, j);
3092 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3093 {
da235a7c
JK
3094 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3095 callback, data))
3096 return 1;
288e77a7 3097 continue;
da235a7c 3098 }
9291a0cd 3099
da235a7c
JK
3100 if (real_path != NULL)
3101 {
af529f8f
JK
3102 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3103 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3104 if (this_real_name != NULL
af529f8f 3105 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3106 {
f5b95b50 3107 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3108 callback, data))
3109 return 1;
288e77a7 3110 continue;
9291a0cd
TT
3111 }
3112 }
3113 }
3114 }
3115
9291a0cd
TT
3116 return 0;
3117}
3118
da51c347
DE
3119/* Struct used to manage iterating over all CUs looking for a symbol. */
3120
3121struct dw2_symtab_iterator
9291a0cd 3122{
da51c347
DE
3123 /* The internalized form of .gdb_index. */
3124 struct mapped_index *index;
3125 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3126 int want_specific_block;
3127 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3128 Unused if !WANT_SPECIFIC_BLOCK. */
3129 int block_index;
3130 /* The kind of symbol we're looking for. */
3131 domain_enum domain;
3132 /* The list of CUs from the index entry of the symbol,
3133 or NULL if not found. */
3134 offset_type *vec;
3135 /* The next element in VEC to look at. */
3136 int next;
3137 /* The number of elements in VEC, or zero if there is no match. */
3138 int length;
3139};
9291a0cd 3140
da51c347
DE
3141/* Initialize the index symtab iterator ITER.
3142 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3143 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3144
9291a0cd 3145static void
da51c347
DE
3146dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3147 struct mapped_index *index,
3148 int want_specific_block,
3149 int block_index,
3150 domain_enum domain,
3151 const char *name)
3152{
3153 iter->index = index;
3154 iter->want_specific_block = want_specific_block;
3155 iter->block_index = block_index;
3156 iter->domain = domain;
3157 iter->next = 0;
3158
3159 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3160 iter->length = MAYBE_SWAP (*iter->vec);
3161 else
3162 {
3163 iter->vec = NULL;
3164 iter->length = 0;
3165 }
3166}
3167
3168/* Return the next matching CU or NULL if there are no more. */
3169
3170static struct dwarf2_per_cu_data *
3171dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3172{
3173 for ( ; iter->next < iter->length; ++iter->next)
3174 {
3175 offset_type cu_index_and_attrs =
3176 MAYBE_SWAP (iter->vec[iter->next + 1]);
3177 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3178 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3179 int want_static = iter->block_index != GLOBAL_BLOCK;
3180 /* This value is only valid for index versions >= 7. */
3181 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3182 gdb_index_symbol_kind symbol_kind =
3183 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3184 /* Only check the symbol attributes if they're present.
3185 Indices prior to version 7 don't record them,
3186 and indices >= 7 may elide them for certain symbols
3187 (gold does this). */
3188 int attrs_valid =
3189 (iter->index->version >= 7
3190 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3191
3192 /* Skip if already read in. */
3193 if (per_cu->v.quick->symtab)
3194 continue;
3195
3196 if (attrs_valid
3197 && iter->want_specific_block
3198 && want_static != is_static)
3199 continue;
3200
3201 /* Only check the symbol's kind if it has one. */
3202 if (attrs_valid)
3203 {
3204 switch (iter->domain)
3205 {
3206 case VAR_DOMAIN:
3207 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3208 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3209 /* Some types are also in VAR_DOMAIN. */
3210 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3211 continue;
3212 break;
3213 case STRUCT_DOMAIN:
3214 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3215 continue;
3216 break;
3217 case LABEL_DOMAIN:
3218 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3219 continue;
3220 break;
3221 default:
3222 break;
3223 }
3224 }
3225
3226 ++iter->next;
3227 return per_cu;
3228 }
3229
3230 return NULL;
3231}
3232
3233static struct symtab *
3234dw2_lookup_symbol (struct objfile *objfile, int block_index,
3235 const char *name, domain_enum domain)
9291a0cd 3236{
da51c347 3237 struct symtab *stab_best = NULL;
156942c7
DE
3238 struct mapped_index *index;
3239
9291a0cd
TT
3240 dw2_setup (objfile);
3241
156942c7
DE
3242 index = dwarf2_per_objfile->index_table;
3243
da51c347 3244 /* index is NULL if OBJF_READNOW. */
156942c7 3245 if (index)
9291a0cd 3246 {
da51c347
DE
3247 struct dw2_symtab_iterator iter;
3248 struct dwarf2_per_cu_data *per_cu;
3249
3250 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3251
da51c347 3252 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3253 {
da51c347
DE
3254 struct symbol *sym = NULL;
3255 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3256
3257 /* Some caution must be observed with overloaded functions
3258 and methods, since the index will not contain any overload
3259 information (but NAME might contain it). */
3260 if (stab->primary)
9291a0cd 3261 {
da51c347
DE
3262 struct blockvector *bv = BLOCKVECTOR (stab);
3263 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3264
da51c347
DE
3265 sym = lookup_block_symbol (block, name, domain);
3266 }
1fd400ff 3267
da51c347
DE
3268 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3269 {
3270 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3271 return stab;
3272
3273 stab_best = stab;
9291a0cd 3274 }
da51c347
DE
3275
3276 /* Keep looking through other CUs. */
9291a0cd
TT
3277 }
3278 }
9291a0cd 3279
da51c347 3280 return stab_best;
9291a0cd
TT
3281}
3282
3283static void
3284dw2_print_stats (struct objfile *objfile)
3285{
3286 int i, count;
3287
3288 dw2_setup (objfile);
3289 count = 0;
1fd400ff 3290 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3291 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3292 {
e254ef6a 3293 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3294
e254ef6a 3295 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3296 ++count;
3297 }
3298 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3299}
3300
3301static void
3302dw2_dump (struct objfile *objfile)
3303{
3304 /* Nothing worth printing. */
3305}
3306
3307static void
3308dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3309 struct section_offsets *delta)
3310{
3311 /* There's nothing to relocate here. */
3312}
3313
3314static void
3315dw2_expand_symtabs_for_function (struct objfile *objfile,
3316 const char *func_name)
3317{
da51c347
DE
3318 struct mapped_index *index;
3319
3320 dw2_setup (objfile);
3321
3322 index = dwarf2_per_objfile->index_table;
3323
3324 /* index is NULL if OBJF_READNOW. */
3325 if (index)
3326 {
3327 struct dw2_symtab_iterator iter;
3328 struct dwarf2_per_cu_data *per_cu;
3329
3330 /* Note: It doesn't matter what we pass for block_index here. */
3331 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3332 func_name);
3333
3334 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3335 dw2_instantiate_symtab (per_cu);
3336 }
9291a0cd
TT
3337}
3338
3339static void
3340dw2_expand_all_symtabs (struct objfile *objfile)
3341{
3342 int i;
3343
3344 dw2_setup (objfile);
1fd400ff
TT
3345
3346 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3347 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3348 {
e254ef6a 3349 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3350
a0f42c21 3351 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3352 }
3353}
3354
3355static void
652a8996
JK
3356dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3357 const char *fullname)
9291a0cd
TT
3358{
3359 int i;
3360
3361 dw2_setup (objfile);
d4637a04
DE
3362
3363 /* We don't need to consider type units here.
3364 This is only called for examining code, e.g. expand_line_sal.
3365 There can be an order of magnitude (or more) more type units
3366 than comp units, and we avoid them if we can. */
3367
3368 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3369 {
3370 int j;
e254ef6a 3371 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3372 struct quick_file_names *file_data;
9291a0cd 3373
3d7bb9d9 3374 /* We only need to look at symtabs not already expanded. */
e254ef6a 3375 if (per_cu->v.quick->symtab)
9291a0cd
TT
3376 continue;
3377
7b9f3c50
DE
3378 file_data = dw2_get_file_names (objfile, per_cu);
3379 if (file_data == NULL)
9291a0cd
TT
3380 continue;
3381
7b9f3c50 3382 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3383 {
652a8996
JK
3384 const char *this_fullname = file_data->file_names[j];
3385
3386 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3387 {
a0f42c21 3388 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3389 break;
3390 }
3391 }
3392 }
3393}
3394
356d9f9d
TT
3395/* A helper function for dw2_find_symbol_file that finds the primary
3396 file name for a given CU. This is a die_reader_func. */
3397
3398static void
3399dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3400 gdb_byte *info_ptr,
3401 struct die_info *comp_unit_die,
3402 int has_children,
3403 void *data)
3404{
3405 const char **result_ptr = data;
3406 struct dwarf2_cu *cu = reader->cu;
3407 struct attribute *attr;
3408
3409 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3410 if (attr == NULL)
3411 *result_ptr = NULL;
3412 else
3413 *result_ptr = DW_STRING (attr);
3414}
3415
dd786858 3416static const char *
9291a0cd
TT
3417dw2_find_symbol_file (struct objfile *objfile, const char *name)
3418{
e254ef6a 3419 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3420 offset_type *vec;
356d9f9d 3421 const char *filename;
9291a0cd
TT
3422
3423 dw2_setup (objfile);
3424
ae2de4f8 3425 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3426 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3427 {
3428 struct symtab *s;
3429
d790cf0a
DE
3430 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3431 {
3432 struct blockvector *bv = BLOCKVECTOR (s);
3433 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3434 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3435
3436 if (sym)
652a8996
JK
3437 {
3438 /* Only file extension of returned filename is recognized. */
3439 return SYMBOL_SYMTAB (sym)->filename;
3440 }
d790cf0a 3441 }
96408a79
SA
3442 return NULL;
3443 }
9291a0cd
TT
3444
3445 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3446 name, &vec))
3447 return NULL;
3448
3449 /* Note that this just looks at the very first one named NAME -- but
3450 actually we are looking for a function. find_main_filename
3451 should be rewritten so that it doesn't require a custom hook. It
3452 could just use the ordinary symbol tables. */
3453 /* vec[0] is the length, which must always be >0. */
156942c7 3454 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3455
356d9f9d 3456 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3457 {
3458 /* Only file extension of returned filename is recognized. */
3459 return per_cu->v.quick->symtab->filename;
3460 }
356d9f9d 3461
f4dc4d17
DE
3462 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3463 dw2_get_primary_filename_reader, &filename);
9291a0cd 3464
652a8996 3465 /* Only file extension of returned filename is recognized. */
356d9f9d 3466 return filename;
9291a0cd
TT
3467}
3468
3469static void
40658b94
PH
3470dw2_map_matching_symbols (const char * name, domain_enum namespace,
3471 struct objfile *objfile, int global,
3472 int (*callback) (struct block *,
3473 struct symbol *, void *),
2edb89d3
JK
3474 void *data, symbol_compare_ftype *match,
3475 symbol_compare_ftype *ordered_compare)
9291a0cd 3476{
40658b94 3477 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3478 current language is Ada for a non-Ada objfile using GNU index. As Ada
3479 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3480}
3481
3482static void
f8eba3c6
TT
3483dw2_expand_symtabs_matching
3484 (struct objfile *objfile,
fbd9ab74 3485 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3486 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3487 enum search_domain kind,
3488 void *data)
9291a0cd
TT
3489{
3490 int i;
3491 offset_type iter;
4b5246aa 3492 struct mapped_index *index;
9291a0cd
TT
3493
3494 dw2_setup (objfile);
ae2de4f8
DE
3495
3496 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3497 if (!dwarf2_per_objfile->index_table)
3498 return;
4b5246aa 3499 index = dwarf2_per_objfile->index_table;
9291a0cd 3500
7b08b9eb 3501 if (file_matcher != NULL)
24c79950
TT
3502 {
3503 struct cleanup *cleanup;
3504 htab_t visited_found, visited_not_found;
3505
3506 visited_found = htab_create_alloc (10,
3507 htab_hash_pointer, htab_eq_pointer,
3508 NULL, xcalloc, xfree);
3509 cleanup = make_cleanup_htab_delete (visited_found);
3510 visited_not_found = htab_create_alloc (10,
3511 htab_hash_pointer, htab_eq_pointer,
3512 NULL, xcalloc, xfree);
3513 make_cleanup_htab_delete (visited_not_found);
3514
848e3e78
DE
3515 /* The rule is CUs specify all the files, including those used by
3516 any TU, so there's no need to scan TUs here. */
3517
3518 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3519 {
3520 int j;
f4dc4d17 3521 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3522 struct quick_file_names *file_data;
3523 void **slot;
7b08b9eb 3524
24c79950 3525 per_cu->v.quick->mark = 0;
3d7bb9d9 3526
24c79950
TT
3527 /* We only need to look at symtabs not already expanded. */
3528 if (per_cu->v.quick->symtab)
3529 continue;
7b08b9eb 3530
24c79950
TT
3531 file_data = dw2_get_file_names (objfile, per_cu);
3532 if (file_data == NULL)
3533 continue;
7b08b9eb 3534
24c79950
TT
3535 if (htab_find (visited_not_found, file_data) != NULL)
3536 continue;
3537 else if (htab_find (visited_found, file_data) != NULL)
3538 {
3539 per_cu->v.quick->mark = 1;
3540 continue;
3541 }
3542
3543 for (j = 0; j < file_data->num_file_names; ++j)
3544 {
da235a7c
JK
3545 const char *this_real_name;
3546
fbd9ab74 3547 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3548 {
3549 per_cu->v.quick->mark = 1;
3550 break;
3551 }
da235a7c
JK
3552
3553 /* Before we invoke realpath, which can get expensive when many
3554 files are involved, do a quick comparison of the basenames. */
3555 if (!basenames_may_differ
3556 && !file_matcher (lbasename (file_data->file_names[j]),
3557 data, 1))
3558 continue;
3559
3560 this_real_name = dw2_get_real_path (objfile, file_data, j);
3561 if (file_matcher (this_real_name, data, 0))
3562 {
3563 per_cu->v.quick->mark = 1;
3564 break;
3565 }
24c79950
TT
3566 }
3567
3568 slot = htab_find_slot (per_cu->v.quick->mark
3569 ? visited_found
3570 : visited_not_found,
3571 file_data, INSERT);
3572 *slot = file_data;
3573 }
3574
3575 do_cleanups (cleanup);
3576 }
9291a0cd 3577
3876f04e 3578 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3579 {
3580 offset_type idx = 2 * iter;
3581 const char *name;
3582 offset_type *vec, vec_len, vec_idx;
3583
3876f04e 3584 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3585 continue;
3586
3876f04e 3587 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3588
e078317b 3589 if (! (*name_matcher) (name, data))
9291a0cd
TT
3590 continue;
3591
3592 /* The name was matched, now expand corresponding CUs that were
3593 marked. */
4b5246aa 3594 vec = (offset_type *) (index->constant_pool
3876f04e 3595 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3596 vec_len = MAYBE_SWAP (vec[0]);
3597 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3598 {
e254ef6a 3599 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3600 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3601 gdb_index_symbol_kind symbol_kind =
3602 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3603 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3604
3605 /* Don't crash on bad data. */
3606 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3607 + dwarf2_per_objfile->n_type_units))
156942c7 3608 continue;
1fd400ff 3609
156942c7
DE
3610 /* Only check the symbol's kind if it has one.
3611 Indices prior to version 7 don't record it. */
3612 if (index->version >= 7)
3613 {
3614 switch (kind)
3615 {
3616 case VARIABLES_DOMAIN:
3617 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3618 continue;
3619 break;
3620 case FUNCTIONS_DOMAIN:
3621 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3622 continue;
3623 break;
3624 case TYPES_DOMAIN:
3625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3626 continue;
3627 break;
3628 default:
3629 break;
3630 }
3631 }
3632
3633 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3634 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3635 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3636 }
3637 }
3638}
3639
9703b513
TT
3640/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3641 symtab. */
3642
3643static struct symtab *
3644recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3645{
3646 int i;
3647
3648 if (BLOCKVECTOR (symtab) != NULL
3649 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3650 return symtab;
3651
a3ec0bb1
DE
3652 if (symtab->includes == NULL)
3653 return NULL;
3654
9703b513
TT
3655 for (i = 0; symtab->includes[i]; ++i)
3656 {
a3ec0bb1 3657 struct symtab *s = symtab->includes[i];
9703b513
TT
3658
3659 s = recursively_find_pc_sect_symtab (s, pc);
3660 if (s != NULL)
3661 return s;
3662 }
3663
3664 return NULL;
3665}
3666
9291a0cd
TT
3667static struct symtab *
3668dw2_find_pc_sect_symtab (struct objfile *objfile,
3669 struct minimal_symbol *msymbol,
3670 CORE_ADDR pc,
3671 struct obj_section *section,
3672 int warn_if_readin)
3673{
3674 struct dwarf2_per_cu_data *data;
9703b513 3675 struct symtab *result;
9291a0cd
TT
3676
3677 dw2_setup (objfile);
3678
3679 if (!objfile->psymtabs_addrmap)
3680 return NULL;
3681
3682 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3683 if (!data)
3684 return NULL;
3685
3686 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3687 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3688 paddress (get_objfile_arch (objfile), pc));
3689
9703b513
TT
3690 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3691 gdb_assert (result != NULL);
3692 return result;
9291a0cd
TT
3693}
3694
9291a0cd 3695static void
44b13c5a 3696dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3697 void *data, int need_fullname)
9291a0cd
TT
3698{
3699 int i;
24c79950
TT
3700 struct cleanup *cleanup;
3701 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3702 NULL, xcalloc, xfree);
9291a0cd 3703
24c79950 3704 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3705 dw2_setup (objfile);
ae2de4f8 3706
848e3e78
DE
3707 /* The rule is CUs specify all the files, including those used by
3708 any TU, so there's no need to scan TUs here.
3709 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3710
848e3e78 3711 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3712 {
3713 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3714
3715 if (per_cu->v.quick->symtab)
3716 {
3717 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3718 INSERT);
3719
3720 *slot = per_cu->v.quick->file_names;
3721 }
3722 }
3723
848e3e78 3724 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3725 {
3726 int j;
f4dc4d17 3727 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3728 struct quick_file_names *file_data;
24c79950 3729 void **slot;
9291a0cd 3730
3d7bb9d9 3731 /* We only need to look at symtabs not already expanded. */
e254ef6a 3732 if (per_cu->v.quick->symtab)
9291a0cd
TT
3733 continue;
3734
7b9f3c50
DE
3735 file_data = dw2_get_file_names (objfile, per_cu);
3736 if (file_data == NULL)
9291a0cd
TT
3737 continue;
3738
24c79950
TT
3739 slot = htab_find_slot (visited, file_data, INSERT);
3740 if (*slot)
3741 {
3742 /* Already visited. */
3743 continue;
3744 }
3745 *slot = file_data;
3746
7b9f3c50 3747 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3748 {
74e2f255
DE
3749 const char *this_real_name;
3750
3751 if (need_fullname)
3752 this_real_name = dw2_get_real_path (objfile, file_data, j);
3753 else
3754 this_real_name = NULL;
7b9f3c50 3755 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3756 }
3757 }
24c79950
TT
3758
3759 do_cleanups (cleanup);
9291a0cd
TT
3760}
3761
3762static int
3763dw2_has_symbols (struct objfile *objfile)
3764{
3765 return 1;
3766}
3767
3768const struct quick_symbol_functions dwarf2_gdb_index_functions =
3769{
3770 dw2_has_symbols,
3771 dw2_find_last_source_symtab,
3772 dw2_forget_cached_source_info,
f8eba3c6 3773 dw2_map_symtabs_matching_filename,
9291a0cd 3774 dw2_lookup_symbol,
9291a0cd
TT
3775 dw2_print_stats,
3776 dw2_dump,
3777 dw2_relocate,
3778 dw2_expand_symtabs_for_function,
3779 dw2_expand_all_symtabs,
652a8996 3780 dw2_expand_symtabs_with_fullname,
9291a0cd 3781 dw2_find_symbol_file,
40658b94 3782 dw2_map_matching_symbols,
9291a0cd
TT
3783 dw2_expand_symtabs_matching,
3784 dw2_find_pc_sect_symtab,
9291a0cd
TT
3785 dw2_map_symbol_filenames
3786};
3787
3788/* Initialize for reading DWARF for this objfile. Return 0 if this
3789 file will use psymtabs, or 1 if using the GNU index. */
3790
3791int
3792dwarf2_initialize_objfile (struct objfile *objfile)
3793{
3794 /* If we're about to read full symbols, don't bother with the
3795 indices. In this case we also don't care if some other debug
3796 format is making psymtabs, because they are all about to be
3797 expanded anyway. */
3798 if ((objfile->flags & OBJF_READNOW))
3799 {
3800 int i;
3801
3802 dwarf2_per_objfile->using_index = 1;
3803 create_all_comp_units (objfile);
0e50663e 3804 create_all_type_units (objfile);
7b9f3c50
DE
3805 dwarf2_per_objfile->quick_file_names_table =
3806 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3807
1fd400ff 3808 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3809 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3810 {
e254ef6a 3811 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3812
e254ef6a
DE
3813 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3814 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3815 }
3816
3817 /* Return 1 so that gdb sees the "quick" functions. However,
3818 these functions will be no-ops because we will have expanded
3819 all symtabs. */
3820 return 1;
3821 }
3822
3823 if (dwarf2_read_index (objfile))
3824 return 1;
3825
9291a0cd
TT
3826 return 0;
3827}
3828
3829\f
3830
dce234bc
PP
3831/* Build a partial symbol table. */
3832
3833void
f29dff0a 3834dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3835{
c9bf0622
TT
3836 volatile struct gdb_exception except;
3837
f29dff0a 3838 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3839 {
3840 init_psymbol_list (objfile, 1024);
3841 }
3842
c9bf0622
TT
3843 TRY_CATCH (except, RETURN_MASK_ERROR)
3844 {
3845 /* This isn't really ideal: all the data we allocate on the
3846 objfile's obstack is still uselessly kept around. However,
3847 freeing it seems unsafe. */
3848 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3849
3850 dwarf2_build_psymtabs_hard (objfile);
3851 discard_cleanups (cleanups);
3852 }
3853 if (except.reason < 0)
3854 exception_print (gdb_stderr, except);
c906108c 3855}
c906108c 3856
1ce1cefd
DE
3857/* Return the total length of the CU described by HEADER. */
3858
3859static unsigned int
3860get_cu_length (const struct comp_unit_head *header)
3861{
3862 return header->initial_length_size + header->length;
3863}
3864
45452591
DE
3865/* Return TRUE if OFFSET is within CU_HEADER. */
3866
3867static inline int
b64f50a1 3868offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3869{
b64f50a1 3870 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3871 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3872
b64f50a1 3873 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3874}
3875
3b80fe9b
DE
3876/* Find the base address of the compilation unit for range lists and
3877 location lists. It will normally be specified by DW_AT_low_pc.
3878 In DWARF-3 draft 4, the base address could be overridden by
3879 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3880 compilation units with discontinuous ranges. */
3881
3882static void
3883dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3884{
3885 struct attribute *attr;
3886
3887 cu->base_known = 0;
3888 cu->base_address = 0;
3889
3890 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3891 if (attr)
3892 {
3893 cu->base_address = DW_ADDR (attr);
3894 cu->base_known = 1;
3895 }
3896 else
3897 {
3898 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3899 if (attr)
3900 {
3901 cu->base_address = DW_ADDR (attr);
3902 cu->base_known = 1;
3903 }
3904 }
3905}
3906
93311388
DE
3907/* Read in the comp unit header information from the debug_info at info_ptr.
3908 NOTE: This leaves members offset, first_die_offset to be filled in
3909 by the caller. */
107d2387 3910
fe1b8b76 3911static gdb_byte *
107d2387 3912read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3913 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3914{
3915 int signed_addr;
891d2f0b 3916 unsigned int bytes_read;
c764a876
DE
3917
3918 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3919 cu_header->initial_length_size = bytes_read;
3920 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3921 info_ptr += bytes_read;
107d2387
AC
3922 cu_header->version = read_2_bytes (abfd, info_ptr);
3923 info_ptr += 2;
b64f50a1
JK
3924 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3925 &bytes_read);
613e1657 3926 info_ptr += bytes_read;
107d2387
AC
3927 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3928 info_ptr += 1;
3929 signed_addr = bfd_get_sign_extend_vma (abfd);
3930 if (signed_addr < 0)
8e65ff28 3931 internal_error (__FILE__, __LINE__,
e2e0b3e5 3932 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3933 cu_header->signed_addr_p = signed_addr;
c764a876 3934
107d2387
AC
3935 return info_ptr;
3936}
3937
36586728
TT
3938/* Helper function that returns the proper abbrev section for
3939 THIS_CU. */
3940
3941static struct dwarf2_section_info *
3942get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3943{
3944 struct dwarf2_section_info *abbrev;
3945
3946 if (this_cu->is_dwz)
3947 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3948 else
3949 abbrev = &dwarf2_per_objfile->abbrev;
3950
3951 return abbrev;
3952}
3953
9ff913ba
DE
3954/* Subroutine of read_and_check_comp_unit_head and
3955 read_and_check_type_unit_head to simplify them.
3956 Perform various error checking on the header. */
3957
3958static void
3959error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3960 struct dwarf2_section_info *section,
3961 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3962{
3963 bfd *abfd = section->asection->owner;
3964 const char *filename = bfd_get_filename (abfd);
3965
3966 if (header->version != 2 && header->version != 3 && header->version != 4)
3967 error (_("Dwarf Error: wrong version in compilation unit header "
3968 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3969 filename);
3970
b64f50a1 3971 if (header->abbrev_offset.sect_off
36586728 3972 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3973 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3974 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3975 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3976 filename);
3977
3978 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3979 avoid potential 32-bit overflow. */
1ce1cefd 3980 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3981 > section->size)
3982 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3983 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3984 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3985 filename);
3986}
3987
3988/* Read in a CU/TU header and perform some basic error checking.
3989 The contents of the header are stored in HEADER.
3990 The result is a pointer to the start of the first DIE. */
adabb602 3991
fe1b8b76 3992static gdb_byte *
9ff913ba
DE
3993read_and_check_comp_unit_head (struct comp_unit_head *header,
3994 struct dwarf2_section_info *section,
4bdcc0c1 3995 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3996 gdb_byte *info_ptr,
3997 int is_debug_types_section)
72bf9492 3998{
fe1b8b76 3999 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4000 bfd *abfd = section->asection->owner;
72bf9492 4001
b64f50a1 4002 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4003
72bf9492
DJ
4004 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4005
460c1c54
CC
4006 /* If we're reading a type unit, skip over the signature and
4007 type_offset fields. */
b0df02fd 4008 if (is_debug_types_section)
460c1c54
CC
4009 info_ptr += 8 /*signature*/ + header->offset_size;
4010
b64f50a1 4011 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4012
4bdcc0c1 4013 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4014
4015 return info_ptr;
4016}
4017
348e048f
DE
4018/* Read in the types comp unit header information from .debug_types entry at
4019 types_ptr. The result is a pointer to one past the end of the header. */
4020
4021static gdb_byte *
9ff913ba
DE
4022read_and_check_type_unit_head (struct comp_unit_head *header,
4023 struct dwarf2_section_info *section,
4bdcc0c1 4024 struct dwarf2_section_info *abbrev_section,
9ff913ba 4025 gdb_byte *info_ptr,
dee91e82
DE
4026 ULONGEST *signature,
4027 cu_offset *type_offset_in_tu)
348e048f 4028{
9ff913ba
DE
4029 gdb_byte *beg_of_comp_unit = info_ptr;
4030 bfd *abfd = section->asection->owner;
348e048f 4031
b64f50a1 4032 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4033
9ff913ba 4034 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4035
9ff913ba
DE
4036 /* If we're reading a type unit, skip over the signature and
4037 type_offset fields. */
4038 if (signature != NULL)
4039 *signature = read_8_bytes (abfd, info_ptr);
4040 info_ptr += 8;
dee91e82
DE
4041 if (type_offset_in_tu != NULL)
4042 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4043 header->offset_size);
9ff913ba
DE
4044 info_ptr += header->offset_size;
4045
b64f50a1 4046 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4047
4bdcc0c1 4048 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4049
4050 return info_ptr;
348e048f
DE
4051}
4052
f4dc4d17
DE
4053/* Fetch the abbreviation table offset from a comp or type unit header. */
4054
4055static sect_offset
4056read_abbrev_offset (struct dwarf2_section_info *section,
4057 sect_offset offset)
4058{
4059 bfd *abfd = section->asection->owner;
4060 gdb_byte *info_ptr;
4061 unsigned int length, initial_length_size, offset_size;
4062 sect_offset abbrev_offset;
4063
4064 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4065 info_ptr = section->buffer + offset.sect_off;
4066 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4067 offset_size = initial_length_size == 4 ? 4 : 8;
4068 info_ptr += initial_length_size + 2 /*version*/;
4069 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4070 return abbrev_offset;
4071}
4072
aaa75496
JB
4073/* Allocate a new partial symtab for file named NAME and mark this new
4074 partial symtab as being an include of PST. */
4075
4076static void
4077dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4078 struct objfile *objfile)
4079{
4080 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4081
fbd9ab74
JK
4082 if (!IS_ABSOLUTE_PATH (subpst->filename))
4083 {
4084 /* It shares objfile->objfile_obstack. */
4085 subpst->dirname = pst->dirname;
4086 }
4087
aaa75496
JB
4088 subpst->section_offsets = pst->section_offsets;
4089 subpst->textlow = 0;
4090 subpst->texthigh = 0;
4091
4092 subpst->dependencies = (struct partial_symtab **)
4093 obstack_alloc (&objfile->objfile_obstack,
4094 sizeof (struct partial_symtab *));
4095 subpst->dependencies[0] = pst;
4096 subpst->number_of_dependencies = 1;
4097
4098 subpst->globals_offset = 0;
4099 subpst->n_global_syms = 0;
4100 subpst->statics_offset = 0;
4101 subpst->n_static_syms = 0;
4102 subpst->symtab = NULL;
4103 subpst->read_symtab = pst->read_symtab;
4104 subpst->readin = 0;
4105
4106 /* No private part is necessary for include psymtabs. This property
4107 can be used to differentiate between such include psymtabs and
10b3939b 4108 the regular ones. */
58a9656e 4109 subpst->read_symtab_private = NULL;
aaa75496
JB
4110}
4111
4112/* Read the Line Number Program data and extract the list of files
4113 included by the source file represented by PST. Build an include
d85a05f0 4114 partial symtab for each of these included files. */
aaa75496
JB
4115
4116static void
4117dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4118 struct die_info *die,
4119 struct partial_symtab *pst)
aaa75496 4120{
d85a05f0
DJ
4121 struct line_header *lh = NULL;
4122 struct attribute *attr;
aaa75496 4123
d85a05f0
DJ
4124 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4125 if (attr)
3019eac3 4126 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4127 if (lh == NULL)
4128 return; /* No linetable, so no includes. */
4129
c6da4cef 4130 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4131 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4132
4133 free_line_header (lh);
4134}
4135
348e048f 4136static hashval_t
52dc124a 4137hash_signatured_type (const void *item)
348e048f 4138{
52dc124a 4139 const struct signatured_type *sig_type = item;
9a619af0 4140
348e048f 4141 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4142 return sig_type->signature;
348e048f
DE
4143}
4144
4145static int
52dc124a 4146eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4147{
4148 const struct signatured_type *lhs = item_lhs;
4149 const struct signatured_type *rhs = item_rhs;
9a619af0 4150
348e048f
DE
4151 return lhs->signature == rhs->signature;
4152}
4153
1fd400ff
TT
4154/* Allocate a hash table for signatured types. */
4155
4156static htab_t
673bfd45 4157allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4158{
4159 return htab_create_alloc_ex (41,
52dc124a
DE
4160 hash_signatured_type,
4161 eq_signatured_type,
1fd400ff
TT
4162 NULL,
4163 &objfile->objfile_obstack,
4164 hashtab_obstack_allocate,
4165 dummy_obstack_deallocate);
4166}
4167
d467dd73 4168/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4169
4170static int
d467dd73 4171add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4172{
4173 struct signatured_type *sigt = *slot;
b4dd5633 4174 struct signatured_type ***datap = datum;
1fd400ff 4175
b4dd5633 4176 **datap = sigt;
1fd400ff
TT
4177 ++*datap;
4178
4179 return 1;
4180}
4181
3019eac3 4182/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4183 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4184 NULL otherwise.
4185 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4186 The result is a pointer to the hash table or NULL if there are
4187 no types. */
348e048f 4188
3019eac3
DE
4189static htab_t
4190create_debug_types_hash_table (struct dwo_file *dwo_file,
4191 VEC (dwarf2_section_info_def) *types)
348e048f 4192{
3019eac3 4193 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4194 htab_t types_htab = NULL;
8b70b953
TT
4195 int ix;
4196 struct dwarf2_section_info *section;
4bdcc0c1 4197 struct dwarf2_section_info *abbrev_section;
348e048f 4198
3019eac3
DE
4199 if (VEC_empty (dwarf2_section_info_def, types))
4200 return NULL;
348e048f 4201
4bdcc0c1
DE
4202 abbrev_section = (dwo_file != NULL
4203 ? &dwo_file->sections.abbrev
4204 : &dwarf2_per_objfile->abbrev);
4205
09406207
DE
4206 if (dwarf2_read_debug)
4207 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4208 dwo_file ? ".dwo" : "",
4209 bfd_get_filename (abbrev_section->asection->owner));
4210
8b70b953 4211 for (ix = 0;
3019eac3 4212 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4213 ++ix)
4214 {
3019eac3 4215 bfd *abfd;
8b70b953 4216 gdb_byte *info_ptr, *end_ptr;
36586728 4217 struct dwarf2_section_info *abbrev_section;
348e048f 4218
8b70b953
TT
4219 dwarf2_read_section (objfile, section);
4220 info_ptr = section->buffer;
348e048f 4221
8b70b953
TT
4222 if (info_ptr == NULL)
4223 continue;
348e048f 4224
3019eac3
DE
4225 /* We can't set abfd until now because the section may be empty or
4226 not present, in which case section->asection will be NULL. */
4227 abfd = section->asection->owner;
4228
36586728
TT
4229 if (dwo_file)
4230 abbrev_section = &dwo_file->sections.abbrev;
4231 else
4232 abbrev_section = &dwarf2_per_objfile->abbrev;
4233
8b70b953 4234 if (types_htab == NULL)
3019eac3
DE
4235 {
4236 if (dwo_file)
4237 types_htab = allocate_dwo_unit_table (objfile);
4238 else
4239 types_htab = allocate_signatured_type_table (objfile);
4240 }
348e048f 4241
dee91e82
DE
4242 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4243 because we don't need to read any dies: the signature is in the
4244 header. */
8b70b953
TT
4245
4246 end_ptr = info_ptr + section->size;
4247 while (info_ptr < end_ptr)
4248 {
b64f50a1 4249 sect_offset offset;
3019eac3 4250 cu_offset type_offset_in_tu;
8b70b953 4251 ULONGEST signature;
52dc124a 4252 struct signatured_type *sig_type;
3019eac3 4253 struct dwo_unit *dwo_tu;
8b70b953
TT
4254 void **slot;
4255 gdb_byte *ptr = info_ptr;
9ff913ba 4256 struct comp_unit_head header;
dee91e82 4257 unsigned int length;
348e048f 4258
b64f50a1 4259 offset.sect_off = ptr - section->buffer;
348e048f 4260
8b70b953 4261 /* We need to read the type's signature in order to build the hash
9ff913ba 4262 table, but we don't need anything else just yet. */
348e048f 4263
4bdcc0c1
DE
4264 ptr = read_and_check_type_unit_head (&header, section,
4265 abbrev_section, ptr,
3019eac3 4266 &signature, &type_offset_in_tu);
6caca83c 4267
1ce1cefd 4268 length = get_cu_length (&header);
dee91e82 4269
6caca83c 4270 /* Skip dummy type units. */
dee91e82
DE
4271 if (ptr >= info_ptr + length
4272 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4273 {
1ce1cefd 4274 info_ptr += length;
6caca83c
CC
4275 continue;
4276 }
8b70b953 4277
3019eac3
DE
4278 if (dwo_file)
4279 {
4280 sig_type = NULL;
4281 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4282 struct dwo_unit);
4283 dwo_tu->dwo_file = dwo_file;
4284 dwo_tu->signature = signature;
4285 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4286 dwo_tu->info_or_types_section = section;
4287 dwo_tu->offset = offset;
4288 dwo_tu->length = length;
4289 }
4290 else
4291 {
4292 /* N.B.: type_offset is not usable if this type uses a DWO file.
4293 The real type_offset is in the DWO file. */
4294 dwo_tu = NULL;
4295 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4296 struct signatured_type);
4297 sig_type->signature = signature;
4298 sig_type->type_offset_in_tu = type_offset_in_tu;
4299 sig_type->per_cu.objfile = objfile;
4300 sig_type->per_cu.is_debug_types = 1;
4301 sig_type->per_cu.info_or_types_section = section;
4302 sig_type->per_cu.offset = offset;
4303 sig_type->per_cu.length = length;
4304 }
8b70b953 4305
3019eac3
DE
4306 slot = htab_find_slot (types_htab,
4307 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4308 INSERT);
8b70b953
TT
4309 gdb_assert (slot != NULL);
4310 if (*slot != NULL)
4311 {
3019eac3
DE
4312 sect_offset dup_offset;
4313
4314 if (dwo_file)
4315 {
4316 const struct dwo_unit *dup_tu = *slot;
4317
4318 dup_offset = dup_tu->offset;
4319 }
4320 else
4321 {
4322 const struct signatured_type *dup_tu = *slot;
4323
4324 dup_offset = dup_tu->per_cu.offset;
4325 }
b3c8eb43 4326
8b70b953
TT
4327 complaint (&symfile_complaints,
4328 _("debug type entry at offset 0x%x is duplicate to the "
4329 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4330 offset.sect_off, dup_offset.sect_off,
8b70b953 4331 phex (signature, sizeof (signature)));
8b70b953 4332 }
3019eac3 4333 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4334
09406207 4335 if (dwarf2_read_debug)
8b70b953 4336 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4337 offset.sect_off,
4338 phex (signature, sizeof (signature)));
348e048f 4339
dee91e82 4340 info_ptr += length;
8b70b953 4341 }
348e048f
DE
4342 }
4343
3019eac3
DE
4344 return types_htab;
4345}
4346
4347/* Create the hash table of all entries in the .debug_types section,
4348 and initialize all_type_units.
4349 The result is zero if there is an error (e.g. missing .debug_types section),
4350 otherwise non-zero. */
4351
4352static int
4353create_all_type_units (struct objfile *objfile)
4354{
4355 htab_t types_htab;
b4dd5633 4356 struct signatured_type **iter;
3019eac3
DE
4357
4358 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4359 if (types_htab == NULL)
4360 {
4361 dwarf2_per_objfile->signatured_types = NULL;
4362 return 0;
4363 }
4364
348e048f
DE
4365 dwarf2_per_objfile->signatured_types = types_htab;
4366
d467dd73
DE
4367 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4368 dwarf2_per_objfile->all_type_units
1fd400ff 4369 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4370 dwarf2_per_objfile->n_type_units
b4dd5633 4371 * sizeof (struct signatured_type *));
d467dd73
DE
4372 iter = &dwarf2_per_objfile->all_type_units[0];
4373 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4374 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4375 == dwarf2_per_objfile->n_type_units);
1fd400ff 4376
348e048f
DE
4377 return 1;
4378}
4379
380bca97 4380/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4381 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4382
4383static struct signatured_type *
e319fa28 4384lookup_signatured_type (ULONGEST sig)
348e048f
DE
4385{
4386 struct signatured_type find_entry, *entry;
4387
4388 if (dwarf2_per_objfile->signatured_types == NULL)
4389 {
4390 complaint (&symfile_complaints,
55f1336d 4391 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4392 return NULL;
348e048f
DE
4393 }
4394
4395 find_entry.signature = sig;
4396 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4397 return entry;
4398}
42e7ad6c
DE
4399\f
4400/* Low level DIE reading support. */
348e048f 4401
d85a05f0
DJ
4402/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4403
4404static void
4405init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4406 struct dwarf2_cu *cu,
3019eac3
DE
4407 struct dwarf2_section_info *section,
4408 struct dwo_file *dwo_file)
d85a05f0 4409{
fceca515 4410 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4411 reader->abfd = section->asection->owner;
d85a05f0 4412 reader->cu = cu;
3019eac3 4413 reader->dwo_file = dwo_file;
dee91e82
DE
4414 reader->die_section = section;
4415 reader->buffer = section->buffer;
f664829e 4416 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4417}
4418
fd820528 4419/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4420 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4421
f4dc4d17
DE
4422 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4423 Otherwise the table specified in the comp unit header is read in and used.
4424 This is an optimization for when we already have the abbrev table.
4425
dee91e82
DE
4426 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4427 Otherwise, a new CU is allocated with xmalloc.
4428
4429 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4430 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4431
4432 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4433 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4434
70221824 4435static void
fd820528 4436init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4437 struct abbrev_table *abbrev_table,
fd820528
DE
4438 int use_existing_cu, int keep,
4439 die_reader_func_ftype *die_reader_func,
4440 void *data)
c906108c 4441{
dee91e82 4442 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4443 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4444 bfd *abfd = section->asection->owner;
dee91e82
DE
4445 struct dwarf2_cu *cu;
4446 gdb_byte *begin_info_ptr, *info_ptr;
4447 struct die_reader_specs reader;
d85a05f0 4448 struct die_info *comp_unit_die;
dee91e82 4449 int has_children;
d85a05f0 4450 struct attribute *attr;
dee91e82
DE
4451 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4452 struct signatured_type *sig_type = NULL;
4bdcc0c1 4453 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4454 /* Non-zero if CU currently points to a DWO file and we need to
4455 reread it. When this happens we need to reread the skeleton die
4456 before we can reread the DWO file. */
4457 int rereading_dwo_cu = 0;
c906108c 4458
09406207
DE
4459 if (dwarf2_die_debug)
4460 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4461 this_cu->is_debug_types ? "type" : "comp",
4462 this_cu->offset.sect_off);
4463
dee91e82
DE
4464 if (use_existing_cu)
4465 gdb_assert (keep);
23745b47 4466
dee91e82
DE
4467 cleanups = make_cleanup (null_cleanup, NULL);
4468
4469 /* This is cheap if the section is already read in. */
4470 dwarf2_read_section (objfile, section);
4471
4472 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4473
4474 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4475
4476 if (use_existing_cu && this_cu->cu != NULL)
4477 {
4478 cu = this_cu->cu;
42e7ad6c
DE
4479
4480 /* If this CU is from a DWO file we need to start over, we need to
4481 refetch the attributes from the skeleton CU.
4482 This could be optimized by retrieving those attributes from when we
4483 were here the first time: the previous comp_unit_die was stored in
4484 comp_unit_obstack. But there's no data yet that we need this
4485 optimization. */
4486 if (cu->dwo_unit != NULL)
4487 rereading_dwo_cu = 1;
dee91e82
DE
4488 }
4489 else
4490 {
4491 /* If !use_existing_cu, this_cu->cu must be NULL. */
4492 gdb_assert (this_cu->cu == NULL);
4493
4494 cu = xmalloc (sizeof (*cu));
4495 init_one_comp_unit (cu, this_cu);
4496
4497 /* If an error occurs while loading, release our storage. */
4498 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4499 }
dee91e82 4500
42e7ad6c
DE
4501 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4502 {
4503 /* We already have the header, there's no need to read it in again. */
4504 info_ptr += cu->header.first_die_offset.cu_off;
4505 }
4506 else
4507 {
3019eac3 4508 if (this_cu->is_debug_types)
dee91e82
DE
4509 {
4510 ULONGEST signature;
42e7ad6c 4511 cu_offset type_offset_in_tu;
dee91e82 4512
4bdcc0c1
DE
4513 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4514 abbrev_section, info_ptr,
42e7ad6c
DE
4515 &signature,
4516 &type_offset_in_tu);
dee91e82 4517
42e7ad6c
DE
4518 /* Since per_cu is the first member of struct signatured_type,
4519 we can go from a pointer to one to a pointer to the other. */
4520 sig_type = (struct signatured_type *) this_cu;
4521 gdb_assert (sig_type->signature == signature);
4522 gdb_assert (sig_type->type_offset_in_tu.cu_off
4523 == type_offset_in_tu.cu_off);
dee91e82
DE
4524 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4525
42e7ad6c
DE
4526 /* LENGTH has not been set yet for type units if we're
4527 using .gdb_index. */
1ce1cefd 4528 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4529
4530 /* Establish the type offset that can be used to lookup the type. */
4531 sig_type->type_offset_in_section.sect_off =
4532 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4533 }
4534 else
4535 {
4bdcc0c1
DE
4536 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4537 abbrev_section,
4538 info_ptr, 0);
dee91e82
DE
4539
4540 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4541 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4542 }
4543 }
10b3939b 4544
6caca83c 4545 /* Skip dummy compilation units. */
dee91e82 4546 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4547 || peek_abbrev_code (abfd, info_ptr) == 0)
4548 {
dee91e82 4549 do_cleanups (cleanups);
21b2bd31 4550 return;
6caca83c
CC
4551 }
4552
433df2d4
DE
4553 /* If we don't have them yet, read the abbrevs for this compilation unit.
4554 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4555 done. Note that it's important that if the CU had an abbrev table
4556 on entry we don't free it when we're done: Somewhere up the call stack
4557 it may be in use. */
f4dc4d17
DE
4558 if (abbrev_table != NULL)
4559 {
4560 gdb_assert (cu->abbrev_table == NULL);
4561 gdb_assert (cu->header.abbrev_offset.sect_off
4562 == abbrev_table->offset.sect_off);
4563 cu->abbrev_table = abbrev_table;
4564 }
4565 else if (cu->abbrev_table == NULL)
dee91e82 4566 {
4bdcc0c1 4567 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4568 make_cleanup (dwarf2_free_abbrev_table, cu);
4569 }
42e7ad6c
DE
4570 else if (rereading_dwo_cu)
4571 {
4572 dwarf2_free_abbrev_table (cu);
4573 dwarf2_read_abbrevs (cu, abbrev_section);
4574 }
af703f96 4575
dee91e82 4576 /* Read the top level CU/TU die. */
3019eac3 4577 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4578 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4579
3019eac3
DE
4580 /* If we have a DWO stub, process it and then read in the DWO file.
4581 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4582 a DWO CU, that this test will fail. */
4583 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4584 if (attr)
4585 {
15d034d0 4586 const char *dwo_name = DW_STRING (attr);
42e7ad6c 4587 const char *comp_dir_string;
3019eac3
DE
4588 struct dwo_unit *dwo_unit;
4589 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4590 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4591 int i,num_extra_attrs;
4bdcc0c1 4592 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4593
4594 if (has_children)
4595 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4596 " has children (offset 0x%x) [in module %s]"),
4597 this_cu->offset.sect_off, bfd_get_filename (abfd));
4598
4599 /* These attributes aren't processed until later:
4600 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4601 However, the attribute is found in the stub which we won't have later.
4602 In order to not impose this complication on the rest of the code,
4603 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4604
4605 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4606 DWO file. */
42e7ad6c 4607 stmt_list = NULL;
3019eac3
DE
4608 if (! this_cu->is_debug_types)
4609 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4610 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4611 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4612 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4613 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4614
4615 /* There should be a DW_AT_addr_base attribute here (if needed).
4616 We need the value before we can process DW_FORM_GNU_addr_index. */
4617 cu->addr_base = 0;
3019eac3
DE
4618 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4619 if (attr)
2e3cf129
DE
4620 cu->addr_base = DW_UNSND (attr);
4621
4622 /* There should be a DW_AT_ranges_base attribute here (if needed).
4623 We need the value before we can process DW_AT_ranges. */
4624 cu->ranges_base = 0;
4625 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4626 if (attr)
4627 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4628
4629 if (this_cu->is_debug_types)
4630 {
4631 gdb_assert (sig_type != NULL);
4632 signature = sig_type->signature;
4633 }
4634 else
4635 {
4636 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4637 if (! attr)
4638 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4639 dwo_name);
4640 signature = DW_UNSND (attr);
4641 }
4642
4643 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4644 comp_dir_string = NULL;
4645 if (comp_dir)
4646 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4647
4648 if (this_cu->is_debug_types)
42e7ad6c 4649 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4650 else
42e7ad6c 4651 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4652 signature);
4653
4654 if (dwo_unit == NULL)
4655 {
4656 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4657 " with ID %s [in module %s]"),
4658 this_cu->offset.sect_off,
4659 phex (signature, sizeof (signature)),
4660 objfile->name);
4661 }
4662
4663 /* Set up for reading the DWO CU/TU. */
4664 cu->dwo_unit = dwo_unit;
4665 section = dwo_unit->info_or_types_section;
80626a55 4666 dwarf2_read_section (objfile, section);
3019eac3 4667 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4668 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4669 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4670
4671 if (this_cu->is_debug_types)
4672 {
4673 ULONGEST signature;
80626a55 4674 cu_offset type_offset_in_tu;
3019eac3 4675
4bdcc0c1
DE
4676 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4677 dwo_abbrev_section,
4678 info_ptr,
80626a55
DE
4679 &signature,
4680 &type_offset_in_tu);
3019eac3
DE
4681 gdb_assert (sig_type->signature == signature);
4682 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4683 /* For DWOs coming from DWP files, we don't know the CU length
4684 nor the type's offset in the TU until now. */
4685 dwo_unit->length = get_cu_length (&cu->header);
4686 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4687
4688 /* Establish the type offset that can be used to lookup the type.
4689 For DWO files, we don't know it until now. */
4690 sig_type->type_offset_in_section.sect_off =
4691 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4692 }
4693 else
4694 {
4bdcc0c1
DE
4695 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4696 dwo_abbrev_section,
4697 info_ptr, 0);
3019eac3 4698 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4699 /* For DWOs coming from DWP files, we don't know the CU length
4700 until now. */
4701 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4702 }
4703
4704 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4705 if (abbrev_table == NULL)
4706 {
4707 dwarf2_free_abbrev_table (cu);
4708 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4709 }
4710 else
4711 {
4712 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4713 make_cleanup (dwarf2_free_abbrev_table, cu);
4714 }
3019eac3
DE
4715
4716 /* Read in the die, but leave space to copy over the attributes
4717 from the stub. This has the benefit of simplifying the rest of
4718 the code - all the real work is done here. */
4719 num_extra_attrs = ((stmt_list != NULL)
4720 + (low_pc != NULL)
4721 + (high_pc != NULL)
42e7ad6c
DE
4722 + (ranges != NULL)
4723 + (comp_dir != NULL));
3019eac3
DE
4724 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4725 &has_children, num_extra_attrs);
4726
4727 /* Copy over the attributes from the stub to the DWO die. */
4728 i = comp_unit_die->num_attrs;
4729 if (stmt_list != NULL)
4730 comp_unit_die->attrs[i++] = *stmt_list;
4731 if (low_pc != NULL)
4732 comp_unit_die->attrs[i++] = *low_pc;
4733 if (high_pc != NULL)
4734 comp_unit_die->attrs[i++] = *high_pc;
4735 if (ranges != NULL)
4736 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4737 if (comp_dir != NULL)
4738 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4739 comp_unit_die->num_attrs += num_extra_attrs;
4740
4741 /* Skip dummy compilation units. */
4742 if (info_ptr >= begin_info_ptr + dwo_unit->length
4743 || peek_abbrev_code (abfd, info_ptr) == 0)
4744 {
4745 do_cleanups (cleanups);
4746 return;
4747 }
4748 }
4749
dee91e82
DE
4750 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4751
4752 if (free_cu_cleanup != NULL)
348e048f 4753 {
dee91e82
DE
4754 if (keep)
4755 {
4756 /* We've successfully allocated this compilation unit. Let our
4757 caller clean it up when finished with it. */
4758 discard_cleanups (free_cu_cleanup);
4759
4760 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4761 So we have to manually free the abbrev table. */
4762 dwarf2_free_abbrev_table (cu);
4763
4764 /* Link this CU into read_in_chain. */
4765 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4766 dwarf2_per_objfile->read_in_chain = this_cu;
4767 }
4768 else
4769 do_cleanups (free_cu_cleanup);
348e048f 4770 }
dee91e82
DE
4771
4772 do_cleanups (cleanups);
4773}
4774
3019eac3
DE
4775/* Read CU/TU THIS_CU in section SECTION,
4776 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4777 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4778 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4779
4780 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4781 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4782
4783 We fill in THIS_CU->length.
4784
4785 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4786 linker) then DIE_READER_FUNC will not get called.
4787
4788 THIS_CU->cu is always freed when done.
3019eac3
DE
4789 This is done in order to not leave THIS_CU->cu in a state where we have
4790 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4791
4792static void
4793init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4794 struct dwarf2_section_info *abbrev_section,
3019eac3 4795 struct dwo_file *dwo_file,
dee91e82
DE
4796 die_reader_func_ftype *die_reader_func,
4797 void *data)
4798{
4799 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4800 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4801 bfd *abfd = section->asection->owner;
dee91e82
DE
4802 struct dwarf2_cu cu;
4803 gdb_byte *begin_info_ptr, *info_ptr;
4804 struct die_reader_specs reader;
4805 struct cleanup *cleanups;
4806 struct die_info *comp_unit_die;
4807 int has_children;
4808
09406207
DE
4809 if (dwarf2_die_debug)
4810 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4811 this_cu->is_debug_types ? "type" : "comp",
4812 this_cu->offset.sect_off);
4813
dee91e82
DE
4814 gdb_assert (this_cu->cu == NULL);
4815
dee91e82
DE
4816 /* This is cheap if the section is already read in. */
4817 dwarf2_read_section (objfile, section);
4818
4819 init_one_comp_unit (&cu, this_cu);
4820
4821 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4822
4823 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4824 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4825 abbrev_section, info_ptr,
3019eac3 4826 this_cu->is_debug_types);
dee91e82 4827
1ce1cefd 4828 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4829
4830 /* Skip dummy compilation units. */
4831 if (info_ptr >= begin_info_ptr + this_cu->length
4832 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4833 {
dee91e82 4834 do_cleanups (cleanups);
21b2bd31 4835 return;
93311388 4836 }
72bf9492 4837
dee91e82
DE
4838 dwarf2_read_abbrevs (&cu, abbrev_section);
4839 make_cleanup (dwarf2_free_abbrev_table, &cu);
4840
3019eac3 4841 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4842 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4843
4844 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4845
4846 do_cleanups (cleanups);
4847}
4848
3019eac3
DE
4849/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4850 does not lookup the specified DWO file.
4851 This cannot be used to read DWO files.
dee91e82
DE
4852
4853 THIS_CU->cu is always freed when done.
3019eac3
DE
4854 This is done in order to not leave THIS_CU->cu in a state where we have
4855 to care whether it refers to the "main" CU or the DWO CU.
4856 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4857
4858static void
4859init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4860 die_reader_func_ftype *die_reader_func,
4861 void *data)
4862{
4863 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4864 get_abbrev_section_for_cu (this_cu),
3019eac3 4865 NULL,
dee91e82
DE
4866 die_reader_func, data);
4867}
4868
f4dc4d17
DE
4869/* Create a psymtab named NAME and assign it to PER_CU.
4870
4871 The caller must fill in the following details:
4872 dirname, textlow, texthigh. */
4873
4874static struct partial_symtab *
4875create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4876{
4877 struct objfile *objfile = per_cu->objfile;
4878 struct partial_symtab *pst;
4879
4880 pst = start_psymtab_common (objfile, objfile->section_offsets,
4881 name, 0,
4882 objfile->global_psymbols.next,
4883 objfile->static_psymbols.next);
4884
4885 pst->psymtabs_addrmap_supported = 1;
4886
4887 /* This is the glue that links PST into GDB's symbol API. */
4888 pst->read_symtab_private = per_cu;
257e7a09 4889 pst->read_symtab = dwarf2_read_symtab;
f4dc4d17
DE
4890 per_cu->v.psymtab = pst;
4891
4892 return pst;
4893}
4894
dee91e82
DE
4895/* die_reader_func for process_psymtab_comp_unit. */
4896
4897static void
4898process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4899 gdb_byte *info_ptr,
4900 struct die_info *comp_unit_die,
4901 int has_children,
4902 void *data)
4903{
4904 struct dwarf2_cu *cu = reader->cu;
4905 struct objfile *objfile = cu->objfile;
4906 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4907 struct attribute *attr;
4908 CORE_ADDR baseaddr;
4909 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4910 struct partial_symtab *pst;
4911 int has_pc_info;
4912 const char *filename;
95554aad 4913 int *want_partial_unit_ptr = data;
dee91e82 4914
95554aad
TT
4915 if (comp_unit_die->tag == DW_TAG_partial_unit
4916 && (want_partial_unit_ptr == NULL
4917 || !*want_partial_unit_ptr))
dee91e82
DE
4918 return;
4919
f4dc4d17
DE
4920 gdb_assert (! per_cu->is_debug_types);
4921
95554aad 4922 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4923
4924 cu->list_in_scope = &file_symbols;
c906108c 4925
93311388 4926 /* Allocate a new partial symbol table structure. */
dee91e82 4927 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4928 if (attr == NULL || !DW_STRING (attr))
4929 filename = "";
4930 else
4931 filename = DW_STRING (attr);
72bf9492 4932
f4dc4d17
DE
4933 pst = create_partial_symtab (per_cu, filename);
4934
4935 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4936 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4937 if (attr != NULL)
4938 pst->dirname = DW_STRING (attr);
72bf9492 4939
93311388 4940 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4941
dee91e82 4942 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4943
93311388
DE
4944 /* Possibly set the default values of LOWPC and HIGHPC from
4945 `DW_AT_ranges'. */
d85a05f0 4946 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4947 &best_highpc, cu, pst);
d85a05f0 4948 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4949 /* Store the contiguous range if it is not empty; it can be empty for
4950 CUs with no code. */
4951 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4952 best_lowpc + baseaddr,
4953 best_highpc + baseaddr - 1, pst);
93311388
DE
4954
4955 /* Check if comp unit has_children.
4956 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4957 If not, there's no more debug_info for this comp unit. */
d85a05f0 4958 if (has_children)
93311388
DE
4959 {
4960 struct partial_die_info *first_die;
4961 CORE_ADDR lowpc, highpc;
31ffec48 4962
93311388
DE
4963 lowpc = ((CORE_ADDR) -1);
4964 highpc = ((CORE_ADDR) 0);
c906108c 4965
dee91e82 4966 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4967
93311388 4968 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4969 ! has_pc_info, cu);
57c22c6c 4970
93311388
DE
4971 /* If we didn't find a lowpc, set it to highpc to avoid
4972 complaints from `maint check'. */
4973 if (lowpc == ((CORE_ADDR) -1))
4974 lowpc = highpc;
10b3939b 4975
93311388
DE
4976 /* If the compilation unit didn't have an explicit address range,
4977 then use the information extracted from its child dies. */
d85a05f0 4978 if (! has_pc_info)
93311388 4979 {
d85a05f0
DJ
4980 best_lowpc = lowpc;
4981 best_highpc = highpc;
93311388
DE
4982 }
4983 }
d85a05f0
DJ
4984 pst->textlow = best_lowpc + baseaddr;
4985 pst->texthigh = best_highpc + baseaddr;
c906108c 4986
93311388
DE
4987 pst->n_global_syms = objfile->global_psymbols.next -
4988 (objfile->global_psymbols.list + pst->globals_offset);
4989 pst->n_static_syms = objfile->static_psymbols.next -
4990 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 4991 sort_pst_symbols (objfile, pst);
c906108c 4992
796a7ff8 4993 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
95554aad
TT
4994 {
4995 int i;
796a7ff8 4996 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
4997 struct dwarf2_per_cu_data *iter;
4998
4999 /* Fill in 'dependencies' here; we fill in 'users' in a
5000 post-pass. */
5001 pst->number_of_dependencies = len;
5002 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5003 len * sizeof (struct symtab *));
5004 for (i = 0;
796a7ff8 5005 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
5006 i, iter);
5007 ++i)
5008 pst->dependencies[i] = iter->v.psymtab;
5009
796a7ff8 5010 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
5011 }
5012
f4dc4d17
DE
5013 /* Get the list of files included in the current compilation unit,
5014 and build a psymtab for each of them. */
5015 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
5016
5017 if (dwarf2_read_debug)
5018 {
5019 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5020
5021 fprintf_unfiltered (gdb_stdlog,
844226d6 5022 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
5023 ", %d global, %d static syms\n",
5024 per_cu->is_debug_types ? "type" : "comp",
5025 per_cu->offset.sect_off,
5026 paddress (gdbarch, pst->textlow),
5027 paddress (gdbarch, pst->texthigh),
5028 pst->n_global_syms, pst->n_static_syms);
5029 }
dee91e82 5030}
ae038cb0 5031
dee91e82
DE
5032/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5033 Process compilation unit THIS_CU for a psymtab. */
5034
5035static void
95554aad
TT
5036process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5037 int want_partial_unit)
dee91e82
DE
5038{
5039 /* If this compilation unit was already read in, free the
5040 cached copy in order to read it in again. This is
5041 necessary because we skipped some symbols when we first
5042 read in the compilation unit (see load_partial_dies).
5043 This problem could be avoided, but the benefit is unclear. */
5044 if (this_cu->cu != NULL)
5045 free_one_cached_comp_unit (this_cu);
5046
3019eac3 5047 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
5048 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5049 process_psymtab_comp_unit_reader,
95554aad 5050 &want_partial_unit);
dee91e82
DE
5051
5052 /* Age out any secondary CUs. */
5053 age_cached_comp_units ();
93311388 5054}
ff013f42 5055
f4dc4d17
DE
5056static hashval_t
5057hash_type_unit_group (const void *item)
5058{
094b34ac 5059 const struct type_unit_group *tu_group = item;
f4dc4d17 5060
094b34ac 5061 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5062}
348e048f
DE
5063
5064static int
f4dc4d17 5065eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5066{
f4dc4d17
DE
5067 const struct type_unit_group *lhs = item_lhs;
5068 const struct type_unit_group *rhs = item_rhs;
348e048f 5069
094b34ac 5070 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5071}
348e048f 5072
f4dc4d17
DE
5073/* Allocate a hash table for type unit groups. */
5074
5075static htab_t
5076allocate_type_unit_groups_table (void)
5077{
5078 return htab_create_alloc_ex (3,
5079 hash_type_unit_group,
5080 eq_type_unit_group,
5081 NULL,
5082 &dwarf2_per_objfile->objfile->objfile_obstack,
5083 hashtab_obstack_allocate,
5084 dummy_obstack_deallocate);
5085}
dee91e82 5086
f4dc4d17
DE
5087/* Type units that don't have DW_AT_stmt_list are grouped into their own
5088 partial symtabs. We combine several TUs per psymtab to not let the size
5089 of any one psymtab grow too big. */
5090#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5091#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5092
094b34ac 5093/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5094 Create the type_unit_group object used to hold one or more TUs. */
5095
5096static struct type_unit_group *
094b34ac 5097create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5098{
5099 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5100 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5101 struct type_unit_group *tu_group;
f4dc4d17
DE
5102
5103 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5104 struct type_unit_group);
094b34ac 5105 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5106 per_cu->objfile = objfile;
5107 per_cu->is_debug_types = 1;
796a7ff8 5108 per_cu->type_unit_group = tu_group;
f4dc4d17 5109
094b34ac
DE
5110 if (dwarf2_per_objfile->using_index)
5111 {
5112 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5113 struct dwarf2_per_cu_quick_data);
5114 tu_group->t.first_tu = cu->per_cu;
5115 }
5116 else
5117 {
5118 unsigned int line_offset = line_offset_struct.sect_off;
5119 struct partial_symtab *pst;
5120 char *name;
5121
5122 /* Give the symtab a useful name for debug purposes. */
5123 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5124 name = xstrprintf ("<type_units_%d>",
5125 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5126 else
5127 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5128
5129 pst = create_partial_symtab (per_cu, name);
5130 pst->anonymous = 1;
f4dc4d17 5131
094b34ac
DE
5132 xfree (name);
5133 }
f4dc4d17 5134
094b34ac
DE
5135 tu_group->hash.dwo_unit = cu->dwo_unit;
5136 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5137
5138 return tu_group;
5139}
5140
094b34ac
DE
5141/* Look up the type_unit_group for type unit CU, and create it if necessary.
5142 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5143
5144static struct type_unit_group *
094b34ac 5145get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5146{
5147 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5148 struct type_unit_group *tu_group;
5149 void **slot;
5150 unsigned int line_offset;
5151 struct type_unit_group type_unit_group_for_lookup;
5152
5153 if (dwarf2_per_objfile->type_unit_groups == NULL)
5154 {
5155 dwarf2_per_objfile->type_unit_groups =
5156 allocate_type_unit_groups_table ();
5157 }
5158
5159 /* Do we need to create a new group, or can we use an existing one? */
5160
5161 if (stmt_list)
5162 {
5163 line_offset = DW_UNSND (stmt_list);
5164 ++tu_stats->nr_symtab_sharers;
5165 }
5166 else
5167 {
5168 /* Ugh, no stmt_list. Rare, but we have to handle it.
5169 We can do various things here like create one group per TU or
5170 spread them over multiple groups to split up the expansion work.
5171 To avoid worst case scenarios (too many groups or too large groups)
5172 we, umm, group them in bunches. */
5173 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5174 | (tu_stats->nr_stmt_less_type_units
5175 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5176 ++tu_stats->nr_stmt_less_type_units;
5177 }
5178
094b34ac
DE
5179 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5180 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5181 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5182 &type_unit_group_for_lookup, INSERT);
5183 if (*slot != NULL)
5184 {
5185 tu_group = *slot;
5186 gdb_assert (tu_group != NULL);
5187 }
5188 else
5189 {
5190 sect_offset line_offset_struct;
5191
5192 line_offset_struct.sect_off = line_offset;
094b34ac 5193 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5194 *slot = tu_group;
5195 ++tu_stats->nr_symtabs;
5196 }
5197
5198 return tu_group;
5199}
5200
5201/* Struct used to sort TUs by their abbreviation table offset. */
5202
5203struct tu_abbrev_offset
5204{
5205 struct signatured_type *sig_type;
5206 sect_offset abbrev_offset;
5207};
5208
5209/* Helper routine for build_type_unit_groups, passed to qsort. */
5210
5211static int
5212sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5213{
5214 const struct tu_abbrev_offset * const *a = ap;
5215 const struct tu_abbrev_offset * const *b = bp;
5216 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5217 unsigned int boff = (*b)->abbrev_offset.sect_off;
5218
5219 return (aoff > boff) - (aoff < boff);
5220}
5221
5222/* A helper function to add a type_unit_group to a table. */
5223
5224static int
5225add_type_unit_group_to_table (void **slot, void *datum)
5226{
5227 struct type_unit_group *tu_group = *slot;
5228 struct type_unit_group ***datap = datum;
5229
5230 **datap = tu_group;
5231 ++*datap;
5232
5233 return 1;
5234}
5235
5236/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5237 each one passing FUNC,DATA.
5238
5239 The efficiency is because we sort TUs by the abbrev table they use and
5240 only read each abbrev table once. In one program there are 200K TUs
5241 sharing 8K abbrev tables.
5242
5243 The main purpose of this function is to support building the
5244 dwarf2_per_objfile->type_unit_groups table.
5245 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5246 can collapse the search space by grouping them by stmt_list.
5247 The savings can be significant, in the same program from above the 200K TUs
5248 share 8K stmt_list tables.
5249
5250 FUNC is expected to call get_type_unit_group, which will create the
5251 struct type_unit_group if necessary and add it to
5252 dwarf2_per_objfile->type_unit_groups. */
5253
5254static void
5255build_type_unit_groups (die_reader_func_ftype *func, void *data)
5256{
5257 struct objfile *objfile = dwarf2_per_objfile->objfile;
5258 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5259 struct cleanup *cleanups;
5260 struct abbrev_table *abbrev_table;
5261 sect_offset abbrev_offset;
5262 struct tu_abbrev_offset *sorted_by_abbrev;
5263 struct type_unit_group **iter;
5264 int i;
5265
5266 /* It's up to the caller to not call us multiple times. */
5267 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5268
5269 if (dwarf2_per_objfile->n_type_units == 0)
5270 return;
5271
5272 /* TUs typically share abbrev tables, and there can be way more TUs than
5273 abbrev tables. Sort by abbrev table to reduce the number of times we
5274 read each abbrev table in.
5275 Alternatives are to punt or to maintain a cache of abbrev tables.
5276 This is simpler and efficient enough for now.
5277
5278 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5279 symtab to use). Typically TUs with the same abbrev offset have the same
5280 stmt_list value too so in practice this should work well.
5281
5282 The basic algorithm here is:
5283
5284 sort TUs by abbrev table
5285 for each TU with same abbrev table:
5286 read abbrev table if first user
5287 read TU top level DIE
5288 [IWBN if DWO skeletons had DW_AT_stmt_list]
5289 call FUNC */
5290
5291 if (dwarf2_read_debug)
5292 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5293
5294 /* Sort in a separate table to maintain the order of all_type_units
5295 for .gdb_index: TU indices directly index all_type_units. */
5296 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5297 dwarf2_per_objfile->n_type_units);
5298 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5299 {
5300 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5301
5302 sorted_by_abbrev[i].sig_type = sig_type;
5303 sorted_by_abbrev[i].abbrev_offset =
5304 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5305 sig_type->per_cu.offset);
5306 }
5307 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5308 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5309 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5310
094b34ac
DE
5311 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5312 called any number of times, so we don't reset tu_stats here. */
5313
f4dc4d17
DE
5314 abbrev_offset.sect_off = ~(unsigned) 0;
5315 abbrev_table = NULL;
5316 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5317
5318 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5319 {
5320 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5321
5322 /* Switch to the next abbrev table if necessary. */
5323 if (abbrev_table == NULL
5324 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5325 {
5326 if (abbrev_table != NULL)
5327 {
5328 abbrev_table_free (abbrev_table);
5329 /* Reset to NULL in case abbrev_table_read_table throws
5330 an error: abbrev_table_free_cleanup will get called. */
5331 abbrev_table = NULL;
5332 }
5333 abbrev_offset = tu->abbrev_offset;
5334 abbrev_table =
5335 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5336 abbrev_offset);
5337 ++tu_stats->nr_uniq_abbrev_tables;
5338 }
5339
5340 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5341 func, data);
5342 }
5343
5344 /* Create a vector of pointers to primary type units to make it easy to
5345 iterate over them and CUs. See dw2_get_primary_cu. */
5346 dwarf2_per_objfile->n_type_unit_groups =
5347 htab_elements (dwarf2_per_objfile->type_unit_groups);
5348 dwarf2_per_objfile->all_type_unit_groups =
5349 obstack_alloc (&objfile->objfile_obstack,
5350 dwarf2_per_objfile->n_type_unit_groups
5351 * sizeof (struct type_unit_group *));
5352 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5353 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5354 add_type_unit_group_to_table, &iter);
5355 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5356 == dwarf2_per_objfile->n_type_unit_groups);
5357
5358 do_cleanups (cleanups);
5359
5360 if (dwarf2_read_debug)
5361 {
5362 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5363 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5364 dwarf2_per_objfile->n_type_units);
5365 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5366 tu_stats->nr_uniq_abbrev_tables);
5367 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5368 tu_stats->nr_symtabs);
5369 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5370 tu_stats->nr_symtab_sharers);
5371 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5372 tu_stats->nr_stmt_less_type_units);
5373 }
5374}
5375
5376/* Reader function for build_type_psymtabs. */
5377
5378static void
5379build_type_psymtabs_reader (const struct die_reader_specs *reader,
5380 gdb_byte *info_ptr,
5381 struct die_info *type_unit_die,
5382 int has_children,
5383 void *data)
5384{
5385 struct objfile *objfile = dwarf2_per_objfile->objfile;
5386 struct dwarf2_cu *cu = reader->cu;
5387 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5388 struct type_unit_group *tu_group;
5389 struct attribute *attr;
5390 struct partial_die_info *first_die;
5391 CORE_ADDR lowpc, highpc;
5392 struct partial_symtab *pst;
5393
5394 gdb_assert (data == NULL);
5395
5396 if (! has_children)
5397 return;
5398
5399 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5400 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5401
094b34ac 5402 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5403
5404 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5405 cu->list_in_scope = &file_symbols;
5406 pst = create_partial_symtab (per_cu, "");
5407 pst->anonymous = 1;
5408
5409 first_die = load_partial_dies (reader, info_ptr, 1);
5410
5411 lowpc = (CORE_ADDR) -1;
5412 highpc = (CORE_ADDR) 0;
5413 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5414
5415 pst->n_global_syms = objfile->global_psymbols.next -
5416 (objfile->global_psymbols.list + pst->globals_offset);
5417 pst->n_static_syms = objfile->static_psymbols.next -
5418 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5419 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5420}
5421
5422/* Traversal function for build_type_psymtabs. */
5423
5424static int
5425build_type_psymtab_dependencies (void **slot, void *info)
5426{
5427 struct objfile *objfile = dwarf2_per_objfile->objfile;
5428 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5429 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5430 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5431 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5432 struct dwarf2_per_cu_data *iter;
5433 int i;
5434
5435 gdb_assert (len > 0);
5436
5437 pst->number_of_dependencies = len;
5438 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5439 len * sizeof (struct psymtab *));
5440 for (i = 0;
094b34ac 5441 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5442 ++i)
5443 {
5444 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 5445 iter->type_unit_group = tu_group;
f4dc4d17
DE
5446 }
5447
094b34ac 5448 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5449
5450 return 1;
5451}
5452
5453/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5454 Build partial symbol tables for the .debug_types comp-units. */
5455
5456static void
5457build_type_psymtabs (struct objfile *objfile)
5458{
0e50663e 5459 if (! create_all_type_units (objfile))
348e048f
DE
5460 return;
5461
f4dc4d17
DE
5462 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5463
5464 /* Now that all TUs have been processed we can fill in the dependencies. */
5465 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5466 build_type_psymtab_dependencies, NULL);
348e048f
DE
5467}
5468
60606b2c
TT
5469/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5470
5471static void
5472psymtabs_addrmap_cleanup (void *o)
5473{
5474 struct objfile *objfile = o;
ec61707d 5475
60606b2c
TT
5476 objfile->psymtabs_addrmap = NULL;
5477}
5478
95554aad
TT
5479/* Compute the 'user' field for each psymtab in OBJFILE. */
5480
5481static void
5482set_partial_user (struct objfile *objfile)
5483{
5484 int i;
5485
5486 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5487 {
5488 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5489 struct partial_symtab *pst = per_cu->v.psymtab;
5490 int j;
5491
36586728
TT
5492 if (pst == NULL)
5493 continue;
5494
95554aad
TT
5495 for (j = 0; j < pst->number_of_dependencies; ++j)
5496 {
5497 /* Set the 'user' field only if it is not already set. */
5498 if (pst->dependencies[j]->user == NULL)
5499 pst->dependencies[j]->user = pst;
5500 }
5501 }
5502}
5503
93311388
DE
5504/* Build the partial symbol table by doing a quick pass through the
5505 .debug_info and .debug_abbrev sections. */
72bf9492 5506
93311388 5507static void
c67a9c90 5508dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5509{
60606b2c
TT
5510 struct cleanup *back_to, *addrmap_cleanup;
5511 struct obstack temp_obstack;
21b2bd31 5512 int i;
93311388 5513
45cfd468
DE
5514 if (dwarf2_read_debug)
5515 {
5516 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5517 objfile->name);
5518 }
5519
98bfdba5
PA
5520 dwarf2_per_objfile->reading_partial_symbols = 1;
5521
be391dca 5522 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5523
93311388
DE
5524 /* Any cached compilation units will be linked by the per-objfile
5525 read_in_chain. Make sure to free them when we're done. */
5526 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5527
348e048f
DE
5528 build_type_psymtabs (objfile);
5529
93311388 5530 create_all_comp_units (objfile);
c906108c 5531
60606b2c
TT
5532 /* Create a temporary address map on a temporary obstack. We later
5533 copy this to the final obstack. */
5534 obstack_init (&temp_obstack);
5535 make_cleanup_obstack_free (&temp_obstack);
5536 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5537 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5538
21b2bd31 5539 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5540 {
21b2bd31 5541 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5542
95554aad 5543 process_psymtab_comp_unit (per_cu, 0);
c906108c 5544 }
ff013f42 5545
95554aad
TT
5546 set_partial_user (objfile);
5547
ff013f42
JK
5548 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5549 &objfile->objfile_obstack);
60606b2c 5550 discard_cleanups (addrmap_cleanup);
ff013f42 5551
ae038cb0 5552 do_cleanups (back_to);
45cfd468
DE
5553
5554 if (dwarf2_read_debug)
5555 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5556 objfile->name);
ae038cb0
DJ
5557}
5558
3019eac3 5559/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5560
5561static void
dee91e82
DE
5562load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5563 gdb_byte *info_ptr,
5564 struct die_info *comp_unit_die,
5565 int has_children,
5566 void *data)
ae038cb0 5567{
dee91e82 5568 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5569
95554aad 5570 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5571
ae038cb0
DJ
5572 /* Check if comp unit has_children.
5573 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5574 If not, there's no more debug_info for this comp unit. */
d85a05f0 5575 if (has_children)
dee91e82
DE
5576 load_partial_dies (reader, info_ptr, 0);
5577}
98bfdba5 5578
dee91e82
DE
5579/* Load the partial DIEs for a secondary CU into memory.
5580 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5581
dee91e82
DE
5582static void
5583load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5584{
f4dc4d17
DE
5585 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5586 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5587}
5588
ae038cb0 5589static void
36586728
TT
5590read_comp_units_from_section (struct objfile *objfile,
5591 struct dwarf2_section_info *section,
5592 unsigned int is_dwz,
5593 int *n_allocated,
5594 int *n_comp_units,
5595 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5596{
be391dca 5597 gdb_byte *info_ptr;
36586728 5598 bfd *abfd = section->asection->owner;
be391dca 5599
36586728 5600 dwarf2_read_section (objfile, section);
ae038cb0 5601
36586728 5602 info_ptr = section->buffer;
6e70227d 5603
36586728 5604 while (info_ptr < section->buffer + section->size)
ae038cb0 5605 {
c764a876 5606 unsigned int length, initial_length_size;
ae038cb0 5607 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5608 sect_offset offset;
ae038cb0 5609
36586728 5610 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5611
5612 /* Read just enough information to find out where the next
5613 compilation unit is. */
36586728 5614 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5615
5616 /* Save the compilation unit for later lookup. */
5617 this_cu = obstack_alloc (&objfile->objfile_obstack,
5618 sizeof (struct dwarf2_per_cu_data));
5619 memset (this_cu, 0, sizeof (*this_cu));
5620 this_cu->offset = offset;
c764a876 5621 this_cu->length = length + initial_length_size;
36586728 5622 this_cu->is_dwz = is_dwz;
9291a0cd 5623 this_cu->objfile = objfile;
36586728 5624 this_cu->info_or_types_section = section;
ae038cb0 5625
36586728 5626 if (*n_comp_units == *n_allocated)
ae038cb0 5627 {
36586728
TT
5628 *n_allocated *= 2;
5629 *all_comp_units = xrealloc (*all_comp_units,
5630 *n_allocated
5631 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5632 }
36586728
TT
5633 (*all_comp_units)[*n_comp_units] = this_cu;
5634 ++*n_comp_units;
ae038cb0
DJ
5635
5636 info_ptr = info_ptr + this_cu->length;
5637 }
36586728
TT
5638}
5639
5640/* Create a list of all compilation units in OBJFILE.
5641 This is only done for -readnow and building partial symtabs. */
5642
5643static void
5644create_all_comp_units (struct objfile *objfile)
5645{
5646 int n_allocated;
5647 int n_comp_units;
5648 struct dwarf2_per_cu_data **all_comp_units;
5649
5650 n_comp_units = 0;
5651 n_allocated = 10;
5652 all_comp_units = xmalloc (n_allocated
5653 * sizeof (struct dwarf2_per_cu_data *));
5654
5655 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5656 &n_allocated, &n_comp_units, &all_comp_units);
5657
5658 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5659 {
5660 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5661
5662 read_comp_units_from_section (objfile, &dwz->info, 1,
5663 &n_allocated, &n_comp_units,
5664 &all_comp_units);
5665 }
ae038cb0
DJ
5666
5667 dwarf2_per_objfile->all_comp_units
5668 = obstack_alloc (&objfile->objfile_obstack,
5669 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5670 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5671 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5672 xfree (all_comp_units);
5673 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5674}
5675
5734ee8b
DJ
5676/* Process all loaded DIEs for compilation unit CU, starting at
5677 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5678 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5679 DW_AT_ranges). If NEED_PC is set, then this function will set
5680 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5681 and record the covered ranges in the addrmap. */
c906108c 5682
72bf9492
DJ
5683static void
5684scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5685 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5686{
72bf9492 5687 struct partial_die_info *pdi;
c906108c 5688
91c24f0a
DC
5689 /* Now, march along the PDI's, descending into ones which have
5690 interesting children but skipping the children of the other ones,
5691 until we reach the end of the compilation unit. */
c906108c 5692
72bf9492 5693 pdi = first_die;
91c24f0a 5694
72bf9492
DJ
5695 while (pdi != NULL)
5696 {
5697 fixup_partial_die (pdi, cu);
c906108c 5698
f55ee35c 5699 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5700 children, so we need to look at them. Ditto for anonymous
5701 enums. */
933c6fe4 5702
72bf9492 5703 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5704 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5705 || pdi->tag == DW_TAG_imported_unit)
c906108c 5706 {
72bf9492 5707 switch (pdi->tag)
c906108c
SS
5708 {
5709 case DW_TAG_subprogram:
5734ee8b 5710 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5711 break;
72929c62 5712 case DW_TAG_constant:
c906108c
SS
5713 case DW_TAG_variable:
5714 case DW_TAG_typedef:
91c24f0a 5715 case DW_TAG_union_type:
72bf9492 5716 if (!pdi->is_declaration)
63d06c5c 5717 {
72bf9492 5718 add_partial_symbol (pdi, cu);
63d06c5c
DC
5719 }
5720 break;
c906108c 5721 case DW_TAG_class_type:
680b30c7 5722 case DW_TAG_interface_type:
c906108c 5723 case DW_TAG_structure_type:
72bf9492 5724 if (!pdi->is_declaration)
c906108c 5725 {
72bf9492 5726 add_partial_symbol (pdi, cu);
c906108c
SS
5727 }
5728 break;
91c24f0a 5729 case DW_TAG_enumeration_type:
72bf9492
DJ
5730 if (!pdi->is_declaration)
5731 add_partial_enumeration (pdi, cu);
c906108c
SS
5732 break;
5733 case DW_TAG_base_type:
a02abb62 5734 case DW_TAG_subrange_type:
c906108c 5735 /* File scope base type definitions are added to the partial
c5aa993b 5736 symbol table. */
72bf9492 5737 add_partial_symbol (pdi, cu);
c906108c 5738 break;
d9fa45fe 5739 case DW_TAG_namespace:
5734ee8b 5740 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5741 break;
5d7cb8df
JK
5742 case DW_TAG_module:
5743 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5744 break;
95554aad
TT
5745 case DW_TAG_imported_unit:
5746 {
5747 struct dwarf2_per_cu_data *per_cu;
5748
f4dc4d17
DE
5749 /* For now we don't handle imported units in type units. */
5750 if (cu->per_cu->is_debug_types)
5751 {
5752 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5753 " supported in type units [in module %s]"),
5754 cu->objfile->name);
5755 }
5756
95554aad 5757 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5758 pdi->is_dwz,
95554aad
TT
5759 cu->objfile);
5760
5761 /* Go read the partial unit, if needed. */
5762 if (per_cu->v.psymtab == NULL)
5763 process_psymtab_comp_unit (per_cu, 1);
5764
f4dc4d17 5765 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5766 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5767 }
5768 break;
c906108c
SS
5769 default:
5770 break;
5771 }
5772 }
5773
72bf9492
DJ
5774 /* If the die has a sibling, skip to the sibling. */
5775
5776 pdi = pdi->die_sibling;
5777 }
5778}
5779
5780/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5781
72bf9492 5782 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5783 name is concatenated with "::" and the partial DIE's name. For
5784 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5785 Enumerators are an exception; they use the scope of their parent
5786 enumeration type, i.e. the name of the enumeration type is not
5787 prepended to the enumerator.
91c24f0a 5788
72bf9492
DJ
5789 There are two complexities. One is DW_AT_specification; in this
5790 case "parent" means the parent of the target of the specification,
5791 instead of the direct parent of the DIE. The other is compilers
5792 which do not emit DW_TAG_namespace; in this case we try to guess
5793 the fully qualified name of structure types from their members'
5794 linkage names. This must be done using the DIE's children rather
5795 than the children of any DW_AT_specification target. We only need
5796 to do this for structures at the top level, i.e. if the target of
5797 any DW_AT_specification (if any; otherwise the DIE itself) does not
5798 have a parent. */
5799
5800/* Compute the scope prefix associated with PDI's parent, in
5801 compilation unit CU. The result will be allocated on CU's
5802 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5803 field. NULL is returned if no prefix is necessary. */
15d034d0 5804static const char *
72bf9492
DJ
5805partial_die_parent_scope (struct partial_die_info *pdi,
5806 struct dwarf2_cu *cu)
5807{
15d034d0 5808 const char *grandparent_scope;
72bf9492 5809 struct partial_die_info *parent, *real_pdi;
91c24f0a 5810
72bf9492
DJ
5811 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5812 then this means the parent of the specification DIE. */
5813
5814 real_pdi = pdi;
72bf9492 5815 while (real_pdi->has_specification)
36586728
TT
5816 real_pdi = find_partial_die (real_pdi->spec_offset,
5817 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5818
5819 parent = real_pdi->die_parent;
5820 if (parent == NULL)
5821 return NULL;
5822
5823 if (parent->scope_set)
5824 return parent->scope;
5825
5826 fixup_partial_die (parent, cu);
5827
10b3939b 5828 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5829
acebe513
UW
5830 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5831 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5832 Work around this problem here. */
5833 if (cu->language == language_cplus
6e70227d 5834 && parent->tag == DW_TAG_namespace
acebe513
UW
5835 && strcmp (parent->name, "::") == 0
5836 && grandparent_scope == NULL)
5837 {
5838 parent->scope = NULL;
5839 parent->scope_set = 1;
5840 return NULL;
5841 }
5842
9c6c53f7
SA
5843 if (pdi->tag == DW_TAG_enumerator)
5844 /* Enumerators should not get the name of the enumeration as a prefix. */
5845 parent->scope = grandparent_scope;
5846 else if (parent->tag == DW_TAG_namespace
f55ee35c 5847 || parent->tag == DW_TAG_module
72bf9492
DJ
5848 || parent->tag == DW_TAG_structure_type
5849 || parent->tag == DW_TAG_class_type
680b30c7 5850 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5851 || parent->tag == DW_TAG_union_type
5852 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5853 {
5854 if (grandparent_scope == NULL)
5855 parent->scope = parent->name;
5856 else
3e43a32a
MS
5857 parent->scope = typename_concat (&cu->comp_unit_obstack,
5858 grandparent_scope,
f55ee35c 5859 parent->name, 0, cu);
72bf9492 5860 }
72bf9492
DJ
5861 else
5862 {
5863 /* FIXME drow/2004-04-01: What should we be doing with
5864 function-local names? For partial symbols, we should probably be
5865 ignoring them. */
5866 complaint (&symfile_complaints,
e2e0b3e5 5867 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5868 parent->tag, pdi->offset.sect_off);
72bf9492 5869 parent->scope = grandparent_scope;
c906108c
SS
5870 }
5871
72bf9492
DJ
5872 parent->scope_set = 1;
5873 return parent->scope;
5874}
5875
5876/* Return the fully scoped name associated with PDI, from compilation unit
5877 CU. The result will be allocated with malloc. */
4568ecf9 5878
72bf9492
DJ
5879static char *
5880partial_die_full_name (struct partial_die_info *pdi,
5881 struct dwarf2_cu *cu)
5882{
15d034d0 5883 const char *parent_scope;
72bf9492 5884
98bfdba5
PA
5885 /* If this is a template instantiation, we can not work out the
5886 template arguments from partial DIEs. So, unfortunately, we have
5887 to go through the full DIEs. At least any work we do building
5888 types here will be reused if full symbols are loaded later. */
5889 if (pdi->has_template_arguments)
5890 {
5891 fixup_partial_die (pdi, cu);
5892
5893 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5894 {
5895 struct die_info *die;
5896 struct attribute attr;
5897 struct dwarf2_cu *ref_cu = cu;
5898
b64f50a1 5899 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5900 attr.name = 0;
5901 attr.form = DW_FORM_ref_addr;
4568ecf9 5902 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5903 die = follow_die_ref (NULL, &attr, &ref_cu);
5904
5905 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5906 }
5907 }
5908
72bf9492
DJ
5909 parent_scope = partial_die_parent_scope (pdi, cu);
5910 if (parent_scope == NULL)
5911 return NULL;
5912 else
f55ee35c 5913 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5914}
5915
5916static void
72bf9492 5917add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5918{
e7c27a73 5919 struct objfile *objfile = cu->objfile;
c906108c 5920 CORE_ADDR addr = 0;
15d034d0 5921 const char *actual_name = NULL;
e142c38c 5922 CORE_ADDR baseaddr;
15d034d0 5923 char *built_actual_name;
e142c38c
DJ
5924
5925 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5926
15d034d0
TT
5927 built_actual_name = partial_die_full_name (pdi, cu);
5928 if (built_actual_name != NULL)
5929 actual_name = built_actual_name;
63d06c5c 5930
72bf9492
DJ
5931 if (actual_name == NULL)
5932 actual_name = pdi->name;
5933
c906108c
SS
5934 switch (pdi->tag)
5935 {
5936 case DW_TAG_subprogram:
2cfa0c8d 5937 if (pdi->is_external || cu->language == language_ada)
c906108c 5938 {
2cfa0c8d
JB
5939 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5940 of the global scope. But in Ada, we want to be able to access
5941 nested procedures globally. So all Ada subprograms are stored
5942 in the global scope. */
f47fb265 5943 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5944 mst_text, objfile); */
f47fb265 5945 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5946 built_actual_name != NULL,
f47fb265
MS
5947 VAR_DOMAIN, LOC_BLOCK,
5948 &objfile->global_psymbols,
5949 0, pdi->lowpc + baseaddr,
5950 cu->language, objfile);
c906108c
SS
5951 }
5952 else
5953 {
f47fb265 5954 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5955 mst_file_text, objfile); */
f47fb265 5956 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5957 built_actual_name != NULL,
f47fb265
MS
5958 VAR_DOMAIN, LOC_BLOCK,
5959 &objfile->static_psymbols,
5960 0, pdi->lowpc + baseaddr,
5961 cu->language, objfile);
c906108c
SS
5962 }
5963 break;
72929c62
JB
5964 case DW_TAG_constant:
5965 {
5966 struct psymbol_allocation_list *list;
5967
5968 if (pdi->is_external)
5969 list = &objfile->global_psymbols;
5970 else
5971 list = &objfile->static_psymbols;
f47fb265 5972 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5973 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 5974 list, 0, 0, cu->language, objfile);
72929c62
JB
5975 }
5976 break;
c906108c 5977 case DW_TAG_variable:
95554aad
TT
5978 if (pdi->d.locdesc)
5979 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5980
95554aad 5981 if (pdi->d.locdesc
caac4577
JG
5982 && addr == 0
5983 && !dwarf2_per_objfile->has_section_at_zero)
5984 {
5985 /* A global or static variable may also have been stripped
5986 out by the linker if unused, in which case its address
5987 will be nullified; do not add such variables into partial
5988 symbol table then. */
5989 }
5990 else if (pdi->is_external)
c906108c
SS
5991 {
5992 /* Global Variable.
5993 Don't enter into the minimal symbol tables as there is
5994 a minimal symbol table entry from the ELF symbols already.
5995 Enter into partial symbol table if it has a location
5996 descriptor or a type.
5997 If the location descriptor is missing, new_symbol will create
5998 a LOC_UNRESOLVED symbol, the address of the variable will then
5999 be determined from the minimal symbol table whenever the variable
6000 is referenced.
6001 The address for the partial symbol table entry is not
6002 used by GDB, but it comes in handy for debugging partial symbol
6003 table building. */
6004
95554aad 6005 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6006 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6007 built_actual_name != NULL,
f47fb265
MS
6008 VAR_DOMAIN, LOC_STATIC,
6009 &objfile->global_psymbols,
6010 0, addr + baseaddr,
6011 cu->language, objfile);
c906108c
SS
6012 }
6013 else
6014 {
0963b4bd 6015 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6016 if (pdi->d.locdesc == NULL)
decbce07 6017 {
15d034d0 6018 xfree (built_actual_name);
decbce07
MS
6019 return;
6020 }
f47fb265 6021 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6022 mst_file_data, objfile); */
f47fb265 6023 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6024 built_actual_name != NULL,
f47fb265
MS
6025 VAR_DOMAIN, LOC_STATIC,
6026 &objfile->static_psymbols,
6027 0, addr + baseaddr,
6028 cu->language, objfile);
c906108c
SS
6029 }
6030 break;
6031 case DW_TAG_typedef:
6032 case DW_TAG_base_type:
a02abb62 6033 case DW_TAG_subrange_type:
38d518c9 6034 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6035 built_actual_name != NULL,
176620f1 6036 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6037 &objfile->static_psymbols,
e142c38c 6038 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6039 break;
72bf9492
DJ
6040 case DW_TAG_namespace:
6041 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6042 built_actual_name != NULL,
72bf9492
DJ
6043 VAR_DOMAIN, LOC_TYPEDEF,
6044 &objfile->global_psymbols,
6045 0, (CORE_ADDR) 0, cu->language, objfile);
6046 break;
c906108c 6047 case DW_TAG_class_type:
680b30c7 6048 case DW_TAG_interface_type:
c906108c
SS
6049 case DW_TAG_structure_type:
6050 case DW_TAG_union_type:
6051 case DW_TAG_enumeration_type:
fa4028e9
JB
6052 /* Skip external references. The DWARF standard says in the section
6053 about "Structure, Union, and Class Type Entries": "An incomplete
6054 structure, union or class type is represented by a structure,
6055 union or class entry that does not have a byte size attribute
6056 and that has a DW_AT_declaration attribute." */
6057 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6058 {
15d034d0 6059 xfree (built_actual_name);
decbce07
MS
6060 return;
6061 }
fa4028e9 6062
63d06c5c
DC
6063 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6064 static vs. global. */
38d518c9 6065 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6066 built_actual_name != NULL,
176620f1 6067 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6068 (cu->language == language_cplus
6069 || cu->language == language_java)
63d06c5c
DC
6070 ? &objfile->global_psymbols
6071 : &objfile->static_psymbols,
e142c38c 6072 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6073
c906108c
SS
6074 break;
6075 case DW_TAG_enumerator:
38d518c9 6076 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6077 built_actual_name != NULL,
176620f1 6078 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6079 (cu->language == language_cplus
6080 || cu->language == language_java)
f6fe98ef
DJ
6081 ? &objfile->global_psymbols
6082 : &objfile->static_psymbols,
e142c38c 6083 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6084 break;
6085 default:
6086 break;
6087 }
5c4e30ca 6088
15d034d0 6089 xfree (built_actual_name);
c906108c
SS
6090}
6091
5c4e30ca
DC
6092/* Read a partial die corresponding to a namespace; also, add a symbol
6093 corresponding to that namespace to the symbol table. NAMESPACE is
6094 the name of the enclosing namespace. */
91c24f0a 6095
72bf9492
DJ
6096static void
6097add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6098 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6099 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6100{
72bf9492 6101 /* Add a symbol for the namespace. */
e7c27a73 6102
72bf9492 6103 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6104
6105 /* Now scan partial symbols in that namespace. */
6106
91c24f0a 6107 if (pdi->has_children)
5734ee8b 6108 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6109}
6110
5d7cb8df
JK
6111/* Read a partial die corresponding to a Fortran module. */
6112
6113static void
6114add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6115 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6116{
f55ee35c 6117 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6118
6119 if (pdi->has_children)
6120 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6121}
6122
bc30ff58
JB
6123/* Read a partial die corresponding to a subprogram and create a partial
6124 symbol for that subprogram. When the CU language allows it, this
6125 routine also defines a partial symbol for each nested subprogram
6126 that this subprogram contains.
6e70227d 6127
bc30ff58
JB
6128 DIE my also be a lexical block, in which case we simply search
6129 recursively for suprograms defined inside that lexical block.
6130 Again, this is only performed when the CU language allows this
6131 type of definitions. */
6132
6133static void
6134add_partial_subprogram (struct partial_die_info *pdi,
6135 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6136 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6137{
6138 if (pdi->tag == DW_TAG_subprogram)
6139 {
6140 if (pdi->has_pc_info)
6141 {
6142 if (pdi->lowpc < *lowpc)
6143 *lowpc = pdi->lowpc;
6144 if (pdi->highpc > *highpc)
6145 *highpc = pdi->highpc;
5734ee8b
DJ
6146 if (need_pc)
6147 {
6148 CORE_ADDR baseaddr;
6149 struct objfile *objfile = cu->objfile;
6150
6151 baseaddr = ANOFFSET (objfile->section_offsets,
6152 SECT_OFF_TEXT (objfile));
6153 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6154 pdi->lowpc + baseaddr,
6155 pdi->highpc - 1 + baseaddr,
9291a0cd 6156 cu->per_cu->v.psymtab);
5734ee8b 6157 }
481860b3
GB
6158 }
6159
6160 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6161 {
bc30ff58 6162 if (!pdi->is_declaration)
e8d05480
JB
6163 /* Ignore subprogram DIEs that do not have a name, they are
6164 illegal. Do not emit a complaint at this point, we will
6165 do so when we convert this psymtab into a symtab. */
6166 if (pdi->name)
6167 add_partial_symbol (pdi, cu);
bc30ff58
JB
6168 }
6169 }
6e70227d 6170
bc30ff58
JB
6171 if (! pdi->has_children)
6172 return;
6173
6174 if (cu->language == language_ada)
6175 {
6176 pdi = pdi->die_child;
6177 while (pdi != NULL)
6178 {
6179 fixup_partial_die (pdi, cu);
6180 if (pdi->tag == DW_TAG_subprogram
6181 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6182 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6183 pdi = pdi->die_sibling;
6184 }
6185 }
6186}
6187
91c24f0a
DC
6188/* Read a partial die corresponding to an enumeration type. */
6189
72bf9492
DJ
6190static void
6191add_partial_enumeration (struct partial_die_info *enum_pdi,
6192 struct dwarf2_cu *cu)
91c24f0a 6193{
72bf9492 6194 struct partial_die_info *pdi;
91c24f0a
DC
6195
6196 if (enum_pdi->name != NULL)
72bf9492
DJ
6197 add_partial_symbol (enum_pdi, cu);
6198
6199 pdi = enum_pdi->die_child;
6200 while (pdi)
91c24f0a 6201 {
72bf9492 6202 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6203 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6204 else
72bf9492
DJ
6205 add_partial_symbol (pdi, cu);
6206 pdi = pdi->die_sibling;
91c24f0a 6207 }
91c24f0a
DC
6208}
6209
6caca83c
CC
6210/* Return the initial uleb128 in the die at INFO_PTR. */
6211
6212static unsigned int
6213peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6214{
6215 unsigned int bytes_read;
6216
6217 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6218}
6219
4bb7a0a7
DJ
6220/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6221 Return the corresponding abbrev, or NULL if the number is zero (indicating
6222 an empty DIE). In either case *BYTES_READ will be set to the length of
6223 the initial number. */
6224
6225static struct abbrev_info *
fe1b8b76 6226peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6227 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6228{
6229 bfd *abfd = cu->objfile->obfd;
6230 unsigned int abbrev_number;
6231 struct abbrev_info *abbrev;
6232
6233 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6234
6235 if (abbrev_number == 0)
6236 return NULL;
6237
433df2d4 6238 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6239 if (!abbrev)
6240 {
3e43a32a
MS
6241 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6242 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6243 }
6244
6245 return abbrev;
6246}
6247
93311388
DE
6248/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6249 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6250 DIE. Any children of the skipped DIEs will also be skipped. */
6251
fe1b8b76 6252static gdb_byte *
dee91e82 6253skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6254{
dee91e82 6255 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6256 struct abbrev_info *abbrev;
6257 unsigned int bytes_read;
6258
6259 while (1)
6260 {
6261 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6262 if (abbrev == NULL)
6263 return info_ptr + bytes_read;
6264 else
dee91e82 6265 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6266 }
6267}
6268
93311388
DE
6269/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6270 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6271 abbrev corresponding to that skipped uleb128 should be passed in
6272 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6273 children. */
6274
fe1b8b76 6275static gdb_byte *
dee91e82
DE
6276skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6277 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6278{
6279 unsigned int bytes_read;
6280 struct attribute attr;
dee91e82
DE
6281 bfd *abfd = reader->abfd;
6282 struct dwarf2_cu *cu = reader->cu;
6283 gdb_byte *buffer = reader->buffer;
f664829e
DE
6284 const gdb_byte *buffer_end = reader->buffer_end;
6285 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6286 unsigned int form, i;
6287
6288 for (i = 0; i < abbrev->num_attrs; i++)
6289 {
6290 /* The only abbrev we care about is DW_AT_sibling. */
6291 if (abbrev->attrs[i].name == DW_AT_sibling)
6292 {
dee91e82 6293 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6294 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6295 complaint (&symfile_complaints,
6296 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6297 else
b64f50a1 6298 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6299 }
6300
6301 /* If it isn't DW_AT_sibling, skip this attribute. */
6302 form = abbrev->attrs[i].form;
6303 skip_attribute:
6304 switch (form)
6305 {
4bb7a0a7 6306 case DW_FORM_ref_addr:
ae411497
TT
6307 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6308 and later it is offset sized. */
6309 if (cu->header.version == 2)
6310 info_ptr += cu->header.addr_size;
6311 else
6312 info_ptr += cu->header.offset_size;
6313 break;
36586728
TT
6314 case DW_FORM_GNU_ref_alt:
6315 info_ptr += cu->header.offset_size;
6316 break;
ae411497 6317 case DW_FORM_addr:
4bb7a0a7
DJ
6318 info_ptr += cu->header.addr_size;
6319 break;
6320 case DW_FORM_data1:
6321 case DW_FORM_ref1:
6322 case DW_FORM_flag:
6323 info_ptr += 1;
6324 break;
2dc7f7b3
TT
6325 case DW_FORM_flag_present:
6326 break;
4bb7a0a7
DJ
6327 case DW_FORM_data2:
6328 case DW_FORM_ref2:
6329 info_ptr += 2;
6330 break;
6331 case DW_FORM_data4:
6332 case DW_FORM_ref4:
6333 info_ptr += 4;
6334 break;
6335 case DW_FORM_data8:
6336 case DW_FORM_ref8:
55f1336d 6337 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6338 info_ptr += 8;
6339 break;
6340 case DW_FORM_string:
9b1c24c8 6341 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6342 info_ptr += bytes_read;
6343 break;
2dc7f7b3 6344 case DW_FORM_sec_offset:
4bb7a0a7 6345 case DW_FORM_strp:
36586728 6346 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6347 info_ptr += cu->header.offset_size;
6348 break;
2dc7f7b3 6349 case DW_FORM_exprloc:
4bb7a0a7
DJ
6350 case DW_FORM_block:
6351 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6352 info_ptr += bytes_read;
6353 break;
6354 case DW_FORM_block1:
6355 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6356 break;
6357 case DW_FORM_block2:
6358 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6359 break;
6360 case DW_FORM_block4:
6361 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6362 break;
6363 case DW_FORM_sdata:
6364 case DW_FORM_udata:
6365 case DW_FORM_ref_udata:
3019eac3
DE
6366 case DW_FORM_GNU_addr_index:
6367 case DW_FORM_GNU_str_index:
f664829e 6368 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6369 break;
6370 case DW_FORM_indirect:
6371 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6372 info_ptr += bytes_read;
6373 /* We need to continue parsing from here, so just go back to
6374 the top. */
6375 goto skip_attribute;
6376
6377 default:
3e43a32a
MS
6378 error (_("Dwarf Error: Cannot handle %s "
6379 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6380 dwarf_form_name (form),
6381 bfd_get_filename (abfd));
6382 }
6383 }
6384
6385 if (abbrev->has_children)
dee91e82 6386 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6387 else
6388 return info_ptr;
6389}
6390
93311388 6391/* Locate ORIG_PDI's sibling.
dee91e82 6392 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6393
fe1b8b76 6394static gdb_byte *
dee91e82
DE
6395locate_pdi_sibling (const struct die_reader_specs *reader,
6396 struct partial_die_info *orig_pdi,
6397 gdb_byte *info_ptr)
91c24f0a
DC
6398{
6399 /* Do we know the sibling already? */
72bf9492 6400
91c24f0a
DC
6401 if (orig_pdi->sibling)
6402 return orig_pdi->sibling;
6403
6404 /* Are there any children to deal with? */
6405
6406 if (!orig_pdi->has_children)
6407 return info_ptr;
6408
4bb7a0a7 6409 /* Skip the children the long way. */
91c24f0a 6410
dee91e82 6411 return skip_children (reader, info_ptr);
91c24f0a
DC
6412}
6413
257e7a09 6414/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6415 not NULL. */
c906108c
SS
6416
6417static void
257e7a09
YQ
6418dwarf2_read_symtab (struct partial_symtab *self,
6419 struct objfile *objfile)
c906108c 6420{
257e7a09 6421 if (self->readin)
c906108c 6422 {
442e4d9c 6423 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6424 self->filename);
442e4d9c
YQ
6425 }
6426 else
6427 {
6428 if (info_verbose)
c906108c 6429 {
442e4d9c 6430 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6431 self->filename);
442e4d9c 6432 gdb_flush (gdb_stdout);
c906108c 6433 }
c906108c 6434
442e4d9c
YQ
6435 /* Restore our global data. */
6436 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6437
442e4d9c
YQ
6438 /* If this psymtab is constructed from a debug-only objfile, the
6439 has_section_at_zero flag will not necessarily be correct. We
6440 can get the correct value for this flag by looking at the data
6441 associated with the (presumably stripped) associated objfile. */
6442 if (objfile->separate_debug_objfile_backlink)
6443 {
6444 struct dwarf2_per_objfile *dpo_backlink
6445 = objfile_data (objfile->separate_debug_objfile_backlink,
6446 dwarf2_objfile_data_key);
9a619af0 6447
442e4d9c
YQ
6448 dwarf2_per_objfile->has_section_at_zero
6449 = dpo_backlink->has_section_at_zero;
6450 }
b2ab525c 6451
442e4d9c 6452 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6453
257e7a09 6454 psymtab_to_symtab_1 (self);
c906108c 6455
442e4d9c
YQ
6456 /* Finish up the debug error message. */
6457 if (info_verbose)
6458 printf_filtered (_("done.\n"));
c906108c 6459 }
95554aad
TT
6460
6461 process_cu_includes ();
c906108c 6462}
9cdd5dbd
DE
6463\f
6464/* Reading in full CUs. */
c906108c 6465
10b3939b
DJ
6466/* Add PER_CU to the queue. */
6467
6468static void
95554aad
TT
6469queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6470 enum language pretend_language)
10b3939b
DJ
6471{
6472 struct dwarf2_queue_item *item;
6473
6474 per_cu->queued = 1;
6475 item = xmalloc (sizeof (*item));
6476 item->per_cu = per_cu;
95554aad 6477 item->pretend_language = pretend_language;
10b3939b
DJ
6478 item->next = NULL;
6479
6480 if (dwarf2_queue == NULL)
6481 dwarf2_queue = item;
6482 else
6483 dwarf2_queue_tail->next = item;
6484
6485 dwarf2_queue_tail = item;
6486}
6487
0907af0c
DE
6488/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6489 unit and add it to our queue.
6490 The result is non-zero if PER_CU was queued, otherwise the result is zero
6491 meaning either PER_CU is already queued or it is already loaded. */
6492
6493static int
6494maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6495 struct dwarf2_per_cu_data *per_cu,
6496 enum language pretend_language)
6497{
6498 /* We may arrive here during partial symbol reading, if we need full
6499 DIEs to process an unusual case (e.g. template arguments). Do
6500 not queue PER_CU, just tell our caller to load its DIEs. */
6501 if (dwarf2_per_objfile->reading_partial_symbols)
6502 {
6503 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6504 return 1;
6505 return 0;
6506 }
6507
6508 /* Mark the dependence relation so that we don't flush PER_CU
6509 too early. */
6510 dwarf2_add_dependence (this_cu, per_cu);
6511
6512 /* If it's already on the queue, we have nothing to do. */
6513 if (per_cu->queued)
6514 return 0;
6515
6516 /* If the compilation unit is already loaded, just mark it as
6517 used. */
6518 if (per_cu->cu != NULL)
6519 {
6520 per_cu->cu->last_used = 0;
6521 return 0;
6522 }
6523
6524 /* Add it to the queue. */
6525 queue_comp_unit (per_cu, pretend_language);
6526
6527 return 1;
6528}
6529
10b3939b
DJ
6530/* Process the queue. */
6531
6532static void
a0f42c21 6533process_queue (void)
10b3939b
DJ
6534{
6535 struct dwarf2_queue_item *item, *next_item;
6536
45cfd468
DE
6537 if (dwarf2_read_debug)
6538 {
6539 fprintf_unfiltered (gdb_stdlog,
6540 "Expanding one or more symtabs of objfile %s ...\n",
6541 dwarf2_per_objfile->objfile->name);
6542 }
6543
03dd20cc
DJ
6544 /* The queue starts out with one item, but following a DIE reference
6545 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6546 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6547 {
9291a0cd
TT
6548 if (dwarf2_per_objfile->using_index
6549 ? !item->per_cu->v.quick->symtab
6550 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6551 {
6552 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6553
6554 if (dwarf2_read_debug)
6555 {
6556 fprintf_unfiltered (gdb_stdlog,
6557 "Expanding symtab of %s at offset 0x%x\n",
6558 per_cu->is_debug_types ? "TU" : "CU",
6559 per_cu->offset.sect_off);
6560 }
6561
6562 if (per_cu->is_debug_types)
6563 process_full_type_unit (per_cu, item->pretend_language);
6564 else
6565 process_full_comp_unit (per_cu, item->pretend_language);
6566
6567 if (dwarf2_read_debug)
6568 {
6569 fprintf_unfiltered (gdb_stdlog,
6570 "Done expanding %s at offset 0x%x\n",
6571 per_cu->is_debug_types ? "TU" : "CU",
6572 per_cu->offset.sect_off);
6573 }
6574 }
10b3939b
DJ
6575
6576 item->per_cu->queued = 0;
6577 next_item = item->next;
6578 xfree (item);
6579 }
6580
6581 dwarf2_queue_tail = NULL;
45cfd468
DE
6582
6583 if (dwarf2_read_debug)
6584 {
6585 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6586 dwarf2_per_objfile->objfile->name);
6587 }
10b3939b
DJ
6588}
6589
6590/* Free all allocated queue entries. This function only releases anything if
6591 an error was thrown; if the queue was processed then it would have been
6592 freed as we went along. */
6593
6594static void
6595dwarf2_release_queue (void *dummy)
6596{
6597 struct dwarf2_queue_item *item, *last;
6598
6599 item = dwarf2_queue;
6600 while (item)
6601 {
6602 /* Anything still marked queued is likely to be in an
6603 inconsistent state, so discard it. */
6604 if (item->per_cu->queued)
6605 {
6606 if (item->per_cu->cu != NULL)
dee91e82 6607 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6608 item->per_cu->queued = 0;
6609 }
6610
6611 last = item;
6612 item = item->next;
6613 xfree (last);
6614 }
6615
6616 dwarf2_queue = dwarf2_queue_tail = NULL;
6617}
6618
6619/* Read in full symbols for PST, and anything it depends on. */
6620
c906108c 6621static void
fba45db2 6622psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6623{
10b3939b 6624 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6625 int i;
6626
95554aad
TT
6627 if (pst->readin)
6628 return;
6629
aaa75496 6630 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6631 if (!pst->dependencies[i]->readin
6632 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6633 {
6634 /* Inform about additional files that need to be read in. */
6635 if (info_verbose)
6636 {
a3f17187 6637 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6638 fputs_filtered (" ", gdb_stdout);
6639 wrap_here ("");
6640 fputs_filtered ("and ", gdb_stdout);
6641 wrap_here ("");
6642 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6643 wrap_here (""); /* Flush output. */
aaa75496
JB
6644 gdb_flush (gdb_stdout);
6645 }
6646 psymtab_to_symtab_1 (pst->dependencies[i]);
6647 }
6648
e38df1d0 6649 per_cu = pst->read_symtab_private;
10b3939b
DJ
6650
6651 if (per_cu == NULL)
aaa75496
JB
6652 {
6653 /* It's an include file, no symbols to read for it.
6654 Everything is in the parent symtab. */
6655 pst->readin = 1;
6656 return;
6657 }
c906108c 6658
a0f42c21 6659 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6660}
6661
dee91e82
DE
6662/* Trivial hash function for die_info: the hash value of a DIE
6663 is its offset in .debug_info for this objfile. */
10b3939b 6664
dee91e82
DE
6665static hashval_t
6666die_hash (const void *item)
10b3939b 6667{
dee91e82 6668 const struct die_info *die = item;
6502dd73 6669
dee91e82
DE
6670 return die->offset.sect_off;
6671}
63d06c5c 6672
dee91e82
DE
6673/* Trivial comparison function for die_info structures: two DIEs
6674 are equal if they have the same offset. */
98bfdba5 6675
dee91e82
DE
6676static int
6677die_eq (const void *item_lhs, const void *item_rhs)
6678{
6679 const struct die_info *die_lhs = item_lhs;
6680 const struct die_info *die_rhs = item_rhs;
c906108c 6681
dee91e82
DE
6682 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6683}
c906108c 6684
dee91e82
DE
6685/* die_reader_func for load_full_comp_unit.
6686 This is identical to read_signatured_type_reader,
6687 but is kept separate for now. */
c906108c 6688
dee91e82
DE
6689static void
6690load_full_comp_unit_reader (const struct die_reader_specs *reader,
6691 gdb_byte *info_ptr,
6692 struct die_info *comp_unit_die,
6693 int has_children,
6694 void *data)
6695{
6696 struct dwarf2_cu *cu = reader->cu;
95554aad 6697 enum language *language_ptr = data;
6caca83c 6698
dee91e82
DE
6699 gdb_assert (cu->die_hash == NULL);
6700 cu->die_hash =
6701 htab_create_alloc_ex (cu->header.length / 12,
6702 die_hash,
6703 die_eq,
6704 NULL,
6705 &cu->comp_unit_obstack,
6706 hashtab_obstack_allocate,
6707 dummy_obstack_deallocate);
e142c38c 6708
dee91e82
DE
6709 if (has_children)
6710 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6711 &info_ptr, comp_unit_die);
6712 cu->dies = comp_unit_die;
6713 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6714
6715 /* We try not to read any attributes in this function, because not
9cdd5dbd 6716 all CUs needed for references have been loaded yet, and symbol
10b3939b 6717 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6718 or we won't be able to build types correctly.
6719 Similarly, if we do not read the producer, we can not apply
6720 producer-specific interpretation. */
95554aad 6721 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6722}
10b3939b 6723
dee91e82 6724/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6725
dee91e82 6726static void
95554aad
TT
6727load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6728 enum language pretend_language)
dee91e82 6729{
3019eac3 6730 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6731
f4dc4d17
DE
6732 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6733 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6734}
6735
3da10d80
KS
6736/* Add a DIE to the delayed physname list. */
6737
6738static void
6739add_to_method_list (struct type *type, int fnfield_index, int index,
6740 const char *name, struct die_info *die,
6741 struct dwarf2_cu *cu)
6742{
6743 struct delayed_method_info mi;
6744 mi.type = type;
6745 mi.fnfield_index = fnfield_index;
6746 mi.index = index;
6747 mi.name = name;
6748 mi.die = die;
6749 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6750}
6751
6752/* A cleanup for freeing the delayed method list. */
6753
6754static void
6755free_delayed_list (void *ptr)
6756{
6757 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6758 if (cu->method_list != NULL)
6759 {
6760 VEC_free (delayed_method_info, cu->method_list);
6761 cu->method_list = NULL;
6762 }
6763}
6764
6765/* Compute the physnames of any methods on the CU's method list.
6766
6767 The computation of method physnames is delayed in order to avoid the
6768 (bad) condition that one of the method's formal parameters is of an as yet
6769 incomplete type. */
6770
6771static void
6772compute_delayed_physnames (struct dwarf2_cu *cu)
6773{
6774 int i;
6775 struct delayed_method_info *mi;
6776 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6777 {
1d06ead6 6778 const char *physname;
3da10d80
KS
6779 struct fn_fieldlist *fn_flp
6780 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6781 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6782 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6783 }
6784}
6785
a766d390
DE
6786/* Go objects should be embedded in a DW_TAG_module DIE,
6787 and it's not clear if/how imported objects will appear.
6788 To keep Go support simple until that's worked out,
6789 go back through what we've read and create something usable.
6790 We could do this while processing each DIE, and feels kinda cleaner,
6791 but that way is more invasive.
6792 This is to, for example, allow the user to type "p var" or "b main"
6793 without having to specify the package name, and allow lookups
6794 of module.object to work in contexts that use the expression
6795 parser. */
6796
6797static void
6798fixup_go_packaging (struct dwarf2_cu *cu)
6799{
6800 char *package_name = NULL;
6801 struct pending *list;
6802 int i;
6803
6804 for (list = global_symbols; list != NULL; list = list->next)
6805 {
6806 for (i = 0; i < list->nsyms; ++i)
6807 {
6808 struct symbol *sym = list->symbol[i];
6809
6810 if (SYMBOL_LANGUAGE (sym) == language_go
6811 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6812 {
6813 char *this_package_name = go_symbol_package_name (sym);
6814
6815 if (this_package_name == NULL)
6816 continue;
6817 if (package_name == NULL)
6818 package_name = this_package_name;
6819 else
6820 {
6821 if (strcmp (package_name, this_package_name) != 0)
6822 complaint (&symfile_complaints,
6823 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6824 (SYMBOL_SYMTAB (sym)
05cba821 6825 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6826 : cu->objfile->name),
6827 this_package_name, package_name);
6828 xfree (this_package_name);
6829 }
6830 }
6831 }
6832 }
6833
6834 if (package_name != NULL)
6835 {
6836 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6837 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6838 package_name,
6839 strlen (package_name));
a766d390 6840 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6841 saved_package_name, objfile);
a766d390
DE
6842 struct symbol *sym;
6843
6844 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6845
6846 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6847 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6848 SYMBOL_SET_NAMES (sym, saved_package_name,
6849 strlen (saved_package_name), 0, objfile);
a766d390
DE
6850 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6851 e.g., "main" finds the "main" module and not C's main(). */
6852 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6853 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6854 SYMBOL_TYPE (sym) = type;
6855
6856 add_symbol_to_list (sym, &global_symbols);
6857
6858 xfree (package_name);
6859 }
6860}
6861
95554aad
TT
6862static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6863
6864/* Return the symtab for PER_CU. This works properly regardless of
6865 whether we're using the index or psymtabs. */
6866
6867static struct symtab *
6868get_symtab (struct dwarf2_per_cu_data *per_cu)
6869{
6870 return (dwarf2_per_objfile->using_index
6871 ? per_cu->v.quick->symtab
6872 : per_cu->v.psymtab->symtab);
6873}
6874
6875/* A helper function for computing the list of all symbol tables
6876 included by PER_CU. */
6877
6878static void
6879recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6880 htab_t all_children,
6881 struct dwarf2_per_cu_data *per_cu)
6882{
6883 void **slot;
6884 int ix;
6885 struct dwarf2_per_cu_data *iter;
6886
6887 slot = htab_find_slot (all_children, per_cu, INSERT);
6888 if (*slot != NULL)
6889 {
6890 /* This inclusion and its children have been processed. */
6891 return;
6892 }
6893
6894 *slot = per_cu;
6895 /* Only add a CU if it has a symbol table. */
6896 if (get_symtab (per_cu) != NULL)
6897 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6898
6899 for (ix = 0;
796a7ff8 6900 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
6901 ++ix)
6902 recursively_compute_inclusions (result, all_children, iter);
6903}
6904
6905/* Compute the symtab 'includes' fields for the symtab related to
6906 PER_CU. */
6907
6908static void
6909compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6910{
f4dc4d17
DE
6911 gdb_assert (! per_cu->is_debug_types);
6912
796a7ff8 6913 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
6914 {
6915 int ix, len;
6916 struct dwarf2_per_cu_data *iter;
6917 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6918 htab_t all_children;
6919 struct symtab *symtab = get_symtab (per_cu);
6920
6921 /* If we don't have a symtab, we can just skip this case. */
6922 if (symtab == NULL)
6923 return;
6924
6925 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6926 NULL, xcalloc, xfree);
6927
6928 for (ix = 0;
796a7ff8 6929 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
6930 ix, iter);
6931 ++ix)
6932 recursively_compute_inclusions (&result_children, all_children, iter);
6933
796a7ff8
DE
6934 /* Now we have a transitive closure of all the included CUs, and
6935 for .gdb_index version 7 the included TUs, so we can convert it
6936 to a list of symtabs. */
95554aad
TT
6937 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6938 symtab->includes
6939 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6940 (len + 1) * sizeof (struct symtab *));
6941 for (ix = 0;
6942 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6943 ++ix)
6944 symtab->includes[ix] = get_symtab (iter);
6945 symtab->includes[len] = NULL;
6946
6947 VEC_free (dwarf2_per_cu_ptr, result_children);
6948 htab_delete (all_children);
6949 }
6950}
6951
6952/* Compute the 'includes' field for the symtabs of all the CUs we just
6953 read. */
6954
6955static void
6956process_cu_includes (void)
6957{
6958 int ix;
6959 struct dwarf2_per_cu_data *iter;
6960
6961 for (ix = 0;
6962 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6963 ix, iter);
6964 ++ix)
f4dc4d17
DE
6965 {
6966 if (! iter->is_debug_types)
6967 compute_symtab_includes (iter);
6968 }
95554aad
TT
6969
6970 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6971}
6972
9cdd5dbd 6973/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6974 already been loaded into memory. */
6975
6976static void
95554aad
TT
6977process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6978 enum language pretend_language)
10b3939b 6979{
10b3939b 6980 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6981 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6982 CORE_ADDR lowpc, highpc;
6983 struct symtab *symtab;
3da10d80 6984 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6985 CORE_ADDR baseaddr;
4359dff1 6986 struct block *static_block;
10b3939b
DJ
6987
6988 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6989
10b3939b
DJ
6990 buildsym_init ();
6991 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6992 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6993
6994 cu->list_in_scope = &file_symbols;
c906108c 6995
95554aad
TT
6996 cu->language = pretend_language;
6997 cu->language_defn = language_def (cu->language);
6998
c906108c 6999 /* Do line number decoding in read_file_scope () */
10b3939b 7000 process_die (cu->dies, cu);
c906108c 7001
a766d390
DE
7002 /* For now fudge the Go package. */
7003 if (cu->language == language_go)
7004 fixup_go_packaging (cu);
7005
3da10d80
KS
7006 /* Now that we have processed all the DIEs in the CU, all the types
7007 should be complete, and it should now be safe to compute all of the
7008 physnames. */
7009 compute_delayed_physnames (cu);
7010 do_cleanups (delayed_list_cleanup);
7011
fae299cd
DC
7012 /* Some compilers don't define a DW_AT_high_pc attribute for the
7013 compilation unit. If the DW_AT_high_pc is missing, synthesize
7014 it, by scanning the DIE's below the compilation unit. */
10b3939b 7015 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7016
36586728
TT
7017 static_block
7018 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7019 per_cu->imported_symtabs != NULL);
4359dff1
JK
7020
7021 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7022 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7023 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7024 addrmap to help ensure it has an accurate map of pc values belonging to
7025 this comp unit. */
7026 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7027
7028 symtab = end_symtab_from_static_block (static_block, objfile,
7029 SECT_OFF_TEXT (objfile), 0);
c906108c 7030
8be455d7 7031 if (symtab != NULL)
c906108c 7032 {
df15bd07 7033 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7034
8be455d7
JK
7035 /* Set symtab language to language from DW_AT_language. If the
7036 compilation is from a C file generated by language preprocessors, do
7037 not set the language if it was already deduced by start_subfile. */
7038 if (!(cu->language == language_c && symtab->language != language_c))
7039 symtab->language = cu->language;
7040
7041 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7042 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7043 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7044 there were bugs in prologue debug info, fixed later in GCC-4.5
7045 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7046
7047 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7048 needed, it would be wrong due to missing DW_AT_producer there.
7049
7050 Still one can confuse GDB by using non-standard GCC compilation
7051 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7052 */
ab260dad 7053 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7054 symtab->locations_valid = 1;
e0d00bc7
JK
7055
7056 if (gcc_4_minor >= 5)
7057 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7058
7059 symtab->call_site_htab = cu->call_site_htab;
c906108c 7060 }
9291a0cd
TT
7061
7062 if (dwarf2_per_objfile->using_index)
7063 per_cu->v.quick->symtab = symtab;
7064 else
7065 {
7066 struct partial_symtab *pst = per_cu->v.psymtab;
7067 pst->symtab = symtab;
7068 pst->readin = 1;
7069 }
c906108c 7070
95554aad
TT
7071 /* Push it for inclusion processing later. */
7072 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7073
c906108c 7074 do_cleanups (back_to);
f4dc4d17 7075}
45cfd468 7076
f4dc4d17
DE
7077/* Generate full symbol information for type unit PER_CU, whose DIEs have
7078 already been loaded into memory. */
7079
7080static void
7081process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7082 enum language pretend_language)
7083{
7084 struct dwarf2_cu *cu = per_cu->cu;
7085 struct objfile *objfile = per_cu->objfile;
7086 struct symtab *symtab;
7087 struct cleanup *back_to, *delayed_list_cleanup;
7088
7089 buildsym_init ();
7090 back_to = make_cleanup (really_free_pendings, NULL);
7091 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7092
7093 cu->list_in_scope = &file_symbols;
7094
7095 cu->language = pretend_language;
7096 cu->language_defn = language_def (cu->language);
7097
7098 /* The symbol tables are set up in read_type_unit_scope. */
7099 process_die (cu->dies, cu);
7100
7101 /* For now fudge the Go package. */
7102 if (cu->language == language_go)
7103 fixup_go_packaging (cu);
7104
7105 /* Now that we have processed all the DIEs in the CU, all the types
7106 should be complete, and it should now be safe to compute all of the
7107 physnames. */
7108 compute_delayed_physnames (cu);
7109 do_cleanups (delayed_list_cleanup);
7110
7111 /* TUs share symbol tables.
7112 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7113 of it with end_expandable_symtab. Otherwise, complete the addition of
7114 this TU's symbols to the existing symtab. */
796a7ff8 7115 if (per_cu->type_unit_group->primary_symtab == NULL)
45cfd468 7116 {
f4dc4d17 7117 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
796a7ff8 7118 per_cu->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7119
7120 if (symtab != NULL)
7121 {
7122 /* Set symtab language to language from DW_AT_language. If the
7123 compilation is from a C file generated by language preprocessors,
7124 do not set the language if it was already deduced by
7125 start_subfile. */
7126 if (!(cu->language == language_c && symtab->language != language_c))
7127 symtab->language = cu->language;
7128 }
7129 }
7130 else
7131 {
7132 augment_type_symtab (objfile,
796a7ff8
DE
7133 per_cu->type_unit_group->primary_symtab);
7134 symtab = per_cu->type_unit_group->primary_symtab;
f4dc4d17
DE
7135 }
7136
7137 if (dwarf2_per_objfile->using_index)
7138 per_cu->v.quick->symtab = symtab;
7139 else
7140 {
7141 struct partial_symtab *pst = per_cu->v.psymtab;
7142 pst->symtab = symtab;
7143 pst->readin = 1;
45cfd468 7144 }
f4dc4d17
DE
7145
7146 do_cleanups (back_to);
c906108c
SS
7147}
7148
95554aad
TT
7149/* Process an imported unit DIE. */
7150
7151static void
7152process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7153{
7154 struct attribute *attr;
7155
f4dc4d17
DE
7156 /* For now we don't handle imported units in type units. */
7157 if (cu->per_cu->is_debug_types)
7158 {
7159 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7160 " supported in type units [in module %s]"),
7161 cu->objfile->name);
7162 }
7163
95554aad
TT
7164 attr = dwarf2_attr (die, DW_AT_import, cu);
7165 if (attr != NULL)
7166 {
7167 struct dwarf2_per_cu_data *per_cu;
7168 struct symtab *imported_symtab;
7169 sect_offset offset;
36586728 7170 int is_dwz;
95554aad
TT
7171
7172 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7173 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7174 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7175
7176 /* Queue the unit, if needed. */
7177 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7178 load_full_comp_unit (per_cu, cu->language);
7179
796a7ff8 7180 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7181 per_cu);
7182 }
7183}
7184
c906108c
SS
7185/* Process a die and its children. */
7186
7187static void
e7c27a73 7188process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7189{
7190 switch (die->tag)
7191 {
7192 case DW_TAG_padding:
7193 break;
7194 case DW_TAG_compile_unit:
95554aad 7195 case DW_TAG_partial_unit:
e7c27a73 7196 read_file_scope (die, cu);
c906108c 7197 break;
348e048f
DE
7198 case DW_TAG_type_unit:
7199 read_type_unit_scope (die, cu);
7200 break;
c906108c 7201 case DW_TAG_subprogram:
c906108c 7202 case DW_TAG_inlined_subroutine:
edb3359d 7203 read_func_scope (die, cu);
c906108c
SS
7204 break;
7205 case DW_TAG_lexical_block:
14898363
L
7206 case DW_TAG_try_block:
7207 case DW_TAG_catch_block:
e7c27a73 7208 read_lexical_block_scope (die, cu);
c906108c 7209 break;
96408a79
SA
7210 case DW_TAG_GNU_call_site:
7211 read_call_site_scope (die, cu);
7212 break;
c906108c 7213 case DW_TAG_class_type:
680b30c7 7214 case DW_TAG_interface_type:
c906108c
SS
7215 case DW_TAG_structure_type:
7216 case DW_TAG_union_type:
134d01f1 7217 process_structure_scope (die, cu);
c906108c
SS
7218 break;
7219 case DW_TAG_enumeration_type:
134d01f1 7220 process_enumeration_scope (die, cu);
c906108c 7221 break;
134d01f1 7222
f792889a
DJ
7223 /* These dies have a type, but processing them does not create
7224 a symbol or recurse to process the children. Therefore we can
7225 read them on-demand through read_type_die. */
c906108c 7226 case DW_TAG_subroutine_type:
72019c9c 7227 case DW_TAG_set_type:
c906108c 7228 case DW_TAG_array_type:
c906108c 7229 case DW_TAG_pointer_type:
c906108c 7230 case DW_TAG_ptr_to_member_type:
c906108c 7231 case DW_TAG_reference_type:
c906108c 7232 case DW_TAG_string_type:
c906108c 7233 break;
134d01f1 7234
c906108c 7235 case DW_TAG_base_type:
a02abb62 7236 case DW_TAG_subrange_type:
cb249c71 7237 case DW_TAG_typedef:
134d01f1
DJ
7238 /* Add a typedef symbol for the type definition, if it has a
7239 DW_AT_name. */
f792889a 7240 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7241 break;
c906108c 7242 case DW_TAG_common_block:
e7c27a73 7243 read_common_block (die, cu);
c906108c
SS
7244 break;
7245 case DW_TAG_common_inclusion:
7246 break;
d9fa45fe 7247 case DW_TAG_namespace:
4d4ec4e5 7248 cu->processing_has_namespace_info = 1;
e7c27a73 7249 read_namespace (die, cu);
d9fa45fe 7250 break;
5d7cb8df 7251 case DW_TAG_module:
4d4ec4e5 7252 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7253 read_module (die, cu);
7254 break;
d9fa45fe
DC
7255 case DW_TAG_imported_declaration:
7256 case DW_TAG_imported_module:
4d4ec4e5 7257 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7258 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7259 || cu->language != language_fortran))
7260 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7261 dwarf_tag_name (die->tag));
7262 read_import_statement (die, cu);
d9fa45fe 7263 break;
95554aad
TT
7264
7265 case DW_TAG_imported_unit:
7266 process_imported_unit_die (die, cu);
7267 break;
7268
c906108c 7269 default:
e7c27a73 7270 new_symbol (die, NULL, cu);
c906108c
SS
7271 break;
7272 }
7273}
7274
94af9270
KS
7275/* A helper function for dwarf2_compute_name which determines whether DIE
7276 needs to have the name of the scope prepended to the name listed in the
7277 die. */
7278
7279static int
7280die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7281{
1c809c68
TT
7282 struct attribute *attr;
7283
94af9270
KS
7284 switch (die->tag)
7285 {
7286 case DW_TAG_namespace:
7287 case DW_TAG_typedef:
7288 case DW_TAG_class_type:
7289 case DW_TAG_interface_type:
7290 case DW_TAG_structure_type:
7291 case DW_TAG_union_type:
7292 case DW_TAG_enumeration_type:
7293 case DW_TAG_enumerator:
7294 case DW_TAG_subprogram:
7295 case DW_TAG_member:
7296 return 1;
7297
7298 case DW_TAG_variable:
c2b0a229 7299 case DW_TAG_constant:
94af9270
KS
7300 /* We only need to prefix "globally" visible variables. These include
7301 any variable marked with DW_AT_external or any variable that
7302 lives in a namespace. [Variables in anonymous namespaces
7303 require prefixing, but they are not DW_AT_external.] */
7304
7305 if (dwarf2_attr (die, DW_AT_specification, cu))
7306 {
7307 struct dwarf2_cu *spec_cu = cu;
9a619af0 7308
94af9270
KS
7309 return die_needs_namespace (die_specification (die, &spec_cu),
7310 spec_cu);
7311 }
7312
1c809c68 7313 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7314 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7315 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7316 return 0;
7317 /* A variable in a lexical block of some kind does not need a
7318 namespace, even though in C++ such variables may be external
7319 and have a mangled name. */
7320 if (die->parent->tag == DW_TAG_lexical_block
7321 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7322 || die->parent->tag == DW_TAG_catch_block
7323 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7324 return 0;
7325 return 1;
94af9270
KS
7326
7327 default:
7328 return 0;
7329 }
7330}
7331
98bfdba5
PA
7332/* Retrieve the last character from a mem_file. */
7333
7334static void
7335do_ui_file_peek_last (void *object, const char *buffer, long length)
7336{
7337 char *last_char_p = (char *) object;
7338
7339 if (length > 0)
7340 *last_char_p = buffer[length - 1];
7341}
7342
94af9270 7343/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7344 compute the physname for the object, which include a method's:
7345 - formal parameters (C++/Java),
7346 - receiver type (Go),
7347 - return type (Java).
7348
7349 The term "physname" is a bit confusing.
7350 For C++, for example, it is the demangled name.
7351 For Go, for example, it's the mangled name.
94af9270 7352
af6b7be1
JB
7353 For Ada, return the DIE's linkage name rather than the fully qualified
7354 name. PHYSNAME is ignored..
7355
94af9270
KS
7356 The result is allocated on the objfile_obstack and canonicalized. */
7357
7358static const char *
15d034d0
TT
7359dwarf2_compute_name (const char *name,
7360 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7361 int physname)
7362{
bb5ed363
DE
7363 struct objfile *objfile = cu->objfile;
7364
94af9270
KS
7365 if (name == NULL)
7366 name = dwarf2_name (die, cu);
7367
f55ee35c
JK
7368 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7369 compute it by typename_concat inside GDB. */
7370 if (cu->language == language_ada
7371 || (cu->language == language_fortran && physname))
7372 {
7373 /* For Ada unit, we prefer the linkage name over the name, as
7374 the former contains the exported name, which the user expects
7375 to be able to reference. Ideally, we want the user to be able
7376 to reference this entity using either natural or linkage name,
7377 but we haven't started looking at this enhancement yet. */
7378 struct attribute *attr;
7379
7380 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7381 if (attr == NULL)
7382 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7383 if (attr && DW_STRING (attr))
7384 return DW_STRING (attr);
7385 }
7386
94af9270
KS
7387 /* These are the only languages we know how to qualify names in. */
7388 if (name != NULL
f55ee35c
JK
7389 && (cu->language == language_cplus || cu->language == language_java
7390 || cu->language == language_fortran))
94af9270
KS
7391 {
7392 if (die_needs_namespace (die, cu))
7393 {
7394 long length;
0d5cff50 7395 const char *prefix;
94af9270
KS
7396 struct ui_file *buf;
7397
7398 prefix = determine_prefix (die, cu);
7399 buf = mem_fileopen ();
7400 if (*prefix != '\0')
7401 {
f55ee35c
JK
7402 char *prefixed_name = typename_concat (NULL, prefix, name,
7403 physname, cu);
9a619af0 7404
94af9270
KS
7405 fputs_unfiltered (prefixed_name, buf);
7406 xfree (prefixed_name);
7407 }
7408 else
62d5b8da 7409 fputs_unfiltered (name, buf);
94af9270 7410
98bfdba5
PA
7411 /* Template parameters may be specified in the DIE's DW_AT_name, or
7412 as children with DW_TAG_template_type_param or
7413 DW_TAG_value_type_param. If the latter, add them to the name
7414 here. If the name already has template parameters, then
7415 skip this step; some versions of GCC emit both, and
7416 it is more efficient to use the pre-computed name.
7417
7418 Something to keep in mind about this process: it is very
7419 unlikely, or in some cases downright impossible, to produce
7420 something that will match the mangled name of a function.
7421 If the definition of the function has the same debug info,
7422 we should be able to match up with it anyway. But fallbacks
7423 using the minimal symbol, for instance to find a method
7424 implemented in a stripped copy of libstdc++, will not work.
7425 If we do not have debug info for the definition, we will have to
7426 match them up some other way.
7427
7428 When we do name matching there is a related problem with function
7429 templates; two instantiated function templates are allowed to
7430 differ only by their return types, which we do not add here. */
7431
7432 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7433 {
7434 struct attribute *attr;
7435 struct die_info *child;
7436 int first = 1;
7437
7438 die->building_fullname = 1;
7439
7440 for (child = die->child; child != NULL; child = child->sibling)
7441 {
7442 struct type *type;
12df843f 7443 LONGEST value;
98bfdba5
PA
7444 gdb_byte *bytes;
7445 struct dwarf2_locexpr_baton *baton;
7446 struct value *v;
7447
7448 if (child->tag != DW_TAG_template_type_param
7449 && child->tag != DW_TAG_template_value_param)
7450 continue;
7451
7452 if (first)
7453 {
7454 fputs_unfiltered ("<", buf);
7455 first = 0;
7456 }
7457 else
7458 fputs_unfiltered (", ", buf);
7459
7460 attr = dwarf2_attr (child, DW_AT_type, cu);
7461 if (attr == NULL)
7462 {
7463 complaint (&symfile_complaints,
7464 _("template parameter missing DW_AT_type"));
7465 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7466 continue;
7467 }
7468 type = die_type (child, cu);
7469
7470 if (child->tag == DW_TAG_template_type_param)
7471 {
79d43c61 7472 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7473 continue;
7474 }
7475
7476 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7477 if (attr == NULL)
7478 {
7479 complaint (&symfile_complaints,
3e43a32a
MS
7480 _("template parameter missing "
7481 "DW_AT_const_value"));
98bfdba5
PA
7482 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7483 continue;
7484 }
7485
7486 dwarf2_const_value_attr (attr, type, name,
7487 &cu->comp_unit_obstack, cu,
7488 &value, &bytes, &baton);
7489
7490 if (TYPE_NOSIGN (type))
7491 /* GDB prints characters as NUMBER 'CHAR'. If that's
7492 changed, this can use value_print instead. */
7493 c_printchar (value, type, buf);
7494 else
7495 {
7496 struct value_print_options opts;
7497
7498 if (baton != NULL)
7499 v = dwarf2_evaluate_loc_desc (type, NULL,
7500 baton->data,
7501 baton->size,
7502 baton->per_cu);
7503 else if (bytes != NULL)
7504 {
7505 v = allocate_value (type);
7506 memcpy (value_contents_writeable (v), bytes,
7507 TYPE_LENGTH (type));
7508 }
7509 else
7510 v = value_from_longest (type, value);
7511
3e43a32a
MS
7512 /* Specify decimal so that we do not depend on
7513 the radix. */
98bfdba5
PA
7514 get_formatted_print_options (&opts, 'd');
7515 opts.raw = 1;
7516 value_print (v, buf, &opts);
7517 release_value (v);
7518 value_free (v);
7519 }
7520 }
7521
7522 die->building_fullname = 0;
7523
7524 if (!first)
7525 {
7526 /* Close the argument list, with a space if necessary
7527 (nested templates). */
7528 char last_char = '\0';
7529 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7530 if (last_char == '>')
7531 fputs_unfiltered (" >", buf);
7532 else
7533 fputs_unfiltered (">", buf);
7534 }
7535 }
7536
94af9270
KS
7537 /* For Java and C++ methods, append formal parameter type
7538 information, if PHYSNAME. */
6e70227d 7539
94af9270
KS
7540 if (physname && die->tag == DW_TAG_subprogram
7541 && (cu->language == language_cplus
7542 || cu->language == language_java))
7543 {
7544 struct type *type = read_type_die (die, cu);
7545
79d43c61
TT
7546 c_type_print_args (type, buf, 1, cu->language,
7547 &type_print_raw_options);
94af9270
KS
7548
7549 if (cu->language == language_java)
7550 {
7551 /* For java, we must append the return type to method
0963b4bd 7552 names. */
94af9270
KS
7553 if (die->tag == DW_TAG_subprogram)
7554 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7555 0, 0, &type_print_raw_options);
94af9270
KS
7556 }
7557 else if (cu->language == language_cplus)
7558 {
60430eff
DJ
7559 /* Assume that an artificial first parameter is
7560 "this", but do not crash if it is not. RealView
7561 marks unnamed (and thus unused) parameters as
7562 artificial; there is no way to differentiate
7563 the two cases. */
94af9270
KS
7564 if (TYPE_NFIELDS (type) > 0
7565 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7566 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7567 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7568 0))))
94af9270
KS
7569 fputs_unfiltered (" const", buf);
7570 }
7571 }
7572
bb5ed363 7573 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7574 &length);
7575 ui_file_delete (buf);
7576
7577 if (cu->language == language_cplus)
7578 {
15d034d0 7579 const char *cname
94af9270 7580 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7581 &objfile->objfile_obstack);
9a619af0 7582
94af9270
KS
7583 if (cname != NULL)
7584 name = cname;
7585 }
7586 }
7587 }
7588
7589 return name;
7590}
7591
0114d602
DJ
7592/* Return the fully qualified name of DIE, based on its DW_AT_name.
7593 If scope qualifiers are appropriate they will be added. The result
7594 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7595 not have a name. NAME may either be from a previous call to
7596 dwarf2_name or NULL.
7597
0963b4bd 7598 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7599
7600static const char *
15d034d0 7601dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7602{
94af9270
KS
7603 return dwarf2_compute_name (name, die, cu, 0);
7604}
0114d602 7605
94af9270
KS
7606/* Construct a physname for the given DIE in CU. NAME may either be
7607 from a previous call to dwarf2_name or NULL. The result will be
7608 allocated on the objfile_objstack or NULL if the DIE does not have a
7609 name.
0114d602 7610
94af9270 7611 The output string will be canonicalized (if C++/Java). */
0114d602 7612
94af9270 7613static const char *
15d034d0 7614dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7615{
bb5ed363 7616 struct objfile *objfile = cu->objfile;
900e11f9
JK
7617 struct attribute *attr;
7618 const char *retval, *mangled = NULL, *canon = NULL;
7619 struct cleanup *back_to;
7620 int need_copy = 1;
7621
7622 /* In this case dwarf2_compute_name is just a shortcut not building anything
7623 on its own. */
7624 if (!die_needs_namespace (die, cu))
7625 return dwarf2_compute_name (name, die, cu, 1);
7626
7627 back_to = make_cleanup (null_cleanup, NULL);
7628
7629 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7630 if (!attr)
7631 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7632
7633 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7634 has computed. */
7635 if (attr && DW_STRING (attr))
7636 {
7637 char *demangled;
7638
7639 mangled = DW_STRING (attr);
7640
7641 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7642 type. It is easier for GDB users to search for such functions as
7643 `name(params)' than `long name(params)'. In such case the minimal
7644 symbol names do not match the full symbol names but for template
7645 functions there is never a need to look up their definition from their
7646 declaration so the only disadvantage remains the minimal symbol
7647 variant `long name(params)' does not have the proper inferior type.
7648 */
7649
a766d390
DE
7650 if (cu->language == language_go)
7651 {
7652 /* This is a lie, but we already lie to the caller new_symbol_full.
7653 new_symbol_full assumes we return the mangled name.
7654 This just undoes that lie until things are cleaned up. */
7655 demangled = NULL;
7656 }
7657 else
7658 {
7659 demangled = cplus_demangle (mangled,
7660 (DMGL_PARAMS | DMGL_ANSI
7661 | (cu->language == language_java
7662 ? DMGL_JAVA | DMGL_RET_POSTFIX
7663 : DMGL_RET_DROP)));
7664 }
900e11f9
JK
7665 if (demangled)
7666 {
7667 make_cleanup (xfree, demangled);
7668 canon = demangled;
7669 }
7670 else
7671 {
7672 canon = mangled;
7673 need_copy = 0;
7674 }
7675 }
7676
7677 if (canon == NULL || check_physname)
7678 {
7679 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7680
7681 if (canon != NULL && strcmp (physname, canon) != 0)
7682 {
7683 /* It may not mean a bug in GDB. The compiler could also
7684 compute DW_AT_linkage_name incorrectly. But in such case
7685 GDB would need to be bug-to-bug compatible. */
7686
7687 complaint (&symfile_complaints,
7688 _("Computed physname <%s> does not match demangled <%s> "
7689 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7690 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7691
7692 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7693 is available here - over computed PHYSNAME. It is safer
7694 against both buggy GDB and buggy compilers. */
7695
7696 retval = canon;
7697 }
7698 else
7699 {
7700 retval = physname;
7701 need_copy = 0;
7702 }
7703 }
7704 else
7705 retval = canon;
7706
7707 if (need_copy)
10f0c4bb 7708 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7709
7710 do_cleanups (back_to);
7711 return retval;
0114d602
DJ
7712}
7713
27aa8d6a
SW
7714/* Read the import statement specified by the given die and record it. */
7715
7716static void
7717read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7718{
bb5ed363 7719 struct objfile *objfile = cu->objfile;
27aa8d6a 7720 struct attribute *import_attr;
32019081 7721 struct die_info *imported_die, *child_die;
de4affc9 7722 struct dwarf2_cu *imported_cu;
27aa8d6a 7723 const char *imported_name;
794684b6 7724 const char *imported_name_prefix;
13387711
SW
7725 const char *canonical_name;
7726 const char *import_alias;
7727 const char *imported_declaration = NULL;
794684b6 7728 const char *import_prefix;
32019081
JK
7729 VEC (const_char_ptr) *excludes = NULL;
7730 struct cleanup *cleanups;
13387711 7731
27aa8d6a
SW
7732 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7733 if (import_attr == NULL)
7734 {
7735 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7736 dwarf_tag_name (die->tag));
7737 return;
7738 }
7739
de4affc9
CC
7740 imported_cu = cu;
7741 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7742 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7743 if (imported_name == NULL)
7744 {
7745 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7746
7747 The import in the following code:
7748 namespace A
7749 {
7750 typedef int B;
7751 }
7752
7753 int main ()
7754 {
7755 using A::B;
7756 B b;
7757 return b;
7758 }
7759
7760 ...
7761 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7762 <52> DW_AT_decl_file : 1
7763 <53> DW_AT_decl_line : 6
7764 <54> DW_AT_import : <0x75>
7765 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7766 <59> DW_AT_name : B
7767 <5b> DW_AT_decl_file : 1
7768 <5c> DW_AT_decl_line : 2
7769 <5d> DW_AT_type : <0x6e>
7770 ...
7771 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7772 <76> DW_AT_byte_size : 4
7773 <77> DW_AT_encoding : 5 (signed)
7774
7775 imports the wrong die ( 0x75 instead of 0x58 ).
7776 This case will be ignored until the gcc bug is fixed. */
7777 return;
7778 }
7779
82856980
SW
7780 /* Figure out the local name after import. */
7781 import_alias = dwarf2_name (die, cu);
27aa8d6a 7782
794684b6
SW
7783 /* Figure out where the statement is being imported to. */
7784 import_prefix = determine_prefix (die, cu);
7785
7786 /* Figure out what the scope of the imported die is and prepend it
7787 to the name of the imported die. */
de4affc9 7788 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7789
f55ee35c
JK
7790 if (imported_die->tag != DW_TAG_namespace
7791 && imported_die->tag != DW_TAG_module)
794684b6 7792 {
13387711
SW
7793 imported_declaration = imported_name;
7794 canonical_name = imported_name_prefix;
794684b6 7795 }
13387711 7796 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7797 canonical_name = obconcat (&objfile->objfile_obstack,
7798 imported_name_prefix, "::", imported_name,
7799 (char *) NULL);
13387711
SW
7800 else
7801 canonical_name = imported_name;
794684b6 7802
32019081
JK
7803 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7804
7805 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7806 for (child_die = die->child; child_die && child_die->tag;
7807 child_die = sibling_die (child_die))
7808 {
7809 /* DWARF-4: A Fortran use statement with a “rename list” may be
7810 represented by an imported module entry with an import attribute
7811 referring to the module and owned entries corresponding to those
7812 entities that are renamed as part of being imported. */
7813
7814 if (child_die->tag != DW_TAG_imported_declaration)
7815 {
7816 complaint (&symfile_complaints,
7817 _("child DW_TAG_imported_declaration expected "
7818 "- DIE at 0x%x [in module %s]"),
b64f50a1 7819 child_die->offset.sect_off, objfile->name);
32019081
JK
7820 continue;
7821 }
7822
7823 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7824 if (import_attr == NULL)
7825 {
7826 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7827 dwarf_tag_name (child_die->tag));
7828 continue;
7829 }
7830
7831 imported_cu = cu;
7832 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7833 &imported_cu);
7834 imported_name = dwarf2_name (imported_die, imported_cu);
7835 if (imported_name == NULL)
7836 {
7837 complaint (&symfile_complaints,
7838 _("child DW_TAG_imported_declaration has unknown "
7839 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7840 child_die->offset.sect_off, objfile->name);
32019081
JK
7841 continue;
7842 }
7843
7844 VEC_safe_push (const_char_ptr, excludes, imported_name);
7845
7846 process_die (child_die, cu);
7847 }
7848
c0cc3a76
SW
7849 cp_add_using_directive (import_prefix,
7850 canonical_name,
7851 import_alias,
13387711 7852 imported_declaration,
32019081 7853 excludes,
12aaed36 7854 0,
bb5ed363 7855 &objfile->objfile_obstack);
32019081
JK
7856
7857 do_cleanups (cleanups);
27aa8d6a
SW
7858}
7859
f4dc4d17 7860/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7861
cb1df416
DJ
7862static void
7863free_cu_line_header (void *arg)
7864{
7865 struct dwarf2_cu *cu = arg;
7866
7867 free_line_header (cu->line_header);
7868 cu->line_header = NULL;
7869}
7870
1b80a9fa
JK
7871/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7872 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7873 this, it was first present in GCC release 4.3.0. */
7874
7875static int
7876producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7877{
7878 if (!cu->checked_producer)
7879 check_producer (cu);
7880
7881 return cu->producer_is_gcc_lt_4_3;
7882}
7883
9291a0cd
TT
7884static void
7885find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7886 const char **name, const char **comp_dir)
9291a0cd
TT
7887{
7888 struct attribute *attr;
7889
7890 *name = NULL;
7891 *comp_dir = NULL;
7892
7893 /* Find the filename. Do not use dwarf2_name here, since the filename
7894 is not a source language identifier. */
7895 attr = dwarf2_attr (die, DW_AT_name, cu);
7896 if (attr)
7897 {
7898 *name = DW_STRING (attr);
7899 }
7900
7901 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7902 if (attr)
7903 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7904 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7905 && IS_ABSOLUTE_PATH (*name))
9291a0cd 7906 {
15d034d0
TT
7907 char *d = ldirname (*name);
7908
7909 *comp_dir = d;
7910 if (d != NULL)
7911 make_cleanup (xfree, d);
9291a0cd
TT
7912 }
7913 if (*comp_dir != NULL)
7914 {
7915 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7916 directory, get rid of it. */
7917 char *cp = strchr (*comp_dir, ':');
7918
7919 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7920 *comp_dir = cp + 1;
7921 }
7922
7923 if (*name == NULL)
7924 *name = "<unknown>";
7925}
7926
f4dc4d17
DE
7927/* Handle DW_AT_stmt_list for a compilation unit.
7928 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7929 COMP_DIR is the compilation directory.
7930 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7931
7932static void
7933handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7934 const char *comp_dir)
2ab95328
TT
7935{
7936 struct attribute *attr;
2ab95328 7937
f4dc4d17
DE
7938 gdb_assert (! cu->per_cu->is_debug_types);
7939
2ab95328
TT
7940 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7941 if (attr)
7942 {
7943 unsigned int line_offset = DW_UNSND (attr);
7944 struct line_header *line_header
3019eac3 7945 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7946
7947 if (line_header)
dee91e82
DE
7948 {
7949 cu->line_header = line_header;
7950 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7951 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7952 }
2ab95328
TT
7953 }
7954}
7955
95554aad 7956/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7957
c906108c 7958static void
e7c27a73 7959read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7960{
dee91e82 7961 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7962 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7963 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7964 CORE_ADDR highpc = ((CORE_ADDR) 0);
7965 struct attribute *attr;
15d034d0
TT
7966 const char *name = NULL;
7967 const char *comp_dir = NULL;
c906108c
SS
7968 struct die_info *child_die;
7969 bfd *abfd = objfile->obfd;
e142c38c 7970 CORE_ADDR baseaddr;
6e70227d 7971
e142c38c 7972 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7973
fae299cd 7974 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7975
7976 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7977 from finish_block. */
2acceee2 7978 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7979 lowpc = highpc;
7980 lowpc += baseaddr;
7981 highpc += baseaddr;
7982
9291a0cd 7983 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7984
95554aad 7985 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7986
f4b8a18d
KW
7987 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7988 standardised yet. As a workaround for the language detection we fall
7989 back to the DW_AT_producer string. */
7990 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7991 cu->language = language_opencl;
7992
3019eac3
DE
7993 /* Similar hack for Go. */
7994 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7995 set_cu_language (DW_LANG_Go, cu);
7996
f4dc4d17 7997 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7998
7999 /* Decode line number information if present. We do this before
8000 processing child DIEs, so that the line header table is available
8001 for DW_AT_decl_file. */
f4dc4d17 8002 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8003
8004 /* Process all dies in compilation unit. */
8005 if (die->child != NULL)
8006 {
8007 child_die = die->child;
8008 while (child_die && child_die->tag)
8009 {
8010 process_die (child_die, cu);
8011 child_die = sibling_die (child_die);
8012 }
8013 }
8014
8015 /* Decode macro information, if present. Dwarf 2 macro information
8016 refers to information in the line number info statement program
8017 header, so we can only read it if we've read the header
8018 successfully. */
8019 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8020 if (attr && cu->line_header)
8021 {
8022 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8023 complaint (&symfile_complaints,
8024 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8025
09262596 8026 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8027 }
8028 else
8029 {
8030 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8031 if (attr && cu->line_header)
8032 {
8033 unsigned int macro_offset = DW_UNSND (attr);
8034
09262596 8035 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8036 }
8037 }
8038
8039 do_cleanups (back_to);
8040}
8041
f4dc4d17
DE
8042/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8043 Create the set of symtabs used by this TU, or if this TU is sharing
8044 symtabs with another TU and the symtabs have already been created
8045 then restore those symtabs in the line header.
8046 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8047
8048static void
f4dc4d17 8049setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8050{
f4dc4d17
DE
8051 struct objfile *objfile = dwarf2_per_objfile->objfile;
8052 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8053 struct type_unit_group *tu_group;
8054 int first_time;
8055 struct line_header *lh;
3019eac3 8056 struct attribute *attr;
f4dc4d17 8057 unsigned int i, line_offset;
3019eac3 8058
f4dc4d17 8059 gdb_assert (per_cu->is_debug_types);
3019eac3 8060
f4dc4d17 8061 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8062
f4dc4d17
DE
8063 /* If we're using .gdb_index (includes -readnow) then
8064 per_cu->s.type_unit_group may not have been set up yet. */
796a7ff8
DE
8065 if (per_cu->type_unit_group == NULL)
8066 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8067 tu_group = per_cu->type_unit_group;
f4dc4d17
DE
8068
8069 /* If we've already processed this stmt_list there's no real need to
8070 do it again, we could fake it and just recreate the part we need
8071 (file name,index -> symtab mapping). If data shows this optimization
8072 is useful we can do it then. */
8073 first_time = tu_group->primary_symtab == NULL;
8074
8075 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8076 debug info. */
8077 lh = NULL;
8078 if (attr != NULL)
3019eac3 8079 {
f4dc4d17
DE
8080 line_offset = DW_UNSND (attr);
8081 lh = dwarf_decode_line_header (line_offset, cu);
8082 }
8083 if (lh == NULL)
8084 {
8085 if (first_time)
8086 dwarf2_start_symtab (cu, "", NULL, 0);
8087 else
8088 {
8089 gdb_assert (tu_group->symtabs == NULL);
8090 restart_symtab (0);
8091 }
8092 /* Note: The primary symtab will get allocated at the end. */
8093 return;
3019eac3
DE
8094 }
8095
f4dc4d17
DE
8096 cu->line_header = lh;
8097 make_cleanup (free_cu_line_header, cu);
3019eac3 8098
f4dc4d17
DE
8099 if (first_time)
8100 {
8101 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8102
f4dc4d17
DE
8103 tu_group->num_symtabs = lh->num_file_names;
8104 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8105
f4dc4d17
DE
8106 for (i = 0; i < lh->num_file_names; ++i)
8107 {
8108 char *dir = NULL;
8109 struct file_entry *fe = &lh->file_names[i];
3019eac3 8110
f4dc4d17
DE
8111 if (fe->dir_index)
8112 dir = lh->include_dirs[fe->dir_index - 1];
8113 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8114
f4dc4d17
DE
8115 /* Note: We don't have to watch for the main subfile here, type units
8116 don't have DW_AT_name. */
3019eac3 8117
f4dc4d17
DE
8118 if (current_subfile->symtab == NULL)
8119 {
8120 /* NOTE: start_subfile will recognize when it's been passed
8121 a file it has already seen. So we can't assume there's a
8122 simple mapping from lh->file_names to subfiles,
8123 lh->file_names may contain dups. */
8124 current_subfile->symtab = allocate_symtab (current_subfile->name,
8125 objfile);
8126 }
8127
8128 fe->symtab = current_subfile->symtab;
8129 tu_group->symtabs[i] = fe->symtab;
8130 }
8131 }
8132 else
3019eac3 8133 {
f4dc4d17
DE
8134 restart_symtab (0);
8135
8136 for (i = 0; i < lh->num_file_names; ++i)
8137 {
8138 struct file_entry *fe = &lh->file_names[i];
8139
8140 fe->symtab = tu_group->symtabs[i];
8141 }
3019eac3
DE
8142 }
8143
f4dc4d17
DE
8144 /* The main symtab is allocated last. Type units don't have DW_AT_name
8145 so they don't have a "real" (so to speak) symtab anyway.
8146 There is later code that will assign the main symtab to all symbols
8147 that don't have one. We need to handle the case of a symbol with a
8148 missing symtab (DW_AT_decl_file) anyway. */
8149}
3019eac3 8150
f4dc4d17
DE
8151/* Process DW_TAG_type_unit.
8152 For TUs we want to skip the first top level sibling if it's not the
8153 actual type being defined by this TU. In this case the first top
8154 level sibling is there to provide context only. */
3019eac3 8155
f4dc4d17
DE
8156static void
8157read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8158{
8159 struct die_info *child_die;
3019eac3 8160
f4dc4d17
DE
8161 prepare_one_comp_unit (cu, die, language_minimal);
8162
8163 /* Initialize (or reinitialize) the machinery for building symtabs.
8164 We do this before processing child DIEs, so that the line header table
8165 is available for DW_AT_decl_file. */
8166 setup_type_unit_groups (die, cu);
8167
8168 if (die->child != NULL)
8169 {
8170 child_die = die->child;
8171 while (child_die && child_die->tag)
8172 {
8173 process_die (child_die, cu);
8174 child_die = sibling_die (child_die);
8175 }
8176 }
3019eac3
DE
8177}
8178\f
80626a55
DE
8179/* DWO/DWP files.
8180
8181 http://gcc.gnu.org/wiki/DebugFission
8182 http://gcc.gnu.org/wiki/DebugFissionDWP
8183
8184 To simplify handling of both DWO files ("object" files with the DWARF info)
8185 and DWP files (a file with the DWOs packaged up into one file), we treat
8186 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8187
8188static hashval_t
8189hash_dwo_file (const void *item)
8190{
8191 const struct dwo_file *dwo_file = item;
8192
80626a55 8193 return htab_hash_string (dwo_file->name);
3019eac3
DE
8194}
8195
8196static int
8197eq_dwo_file (const void *item_lhs, const void *item_rhs)
8198{
8199 const struct dwo_file *lhs = item_lhs;
8200 const struct dwo_file *rhs = item_rhs;
8201
80626a55 8202 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8203}
8204
8205/* Allocate a hash table for DWO files. */
8206
8207static htab_t
8208allocate_dwo_file_hash_table (void)
8209{
8210 struct objfile *objfile = dwarf2_per_objfile->objfile;
8211
8212 return htab_create_alloc_ex (41,
8213 hash_dwo_file,
8214 eq_dwo_file,
8215 NULL,
8216 &objfile->objfile_obstack,
8217 hashtab_obstack_allocate,
8218 dummy_obstack_deallocate);
8219}
8220
80626a55
DE
8221/* Lookup DWO file DWO_NAME. */
8222
8223static void **
8224lookup_dwo_file_slot (const char *dwo_name)
8225{
8226 struct dwo_file find_entry;
8227 void **slot;
8228
8229 if (dwarf2_per_objfile->dwo_files == NULL)
8230 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8231
8232 memset (&find_entry, 0, sizeof (find_entry));
8233 find_entry.name = dwo_name;
8234 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8235
8236 return slot;
8237}
8238
3019eac3
DE
8239static hashval_t
8240hash_dwo_unit (const void *item)
8241{
8242 const struct dwo_unit *dwo_unit = item;
8243
8244 /* This drops the top 32 bits of the id, but is ok for a hash. */
8245 return dwo_unit->signature;
8246}
8247
8248static int
8249eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8250{
8251 const struct dwo_unit *lhs = item_lhs;
8252 const struct dwo_unit *rhs = item_rhs;
8253
8254 /* The signature is assumed to be unique within the DWO file.
8255 So while object file CU dwo_id's always have the value zero,
8256 that's OK, assuming each object file DWO file has only one CU,
8257 and that's the rule for now. */
8258 return lhs->signature == rhs->signature;
8259}
8260
8261/* Allocate a hash table for DWO CUs,TUs.
8262 There is one of these tables for each of CUs,TUs for each DWO file. */
8263
8264static htab_t
8265allocate_dwo_unit_table (struct objfile *objfile)
8266{
8267 /* Start out with a pretty small number.
8268 Generally DWO files contain only one CU and maybe some TUs. */
8269 return htab_create_alloc_ex (3,
8270 hash_dwo_unit,
8271 eq_dwo_unit,
8272 NULL,
8273 &objfile->objfile_obstack,
8274 hashtab_obstack_allocate,
8275 dummy_obstack_deallocate);
8276}
8277
80626a55 8278/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8279
8280struct create_dwo_info_table_data
8281{
8282 struct dwo_file *dwo_file;
8283 htab_t cu_htab;
8284};
8285
80626a55 8286/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8287
8288static void
80626a55
DE
8289create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8290 gdb_byte *info_ptr,
8291 struct die_info *comp_unit_die,
8292 int has_children,
8293 void *datap)
3019eac3
DE
8294{
8295 struct dwarf2_cu *cu = reader->cu;
8296 struct objfile *objfile = dwarf2_per_objfile->objfile;
8297 sect_offset offset = cu->per_cu->offset;
8298 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8299 struct create_dwo_info_table_data *data = datap;
8300 struct dwo_file *dwo_file = data->dwo_file;
8301 htab_t cu_htab = data->cu_htab;
8302 void **slot;
8303 struct attribute *attr;
8304 struct dwo_unit *dwo_unit;
8305
8306 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8307 if (attr == NULL)
8308 {
8309 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8310 " its dwo_id [in module %s]"),
80626a55 8311 offset.sect_off, dwo_file->name);
3019eac3
DE
8312 return;
8313 }
8314
8315 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8316 dwo_unit->dwo_file = dwo_file;
8317 dwo_unit->signature = DW_UNSND (attr);
8318 dwo_unit->info_or_types_section = section;
8319 dwo_unit->offset = offset;
8320 dwo_unit->length = cu->per_cu->length;
8321
8322 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8323 gdb_assert (slot != NULL);
8324 if (*slot != NULL)
8325 {
8326 const struct dwo_unit *dup_dwo_unit = *slot;
8327
8328 complaint (&symfile_complaints,
8329 _("debug entry at offset 0x%x is duplicate to the entry at"
8330 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8331 offset.sect_off, dup_dwo_unit->offset.sect_off,
8332 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8333 dwo_file->name);
3019eac3
DE
8334 }
8335 else
8336 *slot = dwo_unit;
8337
09406207 8338 if (dwarf2_read_debug)
3019eac3
DE
8339 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8340 offset.sect_off,
8341 phex (dwo_unit->signature,
8342 sizeof (dwo_unit->signature)));
8343}
8344
80626a55
DE
8345/* Create a hash table to map DWO IDs to their CU entry in
8346 .debug_info.dwo in DWO_FILE.
8347 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8348
8349static htab_t
80626a55 8350create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8351{
8352 struct objfile *objfile = dwarf2_per_objfile->objfile;
8353 struct dwarf2_section_info *section = &dwo_file->sections.info;
8354 bfd *abfd;
8355 htab_t cu_htab;
8356 gdb_byte *info_ptr, *end_ptr;
8357 struct create_dwo_info_table_data create_dwo_info_table_data;
8358
8359 dwarf2_read_section (objfile, section);
8360 info_ptr = section->buffer;
8361
8362 if (info_ptr == NULL)
8363 return NULL;
8364
8365 /* We can't set abfd until now because the section may be empty or
8366 not present, in which case section->asection will be NULL. */
8367 abfd = section->asection->owner;
8368
09406207 8369 if (dwarf2_read_debug)
3019eac3
DE
8370 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8371 bfd_get_filename (abfd));
8372
8373 cu_htab = allocate_dwo_unit_table (objfile);
8374
8375 create_dwo_info_table_data.dwo_file = dwo_file;
8376 create_dwo_info_table_data.cu_htab = cu_htab;
8377
8378 end_ptr = info_ptr + section->size;
8379 while (info_ptr < end_ptr)
8380 {
8381 struct dwarf2_per_cu_data per_cu;
8382
8383 memset (&per_cu, 0, sizeof (per_cu));
8384 per_cu.objfile = objfile;
8385 per_cu.is_debug_types = 0;
8386 per_cu.offset.sect_off = info_ptr - section->buffer;
8387 per_cu.info_or_types_section = section;
8388
8389 init_cutu_and_read_dies_no_follow (&per_cu,
8390 &dwo_file->sections.abbrev,
8391 dwo_file,
80626a55 8392 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8393 &create_dwo_info_table_data);
8394
8395 info_ptr += per_cu.length;
8396 }
8397
8398 return cu_htab;
8399}
8400
80626a55
DE
8401/* DWP file .debug_{cu,tu}_index section format:
8402 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8403
8404 Both index sections have the same format, and serve to map a 64-bit
8405 signature to a set of section numbers. Each section begins with a header,
8406 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8407 indexes, and a pool of 32-bit section numbers. The index sections will be
8408 aligned at 8-byte boundaries in the file.
8409
8410 The index section header contains two unsigned 32-bit values (using the
8411 byte order of the application binary):
8412
8413 N, the number of compilation units or type units in the index
8414 M, the number of slots in the hash table
8415
8416 (We assume that N and M will not exceed 2^32 - 1.)
8417
8418 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8419
8420 The hash table begins at offset 8 in the section, and consists of an array
8421 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8422 order of the application binary). Unused slots in the hash table are 0.
8423 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8424
8425 The parallel table begins immediately after the hash table
8426 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8427 array of 32-bit indexes (using the byte order of the application binary),
8428 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8429 table contains a 32-bit index into the pool of section numbers. For unused
8430 hash table slots, the corresponding entry in the parallel table will be 0.
8431
8432 Given a 64-bit compilation unit signature or a type signature S, an entry
8433 in the hash table is located as follows:
8434
8435 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8436 the low-order k bits all set to 1.
8437
8438 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8439
8440 3) If the hash table entry at index H matches the signature, use that
8441 entry. If the hash table entry at index H is unused (all zeroes),
8442 terminate the search: the signature is not present in the table.
8443
8444 4) Let H = (H + H') modulo M. Repeat at Step 3.
8445
8446 Because M > N and H' and M are relatively prime, the search is guaranteed
8447 to stop at an unused slot or find the match.
8448
8449 The pool of section numbers begins immediately following the hash table
8450 (at offset 8 + 12 * M from the beginning of the section). The pool of
8451 section numbers consists of an array of 32-bit words (using the byte order
8452 of the application binary). Each item in the array is indexed starting
8453 from 0. The hash table entry provides the index of the first section
8454 number in the set. Additional section numbers in the set follow, and the
8455 set is terminated by a 0 entry (section number 0 is not used in ELF).
8456
8457 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8458 section must be the first entry in the set, and the .debug_abbrev.dwo must
8459 be the second entry. Other members of the set may follow in any order. */
8460
8461/* Create a hash table to map DWO IDs to their CU/TU entry in
8462 .debug_{info,types}.dwo in DWP_FILE.
8463 Returns NULL if there isn't one.
8464 Note: This function processes DWP files only, not DWO files. */
8465
8466static struct dwp_hash_table *
8467create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8468{
8469 struct objfile *objfile = dwarf2_per_objfile->objfile;
8470 bfd *dbfd = dwp_file->dbfd;
8471 char *index_ptr, *index_end;
8472 struct dwarf2_section_info *index;
8473 uint32_t version, nr_units, nr_slots;
8474 struct dwp_hash_table *htab;
8475
8476 if (is_debug_types)
8477 index = &dwp_file->sections.tu_index;
8478 else
8479 index = &dwp_file->sections.cu_index;
8480
8481 if (dwarf2_section_empty_p (index))
8482 return NULL;
8483 dwarf2_read_section (objfile, index);
8484
8485 index_ptr = index->buffer;
8486 index_end = index_ptr + index->size;
8487
8488 version = read_4_bytes (dbfd, index_ptr);
8489 index_ptr += 8; /* Skip the unused word. */
8490 nr_units = read_4_bytes (dbfd, index_ptr);
8491 index_ptr += 4;
8492 nr_slots = read_4_bytes (dbfd, index_ptr);
8493 index_ptr += 4;
8494
8495 if (version != 1)
8496 {
8497 error (_("Dwarf Error: unsupported DWP file version (%u)"
8498 " [in module %s]"),
8499 version, dwp_file->name);
8500 }
8501 if (nr_slots != (nr_slots & -nr_slots))
8502 {
8503 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8504 " is not power of 2 [in module %s]"),
8505 nr_slots, dwp_file->name);
8506 }
8507
8508 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8509 htab->nr_units = nr_units;
8510 htab->nr_slots = nr_slots;
8511 htab->hash_table = index_ptr;
8512 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8513 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8514
8515 return htab;
8516}
8517
8518/* Update SECTIONS with the data from SECTP.
8519
8520 This function is like the other "locate" section routines that are
8521 passed to bfd_map_over_sections, but in this context the sections to
8522 read comes from the DWP hash table, not the full ELF section table.
8523
8524 The result is non-zero for success, or zero if an error was found. */
8525
8526static int
8527locate_virtual_dwo_sections (asection *sectp,
8528 struct virtual_dwo_sections *sections)
8529{
8530 const struct dwop_section_names *names = &dwop_section_names;
8531
8532 if (section_is_p (sectp->name, &names->abbrev_dwo))
8533 {
8534 /* There can be only one. */
8535 if (sections->abbrev.asection != NULL)
8536 return 0;
8537 sections->abbrev.asection = sectp;
8538 sections->abbrev.size = bfd_get_section_size (sectp);
8539 }
8540 else if (section_is_p (sectp->name, &names->info_dwo)
8541 || section_is_p (sectp->name, &names->types_dwo))
8542 {
8543 /* There can be only one. */
8544 if (sections->info_or_types.asection != NULL)
8545 return 0;
8546 sections->info_or_types.asection = sectp;
8547 sections->info_or_types.size = bfd_get_section_size (sectp);
8548 }
8549 else if (section_is_p (sectp->name, &names->line_dwo))
8550 {
8551 /* There can be only one. */
8552 if (sections->line.asection != NULL)
8553 return 0;
8554 sections->line.asection = sectp;
8555 sections->line.size = bfd_get_section_size (sectp);
8556 }
8557 else if (section_is_p (sectp->name, &names->loc_dwo))
8558 {
8559 /* There can be only one. */
8560 if (sections->loc.asection != NULL)
8561 return 0;
8562 sections->loc.asection = sectp;
8563 sections->loc.size = bfd_get_section_size (sectp);
8564 }
8565 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8566 {
8567 /* There can be only one. */
8568 if (sections->macinfo.asection != NULL)
8569 return 0;
8570 sections->macinfo.asection = sectp;
8571 sections->macinfo.size = bfd_get_section_size (sectp);
8572 }
8573 else if (section_is_p (sectp->name, &names->macro_dwo))
8574 {
8575 /* There can be only one. */
8576 if (sections->macro.asection != NULL)
8577 return 0;
8578 sections->macro.asection = sectp;
8579 sections->macro.size = bfd_get_section_size (sectp);
8580 }
8581 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8582 {
8583 /* There can be only one. */
8584 if (sections->str_offsets.asection != NULL)
8585 return 0;
8586 sections->str_offsets.asection = sectp;
8587 sections->str_offsets.size = bfd_get_section_size (sectp);
8588 }
8589 else
8590 {
8591 /* No other kind of section is valid. */
8592 return 0;
8593 }
8594
8595 return 1;
8596}
8597
8598/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8599 HTAB is the hash table from the DWP file.
8600 SECTION_INDEX is the index of the DWO in HTAB. */
8601
8602static struct dwo_unit *
8603create_dwo_in_dwp (struct dwp_file *dwp_file,
8604 const struct dwp_hash_table *htab,
8605 uint32_t section_index,
8606 ULONGEST signature, int is_debug_types)
8607{
8608 struct objfile *objfile = dwarf2_per_objfile->objfile;
8609 bfd *dbfd = dwp_file->dbfd;
8610 const char *kind = is_debug_types ? "TU" : "CU";
8611 struct dwo_file *dwo_file;
8612 struct dwo_unit *dwo_unit;
8613 struct virtual_dwo_sections sections;
8614 void **dwo_file_slot;
8615 char *virtual_dwo_name;
8616 struct dwarf2_section_info *cutu;
8617 struct cleanup *cleanups;
8618 int i;
8619
8620 if (dwarf2_read_debug)
8621 {
8622 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8623 kind,
8624 section_index, phex (signature, sizeof (signature)),
8625 dwp_file->name);
8626 }
8627
8628 /* Fetch the sections of this DWO.
8629 Put a limit on the number of sections we look for so that bad data
8630 doesn't cause us to loop forever. */
8631
8632#define MAX_NR_DWO_SECTIONS \
8633 (1 /* .debug_info or .debug_types */ \
8634 + 1 /* .debug_abbrev */ \
8635 + 1 /* .debug_line */ \
8636 + 1 /* .debug_loc */ \
8637 + 1 /* .debug_str_offsets */ \
8638 + 1 /* .debug_macro */ \
8639 + 1 /* .debug_macinfo */ \
8640 + 1 /* trailing zero */)
8641
8642 memset (&sections, 0, sizeof (sections));
8643 cleanups = make_cleanup (null_cleanup, 0);
8644
8645 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8646 {
8647 asection *sectp;
8648 uint32_t section_nr =
8649 read_4_bytes (dbfd,
8650 htab->section_pool
8651 + (section_index + i) * sizeof (uint32_t));
8652
8653 if (section_nr == 0)
8654 break;
8655 if (section_nr >= dwp_file->num_sections)
8656 {
8657 error (_("Dwarf Error: bad DWP hash table, section number too large"
8658 " [in module %s]"),
8659 dwp_file->name);
8660 }
8661
8662 sectp = dwp_file->elf_sections[section_nr];
8663 if (! locate_virtual_dwo_sections (sectp, &sections))
8664 {
8665 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8666 " [in module %s]"),
8667 dwp_file->name);
8668 }
8669 }
8670
8671 if (i < 2
8672 || sections.info_or_types.asection == NULL
8673 || sections.abbrev.asection == NULL)
8674 {
8675 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8676 " [in module %s]"),
8677 dwp_file->name);
8678 }
8679 if (i == MAX_NR_DWO_SECTIONS)
8680 {
8681 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8682 " [in module %s]"),
8683 dwp_file->name);
8684 }
8685
8686 /* It's easier for the rest of the code if we fake a struct dwo_file and
8687 have dwo_unit "live" in that. At least for now.
8688
8689 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8690 However, for each CU + set of TUs that came from the same original DWO
8691 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8692 (fewer struct dwo_file objects to allocated). Remember that for really
8693 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8694
2792b94d
PM
8695 virtual_dwo_name =
8696 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8697 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8698 sections.line.asection ? sections.line.asection->id : 0,
8699 sections.loc.asection ? sections.loc.asection->id : 0,
8700 (sections.str_offsets.asection
8701 ? sections.str_offsets.asection->id
8702 : 0));
80626a55
DE
8703 make_cleanup (xfree, virtual_dwo_name);
8704 /* Can we use an existing virtual DWO file? */
8705 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8706 /* Create one if necessary. */
8707 if (*dwo_file_slot == NULL)
8708 {
8709 if (dwarf2_read_debug)
8710 {
8711 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8712 virtual_dwo_name);
8713 }
8714 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8715 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8716 virtual_dwo_name,
8717 strlen (virtual_dwo_name));
8718 dwo_file->sections.abbrev = sections.abbrev;
8719 dwo_file->sections.line = sections.line;
8720 dwo_file->sections.loc = sections.loc;
8721 dwo_file->sections.macinfo = sections.macinfo;
8722 dwo_file->sections.macro = sections.macro;
8723 dwo_file->sections.str_offsets = sections.str_offsets;
8724 /* The "str" section is global to the entire DWP file. */
8725 dwo_file->sections.str = dwp_file->sections.str;
8726 /* The info or types section is assigned later to dwo_unit,
8727 there's no need to record it in dwo_file.
8728 Also, we can't simply record type sections in dwo_file because
8729 we record a pointer into the vector in dwo_unit. As we collect more
8730 types we'll grow the vector and eventually have to reallocate space
8731 for it, invalidating all the pointers into the current copy. */
8732 *dwo_file_slot = dwo_file;
8733 }
8734 else
8735 {
8736 if (dwarf2_read_debug)
8737 {
8738 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8739 virtual_dwo_name);
8740 }
8741 dwo_file = *dwo_file_slot;
8742 }
8743 do_cleanups (cleanups);
8744
8745 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8746 dwo_unit->dwo_file = dwo_file;
8747 dwo_unit->signature = signature;
8748 dwo_unit->info_or_types_section =
8749 obstack_alloc (&objfile->objfile_obstack,
8750 sizeof (struct dwarf2_section_info));
8751 *dwo_unit->info_or_types_section = sections.info_or_types;
8752 /* offset, length, type_offset_in_tu are set later. */
8753
8754 return dwo_unit;
8755}
8756
8757/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8758
8759static struct dwo_unit *
8760lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8761 const struct dwp_hash_table *htab,
8762 ULONGEST signature, int is_debug_types)
8763{
8764 bfd *dbfd = dwp_file->dbfd;
8765 uint32_t mask = htab->nr_slots - 1;
8766 uint32_t hash = signature & mask;
8767 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8768 unsigned int i;
8769 void **slot;
8770 struct dwo_unit find_dwo_cu, *dwo_cu;
8771
8772 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8773 find_dwo_cu.signature = signature;
8774 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8775
8776 if (*slot != NULL)
8777 return *slot;
8778
8779 /* Use a for loop so that we don't loop forever on bad debug info. */
8780 for (i = 0; i < htab->nr_slots; ++i)
8781 {
8782 ULONGEST signature_in_table;
8783
8784 signature_in_table =
8785 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8786 if (signature_in_table == signature)
8787 {
8788 uint32_t section_index =
8789 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8790
8791 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8792 signature, is_debug_types);
8793 return *slot;
8794 }
8795 if (signature_in_table == 0)
8796 return NULL;
8797 hash = (hash + hash2) & mask;
8798 }
8799
8800 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8801 " [in module %s]"),
8802 dwp_file->name);
8803}
8804
8805/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8806 Open the file specified by FILE_NAME and hand it off to BFD for
8807 preliminary analysis. Return a newly initialized bfd *, which
8808 includes a canonicalized copy of FILE_NAME.
80626a55 8809 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8810 In case of trouble, return NULL.
8811 NOTE: This function is derived from symfile_bfd_open. */
8812
8813static bfd *
80626a55 8814try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8815{
8816 bfd *sym_bfd;
80626a55 8817 int desc, flags;
3019eac3 8818 char *absolute_name;
3019eac3 8819
80626a55
DE
8820 flags = OPF_TRY_CWD_FIRST;
8821 if (is_dwp)
8822 flags |= OPF_SEARCH_IN_PATH;
8823 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8824 O_RDONLY | O_BINARY, &absolute_name);
8825 if (desc < 0)
8826 return NULL;
8827
bb397797 8828 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8829 if (!sym_bfd)
8830 {
3019eac3
DE
8831 xfree (absolute_name);
8832 return NULL;
8833 }
a4453b7e 8834 xfree (absolute_name);
3019eac3
DE
8835 bfd_set_cacheable (sym_bfd, 1);
8836
8837 if (!bfd_check_format (sym_bfd, bfd_object))
8838 {
cbb099e8 8839 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8840 return NULL;
8841 }
8842
3019eac3
DE
8843 return sym_bfd;
8844}
8845
80626a55 8846/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8847 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8848 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8849 The result is the bfd handle of the file.
8850 If there is a problem finding or opening the file, return NULL.
8851 Upon success, the canonicalized path of the file is stored in the bfd,
8852 same as symfile_bfd_open. */
8853
8854static bfd *
80626a55 8855open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8856{
8857 bfd *abfd;
3019eac3 8858
80626a55
DE
8859 if (IS_ABSOLUTE_PATH (file_name))
8860 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8861
8862 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8863
8864 if (comp_dir != NULL)
8865 {
80626a55 8866 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8867
8868 /* NOTE: If comp_dir is a relative path, this will also try the
8869 search path, which seems useful. */
80626a55 8870 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8871 xfree (path_to_try);
8872 if (abfd != NULL)
8873 return abfd;
8874 }
8875
8876 /* That didn't work, try debug-file-directory, which, despite its name,
8877 is a list of paths. */
8878
8879 if (*debug_file_directory == '\0')
8880 return NULL;
8881
80626a55 8882 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8883}
8884
80626a55
DE
8885/* This function is mapped across the sections and remembers the offset and
8886 size of each of the DWO debugging sections we are interested in. */
8887
8888static void
8889dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8890{
8891 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8892 const struct dwop_section_names *names = &dwop_section_names;
8893
8894 if (section_is_p (sectp->name, &names->abbrev_dwo))
8895 {
8896 dwo_sections->abbrev.asection = sectp;
8897 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8898 }
8899 else if (section_is_p (sectp->name, &names->info_dwo))
8900 {
8901 dwo_sections->info.asection = sectp;
8902 dwo_sections->info.size = bfd_get_section_size (sectp);
8903 }
8904 else if (section_is_p (sectp->name, &names->line_dwo))
8905 {
8906 dwo_sections->line.asection = sectp;
8907 dwo_sections->line.size = bfd_get_section_size (sectp);
8908 }
8909 else if (section_is_p (sectp->name, &names->loc_dwo))
8910 {
8911 dwo_sections->loc.asection = sectp;
8912 dwo_sections->loc.size = bfd_get_section_size (sectp);
8913 }
8914 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8915 {
8916 dwo_sections->macinfo.asection = sectp;
8917 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8918 }
8919 else if (section_is_p (sectp->name, &names->macro_dwo))
8920 {
8921 dwo_sections->macro.asection = sectp;
8922 dwo_sections->macro.size = bfd_get_section_size (sectp);
8923 }
8924 else if (section_is_p (sectp->name, &names->str_dwo))
8925 {
8926 dwo_sections->str.asection = sectp;
8927 dwo_sections->str.size = bfd_get_section_size (sectp);
8928 }
8929 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8930 {
8931 dwo_sections->str_offsets.asection = sectp;
8932 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8933 }
8934 else if (section_is_p (sectp->name, &names->types_dwo))
8935 {
8936 struct dwarf2_section_info type_section;
8937
8938 memset (&type_section, 0, sizeof (type_section));
8939 type_section.asection = sectp;
8940 type_section.size = bfd_get_section_size (sectp);
8941 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8942 &type_section);
8943 }
8944}
8945
8946/* Initialize the use of the DWO file specified by DWO_NAME.
8947 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8948
8949static struct dwo_file *
80626a55 8950open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8951{
8952 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8953 struct dwo_file *dwo_file;
8954 bfd *dbfd;
3019eac3
DE
8955 struct cleanup *cleanups;
8956
80626a55
DE
8957 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8958 if (dbfd == NULL)
8959 {
8960 if (dwarf2_read_debug)
8961 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8962 return NULL;
8963 }
8964 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8965 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8966 dwo_name, strlen (dwo_name));
8967 dwo_file->dbfd = dbfd;
3019eac3
DE
8968
8969 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8970
80626a55 8971 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8972
80626a55 8973 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8974
8975 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8976 dwo_file->sections.types);
8977
8978 discard_cleanups (cleanups);
8979
80626a55
DE
8980 if (dwarf2_read_debug)
8981 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8982
3019eac3
DE
8983 return dwo_file;
8984}
8985
80626a55
DE
8986/* This function is mapped across the sections and remembers the offset and
8987 size of each of the DWP debugging sections we are interested in. */
3019eac3 8988
80626a55
DE
8989static void
8990dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8991{
80626a55
DE
8992 struct dwp_file *dwp_file = dwp_file_ptr;
8993 const struct dwop_section_names *names = &dwop_section_names;
8994 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8995
80626a55
DE
8996 /* Record the ELF section number for later lookup: this is what the
8997 .debug_cu_index,.debug_tu_index tables use. */
8998 gdb_assert (elf_section_nr < dwp_file->num_sections);
8999 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9000
80626a55
DE
9001 /* Look for specific sections that we need. */
9002 if (section_is_p (sectp->name, &names->str_dwo))
9003 {
9004 dwp_file->sections.str.asection = sectp;
9005 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9006 }
9007 else if (section_is_p (sectp->name, &names->cu_index))
9008 {
9009 dwp_file->sections.cu_index.asection = sectp;
9010 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9011 }
9012 else if (section_is_p (sectp->name, &names->tu_index))
9013 {
9014 dwp_file->sections.tu_index.asection = sectp;
9015 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9016 }
9017}
3019eac3 9018
80626a55 9019/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9020
80626a55
DE
9021static hashval_t
9022hash_dwp_loaded_cutus (const void *item)
9023{
9024 const struct dwo_unit *dwo_unit = item;
3019eac3 9025
80626a55
DE
9026 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9027 return dwo_unit->signature;
3019eac3
DE
9028}
9029
80626a55 9030/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9031
80626a55
DE
9032static int
9033eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9034{
80626a55
DE
9035 const struct dwo_unit *dua = a;
9036 const struct dwo_unit *dub = b;
3019eac3 9037
80626a55
DE
9038 return dua->signature == dub->signature;
9039}
3019eac3 9040
80626a55 9041/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9042
80626a55
DE
9043static htab_t
9044allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9045{
9046 return htab_create_alloc_ex (3,
9047 hash_dwp_loaded_cutus,
9048 eq_dwp_loaded_cutus,
9049 NULL,
9050 &objfile->objfile_obstack,
9051 hashtab_obstack_allocate,
9052 dummy_obstack_deallocate);
9053}
3019eac3 9054
80626a55
DE
9055/* Initialize the use of the DWP file for the current objfile.
9056 By convention the name of the DWP file is ${objfile}.dwp.
9057 The result is NULL if it can't be found. */
a766d390 9058
80626a55
DE
9059static struct dwp_file *
9060open_and_init_dwp_file (const char *comp_dir)
9061{
9062 struct objfile *objfile = dwarf2_per_objfile->objfile;
9063 struct dwp_file *dwp_file;
9064 char *dwp_name;
9065 bfd *dbfd;
9066 struct cleanup *cleanups;
9067
2792b94d 9068 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9069 cleanups = make_cleanup (xfree, dwp_name);
9070
9071 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9072 if (dbfd == NULL)
9073 {
9074 if (dwarf2_read_debug)
9075 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9076 do_cleanups (cleanups);
9077 return NULL;
3019eac3 9078 }
80626a55
DE
9079 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9080 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9081 dwp_name, strlen (dwp_name));
9082 dwp_file->dbfd = dbfd;
9083 do_cleanups (cleanups);
c906108c 9084
80626a55 9085 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9086
80626a55
DE
9087 /* +1: section 0 is unused */
9088 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9089 dwp_file->elf_sections =
9090 OBSTACK_CALLOC (&objfile->objfile_obstack,
9091 dwp_file->num_sections, asection *);
9092
9093 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9094
9095 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9096
9097 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9098
9099 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9100
9101 discard_cleanups (cleanups);
9102
9103 if (dwarf2_read_debug)
9104 {
9105 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9106 fprintf_unfiltered (gdb_stdlog,
9107 " %u CUs, %u TUs\n",
9108 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9109 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9110 }
9111
9112 return dwp_file;
3019eac3 9113}
c906108c 9114
80626a55
DE
9115/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9116 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9117 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9118 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9119 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9120
9121 This is called, for example, when wanting to read a variable with a
9122 complex location. Therefore we don't want to do file i/o for every call.
9123 Therefore we don't want to look for a DWO file on every call.
9124 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9125 then we check if we've already seen DWO_NAME, and only THEN do we check
9126 for a DWO file.
9127
1c658ad5 9128 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9129 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9130
3019eac3 9131static struct dwo_unit *
80626a55
DE
9132lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9133 const char *dwo_name, const char *comp_dir,
9134 ULONGEST signature, int is_debug_types)
3019eac3
DE
9135{
9136 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9137 const char *kind = is_debug_types ? "TU" : "CU";
9138 void **dwo_file_slot;
3019eac3 9139 struct dwo_file *dwo_file;
80626a55 9140 struct dwp_file *dwp_file;
cb1df416 9141
80626a55 9142 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9143
80626a55
DE
9144 if (! dwarf2_per_objfile->dwp_checked)
9145 {
9146 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9147 dwarf2_per_objfile->dwp_checked = 1;
9148 }
9149 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9150
80626a55 9151 if (dwp_file != NULL)
cf2c3c16 9152 {
80626a55
DE
9153 const struct dwp_hash_table *dwp_htab =
9154 is_debug_types ? dwp_file->tus : dwp_file->cus;
9155
9156 if (dwp_htab != NULL)
9157 {
9158 struct dwo_unit *dwo_cutu =
9159 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9160
9161 if (dwo_cutu != NULL)
9162 {
9163 if (dwarf2_read_debug)
9164 {
9165 fprintf_unfiltered (gdb_stdlog,
9166 "Virtual DWO %s %s found: @%s\n",
9167 kind, hex_string (signature),
9168 host_address_to_string (dwo_cutu));
9169 }
9170 return dwo_cutu;
9171 }
9172 }
9173 }
9174
9175 /* Have we already seen DWO_NAME? */
9176
9177 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9178 if (*dwo_file_slot == NULL)
9179 {
9180 /* Read in the file and build a table of the DWOs it contains. */
9181 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9182 }
9183 /* NOTE: This will be NULL if unable to open the file. */
9184 dwo_file = *dwo_file_slot;
9185
9186 if (dwo_file != NULL)
9187 {
9188 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9189
9190 if (htab != NULL)
9191 {
9192 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9193
80626a55
DE
9194 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9195 find_dwo_cutu.signature = signature;
9196 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9197
80626a55
DE
9198 if (dwo_cutu != NULL)
9199 {
9200 if (dwarf2_read_debug)
9201 {
9202 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9203 kind, dwo_name, hex_string (signature),
9204 host_address_to_string (dwo_cutu));
9205 }
9206 return dwo_cutu;
9207 }
9208 }
2e276125 9209 }
9cdd5dbd 9210
80626a55
DE
9211 /* We didn't find it. This could mean a dwo_id mismatch, or
9212 someone deleted the DWO/DWP file, or the search path isn't set up
9213 correctly to find the file. */
9214
9215 if (dwarf2_read_debug)
9216 {
9217 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9218 kind, dwo_name, hex_string (signature));
9219 }
3019eac3
DE
9220
9221 complaint (&symfile_complaints,
80626a55 9222 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9223 " [in module %s]"),
80626a55 9224 this_unit->offset.sect_off, objfile->name);
3019eac3 9225 return NULL;
5fb290d7
DJ
9226}
9227
80626a55
DE
9228/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9229 See lookup_dwo_cutu_unit for details. */
9230
9231static struct dwo_unit *
9232lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9233 const char *dwo_name, const char *comp_dir,
9234 ULONGEST signature)
9235{
9236 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9237}
9238
9239/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9240 See lookup_dwo_cutu_unit for details. */
9241
9242static struct dwo_unit *
9243lookup_dwo_type_unit (struct signatured_type *this_tu,
9244 const char *dwo_name, const char *comp_dir)
9245{
9246 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9247}
9248
3019eac3
DE
9249/* Free all resources associated with DWO_FILE.
9250 Close the DWO file and munmap the sections.
9251 All memory should be on the objfile obstack. */
348e048f
DE
9252
9253static void
3019eac3 9254free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9255{
3019eac3
DE
9256 int ix;
9257 struct dwarf2_section_info *section;
348e048f 9258
80626a55 9259 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9260
3019eac3
DE
9261 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9262}
348e048f 9263
3019eac3 9264/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9265
3019eac3
DE
9266static void
9267free_dwo_file_cleanup (void *arg)
9268{
9269 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9270 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9271
3019eac3
DE
9272 free_dwo_file (dwo_file, objfile);
9273}
348e048f 9274
3019eac3 9275/* Traversal function for free_dwo_files. */
2ab95328 9276
3019eac3
DE
9277static int
9278free_dwo_file_from_slot (void **slot, void *info)
9279{
9280 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9281 struct objfile *objfile = (struct objfile *) info;
348e048f 9282
3019eac3 9283 free_dwo_file (dwo_file, objfile);
348e048f 9284
3019eac3
DE
9285 return 1;
9286}
348e048f 9287
3019eac3 9288/* Free all resources associated with DWO_FILES. */
348e048f 9289
3019eac3
DE
9290static void
9291free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9292{
9293 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9294}
3019eac3
DE
9295\f
9296/* Read in various DIEs. */
348e048f 9297
d389af10
JK
9298/* qsort helper for inherit_abstract_dies. */
9299
9300static int
9301unsigned_int_compar (const void *ap, const void *bp)
9302{
9303 unsigned int a = *(unsigned int *) ap;
9304 unsigned int b = *(unsigned int *) bp;
9305
9306 return (a > b) - (b > a);
9307}
9308
9309/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9310 Inherit only the children of the DW_AT_abstract_origin DIE not being
9311 already referenced by DW_AT_abstract_origin from the children of the
9312 current DIE. */
d389af10
JK
9313
9314static void
9315inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9316{
9317 struct die_info *child_die;
9318 unsigned die_children_count;
9319 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9320 sect_offset *offsets;
9321 sect_offset *offsets_end, *offsetp;
d389af10
JK
9322 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9323 struct die_info *origin_die;
9324 /* Iterator of the ORIGIN_DIE children. */
9325 struct die_info *origin_child_die;
9326 struct cleanup *cleanups;
9327 struct attribute *attr;
cd02d79d
PA
9328 struct dwarf2_cu *origin_cu;
9329 struct pending **origin_previous_list_in_scope;
d389af10
JK
9330
9331 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9332 if (!attr)
9333 return;
9334
cd02d79d
PA
9335 /* Note that following die references may follow to a die in a
9336 different cu. */
9337
9338 origin_cu = cu;
9339 origin_die = follow_die_ref (die, attr, &origin_cu);
9340
9341 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9342 symbols in. */
9343 origin_previous_list_in_scope = origin_cu->list_in_scope;
9344 origin_cu->list_in_scope = cu->list_in_scope;
9345
edb3359d
DJ
9346 if (die->tag != origin_die->tag
9347 && !(die->tag == DW_TAG_inlined_subroutine
9348 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9349 complaint (&symfile_complaints,
9350 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9351 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9352
9353 child_die = die->child;
9354 die_children_count = 0;
9355 while (child_die && child_die->tag)
9356 {
9357 child_die = sibling_die (child_die);
9358 die_children_count++;
9359 }
9360 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9361 cleanups = make_cleanup (xfree, offsets);
9362
9363 offsets_end = offsets;
9364 child_die = die->child;
9365 while (child_die && child_die->tag)
9366 {
c38f313d
DJ
9367 /* For each CHILD_DIE, find the corresponding child of
9368 ORIGIN_DIE. If there is more than one layer of
9369 DW_AT_abstract_origin, follow them all; there shouldn't be,
9370 but GCC versions at least through 4.4 generate this (GCC PR
9371 40573). */
9372 struct die_info *child_origin_die = child_die;
cd02d79d 9373 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9374
c38f313d
DJ
9375 while (1)
9376 {
cd02d79d
PA
9377 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9378 child_origin_cu);
c38f313d
DJ
9379 if (attr == NULL)
9380 break;
cd02d79d
PA
9381 child_origin_die = follow_die_ref (child_origin_die, attr,
9382 &child_origin_cu);
c38f313d
DJ
9383 }
9384
d389af10
JK
9385 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9386 counterpart may exist. */
c38f313d 9387 if (child_origin_die != child_die)
d389af10 9388 {
edb3359d
DJ
9389 if (child_die->tag != child_origin_die->tag
9390 && !(child_die->tag == DW_TAG_inlined_subroutine
9391 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9392 complaint (&symfile_complaints,
9393 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9394 "different tags"), child_die->offset.sect_off,
9395 child_origin_die->offset.sect_off);
c38f313d
DJ
9396 if (child_origin_die->parent != origin_die)
9397 complaint (&symfile_complaints,
9398 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9399 "different parents"), child_die->offset.sect_off,
9400 child_origin_die->offset.sect_off);
c38f313d
DJ
9401 else
9402 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9403 }
9404 child_die = sibling_die (child_die);
9405 }
9406 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9407 unsigned_int_compar);
9408 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9409 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9410 complaint (&symfile_complaints,
9411 _("Multiple children of DIE 0x%x refer "
9412 "to DIE 0x%x as their abstract origin"),
b64f50a1 9413 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9414
9415 offsetp = offsets;
9416 origin_child_die = origin_die->child;
9417 while (origin_child_die && origin_child_die->tag)
9418 {
9419 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9420 while (offsetp < offsets_end
9421 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9422 offsetp++;
b64f50a1
JK
9423 if (offsetp >= offsets_end
9424 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9425 {
9426 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9427 process_die (origin_child_die, origin_cu);
d389af10
JK
9428 }
9429 origin_child_die = sibling_die (origin_child_die);
9430 }
cd02d79d 9431 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9432
9433 do_cleanups (cleanups);
9434}
9435
c906108c 9436static void
e7c27a73 9437read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9438{
e7c27a73 9439 struct objfile *objfile = cu->objfile;
52f0bd74 9440 struct context_stack *new;
c906108c
SS
9441 CORE_ADDR lowpc;
9442 CORE_ADDR highpc;
9443 struct die_info *child_die;
edb3359d 9444 struct attribute *attr, *call_line, *call_file;
15d034d0 9445 const char *name;
e142c38c 9446 CORE_ADDR baseaddr;
801e3a5b 9447 struct block *block;
edb3359d 9448 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9449 VEC (symbolp) *template_args = NULL;
9450 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9451
9452 if (inlined_func)
9453 {
9454 /* If we do not have call site information, we can't show the
9455 caller of this inlined function. That's too confusing, so
9456 only use the scope for local variables. */
9457 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9458 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9459 if (call_line == NULL || call_file == NULL)
9460 {
9461 read_lexical_block_scope (die, cu);
9462 return;
9463 }
9464 }
c906108c 9465
e142c38c
DJ
9466 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9467
94af9270 9468 name = dwarf2_name (die, cu);
c906108c 9469
e8d05480
JB
9470 /* Ignore functions with missing or empty names. These are actually
9471 illegal according to the DWARF standard. */
9472 if (name == NULL)
9473 {
9474 complaint (&symfile_complaints,
b64f50a1
JK
9475 _("missing name for subprogram DIE at %d"),
9476 die->offset.sect_off);
e8d05480
JB
9477 return;
9478 }
9479
9480 /* Ignore functions with missing or invalid low and high pc attributes. */
9481 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9482 {
ae4d0c03
PM
9483 attr = dwarf2_attr (die, DW_AT_external, cu);
9484 if (!attr || !DW_UNSND (attr))
9485 complaint (&symfile_complaints,
3e43a32a
MS
9486 _("cannot get low and high bounds "
9487 "for subprogram DIE at %d"),
b64f50a1 9488 die->offset.sect_off);
e8d05480
JB
9489 return;
9490 }
c906108c
SS
9491
9492 lowpc += baseaddr;
9493 highpc += baseaddr;
9494
34eaf542
TT
9495 /* If we have any template arguments, then we must allocate a
9496 different sort of symbol. */
9497 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9498 {
9499 if (child_die->tag == DW_TAG_template_type_param
9500 || child_die->tag == DW_TAG_template_value_param)
9501 {
9502 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9503 struct template_symbol);
9504 templ_func->base.is_cplus_template_function = 1;
9505 break;
9506 }
9507 }
9508
c906108c 9509 new = push_context (0, lowpc);
34eaf542
TT
9510 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9511 (struct symbol *) templ_func);
4c2df51b 9512
4cecd739
DJ
9513 /* If there is a location expression for DW_AT_frame_base, record
9514 it. */
e142c38c 9515 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9516 if (attr)
c034e007
AC
9517 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9518 expression is being recorded directly in the function's symbol
9519 and not in a separate frame-base object. I guess this hack is
9520 to avoid adding some sort of frame-base adjunct/annex to the
9521 function's symbol :-(. The problem with doing this is that it
9522 results in a function symbol with a location expression that
9523 has nothing to do with the location of the function, ouch! The
9524 relationship should be: a function's symbol has-a frame base; a
9525 frame-base has-a location expression. */
e7c27a73 9526 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9527
e142c38c 9528 cu->list_in_scope = &local_symbols;
c906108c 9529
639d11d3 9530 if (die->child != NULL)
c906108c 9531 {
639d11d3 9532 child_die = die->child;
c906108c
SS
9533 while (child_die && child_die->tag)
9534 {
34eaf542
TT
9535 if (child_die->tag == DW_TAG_template_type_param
9536 || child_die->tag == DW_TAG_template_value_param)
9537 {
9538 struct symbol *arg = new_symbol (child_die, NULL, cu);
9539
f1078f66
DJ
9540 if (arg != NULL)
9541 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9542 }
9543 else
9544 process_die (child_die, cu);
c906108c
SS
9545 child_die = sibling_die (child_die);
9546 }
9547 }
9548
d389af10
JK
9549 inherit_abstract_dies (die, cu);
9550
4a811a97
UW
9551 /* If we have a DW_AT_specification, we might need to import using
9552 directives from the context of the specification DIE. See the
9553 comment in determine_prefix. */
9554 if (cu->language == language_cplus
9555 && dwarf2_attr (die, DW_AT_specification, cu))
9556 {
9557 struct dwarf2_cu *spec_cu = cu;
9558 struct die_info *spec_die = die_specification (die, &spec_cu);
9559
9560 while (spec_die)
9561 {
9562 child_die = spec_die->child;
9563 while (child_die && child_die->tag)
9564 {
9565 if (child_die->tag == DW_TAG_imported_module)
9566 process_die (child_die, spec_cu);
9567 child_die = sibling_die (child_die);
9568 }
9569
9570 /* In some cases, GCC generates specification DIEs that
9571 themselves contain DW_AT_specification attributes. */
9572 spec_die = die_specification (spec_die, &spec_cu);
9573 }
9574 }
9575
c906108c
SS
9576 new = pop_context ();
9577 /* Make a block for the local symbols within. */
801e3a5b
JB
9578 block = finish_block (new->name, &local_symbols, new->old_blocks,
9579 lowpc, highpc, objfile);
9580
df8a16a1 9581 /* For C++, set the block's scope. */
195a3f6c 9582 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9583 && cu->processing_has_namespace_info)
195a3f6c
TT
9584 block_set_scope (block, determine_prefix (die, cu),
9585 &objfile->objfile_obstack);
df8a16a1 9586
801e3a5b
JB
9587 /* If we have address ranges, record them. */
9588 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9589
34eaf542
TT
9590 /* Attach template arguments to function. */
9591 if (! VEC_empty (symbolp, template_args))
9592 {
9593 gdb_assert (templ_func != NULL);
9594
9595 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9596 templ_func->template_arguments
9597 = obstack_alloc (&objfile->objfile_obstack,
9598 (templ_func->n_template_arguments
9599 * sizeof (struct symbol *)));
9600 memcpy (templ_func->template_arguments,
9601 VEC_address (symbolp, template_args),
9602 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9603 VEC_free (symbolp, template_args);
9604 }
9605
208d8187
JB
9606 /* In C++, we can have functions nested inside functions (e.g., when
9607 a function declares a class that has methods). This means that
9608 when we finish processing a function scope, we may need to go
9609 back to building a containing block's symbol lists. */
9610 local_symbols = new->locals;
27aa8d6a 9611 using_directives = new->using_directives;
208d8187 9612
921e78cf
JB
9613 /* If we've finished processing a top-level function, subsequent
9614 symbols go in the file symbol list. */
9615 if (outermost_context_p ())
e142c38c 9616 cu->list_in_scope = &file_symbols;
c906108c
SS
9617}
9618
9619/* Process all the DIES contained within a lexical block scope. Start
9620 a new scope, process the dies, and then close the scope. */
9621
9622static void
e7c27a73 9623read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9624{
e7c27a73 9625 struct objfile *objfile = cu->objfile;
52f0bd74 9626 struct context_stack *new;
c906108c
SS
9627 CORE_ADDR lowpc, highpc;
9628 struct die_info *child_die;
e142c38c
DJ
9629 CORE_ADDR baseaddr;
9630
9631 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9632
9633 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9634 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9635 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9636 be nasty. Might be easier to properly extend generic blocks to
af34e669 9637 describe ranges. */
d85a05f0 9638 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9639 return;
9640 lowpc += baseaddr;
9641 highpc += baseaddr;
9642
9643 push_context (0, lowpc);
639d11d3 9644 if (die->child != NULL)
c906108c 9645 {
639d11d3 9646 child_die = die->child;
c906108c
SS
9647 while (child_die && child_die->tag)
9648 {
e7c27a73 9649 process_die (child_die, cu);
c906108c
SS
9650 child_die = sibling_die (child_die);
9651 }
9652 }
9653 new = pop_context ();
9654
8540c487 9655 if (local_symbols != NULL || using_directives != NULL)
c906108c 9656 {
801e3a5b
JB
9657 struct block *block
9658 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9659 highpc, objfile);
9660
9661 /* Note that recording ranges after traversing children, as we
9662 do here, means that recording a parent's ranges entails
9663 walking across all its children's ranges as they appear in
9664 the address map, which is quadratic behavior.
9665
9666 It would be nicer to record the parent's ranges before
9667 traversing its children, simply overriding whatever you find
9668 there. But since we don't even decide whether to create a
9669 block until after we've traversed its children, that's hard
9670 to do. */
9671 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9672 }
9673 local_symbols = new->locals;
27aa8d6a 9674 using_directives = new->using_directives;
c906108c
SS
9675}
9676
96408a79
SA
9677/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9678
9679static void
9680read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9681{
9682 struct objfile *objfile = cu->objfile;
9683 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9684 CORE_ADDR pc, baseaddr;
9685 struct attribute *attr;
9686 struct call_site *call_site, call_site_local;
9687 void **slot;
9688 int nparams;
9689 struct die_info *child_die;
9690
9691 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9692
9693 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9694 if (!attr)
9695 {
9696 complaint (&symfile_complaints,
9697 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9698 "DIE 0x%x [in module %s]"),
b64f50a1 9699 die->offset.sect_off, objfile->name);
96408a79
SA
9700 return;
9701 }
9702 pc = DW_ADDR (attr) + baseaddr;
9703
9704 if (cu->call_site_htab == NULL)
9705 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9706 NULL, &objfile->objfile_obstack,
9707 hashtab_obstack_allocate, NULL);
9708 call_site_local.pc = pc;
9709 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9710 if (*slot != NULL)
9711 {
9712 complaint (&symfile_complaints,
9713 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9714 "DIE 0x%x [in module %s]"),
b64f50a1 9715 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9716 return;
9717 }
9718
9719 /* Count parameters at the caller. */
9720
9721 nparams = 0;
9722 for (child_die = die->child; child_die && child_die->tag;
9723 child_die = sibling_die (child_die))
9724 {
9725 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9726 {
9727 complaint (&symfile_complaints,
9728 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9729 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9730 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9731 continue;
9732 }
9733
9734 nparams++;
9735 }
9736
9737 call_site = obstack_alloc (&objfile->objfile_obstack,
9738 (sizeof (*call_site)
9739 + (sizeof (*call_site->parameter)
9740 * (nparams - 1))));
9741 *slot = call_site;
9742 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9743 call_site->pc = pc;
9744
9745 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9746 {
9747 struct die_info *func_die;
9748
9749 /* Skip also over DW_TAG_inlined_subroutine. */
9750 for (func_die = die->parent;
9751 func_die && func_die->tag != DW_TAG_subprogram
9752 && func_die->tag != DW_TAG_subroutine_type;
9753 func_die = func_die->parent);
9754
9755 /* DW_AT_GNU_all_call_sites is a superset
9756 of DW_AT_GNU_all_tail_call_sites. */
9757 if (func_die
9758 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9759 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9760 {
9761 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9762 not complete. But keep CALL_SITE for look ups via call_site_htab,
9763 both the initial caller containing the real return address PC and
9764 the final callee containing the current PC of a chain of tail
9765 calls do not need to have the tail call list complete. But any
9766 function candidate for a virtual tail call frame searched via
9767 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9768 determined unambiguously. */
9769 }
9770 else
9771 {
9772 struct type *func_type = NULL;
9773
9774 if (func_die)
9775 func_type = get_die_type (func_die, cu);
9776 if (func_type != NULL)
9777 {
9778 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9779
9780 /* Enlist this call site to the function. */
9781 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9782 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9783 }
9784 else
9785 complaint (&symfile_complaints,
9786 _("Cannot find function owning DW_TAG_GNU_call_site "
9787 "DIE 0x%x [in module %s]"),
b64f50a1 9788 die->offset.sect_off, objfile->name);
96408a79
SA
9789 }
9790 }
9791
9792 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9793 if (attr == NULL)
9794 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9795 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9796 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9797 /* Keep NULL DWARF_BLOCK. */;
9798 else if (attr_form_is_block (attr))
9799 {
9800 struct dwarf2_locexpr_baton *dlbaton;
9801
9802 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9803 dlbaton->data = DW_BLOCK (attr)->data;
9804 dlbaton->size = DW_BLOCK (attr)->size;
9805 dlbaton->per_cu = cu->per_cu;
9806
9807 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9808 }
9809 else if (is_ref_attr (attr))
9810 {
96408a79
SA
9811 struct dwarf2_cu *target_cu = cu;
9812 struct die_info *target_die;
9813
9814 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9815 gdb_assert (target_cu->objfile == objfile);
9816 if (die_is_declaration (target_die, target_cu))
9817 {
9112db09
JK
9818 const char *target_physname = NULL;
9819 struct attribute *target_attr;
9820
9821 /* Prefer the mangled name; otherwise compute the demangled one. */
9822 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9823 if (target_attr == NULL)
9824 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9825 target_cu);
9826 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9827 target_physname = DW_STRING (target_attr);
9828 else
9829 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9830 if (target_physname == NULL)
9831 complaint (&symfile_complaints,
9832 _("DW_AT_GNU_call_site_target target DIE has invalid "
9833 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9834 die->offset.sect_off, objfile->name);
96408a79 9835 else
7d455152 9836 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9837 }
9838 else
9839 {
9840 CORE_ADDR lowpc;
9841
9842 /* DW_AT_entry_pc should be preferred. */
9843 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9844 complaint (&symfile_complaints,
9845 _("DW_AT_GNU_call_site_target target DIE has invalid "
9846 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9847 die->offset.sect_off, objfile->name);
96408a79
SA
9848 else
9849 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9850 }
9851 }
9852 else
9853 complaint (&symfile_complaints,
9854 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9855 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9856 die->offset.sect_off, objfile->name);
96408a79
SA
9857
9858 call_site->per_cu = cu->per_cu;
9859
9860 for (child_die = die->child;
9861 child_die && child_die->tag;
9862 child_die = sibling_die (child_die))
9863 {
96408a79 9864 struct call_site_parameter *parameter;
1788b2d3 9865 struct attribute *loc, *origin;
96408a79
SA
9866
9867 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9868 {
9869 /* Already printed the complaint above. */
9870 continue;
9871 }
9872
9873 gdb_assert (call_site->parameter_count < nparams);
9874 parameter = &call_site->parameter[call_site->parameter_count];
9875
1788b2d3
JK
9876 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9877 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9878 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9879
24c5c679 9880 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9881 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9882 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9883 {
9884 sect_offset offset;
9885
9886 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9887 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9888 if (!offset_in_cu_p (&cu->header, offset))
9889 {
9890 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9891 binding can be done only inside one CU. Such referenced DIE
9892 therefore cannot be even moved to DW_TAG_partial_unit. */
9893 complaint (&symfile_complaints,
9894 _("DW_AT_abstract_origin offset is not in CU for "
9895 "DW_TAG_GNU_call_site child DIE 0x%x "
9896 "[in module %s]"),
9897 child_die->offset.sect_off, objfile->name);
9898 continue;
9899 }
1788b2d3
JK
9900 parameter->u.param_offset.cu_off = (offset.sect_off
9901 - cu->header.offset.sect_off);
9902 }
9903 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9904 {
9905 complaint (&symfile_complaints,
9906 _("No DW_FORM_block* DW_AT_location for "
9907 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9908 child_die->offset.sect_off, objfile->name);
96408a79
SA
9909 continue;
9910 }
24c5c679 9911 else
96408a79 9912 {
24c5c679
JK
9913 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9914 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9915 if (parameter->u.dwarf_reg != -1)
9916 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9917 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9918 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9919 &parameter->u.fb_offset))
9920 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9921 else
9922 {
9923 complaint (&symfile_complaints,
9924 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9925 "for DW_FORM_block* DW_AT_location is supported for "
9926 "DW_TAG_GNU_call_site child DIE 0x%x "
9927 "[in module %s]"),
9928 child_die->offset.sect_off, objfile->name);
9929 continue;
9930 }
96408a79
SA
9931 }
9932
9933 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9934 if (!attr_form_is_block (attr))
9935 {
9936 complaint (&symfile_complaints,
9937 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9938 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9939 child_die->offset.sect_off, objfile->name);
96408a79
SA
9940 continue;
9941 }
9942 parameter->value = DW_BLOCK (attr)->data;
9943 parameter->value_size = DW_BLOCK (attr)->size;
9944
9945 /* Parameters are not pre-cleared by memset above. */
9946 parameter->data_value = NULL;
9947 parameter->data_value_size = 0;
9948 call_site->parameter_count++;
9949
9950 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9951 if (attr)
9952 {
9953 if (!attr_form_is_block (attr))
9954 complaint (&symfile_complaints,
9955 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9956 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9957 child_die->offset.sect_off, objfile->name);
96408a79
SA
9958 else
9959 {
9960 parameter->data_value = DW_BLOCK (attr)->data;
9961 parameter->data_value_size = DW_BLOCK (attr)->size;
9962 }
9963 }
9964 }
9965}
9966
43039443 9967/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9968 Return 1 if the attributes are present and valid, otherwise, return 0.
9969 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9970
9971static int
9972dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9973 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9974 struct partial_symtab *ranges_pst)
43039443
JK
9975{
9976 struct objfile *objfile = cu->objfile;
9977 struct comp_unit_head *cu_header = &cu->header;
9978 bfd *obfd = objfile->obfd;
9979 unsigned int addr_size = cu_header->addr_size;
9980 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9981 /* Base address selection entry. */
9982 CORE_ADDR base;
9983 int found_base;
9984 unsigned int dummy;
9985 gdb_byte *buffer;
9986 CORE_ADDR marker;
9987 int low_set;
9988 CORE_ADDR low = 0;
9989 CORE_ADDR high = 0;
ff013f42 9990 CORE_ADDR baseaddr;
43039443 9991
d00adf39
DE
9992 found_base = cu->base_known;
9993 base = cu->base_address;
43039443 9994
be391dca 9995 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9996 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9997 {
9998 complaint (&symfile_complaints,
9999 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10000 offset);
10001 return 0;
10002 }
dce234bc 10003 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10004
10005 /* Read in the largest possible address. */
10006 marker = read_address (obfd, buffer, cu, &dummy);
10007 if ((marker & mask) == mask)
10008 {
10009 /* If we found the largest possible address, then
10010 read the base address. */
10011 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10012 buffer += 2 * addr_size;
10013 offset += 2 * addr_size;
10014 found_base = 1;
10015 }
10016
10017 low_set = 0;
10018
e7030f15 10019 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10020
43039443
JK
10021 while (1)
10022 {
10023 CORE_ADDR range_beginning, range_end;
10024
10025 range_beginning = read_address (obfd, buffer, cu, &dummy);
10026 buffer += addr_size;
10027 range_end = read_address (obfd, buffer, cu, &dummy);
10028 buffer += addr_size;
10029 offset += 2 * addr_size;
10030
10031 /* An end of list marker is a pair of zero addresses. */
10032 if (range_beginning == 0 && range_end == 0)
10033 /* Found the end of list entry. */
10034 break;
10035
10036 /* Each base address selection entry is a pair of 2 values.
10037 The first is the largest possible address, the second is
10038 the base address. Check for a base address here. */
10039 if ((range_beginning & mask) == mask)
10040 {
10041 /* If we found the largest possible address, then
10042 read the base address. */
10043 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10044 found_base = 1;
10045 continue;
10046 }
10047
10048 if (!found_base)
10049 {
10050 /* We have no valid base address for the ranges
10051 data. */
10052 complaint (&symfile_complaints,
10053 _("Invalid .debug_ranges data (no base address)"));
10054 return 0;
10055 }
10056
9277c30c
UW
10057 if (range_beginning > range_end)
10058 {
10059 /* Inverted range entries are invalid. */
10060 complaint (&symfile_complaints,
10061 _("Invalid .debug_ranges data (inverted range)"));
10062 return 0;
10063 }
10064
10065 /* Empty range entries have no effect. */
10066 if (range_beginning == range_end)
10067 continue;
10068
43039443
JK
10069 range_beginning += base;
10070 range_end += base;
10071
01093045
DE
10072 /* A not-uncommon case of bad debug info.
10073 Don't pollute the addrmap with bad data. */
10074 if (range_beginning + baseaddr == 0
10075 && !dwarf2_per_objfile->has_section_at_zero)
10076 {
10077 complaint (&symfile_complaints,
10078 _(".debug_ranges entry has start address of zero"
10079 " [in module %s]"), objfile->name);
10080 continue;
10081 }
10082
9277c30c 10083 if (ranges_pst != NULL)
ff013f42 10084 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10085 range_beginning + baseaddr,
10086 range_end - 1 + baseaddr,
ff013f42
JK
10087 ranges_pst);
10088
43039443
JK
10089 /* FIXME: This is recording everything as a low-high
10090 segment of consecutive addresses. We should have a
10091 data structure for discontiguous block ranges
10092 instead. */
10093 if (! low_set)
10094 {
10095 low = range_beginning;
10096 high = range_end;
10097 low_set = 1;
10098 }
10099 else
10100 {
10101 if (range_beginning < low)
10102 low = range_beginning;
10103 if (range_end > high)
10104 high = range_end;
10105 }
10106 }
10107
10108 if (! low_set)
10109 /* If the first entry is an end-of-list marker, the range
10110 describes an empty scope, i.e. no instructions. */
10111 return 0;
10112
10113 if (low_return)
10114 *low_return = low;
10115 if (high_return)
10116 *high_return = high;
10117 return 1;
10118}
10119
af34e669
DJ
10120/* Get low and high pc attributes from a die. Return 1 if the attributes
10121 are present and valid, otherwise, return 0. Return -1 if the range is
10122 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10123
c906108c 10124static int
af34e669 10125dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10126 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10127 struct partial_symtab *pst)
c906108c
SS
10128{
10129 struct attribute *attr;
91da1414 10130 struct attribute *attr_high;
af34e669
DJ
10131 CORE_ADDR low = 0;
10132 CORE_ADDR high = 0;
10133 int ret = 0;
c906108c 10134
91da1414
MW
10135 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10136 if (attr_high)
af34e669 10137 {
e142c38c 10138 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10139 if (attr)
91da1414
MW
10140 {
10141 low = DW_ADDR (attr);
3019eac3
DE
10142 if (attr_high->form == DW_FORM_addr
10143 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10144 high = DW_ADDR (attr_high);
10145 else
10146 high = low + DW_UNSND (attr_high);
10147 }
af34e669
DJ
10148 else
10149 /* Found high w/o low attribute. */
10150 return 0;
10151
10152 /* Found consecutive range of addresses. */
10153 ret = 1;
10154 }
c906108c 10155 else
af34e669 10156 {
e142c38c 10157 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10158 if (attr != NULL)
10159 {
ab435259
DE
10160 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10161 We take advantage of the fact that DW_AT_ranges does not appear
10162 in DW_TAG_compile_unit of DWO files. */
10163 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10164 unsigned int ranges_offset = (DW_UNSND (attr)
10165 + (need_ranges_base
10166 ? cu->ranges_base
10167 : 0));
2e3cf129 10168
af34e669 10169 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10170 .debug_ranges section. */
2e3cf129 10171 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10172 return 0;
43039443 10173 /* Found discontinuous range of addresses. */
af34e669
DJ
10174 ret = -1;
10175 }
10176 }
c906108c 10177
9373cf26
JK
10178 /* read_partial_die has also the strict LOW < HIGH requirement. */
10179 if (high <= low)
c906108c
SS
10180 return 0;
10181
10182 /* When using the GNU linker, .gnu.linkonce. sections are used to
10183 eliminate duplicate copies of functions and vtables and such.
10184 The linker will arbitrarily choose one and discard the others.
10185 The AT_*_pc values for such functions refer to local labels in
10186 these sections. If the section from that file was discarded, the
10187 labels are not in the output, so the relocs get a value of 0.
10188 If this is a discarded function, mark the pc bounds as invalid,
10189 so that GDB will ignore it. */
72dca2f5 10190 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10191 return 0;
10192
10193 *lowpc = low;
96408a79
SA
10194 if (highpc)
10195 *highpc = high;
af34e669 10196 return ret;
c906108c
SS
10197}
10198
b084d499
JB
10199/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10200 its low and high PC addresses. Do nothing if these addresses could not
10201 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10202 and HIGHPC to the high address if greater than HIGHPC. */
10203
10204static void
10205dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10206 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10207 struct dwarf2_cu *cu)
10208{
10209 CORE_ADDR low, high;
10210 struct die_info *child = die->child;
10211
d85a05f0 10212 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10213 {
10214 *lowpc = min (*lowpc, low);
10215 *highpc = max (*highpc, high);
10216 }
10217
10218 /* If the language does not allow nested subprograms (either inside
10219 subprograms or lexical blocks), we're done. */
10220 if (cu->language != language_ada)
10221 return;
6e70227d 10222
b084d499
JB
10223 /* Check all the children of the given DIE. If it contains nested
10224 subprograms, then check their pc bounds. Likewise, we need to
10225 check lexical blocks as well, as they may also contain subprogram
10226 definitions. */
10227 while (child && child->tag)
10228 {
10229 if (child->tag == DW_TAG_subprogram
10230 || child->tag == DW_TAG_lexical_block)
10231 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10232 child = sibling_die (child);
10233 }
10234}
10235
fae299cd
DC
10236/* Get the low and high pc's represented by the scope DIE, and store
10237 them in *LOWPC and *HIGHPC. If the correct values can't be
10238 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10239
10240static void
10241get_scope_pc_bounds (struct die_info *die,
10242 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10243 struct dwarf2_cu *cu)
10244{
10245 CORE_ADDR best_low = (CORE_ADDR) -1;
10246 CORE_ADDR best_high = (CORE_ADDR) 0;
10247 CORE_ADDR current_low, current_high;
10248
d85a05f0 10249 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10250 {
10251 best_low = current_low;
10252 best_high = current_high;
10253 }
10254 else
10255 {
10256 struct die_info *child = die->child;
10257
10258 while (child && child->tag)
10259 {
10260 switch (child->tag) {
10261 case DW_TAG_subprogram:
b084d499 10262 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10263 break;
10264 case DW_TAG_namespace:
f55ee35c 10265 case DW_TAG_module:
fae299cd
DC
10266 /* FIXME: carlton/2004-01-16: Should we do this for
10267 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10268 that current GCC's always emit the DIEs corresponding
10269 to definitions of methods of classes as children of a
10270 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10271 the DIEs giving the declarations, which could be
10272 anywhere). But I don't see any reason why the
10273 standards says that they have to be there. */
10274 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10275
10276 if (current_low != ((CORE_ADDR) -1))
10277 {
10278 best_low = min (best_low, current_low);
10279 best_high = max (best_high, current_high);
10280 }
10281 break;
10282 default:
0963b4bd 10283 /* Ignore. */
fae299cd
DC
10284 break;
10285 }
10286
10287 child = sibling_die (child);
10288 }
10289 }
10290
10291 *lowpc = best_low;
10292 *highpc = best_high;
10293}
10294
801e3a5b
JB
10295/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10296 in DIE. */
380bca97 10297
801e3a5b
JB
10298static void
10299dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10300 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10301{
bb5ed363 10302 struct objfile *objfile = cu->objfile;
801e3a5b 10303 struct attribute *attr;
91da1414 10304 struct attribute *attr_high;
801e3a5b 10305
91da1414
MW
10306 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10307 if (attr_high)
801e3a5b 10308 {
801e3a5b
JB
10309 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10310 if (attr)
10311 {
10312 CORE_ADDR low = DW_ADDR (attr);
91da1414 10313 CORE_ADDR high;
3019eac3
DE
10314 if (attr_high->form == DW_FORM_addr
10315 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10316 high = DW_ADDR (attr_high);
10317 else
10318 high = low + DW_UNSND (attr_high);
9a619af0 10319
801e3a5b
JB
10320 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10321 }
10322 }
10323
10324 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10325 if (attr)
10326 {
bb5ed363 10327 bfd *obfd = objfile->obfd;
ab435259
DE
10328 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10329 We take advantage of the fact that DW_AT_ranges does not appear
10330 in DW_TAG_compile_unit of DWO files. */
10331 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10332
10333 /* The value of the DW_AT_ranges attribute is the offset of the
10334 address range list in the .debug_ranges section. */
ab435259
DE
10335 unsigned long offset = (DW_UNSND (attr)
10336 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10337 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10338
10339 /* For some target architectures, but not others, the
10340 read_address function sign-extends the addresses it returns.
10341 To recognize base address selection entries, we need a
10342 mask. */
10343 unsigned int addr_size = cu->header.addr_size;
10344 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10345
10346 /* The base address, to which the next pair is relative. Note
10347 that this 'base' is a DWARF concept: most entries in a range
10348 list are relative, to reduce the number of relocs against the
10349 debugging information. This is separate from this function's
10350 'baseaddr' argument, which GDB uses to relocate debugging
10351 information from a shared library based on the address at
10352 which the library was loaded. */
d00adf39
DE
10353 CORE_ADDR base = cu->base_address;
10354 int base_known = cu->base_known;
801e3a5b 10355
be391dca 10356 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10357 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10358 {
10359 complaint (&symfile_complaints,
10360 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10361 offset);
10362 return;
10363 }
10364
10365 for (;;)
10366 {
10367 unsigned int bytes_read;
10368 CORE_ADDR start, end;
10369
10370 start = read_address (obfd, buffer, cu, &bytes_read);
10371 buffer += bytes_read;
10372 end = read_address (obfd, buffer, cu, &bytes_read);
10373 buffer += bytes_read;
10374
10375 /* Did we find the end of the range list? */
10376 if (start == 0 && end == 0)
10377 break;
10378
10379 /* Did we find a base address selection entry? */
10380 else if ((start & base_select_mask) == base_select_mask)
10381 {
10382 base = end;
10383 base_known = 1;
10384 }
10385
10386 /* We found an ordinary address range. */
10387 else
10388 {
10389 if (!base_known)
10390 {
10391 complaint (&symfile_complaints,
3e43a32a
MS
10392 _("Invalid .debug_ranges data "
10393 "(no base address)"));
801e3a5b
JB
10394 return;
10395 }
10396
9277c30c
UW
10397 if (start > end)
10398 {
10399 /* Inverted range entries are invalid. */
10400 complaint (&symfile_complaints,
10401 _("Invalid .debug_ranges data "
10402 "(inverted range)"));
10403 return;
10404 }
10405
10406 /* Empty range entries have no effect. */
10407 if (start == end)
10408 continue;
10409
01093045
DE
10410 start += base + baseaddr;
10411 end += base + baseaddr;
10412
10413 /* A not-uncommon case of bad debug info.
10414 Don't pollute the addrmap with bad data. */
10415 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10416 {
10417 complaint (&symfile_complaints,
10418 _(".debug_ranges entry has start address of zero"
10419 " [in module %s]"), objfile->name);
10420 continue;
10421 }
10422
10423 record_block_range (block, start, end - 1);
801e3a5b
JB
10424 }
10425 }
10426 }
10427}
10428
685b1105
JK
10429/* Check whether the producer field indicates either of GCC < 4.6, or the
10430 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10431
685b1105
JK
10432static void
10433check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10434{
10435 const char *cs;
10436 int major, minor, release;
10437
10438 if (cu->producer == NULL)
10439 {
10440 /* For unknown compilers expect their behavior is DWARF version
10441 compliant.
10442
10443 GCC started to support .debug_types sections by -gdwarf-4 since
10444 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10445 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10446 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10447 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10448 }
685b1105 10449 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10450 {
685b1105
JK
10451 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10452
ba919b58
TT
10453 cs = &cu->producer[strlen ("GNU ")];
10454 while (*cs && !isdigit (*cs))
10455 cs++;
10456 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10457 {
10458 /* Not recognized as GCC. */
10459 }
10460 else
1b80a9fa
JK
10461 {
10462 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10463 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10464 }
685b1105
JK
10465 }
10466 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10467 cu->producer_is_icc = 1;
10468 else
10469 {
10470 /* For other non-GCC compilers, expect their behavior is DWARF version
10471 compliant. */
60d5a603
JK
10472 }
10473
ba919b58 10474 cu->checked_producer = 1;
685b1105 10475}
ba919b58 10476
685b1105
JK
10477/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10478 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10479 during 4.6.0 experimental. */
10480
10481static int
10482producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10483{
10484 if (!cu->checked_producer)
10485 check_producer (cu);
10486
10487 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10488}
10489
10490/* Return the default accessibility type if it is not overriden by
10491 DW_AT_accessibility. */
10492
10493static enum dwarf_access_attribute
10494dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10495{
10496 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10497 {
10498 /* The default DWARF 2 accessibility for members is public, the default
10499 accessibility for inheritance is private. */
10500
10501 if (die->tag != DW_TAG_inheritance)
10502 return DW_ACCESS_public;
10503 else
10504 return DW_ACCESS_private;
10505 }
10506 else
10507 {
10508 /* DWARF 3+ defines the default accessibility a different way. The same
10509 rules apply now for DW_TAG_inheritance as for the members and it only
10510 depends on the container kind. */
10511
10512 if (die->parent->tag == DW_TAG_class_type)
10513 return DW_ACCESS_private;
10514 else
10515 return DW_ACCESS_public;
10516 }
10517}
10518
74ac6d43
TT
10519/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10520 offset. If the attribute was not found return 0, otherwise return
10521 1. If it was found but could not properly be handled, set *OFFSET
10522 to 0. */
10523
10524static int
10525handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10526 LONGEST *offset)
10527{
10528 struct attribute *attr;
10529
10530 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10531 if (attr != NULL)
10532 {
10533 *offset = 0;
10534
10535 /* Note that we do not check for a section offset first here.
10536 This is because DW_AT_data_member_location is new in DWARF 4,
10537 so if we see it, we can assume that a constant form is really
10538 a constant and not a section offset. */
10539 if (attr_form_is_constant (attr))
10540 *offset = dwarf2_get_attr_constant_value (attr, 0);
10541 else if (attr_form_is_section_offset (attr))
10542 dwarf2_complex_location_expr_complaint ();
10543 else if (attr_form_is_block (attr))
10544 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10545 else
10546 dwarf2_complex_location_expr_complaint ();
10547
10548 return 1;
10549 }
10550
10551 return 0;
10552}
10553
c906108c
SS
10554/* Add an aggregate field to the field list. */
10555
10556static void
107d2387 10557dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10558 struct dwarf2_cu *cu)
6e70227d 10559{
e7c27a73 10560 struct objfile *objfile = cu->objfile;
5e2b427d 10561 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10562 struct nextfield *new_field;
10563 struct attribute *attr;
10564 struct field *fp;
15d034d0 10565 const char *fieldname = "";
c906108c
SS
10566
10567 /* Allocate a new field list entry and link it in. */
10568 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10569 make_cleanup (xfree, new_field);
c906108c 10570 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10571
10572 if (die->tag == DW_TAG_inheritance)
10573 {
10574 new_field->next = fip->baseclasses;
10575 fip->baseclasses = new_field;
10576 }
10577 else
10578 {
10579 new_field->next = fip->fields;
10580 fip->fields = new_field;
10581 }
c906108c
SS
10582 fip->nfields++;
10583
e142c38c 10584 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10585 if (attr)
10586 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10587 else
10588 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10589 if (new_field->accessibility != DW_ACCESS_public)
10590 fip->non_public_fields = 1;
60d5a603 10591
e142c38c 10592 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10593 if (attr)
10594 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10595 else
10596 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10597
10598 fp = &new_field->field;
a9a9bd0f 10599
e142c38c 10600 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10601 {
74ac6d43
TT
10602 LONGEST offset;
10603
a9a9bd0f 10604 /* Data member other than a C++ static data member. */
6e70227d 10605
c906108c 10606 /* Get type of field. */
e7c27a73 10607 fp->type = die_type (die, cu);
c906108c 10608
d6a843b5 10609 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10610
c906108c 10611 /* Get bit size of field (zero if none). */
e142c38c 10612 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10613 if (attr)
10614 {
10615 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10616 }
10617 else
10618 {
10619 FIELD_BITSIZE (*fp) = 0;
10620 }
10621
10622 /* Get bit offset of field. */
74ac6d43
TT
10623 if (handle_data_member_location (die, cu, &offset))
10624 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10625 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10626 if (attr)
10627 {
5e2b427d 10628 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10629 {
10630 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10631 additional bit offset from the MSB of the containing
10632 anonymous object to the MSB of the field. We don't
10633 have to do anything special since we don't need to
10634 know the size of the anonymous object. */
f41f5e61 10635 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10636 }
10637 else
10638 {
10639 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10640 MSB of the anonymous object, subtract off the number of
10641 bits from the MSB of the field to the MSB of the
10642 object, and then subtract off the number of bits of
10643 the field itself. The result is the bit offset of
10644 the LSB of the field. */
c906108c
SS
10645 int anonymous_size;
10646 int bit_offset = DW_UNSND (attr);
10647
e142c38c 10648 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10649 if (attr)
10650 {
10651 /* The size of the anonymous object containing
10652 the bit field is explicit, so use the
10653 indicated size (in bytes). */
10654 anonymous_size = DW_UNSND (attr);
10655 }
10656 else
10657 {
10658 /* The size of the anonymous object containing
10659 the bit field must be inferred from the type
10660 attribute of the data member containing the
10661 bit field. */
10662 anonymous_size = TYPE_LENGTH (fp->type);
10663 }
f41f5e61
PA
10664 SET_FIELD_BITPOS (*fp,
10665 (FIELD_BITPOS (*fp)
10666 + anonymous_size * bits_per_byte
10667 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10668 }
10669 }
10670
10671 /* Get name of field. */
39cbfefa
DJ
10672 fieldname = dwarf2_name (die, cu);
10673 if (fieldname == NULL)
10674 fieldname = "";
d8151005
DJ
10675
10676 /* The name is already allocated along with this objfile, so we don't
10677 need to duplicate it for the type. */
10678 fp->name = fieldname;
c906108c
SS
10679
10680 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10681 pointer or virtual base class pointer) to private. */
e142c38c 10682 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10683 {
d48cc9dd 10684 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10685 new_field->accessibility = DW_ACCESS_private;
10686 fip->non_public_fields = 1;
10687 }
10688 }
a9a9bd0f 10689 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10690 {
a9a9bd0f
DC
10691 /* C++ static member. */
10692
10693 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10694 is a declaration, but all versions of G++ as of this writing
10695 (so through at least 3.2.1) incorrectly generate
10696 DW_TAG_variable tags. */
6e70227d 10697
ff355380 10698 const char *physname;
c906108c 10699
a9a9bd0f 10700 /* Get name of field. */
39cbfefa
DJ
10701 fieldname = dwarf2_name (die, cu);
10702 if (fieldname == NULL)
c906108c
SS
10703 return;
10704
254e6b9e 10705 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10706 if (attr
10707 /* Only create a symbol if this is an external value.
10708 new_symbol checks this and puts the value in the global symbol
10709 table, which we want. If it is not external, new_symbol
10710 will try to put the value in cu->list_in_scope which is wrong. */
10711 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10712 {
10713 /* A static const member, not much different than an enum as far as
10714 we're concerned, except that we can support more types. */
10715 new_symbol (die, NULL, cu);
10716 }
10717
2df3850c 10718 /* Get physical name. */
ff355380 10719 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10720
d8151005
DJ
10721 /* The name is already allocated along with this objfile, so we don't
10722 need to duplicate it for the type. */
10723 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10724 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10725 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10726 }
10727 else if (die->tag == DW_TAG_inheritance)
10728 {
74ac6d43 10729 LONGEST offset;
d4b96c9a 10730
74ac6d43
TT
10731 /* C++ base class field. */
10732 if (handle_data_member_location (die, cu, &offset))
10733 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10734 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10735 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10736 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10737 fip->nbaseclasses++;
10738 }
10739}
10740
98751a41
JK
10741/* Add a typedef defined in the scope of the FIP's class. */
10742
10743static void
10744dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10745 struct dwarf2_cu *cu)
6e70227d 10746{
98751a41 10747 struct objfile *objfile = cu->objfile;
98751a41
JK
10748 struct typedef_field_list *new_field;
10749 struct attribute *attr;
10750 struct typedef_field *fp;
10751 char *fieldname = "";
10752
10753 /* Allocate a new field list entry and link it in. */
10754 new_field = xzalloc (sizeof (*new_field));
10755 make_cleanup (xfree, new_field);
10756
10757 gdb_assert (die->tag == DW_TAG_typedef);
10758
10759 fp = &new_field->field;
10760
10761 /* Get name of field. */
10762 fp->name = dwarf2_name (die, cu);
10763 if (fp->name == NULL)
10764 return;
10765
10766 fp->type = read_type_die (die, cu);
10767
10768 new_field->next = fip->typedef_field_list;
10769 fip->typedef_field_list = new_field;
10770 fip->typedef_field_list_count++;
10771}
10772
c906108c
SS
10773/* Create the vector of fields, and attach it to the type. */
10774
10775static void
fba45db2 10776dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10777 struct dwarf2_cu *cu)
c906108c
SS
10778{
10779 int nfields = fip->nfields;
10780
10781 /* Record the field count, allocate space for the array of fields,
10782 and create blank accessibility bitfields if necessary. */
10783 TYPE_NFIELDS (type) = nfields;
10784 TYPE_FIELDS (type) = (struct field *)
10785 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10786 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10787
b4ba55a1 10788 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10789 {
10790 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10791
10792 TYPE_FIELD_PRIVATE_BITS (type) =
10793 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10794 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10795
10796 TYPE_FIELD_PROTECTED_BITS (type) =
10797 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10798 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10799
774b6a14
TT
10800 TYPE_FIELD_IGNORE_BITS (type) =
10801 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10802 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10803 }
10804
10805 /* If the type has baseclasses, allocate and clear a bit vector for
10806 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10807 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10808 {
10809 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10810 unsigned char *pointer;
c906108c
SS
10811
10812 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10813 pointer = TYPE_ALLOC (type, num_bytes);
10814 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10815 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10816 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10817 }
10818
3e43a32a
MS
10819 /* Copy the saved-up fields into the field vector. Start from the head of
10820 the list, adding to the tail of the field array, so that they end up in
10821 the same order in the array in which they were added to the list. */
c906108c
SS
10822 while (nfields-- > 0)
10823 {
7d0ccb61
DJ
10824 struct nextfield *fieldp;
10825
10826 if (fip->fields)
10827 {
10828 fieldp = fip->fields;
10829 fip->fields = fieldp->next;
10830 }
10831 else
10832 {
10833 fieldp = fip->baseclasses;
10834 fip->baseclasses = fieldp->next;
10835 }
10836
10837 TYPE_FIELD (type, nfields) = fieldp->field;
10838 switch (fieldp->accessibility)
c906108c 10839 {
c5aa993b 10840 case DW_ACCESS_private:
b4ba55a1
JB
10841 if (cu->language != language_ada)
10842 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10843 break;
c906108c 10844
c5aa993b 10845 case DW_ACCESS_protected:
b4ba55a1
JB
10846 if (cu->language != language_ada)
10847 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10848 break;
c906108c 10849
c5aa993b
JM
10850 case DW_ACCESS_public:
10851 break;
c906108c 10852
c5aa993b
JM
10853 default:
10854 /* Unknown accessibility. Complain and treat it as public. */
10855 {
e2e0b3e5 10856 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10857 fieldp->accessibility);
c5aa993b
JM
10858 }
10859 break;
c906108c
SS
10860 }
10861 if (nfields < fip->nbaseclasses)
10862 {
7d0ccb61 10863 switch (fieldp->virtuality)
c906108c 10864 {
c5aa993b
JM
10865 case DW_VIRTUALITY_virtual:
10866 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10867 if (cu->language == language_ada)
a73c6dcd 10868 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10869 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10870 break;
c906108c
SS
10871 }
10872 }
c906108c
SS
10873 }
10874}
10875
7d27a96d
TT
10876/* Return true if this member function is a constructor, false
10877 otherwise. */
10878
10879static int
10880dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10881{
10882 const char *fieldname;
10883 const char *typename;
10884 int len;
10885
10886 if (die->parent == NULL)
10887 return 0;
10888
10889 if (die->parent->tag != DW_TAG_structure_type
10890 && die->parent->tag != DW_TAG_union_type
10891 && die->parent->tag != DW_TAG_class_type)
10892 return 0;
10893
10894 fieldname = dwarf2_name (die, cu);
10895 typename = dwarf2_name (die->parent, cu);
10896 if (fieldname == NULL || typename == NULL)
10897 return 0;
10898
10899 len = strlen (fieldname);
10900 return (strncmp (fieldname, typename, len) == 0
10901 && (typename[len] == '\0' || typename[len] == '<'));
10902}
10903
c906108c
SS
10904/* Add a member function to the proper fieldlist. */
10905
10906static void
107d2387 10907dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10908 struct type *type, struct dwarf2_cu *cu)
c906108c 10909{
e7c27a73 10910 struct objfile *objfile = cu->objfile;
c906108c
SS
10911 struct attribute *attr;
10912 struct fnfieldlist *flp;
10913 int i;
10914 struct fn_field *fnp;
15d034d0 10915 const char *fieldname;
c906108c 10916 struct nextfnfield *new_fnfield;
f792889a 10917 struct type *this_type;
60d5a603 10918 enum dwarf_access_attribute accessibility;
c906108c 10919
b4ba55a1 10920 if (cu->language == language_ada)
a73c6dcd 10921 error (_("unexpected member function in Ada type"));
b4ba55a1 10922
2df3850c 10923 /* Get name of member function. */
39cbfefa
DJ
10924 fieldname = dwarf2_name (die, cu);
10925 if (fieldname == NULL)
2df3850c 10926 return;
c906108c 10927
c906108c
SS
10928 /* Look up member function name in fieldlist. */
10929 for (i = 0; i < fip->nfnfields; i++)
10930 {
27bfe10e 10931 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10932 break;
10933 }
10934
10935 /* Create new list element if necessary. */
10936 if (i < fip->nfnfields)
10937 flp = &fip->fnfieldlists[i];
10938 else
10939 {
10940 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10941 {
10942 fip->fnfieldlists = (struct fnfieldlist *)
10943 xrealloc (fip->fnfieldlists,
10944 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10945 * sizeof (struct fnfieldlist));
c906108c 10946 if (fip->nfnfields == 0)
c13c43fd 10947 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10948 }
10949 flp = &fip->fnfieldlists[fip->nfnfields];
10950 flp->name = fieldname;
10951 flp->length = 0;
10952 flp->head = NULL;
3da10d80 10953 i = fip->nfnfields++;
c906108c
SS
10954 }
10955
10956 /* Create a new member function field and chain it to the field list
0963b4bd 10957 entry. */
c906108c 10958 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10959 make_cleanup (xfree, new_fnfield);
c906108c
SS
10960 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10961 new_fnfield->next = flp->head;
10962 flp->head = new_fnfield;
10963 flp->length++;
10964
10965 /* Fill in the member function field info. */
10966 fnp = &new_fnfield->fnfield;
3da10d80
KS
10967
10968 /* Delay processing of the physname until later. */
10969 if (cu->language == language_cplus || cu->language == language_java)
10970 {
10971 add_to_method_list (type, i, flp->length - 1, fieldname,
10972 die, cu);
10973 }
10974 else
10975 {
1d06ead6 10976 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10977 fnp->physname = physname ? physname : "";
10978 }
10979
c906108c 10980 fnp->type = alloc_type (objfile);
f792889a
DJ
10981 this_type = read_type_die (die, cu);
10982 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10983 {
f792889a 10984 int nparams = TYPE_NFIELDS (this_type);
c906108c 10985
f792889a 10986 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10987 of the method itself (TYPE_CODE_METHOD). */
10988 smash_to_method_type (fnp->type, type,
f792889a
DJ
10989 TYPE_TARGET_TYPE (this_type),
10990 TYPE_FIELDS (this_type),
10991 TYPE_NFIELDS (this_type),
10992 TYPE_VARARGS (this_type));
c906108c
SS
10993
10994 /* Handle static member functions.
c5aa993b 10995 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10996 member functions. G++ helps GDB by marking the first
10997 parameter for non-static member functions (which is the this
10998 pointer) as artificial. We obtain this information from
10999 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11000 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11001 fnp->voffset = VOFFSET_STATIC;
11002 }
11003 else
e2e0b3e5 11004 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11005 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11006
11007 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11008 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11009 fnp->fcontext = die_containing_type (die, cu);
c906108c 11010
3e43a32a
MS
11011 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11012 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11013
11014 /* Get accessibility. */
e142c38c 11015 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11016 if (attr)
60d5a603
JK
11017 accessibility = DW_UNSND (attr);
11018 else
11019 accessibility = dwarf2_default_access_attribute (die, cu);
11020 switch (accessibility)
c906108c 11021 {
60d5a603
JK
11022 case DW_ACCESS_private:
11023 fnp->is_private = 1;
11024 break;
11025 case DW_ACCESS_protected:
11026 fnp->is_protected = 1;
11027 break;
c906108c
SS
11028 }
11029
b02dede2 11030 /* Check for artificial methods. */
e142c38c 11031 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11032 if (attr && DW_UNSND (attr) != 0)
11033 fnp->is_artificial = 1;
11034
7d27a96d
TT
11035 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11036
0d564a31 11037 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11038 function. For older versions of GCC, this is an offset in the
11039 appropriate virtual table, as specified by DW_AT_containing_type.
11040 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11041 to the object address. */
11042
e142c38c 11043 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11044 if (attr)
8e19ed76 11045 {
aec5aa8b 11046 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11047 {
aec5aa8b
TT
11048 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11049 {
11050 /* Old-style GCC. */
11051 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11052 }
11053 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11054 || (DW_BLOCK (attr)->size > 1
11055 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11056 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11057 {
11058 struct dwarf_block blk;
11059 int offset;
11060
11061 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11062 ? 1 : 2);
11063 blk.size = DW_BLOCK (attr)->size - offset;
11064 blk.data = DW_BLOCK (attr)->data + offset;
11065 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11066 if ((fnp->voffset % cu->header.addr_size) != 0)
11067 dwarf2_complex_location_expr_complaint ();
11068 else
11069 fnp->voffset /= cu->header.addr_size;
11070 fnp->voffset += 2;
11071 }
11072 else
11073 dwarf2_complex_location_expr_complaint ();
11074
11075 if (!fnp->fcontext)
11076 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11077 }
3690dd37 11078 else if (attr_form_is_section_offset (attr))
8e19ed76 11079 {
4d3c2250 11080 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11081 }
11082 else
11083 {
4d3c2250
KB
11084 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11085 fieldname);
8e19ed76 11086 }
0d564a31 11087 }
d48cc9dd
DJ
11088 else
11089 {
11090 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11091 if (attr && DW_UNSND (attr))
11092 {
11093 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11094 complaint (&symfile_complaints,
3e43a32a
MS
11095 _("Member function \"%s\" (offset %d) is virtual "
11096 "but the vtable offset is not specified"),
b64f50a1 11097 fieldname, die->offset.sect_off);
9655fd1a 11098 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11099 TYPE_CPLUS_DYNAMIC (type) = 1;
11100 }
11101 }
c906108c
SS
11102}
11103
11104/* Create the vector of member function fields, and attach it to the type. */
11105
11106static void
fba45db2 11107dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11108 struct dwarf2_cu *cu)
c906108c
SS
11109{
11110 struct fnfieldlist *flp;
c906108c
SS
11111 int i;
11112
b4ba55a1 11113 if (cu->language == language_ada)
a73c6dcd 11114 error (_("unexpected member functions in Ada type"));
b4ba55a1 11115
c906108c
SS
11116 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11117 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11118 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11119
11120 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11121 {
11122 struct nextfnfield *nfp = flp->head;
11123 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11124 int k;
11125
11126 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11127 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11128 fn_flp->fn_fields = (struct fn_field *)
11129 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11130 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11131 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11132 }
11133
11134 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11135}
11136
1168df01
JB
11137/* Returns non-zero if NAME is the name of a vtable member in CU's
11138 language, zero otherwise. */
11139static int
11140is_vtable_name (const char *name, struct dwarf2_cu *cu)
11141{
11142 static const char vptr[] = "_vptr";
987504bb 11143 static const char vtable[] = "vtable";
1168df01 11144
987504bb
JJ
11145 /* Look for the C++ and Java forms of the vtable. */
11146 if ((cu->language == language_java
11147 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11148 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11149 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11150 return 1;
11151
11152 return 0;
11153}
11154
c0dd20ea 11155/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11156 functions, with the ABI-specified layout. If TYPE describes
11157 such a structure, smash it into a member function type.
61049d3b
DJ
11158
11159 GCC shouldn't do this; it should just output pointer to member DIEs.
11160 This is GCC PR debug/28767. */
c0dd20ea 11161
0b92b5bb
TT
11162static void
11163quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11164{
0b92b5bb 11165 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11166
11167 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11168 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11169 return;
c0dd20ea
DJ
11170
11171 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11172 if (TYPE_FIELD_NAME (type, 0) == NULL
11173 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11174 || TYPE_FIELD_NAME (type, 1) == NULL
11175 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11176 return;
c0dd20ea
DJ
11177
11178 /* Find the type of the method. */
0b92b5bb 11179 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11180 if (pfn_type == NULL
11181 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11182 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11183 return;
c0dd20ea
DJ
11184
11185 /* Look for the "this" argument. */
11186 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11187 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11188 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11189 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11190 return;
c0dd20ea
DJ
11191
11192 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11193 new_type = alloc_type (objfile);
11194 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11195 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11196 TYPE_VARARGS (pfn_type));
0b92b5bb 11197 smash_to_methodptr_type (type, new_type);
c0dd20ea 11198}
1168df01 11199
685b1105
JK
11200/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11201 (icc). */
11202
11203static int
11204producer_is_icc (struct dwarf2_cu *cu)
11205{
11206 if (!cu->checked_producer)
11207 check_producer (cu);
11208
11209 return cu->producer_is_icc;
11210}
11211
c906108c 11212/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11213 (definition) to create a type for the structure or union. Fill in
11214 the type's name and general properties; the members will not be
11215 processed until process_structure_type.
c906108c 11216
c767944b
DJ
11217 NOTE: we need to call these functions regardless of whether or not the
11218 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11219 structure or union. This gets the type entered into our set of
11220 user defined types.
11221
11222 However, if the structure is incomplete (an opaque struct/union)
11223 then suppress creating a symbol table entry for it since gdb only
11224 wants to find the one with the complete definition. Note that if
11225 it is complete, we just call new_symbol, which does it's own
11226 checking about whether the struct/union is anonymous or not (and
11227 suppresses creating a symbol table entry itself). */
11228
f792889a 11229static struct type *
134d01f1 11230read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11231{
e7c27a73 11232 struct objfile *objfile = cu->objfile;
c906108c
SS
11233 struct type *type;
11234 struct attribute *attr;
15d034d0 11235 const char *name;
c906108c 11236
348e048f
DE
11237 /* If the definition of this type lives in .debug_types, read that type.
11238 Don't follow DW_AT_specification though, that will take us back up
11239 the chain and we want to go down. */
45e58e77 11240 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11241 if (attr)
11242 {
11243 struct dwarf2_cu *type_cu = cu;
11244 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11245
348e048f
DE
11246 /* We could just recurse on read_structure_type, but we need to call
11247 get_die_type to ensure only one type for this DIE is created.
11248 This is important, for example, because for c++ classes we need
11249 TYPE_NAME set which is only done by new_symbol. Blech. */
11250 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11251
11252 /* TYPE_CU may not be the same as CU.
11253 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11254 return set_die_type (die, type, cu);
11255 }
11256
c0dd20ea 11257 type = alloc_type (objfile);
c906108c 11258 INIT_CPLUS_SPECIFIC (type);
93311388 11259
39cbfefa
DJ
11260 name = dwarf2_name (die, cu);
11261 if (name != NULL)
c906108c 11262 {
987504bb
JJ
11263 if (cu->language == language_cplus
11264 || cu->language == language_java)
63d06c5c 11265 {
15d034d0 11266 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11267
11268 /* dwarf2_full_name might have already finished building the DIE's
11269 type. If so, there is no need to continue. */
11270 if (get_die_type (die, cu) != NULL)
11271 return get_die_type (die, cu);
11272
11273 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11274 if (die->tag == DW_TAG_structure_type
11275 || die->tag == DW_TAG_class_type)
11276 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11277 }
11278 else
11279 {
d8151005
DJ
11280 /* The name is already allocated along with this objfile, so
11281 we don't need to duplicate it for the type. */
7d455152 11282 TYPE_TAG_NAME (type) = name;
94af9270
KS
11283 if (die->tag == DW_TAG_class_type)
11284 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11285 }
c906108c
SS
11286 }
11287
11288 if (die->tag == DW_TAG_structure_type)
11289 {
11290 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11291 }
11292 else if (die->tag == DW_TAG_union_type)
11293 {
11294 TYPE_CODE (type) = TYPE_CODE_UNION;
11295 }
11296 else
11297 {
c906108c
SS
11298 TYPE_CODE (type) = TYPE_CODE_CLASS;
11299 }
11300
0cc2414c
TT
11301 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11302 TYPE_DECLARED_CLASS (type) = 1;
11303
e142c38c 11304 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11305 if (attr)
11306 {
11307 TYPE_LENGTH (type) = DW_UNSND (attr);
11308 }
11309 else
11310 {
11311 TYPE_LENGTH (type) = 0;
11312 }
11313
685b1105
JK
11314 if (producer_is_icc (cu))
11315 {
11316 /* ICC does not output the required DW_AT_declaration
11317 on incomplete types, but gives them a size of zero. */
11318 }
11319 else
11320 TYPE_STUB_SUPPORTED (type) = 1;
11321
dc718098 11322 if (die_is_declaration (die, cu))
876cecd0 11323 TYPE_STUB (type) = 1;
a6c727b2
DJ
11324 else if (attr == NULL && die->child == NULL
11325 && producer_is_realview (cu->producer))
11326 /* RealView does not output the required DW_AT_declaration
11327 on incomplete types. */
11328 TYPE_STUB (type) = 1;
dc718098 11329
c906108c
SS
11330 /* We need to add the type field to the die immediately so we don't
11331 infinitely recurse when dealing with pointers to the structure
0963b4bd 11332 type within the structure itself. */
1c379e20 11333 set_die_type (die, type, cu);
c906108c 11334
7e314c57
JK
11335 /* set_die_type should be already done. */
11336 set_descriptive_type (type, die, cu);
11337
c767944b
DJ
11338 return type;
11339}
11340
11341/* Finish creating a structure or union type, including filling in
11342 its members and creating a symbol for it. */
11343
11344static void
11345process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11346{
11347 struct objfile *objfile = cu->objfile;
11348 struct die_info *child_die = die->child;
11349 struct type *type;
11350
11351 type = get_die_type (die, cu);
11352 if (type == NULL)
11353 type = read_structure_type (die, cu);
11354
e142c38c 11355 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11356 {
11357 struct field_info fi;
11358 struct die_info *child_die;
34eaf542 11359 VEC (symbolp) *template_args = NULL;
c767944b 11360 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11361
11362 memset (&fi, 0, sizeof (struct field_info));
11363
639d11d3 11364 child_die = die->child;
c906108c
SS
11365
11366 while (child_die && child_die->tag)
11367 {
a9a9bd0f
DC
11368 if (child_die->tag == DW_TAG_member
11369 || child_die->tag == DW_TAG_variable)
c906108c 11370 {
a9a9bd0f
DC
11371 /* NOTE: carlton/2002-11-05: A C++ static data member
11372 should be a DW_TAG_member that is a declaration, but
11373 all versions of G++ as of this writing (so through at
11374 least 3.2.1) incorrectly generate DW_TAG_variable
11375 tags for them instead. */
e7c27a73 11376 dwarf2_add_field (&fi, child_die, cu);
c906108c 11377 }
8713b1b1 11378 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11379 {
0963b4bd 11380 /* C++ member function. */
e7c27a73 11381 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11382 }
11383 else if (child_die->tag == DW_TAG_inheritance)
11384 {
11385 /* C++ base class field. */
e7c27a73 11386 dwarf2_add_field (&fi, child_die, cu);
c906108c 11387 }
98751a41
JK
11388 else if (child_die->tag == DW_TAG_typedef)
11389 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11390 else if (child_die->tag == DW_TAG_template_type_param
11391 || child_die->tag == DW_TAG_template_value_param)
11392 {
11393 struct symbol *arg = new_symbol (child_die, NULL, cu);
11394
f1078f66
DJ
11395 if (arg != NULL)
11396 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11397 }
11398
c906108c
SS
11399 child_die = sibling_die (child_die);
11400 }
11401
34eaf542
TT
11402 /* Attach template arguments to type. */
11403 if (! VEC_empty (symbolp, template_args))
11404 {
11405 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11406 TYPE_N_TEMPLATE_ARGUMENTS (type)
11407 = VEC_length (symbolp, template_args);
11408 TYPE_TEMPLATE_ARGUMENTS (type)
11409 = obstack_alloc (&objfile->objfile_obstack,
11410 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11411 * sizeof (struct symbol *)));
11412 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11413 VEC_address (symbolp, template_args),
11414 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11415 * sizeof (struct symbol *)));
11416 VEC_free (symbolp, template_args);
11417 }
11418
c906108c
SS
11419 /* Attach fields and member functions to the type. */
11420 if (fi.nfields)
e7c27a73 11421 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11422 if (fi.nfnfields)
11423 {
e7c27a73 11424 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11425
c5aa993b 11426 /* Get the type which refers to the base class (possibly this
c906108c 11427 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11428 class from the DW_AT_containing_type attribute. This use of
11429 DW_AT_containing_type is a GNU extension. */
c906108c 11430
e142c38c 11431 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11432 {
e7c27a73 11433 struct type *t = die_containing_type (die, cu);
c906108c
SS
11434
11435 TYPE_VPTR_BASETYPE (type) = t;
11436 if (type == t)
11437 {
c906108c
SS
11438 int i;
11439
11440 /* Our own class provides vtbl ptr. */
11441 for (i = TYPE_NFIELDS (t) - 1;
11442 i >= TYPE_N_BASECLASSES (t);
11443 --i)
11444 {
0d5cff50 11445 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11446
1168df01 11447 if (is_vtable_name (fieldname, cu))
c906108c
SS
11448 {
11449 TYPE_VPTR_FIELDNO (type) = i;
11450 break;
11451 }
11452 }
11453
11454 /* Complain if virtual function table field not found. */
11455 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11456 complaint (&symfile_complaints,
3e43a32a
MS
11457 _("virtual function table pointer "
11458 "not found when defining class '%s'"),
4d3c2250
KB
11459 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11460 "");
c906108c
SS
11461 }
11462 else
11463 {
11464 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11465 }
11466 }
f6235d4c
EZ
11467 else if (cu->producer
11468 && strncmp (cu->producer,
11469 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11470 {
11471 /* The IBM XLC compiler does not provide direct indication
11472 of the containing type, but the vtable pointer is
11473 always named __vfp. */
11474
11475 int i;
11476
11477 for (i = TYPE_NFIELDS (type) - 1;
11478 i >= TYPE_N_BASECLASSES (type);
11479 --i)
11480 {
11481 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11482 {
11483 TYPE_VPTR_FIELDNO (type) = i;
11484 TYPE_VPTR_BASETYPE (type) = type;
11485 break;
11486 }
11487 }
11488 }
c906108c 11489 }
98751a41
JK
11490
11491 /* Copy fi.typedef_field_list linked list elements content into the
11492 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11493 if (fi.typedef_field_list)
11494 {
11495 int i = fi.typedef_field_list_count;
11496
a0d7a4ff 11497 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11498 TYPE_TYPEDEF_FIELD_ARRAY (type)
11499 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11500 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11501
11502 /* Reverse the list order to keep the debug info elements order. */
11503 while (--i >= 0)
11504 {
11505 struct typedef_field *dest, *src;
6e70227d 11506
98751a41
JK
11507 dest = &TYPE_TYPEDEF_FIELD (type, i);
11508 src = &fi.typedef_field_list->field;
11509 fi.typedef_field_list = fi.typedef_field_list->next;
11510 *dest = *src;
11511 }
11512 }
c767944b
DJ
11513
11514 do_cleanups (back_to);
eb2a6f42
TT
11515
11516 if (HAVE_CPLUS_STRUCT (type))
11517 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11518 }
63d06c5c 11519
bb5ed363 11520 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11521
90aeadfc
DC
11522 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11523 snapshots) has been known to create a die giving a declaration
11524 for a class that has, as a child, a die giving a definition for a
11525 nested class. So we have to process our children even if the
11526 current die is a declaration. Normally, of course, a declaration
11527 won't have any children at all. */
134d01f1 11528
90aeadfc
DC
11529 while (child_die != NULL && child_die->tag)
11530 {
11531 if (child_die->tag == DW_TAG_member
11532 || child_die->tag == DW_TAG_variable
34eaf542
TT
11533 || child_die->tag == DW_TAG_inheritance
11534 || child_die->tag == DW_TAG_template_value_param
11535 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11536 {
90aeadfc 11537 /* Do nothing. */
134d01f1 11538 }
90aeadfc
DC
11539 else
11540 process_die (child_die, cu);
134d01f1 11541
90aeadfc 11542 child_die = sibling_die (child_die);
134d01f1
DJ
11543 }
11544
fa4028e9
JB
11545 /* Do not consider external references. According to the DWARF standard,
11546 these DIEs are identified by the fact that they have no byte_size
11547 attribute, and a declaration attribute. */
11548 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11549 || !die_is_declaration (die, cu))
c767944b 11550 new_symbol (die, type, cu);
134d01f1
DJ
11551}
11552
11553/* Given a DW_AT_enumeration_type die, set its type. We do not
11554 complete the type's fields yet, or create any symbols. */
c906108c 11555
f792889a 11556static struct type *
134d01f1 11557read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11558{
e7c27a73 11559 struct objfile *objfile = cu->objfile;
c906108c 11560 struct type *type;
c906108c 11561 struct attribute *attr;
0114d602 11562 const char *name;
134d01f1 11563
348e048f
DE
11564 /* If the definition of this type lives in .debug_types, read that type.
11565 Don't follow DW_AT_specification though, that will take us back up
11566 the chain and we want to go down. */
45e58e77 11567 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11568 if (attr)
11569 {
11570 struct dwarf2_cu *type_cu = cu;
11571 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11572
348e048f 11573 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11574
11575 /* TYPE_CU may not be the same as CU.
11576 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11577 return set_die_type (die, type, cu);
11578 }
11579
c906108c
SS
11580 type = alloc_type (objfile);
11581
11582 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11583 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11584 if (name != NULL)
7d455152 11585 TYPE_TAG_NAME (type) = name;
c906108c 11586
e142c38c 11587 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11588 if (attr)
11589 {
11590 TYPE_LENGTH (type) = DW_UNSND (attr);
11591 }
11592 else
11593 {
11594 TYPE_LENGTH (type) = 0;
11595 }
11596
137033e9
JB
11597 /* The enumeration DIE can be incomplete. In Ada, any type can be
11598 declared as private in the package spec, and then defined only
11599 inside the package body. Such types are known as Taft Amendment
11600 Types. When another package uses such a type, an incomplete DIE
11601 may be generated by the compiler. */
02eb380e 11602 if (die_is_declaration (die, cu))
876cecd0 11603 TYPE_STUB (type) = 1;
02eb380e 11604
f792889a 11605 return set_die_type (die, type, cu);
134d01f1
DJ
11606}
11607
11608/* Given a pointer to a die which begins an enumeration, process all
11609 the dies that define the members of the enumeration, and create the
11610 symbol for the enumeration type.
11611
11612 NOTE: We reverse the order of the element list. */
11613
11614static void
11615process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11616{
f792889a 11617 struct type *this_type;
134d01f1 11618
f792889a
DJ
11619 this_type = get_die_type (die, cu);
11620 if (this_type == NULL)
11621 this_type = read_enumeration_type (die, cu);
9dc481d3 11622
639d11d3 11623 if (die->child != NULL)
c906108c 11624 {
9dc481d3
DE
11625 struct die_info *child_die;
11626 struct symbol *sym;
11627 struct field *fields = NULL;
11628 int num_fields = 0;
11629 int unsigned_enum = 1;
15d034d0 11630 const char *name;
cafec441
TT
11631 int flag_enum = 1;
11632 ULONGEST mask = 0;
9dc481d3 11633
639d11d3 11634 child_die = die->child;
c906108c
SS
11635 while (child_die && child_die->tag)
11636 {
11637 if (child_die->tag != DW_TAG_enumerator)
11638 {
e7c27a73 11639 process_die (child_die, cu);
c906108c
SS
11640 }
11641 else
11642 {
39cbfefa
DJ
11643 name = dwarf2_name (child_die, cu);
11644 if (name)
c906108c 11645 {
f792889a 11646 sym = new_symbol (child_die, this_type, cu);
c906108c 11647 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11648 {
11649 unsigned_enum = 0;
11650 flag_enum = 0;
11651 }
11652 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11653 flag_enum = 0;
11654 else
11655 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11656
11657 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11658 {
11659 fields = (struct field *)
11660 xrealloc (fields,
11661 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11662 * sizeof (struct field));
c906108c
SS
11663 }
11664
3567439c 11665 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11666 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11667 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11668 FIELD_BITSIZE (fields[num_fields]) = 0;
11669
11670 num_fields++;
11671 }
11672 }
11673
11674 child_die = sibling_die (child_die);
11675 }
11676
11677 if (num_fields)
11678 {
f792889a
DJ
11679 TYPE_NFIELDS (this_type) = num_fields;
11680 TYPE_FIELDS (this_type) = (struct field *)
11681 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11682 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11683 sizeof (struct field) * num_fields);
b8c9b27d 11684 xfree (fields);
c906108c
SS
11685 }
11686 if (unsigned_enum)
876cecd0 11687 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11688 if (flag_enum)
11689 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11690 }
134d01f1 11691
6c83ed52
TT
11692 /* If we are reading an enum from a .debug_types unit, and the enum
11693 is a declaration, and the enum is not the signatured type in the
11694 unit, then we do not want to add a symbol for it. Adding a
11695 symbol would in some cases obscure the true definition of the
11696 enum, giving users an incomplete type when the definition is
11697 actually available. Note that we do not want to do this for all
11698 enums which are just declarations, because C++0x allows forward
11699 enum declarations. */
3019eac3 11700 if (cu->per_cu->is_debug_types
6c83ed52
TT
11701 && die_is_declaration (die, cu))
11702 {
52dc124a 11703 struct signatured_type *sig_type;
6c83ed52 11704
52dc124a 11705 sig_type
6c83ed52 11706 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11707 cu->per_cu->info_or_types_section,
6c83ed52 11708 cu->per_cu->offset);
3019eac3
DE
11709 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11710 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11711 return;
11712 }
11713
f792889a 11714 new_symbol (die, this_type, cu);
c906108c
SS
11715}
11716
11717/* Extract all information from a DW_TAG_array_type DIE and put it in
11718 the DIE's type field. For now, this only handles one dimensional
11719 arrays. */
11720
f792889a 11721static struct type *
e7c27a73 11722read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11723{
e7c27a73 11724 struct objfile *objfile = cu->objfile;
c906108c 11725 struct die_info *child_die;
7e314c57 11726 struct type *type;
c906108c
SS
11727 struct type *element_type, *range_type, *index_type;
11728 struct type **range_types = NULL;
11729 struct attribute *attr;
11730 int ndim = 0;
11731 struct cleanup *back_to;
15d034d0 11732 const char *name;
c906108c 11733
e7c27a73 11734 element_type = die_type (die, cu);
c906108c 11735
7e314c57
JK
11736 /* The die_type call above may have already set the type for this DIE. */
11737 type = get_die_type (die, cu);
11738 if (type)
11739 return type;
11740
c906108c
SS
11741 /* Irix 6.2 native cc creates array types without children for
11742 arrays with unspecified length. */
639d11d3 11743 if (die->child == NULL)
c906108c 11744 {
46bf5051 11745 index_type = objfile_type (objfile)->builtin_int;
c906108c 11746 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11747 type = create_array_type (NULL, element_type, range_type);
11748 return set_die_type (die, type, cu);
c906108c
SS
11749 }
11750
11751 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11752 child_die = die->child;
c906108c
SS
11753 while (child_die && child_die->tag)
11754 {
11755 if (child_die->tag == DW_TAG_subrange_type)
11756 {
f792889a 11757 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11758
f792889a 11759 if (child_type != NULL)
a02abb62 11760 {
0963b4bd
MS
11761 /* The range type was succesfully read. Save it for the
11762 array type creation. */
a02abb62
JB
11763 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11764 {
11765 range_types = (struct type **)
11766 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11767 * sizeof (struct type *));
11768 if (ndim == 0)
11769 make_cleanup (free_current_contents, &range_types);
11770 }
f792889a 11771 range_types[ndim++] = child_type;
a02abb62 11772 }
c906108c
SS
11773 }
11774 child_die = sibling_die (child_die);
11775 }
11776
11777 /* Dwarf2 dimensions are output from left to right, create the
11778 necessary array types in backwards order. */
7ca2d3a3 11779
c906108c 11780 type = element_type;
7ca2d3a3
DL
11781
11782 if (read_array_order (die, cu) == DW_ORD_col_major)
11783 {
11784 int i = 0;
9a619af0 11785
7ca2d3a3
DL
11786 while (i < ndim)
11787 type = create_array_type (NULL, type, range_types[i++]);
11788 }
11789 else
11790 {
11791 while (ndim-- > 0)
11792 type = create_array_type (NULL, type, range_types[ndim]);
11793 }
c906108c 11794
f5f8a009
EZ
11795 /* Understand Dwarf2 support for vector types (like they occur on
11796 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11797 array type. This is not part of the Dwarf2/3 standard yet, but a
11798 custom vendor extension. The main difference between a regular
11799 array and the vector variant is that vectors are passed by value
11800 to functions. */
e142c38c 11801 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11802 if (attr)
ea37ba09 11803 make_vector_type (type);
f5f8a009 11804
dbc98a8b
KW
11805 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11806 implementation may choose to implement triple vectors using this
11807 attribute. */
11808 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11809 if (attr)
11810 {
11811 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11812 TYPE_LENGTH (type) = DW_UNSND (attr);
11813 else
3e43a32a
MS
11814 complaint (&symfile_complaints,
11815 _("DW_AT_byte_size for array type smaller "
11816 "than the total size of elements"));
dbc98a8b
KW
11817 }
11818
39cbfefa
DJ
11819 name = dwarf2_name (die, cu);
11820 if (name)
11821 TYPE_NAME (type) = name;
6e70227d 11822
0963b4bd 11823 /* Install the type in the die. */
7e314c57
JK
11824 set_die_type (die, type, cu);
11825
11826 /* set_die_type should be already done. */
b4ba55a1
JB
11827 set_descriptive_type (type, die, cu);
11828
c906108c
SS
11829 do_cleanups (back_to);
11830
7e314c57 11831 return type;
c906108c
SS
11832}
11833
7ca2d3a3 11834static enum dwarf_array_dim_ordering
6e70227d 11835read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11836{
11837 struct attribute *attr;
11838
11839 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11840
11841 if (attr) return DW_SND (attr);
11842
0963b4bd
MS
11843 /* GNU F77 is a special case, as at 08/2004 array type info is the
11844 opposite order to the dwarf2 specification, but data is still
11845 laid out as per normal fortran.
7ca2d3a3 11846
0963b4bd
MS
11847 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11848 version checking. */
7ca2d3a3 11849
905e0470
PM
11850 if (cu->language == language_fortran
11851 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11852 {
11853 return DW_ORD_row_major;
11854 }
11855
6e70227d 11856 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11857 {
11858 case array_column_major:
11859 return DW_ORD_col_major;
11860 case array_row_major:
11861 default:
11862 return DW_ORD_row_major;
11863 };
11864}
11865
72019c9c 11866/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11867 the DIE's type field. */
72019c9c 11868
f792889a 11869static struct type *
72019c9c
GM
11870read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11871{
7e314c57
JK
11872 struct type *domain_type, *set_type;
11873 struct attribute *attr;
f792889a 11874
7e314c57
JK
11875 domain_type = die_type (die, cu);
11876
11877 /* The die_type call above may have already set the type for this DIE. */
11878 set_type = get_die_type (die, cu);
11879 if (set_type)
11880 return set_type;
11881
11882 set_type = create_set_type (NULL, domain_type);
11883
11884 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11885 if (attr)
11886 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11887
f792889a 11888 return set_die_type (die, set_type, cu);
72019c9c 11889}
7ca2d3a3 11890
0971de02
TT
11891/* A helper for read_common_block that creates a locexpr baton.
11892 SYM is the symbol which we are marking as computed.
11893 COMMON_DIE is the DIE for the common block.
11894 COMMON_LOC is the location expression attribute for the common
11895 block itself.
11896 MEMBER_LOC is the location expression attribute for the particular
11897 member of the common block that we are processing.
11898 CU is the CU from which the above come. */
11899
11900static void
11901mark_common_block_symbol_computed (struct symbol *sym,
11902 struct die_info *common_die,
11903 struct attribute *common_loc,
11904 struct attribute *member_loc,
11905 struct dwarf2_cu *cu)
11906{
11907 struct objfile *objfile = dwarf2_per_objfile->objfile;
11908 struct dwarf2_locexpr_baton *baton;
11909 gdb_byte *ptr;
11910 unsigned int cu_off;
11911 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11912 LONGEST offset = 0;
11913
11914 gdb_assert (common_loc && member_loc);
11915 gdb_assert (attr_form_is_block (common_loc));
11916 gdb_assert (attr_form_is_block (member_loc)
11917 || attr_form_is_constant (member_loc));
11918
11919 baton = obstack_alloc (&objfile->objfile_obstack,
11920 sizeof (struct dwarf2_locexpr_baton));
11921 baton->per_cu = cu->per_cu;
11922 gdb_assert (baton->per_cu);
11923
11924 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11925
11926 if (attr_form_is_constant (member_loc))
11927 {
11928 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11929 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11930 }
11931 else
11932 baton->size += DW_BLOCK (member_loc)->size;
11933
11934 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11935 baton->data = ptr;
11936
11937 *ptr++ = DW_OP_call4;
11938 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11939 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11940 ptr += 4;
11941
11942 if (attr_form_is_constant (member_loc))
11943 {
11944 *ptr++ = DW_OP_addr;
11945 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11946 ptr += cu->header.addr_size;
11947 }
11948 else
11949 {
11950 /* We have to copy the data here, because DW_OP_call4 will only
11951 use a DW_AT_location attribute. */
11952 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11953 ptr += DW_BLOCK (member_loc)->size;
11954 }
11955
11956 *ptr++ = DW_OP_plus;
11957 gdb_assert (ptr - baton->data == baton->size);
11958
11959 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11960 SYMBOL_LOCATION_BATON (sym) = baton;
11961 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11962}
11963
4357ac6c
TT
11964/* Create appropriate locally-scoped variables for all the
11965 DW_TAG_common_block entries. Also create a struct common_block
11966 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11967 is used to sepate the common blocks name namespace from regular
11968 variable names. */
c906108c
SS
11969
11970static void
e7c27a73 11971read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11972{
0971de02
TT
11973 struct attribute *attr;
11974
11975 attr = dwarf2_attr (die, DW_AT_location, cu);
11976 if (attr)
11977 {
11978 /* Support the .debug_loc offsets. */
11979 if (attr_form_is_block (attr))
11980 {
11981 /* Ok. */
11982 }
11983 else if (attr_form_is_section_offset (attr))
11984 {
11985 dwarf2_complex_location_expr_complaint ();
11986 attr = NULL;
11987 }
11988 else
11989 {
11990 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11991 "common block member");
11992 attr = NULL;
11993 }
11994 }
11995
639d11d3 11996 if (die->child != NULL)
c906108c 11997 {
4357ac6c
TT
11998 struct objfile *objfile = cu->objfile;
11999 struct die_info *child_die;
12000 size_t n_entries = 0, size;
12001 struct common_block *common_block;
12002 struct symbol *sym;
74ac6d43 12003
4357ac6c
TT
12004 for (child_die = die->child;
12005 child_die && child_die->tag;
12006 child_die = sibling_die (child_die))
12007 ++n_entries;
12008
12009 size = (sizeof (struct common_block)
12010 + (n_entries - 1) * sizeof (struct symbol *));
12011 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12012 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12013 common_block->n_entries = 0;
12014
12015 for (child_die = die->child;
12016 child_die && child_die->tag;
12017 child_die = sibling_die (child_die))
12018 {
12019 /* Create the symbol in the DW_TAG_common_block block in the current
12020 symbol scope. */
e7c27a73 12021 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12022 if (sym != NULL)
12023 {
12024 struct attribute *member_loc;
12025
12026 common_block->contents[common_block->n_entries++] = sym;
12027
12028 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12029 cu);
12030 if (member_loc)
12031 {
12032 /* GDB has handled this for a long time, but it is
12033 not specified by DWARF. It seems to have been
12034 emitted by gfortran at least as recently as:
12035 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12036 complaint (&symfile_complaints,
12037 _("Variable in common block has "
12038 "DW_AT_data_member_location "
12039 "- DIE at 0x%x [in module %s]"),
12040 child_die->offset.sect_off, cu->objfile->name);
12041
12042 if (attr_form_is_section_offset (member_loc))
12043 dwarf2_complex_location_expr_complaint ();
12044 else if (attr_form_is_constant (member_loc)
12045 || attr_form_is_block (member_loc))
12046 {
12047 if (attr)
12048 mark_common_block_symbol_computed (sym, die, attr,
12049 member_loc, cu);
12050 }
12051 else
12052 dwarf2_complex_location_expr_complaint ();
12053 }
12054 }
c906108c 12055 }
4357ac6c
TT
12056
12057 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12058 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12059 }
12060}
12061
0114d602 12062/* Create a type for a C++ namespace. */
d9fa45fe 12063
0114d602
DJ
12064static struct type *
12065read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12066{
e7c27a73 12067 struct objfile *objfile = cu->objfile;
0114d602 12068 const char *previous_prefix, *name;
9219021c 12069 int is_anonymous;
0114d602
DJ
12070 struct type *type;
12071
12072 /* For extensions, reuse the type of the original namespace. */
12073 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12074 {
12075 struct die_info *ext_die;
12076 struct dwarf2_cu *ext_cu = cu;
9a619af0 12077
0114d602
DJ
12078 ext_die = dwarf2_extension (die, &ext_cu);
12079 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12080
12081 /* EXT_CU may not be the same as CU.
12082 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12083 return set_die_type (die, type, cu);
12084 }
9219021c 12085
e142c38c 12086 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12087
12088 /* Now build the name of the current namespace. */
12089
0114d602
DJ
12090 previous_prefix = determine_prefix (die, cu);
12091 if (previous_prefix[0] != '\0')
12092 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12093 previous_prefix, name, 0, cu);
0114d602
DJ
12094
12095 /* Create the type. */
12096 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12097 objfile);
abee88f2 12098 TYPE_NAME (type) = name;
0114d602
DJ
12099 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12100
60531b24 12101 return set_die_type (die, type, cu);
0114d602
DJ
12102}
12103
12104/* Read a C++ namespace. */
12105
12106static void
12107read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12108{
12109 struct objfile *objfile = cu->objfile;
0114d602 12110 int is_anonymous;
9219021c 12111
5c4e30ca
DC
12112 /* Add a symbol associated to this if we haven't seen the namespace
12113 before. Also, add a using directive if it's an anonymous
12114 namespace. */
9219021c 12115
f2f0e013 12116 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12117 {
12118 struct type *type;
12119
0114d602 12120 type = read_type_die (die, cu);
e7c27a73 12121 new_symbol (die, type, cu);
5c4e30ca 12122
e8e80198 12123 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12124 if (is_anonymous)
0114d602
DJ
12125 {
12126 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12127
c0cc3a76 12128 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12129 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12130 }
5c4e30ca 12131 }
9219021c 12132
639d11d3 12133 if (die->child != NULL)
d9fa45fe 12134 {
639d11d3 12135 struct die_info *child_die = die->child;
6e70227d 12136
d9fa45fe
DC
12137 while (child_die && child_die->tag)
12138 {
e7c27a73 12139 process_die (child_die, cu);
d9fa45fe
DC
12140 child_die = sibling_die (child_die);
12141 }
12142 }
38d518c9
EZ
12143}
12144
f55ee35c
JK
12145/* Read a Fortran module as type. This DIE can be only a declaration used for
12146 imported module. Still we need that type as local Fortran "use ... only"
12147 declaration imports depend on the created type in determine_prefix. */
12148
12149static struct type *
12150read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12151{
12152 struct objfile *objfile = cu->objfile;
15d034d0 12153 const char *module_name;
f55ee35c
JK
12154 struct type *type;
12155
12156 module_name = dwarf2_name (die, cu);
12157 if (!module_name)
3e43a32a
MS
12158 complaint (&symfile_complaints,
12159 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12160 die->offset.sect_off);
f55ee35c
JK
12161 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12162
12163 /* determine_prefix uses TYPE_TAG_NAME. */
12164 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12165
12166 return set_die_type (die, type, cu);
12167}
12168
5d7cb8df
JK
12169/* Read a Fortran module. */
12170
12171static void
12172read_module (struct die_info *die, struct dwarf2_cu *cu)
12173{
12174 struct die_info *child_die = die->child;
12175
5d7cb8df
JK
12176 while (child_die && child_die->tag)
12177 {
12178 process_die (child_die, cu);
12179 child_die = sibling_die (child_die);
12180 }
12181}
12182
38d518c9
EZ
12183/* Return the name of the namespace represented by DIE. Set
12184 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12185 namespace. */
12186
12187static const char *
e142c38c 12188namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12189{
12190 struct die_info *current_die;
12191 const char *name = NULL;
12192
12193 /* Loop through the extensions until we find a name. */
12194
12195 for (current_die = die;
12196 current_die != NULL;
f2f0e013 12197 current_die = dwarf2_extension (die, &cu))
38d518c9 12198 {
e142c38c 12199 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12200 if (name != NULL)
12201 break;
12202 }
12203
12204 /* Is it an anonymous namespace? */
12205
12206 *is_anonymous = (name == NULL);
12207 if (*is_anonymous)
2b1dbab0 12208 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12209
12210 return name;
d9fa45fe
DC
12211}
12212
c906108c
SS
12213/* Extract all information from a DW_TAG_pointer_type DIE and add to
12214 the user defined type vector. */
12215
f792889a 12216static struct type *
e7c27a73 12217read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12218{
5e2b427d 12219 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12220 struct comp_unit_head *cu_header = &cu->header;
c906108c 12221 struct type *type;
8b2dbe47
KB
12222 struct attribute *attr_byte_size;
12223 struct attribute *attr_address_class;
12224 int byte_size, addr_class;
7e314c57
JK
12225 struct type *target_type;
12226
12227 target_type = die_type (die, cu);
c906108c 12228
7e314c57
JK
12229 /* The die_type call above may have already set the type for this DIE. */
12230 type = get_die_type (die, cu);
12231 if (type)
12232 return type;
12233
12234 type = lookup_pointer_type (target_type);
8b2dbe47 12235
e142c38c 12236 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12237 if (attr_byte_size)
12238 byte_size = DW_UNSND (attr_byte_size);
c906108c 12239 else
8b2dbe47
KB
12240 byte_size = cu_header->addr_size;
12241
e142c38c 12242 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12243 if (attr_address_class)
12244 addr_class = DW_UNSND (attr_address_class);
12245 else
12246 addr_class = DW_ADDR_none;
12247
12248 /* If the pointer size or address class is different than the
12249 default, create a type variant marked as such and set the
12250 length accordingly. */
12251 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12252 {
5e2b427d 12253 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12254 {
12255 int type_flags;
12256
849957d9 12257 type_flags = gdbarch_address_class_type_flags
5e2b427d 12258 (gdbarch, byte_size, addr_class);
876cecd0
TT
12259 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12260 == 0);
8b2dbe47
KB
12261 type = make_type_with_address_space (type, type_flags);
12262 }
12263 else if (TYPE_LENGTH (type) != byte_size)
12264 {
3e43a32a
MS
12265 complaint (&symfile_complaints,
12266 _("invalid pointer size %d"), byte_size);
8b2dbe47 12267 }
6e70227d 12268 else
9a619af0
MS
12269 {
12270 /* Should we also complain about unhandled address classes? */
12271 }
c906108c 12272 }
8b2dbe47
KB
12273
12274 TYPE_LENGTH (type) = byte_size;
f792889a 12275 return set_die_type (die, type, cu);
c906108c
SS
12276}
12277
12278/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12279 the user defined type vector. */
12280
f792889a 12281static struct type *
e7c27a73 12282read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12283{
12284 struct type *type;
12285 struct type *to_type;
12286 struct type *domain;
12287
e7c27a73
DJ
12288 to_type = die_type (die, cu);
12289 domain = die_containing_type (die, cu);
0d5de010 12290
7e314c57
JK
12291 /* The calls above may have already set the type for this DIE. */
12292 type = get_die_type (die, cu);
12293 if (type)
12294 return type;
12295
0d5de010
DJ
12296 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12297 type = lookup_methodptr_type (to_type);
7078baeb
TT
12298 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12299 {
12300 struct type *new_type = alloc_type (cu->objfile);
12301
12302 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12303 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12304 TYPE_VARARGS (to_type));
12305 type = lookup_methodptr_type (new_type);
12306 }
0d5de010
DJ
12307 else
12308 type = lookup_memberptr_type (to_type, domain);
c906108c 12309
f792889a 12310 return set_die_type (die, type, cu);
c906108c
SS
12311}
12312
12313/* Extract all information from a DW_TAG_reference_type DIE and add to
12314 the user defined type vector. */
12315
f792889a 12316static struct type *
e7c27a73 12317read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12318{
e7c27a73 12319 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12320 struct type *type, *target_type;
c906108c
SS
12321 struct attribute *attr;
12322
7e314c57
JK
12323 target_type = die_type (die, cu);
12324
12325 /* The die_type call above may have already set the type for this DIE. */
12326 type = get_die_type (die, cu);
12327 if (type)
12328 return type;
12329
12330 type = lookup_reference_type (target_type);
e142c38c 12331 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12332 if (attr)
12333 {
12334 TYPE_LENGTH (type) = DW_UNSND (attr);
12335 }
12336 else
12337 {
107d2387 12338 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12339 }
f792889a 12340 return set_die_type (die, type, cu);
c906108c
SS
12341}
12342
f792889a 12343static struct type *
e7c27a73 12344read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12345{
f792889a 12346 struct type *base_type, *cv_type;
c906108c 12347
e7c27a73 12348 base_type = die_type (die, cu);
7e314c57
JK
12349
12350 /* The die_type call above may have already set the type for this DIE. */
12351 cv_type = get_die_type (die, cu);
12352 if (cv_type)
12353 return cv_type;
12354
2f608a3a
KW
12355 /* In case the const qualifier is applied to an array type, the element type
12356 is so qualified, not the array type (section 6.7.3 of C99). */
12357 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12358 {
12359 struct type *el_type, *inner_array;
12360
12361 base_type = copy_type (base_type);
12362 inner_array = base_type;
12363
12364 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12365 {
12366 TYPE_TARGET_TYPE (inner_array) =
12367 copy_type (TYPE_TARGET_TYPE (inner_array));
12368 inner_array = TYPE_TARGET_TYPE (inner_array);
12369 }
12370
12371 el_type = TYPE_TARGET_TYPE (inner_array);
12372 TYPE_TARGET_TYPE (inner_array) =
12373 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12374
12375 return set_die_type (die, base_type, cu);
12376 }
12377
f792889a
DJ
12378 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12379 return set_die_type (die, cv_type, cu);
c906108c
SS
12380}
12381
f792889a 12382static struct type *
e7c27a73 12383read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12384{
f792889a 12385 struct type *base_type, *cv_type;
c906108c 12386
e7c27a73 12387 base_type = die_type (die, cu);
7e314c57
JK
12388
12389 /* The die_type call above may have already set the type for this DIE. */
12390 cv_type = get_die_type (die, cu);
12391 if (cv_type)
12392 return cv_type;
12393
f792889a
DJ
12394 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12395 return set_die_type (die, cv_type, cu);
c906108c
SS
12396}
12397
06d66ee9
TT
12398/* Handle DW_TAG_restrict_type. */
12399
12400static struct type *
12401read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12402{
12403 struct type *base_type, *cv_type;
12404
12405 base_type = die_type (die, cu);
12406
12407 /* The die_type call above may have already set the type for this DIE. */
12408 cv_type = get_die_type (die, cu);
12409 if (cv_type)
12410 return cv_type;
12411
12412 cv_type = make_restrict_type (base_type);
12413 return set_die_type (die, cv_type, cu);
12414}
12415
c906108c
SS
12416/* Extract all information from a DW_TAG_string_type DIE and add to
12417 the user defined type vector. It isn't really a user defined type,
12418 but it behaves like one, with other DIE's using an AT_user_def_type
12419 attribute to reference it. */
12420
f792889a 12421static struct type *
e7c27a73 12422read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12423{
e7c27a73 12424 struct objfile *objfile = cu->objfile;
3b7538c0 12425 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12426 struct type *type, *range_type, *index_type, *char_type;
12427 struct attribute *attr;
12428 unsigned int length;
12429
e142c38c 12430 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12431 if (attr)
12432 {
12433 length = DW_UNSND (attr);
12434 }
12435 else
12436 {
0963b4bd 12437 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12438 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12439 if (attr)
12440 {
12441 length = DW_UNSND (attr);
12442 }
12443 else
12444 {
12445 length = 1;
12446 }
c906108c 12447 }
6ccb9162 12448
46bf5051 12449 index_type = objfile_type (objfile)->builtin_int;
c906108c 12450 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12451 char_type = language_string_char_type (cu->language_defn, gdbarch);
12452 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12453
f792889a 12454 return set_die_type (die, type, cu);
c906108c
SS
12455}
12456
12457/* Handle DIES due to C code like:
12458
12459 struct foo
c5aa993b
JM
12460 {
12461 int (*funcp)(int a, long l);
12462 int b;
12463 };
c906108c 12464
0963b4bd 12465 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12466
f792889a 12467static struct type *
e7c27a73 12468read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12469{
bb5ed363 12470 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12471 struct type *type; /* Type that this function returns. */
12472 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12473 struct attribute *attr;
12474
e7c27a73 12475 type = die_type (die, cu);
7e314c57
JK
12476
12477 /* The die_type call above may have already set the type for this DIE. */
12478 ftype = get_die_type (die, cu);
12479 if (ftype)
12480 return ftype;
12481
0c8b41f1 12482 ftype = lookup_function_type (type);
c906108c 12483
5b8101ae 12484 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12485 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12486 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12487 || cu->language == language_cplus
5b8101ae
PM
12488 || cu->language == language_java
12489 || cu->language == language_pascal)
876cecd0 12490 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12491 else if (producer_is_realview (cu->producer))
12492 /* RealView does not emit DW_AT_prototyped. We can not
12493 distinguish prototyped and unprototyped functions; default to
12494 prototyped, since that is more common in modern code (and
12495 RealView warns about unprototyped functions). */
12496 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12497
c055b101
CV
12498 /* Store the calling convention in the type if it's available in
12499 the subroutine die. Otherwise set the calling convention to
12500 the default value DW_CC_normal. */
12501 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12502 if (attr)
12503 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12504 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12505 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12506 else
12507 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12508
12509 /* We need to add the subroutine type to the die immediately so
12510 we don't infinitely recurse when dealing with parameters
0963b4bd 12511 declared as the same subroutine type. */
76c10ea2 12512 set_die_type (die, ftype, cu);
6e70227d 12513
639d11d3 12514 if (die->child != NULL)
c906108c 12515 {
bb5ed363 12516 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12517 struct die_info *child_die;
8072405b 12518 int nparams, iparams;
c906108c
SS
12519
12520 /* Count the number of parameters.
12521 FIXME: GDB currently ignores vararg functions, but knows about
12522 vararg member functions. */
8072405b 12523 nparams = 0;
639d11d3 12524 child_die = die->child;
c906108c
SS
12525 while (child_die && child_die->tag)
12526 {
12527 if (child_die->tag == DW_TAG_formal_parameter)
12528 nparams++;
12529 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12530 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12531 child_die = sibling_die (child_die);
12532 }
12533
12534 /* Allocate storage for parameters and fill them in. */
12535 TYPE_NFIELDS (ftype) = nparams;
12536 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12537 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12538
8072405b
JK
12539 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12540 even if we error out during the parameters reading below. */
12541 for (iparams = 0; iparams < nparams; iparams++)
12542 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12543
12544 iparams = 0;
639d11d3 12545 child_die = die->child;
c906108c
SS
12546 while (child_die && child_die->tag)
12547 {
12548 if (child_die->tag == DW_TAG_formal_parameter)
12549 {
3ce3b1ba
PA
12550 struct type *arg_type;
12551
12552 /* DWARF version 2 has no clean way to discern C++
12553 static and non-static member functions. G++ helps
12554 GDB by marking the first parameter for non-static
12555 member functions (which is the this pointer) as
12556 artificial. We pass this information to
12557 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12558
12559 DWARF version 3 added DW_AT_object_pointer, which GCC
12560 4.5 does not yet generate. */
e142c38c 12561 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12562 if (attr)
12563 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12564 else
418835cc
KS
12565 {
12566 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12567
12568 /* GCC/43521: In java, the formal parameter
12569 "this" is sometimes not marked with DW_AT_artificial. */
12570 if (cu->language == language_java)
12571 {
12572 const char *name = dwarf2_name (child_die, cu);
9a619af0 12573
418835cc
KS
12574 if (name && !strcmp (name, "this"))
12575 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12576 }
12577 }
3ce3b1ba
PA
12578 arg_type = die_type (child_die, cu);
12579
12580 /* RealView does not mark THIS as const, which the testsuite
12581 expects. GCC marks THIS as const in method definitions,
12582 but not in the class specifications (GCC PR 43053). */
12583 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12584 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12585 {
12586 int is_this = 0;
12587 struct dwarf2_cu *arg_cu = cu;
12588 const char *name = dwarf2_name (child_die, cu);
12589
12590 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12591 if (attr)
12592 {
12593 /* If the compiler emits this, use it. */
12594 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12595 is_this = 1;
12596 }
12597 else if (name && strcmp (name, "this") == 0)
12598 /* Function definitions will have the argument names. */
12599 is_this = 1;
12600 else if (name == NULL && iparams == 0)
12601 /* Declarations may not have the names, so like
12602 elsewhere in GDB, assume an artificial first
12603 argument is "this". */
12604 is_this = 1;
12605
12606 if (is_this)
12607 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12608 arg_type, 0);
12609 }
12610
12611 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12612 iparams++;
12613 }
12614 child_die = sibling_die (child_die);
12615 }
12616 }
12617
76c10ea2 12618 return ftype;
c906108c
SS
12619}
12620
f792889a 12621static struct type *
e7c27a73 12622read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12623{
e7c27a73 12624 struct objfile *objfile = cu->objfile;
0114d602 12625 const char *name = NULL;
3c8e0968 12626 struct type *this_type, *target_type;
c906108c 12627
94af9270 12628 name = dwarf2_full_name (NULL, die, cu);
f792889a 12629 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12630 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12631 TYPE_NAME (this_type) = name;
f792889a 12632 set_die_type (die, this_type, cu);
3c8e0968
DE
12633 target_type = die_type (die, cu);
12634 if (target_type != this_type)
12635 TYPE_TARGET_TYPE (this_type) = target_type;
12636 else
12637 {
12638 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12639 spec and cause infinite loops in GDB. */
12640 complaint (&symfile_complaints,
12641 _("Self-referential DW_TAG_typedef "
12642 "- DIE at 0x%x [in module %s]"),
b64f50a1 12643 die->offset.sect_off, objfile->name);
3c8e0968
DE
12644 TYPE_TARGET_TYPE (this_type) = NULL;
12645 }
f792889a 12646 return this_type;
c906108c
SS
12647}
12648
12649/* Find a representation of a given base type and install
12650 it in the TYPE field of the die. */
12651
f792889a 12652static struct type *
e7c27a73 12653read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12654{
e7c27a73 12655 struct objfile *objfile = cu->objfile;
c906108c
SS
12656 struct type *type;
12657 struct attribute *attr;
12658 int encoding = 0, size = 0;
15d034d0 12659 const char *name;
6ccb9162
UW
12660 enum type_code code = TYPE_CODE_INT;
12661 int type_flags = 0;
12662 struct type *target_type = NULL;
c906108c 12663
e142c38c 12664 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12665 if (attr)
12666 {
12667 encoding = DW_UNSND (attr);
12668 }
e142c38c 12669 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12670 if (attr)
12671 {
12672 size = DW_UNSND (attr);
12673 }
39cbfefa 12674 name = dwarf2_name (die, cu);
6ccb9162 12675 if (!name)
c906108c 12676 {
6ccb9162
UW
12677 complaint (&symfile_complaints,
12678 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12679 }
6ccb9162
UW
12680
12681 switch (encoding)
c906108c 12682 {
6ccb9162
UW
12683 case DW_ATE_address:
12684 /* Turn DW_ATE_address into a void * pointer. */
12685 code = TYPE_CODE_PTR;
12686 type_flags |= TYPE_FLAG_UNSIGNED;
12687 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12688 break;
12689 case DW_ATE_boolean:
12690 code = TYPE_CODE_BOOL;
12691 type_flags |= TYPE_FLAG_UNSIGNED;
12692 break;
12693 case DW_ATE_complex_float:
12694 code = TYPE_CODE_COMPLEX;
12695 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12696 break;
12697 case DW_ATE_decimal_float:
12698 code = TYPE_CODE_DECFLOAT;
12699 break;
12700 case DW_ATE_float:
12701 code = TYPE_CODE_FLT;
12702 break;
12703 case DW_ATE_signed:
12704 break;
12705 case DW_ATE_unsigned:
12706 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12707 if (cu->language == language_fortran
12708 && name
12709 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12710 code = TYPE_CODE_CHAR;
6ccb9162
UW
12711 break;
12712 case DW_ATE_signed_char:
6e70227d 12713 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12714 || cu->language == language_pascal
12715 || cu->language == language_fortran)
6ccb9162
UW
12716 code = TYPE_CODE_CHAR;
12717 break;
12718 case DW_ATE_unsigned_char:
868a0084 12719 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12720 || cu->language == language_pascal
12721 || cu->language == language_fortran)
6ccb9162
UW
12722 code = TYPE_CODE_CHAR;
12723 type_flags |= TYPE_FLAG_UNSIGNED;
12724 break;
75079b2b
TT
12725 case DW_ATE_UTF:
12726 /* We just treat this as an integer and then recognize the
12727 type by name elsewhere. */
12728 break;
12729
6ccb9162
UW
12730 default:
12731 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12732 dwarf_type_encoding_name (encoding));
12733 break;
c906108c 12734 }
6ccb9162 12735
0114d602
DJ
12736 type = init_type (code, size, type_flags, NULL, objfile);
12737 TYPE_NAME (type) = name;
6ccb9162
UW
12738 TYPE_TARGET_TYPE (type) = target_type;
12739
0114d602 12740 if (name && strcmp (name, "char") == 0)
876cecd0 12741 TYPE_NOSIGN (type) = 1;
0114d602 12742
f792889a 12743 return set_die_type (die, type, cu);
c906108c
SS
12744}
12745
a02abb62
JB
12746/* Read the given DW_AT_subrange DIE. */
12747
f792889a 12748static struct type *
a02abb62
JB
12749read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12750{
4c9ad8c2 12751 struct type *base_type, *orig_base_type;
a02abb62
JB
12752 struct type *range_type;
12753 struct attribute *attr;
4fae6e18
JK
12754 LONGEST low, high;
12755 int low_default_is_valid;
15d034d0 12756 const char *name;
43bbcdc2 12757 LONGEST negative_mask;
e77813c8 12758
4c9ad8c2
TT
12759 orig_base_type = die_type (die, cu);
12760 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12761 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12762 creating the range type, but we use the result of check_typedef
12763 when examining properties of the type. */
12764 base_type = check_typedef (orig_base_type);
a02abb62 12765
7e314c57
JK
12766 /* The die_type call above may have already set the type for this DIE. */
12767 range_type = get_die_type (die, cu);
12768 if (range_type)
12769 return range_type;
12770
4fae6e18
JK
12771 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12772 omitting DW_AT_lower_bound. */
12773 switch (cu->language)
6e70227d 12774 {
4fae6e18
JK
12775 case language_c:
12776 case language_cplus:
12777 low = 0;
12778 low_default_is_valid = 1;
12779 break;
12780 case language_fortran:
12781 low = 1;
12782 low_default_is_valid = 1;
12783 break;
12784 case language_d:
12785 case language_java:
12786 case language_objc:
12787 low = 0;
12788 low_default_is_valid = (cu->header.version >= 4);
12789 break;
12790 case language_ada:
12791 case language_m2:
12792 case language_pascal:
a02abb62 12793 low = 1;
4fae6e18
JK
12794 low_default_is_valid = (cu->header.version >= 4);
12795 break;
12796 default:
12797 low = 0;
12798 low_default_is_valid = 0;
12799 break;
a02abb62
JB
12800 }
12801
dd5e6932
DJ
12802 /* FIXME: For variable sized arrays either of these could be
12803 a variable rather than a constant value. We'll allow it,
12804 but we don't know how to handle it. */
e142c38c 12805 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12806 if (attr)
4fae6e18
JK
12807 low = dwarf2_get_attr_constant_value (attr, low);
12808 else if (!low_default_is_valid)
12809 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12810 "- DIE at 0x%x [in module %s]"),
12811 die->offset.sect_off, cu->objfile->name);
a02abb62 12812
e142c38c 12813 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12814 if (attr)
6e70227d 12815 {
d48323d8 12816 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12817 {
12818 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12819 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12820 FIXME: GDB does not yet know how to handle dynamic
12821 arrays properly, treat them as arrays with unspecified
12822 length for now.
12823
12824 FIXME: jimb/2003-09-22: GDB does not really know
12825 how to handle arrays of unspecified length
12826 either; we just represent them as zero-length
12827 arrays. Choose an appropriate upper bound given
12828 the lower bound we've computed above. */
12829 high = low - 1;
12830 }
12831 else
12832 high = dwarf2_get_attr_constant_value (attr, 1);
12833 }
e77813c8
PM
12834 else
12835 {
12836 attr = dwarf2_attr (die, DW_AT_count, cu);
12837 if (attr)
12838 {
12839 int count = dwarf2_get_attr_constant_value (attr, 1);
12840 high = low + count - 1;
12841 }
c2ff108b
JK
12842 else
12843 {
12844 /* Unspecified array length. */
12845 high = low - 1;
12846 }
e77813c8
PM
12847 }
12848
12849 /* Dwarf-2 specifications explicitly allows to create subrange types
12850 without specifying a base type.
12851 In that case, the base type must be set to the type of
12852 the lower bound, upper bound or count, in that order, if any of these
12853 three attributes references an object that has a type.
12854 If no base type is found, the Dwarf-2 specifications say that
12855 a signed integer type of size equal to the size of an address should
12856 be used.
12857 For the following C code: `extern char gdb_int [];'
12858 GCC produces an empty range DIE.
12859 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12860 high bound or count are not yet handled by this code. */
e77813c8
PM
12861 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12862 {
12863 struct objfile *objfile = cu->objfile;
12864 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12865 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12866 struct type *int_type = objfile_type (objfile)->builtin_int;
12867
12868 /* Test "int", "long int", and "long long int" objfile types,
12869 and select the first one having a size above or equal to the
12870 architecture address size. */
12871 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12872 base_type = int_type;
12873 else
12874 {
12875 int_type = objfile_type (objfile)->builtin_long;
12876 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12877 base_type = int_type;
12878 else
12879 {
12880 int_type = objfile_type (objfile)->builtin_long_long;
12881 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12882 base_type = int_type;
12883 }
12884 }
12885 }
a02abb62 12886
6e70227d 12887 negative_mask =
43bbcdc2
PH
12888 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12889 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12890 low |= negative_mask;
12891 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12892 high |= negative_mask;
12893
4c9ad8c2 12894 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 12895
bbb0eef6
JK
12896 /* Mark arrays with dynamic length at least as an array of unspecified
12897 length. GDB could check the boundary but before it gets implemented at
12898 least allow accessing the array elements. */
d48323d8 12899 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12900 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12901
c2ff108b
JK
12902 /* Ada expects an empty array on no boundary attributes. */
12903 if (attr == NULL && cu->language != language_ada)
12904 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12905
39cbfefa
DJ
12906 name = dwarf2_name (die, cu);
12907 if (name)
12908 TYPE_NAME (range_type) = name;
6e70227d 12909
e142c38c 12910 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12911 if (attr)
12912 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12913
7e314c57
JK
12914 set_die_type (die, range_type, cu);
12915
12916 /* set_die_type should be already done. */
b4ba55a1
JB
12917 set_descriptive_type (range_type, die, cu);
12918
7e314c57 12919 return range_type;
a02abb62 12920}
6e70227d 12921
f792889a 12922static struct type *
81a17f79
JB
12923read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12924{
12925 struct type *type;
81a17f79 12926
81a17f79
JB
12927 /* For now, we only support the C meaning of an unspecified type: void. */
12928
0114d602
DJ
12929 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12930 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12931
f792889a 12932 return set_die_type (die, type, cu);
81a17f79 12933}
a02abb62 12934
639d11d3
DC
12935/* Read a single die and all its descendents. Set the die's sibling
12936 field to NULL; set other fields in the die correctly, and set all
12937 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12938 location of the info_ptr after reading all of those dies. PARENT
12939 is the parent of the die in question. */
12940
12941static struct die_info *
dee91e82
DE
12942read_die_and_children (const struct die_reader_specs *reader,
12943 gdb_byte *info_ptr,
12944 gdb_byte **new_info_ptr,
12945 struct die_info *parent)
639d11d3
DC
12946{
12947 struct die_info *die;
fe1b8b76 12948 gdb_byte *cur_ptr;
639d11d3
DC
12949 int has_children;
12950
93311388 12951 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12952 if (die == NULL)
12953 {
12954 *new_info_ptr = cur_ptr;
12955 return NULL;
12956 }
93311388 12957 store_in_ref_table (die, reader->cu);
639d11d3
DC
12958
12959 if (has_children)
348e048f 12960 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12961 else
12962 {
12963 die->child = NULL;
12964 *new_info_ptr = cur_ptr;
12965 }
12966
12967 die->sibling = NULL;
12968 die->parent = parent;
12969 return die;
12970}
12971
12972/* Read a die, all of its descendents, and all of its siblings; set
12973 all of the fields of all of the dies correctly. Arguments are as
12974 in read_die_and_children. */
12975
12976static struct die_info *
93311388
DE
12977read_die_and_siblings (const struct die_reader_specs *reader,
12978 gdb_byte *info_ptr,
fe1b8b76 12979 gdb_byte **new_info_ptr,
639d11d3
DC
12980 struct die_info *parent)
12981{
12982 struct die_info *first_die, *last_sibling;
fe1b8b76 12983 gdb_byte *cur_ptr;
639d11d3 12984
c906108c 12985 cur_ptr = info_ptr;
639d11d3
DC
12986 first_die = last_sibling = NULL;
12987
12988 while (1)
c906108c 12989 {
639d11d3 12990 struct die_info *die
dee91e82 12991 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12992
1d325ec1 12993 if (die == NULL)
c906108c 12994 {
639d11d3
DC
12995 *new_info_ptr = cur_ptr;
12996 return first_die;
c906108c 12997 }
1d325ec1
DJ
12998
12999 if (!first_die)
13000 first_die = die;
c906108c 13001 else
1d325ec1
DJ
13002 last_sibling->sibling = die;
13003
13004 last_sibling = die;
c906108c 13005 }
c906108c
SS
13006}
13007
3019eac3
DE
13008/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13009 attributes.
13010 The caller is responsible for filling in the extra attributes
13011 and updating (*DIEP)->num_attrs.
13012 Set DIEP to point to a newly allocated die with its information,
13013 except for its child, sibling, and parent fields.
13014 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
13015
13016static gdb_byte *
3019eac3
DE
13017read_full_die_1 (const struct die_reader_specs *reader,
13018 struct die_info **diep, gdb_byte *info_ptr,
13019 int *has_children, int num_extra_attrs)
93311388 13020{
b64f50a1
JK
13021 unsigned int abbrev_number, bytes_read, i;
13022 sect_offset offset;
93311388
DE
13023 struct abbrev_info *abbrev;
13024 struct die_info *die;
13025 struct dwarf2_cu *cu = reader->cu;
13026 bfd *abfd = reader->abfd;
13027
b64f50a1 13028 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13029 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13030 info_ptr += bytes_read;
13031 if (!abbrev_number)
13032 {
13033 *diep = NULL;
13034 *has_children = 0;
13035 return info_ptr;
13036 }
13037
433df2d4 13038 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13039 if (!abbrev)
348e048f
DE
13040 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13041 abbrev_number,
13042 bfd_get_filename (abfd));
13043
3019eac3 13044 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13045 die->offset = offset;
13046 die->tag = abbrev->tag;
13047 die->abbrev = abbrev_number;
13048
3019eac3
DE
13049 /* Make the result usable.
13050 The caller needs to update num_attrs after adding the extra
13051 attributes. */
93311388
DE
13052 die->num_attrs = abbrev->num_attrs;
13053
13054 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13055 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13056 info_ptr);
93311388
DE
13057
13058 *diep = die;
13059 *has_children = abbrev->has_children;
13060 return info_ptr;
13061}
13062
3019eac3
DE
13063/* Read a die and all its attributes.
13064 Set DIEP to point to a newly allocated die with its information,
13065 except for its child, sibling, and parent fields.
13066 Set HAS_CHILDREN to tell whether the die has children or not. */
13067
13068static gdb_byte *
13069read_full_die (const struct die_reader_specs *reader,
13070 struct die_info **diep, gdb_byte *info_ptr,
13071 int *has_children)
13072{
13073 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13074}
433df2d4
DE
13075\f
13076/* Abbreviation tables.
3019eac3 13077
433df2d4 13078 In DWARF version 2, the description of the debugging information is
c906108c
SS
13079 stored in a separate .debug_abbrev section. Before we read any
13080 dies from a section we read in all abbreviations and install them
433df2d4
DE
13081 in a hash table. */
13082
13083/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13084
13085static struct abbrev_info *
13086abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13087{
13088 struct abbrev_info *abbrev;
13089
13090 abbrev = (struct abbrev_info *)
13091 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13092 memset (abbrev, 0, sizeof (struct abbrev_info));
13093 return abbrev;
13094}
13095
13096/* Add an abbreviation to the table. */
c906108c
SS
13097
13098static void
433df2d4
DE
13099abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13100 unsigned int abbrev_number,
13101 struct abbrev_info *abbrev)
13102{
13103 unsigned int hash_number;
13104
13105 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13106 abbrev->next = abbrev_table->abbrevs[hash_number];
13107 abbrev_table->abbrevs[hash_number] = abbrev;
13108}
dee91e82 13109
433df2d4
DE
13110/* Look up an abbrev in the table.
13111 Returns NULL if the abbrev is not found. */
13112
13113static struct abbrev_info *
13114abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13115 unsigned int abbrev_number)
c906108c 13116{
433df2d4
DE
13117 unsigned int hash_number;
13118 struct abbrev_info *abbrev;
13119
13120 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13121 abbrev = abbrev_table->abbrevs[hash_number];
13122
13123 while (abbrev)
13124 {
13125 if (abbrev->number == abbrev_number)
13126 return abbrev;
13127 abbrev = abbrev->next;
13128 }
13129 return NULL;
13130}
13131
13132/* Read in an abbrev table. */
13133
13134static struct abbrev_table *
13135abbrev_table_read_table (struct dwarf2_section_info *section,
13136 sect_offset offset)
13137{
13138 struct objfile *objfile = dwarf2_per_objfile->objfile;
13139 bfd *abfd = section->asection->owner;
13140 struct abbrev_table *abbrev_table;
fe1b8b76 13141 gdb_byte *abbrev_ptr;
c906108c
SS
13142 struct abbrev_info *cur_abbrev;
13143 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13144 unsigned int abbrev_form;
f3dd6933
DJ
13145 struct attr_abbrev *cur_attrs;
13146 unsigned int allocated_attrs;
c906108c 13147
433df2d4 13148 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13149 abbrev_table->offset = offset;
433df2d4
DE
13150 obstack_init (&abbrev_table->abbrev_obstack);
13151 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13152 (ABBREV_HASH_SIZE
13153 * sizeof (struct abbrev_info *)));
13154 memset (abbrev_table->abbrevs, 0,
13155 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13156
433df2d4
DE
13157 dwarf2_read_section (objfile, section);
13158 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13159 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13160 abbrev_ptr += bytes_read;
13161
f3dd6933
DJ
13162 allocated_attrs = ATTR_ALLOC_CHUNK;
13163 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13164
0963b4bd 13165 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13166 while (abbrev_number)
13167 {
433df2d4 13168 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13169
13170 /* read in abbrev header */
13171 cur_abbrev->number = abbrev_number;
13172 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13173 abbrev_ptr += bytes_read;
13174 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13175 abbrev_ptr += 1;
13176
13177 /* now read in declarations */
13178 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13179 abbrev_ptr += bytes_read;
13180 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13181 abbrev_ptr += bytes_read;
13182 while (abbrev_name)
13183 {
f3dd6933 13184 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13185 {
f3dd6933
DJ
13186 allocated_attrs += ATTR_ALLOC_CHUNK;
13187 cur_attrs
13188 = xrealloc (cur_attrs, (allocated_attrs
13189 * sizeof (struct attr_abbrev)));
c906108c 13190 }
ae038cb0 13191
f3dd6933
DJ
13192 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13193 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13194 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13195 abbrev_ptr += bytes_read;
13196 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13197 abbrev_ptr += bytes_read;
13198 }
13199
433df2d4 13200 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13201 (cur_abbrev->num_attrs
13202 * sizeof (struct attr_abbrev)));
13203 memcpy (cur_abbrev->attrs, cur_attrs,
13204 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13205
433df2d4 13206 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13207
13208 /* Get next abbreviation.
13209 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13210 always properly terminated with an abbrev number of 0.
13211 Exit loop if we encounter an abbreviation which we have
13212 already read (which means we are about to read the abbreviations
13213 for the next compile unit) or if the end of the abbreviation
13214 table is reached. */
433df2d4 13215 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13216 break;
13217 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13218 abbrev_ptr += bytes_read;
433df2d4 13219 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13220 break;
13221 }
f3dd6933
DJ
13222
13223 xfree (cur_attrs);
433df2d4 13224 return abbrev_table;
c906108c
SS
13225}
13226
433df2d4 13227/* Free the resources held by ABBREV_TABLE. */
c906108c 13228
c906108c 13229static void
433df2d4 13230abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13231{
433df2d4
DE
13232 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13233 xfree (abbrev_table);
c906108c
SS
13234}
13235
f4dc4d17
DE
13236/* Same as abbrev_table_free but as a cleanup.
13237 We pass in a pointer to the pointer to the table so that we can
13238 set the pointer to NULL when we're done. It also simplifies
13239 build_type_unit_groups. */
13240
13241static void
13242abbrev_table_free_cleanup (void *table_ptr)
13243{
13244 struct abbrev_table **abbrev_table_ptr = table_ptr;
13245
13246 if (*abbrev_table_ptr != NULL)
13247 abbrev_table_free (*abbrev_table_ptr);
13248 *abbrev_table_ptr = NULL;
13249}
13250
433df2d4
DE
13251/* Read the abbrev table for CU from ABBREV_SECTION. */
13252
13253static void
13254dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13255 struct dwarf2_section_info *abbrev_section)
c906108c 13256{
433df2d4
DE
13257 cu->abbrev_table =
13258 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13259}
c906108c 13260
433df2d4 13261/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13262
433df2d4
DE
13263static void
13264dwarf2_free_abbrev_table (void *ptr_to_cu)
13265{
13266 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13267
433df2d4
DE
13268 abbrev_table_free (cu->abbrev_table);
13269 /* Set this to NULL so that we SEGV if we try to read it later,
13270 and also because free_comp_unit verifies this is NULL. */
13271 cu->abbrev_table = NULL;
13272}
13273\f
72bf9492
DJ
13274/* Returns nonzero if TAG represents a type that we might generate a partial
13275 symbol for. */
13276
13277static int
13278is_type_tag_for_partial (int tag)
13279{
13280 switch (tag)
13281 {
13282#if 0
13283 /* Some types that would be reasonable to generate partial symbols for,
13284 that we don't at present. */
13285 case DW_TAG_array_type:
13286 case DW_TAG_file_type:
13287 case DW_TAG_ptr_to_member_type:
13288 case DW_TAG_set_type:
13289 case DW_TAG_string_type:
13290 case DW_TAG_subroutine_type:
13291#endif
13292 case DW_TAG_base_type:
13293 case DW_TAG_class_type:
680b30c7 13294 case DW_TAG_interface_type:
72bf9492
DJ
13295 case DW_TAG_enumeration_type:
13296 case DW_TAG_structure_type:
13297 case DW_TAG_subrange_type:
13298 case DW_TAG_typedef:
13299 case DW_TAG_union_type:
13300 return 1;
13301 default:
13302 return 0;
13303 }
13304}
13305
13306/* Load all DIEs that are interesting for partial symbols into memory. */
13307
13308static struct partial_die_info *
dee91e82
DE
13309load_partial_dies (const struct die_reader_specs *reader,
13310 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13311{
dee91e82 13312 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13313 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13314 struct partial_die_info *part_die;
13315 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13316 struct abbrev_info *abbrev;
13317 unsigned int bytes_read;
5afb4e99 13318 unsigned int load_all = 0;
72bf9492
DJ
13319 int nesting_level = 1;
13320
13321 parent_die = NULL;
13322 last_die = NULL;
13323
7adf1e79
DE
13324 gdb_assert (cu->per_cu != NULL);
13325 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13326 load_all = 1;
13327
72bf9492
DJ
13328 cu->partial_dies
13329 = htab_create_alloc_ex (cu->header.length / 12,
13330 partial_die_hash,
13331 partial_die_eq,
13332 NULL,
13333 &cu->comp_unit_obstack,
13334 hashtab_obstack_allocate,
13335 dummy_obstack_deallocate);
13336
13337 part_die = obstack_alloc (&cu->comp_unit_obstack,
13338 sizeof (struct partial_die_info));
13339
13340 while (1)
13341 {
13342 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13343
13344 /* A NULL abbrev means the end of a series of children. */
13345 if (abbrev == NULL)
13346 {
13347 if (--nesting_level == 0)
13348 {
13349 /* PART_DIE was probably the last thing allocated on the
13350 comp_unit_obstack, so we could call obstack_free
13351 here. We don't do that because the waste is small,
13352 and will be cleaned up when we're done with this
13353 compilation unit. This way, we're also more robust
13354 against other users of the comp_unit_obstack. */
13355 return first_die;
13356 }
13357 info_ptr += bytes_read;
13358 last_die = parent_die;
13359 parent_die = parent_die->die_parent;
13360 continue;
13361 }
13362
98bfdba5
PA
13363 /* Check for template arguments. We never save these; if
13364 they're seen, we just mark the parent, and go on our way. */
13365 if (parent_die != NULL
13366 && cu->language == language_cplus
13367 && (abbrev->tag == DW_TAG_template_type_param
13368 || abbrev->tag == DW_TAG_template_value_param))
13369 {
13370 parent_die->has_template_arguments = 1;
13371
13372 if (!load_all)
13373 {
13374 /* We don't need a partial DIE for the template argument. */
dee91e82 13375 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13376 continue;
13377 }
13378 }
13379
0d99eb77 13380 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13381 Skip their other children. */
13382 if (!load_all
13383 && cu->language == language_cplus
13384 && parent_die != NULL
13385 && parent_die->tag == DW_TAG_subprogram)
13386 {
dee91e82 13387 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13388 continue;
13389 }
13390
5afb4e99
DJ
13391 /* Check whether this DIE is interesting enough to save. Normally
13392 we would not be interested in members here, but there may be
13393 later variables referencing them via DW_AT_specification (for
13394 static members). */
13395 if (!load_all
13396 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13397 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13398 && abbrev->tag != DW_TAG_enumerator
13399 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13400 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13401 && abbrev->tag != DW_TAG_variable
5afb4e99 13402 && abbrev->tag != DW_TAG_namespace
f55ee35c 13403 && abbrev->tag != DW_TAG_module
95554aad
TT
13404 && abbrev->tag != DW_TAG_member
13405 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13406 {
13407 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13408 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13409 continue;
13410 }
13411
dee91e82
DE
13412 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13413 info_ptr);
72bf9492
DJ
13414
13415 /* This two-pass algorithm for processing partial symbols has a
13416 high cost in cache pressure. Thus, handle some simple cases
13417 here which cover the majority of C partial symbols. DIEs
13418 which neither have specification tags in them, nor could have
13419 specification tags elsewhere pointing at them, can simply be
13420 processed and discarded.
13421
13422 This segment is also optional; scan_partial_symbols and
13423 add_partial_symbol will handle these DIEs if we chain
13424 them in normally. When compilers which do not emit large
13425 quantities of duplicate debug information are more common,
13426 this code can probably be removed. */
13427
13428 /* Any complete simple types at the top level (pretty much all
13429 of them, for a language without namespaces), can be processed
13430 directly. */
13431 if (parent_die == NULL
13432 && part_die->has_specification == 0
13433 && part_die->is_declaration == 0
d8228535 13434 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13435 || part_die->tag == DW_TAG_base_type
13436 || part_die->tag == DW_TAG_subrange_type))
13437 {
13438 if (building_psymtab && part_die->name != NULL)
04a679b8 13439 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13440 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13441 &objfile->static_psymbols,
13442 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13443 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13444 continue;
13445 }
13446
d8228535
JK
13447 /* The exception for DW_TAG_typedef with has_children above is
13448 a workaround of GCC PR debug/47510. In the case of this complaint
13449 type_name_no_tag_or_error will error on such types later.
13450
13451 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13452 it could not find the child DIEs referenced later, this is checked
13453 above. In correct DWARF DW_TAG_typedef should have no children. */
13454
13455 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13456 complaint (&symfile_complaints,
13457 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13458 "- DIE at 0x%x [in module %s]"),
b64f50a1 13459 part_die->offset.sect_off, objfile->name);
d8228535 13460
72bf9492
DJ
13461 /* If we're at the second level, and we're an enumerator, and
13462 our parent has no specification (meaning possibly lives in a
13463 namespace elsewhere), then we can add the partial symbol now
13464 instead of queueing it. */
13465 if (part_die->tag == DW_TAG_enumerator
13466 && parent_die != NULL
13467 && parent_die->die_parent == NULL
13468 && parent_die->tag == DW_TAG_enumeration_type
13469 && parent_die->has_specification == 0)
13470 {
13471 if (part_die->name == NULL)
3e43a32a
MS
13472 complaint (&symfile_complaints,
13473 _("malformed enumerator DIE ignored"));
72bf9492 13474 else if (building_psymtab)
04a679b8 13475 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13476 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13477 (cu->language == language_cplus
13478 || cu->language == language_java)
bb5ed363
DE
13479 ? &objfile->global_psymbols
13480 : &objfile->static_psymbols,
13481 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13482
dee91e82 13483 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13484 continue;
13485 }
13486
13487 /* We'll save this DIE so link it in. */
13488 part_die->die_parent = parent_die;
13489 part_die->die_sibling = NULL;
13490 part_die->die_child = NULL;
13491
13492 if (last_die && last_die == parent_die)
13493 last_die->die_child = part_die;
13494 else if (last_die)
13495 last_die->die_sibling = part_die;
13496
13497 last_die = part_die;
13498
13499 if (first_die == NULL)
13500 first_die = part_die;
13501
13502 /* Maybe add the DIE to the hash table. Not all DIEs that we
13503 find interesting need to be in the hash table, because we
13504 also have the parent/sibling/child chains; only those that we
13505 might refer to by offset later during partial symbol reading.
13506
13507 For now this means things that might have be the target of a
13508 DW_AT_specification, DW_AT_abstract_origin, or
13509 DW_AT_extension. DW_AT_extension will refer only to
13510 namespaces; DW_AT_abstract_origin refers to functions (and
13511 many things under the function DIE, but we do not recurse
13512 into function DIEs during partial symbol reading) and
13513 possibly variables as well; DW_AT_specification refers to
13514 declarations. Declarations ought to have the DW_AT_declaration
13515 flag. It happens that GCC forgets to put it in sometimes, but
13516 only for functions, not for types.
13517
13518 Adding more things than necessary to the hash table is harmless
13519 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13520 wasted time in find_partial_die, when we reread the compilation
13521 unit with load_all_dies set. */
72bf9492 13522
5afb4e99 13523 if (load_all
72929c62 13524 || abbrev->tag == DW_TAG_constant
5afb4e99 13525 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13526 || abbrev->tag == DW_TAG_variable
13527 || abbrev->tag == DW_TAG_namespace
13528 || part_die->is_declaration)
13529 {
13530 void **slot;
13531
13532 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13533 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13534 *slot = part_die;
13535 }
13536
13537 part_die = obstack_alloc (&cu->comp_unit_obstack,
13538 sizeof (struct partial_die_info));
13539
13540 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13541 we have no reason to follow the children of structures; for other
98bfdba5
PA
13542 languages we have to, so that we can get at method physnames
13543 to infer fully qualified class names, for DW_AT_specification,
13544 and for C++ template arguments. For C++, we also look one level
13545 inside functions to find template arguments (if the name of the
13546 function does not already contain the template arguments).
bc30ff58
JB
13547
13548 For Ada, we need to scan the children of subprograms and lexical
13549 blocks as well because Ada allows the definition of nested
13550 entities that could be interesting for the debugger, such as
13551 nested subprograms for instance. */
72bf9492 13552 if (last_die->has_children
5afb4e99
DJ
13553 && (load_all
13554 || last_die->tag == DW_TAG_namespace
f55ee35c 13555 || last_die->tag == DW_TAG_module
72bf9492 13556 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13557 || (cu->language == language_cplus
13558 && last_die->tag == DW_TAG_subprogram
13559 && (last_die->name == NULL
13560 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13561 || (cu->language != language_c
13562 && (last_die->tag == DW_TAG_class_type
680b30c7 13563 || last_die->tag == DW_TAG_interface_type
72bf9492 13564 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13565 || last_die->tag == DW_TAG_union_type))
13566 || (cu->language == language_ada
13567 && (last_die->tag == DW_TAG_subprogram
13568 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13569 {
13570 nesting_level++;
13571 parent_die = last_die;
13572 continue;
13573 }
13574
13575 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13576 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13577
13578 /* Back to the top, do it again. */
13579 }
13580}
13581
c906108c
SS
13582/* Read a minimal amount of information into the minimal die structure. */
13583
fe1b8b76 13584static gdb_byte *
dee91e82
DE
13585read_partial_die (const struct die_reader_specs *reader,
13586 struct partial_die_info *part_die,
13587 struct abbrev_info *abbrev, unsigned int abbrev_len,
13588 gdb_byte *info_ptr)
c906108c 13589{
dee91e82 13590 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13591 struct objfile *objfile = cu->objfile;
dee91e82 13592 gdb_byte *buffer = reader->buffer;
fa238c03 13593 unsigned int i;
c906108c 13594 struct attribute attr;
c5aa993b 13595 int has_low_pc_attr = 0;
c906108c 13596 int has_high_pc_attr = 0;
91da1414 13597 int high_pc_relative = 0;
c906108c 13598
72bf9492 13599 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13600
b64f50a1 13601 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13602
13603 info_ptr += abbrev_len;
13604
13605 if (abbrev == NULL)
13606 return info_ptr;
13607
c906108c
SS
13608 part_die->tag = abbrev->tag;
13609 part_die->has_children = abbrev->has_children;
c906108c
SS
13610
13611 for (i = 0; i < abbrev->num_attrs; ++i)
13612 {
dee91e82 13613 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13614
13615 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13616 partial symbol table. */
c906108c
SS
13617 switch (attr.name)
13618 {
13619 case DW_AT_name:
71c25dea
TT
13620 switch (part_die->tag)
13621 {
13622 case DW_TAG_compile_unit:
95554aad 13623 case DW_TAG_partial_unit:
348e048f 13624 case DW_TAG_type_unit:
71c25dea
TT
13625 /* Compilation units have a DW_AT_name that is a filename, not
13626 a source language identifier. */
13627 case DW_TAG_enumeration_type:
13628 case DW_TAG_enumerator:
13629 /* These tags always have simple identifiers already; no need
13630 to canonicalize them. */
13631 part_die->name = DW_STRING (&attr);
13632 break;
13633 default:
13634 part_die->name
13635 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13636 &objfile->objfile_obstack);
71c25dea
TT
13637 break;
13638 }
c906108c 13639 break;
31ef98ae 13640 case DW_AT_linkage_name:
c906108c 13641 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13642 /* Note that both forms of linkage name might appear. We
13643 assume they will be the same, and we only store the last
13644 one we see. */
94af9270
KS
13645 if (cu->language == language_ada)
13646 part_die->name = DW_STRING (&attr);
abc72ce4 13647 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13648 break;
13649 case DW_AT_low_pc:
13650 has_low_pc_attr = 1;
13651 part_die->lowpc = DW_ADDR (&attr);
13652 break;
13653 case DW_AT_high_pc:
13654 has_high_pc_attr = 1;
3019eac3
DE
13655 if (attr.form == DW_FORM_addr
13656 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13657 part_die->highpc = DW_ADDR (&attr);
13658 else
13659 {
13660 high_pc_relative = 1;
13661 part_die->highpc = DW_UNSND (&attr);
13662 }
c906108c
SS
13663 break;
13664 case DW_AT_location:
0963b4bd 13665 /* Support the .debug_loc offsets. */
8e19ed76
PS
13666 if (attr_form_is_block (&attr))
13667 {
95554aad 13668 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13669 }
3690dd37 13670 else if (attr_form_is_section_offset (&attr))
8e19ed76 13671 {
4d3c2250 13672 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13673 }
13674 else
13675 {
4d3c2250
KB
13676 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13677 "partial symbol information");
8e19ed76 13678 }
c906108c 13679 break;
c906108c
SS
13680 case DW_AT_external:
13681 part_die->is_external = DW_UNSND (&attr);
13682 break;
13683 case DW_AT_declaration:
13684 part_die->is_declaration = DW_UNSND (&attr);
13685 break;
13686 case DW_AT_type:
13687 part_die->has_type = 1;
13688 break;
13689 case DW_AT_abstract_origin:
13690 case DW_AT_specification:
72bf9492
DJ
13691 case DW_AT_extension:
13692 part_die->has_specification = 1;
c764a876 13693 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13694 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13695 || cu->per_cu->is_dwz);
c906108c
SS
13696 break;
13697 case DW_AT_sibling:
13698 /* Ignore absolute siblings, they might point outside of
13699 the current compile unit. */
13700 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13701 complaint (&symfile_complaints,
13702 _("ignoring absolute DW_AT_sibling"));
c906108c 13703 else
b64f50a1 13704 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13705 break;
fa4028e9
JB
13706 case DW_AT_byte_size:
13707 part_die->has_byte_size = 1;
13708 break;
68511cec
CES
13709 case DW_AT_calling_convention:
13710 /* DWARF doesn't provide a way to identify a program's source-level
13711 entry point. DW_AT_calling_convention attributes are only meant
13712 to describe functions' calling conventions.
13713
13714 However, because it's a necessary piece of information in
13715 Fortran, and because DW_CC_program is the only piece of debugging
13716 information whose definition refers to a 'main program' at all,
13717 several compilers have begun marking Fortran main programs with
13718 DW_CC_program --- even when those functions use the standard
13719 calling conventions.
13720
13721 So until DWARF specifies a way to provide this information and
13722 compilers pick up the new representation, we'll support this
13723 practice. */
13724 if (DW_UNSND (&attr) == DW_CC_program
13725 && cu->language == language_fortran)
01f8c46d
JK
13726 {
13727 set_main_name (part_die->name);
13728
13729 /* As this DIE has a static linkage the name would be difficult
13730 to look up later. */
13731 language_of_main = language_fortran;
13732 }
68511cec 13733 break;
481860b3
GB
13734 case DW_AT_inline:
13735 if (DW_UNSND (&attr) == DW_INL_inlined
13736 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13737 part_die->may_be_inlined = 1;
13738 break;
95554aad
TT
13739
13740 case DW_AT_import:
13741 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13742 {
13743 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13744 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13745 || cu->per_cu->is_dwz);
13746 }
95554aad
TT
13747 break;
13748
c906108c
SS
13749 default:
13750 break;
13751 }
13752 }
13753
91da1414
MW
13754 if (high_pc_relative)
13755 part_die->highpc += part_die->lowpc;
13756
9373cf26
JK
13757 if (has_low_pc_attr && has_high_pc_attr)
13758 {
13759 /* When using the GNU linker, .gnu.linkonce. sections are used to
13760 eliminate duplicate copies of functions and vtables and such.
13761 The linker will arbitrarily choose one and discard the others.
13762 The AT_*_pc values for such functions refer to local labels in
13763 these sections. If the section from that file was discarded, the
13764 labels are not in the output, so the relocs get a value of 0.
13765 If this is a discarded function, mark the pc bounds as invalid,
13766 so that GDB will ignore it. */
13767 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13768 {
bb5ed363 13769 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13770
13771 complaint (&symfile_complaints,
13772 _("DW_AT_low_pc %s is zero "
13773 "for DIE at 0x%x [in module %s]"),
13774 paddress (gdbarch, part_die->lowpc),
b64f50a1 13775 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13776 }
13777 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13778 else if (part_die->lowpc >= part_die->highpc)
13779 {
bb5ed363 13780 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13781
13782 complaint (&symfile_complaints,
13783 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13784 "for DIE at 0x%x [in module %s]"),
13785 paddress (gdbarch, part_die->lowpc),
13786 paddress (gdbarch, part_die->highpc),
b64f50a1 13787 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13788 }
13789 else
13790 part_die->has_pc_info = 1;
13791 }
85cbf3d3 13792
c906108c
SS
13793 return info_ptr;
13794}
13795
72bf9492
DJ
13796/* Find a cached partial DIE at OFFSET in CU. */
13797
13798static struct partial_die_info *
b64f50a1 13799find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13800{
13801 struct partial_die_info *lookup_die = NULL;
13802 struct partial_die_info part_die;
13803
13804 part_die.offset = offset;
b64f50a1
JK
13805 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13806 offset.sect_off);
72bf9492 13807
72bf9492
DJ
13808 return lookup_die;
13809}
13810
348e048f
DE
13811/* Find a partial DIE at OFFSET, which may or may not be in CU,
13812 except in the case of .debug_types DIEs which do not reference
13813 outside their CU (they do however referencing other types via
55f1336d 13814 DW_FORM_ref_sig8). */
72bf9492
DJ
13815
13816static struct partial_die_info *
36586728 13817find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13818{
bb5ed363 13819 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13820 struct dwarf2_per_cu_data *per_cu = NULL;
13821 struct partial_die_info *pd = NULL;
72bf9492 13822
36586728
TT
13823 if (offset_in_dwz == cu->per_cu->is_dwz
13824 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13825 {
13826 pd = find_partial_die_in_comp_unit (offset, cu);
13827 if (pd != NULL)
13828 return pd;
0d99eb77
DE
13829 /* We missed recording what we needed.
13830 Load all dies and try again. */
13831 per_cu = cu->per_cu;
5afb4e99 13832 }
0d99eb77
DE
13833 else
13834 {
13835 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13836 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13837 {
13838 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13839 " external reference to offset 0x%lx [in module %s].\n"),
13840 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13841 bfd_get_filename (objfile->obfd));
13842 }
36586728
TT
13843 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13844 objfile);
72bf9492 13845
0d99eb77
DE
13846 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13847 load_partial_comp_unit (per_cu);
ae038cb0 13848
0d99eb77
DE
13849 per_cu->cu->last_used = 0;
13850 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13851 }
5afb4e99 13852
dee91e82
DE
13853 /* If we didn't find it, and not all dies have been loaded,
13854 load them all and try again. */
13855
5afb4e99
DJ
13856 if (pd == NULL && per_cu->load_all_dies == 0)
13857 {
5afb4e99 13858 per_cu->load_all_dies = 1;
fd820528
DE
13859
13860 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13861 THIS_CU->cu may already be in use. So we can't just free it and
13862 replace its DIEs with the ones we read in. Instead, we leave those
13863 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13864 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13865 set. */
dee91e82 13866 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13867
13868 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13869 }
13870
13871 if (pd == NULL)
13872 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13873 _("could not find partial DIE 0x%x "
13874 "in cache [from module %s]\n"),
b64f50a1 13875 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13876 return pd;
72bf9492
DJ
13877}
13878
abc72ce4
DE
13879/* See if we can figure out if the class lives in a namespace. We do
13880 this by looking for a member function; its demangled name will
13881 contain namespace info, if there is any. */
13882
13883static void
13884guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13885 struct dwarf2_cu *cu)
13886{
13887 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13888 what template types look like, because the demangler
13889 frequently doesn't give the same name as the debug info. We
13890 could fix this by only using the demangled name to get the
13891 prefix (but see comment in read_structure_type). */
13892
13893 struct partial_die_info *real_pdi;
13894 struct partial_die_info *child_pdi;
13895
13896 /* If this DIE (this DIE's specification, if any) has a parent, then
13897 we should not do this. We'll prepend the parent's fully qualified
13898 name when we create the partial symbol. */
13899
13900 real_pdi = struct_pdi;
13901 while (real_pdi->has_specification)
36586728
TT
13902 real_pdi = find_partial_die (real_pdi->spec_offset,
13903 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13904
13905 if (real_pdi->die_parent != NULL)
13906 return;
13907
13908 for (child_pdi = struct_pdi->die_child;
13909 child_pdi != NULL;
13910 child_pdi = child_pdi->die_sibling)
13911 {
13912 if (child_pdi->tag == DW_TAG_subprogram
13913 && child_pdi->linkage_name != NULL)
13914 {
13915 char *actual_class_name
13916 = language_class_name_from_physname (cu->language_defn,
13917 child_pdi->linkage_name);
13918 if (actual_class_name != NULL)
13919 {
13920 struct_pdi->name
10f0c4bb
TT
13921 = obstack_copy0 (&cu->objfile->objfile_obstack,
13922 actual_class_name,
13923 strlen (actual_class_name));
abc72ce4
DE
13924 xfree (actual_class_name);
13925 }
13926 break;
13927 }
13928 }
13929}
13930
72bf9492
DJ
13931/* Adjust PART_DIE before generating a symbol for it. This function
13932 may set the is_external flag or change the DIE's name. */
13933
13934static void
13935fixup_partial_die (struct partial_die_info *part_die,
13936 struct dwarf2_cu *cu)
13937{
abc72ce4
DE
13938 /* Once we've fixed up a die, there's no point in doing so again.
13939 This also avoids a memory leak if we were to call
13940 guess_partial_die_structure_name multiple times. */
13941 if (part_die->fixup_called)
13942 return;
13943
72bf9492
DJ
13944 /* If we found a reference attribute and the DIE has no name, try
13945 to find a name in the referred to DIE. */
13946
13947 if (part_die->name == NULL && part_die->has_specification)
13948 {
13949 struct partial_die_info *spec_die;
72bf9492 13950
36586728
TT
13951 spec_die = find_partial_die (part_die->spec_offset,
13952 part_die->spec_is_dwz, cu);
72bf9492 13953
10b3939b 13954 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13955
13956 if (spec_die->name)
13957 {
13958 part_die->name = spec_die->name;
13959
13960 /* Copy DW_AT_external attribute if it is set. */
13961 if (spec_die->is_external)
13962 part_die->is_external = spec_die->is_external;
13963 }
13964 }
13965
13966 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13967
13968 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13969 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13970
abc72ce4
DE
13971 /* If there is no parent die to provide a namespace, and there are
13972 children, see if we can determine the namespace from their linkage
122d1940 13973 name. */
abc72ce4 13974 if (cu->language == language_cplus
8b70b953 13975 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13976 && part_die->die_parent == NULL
13977 && part_die->has_children
13978 && (part_die->tag == DW_TAG_class_type
13979 || part_die->tag == DW_TAG_structure_type
13980 || part_die->tag == DW_TAG_union_type))
13981 guess_partial_die_structure_name (part_die, cu);
13982
53832f31
TT
13983 /* GCC might emit a nameless struct or union that has a linkage
13984 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13985 if (part_die->name == NULL
96408a79
SA
13986 && (part_die->tag == DW_TAG_class_type
13987 || part_die->tag == DW_TAG_interface_type
13988 || part_die->tag == DW_TAG_structure_type
13989 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13990 && part_die->linkage_name != NULL)
13991 {
13992 char *demangled;
13993
13994 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13995 if (demangled)
13996 {
96408a79
SA
13997 const char *base;
13998
13999 /* Strip any leading namespaces/classes, keep only the base name.
14000 DW_AT_name for named DIEs does not contain the prefixes. */
14001 base = strrchr (demangled, ':');
14002 if (base && base > demangled && base[-1] == ':')
14003 base++;
14004 else
14005 base = demangled;
14006
10f0c4bb
TT
14007 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14008 base, strlen (base));
53832f31
TT
14009 xfree (demangled);
14010 }
14011 }
14012
abc72ce4 14013 part_die->fixup_called = 1;
72bf9492
DJ
14014}
14015
a8329558 14016/* Read an attribute value described by an attribute form. */
c906108c 14017
fe1b8b76 14018static gdb_byte *
dee91e82
DE
14019read_attribute_value (const struct die_reader_specs *reader,
14020 struct attribute *attr, unsigned form,
14021 gdb_byte *info_ptr)
c906108c 14022{
dee91e82
DE
14023 struct dwarf2_cu *cu = reader->cu;
14024 bfd *abfd = reader->abfd;
e7c27a73 14025 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14026 unsigned int bytes_read;
14027 struct dwarf_block *blk;
14028
a8329558
KW
14029 attr->form = form;
14030 switch (form)
c906108c 14031 {
c906108c 14032 case DW_FORM_ref_addr:
ae411497 14033 if (cu->header.version == 2)
4568ecf9 14034 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14035 else
4568ecf9
DE
14036 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14037 &cu->header, &bytes_read);
ae411497
TT
14038 info_ptr += bytes_read;
14039 break;
36586728
TT
14040 case DW_FORM_GNU_ref_alt:
14041 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14042 info_ptr += bytes_read;
14043 break;
ae411497 14044 case DW_FORM_addr:
e7c27a73 14045 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14046 info_ptr += bytes_read;
c906108c
SS
14047 break;
14048 case DW_FORM_block2:
7b5a2f43 14049 blk = dwarf_alloc_block (cu);
c906108c
SS
14050 blk->size = read_2_bytes (abfd, info_ptr);
14051 info_ptr += 2;
14052 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14053 info_ptr += blk->size;
14054 DW_BLOCK (attr) = blk;
14055 break;
14056 case DW_FORM_block4:
7b5a2f43 14057 blk = dwarf_alloc_block (cu);
c906108c
SS
14058 blk->size = read_4_bytes (abfd, info_ptr);
14059 info_ptr += 4;
14060 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14061 info_ptr += blk->size;
14062 DW_BLOCK (attr) = blk;
14063 break;
14064 case DW_FORM_data2:
14065 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14066 info_ptr += 2;
14067 break;
14068 case DW_FORM_data4:
14069 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14070 info_ptr += 4;
14071 break;
14072 case DW_FORM_data8:
14073 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14074 info_ptr += 8;
14075 break;
2dc7f7b3
TT
14076 case DW_FORM_sec_offset:
14077 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14078 info_ptr += bytes_read;
14079 break;
c906108c 14080 case DW_FORM_string:
9b1c24c8 14081 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14082 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14083 info_ptr += bytes_read;
14084 break;
4bdf3d34 14085 case DW_FORM_strp:
36586728
TT
14086 if (!cu->per_cu->is_dwz)
14087 {
14088 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14089 &bytes_read);
14090 DW_STRING_IS_CANONICAL (attr) = 0;
14091 info_ptr += bytes_read;
14092 break;
14093 }
14094 /* FALLTHROUGH */
14095 case DW_FORM_GNU_strp_alt:
14096 {
14097 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14098 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14099 &bytes_read);
14100
14101 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14102 DW_STRING_IS_CANONICAL (attr) = 0;
14103 info_ptr += bytes_read;
14104 }
4bdf3d34 14105 break;
2dc7f7b3 14106 case DW_FORM_exprloc:
c906108c 14107 case DW_FORM_block:
7b5a2f43 14108 blk = dwarf_alloc_block (cu);
c906108c
SS
14109 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14110 info_ptr += bytes_read;
14111 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14112 info_ptr += blk->size;
14113 DW_BLOCK (attr) = blk;
14114 break;
14115 case DW_FORM_block1:
7b5a2f43 14116 blk = dwarf_alloc_block (cu);
c906108c
SS
14117 blk->size = read_1_byte (abfd, info_ptr);
14118 info_ptr += 1;
14119 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14120 info_ptr += blk->size;
14121 DW_BLOCK (attr) = blk;
14122 break;
14123 case DW_FORM_data1:
14124 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14125 info_ptr += 1;
14126 break;
14127 case DW_FORM_flag:
14128 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14129 info_ptr += 1;
14130 break;
2dc7f7b3
TT
14131 case DW_FORM_flag_present:
14132 DW_UNSND (attr) = 1;
14133 break;
c906108c
SS
14134 case DW_FORM_sdata:
14135 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14136 info_ptr += bytes_read;
14137 break;
14138 case DW_FORM_udata:
14139 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14140 info_ptr += bytes_read;
14141 break;
14142 case DW_FORM_ref1:
4568ecf9
DE
14143 DW_UNSND (attr) = (cu->header.offset.sect_off
14144 + read_1_byte (abfd, info_ptr));
c906108c
SS
14145 info_ptr += 1;
14146 break;
14147 case DW_FORM_ref2:
4568ecf9
DE
14148 DW_UNSND (attr) = (cu->header.offset.sect_off
14149 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14150 info_ptr += 2;
14151 break;
14152 case DW_FORM_ref4:
4568ecf9
DE
14153 DW_UNSND (attr) = (cu->header.offset.sect_off
14154 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14155 info_ptr += 4;
14156 break;
613e1657 14157 case DW_FORM_ref8:
4568ecf9
DE
14158 DW_UNSND (attr) = (cu->header.offset.sect_off
14159 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14160 info_ptr += 8;
14161 break;
55f1336d 14162 case DW_FORM_ref_sig8:
348e048f
DE
14163 /* Convert the signature to something we can record in DW_UNSND
14164 for later lookup.
14165 NOTE: This is NULL if the type wasn't found. */
14166 DW_SIGNATURED_TYPE (attr) =
e319fa28 14167 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14168 info_ptr += 8;
14169 break;
c906108c 14170 case DW_FORM_ref_udata:
4568ecf9
DE
14171 DW_UNSND (attr) = (cu->header.offset.sect_off
14172 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14173 info_ptr += bytes_read;
14174 break;
c906108c 14175 case DW_FORM_indirect:
a8329558
KW
14176 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14177 info_ptr += bytes_read;
dee91e82 14178 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14179 break;
3019eac3
DE
14180 case DW_FORM_GNU_addr_index:
14181 if (reader->dwo_file == NULL)
14182 {
14183 /* For now flag a hard error.
14184 Later we can turn this into a complaint. */
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 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14190 info_ptr += bytes_read;
14191 break;
14192 case DW_FORM_GNU_str_index:
14193 if (reader->dwo_file == NULL)
14194 {
14195 /* For now flag a hard error.
14196 Later we can turn this into a complaint if warranted. */
14197 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14198 dwarf_form_name (form),
14199 bfd_get_filename (abfd));
14200 }
14201 {
14202 ULONGEST str_index =
14203 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14204
14205 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14206 DW_STRING_IS_CANONICAL (attr) = 0;
14207 info_ptr += bytes_read;
14208 }
14209 break;
c906108c 14210 default:
8a3fe4f8 14211 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14212 dwarf_form_name (form),
14213 bfd_get_filename (abfd));
c906108c 14214 }
28e94949 14215
36586728
TT
14216 /* Super hack. */
14217 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14218 attr->form = DW_FORM_GNU_ref_alt;
14219
28e94949
JB
14220 /* We have seen instances where the compiler tried to emit a byte
14221 size attribute of -1 which ended up being encoded as an unsigned
14222 0xffffffff. Although 0xffffffff is technically a valid size value,
14223 an object of this size seems pretty unlikely so we can relatively
14224 safely treat these cases as if the size attribute was invalid and
14225 treat them as zero by default. */
14226 if (attr->name == DW_AT_byte_size
14227 && form == DW_FORM_data4
14228 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14229 {
14230 complaint
14231 (&symfile_complaints,
43bbcdc2
PH
14232 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14233 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14234 DW_UNSND (attr) = 0;
14235 }
28e94949 14236
c906108c
SS
14237 return info_ptr;
14238}
14239
a8329558
KW
14240/* Read an attribute described by an abbreviated attribute. */
14241
fe1b8b76 14242static gdb_byte *
dee91e82
DE
14243read_attribute (const struct die_reader_specs *reader,
14244 struct attribute *attr, struct attr_abbrev *abbrev,
14245 gdb_byte *info_ptr)
a8329558
KW
14246{
14247 attr->name = abbrev->name;
dee91e82 14248 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14249}
14250
0963b4bd 14251/* Read dwarf information from a buffer. */
c906108c
SS
14252
14253static unsigned int
a1855c1d 14254read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14255{
fe1b8b76 14256 return bfd_get_8 (abfd, buf);
c906108c
SS
14257}
14258
14259static int
a1855c1d 14260read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14261{
fe1b8b76 14262 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14263}
14264
14265static unsigned int
a1855c1d 14266read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14267{
fe1b8b76 14268 return bfd_get_16 (abfd, buf);
c906108c
SS
14269}
14270
21ae7a4d 14271static int
a1855c1d 14272read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14273{
14274 return bfd_get_signed_16 (abfd, buf);
14275}
14276
c906108c 14277static unsigned int
a1855c1d 14278read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14279{
fe1b8b76 14280 return bfd_get_32 (abfd, buf);
c906108c
SS
14281}
14282
21ae7a4d 14283static int
a1855c1d 14284read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14285{
14286 return bfd_get_signed_32 (abfd, buf);
14287}
14288
93311388 14289static ULONGEST
a1855c1d 14290read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14291{
fe1b8b76 14292 return bfd_get_64 (abfd, buf);
c906108c
SS
14293}
14294
14295static CORE_ADDR
fe1b8b76 14296read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14297 unsigned int *bytes_read)
c906108c 14298{
e7c27a73 14299 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14300 CORE_ADDR retval = 0;
14301
107d2387 14302 if (cu_header->signed_addr_p)
c906108c 14303 {
107d2387
AC
14304 switch (cu_header->addr_size)
14305 {
14306 case 2:
fe1b8b76 14307 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14308 break;
14309 case 4:
fe1b8b76 14310 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14311 break;
14312 case 8:
fe1b8b76 14313 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14314 break;
14315 default:
8e65ff28 14316 internal_error (__FILE__, __LINE__,
e2e0b3e5 14317 _("read_address: bad switch, signed [in module %s]"),
659b0389 14318 bfd_get_filename (abfd));
107d2387
AC
14319 }
14320 }
14321 else
14322 {
14323 switch (cu_header->addr_size)
14324 {
14325 case 2:
fe1b8b76 14326 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14327 break;
14328 case 4:
fe1b8b76 14329 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14330 break;
14331 case 8:
fe1b8b76 14332 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14333 break;
14334 default:
8e65ff28 14335 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14336 _("read_address: bad switch, "
14337 "unsigned [in module %s]"),
659b0389 14338 bfd_get_filename (abfd));
107d2387 14339 }
c906108c 14340 }
64367e0a 14341
107d2387
AC
14342 *bytes_read = cu_header->addr_size;
14343 return retval;
c906108c
SS
14344}
14345
f7ef9339 14346/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14347 specification allows the initial length to take up either 4 bytes
14348 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14349 bytes describe the length and all offsets will be 8 bytes in length
14350 instead of 4.
14351
f7ef9339
KB
14352 An older, non-standard 64-bit format is also handled by this
14353 function. The older format in question stores the initial length
14354 as an 8-byte quantity without an escape value. Lengths greater
14355 than 2^32 aren't very common which means that the initial 4 bytes
14356 is almost always zero. Since a length value of zero doesn't make
14357 sense for the 32-bit format, this initial zero can be considered to
14358 be an escape value which indicates the presence of the older 64-bit
14359 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14360 greater than 4GB. If it becomes necessary to handle lengths
14361 somewhat larger than 4GB, we could allow other small values (such
14362 as the non-sensical values of 1, 2, and 3) to also be used as
14363 escape values indicating the presence of the old format.
f7ef9339 14364
917c78fc
MK
14365 The value returned via bytes_read should be used to increment the
14366 relevant pointer after calling read_initial_length().
c764a876 14367
613e1657
KB
14368 [ Note: read_initial_length() and read_offset() are based on the
14369 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14370 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14371 from:
14372
f7ef9339 14373 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14374
613e1657
KB
14375 This document is only a draft and is subject to change. (So beware.)
14376
f7ef9339 14377 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14378 determined empirically by examining 64-bit ELF files produced by
14379 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14380
14381 - Kevin, July 16, 2002
613e1657
KB
14382 ] */
14383
14384static LONGEST
c764a876 14385read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14386{
fe1b8b76 14387 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14388
dd373385 14389 if (length == 0xffffffff)
613e1657 14390 {
fe1b8b76 14391 length = bfd_get_64 (abfd, buf + 4);
613e1657 14392 *bytes_read = 12;
613e1657 14393 }
dd373385 14394 else if (length == 0)
f7ef9339 14395 {
dd373385 14396 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14397 length = bfd_get_64 (abfd, buf);
f7ef9339 14398 *bytes_read = 8;
f7ef9339 14399 }
613e1657
KB
14400 else
14401 {
14402 *bytes_read = 4;
613e1657
KB
14403 }
14404
c764a876
DE
14405 return length;
14406}
dd373385 14407
c764a876
DE
14408/* Cover function for read_initial_length.
14409 Returns the length of the object at BUF, and stores the size of the
14410 initial length in *BYTES_READ and stores the size that offsets will be in
14411 *OFFSET_SIZE.
14412 If the initial length size is not equivalent to that specified in
14413 CU_HEADER then issue a complaint.
14414 This is useful when reading non-comp-unit headers. */
dd373385 14415
c764a876
DE
14416static LONGEST
14417read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14418 const struct comp_unit_head *cu_header,
14419 unsigned int *bytes_read,
14420 unsigned int *offset_size)
14421{
14422 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14423
14424 gdb_assert (cu_header->initial_length_size == 4
14425 || cu_header->initial_length_size == 8
14426 || cu_header->initial_length_size == 12);
14427
14428 if (cu_header->initial_length_size != *bytes_read)
14429 complaint (&symfile_complaints,
14430 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14431
c764a876 14432 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14433 return length;
613e1657
KB
14434}
14435
14436/* Read an offset from the data stream. The size of the offset is
917c78fc 14437 given by cu_header->offset_size. */
613e1657
KB
14438
14439static LONGEST
fe1b8b76 14440read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14441 unsigned int *bytes_read)
c764a876
DE
14442{
14443 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14444
c764a876
DE
14445 *bytes_read = cu_header->offset_size;
14446 return offset;
14447}
14448
14449/* Read an offset from the data stream. */
14450
14451static LONGEST
14452read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14453{
14454 LONGEST retval = 0;
14455
c764a876 14456 switch (offset_size)
613e1657
KB
14457 {
14458 case 4:
fe1b8b76 14459 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14460 break;
14461 case 8:
fe1b8b76 14462 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14463 break;
14464 default:
8e65ff28 14465 internal_error (__FILE__, __LINE__,
c764a876 14466 _("read_offset_1: bad switch [in module %s]"),
659b0389 14467 bfd_get_filename (abfd));
613e1657
KB
14468 }
14469
917c78fc 14470 return retval;
613e1657
KB
14471}
14472
fe1b8b76
JB
14473static gdb_byte *
14474read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14475{
14476 /* If the size of a host char is 8 bits, we can return a pointer
14477 to the buffer, otherwise we have to copy the data to a buffer
14478 allocated on the temporary obstack. */
4bdf3d34 14479 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14480 return buf;
c906108c
SS
14481}
14482
14483static char *
9b1c24c8 14484read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14485{
14486 /* If the size of a host char is 8 bits, we can return a pointer
14487 to the string, otherwise we have to copy the string to a buffer
14488 allocated on the temporary obstack. */
4bdf3d34 14489 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14490 if (*buf == '\0')
14491 {
14492 *bytes_read_ptr = 1;
14493 return NULL;
14494 }
fe1b8b76
JB
14495 *bytes_read_ptr = strlen ((char *) buf) + 1;
14496 return (char *) buf;
4bdf3d34
JJ
14497}
14498
14499static char *
cf2c3c16 14500read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14501{
be391dca 14502 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14503 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14504 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14505 bfd_get_filename (abfd));
dce234bc 14506 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14507 error (_("DW_FORM_strp pointing outside of "
14508 ".debug_str section [in module %s]"),
14509 bfd_get_filename (abfd));
4bdf3d34 14510 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14511 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14512 return NULL;
dce234bc 14513 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14514}
14515
36586728
TT
14516/* Read a string at offset STR_OFFSET in the .debug_str section from
14517 the .dwz file DWZ. Throw an error if the offset is too large. If
14518 the string consists of a single NUL byte, return NULL; otherwise
14519 return a pointer to the string. */
14520
14521static char *
14522read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14523{
14524 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14525
14526 if (dwz->str.buffer == NULL)
14527 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14528 "section [in module %s]"),
14529 bfd_get_filename (dwz->dwz_bfd));
14530 if (str_offset >= dwz->str.size)
14531 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14532 ".debug_str section [in module %s]"),
14533 bfd_get_filename (dwz->dwz_bfd));
14534 gdb_assert (HOST_CHAR_BIT == 8);
14535 if (dwz->str.buffer[str_offset] == '\0')
14536 return NULL;
14537 return (char *) (dwz->str.buffer + str_offset);
14538}
14539
cf2c3c16
TT
14540static char *
14541read_indirect_string (bfd *abfd, gdb_byte *buf,
14542 const struct comp_unit_head *cu_header,
14543 unsigned int *bytes_read_ptr)
14544{
14545 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14546
14547 return read_indirect_string_at_offset (abfd, str_offset);
14548}
14549
12df843f 14550static ULONGEST
fe1b8b76 14551read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14552{
12df843f 14553 ULONGEST result;
ce5d95e1 14554 unsigned int num_read;
c906108c
SS
14555 int i, shift;
14556 unsigned char byte;
14557
14558 result = 0;
14559 shift = 0;
14560 num_read = 0;
14561 i = 0;
14562 while (1)
14563 {
fe1b8b76 14564 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14565 buf++;
14566 num_read++;
12df843f 14567 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14568 if ((byte & 128) == 0)
14569 {
14570 break;
14571 }
14572 shift += 7;
14573 }
14574 *bytes_read_ptr = num_read;
14575 return result;
14576}
14577
12df843f 14578static LONGEST
fe1b8b76 14579read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14580{
12df843f 14581 LONGEST result;
77e0b926 14582 int i, shift, num_read;
c906108c
SS
14583 unsigned char byte;
14584
14585 result = 0;
14586 shift = 0;
c906108c
SS
14587 num_read = 0;
14588 i = 0;
14589 while (1)
14590 {
fe1b8b76 14591 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14592 buf++;
14593 num_read++;
12df843f 14594 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14595 shift += 7;
14596 if ((byte & 128) == 0)
14597 {
14598 break;
14599 }
14600 }
77e0b926 14601 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14602 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14603 *bytes_read_ptr = num_read;
14604 return result;
14605}
14606
3019eac3
DE
14607/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14608 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14609 ADDR_SIZE is the size of addresses from the CU header. */
14610
14611static CORE_ADDR
14612read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14613{
14614 struct objfile *objfile = dwarf2_per_objfile->objfile;
14615 bfd *abfd = objfile->obfd;
14616 const gdb_byte *info_ptr;
14617
14618 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14619 if (dwarf2_per_objfile->addr.buffer == NULL)
14620 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14621 objfile->name);
14622 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14623 error (_("DW_FORM_addr_index pointing outside of "
14624 ".debug_addr section [in module %s]"),
14625 objfile->name);
14626 info_ptr = (dwarf2_per_objfile->addr.buffer
14627 + addr_base + addr_index * addr_size);
14628 if (addr_size == 4)
14629 return bfd_get_32 (abfd, info_ptr);
14630 else
14631 return bfd_get_64 (abfd, info_ptr);
14632}
14633
14634/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14635
14636static CORE_ADDR
14637read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14638{
14639 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14640}
14641
14642/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14643
14644static CORE_ADDR
14645read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14646 unsigned int *bytes_read)
14647{
14648 bfd *abfd = cu->objfile->obfd;
14649 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14650
14651 return read_addr_index (cu, addr_index);
14652}
14653
14654/* Data structure to pass results from dwarf2_read_addr_index_reader
14655 back to dwarf2_read_addr_index. */
14656
14657struct dwarf2_read_addr_index_data
14658{
14659 ULONGEST addr_base;
14660 int addr_size;
14661};
14662
14663/* die_reader_func for dwarf2_read_addr_index. */
14664
14665static void
14666dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14667 gdb_byte *info_ptr,
14668 struct die_info *comp_unit_die,
14669 int has_children,
14670 void *data)
14671{
14672 struct dwarf2_cu *cu = reader->cu;
14673 struct dwarf2_read_addr_index_data *aidata =
14674 (struct dwarf2_read_addr_index_data *) data;
14675
14676 aidata->addr_base = cu->addr_base;
14677 aidata->addr_size = cu->header.addr_size;
14678}
14679
14680/* Given an index in .debug_addr, fetch the value.
14681 NOTE: This can be called during dwarf expression evaluation,
14682 long after the debug information has been read, and thus per_cu->cu
14683 may no longer exist. */
14684
14685CORE_ADDR
14686dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14687 unsigned int addr_index)
14688{
14689 struct objfile *objfile = per_cu->objfile;
14690 struct dwarf2_cu *cu = per_cu->cu;
14691 ULONGEST addr_base;
14692 int addr_size;
14693
14694 /* This is intended to be called from outside this file. */
14695 dw2_setup (objfile);
14696
14697 /* We need addr_base and addr_size.
14698 If we don't have PER_CU->cu, we have to get it.
14699 Nasty, but the alternative is storing the needed info in PER_CU,
14700 which at this point doesn't seem justified: it's not clear how frequently
14701 it would get used and it would increase the size of every PER_CU.
14702 Entry points like dwarf2_per_cu_addr_size do a similar thing
14703 so we're not in uncharted territory here.
14704 Alas we need to be a bit more complicated as addr_base is contained
14705 in the DIE.
14706
14707 We don't need to read the entire CU(/TU).
14708 We just need the header and top level die.
a1b64ce1 14709
3019eac3 14710 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14711 For now we skip this optimization. */
3019eac3
DE
14712
14713 if (cu != NULL)
14714 {
14715 addr_base = cu->addr_base;
14716 addr_size = cu->header.addr_size;
14717 }
14718 else
14719 {
14720 struct dwarf2_read_addr_index_data aidata;
14721
a1b64ce1
DE
14722 /* Note: We can't use init_cutu_and_read_dies_simple here,
14723 we need addr_base. */
14724 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14725 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14726 addr_base = aidata.addr_base;
14727 addr_size = aidata.addr_size;
14728 }
14729
14730 return read_addr_index_1 (addr_index, addr_base, addr_size);
14731}
14732
14733/* Given a DW_AT_str_index, fetch the string. */
14734
14735static char *
14736read_str_index (const struct die_reader_specs *reader,
14737 struct dwarf2_cu *cu, ULONGEST str_index)
14738{
14739 struct objfile *objfile = dwarf2_per_objfile->objfile;
14740 const char *dwo_name = objfile->name;
14741 bfd *abfd = objfile->obfd;
14742 struct dwo_sections *sections = &reader->dwo_file->sections;
14743 gdb_byte *info_ptr;
14744 ULONGEST str_offset;
14745
14746 dwarf2_read_section (objfile, &sections->str);
14747 dwarf2_read_section (objfile, &sections->str_offsets);
14748 if (sections->str.buffer == NULL)
14749 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14750 " in CU at offset 0x%lx [in module %s]"),
14751 (long) cu->header.offset.sect_off, dwo_name);
14752 if (sections->str_offsets.buffer == NULL)
14753 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14754 " in CU at offset 0x%lx [in module %s]"),
14755 (long) cu->header.offset.sect_off, dwo_name);
14756 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14757 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14758 " section in CU at offset 0x%lx [in module %s]"),
14759 (long) cu->header.offset.sect_off, dwo_name);
14760 info_ptr = (sections->str_offsets.buffer
14761 + str_index * cu->header.offset_size);
14762 if (cu->header.offset_size == 4)
14763 str_offset = bfd_get_32 (abfd, info_ptr);
14764 else
14765 str_offset = bfd_get_64 (abfd, info_ptr);
14766 if (str_offset >= sections->str.size)
14767 error (_("Offset from DW_FORM_str_index pointing outside of"
14768 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14769 (long) cu->header.offset.sect_off, dwo_name);
14770 return (char *) (sections->str.buffer + str_offset);
14771}
14772
3019eac3
DE
14773/* Return the length of an LEB128 number in BUF. */
14774
14775static int
14776leb128_size (const gdb_byte *buf)
14777{
14778 const gdb_byte *begin = buf;
14779 gdb_byte byte;
14780
14781 while (1)
14782 {
14783 byte = *buf++;
14784 if ((byte & 128) == 0)
14785 return buf - begin;
14786 }
14787}
14788
c906108c 14789static void
e142c38c 14790set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14791{
14792 switch (lang)
14793 {
14794 case DW_LANG_C89:
76bee0cc 14795 case DW_LANG_C99:
c906108c 14796 case DW_LANG_C:
e142c38c 14797 cu->language = language_c;
c906108c
SS
14798 break;
14799 case DW_LANG_C_plus_plus:
e142c38c 14800 cu->language = language_cplus;
c906108c 14801 break;
6aecb9c2
JB
14802 case DW_LANG_D:
14803 cu->language = language_d;
14804 break;
c906108c
SS
14805 case DW_LANG_Fortran77:
14806 case DW_LANG_Fortran90:
b21b22e0 14807 case DW_LANG_Fortran95:
e142c38c 14808 cu->language = language_fortran;
c906108c 14809 break;
a766d390
DE
14810 case DW_LANG_Go:
14811 cu->language = language_go;
14812 break;
c906108c 14813 case DW_LANG_Mips_Assembler:
e142c38c 14814 cu->language = language_asm;
c906108c 14815 break;
bebd888e 14816 case DW_LANG_Java:
e142c38c 14817 cu->language = language_java;
bebd888e 14818 break;
c906108c 14819 case DW_LANG_Ada83:
8aaf0b47 14820 case DW_LANG_Ada95:
bc5f45f8
JB
14821 cu->language = language_ada;
14822 break;
72019c9c
GM
14823 case DW_LANG_Modula2:
14824 cu->language = language_m2;
14825 break;
fe8e67fd
PM
14826 case DW_LANG_Pascal83:
14827 cu->language = language_pascal;
14828 break;
22566fbd
DJ
14829 case DW_LANG_ObjC:
14830 cu->language = language_objc;
14831 break;
c906108c
SS
14832 case DW_LANG_Cobol74:
14833 case DW_LANG_Cobol85:
c906108c 14834 default:
e142c38c 14835 cu->language = language_minimal;
c906108c
SS
14836 break;
14837 }
e142c38c 14838 cu->language_defn = language_def (cu->language);
c906108c
SS
14839}
14840
14841/* Return the named attribute or NULL if not there. */
14842
14843static struct attribute *
e142c38c 14844dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14845{
a48e046c 14846 for (;;)
c906108c 14847 {
a48e046c
TT
14848 unsigned int i;
14849 struct attribute *spec = NULL;
14850
14851 for (i = 0; i < die->num_attrs; ++i)
14852 {
14853 if (die->attrs[i].name == name)
14854 return &die->attrs[i];
14855 if (die->attrs[i].name == DW_AT_specification
14856 || die->attrs[i].name == DW_AT_abstract_origin)
14857 spec = &die->attrs[i];
14858 }
14859
14860 if (!spec)
14861 break;
c906108c 14862
f2f0e013 14863 die = follow_die_ref (die, spec, &cu);
f2f0e013 14864 }
c5aa993b 14865
c906108c
SS
14866 return NULL;
14867}
14868
348e048f
DE
14869/* Return the named attribute or NULL if not there,
14870 but do not follow DW_AT_specification, etc.
14871 This is for use in contexts where we're reading .debug_types dies.
14872 Following DW_AT_specification, DW_AT_abstract_origin will take us
14873 back up the chain, and we want to go down. */
14874
14875static struct attribute *
45e58e77 14876dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14877{
14878 unsigned int i;
14879
14880 for (i = 0; i < die->num_attrs; ++i)
14881 if (die->attrs[i].name == name)
14882 return &die->attrs[i];
14883
14884 return NULL;
14885}
14886
05cf31d1
JB
14887/* Return non-zero iff the attribute NAME is defined for the given DIE,
14888 and holds a non-zero value. This function should only be used for
2dc7f7b3 14889 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14890
14891static int
14892dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14893{
14894 struct attribute *attr = dwarf2_attr (die, name, cu);
14895
14896 return (attr && DW_UNSND (attr));
14897}
14898
3ca72b44 14899static int
e142c38c 14900die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14901{
05cf31d1
JB
14902 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14903 which value is non-zero. However, we have to be careful with
14904 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14905 (via dwarf2_flag_true_p) follows this attribute. So we may
14906 end up accidently finding a declaration attribute that belongs
14907 to a different DIE referenced by the specification attribute,
14908 even though the given DIE does not have a declaration attribute. */
14909 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14910 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14911}
14912
63d06c5c 14913/* Return the die giving the specification for DIE, if there is
f2f0e013 14914 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14915 containing the return value on output. If there is no
14916 specification, but there is an abstract origin, that is
14917 returned. */
63d06c5c
DC
14918
14919static struct die_info *
f2f0e013 14920die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14921{
f2f0e013
DJ
14922 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14923 *spec_cu);
63d06c5c 14924
edb3359d
DJ
14925 if (spec_attr == NULL)
14926 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14927
63d06c5c
DC
14928 if (spec_attr == NULL)
14929 return NULL;
14930 else
f2f0e013 14931 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14932}
c906108c 14933
debd256d 14934/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14935 refers to.
14936 NOTE: This is also used as a "cleanup" function. */
14937
debd256d
JB
14938static void
14939free_line_header (struct line_header *lh)
14940{
14941 if (lh->standard_opcode_lengths)
a8bc7b56 14942 xfree (lh->standard_opcode_lengths);
debd256d
JB
14943
14944 /* Remember that all the lh->file_names[i].name pointers are
14945 pointers into debug_line_buffer, and don't need to be freed. */
14946 if (lh->file_names)
a8bc7b56 14947 xfree (lh->file_names);
debd256d
JB
14948
14949 /* Similarly for the include directory names. */
14950 if (lh->include_dirs)
a8bc7b56 14951 xfree (lh->include_dirs);
debd256d 14952
a8bc7b56 14953 xfree (lh);
debd256d
JB
14954}
14955
debd256d 14956/* Add an entry to LH's include directory table. */
ae2de4f8 14957
debd256d
JB
14958static void
14959add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14960{
debd256d
JB
14961 /* Grow the array if necessary. */
14962 if (lh->include_dirs_size == 0)
c5aa993b 14963 {
debd256d
JB
14964 lh->include_dirs_size = 1; /* for testing */
14965 lh->include_dirs = xmalloc (lh->include_dirs_size
14966 * sizeof (*lh->include_dirs));
14967 }
14968 else if (lh->num_include_dirs >= lh->include_dirs_size)
14969 {
14970 lh->include_dirs_size *= 2;
14971 lh->include_dirs = xrealloc (lh->include_dirs,
14972 (lh->include_dirs_size
14973 * sizeof (*lh->include_dirs)));
c5aa993b 14974 }
c906108c 14975
debd256d
JB
14976 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14977}
6e70227d 14978
debd256d 14979/* Add an entry to LH's file name table. */
ae2de4f8 14980
debd256d
JB
14981static void
14982add_file_name (struct line_header *lh,
14983 char *name,
14984 unsigned int dir_index,
14985 unsigned int mod_time,
14986 unsigned int length)
14987{
14988 struct file_entry *fe;
14989
14990 /* Grow the array if necessary. */
14991 if (lh->file_names_size == 0)
14992 {
14993 lh->file_names_size = 1; /* for testing */
14994 lh->file_names = xmalloc (lh->file_names_size
14995 * sizeof (*lh->file_names));
14996 }
14997 else if (lh->num_file_names >= lh->file_names_size)
14998 {
14999 lh->file_names_size *= 2;
15000 lh->file_names = xrealloc (lh->file_names,
15001 (lh->file_names_size
15002 * sizeof (*lh->file_names)));
15003 }
15004
15005 fe = &lh->file_names[lh->num_file_names++];
15006 fe->name = name;
15007 fe->dir_index = dir_index;
15008 fe->mod_time = mod_time;
15009 fe->length = length;
aaa75496 15010 fe->included_p = 0;
cb1df416 15011 fe->symtab = NULL;
debd256d 15012}
6e70227d 15013
36586728
TT
15014/* A convenience function to find the proper .debug_line section for a
15015 CU. */
15016
15017static struct dwarf2_section_info *
15018get_debug_line_section (struct dwarf2_cu *cu)
15019{
15020 struct dwarf2_section_info *section;
15021
15022 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15023 DWO file. */
15024 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15025 section = &cu->dwo_unit->dwo_file->sections.line;
15026 else if (cu->per_cu->is_dwz)
15027 {
15028 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15029
15030 section = &dwz->line;
15031 }
15032 else
15033 section = &dwarf2_per_objfile->line;
15034
15035 return section;
15036}
15037
debd256d 15038/* Read the statement program header starting at OFFSET in
3019eac3 15039 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15040 to a struct line_header, allocated using xmalloc.
debd256d
JB
15041
15042 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15043 the returned object point into the dwarf line section buffer,
15044 and must not be freed. */
ae2de4f8 15045
debd256d 15046static struct line_header *
3019eac3 15047dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15048{
15049 struct cleanup *back_to;
15050 struct line_header *lh;
fe1b8b76 15051 gdb_byte *line_ptr;
c764a876 15052 unsigned int bytes_read, offset_size;
debd256d
JB
15053 int i;
15054 char *cur_dir, *cur_file;
3019eac3
DE
15055 struct dwarf2_section_info *section;
15056 bfd *abfd;
15057
36586728 15058 section = get_debug_line_section (cu);
3019eac3
DE
15059 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15060 if (section->buffer == NULL)
debd256d 15061 {
3019eac3
DE
15062 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15063 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15064 else
15065 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15066 return 0;
15067 }
15068
fceca515
DE
15069 /* We can't do this until we know the section is non-empty.
15070 Only then do we know we have such a section. */
15071 abfd = section->asection->owner;
15072
a738430d
MK
15073 /* Make sure that at least there's room for the total_length field.
15074 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15075 if (offset + 4 >= section->size)
debd256d 15076 {
4d3c2250 15077 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15078 return 0;
15079 }
15080
15081 lh = xmalloc (sizeof (*lh));
15082 memset (lh, 0, sizeof (*lh));
15083 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15084 (void *) lh);
15085
3019eac3 15086 line_ptr = section->buffer + offset;
debd256d 15087
a738430d 15088 /* Read in the header. */
6e70227d 15089 lh->total_length =
c764a876
DE
15090 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15091 &bytes_read, &offset_size);
debd256d 15092 line_ptr += bytes_read;
3019eac3 15093 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15094 {
4d3c2250 15095 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15096 return 0;
15097 }
15098 lh->statement_program_end = line_ptr + lh->total_length;
15099 lh->version = read_2_bytes (abfd, line_ptr);
15100 line_ptr += 2;
c764a876
DE
15101 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15102 line_ptr += offset_size;
debd256d
JB
15103 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15104 line_ptr += 1;
2dc7f7b3
TT
15105 if (lh->version >= 4)
15106 {
15107 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15108 line_ptr += 1;
15109 }
15110 else
15111 lh->maximum_ops_per_instruction = 1;
15112
15113 if (lh->maximum_ops_per_instruction == 0)
15114 {
15115 lh->maximum_ops_per_instruction = 1;
15116 complaint (&symfile_complaints,
3e43a32a
MS
15117 _("invalid maximum_ops_per_instruction "
15118 "in `.debug_line' section"));
2dc7f7b3
TT
15119 }
15120
debd256d
JB
15121 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15122 line_ptr += 1;
15123 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15124 line_ptr += 1;
15125 lh->line_range = read_1_byte (abfd, line_ptr);
15126 line_ptr += 1;
15127 lh->opcode_base = read_1_byte (abfd, line_ptr);
15128 line_ptr += 1;
15129 lh->standard_opcode_lengths
fe1b8b76 15130 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15131
15132 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15133 for (i = 1; i < lh->opcode_base; ++i)
15134 {
15135 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15136 line_ptr += 1;
15137 }
15138
a738430d 15139 /* Read directory table. */
9b1c24c8 15140 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15141 {
15142 line_ptr += bytes_read;
15143 add_include_dir (lh, cur_dir);
15144 }
15145 line_ptr += bytes_read;
15146
a738430d 15147 /* Read file name table. */
9b1c24c8 15148 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15149 {
15150 unsigned int dir_index, mod_time, length;
15151
15152 line_ptr += bytes_read;
15153 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15154 line_ptr += bytes_read;
15155 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15156 line_ptr += bytes_read;
15157 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15158 line_ptr += bytes_read;
15159
15160 add_file_name (lh, cur_file, dir_index, mod_time, length);
15161 }
15162 line_ptr += bytes_read;
6e70227d 15163 lh->statement_program_start = line_ptr;
debd256d 15164
3019eac3 15165 if (line_ptr > (section->buffer + section->size))
4d3c2250 15166 complaint (&symfile_complaints,
3e43a32a
MS
15167 _("line number info header doesn't "
15168 "fit in `.debug_line' section"));
debd256d
JB
15169
15170 discard_cleanups (back_to);
15171 return lh;
15172}
c906108c 15173
c6da4cef
DE
15174/* Subroutine of dwarf_decode_lines to simplify it.
15175 Return the file name of the psymtab for included file FILE_INDEX
15176 in line header LH of PST.
15177 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15178 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15179 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15180
15181 The function creates dangling cleanup registration. */
c6da4cef
DE
15182
15183static char *
15184psymtab_include_file_name (const struct line_header *lh, int file_index,
15185 const struct partial_symtab *pst,
15186 const char *comp_dir)
15187{
15188 const struct file_entry fe = lh->file_names [file_index];
15189 char *include_name = fe.name;
15190 char *include_name_to_compare = include_name;
15191 char *dir_name = NULL;
72b9f47f
TT
15192 const char *pst_filename;
15193 char *copied_name = NULL;
c6da4cef
DE
15194 int file_is_pst;
15195
15196 if (fe.dir_index)
15197 dir_name = lh->include_dirs[fe.dir_index - 1];
15198
15199 if (!IS_ABSOLUTE_PATH (include_name)
15200 && (dir_name != NULL || comp_dir != NULL))
15201 {
15202 /* Avoid creating a duplicate psymtab for PST.
15203 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15204 Before we do the comparison, however, we need to account
15205 for DIR_NAME and COMP_DIR.
15206 First prepend dir_name (if non-NULL). If we still don't
15207 have an absolute path prepend comp_dir (if non-NULL).
15208 However, the directory we record in the include-file's
15209 psymtab does not contain COMP_DIR (to match the
15210 corresponding symtab(s)).
15211
15212 Example:
15213
15214 bash$ cd /tmp
15215 bash$ gcc -g ./hello.c
15216 include_name = "hello.c"
15217 dir_name = "."
15218 DW_AT_comp_dir = comp_dir = "/tmp"
15219 DW_AT_name = "./hello.c" */
15220
15221 if (dir_name != NULL)
15222 {
15223 include_name = concat (dir_name, SLASH_STRING,
15224 include_name, (char *)NULL);
15225 include_name_to_compare = include_name;
15226 make_cleanup (xfree, include_name);
15227 }
15228 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15229 {
15230 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15231 include_name, (char *)NULL);
15232 }
15233 }
15234
15235 pst_filename = pst->filename;
15236 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15237 {
72b9f47f
TT
15238 copied_name = concat (pst->dirname, SLASH_STRING,
15239 pst_filename, (char *)NULL);
15240 pst_filename = copied_name;
c6da4cef
DE
15241 }
15242
1e3fad37 15243 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15244
15245 if (include_name_to_compare != include_name)
15246 xfree (include_name_to_compare);
72b9f47f
TT
15247 if (copied_name != NULL)
15248 xfree (copied_name);
c6da4cef
DE
15249
15250 if (file_is_pst)
15251 return NULL;
15252 return include_name;
15253}
15254
c91513d8
PP
15255/* Ignore this record_line request. */
15256
15257static void
15258noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15259{
15260 return;
15261}
15262
f3f5162e
DE
15263/* Subroutine of dwarf_decode_lines to simplify it.
15264 Process the line number information in LH. */
debd256d 15265
c906108c 15266static void
f3f5162e
DE
15267dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15268 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15269{
a8c50c1f 15270 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15271 gdb_byte *line_end;
a8c50c1f 15272 unsigned int bytes_read, extended_len;
c906108c 15273 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15274 CORE_ADDR baseaddr;
15275 struct objfile *objfile = cu->objfile;
f3f5162e 15276 bfd *abfd = objfile->obfd;
fbf65064 15277 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15278 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15279 struct subfile *last_subfile = NULL;
c91513d8
PP
15280 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15281 = record_line;
e142c38c
DJ
15282
15283 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15284
debd256d
JB
15285 line_ptr = lh->statement_program_start;
15286 line_end = lh->statement_program_end;
c906108c
SS
15287
15288 /* Read the statement sequences until there's nothing left. */
15289 while (line_ptr < line_end)
15290 {
15291 /* state machine registers */
15292 CORE_ADDR address = 0;
15293 unsigned int file = 1;
15294 unsigned int line = 1;
15295 unsigned int column = 0;
debd256d 15296 int is_stmt = lh->default_is_stmt;
c906108c
SS
15297 int basic_block = 0;
15298 int end_sequence = 0;
fbf65064 15299 CORE_ADDR addr;
2dc7f7b3 15300 unsigned char op_index = 0;
c906108c 15301
aaa75496 15302 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15303 {
aaa75496 15304 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15305 /* lh->include_dirs and lh->file_names are 0-based, but the
15306 directory and file name numbers in the statement program
15307 are 1-based. */
15308 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15309 char *dir = NULL;
a738430d 15310
debd256d
JB
15311 if (fe->dir_index)
15312 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15313
15314 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15315 }
15316
a738430d 15317 /* Decode the table. */
c5aa993b 15318 while (!end_sequence)
c906108c
SS
15319 {
15320 op_code = read_1_byte (abfd, line_ptr);
15321 line_ptr += 1;
59205f5a
JB
15322 if (line_ptr > line_end)
15323 {
15324 dwarf2_debug_line_missing_end_sequence_complaint ();
15325 break;
15326 }
9aa1fe7e 15327
debd256d 15328 if (op_code >= lh->opcode_base)
6e70227d 15329 {
a738430d 15330 /* Special operand. */
debd256d 15331 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15332 address += (((op_index + (adj_opcode / lh->line_range))
15333 / lh->maximum_ops_per_instruction)
15334 * lh->minimum_instruction_length);
15335 op_index = ((op_index + (adj_opcode / lh->line_range))
15336 % lh->maximum_ops_per_instruction);
debd256d 15337 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15338 if (lh->num_file_names < file || file == 0)
25e43795 15339 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15340 /* For now we ignore lines not starting on an
15341 instruction boundary. */
15342 else if (op_index == 0)
25e43795
DJ
15343 {
15344 lh->file_names[file - 1].included_p = 1;
ca5f395d 15345 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15346 {
15347 if (last_subfile != current_subfile)
15348 {
15349 addr = gdbarch_addr_bits_remove (gdbarch, address);
15350 if (last_subfile)
c91513d8 15351 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15352 last_subfile = current_subfile;
15353 }
25e43795 15354 /* Append row to matrix using current values. */
7019d805 15355 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15356 (*p_record_line) (current_subfile, line, addr);
366da635 15357 }
25e43795 15358 }
ca5f395d 15359 basic_block = 0;
9aa1fe7e
GK
15360 }
15361 else switch (op_code)
c906108c
SS
15362 {
15363 case DW_LNS_extended_op:
3e43a32a
MS
15364 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15365 &bytes_read);
473b7be6 15366 line_ptr += bytes_read;
a8c50c1f 15367 extended_end = line_ptr + extended_len;
c906108c
SS
15368 extended_op = read_1_byte (abfd, line_ptr);
15369 line_ptr += 1;
15370 switch (extended_op)
15371 {
15372 case DW_LNE_end_sequence:
c91513d8 15373 p_record_line = record_line;
c906108c 15374 end_sequence = 1;
c906108c
SS
15375 break;
15376 case DW_LNE_set_address:
e7c27a73 15377 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15378
15379 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15380 {
15381 /* This line table is for a function which has been
15382 GCd by the linker. Ignore it. PR gdb/12528 */
15383
15384 long line_offset
36586728 15385 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15386
15387 complaint (&symfile_complaints,
15388 _(".debug_line address at offset 0x%lx is 0 "
15389 "[in module %s]"),
bb5ed363 15390 line_offset, objfile->name);
c91513d8
PP
15391 p_record_line = noop_record_line;
15392 }
15393
2dc7f7b3 15394 op_index = 0;
107d2387
AC
15395 line_ptr += bytes_read;
15396 address += baseaddr;
c906108c
SS
15397 break;
15398 case DW_LNE_define_file:
debd256d
JB
15399 {
15400 char *cur_file;
15401 unsigned int dir_index, mod_time, length;
6e70227d 15402
3e43a32a
MS
15403 cur_file = read_direct_string (abfd, line_ptr,
15404 &bytes_read);
debd256d
JB
15405 line_ptr += bytes_read;
15406 dir_index =
15407 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15408 line_ptr += bytes_read;
15409 mod_time =
15410 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15411 line_ptr += bytes_read;
15412 length =
15413 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15414 line_ptr += bytes_read;
15415 add_file_name (lh, cur_file, dir_index, mod_time, length);
15416 }
c906108c 15417 break;
d0c6ba3d
CC
15418 case DW_LNE_set_discriminator:
15419 /* The discriminator is not interesting to the debugger;
15420 just ignore it. */
15421 line_ptr = extended_end;
15422 break;
c906108c 15423 default:
4d3c2250 15424 complaint (&symfile_complaints,
e2e0b3e5 15425 _("mangled .debug_line section"));
debd256d 15426 return;
c906108c 15427 }
a8c50c1f
DJ
15428 /* Make sure that we parsed the extended op correctly. If e.g.
15429 we expected a different address size than the producer used,
15430 we may have read the wrong number of bytes. */
15431 if (line_ptr != extended_end)
15432 {
15433 complaint (&symfile_complaints,
15434 _("mangled .debug_line section"));
15435 return;
15436 }
c906108c
SS
15437 break;
15438 case DW_LNS_copy:
59205f5a 15439 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15440 dwarf2_debug_line_missing_file_complaint ();
15441 else
366da635 15442 {
25e43795 15443 lh->file_names[file - 1].included_p = 1;
ca5f395d 15444 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15445 {
15446 if (last_subfile != current_subfile)
15447 {
15448 addr = gdbarch_addr_bits_remove (gdbarch, address);
15449 if (last_subfile)
c91513d8 15450 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15451 last_subfile = current_subfile;
15452 }
7019d805 15453 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15454 (*p_record_line) (current_subfile, line, addr);
fbf65064 15455 }
366da635 15456 }
c906108c
SS
15457 basic_block = 0;
15458 break;
15459 case DW_LNS_advance_pc:
2dc7f7b3
TT
15460 {
15461 CORE_ADDR adjust
15462 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15463
15464 address += (((op_index + adjust)
15465 / lh->maximum_ops_per_instruction)
15466 * lh->minimum_instruction_length);
15467 op_index = ((op_index + adjust)
15468 % lh->maximum_ops_per_instruction);
15469 line_ptr += bytes_read;
15470 }
c906108c
SS
15471 break;
15472 case DW_LNS_advance_line:
15473 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15474 line_ptr += bytes_read;
15475 break;
15476 case DW_LNS_set_file:
debd256d 15477 {
a738430d
MK
15478 /* The arrays lh->include_dirs and lh->file_names are
15479 0-based, but the directory and file name numbers in
15480 the statement program are 1-based. */
debd256d 15481 struct file_entry *fe;
4f1520fb 15482 char *dir = NULL;
a738430d 15483
debd256d
JB
15484 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15485 line_ptr += bytes_read;
59205f5a 15486 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15487 dwarf2_debug_line_missing_file_complaint ();
15488 else
15489 {
15490 fe = &lh->file_names[file - 1];
15491 if (fe->dir_index)
15492 dir = lh->include_dirs[fe->dir_index - 1];
15493 if (!decode_for_pst_p)
15494 {
15495 last_subfile = current_subfile;
15496 dwarf2_start_subfile (fe->name, dir, comp_dir);
15497 }
15498 }
debd256d 15499 }
c906108c
SS
15500 break;
15501 case DW_LNS_set_column:
15502 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15503 line_ptr += bytes_read;
15504 break;
15505 case DW_LNS_negate_stmt:
15506 is_stmt = (!is_stmt);
15507 break;
15508 case DW_LNS_set_basic_block:
15509 basic_block = 1;
15510 break;
c2c6d25f
JM
15511 /* Add to the address register of the state machine the
15512 address increment value corresponding to special opcode
a738430d
MK
15513 255. I.e., this value is scaled by the minimum
15514 instruction length since special opcode 255 would have
b021a221 15515 scaled the increment. */
c906108c 15516 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15517 {
15518 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15519
15520 address += (((op_index + adjust)
15521 / lh->maximum_ops_per_instruction)
15522 * lh->minimum_instruction_length);
15523 op_index = ((op_index + adjust)
15524 % lh->maximum_ops_per_instruction);
15525 }
c906108c
SS
15526 break;
15527 case DW_LNS_fixed_advance_pc:
15528 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15529 op_index = 0;
c906108c
SS
15530 line_ptr += 2;
15531 break;
9aa1fe7e 15532 default:
a738430d
MK
15533 {
15534 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15535 int i;
a738430d 15536
debd256d 15537 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15538 {
15539 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15540 line_ptr += bytes_read;
15541 }
15542 }
c906108c
SS
15543 }
15544 }
59205f5a
JB
15545 if (lh->num_file_names < file || file == 0)
15546 dwarf2_debug_line_missing_file_complaint ();
15547 else
15548 {
15549 lh->file_names[file - 1].included_p = 1;
15550 if (!decode_for_pst_p)
fbf65064
UW
15551 {
15552 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15553 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15554 }
59205f5a 15555 }
c906108c 15556 }
f3f5162e
DE
15557}
15558
15559/* Decode the Line Number Program (LNP) for the given line_header
15560 structure and CU. The actual information extracted and the type
15561 of structures created from the LNP depends on the value of PST.
15562
15563 1. If PST is NULL, then this procedure uses the data from the program
15564 to create all necessary symbol tables, and their linetables.
15565
15566 2. If PST is not NULL, this procedure reads the program to determine
15567 the list of files included by the unit represented by PST, and
15568 builds all the associated partial symbol tables.
15569
15570 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15571 It is used for relative paths in the line table.
15572 NOTE: When processing partial symtabs (pst != NULL),
15573 comp_dir == pst->dirname.
15574
15575 NOTE: It is important that psymtabs have the same file name (via strcmp)
15576 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15577 symtab we don't use it in the name of the psymtabs we create.
15578 E.g. expand_line_sal requires this when finding psymtabs to expand.
15579 A good testcase for this is mb-inline.exp. */
15580
15581static void
15582dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15583 struct dwarf2_cu *cu, struct partial_symtab *pst,
15584 int want_line_info)
15585{
15586 struct objfile *objfile = cu->objfile;
15587 const int decode_for_pst_p = (pst != NULL);
15588 struct subfile *first_subfile = current_subfile;
15589
15590 if (want_line_info)
15591 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15592
15593 if (decode_for_pst_p)
15594 {
15595 int file_index;
15596
15597 /* Now that we're done scanning the Line Header Program, we can
15598 create the psymtab of each included file. */
15599 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15600 if (lh->file_names[file_index].included_p == 1)
15601 {
c6da4cef
DE
15602 char *include_name =
15603 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15604 if (include_name != NULL)
aaa75496
JB
15605 dwarf2_create_include_psymtab (include_name, pst, objfile);
15606 }
15607 }
cb1df416
DJ
15608 else
15609 {
15610 /* Make sure a symtab is created for every file, even files
15611 which contain only variables (i.e. no code with associated
15612 line numbers). */
cb1df416 15613 int i;
cb1df416
DJ
15614
15615 for (i = 0; i < lh->num_file_names; i++)
15616 {
15617 char *dir = NULL;
f3f5162e 15618 struct file_entry *fe;
9a619af0 15619
cb1df416
DJ
15620 fe = &lh->file_names[i];
15621 if (fe->dir_index)
15622 dir = lh->include_dirs[fe->dir_index - 1];
15623 dwarf2_start_subfile (fe->name, dir, comp_dir);
15624
15625 /* Skip the main file; we don't need it, and it must be
15626 allocated last, so that it will show up before the
15627 non-primary symtabs in the objfile's symtab list. */
15628 if (current_subfile == first_subfile)
15629 continue;
15630
15631 if (current_subfile->symtab == NULL)
15632 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15633 objfile);
cb1df416
DJ
15634 fe->symtab = current_subfile->symtab;
15635 }
15636 }
c906108c
SS
15637}
15638
15639/* Start a subfile for DWARF. FILENAME is the name of the file and
15640 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15641 or NULL if not known. COMP_DIR is the compilation directory for the
15642 linetable's compilation unit or NULL if not known.
c906108c
SS
15643 This routine tries to keep line numbers from identical absolute and
15644 relative file names in a common subfile.
15645
15646 Using the `list' example from the GDB testsuite, which resides in
15647 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15648 of /srcdir/list0.c yields the following debugging information for list0.c:
15649
c5aa993b
JM
15650 DW_AT_name: /srcdir/list0.c
15651 DW_AT_comp_dir: /compdir
357e46e7 15652 files.files[0].name: list0.h
c5aa993b 15653 files.files[0].dir: /srcdir
357e46e7 15654 files.files[1].name: list0.c
c5aa993b 15655 files.files[1].dir: /srcdir
c906108c
SS
15656
15657 The line number information for list0.c has to end up in a single
4f1520fb
FR
15658 subfile, so that `break /srcdir/list0.c:1' works as expected.
15659 start_subfile will ensure that this happens provided that we pass the
15660 concatenation of files.files[1].dir and files.files[1].name as the
15661 subfile's name. */
c906108c
SS
15662
15663static void
3e43a32a
MS
15664dwarf2_start_subfile (char *filename, const char *dirname,
15665 const char *comp_dir)
c906108c 15666{
4f1520fb
FR
15667 char *fullname;
15668
15669 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15670 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15671 second argument to start_subfile. To be consistent, we do the
15672 same here. In order not to lose the line information directory,
15673 we concatenate it to the filename when it makes sense.
15674 Note that the Dwarf3 standard says (speaking of filenames in line
15675 information): ``The directory index is ignored for file names
15676 that represent full path names''. Thus ignoring dirname in the
15677 `else' branch below isn't an issue. */
c906108c 15678
d5166ae1 15679 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15680 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15681 else
15682 fullname = filename;
c906108c 15683
4f1520fb
FR
15684 start_subfile (fullname, comp_dir);
15685
15686 if (fullname != filename)
15687 xfree (fullname);
c906108c
SS
15688}
15689
f4dc4d17
DE
15690/* Start a symtab for DWARF.
15691 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15692
15693static void
15694dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15695 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15696{
15697 start_symtab (name, comp_dir, low_pc);
15698 record_debugformat ("DWARF 2");
15699 record_producer (cu->producer);
15700
15701 /* We assume that we're processing GCC output. */
15702 processing_gcc_compilation = 2;
15703
4d4ec4e5 15704 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15705}
15706
4c2df51b
DJ
15707static void
15708var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15709 struct dwarf2_cu *cu)
4c2df51b 15710{
e7c27a73
DJ
15711 struct objfile *objfile = cu->objfile;
15712 struct comp_unit_head *cu_header = &cu->header;
15713
4c2df51b
DJ
15714 /* NOTE drow/2003-01-30: There used to be a comment and some special
15715 code here to turn a symbol with DW_AT_external and a
15716 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15717 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15718 with some versions of binutils) where shared libraries could have
15719 relocations against symbols in their debug information - the
15720 minimal symbol would have the right address, but the debug info
15721 would not. It's no longer necessary, because we will explicitly
15722 apply relocations when we read in the debug information now. */
15723
15724 /* A DW_AT_location attribute with no contents indicates that a
15725 variable has been optimized away. */
15726 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15727 {
15728 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15729 return;
15730 }
15731
15732 /* Handle one degenerate form of location expression specially, to
15733 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15734 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15735 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15736
15737 if (attr_form_is_block (attr)
3019eac3
DE
15738 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15739 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15740 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15741 && (DW_BLOCK (attr)->size
15742 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15743 {
891d2f0b 15744 unsigned int dummy;
4c2df51b 15745
3019eac3
DE
15746 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15747 SYMBOL_VALUE_ADDRESS (sym) =
15748 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15749 else
15750 SYMBOL_VALUE_ADDRESS (sym) =
15751 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15752 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15753 fixup_symbol_section (sym, objfile);
15754 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15755 SYMBOL_SECTION (sym));
4c2df51b
DJ
15756 return;
15757 }
15758
15759 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15760 expression evaluator, and use LOC_COMPUTED only when necessary
15761 (i.e. when the value of a register or memory location is
15762 referenced, or a thread-local block, etc.). Then again, it might
15763 not be worthwhile. I'm assuming that it isn't unless performance
15764 or memory numbers show me otherwise. */
15765
e7c27a73 15766 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15767 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15768
15769 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15770 cu->has_loclist = 1;
4c2df51b
DJ
15771}
15772
c906108c
SS
15773/* Given a pointer to a DWARF information entry, figure out if we need
15774 to make a symbol table entry for it, and if so, create a new entry
15775 and return a pointer to it.
15776 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15777 used the passed type.
15778 If SPACE is not NULL, use it to hold the new symbol. If it is
15779 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15780
15781static struct symbol *
34eaf542
TT
15782new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15783 struct symbol *space)
c906108c 15784{
e7c27a73 15785 struct objfile *objfile = cu->objfile;
c906108c 15786 struct symbol *sym = NULL;
15d034d0 15787 const char *name;
c906108c
SS
15788 struct attribute *attr = NULL;
15789 struct attribute *attr2 = NULL;
e142c38c 15790 CORE_ADDR baseaddr;
e37fd15a
SW
15791 struct pending **list_to_add = NULL;
15792
edb3359d 15793 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15794
15795 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15796
94af9270 15797 name = dwarf2_name (die, cu);
c906108c
SS
15798 if (name)
15799 {
94af9270 15800 const char *linkagename;
34eaf542 15801 int suppress_add = 0;
94af9270 15802
34eaf542
TT
15803 if (space)
15804 sym = space;
15805 else
15806 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15807 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15808
15809 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15810 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15811 linkagename = dwarf2_physname (name, die, cu);
15812 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15813
f55ee35c
JK
15814 /* Fortran does not have mangling standard and the mangling does differ
15815 between gfortran, iFort etc. */
15816 if (cu->language == language_fortran
b250c185 15817 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15818 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15819 dwarf2_full_name (name, die, cu),
29df156d 15820 NULL);
f55ee35c 15821
c906108c 15822 /* Default assumptions.
c5aa993b 15823 Use the passed type or decode it from the die. */
176620f1 15824 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15825 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15826 if (type != NULL)
15827 SYMBOL_TYPE (sym) = type;
15828 else
e7c27a73 15829 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15830 attr = dwarf2_attr (die,
15831 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15832 cu);
c906108c
SS
15833 if (attr)
15834 {
15835 SYMBOL_LINE (sym) = DW_UNSND (attr);
15836 }
cb1df416 15837
edb3359d
DJ
15838 attr = dwarf2_attr (die,
15839 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15840 cu);
cb1df416
DJ
15841 if (attr)
15842 {
15843 int file_index = DW_UNSND (attr);
9a619af0 15844
cb1df416
DJ
15845 if (cu->line_header == NULL
15846 || file_index > cu->line_header->num_file_names)
15847 complaint (&symfile_complaints,
15848 _("file index out of range"));
1c3d648d 15849 else if (file_index > 0)
cb1df416
DJ
15850 {
15851 struct file_entry *fe;
9a619af0 15852
cb1df416
DJ
15853 fe = &cu->line_header->file_names[file_index - 1];
15854 SYMBOL_SYMTAB (sym) = fe->symtab;
15855 }
15856 }
15857
c906108c
SS
15858 switch (die->tag)
15859 {
15860 case DW_TAG_label:
e142c38c 15861 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15862 if (attr)
15863 {
15864 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15865 }
0f5238ed
TT
15866 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15867 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15868 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15869 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15870 break;
15871 case DW_TAG_subprogram:
15872 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15873 finish_block. */
15874 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15875 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15876 if ((attr2 && (DW_UNSND (attr2) != 0))
15877 || cu->language == language_ada)
c906108c 15878 {
2cfa0c8d
JB
15879 /* Subprograms marked external are stored as a global symbol.
15880 Ada subprograms, whether marked external or not, are always
15881 stored as a global symbol, because we want to be able to
15882 access them globally. For instance, we want to be able
15883 to break on a nested subprogram without having to
15884 specify the context. */
e37fd15a 15885 list_to_add = &global_symbols;
c906108c
SS
15886 }
15887 else
15888 {
e37fd15a 15889 list_to_add = cu->list_in_scope;
c906108c
SS
15890 }
15891 break;
edb3359d
DJ
15892 case DW_TAG_inlined_subroutine:
15893 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15894 finish_block. */
15895 SYMBOL_CLASS (sym) = LOC_BLOCK;
15896 SYMBOL_INLINED (sym) = 1;
481860b3 15897 list_to_add = cu->list_in_scope;
edb3359d 15898 break;
34eaf542
TT
15899 case DW_TAG_template_value_param:
15900 suppress_add = 1;
15901 /* Fall through. */
72929c62 15902 case DW_TAG_constant:
c906108c 15903 case DW_TAG_variable:
254e6b9e 15904 case DW_TAG_member:
0963b4bd
MS
15905 /* Compilation with minimal debug info may result in
15906 variables with missing type entries. Change the
15907 misleading `void' type to something sensible. */
c906108c 15908 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15909 SYMBOL_TYPE (sym)
46bf5051 15910 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15911
e142c38c 15912 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15913 /* In the case of DW_TAG_member, we should only be called for
15914 static const members. */
15915 if (die->tag == DW_TAG_member)
15916 {
3863f96c
DE
15917 /* dwarf2_add_field uses die_is_declaration,
15918 so we do the same. */
254e6b9e
DE
15919 gdb_assert (die_is_declaration (die, cu));
15920 gdb_assert (attr);
15921 }
c906108c
SS
15922 if (attr)
15923 {
e7c27a73 15924 dwarf2_const_value (attr, sym, cu);
e142c38c 15925 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15926 if (!suppress_add)
34eaf542
TT
15927 {
15928 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15929 list_to_add = &global_symbols;
34eaf542 15930 else
e37fd15a 15931 list_to_add = cu->list_in_scope;
34eaf542 15932 }
c906108c
SS
15933 break;
15934 }
e142c38c 15935 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15936 if (attr)
15937 {
e7c27a73 15938 var_decode_location (attr, sym, cu);
e142c38c 15939 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15940
15941 /* Fortran explicitly imports any global symbols to the local
15942 scope by DW_TAG_common_block. */
15943 if (cu->language == language_fortran && die->parent
15944 && die->parent->tag == DW_TAG_common_block)
15945 attr2 = NULL;
15946
caac4577
JG
15947 if (SYMBOL_CLASS (sym) == LOC_STATIC
15948 && SYMBOL_VALUE_ADDRESS (sym) == 0
15949 && !dwarf2_per_objfile->has_section_at_zero)
15950 {
15951 /* When a static variable is eliminated by the linker,
15952 the corresponding debug information is not stripped
15953 out, but the variable address is set to null;
15954 do not add such variables into symbol table. */
15955 }
15956 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15957 {
f55ee35c
JK
15958 /* Workaround gfortran PR debug/40040 - it uses
15959 DW_AT_location for variables in -fPIC libraries which may
15960 get overriden by other libraries/executable and get
15961 a different address. Resolve it by the minimal symbol
15962 which may come from inferior's executable using copy
15963 relocation. Make this workaround only for gfortran as for
15964 other compilers GDB cannot guess the minimal symbol
15965 Fortran mangling kind. */
15966 if (cu->language == language_fortran && die->parent
15967 && die->parent->tag == DW_TAG_module
15968 && cu->producer
15969 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15970 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15971
1c809c68
TT
15972 /* A variable with DW_AT_external is never static,
15973 but it may be block-scoped. */
15974 list_to_add = (cu->list_in_scope == &file_symbols
15975 ? &global_symbols : cu->list_in_scope);
1c809c68 15976 }
c906108c 15977 else
e37fd15a 15978 list_to_add = cu->list_in_scope;
c906108c
SS
15979 }
15980 else
15981 {
15982 /* We do not know the address of this symbol.
c5aa993b
JM
15983 If it is an external symbol and we have type information
15984 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15985 The address of the variable will then be determined from
15986 the minimal symbol table whenever the variable is
15987 referenced. */
e142c38c 15988 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15989
15990 /* Fortran explicitly imports any global symbols to the local
15991 scope by DW_TAG_common_block. */
15992 if (cu->language == language_fortran && die->parent
15993 && die->parent->tag == DW_TAG_common_block)
15994 {
15995 /* SYMBOL_CLASS doesn't matter here because
15996 read_common_block is going to reset it. */
15997 if (!suppress_add)
15998 list_to_add = cu->list_in_scope;
15999 }
16000 else if (attr2 && (DW_UNSND (attr2) != 0)
16001 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16002 {
0fe7935b
DJ
16003 /* A variable with DW_AT_external is never static, but it
16004 may be block-scoped. */
16005 list_to_add = (cu->list_in_scope == &file_symbols
16006 ? &global_symbols : cu->list_in_scope);
16007
c906108c 16008 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 16009 }
442ddf59
JK
16010 else if (!die_is_declaration (die, cu))
16011 {
16012 /* Use the default LOC_OPTIMIZED_OUT class. */
16013 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16014 if (!suppress_add)
16015 list_to_add = cu->list_in_scope;
442ddf59 16016 }
c906108c
SS
16017 }
16018 break;
16019 case DW_TAG_formal_parameter:
edb3359d
DJ
16020 /* If we are inside a function, mark this as an argument. If
16021 not, we might be looking at an argument to an inlined function
16022 when we do not have enough information to show inlined frames;
16023 pretend it's a local variable in that case so that the user can
16024 still see it. */
16025 if (context_stack_depth > 0
16026 && context_stack[context_stack_depth - 1].name != NULL)
16027 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16028 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16029 if (attr)
16030 {
e7c27a73 16031 var_decode_location (attr, sym, cu);
c906108c 16032 }
e142c38c 16033 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16034 if (attr)
16035 {
e7c27a73 16036 dwarf2_const_value (attr, sym, cu);
c906108c 16037 }
f346a30d 16038
e37fd15a 16039 list_to_add = cu->list_in_scope;
c906108c
SS
16040 break;
16041 case DW_TAG_unspecified_parameters:
16042 /* From varargs functions; gdb doesn't seem to have any
16043 interest in this information, so just ignore it for now.
16044 (FIXME?) */
16045 break;
34eaf542
TT
16046 case DW_TAG_template_type_param:
16047 suppress_add = 1;
16048 /* Fall through. */
c906108c 16049 case DW_TAG_class_type:
680b30c7 16050 case DW_TAG_interface_type:
c906108c
SS
16051 case DW_TAG_structure_type:
16052 case DW_TAG_union_type:
72019c9c 16053 case DW_TAG_set_type:
c906108c
SS
16054 case DW_TAG_enumeration_type:
16055 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16056 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16057
63d06c5c 16058 {
987504bb 16059 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16060 really ever be static objects: otherwise, if you try
16061 to, say, break of a class's method and you're in a file
16062 which doesn't mention that class, it won't work unless
16063 the check for all static symbols in lookup_symbol_aux
16064 saves you. See the OtherFileClass tests in
16065 gdb.c++/namespace.exp. */
16066
e37fd15a 16067 if (!suppress_add)
34eaf542 16068 {
34eaf542
TT
16069 list_to_add = (cu->list_in_scope == &file_symbols
16070 && (cu->language == language_cplus
16071 || cu->language == language_java)
16072 ? &global_symbols : cu->list_in_scope);
63d06c5c 16073
64382290
TT
16074 /* The semantics of C++ state that "struct foo {
16075 ... }" also defines a typedef for "foo". A Java
16076 class declaration also defines a typedef for the
16077 class. */
16078 if (cu->language == language_cplus
16079 || cu->language == language_java
16080 || cu->language == language_ada)
16081 {
16082 /* The symbol's name is already allocated along
16083 with this objfile, so we don't need to
16084 duplicate it for the type. */
16085 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16086 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16087 }
63d06c5c
DC
16088 }
16089 }
c906108c
SS
16090 break;
16091 case DW_TAG_typedef:
63d06c5c
DC
16092 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16093 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16094 list_to_add = cu->list_in_scope;
63d06c5c 16095 break;
c906108c 16096 case DW_TAG_base_type:
a02abb62 16097 case DW_TAG_subrange_type:
c906108c 16098 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16099 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16100 list_to_add = cu->list_in_scope;
c906108c
SS
16101 break;
16102 case DW_TAG_enumerator:
e142c38c 16103 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16104 if (attr)
16105 {
e7c27a73 16106 dwarf2_const_value (attr, sym, cu);
c906108c 16107 }
63d06c5c
DC
16108 {
16109 /* NOTE: carlton/2003-11-10: See comment above in the
16110 DW_TAG_class_type, etc. block. */
16111
e142c38c 16112 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16113 && (cu->language == language_cplus
16114 || cu->language == language_java)
e142c38c 16115 ? &global_symbols : cu->list_in_scope);
63d06c5c 16116 }
c906108c 16117 break;
5c4e30ca
DC
16118 case DW_TAG_namespace:
16119 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 16120 list_to_add = &global_symbols;
5c4e30ca 16121 break;
4357ac6c 16122 case DW_TAG_common_block:
5a352474 16123 SYMBOL_CLASS (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16124 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16125 add_symbol_to_list (sym, cu->list_in_scope);
16126 break;
c906108c
SS
16127 default:
16128 /* Not a tag we recognize. Hopefully we aren't processing
16129 trash data, but since we must specifically ignore things
16130 we don't recognize, there is nothing else we should do at
0963b4bd 16131 this point. */
e2e0b3e5 16132 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16133 dwarf_tag_name (die->tag));
c906108c
SS
16134 break;
16135 }
df8a16a1 16136
e37fd15a
SW
16137 if (suppress_add)
16138 {
16139 sym->hash_next = objfile->template_symbols;
16140 objfile->template_symbols = sym;
16141 list_to_add = NULL;
16142 }
16143
16144 if (list_to_add != NULL)
16145 add_symbol_to_list (sym, list_to_add);
16146
df8a16a1
DJ
16147 /* For the benefit of old versions of GCC, check for anonymous
16148 namespaces based on the demangled name. */
4d4ec4e5 16149 if (!cu->processing_has_namespace_info
94af9270 16150 && cu->language == language_cplus)
a10964d1 16151 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16152 }
16153 return (sym);
16154}
16155
34eaf542
TT
16156/* A wrapper for new_symbol_full that always allocates a new symbol. */
16157
16158static struct symbol *
16159new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16160{
16161 return new_symbol_full (die, type, cu, NULL);
16162}
16163
98bfdba5
PA
16164/* Given an attr with a DW_FORM_dataN value in host byte order,
16165 zero-extend it as appropriate for the symbol's type. The DWARF
16166 standard (v4) is not entirely clear about the meaning of using
16167 DW_FORM_dataN for a constant with a signed type, where the type is
16168 wider than the data. The conclusion of a discussion on the DWARF
16169 list was that this is unspecified. We choose to always zero-extend
16170 because that is the interpretation long in use by GCC. */
c906108c 16171
98bfdba5
PA
16172static gdb_byte *
16173dwarf2_const_value_data (struct attribute *attr, struct type *type,
16174 const char *name, struct obstack *obstack,
12df843f 16175 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16176{
e7c27a73 16177 struct objfile *objfile = cu->objfile;
e17a4113
UW
16178 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16179 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16180 LONGEST l = DW_UNSND (attr);
16181
16182 if (bits < sizeof (*value) * 8)
16183 {
16184 l &= ((LONGEST) 1 << bits) - 1;
16185 *value = l;
16186 }
16187 else if (bits == sizeof (*value) * 8)
16188 *value = l;
16189 else
16190 {
16191 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16192 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16193 return bytes;
16194 }
16195
16196 return NULL;
16197}
16198
16199/* Read a constant value from an attribute. Either set *VALUE, or if
16200 the value does not fit in *VALUE, set *BYTES - either already
16201 allocated on the objfile obstack, or newly allocated on OBSTACK,
16202 or, set *BATON, if we translated the constant to a location
16203 expression. */
16204
16205static void
16206dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16207 const char *name, struct obstack *obstack,
16208 struct dwarf2_cu *cu,
12df843f 16209 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16210 struct dwarf2_locexpr_baton **baton)
16211{
16212 struct objfile *objfile = cu->objfile;
16213 struct comp_unit_head *cu_header = &cu->header;
c906108c 16214 struct dwarf_block *blk;
98bfdba5
PA
16215 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16216 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16217
16218 *value = 0;
16219 *bytes = NULL;
16220 *baton = NULL;
c906108c
SS
16221
16222 switch (attr->form)
16223 {
16224 case DW_FORM_addr:
3019eac3 16225 case DW_FORM_GNU_addr_index:
ac56253d 16226 {
ac56253d
TT
16227 gdb_byte *data;
16228
98bfdba5
PA
16229 if (TYPE_LENGTH (type) != cu_header->addr_size)
16230 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16231 cu_header->addr_size,
98bfdba5 16232 TYPE_LENGTH (type));
ac56253d
TT
16233 /* Symbols of this form are reasonably rare, so we just
16234 piggyback on the existing location code rather than writing
16235 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16236 *baton = obstack_alloc (&objfile->objfile_obstack,
16237 sizeof (struct dwarf2_locexpr_baton));
16238 (*baton)->per_cu = cu->per_cu;
16239 gdb_assert ((*baton)->per_cu);
ac56253d 16240
98bfdba5
PA
16241 (*baton)->size = 2 + cu_header->addr_size;
16242 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16243 (*baton)->data = data;
ac56253d
TT
16244
16245 data[0] = DW_OP_addr;
16246 store_unsigned_integer (&data[1], cu_header->addr_size,
16247 byte_order, DW_ADDR (attr));
16248 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16249 }
c906108c 16250 break;
4ac36638 16251 case DW_FORM_string:
93b5768b 16252 case DW_FORM_strp:
3019eac3 16253 case DW_FORM_GNU_str_index:
36586728 16254 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16255 /* DW_STRING is already allocated on the objfile obstack, point
16256 directly to it. */
16257 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16258 break;
c906108c
SS
16259 case DW_FORM_block1:
16260 case DW_FORM_block2:
16261 case DW_FORM_block4:
16262 case DW_FORM_block:
2dc7f7b3 16263 case DW_FORM_exprloc:
c906108c 16264 blk = DW_BLOCK (attr);
98bfdba5
PA
16265 if (TYPE_LENGTH (type) != blk->size)
16266 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16267 TYPE_LENGTH (type));
16268 *bytes = blk->data;
c906108c 16269 break;
2df3850c
JM
16270
16271 /* The DW_AT_const_value attributes are supposed to carry the
16272 symbol's value "represented as it would be on the target
16273 architecture." By the time we get here, it's already been
16274 converted to host endianness, so we just need to sign- or
16275 zero-extend it as appropriate. */
16276 case DW_FORM_data1:
3e43a32a
MS
16277 *bytes = dwarf2_const_value_data (attr, type, name,
16278 obstack, cu, value, 8);
2df3850c 16279 break;
c906108c 16280 case DW_FORM_data2:
3e43a32a
MS
16281 *bytes = dwarf2_const_value_data (attr, type, name,
16282 obstack, cu, value, 16);
2df3850c 16283 break;
c906108c 16284 case DW_FORM_data4:
3e43a32a
MS
16285 *bytes = dwarf2_const_value_data (attr, type, name,
16286 obstack, cu, value, 32);
2df3850c 16287 break;
c906108c 16288 case DW_FORM_data8:
3e43a32a
MS
16289 *bytes = dwarf2_const_value_data (attr, type, name,
16290 obstack, cu, value, 64);
2df3850c
JM
16291 break;
16292
c906108c 16293 case DW_FORM_sdata:
98bfdba5 16294 *value = DW_SND (attr);
2df3850c
JM
16295 break;
16296
c906108c 16297 case DW_FORM_udata:
98bfdba5 16298 *value = DW_UNSND (attr);
c906108c 16299 break;
2df3850c 16300
c906108c 16301 default:
4d3c2250 16302 complaint (&symfile_complaints,
e2e0b3e5 16303 _("unsupported const value attribute form: '%s'"),
4d3c2250 16304 dwarf_form_name (attr->form));
98bfdba5 16305 *value = 0;
c906108c
SS
16306 break;
16307 }
16308}
16309
2df3850c 16310
98bfdba5
PA
16311/* Copy constant value from an attribute to a symbol. */
16312
2df3850c 16313static void
98bfdba5
PA
16314dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16315 struct dwarf2_cu *cu)
2df3850c 16316{
98bfdba5
PA
16317 struct objfile *objfile = cu->objfile;
16318 struct comp_unit_head *cu_header = &cu->header;
12df843f 16319 LONGEST value;
98bfdba5
PA
16320 gdb_byte *bytes;
16321 struct dwarf2_locexpr_baton *baton;
2df3850c 16322
98bfdba5
PA
16323 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16324 SYMBOL_PRINT_NAME (sym),
16325 &objfile->objfile_obstack, cu,
16326 &value, &bytes, &baton);
2df3850c 16327
98bfdba5
PA
16328 if (baton != NULL)
16329 {
16330 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16331 SYMBOL_LOCATION_BATON (sym) = baton;
16332 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16333 }
16334 else if (bytes != NULL)
16335 {
16336 SYMBOL_VALUE_BYTES (sym) = bytes;
16337 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16338 }
16339 else
16340 {
16341 SYMBOL_VALUE (sym) = value;
16342 SYMBOL_CLASS (sym) = LOC_CONST;
16343 }
2df3850c
JM
16344}
16345
c906108c
SS
16346/* Return the type of the die in question using its DW_AT_type attribute. */
16347
16348static struct type *
e7c27a73 16349die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16350{
c906108c 16351 struct attribute *type_attr;
c906108c 16352
e142c38c 16353 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16354 if (!type_attr)
16355 {
16356 /* A missing DW_AT_type represents a void type. */
46bf5051 16357 return objfile_type (cu->objfile)->builtin_void;
c906108c 16358 }
348e048f 16359
673bfd45 16360 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16361}
16362
b4ba55a1
JB
16363/* True iff CU's producer generates GNAT Ada auxiliary information
16364 that allows to find parallel types through that information instead
16365 of having to do expensive parallel lookups by type name. */
16366
16367static int
16368need_gnat_info (struct dwarf2_cu *cu)
16369{
16370 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16371 of GNAT produces this auxiliary information, without any indication
16372 that it is produced. Part of enhancing the FSF version of GNAT
16373 to produce that information will be to put in place an indicator
16374 that we can use in order to determine whether the descriptive type
16375 info is available or not. One suggestion that has been made is
16376 to use a new attribute, attached to the CU die. For now, assume
16377 that the descriptive type info is not available. */
16378 return 0;
16379}
16380
b4ba55a1
JB
16381/* Return the auxiliary type of the die in question using its
16382 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16383 attribute is not present. */
16384
16385static struct type *
16386die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16387{
b4ba55a1 16388 struct attribute *type_attr;
b4ba55a1
JB
16389
16390 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16391 if (!type_attr)
16392 return NULL;
16393
673bfd45 16394 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16395}
16396
16397/* If DIE has a descriptive_type attribute, then set the TYPE's
16398 descriptive type accordingly. */
16399
16400static void
16401set_descriptive_type (struct type *type, struct die_info *die,
16402 struct dwarf2_cu *cu)
16403{
16404 struct type *descriptive_type = die_descriptive_type (die, cu);
16405
16406 if (descriptive_type)
16407 {
16408 ALLOCATE_GNAT_AUX_TYPE (type);
16409 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16410 }
16411}
16412
c906108c
SS
16413/* Return the containing type of the die in question using its
16414 DW_AT_containing_type attribute. */
16415
16416static struct type *
e7c27a73 16417die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16418{
c906108c 16419 struct attribute *type_attr;
c906108c 16420
e142c38c 16421 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16422 if (!type_attr)
16423 error (_("Dwarf Error: Problem turning containing type into gdb type "
16424 "[in module %s]"), cu->objfile->name);
16425
673bfd45 16426 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16427}
16428
673bfd45
DE
16429/* Look up the type of DIE in CU using its type attribute ATTR.
16430 If there is no type substitute an error marker. */
16431
c906108c 16432static struct type *
673bfd45
DE
16433lookup_die_type (struct die_info *die, struct attribute *attr,
16434 struct dwarf2_cu *cu)
c906108c 16435{
bb5ed363 16436 struct objfile *objfile = cu->objfile;
f792889a
DJ
16437 struct type *this_type;
16438
673bfd45
DE
16439 /* First see if we have it cached. */
16440
36586728
TT
16441 if (attr->form == DW_FORM_GNU_ref_alt)
16442 {
16443 struct dwarf2_per_cu_data *per_cu;
16444 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16445
16446 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16447 this_type = get_die_type_at_offset (offset, per_cu);
16448 }
16449 else if (is_ref_attr (attr))
673bfd45 16450 {
b64f50a1 16451 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16452
16453 this_type = get_die_type_at_offset (offset, cu->per_cu);
16454 }
55f1336d 16455 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16456 {
16457 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16458
16459 /* sig_type will be NULL if the signatured type is missing from
16460 the debug info. */
16461 if (sig_type == NULL)
16462 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16463 "at 0x%x [in module %s]"),
b64f50a1 16464 die->offset.sect_off, objfile->name);
673bfd45 16465
3019eac3
DE
16466 gdb_assert (sig_type->per_cu.is_debug_types);
16467 /* If we haven't filled in type_offset_in_section yet, then we
16468 haven't read the type in yet. */
16469 this_type = NULL;
16470 if (sig_type->type_offset_in_section.sect_off != 0)
16471 {
16472 this_type =
16473 get_die_type_at_offset (sig_type->type_offset_in_section,
16474 &sig_type->per_cu);
16475 }
673bfd45
DE
16476 }
16477 else
16478 {
16479 dump_die_for_error (die);
16480 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16481 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16482 }
16483
16484 /* If not cached we need to read it in. */
16485
16486 if (this_type == NULL)
16487 {
16488 struct die_info *type_die;
16489 struct dwarf2_cu *type_cu = cu;
16490
16491 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16492 /* If we found the type now, it's probably because the type came
16493 from an inter-CU reference and the type's CU got expanded before
16494 ours. */
16495 this_type = get_die_type (type_die, type_cu);
16496 if (this_type == NULL)
16497 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16498 }
16499
16500 /* If we still don't have a type use an error marker. */
16501
16502 if (this_type == NULL)
c906108c 16503 {
b00fdb78
TT
16504 char *message, *saved;
16505
16506 /* read_type_die already issued a complaint. */
16507 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16508 objfile->name,
b64f50a1
JK
16509 cu->header.offset.sect_off,
16510 die->offset.sect_off);
bb5ed363 16511 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16512 message, strlen (message));
16513 xfree (message);
16514
bb5ed363 16515 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16516 }
673bfd45 16517
f792889a 16518 return this_type;
c906108c
SS
16519}
16520
673bfd45
DE
16521/* Return the type in DIE, CU.
16522 Returns NULL for invalid types.
16523
16524 This first does a lookup in the appropriate type_hash table,
16525 and only reads the die in if necessary.
16526
16527 NOTE: This can be called when reading in partial or full symbols. */
16528
f792889a 16529static struct type *
e7c27a73 16530read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16531{
f792889a
DJ
16532 struct type *this_type;
16533
16534 this_type = get_die_type (die, cu);
16535 if (this_type)
16536 return this_type;
16537
673bfd45
DE
16538 return read_type_die_1 (die, cu);
16539}
16540
16541/* Read the type in DIE, CU.
16542 Returns NULL for invalid types. */
16543
16544static struct type *
16545read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16546{
16547 struct type *this_type = NULL;
16548
c906108c
SS
16549 switch (die->tag)
16550 {
16551 case DW_TAG_class_type:
680b30c7 16552 case DW_TAG_interface_type:
c906108c
SS
16553 case DW_TAG_structure_type:
16554 case DW_TAG_union_type:
f792889a 16555 this_type = read_structure_type (die, cu);
c906108c
SS
16556 break;
16557 case DW_TAG_enumeration_type:
f792889a 16558 this_type = read_enumeration_type (die, cu);
c906108c
SS
16559 break;
16560 case DW_TAG_subprogram:
16561 case DW_TAG_subroutine_type:
edb3359d 16562 case DW_TAG_inlined_subroutine:
f792889a 16563 this_type = read_subroutine_type (die, cu);
c906108c
SS
16564 break;
16565 case DW_TAG_array_type:
f792889a 16566 this_type = read_array_type (die, cu);
c906108c 16567 break;
72019c9c 16568 case DW_TAG_set_type:
f792889a 16569 this_type = read_set_type (die, cu);
72019c9c 16570 break;
c906108c 16571 case DW_TAG_pointer_type:
f792889a 16572 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16573 break;
16574 case DW_TAG_ptr_to_member_type:
f792889a 16575 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16576 break;
16577 case DW_TAG_reference_type:
f792889a 16578 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16579 break;
16580 case DW_TAG_const_type:
f792889a 16581 this_type = read_tag_const_type (die, cu);
c906108c
SS
16582 break;
16583 case DW_TAG_volatile_type:
f792889a 16584 this_type = read_tag_volatile_type (die, cu);
c906108c 16585 break;
06d66ee9
TT
16586 case DW_TAG_restrict_type:
16587 this_type = read_tag_restrict_type (die, cu);
16588 break;
c906108c 16589 case DW_TAG_string_type:
f792889a 16590 this_type = read_tag_string_type (die, cu);
c906108c
SS
16591 break;
16592 case DW_TAG_typedef:
f792889a 16593 this_type = read_typedef (die, cu);
c906108c 16594 break;
a02abb62 16595 case DW_TAG_subrange_type:
f792889a 16596 this_type = read_subrange_type (die, cu);
a02abb62 16597 break;
c906108c 16598 case DW_TAG_base_type:
f792889a 16599 this_type = read_base_type (die, cu);
c906108c 16600 break;
81a17f79 16601 case DW_TAG_unspecified_type:
f792889a 16602 this_type = read_unspecified_type (die, cu);
81a17f79 16603 break;
0114d602
DJ
16604 case DW_TAG_namespace:
16605 this_type = read_namespace_type (die, cu);
16606 break;
f55ee35c
JK
16607 case DW_TAG_module:
16608 this_type = read_module_type (die, cu);
16609 break;
c906108c 16610 default:
3e43a32a
MS
16611 complaint (&symfile_complaints,
16612 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16613 dwarf_tag_name (die->tag));
c906108c
SS
16614 break;
16615 }
63d06c5c 16616
f792889a 16617 return this_type;
63d06c5c
DC
16618}
16619
abc72ce4
DE
16620/* See if we can figure out if the class lives in a namespace. We do
16621 this by looking for a member function; its demangled name will
16622 contain namespace info, if there is any.
16623 Return the computed name or NULL.
16624 Space for the result is allocated on the objfile's obstack.
16625 This is the full-die version of guess_partial_die_structure_name.
16626 In this case we know DIE has no useful parent. */
16627
16628static char *
16629guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16630{
16631 struct die_info *spec_die;
16632 struct dwarf2_cu *spec_cu;
16633 struct die_info *child;
16634
16635 spec_cu = cu;
16636 spec_die = die_specification (die, &spec_cu);
16637 if (spec_die != NULL)
16638 {
16639 die = spec_die;
16640 cu = spec_cu;
16641 }
16642
16643 for (child = die->child;
16644 child != NULL;
16645 child = child->sibling)
16646 {
16647 if (child->tag == DW_TAG_subprogram)
16648 {
16649 struct attribute *attr;
16650
16651 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16652 if (attr == NULL)
16653 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16654 if (attr != NULL)
16655 {
16656 char *actual_name
16657 = language_class_name_from_physname (cu->language_defn,
16658 DW_STRING (attr));
16659 char *name = NULL;
16660
16661 if (actual_name != NULL)
16662 {
15d034d0 16663 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16664
16665 if (die_name != NULL
16666 && strcmp (die_name, actual_name) != 0)
16667 {
16668 /* Strip off the class name from the full name.
16669 We want the prefix. */
16670 int die_name_len = strlen (die_name);
16671 int actual_name_len = strlen (actual_name);
16672
16673 /* Test for '::' as a sanity check. */
16674 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16675 && actual_name[actual_name_len
16676 - die_name_len - 1] == ':')
abc72ce4 16677 name =
10f0c4bb
TT
16678 obstack_copy0 (&cu->objfile->objfile_obstack,
16679 actual_name,
16680 actual_name_len - die_name_len - 2);
abc72ce4
DE
16681 }
16682 }
16683 xfree (actual_name);
16684 return name;
16685 }
16686 }
16687 }
16688
16689 return NULL;
16690}
16691
96408a79
SA
16692/* GCC might emit a nameless typedef that has a linkage name. Determine the
16693 prefix part in such case. See
16694 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16695
16696static char *
16697anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16698{
16699 struct attribute *attr;
16700 char *base;
16701
16702 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16703 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16704 return NULL;
16705
16706 attr = dwarf2_attr (die, DW_AT_name, cu);
16707 if (attr != NULL && DW_STRING (attr) != NULL)
16708 return NULL;
16709
16710 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16711 if (attr == NULL)
16712 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16713 if (attr == NULL || DW_STRING (attr) == NULL)
16714 return NULL;
16715
16716 /* dwarf2_name had to be already called. */
16717 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16718
16719 /* Strip the base name, keep any leading namespaces/classes. */
16720 base = strrchr (DW_STRING (attr), ':');
16721 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16722 return "";
16723
10f0c4bb
TT
16724 return obstack_copy0 (&cu->objfile->objfile_obstack,
16725 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16726}
16727
fdde2d81 16728/* Return the name of the namespace/class that DIE is defined within,
0114d602 16729 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16730
0114d602
DJ
16731 For example, if we're within the method foo() in the following
16732 code:
16733
16734 namespace N {
16735 class C {
16736 void foo () {
16737 }
16738 };
16739 }
16740
16741 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16742
0d5cff50 16743static const char *
e142c38c 16744determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16745{
0114d602
DJ
16746 struct die_info *parent, *spec_die;
16747 struct dwarf2_cu *spec_cu;
16748 struct type *parent_type;
96408a79 16749 char *retval;
63d06c5c 16750
f55ee35c
JK
16751 if (cu->language != language_cplus && cu->language != language_java
16752 && cu->language != language_fortran)
0114d602
DJ
16753 return "";
16754
96408a79
SA
16755 retval = anonymous_struct_prefix (die, cu);
16756 if (retval)
16757 return retval;
16758
0114d602
DJ
16759 /* We have to be careful in the presence of DW_AT_specification.
16760 For example, with GCC 3.4, given the code
16761
16762 namespace N {
16763 void foo() {
16764 // Definition of N::foo.
16765 }
16766 }
16767
16768 then we'll have a tree of DIEs like this:
16769
16770 1: DW_TAG_compile_unit
16771 2: DW_TAG_namespace // N
16772 3: DW_TAG_subprogram // declaration of N::foo
16773 4: DW_TAG_subprogram // definition of N::foo
16774 DW_AT_specification // refers to die #3
16775
16776 Thus, when processing die #4, we have to pretend that we're in
16777 the context of its DW_AT_specification, namely the contex of die
16778 #3. */
16779 spec_cu = cu;
16780 spec_die = die_specification (die, &spec_cu);
16781 if (spec_die == NULL)
16782 parent = die->parent;
16783 else
63d06c5c 16784 {
0114d602
DJ
16785 parent = spec_die->parent;
16786 cu = spec_cu;
63d06c5c 16787 }
0114d602
DJ
16788
16789 if (parent == NULL)
16790 return "";
98bfdba5
PA
16791 else if (parent->building_fullname)
16792 {
16793 const char *name;
16794 const char *parent_name;
16795
16796 /* It has been seen on RealView 2.2 built binaries,
16797 DW_TAG_template_type_param types actually _defined_ as
16798 children of the parent class:
16799
16800 enum E {};
16801 template class <class Enum> Class{};
16802 Class<enum E> class_e;
16803
16804 1: DW_TAG_class_type (Class)
16805 2: DW_TAG_enumeration_type (E)
16806 3: DW_TAG_enumerator (enum1:0)
16807 3: DW_TAG_enumerator (enum2:1)
16808 ...
16809 2: DW_TAG_template_type_param
16810 DW_AT_type DW_FORM_ref_udata (E)
16811
16812 Besides being broken debug info, it can put GDB into an
16813 infinite loop. Consider:
16814
16815 When we're building the full name for Class<E>, we'll start
16816 at Class, and go look over its template type parameters,
16817 finding E. We'll then try to build the full name of E, and
16818 reach here. We're now trying to build the full name of E,
16819 and look over the parent DIE for containing scope. In the
16820 broken case, if we followed the parent DIE of E, we'd again
16821 find Class, and once again go look at its template type
16822 arguments, etc., etc. Simply don't consider such parent die
16823 as source-level parent of this die (it can't be, the language
16824 doesn't allow it), and break the loop here. */
16825 name = dwarf2_name (die, cu);
16826 parent_name = dwarf2_name (parent, cu);
16827 complaint (&symfile_complaints,
16828 _("template param type '%s' defined within parent '%s'"),
16829 name ? name : "<unknown>",
16830 parent_name ? parent_name : "<unknown>");
16831 return "";
16832 }
63d06c5c 16833 else
0114d602
DJ
16834 switch (parent->tag)
16835 {
63d06c5c 16836 case DW_TAG_namespace:
0114d602 16837 parent_type = read_type_die (parent, cu);
acebe513
UW
16838 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16839 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16840 Work around this problem here. */
16841 if (cu->language == language_cplus
16842 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16843 return "";
0114d602
DJ
16844 /* We give a name to even anonymous namespaces. */
16845 return TYPE_TAG_NAME (parent_type);
63d06c5c 16846 case DW_TAG_class_type:
680b30c7 16847 case DW_TAG_interface_type:
63d06c5c 16848 case DW_TAG_structure_type:
0114d602 16849 case DW_TAG_union_type:
f55ee35c 16850 case DW_TAG_module:
0114d602
DJ
16851 parent_type = read_type_die (parent, cu);
16852 if (TYPE_TAG_NAME (parent_type) != NULL)
16853 return TYPE_TAG_NAME (parent_type);
16854 else
16855 /* An anonymous structure is only allowed non-static data
16856 members; no typedefs, no member functions, et cetera.
16857 So it does not need a prefix. */
16858 return "";
abc72ce4 16859 case DW_TAG_compile_unit:
95554aad 16860 case DW_TAG_partial_unit:
abc72ce4
DE
16861 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16862 if (cu->language == language_cplus
8b70b953 16863 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16864 && die->child != NULL
16865 && (die->tag == DW_TAG_class_type
16866 || die->tag == DW_TAG_structure_type
16867 || die->tag == DW_TAG_union_type))
16868 {
16869 char *name = guess_full_die_structure_name (die, cu);
16870 if (name != NULL)
16871 return name;
16872 }
16873 return "";
63d06c5c 16874 default:
8176b9b8 16875 return determine_prefix (parent, cu);
63d06c5c 16876 }
63d06c5c
DC
16877}
16878
3e43a32a
MS
16879/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16880 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16881 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16882 an obconcat, otherwise allocate storage for the result. The CU argument is
16883 used to determine the language and hence, the appropriate separator. */
987504bb 16884
f55ee35c 16885#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16886
16887static char *
f55ee35c
JK
16888typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16889 int physname, struct dwarf2_cu *cu)
63d06c5c 16890{
f55ee35c 16891 const char *lead = "";
5c315b68 16892 const char *sep;
63d06c5c 16893
3e43a32a
MS
16894 if (suffix == NULL || suffix[0] == '\0'
16895 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16896 sep = "";
16897 else if (cu->language == language_java)
16898 sep = ".";
f55ee35c
JK
16899 else if (cu->language == language_fortran && physname)
16900 {
16901 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16902 DW_AT_MIPS_linkage_name is preferred and used instead. */
16903
16904 lead = "__";
16905 sep = "_MOD_";
16906 }
987504bb
JJ
16907 else
16908 sep = "::";
63d06c5c 16909
6dd47d34
DE
16910 if (prefix == NULL)
16911 prefix = "";
16912 if (suffix == NULL)
16913 suffix = "";
16914
987504bb
JJ
16915 if (obs == NULL)
16916 {
3e43a32a
MS
16917 char *retval
16918 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16919
f55ee35c
JK
16920 strcpy (retval, lead);
16921 strcat (retval, prefix);
6dd47d34
DE
16922 strcat (retval, sep);
16923 strcat (retval, suffix);
63d06c5c
DC
16924 return retval;
16925 }
987504bb
JJ
16926 else
16927 {
16928 /* We have an obstack. */
f55ee35c 16929 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16930 }
63d06c5c
DC
16931}
16932
c906108c
SS
16933/* Return sibling of die, NULL if no sibling. */
16934
f9aca02d 16935static struct die_info *
fba45db2 16936sibling_die (struct die_info *die)
c906108c 16937{
639d11d3 16938 return die->sibling;
c906108c
SS
16939}
16940
71c25dea
TT
16941/* Get name of a die, return NULL if not found. */
16942
15d034d0
TT
16943static const char *
16944dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
16945 struct obstack *obstack)
16946{
16947 if (name && cu->language == language_cplus)
16948 {
16949 char *canon_name = cp_canonicalize_string (name);
16950
16951 if (canon_name != NULL)
16952 {
16953 if (strcmp (canon_name, name) != 0)
10f0c4bb 16954 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
16955 xfree (canon_name);
16956 }
16957 }
16958
16959 return name;
c906108c
SS
16960}
16961
9219021c
DC
16962/* Get name of a die, return NULL if not found. */
16963
15d034d0 16964static const char *
e142c38c 16965dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16966{
16967 struct attribute *attr;
16968
e142c38c 16969 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16970 if ((!attr || !DW_STRING (attr))
16971 && die->tag != DW_TAG_class_type
16972 && die->tag != DW_TAG_interface_type
16973 && die->tag != DW_TAG_structure_type
16974 && die->tag != DW_TAG_union_type)
71c25dea
TT
16975 return NULL;
16976
16977 switch (die->tag)
16978 {
16979 case DW_TAG_compile_unit:
95554aad 16980 case DW_TAG_partial_unit:
71c25dea
TT
16981 /* Compilation units have a DW_AT_name that is a filename, not
16982 a source language identifier. */
16983 case DW_TAG_enumeration_type:
16984 case DW_TAG_enumerator:
16985 /* These tags always have simple identifiers already; no need
16986 to canonicalize them. */
16987 return DW_STRING (attr);
907af001 16988
418835cc
KS
16989 case DW_TAG_subprogram:
16990 /* Java constructors will all be named "<init>", so return
16991 the class name when we see this special case. */
16992 if (cu->language == language_java
16993 && DW_STRING (attr) != NULL
16994 && strcmp (DW_STRING (attr), "<init>") == 0)
16995 {
16996 struct dwarf2_cu *spec_cu = cu;
16997 struct die_info *spec_die;
16998
16999 /* GCJ will output '<init>' for Java constructor names.
17000 For this special case, return the name of the parent class. */
17001
17002 /* GCJ may output suprogram DIEs with AT_specification set.
17003 If so, use the name of the specified DIE. */
17004 spec_die = die_specification (die, &spec_cu);
17005 if (spec_die != NULL)
17006 return dwarf2_name (spec_die, spec_cu);
17007
17008 do
17009 {
17010 die = die->parent;
17011 if (die->tag == DW_TAG_class_type)
17012 return dwarf2_name (die, cu);
17013 }
95554aad
TT
17014 while (die->tag != DW_TAG_compile_unit
17015 && die->tag != DW_TAG_partial_unit);
418835cc 17016 }
907af001
UW
17017 break;
17018
17019 case DW_TAG_class_type:
17020 case DW_TAG_interface_type:
17021 case DW_TAG_structure_type:
17022 case DW_TAG_union_type:
17023 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17024 structures or unions. These were of the form "._%d" in GCC 4.1,
17025 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17026 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17027 if (attr && DW_STRING (attr)
17028 && (strncmp (DW_STRING (attr), "._", 2) == 0
17029 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17030 return NULL;
53832f31
TT
17031
17032 /* GCC might emit a nameless typedef that has a linkage name. See
17033 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17034 if (!attr || DW_STRING (attr) == NULL)
17035 {
df5c6c50 17036 char *demangled = NULL;
53832f31
TT
17037
17038 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17039 if (attr == NULL)
17040 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17041
17042 if (attr == NULL || DW_STRING (attr) == NULL)
17043 return NULL;
17044
df5c6c50
JK
17045 /* Avoid demangling DW_STRING (attr) the second time on a second
17046 call for the same DIE. */
17047 if (!DW_STRING_IS_CANONICAL (attr))
17048 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17049
17050 if (demangled)
17051 {
96408a79
SA
17052 char *base;
17053
53832f31 17054 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17055 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17056 demangled, strlen (demangled));
53832f31
TT
17057 DW_STRING_IS_CANONICAL (attr) = 1;
17058 xfree (demangled);
96408a79
SA
17059
17060 /* Strip any leading namespaces/classes, keep only the base name.
17061 DW_AT_name for named DIEs does not contain the prefixes. */
17062 base = strrchr (DW_STRING (attr), ':');
17063 if (base && base > DW_STRING (attr) && base[-1] == ':')
17064 return &base[1];
17065 else
17066 return DW_STRING (attr);
53832f31
TT
17067 }
17068 }
907af001
UW
17069 break;
17070
71c25dea 17071 default:
907af001
UW
17072 break;
17073 }
17074
17075 if (!DW_STRING_IS_CANONICAL (attr))
17076 {
17077 DW_STRING (attr)
17078 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17079 &cu->objfile->objfile_obstack);
17080 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17081 }
907af001 17082 return DW_STRING (attr);
9219021c
DC
17083}
17084
17085/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17086 is none. *EXT_CU is the CU containing DIE on input, and the CU
17087 containing the return value on output. */
9219021c
DC
17088
17089static struct die_info *
f2f0e013 17090dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17091{
17092 struct attribute *attr;
9219021c 17093
f2f0e013 17094 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17095 if (attr == NULL)
17096 return NULL;
17097
f2f0e013 17098 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17099}
17100
c906108c
SS
17101/* Convert a DIE tag into its string name. */
17102
f39c6ffd 17103static const char *
aa1ee363 17104dwarf_tag_name (unsigned tag)
c906108c 17105{
f39c6ffd
TT
17106 const char *name = get_DW_TAG_name (tag);
17107
17108 if (name == NULL)
17109 return "DW_TAG_<unknown>";
17110
17111 return name;
c906108c
SS
17112}
17113
17114/* Convert a DWARF attribute code into its string name. */
17115
f39c6ffd 17116static const char *
aa1ee363 17117dwarf_attr_name (unsigned attr)
c906108c 17118{
f39c6ffd
TT
17119 const char *name;
17120
c764a876 17121#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17122 if (attr == DW_AT_MIPS_fde)
17123 return "DW_AT_MIPS_fde";
17124#else
17125 if (attr == DW_AT_HP_block_index)
17126 return "DW_AT_HP_block_index";
c764a876 17127#endif
f39c6ffd
TT
17128
17129 name = get_DW_AT_name (attr);
17130
17131 if (name == NULL)
17132 return "DW_AT_<unknown>";
17133
17134 return name;
c906108c
SS
17135}
17136
17137/* Convert a DWARF value form code into its string name. */
17138
f39c6ffd 17139static const char *
aa1ee363 17140dwarf_form_name (unsigned form)
c906108c 17141{
f39c6ffd
TT
17142 const char *name = get_DW_FORM_name (form);
17143
17144 if (name == NULL)
17145 return "DW_FORM_<unknown>";
17146
17147 return name;
c906108c
SS
17148}
17149
17150static char *
fba45db2 17151dwarf_bool_name (unsigned mybool)
c906108c
SS
17152{
17153 if (mybool)
17154 return "TRUE";
17155 else
17156 return "FALSE";
17157}
17158
17159/* Convert a DWARF type code into its string name. */
17160
f39c6ffd 17161static const char *
aa1ee363 17162dwarf_type_encoding_name (unsigned enc)
c906108c 17163{
f39c6ffd 17164 const char *name = get_DW_ATE_name (enc);
c906108c 17165
f39c6ffd
TT
17166 if (name == NULL)
17167 return "DW_ATE_<unknown>";
c906108c 17168
f39c6ffd 17169 return name;
c906108c 17170}
c906108c 17171
f9aca02d 17172static void
d97bc12b 17173dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17174{
17175 unsigned int i;
17176
d97bc12b
DE
17177 print_spaces (indent, f);
17178 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17179 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17180
17181 if (die->parent != NULL)
17182 {
17183 print_spaces (indent, f);
17184 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17185 die->parent->offset.sect_off);
d97bc12b
DE
17186 }
17187
17188 print_spaces (indent, f);
17189 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17190 dwarf_bool_name (die->child != NULL));
c906108c 17191
d97bc12b
DE
17192 print_spaces (indent, f);
17193 fprintf_unfiltered (f, " attributes:\n");
17194
c906108c
SS
17195 for (i = 0; i < die->num_attrs; ++i)
17196 {
d97bc12b
DE
17197 print_spaces (indent, f);
17198 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17199 dwarf_attr_name (die->attrs[i].name),
17200 dwarf_form_name (die->attrs[i].form));
d97bc12b 17201
c906108c
SS
17202 switch (die->attrs[i].form)
17203 {
c906108c 17204 case DW_FORM_addr:
3019eac3 17205 case DW_FORM_GNU_addr_index:
d97bc12b 17206 fprintf_unfiltered (f, "address: ");
5af949e3 17207 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17208 break;
17209 case DW_FORM_block2:
17210 case DW_FORM_block4:
17211 case DW_FORM_block:
17212 case DW_FORM_block1:
56eb65bd
SP
17213 fprintf_unfiltered (f, "block: size %s",
17214 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17215 break;
2dc7f7b3 17216 case DW_FORM_exprloc:
56eb65bd
SP
17217 fprintf_unfiltered (f, "expression: size %s",
17218 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17219 break;
4568ecf9
DE
17220 case DW_FORM_ref_addr:
17221 fprintf_unfiltered (f, "ref address: ");
17222 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17223 break;
36586728
TT
17224 case DW_FORM_GNU_ref_alt:
17225 fprintf_unfiltered (f, "alt ref address: ");
17226 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17227 break;
10b3939b
DJ
17228 case DW_FORM_ref1:
17229 case DW_FORM_ref2:
17230 case DW_FORM_ref4:
4568ecf9
DE
17231 case DW_FORM_ref8:
17232 case DW_FORM_ref_udata:
d97bc12b 17233 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17234 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17235 break;
c906108c
SS
17236 case DW_FORM_data1:
17237 case DW_FORM_data2:
17238 case DW_FORM_data4:
ce5d95e1 17239 case DW_FORM_data8:
c906108c
SS
17240 case DW_FORM_udata:
17241 case DW_FORM_sdata:
43bbcdc2
PH
17242 fprintf_unfiltered (f, "constant: %s",
17243 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17244 break;
2dc7f7b3
TT
17245 case DW_FORM_sec_offset:
17246 fprintf_unfiltered (f, "section offset: %s",
17247 pulongest (DW_UNSND (&die->attrs[i])));
17248 break;
55f1336d 17249 case DW_FORM_ref_sig8:
348e048f
DE
17250 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17251 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17252 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17253 else
17254 fprintf_unfiltered (f, "signatured type, offset: unknown");
17255 break;
c906108c 17256 case DW_FORM_string:
4bdf3d34 17257 case DW_FORM_strp:
3019eac3 17258 case DW_FORM_GNU_str_index:
36586728 17259 case DW_FORM_GNU_strp_alt:
8285870a 17260 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17261 DW_STRING (&die->attrs[i])
8285870a
JK
17262 ? DW_STRING (&die->attrs[i]) : "",
17263 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17264 break;
17265 case DW_FORM_flag:
17266 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17267 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17268 else
d97bc12b 17269 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17270 break;
2dc7f7b3
TT
17271 case DW_FORM_flag_present:
17272 fprintf_unfiltered (f, "flag: TRUE");
17273 break;
a8329558 17274 case DW_FORM_indirect:
0963b4bd
MS
17275 /* The reader will have reduced the indirect form to
17276 the "base form" so this form should not occur. */
3e43a32a
MS
17277 fprintf_unfiltered (f,
17278 "unexpected attribute form: DW_FORM_indirect");
a8329558 17279 break;
c906108c 17280 default:
d97bc12b 17281 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17282 die->attrs[i].form);
d97bc12b 17283 break;
c906108c 17284 }
d97bc12b 17285 fprintf_unfiltered (f, "\n");
c906108c
SS
17286 }
17287}
17288
f9aca02d 17289static void
d97bc12b 17290dump_die_for_error (struct die_info *die)
c906108c 17291{
d97bc12b
DE
17292 dump_die_shallow (gdb_stderr, 0, die);
17293}
17294
17295static void
17296dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17297{
17298 int indent = level * 4;
17299
17300 gdb_assert (die != NULL);
17301
17302 if (level >= max_level)
17303 return;
17304
17305 dump_die_shallow (f, indent, die);
17306
17307 if (die->child != NULL)
c906108c 17308 {
d97bc12b
DE
17309 print_spaces (indent, f);
17310 fprintf_unfiltered (f, " Children:");
17311 if (level + 1 < max_level)
17312 {
17313 fprintf_unfiltered (f, "\n");
17314 dump_die_1 (f, level + 1, max_level, die->child);
17315 }
17316 else
17317 {
3e43a32a
MS
17318 fprintf_unfiltered (f,
17319 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17320 }
17321 }
17322
17323 if (die->sibling != NULL && level > 0)
17324 {
17325 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17326 }
17327}
17328
d97bc12b
DE
17329/* This is called from the pdie macro in gdbinit.in.
17330 It's not static so gcc will keep a copy callable from gdb. */
17331
17332void
17333dump_die (struct die_info *die, int max_level)
17334{
17335 dump_die_1 (gdb_stdlog, 0, max_level, die);
17336}
17337
f9aca02d 17338static void
51545339 17339store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17340{
51545339 17341 void **slot;
c906108c 17342
b64f50a1
JK
17343 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17344 INSERT);
51545339
DJ
17345
17346 *slot = die;
c906108c
SS
17347}
17348
b64f50a1
JK
17349/* DW_ADDR is always stored already as sect_offset; despite for the forms
17350 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17351
93311388
DE
17352static int
17353is_ref_attr (struct attribute *attr)
c906108c 17354{
c906108c
SS
17355 switch (attr->form)
17356 {
17357 case DW_FORM_ref_addr:
c906108c
SS
17358 case DW_FORM_ref1:
17359 case DW_FORM_ref2:
17360 case DW_FORM_ref4:
613e1657 17361 case DW_FORM_ref8:
c906108c 17362 case DW_FORM_ref_udata:
36586728 17363 case DW_FORM_GNU_ref_alt:
93311388 17364 return 1;
c906108c 17365 default:
93311388 17366 return 0;
c906108c 17367 }
93311388
DE
17368}
17369
b64f50a1
JK
17370/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17371 required kind. */
17372
17373static sect_offset
93311388
DE
17374dwarf2_get_ref_die_offset (struct attribute *attr)
17375{
4568ecf9 17376 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17377
93311388 17378 if (is_ref_attr (attr))
b64f50a1 17379 return retval;
93311388 17380
b64f50a1 17381 retval.sect_off = 0;
93311388
DE
17382 complaint (&symfile_complaints,
17383 _("unsupported die ref attribute form: '%s'"),
17384 dwarf_form_name (attr->form));
b64f50a1 17385 return retval;
c906108c
SS
17386}
17387
43bbcdc2
PH
17388/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17389 * the value held by the attribute is not constant. */
a02abb62 17390
43bbcdc2 17391static LONGEST
a02abb62
JB
17392dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17393{
17394 if (attr->form == DW_FORM_sdata)
17395 return DW_SND (attr);
17396 else if (attr->form == DW_FORM_udata
17397 || attr->form == DW_FORM_data1
17398 || attr->form == DW_FORM_data2
17399 || attr->form == DW_FORM_data4
17400 || attr->form == DW_FORM_data8)
17401 return DW_UNSND (attr);
17402 else
17403 {
3e43a32a
MS
17404 complaint (&symfile_complaints,
17405 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17406 dwarf_form_name (attr->form));
17407 return default_value;
17408 }
17409}
17410
348e048f
DE
17411/* Follow reference or signature attribute ATTR of SRC_DIE.
17412 On entry *REF_CU is the CU of SRC_DIE.
17413 On exit *REF_CU is the CU of the result. */
17414
17415static struct die_info *
17416follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17417 struct dwarf2_cu **ref_cu)
17418{
17419 struct die_info *die;
17420
17421 if (is_ref_attr (attr))
17422 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17423 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17424 die = follow_die_sig (src_die, attr, ref_cu);
17425 else
17426 {
17427 dump_die_for_error (src_die);
17428 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17429 (*ref_cu)->objfile->name);
17430 }
17431
17432 return die;
03dd20cc
DJ
17433}
17434
5c631832 17435/* Follow reference OFFSET.
673bfd45
DE
17436 On entry *REF_CU is the CU of the source die referencing OFFSET.
17437 On exit *REF_CU is the CU of the result.
17438 Returns NULL if OFFSET is invalid. */
f504f079 17439
f9aca02d 17440static struct die_info *
36586728
TT
17441follow_die_offset (sect_offset offset, int offset_in_dwz,
17442 struct dwarf2_cu **ref_cu)
c906108c 17443{
10b3939b 17444 struct die_info temp_die;
f2f0e013 17445 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17446
348e048f
DE
17447 gdb_assert (cu->per_cu != NULL);
17448
98bfdba5
PA
17449 target_cu = cu;
17450
3019eac3 17451 if (cu->per_cu->is_debug_types)
348e048f
DE
17452 {
17453 /* .debug_types CUs cannot reference anything outside their CU.
17454 If they need to, they have to reference a signatured type via
55f1336d 17455 DW_FORM_ref_sig8. */
348e048f 17456 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17457 return NULL;
348e048f 17458 }
36586728
TT
17459 else if (offset_in_dwz != cu->per_cu->is_dwz
17460 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17461 {
17462 struct dwarf2_per_cu_data *per_cu;
9a619af0 17463
36586728
TT
17464 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17465 cu->objfile);
03dd20cc
DJ
17466
17467 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17468 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17469 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17470
10b3939b
DJ
17471 target_cu = per_cu->cu;
17472 }
98bfdba5
PA
17473 else if (cu->dies == NULL)
17474 {
17475 /* We're loading full DIEs during partial symbol reading. */
17476 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17477 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17478 }
c906108c 17479
f2f0e013 17480 *ref_cu = target_cu;
51545339 17481 temp_die.offset = offset;
b64f50a1 17482 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17483}
10b3939b 17484
5c631832
JK
17485/* Follow reference attribute ATTR of SRC_DIE.
17486 On entry *REF_CU is the CU of SRC_DIE.
17487 On exit *REF_CU is the CU of the result. */
17488
17489static struct die_info *
17490follow_die_ref (struct die_info *src_die, struct attribute *attr,
17491 struct dwarf2_cu **ref_cu)
17492{
b64f50a1 17493 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17494 struct dwarf2_cu *cu = *ref_cu;
17495 struct die_info *die;
17496
36586728
TT
17497 die = follow_die_offset (offset,
17498 (attr->form == DW_FORM_GNU_ref_alt
17499 || cu->per_cu->is_dwz),
17500 ref_cu);
5c631832
JK
17501 if (!die)
17502 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17503 "at 0x%x [in module %s]"),
b64f50a1 17504 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17505
5c631832
JK
17506 return die;
17507}
17508
d83e736b
JK
17509/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17510 Returned value is intended for DW_OP_call*. Returned
17511 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17512
17513struct dwarf2_locexpr_baton
8b9737bf
TT
17514dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17515 struct dwarf2_per_cu_data *per_cu,
17516 CORE_ADDR (*get_frame_pc) (void *baton),
17517 void *baton)
5c631832 17518{
918dd910 17519 struct dwarf2_cu *cu;
5c631832
JK
17520 struct die_info *die;
17521 struct attribute *attr;
17522 struct dwarf2_locexpr_baton retval;
17523
8cf6f0b1
TT
17524 dw2_setup (per_cu->objfile);
17525
918dd910
JK
17526 if (per_cu->cu == NULL)
17527 load_cu (per_cu);
17528 cu = per_cu->cu;
17529
36586728 17530 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17531 if (!die)
17532 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17533 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17534
17535 attr = dwarf2_attr (die, DW_AT_location, cu);
17536 if (!attr)
17537 {
e103e986
JK
17538 /* DWARF: "If there is no such attribute, then there is no effect.".
17539 DATA is ignored if SIZE is 0. */
5c631832 17540
e103e986 17541 retval.data = NULL;
5c631832
JK
17542 retval.size = 0;
17543 }
8cf6f0b1
TT
17544 else if (attr_form_is_section_offset (attr))
17545 {
17546 struct dwarf2_loclist_baton loclist_baton;
17547 CORE_ADDR pc = (*get_frame_pc) (baton);
17548 size_t size;
17549
17550 fill_in_loclist_baton (cu, &loclist_baton, attr);
17551
17552 retval.data = dwarf2_find_location_expression (&loclist_baton,
17553 &size, pc);
17554 retval.size = size;
17555 }
5c631832
JK
17556 else
17557 {
17558 if (!attr_form_is_block (attr))
17559 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17560 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17561 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17562
17563 retval.data = DW_BLOCK (attr)->data;
17564 retval.size = DW_BLOCK (attr)->size;
17565 }
17566 retval.per_cu = cu->per_cu;
918dd910 17567
918dd910
JK
17568 age_cached_comp_units ();
17569
5c631832 17570 return retval;
348e048f
DE
17571}
17572
8b9737bf
TT
17573/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17574 offset. */
17575
17576struct dwarf2_locexpr_baton
17577dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17578 struct dwarf2_per_cu_data *per_cu,
17579 CORE_ADDR (*get_frame_pc) (void *baton),
17580 void *baton)
17581{
17582 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17583
17584 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17585}
17586
8a9b8146
TT
17587/* Return the type of the DIE at DIE_OFFSET in the CU named by
17588 PER_CU. */
17589
17590struct type *
b64f50a1 17591dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17592 struct dwarf2_per_cu_data *per_cu)
17593{
b64f50a1
JK
17594 sect_offset die_offset_sect;
17595
8a9b8146 17596 dw2_setup (per_cu->objfile);
b64f50a1
JK
17597
17598 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17599 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17600}
17601
348e048f
DE
17602/* Follow the signature attribute ATTR in SRC_DIE.
17603 On entry *REF_CU is the CU of SRC_DIE.
17604 On exit *REF_CU is the CU of the result. */
17605
17606static struct die_info *
17607follow_die_sig (struct die_info *src_die, struct attribute *attr,
17608 struct dwarf2_cu **ref_cu)
17609{
17610 struct objfile *objfile = (*ref_cu)->objfile;
17611 struct die_info temp_die;
17612 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17613 struct dwarf2_cu *sig_cu;
17614 struct die_info *die;
17615
17616 /* sig_type will be NULL if the signatured type is missing from
17617 the debug info. */
17618 if (sig_type == NULL)
17619 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17620 "at 0x%x [in module %s]"),
b64f50a1 17621 src_die->offset.sect_off, objfile->name);
348e048f
DE
17622
17623 /* If necessary, add it to the queue and load its DIEs. */
17624
95554aad 17625 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17626 read_signatured_type (sig_type);
348e048f
DE
17627
17628 gdb_assert (sig_type->per_cu.cu != NULL);
17629
17630 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17631 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17632 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17633 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17634 temp_die.offset.sect_off);
348e048f
DE
17635 if (die)
17636 {
796a7ff8
DE
17637 /* For .gdb_index version 7 keep track of included TUs.
17638 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17639 if (dwarf2_per_objfile->index_table != NULL
17640 && dwarf2_per_objfile->index_table->version <= 7)
17641 {
17642 VEC_safe_push (dwarf2_per_cu_ptr,
17643 (*ref_cu)->per_cu->imported_symtabs,
17644 sig_cu->per_cu);
17645 }
17646
348e048f
DE
17647 *ref_cu = sig_cu;
17648 return die;
17649 }
17650
3e43a32a
MS
17651 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17652 "from DIE at 0x%x [in module %s]"),
b64f50a1 17653 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17654}
17655
17656/* Given an offset of a signatured type, return its signatured_type. */
17657
17658static struct signatured_type *
8b70b953
TT
17659lookup_signatured_type_at_offset (struct objfile *objfile,
17660 struct dwarf2_section_info *section,
b64f50a1 17661 sect_offset offset)
348e048f 17662{
b64f50a1 17663 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17664 unsigned int length, initial_length_size;
17665 unsigned int sig_offset;
52dc124a 17666 struct signatured_type find_entry, *sig_type;
348e048f
DE
17667
17668 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17669 sig_offset = (initial_length_size
17670 + 2 /*version*/
17671 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17672 + 1 /*address_size*/);
17673 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17674 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17675
17676 /* This is only used to lookup previously recorded types.
17677 If we didn't find it, it's our bug. */
52dc124a
DE
17678 gdb_assert (sig_type != NULL);
17679 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17680
52dc124a 17681 return sig_type;
348e048f
DE
17682}
17683
e5fe5e75 17684/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17685
17686static void
e5fe5e75 17687load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17688{
52dc124a 17689 struct signatured_type *sig_type;
348e048f 17690
f4dc4d17
DE
17691 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17692 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17693
6721b2ec
DE
17694 /* We have the per_cu, but we need the signatured_type.
17695 Fortunately this is an easy translation. */
17696 gdb_assert (per_cu->is_debug_types);
17697 sig_type = (struct signatured_type *) per_cu;
348e048f 17698
6721b2ec 17699 gdb_assert (per_cu->cu == NULL);
348e048f 17700
52dc124a 17701 read_signatured_type (sig_type);
348e048f 17702
6721b2ec 17703 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17704}
17705
dee91e82
DE
17706/* die_reader_func for read_signatured_type.
17707 This is identical to load_full_comp_unit_reader,
17708 but is kept separate for now. */
348e048f
DE
17709
17710static void
dee91e82
DE
17711read_signatured_type_reader (const struct die_reader_specs *reader,
17712 gdb_byte *info_ptr,
17713 struct die_info *comp_unit_die,
17714 int has_children,
17715 void *data)
348e048f 17716{
dee91e82 17717 struct dwarf2_cu *cu = reader->cu;
348e048f 17718
dee91e82
DE
17719 gdb_assert (cu->die_hash == NULL);
17720 cu->die_hash =
17721 htab_create_alloc_ex (cu->header.length / 12,
17722 die_hash,
17723 die_eq,
17724 NULL,
17725 &cu->comp_unit_obstack,
17726 hashtab_obstack_allocate,
17727 dummy_obstack_deallocate);
348e048f 17728
dee91e82
DE
17729 if (has_children)
17730 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17731 &info_ptr, comp_unit_die);
17732 cu->dies = comp_unit_die;
17733 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17734
17735 /* We try not to read any attributes in this function, because not
9cdd5dbd 17736 all CUs needed for references have been loaded yet, and symbol
348e048f 17737 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17738 or we won't be able to build types correctly.
17739 Similarly, if we do not read the producer, we can not apply
17740 producer-specific interpretation. */
95554aad 17741 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17742}
348e048f 17743
3019eac3
DE
17744/* Read in a signatured type and build its CU and DIEs.
17745 If the type is a stub for the real type in a DWO file,
17746 read in the real type from the DWO file as well. */
dee91e82
DE
17747
17748static void
17749read_signatured_type (struct signatured_type *sig_type)
17750{
17751 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17752
3019eac3 17753 gdb_assert (per_cu->is_debug_types);
dee91e82 17754 gdb_assert (per_cu->cu == NULL);
348e048f 17755
f4dc4d17
DE
17756 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17757 read_signatured_type_reader, NULL);
c906108c
SS
17758}
17759
c906108c
SS
17760/* Decode simple location descriptions.
17761 Given a pointer to a dwarf block that defines a location, compute
17762 the location and return the value.
17763
4cecd739
DJ
17764 NOTE drow/2003-11-18: This function is called in two situations
17765 now: for the address of static or global variables (partial symbols
17766 only) and for offsets into structures which are expected to be
17767 (more or less) constant. The partial symbol case should go away,
17768 and only the constant case should remain. That will let this
17769 function complain more accurately. A few special modes are allowed
17770 without complaint for global variables (for instance, global
17771 register values and thread-local values).
c906108c
SS
17772
17773 A location description containing no operations indicates that the
4cecd739 17774 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17775 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17776 callers will only want a very basic result and this can become a
21ae7a4d
JK
17777 complaint.
17778
17779 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17780
17781static CORE_ADDR
e7c27a73 17782decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17783{
e7c27a73 17784 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17785 size_t i;
17786 size_t size = blk->size;
21ae7a4d
JK
17787 gdb_byte *data = blk->data;
17788 CORE_ADDR stack[64];
17789 int stacki;
17790 unsigned int bytes_read, unsnd;
17791 gdb_byte op;
c906108c 17792
21ae7a4d
JK
17793 i = 0;
17794 stacki = 0;
17795 stack[stacki] = 0;
17796 stack[++stacki] = 0;
17797
17798 while (i < size)
17799 {
17800 op = data[i++];
17801 switch (op)
17802 {
17803 case DW_OP_lit0:
17804 case DW_OP_lit1:
17805 case DW_OP_lit2:
17806 case DW_OP_lit3:
17807 case DW_OP_lit4:
17808 case DW_OP_lit5:
17809 case DW_OP_lit6:
17810 case DW_OP_lit7:
17811 case DW_OP_lit8:
17812 case DW_OP_lit9:
17813 case DW_OP_lit10:
17814 case DW_OP_lit11:
17815 case DW_OP_lit12:
17816 case DW_OP_lit13:
17817 case DW_OP_lit14:
17818 case DW_OP_lit15:
17819 case DW_OP_lit16:
17820 case DW_OP_lit17:
17821 case DW_OP_lit18:
17822 case DW_OP_lit19:
17823 case DW_OP_lit20:
17824 case DW_OP_lit21:
17825 case DW_OP_lit22:
17826 case DW_OP_lit23:
17827 case DW_OP_lit24:
17828 case DW_OP_lit25:
17829 case DW_OP_lit26:
17830 case DW_OP_lit27:
17831 case DW_OP_lit28:
17832 case DW_OP_lit29:
17833 case DW_OP_lit30:
17834 case DW_OP_lit31:
17835 stack[++stacki] = op - DW_OP_lit0;
17836 break;
f1bea926 17837
21ae7a4d
JK
17838 case DW_OP_reg0:
17839 case DW_OP_reg1:
17840 case DW_OP_reg2:
17841 case DW_OP_reg3:
17842 case DW_OP_reg4:
17843 case DW_OP_reg5:
17844 case DW_OP_reg6:
17845 case DW_OP_reg7:
17846 case DW_OP_reg8:
17847 case DW_OP_reg9:
17848 case DW_OP_reg10:
17849 case DW_OP_reg11:
17850 case DW_OP_reg12:
17851 case DW_OP_reg13:
17852 case DW_OP_reg14:
17853 case DW_OP_reg15:
17854 case DW_OP_reg16:
17855 case DW_OP_reg17:
17856 case DW_OP_reg18:
17857 case DW_OP_reg19:
17858 case DW_OP_reg20:
17859 case DW_OP_reg21:
17860 case DW_OP_reg22:
17861 case DW_OP_reg23:
17862 case DW_OP_reg24:
17863 case DW_OP_reg25:
17864 case DW_OP_reg26:
17865 case DW_OP_reg27:
17866 case DW_OP_reg28:
17867 case DW_OP_reg29:
17868 case DW_OP_reg30:
17869 case DW_OP_reg31:
17870 stack[++stacki] = op - DW_OP_reg0;
17871 if (i < size)
17872 dwarf2_complex_location_expr_complaint ();
17873 break;
c906108c 17874
21ae7a4d
JK
17875 case DW_OP_regx:
17876 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17877 i += bytes_read;
17878 stack[++stacki] = unsnd;
17879 if (i < size)
17880 dwarf2_complex_location_expr_complaint ();
17881 break;
c906108c 17882
21ae7a4d
JK
17883 case DW_OP_addr:
17884 stack[++stacki] = read_address (objfile->obfd, &data[i],
17885 cu, &bytes_read);
17886 i += bytes_read;
17887 break;
d53d4ac5 17888
21ae7a4d
JK
17889 case DW_OP_const1u:
17890 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17891 i += 1;
17892 break;
17893
17894 case DW_OP_const1s:
17895 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17896 i += 1;
17897 break;
17898
17899 case DW_OP_const2u:
17900 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17901 i += 2;
17902 break;
17903
17904 case DW_OP_const2s:
17905 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17906 i += 2;
17907 break;
d53d4ac5 17908
21ae7a4d
JK
17909 case DW_OP_const4u:
17910 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17911 i += 4;
17912 break;
17913
17914 case DW_OP_const4s:
17915 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17916 i += 4;
17917 break;
17918
585861ea
JK
17919 case DW_OP_const8u:
17920 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17921 i += 8;
17922 break;
17923
21ae7a4d
JK
17924 case DW_OP_constu:
17925 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17926 &bytes_read);
17927 i += bytes_read;
17928 break;
17929
17930 case DW_OP_consts:
17931 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17932 i += bytes_read;
17933 break;
17934
17935 case DW_OP_dup:
17936 stack[stacki + 1] = stack[stacki];
17937 stacki++;
17938 break;
17939
17940 case DW_OP_plus:
17941 stack[stacki - 1] += stack[stacki];
17942 stacki--;
17943 break;
17944
17945 case DW_OP_plus_uconst:
17946 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17947 &bytes_read);
17948 i += bytes_read;
17949 break;
17950
17951 case DW_OP_minus:
17952 stack[stacki - 1] -= stack[stacki];
17953 stacki--;
17954 break;
17955
17956 case DW_OP_deref:
17957 /* If we're not the last op, then we definitely can't encode
17958 this using GDB's address_class enum. This is valid for partial
17959 global symbols, although the variable's address will be bogus
17960 in the psymtab. */
17961 if (i < size)
17962 dwarf2_complex_location_expr_complaint ();
17963 break;
17964
17965 case DW_OP_GNU_push_tls_address:
17966 /* The top of the stack has the offset from the beginning
17967 of the thread control block at which the variable is located. */
17968 /* Nothing should follow this operator, so the top of stack would
17969 be returned. */
17970 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17971 address will be bogus in the psymtab. Make it always at least
17972 non-zero to not look as a variable garbage collected by linker
17973 which have DW_OP_addr 0. */
21ae7a4d
JK
17974 if (i < size)
17975 dwarf2_complex_location_expr_complaint ();
585861ea 17976 stack[stacki]++;
21ae7a4d
JK
17977 break;
17978
17979 case DW_OP_GNU_uninit:
17980 break;
17981
3019eac3 17982 case DW_OP_GNU_addr_index:
49f6c839 17983 case DW_OP_GNU_const_index:
3019eac3
DE
17984 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17985 &bytes_read);
17986 i += bytes_read;
17987 break;
17988
21ae7a4d
JK
17989 default:
17990 {
f39c6ffd 17991 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17992
17993 if (name)
17994 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17995 name);
17996 else
17997 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17998 op);
17999 }
18000
18001 return (stack[stacki]);
d53d4ac5 18002 }
3c6e0cb3 18003
21ae7a4d
JK
18004 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18005 outside of the allocated space. Also enforce minimum>0. */
18006 if (stacki >= ARRAY_SIZE (stack) - 1)
18007 {
18008 complaint (&symfile_complaints,
18009 _("location description stack overflow"));
18010 return 0;
18011 }
18012
18013 if (stacki <= 0)
18014 {
18015 complaint (&symfile_complaints,
18016 _("location description stack underflow"));
18017 return 0;
18018 }
18019 }
18020 return (stack[stacki]);
c906108c
SS
18021}
18022
18023/* memory allocation interface */
18024
c906108c 18025static struct dwarf_block *
7b5a2f43 18026dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18027{
18028 struct dwarf_block *blk;
18029
18030 blk = (struct dwarf_block *)
7b5a2f43 18031 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18032 return (blk);
18033}
18034
c906108c 18035static struct die_info *
b60c80d6 18036dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18037{
18038 struct die_info *die;
b60c80d6
DJ
18039 size_t size = sizeof (struct die_info);
18040
18041 if (num_attrs > 1)
18042 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18043
b60c80d6 18044 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18045 memset (die, 0, sizeof (struct die_info));
18046 return (die);
18047}
2e276125
JB
18048
18049\f
18050/* Macro support. */
18051
233d95b5
JK
18052/* Return file name relative to the compilation directory of file number I in
18053 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18054 responsible for freeing it. */
233d95b5 18055
2e276125 18056static char *
233d95b5 18057file_file_name (int file, struct line_header *lh)
2e276125 18058{
6a83a1e6
EZ
18059 /* Is the file number a valid index into the line header's file name
18060 table? Remember that file numbers start with one, not zero. */
18061 if (1 <= file && file <= lh->num_file_names)
18062 {
18063 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18064
233d95b5 18065 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18066 return xstrdup (fe->name);
233d95b5
JK
18067 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18068 fe->name, NULL);
6a83a1e6 18069 }
2e276125
JB
18070 else
18071 {
6a83a1e6
EZ
18072 /* The compiler produced a bogus file number. We can at least
18073 record the macro definitions made in the file, even if we
18074 won't be able to find the file by name. */
18075 char fake_name[80];
9a619af0 18076
8c042590
PM
18077 xsnprintf (fake_name, sizeof (fake_name),
18078 "<bad macro file number %d>", file);
2e276125 18079
6e70227d 18080 complaint (&symfile_complaints,
6a83a1e6
EZ
18081 _("bad file number in macro information (%d)"),
18082 file);
2e276125 18083
6a83a1e6 18084 return xstrdup (fake_name);
2e276125
JB
18085 }
18086}
18087
233d95b5
JK
18088/* Return the full name of file number I in *LH's file name table.
18089 Use COMP_DIR as the name of the current directory of the
18090 compilation. The result is allocated using xmalloc; the caller is
18091 responsible for freeing it. */
18092static char *
18093file_full_name (int file, struct line_header *lh, const char *comp_dir)
18094{
18095 /* Is the file number a valid index into the line header's file name
18096 table? Remember that file numbers start with one, not zero. */
18097 if (1 <= file && file <= lh->num_file_names)
18098 {
18099 char *relative = file_file_name (file, lh);
18100
18101 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18102 return relative;
18103 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18104 }
18105 else
18106 return file_file_name (file, lh);
18107}
18108
2e276125
JB
18109
18110static struct macro_source_file *
18111macro_start_file (int file, int line,
18112 struct macro_source_file *current_file,
18113 const char *comp_dir,
18114 struct line_header *lh, struct objfile *objfile)
18115{
233d95b5
JK
18116 /* File name relative to the compilation directory of this source file. */
18117 char *file_name = file_file_name (file, lh);
2e276125
JB
18118
18119 /* We don't create a macro table for this compilation unit
18120 at all until we actually get a filename. */
18121 if (! pending_macros)
6532ff36 18122 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18123 objfile->per_bfd->macro_cache,
18124 comp_dir);
2e276125
JB
18125
18126 if (! current_file)
abc9d0dc
TT
18127 {
18128 /* If we have no current file, then this must be the start_file
18129 directive for the compilation unit's main source file. */
233d95b5 18130 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18131 macro_define_special (pending_macros);
18132 }
2e276125 18133 else
233d95b5 18134 current_file = macro_include (current_file, line, file_name);
2e276125 18135
233d95b5 18136 xfree (file_name);
6e70227d 18137
2e276125
JB
18138 return current_file;
18139}
18140
18141
18142/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18143 followed by a null byte. */
18144static char *
18145copy_string (const char *buf, int len)
18146{
18147 char *s = xmalloc (len + 1);
9a619af0 18148
2e276125
JB
18149 memcpy (s, buf, len);
18150 s[len] = '\0';
2e276125
JB
18151 return s;
18152}
18153
18154
18155static const char *
18156consume_improper_spaces (const char *p, const char *body)
18157{
18158 if (*p == ' ')
18159 {
4d3c2250 18160 complaint (&symfile_complaints,
3e43a32a
MS
18161 _("macro definition contains spaces "
18162 "in formal argument list:\n`%s'"),
4d3c2250 18163 body);
2e276125
JB
18164
18165 while (*p == ' ')
18166 p++;
18167 }
18168
18169 return p;
18170}
18171
18172
18173static void
18174parse_macro_definition (struct macro_source_file *file, int line,
18175 const char *body)
18176{
18177 const char *p;
18178
18179 /* The body string takes one of two forms. For object-like macro
18180 definitions, it should be:
18181
18182 <macro name> " " <definition>
18183
18184 For function-like macro definitions, it should be:
18185
18186 <macro name> "() " <definition>
18187 or
18188 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18189
18190 Spaces may appear only where explicitly indicated, and in the
18191 <definition>.
18192
18193 The Dwarf 2 spec says that an object-like macro's name is always
18194 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18195 the space when the macro's definition is the empty string.
2e276125
JB
18196
18197 The Dwarf 2 spec says that there should be no spaces between the
18198 formal arguments in a function-like macro's formal argument list,
18199 but versions of GCC around March 2002 include spaces after the
18200 commas. */
18201
18202
18203 /* Find the extent of the macro name. The macro name is terminated
18204 by either a space or null character (for an object-like macro) or
18205 an opening paren (for a function-like macro). */
18206 for (p = body; *p; p++)
18207 if (*p == ' ' || *p == '(')
18208 break;
18209
18210 if (*p == ' ' || *p == '\0')
18211 {
18212 /* It's an object-like macro. */
18213 int name_len = p - body;
18214 char *name = copy_string (body, name_len);
18215 const char *replacement;
18216
18217 if (*p == ' ')
18218 replacement = body + name_len + 1;
18219 else
18220 {
4d3c2250 18221 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18222 replacement = body + name_len;
18223 }
6e70227d 18224
2e276125
JB
18225 macro_define_object (file, line, name, replacement);
18226
18227 xfree (name);
18228 }
18229 else if (*p == '(')
18230 {
18231 /* It's a function-like macro. */
18232 char *name = copy_string (body, p - body);
18233 int argc = 0;
18234 int argv_size = 1;
18235 char **argv = xmalloc (argv_size * sizeof (*argv));
18236
18237 p++;
18238
18239 p = consume_improper_spaces (p, body);
18240
18241 /* Parse the formal argument list. */
18242 while (*p && *p != ')')
18243 {
18244 /* Find the extent of the current argument name. */
18245 const char *arg_start = p;
18246
18247 while (*p && *p != ',' && *p != ')' && *p != ' ')
18248 p++;
18249
18250 if (! *p || p == arg_start)
4d3c2250 18251 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18252 else
18253 {
18254 /* Make sure argv has room for the new argument. */
18255 if (argc >= argv_size)
18256 {
18257 argv_size *= 2;
18258 argv = xrealloc (argv, argv_size * sizeof (*argv));
18259 }
18260
18261 argv[argc++] = copy_string (arg_start, p - arg_start);
18262 }
18263
18264 p = consume_improper_spaces (p, body);
18265
18266 /* Consume the comma, if present. */
18267 if (*p == ',')
18268 {
18269 p++;
18270
18271 p = consume_improper_spaces (p, body);
18272 }
18273 }
18274
18275 if (*p == ')')
18276 {
18277 p++;
18278
18279 if (*p == ' ')
18280 /* Perfectly formed definition, no complaints. */
18281 macro_define_function (file, line, name,
6e70227d 18282 argc, (const char **) argv,
2e276125
JB
18283 p + 1);
18284 else if (*p == '\0')
18285 {
18286 /* Complain, but do define it. */
4d3c2250 18287 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18288 macro_define_function (file, line, name,
6e70227d 18289 argc, (const char **) argv,
2e276125
JB
18290 p);
18291 }
18292 else
18293 /* Just complain. */
4d3c2250 18294 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18295 }
18296 else
18297 /* Just complain. */
4d3c2250 18298 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18299
18300 xfree (name);
18301 {
18302 int i;
18303
18304 for (i = 0; i < argc; i++)
18305 xfree (argv[i]);
18306 }
18307 xfree (argv);
18308 }
18309 else
4d3c2250 18310 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18311}
18312
cf2c3c16
TT
18313/* Skip some bytes from BYTES according to the form given in FORM.
18314 Returns the new pointer. */
2e276125 18315
cf2c3c16 18316static gdb_byte *
f664829e 18317skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18318 enum dwarf_form form,
18319 unsigned int offset_size,
18320 struct dwarf2_section_info *section)
2e276125 18321{
cf2c3c16 18322 unsigned int bytes_read;
2e276125 18323
cf2c3c16 18324 switch (form)
2e276125 18325 {
cf2c3c16
TT
18326 case DW_FORM_data1:
18327 case DW_FORM_flag:
18328 ++bytes;
18329 break;
18330
18331 case DW_FORM_data2:
18332 bytes += 2;
18333 break;
18334
18335 case DW_FORM_data4:
18336 bytes += 4;
18337 break;
18338
18339 case DW_FORM_data8:
18340 bytes += 8;
18341 break;
18342
18343 case DW_FORM_string:
18344 read_direct_string (abfd, bytes, &bytes_read);
18345 bytes += bytes_read;
18346 break;
18347
18348 case DW_FORM_sec_offset:
18349 case DW_FORM_strp:
36586728 18350 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18351 bytes += offset_size;
18352 break;
18353
18354 case DW_FORM_block:
18355 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18356 bytes += bytes_read;
18357 break;
18358
18359 case DW_FORM_block1:
18360 bytes += 1 + read_1_byte (abfd, bytes);
18361 break;
18362 case DW_FORM_block2:
18363 bytes += 2 + read_2_bytes (abfd, bytes);
18364 break;
18365 case DW_FORM_block4:
18366 bytes += 4 + read_4_bytes (abfd, bytes);
18367 break;
18368
18369 case DW_FORM_sdata:
18370 case DW_FORM_udata:
3019eac3
DE
18371 case DW_FORM_GNU_addr_index:
18372 case DW_FORM_GNU_str_index:
f664829e
DE
18373 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18374 if (bytes == NULL)
18375 {
18376 dwarf2_section_buffer_overflow_complaint (section);
18377 return NULL;
18378 }
cf2c3c16
TT
18379 break;
18380
18381 default:
18382 {
18383 complain:
18384 complaint (&symfile_complaints,
18385 _("invalid form 0x%x in `%s'"),
18386 form,
18387 section->asection->name);
18388 return NULL;
18389 }
2e276125
JB
18390 }
18391
cf2c3c16
TT
18392 return bytes;
18393}
757a13d0 18394
cf2c3c16
TT
18395/* A helper for dwarf_decode_macros that handles skipping an unknown
18396 opcode. Returns an updated pointer to the macro data buffer; or,
18397 on error, issues a complaint and returns NULL. */
757a13d0 18398
cf2c3c16
TT
18399static gdb_byte *
18400skip_unknown_opcode (unsigned int opcode,
18401 gdb_byte **opcode_definitions,
f664829e 18402 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18403 bfd *abfd,
18404 unsigned int offset_size,
18405 struct dwarf2_section_info *section)
18406{
18407 unsigned int bytes_read, i;
18408 unsigned long arg;
18409 gdb_byte *defn;
2e276125 18410
cf2c3c16 18411 if (opcode_definitions[opcode] == NULL)
2e276125 18412 {
cf2c3c16
TT
18413 complaint (&symfile_complaints,
18414 _("unrecognized DW_MACFINO opcode 0x%x"),
18415 opcode);
18416 return NULL;
18417 }
2e276125 18418
cf2c3c16
TT
18419 defn = opcode_definitions[opcode];
18420 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18421 defn += bytes_read;
2e276125 18422
cf2c3c16
TT
18423 for (i = 0; i < arg; ++i)
18424 {
f664829e
DE
18425 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18426 section);
cf2c3c16
TT
18427 if (mac_ptr == NULL)
18428 {
18429 /* skip_form_bytes already issued the complaint. */
18430 return NULL;
18431 }
18432 }
757a13d0 18433
cf2c3c16
TT
18434 return mac_ptr;
18435}
757a13d0 18436
cf2c3c16
TT
18437/* A helper function which parses the header of a macro section.
18438 If the macro section is the extended (for now called "GNU") type,
18439 then this updates *OFFSET_SIZE. Returns a pointer to just after
18440 the header, or issues a complaint and returns NULL on error. */
757a13d0 18441
cf2c3c16
TT
18442static gdb_byte *
18443dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18444 bfd *abfd,
18445 gdb_byte *mac_ptr,
18446 unsigned int *offset_size,
18447 int section_is_gnu)
18448{
18449 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18450
cf2c3c16
TT
18451 if (section_is_gnu)
18452 {
18453 unsigned int version, flags;
757a13d0 18454
cf2c3c16
TT
18455 version = read_2_bytes (abfd, mac_ptr);
18456 if (version != 4)
18457 {
18458 complaint (&symfile_complaints,
18459 _("unrecognized version `%d' in .debug_macro section"),
18460 version);
18461 return NULL;
18462 }
18463 mac_ptr += 2;
757a13d0 18464
cf2c3c16
TT
18465 flags = read_1_byte (abfd, mac_ptr);
18466 ++mac_ptr;
18467 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18468
cf2c3c16
TT
18469 if ((flags & 2) != 0)
18470 /* We don't need the line table offset. */
18471 mac_ptr += *offset_size;
757a13d0 18472
cf2c3c16
TT
18473 /* Vendor opcode descriptions. */
18474 if ((flags & 4) != 0)
18475 {
18476 unsigned int i, count;
757a13d0 18477
cf2c3c16
TT
18478 count = read_1_byte (abfd, mac_ptr);
18479 ++mac_ptr;
18480 for (i = 0; i < count; ++i)
18481 {
18482 unsigned int opcode, bytes_read;
18483 unsigned long arg;
18484
18485 opcode = read_1_byte (abfd, mac_ptr);
18486 ++mac_ptr;
18487 opcode_definitions[opcode] = mac_ptr;
18488 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18489 mac_ptr += bytes_read;
18490 mac_ptr += arg;
18491 }
757a13d0 18492 }
cf2c3c16 18493 }
757a13d0 18494
cf2c3c16
TT
18495 return mac_ptr;
18496}
757a13d0 18497
cf2c3c16 18498/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18499 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18500
18501static void
18502dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18503 struct macro_source_file *current_file,
15d034d0 18504 struct line_header *lh, const char *comp_dir,
cf2c3c16 18505 struct dwarf2_section_info *section,
36586728 18506 int section_is_gnu, int section_is_dwz,
cf2c3c16 18507 unsigned int offset_size,
8fc3fc34
TT
18508 struct objfile *objfile,
18509 htab_t include_hash)
cf2c3c16
TT
18510{
18511 enum dwarf_macro_record_type macinfo_type;
18512 int at_commandline;
18513 gdb_byte *opcode_definitions[256];
757a13d0 18514
cf2c3c16
TT
18515 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18516 &offset_size, section_is_gnu);
18517 if (mac_ptr == NULL)
18518 {
18519 /* We already issued a complaint. */
18520 return;
18521 }
757a13d0
JK
18522
18523 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18524 GDB is still reading the definitions from command line. First
18525 DW_MACINFO_start_file will need to be ignored as it was already executed
18526 to create CURRENT_FILE for the main source holding also the command line
18527 definitions. On first met DW_MACINFO_start_file this flag is reset to
18528 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18529
18530 at_commandline = 1;
18531
18532 do
18533 {
18534 /* Do we at least have room for a macinfo type byte? */
18535 if (mac_ptr >= mac_end)
18536 {
f664829e 18537 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18538 break;
18539 }
18540
18541 macinfo_type = read_1_byte (abfd, mac_ptr);
18542 mac_ptr++;
18543
cf2c3c16
TT
18544 /* Note that we rely on the fact that the corresponding GNU and
18545 DWARF constants are the same. */
757a13d0
JK
18546 switch (macinfo_type)
18547 {
18548 /* A zero macinfo type indicates the end of the macro
18549 information. */
18550 case 0:
18551 break;
2e276125 18552
cf2c3c16
TT
18553 case DW_MACRO_GNU_define:
18554 case DW_MACRO_GNU_undef:
18555 case DW_MACRO_GNU_define_indirect:
18556 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18557 case DW_MACRO_GNU_define_indirect_alt:
18558 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18559 {
891d2f0b 18560 unsigned int bytes_read;
2e276125
JB
18561 int line;
18562 char *body;
cf2c3c16 18563 int is_define;
2e276125 18564
cf2c3c16
TT
18565 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18566 mac_ptr += bytes_read;
18567
18568 if (macinfo_type == DW_MACRO_GNU_define
18569 || macinfo_type == DW_MACRO_GNU_undef)
18570 {
18571 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18572 mac_ptr += bytes_read;
18573 }
18574 else
18575 {
18576 LONGEST str_offset;
18577
18578 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18579 mac_ptr += offset_size;
2e276125 18580
36586728 18581 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18582 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18583 || section_is_dwz)
36586728
TT
18584 {
18585 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18586
18587 body = read_indirect_string_from_dwz (dwz, str_offset);
18588 }
18589 else
18590 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18591 }
18592
18593 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18594 || macinfo_type == DW_MACRO_GNU_define_indirect
18595 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18596 if (! current_file)
757a13d0
JK
18597 {
18598 /* DWARF violation as no main source is present. */
18599 complaint (&symfile_complaints,
18600 _("debug info with no main source gives macro %s "
18601 "on line %d: %s"),
cf2c3c16
TT
18602 is_define ? _("definition") : _("undefinition"),
18603 line, body);
757a13d0
JK
18604 break;
18605 }
3e43a32a
MS
18606 if ((line == 0 && !at_commandline)
18607 || (line != 0 && at_commandline))
4d3c2250 18608 complaint (&symfile_complaints,
757a13d0
JK
18609 _("debug info gives %s macro %s with %s line %d: %s"),
18610 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18611 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18612 line == 0 ? _("zero") : _("non-zero"), line, body);
18613
cf2c3c16 18614 if (is_define)
757a13d0 18615 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18616 else
18617 {
18618 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18619 || macinfo_type == DW_MACRO_GNU_undef_indirect
18620 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18621 macro_undef (current_file, line, body);
18622 }
2e276125
JB
18623 }
18624 break;
18625
cf2c3c16 18626 case DW_MACRO_GNU_start_file:
2e276125 18627 {
891d2f0b 18628 unsigned int bytes_read;
2e276125
JB
18629 int line, file;
18630
18631 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18632 mac_ptr += bytes_read;
18633 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18634 mac_ptr += bytes_read;
18635
3e43a32a
MS
18636 if ((line == 0 && !at_commandline)
18637 || (line != 0 && at_commandline))
757a13d0
JK
18638 complaint (&symfile_complaints,
18639 _("debug info gives source %d included "
18640 "from %s at %s line %d"),
18641 file, at_commandline ? _("command-line") : _("file"),
18642 line == 0 ? _("zero") : _("non-zero"), line);
18643
18644 if (at_commandline)
18645 {
cf2c3c16
TT
18646 /* This DW_MACRO_GNU_start_file was executed in the
18647 pass one. */
757a13d0
JK
18648 at_commandline = 0;
18649 }
18650 else
18651 current_file = macro_start_file (file, line,
18652 current_file, comp_dir,
cf2c3c16 18653 lh, objfile);
2e276125
JB
18654 }
18655 break;
18656
cf2c3c16 18657 case DW_MACRO_GNU_end_file:
2e276125 18658 if (! current_file)
4d3c2250 18659 complaint (&symfile_complaints,
3e43a32a
MS
18660 _("macro debug info has an unmatched "
18661 "`close_file' directive"));
2e276125
JB
18662 else
18663 {
18664 current_file = current_file->included_by;
18665 if (! current_file)
18666 {
cf2c3c16 18667 enum dwarf_macro_record_type next_type;
2e276125
JB
18668
18669 /* GCC circa March 2002 doesn't produce the zero
18670 type byte marking the end of the compilation
18671 unit. Complain if it's not there, but exit no
18672 matter what. */
18673
18674 /* Do we at least have room for a macinfo type byte? */
18675 if (mac_ptr >= mac_end)
18676 {
f664829e 18677 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18678 return;
18679 }
18680
18681 /* We don't increment mac_ptr here, so this is just
18682 a look-ahead. */
18683 next_type = read_1_byte (abfd, mac_ptr);
18684 if (next_type != 0)
4d3c2250 18685 complaint (&symfile_complaints,
3e43a32a
MS
18686 _("no terminating 0-type entry for "
18687 "macros in `.debug_macinfo' section"));
2e276125
JB
18688
18689 return;
18690 }
18691 }
18692 break;
18693
cf2c3c16 18694 case DW_MACRO_GNU_transparent_include:
36586728 18695 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18696 {
18697 LONGEST offset;
8fc3fc34 18698 void **slot;
a036ba48
TT
18699 bfd *include_bfd = abfd;
18700 struct dwarf2_section_info *include_section = section;
18701 struct dwarf2_section_info alt_section;
18702 gdb_byte *include_mac_end = mac_end;
18703 int is_dwz = section_is_dwz;
18704 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18705
18706 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18707 mac_ptr += offset_size;
18708
a036ba48
TT
18709 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18710 {
18711 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18712
18713 dwarf2_read_section (dwarf2_per_objfile->objfile,
18714 &dwz->macro);
18715
18716 include_bfd = dwz->macro.asection->owner;
18717 include_section = &dwz->macro;
18718 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18719 is_dwz = 1;
18720 }
18721
18722 new_mac_ptr = include_section->buffer + offset;
18723 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18724
8fc3fc34
TT
18725 if (*slot != NULL)
18726 {
18727 /* This has actually happened; see
18728 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18729 complaint (&symfile_complaints,
18730 _("recursive DW_MACRO_GNU_transparent_include in "
18731 ".debug_macro section"));
18732 }
18733 else
18734 {
a036ba48 18735 *slot = new_mac_ptr;
36586728 18736
a036ba48 18737 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18738 include_mac_end, current_file,
8fc3fc34 18739 lh, comp_dir,
36586728 18740 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18741 offset_size, objfile, include_hash);
18742
a036ba48 18743 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18744 }
cf2c3c16
TT
18745 }
18746 break;
18747
2e276125 18748 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18749 if (!section_is_gnu)
18750 {
18751 unsigned int bytes_read;
18752 int constant;
2e276125 18753
cf2c3c16
TT
18754 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18755 mac_ptr += bytes_read;
18756 read_direct_string (abfd, mac_ptr, &bytes_read);
18757 mac_ptr += bytes_read;
2e276125 18758
cf2c3c16
TT
18759 /* We don't recognize any vendor extensions. */
18760 break;
18761 }
18762 /* FALLTHROUGH */
18763
18764 default:
18765 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18766 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18767 section);
18768 if (mac_ptr == NULL)
18769 return;
18770 break;
2e276125 18771 }
757a13d0 18772 } while (macinfo_type != 0);
2e276125 18773}
8e19ed76 18774
cf2c3c16 18775static void
09262596 18776dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18777 const char *comp_dir, int section_is_gnu)
cf2c3c16 18778{
bb5ed363 18779 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18780 struct line_header *lh = cu->line_header;
18781 bfd *abfd;
cf2c3c16
TT
18782 gdb_byte *mac_ptr, *mac_end;
18783 struct macro_source_file *current_file = 0;
18784 enum dwarf_macro_record_type macinfo_type;
18785 unsigned int offset_size = cu->header.offset_size;
18786 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18787 struct cleanup *cleanup;
18788 htab_t include_hash;
18789 void **slot;
09262596
DE
18790 struct dwarf2_section_info *section;
18791 const char *section_name;
18792
18793 if (cu->dwo_unit != NULL)
18794 {
18795 if (section_is_gnu)
18796 {
18797 section = &cu->dwo_unit->dwo_file->sections.macro;
18798 section_name = ".debug_macro.dwo";
18799 }
18800 else
18801 {
18802 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18803 section_name = ".debug_macinfo.dwo";
18804 }
18805 }
18806 else
18807 {
18808 if (section_is_gnu)
18809 {
18810 section = &dwarf2_per_objfile->macro;
18811 section_name = ".debug_macro";
18812 }
18813 else
18814 {
18815 section = &dwarf2_per_objfile->macinfo;
18816 section_name = ".debug_macinfo";
18817 }
18818 }
cf2c3c16 18819
bb5ed363 18820 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18821 if (section->buffer == NULL)
18822 {
fceca515 18823 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18824 return;
18825 }
09262596 18826 abfd = section->asection->owner;
cf2c3c16
TT
18827
18828 /* First pass: Find the name of the base filename.
18829 This filename is needed in order to process all macros whose definition
18830 (or undefinition) comes from the command line. These macros are defined
18831 before the first DW_MACINFO_start_file entry, and yet still need to be
18832 associated to the base file.
18833
18834 To determine the base file name, we scan the macro definitions until we
18835 reach the first DW_MACINFO_start_file entry. We then initialize
18836 CURRENT_FILE accordingly so that any macro definition found before the
18837 first DW_MACINFO_start_file can still be associated to the base file. */
18838
18839 mac_ptr = section->buffer + offset;
18840 mac_end = section->buffer + section->size;
18841
18842 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18843 &offset_size, section_is_gnu);
18844 if (mac_ptr == NULL)
18845 {
18846 /* We already issued a complaint. */
18847 return;
18848 }
18849
18850 do
18851 {
18852 /* Do we at least have room for a macinfo type byte? */
18853 if (mac_ptr >= mac_end)
18854 {
18855 /* Complaint is printed during the second pass as GDB will probably
18856 stop the first pass earlier upon finding
18857 DW_MACINFO_start_file. */
18858 break;
18859 }
18860
18861 macinfo_type = read_1_byte (abfd, mac_ptr);
18862 mac_ptr++;
18863
18864 /* Note that we rely on the fact that the corresponding GNU and
18865 DWARF constants are the same. */
18866 switch (macinfo_type)
18867 {
18868 /* A zero macinfo type indicates the end of the macro
18869 information. */
18870 case 0:
18871 break;
18872
18873 case DW_MACRO_GNU_define:
18874 case DW_MACRO_GNU_undef:
18875 /* Only skip the data by MAC_PTR. */
18876 {
18877 unsigned int bytes_read;
18878
18879 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18880 mac_ptr += bytes_read;
18881 read_direct_string (abfd, mac_ptr, &bytes_read);
18882 mac_ptr += bytes_read;
18883 }
18884 break;
18885
18886 case DW_MACRO_GNU_start_file:
18887 {
18888 unsigned int bytes_read;
18889 int line, file;
18890
18891 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18892 mac_ptr += bytes_read;
18893 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18894 mac_ptr += bytes_read;
18895
18896 current_file = macro_start_file (file, line, current_file,
bb5ed363 18897 comp_dir, lh, objfile);
cf2c3c16
TT
18898 }
18899 break;
18900
18901 case DW_MACRO_GNU_end_file:
18902 /* No data to skip by MAC_PTR. */
18903 break;
18904
18905 case DW_MACRO_GNU_define_indirect:
18906 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18907 case DW_MACRO_GNU_define_indirect_alt:
18908 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18909 {
18910 unsigned int bytes_read;
18911
18912 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18913 mac_ptr += bytes_read;
18914 mac_ptr += offset_size;
18915 }
18916 break;
18917
18918 case DW_MACRO_GNU_transparent_include:
f7a35f02 18919 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18920 /* Note that, according to the spec, a transparent include
18921 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18922 skip this opcode. */
18923 mac_ptr += offset_size;
18924 break;
18925
18926 case DW_MACINFO_vendor_ext:
18927 /* Only skip the data by MAC_PTR. */
18928 if (!section_is_gnu)
18929 {
18930 unsigned int bytes_read;
18931
18932 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18933 mac_ptr += bytes_read;
18934 read_direct_string (abfd, mac_ptr, &bytes_read);
18935 mac_ptr += bytes_read;
18936 }
18937 /* FALLTHROUGH */
18938
18939 default:
18940 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18941 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18942 section);
18943 if (mac_ptr == NULL)
18944 return;
18945 break;
18946 }
18947 } while (macinfo_type != 0 && current_file == NULL);
18948
18949 /* Second pass: Process all entries.
18950
18951 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18952 command-line macro definitions/undefinitions. This flag is unset when we
18953 reach the first DW_MACINFO_start_file entry. */
18954
8fc3fc34
TT
18955 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18956 NULL, xcalloc, xfree);
18957 cleanup = make_cleanup_htab_delete (include_hash);
18958 mac_ptr = section->buffer + offset;
18959 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18960 *slot = mac_ptr;
18961 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18962 current_file, lh, comp_dir, section,
18963 section_is_gnu, 0,
8fc3fc34
TT
18964 offset_size, objfile, include_hash);
18965 do_cleanups (cleanup);
cf2c3c16
TT
18966}
18967
8e19ed76 18968/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18969 if so return true else false. */
380bca97 18970
8e19ed76
PS
18971static int
18972attr_form_is_block (struct attribute *attr)
18973{
18974 return (attr == NULL ? 0 :
18975 attr->form == DW_FORM_block1
18976 || attr->form == DW_FORM_block2
18977 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18978 || attr->form == DW_FORM_block
18979 || attr->form == DW_FORM_exprloc);
8e19ed76 18980}
4c2df51b 18981
c6a0999f
JB
18982/* Return non-zero if ATTR's value is a section offset --- classes
18983 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18984 You may use DW_UNSND (attr) to retrieve such offsets.
18985
18986 Section 7.5.4, "Attribute Encodings", explains that no attribute
18987 may have a value that belongs to more than one of these classes; it
18988 would be ambiguous if we did, because we use the same forms for all
18989 of them. */
380bca97 18990
3690dd37
JB
18991static int
18992attr_form_is_section_offset (struct attribute *attr)
18993{
18994 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18995 || attr->form == DW_FORM_data8
18996 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18997}
18998
3690dd37
JB
18999/* Return non-zero if ATTR's value falls in the 'constant' class, or
19000 zero otherwise. When this function returns true, you can apply
19001 dwarf2_get_attr_constant_value to it.
19002
19003 However, note that for some attributes you must check
19004 attr_form_is_section_offset before using this test. DW_FORM_data4
19005 and DW_FORM_data8 are members of both the constant class, and of
19006 the classes that contain offsets into other debug sections
19007 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19008 that, if an attribute's can be either a constant or one of the
19009 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19010 taken as section offsets, not constants. */
380bca97 19011
3690dd37
JB
19012static int
19013attr_form_is_constant (struct attribute *attr)
19014{
19015 switch (attr->form)
19016 {
19017 case DW_FORM_sdata:
19018 case DW_FORM_udata:
19019 case DW_FORM_data1:
19020 case DW_FORM_data2:
19021 case DW_FORM_data4:
19022 case DW_FORM_data8:
19023 return 1;
19024 default:
19025 return 0;
19026 }
19027}
19028
3019eac3
DE
19029/* Return the .debug_loc section to use for CU.
19030 For DWO files use .debug_loc.dwo. */
19031
19032static struct dwarf2_section_info *
19033cu_debug_loc_section (struct dwarf2_cu *cu)
19034{
19035 if (cu->dwo_unit)
19036 return &cu->dwo_unit->dwo_file->sections.loc;
19037 return &dwarf2_per_objfile->loc;
19038}
19039
8cf6f0b1
TT
19040/* A helper function that fills in a dwarf2_loclist_baton. */
19041
19042static void
19043fill_in_loclist_baton (struct dwarf2_cu *cu,
19044 struct dwarf2_loclist_baton *baton,
19045 struct attribute *attr)
19046{
3019eac3
DE
19047 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19048
19049 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19050
19051 baton->per_cu = cu->per_cu;
19052 gdb_assert (baton->per_cu);
19053 /* We don't know how long the location list is, but make sure we
19054 don't run off the edge of the section. */
3019eac3
DE
19055 baton->size = section->size - DW_UNSND (attr);
19056 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19057 baton->base_address = cu->base_address;
f664829e 19058 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19059}
19060
4c2df51b
DJ
19061static void
19062dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 19063 struct dwarf2_cu *cu)
4c2df51b 19064{
bb5ed363 19065 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19066 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19067
3690dd37 19068 if (attr_form_is_section_offset (attr)
3019eac3 19069 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19070 the section. If so, fall through to the complaint in the
19071 other branch. */
3019eac3 19072 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19073 {
0d53c4c4 19074 struct dwarf2_loclist_baton *baton;
4c2df51b 19075
bb5ed363 19076 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19077 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19078
8cf6f0b1 19079 fill_in_loclist_baton (cu, baton, attr);
be391dca 19080
d00adf39 19081 if (cu->base_known == 0)
0d53c4c4 19082 complaint (&symfile_complaints,
3e43a32a
MS
19083 _("Location list used without "
19084 "specifying the CU base address."));
4c2df51b 19085
768a979c 19086 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
19087 SYMBOL_LOCATION_BATON (sym) = baton;
19088 }
19089 else
19090 {
19091 struct dwarf2_locexpr_baton *baton;
19092
bb5ed363 19093 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19094 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19095 baton->per_cu = cu->per_cu;
19096 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19097
19098 if (attr_form_is_block (attr))
19099 {
19100 /* Note that we're just copying the block's data pointer
19101 here, not the actual data. We're still pointing into the
6502dd73
DJ
19102 info_buffer for SYM's objfile; right now we never release
19103 that buffer, but when we do clean up properly this may
19104 need to change. */
0d53c4c4
DJ
19105 baton->size = DW_BLOCK (attr)->size;
19106 baton->data = DW_BLOCK (attr)->data;
19107 }
19108 else
19109 {
19110 dwarf2_invalid_attrib_class_complaint ("location description",
19111 SYMBOL_NATURAL_NAME (sym));
19112 baton->size = 0;
0d53c4c4 19113 }
6e70227d 19114
768a979c 19115 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
19116 SYMBOL_LOCATION_BATON (sym) = baton;
19117 }
4c2df51b 19118}
6502dd73 19119
9aa1f1e3
TT
19120/* Return the OBJFILE associated with the compilation unit CU. If CU
19121 came from a separate debuginfo file, then the master objfile is
19122 returned. */
ae0d2f24
UW
19123
19124struct objfile *
19125dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19126{
9291a0cd 19127 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19128
19129 /* Return the master objfile, so that we can report and look up the
19130 correct file containing this variable. */
19131 if (objfile->separate_debug_objfile_backlink)
19132 objfile = objfile->separate_debug_objfile_backlink;
19133
19134 return objfile;
19135}
19136
96408a79
SA
19137/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19138 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19139 CU_HEADERP first. */
19140
19141static const struct comp_unit_head *
19142per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19143 struct dwarf2_per_cu_data *per_cu)
19144{
96408a79
SA
19145 gdb_byte *info_ptr;
19146
19147 if (per_cu->cu)
19148 return &per_cu->cu->header;
19149
0bc3a05c 19150 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19151
19152 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19153 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19154
19155 return cu_headerp;
19156}
19157
ae0d2f24
UW
19158/* Return the address size given in the compilation unit header for CU. */
19159
98714339 19160int
ae0d2f24
UW
19161dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19162{
96408a79
SA
19163 struct comp_unit_head cu_header_local;
19164 const struct comp_unit_head *cu_headerp;
c471e790 19165
96408a79
SA
19166 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19167
19168 return cu_headerp->addr_size;
ae0d2f24
UW
19169}
19170
9eae7c52
TT
19171/* Return the offset size given in the compilation unit header for CU. */
19172
19173int
19174dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19175{
96408a79
SA
19176 struct comp_unit_head cu_header_local;
19177 const struct comp_unit_head *cu_headerp;
9c6c53f7 19178
96408a79
SA
19179 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19180
19181 return cu_headerp->offset_size;
19182}
19183
19184/* See its dwarf2loc.h declaration. */
19185
19186int
19187dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19188{
19189 struct comp_unit_head cu_header_local;
19190 const struct comp_unit_head *cu_headerp;
19191
19192 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19193
19194 if (cu_headerp->version == 2)
19195 return cu_headerp->addr_size;
19196 else
19197 return cu_headerp->offset_size;
181cebd4
JK
19198}
19199
9aa1f1e3
TT
19200/* Return the text offset of the CU. The returned offset comes from
19201 this CU's objfile. If this objfile came from a separate debuginfo
19202 file, then the offset may be different from the corresponding
19203 offset in the parent objfile. */
19204
19205CORE_ADDR
19206dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19207{
bb3fa9d0 19208 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19209
19210 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19211}
19212
348e048f
DE
19213/* Locate the .debug_info compilation unit from CU's objfile which contains
19214 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19215
19216static struct dwarf2_per_cu_data *
b64f50a1 19217dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19218 unsigned int offset_in_dwz,
ae038cb0
DJ
19219 struct objfile *objfile)
19220{
19221 struct dwarf2_per_cu_data *this_cu;
19222 int low, high;
36586728 19223 const sect_offset *cu_off;
ae038cb0 19224
ae038cb0
DJ
19225 low = 0;
19226 high = dwarf2_per_objfile->n_comp_units - 1;
19227 while (high > low)
19228 {
36586728 19229 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19230 int mid = low + (high - low) / 2;
9a619af0 19231
36586728
TT
19232 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19233 cu_off = &mid_cu->offset;
19234 if (mid_cu->is_dwz > offset_in_dwz
19235 || (mid_cu->is_dwz == offset_in_dwz
19236 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19237 high = mid;
19238 else
19239 low = mid + 1;
19240 }
19241 gdb_assert (low == high);
36586728
TT
19242 this_cu = dwarf2_per_objfile->all_comp_units[low];
19243 cu_off = &this_cu->offset;
19244 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19245 {
36586728 19246 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19247 error (_("Dwarf Error: could not find partial DIE containing "
19248 "offset 0x%lx [in module %s]"),
b64f50a1 19249 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19250
b64f50a1
JK
19251 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19252 <= offset.sect_off);
ae038cb0
DJ
19253 return dwarf2_per_objfile->all_comp_units[low-1];
19254 }
19255 else
19256 {
19257 this_cu = dwarf2_per_objfile->all_comp_units[low];
19258 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19259 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19260 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19261 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19262 return this_cu;
19263 }
19264}
19265
23745b47 19266/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19267
9816fde3 19268static void
23745b47 19269init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19270{
9816fde3 19271 memset (cu, 0, sizeof (*cu));
23745b47
DE
19272 per_cu->cu = cu;
19273 cu->per_cu = per_cu;
19274 cu->objfile = per_cu->objfile;
93311388 19275 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19276}
19277
19278/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19279
19280static void
95554aad
TT
19281prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19282 enum language pretend_language)
9816fde3
JK
19283{
19284 struct attribute *attr;
19285
19286 /* Set the language we're debugging. */
19287 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19288 if (attr)
19289 set_cu_language (DW_UNSND (attr), cu);
19290 else
9cded63f 19291 {
95554aad 19292 cu->language = pretend_language;
9cded63f
TT
19293 cu->language_defn = language_def (cu->language);
19294 }
dee91e82
DE
19295
19296 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19297 if (attr)
19298 cu->producer = DW_STRING (attr);
93311388
DE
19299}
19300
ae038cb0
DJ
19301/* Release one cached compilation unit, CU. We unlink it from the tree
19302 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19303 the caller is responsible for that.
19304 NOTE: DATA is a void * because this function is also used as a
19305 cleanup routine. */
ae038cb0
DJ
19306
19307static void
68dc6402 19308free_heap_comp_unit (void *data)
ae038cb0
DJ
19309{
19310 struct dwarf2_cu *cu = data;
19311
23745b47
DE
19312 gdb_assert (cu->per_cu != NULL);
19313 cu->per_cu->cu = NULL;
ae038cb0
DJ
19314 cu->per_cu = NULL;
19315
19316 obstack_free (&cu->comp_unit_obstack, NULL);
19317
19318 xfree (cu);
19319}
19320
72bf9492 19321/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19322 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19323 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19324
19325static void
19326free_stack_comp_unit (void *data)
19327{
19328 struct dwarf2_cu *cu = data;
19329
23745b47
DE
19330 gdb_assert (cu->per_cu != NULL);
19331 cu->per_cu->cu = NULL;
19332 cu->per_cu = NULL;
19333
72bf9492
DJ
19334 obstack_free (&cu->comp_unit_obstack, NULL);
19335 cu->partial_dies = NULL;
ae038cb0
DJ
19336}
19337
19338/* Free all cached compilation units. */
19339
19340static void
19341free_cached_comp_units (void *data)
19342{
19343 struct dwarf2_per_cu_data *per_cu, **last_chain;
19344
19345 per_cu = dwarf2_per_objfile->read_in_chain;
19346 last_chain = &dwarf2_per_objfile->read_in_chain;
19347 while (per_cu != NULL)
19348 {
19349 struct dwarf2_per_cu_data *next_cu;
19350
19351 next_cu = per_cu->cu->read_in_chain;
19352
68dc6402 19353 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19354 *last_chain = next_cu;
19355
19356 per_cu = next_cu;
19357 }
19358}
19359
19360/* Increase the age counter on each cached compilation unit, and free
19361 any that are too old. */
19362
19363static void
19364age_cached_comp_units (void)
19365{
19366 struct dwarf2_per_cu_data *per_cu, **last_chain;
19367
19368 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19369 per_cu = dwarf2_per_objfile->read_in_chain;
19370 while (per_cu != NULL)
19371 {
19372 per_cu->cu->last_used ++;
19373 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19374 dwarf2_mark (per_cu->cu);
19375 per_cu = per_cu->cu->read_in_chain;
19376 }
19377
19378 per_cu = dwarf2_per_objfile->read_in_chain;
19379 last_chain = &dwarf2_per_objfile->read_in_chain;
19380 while (per_cu != NULL)
19381 {
19382 struct dwarf2_per_cu_data *next_cu;
19383
19384 next_cu = per_cu->cu->read_in_chain;
19385
19386 if (!per_cu->cu->mark)
19387 {
68dc6402 19388 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19389 *last_chain = next_cu;
19390 }
19391 else
19392 last_chain = &per_cu->cu->read_in_chain;
19393
19394 per_cu = next_cu;
19395 }
19396}
19397
19398/* Remove a single compilation unit from the cache. */
19399
19400static void
dee91e82 19401free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19402{
19403 struct dwarf2_per_cu_data *per_cu, **last_chain;
19404
19405 per_cu = dwarf2_per_objfile->read_in_chain;
19406 last_chain = &dwarf2_per_objfile->read_in_chain;
19407 while (per_cu != NULL)
19408 {
19409 struct dwarf2_per_cu_data *next_cu;
19410
19411 next_cu = per_cu->cu->read_in_chain;
19412
dee91e82 19413 if (per_cu == target_per_cu)
ae038cb0 19414 {
68dc6402 19415 free_heap_comp_unit (per_cu->cu);
dee91e82 19416 per_cu->cu = NULL;
ae038cb0
DJ
19417 *last_chain = next_cu;
19418 break;
19419 }
19420 else
19421 last_chain = &per_cu->cu->read_in_chain;
19422
19423 per_cu = next_cu;
19424 }
19425}
19426
fe3e1990
DJ
19427/* Release all extra memory associated with OBJFILE. */
19428
19429void
19430dwarf2_free_objfile (struct objfile *objfile)
19431{
19432 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19433
19434 if (dwarf2_per_objfile == NULL)
19435 return;
19436
19437 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19438 free_cached_comp_units (NULL);
19439
7b9f3c50
DE
19440 if (dwarf2_per_objfile->quick_file_names_table)
19441 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19442
fe3e1990
DJ
19443 /* Everything else should be on the objfile obstack. */
19444}
19445
dee91e82
DE
19446/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19447 We store these in a hash table separate from the DIEs, and preserve them
19448 when the DIEs are flushed out of cache.
19449
19450 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19451 uniquely identify the type. A file may have multiple .debug_types sections,
19452 or the type may come from a DWO file. We have to use something in
19453 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19454 routine, get_die_type_at_offset, from outside this file, and thus won't
19455 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19456 of the objfile. */
1c379e20 19457
dee91e82 19458struct dwarf2_per_cu_offset_and_type
1c379e20 19459{
dee91e82 19460 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19461 sect_offset offset;
1c379e20
DJ
19462 struct type *type;
19463};
19464
dee91e82 19465/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19466
19467static hashval_t
dee91e82 19468per_cu_offset_and_type_hash (const void *item)
1c379e20 19469{
dee91e82 19470 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19471
dee91e82 19472 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19473}
19474
dee91e82 19475/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19476
19477static int
dee91e82 19478per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19479{
dee91e82
DE
19480 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19481 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19482
dee91e82
DE
19483 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19484 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19485}
19486
19487/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19488 table if necessary. For convenience, return TYPE.
19489
19490 The DIEs reading must have careful ordering to:
19491 * Not cause infite loops trying to read in DIEs as a prerequisite for
19492 reading current DIE.
19493 * Not trying to dereference contents of still incompletely read in types
19494 while reading in other DIEs.
19495 * Enable referencing still incompletely read in types just by a pointer to
19496 the type without accessing its fields.
19497
19498 Therefore caller should follow these rules:
19499 * Try to fetch any prerequisite types we may need to build this DIE type
19500 before building the type and calling set_die_type.
e71ec853 19501 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19502 possible before fetching more types to complete the current type.
19503 * Make the type as complete as possible before fetching more types. */
1c379e20 19504
f792889a 19505static struct type *
1c379e20
DJ
19506set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19507{
dee91e82 19508 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19509 struct objfile *objfile = cu->objfile;
1c379e20 19510
b4ba55a1
JB
19511 /* For Ada types, make sure that the gnat-specific data is always
19512 initialized (if not already set). There are a few types where
19513 we should not be doing so, because the type-specific area is
19514 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19515 where the type-specific area is used to store the floatformat).
19516 But this is not a problem, because the gnat-specific information
19517 is actually not needed for these types. */
19518 if (need_gnat_info (cu)
19519 && TYPE_CODE (type) != TYPE_CODE_FUNC
19520 && TYPE_CODE (type) != TYPE_CODE_FLT
19521 && !HAVE_GNAT_AUX_INFO (type))
19522 INIT_GNAT_SPECIFIC (type);
19523
dee91e82 19524 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19525 {
dee91e82
DE
19526 dwarf2_per_objfile->die_type_hash =
19527 htab_create_alloc_ex (127,
19528 per_cu_offset_and_type_hash,
19529 per_cu_offset_and_type_eq,
19530 NULL,
19531 &objfile->objfile_obstack,
19532 hashtab_obstack_allocate,
19533 dummy_obstack_deallocate);
f792889a 19534 }
1c379e20 19535
dee91e82 19536 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19537 ofs.offset = die->offset;
19538 ofs.type = type;
dee91e82
DE
19539 slot = (struct dwarf2_per_cu_offset_and_type **)
19540 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19541 if (*slot)
19542 complaint (&symfile_complaints,
19543 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19544 die->offset.sect_off);
673bfd45 19545 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19546 **slot = ofs;
f792889a 19547 return type;
1c379e20
DJ
19548}
19549
380bca97 19550/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19551 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19552
19553static struct type *
b64f50a1 19554get_die_type_at_offset (sect_offset offset,
673bfd45 19555 struct dwarf2_per_cu_data *per_cu)
1c379e20 19556{
dee91e82 19557 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19558
dee91e82 19559 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19560 return NULL;
1c379e20 19561
dee91e82 19562 ofs.per_cu = per_cu;
673bfd45 19563 ofs.offset = offset;
dee91e82 19564 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19565 if (slot)
19566 return slot->type;
19567 else
19568 return NULL;
19569}
19570
673bfd45
DE
19571/* Look up the type for DIE in the appropriate type_hash table,
19572 or return NULL if DIE does not have a saved type. */
19573
19574static struct type *
19575get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19576{
19577 return get_die_type_at_offset (die->offset, cu->per_cu);
19578}
19579
10b3939b
DJ
19580/* Add a dependence relationship from CU to REF_PER_CU. */
19581
19582static void
19583dwarf2_add_dependence (struct dwarf2_cu *cu,
19584 struct dwarf2_per_cu_data *ref_per_cu)
19585{
19586 void **slot;
19587
19588 if (cu->dependencies == NULL)
19589 cu->dependencies
19590 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19591 NULL, &cu->comp_unit_obstack,
19592 hashtab_obstack_allocate,
19593 dummy_obstack_deallocate);
19594
19595 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19596 if (*slot == NULL)
19597 *slot = ref_per_cu;
19598}
1c379e20 19599
f504f079
DE
19600/* Subroutine of dwarf2_mark to pass to htab_traverse.
19601 Set the mark field in every compilation unit in the
ae038cb0
DJ
19602 cache that we must keep because we are keeping CU. */
19603
10b3939b
DJ
19604static int
19605dwarf2_mark_helper (void **slot, void *data)
19606{
19607 struct dwarf2_per_cu_data *per_cu;
19608
19609 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19610
19611 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19612 reading of the chain. As such dependencies remain valid it is not much
19613 useful to track and undo them during QUIT cleanups. */
19614 if (per_cu->cu == NULL)
19615 return 1;
19616
10b3939b
DJ
19617 if (per_cu->cu->mark)
19618 return 1;
19619 per_cu->cu->mark = 1;
19620
19621 if (per_cu->cu->dependencies != NULL)
19622 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19623
19624 return 1;
19625}
19626
f504f079
DE
19627/* Set the mark field in CU and in every other compilation unit in the
19628 cache that we must keep because we are keeping CU. */
19629
ae038cb0
DJ
19630static void
19631dwarf2_mark (struct dwarf2_cu *cu)
19632{
19633 if (cu->mark)
19634 return;
19635 cu->mark = 1;
10b3939b
DJ
19636 if (cu->dependencies != NULL)
19637 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19638}
19639
19640static void
19641dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19642{
19643 while (per_cu)
19644 {
19645 per_cu->cu->mark = 0;
19646 per_cu = per_cu->cu->read_in_chain;
19647 }
72bf9492
DJ
19648}
19649
72bf9492
DJ
19650/* Trivial hash function for partial_die_info: the hash value of a DIE
19651 is its offset in .debug_info for this objfile. */
19652
19653static hashval_t
19654partial_die_hash (const void *item)
19655{
19656 const struct partial_die_info *part_die = item;
9a619af0 19657
b64f50a1 19658 return part_die->offset.sect_off;
72bf9492
DJ
19659}
19660
19661/* Trivial comparison function for partial_die_info structures: two DIEs
19662 are equal if they have the same offset. */
19663
19664static int
19665partial_die_eq (const void *item_lhs, const void *item_rhs)
19666{
19667 const struct partial_die_info *part_die_lhs = item_lhs;
19668 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19669
b64f50a1 19670 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19671}
19672
ae038cb0
DJ
19673static struct cmd_list_element *set_dwarf2_cmdlist;
19674static struct cmd_list_element *show_dwarf2_cmdlist;
19675
19676static void
19677set_dwarf2_cmd (char *args, int from_tty)
19678{
19679 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19680}
19681
19682static void
19683show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19684{
ae038cb0
DJ
19685 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19686}
19687
4bf44c1c 19688/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19689
19690static void
c1bd65d0 19691dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19692{
19693 struct dwarf2_per_objfile *data = d;
8b70b953 19694 int ix;
8b70b953 19695
95554aad
TT
19696 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19697 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19698 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19699
19700 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19701 VEC_free (dwarf2_per_cu_ptr,
19702 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19703
8b70b953 19704 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19705
19706 if (data->dwo_files)
19707 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19708
19709 if (data->dwz_file && data->dwz_file->dwz_bfd)
19710 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19711}
19712
19713\f
ae2de4f8 19714/* The "save gdb-index" command. */
9291a0cd
TT
19715
19716/* The contents of the hash table we create when building the string
19717 table. */
19718struct strtab_entry
19719{
19720 offset_type offset;
19721 const char *str;
19722};
19723
559a7a62
JK
19724/* Hash function for a strtab_entry.
19725
19726 Function is used only during write_hash_table so no index format backward
19727 compatibility is needed. */
b89be57b 19728
9291a0cd
TT
19729static hashval_t
19730hash_strtab_entry (const void *e)
19731{
19732 const struct strtab_entry *entry = e;
559a7a62 19733 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19734}
19735
19736/* Equality function for a strtab_entry. */
b89be57b 19737
9291a0cd
TT
19738static int
19739eq_strtab_entry (const void *a, const void *b)
19740{
19741 const struct strtab_entry *ea = a;
19742 const struct strtab_entry *eb = b;
19743 return !strcmp (ea->str, eb->str);
19744}
19745
19746/* Create a strtab_entry hash table. */
b89be57b 19747
9291a0cd
TT
19748static htab_t
19749create_strtab (void)
19750{
19751 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19752 xfree, xcalloc, xfree);
19753}
19754
19755/* Add a string to the constant pool. Return the string's offset in
19756 host order. */
b89be57b 19757
9291a0cd
TT
19758static offset_type
19759add_string (htab_t table, struct obstack *cpool, const char *str)
19760{
19761 void **slot;
19762 struct strtab_entry entry;
19763 struct strtab_entry *result;
19764
19765 entry.str = str;
19766 slot = htab_find_slot (table, &entry, INSERT);
19767 if (*slot)
19768 result = *slot;
19769 else
19770 {
19771 result = XNEW (struct strtab_entry);
19772 result->offset = obstack_object_size (cpool);
19773 result->str = str;
19774 obstack_grow_str0 (cpool, str);
19775 *slot = result;
19776 }
19777 return result->offset;
19778}
19779
19780/* An entry in the symbol table. */
19781struct symtab_index_entry
19782{
19783 /* The name of the symbol. */
19784 const char *name;
19785 /* The offset of the name in the constant pool. */
19786 offset_type index_offset;
19787 /* A sorted vector of the indices of all the CUs that hold an object
19788 of this name. */
19789 VEC (offset_type) *cu_indices;
19790};
19791
19792/* The symbol table. This is a power-of-2-sized hash table. */
19793struct mapped_symtab
19794{
19795 offset_type n_elements;
19796 offset_type size;
19797 struct symtab_index_entry **data;
19798};
19799
19800/* Hash function for a symtab_index_entry. */
b89be57b 19801
9291a0cd
TT
19802static hashval_t
19803hash_symtab_entry (const void *e)
19804{
19805 const struct symtab_index_entry *entry = e;
19806 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19807 sizeof (offset_type) * VEC_length (offset_type,
19808 entry->cu_indices),
19809 0);
19810}
19811
19812/* Equality function for a symtab_index_entry. */
b89be57b 19813
9291a0cd
TT
19814static int
19815eq_symtab_entry (const void *a, const void *b)
19816{
19817 const struct symtab_index_entry *ea = a;
19818 const struct symtab_index_entry *eb = b;
19819 int len = VEC_length (offset_type, ea->cu_indices);
19820 if (len != VEC_length (offset_type, eb->cu_indices))
19821 return 0;
19822 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19823 VEC_address (offset_type, eb->cu_indices),
19824 sizeof (offset_type) * len);
19825}
19826
19827/* Destroy a symtab_index_entry. */
b89be57b 19828
9291a0cd
TT
19829static void
19830delete_symtab_entry (void *p)
19831{
19832 struct symtab_index_entry *entry = p;
19833 VEC_free (offset_type, entry->cu_indices);
19834 xfree (entry);
19835}
19836
19837/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19838
9291a0cd 19839static htab_t
3876f04e 19840create_symbol_hash_table (void)
9291a0cd
TT
19841{
19842 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19843 delete_symtab_entry, xcalloc, xfree);
19844}
19845
19846/* Create a new mapped symtab object. */
b89be57b 19847
9291a0cd
TT
19848static struct mapped_symtab *
19849create_mapped_symtab (void)
19850{
19851 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19852 symtab->n_elements = 0;
19853 symtab->size = 1024;
19854 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19855 return symtab;
19856}
19857
19858/* Destroy a mapped_symtab. */
b89be57b 19859
9291a0cd
TT
19860static void
19861cleanup_mapped_symtab (void *p)
19862{
19863 struct mapped_symtab *symtab = p;
19864 /* The contents of the array are freed when the other hash table is
19865 destroyed. */
19866 xfree (symtab->data);
19867 xfree (symtab);
19868}
19869
19870/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19871 the slot.
19872
19873 Function is used only during write_hash_table so no index format backward
19874 compatibility is needed. */
b89be57b 19875
9291a0cd
TT
19876static struct symtab_index_entry **
19877find_slot (struct mapped_symtab *symtab, const char *name)
19878{
559a7a62 19879 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19880
19881 index = hash & (symtab->size - 1);
19882 step = ((hash * 17) & (symtab->size - 1)) | 1;
19883
19884 for (;;)
19885 {
19886 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19887 return &symtab->data[index];
19888 index = (index + step) & (symtab->size - 1);
19889 }
19890}
19891
19892/* Expand SYMTAB's hash table. */
b89be57b 19893
9291a0cd
TT
19894static void
19895hash_expand (struct mapped_symtab *symtab)
19896{
19897 offset_type old_size = symtab->size;
19898 offset_type i;
19899 struct symtab_index_entry **old_entries = symtab->data;
19900
19901 symtab->size *= 2;
19902 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19903
19904 for (i = 0; i < old_size; ++i)
19905 {
19906 if (old_entries[i])
19907 {
19908 struct symtab_index_entry **slot = find_slot (symtab,
19909 old_entries[i]->name);
19910 *slot = old_entries[i];
19911 }
19912 }
19913
19914 xfree (old_entries);
19915}
19916
156942c7
DE
19917/* Add an entry to SYMTAB. NAME is the name of the symbol.
19918 CU_INDEX is the index of the CU in which the symbol appears.
19919 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19920
9291a0cd
TT
19921static void
19922add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19923 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19924 offset_type cu_index)
19925{
19926 struct symtab_index_entry **slot;
156942c7 19927 offset_type cu_index_and_attrs;
9291a0cd
TT
19928
19929 ++symtab->n_elements;
19930 if (4 * symtab->n_elements / 3 >= symtab->size)
19931 hash_expand (symtab);
19932
19933 slot = find_slot (symtab, name);
19934 if (!*slot)
19935 {
19936 *slot = XNEW (struct symtab_index_entry);
19937 (*slot)->name = name;
156942c7 19938 /* index_offset is set later. */
9291a0cd
TT
19939 (*slot)->cu_indices = NULL;
19940 }
156942c7
DE
19941
19942 cu_index_and_attrs = 0;
19943 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19944 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19945 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19946
19947 /* We don't want to record an index value twice as we want to avoid the
19948 duplication.
19949 We process all global symbols and then all static symbols
19950 (which would allow us to avoid the duplication by only having to check
19951 the last entry pushed), but a symbol could have multiple kinds in one CU.
19952 To keep things simple we don't worry about the duplication here and
19953 sort and uniqufy the list after we've processed all symbols. */
19954 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19955}
19956
19957/* qsort helper routine for uniquify_cu_indices. */
19958
19959static int
19960offset_type_compare (const void *ap, const void *bp)
19961{
19962 offset_type a = *(offset_type *) ap;
19963 offset_type b = *(offset_type *) bp;
19964
19965 return (a > b) - (b > a);
19966}
19967
19968/* Sort and remove duplicates of all symbols' cu_indices lists. */
19969
19970static void
19971uniquify_cu_indices (struct mapped_symtab *symtab)
19972{
19973 int i;
19974
19975 for (i = 0; i < symtab->size; ++i)
19976 {
19977 struct symtab_index_entry *entry = symtab->data[i];
19978
19979 if (entry
19980 && entry->cu_indices != NULL)
19981 {
19982 unsigned int next_to_insert, next_to_check;
19983 offset_type last_value;
19984
19985 qsort (VEC_address (offset_type, entry->cu_indices),
19986 VEC_length (offset_type, entry->cu_indices),
19987 sizeof (offset_type), offset_type_compare);
19988
19989 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19990 next_to_insert = 1;
19991 for (next_to_check = 1;
19992 next_to_check < VEC_length (offset_type, entry->cu_indices);
19993 ++next_to_check)
19994 {
19995 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19996 != last_value)
19997 {
19998 last_value = VEC_index (offset_type, entry->cu_indices,
19999 next_to_check);
20000 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20001 last_value);
20002 ++next_to_insert;
20003 }
20004 }
20005 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20006 }
20007 }
9291a0cd
TT
20008}
20009
20010/* Add a vector of indices to the constant pool. */
b89be57b 20011
9291a0cd 20012static offset_type
3876f04e 20013add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20014 struct symtab_index_entry *entry)
20015{
20016 void **slot;
20017
3876f04e 20018 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20019 if (!*slot)
20020 {
20021 offset_type len = VEC_length (offset_type, entry->cu_indices);
20022 offset_type val = MAYBE_SWAP (len);
20023 offset_type iter;
20024 int i;
20025
20026 *slot = entry;
20027 entry->index_offset = obstack_object_size (cpool);
20028
20029 obstack_grow (cpool, &val, sizeof (val));
20030 for (i = 0;
20031 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20032 ++i)
20033 {
20034 val = MAYBE_SWAP (iter);
20035 obstack_grow (cpool, &val, sizeof (val));
20036 }
20037 }
20038 else
20039 {
20040 struct symtab_index_entry *old_entry = *slot;
20041 entry->index_offset = old_entry->index_offset;
20042 entry = old_entry;
20043 }
20044 return entry->index_offset;
20045}
20046
20047/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20048 constant pool entries going into the obstack CPOOL. */
b89be57b 20049
9291a0cd
TT
20050static void
20051write_hash_table (struct mapped_symtab *symtab,
20052 struct obstack *output, struct obstack *cpool)
20053{
20054 offset_type i;
3876f04e 20055 htab_t symbol_hash_table;
9291a0cd
TT
20056 htab_t str_table;
20057
3876f04e 20058 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20059 str_table = create_strtab ();
3876f04e 20060
9291a0cd
TT
20061 /* We add all the index vectors to the constant pool first, to
20062 ensure alignment is ok. */
20063 for (i = 0; i < symtab->size; ++i)
20064 {
20065 if (symtab->data[i])
3876f04e 20066 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20067 }
20068
20069 /* Now write out the hash table. */
20070 for (i = 0; i < symtab->size; ++i)
20071 {
20072 offset_type str_off, vec_off;
20073
20074 if (symtab->data[i])
20075 {
20076 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20077 vec_off = symtab->data[i]->index_offset;
20078 }
20079 else
20080 {
20081 /* While 0 is a valid constant pool index, it is not valid
20082 to have 0 for both offsets. */
20083 str_off = 0;
20084 vec_off = 0;
20085 }
20086
20087 str_off = MAYBE_SWAP (str_off);
20088 vec_off = MAYBE_SWAP (vec_off);
20089
20090 obstack_grow (output, &str_off, sizeof (str_off));
20091 obstack_grow (output, &vec_off, sizeof (vec_off));
20092 }
20093
20094 htab_delete (str_table);
3876f04e 20095 htab_delete (symbol_hash_table);
9291a0cd
TT
20096}
20097
0a5429f6
DE
20098/* Struct to map psymtab to CU index in the index file. */
20099struct psymtab_cu_index_map
20100{
20101 struct partial_symtab *psymtab;
20102 unsigned int cu_index;
20103};
20104
20105static hashval_t
20106hash_psymtab_cu_index (const void *item)
20107{
20108 const struct psymtab_cu_index_map *map = item;
20109
20110 return htab_hash_pointer (map->psymtab);
20111}
20112
20113static int
20114eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20115{
20116 const struct psymtab_cu_index_map *lhs = item_lhs;
20117 const struct psymtab_cu_index_map *rhs = item_rhs;
20118
20119 return lhs->psymtab == rhs->psymtab;
20120}
20121
20122/* Helper struct for building the address table. */
20123struct addrmap_index_data
20124{
20125 struct objfile *objfile;
20126 struct obstack *addr_obstack;
20127 htab_t cu_index_htab;
20128
20129 /* Non-zero if the previous_* fields are valid.
20130 We can't write an entry until we see the next entry (since it is only then
20131 that we know the end of the entry). */
20132 int previous_valid;
20133 /* Index of the CU in the table of all CUs in the index file. */
20134 unsigned int previous_cu_index;
0963b4bd 20135 /* Start address of the CU. */
0a5429f6
DE
20136 CORE_ADDR previous_cu_start;
20137};
20138
20139/* Write an address entry to OBSTACK. */
b89be57b 20140
9291a0cd 20141static void
0a5429f6
DE
20142add_address_entry (struct objfile *objfile, struct obstack *obstack,
20143 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20144{
0a5429f6 20145 offset_type cu_index_to_write;
9291a0cd
TT
20146 char addr[8];
20147 CORE_ADDR baseaddr;
20148
20149 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20150
0a5429f6
DE
20151 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20152 obstack_grow (obstack, addr, 8);
20153 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20154 obstack_grow (obstack, addr, 8);
20155 cu_index_to_write = MAYBE_SWAP (cu_index);
20156 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20157}
20158
20159/* Worker function for traversing an addrmap to build the address table. */
20160
20161static int
20162add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20163{
20164 struct addrmap_index_data *data = datap;
20165 struct partial_symtab *pst = obj;
0a5429f6
DE
20166
20167 if (data->previous_valid)
20168 add_address_entry (data->objfile, data->addr_obstack,
20169 data->previous_cu_start, start_addr,
20170 data->previous_cu_index);
20171
20172 data->previous_cu_start = start_addr;
20173 if (pst != NULL)
20174 {
20175 struct psymtab_cu_index_map find_map, *map;
20176 find_map.psymtab = pst;
20177 map = htab_find (data->cu_index_htab, &find_map);
20178 gdb_assert (map != NULL);
20179 data->previous_cu_index = map->cu_index;
20180 data->previous_valid = 1;
20181 }
20182 else
20183 data->previous_valid = 0;
20184
20185 return 0;
20186}
20187
20188/* Write OBJFILE's address map to OBSTACK.
20189 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20190 in the index file. */
20191
20192static void
20193write_address_map (struct objfile *objfile, struct obstack *obstack,
20194 htab_t cu_index_htab)
20195{
20196 struct addrmap_index_data addrmap_index_data;
20197
20198 /* When writing the address table, we have to cope with the fact that
20199 the addrmap iterator only provides the start of a region; we have to
20200 wait until the next invocation to get the start of the next region. */
20201
20202 addrmap_index_data.objfile = objfile;
20203 addrmap_index_data.addr_obstack = obstack;
20204 addrmap_index_data.cu_index_htab = cu_index_htab;
20205 addrmap_index_data.previous_valid = 0;
20206
20207 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20208 &addrmap_index_data);
20209
20210 /* It's highly unlikely the last entry (end address = 0xff...ff)
20211 is valid, but we should still handle it.
20212 The end address is recorded as the start of the next region, but that
20213 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20214 anyway. */
20215 if (addrmap_index_data.previous_valid)
20216 add_address_entry (objfile, obstack,
20217 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20218 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20219}
20220
156942c7
DE
20221/* Return the symbol kind of PSYM. */
20222
20223static gdb_index_symbol_kind
20224symbol_kind (struct partial_symbol *psym)
20225{
20226 domain_enum domain = PSYMBOL_DOMAIN (psym);
20227 enum address_class aclass = PSYMBOL_CLASS (psym);
20228
20229 switch (domain)
20230 {
20231 case VAR_DOMAIN:
20232 switch (aclass)
20233 {
20234 case LOC_BLOCK:
20235 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20236 case LOC_TYPEDEF:
20237 return GDB_INDEX_SYMBOL_KIND_TYPE;
20238 case LOC_COMPUTED:
20239 case LOC_CONST_BYTES:
20240 case LOC_OPTIMIZED_OUT:
20241 case LOC_STATIC:
20242 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20243 case LOC_CONST:
20244 /* Note: It's currently impossible to recognize psyms as enum values
20245 short of reading the type info. For now punt. */
20246 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20247 default:
20248 /* There are other LOC_FOO values that one might want to classify
20249 as variables, but dwarf2read.c doesn't currently use them. */
20250 return GDB_INDEX_SYMBOL_KIND_OTHER;
20251 }
20252 case STRUCT_DOMAIN:
20253 return GDB_INDEX_SYMBOL_KIND_TYPE;
20254 default:
20255 return GDB_INDEX_SYMBOL_KIND_OTHER;
20256 }
20257}
20258
9291a0cd 20259/* Add a list of partial symbols to SYMTAB. */
b89be57b 20260
9291a0cd
TT
20261static void
20262write_psymbols (struct mapped_symtab *symtab,
987d643c 20263 htab_t psyms_seen,
9291a0cd
TT
20264 struct partial_symbol **psymp,
20265 int count,
987d643c
TT
20266 offset_type cu_index,
20267 int is_static)
9291a0cd
TT
20268{
20269 for (; count-- > 0; ++psymp)
20270 {
156942c7
DE
20271 struct partial_symbol *psym = *psymp;
20272 void **slot;
987d643c 20273
156942c7 20274 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20275 error (_("Ada is not currently supported by the index"));
987d643c 20276
987d643c 20277 /* Only add a given psymbol once. */
156942c7 20278 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20279 if (!*slot)
20280 {
156942c7
DE
20281 gdb_index_symbol_kind kind = symbol_kind (psym);
20282
20283 *slot = psym;
20284 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20285 is_static, kind, cu_index);
987d643c 20286 }
9291a0cd
TT
20287 }
20288}
20289
20290/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20291 exception if there is an error. */
b89be57b 20292
9291a0cd
TT
20293static void
20294write_obstack (FILE *file, struct obstack *obstack)
20295{
20296 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20297 file)
20298 != obstack_object_size (obstack))
20299 error (_("couldn't data write to file"));
20300}
20301
20302/* Unlink a file if the argument is not NULL. */
b89be57b 20303
9291a0cd
TT
20304static void
20305unlink_if_set (void *p)
20306{
20307 char **filename = p;
20308 if (*filename)
20309 unlink (*filename);
20310}
20311
1fd400ff
TT
20312/* A helper struct used when iterating over debug_types. */
20313struct signatured_type_index_data
20314{
20315 struct objfile *objfile;
20316 struct mapped_symtab *symtab;
20317 struct obstack *types_list;
987d643c 20318 htab_t psyms_seen;
1fd400ff
TT
20319 int cu_index;
20320};
20321
20322/* A helper function that writes a single signatured_type to an
20323 obstack. */
b89be57b 20324
1fd400ff
TT
20325static int
20326write_one_signatured_type (void **slot, void *d)
20327{
20328 struct signatured_type_index_data *info = d;
20329 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20330 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20331 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20332 gdb_byte val[8];
20333
20334 write_psymbols (info->symtab,
987d643c 20335 info->psyms_seen,
3e43a32a
MS
20336 info->objfile->global_psymbols.list
20337 + psymtab->globals_offset,
987d643c
TT
20338 psymtab->n_global_syms, info->cu_index,
20339 0);
1fd400ff 20340 write_psymbols (info->symtab,
987d643c 20341 info->psyms_seen,
3e43a32a
MS
20342 info->objfile->static_psymbols.list
20343 + psymtab->statics_offset,
987d643c
TT
20344 psymtab->n_static_syms, info->cu_index,
20345 1);
1fd400ff 20346
b64f50a1
JK
20347 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20348 entry->per_cu.offset.sect_off);
1fd400ff 20349 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20350 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20351 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20352 obstack_grow (info->types_list, val, 8);
20353 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20354 obstack_grow (info->types_list, val, 8);
20355
20356 ++info->cu_index;
20357
20358 return 1;
20359}
20360
95554aad
TT
20361/* Recurse into all "included" dependencies and write their symbols as
20362 if they appeared in this psymtab. */
20363
20364static void
20365recursively_write_psymbols (struct objfile *objfile,
20366 struct partial_symtab *psymtab,
20367 struct mapped_symtab *symtab,
20368 htab_t psyms_seen,
20369 offset_type cu_index)
20370{
20371 int i;
20372
20373 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20374 if (psymtab->dependencies[i]->user != NULL)
20375 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20376 symtab, psyms_seen, cu_index);
20377
20378 write_psymbols (symtab,
20379 psyms_seen,
20380 objfile->global_psymbols.list + psymtab->globals_offset,
20381 psymtab->n_global_syms, cu_index,
20382 0);
20383 write_psymbols (symtab,
20384 psyms_seen,
20385 objfile->static_psymbols.list + psymtab->statics_offset,
20386 psymtab->n_static_syms, cu_index,
20387 1);
20388}
20389
9291a0cd 20390/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20391
9291a0cd
TT
20392static void
20393write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20394{
20395 struct cleanup *cleanup;
20396 char *filename, *cleanup_filename;
1fd400ff
TT
20397 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20398 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20399 int i;
20400 FILE *out_file;
20401 struct mapped_symtab *symtab;
20402 offset_type val, size_of_contents, total_len;
20403 struct stat st;
987d643c 20404 htab_t psyms_seen;
0a5429f6
DE
20405 htab_t cu_index_htab;
20406 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20407
b4f2f049 20408 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20409 return;
b4f2f049 20410
9291a0cd
TT
20411 if (dwarf2_per_objfile->using_index)
20412 error (_("Cannot use an index to create the index"));
20413
8b70b953
TT
20414 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20415 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20416
9291a0cd 20417 if (stat (objfile->name, &st) < 0)
7e17e088 20418 perror_with_name (objfile->name);
9291a0cd
TT
20419
20420 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20421 INDEX_SUFFIX, (char *) NULL);
20422 cleanup = make_cleanup (xfree, filename);
20423
20424 out_file = fopen (filename, "wb");
20425 if (!out_file)
20426 error (_("Can't open `%s' for writing"), filename);
20427
20428 cleanup_filename = filename;
20429 make_cleanup (unlink_if_set, &cleanup_filename);
20430
20431 symtab = create_mapped_symtab ();
20432 make_cleanup (cleanup_mapped_symtab, symtab);
20433
20434 obstack_init (&addr_obstack);
20435 make_cleanup_obstack_free (&addr_obstack);
20436
20437 obstack_init (&cu_list);
20438 make_cleanup_obstack_free (&cu_list);
20439
1fd400ff
TT
20440 obstack_init (&types_cu_list);
20441 make_cleanup_obstack_free (&types_cu_list);
20442
987d643c
TT
20443 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20444 NULL, xcalloc, xfree);
96408a79 20445 make_cleanup_htab_delete (psyms_seen);
987d643c 20446
0a5429f6
DE
20447 /* While we're scanning CU's create a table that maps a psymtab pointer
20448 (which is what addrmap records) to its index (which is what is recorded
20449 in the index file). This will later be needed to write the address
20450 table. */
20451 cu_index_htab = htab_create_alloc (100,
20452 hash_psymtab_cu_index,
20453 eq_psymtab_cu_index,
20454 NULL, xcalloc, xfree);
96408a79 20455 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20456 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20457 xmalloc (sizeof (struct psymtab_cu_index_map)
20458 * dwarf2_per_objfile->n_comp_units);
20459 make_cleanup (xfree, psymtab_cu_index_map);
20460
20461 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20462 work here. Also, the debug_types entries do not appear in
20463 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20464 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20465 {
3e43a32a
MS
20466 struct dwarf2_per_cu_data *per_cu
20467 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20468 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20469 gdb_byte val[8];
0a5429f6
DE
20470 struct psymtab_cu_index_map *map;
20471 void **slot;
9291a0cd 20472
95554aad
TT
20473 if (psymtab->user == NULL)
20474 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20475
0a5429f6
DE
20476 map = &psymtab_cu_index_map[i];
20477 map->psymtab = psymtab;
20478 map->cu_index = i;
20479 slot = htab_find_slot (cu_index_htab, map, INSERT);
20480 gdb_assert (slot != NULL);
20481 gdb_assert (*slot == NULL);
20482 *slot = map;
9291a0cd 20483
b64f50a1
JK
20484 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20485 per_cu->offset.sect_off);
9291a0cd 20486 obstack_grow (&cu_list, val, 8);
e254ef6a 20487 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20488 obstack_grow (&cu_list, val, 8);
20489 }
20490
0a5429f6
DE
20491 /* Dump the address map. */
20492 write_address_map (objfile, &addr_obstack, cu_index_htab);
20493
1fd400ff
TT
20494 /* Write out the .debug_type entries, if any. */
20495 if (dwarf2_per_objfile->signatured_types)
20496 {
20497 struct signatured_type_index_data sig_data;
20498
20499 sig_data.objfile = objfile;
20500 sig_data.symtab = symtab;
20501 sig_data.types_list = &types_cu_list;
987d643c 20502 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20503 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20504 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20505 write_one_signatured_type, &sig_data);
20506 }
20507
156942c7
DE
20508 /* Now that we've processed all symbols we can shrink their cu_indices
20509 lists. */
20510 uniquify_cu_indices (symtab);
20511
9291a0cd
TT
20512 obstack_init (&constant_pool);
20513 make_cleanup_obstack_free (&constant_pool);
20514 obstack_init (&symtab_obstack);
20515 make_cleanup_obstack_free (&symtab_obstack);
20516 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20517
20518 obstack_init (&contents);
20519 make_cleanup_obstack_free (&contents);
1fd400ff 20520 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20521 total_len = size_of_contents;
20522
20523 /* The version number. */
796a7ff8 20524 val = MAYBE_SWAP (8);
9291a0cd
TT
20525 obstack_grow (&contents, &val, sizeof (val));
20526
20527 /* The offset of the CU list from the start of the file. */
20528 val = MAYBE_SWAP (total_len);
20529 obstack_grow (&contents, &val, sizeof (val));
20530 total_len += obstack_object_size (&cu_list);
20531
1fd400ff
TT
20532 /* The offset of the types CU list from the start of the file. */
20533 val = MAYBE_SWAP (total_len);
20534 obstack_grow (&contents, &val, sizeof (val));
20535 total_len += obstack_object_size (&types_cu_list);
20536
9291a0cd
TT
20537 /* The offset of the address table from the start of the file. */
20538 val = MAYBE_SWAP (total_len);
20539 obstack_grow (&contents, &val, sizeof (val));
20540 total_len += obstack_object_size (&addr_obstack);
20541
20542 /* The offset of the symbol table from the start of the file. */
20543 val = MAYBE_SWAP (total_len);
20544 obstack_grow (&contents, &val, sizeof (val));
20545 total_len += obstack_object_size (&symtab_obstack);
20546
20547 /* The offset of the constant pool from the start of the file. */
20548 val = MAYBE_SWAP (total_len);
20549 obstack_grow (&contents, &val, sizeof (val));
20550 total_len += obstack_object_size (&constant_pool);
20551
20552 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20553
20554 write_obstack (out_file, &contents);
20555 write_obstack (out_file, &cu_list);
1fd400ff 20556 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20557 write_obstack (out_file, &addr_obstack);
20558 write_obstack (out_file, &symtab_obstack);
20559 write_obstack (out_file, &constant_pool);
20560
20561 fclose (out_file);
20562
20563 /* We want to keep the file, so we set cleanup_filename to NULL
20564 here. See unlink_if_set. */
20565 cleanup_filename = NULL;
20566
20567 do_cleanups (cleanup);
20568}
20569
90476074
TT
20570/* Implementation of the `save gdb-index' command.
20571
20572 Note that the file format used by this command is documented in the
20573 GDB manual. Any changes here must be documented there. */
11570e71 20574
9291a0cd
TT
20575static void
20576save_gdb_index_command (char *arg, int from_tty)
20577{
20578 struct objfile *objfile;
20579
20580 if (!arg || !*arg)
96d19272 20581 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20582
20583 ALL_OBJFILES (objfile)
20584 {
20585 struct stat st;
20586
20587 /* If the objfile does not correspond to an actual file, skip it. */
20588 if (stat (objfile->name, &st) < 0)
20589 continue;
20590
20591 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20592 if (dwarf2_per_objfile)
20593 {
20594 volatile struct gdb_exception except;
20595
20596 TRY_CATCH (except, RETURN_MASK_ERROR)
20597 {
20598 write_psymtabs_to_index (objfile, arg);
20599 }
20600 if (except.reason < 0)
20601 exception_fprintf (gdb_stderr, except,
20602 _("Error while writing index for `%s': "),
20603 objfile->name);
20604 }
20605 }
dce234bc
PP
20606}
20607
9291a0cd
TT
20608\f
20609
9eae7c52
TT
20610int dwarf2_always_disassemble;
20611
20612static void
20613show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20614 struct cmd_list_element *c, const char *value)
20615{
3e43a32a
MS
20616 fprintf_filtered (file,
20617 _("Whether to always disassemble "
20618 "DWARF expressions is %s.\n"),
9eae7c52
TT
20619 value);
20620}
20621
900e11f9
JK
20622static void
20623show_check_physname (struct ui_file *file, int from_tty,
20624 struct cmd_list_element *c, const char *value)
20625{
20626 fprintf_filtered (file,
20627 _("Whether to check \"physname\" is %s.\n"),
20628 value);
20629}
20630
6502dd73
DJ
20631void _initialize_dwarf2_read (void);
20632
20633void
20634_initialize_dwarf2_read (void)
20635{
96d19272
JK
20636 struct cmd_list_element *c;
20637
dce234bc 20638 dwarf2_objfile_data_key
c1bd65d0 20639 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20640
1bedd215
AC
20641 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20642Set DWARF 2 specific variables.\n\
20643Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20644 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20645 0/*allow-unknown*/, &maintenance_set_cmdlist);
20646
1bedd215
AC
20647 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20648Show DWARF 2 specific variables\n\
20649Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20650 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20651 0/*allow-unknown*/, &maintenance_show_cmdlist);
20652
20653 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20654 &dwarf2_max_cache_age, _("\
20655Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20656Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20657A higher limit means that cached compilation units will be stored\n\
20658in memory longer, and more total memory will be used. Zero disables\n\
20659caching, which can slow down startup."),
2c5b56ce 20660 NULL,
920d2a44 20661 show_dwarf2_max_cache_age,
2c5b56ce 20662 &set_dwarf2_cmdlist,
ae038cb0 20663 &show_dwarf2_cmdlist);
d97bc12b 20664
9eae7c52
TT
20665 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20666 &dwarf2_always_disassemble, _("\
20667Set whether `info address' always disassembles DWARF expressions."), _("\
20668Show whether `info address' always disassembles DWARF expressions."), _("\
20669When enabled, DWARF expressions are always printed in an assembly-like\n\
20670syntax. When disabled, expressions will be printed in a more\n\
20671conversational style, when possible."),
20672 NULL,
20673 show_dwarf2_always_disassemble,
20674 &set_dwarf2_cmdlist,
20675 &show_dwarf2_cmdlist);
20676
45cfd468
DE
20677 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20678Set debugging of the dwarf2 reader."), _("\
20679Show debugging of the dwarf2 reader."), _("\
20680When enabled, debugging messages are printed during dwarf2 reading\n\
20681and symtab expansion."),
20682 NULL,
20683 NULL,
20684 &setdebuglist, &showdebuglist);
20685
ccce17b0 20686 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20687Set debugging of the dwarf2 DIE reader."), _("\
20688Show debugging of the dwarf2 DIE reader."), _("\
20689When enabled (non-zero), DIEs are dumped after they are read in.\n\
20690The value is the maximum depth to print."),
ccce17b0
YQ
20691 NULL,
20692 NULL,
20693 &setdebuglist, &showdebuglist);
9291a0cd 20694
900e11f9
JK
20695 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20696Set cross-checking of \"physname\" code against demangler."), _("\
20697Show cross-checking of \"physname\" code against demangler."), _("\
20698When enabled, GDB's internal \"physname\" code is checked against\n\
20699the demangler."),
20700 NULL, show_check_physname,
20701 &setdebuglist, &showdebuglist);
20702
e615022a
DE
20703 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20704 no_class, &use_deprecated_index_sections, _("\
20705Set whether to use deprecated gdb_index sections."), _("\
20706Show whether to use deprecated gdb_index sections."), _("\
20707When enabled, deprecated .gdb_index sections are used anyway.\n\
20708Normally they are ignored either because of a missing feature or\n\
20709performance issue.\n\
20710Warning: This option must be enabled before gdb reads the file."),
20711 NULL,
20712 NULL,
20713 &setlist, &showlist);
20714
96d19272 20715 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20716 _("\
fc1a9d6e 20717Save a gdb-index file.\n\
11570e71 20718Usage: save gdb-index DIRECTORY"),
96d19272
JK
20719 &save_cmdlist);
20720 set_cmd_completer (c, filename_completer);
6502dd73 20721}
This page took 2.791055 seconds and 4 git commands to generate.