* Makefile.in (DEPMODE, DEPDIR, depcomp, COMPILE.pre)
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 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"
4c2df51b 70
c906108c
SS
71#include <fcntl.h>
72#include "gdb_string.h"
4bdf3d34 73#include "gdb_assert.h"
c906108c 74#include <sys/types.h>
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
45cfd468
DE
79/* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81static int dwarf2_read_debug = 0;
82
d97bc12b 83/* When non-zero, dump DIEs after they are read in. */
ccce17b0 84static unsigned int dwarf2_die_debug = 0;
d97bc12b 85
900e11f9
JK
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
481860b3 89/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 90static int use_deprecated_index_sections = 0;
481860b3 91
df8a16a1
DJ
92/* When set, the file that we're processing is known to have debugging
93 info for C++ namespaces. GCC 3.3.x did not produce this information,
94 but later versions do. */
95
96static int processing_has_namespace_info;
97
6502dd73
DJ
98static const struct objfile_data *dwarf2_objfile_data_key;
99
dce234bc
PP
100struct dwarf2_section_info
101{
102 asection *asection;
103 gdb_byte *buffer;
104 bfd_size_type size;
be391dca
TT
105 /* True if we have tried to read this section. */
106 int readin;
dce234bc
PP
107};
108
8b70b953
TT
109typedef struct dwarf2_section_info dwarf2_section_info_def;
110DEF_VEC_O (dwarf2_section_info_def);
111
9291a0cd
TT
112/* All offsets in the index are of this type. It must be
113 architecture-independent. */
114typedef uint32_t offset_type;
115
116DEF_VEC_I (offset_type);
117
156942c7
DE
118/* Ensure only legit values are used. */
119#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
120 do { \
121 gdb_assert ((unsigned int) (value) <= 1); \
122 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
123 } while (0)
124
125/* Ensure only legit values are used. */
126#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
127 do { \
128 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
129 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
130 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133/* Ensure we don't use more than the alloted nuber of bits for the CU. */
134#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
137 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
138 } while (0)
139
9291a0cd
TT
140/* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142struct mapped_index
143{
559a7a62
JK
144 /* Index data format version. */
145 int version;
146
9291a0cd
TT
147 /* The total length of the buffer. */
148 off_t total_size;
b11b1f88 149
9291a0cd
TT
150 /* A pointer to the address table data. */
151 const gdb_byte *address_table;
b11b1f88 152
9291a0cd
TT
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
b11b1f88 155
3876f04e
DE
156 /* The symbol table, implemented as a hash table. */
157 const offset_type *symbol_table;
b11b1f88 158
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
b11b1f88 161
9291a0cd
TT
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164};
165
95554aad
TT
166typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
167DEF_VEC_P (dwarf2_per_cu_ptr);
168
9cdd5dbd
DE
169/* Collection of data recorded per objfile.
170 This hangs off of dwarf2_objfile_data_key. */
171
6502dd73
DJ
172struct dwarf2_per_objfile
173{
dce234bc
PP
174 struct dwarf2_section_info info;
175 struct dwarf2_section_info abbrev;
176 struct dwarf2_section_info line;
dce234bc
PP
177 struct dwarf2_section_info loc;
178 struct dwarf2_section_info macinfo;
cf2c3c16 179 struct dwarf2_section_info macro;
dce234bc
PP
180 struct dwarf2_section_info str;
181 struct dwarf2_section_info ranges;
3019eac3 182 struct dwarf2_section_info addr;
dce234bc
PP
183 struct dwarf2_section_info frame;
184 struct dwarf2_section_info eh_frame;
9291a0cd 185 struct dwarf2_section_info gdb_index;
ae038cb0 186
8b70b953
TT
187 VEC (dwarf2_section_info_def) *types;
188
be391dca
TT
189 /* Back link. */
190 struct objfile *objfile;
191
d467dd73 192 /* Table of all the compilation units. This is used to locate
10b3939b 193 the target compilation unit of a particular reference. */
ae038cb0
DJ
194 struct dwarf2_per_cu_data **all_comp_units;
195
196 /* The number of compilation units in ALL_COMP_UNITS. */
197 int n_comp_units;
198
1fd400ff 199 /* The number of .debug_types-related CUs. */
d467dd73 200 int n_type_units;
1fd400ff 201
d467dd73 202 /* The .debug_types-related CUs (TUs). */
b4dd5633 203 struct signatured_type **all_type_units;
1fd400ff 204
f4dc4d17
DE
205 /* The number of entries in all_type_unit_groups. */
206 int n_type_unit_groups;
207
208 /* Table of type unit groups.
209 This exists to make it easy to iterate over all CUs and TU groups. */
210 struct type_unit_group **all_type_unit_groups;
211
212 /* Table of struct type_unit_group objects.
213 The hash key is the DW_AT_stmt_list value. */
214 htab_t type_unit_groups;
72dca2f5 215
348e048f
DE
216 /* A table mapping .debug_types signatures to its signatured_type entry.
217 This is NULL if the .debug_types section hasn't been read in yet. */
218 htab_t signatured_types;
219
f4dc4d17
DE
220 /* Type unit statistics, to see how well the scaling improvements
221 are doing. */
222 struct tu_stats
223 {
224 int nr_uniq_abbrev_tables;
225 int nr_symtabs;
226 int nr_symtab_sharers;
227 int nr_stmt_less_type_units;
228 } tu_stats;
229
230 /* A chain of compilation units that are currently read in, so that
231 they can be freed later. */
232 struct dwarf2_per_cu_data *read_in_chain;
233
3019eac3
DE
234 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
235 This is NULL if the table hasn't been allocated yet. */
236 htab_t dwo_files;
237
80626a55
DE
238 /* Non-zero if we've check for whether there is a DWP file. */
239 int dwp_checked;
240
241 /* The DWP file if there is one, or NULL. */
242 struct dwp_file *dwp_file;
243
36586728
TT
244 /* The shared '.dwz' file, if one exists. This is used when the
245 original data was compressed using 'dwz -m'. */
246 struct dwz_file *dwz_file;
247
72dca2f5
FR
248 /* A flag indicating wether this objfile has a section loaded at a
249 VMA of 0. */
250 int has_section_at_zero;
9291a0cd 251
ae2de4f8
DE
252 /* True if we are using the mapped index,
253 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
254 unsigned char using_index;
255
ae2de4f8 256 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 257 struct mapped_index *index_table;
98bfdba5 258
7b9f3c50 259 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
260 TUs typically share line table entries with a CU, so we maintain a
261 separate table of all line table entries to support the sharing.
262 Note that while there can be way more TUs than CUs, we've already
263 sorted all the TUs into "type unit groups", grouped by their
264 DW_AT_stmt_list value. Therefore the only sharing done here is with a
265 CU and its associated TU group if there is one. */
7b9f3c50
DE
266 htab_t quick_file_names_table;
267
98bfdba5
PA
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
673bfd45 271
dee91e82 272 /* Table mapping type DIEs to their struct type *.
673bfd45 273 This is NULL if not allocated yet.
dee91e82
DE
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
95554aad
TT
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
279};
280
281static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 282
251d32d9 283/* Default names of the debugging sections. */
c906108c 284
233a11ab
CS
285/* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
9cdd5dbd
DE
288static const struct dwarf2_debug_sections dwarf2_elf_names =
289{
251d32d9
TG
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 295 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
3019eac3 299 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
24d3216f
TT
302 { ".gdb_index", ".zgdb_index" },
303 23
251d32d9 304};
c906108c 305
80626a55 306/* List of DWO/DWP sections. */
3019eac3 307
80626a55 308static const struct dwop_section_names
3019eac3
DE
309{
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
09262596
DE
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
3019eac3
DE
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
80626a55
DE
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
3019eac3 321}
80626a55 322dwop_section_names =
3019eac3
DE
323{
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
335};
336
c906108c
SS
337/* local data types */
338
107d2387
AC
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
c906108c 341struct comp_unit_head
a738430d 342{
c764a876 343 unsigned int length;
a738430d 344 short version;
a738430d
MK
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
b64f50a1 347 sect_offset abbrev_offset;
57349743 348
a738430d
MK
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
57349743 351
a738430d
MK
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
57349743 354
a738430d
MK
355 /* Offset to the first byte of this compilation unit header in the
356 .debug_info section, for resolving relative reference dies. */
b64f50a1 357 sect_offset offset;
57349743 358
d00adf39
DE
359 /* Offset to first die in this cu from the start of the cu.
360 This will be the first byte following the compilation unit header. */
b64f50a1 361 cu_offset first_die_offset;
a738430d 362};
c906108c 363
3da10d80
KS
364/* Type used for delaying computation of method physnames.
365 See comments for compute_delayed_physnames. */
366struct delayed_method_info
367{
368 /* The type to which the method is attached, i.e., its parent class. */
369 struct type *type;
370
371 /* The index of the method in the type's function fieldlists. */
372 int fnfield_index;
373
374 /* The index of the method in the fieldlist. */
375 int index;
376
377 /* The name of the DIE. */
378 const char *name;
379
380 /* The DIE associated with this method. */
381 struct die_info *die;
382};
383
384typedef struct delayed_method_info delayed_method_info;
385DEF_VEC_O (delayed_method_info);
386
e7c27a73
DJ
387/* Internal state when decoding a particular compilation unit. */
388struct dwarf2_cu
389{
390 /* The objfile containing this compilation unit. */
391 struct objfile *objfile;
392
d00adf39 393 /* The header of the compilation unit. */
e7c27a73 394 struct comp_unit_head header;
e142c38c 395
d00adf39
DE
396 /* Base address of this compilation unit. */
397 CORE_ADDR base_address;
398
399 /* Non-zero if base_address has been set. */
400 int base_known;
401
e142c38c
DJ
402 /* The language we are debugging. */
403 enum language language;
404 const struct language_defn *language_defn;
405
b0f35d58
DL
406 const char *producer;
407
e142c38c
DJ
408 /* The generic symbol table building routines have separate lists for
409 file scope symbols and all all other scopes (local scopes). So
410 we need to select the right one to pass to add_symbol_to_list().
411 We do it by keeping a pointer to the correct list in list_in_scope.
412
413 FIXME: The original dwarf code just treated the file scope as the
414 first local scope, and all other local scopes as nested local
415 scopes, and worked fine. Check to see if we really need to
416 distinguish these in buildsym.c. */
417 struct pending **list_in_scope;
418
433df2d4
DE
419 /* The abbrev table for this CU.
420 Normally this points to the abbrev table in the objfile.
421 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
422 struct abbrev_table *abbrev_table;
72bf9492 423
b64f50a1
JK
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
426 htab_t partial_dies;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 struct obstack comp_unit_obstack;
431
ae038cb0
DJ
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain;
437
438 /* Backchain to our per_cu entry if the tree has been built. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used;
443
b64f50a1
JK
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
51545339 446 htab_t die_hash;
10b3939b
DJ
447
448 /* Full DIEs if read in. */
449 struct die_info *dies;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies;
455
cb1df416
DJ
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header;
458
3da10d80
KS
459 /* A list of methods which need to have physnames computed
460 after all type information has been read. */
461 VEC (delayed_method_info) *method_list;
462
96408a79
SA
463 /* To be copied to symtab->call_site_htab. */
464 htab_t call_site_htab;
465
034e5797
DE
466 /* Non-NULL if this CU came from a DWO file.
467 There is an invariant here that is important to remember:
468 Except for attributes copied from the top level DIE in the "main"
469 (or "stub") file in preparation for reading the DWO file
470 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
471 Either there isn't a DWO file (in which case this is NULL and the point
472 is moot), or there is and either we're not going to read it (in which
473 case this is NULL) or there is and we are reading it (in which case this
474 is non-NULL). */
3019eac3
DE
475 struct dwo_unit *dwo_unit;
476
477 /* The DW_AT_addr_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 ULONGEST addr_base;
481
2e3cf129
DE
482 /* The DW_AT_ranges_base attribute if present, zero otherwise
483 (zero is a valid value though).
484 Note this value comes from the stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
486 be used without needing to know whether DWO files are in use or not. */
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;
e7c27a73
DJ
506};
507
10b3939b
DJ
508/* Persistent data held for a compilation unit, even when not
509 processing it. We put a pointer to this structure in the
28dee7f5 510 read_symtab_private field of the psymtab. */
10b3939b 511
ae038cb0
DJ
512struct dwarf2_per_cu_data
513{
36586728 514 /* The start offset and length of this compilation unit.
45452591 515 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
516 initial_length_size.
517 If the DIE refers to a DWO file, this is always of the original die,
518 not the DWO file. */
b64f50a1 519 sect_offset offset;
36586728 520 unsigned int length;
ae038cb0
DJ
521
522 /* Flag indicating this compilation unit will be read in before
523 any of the current compilation units are processed. */
c764a876 524 unsigned int queued : 1;
ae038cb0 525
0d99eb77
DE
526 /* This flag will be set when reading partial DIEs if we need to load
527 absolutely all DIEs for this compilation unit, instead of just the ones
528 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
529 hash table and don't find it. */
530 unsigned int load_all_dies : 1;
531
3019eac3
DE
532 /* Non-zero if this CU is from .debug_types. */
533 unsigned int is_debug_types : 1;
534
36586728
TT
535 /* Non-zero if this CU is from the .dwz file. */
536 unsigned int is_dwz : 1;
537
3019eac3
DE
538 /* The section this CU/TU lives in.
539 If the DIE refers to a DWO file, this is always the original die,
540 not the DWO file. */
541 struct dwarf2_section_info *info_or_types_section;
348e048f 542
17ea53c3
JK
543 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
544 of the CU cache it gets reset to NULL again. */
ae038cb0 545 struct dwarf2_cu *cu;
1c379e20 546
9cdd5dbd
DE
547 /* The corresponding objfile.
548 Normally we can get the objfile from dwarf2_per_objfile.
549 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
550 struct objfile *objfile;
551
552 /* When using partial symbol tables, the 'psymtab' field is active.
553 Otherwise the 'quick' field is active. */
554 union
555 {
556 /* The partial symbol table associated with this compilation unit,
95554aad 557 or NULL for unread partial units. */
9291a0cd
TT
558 struct partial_symtab *psymtab;
559
560 /* Data needed by the "quick" functions. */
561 struct dwarf2_per_cu_quick_data *quick;
562 } v;
95554aad 563
f4dc4d17
DE
564 union
565 {
566 /* The CUs we import using DW_TAG_imported_unit. This is filled in
567 while reading psymtabs, used to compute the psymtab dependencies,
568 and then cleared. Then it is filled in again while reading full
569 symbols, and only deleted when the objfile is destroyed. */
570 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
571
572 /* Type units are grouped by their DW_AT_stmt_list entry so that they
573 can share them. If this is a TU, this points to the containing
574 symtab. */
575 struct type_unit_group *type_unit_group;
576 } s;
ae038cb0
DJ
577};
578
348e048f
DE
579/* Entry in the signatured_types hash table. */
580
581struct signatured_type
582{
42e7ad6c
DE
583 /* The "per_cu" object of this type.
584 N.B.: This is the first member so that it's easy to convert pointers
585 between them. */
586 struct dwarf2_per_cu_data per_cu;
587
3019eac3 588 /* The type's signature. */
348e048f
DE
589 ULONGEST signature;
590
3019eac3
DE
591 /* Offset in the TU of the type's DIE, as read from the TU header.
592 If the definition lives in a DWO file, this value is unusable. */
593 cu_offset type_offset_in_tu;
594
595 /* Offset in the section of the type's DIE.
596 If the definition lives in a DWO file, this is the offset in the
597 .debug_types.dwo section.
598 The value is zero until the actual value is known.
599 Zero is otherwise not a valid section offset. */
600 sect_offset type_offset_in_section;
348e048f
DE
601};
602
094b34ac
DE
603/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
604 This includes type_unit_group and quick_file_names. */
605
606struct stmt_list_hash
607{
608 /* The DWO unit this table is from or NULL if there is none. */
609 struct dwo_unit *dwo_unit;
610
611 /* Offset in .debug_line or .debug_line.dwo. */
612 sect_offset line_offset;
613};
614
f4dc4d17
DE
615/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
616 an object of this type. */
617
618struct type_unit_group
619{
620 /* dwarf2read.c's main "handle" on the symtab.
621 To simplify things we create an artificial CU that "includes" all the
622 type units using this stmt_list so that the rest of the code still has
623 a "per_cu" handle on the symtab.
624 This PER_CU is recognized by having no section. */
625#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
626 struct dwarf2_per_cu_data per_cu;
627
628 union
629 {
630 /* The TUs that share this DW_AT_stmt_list entry.
631 This is added to while parsing type units to build partial symtabs,
632 and is deleted afterwards and not used again. */
633 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 634
094b34ac
DE
635 /* When reading the line table in "quick" functions, we need a real TU.
636 Any will do, we know they all share the same DW_AT_stmt_list entry.
637 For simplicity's sake, we pick the first one. */
638 struct dwarf2_per_cu_data *first_tu;
639 } t;
f4dc4d17
DE
640
641 /* The primary symtab.
094b34ac
DE
642 Type units in a group needn't all be defined in the same source file,
643 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
644 struct symtab *primary_symtab;
645
094b34ac
DE
646 /* The data used to construct the hash key. */
647 struct stmt_list_hash hash;
f4dc4d17
DE
648
649 /* The number of symtabs from the line header.
650 The value here must match line_header.num_file_names. */
651 unsigned int num_symtabs;
652
653 /* The symbol tables for this TU (obtained from the files listed in
654 DW_AT_stmt_list).
655 WARNING: The order of entries here must match the order of entries
656 in the line header. After the first TU using this type_unit_group, the
657 line header for the subsequent TUs is recreated from this. This is done
658 because we need to use the same symtabs for each TU using the same
659 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
660 there's no guarantee the line header doesn't have duplicate entries. */
661 struct symtab **symtabs;
662};
663
80626a55 664/* These sections are what may appear in a DWO file. */
3019eac3
DE
665
666struct dwo_sections
667{
668 struct dwarf2_section_info abbrev;
3019eac3
DE
669 struct dwarf2_section_info line;
670 struct dwarf2_section_info loc;
09262596
DE
671 struct dwarf2_section_info macinfo;
672 struct dwarf2_section_info macro;
3019eac3
DE
673 struct dwarf2_section_info str;
674 struct dwarf2_section_info str_offsets;
80626a55
DE
675 /* In the case of a virtual DWO file, these two are unused. */
676 struct dwarf2_section_info info;
3019eac3
DE
677 VEC (dwarf2_section_info_def) *types;
678};
679
680/* Common bits of DWO CUs/TUs. */
681
682struct dwo_unit
683{
684 /* Backlink to the containing struct dwo_file. */
685 struct dwo_file *dwo_file;
686
687 /* The "id" that distinguishes this CU/TU.
688 .debug_info calls this "dwo_id", .debug_types calls this "signature".
689 Since signatures came first, we stick with it for consistency. */
690 ULONGEST signature;
691
692 /* The section this CU/TU lives in, in the DWO file. */
693 struct dwarf2_section_info *info_or_types_section;
694
695 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
696 sect_offset offset;
697 unsigned int length;
698
699 /* For types, offset in the type's DIE of the type defined by this TU. */
700 cu_offset type_offset_in_tu;
701};
702
80626a55
DE
703/* Data for one DWO file.
704 This includes virtual DWO files that have been packaged into a
705 DWP file. */
3019eac3
DE
706
707struct dwo_file
708{
80626a55
DE
709 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
710 For virtual DWO files the name is constructed from the section offsets
711 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
712 from related CU+TUs. */
713 const char *name;
3019eac3 714
80626a55
DE
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 bfd *dbfd;
3019eac3
DE
718
719 /* Section info for this file. */
720 struct dwo_sections sections;
721
722 /* Table of CUs in the file.
723 Each element is a struct dwo_unit. */
724 htab_t cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_t tus;
729};
730
80626a55
DE
731/* These sections are what may appear in a DWP file. */
732
733struct dwp_sections
734{
735 struct dwarf2_section_info str;
736 struct dwarf2_section_info cu_index;
737 struct dwarf2_section_info tu_index;
738 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
739 by section number. We don't need to record them here. */
740};
741
742/* These sections are what may appear in a virtual DWO file. */
743
744struct virtual_dwo_sections
745{
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info line;
748 struct dwarf2_section_info loc;
749 struct dwarf2_section_info macinfo;
750 struct dwarf2_section_info macro;
751 struct dwarf2_section_info str_offsets;
752 /* Each DWP hash table entry records one CU or one TU.
753 That is recorded here, and copied to dwo_unit.info_or_types_section. */
754 struct dwarf2_section_info info_or_types;
755};
756
757/* Contents of DWP hash tables. */
758
759struct dwp_hash_table
760{
761 uint32_t nr_units, nr_slots;
762 const gdb_byte *hash_table, *unit_table, *section_pool;
763};
764
765/* Data for one DWP file. */
766
767struct dwp_file
768{
769 /* Name of the file. */
770 const char *name;
771
772 /* The bfd, when the file is open. Otherwise this is NULL. */
773 bfd *dbfd;
774
775 /* Section info for this file. */
776 struct dwp_sections sections;
777
778 /* Table of CUs in the file. */
779 const struct dwp_hash_table *cus;
780
781 /* Table of TUs in the file. */
782 const struct dwp_hash_table *tus;
783
784 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
785 htab_t loaded_cutus;
786
787 /* Table to map ELF section numbers to their sections. */
788 unsigned int num_sections;
789 asection **elf_sections;
790};
791
36586728
TT
792/* This represents a '.dwz' file. */
793
794struct dwz_file
795{
796 /* A dwz file can only contain a few sections. */
797 struct dwarf2_section_info abbrev;
798 struct dwarf2_section_info info;
799 struct dwarf2_section_info str;
800 struct dwarf2_section_info line;
801 struct dwarf2_section_info macro;
2ec9a5e0 802 struct dwarf2_section_info gdb_index;
36586728
TT
803
804 /* The dwz's BFD. */
805 bfd *dwz_bfd;
806};
807
0963b4bd
MS
808/* Struct used to pass misc. parameters to read_die_and_children, et
809 al. which are used for both .debug_info and .debug_types dies.
810 All parameters here are unchanging for the life of the call. This
dee91e82 811 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
812
813struct die_reader_specs
814{
dee91e82 815 /* die_section->asection->owner. */
93311388
DE
816 bfd* abfd;
817
818 /* The CU of the DIE we are parsing. */
819 struct dwarf2_cu *cu;
820
80626a55 821 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
822 struct dwo_file *dwo_file;
823
dee91e82 824 /* The section the die comes from.
3019eac3 825 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
826 struct dwarf2_section_info *die_section;
827
828 /* die_section->buffer. */
829 gdb_byte *buffer;
f664829e
DE
830
831 /* The end of the buffer. */
832 const gdb_byte *buffer_end;
93311388
DE
833};
834
fd820528 835/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
836typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
837 gdb_byte *info_ptr,
838 struct die_info *comp_unit_die,
839 int has_children,
840 void *data);
841
debd256d
JB
842/* The line number information for a compilation unit (found in the
843 .debug_line section) begins with a "statement program header",
844 which contains the following information. */
845struct line_header
846{
847 unsigned int total_length;
848 unsigned short version;
849 unsigned int header_length;
850 unsigned char minimum_instruction_length;
2dc7f7b3 851 unsigned char maximum_ops_per_instruction;
debd256d
JB
852 unsigned char default_is_stmt;
853 int line_base;
854 unsigned char line_range;
855 unsigned char opcode_base;
856
857 /* standard_opcode_lengths[i] is the number of operands for the
858 standard opcode whose value is i. This means that
859 standard_opcode_lengths[0] is unused, and the last meaningful
860 element is standard_opcode_lengths[opcode_base - 1]. */
861 unsigned char *standard_opcode_lengths;
862
863 /* The include_directories table. NOTE! These strings are not
864 allocated with xmalloc; instead, they are pointers into
865 debug_line_buffer. If you try to free them, `free' will get
866 indigestion. */
867 unsigned int num_include_dirs, include_dirs_size;
868 char **include_dirs;
869
870 /* The file_names table. NOTE! These strings are not allocated
871 with xmalloc; instead, they are pointers into debug_line_buffer.
872 Don't try to free them directly. */
873 unsigned int num_file_names, file_names_size;
874 struct file_entry
c906108c 875 {
debd256d
JB
876 char *name;
877 unsigned int dir_index;
878 unsigned int mod_time;
879 unsigned int length;
aaa75496 880 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 881 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
882 } *file_names;
883
884 /* The start and end of the statement program following this
6502dd73 885 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 886 gdb_byte *statement_program_start, *statement_program_end;
debd256d 887};
c906108c
SS
888
889/* When we construct a partial symbol table entry we only
0963b4bd 890 need this much information. */
c906108c
SS
891struct partial_die_info
892 {
72bf9492 893 /* Offset of this DIE. */
b64f50a1 894 sect_offset offset;
72bf9492
DJ
895
896 /* DWARF-2 tag for this DIE. */
897 ENUM_BITFIELD(dwarf_tag) tag : 16;
898
72bf9492
DJ
899 /* Assorted flags describing the data found in this DIE. */
900 unsigned int has_children : 1;
901 unsigned int is_external : 1;
902 unsigned int is_declaration : 1;
903 unsigned int has_type : 1;
904 unsigned int has_specification : 1;
905 unsigned int has_pc_info : 1;
481860b3 906 unsigned int may_be_inlined : 1;
72bf9492
DJ
907
908 /* Flag set if the SCOPE field of this structure has been
909 computed. */
910 unsigned int scope_set : 1;
911
fa4028e9
JB
912 /* Flag set if the DIE has a byte_size attribute. */
913 unsigned int has_byte_size : 1;
914
98bfdba5
PA
915 /* Flag set if any of the DIE's children are template arguments. */
916 unsigned int has_template_arguments : 1;
917
abc72ce4
DE
918 /* Flag set if fixup_partial_die has been called on this die. */
919 unsigned int fixup_called : 1;
920
36586728
TT
921 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
922 unsigned int is_dwz : 1;
923
924 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
925 unsigned int spec_is_dwz : 1;
926
72bf9492 927 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 928 sometimes a default name for unnamed DIEs. */
c906108c 929 char *name;
72bf9492 930
abc72ce4
DE
931 /* The linkage name, if present. */
932 const char *linkage_name;
933
72bf9492
DJ
934 /* The scope to prepend to our children. This is generally
935 allocated on the comp_unit_obstack, so will disappear
936 when this compilation unit leaves the cache. */
937 char *scope;
938
95554aad
TT
939 /* Some data associated with the partial DIE. The tag determines
940 which field is live. */
941 union
942 {
943 /* The location description associated with this DIE, if any. */
944 struct dwarf_block *locdesc;
945 /* The offset of an import, for DW_TAG_imported_unit. */
946 sect_offset offset;
947 } d;
72bf9492
DJ
948
949 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
950 CORE_ADDR lowpc;
951 CORE_ADDR highpc;
72bf9492 952
93311388 953 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 954 DW_AT_sibling, if any. */
abc72ce4
DE
955 /* NOTE: This member isn't strictly necessary, read_partial_die could
956 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 957 gdb_byte *sibling;
72bf9492
DJ
958
959 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
960 DW_AT_specification (or DW_AT_abstract_origin or
961 DW_AT_extension). */
b64f50a1 962 sect_offset spec_offset;
72bf9492
DJ
963
964 /* Pointers to this DIE's parent, first child, and next sibling,
965 if any. */
966 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
967 };
968
0963b4bd 969/* This data structure holds the information of an abbrev. */
c906108c
SS
970struct abbrev_info
971 {
972 unsigned int number; /* number identifying abbrev */
973 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
974 unsigned short has_children; /* boolean */
975 unsigned short num_attrs; /* number of attributes */
c906108c
SS
976 struct attr_abbrev *attrs; /* an array of attribute descriptions */
977 struct abbrev_info *next; /* next in chain */
978 };
979
980struct attr_abbrev
981 {
9d25dd43
DE
982 ENUM_BITFIELD(dwarf_attribute) name : 16;
983 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
984 };
985
433df2d4
DE
986/* Size of abbrev_table.abbrev_hash_table. */
987#define ABBREV_HASH_SIZE 121
988
989/* Top level data structure to contain an abbreviation table. */
990
991struct abbrev_table
992{
f4dc4d17
DE
993 /* Where the abbrev table came from.
994 This is used as a sanity check when the table is used. */
433df2d4
DE
995 sect_offset offset;
996
997 /* Storage for the abbrev table. */
998 struct obstack abbrev_obstack;
999
1000 /* Hash table of abbrevs.
1001 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1002 It could be statically allocated, but the previous code didn't so we
1003 don't either. */
1004 struct abbrev_info **abbrevs;
1005};
1006
0963b4bd 1007/* Attributes have a name and a value. */
b60c80d6
DJ
1008struct attribute
1009 {
9d25dd43 1010 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1011 ENUM_BITFIELD(dwarf_form) form : 15;
1012
1013 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1014 field should be in u.str (existing only for DW_STRING) but it is kept
1015 here for better struct attribute alignment. */
1016 unsigned int string_is_canonical : 1;
1017
b60c80d6
DJ
1018 union
1019 {
1020 char *str;
1021 struct dwarf_block *blk;
43bbcdc2
PH
1022 ULONGEST unsnd;
1023 LONGEST snd;
b60c80d6 1024 CORE_ADDR addr;
348e048f 1025 struct signatured_type *signatured_type;
b60c80d6
DJ
1026 }
1027 u;
1028 };
1029
0963b4bd 1030/* This data structure holds a complete die structure. */
c906108c
SS
1031struct die_info
1032 {
76815b17
DE
1033 /* DWARF-2 tag for this DIE. */
1034 ENUM_BITFIELD(dwarf_tag) tag : 16;
1035
1036 /* Number of attributes */
98bfdba5
PA
1037 unsigned char num_attrs;
1038
1039 /* True if we're presently building the full type name for the
1040 type derived from this DIE. */
1041 unsigned char building_fullname : 1;
76815b17
DE
1042
1043 /* Abbrev number */
1044 unsigned int abbrev;
1045
93311388 1046 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1047 sect_offset offset;
78ba4af6
JB
1048
1049 /* The dies in a compilation unit form an n-ary tree. PARENT
1050 points to this die's parent; CHILD points to the first child of
1051 this node; and all the children of a given node are chained
4950bc1c 1052 together via their SIBLING fields. */
639d11d3
DC
1053 struct die_info *child; /* Its first child, if any. */
1054 struct die_info *sibling; /* Its next sibling, if any. */
1055 struct die_info *parent; /* Its parent, if any. */
c906108c 1056
b60c80d6
DJ
1057 /* An array of attributes, with NUM_ATTRS elements. There may be
1058 zero, but it's not common and zero-sized arrays are not
1059 sufficiently portable C. */
1060 struct attribute attrs[1];
c906108c
SS
1061 };
1062
0963b4bd 1063/* Get at parts of an attribute structure. */
c906108c
SS
1064
1065#define DW_STRING(attr) ((attr)->u.str)
8285870a 1066#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1067#define DW_UNSND(attr) ((attr)->u.unsnd)
1068#define DW_BLOCK(attr) ((attr)->u.blk)
1069#define DW_SND(attr) ((attr)->u.snd)
1070#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1071#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1072
0963b4bd 1073/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1074struct dwarf_block
1075 {
56eb65bd 1076 size_t size;
1d6edc3c
JK
1077
1078 /* Valid only if SIZE is not zero. */
fe1b8b76 1079 gdb_byte *data;
c906108c
SS
1080 };
1081
c906108c
SS
1082#ifndef ATTR_ALLOC_CHUNK
1083#define ATTR_ALLOC_CHUNK 4
1084#endif
1085
c906108c
SS
1086/* Allocate fields for structs, unions and enums in this size. */
1087#ifndef DW_FIELD_ALLOC_CHUNK
1088#define DW_FIELD_ALLOC_CHUNK 4
1089#endif
1090
c906108c
SS
1091/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1092 but this would require a corresponding change in unpack_field_as_long
1093 and friends. */
1094static int bits_per_byte = 8;
1095
1096/* The routines that read and process dies for a C struct or C++ class
1097 pass lists of data member fields and lists of member function fields
1098 in an instance of a field_info structure, as defined below. */
1099struct field_info
c5aa993b 1100 {
0963b4bd 1101 /* List of data member and baseclasses fields. */
c5aa993b
JM
1102 struct nextfield
1103 {
1104 struct nextfield *next;
1105 int accessibility;
1106 int virtuality;
1107 struct field field;
1108 }
7d0ccb61 1109 *fields, *baseclasses;
c906108c 1110
7d0ccb61 1111 /* Number of fields (including baseclasses). */
c5aa993b 1112 int nfields;
c906108c 1113
c5aa993b
JM
1114 /* Number of baseclasses. */
1115 int nbaseclasses;
c906108c 1116
c5aa993b
JM
1117 /* Set if the accesibility of one of the fields is not public. */
1118 int non_public_fields;
c906108c 1119
c5aa993b
JM
1120 /* Member function fields array, entries are allocated in the order they
1121 are encountered in the object file. */
1122 struct nextfnfield
1123 {
1124 struct nextfnfield *next;
1125 struct fn_field fnfield;
1126 }
1127 *fnfields;
c906108c 1128
c5aa993b
JM
1129 /* Member function fieldlist array, contains name of possibly overloaded
1130 member function, number of overloaded member functions and a pointer
1131 to the head of the member function field chain. */
1132 struct fnfieldlist
1133 {
1134 char *name;
1135 int length;
1136 struct nextfnfield *head;
1137 }
1138 *fnfieldlists;
c906108c 1139
c5aa993b
JM
1140 /* Number of entries in the fnfieldlists array. */
1141 int nfnfields;
98751a41
JK
1142
1143 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1144 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1145 struct typedef_field_list
1146 {
1147 struct typedef_field field;
1148 struct typedef_field_list *next;
1149 }
1150 *typedef_field_list;
1151 unsigned typedef_field_list_count;
c5aa993b 1152 };
c906108c 1153
10b3939b
DJ
1154/* One item on the queue of compilation units to read in full symbols
1155 for. */
1156struct dwarf2_queue_item
1157{
1158 struct dwarf2_per_cu_data *per_cu;
95554aad 1159 enum language pretend_language;
10b3939b
DJ
1160 struct dwarf2_queue_item *next;
1161};
1162
1163/* The current queue. */
1164static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1165
ae038cb0
DJ
1166/* Loaded secondary compilation units are kept in memory until they
1167 have not been referenced for the processing of this many
1168 compilation units. Set this to zero to disable caching. Cache
1169 sizes of up to at least twenty will improve startup time for
1170 typical inter-CU-reference binaries, at an obvious memory cost. */
1171static int dwarf2_max_cache_age = 5;
920d2a44
AC
1172static void
1173show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1174 struct cmd_list_element *c, const char *value)
1175{
3e43a32a
MS
1176 fprintf_filtered (file, _("The upper bound on the age of cached "
1177 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1178 value);
1179}
1180
ae038cb0 1181
0963b4bd 1182/* Various complaints about symbol reading that don't abort the process. */
c906108c 1183
4d3c2250
KB
1184static void
1185dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1186{
4d3c2250 1187 complaint (&symfile_complaints,
e2e0b3e5 1188 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1189}
1190
25e43795
DJ
1191static void
1192dwarf2_debug_line_missing_file_complaint (void)
1193{
1194 complaint (&symfile_complaints,
1195 _(".debug_line section has line data without a file"));
1196}
1197
59205f5a
JB
1198static void
1199dwarf2_debug_line_missing_end_sequence_complaint (void)
1200{
1201 complaint (&symfile_complaints,
3e43a32a
MS
1202 _(".debug_line section has line "
1203 "program sequence without an end"));
59205f5a
JB
1204}
1205
4d3c2250
KB
1206static void
1207dwarf2_complex_location_expr_complaint (void)
2e276125 1208{
e2e0b3e5 1209 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1210}
1211
4d3c2250
KB
1212static void
1213dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1214 int arg3)
2e276125 1215{
4d3c2250 1216 complaint (&symfile_complaints,
3e43a32a
MS
1217 _("const value length mismatch for '%s', got %d, expected %d"),
1218 arg1, arg2, arg3);
4d3c2250
KB
1219}
1220
1221static void
f664829e 1222dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1223{
4d3c2250 1224 complaint (&symfile_complaints,
f664829e
DE
1225 _("debug info runs off end of %s section"
1226 " [in module %s]"),
1227 section->asection->name,
1228 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1229}
1230
1231static void
1232dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1233{
4d3c2250 1234 complaint (&symfile_complaints,
3e43a32a
MS
1235 _("macro debug info contains a "
1236 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1237 arg1);
1238}
1239
1240static void
1241dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1242{
4d3c2250 1243 complaint (&symfile_complaints,
3e43a32a
MS
1244 _("invalid attribute class or form for '%s' in '%s'"),
1245 arg1, arg2);
4d3c2250 1246}
c906108c 1247
c906108c
SS
1248/* local function prototypes */
1249
4efb68b1 1250static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1251
aaa75496
JB
1252static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1253 struct objfile *);
1254
918dd910
JK
1255static void dwarf2_find_base_address (struct die_info *die,
1256 struct dwarf2_cu *cu);
1257
c67a9c90 1258static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1259
72bf9492
DJ
1260static void scan_partial_symbols (struct partial_die_info *,
1261 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1262 int, struct dwarf2_cu *);
c906108c 1263
72bf9492
DJ
1264static void add_partial_symbol (struct partial_die_info *,
1265 struct dwarf2_cu *);
63d06c5c 1266
72bf9492
DJ
1267static void add_partial_namespace (struct partial_die_info *pdi,
1268 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1269 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1270
5d7cb8df
JK
1271static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1272 CORE_ADDR *highpc, int need_pc,
1273 struct dwarf2_cu *cu);
1274
72bf9492
DJ
1275static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1276 struct dwarf2_cu *cu);
91c24f0a 1277
bc30ff58
JB
1278static void add_partial_subprogram (struct partial_die_info *pdi,
1279 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1280 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1281
a14ed312 1282static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1283
a14ed312 1284static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1285
433df2d4
DE
1286static struct abbrev_info *abbrev_table_lookup_abbrev
1287 (const struct abbrev_table *, unsigned int);
1288
1289static struct abbrev_table *abbrev_table_read_table
1290 (struct dwarf2_section_info *, sect_offset);
1291
1292static void abbrev_table_free (struct abbrev_table *);
1293
f4dc4d17
DE
1294static void abbrev_table_free_cleanup (void *);
1295
dee91e82
DE
1296static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1297 struct dwarf2_section_info *);
c906108c 1298
f3dd6933 1299static void dwarf2_free_abbrev_table (void *);
c906108c 1300
6caca83c
CC
1301static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1302
dee91e82
DE
1303static struct partial_die_info *load_partial_dies
1304 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1305
dee91e82
DE
1306static gdb_byte *read_partial_die (const struct die_reader_specs *,
1307 struct partial_die_info *,
1308 struct abbrev_info *,
1309 unsigned int,
1310 gdb_byte *);
c906108c 1311
36586728 1312static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1313 struct dwarf2_cu *);
72bf9492
DJ
1314
1315static void fixup_partial_die (struct partial_die_info *,
1316 struct dwarf2_cu *);
1317
dee91e82
DE
1318static gdb_byte *read_attribute (const struct die_reader_specs *,
1319 struct attribute *, struct attr_abbrev *,
1320 gdb_byte *);
a8329558 1321
a1855c1d 1322static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1323
a1855c1d 1324static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1325
a1855c1d 1326static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1327
a1855c1d 1328static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1329
a1855c1d 1330static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1331
fe1b8b76 1332static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1333 unsigned int *);
c906108c 1334
c764a876
DE
1335static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1336
1337static LONGEST read_checked_initial_length_and_offset
1338 (bfd *, gdb_byte *, const struct comp_unit_head *,
1339 unsigned int *, unsigned int *);
613e1657 1340
fe1b8b76 1341static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1342 unsigned int *);
1343
1344static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1345
f4dc4d17
DE
1346static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1347 sect_offset);
1348
fe1b8b76 1349static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1350
9b1c24c8 1351static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1352
fe1b8b76
JB
1353static char *read_indirect_string (bfd *, gdb_byte *,
1354 const struct comp_unit_head *,
1355 unsigned int *);
4bdf3d34 1356
36586728
TT
1357static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1358
12df843f 1359static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1360
12df843f 1361static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1362
3019eac3
DE
1363static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1364 unsigned int *);
1365
1366static char *read_str_index (const struct die_reader_specs *reader,
1367 struct dwarf2_cu *cu, ULONGEST str_index);
1368
e142c38c 1369static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1370
e142c38c
DJ
1371static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1372 struct dwarf2_cu *);
c906108c 1373
348e048f 1374static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1375 unsigned int);
348e048f 1376
05cf31d1
JB
1377static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1378 struct dwarf2_cu *cu);
1379
e142c38c 1380static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1381
e142c38c 1382static struct die_info *die_specification (struct die_info *die,
f2f0e013 1383 struct dwarf2_cu **);
63d06c5c 1384
debd256d
JB
1385static void free_line_header (struct line_header *lh);
1386
aaa75496
JB
1387static void add_file_name (struct line_header *, char *, unsigned int,
1388 unsigned int, unsigned int);
1389
3019eac3
DE
1390static struct line_header *dwarf_decode_line_header (unsigned int offset,
1391 struct dwarf2_cu *cu);
debd256d 1392
f3f5162e
DE
1393static void dwarf_decode_lines (struct line_header *, const char *,
1394 struct dwarf2_cu *, struct partial_symtab *,
1395 int);
c906108c 1396
72b9f47f 1397static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1398
f4dc4d17
DE
1399static void dwarf2_start_symtab (struct dwarf2_cu *,
1400 char *, char *, CORE_ADDR);
1401
a14ed312 1402static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1403 struct dwarf2_cu *);
c906108c 1404
34eaf542
TT
1405static struct symbol *new_symbol_full (struct die_info *, struct type *,
1406 struct dwarf2_cu *, struct symbol *);
1407
a14ed312 1408static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1409 struct dwarf2_cu *);
c906108c 1410
98bfdba5
PA
1411static void dwarf2_const_value_attr (struct attribute *attr,
1412 struct type *type,
1413 const char *name,
1414 struct obstack *obstack,
12df843f 1415 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1416 gdb_byte **bytes,
1417 struct dwarf2_locexpr_baton **baton);
2df3850c 1418
e7c27a73 1419static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1420
b4ba55a1
JB
1421static int need_gnat_info (struct dwarf2_cu *);
1422
3e43a32a
MS
1423static struct type *die_descriptive_type (struct die_info *,
1424 struct dwarf2_cu *);
b4ba55a1
JB
1425
1426static void set_descriptive_type (struct type *, struct die_info *,
1427 struct dwarf2_cu *);
1428
e7c27a73
DJ
1429static struct type *die_containing_type (struct die_info *,
1430 struct dwarf2_cu *);
c906108c 1431
673bfd45
DE
1432static struct type *lookup_die_type (struct die_info *, struct attribute *,
1433 struct dwarf2_cu *);
c906108c 1434
f792889a 1435static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1436
673bfd45
DE
1437static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1438
0d5cff50 1439static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1440
6e70227d 1441static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1442 const char *suffix, int physname,
1443 struct dwarf2_cu *cu);
63d06c5c 1444
e7c27a73 1445static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1446
348e048f
DE
1447static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1448
e7c27a73 1449static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1450
e7c27a73 1451static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1452
96408a79
SA
1453static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1454
ff013f42
JK
1455static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1456 struct dwarf2_cu *, struct partial_symtab *);
1457
a14ed312 1458static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1459 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1460 struct partial_symtab *);
c906108c 1461
fae299cd
DC
1462static void get_scope_pc_bounds (struct die_info *,
1463 CORE_ADDR *, CORE_ADDR *,
1464 struct dwarf2_cu *);
1465
801e3a5b
JB
1466static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1467 CORE_ADDR, struct dwarf2_cu *);
1468
a14ed312 1469static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1470 struct dwarf2_cu *);
c906108c 1471
a14ed312 1472static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1473 struct type *, struct dwarf2_cu *);
c906108c 1474
a14ed312 1475static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1476 struct die_info *, struct type *,
e7c27a73 1477 struct dwarf2_cu *);
c906108c 1478
a14ed312 1479static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1480 struct type *,
1481 struct dwarf2_cu *);
c906108c 1482
134d01f1 1483static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1484
e7c27a73 1485static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1486
e7c27a73 1487static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1488
5d7cb8df
JK
1489static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1490
27aa8d6a
SW
1491static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1492
f55ee35c
JK
1493static struct type *read_module_type (struct die_info *die,
1494 struct dwarf2_cu *cu);
1495
38d518c9 1496static const char *namespace_name (struct die_info *die,
e142c38c 1497 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1498
134d01f1 1499static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1500
e7c27a73 1501static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1502
6e70227d 1503static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1504 struct dwarf2_cu *);
1505
dee91e82 1506static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1507 gdb_byte *info_ptr,
fe1b8b76 1508 gdb_byte **new_info_ptr,
639d11d3
DC
1509 struct die_info *parent);
1510
dee91e82 1511static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1512 gdb_byte *info_ptr,
fe1b8b76 1513 gdb_byte **new_info_ptr,
639d11d3
DC
1514 struct die_info *parent);
1515
3019eac3
DE
1516static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1517 struct die_info **, gdb_byte *, int *, int);
1518
dee91e82
DE
1519static gdb_byte *read_full_die (const struct die_reader_specs *,
1520 struct die_info **, gdb_byte *, int *);
93311388 1521
e7c27a73 1522static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1523
71c25dea
TT
1524static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1525 struct obstack *);
1526
e142c38c 1527static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1528
98bfdba5
PA
1529static const char *dwarf2_full_name (char *name,
1530 struct die_info *die,
1531 struct dwarf2_cu *cu);
1532
e142c38c 1533static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1534 struct dwarf2_cu **);
9219021c 1535
f39c6ffd 1536static const char *dwarf_tag_name (unsigned int);
c906108c 1537
f39c6ffd 1538static const char *dwarf_attr_name (unsigned int);
c906108c 1539
f39c6ffd 1540static const char *dwarf_form_name (unsigned int);
c906108c 1541
a14ed312 1542static char *dwarf_bool_name (unsigned int);
c906108c 1543
f39c6ffd 1544static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1545
f9aca02d 1546static struct die_info *sibling_die (struct die_info *);
c906108c 1547
d97bc12b
DE
1548static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1549
1550static void dump_die_for_error (struct die_info *);
1551
1552static void dump_die_1 (struct ui_file *, int level, int max_level,
1553 struct die_info *);
c906108c 1554
d97bc12b 1555/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1556
51545339 1557static void store_in_ref_table (struct die_info *,
10b3939b 1558 struct dwarf2_cu *);
c906108c 1559
93311388
DE
1560static int is_ref_attr (struct attribute *);
1561
b64f50a1 1562static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1563
43bbcdc2 1564static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1565
348e048f
DE
1566static struct die_info *follow_die_ref_or_sig (struct die_info *,
1567 struct attribute *,
1568 struct dwarf2_cu **);
1569
10b3939b
DJ
1570static struct die_info *follow_die_ref (struct die_info *,
1571 struct attribute *,
f2f0e013 1572 struct dwarf2_cu **);
c906108c 1573
348e048f
DE
1574static struct die_info *follow_die_sig (struct die_info *,
1575 struct attribute *,
1576 struct dwarf2_cu **);
1577
6c83ed52
TT
1578static struct signatured_type *lookup_signatured_type_at_offset
1579 (struct objfile *objfile,
b64f50a1 1580 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1581
e5fe5e75 1582static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1583
52dc124a 1584static void read_signatured_type (struct signatured_type *);
348e048f 1585
f4dc4d17 1586static struct type_unit_group *get_type_unit_group
094b34ac 1587 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1588
1589static void build_type_unit_groups (die_reader_func_ftype *, void *);
1590
c906108c
SS
1591/* memory allocation interface */
1592
7b5a2f43 1593static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1594
b60c80d6 1595static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1596
09262596
DE
1597static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1598 char *, int);
2e276125 1599
8e19ed76
PS
1600static int attr_form_is_block (struct attribute *);
1601
3690dd37
JB
1602static int attr_form_is_section_offset (struct attribute *);
1603
1604static int attr_form_is_constant (struct attribute *);
1605
8cf6f0b1
TT
1606static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1607 struct dwarf2_loclist_baton *baton,
1608 struct attribute *attr);
1609
93e7bd98
DJ
1610static void dwarf2_symbol_mark_computed (struct attribute *attr,
1611 struct symbol *sym,
1612 struct dwarf2_cu *cu);
4c2df51b 1613
dee91e82
DE
1614static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1615 gdb_byte *info_ptr,
1616 struct abbrev_info *abbrev);
4bb7a0a7 1617
72bf9492
DJ
1618static void free_stack_comp_unit (void *);
1619
72bf9492
DJ
1620static hashval_t partial_die_hash (const void *item);
1621
1622static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1623
ae038cb0 1624static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1625 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1626
9816fde3 1627static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1628 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1629
1630static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1631 struct die_info *comp_unit_die,
1632 enum language pretend_language);
93311388 1633
68dc6402 1634static void free_heap_comp_unit (void *);
ae038cb0
DJ
1635
1636static void free_cached_comp_units (void *);
1637
1638static void age_cached_comp_units (void);
1639
dee91e82 1640static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1641
f792889a
DJ
1642static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *);
1c379e20 1644
ae038cb0
DJ
1645static void create_all_comp_units (struct objfile *);
1646
0e50663e 1647static int create_all_type_units (struct objfile *);
1fd400ff 1648
95554aad
TT
1649static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1650 enum language);
10b3939b 1651
95554aad
TT
1652static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1653 enum language);
10b3939b 1654
f4dc4d17
DE
1655static void process_full_type_unit (struct dwarf2_per_cu_data *,
1656 enum language);
1657
10b3939b
DJ
1658static void dwarf2_add_dependence (struct dwarf2_cu *,
1659 struct dwarf2_per_cu_data *);
1660
ae038cb0
DJ
1661static void dwarf2_mark (struct dwarf2_cu *);
1662
1663static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1664
b64f50a1 1665static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1666 struct dwarf2_per_cu_data *per_cu);
1667
f792889a 1668static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1669
9291a0cd
TT
1670static void dwarf2_release_queue (void *dummy);
1671
95554aad
TT
1672static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1673 enum language pretend_language);
1674
1675static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1676 struct dwarf2_per_cu_data *per_cu,
1677 enum language pretend_language);
9291a0cd 1678
a0f42c21 1679static void process_queue (void);
9291a0cd
TT
1680
1681static void find_file_and_directory (struct die_info *die,
1682 struct dwarf2_cu *cu,
1683 char **name, char **comp_dir);
1684
1685static char *file_full_name (int file, struct line_header *lh,
1686 const char *comp_dir);
1687
36586728
TT
1688static gdb_byte *read_and_check_comp_unit_head
1689 (struct comp_unit_head *header,
1690 struct dwarf2_section_info *section,
1691 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1692 int is_debug_types_section);
1693
fd820528 1694static void init_cutu_and_read_dies
f4dc4d17
DE
1695 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1696 int use_existing_cu, int keep,
3019eac3
DE
1697 die_reader_func_ftype *die_reader_func, void *data);
1698
dee91e82
DE
1699static void init_cutu_and_read_dies_simple
1700 (struct dwarf2_per_cu_data *this_cu,
1701 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1702
673bfd45 1703static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1704
3019eac3
DE
1705static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1706
1707static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1708 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1709
1710static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1711 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1712
1713static void free_dwo_file_cleanup (void *);
1714
95554aad
TT
1715static void process_cu_includes (void);
1716
1b80a9fa
JK
1717static void check_producer (struct dwarf2_cu *cu);
1718
9291a0cd
TT
1719#if WORDS_BIGENDIAN
1720
1721/* Convert VALUE between big- and little-endian. */
1722static offset_type
1723byte_swap (offset_type value)
1724{
1725 offset_type result;
1726
1727 result = (value & 0xff) << 24;
1728 result |= (value & 0xff00) << 8;
1729 result |= (value & 0xff0000) >> 8;
1730 result |= (value & 0xff000000) >> 24;
1731 return result;
1732}
1733
1734#define MAYBE_SWAP(V) byte_swap (V)
1735
1736#else
1737#define MAYBE_SWAP(V) (V)
1738#endif /* WORDS_BIGENDIAN */
1739
1740/* The suffix for an index file. */
1741#define INDEX_SUFFIX ".gdb-index"
1742
3da10d80
KS
1743static const char *dwarf2_physname (char *name, struct die_info *die,
1744 struct dwarf2_cu *cu);
1745
c906108c 1746/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1747 information and return true if we have enough to do something.
1748 NAMES points to the dwarf2 section names, or is NULL if the standard
1749 ELF names are used. */
c906108c
SS
1750
1751int
251d32d9
TG
1752dwarf2_has_info (struct objfile *objfile,
1753 const struct dwarf2_debug_sections *names)
c906108c 1754{
be391dca
TT
1755 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1756 if (!dwarf2_per_objfile)
1757 {
1758 /* Initialize per-objfile state. */
1759 struct dwarf2_per_objfile *data
1760 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1761
be391dca
TT
1762 memset (data, 0, sizeof (*data));
1763 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1764 dwarf2_per_objfile = data;
6502dd73 1765
251d32d9
TG
1766 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1767 (void *) names);
be391dca
TT
1768 dwarf2_per_objfile->objfile = objfile;
1769 }
1770 return (dwarf2_per_objfile->info.asection != NULL
1771 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1772}
1773
251d32d9
TG
1774/* When loading sections, we look either for uncompressed section or for
1775 compressed section names. */
233a11ab
CS
1776
1777static int
251d32d9
TG
1778section_is_p (const char *section_name,
1779 const struct dwarf2_section_names *names)
233a11ab 1780{
251d32d9
TG
1781 if (names->normal != NULL
1782 && strcmp (section_name, names->normal) == 0)
1783 return 1;
1784 if (names->compressed != NULL
1785 && strcmp (section_name, names->compressed) == 0)
1786 return 1;
1787 return 0;
233a11ab
CS
1788}
1789
c906108c
SS
1790/* This function is mapped across the sections and remembers the
1791 offset and size of each of the debugging sections we are interested
1792 in. */
1793
1794static void
251d32d9 1795dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1796{
251d32d9 1797 const struct dwarf2_debug_sections *names;
dc7650b8 1798 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1799
1800 if (vnames == NULL)
1801 names = &dwarf2_elf_names;
1802 else
1803 names = (const struct dwarf2_debug_sections *) vnames;
1804
dc7650b8
JK
1805 if ((aflag & SEC_HAS_CONTENTS) == 0)
1806 {
1807 }
1808 else if (section_is_p (sectp->name, &names->info))
c906108c 1809 {
dce234bc
PP
1810 dwarf2_per_objfile->info.asection = sectp;
1811 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1812 }
251d32d9 1813 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1814 {
dce234bc
PP
1815 dwarf2_per_objfile->abbrev.asection = sectp;
1816 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1817 }
251d32d9 1818 else if (section_is_p (sectp->name, &names->line))
c906108c 1819 {
dce234bc
PP
1820 dwarf2_per_objfile->line.asection = sectp;
1821 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1822 }
251d32d9 1823 else if (section_is_p (sectp->name, &names->loc))
c906108c 1824 {
dce234bc
PP
1825 dwarf2_per_objfile->loc.asection = sectp;
1826 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1827 }
251d32d9 1828 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1829 {
dce234bc
PP
1830 dwarf2_per_objfile->macinfo.asection = sectp;
1831 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1832 }
cf2c3c16
TT
1833 else if (section_is_p (sectp->name, &names->macro))
1834 {
1835 dwarf2_per_objfile->macro.asection = sectp;
1836 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1837 }
251d32d9 1838 else if (section_is_p (sectp->name, &names->str))
c906108c 1839 {
dce234bc
PP
1840 dwarf2_per_objfile->str.asection = sectp;
1841 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1842 }
3019eac3
DE
1843 else if (section_is_p (sectp->name, &names->addr))
1844 {
1845 dwarf2_per_objfile->addr.asection = sectp;
1846 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1847 }
251d32d9 1848 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1849 {
dce234bc
PP
1850 dwarf2_per_objfile->frame.asection = sectp;
1851 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1852 }
251d32d9 1853 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1854 {
dc7650b8
JK
1855 dwarf2_per_objfile->eh_frame.asection = sectp;
1856 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1857 }
251d32d9 1858 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1859 {
dce234bc
PP
1860 dwarf2_per_objfile->ranges.asection = sectp;
1861 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1862 }
251d32d9 1863 else if (section_is_p (sectp->name, &names->types))
348e048f 1864 {
8b70b953
TT
1865 struct dwarf2_section_info type_section;
1866
1867 memset (&type_section, 0, sizeof (type_section));
1868 type_section.asection = sectp;
1869 type_section.size = bfd_get_section_size (sectp);
1870
1871 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1872 &type_section);
348e048f 1873 }
251d32d9 1874 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1875 {
1876 dwarf2_per_objfile->gdb_index.asection = sectp;
1877 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1878 }
dce234bc 1879
72dca2f5
FR
1880 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1881 && bfd_section_vma (abfd, sectp) == 0)
1882 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1883}
1884
fceca515
DE
1885/* A helper function that decides whether a section is empty,
1886 or not present. */
9e0ac564
TT
1887
1888static int
1889dwarf2_section_empty_p (struct dwarf2_section_info *info)
1890{
1891 return info->asection == NULL || info->size == 0;
1892}
1893
3019eac3
DE
1894/* Read the contents of the section INFO.
1895 OBJFILE is the main object file, but not necessarily the file where
1896 the section comes from. E.g., for DWO files INFO->asection->owner
1897 is the bfd of the DWO file.
dce234bc 1898 If the section is compressed, uncompress it before returning. */
c906108c 1899
dce234bc
PP
1900static void
1901dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1902{
dce234bc 1903 asection *sectp = info->asection;
3019eac3 1904 bfd *abfd;
dce234bc
PP
1905 gdb_byte *buf, *retbuf;
1906 unsigned char header[4];
c906108c 1907
be391dca
TT
1908 if (info->readin)
1909 return;
dce234bc 1910 info->buffer = NULL;
be391dca 1911 info->readin = 1;
188dd5d6 1912
9e0ac564 1913 if (dwarf2_section_empty_p (info))
dce234bc 1914 return;
c906108c 1915
3019eac3
DE
1916 abfd = sectp->owner;
1917
4bf44c1c
TT
1918 /* If the section has relocations, we must read it ourselves.
1919 Otherwise we attach it to the BFD. */
1920 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1921 {
4bf44c1c 1922 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1923
4bf44c1c
TT
1924 /* We have to cast away const here for historical reasons.
1925 Fixing dwarf2read to be const-correct would be quite nice. */
1926 info->buffer = (gdb_byte *) bytes;
1927 return;
dce234bc 1928 }
dce234bc 1929
4bf44c1c
TT
1930 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1931 info->buffer = buf;
dce234bc
PP
1932
1933 /* When debugging .o files, we may need to apply relocations; see
1934 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1935 We never compress sections in .o files, so we only need to
1936 try this when the section is not compressed. */
ac8035ab 1937 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1938 if (retbuf != NULL)
1939 {
1940 info->buffer = retbuf;
1941 return;
1942 }
1943
1944 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1945 || bfd_bread (buf, info->size, abfd) != info->size)
1946 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1947 bfd_get_filename (abfd));
1948}
1949
9e0ac564
TT
1950/* A helper function that returns the size of a section in a safe way.
1951 If you are positive that the section has been read before using the
1952 size, then it is safe to refer to the dwarf2_section_info object's
1953 "size" field directly. In other cases, you must call this
1954 function, because for compressed sections the size field is not set
1955 correctly until the section has been read. */
1956
1957static bfd_size_type
1958dwarf2_section_size (struct objfile *objfile,
1959 struct dwarf2_section_info *info)
1960{
1961 if (!info->readin)
1962 dwarf2_read_section (objfile, info);
1963 return info->size;
1964}
1965
dce234bc 1966/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1967 SECTION_NAME. */
af34e669 1968
dce234bc 1969void
3017a003
TG
1970dwarf2_get_section_info (struct objfile *objfile,
1971 enum dwarf2_section_enum sect,
dce234bc
PP
1972 asection **sectp, gdb_byte **bufp,
1973 bfd_size_type *sizep)
1974{
1975 struct dwarf2_per_objfile *data
1976 = objfile_data (objfile, dwarf2_objfile_data_key);
1977 struct dwarf2_section_info *info;
a3b2a86b
TT
1978
1979 /* We may see an objfile without any DWARF, in which case we just
1980 return nothing. */
1981 if (data == NULL)
1982 {
1983 *sectp = NULL;
1984 *bufp = NULL;
1985 *sizep = 0;
1986 return;
1987 }
3017a003
TG
1988 switch (sect)
1989 {
1990 case DWARF2_DEBUG_FRAME:
1991 info = &data->frame;
1992 break;
1993 case DWARF2_EH_FRAME:
1994 info = &data->eh_frame;
1995 break;
1996 default:
1997 gdb_assert_not_reached ("unexpected section");
1998 }
dce234bc 1999
9e0ac564 2000 dwarf2_read_section (objfile, info);
dce234bc
PP
2001
2002 *sectp = info->asection;
2003 *bufp = info->buffer;
2004 *sizep = info->size;
2005}
2006
36586728
TT
2007/* A helper function to find the sections for a .dwz file. */
2008
2009static void
2010locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2011{
2012 struct dwz_file *dwz_file = arg;
2013
2014 /* Note that we only support the standard ELF names, because .dwz
2015 is ELF-only (at the time of writing). */
2016 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2017 {
2018 dwz_file->abbrev.asection = sectp;
2019 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2020 }
2021 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2022 {
2023 dwz_file->info.asection = sectp;
2024 dwz_file->info.size = bfd_get_section_size (sectp);
2025 }
2026 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2027 {
2028 dwz_file->str.asection = sectp;
2029 dwz_file->str.size = bfd_get_section_size (sectp);
2030 }
2031 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2032 {
2033 dwz_file->line.asection = sectp;
2034 dwz_file->line.size = bfd_get_section_size (sectp);
2035 }
2036 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2037 {
2038 dwz_file->macro.asection = sectp;
2039 dwz_file->macro.size = bfd_get_section_size (sectp);
2040 }
2ec9a5e0
TT
2041 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2042 {
2043 dwz_file->gdb_index.asection = sectp;
2044 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2045 }
36586728
TT
2046}
2047
2048/* Open the separate '.dwz' debug file, if needed. Error if the file
2049 cannot be found. */
2050
2051static struct dwz_file *
2052dwarf2_get_dwz_file (void)
2053{
2054 bfd *abfd, *dwz_bfd;
2055 asection *section;
2056 gdb_byte *data;
2057 struct cleanup *cleanup;
2058 const char *filename;
2059 struct dwz_file *result;
2060
2061 if (dwarf2_per_objfile->dwz_file != NULL)
2062 return dwarf2_per_objfile->dwz_file;
2063
2064 abfd = dwarf2_per_objfile->objfile->obfd;
2065 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2066 if (section == NULL)
2067 error (_("could not find '.gnu_debugaltlink' section"));
2068 if (!bfd_malloc_and_get_section (abfd, section, &data))
2069 error (_("could not read '.gnu_debugaltlink' section: %s"),
2070 bfd_errmsg (bfd_get_error ()));
2071 cleanup = make_cleanup (xfree, data);
2072
2073 filename = data;
2074 if (!IS_ABSOLUTE_PATH (filename))
2075 {
2076 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2077 char *rel;
2078
2079 make_cleanup (xfree, abs);
2080 abs = ldirname (abs);
2081 make_cleanup (xfree, abs);
2082
2083 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2084 make_cleanup (xfree, rel);
2085 filename = rel;
2086 }
2087
2088 /* The format is just a NUL-terminated file name, followed by the
2089 build-id. For now, though, we ignore the build-id. */
2090 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2091 if (dwz_bfd == NULL)
2092 error (_("could not read '%s': %s"), filename,
2093 bfd_errmsg (bfd_get_error ()));
2094
2095 if (!bfd_check_format (dwz_bfd, bfd_object))
2096 {
2097 gdb_bfd_unref (dwz_bfd);
2098 error (_("file '%s' was not usable: %s"), filename,
2099 bfd_errmsg (bfd_get_error ()));
2100 }
2101
2102 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2103 struct dwz_file);
2104 result->dwz_bfd = dwz_bfd;
2105
2106 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2107
2108 do_cleanups (cleanup);
2109
2110 return result;
2111}
9291a0cd 2112\f
7b9f3c50
DE
2113/* DWARF quick_symbols_functions support. */
2114
2115/* TUs can share .debug_line entries, and there can be a lot more TUs than
2116 unique line tables, so we maintain a separate table of all .debug_line
2117 derived entries to support the sharing.
2118 All the quick functions need is the list of file names. We discard the
2119 line_header when we're done and don't need to record it here. */
2120struct quick_file_names
2121{
094b34ac
DE
2122 /* The data used to construct the hash key. */
2123 struct stmt_list_hash hash;
7b9f3c50
DE
2124
2125 /* The number of entries in file_names, real_names. */
2126 unsigned int num_file_names;
2127
2128 /* The file names from the line table, after being run through
2129 file_full_name. */
2130 const char **file_names;
2131
2132 /* The file names from the line table after being run through
2133 gdb_realpath. These are computed lazily. */
2134 const char **real_names;
2135};
2136
2137/* When using the index (and thus not using psymtabs), each CU has an
2138 object of this type. This is used to hold information needed by
2139 the various "quick" methods. */
2140struct dwarf2_per_cu_quick_data
2141{
2142 /* The file table. This can be NULL if there was no file table
2143 or it's currently not read in.
2144 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2145 struct quick_file_names *file_names;
2146
2147 /* The corresponding symbol table. This is NULL if symbols for this
2148 CU have not yet been read. */
2149 struct symtab *symtab;
2150
2151 /* A temporary mark bit used when iterating over all CUs in
2152 expand_symtabs_matching. */
2153 unsigned int mark : 1;
2154
2155 /* True if we've tried to read the file table and found there isn't one.
2156 There will be no point in trying to read it again next time. */
2157 unsigned int no_file_data : 1;
2158};
2159
094b34ac
DE
2160/* Utility hash function for a stmt_list_hash. */
2161
2162static hashval_t
2163hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2164{
2165 hashval_t v = 0;
2166
2167 if (stmt_list_hash->dwo_unit != NULL)
2168 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2169 v += stmt_list_hash->line_offset.sect_off;
2170 return v;
2171}
2172
2173/* Utility equality function for a stmt_list_hash. */
2174
2175static int
2176eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2177 const struct stmt_list_hash *rhs)
2178{
2179 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2180 return 0;
2181 if (lhs->dwo_unit != NULL
2182 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2183 return 0;
2184
2185 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2186}
2187
7b9f3c50
DE
2188/* Hash function for a quick_file_names. */
2189
2190static hashval_t
2191hash_file_name_entry (const void *e)
2192{
2193 const struct quick_file_names *file_data = e;
2194
094b34ac 2195 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2196}
2197
2198/* Equality function for a quick_file_names. */
2199
2200static int
2201eq_file_name_entry (const void *a, const void *b)
2202{
2203 const struct quick_file_names *ea = a;
2204 const struct quick_file_names *eb = b;
2205
094b34ac 2206 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2207}
2208
2209/* Delete function for a quick_file_names. */
2210
2211static void
2212delete_file_name_entry (void *e)
2213{
2214 struct quick_file_names *file_data = e;
2215 int i;
2216
2217 for (i = 0; i < file_data->num_file_names; ++i)
2218 {
2219 xfree ((void*) file_data->file_names[i]);
2220 if (file_data->real_names)
2221 xfree ((void*) file_data->real_names[i]);
2222 }
2223
2224 /* The space for the struct itself lives on objfile_obstack,
2225 so we don't free it here. */
2226}
2227
2228/* Create a quick_file_names hash table. */
2229
2230static htab_t
2231create_quick_file_names_table (unsigned int nr_initial_entries)
2232{
2233 return htab_create_alloc (nr_initial_entries,
2234 hash_file_name_entry, eq_file_name_entry,
2235 delete_file_name_entry, xcalloc, xfree);
2236}
9291a0cd 2237
918dd910
JK
2238/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2239 have to be created afterwards. You should call age_cached_comp_units after
2240 processing PER_CU->CU. dw2_setup must have been already called. */
2241
2242static void
2243load_cu (struct dwarf2_per_cu_data *per_cu)
2244{
3019eac3 2245 if (per_cu->is_debug_types)
e5fe5e75 2246 load_full_type_unit (per_cu);
918dd910 2247 else
95554aad 2248 load_full_comp_unit (per_cu, language_minimal);
918dd910 2249
918dd910 2250 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2251
2252 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2253}
2254
a0f42c21 2255/* Read in the symbols for PER_CU. */
2fdf6df6 2256
9291a0cd 2257static void
a0f42c21 2258dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2259{
2260 struct cleanup *back_to;
2261
f4dc4d17
DE
2262 /* Skip type_unit_groups, reading the type units they contain
2263 is handled elsewhere. */
2264 if (IS_TYPE_UNIT_GROUP (per_cu))
2265 return;
2266
9291a0cd
TT
2267 back_to = make_cleanup (dwarf2_release_queue, NULL);
2268
95554aad
TT
2269 if (dwarf2_per_objfile->using_index
2270 ? per_cu->v.quick->symtab == NULL
2271 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2272 {
2273 queue_comp_unit (per_cu, language_minimal);
2274 load_cu (per_cu);
2275 }
9291a0cd 2276
a0f42c21 2277 process_queue ();
9291a0cd
TT
2278
2279 /* Age the cache, releasing compilation units that have not
2280 been used recently. */
2281 age_cached_comp_units ();
2282
2283 do_cleanups (back_to);
2284}
2285
2286/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2287 the objfile from which this CU came. Returns the resulting symbol
2288 table. */
2fdf6df6 2289
9291a0cd 2290static struct symtab *
a0f42c21 2291dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2292{
95554aad 2293 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2294 if (!per_cu->v.quick->symtab)
2295 {
2296 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2297 increment_reading_symtab ();
a0f42c21 2298 dw2_do_instantiate_symtab (per_cu);
95554aad 2299 process_cu_includes ();
9291a0cd
TT
2300 do_cleanups (back_to);
2301 }
2302 return per_cu->v.quick->symtab;
2303}
2304
f4dc4d17
DE
2305/* Return the CU given its index.
2306
2307 This is intended for loops like:
2308
2309 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2310 + dwarf2_per_objfile->n_type_units); ++i)
2311 {
2312 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2313
2314 ...;
2315 }
2316*/
2fdf6df6 2317
1fd400ff
TT
2318static struct dwarf2_per_cu_data *
2319dw2_get_cu (int index)
2320{
2321 if (index >= dwarf2_per_objfile->n_comp_units)
2322 {
f4dc4d17 2323 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2324 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2325 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2326 }
2327
2328 return dwarf2_per_objfile->all_comp_units[index];
2329}
2330
2331/* Return the primary CU given its index.
2332 The difference between this function and dw2_get_cu is in the handling
2333 of type units (TUs). Here we return the type_unit_group object.
2334
2335 This is intended for loops like:
2336
2337 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2338 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2339 {
2340 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2341
2342 ...;
2343 }
2344*/
2345
2346static struct dwarf2_per_cu_data *
2347dw2_get_primary_cu (int index)
2348{
2349 if (index >= dwarf2_per_objfile->n_comp_units)
2350 {
1fd400ff 2351 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2352 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2353 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2354 }
f4dc4d17 2355
1fd400ff
TT
2356 return dwarf2_per_objfile->all_comp_units[index];
2357}
2358
2ec9a5e0
TT
2359/* A helper for create_cus_from_index that handles a given list of
2360 CUs. */
2fdf6df6 2361
74a0d9f6 2362static void
2ec9a5e0
TT
2363create_cus_from_index_list (struct objfile *objfile,
2364 const gdb_byte *cu_list, offset_type n_elements,
2365 struct dwarf2_section_info *section,
2366 int is_dwz,
2367 int base_offset)
9291a0cd
TT
2368{
2369 offset_type i;
9291a0cd 2370
2ec9a5e0 2371 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2372 {
2373 struct dwarf2_per_cu_data *the_cu;
2374 ULONGEST offset, length;
2375
74a0d9f6
JK
2376 gdb_static_assert (sizeof (ULONGEST) >= 8);
2377 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2378 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2379 cu_list += 2 * 8;
2380
2381 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2382 struct dwarf2_per_cu_data);
b64f50a1 2383 the_cu->offset.sect_off = offset;
9291a0cd
TT
2384 the_cu->length = length;
2385 the_cu->objfile = objfile;
2ec9a5e0 2386 the_cu->info_or_types_section = section;
9291a0cd
TT
2387 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2388 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2389 the_cu->is_dwz = is_dwz;
2390 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2391 }
9291a0cd
TT
2392}
2393
2ec9a5e0 2394/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2395 the CU objects for this objfile. */
2ec9a5e0 2396
74a0d9f6 2397static void
2ec9a5e0
TT
2398create_cus_from_index (struct objfile *objfile,
2399 const gdb_byte *cu_list, offset_type cu_list_elements,
2400 const gdb_byte *dwz_list, offset_type dwz_elements)
2401{
2402 struct dwz_file *dwz;
2403
2404 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2405 dwarf2_per_objfile->all_comp_units
2406 = obstack_alloc (&objfile->objfile_obstack,
2407 dwarf2_per_objfile->n_comp_units
2408 * sizeof (struct dwarf2_per_cu_data *));
2409
74a0d9f6
JK
2410 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2411 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2412
2413 if (dwz_elements == 0)
74a0d9f6 2414 return;
2ec9a5e0
TT
2415
2416 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2417 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2418 cu_list_elements / 2);
2ec9a5e0
TT
2419}
2420
1fd400ff 2421/* Create the signatured type hash table from the index. */
673bfd45 2422
74a0d9f6 2423static void
673bfd45 2424create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2425 struct dwarf2_section_info *section,
673bfd45
DE
2426 const gdb_byte *bytes,
2427 offset_type elements)
1fd400ff
TT
2428{
2429 offset_type i;
673bfd45 2430 htab_t sig_types_hash;
1fd400ff 2431
d467dd73
DE
2432 dwarf2_per_objfile->n_type_units = elements / 3;
2433 dwarf2_per_objfile->all_type_units
1fd400ff 2434 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2435 dwarf2_per_objfile->n_type_units
b4dd5633 2436 * sizeof (struct signatured_type *));
1fd400ff 2437
673bfd45 2438 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2439
2440 for (i = 0; i < elements; i += 3)
2441 {
52dc124a
DE
2442 struct signatured_type *sig_type;
2443 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2444 void **slot;
2445
74a0d9f6
JK
2446 gdb_static_assert (sizeof (ULONGEST) >= 8);
2447 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2448 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2449 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2450 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2451 bytes += 3 * 8;
2452
52dc124a 2453 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2454 struct signatured_type);
52dc124a 2455 sig_type->signature = signature;
3019eac3
DE
2456 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2457 sig_type->per_cu.is_debug_types = 1;
2458 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2459 sig_type->per_cu.offset.sect_off = offset;
2460 sig_type->per_cu.objfile = objfile;
2461 sig_type->per_cu.v.quick
1fd400ff
TT
2462 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2463 struct dwarf2_per_cu_quick_data);
2464
52dc124a
DE
2465 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2466 *slot = sig_type;
1fd400ff 2467
b4dd5633 2468 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2469 }
2470
673bfd45 2471 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2472}
2473
9291a0cd
TT
2474/* Read the address map data from the mapped index, and use it to
2475 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2476
9291a0cd
TT
2477static void
2478create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2479{
2480 const gdb_byte *iter, *end;
2481 struct obstack temp_obstack;
2482 struct addrmap *mutable_map;
2483 struct cleanup *cleanup;
2484 CORE_ADDR baseaddr;
2485
2486 obstack_init (&temp_obstack);
2487 cleanup = make_cleanup_obstack_free (&temp_obstack);
2488 mutable_map = addrmap_create_mutable (&temp_obstack);
2489
2490 iter = index->address_table;
2491 end = iter + index->address_table_size;
2492
2493 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2494
2495 while (iter < end)
2496 {
2497 ULONGEST hi, lo, cu_index;
2498 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2499 iter += 8;
2500 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2501 iter += 8;
2502 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2503 iter += 4;
2504
2505 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2506 dw2_get_cu (cu_index));
9291a0cd
TT
2507 }
2508
2509 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2510 &objfile->objfile_obstack);
2511 do_cleanups (cleanup);
2512}
2513
59d7bcaf
JK
2514/* The hash function for strings in the mapped index. This is the same as
2515 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2516 implementation. This is necessary because the hash function is tied to the
2517 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2518 SYMBOL_HASH_NEXT.
2519
2520 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2521
9291a0cd 2522static hashval_t
559a7a62 2523mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2524{
2525 const unsigned char *str = (const unsigned char *) p;
2526 hashval_t r = 0;
2527 unsigned char c;
2528
2529 while ((c = *str++) != 0)
559a7a62
JK
2530 {
2531 if (index_version >= 5)
2532 c = tolower (c);
2533 r = r * 67 + c - 113;
2534 }
9291a0cd
TT
2535
2536 return r;
2537}
2538
2539/* Find a slot in the mapped index INDEX for the object named NAME.
2540 If NAME is found, set *VEC_OUT to point to the CU vector in the
2541 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2542
9291a0cd
TT
2543static int
2544find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2545 offset_type **vec_out)
2546{
0cf03b49
JK
2547 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2548 offset_type hash;
9291a0cd 2549 offset_type slot, step;
559a7a62 2550 int (*cmp) (const char *, const char *);
9291a0cd 2551
0cf03b49
JK
2552 if (current_language->la_language == language_cplus
2553 || current_language->la_language == language_java
2554 || current_language->la_language == language_fortran)
2555 {
2556 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2557 not contain any. */
2558 const char *paren = strchr (name, '(');
2559
2560 if (paren)
2561 {
2562 char *dup;
2563
2564 dup = xmalloc (paren - name + 1);
2565 memcpy (dup, name, paren - name);
2566 dup[paren - name] = 0;
2567
2568 make_cleanup (xfree, dup);
2569 name = dup;
2570 }
2571 }
2572
559a7a62 2573 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2574 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2575 simulate our NAME being searched is also lowercased. */
2576 hash = mapped_index_string_hash ((index->version == 4
2577 && case_sensitivity == case_sensitive_off
2578 ? 5 : index->version),
2579 name);
2580
3876f04e
DE
2581 slot = hash & (index->symbol_table_slots - 1);
2582 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2583 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2584
2585 for (;;)
2586 {
2587 /* Convert a slot number to an offset into the table. */
2588 offset_type i = 2 * slot;
2589 const char *str;
3876f04e 2590 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2591 {
2592 do_cleanups (back_to);
2593 return 0;
2594 }
9291a0cd 2595
3876f04e 2596 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2597 if (!cmp (name, str))
9291a0cd
TT
2598 {
2599 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2600 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2601 do_cleanups (back_to);
9291a0cd
TT
2602 return 1;
2603 }
2604
3876f04e 2605 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2606 }
2607}
2608
2ec9a5e0
TT
2609/* A helper function that reads the .gdb_index from SECTION and fills
2610 in MAP. FILENAME is the name of the file containing the section;
2611 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2612 ok to use deprecated sections.
2613
2614 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2615 out parameters that are filled in with information about the CU and
2616 TU lists in the section.
2617
2618 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2619
9291a0cd 2620static int
2ec9a5e0
TT
2621read_index_from_section (struct objfile *objfile,
2622 const char *filename,
2623 int deprecated_ok,
2624 struct dwarf2_section_info *section,
2625 struct mapped_index *map,
2626 const gdb_byte **cu_list,
2627 offset_type *cu_list_elements,
2628 const gdb_byte **types_list,
2629 offset_type *types_list_elements)
9291a0cd 2630{
9291a0cd 2631 char *addr;
2ec9a5e0 2632 offset_type version;
b3b272e1 2633 offset_type *metadata;
1fd400ff 2634 int i;
9291a0cd 2635
2ec9a5e0 2636 if (dwarf2_section_empty_p (section))
9291a0cd 2637 return 0;
82430852
JK
2638
2639 /* Older elfutils strip versions could keep the section in the main
2640 executable while splitting it for the separate debug info file. */
2ec9a5e0 2641 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2642 return 0;
2643
2ec9a5e0 2644 dwarf2_read_section (objfile, section);
9291a0cd 2645
2ec9a5e0 2646 addr = section->buffer;
9291a0cd 2647 /* Version check. */
1fd400ff 2648 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2649 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2650 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2651 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2652 indices. */
831adc1f 2653 if (version < 4)
481860b3
GB
2654 {
2655 static int warning_printed = 0;
2656 if (!warning_printed)
2657 {
2658 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2659 filename);
481860b3
GB
2660 warning_printed = 1;
2661 }
2662 return 0;
2663 }
2664 /* Index version 4 uses a different hash function than index version
2665 5 and later.
2666
2667 Versions earlier than 6 did not emit psymbols for inlined
2668 functions. Using these files will cause GDB not to be able to
2669 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2670 indices unless the user has done
2671 "set use-deprecated-index-sections on". */
2ec9a5e0 2672 if (version < 6 && !deprecated_ok)
481860b3
GB
2673 {
2674 static int warning_printed = 0;
2675 if (!warning_printed)
2676 {
e615022a
DE
2677 warning (_("\
2678Skipping deprecated .gdb_index section in %s.\n\
2679Do \"set use-deprecated-index-sections on\" before the file is read\n\
2680to use the section anyway."),
2ec9a5e0 2681 filename);
481860b3
GB
2682 warning_printed = 1;
2683 }
2684 return 0;
2685 }
2686 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2687 longer backward compatible. */
156942c7 2688 if (version > 7)
594e8718 2689 return 0;
9291a0cd 2690
559a7a62 2691 map->version = version;
2ec9a5e0 2692 map->total_size = section->size;
9291a0cd
TT
2693
2694 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2695
2696 i = 0;
2ec9a5e0
TT
2697 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2698 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2699 / 8);
1fd400ff
TT
2700 ++i;
2701
2ec9a5e0
TT
2702 *types_list = addr + MAYBE_SWAP (metadata[i]);
2703 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2704 - MAYBE_SWAP (metadata[i]))
2705 / 8);
987d643c 2706 ++i;
1fd400ff
TT
2707
2708 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2709 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2710 - MAYBE_SWAP (metadata[i]));
2711 ++i;
2712
3876f04e
DE
2713 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2714 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2715 - MAYBE_SWAP (metadata[i]))
2716 / (2 * sizeof (offset_type)));
1fd400ff 2717 ++i;
9291a0cd 2718
1fd400ff
TT
2719 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2720
2ec9a5e0
TT
2721 return 1;
2722}
2723
2724
2725/* Read the index file. If everything went ok, initialize the "quick"
2726 elements of all the CUs and return 1. Otherwise, return 0. */
2727
2728static int
2729dwarf2_read_index (struct objfile *objfile)
2730{
2731 struct mapped_index local_map, *map;
2732 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2733 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2734
2735 if (!read_index_from_section (objfile, objfile->name,
2736 use_deprecated_index_sections,
2737 &dwarf2_per_objfile->gdb_index, &local_map,
2738 &cu_list, &cu_list_elements,
2739 &types_list, &types_list_elements))
2740 return 0;
2741
0fefef59 2742 /* Don't use the index if it's empty. */
2ec9a5e0 2743 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2744 return 0;
2745
2ec9a5e0
TT
2746 /* If there is a .dwz file, read it so we can get its CU list as
2747 well. */
2748 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2749 {
2750 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2751 struct mapped_index dwz_map;
2752 const gdb_byte *dwz_types_ignore;
2753 offset_type dwz_types_elements_ignore;
2754
2755 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2756 1,
2757 &dwz->gdb_index, &dwz_map,
2758 &dwz_list, &dwz_list_elements,
2759 &dwz_types_ignore,
2760 &dwz_types_elements_ignore))
2761 {
2762 warning (_("could not read '.gdb_index' section from %s; skipping"),
2763 bfd_get_filename (dwz->dwz_bfd));
2764 return 0;
2765 }
2766 }
2767
74a0d9f6
JK
2768 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2769 dwz_list_elements);
1fd400ff 2770
8b70b953
TT
2771 if (types_list_elements)
2772 {
2773 struct dwarf2_section_info *section;
2774
2775 /* We can only handle a single .debug_types when we have an
2776 index. */
2777 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2778 return 0;
2779
2780 section = VEC_index (dwarf2_section_info_def,
2781 dwarf2_per_objfile->types, 0);
2782
74a0d9f6
JK
2783 create_signatured_type_table_from_index (objfile, section, types_list,
2784 types_list_elements);
8b70b953 2785 }
9291a0cd 2786
2ec9a5e0
TT
2787 create_addrmap_from_index (objfile, &local_map);
2788
2789 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2790 *map = local_map;
9291a0cd
TT
2791
2792 dwarf2_per_objfile->index_table = map;
2793 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2794 dwarf2_per_objfile->quick_file_names_table =
2795 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2796
2797 return 1;
2798}
2799
2800/* A helper for the "quick" functions which sets the global
2801 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2802
9291a0cd
TT
2803static void
2804dw2_setup (struct objfile *objfile)
2805{
2806 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2807 gdb_assert (dwarf2_per_objfile);
2808}
2809
f4dc4d17
DE
2810/* Reader function for dw2_build_type_unit_groups. */
2811
2812static void
2813dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2814 gdb_byte *info_ptr,
2815 struct die_info *type_unit_die,
2816 int has_children,
2817 void *data)
2818{
2819 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2820 struct attribute *attr;
2821 struct type_unit_group *tu_group;
2822
2823 gdb_assert (data == NULL);
2824
2825 if (! has_children)
2826 return;
2827
2828 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2829 /* Call this for its side-effect of creating the associated
2830 struct type_unit_group if it doesn't already exist. */
094b34ac 2831 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2832}
2833
2834/* Build dwarf2_per_objfile->type_unit_groups.
2835 This function may be called multiple times. */
2836
2837static void
2838dw2_build_type_unit_groups (void)
2839{
2840 if (dwarf2_per_objfile->type_unit_groups == NULL)
2841 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2842}
2843
dee91e82 2844/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2845
dee91e82
DE
2846static void
2847dw2_get_file_names_reader (const struct die_reader_specs *reader,
2848 gdb_byte *info_ptr,
2849 struct die_info *comp_unit_die,
2850 int has_children,
2851 void *data)
9291a0cd 2852{
dee91e82
DE
2853 struct dwarf2_cu *cu = reader->cu;
2854 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2855 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2856 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2857 struct line_header *lh;
9291a0cd 2858 struct attribute *attr;
dee91e82 2859 int i;
9291a0cd 2860 char *name, *comp_dir;
7b9f3c50
DE
2861 void **slot;
2862 struct quick_file_names *qfn;
2863 unsigned int line_offset;
9291a0cd 2864
07261596
TT
2865 /* Our callers never want to match partial units -- instead they
2866 will match the enclosing full CU. */
2867 if (comp_unit_die->tag == DW_TAG_partial_unit)
2868 {
2869 this_cu->v.quick->no_file_data = 1;
2870 return;
2871 }
2872
094b34ac
DE
2873 /* If we're reading the line header for TUs, store it in the "per_cu"
2874 for tu_group. */
2875 if (this_cu->is_debug_types)
2876 {
2877 struct type_unit_group *tu_group = data;
2878
2879 gdb_assert (tu_group != NULL);
2880 lh_cu = &tu_group->per_cu;
2881 }
2882 else
2883 lh_cu = this_cu;
2884
7b9f3c50
DE
2885 lh = NULL;
2886 slot = NULL;
2887 line_offset = 0;
dee91e82
DE
2888
2889 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2890 if (attr)
2891 {
7b9f3c50
DE
2892 struct quick_file_names find_entry;
2893
2894 line_offset = DW_UNSND (attr);
2895
2896 /* We may have already read in this line header (TU line header sharing).
2897 If we have we're done. */
094b34ac
DE
2898 find_entry.hash.dwo_unit = cu->dwo_unit;
2899 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2900 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2901 &find_entry, INSERT);
2902 if (*slot != NULL)
2903 {
094b34ac 2904 lh_cu->v.quick->file_names = *slot;
dee91e82 2905 return;
7b9f3c50
DE
2906 }
2907
3019eac3 2908 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2909 }
2910 if (lh == NULL)
2911 {
094b34ac 2912 lh_cu->v.quick->no_file_data = 1;
dee91e82 2913 return;
9291a0cd
TT
2914 }
2915
7b9f3c50 2916 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2917 qfn->hash.dwo_unit = cu->dwo_unit;
2918 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2919 gdb_assert (slot != NULL);
2920 *slot = qfn;
9291a0cd 2921
dee91e82 2922 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2923
7b9f3c50
DE
2924 qfn->num_file_names = lh->num_file_names;
2925 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2926 lh->num_file_names * sizeof (char *));
9291a0cd 2927 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2928 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2929 qfn->real_names = NULL;
9291a0cd 2930
7b9f3c50 2931 free_line_header (lh);
7b9f3c50 2932
094b34ac 2933 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2934}
2935
2936/* A helper for the "quick" functions which attempts to read the line
2937 table for THIS_CU. */
2938
2939static struct quick_file_names *
2940dw2_get_file_names (struct objfile *objfile,
2941 struct dwarf2_per_cu_data *this_cu)
2942{
f4dc4d17
DE
2943 /* For TUs this should only be called on the parent group. */
2944 if (this_cu->is_debug_types)
2945 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2946
dee91e82
DE
2947 if (this_cu->v.quick->file_names != NULL)
2948 return this_cu->v.quick->file_names;
2949 /* If we know there is no line data, no point in looking again. */
2950 if (this_cu->v.quick->no_file_data)
2951 return NULL;
2952
3019eac3
DE
2953 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2954 in the stub for CUs, there's is no need to lookup the DWO file.
2955 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2956 DWO file. */
2957 if (this_cu->is_debug_types)
094b34ac
DE
2958 {
2959 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2960
2961 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2962 dw2_get_file_names_reader, tu_group);
2963 }
3019eac3
DE
2964 else
2965 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2966
2967 if (this_cu->v.quick->no_file_data)
2968 return NULL;
2969 return this_cu->v.quick->file_names;
9291a0cd
TT
2970}
2971
2972/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2973 real path for a given file name from the line table. */
2fdf6df6 2974
9291a0cd 2975static const char *
7b9f3c50
DE
2976dw2_get_real_path (struct objfile *objfile,
2977 struct quick_file_names *qfn, int index)
9291a0cd 2978{
7b9f3c50
DE
2979 if (qfn->real_names == NULL)
2980 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2981 qfn->num_file_names, sizeof (char *));
9291a0cd 2982
7b9f3c50
DE
2983 if (qfn->real_names[index] == NULL)
2984 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2985
7b9f3c50 2986 return qfn->real_names[index];
9291a0cd
TT
2987}
2988
2989static struct symtab *
2990dw2_find_last_source_symtab (struct objfile *objfile)
2991{
2992 int index;
ae2de4f8 2993
9291a0cd
TT
2994 dw2_setup (objfile);
2995 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2996 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2997}
2998
7b9f3c50
DE
2999/* Traversal function for dw2_forget_cached_source_info. */
3000
3001static int
3002dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3003{
7b9f3c50 3004 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3005
7b9f3c50 3006 if (file_data->real_names)
9291a0cd 3007 {
7b9f3c50 3008 int i;
9291a0cd 3009
7b9f3c50 3010 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3011 {
7b9f3c50
DE
3012 xfree ((void*) file_data->real_names[i]);
3013 file_data->real_names[i] = NULL;
9291a0cd
TT
3014 }
3015 }
7b9f3c50
DE
3016
3017 return 1;
3018}
3019
3020static void
3021dw2_forget_cached_source_info (struct objfile *objfile)
3022{
3023 dw2_setup (objfile);
3024
3025 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3026 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3027}
3028
f8eba3c6
TT
3029/* Helper function for dw2_map_symtabs_matching_filename that expands
3030 the symtabs and calls the iterator. */
3031
3032static int
3033dw2_map_expand_apply (struct objfile *objfile,
3034 struct dwarf2_per_cu_data *per_cu,
3035 const char *name,
3036 const char *full_path, const char *real_path,
3037 int (*callback) (struct symtab *, void *),
3038 void *data)
3039{
3040 struct symtab *last_made = objfile->symtabs;
3041
3042 /* Don't visit already-expanded CUs. */
3043 if (per_cu->v.quick->symtab)
3044 return 0;
3045
3046 /* This may expand more than one symtab, and we want to iterate over
3047 all of them. */
a0f42c21 3048 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3049
3050 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3051 objfile->symtabs, last_made);
3052}
3053
3054/* Implementation of the map_symtabs_matching_filename method. */
3055
9291a0cd 3056static int
f8eba3c6
TT
3057dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3058 const char *full_path, const char *real_path,
3059 int (*callback) (struct symtab *, void *),
3060 void *data)
9291a0cd
TT
3061{
3062 int i;
c011a4f4 3063 const char *name_basename = lbasename (name);
4aac40c8
TT
3064 int name_len = strlen (name);
3065 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3066
3067 dw2_setup (objfile);
ae2de4f8 3068
f4dc4d17
DE
3069 dw2_build_type_unit_groups ();
3070
1fd400ff 3071 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3072 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3073 {
3074 int j;
f4dc4d17 3075 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3076 struct quick_file_names *file_data;
9291a0cd 3077
3d7bb9d9 3078 /* We only need to look at symtabs not already expanded. */
e254ef6a 3079 if (per_cu->v.quick->symtab)
9291a0cd
TT
3080 continue;
3081
7b9f3c50
DE
3082 file_data = dw2_get_file_names (objfile, per_cu);
3083 if (file_data == NULL)
9291a0cd
TT
3084 continue;
3085
7b9f3c50 3086 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3087 {
7b9f3c50 3088 const char *this_name = file_data->file_names[j];
9291a0cd 3089
4aac40c8
TT
3090 if (FILENAME_CMP (name, this_name) == 0
3091 || (!is_abs && compare_filenames_for_search (this_name,
3092 name, name_len)))
9291a0cd 3093 {
f8eba3c6
TT
3094 if (dw2_map_expand_apply (objfile, per_cu,
3095 name, full_path, real_path,
3096 callback, data))
3097 return 1;
4aac40c8 3098 }
9291a0cd 3099
c011a4f4
DE
3100 /* Before we invoke realpath, which can get expensive when many
3101 files are involved, do a quick comparison of the basenames. */
3102 if (! basenames_may_differ
3103 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3104 continue;
3105
9291a0cd
TT
3106 if (full_path != NULL)
3107 {
7b9f3c50
DE
3108 const char *this_real_name = dw2_get_real_path (objfile,
3109 file_data, j);
9291a0cd 3110
7b9f3c50 3111 if (this_real_name != NULL
4aac40c8
TT
3112 && (FILENAME_CMP (full_path, this_real_name) == 0
3113 || (!is_abs
3114 && compare_filenames_for_search (this_real_name,
3115 name, name_len))))
9291a0cd 3116 {
f8eba3c6
TT
3117 if (dw2_map_expand_apply (objfile, per_cu,
3118 name, full_path, real_path,
3119 callback, data))
3120 return 1;
9291a0cd
TT
3121 }
3122 }
3123
3124 if (real_path != NULL)
3125 {
7b9f3c50
DE
3126 const char *this_real_name = dw2_get_real_path (objfile,
3127 file_data, j);
9291a0cd 3128
7b9f3c50 3129 if (this_real_name != NULL
4aac40c8
TT
3130 && (FILENAME_CMP (real_path, this_real_name) == 0
3131 || (!is_abs
3132 && compare_filenames_for_search (this_real_name,
3133 name, name_len))))
9291a0cd 3134 {
f8eba3c6
TT
3135 if (dw2_map_expand_apply (objfile, per_cu,
3136 name, full_path, real_path,
3137 callback, data))
3138 return 1;
9291a0cd
TT
3139 }
3140 }
3141 }
3142 }
3143
9291a0cd
TT
3144 return 0;
3145}
3146
3147static struct symtab *
3148dw2_lookup_symbol (struct objfile *objfile, int block_index,
3149 const char *name, domain_enum domain)
3150{
774b6a14 3151 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
3152 instead. */
3153 return NULL;
3154}
3155
3156/* A helper function that expands all symtabs that hold an object
156942c7
DE
3157 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3158 symbols in block BLOCK_KIND. */
2fdf6df6 3159
9291a0cd 3160static void
156942c7
DE
3161dw2_do_expand_symtabs_matching (struct objfile *objfile,
3162 int want_specific_block,
3163 enum block_enum block_kind,
3164 const char *name, domain_enum domain)
9291a0cd 3165{
156942c7
DE
3166 struct mapped_index *index;
3167
9291a0cd
TT
3168 dw2_setup (objfile);
3169
156942c7
DE
3170 index = dwarf2_per_objfile->index_table;
3171
ae2de4f8 3172 /* index_table is NULL if OBJF_READNOW. */
156942c7 3173 if (index)
9291a0cd
TT
3174 {
3175 offset_type *vec;
3176
156942c7 3177 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
3178 {
3179 offset_type i, len = MAYBE_SWAP (*vec);
3180 for (i = 0; i < len; ++i)
3181 {
156942c7
DE
3182 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3183 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 3184 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
3185 int want_static = block_kind != GLOBAL_BLOCK;
3186 /* This value is only valid for index versions >= 7. */
3187 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3188 gdb_index_symbol_kind symbol_kind =
3189 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
83a788b8
DE
3190 /* Only check the symbol attributes if they're present.
3191 Indices prior to version 7 don't record them,
3192 and indices >= 7 may elide them for certain symbols
3193 (gold does this). */
3194 int attrs_valid =
3195 (index->version >= 7
3196 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3197
3198 if (attrs_valid
3199 && want_specific_block
156942c7
DE
3200 && want_static != is_static)
3201 continue;
3202
83a788b8
DE
3203 /* Only check the symbol's kind if it has one. */
3204 if (attrs_valid)
156942c7
DE
3205 {
3206 switch (domain)
3207 {
3208 case VAR_DOMAIN:
3209 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3210 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3211 /* Some types are also in VAR_DOMAIN. */
3212 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3213 continue;
3214 break;
3215 case STRUCT_DOMAIN:
3216 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3217 continue;
3218 break;
3219 case LABEL_DOMAIN:
3220 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3221 continue;
3222 break;
3223 default:
3224 break;
3225 }
3226 }
1fd400ff 3227
a0f42c21 3228 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3229 }
3230 }
3231 }
3232}
3233
774b6a14
TT
3234static void
3235dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 3236 enum block_enum block_kind, const char *name,
774b6a14 3237 domain_enum domain)
9291a0cd 3238{
156942c7 3239 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
3240}
3241
3242static void
3243dw2_print_stats (struct objfile *objfile)
3244{
3245 int i, count;
3246
3247 dw2_setup (objfile);
3248 count = 0;
1fd400ff 3249 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3250 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3251 {
e254ef6a 3252 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3253
e254ef6a 3254 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3255 ++count;
3256 }
3257 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3258}
3259
3260static void
3261dw2_dump (struct objfile *objfile)
3262{
3263 /* Nothing worth printing. */
3264}
3265
3266static void
3267dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3268 struct section_offsets *delta)
3269{
3270 /* There's nothing to relocate here. */
3271}
3272
3273static void
3274dw2_expand_symtabs_for_function (struct objfile *objfile,
3275 const char *func_name)
3276{
156942c7
DE
3277 /* Note: It doesn't matter what we pass for block_kind here. */
3278 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3279 VAR_DOMAIN);
9291a0cd
TT
3280}
3281
3282static void
3283dw2_expand_all_symtabs (struct objfile *objfile)
3284{
3285 int i;
3286
3287 dw2_setup (objfile);
1fd400ff
TT
3288
3289 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3290 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3291 {
e254ef6a 3292 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3293
a0f42c21 3294 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3295 }
3296}
3297
3298static void
3299dw2_expand_symtabs_with_filename (struct objfile *objfile,
3300 const char *filename)
3301{
3302 int i;
3303
3304 dw2_setup (objfile);
d4637a04
DE
3305
3306 /* We don't need to consider type units here.
3307 This is only called for examining code, e.g. expand_line_sal.
3308 There can be an order of magnitude (or more) more type units
3309 than comp units, and we avoid them if we can. */
3310
3311 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3312 {
3313 int j;
e254ef6a 3314 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3315 struct quick_file_names *file_data;
9291a0cd 3316
3d7bb9d9 3317 /* We only need to look at symtabs not already expanded. */
e254ef6a 3318 if (per_cu->v.quick->symtab)
9291a0cd
TT
3319 continue;
3320
7b9f3c50
DE
3321 file_data = dw2_get_file_names (objfile, per_cu);
3322 if (file_data == NULL)
9291a0cd
TT
3323 continue;
3324
7b9f3c50 3325 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3326 {
7b9f3c50 3327 const char *this_name = file_data->file_names[j];
1ef75ecc 3328 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3329 {
a0f42c21 3330 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3331 break;
3332 }
3333 }
3334 }
3335}
3336
356d9f9d
TT
3337/* A helper function for dw2_find_symbol_file that finds the primary
3338 file name for a given CU. This is a die_reader_func. */
3339
3340static void
3341dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3342 gdb_byte *info_ptr,
3343 struct die_info *comp_unit_die,
3344 int has_children,
3345 void *data)
3346{
3347 const char **result_ptr = data;
3348 struct dwarf2_cu *cu = reader->cu;
3349 struct attribute *attr;
3350
3351 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3352 if (attr == NULL)
3353 *result_ptr = NULL;
3354 else
3355 *result_ptr = DW_STRING (attr);
3356}
3357
dd786858 3358static const char *
9291a0cd
TT
3359dw2_find_symbol_file (struct objfile *objfile, const char *name)
3360{
e254ef6a 3361 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3362 offset_type *vec;
356d9f9d 3363 const char *filename;
9291a0cd
TT
3364
3365 dw2_setup (objfile);
3366
ae2de4f8 3367 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3368 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3369 {
3370 struct symtab *s;
3371
d790cf0a
DE
3372 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3373 {
3374 struct blockvector *bv = BLOCKVECTOR (s);
3375 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3376 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3377
3378 if (sym)
210bbc17 3379 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3380 }
96408a79
SA
3381 return NULL;
3382 }
9291a0cd
TT
3383
3384 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3385 name, &vec))
3386 return NULL;
3387
3388 /* Note that this just looks at the very first one named NAME -- but
3389 actually we are looking for a function. find_main_filename
3390 should be rewritten so that it doesn't require a custom hook. It
3391 could just use the ordinary symbol tables. */
3392 /* vec[0] is the length, which must always be >0. */
156942c7 3393 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3394
356d9f9d
TT
3395 if (per_cu->v.quick->symtab != NULL)
3396 return per_cu->v.quick->symtab->filename;
3397
f4dc4d17
DE
3398 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3399 dw2_get_primary_filename_reader, &filename);
9291a0cd 3400
356d9f9d 3401 return filename;
9291a0cd
TT
3402}
3403
3404static void
40658b94
PH
3405dw2_map_matching_symbols (const char * name, domain_enum namespace,
3406 struct objfile *objfile, int global,
3407 int (*callback) (struct block *,
3408 struct symbol *, void *),
2edb89d3
JK
3409 void *data, symbol_compare_ftype *match,
3410 symbol_compare_ftype *ordered_compare)
9291a0cd 3411{
40658b94 3412 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3413 current language is Ada for a non-Ada objfile using GNU index. As Ada
3414 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3415}
3416
3417static void
f8eba3c6
TT
3418dw2_expand_symtabs_matching
3419 (struct objfile *objfile,
3420 int (*file_matcher) (const char *, void *),
e078317b 3421 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3422 enum search_domain kind,
3423 void *data)
9291a0cd
TT
3424{
3425 int i;
3426 offset_type iter;
4b5246aa 3427 struct mapped_index *index;
9291a0cd
TT
3428
3429 dw2_setup (objfile);
ae2de4f8
DE
3430
3431 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3432 if (!dwarf2_per_objfile->index_table)
3433 return;
4b5246aa 3434 index = dwarf2_per_objfile->index_table;
9291a0cd 3435
7b08b9eb 3436 if (file_matcher != NULL)
24c79950
TT
3437 {
3438 struct cleanup *cleanup;
3439 htab_t visited_found, visited_not_found;
3440
f4dc4d17
DE
3441 dw2_build_type_unit_groups ();
3442
24c79950
TT
3443 visited_found = htab_create_alloc (10,
3444 htab_hash_pointer, htab_eq_pointer,
3445 NULL, xcalloc, xfree);
3446 cleanup = make_cleanup_htab_delete (visited_found);
3447 visited_not_found = htab_create_alloc (10,
3448 htab_hash_pointer, htab_eq_pointer,
3449 NULL, xcalloc, xfree);
3450 make_cleanup_htab_delete (visited_not_found);
3451
3452 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3453 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3454 {
3455 int j;
f4dc4d17 3456 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3457 struct quick_file_names *file_data;
3458 void **slot;
7b08b9eb 3459
24c79950 3460 per_cu->v.quick->mark = 0;
3d7bb9d9 3461
24c79950
TT
3462 /* We only need to look at symtabs not already expanded. */
3463 if (per_cu->v.quick->symtab)
3464 continue;
7b08b9eb 3465
24c79950
TT
3466 file_data = dw2_get_file_names (objfile, per_cu);
3467 if (file_data == NULL)
3468 continue;
7b08b9eb 3469
24c79950
TT
3470 if (htab_find (visited_not_found, file_data) != NULL)
3471 continue;
3472 else if (htab_find (visited_found, file_data) != NULL)
3473 {
3474 per_cu->v.quick->mark = 1;
3475 continue;
3476 }
3477
3478 for (j = 0; j < file_data->num_file_names; ++j)
3479 {
3480 if (file_matcher (file_data->file_names[j], data))
3481 {
3482 per_cu->v.quick->mark = 1;
3483 break;
3484 }
3485 }
3486
3487 slot = htab_find_slot (per_cu->v.quick->mark
3488 ? visited_found
3489 : visited_not_found,
3490 file_data, INSERT);
3491 *slot = file_data;
3492 }
3493
3494 do_cleanups (cleanup);
3495 }
9291a0cd 3496
3876f04e 3497 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3498 {
3499 offset_type idx = 2 * iter;
3500 const char *name;
3501 offset_type *vec, vec_len, vec_idx;
3502
3876f04e 3503 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3504 continue;
3505
3876f04e 3506 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3507
e078317b 3508 if (! (*name_matcher) (name, data))
9291a0cd
TT
3509 continue;
3510
3511 /* The name was matched, now expand corresponding CUs that were
3512 marked. */
4b5246aa 3513 vec = (offset_type *) (index->constant_pool
3876f04e 3514 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3515 vec_len = MAYBE_SWAP (vec[0]);
3516 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3517 {
e254ef6a 3518 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3519 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3520 gdb_index_symbol_kind symbol_kind =
3521 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3522 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3523
3524 /* Don't crash on bad data. */
3525 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3526 + dwarf2_per_objfile->n_type_units))
156942c7 3527 continue;
1fd400ff 3528
156942c7
DE
3529 /* Only check the symbol's kind if it has one.
3530 Indices prior to version 7 don't record it. */
3531 if (index->version >= 7)
3532 {
3533 switch (kind)
3534 {
3535 case VARIABLES_DOMAIN:
3536 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3537 continue;
3538 break;
3539 case FUNCTIONS_DOMAIN:
3540 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3541 continue;
3542 break;
3543 case TYPES_DOMAIN:
3544 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3545 continue;
3546 break;
3547 default:
3548 break;
3549 }
3550 }
3551
3552 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3553 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3554 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3555 }
3556 }
3557}
3558
9703b513
TT
3559/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3560 symtab. */
3561
3562static struct symtab *
3563recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3564{
3565 int i;
3566
3567 if (BLOCKVECTOR (symtab) != NULL
3568 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3569 return symtab;
3570
a3ec0bb1
DE
3571 if (symtab->includes == NULL)
3572 return NULL;
3573
9703b513
TT
3574 for (i = 0; symtab->includes[i]; ++i)
3575 {
a3ec0bb1 3576 struct symtab *s = symtab->includes[i];
9703b513
TT
3577
3578 s = recursively_find_pc_sect_symtab (s, pc);
3579 if (s != NULL)
3580 return s;
3581 }
3582
3583 return NULL;
3584}
3585
9291a0cd
TT
3586static struct symtab *
3587dw2_find_pc_sect_symtab (struct objfile *objfile,
3588 struct minimal_symbol *msymbol,
3589 CORE_ADDR pc,
3590 struct obj_section *section,
3591 int warn_if_readin)
3592{
3593 struct dwarf2_per_cu_data *data;
9703b513 3594 struct symtab *result;
9291a0cd
TT
3595
3596 dw2_setup (objfile);
3597
3598 if (!objfile->psymtabs_addrmap)
3599 return NULL;
3600
3601 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3602 if (!data)
3603 return NULL;
3604
3605 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3606 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3607 paddress (get_objfile_arch (objfile), pc));
3608
9703b513
TT
3609 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3610 gdb_assert (result != NULL);
3611 return result;
9291a0cd
TT
3612}
3613
9291a0cd 3614static void
44b13c5a 3615dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3616 void *data, int need_fullname)
9291a0cd
TT
3617{
3618 int i;
24c79950
TT
3619 struct cleanup *cleanup;
3620 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3621 NULL, xcalloc, xfree);
9291a0cd 3622
24c79950 3623 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3624 dw2_setup (objfile);
ae2de4f8 3625
f4dc4d17
DE
3626 dw2_build_type_unit_groups ();
3627
24c79950
TT
3628 /* We can ignore file names coming from already-expanded CUs. */
3629 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3630 + dwarf2_per_objfile->n_type_units); ++i)
3631 {
3632 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3633
3634 if (per_cu->v.quick->symtab)
3635 {
3636 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3637 INSERT);
3638
3639 *slot = per_cu->v.quick->file_names;
3640 }
3641 }
3642
1fd400ff 3643 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3644 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3645 {
3646 int j;
f4dc4d17 3647 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3648 struct quick_file_names *file_data;
24c79950 3649 void **slot;
9291a0cd 3650
3d7bb9d9 3651 /* We only need to look at symtabs not already expanded. */
e254ef6a 3652 if (per_cu->v.quick->symtab)
9291a0cd
TT
3653 continue;
3654
7b9f3c50
DE
3655 file_data = dw2_get_file_names (objfile, per_cu);
3656 if (file_data == NULL)
9291a0cd
TT
3657 continue;
3658
24c79950
TT
3659 slot = htab_find_slot (visited, file_data, INSERT);
3660 if (*slot)
3661 {
3662 /* Already visited. */
3663 continue;
3664 }
3665 *slot = file_data;
3666
7b9f3c50 3667 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3668 {
74e2f255
DE
3669 const char *this_real_name;
3670
3671 if (need_fullname)
3672 this_real_name = dw2_get_real_path (objfile, file_data, j);
3673 else
3674 this_real_name = NULL;
7b9f3c50 3675 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3676 }
3677 }
24c79950
TT
3678
3679 do_cleanups (cleanup);
9291a0cd
TT
3680}
3681
3682static int
3683dw2_has_symbols (struct objfile *objfile)
3684{
3685 return 1;
3686}
3687
3688const struct quick_symbol_functions dwarf2_gdb_index_functions =
3689{
3690 dw2_has_symbols,
3691 dw2_find_last_source_symtab,
3692 dw2_forget_cached_source_info,
f8eba3c6 3693 dw2_map_symtabs_matching_filename,
9291a0cd 3694 dw2_lookup_symbol,
774b6a14 3695 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3696 dw2_print_stats,
3697 dw2_dump,
3698 dw2_relocate,
3699 dw2_expand_symtabs_for_function,
3700 dw2_expand_all_symtabs,
3701 dw2_expand_symtabs_with_filename,
3702 dw2_find_symbol_file,
40658b94 3703 dw2_map_matching_symbols,
9291a0cd
TT
3704 dw2_expand_symtabs_matching,
3705 dw2_find_pc_sect_symtab,
9291a0cd
TT
3706 dw2_map_symbol_filenames
3707};
3708
3709/* Initialize for reading DWARF for this objfile. Return 0 if this
3710 file will use psymtabs, or 1 if using the GNU index. */
3711
3712int
3713dwarf2_initialize_objfile (struct objfile *objfile)
3714{
3715 /* If we're about to read full symbols, don't bother with the
3716 indices. In this case we also don't care if some other debug
3717 format is making psymtabs, because they are all about to be
3718 expanded anyway. */
3719 if ((objfile->flags & OBJF_READNOW))
3720 {
3721 int i;
3722
3723 dwarf2_per_objfile->using_index = 1;
3724 create_all_comp_units (objfile);
0e50663e 3725 create_all_type_units (objfile);
7b9f3c50
DE
3726 dwarf2_per_objfile->quick_file_names_table =
3727 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3728
1fd400ff 3729 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3730 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3731 {
e254ef6a 3732 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3733
e254ef6a
DE
3734 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3735 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3736 }
3737
3738 /* Return 1 so that gdb sees the "quick" functions. However,
3739 these functions will be no-ops because we will have expanded
3740 all symtabs. */
3741 return 1;
3742 }
3743
3744 if (dwarf2_read_index (objfile))
3745 return 1;
3746
9291a0cd
TT
3747 return 0;
3748}
3749
3750\f
3751
dce234bc
PP
3752/* Build a partial symbol table. */
3753
3754void
f29dff0a 3755dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3756{
f29dff0a 3757 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3758 {
3759 init_psymbol_list (objfile, 1024);
3760 }
3761
d146bf1e 3762 dwarf2_build_psymtabs_hard (objfile);
c906108c 3763}
c906108c 3764
1ce1cefd
DE
3765/* Return the total length of the CU described by HEADER. */
3766
3767static unsigned int
3768get_cu_length (const struct comp_unit_head *header)
3769{
3770 return header->initial_length_size + header->length;
3771}
3772
45452591
DE
3773/* Return TRUE if OFFSET is within CU_HEADER. */
3774
3775static inline int
b64f50a1 3776offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3777{
b64f50a1 3778 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3779 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3780
b64f50a1 3781 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3782}
3783
3b80fe9b
DE
3784/* Find the base address of the compilation unit for range lists and
3785 location lists. It will normally be specified by DW_AT_low_pc.
3786 In DWARF-3 draft 4, the base address could be overridden by
3787 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3788 compilation units with discontinuous ranges. */
3789
3790static void
3791dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3792{
3793 struct attribute *attr;
3794
3795 cu->base_known = 0;
3796 cu->base_address = 0;
3797
3798 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3799 if (attr)
3800 {
3801 cu->base_address = DW_ADDR (attr);
3802 cu->base_known = 1;
3803 }
3804 else
3805 {
3806 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3807 if (attr)
3808 {
3809 cu->base_address = DW_ADDR (attr);
3810 cu->base_known = 1;
3811 }
3812 }
3813}
3814
93311388
DE
3815/* Read in the comp unit header information from the debug_info at info_ptr.
3816 NOTE: This leaves members offset, first_die_offset to be filled in
3817 by the caller. */
107d2387 3818
fe1b8b76 3819static gdb_byte *
107d2387 3820read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3821 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3822{
3823 int signed_addr;
891d2f0b 3824 unsigned int bytes_read;
c764a876
DE
3825
3826 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3827 cu_header->initial_length_size = bytes_read;
3828 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3829 info_ptr += bytes_read;
107d2387
AC
3830 cu_header->version = read_2_bytes (abfd, info_ptr);
3831 info_ptr += 2;
b64f50a1
JK
3832 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3833 &bytes_read);
613e1657 3834 info_ptr += bytes_read;
107d2387
AC
3835 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3836 info_ptr += 1;
3837 signed_addr = bfd_get_sign_extend_vma (abfd);
3838 if (signed_addr < 0)
8e65ff28 3839 internal_error (__FILE__, __LINE__,
e2e0b3e5 3840 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3841 cu_header->signed_addr_p = signed_addr;
c764a876 3842
107d2387
AC
3843 return info_ptr;
3844}
3845
36586728
TT
3846/* Helper function that returns the proper abbrev section for
3847 THIS_CU. */
3848
3849static struct dwarf2_section_info *
3850get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3851{
3852 struct dwarf2_section_info *abbrev;
3853
3854 if (this_cu->is_dwz)
3855 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3856 else
3857 abbrev = &dwarf2_per_objfile->abbrev;
3858
3859 return abbrev;
3860}
3861
9ff913ba
DE
3862/* Subroutine of read_and_check_comp_unit_head and
3863 read_and_check_type_unit_head to simplify them.
3864 Perform various error checking on the header. */
3865
3866static void
3867error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3868 struct dwarf2_section_info *section,
3869 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3870{
3871 bfd *abfd = section->asection->owner;
3872 const char *filename = bfd_get_filename (abfd);
3873
3874 if (header->version != 2 && header->version != 3 && header->version != 4)
3875 error (_("Dwarf Error: wrong version in compilation unit header "
3876 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3877 filename);
3878
b64f50a1 3879 if (header->abbrev_offset.sect_off
36586728 3880 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3881 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3882 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3883 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3884 filename);
3885
3886 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3887 avoid potential 32-bit overflow. */
1ce1cefd 3888 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3889 > section->size)
3890 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3891 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3892 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3893 filename);
3894}
3895
3896/* Read in a CU/TU header and perform some basic error checking.
3897 The contents of the header are stored in HEADER.
3898 The result is a pointer to the start of the first DIE. */
adabb602 3899
fe1b8b76 3900static gdb_byte *
9ff913ba
DE
3901read_and_check_comp_unit_head (struct comp_unit_head *header,
3902 struct dwarf2_section_info *section,
4bdcc0c1 3903 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3904 gdb_byte *info_ptr,
3905 int is_debug_types_section)
72bf9492 3906{
fe1b8b76 3907 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3908 bfd *abfd = section->asection->owner;
72bf9492 3909
b64f50a1 3910 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3911
72bf9492
DJ
3912 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3913
460c1c54
CC
3914 /* If we're reading a type unit, skip over the signature and
3915 type_offset fields. */
b0df02fd 3916 if (is_debug_types_section)
460c1c54
CC
3917 info_ptr += 8 /*signature*/ + header->offset_size;
3918
b64f50a1 3919 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3920
4bdcc0c1 3921 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3922
3923 return info_ptr;
3924}
3925
348e048f
DE
3926/* Read in the types comp unit header information from .debug_types entry at
3927 types_ptr. The result is a pointer to one past the end of the header. */
3928
3929static gdb_byte *
9ff913ba
DE
3930read_and_check_type_unit_head (struct comp_unit_head *header,
3931 struct dwarf2_section_info *section,
4bdcc0c1 3932 struct dwarf2_section_info *abbrev_section,
9ff913ba 3933 gdb_byte *info_ptr,
dee91e82
DE
3934 ULONGEST *signature,
3935 cu_offset *type_offset_in_tu)
348e048f 3936{
9ff913ba
DE
3937 gdb_byte *beg_of_comp_unit = info_ptr;
3938 bfd *abfd = section->asection->owner;
348e048f 3939
b64f50a1 3940 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3941
9ff913ba 3942 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3943
9ff913ba
DE
3944 /* If we're reading a type unit, skip over the signature and
3945 type_offset fields. */
3946 if (signature != NULL)
3947 *signature = read_8_bytes (abfd, info_ptr);
3948 info_ptr += 8;
dee91e82
DE
3949 if (type_offset_in_tu != NULL)
3950 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3951 header->offset_size);
9ff913ba
DE
3952 info_ptr += header->offset_size;
3953
b64f50a1 3954 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3955
4bdcc0c1 3956 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3957
3958 return info_ptr;
348e048f
DE
3959}
3960
f4dc4d17
DE
3961/* Fetch the abbreviation table offset from a comp or type unit header. */
3962
3963static sect_offset
3964read_abbrev_offset (struct dwarf2_section_info *section,
3965 sect_offset offset)
3966{
3967 bfd *abfd = section->asection->owner;
3968 gdb_byte *info_ptr;
3969 unsigned int length, initial_length_size, offset_size;
3970 sect_offset abbrev_offset;
3971
3972 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3973 info_ptr = section->buffer + offset.sect_off;
3974 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3975 offset_size = initial_length_size == 4 ? 4 : 8;
3976 info_ptr += initial_length_size + 2 /*version*/;
3977 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3978 return abbrev_offset;
3979}
3980
aaa75496
JB
3981/* Allocate a new partial symtab for file named NAME and mark this new
3982 partial symtab as being an include of PST. */
3983
3984static void
3985dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3986 struct objfile *objfile)
3987{
3988 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3989
3990 subpst->section_offsets = pst->section_offsets;
3991 subpst->textlow = 0;
3992 subpst->texthigh = 0;
3993
3994 subpst->dependencies = (struct partial_symtab **)
3995 obstack_alloc (&objfile->objfile_obstack,
3996 sizeof (struct partial_symtab *));
3997 subpst->dependencies[0] = pst;
3998 subpst->number_of_dependencies = 1;
3999
4000 subpst->globals_offset = 0;
4001 subpst->n_global_syms = 0;
4002 subpst->statics_offset = 0;
4003 subpst->n_static_syms = 0;
4004 subpst->symtab = NULL;
4005 subpst->read_symtab = pst->read_symtab;
4006 subpst->readin = 0;
4007
4008 /* No private part is necessary for include psymtabs. This property
4009 can be used to differentiate between such include psymtabs and
10b3939b 4010 the regular ones. */
58a9656e 4011 subpst->read_symtab_private = NULL;
aaa75496
JB
4012}
4013
4014/* Read the Line Number Program data and extract the list of files
4015 included by the source file represented by PST. Build an include
d85a05f0 4016 partial symtab for each of these included files. */
aaa75496
JB
4017
4018static void
4019dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4020 struct die_info *die,
4021 struct partial_symtab *pst)
aaa75496 4022{
d85a05f0
DJ
4023 struct line_header *lh = NULL;
4024 struct attribute *attr;
aaa75496 4025
d85a05f0
DJ
4026 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4027 if (attr)
3019eac3 4028 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4029 if (lh == NULL)
4030 return; /* No linetable, so no includes. */
4031
c6da4cef 4032 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4033 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4034
4035 free_line_header (lh);
4036}
4037
348e048f 4038static hashval_t
52dc124a 4039hash_signatured_type (const void *item)
348e048f 4040{
52dc124a 4041 const struct signatured_type *sig_type = item;
9a619af0 4042
348e048f 4043 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4044 return sig_type->signature;
348e048f
DE
4045}
4046
4047static int
52dc124a 4048eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4049{
4050 const struct signatured_type *lhs = item_lhs;
4051 const struct signatured_type *rhs = item_rhs;
9a619af0 4052
348e048f
DE
4053 return lhs->signature == rhs->signature;
4054}
4055
1fd400ff
TT
4056/* Allocate a hash table for signatured types. */
4057
4058static htab_t
673bfd45 4059allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4060{
4061 return htab_create_alloc_ex (41,
52dc124a
DE
4062 hash_signatured_type,
4063 eq_signatured_type,
1fd400ff
TT
4064 NULL,
4065 &objfile->objfile_obstack,
4066 hashtab_obstack_allocate,
4067 dummy_obstack_deallocate);
4068}
4069
d467dd73 4070/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4071
4072static int
d467dd73 4073add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4074{
4075 struct signatured_type *sigt = *slot;
b4dd5633 4076 struct signatured_type ***datap = datum;
1fd400ff 4077
b4dd5633 4078 **datap = sigt;
1fd400ff
TT
4079 ++*datap;
4080
4081 return 1;
4082}
4083
3019eac3 4084/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4085 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4086 NULL otherwise.
4087 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4088 The result is a pointer to the hash table or NULL if there are
4089 no types. */
348e048f 4090
3019eac3
DE
4091static htab_t
4092create_debug_types_hash_table (struct dwo_file *dwo_file,
4093 VEC (dwarf2_section_info_def) *types)
348e048f 4094{
3019eac3 4095 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4096 htab_t types_htab = NULL;
8b70b953
TT
4097 int ix;
4098 struct dwarf2_section_info *section;
4bdcc0c1 4099 struct dwarf2_section_info *abbrev_section;
348e048f 4100
3019eac3
DE
4101 if (VEC_empty (dwarf2_section_info_def, types))
4102 return NULL;
348e048f 4103
4bdcc0c1
DE
4104 abbrev_section = (dwo_file != NULL
4105 ? &dwo_file->sections.abbrev
4106 : &dwarf2_per_objfile->abbrev);
4107
09406207
DE
4108 if (dwarf2_read_debug)
4109 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4110 dwo_file ? ".dwo" : "",
4111 bfd_get_filename (abbrev_section->asection->owner));
4112
8b70b953 4113 for (ix = 0;
3019eac3 4114 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4115 ++ix)
4116 {
3019eac3 4117 bfd *abfd;
8b70b953 4118 gdb_byte *info_ptr, *end_ptr;
36586728 4119 struct dwarf2_section_info *abbrev_section;
348e048f 4120
8b70b953
TT
4121 dwarf2_read_section (objfile, section);
4122 info_ptr = section->buffer;
348e048f 4123
8b70b953
TT
4124 if (info_ptr == NULL)
4125 continue;
348e048f 4126
3019eac3
DE
4127 /* We can't set abfd until now because the section may be empty or
4128 not present, in which case section->asection will be NULL. */
4129 abfd = section->asection->owner;
4130
36586728
TT
4131 if (dwo_file)
4132 abbrev_section = &dwo_file->sections.abbrev;
4133 else
4134 abbrev_section = &dwarf2_per_objfile->abbrev;
4135
8b70b953 4136 if (types_htab == NULL)
3019eac3
DE
4137 {
4138 if (dwo_file)
4139 types_htab = allocate_dwo_unit_table (objfile);
4140 else
4141 types_htab = allocate_signatured_type_table (objfile);
4142 }
348e048f 4143
dee91e82
DE
4144 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4145 because we don't need to read any dies: the signature is in the
4146 header. */
8b70b953
TT
4147
4148 end_ptr = info_ptr + section->size;
4149 while (info_ptr < end_ptr)
4150 {
b64f50a1 4151 sect_offset offset;
3019eac3 4152 cu_offset type_offset_in_tu;
8b70b953 4153 ULONGEST signature;
52dc124a 4154 struct signatured_type *sig_type;
3019eac3 4155 struct dwo_unit *dwo_tu;
8b70b953
TT
4156 void **slot;
4157 gdb_byte *ptr = info_ptr;
9ff913ba 4158 struct comp_unit_head header;
dee91e82 4159 unsigned int length;
348e048f 4160
b64f50a1 4161 offset.sect_off = ptr - section->buffer;
348e048f 4162
8b70b953 4163 /* We need to read the type's signature in order to build the hash
9ff913ba 4164 table, but we don't need anything else just yet. */
348e048f 4165
4bdcc0c1
DE
4166 ptr = read_and_check_type_unit_head (&header, section,
4167 abbrev_section, ptr,
3019eac3 4168 &signature, &type_offset_in_tu);
6caca83c 4169
1ce1cefd 4170 length = get_cu_length (&header);
dee91e82 4171
6caca83c 4172 /* Skip dummy type units. */
dee91e82
DE
4173 if (ptr >= info_ptr + length
4174 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4175 {
1ce1cefd 4176 info_ptr += length;
6caca83c
CC
4177 continue;
4178 }
8b70b953 4179
3019eac3
DE
4180 if (dwo_file)
4181 {
4182 sig_type = NULL;
4183 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4184 struct dwo_unit);
4185 dwo_tu->dwo_file = dwo_file;
4186 dwo_tu->signature = signature;
4187 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4188 dwo_tu->info_or_types_section = section;
4189 dwo_tu->offset = offset;
4190 dwo_tu->length = length;
4191 }
4192 else
4193 {
4194 /* N.B.: type_offset is not usable if this type uses a DWO file.
4195 The real type_offset is in the DWO file. */
4196 dwo_tu = NULL;
4197 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4198 struct signatured_type);
4199 sig_type->signature = signature;
4200 sig_type->type_offset_in_tu = type_offset_in_tu;
4201 sig_type->per_cu.objfile = objfile;
4202 sig_type->per_cu.is_debug_types = 1;
4203 sig_type->per_cu.info_or_types_section = section;
4204 sig_type->per_cu.offset = offset;
4205 sig_type->per_cu.length = length;
4206 }
8b70b953 4207
3019eac3
DE
4208 slot = htab_find_slot (types_htab,
4209 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4210 INSERT);
8b70b953
TT
4211 gdb_assert (slot != NULL);
4212 if (*slot != NULL)
4213 {
3019eac3
DE
4214 sect_offset dup_offset;
4215
4216 if (dwo_file)
4217 {
4218 const struct dwo_unit *dup_tu = *slot;
4219
4220 dup_offset = dup_tu->offset;
4221 }
4222 else
4223 {
4224 const struct signatured_type *dup_tu = *slot;
4225
4226 dup_offset = dup_tu->per_cu.offset;
4227 }
b3c8eb43 4228
8b70b953
TT
4229 complaint (&symfile_complaints,
4230 _("debug type entry at offset 0x%x is duplicate to the "
4231 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4232 offset.sect_off, dup_offset.sect_off,
8b70b953 4233 phex (signature, sizeof (signature)));
8b70b953 4234 }
3019eac3 4235 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4236
09406207 4237 if (dwarf2_read_debug)
8b70b953 4238 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4239 offset.sect_off,
4240 phex (signature, sizeof (signature)));
348e048f 4241
dee91e82 4242 info_ptr += length;
8b70b953 4243 }
348e048f
DE
4244 }
4245
3019eac3
DE
4246 return types_htab;
4247}
4248
4249/* Create the hash table of all entries in the .debug_types section,
4250 and initialize all_type_units.
4251 The result is zero if there is an error (e.g. missing .debug_types section),
4252 otherwise non-zero. */
4253
4254static int
4255create_all_type_units (struct objfile *objfile)
4256{
4257 htab_t types_htab;
b4dd5633 4258 struct signatured_type **iter;
3019eac3
DE
4259
4260 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4261 if (types_htab == NULL)
4262 {
4263 dwarf2_per_objfile->signatured_types = NULL;
4264 return 0;
4265 }
4266
348e048f
DE
4267 dwarf2_per_objfile->signatured_types = types_htab;
4268
d467dd73
DE
4269 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4270 dwarf2_per_objfile->all_type_units
1fd400ff 4271 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4272 dwarf2_per_objfile->n_type_units
b4dd5633 4273 * sizeof (struct signatured_type *));
d467dd73
DE
4274 iter = &dwarf2_per_objfile->all_type_units[0];
4275 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4276 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4277 == dwarf2_per_objfile->n_type_units);
1fd400ff 4278
348e048f
DE
4279 return 1;
4280}
4281
380bca97 4282/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4283 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4284
4285static struct signatured_type *
e319fa28 4286lookup_signatured_type (ULONGEST sig)
348e048f
DE
4287{
4288 struct signatured_type find_entry, *entry;
4289
4290 if (dwarf2_per_objfile->signatured_types == NULL)
4291 {
4292 complaint (&symfile_complaints,
55f1336d 4293 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4294 return NULL;
348e048f
DE
4295 }
4296
4297 find_entry.signature = sig;
4298 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4299 return entry;
4300}
42e7ad6c
DE
4301\f
4302/* Low level DIE reading support. */
348e048f 4303
d85a05f0
DJ
4304/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4305
4306static void
4307init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4308 struct dwarf2_cu *cu,
3019eac3
DE
4309 struct dwarf2_section_info *section,
4310 struct dwo_file *dwo_file)
d85a05f0 4311{
fceca515 4312 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4313 reader->abfd = section->asection->owner;
d85a05f0 4314 reader->cu = cu;
3019eac3 4315 reader->dwo_file = dwo_file;
dee91e82
DE
4316 reader->die_section = section;
4317 reader->buffer = section->buffer;
f664829e 4318 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4319}
4320
fd820528 4321/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4322 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4323
f4dc4d17
DE
4324 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4325 Otherwise the table specified in the comp unit header is read in and used.
4326 This is an optimization for when we already have the abbrev table.
4327
dee91e82
DE
4328 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4329 Otherwise, a new CU is allocated with xmalloc.
4330
4331 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4332 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4333
4334 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4335 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4336
70221824 4337static void
fd820528 4338init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4339 struct abbrev_table *abbrev_table,
fd820528
DE
4340 int use_existing_cu, int keep,
4341 die_reader_func_ftype *die_reader_func,
4342 void *data)
c906108c 4343{
dee91e82 4344 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4345 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4346 bfd *abfd = section->asection->owner;
dee91e82
DE
4347 struct dwarf2_cu *cu;
4348 gdb_byte *begin_info_ptr, *info_ptr;
4349 struct die_reader_specs reader;
d85a05f0 4350 struct die_info *comp_unit_die;
dee91e82 4351 int has_children;
d85a05f0 4352 struct attribute *attr;
dee91e82
DE
4353 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4354 struct signatured_type *sig_type = NULL;
4bdcc0c1 4355 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4356 /* Non-zero if CU currently points to a DWO file and we need to
4357 reread it. When this happens we need to reread the skeleton die
4358 before we can reread the DWO file. */
4359 int rereading_dwo_cu = 0;
c906108c 4360
09406207
DE
4361 if (dwarf2_die_debug)
4362 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4363 this_cu->is_debug_types ? "type" : "comp",
4364 this_cu->offset.sect_off);
4365
dee91e82
DE
4366 if (use_existing_cu)
4367 gdb_assert (keep);
23745b47 4368
dee91e82
DE
4369 cleanups = make_cleanup (null_cleanup, NULL);
4370
4371 /* This is cheap if the section is already read in. */
4372 dwarf2_read_section (objfile, section);
4373
4374 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4375
4376 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4377
4378 if (use_existing_cu && this_cu->cu != NULL)
4379 {
4380 cu = this_cu->cu;
42e7ad6c
DE
4381
4382 /* If this CU is from a DWO file we need to start over, we need to
4383 refetch the attributes from the skeleton CU.
4384 This could be optimized by retrieving those attributes from when we
4385 were here the first time: the previous comp_unit_die was stored in
4386 comp_unit_obstack. But there's no data yet that we need this
4387 optimization. */
4388 if (cu->dwo_unit != NULL)
4389 rereading_dwo_cu = 1;
dee91e82
DE
4390 }
4391 else
4392 {
4393 /* If !use_existing_cu, this_cu->cu must be NULL. */
4394 gdb_assert (this_cu->cu == NULL);
4395
4396 cu = xmalloc (sizeof (*cu));
4397 init_one_comp_unit (cu, this_cu);
4398
4399 /* If an error occurs while loading, release our storage. */
4400 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4401 }
dee91e82 4402
42e7ad6c
DE
4403 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4404 {
4405 /* We already have the header, there's no need to read it in again. */
4406 info_ptr += cu->header.first_die_offset.cu_off;
4407 }
4408 else
4409 {
3019eac3 4410 if (this_cu->is_debug_types)
dee91e82
DE
4411 {
4412 ULONGEST signature;
42e7ad6c 4413 cu_offset type_offset_in_tu;
dee91e82 4414
4bdcc0c1
DE
4415 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4416 abbrev_section, info_ptr,
42e7ad6c
DE
4417 &signature,
4418 &type_offset_in_tu);
dee91e82 4419
42e7ad6c
DE
4420 /* Since per_cu is the first member of struct signatured_type,
4421 we can go from a pointer to one to a pointer to the other. */
4422 sig_type = (struct signatured_type *) this_cu;
4423 gdb_assert (sig_type->signature == signature);
4424 gdb_assert (sig_type->type_offset_in_tu.cu_off
4425 == type_offset_in_tu.cu_off);
dee91e82
DE
4426 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4427
42e7ad6c
DE
4428 /* LENGTH has not been set yet for type units if we're
4429 using .gdb_index. */
1ce1cefd 4430 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4431
4432 /* Establish the type offset that can be used to lookup the type. */
4433 sig_type->type_offset_in_section.sect_off =
4434 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4435 }
4436 else
4437 {
4bdcc0c1
DE
4438 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4439 abbrev_section,
4440 info_ptr, 0);
dee91e82
DE
4441
4442 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4443 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4444 }
4445 }
10b3939b 4446
6caca83c 4447 /* Skip dummy compilation units. */
dee91e82 4448 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4449 || peek_abbrev_code (abfd, info_ptr) == 0)
4450 {
dee91e82 4451 do_cleanups (cleanups);
21b2bd31 4452 return;
6caca83c
CC
4453 }
4454
433df2d4
DE
4455 /* If we don't have them yet, read the abbrevs for this compilation unit.
4456 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4457 done. Note that it's important that if the CU had an abbrev table
4458 on entry we don't free it when we're done: Somewhere up the call stack
4459 it may be in use. */
f4dc4d17
DE
4460 if (abbrev_table != NULL)
4461 {
4462 gdb_assert (cu->abbrev_table == NULL);
4463 gdb_assert (cu->header.abbrev_offset.sect_off
4464 == abbrev_table->offset.sect_off);
4465 cu->abbrev_table = abbrev_table;
4466 }
4467 else if (cu->abbrev_table == NULL)
dee91e82 4468 {
4bdcc0c1 4469 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4470 make_cleanup (dwarf2_free_abbrev_table, cu);
4471 }
42e7ad6c
DE
4472 else if (rereading_dwo_cu)
4473 {
4474 dwarf2_free_abbrev_table (cu);
4475 dwarf2_read_abbrevs (cu, abbrev_section);
4476 }
af703f96 4477
dee91e82 4478 /* Read the top level CU/TU die. */
3019eac3 4479 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4480 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4481
3019eac3
DE
4482 /* If we have a DWO stub, process it and then read in the DWO file.
4483 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4484 a DWO CU, that this test will fail. */
4485 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4486 if (attr)
4487 {
4488 char *dwo_name = DW_STRING (attr);
42e7ad6c 4489 const char *comp_dir_string;
3019eac3
DE
4490 struct dwo_unit *dwo_unit;
4491 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4492 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4493 int i,num_extra_attrs;
4bdcc0c1 4494 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4495
4496 if (has_children)
4497 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4498 " has children (offset 0x%x) [in module %s]"),
4499 this_cu->offset.sect_off, bfd_get_filename (abfd));
4500
4501 /* These attributes aren't processed until later:
4502 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4503 However, the attribute is found in the stub which we won't have later.
4504 In order to not impose this complication on the rest of the code,
4505 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4506
4507 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4508 DWO file. */
42e7ad6c 4509 stmt_list = NULL;
3019eac3
DE
4510 if (! this_cu->is_debug_types)
4511 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4512 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4513 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4514 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4515 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4516
4517 /* There should be a DW_AT_addr_base attribute here (if needed).
4518 We need the value before we can process DW_FORM_GNU_addr_index. */
4519 cu->addr_base = 0;
3019eac3
DE
4520 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4521 if (attr)
2e3cf129
DE
4522 cu->addr_base = DW_UNSND (attr);
4523
4524 /* There should be a DW_AT_ranges_base attribute here (if needed).
4525 We need the value before we can process DW_AT_ranges. */
4526 cu->ranges_base = 0;
4527 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4528 if (attr)
4529 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4530
4531 if (this_cu->is_debug_types)
4532 {
4533 gdb_assert (sig_type != NULL);
4534 signature = sig_type->signature;
4535 }
4536 else
4537 {
4538 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4539 if (! attr)
4540 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4541 dwo_name);
4542 signature = DW_UNSND (attr);
4543 }
4544
4545 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4546 comp_dir_string = NULL;
4547 if (comp_dir)
4548 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4549
4550 if (this_cu->is_debug_types)
42e7ad6c 4551 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4552 else
42e7ad6c 4553 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4554 signature);
4555
4556 if (dwo_unit == NULL)
4557 {
4558 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4559 " with ID %s [in module %s]"),
4560 this_cu->offset.sect_off,
4561 phex (signature, sizeof (signature)),
4562 objfile->name);
4563 }
4564
4565 /* Set up for reading the DWO CU/TU. */
4566 cu->dwo_unit = dwo_unit;
4567 section = dwo_unit->info_or_types_section;
80626a55 4568 dwarf2_read_section (objfile, section);
3019eac3 4569 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4570 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4571 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4572
4573 if (this_cu->is_debug_types)
4574 {
4575 ULONGEST signature;
80626a55 4576 cu_offset type_offset_in_tu;
3019eac3 4577
4bdcc0c1
DE
4578 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4579 dwo_abbrev_section,
4580 info_ptr,
80626a55
DE
4581 &signature,
4582 &type_offset_in_tu);
3019eac3
DE
4583 gdb_assert (sig_type->signature == signature);
4584 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4585 /* For DWOs coming from DWP files, we don't know the CU length
4586 nor the type's offset in the TU until now. */
4587 dwo_unit->length = get_cu_length (&cu->header);
4588 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4589
4590 /* Establish the type offset that can be used to lookup the type.
4591 For DWO files, we don't know it until now. */
4592 sig_type->type_offset_in_section.sect_off =
4593 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4594 }
4595 else
4596 {
4bdcc0c1
DE
4597 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4598 dwo_abbrev_section,
4599 info_ptr, 0);
3019eac3 4600 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4601 /* For DWOs coming from DWP files, we don't know the CU length
4602 until now. */
4603 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4604 }
4605
4606 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4607 if (abbrev_table == NULL)
4608 {
4609 dwarf2_free_abbrev_table (cu);
4610 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4611 }
4612 else
4613 {
4614 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4615 make_cleanup (dwarf2_free_abbrev_table, cu);
4616 }
3019eac3
DE
4617
4618 /* Read in the die, but leave space to copy over the attributes
4619 from the stub. This has the benefit of simplifying the rest of
4620 the code - all the real work is done here. */
4621 num_extra_attrs = ((stmt_list != NULL)
4622 + (low_pc != NULL)
4623 + (high_pc != NULL)
42e7ad6c
DE
4624 + (ranges != NULL)
4625 + (comp_dir != NULL));
3019eac3
DE
4626 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4627 &has_children, num_extra_attrs);
4628
4629 /* Copy over the attributes from the stub to the DWO die. */
4630 i = comp_unit_die->num_attrs;
4631 if (stmt_list != NULL)
4632 comp_unit_die->attrs[i++] = *stmt_list;
4633 if (low_pc != NULL)
4634 comp_unit_die->attrs[i++] = *low_pc;
4635 if (high_pc != NULL)
4636 comp_unit_die->attrs[i++] = *high_pc;
4637 if (ranges != NULL)
4638 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4639 if (comp_dir != NULL)
4640 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4641 comp_unit_die->num_attrs += num_extra_attrs;
4642
4643 /* Skip dummy compilation units. */
4644 if (info_ptr >= begin_info_ptr + dwo_unit->length
4645 || peek_abbrev_code (abfd, info_ptr) == 0)
4646 {
4647 do_cleanups (cleanups);
4648 return;
4649 }
4650 }
4651
dee91e82
DE
4652 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4653
4654 if (free_cu_cleanup != NULL)
348e048f 4655 {
dee91e82
DE
4656 if (keep)
4657 {
4658 /* We've successfully allocated this compilation unit. Let our
4659 caller clean it up when finished with it. */
4660 discard_cleanups (free_cu_cleanup);
4661
4662 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4663 So we have to manually free the abbrev table. */
4664 dwarf2_free_abbrev_table (cu);
4665
4666 /* Link this CU into read_in_chain. */
4667 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4668 dwarf2_per_objfile->read_in_chain = this_cu;
4669 }
4670 else
4671 do_cleanups (free_cu_cleanup);
348e048f 4672 }
dee91e82
DE
4673
4674 do_cleanups (cleanups);
4675}
4676
3019eac3
DE
4677/* Read CU/TU THIS_CU in section SECTION,
4678 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4679 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4680 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4681
4682 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4683 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4684
4685 We fill in THIS_CU->length.
4686
4687 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4688 linker) then DIE_READER_FUNC will not get called.
4689
4690 THIS_CU->cu is always freed when done.
3019eac3
DE
4691 This is done in order to not leave THIS_CU->cu in a state where we have
4692 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4693
4694static void
4695init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4696 struct dwarf2_section_info *abbrev_section,
3019eac3 4697 struct dwo_file *dwo_file,
dee91e82
DE
4698 die_reader_func_ftype *die_reader_func,
4699 void *data)
4700{
4701 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4702 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4703 bfd *abfd = section->asection->owner;
dee91e82
DE
4704 struct dwarf2_cu cu;
4705 gdb_byte *begin_info_ptr, *info_ptr;
4706 struct die_reader_specs reader;
4707 struct cleanup *cleanups;
4708 struct die_info *comp_unit_die;
4709 int has_children;
4710
09406207
DE
4711 if (dwarf2_die_debug)
4712 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4713 this_cu->is_debug_types ? "type" : "comp",
4714 this_cu->offset.sect_off);
4715
dee91e82
DE
4716 gdb_assert (this_cu->cu == NULL);
4717
dee91e82
DE
4718 /* This is cheap if the section is already read in. */
4719 dwarf2_read_section (objfile, section);
4720
4721 init_one_comp_unit (&cu, this_cu);
4722
4723 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4724
4725 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4726 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4727 abbrev_section, info_ptr,
3019eac3 4728 this_cu->is_debug_types);
dee91e82 4729
1ce1cefd 4730 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4731
4732 /* Skip dummy compilation units. */
4733 if (info_ptr >= begin_info_ptr + this_cu->length
4734 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4735 {
dee91e82 4736 do_cleanups (cleanups);
21b2bd31 4737 return;
93311388 4738 }
72bf9492 4739
dee91e82
DE
4740 dwarf2_read_abbrevs (&cu, abbrev_section);
4741 make_cleanup (dwarf2_free_abbrev_table, &cu);
4742
3019eac3 4743 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4744 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4745
4746 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4747
4748 do_cleanups (cleanups);
4749}
4750
3019eac3
DE
4751/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4752 does not lookup the specified DWO file.
4753 This cannot be used to read DWO files.
dee91e82
DE
4754
4755 THIS_CU->cu is always freed when done.
3019eac3
DE
4756 This is done in order to not leave THIS_CU->cu in a state where we have
4757 to care whether it refers to the "main" CU or the DWO CU.
4758 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4759
4760static void
4761init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4762 die_reader_func_ftype *die_reader_func,
4763 void *data)
4764{
4765 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4766 get_abbrev_section_for_cu (this_cu),
3019eac3 4767 NULL,
dee91e82
DE
4768 die_reader_func, data);
4769}
4770
f4dc4d17
DE
4771/* Create a psymtab named NAME and assign it to PER_CU.
4772
4773 The caller must fill in the following details:
4774 dirname, textlow, texthigh. */
4775
4776static struct partial_symtab *
4777create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4778{
4779 struct objfile *objfile = per_cu->objfile;
4780 struct partial_symtab *pst;
4781
4782 pst = start_psymtab_common (objfile, objfile->section_offsets,
4783 name, 0,
4784 objfile->global_psymbols.next,
4785 objfile->static_psymbols.next);
4786
4787 pst->psymtabs_addrmap_supported = 1;
4788
4789 /* This is the glue that links PST into GDB's symbol API. */
4790 pst->read_symtab_private = per_cu;
4791 pst->read_symtab = dwarf2_psymtab_to_symtab;
4792 per_cu->v.psymtab = pst;
4793
4794 return pst;
4795}
4796
dee91e82
DE
4797/* die_reader_func for process_psymtab_comp_unit. */
4798
4799static void
4800process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4801 gdb_byte *info_ptr,
4802 struct die_info *comp_unit_die,
4803 int has_children,
4804 void *data)
4805{
4806 struct dwarf2_cu *cu = reader->cu;
4807 struct objfile *objfile = cu->objfile;
4808 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4809 struct attribute *attr;
4810 CORE_ADDR baseaddr;
4811 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4812 struct partial_symtab *pst;
4813 int has_pc_info;
4814 const char *filename;
95554aad 4815 int *want_partial_unit_ptr = data;
dee91e82 4816
95554aad
TT
4817 if (comp_unit_die->tag == DW_TAG_partial_unit
4818 && (want_partial_unit_ptr == NULL
4819 || !*want_partial_unit_ptr))
dee91e82
DE
4820 return;
4821
f4dc4d17
DE
4822 gdb_assert (! per_cu->is_debug_types);
4823
95554aad 4824 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4825
4826 cu->list_in_scope = &file_symbols;
c906108c 4827
93311388 4828 /* Allocate a new partial symbol table structure. */
dee91e82 4829 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4830 if (attr == NULL || !DW_STRING (attr))
4831 filename = "";
4832 else
4833 filename = DW_STRING (attr);
72bf9492 4834
f4dc4d17
DE
4835 pst = create_partial_symtab (per_cu, filename);
4836
4837 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4838 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4839 if (attr != NULL)
4840 pst->dirname = DW_STRING (attr);
72bf9492 4841
93311388 4842 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4843
dee91e82 4844 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4845
93311388
DE
4846 /* Possibly set the default values of LOWPC and HIGHPC from
4847 `DW_AT_ranges'. */
d85a05f0 4848 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4849 &best_highpc, cu, pst);
d85a05f0 4850 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4851 /* Store the contiguous range if it is not empty; it can be empty for
4852 CUs with no code. */
4853 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4854 best_lowpc + baseaddr,
4855 best_highpc + baseaddr - 1, pst);
93311388
DE
4856
4857 /* Check if comp unit has_children.
4858 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4859 If not, there's no more debug_info for this comp unit. */
d85a05f0 4860 if (has_children)
93311388
DE
4861 {
4862 struct partial_die_info *first_die;
4863 CORE_ADDR lowpc, highpc;
31ffec48 4864
93311388
DE
4865 lowpc = ((CORE_ADDR) -1);
4866 highpc = ((CORE_ADDR) 0);
c906108c 4867
dee91e82 4868 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4869
93311388 4870 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4871 ! has_pc_info, cu);
57c22c6c 4872
93311388
DE
4873 /* If we didn't find a lowpc, set it to highpc to avoid
4874 complaints from `maint check'. */
4875 if (lowpc == ((CORE_ADDR) -1))
4876 lowpc = highpc;
10b3939b 4877
93311388
DE
4878 /* If the compilation unit didn't have an explicit address range,
4879 then use the information extracted from its child dies. */
d85a05f0 4880 if (! has_pc_info)
93311388 4881 {
d85a05f0
DJ
4882 best_lowpc = lowpc;
4883 best_highpc = highpc;
93311388
DE
4884 }
4885 }
d85a05f0
DJ
4886 pst->textlow = best_lowpc + baseaddr;
4887 pst->texthigh = best_highpc + baseaddr;
c906108c 4888
93311388
DE
4889 pst->n_global_syms = objfile->global_psymbols.next -
4890 (objfile->global_psymbols.list + pst->globals_offset);
4891 pst->n_static_syms = objfile->static_psymbols.next -
4892 (objfile->static_psymbols.list + pst->statics_offset);
4893 sort_pst_symbols (pst);
c906108c 4894
f4dc4d17 4895 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4896 {
4897 int i;
f4dc4d17 4898 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4899 struct dwarf2_per_cu_data *iter;
4900
4901 /* Fill in 'dependencies' here; we fill in 'users' in a
4902 post-pass. */
4903 pst->number_of_dependencies = len;
4904 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4905 len * sizeof (struct symtab *));
4906 for (i = 0;
f4dc4d17 4907 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4908 i, iter);
4909 ++i)
4910 pst->dependencies[i] = iter->v.psymtab;
4911
f4dc4d17 4912 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4913 }
4914
f4dc4d17
DE
4915 /* Get the list of files included in the current compilation unit,
4916 and build a psymtab for each of them. */
4917 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4918
4919 if (dwarf2_read_debug)
4920 {
4921 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4922
4923 fprintf_unfiltered (gdb_stdlog,
844226d6 4924 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4925 ", %d global, %d static syms\n",
4926 per_cu->is_debug_types ? "type" : "comp",
4927 per_cu->offset.sect_off,
4928 paddress (gdbarch, pst->textlow),
4929 paddress (gdbarch, pst->texthigh),
4930 pst->n_global_syms, pst->n_static_syms);
4931 }
dee91e82 4932}
ae038cb0 4933
dee91e82
DE
4934/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4935 Process compilation unit THIS_CU for a psymtab. */
4936
4937static void
95554aad
TT
4938process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4939 int want_partial_unit)
dee91e82
DE
4940{
4941 /* If this compilation unit was already read in, free the
4942 cached copy in order to read it in again. This is
4943 necessary because we skipped some symbols when we first
4944 read in the compilation unit (see load_partial_dies).
4945 This problem could be avoided, but the benefit is unclear. */
4946 if (this_cu->cu != NULL)
4947 free_one_cached_comp_unit (this_cu);
4948
3019eac3 4949 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
4950 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4951 process_psymtab_comp_unit_reader,
95554aad 4952 &want_partial_unit);
dee91e82
DE
4953
4954 /* Age out any secondary CUs. */
4955 age_cached_comp_units ();
93311388 4956}
ff013f42 4957
f4dc4d17
DE
4958static hashval_t
4959hash_type_unit_group (const void *item)
4960{
094b34ac 4961 const struct type_unit_group *tu_group = item;
f4dc4d17 4962
094b34ac 4963 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4964}
348e048f
DE
4965
4966static int
f4dc4d17 4967eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4968{
f4dc4d17
DE
4969 const struct type_unit_group *lhs = item_lhs;
4970 const struct type_unit_group *rhs = item_rhs;
348e048f 4971
094b34ac 4972 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4973}
348e048f 4974
f4dc4d17
DE
4975/* Allocate a hash table for type unit groups. */
4976
4977static htab_t
4978allocate_type_unit_groups_table (void)
4979{
4980 return htab_create_alloc_ex (3,
4981 hash_type_unit_group,
4982 eq_type_unit_group,
4983 NULL,
4984 &dwarf2_per_objfile->objfile->objfile_obstack,
4985 hashtab_obstack_allocate,
4986 dummy_obstack_deallocate);
4987}
dee91e82 4988
f4dc4d17
DE
4989/* Type units that don't have DW_AT_stmt_list are grouped into their own
4990 partial symtabs. We combine several TUs per psymtab to not let the size
4991 of any one psymtab grow too big. */
4992#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4993#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 4994
094b34ac 4995/* Helper routine for get_type_unit_group.
f4dc4d17
DE
4996 Create the type_unit_group object used to hold one or more TUs. */
4997
4998static struct type_unit_group *
094b34ac 4999create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5000{
5001 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5002 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5003 struct type_unit_group *tu_group;
f4dc4d17
DE
5004
5005 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5006 struct type_unit_group);
094b34ac 5007 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5008 per_cu->objfile = objfile;
5009 per_cu->is_debug_types = 1;
5010 per_cu->s.type_unit_group = tu_group;
5011
094b34ac
DE
5012 if (dwarf2_per_objfile->using_index)
5013 {
5014 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5015 struct dwarf2_per_cu_quick_data);
5016 tu_group->t.first_tu = cu->per_cu;
5017 }
5018 else
5019 {
5020 unsigned int line_offset = line_offset_struct.sect_off;
5021 struct partial_symtab *pst;
5022 char *name;
5023
5024 /* Give the symtab a useful name for debug purposes. */
5025 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5026 name = xstrprintf ("<type_units_%d>",
5027 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5028 else
5029 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5030
5031 pst = create_partial_symtab (per_cu, name);
5032 pst->anonymous = 1;
f4dc4d17 5033
094b34ac
DE
5034 xfree (name);
5035 }
f4dc4d17 5036
094b34ac
DE
5037 tu_group->hash.dwo_unit = cu->dwo_unit;
5038 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5039
5040 return tu_group;
5041}
5042
094b34ac
DE
5043/* Look up the type_unit_group for type unit CU, and create it if necessary.
5044 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5045
5046static struct type_unit_group *
094b34ac 5047get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5048{
5049 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5050 struct type_unit_group *tu_group;
5051 void **slot;
5052 unsigned int line_offset;
5053 struct type_unit_group type_unit_group_for_lookup;
5054
5055 if (dwarf2_per_objfile->type_unit_groups == NULL)
5056 {
5057 dwarf2_per_objfile->type_unit_groups =
5058 allocate_type_unit_groups_table ();
5059 }
5060
5061 /* Do we need to create a new group, or can we use an existing one? */
5062
5063 if (stmt_list)
5064 {
5065 line_offset = DW_UNSND (stmt_list);
5066 ++tu_stats->nr_symtab_sharers;
5067 }
5068 else
5069 {
5070 /* Ugh, no stmt_list. Rare, but we have to handle it.
5071 We can do various things here like create one group per TU or
5072 spread them over multiple groups to split up the expansion work.
5073 To avoid worst case scenarios (too many groups or too large groups)
5074 we, umm, group them in bunches. */
5075 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5076 | (tu_stats->nr_stmt_less_type_units
5077 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5078 ++tu_stats->nr_stmt_less_type_units;
5079 }
5080
094b34ac
DE
5081 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5082 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5083 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5084 &type_unit_group_for_lookup, INSERT);
5085 if (*slot != NULL)
5086 {
5087 tu_group = *slot;
5088 gdb_assert (tu_group != NULL);
5089 }
5090 else
5091 {
5092 sect_offset line_offset_struct;
5093
5094 line_offset_struct.sect_off = line_offset;
094b34ac 5095 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5096 *slot = tu_group;
5097 ++tu_stats->nr_symtabs;
5098 }
5099
5100 return tu_group;
5101}
5102
5103/* Struct used to sort TUs by their abbreviation table offset. */
5104
5105struct tu_abbrev_offset
5106{
5107 struct signatured_type *sig_type;
5108 sect_offset abbrev_offset;
5109};
5110
5111/* Helper routine for build_type_unit_groups, passed to qsort. */
5112
5113static int
5114sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5115{
5116 const struct tu_abbrev_offset * const *a = ap;
5117 const struct tu_abbrev_offset * const *b = bp;
5118 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5119 unsigned int boff = (*b)->abbrev_offset.sect_off;
5120
5121 return (aoff > boff) - (aoff < boff);
5122}
5123
5124/* A helper function to add a type_unit_group to a table. */
5125
5126static int
5127add_type_unit_group_to_table (void **slot, void *datum)
5128{
5129 struct type_unit_group *tu_group = *slot;
5130 struct type_unit_group ***datap = datum;
5131
5132 **datap = tu_group;
5133 ++*datap;
5134
5135 return 1;
5136}
5137
5138/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5139 each one passing FUNC,DATA.
5140
5141 The efficiency is because we sort TUs by the abbrev table they use and
5142 only read each abbrev table once. In one program there are 200K TUs
5143 sharing 8K abbrev tables.
5144
5145 The main purpose of this function is to support building the
5146 dwarf2_per_objfile->type_unit_groups table.
5147 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5148 can collapse the search space by grouping them by stmt_list.
5149 The savings can be significant, in the same program from above the 200K TUs
5150 share 8K stmt_list tables.
5151
5152 FUNC is expected to call get_type_unit_group, which will create the
5153 struct type_unit_group if necessary and add it to
5154 dwarf2_per_objfile->type_unit_groups. */
5155
5156static void
5157build_type_unit_groups (die_reader_func_ftype *func, void *data)
5158{
5159 struct objfile *objfile = dwarf2_per_objfile->objfile;
5160 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5161 struct cleanup *cleanups;
5162 struct abbrev_table *abbrev_table;
5163 sect_offset abbrev_offset;
5164 struct tu_abbrev_offset *sorted_by_abbrev;
5165 struct type_unit_group **iter;
5166 int i;
5167
5168 /* It's up to the caller to not call us multiple times. */
5169 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5170
5171 if (dwarf2_per_objfile->n_type_units == 0)
5172 return;
5173
5174 /* TUs typically share abbrev tables, and there can be way more TUs than
5175 abbrev tables. Sort by abbrev table to reduce the number of times we
5176 read each abbrev table in.
5177 Alternatives are to punt or to maintain a cache of abbrev tables.
5178 This is simpler and efficient enough for now.
5179
5180 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5181 symtab to use). Typically TUs with the same abbrev offset have the same
5182 stmt_list value too so in practice this should work well.
5183
5184 The basic algorithm here is:
5185
5186 sort TUs by abbrev table
5187 for each TU with same abbrev table:
5188 read abbrev table if first user
5189 read TU top level DIE
5190 [IWBN if DWO skeletons had DW_AT_stmt_list]
5191 call FUNC */
5192
5193 if (dwarf2_read_debug)
5194 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5195
5196 /* Sort in a separate table to maintain the order of all_type_units
5197 for .gdb_index: TU indices directly index all_type_units. */
5198 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5199 dwarf2_per_objfile->n_type_units);
5200 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5201 {
5202 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5203
5204 sorted_by_abbrev[i].sig_type = sig_type;
5205 sorted_by_abbrev[i].abbrev_offset =
5206 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5207 sig_type->per_cu.offset);
5208 }
5209 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5210 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5211 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5212
094b34ac
DE
5213 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5214 called any number of times, so we don't reset tu_stats here. */
5215
f4dc4d17
DE
5216 abbrev_offset.sect_off = ~(unsigned) 0;
5217 abbrev_table = NULL;
5218 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5219
5220 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5221 {
5222 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5223
5224 /* Switch to the next abbrev table if necessary. */
5225 if (abbrev_table == NULL
5226 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5227 {
5228 if (abbrev_table != NULL)
5229 {
5230 abbrev_table_free (abbrev_table);
5231 /* Reset to NULL in case abbrev_table_read_table throws
5232 an error: abbrev_table_free_cleanup will get called. */
5233 abbrev_table = NULL;
5234 }
5235 abbrev_offset = tu->abbrev_offset;
5236 abbrev_table =
5237 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5238 abbrev_offset);
5239 ++tu_stats->nr_uniq_abbrev_tables;
5240 }
5241
5242 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5243 func, data);
5244 }
5245
5246 /* Create a vector of pointers to primary type units to make it easy to
5247 iterate over them and CUs. See dw2_get_primary_cu. */
5248 dwarf2_per_objfile->n_type_unit_groups =
5249 htab_elements (dwarf2_per_objfile->type_unit_groups);
5250 dwarf2_per_objfile->all_type_unit_groups =
5251 obstack_alloc (&objfile->objfile_obstack,
5252 dwarf2_per_objfile->n_type_unit_groups
5253 * sizeof (struct type_unit_group *));
5254 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5255 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5256 add_type_unit_group_to_table, &iter);
5257 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5258 == dwarf2_per_objfile->n_type_unit_groups);
5259
5260 do_cleanups (cleanups);
5261
5262 if (dwarf2_read_debug)
5263 {
5264 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5265 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5266 dwarf2_per_objfile->n_type_units);
5267 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5268 tu_stats->nr_uniq_abbrev_tables);
5269 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5270 tu_stats->nr_symtabs);
5271 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5272 tu_stats->nr_symtab_sharers);
5273 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5274 tu_stats->nr_stmt_less_type_units);
5275 }
5276}
5277
5278/* Reader function for build_type_psymtabs. */
5279
5280static void
5281build_type_psymtabs_reader (const struct die_reader_specs *reader,
5282 gdb_byte *info_ptr,
5283 struct die_info *type_unit_die,
5284 int has_children,
5285 void *data)
5286{
5287 struct objfile *objfile = dwarf2_per_objfile->objfile;
5288 struct dwarf2_cu *cu = reader->cu;
5289 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5290 struct type_unit_group *tu_group;
5291 struct attribute *attr;
5292 struct partial_die_info *first_die;
5293 CORE_ADDR lowpc, highpc;
5294 struct partial_symtab *pst;
5295
5296 gdb_assert (data == NULL);
5297
5298 if (! has_children)
5299 return;
5300
5301 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5302 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5303
094b34ac 5304 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5305
5306 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5307 cu->list_in_scope = &file_symbols;
5308 pst = create_partial_symtab (per_cu, "");
5309 pst->anonymous = 1;
5310
5311 first_die = load_partial_dies (reader, info_ptr, 1);
5312
5313 lowpc = (CORE_ADDR) -1;
5314 highpc = (CORE_ADDR) 0;
5315 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5316
5317 pst->n_global_syms = objfile->global_psymbols.next -
5318 (objfile->global_psymbols.list + pst->globals_offset);
5319 pst->n_static_syms = objfile->static_psymbols.next -
5320 (objfile->static_psymbols.list + pst->statics_offset);
5321 sort_pst_symbols (pst);
5322}
5323
5324/* Traversal function for build_type_psymtabs. */
5325
5326static int
5327build_type_psymtab_dependencies (void **slot, void *info)
5328{
5329 struct objfile *objfile = dwarf2_per_objfile->objfile;
5330 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5331 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5332 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5333 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5334 struct dwarf2_per_cu_data *iter;
5335 int i;
5336
5337 gdb_assert (len > 0);
5338
5339 pst->number_of_dependencies = len;
5340 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5341 len * sizeof (struct psymtab *));
5342 for (i = 0;
094b34ac 5343 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5344 ++i)
5345 {
5346 pst->dependencies[i] = iter->v.psymtab;
5347 iter->s.type_unit_group = tu_group;
5348 }
5349
094b34ac 5350 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5351
5352 return 1;
5353}
5354
5355/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5356 Build partial symbol tables for the .debug_types comp-units. */
5357
5358static void
5359build_type_psymtabs (struct objfile *objfile)
5360{
0e50663e 5361 if (! create_all_type_units (objfile))
348e048f
DE
5362 return;
5363
f4dc4d17
DE
5364 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5365
5366 /* Now that all TUs have been processed we can fill in the dependencies. */
5367 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5368 build_type_psymtab_dependencies, NULL);
348e048f
DE
5369}
5370
60606b2c
TT
5371/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5372
5373static void
5374psymtabs_addrmap_cleanup (void *o)
5375{
5376 struct objfile *objfile = o;
ec61707d 5377
60606b2c
TT
5378 objfile->psymtabs_addrmap = NULL;
5379}
5380
95554aad
TT
5381/* Compute the 'user' field for each psymtab in OBJFILE. */
5382
5383static void
5384set_partial_user (struct objfile *objfile)
5385{
5386 int i;
5387
5388 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5389 {
5390 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5391 struct partial_symtab *pst = per_cu->v.psymtab;
5392 int j;
5393
36586728
TT
5394 if (pst == NULL)
5395 continue;
5396
95554aad
TT
5397 for (j = 0; j < pst->number_of_dependencies; ++j)
5398 {
5399 /* Set the 'user' field only if it is not already set. */
5400 if (pst->dependencies[j]->user == NULL)
5401 pst->dependencies[j]->user = pst;
5402 }
5403 }
5404}
5405
93311388
DE
5406/* Build the partial symbol table by doing a quick pass through the
5407 .debug_info and .debug_abbrev sections. */
72bf9492 5408
93311388 5409static void
c67a9c90 5410dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5411{
60606b2c
TT
5412 struct cleanup *back_to, *addrmap_cleanup;
5413 struct obstack temp_obstack;
21b2bd31 5414 int i;
93311388 5415
45cfd468
DE
5416 if (dwarf2_read_debug)
5417 {
5418 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5419 objfile->name);
5420 }
5421
98bfdba5
PA
5422 dwarf2_per_objfile->reading_partial_symbols = 1;
5423
be391dca 5424 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5425
93311388
DE
5426 /* Any cached compilation units will be linked by the per-objfile
5427 read_in_chain. Make sure to free them when we're done. */
5428 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5429
348e048f
DE
5430 build_type_psymtabs (objfile);
5431
93311388 5432 create_all_comp_units (objfile);
c906108c 5433
60606b2c
TT
5434 /* Create a temporary address map on a temporary obstack. We later
5435 copy this to the final obstack. */
5436 obstack_init (&temp_obstack);
5437 make_cleanup_obstack_free (&temp_obstack);
5438 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5439 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5440
21b2bd31 5441 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5442 {
21b2bd31 5443 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5444
95554aad 5445 process_psymtab_comp_unit (per_cu, 0);
c906108c 5446 }
ff013f42 5447
95554aad
TT
5448 set_partial_user (objfile);
5449
ff013f42
JK
5450 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5451 &objfile->objfile_obstack);
60606b2c 5452 discard_cleanups (addrmap_cleanup);
ff013f42 5453
ae038cb0 5454 do_cleanups (back_to);
45cfd468
DE
5455
5456 if (dwarf2_read_debug)
5457 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5458 objfile->name);
ae038cb0
DJ
5459}
5460
3019eac3 5461/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5462
5463static void
dee91e82
DE
5464load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5465 gdb_byte *info_ptr,
5466 struct die_info *comp_unit_die,
5467 int has_children,
5468 void *data)
ae038cb0 5469{
dee91e82 5470 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5471
95554aad 5472 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5473
ae038cb0
DJ
5474 /* Check if comp unit has_children.
5475 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5476 If not, there's no more debug_info for this comp unit. */
d85a05f0 5477 if (has_children)
dee91e82
DE
5478 load_partial_dies (reader, info_ptr, 0);
5479}
98bfdba5 5480
dee91e82
DE
5481/* Load the partial DIEs for a secondary CU into memory.
5482 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5483
dee91e82
DE
5484static void
5485load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5486{
f4dc4d17
DE
5487 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5488 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5489}
5490
ae038cb0 5491static void
36586728
TT
5492read_comp_units_from_section (struct objfile *objfile,
5493 struct dwarf2_section_info *section,
5494 unsigned int is_dwz,
5495 int *n_allocated,
5496 int *n_comp_units,
5497 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5498{
be391dca 5499 gdb_byte *info_ptr;
36586728 5500 bfd *abfd = section->asection->owner;
be391dca 5501
36586728 5502 dwarf2_read_section (objfile, section);
ae038cb0 5503
36586728 5504 info_ptr = section->buffer;
6e70227d 5505
36586728 5506 while (info_ptr < section->buffer + section->size)
ae038cb0 5507 {
c764a876 5508 unsigned int length, initial_length_size;
ae038cb0 5509 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5510 sect_offset offset;
ae038cb0 5511
36586728 5512 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5513
5514 /* Read just enough information to find out where the next
5515 compilation unit is. */
36586728 5516 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5517
5518 /* Save the compilation unit for later lookup. */
5519 this_cu = obstack_alloc (&objfile->objfile_obstack,
5520 sizeof (struct dwarf2_per_cu_data));
5521 memset (this_cu, 0, sizeof (*this_cu));
5522 this_cu->offset = offset;
c764a876 5523 this_cu->length = length + initial_length_size;
36586728 5524 this_cu->is_dwz = is_dwz;
9291a0cd 5525 this_cu->objfile = objfile;
36586728 5526 this_cu->info_or_types_section = section;
ae038cb0 5527
36586728 5528 if (*n_comp_units == *n_allocated)
ae038cb0 5529 {
36586728
TT
5530 *n_allocated *= 2;
5531 *all_comp_units = xrealloc (*all_comp_units,
5532 *n_allocated
5533 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5534 }
36586728
TT
5535 (*all_comp_units)[*n_comp_units] = this_cu;
5536 ++*n_comp_units;
ae038cb0
DJ
5537
5538 info_ptr = info_ptr + this_cu->length;
5539 }
36586728
TT
5540}
5541
5542/* Create a list of all compilation units in OBJFILE.
5543 This is only done for -readnow and building partial symtabs. */
5544
5545static void
5546create_all_comp_units (struct objfile *objfile)
5547{
5548 int n_allocated;
5549 int n_comp_units;
5550 struct dwarf2_per_cu_data **all_comp_units;
5551
5552 n_comp_units = 0;
5553 n_allocated = 10;
5554 all_comp_units = xmalloc (n_allocated
5555 * sizeof (struct dwarf2_per_cu_data *));
5556
5557 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5558 &n_allocated, &n_comp_units, &all_comp_units);
5559
5560 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5561 {
5562 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5563
5564 read_comp_units_from_section (objfile, &dwz->info, 1,
5565 &n_allocated, &n_comp_units,
5566 &all_comp_units);
5567 }
ae038cb0
DJ
5568
5569 dwarf2_per_objfile->all_comp_units
5570 = obstack_alloc (&objfile->objfile_obstack,
5571 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5572 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5573 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5574 xfree (all_comp_units);
5575 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5576}
5577
5734ee8b
DJ
5578/* Process all loaded DIEs for compilation unit CU, starting at
5579 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5580 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5581 DW_AT_ranges). If NEED_PC is set, then this function will set
5582 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5583 and record the covered ranges in the addrmap. */
c906108c 5584
72bf9492
DJ
5585static void
5586scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5587 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5588{
72bf9492 5589 struct partial_die_info *pdi;
c906108c 5590
91c24f0a
DC
5591 /* Now, march along the PDI's, descending into ones which have
5592 interesting children but skipping the children of the other ones,
5593 until we reach the end of the compilation unit. */
c906108c 5594
72bf9492 5595 pdi = first_die;
91c24f0a 5596
72bf9492
DJ
5597 while (pdi != NULL)
5598 {
5599 fixup_partial_die (pdi, cu);
c906108c 5600
f55ee35c 5601 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5602 children, so we need to look at them. Ditto for anonymous
5603 enums. */
933c6fe4 5604
72bf9492 5605 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5606 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5607 || pdi->tag == DW_TAG_imported_unit)
c906108c 5608 {
72bf9492 5609 switch (pdi->tag)
c906108c
SS
5610 {
5611 case DW_TAG_subprogram:
5734ee8b 5612 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5613 break;
72929c62 5614 case DW_TAG_constant:
c906108c
SS
5615 case DW_TAG_variable:
5616 case DW_TAG_typedef:
91c24f0a 5617 case DW_TAG_union_type:
72bf9492 5618 if (!pdi->is_declaration)
63d06c5c 5619 {
72bf9492 5620 add_partial_symbol (pdi, cu);
63d06c5c
DC
5621 }
5622 break;
c906108c 5623 case DW_TAG_class_type:
680b30c7 5624 case DW_TAG_interface_type:
c906108c 5625 case DW_TAG_structure_type:
72bf9492 5626 if (!pdi->is_declaration)
c906108c 5627 {
72bf9492 5628 add_partial_symbol (pdi, cu);
c906108c
SS
5629 }
5630 break;
91c24f0a 5631 case DW_TAG_enumeration_type:
72bf9492
DJ
5632 if (!pdi->is_declaration)
5633 add_partial_enumeration (pdi, cu);
c906108c
SS
5634 break;
5635 case DW_TAG_base_type:
a02abb62 5636 case DW_TAG_subrange_type:
c906108c 5637 /* File scope base type definitions are added to the partial
c5aa993b 5638 symbol table. */
72bf9492 5639 add_partial_symbol (pdi, cu);
c906108c 5640 break;
d9fa45fe 5641 case DW_TAG_namespace:
5734ee8b 5642 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5643 break;
5d7cb8df
JK
5644 case DW_TAG_module:
5645 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5646 break;
95554aad
TT
5647 case DW_TAG_imported_unit:
5648 {
5649 struct dwarf2_per_cu_data *per_cu;
5650
f4dc4d17
DE
5651 /* For now we don't handle imported units in type units. */
5652 if (cu->per_cu->is_debug_types)
5653 {
5654 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5655 " supported in type units [in module %s]"),
5656 cu->objfile->name);
5657 }
5658
95554aad 5659 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5660 pdi->is_dwz,
95554aad
TT
5661 cu->objfile);
5662
5663 /* Go read the partial unit, if needed. */
5664 if (per_cu->v.psymtab == NULL)
5665 process_psymtab_comp_unit (per_cu, 1);
5666
f4dc4d17
DE
5667 VEC_safe_push (dwarf2_per_cu_ptr,
5668 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5669 }
5670 break;
c906108c
SS
5671 default:
5672 break;
5673 }
5674 }
5675
72bf9492
DJ
5676 /* If the die has a sibling, skip to the sibling. */
5677
5678 pdi = pdi->die_sibling;
5679 }
5680}
5681
5682/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5683
72bf9492 5684 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5685 name is concatenated with "::" and the partial DIE's name. For
5686 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5687 Enumerators are an exception; they use the scope of their parent
5688 enumeration type, i.e. the name of the enumeration type is not
5689 prepended to the enumerator.
91c24f0a 5690
72bf9492
DJ
5691 There are two complexities. One is DW_AT_specification; in this
5692 case "parent" means the parent of the target of the specification,
5693 instead of the direct parent of the DIE. The other is compilers
5694 which do not emit DW_TAG_namespace; in this case we try to guess
5695 the fully qualified name of structure types from their members'
5696 linkage names. This must be done using the DIE's children rather
5697 than the children of any DW_AT_specification target. We only need
5698 to do this for structures at the top level, i.e. if the target of
5699 any DW_AT_specification (if any; otherwise the DIE itself) does not
5700 have a parent. */
5701
5702/* Compute the scope prefix associated with PDI's parent, in
5703 compilation unit CU. The result will be allocated on CU's
5704 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5705 field. NULL is returned if no prefix is necessary. */
5706static char *
5707partial_die_parent_scope (struct partial_die_info *pdi,
5708 struct dwarf2_cu *cu)
5709{
5710 char *grandparent_scope;
5711 struct partial_die_info *parent, *real_pdi;
91c24f0a 5712
72bf9492
DJ
5713 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5714 then this means the parent of the specification DIE. */
5715
5716 real_pdi = pdi;
72bf9492 5717 while (real_pdi->has_specification)
36586728
TT
5718 real_pdi = find_partial_die (real_pdi->spec_offset,
5719 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5720
5721 parent = real_pdi->die_parent;
5722 if (parent == NULL)
5723 return NULL;
5724
5725 if (parent->scope_set)
5726 return parent->scope;
5727
5728 fixup_partial_die (parent, cu);
5729
10b3939b 5730 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5731
acebe513
UW
5732 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5733 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5734 Work around this problem here. */
5735 if (cu->language == language_cplus
6e70227d 5736 && parent->tag == DW_TAG_namespace
acebe513
UW
5737 && strcmp (parent->name, "::") == 0
5738 && grandparent_scope == NULL)
5739 {
5740 parent->scope = NULL;
5741 parent->scope_set = 1;
5742 return NULL;
5743 }
5744
9c6c53f7
SA
5745 if (pdi->tag == DW_TAG_enumerator)
5746 /* Enumerators should not get the name of the enumeration as a prefix. */
5747 parent->scope = grandparent_scope;
5748 else if (parent->tag == DW_TAG_namespace
f55ee35c 5749 || parent->tag == DW_TAG_module
72bf9492
DJ
5750 || parent->tag == DW_TAG_structure_type
5751 || parent->tag == DW_TAG_class_type
680b30c7 5752 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5753 || parent->tag == DW_TAG_union_type
5754 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5755 {
5756 if (grandparent_scope == NULL)
5757 parent->scope = parent->name;
5758 else
3e43a32a
MS
5759 parent->scope = typename_concat (&cu->comp_unit_obstack,
5760 grandparent_scope,
f55ee35c 5761 parent->name, 0, cu);
72bf9492 5762 }
72bf9492
DJ
5763 else
5764 {
5765 /* FIXME drow/2004-04-01: What should we be doing with
5766 function-local names? For partial symbols, we should probably be
5767 ignoring them. */
5768 complaint (&symfile_complaints,
e2e0b3e5 5769 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5770 parent->tag, pdi->offset.sect_off);
72bf9492 5771 parent->scope = grandparent_scope;
c906108c
SS
5772 }
5773
72bf9492
DJ
5774 parent->scope_set = 1;
5775 return parent->scope;
5776}
5777
5778/* Return the fully scoped name associated with PDI, from compilation unit
5779 CU. The result will be allocated with malloc. */
4568ecf9 5780
72bf9492
DJ
5781static char *
5782partial_die_full_name (struct partial_die_info *pdi,
5783 struct dwarf2_cu *cu)
5784{
5785 char *parent_scope;
5786
98bfdba5
PA
5787 /* If this is a template instantiation, we can not work out the
5788 template arguments from partial DIEs. So, unfortunately, we have
5789 to go through the full DIEs. At least any work we do building
5790 types here will be reused if full symbols are loaded later. */
5791 if (pdi->has_template_arguments)
5792 {
5793 fixup_partial_die (pdi, cu);
5794
5795 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5796 {
5797 struct die_info *die;
5798 struct attribute attr;
5799 struct dwarf2_cu *ref_cu = cu;
5800
b64f50a1 5801 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5802 attr.name = 0;
5803 attr.form = DW_FORM_ref_addr;
4568ecf9 5804 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5805 die = follow_die_ref (NULL, &attr, &ref_cu);
5806
5807 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5808 }
5809 }
5810
72bf9492
DJ
5811 parent_scope = partial_die_parent_scope (pdi, cu);
5812 if (parent_scope == NULL)
5813 return NULL;
5814 else
f55ee35c 5815 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5816}
5817
5818static void
72bf9492 5819add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5820{
e7c27a73 5821 struct objfile *objfile = cu->objfile;
c906108c 5822 CORE_ADDR addr = 0;
decbce07 5823 char *actual_name = NULL;
e142c38c 5824 CORE_ADDR baseaddr;
72bf9492 5825 int built_actual_name = 0;
e142c38c
DJ
5826
5827 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5828
94af9270
KS
5829 actual_name = partial_die_full_name (pdi, cu);
5830 if (actual_name)
5831 built_actual_name = 1;
63d06c5c 5832
72bf9492
DJ
5833 if (actual_name == NULL)
5834 actual_name = pdi->name;
5835
c906108c
SS
5836 switch (pdi->tag)
5837 {
5838 case DW_TAG_subprogram:
2cfa0c8d 5839 if (pdi->is_external || cu->language == language_ada)
c906108c 5840 {
2cfa0c8d
JB
5841 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5842 of the global scope. But in Ada, we want to be able to access
5843 nested procedures globally. So all Ada subprograms are stored
5844 in the global scope. */
f47fb265 5845 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5846 mst_text, objfile); */
f47fb265
MS
5847 add_psymbol_to_list (actual_name, strlen (actual_name),
5848 built_actual_name,
5849 VAR_DOMAIN, LOC_BLOCK,
5850 &objfile->global_psymbols,
5851 0, pdi->lowpc + baseaddr,
5852 cu->language, objfile);
c906108c
SS
5853 }
5854 else
5855 {
f47fb265 5856 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5857 mst_file_text, objfile); */
f47fb265
MS
5858 add_psymbol_to_list (actual_name, strlen (actual_name),
5859 built_actual_name,
5860 VAR_DOMAIN, LOC_BLOCK,
5861 &objfile->static_psymbols,
5862 0, pdi->lowpc + baseaddr,
5863 cu->language, objfile);
c906108c
SS
5864 }
5865 break;
72929c62
JB
5866 case DW_TAG_constant:
5867 {
5868 struct psymbol_allocation_list *list;
5869
5870 if (pdi->is_external)
5871 list = &objfile->global_psymbols;
5872 else
5873 list = &objfile->static_psymbols;
f47fb265
MS
5874 add_psymbol_to_list (actual_name, strlen (actual_name),
5875 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5876 list, 0, 0, cu->language, objfile);
72929c62
JB
5877 }
5878 break;
c906108c 5879 case DW_TAG_variable:
95554aad
TT
5880 if (pdi->d.locdesc)
5881 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5882
95554aad 5883 if (pdi->d.locdesc
caac4577
JG
5884 && addr == 0
5885 && !dwarf2_per_objfile->has_section_at_zero)
5886 {
5887 /* A global or static variable may also have been stripped
5888 out by the linker if unused, in which case its address
5889 will be nullified; do not add such variables into partial
5890 symbol table then. */
5891 }
5892 else if (pdi->is_external)
c906108c
SS
5893 {
5894 /* Global Variable.
5895 Don't enter into the minimal symbol tables as there is
5896 a minimal symbol table entry from the ELF symbols already.
5897 Enter into partial symbol table if it has a location
5898 descriptor or a type.
5899 If the location descriptor is missing, new_symbol will create
5900 a LOC_UNRESOLVED symbol, the address of the variable will then
5901 be determined from the minimal symbol table whenever the variable
5902 is referenced.
5903 The address for the partial symbol table entry is not
5904 used by GDB, but it comes in handy for debugging partial symbol
5905 table building. */
5906
95554aad 5907 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5908 add_psymbol_to_list (actual_name, strlen (actual_name),
5909 built_actual_name,
5910 VAR_DOMAIN, LOC_STATIC,
5911 &objfile->global_psymbols,
5912 0, addr + baseaddr,
5913 cu->language, objfile);
c906108c
SS
5914 }
5915 else
5916 {
0963b4bd 5917 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5918 if (pdi->d.locdesc == NULL)
decbce07
MS
5919 {
5920 if (built_actual_name)
5921 xfree (actual_name);
5922 return;
5923 }
f47fb265 5924 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5925 mst_file_data, objfile); */
f47fb265
MS
5926 add_psymbol_to_list (actual_name, strlen (actual_name),
5927 built_actual_name,
5928 VAR_DOMAIN, LOC_STATIC,
5929 &objfile->static_psymbols,
5930 0, addr + baseaddr,
5931 cu->language, objfile);
c906108c
SS
5932 }
5933 break;
5934 case DW_TAG_typedef:
5935 case DW_TAG_base_type:
a02abb62 5936 case DW_TAG_subrange_type:
38d518c9 5937 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5938 built_actual_name,
176620f1 5939 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5940 &objfile->static_psymbols,
e142c38c 5941 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5942 break;
72bf9492
DJ
5943 case DW_TAG_namespace:
5944 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5945 built_actual_name,
72bf9492
DJ
5946 VAR_DOMAIN, LOC_TYPEDEF,
5947 &objfile->global_psymbols,
5948 0, (CORE_ADDR) 0, cu->language, objfile);
5949 break;
c906108c 5950 case DW_TAG_class_type:
680b30c7 5951 case DW_TAG_interface_type:
c906108c
SS
5952 case DW_TAG_structure_type:
5953 case DW_TAG_union_type:
5954 case DW_TAG_enumeration_type:
fa4028e9
JB
5955 /* Skip external references. The DWARF standard says in the section
5956 about "Structure, Union, and Class Type Entries": "An incomplete
5957 structure, union or class type is represented by a structure,
5958 union or class entry that does not have a byte size attribute
5959 and that has a DW_AT_declaration attribute." */
5960 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5961 {
5962 if (built_actual_name)
5963 xfree (actual_name);
5964 return;
5965 }
fa4028e9 5966
63d06c5c
DC
5967 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5968 static vs. global. */
38d518c9 5969 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5970 built_actual_name,
176620f1 5971 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5972 (cu->language == language_cplus
5973 || cu->language == language_java)
63d06c5c
DC
5974 ? &objfile->global_psymbols
5975 : &objfile->static_psymbols,
e142c38c 5976 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5977
c906108c
SS
5978 break;
5979 case DW_TAG_enumerator:
38d518c9 5980 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5981 built_actual_name,
176620f1 5982 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5983 (cu->language == language_cplus
5984 || cu->language == language_java)
f6fe98ef
DJ
5985 ? &objfile->global_psymbols
5986 : &objfile->static_psymbols,
e142c38c 5987 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5988 break;
5989 default:
5990 break;
5991 }
5c4e30ca 5992
72bf9492
DJ
5993 if (built_actual_name)
5994 xfree (actual_name);
c906108c
SS
5995}
5996
5c4e30ca
DC
5997/* Read a partial die corresponding to a namespace; also, add a symbol
5998 corresponding to that namespace to the symbol table. NAMESPACE is
5999 the name of the enclosing namespace. */
91c24f0a 6000
72bf9492
DJ
6001static void
6002add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6003 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6004 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6005{
72bf9492 6006 /* Add a symbol for the namespace. */
e7c27a73 6007
72bf9492 6008 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6009
6010 /* Now scan partial symbols in that namespace. */
6011
91c24f0a 6012 if (pdi->has_children)
5734ee8b 6013 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6014}
6015
5d7cb8df
JK
6016/* Read a partial die corresponding to a Fortran module. */
6017
6018static void
6019add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6020 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6021{
f55ee35c 6022 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6023
6024 if (pdi->has_children)
6025 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6026}
6027
bc30ff58
JB
6028/* Read a partial die corresponding to a subprogram and create a partial
6029 symbol for that subprogram. When the CU language allows it, this
6030 routine also defines a partial symbol for each nested subprogram
6031 that this subprogram contains.
6e70227d 6032
bc30ff58
JB
6033 DIE my also be a lexical block, in which case we simply search
6034 recursively for suprograms defined inside that lexical block.
6035 Again, this is only performed when the CU language allows this
6036 type of definitions. */
6037
6038static void
6039add_partial_subprogram (struct partial_die_info *pdi,
6040 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6041 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6042{
6043 if (pdi->tag == DW_TAG_subprogram)
6044 {
6045 if (pdi->has_pc_info)
6046 {
6047 if (pdi->lowpc < *lowpc)
6048 *lowpc = pdi->lowpc;
6049 if (pdi->highpc > *highpc)
6050 *highpc = pdi->highpc;
5734ee8b
DJ
6051 if (need_pc)
6052 {
6053 CORE_ADDR baseaddr;
6054 struct objfile *objfile = cu->objfile;
6055
6056 baseaddr = ANOFFSET (objfile->section_offsets,
6057 SECT_OFF_TEXT (objfile));
6058 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6059 pdi->lowpc + baseaddr,
6060 pdi->highpc - 1 + baseaddr,
9291a0cd 6061 cu->per_cu->v.psymtab);
5734ee8b 6062 }
481860b3
GB
6063 }
6064
6065 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6066 {
bc30ff58 6067 if (!pdi->is_declaration)
e8d05480
JB
6068 /* Ignore subprogram DIEs that do not have a name, they are
6069 illegal. Do not emit a complaint at this point, we will
6070 do so when we convert this psymtab into a symtab. */
6071 if (pdi->name)
6072 add_partial_symbol (pdi, cu);
bc30ff58
JB
6073 }
6074 }
6e70227d 6075
bc30ff58
JB
6076 if (! pdi->has_children)
6077 return;
6078
6079 if (cu->language == language_ada)
6080 {
6081 pdi = pdi->die_child;
6082 while (pdi != NULL)
6083 {
6084 fixup_partial_die (pdi, cu);
6085 if (pdi->tag == DW_TAG_subprogram
6086 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6087 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6088 pdi = pdi->die_sibling;
6089 }
6090 }
6091}
6092
91c24f0a
DC
6093/* Read a partial die corresponding to an enumeration type. */
6094
72bf9492
DJ
6095static void
6096add_partial_enumeration (struct partial_die_info *enum_pdi,
6097 struct dwarf2_cu *cu)
91c24f0a 6098{
72bf9492 6099 struct partial_die_info *pdi;
91c24f0a
DC
6100
6101 if (enum_pdi->name != NULL)
72bf9492
DJ
6102 add_partial_symbol (enum_pdi, cu);
6103
6104 pdi = enum_pdi->die_child;
6105 while (pdi)
91c24f0a 6106 {
72bf9492 6107 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6108 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6109 else
72bf9492
DJ
6110 add_partial_symbol (pdi, cu);
6111 pdi = pdi->die_sibling;
91c24f0a 6112 }
91c24f0a
DC
6113}
6114
6caca83c
CC
6115/* Return the initial uleb128 in the die at INFO_PTR. */
6116
6117static unsigned int
6118peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6119{
6120 unsigned int bytes_read;
6121
6122 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6123}
6124
4bb7a0a7
DJ
6125/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6126 Return the corresponding abbrev, or NULL if the number is zero (indicating
6127 an empty DIE). In either case *BYTES_READ will be set to the length of
6128 the initial number. */
6129
6130static struct abbrev_info *
fe1b8b76 6131peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6132 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6133{
6134 bfd *abfd = cu->objfile->obfd;
6135 unsigned int abbrev_number;
6136 struct abbrev_info *abbrev;
6137
6138 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6139
6140 if (abbrev_number == 0)
6141 return NULL;
6142
433df2d4 6143 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6144 if (!abbrev)
6145 {
3e43a32a
MS
6146 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6147 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6148 }
6149
6150 return abbrev;
6151}
6152
93311388
DE
6153/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6154 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6155 DIE. Any children of the skipped DIEs will also be skipped. */
6156
fe1b8b76 6157static gdb_byte *
dee91e82 6158skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6159{
dee91e82 6160 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6161 struct abbrev_info *abbrev;
6162 unsigned int bytes_read;
6163
6164 while (1)
6165 {
6166 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6167 if (abbrev == NULL)
6168 return info_ptr + bytes_read;
6169 else
dee91e82 6170 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6171 }
6172}
6173
93311388
DE
6174/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6175 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6176 abbrev corresponding to that skipped uleb128 should be passed in
6177 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6178 children. */
6179
fe1b8b76 6180static gdb_byte *
dee91e82
DE
6181skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6182 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6183{
6184 unsigned int bytes_read;
6185 struct attribute attr;
dee91e82
DE
6186 bfd *abfd = reader->abfd;
6187 struct dwarf2_cu *cu = reader->cu;
6188 gdb_byte *buffer = reader->buffer;
f664829e
DE
6189 const gdb_byte *buffer_end = reader->buffer_end;
6190 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6191 unsigned int form, i;
6192
6193 for (i = 0; i < abbrev->num_attrs; i++)
6194 {
6195 /* The only abbrev we care about is DW_AT_sibling. */
6196 if (abbrev->attrs[i].name == DW_AT_sibling)
6197 {
dee91e82 6198 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6199 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6200 complaint (&symfile_complaints,
6201 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6202 else
b64f50a1 6203 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6204 }
6205
6206 /* If it isn't DW_AT_sibling, skip this attribute. */
6207 form = abbrev->attrs[i].form;
6208 skip_attribute:
6209 switch (form)
6210 {
4bb7a0a7 6211 case DW_FORM_ref_addr:
ae411497
TT
6212 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6213 and later it is offset sized. */
6214 if (cu->header.version == 2)
6215 info_ptr += cu->header.addr_size;
6216 else
6217 info_ptr += cu->header.offset_size;
6218 break;
36586728
TT
6219 case DW_FORM_GNU_ref_alt:
6220 info_ptr += cu->header.offset_size;
6221 break;
ae411497 6222 case DW_FORM_addr:
4bb7a0a7
DJ
6223 info_ptr += cu->header.addr_size;
6224 break;
6225 case DW_FORM_data1:
6226 case DW_FORM_ref1:
6227 case DW_FORM_flag:
6228 info_ptr += 1;
6229 break;
2dc7f7b3
TT
6230 case DW_FORM_flag_present:
6231 break;
4bb7a0a7
DJ
6232 case DW_FORM_data2:
6233 case DW_FORM_ref2:
6234 info_ptr += 2;
6235 break;
6236 case DW_FORM_data4:
6237 case DW_FORM_ref4:
6238 info_ptr += 4;
6239 break;
6240 case DW_FORM_data8:
6241 case DW_FORM_ref8:
55f1336d 6242 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6243 info_ptr += 8;
6244 break;
6245 case DW_FORM_string:
9b1c24c8 6246 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6247 info_ptr += bytes_read;
6248 break;
2dc7f7b3 6249 case DW_FORM_sec_offset:
4bb7a0a7 6250 case DW_FORM_strp:
36586728 6251 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6252 info_ptr += cu->header.offset_size;
6253 break;
2dc7f7b3 6254 case DW_FORM_exprloc:
4bb7a0a7
DJ
6255 case DW_FORM_block:
6256 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6257 info_ptr += bytes_read;
6258 break;
6259 case DW_FORM_block1:
6260 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6261 break;
6262 case DW_FORM_block2:
6263 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6264 break;
6265 case DW_FORM_block4:
6266 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6267 break;
6268 case DW_FORM_sdata:
6269 case DW_FORM_udata:
6270 case DW_FORM_ref_udata:
3019eac3
DE
6271 case DW_FORM_GNU_addr_index:
6272 case DW_FORM_GNU_str_index:
f664829e 6273 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6274 break;
6275 case DW_FORM_indirect:
6276 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6277 info_ptr += bytes_read;
6278 /* We need to continue parsing from here, so just go back to
6279 the top. */
6280 goto skip_attribute;
6281
6282 default:
3e43a32a
MS
6283 error (_("Dwarf Error: Cannot handle %s "
6284 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6285 dwarf_form_name (form),
6286 bfd_get_filename (abfd));
6287 }
6288 }
6289
6290 if (abbrev->has_children)
dee91e82 6291 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6292 else
6293 return info_ptr;
6294}
6295
93311388 6296/* Locate ORIG_PDI's sibling.
dee91e82 6297 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6298
fe1b8b76 6299static gdb_byte *
dee91e82
DE
6300locate_pdi_sibling (const struct die_reader_specs *reader,
6301 struct partial_die_info *orig_pdi,
6302 gdb_byte *info_ptr)
91c24f0a
DC
6303{
6304 /* Do we know the sibling already? */
72bf9492 6305
91c24f0a
DC
6306 if (orig_pdi->sibling)
6307 return orig_pdi->sibling;
6308
6309 /* Are there any children to deal with? */
6310
6311 if (!orig_pdi->has_children)
6312 return info_ptr;
6313
4bb7a0a7 6314 /* Skip the children the long way. */
91c24f0a 6315
dee91e82 6316 return skip_children (reader, info_ptr);
91c24f0a
DC
6317}
6318
c906108c
SS
6319/* Expand this partial symbol table into a full symbol table. */
6320
6321static void
fba45db2 6322dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 6323{
c906108c
SS
6324 if (pst != NULL)
6325 {
6326 if (pst->readin)
6327 {
3e43a32a
MS
6328 warning (_("bug: psymtab for %s is already read in."),
6329 pst->filename);
c906108c
SS
6330 }
6331 else
6332 {
6333 if (info_verbose)
6334 {
3e43a32a
MS
6335 printf_filtered (_("Reading in symbols for %s..."),
6336 pst->filename);
c906108c
SS
6337 gdb_flush (gdb_stdout);
6338 }
6339
10b3939b
DJ
6340 /* Restore our global data. */
6341 dwarf2_per_objfile = objfile_data (pst->objfile,
6342 dwarf2_objfile_data_key);
6343
b2ab525c
KB
6344 /* If this psymtab is constructed from a debug-only objfile, the
6345 has_section_at_zero flag will not necessarily be correct. We
6346 can get the correct value for this flag by looking at the data
6347 associated with the (presumably stripped) associated objfile. */
6348 if (pst->objfile->separate_debug_objfile_backlink)
6349 {
6350 struct dwarf2_per_objfile *dpo_backlink
6351 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6352 dwarf2_objfile_data_key);
9a619af0 6353
b2ab525c
KB
6354 dwarf2_per_objfile->has_section_at_zero
6355 = dpo_backlink->has_section_at_zero;
6356 }
6357
98bfdba5
PA
6358 dwarf2_per_objfile->reading_partial_symbols = 0;
6359
c906108c
SS
6360 psymtab_to_symtab_1 (pst);
6361
6362 /* Finish up the debug error message. */
6363 if (info_verbose)
a3f17187 6364 printf_filtered (_("done.\n"));
c906108c
SS
6365 }
6366 }
95554aad
TT
6367
6368 process_cu_includes ();
c906108c 6369}
9cdd5dbd
DE
6370\f
6371/* Reading in full CUs. */
c906108c 6372
10b3939b
DJ
6373/* Add PER_CU to the queue. */
6374
6375static void
95554aad
TT
6376queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6377 enum language pretend_language)
10b3939b
DJ
6378{
6379 struct dwarf2_queue_item *item;
6380
6381 per_cu->queued = 1;
6382 item = xmalloc (sizeof (*item));
6383 item->per_cu = per_cu;
95554aad 6384 item->pretend_language = pretend_language;
10b3939b
DJ
6385 item->next = NULL;
6386
6387 if (dwarf2_queue == NULL)
6388 dwarf2_queue = item;
6389 else
6390 dwarf2_queue_tail->next = item;
6391
6392 dwarf2_queue_tail = item;
6393}
6394
0907af0c
DE
6395/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6396 unit and add it to our queue.
6397 The result is non-zero if PER_CU was queued, otherwise the result is zero
6398 meaning either PER_CU is already queued or it is already loaded. */
6399
6400static int
6401maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6402 struct dwarf2_per_cu_data *per_cu,
6403 enum language pretend_language)
6404{
6405 /* We may arrive here during partial symbol reading, if we need full
6406 DIEs to process an unusual case (e.g. template arguments). Do
6407 not queue PER_CU, just tell our caller to load its DIEs. */
6408 if (dwarf2_per_objfile->reading_partial_symbols)
6409 {
6410 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6411 return 1;
6412 return 0;
6413 }
6414
6415 /* Mark the dependence relation so that we don't flush PER_CU
6416 too early. */
6417 dwarf2_add_dependence (this_cu, per_cu);
6418
6419 /* If it's already on the queue, we have nothing to do. */
6420 if (per_cu->queued)
6421 return 0;
6422
6423 /* If the compilation unit is already loaded, just mark it as
6424 used. */
6425 if (per_cu->cu != NULL)
6426 {
6427 per_cu->cu->last_used = 0;
6428 return 0;
6429 }
6430
6431 /* Add it to the queue. */
6432 queue_comp_unit (per_cu, pretend_language);
6433
6434 return 1;
6435}
6436
10b3939b
DJ
6437/* Process the queue. */
6438
6439static void
a0f42c21 6440process_queue (void)
10b3939b
DJ
6441{
6442 struct dwarf2_queue_item *item, *next_item;
6443
45cfd468
DE
6444 if (dwarf2_read_debug)
6445 {
6446 fprintf_unfiltered (gdb_stdlog,
6447 "Expanding one or more symtabs of objfile %s ...\n",
6448 dwarf2_per_objfile->objfile->name);
6449 }
6450
03dd20cc
DJ
6451 /* The queue starts out with one item, but following a DIE reference
6452 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6453 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6454 {
9291a0cd
TT
6455 if (dwarf2_per_objfile->using_index
6456 ? !item->per_cu->v.quick->symtab
6457 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6458 {
6459 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6460
6461 if (dwarf2_read_debug)
6462 {
6463 fprintf_unfiltered (gdb_stdlog,
6464 "Expanding symtab of %s at offset 0x%x\n",
6465 per_cu->is_debug_types ? "TU" : "CU",
6466 per_cu->offset.sect_off);
6467 }
6468
6469 if (per_cu->is_debug_types)
6470 process_full_type_unit (per_cu, item->pretend_language);
6471 else
6472 process_full_comp_unit (per_cu, item->pretend_language);
6473
6474 if (dwarf2_read_debug)
6475 {
6476 fprintf_unfiltered (gdb_stdlog,
6477 "Done expanding %s at offset 0x%x\n",
6478 per_cu->is_debug_types ? "TU" : "CU",
6479 per_cu->offset.sect_off);
6480 }
6481 }
10b3939b
DJ
6482
6483 item->per_cu->queued = 0;
6484 next_item = item->next;
6485 xfree (item);
6486 }
6487
6488 dwarf2_queue_tail = NULL;
45cfd468
DE
6489
6490 if (dwarf2_read_debug)
6491 {
6492 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6493 dwarf2_per_objfile->objfile->name);
6494 }
10b3939b
DJ
6495}
6496
6497/* Free all allocated queue entries. This function only releases anything if
6498 an error was thrown; if the queue was processed then it would have been
6499 freed as we went along. */
6500
6501static void
6502dwarf2_release_queue (void *dummy)
6503{
6504 struct dwarf2_queue_item *item, *last;
6505
6506 item = dwarf2_queue;
6507 while (item)
6508 {
6509 /* Anything still marked queued is likely to be in an
6510 inconsistent state, so discard it. */
6511 if (item->per_cu->queued)
6512 {
6513 if (item->per_cu->cu != NULL)
dee91e82 6514 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6515 item->per_cu->queued = 0;
6516 }
6517
6518 last = item;
6519 item = item->next;
6520 xfree (last);
6521 }
6522
6523 dwarf2_queue = dwarf2_queue_tail = NULL;
6524}
6525
6526/* Read in full symbols for PST, and anything it depends on. */
6527
c906108c 6528static void
fba45db2 6529psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6530{
10b3939b 6531 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6532 int i;
6533
95554aad
TT
6534 if (pst->readin)
6535 return;
6536
aaa75496 6537 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6538 if (!pst->dependencies[i]->readin
6539 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6540 {
6541 /* Inform about additional files that need to be read in. */
6542 if (info_verbose)
6543 {
a3f17187 6544 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6545 fputs_filtered (" ", gdb_stdout);
6546 wrap_here ("");
6547 fputs_filtered ("and ", gdb_stdout);
6548 wrap_here ("");
6549 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6550 wrap_here (""); /* Flush output. */
aaa75496
JB
6551 gdb_flush (gdb_stdout);
6552 }
6553 psymtab_to_symtab_1 (pst->dependencies[i]);
6554 }
6555
e38df1d0 6556 per_cu = pst->read_symtab_private;
10b3939b
DJ
6557
6558 if (per_cu == NULL)
aaa75496
JB
6559 {
6560 /* It's an include file, no symbols to read for it.
6561 Everything is in the parent symtab. */
6562 pst->readin = 1;
6563 return;
6564 }
c906108c 6565
a0f42c21 6566 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6567}
6568
dee91e82
DE
6569/* Trivial hash function for die_info: the hash value of a DIE
6570 is its offset in .debug_info for this objfile. */
10b3939b 6571
dee91e82
DE
6572static hashval_t
6573die_hash (const void *item)
10b3939b 6574{
dee91e82 6575 const struct die_info *die = item;
6502dd73 6576
dee91e82
DE
6577 return die->offset.sect_off;
6578}
63d06c5c 6579
dee91e82
DE
6580/* Trivial comparison function for die_info structures: two DIEs
6581 are equal if they have the same offset. */
98bfdba5 6582
dee91e82
DE
6583static int
6584die_eq (const void *item_lhs, const void *item_rhs)
6585{
6586 const struct die_info *die_lhs = item_lhs;
6587 const struct die_info *die_rhs = item_rhs;
c906108c 6588
dee91e82
DE
6589 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6590}
c906108c 6591
dee91e82
DE
6592/* die_reader_func for load_full_comp_unit.
6593 This is identical to read_signatured_type_reader,
6594 but is kept separate for now. */
c906108c 6595
dee91e82
DE
6596static void
6597load_full_comp_unit_reader (const struct die_reader_specs *reader,
6598 gdb_byte *info_ptr,
6599 struct die_info *comp_unit_die,
6600 int has_children,
6601 void *data)
6602{
6603 struct dwarf2_cu *cu = reader->cu;
95554aad 6604 enum language *language_ptr = data;
6caca83c 6605
dee91e82
DE
6606 gdb_assert (cu->die_hash == NULL);
6607 cu->die_hash =
6608 htab_create_alloc_ex (cu->header.length / 12,
6609 die_hash,
6610 die_eq,
6611 NULL,
6612 &cu->comp_unit_obstack,
6613 hashtab_obstack_allocate,
6614 dummy_obstack_deallocate);
e142c38c 6615
dee91e82
DE
6616 if (has_children)
6617 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6618 &info_ptr, comp_unit_die);
6619 cu->dies = comp_unit_die;
6620 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6621
6622 /* We try not to read any attributes in this function, because not
9cdd5dbd 6623 all CUs needed for references have been loaded yet, and symbol
10b3939b 6624 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6625 or we won't be able to build types correctly.
6626 Similarly, if we do not read the producer, we can not apply
6627 producer-specific interpretation. */
95554aad 6628 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6629}
10b3939b 6630
dee91e82 6631/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6632
dee91e82 6633static void
95554aad
TT
6634load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6635 enum language pretend_language)
dee91e82 6636{
3019eac3 6637 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6638
f4dc4d17
DE
6639 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6640 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6641}
6642
3da10d80
KS
6643/* Add a DIE to the delayed physname list. */
6644
6645static void
6646add_to_method_list (struct type *type, int fnfield_index, int index,
6647 const char *name, struct die_info *die,
6648 struct dwarf2_cu *cu)
6649{
6650 struct delayed_method_info mi;
6651 mi.type = type;
6652 mi.fnfield_index = fnfield_index;
6653 mi.index = index;
6654 mi.name = name;
6655 mi.die = die;
6656 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6657}
6658
6659/* A cleanup for freeing the delayed method list. */
6660
6661static void
6662free_delayed_list (void *ptr)
6663{
6664 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6665 if (cu->method_list != NULL)
6666 {
6667 VEC_free (delayed_method_info, cu->method_list);
6668 cu->method_list = NULL;
6669 }
6670}
6671
6672/* Compute the physnames of any methods on the CU's method list.
6673
6674 The computation of method physnames is delayed in order to avoid the
6675 (bad) condition that one of the method's formal parameters is of an as yet
6676 incomplete type. */
6677
6678static void
6679compute_delayed_physnames (struct dwarf2_cu *cu)
6680{
6681 int i;
6682 struct delayed_method_info *mi;
6683 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6684 {
1d06ead6 6685 const char *physname;
3da10d80
KS
6686 struct fn_fieldlist *fn_flp
6687 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6688 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6689 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6690 }
6691}
6692
a766d390
DE
6693/* Go objects should be embedded in a DW_TAG_module DIE,
6694 and it's not clear if/how imported objects will appear.
6695 To keep Go support simple until that's worked out,
6696 go back through what we've read and create something usable.
6697 We could do this while processing each DIE, and feels kinda cleaner,
6698 but that way is more invasive.
6699 This is to, for example, allow the user to type "p var" or "b main"
6700 without having to specify the package name, and allow lookups
6701 of module.object to work in contexts that use the expression
6702 parser. */
6703
6704static void
6705fixup_go_packaging (struct dwarf2_cu *cu)
6706{
6707 char *package_name = NULL;
6708 struct pending *list;
6709 int i;
6710
6711 for (list = global_symbols; list != NULL; list = list->next)
6712 {
6713 for (i = 0; i < list->nsyms; ++i)
6714 {
6715 struct symbol *sym = list->symbol[i];
6716
6717 if (SYMBOL_LANGUAGE (sym) == language_go
6718 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6719 {
6720 char *this_package_name = go_symbol_package_name (sym);
6721
6722 if (this_package_name == NULL)
6723 continue;
6724 if (package_name == NULL)
6725 package_name = this_package_name;
6726 else
6727 {
6728 if (strcmp (package_name, this_package_name) != 0)
6729 complaint (&symfile_complaints,
6730 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17
TT
6731 (SYMBOL_SYMTAB (sym)
6732 && SYMBOL_SYMTAB (sym)->filename
6733 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6734 : cu->objfile->name),
6735 this_package_name, package_name);
6736 xfree (this_package_name);
6737 }
6738 }
6739 }
6740 }
6741
6742 if (package_name != NULL)
6743 {
6744 struct objfile *objfile = cu->objfile;
6745 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6746 package_name, objfile);
6747 struct symbol *sym;
6748
6749 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6750
6751 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6752 SYMBOL_SET_LANGUAGE (sym, language_go);
6753 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6754 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6755 e.g., "main" finds the "main" module and not C's main(). */
6756 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6757 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6758 SYMBOL_TYPE (sym) = type;
6759
6760 add_symbol_to_list (sym, &global_symbols);
6761
6762 xfree (package_name);
6763 }
6764}
6765
95554aad
TT
6766static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6767
6768/* Return the symtab for PER_CU. This works properly regardless of
6769 whether we're using the index or psymtabs. */
6770
6771static struct symtab *
6772get_symtab (struct dwarf2_per_cu_data *per_cu)
6773{
6774 return (dwarf2_per_objfile->using_index
6775 ? per_cu->v.quick->symtab
6776 : per_cu->v.psymtab->symtab);
6777}
6778
6779/* A helper function for computing the list of all symbol tables
6780 included by PER_CU. */
6781
6782static void
6783recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6784 htab_t all_children,
6785 struct dwarf2_per_cu_data *per_cu)
6786{
6787 void **slot;
6788 int ix;
6789 struct dwarf2_per_cu_data *iter;
6790
6791 slot = htab_find_slot (all_children, per_cu, INSERT);
6792 if (*slot != NULL)
6793 {
6794 /* This inclusion and its children have been processed. */
6795 return;
6796 }
6797
6798 *slot = per_cu;
6799 /* Only add a CU if it has a symbol table. */
6800 if (get_symtab (per_cu) != NULL)
6801 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6802
6803 for (ix = 0;
f4dc4d17 6804 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6805 ++ix)
6806 recursively_compute_inclusions (result, all_children, iter);
6807}
6808
6809/* Compute the symtab 'includes' fields for the symtab related to
6810 PER_CU. */
6811
6812static void
6813compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6814{
f4dc4d17
DE
6815 gdb_assert (! per_cu->is_debug_types);
6816
6817 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6818 {
6819 int ix, len;
6820 struct dwarf2_per_cu_data *iter;
6821 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6822 htab_t all_children;
6823 struct symtab *symtab = get_symtab (per_cu);
6824
6825 /* If we don't have a symtab, we can just skip this case. */
6826 if (symtab == NULL)
6827 return;
6828
6829 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6830 NULL, xcalloc, xfree);
6831
6832 for (ix = 0;
f4dc4d17 6833 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6834 ix, iter);
6835 ++ix)
6836 recursively_compute_inclusions (&result_children, all_children, iter);
6837
6838 /* Now we have a transitive closure of all the included CUs, so
6839 we can convert it to a list of symtabs. */
6840 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6841 symtab->includes
6842 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6843 (len + 1) * sizeof (struct symtab *));
6844 for (ix = 0;
6845 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6846 ++ix)
6847 symtab->includes[ix] = get_symtab (iter);
6848 symtab->includes[len] = NULL;
6849
6850 VEC_free (dwarf2_per_cu_ptr, result_children);
6851 htab_delete (all_children);
6852 }
6853}
6854
6855/* Compute the 'includes' field for the symtabs of all the CUs we just
6856 read. */
6857
6858static void
6859process_cu_includes (void)
6860{
6861 int ix;
6862 struct dwarf2_per_cu_data *iter;
6863
6864 for (ix = 0;
6865 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6866 ix, iter);
6867 ++ix)
f4dc4d17
DE
6868 {
6869 if (! iter->is_debug_types)
6870 compute_symtab_includes (iter);
6871 }
95554aad
TT
6872
6873 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6874}
6875
9cdd5dbd 6876/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6877 already been loaded into memory. */
6878
6879static void
95554aad
TT
6880process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6881 enum language pretend_language)
10b3939b 6882{
10b3939b 6883 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6884 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6885 CORE_ADDR lowpc, highpc;
6886 struct symtab *symtab;
3da10d80 6887 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6888 CORE_ADDR baseaddr;
4359dff1 6889 struct block *static_block;
10b3939b
DJ
6890
6891 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6892
10b3939b
DJ
6893 buildsym_init ();
6894 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6895 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6896
6897 cu->list_in_scope = &file_symbols;
c906108c 6898
95554aad
TT
6899 cu->language = pretend_language;
6900 cu->language_defn = language_def (cu->language);
6901
c906108c 6902 /* Do line number decoding in read_file_scope () */
10b3939b 6903 process_die (cu->dies, cu);
c906108c 6904
a766d390
DE
6905 /* For now fudge the Go package. */
6906 if (cu->language == language_go)
6907 fixup_go_packaging (cu);
6908
3da10d80
KS
6909 /* Now that we have processed all the DIEs in the CU, all the types
6910 should be complete, and it should now be safe to compute all of the
6911 physnames. */
6912 compute_delayed_physnames (cu);
6913 do_cleanups (delayed_list_cleanup);
6914
fae299cd
DC
6915 /* Some compilers don't define a DW_AT_high_pc attribute for the
6916 compilation unit. If the DW_AT_high_pc is missing, synthesize
6917 it, by scanning the DIE's below the compilation unit. */
10b3939b 6918 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6919
36586728
TT
6920 static_block
6921 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6922 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6923
6924 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6925 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6926 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6927 addrmap to help ensure it has an accurate map of pc values belonging to
6928 this comp unit. */
6929 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6930
6931 symtab = end_symtab_from_static_block (static_block, objfile,
6932 SECT_OFF_TEXT (objfile), 0);
c906108c 6933
8be455d7 6934 if (symtab != NULL)
c906108c 6935 {
df15bd07 6936 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6937
8be455d7
JK
6938 /* Set symtab language to language from DW_AT_language. If the
6939 compilation is from a C file generated by language preprocessors, do
6940 not set the language if it was already deduced by start_subfile. */
6941 if (!(cu->language == language_c && symtab->language != language_c))
6942 symtab->language = cu->language;
6943
6944 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6945 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6946 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6947 there were bugs in prologue debug info, fixed later in GCC-4.5
6948 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6949
6950 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6951 needed, it would be wrong due to missing DW_AT_producer there.
6952
6953 Still one can confuse GDB by using non-standard GCC compilation
6954 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6955 */
ab260dad 6956 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6957 symtab->locations_valid = 1;
e0d00bc7
JK
6958
6959 if (gcc_4_minor >= 5)
6960 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6961
6962 symtab->call_site_htab = cu->call_site_htab;
c906108c 6963 }
9291a0cd
TT
6964
6965 if (dwarf2_per_objfile->using_index)
6966 per_cu->v.quick->symtab = symtab;
6967 else
6968 {
6969 struct partial_symtab *pst = per_cu->v.psymtab;
6970 pst->symtab = symtab;
6971 pst->readin = 1;
6972 }
c906108c 6973
95554aad
TT
6974 /* Push it for inclusion processing later. */
6975 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6976
c906108c 6977 do_cleanups (back_to);
f4dc4d17 6978}
45cfd468 6979
f4dc4d17
DE
6980/* Generate full symbol information for type unit PER_CU, whose DIEs have
6981 already been loaded into memory. */
6982
6983static void
6984process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6985 enum language pretend_language)
6986{
6987 struct dwarf2_cu *cu = per_cu->cu;
6988 struct objfile *objfile = per_cu->objfile;
6989 struct symtab *symtab;
6990 struct cleanup *back_to, *delayed_list_cleanup;
6991
6992 buildsym_init ();
6993 back_to = make_cleanup (really_free_pendings, NULL);
6994 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6995
6996 cu->list_in_scope = &file_symbols;
6997
6998 cu->language = pretend_language;
6999 cu->language_defn = language_def (cu->language);
7000
7001 /* The symbol tables are set up in read_type_unit_scope. */
7002 process_die (cu->dies, cu);
7003
7004 /* For now fudge the Go package. */
7005 if (cu->language == language_go)
7006 fixup_go_packaging (cu);
7007
7008 /* Now that we have processed all the DIEs in the CU, all the types
7009 should be complete, and it should now be safe to compute all of the
7010 physnames. */
7011 compute_delayed_physnames (cu);
7012 do_cleanups (delayed_list_cleanup);
7013
7014 /* TUs share symbol tables.
7015 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7016 of it with end_expandable_symtab. Otherwise, complete the addition of
7017 this TU's symbols to the existing symtab. */
f4dc4d17 7018 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7019 {
f4dc4d17
DE
7020 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7021 per_cu->s.type_unit_group->primary_symtab = symtab;
7022
7023 if (symtab != NULL)
7024 {
7025 /* Set symtab language to language from DW_AT_language. If the
7026 compilation is from a C file generated by language preprocessors,
7027 do not set the language if it was already deduced by
7028 start_subfile. */
7029 if (!(cu->language == language_c && symtab->language != language_c))
7030 symtab->language = cu->language;
7031 }
7032 }
7033 else
7034 {
7035 augment_type_symtab (objfile,
7036 per_cu->s.type_unit_group->primary_symtab);
7037 symtab = per_cu->s.type_unit_group->primary_symtab;
7038 }
7039
7040 if (dwarf2_per_objfile->using_index)
7041 per_cu->v.quick->symtab = symtab;
7042 else
7043 {
7044 struct partial_symtab *pst = per_cu->v.psymtab;
7045 pst->symtab = symtab;
7046 pst->readin = 1;
45cfd468 7047 }
f4dc4d17
DE
7048
7049 do_cleanups (back_to);
c906108c
SS
7050}
7051
95554aad
TT
7052/* Process an imported unit DIE. */
7053
7054static void
7055process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7056{
7057 struct attribute *attr;
7058
f4dc4d17
DE
7059 /* For now we don't handle imported units in type units. */
7060 if (cu->per_cu->is_debug_types)
7061 {
7062 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7063 " supported in type units [in module %s]"),
7064 cu->objfile->name);
7065 }
7066
95554aad
TT
7067 attr = dwarf2_attr (die, DW_AT_import, cu);
7068 if (attr != NULL)
7069 {
7070 struct dwarf2_per_cu_data *per_cu;
7071 struct symtab *imported_symtab;
7072 sect_offset offset;
36586728 7073 int is_dwz;
95554aad
TT
7074
7075 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7076 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7077 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7078
7079 /* Queue the unit, if needed. */
7080 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7081 load_full_comp_unit (per_cu, cu->language);
7082
f4dc4d17 7083 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7084 per_cu);
7085 }
7086}
7087
c906108c
SS
7088/* Process a die and its children. */
7089
7090static void
e7c27a73 7091process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7092{
7093 switch (die->tag)
7094 {
7095 case DW_TAG_padding:
7096 break;
7097 case DW_TAG_compile_unit:
95554aad 7098 case DW_TAG_partial_unit:
e7c27a73 7099 read_file_scope (die, cu);
c906108c 7100 break;
348e048f
DE
7101 case DW_TAG_type_unit:
7102 read_type_unit_scope (die, cu);
7103 break;
c906108c 7104 case DW_TAG_subprogram:
c906108c 7105 case DW_TAG_inlined_subroutine:
edb3359d 7106 read_func_scope (die, cu);
c906108c
SS
7107 break;
7108 case DW_TAG_lexical_block:
14898363
L
7109 case DW_TAG_try_block:
7110 case DW_TAG_catch_block:
e7c27a73 7111 read_lexical_block_scope (die, cu);
c906108c 7112 break;
96408a79
SA
7113 case DW_TAG_GNU_call_site:
7114 read_call_site_scope (die, cu);
7115 break;
c906108c 7116 case DW_TAG_class_type:
680b30c7 7117 case DW_TAG_interface_type:
c906108c
SS
7118 case DW_TAG_structure_type:
7119 case DW_TAG_union_type:
134d01f1 7120 process_structure_scope (die, cu);
c906108c
SS
7121 break;
7122 case DW_TAG_enumeration_type:
134d01f1 7123 process_enumeration_scope (die, cu);
c906108c 7124 break;
134d01f1 7125
f792889a
DJ
7126 /* These dies have a type, but processing them does not create
7127 a symbol or recurse to process the children. Therefore we can
7128 read them on-demand through read_type_die. */
c906108c 7129 case DW_TAG_subroutine_type:
72019c9c 7130 case DW_TAG_set_type:
c906108c 7131 case DW_TAG_array_type:
c906108c 7132 case DW_TAG_pointer_type:
c906108c 7133 case DW_TAG_ptr_to_member_type:
c906108c 7134 case DW_TAG_reference_type:
c906108c 7135 case DW_TAG_string_type:
c906108c 7136 break;
134d01f1 7137
c906108c 7138 case DW_TAG_base_type:
a02abb62 7139 case DW_TAG_subrange_type:
cb249c71 7140 case DW_TAG_typedef:
134d01f1
DJ
7141 /* Add a typedef symbol for the type definition, if it has a
7142 DW_AT_name. */
f792889a 7143 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7144 break;
c906108c 7145 case DW_TAG_common_block:
e7c27a73 7146 read_common_block (die, cu);
c906108c
SS
7147 break;
7148 case DW_TAG_common_inclusion:
7149 break;
d9fa45fe 7150 case DW_TAG_namespace:
63d06c5c 7151 processing_has_namespace_info = 1;
e7c27a73 7152 read_namespace (die, cu);
d9fa45fe 7153 break;
5d7cb8df 7154 case DW_TAG_module:
f55ee35c 7155 processing_has_namespace_info = 1;
5d7cb8df
JK
7156 read_module (die, cu);
7157 break;
d9fa45fe
DC
7158 case DW_TAG_imported_declaration:
7159 case DW_TAG_imported_module:
63d06c5c 7160 processing_has_namespace_info = 1;
27aa8d6a
SW
7161 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7162 || cu->language != language_fortran))
7163 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7164 dwarf_tag_name (die->tag));
7165 read_import_statement (die, cu);
d9fa45fe 7166 break;
95554aad
TT
7167
7168 case DW_TAG_imported_unit:
7169 process_imported_unit_die (die, cu);
7170 break;
7171
c906108c 7172 default:
e7c27a73 7173 new_symbol (die, NULL, cu);
c906108c
SS
7174 break;
7175 }
7176}
7177
94af9270
KS
7178/* A helper function for dwarf2_compute_name which determines whether DIE
7179 needs to have the name of the scope prepended to the name listed in the
7180 die. */
7181
7182static int
7183die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7184{
1c809c68
TT
7185 struct attribute *attr;
7186
94af9270
KS
7187 switch (die->tag)
7188 {
7189 case DW_TAG_namespace:
7190 case DW_TAG_typedef:
7191 case DW_TAG_class_type:
7192 case DW_TAG_interface_type:
7193 case DW_TAG_structure_type:
7194 case DW_TAG_union_type:
7195 case DW_TAG_enumeration_type:
7196 case DW_TAG_enumerator:
7197 case DW_TAG_subprogram:
7198 case DW_TAG_member:
7199 return 1;
7200
7201 case DW_TAG_variable:
c2b0a229 7202 case DW_TAG_constant:
94af9270
KS
7203 /* We only need to prefix "globally" visible variables. These include
7204 any variable marked with DW_AT_external or any variable that
7205 lives in a namespace. [Variables in anonymous namespaces
7206 require prefixing, but they are not DW_AT_external.] */
7207
7208 if (dwarf2_attr (die, DW_AT_specification, cu))
7209 {
7210 struct dwarf2_cu *spec_cu = cu;
9a619af0 7211
94af9270
KS
7212 return die_needs_namespace (die_specification (die, &spec_cu),
7213 spec_cu);
7214 }
7215
1c809c68 7216 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7217 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7218 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7219 return 0;
7220 /* A variable in a lexical block of some kind does not need a
7221 namespace, even though in C++ such variables may be external
7222 and have a mangled name. */
7223 if (die->parent->tag == DW_TAG_lexical_block
7224 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7225 || die->parent->tag == DW_TAG_catch_block
7226 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7227 return 0;
7228 return 1;
94af9270
KS
7229
7230 default:
7231 return 0;
7232 }
7233}
7234
98bfdba5
PA
7235/* Retrieve the last character from a mem_file. */
7236
7237static void
7238do_ui_file_peek_last (void *object, const char *buffer, long length)
7239{
7240 char *last_char_p = (char *) object;
7241
7242 if (length > 0)
7243 *last_char_p = buffer[length - 1];
7244}
7245
94af9270 7246/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7247 compute the physname for the object, which include a method's:
7248 - formal parameters (C++/Java),
7249 - receiver type (Go),
7250 - return type (Java).
7251
7252 The term "physname" is a bit confusing.
7253 For C++, for example, it is the demangled name.
7254 For Go, for example, it's the mangled name.
94af9270 7255
af6b7be1
JB
7256 For Ada, return the DIE's linkage name rather than the fully qualified
7257 name. PHYSNAME is ignored..
7258
94af9270
KS
7259 The result is allocated on the objfile_obstack and canonicalized. */
7260
7261static const char *
7262dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7263 int physname)
7264{
bb5ed363
DE
7265 struct objfile *objfile = cu->objfile;
7266
94af9270
KS
7267 if (name == NULL)
7268 name = dwarf2_name (die, cu);
7269
f55ee35c
JK
7270 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7271 compute it by typename_concat inside GDB. */
7272 if (cu->language == language_ada
7273 || (cu->language == language_fortran && physname))
7274 {
7275 /* For Ada unit, we prefer the linkage name over the name, as
7276 the former contains the exported name, which the user expects
7277 to be able to reference. Ideally, we want the user to be able
7278 to reference this entity using either natural or linkage name,
7279 but we haven't started looking at this enhancement yet. */
7280 struct attribute *attr;
7281
7282 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7283 if (attr == NULL)
7284 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7285 if (attr && DW_STRING (attr))
7286 return DW_STRING (attr);
7287 }
7288
94af9270
KS
7289 /* These are the only languages we know how to qualify names in. */
7290 if (name != NULL
f55ee35c
JK
7291 && (cu->language == language_cplus || cu->language == language_java
7292 || cu->language == language_fortran))
94af9270
KS
7293 {
7294 if (die_needs_namespace (die, cu))
7295 {
7296 long length;
0d5cff50 7297 const char *prefix;
94af9270
KS
7298 struct ui_file *buf;
7299
7300 prefix = determine_prefix (die, cu);
7301 buf = mem_fileopen ();
7302 if (*prefix != '\0')
7303 {
f55ee35c
JK
7304 char *prefixed_name = typename_concat (NULL, prefix, name,
7305 physname, cu);
9a619af0 7306
94af9270
KS
7307 fputs_unfiltered (prefixed_name, buf);
7308 xfree (prefixed_name);
7309 }
7310 else
62d5b8da 7311 fputs_unfiltered (name, buf);
94af9270 7312
98bfdba5
PA
7313 /* Template parameters may be specified in the DIE's DW_AT_name, or
7314 as children with DW_TAG_template_type_param or
7315 DW_TAG_value_type_param. If the latter, add them to the name
7316 here. If the name already has template parameters, then
7317 skip this step; some versions of GCC emit both, and
7318 it is more efficient to use the pre-computed name.
7319
7320 Something to keep in mind about this process: it is very
7321 unlikely, or in some cases downright impossible, to produce
7322 something that will match the mangled name of a function.
7323 If the definition of the function has the same debug info,
7324 we should be able to match up with it anyway. But fallbacks
7325 using the minimal symbol, for instance to find a method
7326 implemented in a stripped copy of libstdc++, will not work.
7327 If we do not have debug info for the definition, we will have to
7328 match them up some other way.
7329
7330 When we do name matching there is a related problem with function
7331 templates; two instantiated function templates are allowed to
7332 differ only by their return types, which we do not add here. */
7333
7334 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7335 {
7336 struct attribute *attr;
7337 struct die_info *child;
7338 int first = 1;
7339
7340 die->building_fullname = 1;
7341
7342 for (child = die->child; child != NULL; child = child->sibling)
7343 {
7344 struct type *type;
12df843f 7345 LONGEST value;
98bfdba5
PA
7346 gdb_byte *bytes;
7347 struct dwarf2_locexpr_baton *baton;
7348 struct value *v;
7349
7350 if (child->tag != DW_TAG_template_type_param
7351 && child->tag != DW_TAG_template_value_param)
7352 continue;
7353
7354 if (first)
7355 {
7356 fputs_unfiltered ("<", buf);
7357 first = 0;
7358 }
7359 else
7360 fputs_unfiltered (", ", buf);
7361
7362 attr = dwarf2_attr (child, DW_AT_type, cu);
7363 if (attr == NULL)
7364 {
7365 complaint (&symfile_complaints,
7366 _("template parameter missing DW_AT_type"));
7367 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7368 continue;
7369 }
7370 type = die_type (child, cu);
7371
7372 if (child->tag == DW_TAG_template_type_param)
7373 {
79d43c61 7374 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7375 continue;
7376 }
7377
7378 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7379 if (attr == NULL)
7380 {
7381 complaint (&symfile_complaints,
3e43a32a
MS
7382 _("template parameter missing "
7383 "DW_AT_const_value"));
98bfdba5
PA
7384 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7385 continue;
7386 }
7387
7388 dwarf2_const_value_attr (attr, type, name,
7389 &cu->comp_unit_obstack, cu,
7390 &value, &bytes, &baton);
7391
7392 if (TYPE_NOSIGN (type))
7393 /* GDB prints characters as NUMBER 'CHAR'. If that's
7394 changed, this can use value_print instead. */
7395 c_printchar (value, type, buf);
7396 else
7397 {
7398 struct value_print_options opts;
7399
7400 if (baton != NULL)
7401 v = dwarf2_evaluate_loc_desc (type, NULL,
7402 baton->data,
7403 baton->size,
7404 baton->per_cu);
7405 else if (bytes != NULL)
7406 {
7407 v = allocate_value (type);
7408 memcpy (value_contents_writeable (v), bytes,
7409 TYPE_LENGTH (type));
7410 }
7411 else
7412 v = value_from_longest (type, value);
7413
3e43a32a
MS
7414 /* Specify decimal so that we do not depend on
7415 the radix. */
98bfdba5
PA
7416 get_formatted_print_options (&opts, 'd');
7417 opts.raw = 1;
7418 value_print (v, buf, &opts);
7419 release_value (v);
7420 value_free (v);
7421 }
7422 }
7423
7424 die->building_fullname = 0;
7425
7426 if (!first)
7427 {
7428 /* Close the argument list, with a space if necessary
7429 (nested templates). */
7430 char last_char = '\0';
7431 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7432 if (last_char == '>')
7433 fputs_unfiltered (" >", buf);
7434 else
7435 fputs_unfiltered (">", buf);
7436 }
7437 }
7438
94af9270
KS
7439 /* For Java and C++ methods, append formal parameter type
7440 information, if PHYSNAME. */
6e70227d 7441
94af9270
KS
7442 if (physname && die->tag == DW_TAG_subprogram
7443 && (cu->language == language_cplus
7444 || cu->language == language_java))
7445 {
7446 struct type *type = read_type_die (die, cu);
7447
79d43c61
TT
7448 c_type_print_args (type, buf, 1, cu->language,
7449 &type_print_raw_options);
94af9270
KS
7450
7451 if (cu->language == language_java)
7452 {
7453 /* For java, we must append the return type to method
0963b4bd 7454 names. */
94af9270
KS
7455 if (die->tag == DW_TAG_subprogram)
7456 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7457 0, 0, &type_print_raw_options);
94af9270
KS
7458 }
7459 else if (cu->language == language_cplus)
7460 {
60430eff
DJ
7461 /* Assume that an artificial first parameter is
7462 "this", but do not crash if it is not. RealView
7463 marks unnamed (and thus unused) parameters as
7464 artificial; there is no way to differentiate
7465 the two cases. */
94af9270
KS
7466 if (TYPE_NFIELDS (type) > 0
7467 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7468 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7469 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7470 0))))
94af9270
KS
7471 fputs_unfiltered (" const", buf);
7472 }
7473 }
7474
bb5ed363 7475 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7476 &length);
7477 ui_file_delete (buf);
7478
7479 if (cu->language == language_cplus)
7480 {
7481 char *cname
7482 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7483 &objfile->objfile_obstack);
9a619af0 7484
94af9270
KS
7485 if (cname != NULL)
7486 name = cname;
7487 }
7488 }
7489 }
7490
7491 return name;
7492}
7493
0114d602
DJ
7494/* Return the fully qualified name of DIE, based on its DW_AT_name.
7495 If scope qualifiers are appropriate they will be added. The result
7496 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7497 not have a name. NAME may either be from a previous call to
7498 dwarf2_name or NULL.
7499
0963b4bd 7500 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7501
7502static const char *
94af9270 7503dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7504{
94af9270
KS
7505 return dwarf2_compute_name (name, die, cu, 0);
7506}
0114d602 7507
94af9270
KS
7508/* Construct a physname for the given DIE in CU. NAME may either be
7509 from a previous call to dwarf2_name or NULL. The result will be
7510 allocated on the objfile_objstack or NULL if the DIE does not have a
7511 name.
0114d602 7512
94af9270 7513 The output string will be canonicalized (if C++/Java). */
0114d602 7514
94af9270
KS
7515static const char *
7516dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7517{
bb5ed363 7518 struct objfile *objfile = cu->objfile;
900e11f9
JK
7519 struct attribute *attr;
7520 const char *retval, *mangled = NULL, *canon = NULL;
7521 struct cleanup *back_to;
7522 int need_copy = 1;
7523
7524 /* In this case dwarf2_compute_name is just a shortcut not building anything
7525 on its own. */
7526 if (!die_needs_namespace (die, cu))
7527 return dwarf2_compute_name (name, die, cu, 1);
7528
7529 back_to = make_cleanup (null_cleanup, NULL);
7530
7531 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7532 if (!attr)
7533 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7534
7535 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7536 has computed. */
7537 if (attr && DW_STRING (attr))
7538 {
7539 char *demangled;
7540
7541 mangled = DW_STRING (attr);
7542
7543 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7544 type. It is easier for GDB users to search for such functions as
7545 `name(params)' than `long name(params)'. In such case the minimal
7546 symbol names do not match the full symbol names but for template
7547 functions there is never a need to look up their definition from their
7548 declaration so the only disadvantage remains the minimal symbol
7549 variant `long name(params)' does not have the proper inferior type.
7550 */
7551
a766d390
DE
7552 if (cu->language == language_go)
7553 {
7554 /* This is a lie, but we already lie to the caller new_symbol_full.
7555 new_symbol_full assumes we return the mangled name.
7556 This just undoes that lie until things are cleaned up. */
7557 demangled = NULL;
7558 }
7559 else
7560 {
7561 demangled = cplus_demangle (mangled,
7562 (DMGL_PARAMS | DMGL_ANSI
7563 | (cu->language == language_java
7564 ? DMGL_JAVA | DMGL_RET_POSTFIX
7565 : DMGL_RET_DROP)));
7566 }
900e11f9
JK
7567 if (demangled)
7568 {
7569 make_cleanup (xfree, demangled);
7570 canon = demangled;
7571 }
7572 else
7573 {
7574 canon = mangled;
7575 need_copy = 0;
7576 }
7577 }
7578
7579 if (canon == NULL || check_physname)
7580 {
7581 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7582
7583 if (canon != NULL && strcmp (physname, canon) != 0)
7584 {
7585 /* It may not mean a bug in GDB. The compiler could also
7586 compute DW_AT_linkage_name incorrectly. But in such case
7587 GDB would need to be bug-to-bug compatible. */
7588
7589 complaint (&symfile_complaints,
7590 _("Computed physname <%s> does not match demangled <%s> "
7591 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7592 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7593
7594 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7595 is available here - over computed PHYSNAME. It is safer
7596 against both buggy GDB and buggy compilers. */
7597
7598 retval = canon;
7599 }
7600 else
7601 {
7602 retval = physname;
7603 need_copy = 0;
7604 }
7605 }
7606 else
7607 retval = canon;
7608
7609 if (need_copy)
7610 retval = obsavestring (retval, strlen (retval),
bb5ed363 7611 &objfile->objfile_obstack);
900e11f9
JK
7612
7613 do_cleanups (back_to);
7614 return retval;
0114d602
DJ
7615}
7616
27aa8d6a
SW
7617/* Read the import statement specified by the given die and record it. */
7618
7619static void
7620read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7621{
bb5ed363 7622 struct objfile *objfile = cu->objfile;
27aa8d6a 7623 struct attribute *import_attr;
32019081 7624 struct die_info *imported_die, *child_die;
de4affc9 7625 struct dwarf2_cu *imported_cu;
27aa8d6a 7626 const char *imported_name;
794684b6 7627 const char *imported_name_prefix;
13387711
SW
7628 const char *canonical_name;
7629 const char *import_alias;
7630 const char *imported_declaration = NULL;
794684b6 7631 const char *import_prefix;
32019081
JK
7632 VEC (const_char_ptr) *excludes = NULL;
7633 struct cleanup *cleanups;
13387711
SW
7634
7635 char *temp;
27aa8d6a
SW
7636
7637 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7638 if (import_attr == NULL)
7639 {
7640 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7641 dwarf_tag_name (die->tag));
7642 return;
7643 }
7644
de4affc9
CC
7645 imported_cu = cu;
7646 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7647 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7648 if (imported_name == NULL)
7649 {
7650 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7651
7652 The import in the following code:
7653 namespace A
7654 {
7655 typedef int B;
7656 }
7657
7658 int main ()
7659 {
7660 using A::B;
7661 B b;
7662 return b;
7663 }
7664
7665 ...
7666 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7667 <52> DW_AT_decl_file : 1
7668 <53> DW_AT_decl_line : 6
7669 <54> DW_AT_import : <0x75>
7670 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7671 <59> DW_AT_name : B
7672 <5b> DW_AT_decl_file : 1
7673 <5c> DW_AT_decl_line : 2
7674 <5d> DW_AT_type : <0x6e>
7675 ...
7676 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7677 <76> DW_AT_byte_size : 4
7678 <77> DW_AT_encoding : 5 (signed)
7679
7680 imports the wrong die ( 0x75 instead of 0x58 ).
7681 This case will be ignored until the gcc bug is fixed. */
7682 return;
7683 }
7684
82856980
SW
7685 /* Figure out the local name after import. */
7686 import_alias = dwarf2_name (die, cu);
27aa8d6a 7687
794684b6
SW
7688 /* Figure out where the statement is being imported to. */
7689 import_prefix = determine_prefix (die, cu);
7690
7691 /* Figure out what the scope of the imported die is and prepend it
7692 to the name of the imported die. */
de4affc9 7693 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7694
f55ee35c
JK
7695 if (imported_die->tag != DW_TAG_namespace
7696 && imported_die->tag != DW_TAG_module)
794684b6 7697 {
13387711
SW
7698 imported_declaration = imported_name;
7699 canonical_name = imported_name_prefix;
794684b6 7700 }
13387711 7701 else if (strlen (imported_name_prefix) > 0)
794684b6 7702 {
13387711
SW
7703 temp = alloca (strlen (imported_name_prefix)
7704 + 2 + strlen (imported_name) + 1);
7705 strcpy (temp, imported_name_prefix);
7706 strcat (temp, "::");
7707 strcat (temp, imported_name);
7708 canonical_name = temp;
794684b6 7709 }
13387711
SW
7710 else
7711 canonical_name = imported_name;
794684b6 7712
32019081
JK
7713 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7714
7715 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7716 for (child_die = die->child; child_die && child_die->tag;
7717 child_die = sibling_die (child_die))
7718 {
7719 /* DWARF-4: A Fortran use statement with a “rename list” may be
7720 represented by an imported module entry with an import attribute
7721 referring to the module and owned entries corresponding to those
7722 entities that are renamed as part of being imported. */
7723
7724 if (child_die->tag != DW_TAG_imported_declaration)
7725 {
7726 complaint (&symfile_complaints,
7727 _("child DW_TAG_imported_declaration expected "
7728 "- DIE at 0x%x [in module %s]"),
b64f50a1 7729 child_die->offset.sect_off, objfile->name);
32019081
JK
7730 continue;
7731 }
7732
7733 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7734 if (import_attr == NULL)
7735 {
7736 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7737 dwarf_tag_name (child_die->tag));
7738 continue;
7739 }
7740
7741 imported_cu = cu;
7742 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7743 &imported_cu);
7744 imported_name = dwarf2_name (imported_die, imported_cu);
7745 if (imported_name == NULL)
7746 {
7747 complaint (&symfile_complaints,
7748 _("child DW_TAG_imported_declaration has unknown "
7749 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7750 child_die->offset.sect_off, objfile->name);
32019081
JK
7751 continue;
7752 }
7753
7754 VEC_safe_push (const_char_ptr, excludes, imported_name);
7755
7756 process_die (child_die, cu);
7757 }
7758
c0cc3a76
SW
7759 cp_add_using_directive (import_prefix,
7760 canonical_name,
7761 import_alias,
13387711 7762 imported_declaration,
32019081 7763 excludes,
bb5ed363 7764 &objfile->objfile_obstack);
32019081
JK
7765
7766 do_cleanups (cleanups);
27aa8d6a
SW
7767}
7768
f4dc4d17 7769/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7770
cb1df416
DJ
7771static void
7772free_cu_line_header (void *arg)
7773{
7774 struct dwarf2_cu *cu = arg;
7775
7776 free_line_header (cu->line_header);
7777 cu->line_header = NULL;
7778}
7779
1b80a9fa
JK
7780/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7781 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7782 this, it was first present in GCC release 4.3.0. */
7783
7784static int
7785producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7786{
7787 if (!cu->checked_producer)
7788 check_producer (cu);
7789
7790 return cu->producer_is_gcc_lt_4_3;
7791}
7792
9291a0cd
TT
7793static void
7794find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7795 char **name, char **comp_dir)
7796{
7797 struct attribute *attr;
7798
7799 *name = NULL;
7800 *comp_dir = NULL;
7801
7802 /* Find the filename. Do not use dwarf2_name here, since the filename
7803 is not a source language identifier. */
7804 attr = dwarf2_attr (die, DW_AT_name, cu);
7805 if (attr)
7806 {
7807 *name = DW_STRING (attr);
7808 }
7809
7810 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7811 if (attr)
7812 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7813 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7814 && IS_ABSOLUTE_PATH (*name))
9291a0cd
TT
7815 {
7816 *comp_dir = ldirname (*name);
7817 if (*comp_dir != NULL)
7818 make_cleanup (xfree, *comp_dir);
7819 }
7820 if (*comp_dir != NULL)
7821 {
7822 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7823 directory, get rid of it. */
7824 char *cp = strchr (*comp_dir, ':');
7825
7826 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7827 *comp_dir = cp + 1;
7828 }
7829
7830 if (*name == NULL)
7831 *name = "<unknown>";
7832}
7833
f4dc4d17
DE
7834/* Handle DW_AT_stmt_list for a compilation unit.
7835 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7836 COMP_DIR is the compilation directory.
7837 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7838
7839static void
7840handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7841 const char *comp_dir)
2ab95328
TT
7842{
7843 struct attribute *attr;
2ab95328 7844
f4dc4d17
DE
7845 gdb_assert (! cu->per_cu->is_debug_types);
7846
2ab95328
TT
7847 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7848 if (attr)
7849 {
7850 unsigned int line_offset = DW_UNSND (attr);
7851 struct line_header *line_header
3019eac3 7852 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7853
7854 if (line_header)
dee91e82
DE
7855 {
7856 cu->line_header = line_header;
7857 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7858 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7859 }
2ab95328
TT
7860 }
7861}
7862
95554aad 7863/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7864
c906108c 7865static void
e7c27a73 7866read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7867{
dee91e82 7868 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7869 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7870 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7871 CORE_ADDR highpc = ((CORE_ADDR) 0);
7872 struct attribute *attr;
e1024ff1 7873 char *name = NULL;
c906108c
SS
7874 char *comp_dir = NULL;
7875 struct die_info *child_die;
7876 bfd *abfd = objfile->obfd;
e142c38c 7877 CORE_ADDR baseaddr;
6e70227d 7878
e142c38c 7879 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7880
fae299cd 7881 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7882
7883 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7884 from finish_block. */
2acceee2 7885 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7886 lowpc = highpc;
7887 lowpc += baseaddr;
7888 highpc += baseaddr;
7889
9291a0cd 7890 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7891
95554aad 7892 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7893
f4b8a18d
KW
7894 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7895 standardised yet. As a workaround for the language detection we fall
7896 back to the DW_AT_producer string. */
7897 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7898 cu->language = language_opencl;
7899
3019eac3
DE
7900 /* Similar hack for Go. */
7901 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7902 set_cu_language (DW_LANG_Go, cu);
7903
f4dc4d17 7904 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7905
7906 /* Decode line number information if present. We do this before
7907 processing child DIEs, so that the line header table is available
7908 for DW_AT_decl_file. */
f4dc4d17 7909 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7910
7911 /* Process all dies in compilation unit. */
7912 if (die->child != NULL)
7913 {
7914 child_die = die->child;
7915 while (child_die && child_die->tag)
7916 {
7917 process_die (child_die, cu);
7918 child_die = sibling_die (child_die);
7919 }
7920 }
7921
7922 /* Decode macro information, if present. Dwarf 2 macro information
7923 refers to information in the line number info statement program
7924 header, so we can only read it if we've read the header
7925 successfully. */
7926 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7927 if (attr && cu->line_header)
7928 {
7929 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7930 complaint (&symfile_complaints,
7931 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7932
09262596 7933 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7934 }
7935 else
7936 {
7937 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7938 if (attr && cu->line_header)
7939 {
7940 unsigned int macro_offset = DW_UNSND (attr);
7941
09262596 7942 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7943 }
7944 }
7945
7946 do_cleanups (back_to);
7947}
7948
f4dc4d17
DE
7949/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7950 Create the set of symtabs used by this TU, or if this TU is sharing
7951 symtabs with another TU and the symtabs have already been created
7952 then restore those symtabs in the line header.
7953 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7954
7955static void
f4dc4d17 7956setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7957{
f4dc4d17
DE
7958 struct objfile *objfile = dwarf2_per_objfile->objfile;
7959 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7960 struct type_unit_group *tu_group;
7961 int first_time;
7962 struct line_header *lh;
3019eac3 7963 struct attribute *attr;
f4dc4d17 7964 unsigned int i, line_offset;
3019eac3 7965
f4dc4d17 7966 gdb_assert (per_cu->is_debug_types);
3019eac3 7967
f4dc4d17 7968 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 7969
f4dc4d17
DE
7970 /* If we're using .gdb_index (includes -readnow) then
7971 per_cu->s.type_unit_group may not have been set up yet. */
7972 if (per_cu->s.type_unit_group == NULL)
094b34ac 7973 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
7974 tu_group = per_cu->s.type_unit_group;
7975
7976 /* If we've already processed this stmt_list there's no real need to
7977 do it again, we could fake it and just recreate the part we need
7978 (file name,index -> symtab mapping). If data shows this optimization
7979 is useful we can do it then. */
7980 first_time = tu_group->primary_symtab == NULL;
7981
7982 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7983 debug info. */
7984 lh = NULL;
7985 if (attr != NULL)
3019eac3 7986 {
f4dc4d17
DE
7987 line_offset = DW_UNSND (attr);
7988 lh = dwarf_decode_line_header (line_offset, cu);
7989 }
7990 if (lh == NULL)
7991 {
7992 if (first_time)
7993 dwarf2_start_symtab (cu, "", NULL, 0);
7994 else
7995 {
7996 gdb_assert (tu_group->symtabs == NULL);
7997 restart_symtab (0);
7998 }
7999 /* Note: The primary symtab will get allocated at the end. */
8000 return;
3019eac3
DE
8001 }
8002
f4dc4d17
DE
8003 cu->line_header = lh;
8004 make_cleanup (free_cu_line_header, cu);
3019eac3 8005
f4dc4d17
DE
8006 if (first_time)
8007 {
8008 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8009
f4dc4d17
DE
8010 tu_group->num_symtabs = lh->num_file_names;
8011 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8012
f4dc4d17
DE
8013 for (i = 0; i < lh->num_file_names; ++i)
8014 {
8015 char *dir = NULL;
8016 struct file_entry *fe = &lh->file_names[i];
3019eac3 8017
f4dc4d17
DE
8018 if (fe->dir_index)
8019 dir = lh->include_dirs[fe->dir_index - 1];
8020 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8021
f4dc4d17
DE
8022 /* Note: We don't have to watch for the main subfile here, type units
8023 don't have DW_AT_name. */
3019eac3 8024
f4dc4d17
DE
8025 if (current_subfile->symtab == NULL)
8026 {
8027 /* NOTE: start_subfile will recognize when it's been passed
8028 a file it has already seen. So we can't assume there's a
8029 simple mapping from lh->file_names to subfiles,
8030 lh->file_names may contain dups. */
8031 current_subfile->symtab = allocate_symtab (current_subfile->name,
8032 objfile);
8033 }
8034
8035 fe->symtab = current_subfile->symtab;
8036 tu_group->symtabs[i] = fe->symtab;
8037 }
8038 }
8039 else
3019eac3 8040 {
f4dc4d17
DE
8041 restart_symtab (0);
8042
8043 for (i = 0; i < lh->num_file_names; ++i)
8044 {
8045 struct file_entry *fe = &lh->file_names[i];
8046
8047 fe->symtab = tu_group->symtabs[i];
8048 }
3019eac3
DE
8049 }
8050
f4dc4d17
DE
8051 /* The main symtab is allocated last. Type units don't have DW_AT_name
8052 so they don't have a "real" (so to speak) symtab anyway.
8053 There is later code that will assign the main symtab to all symbols
8054 that don't have one. We need to handle the case of a symbol with a
8055 missing symtab (DW_AT_decl_file) anyway. */
8056}
3019eac3 8057
f4dc4d17
DE
8058/* Process DW_TAG_type_unit.
8059 For TUs we want to skip the first top level sibling if it's not the
8060 actual type being defined by this TU. In this case the first top
8061 level sibling is there to provide context only. */
3019eac3 8062
f4dc4d17
DE
8063static void
8064read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8065{
8066 struct die_info *child_die;
3019eac3 8067
f4dc4d17
DE
8068 prepare_one_comp_unit (cu, die, language_minimal);
8069
8070 /* Initialize (or reinitialize) the machinery for building symtabs.
8071 We do this before processing child DIEs, so that the line header table
8072 is available for DW_AT_decl_file. */
8073 setup_type_unit_groups (die, cu);
8074
8075 if (die->child != NULL)
8076 {
8077 child_die = die->child;
8078 while (child_die && child_die->tag)
8079 {
8080 process_die (child_die, cu);
8081 child_die = sibling_die (child_die);
8082 }
8083 }
3019eac3
DE
8084}
8085\f
80626a55
DE
8086/* DWO/DWP files.
8087
8088 http://gcc.gnu.org/wiki/DebugFission
8089 http://gcc.gnu.org/wiki/DebugFissionDWP
8090
8091 To simplify handling of both DWO files ("object" files with the DWARF info)
8092 and DWP files (a file with the DWOs packaged up into one file), we treat
8093 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8094
8095static hashval_t
8096hash_dwo_file (const void *item)
8097{
8098 const struct dwo_file *dwo_file = item;
8099
80626a55 8100 return htab_hash_string (dwo_file->name);
3019eac3
DE
8101}
8102
8103static int
8104eq_dwo_file (const void *item_lhs, const void *item_rhs)
8105{
8106 const struct dwo_file *lhs = item_lhs;
8107 const struct dwo_file *rhs = item_rhs;
8108
80626a55 8109 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8110}
8111
8112/* Allocate a hash table for DWO files. */
8113
8114static htab_t
8115allocate_dwo_file_hash_table (void)
8116{
8117 struct objfile *objfile = dwarf2_per_objfile->objfile;
8118
8119 return htab_create_alloc_ex (41,
8120 hash_dwo_file,
8121 eq_dwo_file,
8122 NULL,
8123 &objfile->objfile_obstack,
8124 hashtab_obstack_allocate,
8125 dummy_obstack_deallocate);
8126}
8127
80626a55
DE
8128/* Lookup DWO file DWO_NAME. */
8129
8130static void **
8131lookup_dwo_file_slot (const char *dwo_name)
8132{
8133 struct dwo_file find_entry;
8134 void **slot;
8135
8136 if (dwarf2_per_objfile->dwo_files == NULL)
8137 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8138
8139 memset (&find_entry, 0, sizeof (find_entry));
8140 find_entry.name = dwo_name;
8141 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8142
8143 return slot;
8144}
8145
3019eac3
DE
8146static hashval_t
8147hash_dwo_unit (const void *item)
8148{
8149 const struct dwo_unit *dwo_unit = item;
8150
8151 /* This drops the top 32 bits of the id, but is ok for a hash. */
8152 return dwo_unit->signature;
8153}
8154
8155static int
8156eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8157{
8158 const struct dwo_unit *lhs = item_lhs;
8159 const struct dwo_unit *rhs = item_rhs;
8160
8161 /* The signature is assumed to be unique within the DWO file.
8162 So while object file CU dwo_id's always have the value zero,
8163 that's OK, assuming each object file DWO file has only one CU,
8164 and that's the rule for now. */
8165 return lhs->signature == rhs->signature;
8166}
8167
8168/* Allocate a hash table for DWO CUs,TUs.
8169 There is one of these tables for each of CUs,TUs for each DWO file. */
8170
8171static htab_t
8172allocate_dwo_unit_table (struct objfile *objfile)
8173{
8174 /* Start out with a pretty small number.
8175 Generally DWO files contain only one CU and maybe some TUs. */
8176 return htab_create_alloc_ex (3,
8177 hash_dwo_unit,
8178 eq_dwo_unit,
8179 NULL,
8180 &objfile->objfile_obstack,
8181 hashtab_obstack_allocate,
8182 dummy_obstack_deallocate);
8183}
8184
80626a55 8185/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8186
8187struct create_dwo_info_table_data
8188{
8189 struct dwo_file *dwo_file;
8190 htab_t cu_htab;
8191};
8192
80626a55 8193/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8194
8195static void
80626a55
DE
8196create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8197 gdb_byte *info_ptr,
8198 struct die_info *comp_unit_die,
8199 int has_children,
8200 void *datap)
3019eac3
DE
8201{
8202 struct dwarf2_cu *cu = reader->cu;
8203 struct objfile *objfile = dwarf2_per_objfile->objfile;
8204 sect_offset offset = cu->per_cu->offset;
8205 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8206 struct create_dwo_info_table_data *data = datap;
8207 struct dwo_file *dwo_file = data->dwo_file;
8208 htab_t cu_htab = data->cu_htab;
8209 void **slot;
8210 struct attribute *attr;
8211 struct dwo_unit *dwo_unit;
8212
8213 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8214 if (attr == NULL)
8215 {
8216 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8217 " its dwo_id [in module %s]"),
80626a55 8218 offset.sect_off, dwo_file->name);
3019eac3
DE
8219 return;
8220 }
8221
8222 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8223 dwo_unit->dwo_file = dwo_file;
8224 dwo_unit->signature = DW_UNSND (attr);
8225 dwo_unit->info_or_types_section = section;
8226 dwo_unit->offset = offset;
8227 dwo_unit->length = cu->per_cu->length;
8228
8229 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8230 gdb_assert (slot != NULL);
8231 if (*slot != NULL)
8232 {
8233 const struct dwo_unit *dup_dwo_unit = *slot;
8234
8235 complaint (&symfile_complaints,
8236 _("debug entry at offset 0x%x is duplicate to the entry at"
8237 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8238 offset.sect_off, dup_dwo_unit->offset.sect_off,
8239 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8240 dwo_file->name);
3019eac3
DE
8241 }
8242 else
8243 *slot = dwo_unit;
8244
09406207 8245 if (dwarf2_read_debug)
3019eac3
DE
8246 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8247 offset.sect_off,
8248 phex (dwo_unit->signature,
8249 sizeof (dwo_unit->signature)));
8250}
8251
80626a55
DE
8252/* Create a hash table to map DWO IDs to their CU entry in
8253 .debug_info.dwo in DWO_FILE.
8254 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8255
8256static htab_t
80626a55 8257create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8258{
8259 struct objfile *objfile = dwarf2_per_objfile->objfile;
8260 struct dwarf2_section_info *section = &dwo_file->sections.info;
8261 bfd *abfd;
8262 htab_t cu_htab;
8263 gdb_byte *info_ptr, *end_ptr;
8264 struct create_dwo_info_table_data create_dwo_info_table_data;
8265
8266 dwarf2_read_section (objfile, section);
8267 info_ptr = section->buffer;
8268
8269 if (info_ptr == NULL)
8270 return NULL;
8271
8272 /* We can't set abfd until now because the section may be empty or
8273 not present, in which case section->asection will be NULL. */
8274 abfd = section->asection->owner;
8275
09406207 8276 if (dwarf2_read_debug)
3019eac3
DE
8277 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8278 bfd_get_filename (abfd));
8279
8280 cu_htab = allocate_dwo_unit_table (objfile);
8281
8282 create_dwo_info_table_data.dwo_file = dwo_file;
8283 create_dwo_info_table_data.cu_htab = cu_htab;
8284
8285 end_ptr = info_ptr + section->size;
8286 while (info_ptr < end_ptr)
8287 {
8288 struct dwarf2_per_cu_data per_cu;
8289
8290 memset (&per_cu, 0, sizeof (per_cu));
8291 per_cu.objfile = objfile;
8292 per_cu.is_debug_types = 0;
8293 per_cu.offset.sect_off = info_ptr - section->buffer;
8294 per_cu.info_or_types_section = section;
8295
8296 init_cutu_and_read_dies_no_follow (&per_cu,
8297 &dwo_file->sections.abbrev,
8298 dwo_file,
80626a55 8299 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8300 &create_dwo_info_table_data);
8301
8302 info_ptr += per_cu.length;
8303 }
8304
8305 return cu_htab;
8306}
8307
80626a55
DE
8308/* DWP file .debug_{cu,tu}_index section format:
8309 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8310
8311 Both index sections have the same format, and serve to map a 64-bit
8312 signature to a set of section numbers. Each section begins with a header,
8313 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8314 indexes, and a pool of 32-bit section numbers. The index sections will be
8315 aligned at 8-byte boundaries in the file.
8316
8317 The index section header contains two unsigned 32-bit values (using the
8318 byte order of the application binary):
8319
8320 N, the number of compilation units or type units in the index
8321 M, the number of slots in the hash table
8322
8323 (We assume that N and M will not exceed 2^32 - 1.)
8324
8325 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8326
8327 The hash table begins at offset 8 in the section, and consists of an array
8328 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8329 order of the application binary). Unused slots in the hash table are 0.
8330 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8331
8332 The parallel table begins immediately after the hash table
8333 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8334 array of 32-bit indexes (using the byte order of the application binary),
8335 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8336 table contains a 32-bit index into the pool of section numbers. For unused
8337 hash table slots, the corresponding entry in the parallel table will be 0.
8338
8339 Given a 64-bit compilation unit signature or a type signature S, an entry
8340 in the hash table is located as follows:
8341
8342 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8343 the low-order k bits all set to 1.
8344
8345 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8346
8347 3) If the hash table entry at index H matches the signature, use that
8348 entry. If the hash table entry at index H is unused (all zeroes),
8349 terminate the search: the signature is not present in the table.
8350
8351 4) Let H = (H + H') modulo M. Repeat at Step 3.
8352
8353 Because M > N and H' and M are relatively prime, the search is guaranteed
8354 to stop at an unused slot or find the match.
8355
8356 The pool of section numbers begins immediately following the hash table
8357 (at offset 8 + 12 * M from the beginning of the section). The pool of
8358 section numbers consists of an array of 32-bit words (using the byte order
8359 of the application binary). Each item in the array is indexed starting
8360 from 0. The hash table entry provides the index of the first section
8361 number in the set. Additional section numbers in the set follow, and the
8362 set is terminated by a 0 entry (section number 0 is not used in ELF).
8363
8364 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8365 section must be the first entry in the set, and the .debug_abbrev.dwo must
8366 be the second entry. Other members of the set may follow in any order. */
8367
8368/* Create a hash table to map DWO IDs to their CU/TU entry in
8369 .debug_{info,types}.dwo in DWP_FILE.
8370 Returns NULL if there isn't one.
8371 Note: This function processes DWP files only, not DWO files. */
8372
8373static struct dwp_hash_table *
8374create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8375{
8376 struct objfile *objfile = dwarf2_per_objfile->objfile;
8377 bfd *dbfd = dwp_file->dbfd;
8378 char *index_ptr, *index_end;
8379 struct dwarf2_section_info *index;
8380 uint32_t version, nr_units, nr_slots;
8381 struct dwp_hash_table *htab;
8382
8383 if (is_debug_types)
8384 index = &dwp_file->sections.tu_index;
8385 else
8386 index = &dwp_file->sections.cu_index;
8387
8388 if (dwarf2_section_empty_p (index))
8389 return NULL;
8390 dwarf2_read_section (objfile, index);
8391
8392 index_ptr = index->buffer;
8393 index_end = index_ptr + index->size;
8394
8395 version = read_4_bytes (dbfd, index_ptr);
8396 index_ptr += 8; /* Skip the unused word. */
8397 nr_units = read_4_bytes (dbfd, index_ptr);
8398 index_ptr += 4;
8399 nr_slots = read_4_bytes (dbfd, index_ptr);
8400 index_ptr += 4;
8401
8402 if (version != 1)
8403 {
8404 error (_("Dwarf Error: unsupported DWP file version (%u)"
8405 " [in module %s]"),
8406 version, dwp_file->name);
8407 }
8408 if (nr_slots != (nr_slots & -nr_slots))
8409 {
8410 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8411 " is not power of 2 [in module %s]"),
8412 nr_slots, dwp_file->name);
8413 }
8414
8415 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8416 htab->nr_units = nr_units;
8417 htab->nr_slots = nr_slots;
8418 htab->hash_table = index_ptr;
8419 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8420 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8421
8422 return htab;
8423}
8424
8425/* Update SECTIONS with the data from SECTP.
8426
8427 This function is like the other "locate" section routines that are
8428 passed to bfd_map_over_sections, but in this context the sections to
8429 read comes from the DWP hash table, not the full ELF section table.
8430
8431 The result is non-zero for success, or zero if an error was found. */
8432
8433static int
8434locate_virtual_dwo_sections (asection *sectp,
8435 struct virtual_dwo_sections *sections)
8436{
8437 const struct dwop_section_names *names = &dwop_section_names;
8438
8439 if (section_is_p (sectp->name, &names->abbrev_dwo))
8440 {
8441 /* There can be only one. */
8442 if (sections->abbrev.asection != NULL)
8443 return 0;
8444 sections->abbrev.asection = sectp;
8445 sections->abbrev.size = bfd_get_section_size (sectp);
8446 }
8447 else if (section_is_p (sectp->name, &names->info_dwo)
8448 || section_is_p (sectp->name, &names->types_dwo))
8449 {
8450 /* There can be only one. */
8451 if (sections->info_or_types.asection != NULL)
8452 return 0;
8453 sections->info_or_types.asection = sectp;
8454 sections->info_or_types.size = bfd_get_section_size (sectp);
8455 }
8456 else if (section_is_p (sectp->name, &names->line_dwo))
8457 {
8458 /* There can be only one. */
8459 if (sections->line.asection != NULL)
8460 return 0;
8461 sections->line.asection = sectp;
8462 sections->line.size = bfd_get_section_size (sectp);
8463 }
8464 else if (section_is_p (sectp->name, &names->loc_dwo))
8465 {
8466 /* There can be only one. */
8467 if (sections->loc.asection != NULL)
8468 return 0;
8469 sections->loc.asection = sectp;
8470 sections->loc.size = bfd_get_section_size (sectp);
8471 }
8472 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8473 {
8474 /* There can be only one. */
8475 if (sections->macinfo.asection != NULL)
8476 return 0;
8477 sections->macinfo.asection = sectp;
8478 sections->macinfo.size = bfd_get_section_size (sectp);
8479 }
8480 else if (section_is_p (sectp->name, &names->macro_dwo))
8481 {
8482 /* There can be only one. */
8483 if (sections->macro.asection != NULL)
8484 return 0;
8485 sections->macro.asection = sectp;
8486 sections->macro.size = bfd_get_section_size (sectp);
8487 }
8488 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8489 {
8490 /* There can be only one. */
8491 if (sections->str_offsets.asection != NULL)
8492 return 0;
8493 sections->str_offsets.asection = sectp;
8494 sections->str_offsets.size = bfd_get_section_size (sectp);
8495 }
8496 else
8497 {
8498 /* No other kind of section is valid. */
8499 return 0;
8500 }
8501
8502 return 1;
8503}
8504
8505/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8506 HTAB is the hash table from the DWP file.
8507 SECTION_INDEX is the index of the DWO in HTAB. */
8508
8509static struct dwo_unit *
8510create_dwo_in_dwp (struct dwp_file *dwp_file,
8511 const struct dwp_hash_table *htab,
8512 uint32_t section_index,
8513 ULONGEST signature, int is_debug_types)
8514{
8515 struct objfile *objfile = dwarf2_per_objfile->objfile;
8516 bfd *dbfd = dwp_file->dbfd;
8517 const char *kind = is_debug_types ? "TU" : "CU";
8518 struct dwo_file *dwo_file;
8519 struct dwo_unit *dwo_unit;
8520 struct virtual_dwo_sections sections;
8521 void **dwo_file_slot;
8522 char *virtual_dwo_name;
8523 struct dwarf2_section_info *cutu;
8524 struct cleanup *cleanups;
8525 int i;
8526
8527 if (dwarf2_read_debug)
8528 {
8529 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8530 kind,
8531 section_index, phex (signature, sizeof (signature)),
8532 dwp_file->name);
8533 }
8534
8535 /* Fetch the sections of this DWO.
8536 Put a limit on the number of sections we look for so that bad data
8537 doesn't cause us to loop forever. */
8538
8539#define MAX_NR_DWO_SECTIONS \
8540 (1 /* .debug_info or .debug_types */ \
8541 + 1 /* .debug_abbrev */ \
8542 + 1 /* .debug_line */ \
8543 + 1 /* .debug_loc */ \
8544 + 1 /* .debug_str_offsets */ \
8545 + 1 /* .debug_macro */ \
8546 + 1 /* .debug_macinfo */ \
8547 + 1 /* trailing zero */)
8548
8549 memset (&sections, 0, sizeof (sections));
8550 cleanups = make_cleanup (null_cleanup, 0);
8551
8552 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8553 {
8554 asection *sectp;
8555 uint32_t section_nr =
8556 read_4_bytes (dbfd,
8557 htab->section_pool
8558 + (section_index + i) * sizeof (uint32_t));
8559
8560 if (section_nr == 0)
8561 break;
8562 if (section_nr >= dwp_file->num_sections)
8563 {
8564 error (_("Dwarf Error: bad DWP hash table, section number too large"
8565 " [in module %s]"),
8566 dwp_file->name);
8567 }
8568
8569 sectp = dwp_file->elf_sections[section_nr];
8570 if (! locate_virtual_dwo_sections (sectp, &sections))
8571 {
8572 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8573 " [in module %s]"),
8574 dwp_file->name);
8575 }
8576 }
8577
8578 if (i < 2
8579 || sections.info_or_types.asection == NULL
8580 || sections.abbrev.asection == NULL)
8581 {
8582 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8583 " [in module %s]"),
8584 dwp_file->name);
8585 }
8586 if (i == MAX_NR_DWO_SECTIONS)
8587 {
8588 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8589 " [in module %s]"),
8590 dwp_file->name);
8591 }
8592
8593 /* It's easier for the rest of the code if we fake a struct dwo_file and
8594 have dwo_unit "live" in that. At least for now.
8595
8596 The DWP file can be made up of a random collection of CUs and TUs.
8597 However, for each CU + set of TUs that came from the same original
8598 DWO file, we want combine them back into a virtual DWO file to save space
8599 (fewer struct dwo_file objects to allocated). Remember that for really
8600 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8601
2792b94d
PM
8602 virtual_dwo_name =
8603 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8604 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8605 sections.line.asection ? sections.line.asection->id : 0,
8606 sections.loc.asection ? sections.loc.asection->id : 0,
8607 (sections.str_offsets.asection
8608 ? sections.str_offsets.asection->id
8609 : 0));
80626a55
DE
8610 make_cleanup (xfree, virtual_dwo_name);
8611 /* Can we use an existing virtual DWO file? */
8612 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8613 /* Create one if necessary. */
8614 if (*dwo_file_slot == NULL)
8615 {
8616 if (dwarf2_read_debug)
8617 {
8618 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8619 virtual_dwo_name);
8620 }
8621 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8622 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8623 virtual_dwo_name,
8624 strlen (virtual_dwo_name));
8625 dwo_file->sections.abbrev = sections.abbrev;
8626 dwo_file->sections.line = sections.line;
8627 dwo_file->sections.loc = sections.loc;
8628 dwo_file->sections.macinfo = sections.macinfo;
8629 dwo_file->sections.macro = sections.macro;
8630 dwo_file->sections.str_offsets = sections.str_offsets;
8631 /* The "str" section is global to the entire DWP file. */
8632 dwo_file->sections.str = dwp_file->sections.str;
8633 /* The info or types section is assigned later to dwo_unit,
8634 there's no need to record it in dwo_file.
8635 Also, we can't simply record type sections in dwo_file because
8636 we record a pointer into the vector in dwo_unit. As we collect more
8637 types we'll grow the vector and eventually have to reallocate space
8638 for it, invalidating all the pointers into the current copy. */
8639 *dwo_file_slot = dwo_file;
8640 }
8641 else
8642 {
8643 if (dwarf2_read_debug)
8644 {
8645 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8646 virtual_dwo_name);
8647 }
8648 dwo_file = *dwo_file_slot;
8649 }
8650 do_cleanups (cleanups);
8651
8652 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8653 dwo_unit->dwo_file = dwo_file;
8654 dwo_unit->signature = signature;
8655 dwo_unit->info_or_types_section =
8656 obstack_alloc (&objfile->objfile_obstack,
8657 sizeof (struct dwarf2_section_info));
8658 *dwo_unit->info_or_types_section = sections.info_or_types;
8659 /* offset, length, type_offset_in_tu are set later. */
8660
8661 return dwo_unit;
8662}
8663
8664/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8665
8666static struct dwo_unit *
8667lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8668 const struct dwp_hash_table *htab,
8669 ULONGEST signature, int is_debug_types)
8670{
8671 bfd *dbfd = dwp_file->dbfd;
8672 uint32_t mask = htab->nr_slots - 1;
8673 uint32_t hash = signature & mask;
8674 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8675 unsigned int i;
8676 void **slot;
8677 struct dwo_unit find_dwo_cu, *dwo_cu;
8678
8679 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8680 find_dwo_cu.signature = signature;
8681 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8682
8683 if (*slot != NULL)
8684 return *slot;
8685
8686 /* Use a for loop so that we don't loop forever on bad debug info. */
8687 for (i = 0; i < htab->nr_slots; ++i)
8688 {
8689 ULONGEST signature_in_table;
8690
8691 signature_in_table =
8692 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8693 if (signature_in_table == signature)
8694 {
8695 uint32_t section_index =
8696 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8697
8698 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8699 signature, is_debug_types);
8700 return *slot;
8701 }
8702 if (signature_in_table == 0)
8703 return NULL;
8704 hash = (hash + hash2) & mask;
8705 }
8706
8707 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8708 " [in module %s]"),
8709 dwp_file->name);
8710}
8711
8712/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8713 Open the file specified by FILE_NAME and hand it off to BFD for
8714 preliminary analysis. Return a newly initialized bfd *, which
8715 includes a canonicalized copy of FILE_NAME.
80626a55 8716 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8717 In case of trouble, return NULL.
8718 NOTE: This function is derived from symfile_bfd_open. */
8719
8720static bfd *
80626a55 8721try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8722{
8723 bfd *sym_bfd;
80626a55 8724 int desc, flags;
3019eac3 8725 char *absolute_name;
3019eac3 8726
80626a55
DE
8727 flags = OPF_TRY_CWD_FIRST;
8728 if (is_dwp)
8729 flags |= OPF_SEARCH_IN_PATH;
8730 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8731 O_RDONLY | O_BINARY, &absolute_name);
8732 if (desc < 0)
8733 return NULL;
8734
bb397797 8735 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8736 if (!sym_bfd)
8737 {
3019eac3
DE
8738 xfree (absolute_name);
8739 return NULL;
8740 }
a4453b7e 8741 xfree (absolute_name);
3019eac3
DE
8742 bfd_set_cacheable (sym_bfd, 1);
8743
8744 if (!bfd_check_format (sym_bfd, bfd_object))
8745 {
cbb099e8 8746 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8747 return NULL;
8748 }
8749
3019eac3
DE
8750 return sym_bfd;
8751}
8752
80626a55 8753/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8754 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8755 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8756 The result is the bfd handle of the file.
8757 If there is a problem finding or opening the file, return NULL.
8758 Upon success, the canonicalized path of the file is stored in the bfd,
8759 same as symfile_bfd_open. */
8760
8761static bfd *
80626a55 8762open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8763{
8764 bfd *abfd;
3019eac3 8765
80626a55
DE
8766 if (IS_ABSOLUTE_PATH (file_name))
8767 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8768
8769 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8770
8771 if (comp_dir != NULL)
8772 {
80626a55 8773 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8774
8775 /* NOTE: If comp_dir is a relative path, this will also try the
8776 search path, which seems useful. */
80626a55 8777 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8778 xfree (path_to_try);
8779 if (abfd != NULL)
8780 return abfd;
8781 }
8782
8783 /* That didn't work, try debug-file-directory, which, despite its name,
8784 is a list of paths. */
8785
8786 if (*debug_file_directory == '\0')
8787 return NULL;
8788
80626a55 8789 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8790}
8791
80626a55
DE
8792/* This function is mapped across the sections and remembers the offset and
8793 size of each of the DWO debugging sections we are interested in. */
8794
8795static void
8796dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8797{
8798 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8799 const struct dwop_section_names *names = &dwop_section_names;
8800
8801 if (section_is_p (sectp->name, &names->abbrev_dwo))
8802 {
8803 dwo_sections->abbrev.asection = sectp;
8804 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8805 }
8806 else if (section_is_p (sectp->name, &names->info_dwo))
8807 {
8808 dwo_sections->info.asection = sectp;
8809 dwo_sections->info.size = bfd_get_section_size (sectp);
8810 }
8811 else if (section_is_p (sectp->name, &names->line_dwo))
8812 {
8813 dwo_sections->line.asection = sectp;
8814 dwo_sections->line.size = bfd_get_section_size (sectp);
8815 }
8816 else if (section_is_p (sectp->name, &names->loc_dwo))
8817 {
8818 dwo_sections->loc.asection = sectp;
8819 dwo_sections->loc.size = bfd_get_section_size (sectp);
8820 }
8821 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8822 {
8823 dwo_sections->macinfo.asection = sectp;
8824 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8825 }
8826 else if (section_is_p (sectp->name, &names->macro_dwo))
8827 {
8828 dwo_sections->macro.asection = sectp;
8829 dwo_sections->macro.size = bfd_get_section_size (sectp);
8830 }
8831 else if (section_is_p (sectp->name, &names->str_dwo))
8832 {
8833 dwo_sections->str.asection = sectp;
8834 dwo_sections->str.size = bfd_get_section_size (sectp);
8835 }
8836 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8837 {
8838 dwo_sections->str_offsets.asection = sectp;
8839 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8840 }
8841 else if (section_is_p (sectp->name, &names->types_dwo))
8842 {
8843 struct dwarf2_section_info type_section;
8844
8845 memset (&type_section, 0, sizeof (type_section));
8846 type_section.asection = sectp;
8847 type_section.size = bfd_get_section_size (sectp);
8848 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8849 &type_section);
8850 }
8851}
8852
8853/* Initialize the use of the DWO file specified by DWO_NAME.
8854 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8855
8856static struct dwo_file *
80626a55 8857open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8858{
8859 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8860 struct dwo_file *dwo_file;
8861 bfd *dbfd;
3019eac3
DE
8862 struct cleanup *cleanups;
8863
80626a55
DE
8864 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8865 if (dbfd == NULL)
8866 {
8867 if (dwarf2_read_debug)
8868 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8869 return NULL;
8870 }
8871 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8872 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8873 dwo_name, strlen (dwo_name));
8874 dwo_file->dbfd = dbfd;
3019eac3
DE
8875
8876 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8877
80626a55 8878 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8879
80626a55 8880 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8881
8882 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8883 dwo_file->sections.types);
8884
8885 discard_cleanups (cleanups);
8886
80626a55
DE
8887 if (dwarf2_read_debug)
8888 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8889
3019eac3
DE
8890 return dwo_file;
8891}
8892
80626a55
DE
8893/* This function is mapped across the sections and remembers the offset and
8894 size of each of the DWP debugging sections we are interested in. */
3019eac3 8895
80626a55
DE
8896static void
8897dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8898{
80626a55
DE
8899 struct dwp_file *dwp_file = dwp_file_ptr;
8900 const struct dwop_section_names *names = &dwop_section_names;
8901 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8902
80626a55
DE
8903 /* Record the ELF section number for later lookup: this is what the
8904 .debug_cu_index,.debug_tu_index tables use. */
8905 gdb_assert (elf_section_nr < dwp_file->num_sections);
8906 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8907
80626a55
DE
8908 /* Look for specific sections that we need. */
8909 if (section_is_p (sectp->name, &names->str_dwo))
8910 {
8911 dwp_file->sections.str.asection = sectp;
8912 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8913 }
8914 else if (section_is_p (sectp->name, &names->cu_index))
8915 {
8916 dwp_file->sections.cu_index.asection = sectp;
8917 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8918 }
8919 else if (section_is_p (sectp->name, &names->tu_index))
8920 {
8921 dwp_file->sections.tu_index.asection = sectp;
8922 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8923 }
8924}
3019eac3 8925
80626a55 8926/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8927
80626a55
DE
8928static hashval_t
8929hash_dwp_loaded_cutus (const void *item)
8930{
8931 const struct dwo_unit *dwo_unit = item;
3019eac3 8932
80626a55
DE
8933 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8934 return dwo_unit->signature;
3019eac3
DE
8935}
8936
80626a55 8937/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 8938
80626a55
DE
8939static int
8940eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 8941{
80626a55
DE
8942 const struct dwo_unit *dua = a;
8943 const struct dwo_unit *dub = b;
3019eac3 8944
80626a55
DE
8945 return dua->signature == dub->signature;
8946}
3019eac3 8947
80626a55 8948/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 8949
80626a55
DE
8950static htab_t
8951allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8952{
8953 return htab_create_alloc_ex (3,
8954 hash_dwp_loaded_cutus,
8955 eq_dwp_loaded_cutus,
8956 NULL,
8957 &objfile->objfile_obstack,
8958 hashtab_obstack_allocate,
8959 dummy_obstack_deallocate);
8960}
3019eac3 8961
80626a55
DE
8962/* Initialize the use of the DWP file for the current objfile.
8963 By convention the name of the DWP file is ${objfile}.dwp.
8964 The result is NULL if it can't be found. */
a766d390 8965
80626a55
DE
8966static struct dwp_file *
8967open_and_init_dwp_file (const char *comp_dir)
8968{
8969 struct objfile *objfile = dwarf2_per_objfile->objfile;
8970 struct dwp_file *dwp_file;
8971 char *dwp_name;
8972 bfd *dbfd;
8973 struct cleanup *cleanups;
8974
2792b94d 8975 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
8976 cleanups = make_cleanup (xfree, dwp_name);
8977
8978 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8979 if (dbfd == NULL)
8980 {
8981 if (dwarf2_read_debug)
8982 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8983 do_cleanups (cleanups);
8984 return NULL;
3019eac3 8985 }
80626a55
DE
8986 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
8987 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
8988 dwp_name, strlen (dwp_name));
8989 dwp_file->dbfd = dbfd;
8990 do_cleanups (cleanups);
c906108c 8991
80626a55 8992 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 8993
80626a55
DE
8994 /* +1: section 0 is unused */
8995 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
8996 dwp_file->elf_sections =
8997 OBSTACK_CALLOC (&objfile->objfile_obstack,
8998 dwp_file->num_sections, asection *);
8999
9000 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9001
9002 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9003
9004 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9005
9006 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9007
9008 discard_cleanups (cleanups);
9009
9010 if (dwarf2_read_debug)
9011 {
9012 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9013 fprintf_unfiltered (gdb_stdlog,
9014 " %u CUs, %u TUs\n",
9015 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9016 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9017 }
9018
9019 return dwp_file;
3019eac3 9020}
c906108c 9021
80626a55
DE
9022/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9023 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9024 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9025 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9026 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9027
9028 This is called, for example, when wanting to read a variable with a
9029 complex location. Therefore we don't want to do file i/o for every call.
9030 Therefore we don't want to look for a DWO file on every call.
9031 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9032 then we check if we've already seen DWO_NAME, and only THEN do we check
9033 for a DWO file.
9034
1c658ad5 9035 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9036 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9037
3019eac3 9038static struct dwo_unit *
80626a55
DE
9039lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9040 const char *dwo_name, const char *comp_dir,
9041 ULONGEST signature, int is_debug_types)
3019eac3
DE
9042{
9043 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9044 const char *kind = is_debug_types ? "TU" : "CU";
9045 void **dwo_file_slot;
3019eac3 9046 struct dwo_file *dwo_file;
80626a55 9047 struct dwp_file *dwp_file;
cb1df416 9048
80626a55 9049 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9050
80626a55
DE
9051 if (! dwarf2_per_objfile->dwp_checked)
9052 {
9053 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9054 dwarf2_per_objfile->dwp_checked = 1;
9055 }
9056 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9057
80626a55 9058 if (dwp_file != NULL)
cf2c3c16 9059 {
80626a55
DE
9060 const struct dwp_hash_table *dwp_htab =
9061 is_debug_types ? dwp_file->tus : dwp_file->cus;
9062
9063 if (dwp_htab != NULL)
9064 {
9065 struct dwo_unit *dwo_cutu =
9066 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9067
9068 if (dwo_cutu != NULL)
9069 {
9070 if (dwarf2_read_debug)
9071 {
9072 fprintf_unfiltered (gdb_stdlog,
9073 "Virtual DWO %s %s found: @%s\n",
9074 kind, hex_string (signature),
9075 host_address_to_string (dwo_cutu));
9076 }
9077 return dwo_cutu;
9078 }
9079 }
9080 }
9081
9082 /* Have we already seen DWO_NAME? */
9083
9084 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9085 if (*dwo_file_slot == NULL)
9086 {
9087 /* Read in the file and build a table of the DWOs it contains. */
9088 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9089 }
9090 /* NOTE: This will be NULL if unable to open the file. */
9091 dwo_file = *dwo_file_slot;
9092
9093 if (dwo_file != NULL)
9094 {
9095 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9096
9097 if (htab != NULL)
9098 {
9099 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9100
80626a55
DE
9101 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9102 find_dwo_cutu.signature = signature;
9103 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9104
80626a55
DE
9105 if (dwo_cutu != NULL)
9106 {
9107 if (dwarf2_read_debug)
9108 {
9109 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9110 kind, dwo_name, hex_string (signature),
9111 host_address_to_string (dwo_cutu));
9112 }
9113 return dwo_cutu;
9114 }
9115 }
2e276125 9116 }
9cdd5dbd 9117
80626a55
DE
9118 /* We didn't find it. This could mean a dwo_id mismatch, or
9119 someone deleted the DWO/DWP file, or the search path isn't set up
9120 correctly to find the file. */
9121
9122 if (dwarf2_read_debug)
9123 {
9124 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9125 kind, dwo_name, hex_string (signature));
9126 }
3019eac3
DE
9127
9128 complaint (&symfile_complaints,
80626a55 9129 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9130 " [in module %s]"),
80626a55 9131 this_unit->offset.sect_off, objfile->name);
3019eac3 9132 return NULL;
5fb290d7
DJ
9133}
9134
80626a55
DE
9135/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9136 See lookup_dwo_cutu_unit for details. */
9137
9138static struct dwo_unit *
9139lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9140 const char *dwo_name, const char *comp_dir,
9141 ULONGEST signature)
9142{
9143 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9144}
9145
9146/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9147 See lookup_dwo_cutu_unit for details. */
9148
9149static struct dwo_unit *
9150lookup_dwo_type_unit (struct signatured_type *this_tu,
9151 const char *dwo_name, const char *comp_dir)
9152{
9153 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9154}
9155
3019eac3
DE
9156/* Free all resources associated with DWO_FILE.
9157 Close the DWO file and munmap the sections.
9158 All memory should be on the objfile obstack. */
348e048f
DE
9159
9160static void
3019eac3 9161free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9162{
3019eac3
DE
9163 int ix;
9164 struct dwarf2_section_info *section;
348e048f 9165
80626a55
DE
9166 gdb_assert (dwo_file->dbfd != objfile->obfd);
9167 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9168
3019eac3
DE
9169 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9170}
348e048f 9171
3019eac3 9172/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9173
3019eac3
DE
9174static void
9175free_dwo_file_cleanup (void *arg)
9176{
9177 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9178 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9179
3019eac3
DE
9180 free_dwo_file (dwo_file, objfile);
9181}
348e048f 9182
3019eac3 9183/* Traversal function for free_dwo_files. */
2ab95328 9184
3019eac3
DE
9185static int
9186free_dwo_file_from_slot (void **slot, void *info)
9187{
9188 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9189 struct objfile *objfile = (struct objfile *) info;
348e048f 9190
3019eac3 9191 free_dwo_file (dwo_file, objfile);
348e048f 9192
3019eac3
DE
9193 return 1;
9194}
348e048f 9195
3019eac3 9196/* Free all resources associated with DWO_FILES. */
348e048f 9197
3019eac3
DE
9198static void
9199free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9200{
9201 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9202}
3019eac3
DE
9203\f
9204/* Read in various DIEs. */
348e048f 9205
d389af10
JK
9206/* qsort helper for inherit_abstract_dies. */
9207
9208static int
9209unsigned_int_compar (const void *ap, const void *bp)
9210{
9211 unsigned int a = *(unsigned int *) ap;
9212 unsigned int b = *(unsigned int *) bp;
9213
9214 return (a > b) - (b > a);
9215}
9216
9217/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9218 Inherit only the children of the DW_AT_abstract_origin DIE not being
9219 already referenced by DW_AT_abstract_origin from the children of the
9220 current DIE. */
d389af10
JK
9221
9222static void
9223inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9224{
9225 struct die_info *child_die;
9226 unsigned die_children_count;
9227 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9228 sect_offset *offsets;
9229 sect_offset *offsets_end, *offsetp;
d389af10
JK
9230 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9231 struct die_info *origin_die;
9232 /* Iterator of the ORIGIN_DIE children. */
9233 struct die_info *origin_child_die;
9234 struct cleanup *cleanups;
9235 struct attribute *attr;
cd02d79d
PA
9236 struct dwarf2_cu *origin_cu;
9237 struct pending **origin_previous_list_in_scope;
d389af10
JK
9238
9239 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9240 if (!attr)
9241 return;
9242
cd02d79d
PA
9243 /* Note that following die references may follow to a die in a
9244 different cu. */
9245
9246 origin_cu = cu;
9247 origin_die = follow_die_ref (die, attr, &origin_cu);
9248
9249 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9250 symbols in. */
9251 origin_previous_list_in_scope = origin_cu->list_in_scope;
9252 origin_cu->list_in_scope = cu->list_in_scope;
9253
edb3359d
DJ
9254 if (die->tag != origin_die->tag
9255 && !(die->tag == DW_TAG_inlined_subroutine
9256 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9257 complaint (&symfile_complaints,
9258 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9259 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9260
9261 child_die = die->child;
9262 die_children_count = 0;
9263 while (child_die && child_die->tag)
9264 {
9265 child_die = sibling_die (child_die);
9266 die_children_count++;
9267 }
9268 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9269 cleanups = make_cleanup (xfree, offsets);
9270
9271 offsets_end = offsets;
9272 child_die = die->child;
9273 while (child_die && child_die->tag)
9274 {
c38f313d
DJ
9275 /* For each CHILD_DIE, find the corresponding child of
9276 ORIGIN_DIE. If there is more than one layer of
9277 DW_AT_abstract_origin, follow them all; there shouldn't be,
9278 but GCC versions at least through 4.4 generate this (GCC PR
9279 40573). */
9280 struct die_info *child_origin_die = child_die;
cd02d79d 9281 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9282
c38f313d
DJ
9283 while (1)
9284 {
cd02d79d
PA
9285 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9286 child_origin_cu);
c38f313d
DJ
9287 if (attr == NULL)
9288 break;
cd02d79d
PA
9289 child_origin_die = follow_die_ref (child_origin_die, attr,
9290 &child_origin_cu);
c38f313d
DJ
9291 }
9292
d389af10
JK
9293 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9294 counterpart may exist. */
c38f313d 9295 if (child_origin_die != child_die)
d389af10 9296 {
edb3359d
DJ
9297 if (child_die->tag != child_origin_die->tag
9298 && !(child_die->tag == DW_TAG_inlined_subroutine
9299 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9300 complaint (&symfile_complaints,
9301 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9302 "different tags"), child_die->offset.sect_off,
9303 child_origin_die->offset.sect_off);
c38f313d
DJ
9304 if (child_origin_die->parent != origin_die)
9305 complaint (&symfile_complaints,
9306 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9307 "different parents"), child_die->offset.sect_off,
9308 child_origin_die->offset.sect_off);
c38f313d
DJ
9309 else
9310 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9311 }
9312 child_die = sibling_die (child_die);
9313 }
9314 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9315 unsigned_int_compar);
9316 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9317 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9318 complaint (&symfile_complaints,
9319 _("Multiple children of DIE 0x%x refer "
9320 "to DIE 0x%x as their abstract origin"),
b64f50a1 9321 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9322
9323 offsetp = offsets;
9324 origin_child_die = origin_die->child;
9325 while (origin_child_die && origin_child_die->tag)
9326 {
9327 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9328 while (offsetp < offsets_end
9329 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9330 offsetp++;
b64f50a1
JK
9331 if (offsetp >= offsets_end
9332 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9333 {
9334 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9335 process_die (origin_child_die, origin_cu);
d389af10
JK
9336 }
9337 origin_child_die = sibling_die (origin_child_die);
9338 }
cd02d79d 9339 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9340
9341 do_cleanups (cleanups);
9342}
9343
c906108c 9344static void
e7c27a73 9345read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9346{
e7c27a73 9347 struct objfile *objfile = cu->objfile;
52f0bd74 9348 struct context_stack *new;
c906108c
SS
9349 CORE_ADDR lowpc;
9350 CORE_ADDR highpc;
9351 struct die_info *child_die;
edb3359d 9352 struct attribute *attr, *call_line, *call_file;
c906108c 9353 char *name;
e142c38c 9354 CORE_ADDR baseaddr;
801e3a5b 9355 struct block *block;
edb3359d 9356 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9357 VEC (symbolp) *template_args = NULL;
9358 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9359
9360 if (inlined_func)
9361 {
9362 /* If we do not have call site information, we can't show the
9363 caller of this inlined function. That's too confusing, so
9364 only use the scope for local variables. */
9365 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9366 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9367 if (call_line == NULL || call_file == NULL)
9368 {
9369 read_lexical_block_scope (die, cu);
9370 return;
9371 }
9372 }
c906108c 9373
e142c38c
DJ
9374 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9375
94af9270 9376 name = dwarf2_name (die, cu);
c906108c 9377
e8d05480
JB
9378 /* Ignore functions with missing or empty names. These are actually
9379 illegal according to the DWARF standard. */
9380 if (name == NULL)
9381 {
9382 complaint (&symfile_complaints,
b64f50a1
JK
9383 _("missing name for subprogram DIE at %d"),
9384 die->offset.sect_off);
e8d05480
JB
9385 return;
9386 }
9387
9388 /* Ignore functions with missing or invalid low and high pc attributes. */
9389 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9390 {
ae4d0c03
PM
9391 attr = dwarf2_attr (die, DW_AT_external, cu);
9392 if (!attr || !DW_UNSND (attr))
9393 complaint (&symfile_complaints,
3e43a32a
MS
9394 _("cannot get low and high bounds "
9395 "for subprogram DIE at %d"),
b64f50a1 9396 die->offset.sect_off);
e8d05480
JB
9397 return;
9398 }
c906108c
SS
9399
9400 lowpc += baseaddr;
9401 highpc += baseaddr;
9402
34eaf542
TT
9403 /* If we have any template arguments, then we must allocate a
9404 different sort of symbol. */
9405 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9406 {
9407 if (child_die->tag == DW_TAG_template_type_param
9408 || child_die->tag == DW_TAG_template_value_param)
9409 {
9410 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9411 struct template_symbol);
9412 templ_func->base.is_cplus_template_function = 1;
9413 break;
9414 }
9415 }
9416
c906108c 9417 new = push_context (0, lowpc);
34eaf542
TT
9418 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9419 (struct symbol *) templ_func);
4c2df51b 9420
4cecd739
DJ
9421 /* If there is a location expression for DW_AT_frame_base, record
9422 it. */
e142c38c 9423 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9424 if (attr)
c034e007
AC
9425 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9426 expression is being recorded directly in the function's symbol
9427 and not in a separate frame-base object. I guess this hack is
9428 to avoid adding some sort of frame-base adjunct/annex to the
9429 function's symbol :-(. The problem with doing this is that it
9430 results in a function symbol with a location expression that
9431 has nothing to do with the location of the function, ouch! The
9432 relationship should be: a function's symbol has-a frame base; a
9433 frame-base has-a location expression. */
e7c27a73 9434 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9435
e142c38c 9436 cu->list_in_scope = &local_symbols;
c906108c 9437
639d11d3 9438 if (die->child != NULL)
c906108c 9439 {
639d11d3 9440 child_die = die->child;
c906108c
SS
9441 while (child_die && child_die->tag)
9442 {
34eaf542
TT
9443 if (child_die->tag == DW_TAG_template_type_param
9444 || child_die->tag == DW_TAG_template_value_param)
9445 {
9446 struct symbol *arg = new_symbol (child_die, NULL, cu);
9447
f1078f66
DJ
9448 if (arg != NULL)
9449 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9450 }
9451 else
9452 process_die (child_die, cu);
c906108c
SS
9453 child_die = sibling_die (child_die);
9454 }
9455 }
9456
d389af10
JK
9457 inherit_abstract_dies (die, cu);
9458
4a811a97
UW
9459 /* If we have a DW_AT_specification, we might need to import using
9460 directives from the context of the specification DIE. See the
9461 comment in determine_prefix. */
9462 if (cu->language == language_cplus
9463 && dwarf2_attr (die, DW_AT_specification, cu))
9464 {
9465 struct dwarf2_cu *spec_cu = cu;
9466 struct die_info *spec_die = die_specification (die, &spec_cu);
9467
9468 while (spec_die)
9469 {
9470 child_die = spec_die->child;
9471 while (child_die && child_die->tag)
9472 {
9473 if (child_die->tag == DW_TAG_imported_module)
9474 process_die (child_die, spec_cu);
9475 child_die = sibling_die (child_die);
9476 }
9477
9478 /* In some cases, GCC generates specification DIEs that
9479 themselves contain DW_AT_specification attributes. */
9480 spec_die = die_specification (spec_die, &spec_cu);
9481 }
9482 }
9483
c906108c
SS
9484 new = pop_context ();
9485 /* Make a block for the local symbols within. */
801e3a5b
JB
9486 block = finish_block (new->name, &local_symbols, new->old_blocks,
9487 lowpc, highpc, objfile);
9488
df8a16a1 9489 /* For C++, set the block's scope. */
f55ee35c 9490 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9491 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9492 determine_prefix (die, cu),
df8a16a1
DJ
9493 processing_has_namespace_info);
9494
801e3a5b
JB
9495 /* If we have address ranges, record them. */
9496 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9497
34eaf542
TT
9498 /* Attach template arguments to function. */
9499 if (! VEC_empty (symbolp, template_args))
9500 {
9501 gdb_assert (templ_func != NULL);
9502
9503 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9504 templ_func->template_arguments
9505 = obstack_alloc (&objfile->objfile_obstack,
9506 (templ_func->n_template_arguments
9507 * sizeof (struct symbol *)));
9508 memcpy (templ_func->template_arguments,
9509 VEC_address (symbolp, template_args),
9510 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9511 VEC_free (symbolp, template_args);
9512 }
9513
208d8187
JB
9514 /* In C++, we can have functions nested inside functions (e.g., when
9515 a function declares a class that has methods). This means that
9516 when we finish processing a function scope, we may need to go
9517 back to building a containing block's symbol lists. */
9518 local_symbols = new->locals;
27aa8d6a 9519 using_directives = new->using_directives;
208d8187 9520
921e78cf
JB
9521 /* If we've finished processing a top-level function, subsequent
9522 symbols go in the file symbol list. */
9523 if (outermost_context_p ())
e142c38c 9524 cu->list_in_scope = &file_symbols;
c906108c
SS
9525}
9526
9527/* Process all the DIES contained within a lexical block scope. Start
9528 a new scope, process the dies, and then close the scope. */
9529
9530static void
e7c27a73 9531read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9532{
e7c27a73 9533 struct objfile *objfile = cu->objfile;
52f0bd74 9534 struct context_stack *new;
c906108c
SS
9535 CORE_ADDR lowpc, highpc;
9536 struct die_info *child_die;
e142c38c
DJ
9537 CORE_ADDR baseaddr;
9538
9539 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9540
9541 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9542 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9543 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9544 be nasty. Might be easier to properly extend generic blocks to
af34e669 9545 describe ranges. */
d85a05f0 9546 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9547 return;
9548 lowpc += baseaddr;
9549 highpc += baseaddr;
9550
9551 push_context (0, lowpc);
639d11d3 9552 if (die->child != NULL)
c906108c 9553 {
639d11d3 9554 child_die = die->child;
c906108c
SS
9555 while (child_die && child_die->tag)
9556 {
e7c27a73 9557 process_die (child_die, cu);
c906108c
SS
9558 child_die = sibling_die (child_die);
9559 }
9560 }
9561 new = pop_context ();
9562
8540c487 9563 if (local_symbols != NULL || using_directives != NULL)
c906108c 9564 {
801e3a5b
JB
9565 struct block *block
9566 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9567 highpc, objfile);
9568
9569 /* Note that recording ranges after traversing children, as we
9570 do here, means that recording a parent's ranges entails
9571 walking across all its children's ranges as they appear in
9572 the address map, which is quadratic behavior.
9573
9574 It would be nicer to record the parent's ranges before
9575 traversing its children, simply overriding whatever you find
9576 there. But since we don't even decide whether to create a
9577 block until after we've traversed its children, that's hard
9578 to do. */
9579 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9580 }
9581 local_symbols = new->locals;
27aa8d6a 9582 using_directives = new->using_directives;
c906108c
SS
9583}
9584
96408a79
SA
9585/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9586
9587static void
9588read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9589{
9590 struct objfile *objfile = cu->objfile;
9591 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9592 CORE_ADDR pc, baseaddr;
9593 struct attribute *attr;
9594 struct call_site *call_site, call_site_local;
9595 void **slot;
9596 int nparams;
9597 struct die_info *child_die;
9598
9599 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9600
9601 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9602 if (!attr)
9603 {
9604 complaint (&symfile_complaints,
9605 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9606 "DIE 0x%x [in module %s]"),
b64f50a1 9607 die->offset.sect_off, objfile->name);
96408a79
SA
9608 return;
9609 }
9610 pc = DW_ADDR (attr) + baseaddr;
9611
9612 if (cu->call_site_htab == NULL)
9613 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9614 NULL, &objfile->objfile_obstack,
9615 hashtab_obstack_allocate, NULL);
9616 call_site_local.pc = pc;
9617 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9618 if (*slot != NULL)
9619 {
9620 complaint (&symfile_complaints,
9621 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9622 "DIE 0x%x [in module %s]"),
b64f50a1 9623 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9624 return;
9625 }
9626
9627 /* Count parameters at the caller. */
9628
9629 nparams = 0;
9630 for (child_die = die->child; child_die && child_die->tag;
9631 child_die = sibling_die (child_die))
9632 {
9633 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9634 {
9635 complaint (&symfile_complaints,
9636 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9637 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9638 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9639 continue;
9640 }
9641
9642 nparams++;
9643 }
9644
9645 call_site = obstack_alloc (&objfile->objfile_obstack,
9646 (sizeof (*call_site)
9647 + (sizeof (*call_site->parameter)
9648 * (nparams - 1))));
9649 *slot = call_site;
9650 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9651 call_site->pc = pc;
9652
9653 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9654 {
9655 struct die_info *func_die;
9656
9657 /* Skip also over DW_TAG_inlined_subroutine. */
9658 for (func_die = die->parent;
9659 func_die && func_die->tag != DW_TAG_subprogram
9660 && func_die->tag != DW_TAG_subroutine_type;
9661 func_die = func_die->parent);
9662
9663 /* DW_AT_GNU_all_call_sites is a superset
9664 of DW_AT_GNU_all_tail_call_sites. */
9665 if (func_die
9666 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9667 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9668 {
9669 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9670 not complete. But keep CALL_SITE for look ups via call_site_htab,
9671 both the initial caller containing the real return address PC and
9672 the final callee containing the current PC of a chain of tail
9673 calls do not need to have the tail call list complete. But any
9674 function candidate for a virtual tail call frame searched via
9675 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9676 determined unambiguously. */
9677 }
9678 else
9679 {
9680 struct type *func_type = NULL;
9681
9682 if (func_die)
9683 func_type = get_die_type (func_die, cu);
9684 if (func_type != NULL)
9685 {
9686 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9687
9688 /* Enlist this call site to the function. */
9689 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9690 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9691 }
9692 else
9693 complaint (&symfile_complaints,
9694 _("Cannot find function owning DW_TAG_GNU_call_site "
9695 "DIE 0x%x [in module %s]"),
b64f50a1 9696 die->offset.sect_off, objfile->name);
96408a79
SA
9697 }
9698 }
9699
9700 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9701 if (attr == NULL)
9702 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9703 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9704 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9705 /* Keep NULL DWARF_BLOCK. */;
9706 else if (attr_form_is_block (attr))
9707 {
9708 struct dwarf2_locexpr_baton *dlbaton;
9709
9710 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9711 dlbaton->data = DW_BLOCK (attr)->data;
9712 dlbaton->size = DW_BLOCK (attr)->size;
9713 dlbaton->per_cu = cu->per_cu;
9714
9715 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9716 }
9717 else if (is_ref_attr (attr))
9718 {
96408a79
SA
9719 struct dwarf2_cu *target_cu = cu;
9720 struct die_info *target_die;
9721
9722 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9723 gdb_assert (target_cu->objfile == objfile);
9724 if (die_is_declaration (target_die, target_cu))
9725 {
9726 const char *target_physname;
9727
9728 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9729 if (target_physname == NULL)
9730 complaint (&symfile_complaints,
9731 _("DW_AT_GNU_call_site_target target DIE has invalid "
9732 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9733 die->offset.sect_off, objfile->name);
96408a79
SA
9734 else
9735 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9736 }
9737 else
9738 {
9739 CORE_ADDR lowpc;
9740
9741 /* DW_AT_entry_pc should be preferred. */
9742 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9743 complaint (&symfile_complaints,
9744 _("DW_AT_GNU_call_site_target target DIE has invalid "
9745 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9746 die->offset.sect_off, objfile->name);
96408a79
SA
9747 else
9748 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9749 }
9750 }
9751 else
9752 complaint (&symfile_complaints,
9753 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9754 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9755 die->offset.sect_off, objfile->name);
96408a79
SA
9756
9757 call_site->per_cu = cu->per_cu;
9758
9759 for (child_die = die->child;
9760 child_die && child_die->tag;
9761 child_die = sibling_die (child_die))
9762 {
96408a79 9763 struct call_site_parameter *parameter;
1788b2d3 9764 struct attribute *loc, *origin;
96408a79
SA
9765
9766 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9767 {
9768 /* Already printed the complaint above. */
9769 continue;
9770 }
9771
9772 gdb_assert (call_site->parameter_count < nparams);
9773 parameter = &call_site->parameter[call_site->parameter_count];
9774
1788b2d3
JK
9775 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9776 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9777 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9778
24c5c679 9779 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9780 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9781 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9782 {
9783 sect_offset offset;
9784
9785 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9786 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9787 if (!offset_in_cu_p (&cu->header, offset))
9788 {
9789 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9790 binding can be done only inside one CU. Such referenced DIE
9791 therefore cannot be even moved to DW_TAG_partial_unit. */
9792 complaint (&symfile_complaints,
9793 _("DW_AT_abstract_origin offset is not in CU for "
9794 "DW_TAG_GNU_call_site child DIE 0x%x "
9795 "[in module %s]"),
9796 child_die->offset.sect_off, objfile->name);
9797 continue;
9798 }
1788b2d3
JK
9799 parameter->u.param_offset.cu_off = (offset.sect_off
9800 - cu->header.offset.sect_off);
9801 }
9802 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9803 {
9804 complaint (&symfile_complaints,
9805 _("No DW_FORM_block* DW_AT_location for "
9806 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9807 child_die->offset.sect_off, objfile->name);
96408a79
SA
9808 continue;
9809 }
24c5c679 9810 else
96408a79 9811 {
24c5c679
JK
9812 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9813 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9814 if (parameter->u.dwarf_reg != -1)
9815 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9816 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9817 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9818 &parameter->u.fb_offset))
9819 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9820 else
9821 {
9822 complaint (&symfile_complaints,
9823 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9824 "for DW_FORM_block* DW_AT_location is supported for "
9825 "DW_TAG_GNU_call_site child DIE 0x%x "
9826 "[in module %s]"),
9827 child_die->offset.sect_off, objfile->name);
9828 continue;
9829 }
96408a79
SA
9830 }
9831
9832 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9833 if (!attr_form_is_block (attr))
9834 {
9835 complaint (&symfile_complaints,
9836 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9837 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9838 child_die->offset.sect_off, objfile->name);
96408a79
SA
9839 continue;
9840 }
9841 parameter->value = DW_BLOCK (attr)->data;
9842 parameter->value_size = DW_BLOCK (attr)->size;
9843
9844 /* Parameters are not pre-cleared by memset above. */
9845 parameter->data_value = NULL;
9846 parameter->data_value_size = 0;
9847 call_site->parameter_count++;
9848
9849 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9850 if (attr)
9851 {
9852 if (!attr_form_is_block (attr))
9853 complaint (&symfile_complaints,
9854 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9855 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9856 child_die->offset.sect_off, objfile->name);
96408a79
SA
9857 else
9858 {
9859 parameter->data_value = DW_BLOCK (attr)->data;
9860 parameter->data_value_size = DW_BLOCK (attr)->size;
9861 }
9862 }
9863 }
9864}
9865
43039443 9866/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9867 Return 1 if the attributes are present and valid, otherwise, return 0.
9868 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9869
9870static int
9871dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9872 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9873 struct partial_symtab *ranges_pst)
43039443
JK
9874{
9875 struct objfile *objfile = cu->objfile;
9876 struct comp_unit_head *cu_header = &cu->header;
9877 bfd *obfd = objfile->obfd;
9878 unsigned int addr_size = cu_header->addr_size;
9879 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9880 /* Base address selection entry. */
9881 CORE_ADDR base;
9882 int found_base;
9883 unsigned int dummy;
9884 gdb_byte *buffer;
9885 CORE_ADDR marker;
9886 int low_set;
9887 CORE_ADDR low = 0;
9888 CORE_ADDR high = 0;
ff013f42 9889 CORE_ADDR baseaddr;
43039443 9890
d00adf39
DE
9891 found_base = cu->base_known;
9892 base = cu->base_address;
43039443 9893
be391dca 9894 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9895 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9896 {
9897 complaint (&symfile_complaints,
9898 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9899 offset);
9900 return 0;
9901 }
dce234bc 9902 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9903
9904 /* Read in the largest possible address. */
9905 marker = read_address (obfd, buffer, cu, &dummy);
9906 if ((marker & mask) == mask)
9907 {
9908 /* If we found the largest possible address, then
9909 read the base address. */
9910 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9911 buffer += 2 * addr_size;
9912 offset += 2 * addr_size;
9913 found_base = 1;
9914 }
9915
9916 low_set = 0;
9917
e7030f15 9918 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9919
43039443
JK
9920 while (1)
9921 {
9922 CORE_ADDR range_beginning, range_end;
9923
9924 range_beginning = read_address (obfd, buffer, cu, &dummy);
9925 buffer += addr_size;
9926 range_end = read_address (obfd, buffer, cu, &dummy);
9927 buffer += addr_size;
9928 offset += 2 * addr_size;
9929
9930 /* An end of list marker is a pair of zero addresses. */
9931 if (range_beginning == 0 && range_end == 0)
9932 /* Found the end of list entry. */
9933 break;
9934
9935 /* Each base address selection entry is a pair of 2 values.
9936 The first is the largest possible address, the second is
9937 the base address. Check for a base address here. */
9938 if ((range_beginning & mask) == mask)
9939 {
9940 /* If we found the largest possible address, then
9941 read the base address. */
9942 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9943 found_base = 1;
9944 continue;
9945 }
9946
9947 if (!found_base)
9948 {
9949 /* We have no valid base address for the ranges
9950 data. */
9951 complaint (&symfile_complaints,
9952 _("Invalid .debug_ranges data (no base address)"));
9953 return 0;
9954 }
9955
9277c30c
UW
9956 if (range_beginning > range_end)
9957 {
9958 /* Inverted range entries are invalid. */
9959 complaint (&symfile_complaints,
9960 _("Invalid .debug_ranges data (inverted range)"));
9961 return 0;
9962 }
9963
9964 /* Empty range entries have no effect. */
9965 if (range_beginning == range_end)
9966 continue;
9967
43039443
JK
9968 range_beginning += base;
9969 range_end += base;
9970
01093045
DE
9971 /* A not-uncommon case of bad debug info.
9972 Don't pollute the addrmap with bad data. */
9973 if (range_beginning + baseaddr == 0
9974 && !dwarf2_per_objfile->has_section_at_zero)
9975 {
9976 complaint (&symfile_complaints,
9977 _(".debug_ranges entry has start address of zero"
9978 " [in module %s]"), objfile->name);
9979 continue;
9980 }
9981
9277c30c 9982 if (ranges_pst != NULL)
ff013f42 9983 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
9984 range_beginning + baseaddr,
9985 range_end - 1 + baseaddr,
ff013f42
JK
9986 ranges_pst);
9987
43039443
JK
9988 /* FIXME: This is recording everything as a low-high
9989 segment of consecutive addresses. We should have a
9990 data structure for discontiguous block ranges
9991 instead. */
9992 if (! low_set)
9993 {
9994 low = range_beginning;
9995 high = range_end;
9996 low_set = 1;
9997 }
9998 else
9999 {
10000 if (range_beginning < low)
10001 low = range_beginning;
10002 if (range_end > high)
10003 high = range_end;
10004 }
10005 }
10006
10007 if (! low_set)
10008 /* If the first entry is an end-of-list marker, the range
10009 describes an empty scope, i.e. no instructions. */
10010 return 0;
10011
10012 if (low_return)
10013 *low_return = low;
10014 if (high_return)
10015 *high_return = high;
10016 return 1;
10017}
10018
af34e669
DJ
10019/* Get low and high pc attributes from a die. Return 1 if the attributes
10020 are present and valid, otherwise, return 0. Return -1 if the range is
10021 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10022
c906108c 10023static int
af34e669 10024dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10025 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10026 struct partial_symtab *pst)
c906108c
SS
10027{
10028 struct attribute *attr;
91da1414 10029 struct attribute *attr_high;
af34e669
DJ
10030 CORE_ADDR low = 0;
10031 CORE_ADDR high = 0;
10032 int ret = 0;
c906108c 10033
91da1414
MW
10034 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10035 if (attr_high)
af34e669 10036 {
e142c38c 10037 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10038 if (attr)
91da1414
MW
10039 {
10040 low = DW_ADDR (attr);
3019eac3
DE
10041 if (attr_high->form == DW_FORM_addr
10042 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10043 high = DW_ADDR (attr_high);
10044 else
10045 high = low + DW_UNSND (attr_high);
10046 }
af34e669
DJ
10047 else
10048 /* Found high w/o low attribute. */
10049 return 0;
10050
10051 /* Found consecutive range of addresses. */
10052 ret = 1;
10053 }
c906108c 10054 else
af34e669 10055 {
e142c38c 10056 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10057 if (attr != NULL)
10058 {
2e3cf129
DE
10059 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
10060
af34e669 10061 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10062 .debug_ranges section. */
2e3cf129 10063 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10064 return 0;
43039443 10065 /* Found discontinuous range of addresses. */
af34e669
DJ
10066 ret = -1;
10067 }
10068 }
c906108c 10069
9373cf26
JK
10070 /* read_partial_die has also the strict LOW < HIGH requirement. */
10071 if (high <= low)
c906108c
SS
10072 return 0;
10073
10074 /* When using the GNU linker, .gnu.linkonce. sections are used to
10075 eliminate duplicate copies of functions and vtables and such.
10076 The linker will arbitrarily choose one and discard the others.
10077 The AT_*_pc values for such functions refer to local labels in
10078 these sections. If the section from that file was discarded, the
10079 labels are not in the output, so the relocs get a value of 0.
10080 If this is a discarded function, mark the pc bounds as invalid,
10081 so that GDB will ignore it. */
72dca2f5 10082 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10083 return 0;
10084
10085 *lowpc = low;
96408a79
SA
10086 if (highpc)
10087 *highpc = high;
af34e669 10088 return ret;
c906108c
SS
10089}
10090
b084d499
JB
10091/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10092 its low and high PC addresses. Do nothing if these addresses could not
10093 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10094 and HIGHPC to the high address if greater than HIGHPC. */
10095
10096static void
10097dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10098 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10099 struct dwarf2_cu *cu)
10100{
10101 CORE_ADDR low, high;
10102 struct die_info *child = die->child;
10103
d85a05f0 10104 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10105 {
10106 *lowpc = min (*lowpc, low);
10107 *highpc = max (*highpc, high);
10108 }
10109
10110 /* If the language does not allow nested subprograms (either inside
10111 subprograms or lexical blocks), we're done. */
10112 if (cu->language != language_ada)
10113 return;
6e70227d 10114
b084d499
JB
10115 /* Check all the children of the given DIE. If it contains nested
10116 subprograms, then check their pc bounds. Likewise, we need to
10117 check lexical blocks as well, as they may also contain subprogram
10118 definitions. */
10119 while (child && child->tag)
10120 {
10121 if (child->tag == DW_TAG_subprogram
10122 || child->tag == DW_TAG_lexical_block)
10123 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10124 child = sibling_die (child);
10125 }
10126}
10127
fae299cd
DC
10128/* Get the low and high pc's represented by the scope DIE, and store
10129 them in *LOWPC and *HIGHPC. If the correct values can't be
10130 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10131
10132static void
10133get_scope_pc_bounds (struct die_info *die,
10134 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10135 struct dwarf2_cu *cu)
10136{
10137 CORE_ADDR best_low = (CORE_ADDR) -1;
10138 CORE_ADDR best_high = (CORE_ADDR) 0;
10139 CORE_ADDR current_low, current_high;
10140
d85a05f0 10141 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10142 {
10143 best_low = current_low;
10144 best_high = current_high;
10145 }
10146 else
10147 {
10148 struct die_info *child = die->child;
10149
10150 while (child && child->tag)
10151 {
10152 switch (child->tag) {
10153 case DW_TAG_subprogram:
b084d499 10154 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10155 break;
10156 case DW_TAG_namespace:
f55ee35c 10157 case DW_TAG_module:
fae299cd
DC
10158 /* FIXME: carlton/2004-01-16: Should we do this for
10159 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10160 that current GCC's always emit the DIEs corresponding
10161 to definitions of methods of classes as children of a
10162 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10163 the DIEs giving the declarations, which could be
10164 anywhere). But I don't see any reason why the
10165 standards says that they have to be there. */
10166 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10167
10168 if (current_low != ((CORE_ADDR) -1))
10169 {
10170 best_low = min (best_low, current_low);
10171 best_high = max (best_high, current_high);
10172 }
10173 break;
10174 default:
0963b4bd 10175 /* Ignore. */
fae299cd
DC
10176 break;
10177 }
10178
10179 child = sibling_die (child);
10180 }
10181 }
10182
10183 *lowpc = best_low;
10184 *highpc = best_high;
10185}
10186
801e3a5b
JB
10187/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10188 in DIE. */
380bca97 10189
801e3a5b
JB
10190static void
10191dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10192 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10193{
bb5ed363 10194 struct objfile *objfile = cu->objfile;
801e3a5b 10195 struct attribute *attr;
91da1414 10196 struct attribute *attr_high;
801e3a5b 10197
91da1414
MW
10198 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10199 if (attr_high)
801e3a5b 10200 {
801e3a5b
JB
10201 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10202 if (attr)
10203 {
10204 CORE_ADDR low = DW_ADDR (attr);
91da1414 10205 CORE_ADDR high;
3019eac3
DE
10206 if (attr_high->form == DW_FORM_addr
10207 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10208 high = DW_ADDR (attr_high);
10209 else
10210 high = low + DW_UNSND (attr_high);
9a619af0 10211
801e3a5b
JB
10212 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10213 }
10214 }
10215
10216 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10217 if (attr)
10218 {
bb5ed363 10219 bfd *obfd = objfile->obfd;
801e3a5b
JB
10220
10221 /* The value of the DW_AT_ranges attribute is the offset of the
10222 address range list in the .debug_ranges section. */
2e3cf129 10223 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 10224 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10225
10226 /* For some target architectures, but not others, the
10227 read_address function sign-extends the addresses it returns.
10228 To recognize base address selection entries, we need a
10229 mask. */
10230 unsigned int addr_size = cu->header.addr_size;
10231 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10232
10233 /* The base address, to which the next pair is relative. Note
10234 that this 'base' is a DWARF concept: most entries in a range
10235 list are relative, to reduce the number of relocs against the
10236 debugging information. This is separate from this function's
10237 'baseaddr' argument, which GDB uses to relocate debugging
10238 information from a shared library based on the address at
10239 which the library was loaded. */
d00adf39
DE
10240 CORE_ADDR base = cu->base_address;
10241 int base_known = cu->base_known;
801e3a5b 10242
be391dca 10243 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10244 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10245 {
10246 complaint (&symfile_complaints,
10247 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10248 offset);
10249 return;
10250 }
10251
10252 for (;;)
10253 {
10254 unsigned int bytes_read;
10255 CORE_ADDR start, end;
10256
10257 start = read_address (obfd, buffer, cu, &bytes_read);
10258 buffer += bytes_read;
10259 end = read_address (obfd, buffer, cu, &bytes_read);
10260 buffer += bytes_read;
10261
10262 /* Did we find the end of the range list? */
10263 if (start == 0 && end == 0)
10264 break;
10265
10266 /* Did we find a base address selection entry? */
10267 else if ((start & base_select_mask) == base_select_mask)
10268 {
10269 base = end;
10270 base_known = 1;
10271 }
10272
10273 /* We found an ordinary address range. */
10274 else
10275 {
10276 if (!base_known)
10277 {
10278 complaint (&symfile_complaints,
3e43a32a
MS
10279 _("Invalid .debug_ranges data "
10280 "(no base address)"));
801e3a5b
JB
10281 return;
10282 }
10283
9277c30c
UW
10284 if (start > end)
10285 {
10286 /* Inverted range entries are invalid. */
10287 complaint (&symfile_complaints,
10288 _("Invalid .debug_ranges data "
10289 "(inverted range)"));
10290 return;
10291 }
10292
10293 /* Empty range entries have no effect. */
10294 if (start == end)
10295 continue;
10296
01093045
DE
10297 start += base + baseaddr;
10298 end += base + baseaddr;
10299
10300 /* A not-uncommon case of bad debug info.
10301 Don't pollute the addrmap with bad data. */
10302 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10303 {
10304 complaint (&symfile_complaints,
10305 _(".debug_ranges entry has start address of zero"
10306 " [in module %s]"), objfile->name);
10307 continue;
10308 }
10309
10310 record_block_range (block, start, end - 1);
801e3a5b
JB
10311 }
10312 }
10313 }
10314}
10315
685b1105
JK
10316/* Check whether the producer field indicates either of GCC < 4.6, or the
10317 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10318
685b1105
JK
10319static void
10320check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10321{
10322 const char *cs;
10323 int major, minor, release;
10324
10325 if (cu->producer == NULL)
10326 {
10327 /* For unknown compilers expect their behavior is DWARF version
10328 compliant.
10329
10330 GCC started to support .debug_types sections by -gdwarf-4 since
10331 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10332 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10333 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10334 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10335 }
685b1105 10336 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10337 {
685b1105
JK
10338 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10339
ba919b58
TT
10340 cs = &cu->producer[strlen ("GNU ")];
10341 while (*cs && !isdigit (*cs))
10342 cs++;
10343 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10344 {
10345 /* Not recognized as GCC. */
10346 }
10347 else
1b80a9fa
JK
10348 {
10349 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10350 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10351 }
685b1105
JK
10352 }
10353 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10354 cu->producer_is_icc = 1;
10355 else
10356 {
10357 /* For other non-GCC compilers, expect their behavior is DWARF version
10358 compliant. */
60d5a603
JK
10359 }
10360
ba919b58 10361 cu->checked_producer = 1;
685b1105 10362}
ba919b58 10363
685b1105
JK
10364/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10365 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10366 during 4.6.0 experimental. */
10367
10368static int
10369producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10370{
10371 if (!cu->checked_producer)
10372 check_producer (cu);
10373
10374 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10375}
10376
10377/* Return the default accessibility type if it is not overriden by
10378 DW_AT_accessibility. */
10379
10380static enum dwarf_access_attribute
10381dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10382{
10383 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10384 {
10385 /* The default DWARF 2 accessibility for members is public, the default
10386 accessibility for inheritance is private. */
10387
10388 if (die->tag != DW_TAG_inheritance)
10389 return DW_ACCESS_public;
10390 else
10391 return DW_ACCESS_private;
10392 }
10393 else
10394 {
10395 /* DWARF 3+ defines the default accessibility a different way. The same
10396 rules apply now for DW_TAG_inheritance as for the members and it only
10397 depends on the container kind. */
10398
10399 if (die->parent->tag == DW_TAG_class_type)
10400 return DW_ACCESS_private;
10401 else
10402 return DW_ACCESS_public;
10403 }
10404}
10405
74ac6d43
TT
10406/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10407 offset. If the attribute was not found return 0, otherwise return
10408 1. If it was found but could not properly be handled, set *OFFSET
10409 to 0. */
10410
10411static int
10412handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10413 LONGEST *offset)
10414{
10415 struct attribute *attr;
10416
10417 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10418 if (attr != NULL)
10419 {
10420 *offset = 0;
10421
10422 /* Note that we do not check for a section offset first here.
10423 This is because DW_AT_data_member_location is new in DWARF 4,
10424 so if we see it, we can assume that a constant form is really
10425 a constant and not a section offset. */
10426 if (attr_form_is_constant (attr))
10427 *offset = dwarf2_get_attr_constant_value (attr, 0);
10428 else if (attr_form_is_section_offset (attr))
10429 dwarf2_complex_location_expr_complaint ();
10430 else if (attr_form_is_block (attr))
10431 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10432 else
10433 dwarf2_complex_location_expr_complaint ();
10434
10435 return 1;
10436 }
10437
10438 return 0;
10439}
10440
c906108c
SS
10441/* Add an aggregate field to the field list. */
10442
10443static void
107d2387 10444dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10445 struct dwarf2_cu *cu)
6e70227d 10446{
e7c27a73 10447 struct objfile *objfile = cu->objfile;
5e2b427d 10448 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10449 struct nextfield *new_field;
10450 struct attribute *attr;
10451 struct field *fp;
10452 char *fieldname = "";
10453
10454 /* Allocate a new field list entry and link it in. */
10455 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10456 make_cleanup (xfree, new_field);
c906108c 10457 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10458
10459 if (die->tag == DW_TAG_inheritance)
10460 {
10461 new_field->next = fip->baseclasses;
10462 fip->baseclasses = new_field;
10463 }
10464 else
10465 {
10466 new_field->next = fip->fields;
10467 fip->fields = new_field;
10468 }
c906108c
SS
10469 fip->nfields++;
10470
e142c38c 10471 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10472 if (attr)
10473 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10474 else
10475 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10476 if (new_field->accessibility != DW_ACCESS_public)
10477 fip->non_public_fields = 1;
60d5a603 10478
e142c38c 10479 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10480 if (attr)
10481 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10482 else
10483 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10484
10485 fp = &new_field->field;
a9a9bd0f 10486
e142c38c 10487 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10488 {
74ac6d43
TT
10489 LONGEST offset;
10490
a9a9bd0f 10491 /* Data member other than a C++ static data member. */
6e70227d 10492
c906108c 10493 /* Get type of field. */
e7c27a73 10494 fp->type = die_type (die, cu);
c906108c 10495
d6a843b5 10496 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10497
c906108c 10498 /* Get bit size of field (zero if none). */
e142c38c 10499 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10500 if (attr)
10501 {
10502 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10503 }
10504 else
10505 {
10506 FIELD_BITSIZE (*fp) = 0;
10507 }
10508
10509 /* Get bit offset of field. */
74ac6d43
TT
10510 if (handle_data_member_location (die, cu, &offset))
10511 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10512 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10513 if (attr)
10514 {
5e2b427d 10515 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10516 {
10517 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10518 additional bit offset from the MSB of the containing
10519 anonymous object to the MSB of the field. We don't
10520 have to do anything special since we don't need to
10521 know the size of the anonymous object. */
f41f5e61 10522 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10523 }
10524 else
10525 {
10526 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10527 MSB of the anonymous object, subtract off the number of
10528 bits from the MSB of the field to the MSB of the
10529 object, and then subtract off the number of bits of
10530 the field itself. The result is the bit offset of
10531 the LSB of the field. */
c906108c
SS
10532 int anonymous_size;
10533 int bit_offset = DW_UNSND (attr);
10534
e142c38c 10535 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10536 if (attr)
10537 {
10538 /* The size of the anonymous object containing
10539 the bit field is explicit, so use the
10540 indicated size (in bytes). */
10541 anonymous_size = DW_UNSND (attr);
10542 }
10543 else
10544 {
10545 /* The size of the anonymous object containing
10546 the bit field must be inferred from the type
10547 attribute of the data member containing the
10548 bit field. */
10549 anonymous_size = TYPE_LENGTH (fp->type);
10550 }
f41f5e61
PA
10551 SET_FIELD_BITPOS (*fp,
10552 (FIELD_BITPOS (*fp)
10553 + anonymous_size * bits_per_byte
10554 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10555 }
10556 }
10557
10558 /* Get name of field. */
39cbfefa
DJ
10559 fieldname = dwarf2_name (die, cu);
10560 if (fieldname == NULL)
10561 fieldname = "";
d8151005
DJ
10562
10563 /* The name is already allocated along with this objfile, so we don't
10564 need to duplicate it for the type. */
10565 fp->name = fieldname;
c906108c
SS
10566
10567 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10568 pointer or virtual base class pointer) to private. */
e142c38c 10569 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10570 {
d48cc9dd 10571 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10572 new_field->accessibility = DW_ACCESS_private;
10573 fip->non_public_fields = 1;
10574 }
10575 }
a9a9bd0f 10576 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10577 {
a9a9bd0f
DC
10578 /* C++ static member. */
10579
10580 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10581 is a declaration, but all versions of G++ as of this writing
10582 (so through at least 3.2.1) incorrectly generate
10583 DW_TAG_variable tags. */
6e70227d 10584
ff355380 10585 const char *physname;
c906108c 10586
a9a9bd0f 10587 /* Get name of field. */
39cbfefa
DJ
10588 fieldname = dwarf2_name (die, cu);
10589 if (fieldname == NULL)
c906108c
SS
10590 return;
10591
254e6b9e 10592 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10593 if (attr
10594 /* Only create a symbol if this is an external value.
10595 new_symbol checks this and puts the value in the global symbol
10596 table, which we want. If it is not external, new_symbol
10597 will try to put the value in cu->list_in_scope which is wrong. */
10598 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10599 {
10600 /* A static const member, not much different than an enum as far as
10601 we're concerned, except that we can support more types. */
10602 new_symbol (die, NULL, cu);
10603 }
10604
2df3850c 10605 /* Get physical name. */
ff355380 10606 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10607
d8151005
DJ
10608 /* The name is already allocated along with this objfile, so we don't
10609 need to duplicate it for the type. */
10610 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10611 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10612 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10613 }
10614 else if (die->tag == DW_TAG_inheritance)
10615 {
74ac6d43 10616 LONGEST offset;
d4b96c9a 10617
74ac6d43
TT
10618 /* C++ base class field. */
10619 if (handle_data_member_location (die, cu, &offset))
10620 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10621 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10622 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10623 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10624 fip->nbaseclasses++;
10625 }
10626}
10627
98751a41
JK
10628/* Add a typedef defined in the scope of the FIP's class. */
10629
10630static void
10631dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10632 struct dwarf2_cu *cu)
6e70227d 10633{
98751a41 10634 struct objfile *objfile = cu->objfile;
98751a41
JK
10635 struct typedef_field_list *new_field;
10636 struct attribute *attr;
10637 struct typedef_field *fp;
10638 char *fieldname = "";
10639
10640 /* Allocate a new field list entry and link it in. */
10641 new_field = xzalloc (sizeof (*new_field));
10642 make_cleanup (xfree, new_field);
10643
10644 gdb_assert (die->tag == DW_TAG_typedef);
10645
10646 fp = &new_field->field;
10647
10648 /* Get name of field. */
10649 fp->name = dwarf2_name (die, cu);
10650 if (fp->name == NULL)
10651 return;
10652
10653 fp->type = read_type_die (die, cu);
10654
10655 new_field->next = fip->typedef_field_list;
10656 fip->typedef_field_list = new_field;
10657 fip->typedef_field_list_count++;
10658}
10659
c906108c
SS
10660/* Create the vector of fields, and attach it to the type. */
10661
10662static void
fba45db2 10663dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10664 struct dwarf2_cu *cu)
c906108c
SS
10665{
10666 int nfields = fip->nfields;
10667
10668 /* Record the field count, allocate space for the array of fields,
10669 and create blank accessibility bitfields if necessary. */
10670 TYPE_NFIELDS (type) = nfields;
10671 TYPE_FIELDS (type) = (struct field *)
10672 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10673 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10674
b4ba55a1 10675 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10676 {
10677 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10678
10679 TYPE_FIELD_PRIVATE_BITS (type) =
10680 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10681 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10682
10683 TYPE_FIELD_PROTECTED_BITS (type) =
10684 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10685 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10686
774b6a14
TT
10687 TYPE_FIELD_IGNORE_BITS (type) =
10688 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10689 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10690 }
10691
10692 /* If the type has baseclasses, allocate and clear a bit vector for
10693 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10694 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10695 {
10696 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10697 unsigned char *pointer;
c906108c
SS
10698
10699 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10700 pointer = TYPE_ALLOC (type, num_bytes);
10701 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10702 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10703 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10704 }
10705
3e43a32a
MS
10706 /* Copy the saved-up fields into the field vector. Start from the head of
10707 the list, adding to the tail of the field array, so that they end up in
10708 the same order in the array in which they were added to the list. */
c906108c
SS
10709 while (nfields-- > 0)
10710 {
7d0ccb61
DJ
10711 struct nextfield *fieldp;
10712
10713 if (fip->fields)
10714 {
10715 fieldp = fip->fields;
10716 fip->fields = fieldp->next;
10717 }
10718 else
10719 {
10720 fieldp = fip->baseclasses;
10721 fip->baseclasses = fieldp->next;
10722 }
10723
10724 TYPE_FIELD (type, nfields) = fieldp->field;
10725 switch (fieldp->accessibility)
c906108c 10726 {
c5aa993b 10727 case DW_ACCESS_private:
b4ba55a1
JB
10728 if (cu->language != language_ada)
10729 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10730 break;
c906108c 10731
c5aa993b 10732 case DW_ACCESS_protected:
b4ba55a1
JB
10733 if (cu->language != language_ada)
10734 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10735 break;
c906108c 10736
c5aa993b
JM
10737 case DW_ACCESS_public:
10738 break;
c906108c 10739
c5aa993b
JM
10740 default:
10741 /* Unknown accessibility. Complain and treat it as public. */
10742 {
e2e0b3e5 10743 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10744 fieldp->accessibility);
c5aa993b
JM
10745 }
10746 break;
c906108c
SS
10747 }
10748 if (nfields < fip->nbaseclasses)
10749 {
7d0ccb61 10750 switch (fieldp->virtuality)
c906108c 10751 {
c5aa993b
JM
10752 case DW_VIRTUALITY_virtual:
10753 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10754 if (cu->language == language_ada)
a73c6dcd 10755 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10756 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10757 break;
c906108c
SS
10758 }
10759 }
c906108c
SS
10760 }
10761}
10762
c906108c
SS
10763/* Add a member function to the proper fieldlist. */
10764
10765static void
107d2387 10766dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10767 struct type *type, struct dwarf2_cu *cu)
c906108c 10768{
e7c27a73 10769 struct objfile *objfile = cu->objfile;
c906108c
SS
10770 struct attribute *attr;
10771 struct fnfieldlist *flp;
10772 int i;
10773 struct fn_field *fnp;
10774 char *fieldname;
c906108c 10775 struct nextfnfield *new_fnfield;
f792889a 10776 struct type *this_type;
60d5a603 10777 enum dwarf_access_attribute accessibility;
c906108c 10778
b4ba55a1 10779 if (cu->language == language_ada)
a73c6dcd 10780 error (_("unexpected member function in Ada type"));
b4ba55a1 10781
2df3850c 10782 /* Get name of member function. */
39cbfefa
DJ
10783 fieldname = dwarf2_name (die, cu);
10784 if (fieldname == NULL)
2df3850c 10785 return;
c906108c 10786
c906108c
SS
10787 /* Look up member function name in fieldlist. */
10788 for (i = 0; i < fip->nfnfields; i++)
10789 {
27bfe10e 10790 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10791 break;
10792 }
10793
10794 /* Create new list element if necessary. */
10795 if (i < fip->nfnfields)
10796 flp = &fip->fnfieldlists[i];
10797 else
10798 {
10799 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10800 {
10801 fip->fnfieldlists = (struct fnfieldlist *)
10802 xrealloc (fip->fnfieldlists,
10803 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10804 * sizeof (struct fnfieldlist));
c906108c 10805 if (fip->nfnfields == 0)
c13c43fd 10806 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10807 }
10808 flp = &fip->fnfieldlists[fip->nfnfields];
10809 flp->name = fieldname;
10810 flp->length = 0;
10811 flp->head = NULL;
3da10d80 10812 i = fip->nfnfields++;
c906108c
SS
10813 }
10814
10815 /* Create a new member function field and chain it to the field list
0963b4bd 10816 entry. */
c906108c 10817 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10818 make_cleanup (xfree, new_fnfield);
c906108c
SS
10819 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10820 new_fnfield->next = flp->head;
10821 flp->head = new_fnfield;
10822 flp->length++;
10823
10824 /* Fill in the member function field info. */
10825 fnp = &new_fnfield->fnfield;
3da10d80
KS
10826
10827 /* Delay processing of the physname until later. */
10828 if (cu->language == language_cplus || cu->language == language_java)
10829 {
10830 add_to_method_list (type, i, flp->length - 1, fieldname,
10831 die, cu);
10832 }
10833 else
10834 {
1d06ead6 10835 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10836 fnp->physname = physname ? physname : "";
10837 }
10838
c906108c 10839 fnp->type = alloc_type (objfile);
f792889a
DJ
10840 this_type = read_type_die (die, cu);
10841 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10842 {
f792889a 10843 int nparams = TYPE_NFIELDS (this_type);
c906108c 10844
f792889a 10845 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10846 of the method itself (TYPE_CODE_METHOD). */
10847 smash_to_method_type (fnp->type, type,
f792889a
DJ
10848 TYPE_TARGET_TYPE (this_type),
10849 TYPE_FIELDS (this_type),
10850 TYPE_NFIELDS (this_type),
10851 TYPE_VARARGS (this_type));
c906108c
SS
10852
10853 /* Handle static member functions.
c5aa993b 10854 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10855 member functions. G++ helps GDB by marking the first
10856 parameter for non-static member functions (which is the this
10857 pointer) as artificial. We obtain this information from
10858 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10859 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10860 fnp->voffset = VOFFSET_STATIC;
10861 }
10862 else
e2e0b3e5 10863 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10864 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10865
10866 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10867 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10868 fnp->fcontext = die_containing_type (die, cu);
c906108c 10869
3e43a32a
MS
10870 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10871 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10872
10873 /* Get accessibility. */
e142c38c 10874 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10875 if (attr)
60d5a603
JK
10876 accessibility = DW_UNSND (attr);
10877 else
10878 accessibility = dwarf2_default_access_attribute (die, cu);
10879 switch (accessibility)
c906108c 10880 {
60d5a603
JK
10881 case DW_ACCESS_private:
10882 fnp->is_private = 1;
10883 break;
10884 case DW_ACCESS_protected:
10885 fnp->is_protected = 1;
10886 break;
c906108c
SS
10887 }
10888
b02dede2 10889 /* Check for artificial methods. */
e142c38c 10890 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10891 if (attr && DW_UNSND (attr) != 0)
10892 fnp->is_artificial = 1;
10893
0d564a31 10894 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10895 function. For older versions of GCC, this is an offset in the
10896 appropriate virtual table, as specified by DW_AT_containing_type.
10897 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10898 to the object address. */
10899
e142c38c 10900 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10901 if (attr)
8e19ed76 10902 {
aec5aa8b 10903 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10904 {
aec5aa8b
TT
10905 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10906 {
10907 /* Old-style GCC. */
10908 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10909 }
10910 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10911 || (DW_BLOCK (attr)->size > 1
10912 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10913 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10914 {
10915 struct dwarf_block blk;
10916 int offset;
10917
10918 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10919 ? 1 : 2);
10920 blk.size = DW_BLOCK (attr)->size - offset;
10921 blk.data = DW_BLOCK (attr)->data + offset;
10922 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10923 if ((fnp->voffset % cu->header.addr_size) != 0)
10924 dwarf2_complex_location_expr_complaint ();
10925 else
10926 fnp->voffset /= cu->header.addr_size;
10927 fnp->voffset += 2;
10928 }
10929 else
10930 dwarf2_complex_location_expr_complaint ();
10931
10932 if (!fnp->fcontext)
10933 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10934 }
3690dd37 10935 else if (attr_form_is_section_offset (attr))
8e19ed76 10936 {
4d3c2250 10937 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10938 }
10939 else
10940 {
4d3c2250
KB
10941 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10942 fieldname);
8e19ed76 10943 }
0d564a31 10944 }
d48cc9dd
DJ
10945 else
10946 {
10947 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10948 if (attr && DW_UNSND (attr))
10949 {
10950 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10951 complaint (&symfile_complaints,
3e43a32a
MS
10952 _("Member function \"%s\" (offset %d) is virtual "
10953 "but the vtable offset is not specified"),
b64f50a1 10954 fieldname, die->offset.sect_off);
9655fd1a 10955 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
10956 TYPE_CPLUS_DYNAMIC (type) = 1;
10957 }
10958 }
c906108c
SS
10959}
10960
10961/* Create the vector of member function fields, and attach it to the type. */
10962
10963static void
fba45db2 10964dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10965 struct dwarf2_cu *cu)
c906108c
SS
10966{
10967 struct fnfieldlist *flp;
c906108c
SS
10968 int i;
10969
b4ba55a1 10970 if (cu->language == language_ada)
a73c6dcd 10971 error (_("unexpected member functions in Ada type"));
b4ba55a1 10972
c906108c
SS
10973 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10974 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10975 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10976
10977 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10978 {
10979 struct nextfnfield *nfp = flp->head;
10980 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10981 int k;
10982
10983 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10984 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10985 fn_flp->fn_fields = (struct fn_field *)
10986 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10987 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 10988 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
10989 }
10990
10991 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
10992}
10993
1168df01
JB
10994/* Returns non-zero if NAME is the name of a vtable member in CU's
10995 language, zero otherwise. */
10996static int
10997is_vtable_name (const char *name, struct dwarf2_cu *cu)
10998{
10999 static const char vptr[] = "_vptr";
987504bb 11000 static const char vtable[] = "vtable";
1168df01 11001
987504bb
JJ
11002 /* Look for the C++ and Java forms of the vtable. */
11003 if ((cu->language == language_java
11004 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11005 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11006 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11007 return 1;
11008
11009 return 0;
11010}
11011
c0dd20ea 11012/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11013 functions, with the ABI-specified layout. If TYPE describes
11014 such a structure, smash it into a member function type.
61049d3b
DJ
11015
11016 GCC shouldn't do this; it should just output pointer to member DIEs.
11017 This is GCC PR debug/28767. */
c0dd20ea 11018
0b92b5bb
TT
11019static void
11020quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11021{
0b92b5bb 11022 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11023
11024 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11025 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11026 return;
c0dd20ea
DJ
11027
11028 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11029 if (TYPE_FIELD_NAME (type, 0) == NULL
11030 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11031 || TYPE_FIELD_NAME (type, 1) == NULL
11032 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11033 return;
c0dd20ea
DJ
11034
11035 /* Find the type of the method. */
0b92b5bb 11036 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11037 if (pfn_type == NULL
11038 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11039 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11040 return;
c0dd20ea
DJ
11041
11042 /* Look for the "this" argument. */
11043 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11044 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11045 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11046 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11047 return;
c0dd20ea
DJ
11048
11049 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11050 new_type = alloc_type (objfile);
11051 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11052 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11053 TYPE_VARARGS (pfn_type));
0b92b5bb 11054 smash_to_methodptr_type (type, new_type);
c0dd20ea 11055}
1168df01 11056
685b1105
JK
11057/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11058 (icc). */
11059
11060static int
11061producer_is_icc (struct dwarf2_cu *cu)
11062{
11063 if (!cu->checked_producer)
11064 check_producer (cu);
11065
11066 return cu->producer_is_icc;
11067}
11068
c906108c 11069/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11070 (definition) to create a type for the structure or union. Fill in
11071 the type's name and general properties; the members will not be
11072 processed until process_structure_type.
c906108c 11073
c767944b
DJ
11074 NOTE: we need to call these functions regardless of whether or not the
11075 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11076 structure or union. This gets the type entered into our set of
11077 user defined types.
11078
11079 However, if the structure is incomplete (an opaque struct/union)
11080 then suppress creating a symbol table entry for it since gdb only
11081 wants to find the one with the complete definition. Note that if
11082 it is complete, we just call new_symbol, which does it's own
11083 checking about whether the struct/union is anonymous or not (and
11084 suppresses creating a symbol table entry itself). */
11085
f792889a 11086static struct type *
134d01f1 11087read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11088{
e7c27a73 11089 struct objfile *objfile = cu->objfile;
c906108c
SS
11090 struct type *type;
11091 struct attribute *attr;
39cbfefa 11092 char *name;
c906108c 11093
348e048f
DE
11094 /* If the definition of this type lives in .debug_types, read that type.
11095 Don't follow DW_AT_specification though, that will take us back up
11096 the chain and we want to go down. */
45e58e77 11097 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11098 if (attr)
11099 {
11100 struct dwarf2_cu *type_cu = cu;
11101 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11102
348e048f
DE
11103 /* We could just recurse on read_structure_type, but we need to call
11104 get_die_type to ensure only one type for this DIE is created.
11105 This is important, for example, because for c++ classes we need
11106 TYPE_NAME set which is only done by new_symbol. Blech. */
11107 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11108
11109 /* TYPE_CU may not be the same as CU.
11110 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11111 return set_die_type (die, type, cu);
11112 }
11113
c0dd20ea 11114 type = alloc_type (objfile);
c906108c 11115 INIT_CPLUS_SPECIFIC (type);
93311388 11116
39cbfefa
DJ
11117 name = dwarf2_name (die, cu);
11118 if (name != NULL)
c906108c 11119 {
987504bb
JJ
11120 if (cu->language == language_cplus
11121 || cu->language == language_java)
63d06c5c 11122 {
3da10d80
KS
11123 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11124
11125 /* dwarf2_full_name might have already finished building the DIE's
11126 type. If so, there is no need to continue. */
11127 if (get_die_type (die, cu) != NULL)
11128 return get_die_type (die, cu);
11129
11130 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11131 if (die->tag == DW_TAG_structure_type
11132 || die->tag == DW_TAG_class_type)
11133 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11134 }
11135 else
11136 {
d8151005
DJ
11137 /* The name is already allocated along with this objfile, so
11138 we don't need to duplicate it for the type. */
94af9270
KS
11139 TYPE_TAG_NAME (type) = (char *) name;
11140 if (die->tag == DW_TAG_class_type)
11141 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11142 }
c906108c
SS
11143 }
11144
11145 if (die->tag == DW_TAG_structure_type)
11146 {
11147 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11148 }
11149 else if (die->tag == DW_TAG_union_type)
11150 {
11151 TYPE_CODE (type) = TYPE_CODE_UNION;
11152 }
11153 else
11154 {
c906108c
SS
11155 TYPE_CODE (type) = TYPE_CODE_CLASS;
11156 }
11157
0cc2414c
TT
11158 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11159 TYPE_DECLARED_CLASS (type) = 1;
11160
e142c38c 11161 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11162 if (attr)
11163 {
11164 TYPE_LENGTH (type) = DW_UNSND (attr);
11165 }
11166 else
11167 {
11168 TYPE_LENGTH (type) = 0;
11169 }
11170
685b1105
JK
11171 if (producer_is_icc (cu))
11172 {
11173 /* ICC does not output the required DW_AT_declaration
11174 on incomplete types, but gives them a size of zero. */
11175 }
11176 else
11177 TYPE_STUB_SUPPORTED (type) = 1;
11178
dc718098 11179 if (die_is_declaration (die, cu))
876cecd0 11180 TYPE_STUB (type) = 1;
a6c727b2
DJ
11181 else if (attr == NULL && die->child == NULL
11182 && producer_is_realview (cu->producer))
11183 /* RealView does not output the required DW_AT_declaration
11184 on incomplete types. */
11185 TYPE_STUB (type) = 1;
dc718098 11186
c906108c
SS
11187 /* We need to add the type field to the die immediately so we don't
11188 infinitely recurse when dealing with pointers to the structure
0963b4bd 11189 type within the structure itself. */
1c379e20 11190 set_die_type (die, type, cu);
c906108c 11191
7e314c57
JK
11192 /* set_die_type should be already done. */
11193 set_descriptive_type (type, die, cu);
11194
c767944b
DJ
11195 return type;
11196}
11197
11198/* Finish creating a structure or union type, including filling in
11199 its members and creating a symbol for it. */
11200
11201static void
11202process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11203{
11204 struct objfile *objfile = cu->objfile;
11205 struct die_info *child_die = die->child;
11206 struct type *type;
11207
11208 type = get_die_type (die, cu);
11209 if (type == NULL)
11210 type = read_structure_type (die, cu);
11211
e142c38c 11212 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11213 {
11214 struct field_info fi;
11215 struct die_info *child_die;
34eaf542 11216 VEC (symbolp) *template_args = NULL;
c767944b 11217 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11218
11219 memset (&fi, 0, sizeof (struct field_info));
11220
639d11d3 11221 child_die = die->child;
c906108c
SS
11222
11223 while (child_die && child_die->tag)
11224 {
a9a9bd0f
DC
11225 if (child_die->tag == DW_TAG_member
11226 || child_die->tag == DW_TAG_variable)
c906108c 11227 {
a9a9bd0f
DC
11228 /* NOTE: carlton/2002-11-05: A C++ static data member
11229 should be a DW_TAG_member that is a declaration, but
11230 all versions of G++ as of this writing (so through at
11231 least 3.2.1) incorrectly generate DW_TAG_variable
11232 tags for them instead. */
e7c27a73 11233 dwarf2_add_field (&fi, child_die, cu);
c906108c 11234 }
8713b1b1 11235 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11236 {
0963b4bd 11237 /* C++ member function. */
e7c27a73 11238 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11239 }
11240 else if (child_die->tag == DW_TAG_inheritance)
11241 {
11242 /* C++ base class field. */
e7c27a73 11243 dwarf2_add_field (&fi, child_die, cu);
c906108c 11244 }
98751a41
JK
11245 else if (child_die->tag == DW_TAG_typedef)
11246 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11247 else if (child_die->tag == DW_TAG_template_type_param
11248 || child_die->tag == DW_TAG_template_value_param)
11249 {
11250 struct symbol *arg = new_symbol (child_die, NULL, cu);
11251
f1078f66
DJ
11252 if (arg != NULL)
11253 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11254 }
11255
c906108c
SS
11256 child_die = sibling_die (child_die);
11257 }
11258
34eaf542
TT
11259 /* Attach template arguments to type. */
11260 if (! VEC_empty (symbolp, template_args))
11261 {
11262 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11263 TYPE_N_TEMPLATE_ARGUMENTS (type)
11264 = VEC_length (symbolp, template_args);
11265 TYPE_TEMPLATE_ARGUMENTS (type)
11266 = obstack_alloc (&objfile->objfile_obstack,
11267 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11268 * sizeof (struct symbol *)));
11269 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11270 VEC_address (symbolp, template_args),
11271 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11272 * sizeof (struct symbol *)));
11273 VEC_free (symbolp, template_args);
11274 }
11275
c906108c
SS
11276 /* Attach fields and member functions to the type. */
11277 if (fi.nfields)
e7c27a73 11278 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11279 if (fi.nfnfields)
11280 {
e7c27a73 11281 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11282
c5aa993b 11283 /* Get the type which refers to the base class (possibly this
c906108c 11284 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11285 class from the DW_AT_containing_type attribute. This use of
11286 DW_AT_containing_type is a GNU extension. */
c906108c 11287
e142c38c 11288 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11289 {
e7c27a73 11290 struct type *t = die_containing_type (die, cu);
c906108c
SS
11291
11292 TYPE_VPTR_BASETYPE (type) = t;
11293 if (type == t)
11294 {
c906108c
SS
11295 int i;
11296
11297 /* Our own class provides vtbl ptr. */
11298 for (i = TYPE_NFIELDS (t) - 1;
11299 i >= TYPE_N_BASECLASSES (t);
11300 --i)
11301 {
0d5cff50 11302 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11303
1168df01 11304 if (is_vtable_name (fieldname, cu))
c906108c
SS
11305 {
11306 TYPE_VPTR_FIELDNO (type) = i;
11307 break;
11308 }
11309 }
11310
11311 /* Complain if virtual function table field not found. */
11312 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11313 complaint (&symfile_complaints,
3e43a32a
MS
11314 _("virtual function table pointer "
11315 "not found when defining class '%s'"),
4d3c2250
KB
11316 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11317 "");
c906108c
SS
11318 }
11319 else
11320 {
11321 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11322 }
11323 }
f6235d4c
EZ
11324 else if (cu->producer
11325 && strncmp (cu->producer,
11326 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11327 {
11328 /* The IBM XLC compiler does not provide direct indication
11329 of the containing type, but the vtable pointer is
11330 always named __vfp. */
11331
11332 int i;
11333
11334 for (i = TYPE_NFIELDS (type) - 1;
11335 i >= TYPE_N_BASECLASSES (type);
11336 --i)
11337 {
11338 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11339 {
11340 TYPE_VPTR_FIELDNO (type) = i;
11341 TYPE_VPTR_BASETYPE (type) = type;
11342 break;
11343 }
11344 }
11345 }
c906108c 11346 }
98751a41
JK
11347
11348 /* Copy fi.typedef_field_list linked list elements content into the
11349 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11350 if (fi.typedef_field_list)
11351 {
11352 int i = fi.typedef_field_list_count;
11353
a0d7a4ff 11354 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11355 TYPE_TYPEDEF_FIELD_ARRAY (type)
11356 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11357 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11358
11359 /* Reverse the list order to keep the debug info elements order. */
11360 while (--i >= 0)
11361 {
11362 struct typedef_field *dest, *src;
6e70227d 11363
98751a41
JK
11364 dest = &TYPE_TYPEDEF_FIELD (type, i);
11365 src = &fi.typedef_field_list->field;
11366 fi.typedef_field_list = fi.typedef_field_list->next;
11367 *dest = *src;
11368 }
11369 }
c767944b
DJ
11370
11371 do_cleanups (back_to);
eb2a6f42
TT
11372
11373 if (HAVE_CPLUS_STRUCT (type))
11374 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11375 }
63d06c5c 11376
bb5ed363 11377 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11378
90aeadfc
DC
11379 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11380 snapshots) has been known to create a die giving a declaration
11381 for a class that has, as a child, a die giving a definition for a
11382 nested class. So we have to process our children even if the
11383 current die is a declaration. Normally, of course, a declaration
11384 won't have any children at all. */
134d01f1 11385
90aeadfc
DC
11386 while (child_die != NULL && child_die->tag)
11387 {
11388 if (child_die->tag == DW_TAG_member
11389 || child_die->tag == DW_TAG_variable
34eaf542
TT
11390 || child_die->tag == DW_TAG_inheritance
11391 || child_die->tag == DW_TAG_template_value_param
11392 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11393 {
90aeadfc 11394 /* Do nothing. */
134d01f1 11395 }
90aeadfc
DC
11396 else
11397 process_die (child_die, cu);
134d01f1 11398
90aeadfc 11399 child_die = sibling_die (child_die);
134d01f1
DJ
11400 }
11401
fa4028e9
JB
11402 /* Do not consider external references. According to the DWARF standard,
11403 these DIEs are identified by the fact that they have no byte_size
11404 attribute, and a declaration attribute. */
11405 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11406 || !die_is_declaration (die, cu))
c767944b 11407 new_symbol (die, type, cu);
134d01f1
DJ
11408}
11409
11410/* Given a DW_AT_enumeration_type die, set its type. We do not
11411 complete the type's fields yet, or create any symbols. */
c906108c 11412
f792889a 11413static struct type *
134d01f1 11414read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11415{
e7c27a73 11416 struct objfile *objfile = cu->objfile;
c906108c 11417 struct type *type;
c906108c 11418 struct attribute *attr;
0114d602 11419 const char *name;
134d01f1 11420
348e048f
DE
11421 /* If the definition of this type lives in .debug_types, read that type.
11422 Don't follow DW_AT_specification though, that will take us back up
11423 the chain and we want to go down. */
45e58e77 11424 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11425 if (attr)
11426 {
11427 struct dwarf2_cu *type_cu = cu;
11428 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11429
348e048f 11430 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11431
11432 /* TYPE_CU may not be the same as CU.
11433 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11434 return set_die_type (die, type, cu);
11435 }
11436
c906108c
SS
11437 type = alloc_type (objfile);
11438
11439 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11440 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11441 if (name != NULL)
0114d602 11442 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11443
e142c38c 11444 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11445 if (attr)
11446 {
11447 TYPE_LENGTH (type) = DW_UNSND (attr);
11448 }
11449 else
11450 {
11451 TYPE_LENGTH (type) = 0;
11452 }
11453
137033e9
JB
11454 /* The enumeration DIE can be incomplete. In Ada, any type can be
11455 declared as private in the package spec, and then defined only
11456 inside the package body. Such types are known as Taft Amendment
11457 Types. When another package uses such a type, an incomplete DIE
11458 may be generated by the compiler. */
02eb380e 11459 if (die_is_declaration (die, cu))
876cecd0 11460 TYPE_STUB (type) = 1;
02eb380e 11461
f792889a 11462 return set_die_type (die, type, cu);
134d01f1
DJ
11463}
11464
11465/* Given a pointer to a die which begins an enumeration, process all
11466 the dies that define the members of the enumeration, and create the
11467 symbol for the enumeration type.
11468
11469 NOTE: We reverse the order of the element list. */
11470
11471static void
11472process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11473{
f792889a 11474 struct type *this_type;
134d01f1 11475
f792889a
DJ
11476 this_type = get_die_type (die, cu);
11477 if (this_type == NULL)
11478 this_type = read_enumeration_type (die, cu);
9dc481d3 11479
639d11d3 11480 if (die->child != NULL)
c906108c 11481 {
9dc481d3
DE
11482 struct die_info *child_die;
11483 struct symbol *sym;
11484 struct field *fields = NULL;
11485 int num_fields = 0;
11486 int unsigned_enum = 1;
11487 char *name;
cafec441
TT
11488 int flag_enum = 1;
11489 ULONGEST mask = 0;
9dc481d3 11490
639d11d3 11491 child_die = die->child;
c906108c
SS
11492 while (child_die && child_die->tag)
11493 {
11494 if (child_die->tag != DW_TAG_enumerator)
11495 {
e7c27a73 11496 process_die (child_die, cu);
c906108c
SS
11497 }
11498 else
11499 {
39cbfefa
DJ
11500 name = dwarf2_name (child_die, cu);
11501 if (name)
c906108c 11502 {
f792889a 11503 sym = new_symbol (child_die, this_type, cu);
c906108c 11504 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11505 {
11506 unsigned_enum = 0;
11507 flag_enum = 0;
11508 }
11509 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11510 flag_enum = 0;
11511 else
11512 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11513
11514 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11515 {
11516 fields = (struct field *)
11517 xrealloc (fields,
11518 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11519 * sizeof (struct field));
c906108c
SS
11520 }
11521
3567439c 11522 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11523 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11524 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11525 FIELD_BITSIZE (fields[num_fields]) = 0;
11526
11527 num_fields++;
11528 }
11529 }
11530
11531 child_die = sibling_die (child_die);
11532 }
11533
11534 if (num_fields)
11535 {
f792889a
DJ
11536 TYPE_NFIELDS (this_type) = num_fields;
11537 TYPE_FIELDS (this_type) = (struct field *)
11538 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11539 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11540 sizeof (struct field) * num_fields);
b8c9b27d 11541 xfree (fields);
c906108c
SS
11542 }
11543 if (unsigned_enum)
876cecd0 11544 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11545 if (flag_enum)
11546 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11547 }
134d01f1 11548
6c83ed52
TT
11549 /* If we are reading an enum from a .debug_types unit, and the enum
11550 is a declaration, and the enum is not the signatured type in the
11551 unit, then we do not want to add a symbol for it. Adding a
11552 symbol would in some cases obscure the true definition of the
11553 enum, giving users an incomplete type when the definition is
11554 actually available. Note that we do not want to do this for all
11555 enums which are just declarations, because C++0x allows forward
11556 enum declarations. */
3019eac3 11557 if (cu->per_cu->is_debug_types
6c83ed52
TT
11558 && die_is_declaration (die, cu))
11559 {
52dc124a 11560 struct signatured_type *sig_type;
6c83ed52 11561
52dc124a 11562 sig_type
6c83ed52 11563 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11564 cu->per_cu->info_or_types_section,
6c83ed52 11565 cu->per_cu->offset);
3019eac3
DE
11566 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11567 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11568 return;
11569 }
11570
f792889a 11571 new_symbol (die, this_type, cu);
c906108c
SS
11572}
11573
11574/* Extract all information from a DW_TAG_array_type DIE and put it in
11575 the DIE's type field. For now, this only handles one dimensional
11576 arrays. */
11577
f792889a 11578static struct type *
e7c27a73 11579read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11580{
e7c27a73 11581 struct objfile *objfile = cu->objfile;
c906108c 11582 struct die_info *child_die;
7e314c57 11583 struct type *type;
c906108c
SS
11584 struct type *element_type, *range_type, *index_type;
11585 struct type **range_types = NULL;
11586 struct attribute *attr;
11587 int ndim = 0;
11588 struct cleanup *back_to;
39cbfefa 11589 char *name;
c906108c 11590
e7c27a73 11591 element_type = die_type (die, cu);
c906108c 11592
7e314c57
JK
11593 /* The die_type call above may have already set the type for this DIE. */
11594 type = get_die_type (die, cu);
11595 if (type)
11596 return type;
11597
c906108c
SS
11598 /* Irix 6.2 native cc creates array types without children for
11599 arrays with unspecified length. */
639d11d3 11600 if (die->child == NULL)
c906108c 11601 {
46bf5051 11602 index_type = objfile_type (objfile)->builtin_int;
c906108c 11603 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11604 type = create_array_type (NULL, element_type, range_type);
11605 return set_die_type (die, type, cu);
c906108c
SS
11606 }
11607
11608 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11609 child_die = die->child;
c906108c
SS
11610 while (child_die && child_die->tag)
11611 {
11612 if (child_die->tag == DW_TAG_subrange_type)
11613 {
f792889a 11614 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11615
f792889a 11616 if (child_type != NULL)
a02abb62 11617 {
0963b4bd
MS
11618 /* The range type was succesfully read. Save it for the
11619 array type creation. */
a02abb62
JB
11620 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11621 {
11622 range_types = (struct type **)
11623 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11624 * sizeof (struct type *));
11625 if (ndim == 0)
11626 make_cleanup (free_current_contents, &range_types);
11627 }
f792889a 11628 range_types[ndim++] = child_type;
a02abb62 11629 }
c906108c
SS
11630 }
11631 child_die = sibling_die (child_die);
11632 }
11633
11634 /* Dwarf2 dimensions are output from left to right, create the
11635 necessary array types in backwards order. */
7ca2d3a3 11636
c906108c 11637 type = element_type;
7ca2d3a3
DL
11638
11639 if (read_array_order (die, cu) == DW_ORD_col_major)
11640 {
11641 int i = 0;
9a619af0 11642
7ca2d3a3
DL
11643 while (i < ndim)
11644 type = create_array_type (NULL, type, range_types[i++]);
11645 }
11646 else
11647 {
11648 while (ndim-- > 0)
11649 type = create_array_type (NULL, type, range_types[ndim]);
11650 }
c906108c 11651
f5f8a009
EZ
11652 /* Understand Dwarf2 support for vector types (like they occur on
11653 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11654 array type. This is not part of the Dwarf2/3 standard yet, but a
11655 custom vendor extension. The main difference between a regular
11656 array and the vector variant is that vectors are passed by value
11657 to functions. */
e142c38c 11658 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11659 if (attr)
ea37ba09 11660 make_vector_type (type);
f5f8a009 11661
dbc98a8b
KW
11662 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11663 implementation may choose to implement triple vectors using this
11664 attribute. */
11665 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11666 if (attr)
11667 {
11668 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11669 TYPE_LENGTH (type) = DW_UNSND (attr);
11670 else
3e43a32a
MS
11671 complaint (&symfile_complaints,
11672 _("DW_AT_byte_size for array type smaller "
11673 "than the total size of elements"));
dbc98a8b
KW
11674 }
11675
39cbfefa
DJ
11676 name = dwarf2_name (die, cu);
11677 if (name)
11678 TYPE_NAME (type) = name;
6e70227d 11679
0963b4bd 11680 /* Install the type in the die. */
7e314c57
JK
11681 set_die_type (die, type, cu);
11682
11683 /* set_die_type should be already done. */
b4ba55a1
JB
11684 set_descriptive_type (type, die, cu);
11685
c906108c
SS
11686 do_cleanups (back_to);
11687
7e314c57 11688 return type;
c906108c
SS
11689}
11690
7ca2d3a3 11691static enum dwarf_array_dim_ordering
6e70227d 11692read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11693{
11694 struct attribute *attr;
11695
11696 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11697
11698 if (attr) return DW_SND (attr);
11699
0963b4bd
MS
11700 /* GNU F77 is a special case, as at 08/2004 array type info is the
11701 opposite order to the dwarf2 specification, but data is still
11702 laid out as per normal fortran.
7ca2d3a3 11703
0963b4bd
MS
11704 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11705 version checking. */
7ca2d3a3 11706
905e0470
PM
11707 if (cu->language == language_fortran
11708 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11709 {
11710 return DW_ORD_row_major;
11711 }
11712
6e70227d 11713 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11714 {
11715 case array_column_major:
11716 return DW_ORD_col_major;
11717 case array_row_major:
11718 default:
11719 return DW_ORD_row_major;
11720 };
11721}
11722
72019c9c 11723/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11724 the DIE's type field. */
72019c9c 11725
f792889a 11726static struct type *
72019c9c
GM
11727read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11728{
7e314c57
JK
11729 struct type *domain_type, *set_type;
11730 struct attribute *attr;
f792889a 11731
7e314c57
JK
11732 domain_type = die_type (die, cu);
11733
11734 /* The die_type call above may have already set the type for this DIE. */
11735 set_type = get_die_type (die, cu);
11736 if (set_type)
11737 return set_type;
11738
11739 set_type = create_set_type (NULL, domain_type);
11740
11741 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11742 if (attr)
11743 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11744
f792889a 11745 return set_die_type (die, set_type, cu);
72019c9c 11746}
7ca2d3a3 11747
0971de02
TT
11748/* A helper for read_common_block that creates a locexpr baton.
11749 SYM is the symbol which we are marking as computed.
11750 COMMON_DIE is the DIE for the common block.
11751 COMMON_LOC is the location expression attribute for the common
11752 block itself.
11753 MEMBER_LOC is the location expression attribute for the particular
11754 member of the common block that we are processing.
11755 CU is the CU from which the above come. */
11756
11757static void
11758mark_common_block_symbol_computed (struct symbol *sym,
11759 struct die_info *common_die,
11760 struct attribute *common_loc,
11761 struct attribute *member_loc,
11762 struct dwarf2_cu *cu)
11763{
11764 struct objfile *objfile = dwarf2_per_objfile->objfile;
11765 struct dwarf2_locexpr_baton *baton;
11766 gdb_byte *ptr;
11767 unsigned int cu_off;
11768 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11769 LONGEST offset = 0;
11770
11771 gdb_assert (common_loc && member_loc);
11772 gdb_assert (attr_form_is_block (common_loc));
11773 gdb_assert (attr_form_is_block (member_loc)
11774 || attr_form_is_constant (member_loc));
11775
11776 baton = obstack_alloc (&objfile->objfile_obstack,
11777 sizeof (struct dwarf2_locexpr_baton));
11778 baton->per_cu = cu->per_cu;
11779 gdb_assert (baton->per_cu);
11780
11781 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11782
11783 if (attr_form_is_constant (member_loc))
11784 {
11785 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11786 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11787 }
11788 else
11789 baton->size += DW_BLOCK (member_loc)->size;
11790
11791 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11792 baton->data = ptr;
11793
11794 *ptr++ = DW_OP_call4;
11795 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11796 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11797 ptr += 4;
11798
11799 if (attr_form_is_constant (member_loc))
11800 {
11801 *ptr++ = DW_OP_addr;
11802 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11803 ptr += cu->header.addr_size;
11804 }
11805 else
11806 {
11807 /* We have to copy the data here, because DW_OP_call4 will only
11808 use a DW_AT_location attribute. */
11809 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11810 ptr += DW_BLOCK (member_loc)->size;
11811 }
11812
11813 *ptr++ = DW_OP_plus;
11814 gdb_assert (ptr - baton->data == baton->size);
11815
11816 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11817 SYMBOL_LOCATION_BATON (sym) = baton;
11818 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11819}
11820
4357ac6c
TT
11821/* Create appropriate locally-scoped variables for all the
11822 DW_TAG_common_block entries. Also create a struct common_block
11823 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11824 is used to sepate the common blocks name namespace from regular
11825 variable names. */
c906108c
SS
11826
11827static void
e7c27a73 11828read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11829{
0971de02
TT
11830 struct attribute *attr;
11831
11832 attr = dwarf2_attr (die, DW_AT_location, cu);
11833 if (attr)
11834 {
11835 /* Support the .debug_loc offsets. */
11836 if (attr_form_is_block (attr))
11837 {
11838 /* Ok. */
11839 }
11840 else if (attr_form_is_section_offset (attr))
11841 {
11842 dwarf2_complex_location_expr_complaint ();
11843 attr = NULL;
11844 }
11845 else
11846 {
11847 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11848 "common block member");
11849 attr = NULL;
11850 }
11851 }
11852
639d11d3 11853 if (die->child != NULL)
c906108c 11854 {
4357ac6c
TT
11855 struct objfile *objfile = cu->objfile;
11856 struct die_info *child_die;
11857 size_t n_entries = 0, size;
11858 struct common_block *common_block;
11859 struct symbol *sym;
74ac6d43 11860
4357ac6c
TT
11861 for (child_die = die->child;
11862 child_die && child_die->tag;
11863 child_die = sibling_die (child_die))
11864 ++n_entries;
11865
11866 size = (sizeof (struct common_block)
11867 + (n_entries - 1) * sizeof (struct symbol *));
11868 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11869 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11870 common_block->n_entries = 0;
11871
11872 for (child_die = die->child;
11873 child_die && child_die->tag;
11874 child_die = sibling_die (child_die))
11875 {
11876 /* Create the symbol in the DW_TAG_common_block block in the current
11877 symbol scope. */
e7c27a73 11878 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11879 if (sym != NULL)
11880 {
11881 struct attribute *member_loc;
11882
11883 common_block->contents[common_block->n_entries++] = sym;
11884
11885 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11886 cu);
11887 if (member_loc)
11888 {
11889 /* GDB has handled this for a long time, but it is
11890 not specified by DWARF. It seems to have been
11891 emitted by gfortran at least as recently as:
11892 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11893 complaint (&symfile_complaints,
11894 _("Variable in common block has "
11895 "DW_AT_data_member_location "
11896 "- DIE at 0x%x [in module %s]"),
11897 child_die->offset.sect_off, cu->objfile->name);
11898
11899 if (attr_form_is_section_offset (member_loc))
11900 dwarf2_complex_location_expr_complaint ();
11901 else if (attr_form_is_constant (member_loc)
11902 || attr_form_is_block (member_loc))
11903 {
11904 if (attr)
11905 mark_common_block_symbol_computed (sym, die, attr,
11906 member_loc, cu);
11907 }
11908 else
11909 dwarf2_complex_location_expr_complaint ();
11910 }
11911 }
c906108c 11912 }
4357ac6c
TT
11913
11914 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11915 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
11916 }
11917}
11918
0114d602 11919/* Create a type for a C++ namespace. */
d9fa45fe 11920
0114d602
DJ
11921static struct type *
11922read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11923{
e7c27a73 11924 struct objfile *objfile = cu->objfile;
0114d602 11925 const char *previous_prefix, *name;
9219021c 11926 int is_anonymous;
0114d602
DJ
11927 struct type *type;
11928
11929 /* For extensions, reuse the type of the original namespace. */
11930 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11931 {
11932 struct die_info *ext_die;
11933 struct dwarf2_cu *ext_cu = cu;
9a619af0 11934
0114d602
DJ
11935 ext_die = dwarf2_extension (die, &ext_cu);
11936 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11937
11938 /* EXT_CU may not be the same as CU.
11939 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11940 return set_die_type (die, type, cu);
11941 }
9219021c 11942
e142c38c 11943 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11944
11945 /* Now build the name of the current namespace. */
11946
0114d602
DJ
11947 previous_prefix = determine_prefix (die, cu);
11948 if (previous_prefix[0] != '\0')
11949 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11950 previous_prefix, name, 0, cu);
0114d602
DJ
11951
11952 /* Create the type. */
11953 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11954 objfile);
11955 TYPE_NAME (type) = (char *) name;
11956 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11957
60531b24 11958 return set_die_type (die, type, cu);
0114d602
DJ
11959}
11960
11961/* Read a C++ namespace. */
11962
11963static void
11964read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11965{
11966 struct objfile *objfile = cu->objfile;
0114d602 11967 int is_anonymous;
9219021c 11968
5c4e30ca
DC
11969 /* Add a symbol associated to this if we haven't seen the namespace
11970 before. Also, add a using directive if it's an anonymous
11971 namespace. */
9219021c 11972
f2f0e013 11973 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
11974 {
11975 struct type *type;
11976
0114d602 11977 type = read_type_die (die, cu);
e7c27a73 11978 new_symbol (die, type, cu);
5c4e30ca 11979
e8e80198 11980 namespace_name (die, &is_anonymous, cu);
5c4e30ca 11981 if (is_anonymous)
0114d602
DJ
11982 {
11983 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 11984
c0cc3a76 11985 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 11986 NULL, NULL, &objfile->objfile_obstack);
0114d602 11987 }
5c4e30ca 11988 }
9219021c 11989
639d11d3 11990 if (die->child != NULL)
d9fa45fe 11991 {
639d11d3 11992 struct die_info *child_die = die->child;
6e70227d 11993
d9fa45fe
DC
11994 while (child_die && child_die->tag)
11995 {
e7c27a73 11996 process_die (child_die, cu);
d9fa45fe
DC
11997 child_die = sibling_die (child_die);
11998 }
11999 }
38d518c9
EZ
12000}
12001
f55ee35c
JK
12002/* Read a Fortran module as type. This DIE can be only a declaration used for
12003 imported module. Still we need that type as local Fortran "use ... only"
12004 declaration imports depend on the created type in determine_prefix. */
12005
12006static struct type *
12007read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12008{
12009 struct objfile *objfile = cu->objfile;
12010 char *module_name;
12011 struct type *type;
12012
12013 module_name = dwarf2_name (die, cu);
12014 if (!module_name)
3e43a32a
MS
12015 complaint (&symfile_complaints,
12016 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12017 die->offset.sect_off);
f55ee35c
JK
12018 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12019
12020 /* determine_prefix uses TYPE_TAG_NAME. */
12021 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12022
12023 return set_die_type (die, type, cu);
12024}
12025
5d7cb8df
JK
12026/* Read a Fortran module. */
12027
12028static void
12029read_module (struct die_info *die, struct dwarf2_cu *cu)
12030{
12031 struct die_info *child_die = die->child;
12032
5d7cb8df
JK
12033 while (child_die && child_die->tag)
12034 {
12035 process_die (child_die, cu);
12036 child_die = sibling_die (child_die);
12037 }
12038}
12039
38d518c9
EZ
12040/* Return the name of the namespace represented by DIE. Set
12041 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12042 namespace. */
12043
12044static const char *
e142c38c 12045namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12046{
12047 struct die_info *current_die;
12048 const char *name = NULL;
12049
12050 /* Loop through the extensions until we find a name. */
12051
12052 for (current_die = die;
12053 current_die != NULL;
f2f0e013 12054 current_die = dwarf2_extension (die, &cu))
38d518c9 12055 {
e142c38c 12056 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12057 if (name != NULL)
12058 break;
12059 }
12060
12061 /* Is it an anonymous namespace? */
12062
12063 *is_anonymous = (name == NULL);
12064 if (*is_anonymous)
2b1dbab0 12065 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12066
12067 return name;
d9fa45fe
DC
12068}
12069
c906108c
SS
12070/* Extract all information from a DW_TAG_pointer_type DIE and add to
12071 the user defined type vector. */
12072
f792889a 12073static struct type *
e7c27a73 12074read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12075{
5e2b427d 12076 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12077 struct comp_unit_head *cu_header = &cu->header;
c906108c 12078 struct type *type;
8b2dbe47
KB
12079 struct attribute *attr_byte_size;
12080 struct attribute *attr_address_class;
12081 int byte_size, addr_class;
7e314c57
JK
12082 struct type *target_type;
12083
12084 target_type = die_type (die, cu);
c906108c 12085
7e314c57
JK
12086 /* The die_type call above may have already set the type for this DIE. */
12087 type = get_die_type (die, cu);
12088 if (type)
12089 return type;
12090
12091 type = lookup_pointer_type (target_type);
8b2dbe47 12092
e142c38c 12093 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12094 if (attr_byte_size)
12095 byte_size = DW_UNSND (attr_byte_size);
c906108c 12096 else
8b2dbe47
KB
12097 byte_size = cu_header->addr_size;
12098
e142c38c 12099 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12100 if (attr_address_class)
12101 addr_class = DW_UNSND (attr_address_class);
12102 else
12103 addr_class = DW_ADDR_none;
12104
12105 /* If the pointer size or address class is different than the
12106 default, create a type variant marked as such and set the
12107 length accordingly. */
12108 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12109 {
5e2b427d 12110 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12111 {
12112 int type_flags;
12113
849957d9 12114 type_flags = gdbarch_address_class_type_flags
5e2b427d 12115 (gdbarch, byte_size, addr_class);
876cecd0
TT
12116 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12117 == 0);
8b2dbe47
KB
12118 type = make_type_with_address_space (type, type_flags);
12119 }
12120 else if (TYPE_LENGTH (type) != byte_size)
12121 {
3e43a32a
MS
12122 complaint (&symfile_complaints,
12123 _("invalid pointer size %d"), byte_size);
8b2dbe47 12124 }
6e70227d 12125 else
9a619af0
MS
12126 {
12127 /* Should we also complain about unhandled address classes? */
12128 }
c906108c 12129 }
8b2dbe47
KB
12130
12131 TYPE_LENGTH (type) = byte_size;
f792889a 12132 return set_die_type (die, type, cu);
c906108c
SS
12133}
12134
12135/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12136 the user defined type vector. */
12137
f792889a 12138static struct type *
e7c27a73 12139read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12140{
12141 struct type *type;
12142 struct type *to_type;
12143 struct type *domain;
12144
e7c27a73
DJ
12145 to_type = die_type (die, cu);
12146 domain = die_containing_type (die, cu);
0d5de010 12147
7e314c57
JK
12148 /* The calls above may have already set the type for this DIE. */
12149 type = get_die_type (die, cu);
12150 if (type)
12151 return type;
12152
0d5de010
DJ
12153 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12154 type = lookup_methodptr_type (to_type);
12155 else
12156 type = lookup_memberptr_type (to_type, domain);
c906108c 12157
f792889a 12158 return set_die_type (die, type, cu);
c906108c
SS
12159}
12160
12161/* Extract all information from a DW_TAG_reference_type DIE and add to
12162 the user defined type vector. */
12163
f792889a 12164static struct type *
e7c27a73 12165read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12166{
e7c27a73 12167 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12168 struct type *type, *target_type;
c906108c
SS
12169 struct attribute *attr;
12170
7e314c57
JK
12171 target_type = die_type (die, cu);
12172
12173 /* The die_type call above may have already set the type for this DIE. */
12174 type = get_die_type (die, cu);
12175 if (type)
12176 return type;
12177
12178 type = lookup_reference_type (target_type);
e142c38c 12179 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12180 if (attr)
12181 {
12182 TYPE_LENGTH (type) = DW_UNSND (attr);
12183 }
12184 else
12185 {
107d2387 12186 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12187 }
f792889a 12188 return set_die_type (die, type, cu);
c906108c
SS
12189}
12190
f792889a 12191static struct type *
e7c27a73 12192read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12193{
f792889a 12194 struct type *base_type, *cv_type;
c906108c 12195
e7c27a73 12196 base_type = die_type (die, cu);
7e314c57
JK
12197
12198 /* The die_type call above may have already set the type for this DIE. */
12199 cv_type = get_die_type (die, cu);
12200 if (cv_type)
12201 return cv_type;
12202
2f608a3a
KW
12203 /* In case the const qualifier is applied to an array type, the element type
12204 is so qualified, not the array type (section 6.7.3 of C99). */
12205 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12206 {
12207 struct type *el_type, *inner_array;
12208
12209 base_type = copy_type (base_type);
12210 inner_array = base_type;
12211
12212 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12213 {
12214 TYPE_TARGET_TYPE (inner_array) =
12215 copy_type (TYPE_TARGET_TYPE (inner_array));
12216 inner_array = TYPE_TARGET_TYPE (inner_array);
12217 }
12218
12219 el_type = TYPE_TARGET_TYPE (inner_array);
12220 TYPE_TARGET_TYPE (inner_array) =
12221 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12222
12223 return set_die_type (die, base_type, cu);
12224 }
12225
f792889a
DJ
12226 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12227 return set_die_type (die, cv_type, cu);
c906108c
SS
12228}
12229
f792889a 12230static struct type *
e7c27a73 12231read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12232{
f792889a 12233 struct type *base_type, *cv_type;
c906108c 12234
e7c27a73 12235 base_type = die_type (die, cu);
7e314c57
JK
12236
12237 /* The die_type call above may have already set the type for this DIE. */
12238 cv_type = get_die_type (die, cu);
12239 if (cv_type)
12240 return cv_type;
12241
f792889a
DJ
12242 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12243 return set_die_type (die, cv_type, cu);
c906108c
SS
12244}
12245
12246/* Extract all information from a DW_TAG_string_type DIE and add to
12247 the user defined type vector. It isn't really a user defined type,
12248 but it behaves like one, with other DIE's using an AT_user_def_type
12249 attribute to reference it. */
12250
f792889a 12251static struct type *
e7c27a73 12252read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12253{
e7c27a73 12254 struct objfile *objfile = cu->objfile;
3b7538c0 12255 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12256 struct type *type, *range_type, *index_type, *char_type;
12257 struct attribute *attr;
12258 unsigned int length;
12259
e142c38c 12260 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12261 if (attr)
12262 {
12263 length = DW_UNSND (attr);
12264 }
12265 else
12266 {
0963b4bd 12267 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12268 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12269 if (attr)
12270 {
12271 length = DW_UNSND (attr);
12272 }
12273 else
12274 {
12275 length = 1;
12276 }
c906108c 12277 }
6ccb9162 12278
46bf5051 12279 index_type = objfile_type (objfile)->builtin_int;
c906108c 12280 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12281 char_type = language_string_char_type (cu->language_defn, gdbarch);
12282 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12283
f792889a 12284 return set_die_type (die, type, cu);
c906108c
SS
12285}
12286
12287/* Handle DIES due to C code like:
12288
12289 struct foo
c5aa993b
JM
12290 {
12291 int (*funcp)(int a, long l);
12292 int b;
12293 };
c906108c 12294
0963b4bd 12295 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12296
f792889a 12297static struct type *
e7c27a73 12298read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12299{
bb5ed363 12300 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12301 struct type *type; /* Type that this function returns. */
12302 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12303 struct attribute *attr;
12304
e7c27a73 12305 type = die_type (die, cu);
7e314c57
JK
12306
12307 /* The die_type call above may have already set the type for this DIE. */
12308 ftype = get_die_type (die, cu);
12309 if (ftype)
12310 return ftype;
12311
0c8b41f1 12312 ftype = lookup_function_type (type);
c906108c 12313
5b8101ae 12314 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12315 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12316 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12317 || cu->language == language_cplus
5b8101ae
PM
12318 || cu->language == language_java
12319 || cu->language == language_pascal)
876cecd0 12320 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12321 else if (producer_is_realview (cu->producer))
12322 /* RealView does not emit DW_AT_prototyped. We can not
12323 distinguish prototyped and unprototyped functions; default to
12324 prototyped, since that is more common in modern code (and
12325 RealView warns about unprototyped functions). */
12326 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12327
c055b101
CV
12328 /* Store the calling convention in the type if it's available in
12329 the subroutine die. Otherwise set the calling convention to
12330 the default value DW_CC_normal. */
12331 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12332 if (attr)
12333 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12334 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12335 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12336 else
12337 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12338
12339 /* We need to add the subroutine type to the die immediately so
12340 we don't infinitely recurse when dealing with parameters
0963b4bd 12341 declared as the same subroutine type. */
76c10ea2 12342 set_die_type (die, ftype, cu);
6e70227d 12343
639d11d3 12344 if (die->child != NULL)
c906108c 12345 {
bb5ed363 12346 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12347 struct die_info *child_die;
8072405b 12348 int nparams, iparams;
c906108c
SS
12349
12350 /* Count the number of parameters.
12351 FIXME: GDB currently ignores vararg functions, but knows about
12352 vararg member functions. */
8072405b 12353 nparams = 0;
639d11d3 12354 child_die = die->child;
c906108c
SS
12355 while (child_die && child_die->tag)
12356 {
12357 if (child_die->tag == DW_TAG_formal_parameter)
12358 nparams++;
12359 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12360 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12361 child_die = sibling_die (child_die);
12362 }
12363
12364 /* Allocate storage for parameters and fill them in. */
12365 TYPE_NFIELDS (ftype) = nparams;
12366 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12367 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12368
8072405b
JK
12369 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12370 even if we error out during the parameters reading below. */
12371 for (iparams = 0; iparams < nparams; iparams++)
12372 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12373
12374 iparams = 0;
639d11d3 12375 child_die = die->child;
c906108c
SS
12376 while (child_die && child_die->tag)
12377 {
12378 if (child_die->tag == DW_TAG_formal_parameter)
12379 {
3ce3b1ba
PA
12380 struct type *arg_type;
12381
12382 /* DWARF version 2 has no clean way to discern C++
12383 static and non-static member functions. G++ helps
12384 GDB by marking the first parameter for non-static
12385 member functions (which is the this pointer) as
12386 artificial. We pass this information to
12387 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12388
12389 DWARF version 3 added DW_AT_object_pointer, which GCC
12390 4.5 does not yet generate. */
e142c38c 12391 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12392 if (attr)
12393 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12394 else
418835cc
KS
12395 {
12396 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12397
12398 /* GCC/43521: In java, the formal parameter
12399 "this" is sometimes not marked with DW_AT_artificial. */
12400 if (cu->language == language_java)
12401 {
12402 const char *name = dwarf2_name (child_die, cu);
9a619af0 12403
418835cc
KS
12404 if (name && !strcmp (name, "this"))
12405 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12406 }
12407 }
3ce3b1ba
PA
12408 arg_type = die_type (child_die, cu);
12409
12410 /* RealView does not mark THIS as const, which the testsuite
12411 expects. GCC marks THIS as const in method definitions,
12412 but not in the class specifications (GCC PR 43053). */
12413 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12414 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12415 {
12416 int is_this = 0;
12417 struct dwarf2_cu *arg_cu = cu;
12418 const char *name = dwarf2_name (child_die, cu);
12419
12420 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12421 if (attr)
12422 {
12423 /* If the compiler emits this, use it. */
12424 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12425 is_this = 1;
12426 }
12427 else if (name && strcmp (name, "this") == 0)
12428 /* Function definitions will have the argument names. */
12429 is_this = 1;
12430 else if (name == NULL && iparams == 0)
12431 /* Declarations may not have the names, so like
12432 elsewhere in GDB, assume an artificial first
12433 argument is "this". */
12434 is_this = 1;
12435
12436 if (is_this)
12437 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12438 arg_type, 0);
12439 }
12440
12441 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12442 iparams++;
12443 }
12444 child_die = sibling_die (child_die);
12445 }
12446 }
12447
76c10ea2 12448 return ftype;
c906108c
SS
12449}
12450
f792889a 12451static struct type *
e7c27a73 12452read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12453{
e7c27a73 12454 struct objfile *objfile = cu->objfile;
0114d602 12455 const char *name = NULL;
3c8e0968 12456 struct type *this_type, *target_type;
c906108c 12457
94af9270 12458 name = dwarf2_full_name (NULL, die, cu);
f792889a 12459 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12460 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12461 TYPE_NAME (this_type) = (char *) name;
f792889a 12462 set_die_type (die, this_type, cu);
3c8e0968
DE
12463 target_type = die_type (die, cu);
12464 if (target_type != this_type)
12465 TYPE_TARGET_TYPE (this_type) = target_type;
12466 else
12467 {
12468 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12469 spec and cause infinite loops in GDB. */
12470 complaint (&symfile_complaints,
12471 _("Self-referential DW_TAG_typedef "
12472 "- DIE at 0x%x [in module %s]"),
b64f50a1 12473 die->offset.sect_off, objfile->name);
3c8e0968
DE
12474 TYPE_TARGET_TYPE (this_type) = NULL;
12475 }
f792889a 12476 return this_type;
c906108c
SS
12477}
12478
12479/* Find a representation of a given base type and install
12480 it in the TYPE field of the die. */
12481
f792889a 12482static struct type *
e7c27a73 12483read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12484{
e7c27a73 12485 struct objfile *objfile = cu->objfile;
c906108c
SS
12486 struct type *type;
12487 struct attribute *attr;
12488 int encoding = 0, size = 0;
39cbfefa 12489 char *name;
6ccb9162
UW
12490 enum type_code code = TYPE_CODE_INT;
12491 int type_flags = 0;
12492 struct type *target_type = NULL;
c906108c 12493
e142c38c 12494 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12495 if (attr)
12496 {
12497 encoding = DW_UNSND (attr);
12498 }
e142c38c 12499 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12500 if (attr)
12501 {
12502 size = DW_UNSND (attr);
12503 }
39cbfefa 12504 name = dwarf2_name (die, cu);
6ccb9162 12505 if (!name)
c906108c 12506 {
6ccb9162
UW
12507 complaint (&symfile_complaints,
12508 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12509 }
6ccb9162
UW
12510
12511 switch (encoding)
c906108c 12512 {
6ccb9162
UW
12513 case DW_ATE_address:
12514 /* Turn DW_ATE_address into a void * pointer. */
12515 code = TYPE_CODE_PTR;
12516 type_flags |= TYPE_FLAG_UNSIGNED;
12517 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12518 break;
12519 case DW_ATE_boolean:
12520 code = TYPE_CODE_BOOL;
12521 type_flags |= TYPE_FLAG_UNSIGNED;
12522 break;
12523 case DW_ATE_complex_float:
12524 code = TYPE_CODE_COMPLEX;
12525 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12526 break;
12527 case DW_ATE_decimal_float:
12528 code = TYPE_CODE_DECFLOAT;
12529 break;
12530 case DW_ATE_float:
12531 code = TYPE_CODE_FLT;
12532 break;
12533 case DW_ATE_signed:
12534 break;
12535 case DW_ATE_unsigned:
12536 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12537 if (cu->language == language_fortran
12538 && name
12539 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12540 code = TYPE_CODE_CHAR;
6ccb9162
UW
12541 break;
12542 case DW_ATE_signed_char:
6e70227d 12543 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12544 || cu->language == language_pascal
12545 || cu->language == language_fortran)
6ccb9162
UW
12546 code = TYPE_CODE_CHAR;
12547 break;
12548 case DW_ATE_unsigned_char:
868a0084 12549 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12550 || cu->language == language_pascal
12551 || cu->language == language_fortran)
6ccb9162
UW
12552 code = TYPE_CODE_CHAR;
12553 type_flags |= TYPE_FLAG_UNSIGNED;
12554 break;
75079b2b
TT
12555 case DW_ATE_UTF:
12556 /* We just treat this as an integer and then recognize the
12557 type by name elsewhere. */
12558 break;
12559
6ccb9162
UW
12560 default:
12561 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12562 dwarf_type_encoding_name (encoding));
12563 break;
c906108c 12564 }
6ccb9162 12565
0114d602
DJ
12566 type = init_type (code, size, type_flags, NULL, objfile);
12567 TYPE_NAME (type) = name;
6ccb9162
UW
12568 TYPE_TARGET_TYPE (type) = target_type;
12569
0114d602 12570 if (name && strcmp (name, "char") == 0)
876cecd0 12571 TYPE_NOSIGN (type) = 1;
0114d602 12572
f792889a 12573 return set_die_type (die, type, cu);
c906108c
SS
12574}
12575
a02abb62
JB
12576/* Read the given DW_AT_subrange DIE. */
12577
f792889a 12578static struct type *
a02abb62
JB
12579read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12580{
12581 struct type *base_type;
12582 struct type *range_type;
12583 struct attribute *attr;
4fae6e18
JK
12584 LONGEST low, high;
12585 int low_default_is_valid;
39cbfefa 12586 char *name;
43bbcdc2 12587 LONGEST negative_mask;
e77813c8 12588
a02abb62 12589 base_type = die_type (die, cu);
953ac07e
JK
12590 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12591 check_typedef (base_type);
a02abb62 12592
7e314c57
JK
12593 /* The die_type call above may have already set the type for this DIE. */
12594 range_type = get_die_type (die, cu);
12595 if (range_type)
12596 return range_type;
12597
4fae6e18
JK
12598 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12599 omitting DW_AT_lower_bound. */
12600 switch (cu->language)
6e70227d 12601 {
4fae6e18
JK
12602 case language_c:
12603 case language_cplus:
12604 low = 0;
12605 low_default_is_valid = 1;
12606 break;
12607 case language_fortran:
12608 low = 1;
12609 low_default_is_valid = 1;
12610 break;
12611 case language_d:
12612 case language_java:
12613 case language_objc:
12614 low = 0;
12615 low_default_is_valid = (cu->header.version >= 4);
12616 break;
12617 case language_ada:
12618 case language_m2:
12619 case language_pascal:
a02abb62 12620 low = 1;
4fae6e18
JK
12621 low_default_is_valid = (cu->header.version >= 4);
12622 break;
12623 default:
12624 low = 0;
12625 low_default_is_valid = 0;
12626 break;
a02abb62
JB
12627 }
12628
dd5e6932
DJ
12629 /* FIXME: For variable sized arrays either of these could be
12630 a variable rather than a constant value. We'll allow it,
12631 but we don't know how to handle it. */
e142c38c 12632 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12633 if (attr)
4fae6e18
JK
12634 low = dwarf2_get_attr_constant_value (attr, low);
12635 else if (!low_default_is_valid)
12636 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12637 "- DIE at 0x%x [in module %s]"),
12638 die->offset.sect_off, cu->objfile->name);
a02abb62 12639
e142c38c 12640 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12641 if (attr)
6e70227d 12642 {
d48323d8 12643 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12644 {
12645 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12646 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12647 FIXME: GDB does not yet know how to handle dynamic
12648 arrays properly, treat them as arrays with unspecified
12649 length for now.
12650
12651 FIXME: jimb/2003-09-22: GDB does not really know
12652 how to handle arrays of unspecified length
12653 either; we just represent them as zero-length
12654 arrays. Choose an appropriate upper bound given
12655 the lower bound we've computed above. */
12656 high = low - 1;
12657 }
12658 else
12659 high = dwarf2_get_attr_constant_value (attr, 1);
12660 }
e77813c8
PM
12661 else
12662 {
12663 attr = dwarf2_attr (die, DW_AT_count, cu);
12664 if (attr)
12665 {
12666 int count = dwarf2_get_attr_constant_value (attr, 1);
12667 high = low + count - 1;
12668 }
c2ff108b
JK
12669 else
12670 {
12671 /* Unspecified array length. */
12672 high = low - 1;
12673 }
e77813c8
PM
12674 }
12675
12676 /* Dwarf-2 specifications explicitly allows to create subrange types
12677 without specifying a base type.
12678 In that case, the base type must be set to the type of
12679 the lower bound, upper bound or count, in that order, if any of these
12680 three attributes references an object that has a type.
12681 If no base type is found, the Dwarf-2 specifications say that
12682 a signed integer type of size equal to the size of an address should
12683 be used.
12684 For the following C code: `extern char gdb_int [];'
12685 GCC produces an empty range DIE.
12686 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12687 high bound or count are not yet handled by this code. */
e77813c8
PM
12688 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12689 {
12690 struct objfile *objfile = cu->objfile;
12691 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12692 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12693 struct type *int_type = objfile_type (objfile)->builtin_int;
12694
12695 /* Test "int", "long int", and "long long int" objfile types,
12696 and select the first one having a size above or equal to the
12697 architecture address size. */
12698 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12699 base_type = int_type;
12700 else
12701 {
12702 int_type = objfile_type (objfile)->builtin_long;
12703 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12704 base_type = int_type;
12705 else
12706 {
12707 int_type = objfile_type (objfile)->builtin_long_long;
12708 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12709 base_type = int_type;
12710 }
12711 }
12712 }
a02abb62 12713
6e70227d 12714 negative_mask =
43bbcdc2
PH
12715 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12716 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12717 low |= negative_mask;
12718 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12719 high |= negative_mask;
12720
a02abb62
JB
12721 range_type = create_range_type (NULL, base_type, low, high);
12722
bbb0eef6
JK
12723 /* Mark arrays with dynamic length at least as an array of unspecified
12724 length. GDB could check the boundary but before it gets implemented at
12725 least allow accessing the array elements. */
d48323d8 12726 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12727 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12728
c2ff108b
JK
12729 /* Ada expects an empty array on no boundary attributes. */
12730 if (attr == NULL && cu->language != language_ada)
12731 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12732
39cbfefa
DJ
12733 name = dwarf2_name (die, cu);
12734 if (name)
12735 TYPE_NAME (range_type) = name;
6e70227d 12736
e142c38c 12737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12738 if (attr)
12739 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12740
7e314c57
JK
12741 set_die_type (die, range_type, cu);
12742
12743 /* set_die_type should be already done. */
b4ba55a1
JB
12744 set_descriptive_type (range_type, die, cu);
12745
7e314c57 12746 return range_type;
a02abb62 12747}
6e70227d 12748
f792889a 12749static struct type *
81a17f79
JB
12750read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12751{
12752 struct type *type;
81a17f79 12753
81a17f79
JB
12754 /* For now, we only support the C meaning of an unspecified type: void. */
12755
0114d602
DJ
12756 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12757 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12758
f792889a 12759 return set_die_type (die, type, cu);
81a17f79 12760}
a02abb62 12761
639d11d3
DC
12762/* Read a single die and all its descendents. Set the die's sibling
12763 field to NULL; set other fields in the die correctly, and set all
12764 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12765 location of the info_ptr after reading all of those dies. PARENT
12766 is the parent of the die in question. */
12767
12768static struct die_info *
dee91e82
DE
12769read_die_and_children (const struct die_reader_specs *reader,
12770 gdb_byte *info_ptr,
12771 gdb_byte **new_info_ptr,
12772 struct die_info *parent)
639d11d3
DC
12773{
12774 struct die_info *die;
fe1b8b76 12775 gdb_byte *cur_ptr;
639d11d3
DC
12776 int has_children;
12777
93311388 12778 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12779 if (die == NULL)
12780 {
12781 *new_info_ptr = cur_ptr;
12782 return NULL;
12783 }
93311388 12784 store_in_ref_table (die, reader->cu);
639d11d3
DC
12785
12786 if (has_children)
348e048f 12787 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12788 else
12789 {
12790 die->child = NULL;
12791 *new_info_ptr = cur_ptr;
12792 }
12793
12794 die->sibling = NULL;
12795 die->parent = parent;
12796 return die;
12797}
12798
12799/* Read a die, all of its descendents, and all of its siblings; set
12800 all of the fields of all of the dies correctly. Arguments are as
12801 in read_die_and_children. */
12802
12803static struct die_info *
93311388
DE
12804read_die_and_siblings (const struct die_reader_specs *reader,
12805 gdb_byte *info_ptr,
fe1b8b76 12806 gdb_byte **new_info_ptr,
639d11d3
DC
12807 struct die_info *parent)
12808{
12809 struct die_info *first_die, *last_sibling;
fe1b8b76 12810 gdb_byte *cur_ptr;
639d11d3 12811
c906108c 12812 cur_ptr = info_ptr;
639d11d3
DC
12813 first_die = last_sibling = NULL;
12814
12815 while (1)
c906108c 12816 {
639d11d3 12817 struct die_info *die
dee91e82 12818 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12819
1d325ec1 12820 if (die == NULL)
c906108c 12821 {
639d11d3
DC
12822 *new_info_ptr = cur_ptr;
12823 return first_die;
c906108c 12824 }
1d325ec1
DJ
12825
12826 if (!first_die)
12827 first_die = die;
c906108c 12828 else
1d325ec1
DJ
12829 last_sibling->sibling = die;
12830
12831 last_sibling = die;
c906108c 12832 }
c906108c
SS
12833}
12834
3019eac3
DE
12835/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12836 attributes.
12837 The caller is responsible for filling in the extra attributes
12838 and updating (*DIEP)->num_attrs.
12839 Set DIEP to point to a newly allocated die with its information,
12840 except for its child, sibling, and parent fields.
12841 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12842
12843static gdb_byte *
3019eac3
DE
12844read_full_die_1 (const struct die_reader_specs *reader,
12845 struct die_info **diep, gdb_byte *info_ptr,
12846 int *has_children, int num_extra_attrs)
93311388 12847{
b64f50a1
JK
12848 unsigned int abbrev_number, bytes_read, i;
12849 sect_offset offset;
93311388
DE
12850 struct abbrev_info *abbrev;
12851 struct die_info *die;
12852 struct dwarf2_cu *cu = reader->cu;
12853 bfd *abfd = reader->abfd;
12854
b64f50a1 12855 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12856 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12857 info_ptr += bytes_read;
12858 if (!abbrev_number)
12859 {
12860 *diep = NULL;
12861 *has_children = 0;
12862 return info_ptr;
12863 }
12864
433df2d4 12865 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12866 if (!abbrev)
348e048f
DE
12867 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12868 abbrev_number,
12869 bfd_get_filename (abfd));
12870
3019eac3 12871 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12872 die->offset = offset;
12873 die->tag = abbrev->tag;
12874 die->abbrev = abbrev_number;
12875
3019eac3
DE
12876 /* Make the result usable.
12877 The caller needs to update num_attrs after adding the extra
12878 attributes. */
93311388
DE
12879 die->num_attrs = abbrev->num_attrs;
12880
12881 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12882 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12883 info_ptr);
93311388
DE
12884
12885 *diep = die;
12886 *has_children = abbrev->has_children;
12887 return info_ptr;
12888}
12889
3019eac3
DE
12890/* Read a die and all its attributes.
12891 Set DIEP to point to a newly allocated die with its information,
12892 except for its child, sibling, and parent fields.
12893 Set HAS_CHILDREN to tell whether the die has children or not. */
12894
12895static gdb_byte *
12896read_full_die (const struct die_reader_specs *reader,
12897 struct die_info **diep, gdb_byte *info_ptr,
12898 int *has_children)
12899{
12900 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12901}
433df2d4
DE
12902\f
12903/* Abbreviation tables.
3019eac3 12904
433df2d4 12905 In DWARF version 2, the description of the debugging information is
c906108c
SS
12906 stored in a separate .debug_abbrev section. Before we read any
12907 dies from a section we read in all abbreviations and install them
433df2d4
DE
12908 in a hash table. */
12909
12910/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12911
12912static struct abbrev_info *
12913abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12914{
12915 struct abbrev_info *abbrev;
12916
12917 abbrev = (struct abbrev_info *)
12918 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12919 memset (abbrev, 0, sizeof (struct abbrev_info));
12920 return abbrev;
12921}
12922
12923/* Add an abbreviation to the table. */
c906108c
SS
12924
12925static void
433df2d4
DE
12926abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12927 unsigned int abbrev_number,
12928 struct abbrev_info *abbrev)
12929{
12930 unsigned int hash_number;
12931
12932 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12933 abbrev->next = abbrev_table->abbrevs[hash_number];
12934 abbrev_table->abbrevs[hash_number] = abbrev;
12935}
dee91e82 12936
433df2d4
DE
12937/* Look up an abbrev in the table.
12938 Returns NULL if the abbrev is not found. */
12939
12940static struct abbrev_info *
12941abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12942 unsigned int abbrev_number)
c906108c 12943{
433df2d4
DE
12944 unsigned int hash_number;
12945 struct abbrev_info *abbrev;
12946
12947 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12948 abbrev = abbrev_table->abbrevs[hash_number];
12949
12950 while (abbrev)
12951 {
12952 if (abbrev->number == abbrev_number)
12953 return abbrev;
12954 abbrev = abbrev->next;
12955 }
12956 return NULL;
12957}
12958
12959/* Read in an abbrev table. */
12960
12961static struct abbrev_table *
12962abbrev_table_read_table (struct dwarf2_section_info *section,
12963 sect_offset offset)
12964{
12965 struct objfile *objfile = dwarf2_per_objfile->objfile;
12966 bfd *abfd = section->asection->owner;
12967 struct abbrev_table *abbrev_table;
fe1b8b76 12968 gdb_byte *abbrev_ptr;
c906108c
SS
12969 struct abbrev_info *cur_abbrev;
12970 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 12971 unsigned int abbrev_form;
f3dd6933
DJ
12972 struct attr_abbrev *cur_attrs;
12973 unsigned int allocated_attrs;
c906108c 12974
433df2d4 12975 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 12976 abbrev_table->offset = offset;
433df2d4
DE
12977 obstack_init (&abbrev_table->abbrev_obstack);
12978 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12979 (ABBREV_HASH_SIZE
12980 * sizeof (struct abbrev_info *)));
12981 memset (abbrev_table->abbrevs, 0,
12982 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 12983
433df2d4
DE
12984 dwarf2_read_section (objfile, section);
12985 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
12986 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12987 abbrev_ptr += bytes_read;
12988
f3dd6933
DJ
12989 allocated_attrs = ATTR_ALLOC_CHUNK;
12990 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 12991
0963b4bd 12992 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
12993 while (abbrev_number)
12994 {
433df2d4 12995 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
12996
12997 /* read in abbrev header */
12998 cur_abbrev->number = abbrev_number;
12999 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13000 abbrev_ptr += bytes_read;
13001 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13002 abbrev_ptr += 1;
13003
13004 /* now read in declarations */
13005 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13006 abbrev_ptr += bytes_read;
13007 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13008 abbrev_ptr += bytes_read;
13009 while (abbrev_name)
13010 {
f3dd6933 13011 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13012 {
f3dd6933
DJ
13013 allocated_attrs += ATTR_ALLOC_CHUNK;
13014 cur_attrs
13015 = xrealloc (cur_attrs, (allocated_attrs
13016 * sizeof (struct attr_abbrev)));
c906108c 13017 }
ae038cb0 13018
f3dd6933
DJ
13019 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13020 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13021 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13022 abbrev_ptr += bytes_read;
13023 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13024 abbrev_ptr += bytes_read;
13025 }
13026
433df2d4 13027 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13028 (cur_abbrev->num_attrs
13029 * sizeof (struct attr_abbrev)));
13030 memcpy (cur_abbrev->attrs, cur_attrs,
13031 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13032
433df2d4 13033 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13034
13035 /* Get next abbreviation.
13036 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13037 always properly terminated with an abbrev number of 0.
13038 Exit loop if we encounter an abbreviation which we have
13039 already read (which means we are about to read the abbreviations
13040 for the next compile unit) or if the end of the abbreviation
13041 table is reached. */
433df2d4 13042 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13043 break;
13044 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13045 abbrev_ptr += bytes_read;
433df2d4 13046 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13047 break;
13048 }
f3dd6933
DJ
13049
13050 xfree (cur_attrs);
433df2d4 13051 return abbrev_table;
c906108c
SS
13052}
13053
433df2d4 13054/* Free the resources held by ABBREV_TABLE. */
c906108c 13055
c906108c 13056static void
433df2d4 13057abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13058{
433df2d4
DE
13059 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13060 xfree (abbrev_table);
c906108c
SS
13061}
13062
f4dc4d17
DE
13063/* Same as abbrev_table_free but as a cleanup.
13064 We pass in a pointer to the pointer to the table so that we can
13065 set the pointer to NULL when we're done. It also simplifies
13066 build_type_unit_groups. */
13067
13068static void
13069abbrev_table_free_cleanup (void *table_ptr)
13070{
13071 struct abbrev_table **abbrev_table_ptr = table_ptr;
13072
13073 if (*abbrev_table_ptr != NULL)
13074 abbrev_table_free (*abbrev_table_ptr);
13075 *abbrev_table_ptr = NULL;
13076}
13077
433df2d4
DE
13078/* Read the abbrev table for CU from ABBREV_SECTION. */
13079
13080static void
13081dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13082 struct dwarf2_section_info *abbrev_section)
c906108c 13083{
433df2d4
DE
13084 cu->abbrev_table =
13085 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13086}
c906108c 13087
433df2d4 13088/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13089
433df2d4
DE
13090static void
13091dwarf2_free_abbrev_table (void *ptr_to_cu)
13092{
13093 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13094
433df2d4
DE
13095 abbrev_table_free (cu->abbrev_table);
13096 /* Set this to NULL so that we SEGV if we try to read it later,
13097 and also because free_comp_unit verifies this is NULL. */
13098 cu->abbrev_table = NULL;
13099}
13100\f
72bf9492
DJ
13101/* Returns nonzero if TAG represents a type that we might generate a partial
13102 symbol for. */
13103
13104static int
13105is_type_tag_for_partial (int tag)
13106{
13107 switch (tag)
13108 {
13109#if 0
13110 /* Some types that would be reasonable to generate partial symbols for,
13111 that we don't at present. */
13112 case DW_TAG_array_type:
13113 case DW_TAG_file_type:
13114 case DW_TAG_ptr_to_member_type:
13115 case DW_TAG_set_type:
13116 case DW_TAG_string_type:
13117 case DW_TAG_subroutine_type:
13118#endif
13119 case DW_TAG_base_type:
13120 case DW_TAG_class_type:
680b30c7 13121 case DW_TAG_interface_type:
72bf9492
DJ
13122 case DW_TAG_enumeration_type:
13123 case DW_TAG_structure_type:
13124 case DW_TAG_subrange_type:
13125 case DW_TAG_typedef:
13126 case DW_TAG_union_type:
13127 return 1;
13128 default:
13129 return 0;
13130 }
13131}
13132
13133/* Load all DIEs that are interesting for partial symbols into memory. */
13134
13135static struct partial_die_info *
dee91e82
DE
13136load_partial_dies (const struct die_reader_specs *reader,
13137 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13138{
dee91e82 13139 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13140 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13141 struct partial_die_info *part_die;
13142 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13143 struct abbrev_info *abbrev;
13144 unsigned int bytes_read;
5afb4e99 13145 unsigned int load_all = 0;
72bf9492
DJ
13146 int nesting_level = 1;
13147
13148 parent_die = NULL;
13149 last_die = NULL;
13150
7adf1e79
DE
13151 gdb_assert (cu->per_cu != NULL);
13152 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13153 load_all = 1;
13154
72bf9492
DJ
13155 cu->partial_dies
13156 = htab_create_alloc_ex (cu->header.length / 12,
13157 partial_die_hash,
13158 partial_die_eq,
13159 NULL,
13160 &cu->comp_unit_obstack,
13161 hashtab_obstack_allocate,
13162 dummy_obstack_deallocate);
13163
13164 part_die = obstack_alloc (&cu->comp_unit_obstack,
13165 sizeof (struct partial_die_info));
13166
13167 while (1)
13168 {
13169 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13170
13171 /* A NULL abbrev means the end of a series of children. */
13172 if (abbrev == NULL)
13173 {
13174 if (--nesting_level == 0)
13175 {
13176 /* PART_DIE was probably the last thing allocated on the
13177 comp_unit_obstack, so we could call obstack_free
13178 here. We don't do that because the waste is small,
13179 and will be cleaned up when we're done with this
13180 compilation unit. This way, we're also more robust
13181 against other users of the comp_unit_obstack. */
13182 return first_die;
13183 }
13184 info_ptr += bytes_read;
13185 last_die = parent_die;
13186 parent_die = parent_die->die_parent;
13187 continue;
13188 }
13189
98bfdba5
PA
13190 /* Check for template arguments. We never save these; if
13191 they're seen, we just mark the parent, and go on our way. */
13192 if (parent_die != NULL
13193 && cu->language == language_cplus
13194 && (abbrev->tag == DW_TAG_template_type_param
13195 || abbrev->tag == DW_TAG_template_value_param))
13196 {
13197 parent_die->has_template_arguments = 1;
13198
13199 if (!load_all)
13200 {
13201 /* We don't need a partial DIE for the template argument. */
dee91e82 13202 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13203 continue;
13204 }
13205 }
13206
0d99eb77 13207 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13208 Skip their other children. */
13209 if (!load_all
13210 && cu->language == language_cplus
13211 && parent_die != NULL
13212 && parent_die->tag == DW_TAG_subprogram)
13213 {
dee91e82 13214 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13215 continue;
13216 }
13217
5afb4e99
DJ
13218 /* Check whether this DIE is interesting enough to save. Normally
13219 we would not be interested in members here, but there may be
13220 later variables referencing them via DW_AT_specification (for
13221 static members). */
13222 if (!load_all
13223 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13224 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13225 && abbrev->tag != DW_TAG_enumerator
13226 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13227 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13228 && abbrev->tag != DW_TAG_variable
5afb4e99 13229 && abbrev->tag != DW_TAG_namespace
f55ee35c 13230 && abbrev->tag != DW_TAG_module
95554aad
TT
13231 && abbrev->tag != DW_TAG_member
13232 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13233 {
13234 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13235 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13236 continue;
13237 }
13238
dee91e82
DE
13239 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13240 info_ptr);
72bf9492
DJ
13241
13242 /* This two-pass algorithm for processing partial symbols has a
13243 high cost in cache pressure. Thus, handle some simple cases
13244 here which cover the majority of C partial symbols. DIEs
13245 which neither have specification tags in them, nor could have
13246 specification tags elsewhere pointing at them, can simply be
13247 processed and discarded.
13248
13249 This segment is also optional; scan_partial_symbols and
13250 add_partial_symbol will handle these DIEs if we chain
13251 them in normally. When compilers which do not emit large
13252 quantities of duplicate debug information are more common,
13253 this code can probably be removed. */
13254
13255 /* Any complete simple types at the top level (pretty much all
13256 of them, for a language without namespaces), can be processed
13257 directly. */
13258 if (parent_die == NULL
13259 && part_die->has_specification == 0
13260 && part_die->is_declaration == 0
d8228535 13261 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13262 || part_die->tag == DW_TAG_base_type
13263 || part_die->tag == DW_TAG_subrange_type))
13264 {
13265 if (building_psymtab && part_die->name != NULL)
04a679b8 13266 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13267 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13268 &objfile->static_psymbols,
13269 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13270 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13271 continue;
13272 }
13273
d8228535
JK
13274 /* The exception for DW_TAG_typedef with has_children above is
13275 a workaround of GCC PR debug/47510. In the case of this complaint
13276 type_name_no_tag_or_error will error on such types later.
13277
13278 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13279 it could not find the child DIEs referenced later, this is checked
13280 above. In correct DWARF DW_TAG_typedef should have no children. */
13281
13282 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13283 complaint (&symfile_complaints,
13284 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13285 "- DIE at 0x%x [in module %s]"),
b64f50a1 13286 part_die->offset.sect_off, objfile->name);
d8228535 13287
72bf9492
DJ
13288 /* If we're at the second level, and we're an enumerator, and
13289 our parent has no specification (meaning possibly lives in a
13290 namespace elsewhere), then we can add the partial symbol now
13291 instead of queueing it. */
13292 if (part_die->tag == DW_TAG_enumerator
13293 && parent_die != NULL
13294 && parent_die->die_parent == NULL
13295 && parent_die->tag == DW_TAG_enumeration_type
13296 && parent_die->has_specification == 0)
13297 {
13298 if (part_die->name == NULL)
3e43a32a
MS
13299 complaint (&symfile_complaints,
13300 _("malformed enumerator DIE ignored"));
72bf9492 13301 else if (building_psymtab)
04a679b8 13302 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13303 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13304 (cu->language == language_cplus
13305 || cu->language == language_java)
bb5ed363
DE
13306 ? &objfile->global_psymbols
13307 : &objfile->static_psymbols,
13308 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13309
dee91e82 13310 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13311 continue;
13312 }
13313
13314 /* We'll save this DIE so link it in. */
13315 part_die->die_parent = parent_die;
13316 part_die->die_sibling = NULL;
13317 part_die->die_child = NULL;
13318
13319 if (last_die && last_die == parent_die)
13320 last_die->die_child = part_die;
13321 else if (last_die)
13322 last_die->die_sibling = part_die;
13323
13324 last_die = part_die;
13325
13326 if (first_die == NULL)
13327 first_die = part_die;
13328
13329 /* Maybe add the DIE to the hash table. Not all DIEs that we
13330 find interesting need to be in the hash table, because we
13331 also have the parent/sibling/child chains; only those that we
13332 might refer to by offset later during partial symbol reading.
13333
13334 For now this means things that might have be the target of a
13335 DW_AT_specification, DW_AT_abstract_origin, or
13336 DW_AT_extension. DW_AT_extension will refer only to
13337 namespaces; DW_AT_abstract_origin refers to functions (and
13338 many things under the function DIE, but we do not recurse
13339 into function DIEs during partial symbol reading) and
13340 possibly variables as well; DW_AT_specification refers to
13341 declarations. Declarations ought to have the DW_AT_declaration
13342 flag. It happens that GCC forgets to put it in sometimes, but
13343 only for functions, not for types.
13344
13345 Adding more things than necessary to the hash table is harmless
13346 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13347 wasted time in find_partial_die, when we reread the compilation
13348 unit with load_all_dies set. */
72bf9492 13349
5afb4e99 13350 if (load_all
72929c62 13351 || abbrev->tag == DW_TAG_constant
5afb4e99 13352 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13353 || abbrev->tag == DW_TAG_variable
13354 || abbrev->tag == DW_TAG_namespace
13355 || part_die->is_declaration)
13356 {
13357 void **slot;
13358
13359 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13360 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13361 *slot = part_die;
13362 }
13363
13364 part_die = obstack_alloc (&cu->comp_unit_obstack,
13365 sizeof (struct partial_die_info));
13366
13367 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13368 we have no reason to follow the children of structures; for other
98bfdba5
PA
13369 languages we have to, so that we can get at method physnames
13370 to infer fully qualified class names, for DW_AT_specification,
13371 and for C++ template arguments. For C++, we also look one level
13372 inside functions to find template arguments (if the name of the
13373 function does not already contain the template arguments).
bc30ff58
JB
13374
13375 For Ada, we need to scan the children of subprograms and lexical
13376 blocks as well because Ada allows the definition of nested
13377 entities that could be interesting for the debugger, such as
13378 nested subprograms for instance. */
72bf9492 13379 if (last_die->has_children
5afb4e99
DJ
13380 && (load_all
13381 || last_die->tag == DW_TAG_namespace
f55ee35c 13382 || last_die->tag == DW_TAG_module
72bf9492 13383 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13384 || (cu->language == language_cplus
13385 && last_die->tag == DW_TAG_subprogram
13386 && (last_die->name == NULL
13387 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13388 || (cu->language != language_c
13389 && (last_die->tag == DW_TAG_class_type
680b30c7 13390 || last_die->tag == DW_TAG_interface_type
72bf9492 13391 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13392 || last_die->tag == DW_TAG_union_type))
13393 || (cu->language == language_ada
13394 && (last_die->tag == DW_TAG_subprogram
13395 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13396 {
13397 nesting_level++;
13398 parent_die = last_die;
13399 continue;
13400 }
13401
13402 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13403 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13404
13405 /* Back to the top, do it again. */
13406 }
13407}
13408
c906108c
SS
13409/* Read a minimal amount of information into the minimal die structure. */
13410
fe1b8b76 13411static gdb_byte *
dee91e82
DE
13412read_partial_die (const struct die_reader_specs *reader,
13413 struct partial_die_info *part_die,
13414 struct abbrev_info *abbrev, unsigned int abbrev_len,
13415 gdb_byte *info_ptr)
c906108c 13416{
dee91e82 13417 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13418 struct objfile *objfile = cu->objfile;
dee91e82 13419 gdb_byte *buffer = reader->buffer;
fa238c03 13420 unsigned int i;
c906108c 13421 struct attribute attr;
c5aa993b 13422 int has_low_pc_attr = 0;
c906108c 13423 int has_high_pc_attr = 0;
91da1414 13424 int high_pc_relative = 0;
c906108c 13425
72bf9492 13426 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13427
b64f50a1 13428 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13429
13430 info_ptr += abbrev_len;
13431
13432 if (abbrev == NULL)
13433 return info_ptr;
13434
c906108c
SS
13435 part_die->tag = abbrev->tag;
13436 part_die->has_children = abbrev->has_children;
c906108c
SS
13437
13438 for (i = 0; i < abbrev->num_attrs; ++i)
13439 {
dee91e82 13440 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13441
13442 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13443 partial symbol table. */
c906108c
SS
13444 switch (attr.name)
13445 {
13446 case DW_AT_name:
71c25dea
TT
13447 switch (part_die->tag)
13448 {
13449 case DW_TAG_compile_unit:
95554aad 13450 case DW_TAG_partial_unit:
348e048f 13451 case DW_TAG_type_unit:
71c25dea
TT
13452 /* Compilation units have a DW_AT_name that is a filename, not
13453 a source language identifier. */
13454 case DW_TAG_enumeration_type:
13455 case DW_TAG_enumerator:
13456 /* These tags always have simple identifiers already; no need
13457 to canonicalize them. */
13458 part_die->name = DW_STRING (&attr);
13459 break;
13460 default:
13461 part_die->name
13462 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13463 &objfile->objfile_obstack);
71c25dea
TT
13464 break;
13465 }
c906108c 13466 break;
31ef98ae 13467 case DW_AT_linkage_name:
c906108c 13468 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13469 /* Note that both forms of linkage name might appear. We
13470 assume they will be the same, and we only store the last
13471 one we see. */
94af9270
KS
13472 if (cu->language == language_ada)
13473 part_die->name = DW_STRING (&attr);
abc72ce4 13474 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13475 break;
13476 case DW_AT_low_pc:
13477 has_low_pc_attr = 1;
13478 part_die->lowpc = DW_ADDR (&attr);
13479 break;
13480 case DW_AT_high_pc:
13481 has_high_pc_attr = 1;
3019eac3
DE
13482 if (attr.form == DW_FORM_addr
13483 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13484 part_die->highpc = DW_ADDR (&attr);
13485 else
13486 {
13487 high_pc_relative = 1;
13488 part_die->highpc = DW_UNSND (&attr);
13489 }
c906108c
SS
13490 break;
13491 case DW_AT_location:
0963b4bd 13492 /* Support the .debug_loc offsets. */
8e19ed76
PS
13493 if (attr_form_is_block (&attr))
13494 {
95554aad 13495 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13496 }
3690dd37 13497 else if (attr_form_is_section_offset (&attr))
8e19ed76 13498 {
4d3c2250 13499 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13500 }
13501 else
13502 {
4d3c2250
KB
13503 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13504 "partial symbol information");
8e19ed76 13505 }
c906108c 13506 break;
c906108c
SS
13507 case DW_AT_external:
13508 part_die->is_external = DW_UNSND (&attr);
13509 break;
13510 case DW_AT_declaration:
13511 part_die->is_declaration = DW_UNSND (&attr);
13512 break;
13513 case DW_AT_type:
13514 part_die->has_type = 1;
13515 break;
13516 case DW_AT_abstract_origin:
13517 case DW_AT_specification:
72bf9492
DJ
13518 case DW_AT_extension:
13519 part_die->has_specification = 1;
c764a876 13520 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13521 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13522 || cu->per_cu->is_dwz);
c906108c
SS
13523 break;
13524 case DW_AT_sibling:
13525 /* Ignore absolute siblings, they might point outside of
13526 the current compile unit. */
13527 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13528 complaint (&symfile_complaints,
13529 _("ignoring absolute DW_AT_sibling"));
c906108c 13530 else
b64f50a1 13531 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13532 break;
fa4028e9
JB
13533 case DW_AT_byte_size:
13534 part_die->has_byte_size = 1;
13535 break;
68511cec
CES
13536 case DW_AT_calling_convention:
13537 /* DWARF doesn't provide a way to identify a program's source-level
13538 entry point. DW_AT_calling_convention attributes are only meant
13539 to describe functions' calling conventions.
13540
13541 However, because it's a necessary piece of information in
13542 Fortran, and because DW_CC_program is the only piece of debugging
13543 information whose definition refers to a 'main program' at all,
13544 several compilers have begun marking Fortran main programs with
13545 DW_CC_program --- even when those functions use the standard
13546 calling conventions.
13547
13548 So until DWARF specifies a way to provide this information and
13549 compilers pick up the new representation, we'll support this
13550 practice. */
13551 if (DW_UNSND (&attr) == DW_CC_program
13552 && cu->language == language_fortran)
01f8c46d
JK
13553 {
13554 set_main_name (part_die->name);
13555
13556 /* As this DIE has a static linkage the name would be difficult
13557 to look up later. */
13558 language_of_main = language_fortran;
13559 }
68511cec 13560 break;
481860b3
GB
13561 case DW_AT_inline:
13562 if (DW_UNSND (&attr) == DW_INL_inlined
13563 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13564 part_die->may_be_inlined = 1;
13565 break;
95554aad
TT
13566
13567 case DW_AT_import:
13568 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13569 {
13570 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13571 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13572 || cu->per_cu->is_dwz);
13573 }
95554aad
TT
13574 break;
13575
c906108c
SS
13576 default:
13577 break;
13578 }
13579 }
13580
91da1414
MW
13581 if (high_pc_relative)
13582 part_die->highpc += part_die->lowpc;
13583
9373cf26
JK
13584 if (has_low_pc_attr && has_high_pc_attr)
13585 {
13586 /* When using the GNU linker, .gnu.linkonce. sections are used to
13587 eliminate duplicate copies of functions and vtables and such.
13588 The linker will arbitrarily choose one and discard the others.
13589 The AT_*_pc values for such functions refer to local labels in
13590 these sections. If the section from that file was discarded, the
13591 labels are not in the output, so the relocs get a value of 0.
13592 If this is a discarded function, mark the pc bounds as invalid,
13593 so that GDB will ignore it. */
13594 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13595 {
bb5ed363 13596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13597
13598 complaint (&symfile_complaints,
13599 _("DW_AT_low_pc %s is zero "
13600 "for DIE at 0x%x [in module %s]"),
13601 paddress (gdbarch, part_die->lowpc),
b64f50a1 13602 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13603 }
13604 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13605 else if (part_die->lowpc >= part_die->highpc)
13606 {
bb5ed363 13607 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13608
13609 complaint (&symfile_complaints,
13610 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13611 "for DIE at 0x%x [in module %s]"),
13612 paddress (gdbarch, part_die->lowpc),
13613 paddress (gdbarch, part_die->highpc),
b64f50a1 13614 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13615 }
13616 else
13617 part_die->has_pc_info = 1;
13618 }
85cbf3d3 13619
c906108c
SS
13620 return info_ptr;
13621}
13622
72bf9492
DJ
13623/* Find a cached partial DIE at OFFSET in CU. */
13624
13625static struct partial_die_info *
b64f50a1 13626find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13627{
13628 struct partial_die_info *lookup_die = NULL;
13629 struct partial_die_info part_die;
13630
13631 part_die.offset = offset;
b64f50a1
JK
13632 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13633 offset.sect_off);
72bf9492 13634
72bf9492
DJ
13635 return lookup_die;
13636}
13637
348e048f
DE
13638/* Find a partial DIE at OFFSET, which may or may not be in CU,
13639 except in the case of .debug_types DIEs which do not reference
13640 outside their CU (they do however referencing other types via
55f1336d 13641 DW_FORM_ref_sig8). */
72bf9492
DJ
13642
13643static struct partial_die_info *
36586728 13644find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13645{
bb5ed363 13646 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13647 struct dwarf2_per_cu_data *per_cu = NULL;
13648 struct partial_die_info *pd = NULL;
72bf9492 13649
36586728
TT
13650 if (offset_in_dwz == cu->per_cu->is_dwz
13651 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13652 {
13653 pd = find_partial_die_in_comp_unit (offset, cu);
13654 if (pd != NULL)
13655 return pd;
0d99eb77
DE
13656 /* We missed recording what we needed.
13657 Load all dies and try again. */
13658 per_cu = cu->per_cu;
5afb4e99 13659 }
0d99eb77
DE
13660 else
13661 {
13662 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13663 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13664 {
13665 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13666 " external reference to offset 0x%lx [in module %s].\n"),
13667 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13668 bfd_get_filename (objfile->obfd));
13669 }
36586728
TT
13670 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13671 objfile);
72bf9492 13672
0d99eb77
DE
13673 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13674 load_partial_comp_unit (per_cu);
ae038cb0 13675
0d99eb77
DE
13676 per_cu->cu->last_used = 0;
13677 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13678 }
5afb4e99 13679
dee91e82
DE
13680 /* If we didn't find it, and not all dies have been loaded,
13681 load them all and try again. */
13682
5afb4e99
DJ
13683 if (pd == NULL && per_cu->load_all_dies == 0)
13684 {
5afb4e99 13685 per_cu->load_all_dies = 1;
fd820528
DE
13686
13687 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13688 THIS_CU->cu may already be in use. So we can't just free it and
13689 replace its DIEs with the ones we read in. Instead, we leave those
13690 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13691 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13692 set. */
dee91e82 13693 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13694
13695 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13696 }
13697
13698 if (pd == NULL)
13699 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13700 _("could not find partial DIE 0x%x "
13701 "in cache [from module %s]\n"),
b64f50a1 13702 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13703 return pd;
72bf9492
DJ
13704}
13705
abc72ce4
DE
13706/* See if we can figure out if the class lives in a namespace. We do
13707 this by looking for a member function; its demangled name will
13708 contain namespace info, if there is any. */
13709
13710static void
13711guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13712 struct dwarf2_cu *cu)
13713{
13714 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13715 what template types look like, because the demangler
13716 frequently doesn't give the same name as the debug info. We
13717 could fix this by only using the demangled name to get the
13718 prefix (but see comment in read_structure_type). */
13719
13720 struct partial_die_info *real_pdi;
13721 struct partial_die_info *child_pdi;
13722
13723 /* If this DIE (this DIE's specification, if any) has a parent, then
13724 we should not do this. We'll prepend the parent's fully qualified
13725 name when we create the partial symbol. */
13726
13727 real_pdi = struct_pdi;
13728 while (real_pdi->has_specification)
36586728
TT
13729 real_pdi = find_partial_die (real_pdi->spec_offset,
13730 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13731
13732 if (real_pdi->die_parent != NULL)
13733 return;
13734
13735 for (child_pdi = struct_pdi->die_child;
13736 child_pdi != NULL;
13737 child_pdi = child_pdi->die_sibling)
13738 {
13739 if (child_pdi->tag == DW_TAG_subprogram
13740 && child_pdi->linkage_name != NULL)
13741 {
13742 char *actual_class_name
13743 = language_class_name_from_physname (cu->language_defn,
13744 child_pdi->linkage_name);
13745 if (actual_class_name != NULL)
13746 {
13747 struct_pdi->name
13748 = obsavestring (actual_class_name,
13749 strlen (actual_class_name),
13750 &cu->objfile->objfile_obstack);
13751 xfree (actual_class_name);
13752 }
13753 break;
13754 }
13755 }
13756}
13757
72bf9492
DJ
13758/* Adjust PART_DIE before generating a symbol for it. This function
13759 may set the is_external flag or change the DIE's name. */
13760
13761static void
13762fixup_partial_die (struct partial_die_info *part_die,
13763 struct dwarf2_cu *cu)
13764{
abc72ce4
DE
13765 /* Once we've fixed up a die, there's no point in doing so again.
13766 This also avoids a memory leak if we were to call
13767 guess_partial_die_structure_name multiple times. */
13768 if (part_die->fixup_called)
13769 return;
13770
72bf9492
DJ
13771 /* If we found a reference attribute and the DIE has no name, try
13772 to find a name in the referred to DIE. */
13773
13774 if (part_die->name == NULL && part_die->has_specification)
13775 {
13776 struct partial_die_info *spec_die;
72bf9492 13777
36586728
TT
13778 spec_die = find_partial_die (part_die->spec_offset,
13779 part_die->spec_is_dwz, cu);
72bf9492 13780
10b3939b 13781 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13782
13783 if (spec_die->name)
13784 {
13785 part_die->name = spec_die->name;
13786
13787 /* Copy DW_AT_external attribute if it is set. */
13788 if (spec_die->is_external)
13789 part_die->is_external = spec_die->is_external;
13790 }
13791 }
13792
13793 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13794
13795 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13796 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13797
abc72ce4
DE
13798 /* If there is no parent die to provide a namespace, and there are
13799 children, see if we can determine the namespace from their linkage
122d1940 13800 name. */
abc72ce4 13801 if (cu->language == language_cplus
8b70b953 13802 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13803 && part_die->die_parent == NULL
13804 && part_die->has_children
13805 && (part_die->tag == DW_TAG_class_type
13806 || part_die->tag == DW_TAG_structure_type
13807 || part_die->tag == DW_TAG_union_type))
13808 guess_partial_die_structure_name (part_die, cu);
13809
53832f31
TT
13810 /* GCC might emit a nameless struct or union that has a linkage
13811 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13812 if (part_die->name == NULL
96408a79
SA
13813 && (part_die->tag == DW_TAG_class_type
13814 || part_die->tag == DW_TAG_interface_type
13815 || part_die->tag == DW_TAG_structure_type
13816 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13817 && part_die->linkage_name != NULL)
13818 {
13819 char *demangled;
13820
13821 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13822 if (demangled)
13823 {
96408a79
SA
13824 const char *base;
13825
13826 /* Strip any leading namespaces/classes, keep only the base name.
13827 DW_AT_name for named DIEs does not contain the prefixes. */
13828 base = strrchr (demangled, ':');
13829 if (base && base > demangled && base[-1] == ':')
13830 base++;
13831 else
13832 base = demangled;
13833
13834 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13835 &cu->objfile->objfile_obstack);
13836 xfree (demangled);
13837 }
13838 }
13839
abc72ce4 13840 part_die->fixup_called = 1;
72bf9492
DJ
13841}
13842
a8329558 13843/* Read an attribute value described by an attribute form. */
c906108c 13844
fe1b8b76 13845static gdb_byte *
dee91e82
DE
13846read_attribute_value (const struct die_reader_specs *reader,
13847 struct attribute *attr, unsigned form,
13848 gdb_byte *info_ptr)
c906108c 13849{
dee91e82
DE
13850 struct dwarf2_cu *cu = reader->cu;
13851 bfd *abfd = reader->abfd;
e7c27a73 13852 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13853 unsigned int bytes_read;
13854 struct dwarf_block *blk;
13855
a8329558
KW
13856 attr->form = form;
13857 switch (form)
c906108c 13858 {
c906108c 13859 case DW_FORM_ref_addr:
ae411497 13860 if (cu->header.version == 2)
4568ecf9 13861 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13862 else
4568ecf9
DE
13863 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13864 &cu->header, &bytes_read);
ae411497
TT
13865 info_ptr += bytes_read;
13866 break;
36586728
TT
13867 case DW_FORM_GNU_ref_alt:
13868 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13869 info_ptr += bytes_read;
13870 break;
ae411497 13871 case DW_FORM_addr:
e7c27a73 13872 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13873 info_ptr += bytes_read;
c906108c
SS
13874 break;
13875 case DW_FORM_block2:
7b5a2f43 13876 blk = dwarf_alloc_block (cu);
c906108c
SS
13877 blk->size = read_2_bytes (abfd, info_ptr);
13878 info_ptr += 2;
13879 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13880 info_ptr += blk->size;
13881 DW_BLOCK (attr) = blk;
13882 break;
13883 case DW_FORM_block4:
7b5a2f43 13884 blk = dwarf_alloc_block (cu);
c906108c
SS
13885 blk->size = read_4_bytes (abfd, info_ptr);
13886 info_ptr += 4;
13887 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13888 info_ptr += blk->size;
13889 DW_BLOCK (attr) = blk;
13890 break;
13891 case DW_FORM_data2:
13892 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13893 info_ptr += 2;
13894 break;
13895 case DW_FORM_data4:
13896 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13897 info_ptr += 4;
13898 break;
13899 case DW_FORM_data8:
13900 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13901 info_ptr += 8;
13902 break;
2dc7f7b3
TT
13903 case DW_FORM_sec_offset:
13904 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13905 info_ptr += bytes_read;
13906 break;
c906108c 13907 case DW_FORM_string:
9b1c24c8 13908 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13909 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13910 info_ptr += bytes_read;
13911 break;
4bdf3d34 13912 case DW_FORM_strp:
36586728
TT
13913 if (!cu->per_cu->is_dwz)
13914 {
13915 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13916 &bytes_read);
13917 DW_STRING_IS_CANONICAL (attr) = 0;
13918 info_ptr += bytes_read;
13919 break;
13920 }
13921 /* FALLTHROUGH */
13922 case DW_FORM_GNU_strp_alt:
13923 {
13924 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13925 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13926 &bytes_read);
13927
13928 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13929 DW_STRING_IS_CANONICAL (attr) = 0;
13930 info_ptr += bytes_read;
13931 }
4bdf3d34 13932 break;
2dc7f7b3 13933 case DW_FORM_exprloc:
c906108c 13934 case DW_FORM_block:
7b5a2f43 13935 blk = dwarf_alloc_block (cu);
c906108c
SS
13936 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13937 info_ptr += bytes_read;
13938 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13939 info_ptr += blk->size;
13940 DW_BLOCK (attr) = blk;
13941 break;
13942 case DW_FORM_block1:
7b5a2f43 13943 blk = dwarf_alloc_block (cu);
c906108c
SS
13944 blk->size = read_1_byte (abfd, info_ptr);
13945 info_ptr += 1;
13946 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13947 info_ptr += blk->size;
13948 DW_BLOCK (attr) = blk;
13949 break;
13950 case DW_FORM_data1:
13951 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13952 info_ptr += 1;
13953 break;
13954 case DW_FORM_flag:
13955 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13956 info_ptr += 1;
13957 break;
2dc7f7b3
TT
13958 case DW_FORM_flag_present:
13959 DW_UNSND (attr) = 1;
13960 break;
c906108c
SS
13961 case DW_FORM_sdata:
13962 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13963 info_ptr += bytes_read;
13964 break;
13965 case DW_FORM_udata:
13966 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13967 info_ptr += bytes_read;
13968 break;
13969 case DW_FORM_ref1:
4568ecf9
DE
13970 DW_UNSND (attr) = (cu->header.offset.sect_off
13971 + read_1_byte (abfd, info_ptr));
c906108c
SS
13972 info_ptr += 1;
13973 break;
13974 case DW_FORM_ref2:
4568ecf9
DE
13975 DW_UNSND (attr) = (cu->header.offset.sect_off
13976 + read_2_bytes (abfd, info_ptr));
c906108c
SS
13977 info_ptr += 2;
13978 break;
13979 case DW_FORM_ref4:
4568ecf9
DE
13980 DW_UNSND (attr) = (cu->header.offset.sect_off
13981 + read_4_bytes (abfd, info_ptr));
c906108c
SS
13982 info_ptr += 4;
13983 break;
613e1657 13984 case DW_FORM_ref8:
4568ecf9
DE
13985 DW_UNSND (attr) = (cu->header.offset.sect_off
13986 + read_8_bytes (abfd, info_ptr));
613e1657
KB
13987 info_ptr += 8;
13988 break;
55f1336d 13989 case DW_FORM_ref_sig8:
348e048f
DE
13990 /* Convert the signature to something we can record in DW_UNSND
13991 for later lookup.
13992 NOTE: This is NULL if the type wasn't found. */
13993 DW_SIGNATURED_TYPE (attr) =
e319fa28 13994 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
13995 info_ptr += 8;
13996 break;
c906108c 13997 case DW_FORM_ref_udata:
4568ecf9
DE
13998 DW_UNSND (attr) = (cu->header.offset.sect_off
13999 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14000 info_ptr += bytes_read;
14001 break;
c906108c 14002 case DW_FORM_indirect:
a8329558
KW
14003 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14004 info_ptr += bytes_read;
dee91e82 14005 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14006 break;
3019eac3
DE
14007 case DW_FORM_GNU_addr_index:
14008 if (reader->dwo_file == NULL)
14009 {
14010 /* For now flag a hard error.
14011 Later we can turn this into a complaint. */
14012 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14013 dwarf_form_name (form),
14014 bfd_get_filename (abfd));
14015 }
14016 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14017 info_ptr += bytes_read;
14018 break;
14019 case DW_FORM_GNU_str_index:
14020 if (reader->dwo_file == NULL)
14021 {
14022 /* For now flag a hard error.
14023 Later we can turn this into a complaint if warranted. */
14024 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14025 dwarf_form_name (form),
14026 bfd_get_filename (abfd));
14027 }
14028 {
14029 ULONGEST str_index =
14030 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14031
14032 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14033 DW_STRING_IS_CANONICAL (attr) = 0;
14034 info_ptr += bytes_read;
14035 }
14036 break;
c906108c 14037 default:
8a3fe4f8 14038 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14039 dwarf_form_name (form),
14040 bfd_get_filename (abfd));
c906108c 14041 }
28e94949 14042
36586728
TT
14043 /* Super hack. */
14044 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14045 attr->form = DW_FORM_GNU_ref_alt;
14046
28e94949
JB
14047 /* We have seen instances where the compiler tried to emit a byte
14048 size attribute of -1 which ended up being encoded as an unsigned
14049 0xffffffff. Although 0xffffffff is technically a valid size value,
14050 an object of this size seems pretty unlikely so we can relatively
14051 safely treat these cases as if the size attribute was invalid and
14052 treat them as zero by default. */
14053 if (attr->name == DW_AT_byte_size
14054 && form == DW_FORM_data4
14055 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14056 {
14057 complaint
14058 (&symfile_complaints,
43bbcdc2
PH
14059 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14060 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14061 DW_UNSND (attr) = 0;
14062 }
28e94949 14063
c906108c
SS
14064 return info_ptr;
14065}
14066
a8329558
KW
14067/* Read an attribute described by an abbreviated attribute. */
14068
fe1b8b76 14069static gdb_byte *
dee91e82
DE
14070read_attribute (const struct die_reader_specs *reader,
14071 struct attribute *attr, struct attr_abbrev *abbrev,
14072 gdb_byte *info_ptr)
a8329558
KW
14073{
14074 attr->name = abbrev->name;
dee91e82 14075 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14076}
14077
0963b4bd 14078/* Read dwarf information from a buffer. */
c906108c
SS
14079
14080static unsigned int
a1855c1d 14081read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14082{
fe1b8b76 14083 return bfd_get_8 (abfd, buf);
c906108c
SS
14084}
14085
14086static int
a1855c1d 14087read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14088{
fe1b8b76 14089 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14090}
14091
14092static unsigned int
a1855c1d 14093read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14094{
fe1b8b76 14095 return bfd_get_16 (abfd, buf);
c906108c
SS
14096}
14097
21ae7a4d 14098static int
a1855c1d 14099read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14100{
14101 return bfd_get_signed_16 (abfd, buf);
14102}
14103
c906108c 14104static unsigned int
a1855c1d 14105read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14106{
fe1b8b76 14107 return bfd_get_32 (abfd, buf);
c906108c
SS
14108}
14109
21ae7a4d 14110static int
a1855c1d 14111read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14112{
14113 return bfd_get_signed_32 (abfd, buf);
14114}
14115
93311388 14116static ULONGEST
a1855c1d 14117read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14118{
fe1b8b76 14119 return bfd_get_64 (abfd, buf);
c906108c
SS
14120}
14121
14122static CORE_ADDR
fe1b8b76 14123read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14124 unsigned int *bytes_read)
c906108c 14125{
e7c27a73 14126 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14127 CORE_ADDR retval = 0;
14128
107d2387 14129 if (cu_header->signed_addr_p)
c906108c 14130 {
107d2387
AC
14131 switch (cu_header->addr_size)
14132 {
14133 case 2:
fe1b8b76 14134 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14135 break;
14136 case 4:
fe1b8b76 14137 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14138 break;
14139 case 8:
fe1b8b76 14140 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14141 break;
14142 default:
8e65ff28 14143 internal_error (__FILE__, __LINE__,
e2e0b3e5 14144 _("read_address: bad switch, signed [in module %s]"),
659b0389 14145 bfd_get_filename (abfd));
107d2387
AC
14146 }
14147 }
14148 else
14149 {
14150 switch (cu_header->addr_size)
14151 {
14152 case 2:
fe1b8b76 14153 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14154 break;
14155 case 4:
fe1b8b76 14156 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14157 break;
14158 case 8:
fe1b8b76 14159 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14160 break;
14161 default:
8e65ff28 14162 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14163 _("read_address: bad switch, "
14164 "unsigned [in module %s]"),
659b0389 14165 bfd_get_filename (abfd));
107d2387 14166 }
c906108c 14167 }
64367e0a 14168
107d2387
AC
14169 *bytes_read = cu_header->addr_size;
14170 return retval;
c906108c
SS
14171}
14172
f7ef9339 14173/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14174 specification allows the initial length to take up either 4 bytes
14175 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14176 bytes describe the length and all offsets will be 8 bytes in length
14177 instead of 4.
14178
f7ef9339
KB
14179 An older, non-standard 64-bit format is also handled by this
14180 function. The older format in question stores the initial length
14181 as an 8-byte quantity without an escape value. Lengths greater
14182 than 2^32 aren't very common which means that the initial 4 bytes
14183 is almost always zero. Since a length value of zero doesn't make
14184 sense for the 32-bit format, this initial zero can be considered to
14185 be an escape value which indicates the presence of the older 64-bit
14186 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14187 greater than 4GB. If it becomes necessary to handle lengths
14188 somewhat larger than 4GB, we could allow other small values (such
14189 as the non-sensical values of 1, 2, and 3) to also be used as
14190 escape values indicating the presence of the old format.
f7ef9339 14191
917c78fc
MK
14192 The value returned via bytes_read should be used to increment the
14193 relevant pointer after calling read_initial_length().
c764a876 14194
613e1657
KB
14195 [ Note: read_initial_length() and read_offset() are based on the
14196 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14197 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14198 from:
14199
f7ef9339 14200 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14201
613e1657
KB
14202 This document is only a draft and is subject to change. (So beware.)
14203
f7ef9339 14204 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14205 determined empirically by examining 64-bit ELF files produced by
14206 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14207
14208 - Kevin, July 16, 2002
613e1657
KB
14209 ] */
14210
14211static LONGEST
c764a876 14212read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14213{
fe1b8b76 14214 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14215
dd373385 14216 if (length == 0xffffffff)
613e1657 14217 {
fe1b8b76 14218 length = bfd_get_64 (abfd, buf + 4);
613e1657 14219 *bytes_read = 12;
613e1657 14220 }
dd373385 14221 else if (length == 0)
f7ef9339 14222 {
dd373385 14223 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14224 length = bfd_get_64 (abfd, buf);
f7ef9339 14225 *bytes_read = 8;
f7ef9339 14226 }
613e1657
KB
14227 else
14228 {
14229 *bytes_read = 4;
613e1657
KB
14230 }
14231
c764a876
DE
14232 return length;
14233}
dd373385 14234
c764a876
DE
14235/* Cover function for read_initial_length.
14236 Returns the length of the object at BUF, and stores the size of the
14237 initial length in *BYTES_READ and stores the size that offsets will be in
14238 *OFFSET_SIZE.
14239 If the initial length size is not equivalent to that specified in
14240 CU_HEADER then issue a complaint.
14241 This is useful when reading non-comp-unit headers. */
dd373385 14242
c764a876
DE
14243static LONGEST
14244read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14245 const struct comp_unit_head *cu_header,
14246 unsigned int *bytes_read,
14247 unsigned int *offset_size)
14248{
14249 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14250
14251 gdb_assert (cu_header->initial_length_size == 4
14252 || cu_header->initial_length_size == 8
14253 || cu_header->initial_length_size == 12);
14254
14255 if (cu_header->initial_length_size != *bytes_read)
14256 complaint (&symfile_complaints,
14257 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14258
c764a876 14259 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14260 return length;
613e1657
KB
14261}
14262
14263/* Read an offset from the data stream. The size of the offset is
917c78fc 14264 given by cu_header->offset_size. */
613e1657
KB
14265
14266static LONGEST
fe1b8b76 14267read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14268 unsigned int *bytes_read)
c764a876
DE
14269{
14270 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14271
c764a876
DE
14272 *bytes_read = cu_header->offset_size;
14273 return offset;
14274}
14275
14276/* Read an offset from the data stream. */
14277
14278static LONGEST
14279read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14280{
14281 LONGEST retval = 0;
14282
c764a876 14283 switch (offset_size)
613e1657
KB
14284 {
14285 case 4:
fe1b8b76 14286 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14287 break;
14288 case 8:
fe1b8b76 14289 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14290 break;
14291 default:
8e65ff28 14292 internal_error (__FILE__, __LINE__,
c764a876 14293 _("read_offset_1: bad switch [in module %s]"),
659b0389 14294 bfd_get_filename (abfd));
613e1657
KB
14295 }
14296
917c78fc 14297 return retval;
613e1657
KB
14298}
14299
fe1b8b76
JB
14300static gdb_byte *
14301read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14302{
14303 /* If the size of a host char is 8 bits, we can return a pointer
14304 to the buffer, otherwise we have to copy the data to a buffer
14305 allocated on the temporary obstack. */
4bdf3d34 14306 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14307 return buf;
c906108c
SS
14308}
14309
14310static char *
9b1c24c8 14311read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14312{
14313 /* If the size of a host char is 8 bits, we can return a pointer
14314 to the string, otherwise we have to copy the string to a buffer
14315 allocated on the temporary obstack. */
4bdf3d34 14316 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14317 if (*buf == '\0')
14318 {
14319 *bytes_read_ptr = 1;
14320 return NULL;
14321 }
fe1b8b76
JB
14322 *bytes_read_ptr = strlen ((char *) buf) + 1;
14323 return (char *) buf;
4bdf3d34
JJ
14324}
14325
14326static char *
cf2c3c16 14327read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14328{
be391dca 14329 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14330 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14331 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14332 bfd_get_filename (abfd));
dce234bc 14333 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14334 error (_("DW_FORM_strp pointing outside of "
14335 ".debug_str section [in module %s]"),
14336 bfd_get_filename (abfd));
4bdf3d34 14337 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14338 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14339 return NULL;
dce234bc 14340 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14341}
14342
36586728
TT
14343/* Read a string at offset STR_OFFSET in the .debug_str section from
14344 the .dwz file DWZ. Throw an error if the offset is too large. If
14345 the string consists of a single NUL byte, return NULL; otherwise
14346 return a pointer to the string. */
14347
14348static char *
14349read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14350{
14351 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14352
14353 if (dwz->str.buffer == NULL)
14354 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14355 "section [in module %s]"),
14356 bfd_get_filename (dwz->dwz_bfd));
14357 if (str_offset >= dwz->str.size)
14358 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14359 ".debug_str section [in module %s]"),
14360 bfd_get_filename (dwz->dwz_bfd));
14361 gdb_assert (HOST_CHAR_BIT == 8);
14362 if (dwz->str.buffer[str_offset] == '\0')
14363 return NULL;
14364 return (char *) (dwz->str.buffer + str_offset);
14365}
14366
cf2c3c16
TT
14367static char *
14368read_indirect_string (bfd *abfd, gdb_byte *buf,
14369 const struct comp_unit_head *cu_header,
14370 unsigned int *bytes_read_ptr)
14371{
14372 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14373
14374 return read_indirect_string_at_offset (abfd, str_offset);
14375}
14376
12df843f 14377static ULONGEST
fe1b8b76 14378read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14379{
12df843f 14380 ULONGEST result;
ce5d95e1 14381 unsigned int num_read;
c906108c
SS
14382 int i, shift;
14383 unsigned char byte;
14384
14385 result = 0;
14386 shift = 0;
14387 num_read = 0;
14388 i = 0;
14389 while (1)
14390 {
fe1b8b76 14391 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14392 buf++;
14393 num_read++;
12df843f 14394 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14395 if ((byte & 128) == 0)
14396 {
14397 break;
14398 }
14399 shift += 7;
14400 }
14401 *bytes_read_ptr = num_read;
14402 return result;
14403}
14404
12df843f 14405static LONGEST
fe1b8b76 14406read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14407{
12df843f 14408 LONGEST result;
77e0b926 14409 int i, shift, num_read;
c906108c
SS
14410 unsigned char byte;
14411
14412 result = 0;
14413 shift = 0;
c906108c
SS
14414 num_read = 0;
14415 i = 0;
14416 while (1)
14417 {
fe1b8b76 14418 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14419 buf++;
14420 num_read++;
12df843f 14421 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14422 shift += 7;
14423 if ((byte & 128) == 0)
14424 {
14425 break;
14426 }
14427 }
77e0b926 14428 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14429 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14430 *bytes_read_ptr = num_read;
14431 return result;
14432}
14433
3019eac3
DE
14434/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14435 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14436 ADDR_SIZE is the size of addresses from the CU header. */
14437
14438static CORE_ADDR
14439read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14440{
14441 struct objfile *objfile = dwarf2_per_objfile->objfile;
14442 bfd *abfd = objfile->obfd;
14443 const gdb_byte *info_ptr;
14444
14445 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14446 if (dwarf2_per_objfile->addr.buffer == NULL)
14447 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14448 objfile->name);
14449 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14450 error (_("DW_FORM_addr_index pointing outside of "
14451 ".debug_addr section [in module %s]"),
14452 objfile->name);
14453 info_ptr = (dwarf2_per_objfile->addr.buffer
14454 + addr_base + addr_index * addr_size);
14455 if (addr_size == 4)
14456 return bfd_get_32 (abfd, info_ptr);
14457 else
14458 return bfd_get_64 (abfd, info_ptr);
14459}
14460
14461/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14462
14463static CORE_ADDR
14464read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14465{
14466 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14467}
14468
14469/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14470
14471static CORE_ADDR
14472read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14473 unsigned int *bytes_read)
14474{
14475 bfd *abfd = cu->objfile->obfd;
14476 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14477
14478 return read_addr_index (cu, addr_index);
14479}
14480
14481/* Data structure to pass results from dwarf2_read_addr_index_reader
14482 back to dwarf2_read_addr_index. */
14483
14484struct dwarf2_read_addr_index_data
14485{
14486 ULONGEST addr_base;
14487 int addr_size;
14488};
14489
14490/* die_reader_func for dwarf2_read_addr_index. */
14491
14492static void
14493dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14494 gdb_byte *info_ptr,
14495 struct die_info *comp_unit_die,
14496 int has_children,
14497 void *data)
14498{
14499 struct dwarf2_cu *cu = reader->cu;
14500 struct dwarf2_read_addr_index_data *aidata =
14501 (struct dwarf2_read_addr_index_data *) data;
14502
14503 aidata->addr_base = cu->addr_base;
14504 aidata->addr_size = cu->header.addr_size;
14505}
14506
14507/* Given an index in .debug_addr, fetch the value.
14508 NOTE: This can be called during dwarf expression evaluation,
14509 long after the debug information has been read, and thus per_cu->cu
14510 may no longer exist. */
14511
14512CORE_ADDR
14513dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14514 unsigned int addr_index)
14515{
14516 struct objfile *objfile = per_cu->objfile;
14517 struct dwarf2_cu *cu = per_cu->cu;
14518 ULONGEST addr_base;
14519 int addr_size;
14520
14521 /* This is intended to be called from outside this file. */
14522 dw2_setup (objfile);
14523
14524 /* We need addr_base and addr_size.
14525 If we don't have PER_CU->cu, we have to get it.
14526 Nasty, but the alternative is storing the needed info in PER_CU,
14527 which at this point doesn't seem justified: it's not clear how frequently
14528 it would get used and it would increase the size of every PER_CU.
14529 Entry points like dwarf2_per_cu_addr_size do a similar thing
14530 so we're not in uncharted territory here.
14531 Alas we need to be a bit more complicated as addr_base is contained
14532 in the DIE.
14533
14534 We don't need to read the entire CU(/TU).
14535 We just need the header and top level die.
a1b64ce1 14536
3019eac3 14537 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14538 For now we skip this optimization. */
3019eac3
DE
14539
14540 if (cu != NULL)
14541 {
14542 addr_base = cu->addr_base;
14543 addr_size = cu->header.addr_size;
14544 }
14545 else
14546 {
14547 struct dwarf2_read_addr_index_data aidata;
14548
a1b64ce1
DE
14549 /* Note: We can't use init_cutu_and_read_dies_simple here,
14550 we need addr_base. */
14551 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14552 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14553 addr_base = aidata.addr_base;
14554 addr_size = aidata.addr_size;
14555 }
14556
14557 return read_addr_index_1 (addr_index, addr_base, addr_size);
14558}
14559
14560/* Given a DW_AT_str_index, fetch the string. */
14561
14562static char *
14563read_str_index (const struct die_reader_specs *reader,
14564 struct dwarf2_cu *cu, ULONGEST str_index)
14565{
14566 struct objfile *objfile = dwarf2_per_objfile->objfile;
14567 const char *dwo_name = objfile->name;
14568 bfd *abfd = objfile->obfd;
14569 struct dwo_sections *sections = &reader->dwo_file->sections;
14570 gdb_byte *info_ptr;
14571 ULONGEST str_offset;
14572
14573 dwarf2_read_section (objfile, &sections->str);
14574 dwarf2_read_section (objfile, &sections->str_offsets);
14575 if (sections->str.buffer == NULL)
14576 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14577 " in CU at offset 0x%lx [in module %s]"),
14578 (long) cu->header.offset.sect_off, dwo_name);
14579 if (sections->str_offsets.buffer == NULL)
14580 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14581 " in CU at offset 0x%lx [in module %s]"),
14582 (long) cu->header.offset.sect_off, dwo_name);
14583 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14584 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14585 " section in CU at offset 0x%lx [in module %s]"),
14586 (long) cu->header.offset.sect_off, dwo_name);
14587 info_ptr = (sections->str_offsets.buffer
14588 + str_index * cu->header.offset_size);
14589 if (cu->header.offset_size == 4)
14590 str_offset = bfd_get_32 (abfd, info_ptr);
14591 else
14592 str_offset = bfd_get_64 (abfd, info_ptr);
14593 if (str_offset >= sections->str.size)
14594 error (_("Offset from DW_FORM_str_index pointing outside of"
14595 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14596 (long) cu->header.offset.sect_off, dwo_name);
14597 return (char *) (sections->str.buffer + str_offset);
14598}
14599
3019eac3
DE
14600/* Return the length of an LEB128 number in BUF. */
14601
14602static int
14603leb128_size (const gdb_byte *buf)
14604{
14605 const gdb_byte *begin = buf;
14606 gdb_byte byte;
14607
14608 while (1)
14609 {
14610 byte = *buf++;
14611 if ((byte & 128) == 0)
14612 return buf - begin;
14613 }
14614}
14615
c906108c 14616static void
e142c38c 14617set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14618{
14619 switch (lang)
14620 {
14621 case DW_LANG_C89:
76bee0cc 14622 case DW_LANG_C99:
c906108c 14623 case DW_LANG_C:
e142c38c 14624 cu->language = language_c;
c906108c
SS
14625 break;
14626 case DW_LANG_C_plus_plus:
e142c38c 14627 cu->language = language_cplus;
c906108c 14628 break;
6aecb9c2
JB
14629 case DW_LANG_D:
14630 cu->language = language_d;
14631 break;
c906108c
SS
14632 case DW_LANG_Fortran77:
14633 case DW_LANG_Fortran90:
b21b22e0 14634 case DW_LANG_Fortran95:
e142c38c 14635 cu->language = language_fortran;
c906108c 14636 break;
a766d390
DE
14637 case DW_LANG_Go:
14638 cu->language = language_go;
14639 break;
c906108c 14640 case DW_LANG_Mips_Assembler:
e142c38c 14641 cu->language = language_asm;
c906108c 14642 break;
bebd888e 14643 case DW_LANG_Java:
e142c38c 14644 cu->language = language_java;
bebd888e 14645 break;
c906108c 14646 case DW_LANG_Ada83:
8aaf0b47 14647 case DW_LANG_Ada95:
bc5f45f8
JB
14648 cu->language = language_ada;
14649 break;
72019c9c
GM
14650 case DW_LANG_Modula2:
14651 cu->language = language_m2;
14652 break;
fe8e67fd
PM
14653 case DW_LANG_Pascal83:
14654 cu->language = language_pascal;
14655 break;
22566fbd
DJ
14656 case DW_LANG_ObjC:
14657 cu->language = language_objc;
14658 break;
c906108c
SS
14659 case DW_LANG_Cobol74:
14660 case DW_LANG_Cobol85:
c906108c 14661 default:
e142c38c 14662 cu->language = language_minimal;
c906108c
SS
14663 break;
14664 }
e142c38c 14665 cu->language_defn = language_def (cu->language);
c906108c
SS
14666}
14667
14668/* Return the named attribute or NULL if not there. */
14669
14670static struct attribute *
e142c38c 14671dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14672{
a48e046c 14673 for (;;)
c906108c 14674 {
a48e046c
TT
14675 unsigned int i;
14676 struct attribute *spec = NULL;
14677
14678 for (i = 0; i < die->num_attrs; ++i)
14679 {
14680 if (die->attrs[i].name == name)
14681 return &die->attrs[i];
14682 if (die->attrs[i].name == DW_AT_specification
14683 || die->attrs[i].name == DW_AT_abstract_origin)
14684 spec = &die->attrs[i];
14685 }
14686
14687 if (!spec)
14688 break;
c906108c 14689
f2f0e013 14690 die = follow_die_ref (die, spec, &cu);
f2f0e013 14691 }
c5aa993b 14692
c906108c
SS
14693 return NULL;
14694}
14695
348e048f
DE
14696/* Return the named attribute or NULL if not there,
14697 but do not follow DW_AT_specification, etc.
14698 This is for use in contexts where we're reading .debug_types dies.
14699 Following DW_AT_specification, DW_AT_abstract_origin will take us
14700 back up the chain, and we want to go down. */
14701
14702static struct attribute *
45e58e77 14703dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14704{
14705 unsigned int i;
14706
14707 for (i = 0; i < die->num_attrs; ++i)
14708 if (die->attrs[i].name == name)
14709 return &die->attrs[i];
14710
14711 return NULL;
14712}
14713
05cf31d1
JB
14714/* Return non-zero iff the attribute NAME is defined for the given DIE,
14715 and holds a non-zero value. This function should only be used for
2dc7f7b3 14716 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14717
14718static int
14719dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14720{
14721 struct attribute *attr = dwarf2_attr (die, name, cu);
14722
14723 return (attr && DW_UNSND (attr));
14724}
14725
3ca72b44 14726static int
e142c38c 14727die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14728{
05cf31d1
JB
14729 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14730 which value is non-zero. However, we have to be careful with
14731 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14732 (via dwarf2_flag_true_p) follows this attribute. So we may
14733 end up accidently finding a declaration attribute that belongs
14734 to a different DIE referenced by the specification attribute,
14735 even though the given DIE does not have a declaration attribute. */
14736 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14737 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14738}
14739
63d06c5c 14740/* Return the die giving the specification for DIE, if there is
f2f0e013 14741 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14742 containing the return value on output. If there is no
14743 specification, but there is an abstract origin, that is
14744 returned. */
63d06c5c
DC
14745
14746static struct die_info *
f2f0e013 14747die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14748{
f2f0e013
DJ
14749 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14750 *spec_cu);
63d06c5c 14751
edb3359d
DJ
14752 if (spec_attr == NULL)
14753 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14754
63d06c5c
DC
14755 if (spec_attr == NULL)
14756 return NULL;
14757 else
f2f0e013 14758 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14759}
c906108c 14760
debd256d 14761/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14762 refers to.
14763 NOTE: This is also used as a "cleanup" function. */
14764
debd256d
JB
14765static void
14766free_line_header (struct line_header *lh)
14767{
14768 if (lh->standard_opcode_lengths)
a8bc7b56 14769 xfree (lh->standard_opcode_lengths);
debd256d
JB
14770
14771 /* Remember that all the lh->file_names[i].name pointers are
14772 pointers into debug_line_buffer, and don't need to be freed. */
14773 if (lh->file_names)
a8bc7b56 14774 xfree (lh->file_names);
debd256d
JB
14775
14776 /* Similarly for the include directory names. */
14777 if (lh->include_dirs)
a8bc7b56 14778 xfree (lh->include_dirs);
debd256d 14779
a8bc7b56 14780 xfree (lh);
debd256d
JB
14781}
14782
debd256d 14783/* Add an entry to LH's include directory table. */
ae2de4f8 14784
debd256d
JB
14785static void
14786add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14787{
debd256d
JB
14788 /* Grow the array if necessary. */
14789 if (lh->include_dirs_size == 0)
c5aa993b 14790 {
debd256d
JB
14791 lh->include_dirs_size = 1; /* for testing */
14792 lh->include_dirs = xmalloc (lh->include_dirs_size
14793 * sizeof (*lh->include_dirs));
14794 }
14795 else if (lh->num_include_dirs >= lh->include_dirs_size)
14796 {
14797 lh->include_dirs_size *= 2;
14798 lh->include_dirs = xrealloc (lh->include_dirs,
14799 (lh->include_dirs_size
14800 * sizeof (*lh->include_dirs)));
c5aa993b 14801 }
c906108c 14802
debd256d
JB
14803 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14804}
6e70227d 14805
debd256d 14806/* Add an entry to LH's file name table. */
ae2de4f8 14807
debd256d
JB
14808static void
14809add_file_name (struct line_header *lh,
14810 char *name,
14811 unsigned int dir_index,
14812 unsigned int mod_time,
14813 unsigned int length)
14814{
14815 struct file_entry *fe;
14816
14817 /* Grow the array if necessary. */
14818 if (lh->file_names_size == 0)
14819 {
14820 lh->file_names_size = 1; /* for testing */
14821 lh->file_names = xmalloc (lh->file_names_size
14822 * sizeof (*lh->file_names));
14823 }
14824 else if (lh->num_file_names >= lh->file_names_size)
14825 {
14826 lh->file_names_size *= 2;
14827 lh->file_names = xrealloc (lh->file_names,
14828 (lh->file_names_size
14829 * sizeof (*lh->file_names)));
14830 }
14831
14832 fe = &lh->file_names[lh->num_file_names++];
14833 fe->name = name;
14834 fe->dir_index = dir_index;
14835 fe->mod_time = mod_time;
14836 fe->length = length;
aaa75496 14837 fe->included_p = 0;
cb1df416 14838 fe->symtab = NULL;
debd256d 14839}
6e70227d 14840
36586728
TT
14841/* A convenience function to find the proper .debug_line section for a
14842 CU. */
14843
14844static struct dwarf2_section_info *
14845get_debug_line_section (struct dwarf2_cu *cu)
14846{
14847 struct dwarf2_section_info *section;
14848
14849 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14850 DWO file. */
14851 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14852 section = &cu->dwo_unit->dwo_file->sections.line;
14853 else if (cu->per_cu->is_dwz)
14854 {
14855 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14856
14857 section = &dwz->line;
14858 }
14859 else
14860 section = &dwarf2_per_objfile->line;
14861
14862 return section;
14863}
14864
debd256d 14865/* Read the statement program header starting at OFFSET in
3019eac3 14866 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14867 to a struct line_header, allocated using xmalloc.
debd256d
JB
14868
14869 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14870 the returned object point into the dwarf line section buffer,
14871 and must not be freed. */
ae2de4f8 14872
debd256d 14873static struct line_header *
3019eac3 14874dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14875{
14876 struct cleanup *back_to;
14877 struct line_header *lh;
fe1b8b76 14878 gdb_byte *line_ptr;
c764a876 14879 unsigned int bytes_read, offset_size;
debd256d
JB
14880 int i;
14881 char *cur_dir, *cur_file;
3019eac3
DE
14882 struct dwarf2_section_info *section;
14883 bfd *abfd;
14884
36586728 14885 section = get_debug_line_section (cu);
3019eac3
DE
14886 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14887 if (section->buffer == NULL)
debd256d 14888 {
3019eac3
DE
14889 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14890 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14891 else
14892 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14893 return 0;
14894 }
14895
fceca515
DE
14896 /* We can't do this until we know the section is non-empty.
14897 Only then do we know we have such a section. */
14898 abfd = section->asection->owner;
14899
a738430d
MK
14900 /* Make sure that at least there's room for the total_length field.
14901 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14902 if (offset + 4 >= section->size)
debd256d 14903 {
4d3c2250 14904 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14905 return 0;
14906 }
14907
14908 lh = xmalloc (sizeof (*lh));
14909 memset (lh, 0, sizeof (*lh));
14910 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14911 (void *) lh);
14912
3019eac3 14913 line_ptr = section->buffer + offset;
debd256d 14914
a738430d 14915 /* Read in the header. */
6e70227d 14916 lh->total_length =
c764a876
DE
14917 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14918 &bytes_read, &offset_size);
debd256d 14919 line_ptr += bytes_read;
3019eac3 14920 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14921 {
4d3c2250 14922 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14923 return 0;
14924 }
14925 lh->statement_program_end = line_ptr + lh->total_length;
14926 lh->version = read_2_bytes (abfd, line_ptr);
14927 line_ptr += 2;
c764a876
DE
14928 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14929 line_ptr += offset_size;
debd256d
JB
14930 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14931 line_ptr += 1;
2dc7f7b3
TT
14932 if (lh->version >= 4)
14933 {
14934 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14935 line_ptr += 1;
14936 }
14937 else
14938 lh->maximum_ops_per_instruction = 1;
14939
14940 if (lh->maximum_ops_per_instruction == 0)
14941 {
14942 lh->maximum_ops_per_instruction = 1;
14943 complaint (&symfile_complaints,
3e43a32a
MS
14944 _("invalid maximum_ops_per_instruction "
14945 "in `.debug_line' section"));
2dc7f7b3
TT
14946 }
14947
debd256d
JB
14948 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14949 line_ptr += 1;
14950 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14951 line_ptr += 1;
14952 lh->line_range = read_1_byte (abfd, line_ptr);
14953 line_ptr += 1;
14954 lh->opcode_base = read_1_byte (abfd, line_ptr);
14955 line_ptr += 1;
14956 lh->standard_opcode_lengths
fe1b8b76 14957 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
14958
14959 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14960 for (i = 1; i < lh->opcode_base; ++i)
14961 {
14962 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14963 line_ptr += 1;
14964 }
14965
a738430d 14966 /* Read directory table. */
9b1c24c8 14967 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14968 {
14969 line_ptr += bytes_read;
14970 add_include_dir (lh, cur_dir);
14971 }
14972 line_ptr += bytes_read;
14973
a738430d 14974 /* Read file name table. */
9b1c24c8 14975 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14976 {
14977 unsigned int dir_index, mod_time, length;
14978
14979 line_ptr += bytes_read;
14980 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14981 line_ptr += bytes_read;
14982 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14983 line_ptr += bytes_read;
14984 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14985 line_ptr += bytes_read;
14986
14987 add_file_name (lh, cur_file, dir_index, mod_time, length);
14988 }
14989 line_ptr += bytes_read;
6e70227d 14990 lh->statement_program_start = line_ptr;
debd256d 14991
3019eac3 14992 if (line_ptr > (section->buffer + section->size))
4d3c2250 14993 complaint (&symfile_complaints,
3e43a32a
MS
14994 _("line number info header doesn't "
14995 "fit in `.debug_line' section"));
debd256d
JB
14996
14997 discard_cleanups (back_to);
14998 return lh;
14999}
c906108c 15000
c6da4cef
DE
15001/* Subroutine of dwarf_decode_lines to simplify it.
15002 Return the file name of the psymtab for included file FILE_INDEX
15003 in line header LH of PST.
15004 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15005 If space for the result is malloc'd, it will be freed by a cleanup.
15006 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15007
15008static char *
15009psymtab_include_file_name (const struct line_header *lh, int file_index,
15010 const struct partial_symtab *pst,
15011 const char *comp_dir)
15012{
15013 const struct file_entry fe = lh->file_names [file_index];
15014 char *include_name = fe.name;
15015 char *include_name_to_compare = include_name;
15016 char *dir_name = NULL;
72b9f47f
TT
15017 const char *pst_filename;
15018 char *copied_name = NULL;
c6da4cef
DE
15019 int file_is_pst;
15020
15021 if (fe.dir_index)
15022 dir_name = lh->include_dirs[fe.dir_index - 1];
15023
15024 if (!IS_ABSOLUTE_PATH (include_name)
15025 && (dir_name != NULL || comp_dir != NULL))
15026 {
15027 /* Avoid creating a duplicate psymtab for PST.
15028 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15029 Before we do the comparison, however, we need to account
15030 for DIR_NAME and COMP_DIR.
15031 First prepend dir_name (if non-NULL). If we still don't
15032 have an absolute path prepend comp_dir (if non-NULL).
15033 However, the directory we record in the include-file's
15034 psymtab does not contain COMP_DIR (to match the
15035 corresponding symtab(s)).
15036
15037 Example:
15038
15039 bash$ cd /tmp
15040 bash$ gcc -g ./hello.c
15041 include_name = "hello.c"
15042 dir_name = "."
15043 DW_AT_comp_dir = comp_dir = "/tmp"
15044 DW_AT_name = "./hello.c" */
15045
15046 if (dir_name != NULL)
15047 {
15048 include_name = concat (dir_name, SLASH_STRING,
15049 include_name, (char *)NULL);
15050 include_name_to_compare = include_name;
15051 make_cleanup (xfree, include_name);
15052 }
15053 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15054 {
15055 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15056 include_name, (char *)NULL);
15057 }
15058 }
15059
15060 pst_filename = pst->filename;
15061 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15062 {
72b9f47f
TT
15063 copied_name = concat (pst->dirname, SLASH_STRING,
15064 pst_filename, (char *)NULL);
15065 pst_filename = copied_name;
c6da4cef
DE
15066 }
15067
1e3fad37 15068 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15069
15070 if (include_name_to_compare != include_name)
15071 xfree (include_name_to_compare);
72b9f47f
TT
15072 if (copied_name != NULL)
15073 xfree (copied_name);
c6da4cef
DE
15074
15075 if (file_is_pst)
15076 return NULL;
15077 return include_name;
15078}
15079
c91513d8
PP
15080/* Ignore this record_line request. */
15081
15082static void
15083noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15084{
15085 return;
15086}
15087
f3f5162e
DE
15088/* Subroutine of dwarf_decode_lines to simplify it.
15089 Process the line number information in LH. */
debd256d 15090
c906108c 15091static void
f3f5162e
DE
15092dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15093 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15094{
a8c50c1f 15095 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15096 gdb_byte *line_end;
a8c50c1f 15097 unsigned int bytes_read, extended_len;
c906108c 15098 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15099 CORE_ADDR baseaddr;
15100 struct objfile *objfile = cu->objfile;
f3f5162e 15101 bfd *abfd = objfile->obfd;
fbf65064 15102 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15103 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15104 struct subfile *last_subfile = NULL;
c91513d8
PP
15105 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15106 = record_line;
e142c38c
DJ
15107
15108 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15109
debd256d
JB
15110 line_ptr = lh->statement_program_start;
15111 line_end = lh->statement_program_end;
c906108c
SS
15112
15113 /* Read the statement sequences until there's nothing left. */
15114 while (line_ptr < line_end)
15115 {
15116 /* state machine registers */
15117 CORE_ADDR address = 0;
15118 unsigned int file = 1;
15119 unsigned int line = 1;
15120 unsigned int column = 0;
debd256d 15121 int is_stmt = lh->default_is_stmt;
c906108c
SS
15122 int basic_block = 0;
15123 int end_sequence = 0;
fbf65064 15124 CORE_ADDR addr;
2dc7f7b3 15125 unsigned char op_index = 0;
c906108c 15126
aaa75496 15127 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15128 {
aaa75496 15129 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15130 /* lh->include_dirs and lh->file_names are 0-based, but the
15131 directory and file name numbers in the statement program
15132 are 1-based. */
15133 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15134 char *dir = NULL;
a738430d 15135
debd256d
JB
15136 if (fe->dir_index)
15137 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15138
15139 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15140 }
15141
a738430d 15142 /* Decode the table. */
c5aa993b 15143 while (!end_sequence)
c906108c
SS
15144 {
15145 op_code = read_1_byte (abfd, line_ptr);
15146 line_ptr += 1;
59205f5a
JB
15147 if (line_ptr > line_end)
15148 {
15149 dwarf2_debug_line_missing_end_sequence_complaint ();
15150 break;
15151 }
9aa1fe7e 15152
debd256d 15153 if (op_code >= lh->opcode_base)
6e70227d 15154 {
a738430d 15155 /* Special operand. */
debd256d 15156 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15157 address += (((op_index + (adj_opcode / lh->line_range))
15158 / lh->maximum_ops_per_instruction)
15159 * lh->minimum_instruction_length);
15160 op_index = ((op_index + (adj_opcode / lh->line_range))
15161 % lh->maximum_ops_per_instruction);
debd256d 15162 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15163 if (lh->num_file_names < file || file == 0)
25e43795 15164 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15165 /* For now we ignore lines not starting on an
15166 instruction boundary. */
15167 else if (op_index == 0)
25e43795
DJ
15168 {
15169 lh->file_names[file - 1].included_p = 1;
ca5f395d 15170 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15171 {
15172 if (last_subfile != current_subfile)
15173 {
15174 addr = gdbarch_addr_bits_remove (gdbarch, address);
15175 if (last_subfile)
c91513d8 15176 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15177 last_subfile = current_subfile;
15178 }
25e43795 15179 /* Append row to matrix using current values. */
7019d805 15180 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15181 (*p_record_line) (current_subfile, line, addr);
366da635 15182 }
25e43795 15183 }
ca5f395d 15184 basic_block = 0;
9aa1fe7e
GK
15185 }
15186 else switch (op_code)
c906108c
SS
15187 {
15188 case DW_LNS_extended_op:
3e43a32a
MS
15189 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15190 &bytes_read);
473b7be6 15191 line_ptr += bytes_read;
a8c50c1f 15192 extended_end = line_ptr + extended_len;
c906108c
SS
15193 extended_op = read_1_byte (abfd, line_ptr);
15194 line_ptr += 1;
15195 switch (extended_op)
15196 {
15197 case DW_LNE_end_sequence:
c91513d8 15198 p_record_line = record_line;
c906108c 15199 end_sequence = 1;
c906108c
SS
15200 break;
15201 case DW_LNE_set_address:
e7c27a73 15202 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15203
15204 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15205 {
15206 /* This line table is for a function which has been
15207 GCd by the linker. Ignore it. PR gdb/12528 */
15208
15209 long line_offset
36586728 15210 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15211
15212 complaint (&symfile_complaints,
15213 _(".debug_line address at offset 0x%lx is 0 "
15214 "[in module %s]"),
bb5ed363 15215 line_offset, objfile->name);
c91513d8
PP
15216 p_record_line = noop_record_line;
15217 }
15218
2dc7f7b3 15219 op_index = 0;
107d2387
AC
15220 line_ptr += bytes_read;
15221 address += baseaddr;
c906108c
SS
15222 break;
15223 case DW_LNE_define_file:
debd256d
JB
15224 {
15225 char *cur_file;
15226 unsigned int dir_index, mod_time, length;
6e70227d 15227
3e43a32a
MS
15228 cur_file = read_direct_string (abfd, line_ptr,
15229 &bytes_read);
debd256d
JB
15230 line_ptr += bytes_read;
15231 dir_index =
15232 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15233 line_ptr += bytes_read;
15234 mod_time =
15235 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15236 line_ptr += bytes_read;
15237 length =
15238 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15239 line_ptr += bytes_read;
15240 add_file_name (lh, cur_file, dir_index, mod_time, length);
15241 }
c906108c 15242 break;
d0c6ba3d
CC
15243 case DW_LNE_set_discriminator:
15244 /* The discriminator is not interesting to the debugger;
15245 just ignore it. */
15246 line_ptr = extended_end;
15247 break;
c906108c 15248 default:
4d3c2250 15249 complaint (&symfile_complaints,
e2e0b3e5 15250 _("mangled .debug_line section"));
debd256d 15251 return;
c906108c 15252 }
a8c50c1f
DJ
15253 /* Make sure that we parsed the extended op correctly. If e.g.
15254 we expected a different address size than the producer used,
15255 we may have read the wrong number of bytes. */
15256 if (line_ptr != extended_end)
15257 {
15258 complaint (&symfile_complaints,
15259 _("mangled .debug_line section"));
15260 return;
15261 }
c906108c
SS
15262 break;
15263 case DW_LNS_copy:
59205f5a 15264 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15265 dwarf2_debug_line_missing_file_complaint ();
15266 else
366da635 15267 {
25e43795 15268 lh->file_names[file - 1].included_p = 1;
ca5f395d 15269 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15270 {
15271 if (last_subfile != current_subfile)
15272 {
15273 addr = gdbarch_addr_bits_remove (gdbarch, address);
15274 if (last_subfile)
c91513d8 15275 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15276 last_subfile = current_subfile;
15277 }
7019d805 15278 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15279 (*p_record_line) (current_subfile, line, addr);
fbf65064 15280 }
366da635 15281 }
c906108c
SS
15282 basic_block = 0;
15283 break;
15284 case DW_LNS_advance_pc:
2dc7f7b3
TT
15285 {
15286 CORE_ADDR adjust
15287 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15288
15289 address += (((op_index + adjust)
15290 / lh->maximum_ops_per_instruction)
15291 * lh->minimum_instruction_length);
15292 op_index = ((op_index + adjust)
15293 % lh->maximum_ops_per_instruction);
15294 line_ptr += bytes_read;
15295 }
c906108c
SS
15296 break;
15297 case DW_LNS_advance_line:
15298 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15299 line_ptr += bytes_read;
15300 break;
15301 case DW_LNS_set_file:
debd256d 15302 {
a738430d
MK
15303 /* The arrays lh->include_dirs and lh->file_names are
15304 0-based, but the directory and file name numbers in
15305 the statement program are 1-based. */
debd256d 15306 struct file_entry *fe;
4f1520fb 15307 char *dir = NULL;
a738430d 15308
debd256d
JB
15309 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15310 line_ptr += bytes_read;
59205f5a 15311 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15312 dwarf2_debug_line_missing_file_complaint ();
15313 else
15314 {
15315 fe = &lh->file_names[file - 1];
15316 if (fe->dir_index)
15317 dir = lh->include_dirs[fe->dir_index - 1];
15318 if (!decode_for_pst_p)
15319 {
15320 last_subfile = current_subfile;
15321 dwarf2_start_subfile (fe->name, dir, comp_dir);
15322 }
15323 }
debd256d 15324 }
c906108c
SS
15325 break;
15326 case DW_LNS_set_column:
15327 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15328 line_ptr += bytes_read;
15329 break;
15330 case DW_LNS_negate_stmt:
15331 is_stmt = (!is_stmt);
15332 break;
15333 case DW_LNS_set_basic_block:
15334 basic_block = 1;
15335 break;
c2c6d25f
JM
15336 /* Add to the address register of the state machine the
15337 address increment value corresponding to special opcode
a738430d
MK
15338 255. I.e., this value is scaled by the minimum
15339 instruction length since special opcode 255 would have
b021a221 15340 scaled the increment. */
c906108c 15341 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15342 {
15343 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15344
15345 address += (((op_index + adjust)
15346 / lh->maximum_ops_per_instruction)
15347 * lh->minimum_instruction_length);
15348 op_index = ((op_index + adjust)
15349 % lh->maximum_ops_per_instruction);
15350 }
c906108c
SS
15351 break;
15352 case DW_LNS_fixed_advance_pc:
15353 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15354 op_index = 0;
c906108c
SS
15355 line_ptr += 2;
15356 break;
9aa1fe7e 15357 default:
a738430d
MK
15358 {
15359 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15360 int i;
a738430d 15361
debd256d 15362 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15363 {
15364 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15365 line_ptr += bytes_read;
15366 }
15367 }
c906108c
SS
15368 }
15369 }
59205f5a
JB
15370 if (lh->num_file_names < file || file == 0)
15371 dwarf2_debug_line_missing_file_complaint ();
15372 else
15373 {
15374 lh->file_names[file - 1].included_p = 1;
15375 if (!decode_for_pst_p)
fbf65064
UW
15376 {
15377 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15378 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15379 }
59205f5a 15380 }
c906108c 15381 }
f3f5162e
DE
15382}
15383
15384/* Decode the Line Number Program (LNP) for the given line_header
15385 structure and CU. The actual information extracted and the type
15386 of structures created from the LNP depends on the value of PST.
15387
15388 1. If PST is NULL, then this procedure uses the data from the program
15389 to create all necessary symbol tables, and their linetables.
15390
15391 2. If PST is not NULL, this procedure reads the program to determine
15392 the list of files included by the unit represented by PST, and
15393 builds all the associated partial symbol tables.
15394
15395 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15396 It is used for relative paths in the line table.
15397 NOTE: When processing partial symtabs (pst != NULL),
15398 comp_dir == pst->dirname.
15399
15400 NOTE: It is important that psymtabs have the same file name (via strcmp)
15401 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15402 symtab we don't use it in the name of the psymtabs we create.
15403 E.g. expand_line_sal requires this when finding psymtabs to expand.
15404 A good testcase for this is mb-inline.exp. */
15405
15406static void
15407dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15408 struct dwarf2_cu *cu, struct partial_symtab *pst,
15409 int want_line_info)
15410{
15411 struct objfile *objfile = cu->objfile;
15412 const int decode_for_pst_p = (pst != NULL);
15413 struct subfile *first_subfile = current_subfile;
15414
15415 if (want_line_info)
15416 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15417
15418 if (decode_for_pst_p)
15419 {
15420 int file_index;
15421
15422 /* Now that we're done scanning the Line Header Program, we can
15423 create the psymtab of each included file. */
15424 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15425 if (lh->file_names[file_index].included_p == 1)
15426 {
c6da4cef
DE
15427 char *include_name =
15428 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15429 if (include_name != NULL)
aaa75496
JB
15430 dwarf2_create_include_psymtab (include_name, pst, objfile);
15431 }
15432 }
cb1df416
DJ
15433 else
15434 {
15435 /* Make sure a symtab is created for every file, even files
15436 which contain only variables (i.e. no code with associated
15437 line numbers). */
cb1df416 15438 int i;
cb1df416
DJ
15439
15440 for (i = 0; i < lh->num_file_names; i++)
15441 {
15442 char *dir = NULL;
f3f5162e 15443 struct file_entry *fe;
9a619af0 15444
cb1df416
DJ
15445 fe = &lh->file_names[i];
15446 if (fe->dir_index)
15447 dir = lh->include_dirs[fe->dir_index - 1];
15448 dwarf2_start_subfile (fe->name, dir, comp_dir);
15449
15450 /* Skip the main file; we don't need it, and it must be
15451 allocated last, so that it will show up before the
15452 non-primary symtabs in the objfile's symtab list. */
15453 if (current_subfile == first_subfile)
15454 continue;
15455
15456 if (current_subfile->symtab == NULL)
15457 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15458 objfile);
cb1df416
DJ
15459 fe->symtab = current_subfile->symtab;
15460 }
15461 }
c906108c
SS
15462}
15463
15464/* Start a subfile for DWARF. FILENAME is the name of the file and
15465 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15466 or NULL if not known. COMP_DIR is the compilation directory for the
15467 linetable's compilation unit or NULL if not known.
c906108c
SS
15468 This routine tries to keep line numbers from identical absolute and
15469 relative file names in a common subfile.
15470
15471 Using the `list' example from the GDB testsuite, which resides in
15472 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15473 of /srcdir/list0.c yields the following debugging information for list0.c:
15474
c5aa993b
JM
15475 DW_AT_name: /srcdir/list0.c
15476 DW_AT_comp_dir: /compdir
357e46e7 15477 files.files[0].name: list0.h
c5aa993b 15478 files.files[0].dir: /srcdir
357e46e7 15479 files.files[1].name: list0.c
c5aa993b 15480 files.files[1].dir: /srcdir
c906108c
SS
15481
15482 The line number information for list0.c has to end up in a single
4f1520fb
FR
15483 subfile, so that `break /srcdir/list0.c:1' works as expected.
15484 start_subfile will ensure that this happens provided that we pass the
15485 concatenation of files.files[1].dir and files.files[1].name as the
15486 subfile's name. */
c906108c
SS
15487
15488static void
3e43a32a
MS
15489dwarf2_start_subfile (char *filename, const char *dirname,
15490 const char *comp_dir)
c906108c 15491{
4f1520fb
FR
15492 char *fullname;
15493
15494 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15495 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15496 second argument to start_subfile. To be consistent, we do the
15497 same here. In order not to lose the line information directory,
15498 we concatenate it to the filename when it makes sense.
15499 Note that the Dwarf3 standard says (speaking of filenames in line
15500 information): ``The directory index is ignored for file names
15501 that represent full path names''. Thus ignoring dirname in the
15502 `else' branch below isn't an issue. */
c906108c 15503
d5166ae1 15504 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15505 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15506 else
15507 fullname = filename;
c906108c 15508
4f1520fb
FR
15509 start_subfile (fullname, comp_dir);
15510
15511 if (fullname != filename)
15512 xfree (fullname);
c906108c
SS
15513}
15514
f4dc4d17
DE
15515/* Start a symtab for DWARF.
15516 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15517
15518static void
15519dwarf2_start_symtab (struct dwarf2_cu *cu,
15520 char *name, char *comp_dir, CORE_ADDR low_pc)
15521{
15522 start_symtab (name, comp_dir, low_pc);
15523 record_debugformat ("DWARF 2");
15524 record_producer (cu->producer);
15525
15526 /* We assume that we're processing GCC output. */
15527 processing_gcc_compilation = 2;
15528
15529 processing_has_namespace_info = 0;
15530}
15531
4c2df51b
DJ
15532static void
15533var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15534 struct dwarf2_cu *cu)
4c2df51b 15535{
e7c27a73
DJ
15536 struct objfile *objfile = cu->objfile;
15537 struct comp_unit_head *cu_header = &cu->header;
15538
4c2df51b
DJ
15539 /* NOTE drow/2003-01-30: There used to be a comment and some special
15540 code here to turn a symbol with DW_AT_external and a
15541 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15542 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15543 with some versions of binutils) where shared libraries could have
15544 relocations against symbols in their debug information - the
15545 minimal symbol would have the right address, but the debug info
15546 would not. It's no longer necessary, because we will explicitly
15547 apply relocations when we read in the debug information now. */
15548
15549 /* A DW_AT_location attribute with no contents indicates that a
15550 variable has been optimized away. */
15551 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15552 {
15553 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15554 return;
15555 }
15556
15557 /* Handle one degenerate form of location expression specially, to
15558 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15559 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15560 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15561
15562 if (attr_form_is_block (attr)
3019eac3
DE
15563 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15564 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15565 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15566 && (DW_BLOCK (attr)->size
15567 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15568 {
891d2f0b 15569 unsigned int dummy;
4c2df51b 15570
3019eac3
DE
15571 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15572 SYMBOL_VALUE_ADDRESS (sym) =
15573 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15574 else
15575 SYMBOL_VALUE_ADDRESS (sym) =
15576 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15577 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15578 fixup_symbol_section (sym, objfile);
15579 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15580 SYMBOL_SECTION (sym));
4c2df51b
DJ
15581 return;
15582 }
15583
15584 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15585 expression evaluator, and use LOC_COMPUTED only when necessary
15586 (i.e. when the value of a register or memory location is
15587 referenced, or a thread-local block, etc.). Then again, it might
15588 not be worthwhile. I'm assuming that it isn't unless performance
15589 or memory numbers show me otherwise. */
15590
e7c27a73 15591 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15592 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15593
15594 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15595 cu->has_loclist = 1;
4c2df51b
DJ
15596}
15597
c906108c
SS
15598/* Given a pointer to a DWARF information entry, figure out if we need
15599 to make a symbol table entry for it, and if so, create a new entry
15600 and return a pointer to it.
15601 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15602 used the passed type.
15603 If SPACE is not NULL, use it to hold the new symbol. If it is
15604 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15605
15606static struct symbol *
34eaf542
TT
15607new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15608 struct symbol *space)
c906108c 15609{
e7c27a73 15610 struct objfile *objfile = cu->objfile;
c906108c
SS
15611 struct symbol *sym = NULL;
15612 char *name;
15613 struct attribute *attr = NULL;
15614 struct attribute *attr2 = NULL;
e142c38c 15615 CORE_ADDR baseaddr;
e37fd15a
SW
15616 struct pending **list_to_add = NULL;
15617
edb3359d 15618 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15619
15620 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15621
94af9270 15622 name = dwarf2_name (die, cu);
c906108c
SS
15623 if (name)
15624 {
94af9270 15625 const char *linkagename;
34eaf542 15626 int suppress_add = 0;
94af9270 15627
34eaf542
TT
15628 if (space)
15629 sym = space;
15630 else
15631 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15632 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15633
15634 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15635 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15636 linkagename = dwarf2_physname (name, die, cu);
15637 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15638
f55ee35c
JK
15639 /* Fortran does not have mangling standard and the mangling does differ
15640 between gfortran, iFort etc. */
15641 if (cu->language == language_fortran
b250c185 15642 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
15643 symbol_set_demangled_name (&(sym->ginfo),
15644 (char *) dwarf2_full_name (name, die, cu),
15645 NULL);
f55ee35c 15646
c906108c 15647 /* Default assumptions.
c5aa993b 15648 Use the passed type or decode it from the die. */
176620f1 15649 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15650 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15651 if (type != NULL)
15652 SYMBOL_TYPE (sym) = type;
15653 else
e7c27a73 15654 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15655 attr = dwarf2_attr (die,
15656 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15657 cu);
c906108c
SS
15658 if (attr)
15659 {
15660 SYMBOL_LINE (sym) = DW_UNSND (attr);
15661 }
cb1df416 15662
edb3359d
DJ
15663 attr = dwarf2_attr (die,
15664 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15665 cu);
cb1df416
DJ
15666 if (attr)
15667 {
15668 int file_index = DW_UNSND (attr);
9a619af0 15669
cb1df416
DJ
15670 if (cu->line_header == NULL
15671 || file_index > cu->line_header->num_file_names)
15672 complaint (&symfile_complaints,
15673 _("file index out of range"));
1c3d648d 15674 else if (file_index > 0)
cb1df416
DJ
15675 {
15676 struct file_entry *fe;
9a619af0 15677
cb1df416
DJ
15678 fe = &cu->line_header->file_names[file_index - 1];
15679 SYMBOL_SYMTAB (sym) = fe->symtab;
15680 }
15681 }
15682
c906108c
SS
15683 switch (die->tag)
15684 {
15685 case DW_TAG_label:
e142c38c 15686 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15687 if (attr)
15688 {
15689 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15690 }
0f5238ed
TT
15691 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15692 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15693 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15694 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15695 break;
15696 case DW_TAG_subprogram:
15697 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15698 finish_block. */
15699 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15700 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15701 if ((attr2 && (DW_UNSND (attr2) != 0))
15702 || cu->language == language_ada)
c906108c 15703 {
2cfa0c8d
JB
15704 /* Subprograms marked external are stored as a global symbol.
15705 Ada subprograms, whether marked external or not, are always
15706 stored as a global symbol, because we want to be able to
15707 access them globally. For instance, we want to be able
15708 to break on a nested subprogram without having to
15709 specify the context. */
e37fd15a 15710 list_to_add = &global_symbols;
c906108c
SS
15711 }
15712 else
15713 {
e37fd15a 15714 list_to_add = cu->list_in_scope;
c906108c
SS
15715 }
15716 break;
edb3359d
DJ
15717 case DW_TAG_inlined_subroutine:
15718 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15719 finish_block. */
15720 SYMBOL_CLASS (sym) = LOC_BLOCK;
15721 SYMBOL_INLINED (sym) = 1;
481860b3 15722 list_to_add = cu->list_in_scope;
edb3359d 15723 break;
34eaf542
TT
15724 case DW_TAG_template_value_param:
15725 suppress_add = 1;
15726 /* Fall through. */
72929c62 15727 case DW_TAG_constant:
c906108c 15728 case DW_TAG_variable:
254e6b9e 15729 case DW_TAG_member:
0963b4bd
MS
15730 /* Compilation with minimal debug info may result in
15731 variables with missing type entries. Change the
15732 misleading `void' type to something sensible. */
c906108c 15733 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15734 SYMBOL_TYPE (sym)
46bf5051 15735 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15736
e142c38c 15737 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15738 /* In the case of DW_TAG_member, we should only be called for
15739 static const members. */
15740 if (die->tag == DW_TAG_member)
15741 {
3863f96c
DE
15742 /* dwarf2_add_field uses die_is_declaration,
15743 so we do the same. */
254e6b9e
DE
15744 gdb_assert (die_is_declaration (die, cu));
15745 gdb_assert (attr);
15746 }
c906108c
SS
15747 if (attr)
15748 {
e7c27a73 15749 dwarf2_const_value (attr, sym, cu);
e142c38c 15750 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15751 if (!suppress_add)
34eaf542
TT
15752 {
15753 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15754 list_to_add = &global_symbols;
34eaf542 15755 else
e37fd15a 15756 list_to_add = cu->list_in_scope;
34eaf542 15757 }
c906108c
SS
15758 break;
15759 }
e142c38c 15760 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15761 if (attr)
15762 {
e7c27a73 15763 var_decode_location (attr, sym, cu);
e142c38c 15764 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15765
15766 /* Fortran explicitly imports any global symbols to the local
15767 scope by DW_TAG_common_block. */
15768 if (cu->language == language_fortran && die->parent
15769 && die->parent->tag == DW_TAG_common_block)
15770 attr2 = NULL;
15771
caac4577
JG
15772 if (SYMBOL_CLASS (sym) == LOC_STATIC
15773 && SYMBOL_VALUE_ADDRESS (sym) == 0
15774 && !dwarf2_per_objfile->has_section_at_zero)
15775 {
15776 /* When a static variable is eliminated by the linker,
15777 the corresponding debug information is not stripped
15778 out, but the variable address is set to null;
15779 do not add such variables into symbol table. */
15780 }
15781 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15782 {
f55ee35c
JK
15783 /* Workaround gfortran PR debug/40040 - it uses
15784 DW_AT_location for variables in -fPIC libraries which may
15785 get overriden by other libraries/executable and get
15786 a different address. Resolve it by the minimal symbol
15787 which may come from inferior's executable using copy
15788 relocation. Make this workaround only for gfortran as for
15789 other compilers GDB cannot guess the minimal symbol
15790 Fortran mangling kind. */
15791 if (cu->language == language_fortran && die->parent
15792 && die->parent->tag == DW_TAG_module
15793 && cu->producer
15794 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15795 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15796
1c809c68
TT
15797 /* A variable with DW_AT_external is never static,
15798 but it may be block-scoped. */
15799 list_to_add = (cu->list_in_scope == &file_symbols
15800 ? &global_symbols : cu->list_in_scope);
1c809c68 15801 }
c906108c 15802 else
e37fd15a 15803 list_to_add = cu->list_in_scope;
c906108c
SS
15804 }
15805 else
15806 {
15807 /* We do not know the address of this symbol.
c5aa993b
JM
15808 If it is an external symbol and we have type information
15809 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15810 The address of the variable will then be determined from
15811 the minimal symbol table whenever the variable is
15812 referenced. */
e142c38c 15813 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15814
15815 /* Fortran explicitly imports any global symbols to the local
15816 scope by DW_TAG_common_block. */
15817 if (cu->language == language_fortran && die->parent
15818 && die->parent->tag == DW_TAG_common_block)
15819 {
15820 /* SYMBOL_CLASS doesn't matter here because
15821 read_common_block is going to reset it. */
15822 if (!suppress_add)
15823 list_to_add = cu->list_in_scope;
15824 }
15825 else if (attr2 && (DW_UNSND (attr2) != 0)
15826 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15827 {
0fe7935b
DJ
15828 /* A variable with DW_AT_external is never static, but it
15829 may be block-scoped. */
15830 list_to_add = (cu->list_in_scope == &file_symbols
15831 ? &global_symbols : cu->list_in_scope);
15832
c906108c 15833 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15834 }
442ddf59
JK
15835 else if (!die_is_declaration (die, cu))
15836 {
15837 /* Use the default LOC_OPTIMIZED_OUT class. */
15838 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15839 if (!suppress_add)
15840 list_to_add = cu->list_in_scope;
442ddf59 15841 }
c906108c
SS
15842 }
15843 break;
15844 case DW_TAG_formal_parameter:
edb3359d
DJ
15845 /* If we are inside a function, mark this as an argument. If
15846 not, we might be looking at an argument to an inlined function
15847 when we do not have enough information to show inlined frames;
15848 pretend it's a local variable in that case so that the user can
15849 still see it. */
15850 if (context_stack_depth > 0
15851 && context_stack[context_stack_depth - 1].name != NULL)
15852 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15853 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15854 if (attr)
15855 {
e7c27a73 15856 var_decode_location (attr, sym, cu);
c906108c 15857 }
e142c38c 15858 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15859 if (attr)
15860 {
e7c27a73 15861 dwarf2_const_value (attr, sym, cu);
c906108c 15862 }
f346a30d 15863
e37fd15a 15864 list_to_add = cu->list_in_scope;
c906108c
SS
15865 break;
15866 case DW_TAG_unspecified_parameters:
15867 /* From varargs functions; gdb doesn't seem to have any
15868 interest in this information, so just ignore it for now.
15869 (FIXME?) */
15870 break;
34eaf542
TT
15871 case DW_TAG_template_type_param:
15872 suppress_add = 1;
15873 /* Fall through. */
c906108c 15874 case DW_TAG_class_type:
680b30c7 15875 case DW_TAG_interface_type:
c906108c
SS
15876 case DW_TAG_structure_type:
15877 case DW_TAG_union_type:
72019c9c 15878 case DW_TAG_set_type:
c906108c
SS
15879 case DW_TAG_enumeration_type:
15880 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15881 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15882
63d06c5c 15883 {
987504bb 15884 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15885 really ever be static objects: otherwise, if you try
15886 to, say, break of a class's method and you're in a file
15887 which doesn't mention that class, it won't work unless
15888 the check for all static symbols in lookup_symbol_aux
15889 saves you. See the OtherFileClass tests in
15890 gdb.c++/namespace.exp. */
15891
e37fd15a 15892 if (!suppress_add)
34eaf542 15893 {
34eaf542
TT
15894 list_to_add = (cu->list_in_scope == &file_symbols
15895 && (cu->language == language_cplus
15896 || cu->language == language_java)
15897 ? &global_symbols : cu->list_in_scope);
63d06c5c 15898
64382290
TT
15899 /* The semantics of C++ state that "struct foo {
15900 ... }" also defines a typedef for "foo". A Java
15901 class declaration also defines a typedef for the
15902 class. */
15903 if (cu->language == language_cplus
15904 || cu->language == language_java
15905 || cu->language == language_ada)
15906 {
15907 /* The symbol's name is already allocated along
15908 with this objfile, so we don't need to
15909 duplicate it for the type. */
15910 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15911 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15912 }
63d06c5c
DC
15913 }
15914 }
c906108c
SS
15915 break;
15916 case DW_TAG_typedef:
63d06c5c
DC
15917 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15918 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15919 list_to_add = cu->list_in_scope;
63d06c5c 15920 break;
c906108c 15921 case DW_TAG_base_type:
a02abb62 15922 case DW_TAG_subrange_type:
c906108c 15923 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15924 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15925 list_to_add = cu->list_in_scope;
c906108c
SS
15926 break;
15927 case DW_TAG_enumerator:
e142c38c 15928 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15929 if (attr)
15930 {
e7c27a73 15931 dwarf2_const_value (attr, sym, cu);
c906108c 15932 }
63d06c5c
DC
15933 {
15934 /* NOTE: carlton/2003-11-10: See comment above in the
15935 DW_TAG_class_type, etc. block. */
15936
e142c38c 15937 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15938 && (cu->language == language_cplus
15939 || cu->language == language_java)
e142c38c 15940 ? &global_symbols : cu->list_in_scope);
63d06c5c 15941 }
c906108c 15942 break;
5c4e30ca
DC
15943 case DW_TAG_namespace:
15944 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15945 list_to_add = &global_symbols;
5c4e30ca 15946 break;
4357ac6c
TT
15947 case DW_TAG_common_block:
15948 SYMBOL_CLASS (sym) = LOC_STATIC;
15949 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15950 add_symbol_to_list (sym, cu->list_in_scope);
15951 break;
c906108c
SS
15952 default:
15953 /* Not a tag we recognize. Hopefully we aren't processing
15954 trash data, but since we must specifically ignore things
15955 we don't recognize, there is nothing else we should do at
0963b4bd 15956 this point. */
e2e0b3e5 15957 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 15958 dwarf_tag_name (die->tag));
c906108c
SS
15959 break;
15960 }
df8a16a1 15961
e37fd15a
SW
15962 if (suppress_add)
15963 {
15964 sym->hash_next = objfile->template_symbols;
15965 objfile->template_symbols = sym;
15966 list_to_add = NULL;
15967 }
15968
15969 if (list_to_add != NULL)
15970 add_symbol_to_list (sym, list_to_add);
15971
df8a16a1
DJ
15972 /* For the benefit of old versions of GCC, check for anonymous
15973 namespaces based on the demangled name. */
15974 if (!processing_has_namespace_info
94af9270 15975 && cu->language == language_cplus)
a10964d1 15976 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
15977 }
15978 return (sym);
15979}
15980
34eaf542
TT
15981/* A wrapper for new_symbol_full that always allocates a new symbol. */
15982
15983static struct symbol *
15984new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15985{
15986 return new_symbol_full (die, type, cu, NULL);
15987}
15988
98bfdba5
PA
15989/* Given an attr with a DW_FORM_dataN value in host byte order,
15990 zero-extend it as appropriate for the symbol's type. The DWARF
15991 standard (v4) is not entirely clear about the meaning of using
15992 DW_FORM_dataN for a constant with a signed type, where the type is
15993 wider than the data. The conclusion of a discussion on the DWARF
15994 list was that this is unspecified. We choose to always zero-extend
15995 because that is the interpretation long in use by GCC. */
c906108c 15996
98bfdba5
PA
15997static gdb_byte *
15998dwarf2_const_value_data (struct attribute *attr, struct type *type,
15999 const char *name, struct obstack *obstack,
12df843f 16000 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16001{
e7c27a73 16002 struct objfile *objfile = cu->objfile;
e17a4113
UW
16003 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16004 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16005 LONGEST l = DW_UNSND (attr);
16006
16007 if (bits < sizeof (*value) * 8)
16008 {
16009 l &= ((LONGEST) 1 << bits) - 1;
16010 *value = l;
16011 }
16012 else if (bits == sizeof (*value) * 8)
16013 *value = l;
16014 else
16015 {
16016 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16017 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16018 return bytes;
16019 }
16020
16021 return NULL;
16022}
16023
16024/* Read a constant value from an attribute. Either set *VALUE, or if
16025 the value does not fit in *VALUE, set *BYTES - either already
16026 allocated on the objfile obstack, or newly allocated on OBSTACK,
16027 or, set *BATON, if we translated the constant to a location
16028 expression. */
16029
16030static void
16031dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16032 const char *name, struct obstack *obstack,
16033 struct dwarf2_cu *cu,
12df843f 16034 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16035 struct dwarf2_locexpr_baton **baton)
16036{
16037 struct objfile *objfile = cu->objfile;
16038 struct comp_unit_head *cu_header = &cu->header;
c906108c 16039 struct dwarf_block *blk;
98bfdba5
PA
16040 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16041 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16042
16043 *value = 0;
16044 *bytes = NULL;
16045 *baton = NULL;
c906108c
SS
16046
16047 switch (attr->form)
16048 {
16049 case DW_FORM_addr:
3019eac3 16050 case DW_FORM_GNU_addr_index:
ac56253d 16051 {
ac56253d
TT
16052 gdb_byte *data;
16053
98bfdba5
PA
16054 if (TYPE_LENGTH (type) != cu_header->addr_size)
16055 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16056 cu_header->addr_size,
98bfdba5 16057 TYPE_LENGTH (type));
ac56253d
TT
16058 /* Symbols of this form are reasonably rare, so we just
16059 piggyback on the existing location code rather than writing
16060 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16061 *baton = obstack_alloc (&objfile->objfile_obstack,
16062 sizeof (struct dwarf2_locexpr_baton));
16063 (*baton)->per_cu = cu->per_cu;
16064 gdb_assert ((*baton)->per_cu);
ac56253d 16065
98bfdba5
PA
16066 (*baton)->size = 2 + cu_header->addr_size;
16067 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16068 (*baton)->data = data;
ac56253d
TT
16069
16070 data[0] = DW_OP_addr;
16071 store_unsigned_integer (&data[1], cu_header->addr_size,
16072 byte_order, DW_ADDR (attr));
16073 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16074 }
c906108c 16075 break;
4ac36638 16076 case DW_FORM_string:
93b5768b 16077 case DW_FORM_strp:
3019eac3 16078 case DW_FORM_GNU_str_index:
36586728 16079 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16080 /* DW_STRING is already allocated on the objfile obstack, point
16081 directly to it. */
16082 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16083 break;
c906108c
SS
16084 case DW_FORM_block1:
16085 case DW_FORM_block2:
16086 case DW_FORM_block4:
16087 case DW_FORM_block:
2dc7f7b3 16088 case DW_FORM_exprloc:
c906108c 16089 blk = DW_BLOCK (attr);
98bfdba5
PA
16090 if (TYPE_LENGTH (type) != blk->size)
16091 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16092 TYPE_LENGTH (type));
16093 *bytes = blk->data;
c906108c 16094 break;
2df3850c
JM
16095
16096 /* The DW_AT_const_value attributes are supposed to carry the
16097 symbol's value "represented as it would be on the target
16098 architecture." By the time we get here, it's already been
16099 converted to host endianness, so we just need to sign- or
16100 zero-extend it as appropriate. */
16101 case DW_FORM_data1:
3e43a32a
MS
16102 *bytes = dwarf2_const_value_data (attr, type, name,
16103 obstack, cu, value, 8);
2df3850c 16104 break;
c906108c 16105 case DW_FORM_data2:
3e43a32a
MS
16106 *bytes = dwarf2_const_value_data (attr, type, name,
16107 obstack, cu, value, 16);
2df3850c 16108 break;
c906108c 16109 case DW_FORM_data4:
3e43a32a
MS
16110 *bytes = dwarf2_const_value_data (attr, type, name,
16111 obstack, cu, value, 32);
2df3850c 16112 break;
c906108c 16113 case DW_FORM_data8:
3e43a32a
MS
16114 *bytes = dwarf2_const_value_data (attr, type, name,
16115 obstack, cu, value, 64);
2df3850c
JM
16116 break;
16117
c906108c 16118 case DW_FORM_sdata:
98bfdba5 16119 *value = DW_SND (attr);
2df3850c
JM
16120 break;
16121
c906108c 16122 case DW_FORM_udata:
98bfdba5 16123 *value = DW_UNSND (attr);
c906108c 16124 break;
2df3850c 16125
c906108c 16126 default:
4d3c2250 16127 complaint (&symfile_complaints,
e2e0b3e5 16128 _("unsupported const value attribute form: '%s'"),
4d3c2250 16129 dwarf_form_name (attr->form));
98bfdba5 16130 *value = 0;
c906108c
SS
16131 break;
16132 }
16133}
16134
2df3850c 16135
98bfdba5
PA
16136/* Copy constant value from an attribute to a symbol. */
16137
2df3850c 16138static void
98bfdba5
PA
16139dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16140 struct dwarf2_cu *cu)
2df3850c 16141{
98bfdba5
PA
16142 struct objfile *objfile = cu->objfile;
16143 struct comp_unit_head *cu_header = &cu->header;
12df843f 16144 LONGEST value;
98bfdba5
PA
16145 gdb_byte *bytes;
16146 struct dwarf2_locexpr_baton *baton;
2df3850c 16147
98bfdba5
PA
16148 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16149 SYMBOL_PRINT_NAME (sym),
16150 &objfile->objfile_obstack, cu,
16151 &value, &bytes, &baton);
2df3850c 16152
98bfdba5
PA
16153 if (baton != NULL)
16154 {
16155 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16156 SYMBOL_LOCATION_BATON (sym) = baton;
16157 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16158 }
16159 else if (bytes != NULL)
16160 {
16161 SYMBOL_VALUE_BYTES (sym) = bytes;
16162 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16163 }
16164 else
16165 {
16166 SYMBOL_VALUE (sym) = value;
16167 SYMBOL_CLASS (sym) = LOC_CONST;
16168 }
2df3850c
JM
16169}
16170
c906108c
SS
16171/* Return the type of the die in question using its DW_AT_type attribute. */
16172
16173static struct type *
e7c27a73 16174die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16175{
c906108c 16176 struct attribute *type_attr;
c906108c 16177
e142c38c 16178 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16179 if (!type_attr)
16180 {
16181 /* A missing DW_AT_type represents a void type. */
46bf5051 16182 return objfile_type (cu->objfile)->builtin_void;
c906108c 16183 }
348e048f 16184
673bfd45 16185 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16186}
16187
b4ba55a1
JB
16188/* True iff CU's producer generates GNAT Ada auxiliary information
16189 that allows to find parallel types through that information instead
16190 of having to do expensive parallel lookups by type name. */
16191
16192static int
16193need_gnat_info (struct dwarf2_cu *cu)
16194{
16195 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16196 of GNAT produces this auxiliary information, without any indication
16197 that it is produced. Part of enhancing the FSF version of GNAT
16198 to produce that information will be to put in place an indicator
16199 that we can use in order to determine whether the descriptive type
16200 info is available or not. One suggestion that has been made is
16201 to use a new attribute, attached to the CU die. For now, assume
16202 that the descriptive type info is not available. */
16203 return 0;
16204}
16205
b4ba55a1
JB
16206/* Return the auxiliary type of the die in question using its
16207 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16208 attribute is not present. */
16209
16210static struct type *
16211die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16212{
b4ba55a1 16213 struct attribute *type_attr;
b4ba55a1
JB
16214
16215 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16216 if (!type_attr)
16217 return NULL;
16218
673bfd45 16219 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16220}
16221
16222/* If DIE has a descriptive_type attribute, then set the TYPE's
16223 descriptive type accordingly. */
16224
16225static void
16226set_descriptive_type (struct type *type, struct die_info *die,
16227 struct dwarf2_cu *cu)
16228{
16229 struct type *descriptive_type = die_descriptive_type (die, cu);
16230
16231 if (descriptive_type)
16232 {
16233 ALLOCATE_GNAT_AUX_TYPE (type);
16234 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16235 }
16236}
16237
c906108c
SS
16238/* Return the containing type of the die in question using its
16239 DW_AT_containing_type attribute. */
16240
16241static struct type *
e7c27a73 16242die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16243{
c906108c 16244 struct attribute *type_attr;
c906108c 16245
e142c38c 16246 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16247 if (!type_attr)
16248 error (_("Dwarf Error: Problem turning containing type into gdb type "
16249 "[in module %s]"), cu->objfile->name);
16250
673bfd45 16251 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16252}
16253
673bfd45
DE
16254/* Look up the type of DIE in CU using its type attribute ATTR.
16255 If there is no type substitute an error marker. */
16256
c906108c 16257static struct type *
673bfd45
DE
16258lookup_die_type (struct die_info *die, struct attribute *attr,
16259 struct dwarf2_cu *cu)
c906108c 16260{
bb5ed363 16261 struct objfile *objfile = cu->objfile;
f792889a
DJ
16262 struct type *this_type;
16263
673bfd45
DE
16264 /* First see if we have it cached. */
16265
36586728
TT
16266 if (attr->form == DW_FORM_GNU_ref_alt)
16267 {
16268 struct dwarf2_per_cu_data *per_cu;
16269 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16270
16271 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16272 this_type = get_die_type_at_offset (offset, per_cu);
16273 }
16274 else if (is_ref_attr (attr))
673bfd45 16275 {
b64f50a1 16276 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16277
16278 this_type = get_die_type_at_offset (offset, cu->per_cu);
16279 }
55f1336d 16280 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16281 {
16282 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16283
16284 /* sig_type will be NULL if the signatured type is missing from
16285 the debug info. */
16286 if (sig_type == NULL)
16287 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16288 "at 0x%x [in module %s]"),
b64f50a1 16289 die->offset.sect_off, objfile->name);
673bfd45 16290
3019eac3
DE
16291 gdb_assert (sig_type->per_cu.is_debug_types);
16292 /* If we haven't filled in type_offset_in_section yet, then we
16293 haven't read the type in yet. */
16294 this_type = NULL;
16295 if (sig_type->type_offset_in_section.sect_off != 0)
16296 {
16297 this_type =
16298 get_die_type_at_offset (sig_type->type_offset_in_section,
16299 &sig_type->per_cu);
16300 }
673bfd45
DE
16301 }
16302 else
16303 {
16304 dump_die_for_error (die);
16305 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16306 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16307 }
16308
16309 /* If not cached we need to read it in. */
16310
16311 if (this_type == NULL)
16312 {
16313 struct die_info *type_die;
16314 struct dwarf2_cu *type_cu = cu;
16315
16316 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16317 /* If we found the type now, it's probably because the type came
16318 from an inter-CU reference and the type's CU got expanded before
16319 ours. */
16320 this_type = get_die_type (type_die, type_cu);
16321 if (this_type == NULL)
16322 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16323 }
16324
16325 /* If we still don't have a type use an error marker. */
16326
16327 if (this_type == NULL)
c906108c 16328 {
b00fdb78
TT
16329 char *message, *saved;
16330
16331 /* read_type_die already issued a complaint. */
16332 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16333 objfile->name,
b64f50a1
JK
16334 cu->header.offset.sect_off,
16335 die->offset.sect_off);
bb5ed363 16336 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16337 message, strlen (message));
16338 xfree (message);
16339
bb5ed363 16340 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16341 }
673bfd45 16342
f792889a 16343 return this_type;
c906108c
SS
16344}
16345
673bfd45
DE
16346/* Return the type in DIE, CU.
16347 Returns NULL for invalid types.
16348
16349 This first does a lookup in the appropriate type_hash table,
16350 and only reads the die in if necessary.
16351
16352 NOTE: This can be called when reading in partial or full symbols. */
16353
f792889a 16354static struct type *
e7c27a73 16355read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16356{
f792889a
DJ
16357 struct type *this_type;
16358
16359 this_type = get_die_type (die, cu);
16360 if (this_type)
16361 return this_type;
16362
673bfd45
DE
16363 return read_type_die_1 (die, cu);
16364}
16365
16366/* Read the type in DIE, CU.
16367 Returns NULL for invalid types. */
16368
16369static struct type *
16370read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16371{
16372 struct type *this_type = NULL;
16373
c906108c
SS
16374 switch (die->tag)
16375 {
16376 case DW_TAG_class_type:
680b30c7 16377 case DW_TAG_interface_type:
c906108c
SS
16378 case DW_TAG_structure_type:
16379 case DW_TAG_union_type:
f792889a 16380 this_type = read_structure_type (die, cu);
c906108c
SS
16381 break;
16382 case DW_TAG_enumeration_type:
f792889a 16383 this_type = read_enumeration_type (die, cu);
c906108c
SS
16384 break;
16385 case DW_TAG_subprogram:
16386 case DW_TAG_subroutine_type:
edb3359d 16387 case DW_TAG_inlined_subroutine:
f792889a 16388 this_type = read_subroutine_type (die, cu);
c906108c
SS
16389 break;
16390 case DW_TAG_array_type:
f792889a 16391 this_type = read_array_type (die, cu);
c906108c 16392 break;
72019c9c 16393 case DW_TAG_set_type:
f792889a 16394 this_type = read_set_type (die, cu);
72019c9c 16395 break;
c906108c 16396 case DW_TAG_pointer_type:
f792889a 16397 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16398 break;
16399 case DW_TAG_ptr_to_member_type:
f792889a 16400 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16401 break;
16402 case DW_TAG_reference_type:
f792889a 16403 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16404 break;
16405 case DW_TAG_const_type:
f792889a 16406 this_type = read_tag_const_type (die, cu);
c906108c
SS
16407 break;
16408 case DW_TAG_volatile_type:
f792889a 16409 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
16410 break;
16411 case DW_TAG_string_type:
f792889a 16412 this_type = read_tag_string_type (die, cu);
c906108c
SS
16413 break;
16414 case DW_TAG_typedef:
f792889a 16415 this_type = read_typedef (die, cu);
c906108c 16416 break;
a02abb62 16417 case DW_TAG_subrange_type:
f792889a 16418 this_type = read_subrange_type (die, cu);
a02abb62 16419 break;
c906108c 16420 case DW_TAG_base_type:
f792889a 16421 this_type = read_base_type (die, cu);
c906108c 16422 break;
81a17f79 16423 case DW_TAG_unspecified_type:
f792889a 16424 this_type = read_unspecified_type (die, cu);
81a17f79 16425 break;
0114d602
DJ
16426 case DW_TAG_namespace:
16427 this_type = read_namespace_type (die, cu);
16428 break;
f55ee35c
JK
16429 case DW_TAG_module:
16430 this_type = read_module_type (die, cu);
16431 break;
c906108c 16432 default:
3e43a32a
MS
16433 complaint (&symfile_complaints,
16434 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16435 dwarf_tag_name (die->tag));
c906108c
SS
16436 break;
16437 }
63d06c5c 16438
f792889a 16439 return this_type;
63d06c5c
DC
16440}
16441
abc72ce4
DE
16442/* See if we can figure out if the class lives in a namespace. We do
16443 this by looking for a member function; its demangled name will
16444 contain namespace info, if there is any.
16445 Return the computed name or NULL.
16446 Space for the result is allocated on the objfile's obstack.
16447 This is the full-die version of guess_partial_die_structure_name.
16448 In this case we know DIE has no useful parent. */
16449
16450static char *
16451guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16452{
16453 struct die_info *spec_die;
16454 struct dwarf2_cu *spec_cu;
16455 struct die_info *child;
16456
16457 spec_cu = cu;
16458 spec_die = die_specification (die, &spec_cu);
16459 if (spec_die != NULL)
16460 {
16461 die = spec_die;
16462 cu = spec_cu;
16463 }
16464
16465 for (child = die->child;
16466 child != NULL;
16467 child = child->sibling)
16468 {
16469 if (child->tag == DW_TAG_subprogram)
16470 {
16471 struct attribute *attr;
16472
16473 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16474 if (attr == NULL)
16475 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16476 if (attr != NULL)
16477 {
16478 char *actual_name
16479 = language_class_name_from_physname (cu->language_defn,
16480 DW_STRING (attr));
16481 char *name = NULL;
16482
16483 if (actual_name != NULL)
16484 {
16485 char *die_name = dwarf2_name (die, cu);
16486
16487 if (die_name != NULL
16488 && strcmp (die_name, actual_name) != 0)
16489 {
16490 /* Strip off the class name from the full name.
16491 We want the prefix. */
16492 int die_name_len = strlen (die_name);
16493 int actual_name_len = strlen (actual_name);
16494
16495 /* Test for '::' as a sanity check. */
16496 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16497 && actual_name[actual_name_len
16498 - die_name_len - 1] == ':')
abc72ce4
DE
16499 name =
16500 obsavestring (actual_name,
16501 actual_name_len - die_name_len - 2,
16502 &cu->objfile->objfile_obstack);
16503 }
16504 }
16505 xfree (actual_name);
16506 return name;
16507 }
16508 }
16509 }
16510
16511 return NULL;
16512}
16513
96408a79
SA
16514/* GCC might emit a nameless typedef that has a linkage name. Determine the
16515 prefix part in such case. See
16516 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16517
16518static char *
16519anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16520{
16521 struct attribute *attr;
16522 char *base;
16523
16524 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16525 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16526 return NULL;
16527
16528 attr = dwarf2_attr (die, DW_AT_name, cu);
16529 if (attr != NULL && DW_STRING (attr) != NULL)
16530 return NULL;
16531
16532 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16533 if (attr == NULL)
16534 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16535 if (attr == NULL || DW_STRING (attr) == NULL)
16536 return NULL;
16537
16538 /* dwarf2_name had to be already called. */
16539 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16540
16541 /* Strip the base name, keep any leading namespaces/classes. */
16542 base = strrchr (DW_STRING (attr), ':');
16543 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16544 return "";
16545
16546 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16547 &cu->objfile->objfile_obstack);
16548}
16549
fdde2d81 16550/* Return the name of the namespace/class that DIE is defined within,
0114d602 16551 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16552
0114d602
DJ
16553 For example, if we're within the method foo() in the following
16554 code:
16555
16556 namespace N {
16557 class C {
16558 void foo () {
16559 }
16560 };
16561 }
16562
16563 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16564
0d5cff50 16565static const char *
e142c38c 16566determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16567{
0114d602
DJ
16568 struct die_info *parent, *spec_die;
16569 struct dwarf2_cu *spec_cu;
16570 struct type *parent_type;
96408a79 16571 char *retval;
63d06c5c 16572
f55ee35c
JK
16573 if (cu->language != language_cplus && cu->language != language_java
16574 && cu->language != language_fortran)
0114d602
DJ
16575 return "";
16576
96408a79
SA
16577 retval = anonymous_struct_prefix (die, cu);
16578 if (retval)
16579 return retval;
16580
0114d602
DJ
16581 /* We have to be careful in the presence of DW_AT_specification.
16582 For example, with GCC 3.4, given the code
16583
16584 namespace N {
16585 void foo() {
16586 // Definition of N::foo.
16587 }
16588 }
16589
16590 then we'll have a tree of DIEs like this:
16591
16592 1: DW_TAG_compile_unit
16593 2: DW_TAG_namespace // N
16594 3: DW_TAG_subprogram // declaration of N::foo
16595 4: DW_TAG_subprogram // definition of N::foo
16596 DW_AT_specification // refers to die #3
16597
16598 Thus, when processing die #4, we have to pretend that we're in
16599 the context of its DW_AT_specification, namely the contex of die
16600 #3. */
16601 spec_cu = cu;
16602 spec_die = die_specification (die, &spec_cu);
16603 if (spec_die == NULL)
16604 parent = die->parent;
16605 else
63d06c5c 16606 {
0114d602
DJ
16607 parent = spec_die->parent;
16608 cu = spec_cu;
63d06c5c 16609 }
0114d602
DJ
16610
16611 if (parent == NULL)
16612 return "";
98bfdba5
PA
16613 else if (parent->building_fullname)
16614 {
16615 const char *name;
16616 const char *parent_name;
16617
16618 /* It has been seen on RealView 2.2 built binaries,
16619 DW_TAG_template_type_param types actually _defined_ as
16620 children of the parent class:
16621
16622 enum E {};
16623 template class <class Enum> Class{};
16624 Class<enum E> class_e;
16625
16626 1: DW_TAG_class_type (Class)
16627 2: DW_TAG_enumeration_type (E)
16628 3: DW_TAG_enumerator (enum1:0)
16629 3: DW_TAG_enumerator (enum2:1)
16630 ...
16631 2: DW_TAG_template_type_param
16632 DW_AT_type DW_FORM_ref_udata (E)
16633
16634 Besides being broken debug info, it can put GDB into an
16635 infinite loop. Consider:
16636
16637 When we're building the full name for Class<E>, we'll start
16638 at Class, and go look over its template type parameters,
16639 finding E. We'll then try to build the full name of E, and
16640 reach here. We're now trying to build the full name of E,
16641 and look over the parent DIE for containing scope. In the
16642 broken case, if we followed the parent DIE of E, we'd again
16643 find Class, and once again go look at its template type
16644 arguments, etc., etc. Simply don't consider such parent die
16645 as source-level parent of this die (it can't be, the language
16646 doesn't allow it), and break the loop here. */
16647 name = dwarf2_name (die, cu);
16648 parent_name = dwarf2_name (parent, cu);
16649 complaint (&symfile_complaints,
16650 _("template param type '%s' defined within parent '%s'"),
16651 name ? name : "<unknown>",
16652 parent_name ? parent_name : "<unknown>");
16653 return "";
16654 }
63d06c5c 16655 else
0114d602
DJ
16656 switch (parent->tag)
16657 {
63d06c5c 16658 case DW_TAG_namespace:
0114d602 16659 parent_type = read_type_die (parent, cu);
acebe513
UW
16660 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16661 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16662 Work around this problem here. */
16663 if (cu->language == language_cplus
16664 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16665 return "";
0114d602
DJ
16666 /* We give a name to even anonymous namespaces. */
16667 return TYPE_TAG_NAME (parent_type);
63d06c5c 16668 case DW_TAG_class_type:
680b30c7 16669 case DW_TAG_interface_type:
63d06c5c 16670 case DW_TAG_structure_type:
0114d602 16671 case DW_TAG_union_type:
f55ee35c 16672 case DW_TAG_module:
0114d602
DJ
16673 parent_type = read_type_die (parent, cu);
16674 if (TYPE_TAG_NAME (parent_type) != NULL)
16675 return TYPE_TAG_NAME (parent_type);
16676 else
16677 /* An anonymous structure is only allowed non-static data
16678 members; no typedefs, no member functions, et cetera.
16679 So it does not need a prefix. */
16680 return "";
abc72ce4 16681 case DW_TAG_compile_unit:
95554aad 16682 case DW_TAG_partial_unit:
abc72ce4
DE
16683 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16684 if (cu->language == language_cplus
8b70b953 16685 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16686 && die->child != NULL
16687 && (die->tag == DW_TAG_class_type
16688 || die->tag == DW_TAG_structure_type
16689 || die->tag == DW_TAG_union_type))
16690 {
16691 char *name = guess_full_die_structure_name (die, cu);
16692 if (name != NULL)
16693 return name;
16694 }
16695 return "";
63d06c5c 16696 default:
8176b9b8 16697 return determine_prefix (parent, cu);
63d06c5c 16698 }
63d06c5c
DC
16699}
16700
3e43a32a
MS
16701/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16702 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16703 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16704 an obconcat, otherwise allocate storage for the result. The CU argument is
16705 used to determine the language and hence, the appropriate separator. */
987504bb 16706
f55ee35c 16707#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16708
16709static char *
f55ee35c
JK
16710typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16711 int physname, struct dwarf2_cu *cu)
63d06c5c 16712{
f55ee35c 16713 const char *lead = "";
5c315b68 16714 const char *sep;
63d06c5c 16715
3e43a32a
MS
16716 if (suffix == NULL || suffix[0] == '\0'
16717 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16718 sep = "";
16719 else if (cu->language == language_java)
16720 sep = ".";
f55ee35c
JK
16721 else if (cu->language == language_fortran && physname)
16722 {
16723 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16724 DW_AT_MIPS_linkage_name is preferred and used instead. */
16725
16726 lead = "__";
16727 sep = "_MOD_";
16728 }
987504bb
JJ
16729 else
16730 sep = "::";
63d06c5c 16731
6dd47d34
DE
16732 if (prefix == NULL)
16733 prefix = "";
16734 if (suffix == NULL)
16735 suffix = "";
16736
987504bb
JJ
16737 if (obs == NULL)
16738 {
3e43a32a
MS
16739 char *retval
16740 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16741
f55ee35c
JK
16742 strcpy (retval, lead);
16743 strcat (retval, prefix);
6dd47d34
DE
16744 strcat (retval, sep);
16745 strcat (retval, suffix);
63d06c5c
DC
16746 return retval;
16747 }
987504bb
JJ
16748 else
16749 {
16750 /* We have an obstack. */
f55ee35c 16751 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16752 }
63d06c5c
DC
16753}
16754
c906108c
SS
16755/* Return sibling of die, NULL if no sibling. */
16756
f9aca02d 16757static struct die_info *
fba45db2 16758sibling_die (struct die_info *die)
c906108c 16759{
639d11d3 16760 return die->sibling;
c906108c
SS
16761}
16762
71c25dea
TT
16763/* Get name of a die, return NULL if not found. */
16764
16765static char *
16766dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16767 struct obstack *obstack)
16768{
16769 if (name && cu->language == language_cplus)
16770 {
16771 char *canon_name = cp_canonicalize_string (name);
16772
16773 if (canon_name != NULL)
16774 {
16775 if (strcmp (canon_name, name) != 0)
16776 name = obsavestring (canon_name, strlen (canon_name),
16777 obstack);
16778 xfree (canon_name);
16779 }
16780 }
16781
16782 return name;
c906108c
SS
16783}
16784
9219021c
DC
16785/* Get name of a die, return NULL if not found. */
16786
16787static char *
e142c38c 16788dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16789{
16790 struct attribute *attr;
16791
e142c38c 16792 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16793 if ((!attr || !DW_STRING (attr))
16794 && die->tag != DW_TAG_class_type
16795 && die->tag != DW_TAG_interface_type
16796 && die->tag != DW_TAG_structure_type
16797 && die->tag != DW_TAG_union_type)
71c25dea
TT
16798 return NULL;
16799
16800 switch (die->tag)
16801 {
16802 case DW_TAG_compile_unit:
95554aad 16803 case DW_TAG_partial_unit:
71c25dea
TT
16804 /* Compilation units have a DW_AT_name that is a filename, not
16805 a source language identifier. */
16806 case DW_TAG_enumeration_type:
16807 case DW_TAG_enumerator:
16808 /* These tags always have simple identifiers already; no need
16809 to canonicalize them. */
16810 return DW_STRING (attr);
907af001 16811
418835cc
KS
16812 case DW_TAG_subprogram:
16813 /* Java constructors will all be named "<init>", so return
16814 the class name when we see this special case. */
16815 if (cu->language == language_java
16816 && DW_STRING (attr) != NULL
16817 && strcmp (DW_STRING (attr), "<init>") == 0)
16818 {
16819 struct dwarf2_cu *spec_cu = cu;
16820 struct die_info *spec_die;
16821
16822 /* GCJ will output '<init>' for Java constructor names.
16823 For this special case, return the name of the parent class. */
16824
16825 /* GCJ may output suprogram DIEs with AT_specification set.
16826 If so, use the name of the specified DIE. */
16827 spec_die = die_specification (die, &spec_cu);
16828 if (spec_die != NULL)
16829 return dwarf2_name (spec_die, spec_cu);
16830
16831 do
16832 {
16833 die = die->parent;
16834 if (die->tag == DW_TAG_class_type)
16835 return dwarf2_name (die, cu);
16836 }
95554aad
TT
16837 while (die->tag != DW_TAG_compile_unit
16838 && die->tag != DW_TAG_partial_unit);
418835cc 16839 }
907af001
UW
16840 break;
16841
16842 case DW_TAG_class_type:
16843 case DW_TAG_interface_type:
16844 case DW_TAG_structure_type:
16845 case DW_TAG_union_type:
16846 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16847 structures or unions. These were of the form "._%d" in GCC 4.1,
16848 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16849 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16850 if (attr && DW_STRING (attr)
16851 && (strncmp (DW_STRING (attr), "._", 2) == 0
16852 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16853 return NULL;
53832f31
TT
16854
16855 /* GCC might emit a nameless typedef that has a linkage name. See
16856 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16857 if (!attr || DW_STRING (attr) == NULL)
16858 {
df5c6c50 16859 char *demangled = NULL;
53832f31
TT
16860
16861 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16862 if (attr == NULL)
16863 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16864
16865 if (attr == NULL || DW_STRING (attr) == NULL)
16866 return NULL;
16867
df5c6c50
JK
16868 /* Avoid demangling DW_STRING (attr) the second time on a second
16869 call for the same DIE. */
16870 if (!DW_STRING_IS_CANONICAL (attr))
16871 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16872
16873 if (demangled)
16874 {
96408a79
SA
16875 char *base;
16876
53832f31 16877 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16878 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16879 &cu->objfile->objfile_obstack);
53832f31
TT
16880 DW_STRING_IS_CANONICAL (attr) = 1;
16881 xfree (demangled);
96408a79
SA
16882
16883 /* Strip any leading namespaces/classes, keep only the base name.
16884 DW_AT_name for named DIEs does not contain the prefixes. */
16885 base = strrchr (DW_STRING (attr), ':');
16886 if (base && base > DW_STRING (attr) && base[-1] == ':')
16887 return &base[1];
16888 else
16889 return DW_STRING (attr);
53832f31
TT
16890 }
16891 }
907af001
UW
16892 break;
16893
71c25dea 16894 default:
907af001
UW
16895 break;
16896 }
16897
16898 if (!DW_STRING_IS_CANONICAL (attr))
16899 {
16900 DW_STRING (attr)
16901 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16902 &cu->objfile->objfile_obstack);
16903 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16904 }
907af001 16905 return DW_STRING (attr);
9219021c
DC
16906}
16907
16908/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16909 is none. *EXT_CU is the CU containing DIE on input, and the CU
16910 containing the return value on output. */
9219021c
DC
16911
16912static struct die_info *
f2f0e013 16913dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16914{
16915 struct attribute *attr;
9219021c 16916
f2f0e013 16917 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16918 if (attr == NULL)
16919 return NULL;
16920
f2f0e013 16921 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16922}
16923
c906108c
SS
16924/* Convert a DIE tag into its string name. */
16925
f39c6ffd 16926static const char *
aa1ee363 16927dwarf_tag_name (unsigned tag)
c906108c 16928{
f39c6ffd
TT
16929 const char *name = get_DW_TAG_name (tag);
16930
16931 if (name == NULL)
16932 return "DW_TAG_<unknown>";
16933
16934 return name;
c906108c
SS
16935}
16936
16937/* Convert a DWARF attribute code into its string name. */
16938
f39c6ffd 16939static const char *
aa1ee363 16940dwarf_attr_name (unsigned attr)
c906108c 16941{
f39c6ffd
TT
16942 const char *name;
16943
c764a876 16944#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16945 if (attr == DW_AT_MIPS_fde)
16946 return "DW_AT_MIPS_fde";
16947#else
16948 if (attr == DW_AT_HP_block_index)
16949 return "DW_AT_HP_block_index";
c764a876 16950#endif
f39c6ffd
TT
16951
16952 name = get_DW_AT_name (attr);
16953
16954 if (name == NULL)
16955 return "DW_AT_<unknown>";
16956
16957 return name;
c906108c
SS
16958}
16959
16960/* Convert a DWARF value form code into its string name. */
16961
f39c6ffd 16962static const char *
aa1ee363 16963dwarf_form_name (unsigned form)
c906108c 16964{
f39c6ffd
TT
16965 const char *name = get_DW_FORM_name (form);
16966
16967 if (name == NULL)
16968 return "DW_FORM_<unknown>";
16969
16970 return name;
c906108c
SS
16971}
16972
16973static char *
fba45db2 16974dwarf_bool_name (unsigned mybool)
c906108c
SS
16975{
16976 if (mybool)
16977 return "TRUE";
16978 else
16979 return "FALSE";
16980}
16981
16982/* Convert a DWARF type code into its string name. */
16983
f39c6ffd 16984static const char *
aa1ee363 16985dwarf_type_encoding_name (unsigned enc)
c906108c 16986{
f39c6ffd 16987 const char *name = get_DW_ATE_name (enc);
c906108c 16988
f39c6ffd
TT
16989 if (name == NULL)
16990 return "DW_ATE_<unknown>";
c906108c 16991
f39c6ffd 16992 return name;
c906108c 16993}
c906108c 16994
f9aca02d 16995static void
d97bc12b 16996dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
16997{
16998 unsigned int i;
16999
d97bc12b
DE
17000 print_spaces (indent, f);
17001 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17002 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17003
17004 if (die->parent != NULL)
17005 {
17006 print_spaces (indent, f);
17007 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17008 die->parent->offset.sect_off);
d97bc12b
DE
17009 }
17010
17011 print_spaces (indent, f);
17012 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17013 dwarf_bool_name (die->child != NULL));
c906108c 17014
d97bc12b
DE
17015 print_spaces (indent, f);
17016 fprintf_unfiltered (f, " attributes:\n");
17017
c906108c
SS
17018 for (i = 0; i < die->num_attrs; ++i)
17019 {
d97bc12b
DE
17020 print_spaces (indent, f);
17021 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17022 dwarf_attr_name (die->attrs[i].name),
17023 dwarf_form_name (die->attrs[i].form));
d97bc12b 17024
c906108c
SS
17025 switch (die->attrs[i].form)
17026 {
c906108c 17027 case DW_FORM_addr:
3019eac3 17028 case DW_FORM_GNU_addr_index:
d97bc12b 17029 fprintf_unfiltered (f, "address: ");
5af949e3 17030 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17031 break;
17032 case DW_FORM_block2:
17033 case DW_FORM_block4:
17034 case DW_FORM_block:
17035 case DW_FORM_block1:
56eb65bd
SP
17036 fprintf_unfiltered (f, "block: size %s",
17037 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17038 break;
2dc7f7b3 17039 case DW_FORM_exprloc:
56eb65bd
SP
17040 fprintf_unfiltered (f, "expression: size %s",
17041 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17042 break;
4568ecf9
DE
17043 case DW_FORM_ref_addr:
17044 fprintf_unfiltered (f, "ref address: ");
17045 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17046 break;
36586728
TT
17047 case DW_FORM_GNU_ref_alt:
17048 fprintf_unfiltered (f, "alt ref address: ");
17049 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17050 break;
10b3939b
DJ
17051 case DW_FORM_ref1:
17052 case DW_FORM_ref2:
17053 case DW_FORM_ref4:
4568ecf9
DE
17054 case DW_FORM_ref8:
17055 case DW_FORM_ref_udata:
d97bc12b 17056 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17057 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17058 break;
c906108c
SS
17059 case DW_FORM_data1:
17060 case DW_FORM_data2:
17061 case DW_FORM_data4:
ce5d95e1 17062 case DW_FORM_data8:
c906108c
SS
17063 case DW_FORM_udata:
17064 case DW_FORM_sdata:
43bbcdc2
PH
17065 fprintf_unfiltered (f, "constant: %s",
17066 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17067 break;
2dc7f7b3
TT
17068 case DW_FORM_sec_offset:
17069 fprintf_unfiltered (f, "section offset: %s",
17070 pulongest (DW_UNSND (&die->attrs[i])));
17071 break;
55f1336d 17072 case DW_FORM_ref_sig8:
348e048f
DE
17073 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17074 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17075 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17076 else
17077 fprintf_unfiltered (f, "signatured type, offset: unknown");
17078 break;
c906108c 17079 case DW_FORM_string:
4bdf3d34 17080 case DW_FORM_strp:
3019eac3 17081 case DW_FORM_GNU_str_index:
36586728 17082 case DW_FORM_GNU_strp_alt:
8285870a 17083 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17084 DW_STRING (&die->attrs[i])
8285870a
JK
17085 ? DW_STRING (&die->attrs[i]) : "",
17086 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17087 break;
17088 case DW_FORM_flag:
17089 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17090 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17091 else
d97bc12b 17092 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17093 break;
2dc7f7b3
TT
17094 case DW_FORM_flag_present:
17095 fprintf_unfiltered (f, "flag: TRUE");
17096 break;
a8329558 17097 case DW_FORM_indirect:
0963b4bd
MS
17098 /* The reader will have reduced the indirect form to
17099 the "base form" so this form should not occur. */
3e43a32a
MS
17100 fprintf_unfiltered (f,
17101 "unexpected attribute form: DW_FORM_indirect");
a8329558 17102 break;
c906108c 17103 default:
d97bc12b 17104 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17105 die->attrs[i].form);
d97bc12b 17106 break;
c906108c 17107 }
d97bc12b 17108 fprintf_unfiltered (f, "\n");
c906108c
SS
17109 }
17110}
17111
f9aca02d 17112static void
d97bc12b 17113dump_die_for_error (struct die_info *die)
c906108c 17114{
d97bc12b
DE
17115 dump_die_shallow (gdb_stderr, 0, die);
17116}
17117
17118static void
17119dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17120{
17121 int indent = level * 4;
17122
17123 gdb_assert (die != NULL);
17124
17125 if (level >= max_level)
17126 return;
17127
17128 dump_die_shallow (f, indent, die);
17129
17130 if (die->child != NULL)
c906108c 17131 {
d97bc12b
DE
17132 print_spaces (indent, f);
17133 fprintf_unfiltered (f, " Children:");
17134 if (level + 1 < max_level)
17135 {
17136 fprintf_unfiltered (f, "\n");
17137 dump_die_1 (f, level + 1, max_level, die->child);
17138 }
17139 else
17140 {
3e43a32a
MS
17141 fprintf_unfiltered (f,
17142 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17143 }
17144 }
17145
17146 if (die->sibling != NULL && level > 0)
17147 {
17148 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17149 }
17150}
17151
d97bc12b
DE
17152/* This is called from the pdie macro in gdbinit.in.
17153 It's not static so gcc will keep a copy callable from gdb. */
17154
17155void
17156dump_die (struct die_info *die, int max_level)
17157{
17158 dump_die_1 (gdb_stdlog, 0, max_level, die);
17159}
17160
f9aca02d 17161static void
51545339 17162store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17163{
51545339 17164 void **slot;
c906108c 17165
b64f50a1
JK
17166 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17167 INSERT);
51545339
DJ
17168
17169 *slot = die;
c906108c
SS
17170}
17171
b64f50a1
JK
17172/* DW_ADDR is always stored already as sect_offset; despite for the forms
17173 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17174
93311388
DE
17175static int
17176is_ref_attr (struct attribute *attr)
c906108c 17177{
c906108c
SS
17178 switch (attr->form)
17179 {
17180 case DW_FORM_ref_addr:
c906108c
SS
17181 case DW_FORM_ref1:
17182 case DW_FORM_ref2:
17183 case DW_FORM_ref4:
613e1657 17184 case DW_FORM_ref8:
c906108c 17185 case DW_FORM_ref_udata:
36586728 17186 case DW_FORM_GNU_ref_alt:
93311388 17187 return 1;
c906108c 17188 default:
93311388 17189 return 0;
c906108c 17190 }
93311388
DE
17191}
17192
b64f50a1
JK
17193/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17194 required kind. */
17195
17196static sect_offset
93311388
DE
17197dwarf2_get_ref_die_offset (struct attribute *attr)
17198{
4568ecf9 17199 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17200
93311388 17201 if (is_ref_attr (attr))
b64f50a1 17202 return retval;
93311388 17203
b64f50a1 17204 retval.sect_off = 0;
93311388
DE
17205 complaint (&symfile_complaints,
17206 _("unsupported die ref attribute form: '%s'"),
17207 dwarf_form_name (attr->form));
b64f50a1 17208 return retval;
c906108c
SS
17209}
17210
43bbcdc2
PH
17211/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17212 * the value held by the attribute is not constant. */
a02abb62 17213
43bbcdc2 17214static LONGEST
a02abb62
JB
17215dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17216{
17217 if (attr->form == DW_FORM_sdata)
17218 return DW_SND (attr);
17219 else if (attr->form == DW_FORM_udata
17220 || attr->form == DW_FORM_data1
17221 || attr->form == DW_FORM_data2
17222 || attr->form == DW_FORM_data4
17223 || attr->form == DW_FORM_data8)
17224 return DW_UNSND (attr);
17225 else
17226 {
3e43a32a
MS
17227 complaint (&symfile_complaints,
17228 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17229 dwarf_form_name (attr->form));
17230 return default_value;
17231 }
17232}
17233
348e048f
DE
17234/* Follow reference or signature attribute ATTR of SRC_DIE.
17235 On entry *REF_CU is the CU of SRC_DIE.
17236 On exit *REF_CU is the CU of the result. */
17237
17238static struct die_info *
17239follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17240 struct dwarf2_cu **ref_cu)
17241{
17242 struct die_info *die;
17243
17244 if (is_ref_attr (attr))
17245 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17246 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17247 die = follow_die_sig (src_die, attr, ref_cu);
17248 else
17249 {
17250 dump_die_for_error (src_die);
17251 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17252 (*ref_cu)->objfile->name);
17253 }
17254
17255 return die;
03dd20cc
DJ
17256}
17257
5c631832 17258/* Follow reference OFFSET.
673bfd45
DE
17259 On entry *REF_CU is the CU of the source die referencing OFFSET.
17260 On exit *REF_CU is the CU of the result.
17261 Returns NULL if OFFSET is invalid. */
f504f079 17262
f9aca02d 17263static struct die_info *
36586728
TT
17264follow_die_offset (sect_offset offset, int offset_in_dwz,
17265 struct dwarf2_cu **ref_cu)
c906108c 17266{
10b3939b 17267 struct die_info temp_die;
f2f0e013 17268 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17269
348e048f
DE
17270 gdb_assert (cu->per_cu != NULL);
17271
98bfdba5
PA
17272 target_cu = cu;
17273
3019eac3 17274 if (cu->per_cu->is_debug_types)
348e048f
DE
17275 {
17276 /* .debug_types CUs cannot reference anything outside their CU.
17277 If they need to, they have to reference a signatured type via
55f1336d 17278 DW_FORM_ref_sig8. */
348e048f 17279 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17280 return NULL;
348e048f 17281 }
36586728
TT
17282 else if (offset_in_dwz != cu->per_cu->is_dwz
17283 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17284 {
17285 struct dwarf2_per_cu_data *per_cu;
9a619af0 17286
36586728
TT
17287 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17288 cu->objfile);
03dd20cc
DJ
17289
17290 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17291 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17292 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17293
10b3939b
DJ
17294 target_cu = per_cu->cu;
17295 }
98bfdba5
PA
17296 else if (cu->dies == NULL)
17297 {
17298 /* We're loading full DIEs during partial symbol reading. */
17299 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17300 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17301 }
c906108c 17302
f2f0e013 17303 *ref_cu = target_cu;
51545339 17304 temp_die.offset = offset;
b64f50a1 17305 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17306}
10b3939b 17307
5c631832
JK
17308/* Follow reference attribute ATTR of SRC_DIE.
17309 On entry *REF_CU is the CU of SRC_DIE.
17310 On exit *REF_CU is the CU of the result. */
17311
17312static struct die_info *
17313follow_die_ref (struct die_info *src_die, struct attribute *attr,
17314 struct dwarf2_cu **ref_cu)
17315{
b64f50a1 17316 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17317 struct dwarf2_cu *cu = *ref_cu;
17318 struct die_info *die;
17319
36586728
TT
17320 die = follow_die_offset (offset,
17321 (attr->form == DW_FORM_GNU_ref_alt
17322 || cu->per_cu->is_dwz),
17323 ref_cu);
5c631832
JK
17324 if (!die)
17325 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17326 "at 0x%x [in module %s]"),
b64f50a1 17327 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17328
5c631832
JK
17329 return die;
17330}
17331
d83e736b
JK
17332/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17333 Returned value is intended for DW_OP_call*. Returned
17334 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17335
17336struct dwarf2_locexpr_baton
8b9737bf
TT
17337dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17338 struct dwarf2_per_cu_data *per_cu,
17339 CORE_ADDR (*get_frame_pc) (void *baton),
17340 void *baton)
5c631832 17341{
918dd910 17342 struct dwarf2_cu *cu;
5c631832
JK
17343 struct die_info *die;
17344 struct attribute *attr;
17345 struct dwarf2_locexpr_baton retval;
17346
8cf6f0b1
TT
17347 dw2_setup (per_cu->objfile);
17348
918dd910
JK
17349 if (per_cu->cu == NULL)
17350 load_cu (per_cu);
17351 cu = per_cu->cu;
17352
36586728 17353 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17354 if (!die)
17355 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17356 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17357
17358 attr = dwarf2_attr (die, DW_AT_location, cu);
17359 if (!attr)
17360 {
e103e986
JK
17361 /* DWARF: "If there is no such attribute, then there is no effect.".
17362 DATA is ignored if SIZE is 0. */
5c631832 17363
e103e986 17364 retval.data = NULL;
5c631832
JK
17365 retval.size = 0;
17366 }
8cf6f0b1
TT
17367 else if (attr_form_is_section_offset (attr))
17368 {
17369 struct dwarf2_loclist_baton loclist_baton;
17370 CORE_ADDR pc = (*get_frame_pc) (baton);
17371 size_t size;
17372
17373 fill_in_loclist_baton (cu, &loclist_baton, attr);
17374
17375 retval.data = dwarf2_find_location_expression (&loclist_baton,
17376 &size, pc);
17377 retval.size = size;
17378 }
5c631832
JK
17379 else
17380 {
17381 if (!attr_form_is_block (attr))
17382 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17383 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17384 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17385
17386 retval.data = DW_BLOCK (attr)->data;
17387 retval.size = DW_BLOCK (attr)->size;
17388 }
17389 retval.per_cu = cu->per_cu;
918dd910 17390
918dd910
JK
17391 age_cached_comp_units ();
17392
5c631832 17393 return retval;
348e048f
DE
17394}
17395
8b9737bf
TT
17396/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17397 offset. */
17398
17399struct dwarf2_locexpr_baton
17400dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17401 struct dwarf2_per_cu_data *per_cu,
17402 CORE_ADDR (*get_frame_pc) (void *baton),
17403 void *baton)
17404{
17405 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17406
17407 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17408}
17409
8a9b8146
TT
17410/* Return the type of the DIE at DIE_OFFSET in the CU named by
17411 PER_CU. */
17412
17413struct type *
b64f50a1 17414dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17415 struct dwarf2_per_cu_data *per_cu)
17416{
b64f50a1
JK
17417 sect_offset die_offset_sect;
17418
8a9b8146 17419 dw2_setup (per_cu->objfile);
b64f50a1
JK
17420
17421 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17422 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17423}
17424
348e048f
DE
17425/* Follow the signature attribute ATTR in SRC_DIE.
17426 On entry *REF_CU is the CU of SRC_DIE.
17427 On exit *REF_CU is the CU of the result. */
17428
17429static struct die_info *
17430follow_die_sig (struct die_info *src_die, struct attribute *attr,
17431 struct dwarf2_cu **ref_cu)
17432{
17433 struct objfile *objfile = (*ref_cu)->objfile;
17434 struct die_info temp_die;
17435 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17436 struct dwarf2_cu *sig_cu;
17437 struct die_info *die;
17438
17439 /* sig_type will be NULL if the signatured type is missing from
17440 the debug info. */
17441 if (sig_type == NULL)
17442 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17443 "at 0x%x [in module %s]"),
b64f50a1 17444 src_die->offset.sect_off, objfile->name);
348e048f
DE
17445
17446 /* If necessary, add it to the queue and load its DIEs. */
17447
95554aad 17448 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17449 read_signatured_type (sig_type);
348e048f
DE
17450
17451 gdb_assert (sig_type->per_cu.cu != NULL);
17452
17453 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17454 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17455 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17456 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17457 temp_die.offset.sect_off);
348e048f
DE
17458 if (die)
17459 {
17460 *ref_cu = sig_cu;
17461 return die;
17462 }
17463
3e43a32a
MS
17464 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17465 "from DIE at 0x%x [in module %s]"),
b64f50a1 17466 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17467}
17468
17469/* Given an offset of a signatured type, return its signatured_type. */
17470
17471static struct signatured_type *
8b70b953
TT
17472lookup_signatured_type_at_offset (struct objfile *objfile,
17473 struct dwarf2_section_info *section,
b64f50a1 17474 sect_offset offset)
348e048f 17475{
b64f50a1 17476 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17477 unsigned int length, initial_length_size;
17478 unsigned int sig_offset;
52dc124a 17479 struct signatured_type find_entry, *sig_type;
348e048f
DE
17480
17481 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17482 sig_offset = (initial_length_size
17483 + 2 /*version*/
17484 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17485 + 1 /*address_size*/);
17486 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17487 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17488
17489 /* This is only used to lookup previously recorded types.
17490 If we didn't find it, it's our bug. */
52dc124a
DE
17491 gdb_assert (sig_type != NULL);
17492 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17493
52dc124a 17494 return sig_type;
348e048f
DE
17495}
17496
e5fe5e75 17497/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17498
17499static void
e5fe5e75 17500load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17501{
52dc124a 17502 struct signatured_type *sig_type;
348e048f 17503
f4dc4d17
DE
17504 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17505 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17506
6721b2ec
DE
17507 /* We have the per_cu, but we need the signatured_type.
17508 Fortunately this is an easy translation. */
17509 gdb_assert (per_cu->is_debug_types);
17510 sig_type = (struct signatured_type *) per_cu;
348e048f 17511
6721b2ec 17512 gdb_assert (per_cu->cu == NULL);
348e048f 17513
52dc124a 17514 read_signatured_type (sig_type);
348e048f 17515
6721b2ec 17516 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17517}
17518
dee91e82
DE
17519/* die_reader_func for read_signatured_type.
17520 This is identical to load_full_comp_unit_reader,
17521 but is kept separate for now. */
348e048f
DE
17522
17523static void
dee91e82
DE
17524read_signatured_type_reader (const struct die_reader_specs *reader,
17525 gdb_byte *info_ptr,
17526 struct die_info *comp_unit_die,
17527 int has_children,
17528 void *data)
348e048f 17529{
dee91e82 17530 struct dwarf2_cu *cu = reader->cu;
348e048f 17531
dee91e82
DE
17532 gdb_assert (cu->die_hash == NULL);
17533 cu->die_hash =
17534 htab_create_alloc_ex (cu->header.length / 12,
17535 die_hash,
17536 die_eq,
17537 NULL,
17538 &cu->comp_unit_obstack,
17539 hashtab_obstack_allocate,
17540 dummy_obstack_deallocate);
348e048f 17541
dee91e82
DE
17542 if (has_children)
17543 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17544 &info_ptr, comp_unit_die);
17545 cu->dies = comp_unit_die;
17546 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17547
17548 /* We try not to read any attributes in this function, because not
9cdd5dbd 17549 all CUs needed for references have been loaded yet, and symbol
348e048f 17550 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17551 or we won't be able to build types correctly.
17552 Similarly, if we do not read the producer, we can not apply
17553 producer-specific interpretation. */
95554aad 17554 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17555}
348e048f 17556
3019eac3
DE
17557/* Read in a signatured type and build its CU and DIEs.
17558 If the type is a stub for the real type in a DWO file,
17559 read in the real type from the DWO file as well. */
dee91e82
DE
17560
17561static void
17562read_signatured_type (struct signatured_type *sig_type)
17563{
17564 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17565
3019eac3 17566 gdb_assert (per_cu->is_debug_types);
dee91e82 17567 gdb_assert (per_cu->cu == NULL);
348e048f 17568
f4dc4d17
DE
17569 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17570 read_signatured_type_reader, NULL);
c906108c
SS
17571}
17572
c906108c
SS
17573/* Decode simple location descriptions.
17574 Given a pointer to a dwarf block that defines a location, compute
17575 the location and return the value.
17576
4cecd739
DJ
17577 NOTE drow/2003-11-18: This function is called in two situations
17578 now: for the address of static or global variables (partial symbols
17579 only) and for offsets into structures which are expected to be
17580 (more or less) constant. The partial symbol case should go away,
17581 and only the constant case should remain. That will let this
17582 function complain more accurately. A few special modes are allowed
17583 without complaint for global variables (for instance, global
17584 register values and thread-local values).
c906108c
SS
17585
17586 A location description containing no operations indicates that the
4cecd739 17587 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17588 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17589 callers will only want a very basic result and this can become a
21ae7a4d
JK
17590 complaint.
17591
17592 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17593
17594static CORE_ADDR
e7c27a73 17595decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17596{
e7c27a73 17597 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17598 size_t i;
17599 size_t size = blk->size;
21ae7a4d
JK
17600 gdb_byte *data = blk->data;
17601 CORE_ADDR stack[64];
17602 int stacki;
17603 unsigned int bytes_read, unsnd;
17604 gdb_byte op;
c906108c 17605
21ae7a4d
JK
17606 i = 0;
17607 stacki = 0;
17608 stack[stacki] = 0;
17609 stack[++stacki] = 0;
17610
17611 while (i < size)
17612 {
17613 op = data[i++];
17614 switch (op)
17615 {
17616 case DW_OP_lit0:
17617 case DW_OP_lit1:
17618 case DW_OP_lit2:
17619 case DW_OP_lit3:
17620 case DW_OP_lit4:
17621 case DW_OP_lit5:
17622 case DW_OP_lit6:
17623 case DW_OP_lit7:
17624 case DW_OP_lit8:
17625 case DW_OP_lit9:
17626 case DW_OP_lit10:
17627 case DW_OP_lit11:
17628 case DW_OP_lit12:
17629 case DW_OP_lit13:
17630 case DW_OP_lit14:
17631 case DW_OP_lit15:
17632 case DW_OP_lit16:
17633 case DW_OP_lit17:
17634 case DW_OP_lit18:
17635 case DW_OP_lit19:
17636 case DW_OP_lit20:
17637 case DW_OP_lit21:
17638 case DW_OP_lit22:
17639 case DW_OP_lit23:
17640 case DW_OP_lit24:
17641 case DW_OP_lit25:
17642 case DW_OP_lit26:
17643 case DW_OP_lit27:
17644 case DW_OP_lit28:
17645 case DW_OP_lit29:
17646 case DW_OP_lit30:
17647 case DW_OP_lit31:
17648 stack[++stacki] = op - DW_OP_lit0;
17649 break;
f1bea926 17650
21ae7a4d
JK
17651 case DW_OP_reg0:
17652 case DW_OP_reg1:
17653 case DW_OP_reg2:
17654 case DW_OP_reg3:
17655 case DW_OP_reg4:
17656 case DW_OP_reg5:
17657 case DW_OP_reg6:
17658 case DW_OP_reg7:
17659 case DW_OP_reg8:
17660 case DW_OP_reg9:
17661 case DW_OP_reg10:
17662 case DW_OP_reg11:
17663 case DW_OP_reg12:
17664 case DW_OP_reg13:
17665 case DW_OP_reg14:
17666 case DW_OP_reg15:
17667 case DW_OP_reg16:
17668 case DW_OP_reg17:
17669 case DW_OP_reg18:
17670 case DW_OP_reg19:
17671 case DW_OP_reg20:
17672 case DW_OP_reg21:
17673 case DW_OP_reg22:
17674 case DW_OP_reg23:
17675 case DW_OP_reg24:
17676 case DW_OP_reg25:
17677 case DW_OP_reg26:
17678 case DW_OP_reg27:
17679 case DW_OP_reg28:
17680 case DW_OP_reg29:
17681 case DW_OP_reg30:
17682 case DW_OP_reg31:
17683 stack[++stacki] = op - DW_OP_reg0;
17684 if (i < size)
17685 dwarf2_complex_location_expr_complaint ();
17686 break;
c906108c 17687
21ae7a4d
JK
17688 case DW_OP_regx:
17689 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17690 i += bytes_read;
17691 stack[++stacki] = unsnd;
17692 if (i < size)
17693 dwarf2_complex_location_expr_complaint ();
17694 break;
c906108c 17695
21ae7a4d
JK
17696 case DW_OP_addr:
17697 stack[++stacki] = read_address (objfile->obfd, &data[i],
17698 cu, &bytes_read);
17699 i += bytes_read;
17700 break;
d53d4ac5 17701
21ae7a4d
JK
17702 case DW_OP_const1u:
17703 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17704 i += 1;
17705 break;
17706
17707 case DW_OP_const1s:
17708 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17709 i += 1;
17710 break;
17711
17712 case DW_OP_const2u:
17713 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17714 i += 2;
17715 break;
17716
17717 case DW_OP_const2s:
17718 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17719 i += 2;
17720 break;
d53d4ac5 17721
21ae7a4d
JK
17722 case DW_OP_const4u:
17723 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17724 i += 4;
17725 break;
17726
17727 case DW_OP_const4s:
17728 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17729 i += 4;
17730 break;
17731
585861ea
JK
17732 case DW_OP_const8u:
17733 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17734 i += 8;
17735 break;
17736
21ae7a4d
JK
17737 case DW_OP_constu:
17738 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17739 &bytes_read);
17740 i += bytes_read;
17741 break;
17742
17743 case DW_OP_consts:
17744 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17745 i += bytes_read;
17746 break;
17747
17748 case DW_OP_dup:
17749 stack[stacki + 1] = stack[stacki];
17750 stacki++;
17751 break;
17752
17753 case DW_OP_plus:
17754 stack[stacki - 1] += stack[stacki];
17755 stacki--;
17756 break;
17757
17758 case DW_OP_plus_uconst:
17759 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17760 &bytes_read);
17761 i += bytes_read;
17762 break;
17763
17764 case DW_OP_minus:
17765 stack[stacki - 1] -= stack[stacki];
17766 stacki--;
17767 break;
17768
17769 case DW_OP_deref:
17770 /* If we're not the last op, then we definitely can't encode
17771 this using GDB's address_class enum. This is valid for partial
17772 global symbols, although the variable's address will be bogus
17773 in the psymtab. */
17774 if (i < size)
17775 dwarf2_complex_location_expr_complaint ();
17776 break;
17777
17778 case DW_OP_GNU_push_tls_address:
17779 /* The top of the stack has the offset from the beginning
17780 of the thread control block at which the variable is located. */
17781 /* Nothing should follow this operator, so the top of stack would
17782 be returned. */
17783 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17784 address will be bogus in the psymtab. Make it always at least
17785 non-zero to not look as a variable garbage collected by linker
17786 which have DW_OP_addr 0. */
21ae7a4d
JK
17787 if (i < size)
17788 dwarf2_complex_location_expr_complaint ();
585861ea 17789 stack[stacki]++;
21ae7a4d
JK
17790 break;
17791
17792 case DW_OP_GNU_uninit:
17793 break;
17794
3019eac3 17795 case DW_OP_GNU_addr_index:
49f6c839 17796 case DW_OP_GNU_const_index:
3019eac3
DE
17797 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17798 &bytes_read);
17799 i += bytes_read;
17800 break;
17801
21ae7a4d
JK
17802 default:
17803 {
f39c6ffd 17804 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17805
17806 if (name)
17807 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17808 name);
17809 else
17810 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17811 op);
17812 }
17813
17814 return (stack[stacki]);
d53d4ac5 17815 }
3c6e0cb3 17816
21ae7a4d
JK
17817 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17818 outside of the allocated space. Also enforce minimum>0. */
17819 if (stacki >= ARRAY_SIZE (stack) - 1)
17820 {
17821 complaint (&symfile_complaints,
17822 _("location description stack overflow"));
17823 return 0;
17824 }
17825
17826 if (stacki <= 0)
17827 {
17828 complaint (&symfile_complaints,
17829 _("location description stack underflow"));
17830 return 0;
17831 }
17832 }
17833 return (stack[stacki]);
c906108c
SS
17834}
17835
17836/* memory allocation interface */
17837
c906108c 17838static struct dwarf_block *
7b5a2f43 17839dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17840{
17841 struct dwarf_block *blk;
17842
17843 blk = (struct dwarf_block *)
7b5a2f43 17844 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17845 return (blk);
17846}
17847
c906108c 17848static struct die_info *
b60c80d6 17849dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17850{
17851 struct die_info *die;
b60c80d6
DJ
17852 size_t size = sizeof (struct die_info);
17853
17854 if (num_attrs > 1)
17855 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17856
b60c80d6 17857 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17858 memset (die, 0, sizeof (struct die_info));
17859 return (die);
17860}
2e276125
JB
17861
17862\f
17863/* Macro support. */
17864
2e276125
JB
17865/* Return the full name of file number I in *LH's file name table.
17866 Use COMP_DIR as the name of the current directory of the
17867 compilation. The result is allocated using xmalloc; the caller is
17868 responsible for freeing it. */
17869static char *
17870file_full_name (int file, struct line_header *lh, const char *comp_dir)
17871{
6a83a1e6
EZ
17872 /* Is the file number a valid index into the line header's file name
17873 table? Remember that file numbers start with one, not zero. */
17874 if (1 <= file && file <= lh->num_file_names)
17875 {
17876 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17877
6a83a1e6
EZ
17878 if (IS_ABSOLUTE_PATH (fe->name))
17879 return xstrdup (fe->name);
17880 else
17881 {
17882 const char *dir;
17883 int dir_len;
17884 char *full_name;
17885
17886 if (fe->dir_index)
17887 dir = lh->include_dirs[fe->dir_index - 1];
17888 else
17889 dir = comp_dir;
17890
17891 if (dir)
17892 {
17893 dir_len = strlen (dir);
17894 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17895 strcpy (full_name, dir);
17896 full_name[dir_len] = '/';
17897 strcpy (full_name + dir_len + 1, fe->name);
17898 return full_name;
17899 }
17900 else
17901 return xstrdup (fe->name);
17902 }
17903 }
2e276125
JB
17904 else
17905 {
6a83a1e6
EZ
17906 /* The compiler produced a bogus file number. We can at least
17907 record the macro definitions made in the file, even if we
17908 won't be able to find the file by name. */
17909 char fake_name[80];
9a619af0 17910
8c042590
PM
17911 xsnprintf (fake_name, sizeof (fake_name),
17912 "<bad macro file number %d>", file);
2e276125 17913
6e70227d 17914 complaint (&symfile_complaints,
6a83a1e6
EZ
17915 _("bad file number in macro information (%d)"),
17916 file);
2e276125 17917
6a83a1e6 17918 return xstrdup (fake_name);
2e276125
JB
17919 }
17920}
17921
17922
17923static struct macro_source_file *
17924macro_start_file (int file, int line,
17925 struct macro_source_file *current_file,
17926 const char *comp_dir,
17927 struct line_header *lh, struct objfile *objfile)
17928{
17929 /* The full name of this source file. */
17930 char *full_name = file_full_name (file, lh, comp_dir);
17931
17932 /* We don't create a macro table for this compilation unit
17933 at all until we actually get a filename. */
17934 if (! pending_macros)
6532ff36
TT
17935 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17936 objfile->per_bfd->macro_cache);
2e276125
JB
17937
17938 if (! current_file)
abc9d0dc
TT
17939 {
17940 /* If we have no current file, then this must be the start_file
17941 directive for the compilation unit's main source file. */
17942 current_file = macro_set_main (pending_macros, full_name);
17943 macro_define_special (pending_macros);
17944 }
2e276125
JB
17945 else
17946 current_file = macro_include (current_file, line, full_name);
17947
17948 xfree (full_name);
6e70227d 17949
2e276125
JB
17950 return current_file;
17951}
17952
17953
17954/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17955 followed by a null byte. */
17956static char *
17957copy_string (const char *buf, int len)
17958{
17959 char *s = xmalloc (len + 1);
9a619af0 17960
2e276125
JB
17961 memcpy (s, buf, len);
17962 s[len] = '\0';
2e276125
JB
17963 return s;
17964}
17965
17966
17967static const char *
17968consume_improper_spaces (const char *p, const char *body)
17969{
17970 if (*p == ' ')
17971 {
4d3c2250 17972 complaint (&symfile_complaints,
3e43a32a
MS
17973 _("macro definition contains spaces "
17974 "in formal argument list:\n`%s'"),
4d3c2250 17975 body);
2e276125
JB
17976
17977 while (*p == ' ')
17978 p++;
17979 }
17980
17981 return p;
17982}
17983
17984
17985static void
17986parse_macro_definition (struct macro_source_file *file, int line,
17987 const char *body)
17988{
17989 const char *p;
17990
17991 /* The body string takes one of two forms. For object-like macro
17992 definitions, it should be:
17993
17994 <macro name> " " <definition>
17995
17996 For function-like macro definitions, it should be:
17997
17998 <macro name> "() " <definition>
17999 or
18000 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18001
18002 Spaces may appear only where explicitly indicated, and in the
18003 <definition>.
18004
18005 The Dwarf 2 spec says that an object-like macro's name is always
18006 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18007 the space when the macro's definition is the empty string.
2e276125
JB
18008
18009 The Dwarf 2 spec says that there should be no spaces between the
18010 formal arguments in a function-like macro's formal argument list,
18011 but versions of GCC around March 2002 include spaces after the
18012 commas. */
18013
18014
18015 /* Find the extent of the macro name. The macro name is terminated
18016 by either a space or null character (for an object-like macro) or
18017 an opening paren (for a function-like macro). */
18018 for (p = body; *p; p++)
18019 if (*p == ' ' || *p == '(')
18020 break;
18021
18022 if (*p == ' ' || *p == '\0')
18023 {
18024 /* It's an object-like macro. */
18025 int name_len = p - body;
18026 char *name = copy_string (body, name_len);
18027 const char *replacement;
18028
18029 if (*p == ' ')
18030 replacement = body + name_len + 1;
18031 else
18032 {
4d3c2250 18033 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18034 replacement = body + name_len;
18035 }
6e70227d 18036
2e276125
JB
18037 macro_define_object (file, line, name, replacement);
18038
18039 xfree (name);
18040 }
18041 else if (*p == '(')
18042 {
18043 /* It's a function-like macro. */
18044 char *name = copy_string (body, p - body);
18045 int argc = 0;
18046 int argv_size = 1;
18047 char **argv = xmalloc (argv_size * sizeof (*argv));
18048
18049 p++;
18050
18051 p = consume_improper_spaces (p, body);
18052
18053 /* Parse the formal argument list. */
18054 while (*p && *p != ')')
18055 {
18056 /* Find the extent of the current argument name. */
18057 const char *arg_start = p;
18058
18059 while (*p && *p != ',' && *p != ')' && *p != ' ')
18060 p++;
18061
18062 if (! *p || p == arg_start)
4d3c2250 18063 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18064 else
18065 {
18066 /* Make sure argv has room for the new argument. */
18067 if (argc >= argv_size)
18068 {
18069 argv_size *= 2;
18070 argv = xrealloc (argv, argv_size * sizeof (*argv));
18071 }
18072
18073 argv[argc++] = copy_string (arg_start, p - arg_start);
18074 }
18075
18076 p = consume_improper_spaces (p, body);
18077
18078 /* Consume the comma, if present. */
18079 if (*p == ',')
18080 {
18081 p++;
18082
18083 p = consume_improper_spaces (p, body);
18084 }
18085 }
18086
18087 if (*p == ')')
18088 {
18089 p++;
18090
18091 if (*p == ' ')
18092 /* Perfectly formed definition, no complaints. */
18093 macro_define_function (file, line, name,
6e70227d 18094 argc, (const char **) argv,
2e276125
JB
18095 p + 1);
18096 else if (*p == '\0')
18097 {
18098 /* Complain, but do define it. */
4d3c2250 18099 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18100 macro_define_function (file, line, name,
6e70227d 18101 argc, (const char **) argv,
2e276125
JB
18102 p);
18103 }
18104 else
18105 /* Just complain. */
4d3c2250 18106 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18107 }
18108 else
18109 /* Just complain. */
4d3c2250 18110 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18111
18112 xfree (name);
18113 {
18114 int i;
18115
18116 for (i = 0; i < argc; i++)
18117 xfree (argv[i]);
18118 }
18119 xfree (argv);
18120 }
18121 else
4d3c2250 18122 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18123}
18124
cf2c3c16
TT
18125/* Skip some bytes from BYTES according to the form given in FORM.
18126 Returns the new pointer. */
2e276125 18127
cf2c3c16 18128static gdb_byte *
f664829e 18129skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18130 enum dwarf_form form,
18131 unsigned int offset_size,
18132 struct dwarf2_section_info *section)
2e276125 18133{
cf2c3c16 18134 unsigned int bytes_read;
2e276125 18135
cf2c3c16 18136 switch (form)
2e276125 18137 {
cf2c3c16
TT
18138 case DW_FORM_data1:
18139 case DW_FORM_flag:
18140 ++bytes;
18141 break;
18142
18143 case DW_FORM_data2:
18144 bytes += 2;
18145 break;
18146
18147 case DW_FORM_data4:
18148 bytes += 4;
18149 break;
18150
18151 case DW_FORM_data8:
18152 bytes += 8;
18153 break;
18154
18155 case DW_FORM_string:
18156 read_direct_string (abfd, bytes, &bytes_read);
18157 bytes += bytes_read;
18158 break;
18159
18160 case DW_FORM_sec_offset:
18161 case DW_FORM_strp:
36586728 18162 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18163 bytes += offset_size;
18164 break;
18165
18166 case DW_FORM_block:
18167 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18168 bytes += bytes_read;
18169 break;
18170
18171 case DW_FORM_block1:
18172 bytes += 1 + read_1_byte (abfd, bytes);
18173 break;
18174 case DW_FORM_block2:
18175 bytes += 2 + read_2_bytes (abfd, bytes);
18176 break;
18177 case DW_FORM_block4:
18178 bytes += 4 + read_4_bytes (abfd, bytes);
18179 break;
18180
18181 case DW_FORM_sdata:
18182 case DW_FORM_udata:
3019eac3
DE
18183 case DW_FORM_GNU_addr_index:
18184 case DW_FORM_GNU_str_index:
f664829e
DE
18185 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18186 if (bytes == NULL)
18187 {
18188 dwarf2_section_buffer_overflow_complaint (section);
18189 return NULL;
18190 }
cf2c3c16
TT
18191 break;
18192
18193 default:
18194 {
18195 complain:
18196 complaint (&symfile_complaints,
18197 _("invalid form 0x%x in `%s'"),
18198 form,
18199 section->asection->name);
18200 return NULL;
18201 }
2e276125
JB
18202 }
18203
cf2c3c16
TT
18204 return bytes;
18205}
757a13d0 18206
cf2c3c16
TT
18207/* A helper for dwarf_decode_macros that handles skipping an unknown
18208 opcode. Returns an updated pointer to the macro data buffer; or,
18209 on error, issues a complaint and returns NULL. */
757a13d0 18210
cf2c3c16
TT
18211static gdb_byte *
18212skip_unknown_opcode (unsigned int opcode,
18213 gdb_byte **opcode_definitions,
f664829e 18214 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18215 bfd *abfd,
18216 unsigned int offset_size,
18217 struct dwarf2_section_info *section)
18218{
18219 unsigned int bytes_read, i;
18220 unsigned long arg;
18221 gdb_byte *defn;
2e276125 18222
cf2c3c16 18223 if (opcode_definitions[opcode] == NULL)
2e276125 18224 {
cf2c3c16
TT
18225 complaint (&symfile_complaints,
18226 _("unrecognized DW_MACFINO opcode 0x%x"),
18227 opcode);
18228 return NULL;
18229 }
2e276125 18230
cf2c3c16
TT
18231 defn = opcode_definitions[opcode];
18232 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18233 defn += bytes_read;
2e276125 18234
cf2c3c16
TT
18235 for (i = 0; i < arg; ++i)
18236 {
f664829e
DE
18237 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18238 section);
cf2c3c16
TT
18239 if (mac_ptr == NULL)
18240 {
18241 /* skip_form_bytes already issued the complaint. */
18242 return NULL;
18243 }
18244 }
757a13d0 18245
cf2c3c16
TT
18246 return mac_ptr;
18247}
757a13d0 18248
cf2c3c16
TT
18249/* A helper function which parses the header of a macro section.
18250 If the macro section is the extended (for now called "GNU") type,
18251 then this updates *OFFSET_SIZE. Returns a pointer to just after
18252 the header, or issues a complaint and returns NULL on error. */
757a13d0 18253
cf2c3c16
TT
18254static gdb_byte *
18255dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18256 bfd *abfd,
18257 gdb_byte *mac_ptr,
18258 unsigned int *offset_size,
18259 int section_is_gnu)
18260{
18261 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18262
cf2c3c16
TT
18263 if (section_is_gnu)
18264 {
18265 unsigned int version, flags;
757a13d0 18266
cf2c3c16
TT
18267 version = read_2_bytes (abfd, mac_ptr);
18268 if (version != 4)
18269 {
18270 complaint (&symfile_complaints,
18271 _("unrecognized version `%d' in .debug_macro section"),
18272 version);
18273 return NULL;
18274 }
18275 mac_ptr += 2;
757a13d0 18276
cf2c3c16
TT
18277 flags = read_1_byte (abfd, mac_ptr);
18278 ++mac_ptr;
18279 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18280
cf2c3c16
TT
18281 if ((flags & 2) != 0)
18282 /* We don't need the line table offset. */
18283 mac_ptr += *offset_size;
757a13d0 18284
cf2c3c16
TT
18285 /* Vendor opcode descriptions. */
18286 if ((flags & 4) != 0)
18287 {
18288 unsigned int i, count;
757a13d0 18289
cf2c3c16
TT
18290 count = read_1_byte (abfd, mac_ptr);
18291 ++mac_ptr;
18292 for (i = 0; i < count; ++i)
18293 {
18294 unsigned int opcode, bytes_read;
18295 unsigned long arg;
18296
18297 opcode = read_1_byte (abfd, mac_ptr);
18298 ++mac_ptr;
18299 opcode_definitions[opcode] = mac_ptr;
18300 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18301 mac_ptr += bytes_read;
18302 mac_ptr += arg;
18303 }
757a13d0 18304 }
cf2c3c16 18305 }
757a13d0 18306
cf2c3c16
TT
18307 return mac_ptr;
18308}
757a13d0 18309
cf2c3c16 18310/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18311 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18312
18313static void
18314dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18315 struct macro_source_file *current_file,
18316 struct line_header *lh, char *comp_dir,
18317 struct dwarf2_section_info *section,
36586728 18318 int section_is_gnu, int section_is_dwz,
cf2c3c16 18319 unsigned int offset_size,
8fc3fc34
TT
18320 struct objfile *objfile,
18321 htab_t include_hash)
cf2c3c16
TT
18322{
18323 enum dwarf_macro_record_type macinfo_type;
18324 int at_commandline;
18325 gdb_byte *opcode_definitions[256];
757a13d0 18326
cf2c3c16
TT
18327 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18328 &offset_size, section_is_gnu);
18329 if (mac_ptr == NULL)
18330 {
18331 /* We already issued a complaint. */
18332 return;
18333 }
757a13d0
JK
18334
18335 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18336 GDB is still reading the definitions from command line. First
18337 DW_MACINFO_start_file will need to be ignored as it was already executed
18338 to create CURRENT_FILE for the main source holding also the command line
18339 definitions. On first met DW_MACINFO_start_file this flag is reset to
18340 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18341
18342 at_commandline = 1;
18343
18344 do
18345 {
18346 /* Do we at least have room for a macinfo type byte? */
18347 if (mac_ptr >= mac_end)
18348 {
f664829e 18349 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18350 break;
18351 }
18352
18353 macinfo_type = read_1_byte (abfd, mac_ptr);
18354 mac_ptr++;
18355
cf2c3c16
TT
18356 /* Note that we rely on the fact that the corresponding GNU and
18357 DWARF constants are the same. */
757a13d0
JK
18358 switch (macinfo_type)
18359 {
18360 /* A zero macinfo type indicates the end of the macro
18361 information. */
18362 case 0:
18363 break;
2e276125 18364
cf2c3c16
TT
18365 case DW_MACRO_GNU_define:
18366 case DW_MACRO_GNU_undef:
18367 case DW_MACRO_GNU_define_indirect:
18368 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18369 case DW_MACRO_GNU_define_indirect_alt:
18370 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18371 {
891d2f0b 18372 unsigned int bytes_read;
2e276125
JB
18373 int line;
18374 char *body;
cf2c3c16 18375 int is_define;
2e276125 18376
cf2c3c16
TT
18377 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18378 mac_ptr += bytes_read;
18379
18380 if (macinfo_type == DW_MACRO_GNU_define
18381 || macinfo_type == DW_MACRO_GNU_undef)
18382 {
18383 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18384 mac_ptr += bytes_read;
18385 }
18386 else
18387 {
18388 LONGEST str_offset;
18389
18390 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18391 mac_ptr += offset_size;
2e276125 18392
36586728 18393 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18394 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18395 || section_is_dwz)
36586728
TT
18396 {
18397 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18398
18399 body = read_indirect_string_from_dwz (dwz, str_offset);
18400 }
18401 else
18402 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18403 }
18404
18405 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18406 || macinfo_type == DW_MACRO_GNU_define_indirect
18407 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18408 if (! current_file)
757a13d0
JK
18409 {
18410 /* DWARF violation as no main source is present. */
18411 complaint (&symfile_complaints,
18412 _("debug info with no main source gives macro %s "
18413 "on line %d: %s"),
cf2c3c16
TT
18414 is_define ? _("definition") : _("undefinition"),
18415 line, body);
757a13d0
JK
18416 break;
18417 }
3e43a32a
MS
18418 if ((line == 0 && !at_commandline)
18419 || (line != 0 && at_commandline))
4d3c2250 18420 complaint (&symfile_complaints,
757a13d0
JK
18421 _("debug info gives %s macro %s with %s line %d: %s"),
18422 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18423 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18424 line == 0 ? _("zero") : _("non-zero"), line, body);
18425
cf2c3c16 18426 if (is_define)
757a13d0 18427 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18428 else
18429 {
18430 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18431 || macinfo_type == DW_MACRO_GNU_undef_indirect
18432 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18433 macro_undef (current_file, line, body);
18434 }
2e276125
JB
18435 }
18436 break;
18437
cf2c3c16 18438 case DW_MACRO_GNU_start_file:
2e276125 18439 {
891d2f0b 18440 unsigned int bytes_read;
2e276125
JB
18441 int line, file;
18442
18443 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18444 mac_ptr += bytes_read;
18445 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18446 mac_ptr += bytes_read;
18447
3e43a32a
MS
18448 if ((line == 0 && !at_commandline)
18449 || (line != 0 && at_commandline))
757a13d0
JK
18450 complaint (&symfile_complaints,
18451 _("debug info gives source %d included "
18452 "from %s at %s line %d"),
18453 file, at_commandline ? _("command-line") : _("file"),
18454 line == 0 ? _("zero") : _("non-zero"), line);
18455
18456 if (at_commandline)
18457 {
cf2c3c16
TT
18458 /* This DW_MACRO_GNU_start_file was executed in the
18459 pass one. */
757a13d0
JK
18460 at_commandline = 0;
18461 }
18462 else
18463 current_file = macro_start_file (file, line,
18464 current_file, comp_dir,
cf2c3c16 18465 lh, objfile);
2e276125
JB
18466 }
18467 break;
18468
cf2c3c16 18469 case DW_MACRO_GNU_end_file:
2e276125 18470 if (! current_file)
4d3c2250 18471 complaint (&symfile_complaints,
3e43a32a
MS
18472 _("macro debug info has an unmatched "
18473 "`close_file' directive"));
2e276125
JB
18474 else
18475 {
18476 current_file = current_file->included_by;
18477 if (! current_file)
18478 {
cf2c3c16 18479 enum dwarf_macro_record_type next_type;
2e276125
JB
18480
18481 /* GCC circa March 2002 doesn't produce the zero
18482 type byte marking the end of the compilation
18483 unit. Complain if it's not there, but exit no
18484 matter what. */
18485
18486 /* Do we at least have room for a macinfo type byte? */
18487 if (mac_ptr >= mac_end)
18488 {
f664829e 18489 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18490 return;
18491 }
18492
18493 /* We don't increment mac_ptr here, so this is just
18494 a look-ahead. */
18495 next_type = read_1_byte (abfd, mac_ptr);
18496 if (next_type != 0)
4d3c2250 18497 complaint (&symfile_complaints,
3e43a32a
MS
18498 _("no terminating 0-type entry for "
18499 "macros in `.debug_macinfo' section"));
2e276125
JB
18500
18501 return;
18502 }
18503 }
18504 break;
18505
cf2c3c16 18506 case DW_MACRO_GNU_transparent_include:
36586728 18507 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18508 {
18509 LONGEST offset;
8fc3fc34 18510 void **slot;
a036ba48
TT
18511 bfd *include_bfd = abfd;
18512 struct dwarf2_section_info *include_section = section;
18513 struct dwarf2_section_info alt_section;
18514 gdb_byte *include_mac_end = mac_end;
18515 int is_dwz = section_is_dwz;
18516 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18517
18518 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18519 mac_ptr += offset_size;
18520
a036ba48
TT
18521 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18522 {
18523 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18524
18525 dwarf2_read_section (dwarf2_per_objfile->objfile,
18526 &dwz->macro);
18527
18528 include_bfd = dwz->macro.asection->owner;
18529 include_section = &dwz->macro;
18530 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18531 is_dwz = 1;
18532 }
18533
18534 new_mac_ptr = include_section->buffer + offset;
18535 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18536
8fc3fc34
TT
18537 if (*slot != NULL)
18538 {
18539 /* This has actually happened; see
18540 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18541 complaint (&symfile_complaints,
18542 _("recursive DW_MACRO_GNU_transparent_include in "
18543 ".debug_macro section"));
18544 }
18545 else
18546 {
a036ba48 18547 *slot = new_mac_ptr;
36586728 18548
a036ba48 18549 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18550 include_mac_end, current_file,
8fc3fc34 18551 lh, comp_dir,
36586728 18552 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18553 offset_size, objfile, include_hash);
18554
a036ba48 18555 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18556 }
cf2c3c16
TT
18557 }
18558 break;
18559
2e276125 18560 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18561 if (!section_is_gnu)
18562 {
18563 unsigned int bytes_read;
18564 int constant;
2e276125 18565
cf2c3c16
TT
18566 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18567 mac_ptr += bytes_read;
18568 read_direct_string (abfd, mac_ptr, &bytes_read);
18569 mac_ptr += bytes_read;
2e276125 18570
cf2c3c16
TT
18571 /* We don't recognize any vendor extensions. */
18572 break;
18573 }
18574 /* FALLTHROUGH */
18575
18576 default:
18577 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18578 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18579 section);
18580 if (mac_ptr == NULL)
18581 return;
18582 break;
2e276125 18583 }
757a13d0 18584 } while (macinfo_type != 0);
2e276125 18585}
8e19ed76 18586
cf2c3c16 18587static void
09262596
DE
18588dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18589 char *comp_dir, int section_is_gnu)
cf2c3c16 18590{
bb5ed363 18591 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18592 struct line_header *lh = cu->line_header;
18593 bfd *abfd;
cf2c3c16
TT
18594 gdb_byte *mac_ptr, *mac_end;
18595 struct macro_source_file *current_file = 0;
18596 enum dwarf_macro_record_type macinfo_type;
18597 unsigned int offset_size = cu->header.offset_size;
18598 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18599 struct cleanup *cleanup;
18600 htab_t include_hash;
18601 void **slot;
09262596
DE
18602 struct dwarf2_section_info *section;
18603 const char *section_name;
18604
18605 if (cu->dwo_unit != NULL)
18606 {
18607 if (section_is_gnu)
18608 {
18609 section = &cu->dwo_unit->dwo_file->sections.macro;
18610 section_name = ".debug_macro.dwo";
18611 }
18612 else
18613 {
18614 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18615 section_name = ".debug_macinfo.dwo";
18616 }
18617 }
18618 else
18619 {
18620 if (section_is_gnu)
18621 {
18622 section = &dwarf2_per_objfile->macro;
18623 section_name = ".debug_macro";
18624 }
18625 else
18626 {
18627 section = &dwarf2_per_objfile->macinfo;
18628 section_name = ".debug_macinfo";
18629 }
18630 }
cf2c3c16 18631
bb5ed363 18632 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18633 if (section->buffer == NULL)
18634 {
fceca515 18635 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18636 return;
18637 }
09262596 18638 abfd = section->asection->owner;
cf2c3c16
TT
18639
18640 /* First pass: Find the name of the base filename.
18641 This filename is needed in order to process all macros whose definition
18642 (or undefinition) comes from the command line. These macros are defined
18643 before the first DW_MACINFO_start_file entry, and yet still need to be
18644 associated to the base file.
18645
18646 To determine the base file name, we scan the macro definitions until we
18647 reach the first DW_MACINFO_start_file entry. We then initialize
18648 CURRENT_FILE accordingly so that any macro definition found before the
18649 first DW_MACINFO_start_file can still be associated to the base file. */
18650
18651 mac_ptr = section->buffer + offset;
18652 mac_end = section->buffer + section->size;
18653
18654 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18655 &offset_size, section_is_gnu);
18656 if (mac_ptr == NULL)
18657 {
18658 /* We already issued a complaint. */
18659 return;
18660 }
18661
18662 do
18663 {
18664 /* Do we at least have room for a macinfo type byte? */
18665 if (mac_ptr >= mac_end)
18666 {
18667 /* Complaint is printed during the second pass as GDB will probably
18668 stop the first pass earlier upon finding
18669 DW_MACINFO_start_file. */
18670 break;
18671 }
18672
18673 macinfo_type = read_1_byte (abfd, mac_ptr);
18674 mac_ptr++;
18675
18676 /* Note that we rely on the fact that the corresponding GNU and
18677 DWARF constants are the same. */
18678 switch (macinfo_type)
18679 {
18680 /* A zero macinfo type indicates the end of the macro
18681 information. */
18682 case 0:
18683 break;
18684
18685 case DW_MACRO_GNU_define:
18686 case DW_MACRO_GNU_undef:
18687 /* Only skip the data by MAC_PTR. */
18688 {
18689 unsigned int bytes_read;
18690
18691 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18692 mac_ptr += bytes_read;
18693 read_direct_string (abfd, mac_ptr, &bytes_read);
18694 mac_ptr += bytes_read;
18695 }
18696 break;
18697
18698 case DW_MACRO_GNU_start_file:
18699 {
18700 unsigned int bytes_read;
18701 int line, file;
18702
18703 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18704 mac_ptr += bytes_read;
18705 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18706 mac_ptr += bytes_read;
18707
18708 current_file = macro_start_file (file, line, current_file,
bb5ed363 18709 comp_dir, lh, objfile);
cf2c3c16
TT
18710 }
18711 break;
18712
18713 case DW_MACRO_GNU_end_file:
18714 /* No data to skip by MAC_PTR. */
18715 break;
18716
18717 case DW_MACRO_GNU_define_indirect:
18718 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18719 case DW_MACRO_GNU_define_indirect_alt:
18720 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18721 {
18722 unsigned int bytes_read;
18723
18724 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18725 mac_ptr += bytes_read;
18726 mac_ptr += offset_size;
18727 }
18728 break;
18729
18730 case DW_MACRO_GNU_transparent_include:
f7a35f02 18731 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18732 /* Note that, according to the spec, a transparent include
18733 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18734 skip this opcode. */
18735 mac_ptr += offset_size;
18736 break;
18737
18738 case DW_MACINFO_vendor_ext:
18739 /* Only skip the data by MAC_PTR. */
18740 if (!section_is_gnu)
18741 {
18742 unsigned int bytes_read;
18743
18744 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18745 mac_ptr += bytes_read;
18746 read_direct_string (abfd, mac_ptr, &bytes_read);
18747 mac_ptr += bytes_read;
18748 }
18749 /* FALLTHROUGH */
18750
18751 default:
18752 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18753 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18754 section);
18755 if (mac_ptr == NULL)
18756 return;
18757 break;
18758 }
18759 } while (macinfo_type != 0 && current_file == NULL);
18760
18761 /* Second pass: Process all entries.
18762
18763 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18764 command-line macro definitions/undefinitions. This flag is unset when we
18765 reach the first DW_MACINFO_start_file entry. */
18766
8fc3fc34
TT
18767 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18768 NULL, xcalloc, xfree);
18769 cleanup = make_cleanup_htab_delete (include_hash);
18770 mac_ptr = section->buffer + offset;
18771 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18772 *slot = mac_ptr;
18773 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18774 current_file, lh, comp_dir, section,
18775 section_is_gnu, 0,
8fc3fc34
TT
18776 offset_size, objfile, include_hash);
18777 do_cleanups (cleanup);
cf2c3c16
TT
18778}
18779
8e19ed76 18780/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18781 if so return true else false. */
380bca97 18782
8e19ed76
PS
18783static int
18784attr_form_is_block (struct attribute *attr)
18785{
18786 return (attr == NULL ? 0 :
18787 attr->form == DW_FORM_block1
18788 || attr->form == DW_FORM_block2
18789 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18790 || attr->form == DW_FORM_block
18791 || attr->form == DW_FORM_exprloc);
8e19ed76 18792}
4c2df51b 18793
c6a0999f
JB
18794/* Return non-zero if ATTR's value is a section offset --- classes
18795 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18796 You may use DW_UNSND (attr) to retrieve such offsets.
18797
18798 Section 7.5.4, "Attribute Encodings", explains that no attribute
18799 may have a value that belongs to more than one of these classes; it
18800 would be ambiguous if we did, because we use the same forms for all
18801 of them. */
380bca97 18802
3690dd37
JB
18803static int
18804attr_form_is_section_offset (struct attribute *attr)
18805{
18806 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18807 || attr->form == DW_FORM_data8
18808 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18809}
18810
3690dd37
JB
18811/* Return non-zero if ATTR's value falls in the 'constant' class, or
18812 zero otherwise. When this function returns true, you can apply
18813 dwarf2_get_attr_constant_value to it.
18814
18815 However, note that for some attributes you must check
18816 attr_form_is_section_offset before using this test. DW_FORM_data4
18817 and DW_FORM_data8 are members of both the constant class, and of
18818 the classes that contain offsets into other debug sections
18819 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18820 that, if an attribute's can be either a constant or one of the
18821 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18822 taken as section offsets, not constants. */
380bca97 18823
3690dd37
JB
18824static int
18825attr_form_is_constant (struct attribute *attr)
18826{
18827 switch (attr->form)
18828 {
18829 case DW_FORM_sdata:
18830 case DW_FORM_udata:
18831 case DW_FORM_data1:
18832 case DW_FORM_data2:
18833 case DW_FORM_data4:
18834 case DW_FORM_data8:
18835 return 1;
18836 default:
18837 return 0;
18838 }
18839}
18840
3019eac3
DE
18841/* Return the .debug_loc section to use for CU.
18842 For DWO files use .debug_loc.dwo. */
18843
18844static struct dwarf2_section_info *
18845cu_debug_loc_section (struct dwarf2_cu *cu)
18846{
18847 if (cu->dwo_unit)
18848 return &cu->dwo_unit->dwo_file->sections.loc;
18849 return &dwarf2_per_objfile->loc;
18850}
18851
8cf6f0b1
TT
18852/* A helper function that fills in a dwarf2_loclist_baton. */
18853
18854static void
18855fill_in_loclist_baton (struct dwarf2_cu *cu,
18856 struct dwarf2_loclist_baton *baton,
18857 struct attribute *attr)
18858{
3019eac3
DE
18859 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18860
18861 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18862
18863 baton->per_cu = cu->per_cu;
18864 gdb_assert (baton->per_cu);
18865 /* We don't know how long the location list is, but make sure we
18866 don't run off the edge of the section. */
3019eac3
DE
18867 baton->size = section->size - DW_UNSND (attr);
18868 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18869 baton->base_address = cu->base_address;
f664829e 18870 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18871}
18872
4c2df51b
DJ
18873static void
18874dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18875 struct dwarf2_cu *cu)
4c2df51b 18876{
bb5ed363 18877 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18878 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18879
3690dd37 18880 if (attr_form_is_section_offset (attr)
3019eac3 18881 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18882 the section. If so, fall through to the complaint in the
18883 other branch. */
3019eac3 18884 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18885 {
0d53c4c4 18886 struct dwarf2_loclist_baton *baton;
4c2df51b 18887
bb5ed363 18888 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18889 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18890
8cf6f0b1 18891 fill_in_loclist_baton (cu, baton, attr);
be391dca 18892
d00adf39 18893 if (cu->base_known == 0)
0d53c4c4 18894 complaint (&symfile_complaints,
3e43a32a
MS
18895 _("Location list used without "
18896 "specifying the CU base address."));
4c2df51b 18897
768a979c 18898 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18899 SYMBOL_LOCATION_BATON (sym) = baton;
18900 }
18901 else
18902 {
18903 struct dwarf2_locexpr_baton *baton;
18904
bb5ed363 18905 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18906 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18907 baton->per_cu = cu->per_cu;
18908 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18909
18910 if (attr_form_is_block (attr))
18911 {
18912 /* Note that we're just copying the block's data pointer
18913 here, not the actual data. We're still pointing into the
6502dd73
DJ
18914 info_buffer for SYM's objfile; right now we never release
18915 that buffer, but when we do clean up properly this may
18916 need to change. */
0d53c4c4
DJ
18917 baton->size = DW_BLOCK (attr)->size;
18918 baton->data = DW_BLOCK (attr)->data;
18919 }
18920 else
18921 {
18922 dwarf2_invalid_attrib_class_complaint ("location description",
18923 SYMBOL_NATURAL_NAME (sym));
18924 baton->size = 0;
0d53c4c4 18925 }
6e70227d 18926
768a979c 18927 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18928 SYMBOL_LOCATION_BATON (sym) = baton;
18929 }
4c2df51b 18930}
6502dd73 18931
9aa1f1e3
TT
18932/* Return the OBJFILE associated with the compilation unit CU. If CU
18933 came from a separate debuginfo file, then the master objfile is
18934 returned. */
ae0d2f24
UW
18935
18936struct objfile *
18937dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18938{
9291a0cd 18939 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18940
18941 /* Return the master objfile, so that we can report and look up the
18942 correct file containing this variable. */
18943 if (objfile->separate_debug_objfile_backlink)
18944 objfile = objfile->separate_debug_objfile_backlink;
18945
18946 return objfile;
18947}
18948
96408a79
SA
18949/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18950 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18951 CU_HEADERP first. */
18952
18953static const struct comp_unit_head *
18954per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18955 struct dwarf2_per_cu_data *per_cu)
18956{
96408a79
SA
18957 gdb_byte *info_ptr;
18958
18959 if (per_cu->cu)
18960 return &per_cu->cu->header;
18961
0bc3a05c 18962 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
18963
18964 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 18965 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
18966
18967 return cu_headerp;
18968}
18969
ae0d2f24
UW
18970/* Return the address size given in the compilation unit header for CU. */
18971
98714339 18972int
ae0d2f24
UW
18973dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18974{
96408a79
SA
18975 struct comp_unit_head cu_header_local;
18976 const struct comp_unit_head *cu_headerp;
c471e790 18977
96408a79
SA
18978 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18979
18980 return cu_headerp->addr_size;
ae0d2f24
UW
18981}
18982
9eae7c52
TT
18983/* Return the offset size given in the compilation unit header for CU. */
18984
18985int
18986dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18987{
96408a79
SA
18988 struct comp_unit_head cu_header_local;
18989 const struct comp_unit_head *cu_headerp;
9c6c53f7 18990
96408a79
SA
18991 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18992
18993 return cu_headerp->offset_size;
18994}
18995
18996/* See its dwarf2loc.h declaration. */
18997
18998int
18999dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19000{
19001 struct comp_unit_head cu_header_local;
19002 const struct comp_unit_head *cu_headerp;
19003
19004 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19005
19006 if (cu_headerp->version == 2)
19007 return cu_headerp->addr_size;
19008 else
19009 return cu_headerp->offset_size;
181cebd4
JK
19010}
19011
9aa1f1e3
TT
19012/* Return the text offset of the CU. The returned offset comes from
19013 this CU's objfile. If this objfile came from a separate debuginfo
19014 file, then the offset may be different from the corresponding
19015 offset in the parent objfile. */
19016
19017CORE_ADDR
19018dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19019{
bb3fa9d0 19020 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19021
19022 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19023}
19024
348e048f
DE
19025/* Locate the .debug_info compilation unit from CU's objfile which contains
19026 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19027
19028static struct dwarf2_per_cu_data *
b64f50a1 19029dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19030 unsigned int offset_in_dwz,
ae038cb0
DJ
19031 struct objfile *objfile)
19032{
19033 struct dwarf2_per_cu_data *this_cu;
19034 int low, high;
36586728 19035 const sect_offset *cu_off;
ae038cb0 19036
ae038cb0
DJ
19037 low = 0;
19038 high = dwarf2_per_objfile->n_comp_units - 1;
19039 while (high > low)
19040 {
36586728 19041 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19042 int mid = low + (high - low) / 2;
9a619af0 19043
36586728
TT
19044 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19045 cu_off = &mid_cu->offset;
19046 if (mid_cu->is_dwz > offset_in_dwz
19047 || (mid_cu->is_dwz == offset_in_dwz
19048 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19049 high = mid;
19050 else
19051 low = mid + 1;
19052 }
19053 gdb_assert (low == high);
36586728
TT
19054 this_cu = dwarf2_per_objfile->all_comp_units[low];
19055 cu_off = &this_cu->offset;
19056 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19057 {
36586728 19058 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19059 error (_("Dwarf Error: could not find partial DIE containing "
19060 "offset 0x%lx [in module %s]"),
b64f50a1 19061 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19062
b64f50a1
JK
19063 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19064 <= offset.sect_off);
ae038cb0
DJ
19065 return dwarf2_per_objfile->all_comp_units[low-1];
19066 }
19067 else
19068 {
19069 this_cu = dwarf2_per_objfile->all_comp_units[low];
19070 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19071 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19072 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19073 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19074 return this_cu;
19075 }
19076}
19077
23745b47 19078/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19079
9816fde3 19080static void
23745b47 19081init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19082{
9816fde3 19083 memset (cu, 0, sizeof (*cu));
23745b47
DE
19084 per_cu->cu = cu;
19085 cu->per_cu = per_cu;
19086 cu->objfile = per_cu->objfile;
93311388 19087 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19088}
19089
19090/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19091
19092static void
95554aad
TT
19093prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19094 enum language pretend_language)
9816fde3
JK
19095{
19096 struct attribute *attr;
19097
19098 /* Set the language we're debugging. */
19099 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19100 if (attr)
19101 set_cu_language (DW_UNSND (attr), cu);
19102 else
9cded63f 19103 {
95554aad 19104 cu->language = pretend_language;
9cded63f
TT
19105 cu->language_defn = language_def (cu->language);
19106 }
dee91e82
DE
19107
19108 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19109 if (attr)
19110 cu->producer = DW_STRING (attr);
93311388
DE
19111}
19112
ae038cb0
DJ
19113/* Release one cached compilation unit, CU. We unlink it from the tree
19114 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19115 the caller is responsible for that.
19116 NOTE: DATA is a void * because this function is also used as a
19117 cleanup routine. */
ae038cb0
DJ
19118
19119static void
68dc6402 19120free_heap_comp_unit (void *data)
ae038cb0
DJ
19121{
19122 struct dwarf2_cu *cu = data;
19123
23745b47
DE
19124 gdb_assert (cu->per_cu != NULL);
19125 cu->per_cu->cu = NULL;
ae038cb0
DJ
19126 cu->per_cu = NULL;
19127
19128 obstack_free (&cu->comp_unit_obstack, NULL);
19129
19130 xfree (cu);
19131}
19132
72bf9492 19133/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19134 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19135 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19136
19137static void
19138free_stack_comp_unit (void *data)
19139{
19140 struct dwarf2_cu *cu = data;
19141
23745b47
DE
19142 gdb_assert (cu->per_cu != NULL);
19143 cu->per_cu->cu = NULL;
19144 cu->per_cu = NULL;
19145
72bf9492
DJ
19146 obstack_free (&cu->comp_unit_obstack, NULL);
19147 cu->partial_dies = NULL;
ae038cb0
DJ
19148}
19149
19150/* Free all cached compilation units. */
19151
19152static void
19153free_cached_comp_units (void *data)
19154{
19155 struct dwarf2_per_cu_data *per_cu, **last_chain;
19156
19157 per_cu = dwarf2_per_objfile->read_in_chain;
19158 last_chain = &dwarf2_per_objfile->read_in_chain;
19159 while (per_cu != NULL)
19160 {
19161 struct dwarf2_per_cu_data *next_cu;
19162
19163 next_cu = per_cu->cu->read_in_chain;
19164
68dc6402 19165 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19166 *last_chain = next_cu;
19167
19168 per_cu = next_cu;
19169 }
19170}
19171
19172/* Increase the age counter on each cached compilation unit, and free
19173 any that are too old. */
19174
19175static void
19176age_cached_comp_units (void)
19177{
19178 struct dwarf2_per_cu_data *per_cu, **last_chain;
19179
19180 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19181 per_cu = dwarf2_per_objfile->read_in_chain;
19182 while (per_cu != NULL)
19183 {
19184 per_cu->cu->last_used ++;
19185 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19186 dwarf2_mark (per_cu->cu);
19187 per_cu = per_cu->cu->read_in_chain;
19188 }
19189
19190 per_cu = dwarf2_per_objfile->read_in_chain;
19191 last_chain = &dwarf2_per_objfile->read_in_chain;
19192 while (per_cu != NULL)
19193 {
19194 struct dwarf2_per_cu_data *next_cu;
19195
19196 next_cu = per_cu->cu->read_in_chain;
19197
19198 if (!per_cu->cu->mark)
19199 {
68dc6402 19200 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19201 *last_chain = next_cu;
19202 }
19203 else
19204 last_chain = &per_cu->cu->read_in_chain;
19205
19206 per_cu = next_cu;
19207 }
19208}
19209
19210/* Remove a single compilation unit from the cache. */
19211
19212static void
dee91e82 19213free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19214{
19215 struct dwarf2_per_cu_data *per_cu, **last_chain;
19216
19217 per_cu = dwarf2_per_objfile->read_in_chain;
19218 last_chain = &dwarf2_per_objfile->read_in_chain;
19219 while (per_cu != NULL)
19220 {
19221 struct dwarf2_per_cu_data *next_cu;
19222
19223 next_cu = per_cu->cu->read_in_chain;
19224
dee91e82 19225 if (per_cu == target_per_cu)
ae038cb0 19226 {
68dc6402 19227 free_heap_comp_unit (per_cu->cu);
dee91e82 19228 per_cu->cu = NULL;
ae038cb0
DJ
19229 *last_chain = next_cu;
19230 break;
19231 }
19232 else
19233 last_chain = &per_cu->cu->read_in_chain;
19234
19235 per_cu = next_cu;
19236 }
19237}
19238
fe3e1990
DJ
19239/* Release all extra memory associated with OBJFILE. */
19240
19241void
19242dwarf2_free_objfile (struct objfile *objfile)
19243{
19244 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19245
19246 if (dwarf2_per_objfile == NULL)
19247 return;
19248
19249 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19250 free_cached_comp_units (NULL);
19251
7b9f3c50
DE
19252 if (dwarf2_per_objfile->quick_file_names_table)
19253 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19254
fe3e1990
DJ
19255 /* Everything else should be on the objfile obstack. */
19256}
19257
dee91e82
DE
19258/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19259 We store these in a hash table separate from the DIEs, and preserve them
19260 when the DIEs are flushed out of cache.
19261
19262 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19263 uniquely identify the type. A file may have multiple .debug_types sections,
19264 or the type may come from a DWO file. We have to use something in
19265 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19266 routine, get_die_type_at_offset, from outside this file, and thus won't
19267 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19268 of the objfile. */
1c379e20 19269
dee91e82 19270struct dwarf2_per_cu_offset_and_type
1c379e20 19271{
dee91e82 19272 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19273 sect_offset offset;
1c379e20
DJ
19274 struct type *type;
19275};
19276
dee91e82 19277/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19278
19279static hashval_t
dee91e82 19280per_cu_offset_and_type_hash (const void *item)
1c379e20 19281{
dee91e82 19282 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19283
dee91e82 19284 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19285}
19286
dee91e82 19287/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19288
19289static int
dee91e82 19290per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19291{
dee91e82
DE
19292 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19293 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19294
dee91e82
DE
19295 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19296 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19297}
19298
19299/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19300 table if necessary. For convenience, return TYPE.
19301
19302 The DIEs reading must have careful ordering to:
19303 * Not cause infite loops trying to read in DIEs as a prerequisite for
19304 reading current DIE.
19305 * Not trying to dereference contents of still incompletely read in types
19306 while reading in other DIEs.
19307 * Enable referencing still incompletely read in types just by a pointer to
19308 the type without accessing its fields.
19309
19310 Therefore caller should follow these rules:
19311 * Try to fetch any prerequisite types we may need to build this DIE type
19312 before building the type and calling set_die_type.
e71ec853 19313 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19314 possible before fetching more types to complete the current type.
19315 * Make the type as complete as possible before fetching more types. */
1c379e20 19316
f792889a 19317static struct type *
1c379e20
DJ
19318set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19319{
dee91e82 19320 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19321 struct objfile *objfile = cu->objfile;
1c379e20 19322
b4ba55a1
JB
19323 /* For Ada types, make sure that the gnat-specific data is always
19324 initialized (if not already set). There are a few types where
19325 we should not be doing so, because the type-specific area is
19326 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19327 where the type-specific area is used to store the floatformat).
19328 But this is not a problem, because the gnat-specific information
19329 is actually not needed for these types. */
19330 if (need_gnat_info (cu)
19331 && TYPE_CODE (type) != TYPE_CODE_FUNC
19332 && TYPE_CODE (type) != TYPE_CODE_FLT
19333 && !HAVE_GNAT_AUX_INFO (type))
19334 INIT_GNAT_SPECIFIC (type);
19335
dee91e82 19336 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19337 {
dee91e82
DE
19338 dwarf2_per_objfile->die_type_hash =
19339 htab_create_alloc_ex (127,
19340 per_cu_offset_and_type_hash,
19341 per_cu_offset_and_type_eq,
19342 NULL,
19343 &objfile->objfile_obstack,
19344 hashtab_obstack_allocate,
19345 dummy_obstack_deallocate);
f792889a 19346 }
1c379e20 19347
dee91e82 19348 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19349 ofs.offset = die->offset;
19350 ofs.type = type;
dee91e82
DE
19351 slot = (struct dwarf2_per_cu_offset_and_type **)
19352 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19353 if (*slot)
19354 complaint (&symfile_complaints,
19355 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19356 die->offset.sect_off);
673bfd45 19357 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19358 **slot = ofs;
f792889a 19359 return type;
1c379e20
DJ
19360}
19361
380bca97 19362/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19363 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19364
19365static struct type *
b64f50a1 19366get_die_type_at_offset (sect_offset offset,
673bfd45 19367 struct dwarf2_per_cu_data *per_cu)
1c379e20 19368{
dee91e82 19369 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19370
dee91e82 19371 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19372 return NULL;
1c379e20 19373
dee91e82 19374 ofs.per_cu = per_cu;
673bfd45 19375 ofs.offset = offset;
dee91e82 19376 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19377 if (slot)
19378 return slot->type;
19379 else
19380 return NULL;
19381}
19382
673bfd45
DE
19383/* Look up the type for DIE in the appropriate type_hash table,
19384 or return NULL if DIE does not have a saved type. */
19385
19386static struct type *
19387get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19388{
19389 return get_die_type_at_offset (die->offset, cu->per_cu);
19390}
19391
10b3939b
DJ
19392/* Add a dependence relationship from CU to REF_PER_CU. */
19393
19394static void
19395dwarf2_add_dependence (struct dwarf2_cu *cu,
19396 struct dwarf2_per_cu_data *ref_per_cu)
19397{
19398 void **slot;
19399
19400 if (cu->dependencies == NULL)
19401 cu->dependencies
19402 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19403 NULL, &cu->comp_unit_obstack,
19404 hashtab_obstack_allocate,
19405 dummy_obstack_deallocate);
19406
19407 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19408 if (*slot == NULL)
19409 *slot = ref_per_cu;
19410}
1c379e20 19411
f504f079
DE
19412/* Subroutine of dwarf2_mark to pass to htab_traverse.
19413 Set the mark field in every compilation unit in the
ae038cb0
DJ
19414 cache that we must keep because we are keeping CU. */
19415
10b3939b
DJ
19416static int
19417dwarf2_mark_helper (void **slot, void *data)
19418{
19419 struct dwarf2_per_cu_data *per_cu;
19420
19421 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19422
19423 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19424 reading of the chain. As such dependencies remain valid it is not much
19425 useful to track and undo them during QUIT cleanups. */
19426 if (per_cu->cu == NULL)
19427 return 1;
19428
10b3939b
DJ
19429 if (per_cu->cu->mark)
19430 return 1;
19431 per_cu->cu->mark = 1;
19432
19433 if (per_cu->cu->dependencies != NULL)
19434 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19435
19436 return 1;
19437}
19438
f504f079
DE
19439/* Set the mark field in CU and in every other compilation unit in the
19440 cache that we must keep because we are keeping CU. */
19441
ae038cb0
DJ
19442static void
19443dwarf2_mark (struct dwarf2_cu *cu)
19444{
19445 if (cu->mark)
19446 return;
19447 cu->mark = 1;
10b3939b
DJ
19448 if (cu->dependencies != NULL)
19449 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19450}
19451
19452static void
19453dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19454{
19455 while (per_cu)
19456 {
19457 per_cu->cu->mark = 0;
19458 per_cu = per_cu->cu->read_in_chain;
19459 }
72bf9492
DJ
19460}
19461
72bf9492
DJ
19462/* Trivial hash function for partial_die_info: the hash value of a DIE
19463 is its offset in .debug_info for this objfile. */
19464
19465static hashval_t
19466partial_die_hash (const void *item)
19467{
19468 const struct partial_die_info *part_die = item;
9a619af0 19469
b64f50a1 19470 return part_die->offset.sect_off;
72bf9492
DJ
19471}
19472
19473/* Trivial comparison function for partial_die_info structures: two DIEs
19474 are equal if they have the same offset. */
19475
19476static int
19477partial_die_eq (const void *item_lhs, const void *item_rhs)
19478{
19479 const struct partial_die_info *part_die_lhs = item_lhs;
19480 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19481
b64f50a1 19482 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19483}
19484
ae038cb0
DJ
19485static struct cmd_list_element *set_dwarf2_cmdlist;
19486static struct cmd_list_element *show_dwarf2_cmdlist;
19487
19488static void
19489set_dwarf2_cmd (char *args, int from_tty)
19490{
19491 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19492}
19493
19494static void
19495show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19496{
ae038cb0
DJ
19497 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19498}
19499
4bf44c1c 19500/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19501
19502static void
c1bd65d0 19503dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19504{
19505 struct dwarf2_per_objfile *data = d;
8b70b953 19506 int ix;
8b70b953 19507
95554aad
TT
19508 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19509 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19510 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19511
8b70b953 19512 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19513
19514 if (data->dwo_files)
19515 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19516
19517 if (data->dwz_file && data->dwz_file->dwz_bfd)
19518 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19519}
19520
19521\f
ae2de4f8 19522/* The "save gdb-index" command. */
9291a0cd
TT
19523
19524/* The contents of the hash table we create when building the string
19525 table. */
19526struct strtab_entry
19527{
19528 offset_type offset;
19529 const char *str;
19530};
19531
559a7a62
JK
19532/* Hash function for a strtab_entry.
19533
19534 Function is used only during write_hash_table so no index format backward
19535 compatibility is needed. */
b89be57b 19536
9291a0cd
TT
19537static hashval_t
19538hash_strtab_entry (const void *e)
19539{
19540 const struct strtab_entry *entry = e;
559a7a62 19541 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19542}
19543
19544/* Equality function for a strtab_entry. */
b89be57b 19545
9291a0cd
TT
19546static int
19547eq_strtab_entry (const void *a, const void *b)
19548{
19549 const struct strtab_entry *ea = a;
19550 const struct strtab_entry *eb = b;
19551 return !strcmp (ea->str, eb->str);
19552}
19553
19554/* Create a strtab_entry hash table. */
b89be57b 19555
9291a0cd
TT
19556static htab_t
19557create_strtab (void)
19558{
19559 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19560 xfree, xcalloc, xfree);
19561}
19562
19563/* Add a string to the constant pool. Return the string's offset in
19564 host order. */
b89be57b 19565
9291a0cd
TT
19566static offset_type
19567add_string (htab_t table, struct obstack *cpool, const char *str)
19568{
19569 void **slot;
19570 struct strtab_entry entry;
19571 struct strtab_entry *result;
19572
19573 entry.str = str;
19574 slot = htab_find_slot (table, &entry, INSERT);
19575 if (*slot)
19576 result = *slot;
19577 else
19578 {
19579 result = XNEW (struct strtab_entry);
19580 result->offset = obstack_object_size (cpool);
19581 result->str = str;
19582 obstack_grow_str0 (cpool, str);
19583 *slot = result;
19584 }
19585 return result->offset;
19586}
19587
19588/* An entry in the symbol table. */
19589struct symtab_index_entry
19590{
19591 /* The name of the symbol. */
19592 const char *name;
19593 /* The offset of the name in the constant pool. */
19594 offset_type index_offset;
19595 /* A sorted vector of the indices of all the CUs that hold an object
19596 of this name. */
19597 VEC (offset_type) *cu_indices;
19598};
19599
19600/* The symbol table. This is a power-of-2-sized hash table. */
19601struct mapped_symtab
19602{
19603 offset_type n_elements;
19604 offset_type size;
19605 struct symtab_index_entry **data;
19606};
19607
19608/* Hash function for a symtab_index_entry. */
b89be57b 19609
9291a0cd
TT
19610static hashval_t
19611hash_symtab_entry (const void *e)
19612{
19613 const struct symtab_index_entry *entry = e;
19614 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19615 sizeof (offset_type) * VEC_length (offset_type,
19616 entry->cu_indices),
19617 0);
19618}
19619
19620/* Equality function for a symtab_index_entry. */
b89be57b 19621
9291a0cd
TT
19622static int
19623eq_symtab_entry (const void *a, const void *b)
19624{
19625 const struct symtab_index_entry *ea = a;
19626 const struct symtab_index_entry *eb = b;
19627 int len = VEC_length (offset_type, ea->cu_indices);
19628 if (len != VEC_length (offset_type, eb->cu_indices))
19629 return 0;
19630 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19631 VEC_address (offset_type, eb->cu_indices),
19632 sizeof (offset_type) * len);
19633}
19634
19635/* Destroy a symtab_index_entry. */
b89be57b 19636
9291a0cd
TT
19637static void
19638delete_symtab_entry (void *p)
19639{
19640 struct symtab_index_entry *entry = p;
19641 VEC_free (offset_type, entry->cu_indices);
19642 xfree (entry);
19643}
19644
19645/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19646
9291a0cd 19647static htab_t
3876f04e 19648create_symbol_hash_table (void)
9291a0cd
TT
19649{
19650 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19651 delete_symtab_entry, xcalloc, xfree);
19652}
19653
19654/* Create a new mapped symtab object. */
b89be57b 19655
9291a0cd
TT
19656static struct mapped_symtab *
19657create_mapped_symtab (void)
19658{
19659 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19660 symtab->n_elements = 0;
19661 symtab->size = 1024;
19662 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19663 return symtab;
19664}
19665
19666/* Destroy a mapped_symtab. */
b89be57b 19667
9291a0cd
TT
19668static void
19669cleanup_mapped_symtab (void *p)
19670{
19671 struct mapped_symtab *symtab = p;
19672 /* The contents of the array are freed when the other hash table is
19673 destroyed. */
19674 xfree (symtab->data);
19675 xfree (symtab);
19676}
19677
19678/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19679 the slot.
19680
19681 Function is used only during write_hash_table so no index format backward
19682 compatibility is needed. */
b89be57b 19683
9291a0cd
TT
19684static struct symtab_index_entry **
19685find_slot (struct mapped_symtab *symtab, const char *name)
19686{
559a7a62 19687 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19688
19689 index = hash & (symtab->size - 1);
19690 step = ((hash * 17) & (symtab->size - 1)) | 1;
19691
19692 for (;;)
19693 {
19694 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19695 return &symtab->data[index];
19696 index = (index + step) & (symtab->size - 1);
19697 }
19698}
19699
19700/* Expand SYMTAB's hash table. */
b89be57b 19701
9291a0cd
TT
19702static void
19703hash_expand (struct mapped_symtab *symtab)
19704{
19705 offset_type old_size = symtab->size;
19706 offset_type i;
19707 struct symtab_index_entry **old_entries = symtab->data;
19708
19709 symtab->size *= 2;
19710 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19711
19712 for (i = 0; i < old_size; ++i)
19713 {
19714 if (old_entries[i])
19715 {
19716 struct symtab_index_entry **slot = find_slot (symtab,
19717 old_entries[i]->name);
19718 *slot = old_entries[i];
19719 }
19720 }
19721
19722 xfree (old_entries);
19723}
19724
156942c7
DE
19725/* Add an entry to SYMTAB. NAME is the name of the symbol.
19726 CU_INDEX is the index of the CU in which the symbol appears.
19727 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19728
9291a0cd
TT
19729static void
19730add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19731 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19732 offset_type cu_index)
19733{
19734 struct symtab_index_entry **slot;
156942c7 19735 offset_type cu_index_and_attrs;
9291a0cd
TT
19736
19737 ++symtab->n_elements;
19738 if (4 * symtab->n_elements / 3 >= symtab->size)
19739 hash_expand (symtab);
19740
19741 slot = find_slot (symtab, name);
19742 if (!*slot)
19743 {
19744 *slot = XNEW (struct symtab_index_entry);
19745 (*slot)->name = name;
156942c7 19746 /* index_offset is set later. */
9291a0cd
TT
19747 (*slot)->cu_indices = NULL;
19748 }
156942c7
DE
19749
19750 cu_index_and_attrs = 0;
19751 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19752 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19753 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19754
19755 /* We don't want to record an index value twice as we want to avoid the
19756 duplication.
19757 We process all global symbols and then all static symbols
19758 (which would allow us to avoid the duplication by only having to check
19759 the last entry pushed), but a symbol could have multiple kinds in one CU.
19760 To keep things simple we don't worry about the duplication here and
19761 sort and uniqufy the list after we've processed all symbols. */
19762 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19763}
19764
19765/* qsort helper routine for uniquify_cu_indices. */
19766
19767static int
19768offset_type_compare (const void *ap, const void *bp)
19769{
19770 offset_type a = *(offset_type *) ap;
19771 offset_type b = *(offset_type *) bp;
19772
19773 return (a > b) - (b > a);
19774}
19775
19776/* Sort and remove duplicates of all symbols' cu_indices lists. */
19777
19778static void
19779uniquify_cu_indices (struct mapped_symtab *symtab)
19780{
19781 int i;
19782
19783 for (i = 0; i < symtab->size; ++i)
19784 {
19785 struct symtab_index_entry *entry = symtab->data[i];
19786
19787 if (entry
19788 && entry->cu_indices != NULL)
19789 {
19790 unsigned int next_to_insert, next_to_check;
19791 offset_type last_value;
19792
19793 qsort (VEC_address (offset_type, entry->cu_indices),
19794 VEC_length (offset_type, entry->cu_indices),
19795 sizeof (offset_type), offset_type_compare);
19796
19797 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19798 next_to_insert = 1;
19799 for (next_to_check = 1;
19800 next_to_check < VEC_length (offset_type, entry->cu_indices);
19801 ++next_to_check)
19802 {
19803 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19804 != last_value)
19805 {
19806 last_value = VEC_index (offset_type, entry->cu_indices,
19807 next_to_check);
19808 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19809 last_value);
19810 ++next_to_insert;
19811 }
19812 }
19813 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19814 }
19815 }
9291a0cd
TT
19816}
19817
19818/* Add a vector of indices to the constant pool. */
b89be57b 19819
9291a0cd 19820static offset_type
3876f04e 19821add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19822 struct symtab_index_entry *entry)
19823{
19824 void **slot;
19825
3876f04e 19826 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19827 if (!*slot)
19828 {
19829 offset_type len = VEC_length (offset_type, entry->cu_indices);
19830 offset_type val = MAYBE_SWAP (len);
19831 offset_type iter;
19832 int i;
19833
19834 *slot = entry;
19835 entry->index_offset = obstack_object_size (cpool);
19836
19837 obstack_grow (cpool, &val, sizeof (val));
19838 for (i = 0;
19839 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19840 ++i)
19841 {
19842 val = MAYBE_SWAP (iter);
19843 obstack_grow (cpool, &val, sizeof (val));
19844 }
19845 }
19846 else
19847 {
19848 struct symtab_index_entry *old_entry = *slot;
19849 entry->index_offset = old_entry->index_offset;
19850 entry = old_entry;
19851 }
19852 return entry->index_offset;
19853}
19854
19855/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19856 constant pool entries going into the obstack CPOOL. */
b89be57b 19857
9291a0cd
TT
19858static void
19859write_hash_table (struct mapped_symtab *symtab,
19860 struct obstack *output, struct obstack *cpool)
19861{
19862 offset_type i;
3876f04e 19863 htab_t symbol_hash_table;
9291a0cd
TT
19864 htab_t str_table;
19865
3876f04e 19866 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19867 str_table = create_strtab ();
3876f04e 19868
9291a0cd
TT
19869 /* We add all the index vectors to the constant pool first, to
19870 ensure alignment is ok. */
19871 for (i = 0; i < symtab->size; ++i)
19872 {
19873 if (symtab->data[i])
3876f04e 19874 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19875 }
19876
19877 /* Now write out the hash table. */
19878 for (i = 0; i < symtab->size; ++i)
19879 {
19880 offset_type str_off, vec_off;
19881
19882 if (symtab->data[i])
19883 {
19884 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19885 vec_off = symtab->data[i]->index_offset;
19886 }
19887 else
19888 {
19889 /* While 0 is a valid constant pool index, it is not valid
19890 to have 0 for both offsets. */
19891 str_off = 0;
19892 vec_off = 0;
19893 }
19894
19895 str_off = MAYBE_SWAP (str_off);
19896 vec_off = MAYBE_SWAP (vec_off);
19897
19898 obstack_grow (output, &str_off, sizeof (str_off));
19899 obstack_grow (output, &vec_off, sizeof (vec_off));
19900 }
19901
19902 htab_delete (str_table);
3876f04e 19903 htab_delete (symbol_hash_table);
9291a0cd
TT
19904}
19905
0a5429f6
DE
19906/* Struct to map psymtab to CU index in the index file. */
19907struct psymtab_cu_index_map
19908{
19909 struct partial_symtab *psymtab;
19910 unsigned int cu_index;
19911};
19912
19913static hashval_t
19914hash_psymtab_cu_index (const void *item)
19915{
19916 const struct psymtab_cu_index_map *map = item;
19917
19918 return htab_hash_pointer (map->psymtab);
19919}
19920
19921static int
19922eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19923{
19924 const struct psymtab_cu_index_map *lhs = item_lhs;
19925 const struct psymtab_cu_index_map *rhs = item_rhs;
19926
19927 return lhs->psymtab == rhs->psymtab;
19928}
19929
19930/* Helper struct for building the address table. */
19931struct addrmap_index_data
19932{
19933 struct objfile *objfile;
19934 struct obstack *addr_obstack;
19935 htab_t cu_index_htab;
19936
19937 /* Non-zero if the previous_* fields are valid.
19938 We can't write an entry until we see the next entry (since it is only then
19939 that we know the end of the entry). */
19940 int previous_valid;
19941 /* Index of the CU in the table of all CUs in the index file. */
19942 unsigned int previous_cu_index;
0963b4bd 19943 /* Start address of the CU. */
0a5429f6
DE
19944 CORE_ADDR previous_cu_start;
19945};
19946
19947/* Write an address entry to OBSTACK. */
b89be57b 19948
9291a0cd 19949static void
0a5429f6
DE
19950add_address_entry (struct objfile *objfile, struct obstack *obstack,
19951 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 19952{
0a5429f6 19953 offset_type cu_index_to_write;
9291a0cd
TT
19954 char addr[8];
19955 CORE_ADDR baseaddr;
19956
19957 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19958
0a5429f6
DE
19959 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19960 obstack_grow (obstack, addr, 8);
19961 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19962 obstack_grow (obstack, addr, 8);
19963 cu_index_to_write = MAYBE_SWAP (cu_index);
19964 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19965}
19966
19967/* Worker function for traversing an addrmap to build the address table. */
19968
19969static int
19970add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19971{
19972 struct addrmap_index_data *data = datap;
19973 struct partial_symtab *pst = obj;
0a5429f6
DE
19974
19975 if (data->previous_valid)
19976 add_address_entry (data->objfile, data->addr_obstack,
19977 data->previous_cu_start, start_addr,
19978 data->previous_cu_index);
19979
19980 data->previous_cu_start = start_addr;
19981 if (pst != NULL)
19982 {
19983 struct psymtab_cu_index_map find_map, *map;
19984 find_map.psymtab = pst;
19985 map = htab_find (data->cu_index_htab, &find_map);
19986 gdb_assert (map != NULL);
19987 data->previous_cu_index = map->cu_index;
19988 data->previous_valid = 1;
19989 }
19990 else
19991 data->previous_valid = 0;
19992
19993 return 0;
19994}
19995
19996/* Write OBJFILE's address map to OBSTACK.
19997 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19998 in the index file. */
19999
20000static void
20001write_address_map (struct objfile *objfile, struct obstack *obstack,
20002 htab_t cu_index_htab)
20003{
20004 struct addrmap_index_data addrmap_index_data;
20005
20006 /* When writing the address table, we have to cope with the fact that
20007 the addrmap iterator only provides the start of a region; we have to
20008 wait until the next invocation to get the start of the next region. */
20009
20010 addrmap_index_data.objfile = objfile;
20011 addrmap_index_data.addr_obstack = obstack;
20012 addrmap_index_data.cu_index_htab = cu_index_htab;
20013 addrmap_index_data.previous_valid = 0;
20014
20015 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20016 &addrmap_index_data);
20017
20018 /* It's highly unlikely the last entry (end address = 0xff...ff)
20019 is valid, but we should still handle it.
20020 The end address is recorded as the start of the next region, but that
20021 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20022 anyway. */
20023 if (addrmap_index_data.previous_valid)
20024 add_address_entry (objfile, obstack,
20025 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20026 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20027}
20028
156942c7
DE
20029/* Return the symbol kind of PSYM. */
20030
20031static gdb_index_symbol_kind
20032symbol_kind (struct partial_symbol *psym)
20033{
20034 domain_enum domain = PSYMBOL_DOMAIN (psym);
20035 enum address_class aclass = PSYMBOL_CLASS (psym);
20036
20037 switch (domain)
20038 {
20039 case VAR_DOMAIN:
20040 switch (aclass)
20041 {
20042 case LOC_BLOCK:
20043 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20044 case LOC_TYPEDEF:
20045 return GDB_INDEX_SYMBOL_KIND_TYPE;
20046 case LOC_COMPUTED:
20047 case LOC_CONST_BYTES:
20048 case LOC_OPTIMIZED_OUT:
20049 case LOC_STATIC:
20050 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20051 case LOC_CONST:
20052 /* Note: It's currently impossible to recognize psyms as enum values
20053 short of reading the type info. For now punt. */
20054 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20055 default:
20056 /* There are other LOC_FOO values that one might want to classify
20057 as variables, but dwarf2read.c doesn't currently use them. */
20058 return GDB_INDEX_SYMBOL_KIND_OTHER;
20059 }
20060 case STRUCT_DOMAIN:
20061 return GDB_INDEX_SYMBOL_KIND_TYPE;
20062 default:
20063 return GDB_INDEX_SYMBOL_KIND_OTHER;
20064 }
20065}
20066
9291a0cd 20067/* Add a list of partial symbols to SYMTAB. */
b89be57b 20068
9291a0cd
TT
20069static void
20070write_psymbols (struct mapped_symtab *symtab,
987d643c 20071 htab_t psyms_seen,
9291a0cd
TT
20072 struct partial_symbol **psymp,
20073 int count,
987d643c
TT
20074 offset_type cu_index,
20075 int is_static)
9291a0cd
TT
20076{
20077 for (; count-- > 0; ++psymp)
20078 {
156942c7
DE
20079 struct partial_symbol *psym = *psymp;
20080 void **slot;
987d643c 20081
156942c7 20082 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20083 error (_("Ada is not currently supported by the index"));
987d643c 20084
987d643c 20085 /* Only add a given psymbol once. */
156942c7 20086 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20087 if (!*slot)
20088 {
156942c7
DE
20089 gdb_index_symbol_kind kind = symbol_kind (psym);
20090
20091 *slot = psym;
20092 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20093 is_static, kind, cu_index);
987d643c 20094 }
9291a0cd
TT
20095 }
20096}
20097
20098/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20099 exception if there is an error. */
b89be57b 20100
9291a0cd
TT
20101static void
20102write_obstack (FILE *file, struct obstack *obstack)
20103{
20104 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20105 file)
20106 != obstack_object_size (obstack))
20107 error (_("couldn't data write to file"));
20108}
20109
20110/* Unlink a file if the argument is not NULL. */
b89be57b 20111
9291a0cd
TT
20112static void
20113unlink_if_set (void *p)
20114{
20115 char **filename = p;
20116 if (*filename)
20117 unlink (*filename);
20118}
20119
1fd400ff
TT
20120/* A helper struct used when iterating over debug_types. */
20121struct signatured_type_index_data
20122{
20123 struct objfile *objfile;
20124 struct mapped_symtab *symtab;
20125 struct obstack *types_list;
987d643c 20126 htab_t psyms_seen;
1fd400ff
TT
20127 int cu_index;
20128};
20129
20130/* A helper function that writes a single signatured_type to an
20131 obstack. */
b89be57b 20132
1fd400ff
TT
20133static int
20134write_one_signatured_type (void **slot, void *d)
20135{
20136 struct signatured_type_index_data *info = d;
20137 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20138 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20139 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20140 gdb_byte val[8];
20141
20142 write_psymbols (info->symtab,
987d643c 20143 info->psyms_seen,
3e43a32a
MS
20144 info->objfile->global_psymbols.list
20145 + psymtab->globals_offset,
987d643c
TT
20146 psymtab->n_global_syms, info->cu_index,
20147 0);
1fd400ff 20148 write_psymbols (info->symtab,
987d643c 20149 info->psyms_seen,
3e43a32a
MS
20150 info->objfile->static_psymbols.list
20151 + psymtab->statics_offset,
987d643c
TT
20152 psymtab->n_static_syms, info->cu_index,
20153 1);
1fd400ff 20154
b64f50a1
JK
20155 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20156 entry->per_cu.offset.sect_off);
1fd400ff 20157 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20158 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20159 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20160 obstack_grow (info->types_list, val, 8);
20161 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20162 obstack_grow (info->types_list, val, 8);
20163
20164 ++info->cu_index;
20165
20166 return 1;
20167}
20168
95554aad
TT
20169/* Recurse into all "included" dependencies and write their symbols as
20170 if they appeared in this psymtab. */
20171
20172static void
20173recursively_write_psymbols (struct objfile *objfile,
20174 struct partial_symtab *psymtab,
20175 struct mapped_symtab *symtab,
20176 htab_t psyms_seen,
20177 offset_type cu_index)
20178{
20179 int i;
20180
20181 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20182 if (psymtab->dependencies[i]->user != NULL)
20183 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20184 symtab, psyms_seen, cu_index);
20185
20186 write_psymbols (symtab,
20187 psyms_seen,
20188 objfile->global_psymbols.list + psymtab->globals_offset,
20189 psymtab->n_global_syms, cu_index,
20190 0);
20191 write_psymbols (symtab,
20192 psyms_seen,
20193 objfile->static_psymbols.list + psymtab->statics_offset,
20194 psymtab->n_static_syms, cu_index,
20195 1);
20196}
20197
9291a0cd 20198/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20199
9291a0cd
TT
20200static void
20201write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20202{
20203 struct cleanup *cleanup;
20204 char *filename, *cleanup_filename;
1fd400ff
TT
20205 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20206 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20207 int i;
20208 FILE *out_file;
20209 struct mapped_symtab *symtab;
20210 offset_type val, size_of_contents, total_len;
20211 struct stat st;
987d643c 20212 htab_t psyms_seen;
0a5429f6
DE
20213 htab_t cu_index_htab;
20214 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20215
b4f2f049 20216 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20217 return;
b4f2f049 20218
9291a0cd
TT
20219 if (dwarf2_per_objfile->using_index)
20220 error (_("Cannot use an index to create the index"));
20221
8b70b953
TT
20222 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20223 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20224
9291a0cd 20225 if (stat (objfile->name, &st) < 0)
7e17e088 20226 perror_with_name (objfile->name);
9291a0cd
TT
20227
20228 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20229 INDEX_SUFFIX, (char *) NULL);
20230 cleanup = make_cleanup (xfree, filename);
20231
20232 out_file = fopen (filename, "wb");
20233 if (!out_file)
20234 error (_("Can't open `%s' for writing"), filename);
20235
20236 cleanup_filename = filename;
20237 make_cleanup (unlink_if_set, &cleanup_filename);
20238
20239 symtab = create_mapped_symtab ();
20240 make_cleanup (cleanup_mapped_symtab, symtab);
20241
20242 obstack_init (&addr_obstack);
20243 make_cleanup_obstack_free (&addr_obstack);
20244
20245 obstack_init (&cu_list);
20246 make_cleanup_obstack_free (&cu_list);
20247
1fd400ff
TT
20248 obstack_init (&types_cu_list);
20249 make_cleanup_obstack_free (&types_cu_list);
20250
987d643c
TT
20251 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20252 NULL, xcalloc, xfree);
96408a79 20253 make_cleanup_htab_delete (psyms_seen);
987d643c 20254
0a5429f6
DE
20255 /* While we're scanning CU's create a table that maps a psymtab pointer
20256 (which is what addrmap records) to its index (which is what is recorded
20257 in the index file). This will later be needed to write the address
20258 table. */
20259 cu_index_htab = htab_create_alloc (100,
20260 hash_psymtab_cu_index,
20261 eq_psymtab_cu_index,
20262 NULL, xcalloc, xfree);
96408a79 20263 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20264 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20265 xmalloc (sizeof (struct psymtab_cu_index_map)
20266 * dwarf2_per_objfile->n_comp_units);
20267 make_cleanup (xfree, psymtab_cu_index_map);
20268
20269 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20270 work here. Also, the debug_types entries do not appear in
20271 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20272 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20273 {
3e43a32a
MS
20274 struct dwarf2_per_cu_data *per_cu
20275 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20276 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20277 gdb_byte val[8];
0a5429f6
DE
20278 struct psymtab_cu_index_map *map;
20279 void **slot;
9291a0cd 20280
95554aad
TT
20281 if (psymtab->user == NULL)
20282 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20283
0a5429f6
DE
20284 map = &psymtab_cu_index_map[i];
20285 map->psymtab = psymtab;
20286 map->cu_index = i;
20287 slot = htab_find_slot (cu_index_htab, map, INSERT);
20288 gdb_assert (slot != NULL);
20289 gdb_assert (*slot == NULL);
20290 *slot = map;
9291a0cd 20291
b64f50a1
JK
20292 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20293 per_cu->offset.sect_off);
9291a0cd 20294 obstack_grow (&cu_list, val, 8);
e254ef6a 20295 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20296 obstack_grow (&cu_list, val, 8);
20297 }
20298
0a5429f6
DE
20299 /* Dump the address map. */
20300 write_address_map (objfile, &addr_obstack, cu_index_htab);
20301
1fd400ff
TT
20302 /* Write out the .debug_type entries, if any. */
20303 if (dwarf2_per_objfile->signatured_types)
20304 {
20305 struct signatured_type_index_data sig_data;
20306
20307 sig_data.objfile = objfile;
20308 sig_data.symtab = symtab;
20309 sig_data.types_list = &types_cu_list;
987d643c 20310 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20311 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20312 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20313 write_one_signatured_type, &sig_data);
20314 }
20315
156942c7
DE
20316 /* Now that we've processed all symbols we can shrink their cu_indices
20317 lists. */
20318 uniquify_cu_indices (symtab);
20319
9291a0cd
TT
20320 obstack_init (&constant_pool);
20321 make_cleanup_obstack_free (&constant_pool);
20322 obstack_init (&symtab_obstack);
20323 make_cleanup_obstack_free (&symtab_obstack);
20324 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20325
20326 obstack_init (&contents);
20327 make_cleanup_obstack_free (&contents);
1fd400ff 20328 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20329 total_len = size_of_contents;
20330
20331 /* The version number. */
156942c7 20332 val = MAYBE_SWAP (7);
9291a0cd
TT
20333 obstack_grow (&contents, &val, sizeof (val));
20334
20335 /* The offset of the CU list from the start of the file. */
20336 val = MAYBE_SWAP (total_len);
20337 obstack_grow (&contents, &val, sizeof (val));
20338 total_len += obstack_object_size (&cu_list);
20339
1fd400ff
TT
20340 /* The offset of the types CU list from the start of the file. */
20341 val = MAYBE_SWAP (total_len);
20342 obstack_grow (&contents, &val, sizeof (val));
20343 total_len += obstack_object_size (&types_cu_list);
20344
9291a0cd
TT
20345 /* The offset of the address table from the start of the file. */
20346 val = MAYBE_SWAP (total_len);
20347 obstack_grow (&contents, &val, sizeof (val));
20348 total_len += obstack_object_size (&addr_obstack);
20349
20350 /* The offset of the symbol table from the start of the file. */
20351 val = MAYBE_SWAP (total_len);
20352 obstack_grow (&contents, &val, sizeof (val));
20353 total_len += obstack_object_size (&symtab_obstack);
20354
20355 /* The offset of the constant pool from the start of the file. */
20356 val = MAYBE_SWAP (total_len);
20357 obstack_grow (&contents, &val, sizeof (val));
20358 total_len += obstack_object_size (&constant_pool);
20359
20360 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20361
20362 write_obstack (out_file, &contents);
20363 write_obstack (out_file, &cu_list);
1fd400ff 20364 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20365 write_obstack (out_file, &addr_obstack);
20366 write_obstack (out_file, &symtab_obstack);
20367 write_obstack (out_file, &constant_pool);
20368
20369 fclose (out_file);
20370
20371 /* We want to keep the file, so we set cleanup_filename to NULL
20372 here. See unlink_if_set. */
20373 cleanup_filename = NULL;
20374
20375 do_cleanups (cleanup);
20376}
20377
90476074
TT
20378/* Implementation of the `save gdb-index' command.
20379
20380 Note that the file format used by this command is documented in the
20381 GDB manual. Any changes here must be documented there. */
11570e71 20382
9291a0cd
TT
20383static void
20384save_gdb_index_command (char *arg, int from_tty)
20385{
20386 struct objfile *objfile;
20387
20388 if (!arg || !*arg)
96d19272 20389 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20390
20391 ALL_OBJFILES (objfile)
20392 {
20393 struct stat st;
20394
20395 /* If the objfile does not correspond to an actual file, skip it. */
20396 if (stat (objfile->name, &st) < 0)
20397 continue;
20398
20399 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20400 if (dwarf2_per_objfile)
20401 {
20402 volatile struct gdb_exception except;
20403
20404 TRY_CATCH (except, RETURN_MASK_ERROR)
20405 {
20406 write_psymtabs_to_index (objfile, arg);
20407 }
20408 if (except.reason < 0)
20409 exception_fprintf (gdb_stderr, except,
20410 _("Error while writing index for `%s': "),
20411 objfile->name);
20412 }
20413 }
dce234bc
PP
20414}
20415
9291a0cd
TT
20416\f
20417
9eae7c52
TT
20418int dwarf2_always_disassemble;
20419
20420static void
20421show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20422 struct cmd_list_element *c, const char *value)
20423{
3e43a32a
MS
20424 fprintf_filtered (file,
20425 _("Whether to always disassemble "
20426 "DWARF expressions is %s.\n"),
9eae7c52
TT
20427 value);
20428}
20429
900e11f9
JK
20430static void
20431show_check_physname (struct ui_file *file, int from_tty,
20432 struct cmd_list_element *c, const char *value)
20433{
20434 fprintf_filtered (file,
20435 _("Whether to check \"physname\" is %s.\n"),
20436 value);
20437}
20438
6502dd73
DJ
20439void _initialize_dwarf2_read (void);
20440
20441void
20442_initialize_dwarf2_read (void)
20443{
96d19272
JK
20444 struct cmd_list_element *c;
20445
dce234bc 20446 dwarf2_objfile_data_key
c1bd65d0 20447 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20448
1bedd215
AC
20449 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20450Set DWARF 2 specific variables.\n\
20451Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20452 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20453 0/*allow-unknown*/, &maintenance_set_cmdlist);
20454
1bedd215
AC
20455 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20456Show DWARF 2 specific variables\n\
20457Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20458 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20459 0/*allow-unknown*/, &maintenance_show_cmdlist);
20460
20461 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20462 &dwarf2_max_cache_age, _("\
20463Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20464Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20465A higher limit means that cached compilation units will be stored\n\
20466in memory longer, and more total memory will be used. Zero disables\n\
20467caching, which can slow down startup."),
2c5b56ce 20468 NULL,
920d2a44 20469 show_dwarf2_max_cache_age,
2c5b56ce 20470 &set_dwarf2_cmdlist,
ae038cb0 20471 &show_dwarf2_cmdlist);
d97bc12b 20472
9eae7c52
TT
20473 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20474 &dwarf2_always_disassemble, _("\
20475Set whether `info address' always disassembles DWARF expressions."), _("\
20476Show whether `info address' always disassembles DWARF expressions."), _("\
20477When enabled, DWARF expressions are always printed in an assembly-like\n\
20478syntax. When disabled, expressions will be printed in a more\n\
20479conversational style, when possible."),
20480 NULL,
20481 show_dwarf2_always_disassemble,
20482 &set_dwarf2_cmdlist,
20483 &show_dwarf2_cmdlist);
20484
45cfd468
DE
20485 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20486Set debugging of the dwarf2 reader."), _("\
20487Show debugging of the dwarf2 reader."), _("\
20488When enabled, debugging messages are printed during dwarf2 reading\n\
20489and symtab expansion."),
20490 NULL,
20491 NULL,
20492 &setdebuglist, &showdebuglist);
20493
ccce17b0 20494 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20495Set debugging of the dwarf2 DIE reader."), _("\
20496Show debugging of the dwarf2 DIE reader."), _("\
20497When enabled (non-zero), DIEs are dumped after they are read in.\n\
20498The value is the maximum depth to print."),
ccce17b0
YQ
20499 NULL,
20500 NULL,
20501 &setdebuglist, &showdebuglist);
9291a0cd 20502
900e11f9
JK
20503 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20504Set cross-checking of \"physname\" code against demangler."), _("\
20505Show cross-checking of \"physname\" code against demangler."), _("\
20506When enabled, GDB's internal \"physname\" code is checked against\n\
20507the demangler."),
20508 NULL, show_check_physname,
20509 &setdebuglist, &showdebuglist);
20510
e615022a
DE
20511 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20512 no_class, &use_deprecated_index_sections, _("\
20513Set whether to use deprecated gdb_index sections."), _("\
20514Show whether to use deprecated gdb_index sections."), _("\
20515When enabled, deprecated .gdb_index sections are used anyway.\n\
20516Normally they are ignored either because of a missing feature or\n\
20517performance issue.\n\
20518Warning: This option must be enabled before gdb reads the file."),
20519 NULL,
20520 NULL,
20521 &setlist, &showlist);
20522
96d19272 20523 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20524 _("\
fc1a9d6e 20525Save a gdb-index file.\n\
11570e71 20526Usage: save gdb-index DIRECTORY"),
96d19272
JK
20527 &save_cmdlist);
20528 set_cmd_completer (c, filename_completer);
6502dd73 20529}
This page took 3.026398 seconds and 4 git commands to generate.