* configure.ac (ENABLE_GOLD): Consider *-*-gnu* targets ELF.
[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
ab435259
DE
486 be used without needing to know whether DWO files are in use or not.
487 N.B. This does not apply to DW_AT_ranges appearing in
488 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
489 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
490 DW_AT_ranges_base *would* have to be applied, and we'd have to care
491 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
492 ULONGEST ranges_base;
493
ae038cb0
DJ
494 /* Mark used when releasing cached dies. */
495 unsigned int mark : 1;
496
8be455d7
JK
497 /* This CU references .debug_loc. See the symtab->locations_valid field.
498 This test is imperfect as there may exist optimized debug code not using
499 any location list and still facing inlining issues if handled as
500 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 501 unsigned int has_loclist : 1;
ba919b58 502
1b80a9fa
JK
503 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
504 if all the producer_is_* fields are valid. This information is cached
505 because profiling CU expansion showed excessive time spent in
506 producer_is_gxx_lt_4_6. */
ba919b58
TT
507 unsigned int checked_producer : 1;
508 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 509 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 510 unsigned int producer_is_icc : 1;
e7c27a73
DJ
511};
512
10b3939b
DJ
513/* Persistent data held for a compilation unit, even when not
514 processing it. We put a pointer to this structure in the
28dee7f5 515 read_symtab_private field of the psymtab. */
10b3939b 516
ae038cb0
DJ
517struct dwarf2_per_cu_data
518{
36586728 519 /* The start offset and length of this compilation unit.
45452591 520 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
521 initial_length_size.
522 If the DIE refers to a DWO file, this is always of the original die,
523 not the DWO file. */
b64f50a1 524 sect_offset offset;
36586728 525 unsigned int length;
ae038cb0
DJ
526
527 /* Flag indicating this compilation unit will be read in before
528 any of the current compilation units are processed. */
c764a876 529 unsigned int queued : 1;
ae038cb0 530
0d99eb77
DE
531 /* This flag will be set when reading partial DIEs if we need to load
532 absolutely all DIEs for this compilation unit, instead of just the ones
533 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
534 hash table and don't find it. */
535 unsigned int load_all_dies : 1;
536
3019eac3
DE
537 /* Non-zero if this CU is from .debug_types. */
538 unsigned int is_debug_types : 1;
539
36586728
TT
540 /* Non-zero if this CU is from the .dwz file. */
541 unsigned int is_dwz : 1;
542
3019eac3
DE
543 /* The section this CU/TU lives in.
544 If the DIE refers to a DWO file, this is always the original die,
545 not the DWO file. */
546 struct dwarf2_section_info *info_or_types_section;
348e048f 547
17ea53c3
JK
548 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
549 of the CU cache it gets reset to NULL again. */
ae038cb0 550 struct dwarf2_cu *cu;
1c379e20 551
9cdd5dbd
DE
552 /* The corresponding objfile.
553 Normally we can get the objfile from dwarf2_per_objfile.
554 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
555 struct objfile *objfile;
556
557 /* When using partial symbol tables, the 'psymtab' field is active.
558 Otherwise the 'quick' field is active. */
559 union
560 {
561 /* The partial symbol table associated with this compilation unit,
95554aad 562 or NULL for unread partial units. */
9291a0cd
TT
563 struct partial_symtab *psymtab;
564
565 /* Data needed by the "quick" functions. */
566 struct dwarf2_per_cu_quick_data *quick;
567 } v;
95554aad 568
f4dc4d17
DE
569 union
570 {
571 /* The CUs we import using DW_TAG_imported_unit. This is filled in
572 while reading psymtabs, used to compute the psymtab dependencies,
573 and then cleared. Then it is filled in again while reading full
574 symbols, and only deleted when the objfile is destroyed. */
575 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
576
577 /* Type units are grouped by their DW_AT_stmt_list entry so that they
578 can share them. If this is a TU, this points to the containing
579 symtab. */
580 struct type_unit_group *type_unit_group;
581 } s;
ae038cb0
DJ
582};
583
348e048f
DE
584/* Entry in the signatured_types hash table. */
585
586struct signatured_type
587{
42e7ad6c
DE
588 /* The "per_cu" object of this type.
589 N.B.: This is the first member so that it's easy to convert pointers
590 between them. */
591 struct dwarf2_per_cu_data per_cu;
592
3019eac3 593 /* The type's signature. */
348e048f
DE
594 ULONGEST signature;
595
3019eac3
DE
596 /* Offset in the TU of the type's DIE, as read from the TU header.
597 If the definition lives in a DWO file, this value is unusable. */
598 cu_offset type_offset_in_tu;
599
600 /* Offset in the section of the type's DIE.
601 If the definition lives in a DWO file, this is the offset in the
602 .debug_types.dwo section.
603 The value is zero until the actual value is known.
604 Zero is otherwise not a valid section offset. */
605 sect_offset type_offset_in_section;
348e048f
DE
606};
607
094b34ac
DE
608/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
609 This includes type_unit_group and quick_file_names. */
610
611struct stmt_list_hash
612{
613 /* The DWO unit this table is from or NULL if there is none. */
614 struct dwo_unit *dwo_unit;
615
616 /* Offset in .debug_line or .debug_line.dwo. */
617 sect_offset line_offset;
618};
619
f4dc4d17
DE
620/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
621 an object of this type. */
622
623struct type_unit_group
624{
625 /* dwarf2read.c's main "handle" on the symtab.
626 To simplify things we create an artificial CU that "includes" all the
627 type units using this stmt_list so that the rest of the code still has
628 a "per_cu" handle on the symtab.
629 This PER_CU is recognized by having no section. */
630#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
631 struct dwarf2_per_cu_data per_cu;
632
633 union
634 {
635 /* The TUs that share this DW_AT_stmt_list entry.
636 This is added to while parsing type units to build partial symtabs,
637 and is deleted afterwards and not used again. */
638 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 639
094b34ac
DE
640 /* When reading the line table in "quick" functions, we need a real TU.
641 Any will do, we know they all share the same DW_AT_stmt_list entry.
642 For simplicity's sake, we pick the first one. */
643 struct dwarf2_per_cu_data *first_tu;
644 } t;
f4dc4d17
DE
645
646 /* The primary symtab.
094b34ac
DE
647 Type units in a group needn't all be defined in the same source file,
648 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
649 struct symtab *primary_symtab;
650
094b34ac
DE
651 /* The data used to construct the hash key. */
652 struct stmt_list_hash hash;
f4dc4d17
DE
653
654 /* The number of symtabs from the line header.
655 The value here must match line_header.num_file_names. */
656 unsigned int num_symtabs;
657
658 /* The symbol tables for this TU (obtained from the files listed in
659 DW_AT_stmt_list).
660 WARNING: The order of entries here must match the order of entries
661 in the line header. After the first TU using this type_unit_group, the
662 line header for the subsequent TUs is recreated from this. This is done
663 because we need to use the same symtabs for each TU using the same
664 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
665 there's no guarantee the line header doesn't have duplicate entries. */
666 struct symtab **symtabs;
667};
668
80626a55 669/* These sections are what may appear in a DWO file. */
3019eac3
DE
670
671struct dwo_sections
672{
673 struct dwarf2_section_info abbrev;
3019eac3
DE
674 struct dwarf2_section_info line;
675 struct dwarf2_section_info loc;
09262596
DE
676 struct dwarf2_section_info macinfo;
677 struct dwarf2_section_info macro;
3019eac3
DE
678 struct dwarf2_section_info str;
679 struct dwarf2_section_info str_offsets;
80626a55
DE
680 /* In the case of a virtual DWO file, these two are unused. */
681 struct dwarf2_section_info info;
3019eac3
DE
682 VEC (dwarf2_section_info_def) *types;
683};
684
685/* Common bits of DWO CUs/TUs. */
686
687struct dwo_unit
688{
689 /* Backlink to the containing struct dwo_file. */
690 struct dwo_file *dwo_file;
691
692 /* The "id" that distinguishes this CU/TU.
693 .debug_info calls this "dwo_id", .debug_types calls this "signature".
694 Since signatures came first, we stick with it for consistency. */
695 ULONGEST signature;
696
697 /* The section this CU/TU lives in, in the DWO file. */
698 struct dwarf2_section_info *info_or_types_section;
699
700 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
701 sect_offset offset;
702 unsigned int length;
703
704 /* For types, offset in the type's DIE of the type defined by this TU. */
705 cu_offset type_offset_in_tu;
706};
707
80626a55
DE
708/* Data for one DWO file.
709 This includes virtual DWO files that have been packaged into a
710 DWP file. */
3019eac3
DE
711
712struct dwo_file
713{
80626a55
DE
714 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
715 For virtual DWO files the name is constructed from the section offsets
716 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
717 from related CU+TUs. */
718 const char *name;
3019eac3 719
80626a55
DE
720 /* The bfd, when the file is open. Otherwise this is NULL.
721 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
722 bfd *dbfd;
3019eac3
DE
723
724 /* Section info for this file. */
725 struct dwo_sections sections;
726
727 /* Table of CUs in the file.
728 Each element is a struct dwo_unit. */
729 htab_t cus;
730
731 /* Table of TUs in the file.
732 Each element is a struct dwo_unit. */
733 htab_t tus;
734};
735
80626a55
DE
736/* These sections are what may appear in a DWP file. */
737
738struct dwp_sections
739{
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
744 by section number. We don't need to record them here. */
745};
746
747/* These sections are what may appear in a virtual DWO file. */
748
749struct virtual_dwo_sections
750{
751 struct dwarf2_section_info abbrev;
752 struct dwarf2_section_info line;
753 struct dwarf2_section_info loc;
754 struct dwarf2_section_info macinfo;
755 struct dwarf2_section_info macro;
756 struct dwarf2_section_info str_offsets;
757 /* Each DWP hash table entry records one CU or one TU.
758 That is recorded here, and copied to dwo_unit.info_or_types_section. */
759 struct dwarf2_section_info info_or_types;
760};
761
762/* Contents of DWP hash tables. */
763
764struct dwp_hash_table
765{
766 uint32_t nr_units, nr_slots;
767 const gdb_byte *hash_table, *unit_table, *section_pool;
768};
769
770/* Data for one DWP file. */
771
772struct dwp_file
773{
774 /* Name of the file. */
775 const char *name;
776
777 /* The bfd, when the file is open. Otherwise this is NULL. */
778 bfd *dbfd;
779
780 /* Section info for this file. */
781 struct dwp_sections sections;
782
783 /* Table of CUs in the file. */
784 const struct dwp_hash_table *cus;
785
786 /* Table of TUs in the file. */
787 const struct dwp_hash_table *tus;
788
789 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
790 htab_t loaded_cutus;
791
792 /* Table to map ELF section numbers to their sections. */
793 unsigned int num_sections;
794 asection **elf_sections;
795};
796
36586728
TT
797/* This represents a '.dwz' file. */
798
799struct dwz_file
800{
801 /* A dwz file can only contain a few sections. */
802 struct dwarf2_section_info abbrev;
803 struct dwarf2_section_info info;
804 struct dwarf2_section_info str;
805 struct dwarf2_section_info line;
806 struct dwarf2_section_info macro;
2ec9a5e0 807 struct dwarf2_section_info gdb_index;
36586728
TT
808
809 /* The dwz's BFD. */
810 bfd *dwz_bfd;
811};
812
0963b4bd
MS
813/* Struct used to pass misc. parameters to read_die_and_children, et
814 al. which are used for both .debug_info and .debug_types dies.
815 All parameters here are unchanging for the life of the call. This
dee91e82 816 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
817
818struct die_reader_specs
819{
dee91e82 820 /* die_section->asection->owner. */
93311388
DE
821 bfd* abfd;
822
823 /* The CU of the DIE we are parsing. */
824 struct dwarf2_cu *cu;
825
80626a55 826 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
827 struct dwo_file *dwo_file;
828
dee91e82 829 /* The section the die comes from.
3019eac3 830 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
831 struct dwarf2_section_info *die_section;
832
833 /* die_section->buffer. */
834 gdb_byte *buffer;
f664829e
DE
835
836 /* The end of the buffer. */
837 const gdb_byte *buffer_end;
93311388
DE
838};
839
fd820528 840/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
841typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
842 gdb_byte *info_ptr,
843 struct die_info *comp_unit_die,
844 int has_children,
845 void *data);
846
debd256d
JB
847/* The line number information for a compilation unit (found in the
848 .debug_line section) begins with a "statement program header",
849 which contains the following information. */
850struct line_header
851{
852 unsigned int total_length;
853 unsigned short version;
854 unsigned int header_length;
855 unsigned char minimum_instruction_length;
2dc7f7b3 856 unsigned char maximum_ops_per_instruction;
debd256d
JB
857 unsigned char default_is_stmt;
858 int line_base;
859 unsigned char line_range;
860 unsigned char opcode_base;
861
862 /* standard_opcode_lengths[i] is the number of operands for the
863 standard opcode whose value is i. This means that
864 standard_opcode_lengths[0] is unused, and the last meaningful
865 element is standard_opcode_lengths[opcode_base - 1]. */
866 unsigned char *standard_opcode_lengths;
867
868 /* The include_directories table. NOTE! These strings are not
869 allocated with xmalloc; instead, they are pointers into
870 debug_line_buffer. If you try to free them, `free' will get
871 indigestion. */
872 unsigned int num_include_dirs, include_dirs_size;
873 char **include_dirs;
874
875 /* The file_names table. NOTE! These strings are not allocated
876 with xmalloc; instead, they are pointers into debug_line_buffer.
877 Don't try to free them directly. */
878 unsigned int num_file_names, file_names_size;
879 struct file_entry
c906108c 880 {
debd256d
JB
881 char *name;
882 unsigned int dir_index;
883 unsigned int mod_time;
884 unsigned int length;
aaa75496 885 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 886 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
887 } *file_names;
888
889 /* The start and end of the statement program following this
6502dd73 890 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 891 gdb_byte *statement_program_start, *statement_program_end;
debd256d 892};
c906108c
SS
893
894/* When we construct a partial symbol table entry we only
0963b4bd 895 need this much information. */
c906108c
SS
896struct partial_die_info
897 {
72bf9492 898 /* Offset of this DIE. */
b64f50a1 899 sect_offset offset;
72bf9492
DJ
900
901 /* DWARF-2 tag for this DIE. */
902 ENUM_BITFIELD(dwarf_tag) tag : 16;
903
72bf9492
DJ
904 /* Assorted flags describing the data found in this DIE. */
905 unsigned int has_children : 1;
906 unsigned int is_external : 1;
907 unsigned int is_declaration : 1;
908 unsigned int has_type : 1;
909 unsigned int has_specification : 1;
910 unsigned int has_pc_info : 1;
481860b3 911 unsigned int may_be_inlined : 1;
72bf9492
DJ
912
913 /* Flag set if the SCOPE field of this structure has been
914 computed. */
915 unsigned int scope_set : 1;
916
fa4028e9
JB
917 /* Flag set if the DIE has a byte_size attribute. */
918 unsigned int has_byte_size : 1;
919
98bfdba5
PA
920 /* Flag set if any of the DIE's children are template arguments. */
921 unsigned int has_template_arguments : 1;
922
abc72ce4
DE
923 /* Flag set if fixup_partial_die has been called on this die. */
924 unsigned int fixup_called : 1;
925
36586728
TT
926 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
927 unsigned int is_dwz : 1;
928
929 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
930 unsigned int spec_is_dwz : 1;
931
72bf9492 932 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 933 sometimes a default name for unnamed DIEs. */
c906108c 934 char *name;
72bf9492 935
abc72ce4
DE
936 /* The linkage name, if present. */
937 const char *linkage_name;
938
72bf9492
DJ
939 /* The scope to prepend to our children. This is generally
940 allocated on the comp_unit_obstack, so will disappear
941 when this compilation unit leaves the cache. */
942 char *scope;
943
95554aad
TT
944 /* Some data associated with the partial DIE. The tag determines
945 which field is live. */
946 union
947 {
948 /* The location description associated with this DIE, if any. */
949 struct dwarf_block *locdesc;
950 /* The offset of an import, for DW_TAG_imported_unit. */
951 sect_offset offset;
952 } d;
72bf9492
DJ
953
954 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
955 CORE_ADDR lowpc;
956 CORE_ADDR highpc;
72bf9492 957
93311388 958 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 959 DW_AT_sibling, if any. */
abc72ce4
DE
960 /* NOTE: This member isn't strictly necessary, read_partial_die could
961 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 962 gdb_byte *sibling;
72bf9492
DJ
963
964 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
965 DW_AT_specification (or DW_AT_abstract_origin or
966 DW_AT_extension). */
b64f50a1 967 sect_offset spec_offset;
72bf9492
DJ
968
969 /* Pointers to this DIE's parent, first child, and next sibling,
970 if any. */
971 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
972 };
973
0963b4bd 974/* This data structure holds the information of an abbrev. */
c906108c
SS
975struct abbrev_info
976 {
977 unsigned int number; /* number identifying abbrev */
978 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
979 unsigned short has_children; /* boolean */
980 unsigned short num_attrs; /* number of attributes */
c906108c
SS
981 struct attr_abbrev *attrs; /* an array of attribute descriptions */
982 struct abbrev_info *next; /* next in chain */
983 };
984
985struct attr_abbrev
986 {
9d25dd43
DE
987 ENUM_BITFIELD(dwarf_attribute) name : 16;
988 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
989 };
990
433df2d4
DE
991/* Size of abbrev_table.abbrev_hash_table. */
992#define ABBREV_HASH_SIZE 121
993
994/* Top level data structure to contain an abbreviation table. */
995
996struct abbrev_table
997{
f4dc4d17
DE
998 /* Where the abbrev table came from.
999 This is used as a sanity check when the table is used. */
433df2d4
DE
1000 sect_offset offset;
1001
1002 /* Storage for the abbrev table. */
1003 struct obstack abbrev_obstack;
1004
1005 /* Hash table of abbrevs.
1006 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1007 It could be statically allocated, but the previous code didn't so we
1008 don't either. */
1009 struct abbrev_info **abbrevs;
1010};
1011
0963b4bd 1012/* Attributes have a name and a value. */
b60c80d6
DJ
1013struct attribute
1014 {
9d25dd43 1015 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1016 ENUM_BITFIELD(dwarf_form) form : 15;
1017
1018 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1019 field should be in u.str (existing only for DW_STRING) but it is kept
1020 here for better struct attribute alignment. */
1021 unsigned int string_is_canonical : 1;
1022
b60c80d6
DJ
1023 union
1024 {
1025 char *str;
1026 struct dwarf_block *blk;
43bbcdc2
PH
1027 ULONGEST unsnd;
1028 LONGEST snd;
b60c80d6 1029 CORE_ADDR addr;
348e048f 1030 struct signatured_type *signatured_type;
b60c80d6
DJ
1031 }
1032 u;
1033 };
1034
0963b4bd 1035/* This data structure holds a complete die structure. */
c906108c
SS
1036struct die_info
1037 {
76815b17
DE
1038 /* DWARF-2 tag for this DIE. */
1039 ENUM_BITFIELD(dwarf_tag) tag : 16;
1040
1041 /* Number of attributes */
98bfdba5
PA
1042 unsigned char num_attrs;
1043
1044 /* True if we're presently building the full type name for the
1045 type derived from this DIE. */
1046 unsigned char building_fullname : 1;
76815b17
DE
1047
1048 /* Abbrev number */
1049 unsigned int abbrev;
1050
93311388 1051 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1052 sect_offset offset;
78ba4af6
JB
1053
1054 /* The dies in a compilation unit form an n-ary tree. PARENT
1055 points to this die's parent; CHILD points to the first child of
1056 this node; and all the children of a given node are chained
4950bc1c 1057 together via their SIBLING fields. */
639d11d3
DC
1058 struct die_info *child; /* Its first child, if any. */
1059 struct die_info *sibling; /* Its next sibling, if any. */
1060 struct die_info *parent; /* Its parent, if any. */
c906108c 1061
b60c80d6
DJ
1062 /* An array of attributes, with NUM_ATTRS elements. There may be
1063 zero, but it's not common and zero-sized arrays are not
1064 sufficiently portable C. */
1065 struct attribute attrs[1];
c906108c
SS
1066 };
1067
0963b4bd 1068/* Get at parts of an attribute structure. */
c906108c
SS
1069
1070#define DW_STRING(attr) ((attr)->u.str)
8285870a 1071#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1072#define DW_UNSND(attr) ((attr)->u.unsnd)
1073#define DW_BLOCK(attr) ((attr)->u.blk)
1074#define DW_SND(attr) ((attr)->u.snd)
1075#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1076#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1077
0963b4bd 1078/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1079struct dwarf_block
1080 {
56eb65bd 1081 size_t size;
1d6edc3c
JK
1082
1083 /* Valid only if SIZE is not zero. */
fe1b8b76 1084 gdb_byte *data;
c906108c
SS
1085 };
1086
c906108c
SS
1087#ifndef ATTR_ALLOC_CHUNK
1088#define ATTR_ALLOC_CHUNK 4
1089#endif
1090
c906108c
SS
1091/* Allocate fields for structs, unions and enums in this size. */
1092#ifndef DW_FIELD_ALLOC_CHUNK
1093#define DW_FIELD_ALLOC_CHUNK 4
1094#endif
1095
c906108c
SS
1096/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099static int bits_per_byte = 8;
1100
1101/* The routines that read and process dies for a C struct or C++ class
1102 pass lists of data member fields and lists of member function fields
1103 in an instance of a field_info structure, as defined below. */
1104struct field_info
c5aa993b 1105 {
0963b4bd 1106 /* List of data member and baseclasses fields. */
c5aa993b
JM
1107 struct nextfield
1108 {
1109 struct nextfield *next;
1110 int accessibility;
1111 int virtuality;
1112 struct field field;
1113 }
7d0ccb61 1114 *fields, *baseclasses;
c906108c 1115
7d0ccb61 1116 /* Number of fields (including baseclasses). */
c5aa993b 1117 int nfields;
c906108c 1118
c5aa993b
JM
1119 /* Number of baseclasses. */
1120 int nbaseclasses;
c906108c 1121
c5aa993b
JM
1122 /* Set if the accesibility of one of the fields is not public. */
1123 int non_public_fields;
c906108c 1124
c5aa993b
JM
1125 /* Member function fields array, entries are allocated in the order they
1126 are encountered in the object file. */
1127 struct nextfnfield
1128 {
1129 struct nextfnfield *next;
1130 struct fn_field fnfield;
1131 }
1132 *fnfields;
c906108c 1133
c5aa993b
JM
1134 /* Member function fieldlist array, contains name of possibly overloaded
1135 member function, number of overloaded member functions and a pointer
1136 to the head of the member function field chain. */
1137 struct fnfieldlist
1138 {
1139 char *name;
1140 int length;
1141 struct nextfnfield *head;
1142 }
1143 *fnfieldlists;
c906108c 1144
c5aa993b
JM
1145 /* Number of entries in the fnfieldlists array. */
1146 int nfnfields;
98751a41
JK
1147
1148 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1149 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1150 struct typedef_field_list
1151 {
1152 struct typedef_field field;
1153 struct typedef_field_list *next;
1154 }
1155 *typedef_field_list;
1156 unsigned typedef_field_list_count;
c5aa993b 1157 };
c906108c 1158
10b3939b
DJ
1159/* One item on the queue of compilation units to read in full symbols
1160 for. */
1161struct dwarf2_queue_item
1162{
1163 struct dwarf2_per_cu_data *per_cu;
95554aad 1164 enum language pretend_language;
10b3939b
DJ
1165 struct dwarf2_queue_item *next;
1166};
1167
1168/* The current queue. */
1169static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1170
ae038cb0
DJ
1171/* Loaded secondary compilation units are kept in memory until they
1172 have not been referenced for the processing of this many
1173 compilation units. Set this to zero to disable caching. Cache
1174 sizes of up to at least twenty will improve startup time for
1175 typical inter-CU-reference binaries, at an obvious memory cost. */
1176static int dwarf2_max_cache_age = 5;
920d2a44
AC
1177static void
1178show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1179 struct cmd_list_element *c, const char *value)
1180{
3e43a32a
MS
1181 fprintf_filtered (file, _("The upper bound on the age of cached "
1182 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1183 value);
1184}
1185
ae038cb0 1186
0963b4bd 1187/* Various complaints about symbol reading that don't abort the process. */
c906108c 1188
4d3c2250
KB
1189static void
1190dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1191{
4d3c2250 1192 complaint (&symfile_complaints,
e2e0b3e5 1193 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1194}
1195
25e43795
DJ
1196static void
1197dwarf2_debug_line_missing_file_complaint (void)
1198{
1199 complaint (&symfile_complaints,
1200 _(".debug_line section has line data without a file"));
1201}
1202
59205f5a
JB
1203static void
1204dwarf2_debug_line_missing_end_sequence_complaint (void)
1205{
1206 complaint (&symfile_complaints,
3e43a32a
MS
1207 _(".debug_line section has line "
1208 "program sequence without an end"));
59205f5a
JB
1209}
1210
4d3c2250
KB
1211static void
1212dwarf2_complex_location_expr_complaint (void)
2e276125 1213{
e2e0b3e5 1214 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1215}
1216
4d3c2250
KB
1217static void
1218dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1219 int arg3)
2e276125 1220{
4d3c2250 1221 complaint (&symfile_complaints,
3e43a32a
MS
1222 _("const value length mismatch for '%s', got %d, expected %d"),
1223 arg1, arg2, arg3);
4d3c2250
KB
1224}
1225
1226static void
f664829e 1227dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1228{
4d3c2250 1229 complaint (&symfile_complaints,
f664829e
DE
1230 _("debug info runs off end of %s section"
1231 " [in module %s]"),
1232 section->asection->name,
1233 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1234}
1235
1236static void
1237dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1238{
4d3c2250 1239 complaint (&symfile_complaints,
3e43a32a
MS
1240 _("macro debug info contains a "
1241 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1242 arg1);
1243}
1244
1245static void
1246dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1247{
4d3c2250 1248 complaint (&symfile_complaints,
3e43a32a
MS
1249 _("invalid attribute class or form for '%s' in '%s'"),
1250 arg1, arg2);
4d3c2250 1251}
c906108c 1252
c906108c
SS
1253/* local function prototypes */
1254
4efb68b1 1255static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1256
aaa75496
JB
1257static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1258 struct objfile *);
1259
918dd910
JK
1260static void dwarf2_find_base_address (struct die_info *die,
1261 struct dwarf2_cu *cu);
1262
c67a9c90 1263static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1264
72bf9492
DJ
1265static void scan_partial_symbols (struct partial_die_info *,
1266 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1267 int, struct dwarf2_cu *);
c906108c 1268
72bf9492
DJ
1269static void add_partial_symbol (struct partial_die_info *,
1270 struct dwarf2_cu *);
63d06c5c 1271
72bf9492
DJ
1272static void add_partial_namespace (struct partial_die_info *pdi,
1273 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1274 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1275
5d7cb8df
JK
1276static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1277 CORE_ADDR *highpc, int need_pc,
1278 struct dwarf2_cu *cu);
1279
72bf9492
DJ
1280static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1281 struct dwarf2_cu *cu);
91c24f0a 1282
bc30ff58
JB
1283static void add_partial_subprogram (struct partial_die_info *pdi,
1284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1285 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1286
5c80ed9d
TT
1287static void dwarf2_psymtab_to_symtab (struct objfile *,
1288 struct partial_symtab *);
c906108c 1289
a14ed312 1290static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1291
433df2d4
DE
1292static struct abbrev_info *abbrev_table_lookup_abbrev
1293 (const struct abbrev_table *, unsigned int);
1294
1295static struct abbrev_table *abbrev_table_read_table
1296 (struct dwarf2_section_info *, sect_offset);
1297
1298static void abbrev_table_free (struct abbrev_table *);
1299
f4dc4d17
DE
1300static void abbrev_table_free_cleanup (void *);
1301
dee91e82
DE
1302static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1303 struct dwarf2_section_info *);
c906108c 1304
f3dd6933 1305static void dwarf2_free_abbrev_table (void *);
c906108c 1306
6caca83c
CC
1307static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1308
dee91e82
DE
1309static struct partial_die_info *load_partial_dies
1310 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1311
dee91e82
DE
1312static gdb_byte *read_partial_die (const struct die_reader_specs *,
1313 struct partial_die_info *,
1314 struct abbrev_info *,
1315 unsigned int,
1316 gdb_byte *);
c906108c 1317
36586728 1318static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1319 struct dwarf2_cu *);
72bf9492
DJ
1320
1321static void fixup_partial_die (struct partial_die_info *,
1322 struct dwarf2_cu *);
1323
dee91e82
DE
1324static gdb_byte *read_attribute (const struct die_reader_specs *,
1325 struct attribute *, struct attr_abbrev *,
1326 gdb_byte *);
a8329558 1327
a1855c1d 1328static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1329
a1855c1d 1330static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1331
a1855c1d 1332static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1333
a1855c1d 1334static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1335
a1855c1d 1336static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1337
fe1b8b76 1338static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1339 unsigned int *);
c906108c 1340
c764a876
DE
1341static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1342
1343static LONGEST read_checked_initial_length_and_offset
1344 (bfd *, gdb_byte *, const struct comp_unit_head *,
1345 unsigned int *, unsigned int *);
613e1657 1346
fe1b8b76 1347static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1348 unsigned int *);
1349
1350static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1351
f4dc4d17
DE
1352static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1353 sect_offset);
1354
fe1b8b76 1355static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1356
9b1c24c8 1357static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1358
fe1b8b76
JB
1359static char *read_indirect_string (bfd *, gdb_byte *,
1360 const struct comp_unit_head *,
1361 unsigned int *);
4bdf3d34 1362
36586728
TT
1363static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1364
12df843f 1365static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1366
12df843f 1367static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1368
3019eac3
DE
1369static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1370 unsigned int *);
1371
1372static char *read_str_index (const struct die_reader_specs *reader,
1373 struct dwarf2_cu *cu, ULONGEST str_index);
1374
e142c38c 1375static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1376
e142c38c
DJ
1377static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1378 struct dwarf2_cu *);
c906108c 1379
348e048f 1380static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1381 unsigned int);
348e048f 1382
05cf31d1
JB
1383static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
e142c38c 1386static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1387
e142c38c 1388static struct die_info *die_specification (struct die_info *die,
f2f0e013 1389 struct dwarf2_cu **);
63d06c5c 1390
debd256d
JB
1391static void free_line_header (struct line_header *lh);
1392
aaa75496
JB
1393static void add_file_name (struct line_header *, char *, unsigned int,
1394 unsigned int, unsigned int);
1395
3019eac3
DE
1396static struct line_header *dwarf_decode_line_header (unsigned int offset,
1397 struct dwarf2_cu *cu);
debd256d 1398
f3f5162e
DE
1399static void dwarf_decode_lines (struct line_header *, const char *,
1400 struct dwarf2_cu *, struct partial_symtab *,
1401 int);
c906108c 1402
72b9f47f 1403static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1404
f4dc4d17
DE
1405static void dwarf2_start_symtab (struct dwarf2_cu *,
1406 char *, char *, CORE_ADDR);
1407
a14ed312 1408static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1409 struct dwarf2_cu *);
c906108c 1410
34eaf542
TT
1411static struct symbol *new_symbol_full (struct die_info *, struct type *,
1412 struct dwarf2_cu *, struct symbol *);
1413
a14ed312 1414static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1415 struct dwarf2_cu *);
c906108c 1416
98bfdba5
PA
1417static void dwarf2_const_value_attr (struct attribute *attr,
1418 struct type *type,
1419 const char *name,
1420 struct obstack *obstack,
12df843f 1421 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1422 gdb_byte **bytes,
1423 struct dwarf2_locexpr_baton **baton);
2df3850c 1424
e7c27a73 1425static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1426
b4ba55a1
JB
1427static int need_gnat_info (struct dwarf2_cu *);
1428
3e43a32a
MS
1429static struct type *die_descriptive_type (struct die_info *,
1430 struct dwarf2_cu *);
b4ba55a1
JB
1431
1432static void set_descriptive_type (struct type *, struct die_info *,
1433 struct dwarf2_cu *);
1434
e7c27a73
DJ
1435static struct type *die_containing_type (struct die_info *,
1436 struct dwarf2_cu *);
c906108c 1437
673bfd45
DE
1438static struct type *lookup_die_type (struct die_info *, struct attribute *,
1439 struct dwarf2_cu *);
c906108c 1440
f792889a 1441static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1442
673bfd45
DE
1443static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1444
0d5cff50 1445static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1446
6e70227d 1447static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1448 const char *suffix, int physname,
1449 struct dwarf2_cu *cu);
63d06c5c 1450
e7c27a73 1451static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1452
348e048f
DE
1453static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1454
e7c27a73 1455static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1456
e7c27a73 1457static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1458
96408a79
SA
1459static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1460
ff013f42
JK
1461static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1462 struct dwarf2_cu *, struct partial_symtab *);
1463
a14ed312 1464static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1465 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1466 struct partial_symtab *);
c906108c 1467
fae299cd
DC
1468static void get_scope_pc_bounds (struct die_info *,
1469 CORE_ADDR *, CORE_ADDR *,
1470 struct dwarf2_cu *);
1471
801e3a5b
JB
1472static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1473 CORE_ADDR, struct dwarf2_cu *);
1474
a14ed312 1475static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1476 struct dwarf2_cu *);
c906108c 1477
a14ed312 1478static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1479 struct type *, struct dwarf2_cu *);
c906108c 1480
a14ed312 1481static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1482 struct die_info *, struct type *,
e7c27a73 1483 struct dwarf2_cu *);
c906108c 1484
a14ed312 1485static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1486 struct type *,
1487 struct dwarf2_cu *);
c906108c 1488
134d01f1 1489static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1490
e7c27a73 1491static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1492
e7c27a73 1493static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1494
5d7cb8df
JK
1495static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1496
27aa8d6a
SW
1497static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1498
f55ee35c
JK
1499static struct type *read_module_type (struct die_info *die,
1500 struct dwarf2_cu *cu);
1501
38d518c9 1502static const char *namespace_name (struct die_info *die,
e142c38c 1503 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1504
134d01f1 1505static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1506
e7c27a73 1507static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1508
6e70227d 1509static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1510 struct dwarf2_cu *);
1511
dee91e82 1512static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1513 gdb_byte *info_ptr,
fe1b8b76 1514 gdb_byte **new_info_ptr,
639d11d3
DC
1515 struct die_info *parent);
1516
dee91e82 1517static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1518 gdb_byte *info_ptr,
fe1b8b76 1519 gdb_byte **new_info_ptr,
639d11d3
DC
1520 struct die_info *parent);
1521
3019eac3
DE
1522static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1523 struct die_info **, gdb_byte *, int *, int);
1524
dee91e82
DE
1525static gdb_byte *read_full_die (const struct die_reader_specs *,
1526 struct die_info **, gdb_byte *, int *);
93311388 1527
e7c27a73 1528static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1529
71c25dea
TT
1530static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1531 struct obstack *);
1532
e142c38c 1533static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1534
98bfdba5
PA
1535static const char *dwarf2_full_name (char *name,
1536 struct die_info *die,
1537 struct dwarf2_cu *cu);
1538
e142c38c 1539static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1540 struct dwarf2_cu **);
9219021c 1541
f39c6ffd 1542static const char *dwarf_tag_name (unsigned int);
c906108c 1543
f39c6ffd 1544static const char *dwarf_attr_name (unsigned int);
c906108c 1545
f39c6ffd 1546static const char *dwarf_form_name (unsigned int);
c906108c 1547
a14ed312 1548static char *dwarf_bool_name (unsigned int);
c906108c 1549
f39c6ffd 1550static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1551
f9aca02d 1552static struct die_info *sibling_die (struct die_info *);
c906108c 1553
d97bc12b
DE
1554static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1555
1556static void dump_die_for_error (struct die_info *);
1557
1558static void dump_die_1 (struct ui_file *, int level, int max_level,
1559 struct die_info *);
c906108c 1560
d97bc12b 1561/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1562
51545339 1563static void store_in_ref_table (struct die_info *,
10b3939b 1564 struct dwarf2_cu *);
c906108c 1565
93311388
DE
1566static int is_ref_attr (struct attribute *);
1567
b64f50a1 1568static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1569
43bbcdc2 1570static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1571
348e048f
DE
1572static struct die_info *follow_die_ref_or_sig (struct die_info *,
1573 struct attribute *,
1574 struct dwarf2_cu **);
1575
10b3939b
DJ
1576static struct die_info *follow_die_ref (struct die_info *,
1577 struct attribute *,
f2f0e013 1578 struct dwarf2_cu **);
c906108c 1579
348e048f
DE
1580static struct die_info *follow_die_sig (struct die_info *,
1581 struct attribute *,
1582 struct dwarf2_cu **);
1583
6c83ed52
TT
1584static struct signatured_type *lookup_signatured_type_at_offset
1585 (struct objfile *objfile,
b64f50a1 1586 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1587
e5fe5e75 1588static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1589
52dc124a 1590static void read_signatured_type (struct signatured_type *);
348e048f 1591
f4dc4d17 1592static struct type_unit_group *get_type_unit_group
094b34ac 1593 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1594
1595static void build_type_unit_groups (die_reader_func_ftype *, void *);
1596
c906108c
SS
1597/* memory allocation interface */
1598
7b5a2f43 1599static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1600
b60c80d6 1601static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1602
09262596
DE
1603static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1604 char *, int);
2e276125 1605
8e19ed76
PS
1606static int attr_form_is_block (struct attribute *);
1607
3690dd37
JB
1608static int attr_form_is_section_offset (struct attribute *);
1609
1610static int attr_form_is_constant (struct attribute *);
1611
8cf6f0b1
TT
1612static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1613 struct dwarf2_loclist_baton *baton,
1614 struct attribute *attr);
1615
93e7bd98
DJ
1616static void dwarf2_symbol_mark_computed (struct attribute *attr,
1617 struct symbol *sym,
1618 struct dwarf2_cu *cu);
4c2df51b 1619
dee91e82
DE
1620static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1621 gdb_byte *info_ptr,
1622 struct abbrev_info *abbrev);
4bb7a0a7 1623
72bf9492
DJ
1624static void free_stack_comp_unit (void *);
1625
72bf9492
DJ
1626static hashval_t partial_die_hash (const void *item);
1627
1628static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1629
ae038cb0 1630static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1631 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1632
9816fde3 1633static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1634 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1635
1636static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1637 struct die_info *comp_unit_die,
1638 enum language pretend_language);
93311388 1639
68dc6402 1640static void free_heap_comp_unit (void *);
ae038cb0
DJ
1641
1642static void free_cached_comp_units (void *);
1643
1644static void age_cached_comp_units (void);
1645
dee91e82 1646static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1647
f792889a
DJ
1648static struct type *set_die_type (struct die_info *, struct type *,
1649 struct dwarf2_cu *);
1c379e20 1650
ae038cb0
DJ
1651static void create_all_comp_units (struct objfile *);
1652
0e50663e 1653static int create_all_type_units (struct objfile *);
1fd400ff 1654
95554aad
TT
1655static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1656 enum language);
10b3939b 1657
95554aad
TT
1658static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1659 enum language);
10b3939b 1660
f4dc4d17
DE
1661static void process_full_type_unit (struct dwarf2_per_cu_data *,
1662 enum language);
1663
10b3939b
DJ
1664static void dwarf2_add_dependence (struct dwarf2_cu *,
1665 struct dwarf2_per_cu_data *);
1666
ae038cb0
DJ
1667static void dwarf2_mark (struct dwarf2_cu *);
1668
1669static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1670
b64f50a1 1671static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1672 struct dwarf2_per_cu_data *per_cu);
1673
f792889a 1674static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1675
9291a0cd
TT
1676static void dwarf2_release_queue (void *dummy);
1677
95554aad
TT
1678static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1679 enum language pretend_language);
1680
1681static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1682 struct dwarf2_per_cu_data *per_cu,
1683 enum language pretend_language);
9291a0cd 1684
a0f42c21 1685static void process_queue (void);
9291a0cd
TT
1686
1687static void find_file_and_directory (struct die_info *die,
1688 struct dwarf2_cu *cu,
1689 char **name, char **comp_dir);
1690
1691static char *file_full_name (int file, struct line_header *lh,
1692 const char *comp_dir);
1693
36586728
TT
1694static gdb_byte *read_and_check_comp_unit_head
1695 (struct comp_unit_head *header,
1696 struct dwarf2_section_info *section,
1697 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1698 int is_debug_types_section);
1699
fd820528 1700static void init_cutu_and_read_dies
f4dc4d17
DE
1701 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1702 int use_existing_cu, int keep,
3019eac3
DE
1703 die_reader_func_ftype *die_reader_func, void *data);
1704
dee91e82
DE
1705static void init_cutu_and_read_dies_simple
1706 (struct dwarf2_per_cu_data *this_cu,
1707 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1708
673bfd45 1709static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1710
3019eac3
DE
1711static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1712
1713static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1714 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1715
1716static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1717 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1718
1719static void free_dwo_file_cleanup (void *);
1720
95554aad
TT
1721static void process_cu_includes (void);
1722
1b80a9fa
JK
1723static void check_producer (struct dwarf2_cu *cu);
1724
9291a0cd
TT
1725#if WORDS_BIGENDIAN
1726
1727/* Convert VALUE between big- and little-endian. */
1728static offset_type
1729byte_swap (offset_type value)
1730{
1731 offset_type result;
1732
1733 result = (value & 0xff) << 24;
1734 result |= (value & 0xff00) << 8;
1735 result |= (value & 0xff0000) >> 8;
1736 result |= (value & 0xff000000) >> 24;
1737 return result;
1738}
1739
1740#define MAYBE_SWAP(V) byte_swap (V)
1741
1742#else
1743#define MAYBE_SWAP(V) (V)
1744#endif /* WORDS_BIGENDIAN */
1745
1746/* The suffix for an index file. */
1747#define INDEX_SUFFIX ".gdb-index"
1748
3da10d80
KS
1749static const char *dwarf2_physname (char *name, struct die_info *die,
1750 struct dwarf2_cu *cu);
1751
c906108c 1752/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1753 information and return true if we have enough to do something.
1754 NAMES points to the dwarf2 section names, or is NULL if the standard
1755 ELF names are used. */
c906108c
SS
1756
1757int
251d32d9
TG
1758dwarf2_has_info (struct objfile *objfile,
1759 const struct dwarf2_debug_sections *names)
c906108c 1760{
be391dca
TT
1761 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1762 if (!dwarf2_per_objfile)
1763 {
1764 /* Initialize per-objfile state. */
1765 struct dwarf2_per_objfile *data
1766 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1767
be391dca
TT
1768 memset (data, 0, sizeof (*data));
1769 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1770 dwarf2_per_objfile = data;
6502dd73 1771
251d32d9
TG
1772 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1773 (void *) names);
be391dca
TT
1774 dwarf2_per_objfile->objfile = objfile;
1775 }
1776 return (dwarf2_per_objfile->info.asection != NULL
1777 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1778}
1779
251d32d9
TG
1780/* When loading sections, we look either for uncompressed section or for
1781 compressed section names. */
233a11ab
CS
1782
1783static int
251d32d9
TG
1784section_is_p (const char *section_name,
1785 const struct dwarf2_section_names *names)
233a11ab 1786{
251d32d9
TG
1787 if (names->normal != NULL
1788 && strcmp (section_name, names->normal) == 0)
1789 return 1;
1790 if (names->compressed != NULL
1791 && strcmp (section_name, names->compressed) == 0)
1792 return 1;
1793 return 0;
233a11ab
CS
1794}
1795
c906108c
SS
1796/* This function is mapped across the sections and remembers the
1797 offset and size of each of the debugging sections we are interested
1798 in. */
1799
1800static void
251d32d9 1801dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1802{
251d32d9 1803 const struct dwarf2_debug_sections *names;
dc7650b8 1804 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1805
1806 if (vnames == NULL)
1807 names = &dwarf2_elf_names;
1808 else
1809 names = (const struct dwarf2_debug_sections *) vnames;
1810
dc7650b8
JK
1811 if ((aflag & SEC_HAS_CONTENTS) == 0)
1812 {
1813 }
1814 else if (section_is_p (sectp->name, &names->info))
c906108c 1815 {
dce234bc
PP
1816 dwarf2_per_objfile->info.asection = sectp;
1817 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1818 }
251d32d9 1819 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1820 {
dce234bc
PP
1821 dwarf2_per_objfile->abbrev.asection = sectp;
1822 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1823 }
251d32d9 1824 else if (section_is_p (sectp->name, &names->line))
c906108c 1825 {
dce234bc
PP
1826 dwarf2_per_objfile->line.asection = sectp;
1827 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1828 }
251d32d9 1829 else if (section_is_p (sectp->name, &names->loc))
c906108c 1830 {
dce234bc
PP
1831 dwarf2_per_objfile->loc.asection = sectp;
1832 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1833 }
251d32d9 1834 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1835 {
dce234bc
PP
1836 dwarf2_per_objfile->macinfo.asection = sectp;
1837 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1838 }
cf2c3c16
TT
1839 else if (section_is_p (sectp->name, &names->macro))
1840 {
1841 dwarf2_per_objfile->macro.asection = sectp;
1842 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1843 }
251d32d9 1844 else if (section_is_p (sectp->name, &names->str))
c906108c 1845 {
dce234bc
PP
1846 dwarf2_per_objfile->str.asection = sectp;
1847 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1848 }
3019eac3
DE
1849 else if (section_is_p (sectp->name, &names->addr))
1850 {
1851 dwarf2_per_objfile->addr.asection = sectp;
1852 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1853 }
251d32d9 1854 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1855 {
dce234bc
PP
1856 dwarf2_per_objfile->frame.asection = sectp;
1857 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1858 }
251d32d9 1859 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1860 {
dc7650b8
JK
1861 dwarf2_per_objfile->eh_frame.asection = sectp;
1862 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1863 }
251d32d9 1864 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1865 {
dce234bc
PP
1866 dwarf2_per_objfile->ranges.asection = sectp;
1867 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1868 }
251d32d9 1869 else if (section_is_p (sectp->name, &names->types))
348e048f 1870 {
8b70b953
TT
1871 struct dwarf2_section_info type_section;
1872
1873 memset (&type_section, 0, sizeof (type_section));
1874 type_section.asection = sectp;
1875 type_section.size = bfd_get_section_size (sectp);
1876
1877 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1878 &type_section);
348e048f 1879 }
251d32d9 1880 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1881 {
1882 dwarf2_per_objfile->gdb_index.asection = sectp;
1883 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1884 }
dce234bc 1885
72dca2f5
FR
1886 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1887 && bfd_section_vma (abfd, sectp) == 0)
1888 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1889}
1890
fceca515
DE
1891/* A helper function that decides whether a section is empty,
1892 or not present. */
9e0ac564
TT
1893
1894static int
1895dwarf2_section_empty_p (struct dwarf2_section_info *info)
1896{
1897 return info->asection == NULL || info->size == 0;
1898}
1899
3019eac3
DE
1900/* Read the contents of the section INFO.
1901 OBJFILE is the main object file, but not necessarily the file where
1902 the section comes from. E.g., for DWO files INFO->asection->owner
1903 is the bfd of the DWO file.
dce234bc 1904 If the section is compressed, uncompress it before returning. */
c906108c 1905
dce234bc
PP
1906static void
1907dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1908{
dce234bc 1909 asection *sectp = info->asection;
3019eac3 1910 bfd *abfd;
dce234bc
PP
1911 gdb_byte *buf, *retbuf;
1912 unsigned char header[4];
c906108c 1913
be391dca
TT
1914 if (info->readin)
1915 return;
dce234bc 1916 info->buffer = NULL;
be391dca 1917 info->readin = 1;
188dd5d6 1918
9e0ac564 1919 if (dwarf2_section_empty_p (info))
dce234bc 1920 return;
c906108c 1921
3019eac3
DE
1922 abfd = sectp->owner;
1923
4bf44c1c
TT
1924 /* If the section has relocations, we must read it ourselves.
1925 Otherwise we attach it to the BFD. */
1926 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1927 {
4bf44c1c 1928 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1929
4bf44c1c
TT
1930 /* We have to cast away const here for historical reasons.
1931 Fixing dwarf2read to be const-correct would be quite nice. */
1932 info->buffer = (gdb_byte *) bytes;
1933 return;
dce234bc 1934 }
dce234bc 1935
4bf44c1c
TT
1936 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1937 info->buffer = buf;
dce234bc
PP
1938
1939 /* When debugging .o files, we may need to apply relocations; see
1940 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1941 We never compress sections in .o files, so we only need to
1942 try this when the section is not compressed. */
ac8035ab 1943 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1944 if (retbuf != NULL)
1945 {
1946 info->buffer = retbuf;
1947 return;
1948 }
1949
1950 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1951 || bfd_bread (buf, info->size, abfd) != info->size)
1952 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1953 bfd_get_filename (abfd));
1954}
1955
9e0ac564
TT
1956/* A helper function that returns the size of a section in a safe way.
1957 If you are positive that the section has been read before using the
1958 size, then it is safe to refer to the dwarf2_section_info object's
1959 "size" field directly. In other cases, you must call this
1960 function, because for compressed sections the size field is not set
1961 correctly until the section has been read. */
1962
1963static bfd_size_type
1964dwarf2_section_size (struct objfile *objfile,
1965 struct dwarf2_section_info *info)
1966{
1967 if (!info->readin)
1968 dwarf2_read_section (objfile, info);
1969 return info->size;
1970}
1971
dce234bc 1972/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1973 SECTION_NAME. */
af34e669 1974
dce234bc 1975void
3017a003
TG
1976dwarf2_get_section_info (struct objfile *objfile,
1977 enum dwarf2_section_enum sect,
dce234bc
PP
1978 asection **sectp, gdb_byte **bufp,
1979 bfd_size_type *sizep)
1980{
1981 struct dwarf2_per_objfile *data
1982 = objfile_data (objfile, dwarf2_objfile_data_key);
1983 struct dwarf2_section_info *info;
a3b2a86b
TT
1984
1985 /* We may see an objfile without any DWARF, in which case we just
1986 return nothing. */
1987 if (data == NULL)
1988 {
1989 *sectp = NULL;
1990 *bufp = NULL;
1991 *sizep = 0;
1992 return;
1993 }
3017a003
TG
1994 switch (sect)
1995 {
1996 case DWARF2_DEBUG_FRAME:
1997 info = &data->frame;
1998 break;
1999 case DWARF2_EH_FRAME:
2000 info = &data->eh_frame;
2001 break;
2002 default:
2003 gdb_assert_not_reached ("unexpected section");
2004 }
dce234bc 2005
9e0ac564 2006 dwarf2_read_section (objfile, info);
dce234bc
PP
2007
2008 *sectp = info->asection;
2009 *bufp = info->buffer;
2010 *sizep = info->size;
2011}
2012
36586728
TT
2013/* A helper function to find the sections for a .dwz file. */
2014
2015static void
2016locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2017{
2018 struct dwz_file *dwz_file = arg;
2019
2020 /* Note that we only support the standard ELF names, because .dwz
2021 is ELF-only (at the time of writing). */
2022 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2023 {
2024 dwz_file->abbrev.asection = sectp;
2025 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2026 }
2027 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2028 {
2029 dwz_file->info.asection = sectp;
2030 dwz_file->info.size = bfd_get_section_size (sectp);
2031 }
2032 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2033 {
2034 dwz_file->str.asection = sectp;
2035 dwz_file->str.size = bfd_get_section_size (sectp);
2036 }
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2038 {
2039 dwz_file->line.asection = sectp;
2040 dwz_file->line.size = bfd_get_section_size (sectp);
2041 }
2042 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2043 {
2044 dwz_file->macro.asection = sectp;
2045 dwz_file->macro.size = bfd_get_section_size (sectp);
2046 }
2ec9a5e0
TT
2047 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2048 {
2049 dwz_file->gdb_index.asection = sectp;
2050 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2051 }
36586728
TT
2052}
2053
2054/* Open the separate '.dwz' debug file, if needed. Error if the file
2055 cannot be found. */
2056
2057static struct dwz_file *
2058dwarf2_get_dwz_file (void)
2059{
2060 bfd *abfd, *dwz_bfd;
2061 asection *section;
2062 gdb_byte *data;
2063 struct cleanup *cleanup;
2064 const char *filename;
2065 struct dwz_file *result;
2066
2067 if (dwarf2_per_objfile->dwz_file != NULL)
2068 return dwarf2_per_objfile->dwz_file;
2069
2070 abfd = dwarf2_per_objfile->objfile->obfd;
2071 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2072 if (section == NULL)
2073 error (_("could not find '.gnu_debugaltlink' section"));
2074 if (!bfd_malloc_and_get_section (abfd, section, &data))
2075 error (_("could not read '.gnu_debugaltlink' section: %s"),
2076 bfd_errmsg (bfd_get_error ()));
2077 cleanup = make_cleanup (xfree, data);
2078
2079 filename = data;
2080 if (!IS_ABSOLUTE_PATH (filename))
2081 {
2082 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2083 char *rel;
2084
2085 make_cleanup (xfree, abs);
2086 abs = ldirname (abs);
2087 make_cleanup (xfree, abs);
2088
2089 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2090 make_cleanup (xfree, rel);
2091 filename = rel;
2092 }
2093
2094 /* The format is just a NUL-terminated file name, followed by the
2095 build-id. For now, though, we ignore the build-id. */
2096 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2097 if (dwz_bfd == NULL)
2098 error (_("could not read '%s': %s"), filename,
2099 bfd_errmsg (bfd_get_error ()));
2100
2101 if (!bfd_check_format (dwz_bfd, bfd_object))
2102 {
2103 gdb_bfd_unref (dwz_bfd);
2104 error (_("file '%s' was not usable: %s"), filename,
2105 bfd_errmsg (bfd_get_error ()));
2106 }
2107
2108 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2109 struct dwz_file);
2110 result->dwz_bfd = dwz_bfd;
2111
2112 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2113
2114 do_cleanups (cleanup);
2115
8d2cc612 2116 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2117 return result;
2118}
9291a0cd 2119\f
7b9f3c50
DE
2120/* DWARF quick_symbols_functions support. */
2121
2122/* TUs can share .debug_line entries, and there can be a lot more TUs than
2123 unique line tables, so we maintain a separate table of all .debug_line
2124 derived entries to support the sharing.
2125 All the quick functions need is the list of file names. We discard the
2126 line_header when we're done and don't need to record it here. */
2127struct quick_file_names
2128{
094b34ac
DE
2129 /* The data used to construct the hash key. */
2130 struct stmt_list_hash hash;
7b9f3c50
DE
2131
2132 /* The number of entries in file_names, real_names. */
2133 unsigned int num_file_names;
2134
2135 /* The file names from the line table, after being run through
2136 file_full_name. */
2137 const char **file_names;
2138
2139 /* The file names from the line table after being run through
2140 gdb_realpath. These are computed lazily. */
2141 const char **real_names;
2142};
2143
2144/* When using the index (and thus not using psymtabs), each CU has an
2145 object of this type. This is used to hold information needed by
2146 the various "quick" methods. */
2147struct dwarf2_per_cu_quick_data
2148{
2149 /* The file table. This can be NULL if there was no file table
2150 or it's currently not read in.
2151 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2152 struct quick_file_names *file_names;
2153
2154 /* The corresponding symbol table. This is NULL if symbols for this
2155 CU have not yet been read. */
2156 struct symtab *symtab;
2157
2158 /* A temporary mark bit used when iterating over all CUs in
2159 expand_symtabs_matching. */
2160 unsigned int mark : 1;
2161
2162 /* True if we've tried to read the file table and found there isn't one.
2163 There will be no point in trying to read it again next time. */
2164 unsigned int no_file_data : 1;
2165};
2166
094b34ac
DE
2167/* Utility hash function for a stmt_list_hash. */
2168
2169static hashval_t
2170hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2171{
2172 hashval_t v = 0;
2173
2174 if (stmt_list_hash->dwo_unit != NULL)
2175 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2176 v += stmt_list_hash->line_offset.sect_off;
2177 return v;
2178}
2179
2180/* Utility equality function for a stmt_list_hash. */
2181
2182static int
2183eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2184 const struct stmt_list_hash *rhs)
2185{
2186 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2187 return 0;
2188 if (lhs->dwo_unit != NULL
2189 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2190 return 0;
2191
2192 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2193}
2194
7b9f3c50
DE
2195/* Hash function for a quick_file_names. */
2196
2197static hashval_t
2198hash_file_name_entry (const void *e)
2199{
2200 const struct quick_file_names *file_data = e;
2201
094b34ac 2202 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2203}
2204
2205/* Equality function for a quick_file_names. */
2206
2207static int
2208eq_file_name_entry (const void *a, const void *b)
2209{
2210 const struct quick_file_names *ea = a;
2211 const struct quick_file_names *eb = b;
2212
094b34ac 2213 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2214}
2215
2216/* Delete function for a quick_file_names. */
2217
2218static void
2219delete_file_name_entry (void *e)
2220{
2221 struct quick_file_names *file_data = e;
2222 int i;
2223
2224 for (i = 0; i < file_data->num_file_names; ++i)
2225 {
2226 xfree ((void*) file_data->file_names[i]);
2227 if (file_data->real_names)
2228 xfree ((void*) file_data->real_names[i]);
2229 }
2230
2231 /* The space for the struct itself lives on objfile_obstack,
2232 so we don't free it here. */
2233}
2234
2235/* Create a quick_file_names hash table. */
2236
2237static htab_t
2238create_quick_file_names_table (unsigned int nr_initial_entries)
2239{
2240 return htab_create_alloc (nr_initial_entries,
2241 hash_file_name_entry, eq_file_name_entry,
2242 delete_file_name_entry, xcalloc, xfree);
2243}
9291a0cd 2244
918dd910
JK
2245/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2246 have to be created afterwards. You should call age_cached_comp_units after
2247 processing PER_CU->CU. dw2_setup must have been already called. */
2248
2249static void
2250load_cu (struct dwarf2_per_cu_data *per_cu)
2251{
3019eac3 2252 if (per_cu->is_debug_types)
e5fe5e75 2253 load_full_type_unit (per_cu);
918dd910 2254 else
95554aad 2255 load_full_comp_unit (per_cu, language_minimal);
918dd910 2256
918dd910 2257 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2258
2259 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2260}
2261
a0f42c21 2262/* Read in the symbols for PER_CU. */
2fdf6df6 2263
9291a0cd 2264static void
a0f42c21 2265dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2266{
2267 struct cleanup *back_to;
2268
f4dc4d17
DE
2269 /* Skip type_unit_groups, reading the type units they contain
2270 is handled elsewhere. */
2271 if (IS_TYPE_UNIT_GROUP (per_cu))
2272 return;
2273
9291a0cd
TT
2274 back_to = make_cleanup (dwarf2_release_queue, NULL);
2275
95554aad
TT
2276 if (dwarf2_per_objfile->using_index
2277 ? per_cu->v.quick->symtab == NULL
2278 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2279 {
2280 queue_comp_unit (per_cu, language_minimal);
2281 load_cu (per_cu);
2282 }
9291a0cd 2283
a0f42c21 2284 process_queue ();
9291a0cd
TT
2285
2286 /* Age the cache, releasing compilation units that have not
2287 been used recently. */
2288 age_cached_comp_units ();
2289
2290 do_cleanups (back_to);
2291}
2292
2293/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2294 the objfile from which this CU came. Returns the resulting symbol
2295 table. */
2fdf6df6 2296
9291a0cd 2297static struct symtab *
a0f42c21 2298dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2299{
95554aad 2300 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2301 if (!per_cu->v.quick->symtab)
2302 {
2303 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2304 increment_reading_symtab ();
a0f42c21 2305 dw2_do_instantiate_symtab (per_cu);
95554aad 2306 process_cu_includes ();
9291a0cd
TT
2307 do_cleanups (back_to);
2308 }
2309 return per_cu->v.quick->symtab;
2310}
2311
f4dc4d17
DE
2312/* Return the CU given its index.
2313
2314 This is intended for loops like:
2315
2316 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2317 + dwarf2_per_objfile->n_type_units); ++i)
2318 {
2319 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2320
2321 ...;
2322 }
2323*/
2fdf6df6 2324
1fd400ff
TT
2325static struct dwarf2_per_cu_data *
2326dw2_get_cu (int index)
2327{
2328 if (index >= dwarf2_per_objfile->n_comp_units)
2329 {
f4dc4d17 2330 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2331 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2332 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2333 }
2334
2335 return dwarf2_per_objfile->all_comp_units[index];
2336}
2337
2338/* Return the primary CU given its index.
2339 The difference between this function and dw2_get_cu is in the handling
2340 of type units (TUs). Here we return the type_unit_group object.
2341
2342 This is intended for loops like:
2343
2344 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2345 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2346 {
2347 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2348
2349 ...;
2350 }
2351*/
2352
2353static struct dwarf2_per_cu_data *
2354dw2_get_primary_cu (int index)
2355{
2356 if (index >= dwarf2_per_objfile->n_comp_units)
2357 {
1fd400ff 2358 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2359 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2360 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2361 }
f4dc4d17 2362
1fd400ff
TT
2363 return dwarf2_per_objfile->all_comp_units[index];
2364}
2365
2ec9a5e0
TT
2366/* A helper for create_cus_from_index that handles a given list of
2367 CUs. */
2fdf6df6 2368
74a0d9f6 2369static void
2ec9a5e0
TT
2370create_cus_from_index_list (struct objfile *objfile,
2371 const gdb_byte *cu_list, offset_type n_elements,
2372 struct dwarf2_section_info *section,
2373 int is_dwz,
2374 int base_offset)
9291a0cd
TT
2375{
2376 offset_type i;
9291a0cd 2377
2ec9a5e0 2378 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2379 {
2380 struct dwarf2_per_cu_data *the_cu;
2381 ULONGEST offset, length;
2382
74a0d9f6
JK
2383 gdb_static_assert (sizeof (ULONGEST) >= 8);
2384 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2385 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2386 cu_list += 2 * 8;
2387
2388 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2389 struct dwarf2_per_cu_data);
b64f50a1 2390 the_cu->offset.sect_off = offset;
9291a0cd
TT
2391 the_cu->length = length;
2392 the_cu->objfile = objfile;
2ec9a5e0 2393 the_cu->info_or_types_section = section;
9291a0cd
TT
2394 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2395 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2396 the_cu->is_dwz = is_dwz;
2397 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2398 }
9291a0cd
TT
2399}
2400
2ec9a5e0 2401/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2402 the CU objects for this objfile. */
2ec9a5e0 2403
74a0d9f6 2404static void
2ec9a5e0
TT
2405create_cus_from_index (struct objfile *objfile,
2406 const gdb_byte *cu_list, offset_type cu_list_elements,
2407 const gdb_byte *dwz_list, offset_type dwz_elements)
2408{
2409 struct dwz_file *dwz;
2410
2411 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2412 dwarf2_per_objfile->all_comp_units
2413 = obstack_alloc (&objfile->objfile_obstack,
2414 dwarf2_per_objfile->n_comp_units
2415 * sizeof (struct dwarf2_per_cu_data *));
2416
74a0d9f6
JK
2417 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2418 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2419
2420 if (dwz_elements == 0)
74a0d9f6 2421 return;
2ec9a5e0
TT
2422
2423 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2424 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2425 cu_list_elements / 2);
2ec9a5e0
TT
2426}
2427
1fd400ff 2428/* Create the signatured type hash table from the index. */
673bfd45 2429
74a0d9f6 2430static void
673bfd45 2431create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2432 struct dwarf2_section_info *section,
673bfd45
DE
2433 const gdb_byte *bytes,
2434 offset_type elements)
1fd400ff
TT
2435{
2436 offset_type i;
673bfd45 2437 htab_t sig_types_hash;
1fd400ff 2438
d467dd73
DE
2439 dwarf2_per_objfile->n_type_units = elements / 3;
2440 dwarf2_per_objfile->all_type_units
1fd400ff 2441 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2442 dwarf2_per_objfile->n_type_units
b4dd5633 2443 * sizeof (struct signatured_type *));
1fd400ff 2444
673bfd45 2445 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2446
2447 for (i = 0; i < elements; i += 3)
2448 {
52dc124a
DE
2449 struct signatured_type *sig_type;
2450 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2451 void **slot;
2452
74a0d9f6
JK
2453 gdb_static_assert (sizeof (ULONGEST) >= 8);
2454 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2455 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2456 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2457 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2458 bytes += 3 * 8;
2459
52dc124a 2460 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2461 struct signatured_type);
52dc124a 2462 sig_type->signature = signature;
3019eac3
DE
2463 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2464 sig_type->per_cu.is_debug_types = 1;
2465 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2466 sig_type->per_cu.offset.sect_off = offset;
2467 sig_type->per_cu.objfile = objfile;
2468 sig_type->per_cu.v.quick
1fd400ff
TT
2469 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2470 struct dwarf2_per_cu_quick_data);
2471
52dc124a
DE
2472 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2473 *slot = sig_type;
1fd400ff 2474
b4dd5633 2475 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2476 }
2477
673bfd45 2478 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2479}
2480
9291a0cd
TT
2481/* Read the address map data from the mapped index, and use it to
2482 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2483
9291a0cd
TT
2484static void
2485create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2486{
2487 const gdb_byte *iter, *end;
2488 struct obstack temp_obstack;
2489 struct addrmap *mutable_map;
2490 struct cleanup *cleanup;
2491 CORE_ADDR baseaddr;
2492
2493 obstack_init (&temp_obstack);
2494 cleanup = make_cleanup_obstack_free (&temp_obstack);
2495 mutable_map = addrmap_create_mutable (&temp_obstack);
2496
2497 iter = index->address_table;
2498 end = iter + index->address_table_size;
2499
2500 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2501
2502 while (iter < end)
2503 {
2504 ULONGEST hi, lo, cu_index;
2505 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2506 iter += 8;
2507 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2508 iter += 8;
2509 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2510 iter += 4;
2511
2512 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2513 dw2_get_cu (cu_index));
9291a0cd
TT
2514 }
2515
2516 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2517 &objfile->objfile_obstack);
2518 do_cleanups (cleanup);
2519}
2520
59d7bcaf
JK
2521/* The hash function for strings in the mapped index. This is the same as
2522 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2523 implementation. This is necessary because the hash function is tied to the
2524 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2525 SYMBOL_HASH_NEXT.
2526
2527 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2528
9291a0cd 2529static hashval_t
559a7a62 2530mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2531{
2532 const unsigned char *str = (const unsigned char *) p;
2533 hashval_t r = 0;
2534 unsigned char c;
2535
2536 while ((c = *str++) != 0)
559a7a62
JK
2537 {
2538 if (index_version >= 5)
2539 c = tolower (c);
2540 r = r * 67 + c - 113;
2541 }
9291a0cd
TT
2542
2543 return r;
2544}
2545
2546/* Find a slot in the mapped index INDEX for the object named NAME.
2547 If NAME is found, set *VEC_OUT to point to the CU vector in the
2548 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2549
9291a0cd
TT
2550static int
2551find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2552 offset_type **vec_out)
2553{
0cf03b49
JK
2554 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2555 offset_type hash;
9291a0cd 2556 offset_type slot, step;
559a7a62 2557 int (*cmp) (const char *, const char *);
9291a0cd 2558
0cf03b49
JK
2559 if (current_language->la_language == language_cplus
2560 || current_language->la_language == language_java
2561 || current_language->la_language == language_fortran)
2562 {
2563 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2564 not contain any. */
2565 const char *paren = strchr (name, '(');
2566
2567 if (paren)
2568 {
2569 char *dup;
2570
2571 dup = xmalloc (paren - name + 1);
2572 memcpy (dup, name, paren - name);
2573 dup[paren - name] = 0;
2574
2575 make_cleanup (xfree, dup);
2576 name = dup;
2577 }
2578 }
2579
559a7a62 2580 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2581 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2582 simulate our NAME being searched is also lowercased. */
2583 hash = mapped_index_string_hash ((index->version == 4
2584 && case_sensitivity == case_sensitive_off
2585 ? 5 : index->version),
2586 name);
2587
3876f04e
DE
2588 slot = hash & (index->symbol_table_slots - 1);
2589 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2590 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2591
2592 for (;;)
2593 {
2594 /* Convert a slot number to an offset into the table. */
2595 offset_type i = 2 * slot;
2596 const char *str;
3876f04e 2597 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2598 {
2599 do_cleanups (back_to);
2600 return 0;
2601 }
9291a0cd 2602
3876f04e 2603 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2604 if (!cmp (name, str))
9291a0cd
TT
2605 {
2606 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2607 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2608 do_cleanups (back_to);
9291a0cd
TT
2609 return 1;
2610 }
2611
3876f04e 2612 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2613 }
2614}
2615
2ec9a5e0
TT
2616/* A helper function that reads the .gdb_index from SECTION and fills
2617 in MAP. FILENAME is the name of the file containing the section;
2618 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2619 ok to use deprecated sections.
2620
2621 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2622 out parameters that are filled in with information about the CU and
2623 TU lists in the section.
2624
2625 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2626
9291a0cd 2627static int
2ec9a5e0
TT
2628read_index_from_section (struct objfile *objfile,
2629 const char *filename,
2630 int deprecated_ok,
2631 struct dwarf2_section_info *section,
2632 struct mapped_index *map,
2633 const gdb_byte **cu_list,
2634 offset_type *cu_list_elements,
2635 const gdb_byte **types_list,
2636 offset_type *types_list_elements)
9291a0cd 2637{
9291a0cd 2638 char *addr;
2ec9a5e0 2639 offset_type version;
b3b272e1 2640 offset_type *metadata;
1fd400ff 2641 int i;
9291a0cd 2642
2ec9a5e0 2643 if (dwarf2_section_empty_p (section))
9291a0cd 2644 return 0;
82430852
JK
2645
2646 /* Older elfutils strip versions could keep the section in the main
2647 executable while splitting it for the separate debug info file. */
2ec9a5e0 2648 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2649 return 0;
2650
2ec9a5e0 2651 dwarf2_read_section (objfile, section);
9291a0cd 2652
2ec9a5e0 2653 addr = section->buffer;
9291a0cd 2654 /* Version check. */
1fd400ff 2655 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2656 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2657 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2658 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2659 indices. */
831adc1f 2660 if (version < 4)
481860b3
GB
2661 {
2662 static int warning_printed = 0;
2663 if (!warning_printed)
2664 {
2665 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2666 filename);
481860b3
GB
2667 warning_printed = 1;
2668 }
2669 return 0;
2670 }
2671 /* Index version 4 uses a different hash function than index version
2672 5 and later.
2673
2674 Versions earlier than 6 did not emit psymbols for inlined
2675 functions. Using these files will cause GDB not to be able to
2676 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2677 indices unless the user has done
2678 "set use-deprecated-index-sections on". */
2ec9a5e0 2679 if (version < 6 && !deprecated_ok)
481860b3
GB
2680 {
2681 static int warning_printed = 0;
2682 if (!warning_printed)
2683 {
e615022a
DE
2684 warning (_("\
2685Skipping deprecated .gdb_index section in %s.\n\
2686Do \"set use-deprecated-index-sections on\" before the file is read\n\
2687to use the section anyway."),
2ec9a5e0 2688 filename);
481860b3
GB
2689 warning_printed = 1;
2690 }
2691 return 0;
2692 }
2693 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2694 longer backward compatible. */
156942c7 2695 if (version > 7)
594e8718 2696 return 0;
9291a0cd 2697
559a7a62 2698 map->version = version;
2ec9a5e0 2699 map->total_size = section->size;
9291a0cd
TT
2700
2701 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2702
2703 i = 0;
2ec9a5e0
TT
2704 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2705 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2706 / 8);
1fd400ff
TT
2707 ++i;
2708
2ec9a5e0
TT
2709 *types_list = addr + MAYBE_SWAP (metadata[i]);
2710 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2711 - MAYBE_SWAP (metadata[i]))
2712 / 8);
987d643c 2713 ++i;
1fd400ff
TT
2714
2715 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2716 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2717 - MAYBE_SWAP (metadata[i]));
2718 ++i;
2719
3876f04e
DE
2720 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2721 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2722 - MAYBE_SWAP (metadata[i]))
2723 / (2 * sizeof (offset_type)));
1fd400ff 2724 ++i;
9291a0cd 2725
1fd400ff
TT
2726 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2727
2ec9a5e0
TT
2728 return 1;
2729}
2730
2731
2732/* Read the index file. If everything went ok, initialize the "quick"
2733 elements of all the CUs and return 1. Otherwise, return 0. */
2734
2735static int
2736dwarf2_read_index (struct objfile *objfile)
2737{
2738 struct mapped_index local_map, *map;
2739 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2740 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2741
2742 if (!read_index_from_section (objfile, objfile->name,
2743 use_deprecated_index_sections,
2744 &dwarf2_per_objfile->gdb_index, &local_map,
2745 &cu_list, &cu_list_elements,
2746 &types_list, &types_list_elements))
2747 return 0;
2748
0fefef59 2749 /* Don't use the index if it's empty. */
2ec9a5e0 2750 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2751 return 0;
2752
2ec9a5e0
TT
2753 /* If there is a .dwz file, read it so we can get its CU list as
2754 well. */
2755 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2756 {
2757 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2758 struct mapped_index dwz_map;
2759 const gdb_byte *dwz_types_ignore;
2760 offset_type dwz_types_elements_ignore;
2761
2762 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2763 1,
2764 &dwz->gdb_index, &dwz_map,
2765 &dwz_list, &dwz_list_elements,
2766 &dwz_types_ignore,
2767 &dwz_types_elements_ignore))
2768 {
2769 warning (_("could not read '.gdb_index' section from %s; skipping"),
2770 bfd_get_filename (dwz->dwz_bfd));
2771 return 0;
2772 }
2773 }
2774
74a0d9f6
JK
2775 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2776 dwz_list_elements);
1fd400ff 2777
8b70b953
TT
2778 if (types_list_elements)
2779 {
2780 struct dwarf2_section_info *section;
2781
2782 /* We can only handle a single .debug_types when we have an
2783 index. */
2784 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2785 return 0;
2786
2787 section = VEC_index (dwarf2_section_info_def,
2788 dwarf2_per_objfile->types, 0);
2789
74a0d9f6
JK
2790 create_signatured_type_table_from_index (objfile, section, types_list,
2791 types_list_elements);
8b70b953 2792 }
9291a0cd 2793
2ec9a5e0
TT
2794 create_addrmap_from_index (objfile, &local_map);
2795
2796 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2797 *map = local_map;
9291a0cd
TT
2798
2799 dwarf2_per_objfile->index_table = map;
2800 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2801 dwarf2_per_objfile->quick_file_names_table =
2802 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2803
2804 return 1;
2805}
2806
2807/* A helper for the "quick" functions which sets the global
2808 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2809
9291a0cd
TT
2810static void
2811dw2_setup (struct objfile *objfile)
2812{
2813 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2814 gdb_assert (dwarf2_per_objfile);
2815}
2816
f4dc4d17
DE
2817/* Reader function for dw2_build_type_unit_groups. */
2818
2819static void
2820dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2821 gdb_byte *info_ptr,
2822 struct die_info *type_unit_die,
2823 int has_children,
2824 void *data)
2825{
2826 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2827 struct attribute *attr;
2828 struct type_unit_group *tu_group;
2829
2830 gdb_assert (data == NULL);
2831
2832 if (! has_children)
2833 return;
2834
2835 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2836 /* Call this for its side-effect of creating the associated
2837 struct type_unit_group if it doesn't already exist. */
094b34ac 2838 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2839}
2840
2841/* Build dwarf2_per_objfile->type_unit_groups.
2842 This function may be called multiple times. */
2843
2844static void
2845dw2_build_type_unit_groups (void)
2846{
2847 if (dwarf2_per_objfile->type_unit_groups == NULL)
2848 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2849}
2850
dee91e82 2851/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2852
dee91e82
DE
2853static void
2854dw2_get_file_names_reader (const struct die_reader_specs *reader,
2855 gdb_byte *info_ptr,
2856 struct die_info *comp_unit_die,
2857 int has_children,
2858 void *data)
9291a0cd 2859{
dee91e82
DE
2860 struct dwarf2_cu *cu = reader->cu;
2861 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2862 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2863 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2864 struct line_header *lh;
9291a0cd 2865 struct attribute *attr;
dee91e82 2866 int i;
9291a0cd 2867 char *name, *comp_dir;
7b9f3c50
DE
2868 void **slot;
2869 struct quick_file_names *qfn;
2870 unsigned int line_offset;
9291a0cd 2871
07261596
TT
2872 /* Our callers never want to match partial units -- instead they
2873 will match the enclosing full CU. */
2874 if (comp_unit_die->tag == DW_TAG_partial_unit)
2875 {
2876 this_cu->v.quick->no_file_data = 1;
2877 return;
2878 }
2879
094b34ac
DE
2880 /* If we're reading the line header for TUs, store it in the "per_cu"
2881 for tu_group. */
2882 if (this_cu->is_debug_types)
2883 {
2884 struct type_unit_group *tu_group = data;
2885
2886 gdb_assert (tu_group != NULL);
2887 lh_cu = &tu_group->per_cu;
2888 }
2889 else
2890 lh_cu = this_cu;
2891
7b9f3c50
DE
2892 lh = NULL;
2893 slot = NULL;
2894 line_offset = 0;
dee91e82
DE
2895
2896 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2897 if (attr)
2898 {
7b9f3c50
DE
2899 struct quick_file_names find_entry;
2900
2901 line_offset = DW_UNSND (attr);
2902
2903 /* We may have already read in this line header (TU line header sharing).
2904 If we have we're done. */
094b34ac
DE
2905 find_entry.hash.dwo_unit = cu->dwo_unit;
2906 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2907 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2908 &find_entry, INSERT);
2909 if (*slot != NULL)
2910 {
094b34ac 2911 lh_cu->v.quick->file_names = *slot;
dee91e82 2912 return;
7b9f3c50
DE
2913 }
2914
3019eac3 2915 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2916 }
2917 if (lh == NULL)
2918 {
094b34ac 2919 lh_cu->v.quick->no_file_data = 1;
dee91e82 2920 return;
9291a0cd
TT
2921 }
2922
7b9f3c50 2923 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2924 qfn->hash.dwo_unit = cu->dwo_unit;
2925 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2926 gdb_assert (slot != NULL);
2927 *slot = qfn;
9291a0cd 2928
dee91e82 2929 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2930
7b9f3c50
DE
2931 qfn->num_file_names = lh->num_file_names;
2932 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2933 lh->num_file_names * sizeof (char *));
9291a0cd 2934 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2935 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2936 qfn->real_names = NULL;
9291a0cd 2937
7b9f3c50 2938 free_line_header (lh);
7b9f3c50 2939
094b34ac 2940 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2941}
2942
2943/* A helper for the "quick" functions which attempts to read the line
2944 table for THIS_CU. */
2945
2946static struct quick_file_names *
2947dw2_get_file_names (struct objfile *objfile,
2948 struct dwarf2_per_cu_data *this_cu)
2949{
f4dc4d17
DE
2950 /* For TUs this should only be called on the parent group. */
2951 if (this_cu->is_debug_types)
2952 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2953
dee91e82
DE
2954 if (this_cu->v.quick->file_names != NULL)
2955 return this_cu->v.quick->file_names;
2956 /* If we know there is no line data, no point in looking again. */
2957 if (this_cu->v.quick->no_file_data)
2958 return NULL;
2959
3019eac3
DE
2960 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2961 in the stub for CUs, there's is no need to lookup the DWO file.
2962 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2963 DWO file. */
2964 if (this_cu->is_debug_types)
094b34ac
DE
2965 {
2966 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2967
2968 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2969 dw2_get_file_names_reader, tu_group);
2970 }
3019eac3
DE
2971 else
2972 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2973
2974 if (this_cu->v.quick->no_file_data)
2975 return NULL;
2976 return this_cu->v.quick->file_names;
9291a0cd
TT
2977}
2978
2979/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2980 real path for a given file name from the line table. */
2fdf6df6 2981
9291a0cd 2982static const char *
7b9f3c50
DE
2983dw2_get_real_path (struct objfile *objfile,
2984 struct quick_file_names *qfn, int index)
9291a0cd 2985{
7b9f3c50
DE
2986 if (qfn->real_names == NULL)
2987 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2988 qfn->num_file_names, sizeof (char *));
9291a0cd 2989
7b9f3c50
DE
2990 if (qfn->real_names[index] == NULL)
2991 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2992
7b9f3c50 2993 return qfn->real_names[index];
9291a0cd
TT
2994}
2995
2996static struct symtab *
2997dw2_find_last_source_symtab (struct objfile *objfile)
2998{
2999 int index;
ae2de4f8 3000
9291a0cd
TT
3001 dw2_setup (objfile);
3002 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3003 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3004}
3005
7b9f3c50
DE
3006/* Traversal function for dw2_forget_cached_source_info. */
3007
3008static int
3009dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3010{
7b9f3c50 3011 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3012
7b9f3c50 3013 if (file_data->real_names)
9291a0cd 3014 {
7b9f3c50 3015 int i;
9291a0cd 3016
7b9f3c50 3017 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3018 {
7b9f3c50
DE
3019 xfree ((void*) file_data->real_names[i]);
3020 file_data->real_names[i] = NULL;
9291a0cd
TT
3021 }
3022 }
7b9f3c50
DE
3023
3024 return 1;
3025}
3026
3027static void
3028dw2_forget_cached_source_info (struct objfile *objfile)
3029{
3030 dw2_setup (objfile);
3031
3032 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3033 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3034}
3035
f8eba3c6
TT
3036/* Helper function for dw2_map_symtabs_matching_filename that expands
3037 the symtabs and calls the iterator. */
3038
3039static int
3040dw2_map_expand_apply (struct objfile *objfile,
3041 struct dwarf2_per_cu_data *per_cu,
3042 const char *name,
3043 const char *full_path, const char *real_path,
3044 int (*callback) (struct symtab *, void *),
3045 void *data)
3046{
3047 struct symtab *last_made = objfile->symtabs;
3048
3049 /* Don't visit already-expanded CUs. */
3050 if (per_cu->v.quick->symtab)
3051 return 0;
3052
3053 /* This may expand more than one symtab, and we want to iterate over
3054 all of them. */
a0f42c21 3055 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3056
3057 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3058 objfile->symtabs, last_made);
3059}
3060
3061/* Implementation of the map_symtabs_matching_filename method. */
3062
9291a0cd 3063static int
f8eba3c6
TT
3064dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3065 const char *full_path, const char *real_path,
3066 int (*callback) (struct symtab *, void *),
3067 void *data)
9291a0cd
TT
3068{
3069 int i;
c011a4f4 3070 const char *name_basename = lbasename (name);
4aac40c8
TT
3071 int name_len = strlen (name);
3072 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3073
3074 dw2_setup (objfile);
ae2de4f8 3075
f4dc4d17
DE
3076 dw2_build_type_unit_groups ();
3077
1fd400ff 3078 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3079 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3080 {
3081 int j;
f4dc4d17 3082 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3083 struct quick_file_names *file_data;
9291a0cd 3084
3d7bb9d9 3085 /* We only need to look at symtabs not already expanded. */
e254ef6a 3086 if (per_cu->v.quick->symtab)
9291a0cd
TT
3087 continue;
3088
7b9f3c50
DE
3089 file_data = dw2_get_file_names (objfile, per_cu);
3090 if (file_data == NULL)
9291a0cd
TT
3091 continue;
3092
7b9f3c50 3093 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3094 {
7b9f3c50 3095 const char *this_name = file_data->file_names[j];
9291a0cd 3096
4aac40c8
TT
3097 if (FILENAME_CMP (name, this_name) == 0
3098 || (!is_abs && compare_filenames_for_search (this_name,
3099 name, name_len)))
9291a0cd 3100 {
f8eba3c6
TT
3101 if (dw2_map_expand_apply (objfile, per_cu,
3102 name, full_path, real_path,
3103 callback, data))
3104 return 1;
4aac40c8 3105 }
9291a0cd 3106
c011a4f4
DE
3107 /* Before we invoke realpath, which can get expensive when many
3108 files are involved, do a quick comparison of the basenames. */
3109 if (! basenames_may_differ
3110 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3111 continue;
3112
9291a0cd
TT
3113 if (full_path != NULL)
3114 {
7b9f3c50
DE
3115 const char *this_real_name = dw2_get_real_path (objfile,
3116 file_data, j);
9291a0cd 3117
7b9f3c50 3118 if (this_real_name != NULL
4aac40c8
TT
3119 && (FILENAME_CMP (full_path, this_real_name) == 0
3120 || (!is_abs
3121 && compare_filenames_for_search (this_real_name,
3122 name, name_len))))
9291a0cd 3123 {
f8eba3c6
TT
3124 if (dw2_map_expand_apply (objfile, per_cu,
3125 name, full_path, real_path,
3126 callback, data))
3127 return 1;
9291a0cd
TT
3128 }
3129 }
3130
3131 if (real_path != NULL)
3132 {
7b9f3c50
DE
3133 const char *this_real_name = dw2_get_real_path (objfile,
3134 file_data, j);
9291a0cd 3135
7b9f3c50 3136 if (this_real_name != NULL
4aac40c8
TT
3137 && (FILENAME_CMP (real_path, this_real_name) == 0
3138 || (!is_abs
3139 && compare_filenames_for_search (this_real_name,
3140 name, name_len))))
9291a0cd 3141 {
f8eba3c6
TT
3142 if (dw2_map_expand_apply (objfile, per_cu,
3143 name, full_path, real_path,
3144 callback, data))
3145 return 1;
9291a0cd
TT
3146 }
3147 }
3148 }
3149 }
3150
9291a0cd
TT
3151 return 0;
3152}
3153
3154static struct symtab *
3155dw2_lookup_symbol (struct objfile *objfile, int block_index,
3156 const char *name, domain_enum domain)
3157{
774b6a14 3158 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
3159 instead. */
3160 return NULL;
3161}
3162
3163/* A helper function that expands all symtabs that hold an object
156942c7
DE
3164 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3165 symbols in block BLOCK_KIND. */
2fdf6df6 3166
9291a0cd 3167static void
156942c7
DE
3168dw2_do_expand_symtabs_matching (struct objfile *objfile,
3169 int want_specific_block,
3170 enum block_enum block_kind,
3171 const char *name, domain_enum domain)
9291a0cd 3172{
156942c7
DE
3173 struct mapped_index *index;
3174
9291a0cd
TT
3175 dw2_setup (objfile);
3176
156942c7
DE
3177 index = dwarf2_per_objfile->index_table;
3178
ae2de4f8 3179 /* index_table is NULL if OBJF_READNOW. */
156942c7 3180 if (index)
9291a0cd
TT
3181 {
3182 offset_type *vec;
3183
156942c7 3184 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
3185 {
3186 offset_type i, len = MAYBE_SWAP (*vec);
3187 for (i = 0; i < len; ++i)
3188 {
156942c7
DE
3189 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3190 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 3191 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
3192 int want_static = block_kind != GLOBAL_BLOCK;
3193 /* This value is only valid for index versions >= 7. */
3194 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3195 gdb_index_symbol_kind symbol_kind =
3196 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
83a788b8
DE
3197 /* Only check the symbol attributes if they're present.
3198 Indices prior to version 7 don't record them,
3199 and indices >= 7 may elide them for certain symbols
3200 (gold does this). */
3201 int attrs_valid =
3202 (index->version >= 7
3203 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3204
3205 if (attrs_valid
3206 && want_specific_block
156942c7
DE
3207 && want_static != is_static)
3208 continue;
3209
83a788b8
DE
3210 /* Only check the symbol's kind if it has one. */
3211 if (attrs_valid)
156942c7
DE
3212 {
3213 switch (domain)
3214 {
3215 case VAR_DOMAIN:
3216 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3217 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3218 /* Some types are also in VAR_DOMAIN. */
3219 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3220 continue;
3221 break;
3222 case STRUCT_DOMAIN:
3223 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3224 continue;
3225 break;
3226 case LABEL_DOMAIN:
3227 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3228 continue;
3229 break;
3230 default:
3231 break;
3232 }
3233 }
1fd400ff 3234
a0f42c21 3235 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3236 }
3237 }
3238 }
3239}
3240
774b6a14
TT
3241static void
3242dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 3243 enum block_enum block_kind, const char *name,
774b6a14 3244 domain_enum domain)
9291a0cd 3245{
156942c7 3246 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
3247}
3248
3249static void
3250dw2_print_stats (struct objfile *objfile)
3251{
3252 int i, count;
3253
3254 dw2_setup (objfile);
3255 count = 0;
1fd400ff 3256 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3257 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3258 {
e254ef6a 3259 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3260
e254ef6a 3261 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3262 ++count;
3263 }
3264 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3265}
3266
3267static void
3268dw2_dump (struct objfile *objfile)
3269{
3270 /* Nothing worth printing. */
3271}
3272
3273static void
3274dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3275 struct section_offsets *delta)
3276{
3277 /* There's nothing to relocate here. */
3278}
3279
3280static void
3281dw2_expand_symtabs_for_function (struct objfile *objfile,
3282 const char *func_name)
3283{
156942c7
DE
3284 /* Note: It doesn't matter what we pass for block_kind here. */
3285 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3286 VAR_DOMAIN);
9291a0cd
TT
3287}
3288
3289static void
3290dw2_expand_all_symtabs (struct objfile *objfile)
3291{
3292 int i;
3293
3294 dw2_setup (objfile);
1fd400ff
TT
3295
3296 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3297 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3298 {
e254ef6a 3299 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3300
a0f42c21 3301 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3302 }
3303}
3304
3305static void
3306dw2_expand_symtabs_with_filename (struct objfile *objfile,
3307 const char *filename)
3308{
3309 int i;
3310
3311 dw2_setup (objfile);
d4637a04
DE
3312
3313 /* We don't need to consider type units here.
3314 This is only called for examining code, e.g. expand_line_sal.
3315 There can be an order of magnitude (or more) more type units
3316 than comp units, and we avoid them if we can. */
3317
3318 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3319 {
3320 int j;
e254ef6a 3321 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3322 struct quick_file_names *file_data;
9291a0cd 3323
3d7bb9d9 3324 /* We only need to look at symtabs not already expanded. */
e254ef6a 3325 if (per_cu->v.quick->symtab)
9291a0cd
TT
3326 continue;
3327
7b9f3c50
DE
3328 file_data = dw2_get_file_names (objfile, per_cu);
3329 if (file_data == NULL)
9291a0cd
TT
3330 continue;
3331
7b9f3c50 3332 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3333 {
7b9f3c50 3334 const char *this_name = file_data->file_names[j];
1ef75ecc 3335 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3336 {
a0f42c21 3337 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3338 break;
3339 }
3340 }
3341 }
3342}
3343
356d9f9d
TT
3344/* A helper function for dw2_find_symbol_file that finds the primary
3345 file name for a given CU. This is a die_reader_func. */
3346
3347static void
3348dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3349 gdb_byte *info_ptr,
3350 struct die_info *comp_unit_die,
3351 int has_children,
3352 void *data)
3353{
3354 const char **result_ptr = data;
3355 struct dwarf2_cu *cu = reader->cu;
3356 struct attribute *attr;
3357
3358 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3359 if (attr == NULL)
3360 *result_ptr = NULL;
3361 else
3362 *result_ptr = DW_STRING (attr);
3363}
3364
dd786858 3365static const char *
9291a0cd
TT
3366dw2_find_symbol_file (struct objfile *objfile, const char *name)
3367{
e254ef6a 3368 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3369 offset_type *vec;
356d9f9d 3370 const char *filename;
9291a0cd
TT
3371
3372 dw2_setup (objfile);
3373
ae2de4f8 3374 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3375 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3376 {
3377 struct symtab *s;
3378
d790cf0a
DE
3379 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3380 {
3381 struct blockvector *bv = BLOCKVECTOR (s);
3382 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3383 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3384
3385 if (sym)
210bbc17 3386 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3387 }
96408a79
SA
3388 return NULL;
3389 }
9291a0cd
TT
3390
3391 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3392 name, &vec))
3393 return NULL;
3394
3395 /* Note that this just looks at the very first one named NAME -- but
3396 actually we are looking for a function. find_main_filename
3397 should be rewritten so that it doesn't require a custom hook. It
3398 could just use the ordinary symbol tables. */
3399 /* vec[0] is the length, which must always be >0. */
156942c7 3400 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3401
356d9f9d
TT
3402 if (per_cu->v.quick->symtab != NULL)
3403 return per_cu->v.quick->symtab->filename;
3404
f4dc4d17
DE
3405 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3406 dw2_get_primary_filename_reader, &filename);
9291a0cd 3407
356d9f9d 3408 return filename;
9291a0cd
TT
3409}
3410
3411static void
40658b94
PH
3412dw2_map_matching_symbols (const char * name, domain_enum namespace,
3413 struct objfile *objfile, int global,
3414 int (*callback) (struct block *,
3415 struct symbol *, void *),
2edb89d3
JK
3416 void *data, symbol_compare_ftype *match,
3417 symbol_compare_ftype *ordered_compare)
9291a0cd 3418{
40658b94 3419 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3420 current language is Ada for a non-Ada objfile using GNU index. As Ada
3421 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3422}
3423
3424static void
f8eba3c6
TT
3425dw2_expand_symtabs_matching
3426 (struct objfile *objfile,
3427 int (*file_matcher) (const char *, void *),
e078317b 3428 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3429 enum search_domain kind,
3430 void *data)
9291a0cd
TT
3431{
3432 int i;
3433 offset_type iter;
4b5246aa 3434 struct mapped_index *index;
9291a0cd
TT
3435
3436 dw2_setup (objfile);
ae2de4f8
DE
3437
3438 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3439 if (!dwarf2_per_objfile->index_table)
3440 return;
4b5246aa 3441 index = dwarf2_per_objfile->index_table;
9291a0cd 3442
7b08b9eb 3443 if (file_matcher != NULL)
24c79950
TT
3444 {
3445 struct cleanup *cleanup;
3446 htab_t visited_found, visited_not_found;
3447
f4dc4d17
DE
3448 dw2_build_type_unit_groups ();
3449
24c79950
TT
3450 visited_found = htab_create_alloc (10,
3451 htab_hash_pointer, htab_eq_pointer,
3452 NULL, xcalloc, xfree);
3453 cleanup = make_cleanup_htab_delete (visited_found);
3454 visited_not_found = htab_create_alloc (10,
3455 htab_hash_pointer, htab_eq_pointer,
3456 NULL, xcalloc, xfree);
3457 make_cleanup_htab_delete (visited_not_found);
3458
3459 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3460 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3461 {
3462 int j;
f4dc4d17 3463 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3464 struct quick_file_names *file_data;
3465 void **slot;
7b08b9eb 3466
24c79950 3467 per_cu->v.quick->mark = 0;
3d7bb9d9 3468
24c79950
TT
3469 /* We only need to look at symtabs not already expanded. */
3470 if (per_cu->v.quick->symtab)
3471 continue;
7b08b9eb 3472
24c79950
TT
3473 file_data = dw2_get_file_names (objfile, per_cu);
3474 if (file_data == NULL)
3475 continue;
7b08b9eb 3476
24c79950
TT
3477 if (htab_find (visited_not_found, file_data) != NULL)
3478 continue;
3479 else if (htab_find (visited_found, file_data) != NULL)
3480 {
3481 per_cu->v.quick->mark = 1;
3482 continue;
3483 }
3484
3485 for (j = 0; j < file_data->num_file_names; ++j)
3486 {
3487 if (file_matcher (file_data->file_names[j], data))
3488 {
3489 per_cu->v.quick->mark = 1;
3490 break;
3491 }
3492 }
3493
3494 slot = htab_find_slot (per_cu->v.quick->mark
3495 ? visited_found
3496 : visited_not_found,
3497 file_data, INSERT);
3498 *slot = file_data;
3499 }
3500
3501 do_cleanups (cleanup);
3502 }
9291a0cd 3503
3876f04e 3504 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3505 {
3506 offset_type idx = 2 * iter;
3507 const char *name;
3508 offset_type *vec, vec_len, vec_idx;
3509
3876f04e 3510 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3511 continue;
3512
3876f04e 3513 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3514
e078317b 3515 if (! (*name_matcher) (name, data))
9291a0cd
TT
3516 continue;
3517
3518 /* The name was matched, now expand corresponding CUs that were
3519 marked. */
4b5246aa 3520 vec = (offset_type *) (index->constant_pool
3876f04e 3521 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3522 vec_len = MAYBE_SWAP (vec[0]);
3523 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3524 {
e254ef6a 3525 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3526 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3527 gdb_index_symbol_kind symbol_kind =
3528 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3529 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3530
3531 /* Don't crash on bad data. */
3532 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3533 + dwarf2_per_objfile->n_type_units))
156942c7 3534 continue;
1fd400ff 3535
156942c7
DE
3536 /* Only check the symbol's kind if it has one.
3537 Indices prior to version 7 don't record it. */
3538 if (index->version >= 7)
3539 {
3540 switch (kind)
3541 {
3542 case VARIABLES_DOMAIN:
3543 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3544 continue;
3545 break;
3546 case FUNCTIONS_DOMAIN:
3547 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3548 continue;
3549 break;
3550 case TYPES_DOMAIN:
3551 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3552 continue;
3553 break;
3554 default:
3555 break;
3556 }
3557 }
3558
3559 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3560 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3561 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3562 }
3563 }
3564}
3565
9703b513
TT
3566/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3567 symtab. */
3568
3569static struct symtab *
3570recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3571{
3572 int i;
3573
3574 if (BLOCKVECTOR (symtab) != NULL
3575 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3576 return symtab;
3577
a3ec0bb1
DE
3578 if (symtab->includes == NULL)
3579 return NULL;
3580
9703b513
TT
3581 for (i = 0; symtab->includes[i]; ++i)
3582 {
a3ec0bb1 3583 struct symtab *s = symtab->includes[i];
9703b513
TT
3584
3585 s = recursively_find_pc_sect_symtab (s, pc);
3586 if (s != NULL)
3587 return s;
3588 }
3589
3590 return NULL;
3591}
3592
9291a0cd
TT
3593static struct symtab *
3594dw2_find_pc_sect_symtab (struct objfile *objfile,
3595 struct minimal_symbol *msymbol,
3596 CORE_ADDR pc,
3597 struct obj_section *section,
3598 int warn_if_readin)
3599{
3600 struct dwarf2_per_cu_data *data;
9703b513 3601 struct symtab *result;
9291a0cd
TT
3602
3603 dw2_setup (objfile);
3604
3605 if (!objfile->psymtabs_addrmap)
3606 return NULL;
3607
3608 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3609 if (!data)
3610 return NULL;
3611
3612 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3613 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3614 paddress (get_objfile_arch (objfile), pc));
3615
9703b513
TT
3616 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3617 gdb_assert (result != NULL);
3618 return result;
9291a0cd
TT
3619}
3620
9291a0cd 3621static void
44b13c5a 3622dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3623 void *data, int need_fullname)
9291a0cd
TT
3624{
3625 int i;
24c79950
TT
3626 struct cleanup *cleanup;
3627 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3628 NULL, xcalloc, xfree);
9291a0cd 3629
24c79950 3630 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3631 dw2_setup (objfile);
ae2de4f8 3632
f4dc4d17
DE
3633 dw2_build_type_unit_groups ();
3634
24c79950
TT
3635 /* We can ignore file names coming from already-expanded CUs. */
3636 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3637 + dwarf2_per_objfile->n_type_units); ++i)
3638 {
3639 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3640
3641 if (per_cu->v.quick->symtab)
3642 {
3643 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3644 INSERT);
3645
3646 *slot = per_cu->v.quick->file_names;
3647 }
3648 }
3649
1fd400ff 3650 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3651 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3652 {
3653 int j;
f4dc4d17 3654 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3655 struct quick_file_names *file_data;
24c79950 3656 void **slot;
9291a0cd 3657
3d7bb9d9 3658 /* We only need to look at symtabs not already expanded. */
e254ef6a 3659 if (per_cu->v.quick->symtab)
9291a0cd
TT
3660 continue;
3661
7b9f3c50
DE
3662 file_data = dw2_get_file_names (objfile, per_cu);
3663 if (file_data == NULL)
9291a0cd
TT
3664 continue;
3665
24c79950
TT
3666 slot = htab_find_slot (visited, file_data, INSERT);
3667 if (*slot)
3668 {
3669 /* Already visited. */
3670 continue;
3671 }
3672 *slot = file_data;
3673
7b9f3c50 3674 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3675 {
74e2f255
DE
3676 const char *this_real_name;
3677
3678 if (need_fullname)
3679 this_real_name = dw2_get_real_path (objfile, file_data, j);
3680 else
3681 this_real_name = NULL;
7b9f3c50 3682 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3683 }
3684 }
24c79950
TT
3685
3686 do_cleanups (cleanup);
9291a0cd
TT
3687}
3688
3689static int
3690dw2_has_symbols (struct objfile *objfile)
3691{
3692 return 1;
3693}
3694
3695const struct quick_symbol_functions dwarf2_gdb_index_functions =
3696{
3697 dw2_has_symbols,
3698 dw2_find_last_source_symtab,
3699 dw2_forget_cached_source_info,
f8eba3c6 3700 dw2_map_symtabs_matching_filename,
9291a0cd 3701 dw2_lookup_symbol,
774b6a14 3702 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3703 dw2_print_stats,
3704 dw2_dump,
3705 dw2_relocate,
3706 dw2_expand_symtabs_for_function,
3707 dw2_expand_all_symtabs,
3708 dw2_expand_symtabs_with_filename,
3709 dw2_find_symbol_file,
40658b94 3710 dw2_map_matching_symbols,
9291a0cd
TT
3711 dw2_expand_symtabs_matching,
3712 dw2_find_pc_sect_symtab,
9291a0cd
TT
3713 dw2_map_symbol_filenames
3714};
3715
3716/* Initialize for reading DWARF for this objfile. Return 0 if this
3717 file will use psymtabs, or 1 if using the GNU index. */
3718
3719int
3720dwarf2_initialize_objfile (struct objfile *objfile)
3721{
3722 /* If we're about to read full symbols, don't bother with the
3723 indices. In this case we also don't care if some other debug
3724 format is making psymtabs, because they are all about to be
3725 expanded anyway. */
3726 if ((objfile->flags & OBJF_READNOW))
3727 {
3728 int i;
3729
3730 dwarf2_per_objfile->using_index = 1;
3731 create_all_comp_units (objfile);
0e50663e 3732 create_all_type_units (objfile);
7b9f3c50
DE
3733 dwarf2_per_objfile->quick_file_names_table =
3734 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3735
1fd400ff 3736 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3737 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3738 {
e254ef6a 3739 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3740
e254ef6a
DE
3741 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3742 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3743 }
3744
3745 /* Return 1 so that gdb sees the "quick" functions. However,
3746 these functions will be no-ops because we will have expanded
3747 all symtabs. */
3748 return 1;
3749 }
3750
3751 if (dwarf2_read_index (objfile))
3752 return 1;
3753
9291a0cd
TT
3754 return 0;
3755}
3756
3757\f
3758
dce234bc
PP
3759/* Build a partial symbol table. */
3760
3761void
f29dff0a 3762dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3763{
f29dff0a 3764 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3765 {
3766 init_psymbol_list (objfile, 1024);
3767 }
3768
d146bf1e 3769 dwarf2_build_psymtabs_hard (objfile);
c906108c 3770}
c906108c 3771
1ce1cefd
DE
3772/* Return the total length of the CU described by HEADER. */
3773
3774static unsigned int
3775get_cu_length (const struct comp_unit_head *header)
3776{
3777 return header->initial_length_size + header->length;
3778}
3779
45452591
DE
3780/* Return TRUE if OFFSET is within CU_HEADER. */
3781
3782static inline int
b64f50a1 3783offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3784{
b64f50a1 3785 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3786 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3787
b64f50a1 3788 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3789}
3790
3b80fe9b
DE
3791/* Find the base address of the compilation unit for range lists and
3792 location lists. It will normally be specified by DW_AT_low_pc.
3793 In DWARF-3 draft 4, the base address could be overridden by
3794 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3795 compilation units with discontinuous ranges. */
3796
3797static void
3798dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3799{
3800 struct attribute *attr;
3801
3802 cu->base_known = 0;
3803 cu->base_address = 0;
3804
3805 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3806 if (attr)
3807 {
3808 cu->base_address = DW_ADDR (attr);
3809 cu->base_known = 1;
3810 }
3811 else
3812 {
3813 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3814 if (attr)
3815 {
3816 cu->base_address = DW_ADDR (attr);
3817 cu->base_known = 1;
3818 }
3819 }
3820}
3821
93311388
DE
3822/* Read in the comp unit header information from the debug_info at info_ptr.
3823 NOTE: This leaves members offset, first_die_offset to be filled in
3824 by the caller. */
107d2387 3825
fe1b8b76 3826static gdb_byte *
107d2387 3827read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3828 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3829{
3830 int signed_addr;
891d2f0b 3831 unsigned int bytes_read;
c764a876
DE
3832
3833 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3834 cu_header->initial_length_size = bytes_read;
3835 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3836 info_ptr += bytes_read;
107d2387
AC
3837 cu_header->version = read_2_bytes (abfd, info_ptr);
3838 info_ptr += 2;
b64f50a1
JK
3839 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3840 &bytes_read);
613e1657 3841 info_ptr += bytes_read;
107d2387
AC
3842 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3843 info_ptr += 1;
3844 signed_addr = bfd_get_sign_extend_vma (abfd);
3845 if (signed_addr < 0)
8e65ff28 3846 internal_error (__FILE__, __LINE__,
e2e0b3e5 3847 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3848 cu_header->signed_addr_p = signed_addr;
c764a876 3849
107d2387
AC
3850 return info_ptr;
3851}
3852
36586728
TT
3853/* Helper function that returns the proper abbrev section for
3854 THIS_CU. */
3855
3856static struct dwarf2_section_info *
3857get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3858{
3859 struct dwarf2_section_info *abbrev;
3860
3861 if (this_cu->is_dwz)
3862 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3863 else
3864 abbrev = &dwarf2_per_objfile->abbrev;
3865
3866 return abbrev;
3867}
3868
9ff913ba
DE
3869/* Subroutine of read_and_check_comp_unit_head and
3870 read_and_check_type_unit_head to simplify them.
3871 Perform various error checking on the header. */
3872
3873static void
3874error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3875 struct dwarf2_section_info *section,
3876 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3877{
3878 bfd *abfd = section->asection->owner;
3879 const char *filename = bfd_get_filename (abfd);
3880
3881 if (header->version != 2 && header->version != 3 && header->version != 4)
3882 error (_("Dwarf Error: wrong version in compilation unit header "
3883 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3884 filename);
3885
b64f50a1 3886 if (header->abbrev_offset.sect_off
36586728 3887 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3888 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3889 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3890 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3891 filename);
3892
3893 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3894 avoid potential 32-bit overflow. */
1ce1cefd 3895 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3896 > section->size)
3897 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3898 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3899 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3900 filename);
3901}
3902
3903/* Read in a CU/TU header and perform some basic error checking.
3904 The contents of the header are stored in HEADER.
3905 The result is a pointer to the start of the first DIE. */
adabb602 3906
fe1b8b76 3907static gdb_byte *
9ff913ba
DE
3908read_and_check_comp_unit_head (struct comp_unit_head *header,
3909 struct dwarf2_section_info *section,
4bdcc0c1 3910 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3911 gdb_byte *info_ptr,
3912 int is_debug_types_section)
72bf9492 3913{
fe1b8b76 3914 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3915 bfd *abfd = section->asection->owner;
72bf9492 3916
b64f50a1 3917 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3918
72bf9492
DJ
3919 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3920
460c1c54
CC
3921 /* If we're reading a type unit, skip over the signature and
3922 type_offset fields. */
b0df02fd 3923 if (is_debug_types_section)
460c1c54
CC
3924 info_ptr += 8 /*signature*/ + header->offset_size;
3925
b64f50a1 3926 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3927
4bdcc0c1 3928 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3929
3930 return info_ptr;
3931}
3932
348e048f
DE
3933/* Read in the types comp unit header information from .debug_types entry at
3934 types_ptr. The result is a pointer to one past the end of the header. */
3935
3936static gdb_byte *
9ff913ba
DE
3937read_and_check_type_unit_head (struct comp_unit_head *header,
3938 struct dwarf2_section_info *section,
4bdcc0c1 3939 struct dwarf2_section_info *abbrev_section,
9ff913ba 3940 gdb_byte *info_ptr,
dee91e82
DE
3941 ULONGEST *signature,
3942 cu_offset *type_offset_in_tu)
348e048f 3943{
9ff913ba
DE
3944 gdb_byte *beg_of_comp_unit = info_ptr;
3945 bfd *abfd = section->asection->owner;
348e048f 3946
b64f50a1 3947 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3948
9ff913ba 3949 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3950
9ff913ba
DE
3951 /* If we're reading a type unit, skip over the signature and
3952 type_offset fields. */
3953 if (signature != NULL)
3954 *signature = read_8_bytes (abfd, info_ptr);
3955 info_ptr += 8;
dee91e82
DE
3956 if (type_offset_in_tu != NULL)
3957 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3958 header->offset_size);
9ff913ba
DE
3959 info_ptr += header->offset_size;
3960
b64f50a1 3961 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3962
4bdcc0c1 3963 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3964
3965 return info_ptr;
348e048f
DE
3966}
3967
f4dc4d17
DE
3968/* Fetch the abbreviation table offset from a comp or type unit header. */
3969
3970static sect_offset
3971read_abbrev_offset (struct dwarf2_section_info *section,
3972 sect_offset offset)
3973{
3974 bfd *abfd = section->asection->owner;
3975 gdb_byte *info_ptr;
3976 unsigned int length, initial_length_size, offset_size;
3977 sect_offset abbrev_offset;
3978
3979 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3980 info_ptr = section->buffer + offset.sect_off;
3981 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3982 offset_size = initial_length_size == 4 ? 4 : 8;
3983 info_ptr += initial_length_size + 2 /*version*/;
3984 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3985 return abbrev_offset;
3986}
3987
aaa75496
JB
3988/* Allocate a new partial symtab for file named NAME and mark this new
3989 partial symtab as being an include of PST. */
3990
3991static void
3992dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3993 struct objfile *objfile)
3994{
3995 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3996
3997 subpst->section_offsets = pst->section_offsets;
3998 subpst->textlow = 0;
3999 subpst->texthigh = 0;
4000
4001 subpst->dependencies = (struct partial_symtab **)
4002 obstack_alloc (&objfile->objfile_obstack,
4003 sizeof (struct partial_symtab *));
4004 subpst->dependencies[0] = pst;
4005 subpst->number_of_dependencies = 1;
4006
4007 subpst->globals_offset = 0;
4008 subpst->n_global_syms = 0;
4009 subpst->statics_offset = 0;
4010 subpst->n_static_syms = 0;
4011 subpst->symtab = NULL;
4012 subpst->read_symtab = pst->read_symtab;
4013 subpst->readin = 0;
4014
4015 /* No private part is necessary for include psymtabs. This property
4016 can be used to differentiate between such include psymtabs and
10b3939b 4017 the regular ones. */
58a9656e 4018 subpst->read_symtab_private = NULL;
aaa75496
JB
4019}
4020
4021/* Read the Line Number Program data and extract the list of files
4022 included by the source file represented by PST. Build an include
d85a05f0 4023 partial symtab for each of these included files. */
aaa75496
JB
4024
4025static void
4026dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4027 struct die_info *die,
4028 struct partial_symtab *pst)
aaa75496 4029{
d85a05f0
DJ
4030 struct line_header *lh = NULL;
4031 struct attribute *attr;
aaa75496 4032
d85a05f0
DJ
4033 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4034 if (attr)
3019eac3 4035 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4036 if (lh == NULL)
4037 return; /* No linetable, so no includes. */
4038
c6da4cef 4039 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4040 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4041
4042 free_line_header (lh);
4043}
4044
348e048f 4045static hashval_t
52dc124a 4046hash_signatured_type (const void *item)
348e048f 4047{
52dc124a 4048 const struct signatured_type *sig_type = item;
9a619af0 4049
348e048f 4050 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4051 return sig_type->signature;
348e048f
DE
4052}
4053
4054static int
52dc124a 4055eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4056{
4057 const struct signatured_type *lhs = item_lhs;
4058 const struct signatured_type *rhs = item_rhs;
9a619af0 4059
348e048f
DE
4060 return lhs->signature == rhs->signature;
4061}
4062
1fd400ff
TT
4063/* Allocate a hash table for signatured types. */
4064
4065static htab_t
673bfd45 4066allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4067{
4068 return htab_create_alloc_ex (41,
52dc124a
DE
4069 hash_signatured_type,
4070 eq_signatured_type,
1fd400ff
TT
4071 NULL,
4072 &objfile->objfile_obstack,
4073 hashtab_obstack_allocate,
4074 dummy_obstack_deallocate);
4075}
4076
d467dd73 4077/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4078
4079static int
d467dd73 4080add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4081{
4082 struct signatured_type *sigt = *slot;
b4dd5633 4083 struct signatured_type ***datap = datum;
1fd400ff 4084
b4dd5633 4085 **datap = sigt;
1fd400ff
TT
4086 ++*datap;
4087
4088 return 1;
4089}
4090
3019eac3 4091/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4092 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4093 NULL otherwise.
4094 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4095 The result is a pointer to the hash table or NULL if there are
4096 no types. */
348e048f 4097
3019eac3
DE
4098static htab_t
4099create_debug_types_hash_table (struct dwo_file *dwo_file,
4100 VEC (dwarf2_section_info_def) *types)
348e048f 4101{
3019eac3 4102 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4103 htab_t types_htab = NULL;
8b70b953
TT
4104 int ix;
4105 struct dwarf2_section_info *section;
4bdcc0c1 4106 struct dwarf2_section_info *abbrev_section;
348e048f 4107
3019eac3
DE
4108 if (VEC_empty (dwarf2_section_info_def, types))
4109 return NULL;
348e048f 4110
4bdcc0c1
DE
4111 abbrev_section = (dwo_file != NULL
4112 ? &dwo_file->sections.abbrev
4113 : &dwarf2_per_objfile->abbrev);
4114
09406207
DE
4115 if (dwarf2_read_debug)
4116 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4117 dwo_file ? ".dwo" : "",
4118 bfd_get_filename (abbrev_section->asection->owner));
4119
8b70b953 4120 for (ix = 0;
3019eac3 4121 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4122 ++ix)
4123 {
3019eac3 4124 bfd *abfd;
8b70b953 4125 gdb_byte *info_ptr, *end_ptr;
36586728 4126 struct dwarf2_section_info *abbrev_section;
348e048f 4127
8b70b953
TT
4128 dwarf2_read_section (objfile, section);
4129 info_ptr = section->buffer;
348e048f 4130
8b70b953
TT
4131 if (info_ptr == NULL)
4132 continue;
348e048f 4133
3019eac3
DE
4134 /* We can't set abfd until now because the section may be empty or
4135 not present, in which case section->asection will be NULL. */
4136 abfd = section->asection->owner;
4137
36586728
TT
4138 if (dwo_file)
4139 abbrev_section = &dwo_file->sections.abbrev;
4140 else
4141 abbrev_section = &dwarf2_per_objfile->abbrev;
4142
8b70b953 4143 if (types_htab == NULL)
3019eac3
DE
4144 {
4145 if (dwo_file)
4146 types_htab = allocate_dwo_unit_table (objfile);
4147 else
4148 types_htab = allocate_signatured_type_table (objfile);
4149 }
348e048f 4150
dee91e82
DE
4151 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4152 because we don't need to read any dies: the signature is in the
4153 header. */
8b70b953
TT
4154
4155 end_ptr = info_ptr + section->size;
4156 while (info_ptr < end_ptr)
4157 {
b64f50a1 4158 sect_offset offset;
3019eac3 4159 cu_offset type_offset_in_tu;
8b70b953 4160 ULONGEST signature;
52dc124a 4161 struct signatured_type *sig_type;
3019eac3 4162 struct dwo_unit *dwo_tu;
8b70b953
TT
4163 void **slot;
4164 gdb_byte *ptr = info_ptr;
9ff913ba 4165 struct comp_unit_head header;
dee91e82 4166 unsigned int length;
348e048f 4167
b64f50a1 4168 offset.sect_off = ptr - section->buffer;
348e048f 4169
8b70b953 4170 /* We need to read the type's signature in order to build the hash
9ff913ba 4171 table, but we don't need anything else just yet. */
348e048f 4172
4bdcc0c1
DE
4173 ptr = read_and_check_type_unit_head (&header, section,
4174 abbrev_section, ptr,
3019eac3 4175 &signature, &type_offset_in_tu);
6caca83c 4176
1ce1cefd 4177 length = get_cu_length (&header);
dee91e82 4178
6caca83c 4179 /* Skip dummy type units. */
dee91e82
DE
4180 if (ptr >= info_ptr + length
4181 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4182 {
1ce1cefd 4183 info_ptr += length;
6caca83c
CC
4184 continue;
4185 }
8b70b953 4186
3019eac3
DE
4187 if (dwo_file)
4188 {
4189 sig_type = NULL;
4190 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4191 struct dwo_unit);
4192 dwo_tu->dwo_file = dwo_file;
4193 dwo_tu->signature = signature;
4194 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4195 dwo_tu->info_or_types_section = section;
4196 dwo_tu->offset = offset;
4197 dwo_tu->length = length;
4198 }
4199 else
4200 {
4201 /* N.B.: type_offset is not usable if this type uses a DWO file.
4202 The real type_offset is in the DWO file. */
4203 dwo_tu = NULL;
4204 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4205 struct signatured_type);
4206 sig_type->signature = signature;
4207 sig_type->type_offset_in_tu = type_offset_in_tu;
4208 sig_type->per_cu.objfile = objfile;
4209 sig_type->per_cu.is_debug_types = 1;
4210 sig_type->per_cu.info_or_types_section = section;
4211 sig_type->per_cu.offset = offset;
4212 sig_type->per_cu.length = length;
4213 }
8b70b953 4214
3019eac3
DE
4215 slot = htab_find_slot (types_htab,
4216 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4217 INSERT);
8b70b953
TT
4218 gdb_assert (slot != NULL);
4219 if (*slot != NULL)
4220 {
3019eac3
DE
4221 sect_offset dup_offset;
4222
4223 if (dwo_file)
4224 {
4225 const struct dwo_unit *dup_tu = *slot;
4226
4227 dup_offset = dup_tu->offset;
4228 }
4229 else
4230 {
4231 const struct signatured_type *dup_tu = *slot;
4232
4233 dup_offset = dup_tu->per_cu.offset;
4234 }
b3c8eb43 4235
8b70b953
TT
4236 complaint (&symfile_complaints,
4237 _("debug type entry at offset 0x%x is duplicate to the "
4238 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4239 offset.sect_off, dup_offset.sect_off,
8b70b953 4240 phex (signature, sizeof (signature)));
8b70b953 4241 }
3019eac3 4242 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4243
09406207 4244 if (dwarf2_read_debug)
8b70b953 4245 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4246 offset.sect_off,
4247 phex (signature, sizeof (signature)));
348e048f 4248
dee91e82 4249 info_ptr += length;
8b70b953 4250 }
348e048f
DE
4251 }
4252
3019eac3
DE
4253 return types_htab;
4254}
4255
4256/* Create the hash table of all entries in the .debug_types section,
4257 and initialize all_type_units.
4258 The result is zero if there is an error (e.g. missing .debug_types section),
4259 otherwise non-zero. */
4260
4261static int
4262create_all_type_units (struct objfile *objfile)
4263{
4264 htab_t types_htab;
b4dd5633 4265 struct signatured_type **iter;
3019eac3
DE
4266
4267 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4268 if (types_htab == NULL)
4269 {
4270 dwarf2_per_objfile->signatured_types = NULL;
4271 return 0;
4272 }
4273
348e048f
DE
4274 dwarf2_per_objfile->signatured_types = types_htab;
4275
d467dd73
DE
4276 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4277 dwarf2_per_objfile->all_type_units
1fd400ff 4278 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4279 dwarf2_per_objfile->n_type_units
b4dd5633 4280 * sizeof (struct signatured_type *));
d467dd73
DE
4281 iter = &dwarf2_per_objfile->all_type_units[0];
4282 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4283 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4284 == dwarf2_per_objfile->n_type_units);
1fd400ff 4285
348e048f
DE
4286 return 1;
4287}
4288
380bca97 4289/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4290 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4291
4292static struct signatured_type *
e319fa28 4293lookup_signatured_type (ULONGEST sig)
348e048f
DE
4294{
4295 struct signatured_type find_entry, *entry;
4296
4297 if (dwarf2_per_objfile->signatured_types == NULL)
4298 {
4299 complaint (&symfile_complaints,
55f1336d 4300 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4301 return NULL;
348e048f
DE
4302 }
4303
4304 find_entry.signature = sig;
4305 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4306 return entry;
4307}
42e7ad6c
DE
4308\f
4309/* Low level DIE reading support. */
348e048f 4310
d85a05f0
DJ
4311/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4312
4313static void
4314init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4315 struct dwarf2_cu *cu,
3019eac3
DE
4316 struct dwarf2_section_info *section,
4317 struct dwo_file *dwo_file)
d85a05f0 4318{
fceca515 4319 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4320 reader->abfd = section->asection->owner;
d85a05f0 4321 reader->cu = cu;
3019eac3 4322 reader->dwo_file = dwo_file;
dee91e82
DE
4323 reader->die_section = section;
4324 reader->buffer = section->buffer;
f664829e 4325 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4326}
4327
fd820528 4328/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4329 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4330
f4dc4d17
DE
4331 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4332 Otherwise the table specified in the comp unit header is read in and used.
4333 This is an optimization for when we already have the abbrev table.
4334
dee91e82
DE
4335 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4336 Otherwise, a new CU is allocated with xmalloc.
4337
4338 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4339 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4340
4341 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4342 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4343
70221824 4344static void
fd820528 4345init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4346 struct abbrev_table *abbrev_table,
fd820528
DE
4347 int use_existing_cu, int keep,
4348 die_reader_func_ftype *die_reader_func,
4349 void *data)
c906108c 4350{
dee91e82 4351 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4352 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4353 bfd *abfd = section->asection->owner;
dee91e82
DE
4354 struct dwarf2_cu *cu;
4355 gdb_byte *begin_info_ptr, *info_ptr;
4356 struct die_reader_specs reader;
d85a05f0 4357 struct die_info *comp_unit_die;
dee91e82 4358 int has_children;
d85a05f0 4359 struct attribute *attr;
dee91e82
DE
4360 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4361 struct signatured_type *sig_type = NULL;
4bdcc0c1 4362 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4363 /* Non-zero if CU currently points to a DWO file and we need to
4364 reread it. When this happens we need to reread the skeleton die
4365 before we can reread the DWO file. */
4366 int rereading_dwo_cu = 0;
c906108c 4367
09406207
DE
4368 if (dwarf2_die_debug)
4369 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4370 this_cu->is_debug_types ? "type" : "comp",
4371 this_cu->offset.sect_off);
4372
dee91e82
DE
4373 if (use_existing_cu)
4374 gdb_assert (keep);
23745b47 4375
dee91e82
DE
4376 cleanups = make_cleanup (null_cleanup, NULL);
4377
4378 /* This is cheap if the section is already read in. */
4379 dwarf2_read_section (objfile, section);
4380
4381 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4382
4383 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4384
4385 if (use_existing_cu && this_cu->cu != NULL)
4386 {
4387 cu = this_cu->cu;
42e7ad6c
DE
4388
4389 /* If this CU is from a DWO file we need to start over, we need to
4390 refetch the attributes from the skeleton CU.
4391 This could be optimized by retrieving those attributes from when we
4392 were here the first time: the previous comp_unit_die was stored in
4393 comp_unit_obstack. But there's no data yet that we need this
4394 optimization. */
4395 if (cu->dwo_unit != NULL)
4396 rereading_dwo_cu = 1;
dee91e82
DE
4397 }
4398 else
4399 {
4400 /* If !use_existing_cu, this_cu->cu must be NULL. */
4401 gdb_assert (this_cu->cu == NULL);
4402
4403 cu = xmalloc (sizeof (*cu));
4404 init_one_comp_unit (cu, this_cu);
4405
4406 /* If an error occurs while loading, release our storage. */
4407 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4408 }
dee91e82 4409
42e7ad6c
DE
4410 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4411 {
4412 /* We already have the header, there's no need to read it in again. */
4413 info_ptr += cu->header.first_die_offset.cu_off;
4414 }
4415 else
4416 {
3019eac3 4417 if (this_cu->is_debug_types)
dee91e82
DE
4418 {
4419 ULONGEST signature;
42e7ad6c 4420 cu_offset type_offset_in_tu;
dee91e82 4421
4bdcc0c1
DE
4422 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4423 abbrev_section, info_ptr,
42e7ad6c
DE
4424 &signature,
4425 &type_offset_in_tu);
dee91e82 4426
42e7ad6c
DE
4427 /* Since per_cu is the first member of struct signatured_type,
4428 we can go from a pointer to one to a pointer to the other. */
4429 sig_type = (struct signatured_type *) this_cu;
4430 gdb_assert (sig_type->signature == signature);
4431 gdb_assert (sig_type->type_offset_in_tu.cu_off
4432 == type_offset_in_tu.cu_off);
dee91e82
DE
4433 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4434
42e7ad6c
DE
4435 /* LENGTH has not been set yet for type units if we're
4436 using .gdb_index. */
1ce1cefd 4437 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4438
4439 /* Establish the type offset that can be used to lookup the type. */
4440 sig_type->type_offset_in_section.sect_off =
4441 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4442 }
4443 else
4444 {
4bdcc0c1
DE
4445 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4446 abbrev_section,
4447 info_ptr, 0);
dee91e82
DE
4448
4449 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4450 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4451 }
4452 }
10b3939b 4453
6caca83c 4454 /* Skip dummy compilation units. */
dee91e82 4455 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4456 || peek_abbrev_code (abfd, info_ptr) == 0)
4457 {
dee91e82 4458 do_cleanups (cleanups);
21b2bd31 4459 return;
6caca83c
CC
4460 }
4461
433df2d4
DE
4462 /* If we don't have them yet, read the abbrevs for this compilation unit.
4463 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4464 done. Note that it's important that if the CU had an abbrev table
4465 on entry we don't free it when we're done: Somewhere up the call stack
4466 it may be in use. */
f4dc4d17
DE
4467 if (abbrev_table != NULL)
4468 {
4469 gdb_assert (cu->abbrev_table == NULL);
4470 gdb_assert (cu->header.abbrev_offset.sect_off
4471 == abbrev_table->offset.sect_off);
4472 cu->abbrev_table = abbrev_table;
4473 }
4474 else if (cu->abbrev_table == NULL)
dee91e82 4475 {
4bdcc0c1 4476 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4477 make_cleanup (dwarf2_free_abbrev_table, cu);
4478 }
42e7ad6c
DE
4479 else if (rereading_dwo_cu)
4480 {
4481 dwarf2_free_abbrev_table (cu);
4482 dwarf2_read_abbrevs (cu, abbrev_section);
4483 }
af703f96 4484
dee91e82 4485 /* Read the top level CU/TU die. */
3019eac3 4486 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4487 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4488
3019eac3
DE
4489 /* If we have a DWO stub, process it and then read in the DWO file.
4490 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4491 a DWO CU, that this test will fail. */
4492 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4493 if (attr)
4494 {
4495 char *dwo_name = DW_STRING (attr);
42e7ad6c 4496 const char *comp_dir_string;
3019eac3
DE
4497 struct dwo_unit *dwo_unit;
4498 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4499 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4500 int i,num_extra_attrs;
4bdcc0c1 4501 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4502
4503 if (has_children)
4504 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4505 " has children (offset 0x%x) [in module %s]"),
4506 this_cu->offset.sect_off, bfd_get_filename (abfd));
4507
4508 /* These attributes aren't processed until later:
4509 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4510 However, the attribute is found in the stub which we won't have later.
4511 In order to not impose this complication on the rest of the code,
4512 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4513
4514 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4515 DWO file. */
42e7ad6c 4516 stmt_list = NULL;
3019eac3
DE
4517 if (! this_cu->is_debug_types)
4518 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4519 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4520 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4521 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4522 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4523
4524 /* There should be a DW_AT_addr_base attribute here (if needed).
4525 We need the value before we can process DW_FORM_GNU_addr_index. */
4526 cu->addr_base = 0;
3019eac3
DE
4527 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4528 if (attr)
2e3cf129
DE
4529 cu->addr_base = DW_UNSND (attr);
4530
4531 /* There should be a DW_AT_ranges_base attribute here (if needed).
4532 We need the value before we can process DW_AT_ranges. */
4533 cu->ranges_base = 0;
4534 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4535 if (attr)
4536 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4537
4538 if (this_cu->is_debug_types)
4539 {
4540 gdb_assert (sig_type != NULL);
4541 signature = sig_type->signature;
4542 }
4543 else
4544 {
4545 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4546 if (! attr)
4547 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4548 dwo_name);
4549 signature = DW_UNSND (attr);
4550 }
4551
4552 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4553 comp_dir_string = NULL;
4554 if (comp_dir)
4555 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4556
4557 if (this_cu->is_debug_types)
42e7ad6c 4558 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4559 else
42e7ad6c 4560 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4561 signature);
4562
4563 if (dwo_unit == NULL)
4564 {
4565 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4566 " with ID %s [in module %s]"),
4567 this_cu->offset.sect_off,
4568 phex (signature, sizeof (signature)),
4569 objfile->name);
4570 }
4571
4572 /* Set up for reading the DWO CU/TU. */
4573 cu->dwo_unit = dwo_unit;
4574 section = dwo_unit->info_or_types_section;
80626a55 4575 dwarf2_read_section (objfile, section);
3019eac3 4576 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4577 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4578 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4579
4580 if (this_cu->is_debug_types)
4581 {
4582 ULONGEST signature;
80626a55 4583 cu_offset type_offset_in_tu;
3019eac3 4584
4bdcc0c1
DE
4585 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4586 dwo_abbrev_section,
4587 info_ptr,
80626a55
DE
4588 &signature,
4589 &type_offset_in_tu);
3019eac3
DE
4590 gdb_assert (sig_type->signature == signature);
4591 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4592 /* For DWOs coming from DWP files, we don't know the CU length
4593 nor the type's offset in the TU until now. */
4594 dwo_unit->length = get_cu_length (&cu->header);
4595 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4596
4597 /* Establish the type offset that can be used to lookup the type.
4598 For DWO files, we don't know it until now. */
4599 sig_type->type_offset_in_section.sect_off =
4600 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4601 }
4602 else
4603 {
4bdcc0c1
DE
4604 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4605 dwo_abbrev_section,
4606 info_ptr, 0);
3019eac3 4607 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4608 /* For DWOs coming from DWP files, we don't know the CU length
4609 until now. */
4610 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4611 }
4612
4613 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4614 if (abbrev_table == NULL)
4615 {
4616 dwarf2_free_abbrev_table (cu);
4617 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4618 }
4619 else
4620 {
4621 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4622 make_cleanup (dwarf2_free_abbrev_table, cu);
4623 }
3019eac3
DE
4624
4625 /* Read in the die, but leave space to copy over the attributes
4626 from the stub. This has the benefit of simplifying the rest of
4627 the code - all the real work is done here. */
4628 num_extra_attrs = ((stmt_list != NULL)
4629 + (low_pc != NULL)
4630 + (high_pc != NULL)
42e7ad6c
DE
4631 + (ranges != NULL)
4632 + (comp_dir != NULL));
3019eac3
DE
4633 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4634 &has_children, num_extra_attrs);
4635
4636 /* Copy over the attributes from the stub to the DWO die. */
4637 i = comp_unit_die->num_attrs;
4638 if (stmt_list != NULL)
4639 comp_unit_die->attrs[i++] = *stmt_list;
4640 if (low_pc != NULL)
4641 comp_unit_die->attrs[i++] = *low_pc;
4642 if (high_pc != NULL)
4643 comp_unit_die->attrs[i++] = *high_pc;
4644 if (ranges != NULL)
4645 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4646 if (comp_dir != NULL)
4647 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4648 comp_unit_die->num_attrs += num_extra_attrs;
4649
4650 /* Skip dummy compilation units. */
4651 if (info_ptr >= begin_info_ptr + dwo_unit->length
4652 || peek_abbrev_code (abfd, info_ptr) == 0)
4653 {
4654 do_cleanups (cleanups);
4655 return;
4656 }
4657 }
4658
dee91e82
DE
4659 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4660
4661 if (free_cu_cleanup != NULL)
348e048f 4662 {
dee91e82
DE
4663 if (keep)
4664 {
4665 /* We've successfully allocated this compilation unit. Let our
4666 caller clean it up when finished with it. */
4667 discard_cleanups (free_cu_cleanup);
4668
4669 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4670 So we have to manually free the abbrev table. */
4671 dwarf2_free_abbrev_table (cu);
4672
4673 /* Link this CU into read_in_chain. */
4674 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4675 dwarf2_per_objfile->read_in_chain = this_cu;
4676 }
4677 else
4678 do_cleanups (free_cu_cleanup);
348e048f 4679 }
dee91e82
DE
4680
4681 do_cleanups (cleanups);
4682}
4683
3019eac3
DE
4684/* Read CU/TU THIS_CU in section SECTION,
4685 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4686 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4687 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4688
4689 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4690 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4691
4692 We fill in THIS_CU->length.
4693
4694 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4695 linker) then DIE_READER_FUNC will not get called.
4696
4697 THIS_CU->cu is always freed when done.
3019eac3
DE
4698 This is done in order to not leave THIS_CU->cu in a state where we have
4699 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4700
4701static void
4702init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4703 struct dwarf2_section_info *abbrev_section,
3019eac3 4704 struct dwo_file *dwo_file,
dee91e82
DE
4705 die_reader_func_ftype *die_reader_func,
4706 void *data)
4707{
4708 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4709 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4710 bfd *abfd = section->asection->owner;
dee91e82
DE
4711 struct dwarf2_cu cu;
4712 gdb_byte *begin_info_ptr, *info_ptr;
4713 struct die_reader_specs reader;
4714 struct cleanup *cleanups;
4715 struct die_info *comp_unit_die;
4716 int has_children;
4717
09406207
DE
4718 if (dwarf2_die_debug)
4719 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4720 this_cu->is_debug_types ? "type" : "comp",
4721 this_cu->offset.sect_off);
4722
dee91e82
DE
4723 gdb_assert (this_cu->cu == NULL);
4724
dee91e82
DE
4725 /* This is cheap if the section is already read in. */
4726 dwarf2_read_section (objfile, section);
4727
4728 init_one_comp_unit (&cu, this_cu);
4729
4730 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4731
4732 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4733 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4734 abbrev_section, info_ptr,
3019eac3 4735 this_cu->is_debug_types);
dee91e82 4736
1ce1cefd 4737 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4738
4739 /* Skip dummy compilation units. */
4740 if (info_ptr >= begin_info_ptr + this_cu->length
4741 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4742 {
dee91e82 4743 do_cleanups (cleanups);
21b2bd31 4744 return;
93311388 4745 }
72bf9492 4746
dee91e82
DE
4747 dwarf2_read_abbrevs (&cu, abbrev_section);
4748 make_cleanup (dwarf2_free_abbrev_table, &cu);
4749
3019eac3 4750 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4751 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4752
4753 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4754
4755 do_cleanups (cleanups);
4756}
4757
3019eac3
DE
4758/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4759 does not lookup the specified DWO file.
4760 This cannot be used to read DWO files.
dee91e82
DE
4761
4762 THIS_CU->cu is always freed when done.
3019eac3
DE
4763 This is done in order to not leave THIS_CU->cu in a state where we have
4764 to care whether it refers to the "main" CU or the DWO CU.
4765 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4766
4767static void
4768init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4769 die_reader_func_ftype *die_reader_func,
4770 void *data)
4771{
4772 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4773 get_abbrev_section_for_cu (this_cu),
3019eac3 4774 NULL,
dee91e82
DE
4775 die_reader_func, data);
4776}
4777
f4dc4d17
DE
4778/* Create a psymtab named NAME and assign it to PER_CU.
4779
4780 The caller must fill in the following details:
4781 dirname, textlow, texthigh. */
4782
4783static struct partial_symtab *
4784create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4785{
4786 struct objfile *objfile = per_cu->objfile;
4787 struct partial_symtab *pst;
4788
4789 pst = start_psymtab_common (objfile, objfile->section_offsets,
4790 name, 0,
4791 objfile->global_psymbols.next,
4792 objfile->static_psymbols.next);
4793
4794 pst->psymtabs_addrmap_supported = 1;
4795
4796 /* This is the glue that links PST into GDB's symbol API. */
4797 pst->read_symtab_private = per_cu;
4798 pst->read_symtab = dwarf2_psymtab_to_symtab;
4799 per_cu->v.psymtab = pst;
4800
4801 return pst;
4802}
4803
dee91e82
DE
4804/* die_reader_func for process_psymtab_comp_unit. */
4805
4806static void
4807process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4808 gdb_byte *info_ptr,
4809 struct die_info *comp_unit_die,
4810 int has_children,
4811 void *data)
4812{
4813 struct dwarf2_cu *cu = reader->cu;
4814 struct objfile *objfile = cu->objfile;
4815 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4816 struct attribute *attr;
4817 CORE_ADDR baseaddr;
4818 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4819 struct partial_symtab *pst;
4820 int has_pc_info;
4821 const char *filename;
95554aad 4822 int *want_partial_unit_ptr = data;
dee91e82 4823
95554aad
TT
4824 if (comp_unit_die->tag == DW_TAG_partial_unit
4825 && (want_partial_unit_ptr == NULL
4826 || !*want_partial_unit_ptr))
dee91e82
DE
4827 return;
4828
f4dc4d17
DE
4829 gdb_assert (! per_cu->is_debug_types);
4830
95554aad 4831 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4832
4833 cu->list_in_scope = &file_symbols;
c906108c 4834
93311388 4835 /* Allocate a new partial symbol table structure. */
dee91e82 4836 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4837 if (attr == NULL || !DW_STRING (attr))
4838 filename = "";
4839 else
4840 filename = DW_STRING (attr);
72bf9492 4841
f4dc4d17
DE
4842 pst = create_partial_symtab (per_cu, filename);
4843
4844 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4845 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4846 if (attr != NULL)
4847 pst->dirname = DW_STRING (attr);
72bf9492 4848
93311388 4849 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4850
dee91e82 4851 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4852
93311388
DE
4853 /* Possibly set the default values of LOWPC and HIGHPC from
4854 `DW_AT_ranges'. */
d85a05f0 4855 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4856 &best_highpc, cu, pst);
d85a05f0 4857 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4858 /* Store the contiguous range if it is not empty; it can be empty for
4859 CUs with no code. */
4860 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4861 best_lowpc + baseaddr,
4862 best_highpc + baseaddr - 1, pst);
93311388
DE
4863
4864 /* Check if comp unit has_children.
4865 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4866 If not, there's no more debug_info for this comp unit. */
d85a05f0 4867 if (has_children)
93311388
DE
4868 {
4869 struct partial_die_info *first_die;
4870 CORE_ADDR lowpc, highpc;
31ffec48 4871
93311388
DE
4872 lowpc = ((CORE_ADDR) -1);
4873 highpc = ((CORE_ADDR) 0);
c906108c 4874
dee91e82 4875 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4876
93311388 4877 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4878 ! has_pc_info, cu);
57c22c6c 4879
93311388
DE
4880 /* If we didn't find a lowpc, set it to highpc to avoid
4881 complaints from `maint check'. */
4882 if (lowpc == ((CORE_ADDR) -1))
4883 lowpc = highpc;
10b3939b 4884
93311388
DE
4885 /* If the compilation unit didn't have an explicit address range,
4886 then use the information extracted from its child dies. */
d85a05f0 4887 if (! has_pc_info)
93311388 4888 {
d85a05f0
DJ
4889 best_lowpc = lowpc;
4890 best_highpc = highpc;
93311388
DE
4891 }
4892 }
d85a05f0
DJ
4893 pst->textlow = best_lowpc + baseaddr;
4894 pst->texthigh = best_highpc + baseaddr;
c906108c 4895
93311388
DE
4896 pst->n_global_syms = objfile->global_psymbols.next -
4897 (objfile->global_psymbols.list + pst->globals_offset);
4898 pst->n_static_syms = objfile->static_psymbols.next -
4899 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 4900 sort_pst_symbols (objfile, pst);
c906108c 4901
f4dc4d17 4902 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4903 {
4904 int i;
f4dc4d17 4905 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4906 struct dwarf2_per_cu_data *iter;
4907
4908 /* Fill in 'dependencies' here; we fill in 'users' in a
4909 post-pass. */
4910 pst->number_of_dependencies = len;
4911 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4912 len * sizeof (struct symtab *));
4913 for (i = 0;
f4dc4d17 4914 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4915 i, iter);
4916 ++i)
4917 pst->dependencies[i] = iter->v.psymtab;
4918
f4dc4d17 4919 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4920 }
4921
f4dc4d17
DE
4922 /* Get the list of files included in the current compilation unit,
4923 and build a psymtab for each of them. */
4924 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4925
4926 if (dwarf2_read_debug)
4927 {
4928 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4929
4930 fprintf_unfiltered (gdb_stdlog,
844226d6 4931 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4932 ", %d global, %d static syms\n",
4933 per_cu->is_debug_types ? "type" : "comp",
4934 per_cu->offset.sect_off,
4935 paddress (gdbarch, pst->textlow),
4936 paddress (gdbarch, pst->texthigh),
4937 pst->n_global_syms, pst->n_static_syms);
4938 }
dee91e82 4939}
ae038cb0 4940
dee91e82
DE
4941/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4942 Process compilation unit THIS_CU for a psymtab. */
4943
4944static void
95554aad
TT
4945process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4946 int want_partial_unit)
dee91e82
DE
4947{
4948 /* If this compilation unit was already read in, free the
4949 cached copy in order to read it in again. This is
4950 necessary because we skipped some symbols when we first
4951 read in the compilation unit (see load_partial_dies).
4952 This problem could be avoided, but the benefit is unclear. */
4953 if (this_cu->cu != NULL)
4954 free_one_cached_comp_unit (this_cu);
4955
3019eac3 4956 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
4957 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4958 process_psymtab_comp_unit_reader,
95554aad 4959 &want_partial_unit);
dee91e82
DE
4960
4961 /* Age out any secondary CUs. */
4962 age_cached_comp_units ();
93311388 4963}
ff013f42 4964
f4dc4d17
DE
4965static hashval_t
4966hash_type_unit_group (const void *item)
4967{
094b34ac 4968 const struct type_unit_group *tu_group = item;
f4dc4d17 4969
094b34ac 4970 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4971}
348e048f
DE
4972
4973static int
f4dc4d17 4974eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4975{
f4dc4d17
DE
4976 const struct type_unit_group *lhs = item_lhs;
4977 const struct type_unit_group *rhs = item_rhs;
348e048f 4978
094b34ac 4979 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4980}
348e048f 4981
f4dc4d17
DE
4982/* Allocate a hash table for type unit groups. */
4983
4984static htab_t
4985allocate_type_unit_groups_table (void)
4986{
4987 return htab_create_alloc_ex (3,
4988 hash_type_unit_group,
4989 eq_type_unit_group,
4990 NULL,
4991 &dwarf2_per_objfile->objfile->objfile_obstack,
4992 hashtab_obstack_allocate,
4993 dummy_obstack_deallocate);
4994}
dee91e82 4995
f4dc4d17
DE
4996/* Type units that don't have DW_AT_stmt_list are grouped into their own
4997 partial symtabs. We combine several TUs per psymtab to not let the size
4998 of any one psymtab grow too big. */
4999#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5000#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5001
094b34ac 5002/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5003 Create the type_unit_group object used to hold one or more TUs. */
5004
5005static struct type_unit_group *
094b34ac 5006create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5007{
5008 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5009 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5010 struct type_unit_group *tu_group;
f4dc4d17
DE
5011
5012 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5013 struct type_unit_group);
094b34ac 5014 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5015 per_cu->objfile = objfile;
5016 per_cu->is_debug_types = 1;
5017 per_cu->s.type_unit_group = tu_group;
5018
094b34ac
DE
5019 if (dwarf2_per_objfile->using_index)
5020 {
5021 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5022 struct dwarf2_per_cu_quick_data);
5023 tu_group->t.first_tu = cu->per_cu;
5024 }
5025 else
5026 {
5027 unsigned int line_offset = line_offset_struct.sect_off;
5028 struct partial_symtab *pst;
5029 char *name;
5030
5031 /* Give the symtab a useful name for debug purposes. */
5032 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5033 name = xstrprintf ("<type_units_%d>",
5034 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5035 else
5036 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5037
5038 pst = create_partial_symtab (per_cu, name);
5039 pst->anonymous = 1;
f4dc4d17 5040
094b34ac
DE
5041 xfree (name);
5042 }
f4dc4d17 5043
094b34ac
DE
5044 tu_group->hash.dwo_unit = cu->dwo_unit;
5045 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5046
5047 return tu_group;
5048}
5049
094b34ac
DE
5050/* Look up the type_unit_group for type unit CU, and create it if necessary.
5051 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5052
5053static struct type_unit_group *
094b34ac 5054get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5055{
5056 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5057 struct type_unit_group *tu_group;
5058 void **slot;
5059 unsigned int line_offset;
5060 struct type_unit_group type_unit_group_for_lookup;
5061
5062 if (dwarf2_per_objfile->type_unit_groups == NULL)
5063 {
5064 dwarf2_per_objfile->type_unit_groups =
5065 allocate_type_unit_groups_table ();
5066 }
5067
5068 /* Do we need to create a new group, or can we use an existing one? */
5069
5070 if (stmt_list)
5071 {
5072 line_offset = DW_UNSND (stmt_list);
5073 ++tu_stats->nr_symtab_sharers;
5074 }
5075 else
5076 {
5077 /* Ugh, no stmt_list. Rare, but we have to handle it.
5078 We can do various things here like create one group per TU or
5079 spread them over multiple groups to split up the expansion work.
5080 To avoid worst case scenarios (too many groups or too large groups)
5081 we, umm, group them in bunches. */
5082 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5083 | (tu_stats->nr_stmt_less_type_units
5084 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5085 ++tu_stats->nr_stmt_less_type_units;
5086 }
5087
094b34ac
DE
5088 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5089 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5090 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5091 &type_unit_group_for_lookup, INSERT);
5092 if (*slot != NULL)
5093 {
5094 tu_group = *slot;
5095 gdb_assert (tu_group != NULL);
5096 }
5097 else
5098 {
5099 sect_offset line_offset_struct;
5100
5101 line_offset_struct.sect_off = line_offset;
094b34ac 5102 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5103 *slot = tu_group;
5104 ++tu_stats->nr_symtabs;
5105 }
5106
5107 return tu_group;
5108}
5109
5110/* Struct used to sort TUs by their abbreviation table offset. */
5111
5112struct tu_abbrev_offset
5113{
5114 struct signatured_type *sig_type;
5115 sect_offset abbrev_offset;
5116};
5117
5118/* Helper routine for build_type_unit_groups, passed to qsort. */
5119
5120static int
5121sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5122{
5123 const struct tu_abbrev_offset * const *a = ap;
5124 const struct tu_abbrev_offset * const *b = bp;
5125 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5126 unsigned int boff = (*b)->abbrev_offset.sect_off;
5127
5128 return (aoff > boff) - (aoff < boff);
5129}
5130
5131/* A helper function to add a type_unit_group to a table. */
5132
5133static int
5134add_type_unit_group_to_table (void **slot, void *datum)
5135{
5136 struct type_unit_group *tu_group = *slot;
5137 struct type_unit_group ***datap = datum;
5138
5139 **datap = tu_group;
5140 ++*datap;
5141
5142 return 1;
5143}
5144
5145/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5146 each one passing FUNC,DATA.
5147
5148 The efficiency is because we sort TUs by the abbrev table they use and
5149 only read each abbrev table once. In one program there are 200K TUs
5150 sharing 8K abbrev tables.
5151
5152 The main purpose of this function is to support building the
5153 dwarf2_per_objfile->type_unit_groups table.
5154 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5155 can collapse the search space by grouping them by stmt_list.
5156 The savings can be significant, in the same program from above the 200K TUs
5157 share 8K stmt_list tables.
5158
5159 FUNC is expected to call get_type_unit_group, which will create the
5160 struct type_unit_group if necessary and add it to
5161 dwarf2_per_objfile->type_unit_groups. */
5162
5163static void
5164build_type_unit_groups (die_reader_func_ftype *func, void *data)
5165{
5166 struct objfile *objfile = dwarf2_per_objfile->objfile;
5167 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5168 struct cleanup *cleanups;
5169 struct abbrev_table *abbrev_table;
5170 sect_offset abbrev_offset;
5171 struct tu_abbrev_offset *sorted_by_abbrev;
5172 struct type_unit_group **iter;
5173 int i;
5174
5175 /* It's up to the caller to not call us multiple times. */
5176 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5177
5178 if (dwarf2_per_objfile->n_type_units == 0)
5179 return;
5180
5181 /* TUs typically share abbrev tables, and there can be way more TUs than
5182 abbrev tables. Sort by abbrev table to reduce the number of times we
5183 read each abbrev table in.
5184 Alternatives are to punt or to maintain a cache of abbrev tables.
5185 This is simpler and efficient enough for now.
5186
5187 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5188 symtab to use). Typically TUs with the same abbrev offset have the same
5189 stmt_list value too so in practice this should work well.
5190
5191 The basic algorithm here is:
5192
5193 sort TUs by abbrev table
5194 for each TU with same abbrev table:
5195 read abbrev table if first user
5196 read TU top level DIE
5197 [IWBN if DWO skeletons had DW_AT_stmt_list]
5198 call FUNC */
5199
5200 if (dwarf2_read_debug)
5201 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5202
5203 /* Sort in a separate table to maintain the order of all_type_units
5204 for .gdb_index: TU indices directly index all_type_units. */
5205 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5206 dwarf2_per_objfile->n_type_units);
5207 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5208 {
5209 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5210
5211 sorted_by_abbrev[i].sig_type = sig_type;
5212 sorted_by_abbrev[i].abbrev_offset =
5213 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5214 sig_type->per_cu.offset);
5215 }
5216 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5217 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5218 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5219
094b34ac
DE
5220 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5221 called any number of times, so we don't reset tu_stats here. */
5222
f4dc4d17
DE
5223 abbrev_offset.sect_off = ~(unsigned) 0;
5224 abbrev_table = NULL;
5225 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5226
5227 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5228 {
5229 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5230
5231 /* Switch to the next abbrev table if necessary. */
5232 if (abbrev_table == NULL
5233 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5234 {
5235 if (abbrev_table != NULL)
5236 {
5237 abbrev_table_free (abbrev_table);
5238 /* Reset to NULL in case abbrev_table_read_table throws
5239 an error: abbrev_table_free_cleanup will get called. */
5240 abbrev_table = NULL;
5241 }
5242 abbrev_offset = tu->abbrev_offset;
5243 abbrev_table =
5244 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5245 abbrev_offset);
5246 ++tu_stats->nr_uniq_abbrev_tables;
5247 }
5248
5249 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5250 func, data);
5251 }
5252
5253 /* Create a vector of pointers to primary type units to make it easy to
5254 iterate over them and CUs. See dw2_get_primary_cu. */
5255 dwarf2_per_objfile->n_type_unit_groups =
5256 htab_elements (dwarf2_per_objfile->type_unit_groups);
5257 dwarf2_per_objfile->all_type_unit_groups =
5258 obstack_alloc (&objfile->objfile_obstack,
5259 dwarf2_per_objfile->n_type_unit_groups
5260 * sizeof (struct type_unit_group *));
5261 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5262 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5263 add_type_unit_group_to_table, &iter);
5264 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5265 == dwarf2_per_objfile->n_type_unit_groups);
5266
5267 do_cleanups (cleanups);
5268
5269 if (dwarf2_read_debug)
5270 {
5271 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5272 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5273 dwarf2_per_objfile->n_type_units);
5274 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5275 tu_stats->nr_uniq_abbrev_tables);
5276 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5277 tu_stats->nr_symtabs);
5278 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5279 tu_stats->nr_symtab_sharers);
5280 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5281 tu_stats->nr_stmt_less_type_units);
5282 }
5283}
5284
5285/* Reader function for build_type_psymtabs. */
5286
5287static void
5288build_type_psymtabs_reader (const struct die_reader_specs *reader,
5289 gdb_byte *info_ptr,
5290 struct die_info *type_unit_die,
5291 int has_children,
5292 void *data)
5293{
5294 struct objfile *objfile = dwarf2_per_objfile->objfile;
5295 struct dwarf2_cu *cu = reader->cu;
5296 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5297 struct type_unit_group *tu_group;
5298 struct attribute *attr;
5299 struct partial_die_info *first_die;
5300 CORE_ADDR lowpc, highpc;
5301 struct partial_symtab *pst;
5302
5303 gdb_assert (data == NULL);
5304
5305 if (! has_children)
5306 return;
5307
5308 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5309 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5310
094b34ac 5311 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5312
5313 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5314 cu->list_in_scope = &file_symbols;
5315 pst = create_partial_symtab (per_cu, "");
5316 pst->anonymous = 1;
5317
5318 first_die = load_partial_dies (reader, info_ptr, 1);
5319
5320 lowpc = (CORE_ADDR) -1;
5321 highpc = (CORE_ADDR) 0;
5322 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5323
5324 pst->n_global_syms = objfile->global_psymbols.next -
5325 (objfile->global_psymbols.list + pst->globals_offset);
5326 pst->n_static_syms = objfile->static_psymbols.next -
5327 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5328 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5329}
5330
5331/* Traversal function for build_type_psymtabs. */
5332
5333static int
5334build_type_psymtab_dependencies (void **slot, void *info)
5335{
5336 struct objfile *objfile = dwarf2_per_objfile->objfile;
5337 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5338 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5339 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5340 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5341 struct dwarf2_per_cu_data *iter;
5342 int i;
5343
5344 gdb_assert (len > 0);
5345
5346 pst->number_of_dependencies = len;
5347 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5348 len * sizeof (struct psymtab *));
5349 for (i = 0;
094b34ac 5350 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5351 ++i)
5352 {
5353 pst->dependencies[i] = iter->v.psymtab;
5354 iter->s.type_unit_group = tu_group;
5355 }
5356
094b34ac 5357 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5358
5359 return 1;
5360}
5361
5362/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5363 Build partial symbol tables for the .debug_types comp-units. */
5364
5365static void
5366build_type_psymtabs (struct objfile *objfile)
5367{
0e50663e 5368 if (! create_all_type_units (objfile))
348e048f
DE
5369 return;
5370
f4dc4d17
DE
5371 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5372
5373 /* Now that all TUs have been processed we can fill in the dependencies. */
5374 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5375 build_type_psymtab_dependencies, NULL);
348e048f
DE
5376}
5377
60606b2c
TT
5378/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5379
5380static void
5381psymtabs_addrmap_cleanup (void *o)
5382{
5383 struct objfile *objfile = o;
ec61707d 5384
60606b2c
TT
5385 objfile->psymtabs_addrmap = NULL;
5386}
5387
95554aad
TT
5388/* Compute the 'user' field for each psymtab in OBJFILE. */
5389
5390static void
5391set_partial_user (struct objfile *objfile)
5392{
5393 int i;
5394
5395 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5396 {
5397 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5398 struct partial_symtab *pst = per_cu->v.psymtab;
5399 int j;
5400
36586728
TT
5401 if (pst == NULL)
5402 continue;
5403
95554aad
TT
5404 for (j = 0; j < pst->number_of_dependencies; ++j)
5405 {
5406 /* Set the 'user' field only if it is not already set. */
5407 if (pst->dependencies[j]->user == NULL)
5408 pst->dependencies[j]->user = pst;
5409 }
5410 }
5411}
5412
93311388
DE
5413/* Build the partial symbol table by doing a quick pass through the
5414 .debug_info and .debug_abbrev sections. */
72bf9492 5415
93311388 5416static void
c67a9c90 5417dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5418{
60606b2c
TT
5419 struct cleanup *back_to, *addrmap_cleanup;
5420 struct obstack temp_obstack;
21b2bd31 5421 int i;
93311388 5422
45cfd468
DE
5423 if (dwarf2_read_debug)
5424 {
5425 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5426 objfile->name);
5427 }
5428
98bfdba5
PA
5429 dwarf2_per_objfile->reading_partial_symbols = 1;
5430
be391dca 5431 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5432
93311388
DE
5433 /* Any cached compilation units will be linked by the per-objfile
5434 read_in_chain. Make sure to free them when we're done. */
5435 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5436
348e048f
DE
5437 build_type_psymtabs (objfile);
5438
93311388 5439 create_all_comp_units (objfile);
c906108c 5440
60606b2c
TT
5441 /* Create a temporary address map on a temporary obstack. We later
5442 copy this to the final obstack. */
5443 obstack_init (&temp_obstack);
5444 make_cleanup_obstack_free (&temp_obstack);
5445 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5446 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5447
21b2bd31 5448 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5449 {
21b2bd31 5450 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5451
95554aad 5452 process_psymtab_comp_unit (per_cu, 0);
c906108c 5453 }
ff013f42 5454
95554aad
TT
5455 set_partial_user (objfile);
5456
ff013f42
JK
5457 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5458 &objfile->objfile_obstack);
60606b2c 5459 discard_cleanups (addrmap_cleanup);
ff013f42 5460
ae038cb0 5461 do_cleanups (back_to);
45cfd468
DE
5462
5463 if (dwarf2_read_debug)
5464 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5465 objfile->name);
ae038cb0
DJ
5466}
5467
3019eac3 5468/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5469
5470static void
dee91e82
DE
5471load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5472 gdb_byte *info_ptr,
5473 struct die_info *comp_unit_die,
5474 int has_children,
5475 void *data)
ae038cb0 5476{
dee91e82 5477 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5478
95554aad 5479 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5480
ae038cb0
DJ
5481 /* Check if comp unit has_children.
5482 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5483 If not, there's no more debug_info for this comp unit. */
d85a05f0 5484 if (has_children)
dee91e82
DE
5485 load_partial_dies (reader, info_ptr, 0);
5486}
98bfdba5 5487
dee91e82
DE
5488/* Load the partial DIEs for a secondary CU into memory.
5489 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5490
dee91e82
DE
5491static void
5492load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5493{
f4dc4d17
DE
5494 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5495 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5496}
5497
ae038cb0 5498static void
36586728
TT
5499read_comp_units_from_section (struct objfile *objfile,
5500 struct dwarf2_section_info *section,
5501 unsigned int is_dwz,
5502 int *n_allocated,
5503 int *n_comp_units,
5504 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5505{
be391dca 5506 gdb_byte *info_ptr;
36586728 5507 bfd *abfd = section->asection->owner;
be391dca 5508
36586728 5509 dwarf2_read_section (objfile, section);
ae038cb0 5510
36586728 5511 info_ptr = section->buffer;
6e70227d 5512
36586728 5513 while (info_ptr < section->buffer + section->size)
ae038cb0 5514 {
c764a876 5515 unsigned int length, initial_length_size;
ae038cb0 5516 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5517 sect_offset offset;
ae038cb0 5518
36586728 5519 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5520
5521 /* Read just enough information to find out where the next
5522 compilation unit is. */
36586728 5523 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5524
5525 /* Save the compilation unit for later lookup. */
5526 this_cu = obstack_alloc (&objfile->objfile_obstack,
5527 sizeof (struct dwarf2_per_cu_data));
5528 memset (this_cu, 0, sizeof (*this_cu));
5529 this_cu->offset = offset;
c764a876 5530 this_cu->length = length + initial_length_size;
36586728 5531 this_cu->is_dwz = is_dwz;
9291a0cd 5532 this_cu->objfile = objfile;
36586728 5533 this_cu->info_or_types_section = section;
ae038cb0 5534
36586728 5535 if (*n_comp_units == *n_allocated)
ae038cb0 5536 {
36586728
TT
5537 *n_allocated *= 2;
5538 *all_comp_units = xrealloc (*all_comp_units,
5539 *n_allocated
5540 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5541 }
36586728
TT
5542 (*all_comp_units)[*n_comp_units] = this_cu;
5543 ++*n_comp_units;
ae038cb0
DJ
5544
5545 info_ptr = info_ptr + this_cu->length;
5546 }
36586728
TT
5547}
5548
5549/* Create a list of all compilation units in OBJFILE.
5550 This is only done for -readnow and building partial symtabs. */
5551
5552static void
5553create_all_comp_units (struct objfile *objfile)
5554{
5555 int n_allocated;
5556 int n_comp_units;
5557 struct dwarf2_per_cu_data **all_comp_units;
5558
5559 n_comp_units = 0;
5560 n_allocated = 10;
5561 all_comp_units = xmalloc (n_allocated
5562 * sizeof (struct dwarf2_per_cu_data *));
5563
5564 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5565 &n_allocated, &n_comp_units, &all_comp_units);
5566
5567 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5568 {
5569 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5570
5571 read_comp_units_from_section (objfile, &dwz->info, 1,
5572 &n_allocated, &n_comp_units,
5573 &all_comp_units);
5574 }
ae038cb0
DJ
5575
5576 dwarf2_per_objfile->all_comp_units
5577 = obstack_alloc (&objfile->objfile_obstack,
5578 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5579 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5580 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5581 xfree (all_comp_units);
5582 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5583}
5584
5734ee8b
DJ
5585/* Process all loaded DIEs for compilation unit CU, starting at
5586 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5587 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5588 DW_AT_ranges). If NEED_PC is set, then this function will set
5589 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5590 and record the covered ranges in the addrmap. */
c906108c 5591
72bf9492
DJ
5592static void
5593scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5594 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5595{
72bf9492 5596 struct partial_die_info *pdi;
c906108c 5597
91c24f0a
DC
5598 /* Now, march along the PDI's, descending into ones which have
5599 interesting children but skipping the children of the other ones,
5600 until we reach the end of the compilation unit. */
c906108c 5601
72bf9492 5602 pdi = first_die;
91c24f0a 5603
72bf9492
DJ
5604 while (pdi != NULL)
5605 {
5606 fixup_partial_die (pdi, cu);
c906108c 5607
f55ee35c 5608 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5609 children, so we need to look at them. Ditto for anonymous
5610 enums. */
933c6fe4 5611
72bf9492 5612 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5613 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5614 || pdi->tag == DW_TAG_imported_unit)
c906108c 5615 {
72bf9492 5616 switch (pdi->tag)
c906108c
SS
5617 {
5618 case DW_TAG_subprogram:
5734ee8b 5619 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5620 break;
72929c62 5621 case DW_TAG_constant:
c906108c
SS
5622 case DW_TAG_variable:
5623 case DW_TAG_typedef:
91c24f0a 5624 case DW_TAG_union_type:
72bf9492 5625 if (!pdi->is_declaration)
63d06c5c 5626 {
72bf9492 5627 add_partial_symbol (pdi, cu);
63d06c5c
DC
5628 }
5629 break;
c906108c 5630 case DW_TAG_class_type:
680b30c7 5631 case DW_TAG_interface_type:
c906108c 5632 case DW_TAG_structure_type:
72bf9492 5633 if (!pdi->is_declaration)
c906108c 5634 {
72bf9492 5635 add_partial_symbol (pdi, cu);
c906108c
SS
5636 }
5637 break;
91c24f0a 5638 case DW_TAG_enumeration_type:
72bf9492
DJ
5639 if (!pdi->is_declaration)
5640 add_partial_enumeration (pdi, cu);
c906108c
SS
5641 break;
5642 case DW_TAG_base_type:
a02abb62 5643 case DW_TAG_subrange_type:
c906108c 5644 /* File scope base type definitions are added to the partial
c5aa993b 5645 symbol table. */
72bf9492 5646 add_partial_symbol (pdi, cu);
c906108c 5647 break;
d9fa45fe 5648 case DW_TAG_namespace:
5734ee8b 5649 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5650 break;
5d7cb8df
JK
5651 case DW_TAG_module:
5652 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5653 break;
95554aad
TT
5654 case DW_TAG_imported_unit:
5655 {
5656 struct dwarf2_per_cu_data *per_cu;
5657
f4dc4d17
DE
5658 /* For now we don't handle imported units in type units. */
5659 if (cu->per_cu->is_debug_types)
5660 {
5661 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5662 " supported in type units [in module %s]"),
5663 cu->objfile->name);
5664 }
5665
95554aad 5666 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5667 pdi->is_dwz,
95554aad
TT
5668 cu->objfile);
5669
5670 /* Go read the partial unit, if needed. */
5671 if (per_cu->v.psymtab == NULL)
5672 process_psymtab_comp_unit (per_cu, 1);
5673
f4dc4d17
DE
5674 VEC_safe_push (dwarf2_per_cu_ptr,
5675 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5676 }
5677 break;
c906108c
SS
5678 default:
5679 break;
5680 }
5681 }
5682
72bf9492
DJ
5683 /* If the die has a sibling, skip to the sibling. */
5684
5685 pdi = pdi->die_sibling;
5686 }
5687}
5688
5689/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5690
72bf9492 5691 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5692 name is concatenated with "::" and the partial DIE's name. For
5693 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5694 Enumerators are an exception; they use the scope of their parent
5695 enumeration type, i.e. the name of the enumeration type is not
5696 prepended to the enumerator.
91c24f0a 5697
72bf9492
DJ
5698 There are two complexities. One is DW_AT_specification; in this
5699 case "parent" means the parent of the target of the specification,
5700 instead of the direct parent of the DIE. The other is compilers
5701 which do not emit DW_TAG_namespace; in this case we try to guess
5702 the fully qualified name of structure types from their members'
5703 linkage names. This must be done using the DIE's children rather
5704 than the children of any DW_AT_specification target. We only need
5705 to do this for structures at the top level, i.e. if the target of
5706 any DW_AT_specification (if any; otherwise the DIE itself) does not
5707 have a parent. */
5708
5709/* Compute the scope prefix associated with PDI's parent, in
5710 compilation unit CU. The result will be allocated on CU's
5711 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5712 field. NULL is returned if no prefix is necessary. */
5713static char *
5714partial_die_parent_scope (struct partial_die_info *pdi,
5715 struct dwarf2_cu *cu)
5716{
5717 char *grandparent_scope;
5718 struct partial_die_info *parent, *real_pdi;
91c24f0a 5719
72bf9492
DJ
5720 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5721 then this means the parent of the specification DIE. */
5722
5723 real_pdi = pdi;
72bf9492 5724 while (real_pdi->has_specification)
36586728
TT
5725 real_pdi = find_partial_die (real_pdi->spec_offset,
5726 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5727
5728 parent = real_pdi->die_parent;
5729 if (parent == NULL)
5730 return NULL;
5731
5732 if (parent->scope_set)
5733 return parent->scope;
5734
5735 fixup_partial_die (parent, cu);
5736
10b3939b 5737 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5738
acebe513
UW
5739 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5740 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5741 Work around this problem here. */
5742 if (cu->language == language_cplus
6e70227d 5743 && parent->tag == DW_TAG_namespace
acebe513
UW
5744 && strcmp (parent->name, "::") == 0
5745 && grandparent_scope == NULL)
5746 {
5747 parent->scope = NULL;
5748 parent->scope_set = 1;
5749 return NULL;
5750 }
5751
9c6c53f7
SA
5752 if (pdi->tag == DW_TAG_enumerator)
5753 /* Enumerators should not get the name of the enumeration as a prefix. */
5754 parent->scope = grandparent_scope;
5755 else if (parent->tag == DW_TAG_namespace
f55ee35c 5756 || parent->tag == DW_TAG_module
72bf9492
DJ
5757 || parent->tag == DW_TAG_structure_type
5758 || parent->tag == DW_TAG_class_type
680b30c7 5759 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5760 || parent->tag == DW_TAG_union_type
5761 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5762 {
5763 if (grandparent_scope == NULL)
5764 parent->scope = parent->name;
5765 else
3e43a32a
MS
5766 parent->scope = typename_concat (&cu->comp_unit_obstack,
5767 grandparent_scope,
f55ee35c 5768 parent->name, 0, cu);
72bf9492 5769 }
72bf9492
DJ
5770 else
5771 {
5772 /* FIXME drow/2004-04-01: What should we be doing with
5773 function-local names? For partial symbols, we should probably be
5774 ignoring them. */
5775 complaint (&symfile_complaints,
e2e0b3e5 5776 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5777 parent->tag, pdi->offset.sect_off);
72bf9492 5778 parent->scope = grandparent_scope;
c906108c
SS
5779 }
5780
72bf9492
DJ
5781 parent->scope_set = 1;
5782 return parent->scope;
5783}
5784
5785/* Return the fully scoped name associated with PDI, from compilation unit
5786 CU. The result will be allocated with malloc. */
4568ecf9 5787
72bf9492
DJ
5788static char *
5789partial_die_full_name (struct partial_die_info *pdi,
5790 struct dwarf2_cu *cu)
5791{
5792 char *parent_scope;
5793
98bfdba5
PA
5794 /* If this is a template instantiation, we can not work out the
5795 template arguments from partial DIEs. So, unfortunately, we have
5796 to go through the full DIEs. At least any work we do building
5797 types here will be reused if full symbols are loaded later. */
5798 if (pdi->has_template_arguments)
5799 {
5800 fixup_partial_die (pdi, cu);
5801
5802 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5803 {
5804 struct die_info *die;
5805 struct attribute attr;
5806 struct dwarf2_cu *ref_cu = cu;
5807
b64f50a1 5808 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5809 attr.name = 0;
5810 attr.form = DW_FORM_ref_addr;
4568ecf9 5811 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5812 die = follow_die_ref (NULL, &attr, &ref_cu);
5813
5814 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5815 }
5816 }
5817
72bf9492
DJ
5818 parent_scope = partial_die_parent_scope (pdi, cu);
5819 if (parent_scope == NULL)
5820 return NULL;
5821 else
f55ee35c 5822 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5823}
5824
5825static void
72bf9492 5826add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5827{
e7c27a73 5828 struct objfile *objfile = cu->objfile;
c906108c 5829 CORE_ADDR addr = 0;
decbce07 5830 char *actual_name = NULL;
e142c38c 5831 CORE_ADDR baseaddr;
72bf9492 5832 int built_actual_name = 0;
e142c38c
DJ
5833
5834 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5835
94af9270
KS
5836 actual_name = partial_die_full_name (pdi, cu);
5837 if (actual_name)
5838 built_actual_name = 1;
63d06c5c 5839
72bf9492
DJ
5840 if (actual_name == NULL)
5841 actual_name = pdi->name;
5842
c906108c
SS
5843 switch (pdi->tag)
5844 {
5845 case DW_TAG_subprogram:
2cfa0c8d 5846 if (pdi->is_external || cu->language == language_ada)
c906108c 5847 {
2cfa0c8d
JB
5848 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5849 of the global scope. But in Ada, we want to be able to access
5850 nested procedures globally. So all Ada subprograms are stored
5851 in the global scope. */
f47fb265 5852 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5853 mst_text, objfile); */
f47fb265
MS
5854 add_psymbol_to_list (actual_name, strlen (actual_name),
5855 built_actual_name,
5856 VAR_DOMAIN, LOC_BLOCK,
5857 &objfile->global_psymbols,
5858 0, pdi->lowpc + baseaddr,
5859 cu->language, objfile);
c906108c
SS
5860 }
5861 else
5862 {
f47fb265 5863 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5864 mst_file_text, objfile); */
f47fb265
MS
5865 add_psymbol_to_list (actual_name, strlen (actual_name),
5866 built_actual_name,
5867 VAR_DOMAIN, LOC_BLOCK,
5868 &objfile->static_psymbols,
5869 0, pdi->lowpc + baseaddr,
5870 cu->language, objfile);
c906108c
SS
5871 }
5872 break;
72929c62
JB
5873 case DW_TAG_constant:
5874 {
5875 struct psymbol_allocation_list *list;
5876
5877 if (pdi->is_external)
5878 list = &objfile->global_psymbols;
5879 else
5880 list = &objfile->static_psymbols;
f47fb265
MS
5881 add_psymbol_to_list (actual_name, strlen (actual_name),
5882 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5883 list, 0, 0, cu->language, objfile);
72929c62
JB
5884 }
5885 break;
c906108c 5886 case DW_TAG_variable:
95554aad
TT
5887 if (pdi->d.locdesc)
5888 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5889
95554aad 5890 if (pdi->d.locdesc
caac4577
JG
5891 && addr == 0
5892 && !dwarf2_per_objfile->has_section_at_zero)
5893 {
5894 /* A global or static variable may also have been stripped
5895 out by the linker if unused, in which case its address
5896 will be nullified; do not add such variables into partial
5897 symbol table then. */
5898 }
5899 else if (pdi->is_external)
c906108c
SS
5900 {
5901 /* Global Variable.
5902 Don't enter into the minimal symbol tables as there is
5903 a minimal symbol table entry from the ELF symbols already.
5904 Enter into partial symbol table if it has a location
5905 descriptor or a type.
5906 If the location descriptor is missing, new_symbol will create
5907 a LOC_UNRESOLVED symbol, the address of the variable will then
5908 be determined from the minimal symbol table whenever the variable
5909 is referenced.
5910 The address for the partial symbol table entry is not
5911 used by GDB, but it comes in handy for debugging partial symbol
5912 table building. */
5913
95554aad 5914 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5915 add_psymbol_to_list (actual_name, strlen (actual_name),
5916 built_actual_name,
5917 VAR_DOMAIN, LOC_STATIC,
5918 &objfile->global_psymbols,
5919 0, addr + baseaddr,
5920 cu->language, objfile);
c906108c
SS
5921 }
5922 else
5923 {
0963b4bd 5924 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5925 if (pdi->d.locdesc == NULL)
decbce07
MS
5926 {
5927 if (built_actual_name)
5928 xfree (actual_name);
5929 return;
5930 }
f47fb265 5931 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5932 mst_file_data, objfile); */
f47fb265
MS
5933 add_psymbol_to_list (actual_name, strlen (actual_name),
5934 built_actual_name,
5935 VAR_DOMAIN, LOC_STATIC,
5936 &objfile->static_psymbols,
5937 0, addr + baseaddr,
5938 cu->language, objfile);
c906108c
SS
5939 }
5940 break;
5941 case DW_TAG_typedef:
5942 case DW_TAG_base_type:
a02abb62 5943 case DW_TAG_subrange_type:
38d518c9 5944 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5945 built_actual_name,
176620f1 5946 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5947 &objfile->static_psymbols,
e142c38c 5948 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5949 break;
72bf9492
DJ
5950 case DW_TAG_namespace:
5951 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5952 built_actual_name,
72bf9492
DJ
5953 VAR_DOMAIN, LOC_TYPEDEF,
5954 &objfile->global_psymbols,
5955 0, (CORE_ADDR) 0, cu->language, objfile);
5956 break;
c906108c 5957 case DW_TAG_class_type:
680b30c7 5958 case DW_TAG_interface_type:
c906108c
SS
5959 case DW_TAG_structure_type:
5960 case DW_TAG_union_type:
5961 case DW_TAG_enumeration_type:
fa4028e9
JB
5962 /* Skip external references. The DWARF standard says in the section
5963 about "Structure, Union, and Class Type Entries": "An incomplete
5964 structure, union or class type is represented by a structure,
5965 union or class entry that does not have a byte size attribute
5966 and that has a DW_AT_declaration attribute." */
5967 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5968 {
5969 if (built_actual_name)
5970 xfree (actual_name);
5971 return;
5972 }
fa4028e9 5973
63d06c5c
DC
5974 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5975 static vs. global. */
38d518c9 5976 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5977 built_actual_name,
176620f1 5978 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5979 (cu->language == language_cplus
5980 || cu->language == language_java)
63d06c5c
DC
5981 ? &objfile->global_psymbols
5982 : &objfile->static_psymbols,
e142c38c 5983 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5984
c906108c
SS
5985 break;
5986 case DW_TAG_enumerator:
38d518c9 5987 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5988 built_actual_name,
176620f1 5989 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5990 (cu->language == language_cplus
5991 || cu->language == language_java)
f6fe98ef
DJ
5992 ? &objfile->global_psymbols
5993 : &objfile->static_psymbols,
e142c38c 5994 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5995 break;
5996 default:
5997 break;
5998 }
5c4e30ca 5999
72bf9492
DJ
6000 if (built_actual_name)
6001 xfree (actual_name);
c906108c
SS
6002}
6003
5c4e30ca
DC
6004/* Read a partial die corresponding to a namespace; also, add a symbol
6005 corresponding to that namespace to the symbol table. NAMESPACE is
6006 the name of the enclosing namespace. */
91c24f0a 6007
72bf9492
DJ
6008static void
6009add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6010 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6011 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6012{
72bf9492 6013 /* Add a symbol for the namespace. */
e7c27a73 6014
72bf9492 6015 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6016
6017 /* Now scan partial symbols in that namespace. */
6018
91c24f0a 6019 if (pdi->has_children)
5734ee8b 6020 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6021}
6022
5d7cb8df
JK
6023/* Read a partial die corresponding to a Fortran module. */
6024
6025static void
6026add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6027 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6028{
f55ee35c 6029 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6030
6031 if (pdi->has_children)
6032 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6033}
6034
bc30ff58
JB
6035/* Read a partial die corresponding to a subprogram and create a partial
6036 symbol for that subprogram. When the CU language allows it, this
6037 routine also defines a partial symbol for each nested subprogram
6038 that this subprogram contains.
6e70227d 6039
bc30ff58
JB
6040 DIE my also be a lexical block, in which case we simply search
6041 recursively for suprograms defined inside that lexical block.
6042 Again, this is only performed when the CU language allows this
6043 type of definitions. */
6044
6045static void
6046add_partial_subprogram (struct partial_die_info *pdi,
6047 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6048 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6049{
6050 if (pdi->tag == DW_TAG_subprogram)
6051 {
6052 if (pdi->has_pc_info)
6053 {
6054 if (pdi->lowpc < *lowpc)
6055 *lowpc = pdi->lowpc;
6056 if (pdi->highpc > *highpc)
6057 *highpc = pdi->highpc;
5734ee8b
DJ
6058 if (need_pc)
6059 {
6060 CORE_ADDR baseaddr;
6061 struct objfile *objfile = cu->objfile;
6062
6063 baseaddr = ANOFFSET (objfile->section_offsets,
6064 SECT_OFF_TEXT (objfile));
6065 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6066 pdi->lowpc + baseaddr,
6067 pdi->highpc - 1 + baseaddr,
9291a0cd 6068 cu->per_cu->v.psymtab);
5734ee8b 6069 }
481860b3
GB
6070 }
6071
6072 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6073 {
bc30ff58 6074 if (!pdi->is_declaration)
e8d05480
JB
6075 /* Ignore subprogram DIEs that do not have a name, they are
6076 illegal. Do not emit a complaint at this point, we will
6077 do so when we convert this psymtab into a symtab. */
6078 if (pdi->name)
6079 add_partial_symbol (pdi, cu);
bc30ff58
JB
6080 }
6081 }
6e70227d 6082
bc30ff58
JB
6083 if (! pdi->has_children)
6084 return;
6085
6086 if (cu->language == language_ada)
6087 {
6088 pdi = pdi->die_child;
6089 while (pdi != NULL)
6090 {
6091 fixup_partial_die (pdi, cu);
6092 if (pdi->tag == DW_TAG_subprogram
6093 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6094 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6095 pdi = pdi->die_sibling;
6096 }
6097 }
6098}
6099
91c24f0a
DC
6100/* Read a partial die corresponding to an enumeration type. */
6101
72bf9492
DJ
6102static void
6103add_partial_enumeration (struct partial_die_info *enum_pdi,
6104 struct dwarf2_cu *cu)
91c24f0a 6105{
72bf9492 6106 struct partial_die_info *pdi;
91c24f0a
DC
6107
6108 if (enum_pdi->name != NULL)
72bf9492
DJ
6109 add_partial_symbol (enum_pdi, cu);
6110
6111 pdi = enum_pdi->die_child;
6112 while (pdi)
91c24f0a 6113 {
72bf9492 6114 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6115 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6116 else
72bf9492
DJ
6117 add_partial_symbol (pdi, cu);
6118 pdi = pdi->die_sibling;
91c24f0a 6119 }
91c24f0a
DC
6120}
6121
6caca83c
CC
6122/* Return the initial uleb128 in the die at INFO_PTR. */
6123
6124static unsigned int
6125peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6126{
6127 unsigned int bytes_read;
6128
6129 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6130}
6131
4bb7a0a7
DJ
6132/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6133 Return the corresponding abbrev, or NULL if the number is zero (indicating
6134 an empty DIE). In either case *BYTES_READ will be set to the length of
6135 the initial number. */
6136
6137static struct abbrev_info *
fe1b8b76 6138peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6139 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6140{
6141 bfd *abfd = cu->objfile->obfd;
6142 unsigned int abbrev_number;
6143 struct abbrev_info *abbrev;
6144
6145 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6146
6147 if (abbrev_number == 0)
6148 return NULL;
6149
433df2d4 6150 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6151 if (!abbrev)
6152 {
3e43a32a
MS
6153 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6154 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6155 }
6156
6157 return abbrev;
6158}
6159
93311388
DE
6160/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6161 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6162 DIE. Any children of the skipped DIEs will also be skipped. */
6163
fe1b8b76 6164static gdb_byte *
dee91e82 6165skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6166{
dee91e82 6167 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6168 struct abbrev_info *abbrev;
6169 unsigned int bytes_read;
6170
6171 while (1)
6172 {
6173 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6174 if (abbrev == NULL)
6175 return info_ptr + bytes_read;
6176 else
dee91e82 6177 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6178 }
6179}
6180
93311388
DE
6181/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6182 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6183 abbrev corresponding to that skipped uleb128 should be passed in
6184 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6185 children. */
6186
fe1b8b76 6187static gdb_byte *
dee91e82
DE
6188skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6189 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6190{
6191 unsigned int bytes_read;
6192 struct attribute attr;
dee91e82
DE
6193 bfd *abfd = reader->abfd;
6194 struct dwarf2_cu *cu = reader->cu;
6195 gdb_byte *buffer = reader->buffer;
f664829e
DE
6196 const gdb_byte *buffer_end = reader->buffer_end;
6197 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6198 unsigned int form, i;
6199
6200 for (i = 0; i < abbrev->num_attrs; i++)
6201 {
6202 /* The only abbrev we care about is DW_AT_sibling. */
6203 if (abbrev->attrs[i].name == DW_AT_sibling)
6204 {
dee91e82 6205 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6206 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6207 complaint (&symfile_complaints,
6208 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6209 else
b64f50a1 6210 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6211 }
6212
6213 /* If it isn't DW_AT_sibling, skip this attribute. */
6214 form = abbrev->attrs[i].form;
6215 skip_attribute:
6216 switch (form)
6217 {
4bb7a0a7 6218 case DW_FORM_ref_addr:
ae411497
TT
6219 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6220 and later it is offset sized. */
6221 if (cu->header.version == 2)
6222 info_ptr += cu->header.addr_size;
6223 else
6224 info_ptr += cu->header.offset_size;
6225 break;
36586728
TT
6226 case DW_FORM_GNU_ref_alt:
6227 info_ptr += cu->header.offset_size;
6228 break;
ae411497 6229 case DW_FORM_addr:
4bb7a0a7
DJ
6230 info_ptr += cu->header.addr_size;
6231 break;
6232 case DW_FORM_data1:
6233 case DW_FORM_ref1:
6234 case DW_FORM_flag:
6235 info_ptr += 1;
6236 break;
2dc7f7b3
TT
6237 case DW_FORM_flag_present:
6238 break;
4bb7a0a7
DJ
6239 case DW_FORM_data2:
6240 case DW_FORM_ref2:
6241 info_ptr += 2;
6242 break;
6243 case DW_FORM_data4:
6244 case DW_FORM_ref4:
6245 info_ptr += 4;
6246 break;
6247 case DW_FORM_data8:
6248 case DW_FORM_ref8:
55f1336d 6249 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6250 info_ptr += 8;
6251 break;
6252 case DW_FORM_string:
9b1c24c8 6253 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6254 info_ptr += bytes_read;
6255 break;
2dc7f7b3 6256 case DW_FORM_sec_offset:
4bb7a0a7 6257 case DW_FORM_strp:
36586728 6258 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6259 info_ptr += cu->header.offset_size;
6260 break;
2dc7f7b3 6261 case DW_FORM_exprloc:
4bb7a0a7
DJ
6262 case DW_FORM_block:
6263 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6264 info_ptr += bytes_read;
6265 break;
6266 case DW_FORM_block1:
6267 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6268 break;
6269 case DW_FORM_block2:
6270 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6271 break;
6272 case DW_FORM_block4:
6273 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6274 break;
6275 case DW_FORM_sdata:
6276 case DW_FORM_udata:
6277 case DW_FORM_ref_udata:
3019eac3
DE
6278 case DW_FORM_GNU_addr_index:
6279 case DW_FORM_GNU_str_index:
f664829e 6280 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6281 break;
6282 case DW_FORM_indirect:
6283 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6284 info_ptr += bytes_read;
6285 /* We need to continue parsing from here, so just go back to
6286 the top. */
6287 goto skip_attribute;
6288
6289 default:
3e43a32a
MS
6290 error (_("Dwarf Error: Cannot handle %s "
6291 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6292 dwarf_form_name (form),
6293 bfd_get_filename (abfd));
6294 }
6295 }
6296
6297 if (abbrev->has_children)
dee91e82 6298 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6299 else
6300 return info_ptr;
6301}
6302
93311388 6303/* Locate ORIG_PDI's sibling.
dee91e82 6304 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6305
fe1b8b76 6306static gdb_byte *
dee91e82
DE
6307locate_pdi_sibling (const struct die_reader_specs *reader,
6308 struct partial_die_info *orig_pdi,
6309 gdb_byte *info_ptr)
91c24f0a
DC
6310{
6311 /* Do we know the sibling already? */
72bf9492 6312
91c24f0a
DC
6313 if (orig_pdi->sibling)
6314 return orig_pdi->sibling;
6315
6316 /* Are there any children to deal with? */
6317
6318 if (!orig_pdi->has_children)
6319 return info_ptr;
6320
4bb7a0a7 6321 /* Skip the children the long way. */
91c24f0a 6322
dee91e82 6323 return skip_children (reader, info_ptr);
91c24f0a
DC
6324}
6325
c906108c
SS
6326/* Expand this partial symbol table into a full symbol table. */
6327
6328static void
5c80ed9d 6329dwarf2_psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
c906108c 6330{
c906108c
SS
6331 if (pst != NULL)
6332 {
6333 if (pst->readin)
6334 {
3e43a32a
MS
6335 warning (_("bug: psymtab for %s is already read in."),
6336 pst->filename);
c906108c
SS
6337 }
6338 else
6339 {
6340 if (info_verbose)
6341 {
3e43a32a
MS
6342 printf_filtered (_("Reading in symbols for %s..."),
6343 pst->filename);
c906108c
SS
6344 gdb_flush (gdb_stdout);
6345 }
6346
10b3939b 6347 /* Restore our global data. */
5c80ed9d 6348 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6349
b2ab525c
KB
6350 /* If this psymtab is constructed from a debug-only objfile, the
6351 has_section_at_zero flag will not necessarily be correct. We
6352 can get the correct value for this flag by looking at the data
6353 associated with the (presumably stripped) associated objfile. */
5c80ed9d 6354 if (objfile->separate_debug_objfile_backlink)
b2ab525c
KB
6355 {
6356 struct dwarf2_per_objfile *dpo_backlink
5c80ed9d 6357 = objfile_data (objfile->separate_debug_objfile_backlink,
b2ab525c 6358 dwarf2_objfile_data_key);
9a619af0 6359
b2ab525c
KB
6360 dwarf2_per_objfile->has_section_at_zero
6361 = dpo_backlink->has_section_at_zero;
6362 }
6363
98bfdba5
PA
6364 dwarf2_per_objfile->reading_partial_symbols = 0;
6365
c906108c
SS
6366 psymtab_to_symtab_1 (pst);
6367
6368 /* Finish up the debug error message. */
6369 if (info_verbose)
a3f17187 6370 printf_filtered (_("done.\n"));
c906108c
SS
6371 }
6372 }
95554aad
TT
6373
6374 process_cu_includes ();
c906108c 6375}
9cdd5dbd
DE
6376\f
6377/* Reading in full CUs. */
c906108c 6378
10b3939b
DJ
6379/* Add PER_CU to the queue. */
6380
6381static void
95554aad
TT
6382queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6383 enum language pretend_language)
10b3939b
DJ
6384{
6385 struct dwarf2_queue_item *item;
6386
6387 per_cu->queued = 1;
6388 item = xmalloc (sizeof (*item));
6389 item->per_cu = per_cu;
95554aad 6390 item->pretend_language = pretend_language;
10b3939b
DJ
6391 item->next = NULL;
6392
6393 if (dwarf2_queue == NULL)
6394 dwarf2_queue = item;
6395 else
6396 dwarf2_queue_tail->next = item;
6397
6398 dwarf2_queue_tail = item;
6399}
6400
0907af0c
DE
6401/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6402 unit and add it to our queue.
6403 The result is non-zero if PER_CU was queued, otherwise the result is zero
6404 meaning either PER_CU is already queued or it is already loaded. */
6405
6406static int
6407maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6408 struct dwarf2_per_cu_data *per_cu,
6409 enum language pretend_language)
6410{
6411 /* We may arrive here during partial symbol reading, if we need full
6412 DIEs to process an unusual case (e.g. template arguments). Do
6413 not queue PER_CU, just tell our caller to load its DIEs. */
6414 if (dwarf2_per_objfile->reading_partial_symbols)
6415 {
6416 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6417 return 1;
6418 return 0;
6419 }
6420
6421 /* Mark the dependence relation so that we don't flush PER_CU
6422 too early. */
6423 dwarf2_add_dependence (this_cu, per_cu);
6424
6425 /* If it's already on the queue, we have nothing to do. */
6426 if (per_cu->queued)
6427 return 0;
6428
6429 /* If the compilation unit is already loaded, just mark it as
6430 used. */
6431 if (per_cu->cu != NULL)
6432 {
6433 per_cu->cu->last_used = 0;
6434 return 0;
6435 }
6436
6437 /* Add it to the queue. */
6438 queue_comp_unit (per_cu, pretend_language);
6439
6440 return 1;
6441}
6442
10b3939b
DJ
6443/* Process the queue. */
6444
6445static void
a0f42c21 6446process_queue (void)
10b3939b
DJ
6447{
6448 struct dwarf2_queue_item *item, *next_item;
6449
45cfd468
DE
6450 if (dwarf2_read_debug)
6451 {
6452 fprintf_unfiltered (gdb_stdlog,
6453 "Expanding one or more symtabs of objfile %s ...\n",
6454 dwarf2_per_objfile->objfile->name);
6455 }
6456
03dd20cc
DJ
6457 /* The queue starts out with one item, but following a DIE reference
6458 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6459 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6460 {
9291a0cd
TT
6461 if (dwarf2_per_objfile->using_index
6462 ? !item->per_cu->v.quick->symtab
6463 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6464 {
6465 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6466
6467 if (dwarf2_read_debug)
6468 {
6469 fprintf_unfiltered (gdb_stdlog,
6470 "Expanding symtab of %s at offset 0x%x\n",
6471 per_cu->is_debug_types ? "TU" : "CU",
6472 per_cu->offset.sect_off);
6473 }
6474
6475 if (per_cu->is_debug_types)
6476 process_full_type_unit (per_cu, item->pretend_language);
6477 else
6478 process_full_comp_unit (per_cu, item->pretend_language);
6479
6480 if (dwarf2_read_debug)
6481 {
6482 fprintf_unfiltered (gdb_stdlog,
6483 "Done expanding %s at offset 0x%x\n",
6484 per_cu->is_debug_types ? "TU" : "CU",
6485 per_cu->offset.sect_off);
6486 }
6487 }
10b3939b
DJ
6488
6489 item->per_cu->queued = 0;
6490 next_item = item->next;
6491 xfree (item);
6492 }
6493
6494 dwarf2_queue_tail = NULL;
45cfd468
DE
6495
6496 if (dwarf2_read_debug)
6497 {
6498 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6499 dwarf2_per_objfile->objfile->name);
6500 }
10b3939b
DJ
6501}
6502
6503/* Free all allocated queue entries. This function only releases anything if
6504 an error was thrown; if the queue was processed then it would have been
6505 freed as we went along. */
6506
6507static void
6508dwarf2_release_queue (void *dummy)
6509{
6510 struct dwarf2_queue_item *item, *last;
6511
6512 item = dwarf2_queue;
6513 while (item)
6514 {
6515 /* Anything still marked queued is likely to be in an
6516 inconsistent state, so discard it. */
6517 if (item->per_cu->queued)
6518 {
6519 if (item->per_cu->cu != NULL)
dee91e82 6520 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6521 item->per_cu->queued = 0;
6522 }
6523
6524 last = item;
6525 item = item->next;
6526 xfree (last);
6527 }
6528
6529 dwarf2_queue = dwarf2_queue_tail = NULL;
6530}
6531
6532/* Read in full symbols for PST, and anything it depends on. */
6533
c906108c 6534static void
fba45db2 6535psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6536{
10b3939b 6537 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6538 int i;
6539
95554aad
TT
6540 if (pst->readin)
6541 return;
6542
aaa75496 6543 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6544 if (!pst->dependencies[i]->readin
6545 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6546 {
6547 /* Inform about additional files that need to be read in. */
6548 if (info_verbose)
6549 {
a3f17187 6550 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6551 fputs_filtered (" ", gdb_stdout);
6552 wrap_here ("");
6553 fputs_filtered ("and ", gdb_stdout);
6554 wrap_here ("");
6555 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6556 wrap_here (""); /* Flush output. */
aaa75496
JB
6557 gdb_flush (gdb_stdout);
6558 }
6559 psymtab_to_symtab_1 (pst->dependencies[i]);
6560 }
6561
e38df1d0 6562 per_cu = pst->read_symtab_private;
10b3939b
DJ
6563
6564 if (per_cu == NULL)
aaa75496
JB
6565 {
6566 /* It's an include file, no symbols to read for it.
6567 Everything is in the parent symtab. */
6568 pst->readin = 1;
6569 return;
6570 }
c906108c 6571
a0f42c21 6572 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6573}
6574
dee91e82
DE
6575/* Trivial hash function for die_info: the hash value of a DIE
6576 is its offset in .debug_info for this objfile. */
10b3939b 6577
dee91e82
DE
6578static hashval_t
6579die_hash (const void *item)
10b3939b 6580{
dee91e82 6581 const struct die_info *die = item;
6502dd73 6582
dee91e82
DE
6583 return die->offset.sect_off;
6584}
63d06c5c 6585
dee91e82
DE
6586/* Trivial comparison function for die_info structures: two DIEs
6587 are equal if they have the same offset. */
98bfdba5 6588
dee91e82
DE
6589static int
6590die_eq (const void *item_lhs, const void *item_rhs)
6591{
6592 const struct die_info *die_lhs = item_lhs;
6593 const struct die_info *die_rhs = item_rhs;
c906108c 6594
dee91e82
DE
6595 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6596}
c906108c 6597
dee91e82
DE
6598/* die_reader_func for load_full_comp_unit.
6599 This is identical to read_signatured_type_reader,
6600 but is kept separate for now. */
c906108c 6601
dee91e82
DE
6602static void
6603load_full_comp_unit_reader (const struct die_reader_specs *reader,
6604 gdb_byte *info_ptr,
6605 struct die_info *comp_unit_die,
6606 int has_children,
6607 void *data)
6608{
6609 struct dwarf2_cu *cu = reader->cu;
95554aad 6610 enum language *language_ptr = data;
6caca83c 6611
dee91e82
DE
6612 gdb_assert (cu->die_hash == NULL);
6613 cu->die_hash =
6614 htab_create_alloc_ex (cu->header.length / 12,
6615 die_hash,
6616 die_eq,
6617 NULL,
6618 &cu->comp_unit_obstack,
6619 hashtab_obstack_allocate,
6620 dummy_obstack_deallocate);
e142c38c 6621
dee91e82
DE
6622 if (has_children)
6623 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6624 &info_ptr, comp_unit_die);
6625 cu->dies = comp_unit_die;
6626 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6627
6628 /* We try not to read any attributes in this function, because not
9cdd5dbd 6629 all CUs needed for references have been loaded yet, and symbol
10b3939b 6630 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6631 or we won't be able to build types correctly.
6632 Similarly, if we do not read the producer, we can not apply
6633 producer-specific interpretation. */
95554aad 6634 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6635}
10b3939b 6636
dee91e82 6637/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6638
dee91e82 6639static void
95554aad
TT
6640load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6641 enum language pretend_language)
dee91e82 6642{
3019eac3 6643 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6644
f4dc4d17
DE
6645 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6646 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6647}
6648
3da10d80
KS
6649/* Add a DIE to the delayed physname list. */
6650
6651static void
6652add_to_method_list (struct type *type, int fnfield_index, int index,
6653 const char *name, struct die_info *die,
6654 struct dwarf2_cu *cu)
6655{
6656 struct delayed_method_info mi;
6657 mi.type = type;
6658 mi.fnfield_index = fnfield_index;
6659 mi.index = index;
6660 mi.name = name;
6661 mi.die = die;
6662 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6663}
6664
6665/* A cleanup for freeing the delayed method list. */
6666
6667static void
6668free_delayed_list (void *ptr)
6669{
6670 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6671 if (cu->method_list != NULL)
6672 {
6673 VEC_free (delayed_method_info, cu->method_list);
6674 cu->method_list = NULL;
6675 }
6676}
6677
6678/* Compute the physnames of any methods on the CU's method list.
6679
6680 The computation of method physnames is delayed in order to avoid the
6681 (bad) condition that one of the method's formal parameters is of an as yet
6682 incomplete type. */
6683
6684static void
6685compute_delayed_physnames (struct dwarf2_cu *cu)
6686{
6687 int i;
6688 struct delayed_method_info *mi;
6689 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6690 {
1d06ead6 6691 const char *physname;
3da10d80
KS
6692 struct fn_fieldlist *fn_flp
6693 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6694 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6695 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6696 }
6697}
6698
a766d390
DE
6699/* Go objects should be embedded in a DW_TAG_module DIE,
6700 and it's not clear if/how imported objects will appear.
6701 To keep Go support simple until that's worked out,
6702 go back through what we've read and create something usable.
6703 We could do this while processing each DIE, and feels kinda cleaner,
6704 but that way is more invasive.
6705 This is to, for example, allow the user to type "p var" or "b main"
6706 without having to specify the package name, and allow lookups
6707 of module.object to work in contexts that use the expression
6708 parser. */
6709
6710static void
6711fixup_go_packaging (struct dwarf2_cu *cu)
6712{
6713 char *package_name = NULL;
6714 struct pending *list;
6715 int i;
6716
6717 for (list = global_symbols; list != NULL; list = list->next)
6718 {
6719 for (i = 0; i < list->nsyms; ++i)
6720 {
6721 struct symbol *sym = list->symbol[i];
6722
6723 if (SYMBOL_LANGUAGE (sym) == language_go
6724 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6725 {
6726 char *this_package_name = go_symbol_package_name (sym);
6727
6728 if (this_package_name == NULL)
6729 continue;
6730 if (package_name == NULL)
6731 package_name = this_package_name;
6732 else
6733 {
6734 if (strcmp (package_name, this_package_name) != 0)
6735 complaint (&symfile_complaints,
6736 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17
TT
6737 (SYMBOL_SYMTAB (sym)
6738 && SYMBOL_SYMTAB (sym)->filename
6739 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6740 : cu->objfile->name),
6741 this_package_name, package_name);
6742 xfree (this_package_name);
6743 }
6744 }
6745 }
6746 }
6747
6748 if (package_name != NULL)
6749 {
6750 struct objfile *objfile = cu->objfile;
6751 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6752 package_name, objfile);
6753 struct symbol *sym;
6754
6755 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6756
6757 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6758 SYMBOL_SET_LANGUAGE (sym, language_go);
6759 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6760 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6761 e.g., "main" finds the "main" module and not C's main(). */
6762 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6763 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6764 SYMBOL_TYPE (sym) = type;
6765
6766 add_symbol_to_list (sym, &global_symbols);
6767
6768 xfree (package_name);
6769 }
6770}
6771
95554aad
TT
6772static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6773
6774/* Return the symtab for PER_CU. This works properly regardless of
6775 whether we're using the index or psymtabs. */
6776
6777static struct symtab *
6778get_symtab (struct dwarf2_per_cu_data *per_cu)
6779{
6780 return (dwarf2_per_objfile->using_index
6781 ? per_cu->v.quick->symtab
6782 : per_cu->v.psymtab->symtab);
6783}
6784
6785/* A helper function for computing the list of all symbol tables
6786 included by PER_CU. */
6787
6788static void
6789recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6790 htab_t all_children,
6791 struct dwarf2_per_cu_data *per_cu)
6792{
6793 void **slot;
6794 int ix;
6795 struct dwarf2_per_cu_data *iter;
6796
6797 slot = htab_find_slot (all_children, per_cu, INSERT);
6798 if (*slot != NULL)
6799 {
6800 /* This inclusion and its children have been processed. */
6801 return;
6802 }
6803
6804 *slot = per_cu;
6805 /* Only add a CU if it has a symbol table. */
6806 if (get_symtab (per_cu) != NULL)
6807 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6808
6809 for (ix = 0;
f4dc4d17 6810 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6811 ++ix)
6812 recursively_compute_inclusions (result, all_children, iter);
6813}
6814
6815/* Compute the symtab 'includes' fields for the symtab related to
6816 PER_CU. */
6817
6818static void
6819compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6820{
f4dc4d17
DE
6821 gdb_assert (! per_cu->is_debug_types);
6822
6823 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6824 {
6825 int ix, len;
6826 struct dwarf2_per_cu_data *iter;
6827 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6828 htab_t all_children;
6829 struct symtab *symtab = get_symtab (per_cu);
6830
6831 /* If we don't have a symtab, we can just skip this case. */
6832 if (symtab == NULL)
6833 return;
6834
6835 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6836 NULL, xcalloc, xfree);
6837
6838 for (ix = 0;
f4dc4d17 6839 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6840 ix, iter);
6841 ++ix)
6842 recursively_compute_inclusions (&result_children, all_children, iter);
6843
6844 /* Now we have a transitive closure of all the included CUs, so
6845 we can convert it to a list of symtabs. */
6846 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6847 symtab->includes
6848 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6849 (len + 1) * sizeof (struct symtab *));
6850 for (ix = 0;
6851 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6852 ++ix)
6853 symtab->includes[ix] = get_symtab (iter);
6854 symtab->includes[len] = NULL;
6855
6856 VEC_free (dwarf2_per_cu_ptr, result_children);
6857 htab_delete (all_children);
6858 }
6859}
6860
6861/* Compute the 'includes' field for the symtabs of all the CUs we just
6862 read. */
6863
6864static void
6865process_cu_includes (void)
6866{
6867 int ix;
6868 struct dwarf2_per_cu_data *iter;
6869
6870 for (ix = 0;
6871 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6872 ix, iter);
6873 ++ix)
f4dc4d17
DE
6874 {
6875 if (! iter->is_debug_types)
6876 compute_symtab_includes (iter);
6877 }
95554aad
TT
6878
6879 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6880}
6881
9cdd5dbd 6882/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6883 already been loaded into memory. */
6884
6885static void
95554aad
TT
6886process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6887 enum language pretend_language)
10b3939b 6888{
10b3939b 6889 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6890 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6891 CORE_ADDR lowpc, highpc;
6892 struct symtab *symtab;
3da10d80 6893 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6894 CORE_ADDR baseaddr;
4359dff1 6895 struct block *static_block;
10b3939b
DJ
6896
6897 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6898
10b3939b
DJ
6899 buildsym_init ();
6900 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6901 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6902
6903 cu->list_in_scope = &file_symbols;
c906108c 6904
95554aad
TT
6905 cu->language = pretend_language;
6906 cu->language_defn = language_def (cu->language);
6907
c906108c 6908 /* Do line number decoding in read_file_scope () */
10b3939b 6909 process_die (cu->dies, cu);
c906108c 6910
a766d390
DE
6911 /* For now fudge the Go package. */
6912 if (cu->language == language_go)
6913 fixup_go_packaging (cu);
6914
3da10d80
KS
6915 /* Now that we have processed all the DIEs in the CU, all the types
6916 should be complete, and it should now be safe to compute all of the
6917 physnames. */
6918 compute_delayed_physnames (cu);
6919 do_cleanups (delayed_list_cleanup);
6920
fae299cd
DC
6921 /* Some compilers don't define a DW_AT_high_pc attribute for the
6922 compilation unit. If the DW_AT_high_pc is missing, synthesize
6923 it, by scanning the DIE's below the compilation unit. */
10b3939b 6924 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6925
36586728
TT
6926 static_block
6927 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6928 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6929
6930 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6931 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6932 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6933 addrmap to help ensure it has an accurate map of pc values belonging to
6934 this comp unit. */
6935 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6936
6937 symtab = end_symtab_from_static_block (static_block, objfile,
6938 SECT_OFF_TEXT (objfile), 0);
c906108c 6939
8be455d7 6940 if (symtab != NULL)
c906108c 6941 {
df15bd07 6942 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6943
8be455d7
JK
6944 /* Set symtab language to language from DW_AT_language. If the
6945 compilation is from a C file generated by language preprocessors, do
6946 not set the language if it was already deduced by start_subfile. */
6947 if (!(cu->language == language_c && symtab->language != language_c))
6948 symtab->language = cu->language;
6949
6950 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6951 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6952 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6953 there were bugs in prologue debug info, fixed later in GCC-4.5
6954 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6955
6956 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6957 needed, it would be wrong due to missing DW_AT_producer there.
6958
6959 Still one can confuse GDB by using non-standard GCC compilation
6960 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6961 */
ab260dad 6962 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6963 symtab->locations_valid = 1;
e0d00bc7
JK
6964
6965 if (gcc_4_minor >= 5)
6966 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6967
6968 symtab->call_site_htab = cu->call_site_htab;
c906108c 6969 }
9291a0cd
TT
6970
6971 if (dwarf2_per_objfile->using_index)
6972 per_cu->v.quick->symtab = symtab;
6973 else
6974 {
6975 struct partial_symtab *pst = per_cu->v.psymtab;
6976 pst->symtab = symtab;
6977 pst->readin = 1;
6978 }
c906108c 6979
95554aad
TT
6980 /* Push it for inclusion processing later. */
6981 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6982
c906108c 6983 do_cleanups (back_to);
f4dc4d17 6984}
45cfd468 6985
f4dc4d17
DE
6986/* Generate full symbol information for type unit PER_CU, whose DIEs have
6987 already been loaded into memory. */
6988
6989static void
6990process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6991 enum language pretend_language)
6992{
6993 struct dwarf2_cu *cu = per_cu->cu;
6994 struct objfile *objfile = per_cu->objfile;
6995 struct symtab *symtab;
6996 struct cleanup *back_to, *delayed_list_cleanup;
6997
6998 buildsym_init ();
6999 back_to = make_cleanup (really_free_pendings, NULL);
7000 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7001
7002 cu->list_in_scope = &file_symbols;
7003
7004 cu->language = pretend_language;
7005 cu->language_defn = language_def (cu->language);
7006
7007 /* The symbol tables are set up in read_type_unit_scope. */
7008 process_die (cu->dies, cu);
7009
7010 /* For now fudge the Go package. */
7011 if (cu->language == language_go)
7012 fixup_go_packaging (cu);
7013
7014 /* Now that we have processed all the DIEs in the CU, all the types
7015 should be complete, and it should now be safe to compute all of the
7016 physnames. */
7017 compute_delayed_physnames (cu);
7018 do_cleanups (delayed_list_cleanup);
7019
7020 /* TUs share symbol tables.
7021 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7022 of it with end_expandable_symtab. Otherwise, complete the addition of
7023 this TU's symbols to the existing symtab. */
f4dc4d17 7024 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7025 {
f4dc4d17
DE
7026 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7027 per_cu->s.type_unit_group->primary_symtab = symtab;
7028
7029 if (symtab != NULL)
7030 {
7031 /* Set symtab language to language from DW_AT_language. If the
7032 compilation is from a C file generated by language preprocessors,
7033 do not set the language if it was already deduced by
7034 start_subfile. */
7035 if (!(cu->language == language_c && symtab->language != language_c))
7036 symtab->language = cu->language;
7037 }
7038 }
7039 else
7040 {
7041 augment_type_symtab (objfile,
7042 per_cu->s.type_unit_group->primary_symtab);
7043 symtab = per_cu->s.type_unit_group->primary_symtab;
7044 }
7045
7046 if (dwarf2_per_objfile->using_index)
7047 per_cu->v.quick->symtab = symtab;
7048 else
7049 {
7050 struct partial_symtab *pst = per_cu->v.psymtab;
7051 pst->symtab = symtab;
7052 pst->readin = 1;
45cfd468 7053 }
f4dc4d17
DE
7054
7055 do_cleanups (back_to);
c906108c
SS
7056}
7057
95554aad
TT
7058/* Process an imported unit DIE. */
7059
7060static void
7061process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7062{
7063 struct attribute *attr;
7064
f4dc4d17
DE
7065 /* For now we don't handle imported units in type units. */
7066 if (cu->per_cu->is_debug_types)
7067 {
7068 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7069 " supported in type units [in module %s]"),
7070 cu->objfile->name);
7071 }
7072
95554aad
TT
7073 attr = dwarf2_attr (die, DW_AT_import, cu);
7074 if (attr != NULL)
7075 {
7076 struct dwarf2_per_cu_data *per_cu;
7077 struct symtab *imported_symtab;
7078 sect_offset offset;
36586728 7079 int is_dwz;
95554aad
TT
7080
7081 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7082 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7083 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7084
7085 /* Queue the unit, if needed. */
7086 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7087 load_full_comp_unit (per_cu, cu->language);
7088
f4dc4d17 7089 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7090 per_cu);
7091 }
7092}
7093
c906108c
SS
7094/* Process a die and its children. */
7095
7096static void
e7c27a73 7097process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7098{
7099 switch (die->tag)
7100 {
7101 case DW_TAG_padding:
7102 break;
7103 case DW_TAG_compile_unit:
95554aad 7104 case DW_TAG_partial_unit:
e7c27a73 7105 read_file_scope (die, cu);
c906108c 7106 break;
348e048f
DE
7107 case DW_TAG_type_unit:
7108 read_type_unit_scope (die, cu);
7109 break;
c906108c 7110 case DW_TAG_subprogram:
c906108c 7111 case DW_TAG_inlined_subroutine:
edb3359d 7112 read_func_scope (die, cu);
c906108c
SS
7113 break;
7114 case DW_TAG_lexical_block:
14898363
L
7115 case DW_TAG_try_block:
7116 case DW_TAG_catch_block:
e7c27a73 7117 read_lexical_block_scope (die, cu);
c906108c 7118 break;
96408a79
SA
7119 case DW_TAG_GNU_call_site:
7120 read_call_site_scope (die, cu);
7121 break;
c906108c 7122 case DW_TAG_class_type:
680b30c7 7123 case DW_TAG_interface_type:
c906108c
SS
7124 case DW_TAG_structure_type:
7125 case DW_TAG_union_type:
134d01f1 7126 process_structure_scope (die, cu);
c906108c
SS
7127 break;
7128 case DW_TAG_enumeration_type:
134d01f1 7129 process_enumeration_scope (die, cu);
c906108c 7130 break;
134d01f1 7131
f792889a
DJ
7132 /* These dies have a type, but processing them does not create
7133 a symbol or recurse to process the children. Therefore we can
7134 read them on-demand through read_type_die. */
c906108c 7135 case DW_TAG_subroutine_type:
72019c9c 7136 case DW_TAG_set_type:
c906108c 7137 case DW_TAG_array_type:
c906108c 7138 case DW_TAG_pointer_type:
c906108c 7139 case DW_TAG_ptr_to_member_type:
c906108c 7140 case DW_TAG_reference_type:
c906108c 7141 case DW_TAG_string_type:
c906108c 7142 break;
134d01f1 7143
c906108c 7144 case DW_TAG_base_type:
a02abb62 7145 case DW_TAG_subrange_type:
cb249c71 7146 case DW_TAG_typedef:
134d01f1
DJ
7147 /* Add a typedef symbol for the type definition, if it has a
7148 DW_AT_name. */
f792889a 7149 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7150 break;
c906108c 7151 case DW_TAG_common_block:
e7c27a73 7152 read_common_block (die, cu);
c906108c
SS
7153 break;
7154 case DW_TAG_common_inclusion:
7155 break;
d9fa45fe 7156 case DW_TAG_namespace:
63d06c5c 7157 processing_has_namespace_info = 1;
e7c27a73 7158 read_namespace (die, cu);
d9fa45fe 7159 break;
5d7cb8df 7160 case DW_TAG_module:
f55ee35c 7161 processing_has_namespace_info = 1;
5d7cb8df
JK
7162 read_module (die, cu);
7163 break;
d9fa45fe
DC
7164 case DW_TAG_imported_declaration:
7165 case DW_TAG_imported_module:
63d06c5c 7166 processing_has_namespace_info = 1;
27aa8d6a
SW
7167 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7168 || cu->language != language_fortran))
7169 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7170 dwarf_tag_name (die->tag));
7171 read_import_statement (die, cu);
d9fa45fe 7172 break;
95554aad
TT
7173
7174 case DW_TAG_imported_unit:
7175 process_imported_unit_die (die, cu);
7176 break;
7177
c906108c 7178 default:
e7c27a73 7179 new_symbol (die, NULL, cu);
c906108c
SS
7180 break;
7181 }
7182}
7183
94af9270
KS
7184/* A helper function for dwarf2_compute_name which determines whether DIE
7185 needs to have the name of the scope prepended to the name listed in the
7186 die. */
7187
7188static int
7189die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7190{
1c809c68
TT
7191 struct attribute *attr;
7192
94af9270
KS
7193 switch (die->tag)
7194 {
7195 case DW_TAG_namespace:
7196 case DW_TAG_typedef:
7197 case DW_TAG_class_type:
7198 case DW_TAG_interface_type:
7199 case DW_TAG_structure_type:
7200 case DW_TAG_union_type:
7201 case DW_TAG_enumeration_type:
7202 case DW_TAG_enumerator:
7203 case DW_TAG_subprogram:
7204 case DW_TAG_member:
7205 return 1;
7206
7207 case DW_TAG_variable:
c2b0a229 7208 case DW_TAG_constant:
94af9270
KS
7209 /* We only need to prefix "globally" visible variables. These include
7210 any variable marked with DW_AT_external or any variable that
7211 lives in a namespace. [Variables in anonymous namespaces
7212 require prefixing, but they are not DW_AT_external.] */
7213
7214 if (dwarf2_attr (die, DW_AT_specification, cu))
7215 {
7216 struct dwarf2_cu *spec_cu = cu;
9a619af0 7217
94af9270
KS
7218 return die_needs_namespace (die_specification (die, &spec_cu),
7219 spec_cu);
7220 }
7221
1c809c68 7222 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7223 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7224 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7225 return 0;
7226 /* A variable in a lexical block of some kind does not need a
7227 namespace, even though in C++ such variables may be external
7228 and have a mangled name. */
7229 if (die->parent->tag == DW_TAG_lexical_block
7230 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7231 || die->parent->tag == DW_TAG_catch_block
7232 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7233 return 0;
7234 return 1;
94af9270
KS
7235
7236 default:
7237 return 0;
7238 }
7239}
7240
98bfdba5
PA
7241/* Retrieve the last character from a mem_file. */
7242
7243static void
7244do_ui_file_peek_last (void *object, const char *buffer, long length)
7245{
7246 char *last_char_p = (char *) object;
7247
7248 if (length > 0)
7249 *last_char_p = buffer[length - 1];
7250}
7251
94af9270 7252/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7253 compute the physname for the object, which include a method's:
7254 - formal parameters (C++/Java),
7255 - receiver type (Go),
7256 - return type (Java).
7257
7258 The term "physname" is a bit confusing.
7259 For C++, for example, it is the demangled name.
7260 For Go, for example, it's the mangled name.
94af9270 7261
af6b7be1
JB
7262 For Ada, return the DIE's linkage name rather than the fully qualified
7263 name. PHYSNAME is ignored..
7264
94af9270
KS
7265 The result is allocated on the objfile_obstack and canonicalized. */
7266
7267static const char *
7268dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7269 int physname)
7270{
bb5ed363
DE
7271 struct objfile *objfile = cu->objfile;
7272
94af9270
KS
7273 if (name == NULL)
7274 name = dwarf2_name (die, cu);
7275
f55ee35c
JK
7276 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7277 compute it by typename_concat inside GDB. */
7278 if (cu->language == language_ada
7279 || (cu->language == language_fortran && physname))
7280 {
7281 /* For Ada unit, we prefer the linkage name over the name, as
7282 the former contains the exported name, which the user expects
7283 to be able to reference. Ideally, we want the user to be able
7284 to reference this entity using either natural or linkage name,
7285 but we haven't started looking at this enhancement yet. */
7286 struct attribute *attr;
7287
7288 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7289 if (attr == NULL)
7290 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7291 if (attr && DW_STRING (attr))
7292 return DW_STRING (attr);
7293 }
7294
94af9270
KS
7295 /* These are the only languages we know how to qualify names in. */
7296 if (name != NULL
f55ee35c
JK
7297 && (cu->language == language_cplus || cu->language == language_java
7298 || cu->language == language_fortran))
94af9270
KS
7299 {
7300 if (die_needs_namespace (die, cu))
7301 {
7302 long length;
0d5cff50 7303 const char *prefix;
94af9270
KS
7304 struct ui_file *buf;
7305
7306 prefix = determine_prefix (die, cu);
7307 buf = mem_fileopen ();
7308 if (*prefix != '\0')
7309 {
f55ee35c
JK
7310 char *prefixed_name = typename_concat (NULL, prefix, name,
7311 physname, cu);
9a619af0 7312
94af9270
KS
7313 fputs_unfiltered (prefixed_name, buf);
7314 xfree (prefixed_name);
7315 }
7316 else
62d5b8da 7317 fputs_unfiltered (name, buf);
94af9270 7318
98bfdba5
PA
7319 /* Template parameters may be specified in the DIE's DW_AT_name, or
7320 as children with DW_TAG_template_type_param or
7321 DW_TAG_value_type_param. If the latter, add them to the name
7322 here. If the name already has template parameters, then
7323 skip this step; some versions of GCC emit both, and
7324 it is more efficient to use the pre-computed name.
7325
7326 Something to keep in mind about this process: it is very
7327 unlikely, or in some cases downright impossible, to produce
7328 something that will match the mangled name of a function.
7329 If the definition of the function has the same debug info,
7330 we should be able to match up with it anyway. But fallbacks
7331 using the minimal symbol, for instance to find a method
7332 implemented in a stripped copy of libstdc++, will not work.
7333 If we do not have debug info for the definition, we will have to
7334 match them up some other way.
7335
7336 When we do name matching there is a related problem with function
7337 templates; two instantiated function templates are allowed to
7338 differ only by their return types, which we do not add here. */
7339
7340 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7341 {
7342 struct attribute *attr;
7343 struct die_info *child;
7344 int first = 1;
7345
7346 die->building_fullname = 1;
7347
7348 for (child = die->child; child != NULL; child = child->sibling)
7349 {
7350 struct type *type;
12df843f 7351 LONGEST value;
98bfdba5
PA
7352 gdb_byte *bytes;
7353 struct dwarf2_locexpr_baton *baton;
7354 struct value *v;
7355
7356 if (child->tag != DW_TAG_template_type_param
7357 && child->tag != DW_TAG_template_value_param)
7358 continue;
7359
7360 if (first)
7361 {
7362 fputs_unfiltered ("<", buf);
7363 first = 0;
7364 }
7365 else
7366 fputs_unfiltered (", ", buf);
7367
7368 attr = dwarf2_attr (child, DW_AT_type, cu);
7369 if (attr == NULL)
7370 {
7371 complaint (&symfile_complaints,
7372 _("template parameter missing DW_AT_type"));
7373 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7374 continue;
7375 }
7376 type = die_type (child, cu);
7377
7378 if (child->tag == DW_TAG_template_type_param)
7379 {
79d43c61 7380 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7381 continue;
7382 }
7383
7384 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7385 if (attr == NULL)
7386 {
7387 complaint (&symfile_complaints,
3e43a32a
MS
7388 _("template parameter missing "
7389 "DW_AT_const_value"));
98bfdba5
PA
7390 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7391 continue;
7392 }
7393
7394 dwarf2_const_value_attr (attr, type, name,
7395 &cu->comp_unit_obstack, cu,
7396 &value, &bytes, &baton);
7397
7398 if (TYPE_NOSIGN (type))
7399 /* GDB prints characters as NUMBER 'CHAR'. If that's
7400 changed, this can use value_print instead. */
7401 c_printchar (value, type, buf);
7402 else
7403 {
7404 struct value_print_options opts;
7405
7406 if (baton != NULL)
7407 v = dwarf2_evaluate_loc_desc (type, NULL,
7408 baton->data,
7409 baton->size,
7410 baton->per_cu);
7411 else if (bytes != NULL)
7412 {
7413 v = allocate_value (type);
7414 memcpy (value_contents_writeable (v), bytes,
7415 TYPE_LENGTH (type));
7416 }
7417 else
7418 v = value_from_longest (type, value);
7419
3e43a32a
MS
7420 /* Specify decimal so that we do not depend on
7421 the radix. */
98bfdba5
PA
7422 get_formatted_print_options (&opts, 'd');
7423 opts.raw = 1;
7424 value_print (v, buf, &opts);
7425 release_value (v);
7426 value_free (v);
7427 }
7428 }
7429
7430 die->building_fullname = 0;
7431
7432 if (!first)
7433 {
7434 /* Close the argument list, with a space if necessary
7435 (nested templates). */
7436 char last_char = '\0';
7437 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7438 if (last_char == '>')
7439 fputs_unfiltered (" >", buf);
7440 else
7441 fputs_unfiltered (">", buf);
7442 }
7443 }
7444
94af9270
KS
7445 /* For Java and C++ methods, append formal parameter type
7446 information, if PHYSNAME. */
6e70227d 7447
94af9270
KS
7448 if (physname && die->tag == DW_TAG_subprogram
7449 && (cu->language == language_cplus
7450 || cu->language == language_java))
7451 {
7452 struct type *type = read_type_die (die, cu);
7453
79d43c61
TT
7454 c_type_print_args (type, buf, 1, cu->language,
7455 &type_print_raw_options);
94af9270
KS
7456
7457 if (cu->language == language_java)
7458 {
7459 /* For java, we must append the return type to method
0963b4bd 7460 names. */
94af9270
KS
7461 if (die->tag == DW_TAG_subprogram)
7462 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7463 0, 0, &type_print_raw_options);
94af9270
KS
7464 }
7465 else if (cu->language == language_cplus)
7466 {
60430eff
DJ
7467 /* Assume that an artificial first parameter is
7468 "this", but do not crash if it is not. RealView
7469 marks unnamed (and thus unused) parameters as
7470 artificial; there is no way to differentiate
7471 the two cases. */
94af9270
KS
7472 if (TYPE_NFIELDS (type) > 0
7473 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7474 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7475 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7476 0))))
94af9270
KS
7477 fputs_unfiltered (" const", buf);
7478 }
7479 }
7480
bb5ed363 7481 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7482 &length);
7483 ui_file_delete (buf);
7484
7485 if (cu->language == language_cplus)
7486 {
7487 char *cname
7488 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7489 &objfile->objfile_obstack);
9a619af0 7490
94af9270
KS
7491 if (cname != NULL)
7492 name = cname;
7493 }
7494 }
7495 }
7496
7497 return name;
7498}
7499
0114d602
DJ
7500/* Return the fully qualified name of DIE, based on its DW_AT_name.
7501 If scope qualifiers are appropriate they will be added. The result
7502 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7503 not have a name. NAME may either be from a previous call to
7504 dwarf2_name or NULL.
7505
0963b4bd 7506 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7507
7508static const char *
94af9270 7509dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7510{
94af9270
KS
7511 return dwarf2_compute_name (name, die, cu, 0);
7512}
0114d602 7513
94af9270
KS
7514/* Construct a physname for the given DIE in CU. NAME may either be
7515 from a previous call to dwarf2_name or NULL. The result will be
7516 allocated on the objfile_objstack or NULL if the DIE does not have a
7517 name.
0114d602 7518
94af9270 7519 The output string will be canonicalized (if C++/Java). */
0114d602 7520
94af9270
KS
7521static const char *
7522dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7523{
bb5ed363 7524 struct objfile *objfile = cu->objfile;
900e11f9
JK
7525 struct attribute *attr;
7526 const char *retval, *mangled = NULL, *canon = NULL;
7527 struct cleanup *back_to;
7528 int need_copy = 1;
7529
7530 /* In this case dwarf2_compute_name is just a shortcut not building anything
7531 on its own. */
7532 if (!die_needs_namespace (die, cu))
7533 return dwarf2_compute_name (name, die, cu, 1);
7534
7535 back_to = make_cleanup (null_cleanup, NULL);
7536
7537 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7538 if (!attr)
7539 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7540
7541 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7542 has computed. */
7543 if (attr && DW_STRING (attr))
7544 {
7545 char *demangled;
7546
7547 mangled = DW_STRING (attr);
7548
7549 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7550 type. It is easier for GDB users to search for such functions as
7551 `name(params)' than `long name(params)'. In such case the minimal
7552 symbol names do not match the full symbol names but for template
7553 functions there is never a need to look up their definition from their
7554 declaration so the only disadvantage remains the minimal symbol
7555 variant `long name(params)' does not have the proper inferior type.
7556 */
7557
a766d390
DE
7558 if (cu->language == language_go)
7559 {
7560 /* This is a lie, but we already lie to the caller new_symbol_full.
7561 new_symbol_full assumes we return the mangled name.
7562 This just undoes that lie until things are cleaned up. */
7563 demangled = NULL;
7564 }
7565 else
7566 {
7567 demangled = cplus_demangle (mangled,
7568 (DMGL_PARAMS | DMGL_ANSI
7569 | (cu->language == language_java
7570 ? DMGL_JAVA | DMGL_RET_POSTFIX
7571 : DMGL_RET_DROP)));
7572 }
900e11f9
JK
7573 if (demangled)
7574 {
7575 make_cleanup (xfree, demangled);
7576 canon = demangled;
7577 }
7578 else
7579 {
7580 canon = mangled;
7581 need_copy = 0;
7582 }
7583 }
7584
7585 if (canon == NULL || check_physname)
7586 {
7587 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7588
7589 if (canon != NULL && strcmp (physname, canon) != 0)
7590 {
7591 /* It may not mean a bug in GDB. The compiler could also
7592 compute DW_AT_linkage_name incorrectly. But in such case
7593 GDB would need to be bug-to-bug compatible. */
7594
7595 complaint (&symfile_complaints,
7596 _("Computed physname <%s> does not match demangled <%s> "
7597 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7598 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7599
7600 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7601 is available here - over computed PHYSNAME. It is safer
7602 against both buggy GDB and buggy compilers. */
7603
7604 retval = canon;
7605 }
7606 else
7607 {
7608 retval = physname;
7609 need_copy = 0;
7610 }
7611 }
7612 else
7613 retval = canon;
7614
7615 if (need_copy)
7616 retval = obsavestring (retval, strlen (retval),
bb5ed363 7617 &objfile->objfile_obstack);
900e11f9
JK
7618
7619 do_cleanups (back_to);
7620 return retval;
0114d602
DJ
7621}
7622
27aa8d6a
SW
7623/* Read the import statement specified by the given die and record it. */
7624
7625static void
7626read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7627{
bb5ed363 7628 struct objfile *objfile = cu->objfile;
27aa8d6a 7629 struct attribute *import_attr;
32019081 7630 struct die_info *imported_die, *child_die;
de4affc9 7631 struct dwarf2_cu *imported_cu;
27aa8d6a 7632 const char *imported_name;
794684b6 7633 const char *imported_name_prefix;
13387711
SW
7634 const char *canonical_name;
7635 const char *import_alias;
7636 const char *imported_declaration = NULL;
794684b6 7637 const char *import_prefix;
32019081
JK
7638 VEC (const_char_ptr) *excludes = NULL;
7639 struct cleanup *cleanups;
13387711
SW
7640
7641 char *temp;
27aa8d6a
SW
7642
7643 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7644 if (import_attr == NULL)
7645 {
7646 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7647 dwarf_tag_name (die->tag));
7648 return;
7649 }
7650
de4affc9
CC
7651 imported_cu = cu;
7652 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7653 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7654 if (imported_name == NULL)
7655 {
7656 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7657
7658 The import in the following code:
7659 namespace A
7660 {
7661 typedef int B;
7662 }
7663
7664 int main ()
7665 {
7666 using A::B;
7667 B b;
7668 return b;
7669 }
7670
7671 ...
7672 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7673 <52> DW_AT_decl_file : 1
7674 <53> DW_AT_decl_line : 6
7675 <54> DW_AT_import : <0x75>
7676 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7677 <59> DW_AT_name : B
7678 <5b> DW_AT_decl_file : 1
7679 <5c> DW_AT_decl_line : 2
7680 <5d> DW_AT_type : <0x6e>
7681 ...
7682 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7683 <76> DW_AT_byte_size : 4
7684 <77> DW_AT_encoding : 5 (signed)
7685
7686 imports the wrong die ( 0x75 instead of 0x58 ).
7687 This case will be ignored until the gcc bug is fixed. */
7688 return;
7689 }
7690
82856980
SW
7691 /* Figure out the local name after import. */
7692 import_alias = dwarf2_name (die, cu);
27aa8d6a 7693
794684b6
SW
7694 /* Figure out where the statement is being imported to. */
7695 import_prefix = determine_prefix (die, cu);
7696
7697 /* Figure out what the scope of the imported die is and prepend it
7698 to the name of the imported die. */
de4affc9 7699 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7700
f55ee35c
JK
7701 if (imported_die->tag != DW_TAG_namespace
7702 && imported_die->tag != DW_TAG_module)
794684b6 7703 {
13387711
SW
7704 imported_declaration = imported_name;
7705 canonical_name = imported_name_prefix;
794684b6 7706 }
13387711 7707 else if (strlen (imported_name_prefix) > 0)
794684b6 7708 {
13387711
SW
7709 temp = alloca (strlen (imported_name_prefix)
7710 + 2 + strlen (imported_name) + 1);
7711 strcpy (temp, imported_name_prefix);
7712 strcat (temp, "::");
7713 strcat (temp, imported_name);
7714 canonical_name = temp;
794684b6 7715 }
13387711
SW
7716 else
7717 canonical_name = imported_name;
794684b6 7718
32019081
JK
7719 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7720
7721 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7722 for (child_die = die->child; child_die && child_die->tag;
7723 child_die = sibling_die (child_die))
7724 {
7725 /* DWARF-4: A Fortran use statement with a “rename list” may be
7726 represented by an imported module entry with an import attribute
7727 referring to the module and owned entries corresponding to those
7728 entities that are renamed as part of being imported. */
7729
7730 if (child_die->tag != DW_TAG_imported_declaration)
7731 {
7732 complaint (&symfile_complaints,
7733 _("child DW_TAG_imported_declaration expected "
7734 "- DIE at 0x%x [in module %s]"),
b64f50a1 7735 child_die->offset.sect_off, objfile->name);
32019081
JK
7736 continue;
7737 }
7738
7739 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7740 if (import_attr == NULL)
7741 {
7742 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7743 dwarf_tag_name (child_die->tag));
7744 continue;
7745 }
7746
7747 imported_cu = cu;
7748 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7749 &imported_cu);
7750 imported_name = dwarf2_name (imported_die, imported_cu);
7751 if (imported_name == NULL)
7752 {
7753 complaint (&symfile_complaints,
7754 _("child DW_TAG_imported_declaration has unknown "
7755 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7756 child_die->offset.sect_off, objfile->name);
32019081
JK
7757 continue;
7758 }
7759
7760 VEC_safe_push (const_char_ptr, excludes, imported_name);
7761
7762 process_die (child_die, cu);
7763 }
7764
c0cc3a76
SW
7765 cp_add_using_directive (import_prefix,
7766 canonical_name,
7767 import_alias,
13387711 7768 imported_declaration,
32019081 7769 excludes,
bb5ed363 7770 &objfile->objfile_obstack);
32019081
JK
7771
7772 do_cleanups (cleanups);
27aa8d6a
SW
7773}
7774
f4dc4d17 7775/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7776
cb1df416
DJ
7777static void
7778free_cu_line_header (void *arg)
7779{
7780 struct dwarf2_cu *cu = arg;
7781
7782 free_line_header (cu->line_header);
7783 cu->line_header = NULL;
7784}
7785
1b80a9fa
JK
7786/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7787 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7788 this, it was first present in GCC release 4.3.0. */
7789
7790static int
7791producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7792{
7793 if (!cu->checked_producer)
7794 check_producer (cu);
7795
7796 return cu->producer_is_gcc_lt_4_3;
7797}
7798
9291a0cd
TT
7799static void
7800find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7801 char **name, char **comp_dir)
7802{
7803 struct attribute *attr;
7804
7805 *name = NULL;
7806 *comp_dir = NULL;
7807
7808 /* Find the filename. Do not use dwarf2_name here, since the filename
7809 is not a source language identifier. */
7810 attr = dwarf2_attr (die, DW_AT_name, cu);
7811 if (attr)
7812 {
7813 *name = DW_STRING (attr);
7814 }
7815
7816 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7817 if (attr)
7818 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7819 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7820 && IS_ABSOLUTE_PATH (*name))
9291a0cd
TT
7821 {
7822 *comp_dir = ldirname (*name);
7823 if (*comp_dir != NULL)
7824 make_cleanup (xfree, *comp_dir);
7825 }
7826 if (*comp_dir != NULL)
7827 {
7828 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7829 directory, get rid of it. */
7830 char *cp = strchr (*comp_dir, ':');
7831
7832 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7833 *comp_dir = cp + 1;
7834 }
7835
7836 if (*name == NULL)
7837 *name = "<unknown>";
7838}
7839
f4dc4d17
DE
7840/* Handle DW_AT_stmt_list for a compilation unit.
7841 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7842 COMP_DIR is the compilation directory.
7843 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7844
7845static void
7846handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7847 const char *comp_dir)
2ab95328
TT
7848{
7849 struct attribute *attr;
2ab95328 7850
f4dc4d17
DE
7851 gdb_assert (! cu->per_cu->is_debug_types);
7852
2ab95328
TT
7853 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7854 if (attr)
7855 {
7856 unsigned int line_offset = DW_UNSND (attr);
7857 struct line_header *line_header
3019eac3 7858 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7859
7860 if (line_header)
dee91e82
DE
7861 {
7862 cu->line_header = line_header;
7863 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7864 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7865 }
2ab95328
TT
7866 }
7867}
7868
95554aad 7869/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7870
c906108c 7871static void
e7c27a73 7872read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7873{
dee91e82 7874 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7875 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7876 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7877 CORE_ADDR highpc = ((CORE_ADDR) 0);
7878 struct attribute *attr;
e1024ff1 7879 char *name = NULL;
c906108c
SS
7880 char *comp_dir = NULL;
7881 struct die_info *child_die;
7882 bfd *abfd = objfile->obfd;
e142c38c 7883 CORE_ADDR baseaddr;
6e70227d 7884
e142c38c 7885 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7886
fae299cd 7887 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7888
7889 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7890 from finish_block. */
2acceee2 7891 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7892 lowpc = highpc;
7893 lowpc += baseaddr;
7894 highpc += baseaddr;
7895
9291a0cd 7896 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7897
95554aad 7898 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7899
f4b8a18d
KW
7900 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7901 standardised yet. As a workaround for the language detection we fall
7902 back to the DW_AT_producer string. */
7903 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7904 cu->language = language_opencl;
7905
3019eac3
DE
7906 /* Similar hack for Go. */
7907 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7908 set_cu_language (DW_LANG_Go, cu);
7909
f4dc4d17 7910 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7911
7912 /* Decode line number information if present. We do this before
7913 processing child DIEs, so that the line header table is available
7914 for DW_AT_decl_file. */
f4dc4d17 7915 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7916
7917 /* Process all dies in compilation unit. */
7918 if (die->child != NULL)
7919 {
7920 child_die = die->child;
7921 while (child_die && child_die->tag)
7922 {
7923 process_die (child_die, cu);
7924 child_die = sibling_die (child_die);
7925 }
7926 }
7927
7928 /* Decode macro information, if present. Dwarf 2 macro information
7929 refers to information in the line number info statement program
7930 header, so we can only read it if we've read the header
7931 successfully. */
7932 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7933 if (attr && cu->line_header)
7934 {
7935 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7936 complaint (&symfile_complaints,
7937 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7938
09262596 7939 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7940 }
7941 else
7942 {
7943 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7944 if (attr && cu->line_header)
7945 {
7946 unsigned int macro_offset = DW_UNSND (attr);
7947
09262596 7948 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7949 }
7950 }
7951
7952 do_cleanups (back_to);
7953}
7954
f4dc4d17
DE
7955/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7956 Create the set of symtabs used by this TU, or if this TU is sharing
7957 symtabs with another TU and the symtabs have already been created
7958 then restore those symtabs in the line header.
7959 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7960
7961static void
f4dc4d17 7962setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7963{
f4dc4d17
DE
7964 struct objfile *objfile = dwarf2_per_objfile->objfile;
7965 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7966 struct type_unit_group *tu_group;
7967 int first_time;
7968 struct line_header *lh;
3019eac3 7969 struct attribute *attr;
f4dc4d17 7970 unsigned int i, line_offset;
3019eac3 7971
f4dc4d17 7972 gdb_assert (per_cu->is_debug_types);
3019eac3 7973
f4dc4d17 7974 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 7975
f4dc4d17
DE
7976 /* If we're using .gdb_index (includes -readnow) then
7977 per_cu->s.type_unit_group may not have been set up yet. */
7978 if (per_cu->s.type_unit_group == NULL)
094b34ac 7979 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
7980 tu_group = per_cu->s.type_unit_group;
7981
7982 /* If we've already processed this stmt_list there's no real need to
7983 do it again, we could fake it and just recreate the part we need
7984 (file name,index -> symtab mapping). If data shows this optimization
7985 is useful we can do it then. */
7986 first_time = tu_group->primary_symtab == NULL;
7987
7988 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7989 debug info. */
7990 lh = NULL;
7991 if (attr != NULL)
3019eac3 7992 {
f4dc4d17
DE
7993 line_offset = DW_UNSND (attr);
7994 lh = dwarf_decode_line_header (line_offset, cu);
7995 }
7996 if (lh == NULL)
7997 {
7998 if (first_time)
7999 dwarf2_start_symtab (cu, "", NULL, 0);
8000 else
8001 {
8002 gdb_assert (tu_group->symtabs == NULL);
8003 restart_symtab (0);
8004 }
8005 /* Note: The primary symtab will get allocated at the end. */
8006 return;
3019eac3
DE
8007 }
8008
f4dc4d17
DE
8009 cu->line_header = lh;
8010 make_cleanup (free_cu_line_header, cu);
3019eac3 8011
f4dc4d17
DE
8012 if (first_time)
8013 {
8014 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8015
f4dc4d17
DE
8016 tu_group->num_symtabs = lh->num_file_names;
8017 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8018
f4dc4d17
DE
8019 for (i = 0; i < lh->num_file_names; ++i)
8020 {
8021 char *dir = NULL;
8022 struct file_entry *fe = &lh->file_names[i];
3019eac3 8023
f4dc4d17
DE
8024 if (fe->dir_index)
8025 dir = lh->include_dirs[fe->dir_index - 1];
8026 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8027
f4dc4d17
DE
8028 /* Note: We don't have to watch for the main subfile here, type units
8029 don't have DW_AT_name. */
3019eac3 8030
f4dc4d17
DE
8031 if (current_subfile->symtab == NULL)
8032 {
8033 /* NOTE: start_subfile will recognize when it's been passed
8034 a file it has already seen. So we can't assume there's a
8035 simple mapping from lh->file_names to subfiles,
8036 lh->file_names may contain dups. */
8037 current_subfile->symtab = allocate_symtab (current_subfile->name,
8038 objfile);
8039 }
8040
8041 fe->symtab = current_subfile->symtab;
8042 tu_group->symtabs[i] = fe->symtab;
8043 }
8044 }
8045 else
3019eac3 8046 {
f4dc4d17
DE
8047 restart_symtab (0);
8048
8049 for (i = 0; i < lh->num_file_names; ++i)
8050 {
8051 struct file_entry *fe = &lh->file_names[i];
8052
8053 fe->symtab = tu_group->symtabs[i];
8054 }
3019eac3
DE
8055 }
8056
f4dc4d17
DE
8057 /* The main symtab is allocated last. Type units don't have DW_AT_name
8058 so they don't have a "real" (so to speak) symtab anyway.
8059 There is later code that will assign the main symtab to all symbols
8060 that don't have one. We need to handle the case of a symbol with a
8061 missing symtab (DW_AT_decl_file) anyway. */
8062}
3019eac3 8063
f4dc4d17
DE
8064/* Process DW_TAG_type_unit.
8065 For TUs we want to skip the first top level sibling if it's not the
8066 actual type being defined by this TU. In this case the first top
8067 level sibling is there to provide context only. */
3019eac3 8068
f4dc4d17
DE
8069static void
8070read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8071{
8072 struct die_info *child_die;
3019eac3 8073
f4dc4d17
DE
8074 prepare_one_comp_unit (cu, die, language_minimal);
8075
8076 /* Initialize (or reinitialize) the machinery for building symtabs.
8077 We do this before processing child DIEs, so that the line header table
8078 is available for DW_AT_decl_file. */
8079 setup_type_unit_groups (die, cu);
8080
8081 if (die->child != NULL)
8082 {
8083 child_die = die->child;
8084 while (child_die && child_die->tag)
8085 {
8086 process_die (child_die, cu);
8087 child_die = sibling_die (child_die);
8088 }
8089 }
3019eac3
DE
8090}
8091\f
80626a55
DE
8092/* DWO/DWP files.
8093
8094 http://gcc.gnu.org/wiki/DebugFission
8095 http://gcc.gnu.org/wiki/DebugFissionDWP
8096
8097 To simplify handling of both DWO files ("object" files with the DWARF info)
8098 and DWP files (a file with the DWOs packaged up into one file), we treat
8099 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8100
8101static hashval_t
8102hash_dwo_file (const void *item)
8103{
8104 const struct dwo_file *dwo_file = item;
8105
80626a55 8106 return htab_hash_string (dwo_file->name);
3019eac3
DE
8107}
8108
8109static int
8110eq_dwo_file (const void *item_lhs, const void *item_rhs)
8111{
8112 const struct dwo_file *lhs = item_lhs;
8113 const struct dwo_file *rhs = item_rhs;
8114
80626a55 8115 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8116}
8117
8118/* Allocate a hash table for DWO files. */
8119
8120static htab_t
8121allocate_dwo_file_hash_table (void)
8122{
8123 struct objfile *objfile = dwarf2_per_objfile->objfile;
8124
8125 return htab_create_alloc_ex (41,
8126 hash_dwo_file,
8127 eq_dwo_file,
8128 NULL,
8129 &objfile->objfile_obstack,
8130 hashtab_obstack_allocate,
8131 dummy_obstack_deallocate);
8132}
8133
80626a55
DE
8134/* Lookup DWO file DWO_NAME. */
8135
8136static void **
8137lookup_dwo_file_slot (const char *dwo_name)
8138{
8139 struct dwo_file find_entry;
8140 void **slot;
8141
8142 if (dwarf2_per_objfile->dwo_files == NULL)
8143 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8144
8145 memset (&find_entry, 0, sizeof (find_entry));
8146 find_entry.name = dwo_name;
8147 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8148
8149 return slot;
8150}
8151
3019eac3
DE
8152static hashval_t
8153hash_dwo_unit (const void *item)
8154{
8155 const struct dwo_unit *dwo_unit = item;
8156
8157 /* This drops the top 32 bits of the id, but is ok for a hash. */
8158 return dwo_unit->signature;
8159}
8160
8161static int
8162eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8163{
8164 const struct dwo_unit *lhs = item_lhs;
8165 const struct dwo_unit *rhs = item_rhs;
8166
8167 /* The signature is assumed to be unique within the DWO file.
8168 So while object file CU dwo_id's always have the value zero,
8169 that's OK, assuming each object file DWO file has only one CU,
8170 and that's the rule for now. */
8171 return lhs->signature == rhs->signature;
8172}
8173
8174/* Allocate a hash table for DWO CUs,TUs.
8175 There is one of these tables for each of CUs,TUs for each DWO file. */
8176
8177static htab_t
8178allocate_dwo_unit_table (struct objfile *objfile)
8179{
8180 /* Start out with a pretty small number.
8181 Generally DWO files contain only one CU and maybe some TUs. */
8182 return htab_create_alloc_ex (3,
8183 hash_dwo_unit,
8184 eq_dwo_unit,
8185 NULL,
8186 &objfile->objfile_obstack,
8187 hashtab_obstack_allocate,
8188 dummy_obstack_deallocate);
8189}
8190
80626a55 8191/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8192
8193struct create_dwo_info_table_data
8194{
8195 struct dwo_file *dwo_file;
8196 htab_t cu_htab;
8197};
8198
80626a55 8199/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8200
8201static void
80626a55
DE
8202create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8203 gdb_byte *info_ptr,
8204 struct die_info *comp_unit_die,
8205 int has_children,
8206 void *datap)
3019eac3
DE
8207{
8208 struct dwarf2_cu *cu = reader->cu;
8209 struct objfile *objfile = dwarf2_per_objfile->objfile;
8210 sect_offset offset = cu->per_cu->offset;
8211 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8212 struct create_dwo_info_table_data *data = datap;
8213 struct dwo_file *dwo_file = data->dwo_file;
8214 htab_t cu_htab = data->cu_htab;
8215 void **slot;
8216 struct attribute *attr;
8217 struct dwo_unit *dwo_unit;
8218
8219 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8220 if (attr == NULL)
8221 {
8222 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8223 " its dwo_id [in module %s]"),
80626a55 8224 offset.sect_off, dwo_file->name);
3019eac3
DE
8225 return;
8226 }
8227
8228 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8229 dwo_unit->dwo_file = dwo_file;
8230 dwo_unit->signature = DW_UNSND (attr);
8231 dwo_unit->info_or_types_section = section;
8232 dwo_unit->offset = offset;
8233 dwo_unit->length = cu->per_cu->length;
8234
8235 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8236 gdb_assert (slot != NULL);
8237 if (*slot != NULL)
8238 {
8239 const struct dwo_unit *dup_dwo_unit = *slot;
8240
8241 complaint (&symfile_complaints,
8242 _("debug entry at offset 0x%x is duplicate to the entry at"
8243 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8244 offset.sect_off, dup_dwo_unit->offset.sect_off,
8245 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8246 dwo_file->name);
3019eac3
DE
8247 }
8248 else
8249 *slot = dwo_unit;
8250
09406207 8251 if (dwarf2_read_debug)
3019eac3
DE
8252 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8253 offset.sect_off,
8254 phex (dwo_unit->signature,
8255 sizeof (dwo_unit->signature)));
8256}
8257
80626a55
DE
8258/* Create a hash table to map DWO IDs to their CU entry in
8259 .debug_info.dwo in DWO_FILE.
8260 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8261
8262static htab_t
80626a55 8263create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8264{
8265 struct objfile *objfile = dwarf2_per_objfile->objfile;
8266 struct dwarf2_section_info *section = &dwo_file->sections.info;
8267 bfd *abfd;
8268 htab_t cu_htab;
8269 gdb_byte *info_ptr, *end_ptr;
8270 struct create_dwo_info_table_data create_dwo_info_table_data;
8271
8272 dwarf2_read_section (objfile, section);
8273 info_ptr = section->buffer;
8274
8275 if (info_ptr == NULL)
8276 return NULL;
8277
8278 /* We can't set abfd until now because the section may be empty or
8279 not present, in which case section->asection will be NULL. */
8280 abfd = section->asection->owner;
8281
09406207 8282 if (dwarf2_read_debug)
3019eac3
DE
8283 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8284 bfd_get_filename (abfd));
8285
8286 cu_htab = allocate_dwo_unit_table (objfile);
8287
8288 create_dwo_info_table_data.dwo_file = dwo_file;
8289 create_dwo_info_table_data.cu_htab = cu_htab;
8290
8291 end_ptr = info_ptr + section->size;
8292 while (info_ptr < end_ptr)
8293 {
8294 struct dwarf2_per_cu_data per_cu;
8295
8296 memset (&per_cu, 0, sizeof (per_cu));
8297 per_cu.objfile = objfile;
8298 per_cu.is_debug_types = 0;
8299 per_cu.offset.sect_off = info_ptr - section->buffer;
8300 per_cu.info_or_types_section = section;
8301
8302 init_cutu_and_read_dies_no_follow (&per_cu,
8303 &dwo_file->sections.abbrev,
8304 dwo_file,
80626a55 8305 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8306 &create_dwo_info_table_data);
8307
8308 info_ptr += per_cu.length;
8309 }
8310
8311 return cu_htab;
8312}
8313
80626a55
DE
8314/* DWP file .debug_{cu,tu}_index section format:
8315 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8316
8317 Both index sections have the same format, and serve to map a 64-bit
8318 signature to a set of section numbers. Each section begins with a header,
8319 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8320 indexes, and a pool of 32-bit section numbers. The index sections will be
8321 aligned at 8-byte boundaries in the file.
8322
8323 The index section header contains two unsigned 32-bit values (using the
8324 byte order of the application binary):
8325
8326 N, the number of compilation units or type units in the index
8327 M, the number of slots in the hash table
8328
8329 (We assume that N and M will not exceed 2^32 - 1.)
8330
8331 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8332
8333 The hash table begins at offset 8 in the section, and consists of an array
8334 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8335 order of the application binary). Unused slots in the hash table are 0.
8336 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8337
8338 The parallel table begins immediately after the hash table
8339 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8340 array of 32-bit indexes (using the byte order of the application binary),
8341 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8342 table contains a 32-bit index into the pool of section numbers. For unused
8343 hash table slots, the corresponding entry in the parallel table will be 0.
8344
8345 Given a 64-bit compilation unit signature or a type signature S, an entry
8346 in the hash table is located as follows:
8347
8348 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8349 the low-order k bits all set to 1.
8350
8351 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8352
8353 3) If the hash table entry at index H matches the signature, use that
8354 entry. If the hash table entry at index H is unused (all zeroes),
8355 terminate the search: the signature is not present in the table.
8356
8357 4) Let H = (H + H') modulo M. Repeat at Step 3.
8358
8359 Because M > N and H' and M are relatively prime, the search is guaranteed
8360 to stop at an unused slot or find the match.
8361
8362 The pool of section numbers begins immediately following the hash table
8363 (at offset 8 + 12 * M from the beginning of the section). The pool of
8364 section numbers consists of an array of 32-bit words (using the byte order
8365 of the application binary). Each item in the array is indexed starting
8366 from 0. The hash table entry provides the index of the first section
8367 number in the set. Additional section numbers in the set follow, and the
8368 set is terminated by a 0 entry (section number 0 is not used in ELF).
8369
8370 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8371 section must be the first entry in the set, and the .debug_abbrev.dwo must
8372 be the second entry. Other members of the set may follow in any order. */
8373
8374/* Create a hash table to map DWO IDs to their CU/TU entry in
8375 .debug_{info,types}.dwo in DWP_FILE.
8376 Returns NULL if there isn't one.
8377 Note: This function processes DWP files only, not DWO files. */
8378
8379static struct dwp_hash_table *
8380create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8381{
8382 struct objfile *objfile = dwarf2_per_objfile->objfile;
8383 bfd *dbfd = dwp_file->dbfd;
8384 char *index_ptr, *index_end;
8385 struct dwarf2_section_info *index;
8386 uint32_t version, nr_units, nr_slots;
8387 struct dwp_hash_table *htab;
8388
8389 if (is_debug_types)
8390 index = &dwp_file->sections.tu_index;
8391 else
8392 index = &dwp_file->sections.cu_index;
8393
8394 if (dwarf2_section_empty_p (index))
8395 return NULL;
8396 dwarf2_read_section (objfile, index);
8397
8398 index_ptr = index->buffer;
8399 index_end = index_ptr + index->size;
8400
8401 version = read_4_bytes (dbfd, index_ptr);
8402 index_ptr += 8; /* Skip the unused word. */
8403 nr_units = read_4_bytes (dbfd, index_ptr);
8404 index_ptr += 4;
8405 nr_slots = read_4_bytes (dbfd, index_ptr);
8406 index_ptr += 4;
8407
8408 if (version != 1)
8409 {
8410 error (_("Dwarf Error: unsupported DWP file version (%u)"
8411 " [in module %s]"),
8412 version, dwp_file->name);
8413 }
8414 if (nr_slots != (nr_slots & -nr_slots))
8415 {
8416 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8417 " is not power of 2 [in module %s]"),
8418 nr_slots, dwp_file->name);
8419 }
8420
8421 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8422 htab->nr_units = nr_units;
8423 htab->nr_slots = nr_slots;
8424 htab->hash_table = index_ptr;
8425 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8426 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8427
8428 return htab;
8429}
8430
8431/* Update SECTIONS with the data from SECTP.
8432
8433 This function is like the other "locate" section routines that are
8434 passed to bfd_map_over_sections, but in this context the sections to
8435 read comes from the DWP hash table, not the full ELF section table.
8436
8437 The result is non-zero for success, or zero if an error was found. */
8438
8439static int
8440locate_virtual_dwo_sections (asection *sectp,
8441 struct virtual_dwo_sections *sections)
8442{
8443 const struct dwop_section_names *names = &dwop_section_names;
8444
8445 if (section_is_p (sectp->name, &names->abbrev_dwo))
8446 {
8447 /* There can be only one. */
8448 if (sections->abbrev.asection != NULL)
8449 return 0;
8450 sections->abbrev.asection = sectp;
8451 sections->abbrev.size = bfd_get_section_size (sectp);
8452 }
8453 else if (section_is_p (sectp->name, &names->info_dwo)
8454 || section_is_p (sectp->name, &names->types_dwo))
8455 {
8456 /* There can be only one. */
8457 if (sections->info_or_types.asection != NULL)
8458 return 0;
8459 sections->info_or_types.asection = sectp;
8460 sections->info_or_types.size = bfd_get_section_size (sectp);
8461 }
8462 else if (section_is_p (sectp->name, &names->line_dwo))
8463 {
8464 /* There can be only one. */
8465 if (sections->line.asection != NULL)
8466 return 0;
8467 sections->line.asection = sectp;
8468 sections->line.size = bfd_get_section_size (sectp);
8469 }
8470 else if (section_is_p (sectp->name, &names->loc_dwo))
8471 {
8472 /* There can be only one. */
8473 if (sections->loc.asection != NULL)
8474 return 0;
8475 sections->loc.asection = sectp;
8476 sections->loc.size = bfd_get_section_size (sectp);
8477 }
8478 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8479 {
8480 /* There can be only one. */
8481 if (sections->macinfo.asection != NULL)
8482 return 0;
8483 sections->macinfo.asection = sectp;
8484 sections->macinfo.size = bfd_get_section_size (sectp);
8485 }
8486 else if (section_is_p (sectp->name, &names->macro_dwo))
8487 {
8488 /* There can be only one. */
8489 if (sections->macro.asection != NULL)
8490 return 0;
8491 sections->macro.asection = sectp;
8492 sections->macro.size = bfd_get_section_size (sectp);
8493 }
8494 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8495 {
8496 /* There can be only one. */
8497 if (sections->str_offsets.asection != NULL)
8498 return 0;
8499 sections->str_offsets.asection = sectp;
8500 sections->str_offsets.size = bfd_get_section_size (sectp);
8501 }
8502 else
8503 {
8504 /* No other kind of section is valid. */
8505 return 0;
8506 }
8507
8508 return 1;
8509}
8510
8511/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8512 HTAB is the hash table from the DWP file.
8513 SECTION_INDEX is the index of the DWO in HTAB. */
8514
8515static struct dwo_unit *
8516create_dwo_in_dwp (struct dwp_file *dwp_file,
8517 const struct dwp_hash_table *htab,
8518 uint32_t section_index,
8519 ULONGEST signature, int is_debug_types)
8520{
8521 struct objfile *objfile = dwarf2_per_objfile->objfile;
8522 bfd *dbfd = dwp_file->dbfd;
8523 const char *kind = is_debug_types ? "TU" : "CU";
8524 struct dwo_file *dwo_file;
8525 struct dwo_unit *dwo_unit;
8526 struct virtual_dwo_sections sections;
8527 void **dwo_file_slot;
8528 char *virtual_dwo_name;
8529 struct dwarf2_section_info *cutu;
8530 struct cleanup *cleanups;
8531 int i;
8532
8533 if (dwarf2_read_debug)
8534 {
8535 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8536 kind,
8537 section_index, phex (signature, sizeof (signature)),
8538 dwp_file->name);
8539 }
8540
8541 /* Fetch the sections of this DWO.
8542 Put a limit on the number of sections we look for so that bad data
8543 doesn't cause us to loop forever. */
8544
8545#define MAX_NR_DWO_SECTIONS \
8546 (1 /* .debug_info or .debug_types */ \
8547 + 1 /* .debug_abbrev */ \
8548 + 1 /* .debug_line */ \
8549 + 1 /* .debug_loc */ \
8550 + 1 /* .debug_str_offsets */ \
8551 + 1 /* .debug_macro */ \
8552 + 1 /* .debug_macinfo */ \
8553 + 1 /* trailing zero */)
8554
8555 memset (&sections, 0, sizeof (sections));
8556 cleanups = make_cleanup (null_cleanup, 0);
8557
8558 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8559 {
8560 asection *sectp;
8561 uint32_t section_nr =
8562 read_4_bytes (dbfd,
8563 htab->section_pool
8564 + (section_index + i) * sizeof (uint32_t));
8565
8566 if (section_nr == 0)
8567 break;
8568 if (section_nr >= dwp_file->num_sections)
8569 {
8570 error (_("Dwarf Error: bad DWP hash table, section number too large"
8571 " [in module %s]"),
8572 dwp_file->name);
8573 }
8574
8575 sectp = dwp_file->elf_sections[section_nr];
8576 if (! locate_virtual_dwo_sections (sectp, &sections))
8577 {
8578 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8579 " [in module %s]"),
8580 dwp_file->name);
8581 }
8582 }
8583
8584 if (i < 2
8585 || sections.info_or_types.asection == NULL
8586 || sections.abbrev.asection == NULL)
8587 {
8588 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8589 " [in module %s]"),
8590 dwp_file->name);
8591 }
8592 if (i == MAX_NR_DWO_SECTIONS)
8593 {
8594 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8595 " [in module %s]"),
8596 dwp_file->name);
8597 }
8598
8599 /* It's easier for the rest of the code if we fake a struct dwo_file and
8600 have dwo_unit "live" in that. At least for now.
8601
8602 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8603 However, for each CU + set of TUs that came from the same original DWO
8604 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8605 (fewer struct dwo_file objects to allocated). Remember that for really
8606 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8607
2792b94d
PM
8608 virtual_dwo_name =
8609 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8610 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8611 sections.line.asection ? sections.line.asection->id : 0,
8612 sections.loc.asection ? sections.loc.asection->id : 0,
8613 (sections.str_offsets.asection
8614 ? sections.str_offsets.asection->id
8615 : 0));
80626a55
DE
8616 make_cleanup (xfree, virtual_dwo_name);
8617 /* Can we use an existing virtual DWO file? */
8618 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8619 /* Create one if necessary. */
8620 if (*dwo_file_slot == NULL)
8621 {
8622 if (dwarf2_read_debug)
8623 {
8624 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8625 virtual_dwo_name);
8626 }
8627 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8628 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8629 virtual_dwo_name,
8630 strlen (virtual_dwo_name));
8631 dwo_file->sections.abbrev = sections.abbrev;
8632 dwo_file->sections.line = sections.line;
8633 dwo_file->sections.loc = sections.loc;
8634 dwo_file->sections.macinfo = sections.macinfo;
8635 dwo_file->sections.macro = sections.macro;
8636 dwo_file->sections.str_offsets = sections.str_offsets;
8637 /* The "str" section is global to the entire DWP file. */
8638 dwo_file->sections.str = dwp_file->sections.str;
8639 /* The info or types section is assigned later to dwo_unit,
8640 there's no need to record it in dwo_file.
8641 Also, we can't simply record type sections in dwo_file because
8642 we record a pointer into the vector in dwo_unit. As we collect more
8643 types we'll grow the vector and eventually have to reallocate space
8644 for it, invalidating all the pointers into the current copy. */
8645 *dwo_file_slot = dwo_file;
8646 }
8647 else
8648 {
8649 if (dwarf2_read_debug)
8650 {
8651 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8652 virtual_dwo_name);
8653 }
8654 dwo_file = *dwo_file_slot;
8655 }
8656 do_cleanups (cleanups);
8657
8658 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8659 dwo_unit->dwo_file = dwo_file;
8660 dwo_unit->signature = signature;
8661 dwo_unit->info_or_types_section =
8662 obstack_alloc (&objfile->objfile_obstack,
8663 sizeof (struct dwarf2_section_info));
8664 *dwo_unit->info_or_types_section = sections.info_or_types;
8665 /* offset, length, type_offset_in_tu are set later. */
8666
8667 return dwo_unit;
8668}
8669
8670/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8671
8672static struct dwo_unit *
8673lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8674 const struct dwp_hash_table *htab,
8675 ULONGEST signature, int is_debug_types)
8676{
8677 bfd *dbfd = dwp_file->dbfd;
8678 uint32_t mask = htab->nr_slots - 1;
8679 uint32_t hash = signature & mask;
8680 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8681 unsigned int i;
8682 void **slot;
8683 struct dwo_unit find_dwo_cu, *dwo_cu;
8684
8685 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8686 find_dwo_cu.signature = signature;
8687 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8688
8689 if (*slot != NULL)
8690 return *slot;
8691
8692 /* Use a for loop so that we don't loop forever on bad debug info. */
8693 for (i = 0; i < htab->nr_slots; ++i)
8694 {
8695 ULONGEST signature_in_table;
8696
8697 signature_in_table =
8698 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8699 if (signature_in_table == signature)
8700 {
8701 uint32_t section_index =
8702 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8703
8704 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8705 signature, is_debug_types);
8706 return *slot;
8707 }
8708 if (signature_in_table == 0)
8709 return NULL;
8710 hash = (hash + hash2) & mask;
8711 }
8712
8713 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8714 " [in module %s]"),
8715 dwp_file->name);
8716}
8717
8718/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8719 Open the file specified by FILE_NAME and hand it off to BFD for
8720 preliminary analysis. Return a newly initialized bfd *, which
8721 includes a canonicalized copy of FILE_NAME.
80626a55 8722 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8723 In case of trouble, return NULL.
8724 NOTE: This function is derived from symfile_bfd_open. */
8725
8726static bfd *
80626a55 8727try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8728{
8729 bfd *sym_bfd;
80626a55 8730 int desc, flags;
3019eac3 8731 char *absolute_name;
3019eac3 8732
80626a55
DE
8733 flags = OPF_TRY_CWD_FIRST;
8734 if (is_dwp)
8735 flags |= OPF_SEARCH_IN_PATH;
8736 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8737 O_RDONLY | O_BINARY, &absolute_name);
8738 if (desc < 0)
8739 return NULL;
8740
bb397797 8741 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8742 if (!sym_bfd)
8743 {
3019eac3
DE
8744 xfree (absolute_name);
8745 return NULL;
8746 }
a4453b7e 8747 xfree (absolute_name);
3019eac3
DE
8748 bfd_set_cacheable (sym_bfd, 1);
8749
8750 if (!bfd_check_format (sym_bfd, bfd_object))
8751 {
cbb099e8 8752 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8753 return NULL;
8754 }
8755
3019eac3
DE
8756 return sym_bfd;
8757}
8758
80626a55 8759/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8760 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8761 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8762 The result is the bfd handle of the file.
8763 If there is a problem finding or opening the file, return NULL.
8764 Upon success, the canonicalized path of the file is stored in the bfd,
8765 same as symfile_bfd_open. */
8766
8767static bfd *
80626a55 8768open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8769{
8770 bfd *abfd;
3019eac3 8771
80626a55
DE
8772 if (IS_ABSOLUTE_PATH (file_name))
8773 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8774
8775 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8776
8777 if (comp_dir != NULL)
8778 {
80626a55 8779 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8780
8781 /* NOTE: If comp_dir is a relative path, this will also try the
8782 search path, which seems useful. */
80626a55 8783 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8784 xfree (path_to_try);
8785 if (abfd != NULL)
8786 return abfd;
8787 }
8788
8789 /* That didn't work, try debug-file-directory, which, despite its name,
8790 is a list of paths. */
8791
8792 if (*debug_file_directory == '\0')
8793 return NULL;
8794
80626a55 8795 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8796}
8797
80626a55
DE
8798/* This function is mapped across the sections and remembers the offset and
8799 size of each of the DWO debugging sections we are interested in. */
8800
8801static void
8802dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8803{
8804 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8805 const struct dwop_section_names *names = &dwop_section_names;
8806
8807 if (section_is_p (sectp->name, &names->abbrev_dwo))
8808 {
8809 dwo_sections->abbrev.asection = sectp;
8810 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8811 }
8812 else if (section_is_p (sectp->name, &names->info_dwo))
8813 {
8814 dwo_sections->info.asection = sectp;
8815 dwo_sections->info.size = bfd_get_section_size (sectp);
8816 }
8817 else if (section_is_p (sectp->name, &names->line_dwo))
8818 {
8819 dwo_sections->line.asection = sectp;
8820 dwo_sections->line.size = bfd_get_section_size (sectp);
8821 }
8822 else if (section_is_p (sectp->name, &names->loc_dwo))
8823 {
8824 dwo_sections->loc.asection = sectp;
8825 dwo_sections->loc.size = bfd_get_section_size (sectp);
8826 }
8827 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8828 {
8829 dwo_sections->macinfo.asection = sectp;
8830 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8831 }
8832 else if (section_is_p (sectp->name, &names->macro_dwo))
8833 {
8834 dwo_sections->macro.asection = sectp;
8835 dwo_sections->macro.size = bfd_get_section_size (sectp);
8836 }
8837 else if (section_is_p (sectp->name, &names->str_dwo))
8838 {
8839 dwo_sections->str.asection = sectp;
8840 dwo_sections->str.size = bfd_get_section_size (sectp);
8841 }
8842 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8843 {
8844 dwo_sections->str_offsets.asection = sectp;
8845 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8846 }
8847 else if (section_is_p (sectp->name, &names->types_dwo))
8848 {
8849 struct dwarf2_section_info type_section;
8850
8851 memset (&type_section, 0, sizeof (type_section));
8852 type_section.asection = sectp;
8853 type_section.size = bfd_get_section_size (sectp);
8854 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8855 &type_section);
8856 }
8857}
8858
8859/* Initialize the use of the DWO file specified by DWO_NAME.
8860 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8861
8862static struct dwo_file *
80626a55 8863open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8864{
8865 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8866 struct dwo_file *dwo_file;
8867 bfd *dbfd;
3019eac3
DE
8868 struct cleanup *cleanups;
8869
80626a55
DE
8870 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8871 if (dbfd == NULL)
8872 {
8873 if (dwarf2_read_debug)
8874 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8875 return NULL;
8876 }
8877 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8878 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8879 dwo_name, strlen (dwo_name));
8880 dwo_file->dbfd = dbfd;
3019eac3
DE
8881
8882 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8883
80626a55 8884 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8885
80626a55 8886 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8887
8888 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8889 dwo_file->sections.types);
8890
8891 discard_cleanups (cleanups);
8892
80626a55
DE
8893 if (dwarf2_read_debug)
8894 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8895
3019eac3
DE
8896 return dwo_file;
8897}
8898
80626a55
DE
8899/* This function is mapped across the sections and remembers the offset and
8900 size of each of the DWP debugging sections we are interested in. */
3019eac3 8901
80626a55
DE
8902static void
8903dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8904{
80626a55
DE
8905 struct dwp_file *dwp_file = dwp_file_ptr;
8906 const struct dwop_section_names *names = &dwop_section_names;
8907 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8908
80626a55
DE
8909 /* Record the ELF section number for later lookup: this is what the
8910 .debug_cu_index,.debug_tu_index tables use. */
8911 gdb_assert (elf_section_nr < dwp_file->num_sections);
8912 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8913
80626a55
DE
8914 /* Look for specific sections that we need. */
8915 if (section_is_p (sectp->name, &names->str_dwo))
8916 {
8917 dwp_file->sections.str.asection = sectp;
8918 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8919 }
8920 else if (section_is_p (sectp->name, &names->cu_index))
8921 {
8922 dwp_file->sections.cu_index.asection = sectp;
8923 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8924 }
8925 else if (section_is_p (sectp->name, &names->tu_index))
8926 {
8927 dwp_file->sections.tu_index.asection = sectp;
8928 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8929 }
8930}
3019eac3 8931
80626a55 8932/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8933
80626a55
DE
8934static hashval_t
8935hash_dwp_loaded_cutus (const void *item)
8936{
8937 const struct dwo_unit *dwo_unit = item;
3019eac3 8938
80626a55
DE
8939 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8940 return dwo_unit->signature;
3019eac3
DE
8941}
8942
80626a55 8943/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 8944
80626a55
DE
8945static int
8946eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 8947{
80626a55
DE
8948 const struct dwo_unit *dua = a;
8949 const struct dwo_unit *dub = b;
3019eac3 8950
80626a55
DE
8951 return dua->signature == dub->signature;
8952}
3019eac3 8953
80626a55 8954/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 8955
80626a55
DE
8956static htab_t
8957allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8958{
8959 return htab_create_alloc_ex (3,
8960 hash_dwp_loaded_cutus,
8961 eq_dwp_loaded_cutus,
8962 NULL,
8963 &objfile->objfile_obstack,
8964 hashtab_obstack_allocate,
8965 dummy_obstack_deallocate);
8966}
3019eac3 8967
80626a55
DE
8968/* Initialize the use of the DWP file for the current objfile.
8969 By convention the name of the DWP file is ${objfile}.dwp.
8970 The result is NULL if it can't be found. */
a766d390 8971
80626a55
DE
8972static struct dwp_file *
8973open_and_init_dwp_file (const char *comp_dir)
8974{
8975 struct objfile *objfile = dwarf2_per_objfile->objfile;
8976 struct dwp_file *dwp_file;
8977 char *dwp_name;
8978 bfd *dbfd;
8979 struct cleanup *cleanups;
8980
2792b94d 8981 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
8982 cleanups = make_cleanup (xfree, dwp_name);
8983
8984 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8985 if (dbfd == NULL)
8986 {
8987 if (dwarf2_read_debug)
8988 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8989 do_cleanups (cleanups);
8990 return NULL;
3019eac3 8991 }
80626a55
DE
8992 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
8993 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
8994 dwp_name, strlen (dwp_name));
8995 dwp_file->dbfd = dbfd;
8996 do_cleanups (cleanups);
c906108c 8997
80626a55 8998 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 8999
80626a55
DE
9000 /* +1: section 0 is unused */
9001 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9002 dwp_file->elf_sections =
9003 OBSTACK_CALLOC (&objfile->objfile_obstack,
9004 dwp_file->num_sections, asection *);
9005
9006 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9007
9008 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9009
9010 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9011
9012 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9013
9014 discard_cleanups (cleanups);
9015
9016 if (dwarf2_read_debug)
9017 {
9018 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9019 fprintf_unfiltered (gdb_stdlog,
9020 " %u CUs, %u TUs\n",
9021 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9022 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9023 }
9024
9025 return dwp_file;
3019eac3 9026}
c906108c 9027
80626a55
DE
9028/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9029 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9030 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9031 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9032 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9033
9034 This is called, for example, when wanting to read a variable with a
9035 complex location. Therefore we don't want to do file i/o for every call.
9036 Therefore we don't want to look for a DWO file on every call.
9037 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9038 then we check if we've already seen DWO_NAME, and only THEN do we check
9039 for a DWO file.
9040
1c658ad5 9041 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9042 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9043
3019eac3 9044static struct dwo_unit *
80626a55
DE
9045lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9046 const char *dwo_name, const char *comp_dir,
9047 ULONGEST signature, int is_debug_types)
3019eac3
DE
9048{
9049 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9050 const char *kind = is_debug_types ? "TU" : "CU";
9051 void **dwo_file_slot;
3019eac3 9052 struct dwo_file *dwo_file;
80626a55 9053 struct dwp_file *dwp_file;
cb1df416 9054
80626a55 9055 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9056
80626a55
DE
9057 if (! dwarf2_per_objfile->dwp_checked)
9058 {
9059 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9060 dwarf2_per_objfile->dwp_checked = 1;
9061 }
9062 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9063
80626a55 9064 if (dwp_file != NULL)
cf2c3c16 9065 {
80626a55
DE
9066 const struct dwp_hash_table *dwp_htab =
9067 is_debug_types ? dwp_file->tus : dwp_file->cus;
9068
9069 if (dwp_htab != NULL)
9070 {
9071 struct dwo_unit *dwo_cutu =
9072 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9073
9074 if (dwo_cutu != NULL)
9075 {
9076 if (dwarf2_read_debug)
9077 {
9078 fprintf_unfiltered (gdb_stdlog,
9079 "Virtual DWO %s %s found: @%s\n",
9080 kind, hex_string (signature),
9081 host_address_to_string (dwo_cutu));
9082 }
9083 return dwo_cutu;
9084 }
9085 }
9086 }
9087
9088 /* Have we already seen DWO_NAME? */
9089
9090 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9091 if (*dwo_file_slot == NULL)
9092 {
9093 /* Read in the file and build a table of the DWOs it contains. */
9094 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9095 }
9096 /* NOTE: This will be NULL if unable to open the file. */
9097 dwo_file = *dwo_file_slot;
9098
9099 if (dwo_file != NULL)
9100 {
9101 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9102
9103 if (htab != NULL)
9104 {
9105 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9106
80626a55
DE
9107 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9108 find_dwo_cutu.signature = signature;
9109 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9110
80626a55
DE
9111 if (dwo_cutu != NULL)
9112 {
9113 if (dwarf2_read_debug)
9114 {
9115 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9116 kind, dwo_name, hex_string (signature),
9117 host_address_to_string (dwo_cutu));
9118 }
9119 return dwo_cutu;
9120 }
9121 }
2e276125 9122 }
9cdd5dbd 9123
80626a55
DE
9124 /* We didn't find it. This could mean a dwo_id mismatch, or
9125 someone deleted the DWO/DWP file, or the search path isn't set up
9126 correctly to find the file. */
9127
9128 if (dwarf2_read_debug)
9129 {
9130 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9131 kind, dwo_name, hex_string (signature));
9132 }
3019eac3
DE
9133
9134 complaint (&symfile_complaints,
80626a55 9135 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9136 " [in module %s]"),
80626a55 9137 this_unit->offset.sect_off, objfile->name);
3019eac3 9138 return NULL;
5fb290d7
DJ
9139}
9140
80626a55
DE
9141/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9142 See lookup_dwo_cutu_unit for details. */
9143
9144static struct dwo_unit *
9145lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9146 const char *dwo_name, const char *comp_dir,
9147 ULONGEST signature)
9148{
9149 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9150}
9151
9152/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9153 See lookup_dwo_cutu_unit for details. */
9154
9155static struct dwo_unit *
9156lookup_dwo_type_unit (struct signatured_type *this_tu,
9157 const char *dwo_name, const char *comp_dir)
9158{
9159 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9160}
9161
3019eac3
DE
9162/* Free all resources associated with DWO_FILE.
9163 Close the DWO file and munmap the sections.
9164 All memory should be on the objfile obstack. */
348e048f
DE
9165
9166static void
3019eac3 9167free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9168{
3019eac3
DE
9169 int ix;
9170 struct dwarf2_section_info *section;
348e048f 9171
80626a55
DE
9172 gdb_assert (dwo_file->dbfd != objfile->obfd);
9173 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9174
3019eac3
DE
9175 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9176}
348e048f 9177
3019eac3 9178/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9179
3019eac3
DE
9180static void
9181free_dwo_file_cleanup (void *arg)
9182{
9183 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9184 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9185
3019eac3
DE
9186 free_dwo_file (dwo_file, objfile);
9187}
348e048f 9188
3019eac3 9189/* Traversal function for free_dwo_files. */
2ab95328 9190
3019eac3
DE
9191static int
9192free_dwo_file_from_slot (void **slot, void *info)
9193{
9194 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9195 struct objfile *objfile = (struct objfile *) info;
348e048f 9196
3019eac3 9197 free_dwo_file (dwo_file, objfile);
348e048f 9198
3019eac3
DE
9199 return 1;
9200}
348e048f 9201
3019eac3 9202/* Free all resources associated with DWO_FILES. */
348e048f 9203
3019eac3
DE
9204static void
9205free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9206{
9207 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9208}
3019eac3
DE
9209\f
9210/* Read in various DIEs. */
348e048f 9211
d389af10
JK
9212/* qsort helper for inherit_abstract_dies. */
9213
9214static int
9215unsigned_int_compar (const void *ap, const void *bp)
9216{
9217 unsigned int a = *(unsigned int *) ap;
9218 unsigned int b = *(unsigned int *) bp;
9219
9220 return (a > b) - (b > a);
9221}
9222
9223/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9224 Inherit only the children of the DW_AT_abstract_origin DIE not being
9225 already referenced by DW_AT_abstract_origin from the children of the
9226 current DIE. */
d389af10
JK
9227
9228static void
9229inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9230{
9231 struct die_info *child_die;
9232 unsigned die_children_count;
9233 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9234 sect_offset *offsets;
9235 sect_offset *offsets_end, *offsetp;
d389af10
JK
9236 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9237 struct die_info *origin_die;
9238 /* Iterator of the ORIGIN_DIE children. */
9239 struct die_info *origin_child_die;
9240 struct cleanup *cleanups;
9241 struct attribute *attr;
cd02d79d
PA
9242 struct dwarf2_cu *origin_cu;
9243 struct pending **origin_previous_list_in_scope;
d389af10
JK
9244
9245 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9246 if (!attr)
9247 return;
9248
cd02d79d
PA
9249 /* Note that following die references may follow to a die in a
9250 different cu. */
9251
9252 origin_cu = cu;
9253 origin_die = follow_die_ref (die, attr, &origin_cu);
9254
9255 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9256 symbols in. */
9257 origin_previous_list_in_scope = origin_cu->list_in_scope;
9258 origin_cu->list_in_scope = cu->list_in_scope;
9259
edb3359d
DJ
9260 if (die->tag != origin_die->tag
9261 && !(die->tag == DW_TAG_inlined_subroutine
9262 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9263 complaint (&symfile_complaints,
9264 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9265 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9266
9267 child_die = die->child;
9268 die_children_count = 0;
9269 while (child_die && child_die->tag)
9270 {
9271 child_die = sibling_die (child_die);
9272 die_children_count++;
9273 }
9274 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9275 cleanups = make_cleanup (xfree, offsets);
9276
9277 offsets_end = offsets;
9278 child_die = die->child;
9279 while (child_die && child_die->tag)
9280 {
c38f313d
DJ
9281 /* For each CHILD_DIE, find the corresponding child of
9282 ORIGIN_DIE. If there is more than one layer of
9283 DW_AT_abstract_origin, follow them all; there shouldn't be,
9284 but GCC versions at least through 4.4 generate this (GCC PR
9285 40573). */
9286 struct die_info *child_origin_die = child_die;
cd02d79d 9287 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9288
c38f313d
DJ
9289 while (1)
9290 {
cd02d79d
PA
9291 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9292 child_origin_cu);
c38f313d
DJ
9293 if (attr == NULL)
9294 break;
cd02d79d
PA
9295 child_origin_die = follow_die_ref (child_origin_die, attr,
9296 &child_origin_cu);
c38f313d
DJ
9297 }
9298
d389af10
JK
9299 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9300 counterpart may exist. */
c38f313d 9301 if (child_origin_die != child_die)
d389af10 9302 {
edb3359d
DJ
9303 if (child_die->tag != child_origin_die->tag
9304 && !(child_die->tag == DW_TAG_inlined_subroutine
9305 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9306 complaint (&symfile_complaints,
9307 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9308 "different tags"), child_die->offset.sect_off,
9309 child_origin_die->offset.sect_off);
c38f313d
DJ
9310 if (child_origin_die->parent != origin_die)
9311 complaint (&symfile_complaints,
9312 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9313 "different parents"), child_die->offset.sect_off,
9314 child_origin_die->offset.sect_off);
c38f313d
DJ
9315 else
9316 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9317 }
9318 child_die = sibling_die (child_die);
9319 }
9320 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9321 unsigned_int_compar);
9322 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9323 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9324 complaint (&symfile_complaints,
9325 _("Multiple children of DIE 0x%x refer "
9326 "to DIE 0x%x as their abstract origin"),
b64f50a1 9327 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9328
9329 offsetp = offsets;
9330 origin_child_die = origin_die->child;
9331 while (origin_child_die && origin_child_die->tag)
9332 {
9333 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9334 while (offsetp < offsets_end
9335 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9336 offsetp++;
b64f50a1
JK
9337 if (offsetp >= offsets_end
9338 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9339 {
9340 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9341 process_die (origin_child_die, origin_cu);
d389af10
JK
9342 }
9343 origin_child_die = sibling_die (origin_child_die);
9344 }
cd02d79d 9345 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9346
9347 do_cleanups (cleanups);
9348}
9349
c906108c 9350static void
e7c27a73 9351read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9352{
e7c27a73 9353 struct objfile *objfile = cu->objfile;
52f0bd74 9354 struct context_stack *new;
c906108c
SS
9355 CORE_ADDR lowpc;
9356 CORE_ADDR highpc;
9357 struct die_info *child_die;
edb3359d 9358 struct attribute *attr, *call_line, *call_file;
c906108c 9359 char *name;
e142c38c 9360 CORE_ADDR baseaddr;
801e3a5b 9361 struct block *block;
edb3359d 9362 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9363 VEC (symbolp) *template_args = NULL;
9364 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9365
9366 if (inlined_func)
9367 {
9368 /* If we do not have call site information, we can't show the
9369 caller of this inlined function. That's too confusing, so
9370 only use the scope for local variables. */
9371 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9372 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9373 if (call_line == NULL || call_file == NULL)
9374 {
9375 read_lexical_block_scope (die, cu);
9376 return;
9377 }
9378 }
c906108c 9379
e142c38c
DJ
9380 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9381
94af9270 9382 name = dwarf2_name (die, cu);
c906108c 9383
e8d05480
JB
9384 /* Ignore functions with missing or empty names. These are actually
9385 illegal according to the DWARF standard. */
9386 if (name == NULL)
9387 {
9388 complaint (&symfile_complaints,
b64f50a1
JK
9389 _("missing name for subprogram DIE at %d"),
9390 die->offset.sect_off);
e8d05480
JB
9391 return;
9392 }
9393
9394 /* Ignore functions with missing or invalid low and high pc attributes. */
9395 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9396 {
ae4d0c03
PM
9397 attr = dwarf2_attr (die, DW_AT_external, cu);
9398 if (!attr || !DW_UNSND (attr))
9399 complaint (&symfile_complaints,
3e43a32a
MS
9400 _("cannot get low and high bounds "
9401 "for subprogram DIE at %d"),
b64f50a1 9402 die->offset.sect_off);
e8d05480
JB
9403 return;
9404 }
c906108c
SS
9405
9406 lowpc += baseaddr;
9407 highpc += baseaddr;
9408
34eaf542
TT
9409 /* If we have any template arguments, then we must allocate a
9410 different sort of symbol. */
9411 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9412 {
9413 if (child_die->tag == DW_TAG_template_type_param
9414 || child_die->tag == DW_TAG_template_value_param)
9415 {
9416 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9417 struct template_symbol);
9418 templ_func->base.is_cplus_template_function = 1;
9419 break;
9420 }
9421 }
9422
c906108c 9423 new = push_context (0, lowpc);
34eaf542
TT
9424 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9425 (struct symbol *) templ_func);
4c2df51b 9426
4cecd739
DJ
9427 /* If there is a location expression for DW_AT_frame_base, record
9428 it. */
e142c38c 9429 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9430 if (attr)
c034e007
AC
9431 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9432 expression is being recorded directly in the function's symbol
9433 and not in a separate frame-base object. I guess this hack is
9434 to avoid adding some sort of frame-base adjunct/annex to the
9435 function's symbol :-(. The problem with doing this is that it
9436 results in a function symbol with a location expression that
9437 has nothing to do with the location of the function, ouch! The
9438 relationship should be: a function's symbol has-a frame base; a
9439 frame-base has-a location expression. */
e7c27a73 9440 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9441
e142c38c 9442 cu->list_in_scope = &local_symbols;
c906108c 9443
639d11d3 9444 if (die->child != NULL)
c906108c 9445 {
639d11d3 9446 child_die = die->child;
c906108c
SS
9447 while (child_die && child_die->tag)
9448 {
34eaf542
TT
9449 if (child_die->tag == DW_TAG_template_type_param
9450 || child_die->tag == DW_TAG_template_value_param)
9451 {
9452 struct symbol *arg = new_symbol (child_die, NULL, cu);
9453
f1078f66
DJ
9454 if (arg != NULL)
9455 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9456 }
9457 else
9458 process_die (child_die, cu);
c906108c
SS
9459 child_die = sibling_die (child_die);
9460 }
9461 }
9462
d389af10
JK
9463 inherit_abstract_dies (die, cu);
9464
4a811a97
UW
9465 /* If we have a DW_AT_specification, we might need to import using
9466 directives from the context of the specification DIE. See the
9467 comment in determine_prefix. */
9468 if (cu->language == language_cplus
9469 && dwarf2_attr (die, DW_AT_specification, cu))
9470 {
9471 struct dwarf2_cu *spec_cu = cu;
9472 struct die_info *spec_die = die_specification (die, &spec_cu);
9473
9474 while (spec_die)
9475 {
9476 child_die = spec_die->child;
9477 while (child_die && child_die->tag)
9478 {
9479 if (child_die->tag == DW_TAG_imported_module)
9480 process_die (child_die, spec_cu);
9481 child_die = sibling_die (child_die);
9482 }
9483
9484 /* In some cases, GCC generates specification DIEs that
9485 themselves contain DW_AT_specification attributes. */
9486 spec_die = die_specification (spec_die, &spec_cu);
9487 }
9488 }
9489
c906108c
SS
9490 new = pop_context ();
9491 /* Make a block for the local symbols within. */
801e3a5b
JB
9492 block = finish_block (new->name, &local_symbols, new->old_blocks,
9493 lowpc, highpc, objfile);
9494
df8a16a1 9495 /* For C++, set the block's scope. */
f55ee35c 9496 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9497 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9498 determine_prefix (die, cu),
df8a16a1
DJ
9499 processing_has_namespace_info);
9500
801e3a5b
JB
9501 /* If we have address ranges, record them. */
9502 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9503
34eaf542
TT
9504 /* Attach template arguments to function. */
9505 if (! VEC_empty (symbolp, template_args))
9506 {
9507 gdb_assert (templ_func != NULL);
9508
9509 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9510 templ_func->template_arguments
9511 = obstack_alloc (&objfile->objfile_obstack,
9512 (templ_func->n_template_arguments
9513 * sizeof (struct symbol *)));
9514 memcpy (templ_func->template_arguments,
9515 VEC_address (symbolp, template_args),
9516 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9517 VEC_free (symbolp, template_args);
9518 }
9519
208d8187
JB
9520 /* In C++, we can have functions nested inside functions (e.g., when
9521 a function declares a class that has methods). This means that
9522 when we finish processing a function scope, we may need to go
9523 back to building a containing block's symbol lists. */
9524 local_symbols = new->locals;
27aa8d6a 9525 using_directives = new->using_directives;
208d8187 9526
921e78cf
JB
9527 /* If we've finished processing a top-level function, subsequent
9528 symbols go in the file symbol list. */
9529 if (outermost_context_p ())
e142c38c 9530 cu->list_in_scope = &file_symbols;
c906108c
SS
9531}
9532
9533/* Process all the DIES contained within a lexical block scope. Start
9534 a new scope, process the dies, and then close the scope. */
9535
9536static void
e7c27a73 9537read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9538{
e7c27a73 9539 struct objfile *objfile = cu->objfile;
52f0bd74 9540 struct context_stack *new;
c906108c
SS
9541 CORE_ADDR lowpc, highpc;
9542 struct die_info *child_die;
e142c38c
DJ
9543 CORE_ADDR baseaddr;
9544
9545 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9546
9547 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9548 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9549 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9550 be nasty. Might be easier to properly extend generic blocks to
af34e669 9551 describe ranges. */
d85a05f0 9552 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9553 return;
9554 lowpc += baseaddr;
9555 highpc += baseaddr;
9556
9557 push_context (0, lowpc);
639d11d3 9558 if (die->child != NULL)
c906108c 9559 {
639d11d3 9560 child_die = die->child;
c906108c
SS
9561 while (child_die && child_die->tag)
9562 {
e7c27a73 9563 process_die (child_die, cu);
c906108c
SS
9564 child_die = sibling_die (child_die);
9565 }
9566 }
9567 new = pop_context ();
9568
8540c487 9569 if (local_symbols != NULL || using_directives != NULL)
c906108c 9570 {
801e3a5b
JB
9571 struct block *block
9572 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9573 highpc, objfile);
9574
9575 /* Note that recording ranges after traversing children, as we
9576 do here, means that recording a parent's ranges entails
9577 walking across all its children's ranges as they appear in
9578 the address map, which is quadratic behavior.
9579
9580 It would be nicer to record the parent's ranges before
9581 traversing its children, simply overriding whatever you find
9582 there. But since we don't even decide whether to create a
9583 block until after we've traversed its children, that's hard
9584 to do. */
9585 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9586 }
9587 local_symbols = new->locals;
27aa8d6a 9588 using_directives = new->using_directives;
c906108c
SS
9589}
9590
96408a79
SA
9591/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9592
9593static void
9594read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9595{
9596 struct objfile *objfile = cu->objfile;
9597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9598 CORE_ADDR pc, baseaddr;
9599 struct attribute *attr;
9600 struct call_site *call_site, call_site_local;
9601 void **slot;
9602 int nparams;
9603 struct die_info *child_die;
9604
9605 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9606
9607 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9608 if (!attr)
9609 {
9610 complaint (&symfile_complaints,
9611 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9612 "DIE 0x%x [in module %s]"),
b64f50a1 9613 die->offset.sect_off, objfile->name);
96408a79
SA
9614 return;
9615 }
9616 pc = DW_ADDR (attr) + baseaddr;
9617
9618 if (cu->call_site_htab == NULL)
9619 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9620 NULL, &objfile->objfile_obstack,
9621 hashtab_obstack_allocate, NULL);
9622 call_site_local.pc = pc;
9623 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9624 if (*slot != NULL)
9625 {
9626 complaint (&symfile_complaints,
9627 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9628 "DIE 0x%x [in module %s]"),
b64f50a1 9629 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9630 return;
9631 }
9632
9633 /* Count parameters at the caller. */
9634
9635 nparams = 0;
9636 for (child_die = die->child; child_die && child_die->tag;
9637 child_die = sibling_die (child_die))
9638 {
9639 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9640 {
9641 complaint (&symfile_complaints,
9642 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9643 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9644 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9645 continue;
9646 }
9647
9648 nparams++;
9649 }
9650
9651 call_site = obstack_alloc (&objfile->objfile_obstack,
9652 (sizeof (*call_site)
9653 + (sizeof (*call_site->parameter)
9654 * (nparams - 1))));
9655 *slot = call_site;
9656 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9657 call_site->pc = pc;
9658
9659 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9660 {
9661 struct die_info *func_die;
9662
9663 /* Skip also over DW_TAG_inlined_subroutine. */
9664 for (func_die = die->parent;
9665 func_die && func_die->tag != DW_TAG_subprogram
9666 && func_die->tag != DW_TAG_subroutine_type;
9667 func_die = func_die->parent);
9668
9669 /* DW_AT_GNU_all_call_sites is a superset
9670 of DW_AT_GNU_all_tail_call_sites. */
9671 if (func_die
9672 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9673 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9674 {
9675 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9676 not complete. But keep CALL_SITE for look ups via call_site_htab,
9677 both the initial caller containing the real return address PC and
9678 the final callee containing the current PC of a chain of tail
9679 calls do not need to have the tail call list complete. But any
9680 function candidate for a virtual tail call frame searched via
9681 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9682 determined unambiguously. */
9683 }
9684 else
9685 {
9686 struct type *func_type = NULL;
9687
9688 if (func_die)
9689 func_type = get_die_type (func_die, cu);
9690 if (func_type != NULL)
9691 {
9692 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9693
9694 /* Enlist this call site to the function. */
9695 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9696 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9697 }
9698 else
9699 complaint (&symfile_complaints,
9700 _("Cannot find function owning DW_TAG_GNU_call_site "
9701 "DIE 0x%x [in module %s]"),
b64f50a1 9702 die->offset.sect_off, objfile->name);
96408a79
SA
9703 }
9704 }
9705
9706 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9707 if (attr == NULL)
9708 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9709 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9710 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9711 /* Keep NULL DWARF_BLOCK. */;
9712 else if (attr_form_is_block (attr))
9713 {
9714 struct dwarf2_locexpr_baton *dlbaton;
9715
9716 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9717 dlbaton->data = DW_BLOCK (attr)->data;
9718 dlbaton->size = DW_BLOCK (attr)->size;
9719 dlbaton->per_cu = cu->per_cu;
9720
9721 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9722 }
9723 else if (is_ref_attr (attr))
9724 {
96408a79
SA
9725 struct dwarf2_cu *target_cu = cu;
9726 struct die_info *target_die;
9727
9728 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9729 gdb_assert (target_cu->objfile == objfile);
9730 if (die_is_declaration (target_die, target_cu))
9731 {
9732 const char *target_physname;
9733
9734 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9735 if (target_physname == NULL)
9736 complaint (&symfile_complaints,
9737 _("DW_AT_GNU_call_site_target target DIE has invalid "
9738 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9739 die->offset.sect_off, objfile->name);
96408a79
SA
9740 else
9741 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9742 }
9743 else
9744 {
9745 CORE_ADDR lowpc;
9746
9747 /* DW_AT_entry_pc should be preferred. */
9748 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9749 complaint (&symfile_complaints,
9750 _("DW_AT_GNU_call_site_target target DIE has invalid "
9751 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9752 die->offset.sect_off, objfile->name);
96408a79
SA
9753 else
9754 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9755 }
9756 }
9757 else
9758 complaint (&symfile_complaints,
9759 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9760 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9761 die->offset.sect_off, objfile->name);
96408a79
SA
9762
9763 call_site->per_cu = cu->per_cu;
9764
9765 for (child_die = die->child;
9766 child_die && child_die->tag;
9767 child_die = sibling_die (child_die))
9768 {
96408a79 9769 struct call_site_parameter *parameter;
1788b2d3 9770 struct attribute *loc, *origin;
96408a79
SA
9771
9772 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9773 {
9774 /* Already printed the complaint above. */
9775 continue;
9776 }
9777
9778 gdb_assert (call_site->parameter_count < nparams);
9779 parameter = &call_site->parameter[call_site->parameter_count];
9780
1788b2d3
JK
9781 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9782 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9783 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9784
24c5c679 9785 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9786 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9787 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9788 {
9789 sect_offset offset;
9790
9791 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9792 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9793 if (!offset_in_cu_p (&cu->header, offset))
9794 {
9795 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9796 binding can be done only inside one CU. Such referenced DIE
9797 therefore cannot be even moved to DW_TAG_partial_unit. */
9798 complaint (&symfile_complaints,
9799 _("DW_AT_abstract_origin offset is not in CU for "
9800 "DW_TAG_GNU_call_site child DIE 0x%x "
9801 "[in module %s]"),
9802 child_die->offset.sect_off, objfile->name);
9803 continue;
9804 }
1788b2d3
JK
9805 parameter->u.param_offset.cu_off = (offset.sect_off
9806 - cu->header.offset.sect_off);
9807 }
9808 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9809 {
9810 complaint (&symfile_complaints,
9811 _("No DW_FORM_block* DW_AT_location for "
9812 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9813 child_die->offset.sect_off, objfile->name);
96408a79
SA
9814 continue;
9815 }
24c5c679 9816 else
96408a79 9817 {
24c5c679
JK
9818 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9819 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9820 if (parameter->u.dwarf_reg != -1)
9821 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9822 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9823 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9824 &parameter->u.fb_offset))
9825 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9826 else
9827 {
9828 complaint (&symfile_complaints,
9829 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9830 "for DW_FORM_block* DW_AT_location is supported for "
9831 "DW_TAG_GNU_call_site child DIE 0x%x "
9832 "[in module %s]"),
9833 child_die->offset.sect_off, objfile->name);
9834 continue;
9835 }
96408a79
SA
9836 }
9837
9838 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9839 if (!attr_form_is_block (attr))
9840 {
9841 complaint (&symfile_complaints,
9842 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9843 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9844 child_die->offset.sect_off, objfile->name);
96408a79
SA
9845 continue;
9846 }
9847 parameter->value = DW_BLOCK (attr)->data;
9848 parameter->value_size = DW_BLOCK (attr)->size;
9849
9850 /* Parameters are not pre-cleared by memset above. */
9851 parameter->data_value = NULL;
9852 parameter->data_value_size = 0;
9853 call_site->parameter_count++;
9854
9855 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9856 if (attr)
9857 {
9858 if (!attr_form_is_block (attr))
9859 complaint (&symfile_complaints,
9860 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9861 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9862 child_die->offset.sect_off, objfile->name);
96408a79
SA
9863 else
9864 {
9865 parameter->data_value = DW_BLOCK (attr)->data;
9866 parameter->data_value_size = DW_BLOCK (attr)->size;
9867 }
9868 }
9869 }
9870}
9871
43039443 9872/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9873 Return 1 if the attributes are present and valid, otherwise, return 0.
9874 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9875
9876static int
9877dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9878 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9879 struct partial_symtab *ranges_pst)
43039443
JK
9880{
9881 struct objfile *objfile = cu->objfile;
9882 struct comp_unit_head *cu_header = &cu->header;
9883 bfd *obfd = objfile->obfd;
9884 unsigned int addr_size = cu_header->addr_size;
9885 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9886 /* Base address selection entry. */
9887 CORE_ADDR base;
9888 int found_base;
9889 unsigned int dummy;
9890 gdb_byte *buffer;
9891 CORE_ADDR marker;
9892 int low_set;
9893 CORE_ADDR low = 0;
9894 CORE_ADDR high = 0;
ff013f42 9895 CORE_ADDR baseaddr;
43039443 9896
d00adf39
DE
9897 found_base = cu->base_known;
9898 base = cu->base_address;
43039443 9899
be391dca 9900 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9901 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9902 {
9903 complaint (&symfile_complaints,
9904 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9905 offset);
9906 return 0;
9907 }
dce234bc 9908 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9909
9910 /* Read in the largest possible address. */
9911 marker = read_address (obfd, buffer, cu, &dummy);
9912 if ((marker & mask) == mask)
9913 {
9914 /* If we found the largest possible address, then
9915 read the base address. */
9916 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9917 buffer += 2 * addr_size;
9918 offset += 2 * addr_size;
9919 found_base = 1;
9920 }
9921
9922 low_set = 0;
9923
e7030f15 9924 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9925
43039443
JK
9926 while (1)
9927 {
9928 CORE_ADDR range_beginning, range_end;
9929
9930 range_beginning = read_address (obfd, buffer, cu, &dummy);
9931 buffer += addr_size;
9932 range_end = read_address (obfd, buffer, cu, &dummy);
9933 buffer += addr_size;
9934 offset += 2 * addr_size;
9935
9936 /* An end of list marker is a pair of zero addresses. */
9937 if (range_beginning == 0 && range_end == 0)
9938 /* Found the end of list entry. */
9939 break;
9940
9941 /* Each base address selection entry is a pair of 2 values.
9942 The first is the largest possible address, the second is
9943 the base address. Check for a base address here. */
9944 if ((range_beginning & mask) == mask)
9945 {
9946 /* If we found the largest possible address, then
9947 read the base address. */
9948 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9949 found_base = 1;
9950 continue;
9951 }
9952
9953 if (!found_base)
9954 {
9955 /* We have no valid base address for the ranges
9956 data. */
9957 complaint (&symfile_complaints,
9958 _("Invalid .debug_ranges data (no base address)"));
9959 return 0;
9960 }
9961
9277c30c
UW
9962 if (range_beginning > range_end)
9963 {
9964 /* Inverted range entries are invalid. */
9965 complaint (&symfile_complaints,
9966 _("Invalid .debug_ranges data (inverted range)"));
9967 return 0;
9968 }
9969
9970 /* Empty range entries have no effect. */
9971 if (range_beginning == range_end)
9972 continue;
9973
43039443
JK
9974 range_beginning += base;
9975 range_end += base;
9976
01093045
DE
9977 /* A not-uncommon case of bad debug info.
9978 Don't pollute the addrmap with bad data. */
9979 if (range_beginning + baseaddr == 0
9980 && !dwarf2_per_objfile->has_section_at_zero)
9981 {
9982 complaint (&symfile_complaints,
9983 _(".debug_ranges entry has start address of zero"
9984 " [in module %s]"), objfile->name);
9985 continue;
9986 }
9987
9277c30c 9988 if (ranges_pst != NULL)
ff013f42 9989 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
9990 range_beginning + baseaddr,
9991 range_end - 1 + baseaddr,
ff013f42
JK
9992 ranges_pst);
9993
43039443
JK
9994 /* FIXME: This is recording everything as a low-high
9995 segment of consecutive addresses. We should have a
9996 data structure for discontiguous block ranges
9997 instead. */
9998 if (! low_set)
9999 {
10000 low = range_beginning;
10001 high = range_end;
10002 low_set = 1;
10003 }
10004 else
10005 {
10006 if (range_beginning < low)
10007 low = range_beginning;
10008 if (range_end > high)
10009 high = range_end;
10010 }
10011 }
10012
10013 if (! low_set)
10014 /* If the first entry is an end-of-list marker, the range
10015 describes an empty scope, i.e. no instructions. */
10016 return 0;
10017
10018 if (low_return)
10019 *low_return = low;
10020 if (high_return)
10021 *high_return = high;
10022 return 1;
10023}
10024
af34e669
DJ
10025/* Get low and high pc attributes from a die. Return 1 if the attributes
10026 are present and valid, otherwise, return 0. Return -1 if the range is
10027 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10028
c906108c 10029static int
af34e669 10030dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10031 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10032 struct partial_symtab *pst)
c906108c
SS
10033{
10034 struct attribute *attr;
91da1414 10035 struct attribute *attr_high;
af34e669
DJ
10036 CORE_ADDR low = 0;
10037 CORE_ADDR high = 0;
10038 int ret = 0;
c906108c 10039
91da1414
MW
10040 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10041 if (attr_high)
af34e669 10042 {
e142c38c 10043 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10044 if (attr)
91da1414
MW
10045 {
10046 low = DW_ADDR (attr);
3019eac3
DE
10047 if (attr_high->form == DW_FORM_addr
10048 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10049 high = DW_ADDR (attr_high);
10050 else
10051 high = low + DW_UNSND (attr_high);
10052 }
af34e669
DJ
10053 else
10054 /* Found high w/o low attribute. */
10055 return 0;
10056
10057 /* Found consecutive range of addresses. */
10058 ret = 1;
10059 }
c906108c 10060 else
af34e669 10061 {
e142c38c 10062 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10063 if (attr != NULL)
10064 {
ab435259
DE
10065 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10066 We take advantage of the fact that DW_AT_ranges does not appear
10067 in DW_TAG_compile_unit of DWO files. */
10068 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10069 unsigned int ranges_offset = (DW_UNSND (attr)
10070 + (need_ranges_base
10071 ? cu->ranges_base
10072 : 0));
2e3cf129 10073
af34e669 10074 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10075 .debug_ranges section. */
2e3cf129 10076 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10077 return 0;
43039443 10078 /* Found discontinuous range of addresses. */
af34e669
DJ
10079 ret = -1;
10080 }
10081 }
c906108c 10082
9373cf26
JK
10083 /* read_partial_die has also the strict LOW < HIGH requirement. */
10084 if (high <= low)
c906108c
SS
10085 return 0;
10086
10087 /* When using the GNU linker, .gnu.linkonce. sections are used to
10088 eliminate duplicate copies of functions and vtables and such.
10089 The linker will arbitrarily choose one and discard the others.
10090 The AT_*_pc values for such functions refer to local labels in
10091 these sections. If the section from that file was discarded, the
10092 labels are not in the output, so the relocs get a value of 0.
10093 If this is a discarded function, mark the pc bounds as invalid,
10094 so that GDB will ignore it. */
72dca2f5 10095 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10096 return 0;
10097
10098 *lowpc = low;
96408a79
SA
10099 if (highpc)
10100 *highpc = high;
af34e669 10101 return ret;
c906108c
SS
10102}
10103
b084d499
JB
10104/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10105 its low and high PC addresses. Do nothing if these addresses could not
10106 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10107 and HIGHPC to the high address if greater than HIGHPC. */
10108
10109static void
10110dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10111 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10112 struct dwarf2_cu *cu)
10113{
10114 CORE_ADDR low, high;
10115 struct die_info *child = die->child;
10116
d85a05f0 10117 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10118 {
10119 *lowpc = min (*lowpc, low);
10120 *highpc = max (*highpc, high);
10121 }
10122
10123 /* If the language does not allow nested subprograms (either inside
10124 subprograms or lexical blocks), we're done. */
10125 if (cu->language != language_ada)
10126 return;
6e70227d 10127
b084d499
JB
10128 /* Check all the children of the given DIE. If it contains nested
10129 subprograms, then check their pc bounds. Likewise, we need to
10130 check lexical blocks as well, as they may also contain subprogram
10131 definitions. */
10132 while (child && child->tag)
10133 {
10134 if (child->tag == DW_TAG_subprogram
10135 || child->tag == DW_TAG_lexical_block)
10136 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10137 child = sibling_die (child);
10138 }
10139}
10140
fae299cd
DC
10141/* Get the low and high pc's represented by the scope DIE, and store
10142 them in *LOWPC and *HIGHPC. If the correct values can't be
10143 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10144
10145static void
10146get_scope_pc_bounds (struct die_info *die,
10147 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10148 struct dwarf2_cu *cu)
10149{
10150 CORE_ADDR best_low = (CORE_ADDR) -1;
10151 CORE_ADDR best_high = (CORE_ADDR) 0;
10152 CORE_ADDR current_low, current_high;
10153
d85a05f0 10154 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10155 {
10156 best_low = current_low;
10157 best_high = current_high;
10158 }
10159 else
10160 {
10161 struct die_info *child = die->child;
10162
10163 while (child && child->tag)
10164 {
10165 switch (child->tag) {
10166 case DW_TAG_subprogram:
b084d499 10167 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10168 break;
10169 case DW_TAG_namespace:
f55ee35c 10170 case DW_TAG_module:
fae299cd
DC
10171 /* FIXME: carlton/2004-01-16: Should we do this for
10172 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10173 that current GCC's always emit the DIEs corresponding
10174 to definitions of methods of classes as children of a
10175 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10176 the DIEs giving the declarations, which could be
10177 anywhere). But I don't see any reason why the
10178 standards says that they have to be there. */
10179 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10180
10181 if (current_low != ((CORE_ADDR) -1))
10182 {
10183 best_low = min (best_low, current_low);
10184 best_high = max (best_high, current_high);
10185 }
10186 break;
10187 default:
0963b4bd 10188 /* Ignore. */
fae299cd
DC
10189 break;
10190 }
10191
10192 child = sibling_die (child);
10193 }
10194 }
10195
10196 *lowpc = best_low;
10197 *highpc = best_high;
10198}
10199
801e3a5b
JB
10200/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10201 in DIE. */
380bca97 10202
801e3a5b
JB
10203static void
10204dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10205 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10206{
bb5ed363 10207 struct objfile *objfile = cu->objfile;
801e3a5b 10208 struct attribute *attr;
91da1414 10209 struct attribute *attr_high;
801e3a5b 10210
91da1414
MW
10211 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10212 if (attr_high)
801e3a5b 10213 {
801e3a5b
JB
10214 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10215 if (attr)
10216 {
10217 CORE_ADDR low = DW_ADDR (attr);
91da1414 10218 CORE_ADDR high;
3019eac3
DE
10219 if (attr_high->form == DW_FORM_addr
10220 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10221 high = DW_ADDR (attr_high);
10222 else
10223 high = low + DW_UNSND (attr_high);
9a619af0 10224
801e3a5b
JB
10225 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10226 }
10227 }
10228
10229 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10230 if (attr)
10231 {
bb5ed363 10232 bfd *obfd = objfile->obfd;
ab435259
DE
10233 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10234 We take advantage of the fact that DW_AT_ranges does not appear
10235 in DW_TAG_compile_unit of DWO files. */
10236 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10237
10238 /* The value of the DW_AT_ranges attribute is the offset of the
10239 address range list in the .debug_ranges section. */
ab435259
DE
10240 unsigned long offset = (DW_UNSND (attr)
10241 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10242 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10243
10244 /* For some target architectures, but not others, the
10245 read_address function sign-extends the addresses it returns.
10246 To recognize base address selection entries, we need a
10247 mask. */
10248 unsigned int addr_size = cu->header.addr_size;
10249 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10250
10251 /* The base address, to which the next pair is relative. Note
10252 that this 'base' is a DWARF concept: most entries in a range
10253 list are relative, to reduce the number of relocs against the
10254 debugging information. This is separate from this function's
10255 'baseaddr' argument, which GDB uses to relocate debugging
10256 information from a shared library based on the address at
10257 which the library was loaded. */
d00adf39
DE
10258 CORE_ADDR base = cu->base_address;
10259 int base_known = cu->base_known;
801e3a5b 10260
be391dca 10261 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10262 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10263 {
10264 complaint (&symfile_complaints,
10265 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10266 offset);
10267 return;
10268 }
10269
10270 for (;;)
10271 {
10272 unsigned int bytes_read;
10273 CORE_ADDR start, end;
10274
10275 start = read_address (obfd, buffer, cu, &bytes_read);
10276 buffer += bytes_read;
10277 end = read_address (obfd, buffer, cu, &bytes_read);
10278 buffer += bytes_read;
10279
10280 /* Did we find the end of the range list? */
10281 if (start == 0 && end == 0)
10282 break;
10283
10284 /* Did we find a base address selection entry? */
10285 else if ((start & base_select_mask) == base_select_mask)
10286 {
10287 base = end;
10288 base_known = 1;
10289 }
10290
10291 /* We found an ordinary address range. */
10292 else
10293 {
10294 if (!base_known)
10295 {
10296 complaint (&symfile_complaints,
3e43a32a
MS
10297 _("Invalid .debug_ranges data "
10298 "(no base address)"));
801e3a5b
JB
10299 return;
10300 }
10301
9277c30c
UW
10302 if (start > end)
10303 {
10304 /* Inverted range entries are invalid. */
10305 complaint (&symfile_complaints,
10306 _("Invalid .debug_ranges data "
10307 "(inverted range)"));
10308 return;
10309 }
10310
10311 /* Empty range entries have no effect. */
10312 if (start == end)
10313 continue;
10314
01093045
DE
10315 start += base + baseaddr;
10316 end += base + baseaddr;
10317
10318 /* A not-uncommon case of bad debug info.
10319 Don't pollute the addrmap with bad data. */
10320 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10321 {
10322 complaint (&symfile_complaints,
10323 _(".debug_ranges entry has start address of zero"
10324 " [in module %s]"), objfile->name);
10325 continue;
10326 }
10327
10328 record_block_range (block, start, end - 1);
801e3a5b
JB
10329 }
10330 }
10331 }
10332}
10333
685b1105
JK
10334/* Check whether the producer field indicates either of GCC < 4.6, or the
10335 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10336
685b1105
JK
10337static void
10338check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10339{
10340 const char *cs;
10341 int major, minor, release;
10342
10343 if (cu->producer == NULL)
10344 {
10345 /* For unknown compilers expect their behavior is DWARF version
10346 compliant.
10347
10348 GCC started to support .debug_types sections by -gdwarf-4 since
10349 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10350 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10351 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10352 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10353 }
685b1105 10354 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10355 {
685b1105
JK
10356 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10357
ba919b58
TT
10358 cs = &cu->producer[strlen ("GNU ")];
10359 while (*cs && !isdigit (*cs))
10360 cs++;
10361 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10362 {
10363 /* Not recognized as GCC. */
10364 }
10365 else
1b80a9fa
JK
10366 {
10367 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10368 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10369 }
685b1105
JK
10370 }
10371 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10372 cu->producer_is_icc = 1;
10373 else
10374 {
10375 /* For other non-GCC compilers, expect their behavior is DWARF version
10376 compliant. */
60d5a603
JK
10377 }
10378
ba919b58 10379 cu->checked_producer = 1;
685b1105 10380}
ba919b58 10381
685b1105
JK
10382/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10383 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10384 during 4.6.0 experimental. */
10385
10386static int
10387producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10388{
10389 if (!cu->checked_producer)
10390 check_producer (cu);
10391
10392 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10393}
10394
10395/* Return the default accessibility type if it is not overriden by
10396 DW_AT_accessibility. */
10397
10398static enum dwarf_access_attribute
10399dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10400{
10401 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10402 {
10403 /* The default DWARF 2 accessibility for members is public, the default
10404 accessibility for inheritance is private. */
10405
10406 if (die->tag != DW_TAG_inheritance)
10407 return DW_ACCESS_public;
10408 else
10409 return DW_ACCESS_private;
10410 }
10411 else
10412 {
10413 /* DWARF 3+ defines the default accessibility a different way. The same
10414 rules apply now for DW_TAG_inheritance as for the members and it only
10415 depends on the container kind. */
10416
10417 if (die->parent->tag == DW_TAG_class_type)
10418 return DW_ACCESS_private;
10419 else
10420 return DW_ACCESS_public;
10421 }
10422}
10423
74ac6d43
TT
10424/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10425 offset. If the attribute was not found return 0, otherwise return
10426 1. If it was found but could not properly be handled, set *OFFSET
10427 to 0. */
10428
10429static int
10430handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10431 LONGEST *offset)
10432{
10433 struct attribute *attr;
10434
10435 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10436 if (attr != NULL)
10437 {
10438 *offset = 0;
10439
10440 /* Note that we do not check for a section offset first here.
10441 This is because DW_AT_data_member_location is new in DWARF 4,
10442 so if we see it, we can assume that a constant form is really
10443 a constant and not a section offset. */
10444 if (attr_form_is_constant (attr))
10445 *offset = dwarf2_get_attr_constant_value (attr, 0);
10446 else if (attr_form_is_section_offset (attr))
10447 dwarf2_complex_location_expr_complaint ();
10448 else if (attr_form_is_block (attr))
10449 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10450 else
10451 dwarf2_complex_location_expr_complaint ();
10452
10453 return 1;
10454 }
10455
10456 return 0;
10457}
10458
c906108c
SS
10459/* Add an aggregate field to the field list. */
10460
10461static void
107d2387 10462dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10463 struct dwarf2_cu *cu)
6e70227d 10464{
e7c27a73 10465 struct objfile *objfile = cu->objfile;
5e2b427d 10466 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10467 struct nextfield *new_field;
10468 struct attribute *attr;
10469 struct field *fp;
10470 char *fieldname = "";
10471
10472 /* Allocate a new field list entry and link it in. */
10473 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10474 make_cleanup (xfree, new_field);
c906108c 10475 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10476
10477 if (die->tag == DW_TAG_inheritance)
10478 {
10479 new_field->next = fip->baseclasses;
10480 fip->baseclasses = new_field;
10481 }
10482 else
10483 {
10484 new_field->next = fip->fields;
10485 fip->fields = new_field;
10486 }
c906108c
SS
10487 fip->nfields++;
10488
e142c38c 10489 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10490 if (attr)
10491 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10492 else
10493 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10494 if (new_field->accessibility != DW_ACCESS_public)
10495 fip->non_public_fields = 1;
60d5a603 10496
e142c38c 10497 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10498 if (attr)
10499 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10500 else
10501 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10502
10503 fp = &new_field->field;
a9a9bd0f 10504
e142c38c 10505 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10506 {
74ac6d43
TT
10507 LONGEST offset;
10508
a9a9bd0f 10509 /* Data member other than a C++ static data member. */
6e70227d 10510
c906108c 10511 /* Get type of field. */
e7c27a73 10512 fp->type = die_type (die, cu);
c906108c 10513
d6a843b5 10514 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10515
c906108c 10516 /* Get bit size of field (zero if none). */
e142c38c 10517 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10518 if (attr)
10519 {
10520 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10521 }
10522 else
10523 {
10524 FIELD_BITSIZE (*fp) = 0;
10525 }
10526
10527 /* Get bit offset of field. */
74ac6d43
TT
10528 if (handle_data_member_location (die, cu, &offset))
10529 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10530 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10531 if (attr)
10532 {
5e2b427d 10533 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10534 {
10535 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10536 additional bit offset from the MSB of the containing
10537 anonymous object to the MSB of the field. We don't
10538 have to do anything special since we don't need to
10539 know the size of the anonymous object. */
f41f5e61 10540 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10541 }
10542 else
10543 {
10544 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10545 MSB of the anonymous object, subtract off the number of
10546 bits from the MSB of the field to the MSB of the
10547 object, and then subtract off the number of bits of
10548 the field itself. The result is the bit offset of
10549 the LSB of the field. */
c906108c
SS
10550 int anonymous_size;
10551 int bit_offset = DW_UNSND (attr);
10552
e142c38c 10553 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10554 if (attr)
10555 {
10556 /* The size of the anonymous object containing
10557 the bit field is explicit, so use the
10558 indicated size (in bytes). */
10559 anonymous_size = DW_UNSND (attr);
10560 }
10561 else
10562 {
10563 /* The size of the anonymous object containing
10564 the bit field must be inferred from the type
10565 attribute of the data member containing the
10566 bit field. */
10567 anonymous_size = TYPE_LENGTH (fp->type);
10568 }
f41f5e61
PA
10569 SET_FIELD_BITPOS (*fp,
10570 (FIELD_BITPOS (*fp)
10571 + anonymous_size * bits_per_byte
10572 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10573 }
10574 }
10575
10576 /* Get name of field. */
39cbfefa
DJ
10577 fieldname = dwarf2_name (die, cu);
10578 if (fieldname == NULL)
10579 fieldname = "";
d8151005
DJ
10580
10581 /* The name is already allocated along with this objfile, so we don't
10582 need to duplicate it for the type. */
10583 fp->name = fieldname;
c906108c
SS
10584
10585 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10586 pointer or virtual base class pointer) to private. */
e142c38c 10587 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10588 {
d48cc9dd 10589 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10590 new_field->accessibility = DW_ACCESS_private;
10591 fip->non_public_fields = 1;
10592 }
10593 }
a9a9bd0f 10594 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10595 {
a9a9bd0f
DC
10596 /* C++ static member. */
10597
10598 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10599 is a declaration, but all versions of G++ as of this writing
10600 (so through at least 3.2.1) incorrectly generate
10601 DW_TAG_variable tags. */
6e70227d 10602
ff355380 10603 const char *physname;
c906108c 10604
a9a9bd0f 10605 /* Get name of field. */
39cbfefa
DJ
10606 fieldname = dwarf2_name (die, cu);
10607 if (fieldname == NULL)
c906108c
SS
10608 return;
10609
254e6b9e 10610 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10611 if (attr
10612 /* Only create a symbol if this is an external value.
10613 new_symbol checks this and puts the value in the global symbol
10614 table, which we want. If it is not external, new_symbol
10615 will try to put the value in cu->list_in_scope which is wrong. */
10616 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10617 {
10618 /* A static const member, not much different than an enum as far as
10619 we're concerned, except that we can support more types. */
10620 new_symbol (die, NULL, cu);
10621 }
10622
2df3850c 10623 /* Get physical name. */
ff355380 10624 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10625
d8151005
DJ
10626 /* The name is already allocated along with this objfile, so we don't
10627 need to duplicate it for the type. */
10628 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10629 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10630 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10631 }
10632 else if (die->tag == DW_TAG_inheritance)
10633 {
74ac6d43 10634 LONGEST offset;
d4b96c9a 10635
74ac6d43
TT
10636 /* C++ base class field. */
10637 if (handle_data_member_location (die, cu, &offset))
10638 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10639 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10640 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10641 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10642 fip->nbaseclasses++;
10643 }
10644}
10645
98751a41
JK
10646/* Add a typedef defined in the scope of the FIP's class. */
10647
10648static void
10649dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10650 struct dwarf2_cu *cu)
6e70227d 10651{
98751a41 10652 struct objfile *objfile = cu->objfile;
98751a41
JK
10653 struct typedef_field_list *new_field;
10654 struct attribute *attr;
10655 struct typedef_field *fp;
10656 char *fieldname = "";
10657
10658 /* Allocate a new field list entry and link it in. */
10659 new_field = xzalloc (sizeof (*new_field));
10660 make_cleanup (xfree, new_field);
10661
10662 gdb_assert (die->tag == DW_TAG_typedef);
10663
10664 fp = &new_field->field;
10665
10666 /* Get name of field. */
10667 fp->name = dwarf2_name (die, cu);
10668 if (fp->name == NULL)
10669 return;
10670
10671 fp->type = read_type_die (die, cu);
10672
10673 new_field->next = fip->typedef_field_list;
10674 fip->typedef_field_list = new_field;
10675 fip->typedef_field_list_count++;
10676}
10677
c906108c
SS
10678/* Create the vector of fields, and attach it to the type. */
10679
10680static void
fba45db2 10681dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10682 struct dwarf2_cu *cu)
c906108c
SS
10683{
10684 int nfields = fip->nfields;
10685
10686 /* Record the field count, allocate space for the array of fields,
10687 and create blank accessibility bitfields if necessary. */
10688 TYPE_NFIELDS (type) = nfields;
10689 TYPE_FIELDS (type) = (struct field *)
10690 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10691 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10692
b4ba55a1 10693 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10694 {
10695 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10696
10697 TYPE_FIELD_PRIVATE_BITS (type) =
10698 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10699 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10700
10701 TYPE_FIELD_PROTECTED_BITS (type) =
10702 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10703 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10704
774b6a14
TT
10705 TYPE_FIELD_IGNORE_BITS (type) =
10706 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10707 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10708 }
10709
10710 /* If the type has baseclasses, allocate and clear a bit vector for
10711 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10712 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10713 {
10714 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10715 unsigned char *pointer;
c906108c
SS
10716
10717 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10718 pointer = TYPE_ALLOC (type, num_bytes);
10719 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10720 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10721 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10722 }
10723
3e43a32a
MS
10724 /* Copy the saved-up fields into the field vector. Start from the head of
10725 the list, adding to the tail of the field array, so that they end up in
10726 the same order in the array in which they were added to the list. */
c906108c
SS
10727 while (nfields-- > 0)
10728 {
7d0ccb61
DJ
10729 struct nextfield *fieldp;
10730
10731 if (fip->fields)
10732 {
10733 fieldp = fip->fields;
10734 fip->fields = fieldp->next;
10735 }
10736 else
10737 {
10738 fieldp = fip->baseclasses;
10739 fip->baseclasses = fieldp->next;
10740 }
10741
10742 TYPE_FIELD (type, nfields) = fieldp->field;
10743 switch (fieldp->accessibility)
c906108c 10744 {
c5aa993b 10745 case DW_ACCESS_private:
b4ba55a1
JB
10746 if (cu->language != language_ada)
10747 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10748 break;
c906108c 10749
c5aa993b 10750 case DW_ACCESS_protected:
b4ba55a1
JB
10751 if (cu->language != language_ada)
10752 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10753 break;
c906108c 10754
c5aa993b
JM
10755 case DW_ACCESS_public:
10756 break;
c906108c 10757
c5aa993b
JM
10758 default:
10759 /* Unknown accessibility. Complain and treat it as public. */
10760 {
e2e0b3e5 10761 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10762 fieldp->accessibility);
c5aa993b
JM
10763 }
10764 break;
c906108c
SS
10765 }
10766 if (nfields < fip->nbaseclasses)
10767 {
7d0ccb61 10768 switch (fieldp->virtuality)
c906108c 10769 {
c5aa993b
JM
10770 case DW_VIRTUALITY_virtual:
10771 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10772 if (cu->language == language_ada)
a73c6dcd 10773 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10774 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10775 break;
c906108c
SS
10776 }
10777 }
c906108c
SS
10778 }
10779}
10780
7d27a96d
TT
10781/* Return true if this member function is a constructor, false
10782 otherwise. */
10783
10784static int
10785dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10786{
10787 const char *fieldname;
10788 const char *typename;
10789 int len;
10790
10791 if (die->parent == NULL)
10792 return 0;
10793
10794 if (die->parent->tag != DW_TAG_structure_type
10795 && die->parent->tag != DW_TAG_union_type
10796 && die->parent->tag != DW_TAG_class_type)
10797 return 0;
10798
10799 fieldname = dwarf2_name (die, cu);
10800 typename = dwarf2_name (die->parent, cu);
10801 if (fieldname == NULL || typename == NULL)
10802 return 0;
10803
10804 len = strlen (fieldname);
10805 return (strncmp (fieldname, typename, len) == 0
10806 && (typename[len] == '\0' || typename[len] == '<'));
10807}
10808
c906108c
SS
10809/* Add a member function to the proper fieldlist. */
10810
10811static void
107d2387 10812dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10813 struct type *type, struct dwarf2_cu *cu)
c906108c 10814{
e7c27a73 10815 struct objfile *objfile = cu->objfile;
c906108c
SS
10816 struct attribute *attr;
10817 struct fnfieldlist *flp;
10818 int i;
10819 struct fn_field *fnp;
10820 char *fieldname;
c906108c 10821 struct nextfnfield *new_fnfield;
f792889a 10822 struct type *this_type;
60d5a603 10823 enum dwarf_access_attribute accessibility;
c906108c 10824
b4ba55a1 10825 if (cu->language == language_ada)
a73c6dcd 10826 error (_("unexpected member function in Ada type"));
b4ba55a1 10827
2df3850c 10828 /* Get name of member function. */
39cbfefa
DJ
10829 fieldname = dwarf2_name (die, cu);
10830 if (fieldname == NULL)
2df3850c 10831 return;
c906108c 10832
c906108c
SS
10833 /* Look up member function name in fieldlist. */
10834 for (i = 0; i < fip->nfnfields; i++)
10835 {
27bfe10e 10836 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10837 break;
10838 }
10839
10840 /* Create new list element if necessary. */
10841 if (i < fip->nfnfields)
10842 flp = &fip->fnfieldlists[i];
10843 else
10844 {
10845 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10846 {
10847 fip->fnfieldlists = (struct fnfieldlist *)
10848 xrealloc (fip->fnfieldlists,
10849 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10850 * sizeof (struct fnfieldlist));
c906108c 10851 if (fip->nfnfields == 0)
c13c43fd 10852 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10853 }
10854 flp = &fip->fnfieldlists[fip->nfnfields];
10855 flp->name = fieldname;
10856 flp->length = 0;
10857 flp->head = NULL;
3da10d80 10858 i = fip->nfnfields++;
c906108c
SS
10859 }
10860
10861 /* Create a new member function field and chain it to the field list
0963b4bd 10862 entry. */
c906108c 10863 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10864 make_cleanup (xfree, new_fnfield);
c906108c
SS
10865 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10866 new_fnfield->next = flp->head;
10867 flp->head = new_fnfield;
10868 flp->length++;
10869
10870 /* Fill in the member function field info. */
10871 fnp = &new_fnfield->fnfield;
3da10d80
KS
10872
10873 /* Delay processing of the physname until later. */
10874 if (cu->language == language_cplus || cu->language == language_java)
10875 {
10876 add_to_method_list (type, i, flp->length - 1, fieldname,
10877 die, cu);
10878 }
10879 else
10880 {
1d06ead6 10881 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10882 fnp->physname = physname ? physname : "";
10883 }
10884
c906108c 10885 fnp->type = alloc_type (objfile);
f792889a
DJ
10886 this_type = read_type_die (die, cu);
10887 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10888 {
f792889a 10889 int nparams = TYPE_NFIELDS (this_type);
c906108c 10890
f792889a 10891 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10892 of the method itself (TYPE_CODE_METHOD). */
10893 smash_to_method_type (fnp->type, type,
f792889a
DJ
10894 TYPE_TARGET_TYPE (this_type),
10895 TYPE_FIELDS (this_type),
10896 TYPE_NFIELDS (this_type),
10897 TYPE_VARARGS (this_type));
c906108c
SS
10898
10899 /* Handle static member functions.
c5aa993b 10900 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10901 member functions. G++ helps GDB by marking the first
10902 parameter for non-static member functions (which is the this
10903 pointer) as artificial. We obtain this information from
10904 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10905 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10906 fnp->voffset = VOFFSET_STATIC;
10907 }
10908 else
e2e0b3e5 10909 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10910 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10911
10912 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10913 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10914 fnp->fcontext = die_containing_type (die, cu);
c906108c 10915
3e43a32a
MS
10916 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10917 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10918
10919 /* Get accessibility. */
e142c38c 10920 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10921 if (attr)
60d5a603
JK
10922 accessibility = DW_UNSND (attr);
10923 else
10924 accessibility = dwarf2_default_access_attribute (die, cu);
10925 switch (accessibility)
c906108c 10926 {
60d5a603
JK
10927 case DW_ACCESS_private:
10928 fnp->is_private = 1;
10929 break;
10930 case DW_ACCESS_protected:
10931 fnp->is_protected = 1;
10932 break;
c906108c
SS
10933 }
10934
b02dede2 10935 /* Check for artificial methods. */
e142c38c 10936 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10937 if (attr && DW_UNSND (attr) != 0)
10938 fnp->is_artificial = 1;
10939
7d27a96d
TT
10940 fnp->is_constructor = dwarf2_is_constructor (die, cu);
10941
0d564a31 10942 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10943 function. For older versions of GCC, this is an offset in the
10944 appropriate virtual table, as specified by DW_AT_containing_type.
10945 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10946 to the object address. */
10947
e142c38c 10948 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10949 if (attr)
8e19ed76 10950 {
aec5aa8b 10951 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10952 {
aec5aa8b
TT
10953 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10954 {
10955 /* Old-style GCC. */
10956 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10957 }
10958 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10959 || (DW_BLOCK (attr)->size > 1
10960 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10961 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10962 {
10963 struct dwarf_block blk;
10964 int offset;
10965
10966 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10967 ? 1 : 2);
10968 blk.size = DW_BLOCK (attr)->size - offset;
10969 blk.data = DW_BLOCK (attr)->data + offset;
10970 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10971 if ((fnp->voffset % cu->header.addr_size) != 0)
10972 dwarf2_complex_location_expr_complaint ();
10973 else
10974 fnp->voffset /= cu->header.addr_size;
10975 fnp->voffset += 2;
10976 }
10977 else
10978 dwarf2_complex_location_expr_complaint ();
10979
10980 if (!fnp->fcontext)
10981 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10982 }
3690dd37 10983 else if (attr_form_is_section_offset (attr))
8e19ed76 10984 {
4d3c2250 10985 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10986 }
10987 else
10988 {
4d3c2250
KB
10989 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10990 fieldname);
8e19ed76 10991 }
0d564a31 10992 }
d48cc9dd
DJ
10993 else
10994 {
10995 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10996 if (attr && DW_UNSND (attr))
10997 {
10998 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10999 complaint (&symfile_complaints,
3e43a32a
MS
11000 _("Member function \"%s\" (offset %d) is virtual "
11001 "but the vtable offset is not specified"),
b64f50a1 11002 fieldname, die->offset.sect_off);
9655fd1a 11003 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11004 TYPE_CPLUS_DYNAMIC (type) = 1;
11005 }
11006 }
c906108c
SS
11007}
11008
11009/* Create the vector of member function fields, and attach it to the type. */
11010
11011static void
fba45db2 11012dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11013 struct dwarf2_cu *cu)
c906108c
SS
11014{
11015 struct fnfieldlist *flp;
c906108c
SS
11016 int i;
11017
b4ba55a1 11018 if (cu->language == language_ada)
a73c6dcd 11019 error (_("unexpected member functions in Ada type"));
b4ba55a1 11020
c906108c
SS
11021 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11022 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11023 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11024
11025 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11026 {
11027 struct nextfnfield *nfp = flp->head;
11028 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11029 int k;
11030
11031 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11032 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11033 fn_flp->fn_fields = (struct fn_field *)
11034 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11035 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11036 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11037 }
11038
11039 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11040}
11041
1168df01
JB
11042/* Returns non-zero if NAME is the name of a vtable member in CU's
11043 language, zero otherwise. */
11044static int
11045is_vtable_name (const char *name, struct dwarf2_cu *cu)
11046{
11047 static const char vptr[] = "_vptr";
987504bb 11048 static const char vtable[] = "vtable";
1168df01 11049
987504bb
JJ
11050 /* Look for the C++ and Java forms of the vtable. */
11051 if ((cu->language == language_java
11052 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11053 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11054 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11055 return 1;
11056
11057 return 0;
11058}
11059
c0dd20ea 11060/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11061 functions, with the ABI-specified layout. If TYPE describes
11062 such a structure, smash it into a member function type.
61049d3b
DJ
11063
11064 GCC shouldn't do this; it should just output pointer to member DIEs.
11065 This is GCC PR debug/28767. */
c0dd20ea 11066
0b92b5bb
TT
11067static void
11068quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11069{
0b92b5bb 11070 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11071
11072 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11073 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11074 return;
c0dd20ea
DJ
11075
11076 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11077 if (TYPE_FIELD_NAME (type, 0) == NULL
11078 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11079 || TYPE_FIELD_NAME (type, 1) == NULL
11080 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11081 return;
c0dd20ea
DJ
11082
11083 /* Find the type of the method. */
0b92b5bb 11084 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11085 if (pfn_type == NULL
11086 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11087 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11088 return;
c0dd20ea
DJ
11089
11090 /* Look for the "this" argument. */
11091 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11092 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11093 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11094 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11095 return;
c0dd20ea
DJ
11096
11097 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11098 new_type = alloc_type (objfile);
11099 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11100 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11101 TYPE_VARARGS (pfn_type));
0b92b5bb 11102 smash_to_methodptr_type (type, new_type);
c0dd20ea 11103}
1168df01 11104
685b1105
JK
11105/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11106 (icc). */
11107
11108static int
11109producer_is_icc (struct dwarf2_cu *cu)
11110{
11111 if (!cu->checked_producer)
11112 check_producer (cu);
11113
11114 return cu->producer_is_icc;
11115}
11116
c906108c 11117/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11118 (definition) to create a type for the structure or union. Fill in
11119 the type's name and general properties; the members will not be
11120 processed until process_structure_type.
c906108c 11121
c767944b
DJ
11122 NOTE: we need to call these functions regardless of whether or not the
11123 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11124 structure or union. This gets the type entered into our set of
11125 user defined types.
11126
11127 However, if the structure is incomplete (an opaque struct/union)
11128 then suppress creating a symbol table entry for it since gdb only
11129 wants to find the one with the complete definition. Note that if
11130 it is complete, we just call new_symbol, which does it's own
11131 checking about whether the struct/union is anonymous or not (and
11132 suppresses creating a symbol table entry itself). */
11133
f792889a 11134static struct type *
134d01f1 11135read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11136{
e7c27a73 11137 struct objfile *objfile = cu->objfile;
c906108c
SS
11138 struct type *type;
11139 struct attribute *attr;
39cbfefa 11140 char *name;
c906108c 11141
348e048f
DE
11142 /* If the definition of this type lives in .debug_types, read that type.
11143 Don't follow DW_AT_specification though, that will take us back up
11144 the chain and we want to go down. */
45e58e77 11145 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11146 if (attr)
11147 {
11148 struct dwarf2_cu *type_cu = cu;
11149 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11150
348e048f
DE
11151 /* We could just recurse on read_structure_type, but we need to call
11152 get_die_type to ensure only one type for this DIE is created.
11153 This is important, for example, because for c++ classes we need
11154 TYPE_NAME set which is only done by new_symbol. Blech. */
11155 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11156
11157 /* TYPE_CU may not be the same as CU.
11158 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11159 return set_die_type (die, type, cu);
11160 }
11161
c0dd20ea 11162 type = alloc_type (objfile);
c906108c 11163 INIT_CPLUS_SPECIFIC (type);
93311388 11164
39cbfefa
DJ
11165 name = dwarf2_name (die, cu);
11166 if (name != NULL)
c906108c 11167 {
987504bb
JJ
11168 if (cu->language == language_cplus
11169 || cu->language == language_java)
63d06c5c 11170 {
3da10d80
KS
11171 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11172
11173 /* dwarf2_full_name might have already finished building the DIE's
11174 type. If so, there is no need to continue. */
11175 if (get_die_type (die, cu) != NULL)
11176 return get_die_type (die, cu);
11177
11178 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11179 if (die->tag == DW_TAG_structure_type
11180 || die->tag == DW_TAG_class_type)
11181 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11182 }
11183 else
11184 {
d8151005
DJ
11185 /* The name is already allocated along with this objfile, so
11186 we don't need to duplicate it for the type. */
94af9270
KS
11187 TYPE_TAG_NAME (type) = (char *) name;
11188 if (die->tag == DW_TAG_class_type)
11189 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11190 }
c906108c
SS
11191 }
11192
11193 if (die->tag == DW_TAG_structure_type)
11194 {
11195 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11196 }
11197 else if (die->tag == DW_TAG_union_type)
11198 {
11199 TYPE_CODE (type) = TYPE_CODE_UNION;
11200 }
11201 else
11202 {
c906108c
SS
11203 TYPE_CODE (type) = TYPE_CODE_CLASS;
11204 }
11205
0cc2414c
TT
11206 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11207 TYPE_DECLARED_CLASS (type) = 1;
11208
e142c38c 11209 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11210 if (attr)
11211 {
11212 TYPE_LENGTH (type) = DW_UNSND (attr);
11213 }
11214 else
11215 {
11216 TYPE_LENGTH (type) = 0;
11217 }
11218
685b1105
JK
11219 if (producer_is_icc (cu))
11220 {
11221 /* ICC does not output the required DW_AT_declaration
11222 on incomplete types, but gives them a size of zero. */
11223 }
11224 else
11225 TYPE_STUB_SUPPORTED (type) = 1;
11226
dc718098 11227 if (die_is_declaration (die, cu))
876cecd0 11228 TYPE_STUB (type) = 1;
a6c727b2
DJ
11229 else if (attr == NULL && die->child == NULL
11230 && producer_is_realview (cu->producer))
11231 /* RealView does not output the required DW_AT_declaration
11232 on incomplete types. */
11233 TYPE_STUB (type) = 1;
dc718098 11234
c906108c
SS
11235 /* We need to add the type field to the die immediately so we don't
11236 infinitely recurse when dealing with pointers to the structure
0963b4bd 11237 type within the structure itself. */
1c379e20 11238 set_die_type (die, type, cu);
c906108c 11239
7e314c57
JK
11240 /* set_die_type should be already done. */
11241 set_descriptive_type (type, die, cu);
11242
c767944b
DJ
11243 return type;
11244}
11245
11246/* Finish creating a structure or union type, including filling in
11247 its members and creating a symbol for it. */
11248
11249static void
11250process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11251{
11252 struct objfile *objfile = cu->objfile;
11253 struct die_info *child_die = die->child;
11254 struct type *type;
11255
11256 type = get_die_type (die, cu);
11257 if (type == NULL)
11258 type = read_structure_type (die, cu);
11259
e142c38c 11260 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11261 {
11262 struct field_info fi;
11263 struct die_info *child_die;
34eaf542 11264 VEC (symbolp) *template_args = NULL;
c767944b 11265 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11266
11267 memset (&fi, 0, sizeof (struct field_info));
11268
639d11d3 11269 child_die = die->child;
c906108c
SS
11270
11271 while (child_die && child_die->tag)
11272 {
a9a9bd0f
DC
11273 if (child_die->tag == DW_TAG_member
11274 || child_die->tag == DW_TAG_variable)
c906108c 11275 {
a9a9bd0f
DC
11276 /* NOTE: carlton/2002-11-05: A C++ static data member
11277 should be a DW_TAG_member that is a declaration, but
11278 all versions of G++ as of this writing (so through at
11279 least 3.2.1) incorrectly generate DW_TAG_variable
11280 tags for them instead. */
e7c27a73 11281 dwarf2_add_field (&fi, child_die, cu);
c906108c 11282 }
8713b1b1 11283 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11284 {
0963b4bd 11285 /* C++ member function. */
e7c27a73 11286 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11287 }
11288 else if (child_die->tag == DW_TAG_inheritance)
11289 {
11290 /* C++ base class field. */
e7c27a73 11291 dwarf2_add_field (&fi, child_die, cu);
c906108c 11292 }
98751a41
JK
11293 else if (child_die->tag == DW_TAG_typedef)
11294 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11295 else if (child_die->tag == DW_TAG_template_type_param
11296 || child_die->tag == DW_TAG_template_value_param)
11297 {
11298 struct symbol *arg = new_symbol (child_die, NULL, cu);
11299
f1078f66
DJ
11300 if (arg != NULL)
11301 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11302 }
11303
c906108c
SS
11304 child_die = sibling_die (child_die);
11305 }
11306
34eaf542
TT
11307 /* Attach template arguments to type. */
11308 if (! VEC_empty (symbolp, template_args))
11309 {
11310 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11311 TYPE_N_TEMPLATE_ARGUMENTS (type)
11312 = VEC_length (symbolp, template_args);
11313 TYPE_TEMPLATE_ARGUMENTS (type)
11314 = obstack_alloc (&objfile->objfile_obstack,
11315 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11316 * sizeof (struct symbol *)));
11317 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11318 VEC_address (symbolp, template_args),
11319 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11320 * sizeof (struct symbol *)));
11321 VEC_free (symbolp, template_args);
11322 }
11323
c906108c
SS
11324 /* Attach fields and member functions to the type. */
11325 if (fi.nfields)
e7c27a73 11326 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11327 if (fi.nfnfields)
11328 {
e7c27a73 11329 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11330
c5aa993b 11331 /* Get the type which refers to the base class (possibly this
c906108c 11332 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11333 class from the DW_AT_containing_type attribute. This use of
11334 DW_AT_containing_type is a GNU extension. */
c906108c 11335
e142c38c 11336 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11337 {
e7c27a73 11338 struct type *t = die_containing_type (die, cu);
c906108c
SS
11339
11340 TYPE_VPTR_BASETYPE (type) = t;
11341 if (type == t)
11342 {
c906108c
SS
11343 int i;
11344
11345 /* Our own class provides vtbl ptr. */
11346 for (i = TYPE_NFIELDS (t) - 1;
11347 i >= TYPE_N_BASECLASSES (t);
11348 --i)
11349 {
0d5cff50 11350 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11351
1168df01 11352 if (is_vtable_name (fieldname, cu))
c906108c
SS
11353 {
11354 TYPE_VPTR_FIELDNO (type) = i;
11355 break;
11356 }
11357 }
11358
11359 /* Complain if virtual function table field not found. */
11360 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11361 complaint (&symfile_complaints,
3e43a32a
MS
11362 _("virtual function table pointer "
11363 "not found when defining class '%s'"),
4d3c2250
KB
11364 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11365 "");
c906108c
SS
11366 }
11367 else
11368 {
11369 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11370 }
11371 }
f6235d4c
EZ
11372 else if (cu->producer
11373 && strncmp (cu->producer,
11374 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11375 {
11376 /* The IBM XLC compiler does not provide direct indication
11377 of the containing type, but the vtable pointer is
11378 always named __vfp. */
11379
11380 int i;
11381
11382 for (i = TYPE_NFIELDS (type) - 1;
11383 i >= TYPE_N_BASECLASSES (type);
11384 --i)
11385 {
11386 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11387 {
11388 TYPE_VPTR_FIELDNO (type) = i;
11389 TYPE_VPTR_BASETYPE (type) = type;
11390 break;
11391 }
11392 }
11393 }
c906108c 11394 }
98751a41
JK
11395
11396 /* Copy fi.typedef_field_list linked list elements content into the
11397 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11398 if (fi.typedef_field_list)
11399 {
11400 int i = fi.typedef_field_list_count;
11401
a0d7a4ff 11402 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11403 TYPE_TYPEDEF_FIELD_ARRAY (type)
11404 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11405 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11406
11407 /* Reverse the list order to keep the debug info elements order. */
11408 while (--i >= 0)
11409 {
11410 struct typedef_field *dest, *src;
6e70227d 11411
98751a41
JK
11412 dest = &TYPE_TYPEDEF_FIELD (type, i);
11413 src = &fi.typedef_field_list->field;
11414 fi.typedef_field_list = fi.typedef_field_list->next;
11415 *dest = *src;
11416 }
11417 }
c767944b
DJ
11418
11419 do_cleanups (back_to);
eb2a6f42
TT
11420
11421 if (HAVE_CPLUS_STRUCT (type))
11422 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11423 }
63d06c5c 11424
bb5ed363 11425 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11426
90aeadfc
DC
11427 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11428 snapshots) has been known to create a die giving a declaration
11429 for a class that has, as a child, a die giving a definition for a
11430 nested class. So we have to process our children even if the
11431 current die is a declaration. Normally, of course, a declaration
11432 won't have any children at all. */
134d01f1 11433
90aeadfc
DC
11434 while (child_die != NULL && child_die->tag)
11435 {
11436 if (child_die->tag == DW_TAG_member
11437 || child_die->tag == DW_TAG_variable
34eaf542
TT
11438 || child_die->tag == DW_TAG_inheritance
11439 || child_die->tag == DW_TAG_template_value_param
11440 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11441 {
90aeadfc 11442 /* Do nothing. */
134d01f1 11443 }
90aeadfc
DC
11444 else
11445 process_die (child_die, cu);
134d01f1 11446
90aeadfc 11447 child_die = sibling_die (child_die);
134d01f1
DJ
11448 }
11449
fa4028e9
JB
11450 /* Do not consider external references. According to the DWARF standard,
11451 these DIEs are identified by the fact that they have no byte_size
11452 attribute, and a declaration attribute. */
11453 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11454 || !die_is_declaration (die, cu))
c767944b 11455 new_symbol (die, type, cu);
134d01f1
DJ
11456}
11457
11458/* Given a DW_AT_enumeration_type die, set its type. We do not
11459 complete the type's fields yet, or create any symbols. */
c906108c 11460
f792889a 11461static struct type *
134d01f1 11462read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11463{
e7c27a73 11464 struct objfile *objfile = cu->objfile;
c906108c 11465 struct type *type;
c906108c 11466 struct attribute *attr;
0114d602 11467 const char *name;
134d01f1 11468
348e048f
DE
11469 /* If the definition of this type lives in .debug_types, read that type.
11470 Don't follow DW_AT_specification though, that will take us back up
11471 the chain and we want to go down. */
45e58e77 11472 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11473 if (attr)
11474 {
11475 struct dwarf2_cu *type_cu = cu;
11476 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11477
348e048f 11478 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11479
11480 /* TYPE_CU may not be the same as CU.
11481 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11482 return set_die_type (die, type, cu);
11483 }
11484
c906108c
SS
11485 type = alloc_type (objfile);
11486
11487 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11488 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11489 if (name != NULL)
0114d602 11490 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11491
e142c38c 11492 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11493 if (attr)
11494 {
11495 TYPE_LENGTH (type) = DW_UNSND (attr);
11496 }
11497 else
11498 {
11499 TYPE_LENGTH (type) = 0;
11500 }
11501
137033e9
JB
11502 /* The enumeration DIE can be incomplete. In Ada, any type can be
11503 declared as private in the package spec, and then defined only
11504 inside the package body. Such types are known as Taft Amendment
11505 Types. When another package uses such a type, an incomplete DIE
11506 may be generated by the compiler. */
02eb380e 11507 if (die_is_declaration (die, cu))
876cecd0 11508 TYPE_STUB (type) = 1;
02eb380e 11509
f792889a 11510 return set_die_type (die, type, cu);
134d01f1
DJ
11511}
11512
11513/* Given a pointer to a die which begins an enumeration, process all
11514 the dies that define the members of the enumeration, and create the
11515 symbol for the enumeration type.
11516
11517 NOTE: We reverse the order of the element list. */
11518
11519static void
11520process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11521{
f792889a 11522 struct type *this_type;
134d01f1 11523
f792889a
DJ
11524 this_type = get_die_type (die, cu);
11525 if (this_type == NULL)
11526 this_type = read_enumeration_type (die, cu);
9dc481d3 11527
639d11d3 11528 if (die->child != NULL)
c906108c 11529 {
9dc481d3
DE
11530 struct die_info *child_die;
11531 struct symbol *sym;
11532 struct field *fields = NULL;
11533 int num_fields = 0;
11534 int unsigned_enum = 1;
11535 char *name;
cafec441
TT
11536 int flag_enum = 1;
11537 ULONGEST mask = 0;
9dc481d3 11538
639d11d3 11539 child_die = die->child;
c906108c
SS
11540 while (child_die && child_die->tag)
11541 {
11542 if (child_die->tag != DW_TAG_enumerator)
11543 {
e7c27a73 11544 process_die (child_die, cu);
c906108c
SS
11545 }
11546 else
11547 {
39cbfefa
DJ
11548 name = dwarf2_name (child_die, cu);
11549 if (name)
c906108c 11550 {
f792889a 11551 sym = new_symbol (child_die, this_type, cu);
c906108c 11552 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11553 {
11554 unsigned_enum = 0;
11555 flag_enum = 0;
11556 }
11557 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11558 flag_enum = 0;
11559 else
11560 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11561
11562 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11563 {
11564 fields = (struct field *)
11565 xrealloc (fields,
11566 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11567 * sizeof (struct field));
c906108c
SS
11568 }
11569
3567439c 11570 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11571 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11572 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11573 FIELD_BITSIZE (fields[num_fields]) = 0;
11574
11575 num_fields++;
11576 }
11577 }
11578
11579 child_die = sibling_die (child_die);
11580 }
11581
11582 if (num_fields)
11583 {
f792889a
DJ
11584 TYPE_NFIELDS (this_type) = num_fields;
11585 TYPE_FIELDS (this_type) = (struct field *)
11586 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11587 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11588 sizeof (struct field) * num_fields);
b8c9b27d 11589 xfree (fields);
c906108c
SS
11590 }
11591 if (unsigned_enum)
876cecd0 11592 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11593 if (flag_enum)
11594 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11595 }
134d01f1 11596
6c83ed52
TT
11597 /* If we are reading an enum from a .debug_types unit, and the enum
11598 is a declaration, and the enum is not the signatured type in the
11599 unit, then we do not want to add a symbol for it. Adding a
11600 symbol would in some cases obscure the true definition of the
11601 enum, giving users an incomplete type when the definition is
11602 actually available. Note that we do not want to do this for all
11603 enums which are just declarations, because C++0x allows forward
11604 enum declarations. */
3019eac3 11605 if (cu->per_cu->is_debug_types
6c83ed52
TT
11606 && die_is_declaration (die, cu))
11607 {
52dc124a 11608 struct signatured_type *sig_type;
6c83ed52 11609
52dc124a 11610 sig_type
6c83ed52 11611 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11612 cu->per_cu->info_or_types_section,
6c83ed52 11613 cu->per_cu->offset);
3019eac3
DE
11614 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11615 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11616 return;
11617 }
11618
f792889a 11619 new_symbol (die, this_type, cu);
c906108c
SS
11620}
11621
11622/* Extract all information from a DW_TAG_array_type DIE and put it in
11623 the DIE's type field. For now, this only handles one dimensional
11624 arrays. */
11625
f792889a 11626static struct type *
e7c27a73 11627read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11628{
e7c27a73 11629 struct objfile *objfile = cu->objfile;
c906108c 11630 struct die_info *child_die;
7e314c57 11631 struct type *type;
c906108c
SS
11632 struct type *element_type, *range_type, *index_type;
11633 struct type **range_types = NULL;
11634 struct attribute *attr;
11635 int ndim = 0;
11636 struct cleanup *back_to;
39cbfefa 11637 char *name;
c906108c 11638
e7c27a73 11639 element_type = die_type (die, cu);
c906108c 11640
7e314c57
JK
11641 /* The die_type call above may have already set the type for this DIE. */
11642 type = get_die_type (die, cu);
11643 if (type)
11644 return type;
11645
c906108c
SS
11646 /* Irix 6.2 native cc creates array types without children for
11647 arrays with unspecified length. */
639d11d3 11648 if (die->child == NULL)
c906108c 11649 {
46bf5051 11650 index_type = objfile_type (objfile)->builtin_int;
c906108c 11651 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11652 type = create_array_type (NULL, element_type, range_type);
11653 return set_die_type (die, type, cu);
c906108c
SS
11654 }
11655
11656 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11657 child_die = die->child;
c906108c
SS
11658 while (child_die && child_die->tag)
11659 {
11660 if (child_die->tag == DW_TAG_subrange_type)
11661 {
f792889a 11662 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11663
f792889a 11664 if (child_type != NULL)
a02abb62 11665 {
0963b4bd
MS
11666 /* The range type was succesfully read. Save it for the
11667 array type creation. */
a02abb62
JB
11668 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11669 {
11670 range_types = (struct type **)
11671 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11672 * sizeof (struct type *));
11673 if (ndim == 0)
11674 make_cleanup (free_current_contents, &range_types);
11675 }
f792889a 11676 range_types[ndim++] = child_type;
a02abb62 11677 }
c906108c
SS
11678 }
11679 child_die = sibling_die (child_die);
11680 }
11681
11682 /* Dwarf2 dimensions are output from left to right, create the
11683 necessary array types in backwards order. */
7ca2d3a3 11684
c906108c 11685 type = element_type;
7ca2d3a3
DL
11686
11687 if (read_array_order (die, cu) == DW_ORD_col_major)
11688 {
11689 int i = 0;
9a619af0 11690
7ca2d3a3
DL
11691 while (i < ndim)
11692 type = create_array_type (NULL, type, range_types[i++]);
11693 }
11694 else
11695 {
11696 while (ndim-- > 0)
11697 type = create_array_type (NULL, type, range_types[ndim]);
11698 }
c906108c 11699
f5f8a009
EZ
11700 /* Understand Dwarf2 support for vector types (like they occur on
11701 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11702 array type. This is not part of the Dwarf2/3 standard yet, but a
11703 custom vendor extension. The main difference between a regular
11704 array and the vector variant is that vectors are passed by value
11705 to functions. */
e142c38c 11706 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11707 if (attr)
ea37ba09 11708 make_vector_type (type);
f5f8a009 11709
dbc98a8b
KW
11710 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11711 implementation may choose to implement triple vectors using this
11712 attribute. */
11713 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11714 if (attr)
11715 {
11716 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11717 TYPE_LENGTH (type) = DW_UNSND (attr);
11718 else
3e43a32a
MS
11719 complaint (&symfile_complaints,
11720 _("DW_AT_byte_size for array type smaller "
11721 "than the total size of elements"));
dbc98a8b
KW
11722 }
11723
39cbfefa
DJ
11724 name = dwarf2_name (die, cu);
11725 if (name)
11726 TYPE_NAME (type) = name;
6e70227d 11727
0963b4bd 11728 /* Install the type in the die. */
7e314c57
JK
11729 set_die_type (die, type, cu);
11730
11731 /* set_die_type should be already done. */
b4ba55a1
JB
11732 set_descriptive_type (type, die, cu);
11733
c906108c
SS
11734 do_cleanups (back_to);
11735
7e314c57 11736 return type;
c906108c
SS
11737}
11738
7ca2d3a3 11739static enum dwarf_array_dim_ordering
6e70227d 11740read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11741{
11742 struct attribute *attr;
11743
11744 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11745
11746 if (attr) return DW_SND (attr);
11747
0963b4bd
MS
11748 /* GNU F77 is a special case, as at 08/2004 array type info is the
11749 opposite order to the dwarf2 specification, but data is still
11750 laid out as per normal fortran.
7ca2d3a3 11751
0963b4bd
MS
11752 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11753 version checking. */
7ca2d3a3 11754
905e0470
PM
11755 if (cu->language == language_fortran
11756 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11757 {
11758 return DW_ORD_row_major;
11759 }
11760
6e70227d 11761 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11762 {
11763 case array_column_major:
11764 return DW_ORD_col_major;
11765 case array_row_major:
11766 default:
11767 return DW_ORD_row_major;
11768 };
11769}
11770
72019c9c 11771/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11772 the DIE's type field. */
72019c9c 11773
f792889a 11774static struct type *
72019c9c
GM
11775read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11776{
7e314c57
JK
11777 struct type *domain_type, *set_type;
11778 struct attribute *attr;
f792889a 11779
7e314c57
JK
11780 domain_type = die_type (die, cu);
11781
11782 /* The die_type call above may have already set the type for this DIE. */
11783 set_type = get_die_type (die, cu);
11784 if (set_type)
11785 return set_type;
11786
11787 set_type = create_set_type (NULL, domain_type);
11788
11789 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11790 if (attr)
11791 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11792
f792889a 11793 return set_die_type (die, set_type, cu);
72019c9c 11794}
7ca2d3a3 11795
0971de02
TT
11796/* A helper for read_common_block that creates a locexpr baton.
11797 SYM is the symbol which we are marking as computed.
11798 COMMON_DIE is the DIE for the common block.
11799 COMMON_LOC is the location expression attribute for the common
11800 block itself.
11801 MEMBER_LOC is the location expression attribute for the particular
11802 member of the common block that we are processing.
11803 CU is the CU from which the above come. */
11804
11805static void
11806mark_common_block_symbol_computed (struct symbol *sym,
11807 struct die_info *common_die,
11808 struct attribute *common_loc,
11809 struct attribute *member_loc,
11810 struct dwarf2_cu *cu)
11811{
11812 struct objfile *objfile = dwarf2_per_objfile->objfile;
11813 struct dwarf2_locexpr_baton *baton;
11814 gdb_byte *ptr;
11815 unsigned int cu_off;
11816 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11817 LONGEST offset = 0;
11818
11819 gdb_assert (common_loc && member_loc);
11820 gdb_assert (attr_form_is_block (common_loc));
11821 gdb_assert (attr_form_is_block (member_loc)
11822 || attr_form_is_constant (member_loc));
11823
11824 baton = obstack_alloc (&objfile->objfile_obstack,
11825 sizeof (struct dwarf2_locexpr_baton));
11826 baton->per_cu = cu->per_cu;
11827 gdb_assert (baton->per_cu);
11828
11829 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11830
11831 if (attr_form_is_constant (member_loc))
11832 {
11833 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11834 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11835 }
11836 else
11837 baton->size += DW_BLOCK (member_loc)->size;
11838
11839 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11840 baton->data = ptr;
11841
11842 *ptr++ = DW_OP_call4;
11843 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11844 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11845 ptr += 4;
11846
11847 if (attr_form_is_constant (member_loc))
11848 {
11849 *ptr++ = DW_OP_addr;
11850 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11851 ptr += cu->header.addr_size;
11852 }
11853 else
11854 {
11855 /* We have to copy the data here, because DW_OP_call4 will only
11856 use a DW_AT_location attribute. */
11857 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11858 ptr += DW_BLOCK (member_loc)->size;
11859 }
11860
11861 *ptr++ = DW_OP_plus;
11862 gdb_assert (ptr - baton->data == baton->size);
11863
11864 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11865 SYMBOL_LOCATION_BATON (sym) = baton;
11866 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11867}
11868
4357ac6c
TT
11869/* Create appropriate locally-scoped variables for all the
11870 DW_TAG_common_block entries. Also create a struct common_block
11871 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11872 is used to sepate the common blocks name namespace from regular
11873 variable names. */
c906108c
SS
11874
11875static void
e7c27a73 11876read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11877{
0971de02
TT
11878 struct attribute *attr;
11879
11880 attr = dwarf2_attr (die, DW_AT_location, cu);
11881 if (attr)
11882 {
11883 /* Support the .debug_loc offsets. */
11884 if (attr_form_is_block (attr))
11885 {
11886 /* Ok. */
11887 }
11888 else if (attr_form_is_section_offset (attr))
11889 {
11890 dwarf2_complex_location_expr_complaint ();
11891 attr = NULL;
11892 }
11893 else
11894 {
11895 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11896 "common block member");
11897 attr = NULL;
11898 }
11899 }
11900
639d11d3 11901 if (die->child != NULL)
c906108c 11902 {
4357ac6c
TT
11903 struct objfile *objfile = cu->objfile;
11904 struct die_info *child_die;
11905 size_t n_entries = 0, size;
11906 struct common_block *common_block;
11907 struct symbol *sym;
74ac6d43 11908
4357ac6c
TT
11909 for (child_die = die->child;
11910 child_die && child_die->tag;
11911 child_die = sibling_die (child_die))
11912 ++n_entries;
11913
11914 size = (sizeof (struct common_block)
11915 + (n_entries - 1) * sizeof (struct symbol *));
11916 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11917 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11918 common_block->n_entries = 0;
11919
11920 for (child_die = die->child;
11921 child_die && child_die->tag;
11922 child_die = sibling_die (child_die))
11923 {
11924 /* Create the symbol in the DW_TAG_common_block block in the current
11925 symbol scope. */
e7c27a73 11926 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11927 if (sym != NULL)
11928 {
11929 struct attribute *member_loc;
11930
11931 common_block->contents[common_block->n_entries++] = sym;
11932
11933 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11934 cu);
11935 if (member_loc)
11936 {
11937 /* GDB has handled this for a long time, but it is
11938 not specified by DWARF. It seems to have been
11939 emitted by gfortran at least as recently as:
11940 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11941 complaint (&symfile_complaints,
11942 _("Variable in common block has "
11943 "DW_AT_data_member_location "
11944 "- DIE at 0x%x [in module %s]"),
11945 child_die->offset.sect_off, cu->objfile->name);
11946
11947 if (attr_form_is_section_offset (member_loc))
11948 dwarf2_complex_location_expr_complaint ();
11949 else if (attr_form_is_constant (member_loc)
11950 || attr_form_is_block (member_loc))
11951 {
11952 if (attr)
11953 mark_common_block_symbol_computed (sym, die, attr,
11954 member_loc, cu);
11955 }
11956 else
11957 dwarf2_complex_location_expr_complaint ();
11958 }
11959 }
c906108c 11960 }
4357ac6c
TT
11961
11962 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11963 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
11964 }
11965}
11966
0114d602 11967/* Create a type for a C++ namespace. */
d9fa45fe 11968
0114d602
DJ
11969static struct type *
11970read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11971{
e7c27a73 11972 struct objfile *objfile = cu->objfile;
0114d602 11973 const char *previous_prefix, *name;
9219021c 11974 int is_anonymous;
0114d602
DJ
11975 struct type *type;
11976
11977 /* For extensions, reuse the type of the original namespace. */
11978 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11979 {
11980 struct die_info *ext_die;
11981 struct dwarf2_cu *ext_cu = cu;
9a619af0 11982
0114d602
DJ
11983 ext_die = dwarf2_extension (die, &ext_cu);
11984 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11985
11986 /* EXT_CU may not be the same as CU.
11987 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11988 return set_die_type (die, type, cu);
11989 }
9219021c 11990
e142c38c 11991 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11992
11993 /* Now build the name of the current namespace. */
11994
0114d602
DJ
11995 previous_prefix = determine_prefix (die, cu);
11996 if (previous_prefix[0] != '\0')
11997 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11998 previous_prefix, name, 0, cu);
0114d602
DJ
11999
12000 /* Create the type. */
12001 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12002 objfile);
12003 TYPE_NAME (type) = (char *) name;
12004 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12005
60531b24 12006 return set_die_type (die, type, cu);
0114d602
DJ
12007}
12008
12009/* Read a C++ namespace. */
12010
12011static void
12012read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12013{
12014 struct objfile *objfile = cu->objfile;
0114d602 12015 int is_anonymous;
9219021c 12016
5c4e30ca
DC
12017 /* Add a symbol associated to this if we haven't seen the namespace
12018 before. Also, add a using directive if it's an anonymous
12019 namespace. */
9219021c 12020
f2f0e013 12021 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12022 {
12023 struct type *type;
12024
0114d602 12025 type = read_type_die (die, cu);
e7c27a73 12026 new_symbol (die, type, cu);
5c4e30ca 12027
e8e80198 12028 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12029 if (is_anonymous)
0114d602
DJ
12030 {
12031 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12032
c0cc3a76 12033 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 12034 NULL, NULL, &objfile->objfile_obstack);
0114d602 12035 }
5c4e30ca 12036 }
9219021c 12037
639d11d3 12038 if (die->child != NULL)
d9fa45fe 12039 {
639d11d3 12040 struct die_info *child_die = die->child;
6e70227d 12041
d9fa45fe
DC
12042 while (child_die && child_die->tag)
12043 {
e7c27a73 12044 process_die (child_die, cu);
d9fa45fe
DC
12045 child_die = sibling_die (child_die);
12046 }
12047 }
38d518c9
EZ
12048}
12049
f55ee35c
JK
12050/* Read a Fortran module as type. This DIE can be only a declaration used for
12051 imported module. Still we need that type as local Fortran "use ... only"
12052 declaration imports depend on the created type in determine_prefix. */
12053
12054static struct type *
12055read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12056{
12057 struct objfile *objfile = cu->objfile;
12058 char *module_name;
12059 struct type *type;
12060
12061 module_name = dwarf2_name (die, cu);
12062 if (!module_name)
3e43a32a
MS
12063 complaint (&symfile_complaints,
12064 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12065 die->offset.sect_off);
f55ee35c
JK
12066 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12067
12068 /* determine_prefix uses TYPE_TAG_NAME. */
12069 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12070
12071 return set_die_type (die, type, cu);
12072}
12073
5d7cb8df
JK
12074/* Read a Fortran module. */
12075
12076static void
12077read_module (struct die_info *die, struct dwarf2_cu *cu)
12078{
12079 struct die_info *child_die = die->child;
12080
5d7cb8df
JK
12081 while (child_die && child_die->tag)
12082 {
12083 process_die (child_die, cu);
12084 child_die = sibling_die (child_die);
12085 }
12086}
12087
38d518c9
EZ
12088/* Return the name of the namespace represented by DIE. Set
12089 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12090 namespace. */
12091
12092static const char *
e142c38c 12093namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12094{
12095 struct die_info *current_die;
12096 const char *name = NULL;
12097
12098 /* Loop through the extensions until we find a name. */
12099
12100 for (current_die = die;
12101 current_die != NULL;
f2f0e013 12102 current_die = dwarf2_extension (die, &cu))
38d518c9 12103 {
e142c38c 12104 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12105 if (name != NULL)
12106 break;
12107 }
12108
12109 /* Is it an anonymous namespace? */
12110
12111 *is_anonymous = (name == NULL);
12112 if (*is_anonymous)
2b1dbab0 12113 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12114
12115 return name;
d9fa45fe
DC
12116}
12117
c906108c
SS
12118/* Extract all information from a DW_TAG_pointer_type DIE and add to
12119 the user defined type vector. */
12120
f792889a 12121static struct type *
e7c27a73 12122read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12123{
5e2b427d 12124 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12125 struct comp_unit_head *cu_header = &cu->header;
c906108c 12126 struct type *type;
8b2dbe47
KB
12127 struct attribute *attr_byte_size;
12128 struct attribute *attr_address_class;
12129 int byte_size, addr_class;
7e314c57
JK
12130 struct type *target_type;
12131
12132 target_type = die_type (die, cu);
c906108c 12133
7e314c57
JK
12134 /* The die_type call above may have already set the type for this DIE. */
12135 type = get_die_type (die, cu);
12136 if (type)
12137 return type;
12138
12139 type = lookup_pointer_type (target_type);
8b2dbe47 12140
e142c38c 12141 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12142 if (attr_byte_size)
12143 byte_size = DW_UNSND (attr_byte_size);
c906108c 12144 else
8b2dbe47
KB
12145 byte_size = cu_header->addr_size;
12146
e142c38c 12147 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12148 if (attr_address_class)
12149 addr_class = DW_UNSND (attr_address_class);
12150 else
12151 addr_class = DW_ADDR_none;
12152
12153 /* If the pointer size or address class is different than the
12154 default, create a type variant marked as such and set the
12155 length accordingly. */
12156 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12157 {
5e2b427d 12158 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12159 {
12160 int type_flags;
12161
849957d9 12162 type_flags = gdbarch_address_class_type_flags
5e2b427d 12163 (gdbarch, byte_size, addr_class);
876cecd0
TT
12164 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12165 == 0);
8b2dbe47
KB
12166 type = make_type_with_address_space (type, type_flags);
12167 }
12168 else if (TYPE_LENGTH (type) != byte_size)
12169 {
3e43a32a
MS
12170 complaint (&symfile_complaints,
12171 _("invalid pointer size %d"), byte_size);
8b2dbe47 12172 }
6e70227d 12173 else
9a619af0
MS
12174 {
12175 /* Should we also complain about unhandled address classes? */
12176 }
c906108c 12177 }
8b2dbe47
KB
12178
12179 TYPE_LENGTH (type) = byte_size;
f792889a 12180 return set_die_type (die, type, cu);
c906108c
SS
12181}
12182
12183/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12184 the user defined type vector. */
12185
f792889a 12186static struct type *
e7c27a73 12187read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12188{
12189 struct type *type;
12190 struct type *to_type;
12191 struct type *domain;
12192
e7c27a73
DJ
12193 to_type = die_type (die, cu);
12194 domain = die_containing_type (die, cu);
0d5de010 12195
7e314c57
JK
12196 /* The calls above may have already set the type for this DIE. */
12197 type = get_die_type (die, cu);
12198 if (type)
12199 return type;
12200
0d5de010
DJ
12201 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12202 type = lookup_methodptr_type (to_type);
12203 else
12204 type = lookup_memberptr_type (to_type, domain);
c906108c 12205
f792889a 12206 return set_die_type (die, type, cu);
c906108c
SS
12207}
12208
12209/* Extract all information from a DW_TAG_reference_type DIE and add to
12210 the user defined type vector. */
12211
f792889a 12212static struct type *
e7c27a73 12213read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12214{
e7c27a73 12215 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12216 struct type *type, *target_type;
c906108c
SS
12217 struct attribute *attr;
12218
7e314c57
JK
12219 target_type = die_type (die, cu);
12220
12221 /* The die_type call above may have already set the type for this DIE. */
12222 type = get_die_type (die, cu);
12223 if (type)
12224 return type;
12225
12226 type = lookup_reference_type (target_type);
e142c38c 12227 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12228 if (attr)
12229 {
12230 TYPE_LENGTH (type) = DW_UNSND (attr);
12231 }
12232 else
12233 {
107d2387 12234 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12235 }
f792889a 12236 return set_die_type (die, type, cu);
c906108c
SS
12237}
12238
f792889a 12239static struct type *
e7c27a73 12240read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12241{
f792889a 12242 struct type *base_type, *cv_type;
c906108c 12243
e7c27a73 12244 base_type = die_type (die, cu);
7e314c57
JK
12245
12246 /* The die_type call above may have already set the type for this DIE. */
12247 cv_type = get_die_type (die, cu);
12248 if (cv_type)
12249 return cv_type;
12250
2f608a3a
KW
12251 /* In case the const qualifier is applied to an array type, the element type
12252 is so qualified, not the array type (section 6.7.3 of C99). */
12253 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12254 {
12255 struct type *el_type, *inner_array;
12256
12257 base_type = copy_type (base_type);
12258 inner_array = base_type;
12259
12260 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12261 {
12262 TYPE_TARGET_TYPE (inner_array) =
12263 copy_type (TYPE_TARGET_TYPE (inner_array));
12264 inner_array = TYPE_TARGET_TYPE (inner_array);
12265 }
12266
12267 el_type = TYPE_TARGET_TYPE (inner_array);
12268 TYPE_TARGET_TYPE (inner_array) =
12269 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12270
12271 return set_die_type (die, base_type, cu);
12272 }
12273
f792889a
DJ
12274 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12275 return set_die_type (die, cv_type, cu);
c906108c
SS
12276}
12277
f792889a 12278static struct type *
e7c27a73 12279read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12280{
f792889a 12281 struct type *base_type, *cv_type;
c906108c 12282
e7c27a73 12283 base_type = die_type (die, cu);
7e314c57
JK
12284
12285 /* The die_type call above may have already set the type for this DIE. */
12286 cv_type = get_die_type (die, cu);
12287 if (cv_type)
12288 return cv_type;
12289
f792889a
DJ
12290 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12291 return set_die_type (die, cv_type, cu);
c906108c
SS
12292}
12293
12294/* Extract all information from a DW_TAG_string_type DIE and add to
12295 the user defined type vector. It isn't really a user defined type,
12296 but it behaves like one, with other DIE's using an AT_user_def_type
12297 attribute to reference it. */
12298
f792889a 12299static struct type *
e7c27a73 12300read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12301{
e7c27a73 12302 struct objfile *objfile = cu->objfile;
3b7538c0 12303 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12304 struct type *type, *range_type, *index_type, *char_type;
12305 struct attribute *attr;
12306 unsigned int length;
12307
e142c38c 12308 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12309 if (attr)
12310 {
12311 length = DW_UNSND (attr);
12312 }
12313 else
12314 {
0963b4bd 12315 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12316 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12317 if (attr)
12318 {
12319 length = DW_UNSND (attr);
12320 }
12321 else
12322 {
12323 length = 1;
12324 }
c906108c 12325 }
6ccb9162 12326
46bf5051 12327 index_type = objfile_type (objfile)->builtin_int;
c906108c 12328 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12329 char_type = language_string_char_type (cu->language_defn, gdbarch);
12330 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12331
f792889a 12332 return set_die_type (die, type, cu);
c906108c
SS
12333}
12334
12335/* Handle DIES due to C code like:
12336
12337 struct foo
c5aa993b
JM
12338 {
12339 int (*funcp)(int a, long l);
12340 int b;
12341 };
c906108c 12342
0963b4bd 12343 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12344
f792889a 12345static struct type *
e7c27a73 12346read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12347{
bb5ed363 12348 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12349 struct type *type; /* Type that this function returns. */
12350 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12351 struct attribute *attr;
12352
e7c27a73 12353 type = die_type (die, cu);
7e314c57
JK
12354
12355 /* The die_type call above may have already set the type for this DIE. */
12356 ftype = get_die_type (die, cu);
12357 if (ftype)
12358 return ftype;
12359
0c8b41f1 12360 ftype = lookup_function_type (type);
c906108c 12361
5b8101ae 12362 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12363 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12364 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12365 || cu->language == language_cplus
5b8101ae
PM
12366 || cu->language == language_java
12367 || cu->language == language_pascal)
876cecd0 12368 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12369 else if (producer_is_realview (cu->producer))
12370 /* RealView does not emit DW_AT_prototyped. We can not
12371 distinguish prototyped and unprototyped functions; default to
12372 prototyped, since that is more common in modern code (and
12373 RealView warns about unprototyped functions). */
12374 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12375
c055b101
CV
12376 /* Store the calling convention in the type if it's available in
12377 the subroutine die. Otherwise set the calling convention to
12378 the default value DW_CC_normal. */
12379 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12380 if (attr)
12381 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12382 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12383 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12384 else
12385 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12386
12387 /* We need to add the subroutine type to the die immediately so
12388 we don't infinitely recurse when dealing with parameters
0963b4bd 12389 declared as the same subroutine type. */
76c10ea2 12390 set_die_type (die, ftype, cu);
6e70227d 12391
639d11d3 12392 if (die->child != NULL)
c906108c 12393 {
bb5ed363 12394 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12395 struct die_info *child_die;
8072405b 12396 int nparams, iparams;
c906108c
SS
12397
12398 /* Count the number of parameters.
12399 FIXME: GDB currently ignores vararg functions, but knows about
12400 vararg member functions. */
8072405b 12401 nparams = 0;
639d11d3 12402 child_die = die->child;
c906108c
SS
12403 while (child_die && child_die->tag)
12404 {
12405 if (child_die->tag == DW_TAG_formal_parameter)
12406 nparams++;
12407 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12408 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12409 child_die = sibling_die (child_die);
12410 }
12411
12412 /* Allocate storage for parameters and fill them in. */
12413 TYPE_NFIELDS (ftype) = nparams;
12414 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12415 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12416
8072405b
JK
12417 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12418 even if we error out during the parameters reading below. */
12419 for (iparams = 0; iparams < nparams; iparams++)
12420 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12421
12422 iparams = 0;
639d11d3 12423 child_die = die->child;
c906108c
SS
12424 while (child_die && child_die->tag)
12425 {
12426 if (child_die->tag == DW_TAG_formal_parameter)
12427 {
3ce3b1ba
PA
12428 struct type *arg_type;
12429
12430 /* DWARF version 2 has no clean way to discern C++
12431 static and non-static member functions. G++ helps
12432 GDB by marking the first parameter for non-static
12433 member functions (which is the this pointer) as
12434 artificial. We pass this information to
12435 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12436
12437 DWARF version 3 added DW_AT_object_pointer, which GCC
12438 4.5 does not yet generate. */
e142c38c 12439 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12440 if (attr)
12441 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12442 else
418835cc
KS
12443 {
12444 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12445
12446 /* GCC/43521: In java, the formal parameter
12447 "this" is sometimes not marked with DW_AT_artificial. */
12448 if (cu->language == language_java)
12449 {
12450 const char *name = dwarf2_name (child_die, cu);
9a619af0 12451
418835cc
KS
12452 if (name && !strcmp (name, "this"))
12453 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12454 }
12455 }
3ce3b1ba
PA
12456 arg_type = die_type (child_die, cu);
12457
12458 /* RealView does not mark THIS as const, which the testsuite
12459 expects. GCC marks THIS as const in method definitions,
12460 but not in the class specifications (GCC PR 43053). */
12461 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12462 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12463 {
12464 int is_this = 0;
12465 struct dwarf2_cu *arg_cu = cu;
12466 const char *name = dwarf2_name (child_die, cu);
12467
12468 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12469 if (attr)
12470 {
12471 /* If the compiler emits this, use it. */
12472 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12473 is_this = 1;
12474 }
12475 else if (name && strcmp (name, "this") == 0)
12476 /* Function definitions will have the argument names. */
12477 is_this = 1;
12478 else if (name == NULL && iparams == 0)
12479 /* Declarations may not have the names, so like
12480 elsewhere in GDB, assume an artificial first
12481 argument is "this". */
12482 is_this = 1;
12483
12484 if (is_this)
12485 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12486 arg_type, 0);
12487 }
12488
12489 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12490 iparams++;
12491 }
12492 child_die = sibling_die (child_die);
12493 }
12494 }
12495
76c10ea2 12496 return ftype;
c906108c
SS
12497}
12498
f792889a 12499static struct type *
e7c27a73 12500read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12501{
e7c27a73 12502 struct objfile *objfile = cu->objfile;
0114d602 12503 const char *name = NULL;
3c8e0968 12504 struct type *this_type, *target_type;
c906108c 12505
94af9270 12506 name = dwarf2_full_name (NULL, die, cu);
f792889a 12507 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12508 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12509 TYPE_NAME (this_type) = (char *) name;
f792889a 12510 set_die_type (die, this_type, cu);
3c8e0968
DE
12511 target_type = die_type (die, cu);
12512 if (target_type != this_type)
12513 TYPE_TARGET_TYPE (this_type) = target_type;
12514 else
12515 {
12516 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12517 spec and cause infinite loops in GDB. */
12518 complaint (&symfile_complaints,
12519 _("Self-referential DW_TAG_typedef "
12520 "- DIE at 0x%x [in module %s]"),
b64f50a1 12521 die->offset.sect_off, objfile->name);
3c8e0968
DE
12522 TYPE_TARGET_TYPE (this_type) = NULL;
12523 }
f792889a 12524 return this_type;
c906108c
SS
12525}
12526
12527/* Find a representation of a given base type and install
12528 it in the TYPE field of the die. */
12529
f792889a 12530static struct type *
e7c27a73 12531read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12532{
e7c27a73 12533 struct objfile *objfile = cu->objfile;
c906108c
SS
12534 struct type *type;
12535 struct attribute *attr;
12536 int encoding = 0, size = 0;
39cbfefa 12537 char *name;
6ccb9162
UW
12538 enum type_code code = TYPE_CODE_INT;
12539 int type_flags = 0;
12540 struct type *target_type = NULL;
c906108c 12541
e142c38c 12542 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12543 if (attr)
12544 {
12545 encoding = DW_UNSND (attr);
12546 }
e142c38c 12547 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12548 if (attr)
12549 {
12550 size = DW_UNSND (attr);
12551 }
39cbfefa 12552 name = dwarf2_name (die, cu);
6ccb9162 12553 if (!name)
c906108c 12554 {
6ccb9162
UW
12555 complaint (&symfile_complaints,
12556 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12557 }
6ccb9162
UW
12558
12559 switch (encoding)
c906108c 12560 {
6ccb9162
UW
12561 case DW_ATE_address:
12562 /* Turn DW_ATE_address into a void * pointer. */
12563 code = TYPE_CODE_PTR;
12564 type_flags |= TYPE_FLAG_UNSIGNED;
12565 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12566 break;
12567 case DW_ATE_boolean:
12568 code = TYPE_CODE_BOOL;
12569 type_flags |= TYPE_FLAG_UNSIGNED;
12570 break;
12571 case DW_ATE_complex_float:
12572 code = TYPE_CODE_COMPLEX;
12573 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12574 break;
12575 case DW_ATE_decimal_float:
12576 code = TYPE_CODE_DECFLOAT;
12577 break;
12578 case DW_ATE_float:
12579 code = TYPE_CODE_FLT;
12580 break;
12581 case DW_ATE_signed:
12582 break;
12583 case DW_ATE_unsigned:
12584 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12585 if (cu->language == language_fortran
12586 && name
12587 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12588 code = TYPE_CODE_CHAR;
6ccb9162
UW
12589 break;
12590 case DW_ATE_signed_char:
6e70227d 12591 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12592 || cu->language == language_pascal
12593 || cu->language == language_fortran)
6ccb9162
UW
12594 code = TYPE_CODE_CHAR;
12595 break;
12596 case DW_ATE_unsigned_char:
868a0084 12597 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12598 || cu->language == language_pascal
12599 || cu->language == language_fortran)
6ccb9162
UW
12600 code = TYPE_CODE_CHAR;
12601 type_flags |= TYPE_FLAG_UNSIGNED;
12602 break;
75079b2b
TT
12603 case DW_ATE_UTF:
12604 /* We just treat this as an integer and then recognize the
12605 type by name elsewhere. */
12606 break;
12607
6ccb9162
UW
12608 default:
12609 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12610 dwarf_type_encoding_name (encoding));
12611 break;
c906108c 12612 }
6ccb9162 12613
0114d602
DJ
12614 type = init_type (code, size, type_flags, NULL, objfile);
12615 TYPE_NAME (type) = name;
6ccb9162
UW
12616 TYPE_TARGET_TYPE (type) = target_type;
12617
0114d602 12618 if (name && strcmp (name, "char") == 0)
876cecd0 12619 TYPE_NOSIGN (type) = 1;
0114d602 12620
f792889a 12621 return set_die_type (die, type, cu);
c906108c
SS
12622}
12623
a02abb62
JB
12624/* Read the given DW_AT_subrange DIE. */
12625
f792889a 12626static struct type *
a02abb62
JB
12627read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12628{
12629 struct type *base_type;
12630 struct type *range_type;
12631 struct attribute *attr;
4fae6e18
JK
12632 LONGEST low, high;
12633 int low_default_is_valid;
39cbfefa 12634 char *name;
43bbcdc2 12635 LONGEST negative_mask;
e77813c8 12636
a02abb62 12637 base_type = die_type (die, cu);
953ac07e
JK
12638 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12639 check_typedef (base_type);
a02abb62 12640
7e314c57
JK
12641 /* The die_type call above may have already set the type for this DIE. */
12642 range_type = get_die_type (die, cu);
12643 if (range_type)
12644 return range_type;
12645
4fae6e18
JK
12646 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12647 omitting DW_AT_lower_bound. */
12648 switch (cu->language)
6e70227d 12649 {
4fae6e18
JK
12650 case language_c:
12651 case language_cplus:
12652 low = 0;
12653 low_default_is_valid = 1;
12654 break;
12655 case language_fortran:
12656 low = 1;
12657 low_default_is_valid = 1;
12658 break;
12659 case language_d:
12660 case language_java:
12661 case language_objc:
12662 low = 0;
12663 low_default_is_valid = (cu->header.version >= 4);
12664 break;
12665 case language_ada:
12666 case language_m2:
12667 case language_pascal:
a02abb62 12668 low = 1;
4fae6e18
JK
12669 low_default_is_valid = (cu->header.version >= 4);
12670 break;
12671 default:
12672 low = 0;
12673 low_default_is_valid = 0;
12674 break;
a02abb62
JB
12675 }
12676
dd5e6932
DJ
12677 /* FIXME: For variable sized arrays either of these could be
12678 a variable rather than a constant value. We'll allow it,
12679 but we don't know how to handle it. */
e142c38c 12680 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12681 if (attr)
4fae6e18
JK
12682 low = dwarf2_get_attr_constant_value (attr, low);
12683 else if (!low_default_is_valid)
12684 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12685 "- DIE at 0x%x [in module %s]"),
12686 die->offset.sect_off, cu->objfile->name);
a02abb62 12687
e142c38c 12688 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12689 if (attr)
6e70227d 12690 {
d48323d8 12691 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12692 {
12693 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12694 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12695 FIXME: GDB does not yet know how to handle dynamic
12696 arrays properly, treat them as arrays with unspecified
12697 length for now.
12698
12699 FIXME: jimb/2003-09-22: GDB does not really know
12700 how to handle arrays of unspecified length
12701 either; we just represent them as zero-length
12702 arrays. Choose an appropriate upper bound given
12703 the lower bound we've computed above. */
12704 high = low - 1;
12705 }
12706 else
12707 high = dwarf2_get_attr_constant_value (attr, 1);
12708 }
e77813c8
PM
12709 else
12710 {
12711 attr = dwarf2_attr (die, DW_AT_count, cu);
12712 if (attr)
12713 {
12714 int count = dwarf2_get_attr_constant_value (attr, 1);
12715 high = low + count - 1;
12716 }
c2ff108b
JK
12717 else
12718 {
12719 /* Unspecified array length. */
12720 high = low - 1;
12721 }
e77813c8
PM
12722 }
12723
12724 /* Dwarf-2 specifications explicitly allows to create subrange types
12725 without specifying a base type.
12726 In that case, the base type must be set to the type of
12727 the lower bound, upper bound or count, in that order, if any of these
12728 three attributes references an object that has a type.
12729 If no base type is found, the Dwarf-2 specifications say that
12730 a signed integer type of size equal to the size of an address should
12731 be used.
12732 For the following C code: `extern char gdb_int [];'
12733 GCC produces an empty range DIE.
12734 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12735 high bound or count are not yet handled by this code. */
e77813c8
PM
12736 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12737 {
12738 struct objfile *objfile = cu->objfile;
12739 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12740 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12741 struct type *int_type = objfile_type (objfile)->builtin_int;
12742
12743 /* Test "int", "long int", and "long long int" objfile types,
12744 and select the first one having a size above or equal to the
12745 architecture address size. */
12746 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12747 base_type = int_type;
12748 else
12749 {
12750 int_type = objfile_type (objfile)->builtin_long;
12751 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12752 base_type = int_type;
12753 else
12754 {
12755 int_type = objfile_type (objfile)->builtin_long_long;
12756 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12757 base_type = int_type;
12758 }
12759 }
12760 }
a02abb62 12761
6e70227d 12762 negative_mask =
43bbcdc2
PH
12763 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12764 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12765 low |= negative_mask;
12766 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12767 high |= negative_mask;
12768
a02abb62
JB
12769 range_type = create_range_type (NULL, base_type, low, high);
12770
bbb0eef6
JK
12771 /* Mark arrays with dynamic length at least as an array of unspecified
12772 length. GDB could check the boundary but before it gets implemented at
12773 least allow accessing the array elements. */
d48323d8 12774 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12775 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12776
c2ff108b
JK
12777 /* Ada expects an empty array on no boundary attributes. */
12778 if (attr == NULL && cu->language != language_ada)
12779 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12780
39cbfefa
DJ
12781 name = dwarf2_name (die, cu);
12782 if (name)
12783 TYPE_NAME (range_type) = name;
6e70227d 12784
e142c38c 12785 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12786 if (attr)
12787 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12788
7e314c57
JK
12789 set_die_type (die, range_type, cu);
12790
12791 /* set_die_type should be already done. */
b4ba55a1
JB
12792 set_descriptive_type (range_type, die, cu);
12793
7e314c57 12794 return range_type;
a02abb62 12795}
6e70227d 12796
f792889a 12797static struct type *
81a17f79
JB
12798read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12799{
12800 struct type *type;
81a17f79 12801
81a17f79
JB
12802 /* For now, we only support the C meaning of an unspecified type: void. */
12803
0114d602
DJ
12804 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12805 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12806
f792889a 12807 return set_die_type (die, type, cu);
81a17f79 12808}
a02abb62 12809
639d11d3
DC
12810/* Read a single die and all its descendents. Set the die's sibling
12811 field to NULL; set other fields in the die correctly, and set all
12812 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12813 location of the info_ptr after reading all of those dies. PARENT
12814 is the parent of the die in question. */
12815
12816static struct die_info *
dee91e82
DE
12817read_die_and_children (const struct die_reader_specs *reader,
12818 gdb_byte *info_ptr,
12819 gdb_byte **new_info_ptr,
12820 struct die_info *parent)
639d11d3
DC
12821{
12822 struct die_info *die;
fe1b8b76 12823 gdb_byte *cur_ptr;
639d11d3
DC
12824 int has_children;
12825
93311388 12826 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12827 if (die == NULL)
12828 {
12829 *new_info_ptr = cur_ptr;
12830 return NULL;
12831 }
93311388 12832 store_in_ref_table (die, reader->cu);
639d11d3
DC
12833
12834 if (has_children)
348e048f 12835 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12836 else
12837 {
12838 die->child = NULL;
12839 *new_info_ptr = cur_ptr;
12840 }
12841
12842 die->sibling = NULL;
12843 die->parent = parent;
12844 return die;
12845}
12846
12847/* Read a die, all of its descendents, and all of its siblings; set
12848 all of the fields of all of the dies correctly. Arguments are as
12849 in read_die_and_children. */
12850
12851static struct die_info *
93311388
DE
12852read_die_and_siblings (const struct die_reader_specs *reader,
12853 gdb_byte *info_ptr,
fe1b8b76 12854 gdb_byte **new_info_ptr,
639d11d3
DC
12855 struct die_info *parent)
12856{
12857 struct die_info *first_die, *last_sibling;
fe1b8b76 12858 gdb_byte *cur_ptr;
639d11d3 12859
c906108c 12860 cur_ptr = info_ptr;
639d11d3
DC
12861 first_die = last_sibling = NULL;
12862
12863 while (1)
c906108c 12864 {
639d11d3 12865 struct die_info *die
dee91e82 12866 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12867
1d325ec1 12868 if (die == NULL)
c906108c 12869 {
639d11d3
DC
12870 *new_info_ptr = cur_ptr;
12871 return first_die;
c906108c 12872 }
1d325ec1
DJ
12873
12874 if (!first_die)
12875 first_die = die;
c906108c 12876 else
1d325ec1
DJ
12877 last_sibling->sibling = die;
12878
12879 last_sibling = die;
c906108c 12880 }
c906108c
SS
12881}
12882
3019eac3
DE
12883/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12884 attributes.
12885 The caller is responsible for filling in the extra attributes
12886 and updating (*DIEP)->num_attrs.
12887 Set DIEP to point to a newly allocated die with its information,
12888 except for its child, sibling, and parent fields.
12889 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12890
12891static gdb_byte *
3019eac3
DE
12892read_full_die_1 (const struct die_reader_specs *reader,
12893 struct die_info **diep, gdb_byte *info_ptr,
12894 int *has_children, int num_extra_attrs)
93311388 12895{
b64f50a1
JK
12896 unsigned int abbrev_number, bytes_read, i;
12897 sect_offset offset;
93311388
DE
12898 struct abbrev_info *abbrev;
12899 struct die_info *die;
12900 struct dwarf2_cu *cu = reader->cu;
12901 bfd *abfd = reader->abfd;
12902
b64f50a1 12903 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12904 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12905 info_ptr += bytes_read;
12906 if (!abbrev_number)
12907 {
12908 *diep = NULL;
12909 *has_children = 0;
12910 return info_ptr;
12911 }
12912
433df2d4 12913 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12914 if (!abbrev)
348e048f
DE
12915 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12916 abbrev_number,
12917 bfd_get_filename (abfd));
12918
3019eac3 12919 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12920 die->offset = offset;
12921 die->tag = abbrev->tag;
12922 die->abbrev = abbrev_number;
12923
3019eac3
DE
12924 /* Make the result usable.
12925 The caller needs to update num_attrs after adding the extra
12926 attributes. */
93311388
DE
12927 die->num_attrs = abbrev->num_attrs;
12928
12929 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12930 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12931 info_ptr);
93311388
DE
12932
12933 *diep = die;
12934 *has_children = abbrev->has_children;
12935 return info_ptr;
12936}
12937
3019eac3
DE
12938/* Read a die and all its attributes.
12939 Set DIEP to point to a newly allocated die with its information,
12940 except for its child, sibling, and parent fields.
12941 Set HAS_CHILDREN to tell whether the die has children or not. */
12942
12943static gdb_byte *
12944read_full_die (const struct die_reader_specs *reader,
12945 struct die_info **diep, gdb_byte *info_ptr,
12946 int *has_children)
12947{
12948 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12949}
433df2d4
DE
12950\f
12951/* Abbreviation tables.
3019eac3 12952
433df2d4 12953 In DWARF version 2, the description of the debugging information is
c906108c
SS
12954 stored in a separate .debug_abbrev section. Before we read any
12955 dies from a section we read in all abbreviations and install them
433df2d4
DE
12956 in a hash table. */
12957
12958/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12959
12960static struct abbrev_info *
12961abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12962{
12963 struct abbrev_info *abbrev;
12964
12965 abbrev = (struct abbrev_info *)
12966 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12967 memset (abbrev, 0, sizeof (struct abbrev_info));
12968 return abbrev;
12969}
12970
12971/* Add an abbreviation to the table. */
c906108c
SS
12972
12973static void
433df2d4
DE
12974abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12975 unsigned int abbrev_number,
12976 struct abbrev_info *abbrev)
12977{
12978 unsigned int hash_number;
12979
12980 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12981 abbrev->next = abbrev_table->abbrevs[hash_number];
12982 abbrev_table->abbrevs[hash_number] = abbrev;
12983}
dee91e82 12984
433df2d4
DE
12985/* Look up an abbrev in the table.
12986 Returns NULL if the abbrev is not found. */
12987
12988static struct abbrev_info *
12989abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12990 unsigned int abbrev_number)
c906108c 12991{
433df2d4
DE
12992 unsigned int hash_number;
12993 struct abbrev_info *abbrev;
12994
12995 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12996 abbrev = abbrev_table->abbrevs[hash_number];
12997
12998 while (abbrev)
12999 {
13000 if (abbrev->number == abbrev_number)
13001 return abbrev;
13002 abbrev = abbrev->next;
13003 }
13004 return NULL;
13005}
13006
13007/* Read in an abbrev table. */
13008
13009static struct abbrev_table *
13010abbrev_table_read_table (struct dwarf2_section_info *section,
13011 sect_offset offset)
13012{
13013 struct objfile *objfile = dwarf2_per_objfile->objfile;
13014 bfd *abfd = section->asection->owner;
13015 struct abbrev_table *abbrev_table;
fe1b8b76 13016 gdb_byte *abbrev_ptr;
c906108c
SS
13017 struct abbrev_info *cur_abbrev;
13018 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13019 unsigned int abbrev_form;
f3dd6933
DJ
13020 struct attr_abbrev *cur_attrs;
13021 unsigned int allocated_attrs;
c906108c 13022
433df2d4 13023 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13024 abbrev_table->offset = offset;
433df2d4
DE
13025 obstack_init (&abbrev_table->abbrev_obstack);
13026 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13027 (ABBREV_HASH_SIZE
13028 * sizeof (struct abbrev_info *)));
13029 memset (abbrev_table->abbrevs, 0,
13030 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13031
433df2d4
DE
13032 dwarf2_read_section (objfile, section);
13033 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13034 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13035 abbrev_ptr += bytes_read;
13036
f3dd6933
DJ
13037 allocated_attrs = ATTR_ALLOC_CHUNK;
13038 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13039
0963b4bd 13040 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13041 while (abbrev_number)
13042 {
433df2d4 13043 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13044
13045 /* read in abbrev header */
13046 cur_abbrev->number = abbrev_number;
13047 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13048 abbrev_ptr += bytes_read;
13049 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13050 abbrev_ptr += 1;
13051
13052 /* now read in declarations */
13053 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13054 abbrev_ptr += bytes_read;
13055 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13056 abbrev_ptr += bytes_read;
13057 while (abbrev_name)
13058 {
f3dd6933 13059 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13060 {
f3dd6933
DJ
13061 allocated_attrs += ATTR_ALLOC_CHUNK;
13062 cur_attrs
13063 = xrealloc (cur_attrs, (allocated_attrs
13064 * sizeof (struct attr_abbrev)));
c906108c 13065 }
ae038cb0 13066
f3dd6933
DJ
13067 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13068 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13069 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13070 abbrev_ptr += bytes_read;
13071 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13072 abbrev_ptr += bytes_read;
13073 }
13074
433df2d4 13075 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13076 (cur_abbrev->num_attrs
13077 * sizeof (struct attr_abbrev)));
13078 memcpy (cur_abbrev->attrs, cur_attrs,
13079 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13080
433df2d4 13081 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13082
13083 /* Get next abbreviation.
13084 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13085 always properly terminated with an abbrev number of 0.
13086 Exit loop if we encounter an abbreviation which we have
13087 already read (which means we are about to read the abbreviations
13088 for the next compile unit) or if the end of the abbreviation
13089 table is reached. */
433df2d4 13090 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13091 break;
13092 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13093 abbrev_ptr += bytes_read;
433df2d4 13094 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13095 break;
13096 }
f3dd6933
DJ
13097
13098 xfree (cur_attrs);
433df2d4 13099 return abbrev_table;
c906108c
SS
13100}
13101
433df2d4 13102/* Free the resources held by ABBREV_TABLE. */
c906108c 13103
c906108c 13104static void
433df2d4 13105abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13106{
433df2d4
DE
13107 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13108 xfree (abbrev_table);
c906108c
SS
13109}
13110
f4dc4d17
DE
13111/* Same as abbrev_table_free but as a cleanup.
13112 We pass in a pointer to the pointer to the table so that we can
13113 set the pointer to NULL when we're done. It also simplifies
13114 build_type_unit_groups. */
13115
13116static void
13117abbrev_table_free_cleanup (void *table_ptr)
13118{
13119 struct abbrev_table **abbrev_table_ptr = table_ptr;
13120
13121 if (*abbrev_table_ptr != NULL)
13122 abbrev_table_free (*abbrev_table_ptr);
13123 *abbrev_table_ptr = NULL;
13124}
13125
433df2d4
DE
13126/* Read the abbrev table for CU from ABBREV_SECTION. */
13127
13128static void
13129dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13130 struct dwarf2_section_info *abbrev_section)
c906108c 13131{
433df2d4
DE
13132 cu->abbrev_table =
13133 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13134}
c906108c 13135
433df2d4 13136/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13137
433df2d4
DE
13138static void
13139dwarf2_free_abbrev_table (void *ptr_to_cu)
13140{
13141 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13142
433df2d4
DE
13143 abbrev_table_free (cu->abbrev_table);
13144 /* Set this to NULL so that we SEGV if we try to read it later,
13145 and also because free_comp_unit verifies this is NULL. */
13146 cu->abbrev_table = NULL;
13147}
13148\f
72bf9492
DJ
13149/* Returns nonzero if TAG represents a type that we might generate a partial
13150 symbol for. */
13151
13152static int
13153is_type_tag_for_partial (int tag)
13154{
13155 switch (tag)
13156 {
13157#if 0
13158 /* Some types that would be reasonable to generate partial symbols for,
13159 that we don't at present. */
13160 case DW_TAG_array_type:
13161 case DW_TAG_file_type:
13162 case DW_TAG_ptr_to_member_type:
13163 case DW_TAG_set_type:
13164 case DW_TAG_string_type:
13165 case DW_TAG_subroutine_type:
13166#endif
13167 case DW_TAG_base_type:
13168 case DW_TAG_class_type:
680b30c7 13169 case DW_TAG_interface_type:
72bf9492
DJ
13170 case DW_TAG_enumeration_type:
13171 case DW_TAG_structure_type:
13172 case DW_TAG_subrange_type:
13173 case DW_TAG_typedef:
13174 case DW_TAG_union_type:
13175 return 1;
13176 default:
13177 return 0;
13178 }
13179}
13180
13181/* Load all DIEs that are interesting for partial symbols into memory. */
13182
13183static struct partial_die_info *
dee91e82
DE
13184load_partial_dies (const struct die_reader_specs *reader,
13185 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13186{
dee91e82 13187 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13188 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13189 struct partial_die_info *part_die;
13190 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13191 struct abbrev_info *abbrev;
13192 unsigned int bytes_read;
5afb4e99 13193 unsigned int load_all = 0;
72bf9492
DJ
13194 int nesting_level = 1;
13195
13196 parent_die = NULL;
13197 last_die = NULL;
13198
7adf1e79
DE
13199 gdb_assert (cu->per_cu != NULL);
13200 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13201 load_all = 1;
13202
72bf9492
DJ
13203 cu->partial_dies
13204 = htab_create_alloc_ex (cu->header.length / 12,
13205 partial_die_hash,
13206 partial_die_eq,
13207 NULL,
13208 &cu->comp_unit_obstack,
13209 hashtab_obstack_allocate,
13210 dummy_obstack_deallocate);
13211
13212 part_die = obstack_alloc (&cu->comp_unit_obstack,
13213 sizeof (struct partial_die_info));
13214
13215 while (1)
13216 {
13217 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13218
13219 /* A NULL abbrev means the end of a series of children. */
13220 if (abbrev == NULL)
13221 {
13222 if (--nesting_level == 0)
13223 {
13224 /* PART_DIE was probably the last thing allocated on the
13225 comp_unit_obstack, so we could call obstack_free
13226 here. We don't do that because the waste is small,
13227 and will be cleaned up when we're done with this
13228 compilation unit. This way, we're also more robust
13229 against other users of the comp_unit_obstack. */
13230 return first_die;
13231 }
13232 info_ptr += bytes_read;
13233 last_die = parent_die;
13234 parent_die = parent_die->die_parent;
13235 continue;
13236 }
13237
98bfdba5
PA
13238 /* Check for template arguments. We never save these; if
13239 they're seen, we just mark the parent, and go on our way. */
13240 if (parent_die != NULL
13241 && cu->language == language_cplus
13242 && (abbrev->tag == DW_TAG_template_type_param
13243 || abbrev->tag == DW_TAG_template_value_param))
13244 {
13245 parent_die->has_template_arguments = 1;
13246
13247 if (!load_all)
13248 {
13249 /* We don't need a partial DIE for the template argument. */
dee91e82 13250 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13251 continue;
13252 }
13253 }
13254
0d99eb77 13255 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13256 Skip their other children. */
13257 if (!load_all
13258 && cu->language == language_cplus
13259 && parent_die != NULL
13260 && parent_die->tag == DW_TAG_subprogram)
13261 {
dee91e82 13262 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13263 continue;
13264 }
13265
5afb4e99
DJ
13266 /* Check whether this DIE is interesting enough to save. Normally
13267 we would not be interested in members here, but there may be
13268 later variables referencing them via DW_AT_specification (for
13269 static members). */
13270 if (!load_all
13271 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13272 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13273 && abbrev->tag != DW_TAG_enumerator
13274 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13275 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13276 && abbrev->tag != DW_TAG_variable
5afb4e99 13277 && abbrev->tag != DW_TAG_namespace
f55ee35c 13278 && abbrev->tag != DW_TAG_module
95554aad
TT
13279 && abbrev->tag != DW_TAG_member
13280 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13281 {
13282 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13283 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13284 continue;
13285 }
13286
dee91e82
DE
13287 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13288 info_ptr);
72bf9492
DJ
13289
13290 /* This two-pass algorithm for processing partial symbols has a
13291 high cost in cache pressure. Thus, handle some simple cases
13292 here which cover the majority of C partial symbols. DIEs
13293 which neither have specification tags in them, nor could have
13294 specification tags elsewhere pointing at them, can simply be
13295 processed and discarded.
13296
13297 This segment is also optional; scan_partial_symbols and
13298 add_partial_symbol will handle these DIEs if we chain
13299 them in normally. When compilers which do not emit large
13300 quantities of duplicate debug information are more common,
13301 this code can probably be removed. */
13302
13303 /* Any complete simple types at the top level (pretty much all
13304 of them, for a language without namespaces), can be processed
13305 directly. */
13306 if (parent_die == NULL
13307 && part_die->has_specification == 0
13308 && part_die->is_declaration == 0
d8228535 13309 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13310 || part_die->tag == DW_TAG_base_type
13311 || part_die->tag == DW_TAG_subrange_type))
13312 {
13313 if (building_psymtab && part_die->name != NULL)
04a679b8 13314 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13315 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13316 &objfile->static_psymbols,
13317 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13318 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13319 continue;
13320 }
13321
d8228535
JK
13322 /* The exception for DW_TAG_typedef with has_children above is
13323 a workaround of GCC PR debug/47510. In the case of this complaint
13324 type_name_no_tag_or_error will error on such types later.
13325
13326 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13327 it could not find the child DIEs referenced later, this is checked
13328 above. In correct DWARF DW_TAG_typedef should have no children. */
13329
13330 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13331 complaint (&symfile_complaints,
13332 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13333 "- DIE at 0x%x [in module %s]"),
b64f50a1 13334 part_die->offset.sect_off, objfile->name);
d8228535 13335
72bf9492
DJ
13336 /* If we're at the second level, and we're an enumerator, and
13337 our parent has no specification (meaning possibly lives in a
13338 namespace elsewhere), then we can add the partial symbol now
13339 instead of queueing it. */
13340 if (part_die->tag == DW_TAG_enumerator
13341 && parent_die != NULL
13342 && parent_die->die_parent == NULL
13343 && parent_die->tag == DW_TAG_enumeration_type
13344 && parent_die->has_specification == 0)
13345 {
13346 if (part_die->name == NULL)
3e43a32a
MS
13347 complaint (&symfile_complaints,
13348 _("malformed enumerator DIE ignored"));
72bf9492 13349 else if (building_psymtab)
04a679b8 13350 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13351 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13352 (cu->language == language_cplus
13353 || cu->language == language_java)
bb5ed363
DE
13354 ? &objfile->global_psymbols
13355 : &objfile->static_psymbols,
13356 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13357
dee91e82 13358 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13359 continue;
13360 }
13361
13362 /* We'll save this DIE so link it in. */
13363 part_die->die_parent = parent_die;
13364 part_die->die_sibling = NULL;
13365 part_die->die_child = NULL;
13366
13367 if (last_die && last_die == parent_die)
13368 last_die->die_child = part_die;
13369 else if (last_die)
13370 last_die->die_sibling = part_die;
13371
13372 last_die = part_die;
13373
13374 if (first_die == NULL)
13375 first_die = part_die;
13376
13377 /* Maybe add the DIE to the hash table. Not all DIEs that we
13378 find interesting need to be in the hash table, because we
13379 also have the parent/sibling/child chains; only those that we
13380 might refer to by offset later during partial symbol reading.
13381
13382 For now this means things that might have be the target of a
13383 DW_AT_specification, DW_AT_abstract_origin, or
13384 DW_AT_extension. DW_AT_extension will refer only to
13385 namespaces; DW_AT_abstract_origin refers to functions (and
13386 many things under the function DIE, but we do not recurse
13387 into function DIEs during partial symbol reading) and
13388 possibly variables as well; DW_AT_specification refers to
13389 declarations. Declarations ought to have the DW_AT_declaration
13390 flag. It happens that GCC forgets to put it in sometimes, but
13391 only for functions, not for types.
13392
13393 Adding more things than necessary to the hash table is harmless
13394 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13395 wasted time in find_partial_die, when we reread the compilation
13396 unit with load_all_dies set. */
72bf9492 13397
5afb4e99 13398 if (load_all
72929c62 13399 || abbrev->tag == DW_TAG_constant
5afb4e99 13400 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13401 || abbrev->tag == DW_TAG_variable
13402 || abbrev->tag == DW_TAG_namespace
13403 || part_die->is_declaration)
13404 {
13405 void **slot;
13406
13407 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13408 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13409 *slot = part_die;
13410 }
13411
13412 part_die = obstack_alloc (&cu->comp_unit_obstack,
13413 sizeof (struct partial_die_info));
13414
13415 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13416 we have no reason to follow the children of structures; for other
98bfdba5
PA
13417 languages we have to, so that we can get at method physnames
13418 to infer fully qualified class names, for DW_AT_specification,
13419 and for C++ template arguments. For C++, we also look one level
13420 inside functions to find template arguments (if the name of the
13421 function does not already contain the template arguments).
bc30ff58
JB
13422
13423 For Ada, we need to scan the children of subprograms and lexical
13424 blocks as well because Ada allows the definition of nested
13425 entities that could be interesting for the debugger, such as
13426 nested subprograms for instance. */
72bf9492 13427 if (last_die->has_children
5afb4e99
DJ
13428 && (load_all
13429 || last_die->tag == DW_TAG_namespace
f55ee35c 13430 || last_die->tag == DW_TAG_module
72bf9492 13431 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13432 || (cu->language == language_cplus
13433 && last_die->tag == DW_TAG_subprogram
13434 && (last_die->name == NULL
13435 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13436 || (cu->language != language_c
13437 && (last_die->tag == DW_TAG_class_type
680b30c7 13438 || last_die->tag == DW_TAG_interface_type
72bf9492 13439 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13440 || last_die->tag == DW_TAG_union_type))
13441 || (cu->language == language_ada
13442 && (last_die->tag == DW_TAG_subprogram
13443 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13444 {
13445 nesting_level++;
13446 parent_die = last_die;
13447 continue;
13448 }
13449
13450 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13451 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13452
13453 /* Back to the top, do it again. */
13454 }
13455}
13456
c906108c
SS
13457/* Read a minimal amount of information into the minimal die structure. */
13458
fe1b8b76 13459static gdb_byte *
dee91e82
DE
13460read_partial_die (const struct die_reader_specs *reader,
13461 struct partial_die_info *part_die,
13462 struct abbrev_info *abbrev, unsigned int abbrev_len,
13463 gdb_byte *info_ptr)
c906108c 13464{
dee91e82 13465 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13466 struct objfile *objfile = cu->objfile;
dee91e82 13467 gdb_byte *buffer = reader->buffer;
fa238c03 13468 unsigned int i;
c906108c 13469 struct attribute attr;
c5aa993b 13470 int has_low_pc_attr = 0;
c906108c 13471 int has_high_pc_attr = 0;
91da1414 13472 int high_pc_relative = 0;
c906108c 13473
72bf9492 13474 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13475
b64f50a1 13476 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13477
13478 info_ptr += abbrev_len;
13479
13480 if (abbrev == NULL)
13481 return info_ptr;
13482
c906108c
SS
13483 part_die->tag = abbrev->tag;
13484 part_die->has_children = abbrev->has_children;
c906108c
SS
13485
13486 for (i = 0; i < abbrev->num_attrs; ++i)
13487 {
dee91e82 13488 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13489
13490 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13491 partial symbol table. */
c906108c
SS
13492 switch (attr.name)
13493 {
13494 case DW_AT_name:
71c25dea
TT
13495 switch (part_die->tag)
13496 {
13497 case DW_TAG_compile_unit:
95554aad 13498 case DW_TAG_partial_unit:
348e048f 13499 case DW_TAG_type_unit:
71c25dea
TT
13500 /* Compilation units have a DW_AT_name that is a filename, not
13501 a source language identifier. */
13502 case DW_TAG_enumeration_type:
13503 case DW_TAG_enumerator:
13504 /* These tags always have simple identifiers already; no need
13505 to canonicalize them. */
13506 part_die->name = DW_STRING (&attr);
13507 break;
13508 default:
13509 part_die->name
13510 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13511 &objfile->objfile_obstack);
71c25dea
TT
13512 break;
13513 }
c906108c 13514 break;
31ef98ae 13515 case DW_AT_linkage_name:
c906108c 13516 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13517 /* Note that both forms of linkage name might appear. We
13518 assume they will be the same, and we only store the last
13519 one we see. */
94af9270
KS
13520 if (cu->language == language_ada)
13521 part_die->name = DW_STRING (&attr);
abc72ce4 13522 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13523 break;
13524 case DW_AT_low_pc:
13525 has_low_pc_attr = 1;
13526 part_die->lowpc = DW_ADDR (&attr);
13527 break;
13528 case DW_AT_high_pc:
13529 has_high_pc_attr = 1;
3019eac3
DE
13530 if (attr.form == DW_FORM_addr
13531 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13532 part_die->highpc = DW_ADDR (&attr);
13533 else
13534 {
13535 high_pc_relative = 1;
13536 part_die->highpc = DW_UNSND (&attr);
13537 }
c906108c
SS
13538 break;
13539 case DW_AT_location:
0963b4bd 13540 /* Support the .debug_loc offsets. */
8e19ed76
PS
13541 if (attr_form_is_block (&attr))
13542 {
95554aad 13543 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13544 }
3690dd37 13545 else if (attr_form_is_section_offset (&attr))
8e19ed76 13546 {
4d3c2250 13547 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13548 }
13549 else
13550 {
4d3c2250
KB
13551 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13552 "partial symbol information");
8e19ed76 13553 }
c906108c 13554 break;
c906108c
SS
13555 case DW_AT_external:
13556 part_die->is_external = DW_UNSND (&attr);
13557 break;
13558 case DW_AT_declaration:
13559 part_die->is_declaration = DW_UNSND (&attr);
13560 break;
13561 case DW_AT_type:
13562 part_die->has_type = 1;
13563 break;
13564 case DW_AT_abstract_origin:
13565 case DW_AT_specification:
72bf9492
DJ
13566 case DW_AT_extension:
13567 part_die->has_specification = 1;
c764a876 13568 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13569 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13570 || cu->per_cu->is_dwz);
c906108c
SS
13571 break;
13572 case DW_AT_sibling:
13573 /* Ignore absolute siblings, they might point outside of
13574 the current compile unit. */
13575 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13576 complaint (&symfile_complaints,
13577 _("ignoring absolute DW_AT_sibling"));
c906108c 13578 else
b64f50a1 13579 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13580 break;
fa4028e9
JB
13581 case DW_AT_byte_size:
13582 part_die->has_byte_size = 1;
13583 break;
68511cec
CES
13584 case DW_AT_calling_convention:
13585 /* DWARF doesn't provide a way to identify a program's source-level
13586 entry point. DW_AT_calling_convention attributes are only meant
13587 to describe functions' calling conventions.
13588
13589 However, because it's a necessary piece of information in
13590 Fortran, and because DW_CC_program is the only piece of debugging
13591 information whose definition refers to a 'main program' at all,
13592 several compilers have begun marking Fortran main programs with
13593 DW_CC_program --- even when those functions use the standard
13594 calling conventions.
13595
13596 So until DWARF specifies a way to provide this information and
13597 compilers pick up the new representation, we'll support this
13598 practice. */
13599 if (DW_UNSND (&attr) == DW_CC_program
13600 && cu->language == language_fortran)
01f8c46d
JK
13601 {
13602 set_main_name (part_die->name);
13603
13604 /* As this DIE has a static linkage the name would be difficult
13605 to look up later. */
13606 language_of_main = language_fortran;
13607 }
68511cec 13608 break;
481860b3
GB
13609 case DW_AT_inline:
13610 if (DW_UNSND (&attr) == DW_INL_inlined
13611 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13612 part_die->may_be_inlined = 1;
13613 break;
95554aad
TT
13614
13615 case DW_AT_import:
13616 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13617 {
13618 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13619 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13620 || cu->per_cu->is_dwz);
13621 }
95554aad
TT
13622 break;
13623
c906108c
SS
13624 default:
13625 break;
13626 }
13627 }
13628
91da1414
MW
13629 if (high_pc_relative)
13630 part_die->highpc += part_die->lowpc;
13631
9373cf26
JK
13632 if (has_low_pc_attr && has_high_pc_attr)
13633 {
13634 /* When using the GNU linker, .gnu.linkonce. sections are used to
13635 eliminate duplicate copies of functions and vtables and such.
13636 The linker will arbitrarily choose one and discard the others.
13637 The AT_*_pc values for such functions refer to local labels in
13638 these sections. If the section from that file was discarded, the
13639 labels are not in the output, so the relocs get a value of 0.
13640 If this is a discarded function, mark the pc bounds as invalid,
13641 so that GDB will ignore it. */
13642 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13643 {
bb5ed363 13644 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13645
13646 complaint (&symfile_complaints,
13647 _("DW_AT_low_pc %s is zero "
13648 "for DIE at 0x%x [in module %s]"),
13649 paddress (gdbarch, part_die->lowpc),
b64f50a1 13650 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13651 }
13652 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13653 else if (part_die->lowpc >= part_die->highpc)
13654 {
bb5ed363 13655 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13656
13657 complaint (&symfile_complaints,
13658 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13659 "for DIE at 0x%x [in module %s]"),
13660 paddress (gdbarch, part_die->lowpc),
13661 paddress (gdbarch, part_die->highpc),
b64f50a1 13662 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13663 }
13664 else
13665 part_die->has_pc_info = 1;
13666 }
85cbf3d3 13667
c906108c
SS
13668 return info_ptr;
13669}
13670
72bf9492
DJ
13671/* Find a cached partial DIE at OFFSET in CU. */
13672
13673static struct partial_die_info *
b64f50a1 13674find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13675{
13676 struct partial_die_info *lookup_die = NULL;
13677 struct partial_die_info part_die;
13678
13679 part_die.offset = offset;
b64f50a1
JK
13680 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13681 offset.sect_off);
72bf9492 13682
72bf9492
DJ
13683 return lookup_die;
13684}
13685
348e048f
DE
13686/* Find a partial DIE at OFFSET, which may or may not be in CU,
13687 except in the case of .debug_types DIEs which do not reference
13688 outside their CU (they do however referencing other types via
55f1336d 13689 DW_FORM_ref_sig8). */
72bf9492
DJ
13690
13691static struct partial_die_info *
36586728 13692find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13693{
bb5ed363 13694 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13695 struct dwarf2_per_cu_data *per_cu = NULL;
13696 struct partial_die_info *pd = NULL;
72bf9492 13697
36586728
TT
13698 if (offset_in_dwz == cu->per_cu->is_dwz
13699 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13700 {
13701 pd = find_partial_die_in_comp_unit (offset, cu);
13702 if (pd != NULL)
13703 return pd;
0d99eb77
DE
13704 /* We missed recording what we needed.
13705 Load all dies and try again. */
13706 per_cu = cu->per_cu;
5afb4e99 13707 }
0d99eb77
DE
13708 else
13709 {
13710 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13711 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13712 {
13713 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13714 " external reference to offset 0x%lx [in module %s].\n"),
13715 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13716 bfd_get_filename (objfile->obfd));
13717 }
36586728
TT
13718 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13719 objfile);
72bf9492 13720
0d99eb77
DE
13721 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13722 load_partial_comp_unit (per_cu);
ae038cb0 13723
0d99eb77
DE
13724 per_cu->cu->last_used = 0;
13725 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13726 }
5afb4e99 13727
dee91e82
DE
13728 /* If we didn't find it, and not all dies have been loaded,
13729 load them all and try again. */
13730
5afb4e99
DJ
13731 if (pd == NULL && per_cu->load_all_dies == 0)
13732 {
5afb4e99 13733 per_cu->load_all_dies = 1;
fd820528
DE
13734
13735 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13736 THIS_CU->cu may already be in use. So we can't just free it and
13737 replace its DIEs with the ones we read in. Instead, we leave those
13738 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13739 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13740 set. */
dee91e82 13741 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13742
13743 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13744 }
13745
13746 if (pd == NULL)
13747 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13748 _("could not find partial DIE 0x%x "
13749 "in cache [from module %s]\n"),
b64f50a1 13750 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13751 return pd;
72bf9492
DJ
13752}
13753
abc72ce4
DE
13754/* See if we can figure out if the class lives in a namespace. We do
13755 this by looking for a member function; its demangled name will
13756 contain namespace info, if there is any. */
13757
13758static void
13759guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13760 struct dwarf2_cu *cu)
13761{
13762 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13763 what template types look like, because the demangler
13764 frequently doesn't give the same name as the debug info. We
13765 could fix this by only using the demangled name to get the
13766 prefix (but see comment in read_structure_type). */
13767
13768 struct partial_die_info *real_pdi;
13769 struct partial_die_info *child_pdi;
13770
13771 /* If this DIE (this DIE's specification, if any) has a parent, then
13772 we should not do this. We'll prepend the parent's fully qualified
13773 name when we create the partial symbol. */
13774
13775 real_pdi = struct_pdi;
13776 while (real_pdi->has_specification)
36586728
TT
13777 real_pdi = find_partial_die (real_pdi->spec_offset,
13778 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13779
13780 if (real_pdi->die_parent != NULL)
13781 return;
13782
13783 for (child_pdi = struct_pdi->die_child;
13784 child_pdi != NULL;
13785 child_pdi = child_pdi->die_sibling)
13786 {
13787 if (child_pdi->tag == DW_TAG_subprogram
13788 && child_pdi->linkage_name != NULL)
13789 {
13790 char *actual_class_name
13791 = language_class_name_from_physname (cu->language_defn,
13792 child_pdi->linkage_name);
13793 if (actual_class_name != NULL)
13794 {
13795 struct_pdi->name
13796 = obsavestring (actual_class_name,
13797 strlen (actual_class_name),
13798 &cu->objfile->objfile_obstack);
13799 xfree (actual_class_name);
13800 }
13801 break;
13802 }
13803 }
13804}
13805
72bf9492
DJ
13806/* Adjust PART_DIE before generating a symbol for it. This function
13807 may set the is_external flag or change the DIE's name. */
13808
13809static void
13810fixup_partial_die (struct partial_die_info *part_die,
13811 struct dwarf2_cu *cu)
13812{
abc72ce4
DE
13813 /* Once we've fixed up a die, there's no point in doing so again.
13814 This also avoids a memory leak if we were to call
13815 guess_partial_die_structure_name multiple times. */
13816 if (part_die->fixup_called)
13817 return;
13818
72bf9492
DJ
13819 /* If we found a reference attribute and the DIE has no name, try
13820 to find a name in the referred to DIE. */
13821
13822 if (part_die->name == NULL && part_die->has_specification)
13823 {
13824 struct partial_die_info *spec_die;
72bf9492 13825
36586728
TT
13826 spec_die = find_partial_die (part_die->spec_offset,
13827 part_die->spec_is_dwz, cu);
72bf9492 13828
10b3939b 13829 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13830
13831 if (spec_die->name)
13832 {
13833 part_die->name = spec_die->name;
13834
13835 /* Copy DW_AT_external attribute if it is set. */
13836 if (spec_die->is_external)
13837 part_die->is_external = spec_die->is_external;
13838 }
13839 }
13840
13841 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13842
13843 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13844 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13845
abc72ce4
DE
13846 /* If there is no parent die to provide a namespace, and there are
13847 children, see if we can determine the namespace from their linkage
122d1940 13848 name. */
abc72ce4 13849 if (cu->language == language_cplus
8b70b953 13850 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13851 && part_die->die_parent == NULL
13852 && part_die->has_children
13853 && (part_die->tag == DW_TAG_class_type
13854 || part_die->tag == DW_TAG_structure_type
13855 || part_die->tag == DW_TAG_union_type))
13856 guess_partial_die_structure_name (part_die, cu);
13857
53832f31
TT
13858 /* GCC might emit a nameless struct or union that has a linkage
13859 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13860 if (part_die->name == NULL
96408a79
SA
13861 && (part_die->tag == DW_TAG_class_type
13862 || part_die->tag == DW_TAG_interface_type
13863 || part_die->tag == DW_TAG_structure_type
13864 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13865 && part_die->linkage_name != NULL)
13866 {
13867 char *demangled;
13868
13869 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13870 if (demangled)
13871 {
96408a79
SA
13872 const char *base;
13873
13874 /* Strip any leading namespaces/classes, keep only the base name.
13875 DW_AT_name for named DIEs does not contain the prefixes. */
13876 base = strrchr (demangled, ':');
13877 if (base && base > demangled && base[-1] == ':')
13878 base++;
13879 else
13880 base = demangled;
13881
13882 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13883 &cu->objfile->objfile_obstack);
13884 xfree (demangled);
13885 }
13886 }
13887
abc72ce4 13888 part_die->fixup_called = 1;
72bf9492
DJ
13889}
13890
a8329558 13891/* Read an attribute value described by an attribute form. */
c906108c 13892
fe1b8b76 13893static gdb_byte *
dee91e82
DE
13894read_attribute_value (const struct die_reader_specs *reader,
13895 struct attribute *attr, unsigned form,
13896 gdb_byte *info_ptr)
c906108c 13897{
dee91e82
DE
13898 struct dwarf2_cu *cu = reader->cu;
13899 bfd *abfd = reader->abfd;
e7c27a73 13900 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13901 unsigned int bytes_read;
13902 struct dwarf_block *blk;
13903
a8329558
KW
13904 attr->form = form;
13905 switch (form)
c906108c 13906 {
c906108c 13907 case DW_FORM_ref_addr:
ae411497 13908 if (cu->header.version == 2)
4568ecf9 13909 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13910 else
4568ecf9
DE
13911 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13912 &cu->header, &bytes_read);
ae411497
TT
13913 info_ptr += bytes_read;
13914 break;
36586728
TT
13915 case DW_FORM_GNU_ref_alt:
13916 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13917 info_ptr += bytes_read;
13918 break;
ae411497 13919 case DW_FORM_addr:
e7c27a73 13920 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13921 info_ptr += bytes_read;
c906108c
SS
13922 break;
13923 case DW_FORM_block2:
7b5a2f43 13924 blk = dwarf_alloc_block (cu);
c906108c
SS
13925 blk->size = read_2_bytes (abfd, info_ptr);
13926 info_ptr += 2;
13927 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13928 info_ptr += blk->size;
13929 DW_BLOCK (attr) = blk;
13930 break;
13931 case DW_FORM_block4:
7b5a2f43 13932 blk = dwarf_alloc_block (cu);
c906108c
SS
13933 blk->size = read_4_bytes (abfd, info_ptr);
13934 info_ptr += 4;
13935 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13936 info_ptr += blk->size;
13937 DW_BLOCK (attr) = blk;
13938 break;
13939 case DW_FORM_data2:
13940 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13941 info_ptr += 2;
13942 break;
13943 case DW_FORM_data4:
13944 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13945 info_ptr += 4;
13946 break;
13947 case DW_FORM_data8:
13948 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13949 info_ptr += 8;
13950 break;
2dc7f7b3
TT
13951 case DW_FORM_sec_offset:
13952 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13953 info_ptr += bytes_read;
13954 break;
c906108c 13955 case DW_FORM_string:
9b1c24c8 13956 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13957 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13958 info_ptr += bytes_read;
13959 break;
4bdf3d34 13960 case DW_FORM_strp:
36586728
TT
13961 if (!cu->per_cu->is_dwz)
13962 {
13963 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13964 &bytes_read);
13965 DW_STRING_IS_CANONICAL (attr) = 0;
13966 info_ptr += bytes_read;
13967 break;
13968 }
13969 /* FALLTHROUGH */
13970 case DW_FORM_GNU_strp_alt:
13971 {
13972 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13973 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13974 &bytes_read);
13975
13976 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13977 DW_STRING_IS_CANONICAL (attr) = 0;
13978 info_ptr += bytes_read;
13979 }
4bdf3d34 13980 break;
2dc7f7b3 13981 case DW_FORM_exprloc:
c906108c 13982 case DW_FORM_block:
7b5a2f43 13983 blk = dwarf_alloc_block (cu);
c906108c
SS
13984 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13985 info_ptr += bytes_read;
13986 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13987 info_ptr += blk->size;
13988 DW_BLOCK (attr) = blk;
13989 break;
13990 case DW_FORM_block1:
7b5a2f43 13991 blk = dwarf_alloc_block (cu);
c906108c
SS
13992 blk->size = read_1_byte (abfd, info_ptr);
13993 info_ptr += 1;
13994 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13995 info_ptr += blk->size;
13996 DW_BLOCK (attr) = blk;
13997 break;
13998 case DW_FORM_data1:
13999 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14000 info_ptr += 1;
14001 break;
14002 case DW_FORM_flag:
14003 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14004 info_ptr += 1;
14005 break;
2dc7f7b3
TT
14006 case DW_FORM_flag_present:
14007 DW_UNSND (attr) = 1;
14008 break;
c906108c
SS
14009 case DW_FORM_sdata:
14010 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14011 info_ptr += bytes_read;
14012 break;
14013 case DW_FORM_udata:
14014 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14015 info_ptr += bytes_read;
14016 break;
14017 case DW_FORM_ref1:
4568ecf9
DE
14018 DW_UNSND (attr) = (cu->header.offset.sect_off
14019 + read_1_byte (abfd, info_ptr));
c906108c
SS
14020 info_ptr += 1;
14021 break;
14022 case DW_FORM_ref2:
4568ecf9
DE
14023 DW_UNSND (attr) = (cu->header.offset.sect_off
14024 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14025 info_ptr += 2;
14026 break;
14027 case DW_FORM_ref4:
4568ecf9
DE
14028 DW_UNSND (attr) = (cu->header.offset.sect_off
14029 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14030 info_ptr += 4;
14031 break;
613e1657 14032 case DW_FORM_ref8:
4568ecf9
DE
14033 DW_UNSND (attr) = (cu->header.offset.sect_off
14034 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14035 info_ptr += 8;
14036 break;
55f1336d 14037 case DW_FORM_ref_sig8:
348e048f
DE
14038 /* Convert the signature to something we can record in DW_UNSND
14039 for later lookup.
14040 NOTE: This is NULL if the type wasn't found. */
14041 DW_SIGNATURED_TYPE (attr) =
e319fa28 14042 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14043 info_ptr += 8;
14044 break;
c906108c 14045 case DW_FORM_ref_udata:
4568ecf9
DE
14046 DW_UNSND (attr) = (cu->header.offset.sect_off
14047 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14048 info_ptr += bytes_read;
14049 break;
c906108c 14050 case DW_FORM_indirect:
a8329558
KW
14051 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14052 info_ptr += bytes_read;
dee91e82 14053 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14054 break;
3019eac3
DE
14055 case DW_FORM_GNU_addr_index:
14056 if (reader->dwo_file == NULL)
14057 {
14058 /* For now flag a hard error.
14059 Later we can turn this into a complaint. */
14060 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14061 dwarf_form_name (form),
14062 bfd_get_filename (abfd));
14063 }
14064 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14065 info_ptr += bytes_read;
14066 break;
14067 case DW_FORM_GNU_str_index:
14068 if (reader->dwo_file == NULL)
14069 {
14070 /* For now flag a hard error.
14071 Later we can turn this into a complaint if warranted. */
14072 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14073 dwarf_form_name (form),
14074 bfd_get_filename (abfd));
14075 }
14076 {
14077 ULONGEST str_index =
14078 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14079
14080 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14081 DW_STRING_IS_CANONICAL (attr) = 0;
14082 info_ptr += bytes_read;
14083 }
14084 break;
c906108c 14085 default:
8a3fe4f8 14086 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14087 dwarf_form_name (form),
14088 bfd_get_filename (abfd));
c906108c 14089 }
28e94949 14090
36586728
TT
14091 /* Super hack. */
14092 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14093 attr->form = DW_FORM_GNU_ref_alt;
14094
28e94949
JB
14095 /* We have seen instances where the compiler tried to emit a byte
14096 size attribute of -1 which ended up being encoded as an unsigned
14097 0xffffffff. Although 0xffffffff is technically a valid size value,
14098 an object of this size seems pretty unlikely so we can relatively
14099 safely treat these cases as if the size attribute was invalid and
14100 treat them as zero by default. */
14101 if (attr->name == DW_AT_byte_size
14102 && form == DW_FORM_data4
14103 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14104 {
14105 complaint
14106 (&symfile_complaints,
43bbcdc2
PH
14107 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14108 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14109 DW_UNSND (attr) = 0;
14110 }
28e94949 14111
c906108c
SS
14112 return info_ptr;
14113}
14114
a8329558
KW
14115/* Read an attribute described by an abbreviated attribute. */
14116
fe1b8b76 14117static gdb_byte *
dee91e82
DE
14118read_attribute (const struct die_reader_specs *reader,
14119 struct attribute *attr, struct attr_abbrev *abbrev,
14120 gdb_byte *info_ptr)
a8329558
KW
14121{
14122 attr->name = abbrev->name;
dee91e82 14123 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14124}
14125
0963b4bd 14126/* Read dwarf information from a buffer. */
c906108c
SS
14127
14128static unsigned int
a1855c1d 14129read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14130{
fe1b8b76 14131 return bfd_get_8 (abfd, buf);
c906108c
SS
14132}
14133
14134static int
a1855c1d 14135read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14136{
fe1b8b76 14137 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14138}
14139
14140static unsigned int
a1855c1d 14141read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14142{
fe1b8b76 14143 return bfd_get_16 (abfd, buf);
c906108c
SS
14144}
14145
21ae7a4d 14146static int
a1855c1d 14147read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14148{
14149 return bfd_get_signed_16 (abfd, buf);
14150}
14151
c906108c 14152static unsigned int
a1855c1d 14153read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14154{
fe1b8b76 14155 return bfd_get_32 (abfd, buf);
c906108c
SS
14156}
14157
21ae7a4d 14158static int
a1855c1d 14159read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14160{
14161 return bfd_get_signed_32 (abfd, buf);
14162}
14163
93311388 14164static ULONGEST
a1855c1d 14165read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14166{
fe1b8b76 14167 return bfd_get_64 (abfd, buf);
c906108c
SS
14168}
14169
14170static CORE_ADDR
fe1b8b76 14171read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14172 unsigned int *bytes_read)
c906108c 14173{
e7c27a73 14174 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14175 CORE_ADDR retval = 0;
14176
107d2387 14177 if (cu_header->signed_addr_p)
c906108c 14178 {
107d2387
AC
14179 switch (cu_header->addr_size)
14180 {
14181 case 2:
fe1b8b76 14182 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14183 break;
14184 case 4:
fe1b8b76 14185 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14186 break;
14187 case 8:
fe1b8b76 14188 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14189 break;
14190 default:
8e65ff28 14191 internal_error (__FILE__, __LINE__,
e2e0b3e5 14192 _("read_address: bad switch, signed [in module %s]"),
659b0389 14193 bfd_get_filename (abfd));
107d2387
AC
14194 }
14195 }
14196 else
14197 {
14198 switch (cu_header->addr_size)
14199 {
14200 case 2:
fe1b8b76 14201 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14202 break;
14203 case 4:
fe1b8b76 14204 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14205 break;
14206 case 8:
fe1b8b76 14207 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14208 break;
14209 default:
8e65ff28 14210 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14211 _("read_address: bad switch, "
14212 "unsigned [in module %s]"),
659b0389 14213 bfd_get_filename (abfd));
107d2387 14214 }
c906108c 14215 }
64367e0a 14216
107d2387
AC
14217 *bytes_read = cu_header->addr_size;
14218 return retval;
c906108c
SS
14219}
14220
f7ef9339 14221/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14222 specification allows the initial length to take up either 4 bytes
14223 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14224 bytes describe the length and all offsets will be 8 bytes in length
14225 instead of 4.
14226
f7ef9339
KB
14227 An older, non-standard 64-bit format is also handled by this
14228 function. The older format in question stores the initial length
14229 as an 8-byte quantity without an escape value. Lengths greater
14230 than 2^32 aren't very common which means that the initial 4 bytes
14231 is almost always zero. Since a length value of zero doesn't make
14232 sense for the 32-bit format, this initial zero can be considered to
14233 be an escape value which indicates the presence of the older 64-bit
14234 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14235 greater than 4GB. If it becomes necessary to handle lengths
14236 somewhat larger than 4GB, we could allow other small values (such
14237 as the non-sensical values of 1, 2, and 3) to also be used as
14238 escape values indicating the presence of the old format.
f7ef9339 14239
917c78fc
MK
14240 The value returned via bytes_read should be used to increment the
14241 relevant pointer after calling read_initial_length().
c764a876 14242
613e1657
KB
14243 [ Note: read_initial_length() and read_offset() are based on the
14244 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14245 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14246 from:
14247
f7ef9339 14248 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14249
613e1657
KB
14250 This document is only a draft and is subject to change. (So beware.)
14251
f7ef9339 14252 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14253 determined empirically by examining 64-bit ELF files produced by
14254 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14255
14256 - Kevin, July 16, 2002
613e1657
KB
14257 ] */
14258
14259static LONGEST
c764a876 14260read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14261{
fe1b8b76 14262 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14263
dd373385 14264 if (length == 0xffffffff)
613e1657 14265 {
fe1b8b76 14266 length = bfd_get_64 (abfd, buf + 4);
613e1657 14267 *bytes_read = 12;
613e1657 14268 }
dd373385 14269 else if (length == 0)
f7ef9339 14270 {
dd373385 14271 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14272 length = bfd_get_64 (abfd, buf);
f7ef9339 14273 *bytes_read = 8;
f7ef9339 14274 }
613e1657
KB
14275 else
14276 {
14277 *bytes_read = 4;
613e1657
KB
14278 }
14279
c764a876
DE
14280 return length;
14281}
dd373385 14282
c764a876
DE
14283/* Cover function for read_initial_length.
14284 Returns the length of the object at BUF, and stores the size of the
14285 initial length in *BYTES_READ and stores the size that offsets will be in
14286 *OFFSET_SIZE.
14287 If the initial length size is not equivalent to that specified in
14288 CU_HEADER then issue a complaint.
14289 This is useful when reading non-comp-unit headers. */
dd373385 14290
c764a876
DE
14291static LONGEST
14292read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14293 const struct comp_unit_head *cu_header,
14294 unsigned int *bytes_read,
14295 unsigned int *offset_size)
14296{
14297 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14298
14299 gdb_assert (cu_header->initial_length_size == 4
14300 || cu_header->initial_length_size == 8
14301 || cu_header->initial_length_size == 12);
14302
14303 if (cu_header->initial_length_size != *bytes_read)
14304 complaint (&symfile_complaints,
14305 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14306
c764a876 14307 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14308 return length;
613e1657
KB
14309}
14310
14311/* Read an offset from the data stream. The size of the offset is
917c78fc 14312 given by cu_header->offset_size. */
613e1657
KB
14313
14314static LONGEST
fe1b8b76 14315read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14316 unsigned int *bytes_read)
c764a876
DE
14317{
14318 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14319
c764a876
DE
14320 *bytes_read = cu_header->offset_size;
14321 return offset;
14322}
14323
14324/* Read an offset from the data stream. */
14325
14326static LONGEST
14327read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14328{
14329 LONGEST retval = 0;
14330
c764a876 14331 switch (offset_size)
613e1657
KB
14332 {
14333 case 4:
fe1b8b76 14334 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14335 break;
14336 case 8:
fe1b8b76 14337 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14338 break;
14339 default:
8e65ff28 14340 internal_error (__FILE__, __LINE__,
c764a876 14341 _("read_offset_1: bad switch [in module %s]"),
659b0389 14342 bfd_get_filename (abfd));
613e1657
KB
14343 }
14344
917c78fc 14345 return retval;
613e1657
KB
14346}
14347
fe1b8b76
JB
14348static gdb_byte *
14349read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14350{
14351 /* If the size of a host char is 8 bits, we can return a pointer
14352 to the buffer, otherwise we have to copy the data to a buffer
14353 allocated on the temporary obstack. */
4bdf3d34 14354 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14355 return buf;
c906108c
SS
14356}
14357
14358static char *
9b1c24c8 14359read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14360{
14361 /* If the size of a host char is 8 bits, we can return a pointer
14362 to the string, otherwise we have to copy the string to a buffer
14363 allocated on the temporary obstack. */
4bdf3d34 14364 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14365 if (*buf == '\0')
14366 {
14367 *bytes_read_ptr = 1;
14368 return NULL;
14369 }
fe1b8b76
JB
14370 *bytes_read_ptr = strlen ((char *) buf) + 1;
14371 return (char *) buf;
4bdf3d34
JJ
14372}
14373
14374static char *
cf2c3c16 14375read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14376{
be391dca 14377 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14378 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14379 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14380 bfd_get_filename (abfd));
dce234bc 14381 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14382 error (_("DW_FORM_strp pointing outside of "
14383 ".debug_str section [in module %s]"),
14384 bfd_get_filename (abfd));
4bdf3d34 14385 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14386 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14387 return NULL;
dce234bc 14388 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14389}
14390
36586728
TT
14391/* Read a string at offset STR_OFFSET in the .debug_str section from
14392 the .dwz file DWZ. Throw an error if the offset is too large. If
14393 the string consists of a single NUL byte, return NULL; otherwise
14394 return a pointer to the string. */
14395
14396static char *
14397read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14398{
14399 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14400
14401 if (dwz->str.buffer == NULL)
14402 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14403 "section [in module %s]"),
14404 bfd_get_filename (dwz->dwz_bfd));
14405 if (str_offset >= dwz->str.size)
14406 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14407 ".debug_str section [in module %s]"),
14408 bfd_get_filename (dwz->dwz_bfd));
14409 gdb_assert (HOST_CHAR_BIT == 8);
14410 if (dwz->str.buffer[str_offset] == '\0')
14411 return NULL;
14412 return (char *) (dwz->str.buffer + str_offset);
14413}
14414
cf2c3c16
TT
14415static char *
14416read_indirect_string (bfd *abfd, gdb_byte *buf,
14417 const struct comp_unit_head *cu_header,
14418 unsigned int *bytes_read_ptr)
14419{
14420 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14421
14422 return read_indirect_string_at_offset (abfd, str_offset);
14423}
14424
12df843f 14425static ULONGEST
fe1b8b76 14426read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14427{
12df843f 14428 ULONGEST result;
ce5d95e1 14429 unsigned int num_read;
c906108c
SS
14430 int i, shift;
14431 unsigned char byte;
14432
14433 result = 0;
14434 shift = 0;
14435 num_read = 0;
14436 i = 0;
14437 while (1)
14438 {
fe1b8b76 14439 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14440 buf++;
14441 num_read++;
12df843f 14442 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14443 if ((byte & 128) == 0)
14444 {
14445 break;
14446 }
14447 shift += 7;
14448 }
14449 *bytes_read_ptr = num_read;
14450 return result;
14451}
14452
12df843f 14453static LONGEST
fe1b8b76 14454read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14455{
12df843f 14456 LONGEST result;
77e0b926 14457 int i, shift, num_read;
c906108c
SS
14458 unsigned char byte;
14459
14460 result = 0;
14461 shift = 0;
c906108c
SS
14462 num_read = 0;
14463 i = 0;
14464 while (1)
14465 {
fe1b8b76 14466 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14467 buf++;
14468 num_read++;
12df843f 14469 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14470 shift += 7;
14471 if ((byte & 128) == 0)
14472 {
14473 break;
14474 }
14475 }
77e0b926 14476 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14477 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14478 *bytes_read_ptr = num_read;
14479 return result;
14480}
14481
3019eac3
DE
14482/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14483 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14484 ADDR_SIZE is the size of addresses from the CU header. */
14485
14486static CORE_ADDR
14487read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14488{
14489 struct objfile *objfile = dwarf2_per_objfile->objfile;
14490 bfd *abfd = objfile->obfd;
14491 const gdb_byte *info_ptr;
14492
14493 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14494 if (dwarf2_per_objfile->addr.buffer == NULL)
14495 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14496 objfile->name);
14497 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14498 error (_("DW_FORM_addr_index pointing outside of "
14499 ".debug_addr section [in module %s]"),
14500 objfile->name);
14501 info_ptr = (dwarf2_per_objfile->addr.buffer
14502 + addr_base + addr_index * addr_size);
14503 if (addr_size == 4)
14504 return bfd_get_32 (abfd, info_ptr);
14505 else
14506 return bfd_get_64 (abfd, info_ptr);
14507}
14508
14509/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14510
14511static CORE_ADDR
14512read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14513{
14514 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14515}
14516
14517/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14518
14519static CORE_ADDR
14520read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14521 unsigned int *bytes_read)
14522{
14523 bfd *abfd = cu->objfile->obfd;
14524 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14525
14526 return read_addr_index (cu, addr_index);
14527}
14528
14529/* Data structure to pass results from dwarf2_read_addr_index_reader
14530 back to dwarf2_read_addr_index. */
14531
14532struct dwarf2_read_addr_index_data
14533{
14534 ULONGEST addr_base;
14535 int addr_size;
14536};
14537
14538/* die_reader_func for dwarf2_read_addr_index. */
14539
14540static void
14541dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14542 gdb_byte *info_ptr,
14543 struct die_info *comp_unit_die,
14544 int has_children,
14545 void *data)
14546{
14547 struct dwarf2_cu *cu = reader->cu;
14548 struct dwarf2_read_addr_index_data *aidata =
14549 (struct dwarf2_read_addr_index_data *) data;
14550
14551 aidata->addr_base = cu->addr_base;
14552 aidata->addr_size = cu->header.addr_size;
14553}
14554
14555/* Given an index in .debug_addr, fetch the value.
14556 NOTE: This can be called during dwarf expression evaluation,
14557 long after the debug information has been read, and thus per_cu->cu
14558 may no longer exist. */
14559
14560CORE_ADDR
14561dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14562 unsigned int addr_index)
14563{
14564 struct objfile *objfile = per_cu->objfile;
14565 struct dwarf2_cu *cu = per_cu->cu;
14566 ULONGEST addr_base;
14567 int addr_size;
14568
14569 /* This is intended to be called from outside this file. */
14570 dw2_setup (objfile);
14571
14572 /* We need addr_base and addr_size.
14573 If we don't have PER_CU->cu, we have to get it.
14574 Nasty, but the alternative is storing the needed info in PER_CU,
14575 which at this point doesn't seem justified: it's not clear how frequently
14576 it would get used and it would increase the size of every PER_CU.
14577 Entry points like dwarf2_per_cu_addr_size do a similar thing
14578 so we're not in uncharted territory here.
14579 Alas we need to be a bit more complicated as addr_base is contained
14580 in the DIE.
14581
14582 We don't need to read the entire CU(/TU).
14583 We just need the header and top level die.
a1b64ce1 14584
3019eac3 14585 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14586 For now we skip this optimization. */
3019eac3
DE
14587
14588 if (cu != NULL)
14589 {
14590 addr_base = cu->addr_base;
14591 addr_size = cu->header.addr_size;
14592 }
14593 else
14594 {
14595 struct dwarf2_read_addr_index_data aidata;
14596
a1b64ce1
DE
14597 /* Note: We can't use init_cutu_and_read_dies_simple here,
14598 we need addr_base. */
14599 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14600 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14601 addr_base = aidata.addr_base;
14602 addr_size = aidata.addr_size;
14603 }
14604
14605 return read_addr_index_1 (addr_index, addr_base, addr_size);
14606}
14607
14608/* Given a DW_AT_str_index, fetch the string. */
14609
14610static char *
14611read_str_index (const struct die_reader_specs *reader,
14612 struct dwarf2_cu *cu, ULONGEST str_index)
14613{
14614 struct objfile *objfile = dwarf2_per_objfile->objfile;
14615 const char *dwo_name = objfile->name;
14616 bfd *abfd = objfile->obfd;
14617 struct dwo_sections *sections = &reader->dwo_file->sections;
14618 gdb_byte *info_ptr;
14619 ULONGEST str_offset;
14620
14621 dwarf2_read_section (objfile, &sections->str);
14622 dwarf2_read_section (objfile, &sections->str_offsets);
14623 if (sections->str.buffer == NULL)
14624 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14625 " in CU at offset 0x%lx [in module %s]"),
14626 (long) cu->header.offset.sect_off, dwo_name);
14627 if (sections->str_offsets.buffer == NULL)
14628 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14629 " in CU at offset 0x%lx [in module %s]"),
14630 (long) cu->header.offset.sect_off, dwo_name);
14631 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14632 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14633 " section in CU at offset 0x%lx [in module %s]"),
14634 (long) cu->header.offset.sect_off, dwo_name);
14635 info_ptr = (sections->str_offsets.buffer
14636 + str_index * cu->header.offset_size);
14637 if (cu->header.offset_size == 4)
14638 str_offset = bfd_get_32 (abfd, info_ptr);
14639 else
14640 str_offset = bfd_get_64 (abfd, info_ptr);
14641 if (str_offset >= sections->str.size)
14642 error (_("Offset from DW_FORM_str_index pointing outside of"
14643 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14644 (long) cu->header.offset.sect_off, dwo_name);
14645 return (char *) (sections->str.buffer + str_offset);
14646}
14647
3019eac3
DE
14648/* Return the length of an LEB128 number in BUF. */
14649
14650static int
14651leb128_size (const gdb_byte *buf)
14652{
14653 const gdb_byte *begin = buf;
14654 gdb_byte byte;
14655
14656 while (1)
14657 {
14658 byte = *buf++;
14659 if ((byte & 128) == 0)
14660 return buf - begin;
14661 }
14662}
14663
c906108c 14664static void
e142c38c 14665set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14666{
14667 switch (lang)
14668 {
14669 case DW_LANG_C89:
76bee0cc 14670 case DW_LANG_C99:
c906108c 14671 case DW_LANG_C:
e142c38c 14672 cu->language = language_c;
c906108c
SS
14673 break;
14674 case DW_LANG_C_plus_plus:
e142c38c 14675 cu->language = language_cplus;
c906108c 14676 break;
6aecb9c2
JB
14677 case DW_LANG_D:
14678 cu->language = language_d;
14679 break;
c906108c
SS
14680 case DW_LANG_Fortran77:
14681 case DW_LANG_Fortran90:
b21b22e0 14682 case DW_LANG_Fortran95:
e142c38c 14683 cu->language = language_fortran;
c906108c 14684 break;
a766d390
DE
14685 case DW_LANG_Go:
14686 cu->language = language_go;
14687 break;
c906108c 14688 case DW_LANG_Mips_Assembler:
e142c38c 14689 cu->language = language_asm;
c906108c 14690 break;
bebd888e 14691 case DW_LANG_Java:
e142c38c 14692 cu->language = language_java;
bebd888e 14693 break;
c906108c 14694 case DW_LANG_Ada83:
8aaf0b47 14695 case DW_LANG_Ada95:
bc5f45f8
JB
14696 cu->language = language_ada;
14697 break;
72019c9c
GM
14698 case DW_LANG_Modula2:
14699 cu->language = language_m2;
14700 break;
fe8e67fd
PM
14701 case DW_LANG_Pascal83:
14702 cu->language = language_pascal;
14703 break;
22566fbd
DJ
14704 case DW_LANG_ObjC:
14705 cu->language = language_objc;
14706 break;
c906108c
SS
14707 case DW_LANG_Cobol74:
14708 case DW_LANG_Cobol85:
c906108c 14709 default:
e142c38c 14710 cu->language = language_minimal;
c906108c
SS
14711 break;
14712 }
e142c38c 14713 cu->language_defn = language_def (cu->language);
c906108c
SS
14714}
14715
14716/* Return the named attribute or NULL if not there. */
14717
14718static struct attribute *
e142c38c 14719dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14720{
a48e046c 14721 for (;;)
c906108c 14722 {
a48e046c
TT
14723 unsigned int i;
14724 struct attribute *spec = NULL;
14725
14726 for (i = 0; i < die->num_attrs; ++i)
14727 {
14728 if (die->attrs[i].name == name)
14729 return &die->attrs[i];
14730 if (die->attrs[i].name == DW_AT_specification
14731 || die->attrs[i].name == DW_AT_abstract_origin)
14732 spec = &die->attrs[i];
14733 }
14734
14735 if (!spec)
14736 break;
c906108c 14737
f2f0e013 14738 die = follow_die_ref (die, spec, &cu);
f2f0e013 14739 }
c5aa993b 14740
c906108c
SS
14741 return NULL;
14742}
14743
348e048f
DE
14744/* Return the named attribute or NULL if not there,
14745 but do not follow DW_AT_specification, etc.
14746 This is for use in contexts where we're reading .debug_types dies.
14747 Following DW_AT_specification, DW_AT_abstract_origin will take us
14748 back up the chain, and we want to go down. */
14749
14750static struct attribute *
45e58e77 14751dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14752{
14753 unsigned int i;
14754
14755 for (i = 0; i < die->num_attrs; ++i)
14756 if (die->attrs[i].name == name)
14757 return &die->attrs[i];
14758
14759 return NULL;
14760}
14761
05cf31d1
JB
14762/* Return non-zero iff the attribute NAME is defined for the given DIE,
14763 and holds a non-zero value. This function should only be used for
2dc7f7b3 14764 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14765
14766static int
14767dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14768{
14769 struct attribute *attr = dwarf2_attr (die, name, cu);
14770
14771 return (attr && DW_UNSND (attr));
14772}
14773
3ca72b44 14774static int
e142c38c 14775die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14776{
05cf31d1
JB
14777 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14778 which value is non-zero. However, we have to be careful with
14779 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14780 (via dwarf2_flag_true_p) follows this attribute. So we may
14781 end up accidently finding a declaration attribute that belongs
14782 to a different DIE referenced by the specification attribute,
14783 even though the given DIE does not have a declaration attribute. */
14784 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14785 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14786}
14787
63d06c5c 14788/* Return the die giving the specification for DIE, if there is
f2f0e013 14789 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14790 containing the return value on output. If there is no
14791 specification, but there is an abstract origin, that is
14792 returned. */
63d06c5c
DC
14793
14794static struct die_info *
f2f0e013 14795die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14796{
f2f0e013
DJ
14797 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14798 *spec_cu);
63d06c5c 14799
edb3359d
DJ
14800 if (spec_attr == NULL)
14801 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14802
63d06c5c
DC
14803 if (spec_attr == NULL)
14804 return NULL;
14805 else
f2f0e013 14806 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14807}
c906108c 14808
debd256d 14809/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14810 refers to.
14811 NOTE: This is also used as a "cleanup" function. */
14812
debd256d
JB
14813static void
14814free_line_header (struct line_header *lh)
14815{
14816 if (lh->standard_opcode_lengths)
a8bc7b56 14817 xfree (lh->standard_opcode_lengths);
debd256d
JB
14818
14819 /* Remember that all the lh->file_names[i].name pointers are
14820 pointers into debug_line_buffer, and don't need to be freed. */
14821 if (lh->file_names)
a8bc7b56 14822 xfree (lh->file_names);
debd256d
JB
14823
14824 /* Similarly for the include directory names. */
14825 if (lh->include_dirs)
a8bc7b56 14826 xfree (lh->include_dirs);
debd256d 14827
a8bc7b56 14828 xfree (lh);
debd256d
JB
14829}
14830
debd256d 14831/* Add an entry to LH's include directory table. */
ae2de4f8 14832
debd256d
JB
14833static void
14834add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14835{
debd256d
JB
14836 /* Grow the array if necessary. */
14837 if (lh->include_dirs_size == 0)
c5aa993b 14838 {
debd256d
JB
14839 lh->include_dirs_size = 1; /* for testing */
14840 lh->include_dirs = xmalloc (lh->include_dirs_size
14841 * sizeof (*lh->include_dirs));
14842 }
14843 else if (lh->num_include_dirs >= lh->include_dirs_size)
14844 {
14845 lh->include_dirs_size *= 2;
14846 lh->include_dirs = xrealloc (lh->include_dirs,
14847 (lh->include_dirs_size
14848 * sizeof (*lh->include_dirs)));
c5aa993b 14849 }
c906108c 14850
debd256d
JB
14851 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14852}
6e70227d 14853
debd256d 14854/* Add an entry to LH's file name table. */
ae2de4f8 14855
debd256d
JB
14856static void
14857add_file_name (struct line_header *lh,
14858 char *name,
14859 unsigned int dir_index,
14860 unsigned int mod_time,
14861 unsigned int length)
14862{
14863 struct file_entry *fe;
14864
14865 /* Grow the array if necessary. */
14866 if (lh->file_names_size == 0)
14867 {
14868 lh->file_names_size = 1; /* for testing */
14869 lh->file_names = xmalloc (lh->file_names_size
14870 * sizeof (*lh->file_names));
14871 }
14872 else if (lh->num_file_names >= lh->file_names_size)
14873 {
14874 lh->file_names_size *= 2;
14875 lh->file_names = xrealloc (lh->file_names,
14876 (lh->file_names_size
14877 * sizeof (*lh->file_names)));
14878 }
14879
14880 fe = &lh->file_names[lh->num_file_names++];
14881 fe->name = name;
14882 fe->dir_index = dir_index;
14883 fe->mod_time = mod_time;
14884 fe->length = length;
aaa75496 14885 fe->included_p = 0;
cb1df416 14886 fe->symtab = NULL;
debd256d 14887}
6e70227d 14888
36586728
TT
14889/* A convenience function to find the proper .debug_line section for a
14890 CU. */
14891
14892static struct dwarf2_section_info *
14893get_debug_line_section (struct dwarf2_cu *cu)
14894{
14895 struct dwarf2_section_info *section;
14896
14897 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14898 DWO file. */
14899 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14900 section = &cu->dwo_unit->dwo_file->sections.line;
14901 else if (cu->per_cu->is_dwz)
14902 {
14903 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14904
14905 section = &dwz->line;
14906 }
14907 else
14908 section = &dwarf2_per_objfile->line;
14909
14910 return section;
14911}
14912
debd256d 14913/* Read the statement program header starting at OFFSET in
3019eac3 14914 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14915 to a struct line_header, allocated using xmalloc.
debd256d
JB
14916
14917 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14918 the returned object point into the dwarf line section buffer,
14919 and must not be freed. */
ae2de4f8 14920
debd256d 14921static struct line_header *
3019eac3 14922dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14923{
14924 struct cleanup *back_to;
14925 struct line_header *lh;
fe1b8b76 14926 gdb_byte *line_ptr;
c764a876 14927 unsigned int bytes_read, offset_size;
debd256d
JB
14928 int i;
14929 char *cur_dir, *cur_file;
3019eac3
DE
14930 struct dwarf2_section_info *section;
14931 bfd *abfd;
14932
36586728 14933 section = get_debug_line_section (cu);
3019eac3
DE
14934 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14935 if (section->buffer == NULL)
debd256d 14936 {
3019eac3
DE
14937 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14938 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14939 else
14940 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14941 return 0;
14942 }
14943
fceca515
DE
14944 /* We can't do this until we know the section is non-empty.
14945 Only then do we know we have such a section. */
14946 abfd = section->asection->owner;
14947
a738430d
MK
14948 /* Make sure that at least there's room for the total_length field.
14949 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14950 if (offset + 4 >= section->size)
debd256d 14951 {
4d3c2250 14952 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14953 return 0;
14954 }
14955
14956 lh = xmalloc (sizeof (*lh));
14957 memset (lh, 0, sizeof (*lh));
14958 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14959 (void *) lh);
14960
3019eac3 14961 line_ptr = section->buffer + offset;
debd256d 14962
a738430d 14963 /* Read in the header. */
6e70227d 14964 lh->total_length =
c764a876
DE
14965 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14966 &bytes_read, &offset_size);
debd256d 14967 line_ptr += bytes_read;
3019eac3 14968 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14969 {
4d3c2250 14970 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14971 return 0;
14972 }
14973 lh->statement_program_end = line_ptr + lh->total_length;
14974 lh->version = read_2_bytes (abfd, line_ptr);
14975 line_ptr += 2;
c764a876
DE
14976 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14977 line_ptr += offset_size;
debd256d
JB
14978 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14979 line_ptr += 1;
2dc7f7b3
TT
14980 if (lh->version >= 4)
14981 {
14982 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14983 line_ptr += 1;
14984 }
14985 else
14986 lh->maximum_ops_per_instruction = 1;
14987
14988 if (lh->maximum_ops_per_instruction == 0)
14989 {
14990 lh->maximum_ops_per_instruction = 1;
14991 complaint (&symfile_complaints,
3e43a32a
MS
14992 _("invalid maximum_ops_per_instruction "
14993 "in `.debug_line' section"));
2dc7f7b3
TT
14994 }
14995
debd256d
JB
14996 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14997 line_ptr += 1;
14998 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14999 line_ptr += 1;
15000 lh->line_range = read_1_byte (abfd, line_ptr);
15001 line_ptr += 1;
15002 lh->opcode_base = read_1_byte (abfd, line_ptr);
15003 line_ptr += 1;
15004 lh->standard_opcode_lengths
fe1b8b76 15005 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15006
15007 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15008 for (i = 1; i < lh->opcode_base; ++i)
15009 {
15010 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15011 line_ptr += 1;
15012 }
15013
a738430d 15014 /* Read directory table. */
9b1c24c8 15015 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15016 {
15017 line_ptr += bytes_read;
15018 add_include_dir (lh, cur_dir);
15019 }
15020 line_ptr += bytes_read;
15021
a738430d 15022 /* Read file name table. */
9b1c24c8 15023 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15024 {
15025 unsigned int dir_index, mod_time, length;
15026
15027 line_ptr += bytes_read;
15028 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15029 line_ptr += bytes_read;
15030 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15031 line_ptr += bytes_read;
15032 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15033 line_ptr += bytes_read;
15034
15035 add_file_name (lh, cur_file, dir_index, mod_time, length);
15036 }
15037 line_ptr += bytes_read;
6e70227d 15038 lh->statement_program_start = line_ptr;
debd256d 15039
3019eac3 15040 if (line_ptr > (section->buffer + section->size))
4d3c2250 15041 complaint (&symfile_complaints,
3e43a32a
MS
15042 _("line number info header doesn't "
15043 "fit in `.debug_line' section"));
debd256d
JB
15044
15045 discard_cleanups (back_to);
15046 return lh;
15047}
c906108c 15048
c6da4cef
DE
15049/* Subroutine of dwarf_decode_lines to simplify it.
15050 Return the file name of the psymtab for included file FILE_INDEX
15051 in line header LH of PST.
15052 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15053 If space for the result is malloc'd, it will be freed by a cleanup.
15054 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15055
15056static char *
15057psymtab_include_file_name (const struct line_header *lh, int file_index,
15058 const struct partial_symtab *pst,
15059 const char *comp_dir)
15060{
15061 const struct file_entry fe = lh->file_names [file_index];
15062 char *include_name = fe.name;
15063 char *include_name_to_compare = include_name;
15064 char *dir_name = NULL;
72b9f47f
TT
15065 const char *pst_filename;
15066 char *copied_name = NULL;
c6da4cef
DE
15067 int file_is_pst;
15068
15069 if (fe.dir_index)
15070 dir_name = lh->include_dirs[fe.dir_index - 1];
15071
15072 if (!IS_ABSOLUTE_PATH (include_name)
15073 && (dir_name != NULL || comp_dir != NULL))
15074 {
15075 /* Avoid creating a duplicate psymtab for PST.
15076 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15077 Before we do the comparison, however, we need to account
15078 for DIR_NAME and COMP_DIR.
15079 First prepend dir_name (if non-NULL). If we still don't
15080 have an absolute path prepend comp_dir (if non-NULL).
15081 However, the directory we record in the include-file's
15082 psymtab does not contain COMP_DIR (to match the
15083 corresponding symtab(s)).
15084
15085 Example:
15086
15087 bash$ cd /tmp
15088 bash$ gcc -g ./hello.c
15089 include_name = "hello.c"
15090 dir_name = "."
15091 DW_AT_comp_dir = comp_dir = "/tmp"
15092 DW_AT_name = "./hello.c" */
15093
15094 if (dir_name != NULL)
15095 {
15096 include_name = concat (dir_name, SLASH_STRING,
15097 include_name, (char *)NULL);
15098 include_name_to_compare = include_name;
15099 make_cleanup (xfree, include_name);
15100 }
15101 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15102 {
15103 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15104 include_name, (char *)NULL);
15105 }
15106 }
15107
15108 pst_filename = pst->filename;
15109 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15110 {
72b9f47f
TT
15111 copied_name = concat (pst->dirname, SLASH_STRING,
15112 pst_filename, (char *)NULL);
15113 pst_filename = copied_name;
c6da4cef
DE
15114 }
15115
1e3fad37 15116 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15117
15118 if (include_name_to_compare != include_name)
15119 xfree (include_name_to_compare);
72b9f47f
TT
15120 if (copied_name != NULL)
15121 xfree (copied_name);
c6da4cef
DE
15122
15123 if (file_is_pst)
15124 return NULL;
15125 return include_name;
15126}
15127
c91513d8
PP
15128/* Ignore this record_line request. */
15129
15130static void
15131noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15132{
15133 return;
15134}
15135
f3f5162e
DE
15136/* Subroutine of dwarf_decode_lines to simplify it.
15137 Process the line number information in LH. */
debd256d 15138
c906108c 15139static void
f3f5162e
DE
15140dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15141 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15142{
a8c50c1f 15143 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15144 gdb_byte *line_end;
a8c50c1f 15145 unsigned int bytes_read, extended_len;
c906108c 15146 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15147 CORE_ADDR baseaddr;
15148 struct objfile *objfile = cu->objfile;
f3f5162e 15149 bfd *abfd = objfile->obfd;
fbf65064 15150 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15151 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15152 struct subfile *last_subfile = NULL;
c91513d8
PP
15153 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15154 = record_line;
e142c38c
DJ
15155
15156 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15157
debd256d
JB
15158 line_ptr = lh->statement_program_start;
15159 line_end = lh->statement_program_end;
c906108c
SS
15160
15161 /* Read the statement sequences until there's nothing left. */
15162 while (line_ptr < line_end)
15163 {
15164 /* state machine registers */
15165 CORE_ADDR address = 0;
15166 unsigned int file = 1;
15167 unsigned int line = 1;
15168 unsigned int column = 0;
debd256d 15169 int is_stmt = lh->default_is_stmt;
c906108c
SS
15170 int basic_block = 0;
15171 int end_sequence = 0;
fbf65064 15172 CORE_ADDR addr;
2dc7f7b3 15173 unsigned char op_index = 0;
c906108c 15174
aaa75496 15175 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15176 {
aaa75496 15177 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15178 /* lh->include_dirs and lh->file_names are 0-based, but the
15179 directory and file name numbers in the statement program
15180 are 1-based. */
15181 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15182 char *dir = NULL;
a738430d 15183
debd256d
JB
15184 if (fe->dir_index)
15185 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15186
15187 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15188 }
15189
a738430d 15190 /* Decode the table. */
c5aa993b 15191 while (!end_sequence)
c906108c
SS
15192 {
15193 op_code = read_1_byte (abfd, line_ptr);
15194 line_ptr += 1;
59205f5a
JB
15195 if (line_ptr > line_end)
15196 {
15197 dwarf2_debug_line_missing_end_sequence_complaint ();
15198 break;
15199 }
9aa1fe7e 15200
debd256d 15201 if (op_code >= lh->opcode_base)
6e70227d 15202 {
a738430d 15203 /* Special operand. */
debd256d 15204 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15205 address += (((op_index + (adj_opcode / lh->line_range))
15206 / lh->maximum_ops_per_instruction)
15207 * lh->minimum_instruction_length);
15208 op_index = ((op_index + (adj_opcode / lh->line_range))
15209 % lh->maximum_ops_per_instruction);
debd256d 15210 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15211 if (lh->num_file_names < file || file == 0)
25e43795 15212 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15213 /* For now we ignore lines not starting on an
15214 instruction boundary. */
15215 else if (op_index == 0)
25e43795
DJ
15216 {
15217 lh->file_names[file - 1].included_p = 1;
ca5f395d 15218 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15219 {
15220 if (last_subfile != current_subfile)
15221 {
15222 addr = gdbarch_addr_bits_remove (gdbarch, address);
15223 if (last_subfile)
c91513d8 15224 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15225 last_subfile = current_subfile;
15226 }
25e43795 15227 /* Append row to matrix using current values. */
7019d805 15228 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15229 (*p_record_line) (current_subfile, line, addr);
366da635 15230 }
25e43795 15231 }
ca5f395d 15232 basic_block = 0;
9aa1fe7e
GK
15233 }
15234 else switch (op_code)
c906108c
SS
15235 {
15236 case DW_LNS_extended_op:
3e43a32a
MS
15237 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15238 &bytes_read);
473b7be6 15239 line_ptr += bytes_read;
a8c50c1f 15240 extended_end = line_ptr + extended_len;
c906108c
SS
15241 extended_op = read_1_byte (abfd, line_ptr);
15242 line_ptr += 1;
15243 switch (extended_op)
15244 {
15245 case DW_LNE_end_sequence:
c91513d8 15246 p_record_line = record_line;
c906108c 15247 end_sequence = 1;
c906108c
SS
15248 break;
15249 case DW_LNE_set_address:
e7c27a73 15250 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15251
15252 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15253 {
15254 /* This line table is for a function which has been
15255 GCd by the linker. Ignore it. PR gdb/12528 */
15256
15257 long line_offset
36586728 15258 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15259
15260 complaint (&symfile_complaints,
15261 _(".debug_line address at offset 0x%lx is 0 "
15262 "[in module %s]"),
bb5ed363 15263 line_offset, objfile->name);
c91513d8
PP
15264 p_record_line = noop_record_line;
15265 }
15266
2dc7f7b3 15267 op_index = 0;
107d2387
AC
15268 line_ptr += bytes_read;
15269 address += baseaddr;
c906108c
SS
15270 break;
15271 case DW_LNE_define_file:
debd256d
JB
15272 {
15273 char *cur_file;
15274 unsigned int dir_index, mod_time, length;
6e70227d 15275
3e43a32a
MS
15276 cur_file = read_direct_string (abfd, line_ptr,
15277 &bytes_read);
debd256d
JB
15278 line_ptr += bytes_read;
15279 dir_index =
15280 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15281 line_ptr += bytes_read;
15282 mod_time =
15283 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15284 line_ptr += bytes_read;
15285 length =
15286 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15287 line_ptr += bytes_read;
15288 add_file_name (lh, cur_file, dir_index, mod_time, length);
15289 }
c906108c 15290 break;
d0c6ba3d
CC
15291 case DW_LNE_set_discriminator:
15292 /* The discriminator is not interesting to the debugger;
15293 just ignore it. */
15294 line_ptr = extended_end;
15295 break;
c906108c 15296 default:
4d3c2250 15297 complaint (&symfile_complaints,
e2e0b3e5 15298 _("mangled .debug_line section"));
debd256d 15299 return;
c906108c 15300 }
a8c50c1f
DJ
15301 /* Make sure that we parsed the extended op correctly. If e.g.
15302 we expected a different address size than the producer used,
15303 we may have read the wrong number of bytes. */
15304 if (line_ptr != extended_end)
15305 {
15306 complaint (&symfile_complaints,
15307 _("mangled .debug_line section"));
15308 return;
15309 }
c906108c
SS
15310 break;
15311 case DW_LNS_copy:
59205f5a 15312 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15313 dwarf2_debug_line_missing_file_complaint ();
15314 else
366da635 15315 {
25e43795 15316 lh->file_names[file - 1].included_p = 1;
ca5f395d 15317 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15318 {
15319 if (last_subfile != current_subfile)
15320 {
15321 addr = gdbarch_addr_bits_remove (gdbarch, address);
15322 if (last_subfile)
c91513d8 15323 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15324 last_subfile = current_subfile;
15325 }
7019d805 15326 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15327 (*p_record_line) (current_subfile, line, addr);
fbf65064 15328 }
366da635 15329 }
c906108c
SS
15330 basic_block = 0;
15331 break;
15332 case DW_LNS_advance_pc:
2dc7f7b3
TT
15333 {
15334 CORE_ADDR adjust
15335 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15336
15337 address += (((op_index + adjust)
15338 / lh->maximum_ops_per_instruction)
15339 * lh->minimum_instruction_length);
15340 op_index = ((op_index + adjust)
15341 % lh->maximum_ops_per_instruction);
15342 line_ptr += bytes_read;
15343 }
c906108c
SS
15344 break;
15345 case DW_LNS_advance_line:
15346 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15347 line_ptr += bytes_read;
15348 break;
15349 case DW_LNS_set_file:
debd256d 15350 {
a738430d
MK
15351 /* The arrays lh->include_dirs and lh->file_names are
15352 0-based, but the directory and file name numbers in
15353 the statement program are 1-based. */
debd256d 15354 struct file_entry *fe;
4f1520fb 15355 char *dir = NULL;
a738430d 15356
debd256d
JB
15357 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15358 line_ptr += bytes_read;
59205f5a 15359 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15360 dwarf2_debug_line_missing_file_complaint ();
15361 else
15362 {
15363 fe = &lh->file_names[file - 1];
15364 if (fe->dir_index)
15365 dir = lh->include_dirs[fe->dir_index - 1];
15366 if (!decode_for_pst_p)
15367 {
15368 last_subfile = current_subfile;
15369 dwarf2_start_subfile (fe->name, dir, comp_dir);
15370 }
15371 }
debd256d 15372 }
c906108c
SS
15373 break;
15374 case DW_LNS_set_column:
15375 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15376 line_ptr += bytes_read;
15377 break;
15378 case DW_LNS_negate_stmt:
15379 is_stmt = (!is_stmt);
15380 break;
15381 case DW_LNS_set_basic_block:
15382 basic_block = 1;
15383 break;
c2c6d25f
JM
15384 /* Add to the address register of the state machine the
15385 address increment value corresponding to special opcode
a738430d
MK
15386 255. I.e., this value is scaled by the minimum
15387 instruction length since special opcode 255 would have
b021a221 15388 scaled the increment. */
c906108c 15389 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15390 {
15391 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15392
15393 address += (((op_index + adjust)
15394 / lh->maximum_ops_per_instruction)
15395 * lh->minimum_instruction_length);
15396 op_index = ((op_index + adjust)
15397 % lh->maximum_ops_per_instruction);
15398 }
c906108c
SS
15399 break;
15400 case DW_LNS_fixed_advance_pc:
15401 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15402 op_index = 0;
c906108c
SS
15403 line_ptr += 2;
15404 break;
9aa1fe7e 15405 default:
a738430d
MK
15406 {
15407 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15408 int i;
a738430d 15409
debd256d 15410 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15411 {
15412 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15413 line_ptr += bytes_read;
15414 }
15415 }
c906108c
SS
15416 }
15417 }
59205f5a
JB
15418 if (lh->num_file_names < file || file == 0)
15419 dwarf2_debug_line_missing_file_complaint ();
15420 else
15421 {
15422 lh->file_names[file - 1].included_p = 1;
15423 if (!decode_for_pst_p)
fbf65064
UW
15424 {
15425 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15426 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15427 }
59205f5a 15428 }
c906108c 15429 }
f3f5162e
DE
15430}
15431
15432/* Decode the Line Number Program (LNP) for the given line_header
15433 structure and CU. The actual information extracted and the type
15434 of structures created from the LNP depends on the value of PST.
15435
15436 1. If PST is NULL, then this procedure uses the data from the program
15437 to create all necessary symbol tables, and their linetables.
15438
15439 2. If PST is not NULL, this procedure reads the program to determine
15440 the list of files included by the unit represented by PST, and
15441 builds all the associated partial symbol tables.
15442
15443 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15444 It is used for relative paths in the line table.
15445 NOTE: When processing partial symtabs (pst != NULL),
15446 comp_dir == pst->dirname.
15447
15448 NOTE: It is important that psymtabs have the same file name (via strcmp)
15449 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15450 symtab we don't use it in the name of the psymtabs we create.
15451 E.g. expand_line_sal requires this when finding psymtabs to expand.
15452 A good testcase for this is mb-inline.exp. */
15453
15454static void
15455dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15456 struct dwarf2_cu *cu, struct partial_symtab *pst,
15457 int want_line_info)
15458{
15459 struct objfile *objfile = cu->objfile;
15460 const int decode_for_pst_p = (pst != NULL);
15461 struct subfile *first_subfile = current_subfile;
15462
15463 if (want_line_info)
15464 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15465
15466 if (decode_for_pst_p)
15467 {
15468 int file_index;
15469
15470 /* Now that we're done scanning the Line Header Program, we can
15471 create the psymtab of each included file. */
15472 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15473 if (lh->file_names[file_index].included_p == 1)
15474 {
c6da4cef
DE
15475 char *include_name =
15476 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15477 if (include_name != NULL)
aaa75496
JB
15478 dwarf2_create_include_psymtab (include_name, pst, objfile);
15479 }
15480 }
cb1df416
DJ
15481 else
15482 {
15483 /* Make sure a symtab is created for every file, even files
15484 which contain only variables (i.e. no code with associated
15485 line numbers). */
cb1df416 15486 int i;
cb1df416
DJ
15487
15488 for (i = 0; i < lh->num_file_names; i++)
15489 {
15490 char *dir = NULL;
f3f5162e 15491 struct file_entry *fe;
9a619af0 15492
cb1df416
DJ
15493 fe = &lh->file_names[i];
15494 if (fe->dir_index)
15495 dir = lh->include_dirs[fe->dir_index - 1];
15496 dwarf2_start_subfile (fe->name, dir, comp_dir);
15497
15498 /* Skip the main file; we don't need it, and it must be
15499 allocated last, so that it will show up before the
15500 non-primary symtabs in the objfile's symtab list. */
15501 if (current_subfile == first_subfile)
15502 continue;
15503
15504 if (current_subfile->symtab == NULL)
15505 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15506 objfile);
cb1df416
DJ
15507 fe->symtab = current_subfile->symtab;
15508 }
15509 }
c906108c
SS
15510}
15511
15512/* Start a subfile for DWARF. FILENAME is the name of the file and
15513 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15514 or NULL if not known. COMP_DIR is the compilation directory for the
15515 linetable's compilation unit or NULL if not known.
c906108c
SS
15516 This routine tries to keep line numbers from identical absolute and
15517 relative file names in a common subfile.
15518
15519 Using the `list' example from the GDB testsuite, which resides in
15520 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15521 of /srcdir/list0.c yields the following debugging information for list0.c:
15522
c5aa993b
JM
15523 DW_AT_name: /srcdir/list0.c
15524 DW_AT_comp_dir: /compdir
357e46e7 15525 files.files[0].name: list0.h
c5aa993b 15526 files.files[0].dir: /srcdir
357e46e7 15527 files.files[1].name: list0.c
c5aa993b 15528 files.files[1].dir: /srcdir
c906108c
SS
15529
15530 The line number information for list0.c has to end up in a single
4f1520fb
FR
15531 subfile, so that `break /srcdir/list0.c:1' works as expected.
15532 start_subfile will ensure that this happens provided that we pass the
15533 concatenation of files.files[1].dir and files.files[1].name as the
15534 subfile's name. */
c906108c
SS
15535
15536static void
3e43a32a
MS
15537dwarf2_start_subfile (char *filename, const char *dirname,
15538 const char *comp_dir)
c906108c 15539{
4f1520fb
FR
15540 char *fullname;
15541
15542 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15543 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15544 second argument to start_subfile. To be consistent, we do the
15545 same here. In order not to lose the line information directory,
15546 we concatenate it to the filename when it makes sense.
15547 Note that the Dwarf3 standard says (speaking of filenames in line
15548 information): ``The directory index is ignored for file names
15549 that represent full path names''. Thus ignoring dirname in the
15550 `else' branch below isn't an issue. */
c906108c 15551
d5166ae1 15552 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15553 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15554 else
15555 fullname = filename;
c906108c 15556
4f1520fb
FR
15557 start_subfile (fullname, comp_dir);
15558
15559 if (fullname != filename)
15560 xfree (fullname);
c906108c
SS
15561}
15562
f4dc4d17
DE
15563/* Start a symtab for DWARF.
15564 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15565
15566static void
15567dwarf2_start_symtab (struct dwarf2_cu *cu,
15568 char *name, char *comp_dir, CORE_ADDR low_pc)
15569{
15570 start_symtab (name, comp_dir, low_pc);
15571 record_debugformat ("DWARF 2");
15572 record_producer (cu->producer);
15573
15574 /* We assume that we're processing GCC output. */
15575 processing_gcc_compilation = 2;
15576
15577 processing_has_namespace_info = 0;
15578}
15579
4c2df51b
DJ
15580static void
15581var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15582 struct dwarf2_cu *cu)
4c2df51b 15583{
e7c27a73
DJ
15584 struct objfile *objfile = cu->objfile;
15585 struct comp_unit_head *cu_header = &cu->header;
15586
4c2df51b
DJ
15587 /* NOTE drow/2003-01-30: There used to be a comment and some special
15588 code here to turn a symbol with DW_AT_external and a
15589 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15590 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15591 with some versions of binutils) where shared libraries could have
15592 relocations against symbols in their debug information - the
15593 minimal symbol would have the right address, but the debug info
15594 would not. It's no longer necessary, because we will explicitly
15595 apply relocations when we read in the debug information now. */
15596
15597 /* A DW_AT_location attribute with no contents indicates that a
15598 variable has been optimized away. */
15599 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15600 {
15601 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15602 return;
15603 }
15604
15605 /* Handle one degenerate form of location expression specially, to
15606 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15607 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15608 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15609
15610 if (attr_form_is_block (attr)
3019eac3
DE
15611 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15612 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15613 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15614 && (DW_BLOCK (attr)->size
15615 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15616 {
891d2f0b 15617 unsigned int dummy;
4c2df51b 15618
3019eac3
DE
15619 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15620 SYMBOL_VALUE_ADDRESS (sym) =
15621 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15622 else
15623 SYMBOL_VALUE_ADDRESS (sym) =
15624 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15625 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15626 fixup_symbol_section (sym, objfile);
15627 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15628 SYMBOL_SECTION (sym));
4c2df51b
DJ
15629 return;
15630 }
15631
15632 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15633 expression evaluator, and use LOC_COMPUTED only when necessary
15634 (i.e. when the value of a register or memory location is
15635 referenced, or a thread-local block, etc.). Then again, it might
15636 not be worthwhile. I'm assuming that it isn't unless performance
15637 or memory numbers show me otherwise. */
15638
e7c27a73 15639 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15640 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15641
15642 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15643 cu->has_loclist = 1;
4c2df51b
DJ
15644}
15645
c906108c
SS
15646/* Given a pointer to a DWARF information entry, figure out if we need
15647 to make a symbol table entry for it, and if so, create a new entry
15648 and return a pointer to it.
15649 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15650 used the passed type.
15651 If SPACE is not NULL, use it to hold the new symbol. If it is
15652 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15653
15654static struct symbol *
34eaf542
TT
15655new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15656 struct symbol *space)
c906108c 15657{
e7c27a73 15658 struct objfile *objfile = cu->objfile;
c906108c
SS
15659 struct symbol *sym = NULL;
15660 char *name;
15661 struct attribute *attr = NULL;
15662 struct attribute *attr2 = NULL;
e142c38c 15663 CORE_ADDR baseaddr;
e37fd15a
SW
15664 struct pending **list_to_add = NULL;
15665
edb3359d 15666 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15667
15668 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15669
94af9270 15670 name = dwarf2_name (die, cu);
c906108c
SS
15671 if (name)
15672 {
94af9270 15673 const char *linkagename;
34eaf542 15674 int suppress_add = 0;
94af9270 15675
34eaf542
TT
15676 if (space)
15677 sym = space;
15678 else
15679 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15680 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15681
15682 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15683 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15684 linkagename = dwarf2_physname (name, die, cu);
15685 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15686
f55ee35c
JK
15687 /* Fortran does not have mangling standard and the mangling does differ
15688 between gfortran, iFort etc. */
15689 if (cu->language == language_fortran
b250c185 15690 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
15691 symbol_set_demangled_name (&(sym->ginfo),
15692 (char *) dwarf2_full_name (name, die, cu),
15693 NULL);
f55ee35c 15694
c906108c 15695 /* Default assumptions.
c5aa993b 15696 Use the passed type or decode it from the die. */
176620f1 15697 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15698 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15699 if (type != NULL)
15700 SYMBOL_TYPE (sym) = type;
15701 else
e7c27a73 15702 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15703 attr = dwarf2_attr (die,
15704 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15705 cu);
c906108c
SS
15706 if (attr)
15707 {
15708 SYMBOL_LINE (sym) = DW_UNSND (attr);
15709 }
cb1df416 15710
edb3359d
DJ
15711 attr = dwarf2_attr (die,
15712 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15713 cu);
cb1df416
DJ
15714 if (attr)
15715 {
15716 int file_index = DW_UNSND (attr);
9a619af0 15717
cb1df416
DJ
15718 if (cu->line_header == NULL
15719 || file_index > cu->line_header->num_file_names)
15720 complaint (&symfile_complaints,
15721 _("file index out of range"));
1c3d648d 15722 else if (file_index > 0)
cb1df416
DJ
15723 {
15724 struct file_entry *fe;
9a619af0 15725
cb1df416
DJ
15726 fe = &cu->line_header->file_names[file_index - 1];
15727 SYMBOL_SYMTAB (sym) = fe->symtab;
15728 }
15729 }
15730
c906108c
SS
15731 switch (die->tag)
15732 {
15733 case DW_TAG_label:
e142c38c 15734 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15735 if (attr)
15736 {
15737 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15738 }
0f5238ed
TT
15739 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15740 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15741 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15742 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15743 break;
15744 case DW_TAG_subprogram:
15745 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15746 finish_block. */
15747 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15748 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15749 if ((attr2 && (DW_UNSND (attr2) != 0))
15750 || cu->language == language_ada)
c906108c 15751 {
2cfa0c8d
JB
15752 /* Subprograms marked external are stored as a global symbol.
15753 Ada subprograms, whether marked external or not, are always
15754 stored as a global symbol, because we want to be able to
15755 access them globally. For instance, we want to be able
15756 to break on a nested subprogram without having to
15757 specify the context. */
e37fd15a 15758 list_to_add = &global_symbols;
c906108c
SS
15759 }
15760 else
15761 {
e37fd15a 15762 list_to_add = cu->list_in_scope;
c906108c
SS
15763 }
15764 break;
edb3359d
DJ
15765 case DW_TAG_inlined_subroutine:
15766 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15767 finish_block. */
15768 SYMBOL_CLASS (sym) = LOC_BLOCK;
15769 SYMBOL_INLINED (sym) = 1;
481860b3 15770 list_to_add = cu->list_in_scope;
edb3359d 15771 break;
34eaf542
TT
15772 case DW_TAG_template_value_param:
15773 suppress_add = 1;
15774 /* Fall through. */
72929c62 15775 case DW_TAG_constant:
c906108c 15776 case DW_TAG_variable:
254e6b9e 15777 case DW_TAG_member:
0963b4bd
MS
15778 /* Compilation with minimal debug info may result in
15779 variables with missing type entries. Change the
15780 misleading `void' type to something sensible. */
c906108c 15781 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15782 SYMBOL_TYPE (sym)
46bf5051 15783 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15784
e142c38c 15785 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15786 /* In the case of DW_TAG_member, we should only be called for
15787 static const members. */
15788 if (die->tag == DW_TAG_member)
15789 {
3863f96c
DE
15790 /* dwarf2_add_field uses die_is_declaration,
15791 so we do the same. */
254e6b9e
DE
15792 gdb_assert (die_is_declaration (die, cu));
15793 gdb_assert (attr);
15794 }
c906108c
SS
15795 if (attr)
15796 {
e7c27a73 15797 dwarf2_const_value (attr, sym, cu);
e142c38c 15798 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15799 if (!suppress_add)
34eaf542
TT
15800 {
15801 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15802 list_to_add = &global_symbols;
34eaf542 15803 else
e37fd15a 15804 list_to_add = cu->list_in_scope;
34eaf542 15805 }
c906108c
SS
15806 break;
15807 }
e142c38c 15808 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15809 if (attr)
15810 {
e7c27a73 15811 var_decode_location (attr, sym, cu);
e142c38c 15812 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15813
15814 /* Fortran explicitly imports any global symbols to the local
15815 scope by DW_TAG_common_block. */
15816 if (cu->language == language_fortran && die->parent
15817 && die->parent->tag == DW_TAG_common_block)
15818 attr2 = NULL;
15819
caac4577
JG
15820 if (SYMBOL_CLASS (sym) == LOC_STATIC
15821 && SYMBOL_VALUE_ADDRESS (sym) == 0
15822 && !dwarf2_per_objfile->has_section_at_zero)
15823 {
15824 /* When a static variable is eliminated by the linker,
15825 the corresponding debug information is not stripped
15826 out, but the variable address is set to null;
15827 do not add such variables into symbol table. */
15828 }
15829 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15830 {
f55ee35c
JK
15831 /* Workaround gfortran PR debug/40040 - it uses
15832 DW_AT_location for variables in -fPIC libraries which may
15833 get overriden by other libraries/executable and get
15834 a different address. Resolve it by the minimal symbol
15835 which may come from inferior's executable using copy
15836 relocation. Make this workaround only for gfortran as for
15837 other compilers GDB cannot guess the minimal symbol
15838 Fortran mangling kind. */
15839 if (cu->language == language_fortran && die->parent
15840 && die->parent->tag == DW_TAG_module
15841 && cu->producer
15842 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15843 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15844
1c809c68
TT
15845 /* A variable with DW_AT_external is never static,
15846 but it may be block-scoped. */
15847 list_to_add = (cu->list_in_scope == &file_symbols
15848 ? &global_symbols : cu->list_in_scope);
1c809c68 15849 }
c906108c 15850 else
e37fd15a 15851 list_to_add = cu->list_in_scope;
c906108c
SS
15852 }
15853 else
15854 {
15855 /* We do not know the address of this symbol.
c5aa993b
JM
15856 If it is an external symbol and we have type information
15857 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15858 The address of the variable will then be determined from
15859 the minimal symbol table whenever the variable is
15860 referenced. */
e142c38c 15861 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15862
15863 /* Fortran explicitly imports any global symbols to the local
15864 scope by DW_TAG_common_block. */
15865 if (cu->language == language_fortran && die->parent
15866 && die->parent->tag == DW_TAG_common_block)
15867 {
15868 /* SYMBOL_CLASS doesn't matter here because
15869 read_common_block is going to reset it. */
15870 if (!suppress_add)
15871 list_to_add = cu->list_in_scope;
15872 }
15873 else if (attr2 && (DW_UNSND (attr2) != 0)
15874 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15875 {
0fe7935b
DJ
15876 /* A variable with DW_AT_external is never static, but it
15877 may be block-scoped. */
15878 list_to_add = (cu->list_in_scope == &file_symbols
15879 ? &global_symbols : cu->list_in_scope);
15880
c906108c 15881 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15882 }
442ddf59
JK
15883 else if (!die_is_declaration (die, cu))
15884 {
15885 /* Use the default LOC_OPTIMIZED_OUT class. */
15886 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15887 if (!suppress_add)
15888 list_to_add = cu->list_in_scope;
442ddf59 15889 }
c906108c
SS
15890 }
15891 break;
15892 case DW_TAG_formal_parameter:
edb3359d
DJ
15893 /* If we are inside a function, mark this as an argument. If
15894 not, we might be looking at an argument to an inlined function
15895 when we do not have enough information to show inlined frames;
15896 pretend it's a local variable in that case so that the user can
15897 still see it. */
15898 if (context_stack_depth > 0
15899 && context_stack[context_stack_depth - 1].name != NULL)
15900 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15901 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15902 if (attr)
15903 {
e7c27a73 15904 var_decode_location (attr, sym, cu);
c906108c 15905 }
e142c38c 15906 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15907 if (attr)
15908 {
e7c27a73 15909 dwarf2_const_value (attr, sym, cu);
c906108c 15910 }
f346a30d 15911
e37fd15a 15912 list_to_add = cu->list_in_scope;
c906108c
SS
15913 break;
15914 case DW_TAG_unspecified_parameters:
15915 /* From varargs functions; gdb doesn't seem to have any
15916 interest in this information, so just ignore it for now.
15917 (FIXME?) */
15918 break;
34eaf542
TT
15919 case DW_TAG_template_type_param:
15920 suppress_add = 1;
15921 /* Fall through. */
c906108c 15922 case DW_TAG_class_type:
680b30c7 15923 case DW_TAG_interface_type:
c906108c
SS
15924 case DW_TAG_structure_type:
15925 case DW_TAG_union_type:
72019c9c 15926 case DW_TAG_set_type:
c906108c
SS
15927 case DW_TAG_enumeration_type:
15928 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15929 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15930
63d06c5c 15931 {
987504bb 15932 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15933 really ever be static objects: otherwise, if you try
15934 to, say, break of a class's method and you're in a file
15935 which doesn't mention that class, it won't work unless
15936 the check for all static symbols in lookup_symbol_aux
15937 saves you. See the OtherFileClass tests in
15938 gdb.c++/namespace.exp. */
15939
e37fd15a 15940 if (!suppress_add)
34eaf542 15941 {
34eaf542
TT
15942 list_to_add = (cu->list_in_scope == &file_symbols
15943 && (cu->language == language_cplus
15944 || cu->language == language_java)
15945 ? &global_symbols : cu->list_in_scope);
63d06c5c 15946
64382290
TT
15947 /* The semantics of C++ state that "struct foo {
15948 ... }" also defines a typedef for "foo". A Java
15949 class declaration also defines a typedef for the
15950 class. */
15951 if (cu->language == language_cplus
15952 || cu->language == language_java
15953 || cu->language == language_ada)
15954 {
15955 /* The symbol's name is already allocated along
15956 with this objfile, so we don't need to
15957 duplicate it for the type. */
15958 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15959 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15960 }
63d06c5c
DC
15961 }
15962 }
c906108c
SS
15963 break;
15964 case DW_TAG_typedef:
63d06c5c
DC
15965 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15966 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15967 list_to_add = cu->list_in_scope;
63d06c5c 15968 break;
c906108c 15969 case DW_TAG_base_type:
a02abb62 15970 case DW_TAG_subrange_type:
c906108c 15971 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15972 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15973 list_to_add = cu->list_in_scope;
c906108c
SS
15974 break;
15975 case DW_TAG_enumerator:
e142c38c 15976 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15977 if (attr)
15978 {
e7c27a73 15979 dwarf2_const_value (attr, sym, cu);
c906108c 15980 }
63d06c5c
DC
15981 {
15982 /* NOTE: carlton/2003-11-10: See comment above in the
15983 DW_TAG_class_type, etc. block. */
15984
e142c38c 15985 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15986 && (cu->language == language_cplus
15987 || cu->language == language_java)
e142c38c 15988 ? &global_symbols : cu->list_in_scope);
63d06c5c 15989 }
c906108c 15990 break;
5c4e30ca
DC
15991 case DW_TAG_namespace:
15992 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15993 list_to_add = &global_symbols;
5c4e30ca 15994 break;
4357ac6c
TT
15995 case DW_TAG_common_block:
15996 SYMBOL_CLASS (sym) = LOC_STATIC;
15997 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15998 add_symbol_to_list (sym, cu->list_in_scope);
15999 break;
c906108c
SS
16000 default:
16001 /* Not a tag we recognize. Hopefully we aren't processing
16002 trash data, but since we must specifically ignore things
16003 we don't recognize, there is nothing else we should do at
0963b4bd 16004 this point. */
e2e0b3e5 16005 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16006 dwarf_tag_name (die->tag));
c906108c
SS
16007 break;
16008 }
df8a16a1 16009
e37fd15a
SW
16010 if (suppress_add)
16011 {
16012 sym->hash_next = objfile->template_symbols;
16013 objfile->template_symbols = sym;
16014 list_to_add = NULL;
16015 }
16016
16017 if (list_to_add != NULL)
16018 add_symbol_to_list (sym, list_to_add);
16019
df8a16a1
DJ
16020 /* For the benefit of old versions of GCC, check for anonymous
16021 namespaces based on the demangled name. */
16022 if (!processing_has_namespace_info
94af9270 16023 && cu->language == language_cplus)
a10964d1 16024 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16025 }
16026 return (sym);
16027}
16028
34eaf542
TT
16029/* A wrapper for new_symbol_full that always allocates a new symbol. */
16030
16031static struct symbol *
16032new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16033{
16034 return new_symbol_full (die, type, cu, NULL);
16035}
16036
98bfdba5
PA
16037/* Given an attr with a DW_FORM_dataN value in host byte order,
16038 zero-extend it as appropriate for the symbol's type. The DWARF
16039 standard (v4) is not entirely clear about the meaning of using
16040 DW_FORM_dataN for a constant with a signed type, where the type is
16041 wider than the data. The conclusion of a discussion on the DWARF
16042 list was that this is unspecified. We choose to always zero-extend
16043 because that is the interpretation long in use by GCC. */
c906108c 16044
98bfdba5
PA
16045static gdb_byte *
16046dwarf2_const_value_data (struct attribute *attr, struct type *type,
16047 const char *name, struct obstack *obstack,
12df843f 16048 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16049{
e7c27a73 16050 struct objfile *objfile = cu->objfile;
e17a4113
UW
16051 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16052 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16053 LONGEST l = DW_UNSND (attr);
16054
16055 if (bits < sizeof (*value) * 8)
16056 {
16057 l &= ((LONGEST) 1 << bits) - 1;
16058 *value = l;
16059 }
16060 else if (bits == sizeof (*value) * 8)
16061 *value = l;
16062 else
16063 {
16064 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16065 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16066 return bytes;
16067 }
16068
16069 return NULL;
16070}
16071
16072/* Read a constant value from an attribute. Either set *VALUE, or if
16073 the value does not fit in *VALUE, set *BYTES - either already
16074 allocated on the objfile obstack, or newly allocated on OBSTACK,
16075 or, set *BATON, if we translated the constant to a location
16076 expression. */
16077
16078static void
16079dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16080 const char *name, struct obstack *obstack,
16081 struct dwarf2_cu *cu,
12df843f 16082 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16083 struct dwarf2_locexpr_baton **baton)
16084{
16085 struct objfile *objfile = cu->objfile;
16086 struct comp_unit_head *cu_header = &cu->header;
c906108c 16087 struct dwarf_block *blk;
98bfdba5
PA
16088 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16089 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16090
16091 *value = 0;
16092 *bytes = NULL;
16093 *baton = NULL;
c906108c
SS
16094
16095 switch (attr->form)
16096 {
16097 case DW_FORM_addr:
3019eac3 16098 case DW_FORM_GNU_addr_index:
ac56253d 16099 {
ac56253d
TT
16100 gdb_byte *data;
16101
98bfdba5
PA
16102 if (TYPE_LENGTH (type) != cu_header->addr_size)
16103 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16104 cu_header->addr_size,
98bfdba5 16105 TYPE_LENGTH (type));
ac56253d
TT
16106 /* Symbols of this form are reasonably rare, so we just
16107 piggyback on the existing location code rather than writing
16108 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16109 *baton = obstack_alloc (&objfile->objfile_obstack,
16110 sizeof (struct dwarf2_locexpr_baton));
16111 (*baton)->per_cu = cu->per_cu;
16112 gdb_assert ((*baton)->per_cu);
ac56253d 16113
98bfdba5
PA
16114 (*baton)->size = 2 + cu_header->addr_size;
16115 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16116 (*baton)->data = data;
ac56253d
TT
16117
16118 data[0] = DW_OP_addr;
16119 store_unsigned_integer (&data[1], cu_header->addr_size,
16120 byte_order, DW_ADDR (attr));
16121 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16122 }
c906108c 16123 break;
4ac36638 16124 case DW_FORM_string:
93b5768b 16125 case DW_FORM_strp:
3019eac3 16126 case DW_FORM_GNU_str_index:
36586728 16127 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16128 /* DW_STRING is already allocated on the objfile obstack, point
16129 directly to it. */
16130 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16131 break;
c906108c
SS
16132 case DW_FORM_block1:
16133 case DW_FORM_block2:
16134 case DW_FORM_block4:
16135 case DW_FORM_block:
2dc7f7b3 16136 case DW_FORM_exprloc:
c906108c 16137 blk = DW_BLOCK (attr);
98bfdba5
PA
16138 if (TYPE_LENGTH (type) != blk->size)
16139 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16140 TYPE_LENGTH (type));
16141 *bytes = blk->data;
c906108c 16142 break;
2df3850c
JM
16143
16144 /* The DW_AT_const_value attributes are supposed to carry the
16145 symbol's value "represented as it would be on the target
16146 architecture." By the time we get here, it's already been
16147 converted to host endianness, so we just need to sign- or
16148 zero-extend it as appropriate. */
16149 case DW_FORM_data1:
3e43a32a
MS
16150 *bytes = dwarf2_const_value_data (attr, type, name,
16151 obstack, cu, value, 8);
2df3850c 16152 break;
c906108c 16153 case DW_FORM_data2:
3e43a32a
MS
16154 *bytes = dwarf2_const_value_data (attr, type, name,
16155 obstack, cu, value, 16);
2df3850c 16156 break;
c906108c 16157 case DW_FORM_data4:
3e43a32a
MS
16158 *bytes = dwarf2_const_value_data (attr, type, name,
16159 obstack, cu, value, 32);
2df3850c 16160 break;
c906108c 16161 case DW_FORM_data8:
3e43a32a
MS
16162 *bytes = dwarf2_const_value_data (attr, type, name,
16163 obstack, cu, value, 64);
2df3850c
JM
16164 break;
16165
c906108c 16166 case DW_FORM_sdata:
98bfdba5 16167 *value = DW_SND (attr);
2df3850c
JM
16168 break;
16169
c906108c 16170 case DW_FORM_udata:
98bfdba5 16171 *value = DW_UNSND (attr);
c906108c 16172 break;
2df3850c 16173
c906108c 16174 default:
4d3c2250 16175 complaint (&symfile_complaints,
e2e0b3e5 16176 _("unsupported const value attribute form: '%s'"),
4d3c2250 16177 dwarf_form_name (attr->form));
98bfdba5 16178 *value = 0;
c906108c
SS
16179 break;
16180 }
16181}
16182
2df3850c 16183
98bfdba5
PA
16184/* Copy constant value from an attribute to a symbol. */
16185
2df3850c 16186static void
98bfdba5
PA
16187dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16188 struct dwarf2_cu *cu)
2df3850c 16189{
98bfdba5
PA
16190 struct objfile *objfile = cu->objfile;
16191 struct comp_unit_head *cu_header = &cu->header;
12df843f 16192 LONGEST value;
98bfdba5
PA
16193 gdb_byte *bytes;
16194 struct dwarf2_locexpr_baton *baton;
2df3850c 16195
98bfdba5
PA
16196 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16197 SYMBOL_PRINT_NAME (sym),
16198 &objfile->objfile_obstack, cu,
16199 &value, &bytes, &baton);
2df3850c 16200
98bfdba5
PA
16201 if (baton != NULL)
16202 {
16203 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16204 SYMBOL_LOCATION_BATON (sym) = baton;
16205 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16206 }
16207 else if (bytes != NULL)
16208 {
16209 SYMBOL_VALUE_BYTES (sym) = bytes;
16210 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16211 }
16212 else
16213 {
16214 SYMBOL_VALUE (sym) = value;
16215 SYMBOL_CLASS (sym) = LOC_CONST;
16216 }
2df3850c
JM
16217}
16218
c906108c
SS
16219/* Return the type of the die in question using its DW_AT_type attribute. */
16220
16221static struct type *
e7c27a73 16222die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16223{
c906108c 16224 struct attribute *type_attr;
c906108c 16225
e142c38c 16226 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16227 if (!type_attr)
16228 {
16229 /* A missing DW_AT_type represents a void type. */
46bf5051 16230 return objfile_type (cu->objfile)->builtin_void;
c906108c 16231 }
348e048f 16232
673bfd45 16233 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16234}
16235
b4ba55a1
JB
16236/* True iff CU's producer generates GNAT Ada auxiliary information
16237 that allows to find parallel types through that information instead
16238 of having to do expensive parallel lookups by type name. */
16239
16240static int
16241need_gnat_info (struct dwarf2_cu *cu)
16242{
16243 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16244 of GNAT produces this auxiliary information, without any indication
16245 that it is produced. Part of enhancing the FSF version of GNAT
16246 to produce that information will be to put in place an indicator
16247 that we can use in order to determine whether the descriptive type
16248 info is available or not. One suggestion that has been made is
16249 to use a new attribute, attached to the CU die. For now, assume
16250 that the descriptive type info is not available. */
16251 return 0;
16252}
16253
b4ba55a1
JB
16254/* Return the auxiliary type of the die in question using its
16255 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16256 attribute is not present. */
16257
16258static struct type *
16259die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16260{
b4ba55a1 16261 struct attribute *type_attr;
b4ba55a1
JB
16262
16263 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16264 if (!type_attr)
16265 return NULL;
16266
673bfd45 16267 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16268}
16269
16270/* If DIE has a descriptive_type attribute, then set the TYPE's
16271 descriptive type accordingly. */
16272
16273static void
16274set_descriptive_type (struct type *type, struct die_info *die,
16275 struct dwarf2_cu *cu)
16276{
16277 struct type *descriptive_type = die_descriptive_type (die, cu);
16278
16279 if (descriptive_type)
16280 {
16281 ALLOCATE_GNAT_AUX_TYPE (type);
16282 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16283 }
16284}
16285
c906108c
SS
16286/* Return the containing type of the die in question using its
16287 DW_AT_containing_type attribute. */
16288
16289static struct type *
e7c27a73 16290die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16291{
c906108c 16292 struct attribute *type_attr;
c906108c 16293
e142c38c 16294 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16295 if (!type_attr)
16296 error (_("Dwarf Error: Problem turning containing type into gdb type "
16297 "[in module %s]"), cu->objfile->name);
16298
673bfd45 16299 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16300}
16301
673bfd45
DE
16302/* Look up the type of DIE in CU using its type attribute ATTR.
16303 If there is no type substitute an error marker. */
16304
c906108c 16305static struct type *
673bfd45
DE
16306lookup_die_type (struct die_info *die, struct attribute *attr,
16307 struct dwarf2_cu *cu)
c906108c 16308{
bb5ed363 16309 struct objfile *objfile = cu->objfile;
f792889a
DJ
16310 struct type *this_type;
16311
673bfd45
DE
16312 /* First see if we have it cached. */
16313
36586728
TT
16314 if (attr->form == DW_FORM_GNU_ref_alt)
16315 {
16316 struct dwarf2_per_cu_data *per_cu;
16317 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16318
16319 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16320 this_type = get_die_type_at_offset (offset, per_cu);
16321 }
16322 else if (is_ref_attr (attr))
673bfd45 16323 {
b64f50a1 16324 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16325
16326 this_type = get_die_type_at_offset (offset, cu->per_cu);
16327 }
55f1336d 16328 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16329 {
16330 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16331
16332 /* sig_type will be NULL if the signatured type is missing from
16333 the debug info. */
16334 if (sig_type == NULL)
16335 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16336 "at 0x%x [in module %s]"),
b64f50a1 16337 die->offset.sect_off, objfile->name);
673bfd45 16338
3019eac3
DE
16339 gdb_assert (sig_type->per_cu.is_debug_types);
16340 /* If we haven't filled in type_offset_in_section yet, then we
16341 haven't read the type in yet. */
16342 this_type = NULL;
16343 if (sig_type->type_offset_in_section.sect_off != 0)
16344 {
16345 this_type =
16346 get_die_type_at_offset (sig_type->type_offset_in_section,
16347 &sig_type->per_cu);
16348 }
673bfd45
DE
16349 }
16350 else
16351 {
16352 dump_die_for_error (die);
16353 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16354 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16355 }
16356
16357 /* If not cached we need to read it in. */
16358
16359 if (this_type == NULL)
16360 {
16361 struct die_info *type_die;
16362 struct dwarf2_cu *type_cu = cu;
16363
16364 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16365 /* If we found the type now, it's probably because the type came
16366 from an inter-CU reference and the type's CU got expanded before
16367 ours. */
16368 this_type = get_die_type (type_die, type_cu);
16369 if (this_type == NULL)
16370 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16371 }
16372
16373 /* If we still don't have a type use an error marker. */
16374
16375 if (this_type == NULL)
c906108c 16376 {
b00fdb78
TT
16377 char *message, *saved;
16378
16379 /* read_type_die already issued a complaint. */
16380 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16381 objfile->name,
b64f50a1
JK
16382 cu->header.offset.sect_off,
16383 die->offset.sect_off);
bb5ed363 16384 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16385 message, strlen (message));
16386 xfree (message);
16387
bb5ed363 16388 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16389 }
673bfd45 16390
f792889a 16391 return this_type;
c906108c
SS
16392}
16393
673bfd45
DE
16394/* Return the type in DIE, CU.
16395 Returns NULL for invalid types.
16396
16397 This first does a lookup in the appropriate type_hash table,
16398 and only reads the die in if necessary.
16399
16400 NOTE: This can be called when reading in partial or full symbols. */
16401
f792889a 16402static struct type *
e7c27a73 16403read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16404{
f792889a
DJ
16405 struct type *this_type;
16406
16407 this_type = get_die_type (die, cu);
16408 if (this_type)
16409 return this_type;
16410
673bfd45
DE
16411 return read_type_die_1 (die, cu);
16412}
16413
16414/* Read the type in DIE, CU.
16415 Returns NULL for invalid types. */
16416
16417static struct type *
16418read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16419{
16420 struct type *this_type = NULL;
16421
c906108c
SS
16422 switch (die->tag)
16423 {
16424 case DW_TAG_class_type:
680b30c7 16425 case DW_TAG_interface_type:
c906108c
SS
16426 case DW_TAG_structure_type:
16427 case DW_TAG_union_type:
f792889a 16428 this_type = read_structure_type (die, cu);
c906108c
SS
16429 break;
16430 case DW_TAG_enumeration_type:
f792889a 16431 this_type = read_enumeration_type (die, cu);
c906108c
SS
16432 break;
16433 case DW_TAG_subprogram:
16434 case DW_TAG_subroutine_type:
edb3359d 16435 case DW_TAG_inlined_subroutine:
f792889a 16436 this_type = read_subroutine_type (die, cu);
c906108c
SS
16437 break;
16438 case DW_TAG_array_type:
f792889a 16439 this_type = read_array_type (die, cu);
c906108c 16440 break;
72019c9c 16441 case DW_TAG_set_type:
f792889a 16442 this_type = read_set_type (die, cu);
72019c9c 16443 break;
c906108c 16444 case DW_TAG_pointer_type:
f792889a 16445 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16446 break;
16447 case DW_TAG_ptr_to_member_type:
f792889a 16448 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16449 break;
16450 case DW_TAG_reference_type:
f792889a 16451 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16452 break;
16453 case DW_TAG_const_type:
f792889a 16454 this_type = read_tag_const_type (die, cu);
c906108c
SS
16455 break;
16456 case DW_TAG_volatile_type:
f792889a 16457 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
16458 break;
16459 case DW_TAG_string_type:
f792889a 16460 this_type = read_tag_string_type (die, cu);
c906108c
SS
16461 break;
16462 case DW_TAG_typedef:
f792889a 16463 this_type = read_typedef (die, cu);
c906108c 16464 break;
a02abb62 16465 case DW_TAG_subrange_type:
f792889a 16466 this_type = read_subrange_type (die, cu);
a02abb62 16467 break;
c906108c 16468 case DW_TAG_base_type:
f792889a 16469 this_type = read_base_type (die, cu);
c906108c 16470 break;
81a17f79 16471 case DW_TAG_unspecified_type:
f792889a 16472 this_type = read_unspecified_type (die, cu);
81a17f79 16473 break;
0114d602
DJ
16474 case DW_TAG_namespace:
16475 this_type = read_namespace_type (die, cu);
16476 break;
f55ee35c
JK
16477 case DW_TAG_module:
16478 this_type = read_module_type (die, cu);
16479 break;
c906108c 16480 default:
3e43a32a
MS
16481 complaint (&symfile_complaints,
16482 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16483 dwarf_tag_name (die->tag));
c906108c
SS
16484 break;
16485 }
63d06c5c 16486
f792889a 16487 return this_type;
63d06c5c
DC
16488}
16489
abc72ce4
DE
16490/* See if we can figure out if the class lives in a namespace. We do
16491 this by looking for a member function; its demangled name will
16492 contain namespace info, if there is any.
16493 Return the computed name or NULL.
16494 Space for the result is allocated on the objfile's obstack.
16495 This is the full-die version of guess_partial_die_structure_name.
16496 In this case we know DIE has no useful parent. */
16497
16498static char *
16499guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16500{
16501 struct die_info *spec_die;
16502 struct dwarf2_cu *spec_cu;
16503 struct die_info *child;
16504
16505 spec_cu = cu;
16506 spec_die = die_specification (die, &spec_cu);
16507 if (spec_die != NULL)
16508 {
16509 die = spec_die;
16510 cu = spec_cu;
16511 }
16512
16513 for (child = die->child;
16514 child != NULL;
16515 child = child->sibling)
16516 {
16517 if (child->tag == DW_TAG_subprogram)
16518 {
16519 struct attribute *attr;
16520
16521 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16522 if (attr == NULL)
16523 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16524 if (attr != NULL)
16525 {
16526 char *actual_name
16527 = language_class_name_from_physname (cu->language_defn,
16528 DW_STRING (attr));
16529 char *name = NULL;
16530
16531 if (actual_name != NULL)
16532 {
16533 char *die_name = dwarf2_name (die, cu);
16534
16535 if (die_name != NULL
16536 && strcmp (die_name, actual_name) != 0)
16537 {
16538 /* Strip off the class name from the full name.
16539 We want the prefix. */
16540 int die_name_len = strlen (die_name);
16541 int actual_name_len = strlen (actual_name);
16542
16543 /* Test for '::' as a sanity check. */
16544 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16545 && actual_name[actual_name_len
16546 - die_name_len - 1] == ':')
abc72ce4
DE
16547 name =
16548 obsavestring (actual_name,
16549 actual_name_len - die_name_len - 2,
16550 &cu->objfile->objfile_obstack);
16551 }
16552 }
16553 xfree (actual_name);
16554 return name;
16555 }
16556 }
16557 }
16558
16559 return NULL;
16560}
16561
96408a79
SA
16562/* GCC might emit a nameless typedef that has a linkage name. Determine the
16563 prefix part in such case. See
16564 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16565
16566static char *
16567anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16568{
16569 struct attribute *attr;
16570 char *base;
16571
16572 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16573 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16574 return NULL;
16575
16576 attr = dwarf2_attr (die, DW_AT_name, cu);
16577 if (attr != NULL && DW_STRING (attr) != NULL)
16578 return NULL;
16579
16580 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16581 if (attr == NULL)
16582 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16583 if (attr == NULL || DW_STRING (attr) == NULL)
16584 return NULL;
16585
16586 /* dwarf2_name had to be already called. */
16587 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16588
16589 /* Strip the base name, keep any leading namespaces/classes. */
16590 base = strrchr (DW_STRING (attr), ':');
16591 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16592 return "";
16593
16594 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16595 &cu->objfile->objfile_obstack);
16596}
16597
fdde2d81 16598/* Return the name of the namespace/class that DIE is defined within,
0114d602 16599 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16600
0114d602
DJ
16601 For example, if we're within the method foo() in the following
16602 code:
16603
16604 namespace N {
16605 class C {
16606 void foo () {
16607 }
16608 };
16609 }
16610
16611 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16612
0d5cff50 16613static const char *
e142c38c 16614determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16615{
0114d602
DJ
16616 struct die_info *parent, *spec_die;
16617 struct dwarf2_cu *spec_cu;
16618 struct type *parent_type;
96408a79 16619 char *retval;
63d06c5c 16620
f55ee35c
JK
16621 if (cu->language != language_cplus && cu->language != language_java
16622 && cu->language != language_fortran)
0114d602
DJ
16623 return "";
16624
96408a79
SA
16625 retval = anonymous_struct_prefix (die, cu);
16626 if (retval)
16627 return retval;
16628
0114d602
DJ
16629 /* We have to be careful in the presence of DW_AT_specification.
16630 For example, with GCC 3.4, given the code
16631
16632 namespace N {
16633 void foo() {
16634 // Definition of N::foo.
16635 }
16636 }
16637
16638 then we'll have a tree of DIEs like this:
16639
16640 1: DW_TAG_compile_unit
16641 2: DW_TAG_namespace // N
16642 3: DW_TAG_subprogram // declaration of N::foo
16643 4: DW_TAG_subprogram // definition of N::foo
16644 DW_AT_specification // refers to die #3
16645
16646 Thus, when processing die #4, we have to pretend that we're in
16647 the context of its DW_AT_specification, namely the contex of die
16648 #3. */
16649 spec_cu = cu;
16650 spec_die = die_specification (die, &spec_cu);
16651 if (spec_die == NULL)
16652 parent = die->parent;
16653 else
63d06c5c 16654 {
0114d602
DJ
16655 parent = spec_die->parent;
16656 cu = spec_cu;
63d06c5c 16657 }
0114d602
DJ
16658
16659 if (parent == NULL)
16660 return "";
98bfdba5
PA
16661 else if (parent->building_fullname)
16662 {
16663 const char *name;
16664 const char *parent_name;
16665
16666 /* It has been seen on RealView 2.2 built binaries,
16667 DW_TAG_template_type_param types actually _defined_ as
16668 children of the parent class:
16669
16670 enum E {};
16671 template class <class Enum> Class{};
16672 Class<enum E> class_e;
16673
16674 1: DW_TAG_class_type (Class)
16675 2: DW_TAG_enumeration_type (E)
16676 3: DW_TAG_enumerator (enum1:0)
16677 3: DW_TAG_enumerator (enum2:1)
16678 ...
16679 2: DW_TAG_template_type_param
16680 DW_AT_type DW_FORM_ref_udata (E)
16681
16682 Besides being broken debug info, it can put GDB into an
16683 infinite loop. Consider:
16684
16685 When we're building the full name for Class<E>, we'll start
16686 at Class, and go look over its template type parameters,
16687 finding E. We'll then try to build the full name of E, and
16688 reach here. We're now trying to build the full name of E,
16689 and look over the parent DIE for containing scope. In the
16690 broken case, if we followed the parent DIE of E, we'd again
16691 find Class, and once again go look at its template type
16692 arguments, etc., etc. Simply don't consider such parent die
16693 as source-level parent of this die (it can't be, the language
16694 doesn't allow it), and break the loop here. */
16695 name = dwarf2_name (die, cu);
16696 parent_name = dwarf2_name (parent, cu);
16697 complaint (&symfile_complaints,
16698 _("template param type '%s' defined within parent '%s'"),
16699 name ? name : "<unknown>",
16700 parent_name ? parent_name : "<unknown>");
16701 return "";
16702 }
63d06c5c 16703 else
0114d602
DJ
16704 switch (parent->tag)
16705 {
63d06c5c 16706 case DW_TAG_namespace:
0114d602 16707 parent_type = read_type_die (parent, cu);
acebe513
UW
16708 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16709 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16710 Work around this problem here. */
16711 if (cu->language == language_cplus
16712 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16713 return "";
0114d602
DJ
16714 /* We give a name to even anonymous namespaces. */
16715 return TYPE_TAG_NAME (parent_type);
63d06c5c 16716 case DW_TAG_class_type:
680b30c7 16717 case DW_TAG_interface_type:
63d06c5c 16718 case DW_TAG_structure_type:
0114d602 16719 case DW_TAG_union_type:
f55ee35c 16720 case DW_TAG_module:
0114d602
DJ
16721 parent_type = read_type_die (parent, cu);
16722 if (TYPE_TAG_NAME (parent_type) != NULL)
16723 return TYPE_TAG_NAME (parent_type);
16724 else
16725 /* An anonymous structure is only allowed non-static data
16726 members; no typedefs, no member functions, et cetera.
16727 So it does not need a prefix. */
16728 return "";
abc72ce4 16729 case DW_TAG_compile_unit:
95554aad 16730 case DW_TAG_partial_unit:
abc72ce4
DE
16731 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16732 if (cu->language == language_cplus
8b70b953 16733 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16734 && die->child != NULL
16735 && (die->tag == DW_TAG_class_type
16736 || die->tag == DW_TAG_structure_type
16737 || die->tag == DW_TAG_union_type))
16738 {
16739 char *name = guess_full_die_structure_name (die, cu);
16740 if (name != NULL)
16741 return name;
16742 }
16743 return "";
63d06c5c 16744 default:
8176b9b8 16745 return determine_prefix (parent, cu);
63d06c5c 16746 }
63d06c5c
DC
16747}
16748
3e43a32a
MS
16749/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16750 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16751 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16752 an obconcat, otherwise allocate storage for the result. The CU argument is
16753 used to determine the language and hence, the appropriate separator. */
987504bb 16754
f55ee35c 16755#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16756
16757static char *
f55ee35c
JK
16758typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16759 int physname, struct dwarf2_cu *cu)
63d06c5c 16760{
f55ee35c 16761 const char *lead = "";
5c315b68 16762 const char *sep;
63d06c5c 16763
3e43a32a
MS
16764 if (suffix == NULL || suffix[0] == '\0'
16765 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16766 sep = "";
16767 else if (cu->language == language_java)
16768 sep = ".";
f55ee35c
JK
16769 else if (cu->language == language_fortran && physname)
16770 {
16771 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16772 DW_AT_MIPS_linkage_name is preferred and used instead. */
16773
16774 lead = "__";
16775 sep = "_MOD_";
16776 }
987504bb
JJ
16777 else
16778 sep = "::";
63d06c5c 16779
6dd47d34
DE
16780 if (prefix == NULL)
16781 prefix = "";
16782 if (suffix == NULL)
16783 suffix = "";
16784
987504bb
JJ
16785 if (obs == NULL)
16786 {
3e43a32a
MS
16787 char *retval
16788 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16789
f55ee35c
JK
16790 strcpy (retval, lead);
16791 strcat (retval, prefix);
6dd47d34
DE
16792 strcat (retval, sep);
16793 strcat (retval, suffix);
63d06c5c
DC
16794 return retval;
16795 }
987504bb
JJ
16796 else
16797 {
16798 /* We have an obstack. */
f55ee35c 16799 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16800 }
63d06c5c
DC
16801}
16802
c906108c
SS
16803/* Return sibling of die, NULL if no sibling. */
16804
f9aca02d 16805static struct die_info *
fba45db2 16806sibling_die (struct die_info *die)
c906108c 16807{
639d11d3 16808 return die->sibling;
c906108c
SS
16809}
16810
71c25dea
TT
16811/* Get name of a die, return NULL if not found. */
16812
16813static char *
16814dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16815 struct obstack *obstack)
16816{
16817 if (name && cu->language == language_cplus)
16818 {
16819 char *canon_name = cp_canonicalize_string (name);
16820
16821 if (canon_name != NULL)
16822 {
16823 if (strcmp (canon_name, name) != 0)
16824 name = obsavestring (canon_name, strlen (canon_name),
16825 obstack);
16826 xfree (canon_name);
16827 }
16828 }
16829
16830 return name;
c906108c
SS
16831}
16832
9219021c
DC
16833/* Get name of a die, return NULL if not found. */
16834
16835static char *
e142c38c 16836dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16837{
16838 struct attribute *attr;
16839
e142c38c 16840 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16841 if ((!attr || !DW_STRING (attr))
16842 && die->tag != DW_TAG_class_type
16843 && die->tag != DW_TAG_interface_type
16844 && die->tag != DW_TAG_structure_type
16845 && die->tag != DW_TAG_union_type)
71c25dea
TT
16846 return NULL;
16847
16848 switch (die->tag)
16849 {
16850 case DW_TAG_compile_unit:
95554aad 16851 case DW_TAG_partial_unit:
71c25dea
TT
16852 /* Compilation units have a DW_AT_name that is a filename, not
16853 a source language identifier. */
16854 case DW_TAG_enumeration_type:
16855 case DW_TAG_enumerator:
16856 /* These tags always have simple identifiers already; no need
16857 to canonicalize them. */
16858 return DW_STRING (attr);
907af001 16859
418835cc
KS
16860 case DW_TAG_subprogram:
16861 /* Java constructors will all be named "<init>", so return
16862 the class name when we see this special case. */
16863 if (cu->language == language_java
16864 && DW_STRING (attr) != NULL
16865 && strcmp (DW_STRING (attr), "<init>") == 0)
16866 {
16867 struct dwarf2_cu *spec_cu = cu;
16868 struct die_info *spec_die;
16869
16870 /* GCJ will output '<init>' for Java constructor names.
16871 For this special case, return the name of the parent class. */
16872
16873 /* GCJ may output suprogram DIEs with AT_specification set.
16874 If so, use the name of the specified DIE. */
16875 spec_die = die_specification (die, &spec_cu);
16876 if (spec_die != NULL)
16877 return dwarf2_name (spec_die, spec_cu);
16878
16879 do
16880 {
16881 die = die->parent;
16882 if (die->tag == DW_TAG_class_type)
16883 return dwarf2_name (die, cu);
16884 }
95554aad
TT
16885 while (die->tag != DW_TAG_compile_unit
16886 && die->tag != DW_TAG_partial_unit);
418835cc 16887 }
907af001
UW
16888 break;
16889
16890 case DW_TAG_class_type:
16891 case DW_TAG_interface_type:
16892 case DW_TAG_structure_type:
16893 case DW_TAG_union_type:
16894 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16895 structures or unions. These were of the form "._%d" in GCC 4.1,
16896 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16897 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16898 if (attr && DW_STRING (attr)
16899 && (strncmp (DW_STRING (attr), "._", 2) == 0
16900 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16901 return NULL;
53832f31
TT
16902
16903 /* GCC might emit a nameless typedef that has a linkage name. See
16904 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16905 if (!attr || DW_STRING (attr) == NULL)
16906 {
df5c6c50 16907 char *demangled = NULL;
53832f31
TT
16908
16909 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16910 if (attr == NULL)
16911 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16912
16913 if (attr == NULL || DW_STRING (attr) == NULL)
16914 return NULL;
16915
df5c6c50
JK
16916 /* Avoid demangling DW_STRING (attr) the second time on a second
16917 call for the same DIE. */
16918 if (!DW_STRING_IS_CANONICAL (attr))
16919 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16920
16921 if (demangled)
16922 {
96408a79
SA
16923 char *base;
16924
53832f31 16925 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16926 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16927 &cu->objfile->objfile_obstack);
53832f31
TT
16928 DW_STRING_IS_CANONICAL (attr) = 1;
16929 xfree (demangled);
96408a79
SA
16930
16931 /* Strip any leading namespaces/classes, keep only the base name.
16932 DW_AT_name for named DIEs does not contain the prefixes. */
16933 base = strrchr (DW_STRING (attr), ':');
16934 if (base && base > DW_STRING (attr) && base[-1] == ':')
16935 return &base[1];
16936 else
16937 return DW_STRING (attr);
53832f31
TT
16938 }
16939 }
907af001
UW
16940 break;
16941
71c25dea 16942 default:
907af001
UW
16943 break;
16944 }
16945
16946 if (!DW_STRING_IS_CANONICAL (attr))
16947 {
16948 DW_STRING (attr)
16949 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16950 &cu->objfile->objfile_obstack);
16951 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16952 }
907af001 16953 return DW_STRING (attr);
9219021c
DC
16954}
16955
16956/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16957 is none. *EXT_CU is the CU containing DIE on input, and the CU
16958 containing the return value on output. */
9219021c
DC
16959
16960static struct die_info *
f2f0e013 16961dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16962{
16963 struct attribute *attr;
9219021c 16964
f2f0e013 16965 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16966 if (attr == NULL)
16967 return NULL;
16968
f2f0e013 16969 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16970}
16971
c906108c
SS
16972/* Convert a DIE tag into its string name. */
16973
f39c6ffd 16974static const char *
aa1ee363 16975dwarf_tag_name (unsigned tag)
c906108c 16976{
f39c6ffd
TT
16977 const char *name = get_DW_TAG_name (tag);
16978
16979 if (name == NULL)
16980 return "DW_TAG_<unknown>";
16981
16982 return name;
c906108c
SS
16983}
16984
16985/* Convert a DWARF attribute code into its string name. */
16986
f39c6ffd 16987static const char *
aa1ee363 16988dwarf_attr_name (unsigned attr)
c906108c 16989{
f39c6ffd
TT
16990 const char *name;
16991
c764a876 16992#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16993 if (attr == DW_AT_MIPS_fde)
16994 return "DW_AT_MIPS_fde";
16995#else
16996 if (attr == DW_AT_HP_block_index)
16997 return "DW_AT_HP_block_index";
c764a876 16998#endif
f39c6ffd
TT
16999
17000 name = get_DW_AT_name (attr);
17001
17002 if (name == NULL)
17003 return "DW_AT_<unknown>";
17004
17005 return name;
c906108c
SS
17006}
17007
17008/* Convert a DWARF value form code into its string name. */
17009
f39c6ffd 17010static const char *
aa1ee363 17011dwarf_form_name (unsigned form)
c906108c 17012{
f39c6ffd
TT
17013 const char *name = get_DW_FORM_name (form);
17014
17015 if (name == NULL)
17016 return "DW_FORM_<unknown>";
17017
17018 return name;
c906108c
SS
17019}
17020
17021static char *
fba45db2 17022dwarf_bool_name (unsigned mybool)
c906108c
SS
17023{
17024 if (mybool)
17025 return "TRUE";
17026 else
17027 return "FALSE";
17028}
17029
17030/* Convert a DWARF type code into its string name. */
17031
f39c6ffd 17032static const char *
aa1ee363 17033dwarf_type_encoding_name (unsigned enc)
c906108c 17034{
f39c6ffd 17035 const char *name = get_DW_ATE_name (enc);
c906108c 17036
f39c6ffd
TT
17037 if (name == NULL)
17038 return "DW_ATE_<unknown>";
c906108c 17039
f39c6ffd 17040 return name;
c906108c 17041}
c906108c 17042
f9aca02d 17043static void
d97bc12b 17044dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17045{
17046 unsigned int i;
17047
d97bc12b
DE
17048 print_spaces (indent, f);
17049 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17050 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17051
17052 if (die->parent != NULL)
17053 {
17054 print_spaces (indent, f);
17055 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17056 die->parent->offset.sect_off);
d97bc12b
DE
17057 }
17058
17059 print_spaces (indent, f);
17060 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17061 dwarf_bool_name (die->child != NULL));
c906108c 17062
d97bc12b
DE
17063 print_spaces (indent, f);
17064 fprintf_unfiltered (f, " attributes:\n");
17065
c906108c
SS
17066 for (i = 0; i < die->num_attrs; ++i)
17067 {
d97bc12b
DE
17068 print_spaces (indent, f);
17069 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17070 dwarf_attr_name (die->attrs[i].name),
17071 dwarf_form_name (die->attrs[i].form));
d97bc12b 17072
c906108c
SS
17073 switch (die->attrs[i].form)
17074 {
c906108c 17075 case DW_FORM_addr:
3019eac3 17076 case DW_FORM_GNU_addr_index:
d97bc12b 17077 fprintf_unfiltered (f, "address: ");
5af949e3 17078 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17079 break;
17080 case DW_FORM_block2:
17081 case DW_FORM_block4:
17082 case DW_FORM_block:
17083 case DW_FORM_block1:
56eb65bd
SP
17084 fprintf_unfiltered (f, "block: size %s",
17085 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17086 break;
2dc7f7b3 17087 case DW_FORM_exprloc:
56eb65bd
SP
17088 fprintf_unfiltered (f, "expression: size %s",
17089 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17090 break;
4568ecf9
DE
17091 case DW_FORM_ref_addr:
17092 fprintf_unfiltered (f, "ref address: ");
17093 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17094 break;
36586728
TT
17095 case DW_FORM_GNU_ref_alt:
17096 fprintf_unfiltered (f, "alt ref address: ");
17097 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17098 break;
10b3939b
DJ
17099 case DW_FORM_ref1:
17100 case DW_FORM_ref2:
17101 case DW_FORM_ref4:
4568ecf9
DE
17102 case DW_FORM_ref8:
17103 case DW_FORM_ref_udata:
d97bc12b 17104 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17105 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17106 break;
c906108c
SS
17107 case DW_FORM_data1:
17108 case DW_FORM_data2:
17109 case DW_FORM_data4:
ce5d95e1 17110 case DW_FORM_data8:
c906108c
SS
17111 case DW_FORM_udata:
17112 case DW_FORM_sdata:
43bbcdc2
PH
17113 fprintf_unfiltered (f, "constant: %s",
17114 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17115 break;
2dc7f7b3
TT
17116 case DW_FORM_sec_offset:
17117 fprintf_unfiltered (f, "section offset: %s",
17118 pulongest (DW_UNSND (&die->attrs[i])));
17119 break;
55f1336d 17120 case DW_FORM_ref_sig8:
348e048f
DE
17121 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17122 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17123 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17124 else
17125 fprintf_unfiltered (f, "signatured type, offset: unknown");
17126 break;
c906108c 17127 case DW_FORM_string:
4bdf3d34 17128 case DW_FORM_strp:
3019eac3 17129 case DW_FORM_GNU_str_index:
36586728 17130 case DW_FORM_GNU_strp_alt:
8285870a 17131 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17132 DW_STRING (&die->attrs[i])
8285870a
JK
17133 ? DW_STRING (&die->attrs[i]) : "",
17134 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17135 break;
17136 case DW_FORM_flag:
17137 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17138 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17139 else
d97bc12b 17140 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17141 break;
2dc7f7b3
TT
17142 case DW_FORM_flag_present:
17143 fprintf_unfiltered (f, "flag: TRUE");
17144 break;
a8329558 17145 case DW_FORM_indirect:
0963b4bd
MS
17146 /* The reader will have reduced the indirect form to
17147 the "base form" so this form should not occur. */
3e43a32a
MS
17148 fprintf_unfiltered (f,
17149 "unexpected attribute form: DW_FORM_indirect");
a8329558 17150 break;
c906108c 17151 default:
d97bc12b 17152 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17153 die->attrs[i].form);
d97bc12b 17154 break;
c906108c 17155 }
d97bc12b 17156 fprintf_unfiltered (f, "\n");
c906108c
SS
17157 }
17158}
17159
f9aca02d 17160static void
d97bc12b 17161dump_die_for_error (struct die_info *die)
c906108c 17162{
d97bc12b
DE
17163 dump_die_shallow (gdb_stderr, 0, die);
17164}
17165
17166static void
17167dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17168{
17169 int indent = level * 4;
17170
17171 gdb_assert (die != NULL);
17172
17173 if (level >= max_level)
17174 return;
17175
17176 dump_die_shallow (f, indent, die);
17177
17178 if (die->child != NULL)
c906108c 17179 {
d97bc12b
DE
17180 print_spaces (indent, f);
17181 fprintf_unfiltered (f, " Children:");
17182 if (level + 1 < max_level)
17183 {
17184 fprintf_unfiltered (f, "\n");
17185 dump_die_1 (f, level + 1, max_level, die->child);
17186 }
17187 else
17188 {
3e43a32a
MS
17189 fprintf_unfiltered (f,
17190 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17191 }
17192 }
17193
17194 if (die->sibling != NULL && level > 0)
17195 {
17196 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17197 }
17198}
17199
d97bc12b
DE
17200/* This is called from the pdie macro in gdbinit.in.
17201 It's not static so gcc will keep a copy callable from gdb. */
17202
17203void
17204dump_die (struct die_info *die, int max_level)
17205{
17206 dump_die_1 (gdb_stdlog, 0, max_level, die);
17207}
17208
f9aca02d 17209static void
51545339 17210store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17211{
51545339 17212 void **slot;
c906108c 17213
b64f50a1
JK
17214 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17215 INSERT);
51545339
DJ
17216
17217 *slot = die;
c906108c
SS
17218}
17219
b64f50a1
JK
17220/* DW_ADDR is always stored already as sect_offset; despite for the forms
17221 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17222
93311388
DE
17223static int
17224is_ref_attr (struct attribute *attr)
c906108c 17225{
c906108c
SS
17226 switch (attr->form)
17227 {
17228 case DW_FORM_ref_addr:
c906108c
SS
17229 case DW_FORM_ref1:
17230 case DW_FORM_ref2:
17231 case DW_FORM_ref4:
613e1657 17232 case DW_FORM_ref8:
c906108c 17233 case DW_FORM_ref_udata:
36586728 17234 case DW_FORM_GNU_ref_alt:
93311388 17235 return 1;
c906108c 17236 default:
93311388 17237 return 0;
c906108c 17238 }
93311388
DE
17239}
17240
b64f50a1
JK
17241/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17242 required kind. */
17243
17244static sect_offset
93311388
DE
17245dwarf2_get_ref_die_offset (struct attribute *attr)
17246{
4568ecf9 17247 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17248
93311388 17249 if (is_ref_attr (attr))
b64f50a1 17250 return retval;
93311388 17251
b64f50a1 17252 retval.sect_off = 0;
93311388
DE
17253 complaint (&symfile_complaints,
17254 _("unsupported die ref attribute form: '%s'"),
17255 dwarf_form_name (attr->form));
b64f50a1 17256 return retval;
c906108c
SS
17257}
17258
43bbcdc2
PH
17259/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17260 * the value held by the attribute is not constant. */
a02abb62 17261
43bbcdc2 17262static LONGEST
a02abb62
JB
17263dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17264{
17265 if (attr->form == DW_FORM_sdata)
17266 return DW_SND (attr);
17267 else if (attr->form == DW_FORM_udata
17268 || attr->form == DW_FORM_data1
17269 || attr->form == DW_FORM_data2
17270 || attr->form == DW_FORM_data4
17271 || attr->form == DW_FORM_data8)
17272 return DW_UNSND (attr);
17273 else
17274 {
3e43a32a
MS
17275 complaint (&symfile_complaints,
17276 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17277 dwarf_form_name (attr->form));
17278 return default_value;
17279 }
17280}
17281
348e048f
DE
17282/* Follow reference or signature attribute ATTR of SRC_DIE.
17283 On entry *REF_CU is the CU of SRC_DIE.
17284 On exit *REF_CU is the CU of the result. */
17285
17286static struct die_info *
17287follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17288 struct dwarf2_cu **ref_cu)
17289{
17290 struct die_info *die;
17291
17292 if (is_ref_attr (attr))
17293 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17294 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17295 die = follow_die_sig (src_die, attr, ref_cu);
17296 else
17297 {
17298 dump_die_for_error (src_die);
17299 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17300 (*ref_cu)->objfile->name);
17301 }
17302
17303 return die;
03dd20cc
DJ
17304}
17305
5c631832 17306/* Follow reference OFFSET.
673bfd45
DE
17307 On entry *REF_CU is the CU of the source die referencing OFFSET.
17308 On exit *REF_CU is the CU of the result.
17309 Returns NULL if OFFSET is invalid. */
f504f079 17310
f9aca02d 17311static struct die_info *
36586728
TT
17312follow_die_offset (sect_offset offset, int offset_in_dwz,
17313 struct dwarf2_cu **ref_cu)
c906108c 17314{
10b3939b 17315 struct die_info temp_die;
f2f0e013 17316 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17317
348e048f
DE
17318 gdb_assert (cu->per_cu != NULL);
17319
98bfdba5
PA
17320 target_cu = cu;
17321
3019eac3 17322 if (cu->per_cu->is_debug_types)
348e048f
DE
17323 {
17324 /* .debug_types CUs cannot reference anything outside their CU.
17325 If they need to, they have to reference a signatured type via
55f1336d 17326 DW_FORM_ref_sig8. */
348e048f 17327 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17328 return NULL;
348e048f 17329 }
36586728
TT
17330 else if (offset_in_dwz != cu->per_cu->is_dwz
17331 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17332 {
17333 struct dwarf2_per_cu_data *per_cu;
9a619af0 17334
36586728
TT
17335 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17336 cu->objfile);
03dd20cc
DJ
17337
17338 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17339 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17340 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17341
10b3939b
DJ
17342 target_cu = per_cu->cu;
17343 }
98bfdba5
PA
17344 else if (cu->dies == NULL)
17345 {
17346 /* We're loading full DIEs during partial symbol reading. */
17347 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17348 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17349 }
c906108c 17350
f2f0e013 17351 *ref_cu = target_cu;
51545339 17352 temp_die.offset = offset;
b64f50a1 17353 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17354}
10b3939b 17355
5c631832
JK
17356/* Follow reference attribute ATTR of SRC_DIE.
17357 On entry *REF_CU is the CU of SRC_DIE.
17358 On exit *REF_CU is the CU of the result. */
17359
17360static struct die_info *
17361follow_die_ref (struct die_info *src_die, struct attribute *attr,
17362 struct dwarf2_cu **ref_cu)
17363{
b64f50a1 17364 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17365 struct dwarf2_cu *cu = *ref_cu;
17366 struct die_info *die;
17367
36586728
TT
17368 die = follow_die_offset (offset,
17369 (attr->form == DW_FORM_GNU_ref_alt
17370 || cu->per_cu->is_dwz),
17371 ref_cu);
5c631832
JK
17372 if (!die)
17373 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17374 "at 0x%x [in module %s]"),
b64f50a1 17375 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17376
5c631832
JK
17377 return die;
17378}
17379
d83e736b
JK
17380/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17381 Returned value is intended for DW_OP_call*. Returned
17382 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17383
17384struct dwarf2_locexpr_baton
8b9737bf
TT
17385dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17386 struct dwarf2_per_cu_data *per_cu,
17387 CORE_ADDR (*get_frame_pc) (void *baton),
17388 void *baton)
5c631832 17389{
918dd910 17390 struct dwarf2_cu *cu;
5c631832
JK
17391 struct die_info *die;
17392 struct attribute *attr;
17393 struct dwarf2_locexpr_baton retval;
17394
8cf6f0b1
TT
17395 dw2_setup (per_cu->objfile);
17396
918dd910
JK
17397 if (per_cu->cu == NULL)
17398 load_cu (per_cu);
17399 cu = per_cu->cu;
17400
36586728 17401 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17402 if (!die)
17403 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17404 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17405
17406 attr = dwarf2_attr (die, DW_AT_location, cu);
17407 if (!attr)
17408 {
e103e986
JK
17409 /* DWARF: "If there is no such attribute, then there is no effect.".
17410 DATA is ignored if SIZE is 0. */
5c631832 17411
e103e986 17412 retval.data = NULL;
5c631832
JK
17413 retval.size = 0;
17414 }
8cf6f0b1
TT
17415 else if (attr_form_is_section_offset (attr))
17416 {
17417 struct dwarf2_loclist_baton loclist_baton;
17418 CORE_ADDR pc = (*get_frame_pc) (baton);
17419 size_t size;
17420
17421 fill_in_loclist_baton (cu, &loclist_baton, attr);
17422
17423 retval.data = dwarf2_find_location_expression (&loclist_baton,
17424 &size, pc);
17425 retval.size = size;
17426 }
5c631832
JK
17427 else
17428 {
17429 if (!attr_form_is_block (attr))
17430 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17431 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17432 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17433
17434 retval.data = DW_BLOCK (attr)->data;
17435 retval.size = DW_BLOCK (attr)->size;
17436 }
17437 retval.per_cu = cu->per_cu;
918dd910 17438
918dd910
JK
17439 age_cached_comp_units ();
17440
5c631832 17441 return retval;
348e048f
DE
17442}
17443
8b9737bf
TT
17444/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17445 offset. */
17446
17447struct dwarf2_locexpr_baton
17448dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17449 struct dwarf2_per_cu_data *per_cu,
17450 CORE_ADDR (*get_frame_pc) (void *baton),
17451 void *baton)
17452{
17453 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17454
17455 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17456}
17457
8a9b8146
TT
17458/* Return the type of the DIE at DIE_OFFSET in the CU named by
17459 PER_CU. */
17460
17461struct type *
b64f50a1 17462dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17463 struct dwarf2_per_cu_data *per_cu)
17464{
b64f50a1
JK
17465 sect_offset die_offset_sect;
17466
8a9b8146 17467 dw2_setup (per_cu->objfile);
b64f50a1
JK
17468
17469 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17470 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17471}
17472
348e048f
DE
17473/* Follow the signature attribute ATTR in SRC_DIE.
17474 On entry *REF_CU is the CU of SRC_DIE.
17475 On exit *REF_CU is the CU of the result. */
17476
17477static struct die_info *
17478follow_die_sig (struct die_info *src_die, struct attribute *attr,
17479 struct dwarf2_cu **ref_cu)
17480{
17481 struct objfile *objfile = (*ref_cu)->objfile;
17482 struct die_info temp_die;
17483 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17484 struct dwarf2_cu *sig_cu;
17485 struct die_info *die;
17486
17487 /* sig_type will be NULL if the signatured type is missing from
17488 the debug info. */
17489 if (sig_type == NULL)
17490 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17491 "at 0x%x [in module %s]"),
b64f50a1 17492 src_die->offset.sect_off, objfile->name);
348e048f
DE
17493
17494 /* If necessary, add it to the queue and load its DIEs. */
17495
95554aad 17496 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17497 read_signatured_type (sig_type);
348e048f
DE
17498
17499 gdb_assert (sig_type->per_cu.cu != NULL);
17500
17501 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17502 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17503 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17504 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17505 temp_die.offset.sect_off);
348e048f
DE
17506 if (die)
17507 {
17508 *ref_cu = sig_cu;
17509 return die;
17510 }
17511
3e43a32a
MS
17512 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17513 "from DIE at 0x%x [in module %s]"),
b64f50a1 17514 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17515}
17516
17517/* Given an offset of a signatured type, return its signatured_type. */
17518
17519static struct signatured_type *
8b70b953
TT
17520lookup_signatured_type_at_offset (struct objfile *objfile,
17521 struct dwarf2_section_info *section,
b64f50a1 17522 sect_offset offset)
348e048f 17523{
b64f50a1 17524 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17525 unsigned int length, initial_length_size;
17526 unsigned int sig_offset;
52dc124a 17527 struct signatured_type find_entry, *sig_type;
348e048f
DE
17528
17529 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17530 sig_offset = (initial_length_size
17531 + 2 /*version*/
17532 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17533 + 1 /*address_size*/);
17534 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17535 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17536
17537 /* This is only used to lookup previously recorded types.
17538 If we didn't find it, it's our bug. */
52dc124a
DE
17539 gdb_assert (sig_type != NULL);
17540 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17541
52dc124a 17542 return sig_type;
348e048f
DE
17543}
17544
e5fe5e75 17545/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17546
17547static void
e5fe5e75 17548load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17549{
52dc124a 17550 struct signatured_type *sig_type;
348e048f 17551
f4dc4d17
DE
17552 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17553 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17554
6721b2ec
DE
17555 /* We have the per_cu, but we need the signatured_type.
17556 Fortunately this is an easy translation. */
17557 gdb_assert (per_cu->is_debug_types);
17558 sig_type = (struct signatured_type *) per_cu;
348e048f 17559
6721b2ec 17560 gdb_assert (per_cu->cu == NULL);
348e048f 17561
52dc124a 17562 read_signatured_type (sig_type);
348e048f 17563
6721b2ec 17564 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17565}
17566
dee91e82
DE
17567/* die_reader_func for read_signatured_type.
17568 This is identical to load_full_comp_unit_reader,
17569 but is kept separate for now. */
348e048f
DE
17570
17571static void
dee91e82
DE
17572read_signatured_type_reader (const struct die_reader_specs *reader,
17573 gdb_byte *info_ptr,
17574 struct die_info *comp_unit_die,
17575 int has_children,
17576 void *data)
348e048f 17577{
dee91e82 17578 struct dwarf2_cu *cu = reader->cu;
348e048f 17579
dee91e82
DE
17580 gdb_assert (cu->die_hash == NULL);
17581 cu->die_hash =
17582 htab_create_alloc_ex (cu->header.length / 12,
17583 die_hash,
17584 die_eq,
17585 NULL,
17586 &cu->comp_unit_obstack,
17587 hashtab_obstack_allocate,
17588 dummy_obstack_deallocate);
348e048f 17589
dee91e82
DE
17590 if (has_children)
17591 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17592 &info_ptr, comp_unit_die);
17593 cu->dies = comp_unit_die;
17594 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17595
17596 /* We try not to read any attributes in this function, because not
9cdd5dbd 17597 all CUs needed for references have been loaded yet, and symbol
348e048f 17598 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17599 or we won't be able to build types correctly.
17600 Similarly, if we do not read the producer, we can not apply
17601 producer-specific interpretation. */
95554aad 17602 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17603}
348e048f 17604
3019eac3
DE
17605/* Read in a signatured type and build its CU and DIEs.
17606 If the type is a stub for the real type in a DWO file,
17607 read in the real type from the DWO file as well. */
dee91e82
DE
17608
17609static void
17610read_signatured_type (struct signatured_type *sig_type)
17611{
17612 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17613
3019eac3 17614 gdb_assert (per_cu->is_debug_types);
dee91e82 17615 gdb_assert (per_cu->cu == NULL);
348e048f 17616
f4dc4d17
DE
17617 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17618 read_signatured_type_reader, NULL);
c906108c
SS
17619}
17620
c906108c
SS
17621/* Decode simple location descriptions.
17622 Given a pointer to a dwarf block that defines a location, compute
17623 the location and return the value.
17624
4cecd739
DJ
17625 NOTE drow/2003-11-18: This function is called in two situations
17626 now: for the address of static or global variables (partial symbols
17627 only) and for offsets into structures which are expected to be
17628 (more or less) constant. The partial symbol case should go away,
17629 and only the constant case should remain. That will let this
17630 function complain more accurately. A few special modes are allowed
17631 without complaint for global variables (for instance, global
17632 register values and thread-local values).
c906108c
SS
17633
17634 A location description containing no operations indicates that the
4cecd739 17635 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17636 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17637 callers will only want a very basic result and this can become a
21ae7a4d
JK
17638 complaint.
17639
17640 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17641
17642static CORE_ADDR
e7c27a73 17643decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17644{
e7c27a73 17645 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17646 size_t i;
17647 size_t size = blk->size;
21ae7a4d
JK
17648 gdb_byte *data = blk->data;
17649 CORE_ADDR stack[64];
17650 int stacki;
17651 unsigned int bytes_read, unsnd;
17652 gdb_byte op;
c906108c 17653
21ae7a4d
JK
17654 i = 0;
17655 stacki = 0;
17656 stack[stacki] = 0;
17657 stack[++stacki] = 0;
17658
17659 while (i < size)
17660 {
17661 op = data[i++];
17662 switch (op)
17663 {
17664 case DW_OP_lit0:
17665 case DW_OP_lit1:
17666 case DW_OP_lit2:
17667 case DW_OP_lit3:
17668 case DW_OP_lit4:
17669 case DW_OP_lit5:
17670 case DW_OP_lit6:
17671 case DW_OP_lit7:
17672 case DW_OP_lit8:
17673 case DW_OP_lit9:
17674 case DW_OP_lit10:
17675 case DW_OP_lit11:
17676 case DW_OP_lit12:
17677 case DW_OP_lit13:
17678 case DW_OP_lit14:
17679 case DW_OP_lit15:
17680 case DW_OP_lit16:
17681 case DW_OP_lit17:
17682 case DW_OP_lit18:
17683 case DW_OP_lit19:
17684 case DW_OP_lit20:
17685 case DW_OP_lit21:
17686 case DW_OP_lit22:
17687 case DW_OP_lit23:
17688 case DW_OP_lit24:
17689 case DW_OP_lit25:
17690 case DW_OP_lit26:
17691 case DW_OP_lit27:
17692 case DW_OP_lit28:
17693 case DW_OP_lit29:
17694 case DW_OP_lit30:
17695 case DW_OP_lit31:
17696 stack[++stacki] = op - DW_OP_lit0;
17697 break;
f1bea926 17698
21ae7a4d
JK
17699 case DW_OP_reg0:
17700 case DW_OP_reg1:
17701 case DW_OP_reg2:
17702 case DW_OP_reg3:
17703 case DW_OP_reg4:
17704 case DW_OP_reg5:
17705 case DW_OP_reg6:
17706 case DW_OP_reg7:
17707 case DW_OP_reg8:
17708 case DW_OP_reg9:
17709 case DW_OP_reg10:
17710 case DW_OP_reg11:
17711 case DW_OP_reg12:
17712 case DW_OP_reg13:
17713 case DW_OP_reg14:
17714 case DW_OP_reg15:
17715 case DW_OP_reg16:
17716 case DW_OP_reg17:
17717 case DW_OP_reg18:
17718 case DW_OP_reg19:
17719 case DW_OP_reg20:
17720 case DW_OP_reg21:
17721 case DW_OP_reg22:
17722 case DW_OP_reg23:
17723 case DW_OP_reg24:
17724 case DW_OP_reg25:
17725 case DW_OP_reg26:
17726 case DW_OP_reg27:
17727 case DW_OP_reg28:
17728 case DW_OP_reg29:
17729 case DW_OP_reg30:
17730 case DW_OP_reg31:
17731 stack[++stacki] = op - DW_OP_reg0;
17732 if (i < size)
17733 dwarf2_complex_location_expr_complaint ();
17734 break;
c906108c 17735
21ae7a4d
JK
17736 case DW_OP_regx:
17737 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17738 i += bytes_read;
17739 stack[++stacki] = unsnd;
17740 if (i < size)
17741 dwarf2_complex_location_expr_complaint ();
17742 break;
c906108c 17743
21ae7a4d
JK
17744 case DW_OP_addr:
17745 stack[++stacki] = read_address (objfile->obfd, &data[i],
17746 cu, &bytes_read);
17747 i += bytes_read;
17748 break;
d53d4ac5 17749
21ae7a4d
JK
17750 case DW_OP_const1u:
17751 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17752 i += 1;
17753 break;
17754
17755 case DW_OP_const1s:
17756 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17757 i += 1;
17758 break;
17759
17760 case DW_OP_const2u:
17761 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17762 i += 2;
17763 break;
17764
17765 case DW_OP_const2s:
17766 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17767 i += 2;
17768 break;
d53d4ac5 17769
21ae7a4d
JK
17770 case DW_OP_const4u:
17771 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17772 i += 4;
17773 break;
17774
17775 case DW_OP_const4s:
17776 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17777 i += 4;
17778 break;
17779
585861ea
JK
17780 case DW_OP_const8u:
17781 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17782 i += 8;
17783 break;
17784
21ae7a4d
JK
17785 case DW_OP_constu:
17786 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17787 &bytes_read);
17788 i += bytes_read;
17789 break;
17790
17791 case DW_OP_consts:
17792 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17793 i += bytes_read;
17794 break;
17795
17796 case DW_OP_dup:
17797 stack[stacki + 1] = stack[stacki];
17798 stacki++;
17799 break;
17800
17801 case DW_OP_plus:
17802 stack[stacki - 1] += stack[stacki];
17803 stacki--;
17804 break;
17805
17806 case DW_OP_plus_uconst:
17807 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17808 &bytes_read);
17809 i += bytes_read;
17810 break;
17811
17812 case DW_OP_minus:
17813 stack[stacki - 1] -= stack[stacki];
17814 stacki--;
17815 break;
17816
17817 case DW_OP_deref:
17818 /* If we're not the last op, then we definitely can't encode
17819 this using GDB's address_class enum. This is valid for partial
17820 global symbols, although the variable's address will be bogus
17821 in the psymtab. */
17822 if (i < size)
17823 dwarf2_complex_location_expr_complaint ();
17824 break;
17825
17826 case DW_OP_GNU_push_tls_address:
17827 /* The top of the stack has the offset from the beginning
17828 of the thread control block at which the variable is located. */
17829 /* Nothing should follow this operator, so the top of stack would
17830 be returned. */
17831 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17832 address will be bogus in the psymtab. Make it always at least
17833 non-zero to not look as a variable garbage collected by linker
17834 which have DW_OP_addr 0. */
21ae7a4d
JK
17835 if (i < size)
17836 dwarf2_complex_location_expr_complaint ();
585861ea 17837 stack[stacki]++;
21ae7a4d
JK
17838 break;
17839
17840 case DW_OP_GNU_uninit:
17841 break;
17842
3019eac3 17843 case DW_OP_GNU_addr_index:
49f6c839 17844 case DW_OP_GNU_const_index:
3019eac3
DE
17845 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17846 &bytes_read);
17847 i += bytes_read;
17848 break;
17849
21ae7a4d
JK
17850 default:
17851 {
f39c6ffd 17852 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17853
17854 if (name)
17855 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17856 name);
17857 else
17858 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17859 op);
17860 }
17861
17862 return (stack[stacki]);
d53d4ac5 17863 }
3c6e0cb3 17864
21ae7a4d
JK
17865 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17866 outside of the allocated space. Also enforce minimum>0. */
17867 if (stacki >= ARRAY_SIZE (stack) - 1)
17868 {
17869 complaint (&symfile_complaints,
17870 _("location description stack overflow"));
17871 return 0;
17872 }
17873
17874 if (stacki <= 0)
17875 {
17876 complaint (&symfile_complaints,
17877 _("location description stack underflow"));
17878 return 0;
17879 }
17880 }
17881 return (stack[stacki]);
c906108c
SS
17882}
17883
17884/* memory allocation interface */
17885
c906108c 17886static struct dwarf_block *
7b5a2f43 17887dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17888{
17889 struct dwarf_block *blk;
17890
17891 blk = (struct dwarf_block *)
7b5a2f43 17892 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17893 return (blk);
17894}
17895
c906108c 17896static struct die_info *
b60c80d6 17897dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17898{
17899 struct die_info *die;
b60c80d6
DJ
17900 size_t size = sizeof (struct die_info);
17901
17902 if (num_attrs > 1)
17903 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17904
b60c80d6 17905 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17906 memset (die, 0, sizeof (struct die_info));
17907 return (die);
17908}
2e276125
JB
17909
17910\f
17911/* Macro support. */
17912
2e276125
JB
17913/* Return the full name of file number I in *LH's file name table.
17914 Use COMP_DIR as the name of the current directory of the
17915 compilation. The result is allocated using xmalloc; the caller is
17916 responsible for freeing it. */
17917static char *
17918file_full_name (int file, struct line_header *lh, const char *comp_dir)
17919{
6a83a1e6
EZ
17920 /* Is the file number a valid index into the line header's file name
17921 table? Remember that file numbers start with one, not zero. */
17922 if (1 <= file && file <= lh->num_file_names)
17923 {
17924 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17925
6a83a1e6
EZ
17926 if (IS_ABSOLUTE_PATH (fe->name))
17927 return xstrdup (fe->name);
17928 else
17929 {
17930 const char *dir;
17931 int dir_len;
17932 char *full_name;
17933
17934 if (fe->dir_index)
17935 dir = lh->include_dirs[fe->dir_index - 1];
17936 else
17937 dir = comp_dir;
17938
17939 if (dir)
17940 {
17941 dir_len = strlen (dir);
17942 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17943 strcpy (full_name, dir);
17944 full_name[dir_len] = '/';
17945 strcpy (full_name + dir_len + 1, fe->name);
17946 return full_name;
17947 }
17948 else
17949 return xstrdup (fe->name);
17950 }
17951 }
2e276125
JB
17952 else
17953 {
6a83a1e6
EZ
17954 /* The compiler produced a bogus file number. We can at least
17955 record the macro definitions made in the file, even if we
17956 won't be able to find the file by name. */
17957 char fake_name[80];
9a619af0 17958
8c042590
PM
17959 xsnprintf (fake_name, sizeof (fake_name),
17960 "<bad macro file number %d>", file);
2e276125 17961
6e70227d 17962 complaint (&symfile_complaints,
6a83a1e6
EZ
17963 _("bad file number in macro information (%d)"),
17964 file);
2e276125 17965
6a83a1e6 17966 return xstrdup (fake_name);
2e276125
JB
17967 }
17968}
17969
17970
17971static struct macro_source_file *
17972macro_start_file (int file, int line,
17973 struct macro_source_file *current_file,
17974 const char *comp_dir,
17975 struct line_header *lh, struct objfile *objfile)
17976{
17977 /* The full name of this source file. */
17978 char *full_name = file_full_name (file, lh, comp_dir);
17979
17980 /* We don't create a macro table for this compilation unit
17981 at all until we actually get a filename. */
17982 if (! pending_macros)
6532ff36
TT
17983 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17984 objfile->per_bfd->macro_cache);
2e276125
JB
17985
17986 if (! current_file)
abc9d0dc
TT
17987 {
17988 /* If we have no current file, then this must be the start_file
17989 directive for the compilation unit's main source file. */
17990 current_file = macro_set_main (pending_macros, full_name);
17991 macro_define_special (pending_macros);
17992 }
2e276125
JB
17993 else
17994 current_file = macro_include (current_file, line, full_name);
17995
17996 xfree (full_name);
6e70227d 17997
2e276125
JB
17998 return current_file;
17999}
18000
18001
18002/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18003 followed by a null byte. */
18004static char *
18005copy_string (const char *buf, int len)
18006{
18007 char *s = xmalloc (len + 1);
9a619af0 18008
2e276125
JB
18009 memcpy (s, buf, len);
18010 s[len] = '\0';
2e276125
JB
18011 return s;
18012}
18013
18014
18015static const char *
18016consume_improper_spaces (const char *p, const char *body)
18017{
18018 if (*p == ' ')
18019 {
4d3c2250 18020 complaint (&symfile_complaints,
3e43a32a
MS
18021 _("macro definition contains spaces "
18022 "in formal argument list:\n`%s'"),
4d3c2250 18023 body);
2e276125
JB
18024
18025 while (*p == ' ')
18026 p++;
18027 }
18028
18029 return p;
18030}
18031
18032
18033static void
18034parse_macro_definition (struct macro_source_file *file, int line,
18035 const char *body)
18036{
18037 const char *p;
18038
18039 /* The body string takes one of two forms. For object-like macro
18040 definitions, it should be:
18041
18042 <macro name> " " <definition>
18043
18044 For function-like macro definitions, it should be:
18045
18046 <macro name> "() " <definition>
18047 or
18048 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18049
18050 Spaces may appear only where explicitly indicated, and in the
18051 <definition>.
18052
18053 The Dwarf 2 spec says that an object-like macro's name is always
18054 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18055 the space when the macro's definition is the empty string.
2e276125
JB
18056
18057 The Dwarf 2 spec says that there should be no spaces between the
18058 formal arguments in a function-like macro's formal argument list,
18059 but versions of GCC around March 2002 include spaces after the
18060 commas. */
18061
18062
18063 /* Find the extent of the macro name. The macro name is terminated
18064 by either a space or null character (for an object-like macro) or
18065 an opening paren (for a function-like macro). */
18066 for (p = body; *p; p++)
18067 if (*p == ' ' || *p == '(')
18068 break;
18069
18070 if (*p == ' ' || *p == '\0')
18071 {
18072 /* It's an object-like macro. */
18073 int name_len = p - body;
18074 char *name = copy_string (body, name_len);
18075 const char *replacement;
18076
18077 if (*p == ' ')
18078 replacement = body + name_len + 1;
18079 else
18080 {
4d3c2250 18081 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18082 replacement = body + name_len;
18083 }
6e70227d 18084
2e276125
JB
18085 macro_define_object (file, line, name, replacement);
18086
18087 xfree (name);
18088 }
18089 else if (*p == '(')
18090 {
18091 /* It's a function-like macro. */
18092 char *name = copy_string (body, p - body);
18093 int argc = 0;
18094 int argv_size = 1;
18095 char **argv = xmalloc (argv_size * sizeof (*argv));
18096
18097 p++;
18098
18099 p = consume_improper_spaces (p, body);
18100
18101 /* Parse the formal argument list. */
18102 while (*p && *p != ')')
18103 {
18104 /* Find the extent of the current argument name. */
18105 const char *arg_start = p;
18106
18107 while (*p && *p != ',' && *p != ')' && *p != ' ')
18108 p++;
18109
18110 if (! *p || p == arg_start)
4d3c2250 18111 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18112 else
18113 {
18114 /* Make sure argv has room for the new argument. */
18115 if (argc >= argv_size)
18116 {
18117 argv_size *= 2;
18118 argv = xrealloc (argv, argv_size * sizeof (*argv));
18119 }
18120
18121 argv[argc++] = copy_string (arg_start, p - arg_start);
18122 }
18123
18124 p = consume_improper_spaces (p, body);
18125
18126 /* Consume the comma, if present. */
18127 if (*p == ',')
18128 {
18129 p++;
18130
18131 p = consume_improper_spaces (p, body);
18132 }
18133 }
18134
18135 if (*p == ')')
18136 {
18137 p++;
18138
18139 if (*p == ' ')
18140 /* Perfectly formed definition, no complaints. */
18141 macro_define_function (file, line, name,
6e70227d 18142 argc, (const char **) argv,
2e276125
JB
18143 p + 1);
18144 else if (*p == '\0')
18145 {
18146 /* Complain, but do define it. */
4d3c2250 18147 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18148 macro_define_function (file, line, name,
6e70227d 18149 argc, (const char **) argv,
2e276125
JB
18150 p);
18151 }
18152 else
18153 /* Just complain. */
4d3c2250 18154 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18155 }
18156 else
18157 /* Just complain. */
4d3c2250 18158 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18159
18160 xfree (name);
18161 {
18162 int i;
18163
18164 for (i = 0; i < argc; i++)
18165 xfree (argv[i]);
18166 }
18167 xfree (argv);
18168 }
18169 else
4d3c2250 18170 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18171}
18172
cf2c3c16
TT
18173/* Skip some bytes from BYTES according to the form given in FORM.
18174 Returns the new pointer. */
2e276125 18175
cf2c3c16 18176static gdb_byte *
f664829e 18177skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18178 enum dwarf_form form,
18179 unsigned int offset_size,
18180 struct dwarf2_section_info *section)
2e276125 18181{
cf2c3c16 18182 unsigned int bytes_read;
2e276125 18183
cf2c3c16 18184 switch (form)
2e276125 18185 {
cf2c3c16
TT
18186 case DW_FORM_data1:
18187 case DW_FORM_flag:
18188 ++bytes;
18189 break;
18190
18191 case DW_FORM_data2:
18192 bytes += 2;
18193 break;
18194
18195 case DW_FORM_data4:
18196 bytes += 4;
18197 break;
18198
18199 case DW_FORM_data8:
18200 bytes += 8;
18201 break;
18202
18203 case DW_FORM_string:
18204 read_direct_string (abfd, bytes, &bytes_read);
18205 bytes += bytes_read;
18206 break;
18207
18208 case DW_FORM_sec_offset:
18209 case DW_FORM_strp:
36586728 18210 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18211 bytes += offset_size;
18212 break;
18213
18214 case DW_FORM_block:
18215 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18216 bytes += bytes_read;
18217 break;
18218
18219 case DW_FORM_block1:
18220 bytes += 1 + read_1_byte (abfd, bytes);
18221 break;
18222 case DW_FORM_block2:
18223 bytes += 2 + read_2_bytes (abfd, bytes);
18224 break;
18225 case DW_FORM_block4:
18226 bytes += 4 + read_4_bytes (abfd, bytes);
18227 break;
18228
18229 case DW_FORM_sdata:
18230 case DW_FORM_udata:
3019eac3
DE
18231 case DW_FORM_GNU_addr_index:
18232 case DW_FORM_GNU_str_index:
f664829e
DE
18233 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18234 if (bytes == NULL)
18235 {
18236 dwarf2_section_buffer_overflow_complaint (section);
18237 return NULL;
18238 }
cf2c3c16
TT
18239 break;
18240
18241 default:
18242 {
18243 complain:
18244 complaint (&symfile_complaints,
18245 _("invalid form 0x%x in `%s'"),
18246 form,
18247 section->asection->name);
18248 return NULL;
18249 }
2e276125
JB
18250 }
18251
cf2c3c16
TT
18252 return bytes;
18253}
757a13d0 18254
cf2c3c16
TT
18255/* A helper for dwarf_decode_macros that handles skipping an unknown
18256 opcode. Returns an updated pointer to the macro data buffer; or,
18257 on error, issues a complaint and returns NULL. */
757a13d0 18258
cf2c3c16
TT
18259static gdb_byte *
18260skip_unknown_opcode (unsigned int opcode,
18261 gdb_byte **opcode_definitions,
f664829e 18262 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18263 bfd *abfd,
18264 unsigned int offset_size,
18265 struct dwarf2_section_info *section)
18266{
18267 unsigned int bytes_read, i;
18268 unsigned long arg;
18269 gdb_byte *defn;
2e276125 18270
cf2c3c16 18271 if (opcode_definitions[opcode] == NULL)
2e276125 18272 {
cf2c3c16
TT
18273 complaint (&symfile_complaints,
18274 _("unrecognized DW_MACFINO opcode 0x%x"),
18275 opcode);
18276 return NULL;
18277 }
2e276125 18278
cf2c3c16
TT
18279 defn = opcode_definitions[opcode];
18280 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18281 defn += bytes_read;
2e276125 18282
cf2c3c16
TT
18283 for (i = 0; i < arg; ++i)
18284 {
f664829e
DE
18285 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18286 section);
cf2c3c16
TT
18287 if (mac_ptr == NULL)
18288 {
18289 /* skip_form_bytes already issued the complaint. */
18290 return NULL;
18291 }
18292 }
757a13d0 18293
cf2c3c16
TT
18294 return mac_ptr;
18295}
757a13d0 18296
cf2c3c16
TT
18297/* A helper function which parses the header of a macro section.
18298 If the macro section is the extended (for now called "GNU") type,
18299 then this updates *OFFSET_SIZE. Returns a pointer to just after
18300 the header, or issues a complaint and returns NULL on error. */
757a13d0 18301
cf2c3c16
TT
18302static gdb_byte *
18303dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18304 bfd *abfd,
18305 gdb_byte *mac_ptr,
18306 unsigned int *offset_size,
18307 int section_is_gnu)
18308{
18309 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18310
cf2c3c16
TT
18311 if (section_is_gnu)
18312 {
18313 unsigned int version, flags;
757a13d0 18314
cf2c3c16
TT
18315 version = read_2_bytes (abfd, mac_ptr);
18316 if (version != 4)
18317 {
18318 complaint (&symfile_complaints,
18319 _("unrecognized version `%d' in .debug_macro section"),
18320 version);
18321 return NULL;
18322 }
18323 mac_ptr += 2;
757a13d0 18324
cf2c3c16
TT
18325 flags = read_1_byte (abfd, mac_ptr);
18326 ++mac_ptr;
18327 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18328
cf2c3c16
TT
18329 if ((flags & 2) != 0)
18330 /* We don't need the line table offset. */
18331 mac_ptr += *offset_size;
757a13d0 18332
cf2c3c16
TT
18333 /* Vendor opcode descriptions. */
18334 if ((flags & 4) != 0)
18335 {
18336 unsigned int i, count;
757a13d0 18337
cf2c3c16
TT
18338 count = read_1_byte (abfd, mac_ptr);
18339 ++mac_ptr;
18340 for (i = 0; i < count; ++i)
18341 {
18342 unsigned int opcode, bytes_read;
18343 unsigned long arg;
18344
18345 opcode = read_1_byte (abfd, mac_ptr);
18346 ++mac_ptr;
18347 opcode_definitions[opcode] = mac_ptr;
18348 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18349 mac_ptr += bytes_read;
18350 mac_ptr += arg;
18351 }
757a13d0 18352 }
cf2c3c16 18353 }
757a13d0 18354
cf2c3c16
TT
18355 return mac_ptr;
18356}
757a13d0 18357
cf2c3c16 18358/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18359 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18360
18361static void
18362dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18363 struct macro_source_file *current_file,
18364 struct line_header *lh, char *comp_dir,
18365 struct dwarf2_section_info *section,
36586728 18366 int section_is_gnu, int section_is_dwz,
cf2c3c16 18367 unsigned int offset_size,
8fc3fc34
TT
18368 struct objfile *objfile,
18369 htab_t include_hash)
cf2c3c16
TT
18370{
18371 enum dwarf_macro_record_type macinfo_type;
18372 int at_commandline;
18373 gdb_byte *opcode_definitions[256];
757a13d0 18374
cf2c3c16
TT
18375 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18376 &offset_size, section_is_gnu);
18377 if (mac_ptr == NULL)
18378 {
18379 /* We already issued a complaint. */
18380 return;
18381 }
757a13d0
JK
18382
18383 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18384 GDB is still reading the definitions from command line. First
18385 DW_MACINFO_start_file will need to be ignored as it was already executed
18386 to create CURRENT_FILE for the main source holding also the command line
18387 definitions. On first met DW_MACINFO_start_file this flag is reset to
18388 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18389
18390 at_commandline = 1;
18391
18392 do
18393 {
18394 /* Do we at least have room for a macinfo type byte? */
18395 if (mac_ptr >= mac_end)
18396 {
f664829e 18397 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18398 break;
18399 }
18400
18401 macinfo_type = read_1_byte (abfd, mac_ptr);
18402 mac_ptr++;
18403
cf2c3c16
TT
18404 /* Note that we rely on the fact that the corresponding GNU and
18405 DWARF constants are the same. */
757a13d0
JK
18406 switch (macinfo_type)
18407 {
18408 /* A zero macinfo type indicates the end of the macro
18409 information. */
18410 case 0:
18411 break;
2e276125 18412
cf2c3c16
TT
18413 case DW_MACRO_GNU_define:
18414 case DW_MACRO_GNU_undef:
18415 case DW_MACRO_GNU_define_indirect:
18416 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18417 case DW_MACRO_GNU_define_indirect_alt:
18418 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18419 {
891d2f0b 18420 unsigned int bytes_read;
2e276125
JB
18421 int line;
18422 char *body;
cf2c3c16 18423 int is_define;
2e276125 18424
cf2c3c16
TT
18425 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18426 mac_ptr += bytes_read;
18427
18428 if (macinfo_type == DW_MACRO_GNU_define
18429 || macinfo_type == DW_MACRO_GNU_undef)
18430 {
18431 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18432 mac_ptr += bytes_read;
18433 }
18434 else
18435 {
18436 LONGEST str_offset;
18437
18438 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18439 mac_ptr += offset_size;
2e276125 18440
36586728 18441 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18442 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18443 || section_is_dwz)
36586728
TT
18444 {
18445 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18446
18447 body = read_indirect_string_from_dwz (dwz, str_offset);
18448 }
18449 else
18450 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18451 }
18452
18453 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18454 || macinfo_type == DW_MACRO_GNU_define_indirect
18455 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18456 if (! current_file)
757a13d0
JK
18457 {
18458 /* DWARF violation as no main source is present. */
18459 complaint (&symfile_complaints,
18460 _("debug info with no main source gives macro %s "
18461 "on line %d: %s"),
cf2c3c16
TT
18462 is_define ? _("definition") : _("undefinition"),
18463 line, body);
757a13d0
JK
18464 break;
18465 }
3e43a32a
MS
18466 if ((line == 0 && !at_commandline)
18467 || (line != 0 && at_commandline))
4d3c2250 18468 complaint (&symfile_complaints,
757a13d0
JK
18469 _("debug info gives %s macro %s with %s line %d: %s"),
18470 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18471 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18472 line == 0 ? _("zero") : _("non-zero"), line, body);
18473
cf2c3c16 18474 if (is_define)
757a13d0 18475 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18476 else
18477 {
18478 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18479 || macinfo_type == DW_MACRO_GNU_undef_indirect
18480 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18481 macro_undef (current_file, line, body);
18482 }
2e276125
JB
18483 }
18484 break;
18485
cf2c3c16 18486 case DW_MACRO_GNU_start_file:
2e276125 18487 {
891d2f0b 18488 unsigned int bytes_read;
2e276125
JB
18489 int line, file;
18490
18491 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18492 mac_ptr += bytes_read;
18493 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18494 mac_ptr += bytes_read;
18495
3e43a32a
MS
18496 if ((line == 0 && !at_commandline)
18497 || (line != 0 && at_commandline))
757a13d0
JK
18498 complaint (&symfile_complaints,
18499 _("debug info gives source %d included "
18500 "from %s at %s line %d"),
18501 file, at_commandline ? _("command-line") : _("file"),
18502 line == 0 ? _("zero") : _("non-zero"), line);
18503
18504 if (at_commandline)
18505 {
cf2c3c16
TT
18506 /* This DW_MACRO_GNU_start_file was executed in the
18507 pass one. */
757a13d0
JK
18508 at_commandline = 0;
18509 }
18510 else
18511 current_file = macro_start_file (file, line,
18512 current_file, comp_dir,
cf2c3c16 18513 lh, objfile);
2e276125
JB
18514 }
18515 break;
18516
cf2c3c16 18517 case DW_MACRO_GNU_end_file:
2e276125 18518 if (! current_file)
4d3c2250 18519 complaint (&symfile_complaints,
3e43a32a
MS
18520 _("macro debug info has an unmatched "
18521 "`close_file' directive"));
2e276125
JB
18522 else
18523 {
18524 current_file = current_file->included_by;
18525 if (! current_file)
18526 {
cf2c3c16 18527 enum dwarf_macro_record_type next_type;
2e276125
JB
18528
18529 /* GCC circa March 2002 doesn't produce the zero
18530 type byte marking the end of the compilation
18531 unit. Complain if it's not there, but exit no
18532 matter what. */
18533
18534 /* Do we at least have room for a macinfo type byte? */
18535 if (mac_ptr >= mac_end)
18536 {
f664829e 18537 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18538 return;
18539 }
18540
18541 /* We don't increment mac_ptr here, so this is just
18542 a look-ahead. */
18543 next_type = read_1_byte (abfd, mac_ptr);
18544 if (next_type != 0)
4d3c2250 18545 complaint (&symfile_complaints,
3e43a32a
MS
18546 _("no terminating 0-type entry for "
18547 "macros in `.debug_macinfo' section"));
2e276125
JB
18548
18549 return;
18550 }
18551 }
18552 break;
18553
cf2c3c16 18554 case DW_MACRO_GNU_transparent_include:
36586728 18555 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18556 {
18557 LONGEST offset;
8fc3fc34 18558 void **slot;
a036ba48
TT
18559 bfd *include_bfd = abfd;
18560 struct dwarf2_section_info *include_section = section;
18561 struct dwarf2_section_info alt_section;
18562 gdb_byte *include_mac_end = mac_end;
18563 int is_dwz = section_is_dwz;
18564 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18565
18566 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18567 mac_ptr += offset_size;
18568
a036ba48
TT
18569 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18570 {
18571 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18572
18573 dwarf2_read_section (dwarf2_per_objfile->objfile,
18574 &dwz->macro);
18575
18576 include_bfd = dwz->macro.asection->owner;
18577 include_section = &dwz->macro;
18578 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18579 is_dwz = 1;
18580 }
18581
18582 new_mac_ptr = include_section->buffer + offset;
18583 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18584
8fc3fc34
TT
18585 if (*slot != NULL)
18586 {
18587 /* This has actually happened; see
18588 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18589 complaint (&symfile_complaints,
18590 _("recursive DW_MACRO_GNU_transparent_include in "
18591 ".debug_macro section"));
18592 }
18593 else
18594 {
a036ba48 18595 *slot = new_mac_ptr;
36586728 18596
a036ba48 18597 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18598 include_mac_end, current_file,
8fc3fc34 18599 lh, comp_dir,
36586728 18600 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18601 offset_size, objfile, include_hash);
18602
a036ba48 18603 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18604 }
cf2c3c16
TT
18605 }
18606 break;
18607
2e276125 18608 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18609 if (!section_is_gnu)
18610 {
18611 unsigned int bytes_read;
18612 int constant;
2e276125 18613
cf2c3c16
TT
18614 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18615 mac_ptr += bytes_read;
18616 read_direct_string (abfd, mac_ptr, &bytes_read);
18617 mac_ptr += bytes_read;
2e276125 18618
cf2c3c16
TT
18619 /* We don't recognize any vendor extensions. */
18620 break;
18621 }
18622 /* FALLTHROUGH */
18623
18624 default:
18625 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18626 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18627 section);
18628 if (mac_ptr == NULL)
18629 return;
18630 break;
2e276125 18631 }
757a13d0 18632 } while (macinfo_type != 0);
2e276125 18633}
8e19ed76 18634
cf2c3c16 18635static void
09262596
DE
18636dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18637 char *comp_dir, int section_is_gnu)
cf2c3c16 18638{
bb5ed363 18639 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18640 struct line_header *lh = cu->line_header;
18641 bfd *abfd;
cf2c3c16
TT
18642 gdb_byte *mac_ptr, *mac_end;
18643 struct macro_source_file *current_file = 0;
18644 enum dwarf_macro_record_type macinfo_type;
18645 unsigned int offset_size = cu->header.offset_size;
18646 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18647 struct cleanup *cleanup;
18648 htab_t include_hash;
18649 void **slot;
09262596
DE
18650 struct dwarf2_section_info *section;
18651 const char *section_name;
18652
18653 if (cu->dwo_unit != NULL)
18654 {
18655 if (section_is_gnu)
18656 {
18657 section = &cu->dwo_unit->dwo_file->sections.macro;
18658 section_name = ".debug_macro.dwo";
18659 }
18660 else
18661 {
18662 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18663 section_name = ".debug_macinfo.dwo";
18664 }
18665 }
18666 else
18667 {
18668 if (section_is_gnu)
18669 {
18670 section = &dwarf2_per_objfile->macro;
18671 section_name = ".debug_macro";
18672 }
18673 else
18674 {
18675 section = &dwarf2_per_objfile->macinfo;
18676 section_name = ".debug_macinfo";
18677 }
18678 }
cf2c3c16 18679
bb5ed363 18680 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18681 if (section->buffer == NULL)
18682 {
fceca515 18683 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18684 return;
18685 }
09262596 18686 abfd = section->asection->owner;
cf2c3c16
TT
18687
18688 /* First pass: Find the name of the base filename.
18689 This filename is needed in order to process all macros whose definition
18690 (or undefinition) comes from the command line. These macros are defined
18691 before the first DW_MACINFO_start_file entry, and yet still need to be
18692 associated to the base file.
18693
18694 To determine the base file name, we scan the macro definitions until we
18695 reach the first DW_MACINFO_start_file entry. We then initialize
18696 CURRENT_FILE accordingly so that any macro definition found before the
18697 first DW_MACINFO_start_file can still be associated to the base file. */
18698
18699 mac_ptr = section->buffer + offset;
18700 mac_end = section->buffer + section->size;
18701
18702 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18703 &offset_size, section_is_gnu);
18704 if (mac_ptr == NULL)
18705 {
18706 /* We already issued a complaint. */
18707 return;
18708 }
18709
18710 do
18711 {
18712 /* Do we at least have room for a macinfo type byte? */
18713 if (mac_ptr >= mac_end)
18714 {
18715 /* Complaint is printed during the second pass as GDB will probably
18716 stop the first pass earlier upon finding
18717 DW_MACINFO_start_file. */
18718 break;
18719 }
18720
18721 macinfo_type = read_1_byte (abfd, mac_ptr);
18722 mac_ptr++;
18723
18724 /* Note that we rely on the fact that the corresponding GNU and
18725 DWARF constants are the same. */
18726 switch (macinfo_type)
18727 {
18728 /* A zero macinfo type indicates the end of the macro
18729 information. */
18730 case 0:
18731 break;
18732
18733 case DW_MACRO_GNU_define:
18734 case DW_MACRO_GNU_undef:
18735 /* Only skip the data by MAC_PTR. */
18736 {
18737 unsigned int bytes_read;
18738
18739 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18740 mac_ptr += bytes_read;
18741 read_direct_string (abfd, mac_ptr, &bytes_read);
18742 mac_ptr += bytes_read;
18743 }
18744 break;
18745
18746 case DW_MACRO_GNU_start_file:
18747 {
18748 unsigned int bytes_read;
18749 int line, file;
18750
18751 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18752 mac_ptr += bytes_read;
18753 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18754 mac_ptr += bytes_read;
18755
18756 current_file = macro_start_file (file, line, current_file,
bb5ed363 18757 comp_dir, lh, objfile);
cf2c3c16
TT
18758 }
18759 break;
18760
18761 case DW_MACRO_GNU_end_file:
18762 /* No data to skip by MAC_PTR. */
18763 break;
18764
18765 case DW_MACRO_GNU_define_indirect:
18766 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18767 case DW_MACRO_GNU_define_indirect_alt:
18768 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18769 {
18770 unsigned int bytes_read;
18771
18772 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18773 mac_ptr += bytes_read;
18774 mac_ptr += offset_size;
18775 }
18776 break;
18777
18778 case DW_MACRO_GNU_transparent_include:
f7a35f02 18779 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18780 /* Note that, according to the spec, a transparent include
18781 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18782 skip this opcode. */
18783 mac_ptr += offset_size;
18784 break;
18785
18786 case DW_MACINFO_vendor_ext:
18787 /* Only skip the data by MAC_PTR. */
18788 if (!section_is_gnu)
18789 {
18790 unsigned int bytes_read;
18791
18792 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18793 mac_ptr += bytes_read;
18794 read_direct_string (abfd, mac_ptr, &bytes_read);
18795 mac_ptr += bytes_read;
18796 }
18797 /* FALLTHROUGH */
18798
18799 default:
18800 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18801 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18802 section);
18803 if (mac_ptr == NULL)
18804 return;
18805 break;
18806 }
18807 } while (macinfo_type != 0 && current_file == NULL);
18808
18809 /* Second pass: Process all entries.
18810
18811 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18812 command-line macro definitions/undefinitions. This flag is unset when we
18813 reach the first DW_MACINFO_start_file entry. */
18814
8fc3fc34
TT
18815 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18816 NULL, xcalloc, xfree);
18817 cleanup = make_cleanup_htab_delete (include_hash);
18818 mac_ptr = section->buffer + offset;
18819 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18820 *slot = mac_ptr;
18821 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18822 current_file, lh, comp_dir, section,
18823 section_is_gnu, 0,
8fc3fc34
TT
18824 offset_size, objfile, include_hash);
18825 do_cleanups (cleanup);
cf2c3c16
TT
18826}
18827
8e19ed76 18828/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18829 if so return true else false. */
380bca97 18830
8e19ed76
PS
18831static int
18832attr_form_is_block (struct attribute *attr)
18833{
18834 return (attr == NULL ? 0 :
18835 attr->form == DW_FORM_block1
18836 || attr->form == DW_FORM_block2
18837 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18838 || attr->form == DW_FORM_block
18839 || attr->form == DW_FORM_exprloc);
8e19ed76 18840}
4c2df51b 18841
c6a0999f
JB
18842/* Return non-zero if ATTR's value is a section offset --- classes
18843 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18844 You may use DW_UNSND (attr) to retrieve such offsets.
18845
18846 Section 7.5.4, "Attribute Encodings", explains that no attribute
18847 may have a value that belongs to more than one of these classes; it
18848 would be ambiguous if we did, because we use the same forms for all
18849 of them. */
380bca97 18850
3690dd37
JB
18851static int
18852attr_form_is_section_offset (struct attribute *attr)
18853{
18854 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18855 || attr->form == DW_FORM_data8
18856 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18857}
18858
3690dd37
JB
18859/* Return non-zero if ATTR's value falls in the 'constant' class, or
18860 zero otherwise. When this function returns true, you can apply
18861 dwarf2_get_attr_constant_value to it.
18862
18863 However, note that for some attributes you must check
18864 attr_form_is_section_offset before using this test. DW_FORM_data4
18865 and DW_FORM_data8 are members of both the constant class, and of
18866 the classes that contain offsets into other debug sections
18867 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18868 that, if an attribute's can be either a constant or one of the
18869 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18870 taken as section offsets, not constants. */
380bca97 18871
3690dd37
JB
18872static int
18873attr_form_is_constant (struct attribute *attr)
18874{
18875 switch (attr->form)
18876 {
18877 case DW_FORM_sdata:
18878 case DW_FORM_udata:
18879 case DW_FORM_data1:
18880 case DW_FORM_data2:
18881 case DW_FORM_data4:
18882 case DW_FORM_data8:
18883 return 1;
18884 default:
18885 return 0;
18886 }
18887}
18888
3019eac3
DE
18889/* Return the .debug_loc section to use for CU.
18890 For DWO files use .debug_loc.dwo. */
18891
18892static struct dwarf2_section_info *
18893cu_debug_loc_section (struct dwarf2_cu *cu)
18894{
18895 if (cu->dwo_unit)
18896 return &cu->dwo_unit->dwo_file->sections.loc;
18897 return &dwarf2_per_objfile->loc;
18898}
18899
8cf6f0b1
TT
18900/* A helper function that fills in a dwarf2_loclist_baton. */
18901
18902static void
18903fill_in_loclist_baton (struct dwarf2_cu *cu,
18904 struct dwarf2_loclist_baton *baton,
18905 struct attribute *attr)
18906{
3019eac3
DE
18907 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18908
18909 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18910
18911 baton->per_cu = cu->per_cu;
18912 gdb_assert (baton->per_cu);
18913 /* We don't know how long the location list is, but make sure we
18914 don't run off the edge of the section. */
3019eac3
DE
18915 baton->size = section->size - DW_UNSND (attr);
18916 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18917 baton->base_address = cu->base_address;
f664829e 18918 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18919}
18920
4c2df51b
DJ
18921static void
18922dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18923 struct dwarf2_cu *cu)
4c2df51b 18924{
bb5ed363 18925 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18926 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18927
3690dd37 18928 if (attr_form_is_section_offset (attr)
3019eac3 18929 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18930 the section. If so, fall through to the complaint in the
18931 other branch. */
3019eac3 18932 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18933 {
0d53c4c4 18934 struct dwarf2_loclist_baton *baton;
4c2df51b 18935
bb5ed363 18936 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18937 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18938
8cf6f0b1 18939 fill_in_loclist_baton (cu, baton, attr);
be391dca 18940
d00adf39 18941 if (cu->base_known == 0)
0d53c4c4 18942 complaint (&symfile_complaints,
3e43a32a
MS
18943 _("Location list used without "
18944 "specifying the CU base address."));
4c2df51b 18945
768a979c 18946 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18947 SYMBOL_LOCATION_BATON (sym) = baton;
18948 }
18949 else
18950 {
18951 struct dwarf2_locexpr_baton *baton;
18952
bb5ed363 18953 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18954 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18955 baton->per_cu = cu->per_cu;
18956 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18957
18958 if (attr_form_is_block (attr))
18959 {
18960 /* Note that we're just copying the block's data pointer
18961 here, not the actual data. We're still pointing into the
6502dd73
DJ
18962 info_buffer for SYM's objfile; right now we never release
18963 that buffer, but when we do clean up properly this may
18964 need to change. */
0d53c4c4
DJ
18965 baton->size = DW_BLOCK (attr)->size;
18966 baton->data = DW_BLOCK (attr)->data;
18967 }
18968 else
18969 {
18970 dwarf2_invalid_attrib_class_complaint ("location description",
18971 SYMBOL_NATURAL_NAME (sym));
18972 baton->size = 0;
0d53c4c4 18973 }
6e70227d 18974
768a979c 18975 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18976 SYMBOL_LOCATION_BATON (sym) = baton;
18977 }
4c2df51b 18978}
6502dd73 18979
9aa1f1e3
TT
18980/* Return the OBJFILE associated with the compilation unit CU. If CU
18981 came from a separate debuginfo file, then the master objfile is
18982 returned. */
ae0d2f24
UW
18983
18984struct objfile *
18985dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18986{
9291a0cd 18987 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18988
18989 /* Return the master objfile, so that we can report and look up the
18990 correct file containing this variable. */
18991 if (objfile->separate_debug_objfile_backlink)
18992 objfile = objfile->separate_debug_objfile_backlink;
18993
18994 return objfile;
18995}
18996
96408a79
SA
18997/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18998 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18999 CU_HEADERP first. */
19000
19001static const struct comp_unit_head *
19002per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19003 struct dwarf2_per_cu_data *per_cu)
19004{
96408a79
SA
19005 gdb_byte *info_ptr;
19006
19007 if (per_cu->cu)
19008 return &per_cu->cu->header;
19009
0bc3a05c 19010 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19011
19012 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19013 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19014
19015 return cu_headerp;
19016}
19017
ae0d2f24
UW
19018/* Return the address size given in the compilation unit header for CU. */
19019
98714339 19020int
ae0d2f24
UW
19021dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19022{
96408a79
SA
19023 struct comp_unit_head cu_header_local;
19024 const struct comp_unit_head *cu_headerp;
c471e790 19025
96408a79
SA
19026 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19027
19028 return cu_headerp->addr_size;
ae0d2f24
UW
19029}
19030
9eae7c52
TT
19031/* Return the offset size given in the compilation unit header for CU. */
19032
19033int
19034dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19035{
96408a79
SA
19036 struct comp_unit_head cu_header_local;
19037 const struct comp_unit_head *cu_headerp;
9c6c53f7 19038
96408a79
SA
19039 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19040
19041 return cu_headerp->offset_size;
19042}
19043
19044/* See its dwarf2loc.h declaration. */
19045
19046int
19047dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19048{
19049 struct comp_unit_head cu_header_local;
19050 const struct comp_unit_head *cu_headerp;
19051
19052 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19053
19054 if (cu_headerp->version == 2)
19055 return cu_headerp->addr_size;
19056 else
19057 return cu_headerp->offset_size;
181cebd4
JK
19058}
19059
9aa1f1e3
TT
19060/* Return the text offset of the CU. The returned offset comes from
19061 this CU's objfile. If this objfile came from a separate debuginfo
19062 file, then the offset may be different from the corresponding
19063 offset in the parent objfile. */
19064
19065CORE_ADDR
19066dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19067{
bb3fa9d0 19068 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19069
19070 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19071}
19072
348e048f
DE
19073/* Locate the .debug_info compilation unit from CU's objfile which contains
19074 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19075
19076static struct dwarf2_per_cu_data *
b64f50a1 19077dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19078 unsigned int offset_in_dwz,
ae038cb0
DJ
19079 struct objfile *objfile)
19080{
19081 struct dwarf2_per_cu_data *this_cu;
19082 int low, high;
36586728 19083 const sect_offset *cu_off;
ae038cb0 19084
ae038cb0
DJ
19085 low = 0;
19086 high = dwarf2_per_objfile->n_comp_units - 1;
19087 while (high > low)
19088 {
36586728 19089 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19090 int mid = low + (high - low) / 2;
9a619af0 19091
36586728
TT
19092 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19093 cu_off = &mid_cu->offset;
19094 if (mid_cu->is_dwz > offset_in_dwz
19095 || (mid_cu->is_dwz == offset_in_dwz
19096 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19097 high = mid;
19098 else
19099 low = mid + 1;
19100 }
19101 gdb_assert (low == high);
36586728
TT
19102 this_cu = dwarf2_per_objfile->all_comp_units[low];
19103 cu_off = &this_cu->offset;
19104 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19105 {
36586728 19106 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19107 error (_("Dwarf Error: could not find partial DIE containing "
19108 "offset 0x%lx [in module %s]"),
b64f50a1 19109 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19110
b64f50a1
JK
19111 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19112 <= offset.sect_off);
ae038cb0
DJ
19113 return dwarf2_per_objfile->all_comp_units[low-1];
19114 }
19115 else
19116 {
19117 this_cu = dwarf2_per_objfile->all_comp_units[low];
19118 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19119 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19120 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19121 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19122 return this_cu;
19123 }
19124}
19125
23745b47 19126/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19127
9816fde3 19128static void
23745b47 19129init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19130{
9816fde3 19131 memset (cu, 0, sizeof (*cu));
23745b47
DE
19132 per_cu->cu = cu;
19133 cu->per_cu = per_cu;
19134 cu->objfile = per_cu->objfile;
93311388 19135 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19136}
19137
19138/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19139
19140static void
95554aad
TT
19141prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19142 enum language pretend_language)
9816fde3
JK
19143{
19144 struct attribute *attr;
19145
19146 /* Set the language we're debugging. */
19147 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19148 if (attr)
19149 set_cu_language (DW_UNSND (attr), cu);
19150 else
9cded63f 19151 {
95554aad 19152 cu->language = pretend_language;
9cded63f
TT
19153 cu->language_defn = language_def (cu->language);
19154 }
dee91e82
DE
19155
19156 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19157 if (attr)
19158 cu->producer = DW_STRING (attr);
93311388
DE
19159}
19160
ae038cb0
DJ
19161/* Release one cached compilation unit, CU. We unlink it from the tree
19162 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19163 the caller is responsible for that.
19164 NOTE: DATA is a void * because this function is also used as a
19165 cleanup routine. */
ae038cb0
DJ
19166
19167static void
68dc6402 19168free_heap_comp_unit (void *data)
ae038cb0
DJ
19169{
19170 struct dwarf2_cu *cu = data;
19171
23745b47
DE
19172 gdb_assert (cu->per_cu != NULL);
19173 cu->per_cu->cu = NULL;
ae038cb0
DJ
19174 cu->per_cu = NULL;
19175
19176 obstack_free (&cu->comp_unit_obstack, NULL);
19177
19178 xfree (cu);
19179}
19180
72bf9492 19181/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19182 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19183 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19184
19185static void
19186free_stack_comp_unit (void *data)
19187{
19188 struct dwarf2_cu *cu = data;
19189
23745b47
DE
19190 gdb_assert (cu->per_cu != NULL);
19191 cu->per_cu->cu = NULL;
19192 cu->per_cu = NULL;
19193
72bf9492
DJ
19194 obstack_free (&cu->comp_unit_obstack, NULL);
19195 cu->partial_dies = NULL;
ae038cb0
DJ
19196}
19197
19198/* Free all cached compilation units. */
19199
19200static void
19201free_cached_comp_units (void *data)
19202{
19203 struct dwarf2_per_cu_data *per_cu, **last_chain;
19204
19205 per_cu = dwarf2_per_objfile->read_in_chain;
19206 last_chain = &dwarf2_per_objfile->read_in_chain;
19207 while (per_cu != NULL)
19208 {
19209 struct dwarf2_per_cu_data *next_cu;
19210
19211 next_cu = per_cu->cu->read_in_chain;
19212
68dc6402 19213 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19214 *last_chain = next_cu;
19215
19216 per_cu = next_cu;
19217 }
19218}
19219
19220/* Increase the age counter on each cached compilation unit, and free
19221 any that are too old. */
19222
19223static void
19224age_cached_comp_units (void)
19225{
19226 struct dwarf2_per_cu_data *per_cu, **last_chain;
19227
19228 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19229 per_cu = dwarf2_per_objfile->read_in_chain;
19230 while (per_cu != NULL)
19231 {
19232 per_cu->cu->last_used ++;
19233 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19234 dwarf2_mark (per_cu->cu);
19235 per_cu = per_cu->cu->read_in_chain;
19236 }
19237
19238 per_cu = dwarf2_per_objfile->read_in_chain;
19239 last_chain = &dwarf2_per_objfile->read_in_chain;
19240 while (per_cu != NULL)
19241 {
19242 struct dwarf2_per_cu_data *next_cu;
19243
19244 next_cu = per_cu->cu->read_in_chain;
19245
19246 if (!per_cu->cu->mark)
19247 {
68dc6402 19248 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19249 *last_chain = next_cu;
19250 }
19251 else
19252 last_chain = &per_cu->cu->read_in_chain;
19253
19254 per_cu = next_cu;
19255 }
19256}
19257
19258/* Remove a single compilation unit from the cache. */
19259
19260static void
dee91e82 19261free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19262{
19263 struct dwarf2_per_cu_data *per_cu, **last_chain;
19264
19265 per_cu = dwarf2_per_objfile->read_in_chain;
19266 last_chain = &dwarf2_per_objfile->read_in_chain;
19267 while (per_cu != NULL)
19268 {
19269 struct dwarf2_per_cu_data *next_cu;
19270
19271 next_cu = per_cu->cu->read_in_chain;
19272
dee91e82 19273 if (per_cu == target_per_cu)
ae038cb0 19274 {
68dc6402 19275 free_heap_comp_unit (per_cu->cu);
dee91e82 19276 per_cu->cu = NULL;
ae038cb0
DJ
19277 *last_chain = next_cu;
19278 break;
19279 }
19280 else
19281 last_chain = &per_cu->cu->read_in_chain;
19282
19283 per_cu = next_cu;
19284 }
19285}
19286
fe3e1990
DJ
19287/* Release all extra memory associated with OBJFILE. */
19288
19289void
19290dwarf2_free_objfile (struct objfile *objfile)
19291{
19292 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19293
19294 if (dwarf2_per_objfile == NULL)
19295 return;
19296
19297 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19298 free_cached_comp_units (NULL);
19299
7b9f3c50
DE
19300 if (dwarf2_per_objfile->quick_file_names_table)
19301 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19302
fe3e1990
DJ
19303 /* Everything else should be on the objfile obstack. */
19304}
19305
dee91e82
DE
19306/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19307 We store these in a hash table separate from the DIEs, and preserve them
19308 when the DIEs are flushed out of cache.
19309
19310 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19311 uniquely identify the type. A file may have multiple .debug_types sections,
19312 or the type may come from a DWO file. We have to use something in
19313 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19314 routine, get_die_type_at_offset, from outside this file, and thus won't
19315 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19316 of the objfile. */
1c379e20 19317
dee91e82 19318struct dwarf2_per_cu_offset_and_type
1c379e20 19319{
dee91e82 19320 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19321 sect_offset offset;
1c379e20
DJ
19322 struct type *type;
19323};
19324
dee91e82 19325/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19326
19327static hashval_t
dee91e82 19328per_cu_offset_and_type_hash (const void *item)
1c379e20 19329{
dee91e82 19330 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19331
dee91e82 19332 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19333}
19334
dee91e82 19335/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19336
19337static int
dee91e82 19338per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19339{
dee91e82
DE
19340 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19341 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19342
dee91e82
DE
19343 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19344 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19345}
19346
19347/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19348 table if necessary. For convenience, return TYPE.
19349
19350 The DIEs reading must have careful ordering to:
19351 * Not cause infite loops trying to read in DIEs as a prerequisite for
19352 reading current DIE.
19353 * Not trying to dereference contents of still incompletely read in types
19354 while reading in other DIEs.
19355 * Enable referencing still incompletely read in types just by a pointer to
19356 the type without accessing its fields.
19357
19358 Therefore caller should follow these rules:
19359 * Try to fetch any prerequisite types we may need to build this DIE type
19360 before building the type and calling set_die_type.
e71ec853 19361 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19362 possible before fetching more types to complete the current type.
19363 * Make the type as complete as possible before fetching more types. */
1c379e20 19364
f792889a 19365static struct type *
1c379e20
DJ
19366set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19367{
dee91e82 19368 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19369 struct objfile *objfile = cu->objfile;
1c379e20 19370
b4ba55a1
JB
19371 /* For Ada types, make sure that the gnat-specific data is always
19372 initialized (if not already set). There are a few types where
19373 we should not be doing so, because the type-specific area is
19374 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19375 where the type-specific area is used to store the floatformat).
19376 But this is not a problem, because the gnat-specific information
19377 is actually not needed for these types. */
19378 if (need_gnat_info (cu)
19379 && TYPE_CODE (type) != TYPE_CODE_FUNC
19380 && TYPE_CODE (type) != TYPE_CODE_FLT
19381 && !HAVE_GNAT_AUX_INFO (type))
19382 INIT_GNAT_SPECIFIC (type);
19383
dee91e82 19384 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19385 {
dee91e82
DE
19386 dwarf2_per_objfile->die_type_hash =
19387 htab_create_alloc_ex (127,
19388 per_cu_offset_and_type_hash,
19389 per_cu_offset_and_type_eq,
19390 NULL,
19391 &objfile->objfile_obstack,
19392 hashtab_obstack_allocate,
19393 dummy_obstack_deallocate);
f792889a 19394 }
1c379e20 19395
dee91e82 19396 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19397 ofs.offset = die->offset;
19398 ofs.type = type;
dee91e82
DE
19399 slot = (struct dwarf2_per_cu_offset_and_type **)
19400 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19401 if (*slot)
19402 complaint (&symfile_complaints,
19403 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19404 die->offset.sect_off);
673bfd45 19405 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19406 **slot = ofs;
f792889a 19407 return type;
1c379e20
DJ
19408}
19409
380bca97 19410/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19411 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19412
19413static struct type *
b64f50a1 19414get_die_type_at_offset (sect_offset offset,
673bfd45 19415 struct dwarf2_per_cu_data *per_cu)
1c379e20 19416{
dee91e82 19417 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19418
dee91e82 19419 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19420 return NULL;
1c379e20 19421
dee91e82 19422 ofs.per_cu = per_cu;
673bfd45 19423 ofs.offset = offset;
dee91e82 19424 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19425 if (slot)
19426 return slot->type;
19427 else
19428 return NULL;
19429}
19430
673bfd45
DE
19431/* Look up the type for DIE in the appropriate type_hash table,
19432 or return NULL if DIE does not have a saved type. */
19433
19434static struct type *
19435get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19436{
19437 return get_die_type_at_offset (die->offset, cu->per_cu);
19438}
19439
10b3939b
DJ
19440/* Add a dependence relationship from CU to REF_PER_CU. */
19441
19442static void
19443dwarf2_add_dependence (struct dwarf2_cu *cu,
19444 struct dwarf2_per_cu_data *ref_per_cu)
19445{
19446 void **slot;
19447
19448 if (cu->dependencies == NULL)
19449 cu->dependencies
19450 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19451 NULL, &cu->comp_unit_obstack,
19452 hashtab_obstack_allocate,
19453 dummy_obstack_deallocate);
19454
19455 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19456 if (*slot == NULL)
19457 *slot = ref_per_cu;
19458}
1c379e20 19459
f504f079
DE
19460/* Subroutine of dwarf2_mark to pass to htab_traverse.
19461 Set the mark field in every compilation unit in the
ae038cb0
DJ
19462 cache that we must keep because we are keeping CU. */
19463
10b3939b
DJ
19464static int
19465dwarf2_mark_helper (void **slot, void *data)
19466{
19467 struct dwarf2_per_cu_data *per_cu;
19468
19469 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19470
19471 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19472 reading of the chain. As such dependencies remain valid it is not much
19473 useful to track and undo them during QUIT cleanups. */
19474 if (per_cu->cu == NULL)
19475 return 1;
19476
10b3939b
DJ
19477 if (per_cu->cu->mark)
19478 return 1;
19479 per_cu->cu->mark = 1;
19480
19481 if (per_cu->cu->dependencies != NULL)
19482 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19483
19484 return 1;
19485}
19486
f504f079
DE
19487/* Set the mark field in CU and in every other compilation unit in the
19488 cache that we must keep because we are keeping CU. */
19489
ae038cb0
DJ
19490static void
19491dwarf2_mark (struct dwarf2_cu *cu)
19492{
19493 if (cu->mark)
19494 return;
19495 cu->mark = 1;
10b3939b
DJ
19496 if (cu->dependencies != NULL)
19497 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19498}
19499
19500static void
19501dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19502{
19503 while (per_cu)
19504 {
19505 per_cu->cu->mark = 0;
19506 per_cu = per_cu->cu->read_in_chain;
19507 }
72bf9492
DJ
19508}
19509
72bf9492
DJ
19510/* Trivial hash function for partial_die_info: the hash value of a DIE
19511 is its offset in .debug_info for this objfile. */
19512
19513static hashval_t
19514partial_die_hash (const void *item)
19515{
19516 const struct partial_die_info *part_die = item;
9a619af0 19517
b64f50a1 19518 return part_die->offset.sect_off;
72bf9492
DJ
19519}
19520
19521/* Trivial comparison function for partial_die_info structures: two DIEs
19522 are equal if they have the same offset. */
19523
19524static int
19525partial_die_eq (const void *item_lhs, const void *item_rhs)
19526{
19527 const struct partial_die_info *part_die_lhs = item_lhs;
19528 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19529
b64f50a1 19530 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19531}
19532
ae038cb0
DJ
19533static struct cmd_list_element *set_dwarf2_cmdlist;
19534static struct cmd_list_element *show_dwarf2_cmdlist;
19535
19536static void
19537set_dwarf2_cmd (char *args, int from_tty)
19538{
19539 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19540}
19541
19542static void
19543show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19544{
ae038cb0
DJ
19545 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19546}
19547
4bf44c1c 19548/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19549
19550static void
c1bd65d0 19551dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19552{
19553 struct dwarf2_per_objfile *data = d;
8b70b953 19554 int ix;
8b70b953 19555
95554aad
TT
19556 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19557 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19558 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19559
8b70b953 19560 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19561
19562 if (data->dwo_files)
19563 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19564
19565 if (data->dwz_file && data->dwz_file->dwz_bfd)
19566 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19567}
19568
19569\f
ae2de4f8 19570/* The "save gdb-index" command. */
9291a0cd
TT
19571
19572/* The contents of the hash table we create when building the string
19573 table. */
19574struct strtab_entry
19575{
19576 offset_type offset;
19577 const char *str;
19578};
19579
559a7a62
JK
19580/* Hash function for a strtab_entry.
19581
19582 Function is used only during write_hash_table so no index format backward
19583 compatibility is needed. */
b89be57b 19584
9291a0cd
TT
19585static hashval_t
19586hash_strtab_entry (const void *e)
19587{
19588 const struct strtab_entry *entry = e;
559a7a62 19589 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19590}
19591
19592/* Equality function for a strtab_entry. */
b89be57b 19593
9291a0cd
TT
19594static int
19595eq_strtab_entry (const void *a, const void *b)
19596{
19597 const struct strtab_entry *ea = a;
19598 const struct strtab_entry *eb = b;
19599 return !strcmp (ea->str, eb->str);
19600}
19601
19602/* Create a strtab_entry hash table. */
b89be57b 19603
9291a0cd
TT
19604static htab_t
19605create_strtab (void)
19606{
19607 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19608 xfree, xcalloc, xfree);
19609}
19610
19611/* Add a string to the constant pool. Return the string's offset in
19612 host order. */
b89be57b 19613
9291a0cd
TT
19614static offset_type
19615add_string (htab_t table, struct obstack *cpool, const char *str)
19616{
19617 void **slot;
19618 struct strtab_entry entry;
19619 struct strtab_entry *result;
19620
19621 entry.str = str;
19622 slot = htab_find_slot (table, &entry, INSERT);
19623 if (*slot)
19624 result = *slot;
19625 else
19626 {
19627 result = XNEW (struct strtab_entry);
19628 result->offset = obstack_object_size (cpool);
19629 result->str = str;
19630 obstack_grow_str0 (cpool, str);
19631 *slot = result;
19632 }
19633 return result->offset;
19634}
19635
19636/* An entry in the symbol table. */
19637struct symtab_index_entry
19638{
19639 /* The name of the symbol. */
19640 const char *name;
19641 /* The offset of the name in the constant pool. */
19642 offset_type index_offset;
19643 /* A sorted vector of the indices of all the CUs that hold an object
19644 of this name. */
19645 VEC (offset_type) *cu_indices;
19646};
19647
19648/* The symbol table. This is a power-of-2-sized hash table. */
19649struct mapped_symtab
19650{
19651 offset_type n_elements;
19652 offset_type size;
19653 struct symtab_index_entry **data;
19654};
19655
19656/* Hash function for a symtab_index_entry. */
b89be57b 19657
9291a0cd
TT
19658static hashval_t
19659hash_symtab_entry (const void *e)
19660{
19661 const struct symtab_index_entry *entry = e;
19662 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19663 sizeof (offset_type) * VEC_length (offset_type,
19664 entry->cu_indices),
19665 0);
19666}
19667
19668/* Equality function for a symtab_index_entry. */
b89be57b 19669
9291a0cd
TT
19670static int
19671eq_symtab_entry (const void *a, const void *b)
19672{
19673 const struct symtab_index_entry *ea = a;
19674 const struct symtab_index_entry *eb = b;
19675 int len = VEC_length (offset_type, ea->cu_indices);
19676 if (len != VEC_length (offset_type, eb->cu_indices))
19677 return 0;
19678 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19679 VEC_address (offset_type, eb->cu_indices),
19680 sizeof (offset_type) * len);
19681}
19682
19683/* Destroy a symtab_index_entry. */
b89be57b 19684
9291a0cd
TT
19685static void
19686delete_symtab_entry (void *p)
19687{
19688 struct symtab_index_entry *entry = p;
19689 VEC_free (offset_type, entry->cu_indices);
19690 xfree (entry);
19691}
19692
19693/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19694
9291a0cd 19695static htab_t
3876f04e 19696create_symbol_hash_table (void)
9291a0cd
TT
19697{
19698 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19699 delete_symtab_entry, xcalloc, xfree);
19700}
19701
19702/* Create a new mapped symtab object. */
b89be57b 19703
9291a0cd
TT
19704static struct mapped_symtab *
19705create_mapped_symtab (void)
19706{
19707 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19708 symtab->n_elements = 0;
19709 symtab->size = 1024;
19710 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19711 return symtab;
19712}
19713
19714/* Destroy a mapped_symtab. */
b89be57b 19715
9291a0cd
TT
19716static void
19717cleanup_mapped_symtab (void *p)
19718{
19719 struct mapped_symtab *symtab = p;
19720 /* The contents of the array are freed when the other hash table is
19721 destroyed. */
19722 xfree (symtab->data);
19723 xfree (symtab);
19724}
19725
19726/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19727 the slot.
19728
19729 Function is used only during write_hash_table so no index format backward
19730 compatibility is needed. */
b89be57b 19731
9291a0cd
TT
19732static struct symtab_index_entry **
19733find_slot (struct mapped_symtab *symtab, const char *name)
19734{
559a7a62 19735 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19736
19737 index = hash & (symtab->size - 1);
19738 step = ((hash * 17) & (symtab->size - 1)) | 1;
19739
19740 for (;;)
19741 {
19742 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19743 return &symtab->data[index];
19744 index = (index + step) & (symtab->size - 1);
19745 }
19746}
19747
19748/* Expand SYMTAB's hash table. */
b89be57b 19749
9291a0cd
TT
19750static void
19751hash_expand (struct mapped_symtab *symtab)
19752{
19753 offset_type old_size = symtab->size;
19754 offset_type i;
19755 struct symtab_index_entry **old_entries = symtab->data;
19756
19757 symtab->size *= 2;
19758 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19759
19760 for (i = 0; i < old_size; ++i)
19761 {
19762 if (old_entries[i])
19763 {
19764 struct symtab_index_entry **slot = find_slot (symtab,
19765 old_entries[i]->name);
19766 *slot = old_entries[i];
19767 }
19768 }
19769
19770 xfree (old_entries);
19771}
19772
156942c7
DE
19773/* Add an entry to SYMTAB. NAME is the name of the symbol.
19774 CU_INDEX is the index of the CU in which the symbol appears.
19775 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19776
9291a0cd
TT
19777static void
19778add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19779 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19780 offset_type cu_index)
19781{
19782 struct symtab_index_entry **slot;
156942c7 19783 offset_type cu_index_and_attrs;
9291a0cd
TT
19784
19785 ++symtab->n_elements;
19786 if (4 * symtab->n_elements / 3 >= symtab->size)
19787 hash_expand (symtab);
19788
19789 slot = find_slot (symtab, name);
19790 if (!*slot)
19791 {
19792 *slot = XNEW (struct symtab_index_entry);
19793 (*slot)->name = name;
156942c7 19794 /* index_offset is set later. */
9291a0cd
TT
19795 (*slot)->cu_indices = NULL;
19796 }
156942c7
DE
19797
19798 cu_index_and_attrs = 0;
19799 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19800 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19801 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19802
19803 /* We don't want to record an index value twice as we want to avoid the
19804 duplication.
19805 We process all global symbols and then all static symbols
19806 (which would allow us to avoid the duplication by only having to check
19807 the last entry pushed), but a symbol could have multiple kinds in one CU.
19808 To keep things simple we don't worry about the duplication here and
19809 sort and uniqufy the list after we've processed all symbols. */
19810 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19811}
19812
19813/* qsort helper routine for uniquify_cu_indices. */
19814
19815static int
19816offset_type_compare (const void *ap, const void *bp)
19817{
19818 offset_type a = *(offset_type *) ap;
19819 offset_type b = *(offset_type *) bp;
19820
19821 return (a > b) - (b > a);
19822}
19823
19824/* Sort and remove duplicates of all symbols' cu_indices lists. */
19825
19826static void
19827uniquify_cu_indices (struct mapped_symtab *symtab)
19828{
19829 int i;
19830
19831 for (i = 0; i < symtab->size; ++i)
19832 {
19833 struct symtab_index_entry *entry = symtab->data[i];
19834
19835 if (entry
19836 && entry->cu_indices != NULL)
19837 {
19838 unsigned int next_to_insert, next_to_check;
19839 offset_type last_value;
19840
19841 qsort (VEC_address (offset_type, entry->cu_indices),
19842 VEC_length (offset_type, entry->cu_indices),
19843 sizeof (offset_type), offset_type_compare);
19844
19845 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19846 next_to_insert = 1;
19847 for (next_to_check = 1;
19848 next_to_check < VEC_length (offset_type, entry->cu_indices);
19849 ++next_to_check)
19850 {
19851 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19852 != last_value)
19853 {
19854 last_value = VEC_index (offset_type, entry->cu_indices,
19855 next_to_check);
19856 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19857 last_value);
19858 ++next_to_insert;
19859 }
19860 }
19861 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19862 }
19863 }
9291a0cd
TT
19864}
19865
19866/* Add a vector of indices to the constant pool. */
b89be57b 19867
9291a0cd 19868static offset_type
3876f04e 19869add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19870 struct symtab_index_entry *entry)
19871{
19872 void **slot;
19873
3876f04e 19874 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19875 if (!*slot)
19876 {
19877 offset_type len = VEC_length (offset_type, entry->cu_indices);
19878 offset_type val = MAYBE_SWAP (len);
19879 offset_type iter;
19880 int i;
19881
19882 *slot = entry;
19883 entry->index_offset = obstack_object_size (cpool);
19884
19885 obstack_grow (cpool, &val, sizeof (val));
19886 for (i = 0;
19887 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19888 ++i)
19889 {
19890 val = MAYBE_SWAP (iter);
19891 obstack_grow (cpool, &val, sizeof (val));
19892 }
19893 }
19894 else
19895 {
19896 struct symtab_index_entry *old_entry = *slot;
19897 entry->index_offset = old_entry->index_offset;
19898 entry = old_entry;
19899 }
19900 return entry->index_offset;
19901}
19902
19903/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19904 constant pool entries going into the obstack CPOOL. */
b89be57b 19905
9291a0cd
TT
19906static void
19907write_hash_table (struct mapped_symtab *symtab,
19908 struct obstack *output, struct obstack *cpool)
19909{
19910 offset_type i;
3876f04e 19911 htab_t symbol_hash_table;
9291a0cd
TT
19912 htab_t str_table;
19913
3876f04e 19914 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19915 str_table = create_strtab ();
3876f04e 19916
9291a0cd
TT
19917 /* We add all the index vectors to the constant pool first, to
19918 ensure alignment is ok. */
19919 for (i = 0; i < symtab->size; ++i)
19920 {
19921 if (symtab->data[i])
3876f04e 19922 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19923 }
19924
19925 /* Now write out the hash table. */
19926 for (i = 0; i < symtab->size; ++i)
19927 {
19928 offset_type str_off, vec_off;
19929
19930 if (symtab->data[i])
19931 {
19932 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19933 vec_off = symtab->data[i]->index_offset;
19934 }
19935 else
19936 {
19937 /* While 0 is a valid constant pool index, it is not valid
19938 to have 0 for both offsets. */
19939 str_off = 0;
19940 vec_off = 0;
19941 }
19942
19943 str_off = MAYBE_SWAP (str_off);
19944 vec_off = MAYBE_SWAP (vec_off);
19945
19946 obstack_grow (output, &str_off, sizeof (str_off));
19947 obstack_grow (output, &vec_off, sizeof (vec_off));
19948 }
19949
19950 htab_delete (str_table);
3876f04e 19951 htab_delete (symbol_hash_table);
9291a0cd
TT
19952}
19953
0a5429f6
DE
19954/* Struct to map psymtab to CU index in the index file. */
19955struct psymtab_cu_index_map
19956{
19957 struct partial_symtab *psymtab;
19958 unsigned int cu_index;
19959};
19960
19961static hashval_t
19962hash_psymtab_cu_index (const void *item)
19963{
19964 const struct psymtab_cu_index_map *map = item;
19965
19966 return htab_hash_pointer (map->psymtab);
19967}
19968
19969static int
19970eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19971{
19972 const struct psymtab_cu_index_map *lhs = item_lhs;
19973 const struct psymtab_cu_index_map *rhs = item_rhs;
19974
19975 return lhs->psymtab == rhs->psymtab;
19976}
19977
19978/* Helper struct for building the address table. */
19979struct addrmap_index_data
19980{
19981 struct objfile *objfile;
19982 struct obstack *addr_obstack;
19983 htab_t cu_index_htab;
19984
19985 /* Non-zero if the previous_* fields are valid.
19986 We can't write an entry until we see the next entry (since it is only then
19987 that we know the end of the entry). */
19988 int previous_valid;
19989 /* Index of the CU in the table of all CUs in the index file. */
19990 unsigned int previous_cu_index;
0963b4bd 19991 /* Start address of the CU. */
0a5429f6
DE
19992 CORE_ADDR previous_cu_start;
19993};
19994
19995/* Write an address entry to OBSTACK. */
b89be57b 19996
9291a0cd 19997static void
0a5429f6
DE
19998add_address_entry (struct objfile *objfile, struct obstack *obstack,
19999 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20000{
0a5429f6 20001 offset_type cu_index_to_write;
9291a0cd
TT
20002 char addr[8];
20003 CORE_ADDR baseaddr;
20004
20005 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20006
0a5429f6
DE
20007 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20008 obstack_grow (obstack, addr, 8);
20009 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20010 obstack_grow (obstack, addr, 8);
20011 cu_index_to_write = MAYBE_SWAP (cu_index);
20012 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20013}
20014
20015/* Worker function for traversing an addrmap to build the address table. */
20016
20017static int
20018add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20019{
20020 struct addrmap_index_data *data = datap;
20021 struct partial_symtab *pst = obj;
0a5429f6
DE
20022
20023 if (data->previous_valid)
20024 add_address_entry (data->objfile, data->addr_obstack,
20025 data->previous_cu_start, start_addr,
20026 data->previous_cu_index);
20027
20028 data->previous_cu_start = start_addr;
20029 if (pst != NULL)
20030 {
20031 struct psymtab_cu_index_map find_map, *map;
20032 find_map.psymtab = pst;
20033 map = htab_find (data->cu_index_htab, &find_map);
20034 gdb_assert (map != NULL);
20035 data->previous_cu_index = map->cu_index;
20036 data->previous_valid = 1;
20037 }
20038 else
20039 data->previous_valid = 0;
20040
20041 return 0;
20042}
20043
20044/* Write OBJFILE's address map to OBSTACK.
20045 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20046 in the index file. */
20047
20048static void
20049write_address_map (struct objfile *objfile, struct obstack *obstack,
20050 htab_t cu_index_htab)
20051{
20052 struct addrmap_index_data addrmap_index_data;
20053
20054 /* When writing the address table, we have to cope with the fact that
20055 the addrmap iterator only provides the start of a region; we have to
20056 wait until the next invocation to get the start of the next region. */
20057
20058 addrmap_index_data.objfile = objfile;
20059 addrmap_index_data.addr_obstack = obstack;
20060 addrmap_index_data.cu_index_htab = cu_index_htab;
20061 addrmap_index_data.previous_valid = 0;
20062
20063 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20064 &addrmap_index_data);
20065
20066 /* It's highly unlikely the last entry (end address = 0xff...ff)
20067 is valid, but we should still handle it.
20068 The end address is recorded as the start of the next region, but that
20069 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20070 anyway. */
20071 if (addrmap_index_data.previous_valid)
20072 add_address_entry (objfile, obstack,
20073 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20074 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20075}
20076
156942c7
DE
20077/* Return the symbol kind of PSYM. */
20078
20079static gdb_index_symbol_kind
20080symbol_kind (struct partial_symbol *psym)
20081{
20082 domain_enum domain = PSYMBOL_DOMAIN (psym);
20083 enum address_class aclass = PSYMBOL_CLASS (psym);
20084
20085 switch (domain)
20086 {
20087 case VAR_DOMAIN:
20088 switch (aclass)
20089 {
20090 case LOC_BLOCK:
20091 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20092 case LOC_TYPEDEF:
20093 return GDB_INDEX_SYMBOL_KIND_TYPE;
20094 case LOC_COMPUTED:
20095 case LOC_CONST_BYTES:
20096 case LOC_OPTIMIZED_OUT:
20097 case LOC_STATIC:
20098 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20099 case LOC_CONST:
20100 /* Note: It's currently impossible to recognize psyms as enum values
20101 short of reading the type info. For now punt. */
20102 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20103 default:
20104 /* There are other LOC_FOO values that one might want to classify
20105 as variables, but dwarf2read.c doesn't currently use them. */
20106 return GDB_INDEX_SYMBOL_KIND_OTHER;
20107 }
20108 case STRUCT_DOMAIN:
20109 return GDB_INDEX_SYMBOL_KIND_TYPE;
20110 default:
20111 return GDB_INDEX_SYMBOL_KIND_OTHER;
20112 }
20113}
20114
9291a0cd 20115/* Add a list of partial symbols to SYMTAB. */
b89be57b 20116
9291a0cd
TT
20117static void
20118write_psymbols (struct mapped_symtab *symtab,
987d643c 20119 htab_t psyms_seen,
9291a0cd
TT
20120 struct partial_symbol **psymp,
20121 int count,
987d643c
TT
20122 offset_type cu_index,
20123 int is_static)
9291a0cd
TT
20124{
20125 for (; count-- > 0; ++psymp)
20126 {
156942c7
DE
20127 struct partial_symbol *psym = *psymp;
20128 void **slot;
987d643c 20129
156942c7 20130 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20131 error (_("Ada is not currently supported by the index"));
987d643c 20132
987d643c 20133 /* Only add a given psymbol once. */
156942c7 20134 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20135 if (!*slot)
20136 {
156942c7
DE
20137 gdb_index_symbol_kind kind = symbol_kind (psym);
20138
20139 *slot = psym;
20140 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20141 is_static, kind, cu_index);
987d643c 20142 }
9291a0cd
TT
20143 }
20144}
20145
20146/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20147 exception if there is an error. */
b89be57b 20148
9291a0cd
TT
20149static void
20150write_obstack (FILE *file, struct obstack *obstack)
20151{
20152 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20153 file)
20154 != obstack_object_size (obstack))
20155 error (_("couldn't data write to file"));
20156}
20157
20158/* Unlink a file if the argument is not NULL. */
b89be57b 20159
9291a0cd
TT
20160static void
20161unlink_if_set (void *p)
20162{
20163 char **filename = p;
20164 if (*filename)
20165 unlink (*filename);
20166}
20167
1fd400ff
TT
20168/* A helper struct used when iterating over debug_types. */
20169struct signatured_type_index_data
20170{
20171 struct objfile *objfile;
20172 struct mapped_symtab *symtab;
20173 struct obstack *types_list;
987d643c 20174 htab_t psyms_seen;
1fd400ff
TT
20175 int cu_index;
20176};
20177
20178/* A helper function that writes a single signatured_type to an
20179 obstack. */
b89be57b 20180
1fd400ff
TT
20181static int
20182write_one_signatured_type (void **slot, void *d)
20183{
20184 struct signatured_type_index_data *info = d;
20185 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20186 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20187 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20188 gdb_byte val[8];
20189
20190 write_psymbols (info->symtab,
987d643c 20191 info->psyms_seen,
3e43a32a
MS
20192 info->objfile->global_psymbols.list
20193 + psymtab->globals_offset,
987d643c
TT
20194 psymtab->n_global_syms, info->cu_index,
20195 0);
1fd400ff 20196 write_psymbols (info->symtab,
987d643c 20197 info->psyms_seen,
3e43a32a
MS
20198 info->objfile->static_psymbols.list
20199 + psymtab->statics_offset,
987d643c
TT
20200 psymtab->n_static_syms, info->cu_index,
20201 1);
1fd400ff 20202
b64f50a1
JK
20203 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20204 entry->per_cu.offset.sect_off);
1fd400ff 20205 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20206 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20207 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20208 obstack_grow (info->types_list, val, 8);
20209 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20210 obstack_grow (info->types_list, val, 8);
20211
20212 ++info->cu_index;
20213
20214 return 1;
20215}
20216
95554aad
TT
20217/* Recurse into all "included" dependencies and write their symbols as
20218 if they appeared in this psymtab. */
20219
20220static void
20221recursively_write_psymbols (struct objfile *objfile,
20222 struct partial_symtab *psymtab,
20223 struct mapped_symtab *symtab,
20224 htab_t psyms_seen,
20225 offset_type cu_index)
20226{
20227 int i;
20228
20229 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20230 if (psymtab->dependencies[i]->user != NULL)
20231 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20232 symtab, psyms_seen, cu_index);
20233
20234 write_psymbols (symtab,
20235 psyms_seen,
20236 objfile->global_psymbols.list + psymtab->globals_offset,
20237 psymtab->n_global_syms, cu_index,
20238 0);
20239 write_psymbols (symtab,
20240 psyms_seen,
20241 objfile->static_psymbols.list + psymtab->statics_offset,
20242 psymtab->n_static_syms, cu_index,
20243 1);
20244}
20245
9291a0cd 20246/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20247
9291a0cd
TT
20248static void
20249write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20250{
20251 struct cleanup *cleanup;
20252 char *filename, *cleanup_filename;
1fd400ff
TT
20253 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20254 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20255 int i;
20256 FILE *out_file;
20257 struct mapped_symtab *symtab;
20258 offset_type val, size_of_contents, total_len;
20259 struct stat st;
987d643c 20260 htab_t psyms_seen;
0a5429f6
DE
20261 htab_t cu_index_htab;
20262 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20263
b4f2f049 20264 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20265 return;
b4f2f049 20266
9291a0cd
TT
20267 if (dwarf2_per_objfile->using_index)
20268 error (_("Cannot use an index to create the index"));
20269
8b70b953
TT
20270 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20271 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20272
9291a0cd 20273 if (stat (objfile->name, &st) < 0)
7e17e088 20274 perror_with_name (objfile->name);
9291a0cd
TT
20275
20276 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20277 INDEX_SUFFIX, (char *) NULL);
20278 cleanup = make_cleanup (xfree, filename);
20279
20280 out_file = fopen (filename, "wb");
20281 if (!out_file)
20282 error (_("Can't open `%s' for writing"), filename);
20283
20284 cleanup_filename = filename;
20285 make_cleanup (unlink_if_set, &cleanup_filename);
20286
20287 symtab = create_mapped_symtab ();
20288 make_cleanup (cleanup_mapped_symtab, symtab);
20289
20290 obstack_init (&addr_obstack);
20291 make_cleanup_obstack_free (&addr_obstack);
20292
20293 obstack_init (&cu_list);
20294 make_cleanup_obstack_free (&cu_list);
20295
1fd400ff
TT
20296 obstack_init (&types_cu_list);
20297 make_cleanup_obstack_free (&types_cu_list);
20298
987d643c
TT
20299 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20300 NULL, xcalloc, xfree);
96408a79 20301 make_cleanup_htab_delete (psyms_seen);
987d643c 20302
0a5429f6
DE
20303 /* While we're scanning CU's create a table that maps a psymtab pointer
20304 (which is what addrmap records) to its index (which is what is recorded
20305 in the index file). This will later be needed to write the address
20306 table. */
20307 cu_index_htab = htab_create_alloc (100,
20308 hash_psymtab_cu_index,
20309 eq_psymtab_cu_index,
20310 NULL, xcalloc, xfree);
96408a79 20311 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20312 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20313 xmalloc (sizeof (struct psymtab_cu_index_map)
20314 * dwarf2_per_objfile->n_comp_units);
20315 make_cleanup (xfree, psymtab_cu_index_map);
20316
20317 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20318 work here. Also, the debug_types entries do not appear in
20319 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20320 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20321 {
3e43a32a
MS
20322 struct dwarf2_per_cu_data *per_cu
20323 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20324 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20325 gdb_byte val[8];
0a5429f6
DE
20326 struct psymtab_cu_index_map *map;
20327 void **slot;
9291a0cd 20328
95554aad
TT
20329 if (psymtab->user == NULL)
20330 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20331
0a5429f6
DE
20332 map = &psymtab_cu_index_map[i];
20333 map->psymtab = psymtab;
20334 map->cu_index = i;
20335 slot = htab_find_slot (cu_index_htab, map, INSERT);
20336 gdb_assert (slot != NULL);
20337 gdb_assert (*slot == NULL);
20338 *slot = map;
9291a0cd 20339
b64f50a1
JK
20340 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20341 per_cu->offset.sect_off);
9291a0cd 20342 obstack_grow (&cu_list, val, 8);
e254ef6a 20343 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20344 obstack_grow (&cu_list, val, 8);
20345 }
20346
0a5429f6
DE
20347 /* Dump the address map. */
20348 write_address_map (objfile, &addr_obstack, cu_index_htab);
20349
1fd400ff
TT
20350 /* Write out the .debug_type entries, if any. */
20351 if (dwarf2_per_objfile->signatured_types)
20352 {
20353 struct signatured_type_index_data sig_data;
20354
20355 sig_data.objfile = objfile;
20356 sig_data.symtab = symtab;
20357 sig_data.types_list = &types_cu_list;
987d643c 20358 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20359 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20360 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20361 write_one_signatured_type, &sig_data);
20362 }
20363
156942c7
DE
20364 /* Now that we've processed all symbols we can shrink their cu_indices
20365 lists. */
20366 uniquify_cu_indices (symtab);
20367
9291a0cd
TT
20368 obstack_init (&constant_pool);
20369 make_cleanup_obstack_free (&constant_pool);
20370 obstack_init (&symtab_obstack);
20371 make_cleanup_obstack_free (&symtab_obstack);
20372 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20373
20374 obstack_init (&contents);
20375 make_cleanup_obstack_free (&contents);
1fd400ff 20376 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20377 total_len = size_of_contents;
20378
20379 /* The version number. */
156942c7 20380 val = MAYBE_SWAP (7);
9291a0cd
TT
20381 obstack_grow (&contents, &val, sizeof (val));
20382
20383 /* The offset of the CU list from the start of the file. */
20384 val = MAYBE_SWAP (total_len);
20385 obstack_grow (&contents, &val, sizeof (val));
20386 total_len += obstack_object_size (&cu_list);
20387
1fd400ff
TT
20388 /* The offset of the types CU list from the start of the file. */
20389 val = MAYBE_SWAP (total_len);
20390 obstack_grow (&contents, &val, sizeof (val));
20391 total_len += obstack_object_size (&types_cu_list);
20392
9291a0cd
TT
20393 /* The offset of the address table from the start of the file. */
20394 val = MAYBE_SWAP (total_len);
20395 obstack_grow (&contents, &val, sizeof (val));
20396 total_len += obstack_object_size (&addr_obstack);
20397
20398 /* The offset of the symbol table from the start of the file. */
20399 val = MAYBE_SWAP (total_len);
20400 obstack_grow (&contents, &val, sizeof (val));
20401 total_len += obstack_object_size (&symtab_obstack);
20402
20403 /* The offset of the constant pool from the start of the file. */
20404 val = MAYBE_SWAP (total_len);
20405 obstack_grow (&contents, &val, sizeof (val));
20406 total_len += obstack_object_size (&constant_pool);
20407
20408 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20409
20410 write_obstack (out_file, &contents);
20411 write_obstack (out_file, &cu_list);
1fd400ff 20412 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20413 write_obstack (out_file, &addr_obstack);
20414 write_obstack (out_file, &symtab_obstack);
20415 write_obstack (out_file, &constant_pool);
20416
20417 fclose (out_file);
20418
20419 /* We want to keep the file, so we set cleanup_filename to NULL
20420 here. See unlink_if_set. */
20421 cleanup_filename = NULL;
20422
20423 do_cleanups (cleanup);
20424}
20425
90476074
TT
20426/* Implementation of the `save gdb-index' command.
20427
20428 Note that the file format used by this command is documented in the
20429 GDB manual. Any changes here must be documented there. */
11570e71 20430
9291a0cd
TT
20431static void
20432save_gdb_index_command (char *arg, int from_tty)
20433{
20434 struct objfile *objfile;
20435
20436 if (!arg || !*arg)
96d19272 20437 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20438
20439 ALL_OBJFILES (objfile)
20440 {
20441 struct stat st;
20442
20443 /* If the objfile does not correspond to an actual file, skip it. */
20444 if (stat (objfile->name, &st) < 0)
20445 continue;
20446
20447 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20448 if (dwarf2_per_objfile)
20449 {
20450 volatile struct gdb_exception except;
20451
20452 TRY_CATCH (except, RETURN_MASK_ERROR)
20453 {
20454 write_psymtabs_to_index (objfile, arg);
20455 }
20456 if (except.reason < 0)
20457 exception_fprintf (gdb_stderr, except,
20458 _("Error while writing index for `%s': "),
20459 objfile->name);
20460 }
20461 }
dce234bc
PP
20462}
20463
9291a0cd
TT
20464\f
20465
9eae7c52
TT
20466int dwarf2_always_disassemble;
20467
20468static void
20469show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20470 struct cmd_list_element *c, const char *value)
20471{
3e43a32a
MS
20472 fprintf_filtered (file,
20473 _("Whether to always disassemble "
20474 "DWARF expressions is %s.\n"),
9eae7c52
TT
20475 value);
20476}
20477
900e11f9
JK
20478static void
20479show_check_physname (struct ui_file *file, int from_tty,
20480 struct cmd_list_element *c, const char *value)
20481{
20482 fprintf_filtered (file,
20483 _("Whether to check \"physname\" is %s.\n"),
20484 value);
20485}
20486
6502dd73
DJ
20487void _initialize_dwarf2_read (void);
20488
20489void
20490_initialize_dwarf2_read (void)
20491{
96d19272
JK
20492 struct cmd_list_element *c;
20493
dce234bc 20494 dwarf2_objfile_data_key
c1bd65d0 20495 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20496
1bedd215
AC
20497 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20498Set DWARF 2 specific variables.\n\
20499Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20500 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20501 0/*allow-unknown*/, &maintenance_set_cmdlist);
20502
1bedd215
AC
20503 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20504Show DWARF 2 specific variables\n\
20505Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20506 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20507 0/*allow-unknown*/, &maintenance_show_cmdlist);
20508
20509 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20510 &dwarf2_max_cache_age, _("\
20511Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20512Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20513A higher limit means that cached compilation units will be stored\n\
20514in memory longer, and more total memory will be used. Zero disables\n\
20515caching, which can slow down startup."),
2c5b56ce 20516 NULL,
920d2a44 20517 show_dwarf2_max_cache_age,
2c5b56ce 20518 &set_dwarf2_cmdlist,
ae038cb0 20519 &show_dwarf2_cmdlist);
d97bc12b 20520
9eae7c52
TT
20521 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20522 &dwarf2_always_disassemble, _("\
20523Set whether `info address' always disassembles DWARF expressions."), _("\
20524Show whether `info address' always disassembles DWARF expressions."), _("\
20525When enabled, DWARF expressions are always printed in an assembly-like\n\
20526syntax. When disabled, expressions will be printed in a more\n\
20527conversational style, when possible."),
20528 NULL,
20529 show_dwarf2_always_disassemble,
20530 &set_dwarf2_cmdlist,
20531 &show_dwarf2_cmdlist);
20532
45cfd468
DE
20533 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20534Set debugging of the dwarf2 reader."), _("\
20535Show debugging of the dwarf2 reader."), _("\
20536When enabled, debugging messages are printed during dwarf2 reading\n\
20537and symtab expansion."),
20538 NULL,
20539 NULL,
20540 &setdebuglist, &showdebuglist);
20541
ccce17b0 20542 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20543Set debugging of the dwarf2 DIE reader."), _("\
20544Show debugging of the dwarf2 DIE reader."), _("\
20545When enabled (non-zero), DIEs are dumped after they are read in.\n\
20546The value is the maximum depth to print."),
ccce17b0
YQ
20547 NULL,
20548 NULL,
20549 &setdebuglist, &showdebuglist);
9291a0cd 20550
900e11f9
JK
20551 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20552Set cross-checking of \"physname\" code against demangler."), _("\
20553Show cross-checking of \"physname\" code against demangler."), _("\
20554When enabled, GDB's internal \"physname\" code is checked against\n\
20555the demangler."),
20556 NULL, show_check_physname,
20557 &setdebuglist, &showdebuglist);
20558
e615022a
DE
20559 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20560 no_class, &use_deprecated_index_sections, _("\
20561Set whether to use deprecated gdb_index sections."), _("\
20562Show whether to use deprecated gdb_index sections."), _("\
20563When enabled, deprecated .gdb_index sections are used anyway.\n\
20564Normally they are ignored either because of a missing feature or\n\
20565performance issue.\n\
20566Warning: This option must be enabled before gdb reads the file."),
20567 NULL,
20568 NULL,
20569 &setlist, &showlist);
20570
96d19272 20571 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20572 _("\
fc1a9d6e 20573Save a gdb-index file.\n\
11570e71 20574Usage: save gdb-index DIRECTORY"),
96d19272
JK
20575 &save_cmdlist);
20576 set_cmd_completer (c, filename_completer);
6502dd73 20577}
This page took 3.029306 seconds and 4 git commands to generate.