* dwarf2read.c (struct partial_die_info) <name, scope>: Now
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
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. */
15d034d0 934 const 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. */
15d034d0 942 const char *scope;
72bf9492 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 {
15d034d0 1025 const char *str;
b60c80d6 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 {
15d034d0 1139 const char *name;
c5aa993b
JM
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
257e7a09
YQ
1287static void dwarf2_read_symtab (struct partial_symtab *,
1288 struct objfile *);
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 1405static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1406 const char *, const char *, CORE_ADDR);
f4dc4d17 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
15d034d0
TT
1530static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1531 struct obstack *);
71c25dea 1532
15d034d0 1533static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1534
15d034d0 1535static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
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 1603static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1604 const 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,
15d034d0 1689 const char **name, const char **comp_dir);
9291a0cd
TT
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
15d034d0 1749static const char *dwarf2_physname (const char *name, struct die_info *die,
3da10d80
KS
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
dee91e82 2817/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2818
dee91e82
DE
2819static void
2820dw2_get_file_names_reader (const struct die_reader_specs *reader,
2821 gdb_byte *info_ptr,
2822 struct die_info *comp_unit_die,
2823 int has_children,
2824 void *data)
9291a0cd 2825{
dee91e82
DE
2826 struct dwarf2_cu *cu = reader->cu;
2827 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2828 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2829 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2830 struct line_header *lh;
9291a0cd 2831 struct attribute *attr;
dee91e82 2832 int i;
15d034d0 2833 const char *name, *comp_dir;
7b9f3c50
DE
2834 void **slot;
2835 struct quick_file_names *qfn;
2836 unsigned int line_offset;
9291a0cd 2837
07261596
TT
2838 /* Our callers never want to match partial units -- instead they
2839 will match the enclosing full CU. */
2840 if (comp_unit_die->tag == DW_TAG_partial_unit)
2841 {
2842 this_cu->v.quick->no_file_data = 1;
2843 return;
2844 }
2845
094b34ac
DE
2846 /* If we're reading the line header for TUs, store it in the "per_cu"
2847 for tu_group. */
2848 if (this_cu->is_debug_types)
2849 {
2850 struct type_unit_group *tu_group = data;
2851
2852 gdb_assert (tu_group != NULL);
2853 lh_cu = &tu_group->per_cu;
2854 }
2855 else
2856 lh_cu = this_cu;
2857
7b9f3c50
DE
2858 lh = NULL;
2859 slot = NULL;
2860 line_offset = 0;
dee91e82
DE
2861
2862 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2863 if (attr)
2864 {
7b9f3c50
DE
2865 struct quick_file_names find_entry;
2866
2867 line_offset = DW_UNSND (attr);
2868
2869 /* We may have already read in this line header (TU line header sharing).
2870 If we have we're done. */
094b34ac
DE
2871 find_entry.hash.dwo_unit = cu->dwo_unit;
2872 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2873 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2874 &find_entry, INSERT);
2875 if (*slot != NULL)
2876 {
094b34ac 2877 lh_cu->v.quick->file_names = *slot;
dee91e82 2878 return;
7b9f3c50
DE
2879 }
2880
3019eac3 2881 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2882 }
2883 if (lh == NULL)
2884 {
094b34ac 2885 lh_cu->v.quick->no_file_data = 1;
dee91e82 2886 return;
9291a0cd
TT
2887 }
2888
7b9f3c50 2889 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2890 qfn->hash.dwo_unit = cu->dwo_unit;
2891 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2892 gdb_assert (slot != NULL);
2893 *slot = qfn;
9291a0cd 2894
dee91e82 2895 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2896
7b9f3c50
DE
2897 qfn->num_file_names = lh->num_file_names;
2898 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2899 lh->num_file_names * sizeof (char *));
9291a0cd 2900 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2901 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2902 qfn->real_names = NULL;
9291a0cd 2903
7b9f3c50 2904 free_line_header (lh);
7b9f3c50 2905
094b34ac 2906 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2907}
2908
2909/* A helper for the "quick" functions which attempts to read the line
2910 table for THIS_CU. */
2911
2912static struct quick_file_names *
2913dw2_get_file_names (struct objfile *objfile,
2914 struct dwarf2_per_cu_data *this_cu)
2915{
f4dc4d17
DE
2916 /* For TUs this should only be called on the parent group. */
2917 if (this_cu->is_debug_types)
2918 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2919
dee91e82
DE
2920 if (this_cu->v.quick->file_names != NULL)
2921 return this_cu->v.quick->file_names;
2922 /* If we know there is no line data, no point in looking again. */
2923 if (this_cu->v.quick->no_file_data)
2924 return NULL;
2925
3019eac3
DE
2926 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2927 in the stub for CUs, there's is no need to lookup the DWO file.
2928 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2929 DWO file. */
2930 if (this_cu->is_debug_types)
094b34ac
DE
2931 {
2932 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2933
2934 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2935 dw2_get_file_names_reader, tu_group);
2936 }
3019eac3
DE
2937 else
2938 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2939
2940 if (this_cu->v.quick->no_file_data)
2941 return NULL;
2942 return this_cu->v.quick->file_names;
9291a0cd
TT
2943}
2944
2945/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2946 real path for a given file name from the line table. */
2fdf6df6 2947
9291a0cd 2948static const char *
7b9f3c50
DE
2949dw2_get_real_path (struct objfile *objfile,
2950 struct quick_file_names *qfn, int index)
9291a0cd 2951{
7b9f3c50
DE
2952 if (qfn->real_names == NULL)
2953 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2954 qfn->num_file_names, sizeof (char *));
9291a0cd 2955
7b9f3c50
DE
2956 if (qfn->real_names[index] == NULL)
2957 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2958
7b9f3c50 2959 return qfn->real_names[index];
9291a0cd
TT
2960}
2961
2962static struct symtab *
2963dw2_find_last_source_symtab (struct objfile *objfile)
2964{
2965 int index;
ae2de4f8 2966
9291a0cd
TT
2967 dw2_setup (objfile);
2968 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2969 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2970}
2971
7b9f3c50
DE
2972/* Traversal function for dw2_forget_cached_source_info. */
2973
2974static int
2975dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2976{
7b9f3c50 2977 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2978
7b9f3c50 2979 if (file_data->real_names)
9291a0cd 2980 {
7b9f3c50 2981 int i;
9291a0cd 2982
7b9f3c50 2983 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2984 {
7b9f3c50
DE
2985 xfree ((void*) file_data->real_names[i]);
2986 file_data->real_names[i] = NULL;
9291a0cd
TT
2987 }
2988 }
7b9f3c50
DE
2989
2990 return 1;
2991}
2992
2993static void
2994dw2_forget_cached_source_info (struct objfile *objfile)
2995{
2996 dw2_setup (objfile);
2997
2998 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2999 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3000}
3001
f8eba3c6
TT
3002/* Helper function for dw2_map_symtabs_matching_filename that expands
3003 the symtabs and calls the iterator. */
3004
3005static int
3006dw2_map_expand_apply (struct objfile *objfile,
3007 struct dwarf2_per_cu_data *per_cu,
3008 const char *name,
3009 const char *full_path, const char *real_path,
3010 int (*callback) (struct symtab *, void *),
3011 void *data)
3012{
3013 struct symtab *last_made = objfile->symtabs;
3014
3015 /* Don't visit already-expanded CUs. */
3016 if (per_cu->v.quick->symtab)
3017 return 0;
3018
3019 /* This may expand more than one symtab, and we want to iterate over
3020 all of them. */
a0f42c21 3021 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3022
3023 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3024 objfile->symtabs, last_made);
3025}
3026
3027/* Implementation of the map_symtabs_matching_filename method. */
3028
9291a0cd 3029static int
f8eba3c6
TT
3030dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3031 const char *full_path, const char *real_path,
3032 int (*callback) (struct symtab *, void *),
3033 void *data)
9291a0cd
TT
3034{
3035 int i;
c011a4f4 3036 const char *name_basename = lbasename (name);
4aac40c8 3037 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3038
3039 dw2_setup (objfile);
ae2de4f8 3040
848e3e78
DE
3041 /* The rule is CUs specify all the files, including those used by
3042 any TU, so there's no need to scan TUs here. */
f4dc4d17 3043
848e3e78 3044 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3045 {
3046 int j;
f4dc4d17 3047 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3048 struct quick_file_names *file_data;
9291a0cd 3049
3d7bb9d9 3050 /* We only need to look at symtabs not already expanded. */
e254ef6a 3051 if (per_cu->v.quick->symtab)
9291a0cd
TT
3052 continue;
3053
7b9f3c50
DE
3054 file_data = dw2_get_file_names (objfile, per_cu);
3055 if (file_data == NULL)
9291a0cd
TT
3056 continue;
3057
7b9f3c50 3058 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3059 {
7b9f3c50 3060 const char *this_name = file_data->file_names[j];
9291a0cd 3061
4aac40c8 3062 if (FILENAME_CMP (name, this_name) == 0
b57a636e 3063 || (!is_abs && compare_filenames_for_search (this_name, name)))
9291a0cd 3064 {
f8eba3c6
TT
3065 if (dw2_map_expand_apply (objfile, per_cu,
3066 name, full_path, real_path,
3067 callback, data))
3068 return 1;
4aac40c8 3069 }
9291a0cd 3070
c011a4f4
DE
3071 /* Before we invoke realpath, which can get expensive when many
3072 files are involved, do a quick comparison of the basenames. */
3073 if (! basenames_may_differ
3074 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3075 continue;
3076
9291a0cd
TT
3077 if (full_path != NULL)
3078 {
7b9f3c50
DE
3079 const char *this_real_name = dw2_get_real_path (objfile,
3080 file_data, j);
9291a0cd 3081
7b9f3c50 3082 if (this_real_name != NULL
4aac40c8
TT
3083 && (FILENAME_CMP (full_path, this_real_name) == 0
3084 || (!is_abs
3085 && compare_filenames_for_search (this_real_name,
b57a636e 3086 name))))
9291a0cd 3087 {
f8eba3c6
TT
3088 if (dw2_map_expand_apply (objfile, per_cu,
3089 name, full_path, real_path,
3090 callback, data))
3091 return 1;
9291a0cd
TT
3092 }
3093 }
3094
3095 if (real_path != NULL)
3096 {
7b9f3c50
DE
3097 const char *this_real_name = dw2_get_real_path (objfile,
3098 file_data, j);
9291a0cd 3099
7b9f3c50 3100 if (this_real_name != NULL
4aac40c8
TT
3101 && (FILENAME_CMP (real_path, this_real_name) == 0
3102 || (!is_abs
3103 && compare_filenames_for_search (this_real_name,
b57a636e 3104 name))))
9291a0cd 3105 {
f8eba3c6
TT
3106 if (dw2_map_expand_apply (objfile, per_cu,
3107 name, full_path, real_path,
3108 callback, data))
3109 return 1;
9291a0cd
TT
3110 }
3111 }
3112 }
3113 }
3114
9291a0cd
TT
3115 return 0;
3116}
3117
da51c347
DE
3118/* Struct used to manage iterating over all CUs looking for a symbol. */
3119
3120struct dw2_symtab_iterator
9291a0cd 3121{
da51c347
DE
3122 /* The internalized form of .gdb_index. */
3123 struct mapped_index *index;
3124 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3125 int want_specific_block;
3126 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3127 Unused if !WANT_SPECIFIC_BLOCK. */
3128 int block_index;
3129 /* The kind of symbol we're looking for. */
3130 domain_enum domain;
3131 /* The list of CUs from the index entry of the symbol,
3132 or NULL if not found. */
3133 offset_type *vec;
3134 /* The next element in VEC to look at. */
3135 int next;
3136 /* The number of elements in VEC, or zero if there is no match. */
3137 int length;
3138};
9291a0cd 3139
da51c347
DE
3140/* Initialize the index symtab iterator ITER.
3141 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3142 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3143
9291a0cd 3144static void
da51c347
DE
3145dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3146 struct mapped_index *index,
3147 int want_specific_block,
3148 int block_index,
3149 domain_enum domain,
3150 const char *name)
3151{
3152 iter->index = index;
3153 iter->want_specific_block = want_specific_block;
3154 iter->block_index = block_index;
3155 iter->domain = domain;
3156 iter->next = 0;
3157
3158 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3159 iter->length = MAYBE_SWAP (*iter->vec);
3160 else
3161 {
3162 iter->vec = NULL;
3163 iter->length = 0;
3164 }
3165}
3166
3167/* Return the next matching CU or NULL if there are no more. */
3168
3169static struct dwarf2_per_cu_data *
3170dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3171{
3172 for ( ; iter->next < iter->length; ++iter->next)
3173 {
3174 offset_type cu_index_and_attrs =
3175 MAYBE_SWAP (iter->vec[iter->next + 1]);
3176 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3177 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3178 int want_static = iter->block_index != GLOBAL_BLOCK;
3179 /* This value is only valid for index versions >= 7. */
3180 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3181 gdb_index_symbol_kind symbol_kind =
3182 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3183 /* Only check the symbol attributes if they're present.
3184 Indices prior to version 7 don't record them,
3185 and indices >= 7 may elide them for certain symbols
3186 (gold does this). */
3187 int attrs_valid =
3188 (iter->index->version >= 7
3189 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3190
3191 /* Skip if already read in. */
3192 if (per_cu->v.quick->symtab)
3193 continue;
3194
3195 if (attrs_valid
3196 && iter->want_specific_block
3197 && want_static != is_static)
3198 continue;
3199
3200 /* Only check the symbol's kind if it has one. */
3201 if (attrs_valid)
3202 {
3203 switch (iter->domain)
3204 {
3205 case VAR_DOMAIN:
3206 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3207 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3208 /* Some types are also in VAR_DOMAIN. */
3209 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3210 continue;
3211 break;
3212 case STRUCT_DOMAIN:
3213 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3214 continue;
3215 break;
3216 case LABEL_DOMAIN:
3217 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3218 continue;
3219 break;
3220 default:
3221 break;
3222 }
3223 }
3224
3225 ++iter->next;
3226 return per_cu;
3227 }
3228
3229 return NULL;
3230}
3231
3232static struct symtab *
3233dw2_lookup_symbol (struct objfile *objfile, int block_index,
3234 const char *name, domain_enum domain)
9291a0cd 3235{
da51c347 3236 struct symtab *stab_best = NULL;
156942c7
DE
3237 struct mapped_index *index;
3238
9291a0cd
TT
3239 dw2_setup (objfile);
3240
156942c7
DE
3241 index = dwarf2_per_objfile->index_table;
3242
da51c347 3243 /* index is NULL if OBJF_READNOW. */
156942c7 3244 if (index)
9291a0cd 3245 {
da51c347
DE
3246 struct dw2_symtab_iterator iter;
3247 struct dwarf2_per_cu_data *per_cu;
3248
3249 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3250
da51c347 3251 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3252 {
da51c347
DE
3253 struct symbol *sym = NULL;
3254 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3255
3256 /* Some caution must be observed with overloaded functions
3257 and methods, since the index will not contain any overload
3258 information (but NAME might contain it). */
3259 if (stab->primary)
9291a0cd 3260 {
da51c347
DE
3261 struct blockvector *bv = BLOCKVECTOR (stab);
3262 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3263
da51c347
DE
3264 sym = lookup_block_symbol (block, name, domain);
3265 }
1fd400ff 3266
da51c347
DE
3267 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3268 {
3269 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3270 return stab;
3271
3272 stab_best = stab;
9291a0cd 3273 }
da51c347
DE
3274
3275 /* Keep looking through other CUs. */
9291a0cd
TT
3276 }
3277 }
9291a0cd 3278
da51c347 3279 return stab_best;
9291a0cd
TT
3280}
3281
3282static void
3283dw2_print_stats (struct objfile *objfile)
3284{
3285 int i, count;
3286
3287 dw2_setup (objfile);
3288 count = 0;
1fd400ff 3289 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3290 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3291 {
e254ef6a 3292 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3293
e254ef6a 3294 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3295 ++count;
3296 }
3297 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3298}
3299
3300static void
3301dw2_dump (struct objfile *objfile)
3302{
3303 /* Nothing worth printing. */
3304}
3305
3306static void
3307dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3308 struct section_offsets *delta)
3309{
3310 /* There's nothing to relocate here. */
3311}
3312
3313static void
3314dw2_expand_symtabs_for_function (struct objfile *objfile,
3315 const char *func_name)
3316{
da51c347
DE
3317 struct mapped_index *index;
3318
3319 dw2_setup (objfile);
3320
3321 index = dwarf2_per_objfile->index_table;
3322
3323 /* index is NULL if OBJF_READNOW. */
3324 if (index)
3325 {
3326 struct dw2_symtab_iterator iter;
3327 struct dwarf2_per_cu_data *per_cu;
3328
3329 /* Note: It doesn't matter what we pass for block_index here. */
3330 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3331 func_name);
3332
3333 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3334 dw2_instantiate_symtab (per_cu);
3335 }
9291a0cd
TT
3336}
3337
3338static void
3339dw2_expand_all_symtabs (struct objfile *objfile)
3340{
3341 int i;
3342
3343 dw2_setup (objfile);
1fd400ff
TT
3344
3345 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3346 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3347 {
e254ef6a 3348 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3349
a0f42c21 3350 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3351 }
3352}
3353
3354static void
3355dw2_expand_symtabs_with_filename (struct objfile *objfile,
3356 const char *filename)
3357{
3358 int i;
3359
3360 dw2_setup (objfile);
d4637a04
DE
3361
3362 /* We don't need to consider type units here.
3363 This is only called for examining code, e.g. expand_line_sal.
3364 There can be an order of magnitude (or more) more type units
3365 than comp units, and we avoid them if we can. */
3366
3367 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3368 {
3369 int j;
e254ef6a 3370 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3371 struct quick_file_names *file_data;
9291a0cd 3372
3d7bb9d9 3373 /* We only need to look at symtabs not already expanded. */
e254ef6a 3374 if (per_cu->v.quick->symtab)
9291a0cd
TT
3375 continue;
3376
7b9f3c50
DE
3377 file_data = dw2_get_file_names (objfile, per_cu);
3378 if (file_data == NULL)
9291a0cd
TT
3379 continue;
3380
7b9f3c50 3381 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3382 {
7b9f3c50 3383 const char *this_name = file_data->file_names[j];
1ef75ecc 3384 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3385 {
a0f42c21 3386 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3387 break;
3388 }
3389 }
3390 }
3391}
3392
356d9f9d
TT
3393/* A helper function for dw2_find_symbol_file that finds the primary
3394 file name for a given CU. This is a die_reader_func. */
3395
3396static void
3397dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3398 gdb_byte *info_ptr,
3399 struct die_info *comp_unit_die,
3400 int has_children,
3401 void *data)
3402{
3403 const char **result_ptr = data;
3404 struct dwarf2_cu *cu = reader->cu;
3405 struct attribute *attr;
3406
3407 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3408 if (attr == NULL)
3409 *result_ptr = NULL;
3410 else
3411 *result_ptr = DW_STRING (attr);
3412}
3413
dd786858 3414static const char *
9291a0cd
TT
3415dw2_find_symbol_file (struct objfile *objfile, const char *name)
3416{
e254ef6a 3417 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3418 offset_type *vec;
356d9f9d 3419 const char *filename;
9291a0cd
TT
3420
3421 dw2_setup (objfile);
3422
ae2de4f8 3423 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3424 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3425 {
3426 struct symtab *s;
3427
d790cf0a
DE
3428 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3429 {
3430 struct blockvector *bv = BLOCKVECTOR (s);
3431 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3432 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3433
3434 if (sym)
210bbc17 3435 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3436 }
96408a79
SA
3437 return NULL;
3438 }
9291a0cd
TT
3439
3440 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3441 name, &vec))
3442 return NULL;
3443
3444 /* Note that this just looks at the very first one named NAME -- but
3445 actually we are looking for a function. find_main_filename
3446 should be rewritten so that it doesn't require a custom hook. It
3447 could just use the ordinary symbol tables. */
3448 /* vec[0] is the length, which must always be >0. */
156942c7 3449 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3450
356d9f9d
TT
3451 if (per_cu->v.quick->symtab != NULL)
3452 return per_cu->v.quick->symtab->filename;
3453
f4dc4d17
DE
3454 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3455 dw2_get_primary_filename_reader, &filename);
9291a0cd 3456
356d9f9d 3457 return filename;
9291a0cd
TT
3458}
3459
3460static void
40658b94
PH
3461dw2_map_matching_symbols (const char * name, domain_enum namespace,
3462 struct objfile *objfile, int global,
3463 int (*callback) (struct block *,
3464 struct symbol *, void *),
2edb89d3
JK
3465 void *data, symbol_compare_ftype *match,
3466 symbol_compare_ftype *ordered_compare)
9291a0cd 3467{
40658b94 3468 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3469 current language is Ada for a non-Ada objfile using GNU index. As Ada
3470 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3471}
3472
3473static void
f8eba3c6
TT
3474dw2_expand_symtabs_matching
3475 (struct objfile *objfile,
3476 int (*file_matcher) (const char *, void *),
e078317b 3477 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3478 enum search_domain kind,
3479 void *data)
9291a0cd
TT
3480{
3481 int i;
3482 offset_type iter;
4b5246aa 3483 struct mapped_index *index;
9291a0cd
TT
3484
3485 dw2_setup (objfile);
ae2de4f8
DE
3486
3487 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3488 if (!dwarf2_per_objfile->index_table)
3489 return;
4b5246aa 3490 index = dwarf2_per_objfile->index_table;
9291a0cd 3491
7b08b9eb 3492 if (file_matcher != NULL)
24c79950
TT
3493 {
3494 struct cleanup *cleanup;
3495 htab_t visited_found, visited_not_found;
3496
3497 visited_found = htab_create_alloc (10,
3498 htab_hash_pointer, htab_eq_pointer,
3499 NULL, xcalloc, xfree);
3500 cleanup = make_cleanup_htab_delete (visited_found);
3501 visited_not_found = htab_create_alloc (10,
3502 htab_hash_pointer, htab_eq_pointer,
3503 NULL, xcalloc, xfree);
3504 make_cleanup_htab_delete (visited_not_found);
3505
848e3e78
DE
3506 /* The rule is CUs specify all the files, including those used by
3507 any TU, so there's no need to scan TUs here. */
3508
3509 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3510 {
3511 int j;
f4dc4d17 3512 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3513 struct quick_file_names *file_data;
3514 void **slot;
7b08b9eb 3515
24c79950 3516 per_cu->v.quick->mark = 0;
3d7bb9d9 3517
24c79950
TT
3518 /* We only need to look at symtabs not already expanded. */
3519 if (per_cu->v.quick->symtab)
3520 continue;
7b08b9eb 3521
24c79950
TT
3522 file_data = dw2_get_file_names (objfile, per_cu);
3523 if (file_data == NULL)
3524 continue;
7b08b9eb 3525
24c79950
TT
3526 if (htab_find (visited_not_found, file_data) != NULL)
3527 continue;
3528 else if (htab_find (visited_found, file_data) != NULL)
3529 {
3530 per_cu->v.quick->mark = 1;
3531 continue;
3532 }
3533
3534 for (j = 0; j < file_data->num_file_names; ++j)
3535 {
3536 if (file_matcher (file_data->file_names[j], data))
3537 {
3538 per_cu->v.quick->mark = 1;
3539 break;
3540 }
3541 }
3542
3543 slot = htab_find_slot (per_cu->v.quick->mark
3544 ? visited_found
3545 : visited_not_found,
3546 file_data, INSERT);
3547 *slot = file_data;
3548 }
3549
3550 do_cleanups (cleanup);
3551 }
9291a0cd 3552
3876f04e 3553 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3554 {
3555 offset_type idx = 2 * iter;
3556 const char *name;
3557 offset_type *vec, vec_len, vec_idx;
3558
3876f04e 3559 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3560 continue;
3561
3876f04e 3562 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3563
e078317b 3564 if (! (*name_matcher) (name, data))
9291a0cd
TT
3565 continue;
3566
3567 /* The name was matched, now expand corresponding CUs that were
3568 marked. */
4b5246aa 3569 vec = (offset_type *) (index->constant_pool
3876f04e 3570 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3571 vec_len = MAYBE_SWAP (vec[0]);
3572 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3573 {
e254ef6a 3574 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3575 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3576 gdb_index_symbol_kind symbol_kind =
3577 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3578 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3579
3580 /* Don't crash on bad data. */
3581 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3582 + dwarf2_per_objfile->n_type_units))
156942c7 3583 continue;
1fd400ff 3584
156942c7
DE
3585 /* Only check the symbol's kind if it has one.
3586 Indices prior to version 7 don't record it. */
3587 if (index->version >= 7)
3588 {
3589 switch (kind)
3590 {
3591 case VARIABLES_DOMAIN:
3592 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3593 continue;
3594 break;
3595 case FUNCTIONS_DOMAIN:
3596 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3597 continue;
3598 break;
3599 case TYPES_DOMAIN:
3600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3601 continue;
3602 break;
3603 default:
3604 break;
3605 }
3606 }
3607
3608 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3609 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3610 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3611 }
3612 }
3613}
3614
9703b513
TT
3615/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3616 symtab. */
3617
3618static struct symtab *
3619recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3620{
3621 int i;
3622
3623 if (BLOCKVECTOR (symtab) != NULL
3624 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3625 return symtab;
3626
a3ec0bb1
DE
3627 if (symtab->includes == NULL)
3628 return NULL;
3629
9703b513
TT
3630 for (i = 0; symtab->includes[i]; ++i)
3631 {
a3ec0bb1 3632 struct symtab *s = symtab->includes[i];
9703b513
TT
3633
3634 s = recursively_find_pc_sect_symtab (s, pc);
3635 if (s != NULL)
3636 return s;
3637 }
3638
3639 return NULL;
3640}
3641
9291a0cd
TT
3642static struct symtab *
3643dw2_find_pc_sect_symtab (struct objfile *objfile,
3644 struct minimal_symbol *msymbol,
3645 CORE_ADDR pc,
3646 struct obj_section *section,
3647 int warn_if_readin)
3648{
3649 struct dwarf2_per_cu_data *data;
9703b513 3650 struct symtab *result;
9291a0cd
TT
3651
3652 dw2_setup (objfile);
3653
3654 if (!objfile->psymtabs_addrmap)
3655 return NULL;
3656
3657 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3658 if (!data)
3659 return NULL;
3660
3661 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3662 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3663 paddress (get_objfile_arch (objfile), pc));
3664
9703b513
TT
3665 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3666 gdb_assert (result != NULL);
3667 return result;
9291a0cd
TT
3668}
3669
9291a0cd 3670static void
44b13c5a 3671dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3672 void *data, int need_fullname)
9291a0cd
TT
3673{
3674 int i;
24c79950
TT
3675 struct cleanup *cleanup;
3676 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3677 NULL, xcalloc, xfree);
9291a0cd 3678
24c79950 3679 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3680 dw2_setup (objfile);
ae2de4f8 3681
848e3e78
DE
3682 /* The rule is CUs specify all the files, including those used by
3683 any TU, so there's no need to scan TUs here.
3684 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3685
848e3e78 3686 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3687 {
3688 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3689
3690 if (per_cu->v.quick->symtab)
3691 {
3692 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3693 INSERT);
3694
3695 *slot = per_cu->v.quick->file_names;
3696 }
3697 }
3698
848e3e78 3699 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3700 {
3701 int j;
f4dc4d17 3702 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3703 struct quick_file_names *file_data;
24c79950 3704 void **slot;
9291a0cd 3705
3d7bb9d9 3706 /* We only need to look at symtabs not already expanded. */
e254ef6a 3707 if (per_cu->v.quick->symtab)
9291a0cd
TT
3708 continue;
3709
7b9f3c50
DE
3710 file_data = dw2_get_file_names (objfile, per_cu);
3711 if (file_data == NULL)
9291a0cd
TT
3712 continue;
3713
24c79950
TT
3714 slot = htab_find_slot (visited, file_data, INSERT);
3715 if (*slot)
3716 {
3717 /* Already visited. */
3718 continue;
3719 }
3720 *slot = file_data;
3721
7b9f3c50 3722 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3723 {
74e2f255
DE
3724 const char *this_real_name;
3725
3726 if (need_fullname)
3727 this_real_name = dw2_get_real_path (objfile, file_data, j);
3728 else
3729 this_real_name = NULL;
7b9f3c50 3730 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3731 }
3732 }
24c79950
TT
3733
3734 do_cleanups (cleanup);
9291a0cd
TT
3735}
3736
3737static int
3738dw2_has_symbols (struct objfile *objfile)
3739{
3740 return 1;
3741}
3742
3743const struct quick_symbol_functions dwarf2_gdb_index_functions =
3744{
3745 dw2_has_symbols,
3746 dw2_find_last_source_symtab,
3747 dw2_forget_cached_source_info,
f8eba3c6 3748 dw2_map_symtabs_matching_filename,
9291a0cd 3749 dw2_lookup_symbol,
9291a0cd
TT
3750 dw2_print_stats,
3751 dw2_dump,
3752 dw2_relocate,
3753 dw2_expand_symtabs_for_function,
3754 dw2_expand_all_symtabs,
3755 dw2_expand_symtabs_with_filename,
3756 dw2_find_symbol_file,
40658b94 3757 dw2_map_matching_symbols,
9291a0cd
TT
3758 dw2_expand_symtabs_matching,
3759 dw2_find_pc_sect_symtab,
9291a0cd
TT
3760 dw2_map_symbol_filenames
3761};
3762
3763/* Initialize for reading DWARF for this objfile. Return 0 if this
3764 file will use psymtabs, or 1 if using the GNU index. */
3765
3766int
3767dwarf2_initialize_objfile (struct objfile *objfile)
3768{
3769 /* If we're about to read full symbols, don't bother with the
3770 indices. In this case we also don't care if some other debug
3771 format is making psymtabs, because they are all about to be
3772 expanded anyway. */
3773 if ((objfile->flags & OBJF_READNOW))
3774 {
3775 int i;
3776
3777 dwarf2_per_objfile->using_index = 1;
3778 create_all_comp_units (objfile);
0e50663e 3779 create_all_type_units (objfile);
7b9f3c50
DE
3780 dwarf2_per_objfile->quick_file_names_table =
3781 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3782
1fd400ff 3783 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3784 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3785 {
e254ef6a 3786 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3787
e254ef6a
DE
3788 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3789 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3790 }
3791
3792 /* Return 1 so that gdb sees the "quick" functions. However,
3793 these functions will be no-ops because we will have expanded
3794 all symtabs. */
3795 return 1;
3796 }
3797
3798 if (dwarf2_read_index (objfile))
3799 return 1;
3800
9291a0cd
TT
3801 return 0;
3802}
3803
3804\f
3805
dce234bc
PP
3806/* Build a partial symbol table. */
3807
3808void
f29dff0a 3809dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3810{
c9bf0622
TT
3811 volatile struct gdb_exception except;
3812
f29dff0a 3813 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3814 {
3815 init_psymbol_list (objfile, 1024);
3816 }
3817
c9bf0622
TT
3818 TRY_CATCH (except, RETURN_MASK_ERROR)
3819 {
3820 /* This isn't really ideal: all the data we allocate on the
3821 objfile's obstack is still uselessly kept around. However,
3822 freeing it seems unsafe. */
3823 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3824
3825 dwarf2_build_psymtabs_hard (objfile);
3826 discard_cleanups (cleanups);
3827 }
3828 if (except.reason < 0)
3829 exception_print (gdb_stderr, except);
c906108c 3830}
c906108c 3831
1ce1cefd
DE
3832/* Return the total length of the CU described by HEADER. */
3833
3834static unsigned int
3835get_cu_length (const struct comp_unit_head *header)
3836{
3837 return header->initial_length_size + header->length;
3838}
3839
45452591
DE
3840/* Return TRUE if OFFSET is within CU_HEADER. */
3841
3842static inline int
b64f50a1 3843offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3844{
b64f50a1 3845 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3846 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3847
b64f50a1 3848 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3849}
3850
3b80fe9b
DE
3851/* Find the base address of the compilation unit for range lists and
3852 location lists. It will normally be specified by DW_AT_low_pc.
3853 In DWARF-3 draft 4, the base address could be overridden by
3854 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3855 compilation units with discontinuous ranges. */
3856
3857static void
3858dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3859{
3860 struct attribute *attr;
3861
3862 cu->base_known = 0;
3863 cu->base_address = 0;
3864
3865 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3866 if (attr)
3867 {
3868 cu->base_address = DW_ADDR (attr);
3869 cu->base_known = 1;
3870 }
3871 else
3872 {
3873 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3874 if (attr)
3875 {
3876 cu->base_address = DW_ADDR (attr);
3877 cu->base_known = 1;
3878 }
3879 }
3880}
3881
93311388
DE
3882/* Read in the comp unit header information from the debug_info at info_ptr.
3883 NOTE: This leaves members offset, first_die_offset to be filled in
3884 by the caller. */
107d2387 3885
fe1b8b76 3886static gdb_byte *
107d2387 3887read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3888 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3889{
3890 int signed_addr;
891d2f0b 3891 unsigned int bytes_read;
c764a876
DE
3892
3893 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3894 cu_header->initial_length_size = bytes_read;
3895 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3896 info_ptr += bytes_read;
107d2387
AC
3897 cu_header->version = read_2_bytes (abfd, info_ptr);
3898 info_ptr += 2;
b64f50a1
JK
3899 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3900 &bytes_read);
613e1657 3901 info_ptr += bytes_read;
107d2387
AC
3902 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3903 info_ptr += 1;
3904 signed_addr = bfd_get_sign_extend_vma (abfd);
3905 if (signed_addr < 0)
8e65ff28 3906 internal_error (__FILE__, __LINE__,
e2e0b3e5 3907 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3908 cu_header->signed_addr_p = signed_addr;
c764a876 3909
107d2387
AC
3910 return info_ptr;
3911}
3912
36586728
TT
3913/* Helper function that returns the proper abbrev section for
3914 THIS_CU. */
3915
3916static struct dwarf2_section_info *
3917get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3918{
3919 struct dwarf2_section_info *abbrev;
3920
3921 if (this_cu->is_dwz)
3922 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3923 else
3924 abbrev = &dwarf2_per_objfile->abbrev;
3925
3926 return abbrev;
3927}
3928
9ff913ba
DE
3929/* Subroutine of read_and_check_comp_unit_head and
3930 read_and_check_type_unit_head to simplify them.
3931 Perform various error checking on the header. */
3932
3933static void
3934error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3935 struct dwarf2_section_info *section,
3936 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3937{
3938 bfd *abfd = section->asection->owner;
3939 const char *filename = bfd_get_filename (abfd);
3940
3941 if (header->version != 2 && header->version != 3 && header->version != 4)
3942 error (_("Dwarf Error: wrong version in compilation unit header "
3943 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3944 filename);
3945
b64f50a1 3946 if (header->abbrev_offset.sect_off
36586728 3947 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3948 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3949 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3950 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3951 filename);
3952
3953 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3954 avoid potential 32-bit overflow. */
1ce1cefd 3955 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3956 > section->size)
3957 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3958 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3959 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3960 filename);
3961}
3962
3963/* Read in a CU/TU header and perform some basic error checking.
3964 The contents of the header are stored in HEADER.
3965 The result is a pointer to the start of the first DIE. */
adabb602 3966
fe1b8b76 3967static gdb_byte *
9ff913ba
DE
3968read_and_check_comp_unit_head (struct comp_unit_head *header,
3969 struct dwarf2_section_info *section,
4bdcc0c1 3970 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3971 gdb_byte *info_ptr,
3972 int is_debug_types_section)
72bf9492 3973{
fe1b8b76 3974 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3975 bfd *abfd = section->asection->owner;
72bf9492 3976
b64f50a1 3977 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3978
72bf9492
DJ
3979 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3980
460c1c54
CC
3981 /* If we're reading a type unit, skip over the signature and
3982 type_offset fields. */
b0df02fd 3983 if (is_debug_types_section)
460c1c54
CC
3984 info_ptr += 8 /*signature*/ + header->offset_size;
3985
b64f50a1 3986 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3987
4bdcc0c1 3988 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3989
3990 return info_ptr;
3991}
3992
348e048f
DE
3993/* Read in the types comp unit header information from .debug_types entry at
3994 types_ptr. The result is a pointer to one past the end of the header. */
3995
3996static gdb_byte *
9ff913ba
DE
3997read_and_check_type_unit_head (struct comp_unit_head *header,
3998 struct dwarf2_section_info *section,
4bdcc0c1 3999 struct dwarf2_section_info *abbrev_section,
9ff913ba 4000 gdb_byte *info_ptr,
dee91e82
DE
4001 ULONGEST *signature,
4002 cu_offset *type_offset_in_tu)
348e048f 4003{
9ff913ba
DE
4004 gdb_byte *beg_of_comp_unit = info_ptr;
4005 bfd *abfd = section->asection->owner;
348e048f 4006
b64f50a1 4007 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4008
9ff913ba 4009 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4010
9ff913ba
DE
4011 /* If we're reading a type unit, skip over the signature and
4012 type_offset fields. */
4013 if (signature != NULL)
4014 *signature = read_8_bytes (abfd, info_ptr);
4015 info_ptr += 8;
dee91e82
DE
4016 if (type_offset_in_tu != NULL)
4017 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4018 header->offset_size);
9ff913ba
DE
4019 info_ptr += header->offset_size;
4020
b64f50a1 4021 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4022
4bdcc0c1 4023 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4024
4025 return info_ptr;
348e048f
DE
4026}
4027
f4dc4d17
DE
4028/* Fetch the abbreviation table offset from a comp or type unit header. */
4029
4030static sect_offset
4031read_abbrev_offset (struct dwarf2_section_info *section,
4032 sect_offset offset)
4033{
4034 bfd *abfd = section->asection->owner;
4035 gdb_byte *info_ptr;
4036 unsigned int length, initial_length_size, offset_size;
4037 sect_offset abbrev_offset;
4038
4039 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4040 info_ptr = section->buffer + offset.sect_off;
4041 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4042 offset_size = initial_length_size == 4 ? 4 : 8;
4043 info_ptr += initial_length_size + 2 /*version*/;
4044 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4045 return abbrev_offset;
4046}
4047
aaa75496
JB
4048/* Allocate a new partial symtab for file named NAME and mark this new
4049 partial symtab as being an include of PST. */
4050
4051static void
4052dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4053 struct objfile *objfile)
4054{
4055 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4056
4057 subpst->section_offsets = pst->section_offsets;
4058 subpst->textlow = 0;
4059 subpst->texthigh = 0;
4060
4061 subpst->dependencies = (struct partial_symtab **)
4062 obstack_alloc (&objfile->objfile_obstack,
4063 sizeof (struct partial_symtab *));
4064 subpst->dependencies[0] = pst;
4065 subpst->number_of_dependencies = 1;
4066
4067 subpst->globals_offset = 0;
4068 subpst->n_global_syms = 0;
4069 subpst->statics_offset = 0;
4070 subpst->n_static_syms = 0;
4071 subpst->symtab = NULL;
4072 subpst->read_symtab = pst->read_symtab;
4073 subpst->readin = 0;
4074
4075 /* No private part is necessary for include psymtabs. This property
4076 can be used to differentiate between such include psymtabs and
10b3939b 4077 the regular ones. */
58a9656e 4078 subpst->read_symtab_private = NULL;
aaa75496
JB
4079}
4080
4081/* Read the Line Number Program data and extract the list of files
4082 included by the source file represented by PST. Build an include
d85a05f0 4083 partial symtab for each of these included files. */
aaa75496
JB
4084
4085static void
4086dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4087 struct die_info *die,
4088 struct partial_symtab *pst)
aaa75496 4089{
d85a05f0
DJ
4090 struct line_header *lh = NULL;
4091 struct attribute *attr;
aaa75496 4092
d85a05f0
DJ
4093 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4094 if (attr)
3019eac3 4095 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4096 if (lh == NULL)
4097 return; /* No linetable, so no includes. */
4098
c6da4cef 4099 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4100 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4101
4102 free_line_header (lh);
4103}
4104
348e048f 4105static hashval_t
52dc124a 4106hash_signatured_type (const void *item)
348e048f 4107{
52dc124a 4108 const struct signatured_type *sig_type = item;
9a619af0 4109
348e048f 4110 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4111 return sig_type->signature;
348e048f
DE
4112}
4113
4114static int
52dc124a 4115eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4116{
4117 const struct signatured_type *lhs = item_lhs;
4118 const struct signatured_type *rhs = item_rhs;
9a619af0 4119
348e048f
DE
4120 return lhs->signature == rhs->signature;
4121}
4122
1fd400ff
TT
4123/* Allocate a hash table for signatured types. */
4124
4125static htab_t
673bfd45 4126allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4127{
4128 return htab_create_alloc_ex (41,
52dc124a
DE
4129 hash_signatured_type,
4130 eq_signatured_type,
1fd400ff
TT
4131 NULL,
4132 &objfile->objfile_obstack,
4133 hashtab_obstack_allocate,
4134 dummy_obstack_deallocate);
4135}
4136
d467dd73 4137/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4138
4139static int
d467dd73 4140add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4141{
4142 struct signatured_type *sigt = *slot;
b4dd5633 4143 struct signatured_type ***datap = datum;
1fd400ff 4144
b4dd5633 4145 **datap = sigt;
1fd400ff
TT
4146 ++*datap;
4147
4148 return 1;
4149}
4150
3019eac3 4151/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4152 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4153 NULL otherwise.
4154 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4155 The result is a pointer to the hash table or NULL if there are
4156 no types. */
348e048f 4157
3019eac3
DE
4158static htab_t
4159create_debug_types_hash_table (struct dwo_file *dwo_file,
4160 VEC (dwarf2_section_info_def) *types)
348e048f 4161{
3019eac3 4162 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4163 htab_t types_htab = NULL;
8b70b953
TT
4164 int ix;
4165 struct dwarf2_section_info *section;
4bdcc0c1 4166 struct dwarf2_section_info *abbrev_section;
348e048f 4167
3019eac3
DE
4168 if (VEC_empty (dwarf2_section_info_def, types))
4169 return NULL;
348e048f 4170
4bdcc0c1
DE
4171 abbrev_section = (dwo_file != NULL
4172 ? &dwo_file->sections.abbrev
4173 : &dwarf2_per_objfile->abbrev);
4174
09406207
DE
4175 if (dwarf2_read_debug)
4176 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4177 dwo_file ? ".dwo" : "",
4178 bfd_get_filename (abbrev_section->asection->owner));
4179
8b70b953 4180 for (ix = 0;
3019eac3 4181 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4182 ++ix)
4183 {
3019eac3 4184 bfd *abfd;
8b70b953 4185 gdb_byte *info_ptr, *end_ptr;
36586728 4186 struct dwarf2_section_info *abbrev_section;
348e048f 4187
8b70b953
TT
4188 dwarf2_read_section (objfile, section);
4189 info_ptr = section->buffer;
348e048f 4190
8b70b953
TT
4191 if (info_ptr == NULL)
4192 continue;
348e048f 4193
3019eac3
DE
4194 /* We can't set abfd until now because the section may be empty or
4195 not present, in which case section->asection will be NULL. */
4196 abfd = section->asection->owner;
4197
36586728
TT
4198 if (dwo_file)
4199 abbrev_section = &dwo_file->sections.abbrev;
4200 else
4201 abbrev_section = &dwarf2_per_objfile->abbrev;
4202
8b70b953 4203 if (types_htab == NULL)
3019eac3
DE
4204 {
4205 if (dwo_file)
4206 types_htab = allocate_dwo_unit_table (objfile);
4207 else
4208 types_htab = allocate_signatured_type_table (objfile);
4209 }
348e048f 4210
dee91e82
DE
4211 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4212 because we don't need to read any dies: the signature is in the
4213 header. */
8b70b953
TT
4214
4215 end_ptr = info_ptr + section->size;
4216 while (info_ptr < end_ptr)
4217 {
b64f50a1 4218 sect_offset offset;
3019eac3 4219 cu_offset type_offset_in_tu;
8b70b953 4220 ULONGEST signature;
52dc124a 4221 struct signatured_type *sig_type;
3019eac3 4222 struct dwo_unit *dwo_tu;
8b70b953
TT
4223 void **slot;
4224 gdb_byte *ptr = info_ptr;
9ff913ba 4225 struct comp_unit_head header;
dee91e82 4226 unsigned int length;
348e048f 4227
b64f50a1 4228 offset.sect_off = ptr - section->buffer;
348e048f 4229
8b70b953 4230 /* We need to read the type's signature in order to build the hash
9ff913ba 4231 table, but we don't need anything else just yet. */
348e048f 4232
4bdcc0c1
DE
4233 ptr = read_and_check_type_unit_head (&header, section,
4234 abbrev_section, ptr,
3019eac3 4235 &signature, &type_offset_in_tu);
6caca83c 4236
1ce1cefd 4237 length = get_cu_length (&header);
dee91e82 4238
6caca83c 4239 /* Skip dummy type units. */
dee91e82
DE
4240 if (ptr >= info_ptr + length
4241 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4242 {
1ce1cefd 4243 info_ptr += length;
6caca83c
CC
4244 continue;
4245 }
8b70b953 4246
3019eac3
DE
4247 if (dwo_file)
4248 {
4249 sig_type = NULL;
4250 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4251 struct dwo_unit);
4252 dwo_tu->dwo_file = dwo_file;
4253 dwo_tu->signature = signature;
4254 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4255 dwo_tu->info_or_types_section = section;
4256 dwo_tu->offset = offset;
4257 dwo_tu->length = length;
4258 }
4259 else
4260 {
4261 /* N.B.: type_offset is not usable if this type uses a DWO file.
4262 The real type_offset is in the DWO file. */
4263 dwo_tu = NULL;
4264 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4265 struct signatured_type);
4266 sig_type->signature = signature;
4267 sig_type->type_offset_in_tu = type_offset_in_tu;
4268 sig_type->per_cu.objfile = objfile;
4269 sig_type->per_cu.is_debug_types = 1;
4270 sig_type->per_cu.info_or_types_section = section;
4271 sig_type->per_cu.offset = offset;
4272 sig_type->per_cu.length = length;
4273 }
8b70b953 4274
3019eac3
DE
4275 slot = htab_find_slot (types_htab,
4276 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4277 INSERT);
8b70b953
TT
4278 gdb_assert (slot != NULL);
4279 if (*slot != NULL)
4280 {
3019eac3
DE
4281 sect_offset dup_offset;
4282
4283 if (dwo_file)
4284 {
4285 const struct dwo_unit *dup_tu = *slot;
4286
4287 dup_offset = dup_tu->offset;
4288 }
4289 else
4290 {
4291 const struct signatured_type *dup_tu = *slot;
4292
4293 dup_offset = dup_tu->per_cu.offset;
4294 }
b3c8eb43 4295
8b70b953
TT
4296 complaint (&symfile_complaints,
4297 _("debug type entry at offset 0x%x is duplicate to the "
4298 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4299 offset.sect_off, dup_offset.sect_off,
8b70b953 4300 phex (signature, sizeof (signature)));
8b70b953 4301 }
3019eac3 4302 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4303
09406207 4304 if (dwarf2_read_debug)
8b70b953 4305 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4306 offset.sect_off,
4307 phex (signature, sizeof (signature)));
348e048f 4308
dee91e82 4309 info_ptr += length;
8b70b953 4310 }
348e048f
DE
4311 }
4312
3019eac3
DE
4313 return types_htab;
4314}
4315
4316/* Create the hash table of all entries in the .debug_types section,
4317 and initialize all_type_units.
4318 The result is zero if there is an error (e.g. missing .debug_types section),
4319 otherwise non-zero. */
4320
4321static int
4322create_all_type_units (struct objfile *objfile)
4323{
4324 htab_t types_htab;
b4dd5633 4325 struct signatured_type **iter;
3019eac3
DE
4326
4327 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4328 if (types_htab == NULL)
4329 {
4330 dwarf2_per_objfile->signatured_types = NULL;
4331 return 0;
4332 }
4333
348e048f
DE
4334 dwarf2_per_objfile->signatured_types = types_htab;
4335
d467dd73
DE
4336 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4337 dwarf2_per_objfile->all_type_units
1fd400ff 4338 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4339 dwarf2_per_objfile->n_type_units
b4dd5633 4340 * sizeof (struct signatured_type *));
d467dd73
DE
4341 iter = &dwarf2_per_objfile->all_type_units[0];
4342 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4343 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4344 == dwarf2_per_objfile->n_type_units);
1fd400ff 4345
348e048f
DE
4346 return 1;
4347}
4348
380bca97 4349/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4350 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4351
4352static struct signatured_type *
e319fa28 4353lookup_signatured_type (ULONGEST sig)
348e048f
DE
4354{
4355 struct signatured_type find_entry, *entry;
4356
4357 if (dwarf2_per_objfile->signatured_types == NULL)
4358 {
4359 complaint (&symfile_complaints,
55f1336d 4360 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4361 return NULL;
348e048f
DE
4362 }
4363
4364 find_entry.signature = sig;
4365 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4366 return entry;
4367}
42e7ad6c
DE
4368\f
4369/* Low level DIE reading support. */
348e048f 4370
d85a05f0
DJ
4371/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4372
4373static void
4374init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4375 struct dwarf2_cu *cu,
3019eac3
DE
4376 struct dwarf2_section_info *section,
4377 struct dwo_file *dwo_file)
d85a05f0 4378{
fceca515 4379 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4380 reader->abfd = section->asection->owner;
d85a05f0 4381 reader->cu = cu;
3019eac3 4382 reader->dwo_file = dwo_file;
dee91e82
DE
4383 reader->die_section = section;
4384 reader->buffer = section->buffer;
f664829e 4385 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4386}
4387
fd820528 4388/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4389 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4390
f4dc4d17
DE
4391 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4392 Otherwise the table specified in the comp unit header is read in and used.
4393 This is an optimization for when we already have the abbrev table.
4394
dee91e82
DE
4395 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4396 Otherwise, a new CU is allocated with xmalloc.
4397
4398 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4399 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4400
4401 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4402 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4403
70221824 4404static void
fd820528 4405init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4406 struct abbrev_table *abbrev_table,
fd820528
DE
4407 int use_existing_cu, int keep,
4408 die_reader_func_ftype *die_reader_func,
4409 void *data)
c906108c 4410{
dee91e82 4411 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4412 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4413 bfd *abfd = section->asection->owner;
dee91e82
DE
4414 struct dwarf2_cu *cu;
4415 gdb_byte *begin_info_ptr, *info_ptr;
4416 struct die_reader_specs reader;
d85a05f0 4417 struct die_info *comp_unit_die;
dee91e82 4418 int has_children;
d85a05f0 4419 struct attribute *attr;
dee91e82
DE
4420 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4421 struct signatured_type *sig_type = NULL;
4bdcc0c1 4422 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4423 /* Non-zero if CU currently points to a DWO file and we need to
4424 reread it. When this happens we need to reread the skeleton die
4425 before we can reread the DWO file. */
4426 int rereading_dwo_cu = 0;
c906108c 4427
09406207
DE
4428 if (dwarf2_die_debug)
4429 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4430 this_cu->is_debug_types ? "type" : "comp",
4431 this_cu->offset.sect_off);
4432
dee91e82
DE
4433 if (use_existing_cu)
4434 gdb_assert (keep);
23745b47 4435
dee91e82
DE
4436 cleanups = make_cleanup (null_cleanup, NULL);
4437
4438 /* This is cheap if the section is already read in. */
4439 dwarf2_read_section (objfile, section);
4440
4441 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4442
4443 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4444
4445 if (use_existing_cu && this_cu->cu != NULL)
4446 {
4447 cu = this_cu->cu;
42e7ad6c
DE
4448
4449 /* If this CU is from a DWO file we need to start over, we need to
4450 refetch the attributes from the skeleton CU.
4451 This could be optimized by retrieving those attributes from when we
4452 were here the first time: the previous comp_unit_die was stored in
4453 comp_unit_obstack. But there's no data yet that we need this
4454 optimization. */
4455 if (cu->dwo_unit != NULL)
4456 rereading_dwo_cu = 1;
dee91e82
DE
4457 }
4458 else
4459 {
4460 /* If !use_existing_cu, this_cu->cu must be NULL. */
4461 gdb_assert (this_cu->cu == NULL);
4462
4463 cu = xmalloc (sizeof (*cu));
4464 init_one_comp_unit (cu, this_cu);
4465
4466 /* If an error occurs while loading, release our storage. */
4467 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4468 }
dee91e82 4469
42e7ad6c
DE
4470 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4471 {
4472 /* We already have the header, there's no need to read it in again. */
4473 info_ptr += cu->header.first_die_offset.cu_off;
4474 }
4475 else
4476 {
3019eac3 4477 if (this_cu->is_debug_types)
dee91e82
DE
4478 {
4479 ULONGEST signature;
42e7ad6c 4480 cu_offset type_offset_in_tu;
dee91e82 4481
4bdcc0c1
DE
4482 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4483 abbrev_section, info_ptr,
42e7ad6c
DE
4484 &signature,
4485 &type_offset_in_tu);
dee91e82 4486
42e7ad6c
DE
4487 /* Since per_cu is the first member of struct signatured_type,
4488 we can go from a pointer to one to a pointer to the other. */
4489 sig_type = (struct signatured_type *) this_cu;
4490 gdb_assert (sig_type->signature == signature);
4491 gdb_assert (sig_type->type_offset_in_tu.cu_off
4492 == type_offset_in_tu.cu_off);
dee91e82
DE
4493 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4494
42e7ad6c
DE
4495 /* LENGTH has not been set yet for type units if we're
4496 using .gdb_index. */
1ce1cefd 4497 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4498
4499 /* Establish the type offset that can be used to lookup the type. */
4500 sig_type->type_offset_in_section.sect_off =
4501 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4502 }
4503 else
4504 {
4bdcc0c1
DE
4505 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4506 abbrev_section,
4507 info_ptr, 0);
dee91e82
DE
4508
4509 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4510 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4511 }
4512 }
10b3939b 4513
6caca83c 4514 /* Skip dummy compilation units. */
dee91e82 4515 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4516 || peek_abbrev_code (abfd, info_ptr) == 0)
4517 {
dee91e82 4518 do_cleanups (cleanups);
21b2bd31 4519 return;
6caca83c
CC
4520 }
4521
433df2d4
DE
4522 /* If we don't have them yet, read the abbrevs for this compilation unit.
4523 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4524 done. Note that it's important that if the CU had an abbrev table
4525 on entry we don't free it when we're done: Somewhere up the call stack
4526 it may be in use. */
f4dc4d17
DE
4527 if (abbrev_table != NULL)
4528 {
4529 gdb_assert (cu->abbrev_table == NULL);
4530 gdb_assert (cu->header.abbrev_offset.sect_off
4531 == abbrev_table->offset.sect_off);
4532 cu->abbrev_table = abbrev_table;
4533 }
4534 else if (cu->abbrev_table == NULL)
dee91e82 4535 {
4bdcc0c1 4536 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4537 make_cleanup (dwarf2_free_abbrev_table, cu);
4538 }
42e7ad6c
DE
4539 else if (rereading_dwo_cu)
4540 {
4541 dwarf2_free_abbrev_table (cu);
4542 dwarf2_read_abbrevs (cu, abbrev_section);
4543 }
af703f96 4544
dee91e82 4545 /* Read the top level CU/TU die. */
3019eac3 4546 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4547 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4548
3019eac3
DE
4549 /* If we have a DWO stub, process it and then read in the DWO file.
4550 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4551 a DWO CU, that this test will fail. */
4552 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4553 if (attr)
4554 {
15d034d0 4555 const char *dwo_name = DW_STRING (attr);
42e7ad6c 4556 const char *comp_dir_string;
3019eac3
DE
4557 struct dwo_unit *dwo_unit;
4558 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4559 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4560 int i,num_extra_attrs;
4bdcc0c1 4561 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4562
4563 if (has_children)
4564 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4565 " has children (offset 0x%x) [in module %s]"),
4566 this_cu->offset.sect_off, bfd_get_filename (abfd));
4567
4568 /* These attributes aren't processed until later:
4569 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4570 However, the attribute is found in the stub which we won't have later.
4571 In order to not impose this complication on the rest of the code,
4572 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4573
4574 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4575 DWO file. */
42e7ad6c 4576 stmt_list = NULL;
3019eac3
DE
4577 if (! this_cu->is_debug_types)
4578 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4579 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4580 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4581 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4582 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4583
4584 /* There should be a DW_AT_addr_base attribute here (if needed).
4585 We need the value before we can process DW_FORM_GNU_addr_index. */
4586 cu->addr_base = 0;
3019eac3
DE
4587 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4588 if (attr)
2e3cf129
DE
4589 cu->addr_base = DW_UNSND (attr);
4590
4591 /* There should be a DW_AT_ranges_base attribute here (if needed).
4592 We need the value before we can process DW_AT_ranges. */
4593 cu->ranges_base = 0;
4594 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4595 if (attr)
4596 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4597
4598 if (this_cu->is_debug_types)
4599 {
4600 gdb_assert (sig_type != NULL);
4601 signature = sig_type->signature;
4602 }
4603 else
4604 {
4605 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4606 if (! attr)
4607 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4608 dwo_name);
4609 signature = DW_UNSND (attr);
4610 }
4611
4612 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4613 comp_dir_string = NULL;
4614 if (comp_dir)
4615 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4616
4617 if (this_cu->is_debug_types)
42e7ad6c 4618 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4619 else
42e7ad6c 4620 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4621 signature);
4622
4623 if (dwo_unit == NULL)
4624 {
4625 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4626 " with ID %s [in module %s]"),
4627 this_cu->offset.sect_off,
4628 phex (signature, sizeof (signature)),
4629 objfile->name);
4630 }
4631
4632 /* Set up for reading the DWO CU/TU. */
4633 cu->dwo_unit = dwo_unit;
4634 section = dwo_unit->info_or_types_section;
80626a55 4635 dwarf2_read_section (objfile, section);
3019eac3 4636 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4637 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4638 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4639
4640 if (this_cu->is_debug_types)
4641 {
4642 ULONGEST signature;
80626a55 4643 cu_offset type_offset_in_tu;
3019eac3 4644
4bdcc0c1
DE
4645 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4646 dwo_abbrev_section,
4647 info_ptr,
80626a55
DE
4648 &signature,
4649 &type_offset_in_tu);
3019eac3
DE
4650 gdb_assert (sig_type->signature == signature);
4651 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4652 /* For DWOs coming from DWP files, we don't know the CU length
4653 nor the type's offset in the TU until now. */
4654 dwo_unit->length = get_cu_length (&cu->header);
4655 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4656
4657 /* Establish the type offset that can be used to lookup the type.
4658 For DWO files, we don't know it until now. */
4659 sig_type->type_offset_in_section.sect_off =
4660 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4661 }
4662 else
4663 {
4bdcc0c1
DE
4664 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4665 dwo_abbrev_section,
4666 info_ptr, 0);
3019eac3 4667 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4668 /* For DWOs coming from DWP files, we don't know the CU length
4669 until now. */
4670 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4671 }
4672
4673 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4674 if (abbrev_table == NULL)
4675 {
4676 dwarf2_free_abbrev_table (cu);
4677 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4678 }
4679 else
4680 {
4681 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4682 make_cleanup (dwarf2_free_abbrev_table, cu);
4683 }
3019eac3
DE
4684
4685 /* Read in the die, but leave space to copy over the attributes
4686 from the stub. This has the benefit of simplifying the rest of
4687 the code - all the real work is done here. */
4688 num_extra_attrs = ((stmt_list != NULL)
4689 + (low_pc != NULL)
4690 + (high_pc != NULL)
42e7ad6c
DE
4691 + (ranges != NULL)
4692 + (comp_dir != NULL));
3019eac3
DE
4693 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4694 &has_children, num_extra_attrs);
4695
4696 /* Copy over the attributes from the stub to the DWO die. */
4697 i = comp_unit_die->num_attrs;
4698 if (stmt_list != NULL)
4699 comp_unit_die->attrs[i++] = *stmt_list;
4700 if (low_pc != NULL)
4701 comp_unit_die->attrs[i++] = *low_pc;
4702 if (high_pc != NULL)
4703 comp_unit_die->attrs[i++] = *high_pc;
4704 if (ranges != NULL)
4705 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4706 if (comp_dir != NULL)
4707 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4708 comp_unit_die->num_attrs += num_extra_attrs;
4709
4710 /* Skip dummy compilation units. */
4711 if (info_ptr >= begin_info_ptr + dwo_unit->length
4712 || peek_abbrev_code (abfd, info_ptr) == 0)
4713 {
4714 do_cleanups (cleanups);
4715 return;
4716 }
4717 }
4718
dee91e82
DE
4719 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4720
4721 if (free_cu_cleanup != NULL)
348e048f 4722 {
dee91e82
DE
4723 if (keep)
4724 {
4725 /* We've successfully allocated this compilation unit. Let our
4726 caller clean it up when finished with it. */
4727 discard_cleanups (free_cu_cleanup);
4728
4729 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4730 So we have to manually free the abbrev table. */
4731 dwarf2_free_abbrev_table (cu);
4732
4733 /* Link this CU into read_in_chain. */
4734 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4735 dwarf2_per_objfile->read_in_chain = this_cu;
4736 }
4737 else
4738 do_cleanups (free_cu_cleanup);
348e048f 4739 }
dee91e82
DE
4740
4741 do_cleanups (cleanups);
4742}
4743
3019eac3
DE
4744/* Read CU/TU THIS_CU in section SECTION,
4745 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4746 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4747 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4748
4749 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4750 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4751
4752 We fill in THIS_CU->length.
4753
4754 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4755 linker) then DIE_READER_FUNC will not get called.
4756
4757 THIS_CU->cu is always freed when done.
3019eac3
DE
4758 This is done in order to not leave THIS_CU->cu in a state where we have
4759 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4760
4761static void
4762init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4763 struct dwarf2_section_info *abbrev_section,
3019eac3 4764 struct dwo_file *dwo_file,
dee91e82
DE
4765 die_reader_func_ftype *die_reader_func,
4766 void *data)
4767{
4768 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4769 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4770 bfd *abfd = section->asection->owner;
dee91e82
DE
4771 struct dwarf2_cu cu;
4772 gdb_byte *begin_info_ptr, *info_ptr;
4773 struct die_reader_specs reader;
4774 struct cleanup *cleanups;
4775 struct die_info *comp_unit_die;
4776 int has_children;
4777
09406207
DE
4778 if (dwarf2_die_debug)
4779 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4780 this_cu->is_debug_types ? "type" : "comp",
4781 this_cu->offset.sect_off);
4782
dee91e82
DE
4783 gdb_assert (this_cu->cu == NULL);
4784
dee91e82
DE
4785 /* This is cheap if the section is already read in. */
4786 dwarf2_read_section (objfile, section);
4787
4788 init_one_comp_unit (&cu, this_cu);
4789
4790 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4791
4792 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4793 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4794 abbrev_section, info_ptr,
3019eac3 4795 this_cu->is_debug_types);
dee91e82 4796
1ce1cefd 4797 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4798
4799 /* Skip dummy compilation units. */
4800 if (info_ptr >= begin_info_ptr + this_cu->length
4801 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4802 {
dee91e82 4803 do_cleanups (cleanups);
21b2bd31 4804 return;
93311388 4805 }
72bf9492 4806
dee91e82
DE
4807 dwarf2_read_abbrevs (&cu, abbrev_section);
4808 make_cleanup (dwarf2_free_abbrev_table, &cu);
4809
3019eac3 4810 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4811 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4812
4813 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4814
4815 do_cleanups (cleanups);
4816}
4817
3019eac3
DE
4818/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4819 does not lookup the specified DWO file.
4820 This cannot be used to read DWO files.
dee91e82
DE
4821
4822 THIS_CU->cu is always freed when done.
3019eac3
DE
4823 This is done in order to not leave THIS_CU->cu in a state where we have
4824 to care whether it refers to the "main" CU or the DWO CU.
4825 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4826
4827static void
4828init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4829 die_reader_func_ftype *die_reader_func,
4830 void *data)
4831{
4832 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4833 get_abbrev_section_for_cu (this_cu),
3019eac3 4834 NULL,
dee91e82
DE
4835 die_reader_func, data);
4836}
4837
f4dc4d17
DE
4838/* Create a psymtab named NAME and assign it to PER_CU.
4839
4840 The caller must fill in the following details:
4841 dirname, textlow, texthigh. */
4842
4843static struct partial_symtab *
4844create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4845{
4846 struct objfile *objfile = per_cu->objfile;
4847 struct partial_symtab *pst;
4848
4849 pst = start_psymtab_common (objfile, objfile->section_offsets,
4850 name, 0,
4851 objfile->global_psymbols.next,
4852 objfile->static_psymbols.next);
4853
4854 pst->psymtabs_addrmap_supported = 1;
4855
4856 /* This is the glue that links PST into GDB's symbol API. */
4857 pst->read_symtab_private = per_cu;
257e7a09 4858 pst->read_symtab = dwarf2_read_symtab;
f4dc4d17
DE
4859 per_cu->v.psymtab = pst;
4860
4861 return pst;
4862}
4863
dee91e82
DE
4864/* die_reader_func for process_psymtab_comp_unit. */
4865
4866static void
4867process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4868 gdb_byte *info_ptr,
4869 struct die_info *comp_unit_die,
4870 int has_children,
4871 void *data)
4872{
4873 struct dwarf2_cu *cu = reader->cu;
4874 struct objfile *objfile = cu->objfile;
4875 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4876 struct attribute *attr;
4877 CORE_ADDR baseaddr;
4878 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4879 struct partial_symtab *pst;
4880 int has_pc_info;
4881 const char *filename;
95554aad 4882 int *want_partial_unit_ptr = data;
dee91e82 4883
95554aad
TT
4884 if (comp_unit_die->tag == DW_TAG_partial_unit
4885 && (want_partial_unit_ptr == NULL
4886 || !*want_partial_unit_ptr))
dee91e82
DE
4887 return;
4888
f4dc4d17
DE
4889 gdb_assert (! per_cu->is_debug_types);
4890
95554aad 4891 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4892
4893 cu->list_in_scope = &file_symbols;
c906108c 4894
93311388 4895 /* Allocate a new partial symbol table structure. */
dee91e82 4896 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4897 if (attr == NULL || !DW_STRING (attr))
4898 filename = "";
4899 else
4900 filename = DW_STRING (attr);
72bf9492 4901
f4dc4d17
DE
4902 pst = create_partial_symtab (per_cu, filename);
4903
4904 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4905 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4906 if (attr != NULL)
4907 pst->dirname = DW_STRING (attr);
72bf9492 4908
93311388 4909 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4910
dee91e82 4911 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4912
93311388
DE
4913 /* Possibly set the default values of LOWPC and HIGHPC from
4914 `DW_AT_ranges'. */
d85a05f0 4915 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4916 &best_highpc, cu, pst);
d85a05f0 4917 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4918 /* Store the contiguous range if it is not empty; it can be empty for
4919 CUs with no code. */
4920 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4921 best_lowpc + baseaddr,
4922 best_highpc + baseaddr - 1, pst);
93311388
DE
4923
4924 /* Check if comp unit has_children.
4925 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4926 If not, there's no more debug_info for this comp unit. */
d85a05f0 4927 if (has_children)
93311388
DE
4928 {
4929 struct partial_die_info *first_die;
4930 CORE_ADDR lowpc, highpc;
31ffec48 4931
93311388
DE
4932 lowpc = ((CORE_ADDR) -1);
4933 highpc = ((CORE_ADDR) 0);
c906108c 4934
dee91e82 4935 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4936
93311388 4937 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4938 ! has_pc_info, cu);
57c22c6c 4939
93311388
DE
4940 /* If we didn't find a lowpc, set it to highpc to avoid
4941 complaints from `maint check'. */
4942 if (lowpc == ((CORE_ADDR) -1))
4943 lowpc = highpc;
10b3939b 4944
93311388
DE
4945 /* If the compilation unit didn't have an explicit address range,
4946 then use the information extracted from its child dies. */
d85a05f0 4947 if (! has_pc_info)
93311388 4948 {
d85a05f0
DJ
4949 best_lowpc = lowpc;
4950 best_highpc = highpc;
93311388
DE
4951 }
4952 }
d85a05f0
DJ
4953 pst->textlow = best_lowpc + baseaddr;
4954 pst->texthigh = best_highpc + baseaddr;
c906108c 4955
93311388
DE
4956 pst->n_global_syms = objfile->global_psymbols.next -
4957 (objfile->global_psymbols.list + pst->globals_offset);
4958 pst->n_static_syms = objfile->static_psymbols.next -
4959 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 4960 sort_pst_symbols (objfile, pst);
c906108c 4961
f4dc4d17 4962 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4963 {
4964 int i;
f4dc4d17 4965 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4966 struct dwarf2_per_cu_data *iter;
4967
4968 /* Fill in 'dependencies' here; we fill in 'users' in a
4969 post-pass. */
4970 pst->number_of_dependencies = len;
4971 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4972 len * sizeof (struct symtab *));
4973 for (i = 0;
f4dc4d17 4974 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4975 i, iter);
4976 ++i)
4977 pst->dependencies[i] = iter->v.psymtab;
4978
f4dc4d17 4979 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4980 }
4981
f4dc4d17
DE
4982 /* Get the list of files included in the current compilation unit,
4983 and build a psymtab for each of them. */
4984 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4985
4986 if (dwarf2_read_debug)
4987 {
4988 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4989
4990 fprintf_unfiltered (gdb_stdlog,
844226d6 4991 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4992 ", %d global, %d static syms\n",
4993 per_cu->is_debug_types ? "type" : "comp",
4994 per_cu->offset.sect_off,
4995 paddress (gdbarch, pst->textlow),
4996 paddress (gdbarch, pst->texthigh),
4997 pst->n_global_syms, pst->n_static_syms);
4998 }
dee91e82 4999}
ae038cb0 5000
dee91e82
DE
5001/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5002 Process compilation unit THIS_CU for a psymtab. */
5003
5004static void
95554aad
TT
5005process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5006 int want_partial_unit)
dee91e82
DE
5007{
5008 /* If this compilation unit was already read in, free the
5009 cached copy in order to read it in again. This is
5010 necessary because we skipped some symbols when we first
5011 read in the compilation unit (see load_partial_dies).
5012 This problem could be avoided, but the benefit is unclear. */
5013 if (this_cu->cu != NULL)
5014 free_one_cached_comp_unit (this_cu);
5015
3019eac3 5016 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
5017 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5018 process_psymtab_comp_unit_reader,
95554aad 5019 &want_partial_unit);
dee91e82
DE
5020
5021 /* Age out any secondary CUs. */
5022 age_cached_comp_units ();
93311388 5023}
ff013f42 5024
f4dc4d17
DE
5025static hashval_t
5026hash_type_unit_group (const void *item)
5027{
094b34ac 5028 const struct type_unit_group *tu_group = item;
f4dc4d17 5029
094b34ac 5030 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5031}
348e048f
DE
5032
5033static int
f4dc4d17 5034eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5035{
f4dc4d17
DE
5036 const struct type_unit_group *lhs = item_lhs;
5037 const struct type_unit_group *rhs = item_rhs;
348e048f 5038
094b34ac 5039 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5040}
348e048f 5041
f4dc4d17
DE
5042/* Allocate a hash table for type unit groups. */
5043
5044static htab_t
5045allocate_type_unit_groups_table (void)
5046{
5047 return htab_create_alloc_ex (3,
5048 hash_type_unit_group,
5049 eq_type_unit_group,
5050 NULL,
5051 &dwarf2_per_objfile->objfile->objfile_obstack,
5052 hashtab_obstack_allocate,
5053 dummy_obstack_deallocate);
5054}
dee91e82 5055
f4dc4d17
DE
5056/* Type units that don't have DW_AT_stmt_list are grouped into their own
5057 partial symtabs. We combine several TUs per psymtab to not let the size
5058 of any one psymtab grow too big. */
5059#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5060#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5061
094b34ac 5062/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5063 Create the type_unit_group object used to hold one or more TUs. */
5064
5065static struct type_unit_group *
094b34ac 5066create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5067{
5068 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5069 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5070 struct type_unit_group *tu_group;
f4dc4d17
DE
5071
5072 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5073 struct type_unit_group);
094b34ac 5074 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5075 per_cu->objfile = objfile;
5076 per_cu->is_debug_types = 1;
5077 per_cu->s.type_unit_group = tu_group;
5078
094b34ac
DE
5079 if (dwarf2_per_objfile->using_index)
5080 {
5081 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5082 struct dwarf2_per_cu_quick_data);
5083 tu_group->t.first_tu = cu->per_cu;
5084 }
5085 else
5086 {
5087 unsigned int line_offset = line_offset_struct.sect_off;
5088 struct partial_symtab *pst;
5089 char *name;
5090
5091 /* Give the symtab a useful name for debug purposes. */
5092 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5093 name = xstrprintf ("<type_units_%d>",
5094 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5095 else
5096 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5097
5098 pst = create_partial_symtab (per_cu, name);
5099 pst->anonymous = 1;
f4dc4d17 5100
094b34ac
DE
5101 xfree (name);
5102 }
f4dc4d17 5103
094b34ac
DE
5104 tu_group->hash.dwo_unit = cu->dwo_unit;
5105 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5106
5107 return tu_group;
5108}
5109
094b34ac
DE
5110/* Look up the type_unit_group for type unit CU, and create it if necessary.
5111 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5112
5113static struct type_unit_group *
094b34ac 5114get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5115{
5116 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5117 struct type_unit_group *tu_group;
5118 void **slot;
5119 unsigned int line_offset;
5120 struct type_unit_group type_unit_group_for_lookup;
5121
5122 if (dwarf2_per_objfile->type_unit_groups == NULL)
5123 {
5124 dwarf2_per_objfile->type_unit_groups =
5125 allocate_type_unit_groups_table ();
5126 }
5127
5128 /* Do we need to create a new group, or can we use an existing one? */
5129
5130 if (stmt_list)
5131 {
5132 line_offset = DW_UNSND (stmt_list);
5133 ++tu_stats->nr_symtab_sharers;
5134 }
5135 else
5136 {
5137 /* Ugh, no stmt_list. Rare, but we have to handle it.
5138 We can do various things here like create one group per TU or
5139 spread them over multiple groups to split up the expansion work.
5140 To avoid worst case scenarios (too many groups or too large groups)
5141 we, umm, group them in bunches. */
5142 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5143 | (tu_stats->nr_stmt_less_type_units
5144 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5145 ++tu_stats->nr_stmt_less_type_units;
5146 }
5147
094b34ac
DE
5148 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5149 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5150 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5151 &type_unit_group_for_lookup, INSERT);
5152 if (*slot != NULL)
5153 {
5154 tu_group = *slot;
5155 gdb_assert (tu_group != NULL);
5156 }
5157 else
5158 {
5159 sect_offset line_offset_struct;
5160
5161 line_offset_struct.sect_off = line_offset;
094b34ac 5162 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5163 *slot = tu_group;
5164 ++tu_stats->nr_symtabs;
5165 }
5166
5167 return tu_group;
5168}
5169
5170/* Struct used to sort TUs by their abbreviation table offset. */
5171
5172struct tu_abbrev_offset
5173{
5174 struct signatured_type *sig_type;
5175 sect_offset abbrev_offset;
5176};
5177
5178/* Helper routine for build_type_unit_groups, passed to qsort. */
5179
5180static int
5181sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5182{
5183 const struct tu_abbrev_offset * const *a = ap;
5184 const struct tu_abbrev_offset * const *b = bp;
5185 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5186 unsigned int boff = (*b)->abbrev_offset.sect_off;
5187
5188 return (aoff > boff) - (aoff < boff);
5189}
5190
5191/* A helper function to add a type_unit_group to a table. */
5192
5193static int
5194add_type_unit_group_to_table (void **slot, void *datum)
5195{
5196 struct type_unit_group *tu_group = *slot;
5197 struct type_unit_group ***datap = datum;
5198
5199 **datap = tu_group;
5200 ++*datap;
5201
5202 return 1;
5203}
5204
5205/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5206 each one passing FUNC,DATA.
5207
5208 The efficiency is because we sort TUs by the abbrev table they use and
5209 only read each abbrev table once. In one program there are 200K TUs
5210 sharing 8K abbrev tables.
5211
5212 The main purpose of this function is to support building the
5213 dwarf2_per_objfile->type_unit_groups table.
5214 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5215 can collapse the search space by grouping them by stmt_list.
5216 The savings can be significant, in the same program from above the 200K TUs
5217 share 8K stmt_list tables.
5218
5219 FUNC is expected to call get_type_unit_group, which will create the
5220 struct type_unit_group if necessary and add it to
5221 dwarf2_per_objfile->type_unit_groups. */
5222
5223static void
5224build_type_unit_groups (die_reader_func_ftype *func, void *data)
5225{
5226 struct objfile *objfile = dwarf2_per_objfile->objfile;
5227 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5228 struct cleanup *cleanups;
5229 struct abbrev_table *abbrev_table;
5230 sect_offset abbrev_offset;
5231 struct tu_abbrev_offset *sorted_by_abbrev;
5232 struct type_unit_group **iter;
5233 int i;
5234
5235 /* It's up to the caller to not call us multiple times. */
5236 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5237
5238 if (dwarf2_per_objfile->n_type_units == 0)
5239 return;
5240
5241 /* TUs typically share abbrev tables, and there can be way more TUs than
5242 abbrev tables. Sort by abbrev table to reduce the number of times we
5243 read each abbrev table in.
5244 Alternatives are to punt or to maintain a cache of abbrev tables.
5245 This is simpler and efficient enough for now.
5246
5247 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5248 symtab to use). Typically TUs with the same abbrev offset have the same
5249 stmt_list value too so in practice this should work well.
5250
5251 The basic algorithm here is:
5252
5253 sort TUs by abbrev table
5254 for each TU with same abbrev table:
5255 read abbrev table if first user
5256 read TU top level DIE
5257 [IWBN if DWO skeletons had DW_AT_stmt_list]
5258 call FUNC */
5259
5260 if (dwarf2_read_debug)
5261 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5262
5263 /* Sort in a separate table to maintain the order of all_type_units
5264 for .gdb_index: TU indices directly index all_type_units. */
5265 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5266 dwarf2_per_objfile->n_type_units);
5267 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5268 {
5269 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5270
5271 sorted_by_abbrev[i].sig_type = sig_type;
5272 sorted_by_abbrev[i].abbrev_offset =
5273 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5274 sig_type->per_cu.offset);
5275 }
5276 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5277 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5278 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5279
094b34ac
DE
5280 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5281 called any number of times, so we don't reset tu_stats here. */
5282
f4dc4d17
DE
5283 abbrev_offset.sect_off = ~(unsigned) 0;
5284 abbrev_table = NULL;
5285 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5286
5287 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5288 {
5289 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5290
5291 /* Switch to the next abbrev table if necessary. */
5292 if (abbrev_table == NULL
5293 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5294 {
5295 if (abbrev_table != NULL)
5296 {
5297 abbrev_table_free (abbrev_table);
5298 /* Reset to NULL in case abbrev_table_read_table throws
5299 an error: abbrev_table_free_cleanup will get called. */
5300 abbrev_table = NULL;
5301 }
5302 abbrev_offset = tu->abbrev_offset;
5303 abbrev_table =
5304 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5305 abbrev_offset);
5306 ++tu_stats->nr_uniq_abbrev_tables;
5307 }
5308
5309 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5310 func, data);
5311 }
5312
5313 /* Create a vector of pointers to primary type units to make it easy to
5314 iterate over them and CUs. See dw2_get_primary_cu. */
5315 dwarf2_per_objfile->n_type_unit_groups =
5316 htab_elements (dwarf2_per_objfile->type_unit_groups);
5317 dwarf2_per_objfile->all_type_unit_groups =
5318 obstack_alloc (&objfile->objfile_obstack,
5319 dwarf2_per_objfile->n_type_unit_groups
5320 * sizeof (struct type_unit_group *));
5321 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5322 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5323 add_type_unit_group_to_table, &iter);
5324 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5325 == dwarf2_per_objfile->n_type_unit_groups);
5326
5327 do_cleanups (cleanups);
5328
5329 if (dwarf2_read_debug)
5330 {
5331 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5332 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5333 dwarf2_per_objfile->n_type_units);
5334 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5335 tu_stats->nr_uniq_abbrev_tables);
5336 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5337 tu_stats->nr_symtabs);
5338 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5339 tu_stats->nr_symtab_sharers);
5340 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5341 tu_stats->nr_stmt_less_type_units);
5342 }
5343}
5344
5345/* Reader function for build_type_psymtabs. */
5346
5347static void
5348build_type_psymtabs_reader (const struct die_reader_specs *reader,
5349 gdb_byte *info_ptr,
5350 struct die_info *type_unit_die,
5351 int has_children,
5352 void *data)
5353{
5354 struct objfile *objfile = dwarf2_per_objfile->objfile;
5355 struct dwarf2_cu *cu = reader->cu;
5356 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5357 struct type_unit_group *tu_group;
5358 struct attribute *attr;
5359 struct partial_die_info *first_die;
5360 CORE_ADDR lowpc, highpc;
5361 struct partial_symtab *pst;
5362
5363 gdb_assert (data == NULL);
5364
5365 if (! has_children)
5366 return;
5367
5368 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5369 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5370
094b34ac 5371 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5372
5373 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5374 cu->list_in_scope = &file_symbols;
5375 pst = create_partial_symtab (per_cu, "");
5376 pst->anonymous = 1;
5377
5378 first_die = load_partial_dies (reader, info_ptr, 1);
5379
5380 lowpc = (CORE_ADDR) -1;
5381 highpc = (CORE_ADDR) 0;
5382 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5383
5384 pst->n_global_syms = objfile->global_psymbols.next -
5385 (objfile->global_psymbols.list + pst->globals_offset);
5386 pst->n_static_syms = objfile->static_psymbols.next -
5387 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5388 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5389}
5390
5391/* Traversal function for build_type_psymtabs. */
5392
5393static int
5394build_type_psymtab_dependencies (void **slot, void *info)
5395{
5396 struct objfile *objfile = dwarf2_per_objfile->objfile;
5397 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5398 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5399 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5400 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5401 struct dwarf2_per_cu_data *iter;
5402 int i;
5403
5404 gdb_assert (len > 0);
5405
5406 pst->number_of_dependencies = len;
5407 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5408 len * sizeof (struct psymtab *));
5409 for (i = 0;
094b34ac 5410 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5411 ++i)
5412 {
5413 pst->dependencies[i] = iter->v.psymtab;
5414 iter->s.type_unit_group = tu_group;
5415 }
5416
094b34ac 5417 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5418
5419 return 1;
5420}
5421
5422/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5423 Build partial symbol tables for the .debug_types comp-units. */
5424
5425static void
5426build_type_psymtabs (struct objfile *objfile)
5427{
0e50663e 5428 if (! create_all_type_units (objfile))
348e048f
DE
5429 return;
5430
f4dc4d17
DE
5431 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5432
5433 /* Now that all TUs have been processed we can fill in the dependencies. */
5434 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5435 build_type_psymtab_dependencies, NULL);
348e048f
DE
5436}
5437
60606b2c
TT
5438/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5439
5440static void
5441psymtabs_addrmap_cleanup (void *o)
5442{
5443 struct objfile *objfile = o;
ec61707d 5444
60606b2c
TT
5445 objfile->psymtabs_addrmap = NULL;
5446}
5447
95554aad
TT
5448/* Compute the 'user' field for each psymtab in OBJFILE. */
5449
5450static void
5451set_partial_user (struct objfile *objfile)
5452{
5453 int i;
5454
5455 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5456 {
5457 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5458 struct partial_symtab *pst = per_cu->v.psymtab;
5459 int j;
5460
36586728
TT
5461 if (pst == NULL)
5462 continue;
5463
95554aad
TT
5464 for (j = 0; j < pst->number_of_dependencies; ++j)
5465 {
5466 /* Set the 'user' field only if it is not already set. */
5467 if (pst->dependencies[j]->user == NULL)
5468 pst->dependencies[j]->user = pst;
5469 }
5470 }
5471}
5472
93311388
DE
5473/* Build the partial symbol table by doing a quick pass through the
5474 .debug_info and .debug_abbrev sections. */
72bf9492 5475
93311388 5476static void
c67a9c90 5477dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5478{
60606b2c
TT
5479 struct cleanup *back_to, *addrmap_cleanup;
5480 struct obstack temp_obstack;
21b2bd31 5481 int i;
93311388 5482
45cfd468
DE
5483 if (dwarf2_read_debug)
5484 {
5485 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5486 objfile->name);
5487 }
5488
98bfdba5
PA
5489 dwarf2_per_objfile->reading_partial_symbols = 1;
5490
be391dca 5491 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5492
93311388
DE
5493 /* Any cached compilation units will be linked by the per-objfile
5494 read_in_chain. Make sure to free them when we're done. */
5495 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5496
348e048f
DE
5497 build_type_psymtabs (objfile);
5498
93311388 5499 create_all_comp_units (objfile);
c906108c 5500
60606b2c
TT
5501 /* Create a temporary address map on a temporary obstack. We later
5502 copy this to the final obstack. */
5503 obstack_init (&temp_obstack);
5504 make_cleanup_obstack_free (&temp_obstack);
5505 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5506 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5507
21b2bd31 5508 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5509 {
21b2bd31 5510 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5511
95554aad 5512 process_psymtab_comp_unit (per_cu, 0);
c906108c 5513 }
ff013f42 5514
95554aad
TT
5515 set_partial_user (objfile);
5516
ff013f42
JK
5517 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5518 &objfile->objfile_obstack);
60606b2c 5519 discard_cleanups (addrmap_cleanup);
ff013f42 5520
ae038cb0 5521 do_cleanups (back_to);
45cfd468
DE
5522
5523 if (dwarf2_read_debug)
5524 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5525 objfile->name);
ae038cb0
DJ
5526}
5527
3019eac3 5528/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5529
5530static void
dee91e82
DE
5531load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5532 gdb_byte *info_ptr,
5533 struct die_info *comp_unit_die,
5534 int has_children,
5535 void *data)
ae038cb0 5536{
dee91e82 5537 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5538
95554aad 5539 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5540
ae038cb0
DJ
5541 /* Check if comp unit has_children.
5542 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5543 If not, there's no more debug_info for this comp unit. */
d85a05f0 5544 if (has_children)
dee91e82
DE
5545 load_partial_dies (reader, info_ptr, 0);
5546}
98bfdba5 5547
dee91e82
DE
5548/* Load the partial DIEs for a secondary CU into memory.
5549 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5550
dee91e82
DE
5551static void
5552load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5553{
f4dc4d17
DE
5554 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5555 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5556}
5557
ae038cb0 5558static void
36586728
TT
5559read_comp_units_from_section (struct objfile *objfile,
5560 struct dwarf2_section_info *section,
5561 unsigned int is_dwz,
5562 int *n_allocated,
5563 int *n_comp_units,
5564 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5565{
be391dca 5566 gdb_byte *info_ptr;
36586728 5567 bfd *abfd = section->asection->owner;
be391dca 5568
36586728 5569 dwarf2_read_section (objfile, section);
ae038cb0 5570
36586728 5571 info_ptr = section->buffer;
6e70227d 5572
36586728 5573 while (info_ptr < section->buffer + section->size)
ae038cb0 5574 {
c764a876 5575 unsigned int length, initial_length_size;
ae038cb0 5576 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5577 sect_offset offset;
ae038cb0 5578
36586728 5579 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5580
5581 /* Read just enough information to find out where the next
5582 compilation unit is. */
36586728 5583 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5584
5585 /* Save the compilation unit for later lookup. */
5586 this_cu = obstack_alloc (&objfile->objfile_obstack,
5587 sizeof (struct dwarf2_per_cu_data));
5588 memset (this_cu, 0, sizeof (*this_cu));
5589 this_cu->offset = offset;
c764a876 5590 this_cu->length = length + initial_length_size;
36586728 5591 this_cu->is_dwz = is_dwz;
9291a0cd 5592 this_cu->objfile = objfile;
36586728 5593 this_cu->info_or_types_section = section;
ae038cb0 5594
36586728 5595 if (*n_comp_units == *n_allocated)
ae038cb0 5596 {
36586728
TT
5597 *n_allocated *= 2;
5598 *all_comp_units = xrealloc (*all_comp_units,
5599 *n_allocated
5600 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5601 }
36586728
TT
5602 (*all_comp_units)[*n_comp_units] = this_cu;
5603 ++*n_comp_units;
ae038cb0
DJ
5604
5605 info_ptr = info_ptr + this_cu->length;
5606 }
36586728
TT
5607}
5608
5609/* Create a list of all compilation units in OBJFILE.
5610 This is only done for -readnow and building partial symtabs. */
5611
5612static void
5613create_all_comp_units (struct objfile *objfile)
5614{
5615 int n_allocated;
5616 int n_comp_units;
5617 struct dwarf2_per_cu_data **all_comp_units;
5618
5619 n_comp_units = 0;
5620 n_allocated = 10;
5621 all_comp_units = xmalloc (n_allocated
5622 * sizeof (struct dwarf2_per_cu_data *));
5623
5624 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5625 &n_allocated, &n_comp_units, &all_comp_units);
5626
5627 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5628 {
5629 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5630
5631 read_comp_units_from_section (objfile, &dwz->info, 1,
5632 &n_allocated, &n_comp_units,
5633 &all_comp_units);
5634 }
ae038cb0
DJ
5635
5636 dwarf2_per_objfile->all_comp_units
5637 = obstack_alloc (&objfile->objfile_obstack,
5638 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5639 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5640 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5641 xfree (all_comp_units);
5642 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5643}
5644
5734ee8b
DJ
5645/* Process all loaded DIEs for compilation unit CU, starting at
5646 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5647 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5648 DW_AT_ranges). If NEED_PC is set, then this function will set
5649 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5650 and record the covered ranges in the addrmap. */
c906108c 5651
72bf9492
DJ
5652static void
5653scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5654 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5655{
72bf9492 5656 struct partial_die_info *pdi;
c906108c 5657
91c24f0a
DC
5658 /* Now, march along the PDI's, descending into ones which have
5659 interesting children but skipping the children of the other ones,
5660 until we reach the end of the compilation unit. */
c906108c 5661
72bf9492 5662 pdi = first_die;
91c24f0a 5663
72bf9492
DJ
5664 while (pdi != NULL)
5665 {
5666 fixup_partial_die (pdi, cu);
c906108c 5667
f55ee35c 5668 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5669 children, so we need to look at them. Ditto for anonymous
5670 enums. */
933c6fe4 5671
72bf9492 5672 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5673 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5674 || pdi->tag == DW_TAG_imported_unit)
c906108c 5675 {
72bf9492 5676 switch (pdi->tag)
c906108c
SS
5677 {
5678 case DW_TAG_subprogram:
5734ee8b 5679 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5680 break;
72929c62 5681 case DW_TAG_constant:
c906108c
SS
5682 case DW_TAG_variable:
5683 case DW_TAG_typedef:
91c24f0a 5684 case DW_TAG_union_type:
72bf9492 5685 if (!pdi->is_declaration)
63d06c5c 5686 {
72bf9492 5687 add_partial_symbol (pdi, cu);
63d06c5c
DC
5688 }
5689 break;
c906108c 5690 case DW_TAG_class_type:
680b30c7 5691 case DW_TAG_interface_type:
c906108c 5692 case DW_TAG_structure_type:
72bf9492 5693 if (!pdi->is_declaration)
c906108c 5694 {
72bf9492 5695 add_partial_symbol (pdi, cu);
c906108c
SS
5696 }
5697 break;
91c24f0a 5698 case DW_TAG_enumeration_type:
72bf9492
DJ
5699 if (!pdi->is_declaration)
5700 add_partial_enumeration (pdi, cu);
c906108c
SS
5701 break;
5702 case DW_TAG_base_type:
a02abb62 5703 case DW_TAG_subrange_type:
c906108c 5704 /* File scope base type definitions are added to the partial
c5aa993b 5705 symbol table. */
72bf9492 5706 add_partial_symbol (pdi, cu);
c906108c 5707 break;
d9fa45fe 5708 case DW_TAG_namespace:
5734ee8b 5709 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5710 break;
5d7cb8df
JK
5711 case DW_TAG_module:
5712 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5713 break;
95554aad
TT
5714 case DW_TAG_imported_unit:
5715 {
5716 struct dwarf2_per_cu_data *per_cu;
5717
f4dc4d17
DE
5718 /* For now we don't handle imported units in type units. */
5719 if (cu->per_cu->is_debug_types)
5720 {
5721 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5722 " supported in type units [in module %s]"),
5723 cu->objfile->name);
5724 }
5725
95554aad 5726 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5727 pdi->is_dwz,
95554aad
TT
5728 cu->objfile);
5729
5730 /* Go read the partial unit, if needed. */
5731 if (per_cu->v.psymtab == NULL)
5732 process_psymtab_comp_unit (per_cu, 1);
5733
f4dc4d17
DE
5734 VEC_safe_push (dwarf2_per_cu_ptr,
5735 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5736 }
5737 break;
c906108c
SS
5738 default:
5739 break;
5740 }
5741 }
5742
72bf9492
DJ
5743 /* If the die has a sibling, skip to the sibling. */
5744
5745 pdi = pdi->die_sibling;
5746 }
5747}
5748
5749/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5750
72bf9492 5751 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5752 name is concatenated with "::" and the partial DIE's name. For
5753 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5754 Enumerators are an exception; they use the scope of their parent
5755 enumeration type, i.e. the name of the enumeration type is not
5756 prepended to the enumerator.
91c24f0a 5757
72bf9492
DJ
5758 There are two complexities. One is DW_AT_specification; in this
5759 case "parent" means the parent of the target of the specification,
5760 instead of the direct parent of the DIE. The other is compilers
5761 which do not emit DW_TAG_namespace; in this case we try to guess
5762 the fully qualified name of structure types from their members'
5763 linkage names. This must be done using the DIE's children rather
5764 than the children of any DW_AT_specification target. We only need
5765 to do this for structures at the top level, i.e. if the target of
5766 any DW_AT_specification (if any; otherwise the DIE itself) does not
5767 have a parent. */
5768
5769/* Compute the scope prefix associated with PDI's parent, in
5770 compilation unit CU. The result will be allocated on CU's
5771 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5772 field. NULL is returned if no prefix is necessary. */
15d034d0 5773static const char *
72bf9492
DJ
5774partial_die_parent_scope (struct partial_die_info *pdi,
5775 struct dwarf2_cu *cu)
5776{
15d034d0 5777 const char *grandparent_scope;
72bf9492 5778 struct partial_die_info *parent, *real_pdi;
91c24f0a 5779
72bf9492
DJ
5780 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5781 then this means the parent of the specification DIE. */
5782
5783 real_pdi = pdi;
72bf9492 5784 while (real_pdi->has_specification)
36586728
TT
5785 real_pdi = find_partial_die (real_pdi->spec_offset,
5786 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5787
5788 parent = real_pdi->die_parent;
5789 if (parent == NULL)
5790 return NULL;
5791
5792 if (parent->scope_set)
5793 return parent->scope;
5794
5795 fixup_partial_die (parent, cu);
5796
10b3939b 5797 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5798
acebe513
UW
5799 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5800 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5801 Work around this problem here. */
5802 if (cu->language == language_cplus
6e70227d 5803 && parent->tag == DW_TAG_namespace
acebe513
UW
5804 && strcmp (parent->name, "::") == 0
5805 && grandparent_scope == NULL)
5806 {
5807 parent->scope = NULL;
5808 parent->scope_set = 1;
5809 return NULL;
5810 }
5811
9c6c53f7
SA
5812 if (pdi->tag == DW_TAG_enumerator)
5813 /* Enumerators should not get the name of the enumeration as a prefix. */
5814 parent->scope = grandparent_scope;
5815 else if (parent->tag == DW_TAG_namespace
f55ee35c 5816 || parent->tag == DW_TAG_module
72bf9492
DJ
5817 || parent->tag == DW_TAG_structure_type
5818 || parent->tag == DW_TAG_class_type
680b30c7 5819 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5820 || parent->tag == DW_TAG_union_type
5821 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5822 {
5823 if (grandparent_scope == NULL)
5824 parent->scope = parent->name;
5825 else
3e43a32a
MS
5826 parent->scope = typename_concat (&cu->comp_unit_obstack,
5827 grandparent_scope,
f55ee35c 5828 parent->name, 0, cu);
72bf9492 5829 }
72bf9492
DJ
5830 else
5831 {
5832 /* FIXME drow/2004-04-01: What should we be doing with
5833 function-local names? For partial symbols, we should probably be
5834 ignoring them. */
5835 complaint (&symfile_complaints,
e2e0b3e5 5836 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5837 parent->tag, pdi->offset.sect_off);
72bf9492 5838 parent->scope = grandparent_scope;
c906108c
SS
5839 }
5840
72bf9492
DJ
5841 parent->scope_set = 1;
5842 return parent->scope;
5843}
5844
5845/* Return the fully scoped name associated with PDI, from compilation unit
5846 CU. The result will be allocated with malloc. */
4568ecf9 5847
72bf9492
DJ
5848static char *
5849partial_die_full_name (struct partial_die_info *pdi,
5850 struct dwarf2_cu *cu)
5851{
15d034d0 5852 const char *parent_scope;
72bf9492 5853
98bfdba5
PA
5854 /* If this is a template instantiation, we can not work out the
5855 template arguments from partial DIEs. So, unfortunately, we have
5856 to go through the full DIEs. At least any work we do building
5857 types here will be reused if full symbols are loaded later. */
5858 if (pdi->has_template_arguments)
5859 {
5860 fixup_partial_die (pdi, cu);
5861
5862 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5863 {
5864 struct die_info *die;
5865 struct attribute attr;
5866 struct dwarf2_cu *ref_cu = cu;
5867
b64f50a1 5868 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5869 attr.name = 0;
5870 attr.form = DW_FORM_ref_addr;
4568ecf9 5871 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5872 die = follow_die_ref (NULL, &attr, &ref_cu);
5873
5874 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5875 }
5876 }
5877
72bf9492
DJ
5878 parent_scope = partial_die_parent_scope (pdi, cu);
5879 if (parent_scope == NULL)
5880 return NULL;
5881 else
f55ee35c 5882 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5883}
5884
5885static void
72bf9492 5886add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5887{
e7c27a73 5888 struct objfile *objfile = cu->objfile;
c906108c 5889 CORE_ADDR addr = 0;
15d034d0 5890 const char *actual_name = NULL;
e142c38c 5891 CORE_ADDR baseaddr;
15d034d0 5892 char *built_actual_name;
e142c38c
DJ
5893
5894 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5895
15d034d0
TT
5896 built_actual_name = partial_die_full_name (pdi, cu);
5897 if (built_actual_name != NULL)
5898 actual_name = built_actual_name;
63d06c5c 5899
72bf9492
DJ
5900 if (actual_name == NULL)
5901 actual_name = pdi->name;
5902
c906108c
SS
5903 switch (pdi->tag)
5904 {
5905 case DW_TAG_subprogram:
2cfa0c8d 5906 if (pdi->is_external || cu->language == language_ada)
c906108c 5907 {
2cfa0c8d
JB
5908 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5909 of the global scope. But in Ada, we want to be able to access
5910 nested procedures globally. So all Ada subprograms are stored
5911 in the global scope. */
f47fb265 5912 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5913 mst_text, objfile); */
f47fb265 5914 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5915 built_actual_name != NULL,
f47fb265
MS
5916 VAR_DOMAIN, LOC_BLOCK,
5917 &objfile->global_psymbols,
5918 0, pdi->lowpc + baseaddr,
5919 cu->language, objfile);
c906108c
SS
5920 }
5921 else
5922 {
f47fb265 5923 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5924 mst_file_text, objfile); */
f47fb265 5925 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5926 built_actual_name != NULL,
f47fb265
MS
5927 VAR_DOMAIN, LOC_BLOCK,
5928 &objfile->static_psymbols,
5929 0, pdi->lowpc + baseaddr,
5930 cu->language, objfile);
c906108c
SS
5931 }
5932 break;
72929c62
JB
5933 case DW_TAG_constant:
5934 {
5935 struct psymbol_allocation_list *list;
5936
5937 if (pdi->is_external)
5938 list = &objfile->global_psymbols;
5939 else
5940 list = &objfile->static_psymbols;
f47fb265 5941 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5942 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 5943 list, 0, 0, cu->language, objfile);
72929c62
JB
5944 }
5945 break;
c906108c 5946 case DW_TAG_variable:
95554aad
TT
5947 if (pdi->d.locdesc)
5948 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5949
95554aad 5950 if (pdi->d.locdesc
caac4577
JG
5951 && addr == 0
5952 && !dwarf2_per_objfile->has_section_at_zero)
5953 {
5954 /* A global or static variable may also have been stripped
5955 out by the linker if unused, in which case its address
5956 will be nullified; do not add such variables into partial
5957 symbol table then. */
5958 }
5959 else if (pdi->is_external)
c906108c
SS
5960 {
5961 /* Global Variable.
5962 Don't enter into the minimal symbol tables as there is
5963 a minimal symbol table entry from the ELF symbols already.
5964 Enter into partial symbol table if it has a location
5965 descriptor or a type.
5966 If the location descriptor is missing, new_symbol will create
5967 a LOC_UNRESOLVED symbol, the address of the variable will then
5968 be determined from the minimal symbol table whenever the variable
5969 is referenced.
5970 The address for the partial symbol table entry is not
5971 used by GDB, but it comes in handy for debugging partial symbol
5972 table building. */
5973
95554aad 5974 if (pdi->d.locdesc || pdi->has_type)
f47fb265 5975 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5976 built_actual_name != NULL,
f47fb265
MS
5977 VAR_DOMAIN, LOC_STATIC,
5978 &objfile->global_psymbols,
5979 0, addr + baseaddr,
5980 cu->language, objfile);
c906108c
SS
5981 }
5982 else
5983 {
0963b4bd 5984 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5985 if (pdi->d.locdesc == NULL)
decbce07 5986 {
15d034d0 5987 xfree (built_actual_name);
decbce07
MS
5988 return;
5989 }
f47fb265 5990 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5991 mst_file_data, objfile); */
f47fb265 5992 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5993 built_actual_name != NULL,
f47fb265
MS
5994 VAR_DOMAIN, LOC_STATIC,
5995 &objfile->static_psymbols,
5996 0, addr + baseaddr,
5997 cu->language, objfile);
c906108c
SS
5998 }
5999 break;
6000 case DW_TAG_typedef:
6001 case DW_TAG_base_type:
a02abb62 6002 case DW_TAG_subrange_type:
38d518c9 6003 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6004 built_actual_name != NULL,
176620f1 6005 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6006 &objfile->static_psymbols,
e142c38c 6007 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6008 break;
72bf9492
DJ
6009 case DW_TAG_namespace:
6010 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6011 built_actual_name != NULL,
72bf9492
DJ
6012 VAR_DOMAIN, LOC_TYPEDEF,
6013 &objfile->global_psymbols,
6014 0, (CORE_ADDR) 0, cu->language, objfile);
6015 break;
c906108c 6016 case DW_TAG_class_type:
680b30c7 6017 case DW_TAG_interface_type:
c906108c
SS
6018 case DW_TAG_structure_type:
6019 case DW_TAG_union_type:
6020 case DW_TAG_enumeration_type:
fa4028e9
JB
6021 /* Skip external references. The DWARF standard says in the section
6022 about "Structure, Union, and Class Type Entries": "An incomplete
6023 structure, union or class type is represented by a structure,
6024 union or class entry that does not have a byte size attribute
6025 and that has a DW_AT_declaration attribute." */
6026 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6027 {
15d034d0 6028 xfree (built_actual_name);
decbce07
MS
6029 return;
6030 }
fa4028e9 6031
63d06c5c
DC
6032 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6033 static vs. global. */
38d518c9 6034 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6035 built_actual_name != NULL,
176620f1 6036 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6037 (cu->language == language_cplus
6038 || cu->language == language_java)
63d06c5c
DC
6039 ? &objfile->global_psymbols
6040 : &objfile->static_psymbols,
e142c38c 6041 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6042
c906108c
SS
6043 break;
6044 case DW_TAG_enumerator:
38d518c9 6045 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6046 built_actual_name != NULL,
176620f1 6047 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6048 (cu->language == language_cplus
6049 || cu->language == language_java)
f6fe98ef
DJ
6050 ? &objfile->global_psymbols
6051 : &objfile->static_psymbols,
e142c38c 6052 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6053 break;
6054 default:
6055 break;
6056 }
5c4e30ca 6057
15d034d0 6058 xfree (built_actual_name);
c906108c
SS
6059}
6060
5c4e30ca
DC
6061/* Read a partial die corresponding to a namespace; also, add a symbol
6062 corresponding to that namespace to the symbol table. NAMESPACE is
6063 the name of the enclosing namespace. */
91c24f0a 6064
72bf9492
DJ
6065static void
6066add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6068 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6069{
72bf9492 6070 /* Add a symbol for the namespace. */
e7c27a73 6071
72bf9492 6072 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6073
6074 /* Now scan partial symbols in that namespace. */
6075
91c24f0a 6076 if (pdi->has_children)
5734ee8b 6077 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6078}
6079
5d7cb8df
JK
6080/* Read a partial die corresponding to a Fortran module. */
6081
6082static void
6083add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6084 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6085{
f55ee35c 6086 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6087
6088 if (pdi->has_children)
6089 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6090}
6091
bc30ff58
JB
6092/* Read a partial die corresponding to a subprogram and create a partial
6093 symbol for that subprogram. When the CU language allows it, this
6094 routine also defines a partial symbol for each nested subprogram
6095 that this subprogram contains.
6e70227d 6096
bc30ff58
JB
6097 DIE my also be a lexical block, in which case we simply search
6098 recursively for suprograms defined inside that lexical block.
6099 Again, this is only performed when the CU language allows this
6100 type of definitions. */
6101
6102static void
6103add_partial_subprogram (struct partial_die_info *pdi,
6104 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6105 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6106{
6107 if (pdi->tag == DW_TAG_subprogram)
6108 {
6109 if (pdi->has_pc_info)
6110 {
6111 if (pdi->lowpc < *lowpc)
6112 *lowpc = pdi->lowpc;
6113 if (pdi->highpc > *highpc)
6114 *highpc = pdi->highpc;
5734ee8b
DJ
6115 if (need_pc)
6116 {
6117 CORE_ADDR baseaddr;
6118 struct objfile *objfile = cu->objfile;
6119
6120 baseaddr = ANOFFSET (objfile->section_offsets,
6121 SECT_OFF_TEXT (objfile));
6122 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6123 pdi->lowpc + baseaddr,
6124 pdi->highpc - 1 + baseaddr,
9291a0cd 6125 cu->per_cu->v.psymtab);
5734ee8b 6126 }
481860b3
GB
6127 }
6128
6129 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6130 {
bc30ff58 6131 if (!pdi->is_declaration)
e8d05480
JB
6132 /* Ignore subprogram DIEs that do not have a name, they are
6133 illegal. Do not emit a complaint at this point, we will
6134 do so when we convert this psymtab into a symtab. */
6135 if (pdi->name)
6136 add_partial_symbol (pdi, cu);
bc30ff58
JB
6137 }
6138 }
6e70227d 6139
bc30ff58
JB
6140 if (! pdi->has_children)
6141 return;
6142
6143 if (cu->language == language_ada)
6144 {
6145 pdi = pdi->die_child;
6146 while (pdi != NULL)
6147 {
6148 fixup_partial_die (pdi, cu);
6149 if (pdi->tag == DW_TAG_subprogram
6150 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6151 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6152 pdi = pdi->die_sibling;
6153 }
6154 }
6155}
6156
91c24f0a
DC
6157/* Read a partial die corresponding to an enumeration type. */
6158
72bf9492
DJ
6159static void
6160add_partial_enumeration (struct partial_die_info *enum_pdi,
6161 struct dwarf2_cu *cu)
91c24f0a 6162{
72bf9492 6163 struct partial_die_info *pdi;
91c24f0a
DC
6164
6165 if (enum_pdi->name != NULL)
72bf9492
DJ
6166 add_partial_symbol (enum_pdi, cu);
6167
6168 pdi = enum_pdi->die_child;
6169 while (pdi)
91c24f0a 6170 {
72bf9492 6171 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6172 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6173 else
72bf9492
DJ
6174 add_partial_symbol (pdi, cu);
6175 pdi = pdi->die_sibling;
91c24f0a 6176 }
91c24f0a
DC
6177}
6178
6caca83c
CC
6179/* Return the initial uleb128 in the die at INFO_PTR. */
6180
6181static unsigned int
6182peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6183{
6184 unsigned int bytes_read;
6185
6186 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6187}
6188
4bb7a0a7
DJ
6189/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6190 Return the corresponding abbrev, or NULL if the number is zero (indicating
6191 an empty DIE). In either case *BYTES_READ will be set to the length of
6192 the initial number. */
6193
6194static struct abbrev_info *
fe1b8b76 6195peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6196 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6197{
6198 bfd *abfd = cu->objfile->obfd;
6199 unsigned int abbrev_number;
6200 struct abbrev_info *abbrev;
6201
6202 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6203
6204 if (abbrev_number == 0)
6205 return NULL;
6206
433df2d4 6207 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6208 if (!abbrev)
6209 {
3e43a32a
MS
6210 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6211 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6212 }
6213
6214 return abbrev;
6215}
6216
93311388
DE
6217/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6218 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6219 DIE. Any children of the skipped DIEs will also be skipped. */
6220
fe1b8b76 6221static gdb_byte *
dee91e82 6222skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6223{
dee91e82 6224 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6225 struct abbrev_info *abbrev;
6226 unsigned int bytes_read;
6227
6228 while (1)
6229 {
6230 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6231 if (abbrev == NULL)
6232 return info_ptr + bytes_read;
6233 else
dee91e82 6234 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6235 }
6236}
6237
93311388
DE
6238/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6239 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6240 abbrev corresponding to that skipped uleb128 should be passed in
6241 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6242 children. */
6243
fe1b8b76 6244static gdb_byte *
dee91e82
DE
6245skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6246 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6247{
6248 unsigned int bytes_read;
6249 struct attribute attr;
dee91e82
DE
6250 bfd *abfd = reader->abfd;
6251 struct dwarf2_cu *cu = reader->cu;
6252 gdb_byte *buffer = reader->buffer;
f664829e
DE
6253 const gdb_byte *buffer_end = reader->buffer_end;
6254 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6255 unsigned int form, i;
6256
6257 for (i = 0; i < abbrev->num_attrs; i++)
6258 {
6259 /* The only abbrev we care about is DW_AT_sibling. */
6260 if (abbrev->attrs[i].name == DW_AT_sibling)
6261 {
dee91e82 6262 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6263 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6264 complaint (&symfile_complaints,
6265 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6266 else
b64f50a1 6267 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6268 }
6269
6270 /* If it isn't DW_AT_sibling, skip this attribute. */
6271 form = abbrev->attrs[i].form;
6272 skip_attribute:
6273 switch (form)
6274 {
4bb7a0a7 6275 case DW_FORM_ref_addr:
ae411497
TT
6276 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6277 and later it is offset sized. */
6278 if (cu->header.version == 2)
6279 info_ptr += cu->header.addr_size;
6280 else
6281 info_ptr += cu->header.offset_size;
6282 break;
36586728
TT
6283 case DW_FORM_GNU_ref_alt:
6284 info_ptr += cu->header.offset_size;
6285 break;
ae411497 6286 case DW_FORM_addr:
4bb7a0a7
DJ
6287 info_ptr += cu->header.addr_size;
6288 break;
6289 case DW_FORM_data1:
6290 case DW_FORM_ref1:
6291 case DW_FORM_flag:
6292 info_ptr += 1;
6293 break;
2dc7f7b3
TT
6294 case DW_FORM_flag_present:
6295 break;
4bb7a0a7
DJ
6296 case DW_FORM_data2:
6297 case DW_FORM_ref2:
6298 info_ptr += 2;
6299 break;
6300 case DW_FORM_data4:
6301 case DW_FORM_ref4:
6302 info_ptr += 4;
6303 break;
6304 case DW_FORM_data8:
6305 case DW_FORM_ref8:
55f1336d 6306 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6307 info_ptr += 8;
6308 break;
6309 case DW_FORM_string:
9b1c24c8 6310 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6311 info_ptr += bytes_read;
6312 break;
2dc7f7b3 6313 case DW_FORM_sec_offset:
4bb7a0a7 6314 case DW_FORM_strp:
36586728 6315 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6316 info_ptr += cu->header.offset_size;
6317 break;
2dc7f7b3 6318 case DW_FORM_exprloc:
4bb7a0a7
DJ
6319 case DW_FORM_block:
6320 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6321 info_ptr += bytes_read;
6322 break;
6323 case DW_FORM_block1:
6324 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6325 break;
6326 case DW_FORM_block2:
6327 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6328 break;
6329 case DW_FORM_block4:
6330 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6331 break;
6332 case DW_FORM_sdata:
6333 case DW_FORM_udata:
6334 case DW_FORM_ref_udata:
3019eac3
DE
6335 case DW_FORM_GNU_addr_index:
6336 case DW_FORM_GNU_str_index:
f664829e 6337 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6338 break;
6339 case DW_FORM_indirect:
6340 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6341 info_ptr += bytes_read;
6342 /* We need to continue parsing from here, so just go back to
6343 the top. */
6344 goto skip_attribute;
6345
6346 default:
3e43a32a
MS
6347 error (_("Dwarf Error: Cannot handle %s "
6348 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6349 dwarf_form_name (form),
6350 bfd_get_filename (abfd));
6351 }
6352 }
6353
6354 if (abbrev->has_children)
dee91e82 6355 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6356 else
6357 return info_ptr;
6358}
6359
93311388 6360/* Locate ORIG_PDI's sibling.
dee91e82 6361 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6362
fe1b8b76 6363static gdb_byte *
dee91e82
DE
6364locate_pdi_sibling (const struct die_reader_specs *reader,
6365 struct partial_die_info *orig_pdi,
6366 gdb_byte *info_ptr)
91c24f0a
DC
6367{
6368 /* Do we know the sibling already? */
72bf9492 6369
91c24f0a
DC
6370 if (orig_pdi->sibling)
6371 return orig_pdi->sibling;
6372
6373 /* Are there any children to deal with? */
6374
6375 if (!orig_pdi->has_children)
6376 return info_ptr;
6377
4bb7a0a7 6378 /* Skip the children the long way. */
91c24f0a 6379
dee91e82 6380 return skip_children (reader, info_ptr);
91c24f0a
DC
6381}
6382
257e7a09 6383/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6384 not NULL. */
c906108c
SS
6385
6386static void
257e7a09
YQ
6387dwarf2_read_symtab (struct partial_symtab *self,
6388 struct objfile *objfile)
c906108c 6389{
257e7a09 6390 if (self->readin)
c906108c 6391 {
442e4d9c 6392 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6393 self->filename);
442e4d9c
YQ
6394 }
6395 else
6396 {
6397 if (info_verbose)
c906108c 6398 {
442e4d9c 6399 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6400 self->filename);
442e4d9c 6401 gdb_flush (gdb_stdout);
c906108c 6402 }
c906108c 6403
442e4d9c
YQ
6404 /* Restore our global data. */
6405 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6406
442e4d9c
YQ
6407 /* If this psymtab is constructed from a debug-only objfile, the
6408 has_section_at_zero flag will not necessarily be correct. We
6409 can get the correct value for this flag by looking at the data
6410 associated with the (presumably stripped) associated objfile. */
6411 if (objfile->separate_debug_objfile_backlink)
6412 {
6413 struct dwarf2_per_objfile *dpo_backlink
6414 = objfile_data (objfile->separate_debug_objfile_backlink,
6415 dwarf2_objfile_data_key);
9a619af0 6416
442e4d9c
YQ
6417 dwarf2_per_objfile->has_section_at_zero
6418 = dpo_backlink->has_section_at_zero;
6419 }
b2ab525c 6420
442e4d9c 6421 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6422
257e7a09 6423 psymtab_to_symtab_1 (self);
c906108c 6424
442e4d9c
YQ
6425 /* Finish up the debug error message. */
6426 if (info_verbose)
6427 printf_filtered (_("done.\n"));
c906108c 6428 }
95554aad
TT
6429
6430 process_cu_includes ();
c906108c 6431}
9cdd5dbd
DE
6432\f
6433/* Reading in full CUs. */
c906108c 6434
10b3939b
DJ
6435/* Add PER_CU to the queue. */
6436
6437static void
95554aad
TT
6438queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6439 enum language pretend_language)
10b3939b
DJ
6440{
6441 struct dwarf2_queue_item *item;
6442
6443 per_cu->queued = 1;
6444 item = xmalloc (sizeof (*item));
6445 item->per_cu = per_cu;
95554aad 6446 item->pretend_language = pretend_language;
10b3939b
DJ
6447 item->next = NULL;
6448
6449 if (dwarf2_queue == NULL)
6450 dwarf2_queue = item;
6451 else
6452 dwarf2_queue_tail->next = item;
6453
6454 dwarf2_queue_tail = item;
6455}
6456
0907af0c
DE
6457/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6458 unit and add it to our queue.
6459 The result is non-zero if PER_CU was queued, otherwise the result is zero
6460 meaning either PER_CU is already queued or it is already loaded. */
6461
6462static int
6463maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6464 struct dwarf2_per_cu_data *per_cu,
6465 enum language pretend_language)
6466{
6467 /* We may arrive here during partial symbol reading, if we need full
6468 DIEs to process an unusual case (e.g. template arguments). Do
6469 not queue PER_CU, just tell our caller to load its DIEs. */
6470 if (dwarf2_per_objfile->reading_partial_symbols)
6471 {
6472 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6473 return 1;
6474 return 0;
6475 }
6476
6477 /* Mark the dependence relation so that we don't flush PER_CU
6478 too early. */
6479 dwarf2_add_dependence (this_cu, per_cu);
6480
6481 /* If it's already on the queue, we have nothing to do. */
6482 if (per_cu->queued)
6483 return 0;
6484
6485 /* If the compilation unit is already loaded, just mark it as
6486 used. */
6487 if (per_cu->cu != NULL)
6488 {
6489 per_cu->cu->last_used = 0;
6490 return 0;
6491 }
6492
6493 /* Add it to the queue. */
6494 queue_comp_unit (per_cu, pretend_language);
6495
6496 return 1;
6497}
6498
10b3939b
DJ
6499/* Process the queue. */
6500
6501static void
a0f42c21 6502process_queue (void)
10b3939b
DJ
6503{
6504 struct dwarf2_queue_item *item, *next_item;
6505
45cfd468
DE
6506 if (dwarf2_read_debug)
6507 {
6508 fprintf_unfiltered (gdb_stdlog,
6509 "Expanding one or more symtabs of objfile %s ...\n",
6510 dwarf2_per_objfile->objfile->name);
6511 }
6512
03dd20cc
DJ
6513 /* The queue starts out with one item, but following a DIE reference
6514 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6515 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6516 {
9291a0cd
TT
6517 if (dwarf2_per_objfile->using_index
6518 ? !item->per_cu->v.quick->symtab
6519 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6520 {
6521 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6522
6523 if (dwarf2_read_debug)
6524 {
6525 fprintf_unfiltered (gdb_stdlog,
6526 "Expanding symtab of %s at offset 0x%x\n",
6527 per_cu->is_debug_types ? "TU" : "CU",
6528 per_cu->offset.sect_off);
6529 }
6530
6531 if (per_cu->is_debug_types)
6532 process_full_type_unit (per_cu, item->pretend_language);
6533 else
6534 process_full_comp_unit (per_cu, item->pretend_language);
6535
6536 if (dwarf2_read_debug)
6537 {
6538 fprintf_unfiltered (gdb_stdlog,
6539 "Done expanding %s at offset 0x%x\n",
6540 per_cu->is_debug_types ? "TU" : "CU",
6541 per_cu->offset.sect_off);
6542 }
6543 }
10b3939b
DJ
6544
6545 item->per_cu->queued = 0;
6546 next_item = item->next;
6547 xfree (item);
6548 }
6549
6550 dwarf2_queue_tail = NULL;
45cfd468
DE
6551
6552 if (dwarf2_read_debug)
6553 {
6554 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6555 dwarf2_per_objfile->objfile->name);
6556 }
10b3939b
DJ
6557}
6558
6559/* Free all allocated queue entries. This function only releases anything if
6560 an error was thrown; if the queue was processed then it would have been
6561 freed as we went along. */
6562
6563static void
6564dwarf2_release_queue (void *dummy)
6565{
6566 struct dwarf2_queue_item *item, *last;
6567
6568 item = dwarf2_queue;
6569 while (item)
6570 {
6571 /* Anything still marked queued is likely to be in an
6572 inconsistent state, so discard it. */
6573 if (item->per_cu->queued)
6574 {
6575 if (item->per_cu->cu != NULL)
dee91e82 6576 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6577 item->per_cu->queued = 0;
6578 }
6579
6580 last = item;
6581 item = item->next;
6582 xfree (last);
6583 }
6584
6585 dwarf2_queue = dwarf2_queue_tail = NULL;
6586}
6587
6588/* Read in full symbols for PST, and anything it depends on. */
6589
c906108c 6590static void
fba45db2 6591psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6592{
10b3939b 6593 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6594 int i;
6595
95554aad
TT
6596 if (pst->readin)
6597 return;
6598
aaa75496 6599 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6600 if (!pst->dependencies[i]->readin
6601 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6602 {
6603 /* Inform about additional files that need to be read in. */
6604 if (info_verbose)
6605 {
a3f17187 6606 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6607 fputs_filtered (" ", gdb_stdout);
6608 wrap_here ("");
6609 fputs_filtered ("and ", gdb_stdout);
6610 wrap_here ("");
6611 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6612 wrap_here (""); /* Flush output. */
aaa75496
JB
6613 gdb_flush (gdb_stdout);
6614 }
6615 psymtab_to_symtab_1 (pst->dependencies[i]);
6616 }
6617
e38df1d0 6618 per_cu = pst->read_symtab_private;
10b3939b
DJ
6619
6620 if (per_cu == NULL)
aaa75496
JB
6621 {
6622 /* It's an include file, no symbols to read for it.
6623 Everything is in the parent symtab. */
6624 pst->readin = 1;
6625 return;
6626 }
c906108c 6627
a0f42c21 6628 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6629}
6630
dee91e82
DE
6631/* Trivial hash function for die_info: the hash value of a DIE
6632 is its offset in .debug_info for this objfile. */
10b3939b 6633
dee91e82
DE
6634static hashval_t
6635die_hash (const void *item)
10b3939b 6636{
dee91e82 6637 const struct die_info *die = item;
6502dd73 6638
dee91e82
DE
6639 return die->offset.sect_off;
6640}
63d06c5c 6641
dee91e82
DE
6642/* Trivial comparison function for die_info structures: two DIEs
6643 are equal if they have the same offset. */
98bfdba5 6644
dee91e82
DE
6645static int
6646die_eq (const void *item_lhs, const void *item_rhs)
6647{
6648 const struct die_info *die_lhs = item_lhs;
6649 const struct die_info *die_rhs = item_rhs;
c906108c 6650
dee91e82
DE
6651 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6652}
c906108c 6653
dee91e82
DE
6654/* die_reader_func for load_full_comp_unit.
6655 This is identical to read_signatured_type_reader,
6656 but is kept separate for now. */
c906108c 6657
dee91e82
DE
6658static void
6659load_full_comp_unit_reader (const struct die_reader_specs *reader,
6660 gdb_byte *info_ptr,
6661 struct die_info *comp_unit_die,
6662 int has_children,
6663 void *data)
6664{
6665 struct dwarf2_cu *cu = reader->cu;
95554aad 6666 enum language *language_ptr = data;
6caca83c 6667
dee91e82
DE
6668 gdb_assert (cu->die_hash == NULL);
6669 cu->die_hash =
6670 htab_create_alloc_ex (cu->header.length / 12,
6671 die_hash,
6672 die_eq,
6673 NULL,
6674 &cu->comp_unit_obstack,
6675 hashtab_obstack_allocate,
6676 dummy_obstack_deallocate);
e142c38c 6677
dee91e82
DE
6678 if (has_children)
6679 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6680 &info_ptr, comp_unit_die);
6681 cu->dies = comp_unit_die;
6682 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6683
6684 /* We try not to read any attributes in this function, because not
9cdd5dbd 6685 all CUs needed for references have been loaded yet, and symbol
10b3939b 6686 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6687 or we won't be able to build types correctly.
6688 Similarly, if we do not read the producer, we can not apply
6689 producer-specific interpretation. */
95554aad 6690 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6691}
10b3939b 6692
dee91e82 6693/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6694
dee91e82 6695static void
95554aad
TT
6696load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6697 enum language pretend_language)
dee91e82 6698{
3019eac3 6699 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6700
f4dc4d17
DE
6701 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6702 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6703}
6704
3da10d80
KS
6705/* Add a DIE to the delayed physname list. */
6706
6707static void
6708add_to_method_list (struct type *type, int fnfield_index, int index,
6709 const char *name, struct die_info *die,
6710 struct dwarf2_cu *cu)
6711{
6712 struct delayed_method_info mi;
6713 mi.type = type;
6714 mi.fnfield_index = fnfield_index;
6715 mi.index = index;
6716 mi.name = name;
6717 mi.die = die;
6718 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6719}
6720
6721/* A cleanup for freeing the delayed method list. */
6722
6723static void
6724free_delayed_list (void *ptr)
6725{
6726 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6727 if (cu->method_list != NULL)
6728 {
6729 VEC_free (delayed_method_info, cu->method_list);
6730 cu->method_list = NULL;
6731 }
6732}
6733
6734/* Compute the physnames of any methods on the CU's method list.
6735
6736 The computation of method physnames is delayed in order to avoid the
6737 (bad) condition that one of the method's formal parameters is of an as yet
6738 incomplete type. */
6739
6740static void
6741compute_delayed_physnames (struct dwarf2_cu *cu)
6742{
6743 int i;
6744 struct delayed_method_info *mi;
6745 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6746 {
1d06ead6 6747 const char *physname;
3da10d80
KS
6748 struct fn_fieldlist *fn_flp
6749 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6750 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6751 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6752 }
6753}
6754
a766d390
DE
6755/* Go objects should be embedded in a DW_TAG_module DIE,
6756 and it's not clear if/how imported objects will appear.
6757 To keep Go support simple until that's worked out,
6758 go back through what we've read and create something usable.
6759 We could do this while processing each DIE, and feels kinda cleaner,
6760 but that way is more invasive.
6761 This is to, for example, allow the user to type "p var" or "b main"
6762 without having to specify the package name, and allow lookups
6763 of module.object to work in contexts that use the expression
6764 parser. */
6765
6766static void
6767fixup_go_packaging (struct dwarf2_cu *cu)
6768{
6769 char *package_name = NULL;
6770 struct pending *list;
6771 int i;
6772
6773 for (list = global_symbols; list != NULL; list = list->next)
6774 {
6775 for (i = 0; i < list->nsyms; ++i)
6776 {
6777 struct symbol *sym = list->symbol[i];
6778
6779 if (SYMBOL_LANGUAGE (sym) == language_go
6780 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6781 {
6782 char *this_package_name = go_symbol_package_name (sym);
6783
6784 if (this_package_name == NULL)
6785 continue;
6786 if (package_name == NULL)
6787 package_name = this_package_name;
6788 else
6789 {
6790 if (strcmp (package_name, this_package_name) != 0)
6791 complaint (&symfile_complaints,
6792 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6793 (SYMBOL_SYMTAB (sym)
210bbc17 6794 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6795 : cu->objfile->name),
6796 this_package_name, package_name);
6797 xfree (this_package_name);
6798 }
6799 }
6800 }
6801 }
6802
6803 if (package_name != NULL)
6804 {
6805 struct objfile *objfile = cu->objfile;
6806 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6807 package_name, objfile);
6808 struct symbol *sym;
6809
6810 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6811
6812 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6813 SYMBOL_SET_LANGUAGE (sym, language_go);
6814 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6815 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6816 e.g., "main" finds the "main" module and not C's main(). */
6817 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6818 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6819 SYMBOL_TYPE (sym) = type;
6820
6821 add_symbol_to_list (sym, &global_symbols);
6822
6823 xfree (package_name);
6824 }
6825}
6826
95554aad
TT
6827static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6828
6829/* Return the symtab for PER_CU. This works properly regardless of
6830 whether we're using the index or psymtabs. */
6831
6832static struct symtab *
6833get_symtab (struct dwarf2_per_cu_data *per_cu)
6834{
6835 return (dwarf2_per_objfile->using_index
6836 ? per_cu->v.quick->symtab
6837 : per_cu->v.psymtab->symtab);
6838}
6839
6840/* A helper function for computing the list of all symbol tables
6841 included by PER_CU. */
6842
6843static void
6844recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6845 htab_t all_children,
6846 struct dwarf2_per_cu_data *per_cu)
6847{
6848 void **slot;
6849 int ix;
6850 struct dwarf2_per_cu_data *iter;
6851
6852 slot = htab_find_slot (all_children, per_cu, INSERT);
6853 if (*slot != NULL)
6854 {
6855 /* This inclusion and its children have been processed. */
6856 return;
6857 }
6858
6859 *slot = per_cu;
6860 /* Only add a CU if it has a symbol table. */
6861 if (get_symtab (per_cu) != NULL)
6862 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6863
6864 for (ix = 0;
f4dc4d17 6865 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6866 ++ix)
6867 recursively_compute_inclusions (result, all_children, iter);
6868}
6869
6870/* Compute the symtab 'includes' fields for the symtab related to
6871 PER_CU. */
6872
6873static void
6874compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6875{
f4dc4d17
DE
6876 gdb_assert (! per_cu->is_debug_types);
6877
6878 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6879 {
6880 int ix, len;
6881 struct dwarf2_per_cu_data *iter;
6882 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6883 htab_t all_children;
6884 struct symtab *symtab = get_symtab (per_cu);
6885
6886 /* If we don't have a symtab, we can just skip this case. */
6887 if (symtab == NULL)
6888 return;
6889
6890 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6891 NULL, xcalloc, xfree);
6892
6893 for (ix = 0;
f4dc4d17 6894 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6895 ix, iter);
6896 ++ix)
6897 recursively_compute_inclusions (&result_children, all_children, iter);
6898
6899 /* Now we have a transitive closure of all the included CUs, so
6900 we can convert it to a list of symtabs. */
6901 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6902 symtab->includes
6903 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6904 (len + 1) * sizeof (struct symtab *));
6905 for (ix = 0;
6906 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6907 ++ix)
6908 symtab->includes[ix] = get_symtab (iter);
6909 symtab->includes[len] = NULL;
6910
6911 VEC_free (dwarf2_per_cu_ptr, result_children);
6912 htab_delete (all_children);
6913 }
6914}
6915
6916/* Compute the 'includes' field for the symtabs of all the CUs we just
6917 read. */
6918
6919static void
6920process_cu_includes (void)
6921{
6922 int ix;
6923 struct dwarf2_per_cu_data *iter;
6924
6925 for (ix = 0;
6926 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6927 ix, iter);
6928 ++ix)
f4dc4d17
DE
6929 {
6930 if (! iter->is_debug_types)
6931 compute_symtab_includes (iter);
6932 }
95554aad
TT
6933
6934 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6935}
6936
9cdd5dbd 6937/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6938 already been loaded into memory. */
6939
6940static void
95554aad
TT
6941process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6942 enum language pretend_language)
10b3939b 6943{
10b3939b 6944 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6945 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6946 CORE_ADDR lowpc, highpc;
6947 struct symtab *symtab;
3da10d80 6948 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6949 CORE_ADDR baseaddr;
4359dff1 6950 struct block *static_block;
10b3939b
DJ
6951
6952 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6953
10b3939b
DJ
6954 buildsym_init ();
6955 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6956 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6957
6958 cu->list_in_scope = &file_symbols;
c906108c 6959
95554aad
TT
6960 cu->language = pretend_language;
6961 cu->language_defn = language_def (cu->language);
6962
c906108c 6963 /* Do line number decoding in read_file_scope () */
10b3939b 6964 process_die (cu->dies, cu);
c906108c 6965
a766d390
DE
6966 /* For now fudge the Go package. */
6967 if (cu->language == language_go)
6968 fixup_go_packaging (cu);
6969
3da10d80
KS
6970 /* Now that we have processed all the DIEs in the CU, all the types
6971 should be complete, and it should now be safe to compute all of the
6972 physnames. */
6973 compute_delayed_physnames (cu);
6974 do_cleanups (delayed_list_cleanup);
6975
fae299cd
DC
6976 /* Some compilers don't define a DW_AT_high_pc attribute for the
6977 compilation unit. If the DW_AT_high_pc is missing, synthesize
6978 it, by scanning the DIE's below the compilation unit. */
10b3939b 6979 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6980
36586728
TT
6981 static_block
6982 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6983 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6984
6985 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6986 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6987 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6988 addrmap to help ensure it has an accurate map of pc values belonging to
6989 this comp unit. */
6990 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6991
6992 symtab = end_symtab_from_static_block (static_block, objfile,
6993 SECT_OFF_TEXT (objfile), 0);
c906108c 6994
8be455d7 6995 if (symtab != NULL)
c906108c 6996 {
df15bd07 6997 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6998
8be455d7
JK
6999 /* Set symtab language to language from DW_AT_language. If the
7000 compilation is from a C file generated by language preprocessors, do
7001 not set the language if it was already deduced by start_subfile. */
7002 if (!(cu->language == language_c && symtab->language != language_c))
7003 symtab->language = cu->language;
7004
7005 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7006 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7007 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7008 there were bugs in prologue debug info, fixed later in GCC-4.5
7009 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7010
7011 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7012 needed, it would be wrong due to missing DW_AT_producer there.
7013
7014 Still one can confuse GDB by using non-standard GCC compilation
7015 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7016 */
ab260dad 7017 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7018 symtab->locations_valid = 1;
e0d00bc7
JK
7019
7020 if (gcc_4_minor >= 5)
7021 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7022
7023 symtab->call_site_htab = cu->call_site_htab;
c906108c 7024 }
9291a0cd
TT
7025
7026 if (dwarf2_per_objfile->using_index)
7027 per_cu->v.quick->symtab = symtab;
7028 else
7029 {
7030 struct partial_symtab *pst = per_cu->v.psymtab;
7031 pst->symtab = symtab;
7032 pst->readin = 1;
7033 }
c906108c 7034
95554aad
TT
7035 /* Push it for inclusion processing later. */
7036 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7037
c906108c 7038 do_cleanups (back_to);
f4dc4d17 7039}
45cfd468 7040
f4dc4d17
DE
7041/* Generate full symbol information for type unit PER_CU, whose DIEs have
7042 already been loaded into memory. */
7043
7044static void
7045process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7046 enum language pretend_language)
7047{
7048 struct dwarf2_cu *cu = per_cu->cu;
7049 struct objfile *objfile = per_cu->objfile;
7050 struct symtab *symtab;
7051 struct cleanup *back_to, *delayed_list_cleanup;
7052
7053 buildsym_init ();
7054 back_to = make_cleanup (really_free_pendings, NULL);
7055 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7056
7057 cu->list_in_scope = &file_symbols;
7058
7059 cu->language = pretend_language;
7060 cu->language_defn = language_def (cu->language);
7061
7062 /* The symbol tables are set up in read_type_unit_scope. */
7063 process_die (cu->dies, cu);
7064
7065 /* For now fudge the Go package. */
7066 if (cu->language == language_go)
7067 fixup_go_packaging (cu);
7068
7069 /* Now that we have processed all the DIEs in the CU, all the types
7070 should be complete, and it should now be safe to compute all of the
7071 physnames. */
7072 compute_delayed_physnames (cu);
7073 do_cleanups (delayed_list_cleanup);
7074
7075 /* TUs share symbol tables.
7076 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7077 of it with end_expandable_symtab. Otherwise, complete the addition of
7078 this TU's symbols to the existing symtab. */
f4dc4d17 7079 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7080 {
f4dc4d17
DE
7081 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7082 per_cu->s.type_unit_group->primary_symtab = symtab;
7083
7084 if (symtab != NULL)
7085 {
7086 /* Set symtab language to language from DW_AT_language. If the
7087 compilation is from a C file generated by language preprocessors,
7088 do not set the language if it was already deduced by
7089 start_subfile. */
7090 if (!(cu->language == language_c && symtab->language != language_c))
7091 symtab->language = cu->language;
7092 }
7093 }
7094 else
7095 {
7096 augment_type_symtab (objfile,
7097 per_cu->s.type_unit_group->primary_symtab);
7098 symtab = per_cu->s.type_unit_group->primary_symtab;
7099 }
7100
7101 if (dwarf2_per_objfile->using_index)
7102 per_cu->v.quick->symtab = symtab;
7103 else
7104 {
7105 struct partial_symtab *pst = per_cu->v.psymtab;
7106 pst->symtab = symtab;
7107 pst->readin = 1;
45cfd468 7108 }
f4dc4d17
DE
7109
7110 do_cleanups (back_to);
c906108c
SS
7111}
7112
95554aad
TT
7113/* Process an imported unit DIE. */
7114
7115static void
7116process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7117{
7118 struct attribute *attr;
7119
f4dc4d17
DE
7120 /* For now we don't handle imported units in type units. */
7121 if (cu->per_cu->is_debug_types)
7122 {
7123 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7124 " supported in type units [in module %s]"),
7125 cu->objfile->name);
7126 }
7127
95554aad
TT
7128 attr = dwarf2_attr (die, DW_AT_import, cu);
7129 if (attr != NULL)
7130 {
7131 struct dwarf2_per_cu_data *per_cu;
7132 struct symtab *imported_symtab;
7133 sect_offset offset;
36586728 7134 int is_dwz;
95554aad
TT
7135
7136 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7137 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7138 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7139
7140 /* Queue the unit, if needed. */
7141 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7142 load_full_comp_unit (per_cu, cu->language);
7143
f4dc4d17 7144 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7145 per_cu);
7146 }
7147}
7148
c906108c
SS
7149/* Process a die and its children. */
7150
7151static void
e7c27a73 7152process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7153{
7154 switch (die->tag)
7155 {
7156 case DW_TAG_padding:
7157 break;
7158 case DW_TAG_compile_unit:
95554aad 7159 case DW_TAG_partial_unit:
e7c27a73 7160 read_file_scope (die, cu);
c906108c 7161 break;
348e048f
DE
7162 case DW_TAG_type_unit:
7163 read_type_unit_scope (die, cu);
7164 break;
c906108c 7165 case DW_TAG_subprogram:
c906108c 7166 case DW_TAG_inlined_subroutine:
edb3359d 7167 read_func_scope (die, cu);
c906108c
SS
7168 break;
7169 case DW_TAG_lexical_block:
14898363
L
7170 case DW_TAG_try_block:
7171 case DW_TAG_catch_block:
e7c27a73 7172 read_lexical_block_scope (die, cu);
c906108c 7173 break;
96408a79
SA
7174 case DW_TAG_GNU_call_site:
7175 read_call_site_scope (die, cu);
7176 break;
c906108c 7177 case DW_TAG_class_type:
680b30c7 7178 case DW_TAG_interface_type:
c906108c
SS
7179 case DW_TAG_structure_type:
7180 case DW_TAG_union_type:
134d01f1 7181 process_structure_scope (die, cu);
c906108c
SS
7182 break;
7183 case DW_TAG_enumeration_type:
134d01f1 7184 process_enumeration_scope (die, cu);
c906108c 7185 break;
134d01f1 7186
f792889a
DJ
7187 /* These dies have a type, but processing them does not create
7188 a symbol or recurse to process the children. Therefore we can
7189 read them on-demand through read_type_die. */
c906108c 7190 case DW_TAG_subroutine_type:
72019c9c 7191 case DW_TAG_set_type:
c906108c 7192 case DW_TAG_array_type:
c906108c 7193 case DW_TAG_pointer_type:
c906108c 7194 case DW_TAG_ptr_to_member_type:
c906108c 7195 case DW_TAG_reference_type:
c906108c 7196 case DW_TAG_string_type:
c906108c 7197 break;
134d01f1 7198
c906108c 7199 case DW_TAG_base_type:
a02abb62 7200 case DW_TAG_subrange_type:
cb249c71 7201 case DW_TAG_typedef:
134d01f1
DJ
7202 /* Add a typedef symbol for the type definition, if it has a
7203 DW_AT_name. */
f792889a 7204 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7205 break;
c906108c 7206 case DW_TAG_common_block:
e7c27a73 7207 read_common_block (die, cu);
c906108c
SS
7208 break;
7209 case DW_TAG_common_inclusion:
7210 break;
d9fa45fe 7211 case DW_TAG_namespace:
63d06c5c 7212 processing_has_namespace_info = 1;
e7c27a73 7213 read_namespace (die, cu);
d9fa45fe 7214 break;
5d7cb8df 7215 case DW_TAG_module:
f55ee35c 7216 processing_has_namespace_info = 1;
5d7cb8df
JK
7217 read_module (die, cu);
7218 break;
d9fa45fe
DC
7219 case DW_TAG_imported_declaration:
7220 case DW_TAG_imported_module:
63d06c5c 7221 processing_has_namespace_info = 1;
27aa8d6a
SW
7222 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7223 || cu->language != language_fortran))
7224 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7225 dwarf_tag_name (die->tag));
7226 read_import_statement (die, cu);
d9fa45fe 7227 break;
95554aad
TT
7228
7229 case DW_TAG_imported_unit:
7230 process_imported_unit_die (die, cu);
7231 break;
7232
c906108c 7233 default:
e7c27a73 7234 new_symbol (die, NULL, cu);
c906108c
SS
7235 break;
7236 }
7237}
7238
94af9270
KS
7239/* A helper function for dwarf2_compute_name which determines whether DIE
7240 needs to have the name of the scope prepended to the name listed in the
7241 die. */
7242
7243static int
7244die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7245{
1c809c68
TT
7246 struct attribute *attr;
7247
94af9270
KS
7248 switch (die->tag)
7249 {
7250 case DW_TAG_namespace:
7251 case DW_TAG_typedef:
7252 case DW_TAG_class_type:
7253 case DW_TAG_interface_type:
7254 case DW_TAG_structure_type:
7255 case DW_TAG_union_type:
7256 case DW_TAG_enumeration_type:
7257 case DW_TAG_enumerator:
7258 case DW_TAG_subprogram:
7259 case DW_TAG_member:
7260 return 1;
7261
7262 case DW_TAG_variable:
c2b0a229 7263 case DW_TAG_constant:
94af9270
KS
7264 /* We only need to prefix "globally" visible variables. These include
7265 any variable marked with DW_AT_external or any variable that
7266 lives in a namespace. [Variables in anonymous namespaces
7267 require prefixing, but they are not DW_AT_external.] */
7268
7269 if (dwarf2_attr (die, DW_AT_specification, cu))
7270 {
7271 struct dwarf2_cu *spec_cu = cu;
9a619af0 7272
94af9270
KS
7273 return die_needs_namespace (die_specification (die, &spec_cu),
7274 spec_cu);
7275 }
7276
1c809c68 7277 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7278 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7279 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7280 return 0;
7281 /* A variable in a lexical block of some kind does not need a
7282 namespace, even though in C++ such variables may be external
7283 and have a mangled name. */
7284 if (die->parent->tag == DW_TAG_lexical_block
7285 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7286 || die->parent->tag == DW_TAG_catch_block
7287 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7288 return 0;
7289 return 1;
94af9270
KS
7290
7291 default:
7292 return 0;
7293 }
7294}
7295
98bfdba5
PA
7296/* Retrieve the last character from a mem_file. */
7297
7298static void
7299do_ui_file_peek_last (void *object, const char *buffer, long length)
7300{
7301 char *last_char_p = (char *) object;
7302
7303 if (length > 0)
7304 *last_char_p = buffer[length - 1];
7305}
7306
94af9270 7307/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7308 compute the physname for the object, which include a method's:
7309 - formal parameters (C++/Java),
7310 - receiver type (Go),
7311 - return type (Java).
7312
7313 The term "physname" is a bit confusing.
7314 For C++, for example, it is the demangled name.
7315 For Go, for example, it's the mangled name.
94af9270 7316
af6b7be1
JB
7317 For Ada, return the DIE's linkage name rather than the fully qualified
7318 name. PHYSNAME is ignored..
7319
94af9270
KS
7320 The result is allocated on the objfile_obstack and canonicalized. */
7321
7322static const char *
15d034d0
TT
7323dwarf2_compute_name (const char *name,
7324 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7325 int physname)
7326{
bb5ed363
DE
7327 struct objfile *objfile = cu->objfile;
7328
94af9270
KS
7329 if (name == NULL)
7330 name = dwarf2_name (die, cu);
7331
f55ee35c
JK
7332 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7333 compute it by typename_concat inside GDB. */
7334 if (cu->language == language_ada
7335 || (cu->language == language_fortran && physname))
7336 {
7337 /* For Ada unit, we prefer the linkage name over the name, as
7338 the former contains the exported name, which the user expects
7339 to be able to reference. Ideally, we want the user to be able
7340 to reference this entity using either natural or linkage name,
7341 but we haven't started looking at this enhancement yet. */
7342 struct attribute *attr;
7343
7344 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7345 if (attr == NULL)
7346 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7347 if (attr && DW_STRING (attr))
7348 return DW_STRING (attr);
7349 }
7350
94af9270
KS
7351 /* These are the only languages we know how to qualify names in. */
7352 if (name != NULL
f55ee35c
JK
7353 && (cu->language == language_cplus || cu->language == language_java
7354 || cu->language == language_fortran))
94af9270
KS
7355 {
7356 if (die_needs_namespace (die, cu))
7357 {
7358 long length;
0d5cff50 7359 const char *prefix;
94af9270
KS
7360 struct ui_file *buf;
7361
7362 prefix = determine_prefix (die, cu);
7363 buf = mem_fileopen ();
7364 if (*prefix != '\0')
7365 {
f55ee35c
JK
7366 char *prefixed_name = typename_concat (NULL, prefix, name,
7367 physname, cu);
9a619af0 7368
94af9270
KS
7369 fputs_unfiltered (prefixed_name, buf);
7370 xfree (prefixed_name);
7371 }
7372 else
62d5b8da 7373 fputs_unfiltered (name, buf);
94af9270 7374
98bfdba5
PA
7375 /* Template parameters may be specified in the DIE's DW_AT_name, or
7376 as children with DW_TAG_template_type_param or
7377 DW_TAG_value_type_param. If the latter, add them to the name
7378 here. If the name already has template parameters, then
7379 skip this step; some versions of GCC emit both, and
7380 it is more efficient to use the pre-computed name.
7381
7382 Something to keep in mind about this process: it is very
7383 unlikely, or in some cases downright impossible, to produce
7384 something that will match the mangled name of a function.
7385 If the definition of the function has the same debug info,
7386 we should be able to match up with it anyway. But fallbacks
7387 using the minimal symbol, for instance to find a method
7388 implemented in a stripped copy of libstdc++, will not work.
7389 If we do not have debug info for the definition, we will have to
7390 match them up some other way.
7391
7392 When we do name matching there is a related problem with function
7393 templates; two instantiated function templates are allowed to
7394 differ only by their return types, which we do not add here. */
7395
7396 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7397 {
7398 struct attribute *attr;
7399 struct die_info *child;
7400 int first = 1;
7401
7402 die->building_fullname = 1;
7403
7404 for (child = die->child; child != NULL; child = child->sibling)
7405 {
7406 struct type *type;
12df843f 7407 LONGEST value;
98bfdba5
PA
7408 gdb_byte *bytes;
7409 struct dwarf2_locexpr_baton *baton;
7410 struct value *v;
7411
7412 if (child->tag != DW_TAG_template_type_param
7413 && child->tag != DW_TAG_template_value_param)
7414 continue;
7415
7416 if (first)
7417 {
7418 fputs_unfiltered ("<", buf);
7419 first = 0;
7420 }
7421 else
7422 fputs_unfiltered (", ", buf);
7423
7424 attr = dwarf2_attr (child, DW_AT_type, cu);
7425 if (attr == NULL)
7426 {
7427 complaint (&symfile_complaints,
7428 _("template parameter missing DW_AT_type"));
7429 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7430 continue;
7431 }
7432 type = die_type (child, cu);
7433
7434 if (child->tag == DW_TAG_template_type_param)
7435 {
79d43c61 7436 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7437 continue;
7438 }
7439
7440 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7441 if (attr == NULL)
7442 {
7443 complaint (&symfile_complaints,
3e43a32a
MS
7444 _("template parameter missing "
7445 "DW_AT_const_value"));
98bfdba5
PA
7446 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7447 continue;
7448 }
7449
7450 dwarf2_const_value_attr (attr, type, name,
7451 &cu->comp_unit_obstack, cu,
7452 &value, &bytes, &baton);
7453
7454 if (TYPE_NOSIGN (type))
7455 /* GDB prints characters as NUMBER 'CHAR'. If that's
7456 changed, this can use value_print instead. */
7457 c_printchar (value, type, buf);
7458 else
7459 {
7460 struct value_print_options opts;
7461
7462 if (baton != NULL)
7463 v = dwarf2_evaluate_loc_desc (type, NULL,
7464 baton->data,
7465 baton->size,
7466 baton->per_cu);
7467 else if (bytes != NULL)
7468 {
7469 v = allocate_value (type);
7470 memcpy (value_contents_writeable (v), bytes,
7471 TYPE_LENGTH (type));
7472 }
7473 else
7474 v = value_from_longest (type, value);
7475
3e43a32a
MS
7476 /* Specify decimal so that we do not depend on
7477 the radix. */
98bfdba5
PA
7478 get_formatted_print_options (&opts, 'd');
7479 opts.raw = 1;
7480 value_print (v, buf, &opts);
7481 release_value (v);
7482 value_free (v);
7483 }
7484 }
7485
7486 die->building_fullname = 0;
7487
7488 if (!first)
7489 {
7490 /* Close the argument list, with a space if necessary
7491 (nested templates). */
7492 char last_char = '\0';
7493 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7494 if (last_char == '>')
7495 fputs_unfiltered (" >", buf);
7496 else
7497 fputs_unfiltered (">", buf);
7498 }
7499 }
7500
94af9270
KS
7501 /* For Java and C++ methods, append formal parameter type
7502 information, if PHYSNAME. */
6e70227d 7503
94af9270
KS
7504 if (physname && die->tag == DW_TAG_subprogram
7505 && (cu->language == language_cplus
7506 || cu->language == language_java))
7507 {
7508 struct type *type = read_type_die (die, cu);
7509
79d43c61
TT
7510 c_type_print_args (type, buf, 1, cu->language,
7511 &type_print_raw_options);
94af9270
KS
7512
7513 if (cu->language == language_java)
7514 {
7515 /* For java, we must append the return type to method
0963b4bd 7516 names. */
94af9270
KS
7517 if (die->tag == DW_TAG_subprogram)
7518 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7519 0, 0, &type_print_raw_options);
94af9270
KS
7520 }
7521 else if (cu->language == language_cplus)
7522 {
60430eff
DJ
7523 /* Assume that an artificial first parameter is
7524 "this", but do not crash if it is not. RealView
7525 marks unnamed (and thus unused) parameters as
7526 artificial; there is no way to differentiate
7527 the two cases. */
94af9270
KS
7528 if (TYPE_NFIELDS (type) > 0
7529 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7530 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7531 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7532 0))))
94af9270
KS
7533 fputs_unfiltered (" const", buf);
7534 }
7535 }
7536
bb5ed363 7537 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7538 &length);
7539 ui_file_delete (buf);
7540
7541 if (cu->language == language_cplus)
7542 {
15d034d0 7543 const char *cname
94af9270 7544 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7545 &objfile->objfile_obstack);
9a619af0 7546
94af9270
KS
7547 if (cname != NULL)
7548 name = cname;
7549 }
7550 }
7551 }
7552
7553 return name;
7554}
7555
0114d602
DJ
7556/* Return the fully qualified name of DIE, based on its DW_AT_name.
7557 If scope qualifiers are appropriate they will be added. The result
7558 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7559 not have a name. NAME may either be from a previous call to
7560 dwarf2_name or NULL.
7561
0963b4bd 7562 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7563
7564static const char *
15d034d0 7565dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7566{
94af9270
KS
7567 return dwarf2_compute_name (name, die, cu, 0);
7568}
0114d602 7569
94af9270
KS
7570/* Construct a physname for the given DIE in CU. NAME may either be
7571 from a previous call to dwarf2_name or NULL. The result will be
7572 allocated on the objfile_objstack or NULL if the DIE does not have a
7573 name.
0114d602 7574
94af9270 7575 The output string will be canonicalized (if C++/Java). */
0114d602 7576
94af9270 7577static const char *
15d034d0 7578dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7579{
bb5ed363 7580 struct objfile *objfile = cu->objfile;
900e11f9
JK
7581 struct attribute *attr;
7582 const char *retval, *mangled = NULL, *canon = NULL;
7583 struct cleanup *back_to;
7584 int need_copy = 1;
7585
7586 /* In this case dwarf2_compute_name is just a shortcut not building anything
7587 on its own. */
7588 if (!die_needs_namespace (die, cu))
7589 return dwarf2_compute_name (name, die, cu, 1);
7590
7591 back_to = make_cleanup (null_cleanup, NULL);
7592
7593 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7594 if (!attr)
7595 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7596
7597 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7598 has computed. */
7599 if (attr && DW_STRING (attr))
7600 {
7601 char *demangled;
7602
7603 mangled = DW_STRING (attr);
7604
7605 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7606 type. It is easier for GDB users to search for such functions as
7607 `name(params)' than `long name(params)'. In such case the minimal
7608 symbol names do not match the full symbol names but for template
7609 functions there is never a need to look up their definition from their
7610 declaration so the only disadvantage remains the minimal symbol
7611 variant `long name(params)' does not have the proper inferior type.
7612 */
7613
a766d390
DE
7614 if (cu->language == language_go)
7615 {
7616 /* This is a lie, but we already lie to the caller new_symbol_full.
7617 new_symbol_full assumes we return the mangled name.
7618 This just undoes that lie until things are cleaned up. */
7619 demangled = NULL;
7620 }
7621 else
7622 {
7623 demangled = cplus_demangle (mangled,
7624 (DMGL_PARAMS | DMGL_ANSI
7625 | (cu->language == language_java
7626 ? DMGL_JAVA | DMGL_RET_POSTFIX
7627 : DMGL_RET_DROP)));
7628 }
900e11f9
JK
7629 if (demangled)
7630 {
7631 make_cleanup (xfree, demangled);
7632 canon = demangled;
7633 }
7634 else
7635 {
7636 canon = mangled;
7637 need_copy = 0;
7638 }
7639 }
7640
7641 if (canon == NULL || check_physname)
7642 {
7643 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7644
7645 if (canon != NULL && strcmp (physname, canon) != 0)
7646 {
7647 /* It may not mean a bug in GDB. The compiler could also
7648 compute DW_AT_linkage_name incorrectly. But in such case
7649 GDB would need to be bug-to-bug compatible. */
7650
7651 complaint (&symfile_complaints,
7652 _("Computed physname <%s> does not match demangled <%s> "
7653 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7654 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7655
7656 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7657 is available here - over computed PHYSNAME. It is safer
7658 against both buggy GDB and buggy compilers. */
7659
7660 retval = canon;
7661 }
7662 else
7663 {
7664 retval = physname;
7665 need_copy = 0;
7666 }
7667 }
7668 else
7669 retval = canon;
7670
7671 if (need_copy)
7672 retval = obsavestring (retval, strlen (retval),
bb5ed363 7673 &objfile->objfile_obstack);
900e11f9
JK
7674
7675 do_cleanups (back_to);
7676 return retval;
0114d602
DJ
7677}
7678
27aa8d6a
SW
7679/* Read the import statement specified by the given die and record it. */
7680
7681static void
7682read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7683{
bb5ed363 7684 struct objfile *objfile = cu->objfile;
27aa8d6a 7685 struct attribute *import_attr;
32019081 7686 struct die_info *imported_die, *child_die;
de4affc9 7687 struct dwarf2_cu *imported_cu;
27aa8d6a 7688 const char *imported_name;
794684b6 7689 const char *imported_name_prefix;
13387711
SW
7690 const char *canonical_name;
7691 const char *import_alias;
7692 const char *imported_declaration = NULL;
794684b6 7693 const char *import_prefix;
32019081
JK
7694 VEC (const_char_ptr) *excludes = NULL;
7695 struct cleanup *cleanups;
13387711
SW
7696
7697 char *temp;
27aa8d6a
SW
7698
7699 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7700 if (import_attr == NULL)
7701 {
7702 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7703 dwarf_tag_name (die->tag));
7704 return;
7705 }
7706
de4affc9
CC
7707 imported_cu = cu;
7708 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7709 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7710 if (imported_name == NULL)
7711 {
7712 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7713
7714 The import in the following code:
7715 namespace A
7716 {
7717 typedef int B;
7718 }
7719
7720 int main ()
7721 {
7722 using A::B;
7723 B b;
7724 return b;
7725 }
7726
7727 ...
7728 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7729 <52> DW_AT_decl_file : 1
7730 <53> DW_AT_decl_line : 6
7731 <54> DW_AT_import : <0x75>
7732 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7733 <59> DW_AT_name : B
7734 <5b> DW_AT_decl_file : 1
7735 <5c> DW_AT_decl_line : 2
7736 <5d> DW_AT_type : <0x6e>
7737 ...
7738 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7739 <76> DW_AT_byte_size : 4
7740 <77> DW_AT_encoding : 5 (signed)
7741
7742 imports the wrong die ( 0x75 instead of 0x58 ).
7743 This case will be ignored until the gcc bug is fixed. */
7744 return;
7745 }
7746
82856980
SW
7747 /* Figure out the local name after import. */
7748 import_alias = dwarf2_name (die, cu);
27aa8d6a 7749
794684b6
SW
7750 /* Figure out where the statement is being imported to. */
7751 import_prefix = determine_prefix (die, cu);
7752
7753 /* Figure out what the scope of the imported die is and prepend it
7754 to the name of the imported die. */
de4affc9 7755 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7756
f55ee35c
JK
7757 if (imported_die->tag != DW_TAG_namespace
7758 && imported_die->tag != DW_TAG_module)
794684b6 7759 {
13387711
SW
7760 imported_declaration = imported_name;
7761 canonical_name = imported_name_prefix;
794684b6 7762 }
13387711 7763 else if (strlen (imported_name_prefix) > 0)
794684b6 7764 {
13387711
SW
7765 temp = alloca (strlen (imported_name_prefix)
7766 + 2 + strlen (imported_name) + 1);
7767 strcpy (temp, imported_name_prefix);
7768 strcat (temp, "::");
7769 strcat (temp, imported_name);
7770 canonical_name = temp;
794684b6 7771 }
13387711
SW
7772 else
7773 canonical_name = imported_name;
794684b6 7774
32019081
JK
7775 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7776
7777 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7778 for (child_die = die->child; child_die && child_die->tag;
7779 child_die = sibling_die (child_die))
7780 {
7781 /* DWARF-4: A Fortran use statement with a “rename list” may be
7782 represented by an imported module entry with an import attribute
7783 referring to the module and owned entries corresponding to those
7784 entities that are renamed as part of being imported. */
7785
7786 if (child_die->tag != DW_TAG_imported_declaration)
7787 {
7788 complaint (&symfile_complaints,
7789 _("child DW_TAG_imported_declaration expected "
7790 "- DIE at 0x%x [in module %s]"),
b64f50a1 7791 child_die->offset.sect_off, objfile->name);
32019081
JK
7792 continue;
7793 }
7794
7795 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7796 if (import_attr == NULL)
7797 {
7798 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7799 dwarf_tag_name (child_die->tag));
7800 continue;
7801 }
7802
7803 imported_cu = cu;
7804 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7805 &imported_cu);
7806 imported_name = dwarf2_name (imported_die, imported_cu);
7807 if (imported_name == NULL)
7808 {
7809 complaint (&symfile_complaints,
7810 _("child DW_TAG_imported_declaration has unknown "
7811 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7812 child_die->offset.sect_off, objfile->name);
32019081
JK
7813 continue;
7814 }
7815
7816 VEC_safe_push (const_char_ptr, excludes, imported_name);
7817
7818 process_die (child_die, cu);
7819 }
7820
c0cc3a76
SW
7821 cp_add_using_directive (import_prefix,
7822 canonical_name,
7823 import_alias,
13387711 7824 imported_declaration,
32019081 7825 excludes,
bb5ed363 7826 &objfile->objfile_obstack);
32019081
JK
7827
7828 do_cleanups (cleanups);
27aa8d6a
SW
7829}
7830
f4dc4d17 7831/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7832
cb1df416
DJ
7833static void
7834free_cu_line_header (void *arg)
7835{
7836 struct dwarf2_cu *cu = arg;
7837
7838 free_line_header (cu->line_header);
7839 cu->line_header = NULL;
7840}
7841
1b80a9fa
JK
7842/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7843 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7844 this, it was first present in GCC release 4.3.0. */
7845
7846static int
7847producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7848{
7849 if (!cu->checked_producer)
7850 check_producer (cu);
7851
7852 return cu->producer_is_gcc_lt_4_3;
7853}
7854
9291a0cd
TT
7855static void
7856find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7857 const char **name, const char **comp_dir)
9291a0cd
TT
7858{
7859 struct attribute *attr;
7860
7861 *name = NULL;
7862 *comp_dir = NULL;
7863
7864 /* Find the filename. Do not use dwarf2_name here, since the filename
7865 is not a source language identifier. */
7866 attr = dwarf2_attr (die, DW_AT_name, cu);
7867 if (attr)
7868 {
7869 *name = DW_STRING (attr);
7870 }
7871
7872 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7873 if (attr)
7874 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7875 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7876 && IS_ABSOLUTE_PATH (*name))
9291a0cd 7877 {
15d034d0
TT
7878 char *d = ldirname (*name);
7879
7880 *comp_dir = d;
7881 if (d != NULL)
7882 make_cleanup (xfree, d);
9291a0cd
TT
7883 }
7884 if (*comp_dir != NULL)
7885 {
7886 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7887 directory, get rid of it. */
7888 char *cp = strchr (*comp_dir, ':');
7889
7890 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7891 *comp_dir = cp + 1;
7892 }
7893
7894 if (*name == NULL)
7895 *name = "<unknown>";
7896}
7897
f4dc4d17
DE
7898/* Handle DW_AT_stmt_list for a compilation unit.
7899 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7900 COMP_DIR is the compilation directory.
7901 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7902
7903static void
7904handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7905 const char *comp_dir)
2ab95328
TT
7906{
7907 struct attribute *attr;
2ab95328 7908
f4dc4d17
DE
7909 gdb_assert (! cu->per_cu->is_debug_types);
7910
2ab95328
TT
7911 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7912 if (attr)
7913 {
7914 unsigned int line_offset = DW_UNSND (attr);
7915 struct line_header *line_header
3019eac3 7916 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7917
7918 if (line_header)
dee91e82
DE
7919 {
7920 cu->line_header = line_header;
7921 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7922 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7923 }
2ab95328
TT
7924 }
7925}
7926
95554aad 7927/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7928
c906108c 7929static void
e7c27a73 7930read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7931{
dee91e82 7932 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7933 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7934 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7935 CORE_ADDR highpc = ((CORE_ADDR) 0);
7936 struct attribute *attr;
15d034d0
TT
7937 const char *name = NULL;
7938 const char *comp_dir = NULL;
c906108c
SS
7939 struct die_info *child_die;
7940 bfd *abfd = objfile->obfd;
e142c38c 7941 CORE_ADDR baseaddr;
6e70227d 7942
e142c38c 7943 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7944
fae299cd 7945 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7946
7947 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7948 from finish_block. */
2acceee2 7949 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7950 lowpc = highpc;
7951 lowpc += baseaddr;
7952 highpc += baseaddr;
7953
9291a0cd 7954 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7955
95554aad 7956 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7957
f4b8a18d
KW
7958 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7959 standardised yet. As a workaround for the language detection we fall
7960 back to the DW_AT_producer string. */
7961 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7962 cu->language = language_opencl;
7963
3019eac3
DE
7964 /* Similar hack for Go. */
7965 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7966 set_cu_language (DW_LANG_Go, cu);
7967
f4dc4d17 7968 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7969
7970 /* Decode line number information if present. We do this before
7971 processing child DIEs, so that the line header table is available
7972 for DW_AT_decl_file. */
f4dc4d17 7973 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7974
7975 /* Process all dies in compilation unit. */
7976 if (die->child != NULL)
7977 {
7978 child_die = die->child;
7979 while (child_die && child_die->tag)
7980 {
7981 process_die (child_die, cu);
7982 child_die = sibling_die (child_die);
7983 }
7984 }
7985
7986 /* Decode macro information, if present. Dwarf 2 macro information
7987 refers to information in the line number info statement program
7988 header, so we can only read it if we've read the header
7989 successfully. */
7990 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7991 if (attr && cu->line_header)
7992 {
7993 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7994 complaint (&symfile_complaints,
7995 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7996
09262596 7997 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7998 }
7999 else
8000 {
8001 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8002 if (attr && cu->line_header)
8003 {
8004 unsigned int macro_offset = DW_UNSND (attr);
8005
09262596 8006 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8007 }
8008 }
8009
8010 do_cleanups (back_to);
8011}
8012
f4dc4d17
DE
8013/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8014 Create the set of symtabs used by this TU, or if this TU is sharing
8015 symtabs with another TU and the symtabs have already been created
8016 then restore those symtabs in the line header.
8017 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8018
8019static void
f4dc4d17 8020setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8021{
f4dc4d17
DE
8022 struct objfile *objfile = dwarf2_per_objfile->objfile;
8023 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8024 struct type_unit_group *tu_group;
8025 int first_time;
8026 struct line_header *lh;
3019eac3 8027 struct attribute *attr;
f4dc4d17 8028 unsigned int i, line_offset;
3019eac3 8029
f4dc4d17 8030 gdb_assert (per_cu->is_debug_types);
3019eac3 8031
f4dc4d17 8032 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8033
f4dc4d17
DE
8034 /* If we're using .gdb_index (includes -readnow) then
8035 per_cu->s.type_unit_group may not have been set up yet. */
8036 if (per_cu->s.type_unit_group == NULL)
094b34ac 8037 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
8038 tu_group = per_cu->s.type_unit_group;
8039
8040 /* If we've already processed this stmt_list there's no real need to
8041 do it again, we could fake it and just recreate the part we need
8042 (file name,index -> symtab mapping). If data shows this optimization
8043 is useful we can do it then. */
8044 first_time = tu_group->primary_symtab == NULL;
8045
8046 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8047 debug info. */
8048 lh = NULL;
8049 if (attr != NULL)
3019eac3 8050 {
f4dc4d17
DE
8051 line_offset = DW_UNSND (attr);
8052 lh = dwarf_decode_line_header (line_offset, cu);
8053 }
8054 if (lh == NULL)
8055 {
8056 if (first_time)
8057 dwarf2_start_symtab (cu, "", NULL, 0);
8058 else
8059 {
8060 gdb_assert (tu_group->symtabs == NULL);
8061 restart_symtab (0);
8062 }
8063 /* Note: The primary symtab will get allocated at the end. */
8064 return;
3019eac3
DE
8065 }
8066
f4dc4d17
DE
8067 cu->line_header = lh;
8068 make_cleanup (free_cu_line_header, cu);
3019eac3 8069
f4dc4d17
DE
8070 if (first_time)
8071 {
8072 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8073
f4dc4d17
DE
8074 tu_group->num_symtabs = lh->num_file_names;
8075 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8076
f4dc4d17
DE
8077 for (i = 0; i < lh->num_file_names; ++i)
8078 {
8079 char *dir = NULL;
8080 struct file_entry *fe = &lh->file_names[i];
3019eac3 8081
f4dc4d17
DE
8082 if (fe->dir_index)
8083 dir = lh->include_dirs[fe->dir_index - 1];
8084 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8085
f4dc4d17
DE
8086 /* Note: We don't have to watch for the main subfile here, type units
8087 don't have DW_AT_name. */
3019eac3 8088
f4dc4d17
DE
8089 if (current_subfile->symtab == NULL)
8090 {
8091 /* NOTE: start_subfile will recognize when it's been passed
8092 a file it has already seen. So we can't assume there's a
8093 simple mapping from lh->file_names to subfiles,
8094 lh->file_names may contain dups. */
8095 current_subfile->symtab = allocate_symtab (current_subfile->name,
8096 objfile);
8097 }
8098
8099 fe->symtab = current_subfile->symtab;
8100 tu_group->symtabs[i] = fe->symtab;
8101 }
8102 }
8103 else
3019eac3 8104 {
f4dc4d17
DE
8105 restart_symtab (0);
8106
8107 for (i = 0; i < lh->num_file_names; ++i)
8108 {
8109 struct file_entry *fe = &lh->file_names[i];
8110
8111 fe->symtab = tu_group->symtabs[i];
8112 }
3019eac3
DE
8113 }
8114
f4dc4d17
DE
8115 /* The main symtab is allocated last. Type units don't have DW_AT_name
8116 so they don't have a "real" (so to speak) symtab anyway.
8117 There is later code that will assign the main symtab to all symbols
8118 that don't have one. We need to handle the case of a symbol with a
8119 missing symtab (DW_AT_decl_file) anyway. */
8120}
3019eac3 8121
f4dc4d17
DE
8122/* Process DW_TAG_type_unit.
8123 For TUs we want to skip the first top level sibling if it's not the
8124 actual type being defined by this TU. In this case the first top
8125 level sibling is there to provide context only. */
3019eac3 8126
f4dc4d17
DE
8127static void
8128read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8129{
8130 struct die_info *child_die;
3019eac3 8131
f4dc4d17
DE
8132 prepare_one_comp_unit (cu, die, language_minimal);
8133
8134 /* Initialize (or reinitialize) the machinery for building symtabs.
8135 We do this before processing child DIEs, so that the line header table
8136 is available for DW_AT_decl_file. */
8137 setup_type_unit_groups (die, cu);
8138
8139 if (die->child != NULL)
8140 {
8141 child_die = die->child;
8142 while (child_die && child_die->tag)
8143 {
8144 process_die (child_die, cu);
8145 child_die = sibling_die (child_die);
8146 }
8147 }
3019eac3
DE
8148}
8149\f
80626a55
DE
8150/* DWO/DWP files.
8151
8152 http://gcc.gnu.org/wiki/DebugFission
8153 http://gcc.gnu.org/wiki/DebugFissionDWP
8154
8155 To simplify handling of both DWO files ("object" files with the DWARF info)
8156 and DWP files (a file with the DWOs packaged up into one file), we treat
8157 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8158
8159static hashval_t
8160hash_dwo_file (const void *item)
8161{
8162 const struct dwo_file *dwo_file = item;
8163
80626a55 8164 return htab_hash_string (dwo_file->name);
3019eac3
DE
8165}
8166
8167static int
8168eq_dwo_file (const void *item_lhs, const void *item_rhs)
8169{
8170 const struct dwo_file *lhs = item_lhs;
8171 const struct dwo_file *rhs = item_rhs;
8172
80626a55 8173 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8174}
8175
8176/* Allocate a hash table for DWO files. */
8177
8178static htab_t
8179allocate_dwo_file_hash_table (void)
8180{
8181 struct objfile *objfile = dwarf2_per_objfile->objfile;
8182
8183 return htab_create_alloc_ex (41,
8184 hash_dwo_file,
8185 eq_dwo_file,
8186 NULL,
8187 &objfile->objfile_obstack,
8188 hashtab_obstack_allocate,
8189 dummy_obstack_deallocate);
8190}
8191
80626a55
DE
8192/* Lookup DWO file DWO_NAME. */
8193
8194static void **
8195lookup_dwo_file_slot (const char *dwo_name)
8196{
8197 struct dwo_file find_entry;
8198 void **slot;
8199
8200 if (dwarf2_per_objfile->dwo_files == NULL)
8201 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8202
8203 memset (&find_entry, 0, sizeof (find_entry));
8204 find_entry.name = dwo_name;
8205 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8206
8207 return slot;
8208}
8209
3019eac3
DE
8210static hashval_t
8211hash_dwo_unit (const void *item)
8212{
8213 const struct dwo_unit *dwo_unit = item;
8214
8215 /* This drops the top 32 bits of the id, but is ok for a hash. */
8216 return dwo_unit->signature;
8217}
8218
8219static int
8220eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8221{
8222 const struct dwo_unit *lhs = item_lhs;
8223 const struct dwo_unit *rhs = item_rhs;
8224
8225 /* The signature is assumed to be unique within the DWO file.
8226 So while object file CU dwo_id's always have the value zero,
8227 that's OK, assuming each object file DWO file has only one CU,
8228 and that's the rule for now. */
8229 return lhs->signature == rhs->signature;
8230}
8231
8232/* Allocate a hash table for DWO CUs,TUs.
8233 There is one of these tables for each of CUs,TUs for each DWO file. */
8234
8235static htab_t
8236allocate_dwo_unit_table (struct objfile *objfile)
8237{
8238 /* Start out with a pretty small number.
8239 Generally DWO files contain only one CU and maybe some TUs. */
8240 return htab_create_alloc_ex (3,
8241 hash_dwo_unit,
8242 eq_dwo_unit,
8243 NULL,
8244 &objfile->objfile_obstack,
8245 hashtab_obstack_allocate,
8246 dummy_obstack_deallocate);
8247}
8248
80626a55 8249/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8250
8251struct create_dwo_info_table_data
8252{
8253 struct dwo_file *dwo_file;
8254 htab_t cu_htab;
8255};
8256
80626a55 8257/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8258
8259static void
80626a55
DE
8260create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8261 gdb_byte *info_ptr,
8262 struct die_info *comp_unit_die,
8263 int has_children,
8264 void *datap)
3019eac3
DE
8265{
8266 struct dwarf2_cu *cu = reader->cu;
8267 struct objfile *objfile = dwarf2_per_objfile->objfile;
8268 sect_offset offset = cu->per_cu->offset;
8269 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8270 struct create_dwo_info_table_data *data = datap;
8271 struct dwo_file *dwo_file = data->dwo_file;
8272 htab_t cu_htab = data->cu_htab;
8273 void **slot;
8274 struct attribute *attr;
8275 struct dwo_unit *dwo_unit;
8276
8277 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8278 if (attr == NULL)
8279 {
8280 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8281 " its dwo_id [in module %s]"),
80626a55 8282 offset.sect_off, dwo_file->name);
3019eac3
DE
8283 return;
8284 }
8285
8286 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8287 dwo_unit->dwo_file = dwo_file;
8288 dwo_unit->signature = DW_UNSND (attr);
8289 dwo_unit->info_or_types_section = section;
8290 dwo_unit->offset = offset;
8291 dwo_unit->length = cu->per_cu->length;
8292
8293 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8294 gdb_assert (slot != NULL);
8295 if (*slot != NULL)
8296 {
8297 const struct dwo_unit *dup_dwo_unit = *slot;
8298
8299 complaint (&symfile_complaints,
8300 _("debug entry at offset 0x%x is duplicate to the entry at"
8301 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8302 offset.sect_off, dup_dwo_unit->offset.sect_off,
8303 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8304 dwo_file->name);
3019eac3
DE
8305 }
8306 else
8307 *slot = dwo_unit;
8308
09406207 8309 if (dwarf2_read_debug)
3019eac3
DE
8310 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8311 offset.sect_off,
8312 phex (dwo_unit->signature,
8313 sizeof (dwo_unit->signature)));
8314}
8315
80626a55
DE
8316/* Create a hash table to map DWO IDs to their CU entry in
8317 .debug_info.dwo in DWO_FILE.
8318 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8319
8320static htab_t
80626a55 8321create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8322{
8323 struct objfile *objfile = dwarf2_per_objfile->objfile;
8324 struct dwarf2_section_info *section = &dwo_file->sections.info;
8325 bfd *abfd;
8326 htab_t cu_htab;
8327 gdb_byte *info_ptr, *end_ptr;
8328 struct create_dwo_info_table_data create_dwo_info_table_data;
8329
8330 dwarf2_read_section (objfile, section);
8331 info_ptr = section->buffer;
8332
8333 if (info_ptr == NULL)
8334 return NULL;
8335
8336 /* We can't set abfd until now because the section may be empty or
8337 not present, in which case section->asection will be NULL. */
8338 abfd = section->asection->owner;
8339
09406207 8340 if (dwarf2_read_debug)
3019eac3
DE
8341 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8342 bfd_get_filename (abfd));
8343
8344 cu_htab = allocate_dwo_unit_table (objfile);
8345
8346 create_dwo_info_table_data.dwo_file = dwo_file;
8347 create_dwo_info_table_data.cu_htab = cu_htab;
8348
8349 end_ptr = info_ptr + section->size;
8350 while (info_ptr < end_ptr)
8351 {
8352 struct dwarf2_per_cu_data per_cu;
8353
8354 memset (&per_cu, 0, sizeof (per_cu));
8355 per_cu.objfile = objfile;
8356 per_cu.is_debug_types = 0;
8357 per_cu.offset.sect_off = info_ptr - section->buffer;
8358 per_cu.info_or_types_section = section;
8359
8360 init_cutu_and_read_dies_no_follow (&per_cu,
8361 &dwo_file->sections.abbrev,
8362 dwo_file,
80626a55 8363 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8364 &create_dwo_info_table_data);
8365
8366 info_ptr += per_cu.length;
8367 }
8368
8369 return cu_htab;
8370}
8371
80626a55
DE
8372/* DWP file .debug_{cu,tu}_index section format:
8373 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8374
8375 Both index sections have the same format, and serve to map a 64-bit
8376 signature to a set of section numbers. Each section begins with a header,
8377 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8378 indexes, and a pool of 32-bit section numbers. The index sections will be
8379 aligned at 8-byte boundaries in the file.
8380
8381 The index section header contains two unsigned 32-bit values (using the
8382 byte order of the application binary):
8383
8384 N, the number of compilation units or type units in the index
8385 M, the number of slots in the hash table
8386
8387 (We assume that N and M will not exceed 2^32 - 1.)
8388
8389 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8390
8391 The hash table begins at offset 8 in the section, and consists of an array
8392 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8393 order of the application binary). Unused slots in the hash table are 0.
8394 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8395
8396 The parallel table begins immediately after the hash table
8397 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8398 array of 32-bit indexes (using the byte order of the application binary),
8399 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8400 table contains a 32-bit index into the pool of section numbers. For unused
8401 hash table slots, the corresponding entry in the parallel table will be 0.
8402
8403 Given a 64-bit compilation unit signature or a type signature S, an entry
8404 in the hash table is located as follows:
8405
8406 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8407 the low-order k bits all set to 1.
8408
8409 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8410
8411 3) If the hash table entry at index H matches the signature, use that
8412 entry. If the hash table entry at index H is unused (all zeroes),
8413 terminate the search: the signature is not present in the table.
8414
8415 4) Let H = (H + H') modulo M. Repeat at Step 3.
8416
8417 Because M > N and H' and M are relatively prime, the search is guaranteed
8418 to stop at an unused slot or find the match.
8419
8420 The pool of section numbers begins immediately following the hash table
8421 (at offset 8 + 12 * M from the beginning of the section). The pool of
8422 section numbers consists of an array of 32-bit words (using the byte order
8423 of the application binary). Each item in the array is indexed starting
8424 from 0. The hash table entry provides the index of the first section
8425 number in the set. Additional section numbers in the set follow, and the
8426 set is terminated by a 0 entry (section number 0 is not used in ELF).
8427
8428 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8429 section must be the first entry in the set, and the .debug_abbrev.dwo must
8430 be the second entry. Other members of the set may follow in any order. */
8431
8432/* Create a hash table to map DWO IDs to their CU/TU entry in
8433 .debug_{info,types}.dwo in DWP_FILE.
8434 Returns NULL if there isn't one.
8435 Note: This function processes DWP files only, not DWO files. */
8436
8437static struct dwp_hash_table *
8438create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8439{
8440 struct objfile *objfile = dwarf2_per_objfile->objfile;
8441 bfd *dbfd = dwp_file->dbfd;
8442 char *index_ptr, *index_end;
8443 struct dwarf2_section_info *index;
8444 uint32_t version, nr_units, nr_slots;
8445 struct dwp_hash_table *htab;
8446
8447 if (is_debug_types)
8448 index = &dwp_file->sections.tu_index;
8449 else
8450 index = &dwp_file->sections.cu_index;
8451
8452 if (dwarf2_section_empty_p (index))
8453 return NULL;
8454 dwarf2_read_section (objfile, index);
8455
8456 index_ptr = index->buffer;
8457 index_end = index_ptr + index->size;
8458
8459 version = read_4_bytes (dbfd, index_ptr);
8460 index_ptr += 8; /* Skip the unused word. */
8461 nr_units = read_4_bytes (dbfd, index_ptr);
8462 index_ptr += 4;
8463 nr_slots = read_4_bytes (dbfd, index_ptr);
8464 index_ptr += 4;
8465
8466 if (version != 1)
8467 {
8468 error (_("Dwarf Error: unsupported DWP file version (%u)"
8469 " [in module %s]"),
8470 version, dwp_file->name);
8471 }
8472 if (nr_slots != (nr_slots & -nr_slots))
8473 {
8474 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8475 " is not power of 2 [in module %s]"),
8476 nr_slots, dwp_file->name);
8477 }
8478
8479 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8480 htab->nr_units = nr_units;
8481 htab->nr_slots = nr_slots;
8482 htab->hash_table = index_ptr;
8483 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8484 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8485
8486 return htab;
8487}
8488
8489/* Update SECTIONS with the data from SECTP.
8490
8491 This function is like the other "locate" section routines that are
8492 passed to bfd_map_over_sections, but in this context the sections to
8493 read comes from the DWP hash table, not the full ELF section table.
8494
8495 The result is non-zero for success, or zero if an error was found. */
8496
8497static int
8498locate_virtual_dwo_sections (asection *sectp,
8499 struct virtual_dwo_sections *sections)
8500{
8501 const struct dwop_section_names *names = &dwop_section_names;
8502
8503 if (section_is_p (sectp->name, &names->abbrev_dwo))
8504 {
8505 /* There can be only one. */
8506 if (sections->abbrev.asection != NULL)
8507 return 0;
8508 sections->abbrev.asection = sectp;
8509 sections->abbrev.size = bfd_get_section_size (sectp);
8510 }
8511 else if (section_is_p (sectp->name, &names->info_dwo)
8512 || section_is_p (sectp->name, &names->types_dwo))
8513 {
8514 /* There can be only one. */
8515 if (sections->info_or_types.asection != NULL)
8516 return 0;
8517 sections->info_or_types.asection = sectp;
8518 sections->info_or_types.size = bfd_get_section_size (sectp);
8519 }
8520 else if (section_is_p (sectp->name, &names->line_dwo))
8521 {
8522 /* There can be only one. */
8523 if (sections->line.asection != NULL)
8524 return 0;
8525 sections->line.asection = sectp;
8526 sections->line.size = bfd_get_section_size (sectp);
8527 }
8528 else if (section_is_p (sectp->name, &names->loc_dwo))
8529 {
8530 /* There can be only one. */
8531 if (sections->loc.asection != NULL)
8532 return 0;
8533 sections->loc.asection = sectp;
8534 sections->loc.size = bfd_get_section_size (sectp);
8535 }
8536 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8537 {
8538 /* There can be only one. */
8539 if (sections->macinfo.asection != NULL)
8540 return 0;
8541 sections->macinfo.asection = sectp;
8542 sections->macinfo.size = bfd_get_section_size (sectp);
8543 }
8544 else if (section_is_p (sectp->name, &names->macro_dwo))
8545 {
8546 /* There can be only one. */
8547 if (sections->macro.asection != NULL)
8548 return 0;
8549 sections->macro.asection = sectp;
8550 sections->macro.size = bfd_get_section_size (sectp);
8551 }
8552 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8553 {
8554 /* There can be only one. */
8555 if (sections->str_offsets.asection != NULL)
8556 return 0;
8557 sections->str_offsets.asection = sectp;
8558 sections->str_offsets.size = bfd_get_section_size (sectp);
8559 }
8560 else
8561 {
8562 /* No other kind of section is valid. */
8563 return 0;
8564 }
8565
8566 return 1;
8567}
8568
8569/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8570 HTAB is the hash table from the DWP file.
8571 SECTION_INDEX is the index of the DWO in HTAB. */
8572
8573static struct dwo_unit *
8574create_dwo_in_dwp (struct dwp_file *dwp_file,
8575 const struct dwp_hash_table *htab,
8576 uint32_t section_index,
8577 ULONGEST signature, int is_debug_types)
8578{
8579 struct objfile *objfile = dwarf2_per_objfile->objfile;
8580 bfd *dbfd = dwp_file->dbfd;
8581 const char *kind = is_debug_types ? "TU" : "CU";
8582 struct dwo_file *dwo_file;
8583 struct dwo_unit *dwo_unit;
8584 struct virtual_dwo_sections sections;
8585 void **dwo_file_slot;
8586 char *virtual_dwo_name;
8587 struct dwarf2_section_info *cutu;
8588 struct cleanup *cleanups;
8589 int i;
8590
8591 if (dwarf2_read_debug)
8592 {
8593 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8594 kind,
8595 section_index, phex (signature, sizeof (signature)),
8596 dwp_file->name);
8597 }
8598
8599 /* Fetch the sections of this DWO.
8600 Put a limit on the number of sections we look for so that bad data
8601 doesn't cause us to loop forever. */
8602
8603#define MAX_NR_DWO_SECTIONS \
8604 (1 /* .debug_info or .debug_types */ \
8605 + 1 /* .debug_abbrev */ \
8606 + 1 /* .debug_line */ \
8607 + 1 /* .debug_loc */ \
8608 + 1 /* .debug_str_offsets */ \
8609 + 1 /* .debug_macro */ \
8610 + 1 /* .debug_macinfo */ \
8611 + 1 /* trailing zero */)
8612
8613 memset (&sections, 0, sizeof (sections));
8614 cleanups = make_cleanup (null_cleanup, 0);
8615
8616 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8617 {
8618 asection *sectp;
8619 uint32_t section_nr =
8620 read_4_bytes (dbfd,
8621 htab->section_pool
8622 + (section_index + i) * sizeof (uint32_t));
8623
8624 if (section_nr == 0)
8625 break;
8626 if (section_nr >= dwp_file->num_sections)
8627 {
8628 error (_("Dwarf Error: bad DWP hash table, section number too large"
8629 " [in module %s]"),
8630 dwp_file->name);
8631 }
8632
8633 sectp = dwp_file->elf_sections[section_nr];
8634 if (! locate_virtual_dwo_sections (sectp, &sections))
8635 {
8636 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8637 " [in module %s]"),
8638 dwp_file->name);
8639 }
8640 }
8641
8642 if (i < 2
8643 || sections.info_or_types.asection == NULL
8644 || sections.abbrev.asection == NULL)
8645 {
8646 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8647 " [in module %s]"),
8648 dwp_file->name);
8649 }
8650 if (i == MAX_NR_DWO_SECTIONS)
8651 {
8652 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8653 " [in module %s]"),
8654 dwp_file->name);
8655 }
8656
8657 /* It's easier for the rest of the code if we fake a struct dwo_file and
8658 have dwo_unit "live" in that. At least for now.
8659
8660 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8661 However, for each CU + set of TUs that came from the same original DWO
8662 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8663 (fewer struct dwo_file objects to allocated). Remember that for really
8664 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8665
2792b94d
PM
8666 virtual_dwo_name =
8667 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8668 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8669 sections.line.asection ? sections.line.asection->id : 0,
8670 sections.loc.asection ? sections.loc.asection->id : 0,
8671 (sections.str_offsets.asection
8672 ? sections.str_offsets.asection->id
8673 : 0));
80626a55
DE
8674 make_cleanup (xfree, virtual_dwo_name);
8675 /* Can we use an existing virtual DWO file? */
8676 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8677 /* Create one if necessary. */
8678 if (*dwo_file_slot == NULL)
8679 {
8680 if (dwarf2_read_debug)
8681 {
8682 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8683 virtual_dwo_name);
8684 }
8685 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8686 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8687 virtual_dwo_name,
8688 strlen (virtual_dwo_name));
8689 dwo_file->sections.abbrev = sections.abbrev;
8690 dwo_file->sections.line = sections.line;
8691 dwo_file->sections.loc = sections.loc;
8692 dwo_file->sections.macinfo = sections.macinfo;
8693 dwo_file->sections.macro = sections.macro;
8694 dwo_file->sections.str_offsets = sections.str_offsets;
8695 /* The "str" section is global to the entire DWP file. */
8696 dwo_file->sections.str = dwp_file->sections.str;
8697 /* The info or types section is assigned later to dwo_unit,
8698 there's no need to record it in dwo_file.
8699 Also, we can't simply record type sections in dwo_file because
8700 we record a pointer into the vector in dwo_unit. As we collect more
8701 types we'll grow the vector and eventually have to reallocate space
8702 for it, invalidating all the pointers into the current copy. */
8703 *dwo_file_slot = dwo_file;
8704 }
8705 else
8706 {
8707 if (dwarf2_read_debug)
8708 {
8709 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8710 virtual_dwo_name);
8711 }
8712 dwo_file = *dwo_file_slot;
8713 }
8714 do_cleanups (cleanups);
8715
8716 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8717 dwo_unit->dwo_file = dwo_file;
8718 dwo_unit->signature = signature;
8719 dwo_unit->info_or_types_section =
8720 obstack_alloc (&objfile->objfile_obstack,
8721 sizeof (struct dwarf2_section_info));
8722 *dwo_unit->info_or_types_section = sections.info_or_types;
8723 /* offset, length, type_offset_in_tu are set later. */
8724
8725 return dwo_unit;
8726}
8727
8728/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8729
8730static struct dwo_unit *
8731lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8732 const struct dwp_hash_table *htab,
8733 ULONGEST signature, int is_debug_types)
8734{
8735 bfd *dbfd = dwp_file->dbfd;
8736 uint32_t mask = htab->nr_slots - 1;
8737 uint32_t hash = signature & mask;
8738 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8739 unsigned int i;
8740 void **slot;
8741 struct dwo_unit find_dwo_cu, *dwo_cu;
8742
8743 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8744 find_dwo_cu.signature = signature;
8745 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8746
8747 if (*slot != NULL)
8748 return *slot;
8749
8750 /* Use a for loop so that we don't loop forever on bad debug info. */
8751 for (i = 0; i < htab->nr_slots; ++i)
8752 {
8753 ULONGEST signature_in_table;
8754
8755 signature_in_table =
8756 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8757 if (signature_in_table == signature)
8758 {
8759 uint32_t section_index =
8760 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8761
8762 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8763 signature, is_debug_types);
8764 return *slot;
8765 }
8766 if (signature_in_table == 0)
8767 return NULL;
8768 hash = (hash + hash2) & mask;
8769 }
8770
8771 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8772 " [in module %s]"),
8773 dwp_file->name);
8774}
8775
8776/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8777 Open the file specified by FILE_NAME and hand it off to BFD for
8778 preliminary analysis. Return a newly initialized bfd *, which
8779 includes a canonicalized copy of FILE_NAME.
80626a55 8780 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8781 In case of trouble, return NULL.
8782 NOTE: This function is derived from symfile_bfd_open. */
8783
8784static bfd *
80626a55 8785try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8786{
8787 bfd *sym_bfd;
80626a55 8788 int desc, flags;
3019eac3 8789 char *absolute_name;
3019eac3 8790
80626a55
DE
8791 flags = OPF_TRY_CWD_FIRST;
8792 if (is_dwp)
8793 flags |= OPF_SEARCH_IN_PATH;
8794 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8795 O_RDONLY | O_BINARY, &absolute_name);
8796 if (desc < 0)
8797 return NULL;
8798
bb397797 8799 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8800 if (!sym_bfd)
8801 {
3019eac3
DE
8802 xfree (absolute_name);
8803 return NULL;
8804 }
a4453b7e 8805 xfree (absolute_name);
3019eac3
DE
8806 bfd_set_cacheable (sym_bfd, 1);
8807
8808 if (!bfd_check_format (sym_bfd, bfd_object))
8809 {
cbb099e8 8810 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8811 return NULL;
8812 }
8813
3019eac3
DE
8814 return sym_bfd;
8815}
8816
80626a55 8817/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8818 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8819 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8820 The result is the bfd handle of the file.
8821 If there is a problem finding or opening the file, return NULL.
8822 Upon success, the canonicalized path of the file is stored in the bfd,
8823 same as symfile_bfd_open. */
8824
8825static bfd *
80626a55 8826open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8827{
8828 bfd *abfd;
3019eac3 8829
80626a55
DE
8830 if (IS_ABSOLUTE_PATH (file_name))
8831 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8832
8833 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8834
8835 if (comp_dir != NULL)
8836 {
80626a55 8837 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8838
8839 /* NOTE: If comp_dir is a relative path, this will also try the
8840 search path, which seems useful. */
80626a55 8841 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8842 xfree (path_to_try);
8843 if (abfd != NULL)
8844 return abfd;
8845 }
8846
8847 /* That didn't work, try debug-file-directory, which, despite its name,
8848 is a list of paths. */
8849
8850 if (*debug_file_directory == '\0')
8851 return NULL;
8852
80626a55 8853 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8854}
8855
80626a55
DE
8856/* This function is mapped across the sections and remembers the offset and
8857 size of each of the DWO debugging sections we are interested in. */
8858
8859static void
8860dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8861{
8862 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8863 const struct dwop_section_names *names = &dwop_section_names;
8864
8865 if (section_is_p (sectp->name, &names->abbrev_dwo))
8866 {
8867 dwo_sections->abbrev.asection = sectp;
8868 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8869 }
8870 else if (section_is_p (sectp->name, &names->info_dwo))
8871 {
8872 dwo_sections->info.asection = sectp;
8873 dwo_sections->info.size = bfd_get_section_size (sectp);
8874 }
8875 else if (section_is_p (sectp->name, &names->line_dwo))
8876 {
8877 dwo_sections->line.asection = sectp;
8878 dwo_sections->line.size = bfd_get_section_size (sectp);
8879 }
8880 else if (section_is_p (sectp->name, &names->loc_dwo))
8881 {
8882 dwo_sections->loc.asection = sectp;
8883 dwo_sections->loc.size = bfd_get_section_size (sectp);
8884 }
8885 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8886 {
8887 dwo_sections->macinfo.asection = sectp;
8888 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8889 }
8890 else if (section_is_p (sectp->name, &names->macro_dwo))
8891 {
8892 dwo_sections->macro.asection = sectp;
8893 dwo_sections->macro.size = bfd_get_section_size (sectp);
8894 }
8895 else if (section_is_p (sectp->name, &names->str_dwo))
8896 {
8897 dwo_sections->str.asection = sectp;
8898 dwo_sections->str.size = bfd_get_section_size (sectp);
8899 }
8900 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8901 {
8902 dwo_sections->str_offsets.asection = sectp;
8903 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8904 }
8905 else if (section_is_p (sectp->name, &names->types_dwo))
8906 {
8907 struct dwarf2_section_info type_section;
8908
8909 memset (&type_section, 0, sizeof (type_section));
8910 type_section.asection = sectp;
8911 type_section.size = bfd_get_section_size (sectp);
8912 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8913 &type_section);
8914 }
8915}
8916
8917/* Initialize the use of the DWO file specified by DWO_NAME.
8918 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8919
8920static struct dwo_file *
80626a55 8921open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8922{
8923 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8924 struct dwo_file *dwo_file;
8925 bfd *dbfd;
3019eac3
DE
8926 struct cleanup *cleanups;
8927
80626a55
DE
8928 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8929 if (dbfd == NULL)
8930 {
8931 if (dwarf2_read_debug)
8932 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8933 return NULL;
8934 }
8935 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8936 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8937 dwo_name, strlen (dwo_name));
8938 dwo_file->dbfd = dbfd;
3019eac3
DE
8939
8940 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8941
80626a55 8942 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8943
80626a55 8944 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8945
8946 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8947 dwo_file->sections.types);
8948
8949 discard_cleanups (cleanups);
8950
80626a55
DE
8951 if (dwarf2_read_debug)
8952 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8953
3019eac3
DE
8954 return dwo_file;
8955}
8956
80626a55
DE
8957/* This function is mapped across the sections and remembers the offset and
8958 size of each of the DWP debugging sections we are interested in. */
3019eac3 8959
80626a55
DE
8960static void
8961dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8962{
80626a55
DE
8963 struct dwp_file *dwp_file = dwp_file_ptr;
8964 const struct dwop_section_names *names = &dwop_section_names;
8965 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8966
80626a55
DE
8967 /* Record the ELF section number for later lookup: this is what the
8968 .debug_cu_index,.debug_tu_index tables use. */
8969 gdb_assert (elf_section_nr < dwp_file->num_sections);
8970 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8971
80626a55
DE
8972 /* Look for specific sections that we need. */
8973 if (section_is_p (sectp->name, &names->str_dwo))
8974 {
8975 dwp_file->sections.str.asection = sectp;
8976 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8977 }
8978 else if (section_is_p (sectp->name, &names->cu_index))
8979 {
8980 dwp_file->sections.cu_index.asection = sectp;
8981 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8982 }
8983 else if (section_is_p (sectp->name, &names->tu_index))
8984 {
8985 dwp_file->sections.tu_index.asection = sectp;
8986 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8987 }
8988}
3019eac3 8989
80626a55 8990/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8991
80626a55
DE
8992static hashval_t
8993hash_dwp_loaded_cutus (const void *item)
8994{
8995 const struct dwo_unit *dwo_unit = item;
3019eac3 8996
80626a55
DE
8997 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8998 return dwo_unit->signature;
3019eac3
DE
8999}
9000
80626a55 9001/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9002
80626a55
DE
9003static int
9004eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9005{
80626a55
DE
9006 const struct dwo_unit *dua = a;
9007 const struct dwo_unit *dub = b;
3019eac3 9008
80626a55
DE
9009 return dua->signature == dub->signature;
9010}
3019eac3 9011
80626a55 9012/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9013
80626a55
DE
9014static htab_t
9015allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9016{
9017 return htab_create_alloc_ex (3,
9018 hash_dwp_loaded_cutus,
9019 eq_dwp_loaded_cutus,
9020 NULL,
9021 &objfile->objfile_obstack,
9022 hashtab_obstack_allocate,
9023 dummy_obstack_deallocate);
9024}
3019eac3 9025
80626a55
DE
9026/* Initialize the use of the DWP file for the current objfile.
9027 By convention the name of the DWP file is ${objfile}.dwp.
9028 The result is NULL if it can't be found. */
a766d390 9029
80626a55
DE
9030static struct dwp_file *
9031open_and_init_dwp_file (const char *comp_dir)
9032{
9033 struct objfile *objfile = dwarf2_per_objfile->objfile;
9034 struct dwp_file *dwp_file;
9035 char *dwp_name;
9036 bfd *dbfd;
9037 struct cleanup *cleanups;
9038
2792b94d 9039 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9040 cleanups = make_cleanup (xfree, dwp_name);
9041
9042 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9043 if (dbfd == NULL)
9044 {
9045 if (dwarf2_read_debug)
9046 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9047 do_cleanups (cleanups);
9048 return NULL;
3019eac3 9049 }
80626a55
DE
9050 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9051 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9052 dwp_name, strlen (dwp_name));
9053 dwp_file->dbfd = dbfd;
9054 do_cleanups (cleanups);
c906108c 9055
80626a55 9056 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9057
80626a55
DE
9058 /* +1: section 0 is unused */
9059 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9060 dwp_file->elf_sections =
9061 OBSTACK_CALLOC (&objfile->objfile_obstack,
9062 dwp_file->num_sections, asection *);
9063
9064 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9065
9066 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9067
9068 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9069
9070 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9071
9072 discard_cleanups (cleanups);
9073
9074 if (dwarf2_read_debug)
9075 {
9076 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9077 fprintf_unfiltered (gdb_stdlog,
9078 " %u CUs, %u TUs\n",
9079 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9080 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9081 }
9082
9083 return dwp_file;
3019eac3 9084}
c906108c 9085
80626a55
DE
9086/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9087 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9088 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9089 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9090 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9091
9092 This is called, for example, when wanting to read a variable with a
9093 complex location. Therefore we don't want to do file i/o for every call.
9094 Therefore we don't want to look for a DWO file on every call.
9095 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9096 then we check if we've already seen DWO_NAME, and only THEN do we check
9097 for a DWO file.
9098
1c658ad5 9099 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9100 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9101
3019eac3 9102static struct dwo_unit *
80626a55
DE
9103lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9104 const char *dwo_name, const char *comp_dir,
9105 ULONGEST signature, int is_debug_types)
3019eac3
DE
9106{
9107 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9108 const char *kind = is_debug_types ? "TU" : "CU";
9109 void **dwo_file_slot;
3019eac3 9110 struct dwo_file *dwo_file;
80626a55 9111 struct dwp_file *dwp_file;
cb1df416 9112
80626a55 9113 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9114
80626a55
DE
9115 if (! dwarf2_per_objfile->dwp_checked)
9116 {
9117 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9118 dwarf2_per_objfile->dwp_checked = 1;
9119 }
9120 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9121
80626a55 9122 if (dwp_file != NULL)
cf2c3c16 9123 {
80626a55
DE
9124 const struct dwp_hash_table *dwp_htab =
9125 is_debug_types ? dwp_file->tus : dwp_file->cus;
9126
9127 if (dwp_htab != NULL)
9128 {
9129 struct dwo_unit *dwo_cutu =
9130 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9131
9132 if (dwo_cutu != NULL)
9133 {
9134 if (dwarf2_read_debug)
9135 {
9136 fprintf_unfiltered (gdb_stdlog,
9137 "Virtual DWO %s %s found: @%s\n",
9138 kind, hex_string (signature),
9139 host_address_to_string (dwo_cutu));
9140 }
9141 return dwo_cutu;
9142 }
9143 }
9144 }
9145
9146 /* Have we already seen DWO_NAME? */
9147
9148 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9149 if (*dwo_file_slot == NULL)
9150 {
9151 /* Read in the file and build a table of the DWOs it contains. */
9152 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9153 }
9154 /* NOTE: This will be NULL if unable to open the file. */
9155 dwo_file = *dwo_file_slot;
9156
9157 if (dwo_file != NULL)
9158 {
9159 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9160
9161 if (htab != NULL)
9162 {
9163 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9164
80626a55
DE
9165 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9166 find_dwo_cutu.signature = signature;
9167 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9168
80626a55
DE
9169 if (dwo_cutu != NULL)
9170 {
9171 if (dwarf2_read_debug)
9172 {
9173 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9174 kind, dwo_name, hex_string (signature),
9175 host_address_to_string (dwo_cutu));
9176 }
9177 return dwo_cutu;
9178 }
9179 }
2e276125 9180 }
9cdd5dbd 9181
80626a55
DE
9182 /* We didn't find it. This could mean a dwo_id mismatch, or
9183 someone deleted the DWO/DWP file, or the search path isn't set up
9184 correctly to find the file. */
9185
9186 if (dwarf2_read_debug)
9187 {
9188 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9189 kind, dwo_name, hex_string (signature));
9190 }
3019eac3
DE
9191
9192 complaint (&symfile_complaints,
80626a55 9193 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9194 " [in module %s]"),
80626a55 9195 this_unit->offset.sect_off, objfile->name);
3019eac3 9196 return NULL;
5fb290d7
DJ
9197}
9198
80626a55
DE
9199/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9200 See lookup_dwo_cutu_unit for details. */
9201
9202static struct dwo_unit *
9203lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9204 const char *dwo_name, const char *comp_dir,
9205 ULONGEST signature)
9206{
9207 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9208}
9209
9210/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9211 See lookup_dwo_cutu_unit for details. */
9212
9213static struct dwo_unit *
9214lookup_dwo_type_unit (struct signatured_type *this_tu,
9215 const char *dwo_name, const char *comp_dir)
9216{
9217 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9218}
9219
3019eac3
DE
9220/* Free all resources associated with DWO_FILE.
9221 Close the DWO file and munmap the sections.
9222 All memory should be on the objfile obstack. */
348e048f
DE
9223
9224static void
3019eac3 9225free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9226{
3019eac3
DE
9227 int ix;
9228 struct dwarf2_section_info *section;
348e048f 9229
80626a55
DE
9230 gdb_assert (dwo_file->dbfd != objfile->obfd);
9231 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9232
3019eac3
DE
9233 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9234}
348e048f 9235
3019eac3 9236/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9237
3019eac3
DE
9238static void
9239free_dwo_file_cleanup (void *arg)
9240{
9241 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9242 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9243
3019eac3
DE
9244 free_dwo_file (dwo_file, objfile);
9245}
348e048f 9246
3019eac3 9247/* Traversal function for free_dwo_files. */
2ab95328 9248
3019eac3
DE
9249static int
9250free_dwo_file_from_slot (void **slot, void *info)
9251{
9252 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9253 struct objfile *objfile = (struct objfile *) info;
348e048f 9254
3019eac3 9255 free_dwo_file (dwo_file, objfile);
348e048f 9256
3019eac3
DE
9257 return 1;
9258}
348e048f 9259
3019eac3 9260/* Free all resources associated with DWO_FILES. */
348e048f 9261
3019eac3
DE
9262static void
9263free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9264{
9265 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9266}
3019eac3
DE
9267\f
9268/* Read in various DIEs. */
348e048f 9269
d389af10
JK
9270/* qsort helper for inherit_abstract_dies. */
9271
9272static int
9273unsigned_int_compar (const void *ap, const void *bp)
9274{
9275 unsigned int a = *(unsigned int *) ap;
9276 unsigned int b = *(unsigned int *) bp;
9277
9278 return (a > b) - (b > a);
9279}
9280
9281/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9282 Inherit only the children of the DW_AT_abstract_origin DIE not being
9283 already referenced by DW_AT_abstract_origin from the children of the
9284 current DIE. */
d389af10
JK
9285
9286static void
9287inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9288{
9289 struct die_info *child_die;
9290 unsigned die_children_count;
9291 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9292 sect_offset *offsets;
9293 sect_offset *offsets_end, *offsetp;
d389af10
JK
9294 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9295 struct die_info *origin_die;
9296 /* Iterator of the ORIGIN_DIE children. */
9297 struct die_info *origin_child_die;
9298 struct cleanup *cleanups;
9299 struct attribute *attr;
cd02d79d
PA
9300 struct dwarf2_cu *origin_cu;
9301 struct pending **origin_previous_list_in_scope;
d389af10
JK
9302
9303 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9304 if (!attr)
9305 return;
9306
cd02d79d
PA
9307 /* Note that following die references may follow to a die in a
9308 different cu. */
9309
9310 origin_cu = cu;
9311 origin_die = follow_die_ref (die, attr, &origin_cu);
9312
9313 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9314 symbols in. */
9315 origin_previous_list_in_scope = origin_cu->list_in_scope;
9316 origin_cu->list_in_scope = cu->list_in_scope;
9317
edb3359d
DJ
9318 if (die->tag != origin_die->tag
9319 && !(die->tag == DW_TAG_inlined_subroutine
9320 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9321 complaint (&symfile_complaints,
9322 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9323 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9324
9325 child_die = die->child;
9326 die_children_count = 0;
9327 while (child_die && child_die->tag)
9328 {
9329 child_die = sibling_die (child_die);
9330 die_children_count++;
9331 }
9332 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9333 cleanups = make_cleanup (xfree, offsets);
9334
9335 offsets_end = offsets;
9336 child_die = die->child;
9337 while (child_die && child_die->tag)
9338 {
c38f313d
DJ
9339 /* For each CHILD_DIE, find the corresponding child of
9340 ORIGIN_DIE. If there is more than one layer of
9341 DW_AT_abstract_origin, follow them all; there shouldn't be,
9342 but GCC versions at least through 4.4 generate this (GCC PR
9343 40573). */
9344 struct die_info *child_origin_die = child_die;
cd02d79d 9345 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9346
c38f313d
DJ
9347 while (1)
9348 {
cd02d79d
PA
9349 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9350 child_origin_cu);
c38f313d
DJ
9351 if (attr == NULL)
9352 break;
cd02d79d
PA
9353 child_origin_die = follow_die_ref (child_origin_die, attr,
9354 &child_origin_cu);
c38f313d
DJ
9355 }
9356
d389af10
JK
9357 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9358 counterpart may exist. */
c38f313d 9359 if (child_origin_die != child_die)
d389af10 9360 {
edb3359d
DJ
9361 if (child_die->tag != child_origin_die->tag
9362 && !(child_die->tag == DW_TAG_inlined_subroutine
9363 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9364 complaint (&symfile_complaints,
9365 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9366 "different tags"), child_die->offset.sect_off,
9367 child_origin_die->offset.sect_off);
c38f313d
DJ
9368 if (child_origin_die->parent != origin_die)
9369 complaint (&symfile_complaints,
9370 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9371 "different parents"), child_die->offset.sect_off,
9372 child_origin_die->offset.sect_off);
c38f313d
DJ
9373 else
9374 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9375 }
9376 child_die = sibling_die (child_die);
9377 }
9378 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9379 unsigned_int_compar);
9380 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9381 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9382 complaint (&symfile_complaints,
9383 _("Multiple children of DIE 0x%x refer "
9384 "to DIE 0x%x as their abstract origin"),
b64f50a1 9385 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9386
9387 offsetp = offsets;
9388 origin_child_die = origin_die->child;
9389 while (origin_child_die && origin_child_die->tag)
9390 {
9391 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9392 while (offsetp < offsets_end
9393 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9394 offsetp++;
b64f50a1
JK
9395 if (offsetp >= offsets_end
9396 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9397 {
9398 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9399 process_die (origin_child_die, origin_cu);
d389af10
JK
9400 }
9401 origin_child_die = sibling_die (origin_child_die);
9402 }
cd02d79d 9403 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9404
9405 do_cleanups (cleanups);
9406}
9407
c906108c 9408static void
e7c27a73 9409read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9410{
e7c27a73 9411 struct objfile *objfile = cu->objfile;
52f0bd74 9412 struct context_stack *new;
c906108c
SS
9413 CORE_ADDR lowpc;
9414 CORE_ADDR highpc;
9415 struct die_info *child_die;
edb3359d 9416 struct attribute *attr, *call_line, *call_file;
15d034d0 9417 const char *name;
e142c38c 9418 CORE_ADDR baseaddr;
801e3a5b 9419 struct block *block;
edb3359d 9420 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9421 VEC (symbolp) *template_args = NULL;
9422 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9423
9424 if (inlined_func)
9425 {
9426 /* If we do not have call site information, we can't show the
9427 caller of this inlined function. That's too confusing, so
9428 only use the scope for local variables. */
9429 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9430 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9431 if (call_line == NULL || call_file == NULL)
9432 {
9433 read_lexical_block_scope (die, cu);
9434 return;
9435 }
9436 }
c906108c 9437
e142c38c
DJ
9438 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9439
94af9270 9440 name = dwarf2_name (die, cu);
c906108c 9441
e8d05480
JB
9442 /* Ignore functions with missing or empty names. These are actually
9443 illegal according to the DWARF standard. */
9444 if (name == NULL)
9445 {
9446 complaint (&symfile_complaints,
b64f50a1
JK
9447 _("missing name for subprogram DIE at %d"),
9448 die->offset.sect_off);
e8d05480
JB
9449 return;
9450 }
9451
9452 /* Ignore functions with missing or invalid low and high pc attributes. */
9453 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9454 {
ae4d0c03
PM
9455 attr = dwarf2_attr (die, DW_AT_external, cu);
9456 if (!attr || !DW_UNSND (attr))
9457 complaint (&symfile_complaints,
3e43a32a
MS
9458 _("cannot get low and high bounds "
9459 "for subprogram DIE at %d"),
b64f50a1 9460 die->offset.sect_off);
e8d05480
JB
9461 return;
9462 }
c906108c
SS
9463
9464 lowpc += baseaddr;
9465 highpc += baseaddr;
9466
34eaf542
TT
9467 /* If we have any template arguments, then we must allocate a
9468 different sort of symbol. */
9469 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9470 {
9471 if (child_die->tag == DW_TAG_template_type_param
9472 || child_die->tag == DW_TAG_template_value_param)
9473 {
9474 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9475 struct template_symbol);
9476 templ_func->base.is_cplus_template_function = 1;
9477 break;
9478 }
9479 }
9480
c906108c 9481 new = push_context (0, lowpc);
34eaf542
TT
9482 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9483 (struct symbol *) templ_func);
4c2df51b 9484
4cecd739
DJ
9485 /* If there is a location expression for DW_AT_frame_base, record
9486 it. */
e142c38c 9487 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9488 if (attr)
c034e007
AC
9489 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9490 expression is being recorded directly in the function's symbol
9491 and not in a separate frame-base object. I guess this hack is
9492 to avoid adding some sort of frame-base adjunct/annex to the
9493 function's symbol :-(. The problem with doing this is that it
9494 results in a function symbol with a location expression that
9495 has nothing to do with the location of the function, ouch! The
9496 relationship should be: a function's symbol has-a frame base; a
9497 frame-base has-a location expression. */
e7c27a73 9498 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9499
e142c38c 9500 cu->list_in_scope = &local_symbols;
c906108c 9501
639d11d3 9502 if (die->child != NULL)
c906108c 9503 {
639d11d3 9504 child_die = die->child;
c906108c
SS
9505 while (child_die && child_die->tag)
9506 {
34eaf542
TT
9507 if (child_die->tag == DW_TAG_template_type_param
9508 || child_die->tag == DW_TAG_template_value_param)
9509 {
9510 struct symbol *arg = new_symbol (child_die, NULL, cu);
9511
f1078f66
DJ
9512 if (arg != NULL)
9513 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9514 }
9515 else
9516 process_die (child_die, cu);
c906108c
SS
9517 child_die = sibling_die (child_die);
9518 }
9519 }
9520
d389af10
JK
9521 inherit_abstract_dies (die, cu);
9522
4a811a97
UW
9523 /* If we have a DW_AT_specification, we might need to import using
9524 directives from the context of the specification DIE. See the
9525 comment in determine_prefix. */
9526 if (cu->language == language_cplus
9527 && dwarf2_attr (die, DW_AT_specification, cu))
9528 {
9529 struct dwarf2_cu *spec_cu = cu;
9530 struct die_info *spec_die = die_specification (die, &spec_cu);
9531
9532 while (spec_die)
9533 {
9534 child_die = spec_die->child;
9535 while (child_die && child_die->tag)
9536 {
9537 if (child_die->tag == DW_TAG_imported_module)
9538 process_die (child_die, spec_cu);
9539 child_die = sibling_die (child_die);
9540 }
9541
9542 /* In some cases, GCC generates specification DIEs that
9543 themselves contain DW_AT_specification attributes. */
9544 spec_die = die_specification (spec_die, &spec_cu);
9545 }
9546 }
9547
c906108c
SS
9548 new = pop_context ();
9549 /* Make a block for the local symbols within. */
801e3a5b
JB
9550 block = finish_block (new->name, &local_symbols, new->old_blocks,
9551 lowpc, highpc, objfile);
9552
df8a16a1 9553 /* For C++, set the block's scope. */
f55ee35c 9554 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9555 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9556 determine_prefix (die, cu),
df8a16a1
DJ
9557 processing_has_namespace_info);
9558
801e3a5b
JB
9559 /* If we have address ranges, record them. */
9560 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9561
34eaf542
TT
9562 /* Attach template arguments to function. */
9563 if (! VEC_empty (symbolp, template_args))
9564 {
9565 gdb_assert (templ_func != NULL);
9566
9567 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9568 templ_func->template_arguments
9569 = obstack_alloc (&objfile->objfile_obstack,
9570 (templ_func->n_template_arguments
9571 * sizeof (struct symbol *)));
9572 memcpy (templ_func->template_arguments,
9573 VEC_address (symbolp, template_args),
9574 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9575 VEC_free (symbolp, template_args);
9576 }
9577
208d8187
JB
9578 /* In C++, we can have functions nested inside functions (e.g., when
9579 a function declares a class that has methods). This means that
9580 when we finish processing a function scope, we may need to go
9581 back to building a containing block's symbol lists. */
9582 local_symbols = new->locals;
27aa8d6a 9583 using_directives = new->using_directives;
208d8187 9584
921e78cf
JB
9585 /* If we've finished processing a top-level function, subsequent
9586 symbols go in the file symbol list. */
9587 if (outermost_context_p ())
e142c38c 9588 cu->list_in_scope = &file_symbols;
c906108c
SS
9589}
9590
9591/* Process all the DIES contained within a lexical block scope. Start
9592 a new scope, process the dies, and then close the scope. */
9593
9594static void
e7c27a73 9595read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9596{
e7c27a73 9597 struct objfile *objfile = cu->objfile;
52f0bd74 9598 struct context_stack *new;
c906108c
SS
9599 CORE_ADDR lowpc, highpc;
9600 struct die_info *child_die;
e142c38c
DJ
9601 CORE_ADDR baseaddr;
9602
9603 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9604
9605 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9606 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9607 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9608 be nasty. Might be easier to properly extend generic blocks to
af34e669 9609 describe ranges. */
d85a05f0 9610 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9611 return;
9612 lowpc += baseaddr;
9613 highpc += baseaddr;
9614
9615 push_context (0, lowpc);
639d11d3 9616 if (die->child != NULL)
c906108c 9617 {
639d11d3 9618 child_die = die->child;
c906108c
SS
9619 while (child_die && child_die->tag)
9620 {
e7c27a73 9621 process_die (child_die, cu);
c906108c
SS
9622 child_die = sibling_die (child_die);
9623 }
9624 }
9625 new = pop_context ();
9626
8540c487 9627 if (local_symbols != NULL || using_directives != NULL)
c906108c 9628 {
801e3a5b
JB
9629 struct block *block
9630 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9631 highpc, objfile);
9632
9633 /* Note that recording ranges after traversing children, as we
9634 do here, means that recording a parent's ranges entails
9635 walking across all its children's ranges as they appear in
9636 the address map, which is quadratic behavior.
9637
9638 It would be nicer to record the parent's ranges before
9639 traversing its children, simply overriding whatever you find
9640 there. But since we don't even decide whether to create a
9641 block until after we've traversed its children, that's hard
9642 to do. */
9643 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9644 }
9645 local_symbols = new->locals;
27aa8d6a 9646 using_directives = new->using_directives;
c906108c
SS
9647}
9648
96408a79
SA
9649/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9650
9651static void
9652read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9653{
9654 struct objfile *objfile = cu->objfile;
9655 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9656 CORE_ADDR pc, baseaddr;
9657 struct attribute *attr;
9658 struct call_site *call_site, call_site_local;
9659 void **slot;
9660 int nparams;
9661 struct die_info *child_die;
9662
9663 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9664
9665 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9666 if (!attr)
9667 {
9668 complaint (&symfile_complaints,
9669 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9670 "DIE 0x%x [in module %s]"),
b64f50a1 9671 die->offset.sect_off, objfile->name);
96408a79
SA
9672 return;
9673 }
9674 pc = DW_ADDR (attr) + baseaddr;
9675
9676 if (cu->call_site_htab == NULL)
9677 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9678 NULL, &objfile->objfile_obstack,
9679 hashtab_obstack_allocate, NULL);
9680 call_site_local.pc = pc;
9681 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9682 if (*slot != NULL)
9683 {
9684 complaint (&symfile_complaints,
9685 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9686 "DIE 0x%x [in module %s]"),
b64f50a1 9687 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9688 return;
9689 }
9690
9691 /* Count parameters at the caller. */
9692
9693 nparams = 0;
9694 for (child_die = die->child; child_die && child_die->tag;
9695 child_die = sibling_die (child_die))
9696 {
9697 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9698 {
9699 complaint (&symfile_complaints,
9700 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9701 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9702 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9703 continue;
9704 }
9705
9706 nparams++;
9707 }
9708
9709 call_site = obstack_alloc (&objfile->objfile_obstack,
9710 (sizeof (*call_site)
9711 + (sizeof (*call_site->parameter)
9712 * (nparams - 1))));
9713 *slot = call_site;
9714 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9715 call_site->pc = pc;
9716
9717 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9718 {
9719 struct die_info *func_die;
9720
9721 /* Skip also over DW_TAG_inlined_subroutine. */
9722 for (func_die = die->parent;
9723 func_die && func_die->tag != DW_TAG_subprogram
9724 && func_die->tag != DW_TAG_subroutine_type;
9725 func_die = func_die->parent);
9726
9727 /* DW_AT_GNU_all_call_sites is a superset
9728 of DW_AT_GNU_all_tail_call_sites. */
9729 if (func_die
9730 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9731 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9732 {
9733 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9734 not complete. But keep CALL_SITE for look ups via call_site_htab,
9735 both the initial caller containing the real return address PC and
9736 the final callee containing the current PC of a chain of tail
9737 calls do not need to have the tail call list complete. But any
9738 function candidate for a virtual tail call frame searched via
9739 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9740 determined unambiguously. */
9741 }
9742 else
9743 {
9744 struct type *func_type = NULL;
9745
9746 if (func_die)
9747 func_type = get_die_type (func_die, cu);
9748 if (func_type != NULL)
9749 {
9750 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9751
9752 /* Enlist this call site to the function. */
9753 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9754 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9755 }
9756 else
9757 complaint (&symfile_complaints,
9758 _("Cannot find function owning DW_TAG_GNU_call_site "
9759 "DIE 0x%x [in module %s]"),
b64f50a1 9760 die->offset.sect_off, objfile->name);
96408a79
SA
9761 }
9762 }
9763
9764 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9765 if (attr == NULL)
9766 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9767 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9768 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9769 /* Keep NULL DWARF_BLOCK. */;
9770 else if (attr_form_is_block (attr))
9771 {
9772 struct dwarf2_locexpr_baton *dlbaton;
9773
9774 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9775 dlbaton->data = DW_BLOCK (attr)->data;
9776 dlbaton->size = DW_BLOCK (attr)->size;
9777 dlbaton->per_cu = cu->per_cu;
9778
9779 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9780 }
9781 else if (is_ref_attr (attr))
9782 {
96408a79
SA
9783 struct dwarf2_cu *target_cu = cu;
9784 struct die_info *target_die;
9785
9786 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9787 gdb_assert (target_cu->objfile == objfile);
9788 if (die_is_declaration (target_die, target_cu))
9789 {
9790 const char *target_physname;
9791
9792 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9793 if (target_physname == NULL)
9794 complaint (&symfile_complaints,
9795 _("DW_AT_GNU_call_site_target target DIE has invalid "
9796 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9797 die->offset.sect_off, objfile->name);
96408a79
SA
9798 else
9799 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9800 }
9801 else
9802 {
9803 CORE_ADDR lowpc;
9804
9805 /* DW_AT_entry_pc should be preferred. */
9806 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9807 complaint (&symfile_complaints,
9808 _("DW_AT_GNU_call_site_target target DIE has invalid "
9809 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9810 die->offset.sect_off, objfile->name);
96408a79
SA
9811 else
9812 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9813 }
9814 }
9815 else
9816 complaint (&symfile_complaints,
9817 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9818 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9819 die->offset.sect_off, objfile->name);
96408a79
SA
9820
9821 call_site->per_cu = cu->per_cu;
9822
9823 for (child_die = die->child;
9824 child_die && child_die->tag;
9825 child_die = sibling_die (child_die))
9826 {
96408a79 9827 struct call_site_parameter *parameter;
1788b2d3 9828 struct attribute *loc, *origin;
96408a79
SA
9829
9830 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9831 {
9832 /* Already printed the complaint above. */
9833 continue;
9834 }
9835
9836 gdb_assert (call_site->parameter_count < nparams);
9837 parameter = &call_site->parameter[call_site->parameter_count];
9838
1788b2d3
JK
9839 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9840 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9841 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9842
24c5c679 9843 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9844 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9845 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9846 {
9847 sect_offset offset;
9848
9849 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9850 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9851 if (!offset_in_cu_p (&cu->header, offset))
9852 {
9853 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9854 binding can be done only inside one CU. Such referenced DIE
9855 therefore cannot be even moved to DW_TAG_partial_unit. */
9856 complaint (&symfile_complaints,
9857 _("DW_AT_abstract_origin offset is not in CU for "
9858 "DW_TAG_GNU_call_site child DIE 0x%x "
9859 "[in module %s]"),
9860 child_die->offset.sect_off, objfile->name);
9861 continue;
9862 }
1788b2d3
JK
9863 parameter->u.param_offset.cu_off = (offset.sect_off
9864 - cu->header.offset.sect_off);
9865 }
9866 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9867 {
9868 complaint (&symfile_complaints,
9869 _("No DW_FORM_block* DW_AT_location for "
9870 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9871 child_die->offset.sect_off, objfile->name);
96408a79
SA
9872 continue;
9873 }
24c5c679 9874 else
96408a79 9875 {
24c5c679
JK
9876 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9877 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9878 if (parameter->u.dwarf_reg != -1)
9879 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9880 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9881 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9882 &parameter->u.fb_offset))
9883 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9884 else
9885 {
9886 complaint (&symfile_complaints,
9887 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9888 "for DW_FORM_block* DW_AT_location is supported for "
9889 "DW_TAG_GNU_call_site child DIE 0x%x "
9890 "[in module %s]"),
9891 child_die->offset.sect_off, objfile->name);
9892 continue;
9893 }
96408a79
SA
9894 }
9895
9896 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9897 if (!attr_form_is_block (attr))
9898 {
9899 complaint (&symfile_complaints,
9900 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9901 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9902 child_die->offset.sect_off, objfile->name);
96408a79
SA
9903 continue;
9904 }
9905 parameter->value = DW_BLOCK (attr)->data;
9906 parameter->value_size = DW_BLOCK (attr)->size;
9907
9908 /* Parameters are not pre-cleared by memset above. */
9909 parameter->data_value = NULL;
9910 parameter->data_value_size = 0;
9911 call_site->parameter_count++;
9912
9913 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9914 if (attr)
9915 {
9916 if (!attr_form_is_block (attr))
9917 complaint (&symfile_complaints,
9918 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9919 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9920 child_die->offset.sect_off, objfile->name);
96408a79
SA
9921 else
9922 {
9923 parameter->data_value = DW_BLOCK (attr)->data;
9924 parameter->data_value_size = DW_BLOCK (attr)->size;
9925 }
9926 }
9927 }
9928}
9929
43039443 9930/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9931 Return 1 if the attributes are present and valid, otherwise, return 0.
9932 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9933
9934static int
9935dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9936 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9937 struct partial_symtab *ranges_pst)
43039443
JK
9938{
9939 struct objfile *objfile = cu->objfile;
9940 struct comp_unit_head *cu_header = &cu->header;
9941 bfd *obfd = objfile->obfd;
9942 unsigned int addr_size = cu_header->addr_size;
9943 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9944 /* Base address selection entry. */
9945 CORE_ADDR base;
9946 int found_base;
9947 unsigned int dummy;
9948 gdb_byte *buffer;
9949 CORE_ADDR marker;
9950 int low_set;
9951 CORE_ADDR low = 0;
9952 CORE_ADDR high = 0;
ff013f42 9953 CORE_ADDR baseaddr;
43039443 9954
d00adf39
DE
9955 found_base = cu->base_known;
9956 base = cu->base_address;
43039443 9957
be391dca 9958 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9959 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9960 {
9961 complaint (&symfile_complaints,
9962 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9963 offset);
9964 return 0;
9965 }
dce234bc 9966 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9967
9968 /* Read in the largest possible address. */
9969 marker = read_address (obfd, buffer, cu, &dummy);
9970 if ((marker & mask) == mask)
9971 {
9972 /* If we found the largest possible address, then
9973 read the base address. */
9974 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9975 buffer += 2 * addr_size;
9976 offset += 2 * addr_size;
9977 found_base = 1;
9978 }
9979
9980 low_set = 0;
9981
e7030f15 9982 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9983
43039443
JK
9984 while (1)
9985 {
9986 CORE_ADDR range_beginning, range_end;
9987
9988 range_beginning = read_address (obfd, buffer, cu, &dummy);
9989 buffer += addr_size;
9990 range_end = read_address (obfd, buffer, cu, &dummy);
9991 buffer += addr_size;
9992 offset += 2 * addr_size;
9993
9994 /* An end of list marker is a pair of zero addresses. */
9995 if (range_beginning == 0 && range_end == 0)
9996 /* Found the end of list entry. */
9997 break;
9998
9999 /* Each base address selection entry is a pair of 2 values.
10000 The first is the largest possible address, the second is
10001 the base address. Check for a base address here. */
10002 if ((range_beginning & mask) == mask)
10003 {
10004 /* If we found the largest possible address, then
10005 read the base address. */
10006 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10007 found_base = 1;
10008 continue;
10009 }
10010
10011 if (!found_base)
10012 {
10013 /* We have no valid base address for the ranges
10014 data. */
10015 complaint (&symfile_complaints,
10016 _("Invalid .debug_ranges data (no base address)"));
10017 return 0;
10018 }
10019
9277c30c
UW
10020 if (range_beginning > range_end)
10021 {
10022 /* Inverted range entries are invalid. */
10023 complaint (&symfile_complaints,
10024 _("Invalid .debug_ranges data (inverted range)"));
10025 return 0;
10026 }
10027
10028 /* Empty range entries have no effect. */
10029 if (range_beginning == range_end)
10030 continue;
10031
43039443
JK
10032 range_beginning += base;
10033 range_end += base;
10034
01093045
DE
10035 /* A not-uncommon case of bad debug info.
10036 Don't pollute the addrmap with bad data. */
10037 if (range_beginning + baseaddr == 0
10038 && !dwarf2_per_objfile->has_section_at_zero)
10039 {
10040 complaint (&symfile_complaints,
10041 _(".debug_ranges entry has start address of zero"
10042 " [in module %s]"), objfile->name);
10043 continue;
10044 }
10045
9277c30c 10046 if (ranges_pst != NULL)
ff013f42 10047 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10048 range_beginning + baseaddr,
10049 range_end - 1 + baseaddr,
ff013f42
JK
10050 ranges_pst);
10051
43039443
JK
10052 /* FIXME: This is recording everything as a low-high
10053 segment of consecutive addresses. We should have a
10054 data structure for discontiguous block ranges
10055 instead. */
10056 if (! low_set)
10057 {
10058 low = range_beginning;
10059 high = range_end;
10060 low_set = 1;
10061 }
10062 else
10063 {
10064 if (range_beginning < low)
10065 low = range_beginning;
10066 if (range_end > high)
10067 high = range_end;
10068 }
10069 }
10070
10071 if (! low_set)
10072 /* If the first entry is an end-of-list marker, the range
10073 describes an empty scope, i.e. no instructions. */
10074 return 0;
10075
10076 if (low_return)
10077 *low_return = low;
10078 if (high_return)
10079 *high_return = high;
10080 return 1;
10081}
10082
af34e669
DJ
10083/* Get low and high pc attributes from a die. Return 1 if the attributes
10084 are present and valid, otherwise, return 0. Return -1 if the range is
10085 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10086
c906108c 10087static int
af34e669 10088dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10089 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10090 struct partial_symtab *pst)
c906108c
SS
10091{
10092 struct attribute *attr;
91da1414 10093 struct attribute *attr_high;
af34e669
DJ
10094 CORE_ADDR low = 0;
10095 CORE_ADDR high = 0;
10096 int ret = 0;
c906108c 10097
91da1414
MW
10098 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10099 if (attr_high)
af34e669 10100 {
e142c38c 10101 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10102 if (attr)
91da1414
MW
10103 {
10104 low = DW_ADDR (attr);
3019eac3
DE
10105 if (attr_high->form == DW_FORM_addr
10106 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10107 high = DW_ADDR (attr_high);
10108 else
10109 high = low + DW_UNSND (attr_high);
10110 }
af34e669
DJ
10111 else
10112 /* Found high w/o low attribute. */
10113 return 0;
10114
10115 /* Found consecutive range of addresses. */
10116 ret = 1;
10117 }
c906108c 10118 else
af34e669 10119 {
e142c38c 10120 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10121 if (attr != NULL)
10122 {
ab435259
DE
10123 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10124 We take advantage of the fact that DW_AT_ranges does not appear
10125 in DW_TAG_compile_unit of DWO files. */
10126 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10127 unsigned int ranges_offset = (DW_UNSND (attr)
10128 + (need_ranges_base
10129 ? cu->ranges_base
10130 : 0));
2e3cf129 10131
af34e669 10132 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10133 .debug_ranges section. */
2e3cf129 10134 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10135 return 0;
43039443 10136 /* Found discontinuous range of addresses. */
af34e669
DJ
10137 ret = -1;
10138 }
10139 }
c906108c 10140
9373cf26
JK
10141 /* read_partial_die has also the strict LOW < HIGH requirement. */
10142 if (high <= low)
c906108c
SS
10143 return 0;
10144
10145 /* When using the GNU linker, .gnu.linkonce. sections are used to
10146 eliminate duplicate copies of functions and vtables and such.
10147 The linker will arbitrarily choose one and discard the others.
10148 The AT_*_pc values for such functions refer to local labels in
10149 these sections. If the section from that file was discarded, the
10150 labels are not in the output, so the relocs get a value of 0.
10151 If this is a discarded function, mark the pc bounds as invalid,
10152 so that GDB will ignore it. */
72dca2f5 10153 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10154 return 0;
10155
10156 *lowpc = low;
96408a79
SA
10157 if (highpc)
10158 *highpc = high;
af34e669 10159 return ret;
c906108c
SS
10160}
10161
b084d499
JB
10162/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10163 its low and high PC addresses. Do nothing if these addresses could not
10164 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10165 and HIGHPC to the high address if greater than HIGHPC. */
10166
10167static void
10168dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10169 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10170 struct dwarf2_cu *cu)
10171{
10172 CORE_ADDR low, high;
10173 struct die_info *child = die->child;
10174
d85a05f0 10175 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10176 {
10177 *lowpc = min (*lowpc, low);
10178 *highpc = max (*highpc, high);
10179 }
10180
10181 /* If the language does not allow nested subprograms (either inside
10182 subprograms or lexical blocks), we're done. */
10183 if (cu->language != language_ada)
10184 return;
6e70227d 10185
b084d499
JB
10186 /* Check all the children of the given DIE. If it contains nested
10187 subprograms, then check their pc bounds. Likewise, we need to
10188 check lexical blocks as well, as they may also contain subprogram
10189 definitions. */
10190 while (child && child->tag)
10191 {
10192 if (child->tag == DW_TAG_subprogram
10193 || child->tag == DW_TAG_lexical_block)
10194 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10195 child = sibling_die (child);
10196 }
10197}
10198
fae299cd
DC
10199/* Get the low and high pc's represented by the scope DIE, and store
10200 them in *LOWPC and *HIGHPC. If the correct values can't be
10201 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10202
10203static void
10204get_scope_pc_bounds (struct die_info *die,
10205 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10206 struct dwarf2_cu *cu)
10207{
10208 CORE_ADDR best_low = (CORE_ADDR) -1;
10209 CORE_ADDR best_high = (CORE_ADDR) 0;
10210 CORE_ADDR current_low, current_high;
10211
d85a05f0 10212 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10213 {
10214 best_low = current_low;
10215 best_high = current_high;
10216 }
10217 else
10218 {
10219 struct die_info *child = die->child;
10220
10221 while (child && child->tag)
10222 {
10223 switch (child->tag) {
10224 case DW_TAG_subprogram:
b084d499 10225 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10226 break;
10227 case DW_TAG_namespace:
f55ee35c 10228 case DW_TAG_module:
fae299cd
DC
10229 /* FIXME: carlton/2004-01-16: Should we do this for
10230 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10231 that current GCC's always emit the DIEs corresponding
10232 to definitions of methods of classes as children of a
10233 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10234 the DIEs giving the declarations, which could be
10235 anywhere). But I don't see any reason why the
10236 standards says that they have to be there. */
10237 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10238
10239 if (current_low != ((CORE_ADDR) -1))
10240 {
10241 best_low = min (best_low, current_low);
10242 best_high = max (best_high, current_high);
10243 }
10244 break;
10245 default:
0963b4bd 10246 /* Ignore. */
fae299cd
DC
10247 break;
10248 }
10249
10250 child = sibling_die (child);
10251 }
10252 }
10253
10254 *lowpc = best_low;
10255 *highpc = best_high;
10256}
10257
801e3a5b
JB
10258/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10259 in DIE. */
380bca97 10260
801e3a5b
JB
10261static void
10262dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10263 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10264{
bb5ed363 10265 struct objfile *objfile = cu->objfile;
801e3a5b 10266 struct attribute *attr;
91da1414 10267 struct attribute *attr_high;
801e3a5b 10268
91da1414
MW
10269 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10270 if (attr_high)
801e3a5b 10271 {
801e3a5b
JB
10272 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10273 if (attr)
10274 {
10275 CORE_ADDR low = DW_ADDR (attr);
91da1414 10276 CORE_ADDR high;
3019eac3
DE
10277 if (attr_high->form == DW_FORM_addr
10278 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10279 high = DW_ADDR (attr_high);
10280 else
10281 high = low + DW_UNSND (attr_high);
9a619af0 10282
801e3a5b
JB
10283 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10284 }
10285 }
10286
10287 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10288 if (attr)
10289 {
bb5ed363 10290 bfd *obfd = objfile->obfd;
ab435259
DE
10291 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10292 We take advantage of the fact that DW_AT_ranges does not appear
10293 in DW_TAG_compile_unit of DWO files. */
10294 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10295
10296 /* The value of the DW_AT_ranges attribute is the offset of the
10297 address range list in the .debug_ranges section. */
ab435259
DE
10298 unsigned long offset = (DW_UNSND (attr)
10299 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10300 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10301
10302 /* For some target architectures, but not others, the
10303 read_address function sign-extends the addresses it returns.
10304 To recognize base address selection entries, we need a
10305 mask. */
10306 unsigned int addr_size = cu->header.addr_size;
10307 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10308
10309 /* The base address, to which the next pair is relative. Note
10310 that this 'base' is a DWARF concept: most entries in a range
10311 list are relative, to reduce the number of relocs against the
10312 debugging information. This is separate from this function's
10313 'baseaddr' argument, which GDB uses to relocate debugging
10314 information from a shared library based on the address at
10315 which the library was loaded. */
d00adf39
DE
10316 CORE_ADDR base = cu->base_address;
10317 int base_known = cu->base_known;
801e3a5b 10318
be391dca 10319 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10320 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10321 {
10322 complaint (&symfile_complaints,
10323 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10324 offset);
10325 return;
10326 }
10327
10328 for (;;)
10329 {
10330 unsigned int bytes_read;
10331 CORE_ADDR start, end;
10332
10333 start = read_address (obfd, buffer, cu, &bytes_read);
10334 buffer += bytes_read;
10335 end = read_address (obfd, buffer, cu, &bytes_read);
10336 buffer += bytes_read;
10337
10338 /* Did we find the end of the range list? */
10339 if (start == 0 && end == 0)
10340 break;
10341
10342 /* Did we find a base address selection entry? */
10343 else if ((start & base_select_mask) == base_select_mask)
10344 {
10345 base = end;
10346 base_known = 1;
10347 }
10348
10349 /* We found an ordinary address range. */
10350 else
10351 {
10352 if (!base_known)
10353 {
10354 complaint (&symfile_complaints,
3e43a32a
MS
10355 _("Invalid .debug_ranges data "
10356 "(no base address)"));
801e3a5b
JB
10357 return;
10358 }
10359
9277c30c
UW
10360 if (start > end)
10361 {
10362 /* Inverted range entries are invalid. */
10363 complaint (&symfile_complaints,
10364 _("Invalid .debug_ranges data "
10365 "(inverted range)"));
10366 return;
10367 }
10368
10369 /* Empty range entries have no effect. */
10370 if (start == end)
10371 continue;
10372
01093045
DE
10373 start += base + baseaddr;
10374 end += base + baseaddr;
10375
10376 /* A not-uncommon case of bad debug info.
10377 Don't pollute the addrmap with bad data. */
10378 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10379 {
10380 complaint (&symfile_complaints,
10381 _(".debug_ranges entry has start address of zero"
10382 " [in module %s]"), objfile->name);
10383 continue;
10384 }
10385
10386 record_block_range (block, start, end - 1);
801e3a5b
JB
10387 }
10388 }
10389 }
10390}
10391
685b1105
JK
10392/* Check whether the producer field indicates either of GCC < 4.6, or the
10393 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10394
685b1105
JK
10395static void
10396check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10397{
10398 const char *cs;
10399 int major, minor, release;
10400
10401 if (cu->producer == NULL)
10402 {
10403 /* For unknown compilers expect their behavior is DWARF version
10404 compliant.
10405
10406 GCC started to support .debug_types sections by -gdwarf-4 since
10407 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10408 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10409 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10410 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10411 }
685b1105 10412 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10413 {
685b1105
JK
10414 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10415
ba919b58
TT
10416 cs = &cu->producer[strlen ("GNU ")];
10417 while (*cs && !isdigit (*cs))
10418 cs++;
10419 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10420 {
10421 /* Not recognized as GCC. */
10422 }
10423 else
1b80a9fa
JK
10424 {
10425 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10426 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10427 }
685b1105
JK
10428 }
10429 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10430 cu->producer_is_icc = 1;
10431 else
10432 {
10433 /* For other non-GCC compilers, expect their behavior is DWARF version
10434 compliant. */
60d5a603
JK
10435 }
10436
ba919b58 10437 cu->checked_producer = 1;
685b1105 10438}
ba919b58 10439
685b1105
JK
10440/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10441 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10442 during 4.6.0 experimental. */
10443
10444static int
10445producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10446{
10447 if (!cu->checked_producer)
10448 check_producer (cu);
10449
10450 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10451}
10452
10453/* Return the default accessibility type if it is not overriden by
10454 DW_AT_accessibility. */
10455
10456static enum dwarf_access_attribute
10457dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10458{
10459 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10460 {
10461 /* The default DWARF 2 accessibility for members is public, the default
10462 accessibility for inheritance is private. */
10463
10464 if (die->tag != DW_TAG_inheritance)
10465 return DW_ACCESS_public;
10466 else
10467 return DW_ACCESS_private;
10468 }
10469 else
10470 {
10471 /* DWARF 3+ defines the default accessibility a different way. The same
10472 rules apply now for DW_TAG_inheritance as for the members and it only
10473 depends on the container kind. */
10474
10475 if (die->parent->tag == DW_TAG_class_type)
10476 return DW_ACCESS_private;
10477 else
10478 return DW_ACCESS_public;
10479 }
10480}
10481
74ac6d43
TT
10482/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10483 offset. If the attribute was not found return 0, otherwise return
10484 1. If it was found but could not properly be handled, set *OFFSET
10485 to 0. */
10486
10487static int
10488handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10489 LONGEST *offset)
10490{
10491 struct attribute *attr;
10492
10493 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10494 if (attr != NULL)
10495 {
10496 *offset = 0;
10497
10498 /* Note that we do not check for a section offset first here.
10499 This is because DW_AT_data_member_location is new in DWARF 4,
10500 so if we see it, we can assume that a constant form is really
10501 a constant and not a section offset. */
10502 if (attr_form_is_constant (attr))
10503 *offset = dwarf2_get_attr_constant_value (attr, 0);
10504 else if (attr_form_is_section_offset (attr))
10505 dwarf2_complex_location_expr_complaint ();
10506 else if (attr_form_is_block (attr))
10507 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10508 else
10509 dwarf2_complex_location_expr_complaint ();
10510
10511 return 1;
10512 }
10513
10514 return 0;
10515}
10516
c906108c
SS
10517/* Add an aggregate field to the field list. */
10518
10519static void
107d2387 10520dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10521 struct dwarf2_cu *cu)
6e70227d 10522{
e7c27a73 10523 struct objfile *objfile = cu->objfile;
5e2b427d 10524 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10525 struct nextfield *new_field;
10526 struct attribute *attr;
10527 struct field *fp;
15d034d0 10528 const char *fieldname = "";
c906108c
SS
10529
10530 /* Allocate a new field list entry and link it in. */
10531 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10532 make_cleanup (xfree, new_field);
c906108c 10533 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10534
10535 if (die->tag == DW_TAG_inheritance)
10536 {
10537 new_field->next = fip->baseclasses;
10538 fip->baseclasses = new_field;
10539 }
10540 else
10541 {
10542 new_field->next = fip->fields;
10543 fip->fields = new_field;
10544 }
c906108c
SS
10545 fip->nfields++;
10546
e142c38c 10547 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10548 if (attr)
10549 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10550 else
10551 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10552 if (new_field->accessibility != DW_ACCESS_public)
10553 fip->non_public_fields = 1;
60d5a603 10554
e142c38c 10555 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10556 if (attr)
10557 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10558 else
10559 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10560
10561 fp = &new_field->field;
a9a9bd0f 10562
e142c38c 10563 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10564 {
74ac6d43
TT
10565 LONGEST offset;
10566
a9a9bd0f 10567 /* Data member other than a C++ static data member. */
6e70227d 10568
c906108c 10569 /* Get type of field. */
e7c27a73 10570 fp->type = die_type (die, cu);
c906108c 10571
d6a843b5 10572 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10573
c906108c 10574 /* Get bit size of field (zero if none). */
e142c38c 10575 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10576 if (attr)
10577 {
10578 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10579 }
10580 else
10581 {
10582 FIELD_BITSIZE (*fp) = 0;
10583 }
10584
10585 /* Get bit offset of field. */
74ac6d43
TT
10586 if (handle_data_member_location (die, cu, &offset))
10587 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10588 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10589 if (attr)
10590 {
5e2b427d 10591 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10592 {
10593 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10594 additional bit offset from the MSB of the containing
10595 anonymous object to the MSB of the field. We don't
10596 have to do anything special since we don't need to
10597 know the size of the anonymous object. */
f41f5e61 10598 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10599 }
10600 else
10601 {
10602 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10603 MSB of the anonymous object, subtract off the number of
10604 bits from the MSB of the field to the MSB of the
10605 object, and then subtract off the number of bits of
10606 the field itself. The result is the bit offset of
10607 the LSB of the field. */
c906108c
SS
10608 int anonymous_size;
10609 int bit_offset = DW_UNSND (attr);
10610
e142c38c 10611 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10612 if (attr)
10613 {
10614 /* The size of the anonymous object containing
10615 the bit field is explicit, so use the
10616 indicated size (in bytes). */
10617 anonymous_size = DW_UNSND (attr);
10618 }
10619 else
10620 {
10621 /* The size of the anonymous object containing
10622 the bit field must be inferred from the type
10623 attribute of the data member containing the
10624 bit field. */
10625 anonymous_size = TYPE_LENGTH (fp->type);
10626 }
f41f5e61
PA
10627 SET_FIELD_BITPOS (*fp,
10628 (FIELD_BITPOS (*fp)
10629 + anonymous_size * bits_per_byte
10630 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10631 }
10632 }
10633
10634 /* Get name of field. */
39cbfefa
DJ
10635 fieldname = dwarf2_name (die, cu);
10636 if (fieldname == NULL)
10637 fieldname = "";
d8151005
DJ
10638
10639 /* The name is already allocated along with this objfile, so we don't
10640 need to duplicate it for the type. */
10641 fp->name = fieldname;
c906108c
SS
10642
10643 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10644 pointer or virtual base class pointer) to private. */
e142c38c 10645 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10646 {
d48cc9dd 10647 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10648 new_field->accessibility = DW_ACCESS_private;
10649 fip->non_public_fields = 1;
10650 }
10651 }
a9a9bd0f 10652 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10653 {
a9a9bd0f
DC
10654 /* C++ static member. */
10655
10656 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10657 is a declaration, but all versions of G++ as of this writing
10658 (so through at least 3.2.1) incorrectly generate
10659 DW_TAG_variable tags. */
6e70227d 10660
ff355380 10661 const char *physname;
c906108c 10662
a9a9bd0f 10663 /* Get name of field. */
39cbfefa
DJ
10664 fieldname = dwarf2_name (die, cu);
10665 if (fieldname == NULL)
c906108c
SS
10666 return;
10667
254e6b9e 10668 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10669 if (attr
10670 /* Only create a symbol if this is an external value.
10671 new_symbol checks this and puts the value in the global symbol
10672 table, which we want. If it is not external, new_symbol
10673 will try to put the value in cu->list_in_scope which is wrong. */
10674 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10675 {
10676 /* A static const member, not much different than an enum as far as
10677 we're concerned, except that we can support more types. */
10678 new_symbol (die, NULL, cu);
10679 }
10680
2df3850c 10681 /* Get physical name. */
ff355380 10682 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10683
d8151005
DJ
10684 /* The name is already allocated along with this objfile, so we don't
10685 need to duplicate it for the type. */
10686 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10687 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10688 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10689 }
10690 else if (die->tag == DW_TAG_inheritance)
10691 {
74ac6d43 10692 LONGEST offset;
d4b96c9a 10693
74ac6d43
TT
10694 /* C++ base class field. */
10695 if (handle_data_member_location (die, cu, &offset))
10696 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10697 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10698 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10699 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10700 fip->nbaseclasses++;
10701 }
10702}
10703
98751a41
JK
10704/* Add a typedef defined in the scope of the FIP's class. */
10705
10706static void
10707dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10708 struct dwarf2_cu *cu)
6e70227d 10709{
98751a41 10710 struct objfile *objfile = cu->objfile;
98751a41
JK
10711 struct typedef_field_list *new_field;
10712 struct attribute *attr;
10713 struct typedef_field *fp;
10714 char *fieldname = "";
10715
10716 /* Allocate a new field list entry and link it in. */
10717 new_field = xzalloc (sizeof (*new_field));
10718 make_cleanup (xfree, new_field);
10719
10720 gdb_assert (die->tag == DW_TAG_typedef);
10721
10722 fp = &new_field->field;
10723
10724 /* Get name of field. */
10725 fp->name = dwarf2_name (die, cu);
10726 if (fp->name == NULL)
10727 return;
10728
10729 fp->type = read_type_die (die, cu);
10730
10731 new_field->next = fip->typedef_field_list;
10732 fip->typedef_field_list = new_field;
10733 fip->typedef_field_list_count++;
10734}
10735
c906108c
SS
10736/* Create the vector of fields, and attach it to the type. */
10737
10738static void
fba45db2 10739dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10740 struct dwarf2_cu *cu)
c906108c
SS
10741{
10742 int nfields = fip->nfields;
10743
10744 /* Record the field count, allocate space for the array of fields,
10745 and create blank accessibility bitfields if necessary. */
10746 TYPE_NFIELDS (type) = nfields;
10747 TYPE_FIELDS (type) = (struct field *)
10748 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10749 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10750
b4ba55a1 10751 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10752 {
10753 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10754
10755 TYPE_FIELD_PRIVATE_BITS (type) =
10756 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10757 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10758
10759 TYPE_FIELD_PROTECTED_BITS (type) =
10760 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10761 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10762
774b6a14
TT
10763 TYPE_FIELD_IGNORE_BITS (type) =
10764 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10765 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10766 }
10767
10768 /* If the type has baseclasses, allocate and clear a bit vector for
10769 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10770 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10771 {
10772 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10773 unsigned char *pointer;
c906108c
SS
10774
10775 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10776 pointer = TYPE_ALLOC (type, num_bytes);
10777 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10778 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10779 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10780 }
10781
3e43a32a
MS
10782 /* Copy the saved-up fields into the field vector. Start from the head of
10783 the list, adding to the tail of the field array, so that they end up in
10784 the same order in the array in which they were added to the list. */
c906108c
SS
10785 while (nfields-- > 0)
10786 {
7d0ccb61
DJ
10787 struct nextfield *fieldp;
10788
10789 if (fip->fields)
10790 {
10791 fieldp = fip->fields;
10792 fip->fields = fieldp->next;
10793 }
10794 else
10795 {
10796 fieldp = fip->baseclasses;
10797 fip->baseclasses = fieldp->next;
10798 }
10799
10800 TYPE_FIELD (type, nfields) = fieldp->field;
10801 switch (fieldp->accessibility)
c906108c 10802 {
c5aa993b 10803 case DW_ACCESS_private:
b4ba55a1
JB
10804 if (cu->language != language_ada)
10805 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10806 break;
c906108c 10807
c5aa993b 10808 case DW_ACCESS_protected:
b4ba55a1
JB
10809 if (cu->language != language_ada)
10810 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10811 break;
c906108c 10812
c5aa993b
JM
10813 case DW_ACCESS_public:
10814 break;
c906108c 10815
c5aa993b
JM
10816 default:
10817 /* Unknown accessibility. Complain and treat it as public. */
10818 {
e2e0b3e5 10819 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10820 fieldp->accessibility);
c5aa993b
JM
10821 }
10822 break;
c906108c
SS
10823 }
10824 if (nfields < fip->nbaseclasses)
10825 {
7d0ccb61 10826 switch (fieldp->virtuality)
c906108c 10827 {
c5aa993b
JM
10828 case DW_VIRTUALITY_virtual:
10829 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10830 if (cu->language == language_ada)
a73c6dcd 10831 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10832 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10833 break;
c906108c
SS
10834 }
10835 }
c906108c
SS
10836 }
10837}
10838
7d27a96d
TT
10839/* Return true if this member function is a constructor, false
10840 otherwise. */
10841
10842static int
10843dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10844{
10845 const char *fieldname;
10846 const char *typename;
10847 int len;
10848
10849 if (die->parent == NULL)
10850 return 0;
10851
10852 if (die->parent->tag != DW_TAG_structure_type
10853 && die->parent->tag != DW_TAG_union_type
10854 && die->parent->tag != DW_TAG_class_type)
10855 return 0;
10856
10857 fieldname = dwarf2_name (die, cu);
10858 typename = dwarf2_name (die->parent, cu);
10859 if (fieldname == NULL || typename == NULL)
10860 return 0;
10861
10862 len = strlen (fieldname);
10863 return (strncmp (fieldname, typename, len) == 0
10864 && (typename[len] == '\0' || typename[len] == '<'));
10865}
10866
c906108c
SS
10867/* Add a member function to the proper fieldlist. */
10868
10869static void
107d2387 10870dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10871 struct type *type, struct dwarf2_cu *cu)
c906108c 10872{
e7c27a73 10873 struct objfile *objfile = cu->objfile;
c906108c
SS
10874 struct attribute *attr;
10875 struct fnfieldlist *flp;
10876 int i;
10877 struct fn_field *fnp;
15d034d0 10878 const char *fieldname;
c906108c 10879 struct nextfnfield *new_fnfield;
f792889a 10880 struct type *this_type;
60d5a603 10881 enum dwarf_access_attribute accessibility;
c906108c 10882
b4ba55a1 10883 if (cu->language == language_ada)
a73c6dcd 10884 error (_("unexpected member function in Ada type"));
b4ba55a1 10885
2df3850c 10886 /* Get name of member function. */
39cbfefa
DJ
10887 fieldname = dwarf2_name (die, cu);
10888 if (fieldname == NULL)
2df3850c 10889 return;
c906108c 10890
c906108c
SS
10891 /* Look up member function name in fieldlist. */
10892 for (i = 0; i < fip->nfnfields; i++)
10893 {
27bfe10e 10894 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10895 break;
10896 }
10897
10898 /* Create new list element if necessary. */
10899 if (i < fip->nfnfields)
10900 flp = &fip->fnfieldlists[i];
10901 else
10902 {
10903 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10904 {
10905 fip->fnfieldlists = (struct fnfieldlist *)
10906 xrealloc (fip->fnfieldlists,
10907 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10908 * sizeof (struct fnfieldlist));
c906108c 10909 if (fip->nfnfields == 0)
c13c43fd 10910 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10911 }
10912 flp = &fip->fnfieldlists[fip->nfnfields];
10913 flp->name = fieldname;
10914 flp->length = 0;
10915 flp->head = NULL;
3da10d80 10916 i = fip->nfnfields++;
c906108c
SS
10917 }
10918
10919 /* Create a new member function field and chain it to the field list
0963b4bd 10920 entry. */
c906108c 10921 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10922 make_cleanup (xfree, new_fnfield);
c906108c
SS
10923 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10924 new_fnfield->next = flp->head;
10925 flp->head = new_fnfield;
10926 flp->length++;
10927
10928 /* Fill in the member function field info. */
10929 fnp = &new_fnfield->fnfield;
3da10d80
KS
10930
10931 /* Delay processing of the physname until later. */
10932 if (cu->language == language_cplus || cu->language == language_java)
10933 {
10934 add_to_method_list (type, i, flp->length - 1, fieldname,
10935 die, cu);
10936 }
10937 else
10938 {
1d06ead6 10939 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10940 fnp->physname = physname ? physname : "";
10941 }
10942
c906108c 10943 fnp->type = alloc_type (objfile);
f792889a
DJ
10944 this_type = read_type_die (die, cu);
10945 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10946 {
f792889a 10947 int nparams = TYPE_NFIELDS (this_type);
c906108c 10948
f792889a 10949 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10950 of the method itself (TYPE_CODE_METHOD). */
10951 smash_to_method_type (fnp->type, type,
f792889a
DJ
10952 TYPE_TARGET_TYPE (this_type),
10953 TYPE_FIELDS (this_type),
10954 TYPE_NFIELDS (this_type),
10955 TYPE_VARARGS (this_type));
c906108c
SS
10956
10957 /* Handle static member functions.
c5aa993b 10958 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10959 member functions. G++ helps GDB by marking the first
10960 parameter for non-static member functions (which is the this
10961 pointer) as artificial. We obtain this information from
10962 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10963 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10964 fnp->voffset = VOFFSET_STATIC;
10965 }
10966 else
e2e0b3e5 10967 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10968 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10969
10970 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10971 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10972 fnp->fcontext = die_containing_type (die, cu);
c906108c 10973
3e43a32a
MS
10974 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10975 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10976
10977 /* Get accessibility. */
e142c38c 10978 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10979 if (attr)
60d5a603
JK
10980 accessibility = DW_UNSND (attr);
10981 else
10982 accessibility = dwarf2_default_access_attribute (die, cu);
10983 switch (accessibility)
c906108c 10984 {
60d5a603
JK
10985 case DW_ACCESS_private:
10986 fnp->is_private = 1;
10987 break;
10988 case DW_ACCESS_protected:
10989 fnp->is_protected = 1;
10990 break;
c906108c
SS
10991 }
10992
b02dede2 10993 /* Check for artificial methods. */
e142c38c 10994 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10995 if (attr && DW_UNSND (attr) != 0)
10996 fnp->is_artificial = 1;
10997
7d27a96d
TT
10998 fnp->is_constructor = dwarf2_is_constructor (die, cu);
10999
0d564a31 11000 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11001 function. For older versions of GCC, this is an offset in the
11002 appropriate virtual table, as specified by DW_AT_containing_type.
11003 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11004 to the object address. */
11005
e142c38c 11006 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11007 if (attr)
8e19ed76 11008 {
aec5aa8b 11009 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11010 {
aec5aa8b
TT
11011 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11012 {
11013 /* Old-style GCC. */
11014 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11015 }
11016 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11017 || (DW_BLOCK (attr)->size > 1
11018 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11019 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11020 {
11021 struct dwarf_block blk;
11022 int offset;
11023
11024 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11025 ? 1 : 2);
11026 blk.size = DW_BLOCK (attr)->size - offset;
11027 blk.data = DW_BLOCK (attr)->data + offset;
11028 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11029 if ((fnp->voffset % cu->header.addr_size) != 0)
11030 dwarf2_complex_location_expr_complaint ();
11031 else
11032 fnp->voffset /= cu->header.addr_size;
11033 fnp->voffset += 2;
11034 }
11035 else
11036 dwarf2_complex_location_expr_complaint ();
11037
11038 if (!fnp->fcontext)
11039 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11040 }
3690dd37 11041 else if (attr_form_is_section_offset (attr))
8e19ed76 11042 {
4d3c2250 11043 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11044 }
11045 else
11046 {
4d3c2250
KB
11047 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11048 fieldname);
8e19ed76 11049 }
0d564a31 11050 }
d48cc9dd
DJ
11051 else
11052 {
11053 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11054 if (attr && DW_UNSND (attr))
11055 {
11056 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11057 complaint (&symfile_complaints,
3e43a32a
MS
11058 _("Member function \"%s\" (offset %d) is virtual "
11059 "but the vtable offset is not specified"),
b64f50a1 11060 fieldname, die->offset.sect_off);
9655fd1a 11061 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11062 TYPE_CPLUS_DYNAMIC (type) = 1;
11063 }
11064 }
c906108c
SS
11065}
11066
11067/* Create the vector of member function fields, and attach it to the type. */
11068
11069static void
fba45db2 11070dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11071 struct dwarf2_cu *cu)
c906108c
SS
11072{
11073 struct fnfieldlist *flp;
c906108c
SS
11074 int i;
11075
b4ba55a1 11076 if (cu->language == language_ada)
a73c6dcd 11077 error (_("unexpected member functions in Ada type"));
b4ba55a1 11078
c906108c
SS
11079 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11080 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11081 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11082
11083 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11084 {
11085 struct nextfnfield *nfp = flp->head;
11086 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11087 int k;
11088
11089 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11090 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11091 fn_flp->fn_fields = (struct fn_field *)
11092 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11093 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11094 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11095 }
11096
11097 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11098}
11099
1168df01
JB
11100/* Returns non-zero if NAME is the name of a vtable member in CU's
11101 language, zero otherwise. */
11102static int
11103is_vtable_name (const char *name, struct dwarf2_cu *cu)
11104{
11105 static const char vptr[] = "_vptr";
987504bb 11106 static const char vtable[] = "vtable";
1168df01 11107
987504bb
JJ
11108 /* Look for the C++ and Java forms of the vtable. */
11109 if ((cu->language == language_java
11110 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11111 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11112 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11113 return 1;
11114
11115 return 0;
11116}
11117
c0dd20ea 11118/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11119 functions, with the ABI-specified layout. If TYPE describes
11120 such a structure, smash it into a member function type.
61049d3b
DJ
11121
11122 GCC shouldn't do this; it should just output pointer to member DIEs.
11123 This is GCC PR debug/28767. */
c0dd20ea 11124
0b92b5bb
TT
11125static void
11126quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11127{
0b92b5bb 11128 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11129
11130 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11131 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11132 return;
c0dd20ea
DJ
11133
11134 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11135 if (TYPE_FIELD_NAME (type, 0) == NULL
11136 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11137 || TYPE_FIELD_NAME (type, 1) == NULL
11138 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11139 return;
c0dd20ea
DJ
11140
11141 /* Find the type of the method. */
0b92b5bb 11142 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11143 if (pfn_type == NULL
11144 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11145 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11146 return;
c0dd20ea
DJ
11147
11148 /* Look for the "this" argument. */
11149 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11150 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11151 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11152 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11153 return;
c0dd20ea
DJ
11154
11155 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11156 new_type = alloc_type (objfile);
11157 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11158 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11159 TYPE_VARARGS (pfn_type));
0b92b5bb 11160 smash_to_methodptr_type (type, new_type);
c0dd20ea 11161}
1168df01 11162
685b1105
JK
11163/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11164 (icc). */
11165
11166static int
11167producer_is_icc (struct dwarf2_cu *cu)
11168{
11169 if (!cu->checked_producer)
11170 check_producer (cu);
11171
11172 return cu->producer_is_icc;
11173}
11174
c906108c 11175/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11176 (definition) to create a type for the structure or union. Fill in
11177 the type's name and general properties; the members will not be
11178 processed until process_structure_type.
c906108c 11179
c767944b
DJ
11180 NOTE: we need to call these functions regardless of whether or not the
11181 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11182 structure or union. This gets the type entered into our set of
11183 user defined types.
11184
11185 However, if the structure is incomplete (an opaque struct/union)
11186 then suppress creating a symbol table entry for it since gdb only
11187 wants to find the one with the complete definition. Note that if
11188 it is complete, we just call new_symbol, which does it's own
11189 checking about whether the struct/union is anonymous or not (and
11190 suppresses creating a symbol table entry itself). */
11191
f792889a 11192static struct type *
134d01f1 11193read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11194{
e7c27a73 11195 struct objfile *objfile = cu->objfile;
c906108c
SS
11196 struct type *type;
11197 struct attribute *attr;
15d034d0 11198 const char *name;
c906108c 11199
348e048f
DE
11200 /* If the definition of this type lives in .debug_types, read that type.
11201 Don't follow DW_AT_specification though, that will take us back up
11202 the chain and we want to go down. */
45e58e77 11203 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11204 if (attr)
11205 {
11206 struct dwarf2_cu *type_cu = cu;
11207 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11208
348e048f
DE
11209 /* We could just recurse on read_structure_type, but we need to call
11210 get_die_type to ensure only one type for this DIE is created.
11211 This is important, for example, because for c++ classes we need
11212 TYPE_NAME set which is only done by new_symbol. Blech. */
11213 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11214
11215 /* TYPE_CU may not be the same as CU.
11216 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11217 return set_die_type (die, type, cu);
11218 }
11219
c0dd20ea 11220 type = alloc_type (objfile);
c906108c 11221 INIT_CPLUS_SPECIFIC (type);
93311388 11222
39cbfefa
DJ
11223 name = dwarf2_name (die, cu);
11224 if (name != NULL)
c906108c 11225 {
987504bb
JJ
11226 if (cu->language == language_cplus
11227 || cu->language == language_java)
63d06c5c 11228 {
15d034d0 11229 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11230
11231 /* dwarf2_full_name might have already finished building the DIE's
11232 type. If so, there is no need to continue. */
11233 if (get_die_type (die, cu) != NULL)
11234 return get_die_type (die, cu);
11235
11236 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11237 if (die->tag == DW_TAG_structure_type
11238 || die->tag == DW_TAG_class_type)
11239 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11240 }
11241 else
11242 {
d8151005
DJ
11243 /* The name is already allocated along with this objfile, so
11244 we don't need to duplicate it for the type. */
94af9270
KS
11245 TYPE_TAG_NAME (type) = (char *) name;
11246 if (die->tag == DW_TAG_class_type)
11247 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11248 }
c906108c
SS
11249 }
11250
11251 if (die->tag == DW_TAG_structure_type)
11252 {
11253 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11254 }
11255 else if (die->tag == DW_TAG_union_type)
11256 {
11257 TYPE_CODE (type) = TYPE_CODE_UNION;
11258 }
11259 else
11260 {
c906108c
SS
11261 TYPE_CODE (type) = TYPE_CODE_CLASS;
11262 }
11263
0cc2414c
TT
11264 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11265 TYPE_DECLARED_CLASS (type) = 1;
11266
e142c38c 11267 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11268 if (attr)
11269 {
11270 TYPE_LENGTH (type) = DW_UNSND (attr);
11271 }
11272 else
11273 {
11274 TYPE_LENGTH (type) = 0;
11275 }
11276
685b1105
JK
11277 if (producer_is_icc (cu))
11278 {
11279 /* ICC does not output the required DW_AT_declaration
11280 on incomplete types, but gives them a size of zero. */
11281 }
11282 else
11283 TYPE_STUB_SUPPORTED (type) = 1;
11284
dc718098 11285 if (die_is_declaration (die, cu))
876cecd0 11286 TYPE_STUB (type) = 1;
a6c727b2
DJ
11287 else if (attr == NULL && die->child == NULL
11288 && producer_is_realview (cu->producer))
11289 /* RealView does not output the required DW_AT_declaration
11290 on incomplete types. */
11291 TYPE_STUB (type) = 1;
dc718098 11292
c906108c
SS
11293 /* We need to add the type field to the die immediately so we don't
11294 infinitely recurse when dealing with pointers to the structure
0963b4bd 11295 type within the structure itself. */
1c379e20 11296 set_die_type (die, type, cu);
c906108c 11297
7e314c57
JK
11298 /* set_die_type should be already done. */
11299 set_descriptive_type (type, die, cu);
11300
c767944b
DJ
11301 return type;
11302}
11303
11304/* Finish creating a structure or union type, including filling in
11305 its members and creating a symbol for it. */
11306
11307static void
11308process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11309{
11310 struct objfile *objfile = cu->objfile;
11311 struct die_info *child_die = die->child;
11312 struct type *type;
11313
11314 type = get_die_type (die, cu);
11315 if (type == NULL)
11316 type = read_structure_type (die, cu);
11317
e142c38c 11318 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11319 {
11320 struct field_info fi;
11321 struct die_info *child_die;
34eaf542 11322 VEC (symbolp) *template_args = NULL;
c767944b 11323 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11324
11325 memset (&fi, 0, sizeof (struct field_info));
11326
639d11d3 11327 child_die = die->child;
c906108c
SS
11328
11329 while (child_die && child_die->tag)
11330 {
a9a9bd0f
DC
11331 if (child_die->tag == DW_TAG_member
11332 || child_die->tag == DW_TAG_variable)
c906108c 11333 {
a9a9bd0f
DC
11334 /* NOTE: carlton/2002-11-05: A C++ static data member
11335 should be a DW_TAG_member that is a declaration, but
11336 all versions of G++ as of this writing (so through at
11337 least 3.2.1) incorrectly generate DW_TAG_variable
11338 tags for them instead. */
e7c27a73 11339 dwarf2_add_field (&fi, child_die, cu);
c906108c 11340 }
8713b1b1 11341 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11342 {
0963b4bd 11343 /* C++ member function. */
e7c27a73 11344 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11345 }
11346 else if (child_die->tag == DW_TAG_inheritance)
11347 {
11348 /* C++ base class field. */
e7c27a73 11349 dwarf2_add_field (&fi, child_die, cu);
c906108c 11350 }
98751a41
JK
11351 else if (child_die->tag == DW_TAG_typedef)
11352 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11353 else if (child_die->tag == DW_TAG_template_type_param
11354 || child_die->tag == DW_TAG_template_value_param)
11355 {
11356 struct symbol *arg = new_symbol (child_die, NULL, cu);
11357
f1078f66
DJ
11358 if (arg != NULL)
11359 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11360 }
11361
c906108c
SS
11362 child_die = sibling_die (child_die);
11363 }
11364
34eaf542
TT
11365 /* Attach template arguments to type. */
11366 if (! VEC_empty (symbolp, template_args))
11367 {
11368 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11369 TYPE_N_TEMPLATE_ARGUMENTS (type)
11370 = VEC_length (symbolp, template_args);
11371 TYPE_TEMPLATE_ARGUMENTS (type)
11372 = obstack_alloc (&objfile->objfile_obstack,
11373 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11374 * sizeof (struct symbol *)));
11375 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11376 VEC_address (symbolp, template_args),
11377 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11378 * sizeof (struct symbol *)));
11379 VEC_free (symbolp, template_args);
11380 }
11381
c906108c
SS
11382 /* Attach fields and member functions to the type. */
11383 if (fi.nfields)
e7c27a73 11384 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11385 if (fi.nfnfields)
11386 {
e7c27a73 11387 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11388
c5aa993b 11389 /* Get the type which refers to the base class (possibly this
c906108c 11390 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11391 class from the DW_AT_containing_type attribute. This use of
11392 DW_AT_containing_type is a GNU extension. */
c906108c 11393
e142c38c 11394 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11395 {
e7c27a73 11396 struct type *t = die_containing_type (die, cu);
c906108c
SS
11397
11398 TYPE_VPTR_BASETYPE (type) = t;
11399 if (type == t)
11400 {
c906108c
SS
11401 int i;
11402
11403 /* Our own class provides vtbl ptr. */
11404 for (i = TYPE_NFIELDS (t) - 1;
11405 i >= TYPE_N_BASECLASSES (t);
11406 --i)
11407 {
0d5cff50 11408 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11409
1168df01 11410 if (is_vtable_name (fieldname, cu))
c906108c
SS
11411 {
11412 TYPE_VPTR_FIELDNO (type) = i;
11413 break;
11414 }
11415 }
11416
11417 /* Complain if virtual function table field not found. */
11418 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11419 complaint (&symfile_complaints,
3e43a32a
MS
11420 _("virtual function table pointer "
11421 "not found when defining class '%s'"),
4d3c2250
KB
11422 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11423 "");
c906108c
SS
11424 }
11425 else
11426 {
11427 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11428 }
11429 }
f6235d4c
EZ
11430 else if (cu->producer
11431 && strncmp (cu->producer,
11432 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11433 {
11434 /* The IBM XLC compiler does not provide direct indication
11435 of the containing type, but the vtable pointer is
11436 always named __vfp. */
11437
11438 int i;
11439
11440 for (i = TYPE_NFIELDS (type) - 1;
11441 i >= TYPE_N_BASECLASSES (type);
11442 --i)
11443 {
11444 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11445 {
11446 TYPE_VPTR_FIELDNO (type) = i;
11447 TYPE_VPTR_BASETYPE (type) = type;
11448 break;
11449 }
11450 }
11451 }
c906108c 11452 }
98751a41
JK
11453
11454 /* Copy fi.typedef_field_list linked list elements content into the
11455 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11456 if (fi.typedef_field_list)
11457 {
11458 int i = fi.typedef_field_list_count;
11459
a0d7a4ff 11460 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11461 TYPE_TYPEDEF_FIELD_ARRAY (type)
11462 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11463 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11464
11465 /* Reverse the list order to keep the debug info elements order. */
11466 while (--i >= 0)
11467 {
11468 struct typedef_field *dest, *src;
6e70227d 11469
98751a41
JK
11470 dest = &TYPE_TYPEDEF_FIELD (type, i);
11471 src = &fi.typedef_field_list->field;
11472 fi.typedef_field_list = fi.typedef_field_list->next;
11473 *dest = *src;
11474 }
11475 }
c767944b
DJ
11476
11477 do_cleanups (back_to);
eb2a6f42
TT
11478
11479 if (HAVE_CPLUS_STRUCT (type))
11480 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11481 }
63d06c5c 11482
bb5ed363 11483 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11484
90aeadfc
DC
11485 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11486 snapshots) has been known to create a die giving a declaration
11487 for a class that has, as a child, a die giving a definition for a
11488 nested class. So we have to process our children even if the
11489 current die is a declaration. Normally, of course, a declaration
11490 won't have any children at all. */
134d01f1 11491
90aeadfc
DC
11492 while (child_die != NULL && child_die->tag)
11493 {
11494 if (child_die->tag == DW_TAG_member
11495 || child_die->tag == DW_TAG_variable
34eaf542
TT
11496 || child_die->tag == DW_TAG_inheritance
11497 || child_die->tag == DW_TAG_template_value_param
11498 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11499 {
90aeadfc 11500 /* Do nothing. */
134d01f1 11501 }
90aeadfc
DC
11502 else
11503 process_die (child_die, cu);
134d01f1 11504
90aeadfc 11505 child_die = sibling_die (child_die);
134d01f1
DJ
11506 }
11507
fa4028e9
JB
11508 /* Do not consider external references. According to the DWARF standard,
11509 these DIEs are identified by the fact that they have no byte_size
11510 attribute, and a declaration attribute. */
11511 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11512 || !die_is_declaration (die, cu))
c767944b 11513 new_symbol (die, type, cu);
134d01f1
DJ
11514}
11515
11516/* Given a DW_AT_enumeration_type die, set its type. We do not
11517 complete the type's fields yet, or create any symbols. */
c906108c 11518
f792889a 11519static struct type *
134d01f1 11520read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11521{
e7c27a73 11522 struct objfile *objfile = cu->objfile;
c906108c 11523 struct type *type;
c906108c 11524 struct attribute *attr;
0114d602 11525 const char *name;
134d01f1 11526
348e048f
DE
11527 /* If the definition of this type lives in .debug_types, read that type.
11528 Don't follow DW_AT_specification though, that will take us back up
11529 the chain and we want to go down. */
45e58e77 11530 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11531 if (attr)
11532 {
11533 struct dwarf2_cu *type_cu = cu;
11534 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11535
348e048f 11536 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11537
11538 /* TYPE_CU may not be the same as CU.
11539 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11540 return set_die_type (die, type, cu);
11541 }
11542
c906108c
SS
11543 type = alloc_type (objfile);
11544
11545 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11546 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11547 if (name != NULL)
0114d602 11548 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11549
e142c38c 11550 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11551 if (attr)
11552 {
11553 TYPE_LENGTH (type) = DW_UNSND (attr);
11554 }
11555 else
11556 {
11557 TYPE_LENGTH (type) = 0;
11558 }
11559
137033e9
JB
11560 /* The enumeration DIE can be incomplete. In Ada, any type can be
11561 declared as private in the package spec, and then defined only
11562 inside the package body. Such types are known as Taft Amendment
11563 Types. When another package uses such a type, an incomplete DIE
11564 may be generated by the compiler. */
02eb380e 11565 if (die_is_declaration (die, cu))
876cecd0 11566 TYPE_STUB (type) = 1;
02eb380e 11567
f792889a 11568 return set_die_type (die, type, cu);
134d01f1
DJ
11569}
11570
11571/* Given a pointer to a die which begins an enumeration, process all
11572 the dies that define the members of the enumeration, and create the
11573 symbol for the enumeration type.
11574
11575 NOTE: We reverse the order of the element list. */
11576
11577static void
11578process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11579{
f792889a 11580 struct type *this_type;
134d01f1 11581
f792889a
DJ
11582 this_type = get_die_type (die, cu);
11583 if (this_type == NULL)
11584 this_type = read_enumeration_type (die, cu);
9dc481d3 11585
639d11d3 11586 if (die->child != NULL)
c906108c 11587 {
9dc481d3
DE
11588 struct die_info *child_die;
11589 struct symbol *sym;
11590 struct field *fields = NULL;
11591 int num_fields = 0;
11592 int unsigned_enum = 1;
15d034d0 11593 const char *name;
cafec441
TT
11594 int flag_enum = 1;
11595 ULONGEST mask = 0;
9dc481d3 11596
639d11d3 11597 child_die = die->child;
c906108c
SS
11598 while (child_die && child_die->tag)
11599 {
11600 if (child_die->tag != DW_TAG_enumerator)
11601 {
e7c27a73 11602 process_die (child_die, cu);
c906108c
SS
11603 }
11604 else
11605 {
39cbfefa
DJ
11606 name = dwarf2_name (child_die, cu);
11607 if (name)
c906108c 11608 {
f792889a 11609 sym = new_symbol (child_die, this_type, cu);
c906108c 11610 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11611 {
11612 unsigned_enum = 0;
11613 flag_enum = 0;
11614 }
11615 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11616 flag_enum = 0;
11617 else
11618 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11619
11620 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11621 {
11622 fields = (struct field *)
11623 xrealloc (fields,
11624 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11625 * sizeof (struct field));
c906108c
SS
11626 }
11627
3567439c 11628 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11629 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11630 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11631 FIELD_BITSIZE (fields[num_fields]) = 0;
11632
11633 num_fields++;
11634 }
11635 }
11636
11637 child_die = sibling_die (child_die);
11638 }
11639
11640 if (num_fields)
11641 {
f792889a
DJ
11642 TYPE_NFIELDS (this_type) = num_fields;
11643 TYPE_FIELDS (this_type) = (struct field *)
11644 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11645 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11646 sizeof (struct field) * num_fields);
b8c9b27d 11647 xfree (fields);
c906108c
SS
11648 }
11649 if (unsigned_enum)
876cecd0 11650 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11651 if (flag_enum)
11652 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11653 }
134d01f1 11654
6c83ed52
TT
11655 /* If we are reading an enum from a .debug_types unit, and the enum
11656 is a declaration, and the enum is not the signatured type in the
11657 unit, then we do not want to add a symbol for it. Adding a
11658 symbol would in some cases obscure the true definition of the
11659 enum, giving users an incomplete type when the definition is
11660 actually available. Note that we do not want to do this for all
11661 enums which are just declarations, because C++0x allows forward
11662 enum declarations. */
3019eac3 11663 if (cu->per_cu->is_debug_types
6c83ed52
TT
11664 && die_is_declaration (die, cu))
11665 {
52dc124a 11666 struct signatured_type *sig_type;
6c83ed52 11667
52dc124a 11668 sig_type
6c83ed52 11669 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11670 cu->per_cu->info_or_types_section,
6c83ed52 11671 cu->per_cu->offset);
3019eac3
DE
11672 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11673 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11674 return;
11675 }
11676
f792889a 11677 new_symbol (die, this_type, cu);
c906108c
SS
11678}
11679
11680/* Extract all information from a DW_TAG_array_type DIE and put it in
11681 the DIE's type field. For now, this only handles one dimensional
11682 arrays. */
11683
f792889a 11684static struct type *
e7c27a73 11685read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11686{
e7c27a73 11687 struct objfile *objfile = cu->objfile;
c906108c 11688 struct die_info *child_die;
7e314c57 11689 struct type *type;
c906108c
SS
11690 struct type *element_type, *range_type, *index_type;
11691 struct type **range_types = NULL;
11692 struct attribute *attr;
11693 int ndim = 0;
11694 struct cleanup *back_to;
15d034d0 11695 const char *name;
c906108c 11696
e7c27a73 11697 element_type = die_type (die, cu);
c906108c 11698
7e314c57
JK
11699 /* The die_type call above may have already set the type for this DIE. */
11700 type = get_die_type (die, cu);
11701 if (type)
11702 return type;
11703
c906108c
SS
11704 /* Irix 6.2 native cc creates array types without children for
11705 arrays with unspecified length. */
639d11d3 11706 if (die->child == NULL)
c906108c 11707 {
46bf5051 11708 index_type = objfile_type (objfile)->builtin_int;
c906108c 11709 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11710 type = create_array_type (NULL, element_type, range_type);
11711 return set_die_type (die, type, cu);
c906108c
SS
11712 }
11713
11714 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11715 child_die = die->child;
c906108c
SS
11716 while (child_die && child_die->tag)
11717 {
11718 if (child_die->tag == DW_TAG_subrange_type)
11719 {
f792889a 11720 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11721
f792889a 11722 if (child_type != NULL)
a02abb62 11723 {
0963b4bd
MS
11724 /* The range type was succesfully read. Save it for the
11725 array type creation. */
a02abb62
JB
11726 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11727 {
11728 range_types = (struct type **)
11729 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11730 * sizeof (struct type *));
11731 if (ndim == 0)
11732 make_cleanup (free_current_contents, &range_types);
11733 }
f792889a 11734 range_types[ndim++] = child_type;
a02abb62 11735 }
c906108c
SS
11736 }
11737 child_die = sibling_die (child_die);
11738 }
11739
11740 /* Dwarf2 dimensions are output from left to right, create the
11741 necessary array types in backwards order. */
7ca2d3a3 11742
c906108c 11743 type = element_type;
7ca2d3a3
DL
11744
11745 if (read_array_order (die, cu) == DW_ORD_col_major)
11746 {
11747 int i = 0;
9a619af0 11748
7ca2d3a3
DL
11749 while (i < ndim)
11750 type = create_array_type (NULL, type, range_types[i++]);
11751 }
11752 else
11753 {
11754 while (ndim-- > 0)
11755 type = create_array_type (NULL, type, range_types[ndim]);
11756 }
c906108c 11757
f5f8a009
EZ
11758 /* Understand Dwarf2 support for vector types (like they occur on
11759 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11760 array type. This is not part of the Dwarf2/3 standard yet, but a
11761 custom vendor extension. The main difference between a regular
11762 array and the vector variant is that vectors are passed by value
11763 to functions. */
e142c38c 11764 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11765 if (attr)
ea37ba09 11766 make_vector_type (type);
f5f8a009 11767
dbc98a8b
KW
11768 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11769 implementation may choose to implement triple vectors using this
11770 attribute. */
11771 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11772 if (attr)
11773 {
11774 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11775 TYPE_LENGTH (type) = DW_UNSND (attr);
11776 else
3e43a32a
MS
11777 complaint (&symfile_complaints,
11778 _("DW_AT_byte_size for array type smaller "
11779 "than the total size of elements"));
dbc98a8b
KW
11780 }
11781
39cbfefa
DJ
11782 name = dwarf2_name (die, cu);
11783 if (name)
11784 TYPE_NAME (type) = name;
6e70227d 11785
0963b4bd 11786 /* Install the type in the die. */
7e314c57
JK
11787 set_die_type (die, type, cu);
11788
11789 /* set_die_type should be already done. */
b4ba55a1
JB
11790 set_descriptive_type (type, die, cu);
11791
c906108c
SS
11792 do_cleanups (back_to);
11793
7e314c57 11794 return type;
c906108c
SS
11795}
11796
7ca2d3a3 11797static enum dwarf_array_dim_ordering
6e70227d 11798read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11799{
11800 struct attribute *attr;
11801
11802 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11803
11804 if (attr) return DW_SND (attr);
11805
0963b4bd
MS
11806 /* GNU F77 is a special case, as at 08/2004 array type info is the
11807 opposite order to the dwarf2 specification, but data is still
11808 laid out as per normal fortran.
7ca2d3a3 11809
0963b4bd
MS
11810 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11811 version checking. */
7ca2d3a3 11812
905e0470
PM
11813 if (cu->language == language_fortran
11814 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11815 {
11816 return DW_ORD_row_major;
11817 }
11818
6e70227d 11819 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11820 {
11821 case array_column_major:
11822 return DW_ORD_col_major;
11823 case array_row_major:
11824 default:
11825 return DW_ORD_row_major;
11826 };
11827}
11828
72019c9c 11829/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11830 the DIE's type field. */
72019c9c 11831
f792889a 11832static struct type *
72019c9c
GM
11833read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11834{
7e314c57
JK
11835 struct type *domain_type, *set_type;
11836 struct attribute *attr;
f792889a 11837
7e314c57
JK
11838 domain_type = die_type (die, cu);
11839
11840 /* The die_type call above may have already set the type for this DIE. */
11841 set_type = get_die_type (die, cu);
11842 if (set_type)
11843 return set_type;
11844
11845 set_type = create_set_type (NULL, domain_type);
11846
11847 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11848 if (attr)
11849 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11850
f792889a 11851 return set_die_type (die, set_type, cu);
72019c9c 11852}
7ca2d3a3 11853
0971de02
TT
11854/* A helper for read_common_block that creates a locexpr baton.
11855 SYM is the symbol which we are marking as computed.
11856 COMMON_DIE is the DIE for the common block.
11857 COMMON_LOC is the location expression attribute for the common
11858 block itself.
11859 MEMBER_LOC is the location expression attribute for the particular
11860 member of the common block that we are processing.
11861 CU is the CU from which the above come. */
11862
11863static void
11864mark_common_block_symbol_computed (struct symbol *sym,
11865 struct die_info *common_die,
11866 struct attribute *common_loc,
11867 struct attribute *member_loc,
11868 struct dwarf2_cu *cu)
11869{
11870 struct objfile *objfile = dwarf2_per_objfile->objfile;
11871 struct dwarf2_locexpr_baton *baton;
11872 gdb_byte *ptr;
11873 unsigned int cu_off;
11874 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11875 LONGEST offset = 0;
11876
11877 gdb_assert (common_loc && member_loc);
11878 gdb_assert (attr_form_is_block (common_loc));
11879 gdb_assert (attr_form_is_block (member_loc)
11880 || attr_form_is_constant (member_loc));
11881
11882 baton = obstack_alloc (&objfile->objfile_obstack,
11883 sizeof (struct dwarf2_locexpr_baton));
11884 baton->per_cu = cu->per_cu;
11885 gdb_assert (baton->per_cu);
11886
11887 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11888
11889 if (attr_form_is_constant (member_loc))
11890 {
11891 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11892 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11893 }
11894 else
11895 baton->size += DW_BLOCK (member_loc)->size;
11896
11897 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11898 baton->data = ptr;
11899
11900 *ptr++ = DW_OP_call4;
11901 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11902 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11903 ptr += 4;
11904
11905 if (attr_form_is_constant (member_loc))
11906 {
11907 *ptr++ = DW_OP_addr;
11908 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11909 ptr += cu->header.addr_size;
11910 }
11911 else
11912 {
11913 /* We have to copy the data here, because DW_OP_call4 will only
11914 use a DW_AT_location attribute. */
11915 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11916 ptr += DW_BLOCK (member_loc)->size;
11917 }
11918
11919 *ptr++ = DW_OP_plus;
11920 gdb_assert (ptr - baton->data == baton->size);
11921
11922 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11923 SYMBOL_LOCATION_BATON (sym) = baton;
11924 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11925}
11926
4357ac6c
TT
11927/* Create appropriate locally-scoped variables for all the
11928 DW_TAG_common_block entries. Also create a struct common_block
11929 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11930 is used to sepate the common blocks name namespace from regular
11931 variable names. */
c906108c
SS
11932
11933static void
e7c27a73 11934read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11935{
0971de02
TT
11936 struct attribute *attr;
11937
11938 attr = dwarf2_attr (die, DW_AT_location, cu);
11939 if (attr)
11940 {
11941 /* Support the .debug_loc offsets. */
11942 if (attr_form_is_block (attr))
11943 {
11944 /* Ok. */
11945 }
11946 else if (attr_form_is_section_offset (attr))
11947 {
11948 dwarf2_complex_location_expr_complaint ();
11949 attr = NULL;
11950 }
11951 else
11952 {
11953 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11954 "common block member");
11955 attr = NULL;
11956 }
11957 }
11958
639d11d3 11959 if (die->child != NULL)
c906108c 11960 {
4357ac6c
TT
11961 struct objfile *objfile = cu->objfile;
11962 struct die_info *child_die;
11963 size_t n_entries = 0, size;
11964 struct common_block *common_block;
11965 struct symbol *sym;
74ac6d43 11966
4357ac6c
TT
11967 for (child_die = die->child;
11968 child_die && child_die->tag;
11969 child_die = sibling_die (child_die))
11970 ++n_entries;
11971
11972 size = (sizeof (struct common_block)
11973 + (n_entries - 1) * sizeof (struct symbol *));
11974 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11975 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11976 common_block->n_entries = 0;
11977
11978 for (child_die = die->child;
11979 child_die && child_die->tag;
11980 child_die = sibling_die (child_die))
11981 {
11982 /* Create the symbol in the DW_TAG_common_block block in the current
11983 symbol scope. */
e7c27a73 11984 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11985 if (sym != NULL)
11986 {
11987 struct attribute *member_loc;
11988
11989 common_block->contents[common_block->n_entries++] = sym;
11990
11991 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11992 cu);
11993 if (member_loc)
11994 {
11995 /* GDB has handled this for a long time, but it is
11996 not specified by DWARF. It seems to have been
11997 emitted by gfortran at least as recently as:
11998 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11999 complaint (&symfile_complaints,
12000 _("Variable in common block has "
12001 "DW_AT_data_member_location "
12002 "- DIE at 0x%x [in module %s]"),
12003 child_die->offset.sect_off, cu->objfile->name);
12004
12005 if (attr_form_is_section_offset (member_loc))
12006 dwarf2_complex_location_expr_complaint ();
12007 else if (attr_form_is_constant (member_loc)
12008 || attr_form_is_block (member_loc))
12009 {
12010 if (attr)
12011 mark_common_block_symbol_computed (sym, die, attr,
12012 member_loc, cu);
12013 }
12014 else
12015 dwarf2_complex_location_expr_complaint ();
12016 }
12017 }
c906108c 12018 }
4357ac6c
TT
12019
12020 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12021 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12022 }
12023}
12024
0114d602 12025/* Create a type for a C++ namespace. */
d9fa45fe 12026
0114d602
DJ
12027static struct type *
12028read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12029{
e7c27a73 12030 struct objfile *objfile = cu->objfile;
0114d602 12031 const char *previous_prefix, *name;
9219021c 12032 int is_anonymous;
0114d602
DJ
12033 struct type *type;
12034
12035 /* For extensions, reuse the type of the original namespace. */
12036 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12037 {
12038 struct die_info *ext_die;
12039 struct dwarf2_cu *ext_cu = cu;
9a619af0 12040
0114d602
DJ
12041 ext_die = dwarf2_extension (die, &ext_cu);
12042 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12043
12044 /* EXT_CU may not be the same as CU.
12045 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12046 return set_die_type (die, type, cu);
12047 }
9219021c 12048
e142c38c 12049 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12050
12051 /* Now build the name of the current namespace. */
12052
0114d602
DJ
12053 previous_prefix = determine_prefix (die, cu);
12054 if (previous_prefix[0] != '\0')
12055 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12056 previous_prefix, name, 0, cu);
0114d602
DJ
12057
12058 /* Create the type. */
12059 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12060 objfile);
12061 TYPE_NAME (type) = (char *) name;
12062 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12063
60531b24 12064 return set_die_type (die, type, cu);
0114d602
DJ
12065}
12066
12067/* Read a C++ namespace. */
12068
12069static void
12070read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12071{
12072 struct objfile *objfile = cu->objfile;
0114d602 12073 int is_anonymous;
9219021c 12074
5c4e30ca
DC
12075 /* Add a symbol associated to this if we haven't seen the namespace
12076 before. Also, add a using directive if it's an anonymous
12077 namespace. */
9219021c 12078
f2f0e013 12079 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12080 {
12081 struct type *type;
12082
0114d602 12083 type = read_type_die (die, cu);
e7c27a73 12084 new_symbol (die, type, cu);
5c4e30ca 12085
e8e80198 12086 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12087 if (is_anonymous)
0114d602
DJ
12088 {
12089 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12090
c0cc3a76 12091 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 12092 NULL, NULL, &objfile->objfile_obstack);
0114d602 12093 }
5c4e30ca 12094 }
9219021c 12095
639d11d3 12096 if (die->child != NULL)
d9fa45fe 12097 {
639d11d3 12098 struct die_info *child_die = die->child;
6e70227d 12099
d9fa45fe
DC
12100 while (child_die && child_die->tag)
12101 {
e7c27a73 12102 process_die (child_die, cu);
d9fa45fe
DC
12103 child_die = sibling_die (child_die);
12104 }
12105 }
38d518c9
EZ
12106}
12107
f55ee35c
JK
12108/* Read a Fortran module as type. This DIE can be only a declaration used for
12109 imported module. Still we need that type as local Fortran "use ... only"
12110 declaration imports depend on the created type in determine_prefix. */
12111
12112static struct type *
12113read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12114{
12115 struct objfile *objfile = cu->objfile;
15d034d0 12116 const char *module_name;
f55ee35c
JK
12117 struct type *type;
12118
12119 module_name = dwarf2_name (die, cu);
12120 if (!module_name)
3e43a32a
MS
12121 complaint (&symfile_complaints,
12122 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12123 die->offset.sect_off);
f55ee35c
JK
12124 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12125
12126 /* determine_prefix uses TYPE_TAG_NAME. */
12127 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12128
12129 return set_die_type (die, type, cu);
12130}
12131
5d7cb8df
JK
12132/* Read a Fortran module. */
12133
12134static void
12135read_module (struct die_info *die, struct dwarf2_cu *cu)
12136{
12137 struct die_info *child_die = die->child;
12138
5d7cb8df
JK
12139 while (child_die && child_die->tag)
12140 {
12141 process_die (child_die, cu);
12142 child_die = sibling_die (child_die);
12143 }
12144}
12145
38d518c9
EZ
12146/* Return the name of the namespace represented by DIE. Set
12147 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12148 namespace. */
12149
12150static const char *
e142c38c 12151namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12152{
12153 struct die_info *current_die;
12154 const char *name = NULL;
12155
12156 /* Loop through the extensions until we find a name. */
12157
12158 for (current_die = die;
12159 current_die != NULL;
f2f0e013 12160 current_die = dwarf2_extension (die, &cu))
38d518c9 12161 {
e142c38c 12162 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12163 if (name != NULL)
12164 break;
12165 }
12166
12167 /* Is it an anonymous namespace? */
12168
12169 *is_anonymous = (name == NULL);
12170 if (*is_anonymous)
2b1dbab0 12171 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12172
12173 return name;
d9fa45fe
DC
12174}
12175
c906108c
SS
12176/* Extract all information from a DW_TAG_pointer_type DIE and add to
12177 the user defined type vector. */
12178
f792889a 12179static struct type *
e7c27a73 12180read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12181{
5e2b427d 12182 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12183 struct comp_unit_head *cu_header = &cu->header;
c906108c 12184 struct type *type;
8b2dbe47
KB
12185 struct attribute *attr_byte_size;
12186 struct attribute *attr_address_class;
12187 int byte_size, addr_class;
7e314c57
JK
12188 struct type *target_type;
12189
12190 target_type = die_type (die, cu);
c906108c 12191
7e314c57
JK
12192 /* The die_type call above may have already set the type for this DIE. */
12193 type = get_die_type (die, cu);
12194 if (type)
12195 return type;
12196
12197 type = lookup_pointer_type (target_type);
8b2dbe47 12198
e142c38c 12199 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12200 if (attr_byte_size)
12201 byte_size = DW_UNSND (attr_byte_size);
c906108c 12202 else
8b2dbe47
KB
12203 byte_size = cu_header->addr_size;
12204
e142c38c 12205 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12206 if (attr_address_class)
12207 addr_class = DW_UNSND (attr_address_class);
12208 else
12209 addr_class = DW_ADDR_none;
12210
12211 /* If the pointer size or address class is different than the
12212 default, create a type variant marked as such and set the
12213 length accordingly. */
12214 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12215 {
5e2b427d 12216 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12217 {
12218 int type_flags;
12219
849957d9 12220 type_flags = gdbarch_address_class_type_flags
5e2b427d 12221 (gdbarch, byte_size, addr_class);
876cecd0
TT
12222 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12223 == 0);
8b2dbe47
KB
12224 type = make_type_with_address_space (type, type_flags);
12225 }
12226 else if (TYPE_LENGTH (type) != byte_size)
12227 {
3e43a32a
MS
12228 complaint (&symfile_complaints,
12229 _("invalid pointer size %d"), byte_size);
8b2dbe47 12230 }
6e70227d 12231 else
9a619af0
MS
12232 {
12233 /* Should we also complain about unhandled address classes? */
12234 }
c906108c 12235 }
8b2dbe47
KB
12236
12237 TYPE_LENGTH (type) = byte_size;
f792889a 12238 return set_die_type (die, type, cu);
c906108c
SS
12239}
12240
12241/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12242 the user defined type vector. */
12243
f792889a 12244static struct type *
e7c27a73 12245read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12246{
12247 struct type *type;
12248 struct type *to_type;
12249 struct type *domain;
12250
e7c27a73
DJ
12251 to_type = die_type (die, cu);
12252 domain = die_containing_type (die, cu);
0d5de010 12253
7e314c57
JK
12254 /* The calls above may have already set the type for this DIE. */
12255 type = get_die_type (die, cu);
12256 if (type)
12257 return type;
12258
0d5de010
DJ
12259 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12260 type = lookup_methodptr_type (to_type);
12261 else
12262 type = lookup_memberptr_type (to_type, domain);
c906108c 12263
f792889a 12264 return set_die_type (die, type, cu);
c906108c
SS
12265}
12266
12267/* Extract all information from a DW_TAG_reference_type DIE and add to
12268 the user defined type vector. */
12269
f792889a 12270static struct type *
e7c27a73 12271read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12272{
e7c27a73 12273 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12274 struct type *type, *target_type;
c906108c
SS
12275 struct attribute *attr;
12276
7e314c57
JK
12277 target_type = die_type (die, cu);
12278
12279 /* The die_type call above may have already set the type for this DIE. */
12280 type = get_die_type (die, cu);
12281 if (type)
12282 return type;
12283
12284 type = lookup_reference_type (target_type);
e142c38c 12285 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12286 if (attr)
12287 {
12288 TYPE_LENGTH (type) = DW_UNSND (attr);
12289 }
12290 else
12291 {
107d2387 12292 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12293 }
f792889a 12294 return set_die_type (die, type, cu);
c906108c
SS
12295}
12296
f792889a 12297static struct type *
e7c27a73 12298read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12299{
f792889a 12300 struct type *base_type, *cv_type;
c906108c 12301
e7c27a73 12302 base_type = die_type (die, cu);
7e314c57
JK
12303
12304 /* The die_type call above may have already set the type for this DIE. */
12305 cv_type = get_die_type (die, cu);
12306 if (cv_type)
12307 return cv_type;
12308
2f608a3a
KW
12309 /* In case the const qualifier is applied to an array type, the element type
12310 is so qualified, not the array type (section 6.7.3 of C99). */
12311 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12312 {
12313 struct type *el_type, *inner_array;
12314
12315 base_type = copy_type (base_type);
12316 inner_array = base_type;
12317
12318 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12319 {
12320 TYPE_TARGET_TYPE (inner_array) =
12321 copy_type (TYPE_TARGET_TYPE (inner_array));
12322 inner_array = TYPE_TARGET_TYPE (inner_array);
12323 }
12324
12325 el_type = TYPE_TARGET_TYPE (inner_array);
12326 TYPE_TARGET_TYPE (inner_array) =
12327 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12328
12329 return set_die_type (die, base_type, cu);
12330 }
12331
f792889a
DJ
12332 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12333 return set_die_type (die, cv_type, cu);
c906108c
SS
12334}
12335
f792889a 12336static struct type *
e7c27a73 12337read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12338{
f792889a 12339 struct type *base_type, *cv_type;
c906108c 12340
e7c27a73 12341 base_type = die_type (die, cu);
7e314c57
JK
12342
12343 /* The die_type call above may have already set the type for this DIE. */
12344 cv_type = get_die_type (die, cu);
12345 if (cv_type)
12346 return cv_type;
12347
f792889a
DJ
12348 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12349 return set_die_type (die, cv_type, cu);
c906108c
SS
12350}
12351
06d66ee9
TT
12352/* Handle DW_TAG_restrict_type. */
12353
12354static struct type *
12355read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12356{
12357 struct type *base_type, *cv_type;
12358
12359 base_type = die_type (die, cu);
12360
12361 /* The die_type call above may have already set the type for this DIE. */
12362 cv_type = get_die_type (die, cu);
12363 if (cv_type)
12364 return cv_type;
12365
12366 cv_type = make_restrict_type (base_type);
12367 return set_die_type (die, cv_type, cu);
12368}
12369
c906108c
SS
12370/* Extract all information from a DW_TAG_string_type DIE and add to
12371 the user defined type vector. It isn't really a user defined type,
12372 but it behaves like one, with other DIE's using an AT_user_def_type
12373 attribute to reference it. */
12374
f792889a 12375static struct type *
e7c27a73 12376read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12377{
e7c27a73 12378 struct objfile *objfile = cu->objfile;
3b7538c0 12379 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12380 struct type *type, *range_type, *index_type, *char_type;
12381 struct attribute *attr;
12382 unsigned int length;
12383
e142c38c 12384 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12385 if (attr)
12386 {
12387 length = DW_UNSND (attr);
12388 }
12389 else
12390 {
0963b4bd 12391 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12392 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12393 if (attr)
12394 {
12395 length = DW_UNSND (attr);
12396 }
12397 else
12398 {
12399 length = 1;
12400 }
c906108c 12401 }
6ccb9162 12402
46bf5051 12403 index_type = objfile_type (objfile)->builtin_int;
c906108c 12404 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12405 char_type = language_string_char_type (cu->language_defn, gdbarch);
12406 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12407
f792889a 12408 return set_die_type (die, type, cu);
c906108c
SS
12409}
12410
12411/* Handle DIES due to C code like:
12412
12413 struct foo
c5aa993b
JM
12414 {
12415 int (*funcp)(int a, long l);
12416 int b;
12417 };
c906108c 12418
0963b4bd 12419 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12420
f792889a 12421static struct type *
e7c27a73 12422read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12423{
bb5ed363 12424 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12425 struct type *type; /* Type that this function returns. */
12426 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12427 struct attribute *attr;
12428
e7c27a73 12429 type = die_type (die, cu);
7e314c57
JK
12430
12431 /* The die_type call above may have already set the type for this DIE. */
12432 ftype = get_die_type (die, cu);
12433 if (ftype)
12434 return ftype;
12435
0c8b41f1 12436 ftype = lookup_function_type (type);
c906108c 12437
5b8101ae 12438 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12439 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12440 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12441 || cu->language == language_cplus
5b8101ae
PM
12442 || cu->language == language_java
12443 || cu->language == language_pascal)
876cecd0 12444 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12445 else if (producer_is_realview (cu->producer))
12446 /* RealView does not emit DW_AT_prototyped. We can not
12447 distinguish prototyped and unprototyped functions; default to
12448 prototyped, since that is more common in modern code (and
12449 RealView warns about unprototyped functions). */
12450 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12451
c055b101
CV
12452 /* Store the calling convention in the type if it's available in
12453 the subroutine die. Otherwise set the calling convention to
12454 the default value DW_CC_normal. */
12455 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12456 if (attr)
12457 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12458 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12459 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12460 else
12461 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12462
12463 /* We need to add the subroutine type to the die immediately so
12464 we don't infinitely recurse when dealing with parameters
0963b4bd 12465 declared as the same subroutine type. */
76c10ea2 12466 set_die_type (die, ftype, cu);
6e70227d 12467
639d11d3 12468 if (die->child != NULL)
c906108c 12469 {
bb5ed363 12470 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12471 struct die_info *child_die;
8072405b 12472 int nparams, iparams;
c906108c
SS
12473
12474 /* Count the number of parameters.
12475 FIXME: GDB currently ignores vararg functions, but knows about
12476 vararg member functions. */
8072405b 12477 nparams = 0;
639d11d3 12478 child_die = die->child;
c906108c
SS
12479 while (child_die && child_die->tag)
12480 {
12481 if (child_die->tag == DW_TAG_formal_parameter)
12482 nparams++;
12483 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12484 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12485 child_die = sibling_die (child_die);
12486 }
12487
12488 /* Allocate storage for parameters and fill them in. */
12489 TYPE_NFIELDS (ftype) = nparams;
12490 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12491 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12492
8072405b
JK
12493 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12494 even if we error out during the parameters reading below. */
12495 for (iparams = 0; iparams < nparams; iparams++)
12496 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12497
12498 iparams = 0;
639d11d3 12499 child_die = die->child;
c906108c
SS
12500 while (child_die && child_die->tag)
12501 {
12502 if (child_die->tag == DW_TAG_formal_parameter)
12503 {
3ce3b1ba
PA
12504 struct type *arg_type;
12505
12506 /* DWARF version 2 has no clean way to discern C++
12507 static and non-static member functions. G++ helps
12508 GDB by marking the first parameter for non-static
12509 member functions (which is the this pointer) as
12510 artificial. We pass this information to
12511 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12512
12513 DWARF version 3 added DW_AT_object_pointer, which GCC
12514 4.5 does not yet generate. */
e142c38c 12515 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12516 if (attr)
12517 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12518 else
418835cc
KS
12519 {
12520 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12521
12522 /* GCC/43521: In java, the formal parameter
12523 "this" is sometimes not marked with DW_AT_artificial. */
12524 if (cu->language == language_java)
12525 {
12526 const char *name = dwarf2_name (child_die, cu);
9a619af0 12527
418835cc
KS
12528 if (name && !strcmp (name, "this"))
12529 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12530 }
12531 }
3ce3b1ba
PA
12532 arg_type = die_type (child_die, cu);
12533
12534 /* RealView does not mark THIS as const, which the testsuite
12535 expects. GCC marks THIS as const in method definitions,
12536 but not in the class specifications (GCC PR 43053). */
12537 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12538 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12539 {
12540 int is_this = 0;
12541 struct dwarf2_cu *arg_cu = cu;
12542 const char *name = dwarf2_name (child_die, cu);
12543
12544 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12545 if (attr)
12546 {
12547 /* If the compiler emits this, use it. */
12548 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12549 is_this = 1;
12550 }
12551 else if (name && strcmp (name, "this") == 0)
12552 /* Function definitions will have the argument names. */
12553 is_this = 1;
12554 else if (name == NULL && iparams == 0)
12555 /* Declarations may not have the names, so like
12556 elsewhere in GDB, assume an artificial first
12557 argument is "this". */
12558 is_this = 1;
12559
12560 if (is_this)
12561 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12562 arg_type, 0);
12563 }
12564
12565 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12566 iparams++;
12567 }
12568 child_die = sibling_die (child_die);
12569 }
12570 }
12571
76c10ea2 12572 return ftype;
c906108c
SS
12573}
12574
f792889a 12575static struct type *
e7c27a73 12576read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12577{
e7c27a73 12578 struct objfile *objfile = cu->objfile;
0114d602 12579 const char *name = NULL;
3c8e0968 12580 struct type *this_type, *target_type;
c906108c 12581
94af9270 12582 name = dwarf2_full_name (NULL, die, cu);
f792889a 12583 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12584 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12585 TYPE_NAME (this_type) = (char *) name;
f792889a 12586 set_die_type (die, this_type, cu);
3c8e0968
DE
12587 target_type = die_type (die, cu);
12588 if (target_type != this_type)
12589 TYPE_TARGET_TYPE (this_type) = target_type;
12590 else
12591 {
12592 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12593 spec and cause infinite loops in GDB. */
12594 complaint (&symfile_complaints,
12595 _("Self-referential DW_TAG_typedef "
12596 "- DIE at 0x%x [in module %s]"),
b64f50a1 12597 die->offset.sect_off, objfile->name);
3c8e0968
DE
12598 TYPE_TARGET_TYPE (this_type) = NULL;
12599 }
f792889a 12600 return this_type;
c906108c
SS
12601}
12602
12603/* Find a representation of a given base type and install
12604 it in the TYPE field of the die. */
12605
f792889a 12606static struct type *
e7c27a73 12607read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12608{
e7c27a73 12609 struct objfile *objfile = cu->objfile;
c906108c
SS
12610 struct type *type;
12611 struct attribute *attr;
12612 int encoding = 0, size = 0;
15d034d0 12613 const char *name;
6ccb9162
UW
12614 enum type_code code = TYPE_CODE_INT;
12615 int type_flags = 0;
12616 struct type *target_type = NULL;
c906108c 12617
e142c38c 12618 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12619 if (attr)
12620 {
12621 encoding = DW_UNSND (attr);
12622 }
e142c38c 12623 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12624 if (attr)
12625 {
12626 size = DW_UNSND (attr);
12627 }
39cbfefa 12628 name = dwarf2_name (die, cu);
6ccb9162 12629 if (!name)
c906108c 12630 {
6ccb9162
UW
12631 complaint (&symfile_complaints,
12632 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12633 }
6ccb9162
UW
12634
12635 switch (encoding)
c906108c 12636 {
6ccb9162
UW
12637 case DW_ATE_address:
12638 /* Turn DW_ATE_address into a void * pointer. */
12639 code = TYPE_CODE_PTR;
12640 type_flags |= TYPE_FLAG_UNSIGNED;
12641 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12642 break;
12643 case DW_ATE_boolean:
12644 code = TYPE_CODE_BOOL;
12645 type_flags |= TYPE_FLAG_UNSIGNED;
12646 break;
12647 case DW_ATE_complex_float:
12648 code = TYPE_CODE_COMPLEX;
12649 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12650 break;
12651 case DW_ATE_decimal_float:
12652 code = TYPE_CODE_DECFLOAT;
12653 break;
12654 case DW_ATE_float:
12655 code = TYPE_CODE_FLT;
12656 break;
12657 case DW_ATE_signed:
12658 break;
12659 case DW_ATE_unsigned:
12660 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12661 if (cu->language == language_fortran
12662 && name
12663 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12664 code = TYPE_CODE_CHAR;
6ccb9162
UW
12665 break;
12666 case DW_ATE_signed_char:
6e70227d 12667 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12668 || cu->language == language_pascal
12669 || cu->language == language_fortran)
6ccb9162
UW
12670 code = TYPE_CODE_CHAR;
12671 break;
12672 case DW_ATE_unsigned_char:
868a0084 12673 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12674 || cu->language == language_pascal
12675 || cu->language == language_fortran)
6ccb9162
UW
12676 code = TYPE_CODE_CHAR;
12677 type_flags |= TYPE_FLAG_UNSIGNED;
12678 break;
75079b2b
TT
12679 case DW_ATE_UTF:
12680 /* We just treat this as an integer and then recognize the
12681 type by name elsewhere. */
12682 break;
12683
6ccb9162
UW
12684 default:
12685 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12686 dwarf_type_encoding_name (encoding));
12687 break;
c906108c 12688 }
6ccb9162 12689
0114d602
DJ
12690 type = init_type (code, size, type_flags, NULL, objfile);
12691 TYPE_NAME (type) = name;
6ccb9162
UW
12692 TYPE_TARGET_TYPE (type) = target_type;
12693
0114d602 12694 if (name && strcmp (name, "char") == 0)
876cecd0 12695 TYPE_NOSIGN (type) = 1;
0114d602 12696
f792889a 12697 return set_die_type (die, type, cu);
c906108c
SS
12698}
12699
a02abb62
JB
12700/* Read the given DW_AT_subrange DIE. */
12701
f792889a 12702static struct type *
a02abb62
JB
12703read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12704{
12705 struct type *base_type;
12706 struct type *range_type;
12707 struct attribute *attr;
4fae6e18
JK
12708 LONGEST low, high;
12709 int low_default_is_valid;
15d034d0 12710 const char *name;
43bbcdc2 12711 LONGEST negative_mask;
e77813c8 12712
a02abb62 12713 base_type = die_type (die, cu);
953ac07e
JK
12714 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12715 check_typedef (base_type);
a02abb62 12716
7e314c57
JK
12717 /* The die_type call above may have already set the type for this DIE. */
12718 range_type = get_die_type (die, cu);
12719 if (range_type)
12720 return range_type;
12721
4fae6e18
JK
12722 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12723 omitting DW_AT_lower_bound. */
12724 switch (cu->language)
6e70227d 12725 {
4fae6e18
JK
12726 case language_c:
12727 case language_cplus:
12728 low = 0;
12729 low_default_is_valid = 1;
12730 break;
12731 case language_fortran:
12732 low = 1;
12733 low_default_is_valid = 1;
12734 break;
12735 case language_d:
12736 case language_java:
12737 case language_objc:
12738 low = 0;
12739 low_default_is_valid = (cu->header.version >= 4);
12740 break;
12741 case language_ada:
12742 case language_m2:
12743 case language_pascal:
a02abb62 12744 low = 1;
4fae6e18
JK
12745 low_default_is_valid = (cu->header.version >= 4);
12746 break;
12747 default:
12748 low = 0;
12749 low_default_is_valid = 0;
12750 break;
a02abb62
JB
12751 }
12752
dd5e6932
DJ
12753 /* FIXME: For variable sized arrays either of these could be
12754 a variable rather than a constant value. We'll allow it,
12755 but we don't know how to handle it. */
e142c38c 12756 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12757 if (attr)
4fae6e18
JK
12758 low = dwarf2_get_attr_constant_value (attr, low);
12759 else if (!low_default_is_valid)
12760 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12761 "- DIE at 0x%x [in module %s]"),
12762 die->offset.sect_off, cu->objfile->name);
a02abb62 12763
e142c38c 12764 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12765 if (attr)
6e70227d 12766 {
d48323d8 12767 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12768 {
12769 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12770 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12771 FIXME: GDB does not yet know how to handle dynamic
12772 arrays properly, treat them as arrays with unspecified
12773 length for now.
12774
12775 FIXME: jimb/2003-09-22: GDB does not really know
12776 how to handle arrays of unspecified length
12777 either; we just represent them as zero-length
12778 arrays. Choose an appropriate upper bound given
12779 the lower bound we've computed above. */
12780 high = low - 1;
12781 }
12782 else
12783 high = dwarf2_get_attr_constant_value (attr, 1);
12784 }
e77813c8
PM
12785 else
12786 {
12787 attr = dwarf2_attr (die, DW_AT_count, cu);
12788 if (attr)
12789 {
12790 int count = dwarf2_get_attr_constant_value (attr, 1);
12791 high = low + count - 1;
12792 }
c2ff108b
JK
12793 else
12794 {
12795 /* Unspecified array length. */
12796 high = low - 1;
12797 }
e77813c8
PM
12798 }
12799
12800 /* Dwarf-2 specifications explicitly allows to create subrange types
12801 without specifying a base type.
12802 In that case, the base type must be set to the type of
12803 the lower bound, upper bound or count, in that order, if any of these
12804 three attributes references an object that has a type.
12805 If no base type is found, the Dwarf-2 specifications say that
12806 a signed integer type of size equal to the size of an address should
12807 be used.
12808 For the following C code: `extern char gdb_int [];'
12809 GCC produces an empty range DIE.
12810 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12811 high bound or count are not yet handled by this code. */
e77813c8
PM
12812 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12813 {
12814 struct objfile *objfile = cu->objfile;
12815 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12816 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12817 struct type *int_type = objfile_type (objfile)->builtin_int;
12818
12819 /* Test "int", "long int", and "long long int" objfile types,
12820 and select the first one having a size above or equal to the
12821 architecture address size. */
12822 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12823 base_type = int_type;
12824 else
12825 {
12826 int_type = objfile_type (objfile)->builtin_long;
12827 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12828 base_type = int_type;
12829 else
12830 {
12831 int_type = objfile_type (objfile)->builtin_long_long;
12832 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12833 base_type = int_type;
12834 }
12835 }
12836 }
a02abb62 12837
6e70227d 12838 negative_mask =
43bbcdc2
PH
12839 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12840 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12841 low |= negative_mask;
12842 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12843 high |= negative_mask;
12844
a02abb62
JB
12845 range_type = create_range_type (NULL, base_type, low, high);
12846
bbb0eef6
JK
12847 /* Mark arrays with dynamic length at least as an array of unspecified
12848 length. GDB could check the boundary but before it gets implemented at
12849 least allow accessing the array elements. */
d48323d8 12850 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12851 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12852
c2ff108b
JK
12853 /* Ada expects an empty array on no boundary attributes. */
12854 if (attr == NULL && cu->language != language_ada)
12855 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12856
39cbfefa
DJ
12857 name = dwarf2_name (die, cu);
12858 if (name)
12859 TYPE_NAME (range_type) = name;
6e70227d 12860
e142c38c 12861 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12862 if (attr)
12863 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12864
7e314c57
JK
12865 set_die_type (die, range_type, cu);
12866
12867 /* set_die_type should be already done. */
b4ba55a1
JB
12868 set_descriptive_type (range_type, die, cu);
12869
7e314c57 12870 return range_type;
a02abb62 12871}
6e70227d 12872
f792889a 12873static struct type *
81a17f79
JB
12874read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12875{
12876 struct type *type;
81a17f79 12877
81a17f79
JB
12878 /* For now, we only support the C meaning of an unspecified type: void. */
12879
0114d602
DJ
12880 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12881 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12882
f792889a 12883 return set_die_type (die, type, cu);
81a17f79 12884}
a02abb62 12885
639d11d3
DC
12886/* Read a single die and all its descendents. Set the die's sibling
12887 field to NULL; set other fields in the die correctly, and set all
12888 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12889 location of the info_ptr after reading all of those dies. PARENT
12890 is the parent of the die in question. */
12891
12892static struct die_info *
dee91e82
DE
12893read_die_and_children (const struct die_reader_specs *reader,
12894 gdb_byte *info_ptr,
12895 gdb_byte **new_info_ptr,
12896 struct die_info *parent)
639d11d3
DC
12897{
12898 struct die_info *die;
fe1b8b76 12899 gdb_byte *cur_ptr;
639d11d3
DC
12900 int has_children;
12901
93311388 12902 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12903 if (die == NULL)
12904 {
12905 *new_info_ptr = cur_ptr;
12906 return NULL;
12907 }
93311388 12908 store_in_ref_table (die, reader->cu);
639d11d3
DC
12909
12910 if (has_children)
348e048f 12911 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12912 else
12913 {
12914 die->child = NULL;
12915 *new_info_ptr = cur_ptr;
12916 }
12917
12918 die->sibling = NULL;
12919 die->parent = parent;
12920 return die;
12921}
12922
12923/* Read a die, all of its descendents, and all of its siblings; set
12924 all of the fields of all of the dies correctly. Arguments are as
12925 in read_die_and_children. */
12926
12927static struct die_info *
93311388
DE
12928read_die_and_siblings (const struct die_reader_specs *reader,
12929 gdb_byte *info_ptr,
fe1b8b76 12930 gdb_byte **new_info_ptr,
639d11d3
DC
12931 struct die_info *parent)
12932{
12933 struct die_info *first_die, *last_sibling;
fe1b8b76 12934 gdb_byte *cur_ptr;
639d11d3 12935
c906108c 12936 cur_ptr = info_ptr;
639d11d3
DC
12937 first_die = last_sibling = NULL;
12938
12939 while (1)
c906108c 12940 {
639d11d3 12941 struct die_info *die
dee91e82 12942 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12943
1d325ec1 12944 if (die == NULL)
c906108c 12945 {
639d11d3
DC
12946 *new_info_ptr = cur_ptr;
12947 return first_die;
c906108c 12948 }
1d325ec1
DJ
12949
12950 if (!first_die)
12951 first_die = die;
c906108c 12952 else
1d325ec1
DJ
12953 last_sibling->sibling = die;
12954
12955 last_sibling = die;
c906108c 12956 }
c906108c
SS
12957}
12958
3019eac3
DE
12959/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12960 attributes.
12961 The caller is responsible for filling in the extra attributes
12962 and updating (*DIEP)->num_attrs.
12963 Set DIEP to point to a newly allocated die with its information,
12964 except for its child, sibling, and parent fields.
12965 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12966
12967static gdb_byte *
3019eac3
DE
12968read_full_die_1 (const struct die_reader_specs *reader,
12969 struct die_info **diep, gdb_byte *info_ptr,
12970 int *has_children, int num_extra_attrs)
93311388 12971{
b64f50a1
JK
12972 unsigned int abbrev_number, bytes_read, i;
12973 sect_offset offset;
93311388
DE
12974 struct abbrev_info *abbrev;
12975 struct die_info *die;
12976 struct dwarf2_cu *cu = reader->cu;
12977 bfd *abfd = reader->abfd;
12978
b64f50a1 12979 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12980 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12981 info_ptr += bytes_read;
12982 if (!abbrev_number)
12983 {
12984 *diep = NULL;
12985 *has_children = 0;
12986 return info_ptr;
12987 }
12988
433df2d4 12989 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12990 if (!abbrev)
348e048f
DE
12991 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12992 abbrev_number,
12993 bfd_get_filename (abfd));
12994
3019eac3 12995 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12996 die->offset = offset;
12997 die->tag = abbrev->tag;
12998 die->abbrev = abbrev_number;
12999
3019eac3
DE
13000 /* Make the result usable.
13001 The caller needs to update num_attrs after adding the extra
13002 attributes. */
93311388
DE
13003 die->num_attrs = abbrev->num_attrs;
13004
13005 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13006 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13007 info_ptr);
93311388
DE
13008
13009 *diep = die;
13010 *has_children = abbrev->has_children;
13011 return info_ptr;
13012}
13013
3019eac3
DE
13014/* Read a die and all its attributes.
13015 Set DIEP to point to a newly allocated die with its information,
13016 except for its child, sibling, and parent fields.
13017 Set HAS_CHILDREN to tell whether the die has children or not. */
13018
13019static gdb_byte *
13020read_full_die (const struct die_reader_specs *reader,
13021 struct die_info **diep, gdb_byte *info_ptr,
13022 int *has_children)
13023{
13024 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13025}
433df2d4
DE
13026\f
13027/* Abbreviation tables.
3019eac3 13028
433df2d4 13029 In DWARF version 2, the description of the debugging information is
c906108c
SS
13030 stored in a separate .debug_abbrev section. Before we read any
13031 dies from a section we read in all abbreviations and install them
433df2d4
DE
13032 in a hash table. */
13033
13034/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13035
13036static struct abbrev_info *
13037abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13038{
13039 struct abbrev_info *abbrev;
13040
13041 abbrev = (struct abbrev_info *)
13042 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13043 memset (abbrev, 0, sizeof (struct abbrev_info));
13044 return abbrev;
13045}
13046
13047/* Add an abbreviation to the table. */
c906108c
SS
13048
13049static void
433df2d4
DE
13050abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13051 unsigned int abbrev_number,
13052 struct abbrev_info *abbrev)
13053{
13054 unsigned int hash_number;
13055
13056 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13057 abbrev->next = abbrev_table->abbrevs[hash_number];
13058 abbrev_table->abbrevs[hash_number] = abbrev;
13059}
dee91e82 13060
433df2d4
DE
13061/* Look up an abbrev in the table.
13062 Returns NULL if the abbrev is not found. */
13063
13064static struct abbrev_info *
13065abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13066 unsigned int abbrev_number)
c906108c 13067{
433df2d4
DE
13068 unsigned int hash_number;
13069 struct abbrev_info *abbrev;
13070
13071 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13072 abbrev = abbrev_table->abbrevs[hash_number];
13073
13074 while (abbrev)
13075 {
13076 if (abbrev->number == abbrev_number)
13077 return abbrev;
13078 abbrev = abbrev->next;
13079 }
13080 return NULL;
13081}
13082
13083/* Read in an abbrev table. */
13084
13085static struct abbrev_table *
13086abbrev_table_read_table (struct dwarf2_section_info *section,
13087 sect_offset offset)
13088{
13089 struct objfile *objfile = dwarf2_per_objfile->objfile;
13090 bfd *abfd = section->asection->owner;
13091 struct abbrev_table *abbrev_table;
fe1b8b76 13092 gdb_byte *abbrev_ptr;
c906108c
SS
13093 struct abbrev_info *cur_abbrev;
13094 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13095 unsigned int abbrev_form;
f3dd6933
DJ
13096 struct attr_abbrev *cur_attrs;
13097 unsigned int allocated_attrs;
c906108c 13098
433df2d4 13099 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13100 abbrev_table->offset = offset;
433df2d4
DE
13101 obstack_init (&abbrev_table->abbrev_obstack);
13102 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13103 (ABBREV_HASH_SIZE
13104 * sizeof (struct abbrev_info *)));
13105 memset (abbrev_table->abbrevs, 0,
13106 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13107
433df2d4
DE
13108 dwarf2_read_section (objfile, section);
13109 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13110 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13111 abbrev_ptr += bytes_read;
13112
f3dd6933
DJ
13113 allocated_attrs = ATTR_ALLOC_CHUNK;
13114 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13115
0963b4bd 13116 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13117 while (abbrev_number)
13118 {
433df2d4 13119 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13120
13121 /* read in abbrev header */
13122 cur_abbrev->number = abbrev_number;
13123 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13124 abbrev_ptr += bytes_read;
13125 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13126 abbrev_ptr += 1;
13127
13128 /* now read in declarations */
13129 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13130 abbrev_ptr += bytes_read;
13131 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13132 abbrev_ptr += bytes_read;
13133 while (abbrev_name)
13134 {
f3dd6933 13135 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13136 {
f3dd6933
DJ
13137 allocated_attrs += ATTR_ALLOC_CHUNK;
13138 cur_attrs
13139 = xrealloc (cur_attrs, (allocated_attrs
13140 * sizeof (struct attr_abbrev)));
c906108c 13141 }
ae038cb0 13142
f3dd6933
DJ
13143 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13144 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13145 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13146 abbrev_ptr += bytes_read;
13147 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13148 abbrev_ptr += bytes_read;
13149 }
13150
433df2d4 13151 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13152 (cur_abbrev->num_attrs
13153 * sizeof (struct attr_abbrev)));
13154 memcpy (cur_abbrev->attrs, cur_attrs,
13155 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13156
433df2d4 13157 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13158
13159 /* Get next abbreviation.
13160 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13161 always properly terminated with an abbrev number of 0.
13162 Exit loop if we encounter an abbreviation which we have
13163 already read (which means we are about to read the abbreviations
13164 for the next compile unit) or if the end of the abbreviation
13165 table is reached. */
433df2d4 13166 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13167 break;
13168 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13169 abbrev_ptr += bytes_read;
433df2d4 13170 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13171 break;
13172 }
f3dd6933
DJ
13173
13174 xfree (cur_attrs);
433df2d4 13175 return abbrev_table;
c906108c
SS
13176}
13177
433df2d4 13178/* Free the resources held by ABBREV_TABLE. */
c906108c 13179
c906108c 13180static void
433df2d4 13181abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13182{
433df2d4
DE
13183 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13184 xfree (abbrev_table);
c906108c
SS
13185}
13186
f4dc4d17
DE
13187/* Same as abbrev_table_free but as a cleanup.
13188 We pass in a pointer to the pointer to the table so that we can
13189 set the pointer to NULL when we're done. It also simplifies
13190 build_type_unit_groups. */
13191
13192static void
13193abbrev_table_free_cleanup (void *table_ptr)
13194{
13195 struct abbrev_table **abbrev_table_ptr = table_ptr;
13196
13197 if (*abbrev_table_ptr != NULL)
13198 abbrev_table_free (*abbrev_table_ptr);
13199 *abbrev_table_ptr = NULL;
13200}
13201
433df2d4
DE
13202/* Read the abbrev table for CU from ABBREV_SECTION. */
13203
13204static void
13205dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13206 struct dwarf2_section_info *abbrev_section)
c906108c 13207{
433df2d4
DE
13208 cu->abbrev_table =
13209 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13210}
c906108c 13211
433df2d4 13212/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13213
433df2d4
DE
13214static void
13215dwarf2_free_abbrev_table (void *ptr_to_cu)
13216{
13217 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13218
433df2d4
DE
13219 abbrev_table_free (cu->abbrev_table);
13220 /* Set this to NULL so that we SEGV if we try to read it later,
13221 and also because free_comp_unit verifies this is NULL. */
13222 cu->abbrev_table = NULL;
13223}
13224\f
72bf9492
DJ
13225/* Returns nonzero if TAG represents a type that we might generate a partial
13226 symbol for. */
13227
13228static int
13229is_type_tag_for_partial (int tag)
13230{
13231 switch (tag)
13232 {
13233#if 0
13234 /* Some types that would be reasonable to generate partial symbols for,
13235 that we don't at present. */
13236 case DW_TAG_array_type:
13237 case DW_TAG_file_type:
13238 case DW_TAG_ptr_to_member_type:
13239 case DW_TAG_set_type:
13240 case DW_TAG_string_type:
13241 case DW_TAG_subroutine_type:
13242#endif
13243 case DW_TAG_base_type:
13244 case DW_TAG_class_type:
680b30c7 13245 case DW_TAG_interface_type:
72bf9492
DJ
13246 case DW_TAG_enumeration_type:
13247 case DW_TAG_structure_type:
13248 case DW_TAG_subrange_type:
13249 case DW_TAG_typedef:
13250 case DW_TAG_union_type:
13251 return 1;
13252 default:
13253 return 0;
13254 }
13255}
13256
13257/* Load all DIEs that are interesting for partial symbols into memory. */
13258
13259static struct partial_die_info *
dee91e82
DE
13260load_partial_dies (const struct die_reader_specs *reader,
13261 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13262{
dee91e82 13263 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13264 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13265 struct partial_die_info *part_die;
13266 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13267 struct abbrev_info *abbrev;
13268 unsigned int bytes_read;
5afb4e99 13269 unsigned int load_all = 0;
72bf9492
DJ
13270 int nesting_level = 1;
13271
13272 parent_die = NULL;
13273 last_die = NULL;
13274
7adf1e79
DE
13275 gdb_assert (cu->per_cu != NULL);
13276 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13277 load_all = 1;
13278
72bf9492
DJ
13279 cu->partial_dies
13280 = htab_create_alloc_ex (cu->header.length / 12,
13281 partial_die_hash,
13282 partial_die_eq,
13283 NULL,
13284 &cu->comp_unit_obstack,
13285 hashtab_obstack_allocate,
13286 dummy_obstack_deallocate);
13287
13288 part_die = obstack_alloc (&cu->comp_unit_obstack,
13289 sizeof (struct partial_die_info));
13290
13291 while (1)
13292 {
13293 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13294
13295 /* A NULL abbrev means the end of a series of children. */
13296 if (abbrev == NULL)
13297 {
13298 if (--nesting_level == 0)
13299 {
13300 /* PART_DIE was probably the last thing allocated on the
13301 comp_unit_obstack, so we could call obstack_free
13302 here. We don't do that because the waste is small,
13303 and will be cleaned up when we're done with this
13304 compilation unit. This way, we're also more robust
13305 against other users of the comp_unit_obstack. */
13306 return first_die;
13307 }
13308 info_ptr += bytes_read;
13309 last_die = parent_die;
13310 parent_die = parent_die->die_parent;
13311 continue;
13312 }
13313
98bfdba5
PA
13314 /* Check for template arguments. We never save these; if
13315 they're seen, we just mark the parent, and go on our way. */
13316 if (parent_die != NULL
13317 && cu->language == language_cplus
13318 && (abbrev->tag == DW_TAG_template_type_param
13319 || abbrev->tag == DW_TAG_template_value_param))
13320 {
13321 parent_die->has_template_arguments = 1;
13322
13323 if (!load_all)
13324 {
13325 /* We don't need a partial DIE for the template argument. */
dee91e82 13326 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13327 continue;
13328 }
13329 }
13330
0d99eb77 13331 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13332 Skip their other children. */
13333 if (!load_all
13334 && cu->language == language_cplus
13335 && parent_die != NULL
13336 && parent_die->tag == DW_TAG_subprogram)
13337 {
dee91e82 13338 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13339 continue;
13340 }
13341
5afb4e99
DJ
13342 /* Check whether this DIE is interesting enough to save. Normally
13343 we would not be interested in members here, but there may be
13344 later variables referencing them via DW_AT_specification (for
13345 static members). */
13346 if (!load_all
13347 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13348 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13349 && abbrev->tag != DW_TAG_enumerator
13350 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13351 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13352 && abbrev->tag != DW_TAG_variable
5afb4e99 13353 && abbrev->tag != DW_TAG_namespace
f55ee35c 13354 && abbrev->tag != DW_TAG_module
95554aad
TT
13355 && abbrev->tag != DW_TAG_member
13356 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13357 {
13358 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13359 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13360 continue;
13361 }
13362
dee91e82
DE
13363 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13364 info_ptr);
72bf9492
DJ
13365
13366 /* This two-pass algorithm for processing partial symbols has a
13367 high cost in cache pressure. Thus, handle some simple cases
13368 here which cover the majority of C partial symbols. DIEs
13369 which neither have specification tags in them, nor could have
13370 specification tags elsewhere pointing at them, can simply be
13371 processed and discarded.
13372
13373 This segment is also optional; scan_partial_symbols and
13374 add_partial_symbol will handle these DIEs if we chain
13375 them in normally. When compilers which do not emit large
13376 quantities of duplicate debug information are more common,
13377 this code can probably be removed. */
13378
13379 /* Any complete simple types at the top level (pretty much all
13380 of them, for a language without namespaces), can be processed
13381 directly. */
13382 if (parent_die == NULL
13383 && part_die->has_specification == 0
13384 && part_die->is_declaration == 0
d8228535 13385 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13386 || part_die->tag == DW_TAG_base_type
13387 || part_die->tag == DW_TAG_subrange_type))
13388 {
13389 if (building_psymtab && part_die->name != NULL)
04a679b8 13390 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13391 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13392 &objfile->static_psymbols,
13393 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13394 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13395 continue;
13396 }
13397
d8228535
JK
13398 /* The exception for DW_TAG_typedef with has_children above is
13399 a workaround of GCC PR debug/47510. In the case of this complaint
13400 type_name_no_tag_or_error will error on such types later.
13401
13402 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13403 it could not find the child DIEs referenced later, this is checked
13404 above. In correct DWARF DW_TAG_typedef should have no children. */
13405
13406 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13407 complaint (&symfile_complaints,
13408 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13409 "- DIE at 0x%x [in module %s]"),
b64f50a1 13410 part_die->offset.sect_off, objfile->name);
d8228535 13411
72bf9492
DJ
13412 /* If we're at the second level, and we're an enumerator, and
13413 our parent has no specification (meaning possibly lives in a
13414 namespace elsewhere), then we can add the partial symbol now
13415 instead of queueing it. */
13416 if (part_die->tag == DW_TAG_enumerator
13417 && parent_die != NULL
13418 && parent_die->die_parent == NULL
13419 && parent_die->tag == DW_TAG_enumeration_type
13420 && parent_die->has_specification == 0)
13421 {
13422 if (part_die->name == NULL)
3e43a32a
MS
13423 complaint (&symfile_complaints,
13424 _("malformed enumerator DIE ignored"));
72bf9492 13425 else if (building_psymtab)
04a679b8 13426 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13427 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13428 (cu->language == language_cplus
13429 || cu->language == language_java)
bb5ed363
DE
13430 ? &objfile->global_psymbols
13431 : &objfile->static_psymbols,
13432 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13433
dee91e82 13434 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13435 continue;
13436 }
13437
13438 /* We'll save this DIE so link it in. */
13439 part_die->die_parent = parent_die;
13440 part_die->die_sibling = NULL;
13441 part_die->die_child = NULL;
13442
13443 if (last_die && last_die == parent_die)
13444 last_die->die_child = part_die;
13445 else if (last_die)
13446 last_die->die_sibling = part_die;
13447
13448 last_die = part_die;
13449
13450 if (first_die == NULL)
13451 first_die = part_die;
13452
13453 /* Maybe add the DIE to the hash table. Not all DIEs that we
13454 find interesting need to be in the hash table, because we
13455 also have the parent/sibling/child chains; only those that we
13456 might refer to by offset later during partial symbol reading.
13457
13458 For now this means things that might have be the target of a
13459 DW_AT_specification, DW_AT_abstract_origin, or
13460 DW_AT_extension. DW_AT_extension will refer only to
13461 namespaces; DW_AT_abstract_origin refers to functions (and
13462 many things under the function DIE, but we do not recurse
13463 into function DIEs during partial symbol reading) and
13464 possibly variables as well; DW_AT_specification refers to
13465 declarations. Declarations ought to have the DW_AT_declaration
13466 flag. It happens that GCC forgets to put it in sometimes, but
13467 only for functions, not for types.
13468
13469 Adding more things than necessary to the hash table is harmless
13470 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13471 wasted time in find_partial_die, when we reread the compilation
13472 unit with load_all_dies set. */
72bf9492 13473
5afb4e99 13474 if (load_all
72929c62 13475 || abbrev->tag == DW_TAG_constant
5afb4e99 13476 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13477 || abbrev->tag == DW_TAG_variable
13478 || abbrev->tag == DW_TAG_namespace
13479 || part_die->is_declaration)
13480 {
13481 void **slot;
13482
13483 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13484 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13485 *slot = part_die;
13486 }
13487
13488 part_die = obstack_alloc (&cu->comp_unit_obstack,
13489 sizeof (struct partial_die_info));
13490
13491 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13492 we have no reason to follow the children of structures; for other
98bfdba5
PA
13493 languages we have to, so that we can get at method physnames
13494 to infer fully qualified class names, for DW_AT_specification,
13495 and for C++ template arguments. For C++, we also look one level
13496 inside functions to find template arguments (if the name of the
13497 function does not already contain the template arguments).
bc30ff58
JB
13498
13499 For Ada, we need to scan the children of subprograms and lexical
13500 blocks as well because Ada allows the definition of nested
13501 entities that could be interesting for the debugger, such as
13502 nested subprograms for instance. */
72bf9492 13503 if (last_die->has_children
5afb4e99
DJ
13504 && (load_all
13505 || last_die->tag == DW_TAG_namespace
f55ee35c 13506 || last_die->tag == DW_TAG_module
72bf9492 13507 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13508 || (cu->language == language_cplus
13509 && last_die->tag == DW_TAG_subprogram
13510 && (last_die->name == NULL
13511 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13512 || (cu->language != language_c
13513 && (last_die->tag == DW_TAG_class_type
680b30c7 13514 || last_die->tag == DW_TAG_interface_type
72bf9492 13515 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13516 || last_die->tag == DW_TAG_union_type))
13517 || (cu->language == language_ada
13518 && (last_die->tag == DW_TAG_subprogram
13519 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13520 {
13521 nesting_level++;
13522 parent_die = last_die;
13523 continue;
13524 }
13525
13526 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13527 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13528
13529 /* Back to the top, do it again. */
13530 }
13531}
13532
c906108c
SS
13533/* Read a minimal amount of information into the minimal die structure. */
13534
fe1b8b76 13535static gdb_byte *
dee91e82
DE
13536read_partial_die (const struct die_reader_specs *reader,
13537 struct partial_die_info *part_die,
13538 struct abbrev_info *abbrev, unsigned int abbrev_len,
13539 gdb_byte *info_ptr)
c906108c 13540{
dee91e82 13541 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13542 struct objfile *objfile = cu->objfile;
dee91e82 13543 gdb_byte *buffer = reader->buffer;
fa238c03 13544 unsigned int i;
c906108c 13545 struct attribute attr;
c5aa993b 13546 int has_low_pc_attr = 0;
c906108c 13547 int has_high_pc_attr = 0;
91da1414 13548 int high_pc_relative = 0;
c906108c 13549
72bf9492 13550 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13551
b64f50a1 13552 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13553
13554 info_ptr += abbrev_len;
13555
13556 if (abbrev == NULL)
13557 return info_ptr;
13558
c906108c
SS
13559 part_die->tag = abbrev->tag;
13560 part_die->has_children = abbrev->has_children;
c906108c
SS
13561
13562 for (i = 0; i < abbrev->num_attrs; ++i)
13563 {
dee91e82 13564 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13565
13566 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13567 partial symbol table. */
c906108c
SS
13568 switch (attr.name)
13569 {
13570 case DW_AT_name:
71c25dea
TT
13571 switch (part_die->tag)
13572 {
13573 case DW_TAG_compile_unit:
95554aad 13574 case DW_TAG_partial_unit:
348e048f 13575 case DW_TAG_type_unit:
71c25dea
TT
13576 /* Compilation units have a DW_AT_name that is a filename, not
13577 a source language identifier. */
13578 case DW_TAG_enumeration_type:
13579 case DW_TAG_enumerator:
13580 /* These tags always have simple identifiers already; no need
13581 to canonicalize them. */
13582 part_die->name = DW_STRING (&attr);
13583 break;
13584 default:
13585 part_die->name
13586 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13587 &objfile->objfile_obstack);
71c25dea
TT
13588 break;
13589 }
c906108c 13590 break;
31ef98ae 13591 case DW_AT_linkage_name:
c906108c 13592 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13593 /* Note that both forms of linkage name might appear. We
13594 assume they will be the same, and we only store the last
13595 one we see. */
94af9270
KS
13596 if (cu->language == language_ada)
13597 part_die->name = DW_STRING (&attr);
abc72ce4 13598 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13599 break;
13600 case DW_AT_low_pc:
13601 has_low_pc_attr = 1;
13602 part_die->lowpc = DW_ADDR (&attr);
13603 break;
13604 case DW_AT_high_pc:
13605 has_high_pc_attr = 1;
3019eac3
DE
13606 if (attr.form == DW_FORM_addr
13607 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13608 part_die->highpc = DW_ADDR (&attr);
13609 else
13610 {
13611 high_pc_relative = 1;
13612 part_die->highpc = DW_UNSND (&attr);
13613 }
c906108c
SS
13614 break;
13615 case DW_AT_location:
0963b4bd 13616 /* Support the .debug_loc offsets. */
8e19ed76
PS
13617 if (attr_form_is_block (&attr))
13618 {
95554aad 13619 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13620 }
3690dd37 13621 else if (attr_form_is_section_offset (&attr))
8e19ed76 13622 {
4d3c2250 13623 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13624 }
13625 else
13626 {
4d3c2250
KB
13627 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13628 "partial symbol information");
8e19ed76 13629 }
c906108c 13630 break;
c906108c
SS
13631 case DW_AT_external:
13632 part_die->is_external = DW_UNSND (&attr);
13633 break;
13634 case DW_AT_declaration:
13635 part_die->is_declaration = DW_UNSND (&attr);
13636 break;
13637 case DW_AT_type:
13638 part_die->has_type = 1;
13639 break;
13640 case DW_AT_abstract_origin:
13641 case DW_AT_specification:
72bf9492
DJ
13642 case DW_AT_extension:
13643 part_die->has_specification = 1;
c764a876 13644 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13645 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13646 || cu->per_cu->is_dwz);
c906108c
SS
13647 break;
13648 case DW_AT_sibling:
13649 /* Ignore absolute siblings, they might point outside of
13650 the current compile unit. */
13651 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13652 complaint (&symfile_complaints,
13653 _("ignoring absolute DW_AT_sibling"));
c906108c 13654 else
b64f50a1 13655 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13656 break;
fa4028e9
JB
13657 case DW_AT_byte_size:
13658 part_die->has_byte_size = 1;
13659 break;
68511cec
CES
13660 case DW_AT_calling_convention:
13661 /* DWARF doesn't provide a way to identify a program's source-level
13662 entry point. DW_AT_calling_convention attributes are only meant
13663 to describe functions' calling conventions.
13664
13665 However, because it's a necessary piece of information in
13666 Fortran, and because DW_CC_program is the only piece of debugging
13667 information whose definition refers to a 'main program' at all,
13668 several compilers have begun marking Fortran main programs with
13669 DW_CC_program --- even when those functions use the standard
13670 calling conventions.
13671
13672 So until DWARF specifies a way to provide this information and
13673 compilers pick up the new representation, we'll support this
13674 practice. */
13675 if (DW_UNSND (&attr) == DW_CC_program
13676 && cu->language == language_fortran)
01f8c46d
JK
13677 {
13678 set_main_name (part_die->name);
13679
13680 /* As this DIE has a static linkage the name would be difficult
13681 to look up later. */
13682 language_of_main = language_fortran;
13683 }
68511cec 13684 break;
481860b3
GB
13685 case DW_AT_inline:
13686 if (DW_UNSND (&attr) == DW_INL_inlined
13687 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13688 part_die->may_be_inlined = 1;
13689 break;
95554aad
TT
13690
13691 case DW_AT_import:
13692 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13693 {
13694 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13695 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13696 || cu->per_cu->is_dwz);
13697 }
95554aad
TT
13698 break;
13699
c906108c
SS
13700 default:
13701 break;
13702 }
13703 }
13704
91da1414
MW
13705 if (high_pc_relative)
13706 part_die->highpc += part_die->lowpc;
13707
9373cf26
JK
13708 if (has_low_pc_attr && has_high_pc_attr)
13709 {
13710 /* When using the GNU linker, .gnu.linkonce. sections are used to
13711 eliminate duplicate copies of functions and vtables and such.
13712 The linker will arbitrarily choose one and discard the others.
13713 The AT_*_pc values for such functions refer to local labels in
13714 these sections. If the section from that file was discarded, the
13715 labels are not in the output, so the relocs get a value of 0.
13716 If this is a discarded function, mark the pc bounds as invalid,
13717 so that GDB will ignore it. */
13718 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13719 {
bb5ed363 13720 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13721
13722 complaint (&symfile_complaints,
13723 _("DW_AT_low_pc %s is zero "
13724 "for DIE at 0x%x [in module %s]"),
13725 paddress (gdbarch, part_die->lowpc),
b64f50a1 13726 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13727 }
13728 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13729 else if (part_die->lowpc >= part_die->highpc)
13730 {
bb5ed363 13731 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13732
13733 complaint (&symfile_complaints,
13734 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13735 "for DIE at 0x%x [in module %s]"),
13736 paddress (gdbarch, part_die->lowpc),
13737 paddress (gdbarch, part_die->highpc),
b64f50a1 13738 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13739 }
13740 else
13741 part_die->has_pc_info = 1;
13742 }
85cbf3d3 13743
c906108c
SS
13744 return info_ptr;
13745}
13746
72bf9492
DJ
13747/* Find a cached partial DIE at OFFSET in CU. */
13748
13749static struct partial_die_info *
b64f50a1 13750find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13751{
13752 struct partial_die_info *lookup_die = NULL;
13753 struct partial_die_info part_die;
13754
13755 part_die.offset = offset;
b64f50a1
JK
13756 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13757 offset.sect_off);
72bf9492 13758
72bf9492
DJ
13759 return lookup_die;
13760}
13761
348e048f
DE
13762/* Find a partial DIE at OFFSET, which may or may not be in CU,
13763 except in the case of .debug_types DIEs which do not reference
13764 outside their CU (they do however referencing other types via
55f1336d 13765 DW_FORM_ref_sig8). */
72bf9492
DJ
13766
13767static struct partial_die_info *
36586728 13768find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13769{
bb5ed363 13770 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13771 struct dwarf2_per_cu_data *per_cu = NULL;
13772 struct partial_die_info *pd = NULL;
72bf9492 13773
36586728
TT
13774 if (offset_in_dwz == cu->per_cu->is_dwz
13775 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13776 {
13777 pd = find_partial_die_in_comp_unit (offset, cu);
13778 if (pd != NULL)
13779 return pd;
0d99eb77
DE
13780 /* We missed recording what we needed.
13781 Load all dies and try again. */
13782 per_cu = cu->per_cu;
5afb4e99 13783 }
0d99eb77
DE
13784 else
13785 {
13786 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13787 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13788 {
13789 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13790 " external reference to offset 0x%lx [in module %s].\n"),
13791 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13792 bfd_get_filename (objfile->obfd));
13793 }
36586728
TT
13794 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13795 objfile);
72bf9492 13796
0d99eb77
DE
13797 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13798 load_partial_comp_unit (per_cu);
ae038cb0 13799
0d99eb77
DE
13800 per_cu->cu->last_used = 0;
13801 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13802 }
5afb4e99 13803
dee91e82
DE
13804 /* If we didn't find it, and not all dies have been loaded,
13805 load them all and try again. */
13806
5afb4e99
DJ
13807 if (pd == NULL && per_cu->load_all_dies == 0)
13808 {
5afb4e99 13809 per_cu->load_all_dies = 1;
fd820528
DE
13810
13811 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13812 THIS_CU->cu may already be in use. So we can't just free it and
13813 replace its DIEs with the ones we read in. Instead, we leave those
13814 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13815 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13816 set. */
dee91e82 13817 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13818
13819 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13820 }
13821
13822 if (pd == NULL)
13823 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13824 _("could not find partial DIE 0x%x "
13825 "in cache [from module %s]\n"),
b64f50a1 13826 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13827 return pd;
72bf9492
DJ
13828}
13829
abc72ce4
DE
13830/* See if we can figure out if the class lives in a namespace. We do
13831 this by looking for a member function; its demangled name will
13832 contain namespace info, if there is any. */
13833
13834static void
13835guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13836 struct dwarf2_cu *cu)
13837{
13838 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13839 what template types look like, because the demangler
13840 frequently doesn't give the same name as the debug info. We
13841 could fix this by only using the demangled name to get the
13842 prefix (but see comment in read_structure_type). */
13843
13844 struct partial_die_info *real_pdi;
13845 struct partial_die_info *child_pdi;
13846
13847 /* If this DIE (this DIE's specification, if any) has a parent, then
13848 we should not do this. We'll prepend the parent's fully qualified
13849 name when we create the partial symbol. */
13850
13851 real_pdi = struct_pdi;
13852 while (real_pdi->has_specification)
36586728
TT
13853 real_pdi = find_partial_die (real_pdi->spec_offset,
13854 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13855
13856 if (real_pdi->die_parent != NULL)
13857 return;
13858
13859 for (child_pdi = struct_pdi->die_child;
13860 child_pdi != NULL;
13861 child_pdi = child_pdi->die_sibling)
13862 {
13863 if (child_pdi->tag == DW_TAG_subprogram
13864 && child_pdi->linkage_name != NULL)
13865 {
13866 char *actual_class_name
13867 = language_class_name_from_physname (cu->language_defn,
13868 child_pdi->linkage_name);
13869 if (actual_class_name != NULL)
13870 {
13871 struct_pdi->name
13872 = obsavestring (actual_class_name,
13873 strlen (actual_class_name),
13874 &cu->objfile->objfile_obstack);
13875 xfree (actual_class_name);
13876 }
13877 break;
13878 }
13879 }
13880}
13881
72bf9492
DJ
13882/* Adjust PART_DIE before generating a symbol for it. This function
13883 may set the is_external flag or change the DIE's name. */
13884
13885static void
13886fixup_partial_die (struct partial_die_info *part_die,
13887 struct dwarf2_cu *cu)
13888{
abc72ce4
DE
13889 /* Once we've fixed up a die, there's no point in doing so again.
13890 This also avoids a memory leak if we were to call
13891 guess_partial_die_structure_name multiple times. */
13892 if (part_die->fixup_called)
13893 return;
13894
72bf9492
DJ
13895 /* If we found a reference attribute and the DIE has no name, try
13896 to find a name in the referred to DIE. */
13897
13898 if (part_die->name == NULL && part_die->has_specification)
13899 {
13900 struct partial_die_info *spec_die;
72bf9492 13901
36586728
TT
13902 spec_die = find_partial_die (part_die->spec_offset,
13903 part_die->spec_is_dwz, cu);
72bf9492 13904
10b3939b 13905 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13906
13907 if (spec_die->name)
13908 {
13909 part_die->name = spec_die->name;
13910
13911 /* Copy DW_AT_external attribute if it is set. */
13912 if (spec_die->is_external)
13913 part_die->is_external = spec_die->is_external;
13914 }
13915 }
13916
13917 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13918
13919 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13920 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13921
abc72ce4
DE
13922 /* If there is no parent die to provide a namespace, and there are
13923 children, see if we can determine the namespace from their linkage
122d1940 13924 name. */
abc72ce4 13925 if (cu->language == language_cplus
8b70b953 13926 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13927 && part_die->die_parent == NULL
13928 && part_die->has_children
13929 && (part_die->tag == DW_TAG_class_type
13930 || part_die->tag == DW_TAG_structure_type
13931 || part_die->tag == DW_TAG_union_type))
13932 guess_partial_die_structure_name (part_die, cu);
13933
53832f31
TT
13934 /* GCC might emit a nameless struct or union that has a linkage
13935 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13936 if (part_die->name == NULL
96408a79
SA
13937 && (part_die->tag == DW_TAG_class_type
13938 || part_die->tag == DW_TAG_interface_type
13939 || part_die->tag == DW_TAG_structure_type
13940 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13941 && part_die->linkage_name != NULL)
13942 {
13943 char *demangled;
13944
13945 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13946 if (demangled)
13947 {
96408a79
SA
13948 const char *base;
13949
13950 /* Strip any leading namespaces/classes, keep only the base name.
13951 DW_AT_name for named DIEs does not contain the prefixes. */
13952 base = strrchr (demangled, ':');
13953 if (base && base > demangled && base[-1] == ':')
13954 base++;
13955 else
13956 base = demangled;
13957
13958 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13959 &cu->objfile->objfile_obstack);
13960 xfree (demangled);
13961 }
13962 }
13963
abc72ce4 13964 part_die->fixup_called = 1;
72bf9492
DJ
13965}
13966
a8329558 13967/* Read an attribute value described by an attribute form. */
c906108c 13968
fe1b8b76 13969static gdb_byte *
dee91e82
DE
13970read_attribute_value (const struct die_reader_specs *reader,
13971 struct attribute *attr, unsigned form,
13972 gdb_byte *info_ptr)
c906108c 13973{
dee91e82
DE
13974 struct dwarf2_cu *cu = reader->cu;
13975 bfd *abfd = reader->abfd;
e7c27a73 13976 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13977 unsigned int bytes_read;
13978 struct dwarf_block *blk;
13979
a8329558
KW
13980 attr->form = form;
13981 switch (form)
c906108c 13982 {
c906108c 13983 case DW_FORM_ref_addr:
ae411497 13984 if (cu->header.version == 2)
4568ecf9 13985 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13986 else
4568ecf9
DE
13987 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13988 &cu->header, &bytes_read);
ae411497
TT
13989 info_ptr += bytes_read;
13990 break;
36586728
TT
13991 case DW_FORM_GNU_ref_alt:
13992 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13993 info_ptr += bytes_read;
13994 break;
ae411497 13995 case DW_FORM_addr:
e7c27a73 13996 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13997 info_ptr += bytes_read;
c906108c
SS
13998 break;
13999 case DW_FORM_block2:
7b5a2f43 14000 blk = dwarf_alloc_block (cu);
c906108c
SS
14001 blk->size = read_2_bytes (abfd, info_ptr);
14002 info_ptr += 2;
14003 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14004 info_ptr += blk->size;
14005 DW_BLOCK (attr) = blk;
14006 break;
14007 case DW_FORM_block4:
7b5a2f43 14008 blk = dwarf_alloc_block (cu);
c906108c
SS
14009 blk->size = read_4_bytes (abfd, info_ptr);
14010 info_ptr += 4;
14011 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14012 info_ptr += blk->size;
14013 DW_BLOCK (attr) = blk;
14014 break;
14015 case DW_FORM_data2:
14016 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14017 info_ptr += 2;
14018 break;
14019 case DW_FORM_data4:
14020 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14021 info_ptr += 4;
14022 break;
14023 case DW_FORM_data8:
14024 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14025 info_ptr += 8;
14026 break;
2dc7f7b3
TT
14027 case DW_FORM_sec_offset:
14028 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14029 info_ptr += bytes_read;
14030 break;
c906108c 14031 case DW_FORM_string:
9b1c24c8 14032 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14033 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14034 info_ptr += bytes_read;
14035 break;
4bdf3d34 14036 case DW_FORM_strp:
36586728
TT
14037 if (!cu->per_cu->is_dwz)
14038 {
14039 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14040 &bytes_read);
14041 DW_STRING_IS_CANONICAL (attr) = 0;
14042 info_ptr += bytes_read;
14043 break;
14044 }
14045 /* FALLTHROUGH */
14046 case DW_FORM_GNU_strp_alt:
14047 {
14048 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14049 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14050 &bytes_read);
14051
14052 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14053 DW_STRING_IS_CANONICAL (attr) = 0;
14054 info_ptr += bytes_read;
14055 }
4bdf3d34 14056 break;
2dc7f7b3 14057 case DW_FORM_exprloc:
c906108c 14058 case DW_FORM_block:
7b5a2f43 14059 blk = dwarf_alloc_block (cu);
c906108c
SS
14060 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14061 info_ptr += bytes_read;
14062 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14063 info_ptr += blk->size;
14064 DW_BLOCK (attr) = blk;
14065 break;
14066 case DW_FORM_block1:
7b5a2f43 14067 blk = dwarf_alloc_block (cu);
c906108c
SS
14068 blk->size = read_1_byte (abfd, info_ptr);
14069 info_ptr += 1;
14070 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14071 info_ptr += blk->size;
14072 DW_BLOCK (attr) = blk;
14073 break;
14074 case DW_FORM_data1:
14075 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14076 info_ptr += 1;
14077 break;
14078 case DW_FORM_flag:
14079 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14080 info_ptr += 1;
14081 break;
2dc7f7b3
TT
14082 case DW_FORM_flag_present:
14083 DW_UNSND (attr) = 1;
14084 break;
c906108c
SS
14085 case DW_FORM_sdata:
14086 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14087 info_ptr += bytes_read;
14088 break;
14089 case DW_FORM_udata:
14090 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14091 info_ptr += bytes_read;
14092 break;
14093 case DW_FORM_ref1:
4568ecf9
DE
14094 DW_UNSND (attr) = (cu->header.offset.sect_off
14095 + read_1_byte (abfd, info_ptr));
c906108c
SS
14096 info_ptr += 1;
14097 break;
14098 case DW_FORM_ref2:
4568ecf9
DE
14099 DW_UNSND (attr) = (cu->header.offset.sect_off
14100 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14101 info_ptr += 2;
14102 break;
14103 case DW_FORM_ref4:
4568ecf9
DE
14104 DW_UNSND (attr) = (cu->header.offset.sect_off
14105 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14106 info_ptr += 4;
14107 break;
613e1657 14108 case DW_FORM_ref8:
4568ecf9
DE
14109 DW_UNSND (attr) = (cu->header.offset.sect_off
14110 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14111 info_ptr += 8;
14112 break;
55f1336d 14113 case DW_FORM_ref_sig8:
348e048f
DE
14114 /* Convert the signature to something we can record in DW_UNSND
14115 for later lookup.
14116 NOTE: This is NULL if the type wasn't found. */
14117 DW_SIGNATURED_TYPE (attr) =
e319fa28 14118 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14119 info_ptr += 8;
14120 break;
c906108c 14121 case DW_FORM_ref_udata:
4568ecf9
DE
14122 DW_UNSND (attr) = (cu->header.offset.sect_off
14123 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14124 info_ptr += bytes_read;
14125 break;
c906108c 14126 case DW_FORM_indirect:
a8329558
KW
14127 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14128 info_ptr += bytes_read;
dee91e82 14129 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14130 break;
3019eac3
DE
14131 case DW_FORM_GNU_addr_index:
14132 if (reader->dwo_file == NULL)
14133 {
14134 /* For now flag a hard error.
14135 Later we can turn this into a complaint. */
14136 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14137 dwarf_form_name (form),
14138 bfd_get_filename (abfd));
14139 }
14140 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14141 info_ptr += bytes_read;
14142 break;
14143 case DW_FORM_GNU_str_index:
14144 if (reader->dwo_file == NULL)
14145 {
14146 /* For now flag a hard error.
14147 Later we can turn this into a complaint if warranted. */
14148 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14149 dwarf_form_name (form),
14150 bfd_get_filename (abfd));
14151 }
14152 {
14153 ULONGEST str_index =
14154 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14155
14156 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14157 DW_STRING_IS_CANONICAL (attr) = 0;
14158 info_ptr += bytes_read;
14159 }
14160 break;
c906108c 14161 default:
8a3fe4f8 14162 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14163 dwarf_form_name (form),
14164 bfd_get_filename (abfd));
c906108c 14165 }
28e94949 14166
36586728
TT
14167 /* Super hack. */
14168 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14169 attr->form = DW_FORM_GNU_ref_alt;
14170
28e94949
JB
14171 /* We have seen instances where the compiler tried to emit a byte
14172 size attribute of -1 which ended up being encoded as an unsigned
14173 0xffffffff. Although 0xffffffff is technically a valid size value,
14174 an object of this size seems pretty unlikely so we can relatively
14175 safely treat these cases as if the size attribute was invalid and
14176 treat them as zero by default. */
14177 if (attr->name == DW_AT_byte_size
14178 && form == DW_FORM_data4
14179 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14180 {
14181 complaint
14182 (&symfile_complaints,
43bbcdc2
PH
14183 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14184 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14185 DW_UNSND (attr) = 0;
14186 }
28e94949 14187
c906108c
SS
14188 return info_ptr;
14189}
14190
a8329558
KW
14191/* Read an attribute described by an abbreviated attribute. */
14192
fe1b8b76 14193static gdb_byte *
dee91e82
DE
14194read_attribute (const struct die_reader_specs *reader,
14195 struct attribute *attr, struct attr_abbrev *abbrev,
14196 gdb_byte *info_ptr)
a8329558
KW
14197{
14198 attr->name = abbrev->name;
dee91e82 14199 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14200}
14201
0963b4bd 14202/* Read dwarf information from a buffer. */
c906108c
SS
14203
14204static unsigned int
a1855c1d 14205read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14206{
fe1b8b76 14207 return bfd_get_8 (abfd, buf);
c906108c
SS
14208}
14209
14210static int
a1855c1d 14211read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14212{
fe1b8b76 14213 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14214}
14215
14216static unsigned int
a1855c1d 14217read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14218{
fe1b8b76 14219 return bfd_get_16 (abfd, buf);
c906108c
SS
14220}
14221
21ae7a4d 14222static int
a1855c1d 14223read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14224{
14225 return bfd_get_signed_16 (abfd, buf);
14226}
14227
c906108c 14228static unsigned int
a1855c1d 14229read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14230{
fe1b8b76 14231 return bfd_get_32 (abfd, buf);
c906108c
SS
14232}
14233
21ae7a4d 14234static int
a1855c1d 14235read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14236{
14237 return bfd_get_signed_32 (abfd, buf);
14238}
14239
93311388 14240static ULONGEST
a1855c1d 14241read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14242{
fe1b8b76 14243 return bfd_get_64 (abfd, buf);
c906108c
SS
14244}
14245
14246static CORE_ADDR
fe1b8b76 14247read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14248 unsigned int *bytes_read)
c906108c 14249{
e7c27a73 14250 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14251 CORE_ADDR retval = 0;
14252
107d2387 14253 if (cu_header->signed_addr_p)
c906108c 14254 {
107d2387
AC
14255 switch (cu_header->addr_size)
14256 {
14257 case 2:
fe1b8b76 14258 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14259 break;
14260 case 4:
fe1b8b76 14261 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14262 break;
14263 case 8:
fe1b8b76 14264 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14265 break;
14266 default:
8e65ff28 14267 internal_error (__FILE__, __LINE__,
e2e0b3e5 14268 _("read_address: bad switch, signed [in module %s]"),
659b0389 14269 bfd_get_filename (abfd));
107d2387
AC
14270 }
14271 }
14272 else
14273 {
14274 switch (cu_header->addr_size)
14275 {
14276 case 2:
fe1b8b76 14277 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14278 break;
14279 case 4:
fe1b8b76 14280 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14281 break;
14282 case 8:
fe1b8b76 14283 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14284 break;
14285 default:
8e65ff28 14286 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14287 _("read_address: bad switch, "
14288 "unsigned [in module %s]"),
659b0389 14289 bfd_get_filename (abfd));
107d2387 14290 }
c906108c 14291 }
64367e0a 14292
107d2387
AC
14293 *bytes_read = cu_header->addr_size;
14294 return retval;
c906108c
SS
14295}
14296
f7ef9339 14297/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14298 specification allows the initial length to take up either 4 bytes
14299 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14300 bytes describe the length and all offsets will be 8 bytes in length
14301 instead of 4.
14302
f7ef9339
KB
14303 An older, non-standard 64-bit format is also handled by this
14304 function. The older format in question stores the initial length
14305 as an 8-byte quantity without an escape value. Lengths greater
14306 than 2^32 aren't very common which means that the initial 4 bytes
14307 is almost always zero. Since a length value of zero doesn't make
14308 sense for the 32-bit format, this initial zero can be considered to
14309 be an escape value which indicates the presence of the older 64-bit
14310 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14311 greater than 4GB. If it becomes necessary to handle lengths
14312 somewhat larger than 4GB, we could allow other small values (such
14313 as the non-sensical values of 1, 2, and 3) to also be used as
14314 escape values indicating the presence of the old format.
f7ef9339 14315
917c78fc
MK
14316 The value returned via bytes_read should be used to increment the
14317 relevant pointer after calling read_initial_length().
c764a876 14318
613e1657
KB
14319 [ Note: read_initial_length() and read_offset() are based on the
14320 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14321 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14322 from:
14323
f7ef9339 14324 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14325
613e1657
KB
14326 This document is only a draft and is subject to change. (So beware.)
14327
f7ef9339 14328 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14329 determined empirically by examining 64-bit ELF files produced by
14330 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14331
14332 - Kevin, July 16, 2002
613e1657
KB
14333 ] */
14334
14335static LONGEST
c764a876 14336read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14337{
fe1b8b76 14338 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14339
dd373385 14340 if (length == 0xffffffff)
613e1657 14341 {
fe1b8b76 14342 length = bfd_get_64 (abfd, buf + 4);
613e1657 14343 *bytes_read = 12;
613e1657 14344 }
dd373385 14345 else if (length == 0)
f7ef9339 14346 {
dd373385 14347 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14348 length = bfd_get_64 (abfd, buf);
f7ef9339 14349 *bytes_read = 8;
f7ef9339 14350 }
613e1657
KB
14351 else
14352 {
14353 *bytes_read = 4;
613e1657
KB
14354 }
14355
c764a876
DE
14356 return length;
14357}
dd373385 14358
c764a876
DE
14359/* Cover function for read_initial_length.
14360 Returns the length of the object at BUF, and stores the size of the
14361 initial length in *BYTES_READ and stores the size that offsets will be in
14362 *OFFSET_SIZE.
14363 If the initial length size is not equivalent to that specified in
14364 CU_HEADER then issue a complaint.
14365 This is useful when reading non-comp-unit headers. */
dd373385 14366
c764a876
DE
14367static LONGEST
14368read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14369 const struct comp_unit_head *cu_header,
14370 unsigned int *bytes_read,
14371 unsigned int *offset_size)
14372{
14373 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14374
14375 gdb_assert (cu_header->initial_length_size == 4
14376 || cu_header->initial_length_size == 8
14377 || cu_header->initial_length_size == 12);
14378
14379 if (cu_header->initial_length_size != *bytes_read)
14380 complaint (&symfile_complaints,
14381 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14382
c764a876 14383 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14384 return length;
613e1657
KB
14385}
14386
14387/* Read an offset from the data stream. The size of the offset is
917c78fc 14388 given by cu_header->offset_size. */
613e1657
KB
14389
14390static LONGEST
fe1b8b76 14391read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14392 unsigned int *bytes_read)
c764a876
DE
14393{
14394 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14395
c764a876
DE
14396 *bytes_read = cu_header->offset_size;
14397 return offset;
14398}
14399
14400/* Read an offset from the data stream. */
14401
14402static LONGEST
14403read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14404{
14405 LONGEST retval = 0;
14406
c764a876 14407 switch (offset_size)
613e1657
KB
14408 {
14409 case 4:
fe1b8b76 14410 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14411 break;
14412 case 8:
fe1b8b76 14413 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14414 break;
14415 default:
8e65ff28 14416 internal_error (__FILE__, __LINE__,
c764a876 14417 _("read_offset_1: bad switch [in module %s]"),
659b0389 14418 bfd_get_filename (abfd));
613e1657
KB
14419 }
14420
917c78fc 14421 return retval;
613e1657
KB
14422}
14423
fe1b8b76
JB
14424static gdb_byte *
14425read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14426{
14427 /* If the size of a host char is 8 bits, we can return a pointer
14428 to the buffer, otherwise we have to copy the data to a buffer
14429 allocated on the temporary obstack. */
4bdf3d34 14430 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14431 return buf;
c906108c
SS
14432}
14433
14434static char *
9b1c24c8 14435read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14436{
14437 /* If the size of a host char is 8 bits, we can return a pointer
14438 to the string, otherwise we have to copy the string to a buffer
14439 allocated on the temporary obstack. */
4bdf3d34 14440 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14441 if (*buf == '\0')
14442 {
14443 *bytes_read_ptr = 1;
14444 return NULL;
14445 }
fe1b8b76
JB
14446 *bytes_read_ptr = strlen ((char *) buf) + 1;
14447 return (char *) buf;
4bdf3d34
JJ
14448}
14449
14450static char *
cf2c3c16 14451read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14452{
be391dca 14453 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14454 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14455 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14456 bfd_get_filename (abfd));
dce234bc 14457 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14458 error (_("DW_FORM_strp pointing outside of "
14459 ".debug_str section [in module %s]"),
14460 bfd_get_filename (abfd));
4bdf3d34 14461 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14462 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14463 return NULL;
dce234bc 14464 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14465}
14466
36586728
TT
14467/* Read a string at offset STR_OFFSET in the .debug_str section from
14468 the .dwz file DWZ. Throw an error if the offset is too large. If
14469 the string consists of a single NUL byte, return NULL; otherwise
14470 return a pointer to the string. */
14471
14472static char *
14473read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14474{
14475 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14476
14477 if (dwz->str.buffer == NULL)
14478 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14479 "section [in module %s]"),
14480 bfd_get_filename (dwz->dwz_bfd));
14481 if (str_offset >= dwz->str.size)
14482 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14483 ".debug_str section [in module %s]"),
14484 bfd_get_filename (dwz->dwz_bfd));
14485 gdb_assert (HOST_CHAR_BIT == 8);
14486 if (dwz->str.buffer[str_offset] == '\0')
14487 return NULL;
14488 return (char *) (dwz->str.buffer + str_offset);
14489}
14490
cf2c3c16
TT
14491static char *
14492read_indirect_string (bfd *abfd, gdb_byte *buf,
14493 const struct comp_unit_head *cu_header,
14494 unsigned int *bytes_read_ptr)
14495{
14496 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14497
14498 return read_indirect_string_at_offset (abfd, str_offset);
14499}
14500
12df843f 14501static ULONGEST
fe1b8b76 14502read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14503{
12df843f 14504 ULONGEST result;
ce5d95e1 14505 unsigned int num_read;
c906108c
SS
14506 int i, shift;
14507 unsigned char byte;
14508
14509 result = 0;
14510 shift = 0;
14511 num_read = 0;
14512 i = 0;
14513 while (1)
14514 {
fe1b8b76 14515 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14516 buf++;
14517 num_read++;
12df843f 14518 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14519 if ((byte & 128) == 0)
14520 {
14521 break;
14522 }
14523 shift += 7;
14524 }
14525 *bytes_read_ptr = num_read;
14526 return result;
14527}
14528
12df843f 14529static LONGEST
fe1b8b76 14530read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14531{
12df843f 14532 LONGEST result;
77e0b926 14533 int i, shift, num_read;
c906108c
SS
14534 unsigned char byte;
14535
14536 result = 0;
14537 shift = 0;
c906108c
SS
14538 num_read = 0;
14539 i = 0;
14540 while (1)
14541 {
fe1b8b76 14542 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14543 buf++;
14544 num_read++;
12df843f 14545 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14546 shift += 7;
14547 if ((byte & 128) == 0)
14548 {
14549 break;
14550 }
14551 }
77e0b926 14552 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14553 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14554 *bytes_read_ptr = num_read;
14555 return result;
14556}
14557
3019eac3
DE
14558/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14559 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14560 ADDR_SIZE is the size of addresses from the CU header. */
14561
14562static CORE_ADDR
14563read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14564{
14565 struct objfile *objfile = dwarf2_per_objfile->objfile;
14566 bfd *abfd = objfile->obfd;
14567 const gdb_byte *info_ptr;
14568
14569 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14570 if (dwarf2_per_objfile->addr.buffer == NULL)
14571 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14572 objfile->name);
14573 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14574 error (_("DW_FORM_addr_index pointing outside of "
14575 ".debug_addr section [in module %s]"),
14576 objfile->name);
14577 info_ptr = (dwarf2_per_objfile->addr.buffer
14578 + addr_base + addr_index * addr_size);
14579 if (addr_size == 4)
14580 return bfd_get_32 (abfd, info_ptr);
14581 else
14582 return bfd_get_64 (abfd, info_ptr);
14583}
14584
14585/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14586
14587static CORE_ADDR
14588read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14589{
14590 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14591}
14592
14593/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14594
14595static CORE_ADDR
14596read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14597 unsigned int *bytes_read)
14598{
14599 bfd *abfd = cu->objfile->obfd;
14600 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14601
14602 return read_addr_index (cu, addr_index);
14603}
14604
14605/* Data structure to pass results from dwarf2_read_addr_index_reader
14606 back to dwarf2_read_addr_index. */
14607
14608struct dwarf2_read_addr_index_data
14609{
14610 ULONGEST addr_base;
14611 int addr_size;
14612};
14613
14614/* die_reader_func for dwarf2_read_addr_index. */
14615
14616static void
14617dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14618 gdb_byte *info_ptr,
14619 struct die_info *comp_unit_die,
14620 int has_children,
14621 void *data)
14622{
14623 struct dwarf2_cu *cu = reader->cu;
14624 struct dwarf2_read_addr_index_data *aidata =
14625 (struct dwarf2_read_addr_index_data *) data;
14626
14627 aidata->addr_base = cu->addr_base;
14628 aidata->addr_size = cu->header.addr_size;
14629}
14630
14631/* Given an index in .debug_addr, fetch the value.
14632 NOTE: This can be called during dwarf expression evaluation,
14633 long after the debug information has been read, and thus per_cu->cu
14634 may no longer exist. */
14635
14636CORE_ADDR
14637dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14638 unsigned int addr_index)
14639{
14640 struct objfile *objfile = per_cu->objfile;
14641 struct dwarf2_cu *cu = per_cu->cu;
14642 ULONGEST addr_base;
14643 int addr_size;
14644
14645 /* This is intended to be called from outside this file. */
14646 dw2_setup (objfile);
14647
14648 /* We need addr_base and addr_size.
14649 If we don't have PER_CU->cu, we have to get it.
14650 Nasty, but the alternative is storing the needed info in PER_CU,
14651 which at this point doesn't seem justified: it's not clear how frequently
14652 it would get used and it would increase the size of every PER_CU.
14653 Entry points like dwarf2_per_cu_addr_size do a similar thing
14654 so we're not in uncharted territory here.
14655 Alas we need to be a bit more complicated as addr_base is contained
14656 in the DIE.
14657
14658 We don't need to read the entire CU(/TU).
14659 We just need the header and top level die.
a1b64ce1 14660
3019eac3 14661 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14662 For now we skip this optimization. */
3019eac3
DE
14663
14664 if (cu != NULL)
14665 {
14666 addr_base = cu->addr_base;
14667 addr_size = cu->header.addr_size;
14668 }
14669 else
14670 {
14671 struct dwarf2_read_addr_index_data aidata;
14672
a1b64ce1
DE
14673 /* Note: We can't use init_cutu_and_read_dies_simple here,
14674 we need addr_base. */
14675 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14676 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14677 addr_base = aidata.addr_base;
14678 addr_size = aidata.addr_size;
14679 }
14680
14681 return read_addr_index_1 (addr_index, addr_base, addr_size);
14682}
14683
14684/* Given a DW_AT_str_index, fetch the string. */
14685
14686static char *
14687read_str_index (const struct die_reader_specs *reader,
14688 struct dwarf2_cu *cu, ULONGEST str_index)
14689{
14690 struct objfile *objfile = dwarf2_per_objfile->objfile;
14691 const char *dwo_name = objfile->name;
14692 bfd *abfd = objfile->obfd;
14693 struct dwo_sections *sections = &reader->dwo_file->sections;
14694 gdb_byte *info_ptr;
14695 ULONGEST str_offset;
14696
14697 dwarf2_read_section (objfile, &sections->str);
14698 dwarf2_read_section (objfile, &sections->str_offsets);
14699 if (sections->str.buffer == NULL)
14700 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14701 " in CU at offset 0x%lx [in module %s]"),
14702 (long) cu->header.offset.sect_off, dwo_name);
14703 if (sections->str_offsets.buffer == NULL)
14704 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14705 " in CU at offset 0x%lx [in module %s]"),
14706 (long) cu->header.offset.sect_off, dwo_name);
14707 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14708 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14709 " section in CU at offset 0x%lx [in module %s]"),
14710 (long) cu->header.offset.sect_off, dwo_name);
14711 info_ptr = (sections->str_offsets.buffer
14712 + str_index * cu->header.offset_size);
14713 if (cu->header.offset_size == 4)
14714 str_offset = bfd_get_32 (abfd, info_ptr);
14715 else
14716 str_offset = bfd_get_64 (abfd, info_ptr);
14717 if (str_offset >= sections->str.size)
14718 error (_("Offset from DW_FORM_str_index pointing outside of"
14719 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14720 (long) cu->header.offset.sect_off, dwo_name);
14721 return (char *) (sections->str.buffer + str_offset);
14722}
14723
3019eac3
DE
14724/* Return the length of an LEB128 number in BUF. */
14725
14726static int
14727leb128_size (const gdb_byte *buf)
14728{
14729 const gdb_byte *begin = buf;
14730 gdb_byte byte;
14731
14732 while (1)
14733 {
14734 byte = *buf++;
14735 if ((byte & 128) == 0)
14736 return buf - begin;
14737 }
14738}
14739
c906108c 14740static void
e142c38c 14741set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14742{
14743 switch (lang)
14744 {
14745 case DW_LANG_C89:
76bee0cc 14746 case DW_LANG_C99:
c906108c 14747 case DW_LANG_C:
e142c38c 14748 cu->language = language_c;
c906108c
SS
14749 break;
14750 case DW_LANG_C_plus_plus:
e142c38c 14751 cu->language = language_cplus;
c906108c 14752 break;
6aecb9c2
JB
14753 case DW_LANG_D:
14754 cu->language = language_d;
14755 break;
c906108c
SS
14756 case DW_LANG_Fortran77:
14757 case DW_LANG_Fortran90:
b21b22e0 14758 case DW_LANG_Fortran95:
e142c38c 14759 cu->language = language_fortran;
c906108c 14760 break;
a766d390
DE
14761 case DW_LANG_Go:
14762 cu->language = language_go;
14763 break;
c906108c 14764 case DW_LANG_Mips_Assembler:
e142c38c 14765 cu->language = language_asm;
c906108c 14766 break;
bebd888e 14767 case DW_LANG_Java:
e142c38c 14768 cu->language = language_java;
bebd888e 14769 break;
c906108c 14770 case DW_LANG_Ada83:
8aaf0b47 14771 case DW_LANG_Ada95:
bc5f45f8
JB
14772 cu->language = language_ada;
14773 break;
72019c9c
GM
14774 case DW_LANG_Modula2:
14775 cu->language = language_m2;
14776 break;
fe8e67fd
PM
14777 case DW_LANG_Pascal83:
14778 cu->language = language_pascal;
14779 break;
22566fbd
DJ
14780 case DW_LANG_ObjC:
14781 cu->language = language_objc;
14782 break;
c906108c
SS
14783 case DW_LANG_Cobol74:
14784 case DW_LANG_Cobol85:
c906108c 14785 default:
e142c38c 14786 cu->language = language_minimal;
c906108c
SS
14787 break;
14788 }
e142c38c 14789 cu->language_defn = language_def (cu->language);
c906108c
SS
14790}
14791
14792/* Return the named attribute or NULL if not there. */
14793
14794static struct attribute *
e142c38c 14795dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14796{
a48e046c 14797 for (;;)
c906108c 14798 {
a48e046c
TT
14799 unsigned int i;
14800 struct attribute *spec = NULL;
14801
14802 for (i = 0; i < die->num_attrs; ++i)
14803 {
14804 if (die->attrs[i].name == name)
14805 return &die->attrs[i];
14806 if (die->attrs[i].name == DW_AT_specification
14807 || die->attrs[i].name == DW_AT_abstract_origin)
14808 spec = &die->attrs[i];
14809 }
14810
14811 if (!spec)
14812 break;
c906108c 14813
f2f0e013 14814 die = follow_die_ref (die, spec, &cu);
f2f0e013 14815 }
c5aa993b 14816
c906108c
SS
14817 return NULL;
14818}
14819
348e048f
DE
14820/* Return the named attribute or NULL if not there,
14821 but do not follow DW_AT_specification, etc.
14822 This is for use in contexts where we're reading .debug_types dies.
14823 Following DW_AT_specification, DW_AT_abstract_origin will take us
14824 back up the chain, and we want to go down. */
14825
14826static struct attribute *
45e58e77 14827dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14828{
14829 unsigned int i;
14830
14831 for (i = 0; i < die->num_attrs; ++i)
14832 if (die->attrs[i].name == name)
14833 return &die->attrs[i];
14834
14835 return NULL;
14836}
14837
05cf31d1
JB
14838/* Return non-zero iff the attribute NAME is defined for the given DIE,
14839 and holds a non-zero value. This function should only be used for
2dc7f7b3 14840 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14841
14842static int
14843dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14844{
14845 struct attribute *attr = dwarf2_attr (die, name, cu);
14846
14847 return (attr && DW_UNSND (attr));
14848}
14849
3ca72b44 14850static int
e142c38c 14851die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14852{
05cf31d1
JB
14853 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14854 which value is non-zero. However, we have to be careful with
14855 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14856 (via dwarf2_flag_true_p) follows this attribute. So we may
14857 end up accidently finding a declaration attribute that belongs
14858 to a different DIE referenced by the specification attribute,
14859 even though the given DIE does not have a declaration attribute. */
14860 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14861 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14862}
14863
63d06c5c 14864/* Return the die giving the specification for DIE, if there is
f2f0e013 14865 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14866 containing the return value on output. If there is no
14867 specification, but there is an abstract origin, that is
14868 returned. */
63d06c5c
DC
14869
14870static struct die_info *
f2f0e013 14871die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14872{
f2f0e013
DJ
14873 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14874 *spec_cu);
63d06c5c 14875
edb3359d
DJ
14876 if (spec_attr == NULL)
14877 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14878
63d06c5c
DC
14879 if (spec_attr == NULL)
14880 return NULL;
14881 else
f2f0e013 14882 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14883}
c906108c 14884
debd256d 14885/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14886 refers to.
14887 NOTE: This is also used as a "cleanup" function. */
14888
debd256d
JB
14889static void
14890free_line_header (struct line_header *lh)
14891{
14892 if (lh->standard_opcode_lengths)
a8bc7b56 14893 xfree (lh->standard_opcode_lengths);
debd256d
JB
14894
14895 /* Remember that all the lh->file_names[i].name pointers are
14896 pointers into debug_line_buffer, and don't need to be freed. */
14897 if (lh->file_names)
a8bc7b56 14898 xfree (lh->file_names);
debd256d
JB
14899
14900 /* Similarly for the include directory names. */
14901 if (lh->include_dirs)
a8bc7b56 14902 xfree (lh->include_dirs);
debd256d 14903
a8bc7b56 14904 xfree (lh);
debd256d
JB
14905}
14906
debd256d 14907/* Add an entry to LH's include directory table. */
ae2de4f8 14908
debd256d
JB
14909static void
14910add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14911{
debd256d
JB
14912 /* Grow the array if necessary. */
14913 if (lh->include_dirs_size == 0)
c5aa993b 14914 {
debd256d
JB
14915 lh->include_dirs_size = 1; /* for testing */
14916 lh->include_dirs = xmalloc (lh->include_dirs_size
14917 * sizeof (*lh->include_dirs));
14918 }
14919 else if (lh->num_include_dirs >= lh->include_dirs_size)
14920 {
14921 lh->include_dirs_size *= 2;
14922 lh->include_dirs = xrealloc (lh->include_dirs,
14923 (lh->include_dirs_size
14924 * sizeof (*lh->include_dirs)));
c5aa993b 14925 }
c906108c 14926
debd256d
JB
14927 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14928}
6e70227d 14929
debd256d 14930/* Add an entry to LH's file name table. */
ae2de4f8 14931
debd256d
JB
14932static void
14933add_file_name (struct line_header *lh,
14934 char *name,
14935 unsigned int dir_index,
14936 unsigned int mod_time,
14937 unsigned int length)
14938{
14939 struct file_entry *fe;
14940
14941 /* Grow the array if necessary. */
14942 if (lh->file_names_size == 0)
14943 {
14944 lh->file_names_size = 1; /* for testing */
14945 lh->file_names = xmalloc (lh->file_names_size
14946 * sizeof (*lh->file_names));
14947 }
14948 else if (lh->num_file_names >= lh->file_names_size)
14949 {
14950 lh->file_names_size *= 2;
14951 lh->file_names = xrealloc (lh->file_names,
14952 (lh->file_names_size
14953 * sizeof (*lh->file_names)));
14954 }
14955
14956 fe = &lh->file_names[lh->num_file_names++];
14957 fe->name = name;
14958 fe->dir_index = dir_index;
14959 fe->mod_time = mod_time;
14960 fe->length = length;
aaa75496 14961 fe->included_p = 0;
cb1df416 14962 fe->symtab = NULL;
debd256d 14963}
6e70227d 14964
36586728
TT
14965/* A convenience function to find the proper .debug_line section for a
14966 CU. */
14967
14968static struct dwarf2_section_info *
14969get_debug_line_section (struct dwarf2_cu *cu)
14970{
14971 struct dwarf2_section_info *section;
14972
14973 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14974 DWO file. */
14975 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14976 section = &cu->dwo_unit->dwo_file->sections.line;
14977 else if (cu->per_cu->is_dwz)
14978 {
14979 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14980
14981 section = &dwz->line;
14982 }
14983 else
14984 section = &dwarf2_per_objfile->line;
14985
14986 return section;
14987}
14988
debd256d 14989/* Read the statement program header starting at OFFSET in
3019eac3 14990 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14991 to a struct line_header, allocated using xmalloc.
debd256d
JB
14992
14993 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14994 the returned object point into the dwarf line section buffer,
14995 and must not be freed. */
ae2de4f8 14996
debd256d 14997static struct line_header *
3019eac3 14998dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14999{
15000 struct cleanup *back_to;
15001 struct line_header *lh;
fe1b8b76 15002 gdb_byte *line_ptr;
c764a876 15003 unsigned int bytes_read, offset_size;
debd256d
JB
15004 int i;
15005 char *cur_dir, *cur_file;
3019eac3
DE
15006 struct dwarf2_section_info *section;
15007 bfd *abfd;
15008
36586728 15009 section = get_debug_line_section (cu);
3019eac3
DE
15010 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15011 if (section->buffer == NULL)
debd256d 15012 {
3019eac3
DE
15013 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15014 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15015 else
15016 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15017 return 0;
15018 }
15019
fceca515
DE
15020 /* We can't do this until we know the section is non-empty.
15021 Only then do we know we have such a section. */
15022 abfd = section->asection->owner;
15023
a738430d
MK
15024 /* Make sure that at least there's room for the total_length field.
15025 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15026 if (offset + 4 >= section->size)
debd256d 15027 {
4d3c2250 15028 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15029 return 0;
15030 }
15031
15032 lh = xmalloc (sizeof (*lh));
15033 memset (lh, 0, sizeof (*lh));
15034 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15035 (void *) lh);
15036
3019eac3 15037 line_ptr = section->buffer + offset;
debd256d 15038
a738430d 15039 /* Read in the header. */
6e70227d 15040 lh->total_length =
c764a876
DE
15041 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15042 &bytes_read, &offset_size);
debd256d 15043 line_ptr += bytes_read;
3019eac3 15044 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15045 {
4d3c2250 15046 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15047 return 0;
15048 }
15049 lh->statement_program_end = line_ptr + lh->total_length;
15050 lh->version = read_2_bytes (abfd, line_ptr);
15051 line_ptr += 2;
c764a876
DE
15052 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15053 line_ptr += offset_size;
debd256d
JB
15054 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15055 line_ptr += 1;
2dc7f7b3
TT
15056 if (lh->version >= 4)
15057 {
15058 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15059 line_ptr += 1;
15060 }
15061 else
15062 lh->maximum_ops_per_instruction = 1;
15063
15064 if (lh->maximum_ops_per_instruction == 0)
15065 {
15066 lh->maximum_ops_per_instruction = 1;
15067 complaint (&symfile_complaints,
3e43a32a
MS
15068 _("invalid maximum_ops_per_instruction "
15069 "in `.debug_line' section"));
2dc7f7b3
TT
15070 }
15071
debd256d
JB
15072 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15073 line_ptr += 1;
15074 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15075 line_ptr += 1;
15076 lh->line_range = read_1_byte (abfd, line_ptr);
15077 line_ptr += 1;
15078 lh->opcode_base = read_1_byte (abfd, line_ptr);
15079 line_ptr += 1;
15080 lh->standard_opcode_lengths
fe1b8b76 15081 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15082
15083 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15084 for (i = 1; i < lh->opcode_base; ++i)
15085 {
15086 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15087 line_ptr += 1;
15088 }
15089
a738430d 15090 /* Read directory table. */
9b1c24c8 15091 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15092 {
15093 line_ptr += bytes_read;
15094 add_include_dir (lh, cur_dir);
15095 }
15096 line_ptr += bytes_read;
15097
a738430d 15098 /* Read file name table. */
9b1c24c8 15099 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15100 {
15101 unsigned int dir_index, mod_time, length;
15102
15103 line_ptr += bytes_read;
15104 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15105 line_ptr += bytes_read;
15106 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15107 line_ptr += bytes_read;
15108 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15109 line_ptr += bytes_read;
15110
15111 add_file_name (lh, cur_file, dir_index, mod_time, length);
15112 }
15113 line_ptr += bytes_read;
6e70227d 15114 lh->statement_program_start = line_ptr;
debd256d 15115
3019eac3 15116 if (line_ptr > (section->buffer + section->size))
4d3c2250 15117 complaint (&symfile_complaints,
3e43a32a
MS
15118 _("line number info header doesn't "
15119 "fit in `.debug_line' section"));
debd256d
JB
15120
15121 discard_cleanups (back_to);
15122 return lh;
15123}
c906108c 15124
c6da4cef
DE
15125/* Subroutine of dwarf_decode_lines to simplify it.
15126 Return the file name of the psymtab for included file FILE_INDEX
15127 in line header LH of PST.
15128 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15129 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15130 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15131
15132 The function creates dangling cleanup registration. */
c6da4cef
DE
15133
15134static char *
15135psymtab_include_file_name (const struct line_header *lh, int file_index,
15136 const struct partial_symtab *pst,
15137 const char *comp_dir)
15138{
15139 const struct file_entry fe = lh->file_names [file_index];
15140 char *include_name = fe.name;
15141 char *include_name_to_compare = include_name;
15142 char *dir_name = NULL;
72b9f47f
TT
15143 const char *pst_filename;
15144 char *copied_name = NULL;
c6da4cef
DE
15145 int file_is_pst;
15146
15147 if (fe.dir_index)
15148 dir_name = lh->include_dirs[fe.dir_index - 1];
15149
15150 if (!IS_ABSOLUTE_PATH (include_name)
15151 && (dir_name != NULL || comp_dir != NULL))
15152 {
15153 /* Avoid creating a duplicate psymtab for PST.
15154 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15155 Before we do the comparison, however, we need to account
15156 for DIR_NAME and COMP_DIR.
15157 First prepend dir_name (if non-NULL). If we still don't
15158 have an absolute path prepend comp_dir (if non-NULL).
15159 However, the directory we record in the include-file's
15160 psymtab does not contain COMP_DIR (to match the
15161 corresponding symtab(s)).
15162
15163 Example:
15164
15165 bash$ cd /tmp
15166 bash$ gcc -g ./hello.c
15167 include_name = "hello.c"
15168 dir_name = "."
15169 DW_AT_comp_dir = comp_dir = "/tmp"
15170 DW_AT_name = "./hello.c" */
15171
15172 if (dir_name != NULL)
15173 {
15174 include_name = concat (dir_name, SLASH_STRING,
15175 include_name, (char *)NULL);
15176 include_name_to_compare = include_name;
15177 make_cleanup (xfree, include_name);
15178 }
15179 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15180 {
15181 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15182 include_name, (char *)NULL);
15183 }
15184 }
15185
15186 pst_filename = pst->filename;
15187 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15188 {
72b9f47f
TT
15189 copied_name = concat (pst->dirname, SLASH_STRING,
15190 pst_filename, (char *)NULL);
15191 pst_filename = copied_name;
c6da4cef
DE
15192 }
15193
1e3fad37 15194 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15195
15196 if (include_name_to_compare != include_name)
15197 xfree (include_name_to_compare);
72b9f47f
TT
15198 if (copied_name != NULL)
15199 xfree (copied_name);
c6da4cef
DE
15200
15201 if (file_is_pst)
15202 return NULL;
15203 return include_name;
15204}
15205
c91513d8
PP
15206/* Ignore this record_line request. */
15207
15208static void
15209noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15210{
15211 return;
15212}
15213
f3f5162e
DE
15214/* Subroutine of dwarf_decode_lines to simplify it.
15215 Process the line number information in LH. */
debd256d 15216
c906108c 15217static void
f3f5162e
DE
15218dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15219 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15220{
a8c50c1f 15221 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15222 gdb_byte *line_end;
a8c50c1f 15223 unsigned int bytes_read, extended_len;
c906108c 15224 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15225 CORE_ADDR baseaddr;
15226 struct objfile *objfile = cu->objfile;
f3f5162e 15227 bfd *abfd = objfile->obfd;
fbf65064 15228 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15229 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15230 struct subfile *last_subfile = NULL;
c91513d8
PP
15231 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15232 = record_line;
e142c38c
DJ
15233
15234 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15235
debd256d
JB
15236 line_ptr = lh->statement_program_start;
15237 line_end = lh->statement_program_end;
c906108c
SS
15238
15239 /* Read the statement sequences until there's nothing left. */
15240 while (line_ptr < line_end)
15241 {
15242 /* state machine registers */
15243 CORE_ADDR address = 0;
15244 unsigned int file = 1;
15245 unsigned int line = 1;
15246 unsigned int column = 0;
debd256d 15247 int is_stmt = lh->default_is_stmt;
c906108c
SS
15248 int basic_block = 0;
15249 int end_sequence = 0;
fbf65064 15250 CORE_ADDR addr;
2dc7f7b3 15251 unsigned char op_index = 0;
c906108c 15252
aaa75496 15253 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15254 {
aaa75496 15255 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15256 /* lh->include_dirs and lh->file_names are 0-based, but the
15257 directory and file name numbers in the statement program
15258 are 1-based. */
15259 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15260 char *dir = NULL;
a738430d 15261
debd256d
JB
15262 if (fe->dir_index)
15263 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15264
15265 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15266 }
15267
a738430d 15268 /* Decode the table. */
c5aa993b 15269 while (!end_sequence)
c906108c
SS
15270 {
15271 op_code = read_1_byte (abfd, line_ptr);
15272 line_ptr += 1;
59205f5a
JB
15273 if (line_ptr > line_end)
15274 {
15275 dwarf2_debug_line_missing_end_sequence_complaint ();
15276 break;
15277 }
9aa1fe7e 15278
debd256d 15279 if (op_code >= lh->opcode_base)
6e70227d 15280 {
a738430d 15281 /* Special operand. */
debd256d 15282 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15283 address += (((op_index + (adj_opcode / lh->line_range))
15284 / lh->maximum_ops_per_instruction)
15285 * lh->minimum_instruction_length);
15286 op_index = ((op_index + (adj_opcode / lh->line_range))
15287 % lh->maximum_ops_per_instruction);
debd256d 15288 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15289 if (lh->num_file_names < file || file == 0)
25e43795 15290 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15291 /* For now we ignore lines not starting on an
15292 instruction boundary. */
15293 else if (op_index == 0)
25e43795
DJ
15294 {
15295 lh->file_names[file - 1].included_p = 1;
ca5f395d 15296 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15297 {
15298 if (last_subfile != current_subfile)
15299 {
15300 addr = gdbarch_addr_bits_remove (gdbarch, address);
15301 if (last_subfile)
c91513d8 15302 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15303 last_subfile = current_subfile;
15304 }
25e43795 15305 /* Append row to matrix using current values. */
7019d805 15306 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15307 (*p_record_line) (current_subfile, line, addr);
366da635 15308 }
25e43795 15309 }
ca5f395d 15310 basic_block = 0;
9aa1fe7e
GK
15311 }
15312 else switch (op_code)
c906108c
SS
15313 {
15314 case DW_LNS_extended_op:
3e43a32a
MS
15315 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15316 &bytes_read);
473b7be6 15317 line_ptr += bytes_read;
a8c50c1f 15318 extended_end = line_ptr + extended_len;
c906108c
SS
15319 extended_op = read_1_byte (abfd, line_ptr);
15320 line_ptr += 1;
15321 switch (extended_op)
15322 {
15323 case DW_LNE_end_sequence:
c91513d8 15324 p_record_line = record_line;
c906108c 15325 end_sequence = 1;
c906108c
SS
15326 break;
15327 case DW_LNE_set_address:
e7c27a73 15328 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15329
15330 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15331 {
15332 /* This line table is for a function which has been
15333 GCd by the linker. Ignore it. PR gdb/12528 */
15334
15335 long line_offset
36586728 15336 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15337
15338 complaint (&symfile_complaints,
15339 _(".debug_line address at offset 0x%lx is 0 "
15340 "[in module %s]"),
bb5ed363 15341 line_offset, objfile->name);
c91513d8
PP
15342 p_record_line = noop_record_line;
15343 }
15344
2dc7f7b3 15345 op_index = 0;
107d2387
AC
15346 line_ptr += bytes_read;
15347 address += baseaddr;
c906108c
SS
15348 break;
15349 case DW_LNE_define_file:
debd256d
JB
15350 {
15351 char *cur_file;
15352 unsigned int dir_index, mod_time, length;
6e70227d 15353
3e43a32a
MS
15354 cur_file = read_direct_string (abfd, line_ptr,
15355 &bytes_read);
debd256d
JB
15356 line_ptr += bytes_read;
15357 dir_index =
15358 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15359 line_ptr += bytes_read;
15360 mod_time =
15361 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15362 line_ptr += bytes_read;
15363 length =
15364 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15365 line_ptr += bytes_read;
15366 add_file_name (lh, cur_file, dir_index, mod_time, length);
15367 }
c906108c 15368 break;
d0c6ba3d
CC
15369 case DW_LNE_set_discriminator:
15370 /* The discriminator is not interesting to the debugger;
15371 just ignore it. */
15372 line_ptr = extended_end;
15373 break;
c906108c 15374 default:
4d3c2250 15375 complaint (&symfile_complaints,
e2e0b3e5 15376 _("mangled .debug_line section"));
debd256d 15377 return;
c906108c 15378 }
a8c50c1f
DJ
15379 /* Make sure that we parsed the extended op correctly. If e.g.
15380 we expected a different address size than the producer used,
15381 we may have read the wrong number of bytes. */
15382 if (line_ptr != extended_end)
15383 {
15384 complaint (&symfile_complaints,
15385 _("mangled .debug_line section"));
15386 return;
15387 }
c906108c
SS
15388 break;
15389 case DW_LNS_copy:
59205f5a 15390 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15391 dwarf2_debug_line_missing_file_complaint ();
15392 else
366da635 15393 {
25e43795 15394 lh->file_names[file - 1].included_p = 1;
ca5f395d 15395 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15396 {
15397 if (last_subfile != current_subfile)
15398 {
15399 addr = gdbarch_addr_bits_remove (gdbarch, address);
15400 if (last_subfile)
c91513d8 15401 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15402 last_subfile = current_subfile;
15403 }
7019d805 15404 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15405 (*p_record_line) (current_subfile, line, addr);
fbf65064 15406 }
366da635 15407 }
c906108c
SS
15408 basic_block = 0;
15409 break;
15410 case DW_LNS_advance_pc:
2dc7f7b3
TT
15411 {
15412 CORE_ADDR adjust
15413 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15414
15415 address += (((op_index + adjust)
15416 / lh->maximum_ops_per_instruction)
15417 * lh->minimum_instruction_length);
15418 op_index = ((op_index + adjust)
15419 % lh->maximum_ops_per_instruction);
15420 line_ptr += bytes_read;
15421 }
c906108c
SS
15422 break;
15423 case DW_LNS_advance_line:
15424 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15425 line_ptr += bytes_read;
15426 break;
15427 case DW_LNS_set_file:
debd256d 15428 {
a738430d
MK
15429 /* The arrays lh->include_dirs and lh->file_names are
15430 0-based, but the directory and file name numbers in
15431 the statement program are 1-based. */
debd256d 15432 struct file_entry *fe;
4f1520fb 15433 char *dir = NULL;
a738430d 15434
debd256d
JB
15435 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15436 line_ptr += bytes_read;
59205f5a 15437 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15438 dwarf2_debug_line_missing_file_complaint ();
15439 else
15440 {
15441 fe = &lh->file_names[file - 1];
15442 if (fe->dir_index)
15443 dir = lh->include_dirs[fe->dir_index - 1];
15444 if (!decode_for_pst_p)
15445 {
15446 last_subfile = current_subfile;
15447 dwarf2_start_subfile (fe->name, dir, comp_dir);
15448 }
15449 }
debd256d 15450 }
c906108c
SS
15451 break;
15452 case DW_LNS_set_column:
15453 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15454 line_ptr += bytes_read;
15455 break;
15456 case DW_LNS_negate_stmt:
15457 is_stmt = (!is_stmt);
15458 break;
15459 case DW_LNS_set_basic_block:
15460 basic_block = 1;
15461 break;
c2c6d25f
JM
15462 /* Add to the address register of the state machine the
15463 address increment value corresponding to special opcode
a738430d
MK
15464 255. I.e., this value is scaled by the minimum
15465 instruction length since special opcode 255 would have
b021a221 15466 scaled the increment. */
c906108c 15467 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15468 {
15469 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15470
15471 address += (((op_index + adjust)
15472 / lh->maximum_ops_per_instruction)
15473 * lh->minimum_instruction_length);
15474 op_index = ((op_index + adjust)
15475 % lh->maximum_ops_per_instruction);
15476 }
c906108c
SS
15477 break;
15478 case DW_LNS_fixed_advance_pc:
15479 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15480 op_index = 0;
c906108c
SS
15481 line_ptr += 2;
15482 break;
9aa1fe7e 15483 default:
a738430d
MK
15484 {
15485 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15486 int i;
a738430d 15487
debd256d 15488 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15489 {
15490 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15491 line_ptr += bytes_read;
15492 }
15493 }
c906108c
SS
15494 }
15495 }
59205f5a
JB
15496 if (lh->num_file_names < file || file == 0)
15497 dwarf2_debug_line_missing_file_complaint ();
15498 else
15499 {
15500 lh->file_names[file - 1].included_p = 1;
15501 if (!decode_for_pst_p)
fbf65064
UW
15502 {
15503 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15504 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15505 }
59205f5a 15506 }
c906108c 15507 }
f3f5162e
DE
15508}
15509
15510/* Decode the Line Number Program (LNP) for the given line_header
15511 structure and CU. The actual information extracted and the type
15512 of structures created from the LNP depends on the value of PST.
15513
15514 1. If PST is NULL, then this procedure uses the data from the program
15515 to create all necessary symbol tables, and their linetables.
15516
15517 2. If PST is not NULL, this procedure reads the program to determine
15518 the list of files included by the unit represented by PST, and
15519 builds all the associated partial symbol tables.
15520
15521 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15522 It is used for relative paths in the line table.
15523 NOTE: When processing partial symtabs (pst != NULL),
15524 comp_dir == pst->dirname.
15525
15526 NOTE: It is important that psymtabs have the same file name (via strcmp)
15527 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15528 symtab we don't use it in the name of the psymtabs we create.
15529 E.g. expand_line_sal requires this when finding psymtabs to expand.
15530 A good testcase for this is mb-inline.exp. */
15531
15532static void
15533dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15534 struct dwarf2_cu *cu, struct partial_symtab *pst,
15535 int want_line_info)
15536{
15537 struct objfile *objfile = cu->objfile;
15538 const int decode_for_pst_p = (pst != NULL);
15539 struct subfile *first_subfile = current_subfile;
15540
15541 if (want_line_info)
15542 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15543
15544 if (decode_for_pst_p)
15545 {
15546 int file_index;
15547
15548 /* Now that we're done scanning the Line Header Program, we can
15549 create the psymtab of each included file. */
15550 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15551 if (lh->file_names[file_index].included_p == 1)
15552 {
c6da4cef
DE
15553 char *include_name =
15554 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15555 if (include_name != NULL)
aaa75496
JB
15556 dwarf2_create_include_psymtab (include_name, pst, objfile);
15557 }
15558 }
cb1df416
DJ
15559 else
15560 {
15561 /* Make sure a symtab is created for every file, even files
15562 which contain only variables (i.e. no code with associated
15563 line numbers). */
cb1df416 15564 int i;
cb1df416
DJ
15565
15566 for (i = 0; i < lh->num_file_names; i++)
15567 {
15568 char *dir = NULL;
f3f5162e 15569 struct file_entry *fe;
9a619af0 15570
cb1df416
DJ
15571 fe = &lh->file_names[i];
15572 if (fe->dir_index)
15573 dir = lh->include_dirs[fe->dir_index - 1];
15574 dwarf2_start_subfile (fe->name, dir, comp_dir);
15575
15576 /* Skip the main file; we don't need it, and it must be
15577 allocated last, so that it will show up before the
15578 non-primary symtabs in the objfile's symtab list. */
15579 if (current_subfile == first_subfile)
15580 continue;
15581
15582 if (current_subfile->symtab == NULL)
15583 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15584 objfile);
cb1df416
DJ
15585 fe->symtab = current_subfile->symtab;
15586 }
15587 }
c906108c
SS
15588}
15589
15590/* Start a subfile for DWARF. FILENAME is the name of the file and
15591 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15592 or NULL if not known. COMP_DIR is the compilation directory for the
15593 linetable's compilation unit or NULL if not known.
c906108c
SS
15594 This routine tries to keep line numbers from identical absolute and
15595 relative file names in a common subfile.
15596
15597 Using the `list' example from the GDB testsuite, which resides in
15598 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15599 of /srcdir/list0.c yields the following debugging information for list0.c:
15600
c5aa993b
JM
15601 DW_AT_name: /srcdir/list0.c
15602 DW_AT_comp_dir: /compdir
357e46e7 15603 files.files[0].name: list0.h
c5aa993b 15604 files.files[0].dir: /srcdir
357e46e7 15605 files.files[1].name: list0.c
c5aa993b 15606 files.files[1].dir: /srcdir
c906108c
SS
15607
15608 The line number information for list0.c has to end up in a single
4f1520fb
FR
15609 subfile, so that `break /srcdir/list0.c:1' works as expected.
15610 start_subfile will ensure that this happens provided that we pass the
15611 concatenation of files.files[1].dir and files.files[1].name as the
15612 subfile's name. */
c906108c
SS
15613
15614static void
3e43a32a
MS
15615dwarf2_start_subfile (char *filename, const char *dirname,
15616 const char *comp_dir)
c906108c 15617{
4f1520fb
FR
15618 char *fullname;
15619
15620 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15621 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15622 second argument to start_subfile. To be consistent, we do the
15623 same here. In order not to lose the line information directory,
15624 we concatenate it to the filename when it makes sense.
15625 Note that the Dwarf3 standard says (speaking of filenames in line
15626 information): ``The directory index is ignored for file names
15627 that represent full path names''. Thus ignoring dirname in the
15628 `else' branch below isn't an issue. */
c906108c 15629
d5166ae1 15630 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15631 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15632 else
15633 fullname = filename;
c906108c 15634
4f1520fb
FR
15635 start_subfile (fullname, comp_dir);
15636
15637 if (fullname != filename)
15638 xfree (fullname);
c906108c
SS
15639}
15640
f4dc4d17
DE
15641/* Start a symtab for DWARF.
15642 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15643
15644static void
15645dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15646 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15647{
15648 start_symtab (name, comp_dir, low_pc);
15649 record_debugformat ("DWARF 2");
15650 record_producer (cu->producer);
15651
15652 /* We assume that we're processing GCC output. */
15653 processing_gcc_compilation = 2;
15654
15655 processing_has_namespace_info = 0;
15656}
15657
4c2df51b
DJ
15658static void
15659var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15660 struct dwarf2_cu *cu)
4c2df51b 15661{
e7c27a73
DJ
15662 struct objfile *objfile = cu->objfile;
15663 struct comp_unit_head *cu_header = &cu->header;
15664
4c2df51b
DJ
15665 /* NOTE drow/2003-01-30: There used to be a comment and some special
15666 code here to turn a symbol with DW_AT_external and a
15667 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15668 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15669 with some versions of binutils) where shared libraries could have
15670 relocations against symbols in their debug information - the
15671 minimal symbol would have the right address, but the debug info
15672 would not. It's no longer necessary, because we will explicitly
15673 apply relocations when we read in the debug information now. */
15674
15675 /* A DW_AT_location attribute with no contents indicates that a
15676 variable has been optimized away. */
15677 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15678 {
15679 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15680 return;
15681 }
15682
15683 /* Handle one degenerate form of location expression specially, to
15684 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15685 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15686 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15687
15688 if (attr_form_is_block (attr)
3019eac3
DE
15689 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15690 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15691 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15692 && (DW_BLOCK (attr)->size
15693 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15694 {
891d2f0b 15695 unsigned int dummy;
4c2df51b 15696
3019eac3
DE
15697 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15698 SYMBOL_VALUE_ADDRESS (sym) =
15699 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15700 else
15701 SYMBOL_VALUE_ADDRESS (sym) =
15702 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15703 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15704 fixup_symbol_section (sym, objfile);
15705 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15706 SYMBOL_SECTION (sym));
4c2df51b
DJ
15707 return;
15708 }
15709
15710 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15711 expression evaluator, and use LOC_COMPUTED only when necessary
15712 (i.e. when the value of a register or memory location is
15713 referenced, or a thread-local block, etc.). Then again, it might
15714 not be worthwhile. I'm assuming that it isn't unless performance
15715 or memory numbers show me otherwise. */
15716
e7c27a73 15717 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15718 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15719
15720 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15721 cu->has_loclist = 1;
4c2df51b
DJ
15722}
15723
c906108c
SS
15724/* Given a pointer to a DWARF information entry, figure out if we need
15725 to make a symbol table entry for it, and if so, create a new entry
15726 and return a pointer to it.
15727 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15728 used the passed type.
15729 If SPACE is not NULL, use it to hold the new symbol. If it is
15730 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15731
15732static struct symbol *
34eaf542
TT
15733new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15734 struct symbol *space)
c906108c 15735{
e7c27a73 15736 struct objfile *objfile = cu->objfile;
c906108c 15737 struct symbol *sym = NULL;
15d034d0 15738 const char *name;
c906108c
SS
15739 struct attribute *attr = NULL;
15740 struct attribute *attr2 = NULL;
e142c38c 15741 CORE_ADDR baseaddr;
e37fd15a
SW
15742 struct pending **list_to_add = NULL;
15743
edb3359d 15744 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15745
15746 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15747
94af9270 15748 name = dwarf2_name (die, cu);
c906108c
SS
15749 if (name)
15750 {
94af9270 15751 const char *linkagename;
34eaf542 15752 int suppress_add = 0;
94af9270 15753
34eaf542
TT
15754 if (space)
15755 sym = space;
15756 else
15757 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15758 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15759
15760 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15761 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15762 linkagename = dwarf2_physname (name, die, cu);
15763 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15764
f55ee35c
JK
15765 /* Fortran does not have mangling standard and the mangling does differ
15766 between gfortran, iFort etc. */
15767 if (cu->language == language_fortran
b250c185 15768 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15769 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15770 dwarf2_full_name (name, die, cu),
29df156d 15771 NULL);
f55ee35c 15772
c906108c 15773 /* Default assumptions.
c5aa993b 15774 Use the passed type or decode it from the die. */
176620f1 15775 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15776 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15777 if (type != NULL)
15778 SYMBOL_TYPE (sym) = type;
15779 else
e7c27a73 15780 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15781 attr = dwarf2_attr (die,
15782 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15783 cu);
c906108c
SS
15784 if (attr)
15785 {
15786 SYMBOL_LINE (sym) = DW_UNSND (attr);
15787 }
cb1df416 15788
edb3359d
DJ
15789 attr = dwarf2_attr (die,
15790 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15791 cu);
cb1df416
DJ
15792 if (attr)
15793 {
15794 int file_index = DW_UNSND (attr);
9a619af0 15795
cb1df416
DJ
15796 if (cu->line_header == NULL
15797 || file_index > cu->line_header->num_file_names)
15798 complaint (&symfile_complaints,
15799 _("file index out of range"));
1c3d648d 15800 else if (file_index > 0)
cb1df416
DJ
15801 {
15802 struct file_entry *fe;
9a619af0 15803
cb1df416
DJ
15804 fe = &cu->line_header->file_names[file_index - 1];
15805 SYMBOL_SYMTAB (sym) = fe->symtab;
15806 }
15807 }
15808
c906108c
SS
15809 switch (die->tag)
15810 {
15811 case DW_TAG_label:
e142c38c 15812 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15813 if (attr)
15814 {
15815 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15816 }
0f5238ed
TT
15817 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15818 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15819 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15820 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15821 break;
15822 case DW_TAG_subprogram:
15823 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15824 finish_block. */
15825 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15826 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15827 if ((attr2 && (DW_UNSND (attr2) != 0))
15828 || cu->language == language_ada)
c906108c 15829 {
2cfa0c8d
JB
15830 /* Subprograms marked external are stored as a global symbol.
15831 Ada subprograms, whether marked external or not, are always
15832 stored as a global symbol, because we want to be able to
15833 access them globally. For instance, we want to be able
15834 to break on a nested subprogram without having to
15835 specify the context. */
e37fd15a 15836 list_to_add = &global_symbols;
c906108c
SS
15837 }
15838 else
15839 {
e37fd15a 15840 list_to_add = cu->list_in_scope;
c906108c
SS
15841 }
15842 break;
edb3359d
DJ
15843 case DW_TAG_inlined_subroutine:
15844 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15845 finish_block. */
15846 SYMBOL_CLASS (sym) = LOC_BLOCK;
15847 SYMBOL_INLINED (sym) = 1;
481860b3 15848 list_to_add = cu->list_in_scope;
edb3359d 15849 break;
34eaf542
TT
15850 case DW_TAG_template_value_param:
15851 suppress_add = 1;
15852 /* Fall through. */
72929c62 15853 case DW_TAG_constant:
c906108c 15854 case DW_TAG_variable:
254e6b9e 15855 case DW_TAG_member:
0963b4bd
MS
15856 /* Compilation with minimal debug info may result in
15857 variables with missing type entries. Change the
15858 misleading `void' type to something sensible. */
c906108c 15859 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15860 SYMBOL_TYPE (sym)
46bf5051 15861 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15862
e142c38c 15863 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15864 /* In the case of DW_TAG_member, we should only be called for
15865 static const members. */
15866 if (die->tag == DW_TAG_member)
15867 {
3863f96c
DE
15868 /* dwarf2_add_field uses die_is_declaration,
15869 so we do the same. */
254e6b9e
DE
15870 gdb_assert (die_is_declaration (die, cu));
15871 gdb_assert (attr);
15872 }
c906108c
SS
15873 if (attr)
15874 {
e7c27a73 15875 dwarf2_const_value (attr, sym, cu);
e142c38c 15876 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15877 if (!suppress_add)
34eaf542
TT
15878 {
15879 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15880 list_to_add = &global_symbols;
34eaf542 15881 else
e37fd15a 15882 list_to_add = cu->list_in_scope;
34eaf542 15883 }
c906108c
SS
15884 break;
15885 }
e142c38c 15886 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15887 if (attr)
15888 {
e7c27a73 15889 var_decode_location (attr, sym, cu);
e142c38c 15890 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15891
15892 /* Fortran explicitly imports any global symbols to the local
15893 scope by DW_TAG_common_block. */
15894 if (cu->language == language_fortran && die->parent
15895 && die->parent->tag == DW_TAG_common_block)
15896 attr2 = NULL;
15897
caac4577
JG
15898 if (SYMBOL_CLASS (sym) == LOC_STATIC
15899 && SYMBOL_VALUE_ADDRESS (sym) == 0
15900 && !dwarf2_per_objfile->has_section_at_zero)
15901 {
15902 /* When a static variable is eliminated by the linker,
15903 the corresponding debug information is not stripped
15904 out, but the variable address is set to null;
15905 do not add such variables into symbol table. */
15906 }
15907 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15908 {
f55ee35c
JK
15909 /* Workaround gfortran PR debug/40040 - it uses
15910 DW_AT_location for variables in -fPIC libraries which may
15911 get overriden by other libraries/executable and get
15912 a different address. Resolve it by the minimal symbol
15913 which may come from inferior's executable using copy
15914 relocation. Make this workaround only for gfortran as for
15915 other compilers GDB cannot guess the minimal symbol
15916 Fortran mangling kind. */
15917 if (cu->language == language_fortran && die->parent
15918 && die->parent->tag == DW_TAG_module
15919 && cu->producer
15920 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15921 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15922
1c809c68
TT
15923 /* A variable with DW_AT_external is never static,
15924 but it may be block-scoped. */
15925 list_to_add = (cu->list_in_scope == &file_symbols
15926 ? &global_symbols : cu->list_in_scope);
1c809c68 15927 }
c906108c 15928 else
e37fd15a 15929 list_to_add = cu->list_in_scope;
c906108c
SS
15930 }
15931 else
15932 {
15933 /* We do not know the address of this symbol.
c5aa993b
JM
15934 If it is an external symbol and we have type information
15935 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15936 The address of the variable will then be determined from
15937 the minimal symbol table whenever the variable is
15938 referenced. */
e142c38c 15939 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15940
15941 /* Fortran explicitly imports any global symbols to the local
15942 scope by DW_TAG_common_block. */
15943 if (cu->language == language_fortran && die->parent
15944 && die->parent->tag == DW_TAG_common_block)
15945 {
15946 /* SYMBOL_CLASS doesn't matter here because
15947 read_common_block is going to reset it. */
15948 if (!suppress_add)
15949 list_to_add = cu->list_in_scope;
15950 }
15951 else if (attr2 && (DW_UNSND (attr2) != 0)
15952 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15953 {
0fe7935b
DJ
15954 /* A variable with DW_AT_external is never static, but it
15955 may be block-scoped. */
15956 list_to_add = (cu->list_in_scope == &file_symbols
15957 ? &global_symbols : cu->list_in_scope);
15958
c906108c 15959 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15960 }
442ddf59
JK
15961 else if (!die_is_declaration (die, cu))
15962 {
15963 /* Use the default LOC_OPTIMIZED_OUT class. */
15964 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15965 if (!suppress_add)
15966 list_to_add = cu->list_in_scope;
442ddf59 15967 }
c906108c
SS
15968 }
15969 break;
15970 case DW_TAG_formal_parameter:
edb3359d
DJ
15971 /* If we are inside a function, mark this as an argument. If
15972 not, we might be looking at an argument to an inlined function
15973 when we do not have enough information to show inlined frames;
15974 pretend it's a local variable in that case so that the user can
15975 still see it. */
15976 if (context_stack_depth > 0
15977 && context_stack[context_stack_depth - 1].name != NULL)
15978 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15979 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15980 if (attr)
15981 {
e7c27a73 15982 var_decode_location (attr, sym, cu);
c906108c 15983 }
e142c38c 15984 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15985 if (attr)
15986 {
e7c27a73 15987 dwarf2_const_value (attr, sym, cu);
c906108c 15988 }
f346a30d 15989
e37fd15a 15990 list_to_add = cu->list_in_scope;
c906108c
SS
15991 break;
15992 case DW_TAG_unspecified_parameters:
15993 /* From varargs functions; gdb doesn't seem to have any
15994 interest in this information, so just ignore it for now.
15995 (FIXME?) */
15996 break;
34eaf542
TT
15997 case DW_TAG_template_type_param:
15998 suppress_add = 1;
15999 /* Fall through. */
c906108c 16000 case DW_TAG_class_type:
680b30c7 16001 case DW_TAG_interface_type:
c906108c
SS
16002 case DW_TAG_structure_type:
16003 case DW_TAG_union_type:
72019c9c 16004 case DW_TAG_set_type:
c906108c
SS
16005 case DW_TAG_enumeration_type:
16006 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16007 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16008
63d06c5c 16009 {
987504bb 16010 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16011 really ever be static objects: otherwise, if you try
16012 to, say, break of a class's method and you're in a file
16013 which doesn't mention that class, it won't work unless
16014 the check for all static symbols in lookup_symbol_aux
16015 saves you. See the OtherFileClass tests in
16016 gdb.c++/namespace.exp. */
16017
e37fd15a 16018 if (!suppress_add)
34eaf542 16019 {
34eaf542
TT
16020 list_to_add = (cu->list_in_scope == &file_symbols
16021 && (cu->language == language_cplus
16022 || cu->language == language_java)
16023 ? &global_symbols : cu->list_in_scope);
63d06c5c 16024
64382290
TT
16025 /* The semantics of C++ state that "struct foo {
16026 ... }" also defines a typedef for "foo". A Java
16027 class declaration also defines a typedef for the
16028 class. */
16029 if (cu->language == language_cplus
16030 || cu->language == language_java
16031 || cu->language == language_ada)
16032 {
16033 /* The symbol's name is already allocated along
16034 with this objfile, so we don't need to
16035 duplicate it for the type. */
16036 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16037 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16038 }
63d06c5c
DC
16039 }
16040 }
c906108c
SS
16041 break;
16042 case DW_TAG_typedef:
63d06c5c
DC
16043 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16044 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16045 list_to_add = cu->list_in_scope;
63d06c5c 16046 break;
c906108c 16047 case DW_TAG_base_type:
a02abb62 16048 case DW_TAG_subrange_type:
c906108c 16049 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16050 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16051 list_to_add = cu->list_in_scope;
c906108c
SS
16052 break;
16053 case DW_TAG_enumerator:
e142c38c 16054 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16055 if (attr)
16056 {
e7c27a73 16057 dwarf2_const_value (attr, sym, cu);
c906108c 16058 }
63d06c5c
DC
16059 {
16060 /* NOTE: carlton/2003-11-10: See comment above in the
16061 DW_TAG_class_type, etc. block. */
16062
e142c38c 16063 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16064 && (cu->language == language_cplus
16065 || cu->language == language_java)
e142c38c 16066 ? &global_symbols : cu->list_in_scope);
63d06c5c 16067 }
c906108c 16068 break;
5c4e30ca
DC
16069 case DW_TAG_namespace:
16070 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 16071 list_to_add = &global_symbols;
5c4e30ca 16072 break;
4357ac6c 16073 case DW_TAG_common_block:
5a352474 16074 SYMBOL_CLASS (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16075 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16076 add_symbol_to_list (sym, cu->list_in_scope);
16077 break;
c906108c
SS
16078 default:
16079 /* Not a tag we recognize. Hopefully we aren't processing
16080 trash data, but since we must specifically ignore things
16081 we don't recognize, there is nothing else we should do at
0963b4bd 16082 this point. */
e2e0b3e5 16083 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16084 dwarf_tag_name (die->tag));
c906108c
SS
16085 break;
16086 }
df8a16a1 16087
e37fd15a
SW
16088 if (suppress_add)
16089 {
16090 sym->hash_next = objfile->template_symbols;
16091 objfile->template_symbols = sym;
16092 list_to_add = NULL;
16093 }
16094
16095 if (list_to_add != NULL)
16096 add_symbol_to_list (sym, list_to_add);
16097
df8a16a1
DJ
16098 /* For the benefit of old versions of GCC, check for anonymous
16099 namespaces based on the demangled name. */
16100 if (!processing_has_namespace_info
94af9270 16101 && cu->language == language_cplus)
a10964d1 16102 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16103 }
16104 return (sym);
16105}
16106
34eaf542
TT
16107/* A wrapper for new_symbol_full that always allocates a new symbol. */
16108
16109static struct symbol *
16110new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16111{
16112 return new_symbol_full (die, type, cu, NULL);
16113}
16114
98bfdba5
PA
16115/* Given an attr with a DW_FORM_dataN value in host byte order,
16116 zero-extend it as appropriate for the symbol's type. The DWARF
16117 standard (v4) is not entirely clear about the meaning of using
16118 DW_FORM_dataN for a constant with a signed type, where the type is
16119 wider than the data. The conclusion of a discussion on the DWARF
16120 list was that this is unspecified. We choose to always zero-extend
16121 because that is the interpretation long in use by GCC. */
c906108c 16122
98bfdba5
PA
16123static gdb_byte *
16124dwarf2_const_value_data (struct attribute *attr, struct type *type,
16125 const char *name, struct obstack *obstack,
12df843f 16126 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16127{
e7c27a73 16128 struct objfile *objfile = cu->objfile;
e17a4113
UW
16129 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16130 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16131 LONGEST l = DW_UNSND (attr);
16132
16133 if (bits < sizeof (*value) * 8)
16134 {
16135 l &= ((LONGEST) 1 << bits) - 1;
16136 *value = l;
16137 }
16138 else if (bits == sizeof (*value) * 8)
16139 *value = l;
16140 else
16141 {
16142 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16143 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16144 return bytes;
16145 }
16146
16147 return NULL;
16148}
16149
16150/* Read a constant value from an attribute. Either set *VALUE, or if
16151 the value does not fit in *VALUE, set *BYTES - either already
16152 allocated on the objfile obstack, or newly allocated on OBSTACK,
16153 or, set *BATON, if we translated the constant to a location
16154 expression. */
16155
16156static void
16157dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16158 const char *name, struct obstack *obstack,
16159 struct dwarf2_cu *cu,
12df843f 16160 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16161 struct dwarf2_locexpr_baton **baton)
16162{
16163 struct objfile *objfile = cu->objfile;
16164 struct comp_unit_head *cu_header = &cu->header;
c906108c 16165 struct dwarf_block *blk;
98bfdba5
PA
16166 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16167 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16168
16169 *value = 0;
16170 *bytes = NULL;
16171 *baton = NULL;
c906108c
SS
16172
16173 switch (attr->form)
16174 {
16175 case DW_FORM_addr:
3019eac3 16176 case DW_FORM_GNU_addr_index:
ac56253d 16177 {
ac56253d
TT
16178 gdb_byte *data;
16179
98bfdba5
PA
16180 if (TYPE_LENGTH (type) != cu_header->addr_size)
16181 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16182 cu_header->addr_size,
98bfdba5 16183 TYPE_LENGTH (type));
ac56253d
TT
16184 /* Symbols of this form are reasonably rare, so we just
16185 piggyback on the existing location code rather than writing
16186 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16187 *baton = obstack_alloc (&objfile->objfile_obstack,
16188 sizeof (struct dwarf2_locexpr_baton));
16189 (*baton)->per_cu = cu->per_cu;
16190 gdb_assert ((*baton)->per_cu);
ac56253d 16191
98bfdba5
PA
16192 (*baton)->size = 2 + cu_header->addr_size;
16193 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16194 (*baton)->data = data;
ac56253d
TT
16195
16196 data[0] = DW_OP_addr;
16197 store_unsigned_integer (&data[1], cu_header->addr_size,
16198 byte_order, DW_ADDR (attr));
16199 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16200 }
c906108c 16201 break;
4ac36638 16202 case DW_FORM_string:
93b5768b 16203 case DW_FORM_strp:
3019eac3 16204 case DW_FORM_GNU_str_index:
36586728 16205 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16206 /* DW_STRING is already allocated on the objfile obstack, point
16207 directly to it. */
16208 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16209 break;
c906108c
SS
16210 case DW_FORM_block1:
16211 case DW_FORM_block2:
16212 case DW_FORM_block4:
16213 case DW_FORM_block:
2dc7f7b3 16214 case DW_FORM_exprloc:
c906108c 16215 blk = DW_BLOCK (attr);
98bfdba5
PA
16216 if (TYPE_LENGTH (type) != blk->size)
16217 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16218 TYPE_LENGTH (type));
16219 *bytes = blk->data;
c906108c 16220 break;
2df3850c
JM
16221
16222 /* The DW_AT_const_value attributes are supposed to carry the
16223 symbol's value "represented as it would be on the target
16224 architecture." By the time we get here, it's already been
16225 converted to host endianness, so we just need to sign- or
16226 zero-extend it as appropriate. */
16227 case DW_FORM_data1:
3e43a32a
MS
16228 *bytes = dwarf2_const_value_data (attr, type, name,
16229 obstack, cu, value, 8);
2df3850c 16230 break;
c906108c 16231 case DW_FORM_data2:
3e43a32a
MS
16232 *bytes = dwarf2_const_value_data (attr, type, name,
16233 obstack, cu, value, 16);
2df3850c 16234 break;
c906108c 16235 case DW_FORM_data4:
3e43a32a
MS
16236 *bytes = dwarf2_const_value_data (attr, type, name,
16237 obstack, cu, value, 32);
2df3850c 16238 break;
c906108c 16239 case DW_FORM_data8:
3e43a32a
MS
16240 *bytes = dwarf2_const_value_data (attr, type, name,
16241 obstack, cu, value, 64);
2df3850c
JM
16242 break;
16243
c906108c 16244 case DW_FORM_sdata:
98bfdba5 16245 *value = DW_SND (attr);
2df3850c
JM
16246 break;
16247
c906108c 16248 case DW_FORM_udata:
98bfdba5 16249 *value = DW_UNSND (attr);
c906108c 16250 break;
2df3850c 16251
c906108c 16252 default:
4d3c2250 16253 complaint (&symfile_complaints,
e2e0b3e5 16254 _("unsupported const value attribute form: '%s'"),
4d3c2250 16255 dwarf_form_name (attr->form));
98bfdba5 16256 *value = 0;
c906108c
SS
16257 break;
16258 }
16259}
16260
2df3850c 16261
98bfdba5
PA
16262/* Copy constant value from an attribute to a symbol. */
16263
2df3850c 16264static void
98bfdba5
PA
16265dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16266 struct dwarf2_cu *cu)
2df3850c 16267{
98bfdba5
PA
16268 struct objfile *objfile = cu->objfile;
16269 struct comp_unit_head *cu_header = &cu->header;
12df843f 16270 LONGEST value;
98bfdba5
PA
16271 gdb_byte *bytes;
16272 struct dwarf2_locexpr_baton *baton;
2df3850c 16273
98bfdba5
PA
16274 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16275 SYMBOL_PRINT_NAME (sym),
16276 &objfile->objfile_obstack, cu,
16277 &value, &bytes, &baton);
2df3850c 16278
98bfdba5
PA
16279 if (baton != NULL)
16280 {
16281 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16282 SYMBOL_LOCATION_BATON (sym) = baton;
16283 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16284 }
16285 else if (bytes != NULL)
16286 {
16287 SYMBOL_VALUE_BYTES (sym) = bytes;
16288 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16289 }
16290 else
16291 {
16292 SYMBOL_VALUE (sym) = value;
16293 SYMBOL_CLASS (sym) = LOC_CONST;
16294 }
2df3850c
JM
16295}
16296
c906108c
SS
16297/* Return the type of the die in question using its DW_AT_type attribute. */
16298
16299static struct type *
e7c27a73 16300die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16301{
c906108c 16302 struct attribute *type_attr;
c906108c 16303
e142c38c 16304 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16305 if (!type_attr)
16306 {
16307 /* A missing DW_AT_type represents a void type. */
46bf5051 16308 return objfile_type (cu->objfile)->builtin_void;
c906108c 16309 }
348e048f 16310
673bfd45 16311 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16312}
16313
b4ba55a1
JB
16314/* True iff CU's producer generates GNAT Ada auxiliary information
16315 that allows to find parallel types through that information instead
16316 of having to do expensive parallel lookups by type name. */
16317
16318static int
16319need_gnat_info (struct dwarf2_cu *cu)
16320{
16321 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16322 of GNAT produces this auxiliary information, without any indication
16323 that it is produced. Part of enhancing the FSF version of GNAT
16324 to produce that information will be to put in place an indicator
16325 that we can use in order to determine whether the descriptive type
16326 info is available or not. One suggestion that has been made is
16327 to use a new attribute, attached to the CU die. For now, assume
16328 that the descriptive type info is not available. */
16329 return 0;
16330}
16331
b4ba55a1
JB
16332/* Return the auxiliary type of the die in question using its
16333 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16334 attribute is not present. */
16335
16336static struct type *
16337die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16338{
b4ba55a1 16339 struct attribute *type_attr;
b4ba55a1
JB
16340
16341 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16342 if (!type_attr)
16343 return NULL;
16344
673bfd45 16345 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16346}
16347
16348/* If DIE has a descriptive_type attribute, then set the TYPE's
16349 descriptive type accordingly. */
16350
16351static void
16352set_descriptive_type (struct type *type, struct die_info *die,
16353 struct dwarf2_cu *cu)
16354{
16355 struct type *descriptive_type = die_descriptive_type (die, cu);
16356
16357 if (descriptive_type)
16358 {
16359 ALLOCATE_GNAT_AUX_TYPE (type);
16360 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16361 }
16362}
16363
c906108c
SS
16364/* Return the containing type of the die in question using its
16365 DW_AT_containing_type attribute. */
16366
16367static struct type *
e7c27a73 16368die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16369{
c906108c 16370 struct attribute *type_attr;
c906108c 16371
e142c38c 16372 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16373 if (!type_attr)
16374 error (_("Dwarf Error: Problem turning containing type into gdb type "
16375 "[in module %s]"), cu->objfile->name);
16376
673bfd45 16377 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16378}
16379
673bfd45
DE
16380/* Look up the type of DIE in CU using its type attribute ATTR.
16381 If there is no type substitute an error marker. */
16382
c906108c 16383static struct type *
673bfd45
DE
16384lookup_die_type (struct die_info *die, struct attribute *attr,
16385 struct dwarf2_cu *cu)
c906108c 16386{
bb5ed363 16387 struct objfile *objfile = cu->objfile;
f792889a
DJ
16388 struct type *this_type;
16389
673bfd45
DE
16390 /* First see if we have it cached. */
16391
36586728
TT
16392 if (attr->form == DW_FORM_GNU_ref_alt)
16393 {
16394 struct dwarf2_per_cu_data *per_cu;
16395 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16396
16397 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16398 this_type = get_die_type_at_offset (offset, per_cu);
16399 }
16400 else if (is_ref_attr (attr))
673bfd45 16401 {
b64f50a1 16402 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16403
16404 this_type = get_die_type_at_offset (offset, cu->per_cu);
16405 }
55f1336d 16406 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16407 {
16408 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16409
16410 /* sig_type will be NULL if the signatured type is missing from
16411 the debug info. */
16412 if (sig_type == NULL)
16413 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16414 "at 0x%x [in module %s]"),
b64f50a1 16415 die->offset.sect_off, objfile->name);
673bfd45 16416
3019eac3
DE
16417 gdb_assert (sig_type->per_cu.is_debug_types);
16418 /* If we haven't filled in type_offset_in_section yet, then we
16419 haven't read the type in yet. */
16420 this_type = NULL;
16421 if (sig_type->type_offset_in_section.sect_off != 0)
16422 {
16423 this_type =
16424 get_die_type_at_offset (sig_type->type_offset_in_section,
16425 &sig_type->per_cu);
16426 }
673bfd45
DE
16427 }
16428 else
16429 {
16430 dump_die_for_error (die);
16431 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16432 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16433 }
16434
16435 /* If not cached we need to read it in. */
16436
16437 if (this_type == NULL)
16438 {
16439 struct die_info *type_die;
16440 struct dwarf2_cu *type_cu = cu;
16441
16442 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16443 /* If we found the type now, it's probably because the type came
16444 from an inter-CU reference and the type's CU got expanded before
16445 ours. */
16446 this_type = get_die_type (type_die, type_cu);
16447 if (this_type == NULL)
16448 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16449 }
16450
16451 /* If we still don't have a type use an error marker. */
16452
16453 if (this_type == NULL)
c906108c 16454 {
b00fdb78
TT
16455 char *message, *saved;
16456
16457 /* read_type_die already issued a complaint. */
16458 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16459 objfile->name,
b64f50a1
JK
16460 cu->header.offset.sect_off,
16461 die->offset.sect_off);
bb5ed363 16462 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16463 message, strlen (message));
16464 xfree (message);
16465
bb5ed363 16466 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16467 }
673bfd45 16468
f792889a 16469 return this_type;
c906108c
SS
16470}
16471
673bfd45
DE
16472/* Return the type in DIE, CU.
16473 Returns NULL for invalid types.
16474
16475 This first does a lookup in the appropriate type_hash table,
16476 and only reads the die in if necessary.
16477
16478 NOTE: This can be called when reading in partial or full symbols. */
16479
f792889a 16480static struct type *
e7c27a73 16481read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16482{
f792889a
DJ
16483 struct type *this_type;
16484
16485 this_type = get_die_type (die, cu);
16486 if (this_type)
16487 return this_type;
16488
673bfd45
DE
16489 return read_type_die_1 (die, cu);
16490}
16491
16492/* Read the type in DIE, CU.
16493 Returns NULL for invalid types. */
16494
16495static struct type *
16496read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16497{
16498 struct type *this_type = NULL;
16499
c906108c
SS
16500 switch (die->tag)
16501 {
16502 case DW_TAG_class_type:
680b30c7 16503 case DW_TAG_interface_type:
c906108c
SS
16504 case DW_TAG_structure_type:
16505 case DW_TAG_union_type:
f792889a 16506 this_type = read_structure_type (die, cu);
c906108c
SS
16507 break;
16508 case DW_TAG_enumeration_type:
f792889a 16509 this_type = read_enumeration_type (die, cu);
c906108c
SS
16510 break;
16511 case DW_TAG_subprogram:
16512 case DW_TAG_subroutine_type:
edb3359d 16513 case DW_TAG_inlined_subroutine:
f792889a 16514 this_type = read_subroutine_type (die, cu);
c906108c
SS
16515 break;
16516 case DW_TAG_array_type:
f792889a 16517 this_type = read_array_type (die, cu);
c906108c 16518 break;
72019c9c 16519 case DW_TAG_set_type:
f792889a 16520 this_type = read_set_type (die, cu);
72019c9c 16521 break;
c906108c 16522 case DW_TAG_pointer_type:
f792889a 16523 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16524 break;
16525 case DW_TAG_ptr_to_member_type:
f792889a 16526 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16527 break;
16528 case DW_TAG_reference_type:
f792889a 16529 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16530 break;
16531 case DW_TAG_const_type:
f792889a 16532 this_type = read_tag_const_type (die, cu);
c906108c
SS
16533 break;
16534 case DW_TAG_volatile_type:
f792889a 16535 this_type = read_tag_volatile_type (die, cu);
c906108c 16536 break;
06d66ee9
TT
16537 case DW_TAG_restrict_type:
16538 this_type = read_tag_restrict_type (die, cu);
16539 break;
c906108c 16540 case DW_TAG_string_type:
f792889a 16541 this_type = read_tag_string_type (die, cu);
c906108c
SS
16542 break;
16543 case DW_TAG_typedef:
f792889a 16544 this_type = read_typedef (die, cu);
c906108c 16545 break;
a02abb62 16546 case DW_TAG_subrange_type:
f792889a 16547 this_type = read_subrange_type (die, cu);
a02abb62 16548 break;
c906108c 16549 case DW_TAG_base_type:
f792889a 16550 this_type = read_base_type (die, cu);
c906108c 16551 break;
81a17f79 16552 case DW_TAG_unspecified_type:
f792889a 16553 this_type = read_unspecified_type (die, cu);
81a17f79 16554 break;
0114d602
DJ
16555 case DW_TAG_namespace:
16556 this_type = read_namespace_type (die, cu);
16557 break;
f55ee35c
JK
16558 case DW_TAG_module:
16559 this_type = read_module_type (die, cu);
16560 break;
c906108c 16561 default:
3e43a32a
MS
16562 complaint (&symfile_complaints,
16563 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16564 dwarf_tag_name (die->tag));
c906108c
SS
16565 break;
16566 }
63d06c5c 16567
f792889a 16568 return this_type;
63d06c5c
DC
16569}
16570
abc72ce4
DE
16571/* See if we can figure out if the class lives in a namespace. We do
16572 this by looking for a member function; its demangled name will
16573 contain namespace info, if there is any.
16574 Return the computed name or NULL.
16575 Space for the result is allocated on the objfile's obstack.
16576 This is the full-die version of guess_partial_die_structure_name.
16577 In this case we know DIE has no useful parent. */
16578
16579static char *
16580guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16581{
16582 struct die_info *spec_die;
16583 struct dwarf2_cu *spec_cu;
16584 struct die_info *child;
16585
16586 spec_cu = cu;
16587 spec_die = die_specification (die, &spec_cu);
16588 if (spec_die != NULL)
16589 {
16590 die = spec_die;
16591 cu = spec_cu;
16592 }
16593
16594 for (child = die->child;
16595 child != NULL;
16596 child = child->sibling)
16597 {
16598 if (child->tag == DW_TAG_subprogram)
16599 {
16600 struct attribute *attr;
16601
16602 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16603 if (attr == NULL)
16604 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16605 if (attr != NULL)
16606 {
16607 char *actual_name
16608 = language_class_name_from_physname (cu->language_defn,
16609 DW_STRING (attr));
16610 char *name = NULL;
16611
16612 if (actual_name != NULL)
16613 {
15d034d0 16614 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16615
16616 if (die_name != NULL
16617 && strcmp (die_name, actual_name) != 0)
16618 {
16619 /* Strip off the class name from the full name.
16620 We want the prefix. */
16621 int die_name_len = strlen (die_name);
16622 int actual_name_len = strlen (actual_name);
16623
16624 /* Test for '::' as a sanity check. */
16625 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16626 && actual_name[actual_name_len
16627 - die_name_len - 1] == ':')
abc72ce4
DE
16628 name =
16629 obsavestring (actual_name,
16630 actual_name_len - die_name_len - 2,
16631 &cu->objfile->objfile_obstack);
16632 }
16633 }
16634 xfree (actual_name);
16635 return name;
16636 }
16637 }
16638 }
16639
16640 return NULL;
16641}
16642
96408a79
SA
16643/* GCC might emit a nameless typedef that has a linkage name. Determine the
16644 prefix part in such case. See
16645 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16646
16647static char *
16648anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16649{
16650 struct attribute *attr;
16651 char *base;
16652
16653 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16654 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16655 return NULL;
16656
16657 attr = dwarf2_attr (die, DW_AT_name, cu);
16658 if (attr != NULL && DW_STRING (attr) != NULL)
16659 return NULL;
16660
16661 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16662 if (attr == NULL)
16663 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16664 if (attr == NULL || DW_STRING (attr) == NULL)
16665 return NULL;
16666
16667 /* dwarf2_name had to be already called. */
16668 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16669
16670 /* Strip the base name, keep any leading namespaces/classes. */
16671 base = strrchr (DW_STRING (attr), ':');
16672 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16673 return "";
16674
16675 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16676 &cu->objfile->objfile_obstack);
16677}
16678
fdde2d81 16679/* Return the name of the namespace/class that DIE is defined within,
0114d602 16680 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16681
0114d602
DJ
16682 For example, if we're within the method foo() in the following
16683 code:
16684
16685 namespace N {
16686 class C {
16687 void foo () {
16688 }
16689 };
16690 }
16691
16692 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16693
0d5cff50 16694static const char *
e142c38c 16695determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16696{
0114d602
DJ
16697 struct die_info *parent, *spec_die;
16698 struct dwarf2_cu *spec_cu;
16699 struct type *parent_type;
96408a79 16700 char *retval;
63d06c5c 16701
f55ee35c
JK
16702 if (cu->language != language_cplus && cu->language != language_java
16703 && cu->language != language_fortran)
0114d602
DJ
16704 return "";
16705
96408a79
SA
16706 retval = anonymous_struct_prefix (die, cu);
16707 if (retval)
16708 return retval;
16709
0114d602
DJ
16710 /* We have to be careful in the presence of DW_AT_specification.
16711 For example, with GCC 3.4, given the code
16712
16713 namespace N {
16714 void foo() {
16715 // Definition of N::foo.
16716 }
16717 }
16718
16719 then we'll have a tree of DIEs like this:
16720
16721 1: DW_TAG_compile_unit
16722 2: DW_TAG_namespace // N
16723 3: DW_TAG_subprogram // declaration of N::foo
16724 4: DW_TAG_subprogram // definition of N::foo
16725 DW_AT_specification // refers to die #3
16726
16727 Thus, when processing die #4, we have to pretend that we're in
16728 the context of its DW_AT_specification, namely the contex of die
16729 #3. */
16730 spec_cu = cu;
16731 spec_die = die_specification (die, &spec_cu);
16732 if (spec_die == NULL)
16733 parent = die->parent;
16734 else
63d06c5c 16735 {
0114d602
DJ
16736 parent = spec_die->parent;
16737 cu = spec_cu;
63d06c5c 16738 }
0114d602
DJ
16739
16740 if (parent == NULL)
16741 return "";
98bfdba5
PA
16742 else if (parent->building_fullname)
16743 {
16744 const char *name;
16745 const char *parent_name;
16746
16747 /* It has been seen on RealView 2.2 built binaries,
16748 DW_TAG_template_type_param types actually _defined_ as
16749 children of the parent class:
16750
16751 enum E {};
16752 template class <class Enum> Class{};
16753 Class<enum E> class_e;
16754
16755 1: DW_TAG_class_type (Class)
16756 2: DW_TAG_enumeration_type (E)
16757 3: DW_TAG_enumerator (enum1:0)
16758 3: DW_TAG_enumerator (enum2:1)
16759 ...
16760 2: DW_TAG_template_type_param
16761 DW_AT_type DW_FORM_ref_udata (E)
16762
16763 Besides being broken debug info, it can put GDB into an
16764 infinite loop. Consider:
16765
16766 When we're building the full name for Class<E>, we'll start
16767 at Class, and go look over its template type parameters,
16768 finding E. We'll then try to build the full name of E, and
16769 reach here. We're now trying to build the full name of E,
16770 and look over the parent DIE for containing scope. In the
16771 broken case, if we followed the parent DIE of E, we'd again
16772 find Class, and once again go look at its template type
16773 arguments, etc., etc. Simply don't consider such parent die
16774 as source-level parent of this die (it can't be, the language
16775 doesn't allow it), and break the loop here. */
16776 name = dwarf2_name (die, cu);
16777 parent_name = dwarf2_name (parent, cu);
16778 complaint (&symfile_complaints,
16779 _("template param type '%s' defined within parent '%s'"),
16780 name ? name : "<unknown>",
16781 parent_name ? parent_name : "<unknown>");
16782 return "";
16783 }
63d06c5c 16784 else
0114d602
DJ
16785 switch (parent->tag)
16786 {
63d06c5c 16787 case DW_TAG_namespace:
0114d602 16788 parent_type = read_type_die (parent, cu);
acebe513
UW
16789 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16790 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16791 Work around this problem here. */
16792 if (cu->language == language_cplus
16793 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16794 return "";
0114d602
DJ
16795 /* We give a name to even anonymous namespaces. */
16796 return TYPE_TAG_NAME (parent_type);
63d06c5c 16797 case DW_TAG_class_type:
680b30c7 16798 case DW_TAG_interface_type:
63d06c5c 16799 case DW_TAG_structure_type:
0114d602 16800 case DW_TAG_union_type:
f55ee35c 16801 case DW_TAG_module:
0114d602
DJ
16802 parent_type = read_type_die (parent, cu);
16803 if (TYPE_TAG_NAME (parent_type) != NULL)
16804 return TYPE_TAG_NAME (parent_type);
16805 else
16806 /* An anonymous structure is only allowed non-static data
16807 members; no typedefs, no member functions, et cetera.
16808 So it does not need a prefix. */
16809 return "";
abc72ce4 16810 case DW_TAG_compile_unit:
95554aad 16811 case DW_TAG_partial_unit:
abc72ce4
DE
16812 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16813 if (cu->language == language_cplus
8b70b953 16814 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16815 && die->child != NULL
16816 && (die->tag == DW_TAG_class_type
16817 || die->tag == DW_TAG_structure_type
16818 || die->tag == DW_TAG_union_type))
16819 {
16820 char *name = guess_full_die_structure_name (die, cu);
16821 if (name != NULL)
16822 return name;
16823 }
16824 return "";
63d06c5c 16825 default:
8176b9b8 16826 return determine_prefix (parent, cu);
63d06c5c 16827 }
63d06c5c
DC
16828}
16829
3e43a32a
MS
16830/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16831 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16832 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16833 an obconcat, otherwise allocate storage for the result. The CU argument is
16834 used to determine the language and hence, the appropriate separator. */
987504bb 16835
f55ee35c 16836#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16837
16838static char *
f55ee35c
JK
16839typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16840 int physname, struct dwarf2_cu *cu)
63d06c5c 16841{
f55ee35c 16842 const char *lead = "";
5c315b68 16843 const char *sep;
63d06c5c 16844
3e43a32a
MS
16845 if (suffix == NULL || suffix[0] == '\0'
16846 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16847 sep = "";
16848 else if (cu->language == language_java)
16849 sep = ".";
f55ee35c
JK
16850 else if (cu->language == language_fortran && physname)
16851 {
16852 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16853 DW_AT_MIPS_linkage_name is preferred and used instead. */
16854
16855 lead = "__";
16856 sep = "_MOD_";
16857 }
987504bb
JJ
16858 else
16859 sep = "::";
63d06c5c 16860
6dd47d34
DE
16861 if (prefix == NULL)
16862 prefix = "";
16863 if (suffix == NULL)
16864 suffix = "";
16865
987504bb
JJ
16866 if (obs == NULL)
16867 {
3e43a32a
MS
16868 char *retval
16869 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16870
f55ee35c
JK
16871 strcpy (retval, lead);
16872 strcat (retval, prefix);
6dd47d34
DE
16873 strcat (retval, sep);
16874 strcat (retval, suffix);
63d06c5c
DC
16875 return retval;
16876 }
987504bb
JJ
16877 else
16878 {
16879 /* We have an obstack. */
f55ee35c 16880 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16881 }
63d06c5c
DC
16882}
16883
c906108c
SS
16884/* Return sibling of die, NULL if no sibling. */
16885
f9aca02d 16886static struct die_info *
fba45db2 16887sibling_die (struct die_info *die)
c906108c 16888{
639d11d3 16889 return die->sibling;
c906108c
SS
16890}
16891
71c25dea
TT
16892/* Get name of a die, return NULL if not found. */
16893
15d034d0
TT
16894static const char *
16895dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
16896 struct obstack *obstack)
16897{
16898 if (name && cu->language == language_cplus)
16899 {
16900 char *canon_name = cp_canonicalize_string (name);
16901
16902 if (canon_name != NULL)
16903 {
16904 if (strcmp (canon_name, name) != 0)
16905 name = obsavestring (canon_name, strlen (canon_name),
16906 obstack);
16907 xfree (canon_name);
16908 }
16909 }
16910
16911 return name;
c906108c
SS
16912}
16913
9219021c
DC
16914/* Get name of a die, return NULL if not found. */
16915
15d034d0 16916static const char *
e142c38c 16917dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16918{
16919 struct attribute *attr;
16920
e142c38c 16921 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16922 if ((!attr || !DW_STRING (attr))
16923 && die->tag != DW_TAG_class_type
16924 && die->tag != DW_TAG_interface_type
16925 && die->tag != DW_TAG_structure_type
16926 && die->tag != DW_TAG_union_type)
71c25dea
TT
16927 return NULL;
16928
16929 switch (die->tag)
16930 {
16931 case DW_TAG_compile_unit:
95554aad 16932 case DW_TAG_partial_unit:
71c25dea
TT
16933 /* Compilation units have a DW_AT_name that is a filename, not
16934 a source language identifier. */
16935 case DW_TAG_enumeration_type:
16936 case DW_TAG_enumerator:
16937 /* These tags always have simple identifiers already; no need
16938 to canonicalize them. */
16939 return DW_STRING (attr);
907af001 16940
418835cc
KS
16941 case DW_TAG_subprogram:
16942 /* Java constructors will all be named "<init>", so return
16943 the class name when we see this special case. */
16944 if (cu->language == language_java
16945 && DW_STRING (attr) != NULL
16946 && strcmp (DW_STRING (attr), "<init>") == 0)
16947 {
16948 struct dwarf2_cu *spec_cu = cu;
16949 struct die_info *spec_die;
16950
16951 /* GCJ will output '<init>' for Java constructor names.
16952 For this special case, return the name of the parent class. */
16953
16954 /* GCJ may output suprogram DIEs with AT_specification set.
16955 If so, use the name of the specified DIE. */
16956 spec_die = die_specification (die, &spec_cu);
16957 if (spec_die != NULL)
16958 return dwarf2_name (spec_die, spec_cu);
16959
16960 do
16961 {
16962 die = die->parent;
16963 if (die->tag == DW_TAG_class_type)
16964 return dwarf2_name (die, cu);
16965 }
95554aad
TT
16966 while (die->tag != DW_TAG_compile_unit
16967 && die->tag != DW_TAG_partial_unit);
418835cc 16968 }
907af001
UW
16969 break;
16970
16971 case DW_TAG_class_type:
16972 case DW_TAG_interface_type:
16973 case DW_TAG_structure_type:
16974 case DW_TAG_union_type:
16975 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16976 structures or unions. These were of the form "._%d" in GCC 4.1,
16977 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16978 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16979 if (attr && DW_STRING (attr)
16980 && (strncmp (DW_STRING (attr), "._", 2) == 0
16981 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16982 return NULL;
53832f31
TT
16983
16984 /* GCC might emit a nameless typedef that has a linkage name. See
16985 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16986 if (!attr || DW_STRING (attr) == NULL)
16987 {
df5c6c50 16988 char *demangled = NULL;
53832f31
TT
16989
16990 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16991 if (attr == NULL)
16992 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16993
16994 if (attr == NULL || DW_STRING (attr) == NULL)
16995 return NULL;
16996
df5c6c50
JK
16997 /* Avoid demangling DW_STRING (attr) the second time on a second
16998 call for the same DIE. */
16999 if (!DW_STRING_IS_CANONICAL (attr))
17000 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17001
17002 if (demangled)
17003 {
96408a79
SA
17004 char *base;
17005
53832f31 17006 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
17007 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
17008 &cu->objfile->objfile_obstack);
53832f31
TT
17009 DW_STRING_IS_CANONICAL (attr) = 1;
17010 xfree (demangled);
96408a79
SA
17011
17012 /* Strip any leading namespaces/classes, keep only the base name.
17013 DW_AT_name for named DIEs does not contain the prefixes. */
17014 base = strrchr (DW_STRING (attr), ':');
17015 if (base && base > DW_STRING (attr) && base[-1] == ':')
17016 return &base[1];
17017 else
17018 return DW_STRING (attr);
53832f31
TT
17019 }
17020 }
907af001
UW
17021 break;
17022
71c25dea 17023 default:
907af001
UW
17024 break;
17025 }
17026
17027 if (!DW_STRING_IS_CANONICAL (attr))
17028 {
17029 DW_STRING (attr)
17030 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17031 &cu->objfile->objfile_obstack);
17032 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17033 }
907af001 17034 return DW_STRING (attr);
9219021c
DC
17035}
17036
17037/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17038 is none. *EXT_CU is the CU containing DIE on input, and the CU
17039 containing the return value on output. */
9219021c
DC
17040
17041static struct die_info *
f2f0e013 17042dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17043{
17044 struct attribute *attr;
9219021c 17045
f2f0e013 17046 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17047 if (attr == NULL)
17048 return NULL;
17049
f2f0e013 17050 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17051}
17052
c906108c
SS
17053/* Convert a DIE tag into its string name. */
17054
f39c6ffd 17055static const char *
aa1ee363 17056dwarf_tag_name (unsigned tag)
c906108c 17057{
f39c6ffd
TT
17058 const char *name = get_DW_TAG_name (tag);
17059
17060 if (name == NULL)
17061 return "DW_TAG_<unknown>";
17062
17063 return name;
c906108c
SS
17064}
17065
17066/* Convert a DWARF attribute code into its string name. */
17067
f39c6ffd 17068static const char *
aa1ee363 17069dwarf_attr_name (unsigned attr)
c906108c 17070{
f39c6ffd
TT
17071 const char *name;
17072
c764a876 17073#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17074 if (attr == DW_AT_MIPS_fde)
17075 return "DW_AT_MIPS_fde";
17076#else
17077 if (attr == DW_AT_HP_block_index)
17078 return "DW_AT_HP_block_index";
c764a876 17079#endif
f39c6ffd
TT
17080
17081 name = get_DW_AT_name (attr);
17082
17083 if (name == NULL)
17084 return "DW_AT_<unknown>";
17085
17086 return name;
c906108c
SS
17087}
17088
17089/* Convert a DWARF value form code into its string name. */
17090
f39c6ffd 17091static const char *
aa1ee363 17092dwarf_form_name (unsigned form)
c906108c 17093{
f39c6ffd
TT
17094 const char *name = get_DW_FORM_name (form);
17095
17096 if (name == NULL)
17097 return "DW_FORM_<unknown>";
17098
17099 return name;
c906108c
SS
17100}
17101
17102static char *
fba45db2 17103dwarf_bool_name (unsigned mybool)
c906108c
SS
17104{
17105 if (mybool)
17106 return "TRUE";
17107 else
17108 return "FALSE";
17109}
17110
17111/* Convert a DWARF type code into its string name. */
17112
f39c6ffd 17113static const char *
aa1ee363 17114dwarf_type_encoding_name (unsigned enc)
c906108c 17115{
f39c6ffd 17116 const char *name = get_DW_ATE_name (enc);
c906108c 17117
f39c6ffd
TT
17118 if (name == NULL)
17119 return "DW_ATE_<unknown>";
c906108c 17120
f39c6ffd 17121 return name;
c906108c 17122}
c906108c 17123
f9aca02d 17124static void
d97bc12b 17125dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17126{
17127 unsigned int i;
17128
d97bc12b
DE
17129 print_spaces (indent, f);
17130 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17131 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17132
17133 if (die->parent != NULL)
17134 {
17135 print_spaces (indent, f);
17136 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17137 die->parent->offset.sect_off);
d97bc12b
DE
17138 }
17139
17140 print_spaces (indent, f);
17141 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17142 dwarf_bool_name (die->child != NULL));
c906108c 17143
d97bc12b
DE
17144 print_spaces (indent, f);
17145 fprintf_unfiltered (f, " attributes:\n");
17146
c906108c
SS
17147 for (i = 0; i < die->num_attrs; ++i)
17148 {
d97bc12b
DE
17149 print_spaces (indent, f);
17150 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17151 dwarf_attr_name (die->attrs[i].name),
17152 dwarf_form_name (die->attrs[i].form));
d97bc12b 17153
c906108c
SS
17154 switch (die->attrs[i].form)
17155 {
c906108c 17156 case DW_FORM_addr:
3019eac3 17157 case DW_FORM_GNU_addr_index:
d97bc12b 17158 fprintf_unfiltered (f, "address: ");
5af949e3 17159 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17160 break;
17161 case DW_FORM_block2:
17162 case DW_FORM_block4:
17163 case DW_FORM_block:
17164 case DW_FORM_block1:
56eb65bd
SP
17165 fprintf_unfiltered (f, "block: size %s",
17166 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17167 break;
2dc7f7b3 17168 case DW_FORM_exprloc:
56eb65bd
SP
17169 fprintf_unfiltered (f, "expression: size %s",
17170 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17171 break;
4568ecf9
DE
17172 case DW_FORM_ref_addr:
17173 fprintf_unfiltered (f, "ref address: ");
17174 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17175 break;
36586728
TT
17176 case DW_FORM_GNU_ref_alt:
17177 fprintf_unfiltered (f, "alt ref address: ");
17178 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17179 break;
10b3939b
DJ
17180 case DW_FORM_ref1:
17181 case DW_FORM_ref2:
17182 case DW_FORM_ref4:
4568ecf9
DE
17183 case DW_FORM_ref8:
17184 case DW_FORM_ref_udata:
d97bc12b 17185 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17186 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17187 break;
c906108c
SS
17188 case DW_FORM_data1:
17189 case DW_FORM_data2:
17190 case DW_FORM_data4:
ce5d95e1 17191 case DW_FORM_data8:
c906108c
SS
17192 case DW_FORM_udata:
17193 case DW_FORM_sdata:
43bbcdc2
PH
17194 fprintf_unfiltered (f, "constant: %s",
17195 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17196 break;
2dc7f7b3
TT
17197 case DW_FORM_sec_offset:
17198 fprintf_unfiltered (f, "section offset: %s",
17199 pulongest (DW_UNSND (&die->attrs[i])));
17200 break;
55f1336d 17201 case DW_FORM_ref_sig8:
348e048f
DE
17202 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17203 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17204 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17205 else
17206 fprintf_unfiltered (f, "signatured type, offset: unknown");
17207 break;
c906108c 17208 case DW_FORM_string:
4bdf3d34 17209 case DW_FORM_strp:
3019eac3 17210 case DW_FORM_GNU_str_index:
36586728 17211 case DW_FORM_GNU_strp_alt:
8285870a 17212 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17213 DW_STRING (&die->attrs[i])
8285870a
JK
17214 ? DW_STRING (&die->attrs[i]) : "",
17215 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17216 break;
17217 case DW_FORM_flag:
17218 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17219 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17220 else
d97bc12b 17221 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17222 break;
2dc7f7b3
TT
17223 case DW_FORM_flag_present:
17224 fprintf_unfiltered (f, "flag: TRUE");
17225 break;
a8329558 17226 case DW_FORM_indirect:
0963b4bd
MS
17227 /* The reader will have reduced the indirect form to
17228 the "base form" so this form should not occur. */
3e43a32a
MS
17229 fprintf_unfiltered (f,
17230 "unexpected attribute form: DW_FORM_indirect");
a8329558 17231 break;
c906108c 17232 default:
d97bc12b 17233 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17234 die->attrs[i].form);
d97bc12b 17235 break;
c906108c 17236 }
d97bc12b 17237 fprintf_unfiltered (f, "\n");
c906108c
SS
17238 }
17239}
17240
f9aca02d 17241static void
d97bc12b 17242dump_die_for_error (struct die_info *die)
c906108c 17243{
d97bc12b
DE
17244 dump_die_shallow (gdb_stderr, 0, die);
17245}
17246
17247static void
17248dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17249{
17250 int indent = level * 4;
17251
17252 gdb_assert (die != NULL);
17253
17254 if (level >= max_level)
17255 return;
17256
17257 dump_die_shallow (f, indent, die);
17258
17259 if (die->child != NULL)
c906108c 17260 {
d97bc12b
DE
17261 print_spaces (indent, f);
17262 fprintf_unfiltered (f, " Children:");
17263 if (level + 1 < max_level)
17264 {
17265 fprintf_unfiltered (f, "\n");
17266 dump_die_1 (f, level + 1, max_level, die->child);
17267 }
17268 else
17269 {
3e43a32a
MS
17270 fprintf_unfiltered (f,
17271 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17272 }
17273 }
17274
17275 if (die->sibling != NULL && level > 0)
17276 {
17277 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17278 }
17279}
17280
d97bc12b
DE
17281/* This is called from the pdie macro in gdbinit.in.
17282 It's not static so gcc will keep a copy callable from gdb. */
17283
17284void
17285dump_die (struct die_info *die, int max_level)
17286{
17287 dump_die_1 (gdb_stdlog, 0, max_level, die);
17288}
17289
f9aca02d 17290static void
51545339 17291store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17292{
51545339 17293 void **slot;
c906108c 17294
b64f50a1
JK
17295 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17296 INSERT);
51545339
DJ
17297
17298 *slot = die;
c906108c
SS
17299}
17300
b64f50a1
JK
17301/* DW_ADDR is always stored already as sect_offset; despite for the forms
17302 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17303
93311388
DE
17304static int
17305is_ref_attr (struct attribute *attr)
c906108c 17306{
c906108c
SS
17307 switch (attr->form)
17308 {
17309 case DW_FORM_ref_addr:
c906108c
SS
17310 case DW_FORM_ref1:
17311 case DW_FORM_ref2:
17312 case DW_FORM_ref4:
613e1657 17313 case DW_FORM_ref8:
c906108c 17314 case DW_FORM_ref_udata:
36586728 17315 case DW_FORM_GNU_ref_alt:
93311388 17316 return 1;
c906108c 17317 default:
93311388 17318 return 0;
c906108c 17319 }
93311388
DE
17320}
17321
b64f50a1
JK
17322/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17323 required kind. */
17324
17325static sect_offset
93311388
DE
17326dwarf2_get_ref_die_offset (struct attribute *attr)
17327{
4568ecf9 17328 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17329
93311388 17330 if (is_ref_attr (attr))
b64f50a1 17331 return retval;
93311388 17332
b64f50a1 17333 retval.sect_off = 0;
93311388
DE
17334 complaint (&symfile_complaints,
17335 _("unsupported die ref attribute form: '%s'"),
17336 dwarf_form_name (attr->form));
b64f50a1 17337 return retval;
c906108c
SS
17338}
17339
43bbcdc2
PH
17340/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17341 * the value held by the attribute is not constant. */
a02abb62 17342
43bbcdc2 17343static LONGEST
a02abb62
JB
17344dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17345{
17346 if (attr->form == DW_FORM_sdata)
17347 return DW_SND (attr);
17348 else if (attr->form == DW_FORM_udata
17349 || attr->form == DW_FORM_data1
17350 || attr->form == DW_FORM_data2
17351 || attr->form == DW_FORM_data4
17352 || attr->form == DW_FORM_data8)
17353 return DW_UNSND (attr);
17354 else
17355 {
3e43a32a
MS
17356 complaint (&symfile_complaints,
17357 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17358 dwarf_form_name (attr->form));
17359 return default_value;
17360 }
17361}
17362
348e048f
DE
17363/* Follow reference or signature attribute ATTR of SRC_DIE.
17364 On entry *REF_CU is the CU of SRC_DIE.
17365 On exit *REF_CU is the CU of the result. */
17366
17367static struct die_info *
17368follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17369 struct dwarf2_cu **ref_cu)
17370{
17371 struct die_info *die;
17372
17373 if (is_ref_attr (attr))
17374 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17375 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17376 die = follow_die_sig (src_die, attr, ref_cu);
17377 else
17378 {
17379 dump_die_for_error (src_die);
17380 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17381 (*ref_cu)->objfile->name);
17382 }
17383
17384 return die;
03dd20cc
DJ
17385}
17386
5c631832 17387/* Follow reference OFFSET.
673bfd45
DE
17388 On entry *REF_CU is the CU of the source die referencing OFFSET.
17389 On exit *REF_CU is the CU of the result.
17390 Returns NULL if OFFSET is invalid. */
f504f079 17391
f9aca02d 17392static struct die_info *
36586728
TT
17393follow_die_offset (sect_offset offset, int offset_in_dwz,
17394 struct dwarf2_cu **ref_cu)
c906108c 17395{
10b3939b 17396 struct die_info temp_die;
f2f0e013 17397 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17398
348e048f
DE
17399 gdb_assert (cu->per_cu != NULL);
17400
98bfdba5
PA
17401 target_cu = cu;
17402
3019eac3 17403 if (cu->per_cu->is_debug_types)
348e048f
DE
17404 {
17405 /* .debug_types CUs cannot reference anything outside their CU.
17406 If they need to, they have to reference a signatured type via
55f1336d 17407 DW_FORM_ref_sig8. */
348e048f 17408 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17409 return NULL;
348e048f 17410 }
36586728
TT
17411 else if (offset_in_dwz != cu->per_cu->is_dwz
17412 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17413 {
17414 struct dwarf2_per_cu_data *per_cu;
9a619af0 17415
36586728
TT
17416 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17417 cu->objfile);
03dd20cc
DJ
17418
17419 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17420 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17421 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17422
10b3939b
DJ
17423 target_cu = per_cu->cu;
17424 }
98bfdba5
PA
17425 else if (cu->dies == NULL)
17426 {
17427 /* We're loading full DIEs during partial symbol reading. */
17428 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17429 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17430 }
c906108c 17431
f2f0e013 17432 *ref_cu = target_cu;
51545339 17433 temp_die.offset = offset;
b64f50a1 17434 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17435}
10b3939b 17436
5c631832
JK
17437/* Follow reference attribute ATTR of SRC_DIE.
17438 On entry *REF_CU is the CU of SRC_DIE.
17439 On exit *REF_CU is the CU of the result. */
17440
17441static struct die_info *
17442follow_die_ref (struct die_info *src_die, struct attribute *attr,
17443 struct dwarf2_cu **ref_cu)
17444{
b64f50a1 17445 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17446 struct dwarf2_cu *cu = *ref_cu;
17447 struct die_info *die;
17448
36586728
TT
17449 die = follow_die_offset (offset,
17450 (attr->form == DW_FORM_GNU_ref_alt
17451 || cu->per_cu->is_dwz),
17452 ref_cu);
5c631832
JK
17453 if (!die)
17454 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17455 "at 0x%x [in module %s]"),
b64f50a1 17456 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17457
5c631832
JK
17458 return die;
17459}
17460
d83e736b
JK
17461/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17462 Returned value is intended for DW_OP_call*. Returned
17463 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17464
17465struct dwarf2_locexpr_baton
8b9737bf
TT
17466dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17467 struct dwarf2_per_cu_data *per_cu,
17468 CORE_ADDR (*get_frame_pc) (void *baton),
17469 void *baton)
5c631832 17470{
918dd910 17471 struct dwarf2_cu *cu;
5c631832
JK
17472 struct die_info *die;
17473 struct attribute *attr;
17474 struct dwarf2_locexpr_baton retval;
17475
8cf6f0b1
TT
17476 dw2_setup (per_cu->objfile);
17477
918dd910
JK
17478 if (per_cu->cu == NULL)
17479 load_cu (per_cu);
17480 cu = per_cu->cu;
17481
36586728 17482 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17483 if (!die)
17484 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17485 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17486
17487 attr = dwarf2_attr (die, DW_AT_location, cu);
17488 if (!attr)
17489 {
e103e986
JK
17490 /* DWARF: "If there is no such attribute, then there is no effect.".
17491 DATA is ignored if SIZE is 0. */
5c631832 17492
e103e986 17493 retval.data = NULL;
5c631832
JK
17494 retval.size = 0;
17495 }
8cf6f0b1
TT
17496 else if (attr_form_is_section_offset (attr))
17497 {
17498 struct dwarf2_loclist_baton loclist_baton;
17499 CORE_ADDR pc = (*get_frame_pc) (baton);
17500 size_t size;
17501
17502 fill_in_loclist_baton (cu, &loclist_baton, attr);
17503
17504 retval.data = dwarf2_find_location_expression (&loclist_baton,
17505 &size, pc);
17506 retval.size = size;
17507 }
5c631832
JK
17508 else
17509 {
17510 if (!attr_form_is_block (attr))
17511 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17512 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17513 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17514
17515 retval.data = DW_BLOCK (attr)->data;
17516 retval.size = DW_BLOCK (attr)->size;
17517 }
17518 retval.per_cu = cu->per_cu;
918dd910 17519
918dd910
JK
17520 age_cached_comp_units ();
17521
5c631832 17522 return retval;
348e048f
DE
17523}
17524
8b9737bf
TT
17525/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17526 offset. */
17527
17528struct dwarf2_locexpr_baton
17529dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17530 struct dwarf2_per_cu_data *per_cu,
17531 CORE_ADDR (*get_frame_pc) (void *baton),
17532 void *baton)
17533{
17534 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17535
17536 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17537}
17538
8a9b8146
TT
17539/* Return the type of the DIE at DIE_OFFSET in the CU named by
17540 PER_CU. */
17541
17542struct type *
b64f50a1 17543dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17544 struct dwarf2_per_cu_data *per_cu)
17545{
b64f50a1
JK
17546 sect_offset die_offset_sect;
17547
8a9b8146 17548 dw2_setup (per_cu->objfile);
b64f50a1
JK
17549
17550 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17551 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17552}
17553
348e048f
DE
17554/* Follow the signature attribute ATTR in SRC_DIE.
17555 On entry *REF_CU is the CU of SRC_DIE.
17556 On exit *REF_CU is the CU of the result. */
17557
17558static struct die_info *
17559follow_die_sig (struct die_info *src_die, struct attribute *attr,
17560 struct dwarf2_cu **ref_cu)
17561{
17562 struct objfile *objfile = (*ref_cu)->objfile;
17563 struct die_info temp_die;
17564 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17565 struct dwarf2_cu *sig_cu;
17566 struct die_info *die;
17567
17568 /* sig_type will be NULL if the signatured type is missing from
17569 the debug info. */
17570 if (sig_type == NULL)
17571 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17572 "at 0x%x [in module %s]"),
b64f50a1 17573 src_die->offset.sect_off, objfile->name);
348e048f
DE
17574
17575 /* If necessary, add it to the queue and load its DIEs. */
17576
95554aad 17577 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17578 read_signatured_type (sig_type);
348e048f
DE
17579
17580 gdb_assert (sig_type->per_cu.cu != NULL);
17581
17582 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17583 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17584 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17585 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17586 temp_die.offset.sect_off);
348e048f
DE
17587 if (die)
17588 {
17589 *ref_cu = sig_cu;
17590 return die;
17591 }
17592
3e43a32a
MS
17593 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17594 "from DIE at 0x%x [in module %s]"),
b64f50a1 17595 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17596}
17597
17598/* Given an offset of a signatured type, return its signatured_type. */
17599
17600static struct signatured_type *
8b70b953
TT
17601lookup_signatured_type_at_offset (struct objfile *objfile,
17602 struct dwarf2_section_info *section,
b64f50a1 17603 sect_offset offset)
348e048f 17604{
b64f50a1 17605 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17606 unsigned int length, initial_length_size;
17607 unsigned int sig_offset;
52dc124a 17608 struct signatured_type find_entry, *sig_type;
348e048f
DE
17609
17610 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17611 sig_offset = (initial_length_size
17612 + 2 /*version*/
17613 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17614 + 1 /*address_size*/);
17615 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17616 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17617
17618 /* This is only used to lookup previously recorded types.
17619 If we didn't find it, it's our bug. */
52dc124a
DE
17620 gdb_assert (sig_type != NULL);
17621 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17622
52dc124a 17623 return sig_type;
348e048f
DE
17624}
17625
e5fe5e75 17626/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17627
17628static void
e5fe5e75 17629load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17630{
52dc124a 17631 struct signatured_type *sig_type;
348e048f 17632
f4dc4d17
DE
17633 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17634 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17635
6721b2ec
DE
17636 /* We have the per_cu, but we need the signatured_type.
17637 Fortunately this is an easy translation. */
17638 gdb_assert (per_cu->is_debug_types);
17639 sig_type = (struct signatured_type *) per_cu;
348e048f 17640
6721b2ec 17641 gdb_assert (per_cu->cu == NULL);
348e048f 17642
52dc124a 17643 read_signatured_type (sig_type);
348e048f 17644
6721b2ec 17645 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17646}
17647
dee91e82
DE
17648/* die_reader_func for read_signatured_type.
17649 This is identical to load_full_comp_unit_reader,
17650 but is kept separate for now. */
348e048f
DE
17651
17652static void
dee91e82
DE
17653read_signatured_type_reader (const struct die_reader_specs *reader,
17654 gdb_byte *info_ptr,
17655 struct die_info *comp_unit_die,
17656 int has_children,
17657 void *data)
348e048f 17658{
dee91e82 17659 struct dwarf2_cu *cu = reader->cu;
348e048f 17660
dee91e82
DE
17661 gdb_assert (cu->die_hash == NULL);
17662 cu->die_hash =
17663 htab_create_alloc_ex (cu->header.length / 12,
17664 die_hash,
17665 die_eq,
17666 NULL,
17667 &cu->comp_unit_obstack,
17668 hashtab_obstack_allocate,
17669 dummy_obstack_deallocate);
348e048f 17670
dee91e82
DE
17671 if (has_children)
17672 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17673 &info_ptr, comp_unit_die);
17674 cu->dies = comp_unit_die;
17675 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17676
17677 /* We try not to read any attributes in this function, because not
9cdd5dbd 17678 all CUs needed for references have been loaded yet, and symbol
348e048f 17679 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17680 or we won't be able to build types correctly.
17681 Similarly, if we do not read the producer, we can not apply
17682 producer-specific interpretation. */
95554aad 17683 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17684}
348e048f 17685
3019eac3
DE
17686/* Read in a signatured type and build its CU and DIEs.
17687 If the type is a stub for the real type in a DWO file,
17688 read in the real type from the DWO file as well. */
dee91e82
DE
17689
17690static void
17691read_signatured_type (struct signatured_type *sig_type)
17692{
17693 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17694
3019eac3 17695 gdb_assert (per_cu->is_debug_types);
dee91e82 17696 gdb_assert (per_cu->cu == NULL);
348e048f 17697
f4dc4d17
DE
17698 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17699 read_signatured_type_reader, NULL);
c906108c
SS
17700}
17701
c906108c
SS
17702/* Decode simple location descriptions.
17703 Given a pointer to a dwarf block that defines a location, compute
17704 the location and return the value.
17705
4cecd739
DJ
17706 NOTE drow/2003-11-18: This function is called in two situations
17707 now: for the address of static or global variables (partial symbols
17708 only) and for offsets into structures which are expected to be
17709 (more or less) constant. The partial symbol case should go away,
17710 and only the constant case should remain. That will let this
17711 function complain more accurately. A few special modes are allowed
17712 without complaint for global variables (for instance, global
17713 register values and thread-local values).
c906108c
SS
17714
17715 A location description containing no operations indicates that the
4cecd739 17716 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17717 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17718 callers will only want a very basic result and this can become a
21ae7a4d
JK
17719 complaint.
17720
17721 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17722
17723static CORE_ADDR
e7c27a73 17724decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17725{
e7c27a73 17726 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17727 size_t i;
17728 size_t size = blk->size;
21ae7a4d
JK
17729 gdb_byte *data = blk->data;
17730 CORE_ADDR stack[64];
17731 int stacki;
17732 unsigned int bytes_read, unsnd;
17733 gdb_byte op;
c906108c 17734
21ae7a4d
JK
17735 i = 0;
17736 stacki = 0;
17737 stack[stacki] = 0;
17738 stack[++stacki] = 0;
17739
17740 while (i < size)
17741 {
17742 op = data[i++];
17743 switch (op)
17744 {
17745 case DW_OP_lit0:
17746 case DW_OP_lit1:
17747 case DW_OP_lit2:
17748 case DW_OP_lit3:
17749 case DW_OP_lit4:
17750 case DW_OP_lit5:
17751 case DW_OP_lit6:
17752 case DW_OP_lit7:
17753 case DW_OP_lit8:
17754 case DW_OP_lit9:
17755 case DW_OP_lit10:
17756 case DW_OP_lit11:
17757 case DW_OP_lit12:
17758 case DW_OP_lit13:
17759 case DW_OP_lit14:
17760 case DW_OP_lit15:
17761 case DW_OP_lit16:
17762 case DW_OP_lit17:
17763 case DW_OP_lit18:
17764 case DW_OP_lit19:
17765 case DW_OP_lit20:
17766 case DW_OP_lit21:
17767 case DW_OP_lit22:
17768 case DW_OP_lit23:
17769 case DW_OP_lit24:
17770 case DW_OP_lit25:
17771 case DW_OP_lit26:
17772 case DW_OP_lit27:
17773 case DW_OP_lit28:
17774 case DW_OP_lit29:
17775 case DW_OP_lit30:
17776 case DW_OP_lit31:
17777 stack[++stacki] = op - DW_OP_lit0;
17778 break;
f1bea926 17779
21ae7a4d
JK
17780 case DW_OP_reg0:
17781 case DW_OP_reg1:
17782 case DW_OP_reg2:
17783 case DW_OP_reg3:
17784 case DW_OP_reg4:
17785 case DW_OP_reg5:
17786 case DW_OP_reg6:
17787 case DW_OP_reg7:
17788 case DW_OP_reg8:
17789 case DW_OP_reg9:
17790 case DW_OP_reg10:
17791 case DW_OP_reg11:
17792 case DW_OP_reg12:
17793 case DW_OP_reg13:
17794 case DW_OP_reg14:
17795 case DW_OP_reg15:
17796 case DW_OP_reg16:
17797 case DW_OP_reg17:
17798 case DW_OP_reg18:
17799 case DW_OP_reg19:
17800 case DW_OP_reg20:
17801 case DW_OP_reg21:
17802 case DW_OP_reg22:
17803 case DW_OP_reg23:
17804 case DW_OP_reg24:
17805 case DW_OP_reg25:
17806 case DW_OP_reg26:
17807 case DW_OP_reg27:
17808 case DW_OP_reg28:
17809 case DW_OP_reg29:
17810 case DW_OP_reg30:
17811 case DW_OP_reg31:
17812 stack[++stacki] = op - DW_OP_reg0;
17813 if (i < size)
17814 dwarf2_complex_location_expr_complaint ();
17815 break;
c906108c 17816
21ae7a4d
JK
17817 case DW_OP_regx:
17818 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17819 i += bytes_read;
17820 stack[++stacki] = unsnd;
17821 if (i < size)
17822 dwarf2_complex_location_expr_complaint ();
17823 break;
c906108c 17824
21ae7a4d
JK
17825 case DW_OP_addr:
17826 stack[++stacki] = read_address (objfile->obfd, &data[i],
17827 cu, &bytes_read);
17828 i += bytes_read;
17829 break;
d53d4ac5 17830
21ae7a4d
JK
17831 case DW_OP_const1u:
17832 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17833 i += 1;
17834 break;
17835
17836 case DW_OP_const1s:
17837 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17838 i += 1;
17839 break;
17840
17841 case DW_OP_const2u:
17842 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17843 i += 2;
17844 break;
17845
17846 case DW_OP_const2s:
17847 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17848 i += 2;
17849 break;
d53d4ac5 17850
21ae7a4d
JK
17851 case DW_OP_const4u:
17852 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17853 i += 4;
17854 break;
17855
17856 case DW_OP_const4s:
17857 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17858 i += 4;
17859 break;
17860
585861ea
JK
17861 case DW_OP_const8u:
17862 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17863 i += 8;
17864 break;
17865
21ae7a4d
JK
17866 case DW_OP_constu:
17867 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17868 &bytes_read);
17869 i += bytes_read;
17870 break;
17871
17872 case DW_OP_consts:
17873 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17874 i += bytes_read;
17875 break;
17876
17877 case DW_OP_dup:
17878 stack[stacki + 1] = stack[stacki];
17879 stacki++;
17880 break;
17881
17882 case DW_OP_plus:
17883 stack[stacki - 1] += stack[stacki];
17884 stacki--;
17885 break;
17886
17887 case DW_OP_plus_uconst:
17888 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17889 &bytes_read);
17890 i += bytes_read;
17891 break;
17892
17893 case DW_OP_minus:
17894 stack[stacki - 1] -= stack[stacki];
17895 stacki--;
17896 break;
17897
17898 case DW_OP_deref:
17899 /* If we're not the last op, then we definitely can't encode
17900 this using GDB's address_class enum. This is valid for partial
17901 global symbols, although the variable's address will be bogus
17902 in the psymtab. */
17903 if (i < size)
17904 dwarf2_complex_location_expr_complaint ();
17905 break;
17906
17907 case DW_OP_GNU_push_tls_address:
17908 /* The top of the stack has the offset from the beginning
17909 of the thread control block at which the variable is located. */
17910 /* Nothing should follow this operator, so the top of stack would
17911 be returned. */
17912 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17913 address will be bogus in the psymtab. Make it always at least
17914 non-zero to not look as a variable garbage collected by linker
17915 which have DW_OP_addr 0. */
21ae7a4d
JK
17916 if (i < size)
17917 dwarf2_complex_location_expr_complaint ();
585861ea 17918 stack[stacki]++;
21ae7a4d
JK
17919 break;
17920
17921 case DW_OP_GNU_uninit:
17922 break;
17923
3019eac3 17924 case DW_OP_GNU_addr_index:
49f6c839 17925 case DW_OP_GNU_const_index:
3019eac3
DE
17926 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17927 &bytes_read);
17928 i += bytes_read;
17929 break;
17930
21ae7a4d
JK
17931 default:
17932 {
f39c6ffd 17933 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17934
17935 if (name)
17936 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17937 name);
17938 else
17939 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17940 op);
17941 }
17942
17943 return (stack[stacki]);
d53d4ac5 17944 }
3c6e0cb3 17945
21ae7a4d
JK
17946 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17947 outside of the allocated space. Also enforce minimum>0. */
17948 if (stacki >= ARRAY_SIZE (stack) - 1)
17949 {
17950 complaint (&symfile_complaints,
17951 _("location description stack overflow"));
17952 return 0;
17953 }
17954
17955 if (stacki <= 0)
17956 {
17957 complaint (&symfile_complaints,
17958 _("location description stack underflow"));
17959 return 0;
17960 }
17961 }
17962 return (stack[stacki]);
c906108c
SS
17963}
17964
17965/* memory allocation interface */
17966
c906108c 17967static struct dwarf_block *
7b5a2f43 17968dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17969{
17970 struct dwarf_block *blk;
17971
17972 blk = (struct dwarf_block *)
7b5a2f43 17973 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17974 return (blk);
17975}
17976
c906108c 17977static struct die_info *
b60c80d6 17978dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17979{
17980 struct die_info *die;
b60c80d6
DJ
17981 size_t size = sizeof (struct die_info);
17982
17983 if (num_attrs > 1)
17984 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17985
b60c80d6 17986 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17987 memset (die, 0, sizeof (struct die_info));
17988 return (die);
17989}
2e276125
JB
17990
17991\f
17992/* Macro support. */
17993
2e276125
JB
17994/* Return the full name of file number I in *LH's file name table.
17995 Use COMP_DIR as the name of the current directory of the
17996 compilation. The result is allocated using xmalloc; the caller is
17997 responsible for freeing it. */
17998static char *
17999file_full_name (int file, struct line_header *lh, const char *comp_dir)
18000{
6a83a1e6
EZ
18001 /* Is the file number a valid index into the line header's file name
18002 table? Remember that file numbers start with one, not zero. */
18003 if (1 <= file && file <= lh->num_file_names)
18004 {
18005 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18006
6a83a1e6
EZ
18007 if (IS_ABSOLUTE_PATH (fe->name))
18008 return xstrdup (fe->name);
18009 else
18010 {
18011 const char *dir;
18012 int dir_len;
18013 char *full_name;
18014
18015 if (fe->dir_index)
18016 dir = lh->include_dirs[fe->dir_index - 1];
18017 else
18018 dir = comp_dir;
18019
18020 if (dir)
18021 {
18022 dir_len = strlen (dir);
18023 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
18024 strcpy (full_name, dir);
18025 full_name[dir_len] = '/';
18026 strcpy (full_name + dir_len + 1, fe->name);
18027 return full_name;
18028 }
18029 else
18030 return xstrdup (fe->name);
18031 }
18032 }
2e276125
JB
18033 else
18034 {
6a83a1e6
EZ
18035 /* The compiler produced a bogus file number. We can at least
18036 record the macro definitions made in the file, even if we
18037 won't be able to find the file by name. */
18038 char fake_name[80];
9a619af0 18039
8c042590
PM
18040 xsnprintf (fake_name, sizeof (fake_name),
18041 "<bad macro file number %d>", file);
2e276125 18042
6e70227d 18043 complaint (&symfile_complaints,
6a83a1e6
EZ
18044 _("bad file number in macro information (%d)"),
18045 file);
2e276125 18046
6a83a1e6 18047 return xstrdup (fake_name);
2e276125
JB
18048 }
18049}
18050
18051
18052static struct macro_source_file *
18053macro_start_file (int file, int line,
18054 struct macro_source_file *current_file,
18055 const char *comp_dir,
18056 struct line_header *lh, struct objfile *objfile)
18057{
18058 /* The full name of this source file. */
18059 char *full_name = file_full_name (file, lh, comp_dir);
18060
18061 /* We don't create a macro table for this compilation unit
18062 at all until we actually get a filename. */
18063 if (! pending_macros)
6532ff36
TT
18064 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18065 objfile->per_bfd->macro_cache);
2e276125
JB
18066
18067 if (! current_file)
abc9d0dc
TT
18068 {
18069 /* If we have no current file, then this must be the start_file
18070 directive for the compilation unit's main source file. */
18071 current_file = macro_set_main (pending_macros, full_name);
18072 macro_define_special (pending_macros);
18073 }
2e276125
JB
18074 else
18075 current_file = macro_include (current_file, line, full_name);
18076
18077 xfree (full_name);
6e70227d 18078
2e276125
JB
18079 return current_file;
18080}
18081
18082
18083/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18084 followed by a null byte. */
18085static char *
18086copy_string (const char *buf, int len)
18087{
18088 char *s = xmalloc (len + 1);
9a619af0 18089
2e276125
JB
18090 memcpy (s, buf, len);
18091 s[len] = '\0';
2e276125
JB
18092 return s;
18093}
18094
18095
18096static const char *
18097consume_improper_spaces (const char *p, const char *body)
18098{
18099 if (*p == ' ')
18100 {
4d3c2250 18101 complaint (&symfile_complaints,
3e43a32a
MS
18102 _("macro definition contains spaces "
18103 "in formal argument list:\n`%s'"),
4d3c2250 18104 body);
2e276125
JB
18105
18106 while (*p == ' ')
18107 p++;
18108 }
18109
18110 return p;
18111}
18112
18113
18114static void
18115parse_macro_definition (struct macro_source_file *file, int line,
18116 const char *body)
18117{
18118 const char *p;
18119
18120 /* The body string takes one of two forms. For object-like macro
18121 definitions, it should be:
18122
18123 <macro name> " " <definition>
18124
18125 For function-like macro definitions, it should be:
18126
18127 <macro name> "() " <definition>
18128 or
18129 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18130
18131 Spaces may appear only where explicitly indicated, and in the
18132 <definition>.
18133
18134 The Dwarf 2 spec says that an object-like macro's name is always
18135 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18136 the space when the macro's definition is the empty string.
2e276125
JB
18137
18138 The Dwarf 2 spec says that there should be no spaces between the
18139 formal arguments in a function-like macro's formal argument list,
18140 but versions of GCC around March 2002 include spaces after the
18141 commas. */
18142
18143
18144 /* Find the extent of the macro name. The macro name is terminated
18145 by either a space or null character (for an object-like macro) or
18146 an opening paren (for a function-like macro). */
18147 for (p = body; *p; p++)
18148 if (*p == ' ' || *p == '(')
18149 break;
18150
18151 if (*p == ' ' || *p == '\0')
18152 {
18153 /* It's an object-like macro. */
18154 int name_len = p - body;
18155 char *name = copy_string (body, name_len);
18156 const char *replacement;
18157
18158 if (*p == ' ')
18159 replacement = body + name_len + 1;
18160 else
18161 {
4d3c2250 18162 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18163 replacement = body + name_len;
18164 }
6e70227d 18165
2e276125
JB
18166 macro_define_object (file, line, name, replacement);
18167
18168 xfree (name);
18169 }
18170 else if (*p == '(')
18171 {
18172 /* It's a function-like macro. */
18173 char *name = copy_string (body, p - body);
18174 int argc = 0;
18175 int argv_size = 1;
18176 char **argv = xmalloc (argv_size * sizeof (*argv));
18177
18178 p++;
18179
18180 p = consume_improper_spaces (p, body);
18181
18182 /* Parse the formal argument list. */
18183 while (*p && *p != ')')
18184 {
18185 /* Find the extent of the current argument name. */
18186 const char *arg_start = p;
18187
18188 while (*p && *p != ',' && *p != ')' && *p != ' ')
18189 p++;
18190
18191 if (! *p || p == arg_start)
4d3c2250 18192 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18193 else
18194 {
18195 /* Make sure argv has room for the new argument. */
18196 if (argc >= argv_size)
18197 {
18198 argv_size *= 2;
18199 argv = xrealloc (argv, argv_size * sizeof (*argv));
18200 }
18201
18202 argv[argc++] = copy_string (arg_start, p - arg_start);
18203 }
18204
18205 p = consume_improper_spaces (p, body);
18206
18207 /* Consume the comma, if present. */
18208 if (*p == ',')
18209 {
18210 p++;
18211
18212 p = consume_improper_spaces (p, body);
18213 }
18214 }
18215
18216 if (*p == ')')
18217 {
18218 p++;
18219
18220 if (*p == ' ')
18221 /* Perfectly formed definition, no complaints. */
18222 macro_define_function (file, line, name,
6e70227d 18223 argc, (const char **) argv,
2e276125
JB
18224 p + 1);
18225 else if (*p == '\0')
18226 {
18227 /* Complain, but do define it. */
4d3c2250 18228 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18229 macro_define_function (file, line, name,
6e70227d 18230 argc, (const char **) argv,
2e276125
JB
18231 p);
18232 }
18233 else
18234 /* Just complain. */
4d3c2250 18235 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18236 }
18237 else
18238 /* Just complain. */
4d3c2250 18239 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18240
18241 xfree (name);
18242 {
18243 int i;
18244
18245 for (i = 0; i < argc; i++)
18246 xfree (argv[i]);
18247 }
18248 xfree (argv);
18249 }
18250 else
4d3c2250 18251 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18252}
18253
cf2c3c16
TT
18254/* Skip some bytes from BYTES according to the form given in FORM.
18255 Returns the new pointer. */
2e276125 18256
cf2c3c16 18257static gdb_byte *
f664829e 18258skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18259 enum dwarf_form form,
18260 unsigned int offset_size,
18261 struct dwarf2_section_info *section)
2e276125 18262{
cf2c3c16 18263 unsigned int bytes_read;
2e276125 18264
cf2c3c16 18265 switch (form)
2e276125 18266 {
cf2c3c16
TT
18267 case DW_FORM_data1:
18268 case DW_FORM_flag:
18269 ++bytes;
18270 break;
18271
18272 case DW_FORM_data2:
18273 bytes += 2;
18274 break;
18275
18276 case DW_FORM_data4:
18277 bytes += 4;
18278 break;
18279
18280 case DW_FORM_data8:
18281 bytes += 8;
18282 break;
18283
18284 case DW_FORM_string:
18285 read_direct_string (abfd, bytes, &bytes_read);
18286 bytes += bytes_read;
18287 break;
18288
18289 case DW_FORM_sec_offset:
18290 case DW_FORM_strp:
36586728 18291 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18292 bytes += offset_size;
18293 break;
18294
18295 case DW_FORM_block:
18296 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18297 bytes += bytes_read;
18298 break;
18299
18300 case DW_FORM_block1:
18301 bytes += 1 + read_1_byte (abfd, bytes);
18302 break;
18303 case DW_FORM_block2:
18304 bytes += 2 + read_2_bytes (abfd, bytes);
18305 break;
18306 case DW_FORM_block4:
18307 bytes += 4 + read_4_bytes (abfd, bytes);
18308 break;
18309
18310 case DW_FORM_sdata:
18311 case DW_FORM_udata:
3019eac3
DE
18312 case DW_FORM_GNU_addr_index:
18313 case DW_FORM_GNU_str_index:
f664829e
DE
18314 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18315 if (bytes == NULL)
18316 {
18317 dwarf2_section_buffer_overflow_complaint (section);
18318 return NULL;
18319 }
cf2c3c16
TT
18320 break;
18321
18322 default:
18323 {
18324 complain:
18325 complaint (&symfile_complaints,
18326 _("invalid form 0x%x in `%s'"),
18327 form,
18328 section->asection->name);
18329 return NULL;
18330 }
2e276125
JB
18331 }
18332
cf2c3c16
TT
18333 return bytes;
18334}
757a13d0 18335
cf2c3c16
TT
18336/* A helper for dwarf_decode_macros that handles skipping an unknown
18337 opcode. Returns an updated pointer to the macro data buffer; or,
18338 on error, issues a complaint and returns NULL. */
757a13d0 18339
cf2c3c16
TT
18340static gdb_byte *
18341skip_unknown_opcode (unsigned int opcode,
18342 gdb_byte **opcode_definitions,
f664829e 18343 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18344 bfd *abfd,
18345 unsigned int offset_size,
18346 struct dwarf2_section_info *section)
18347{
18348 unsigned int bytes_read, i;
18349 unsigned long arg;
18350 gdb_byte *defn;
2e276125 18351
cf2c3c16 18352 if (opcode_definitions[opcode] == NULL)
2e276125 18353 {
cf2c3c16
TT
18354 complaint (&symfile_complaints,
18355 _("unrecognized DW_MACFINO opcode 0x%x"),
18356 opcode);
18357 return NULL;
18358 }
2e276125 18359
cf2c3c16
TT
18360 defn = opcode_definitions[opcode];
18361 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18362 defn += bytes_read;
2e276125 18363
cf2c3c16
TT
18364 for (i = 0; i < arg; ++i)
18365 {
f664829e
DE
18366 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18367 section);
cf2c3c16
TT
18368 if (mac_ptr == NULL)
18369 {
18370 /* skip_form_bytes already issued the complaint. */
18371 return NULL;
18372 }
18373 }
757a13d0 18374
cf2c3c16
TT
18375 return mac_ptr;
18376}
757a13d0 18377
cf2c3c16
TT
18378/* A helper function which parses the header of a macro section.
18379 If the macro section is the extended (for now called "GNU") type,
18380 then this updates *OFFSET_SIZE. Returns a pointer to just after
18381 the header, or issues a complaint and returns NULL on error. */
757a13d0 18382
cf2c3c16
TT
18383static gdb_byte *
18384dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18385 bfd *abfd,
18386 gdb_byte *mac_ptr,
18387 unsigned int *offset_size,
18388 int section_is_gnu)
18389{
18390 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18391
cf2c3c16
TT
18392 if (section_is_gnu)
18393 {
18394 unsigned int version, flags;
757a13d0 18395
cf2c3c16
TT
18396 version = read_2_bytes (abfd, mac_ptr);
18397 if (version != 4)
18398 {
18399 complaint (&symfile_complaints,
18400 _("unrecognized version `%d' in .debug_macro section"),
18401 version);
18402 return NULL;
18403 }
18404 mac_ptr += 2;
757a13d0 18405
cf2c3c16
TT
18406 flags = read_1_byte (abfd, mac_ptr);
18407 ++mac_ptr;
18408 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18409
cf2c3c16
TT
18410 if ((flags & 2) != 0)
18411 /* We don't need the line table offset. */
18412 mac_ptr += *offset_size;
757a13d0 18413
cf2c3c16
TT
18414 /* Vendor opcode descriptions. */
18415 if ((flags & 4) != 0)
18416 {
18417 unsigned int i, count;
757a13d0 18418
cf2c3c16
TT
18419 count = read_1_byte (abfd, mac_ptr);
18420 ++mac_ptr;
18421 for (i = 0; i < count; ++i)
18422 {
18423 unsigned int opcode, bytes_read;
18424 unsigned long arg;
18425
18426 opcode = read_1_byte (abfd, mac_ptr);
18427 ++mac_ptr;
18428 opcode_definitions[opcode] = mac_ptr;
18429 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18430 mac_ptr += bytes_read;
18431 mac_ptr += arg;
18432 }
757a13d0 18433 }
cf2c3c16 18434 }
757a13d0 18435
cf2c3c16
TT
18436 return mac_ptr;
18437}
757a13d0 18438
cf2c3c16 18439/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18440 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18441
18442static void
18443dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18444 struct macro_source_file *current_file,
15d034d0 18445 struct line_header *lh, const char *comp_dir,
cf2c3c16 18446 struct dwarf2_section_info *section,
36586728 18447 int section_is_gnu, int section_is_dwz,
cf2c3c16 18448 unsigned int offset_size,
8fc3fc34
TT
18449 struct objfile *objfile,
18450 htab_t include_hash)
cf2c3c16
TT
18451{
18452 enum dwarf_macro_record_type macinfo_type;
18453 int at_commandline;
18454 gdb_byte *opcode_definitions[256];
757a13d0 18455
cf2c3c16
TT
18456 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18457 &offset_size, section_is_gnu);
18458 if (mac_ptr == NULL)
18459 {
18460 /* We already issued a complaint. */
18461 return;
18462 }
757a13d0
JK
18463
18464 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18465 GDB is still reading the definitions from command line. First
18466 DW_MACINFO_start_file will need to be ignored as it was already executed
18467 to create CURRENT_FILE for the main source holding also the command line
18468 definitions. On first met DW_MACINFO_start_file this flag is reset to
18469 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18470
18471 at_commandline = 1;
18472
18473 do
18474 {
18475 /* Do we at least have room for a macinfo type byte? */
18476 if (mac_ptr >= mac_end)
18477 {
f664829e 18478 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18479 break;
18480 }
18481
18482 macinfo_type = read_1_byte (abfd, mac_ptr);
18483 mac_ptr++;
18484
cf2c3c16
TT
18485 /* Note that we rely on the fact that the corresponding GNU and
18486 DWARF constants are the same. */
757a13d0
JK
18487 switch (macinfo_type)
18488 {
18489 /* A zero macinfo type indicates the end of the macro
18490 information. */
18491 case 0:
18492 break;
2e276125 18493
cf2c3c16
TT
18494 case DW_MACRO_GNU_define:
18495 case DW_MACRO_GNU_undef:
18496 case DW_MACRO_GNU_define_indirect:
18497 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18498 case DW_MACRO_GNU_define_indirect_alt:
18499 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18500 {
891d2f0b 18501 unsigned int bytes_read;
2e276125
JB
18502 int line;
18503 char *body;
cf2c3c16 18504 int is_define;
2e276125 18505
cf2c3c16
TT
18506 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18507 mac_ptr += bytes_read;
18508
18509 if (macinfo_type == DW_MACRO_GNU_define
18510 || macinfo_type == DW_MACRO_GNU_undef)
18511 {
18512 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18513 mac_ptr += bytes_read;
18514 }
18515 else
18516 {
18517 LONGEST str_offset;
18518
18519 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18520 mac_ptr += offset_size;
2e276125 18521
36586728 18522 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18523 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18524 || section_is_dwz)
36586728
TT
18525 {
18526 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18527
18528 body = read_indirect_string_from_dwz (dwz, str_offset);
18529 }
18530 else
18531 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18532 }
18533
18534 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18535 || macinfo_type == DW_MACRO_GNU_define_indirect
18536 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18537 if (! current_file)
757a13d0
JK
18538 {
18539 /* DWARF violation as no main source is present. */
18540 complaint (&symfile_complaints,
18541 _("debug info with no main source gives macro %s "
18542 "on line %d: %s"),
cf2c3c16
TT
18543 is_define ? _("definition") : _("undefinition"),
18544 line, body);
757a13d0
JK
18545 break;
18546 }
3e43a32a
MS
18547 if ((line == 0 && !at_commandline)
18548 || (line != 0 && at_commandline))
4d3c2250 18549 complaint (&symfile_complaints,
757a13d0
JK
18550 _("debug info gives %s macro %s with %s line %d: %s"),
18551 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18552 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18553 line == 0 ? _("zero") : _("non-zero"), line, body);
18554
cf2c3c16 18555 if (is_define)
757a13d0 18556 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18557 else
18558 {
18559 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18560 || macinfo_type == DW_MACRO_GNU_undef_indirect
18561 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18562 macro_undef (current_file, line, body);
18563 }
2e276125
JB
18564 }
18565 break;
18566
cf2c3c16 18567 case DW_MACRO_GNU_start_file:
2e276125 18568 {
891d2f0b 18569 unsigned int bytes_read;
2e276125
JB
18570 int line, file;
18571
18572 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18573 mac_ptr += bytes_read;
18574 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18575 mac_ptr += bytes_read;
18576
3e43a32a
MS
18577 if ((line == 0 && !at_commandline)
18578 || (line != 0 && at_commandline))
757a13d0
JK
18579 complaint (&symfile_complaints,
18580 _("debug info gives source %d included "
18581 "from %s at %s line %d"),
18582 file, at_commandline ? _("command-line") : _("file"),
18583 line == 0 ? _("zero") : _("non-zero"), line);
18584
18585 if (at_commandline)
18586 {
cf2c3c16
TT
18587 /* This DW_MACRO_GNU_start_file was executed in the
18588 pass one. */
757a13d0
JK
18589 at_commandline = 0;
18590 }
18591 else
18592 current_file = macro_start_file (file, line,
18593 current_file, comp_dir,
cf2c3c16 18594 lh, objfile);
2e276125
JB
18595 }
18596 break;
18597
cf2c3c16 18598 case DW_MACRO_GNU_end_file:
2e276125 18599 if (! current_file)
4d3c2250 18600 complaint (&symfile_complaints,
3e43a32a
MS
18601 _("macro debug info has an unmatched "
18602 "`close_file' directive"));
2e276125
JB
18603 else
18604 {
18605 current_file = current_file->included_by;
18606 if (! current_file)
18607 {
cf2c3c16 18608 enum dwarf_macro_record_type next_type;
2e276125
JB
18609
18610 /* GCC circa March 2002 doesn't produce the zero
18611 type byte marking the end of the compilation
18612 unit. Complain if it's not there, but exit no
18613 matter what. */
18614
18615 /* Do we at least have room for a macinfo type byte? */
18616 if (mac_ptr >= mac_end)
18617 {
f664829e 18618 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18619 return;
18620 }
18621
18622 /* We don't increment mac_ptr here, so this is just
18623 a look-ahead. */
18624 next_type = read_1_byte (abfd, mac_ptr);
18625 if (next_type != 0)
4d3c2250 18626 complaint (&symfile_complaints,
3e43a32a
MS
18627 _("no terminating 0-type entry for "
18628 "macros in `.debug_macinfo' section"));
2e276125
JB
18629
18630 return;
18631 }
18632 }
18633 break;
18634
cf2c3c16 18635 case DW_MACRO_GNU_transparent_include:
36586728 18636 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18637 {
18638 LONGEST offset;
8fc3fc34 18639 void **slot;
a036ba48
TT
18640 bfd *include_bfd = abfd;
18641 struct dwarf2_section_info *include_section = section;
18642 struct dwarf2_section_info alt_section;
18643 gdb_byte *include_mac_end = mac_end;
18644 int is_dwz = section_is_dwz;
18645 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18646
18647 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18648 mac_ptr += offset_size;
18649
a036ba48
TT
18650 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18651 {
18652 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18653
18654 dwarf2_read_section (dwarf2_per_objfile->objfile,
18655 &dwz->macro);
18656
18657 include_bfd = dwz->macro.asection->owner;
18658 include_section = &dwz->macro;
18659 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18660 is_dwz = 1;
18661 }
18662
18663 new_mac_ptr = include_section->buffer + offset;
18664 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18665
8fc3fc34
TT
18666 if (*slot != NULL)
18667 {
18668 /* This has actually happened; see
18669 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18670 complaint (&symfile_complaints,
18671 _("recursive DW_MACRO_GNU_transparent_include in "
18672 ".debug_macro section"));
18673 }
18674 else
18675 {
a036ba48 18676 *slot = new_mac_ptr;
36586728 18677
a036ba48 18678 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18679 include_mac_end, current_file,
8fc3fc34 18680 lh, comp_dir,
36586728 18681 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18682 offset_size, objfile, include_hash);
18683
a036ba48 18684 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18685 }
cf2c3c16
TT
18686 }
18687 break;
18688
2e276125 18689 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18690 if (!section_is_gnu)
18691 {
18692 unsigned int bytes_read;
18693 int constant;
2e276125 18694
cf2c3c16
TT
18695 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18696 mac_ptr += bytes_read;
18697 read_direct_string (abfd, mac_ptr, &bytes_read);
18698 mac_ptr += bytes_read;
2e276125 18699
cf2c3c16
TT
18700 /* We don't recognize any vendor extensions. */
18701 break;
18702 }
18703 /* FALLTHROUGH */
18704
18705 default:
18706 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18707 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18708 section);
18709 if (mac_ptr == NULL)
18710 return;
18711 break;
2e276125 18712 }
757a13d0 18713 } while (macinfo_type != 0);
2e276125 18714}
8e19ed76 18715
cf2c3c16 18716static void
09262596 18717dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18718 const char *comp_dir, int section_is_gnu)
cf2c3c16 18719{
bb5ed363 18720 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18721 struct line_header *lh = cu->line_header;
18722 bfd *abfd;
cf2c3c16
TT
18723 gdb_byte *mac_ptr, *mac_end;
18724 struct macro_source_file *current_file = 0;
18725 enum dwarf_macro_record_type macinfo_type;
18726 unsigned int offset_size = cu->header.offset_size;
18727 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18728 struct cleanup *cleanup;
18729 htab_t include_hash;
18730 void **slot;
09262596
DE
18731 struct dwarf2_section_info *section;
18732 const char *section_name;
18733
18734 if (cu->dwo_unit != NULL)
18735 {
18736 if (section_is_gnu)
18737 {
18738 section = &cu->dwo_unit->dwo_file->sections.macro;
18739 section_name = ".debug_macro.dwo";
18740 }
18741 else
18742 {
18743 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18744 section_name = ".debug_macinfo.dwo";
18745 }
18746 }
18747 else
18748 {
18749 if (section_is_gnu)
18750 {
18751 section = &dwarf2_per_objfile->macro;
18752 section_name = ".debug_macro";
18753 }
18754 else
18755 {
18756 section = &dwarf2_per_objfile->macinfo;
18757 section_name = ".debug_macinfo";
18758 }
18759 }
cf2c3c16 18760
bb5ed363 18761 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18762 if (section->buffer == NULL)
18763 {
fceca515 18764 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18765 return;
18766 }
09262596 18767 abfd = section->asection->owner;
cf2c3c16
TT
18768
18769 /* First pass: Find the name of the base filename.
18770 This filename is needed in order to process all macros whose definition
18771 (or undefinition) comes from the command line. These macros are defined
18772 before the first DW_MACINFO_start_file entry, and yet still need to be
18773 associated to the base file.
18774
18775 To determine the base file name, we scan the macro definitions until we
18776 reach the first DW_MACINFO_start_file entry. We then initialize
18777 CURRENT_FILE accordingly so that any macro definition found before the
18778 first DW_MACINFO_start_file can still be associated to the base file. */
18779
18780 mac_ptr = section->buffer + offset;
18781 mac_end = section->buffer + section->size;
18782
18783 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18784 &offset_size, section_is_gnu);
18785 if (mac_ptr == NULL)
18786 {
18787 /* We already issued a complaint. */
18788 return;
18789 }
18790
18791 do
18792 {
18793 /* Do we at least have room for a macinfo type byte? */
18794 if (mac_ptr >= mac_end)
18795 {
18796 /* Complaint is printed during the second pass as GDB will probably
18797 stop the first pass earlier upon finding
18798 DW_MACINFO_start_file. */
18799 break;
18800 }
18801
18802 macinfo_type = read_1_byte (abfd, mac_ptr);
18803 mac_ptr++;
18804
18805 /* Note that we rely on the fact that the corresponding GNU and
18806 DWARF constants are the same. */
18807 switch (macinfo_type)
18808 {
18809 /* A zero macinfo type indicates the end of the macro
18810 information. */
18811 case 0:
18812 break;
18813
18814 case DW_MACRO_GNU_define:
18815 case DW_MACRO_GNU_undef:
18816 /* Only skip the data by MAC_PTR. */
18817 {
18818 unsigned int bytes_read;
18819
18820 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18821 mac_ptr += bytes_read;
18822 read_direct_string (abfd, mac_ptr, &bytes_read);
18823 mac_ptr += bytes_read;
18824 }
18825 break;
18826
18827 case DW_MACRO_GNU_start_file:
18828 {
18829 unsigned int bytes_read;
18830 int line, file;
18831
18832 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18833 mac_ptr += bytes_read;
18834 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18835 mac_ptr += bytes_read;
18836
18837 current_file = macro_start_file (file, line, current_file,
bb5ed363 18838 comp_dir, lh, objfile);
cf2c3c16
TT
18839 }
18840 break;
18841
18842 case DW_MACRO_GNU_end_file:
18843 /* No data to skip by MAC_PTR. */
18844 break;
18845
18846 case DW_MACRO_GNU_define_indirect:
18847 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18848 case DW_MACRO_GNU_define_indirect_alt:
18849 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18850 {
18851 unsigned int bytes_read;
18852
18853 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18854 mac_ptr += bytes_read;
18855 mac_ptr += offset_size;
18856 }
18857 break;
18858
18859 case DW_MACRO_GNU_transparent_include:
f7a35f02 18860 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18861 /* Note that, according to the spec, a transparent include
18862 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18863 skip this opcode. */
18864 mac_ptr += offset_size;
18865 break;
18866
18867 case DW_MACINFO_vendor_ext:
18868 /* Only skip the data by MAC_PTR. */
18869 if (!section_is_gnu)
18870 {
18871 unsigned int bytes_read;
18872
18873 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18874 mac_ptr += bytes_read;
18875 read_direct_string (abfd, mac_ptr, &bytes_read);
18876 mac_ptr += bytes_read;
18877 }
18878 /* FALLTHROUGH */
18879
18880 default:
18881 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18882 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18883 section);
18884 if (mac_ptr == NULL)
18885 return;
18886 break;
18887 }
18888 } while (macinfo_type != 0 && current_file == NULL);
18889
18890 /* Second pass: Process all entries.
18891
18892 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18893 command-line macro definitions/undefinitions. This flag is unset when we
18894 reach the first DW_MACINFO_start_file entry. */
18895
8fc3fc34
TT
18896 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18897 NULL, xcalloc, xfree);
18898 cleanup = make_cleanup_htab_delete (include_hash);
18899 mac_ptr = section->buffer + offset;
18900 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18901 *slot = mac_ptr;
18902 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18903 current_file, lh, comp_dir, section,
18904 section_is_gnu, 0,
8fc3fc34
TT
18905 offset_size, objfile, include_hash);
18906 do_cleanups (cleanup);
cf2c3c16
TT
18907}
18908
8e19ed76 18909/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18910 if so return true else false. */
380bca97 18911
8e19ed76
PS
18912static int
18913attr_form_is_block (struct attribute *attr)
18914{
18915 return (attr == NULL ? 0 :
18916 attr->form == DW_FORM_block1
18917 || attr->form == DW_FORM_block2
18918 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18919 || attr->form == DW_FORM_block
18920 || attr->form == DW_FORM_exprloc);
8e19ed76 18921}
4c2df51b 18922
c6a0999f
JB
18923/* Return non-zero if ATTR's value is a section offset --- classes
18924 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18925 You may use DW_UNSND (attr) to retrieve such offsets.
18926
18927 Section 7.5.4, "Attribute Encodings", explains that no attribute
18928 may have a value that belongs to more than one of these classes; it
18929 would be ambiguous if we did, because we use the same forms for all
18930 of them. */
380bca97 18931
3690dd37
JB
18932static int
18933attr_form_is_section_offset (struct attribute *attr)
18934{
18935 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18936 || attr->form == DW_FORM_data8
18937 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18938}
18939
3690dd37
JB
18940/* Return non-zero if ATTR's value falls in the 'constant' class, or
18941 zero otherwise. When this function returns true, you can apply
18942 dwarf2_get_attr_constant_value to it.
18943
18944 However, note that for some attributes you must check
18945 attr_form_is_section_offset before using this test. DW_FORM_data4
18946 and DW_FORM_data8 are members of both the constant class, and of
18947 the classes that contain offsets into other debug sections
18948 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18949 that, if an attribute's can be either a constant or one of the
18950 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18951 taken as section offsets, not constants. */
380bca97 18952
3690dd37
JB
18953static int
18954attr_form_is_constant (struct attribute *attr)
18955{
18956 switch (attr->form)
18957 {
18958 case DW_FORM_sdata:
18959 case DW_FORM_udata:
18960 case DW_FORM_data1:
18961 case DW_FORM_data2:
18962 case DW_FORM_data4:
18963 case DW_FORM_data8:
18964 return 1;
18965 default:
18966 return 0;
18967 }
18968}
18969
3019eac3
DE
18970/* Return the .debug_loc section to use for CU.
18971 For DWO files use .debug_loc.dwo. */
18972
18973static struct dwarf2_section_info *
18974cu_debug_loc_section (struct dwarf2_cu *cu)
18975{
18976 if (cu->dwo_unit)
18977 return &cu->dwo_unit->dwo_file->sections.loc;
18978 return &dwarf2_per_objfile->loc;
18979}
18980
8cf6f0b1
TT
18981/* A helper function that fills in a dwarf2_loclist_baton. */
18982
18983static void
18984fill_in_loclist_baton (struct dwarf2_cu *cu,
18985 struct dwarf2_loclist_baton *baton,
18986 struct attribute *attr)
18987{
3019eac3
DE
18988 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18989
18990 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18991
18992 baton->per_cu = cu->per_cu;
18993 gdb_assert (baton->per_cu);
18994 /* We don't know how long the location list is, but make sure we
18995 don't run off the edge of the section. */
3019eac3
DE
18996 baton->size = section->size - DW_UNSND (attr);
18997 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18998 baton->base_address = cu->base_address;
f664829e 18999 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19000}
19001
4c2df51b
DJ
19002static void
19003dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 19004 struct dwarf2_cu *cu)
4c2df51b 19005{
bb5ed363 19006 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19007 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19008
3690dd37 19009 if (attr_form_is_section_offset (attr)
3019eac3 19010 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19011 the section. If so, fall through to the complaint in the
19012 other branch. */
3019eac3 19013 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19014 {
0d53c4c4 19015 struct dwarf2_loclist_baton *baton;
4c2df51b 19016
bb5ed363 19017 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19018 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19019
8cf6f0b1 19020 fill_in_loclist_baton (cu, baton, attr);
be391dca 19021
d00adf39 19022 if (cu->base_known == 0)
0d53c4c4 19023 complaint (&symfile_complaints,
3e43a32a
MS
19024 _("Location list used without "
19025 "specifying the CU base address."));
4c2df51b 19026
768a979c 19027 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
19028 SYMBOL_LOCATION_BATON (sym) = baton;
19029 }
19030 else
19031 {
19032 struct dwarf2_locexpr_baton *baton;
19033
bb5ed363 19034 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19035 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19036 baton->per_cu = cu->per_cu;
19037 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19038
19039 if (attr_form_is_block (attr))
19040 {
19041 /* Note that we're just copying the block's data pointer
19042 here, not the actual data. We're still pointing into the
6502dd73
DJ
19043 info_buffer for SYM's objfile; right now we never release
19044 that buffer, but when we do clean up properly this may
19045 need to change. */
0d53c4c4
DJ
19046 baton->size = DW_BLOCK (attr)->size;
19047 baton->data = DW_BLOCK (attr)->data;
19048 }
19049 else
19050 {
19051 dwarf2_invalid_attrib_class_complaint ("location description",
19052 SYMBOL_NATURAL_NAME (sym));
19053 baton->size = 0;
0d53c4c4 19054 }
6e70227d 19055
768a979c 19056 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
19057 SYMBOL_LOCATION_BATON (sym) = baton;
19058 }
4c2df51b 19059}
6502dd73 19060
9aa1f1e3
TT
19061/* Return the OBJFILE associated with the compilation unit CU. If CU
19062 came from a separate debuginfo file, then the master objfile is
19063 returned. */
ae0d2f24
UW
19064
19065struct objfile *
19066dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19067{
9291a0cd 19068 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19069
19070 /* Return the master objfile, so that we can report and look up the
19071 correct file containing this variable. */
19072 if (objfile->separate_debug_objfile_backlink)
19073 objfile = objfile->separate_debug_objfile_backlink;
19074
19075 return objfile;
19076}
19077
96408a79
SA
19078/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19079 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19080 CU_HEADERP first. */
19081
19082static const struct comp_unit_head *
19083per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19084 struct dwarf2_per_cu_data *per_cu)
19085{
96408a79
SA
19086 gdb_byte *info_ptr;
19087
19088 if (per_cu->cu)
19089 return &per_cu->cu->header;
19090
0bc3a05c 19091 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19092
19093 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19094 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19095
19096 return cu_headerp;
19097}
19098
ae0d2f24
UW
19099/* Return the address size given in the compilation unit header for CU. */
19100
98714339 19101int
ae0d2f24
UW
19102dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19103{
96408a79
SA
19104 struct comp_unit_head cu_header_local;
19105 const struct comp_unit_head *cu_headerp;
c471e790 19106
96408a79
SA
19107 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19108
19109 return cu_headerp->addr_size;
ae0d2f24
UW
19110}
19111
9eae7c52
TT
19112/* Return the offset size given in the compilation unit header for CU. */
19113
19114int
19115dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19116{
96408a79
SA
19117 struct comp_unit_head cu_header_local;
19118 const struct comp_unit_head *cu_headerp;
9c6c53f7 19119
96408a79
SA
19120 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19121
19122 return cu_headerp->offset_size;
19123}
19124
19125/* See its dwarf2loc.h declaration. */
19126
19127int
19128dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19129{
19130 struct comp_unit_head cu_header_local;
19131 const struct comp_unit_head *cu_headerp;
19132
19133 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19134
19135 if (cu_headerp->version == 2)
19136 return cu_headerp->addr_size;
19137 else
19138 return cu_headerp->offset_size;
181cebd4
JK
19139}
19140
9aa1f1e3
TT
19141/* Return the text offset of the CU. The returned offset comes from
19142 this CU's objfile. If this objfile came from a separate debuginfo
19143 file, then the offset may be different from the corresponding
19144 offset in the parent objfile. */
19145
19146CORE_ADDR
19147dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19148{
bb3fa9d0 19149 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19150
19151 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19152}
19153
348e048f
DE
19154/* Locate the .debug_info compilation unit from CU's objfile which contains
19155 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19156
19157static struct dwarf2_per_cu_data *
b64f50a1 19158dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19159 unsigned int offset_in_dwz,
ae038cb0
DJ
19160 struct objfile *objfile)
19161{
19162 struct dwarf2_per_cu_data *this_cu;
19163 int low, high;
36586728 19164 const sect_offset *cu_off;
ae038cb0 19165
ae038cb0
DJ
19166 low = 0;
19167 high = dwarf2_per_objfile->n_comp_units - 1;
19168 while (high > low)
19169 {
36586728 19170 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19171 int mid = low + (high - low) / 2;
9a619af0 19172
36586728
TT
19173 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19174 cu_off = &mid_cu->offset;
19175 if (mid_cu->is_dwz > offset_in_dwz
19176 || (mid_cu->is_dwz == offset_in_dwz
19177 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19178 high = mid;
19179 else
19180 low = mid + 1;
19181 }
19182 gdb_assert (low == high);
36586728
TT
19183 this_cu = dwarf2_per_objfile->all_comp_units[low];
19184 cu_off = &this_cu->offset;
19185 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19186 {
36586728 19187 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19188 error (_("Dwarf Error: could not find partial DIE containing "
19189 "offset 0x%lx [in module %s]"),
b64f50a1 19190 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19191
b64f50a1
JK
19192 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19193 <= offset.sect_off);
ae038cb0
DJ
19194 return dwarf2_per_objfile->all_comp_units[low-1];
19195 }
19196 else
19197 {
19198 this_cu = dwarf2_per_objfile->all_comp_units[low];
19199 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19200 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19201 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19202 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19203 return this_cu;
19204 }
19205}
19206
23745b47 19207/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19208
9816fde3 19209static void
23745b47 19210init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19211{
9816fde3 19212 memset (cu, 0, sizeof (*cu));
23745b47
DE
19213 per_cu->cu = cu;
19214 cu->per_cu = per_cu;
19215 cu->objfile = per_cu->objfile;
93311388 19216 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19217}
19218
19219/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19220
19221static void
95554aad
TT
19222prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19223 enum language pretend_language)
9816fde3
JK
19224{
19225 struct attribute *attr;
19226
19227 /* Set the language we're debugging. */
19228 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19229 if (attr)
19230 set_cu_language (DW_UNSND (attr), cu);
19231 else
9cded63f 19232 {
95554aad 19233 cu->language = pretend_language;
9cded63f
TT
19234 cu->language_defn = language_def (cu->language);
19235 }
dee91e82
DE
19236
19237 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19238 if (attr)
19239 cu->producer = DW_STRING (attr);
93311388
DE
19240}
19241
ae038cb0
DJ
19242/* Release one cached compilation unit, CU. We unlink it from the tree
19243 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19244 the caller is responsible for that.
19245 NOTE: DATA is a void * because this function is also used as a
19246 cleanup routine. */
ae038cb0
DJ
19247
19248static void
68dc6402 19249free_heap_comp_unit (void *data)
ae038cb0
DJ
19250{
19251 struct dwarf2_cu *cu = data;
19252
23745b47
DE
19253 gdb_assert (cu->per_cu != NULL);
19254 cu->per_cu->cu = NULL;
ae038cb0
DJ
19255 cu->per_cu = NULL;
19256
19257 obstack_free (&cu->comp_unit_obstack, NULL);
19258
19259 xfree (cu);
19260}
19261
72bf9492 19262/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19263 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19264 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19265
19266static void
19267free_stack_comp_unit (void *data)
19268{
19269 struct dwarf2_cu *cu = data;
19270
23745b47
DE
19271 gdb_assert (cu->per_cu != NULL);
19272 cu->per_cu->cu = NULL;
19273 cu->per_cu = NULL;
19274
72bf9492
DJ
19275 obstack_free (&cu->comp_unit_obstack, NULL);
19276 cu->partial_dies = NULL;
ae038cb0
DJ
19277}
19278
19279/* Free all cached compilation units. */
19280
19281static void
19282free_cached_comp_units (void *data)
19283{
19284 struct dwarf2_per_cu_data *per_cu, **last_chain;
19285
19286 per_cu = dwarf2_per_objfile->read_in_chain;
19287 last_chain = &dwarf2_per_objfile->read_in_chain;
19288 while (per_cu != NULL)
19289 {
19290 struct dwarf2_per_cu_data *next_cu;
19291
19292 next_cu = per_cu->cu->read_in_chain;
19293
68dc6402 19294 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19295 *last_chain = next_cu;
19296
19297 per_cu = next_cu;
19298 }
19299}
19300
19301/* Increase the age counter on each cached compilation unit, and free
19302 any that are too old. */
19303
19304static void
19305age_cached_comp_units (void)
19306{
19307 struct dwarf2_per_cu_data *per_cu, **last_chain;
19308
19309 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19310 per_cu = dwarf2_per_objfile->read_in_chain;
19311 while (per_cu != NULL)
19312 {
19313 per_cu->cu->last_used ++;
19314 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19315 dwarf2_mark (per_cu->cu);
19316 per_cu = per_cu->cu->read_in_chain;
19317 }
19318
19319 per_cu = dwarf2_per_objfile->read_in_chain;
19320 last_chain = &dwarf2_per_objfile->read_in_chain;
19321 while (per_cu != NULL)
19322 {
19323 struct dwarf2_per_cu_data *next_cu;
19324
19325 next_cu = per_cu->cu->read_in_chain;
19326
19327 if (!per_cu->cu->mark)
19328 {
68dc6402 19329 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19330 *last_chain = next_cu;
19331 }
19332 else
19333 last_chain = &per_cu->cu->read_in_chain;
19334
19335 per_cu = next_cu;
19336 }
19337}
19338
19339/* Remove a single compilation unit from the cache. */
19340
19341static void
dee91e82 19342free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19343{
19344 struct dwarf2_per_cu_data *per_cu, **last_chain;
19345
19346 per_cu = dwarf2_per_objfile->read_in_chain;
19347 last_chain = &dwarf2_per_objfile->read_in_chain;
19348 while (per_cu != NULL)
19349 {
19350 struct dwarf2_per_cu_data *next_cu;
19351
19352 next_cu = per_cu->cu->read_in_chain;
19353
dee91e82 19354 if (per_cu == target_per_cu)
ae038cb0 19355 {
68dc6402 19356 free_heap_comp_unit (per_cu->cu);
dee91e82 19357 per_cu->cu = NULL;
ae038cb0
DJ
19358 *last_chain = next_cu;
19359 break;
19360 }
19361 else
19362 last_chain = &per_cu->cu->read_in_chain;
19363
19364 per_cu = next_cu;
19365 }
19366}
19367
fe3e1990
DJ
19368/* Release all extra memory associated with OBJFILE. */
19369
19370void
19371dwarf2_free_objfile (struct objfile *objfile)
19372{
19373 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19374
19375 if (dwarf2_per_objfile == NULL)
19376 return;
19377
19378 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19379 free_cached_comp_units (NULL);
19380
7b9f3c50
DE
19381 if (dwarf2_per_objfile->quick_file_names_table)
19382 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19383
fe3e1990
DJ
19384 /* Everything else should be on the objfile obstack. */
19385}
19386
dee91e82
DE
19387/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19388 We store these in a hash table separate from the DIEs, and preserve them
19389 when the DIEs are flushed out of cache.
19390
19391 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19392 uniquely identify the type. A file may have multiple .debug_types sections,
19393 or the type may come from a DWO file. We have to use something in
19394 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19395 routine, get_die_type_at_offset, from outside this file, and thus won't
19396 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19397 of the objfile. */
1c379e20 19398
dee91e82 19399struct dwarf2_per_cu_offset_and_type
1c379e20 19400{
dee91e82 19401 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19402 sect_offset offset;
1c379e20
DJ
19403 struct type *type;
19404};
19405
dee91e82 19406/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19407
19408static hashval_t
dee91e82 19409per_cu_offset_and_type_hash (const void *item)
1c379e20 19410{
dee91e82 19411 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19412
dee91e82 19413 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19414}
19415
dee91e82 19416/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19417
19418static int
dee91e82 19419per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19420{
dee91e82
DE
19421 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19422 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19423
dee91e82
DE
19424 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19425 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19426}
19427
19428/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19429 table if necessary. For convenience, return TYPE.
19430
19431 The DIEs reading must have careful ordering to:
19432 * Not cause infite loops trying to read in DIEs as a prerequisite for
19433 reading current DIE.
19434 * Not trying to dereference contents of still incompletely read in types
19435 while reading in other DIEs.
19436 * Enable referencing still incompletely read in types just by a pointer to
19437 the type without accessing its fields.
19438
19439 Therefore caller should follow these rules:
19440 * Try to fetch any prerequisite types we may need to build this DIE type
19441 before building the type and calling set_die_type.
e71ec853 19442 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19443 possible before fetching more types to complete the current type.
19444 * Make the type as complete as possible before fetching more types. */
1c379e20 19445
f792889a 19446static struct type *
1c379e20
DJ
19447set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19448{
dee91e82 19449 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19450 struct objfile *objfile = cu->objfile;
1c379e20 19451
b4ba55a1
JB
19452 /* For Ada types, make sure that the gnat-specific data is always
19453 initialized (if not already set). There are a few types where
19454 we should not be doing so, because the type-specific area is
19455 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19456 where the type-specific area is used to store the floatformat).
19457 But this is not a problem, because the gnat-specific information
19458 is actually not needed for these types. */
19459 if (need_gnat_info (cu)
19460 && TYPE_CODE (type) != TYPE_CODE_FUNC
19461 && TYPE_CODE (type) != TYPE_CODE_FLT
19462 && !HAVE_GNAT_AUX_INFO (type))
19463 INIT_GNAT_SPECIFIC (type);
19464
dee91e82 19465 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19466 {
dee91e82
DE
19467 dwarf2_per_objfile->die_type_hash =
19468 htab_create_alloc_ex (127,
19469 per_cu_offset_and_type_hash,
19470 per_cu_offset_and_type_eq,
19471 NULL,
19472 &objfile->objfile_obstack,
19473 hashtab_obstack_allocate,
19474 dummy_obstack_deallocate);
f792889a 19475 }
1c379e20 19476
dee91e82 19477 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19478 ofs.offset = die->offset;
19479 ofs.type = type;
dee91e82
DE
19480 slot = (struct dwarf2_per_cu_offset_and_type **)
19481 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19482 if (*slot)
19483 complaint (&symfile_complaints,
19484 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19485 die->offset.sect_off);
673bfd45 19486 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19487 **slot = ofs;
f792889a 19488 return type;
1c379e20
DJ
19489}
19490
380bca97 19491/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19492 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19493
19494static struct type *
b64f50a1 19495get_die_type_at_offset (sect_offset offset,
673bfd45 19496 struct dwarf2_per_cu_data *per_cu)
1c379e20 19497{
dee91e82 19498 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19499
dee91e82 19500 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19501 return NULL;
1c379e20 19502
dee91e82 19503 ofs.per_cu = per_cu;
673bfd45 19504 ofs.offset = offset;
dee91e82 19505 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19506 if (slot)
19507 return slot->type;
19508 else
19509 return NULL;
19510}
19511
673bfd45
DE
19512/* Look up the type for DIE in the appropriate type_hash table,
19513 or return NULL if DIE does not have a saved type. */
19514
19515static struct type *
19516get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19517{
19518 return get_die_type_at_offset (die->offset, cu->per_cu);
19519}
19520
10b3939b
DJ
19521/* Add a dependence relationship from CU to REF_PER_CU. */
19522
19523static void
19524dwarf2_add_dependence (struct dwarf2_cu *cu,
19525 struct dwarf2_per_cu_data *ref_per_cu)
19526{
19527 void **slot;
19528
19529 if (cu->dependencies == NULL)
19530 cu->dependencies
19531 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19532 NULL, &cu->comp_unit_obstack,
19533 hashtab_obstack_allocate,
19534 dummy_obstack_deallocate);
19535
19536 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19537 if (*slot == NULL)
19538 *slot = ref_per_cu;
19539}
1c379e20 19540
f504f079
DE
19541/* Subroutine of dwarf2_mark to pass to htab_traverse.
19542 Set the mark field in every compilation unit in the
ae038cb0
DJ
19543 cache that we must keep because we are keeping CU. */
19544
10b3939b
DJ
19545static int
19546dwarf2_mark_helper (void **slot, void *data)
19547{
19548 struct dwarf2_per_cu_data *per_cu;
19549
19550 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19551
19552 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19553 reading of the chain. As such dependencies remain valid it is not much
19554 useful to track and undo them during QUIT cleanups. */
19555 if (per_cu->cu == NULL)
19556 return 1;
19557
10b3939b
DJ
19558 if (per_cu->cu->mark)
19559 return 1;
19560 per_cu->cu->mark = 1;
19561
19562 if (per_cu->cu->dependencies != NULL)
19563 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19564
19565 return 1;
19566}
19567
f504f079
DE
19568/* Set the mark field in CU and in every other compilation unit in the
19569 cache that we must keep because we are keeping CU. */
19570
ae038cb0
DJ
19571static void
19572dwarf2_mark (struct dwarf2_cu *cu)
19573{
19574 if (cu->mark)
19575 return;
19576 cu->mark = 1;
10b3939b
DJ
19577 if (cu->dependencies != NULL)
19578 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19579}
19580
19581static void
19582dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19583{
19584 while (per_cu)
19585 {
19586 per_cu->cu->mark = 0;
19587 per_cu = per_cu->cu->read_in_chain;
19588 }
72bf9492
DJ
19589}
19590
72bf9492
DJ
19591/* Trivial hash function for partial_die_info: the hash value of a DIE
19592 is its offset in .debug_info for this objfile. */
19593
19594static hashval_t
19595partial_die_hash (const void *item)
19596{
19597 const struct partial_die_info *part_die = item;
9a619af0 19598
b64f50a1 19599 return part_die->offset.sect_off;
72bf9492
DJ
19600}
19601
19602/* Trivial comparison function for partial_die_info structures: two DIEs
19603 are equal if they have the same offset. */
19604
19605static int
19606partial_die_eq (const void *item_lhs, const void *item_rhs)
19607{
19608 const struct partial_die_info *part_die_lhs = item_lhs;
19609 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19610
b64f50a1 19611 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19612}
19613
ae038cb0
DJ
19614static struct cmd_list_element *set_dwarf2_cmdlist;
19615static struct cmd_list_element *show_dwarf2_cmdlist;
19616
19617static void
19618set_dwarf2_cmd (char *args, int from_tty)
19619{
19620 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19621}
19622
19623static void
19624show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19625{
ae038cb0
DJ
19626 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19627}
19628
4bf44c1c 19629/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19630
19631static void
c1bd65d0 19632dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19633{
19634 struct dwarf2_per_objfile *data = d;
8b70b953 19635 int ix;
8b70b953 19636
95554aad
TT
19637 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19638 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19639 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19640
8b70b953 19641 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19642
19643 if (data->dwo_files)
19644 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19645
19646 if (data->dwz_file && data->dwz_file->dwz_bfd)
19647 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19648}
19649
19650\f
ae2de4f8 19651/* The "save gdb-index" command. */
9291a0cd
TT
19652
19653/* The contents of the hash table we create when building the string
19654 table. */
19655struct strtab_entry
19656{
19657 offset_type offset;
19658 const char *str;
19659};
19660
559a7a62
JK
19661/* Hash function for a strtab_entry.
19662
19663 Function is used only during write_hash_table so no index format backward
19664 compatibility is needed. */
b89be57b 19665
9291a0cd
TT
19666static hashval_t
19667hash_strtab_entry (const void *e)
19668{
19669 const struct strtab_entry *entry = e;
559a7a62 19670 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19671}
19672
19673/* Equality function for a strtab_entry. */
b89be57b 19674
9291a0cd
TT
19675static int
19676eq_strtab_entry (const void *a, const void *b)
19677{
19678 const struct strtab_entry *ea = a;
19679 const struct strtab_entry *eb = b;
19680 return !strcmp (ea->str, eb->str);
19681}
19682
19683/* Create a strtab_entry hash table. */
b89be57b 19684
9291a0cd
TT
19685static htab_t
19686create_strtab (void)
19687{
19688 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19689 xfree, xcalloc, xfree);
19690}
19691
19692/* Add a string to the constant pool. Return the string's offset in
19693 host order. */
b89be57b 19694
9291a0cd
TT
19695static offset_type
19696add_string (htab_t table, struct obstack *cpool, const char *str)
19697{
19698 void **slot;
19699 struct strtab_entry entry;
19700 struct strtab_entry *result;
19701
19702 entry.str = str;
19703 slot = htab_find_slot (table, &entry, INSERT);
19704 if (*slot)
19705 result = *slot;
19706 else
19707 {
19708 result = XNEW (struct strtab_entry);
19709 result->offset = obstack_object_size (cpool);
19710 result->str = str;
19711 obstack_grow_str0 (cpool, str);
19712 *slot = result;
19713 }
19714 return result->offset;
19715}
19716
19717/* An entry in the symbol table. */
19718struct symtab_index_entry
19719{
19720 /* The name of the symbol. */
19721 const char *name;
19722 /* The offset of the name in the constant pool. */
19723 offset_type index_offset;
19724 /* A sorted vector of the indices of all the CUs that hold an object
19725 of this name. */
19726 VEC (offset_type) *cu_indices;
19727};
19728
19729/* The symbol table. This is a power-of-2-sized hash table. */
19730struct mapped_symtab
19731{
19732 offset_type n_elements;
19733 offset_type size;
19734 struct symtab_index_entry **data;
19735};
19736
19737/* Hash function for a symtab_index_entry. */
b89be57b 19738
9291a0cd
TT
19739static hashval_t
19740hash_symtab_entry (const void *e)
19741{
19742 const struct symtab_index_entry *entry = e;
19743 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19744 sizeof (offset_type) * VEC_length (offset_type,
19745 entry->cu_indices),
19746 0);
19747}
19748
19749/* Equality function for a symtab_index_entry. */
b89be57b 19750
9291a0cd
TT
19751static int
19752eq_symtab_entry (const void *a, const void *b)
19753{
19754 const struct symtab_index_entry *ea = a;
19755 const struct symtab_index_entry *eb = b;
19756 int len = VEC_length (offset_type, ea->cu_indices);
19757 if (len != VEC_length (offset_type, eb->cu_indices))
19758 return 0;
19759 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19760 VEC_address (offset_type, eb->cu_indices),
19761 sizeof (offset_type) * len);
19762}
19763
19764/* Destroy a symtab_index_entry. */
b89be57b 19765
9291a0cd
TT
19766static void
19767delete_symtab_entry (void *p)
19768{
19769 struct symtab_index_entry *entry = p;
19770 VEC_free (offset_type, entry->cu_indices);
19771 xfree (entry);
19772}
19773
19774/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19775
9291a0cd 19776static htab_t
3876f04e 19777create_symbol_hash_table (void)
9291a0cd
TT
19778{
19779 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19780 delete_symtab_entry, xcalloc, xfree);
19781}
19782
19783/* Create a new mapped symtab object. */
b89be57b 19784
9291a0cd
TT
19785static struct mapped_symtab *
19786create_mapped_symtab (void)
19787{
19788 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19789 symtab->n_elements = 0;
19790 symtab->size = 1024;
19791 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19792 return symtab;
19793}
19794
19795/* Destroy a mapped_symtab. */
b89be57b 19796
9291a0cd
TT
19797static void
19798cleanup_mapped_symtab (void *p)
19799{
19800 struct mapped_symtab *symtab = p;
19801 /* The contents of the array are freed when the other hash table is
19802 destroyed. */
19803 xfree (symtab->data);
19804 xfree (symtab);
19805}
19806
19807/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19808 the slot.
19809
19810 Function is used only during write_hash_table so no index format backward
19811 compatibility is needed. */
b89be57b 19812
9291a0cd
TT
19813static struct symtab_index_entry **
19814find_slot (struct mapped_symtab *symtab, const char *name)
19815{
559a7a62 19816 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19817
19818 index = hash & (symtab->size - 1);
19819 step = ((hash * 17) & (symtab->size - 1)) | 1;
19820
19821 for (;;)
19822 {
19823 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19824 return &symtab->data[index];
19825 index = (index + step) & (symtab->size - 1);
19826 }
19827}
19828
19829/* Expand SYMTAB's hash table. */
b89be57b 19830
9291a0cd
TT
19831static void
19832hash_expand (struct mapped_symtab *symtab)
19833{
19834 offset_type old_size = symtab->size;
19835 offset_type i;
19836 struct symtab_index_entry **old_entries = symtab->data;
19837
19838 symtab->size *= 2;
19839 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19840
19841 for (i = 0; i < old_size; ++i)
19842 {
19843 if (old_entries[i])
19844 {
19845 struct symtab_index_entry **slot = find_slot (symtab,
19846 old_entries[i]->name);
19847 *slot = old_entries[i];
19848 }
19849 }
19850
19851 xfree (old_entries);
19852}
19853
156942c7
DE
19854/* Add an entry to SYMTAB. NAME is the name of the symbol.
19855 CU_INDEX is the index of the CU in which the symbol appears.
19856 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19857
9291a0cd
TT
19858static void
19859add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19860 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19861 offset_type cu_index)
19862{
19863 struct symtab_index_entry **slot;
156942c7 19864 offset_type cu_index_and_attrs;
9291a0cd
TT
19865
19866 ++symtab->n_elements;
19867 if (4 * symtab->n_elements / 3 >= symtab->size)
19868 hash_expand (symtab);
19869
19870 slot = find_slot (symtab, name);
19871 if (!*slot)
19872 {
19873 *slot = XNEW (struct symtab_index_entry);
19874 (*slot)->name = name;
156942c7 19875 /* index_offset is set later. */
9291a0cd
TT
19876 (*slot)->cu_indices = NULL;
19877 }
156942c7
DE
19878
19879 cu_index_and_attrs = 0;
19880 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19881 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19882 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19883
19884 /* We don't want to record an index value twice as we want to avoid the
19885 duplication.
19886 We process all global symbols and then all static symbols
19887 (which would allow us to avoid the duplication by only having to check
19888 the last entry pushed), but a symbol could have multiple kinds in one CU.
19889 To keep things simple we don't worry about the duplication here and
19890 sort and uniqufy the list after we've processed all symbols. */
19891 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19892}
19893
19894/* qsort helper routine for uniquify_cu_indices. */
19895
19896static int
19897offset_type_compare (const void *ap, const void *bp)
19898{
19899 offset_type a = *(offset_type *) ap;
19900 offset_type b = *(offset_type *) bp;
19901
19902 return (a > b) - (b > a);
19903}
19904
19905/* Sort and remove duplicates of all symbols' cu_indices lists. */
19906
19907static void
19908uniquify_cu_indices (struct mapped_symtab *symtab)
19909{
19910 int i;
19911
19912 for (i = 0; i < symtab->size; ++i)
19913 {
19914 struct symtab_index_entry *entry = symtab->data[i];
19915
19916 if (entry
19917 && entry->cu_indices != NULL)
19918 {
19919 unsigned int next_to_insert, next_to_check;
19920 offset_type last_value;
19921
19922 qsort (VEC_address (offset_type, entry->cu_indices),
19923 VEC_length (offset_type, entry->cu_indices),
19924 sizeof (offset_type), offset_type_compare);
19925
19926 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19927 next_to_insert = 1;
19928 for (next_to_check = 1;
19929 next_to_check < VEC_length (offset_type, entry->cu_indices);
19930 ++next_to_check)
19931 {
19932 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19933 != last_value)
19934 {
19935 last_value = VEC_index (offset_type, entry->cu_indices,
19936 next_to_check);
19937 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19938 last_value);
19939 ++next_to_insert;
19940 }
19941 }
19942 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19943 }
19944 }
9291a0cd
TT
19945}
19946
19947/* Add a vector of indices to the constant pool. */
b89be57b 19948
9291a0cd 19949static offset_type
3876f04e 19950add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19951 struct symtab_index_entry *entry)
19952{
19953 void **slot;
19954
3876f04e 19955 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19956 if (!*slot)
19957 {
19958 offset_type len = VEC_length (offset_type, entry->cu_indices);
19959 offset_type val = MAYBE_SWAP (len);
19960 offset_type iter;
19961 int i;
19962
19963 *slot = entry;
19964 entry->index_offset = obstack_object_size (cpool);
19965
19966 obstack_grow (cpool, &val, sizeof (val));
19967 for (i = 0;
19968 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19969 ++i)
19970 {
19971 val = MAYBE_SWAP (iter);
19972 obstack_grow (cpool, &val, sizeof (val));
19973 }
19974 }
19975 else
19976 {
19977 struct symtab_index_entry *old_entry = *slot;
19978 entry->index_offset = old_entry->index_offset;
19979 entry = old_entry;
19980 }
19981 return entry->index_offset;
19982}
19983
19984/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19985 constant pool entries going into the obstack CPOOL. */
b89be57b 19986
9291a0cd
TT
19987static void
19988write_hash_table (struct mapped_symtab *symtab,
19989 struct obstack *output, struct obstack *cpool)
19990{
19991 offset_type i;
3876f04e 19992 htab_t symbol_hash_table;
9291a0cd
TT
19993 htab_t str_table;
19994
3876f04e 19995 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19996 str_table = create_strtab ();
3876f04e 19997
9291a0cd
TT
19998 /* We add all the index vectors to the constant pool first, to
19999 ensure alignment is ok. */
20000 for (i = 0; i < symtab->size; ++i)
20001 {
20002 if (symtab->data[i])
3876f04e 20003 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20004 }
20005
20006 /* Now write out the hash table. */
20007 for (i = 0; i < symtab->size; ++i)
20008 {
20009 offset_type str_off, vec_off;
20010
20011 if (symtab->data[i])
20012 {
20013 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20014 vec_off = symtab->data[i]->index_offset;
20015 }
20016 else
20017 {
20018 /* While 0 is a valid constant pool index, it is not valid
20019 to have 0 for both offsets. */
20020 str_off = 0;
20021 vec_off = 0;
20022 }
20023
20024 str_off = MAYBE_SWAP (str_off);
20025 vec_off = MAYBE_SWAP (vec_off);
20026
20027 obstack_grow (output, &str_off, sizeof (str_off));
20028 obstack_grow (output, &vec_off, sizeof (vec_off));
20029 }
20030
20031 htab_delete (str_table);
3876f04e 20032 htab_delete (symbol_hash_table);
9291a0cd
TT
20033}
20034
0a5429f6
DE
20035/* Struct to map psymtab to CU index in the index file. */
20036struct psymtab_cu_index_map
20037{
20038 struct partial_symtab *psymtab;
20039 unsigned int cu_index;
20040};
20041
20042static hashval_t
20043hash_psymtab_cu_index (const void *item)
20044{
20045 const struct psymtab_cu_index_map *map = item;
20046
20047 return htab_hash_pointer (map->psymtab);
20048}
20049
20050static int
20051eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20052{
20053 const struct psymtab_cu_index_map *lhs = item_lhs;
20054 const struct psymtab_cu_index_map *rhs = item_rhs;
20055
20056 return lhs->psymtab == rhs->psymtab;
20057}
20058
20059/* Helper struct for building the address table. */
20060struct addrmap_index_data
20061{
20062 struct objfile *objfile;
20063 struct obstack *addr_obstack;
20064 htab_t cu_index_htab;
20065
20066 /* Non-zero if the previous_* fields are valid.
20067 We can't write an entry until we see the next entry (since it is only then
20068 that we know the end of the entry). */
20069 int previous_valid;
20070 /* Index of the CU in the table of all CUs in the index file. */
20071 unsigned int previous_cu_index;
0963b4bd 20072 /* Start address of the CU. */
0a5429f6
DE
20073 CORE_ADDR previous_cu_start;
20074};
20075
20076/* Write an address entry to OBSTACK. */
b89be57b 20077
9291a0cd 20078static void
0a5429f6
DE
20079add_address_entry (struct objfile *objfile, struct obstack *obstack,
20080 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20081{
0a5429f6 20082 offset_type cu_index_to_write;
9291a0cd
TT
20083 char addr[8];
20084 CORE_ADDR baseaddr;
20085
20086 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20087
0a5429f6
DE
20088 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20089 obstack_grow (obstack, addr, 8);
20090 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20091 obstack_grow (obstack, addr, 8);
20092 cu_index_to_write = MAYBE_SWAP (cu_index);
20093 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20094}
20095
20096/* Worker function for traversing an addrmap to build the address table. */
20097
20098static int
20099add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20100{
20101 struct addrmap_index_data *data = datap;
20102 struct partial_symtab *pst = obj;
0a5429f6
DE
20103
20104 if (data->previous_valid)
20105 add_address_entry (data->objfile, data->addr_obstack,
20106 data->previous_cu_start, start_addr,
20107 data->previous_cu_index);
20108
20109 data->previous_cu_start = start_addr;
20110 if (pst != NULL)
20111 {
20112 struct psymtab_cu_index_map find_map, *map;
20113 find_map.psymtab = pst;
20114 map = htab_find (data->cu_index_htab, &find_map);
20115 gdb_assert (map != NULL);
20116 data->previous_cu_index = map->cu_index;
20117 data->previous_valid = 1;
20118 }
20119 else
20120 data->previous_valid = 0;
20121
20122 return 0;
20123}
20124
20125/* Write OBJFILE's address map to OBSTACK.
20126 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20127 in the index file. */
20128
20129static void
20130write_address_map (struct objfile *objfile, struct obstack *obstack,
20131 htab_t cu_index_htab)
20132{
20133 struct addrmap_index_data addrmap_index_data;
20134
20135 /* When writing the address table, we have to cope with the fact that
20136 the addrmap iterator only provides the start of a region; we have to
20137 wait until the next invocation to get the start of the next region. */
20138
20139 addrmap_index_data.objfile = objfile;
20140 addrmap_index_data.addr_obstack = obstack;
20141 addrmap_index_data.cu_index_htab = cu_index_htab;
20142 addrmap_index_data.previous_valid = 0;
20143
20144 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20145 &addrmap_index_data);
20146
20147 /* It's highly unlikely the last entry (end address = 0xff...ff)
20148 is valid, but we should still handle it.
20149 The end address is recorded as the start of the next region, but that
20150 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20151 anyway. */
20152 if (addrmap_index_data.previous_valid)
20153 add_address_entry (objfile, obstack,
20154 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20155 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20156}
20157
156942c7
DE
20158/* Return the symbol kind of PSYM. */
20159
20160static gdb_index_symbol_kind
20161symbol_kind (struct partial_symbol *psym)
20162{
20163 domain_enum domain = PSYMBOL_DOMAIN (psym);
20164 enum address_class aclass = PSYMBOL_CLASS (psym);
20165
20166 switch (domain)
20167 {
20168 case VAR_DOMAIN:
20169 switch (aclass)
20170 {
20171 case LOC_BLOCK:
20172 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20173 case LOC_TYPEDEF:
20174 return GDB_INDEX_SYMBOL_KIND_TYPE;
20175 case LOC_COMPUTED:
20176 case LOC_CONST_BYTES:
20177 case LOC_OPTIMIZED_OUT:
20178 case LOC_STATIC:
20179 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20180 case LOC_CONST:
20181 /* Note: It's currently impossible to recognize psyms as enum values
20182 short of reading the type info. For now punt. */
20183 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20184 default:
20185 /* There are other LOC_FOO values that one might want to classify
20186 as variables, but dwarf2read.c doesn't currently use them. */
20187 return GDB_INDEX_SYMBOL_KIND_OTHER;
20188 }
20189 case STRUCT_DOMAIN:
20190 return GDB_INDEX_SYMBOL_KIND_TYPE;
20191 default:
20192 return GDB_INDEX_SYMBOL_KIND_OTHER;
20193 }
20194}
20195
9291a0cd 20196/* Add a list of partial symbols to SYMTAB. */
b89be57b 20197
9291a0cd
TT
20198static void
20199write_psymbols (struct mapped_symtab *symtab,
987d643c 20200 htab_t psyms_seen,
9291a0cd
TT
20201 struct partial_symbol **psymp,
20202 int count,
987d643c
TT
20203 offset_type cu_index,
20204 int is_static)
9291a0cd
TT
20205{
20206 for (; count-- > 0; ++psymp)
20207 {
156942c7
DE
20208 struct partial_symbol *psym = *psymp;
20209 void **slot;
987d643c 20210
156942c7 20211 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20212 error (_("Ada is not currently supported by the index"));
987d643c 20213
987d643c 20214 /* Only add a given psymbol once. */
156942c7 20215 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20216 if (!*slot)
20217 {
156942c7
DE
20218 gdb_index_symbol_kind kind = symbol_kind (psym);
20219
20220 *slot = psym;
20221 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20222 is_static, kind, cu_index);
987d643c 20223 }
9291a0cd
TT
20224 }
20225}
20226
20227/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20228 exception if there is an error. */
b89be57b 20229
9291a0cd
TT
20230static void
20231write_obstack (FILE *file, struct obstack *obstack)
20232{
20233 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20234 file)
20235 != obstack_object_size (obstack))
20236 error (_("couldn't data write to file"));
20237}
20238
20239/* Unlink a file if the argument is not NULL. */
b89be57b 20240
9291a0cd
TT
20241static void
20242unlink_if_set (void *p)
20243{
20244 char **filename = p;
20245 if (*filename)
20246 unlink (*filename);
20247}
20248
1fd400ff
TT
20249/* A helper struct used when iterating over debug_types. */
20250struct signatured_type_index_data
20251{
20252 struct objfile *objfile;
20253 struct mapped_symtab *symtab;
20254 struct obstack *types_list;
987d643c 20255 htab_t psyms_seen;
1fd400ff
TT
20256 int cu_index;
20257};
20258
20259/* A helper function that writes a single signatured_type to an
20260 obstack. */
b89be57b 20261
1fd400ff
TT
20262static int
20263write_one_signatured_type (void **slot, void *d)
20264{
20265 struct signatured_type_index_data *info = d;
20266 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20267 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20268 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20269 gdb_byte val[8];
20270
20271 write_psymbols (info->symtab,
987d643c 20272 info->psyms_seen,
3e43a32a
MS
20273 info->objfile->global_psymbols.list
20274 + psymtab->globals_offset,
987d643c
TT
20275 psymtab->n_global_syms, info->cu_index,
20276 0);
1fd400ff 20277 write_psymbols (info->symtab,
987d643c 20278 info->psyms_seen,
3e43a32a
MS
20279 info->objfile->static_psymbols.list
20280 + psymtab->statics_offset,
987d643c
TT
20281 psymtab->n_static_syms, info->cu_index,
20282 1);
1fd400ff 20283
b64f50a1
JK
20284 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20285 entry->per_cu.offset.sect_off);
1fd400ff 20286 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20287 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20288 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20289 obstack_grow (info->types_list, val, 8);
20290 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20291 obstack_grow (info->types_list, val, 8);
20292
20293 ++info->cu_index;
20294
20295 return 1;
20296}
20297
95554aad
TT
20298/* Recurse into all "included" dependencies and write their symbols as
20299 if they appeared in this psymtab. */
20300
20301static void
20302recursively_write_psymbols (struct objfile *objfile,
20303 struct partial_symtab *psymtab,
20304 struct mapped_symtab *symtab,
20305 htab_t psyms_seen,
20306 offset_type cu_index)
20307{
20308 int i;
20309
20310 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20311 if (psymtab->dependencies[i]->user != NULL)
20312 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20313 symtab, psyms_seen, cu_index);
20314
20315 write_psymbols (symtab,
20316 psyms_seen,
20317 objfile->global_psymbols.list + psymtab->globals_offset,
20318 psymtab->n_global_syms, cu_index,
20319 0);
20320 write_psymbols (symtab,
20321 psyms_seen,
20322 objfile->static_psymbols.list + psymtab->statics_offset,
20323 psymtab->n_static_syms, cu_index,
20324 1);
20325}
20326
9291a0cd 20327/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20328
9291a0cd
TT
20329static void
20330write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20331{
20332 struct cleanup *cleanup;
20333 char *filename, *cleanup_filename;
1fd400ff
TT
20334 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20335 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20336 int i;
20337 FILE *out_file;
20338 struct mapped_symtab *symtab;
20339 offset_type val, size_of_contents, total_len;
20340 struct stat st;
987d643c 20341 htab_t psyms_seen;
0a5429f6
DE
20342 htab_t cu_index_htab;
20343 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20344
b4f2f049 20345 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20346 return;
b4f2f049 20347
9291a0cd
TT
20348 if (dwarf2_per_objfile->using_index)
20349 error (_("Cannot use an index to create the index"));
20350
8b70b953
TT
20351 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20352 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20353
9291a0cd 20354 if (stat (objfile->name, &st) < 0)
7e17e088 20355 perror_with_name (objfile->name);
9291a0cd
TT
20356
20357 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20358 INDEX_SUFFIX, (char *) NULL);
20359 cleanup = make_cleanup (xfree, filename);
20360
20361 out_file = fopen (filename, "wb");
20362 if (!out_file)
20363 error (_("Can't open `%s' for writing"), filename);
20364
20365 cleanup_filename = filename;
20366 make_cleanup (unlink_if_set, &cleanup_filename);
20367
20368 symtab = create_mapped_symtab ();
20369 make_cleanup (cleanup_mapped_symtab, symtab);
20370
20371 obstack_init (&addr_obstack);
20372 make_cleanup_obstack_free (&addr_obstack);
20373
20374 obstack_init (&cu_list);
20375 make_cleanup_obstack_free (&cu_list);
20376
1fd400ff
TT
20377 obstack_init (&types_cu_list);
20378 make_cleanup_obstack_free (&types_cu_list);
20379
987d643c
TT
20380 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20381 NULL, xcalloc, xfree);
96408a79 20382 make_cleanup_htab_delete (psyms_seen);
987d643c 20383
0a5429f6
DE
20384 /* While we're scanning CU's create a table that maps a psymtab pointer
20385 (which is what addrmap records) to its index (which is what is recorded
20386 in the index file). This will later be needed to write the address
20387 table. */
20388 cu_index_htab = htab_create_alloc (100,
20389 hash_psymtab_cu_index,
20390 eq_psymtab_cu_index,
20391 NULL, xcalloc, xfree);
96408a79 20392 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20393 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20394 xmalloc (sizeof (struct psymtab_cu_index_map)
20395 * dwarf2_per_objfile->n_comp_units);
20396 make_cleanup (xfree, psymtab_cu_index_map);
20397
20398 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20399 work here. Also, the debug_types entries do not appear in
20400 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20401 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20402 {
3e43a32a
MS
20403 struct dwarf2_per_cu_data *per_cu
20404 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20405 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20406 gdb_byte val[8];
0a5429f6
DE
20407 struct psymtab_cu_index_map *map;
20408 void **slot;
9291a0cd 20409
95554aad
TT
20410 if (psymtab->user == NULL)
20411 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20412
0a5429f6
DE
20413 map = &psymtab_cu_index_map[i];
20414 map->psymtab = psymtab;
20415 map->cu_index = i;
20416 slot = htab_find_slot (cu_index_htab, map, INSERT);
20417 gdb_assert (slot != NULL);
20418 gdb_assert (*slot == NULL);
20419 *slot = map;
9291a0cd 20420
b64f50a1
JK
20421 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20422 per_cu->offset.sect_off);
9291a0cd 20423 obstack_grow (&cu_list, val, 8);
e254ef6a 20424 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20425 obstack_grow (&cu_list, val, 8);
20426 }
20427
0a5429f6
DE
20428 /* Dump the address map. */
20429 write_address_map (objfile, &addr_obstack, cu_index_htab);
20430
1fd400ff
TT
20431 /* Write out the .debug_type entries, if any. */
20432 if (dwarf2_per_objfile->signatured_types)
20433 {
20434 struct signatured_type_index_data sig_data;
20435
20436 sig_data.objfile = objfile;
20437 sig_data.symtab = symtab;
20438 sig_data.types_list = &types_cu_list;
987d643c 20439 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20440 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20441 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20442 write_one_signatured_type, &sig_data);
20443 }
20444
156942c7
DE
20445 /* Now that we've processed all symbols we can shrink their cu_indices
20446 lists. */
20447 uniquify_cu_indices (symtab);
20448
9291a0cd
TT
20449 obstack_init (&constant_pool);
20450 make_cleanup_obstack_free (&constant_pool);
20451 obstack_init (&symtab_obstack);
20452 make_cleanup_obstack_free (&symtab_obstack);
20453 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20454
20455 obstack_init (&contents);
20456 make_cleanup_obstack_free (&contents);
1fd400ff 20457 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20458 total_len = size_of_contents;
20459
20460 /* The version number. */
156942c7 20461 val = MAYBE_SWAP (7);
9291a0cd
TT
20462 obstack_grow (&contents, &val, sizeof (val));
20463
20464 /* The offset of the CU list from the start of the file. */
20465 val = MAYBE_SWAP (total_len);
20466 obstack_grow (&contents, &val, sizeof (val));
20467 total_len += obstack_object_size (&cu_list);
20468
1fd400ff
TT
20469 /* The offset of the types CU list from the start of the file. */
20470 val = MAYBE_SWAP (total_len);
20471 obstack_grow (&contents, &val, sizeof (val));
20472 total_len += obstack_object_size (&types_cu_list);
20473
9291a0cd
TT
20474 /* The offset of the address table from the start of the file. */
20475 val = MAYBE_SWAP (total_len);
20476 obstack_grow (&contents, &val, sizeof (val));
20477 total_len += obstack_object_size (&addr_obstack);
20478
20479 /* The offset of the symbol table from the start of the file. */
20480 val = MAYBE_SWAP (total_len);
20481 obstack_grow (&contents, &val, sizeof (val));
20482 total_len += obstack_object_size (&symtab_obstack);
20483
20484 /* The offset of the constant pool from the start of the file. */
20485 val = MAYBE_SWAP (total_len);
20486 obstack_grow (&contents, &val, sizeof (val));
20487 total_len += obstack_object_size (&constant_pool);
20488
20489 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20490
20491 write_obstack (out_file, &contents);
20492 write_obstack (out_file, &cu_list);
1fd400ff 20493 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20494 write_obstack (out_file, &addr_obstack);
20495 write_obstack (out_file, &symtab_obstack);
20496 write_obstack (out_file, &constant_pool);
20497
20498 fclose (out_file);
20499
20500 /* We want to keep the file, so we set cleanup_filename to NULL
20501 here. See unlink_if_set. */
20502 cleanup_filename = NULL;
20503
20504 do_cleanups (cleanup);
20505}
20506
90476074
TT
20507/* Implementation of the `save gdb-index' command.
20508
20509 Note that the file format used by this command is documented in the
20510 GDB manual. Any changes here must be documented there. */
11570e71 20511
9291a0cd
TT
20512static void
20513save_gdb_index_command (char *arg, int from_tty)
20514{
20515 struct objfile *objfile;
20516
20517 if (!arg || !*arg)
96d19272 20518 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20519
20520 ALL_OBJFILES (objfile)
20521 {
20522 struct stat st;
20523
20524 /* If the objfile does not correspond to an actual file, skip it. */
20525 if (stat (objfile->name, &st) < 0)
20526 continue;
20527
20528 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20529 if (dwarf2_per_objfile)
20530 {
20531 volatile struct gdb_exception except;
20532
20533 TRY_CATCH (except, RETURN_MASK_ERROR)
20534 {
20535 write_psymtabs_to_index (objfile, arg);
20536 }
20537 if (except.reason < 0)
20538 exception_fprintf (gdb_stderr, except,
20539 _("Error while writing index for `%s': "),
20540 objfile->name);
20541 }
20542 }
dce234bc
PP
20543}
20544
9291a0cd
TT
20545\f
20546
9eae7c52
TT
20547int dwarf2_always_disassemble;
20548
20549static void
20550show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20551 struct cmd_list_element *c, const char *value)
20552{
3e43a32a
MS
20553 fprintf_filtered (file,
20554 _("Whether to always disassemble "
20555 "DWARF expressions is %s.\n"),
9eae7c52
TT
20556 value);
20557}
20558
900e11f9
JK
20559static void
20560show_check_physname (struct ui_file *file, int from_tty,
20561 struct cmd_list_element *c, const char *value)
20562{
20563 fprintf_filtered (file,
20564 _("Whether to check \"physname\" is %s.\n"),
20565 value);
20566}
20567
6502dd73
DJ
20568void _initialize_dwarf2_read (void);
20569
20570void
20571_initialize_dwarf2_read (void)
20572{
96d19272
JK
20573 struct cmd_list_element *c;
20574
dce234bc 20575 dwarf2_objfile_data_key
c1bd65d0 20576 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20577
1bedd215
AC
20578 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20579Set DWARF 2 specific variables.\n\
20580Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20581 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20582 0/*allow-unknown*/, &maintenance_set_cmdlist);
20583
1bedd215
AC
20584 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20585Show DWARF 2 specific variables\n\
20586Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20587 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20588 0/*allow-unknown*/, &maintenance_show_cmdlist);
20589
20590 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20591 &dwarf2_max_cache_age, _("\
20592Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20593Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20594A higher limit means that cached compilation units will be stored\n\
20595in memory longer, and more total memory will be used. Zero disables\n\
20596caching, which can slow down startup."),
2c5b56ce 20597 NULL,
920d2a44 20598 show_dwarf2_max_cache_age,
2c5b56ce 20599 &set_dwarf2_cmdlist,
ae038cb0 20600 &show_dwarf2_cmdlist);
d97bc12b 20601
9eae7c52
TT
20602 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20603 &dwarf2_always_disassemble, _("\
20604Set whether `info address' always disassembles DWARF expressions."), _("\
20605Show whether `info address' always disassembles DWARF expressions."), _("\
20606When enabled, DWARF expressions are always printed in an assembly-like\n\
20607syntax. When disabled, expressions will be printed in a more\n\
20608conversational style, when possible."),
20609 NULL,
20610 show_dwarf2_always_disassemble,
20611 &set_dwarf2_cmdlist,
20612 &show_dwarf2_cmdlist);
20613
45cfd468
DE
20614 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20615Set debugging of the dwarf2 reader."), _("\
20616Show debugging of the dwarf2 reader."), _("\
20617When enabled, debugging messages are printed during dwarf2 reading\n\
20618and symtab expansion."),
20619 NULL,
20620 NULL,
20621 &setdebuglist, &showdebuglist);
20622
ccce17b0 20623 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20624Set debugging of the dwarf2 DIE reader."), _("\
20625Show debugging of the dwarf2 DIE reader."), _("\
20626When enabled (non-zero), DIEs are dumped after they are read in.\n\
20627The value is the maximum depth to print."),
ccce17b0
YQ
20628 NULL,
20629 NULL,
20630 &setdebuglist, &showdebuglist);
9291a0cd 20631
900e11f9
JK
20632 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20633Set cross-checking of \"physname\" code against demangler."), _("\
20634Show cross-checking of \"physname\" code against demangler."), _("\
20635When enabled, GDB's internal \"physname\" code is checked against\n\
20636the demangler."),
20637 NULL, show_check_physname,
20638 &setdebuglist, &showdebuglist);
20639
e615022a
DE
20640 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20641 no_class, &use_deprecated_index_sections, _("\
20642Set whether to use deprecated gdb_index sections."), _("\
20643Show whether to use deprecated gdb_index sections."), _("\
20644When enabled, deprecated .gdb_index sections are used anyway.\n\
20645Normally they are ignored either because of a missing feature or\n\
20646performance issue.\n\
20647Warning: This option must be enabled before gdb reads the file."),
20648 NULL,
20649 NULL,
20650 &setlist, &showlist);
20651
96d19272 20652 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20653 _("\
fc1a9d6e 20654Save a gdb-index file.\n\
11570e71 20655Usage: save gdb-index DIRECTORY"),
96d19272
JK
20656 &save_cmdlist);
20657 set_cmd_completer (c, filename_completer);
6502dd73 20658}
This page took 2.721572 seconds and 4 git commands to generate.