]> Git Repo - binutils.git/blame - gdb/dwarf2read.c
PR symtab/14442:
[binutils.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 ([email protected]), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
4c2df51b 70
c906108c
SS
71#include <fcntl.h>
72#include "gdb_string.h"
4bdf3d34 73#include "gdb_assert.h"
c906108c 74#include <sys/types.h>
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
45cfd468
DE
79/* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81static int dwarf2_read_debug = 0;
82
d97bc12b 83/* When non-zero, dump DIEs after they are read in. */
ccce17b0 84static unsigned int dwarf2_die_debug = 0;
d97bc12b 85
900e11f9
JK
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
481860b3 89/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 90static int use_deprecated_index_sections = 0;
481860b3 91
df8a16a1
DJ
92/* When set, the file that we're processing is known to have debugging
93 info for C++ namespaces. GCC 3.3.x did not produce this information,
94 but later versions do. */
95
96static int processing_has_namespace_info;
97
6502dd73
DJ
98static const struct objfile_data *dwarf2_objfile_data_key;
99
dce234bc
PP
100struct dwarf2_section_info
101{
102 asection *asection;
103 gdb_byte *buffer;
104 bfd_size_type size;
be391dca
TT
105 /* True if we have tried to read this section. */
106 int readin;
dce234bc
PP
107};
108
8b70b953
TT
109typedef struct dwarf2_section_info dwarf2_section_info_def;
110DEF_VEC_O (dwarf2_section_info_def);
111
9291a0cd
TT
112/* All offsets in the index are of this type. It must be
113 architecture-independent. */
114typedef uint32_t offset_type;
115
116DEF_VEC_I (offset_type);
117
156942c7
DE
118/* Ensure only legit values are used. */
119#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
120 do { \
121 gdb_assert ((unsigned int) (value) <= 1); \
122 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
123 } while (0)
124
125/* Ensure only legit values are used. */
126#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
127 do { \
128 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
129 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
130 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133/* Ensure we don't use more than the alloted nuber of bits for the CU. */
134#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
137 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
138 } while (0)
139
9291a0cd
TT
140/* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142struct mapped_index
143{
559a7a62
JK
144 /* Index data format version. */
145 int version;
146
9291a0cd
TT
147 /* The total length of the buffer. */
148 off_t total_size;
b11b1f88 149
9291a0cd
TT
150 /* A pointer to the address table data. */
151 const gdb_byte *address_table;
b11b1f88 152
9291a0cd
TT
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
b11b1f88 155
3876f04e
DE
156 /* The symbol table, implemented as a hash table. */
157 const offset_type *symbol_table;
b11b1f88 158
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
b11b1f88 161
9291a0cd
TT
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164};
165
95554aad
TT
166typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
167DEF_VEC_P (dwarf2_per_cu_ptr);
168
9cdd5dbd
DE
169/* Collection of data recorded per objfile.
170 This hangs off of dwarf2_objfile_data_key. */
171
6502dd73
DJ
172struct dwarf2_per_objfile
173{
dce234bc
PP
174 struct dwarf2_section_info info;
175 struct dwarf2_section_info abbrev;
176 struct dwarf2_section_info line;
dce234bc
PP
177 struct dwarf2_section_info loc;
178 struct dwarf2_section_info macinfo;
cf2c3c16 179 struct dwarf2_section_info macro;
dce234bc
PP
180 struct dwarf2_section_info str;
181 struct dwarf2_section_info ranges;
3019eac3 182 struct dwarf2_section_info addr;
dce234bc
PP
183 struct dwarf2_section_info frame;
184 struct dwarf2_section_info eh_frame;
9291a0cd 185 struct dwarf2_section_info gdb_index;
ae038cb0 186
8b70b953
TT
187 VEC (dwarf2_section_info_def) *types;
188
be391dca
TT
189 /* Back link. */
190 struct objfile *objfile;
191
d467dd73 192 /* Table of all the compilation units. This is used to locate
10b3939b 193 the target compilation unit of a particular reference. */
ae038cb0
DJ
194 struct dwarf2_per_cu_data **all_comp_units;
195
196 /* The number of compilation units in ALL_COMP_UNITS. */
197 int n_comp_units;
198
1fd400ff 199 /* The number of .debug_types-related CUs. */
d467dd73 200 int n_type_units;
1fd400ff 201
d467dd73 202 /* The .debug_types-related CUs (TUs). */
b4dd5633 203 struct signatured_type **all_type_units;
1fd400ff 204
f4dc4d17
DE
205 /* The number of entries in all_type_unit_groups. */
206 int n_type_unit_groups;
207
208 /* Table of type unit groups.
209 This exists to make it easy to iterate over all CUs and TU groups. */
210 struct type_unit_group **all_type_unit_groups;
211
212 /* Table of struct type_unit_group objects.
213 The hash key is the DW_AT_stmt_list value. */
214 htab_t type_unit_groups;
72dca2f5 215
348e048f
DE
216 /* A table mapping .debug_types signatures to its signatured_type entry.
217 This is NULL if the .debug_types section hasn't been read in yet. */
218 htab_t signatured_types;
219
f4dc4d17
DE
220 /* Type unit statistics, to see how well the scaling improvements
221 are doing. */
222 struct tu_stats
223 {
224 int nr_uniq_abbrev_tables;
225 int nr_symtabs;
226 int nr_symtab_sharers;
227 int nr_stmt_less_type_units;
228 } tu_stats;
229
230 /* A chain of compilation units that are currently read in, so that
231 they can be freed later. */
232 struct dwarf2_per_cu_data *read_in_chain;
233
3019eac3
DE
234 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
235 This is NULL if the table hasn't been allocated yet. */
236 htab_t dwo_files;
237
80626a55
DE
238 /* Non-zero if we've check for whether there is a DWP file. */
239 int dwp_checked;
240
241 /* The DWP file if there is one, or NULL. */
242 struct dwp_file *dwp_file;
243
36586728
TT
244 /* The shared '.dwz' file, if one exists. This is used when the
245 original data was compressed using 'dwz -m'. */
246 struct dwz_file *dwz_file;
247
72dca2f5
FR
248 /* A flag indicating wether this objfile has a section loaded at a
249 VMA of 0. */
250 int has_section_at_zero;
9291a0cd 251
ae2de4f8
DE
252 /* True if we are using the mapped index,
253 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
254 unsigned char using_index;
255
ae2de4f8 256 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 257 struct mapped_index *index_table;
98bfdba5 258
7b9f3c50 259 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
260 TUs typically share line table entries with a CU, so we maintain a
261 separate table of all line table entries to support the sharing.
262 Note that while there can be way more TUs than CUs, we've already
263 sorted all the TUs into "type unit groups", grouped by their
264 DW_AT_stmt_list value. Therefore the only sharing done here is with a
265 CU and its associated TU group if there is one. */
7b9f3c50
DE
266 htab_t quick_file_names_table;
267
98bfdba5
PA
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
673bfd45 271
dee91e82 272 /* Table mapping type DIEs to their struct type *.
673bfd45 273 This is NULL if not allocated yet.
dee91e82
DE
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
95554aad
TT
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
279};
280
281static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 282
251d32d9 283/* Default names of the debugging sections. */
c906108c 284
233a11ab
CS
285/* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
9cdd5dbd
DE
288static const struct dwarf2_debug_sections dwarf2_elf_names =
289{
251d32d9
TG
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 295 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
3019eac3 299 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
24d3216f
TT
302 { ".gdb_index", ".zgdb_index" },
303 23
251d32d9 304};
c906108c 305
80626a55 306/* List of DWO/DWP sections. */
3019eac3 307
80626a55 308static const struct dwop_section_names
3019eac3
DE
309{
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
09262596
DE
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
3019eac3
DE
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
80626a55
DE
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
3019eac3 321}
80626a55 322dwop_section_names =
3019eac3
DE
323{
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
335};
336
c906108c
SS
337/* local data types */
338
107d2387
AC
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
c906108c 341struct comp_unit_head
a738430d 342{
c764a876 343 unsigned int length;
a738430d 344 short version;
a738430d
MK
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
b64f50a1 347 sect_offset abbrev_offset;
57349743 348
a738430d
MK
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
57349743 351
a738430d
MK
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
57349743 354
a738430d
MK
355 /* Offset to the first byte of this compilation unit header in the
356 .debug_info section, for resolving relative reference dies. */
b64f50a1 357 sect_offset offset;
57349743 358
d00adf39
DE
359 /* Offset to first die in this cu from the start of the cu.
360 This will be the first byte following the compilation unit header. */
b64f50a1 361 cu_offset first_die_offset;
a738430d 362};
c906108c 363
3da10d80
KS
364/* Type used for delaying computation of method physnames.
365 See comments for compute_delayed_physnames. */
366struct delayed_method_info
367{
368 /* The type to which the method is attached, i.e., its parent class. */
369 struct type *type;
370
371 /* The index of the method in the type's function fieldlists. */
372 int fnfield_index;
373
374 /* The index of the method in the fieldlist. */
375 int index;
376
377 /* The name of the DIE. */
378 const char *name;
379
380 /* The DIE associated with this method. */
381 struct die_info *die;
382};
383
384typedef struct delayed_method_info delayed_method_info;
385DEF_VEC_O (delayed_method_info);
386
e7c27a73
DJ
387/* Internal state when decoding a particular compilation unit. */
388struct dwarf2_cu
389{
390 /* The objfile containing this compilation unit. */
391 struct objfile *objfile;
392
d00adf39 393 /* The header of the compilation unit. */
e7c27a73 394 struct comp_unit_head header;
e142c38c 395
d00adf39
DE
396 /* Base address of this compilation unit. */
397 CORE_ADDR base_address;
398
399 /* Non-zero if base_address has been set. */
400 int base_known;
401
e142c38c
DJ
402 /* The language we are debugging. */
403 enum language language;
404 const struct language_defn *language_defn;
405
b0f35d58
DL
406 const char *producer;
407
e142c38c
DJ
408 /* The generic symbol table building routines have separate lists for
409 file scope symbols and all all other scopes (local scopes). So
410 we need to select the right one to pass to add_symbol_to_list().
411 We do it by keeping a pointer to the correct list in list_in_scope.
412
413 FIXME: The original dwarf code just treated the file scope as the
414 first local scope, and all other local scopes as nested local
415 scopes, and worked fine. Check to see if we really need to
416 distinguish these in buildsym.c. */
417 struct pending **list_in_scope;
418
433df2d4
DE
419 /* The abbrev table for this CU.
420 Normally this points to the abbrev table in the objfile.
421 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
422 struct abbrev_table *abbrev_table;
72bf9492 423
b64f50a1
JK
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
426 htab_t partial_dies;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 struct obstack comp_unit_obstack;
431
ae038cb0
DJ
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain;
437
438 /* Backchain to our per_cu entry if the tree has been built. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used;
443
b64f50a1
JK
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
51545339 446 htab_t die_hash;
10b3939b
DJ
447
448 /* Full DIEs if read in. */
449 struct die_info *dies;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies;
455
cb1df416
DJ
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header;
458
3da10d80
KS
459 /* A list of methods which need to have physnames computed
460 after all type information has been read. */
461 VEC (delayed_method_info) *method_list;
462
96408a79
SA
463 /* To be copied to symtab->call_site_htab. */
464 htab_t call_site_htab;
465
034e5797
DE
466 /* Non-NULL if this CU came from a DWO file.
467 There is an invariant here that is important to remember:
468 Except for attributes copied from the top level DIE in the "main"
469 (or "stub") file in preparation for reading the DWO file
470 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
471 Either there isn't a DWO file (in which case this is NULL and the point
472 is moot), or there is and either we're not going to read it (in which
473 case this is NULL) or there is and we are reading it (in which case this
474 is non-NULL). */
3019eac3
DE
475 struct dwo_unit *dwo_unit;
476
477 /* The DW_AT_addr_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE. */
480 ULONGEST addr_base;
481
2e3cf129
DE
482 /* The DW_AT_ranges_base attribute if present, zero otherwise
483 (zero is a valid value though).
484 Note this value comes from the stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
486 be used without needing to know whether DWO files are in use or not.
487 N.B. This does not apply to DW_AT_ranges appearing in
488 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
489 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
490 DW_AT_ranges_base *would* have to be applied, and we'd have to care
491 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
492 ULONGEST ranges_base;
493
ae038cb0
DJ
494 /* Mark used when releasing cached dies. */
495 unsigned int mark : 1;
496
8be455d7
JK
497 /* This CU references .debug_loc. See the symtab->locations_valid field.
498 This test is imperfect as there may exist optimized debug code not using
499 any location list and still facing inlining issues if handled as
500 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 501 unsigned int has_loclist : 1;
ba919b58 502
1b80a9fa
JK
503 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
504 if all the producer_is_* fields are valid. This information is cached
505 because profiling CU expansion showed excessive time spent in
506 producer_is_gxx_lt_4_6. */
ba919b58
TT
507 unsigned int checked_producer : 1;
508 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 509 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 510 unsigned int producer_is_icc : 1;
e7c27a73
DJ
511};
512
10b3939b
DJ
513/* Persistent data held for a compilation unit, even when not
514 processing it. We put a pointer to this structure in the
28dee7f5 515 read_symtab_private field of the psymtab. */
10b3939b 516
ae038cb0
DJ
517struct dwarf2_per_cu_data
518{
36586728 519 /* The start offset and length of this compilation unit.
45452591 520 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
521 initial_length_size.
522 If the DIE refers to a DWO file, this is always of the original die,
523 not the DWO file. */
b64f50a1 524 sect_offset offset;
36586728 525 unsigned int length;
ae038cb0
DJ
526
527 /* Flag indicating this compilation unit will be read in before
528 any of the current compilation units are processed. */
c764a876 529 unsigned int queued : 1;
ae038cb0 530
0d99eb77
DE
531 /* This flag will be set when reading partial DIEs if we need to load
532 absolutely all DIEs for this compilation unit, instead of just the ones
533 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
534 hash table and don't find it. */
535 unsigned int load_all_dies : 1;
536
3019eac3
DE
537 /* Non-zero if this CU is from .debug_types. */
538 unsigned int is_debug_types : 1;
539
36586728
TT
540 /* Non-zero if this CU is from the .dwz file. */
541 unsigned int is_dwz : 1;
542
3019eac3
DE
543 /* The section this CU/TU lives in.
544 If the DIE refers to a DWO file, this is always the original die,
545 not the DWO file. */
546 struct dwarf2_section_info *info_or_types_section;
348e048f 547
17ea53c3
JK
548 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
549 of the CU cache it gets reset to NULL again. */
ae038cb0 550 struct dwarf2_cu *cu;
1c379e20 551
9cdd5dbd
DE
552 /* The corresponding objfile.
553 Normally we can get the objfile from dwarf2_per_objfile.
554 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
555 struct objfile *objfile;
556
557 /* When using partial symbol tables, the 'psymtab' field is active.
558 Otherwise the 'quick' field is active. */
559 union
560 {
561 /* The partial symbol table associated with this compilation unit,
95554aad 562 or NULL for unread partial units. */
9291a0cd
TT
563 struct partial_symtab *psymtab;
564
565 /* Data needed by the "quick" functions. */
566 struct dwarf2_per_cu_quick_data *quick;
567 } v;
95554aad 568
f4dc4d17
DE
569 union
570 {
571 /* The CUs we import using DW_TAG_imported_unit. This is filled in
572 while reading psymtabs, used to compute the psymtab dependencies,
573 and then cleared. Then it is filled in again while reading full
574 symbols, and only deleted when the objfile is destroyed. */
575 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
576
577 /* Type units are grouped by their DW_AT_stmt_list entry so that they
578 can share them. If this is a TU, this points to the containing
579 symtab. */
580 struct type_unit_group *type_unit_group;
581 } s;
ae038cb0
DJ
582};
583
348e048f
DE
584/* Entry in the signatured_types hash table. */
585
586struct signatured_type
587{
42e7ad6c
DE
588 /* The "per_cu" object of this type.
589 N.B.: This is the first member so that it's easy to convert pointers
590 between them. */
591 struct dwarf2_per_cu_data per_cu;
592
3019eac3 593 /* The type's signature. */
348e048f
DE
594 ULONGEST signature;
595
3019eac3
DE
596 /* Offset in the TU of the type's DIE, as read from the TU header.
597 If the definition lives in a DWO file, this value is unusable. */
598 cu_offset type_offset_in_tu;
599
600 /* Offset in the section of the type's DIE.
601 If the definition lives in a DWO file, this is the offset in the
602 .debug_types.dwo section.
603 The value is zero until the actual value is known.
604 Zero is otherwise not a valid section offset. */
605 sect_offset type_offset_in_section;
348e048f
DE
606};
607
094b34ac
DE
608/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
609 This includes type_unit_group and quick_file_names. */
610
611struct stmt_list_hash
612{
613 /* The DWO unit this table is from or NULL if there is none. */
614 struct dwo_unit *dwo_unit;
615
616 /* Offset in .debug_line or .debug_line.dwo. */
617 sect_offset line_offset;
618};
619
f4dc4d17
DE
620/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
621 an object of this type. */
622
623struct type_unit_group
624{
625 /* dwarf2read.c's main "handle" on the symtab.
626 To simplify things we create an artificial CU that "includes" all the
627 type units using this stmt_list so that the rest of the code still has
628 a "per_cu" handle on the symtab.
629 This PER_CU is recognized by having no section. */
630#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
631 struct dwarf2_per_cu_data per_cu;
632
633 union
634 {
635 /* The TUs that share this DW_AT_stmt_list entry.
636 This is added to while parsing type units to build partial symtabs,
637 and is deleted afterwards and not used again. */
638 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 639
094b34ac
DE
640 /* When reading the line table in "quick" functions, we need a real TU.
641 Any will do, we know they all share the same DW_AT_stmt_list entry.
642 For simplicity's sake, we pick the first one. */
643 struct dwarf2_per_cu_data *first_tu;
644 } t;
f4dc4d17
DE
645
646 /* The primary symtab.
094b34ac
DE
647 Type units in a group needn't all be defined in the same source file,
648 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
649 struct symtab *primary_symtab;
650
094b34ac
DE
651 /* The data used to construct the hash key. */
652 struct stmt_list_hash hash;
f4dc4d17
DE
653
654 /* The number of symtabs from the line header.
655 The value here must match line_header.num_file_names. */
656 unsigned int num_symtabs;
657
658 /* The symbol tables for this TU (obtained from the files listed in
659 DW_AT_stmt_list).
660 WARNING: The order of entries here must match the order of entries
661 in the line header. After the first TU using this type_unit_group, the
662 line header for the subsequent TUs is recreated from this. This is done
663 because we need to use the same symtabs for each TU using the same
664 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
665 there's no guarantee the line header doesn't have duplicate entries. */
666 struct symtab **symtabs;
667};
668
80626a55 669/* These sections are what may appear in a DWO file. */
3019eac3
DE
670
671struct dwo_sections
672{
673 struct dwarf2_section_info abbrev;
3019eac3
DE
674 struct dwarf2_section_info line;
675 struct dwarf2_section_info loc;
09262596
DE
676 struct dwarf2_section_info macinfo;
677 struct dwarf2_section_info macro;
3019eac3
DE
678 struct dwarf2_section_info str;
679 struct dwarf2_section_info str_offsets;
80626a55
DE
680 /* In the case of a virtual DWO file, these two are unused. */
681 struct dwarf2_section_info info;
3019eac3
DE
682 VEC (dwarf2_section_info_def) *types;
683};
684
685/* Common bits of DWO CUs/TUs. */
686
687struct dwo_unit
688{
689 /* Backlink to the containing struct dwo_file. */
690 struct dwo_file *dwo_file;
691
692 /* The "id" that distinguishes this CU/TU.
693 .debug_info calls this "dwo_id", .debug_types calls this "signature".
694 Since signatures came first, we stick with it for consistency. */
695 ULONGEST signature;
696
697 /* The section this CU/TU lives in, in the DWO file. */
698 struct dwarf2_section_info *info_or_types_section;
699
700 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
701 sect_offset offset;
702 unsigned int length;
703
704 /* For types, offset in the type's DIE of the type defined by this TU. */
705 cu_offset type_offset_in_tu;
706};
707
80626a55
DE
708/* Data for one DWO file.
709 This includes virtual DWO files that have been packaged into a
710 DWP file. */
3019eac3
DE
711
712struct dwo_file
713{
80626a55
DE
714 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
715 For virtual DWO files the name is constructed from the section offsets
716 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
717 from related CU+TUs. */
718 const char *name;
3019eac3 719
80626a55
DE
720 /* The bfd, when the file is open. Otherwise this is NULL.
721 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
722 bfd *dbfd;
3019eac3
DE
723
724 /* Section info for this file. */
725 struct dwo_sections sections;
726
727 /* Table of CUs in the file.
728 Each element is a struct dwo_unit. */
729 htab_t cus;
730
731 /* Table of TUs in the file.
732 Each element is a struct dwo_unit. */
733 htab_t tus;
734};
735
80626a55
DE
736/* These sections are what may appear in a DWP file. */
737
738struct dwp_sections
739{
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
744 by section number. We don't need to record them here. */
745};
746
747/* These sections are what may appear in a virtual DWO file. */
748
749struct virtual_dwo_sections
750{
751 struct dwarf2_section_info abbrev;
752 struct dwarf2_section_info line;
753 struct dwarf2_section_info loc;
754 struct dwarf2_section_info macinfo;
755 struct dwarf2_section_info macro;
756 struct dwarf2_section_info str_offsets;
757 /* Each DWP hash table entry records one CU or one TU.
758 That is recorded here, and copied to dwo_unit.info_or_types_section. */
759 struct dwarf2_section_info info_or_types;
760};
761
762/* Contents of DWP hash tables. */
763
764struct dwp_hash_table
765{
766 uint32_t nr_units, nr_slots;
767 const gdb_byte *hash_table, *unit_table, *section_pool;
768};
769
770/* Data for one DWP file. */
771
772struct dwp_file
773{
774 /* Name of the file. */
775 const char *name;
776
777 /* The bfd, when the file is open. Otherwise this is NULL. */
778 bfd *dbfd;
779
780 /* Section info for this file. */
781 struct dwp_sections sections;
782
783 /* Table of CUs in the file. */
784 const struct dwp_hash_table *cus;
785
786 /* Table of TUs in the file. */
787 const struct dwp_hash_table *tus;
788
789 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
790 htab_t loaded_cutus;
791
792 /* Table to map ELF section numbers to their sections. */
793 unsigned int num_sections;
794 asection **elf_sections;
795};
796
36586728
TT
797/* This represents a '.dwz' file. */
798
799struct dwz_file
800{
801 /* A dwz file can only contain a few sections. */
802 struct dwarf2_section_info abbrev;
803 struct dwarf2_section_info info;
804 struct dwarf2_section_info str;
805 struct dwarf2_section_info line;
806 struct dwarf2_section_info macro;
2ec9a5e0 807 struct dwarf2_section_info gdb_index;
36586728
TT
808
809 /* The dwz's BFD. */
810 bfd *dwz_bfd;
811};
812
0963b4bd
MS
813/* Struct used to pass misc. parameters to read_die_and_children, et
814 al. which are used for both .debug_info and .debug_types dies.
815 All parameters here are unchanging for the life of the call. This
dee91e82 816 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
817
818struct die_reader_specs
819{
dee91e82 820 /* die_section->asection->owner. */
93311388
DE
821 bfd* abfd;
822
823 /* The CU of the DIE we are parsing. */
824 struct dwarf2_cu *cu;
825
80626a55 826 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
827 struct dwo_file *dwo_file;
828
dee91e82 829 /* The section the die comes from.
3019eac3 830 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
831 struct dwarf2_section_info *die_section;
832
833 /* die_section->buffer. */
834 gdb_byte *buffer;
f664829e
DE
835
836 /* The end of the buffer. */
837 const gdb_byte *buffer_end;
93311388
DE
838};
839
fd820528 840/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
841typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
842 gdb_byte *info_ptr,
843 struct die_info *comp_unit_die,
844 int has_children,
845 void *data);
846
debd256d
JB
847/* The line number information for a compilation unit (found in the
848 .debug_line section) begins with a "statement program header",
849 which contains the following information. */
850struct line_header
851{
852 unsigned int total_length;
853 unsigned short version;
854 unsigned int header_length;
855 unsigned char minimum_instruction_length;
2dc7f7b3 856 unsigned char maximum_ops_per_instruction;
debd256d
JB
857 unsigned char default_is_stmt;
858 int line_base;
859 unsigned char line_range;
860 unsigned char opcode_base;
861
862 /* standard_opcode_lengths[i] is the number of operands for the
863 standard opcode whose value is i. This means that
864 standard_opcode_lengths[0] is unused, and the last meaningful
865 element is standard_opcode_lengths[opcode_base - 1]. */
866 unsigned char *standard_opcode_lengths;
867
868 /* The include_directories table. NOTE! These strings are not
869 allocated with xmalloc; instead, they are pointers into
870 debug_line_buffer. If you try to free them, `free' will get
871 indigestion. */
872 unsigned int num_include_dirs, include_dirs_size;
873 char **include_dirs;
874
875 /* The file_names table. NOTE! These strings are not allocated
876 with xmalloc; instead, they are pointers into debug_line_buffer.
877 Don't try to free them directly. */
878 unsigned int num_file_names, file_names_size;
879 struct file_entry
c906108c 880 {
debd256d
JB
881 char *name;
882 unsigned int dir_index;
883 unsigned int mod_time;
884 unsigned int length;
aaa75496 885 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 886 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
887 } *file_names;
888
889 /* The start and end of the statement program following this
6502dd73 890 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 891 gdb_byte *statement_program_start, *statement_program_end;
debd256d 892};
c906108c
SS
893
894/* When we construct a partial symbol table entry we only
0963b4bd 895 need this much information. */
c906108c
SS
896struct partial_die_info
897 {
72bf9492 898 /* Offset of this DIE. */
b64f50a1 899 sect_offset offset;
72bf9492
DJ
900
901 /* DWARF-2 tag for this DIE. */
902 ENUM_BITFIELD(dwarf_tag) tag : 16;
903
72bf9492
DJ
904 /* Assorted flags describing the data found in this DIE. */
905 unsigned int has_children : 1;
906 unsigned int is_external : 1;
907 unsigned int is_declaration : 1;
908 unsigned int has_type : 1;
909 unsigned int has_specification : 1;
910 unsigned int has_pc_info : 1;
481860b3 911 unsigned int may_be_inlined : 1;
72bf9492
DJ
912
913 /* Flag set if the SCOPE field of this structure has been
914 computed. */
915 unsigned int scope_set : 1;
916
fa4028e9
JB
917 /* Flag set if the DIE has a byte_size attribute. */
918 unsigned int has_byte_size : 1;
919
98bfdba5
PA
920 /* Flag set if any of the DIE's children are template arguments. */
921 unsigned int has_template_arguments : 1;
922
abc72ce4
DE
923 /* Flag set if fixup_partial_die has been called on this die. */
924 unsigned int fixup_called : 1;
925
36586728
TT
926 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
927 unsigned int is_dwz : 1;
928
929 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
930 unsigned int spec_is_dwz : 1;
931
72bf9492 932 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 933 sometimes a default name for unnamed DIEs. */
c906108c 934 char *name;
72bf9492 935
abc72ce4
DE
936 /* The linkage name, if present. */
937 const char *linkage_name;
938
72bf9492
DJ
939 /* The scope to prepend to our children. This is generally
940 allocated on the comp_unit_obstack, so will disappear
941 when this compilation unit leaves the cache. */
942 char *scope;
943
95554aad
TT
944 /* Some data associated with the partial DIE. The tag determines
945 which field is live. */
946 union
947 {
948 /* The location description associated with this DIE, if any. */
949 struct dwarf_block *locdesc;
950 /* The offset of an import, for DW_TAG_imported_unit. */
951 sect_offset offset;
952 } d;
72bf9492
DJ
953
954 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
955 CORE_ADDR lowpc;
956 CORE_ADDR highpc;
72bf9492 957
93311388 958 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 959 DW_AT_sibling, if any. */
abc72ce4
DE
960 /* NOTE: This member isn't strictly necessary, read_partial_die could
961 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 962 gdb_byte *sibling;
72bf9492
DJ
963
964 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
965 DW_AT_specification (or DW_AT_abstract_origin or
966 DW_AT_extension). */
b64f50a1 967 sect_offset spec_offset;
72bf9492
DJ
968
969 /* Pointers to this DIE's parent, first child, and next sibling,
970 if any. */
971 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
972 };
973
0963b4bd 974/* This data structure holds the information of an abbrev. */
c906108c
SS
975struct abbrev_info
976 {
977 unsigned int number; /* number identifying abbrev */
978 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
979 unsigned short has_children; /* boolean */
980 unsigned short num_attrs; /* number of attributes */
c906108c
SS
981 struct attr_abbrev *attrs; /* an array of attribute descriptions */
982 struct abbrev_info *next; /* next in chain */
983 };
984
985struct attr_abbrev
986 {
9d25dd43
DE
987 ENUM_BITFIELD(dwarf_attribute) name : 16;
988 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
989 };
990
433df2d4
DE
991/* Size of abbrev_table.abbrev_hash_table. */
992#define ABBREV_HASH_SIZE 121
993
994/* Top level data structure to contain an abbreviation table. */
995
996struct abbrev_table
997{
f4dc4d17
DE
998 /* Where the abbrev table came from.
999 This is used as a sanity check when the table is used. */
433df2d4
DE
1000 sect_offset offset;
1001
1002 /* Storage for the abbrev table. */
1003 struct obstack abbrev_obstack;
1004
1005 /* Hash table of abbrevs.
1006 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1007 It could be statically allocated, but the previous code didn't so we
1008 don't either. */
1009 struct abbrev_info **abbrevs;
1010};
1011
0963b4bd 1012/* Attributes have a name and a value. */
b60c80d6
DJ
1013struct attribute
1014 {
9d25dd43 1015 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1016 ENUM_BITFIELD(dwarf_form) form : 15;
1017
1018 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1019 field should be in u.str (existing only for DW_STRING) but it is kept
1020 here for better struct attribute alignment. */
1021 unsigned int string_is_canonical : 1;
1022
b60c80d6
DJ
1023 union
1024 {
1025 char *str;
1026 struct dwarf_block *blk;
43bbcdc2
PH
1027 ULONGEST unsnd;
1028 LONGEST snd;
b60c80d6 1029 CORE_ADDR addr;
348e048f 1030 struct signatured_type *signatured_type;
b60c80d6
DJ
1031 }
1032 u;
1033 };
1034
0963b4bd 1035/* This data structure holds a complete die structure. */
c906108c
SS
1036struct die_info
1037 {
76815b17
DE
1038 /* DWARF-2 tag for this DIE. */
1039 ENUM_BITFIELD(dwarf_tag) tag : 16;
1040
1041 /* Number of attributes */
98bfdba5
PA
1042 unsigned char num_attrs;
1043
1044 /* True if we're presently building the full type name for the
1045 type derived from this DIE. */
1046 unsigned char building_fullname : 1;
76815b17
DE
1047
1048 /* Abbrev number */
1049 unsigned int abbrev;
1050
93311388 1051 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1052 sect_offset offset;
78ba4af6
JB
1053
1054 /* The dies in a compilation unit form an n-ary tree. PARENT
1055 points to this die's parent; CHILD points to the first child of
1056 this node; and all the children of a given node are chained
4950bc1c 1057 together via their SIBLING fields. */
639d11d3
DC
1058 struct die_info *child; /* Its first child, if any. */
1059 struct die_info *sibling; /* Its next sibling, if any. */
1060 struct die_info *parent; /* Its parent, if any. */
c906108c 1061
b60c80d6
DJ
1062 /* An array of attributes, with NUM_ATTRS elements. There may be
1063 zero, but it's not common and zero-sized arrays are not
1064 sufficiently portable C. */
1065 struct attribute attrs[1];
c906108c
SS
1066 };
1067
0963b4bd 1068/* Get at parts of an attribute structure. */
c906108c
SS
1069
1070#define DW_STRING(attr) ((attr)->u.str)
8285870a 1071#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1072#define DW_UNSND(attr) ((attr)->u.unsnd)
1073#define DW_BLOCK(attr) ((attr)->u.blk)
1074#define DW_SND(attr) ((attr)->u.snd)
1075#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1076#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1077
0963b4bd 1078/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1079struct dwarf_block
1080 {
56eb65bd 1081 size_t size;
1d6edc3c
JK
1082
1083 /* Valid only if SIZE is not zero. */
fe1b8b76 1084 gdb_byte *data;
c906108c
SS
1085 };
1086
c906108c
SS
1087#ifndef ATTR_ALLOC_CHUNK
1088#define ATTR_ALLOC_CHUNK 4
1089#endif
1090
c906108c
SS
1091/* Allocate fields for structs, unions and enums in this size. */
1092#ifndef DW_FIELD_ALLOC_CHUNK
1093#define DW_FIELD_ALLOC_CHUNK 4
1094#endif
1095
c906108c
SS
1096/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099static int bits_per_byte = 8;
1100
1101/* The routines that read and process dies for a C struct or C++ class
1102 pass lists of data member fields and lists of member function fields
1103 in an instance of a field_info structure, as defined below. */
1104struct field_info
c5aa993b 1105 {
0963b4bd 1106 /* List of data member and baseclasses fields. */
c5aa993b
JM
1107 struct nextfield
1108 {
1109 struct nextfield *next;
1110 int accessibility;
1111 int virtuality;
1112 struct field field;
1113 }
7d0ccb61 1114 *fields, *baseclasses;
c906108c 1115
7d0ccb61 1116 /* Number of fields (including baseclasses). */
c5aa993b 1117 int nfields;
c906108c 1118
c5aa993b
JM
1119 /* Number of baseclasses. */
1120 int nbaseclasses;
c906108c 1121
c5aa993b
JM
1122 /* Set if the accesibility of one of the fields is not public. */
1123 int non_public_fields;
c906108c 1124
c5aa993b
JM
1125 /* Member function fields array, entries are allocated in the order they
1126 are encountered in the object file. */
1127 struct nextfnfield
1128 {
1129 struct nextfnfield *next;
1130 struct fn_field fnfield;
1131 }
1132 *fnfields;
c906108c 1133
c5aa993b
JM
1134 /* Member function fieldlist array, contains name of possibly overloaded
1135 member function, number of overloaded member functions and a pointer
1136 to the head of the member function field chain. */
1137 struct fnfieldlist
1138 {
1139 char *name;
1140 int length;
1141 struct nextfnfield *head;
1142 }
1143 *fnfieldlists;
c906108c 1144
c5aa993b
JM
1145 /* Number of entries in the fnfieldlists array. */
1146 int nfnfields;
98751a41
JK
1147
1148 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1149 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1150 struct typedef_field_list
1151 {
1152 struct typedef_field field;
1153 struct typedef_field_list *next;
1154 }
1155 *typedef_field_list;
1156 unsigned typedef_field_list_count;
c5aa993b 1157 };
c906108c 1158
10b3939b
DJ
1159/* One item on the queue of compilation units to read in full symbols
1160 for. */
1161struct dwarf2_queue_item
1162{
1163 struct dwarf2_per_cu_data *per_cu;
95554aad 1164 enum language pretend_language;
10b3939b
DJ
1165 struct dwarf2_queue_item *next;
1166};
1167
1168/* The current queue. */
1169static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1170
ae038cb0
DJ
1171/* Loaded secondary compilation units are kept in memory until they
1172 have not been referenced for the processing of this many
1173 compilation units. Set this to zero to disable caching. Cache
1174 sizes of up to at least twenty will improve startup time for
1175 typical inter-CU-reference binaries, at an obvious memory cost. */
1176static int dwarf2_max_cache_age = 5;
920d2a44
AC
1177static void
1178show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1179 struct cmd_list_element *c, const char *value)
1180{
3e43a32a
MS
1181 fprintf_filtered (file, _("The upper bound on the age of cached "
1182 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1183 value);
1184}
1185
ae038cb0 1186
0963b4bd 1187/* Various complaints about symbol reading that don't abort the process. */
c906108c 1188
4d3c2250
KB
1189static void
1190dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1191{
4d3c2250 1192 complaint (&symfile_complaints,
e2e0b3e5 1193 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1194}
1195
25e43795
DJ
1196static void
1197dwarf2_debug_line_missing_file_complaint (void)
1198{
1199 complaint (&symfile_complaints,
1200 _(".debug_line section has line data without a file"));
1201}
1202
59205f5a
JB
1203static void
1204dwarf2_debug_line_missing_end_sequence_complaint (void)
1205{
1206 complaint (&symfile_complaints,
3e43a32a
MS
1207 _(".debug_line section has line "
1208 "program sequence without an end"));
59205f5a
JB
1209}
1210
4d3c2250
KB
1211static void
1212dwarf2_complex_location_expr_complaint (void)
2e276125 1213{
e2e0b3e5 1214 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1215}
1216
4d3c2250
KB
1217static void
1218dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1219 int arg3)
2e276125 1220{
4d3c2250 1221 complaint (&symfile_complaints,
3e43a32a
MS
1222 _("const value length mismatch for '%s', got %d, expected %d"),
1223 arg1, arg2, arg3);
4d3c2250
KB
1224}
1225
1226static void
f664829e 1227dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1228{
4d3c2250 1229 complaint (&symfile_complaints,
f664829e
DE
1230 _("debug info runs off end of %s section"
1231 " [in module %s]"),
1232 section->asection->name,
1233 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1234}
1235
1236static void
1237dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1238{
4d3c2250 1239 complaint (&symfile_complaints,
3e43a32a
MS
1240 _("macro debug info contains a "
1241 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1242 arg1);
1243}
1244
1245static void
1246dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1247{
4d3c2250 1248 complaint (&symfile_complaints,
3e43a32a
MS
1249 _("invalid attribute class or form for '%s' in '%s'"),
1250 arg1, arg2);
4d3c2250 1251}
c906108c 1252
c906108c
SS
1253/* local function prototypes */
1254
4efb68b1 1255static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1256
aaa75496
JB
1257static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1258 struct objfile *);
1259
918dd910
JK
1260static void dwarf2_find_base_address (struct die_info *die,
1261 struct dwarf2_cu *cu);
1262
c67a9c90 1263static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1264
72bf9492
DJ
1265static void scan_partial_symbols (struct partial_die_info *,
1266 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1267 int, struct dwarf2_cu *);
c906108c 1268
72bf9492
DJ
1269static void add_partial_symbol (struct partial_die_info *,
1270 struct dwarf2_cu *);
63d06c5c 1271
72bf9492
DJ
1272static void add_partial_namespace (struct partial_die_info *pdi,
1273 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1274 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1275
5d7cb8df
JK
1276static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1277 CORE_ADDR *highpc, int need_pc,
1278 struct dwarf2_cu *cu);
1279
72bf9492
DJ
1280static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1281 struct dwarf2_cu *cu);
91c24f0a 1282
bc30ff58
JB
1283static void add_partial_subprogram (struct partial_die_info *pdi,
1284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1285 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1286
5c80ed9d
TT
1287static void dwarf2_psymtab_to_symtab (struct objfile *,
1288 struct partial_symtab *);
c906108c 1289
a14ed312 1290static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1291
433df2d4
DE
1292static struct abbrev_info *abbrev_table_lookup_abbrev
1293 (const struct abbrev_table *, unsigned int);
1294
1295static struct abbrev_table *abbrev_table_read_table
1296 (struct dwarf2_section_info *, sect_offset);
1297
1298static void abbrev_table_free (struct abbrev_table *);
1299
f4dc4d17
DE
1300static void abbrev_table_free_cleanup (void *);
1301
dee91e82
DE
1302static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1303 struct dwarf2_section_info *);
c906108c 1304
f3dd6933 1305static void dwarf2_free_abbrev_table (void *);
c906108c 1306
6caca83c
CC
1307static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1308
dee91e82
DE
1309static struct partial_die_info *load_partial_dies
1310 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1311
dee91e82
DE
1312static gdb_byte *read_partial_die (const struct die_reader_specs *,
1313 struct partial_die_info *,
1314 struct abbrev_info *,
1315 unsigned int,
1316 gdb_byte *);
c906108c 1317
36586728 1318static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1319 struct dwarf2_cu *);
72bf9492
DJ
1320
1321static void fixup_partial_die (struct partial_die_info *,
1322 struct dwarf2_cu *);
1323
dee91e82
DE
1324static gdb_byte *read_attribute (const struct die_reader_specs *,
1325 struct attribute *, struct attr_abbrev *,
1326 gdb_byte *);
a8329558 1327
a1855c1d 1328static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1329
a1855c1d 1330static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1331
a1855c1d 1332static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1333
a1855c1d 1334static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1335
a1855c1d 1336static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1337
fe1b8b76 1338static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1339 unsigned int *);
c906108c 1340
c764a876
DE
1341static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1342
1343static LONGEST read_checked_initial_length_and_offset
1344 (bfd *, gdb_byte *, const struct comp_unit_head *,
1345 unsigned int *, unsigned int *);
613e1657 1346
fe1b8b76 1347static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1348 unsigned int *);
1349
1350static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1351
f4dc4d17
DE
1352static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1353 sect_offset);
1354
fe1b8b76 1355static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1356
9b1c24c8 1357static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1358
fe1b8b76
JB
1359static char *read_indirect_string (bfd *, gdb_byte *,
1360 const struct comp_unit_head *,
1361 unsigned int *);
4bdf3d34 1362
36586728
TT
1363static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1364
12df843f 1365static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1366
12df843f 1367static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1368
3019eac3
DE
1369static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1370 unsigned int *);
1371
1372static char *read_str_index (const struct die_reader_specs *reader,
1373 struct dwarf2_cu *cu, ULONGEST str_index);
1374
e142c38c 1375static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1376
e142c38c
DJ
1377static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1378 struct dwarf2_cu *);
c906108c 1379
348e048f 1380static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1381 unsigned int);
348e048f 1382
05cf31d1
JB
1383static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
e142c38c 1386static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1387
e142c38c 1388static struct die_info *die_specification (struct die_info *die,
f2f0e013 1389 struct dwarf2_cu **);
63d06c5c 1390
debd256d
JB
1391static void free_line_header (struct line_header *lh);
1392
aaa75496
JB
1393static void add_file_name (struct line_header *, char *, unsigned int,
1394 unsigned int, unsigned int);
1395
3019eac3
DE
1396static struct line_header *dwarf_decode_line_header (unsigned int offset,
1397 struct dwarf2_cu *cu);
debd256d 1398
f3f5162e
DE
1399static void dwarf_decode_lines (struct line_header *, const char *,
1400 struct dwarf2_cu *, struct partial_symtab *,
1401 int);
c906108c 1402
72b9f47f 1403static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1404
f4dc4d17
DE
1405static void dwarf2_start_symtab (struct dwarf2_cu *,
1406 char *, char *, CORE_ADDR);
1407
a14ed312 1408static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1409 struct dwarf2_cu *);
c906108c 1410
34eaf542
TT
1411static struct symbol *new_symbol_full (struct die_info *, struct type *,
1412 struct dwarf2_cu *, struct symbol *);
1413
a14ed312 1414static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1415 struct dwarf2_cu *);
c906108c 1416
98bfdba5
PA
1417static void dwarf2_const_value_attr (struct attribute *attr,
1418 struct type *type,
1419 const char *name,
1420 struct obstack *obstack,
12df843f 1421 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1422 gdb_byte **bytes,
1423 struct dwarf2_locexpr_baton **baton);
2df3850c 1424
e7c27a73 1425static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1426
b4ba55a1
JB
1427static int need_gnat_info (struct dwarf2_cu *);
1428
3e43a32a
MS
1429static struct type *die_descriptive_type (struct die_info *,
1430 struct dwarf2_cu *);
b4ba55a1
JB
1431
1432static void set_descriptive_type (struct type *, struct die_info *,
1433 struct dwarf2_cu *);
1434
e7c27a73
DJ
1435static struct type *die_containing_type (struct die_info *,
1436 struct dwarf2_cu *);
c906108c 1437
673bfd45
DE
1438static struct type *lookup_die_type (struct die_info *, struct attribute *,
1439 struct dwarf2_cu *);
c906108c 1440
f792889a 1441static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1442
673bfd45
DE
1443static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1444
0d5cff50 1445static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1446
6e70227d 1447static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1448 const char *suffix, int physname,
1449 struct dwarf2_cu *cu);
63d06c5c 1450
e7c27a73 1451static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1452
348e048f
DE
1453static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1454
e7c27a73 1455static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1456
e7c27a73 1457static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1458
96408a79
SA
1459static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1460
ff013f42
JK
1461static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1462 struct dwarf2_cu *, struct partial_symtab *);
1463
a14ed312 1464static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1465 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1466 struct partial_symtab *);
c906108c 1467
fae299cd
DC
1468static void get_scope_pc_bounds (struct die_info *,
1469 CORE_ADDR *, CORE_ADDR *,
1470 struct dwarf2_cu *);
1471
801e3a5b
JB
1472static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1473 CORE_ADDR, struct dwarf2_cu *);
1474
a14ed312 1475static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1476 struct dwarf2_cu *);
c906108c 1477
a14ed312 1478static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1479 struct type *, struct dwarf2_cu *);
c906108c 1480
a14ed312 1481static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1482 struct die_info *, struct type *,
e7c27a73 1483 struct dwarf2_cu *);
c906108c 1484
a14ed312 1485static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1486 struct type *,
1487 struct dwarf2_cu *);
c906108c 1488
134d01f1 1489static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1490
e7c27a73 1491static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1492
e7c27a73 1493static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1494
5d7cb8df
JK
1495static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1496
27aa8d6a
SW
1497static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1498
f55ee35c
JK
1499static struct type *read_module_type (struct die_info *die,
1500 struct dwarf2_cu *cu);
1501
38d518c9 1502static const char *namespace_name (struct die_info *die,
e142c38c 1503 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1504
134d01f1 1505static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1506
e7c27a73 1507static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1508
6e70227d 1509static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1510 struct dwarf2_cu *);
1511
dee91e82 1512static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1513 gdb_byte *info_ptr,
fe1b8b76 1514 gdb_byte **new_info_ptr,
639d11d3
DC
1515 struct die_info *parent);
1516
dee91e82 1517static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1518 gdb_byte *info_ptr,
fe1b8b76 1519 gdb_byte **new_info_ptr,
639d11d3
DC
1520 struct die_info *parent);
1521
3019eac3
DE
1522static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1523 struct die_info **, gdb_byte *, int *, int);
1524
dee91e82
DE
1525static gdb_byte *read_full_die (const struct die_reader_specs *,
1526 struct die_info **, gdb_byte *, int *);
93311388 1527
e7c27a73 1528static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1529
71c25dea
TT
1530static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1531 struct obstack *);
1532
e142c38c 1533static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1534
98bfdba5
PA
1535static const char *dwarf2_full_name (char *name,
1536 struct die_info *die,
1537 struct dwarf2_cu *cu);
1538
e142c38c 1539static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1540 struct dwarf2_cu **);
9219021c 1541
f39c6ffd 1542static const char *dwarf_tag_name (unsigned int);
c906108c 1543
f39c6ffd 1544static const char *dwarf_attr_name (unsigned int);
c906108c 1545
f39c6ffd 1546static const char *dwarf_form_name (unsigned int);
c906108c 1547
a14ed312 1548static char *dwarf_bool_name (unsigned int);
c906108c 1549
f39c6ffd 1550static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1551
f9aca02d 1552static struct die_info *sibling_die (struct die_info *);
c906108c 1553
d97bc12b
DE
1554static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1555
1556static void dump_die_for_error (struct die_info *);
1557
1558static void dump_die_1 (struct ui_file *, int level, int max_level,
1559 struct die_info *);
c906108c 1560
d97bc12b 1561/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1562
51545339 1563static void store_in_ref_table (struct die_info *,
10b3939b 1564 struct dwarf2_cu *);
c906108c 1565
93311388
DE
1566static int is_ref_attr (struct attribute *);
1567
b64f50a1 1568static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1569
43bbcdc2 1570static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1571
348e048f
DE
1572static struct die_info *follow_die_ref_or_sig (struct die_info *,
1573 struct attribute *,
1574 struct dwarf2_cu **);
1575
10b3939b
DJ
1576static struct die_info *follow_die_ref (struct die_info *,
1577 struct attribute *,
f2f0e013 1578 struct dwarf2_cu **);
c906108c 1579
348e048f
DE
1580static struct die_info *follow_die_sig (struct die_info *,
1581 struct attribute *,
1582 struct dwarf2_cu **);
1583
6c83ed52
TT
1584static struct signatured_type *lookup_signatured_type_at_offset
1585 (struct objfile *objfile,
b64f50a1 1586 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1587
e5fe5e75 1588static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1589
52dc124a 1590static void read_signatured_type (struct signatured_type *);
348e048f 1591
f4dc4d17 1592static struct type_unit_group *get_type_unit_group
094b34ac 1593 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1594
1595static void build_type_unit_groups (die_reader_func_ftype *, void *);
1596
c906108c
SS
1597/* memory allocation interface */
1598
7b5a2f43 1599static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1600
b60c80d6 1601static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1602
09262596
DE
1603static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1604 char *, int);
2e276125 1605
8e19ed76
PS
1606static int attr_form_is_block (struct attribute *);
1607
3690dd37
JB
1608static int attr_form_is_section_offset (struct attribute *);
1609
1610static int attr_form_is_constant (struct attribute *);
1611
8cf6f0b1
TT
1612static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1613 struct dwarf2_loclist_baton *baton,
1614 struct attribute *attr);
1615
93e7bd98
DJ
1616static void dwarf2_symbol_mark_computed (struct attribute *attr,
1617 struct symbol *sym,
1618 struct dwarf2_cu *cu);
4c2df51b 1619
dee91e82
DE
1620static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1621 gdb_byte *info_ptr,
1622 struct abbrev_info *abbrev);
4bb7a0a7 1623
72bf9492
DJ
1624static void free_stack_comp_unit (void *);
1625
72bf9492
DJ
1626static hashval_t partial_die_hash (const void *item);
1627
1628static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1629
ae038cb0 1630static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1631 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1632
9816fde3 1633static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1634 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1635
1636static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1637 struct die_info *comp_unit_die,
1638 enum language pretend_language);
93311388 1639
68dc6402 1640static void free_heap_comp_unit (void *);
ae038cb0
DJ
1641
1642static void free_cached_comp_units (void *);
1643
1644static void age_cached_comp_units (void);
1645
dee91e82 1646static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1647
f792889a
DJ
1648static struct type *set_die_type (struct die_info *, struct type *,
1649 struct dwarf2_cu *);
1c379e20 1650
ae038cb0
DJ
1651static void create_all_comp_units (struct objfile *);
1652
0e50663e 1653static int create_all_type_units (struct objfile *);
1fd400ff 1654
95554aad
TT
1655static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1656 enum language);
10b3939b 1657
95554aad
TT
1658static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1659 enum language);
10b3939b 1660
f4dc4d17
DE
1661static void process_full_type_unit (struct dwarf2_per_cu_data *,
1662 enum language);
1663
10b3939b
DJ
1664static void dwarf2_add_dependence (struct dwarf2_cu *,
1665 struct dwarf2_per_cu_data *);
1666
ae038cb0
DJ
1667static void dwarf2_mark (struct dwarf2_cu *);
1668
1669static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1670
b64f50a1 1671static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1672 struct dwarf2_per_cu_data *per_cu);
1673
f792889a 1674static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1675
9291a0cd
TT
1676static void dwarf2_release_queue (void *dummy);
1677
95554aad
TT
1678static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1679 enum language pretend_language);
1680
1681static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1682 struct dwarf2_per_cu_data *per_cu,
1683 enum language pretend_language);
9291a0cd 1684
a0f42c21 1685static void process_queue (void);
9291a0cd
TT
1686
1687static void find_file_and_directory (struct die_info *die,
1688 struct dwarf2_cu *cu,
1689 char **name, char **comp_dir);
1690
1691static char *file_full_name (int file, struct line_header *lh,
1692 const char *comp_dir);
1693
36586728
TT
1694static gdb_byte *read_and_check_comp_unit_head
1695 (struct comp_unit_head *header,
1696 struct dwarf2_section_info *section,
1697 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1698 int is_debug_types_section);
1699
fd820528 1700static void init_cutu_and_read_dies
f4dc4d17
DE
1701 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1702 int use_existing_cu, int keep,
3019eac3
DE
1703 die_reader_func_ftype *die_reader_func, void *data);
1704
dee91e82
DE
1705static void init_cutu_and_read_dies_simple
1706 (struct dwarf2_per_cu_data *this_cu,
1707 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1708
673bfd45 1709static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1710
3019eac3
DE
1711static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1712
1713static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1714 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1715
1716static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1717 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1718
1719static void free_dwo_file_cleanup (void *);
1720
95554aad
TT
1721static void process_cu_includes (void);
1722
1b80a9fa
JK
1723static void check_producer (struct dwarf2_cu *cu);
1724
9291a0cd
TT
1725#if WORDS_BIGENDIAN
1726
1727/* Convert VALUE between big- and little-endian. */
1728static offset_type
1729byte_swap (offset_type value)
1730{
1731 offset_type result;
1732
1733 result = (value & 0xff) << 24;
1734 result |= (value & 0xff00) << 8;
1735 result |= (value & 0xff0000) >> 8;
1736 result |= (value & 0xff000000) >> 24;
1737 return result;
1738}
1739
1740#define MAYBE_SWAP(V) byte_swap (V)
1741
1742#else
1743#define MAYBE_SWAP(V) (V)
1744#endif /* WORDS_BIGENDIAN */
1745
1746/* The suffix for an index file. */
1747#define INDEX_SUFFIX ".gdb-index"
1748
3da10d80
KS
1749static const char *dwarf2_physname (char *name, struct die_info *die,
1750 struct dwarf2_cu *cu);
1751
c906108c 1752/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1753 information and return true if we have enough to do something.
1754 NAMES points to the dwarf2 section names, or is NULL if the standard
1755 ELF names are used. */
c906108c
SS
1756
1757int
251d32d9
TG
1758dwarf2_has_info (struct objfile *objfile,
1759 const struct dwarf2_debug_sections *names)
c906108c 1760{
be391dca
TT
1761 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1762 if (!dwarf2_per_objfile)
1763 {
1764 /* Initialize per-objfile state. */
1765 struct dwarf2_per_objfile *data
1766 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1767
be391dca
TT
1768 memset (data, 0, sizeof (*data));
1769 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1770 dwarf2_per_objfile = data;
6502dd73 1771
251d32d9
TG
1772 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1773 (void *) names);
be391dca
TT
1774 dwarf2_per_objfile->objfile = objfile;
1775 }
1776 return (dwarf2_per_objfile->info.asection != NULL
1777 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1778}
1779
251d32d9
TG
1780/* When loading sections, we look either for uncompressed section or for
1781 compressed section names. */
233a11ab
CS
1782
1783static int
251d32d9
TG
1784section_is_p (const char *section_name,
1785 const struct dwarf2_section_names *names)
233a11ab 1786{
251d32d9
TG
1787 if (names->normal != NULL
1788 && strcmp (section_name, names->normal) == 0)
1789 return 1;
1790 if (names->compressed != NULL
1791 && strcmp (section_name, names->compressed) == 0)
1792 return 1;
1793 return 0;
233a11ab
CS
1794}
1795
c906108c
SS
1796/* This function is mapped across the sections and remembers the
1797 offset and size of each of the debugging sections we are interested
1798 in. */
1799
1800static void
251d32d9 1801dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1802{
251d32d9 1803 const struct dwarf2_debug_sections *names;
dc7650b8 1804 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1805
1806 if (vnames == NULL)
1807 names = &dwarf2_elf_names;
1808 else
1809 names = (const struct dwarf2_debug_sections *) vnames;
1810
dc7650b8
JK
1811 if ((aflag & SEC_HAS_CONTENTS) == 0)
1812 {
1813 }
1814 else if (section_is_p (sectp->name, &names->info))
c906108c 1815 {
dce234bc
PP
1816 dwarf2_per_objfile->info.asection = sectp;
1817 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1818 }
251d32d9 1819 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1820 {
dce234bc
PP
1821 dwarf2_per_objfile->abbrev.asection = sectp;
1822 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1823 }
251d32d9 1824 else if (section_is_p (sectp->name, &names->line))
c906108c 1825 {
dce234bc
PP
1826 dwarf2_per_objfile->line.asection = sectp;
1827 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1828 }
251d32d9 1829 else if (section_is_p (sectp->name, &names->loc))
c906108c 1830 {
dce234bc
PP
1831 dwarf2_per_objfile->loc.asection = sectp;
1832 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1833 }
251d32d9 1834 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1835 {
dce234bc
PP
1836 dwarf2_per_objfile->macinfo.asection = sectp;
1837 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1838 }
cf2c3c16
TT
1839 else if (section_is_p (sectp->name, &names->macro))
1840 {
1841 dwarf2_per_objfile->macro.asection = sectp;
1842 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1843 }
251d32d9 1844 else if (section_is_p (sectp->name, &names->str))
c906108c 1845 {
dce234bc
PP
1846 dwarf2_per_objfile->str.asection = sectp;
1847 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1848 }
3019eac3
DE
1849 else if (section_is_p (sectp->name, &names->addr))
1850 {
1851 dwarf2_per_objfile->addr.asection = sectp;
1852 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1853 }
251d32d9 1854 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1855 {
dce234bc
PP
1856 dwarf2_per_objfile->frame.asection = sectp;
1857 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1858 }
251d32d9 1859 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1860 {
dc7650b8
JK
1861 dwarf2_per_objfile->eh_frame.asection = sectp;
1862 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1863 }
251d32d9 1864 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1865 {
dce234bc
PP
1866 dwarf2_per_objfile->ranges.asection = sectp;
1867 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1868 }
251d32d9 1869 else if (section_is_p (sectp->name, &names->types))
348e048f 1870 {
8b70b953
TT
1871 struct dwarf2_section_info type_section;
1872
1873 memset (&type_section, 0, sizeof (type_section));
1874 type_section.asection = sectp;
1875 type_section.size = bfd_get_section_size (sectp);
1876
1877 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1878 &type_section);
348e048f 1879 }
251d32d9 1880 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1881 {
1882 dwarf2_per_objfile->gdb_index.asection = sectp;
1883 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1884 }
dce234bc 1885
72dca2f5
FR
1886 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1887 && bfd_section_vma (abfd, sectp) == 0)
1888 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1889}
1890
fceca515
DE
1891/* A helper function that decides whether a section is empty,
1892 or not present. */
9e0ac564
TT
1893
1894static int
1895dwarf2_section_empty_p (struct dwarf2_section_info *info)
1896{
1897 return info->asection == NULL || info->size == 0;
1898}
1899
3019eac3
DE
1900/* Read the contents of the section INFO.
1901 OBJFILE is the main object file, but not necessarily the file where
1902 the section comes from. E.g., for DWO files INFO->asection->owner
1903 is the bfd of the DWO file.
dce234bc 1904 If the section is compressed, uncompress it before returning. */
c906108c 1905
dce234bc
PP
1906static void
1907dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1908{
dce234bc 1909 asection *sectp = info->asection;
3019eac3 1910 bfd *abfd;
dce234bc
PP
1911 gdb_byte *buf, *retbuf;
1912 unsigned char header[4];
c906108c 1913
be391dca
TT
1914 if (info->readin)
1915 return;
dce234bc 1916 info->buffer = NULL;
be391dca 1917 info->readin = 1;
188dd5d6 1918
9e0ac564 1919 if (dwarf2_section_empty_p (info))
dce234bc 1920 return;
c906108c 1921
3019eac3
DE
1922 abfd = sectp->owner;
1923
4bf44c1c
TT
1924 /* If the section has relocations, we must read it ourselves.
1925 Otherwise we attach it to the BFD. */
1926 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1927 {
4bf44c1c 1928 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1929
4bf44c1c
TT
1930 /* We have to cast away const here for historical reasons.
1931 Fixing dwarf2read to be const-correct would be quite nice. */
1932 info->buffer = (gdb_byte *) bytes;
1933 return;
dce234bc 1934 }
dce234bc 1935
4bf44c1c
TT
1936 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1937 info->buffer = buf;
dce234bc
PP
1938
1939 /* When debugging .o files, we may need to apply relocations; see
1940 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1941 We never compress sections in .o files, so we only need to
1942 try this when the section is not compressed. */
ac8035ab 1943 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1944 if (retbuf != NULL)
1945 {
1946 info->buffer = retbuf;
1947 return;
1948 }
1949
1950 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1951 || bfd_bread (buf, info->size, abfd) != info->size)
1952 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1953 bfd_get_filename (abfd));
1954}
1955
9e0ac564
TT
1956/* A helper function that returns the size of a section in a safe way.
1957 If you are positive that the section has been read before using the
1958 size, then it is safe to refer to the dwarf2_section_info object's
1959 "size" field directly. In other cases, you must call this
1960 function, because for compressed sections the size field is not set
1961 correctly until the section has been read. */
1962
1963static bfd_size_type
1964dwarf2_section_size (struct objfile *objfile,
1965 struct dwarf2_section_info *info)
1966{
1967 if (!info->readin)
1968 dwarf2_read_section (objfile, info);
1969 return info->size;
1970}
1971
dce234bc 1972/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1973 SECTION_NAME. */
af34e669 1974
dce234bc 1975void
3017a003
TG
1976dwarf2_get_section_info (struct objfile *objfile,
1977 enum dwarf2_section_enum sect,
dce234bc
PP
1978 asection **sectp, gdb_byte **bufp,
1979 bfd_size_type *sizep)
1980{
1981 struct dwarf2_per_objfile *data
1982 = objfile_data (objfile, dwarf2_objfile_data_key);
1983 struct dwarf2_section_info *info;
a3b2a86b
TT
1984
1985 /* We may see an objfile without any DWARF, in which case we just
1986 return nothing. */
1987 if (data == NULL)
1988 {
1989 *sectp = NULL;
1990 *bufp = NULL;
1991 *sizep = 0;
1992 return;
1993 }
3017a003
TG
1994 switch (sect)
1995 {
1996 case DWARF2_DEBUG_FRAME:
1997 info = &data->frame;
1998 break;
1999 case DWARF2_EH_FRAME:
2000 info = &data->eh_frame;
2001 break;
2002 default:
2003 gdb_assert_not_reached ("unexpected section");
2004 }
dce234bc 2005
9e0ac564 2006 dwarf2_read_section (objfile, info);
dce234bc
PP
2007
2008 *sectp = info->asection;
2009 *bufp = info->buffer;
2010 *sizep = info->size;
2011}
2012
36586728
TT
2013/* A helper function to find the sections for a .dwz file. */
2014
2015static void
2016locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2017{
2018 struct dwz_file *dwz_file = arg;
2019
2020 /* Note that we only support the standard ELF names, because .dwz
2021 is ELF-only (at the time of writing). */
2022 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2023 {
2024 dwz_file->abbrev.asection = sectp;
2025 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2026 }
2027 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2028 {
2029 dwz_file->info.asection = sectp;
2030 dwz_file->info.size = bfd_get_section_size (sectp);
2031 }
2032 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2033 {
2034 dwz_file->str.asection = sectp;
2035 dwz_file->str.size = bfd_get_section_size (sectp);
2036 }
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2038 {
2039 dwz_file->line.asection = sectp;
2040 dwz_file->line.size = bfd_get_section_size (sectp);
2041 }
2042 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2043 {
2044 dwz_file->macro.asection = sectp;
2045 dwz_file->macro.size = bfd_get_section_size (sectp);
2046 }
2ec9a5e0
TT
2047 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2048 {
2049 dwz_file->gdb_index.asection = sectp;
2050 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2051 }
36586728
TT
2052}
2053
2054/* Open the separate '.dwz' debug file, if needed. Error if the file
2055 cannot be found. */
2056
2057static struct dwz_file *
2058dwarf2_get_dwz_file (void)
2059{
2060 bfd *abfd, *dwz_bfd;
2061 asection *section;
2062 gdb_byte *data;
2063 struct cleanup *cleanup;
2064 const char *filename;
2065 struct dwz_file *result;
2066
2067 if (dwarf2_per_objfile->dwz_file != NULL)
2068 return dwarf2_per_objfile->dwz_file;
2069
2070 abfd = dwarf2_per_objfile->objfile->obfd;
2071 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2072 if (section == NULL)
2073 error (_("could not find '.gnu_debugaltlink' section"));
2074 if (!bfd_malloc_and_get_section (abfd, section, &data))
2075 error (_("could not read '.gnu_debugaltlink' section: %s"),
2076 bfd_errmsg (bfd_get_error ()));
2077 cleanup = make_cleanup (xfree, data);
2078
2079 filename = data;
2080 if (!IS_ABSOLUTE_PATH (filename))
2081 {
2082 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2083 char *rel;
2084
2085 make_cleanup (xfree, abs);
2086 abs = ldirname (abs);
2087 make_cleanup (xfree, abs);
2088
2089 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2090 make_cleanup (xfree, rel);
2091 filename = rel;
2092 }
2093
2094 /* The format is just a NUL-terminated file name, followed by the
2095 build-id. For now, though, we ignore the build-id. */
2096 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2097 if (dwz_bfd == NULL)
2098 error (_("could not read '%s': %s"), filename,
2099 bfd_errmsg (bfd_get_error ()));
2100
2101 if (!bfd_check_format (dwz_bfd, bfd_object))
2102 {
2103 gdb_bfd_unref (dwz_bfd);
2104 error (_("file '%s' was not usable: %s"), filename,
2105 bfd_errmsg (bfd_get_error ()));
2106 }
2107
2108 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2109 struct dwz_file);
2110 result->dwz_bfd = dwz_bfd;
2111
2112 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2113
2114 do_cleanups (cleanup);
2115
8d2cc612 2116 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2117 return result;
2118}
9291a0cd 2119\f
7b9f3c50
DE
2120/* DWARF quick_symbols_functions support. */
2121
2122/* TUs can share .debug_line entries, and there can be a lot more TUs than
2123 unique line tables, so we maintain a separate table of all .debug_line
2124 derived entries to support the sharing.
2125 All the quick functions need is the list of file names. We discard the
2126 line_header when we're done and don't need to record it here. */
2127struct quick_file_names
2128{
094b34ac
DE
2129 /* The data used to construct the hash key. */
2130 struct stmt_list_hash hash;
7b9f3c50
DE
2131
2132 /* The number of entries in file_names, real_names. */
2133 unsigned int num_file_names;
2134
2135 /* The file names from the line table, after being run through
2136 file_full_name. */
2137 const char **file_names;
2138
2139 /* The file names from the line table after being run through
2140 gdb_realpath. These are computed lazily. */
2141 const char **real_names;
2142};
2143
2144/* When using the index (and thus not using psymtabs), each CU has an
2145 object of this type. This is used to hold information needed by
2146 the various "quick" methods. */
2147struct dwarf2_per_cu_quick_data
2148{
2149 /* The file table. This can be NULL if there was no file table
2150 or it's currently not read in.
2151 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2152 struct quick_file_names *file_names;
2153
2154 /* The corresponding symbol table. This is NULL if symbols for this
2155 CU have not yet been read. */
2156 struct symtab *symtab;
2157
2158 /* A temporary mark bit used when iterating over all CUs in
2159 expand_symtabs_matching. */
2160 unsigned int mark : 1;
2161
2162 /* True if we've tried to read the file table and found there isn't one.
2163 There will be no point in trying to read it again next time. */
2164 unsigned int no_file_data : 1;
2165};
2166
094b34ac
DE
2167/* Utility hash function for a stmt_list_hash. */
2168
2169static hashval_t
2170hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2171{
2172 hashval_t v = 0;
2173
2174 if (stmt_list_hash->dwo_unit != NULL)
2175 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2176 v += stmt_list_hash->line_offset.sect_off;
2177 return v;
2178}
2179
2180/* Utility equality function for a stmt_list_hash. */
2181
2182static int
2183eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2184 const struct stmt_list_hash *rhs)
2185{
2186 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2187 return 0;
2188 if (lhs->dwo_unit != NULL
2189 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2190 return 0;
2191
2192 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2193}
2194
7b9f3c50
DE
2195/* Hash function for a quick_file_names. */
2196
2197static hashval_t
2198hash_file_name_entry (const void *e)
2199{
2200 const struct quick_file_names *file_data = e;
2201
094b34ac 2202 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2203}
2204
2205/* Equality function for a quick_file_names. */
2206
2207static int
2208eq_file_name_entry (const void *a, const void *b)
2209{
2210 const struct quick_file_names *ea = a;
2211 const struct quick_file_names *eb = b;
2212
094b34ac 2213 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2214}
2215
2216/* Delete function for a quick_file_names. */
2217
2218static void
2219delete_file_name_entry (void *e)
2220{
2221 struct quick_file_names *file_data = e;
2222 int i;
2223
2224 for (i = 0; i < file_data->num_file_names; ++i)
2225 {
2226 xfree ((void*) file_data->file_names[i]);
2227 if (file_data->real_names)
2228 xfree ((void*) file_data->real_names[i]);
2229 }
2230
2231 /* The space for the struct itself lives on objfile_obstack,
2232 so we don't free it here. */
2233}
2234
2235/* Create a quick_file_names hash table. */
2236
2237static htab_t
2238create_quick_file_names_table (unsigned int nr_initial_entries)
2239{
2240 return htab_create_alloc (nr_initial_entries,
2241 hash_file_name_entry, eq_file_name_entry,
2242 delete_file_name_entry, xcalloc, xfree);
2243}
9291a0cd 2244
918dd910
JK
2245/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2246 have to be created afterwards. You should call age_cached_comp_units after
2247 processing PER_CU->CU. dw2_setup must have been already called. */
2248
2249static void
2250load_cu (struct dwarf2_per_cu_data *per_cu)
2251{
3019eac3 2252 if (per_cu->is_debug_types)
e5fe5e75 2253 load_full_type_unit (per_cu);
918dd910 2254 else
95554aad 2255 load_full_comp_unit (per_cu, language_minimal);
918dd910 2256
918dd910 2257 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2258
2259 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2260}
2261
a0f42c21 2262/* Read in the symbols for PER_CU. */
2fdf6df6 2263
9291a0cd 2264static void
a0f42c21 2265dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2266{
2267 struct cleanup *back_to;
2268
f4dc4d17
DE
2269 /* Skip type_unit_groups, reading the type units they contain
2270 is handled elsewhere. */
2271 if (IS_TYPE_UNIT_GROUP (per_cu))
2272 return;
2273
9291a0cd
TT
2274 back_to = make_cleanup (dwarf2_release_queue, NULL);
2275
95554aad
TT
2276 if (dwarf2_per_objfile->using_index
2277 ? per_cu->v.quick->symtab == NULL
2278 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2279 {
2280 queue_comp_unit (per_cu, language_minimal);
2281 load_cu (per_cu);
2282 }
9291a0cd 2283
a0f42c21 2284 process_queue ();
9291a0cd
TT
2285
2286 /* Age the cache, releasing compilation units that have not
2287 been used recently. */
2288 age_cached_comp_units ();
2289
2290 do_cleanups (back_to);
2291}
2292
2293/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2294 the objfile from which this CU came. Returns the resulting symbol
2295 table. */
2fdf6df6 2296
9291a0cd 2297static struct symtab *
a0f42c21 2298dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2299{
95554aad 2300 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2301 if (!per_cu->v.quick->symtab)
2302 {
2303 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2304 increment_reading_symtab ();
a0f42c21 2305 dw2_do_instantiate_symtab (per_cu);
95554aad 2306 process_cu_includes ();
9291a0cd
TT
2307 do_cleanups (back_to);
2308 }
2309 return per_cu->v.quick->symtab;
2310}
2311
f4dc4d17
DE
2312/* Return the CU given its index.
2313
2314 This is intended for loops like:
2315
2316 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2317 + dwarf2_per_objfile->n_type_units); ++i)
2318 {
2319 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2320
2321 ...;
2322 }
2323*/
2fdf6df6 2324
1fd400ff
TT
2325static struct dwarf2_per_cu_data *
2326dw2_get_cu (int index)
2327{
2328 if (index >= dwarf2_per_objfile->n_comp_units)
2329 {
f4dc4d17 2330 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2331 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2332 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2333 }
2334
2335 return dwarf2_per_objfile->all_comp_units[index];
2336}
2337
2338/* Return the primary CU given its index.
2339 The difference between this function and dw2_get_cu is in the handling
2340 of type units (TUs). Here we return the type_unit_group object.
2341
2342 This is intended for loops like:
2343
2344 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2345 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2346 {
2347 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2348
2349 ...;
2350 }
2351*/
2352
2353static struct dwarf2_per_cu_data *
2354dw2_get_primary_cu (int index)
2355{
2356 if (index >= dwarf2_per_objfile->n_comp_units)
2357 {
1fd400ff 2358 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2359 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2360 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2361 }
f4dc4d17 2362
1fd400ff
TT
2363 return dwarf2_per_objfile->all_comp_units[index];
2364}
2365
2ec9a5e0
TT
2366/* A helper for create_cus_from_index that handles a given list of
2367 CUs. */
2fdf6df6 2368
74a0d9f6 2369static void
2ec9a5e0
TT
2370create_cus_from_index_list (struct objfile *objfile,
2371 const gdb_byte *cu_list, offset_type n_elements,
2372 struct dwarf2_section_info *section,
2373 int is_dwz,
2374 int base_offset)
9291a0cd
TT
2375{
2376 offset_type i;
9291a0cd 2377
2ec9a5e0 2378 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2379 {
2380 struct dwarf2_per_cu_data *the_cu;
2381 ULONGEST offset, length;
2382
74a0d9f6
JK
2383 gdb_static_assert (sizeof (ULONGEST) >= 8);
2384 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2385 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2386 cu_list += 2 * 8;
2387
2388 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2389 struct dwarf2_per_cu_data);
b64f50a1 2390 the_cu->offset.sect_off = offset;
9291a0cd
TT
2391 the_cu->length = length;
2392 the_cu->objfile = objfile;
2ec9a5e0 2393 the_cu->info_or_types_section = section;
9291a0cd
TT
2394 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2395 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2396 the_cu->is_dwz = is_dwz;
2397 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2398 }
9291a0cd
TT
2399}
2400
2ec9a5e0 2401/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2402 the CU objects for this objfile. */
2ec9a5e0 2403
74a0d9f6 2404static void
2ec9a5e0
TT
2405create_cus_from_index (struct objfile *objfile,
2406 const gdb_byte *cu_list, offset_type cu_list_elements,
2407 const gdb_byte *dwz_list, offset_type dwz_elements)
2408{
2409 struct dwz_file *dwz;
2410
2411 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2412 dwarf2_per_objfile->all_comp_units
2413 = obstack_alloc (&objfile->objfile_obstack,
2414 dwarf2_per_objfile->n_comp_units
2415 * sizeof (struct dwarf2_per_cu_data *));
2416
74a0d9f6
JK
2417 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2418 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2419
2420 if (dwz_elements == 0)
74a0d9f6 2421 return;
2ec9a5e0
TT
2422
2423 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2424 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2425 cu_list_elements / 2);
2ec9a5e0
TT
2426}
2427
1fd400ff 2428/* Create the signatured type hash table from the index. */
673bfd45 2429
74a0d9f6 2430static void
673bfd45 2431create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2432 struct dwarf2_section_info *section,
673bfd45
DE
2433 const gdb_byte *bytes,
2434 offset_type elements)
1fd400ff
TT
2435{
2436 offset_type i;
673bfd45 2437 htab_t sig_types_hash;
1fd400ff 2438
d467dd73
DE
2439 dwarf2_per_objfile->n_type_units = elements / 3;
2440 dwarf2_per_objfile->all_type_units
1fd400ff 2441 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2442 dwarf2_per_objfile->n_type_units
b4dd5633 2443 * sizeof (struct signatured_type *));
1fd400ff 2444
673bfd45 2445 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2446
2447 for (i = 0; i < elements; i += 3)
2448 {
52dc124a
DE
2449 struct signatured_type *sig_type;
2450 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2451 void **slot;
2452
74a0d9f6
JK
2453 gdb_static_assert (sizeof (ULONGEST) >= 8);
2454 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2455 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2456 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2457 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2458 bytes += 3 * 8;
2459
52dc124a 2460 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2461 struct signatured_type);
52dc124a 2462 sig_type->signature = signature;
3019eac3
DE
2463 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2464 sig_type->per_cu.is_debug_types = 1;
2465 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2466 sig_type->per_cu.offset.sect_off = offset;
2467 sig_type->per_cu.objfile = objfile;
2468 sig_type->per_cu.v.quick
1fd400ff
TT
2469 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2470 struct dwarf2_per_cu_quick_data);
2471
52dc124a
DE
2472 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2473 *slot = sig_type;
1fd400ff 2474
b4dd5633 2475 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2476 }
2477
673bfd45 2478 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2479}
2480
9291a0cd
TT
2481/* Read the address map data from the mapped index, and use it to
2482 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2483
9291a0cd
TT
2484static void
2485create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2486{
2487 const gdb_byte *iter, *end;
2488 struct obstack temp_obstack;
2489 struct addrmap *mutable_map;
2490 struct cleanup *cleanup;
2491 CORE_ADDR baseaddr;
2492
2493 obstack_init (&temp_obstack);
2494 cleanup = make_cleanup_obstack_free (&temp_obstack);
2495 mutable_map = addrmap_create_mutable (&temp_obstack);
2496
2497 iter = index->address_table;
2498 end = iter + index->address_table_size;
2499
2500 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2501
2502 while (iter < end)
2503 {
2504 ULONGEST hi, lo, cu_index;
2505 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2506 iter += 8;
2507 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2508 iter += 8;
2509 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2510 iter += 4;
2511
2512 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2513 dw2_get_cu (cu_index));
9291a0cd
TT
2514 }
2515
2516 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2517 &objfile->objfile_obstack);
2518 do_cleanups (cleanup);
2519}
2520
59d7bcaf
JK
2521/* The hash function for strings in the mapped index. This is the same as
2522 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2523 implementation. This is necessary because the hash function is tied to the
2524 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2525 SYMBOL_HASH_NEXT.
2526
2527 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2528
9291a0cd 2529static hashval_t
559a7a62 2530mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2531{
2532 const unsigned char *str = (const unsigned char *) p;
2533 hashval_t r = 0;
2534 unsigned char c;
2535
2536 while ((c = *str++) != 0)
559a7a62
JK
2537 {
2538 if (index_version >= 5)
2539 c = tolower (c);
2540 r = r * 67 + c - 113;
2541 }
9291a0cd
TT
2542
2543 return r;
2544}
2545
2546/* Find a slot in the mapped index INDEX for the object named NAME.
2547 If NAME is found, set *VEC_OUT to point to the CU vector in the
2548 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2549
9291a0cd
TT
2550static int
2551find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2552 offset_type **vec_out)
2553{
0cf03b49
JK
2554 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2555 offset_type hash;
9291a0cd 2556 offset_type slot, step;
559a7a62 2557 int (*cmp) (const char *, const char *);
9291a0cd 2558
0cf03b49
JK
2559 if (current_language->la_language == language_cplus
2560 || current_language->la_language == language_java
2561 || current_language->la_language == language_fortran)
2562 {
2563 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2564 not contain any. */
2565 const char *paren = strchr (name, '(');
2566
2567 if (paren)
2568 {
2569 char *dup;
2570
2571 dup = xmalloc (paren - name + 1);
2572 memcpy (dup, name, paren - name);
2573 dup[paren - name] = 0;
2574
2575 make_cleanup (xfree, dup);
2576 name = dup;
2577 }
2578 }
2579
559a7a62 2580 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2581 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2582 simulate our NAME being searched is also lowercased. */
2583 hash = mapped_index_string_hash ((index->version == 4
2584 && case_sensitivity == case_sensitive_off
2585 ? 5 : index->version),
2586 name);
2587
3876f04e
DE
2588 slot = hash & (index->symbol_table_slots - 1);
2589 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2590 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2591
2592 for (;;)
2593 {
2594 /* Convert a slot number to an offset into the table. */
2595 offset_type i = 2 * slot;
2596 const char *str;
3876f04e 2597 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2598 {
2599 do_cleanups (back_to);
2600 return 0;
2601 }
9291a0cd 2602
3876f04e 2603 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2604 if (!cmp (name, str))
9291a0cd
TT
2605 {
2606 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2607 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2608 do_cleanups (back_to);
9291a0cd
TT
2609 return 1;
2610 }
2611
3876f04e 2612 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2613 }
2614}
2615
2ec9a5e0
TT
2616/* A helper function that reads the .gdb_index from SECTION and fills
2617 in MAP. FILENAME is the name of the file containing the section;
2618 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2619 ok to use deprecated sections.
2620
2621 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2622 out parameters that are filled in with information about the CU and
2623 TU lists in the section.
2624
2625 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2626
9291a0cd 2627static int
2ec9a5e0
TT
2628read_index_from_section (struct objfile *objfile,
2629 const char *filename,
2630 int deprecated_ok,
2631 struct dwarf2_section_info *section,
2632 struct mapped_index *map,
2633 const gdb_byte **cu_list,
2634 offset_type *cu_list_elements,
2635 const gdb_byte **types_list,
2636 offset_type *types_list_elements)
9291a0cd 2637{
9291a0cd 2638 char *addr;
2ec9a5e0 2639 offset_type version;
b3b272e1 2640 offset_type *metadata;
1fd400ff 2641 int i;
9291a0cd 2642
2ec9a5e0 2643 if (dwarf2_section_empty_p (section))
9291a0cd 2644 return 0;
82430852
JK
2645
2646 /* Older elfutils strip versions could keep the section in the main
2647 executable while splitting it for the separate debug info file. */
2ec9a5e0 2648 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2649 return 0;
2650
2ec9a5e0 2651 dwarf2_read_section (objfile, section);
9291a0cd 2652
2ec9a5e0 2653 addr = section->buffer;
9291a0cd 2654 /* Version check. */
1fd400ff 2655 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2656 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2657 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2658 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2659 indices. */
831adc1f 2660 if (version < 4)
481860b3
GB
2661 {
2662 static int warning_printed = 0;
2663 if (!warning_printed)
2664 {
2665 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2666 filename);
481860b3
GB
2667 warning_printed = 1;
2668 }
2669 return 0;
2670 }
2671 /* Index version 4 uses a different hash function than index version
2672 5 and later.
2673
2674 Versions earlier than 6 did not emit psymbols for inlined
2675 functions. Using these files will cause GDB not to be able to
2676 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2677 indices unless the user has done
2678 "set use-deprecated-index-sections on". */
2ec9a5e0 2679 if (version < 6 && !deprecated_ok)
481860b3
GB
2680 {
2681 static int warning_printed = 0;
2682 if (!warning_printed)
2683 {
e615022a
DE
2684 warning (_("\
2685Skipping deprecated .gdb_index section in %s.\n\
2686Do \"set use-deprecated-index-sections on\" before the file is read\n\
2687to use the section anyway."),
2ec9a5e0 2688 filename);
481860b3
GB
2689 warning_printed = 1;
2690 }
2691 return 0;
2692 }
2693 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2694 longer backward compatible. */
156942c7 2695 if (version > 7)
594e8718 2696 return 0;
9291a0cd 2697
559a7a62 2698 map->version = version;
2ec9a5e0 2699 map->total_size = section->size;
9291a0cd
TT
2700
2701 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2702
2703 i = 0;
2ec9a5e0
TT
2704 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2705 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2706 / 8);
1fd400ff
TT
2707 ++i;
2708
2ec9a5e0
TT
2709 *types_list = addr + MAYBE_SWAP (metadata[i]);
2710 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2711 - MAYBE_SWAP (metadata[i]))
2712 / 8);
987d643c 2713 ++i;
1fd400ff
TT
2714
2715 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2716 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2717 - MAYBE_SWAP (metadata[i]));
2718 ++i;
2719
3876f04e
DE
2720 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2721 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2722 - MAYBE_SWAP (metadata[i]))
2723 / (2 * sizeof (offset_type)));
1fd400ff 2724 ++i;
9291a0cd 2725
1fd400ff
TT
2726 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2727
2ec9a5e0
TT
2728 return 1;
2729}
2730
2731
2732/* Read the index file. If everything went ok, initialize the "quick"
2733 elements of all the CUs and return 1. Otherwise, return 0. */
2734
2735static int
2736dwarf2_read_index (struct objfile *objfile)
2737{
2738 struct mapped_index local_map, *map;
2739 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2740 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2741
2742 if (!read_index_from_section (objfile, objfile->name,
2743 use_deprecated_index_sections,
2744 &dwarf2_per_objfile->gdb_index, &local_map,
2745 &cu_list, &cu_list_elements,
2746 &types_list, &types_list_elements))
2747 return 0;
2748
0fefef59 2749 /* Don't use the index if it's empty. */
2ec9a5e0 2750 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2751 return 0;
2752
2ec9a5e0
TT
2753 /* If there is a .dwz file, read it so we can get its CU list as
2754 well. */
2755 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2756 {
2757 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2758 struct mapped_index dwz_map;
2759 const gdb_byte *dwz_types_ignore;
2760 offset_type dwz_types_elements_ignore;
2761
2762 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2763 1,
2764 &dwz->gdb_index, &dwz_map,
2765 &dwz_list, &dwz_list_elements,
2766 &dwz_types_ignore,
2767 &dwz_types_elements_ignore))
2768 {
2769 warning (_("could not read '.gdb_index' section from %s; skipping"),
2770 bfd_get_filename (dwz->dwz_bfd));
2771 return 0;
2772 }
2773 }
2774
74a0d9f6
JK
2775 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2776 dwz_list_elements);
1fd400ff 2777
8b70b953
TT
2778 if (types_list_elements)
2779 {
2780 struct dwarf2_section_info *section;
2781
2782 /* We can only handle a single .debug_types when we have an
2783 index. */
2784 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2785 return 0;
2786
2787 section = VEC_index (dwarf2_section_info_def,
2788 dwarf2_per_objfile->types, 0);
2789
74a0d9f6
JK
2790 create_signatured_type_table_from_index (objfile, section, types_list,
2791 types_list_elements);
8b70b953 2792 }
9291a0cd 2793
2ec9a5e0
TT
2794 create_addrmap_from_index (objfile, &local_map);
2795
2796 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2797 *map = local_map;
9291a0cd
TT
2798
2799 dwarf2_per_objfile->index_table = map;
2800 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2801 dwarf2_per_objfile->quick_file_names_table =
2802 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2803
2804 return 1;
2805}
2806
2807/* A helper for the "quick" functions which sets the global
2808 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2809
9291a0cd
TT
2810static void
2811dw2_setup (struct objfile *objfile)
2812{
2813 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2814 gdb_assert (dwarf2_per_objfile);
2815}
2816
f4dc4d17
DE
2817/* Reader function for dw2_build_type_unit_groups. */
2818
2819static void
2820dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2821 gdb_byte *info_ptr,
2822 struct die_info *type_unit_die,
2823 int has_children,
2824 void *data)
2825{
2826 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2827 struct attribute *attr;
2828 struct type_unit_group *tu_group;
2829
2830 gdb_assert (data == NULL);
2831
2832 if (! has_children)
2833 return;
2834
2835 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2836 /* Call this for its side-effect of creating the associated
2837 struct type_unit_group if it doesn't already exist. */
094b34ac 2838 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2839}
2840
2841/* Build dwarf2_per_objfile->type_unit_groups.
2842 This function may be called multiple times. */
2843
2844static void
2845dw2_build_type_unit_groups (void)
2846{
2847 if (dwarf2_per_objfile->type_unit_groups == NULL)
2848 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2849}
2850
dee91e82 2851/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2852
dee91e82
DE
2853static void
2854dw2_get_file_names_reader (const struct die_reader_specs *reader,
2855 gdb_byte *info_ptr,
2856 struct die_info *comp_unit_die,
2857 int has_children,
2858 void *data)
9291a0cd 2859{
dee91e82
DE
2860 struct dwarf2_cu *cu = reader->cu;
2861 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2862 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2863 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2864 struct line_header *lh;
9291a0cd 2865 struct attribute *attr;
dee91e82 2866 int i;
9291a0cd 2867 char *name, *comp_dir;
7b9f3c50
DE
2868 void **slot;
2869 struct quick_file_names *qfn;
2870 unsigned int line_offset;
9291a0cd 2871
07261596
TT
2872 /* Our callers never want to match partial units -- instead they
2873 will match the enclosing full CU. */
2874 if (comp_unit_die->tag == DW_TAG_partial_unit)
2875 {
2876 this_cu->v.quick->no_file_data = 1;
2877 return;
2878 }
2879
094b34ac
DE
2880 /* If we're reading the line header for TUs, store it in the "per_cu"
2881 for tu_group. */
2882 if (this_cu->is_debug_types)
2883 {
2884 struct type_unit_group *tu_group = data;
2885
2886 gdb_assert (tu_group != NULL);
2887 lh_cu = &tu_group->per_cu;
2888 }
2889 else
2890 lh_cu = this_cu;
2891
7b9f3c50
DE
2892 lh = NULL;
2893 slot = NULL;
2894 line_offset = 0;
dee91e82
DE
2895
2896 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2897 if (attr)
2898 {
7b9f3c50
DE
2899 struct quick_file_names find_entry;
2900
2901 line_offset = DW_UNSND (attr);
2902
2903 /* We may have already read in this line header (TU line header sharing).
2904 If we have we're done. */
094b34ac
DE
2905 find_entry.hash.dwo_unit = cu->dwo_unit;
2906 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2907 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2908 &find_entry, INSERT);
2909 if (*slot != NULL)
2910 {
094b34ac 2911 lh_cu->v.quick->file_names = *slot;
dee91e82 2912 return;
7b9f3c50
DE
2913 }
2914
3019eac3 2915 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2916 }
2917 if (lh == NULL)
2918 {
094b34ac 2919 lh_cu->v.quick->no_file_data = 1;
dee91e82 2920 return;
9291a0cd
TT
2921 }
2922
7b9f3c50 2923 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2924 qfn->hash.dwo_unit = cu->dwo_unit;
2925 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2926 gdb_assert (slot != NULL);
2927 *slot = qfn;
9291a0cd 2928
dee91e82 2929 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2930
7b9f3c50
DE
2931 qfn->num_file_names = lh->num_file_names;
2932 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2933 lh->num_file_names * sizeof (char *));
9291a0cd 2934 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2935 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2936 qfn->real_names = NULL;
9291a0cd 2937
7b9f3c50 2938 free_line_header (lh);
7b9f3c50 2939
094b34ac 2940 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2941}
2942
2943/* A helper for the "quick" functions which attempts to read the line
2944 table for THIS_CU. */
2945
2946static struct quick_file_names *
2947dw2_get_file_names (struct objfile *objfile,
2948 struct dwarf2_per_cu_data *this_cu)
2949{
f4dc4d17
DE
2950 /* For TUs this should only be called on the parent group. */
2951 if (this_cu->is_debug_types)
2952 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2953
dee91e82
DE
2954 if (this_cu->v.quick->file_names != NULL)
2955 return this_cu->v.quick->file_names;
2956 /* If we know there is no line data, no point in looking again. */
2957 if (this_cu->v.quick->no_file_data)
2958 return NULL;
2959
3019eac3
DE
2960 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2961 in the stub for CUs, there's is no need to lookup the DWO file.
2962 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2963 DWO file. */
2964 if (this_cu->is_debug_types)
094b34ac
DE
2965 {
2966 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2967
2968 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2969 dw2_get_file_names_reader, tu_group);
2970 }
3019eac3
DE
2971 else
2972 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2973
2974 if (this_cu->v.quick->no_file_data)
2975 return NULL;
2976 return this_cu->v.quick->file_names;
9291a0cd
TT
2977}
2978
2979/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2980 real path for a given file name from the line table. */
2fdf6df6 2981
9291a0cd 2982static const char *
7b9f3c50
DE
2983dw2_get_real_path (struct objfile *objfile,
2984 struct quick_file_names *qfn, int index)
9291a0cd 2985{
7b9f3c50
DE
2986 if (qfn->real_names == NULL)
2987 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2988 qfn->num_file_names, sizeof (char *));
9291a0cd 2989
7b9f3c50
DE
2990 if (qfn->real_names[index] == NULL)
2991 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2992
7b9f3c50 2993 return qfn->real_names[index];
9291a0cd
TT
2994}
2995
2996static struct symtab *
2997dw2_find_last_source_symtab (struct objfile *objfile)
2998{
2999 int index;
ae2de4f8 3000
9291a0cd
TT
3001 dw2_setup (objfile);
3002 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3003 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3004}
3005
7b9f3c50
DE
3006/* Traversal function for dw2_forget_cached_source_info. */
3007
3008static int
3009dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3010{
7b9f3c50 3011 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3012
7b9f3c50 3013 if (file_data->real_names)
9291a0cd 3014 {
7b9f3c50 3015 int i;
9291a0cd 3016
7b9f3c50 3017 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3018 {
7b9f3c50
DE
3019 xfree ((void*) file_data->real_names[i]);
3020 file_data->real_names[i] = NULL;
9291a0cd
TT
3021 }
3022 }
7b9f3c50
DE
3023
3024 return 1;
3025}
3026
3027static void
3028dw2_forget_cached_source_info (struct objfile *objfile)
3029{
3030 dw2_setup (objfile);
3031
3032 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3033 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3034}
3035
f8eba3c6
TT
3036/* Helper function for dw2_map_symtabs_matching_filename that expands
3037 the symtabs and calls the iterator. */
3038
3039static int
3040dw2_map_expand_apply (struct objfile *objfile,
3041 struct dwarf2_per_cu_data *per_cu,
3042 const char *name,
3043 const char *full_path, const char *real_path,
3044 int (*callback) (struct symtab *, void *),
3045 void *data)
3046{
3047 struct symtab *last_made = objfile->symtabs;
3048
3049 /* Don't visit already-expanded CUs. */
3050 if (per_cu->v.quick->symtab)
3051 return 0;
3052
3053 /* This may expand more than one symtab, and we want to iterate over
3054 all of them. */
a0f42c21 3055 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3056
3057 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3058 objfile->symtabs, last_made);
3059}
3060
3061/* Implementation of the map_symtabs_matching_filename method. */
3062
9291a0cd 3063static int
f8eba3c6
TT
3064dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3065 const char *full_path, const char *real_path,
3066 int (*callback) (struct symtab *, void *),
3067 void *data)
9291a0cd
TT
3068{
3069 int i;
c011a4f4 3070 const char *name_basename = lbasename (name);
4aac40c8 3071 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3072
3073 dw2_setup (objfile);
ae2de4f8 3074
f4dc4d17
DE
3075 dw2_build_type_unit_groups ();
3076
1fd400ff 3077 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3078 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3079 {
3080 int j;
f4dc4d17 3081 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3082 struct quick_file_names *file_data;
9291a0cd 3083
3d7bb9d9 3084 /* We only need to look at symtabs not already expanded. */
e254ef6a 3085 if (per_cu->v.quick->symtab)
9291a0cd
TT
3086 continue;
3087
7b9f3c50
DE
3088 file_data = dw2_get_file_names (objfile, per_cu);
3089 if (file_data == NULL)
9291a0cd
TT
3090 continue;
3091
7b9f3c50 3092 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3093 {
7b9f3c50 3094 const char *this_name = file_data->file_names[j];
9291a0cd 3095
4aac40c8 3096 if (FILENAME_CMP (name, this_name) == 0
b57a636e 3097 || (!is_abs && compare_filenames_for_search (this_name, name)))
9291a0cd 3098 {
f8eba3c6
TT
3099 if (dw2_map_expand_apply (objfile, per_cu,
3100 name, full_path, real_path,
3101 callback, data))
3102 return 1;
4aac40c8 3103 }
9291a0cd 3104
c011a4f4
DE
3105 /* Before we invoke realpath, which can get expensive when many
3106 files are involved, do a quick comparison of the basenames. */
3107 if (! basenames_may_differ
3108 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3109 continue;
3110
9291a0cd
TT
3111 if (full_path != NULL)
3112 {
7b9f3c50
DE
3113 const char *this_real_name = dw2_get_real_path (objfile,
3114 file_data, j);
9291a0cd 3115
7b9f3c50 3116 if (this_real_name != NULL
4aac40c8
TT
3117 && (FILENAME_CMP (full_path, this_real_name) == 0
3118 || (!is_abs
3119 && compare_filenames_for_search (this_real_name,
b57a636e 3120 name))))
9291a0cd 3121 {
f8eba3c6
TT
3122 if (dw2_map_expand_apply (objfile, per_cu,
3123 name, full_path, real_path,
3124 callback, data))
3125 return 1;
9291a0cd
TT
3126 }
3127 }
3128
3129 if (real_path != NULL)
3130 {
7b9f3c50
DE
3131 const char *this_real_name = dw2_get_real_path (objfile,
3132 file_data, j);
9291a0cd 3133
7b9f3c50 3134 if (this_real_name != NULL
4aac40c8
TT
3135 && (FILENAME_CMP (real_path, this_real_name) == 0
3136 || (!is_abs
3137 && compare_filenames_for_search (this_real_name,
b57a636e 3138 name))))
9291a0cd 3139 {
f8eba3c6
TT
3140 if (dw2_map_expand_apply (objfile, per_cu,
3141 name, full_path, real_path,
3142 callback, data))
3143 return 1;
9291a0cd
TT
3144 }
3145 }
3146 }
3147 }
3148
9291a0cd
TT
3149 return 0;
3150}
3151
da51c347
DE
3152/* Struct used to manage iterating over all CUs looking for a symbol. */
3153
3154struct dw2_symtab_iterator
9291a0cd 3155{
da51c347
DE
3156 /* The internalized form of .gdb_index. */
3157 struct mapped_index *index;
3158 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3159 int want_specific_block;
3160 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3161 Unused if !WANT_SPECIFIC_BLOCK. */
3162 int block_index;
3163 /* The kind of symbol we're looking for. */
3164 domain_enum domain;
3165 /* The list of CUs from the index entry of the symbol,
3166 or NULL if not found. */
3167 offset_type *vec;
3168 /* The next element in VEC to look at. */
3169 int next;
3170 /* The number of elements in VEC, or zero if there is no match. */
3171 int length;
3172};
9291a0cd 3173
da51c347
DE
3174/* Initialize the index symtab iterator ITER.
3175 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3176 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3177
9291a0cd 3178static void
da51c347
DE
3179dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3180 struct mapped_index *index,
3181 int want_specific_block,
3182 int block_index,
3183 domain_enum domain,
3184 const char *name)
3185{
3186 iter->index = index;
3187 iter->want_specific_block = want_specific_block;
3188 iter->block_index = block_index;
3189 iter->domain = domain;
3190 iter->next = 0;
3191
3192 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3193 iter->length = MAYBE_SWAP (*iter->vec);
3194 else
3195 {
3196 iter->vec = NULL;
3197 iter->length = 0;
3198 }
3199}
3200
3201/* Return the next matching CU or NULL if there are no more. */
3202
3203static struct dwarf2_per_cu_data *
3204dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3205{
3206 for ( ; iter->next < iter->length; ++iter->next)
3207 {
3208 offset_type cu_index_and_attrs =
3209 MAYBE_SWAP (iter->vec[iter->next + 1]);
3210 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3211 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3212 int want_static = iter->block_index != GLOBAL_BLOCK;
3213 /* This value is only valid for index versions >= 7. */
3214 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3215 gdb_index_symbol_kind symbol_kind =
3216 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3217 /* Only check the symbol attributes if they're present.
3218 Indices prior to version 7 don't record them,
3219 and indices >= 7 may elide them for certain symbols
3220 (gold does this). */
3221 int attrs_valid =
3222 (iter->index->version >= 7
3223 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3224
3225 /* Skip if already read in. */
3226 if (per_cu->v.quick->symtab)
3227 continue;
3228
3229 if (attrs_valid
3230 && iter->want_specific_block
3231 && want_static != is_static)
3232 continue;
3233
3234 /* Only check the symbol's kind if it has one. */
3235 if (attrs_valid)
3236 {
3237 switch (iter->domain)
3238 {
3239 case VAR_DOMAIN:
3240 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3241 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3242 /* Some types are also in VAR_DOMAIN. */
3243 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3244 continue;
3245 break;
3246 case STRUCT_DOMAIN:
3247 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3248 continue;
3249 break;
3250 case LABEL_DOMAIN:
3251 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3252 continue;
3253 break;
3254 default:
3255 break;
3256 }
3257 }
3258
3259 ++iter->next;
3260 return per_cu;
3261 }
3262
3263 return NULL;
3264}
3265
3266static struct symtab *
3267dw2_lookup_symbol (struct objfile *objfile, int block_index,
3268 const char *name, domain_enum domain)
9291a0cd 3269{
da51c347 3270 struct symtab *stab_best = NULL;
156942c7
DE
3271 struct mapped_index *index;
3272
9291a0cd
TT
3273 dw2_setup (objfile);
3274
156942c7
DE
3275 index = dwarf2_per_objfile->index_table;
3276
da51c347 3277 /* index is NULL if OBJF_READNOW. */
156942c7 3278 if (index)
9291a0cd 3279 {
da51c347
DE
3280 struct dw2_symtab_iterator iter;
3281 struct dwarf2_per_cu_data *per_cu;
3282
3283 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3284
da51c347 3285 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3286 {
da51c347
DE
3287 struct symbol *sym = NULL;
3288 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3289
3290 /* Some caution must be observed with overloaded functions
3291 and methods, since the index will not contain any overload
3292 information (but NAME might contain it). */
3293 if (stab->primary)
9291a0cd 3294 {
da51c347
DE
3295 struct blockvector *bv = BLOCKVECTOR (stab);
3296 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3297
da51c347
DE
3298 sym = lookup_block_symbol (block, name, domain);
3299 }
1fd400ff 3300
da51c347
DE
3301 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3302 {
3303 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3304 return stab;
3305
3306 stab_best = stab;
9291a0cd 3307 }
da51c347
DE
3308
3309 /* Keep looking through other CUs. */
9291a0cd
TT
3310 }
3311 }
9291a0cd 3312
da51c347 3313 return stab_best;
9291a0cd
TT
3314}
3315
3316static void
3317dw2_print_stats (struct objfile *objfile)
3318{
3319 int i, count;
3320
3321 dw2_setup (objfile);
3322 count = 0;
1fd400ff 3323 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3324 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3325 {
e254ef6a 3326 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3327
e254ef6a 3328 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3329 ++count;
3330 }
3331 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3332}
3333
3334static void
3335dw2_dump (struct objfile *objfile)
3336{
3337 /* Nothing worth printing. */
3338}
3339
3340static void
3341dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3342 struct section_offsets *delta)
3343{
3344 /* There's nothing to relocate here. */
3345}
3346
3347static void
3348dw2_expand_symtabs_for_function (struct objfile *objfile,
3349 const char *func_name)
3350{
da51c347
DE
3351 struct mapped_index *index;
3352
3353 dw2_setup (objfile);
3354
3355 index = dwarf2_per_objfile->index_table;
3356
3357 /* index is NULL if OBJF_READNOW. */
3358 if (index)
3359 {
3360 struct dw2_symtab_iterator iter;
3361 struct dwarf2_per_cu_data *per_cu;
3362
3363 /* Note: It doesn't matter what we pass for block_index here. */
3364 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3365 func_name);
3366
3367 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3368 dw2_instantiate_symtab (per_cu);
3369 }
9291a0cd
TT
3370}
3371
3372static void
3373dw2_expand_all_symtabs (struct objfile *objfile)
3374{
3375 int i;
3376
3377 dw2_setup (objfile);
1fd400ff
TT
3378
3379 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3380 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3381 {
e254ef6a 3382 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3383
a0f42c21 3384 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3385 }
3386}
3387
3388static void
3389dw2_expand_symtabs_with_filename (struct objfile *objfile,
3390 const char *filename)
3391{
3392 int i;
3393
3394 dw2_setup (objfile);
d4637a04
DE
3395
3396 /* We don't need to consider type units here.
3397 This is only called for examining code, e.g. expand_line_sal.
3398 There can be an order of magnitude (or more) more type units
3399 than comp units, and we avoid them if we can. */
3400
3401 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3402 {
3403 int j;
e254ef6a 3404 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3405 struct quick_file_names *file_data;
9291a0cd 3406
3d7bb9d9 3407 /* We only need to look at symtabs not already expanded. */
e254ef6a 3408 if (per_cu->v.quick->symtab)
9291a0cd
TT
3409 continue;
3410
7b9f3c50
DE
3411 file_data = dw2_get_file_names (objfile, per_cu);
3412 if (file_data == NULL)
9291a0cd
TT
3413 continue;
3414
7b9f3c50 3415 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3416 {
7b9f3c50 3417 const char *this_name = file_data->file_names[j];
1ef75ecc 3418 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3419 {
a0f42c21 3420 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3421 break;
3422 }
3423 }
3424 }
3425}
3426
356d9f9d
TT
3427/* A helper function for dw2_find_symbol_file that finds the primary
3428 file name for a given CU. This is a die_reader_func. */
3429
3430static void
3431dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3432 gdb_byte *info_ptr,
3433 struct die_info *comp_unit_die,
3434 int has_children,
3435 void *data)
3436{
3437 const char **result_ptr = data;
3438 struct dwarf2_cu *cu = reader->cu;
3439 struct attribute *attr;
3440
3441 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3442 if (attr == NULL)
3443 *result_ptr = NULL;
3444 else
3445 *result_ptr = DW_STRING (attr);
3446}
3447
dd786858 3448static const char *
9291a0cd
TT
3449dw2_find_symbol_file (struct objfile *objfile, const char *name)
3450{
e254ef6a 3451 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3452 offset_type *vec;
356d9f9d 3453 const char *filename;
9291a0cd
TT
3454
3455 dw2_setup (objfile);
3456
ae2de4f8 3457 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3458 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3459 {
3460 struct symtab *s;
3461
d790cf0a
DE
3462 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3463 {
3464 struct blockvector *bv = BLOCKVECTOR (s);
3465 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3466 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3467
3468 if (sym)
210bbc17 3469 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3470 }
96408a79
SA
3471 return NULL;
3472 }
9291a0cd
TT
3473
3474 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3475 name, &vec))
3476 return NULL;
3477
3478 /* Note that this just looks at the very first one named NAME -- but
3479 actually we are looking for a function. find_main_filename
3480 should be rewritten so that it doesn't require a custom hook. It
3481 could just use the ordinary symbol tables. */
3482 /* vec[0] is the length, which must always be >0. */
156942c7 3483 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3484
356d9f9d
TT
3485 if (per_cu->v.quick->symtab != NULL)
3486 return per_cu->v.quick->symtab->filename;
3487
f4dc4d17
DE
3488 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3489 dw2_get_primary_filename_reader, &filename);
9291a0cd 3490
356d9f9d 3491 return filename;
9291a0cd
TT
3492}
3493
3494static void
40658b94
PH
3495dw2_map_matching_symbols (const char * name, domain_enum namespace,
3496 struct objfile *objfile, int global,
3497 int (*callback) (struct block *,
3498 struct symbol *, void *),
2edb89d3
JK
3499 void *data, symbol_compare_ftype *match,
3500 symbol_compare_ftype *ordered_compare)
9291a0cd 3501{
40658b94 3502 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3503 current language is Ada for a non-Ada objfile using GNU index. As Ada
3504 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3505}
3506
3507static void
f8eba3c6
TT
3508dw2_expand_symtabs_matching
3509 (struct objfile *objfile,
3510 int (*file_matcher) (const char *, void *),
e078317b 3511 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3512 enum search_domain kind,
3513 void *data)
9291a0cd
TT
3514{
3515 int i;
3516 offset_type iter;
4b5246aa 3517 struct mapped_index *index;
9291a0cd
TT
3518
3519 dw2_setup (objfile);
ae2de4f8
DE
3520
3521 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3522 if (!dwarf2_per_objfile->index_table)
3523 return;
4b5246aa 3524 index = dwarf2_per_objfile->index_table;
9291a0cd 3525
7b08b9eb 3526 if (file_matcher != NULL)
24c79950
TT
3527 {
3528 struct cleanup *cleanup;
3529 htab_t visited_found, visited_not_found;
3530
f4dc4d17
DE
3531 dw2_build_type_unit_groups ();
3532
24c79950
TT
3533 visited_found = htab_create_alloc (10,
3534 htab_hash_pointer, htab_eq_pointer,
3535 NULL, xcalloc, xfree);
3536 cleanup = make_cleanup_htab_delete (visited_found);
3537 visited_not_found = htab_create_alloc (10,
3538 htab_hash_pointer, htab_eq_pointer,
3539 NULL, xcalloc, xfree);
3540 make_cleanup_htab_delete (visited_not_found);
3541
3542 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3543 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3544 {
3545 int j;
f4dc4d17 3546 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3547 struct quick_file_names *file_data;
3548 void **slot;
7b08b9eb 3549
24c79950 3550 per_cu->v.quick->mark = 0;
3d7bb9d9 3551
24c79950
TT
3552 /* We only need to look at symtabs not already expanded. */
3553 if (per_cu->v.quick->symtab)
3554 continue;
7b08b9eb 3555
24c79950
TT
3556 file_data = dw2_get_file_names (objfile, per_cu);
3557 if (file_data == NULL)
3558 continue;
7b08b9eb 3559
24c79950
TT
3560 if (htab_find (visited_not_found, file_data) != NULL)
3561 continue;
3562 else if (htab_find (visited_found, file_data) != NULL)
3563 {
3564 per_cu->v.quick->mark = 1;
3565 continue;
3566 }
3567
3568 for (j = 0; j < file_data->num_file_names; ++j)
3569 {
3570 if (file_matcher (file_data->file_names[j], data))
3571 {
3572 per_cu->v.quick->mark = 1;
3573 break;
3574 }
3575 }
3576
3577 slot = htab_find_slot (per_cu->v.quick->mark
3578 ? visited_found
3579 : visited_not_found,
3580 file_data, INSERT);
3581 *slot = file_data;
3582 }
3583
3584 do_cleanups (cleanup);
3585 }
9291a0cd 3586
3876f04e 3587 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3588 {
3589 offset_type idx = 2 * iter;
3590 const char *name;
3591 offset_type *vec, vec_len, vec_idx;
3592
3876f04e 3593 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3594 continue;
3595
3876f04e 3596 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3597
e078317b 3598 if (! (*name_matcher) (name, data))
9291a0cd
TT
3599 continue;
3600
3601 /* The name was matched, now expand corresponding CUs that were
3602 marked. */
4b5246aa 3603 vec = (offset_type *) (index->constant_pool
3876f04e 3604 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3605 vec_len = MAYBE_SWAP (vec[0]);
3606 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3607 {
e254ef6a 3608 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3609 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3610 gdb_index_symbol_kind symbol_kind =
3611 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3612 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3613
3614 /* Don't crash on bad data. */
3615 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3616 + dwarf2_per_objfile->n_type_units))
156942c7 3617 continue;
1fd400ff 3618
156942c7
DE
3619 /* Only check the symbol's kind if it has one.
3620 Indices prior to version 7 don't record it. */
3621 if (index->version >= 7)
3622 {
3623 switch (kind)
3624 {
3625 case VARIABLES_DOMAIN:
3626 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3627 continue;
3628 break;
3629 case FUNCTIONS_DOMAIN:
3630 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3631 continue;
3632 break;
3633 case TYPES_DOMAIN:
3634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3635 continue;
3636 break;
3637 default:
3638 break;
3639 }
3640 }
3641
3642 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3643 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3644 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3645 }
3646 }
3647}
3648
9703b513
TT
3649/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3650 symtab. */
3651
3652static struct symtab *
3653recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3654{
3655 int i;
3656
3657 if (BLOCKVECTOR (symtab) != NULL
3658 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3659 return symtab;
3660
a3ec0bb1
DE
3661 if (symtab->includes == NULL)
3662 return NULL;
3663
9703b513
TT
3664 for (i = 0; symtab->includes[i]; ++i)
3665 {
a3ec0bb1 3666 struct symtab *s = symtab->includes[i];
9703b513
TT
3667
3668 s = recursively_find_pc_sect_symtab (s, pc);
3669 if (s != NULL)
3670 return s;
3671 }
3672
3673 return NULL;
3674}
3675
9291a0cd
TT
3676static struct symtab *
3677dw2_find_pc_sect_symtab (struct objfile *objfile,
3678 struct minimal_symbol *msymbol,
3679 CORE_ADDR pc,
3680 struct obj_section *section,
3681 int warn_if_readin)
3682{
3683 struct dwarf2_per_cu_data *data;
9703b513 3684 struct symtab *result;
9291a0cd
TT
3685
3686 dw2_setup (objfile);
3687
3688 if (!objfile->psymtabs_addrmap)
3689 return NULL;
3690
3691 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3692 if (!data)
3693 return NULL;
3694
3695 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3696 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3697 paddress (get_objfile_arch (objfile), pc));
3698
9703b513
TT
3699 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3700 gdb_assert (result != NULL);
3701 return result;
9291a0cd
TT
3702}
3703
9291a0cd 3704static void
44b13c5a 3705dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3706 void *data, int need_fullname)
9291a0cd
TT
3707{
3708 int i;
24c79950
TT
3709 struct cleanup *cleanup;
3710 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3711 NULL, xcalloc, xfree);
9291a0cd 3712
24c79950 3713 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3714 dw2_setup (objfile);
ae2de4f8 3715
f4dc4d17
DE
3716 dw2_build_type_unit_groups ();
3717
24c79950
TT
3718 /* We can ignore file names coming from already-expanded CUs. */
3719 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3720 + dwarf2_per_objfile->n_type_units); ++i)
3721 {
3722 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3723
3724 if (per_cu->v.quick->symtab)
3725 {
3726 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3727 INSERT);
3728
3729 *slot = per_cu->v.quick->file_names;
3730 }
3731 }
3732
1fd400ff 3733 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3734 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3735 {
3736 int j;
f4dc4d17 3737 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3738 struct quick_file_names *file_data;
24c79950 3739 void **slot;
9291a0cd 3740
3d7bb9d9 3741 /* We only need to look at symtabs not already expanded. */
e254ef6a 3742 if (per_cu->v.quick->symtab)
9291a0cd
TT
3743 continue;
3744
7b9f3c50
DE
3745 file_data = dw2_get_file_names (objfile, per_cu);
3746 if (file_data == NULL)
9291a0cd
TT
3747 continue;
3748
24c79950
TT
3749 slot = htab_find_slot (visited, file_data, INSERT);
3750 if (*slot)
3751 {
3752 /* Already visited. */
3753 continue;
3754 }
3755 *slot = file_data;
3756
7b9f3c50 3757 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3758 {
74e2f255
DE
3759 const char *this_real_name;
3760
3761 if (need_fullname)
3762 this_real_name = dw2_get_real_path (objfile, file_data, j);
3763 else
3764 this_real_name = NULL;
7b9f3c50 3765 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3766 }
3767 }
24c79950
TT
3768
3769 do_cleanups (cleanup);
9291a0cd
TT
3770}
3771
3772static int
3773dw2_has_symbols (struct objfile *objfile)
3774{
3775 return 1;
3776}
3777
3778const struct quick_symbol_functions dwarf2_gdb_index_functions =
3779{
3780 dw2_has_symbols,
3781 dw2_find_last_source_symtab,
3782 dw2_forget_cached_source_info,
f8eba3c6 3783 dw2_map_symtabs_matching_filename,
9291a0cd 3784 dw2_lookup_symbol,
9291a0cd
TT
3785 dw2_print_stats,
3786 dw2_dump,
3787 dw2_relocate,
3788 dw2_expand_symtabs_for_function,
3789 dw2_expand_all_symtabs,
3790 dw2_expand_symtabs_with_filename,
3791 dw2_find_symbol_file,
40658b94 3792 dw2_map_matching_symbols,
9291a0cd
TT
3793 dw2_expand_symtabs_matching,
3794 dw2_find_pc_sect_symtab,
9291a0cd
TT
3795 dw2_map_symbol_filenames
3796};
3797
3798/* Initialize for reading DWARF for this objfile. Return 0 if this
3799 file will use psymtabs, or 1 if using the GNU index. */
3800
3801int
3802dwarf2_initialize_objfile (struct objfile *objfile)
3803{
3804 /* If we're about to read full symbols, don't bother with the
3805 indices. In this case we also don't care if some other debug
3806 format is making psymtabs, because they are all about to be
3807 expanded anyway. */
3808 if ((objfile->flags & OBJF_READNOW))
3809 {
3810 int i;
3811
3812 dwarf2_per_objfile->using_index = 1;
3813 create_all_comp_units (objfile);
0e50663e 3814 create_all_type_units (objfile);
7b9f3c50
DE
3815 dwarf2_per_objfile->quick_file_names_table =
3816 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3817
1fd400ff 3818 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3819 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3820 {
e254ef6a 3821 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3822
e254ef6a
DE
3823 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3824 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3825 }
3826
3827 /* Return 1 so that gdb sees the "quick" functions. However,
3828 these functions will be no-ops because we will have expanded
3829 all symtabs. */
3830 return 1;
3831 }
3832
3833 if (dwarf2_read_index (objfile))
3834 return 1;
3835
9291a0cd
TT
3836 return 0;
3837}
3838
3839\f
3840
dce234bc
PP
3841/* Build a partial symbol table. */
3842
3843void
f29dff0a 3844dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3845{
c9bf0622
TT
3846 volatile struct gdb_exception except;
3847
f29dff0a 3848 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3849 {
3850 init_psymbol_list (objfile, 1024);
3851 }
3852
c9bf0622
TT
3853 TRY_CATCH (except, RETURN_MASK_ERROR)
3854 {
3855 /* This isn't really ideal: all the data we allocate on the
3856 objfile's obstack is still uselessly kept around. However,
3857 freeing it seems unsafe. */
3858 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3859
3860 dwarf2_build_psymtabs_hard (objfile);
3861 discard_cleanups (cleanups);
3862 }
3863 if (except.reason < 0)
3864 exception_print (gdb_stderr, except);
c906108c 3865}
c906108c 3866
1ce1cefd
DE
3867/* Return the total length of the CU described by HEADER. */
3868
3869static unsigned int
3870get_cu_length (const struct comp_unit_head *header)
3871{
3872 return header->initial_length_size + header->length;
3873}
3874
45452591
DE
3875/* Return TRUE if OFFSET is within CU_HEADER. */
3876
3877static inline int
b64f50a1 3878offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3879{
b64f50a1 3880 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3881 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3882
b64f50a1 3883 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3884}
3885
3b80fe9b
DE
3886/* Find the base address of the compilation unit for range lists and
3887 location lists. It will normally be specified by DW_AT_low_pc.
3888 In DWARF-3 draft 4, the base address could be overridden by
3889 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3890 compilation units with discontinuous ranges. */
3891
3892static void
3893dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3894{
3895 struct attribute *attr;
3896
3897 cu->base_known = 0;
3898 cu->base_address = 0;
3899
3900 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3901 if (attr)
3902 {
3903 cu->base_address = DW_ADDR (attr);
3904 cu->base_known = 1;
3905 }
3906 else
3907 {
3908 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3909 if (attr)
3910 {
3911 cu->base_address = DW_ADDR (attr);
3912 cu->base_known = 1;
3913 }
3914 }
3915}
3916
93311388
DE
3917/* Read in the comp unit header information from the debug_info at info_ptr.
3918 NOTE: This leaves members offset, first_die_offset to be filled in
3919 by the caller. */
107d2387 3920
fe1b8b76 3921static gdb_byte *
107d2387 3922read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3923 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3924{
3925 int signed_addr;
891d2f0b 3926 unsigned int bytes_read;
c764a876
DE
3927
3928 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3929 cu_header->initial_length_size = bytes_read;
3930 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3931 info_ptr += bytes_read;
107d2387
AC
3932 cu_header->version = read_2_bytes (abfd, info_ptr);
3933 info_ptr += 2;
b64f50a1
JK
3934 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3935 &bytes_read);
613e1657 3936 info_ptr += bytes_read;
107d2387
AC
3937 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3938 info_ptr += 1;
3939 signed_addr = bfd_get_sign_extend_vma (abfd);
3940 if (signed_addr < 0)
8e65ff28 3941 internal_error (__FILE__, __LINE__,
e2e0b3e5 3942 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3943 cu_header->signed_addr_p = signed_addr;
c764a876 3944
107d2387
AC
3945 return info_ptr;
3946}
3947
36586728
TT
3948/* Helper function that returns the proper abbrev section for
3949 THIS_CU. */
3950
3951static struct dwarf2_section_info *
3952get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3953{
3954 struct dwarf2_section_info *abbrev;
3955
3956 if (this_cu->is_dwz)
3957 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3958 else
3959 abbrev = &dwarf2_per_objfile->abbrev;
3960
3961 return abbrev;
3962}
3963
9ff913ba
DE
3964/* Subroutine of read_and_check_comp_unit_head and
3965 read_and_check_type_unit_head to simplify them.
3966 Perform various error checking on the header. */
3967
3968static void
3969error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3970 struct dwarf2_section_info *section,
3971 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3972{
3973 bfd *abfd = section->asection->owner;
3974 const char *filename = bfd_get_filename (abfd);
3975
3976 if (header->version != 2 && header->version != 3 && header->version != 4)
3977 error (_("Dwarf Error: wrong version in compilation unit header "
3978 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3979 filename);
3980
b64f50a1 3981 if (header->abbrev_offset.sect_off
36586728 3982 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3983 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3984 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3985 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3986 filename);
3987
3988 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3989 avoid potential 32-bit overflow. */
1ce1cefd 3990 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3991 > section->size)
3992 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3993 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3994 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3995 filename);
3996}
3997
3998/* Read in a CU/TU header and perform some basic error checking.
3999 The contents of the header are stored in HEADER.
4000 The result is a pointer to the start of the first DIE. */
adabb602 4001
fe1b8b76 4002static gdb_byte *
9ff913ba
DE
4003read_and_check_comp_unit_head (struct comp_unit_head *header,
4004 struct dwarf2_section_info *section,
4bdcc0c1 4005 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
4006 gdb_byte *info_ptr,
4007 int is_debug_types_section)
72bf9492 4008{
fe1b8b76 4009 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4010 bfd *abfd = section->asection->owner;
72bf9492 4011
b64f50a1 4012 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4013
72bf9492
DJ
4014 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4015
460c1c54
CC
4016 /* If we're reading a type unit, skip over the signature and
4017 type_offset fields. */
b0df02fd 4018 if (is_debug_types_section)
460c1c54
CC
4019 info_ptr += 8 /*signature*/ + header->offset_size;
4020
b64f50a1 4021 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4022
4bdcc0c1 4023 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4024
4025 return info_ptr;
4026}
4027
348e048f
DE
4028/* Read in the types comp unit header information from .debug_types entry at
4029 types_ptr. The result is a pointer to one past the end of the header. */
4030
4031static gdb_byte *
9ff913ba
DE
4032read_and_check_type_unit_head (struct comp_unit_head *header,
4033 struct dwarf2_section_info *section,
4bdcc0c1 4034 struct dwarf2_section_info *abbrev_section,
9ff913ba 4035 gdb_byte *info_ptr,
dee91e82
DE
4036 ULONGEST *signature,
4037 cu_offset *type_offset_in_tu)
348e048f 4038{
9ff913ba
DE
4039 gdb_byte *beg_of_comp_unit = info_ptr;
4040 bfd *abfd = section->asection->owner;
348e048f 4041
b64f50a1 4042 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4043
9ff913ba 4044 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4045
9ff913ba
DE
4046 /* If we're reading a type unit, skip over the signature and
4047 type_offset fields. */
4048 if (signature != NULL)
4049 *signature = read_8_bytes (abfd, info_ptr);
4050 info_ptr += 8;
dee91e82
DE
4051 if (type_offset_in_tu != NULL)
4052 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4053 header->offset_size);
9ff913ba
DE
4054 info_ptr += header->offset_size;
4055
b64f50a1 4056 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4057
4bdcc0c1 4058 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4059
4060 return info_ptr;
348e048f
DE
4061}
4062
f4dc4d17
DE
4063/* Fetch the abbreviation table offset from a comp or type unit header. */
4064
4065static sect_offset
4066read_abbrev_offset (struct dwarf2_section_info *section,
4067 sect_offset offset)
4068{
4069 bfd *abfd = section->asection->owner;
4070 gdb_byte *info_ptr;
4071 unsigned int length, initial_length_size, offset_size;
4072 sect_offset abbrev_offset;
4073
4074 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4075 info_ptr = section->buffer + offset.sect_off;
4076 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4077 offset_size = initial_length_size == 4 ? 4 : 8;
4078 info_ptr += initial_length_size + 2 /*version*/;
4079 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4080 return abbrev_offset;
4081}
4082
aaa75496
JB
4083/* Allocate a new partial symtab for file named NAME and mark this new
4084 partial symtab as being an include of PST. */
4085
4086static void
4087dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4088 struct objfile *objfile)
4089{
4090 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4091
4092 subpst->section_offsets = pst->section_offsets;
4093 subpst->textlow = 0;
4094 subpst->texthigh = 0;
4095
4096 subpst->dependencies = (struct partial_symtab **)
4097 obstack_alloc (&objfile->objfile_obstack,
4098 sizeof (struct partial_symtab *));
4099 subpst->dependencies[0] = pst;
4100 subpst->number_of_dependencies = 1;
4101
4102 subpst->globals_offset = 0;
4103 subpst->n_global_syms = 0;
4104 subpst->statics_offset = 0;
4105 subpst->n_static_syms = 0;
4106 subpst->symtab = NULL;
4107 subpst->read_symtab = pst->read_symtab;
4108 subpst->readin = 0;
4109
4110 /* No private part is necessary for include psymtabs. This property
4111 can be used to differentiate between such include psymtabs and
10b3939b 4112 the regular ones. */
58a9656e 4113 subpst->read_symtab_private = NULL;
aaa75496
JB
4114}
4115
4116/* Read the Line Number Program data and extract the list of files
4117 included by the source file represented by PST. Build an include
d85a05f0 4118 partial symtab for each of these included files. */
aaa75496
JB
4119
4120static void
4121dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4122 struct die_info *die,
4123 struct partial_symtab *pst)
aaa75496 4124{
d85a05f0
DJ
4125 struct line_header *lh = NULL;
4126 struct attribute *attr;
aaa75496 4127
d85a05f0
DJ
4128 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4129 if (attr)
3019eac3 4130 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4131 if (lh == NULL)
4132 return; /* No linetable, so no includes. */
4133
c6da4cef 4134 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4135 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4136
4137 free_line_header (lh);
4138}
4139
348e048f 4140static hashval_t
52dc124a 4141hash_signatured_type (const void *item)
348e048f 4142{
52dc124a 4143 const struct signatured_type *sig_type = item;
9a619af0 4144
348e048f 4145 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4146 return sig_type->signature;
348e048f
DE
4147}
4148
4149static int
52dc124a 4150eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4151{
4152 const struct signatured_type *lhs = item_lhs;
4153 const struct signatured_type *rhs = item_rhs;
9a619af0 4154
348e048f
DE
4155 return lhs->signature == rhs->signature;
4156}
4157
1fd400ff
TT
4158/* Allocate a hash table for signatured types. */
4159
4160static htab_t
673bfd45 4161allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4162{
4163 return htab_create_alloc_ex (41,
52dc124a
DE
4164 hash_signatured_type,
4165 eq_signatured_type,
1fd400ff
TT
4166 NULL,
4167 &objfile->objfile_obstack,
4168 hashtab_obstack_allocate,
4169 dummy_obstack_deallocate);
4170}
4171
d467dd73 4172/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4173
4174static int
d467dd73 4175add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4176{
4177 struct signatured_type *sigt = *slot;
b4dd5633 4178 struct signatured_type ***datap = datum;
1fd400ff 4179
b4dd5633 4180 **datap = sigt;
1fd400ff
TT
4181 ++*datap;
4182
4183 return 1;
4184}
4185
3019eac3 4186/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4187 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4188 NULL otherwise.
4189 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4190 The result is a pointer to the hash table or NULL if there are
4191 no types. */
348e048f 4192
3019eac3
DE
4193static htab_t
4194create_debug_types_hash_table (struct dwo_file *dwo_file,
4195 VEC (dwarf2_section_info_def) *types)
348e048f 4196{
3019eac3 4197 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4198 htab_t types_htab = NULL;
8b70b953
TT
4199 int ix;
4200 struct dwarf2_section_info *section;
4bdcc0c1 4201 struct dwarf2_section_info *abbrev_section;
348e048f 4202
3019eac3
DE
4203 if (VEC_empty (dwarf2_section_info_def, types))
4204 return NULL;
348e048f 4205
4bdcc0c1
DE
4206 abbrev_section = (dwo_file != NULL
4207 ? &dwo_file->sections.abbrev
4208 : &dwarf2_per_objfile->abbrev);
4209
09406207
DE
4210 if (dwarf2_read_debug)
4211 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4212 dwo_file ? ".dwo" : "",
4213 bfd_get_filename (abbrev_section->asection->owner));
4214
8b70b953 4215 for (ix = 0;
3019eac3 4216 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4217 ++ix)
4218 {
3019eac3 4219 bfd *abfd;
8b70b953 4220 gdb_byte *info_ptr, *end_ptr;
36586728 4221 struct dwarf2_section_info *abbrev_section;
348e048f 4222
8b70b953
TT
4223 dwarf2_read_section (objfile, section);
4224 info_ptr = section->buffer;
348e048f 4225
8b70b953
TT
4226 if (info_ptr == NULL)
4227 continue;
348e048f 4228
3019eac3
DE
4229 /* We can't set abfd until now because the section may be empty or
4230 not present, in which case section->asection will be NULL. */
4231 abfd = section->asection->owner;
4232
36586728
TT
4233 if (dwo_file)
4234 abbrev_section = &dwo_file->sections.abbrev;
4235 else
4236 abbrev_section = &dwarf2_per_objfile->abbrev;
4237
8b70b953 4238 if (types_htab == NULL)
3019eac3
DE
4239 {
4240 if (dwo_file)
4241 types_htab = allocate_dwo_unit_table (objfile);
4242 else
4243 types_htab = allocate_signatured_type_table (objfile);
4244 }
348e048f 4245
dee91e82
DE
4246 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4247 because we don't need to read any dies: the signature is in the
4248 header. */
8b70b953
TT
4249
4250 end_ptr = info_ptr + section->size;
4251 while (info_ptr < end_ptr)
4252 {
b64f50a1 4253 sect_offset offset;
3019eac3 4254 cu_offset type_offset_in_tu;
8b70b953 4255 ULONGEST signature;
52dc124a 4256 struct signatured_type *sig_type;
3019eac3 4257 struct dwo_unit *dwo_tu;
8b70b953
TT
4258 void **slot;
4259 gdb_byte *ptr = info_ptr;
9ff913ba 4260 struct comp_unit_head header;
dee91e82 4261 unsigned int length;
348e048f 4262
b64f50a1 4263 offset.sect_off = ptr - section->buffer;
348e048f 4264
8b70b953 4265 /* We need to read the type's signature in order to build the hash
9ff913ba 4266 table, but we don't need anything else just yet. */
348e048f 4267
4bdcc0c1
DE
4268 ptr = read_and_check_type_unit_head (&header, section,
4269 abbrev_section, ptr,
3019eac3 4270 &signature, &type_offset_in_tu);
6caca83c 4271
1ce1cefd 4272 length = get_cu_length (&header);
dee91e82 4273
6caca83c 4274 /* Skip dummy type units. */
dee91e82
DE
4275 if (ptr >= info_ptr + length
4276 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4277 {
1ce1cefd 4278 info_ptr += length;
6caca83c
CC
4279 continue;
4280 }
8b70b953 4281
3019eac3
DE
4282 if (dwo_file)
4283 {
4284 sig_type = NULL;
4285 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4286 struct dwo_unit);
4287 dwo_tu->dwo_file = dwo_file;
4288 dwo_tu->signature = signature;
4289 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4290 dwo_tu->info_or_types_section = section;
4291 dwo_tu->offset = offset;
4292 dwo_tu->length = length;
4293 }
4294 else
4295 {
4296 /* N.B.: type_offset is not usable if this type uses a DWO file.
4297 The real type_offset is in the DWO file. */
4298 dwo_tu = NULL;
4299 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4300 struct signatured_type);
4301 sig_type->signature = signature;
4302 sig_type->type_offset_in_tu = type_offset_in_tu;
4303 sig_type->per_cu.objfile = objfile;
4304 sig_type->per_cu.is_debug_types = 1;
4305 sig_type->per_cu.info_or_types_section = section;
4306 sig_type->per_cu.offset = offset;
4307 sig_type->per_cu.length = length;
4308 }
8b70b953 4309
3019eac3
DE
4310 slot = htab_find_slot (types_htab,
4311 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4312 INSERT);
8b70b953
TT
4313 gdb_assert (slot != NULL);
4314 if (*slot != NULL)
4315 {
3019eac3
DE
4316 sect_offset dup_offset;
4317
4318 if (dwo_file)
4319 {
4320 const struct dwo_unit *dup_tu = *slot;
4321
4322 dup_offset = dup_tu->offset;
4323 }
4324 else
4325 {
4326 const struct signatured_type *dup_tu = *slot;
4327
4328 dup_offset = dup_tu->per_cu.offset;
4329 }
b3c8eb43 4330
8b70b953
TT
4331 complaint (&symfile_complaints,
4332 _("debug type entry at offset 0x%x is duplicate to the "
4333 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4334 offset.sect_off, dup_offset.sect_off,
8b70b953 4335 phex (signature, sizeof (signature)));
8b70b953 4336 }
3019eac3 4337 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4338
09406207 4339 if (dwarf2_read_debug)
8b70b953 4340 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4341 offset.sect_off,
4342 phex (signature, sizeof (signature)));
348e048f 4343
dee91e82 4344 info_ptr += length;
8b70b953 4345 }
348e048f
DE
4346 }
4347
3019eac3
DE
4348 return types_htab;
4349}
4350
4351/* Create the hash table of all entries in the .debug_types section,
4352 and initialize all_type_units.
4353 The result is zero if there is an error (e.g. missing .debug_types section),
4354 otherwise non-zero. */
4355
4356static int
4357create_all_type_units (struct objfile *objfile)
4358{
4359 htab_t types_htab;
b4dd5633 4360 struct signatured_type **iter;
3019eac3
DE
4361
4362 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4363 if (types_htab == NULL)
4364 {
4365 dwarf2_per_objfile->signatured_types = NULL;
4366 return 0;
4367 }
4368
348e048f
DE
4369 dwarf2_per_objfile->signatured_types = types_htab;
4370
d467dd73
DE
4371 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4372 dwarf2_per_objfile->all_type_units
1fd400ff 4373 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4374 dwarf2_per_objfile->n_type_units
b4dd5633 4375 * sizeof (struct signatured_type *));
d467dd73
DE
4376 iter = &dwarf2_per_objfile->all_type_units[0];
4377 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4378 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4379 == dwarf2_per_objfile->n_type_units);
1fd400ff 4380
348e048f
DE
4381 return 1;
4382}
4383
380bca97 4384/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4385 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4386
4387static struct signatured_type *
e319fa28 4388lookup_signatured_type (ULONGEST sig)
348e048f
DE
4389{
4390 struct signatured_type find_entry, *entry;
4391
4392 if (dwarf2_per_objfile->signatured_types == NULL)
4393 {
4394 complaint (&symfile_complaints,
55f1336d 4395 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4396 return NULL;
348e048f
DE
4397 }
4398
4399 find_entry.signature = sig;
4400 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4401 return entry;
4402}
42e7ad6c
DE
4403\f
4404/* Low level DIE reading support. */
348e048f 4405
d85a05f0
DJ
4406/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4407
4408static void
4409init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4410 struct dwarf2_cu *cu,
3019eac3
DE
4411 struct dwarf2_section_info *section,
4412 struct dwo_file *dwo_file)
d85a05f0 4413{
fceca515 4414 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4415 reader->abfd = section->asection->owner;
d85a05f0 4416 reader->cu = cu;
3019eac3 4417 reader->dwo_file = dwo_file;
dee91e82
DE
4418 reader->die_section = section;
4419 reader->buffer = section->buffer;
f664829e 4420 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4421}
4422
fd820528 4423/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4424 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4425
f4dc4d17
DE
4426 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4427 Otherwise the table specified in the comp unit header is read in and used.
4428 This is an optimization for when we already have the abbrev table.
4429
dee91e82
DE
4430 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4431 Otherwise, a new CU is allocated with xmalloc.
4432
4433 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4434 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4435
4436 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4437 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4438
70221824 4439static void
fd820528 4440init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4441 struct abbrev_table *abbrev_table,
fd820528
DE
4442 int use_existing_cu, int keep,
4443 die_reader_func_ftype *die_reader_func,
4444 void *data)
c906108c 4445{
dee91e82 4446 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4447 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4448 bfd *abfd = section->asection->owner;
dee91e82
DE
4449 struct dwarf2_cu *cu;
4450 gdb_byte *begin_info_ptr, *info_ptr;
4451 struct die_reader_specs reader;
d85a05f0 4452 struct die_info *comp_unit_die;
dee91e82 4453 int has_children;
d85a05f0 4454 struct attribute *attr;
dee91e82
DE
4455 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4456 struct signatured_type *sig_type = NULL;
4bdcc0c1 4457 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4458 /* Non-zero if CU currently points to a DWO file and we need to
4459 reread it. When this happens we need to reread the skeleton die
4460 before we can reread the DWO file. */
4461 int rereading_dwo_cu = 0;
c906108c 4462
09406207
DE
4463 if (dwarf2_die_debug)
4464 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4465 this_cu->is_debug_types ? "type" : "comp",
4466 this_cu->offset.sect_off);
4467
dee91e82
DE
4468 if (use_existing_cu)
4469 gdb_assert (keep);
23745b47 4470
dee91e82
DE
4471 cleanups = make_cleanup (null_cleanup, NULL);
4472
4473 /* This is cheap if the section is already read in. */
4474 dwarf2_read_section (objfile, section);
4475
4476 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4477
4478 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4479
4480 if (use_existing_cu && this_cu->cu != NULL)
4481 {
4482 cu = this_cu->cu;
42e7ad6c
DE
4483
4484 /* If this CU is from a DWO file we need to start over, we need to
4485 refetch the attributes from the skeleton CU.
4486 This could be optimized by retrieving those attributes from when we
4487 were here the first time: the previous comp_unit_die was stored in
4488 comp_unit_obstack. But there's no data yet that we need this
4489 optimization. */
4490 if (cu->dwo_unit != NULL)
4491 rereading_dwo_cu = 1;
dee91e82
DE
4492 }
4493 else
4494 {
4495 /* If !use_existing_cu, this_cu->cu must be NULL. */
4496 gdb_assert (this_cu->cu == NULL);
4497
4498 cu = xmalloc (sizeof (*cu));
4499 init_one_comp_unit (cu, this_cu);
4500
4501 /* If an error occurs while loading, release our storage. */
4502 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4503 }
dee91e82 4504
42e7ad6c
DE
4505 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4506 {
4507 /* We already have the header, there's no need to read it in again. */
4508 info_ptr += cu->header.first_die_offset.cu_off;
4509 }
4510 else
4511 {
3019eac3 4512 if (this_cu->is_debug_types)
dee91e82
DE
4513 {
4514 ULONGEST signature;
42e7ad6c 4515 cu_offset type_offset_in_tu;
dee91e82 4516
4bdcc0c1
DE
4517 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4518 abbrev_section, info_ptr,
42e7ad6c
DE
4519 &signature,
4520 &type_offset_in_tu);
dee91e82 4521
42e7ad6c
DE
4522 /* Since per_cu is the first member of struct signatured_type,
4523 we can go from a pointer to one to a pointer to the other. */
4524 sig_type = (struct signatured_type *) this_cu;
4525 gdb_assert (sig_type->signature == signature);
4526 gdb_assert (sig_type->type_offset_in_tu.cu_off
4527 == type_offset_in_tu.cu_off);
dee91e82
DE
4528 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4529
42e7ad6c
DE
4530 /* LENGTH has not been set yet for type units if we're
4531 using .gdb_index. */
1ce1cefd 4532 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4533
4534 /* Establish the type offset that can be used to lookup the type. */
4535 sig_type->type_offset_in_section.sect_off =
4536 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4537 }
4538 else
4539 {
4bdcc0c1
DE
4540 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4541 abbrev_section,
4542 info_ptr, 0);
dee91e82
DE
4543
4544 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4545 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4546 }
4547 }
10b3939b 4548
6caca83c 4549 /* Skip dummy compilation units. */
dee91e82 4550 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4551 || peek_abbrev_code (abfd, info_ptr) == 0)
4552 {
dee91e82 4553 do_cleanups (cleanups);
21b2bd31 4554 return;
6caca83c
CC
4555 }
4556
433df2d4
DE
4557 /* If we don't have them yet, read the abbrevs for this compilation unit.
4558 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4559 done. Note that it's important that if the CU had an abbrev table
4560 on entry we don't free it when we're done: Somewhere up the call stack
4561 it may be in use. */
f4dc4d17
DE
4562 if (abbrev_table != NULL)
4563 {
4564 gdb_assert (cu->abbrev_table == NULL);
4565 gdb_assert (cu->header.abbrev_offset.sect_off
4566 == abbrev_table->offset.sect_off);
4567 cu->abbrev_table = abbrev_table;
4568 }
4569 else if (cu->abbrev_table == NULL)
dee91e82 4570 {
4bdcc0c1 4571 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4572 make_cleanup (dwarf2_free_abbrev_table, cu);
4573 }
42e7ad6c
DE
4574 else if (rereading_dwo_cu)
4575 {
4576 dwarf2_free_abbrev_table (cu);
4577 dwarf2_read_abbrevs (cu, abbrev_section);
4578 }
af703f96 4579
dee91e82 4580 /* Read the top level CU/TU die. */
3019eac3 4581 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4582 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4583
3019eac3
DE
4584 /* If we have a DWO stub, process it and then read in the DWO file.
4585 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4586 a DWO CU, that this test will fail. */
4587 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4588 if (attr)
4589 {
4590 char *dwo_name = DW_STRING (attr);
42e7ad6c 4591 const char *comp_dir_string;
3019eac3
DE
4592 struct dwo_unit *dwo_unit;
4593 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4594 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4595 int i,num_extra_attrs;
4bdcc0c1 4596 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4597
4598 if (has_children)
4599 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4600 " has children (offset 0x%x) [in module %s]"),
4601 this_cu->offset.sect_off, bfd_get_filename (abfd));
4602
4603 /* These attributes aren't processed until later:
4604 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4605 However, the attribute is found in the stub which we won't have later.
4606 In order to not impose this complication on the rest of the code,
4607 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4608
4609 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4610 DWO file. */
42e7ad6c 4611 stmt_list = NULL;
3019eac3
DE
4612 if (! this_cu->is_debug_types)
4613 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4614 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4615 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4616 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4617 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4618
4619 /* There should be a DW_AT_addr_base attribute here (if needed).
4620 We need the value before we can process DW_FORM_GNU_addr_index. */
4621 cu->addr_base = 0;
3019eac3
DE
4622 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4623 if (attr)
2e3cf129
DE
4624 cu->addr_base = DW_UNSND (attr);
4625
4626 /* There should be a DW_AT_ranges_base attribute here (if needed).
4627 We need the value before we can process DW_AT_ranges. */
4628 cu->ranges_base = 0;
4629 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4630 if (attr)
4631 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4632
4633 if (this_cu->is_debug_types)
4634 {
4635 gdb_assert (sig_type != NULL);
4636 signature = sig_type->signature;
4637 }
4638 else
4639 {
4640 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4641 if (! attr)
4642 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4643 dwo_name);
4644 signature = DW_UNSND (attr);
4645 }
4646
4647 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4648 comp_dir_string = NULL;
4649 if (comp_dir)
4650 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4651
4652 if (this_cu->is_debug_types)
42e7ad6c 4653 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4654 else
42e7ad6c 4655 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4656 signature);
4657
4658 if (dwo_unit == NULL)
4659 {
4660 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4661 " with ID %s [in module %s]"),
4662 this_cu->offset.sect_off,
4663 phex (signature, sizeof (signature)),
4664 objfile->name);
4665 }
4666
4667 /* Set up for reading the DWO CU/TU. */
4668 cu->dwo_unit = dwo_unit;
4669 section = dwo_unit->info_or_types_section;
80626a55 4670 dwarf2_read_section (objfile, section);
3019eac3 4671 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4672 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4673 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4674
4675 if (this_cu->is_debug_types)
4676 {
4677 ULONGEST signature;
80626a55 4678 cu_offset type_offset_in_tu;
3019eac3 4679
4bdcc0c1
DE
4680 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4681 dwo_abbrev_section,
4682 info_ptr,
80626a55
DE
4683 &signature,
4684 &type_offset_in_tu);
3019eac3
DE
4685 gdb_assert (sig_type->signature == signature);
4686 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4687 /* For DWOs coming from DWP files, we don't know the CU length
4688 nor the type's offset in the TU until now. */
4689 dwo_unit->length = get_cu_length (&cu->header);
4690 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4691
4692 /* Establish the type offset that can be used to lookup the type.
4693 For DWO files, we don't know it until now. */
4694 sig_type->type_offset_in_section.sect_off =
4695 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4696 }
4697 else
4698 {
4bdcc0c1
DE
4699 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4700 dwo_abbrev_section,
4701 info_ptr, 0);
3019eac3 4702 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4703 /* For DWOs coming from DWP files, we don't know the CU length
4704 until now. */
4705 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4706 }
4707
4708 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4709 if (abbrev_table == NULL)
4710 {
4711 dwarf2_free_abbrev_table (cu);
4712 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4713 }
4714 else
4715 {
4716 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4717 make_cleanup (dwarf2_free_abbrev_table, cu);
4718 }
3019eac3
DE
4719
4720 /* Read in the die, but leave space to copy over the attributes
4721 from the stub. This has the benefit of simplifying the rest of
4722 the code - all the real work is done here. */
4723 num_extra_attrs = ((stmt_list != NULL)
4724 + (low_pc != NULL)
4725 + (high_pc != NULL)
42e7ad6c
DE
4726 + (ranges != NULL)
4727 + (comp_dir != NULL));
3019eac3
DE
4728 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4729 &has_children, num_extra_attrs);
4730
4731 /* Copy over the attributes from the stub to the DWO die. */
4732 i = comp_unit_die->num_attrs;
4733 if (stmt_list != NULL)
4734 comp_unit_die->attrs[i++] = *stmt_list;
4735 if (low_pc != NULL)
4736 comp_unit_die->attrs[i++] = *low_pc;
4737 if (high_pc != NULL)
4738 comp_unit_die->attrs[i++] = *high_pc;
4739 if (ranges != NULL)
4740 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4741 if (comp_dir != NULL)
4742 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4743 comp_unit_die->num_attrs += num_extra_attrs;
4744
4745 /* Skip dummy compilation units. */
4746 if (info_ptr >= begin_info_ptr + dwo_unit->length
4747 || peek_abbrev_code (abfd, info_ptr) == 0)
4748 {
4749 do_cleanups (cleanups);
4750 return;
4751 }
4752 }
4753
dee91e82
DE
4754 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4755
4756 if (free_cu_cleanup != NULL)
348e048f 4757 {
dee91e82
DE
4758 if (keep)
4759 {
4760 /* We've successfully allocated this compilation unit. Let our
4761 caller clean it up when finished with it. */
4762 discard_cleanups (free_cu_cleanup);
4763
4764 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4765 So we have to manually free the abbrev table. */
4766 dwarf2_free_abbrev_table (cu);
4767
4768 /* Link this CU into read_in_chain. */
4769 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4770 dwarf2_per_objfile->read_in_chain = this_cu;
4771 }
4772 else
4773 do_cleanups (free_cu_cleanup);
348e048f 4774 }
dee91e82
DE
4775
4776 do_cleanups (cleanups);
4777}
4778
3019eac3
DE
4779/* Read CU/TU THIS_CU in section SECTION,
4780 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4781 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4782 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4783
4784 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4785 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4786
4787 We fill in THIS_CU->length.
4788
4789 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4790 linker) then DIE_READER_FUNC will not get called.
4791
4792 THIS_CU->cu is always freed when done.
3019eac3
DE
4793 This is done in order to not leave THIS_CU->cu in a state where we have
4794 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4795
4796static void
4797init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4798 struct dwarf2_section_info *abbrev_section,
3019eac3 4799 struct dwo_file *dwo_file,
dee91e82
DE
4800 die_reader_func_ftype *die_reader_func,
4801 void *data)
4802{
4803 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4804 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4805 bfd *abfd = section->asection->owner;
dee91e82
DE
4806 struct dwarf2_cu cu;
4807 gdb_byte *begin_info_ptr, *info_ptr;
4808 struct die_reader_specs reader;
4809 struct cleanup *cleanups;
4810 struct die_info *comp_unit_die;
4811 int has_children;
4812
09406207
DE
4813 if (dwarf2_die_debug)
4814 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4815 this_cu->is_debug_types ? "type" : "comp",
4816 this_cu->offset.sect_off);
4817
dee91e82
DE
4818 gdb_assert (this_cu->cu == NULL);
4819
dee91e82
DE
4820 /* This is cheap if the section is already read in. */
4821 dwarf2_read_section (objfile, section);
4822
4823 init_one_comp_unit (&cu, this_cu);
4824
4825 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4826
4827 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4828 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4829 abbrev_section, info_ptr,
3019eac3 4830 this_cu->is_debug_types);
dee91e82 4831
1ce1cefd 4832 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4833
4834 /* Skip dummy compilation units. */
4835 if (info_ptr >= begin_info_ptr + this_cu->length
4836 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4837 {
dee91e82 4838 do_cleanups (cleanups);
21b2bd31 4839 return;
93311388 4840 }
72bf9492 4841
dee91e82
DE
4842 dwarf2_read_abbrevs (&cu, abbrev_section);
4843 make_cleanup (dwarf2_free_abbrev_table, &cu);
4844
3019eac3 4845 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4846 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4847
4848 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4849
4850 do_cleanups (cleanups);
4851}
4852
3019eac3
DE
4853/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4854 does not lookup the specified DWO file.
4855 This cannot be used to read DWO files.
dee91e82
DE
4856
4857 THIS_CU->cu is always freed when done.
3019eac3
DE
4858 This is done in order to not leave THIS_CU->cu in a state where we have
4859 to care whether it refers to the "main" CU or the DWO CU.
4860 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4861
4862static void
4863init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4864 die_reader_func_ftype *die_reader_func,
4865 void *data)
4866{
4867 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4868 get_abbrev_section_for_cu (this_cu),
3019eac3 4869 NULL,
dee91e82
DE
4870 die_reader_func, data);
4871}
4872
f4dc4d17
DE
4873/* Create a psymtab named NAME and assign it to PER_CU.
4874
4875 The caller must fill in the following details:
4876 dirname, textlow, texthigh. */
4877
4878static struct partial_symtab *
4879create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4880{
4881 struct objfile *objfile = per_cu->objfile;
4882 struct partial_symtab *pst;
4883
4884 pst = start_psymtab_common (objfile, objfile->section_offsets,
4885 name, 0,
4886 objfile->global_psymbols.next,
4887 objfile->static_psymbols.next);
4888
4889 pst->psymtabs_addrmap_supported = 1;
4890
4891 /* This is the glue that links PST into GDB's symbol API. */
4892 pst->read_symtab_private = per_cu;
4893 pst->read_symtab = dwarf2_psymtab_to_symtab;
4894 per_cu->v.psymtab = pst;
4895
4896 return pst;
4897}
4898
dee91e82
DE
4899/* die_reader_func for process_psymtab_comp_unit. */
4900
4901static void
4902process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4903 gdb_byte *info_ptr,
4904 struct die_info *comp_unit_die,
4905 int has_children,
4906 void *data)
4907{
4908 struct dwarf2_cu *cu = reader->cu;
4909 struct objfile *objfile = cu->objfile;
4910 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4911 struct attribute *attr;
4912 CORE_ADDR baseaddr;
4913 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4914 struct partial_symtab *pst;
4915 int has_pc_info;
4916 const char *filename;
95554aad 4917 int *want_partial_unit_ptr = data;
dee91e82 4918
95554aad
TT
4919 if (comp_unit_die->tag == DW_TAG_partial_unit
4920 && (want_partial_unit_ptr == NULL
4921 || !*want_partial_unit_ptr))
dee91e82
DE
4922 return;
4923
f4dc4d17
DE
4924 gdb_assert (! per_cu->is_debug_types);
4925
95554aad 4926 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4927
4928 cu->list_in_scope = &file_symbols;
c906108c 4929
93311388 4930 /* Allocate a new partial symbol table structure. */
dee91e82 4931 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4932 if (attr == NULL || !DW_STRING (attr))
4933 filename = "";
4934 else
4935 filename = DW_STRING (attr);
72bf9492 4936
f4dc4d17
DE
4937 pst = create_partial_symtab (per_cu, filename);
4938
4939 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4940 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4941 if (attr != NULL)
4942 pst->dirname = DW_STRING (attr);
72bf9492 4943
93311388 4944 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4945
dee91e82 4946 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4947
93311388
DE
4948 /* Possibly set the default values of LOWPC and HIGHPC from
4949 `DW_AT_ranges'. */
d85a05f0 4950 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4951 &best_highpc, cu, pst);
d85a05f0 4952 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4953 /* Store the contiguous range if it is not empty; it can be empty for
4954 CUs with no code. */
4955 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4956 best_lowpc + baseaddr,
4957 best_highpc + baseaddr - 1, pst);
93311388
DE
4958
4959 /* Check if comp unit has_children.
4960 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4961 If not, there's no more debug_info for this comp unit. */
d85a05f0 4962 if (has_children)
93311388
DE
4963 {
4964 struct partial_die_info *first_die;
4965 CORE_ADDR lowpc, highpc;
31ffec48 4966
93311388
DE
4967 lowpc = ((CORE_ADDR) -1);
4968 highpc = ((CORE_ADDR) 0);
c906108c 4969
dee91e82 4970 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4971
93311388 4972 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4973 ! has_pc_info, cu);
57c22c6c 4974
93311388
DE
4975 /* If we didn't find a lowpc, set it to highpc to avoid
4976 complaints from `maint check'. */
4977 if (lowpc == ((CORE_ADDR) -1))
4978 lowpc = highpc;
10b3939b 4979
93311388
DE
4980 /* If the compilation unit didn't have an explicit address range,
4981 then use the information extracted from its child dies. */
d85a05f0 4982 if (! has_pc_info)
93311388 4983 {
d85a05f0
DJ
4984 best_lowpc = lowpc;
4985 best_highpc = highpc;
93311388
DE
4986 }
4987 }
d85a05f0
DJ
4988 pst->textlow = best_lowpc + baseaddr;
4989 pst->texthigh = best_highpc + baseaddr;
c906108c 4990
93311388
DE
4991 pst->n_global_syms = objfile->global_psymbols.next -
4992 (objfile->global_psymbols.list + pst->globals_offset);
4993 pst->n_static_syms = objfile->static_psymbols.next -
4994 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 4995 sort_pst_symbols (objfile, pst);
c906108c 4996
f4dc4d17 4997 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4998 {
4999 int i;
f4dc4d17 5000 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
5001 struct dwarf2_per_cu_data *iter;
5002
5003 /* Fill in 'dependencies' here; we fill in 'users' in a
5004 post-pass. */
5005 pst->number_of_dependencies = len;
5006 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5007 len * sizeof (struct symtab *));
5008 for (i = 0;
f4dc4d17 5009 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
5010 i, iter);
5011 ++i)
5012 pst->dependencies[i] = iter->v.psymtab;
5013
f4dc4d17 5014 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
5015 }
5016
f4dc4d17
DE
5017 /* Get the list of files included in the current compilation unit,
5018 and build a psymtab for each of them. */
5019 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
5020
5021 if (dwarf2_read_debug)
5022 {
5023 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5024
5025 fprintf_unfiltered (gdb_stdlog,
844226d6 5026 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
5027 ", %d global, %d static syms\n",
5028 per_cu->is_debug_types ? "type" : "comp",
5029 per_cu->offset.sect_off,
5030 paddress (gdbarch, pst->textlow),
5031 paddress (gdbarch, pst->texthigh),
5032 pst->n_global_syms, pst->n_static_syms);
5033 }
dee91e82 5034}
ae038cb0 5035
dee91e82
DE
5036/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5037 Process compilation unit THIS_CU for a psymtab. */
5038
5039static void
95554aad
TT
5040process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5041 int want_partial_unit)
dee91e82
DE
5042{
5043 /* If this compilation unit was already read in, free the
5044 cached copy in order to read it in again. This is
5045 necessary because we skipped some symbols when we first
5046 read in the compilation unit (see load_partial_dies).
5047 This problem could be avoided, but the benefit is unclear. */
5048 if (this_cu->cu != NULL)
5049 free_one_cached_comp_unit (this_cu);
5050
3019eac3 5051 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
5052 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5053 process_psymtab_comp_unit_reader,
95554aad 5054 &want_partial_unit);
dee91e82
DE
5055
5056 /* Age out any secondary CUs. */
5057 age_cached_comp_units ();
93311388 5058}
ff013f42 5059
f4dc4d17
DE
5060static hashval_t
5061hash_type_unit_group (const void *item)
5062{
094b34ac 5063 const struct type_unit_group *tu_group = item;
f4dc4d17 5064
094b34ac 5065 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5066}
348e048f
DE
5067
5068static int
f4dc4d17 5069eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5070{
f4dc4d17
DE
5071 const struct type_unit_group *lhs = item_lhs;
5072 const struct type_unit_group *rhs = item_rhs;
348e048f 5073
094b34ac 5074 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5075}
348e048f 5076
f4dc4d17
DE
5077/* Allocate a hash table for type unit groups. */
5078
5079static htab_t
5080allocate_type_unit_groups_table (void)
5081{
5082 return htab_create_alloc_ex (3,
5083 hash_type_unit_group,
5084 eq_type_unit_group,
5085 NULL,
5086 &dwarf2_per_objfile->objfile->objfile_obstack,
5087 hashtab_obstack_allocate,
5088 dummy_obstack_deallocate);
5089}
dee91e82 5090
f4dc4d17
DE
5091/* Type units that don't have DW_AT_stmt_list are grouped into their own
5092 partial symtabs. We combine several TUs per psymtab to not let the size
5093 of any one psymtab grow too big. */
5094#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5095#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5096
094b34ac 5097/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5098 Create the type_unit_group object used to hold one or more TUs. */
5099
5100static struct type_unit_group *
094b34ac 5101create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5102{
5103 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5104 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5105 struct type_unit_group *tu_group;
f4dc4d17
DE
5106
5107 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5108 struct type_unit_group);
094b34ac 5109 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5110 per_cu->objfile = objfile;
5111 per_cu->is_debug_types = 1;
5112 per_cu->s.type_unit_group = tu_group;
5113
094b34ac
DE
5114 if (dwarf2_per_objfile->using_index)
5115 {
5116 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5117 struct dwarf2_per_cu_quick_data);
5118 tu_group->t.first_tu = cu->per_cu;
5119 }
5120 else
5121 {
5122 unsigned int line_offset = line_offset_struct.sect_off;
5123 struct partial_symtab *pst;
5124 char *name;
5125
5126 /* Give the symtab a useful name for debug purposes. */
5127 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5128 name = xstrprintf ("<type_units_%d>",
5129 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5130 else
5131 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5132
5133 pst = create_partial_symtab (per_cu, name);
5134 pst->anonymous = 1;
f4dc4d17 5135
094b34ac
DE
5136 xfree (name);
5137 }
f4dc4d17 5138
094b34ac
DE
5139 tu_group->hash.dwo_unit = cu->dwo_unit;
5140 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5141
5142 return tu_group;
5143}
5144
094b34ac
DE
5145/* Look up the type_unit_group for type unit CU, and create it if necessary.
5146 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5147
5148static struct type_unit_group *
094b34ac 5149get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5150{
5151 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5152 struct type_unit_group *tu_group;
5153 void **slot;
5154 unsigned int line_offset;
5155 struct type_unit_group type_unit_group_for_lookup;
5156
5157 if (dwarf2_per_objfile->type_unit_groups == NULL)
5158 {
5159 dwarf2_per_objfile->type_unit_groups =
5160 allocate_type_unit_groups_table ();
5161 }
5162
5163 /* Do we need to create a new group, or can we use an existing one? */
5164
5165 if (stmt_list)
5166 {
5167 line_offset = DW_UNSND (stmt_list);
5168 ++tu_stats->nr_symtab_sharers;
5169 }
5170 else
5171 {
5172 /* Ugh, no stmt_list. Rare, but we have to handle it.
5173 We can do various things here like create one group per TU or
5174 spread them over multiple groups to split up the expansion work.
5175 To avoid worst case scenarios (too many groups or too large groups)
5176 we, umm, group them in bunches. */
5177 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5178 | (tu_stats->nr_stmt_less_type_units
5179 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5180 ++tu_stats->nr_stmt_less_type_units;
5181 }
5182
094b34ac
DE
5183 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5184 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5185 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5186 &type_unit_group_for_lookup, INSERT);
5187 if (*slot != NULL)
5188 {
5189 tu_group = *slot;
5190 gdb_assert (tu_group != NULL);
5191 }
5192 else
5193 {
5194 sect_offset line_offset_struct;
5195
5196 line_offset_struct.sect_off = line_offset;
094b34ac 5197 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5198 *slot = tu_group;
5199 ++tu_stats->nr_symtabs;
5200 }
5201
5202 return tu_group;
5203}
5204
5205/* Struct used to sort TUs by their abbreviation table offset. */
5206
5207struct tu_abbrev_offset
5208{
5209 struct signatured_type *sig_type;
5210 sect_offset abbrev_offset;
5211};
5212
5213/* Helper routine for build_type_unit_groups, passed to qsort. */
5214
5215static int
5216sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5217{
5218 const struct tu_abbrev_offset * const *a = ap;
5219 const struct tu_abbrev_offset * const *b = bp;
5220 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5221 unsigned int boff = (*b)->abbrev_offset.sect_off;
5222
5223 return (aoff > boff) - (aoff < boff);
5224}
5225
5226/* A helper function to add a type_unit_group to a table. */
5227
5228static int
5229add_type_unit_group_to_table (void **slot, void *datum)
5230{
5231 struct type_unit_group *tu_group = *slot;
5232 struct type_unit_group ***datap = datum;
5233
5234 **datap = tu_group;
5235 ++*datap;
5236
5237 return 1;
5238}
5239
5240/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5241 each one passing FUNC,DATA.
5242
5243 The efficiency is because we sort TUs by the abbrev table they use and
5244 only read each abbrev table once. In one program there are 200K TUs
5245 sharing 8K abbrev tables.
5246
5247 The main purpose of this function is to support building the
5248 dwarf2_per_objfile->type_unit_groups table.
5249 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5250 can collapse the search space by grouping them by stmt_list.
5251 The savings can be significant, in the same program from above the 200K TUs
5252 share 8K stmt_list tables.
5253
5254 FUNC is expected to call get_type_unit_group, which will create the
5255 struct type_unit_group if necessary and add it to
5256 dwarf2_per_objfile->type_unit_groups. */
5257
5258static void
5259build_type_unit_groups (die_reader_func_ftype *func, void *data)
5260{
5261 struct objfile *objfile = dwarf2_per_objfile->objfile;
5262 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5263 struct cleanup *cleanups;
5264 struct abbrev_table *abbrev_table;
5265 sect_offset abbrev_offset;
5266 struct tu_abbrev_offset *sorted_by_abbrev;
5267 struct type_unit_group **iter;
5268 int i;
5269
5270 /* It's up to the caller to not call us multiple times. */
5271 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5272
5273 if (dwarf2_per_objfile->n_type_units == 0)
5274 return;
5275
5276 /* TUs typically share abbrev tables, and there can be way more TUs than
5277 abbrev tables. Sort by abbrev table to reduce the number of times we
5278 read each abbrev table in.
5279 Alternatives are to punt or to maintain a cache of abbrev tables.
5280 This is simpler and efficient enough for now.
5281
5282 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5283 symtab to use). Typically TUs with the same abbrev offset have the same
5284 stmt_list value too so in practice this should work well.
5285
5286 The basic algorithm here is:
5287
5288 sort TUs by abbrev table
5289 for each TU with same abbrev table:
5290 read abbrev table if first user
5291 read TU top level DIE
5292 [IWBN if DWO skeletons had DW_AT_stmt_list]
5293 call FUNC */
5294
5295 if (dwarf2_read_debug)
5296 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5297
5298 /* Sort in a separate table to maintain the order of all_type_units
5299 for .gdb_index: TU indices directly index all_type_units. */
5300 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5301 dwarf2_per_objfile->n_type_units);
5302 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5303 {
5304 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5305
5306 sorted_by_abbrev[i].sig_type = sig_type;
5307 sorted_by_abbrev[i].abbrev_offset =
5308 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5309 sig_type->per_cu.offset);
5310 }
5311 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5312 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5313 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5314
094b34ac
DE
5315 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5316 called any number of times, so we don't reset tu_stats here. */
5317
f4dc4d17
DE
5318 abbrev_offset.sect_off = ~(unsigned) 0;
5319 abbrev_table = NULL;
5320 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5321
5322 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5323 {
5324 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5325
5326 /* Switch to the next abbrev table if necessary. */
5327 if (abbrev_table == NULL
5328 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5329 {
5330 if (abbrev_table != NULL)
5331 {
5332 abbrev_table_free (abbrev_table);
5333 /* Reset to NULL in case abbrev_table_read_table throws
5334 an error: abbrev_table_free_cleanup will get called. */
5335 abbrev_table = NULL;
5336 }
5337 abbrev_offset = tu->abbrev_offset;
5338 abbrev_table =
5339 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5340 abbrev_offset);
5341 ++tu_stats->nr_uniq_abbrev_tables;
5342 }
5343
5344 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5345 func, data);
5346 }
5347
5348 /* Create a vector of pointers to primary type units to make it easy to
5349 iterate over them and CUs. See dw2_get_primary_cu. */
5350 dwarf2_per_objfile->n_type_unit_groups =
5351 htab_elements (dwarf2_per_objfile->type_unit_groups);
5352 dwarf2_per_objfile->all_type_unit_groups =
5353 obstack_alloc (&objfile->objfile_obstack,
5354 dwarf2_per_objfile->n_type_unit_groups
5355 * sizeof (struct type_unit_group *));
5356 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5357 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5358 add_type_unit_group_to_table, &iter);
5359 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5360 == dwarf2_per_objfile->n_type_unit_groups);
5361
5362 do_cleanups (cleanups);
5363
5364 if (dwarf2_read_debug)
5365 {
5366 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5367 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5368 dwarf2_per_objfile->n_type_units);
5369 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5370 tu_stats->nr_uniq_abbrev_tables);
5371 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5372 tu_stats->nr_symtabs);
5373 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5374 tu_stats->nr_symtab_sharers);
5375 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5376 tu_stats->nr_stmt_less_type_units);
5377 }
5378}
5379
5380/* Reader function for build_type_psymtabs. */
5381
5382static void
5383build_type_psymtabs_reader (const struct die_reader_specs *reader,
5384 gdb_byte *info_ptr,
5385 struct die_info *type_unit_die,
5386 int has_children,
5387 void *data)
5388{
5389 struct objfile *objfile = dwarf2_per_objfile->objfile;
5390 struct dwarf2_cu *cu = reader->cu;
5391 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5392 struct type_unit_group *tu_group;
5393 struct attribute *attr;
5394 struct partial_die_info *first_die;
5395 CORE_ADDR lowpc, highpc;
5396 struct partial_symtab *pst;
5397
5398 gdb_assert (data == NULL);
5399
5400 if (! has_children)
5401 return;
5402
5403 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5404 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5405
094b34ac 5406 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5407
5408 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5409 cu->list_in_scope = &file_symbols;
5410 pst = create_partial_symtab (per_cu, "");
5411 pst->anonymous = 1;
5412
5413 first_die = load_partial_dies (reader, info_ptr, 1);
5414
5415 lowpc = (CORE_ADDR) -1;
5416 highpc = (CORE_ADDR) 0;
5417 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5418
5419 pst->n_global_syms = objfile->global_psymbols.next -
5420 (objfile->global_psymbols.list + pst->globals_offset);
5421 pst->n_static_syms = objfile->static_psymbols.next -
5422 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5423 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5424}
5425
5426/* Traversal function for build_type_psymtabs. */
5427
5428static int
5429build_type_psymtab_dependencies (void **slot, void *info)
5430{
5431 struct objfile *objfile = dwarf2_per_objfile->objfile;
5432 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5433 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5434 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5435 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5436 struct dwarf2_per_cu_data *iter;
5437 int i;
5438
5439 gdb_assert (len > 0);
5440
5441 pst->number_of_dependencies = len;
5442 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5443 len * sizeof (struct psymtab *));
5444 for (i = 0;
094b34ac 5445 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5446 ++i)
5447 {
5448 pst->dependencies[i] = iter->v.psymtab;
5449 iter->s.type_unit_group = tu_group;
5450 }
5451
094b34ac 5452 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5453
5454 return 1;
5455}
5456
5457/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5458 Build partial symbol tables for the .debug_types comp-units. */
5459
5460static void
5461build_type_psymtabs (struct objfile *objfile)
5462{
0e50663e 5463 if (! create_all_type_units (objfile))
348e048f
DE
5464 return;
5465
f4dc4d17
DE
5466 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5467
5468 /* Now that all TUs have been processed we can fill in the dependencies. */
5469 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5470 build_type_psymtab_dependencies, NULL);
348e048f
DE
5471}
5472
60606b2c
TT
5473/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5474
5475static void
5476psymtabs_addrmap_cleanup (void *o)
5477{
5478 struct objfile *objfile = o;
ec61707d 5479
60606b2c
TT
5480 objfile->psymtabs_addrmap = NULL;
5481}
5482
95554aad
TT
5483/* Compute the 'user' field for each psymtab in OBJFILE. */
5484
5485static void
5486set_partial_user (struct objfile *objfile)
5487{
5488 int i;
5489
5490 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5491 {
5492 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5493 struct partial_symtab *pst = per_cu->v.psymtab;
5494 int j;
5495
36586728
TT
5496 if (pst == NULL)
5497 continue;
5498
95554aad
TT
5499 for (j = 0; j < pst->number_of_dependencies; ++j)
5500 {
5501 /* Set the 'user' field only if it is not already set. */
5502 if (pst->dependencies[j]->user == NULL)
5503 pst->dependencies[j]->user = pst;
5504 }
5505 }
5506}
5507
93311388
DE
5508/* Build the partial symbol table by doing a quick pass through the
5509 .debug_info and .debug_abbrev sections. */
72bf9492 5510
93311388 5511static void
c67a9c90 5512dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5513{
60606b2c
TT
5514 struct cleanup *back_to, *addrmap_cleanup;
5515 struct obstack temp_obstack;
21b2bd31 5516 int i;
93311388 5517
45cfd468
DE
5518 if (dwarf2_read_debug)
5519 {
5520 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5521 objfile->name);
5522 }
5523
98bfdba5
PA
5524 dwarf2_per_objfile->reading_partial_symbols = 1;
5525
be391dca 5526 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5527
93311388
DE
5528 /* Any cached compilation units will be linked by the per-objfile
5529 read_in_chain. Make sure to free them when we're done. */
5530 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5531
348e048f
DE
5532 build_type_psymtabs (objfile);
5533
93311388 5534 create_all_comp_units (objfile);
c906108c 5535
60606b2c
TT
5536 /* Create a temporary address map on a temporary obstack. We later
5537 copy this to the final obstack. */
5538 obstack_init (&temp_obstack);
5539 make_cleanup_obstack_free (&temp_obstack);
5540 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5541 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5542
21b2bd31 5543 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5544 {
21b2bd31 5545 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5546
95554aad 5547 process_psymtab_comp_unit (per_cu, 0);
c906108c 5548 }
ff013f42 5549
95554aad
TT
5550 set_partial_user (objfile);
5551
ff013f42
JK
5552 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5553 &objfile->objfile_obstack);
60606b2c 5554 discard_cleanups (addrmap_cleanup);
ff013f42 5555
ae038cb0 5556 do_cleanups (back_to);
45cfd468
DE
5557
5558 if (dwarf2_read_debug)
5559 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5560 objfile->name);
ae038cb0
DJ
5561}
5562
3019eac3 5563/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5564
5565static void
dee91e82
DE
5566load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5567 gdb_byte *info_ptr,
5568 struct die_info *comp_unit_die,
5569 int has_children,
5570 void *data)
ae038cb0 5571{
dee91e82 5572 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5573
95554aad 5574 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5575
ae038cb0
DJ
5576 /* Check if comp unit has_children.
5577 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5578 If not, there's no more debug_info for this comp unit. */
d85a05f0 5579 if (has_children)
dee91e82
DE
5580 load_partial_dies (reader, info_ptr, 0);
5581}
98bfdba5 5582
dee91e82
DE
5583/* Load the partial DIEs for a secondary CU into memory.
5584 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5585
dee91e82
DE
5586static void
5587load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5588{
f4dc4d17
DE
5589 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5590 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5591}
5592
ae038cb0 5593static void
36586728
TT
5594read_comp_units_from_section (struct objfile *objfile,
5595 struct dwarf2_section_info *section,
5596 unsigned int is_dwz,
5597 int *n_allocated,
5598 int *n_comp_units,
5599 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5600{
be391dca 5601 gdb_byte *info_ptr;
36586728 5602 bfd *abfd = section->asection->owner;
be391dca 5603
36586728 5604 dwarf2_read_section (objfile, section);
ae038cb0 5605
36586728 5606 info_ptr = section->buffer;
6e70227d 5607
36586728 5608 while (info_ptr < section->buffer + section->size)
ae038cb0 5609 {
c764a876 5610 unsigned int length, initial_length_size;
ae038cb0 5611 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5612 sect_offset offset;
ae038cb0 5613
36586728 5614 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5615
5616 /* Read just enough information to find out where the next
5617 compilation unit is. */
36586728 5618 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5619
5620 /* Save the compilation unit for later lookup. */
5621 this_cu = obstack_alloc (&objfile->objfile_obstack,
5622 sizeof (struct dwarf2_per_cu_data));
5623 memset (this_cu, 0, sizeof (*this_cu));
5624 this_cu->offset = offset;
c764a876 5625 this_cu->length = length + initial_length_size;
36586728 5626 this_cu->is_dwz = is_dwz;
9291a0cd 5627 this_cu->objfile = objfile;
36586728 5628 this_cu->info_or_types_section = section;
ae038cb0 5629
36586728 5630 if (*n_comp_units == *n_allocated)
ae038cb0 5631 {
36586728
TT
5632 *n_allocated *= 2;
5633 *all_comp_units = xrealloc (*all_comp_units,
5634 *n_allocated
5635 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5636 }
36586728
TT
5637 (*all_comp_units)[*n_comp_units] = this_cu;
5638 ++*n_comp_units;
ae038cb0
DJ
5639
5640 info_ptr = info_ptr + this_cu->length;
5641 }
36586728
TT
5642}
5643
5644/* Create a list of all compilation units in OBJFILE.
5645 This is only done for -readnow and building partial symtabs. */
5646
5647static void
5648create_all_comp_units (struct objfile *objfile)
5649{
5650 int n_allocated;
5651 int n_comp_units;
5652 struct dwarf2_per_cu_data **all_comp_units;
5653
5654 n_comp_units = 0;
5655 n_allocated = 10;
5656 all_comp_units = xmalloc (n_allocated
5657 * sizeof (struct dwarf2_per_cu_data *));
5658
5659 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5660 &n_allocated, &n_comp_units, &all_comp_units);
5661
5662 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5663 {
5664 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5665
5666 read_comp_units_from_section (objfile, &dwz->info, 1,
5667 &n_allocated, &n_comp_units,
5668 &all_comp_units);
5669 }
ae038cb0
DJ
5670
5671 dwarf2_per_objfile->all_comp_units
5672 = obstack_alloc (&objfile->objfile_obstack,
5673 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5674 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5675 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5676 xfree (all_comp_units);
5677 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5678}
5679
5734ee8b
DJ
5680/* Process all loaded DIEs for compilation unit CU, starting at
5681 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5682 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5683 DW_AT_ranges). If NEED_PC is set, then this function will set
5684 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5685 and record the covered ranges in the addrmap. */
c906108c 5686
72bf9492
DJ
5687static void
5688scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5689 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5690{
72bf9492 5691 struct partial_die_info *pdi;
c906108c 5692
91c24f0a
DC
5693 /* Now, march along the PDI's, descending into ones which have
5694 interesting children but skipping the children of the other ones,
5695 until we reach the end of the compilation unit. */
c906108c 5696
72bf9492 5697 pdi = first_die;
91c24f0a 5698
72bf9492
DJ
5699 while (pdi != NULL)
5700 {
5701 fixup_partial_die (pdi, cu);
c906108c 5702
f55ee35c 5703 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5704 children, so we need to look at them. Ditto for anonymous
5705 enums. */
933c6fe4 5706
72bf9492 5707 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5708 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5709 || pdi->tag == DW_TAG_imported_unit)
c906108c 5710 {
72bf9492 5711 switch (pdi->tag)
c906108c
SS
5712 {
5713 case DW_TAG_subprogram:
5734ee8b 5714 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5715 break;
72929c62 5716 case DW_TAG_constant:
c906108c
SS
5717 case DW_TAG_variable:
5718 case DW_TAG_typedef:
91c24f0a 5719 case DW_TAG_union_type:
72bf9492 5720 if (!pdi->is_declaration)
63d06c5c 5721 {
72bf9492 5722 add_partial_symbol (pdi, cu);
63d06c5c
DC
5723 }
5724 break;
c906108c 5725 case DW_TAG_class_type:
680b30c7 5726 case DW_TAG_interface_type:
c906108c 5727 case DW_TAG_structure_type:
72bf9492 5728 if (!pdi->is_declaration)
c906108c 5729 {
72bf9492 5730 add_partial_symbol (pdi, cu);
c906108c
SS
5731 }
5732 break;
91c24f0a 5733 case DW_TAG_enumeration_type:
72bf9492
DJ
5734 if (!pdi->is_declaration)
5735 add_partial_enumeration (pdi, cu);
c906108c
SS
5736 break;
5737 case DW_TAG_base_type:
a02abb62 5738 case DW_TAG_subrange_type:
c906108c 5739 /* File scope base type definitions are added to the partial
c5aa993b 5740 symbol table. */
72bf9492 5741 add_partial_symbol (pdi, cu);
c906108c 5742 break;
d9fa45fe 5743 case DW_TAG_namespace:
5734ee8b 5744 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5745 break;
5d7cb8df
JK
5746 case DW_TAG_module:
5747 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5748 break;
95554aad
TT
5749 case DW_TAG_imported_unit:
5750 {
5751 struct dwarf2_per_cu_data *per_cu;
5752
f4dc4d17
DE
5753 /* For now we don't handle imported units in type units. */
5754 if (cu->per_cu->is_debug_types)
5755 {
5756 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5757 " supported in type units [in module %s]"),
5758 cu->objfile->name);
5759 }
5760
95554aad 5761 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5762 pdi->is_dwz,
95554aad
TT
5763 cu->objfile);
5764
5765 /* Go read the partial unit, if needed. */
5766 if (per_cu->v.psymtab == NULL)
5767 process_psymtab_comp_unit (per_cu, 1);
5768
f4dc4d17
DE
5769 VEC_safe_push (dwarf2_per_cu_ptr,
5770 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5771 }
5772 break;
c906108c
SS
5773 default:
5774 break;
5775 }
5776 }
5777
72bf9492
DJ
5778 /* If the die has a sibling, skip to the sibling. */
5779
5780 pdi = pdi->die_sibling;
5781 }
5782}
5783
5784/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5785
72bf9492 5786 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5787 name is concatenated with "::" and the partial DIE's name. For
5788 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5789 Enumerators are an exception; they use the scope of their parent
5790 enumeration type, i.e. the name of the enumeration type is not
5791 prepended to the enumerator.
91c24f0a 5792
72bf9492
DJ
5793 There are two complexities. One is DW_AT_specification; in this
5794 case "parent" means the parent of the target of the specification,
5795 instead of the direct parent of the DIE. The other is compilers
5796 which do not emit DW_TAG_namespace; in this case we try to guess
5797 the fully qualified name of structure types from their members'
5798 linkage names. This must be done using the DIE's children rather
5799 than the children of any DW_AT_specification target. We only need
5800 to do this for structures at the top level, i.e. if the target of
5801 any DW_AT_specification (if any; otherwise the DIE itself) does not
5802 have a parent. */
5803
5804/* Compute the scope prefix associated with PDI's parent, in
5805 compilation unit CU. The result will be allocated on CU's
5806 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5807 field. NULL is returned if no prefix is necessary. */
5808static char *
5809partial_die_parent_scope (struct partial_die_info *pdi,
5810 struct dwarf2_cu *cu)
5811{
5812 char *grandparent_scope;
5813 struct partial_die_info *parent, *real_pdi;
91c24f0a 5814
72bf9492
DJ
5815 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5816 then this means the parent of the specification DIE. */
5817
5818 real_pdi = pdi;
72bf9492 5819 while (real_pdi->has_specification)
36586728
TT
5820 real_pdi = find_partial_die (real_pdi->spec_offset,
5821 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5822
5823 parent = real_pdi->die_parent;
5824 if (parent == NULL)
5825 return NULL;
5826
5827 if (parent->scope_set)
5828 return parent->scope;
5829
5830 fixup_partial_die (parent, cu);
5831
10b3939b 5832 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5833
acebe513
UW
5834 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5835 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5836 Work around this problem here. */
5837 if (cu->language == language_cplus
6e70227d 5838 && parent->tag == DW_TAG_namespace
acebe513
UW
5839 && strcmp (parent->name, "::") == 0
5840 && grandparent_scope == NULL)
5841 {
5842 parent->scope = NULL;
5843 parent->scope_set = 1;
5844 return NULL;
5845 }
5846
9c6c53f7
SA
5847 if (pdi->tag == DW_TAG_enumerator)
5848 /* Enumerators should not get the name of the enumeration as a prefix. */
5849 parent->scope = grandparent_scope;
5850 else if (parent->tag == DW_TAG_namespace
f55ee35c 5851 || parent->tag == DW_TAG_module
72bf9492
DJ
5852 || parent->tag == DW_TAG_structure_type
5853 || parent->tag == DW_TAG_class_type
680b30c7 5854 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5855 || parent->tag == DW_TAG_union_type
5856 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5857 {
5858 if (grandparent_scope == NULL)
5859 parent->scope = parent->name;
5860 else
3e43a32a
MS
5861 parent->scope = typename_concat (&cu->comp_unit_obstack,
5862 grandparent_scope,
f55ee35c 5863 parent->name, 0, cu);
72bf9492 5864 }
72bf9492
DJ
5865 else
5866 {
5867 /* FIXME drow/2004-04-01: What should we be doing with
5868 function-local names? For partial symbols, we should probably be
5869 ignoring them. */
5870 complaint (&symfile_complaints,
e2e0b3e5 5871 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5872 parent->tag, pdi->offset.sect_off);
72bf9492 5873 parent->scope = grandparent_scope;
c906108c
SS
5874 }
5875
72bf9492
DJ
5876 parent->scope_set = 1;
5877 return parent->scope;
5878}
5879
5880/* Return the fully scoped name associated with PDI, from compilation unit
5881 CU. The result will be allocated with malloc. */
4568ecf9 5882
72bf9492
DJ
5883static char *
5884partial_die_full_name (struct partial_die_info *pdi,
5885 struct dwarf2_cu *cu)
5886{
5887 char *parent_scope;
5888
98bfdba5
PA
5889 /* If this is a template instantiation, we can not work out the
5890 template arguments from partial DIEs. So, unfortunately, we have
5891 to go through the full DIEs. At least any work we do building
5892 types here will be reused if full symbols are loaded later. */
5893 if (pdi->has_template_arguments)
5894 {
5895 fixup_partial_die (pdi, cu);
5896
5897 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5898 {
5899 struct die_info *die;
5900 struct attribute attr;
5901 struct dwarf2_cu *ref_cu = cu;
5902
b64f50a1 5903 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5904 attr.name = 0;
5905 attr.form = DW_FORM_ref_addr;
4568ecf9 5906 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5907 die = follow_die_ref (NULL, &attr, &ref_cu);
5908
5909 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5910 }
5911 }
5912
72bf9492
DJ
5913 parent_scope = partial_die_parent_scope (pdi, cu);
5914 if (parent_scope == NULL)
5915 return NULL;
5916 else
f55ee35c 5917 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5918}
5919
5920static void
72bf9492 5921add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5922{
e7c27a73 5923 struct objfile *objfile = cu->objfile;
c906108c 5924 CORE_ADDR addr = 0;
decbce07 5925 char *actual_name = NULL;
e142c38c 5926 CORE_ADDR baseaddr;
72bf9492 5927 int built_actual_name = 0;
e142c38c
DJ
5928
5929 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5930
94af9270
KS
5931 actual_name = partial_die_full_name (pdi, cu);
5932 if (actual_name)
5933 built_actual_name = 1;
63d06c5c 5934
72bf9492
DJ
5935 if (actual_name == NULL)
5936 actual_name = pdi->name;
5937
c906108c
SS
5938 switch (pdi->tag)
5939 {
5940 case DW_TAG_subprogram:
2cfa0c8d 5941 if (pdi->is_external || cu->language == language_ada)
c906108c 5942 {
2cfa0c8d
JB
5943 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5944 of the global scope. But in Ada, we want to be able to access
5945 nested procedures globally. So all Ada subprograms are stored
5946 in the global scope. */
f47fb265 5947 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5948 mst_text, objfile); */
f47fb265
MS
5949 add_psymbol_to_list (actual_name, strlen (actual_name),
5950 built_actual_name,
5951 VAR_DOMAIN, LOC_BLOCK,
5952 &objfile->global_psymbols,
5953 0, pdi->lowpc + baseaddr,
5954 cu->language, objfile);
c906108c
SS
5955 }
5956 else
5957 {
f47fb265 5958 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5959 mst_file_text, objfile); */
f47fb265
MS
5960 add_psymbol_to_list (actual_name, strlen (actual_name),
5961 built_actual_name,
5962 VAR_DOMAIN, LOC_BLOCK,
5963 &objfile->static_psymbols,
5964 0, pdi->lowpc + baseaddr,
5965 cu->language, objfile);
c906108c
SS
5966 }
5967 break;
72929c62
JB
5968 case DW_TAG_constant:
5969 {
5970 struct psymbol_allocation_list *list;
5971
5972 if (pdi->is_external)
5973 list = &objfile->global_psymbols;
5974 else
5975 list = &objfile->static_psymbols;
f47fb265
MS
5976 add_psymbol_to_list (actual_name, strlen (actual_name),
5977 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5978 list, 0, 0, cu->language, objfile);
72929c62
JB
5979 }
5980 break;
c906108c 5981 case DW_TAG_variable:
95554aad
TT
5982 if (pdi->d.locdesc)
5983 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5984
95554aad 5985 if (pdi->d.locdesc
caac4577
JG
5986 && addr == 0
5987 && !dwarf2_per_objfile->has_section_at_zero)
5988 {
5989 /* A global or static variable may also have been stripped
5990 out by the linker if unused, in which case its address
5991 will be nullified; do not add such variables into partial
5992 symbol table then. */
5993 }
5994 else if (pdi->is_external)
c906108c
SS
5995 {
5996 /* Global Variable.
5997 Don't enter into the minimal symbol tables as there is
5998 a minimal symbol table entry from the ELF symbols already.
5999 Enter into partial symbol table if it has a location
6000 descriptor or a type.
6001 If the location descriptor is missing, new_symbol will create
6002 a LOC_UNRESOLVED symbol, the address of the variable will then
6003 be determined from the minimal symbol table whenever the variable
6004 is referenced.
6005 The address for the partial symbol table entry is not
6006 used by GDB, but it comes in handy for debugging partial symbol
6007 table building. */
6008
95554aad 6009 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
6010 add_psymbol_to_list (actual_name, strlen (actual_name),
6011 built_actual_name,
6012 VAR_DOMAIN, LOC_STATIC,
6013 &objfile->global_psymbols,
6014 0, addr + baseaddr,
6015 cu->language, objfile);
c906108c
SS
6016 }
6017 else
6018 {
0963b4bd 6019 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6020 if (pdi->d.locdesc == NULL)
decbce07
MS
6021 {
6022 if (built_actual_name)
6023 xfree (actual_name);
6024 return;
6025 }
f47fb265 6026 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6027 mst_file_data, objfile); */
f47fb265
MS
6028 add_psymbol_to_list (actual_name, strlen (actual_name),
6029 built_actual_name,
6030 VAR_DOMAIN, LOC_STATIC,
6031 &objfile->static_psymbols,
6032 0, addr + baseaddr,
6033 cu->language, objfile);
c906108c
SS
6034 }
6035 break;
6036 case DW_TAG_typedef:
6037 case DW_TAG_base_type:
a02abb62 6038 case DW_TAG_subrange_type:
38d518c9 6039 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 6040 built_actual_name,
176620f1 6041 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6042 &objfile->static_psymbols,
e142c38c 6043 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6044 break;
72bf9492
DJ
6045 case DW_TAG_namespace:
6046 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 6047 built_actual_name,
72bf9492
DJ
6048 VAR_DOMAIN, LOC_TYPEDEF,
6049 &objfile->global_psymbols,
6050 0, (CORE_ADDR) 0, cu->language, objfile);
6051 break;
c906108c 6052 case DW_TAG_class_type:
680b30c7 6053 case DW_TAG_interface_type:
c906108c
SS
6054 case DW_TAG_structure_type:
6055 case DW_TAG_union_type:
6056 case DW_TAG_enumeration_type:
fa4028e9
JB
6057 /* Skip external references. The DWARF standard says in the section
6058 about "Structure, Union, and Class Type Entries": "An incomplete
6059 structure, union or class type is represented by a structure,
6060 union or class entry that does not have a byte size attribute
6061 and that has a DW_AT_declaration attribute." */
6062 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
6063 {
6064 if (built_actual_name)
6065 xfree (actual_name);
6066 return;
6067 }
fa4028e9 6068
63d06c5c
DC
6069 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6070 static vs. global. */
38d518c9 6071 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 6072 built_actual_name,
176620f1 6073 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6074 (cu->language == language_cplus
6075 || cu->language == language_java)
63d06c5c
DC
6076 ? &objfile->global_psymbols
6077 : &objfile->static_psymbols,
e142c38c 6078 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6079
c906108c
SS
6080 break;
6081 case DW_TAG_enumerator:
38d518c9 6082 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 6083 built_actual_name,
176620f1 6084 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6085 (cu->language == language_cplus
6086 || cu->language == language_java)
f6fe98ef
DJ
6087 ? &objfile->global_psymbols
6088 : &objfile->static_psymbols,
e142c38c 6089 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6090 break;
6091 default:
6092 break;
6093 }
5c4e30ca 6094
72bf9492
DJ
6095 if (built_actual_name)
6096 xfree (actual_name);
c906108c
SS
6097}
6098
5c4e30ca
DC
6099/* Read a partial die corresponding to a namespace; also, add a symbol
6100 corresponding to that namespace to the symbol table. NAMESPACE is
6101 the name of the enclosing namespace. */
91c24f0a 6102
72bf9492
DJ
6103static void
6104add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6105 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6106 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6107{
72bf9492 6108 /* Add a symbol for the namespace. */
e7c27a73 6109
72bf9492 6110 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6111
6112 /* Now scan partial symbols in that namespace. */
6113
91c24f0a 6114 if (pdi->has_children)
5734ee8b 6115 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6116}
6117
5d7cb8df
JK
6118/* Read a partial die corresponding to a Fortran module. */
6119
6120static void
6121add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6122 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6123{
f55ee35c 6124 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6125
6126 if (pdi->has_children)
6127 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6128}
6129
bc30ff58
JB
6130/* Read a partial die corresponding to a subprogram and create a partial
6131 symbol for that subprogram. When the CU language allows it, this
6132 routine also defines a partial symbol for each nested subprogram
6133 that this subprogram contains.
6e70227d 6134
bc30ff58
JB
6135 DIE my also be a lexical block, in which case we simply search
6136 recursively for suprograms defined inside that lexical block.
6137 Again, this is only performed when the CU language allows this
6138 type of definitions. */
6139
6140static void
6141add_partial_subprogram (struct partial_die_info *pdi,
6142 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6143 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6144{
6145 if (pdi->tag == DW_TAG_subprogram)
6146 {
6147 if (pdi->has_pc_info)
6148 {
6149 if (pdi->lowpc < *lowpc)
6150 *lowpc = pdi->lowpc;
6151 if (pdi->highpc > *highpc)
6152 *highpc = pdi->highpc;
5734ee8b
DJ
6153 if (need_pc)
6154 {
6155 CORE_ADDR baseaddr;
6156 struct objfile *objfile = cu->objfile;
6157
6158 baseaddr = ANOFFSET (objfile->section_offsets,
6159 SECT_OFF_TEXT (objfile));
6160 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6161 pdi->lowpc + baseaddr,
6162 pdi->highpc - 1 + baseaddr,
9291a0cd 6163 cu->per_cu->v.psymtab);
5734ee8b 6164 }
481860b3
GB
6165 }
6166
6167 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6168 {
bc30ff58 6169 if (!pdi->is_declaration)
e8d05480
JB
6170 /* Ignore subprogram DIEs that do not have a name, they are
6171 illegal. Do not emit a complaint at this point, we will
6172 do so when we convert this psymtab into a symtab. */
6173 if (pdi->name)
6174 add_partial_symbol (pdi, cu);
bc30ff58
JB
6175 }
6176 }
6e70227d 6177
bc30ff58
JB
6178 if (! pdi->has_children)
6179 return;
6180
6181 if (cu->language == language_ada)
6182 {
6183 pdi = pdi->die_child;
6184 while (pdi != NULL)
6185 {
6186 fixup_partial_die (pdi, cu);
6187 if (pdi->tag == DW_TAG_subprogram
6188 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6189 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6190 pdi = pdi->die_sibling;
6191 }
6192 }
6193}
6194
91c24f0a
DC
6195/* Read a partial die corresponding to an enumeration type. */
6196
72bf9492
DJ
6197static void
6198add_partial_enumeration (struct partial_die_info *enum_pdi,
6199 struct dwarf2_cu *cu)
91c24f0a 6200{
72bf9492 6201 struct partial_die_info *pdi;
91c24f0a
DC
6202
6203 if (enum_pdi->name != NULL)
72bf9492
DJ
6204 add_partial_symbol (enum_pdi, cu);
6205
6206 pdi = enum_pdi->die_child;
6207 while (pdi)
91c24f0a 6208 {
72bf9492 6209 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6210 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6211 else
72bf9492
DJ
6212 add_partial_symbol (pdi, cu);
6213 pdi = pdi->die_sibling;
91c24f0a 6214 }
91c24f0a
DC
6215}
6216
6caca83c
CC
6217/* Return the initial uleb128 in the die at INFO_PTR. */
6218
6219static unsigned int
6220peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6221{
6222 unsigned int bytes_read;
6223
6224 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6225}
6226
4bb7a0a7
DJ
6227/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6228 Return the corresponding abbrev, or NULL if the number is zero (indicating
6229 an empty DIE). In either case *BYTES_READ will be set to the length of
6230 the initial number. */
6231
6232static struct abbrev_info *
fe1b8b76 6233peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6234 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6235{
6236 bfd *abfd = cu->objfile->obfd;
6237 unsigned int abbrev_number;
6238 struct abbrev_info *abbrev;
6239
6240 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6241
6242 if (abbrev_number == 0)
6243 return NULL;
6244
433df2d4 6245 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6246 if (!abbrev)
6247 {
3e43a32a
MS
6248 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6249 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6250 }
6251
6252 return abbrev;
6253}
6254
93311388
DE
6255/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6256 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6257 DIE. Any children of the skipped DIEs will also be skipped. */
6258
fe1b8b76 6259static gdb_byte *
dee91e82 6260skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6261{
dee91e82 6262 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6263 struct abbrev_info *abbrev;
6264 unsigned int bytes_read;
6265
6266 while (1)
6267 {
6268 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6269 if (abbrev == NULL)
6270 return info_ptr + bytes_read;
6271 else
dee91e82 6272 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6273 }
6274}
6275
93311388
DE
6276/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6277 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6278 abbrev corresponding to that skipped uleb128 should be passed in
6279 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6280 children. */
6281
fe1b8b76 6282static gdb_byte *
dee91e82
DE
6283skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6284 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6285{
6286 unsigned int bytes_read;
6287 struct attribute attr;
dee91e82
DE
6288 bfd *abfd = reader->abfd;
6289 struct dwarf2_cu *cu = reader->cu;
6290 gdb_byte *buffer = reader->buffer;
f664829e
DE
6291 const gdb_byte *buffer_end = reader->buffer_end;
6292 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6293 unsigned int form, i;
6294
6295 for (i = 0; i < abbrev->num_attrs; i++)
6296 {
6297 /* The only abbrev we care about is DW_AT_sibling. */
6298 if (abbrev->attrs[i].name == DW_AT_sibling)
6299 {
dee91e82 6300 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6301 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6302 complaint (&symfile_complaints,
6303 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6304 else
b64f50a1 6305 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6306 }
6307
6308 /* If it isn't DW_AT_sibling, skip this attribute. */
6309 form = abbrev->attrs[i].form;
6310 skip_attribute:
6311 switch (form)
6312 {
4bb7a0a7 6313 case DW_FORM_ref_addr:
ae411497
TT
6314 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6315 and later it is offset sized. */
6316 if (cu->header.version == 2)
6317 info_ptr += cu->header.addr_size;
6318 else
6319 info_ptr += cu->header.offset_size;
6320 break;
36586728
TT
6321 case DW_FORM_GNU_ref_alt:
6322 info_ptr += cu->header.offset_size;
6323 break;
ae411497 6324 case DW_FORM_addr:
4bb7a0a7
DJ
6325 info_ptr += cu->header.addr_size;
6326 break;
6327 case DW_FORM_data1:
6328 case DW_FORM_ref1:
6329 case DW_FORM_flag:
6330 info_ptr += 1;
6331 break;
2dc7f7b3
TT
6332 case DW_FORM_flag_present:
6333 break;
4bb7a0a7
DJ
6334 case DW_FORM_data2:
6335 case DW_FORM_ref2:
6336 info_ptr += 2;
6337 break;
6338 case DW_FORM_data4:
6339 case DW_FORM_ref4:
6340 info_ptr += 4;
6341 break;
6342 case DW_FORM_data8:
6343 case DW_FORM_ref8:
55f1336d 6344 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6345 info_ptr += 8;
6346 break;
6347 case DW_FORM_string:
9b1c24c8 6348 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6349 info_ptr += bytes_read;
6350 break;
2dc7f7b3 6351 case DW_FORM_sec_offset:
4bb7a0a7 6352 case DW_FORM_strp:
36586728 6353 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6354 info_ptr += cu->header.offset_size;
6355 break;
2dc7f7b3 6356 case DW_FORM_exprloc:
4bb7a0a7
DJ
6357 case DW_FORM_block:
6358 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6359 info_ptr += bytes_read;
6360 break;
6361 case DW_FORM_block1:
6362 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6363 break;
6364 case DW_FORM_block2:
6365 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6366 break;
6367 case DW_FORM_block4:
6368 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6369 break;
6370 case DW_FORM_sdata:
6371 case DW_FORM_udata:
6372 case DW_FORM_ref_udata:
3019eac3
DE
6373 case DW_FORM_GNU_addr_index:
6374 case DW_FORM_GNU_str_index:
f664829e 6375 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6376 break;
6377 case DW_FORM_indirect:
6378 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6379 info_ptr += bytes_read;
6380 /* We need to continue parsing from here, so just go back to
6381 the top. */
6382 goto skip_attribute;
6383
6384 default:
3e43a32a
MS
6385 error (_("Dwarf Error: Cannot handle %s "
6386 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6387 dwarf_form_name (form),
6388 bfd_get_filename (abfd));
6389 }
6390 }
6391
6392 if (abbrev->has_children)
dee91e82 6393 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6394 else
6395 return info_ptr;
6396}
6397
93311388 6398/* Locate ORIG_PDI's sibling.
dee91e82 6399 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6400
fe1b8b76 6401static gdb_byte *
dee91e82
DE
6402locate_pdi_sibling (const struct die_reader_specs *reader,
6403 struct partial_die_info *orig_pdi,
6404 gdb_byte *info_ptr)
91c24f0a
DC
6405{
6406 /* Do we know the sibling already? */
72bf9492 6407
91c24f0a
DC
6408 if (orig_pdi->sibling)
6409 return orig_pdi->sibling;
6410
6411 /* Are there any children to deal with? */
6412
6413 if (!orig_pdi->has_children)
6414 return info_ptr;
6415
4bb7a0a7 6416 /* Skip the children the long way. */
91c24f0a 6417
dee91e82 6418 return skip_children (reader, info_ptr);
91c24f0a
DC
6419}
6420
442e4d9c
YQ
6421/* Expand this partial symbol table into a full symbol table. PST is
6422 not NULL. */
c906108c
SS
6423
6424static void
5c80ed9d 6425dwarf2_psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
c906108c 6426{
442e4d9c 6427 if (pst->readin)
c906108c 6428 {
442e4d9c
YQ
6429 warning (_("bug: psymtab for %s is already read in."),
6430 pst->filename);
6431 }
6432 else
6433 {
6434 if (info_verbose)
c906108c 6435 {
442e4d9c
YQ
6436 printf_filtered (_("Reading in symbols for %s..."),
6437 pst->filename);
6438 gdb_flush (gdb_stdout);
c906108c 6439 }
c906108c 6440
442e4d9c
YQ
6441 /* Restore our global data. */
6442 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6443
442e4d9c
YQ
6444 /* If this psymtab is constructed from a debug-only objfile, the
6445 has_section_at_zero flag will not necessarily be correct. We
6446 can get the correct value for this flag by looking at the data
6447 associated with the (presumably stripped) associated objfile. */
6448 if (objfile->separate_debug_objfile_backlink)
6449 {
6450 struct dwarf2_per_objfile *dpo_backlink
6451 = objfile_data (objfile->separate_debug_objfile_backlink,
6452 dwarf2_objfile_data_key);
9a619af0 6453
442e4d9c
YQ
6454 dwarf2_per_objfile->has_section_at_zero
6455 = dpo_backlink->has_section_at_zero;
6456 }
b2ab525c 6457
442e4d9c 6458 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6459
442e4d9c 6460 psymtab_to_symtab_1 (pst);
c906108c 6461
442e4d9c
YQ
6462 /* Finish up the debug error message. */
6463 if (info_verbose)
6464 printf_filtered (_("done.\n"));
c906108c 6465 }
95554aad
TT
6466
6467 process_cu_includes ();
c906108c 6468}
9cdd5dbd
DE
6469\f
6470/* Reading in full CUs. */
c906108c 6471
10b3939b
DJ
6472/* Add PER_CU to the queue. */
6473
6474static void
95554aad
TT
6475queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6476 enum language pretend_language)
10b3939b
DJ
6477{
6478 struct dwarf2_queue_item *item;
6479
6480 per_cu->queued = 1;
6481 item = xmalloc (sizeof (*item));
6482 item->per_cu = per_cu;
95554aad 6483 item->pretend_language = pretend_language;
10b3939b
DJ
6484 item->next = NULL;
6485
6486 if (dwarf2_queue == NULL)
6487 dwarf2_queue = item;
6488 else
6489 dwarf2_queue_tail->next = item;
6490
6491 dwarf2_queue_tail = item;
6492}
6493
0907af0c
DE
6494/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6495 unit and add it to our queue.
6496 The result is non-zero if PER_CU was queued, otherwise the result is zero
6497 meaning either PER_CU is already queued or it is already loaded. */
6498
6499static int
6500maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6501 struct dwarf2_per_cu_data *per_cu,
6502 enum language pretend_language)
6503{
6504 /* We may arrive here during partial symbol reading, if we need full
6505 DIEs to process an unusual case (e.g. template arguments). Do
6506 not queue PER_CU, just tell our caller to load its DIEs. */
6507 if (dwarf2_per_objfile->reading_partial_symbols)
6508 {
6509 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6510 return 1;
6511 return 0;
6512 }
6513
6514 /* Mark the dependence relation so that we don't flush PER_CU
6515 too early. */
6516 dwarf2_add_dependence (this_cu, per_cu);
6517
6518 /* If it's already on the queue, we have nothing to do. */
6519 if (per_cu->queued)
6520 return 0;
6521
6522 /* If the compilation unit is already loaded, just mark it as
6523 used. */
6524 if (per_cu->cu != NULL)
6525 {
6526 per_cu->cu->last_used = 0;
6527 return 0;
6528 }
6529
6530 /* Add it to the queue. */
6531 queue_comp_unit (per_cu, pretend_language);
6532
6533 return 1;
6534}
6535
10b3939b
DJ
6536/* Process the queue. */
6537
6538static void
a0f42c21 6539process_queue (void)
10b3939b
DJ
6540{
6541 struct dwarf2_queue_item *item, *next_item;
6542
45cfd468
DE
6543 if (dwarf2_read_debug)
6544 {
6545 fprintf_unfiltered (gdb_stdlog,
6546 "Expanding one or more symtabs of objfile %s ...\n",
6547 dwarf2_per_objfile->objfile->name);
6548 }
6549
03dd20cc
DJ
6550 /* The queue starts out with one item, but following a DIE reference
6551 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6552 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6553 {
9291a0cd
TT
6554 if (dwarf2_per_objfile->using_index
6555 ? !item->per_cu->v.quick->symtab
6556 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6557 {
6558 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6559
6560 if (dwarf2_read_debug)
6561 {
6562 fprintf_unfiltered (gdb_stdlog,
6563 "Expanding symtab of %s at offset 0x%x\n",
6564 per_cu->is_debug_types ? "TU" : "CU",
6565 per_cu->offset.sect_off);
6566 }
6567
6568 if (per_cu->is_debug_types)
6569 process_full_type_unit (per_cu, item->pretend_language);
6570 else
6571 process_full_comp_unit (per_cu, item->pretend_language);
6572
6573 if (dwarf2_read_debug)
6574 {
6575 fprintf_unfiltered (gdb_stdlog,
6576 "Done expanding %s at offset 0x%x\n",
6577 per_cu->is_debug_types ? "TU" : "CU",
6578 per_cu->offset.sect_off);
6579 }
6580 }
10b3939b
DJ
6581
6582 item->per_cu->queued = 0;
6583 next_item = item->next;
6584 xfree (item);
6585 }
6586
6587 dwarf2_queue_tail = NULL;
45cfd468
DE
6588
6589 if (dwarf2_read_debug)
6590 {
6591 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6592 dwarf2_per_objfile->objfile->name);
6593 }
10b3939b
DJ
6594}
6595
6596/* Free all allocated queue entries. This function only releases anything if
6597 an error was thrown; if the queue was processed then it would have been
6598 freed as we went along. */
6599
6600static void
6601dwarf2_release_queue (void *dummy)
6602{
6603 struct dwarf2_queue_item *item, *last;
6604
6605 item = dwarf2_queue;
6606 while (item)
6607 {
6608 /* Anything still marked queued is likely to be in an
6609 inconsistent state, so discard it. */
6610 if (item->per_cu->queued)
6611 {
6612 if (item->per_cu->cu != NULL)
dee91e82 6613 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6614 item->per_cu->queued = 0;
6615 }
6616
6617 last = item;
6618 item = item->next;
6619 xfree (last);
6620 }
6621
6622 dwarf2_queue = dwarf2_queue_tail = NULL;
6623}
6624
6625/* Read in full symbols for PST, and anything it depends on. */
6626
c906108c 6627static void
fba45db2 6628psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6629{
10b3939b 6630 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6631 int i;
6632
95554aad
TT
6633 if (pst->readin)
6634 return;
6635
aaa75496 6636 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6637 if (!pst->dependencies[i]->readin
6638 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6639 {
6640 /* Inform about additional files that need to be read in. */
6641 if (info_verbose)
6642 {
a3f17187 6643 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6644 fputs_filtered (" ", gdb_stdout);
6645 wrap_here ("");
6646 fputs_filtered ("and ", gdb_stdout);
6647 wrap_here ("");
6648 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6649 wrap_here (""); /* Flush output. */
aaa75496
JB
6650 gdb_flush (gdb_stdout);
6651 }
6652 psymtab_to_symtab_1 (pst->dependencies[i]);
6653 }
6654
e38df1d0 6655 per_cu = pst->read_symtab_private;
10b3939b
DJ
6656
6657 if (per_cu == NULL)
aaa75496
JB
6658 {
6659 /* It's an include file, no symbols to read for it.
6660 Everything is in the parent symtab. */
6661 pst->readin = 1;
6662 return;
6663 }
c906108c 6664
a0f42c21 6665 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6666}
6667
dee91e82
DE
6668/* Trivial hash function for die_info: the hash value of a DIE
6669 is its offset in .debug_info for this objfile. */
10b3939b 6670
dee91e82
DE
6671static hashval_t
6672die_hash (const void *item)
10b3939b 6673{
dee91e82 6674 const struct die_info *die = item;
6502dd73 6675
dee91e82
DE
6676 return die->offset.sect_off;
6677}
63d06c5c 6678
dee91e82
DE
6679/* Trivial comparison function for die_info structures: two DIEs
6680 are equal if they have the same offset. */
98bfdba5 6681
dee91e82
DE
6682static int
6683die_eq (const void *item_lhs, const void *item_rhs)
6684{
6685 const struct die_info *die_lhs = item_lhs;
6686 const struct die_info *die_rhs = item_rhs;
c906108c 6687
dee91e82
DE
6688 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6689}
c906108c 6690
dee91e82
DE
6691/* die_reader_func for load_full_comp_unit.
6692 This is identical to read_signatured_type_reader,
6693 but is kept separate for now. */
c906108c 6694
dee91e82
DE
6695static void
6696load_full_comp_unit_reader (const struct die_reader_specs *reader,
6697 gdb_byte *info_ptr,
6698 struct die_info *comp_unit_die,
6699 int has_children,
6700 void *data)
6701{
6702 struct dwarf2_cu *cu = reader->cu;
95554aad 6703 enum language *language_ptr = data;
6caca83c 6704
dee91e82
DE
6705 gdb_assert (cu->die_hash == NULL);
6706 cu->die_hash =
6707 htab_create_alloc_ex (cu->header.length / 12,
6708 die_hash,
6709 die_eq,
6710 NULL,
6711 &cu->comp_unit_obstack,
6712 hashtab_obstack_allocate,
6713 dummy_obstack_deallocate);
e142c38c 6714
dee91e82
DE
6715 if (has_children)
6716 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6717 &info_ptr, comp_unit_die);
6718 cu->dies = comp_unit_die;
6719 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6720
6721 /* We try not to read any attributes in this function, because not
9cdd5dbd 6722 all CUs needed for references have been loaded yet, and symbol
10b3939b 6723 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6724 or we won't be able to build types correctly.
6725 Similarly, if we do not read the producer, we can not apply
6726 producer-specific interpretation. */
95554aad 6727 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6728}
10b3939b 6729
dee91e82 6730/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6731
dee91e82 6732static void
95554aad
TT
6733load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6734 enum language pretend_language)
dee91e82 6735{
3019eac3 6736 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6737
f4dc4d17
DE
6738 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6739 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6740}
6741
3da10d80
KS
6742/* Add a DIE to the delayed physname list. */
6743
6744static void
6745add_to_method_list (struct type *type, int fnfield_index, int index,
6746 const char *name, struct die_info *die,
6747 struct dwarf2_cu *cu)
6748{
6749 struct delayed_method_info mi;
6750 mi.type = type;
6751 mi.fnfield_index = fnfield_index;
6752 mi.index = index;
6753 mi.name = name;
6754 mi.die = die;
6755 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6756}
6757
6758/* A cleanup for freeing the delayed method list. */
6759
6760static void
6761free_delayed_list (void *ptr)
6762{
6763 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6764 if (cu->method_list != NULL)
6765 {
6766 VEC_free (delayed_method_info, cu->method_list);
6767 cu->method_list = NULL;
6768 }
6769}
6770
6771/* Compute the physnames of any methods on the CU's method list.
6772
6773 The computation of method physnames is delayed in order to avoid the
6774 (bad) condition that one of the method's formal parameters is of an as yet
6775 incomplete type. */
6776
6777static void
6778compute_delayed_physnames (struct dwarf2_cu *cu)
6779{
6780 int i;
6781 struct delayed_method_info *mi;
6782 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6783 {
1d06ead6 6784 const char *physname;
3da10d80
KS
6785 struct fn_fieldlist *fn_flp
6786 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6787 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6788 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6789 }
6790}
6791
a766d390
DE
6792/* Go objects should be embedded in a DW_TAG_module DIE,
6793 and it's not clear if/how imported objects will appear.
6794 To keep Go support simple until that's worked out,
6795 go back through what we've read and create something usable.
6796 We could do this while processing each DIE, and feels kinda cleaner,
6797 but that way is more invasive.
6798 This is to, for example, allow the user to type "p var" or "b main"
6799 without having to specify the package name, and allow lookups
6800 of module.object to work in contexts that use the expression
6801 parser. */
6802
6803static void
6804fixup_go_packaging (struct dwarf2_cu *cu)
6805{
6806 char *package_name = NULL;
6807 struct pending *list;
6808 int i;
6809
6810 for (list = global_symbols; list != NULL; list = list->next)
6811 {
6812 for (i = 0; i < list->nsyms; ++i)
6813 {
6814 struct symbol *sym = list->symbol[i];
6815
6816 if (SYMBOL_LANGUAGE (sym) == language_go
6817 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6818 {
6819 char *this_package_name = go_symbol_package_name (sym);
6820
6821 if (this_package_name == NULL)
6822 continue;
6823 if (package_name == NULL)
6824 package_name = this_package_name;
6825 else
6826 {
6827 if (strcmp (package_name, this_package_name) != 0)
6828 complaint (&symfile_complaints,
6829 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6830 (SYMBOL_SYMTAB (sym)
210bbc17 6831 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6832 : cu->objfile->name),
6833 this_package_name, package_name);
6834 xfree (this_package_name);
6835 }
6836 }
6837 }
6838 }
6839
6840 if (package_name != NULL)
6841 {
6842 struct objfile *objfile = cu->objfile;
6843 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6844 package_name, objfile);
6845 struct symbol *sym;
6846
6847 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6848
6849 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6850 SYMBOL_SET_LANGUAGE (sym, language_go);
6851 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6852 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6853 e.g., "main" finds the "main" module and not C's main(). */
6854 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6855 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6856 SYMBOL_TYPE (sym) = type;
6857
6858 add_symbol_to_list (sym, &global_symbols);
6859
6860 xfree (package_name);
6861 }
6862}
6863
95554aad
TT
6864static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6865
6866/* Return the symtab for PER_CU. This works properly regardless of
6867 whether we're using the index or psymtabs. */
6868
6869static struct symtab *
6870get_symtab (struct dwarf2_per_cu_data *per_cu)
6871{
6872 return (dwarf2_per_objfile->using_index
6873 ? per_cu->v.quick->symtab
6874 : per_cu->v.psymtab->symtab);
6875}
6876
6877/* A helper function for computing the list of all symbol tables
6878 included by PER_CU. */
6879
6880static void
6881recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6882 htab_t all_children,
6883 struct dwarf2_per_cu_data *per_cu)
6884{
6885 void **slot;
6886 int ix;
6887 struct dwarf2_per_cu_data *iter;
6888
6889 slot = htab_find_slot (all_children, per_cu, INSERT);
6890 if (*slot != NULL)
6891 {
6892 /* This inclusion and its children have been processed. */
6893 return;
6894 }
6895
6896 *slot = per_cu;
6897 /* Only add a CU if it has a symbol table. */
6898 if (get_symtab (per_cu) != NULL)
6899 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6900
6901 for (ix = 0;
f4dc4d17 6902 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6903 ++ix)
6904 recursively_compute_inclusions (result, all_children, iter);
6905}
6906
6907/* Compute the symtab 'includes' fields for the symtab related to
6908 PER_CU. */
6909
6910static void
6911compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6912{
f4dc4d17
DE
6913 gdb_assert (! per_cu->is_debug_types);
6914
6915 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6916 {
6917 int ix, len;
6918 struct dwarf2_per_cu_data *iter;
6919 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6920 htab_t all_children;
6921 struct symtab *symtab = get_symtab (per_cu);
6922
6923 /* If we don't have a symtab, we can just skip this case. */
6924 if (symtab == NULL)
6925 return;
6926
6927 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6928 NULL, xcalloc, xfree);
6929
6930 for (ix = 0;
f4dc4d17 6931 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6932 ix, iter);
6933 ++ix)
6934 recursively_compute_inclusions (&result_children, all_children, iter);
6935
6936 /* Now we have a transitive closure of all the included CUs, so
6937 we can convert it to a list of symtabs. */
6938 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6939 symtab->includes
6940 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6941 (len + 1) * sizeof (struct symtab *));
6942 for (ix = 0;
6943 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6944 ++ix)
6945 symtab->includes[ix] = get_symtab (iter);
6946 symtab->includes[len] = NULL;
6947
6948 VEC_free (dwarf2_per_cu_ptr, result_children);
6949 htab_delete (all_children);
6950 }
6951}
6952
6953/* Compute the 'includes' field for the symtabs of all the CUs we just
6954 read. */
6955
6956static void
6957process_cu_includes (void)
6958{
6959 int ix;
6960 struct dwarf2_per_cu_data *iter;
6961
6962 for (ix = 0;
6963 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6964 ix, iter);
6965 ++ix)
f4dc4d17
DE
6966 {
6967 if (! iter->is_debug_types)
6968 compute_symtab_includes (iter);
6969 }
95554aad
TT
6970
6971 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6972}
6973
9cdd5dbd 6974/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6975 already been loaded into memory. */
6976
6977static void
95554aad
TT
6978process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6979 enum language pretend_language)
10b3939b 6980{
10b3939b 6981 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6982 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6983 CORE_ADDR lowpc, highpc;
6984 struct symtab *symtab;
3da10d80 6985 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6986 CORE_ADDR baseaddr;
4359dff1 6987 struct block *static_block;
10b3939b
DJ
6988
6989 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6990
10b3939b
DJ
6991 buildsym_init ();
6992 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6993 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6994
6995 cu->list_in_scope = &file_symbols;
c906108c 6996
95554aad
TT
6997 cu->language = pretend_language;
6998 cu->language_defn = language_def (cu->language);
6999
c906108c 7000 /* Do line number decoding in read_file_scope () */
10b3939b 7001 process_die (cu->dies, cu);
c906108c 7002
a766d390
DE
7003 /* For now fudge the Go package. */
7004 if (cu->language == language_go)
7005 fixup_go_packaging (cu);
7006
3da10d80
KS
7007 /* Now that we have processed all the DIEs in the CU, all the types
7008 should be complete, and it should now be safe to compute all of the
7009 physnames. */
7010 compute_delayed_physnames (cu);
7011 do_cleanups (delayed_list_cleanup);
7012
fae299cd
DC
7013 /* Some compilers don't define a DW_AT_high_pc attribute for the
7014 compilation unit. If the DW_AT_high_pc is missing, synthesize
7015 it, by scanning the DIE's below the compilation unit. */
10b3939b 7016 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7017
36586728
TT
7018 static_block
7019 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7020 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
7021
7022 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7023 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7024 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7025 addrmap to help ensure it has an accurate map of pc values belonging to
7026 this comp unit. */
7027 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7028
7029 symtab = end_symtab_from_static_block (static_block, objfile,
7030 SECT_OFF_TEXT (objfile), 0);
c906108c 7031
8be455d7 7032 if (symtab != NULL)
c906108c 7033 {
df15bd07 7034 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7035
8be455d7
JK
7036 /* Set symtab language to language from DW_AT_language. If the
7037 compilation is from a C file generated by language preprocessors, do
7038 not set the language if it was already deduced by start_subfile. */
7039 if (!(cu->language == language_c && symtab->language != language_c))
7040 symtab->language = cu->language;
7041
7042 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7043 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7044 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7045 there were bugs in prologue debug info, fixed later in GCC-4.5
7046 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7047
7048 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7049 needed, it would be wrong due to missing DW_AT_producer there.
7050
7051 Still one can confuse GDB by using non-standard GCC compilation
7052 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7053 */
ab260dad 7054 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7055 symtab->locations_valid = 1;
e0d00bc7
JK
7056
7057 if (gcc_4_minor >= 5)
7058 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7059
7060 symtab->call_site_htab = cu->call_site_htab;
c906108c 7061 }
9291a0cd
TT
7062
7063 if (dwarf2_per_objfile->using_index)
7064 per_cu->v.quick->symtab = symtab;
7065 else
7066 {
7067 struct partial_symtab *pst = per_cu->v.psymtab;
7068 pst->symtab = symtab;
7069 pst->readin = 1;
7070 }
c906108c 7071
95554aad
TT
7072 /* Push it for inclusion processing later. */
7073 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7074
c906108c 7075 do_cleanups (back_to);
f4dc4d17 7076}
45cfd468 7077
f4dc4d17
DE
7078/* Generate full symbol information for type unit PER_CU, whose DIEs have
7079 already been loaded into memory. */
7080
7081static void
7082process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7083 enum language pretend_language)
7084{
7085 struct dwarf2_cu *cu = per_cu->cu;
7086 struct objfile *objfile = per_cu->objfile;
7087 struct symtab *symtab;
7088 struct cleanup *back_to, *delayed_list_cleanup;
7089
7090 buildsym_init ();
7091 back_to = make_cleanup (really_free_pendings, NULL);
7092 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7093
7094 cu->list_in_scope = &file_symbols;
7095
7096 cu->language = pretend_language;
7097 cu->language_defn = language_def (cu->language);
7098
7099 /* The symbol tables are set up in read_type_unit_scope. */
7100 process_die (cu->dies, cu);
7101
7102 /* For now fudge the Go package. */
7103 if (cu->language == language_go)
7104 fixup_go_packaging (cu);
7105
7106 /* Now that we have processed all the DIEs in the CU, all the types
7107 should be complete, and it should now be safe to compute all of the
7108 physnames. */
7109 compute_delayed_physnames (cu);
7110 do_cleanups (delayed_list_cleanup);
7111
7112 /* TUs share symbol tables.
7113 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7114 of it with end_expandable_symtab. Otherwise, complete the addition of
7115 this TU's symbols to the existing symtab. */
f4dc4d17 7116 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7117 {
f4dc4d17
DE
7118 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7119 per_cu->s.type_unit_group->primary_symtab = symtab;
7120
7121 if (symtab != NULL)
7122 {
7123 /* Set symtab language to language from DW_AT_language. If the
7124 compilation is from a C file generated by language preprocessors,
7125 do not set the language if it was already deduced by
7126 start_subfile. */
7127 if (!(cu->language == language_c && symtab->language != language_c))
7128 symtab->language = cu->language;
7129 }
7130 }
7131 else
7132 {
7133 augment_type_symtab (objfile,
7134 per_cu->s.type_unit_group->primary_symtab);
7135 symtab = per_cu->s.type_unit_group->primary_symtab;
7136 }
7137
7138 if (dwarf2_per_objfile->using_index)
7139 per_cu->v.quick->symtab = symtab;
7140 else
7141 {
7142 struct partial_symtab *pst = per_cu->v.psymtab;
7143 pst->symtab = symtab;
7144 pst->readin = 1;
45cfd468 7145 }
f4dc4d17
DE
7146
7147 do_cleanups (back_to);
c906108c
SS
7148}
7149
95554aad
TT
7150/* Process an imported unit DIE. */
7151
7152static void
7153process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7154{
7155 struct attribute *attr;
7156
f4dc4d17
DE
7157 /* For now we don't handle imported units in type units. */
7158 if (cu->per_cu->is_debug_types)
7159 {
7160 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7161 " supported in type units [in module %s]"),
7162 cu->objfile->name);
7163 }
7164
95554aad
TT
7165 attr = dwarf2_attr (die, DW_AT_import, cu);
7166 if (attr != NULL)
7167 {
7168 struct dwarf2_per_cu_data *per_cu;
7169 struct symtab *imported_symtab;
7170 sect_offset offset;
36586728 7171 int is_dwz;
95554aad
TT
7172
7173 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7174 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7175 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7176
7177 /* Queue the unit, if needed. */
7178 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7179 load_full_comp_unit (per_cu, cu->language);
7180
f4dc4d17 7181 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7182 per_cu);
7183 }
7184}
7185
c906108c
SS
7186/* Process a die and its children. */
7187
7188static void
e7c27a73 7189process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7190{
7191 switch (die->tag)
7192 {
7193 case DW_TAG_padding:
7194 break;
7195 case DW_TAG_compile_unit:
95554aad 7196 case DW_TAG_partial_unit:
e7c27a73 7197 read_file_scope (die, cu);
c906108c 7198 break;
348e048f
DE
7199 case DW_TAG_type_unit:
7200 read_type_unit_scope (die, cu);
7201 break;
c906108c 7202 case DW_TAG_subprogram:
c906108c 7203 case DW_TAG_inlined_subroutine:
edb3359d 7204 read_func_scope (die, cu);
c906108c
SS
7205 break;
7206 case DW_TAG_lexical_block:
14898363
L
7207 case DW_TAG_try_block:
7208 case DW_TAG_catch_block:
e7c27a73 7209 read_lexical_block_scope (die, cu);
c906108c 7210 break;
96408a79
SA
7211 case DW_TAG_GNU_call_site:
7212 read_call_site_scope (die, cu);
7213 break;
c906108c 7214 case DW_TAG_class_type:
680b30c7 7215 case DW_TAG_interface_type:
c906108c
SS
7216 case DW_TAG_structure_type:
7217 case DW_TAG_union_type:
134d01f1 7218 process_structure_scope (die, cu);
c906108c
SS
7219 break;
7220 case DW_TAG_enumeration_type:
134d01f1 7221 process_enumeration_scope (die, cu);
c906108c 7222 break;
134d01f1 7223
f792889a
DJ
7224 /* These dies have a type, but processing them does not create
7225 a symbol or recurse to process the children. Therefore we can
7226 read them on-demand through read_type_die. */
c906108c 7227 case DW_TAG_subroutine_type:
72019c9c 7228 case DW_TAG_set_type:
c906108c 7229 case DW_TAG_array_type:
c906108c 7230 case DW_TAG_pointer_type:
c906108c 7231 case DW_TAG_ptr_to_member_type:
c906108c 7232 case DW_TAG_reference_type:
c906108c 7233 case DW_TAG_string_type:
c906108c 7234 break;
134d01f1 7235
c906108c 7236 case DW_TAG_base_type:
a02abb62 7237 case DW_TAG_subrange_type:
cb249c71 7238 case DW_TAG_typedef:
134d01f1
DJ
7239 /* Add a typedef symbol for the type definition, if it has a
7240 DW_AT_name. */
f792889a 7241 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7242 break;
c906108c 7243 case DW_TAG_common_block:
e7c27a73 7244 read_common_block (die, cu);
c906108c
SS
7245 break;
7246 case DW_TAG_common_inclusion:
7247 break;
d9fa45fe 7248 case DW_TAG_namespace:
63d06c5c 7249 processing_has_namespace_info = 1;
e7c27a73 7250 read_namespace (die, cu);
d9fa45fe 7251 break;
5d7cb8df 7252 case DW_TAG_module:
f55ee35c 7253 processing_has_namespace_info = 1;
5d7cb8df
JK
7254 read_module (die, cu);
7255 break;
d9fa45fe
DC
7256 case DW_TAG_imported_declaration:
7257 case DW_TAG_imported_module:
63d06c5c 7258 processing_has_namespace_info = 1;
27aa8d6a
SW
7259 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7260 || cu->language != language_fortran))
7261 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7262 dwarf_tag_name (die->tag));
7263 read_import_statement (die, cu);
d9fa45fe 7264 break;
95554aad
TT
7265
7266 case DW_TAG_imported_unit:
7267 process_imported_unit_die (die, cu);
7268 break;
7269
c906108c 7270 default:
e7c27a73 7271 new_symbol (die, NULL, cu);
c906108c
SS
7272 break;
7273 }
7274}
7275
94af9270
KS
7276/* A helper function for dwarf2_compute_name which determines whether DIE
7277 needs to have the name of the scope prepended to the name listed in the
7278 die. */
7279
7280static int
7281die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7282{
1c809c68
TT
7283 struct attribute *attr;
7284
94af9270
KS
7285 switch (die->tag)
7286 {
7287 case DW_TAG_namespace:
7288 case DW_TAG_typedef:
7289 case DW_TAG_class_type:
7290 case DW_TAG_interface_type:
7291 case DW_TAG_structure_type:
7292 case DW_TAG_union_type:
7293 case DW_TAG_enumeration_type:
7294 case DW_TAG_enumerator:
7295 case DW_TAG_subprogram:
7296 case DW_TAG_member:
7297 return 1;
7298
7299 case DW_TAG_variable:
c2b0a229 7300 case DW_TAG_constant:
94af9270
KS
7301 /* We only need to prefix "globally" visible variables. These include
7302 any variable marked with DW_AT_external or any variable that
7303 lives in a namespace. [Variables in anonymous namespaces
7304 require prefixing, but they are not DW_AT_external.] */
7305
7306 if (dwarf2_attr (die, DW_AT_specification, cu))
7307 {
7308 struct dwarf2_cu *spec_cu = cu;
9a619af0 7309
94af9270
KS
7310 return die_needs_namespace (die_specification (die, &spec_cu),
7311 spec_cu);
7312 }
7313
1c809c68 7314 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7315 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7316 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7317 return 0;
7318 /* A variable in a lexical block of some kind does not need a
7319 namespace, even though in C++ such variables may be external
7320 and have a mangled name. */
7321 if (die->parent->tag == DW_TAG_lexical_block
7322 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7323 || die->parent->tag == DW_TAG_catch_block
7324 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7325 return 0;
7326 return 1;
94af9270
KS
7327
7328 default:
7329 return 0;
7330 }
7331}
7332
98bfdba5
PA
7333/* Retrieve the last character from a mem_file. */
7334
7335static void
7336do_ui_file_peek_last (void *object, const char *buffer, long length)
7337{
7338 char *last_char_p = (char *) object;
7339
7340 if (length > 0)
7341 *last_char_p = buffer[length - 1];
7342}
7343
94af9270 7344/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7345 compute the physname for the object, which include a method's:
7346 - formal parameters (C++/Java),
7347 - receiver type (Go),
7348 - return type (Java).
7349
7350 The term "physname" is a bit confusing.
7351 For C++, for example, it is the demangled name.
7352 For Go, for example, it's the mangled name.
94af9270 7353
af6b7be1
JB
7354 For Ada, return the DIE's linkage name rather than the fully qualified
7355 name. PHYSNAME is ignored..
7356
94af9270
KS
7357 The result is allocated on the objfile_obstack and canonicalized. */
7358
7359static const char *
7360dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7361 int physname)
7362{
bb5ed363
DE
7363 struct objfile *objfile = cu->objfile;
7364
94af9270
KS
7365 if (name == NULL)
7366 name = dwarf2_name (die, cu);
7367
f55ee35c
JK
7368 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7369 compute it by typename_concat inside GDB. */
7370 if (cu->language == language_ada
7371 || (cu->language == language_fortran && physname))
7372 {
7373 /* For Ada unit, we prefer the linkage name over the name, as
7374 the former contains the exported name, which the user expects
7375 to be able to reference. Ideally, we want the user to be able
7376 to reference this entity using either natural or linkage name,
7377 but we haven't started looking at this enhancement yet. */
7378 struct attribute *attr;
7379
7380 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7381 if (attr == NULL)
7382 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7383 if (attr && DW_STRING (attr))
7384 return DW_STRING (attr);
7385 }
7386
94af9270
KS
7387 /* These are the only languages we know how to qualify names in. */
7388 if (name != NULL
f55ee35c
JK
7389 && (cu->language == language_cplus || cu->language == language_java
7390 || cu->language == language_fortran))
94af9270
KS
7391 {
7392 if (die_needs_namespace (die, cu))
7393 {
7394 long length;
0d5cff50 7395 const char *prefix;
94af9270
KS
7396 struct ui_file *buf;
7397
7398 prefix = determine_prefix (die, cu);
7399 buf = mem_fileopen ();
7400 if (*prefix != '\0')
7401 {
f55ee35c
JK
7402 char *prefixed_name = typename_concat (NULL, prefix, name,
7403 physname, cu);
9a619af0 7404
94af9270
KS
7405 fputs_unfiltered (prefixed_name, buf);
7406 xfree (prefixed_name);
7407 }
7408 else
62d5b8da 7409 fputs_unfiltered (name, buf);
94af9270 7410
98bfdba5
PA
7411 /* Template parameters may be specified in the DIE's DW_AT_name, or
7412 as children with DW_TAG_template_type_param or
7413 DW_TAG_value_type_param. If the latter, add them to the name
7414 here. If the name already has template parameters, then
7415 skip this step; some versions of GCC emit both, and
7416 it is more efficient to use the pre-computed name.
7417
7418 Something to keep in mind about this process: it is very
7419 unlikely, or in some cases downright impossible, to produce
7420 something that will match the mangled name of a function.
7421 If the definition of the function has the same debug info,
7422 we should be able to match up with it anyway. But fallbacks
7423 using the minimal symbol, for instance to find a method
7424 implemented in a stripped copy of libstdc++, will not work.
7425 If we do not have debug info for the definition, we will have to
7426 match them up some other way.
7427
7428 When we do name matching there is a related problem with function
7429 templates; two instantiated function templates are allowed to
7430 differ only by their return types, which we do not add here. */
7431
7432 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7433 {
7434 struct attribute *attr;
7435 struct die_info *child;
7436 int first = 1;
7437
7438 die->building_fullname = 1;
7439
7440 for (child = die->child; child != NULL; child = child->sibling)
7441 {
7442 struct type *type;
12df843f 7443 LONGEST value;
98bfdba5
PA
7444 gdb_byte *bytes;
7445 struct dwarf2_locexpr_baton *baton;
7446 struct value *v;
7447
7448 if (child->tag != DW_TAG_template_type_param
7449 && child->tag != DW_TAG_template_value_param)
7450 continue;
7451
7452 if (first)
7453 {
7454 fputs_unfiltered ("<", buf);
7455 first = 0;
7456 }
7457 else
7458 fputs_unfiltered (", ", buf);
7459
7460 attr = dwarf2_attr (child, DW_AT_type, cu);
7461 if (attr == NULL)
7462 {
7463 complaint (&symfile_complaints,
7464 _("template parameter missing DW_AT_type"));
7465 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7466 continue;
7467 }
7468 type = die_type (child, cu);
7469
7470 if (child->tag == DW_TAG_template_type_param)
7471 {
79d43c61 7472 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7473 continue;
7474 }
7475
7476 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7477 if (attr == NULL)
7478 {
7479 complaint (&symfile_complaints,
3e43a32a
MS
7480 _("template parameter missing "
7481 "DW_AT_const_value"));
98bfdba5
PA
7482 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7483 continue;
7484 }
7485
7486 dwarf2_const_value_attr (attr, type, name,
7487 &cu->comp_unit_obstack, cu,
7488 &value, &bytes, &baton);
7489
7490 if (TYPE_NOSIGN (type))
7491 /* GDB prints characters as NUMBER 'CHAR'. If that's
7492 changed, this can use value_print instead. */
7493 c_printchar (value, type, buf);
7494 else
7495 {
7496 struct value_print_options opts;
7497
7498 if (baton != NULL)
7499 v = dwarf2_evaluate_loc_desc (type, NULL,
7500 baton->data,
7501 baton->size,
7502 baton->per_cu);
7503 else if (bytes != NULL)
7504 {
7505 v = allocate_value (type);
7506 memcpy (value_contents_writeable (v), bytes,
7507 TYPE_LENGTH (type));
7508 }
7509 else
7510 v = value_from_longest (type, value);
7511
3e43a32a
MS
7512 /* Specify decimal so that we do not depend on
7513 the radix. */
98bfdba5
PA
7514 get_formatted_print_options (&opts, 'd');
7515 opts.raw = 1;
7516 value_print (v, buf, &opts);
7517 release_value (v);
7518 value_free (v);
7519 }
7520 }
7521
7522 die->building_fullname = 0;
7523
7524 if (!first)
7525 {
7526 /* Close the argument list, with a space if necessary
7527 (nested templates). */
7528 char last_char = '\0';
7529 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7530 if (last_char == '>')
7531 fputs_unfiltered (" >", buf);
7532 else
7533 fputs_unfiltered (">", buf);
7534 }
7535 }
7536
94af9270
KS
7537 /* For Java and C++ methods, append formal parameter type
7538 information, if PHYSNAME. */
6e70227d 7539
94af9270
KS
7540 if (physname && die->tag == DW_TAG_subprogram
7541 && (cu->language == language_cplus
7542 || cu->language == language_java))
7543 {
7544 struct type *type = read_type_die (die, cu);
7545
79d43c61
TT
7546 c_type_print_args (type, buf, 1, cu->language,
7547 &type_print_raw_options);
94af9270
KS
7548
7549 if (cu->language == language_java)
7550 {
7551 /* For java, we must append the return type to method
0963b4bd 7552 names. */
94af9270
KS
7553 if (die->tag == DW_TAG_subprogram)
7554 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7555 0, 0, &type_print_raw_options);
94af9270
KS
7556 }
7557 else if (cu->language == language_cplus)
7558 {
60430eff
DJ
7559 /* Assume that an artificial first parameter is
7560 "this", but do not crash if it is not. RealView
7561 marks unnamed (and thus unused) parameters as
7562 artificial; there is no way to differentiate
7563 the two cases. */
94af9270
KS
7564 if (TYPE_NFIELDS (type) > 0
7565 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7566 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7567 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7568 0))))
94af9270
KS
7569 fputs_unfiltered (" const", buf);
7570 }
7571 }
7572
bb5ed363 7573 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7574 &length);
7575 ui_file_delete (buf);
7576
7577 if (cu->language == language_cplus)
7578 {
7579 char *cname
7580 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7581 &objfile->objfile_obstack);
9a619af0 7582
94af9270
KS
7583 if (cname != NULL)
7584 name = cname;
7585 }
7586 }
7587 }
7588
7589 return name;
7590}
7591
0114d602
DJ
7592/* Return the fully qualified name of DIE, based on its DW_AT_name.
7593 If scope qualifiers are appropriate they will be added. The result
7594 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7595 not have a name. NAME may either be from a previous call to
7596 dwarf2_name or NULL.
7597
0963b4bd 7598 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7599
7600static const char *
94af9270 7601dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7602{
94af9270
KS
7603 return dwarf2_compute_name (name, die, cu, 0);
7604}
0114d602 7605
94af9270
KS
7606/* Construct a physname for the given DIE in CU. NAME may either be
7607 from a previous call to dwarf2_name or NULL. The result will be
7608 allocated on the objfile_objstack or NULL if the DIE does not have a
7609 name.
0114d602 7610
94af9270 7611 The output string will be canonicalized (if C++/Java). */
0114d602 7612
94af9270
KS
7613static const char *
7614dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7615{
bb5ed363 7616 struct objfile *objfile = cu->objfile;
900e11f9
JK
7617 struct attribute *attr;
7618 const char *retval, *mangled = NULL, *canon = NULL;
7619 struct cleanup *back_to;
7620 int need_copy = 1;
7621
7622 /* In this case dwarf2_compute_name is just a shortcut not building anything
7623 on its own. */
7624 if (!die_needs_namespace (die, cu))
7625 return dwarf2_compute_name (name, die, cu, 1);
7626
7627 back_to = make_cleanup (null_cleanup, NULL);
7628
7629 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7630 if (!attr)
7631 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7632
7633 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7634 has computed. */
7635 if (attr && DW_STRING (attr))
7636 {
7637 char *demangled;
7638
7639 mangled = DW_STRING (attr);
7640
7641 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7642 type. It is easier for GDB users to search for such functions as
7643 `name(params)' than `long name(params)'. In such case the minimal
7644 symbol names do not match the full symbol names but for template
7645 functions there is never a need to look up their definition from their
7646 declaration so the only disadvantage remains the minimal symbol
7647 variant `long name(params)' does not have the proper inferior type.
7648 */
7649
a766d390
DE
7650 if (cu->language == language_go)
7651 {
7652 /* This is a lie, but we already lie to the caller new_symbol_full.
7653 new_symbol_full assumes we return the mangled name.
7654 This just undoes that lie until things are cleaned up. */
7655 demangled = NULL;
7656 }
7657 else
7658 {
7659 demangled = cplus_demangle (mangled,
7660 (DMGL_PARAMS | DMGL_ANSI
7661 | (cu->language == language_java
7662 ? DMGL_JAVA | DMGL_RET_POSTFIX
7663 : DMGL_RET_DROP)));
7664 }
900e11f9
JK
7665 if (demangled)
7666 {
7667 make_cleanup (xfree, demangled);
7668 canon = demangled;
7669 }
7670 else
7671 {
7672 canon = mangled;
7673 need_copy = 0;
7674 }
7675 }
7676
7677 if (canon == NULL || check_physname)
7678 {
7679 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7680
7681 if (canon != NULL && strcmp (physname, canon) != 0)
7682 {
7683 /* It may not mean a bug in GDB. The compiler could also
7684 compute DW_AT_linkage_name incorrectly. But in such case
7685 GDB would need to be bug-to-bug compatible. */
7686
7687 complaint (&symfile_complaints,
7688 _("Computed physname <%s> does not match demangled <%s> "
7689 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7690 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7691
7692 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7693 is available here - over computed PHYSNAME. It is safer
7694 against both buggy GDB and buggy compilers. */
7695
7696 retval = canon;
7697 }
7698 else
7699 {
7700 retval = physname;
7701 need_copy = 0;
7702 }
7703 }
7704 else
7705 retval = canon;
7706
7707 if (need_copy)
7708 retval = obsavestring (retval, strlen (retval),
bb5ed363 7709 &objfile->objfile_obstack);
900e11f9
JK
7710
7711 do_cleanups (back_to);
7712 return retval;
0114d602
DJ
7713}
7714
27aa8d6a
SW
7715/* Read the import statement specified by the given die and record it. */
7716
7717static void
7718read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7719{
bb5ed363 7720 struct objfile *objfile = cu->objfile;
27aa8d6a 7721 struct attribute *import_attr;
32019081 7722 struct die_info *imported_die, *child_die;
de4affc9 7723 struct dwarf2_cu *imported_cu;
27aa8d6a 7724 const char *imported_name;
794684b6 7725 const char *imported_name_prefix;
13387711
SW
7726 const char *canonical_name;
7727 const char *import_alias;
7728 const char *imported_declaration = NULL;
794684b6 7729 const char *import_prefix;
32019081
JK
7730 VEC (const_char_ptr) *excludes = NULL;
7731 struct cleanup *cleanups;
13387711
SW
7732
7733 char *temp;
27aa8d6a
SW
7734
7735 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7736 if (import_attr == NULL)
7737 {
7738 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7739 dwarf_tag_name (die->tag));
7740 return;
7741 }
7742
de4affc9
CC
7743 imported_cu = cu;
7744 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7745 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7746 if (imported_name == NULL)
7747 {
7748 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7749
7750 The import in the following code:
7751 namespace A
7752 {
7753 typedef int B;
7754 }
7755
7756 int main ()
7757 {
7758 using A::B;
7759 B b;
7760 return b;
7761 }
7762
7763 ...
7764 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7765 <52> DW_AT_decl_file : 1
7766 <53> DW_AT_decl_line : 6
7767 <54> DW_AT_import : <0x75>
7768 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7769 <59> DW_AT_name : B
7770 <5b> DW_AT_decl_file : 1
7771 <5c> DW_AT_decl_line : 2
7772 <5d> DW_AT_type : <0x6e>
7773 ...
7774 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7775 <76> DW_AT_byte_size : 4
7776 <77> DW_AT_encoding : 5 (signed)
7777
7778 imports the wrong die ( 0x75 instead of 0x58 ).
7779 This case will be ignored until the gcc bug is fixed. */
7780 return;
7781 }
7782
82856980
SW
7783 /* Figure out the local name after import. */
7784 import_alias = dwarf2_name (die, cu);
27aa8d6a 7785
794684b6
SW
7786 /* Figure out where the statement is being imported to. */
7787 import_prefix = determine_prefix (die, cu);
7788
7789 /* Figure out what the scope of the imported die is and prepend it
7790 to the name of the imported die. */
de4affc9 7791 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7792
f55ee35c
JK
7793 if (imported_die->tag != DW_TAG_namespace
7794 && imported_die->tag != DW_TAG_module)
794684b6 7795 {
13387711
SW
7796 imported_declaration = imported_name;
7797 canonical_name = imported_name_prefix;
794684b6 7798 }
13387711 7799 else if (strlen (imported_name_prefix) > 0)
794684b6 7800 {
13387711
SW
7801 temp = alloca (strlen (imported_name_prefix)
7802 + 2 + strlen (imported_name) + 1);
7803 strcpy (temp, imported_name_prefix);
7804 strcat (temp, "::");
7805 strcat (temp, imported_name);
7806 canonical_name = temp;
794684b6 7807 }
13387711
SW
7808 else
7809 canonical_name = imported_name;
794684b6 7810
32019081
JK
7811 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7812
7813 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7814 for (child_die = die->child; child_die && child_die->tag;
7815 child_die = sibling_die (child_die))
7816 {
7817 /* DWARF-4: A Fortran use statement with a “rename list” may be
7818 represented by an imported module entry with an import attribute
7819 referring to the module and owned entries corresponding to those
7820 entities that are renamed as part of being imported. */
7821
7822 if (child_die->tag != DW_TAG_imported_declaration)
7823 {
7824 complaint (&symfile_complaints,
7825 _("child DW_TAG_imported_declaration expected "
7826 "- DIE at 0x%x [in module %s]"),
b64f50a1 7827 child_die->offset.sect_off, objfile->name);
32019081
JK
7828 continue;
7829 }
7830
7831 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7832 if (import_attr == NULL)
7833 {
7834 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7835 dwarf_tag_name (child_die->tag));
7836 continue;
7837 }
7838
7839 imported_cu = cu;
7840 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7841 &imported_cu);
7842 imported_name = dwarf2_name (imported_die, imported_cu);
7843 if (imported_name == NULL)
7844 {
7845 complaint (&symfile_complaints,
7846 _("child DW_TAG_imported_declaration has unknown "
7847 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7848 child_die->offset.sect_off, objfile->name);
32019081
JK
7849 continue;
7850 }
7851
7852 VEC_safe_push (const_char_ptr, excludes, imported_name);
7853
7854 process_die (child_die, cu);
7855 }
7856
c0cc3a76
SW
7857 cp_add_using_directive (import_prefix,
7858 canonical_name,
7859 import_alias,
13387711 7860 imported_declaration,
32019081 7861 excludes,
bb5ed363 7862 &objfile->objfile_obstack);
32019081
JK
7863
7864 do_cleanups (cleanups);
27aa8d6a
SW
7865}
7866
f4dc4d17 7867/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7868
cb1df416
DJ
7869static void
7870free_cu_line_header (void *arg)
7871{
7872 struct dwarf2_cu *cu = arg;
7873
7874 free_line_header (cu->line_header);
7875 cu->line_header = NULL;
7876}
7877
1b80a9fa
JK
7878/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7879 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7880 this, it was first present in GCC release 4.3.0. */
7881
7882static int
7883producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7884{
7885 if (!cu->checked_producer)
7886 check_producer (cu);
7887
7888 return cu->producer_is_gcc_lt_4_3;
7889}
7890
9291a0cd
TT
7891static void
7892find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7893 char **name, char **comp_dir)
7894{
7895 struct attribute *attr;
7896
7897 *name = NULL;
7898 *comp_dir = NULL;
7899
7900 /* Find the filename. Do not use dwarf2_name here, since the filename
7901 is not a source language identifier. */
7902 attr = dwarf2_attr (die, DW_AT_name, cu);
7903 if (attr)
7904 {
7905 *name = DW_STRING (attr);
7906 }
7907
7908 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7909 if (attr)
7910 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7911 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7912 && IS_ABSOLUTE_PATH (*name))
9291a0cd
TT
7913 {
7914 *comp_dir = ldirname (*name);
7915 if (*comp_dir != NULL)
7916 make_cleanup (xfree, *comp_dir);
7917 }
7918 if (*comp_dir != NULL)
7919 {
7920 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7921 directory, get rid of it. */
7922 char *cp = strchr (*comp_dir, ':');
7923
7924 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7925 *comp_dir = cp + 1;
7926 }
7927
7928 if (*name == NULL)
7929 *name = "<unknown>";
7930}
7931
f4dc4d17
DE
7932/* Handle DW_AT_stmt_list for a compilation unit.
7933 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7934 COMP_DIR is the compilation directory.
7935 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7936
7937static void
7938handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7939 const char *comp_dir)
2ab95328
TT
7940{
7941 struct attribute *attr;
2ab95328 7942
f4dc4d17
DE
7943 gdb_assert (! cu->per_cu->is_debug_types);
7944
2ab95328
TT
7945 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7946 if (attr)
7947 {
7948 unsigned int line_offset = DW_UNSND (attr);
7949 struct line_header *line_header
3019eac3 7950 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7951
7952 if (line_header)
dee91e82
DE
7953 {
7954 cu->line_header = line_header;
7955 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7956 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7957 }
2ab95328
TT
7958 }
7959}
7960
95554aad 7961/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7962
c906108c 7963static void
e7c27a73 7964read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7965{
dee91e82 7966 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7967 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7968 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7969 CORE_ADDR highpc = ((CORE_ADDR) 0);
7970 struct attribute *attr;
e1024ff1 7971 char *name = NULL;
c906108c
SS
7972 char *comp_dir = NULL;
7973 struct die_info *child_die;
7974 bfd *abfd = objfile->obfd;
e142c38c 7975 CORE_ADDR baseaddr;
6e70227d 7976
e142c38c 7977 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7978
fae299cd 7979 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7980
7981 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7982 from finish_block. */
2acceee2 7983 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7984 lowpc = highpc;
7985 lowpc += baseaddr;
7986 highpc += baseaddr;
7987
9291a0cd 7988 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7989
95554aad 7990 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7991
f4b8a18d
KW
7992 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7993 standardised yet. As a workaround for the language detection we fall
7994 back to the DW_AT_producer string. */
7995 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7996 cu->language = language_opencl;
7997
3019eac3
DE
7998 /* Similar hack for Go. */
7999 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8000 set_cu_language (DW_LANG_Go, cu);
8001
f4dc4d17 8002 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8003
8004 /* Decode line number information if present. We do this before
8005 processing child DIEs, so that the line header table is available
8006 for DW_AT_decl_file. */
f4dc4d17 8007 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8008
8009 /* Process all dies in compilation unit. */
8010 if (die->child != NULL)
8011 {
8012 child_die = die->child;
8013 while (child_die && child_die->tag)
8014 {
8015 process_die (child_die, cu);
8016 child_die = sibling_die (child_die);
8017 }
8018 }
8019
8020 /* Decode macro information, if present. Dwarf 2 macro information
8021 refers to information in the line number info statement program
8022 header, so we can only read it if we've read the header
8023 successfully. */
8024 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8025 if (attr && cu->line_header)
8026 {
8027 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8028 complaint (&symfile_complaints,
8029 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8030
09262596 8031 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8032 }
8033 else
8034 {
8035 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8036 if (attr && cu->line_header)
8037 {
8038 unsigned int macro_offset = DW_UNSND (attr);
8039
09262596 8040 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8041 }
8042 }
8043
8044 do_cleanups (back_to);
8045}
8046
f4dc4d17
DE
8047/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8048 Create the set of symtabs used by this TU, or if this TU is sharing
8049 symtabs with another TU and the symtabs have already been created
8050 then restore those symtabs in the line header.
8051 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8052
8053static void
f4dc4d17 8054setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8055{
f4dc4d17
DE
8056 struct objfile *objfile = dwarf2_per_objfile->objfile;
8057 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8058 struct type_unit_group *tu_group;
8059 int first_time;
8060 struct line_header *lh;
3019eac3 8061 struct attribute *attr;
f4dc4d17 8062 unsigned int i, line_offset;
3019eac3 8063
f4dc4d17 8064 gdb_assert (per_cu->is_debug_types);
3019eac3 8065
f4dc4d17 8066 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8067
f4dc4d17
DE
8068 /* If we're using .gdb_index (includes -readnow) then
8069 per_cu->s.type_unit_group may not have been set up yet. */
8070 if (per_cu->s.type_unit_group == NULL)
094b34ac 8071 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
8072 tu_group = per_cu->s.type_unit_group;
8073
8074 /* If we've already processed this stmt_list there's no real need to
8075 do it again, we could fake it and just recreate the part we need
8076 (file name,index -> symtab mapping). If data shows this optimization
8077 is useful we can do it then. */
8078 first_time = tu_group->primary_symtab == NULL;
8079
8080 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8081 debug info. */
8082 lh = NULL;
8083 if (attr != NULL)
3019eac3 8084 {
f4dc4d17
DE
8085 line_offset = DW_UNSND (attr);
8086 lh = dwarf_decode_line_header (line_offset, cu);
8087 }
8088 if (lh == NULL)
8089 {
8090 if (first_time)
8091 dwarf2_start_symtab (cu, "", NULL, 0);
8092 else
8093 {
8094 gdb_assert (tu_group->symtabs == NULL);
8095 restart_symtab (0);
8096 }
8097 /* Note: The primary symtab will get allocated at the end. */
8098 return;
3019eac3
DE
8099 }
8100
f4dc4d17
DE
8101 cu->line_header = lh;
8102 make_cleanup (free_cu_line_header, cu);
3019eac3 8103
f4dc4d17
DE
8104 if (first_time)
8105 {
8106 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8107
f4dc4d17
DE
8108 tu_group->num_symtabs = lh->num_file_names;
8109 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8110
f4dc4d17
DE
8111 for (i = 0; i < lh->num_file_names; ++i)
8112 {
8113 char *dir = NULL;
8114 struct file_entry *fe = &lh->file_names[i];
3019eac3 8115
f4dc4d17
DE
8116 if (fe->dir_index)
8117 dir = lh->include_dirs[fe->dir_index - 1];
8118 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8119
f4dc4d17
DE
8120 /* Note: We don't have to watch for the main subfile here, type units
8121 don't have DW_AT_name. */
3019eac3 8122
f4dc4d17
DE
8123 if (current_subfile->symtab == NULL)
8124 {
8125 /* NOTE: start_subfile will recognize when it's been passed
8126 a file it has already seen. So we can't assume there's a
8127 simple mapping from lh->file_names to subfiles,
8128 lh->file_names may contain dups. */
8129 current_subfile->symtab = allocate_symtab (current_subfile->name,
8130 objfile);
8131 }
8132
8133 fe->symtab = current_subfile->symtab;
8134 tu_group->symtabs[i] = fe->symtab;
8135 }
8136 }
8137 else
3019eac3 8138 {
f4dc4d17
DE
8139 restart_symtab (0);
8140
8141 for (i = 0; i < lh->num_file_names; ++i)
8142 {
8143 struct file_entry *fe = &lh->file_names[i];
8144
8145 fe->symtab = tu_group->symtabs[i];
8146 }
3019eac3
DE
8147 }
8148
f4dc4d17
DE
8149 /* The main symtab is allocated last. Type units don't have DW_AT_name
8150 so they don't have a "real" (so to speak) symtab anyway.
8151 There is later code that will assign the main symtab to all symbols
8152 that don't have one. We need to handle the case of a symbol with a
8153 missing symtab (DW_AT_decl_file) anyway. */
8154}
3019eac3 8155
f4dc4d17
DE
8156/* Process DW_TAG_type_unit.
8157 For TUs we want to skip the first top level sibling if it's not the
8158 actual type being defined by this TU. In this case the first top
8159 level sibling is there to provide context only. */
3019eac3 8160
f4dc4d17
DE
8161static void
8162read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8163{
8164 struct die_info *child_die;
3019eac3 8165
f4dc4d17
DE
8166 prepare_one_comp_unit (cu, die, language_minimal);
8167
8168 /* Initialize (or reinitialize) the machinery for building symtabs.
8169 We do this before processing child DIEs, so that the line header table
8170 is available for DW_AT_decl_file. */
8171 setup_type_unit_groups (die, cu);
8172
8173 if (die->child != NULL)
8174 {
8175 child_die = die->child;
8176 while (child_die && child_die->tag)
8177 {
8178 process_die (child_die, cu);
8179 child_die = sibling_die (child_die);
8180 }
8181 }
3019eac3
DE
8182}
8183\f
80626a55
DE
8184/* DWO/DWP files.
8185
8186 http://gcc.gnu.org/wiki/DebugFission
8187 http://gcc.gnu.org/wiki/DebugFissionDWP
8188
8189 To simplify handling of both DWO files ("object" files with the DWARF info)
8190 and DWP files (a file with the DWOs packaged up into one file), we treat
8191 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8192
8193static hashval_t
8194hash_dwo_file (const void *item)
8195{
8196 const struct dwo_file *dwo_file = item;
8197
80626a55 8198 return htab_hash_string (dwo_file->name);
3019eac3
DE
8199}
8200
8201static int
8202eq_dwo_file (const void *item_lhs, const void *item_rhs)
8203{
8204 const struct dwo_file *lhs = item_lhs;
8205 const struct dwo_file *rhs = item_rhs;
8206
80626a55 8207 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8208}
8209
8210/* Allocate a hash table for DWO files. */
8211
8212static htab_t
8213allocate_dwo_file_hash_table (void)
8214{
8215 struct objfile *objfile = dwarf2_per_objfile->objfile;
8216
8217 return htab_create_alloc_ex (41,
8218 hash_dwo_file,
8219 eq_dwo_file,
8220 NULL,
8221 &objfile->objfile_obstack,
8222 hashtab_obstack_allocate,
8223 dummy_obstack_deallocate);
8224}
8225
80626a55
DE
8226/* Lookup DWO file DWO_NAME. */
8227
8228static void **
8229lookup_dwo_file_slot (const char *dwo_name)
8230{
8231 struct dwo_file find_entry;
8232 void **slot;
8233
8234 if (dwarf2_per_objfile->dwo_files == NULL)
8235 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8236
8237 memset (&find_entry, 0, sizeof (find_entry));
8238 find_entry.name = dwo_name;
8239 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8240
8241 return slot;
8242}
8243
3019eac3
DE
8244static hashval_t
8245hash_dwo_unit (const void *item)
8246{
8247 const struct dwo_unit *dwo_unit = item;
8248
8249 /* This drops the top 32 bits of the id, but is ok for a hash. */
8250 return dwo_unit->signature;
8251}
8252
8253static int
8254eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8255{
8256 const struct dwo_unit *lhs = item_lhs;
8257 const struct dwo_unit *rhs = item_rhs;
8258
8259 /* The signature is assumed to be unique within the DWO file.
8260 So while object file CU dwo_id's always have the value zero,
8261 that's OK, assuming each object file DWO file has only one CU,
8262 and that's the rule for now. */
8263 return lhs->signature == rhs->signature;
8264}
8265
8266/* Allocate a hash table for DWO CUs,TUs.
8267 There is one of these tables for each of CUs,TUs for each DWO file. */
8268
8269static htab_t
8270allocate_dwo_unit_table (struct objfile *objfile)
8271{
8272 /* Start out with a pretty small number.
8273 Generally DWO files contain only one CU and maybe some TUs. */
8274 return htab_create_alloc_ex (3,
8275 hash_dwo_unit,
8276 eq_dwo_unit,
8277 NULL,
8278 &objfile->objfile_obstack,
8279 hashtab_obstack_allocate,
8280 dummy_obstack_deallocate);
8281}
8282
80626a55 8283/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8284
8285struct create_dwo_info_table_data
8286{
8287 struct dwo_file *dwo_file;
8288 htab_t cu_htab;
8289};
8290
80626a55 8291/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8292
8293static void
80626a55
DE
8294create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8295 gdb_byte *info_ptr,
8296 struct die_info *comp_unit_die,
8297 int has_children,
8298 void *datap)
3019eac3
DE
8299{
8300 struct dwarf2_cu *cu = reader->cu;
8301 struct objfile *objfile = dwarf2_per_objfile->objfile;
8302 sect_offset offset = cu->per_cu->offset;
8303 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8304 struct create_dwo_info_table_data *data = datap;
8305 struct dwo_file *dwo_file = data->dwo_file;
8306 htab_t cu_htab = data->cu_htab;
8307 void **slot;
8308 struct attribute *attr;
8309 struct dwo_unit *dwo_unit;
8310
8311 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8312 if (attr == NULL)
8313 {
8314 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8315 " its dwo_id [in module %s]"),
80626a55 8316 offset.sect_off, dwo_file->name);
3019eac3
DE
8317 return;
8318 }
8319
8320 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8321 dwo_unit->dwo_file = dwo_file;
8322 dwo_unit->signature = DW_UNSND (attr);
8323 dwo_unit->info_or_types_section = section;
8324 dwo_unit->offset = offset;
8325 dwo_unit->length = cu->per_cu->length;
8326
8327 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8328 gdb_assert (slot != NULL);
8329 if (*slot != NULL)
8330 {
8331 const struct dwo_unit *dup_dwo_unit = *slot;
8332
8333 complaint (&symfile_complaints,
8334 _("debug entry at offset 0x%x is duplicate to the entry at"
8335 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8336 offset.sect_off, dup_dwo_unit->offset.sect_off,
8337 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8338 dwo_file->name);
3019eac3
DE
8339 }
8340 else
8341 *slot = dwo_unit;
8342
09406207 8343 if (dwarf2_read_debug)
3019eac3
DE
8344 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8345 offset.sect_off,
8346 phex (dwo_unit->signature,
8347 sizeof (dwo_unit->signature)));
8348}
8349
80626a55
DE
8350/* Create a hash table to map DWO IDs to their CU entry in
8351 .debug_info.dwo in DWO_FILE.
8352 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8353
8354static htab_t
80626a55 8355create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8356{
8357 struct objfile *objfile = dwarf2_per_objfile->objfile;
8358 struct dwarf2_section_info *section = &dwo_file->sections.info;
8359 bfd *abfd;
8360 htab_t cu_htab;
8361 gdb_byte *info_ptr, *end_ptr;
8362 struct create_dwo_info_table_data create_dwo_info_table_data;
8363
8364 dwarf2_read_section (objfile, section);
8365 info_ptr = section->buffer;
8366
8367 if (info_ptr == NULL)
8368 return NULL;
8369
8370 /* We can't set abfd until now because the section may be empty or
8371 not present, in which case section->asection will be NULL. */
8372 abfd = section->asection->owner;
8373
09406207 8374 if (dwarf2_read_debug)
3019eac3
DE
8375 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8376 bfd_get_filename (abfd));
8377
8378 cu_htab = allocate_dwo_unit_table (objfile);
8379
8380 create_dwo_info_table_data.dwo_file = dwo_file;
8381 create_dwo_info_table_data.cu_htab = cu_htab;
8382
8383 end_ptr = info_ptr + section->size;
8384 while (info_ptr < end_ptr)
8385 {
8386 struct dwarf2_per_cu_data per_cu;
8387
8388 memset (&per_cu, 0, sizeof (per_cu));
8389 per_cu.objfile = objfile;
8390 per_cu.is_debug_types = 0;
8391 per_cu.offset.sect_off = info_ptr - section->buffer;
8392 per_cu.info_or_types_section = section;
8393
8394 init_cutu_and_read_dies_no_follow (&per_cu,
8395 &dwo_file->sections.abbrev,
8396 dwo_file,
80626a55 8397 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8398 &create_dwo_info_table_data);
8399
8400 info_ptr += per_cu.length;
8401 }
8402
8403 return cu_htab;
8404}
8405
80626a55
DE
8406/* DWP file .debug_{cu,tu}_index section format:
8407 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8408
8409 Both index sections have the same format, and serve to map a 64-bit
8410 signature to a set of section numbers. Each section begins with a header,
8411 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8412 indexes, and a pool of 32-bit section numbers. The index sections will be
8413 aligned at 8-byte boundaries in the file.
8414
8415 The index section header contains two unsigned 32-bit values (using the
8416 byte order of the application binary):
8417
8418 N, the number of compilation units or type units in the index
8419 M, the number of slots in the hash table
8420
8421 (We assume that N and M will not exceed 2^32 - 1.)
8422
8423 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8424
8425 The hash table begins at offset 8 in the section, and consists of an array
8426 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8427 order of the application binary). Unused slots in the hash table are 0.
8428 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8429
8430 The parallel table begins immediately after the hash table
8431 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8432 array of 32-bit indexes (using the byte order of the application binary),
8433 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8434 table contains a 32-bit index into the pool of section numbers. For unused
8435 hash table slots, the corresponding entry in the parallel table will be 0.
8436
8437 Given a 64-bit compilation unit signature or a type signature S, an entry
8438 in the hash table is located as follows:
8439
8440 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8441 the low-order k bits all set to 1.
8442
8443 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8444
8445 3) If the hash table entry at index H matches the signature, use that
8446 entry. If the hash table entry at index H is unused (all zeroes),
8447 terminate the search: the signature is not present in the table.
8448
8449 4) Let H = (H + H') modulo M. Repeat at Step 3.
8450
8451 Because M > N and H' and M are relatively prime, the search is guaranteed
8452 to stop at an unused slot or find the match.
8453
8454 The pool of section numbers begins immediately following the hash table
8455 (at offset 8 + 12 * M from the beginning of the section). The pool of
8456 section numbers consists of an array of 32-bit words (using the byte order
8457 of the application binary). Each item in the array is indexed starting
8458 from 0. The hash table entry provides the index of the first section
8459 number in the set. Additional section numbers in the set follow, and the
8460 set is terminated by a 0 entry (section number 0 is not used in ELF).
8461
8462 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8463 section must be the first entry in the set, and the .debug_abbrev.dwo must
8464 be the second entry. Other members of the set may follow in any order. */
8465
8466/* Create a hash table to map DWO IDs to their CU/TU entry in
8467 .debug_{info,types}.dwo in DWP_FILE.
8468 Returns NULL if there isn't one.
8469 Note: This function processes DWP files only, not DWO files. */
8470
8471static struct dwp_hash_table *
8472create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8473{
8474 struct objfile *objfile = dwarf2_per_objfile->objfile;
8475 bfd *dbfd = dwp_file->dbfd;
8476 char *index_ptr, *index_end;
8477 struct dwarf2_section_info *index;
8478 uint32_t version, nr_units, nr_slots;
8479 struct dwp_hash_table *htab;
8480
8481 if (is_debug_types)
8482 index = &dwp_file->sections.tu_index;
8483 else
8484 index = &dwp_file->sections.cu_index;
8485
8486 if (dwarf2_section_empty_p (index))
8487 return NULL;
8488 dwarf2_read_section (objfile, index);
8489
8490 index_ptr = index->buffer;
8491 index_end = index_ptr + index->size;
8492
8493 version = read_4_bytes (dbfd, index_ptr);
8494 index_ptr += 8; /* Skip the unused word. */
8495 nr_units = read_4_bytes (dbfd, index_ptr);
8496 index_ptr += 4;
8497 nr_slots = read_4_bytes (dbfd, index_ptr);
8498 index_ptr += 4;
8499
8500 if (version != 1)
8501 {
8502 error (_("Dwarf Error: unsupported DWP file version (%u)"
8503 " [in module %s]"),
8504 version, dwp_file->name);
8505 }
8506 if (nr_slots != (nr_slots & -nr_slots))
8507 {
8508 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8509 " is not power of 2 [in module %s]"),
8510 nr_slots, dwp_file->name);
8511 }
8512
8513 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8514 htab->nr_units = nr_units;
8515 htab->nr_slots = nr_slots;
8516 htab->hash_table = index_ptr;
8517 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8518 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8519
8520 return htab;
8521}
8522
8523/* Update SECTIONS with the data from SECTP.
8524
8525 This function is like the other "locate" section routines that are
8526 passed to bfd_map_over_sections, but in this context the sections to
8527 read comes from the DWP hash table, not the full ELF section table.
8528
8529 The result is non-zero for success, or zero if an error was found. */
8530
8531static int
8532locate_virtual_dwo_sections (asection *sectp,
8533 struct virtual_dwo_sections *sections)
8534{
8535 const struct dwop_section_names *names = &dwop_section_names;
8536
8537 if (section_is_p (sectp->name, &names->abbrev_dwo))
8538 {
8539 /* There can be only one. */
8540 if (sections->abbrev.asection != NULL)
8541 return 0;
8542 sections->abbrev.asection = sectp;
8543 sections->abbrev.size = bfd_get_section_size (sectp);
8544 }
8545 else if (section_is_p (sectp->name, &names->info_dwo)
8546 || section_is_p (sectp->name, &names->types_dwo))
8547 {
8548 /* There can be only one. */
8549 if (sections->info_or_types.asection != NULL)
8550 return 0;
8551 sections->info_or_types.asection = sectp;
8552 sections->info_or_types.size = bfd_get_section_size (sectp);
8553 }
8554 else if (section_is_p (sectp->name, &names->line_dwo))
8555 {
8556 /* There can be only one. */
8557 if (sections->line.asection != NULL)
8558 return 0;
8559 sections->line.asection = sectp;
8560 sections->line.size = bfd_get_section_size (sectp);
8561 }
8562 else if (section_is_p (sectp->name, &names->loc_dwo))
8563 {
8564 /* There can be only one. */
8565 if (sections->loc.asection != NULL)
8566 return 0;
8567 sections->loc.asection = sectp;
8568 sections->loc.size = bfd_get_section_size (sectp);
8569 }
8570 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8571 {
8572 /* There can be only one. */
8573 if (sections->macinfo.asection != NULL)
8574 return 0;
8575 sections->macinfo.asection = sectp;
8576 sections->macinfo.size = bfd_get_section_size (sectp);
8577 }
8578 else if (section_is_p (sectp->name, &names->macro_dwo))
8579 {
8580 /* There can be only one. */
8581 if (sections->macro.asection != NULL)
8582 return 0;
8583 sections->macro.asection = sectp;
8584 sections->macro.size = bfd_get_section_size (sectp);
8585 }
8586 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8587 {
8588 /* There can be only one. */
8589 if (sections->str_offsets.asection != NULL)
8590 return 0;
8591 sections->str_offsets.asection = sectp;
8592 sections->str_offsets.size = bfd_get_section_size (sectp);
8593 }
8594 else
8595 {
8596 /* No other kind of section is valid. */
8597 return 0;
8598 }
8599
8600 return 1;
8601}
8602
8603/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8604 HTAB is the hash table from the DWP file.
8605 SECTION_INDEX is the index of the DWO in HTAB. */
8606
8607static struct dwo_unit *
8608create_dwo_in_dwp (struct dwp_file *dwp_file,
8609 const struct dwp_hash_table *htab,
8610 uint32_t section_index,
8611 ULONGEST signature, int is_debug_types)
8612{
8613 struct objfile *objfile = dwarf2_per_objfile->objfile;
8614 bfd *dbfd = dwp_file->dbfd;
8615 const char *kind = is_debug_types ? "TU" : "CU";
8616 struct dwo_file *dwo_file;
8617 struct dwo_unit *dwo_unit;
8618 struct virtual_dwo_sections sections;
8619 void **dwo_file_slot;
8620 char *virtual_dwo_name;
8621 struct dwarf2_section_info *cutu;
8622 struct cleanup *cleanups;
8623 int i;
8624
8625 if (dwarf2_read_debug)
8626 {
8627 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8628 kind,
8629 section_index, phex (signature, sizeof (signature)),
8630 dwp_file->name);
8631 }
8632
8633 /* Fetch the sections of this DWO.
8634 Put a limit on the number of sections we look for so that bad data
8635 doesn't cause us to loop forever. */
8636
8637#define MAX_NR_DWO_SECTIONS \
8638 (1 /* .debug_info or .debug_types */ \
8639 + 1 /* .debug_abbrev */ \
8640 + 1 /* .debug_line */ \
8641 + 1 /* .debug_loc */ \
8642 + 1 /* .debug_str_offsets */ \
8643 + 1 /* .debug_macro */ \
8644 + 1 /* .debug_macinfo */ \
8645 + 1 /* trailing zero */)
8646
8647 memset (&sections, 0, sizeof (sections));
8648 cleanups = make_cleanup (null_cleanup, 0);
8649
8650 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8651 {
8652 asection *sectp;
8653 uint32_t section_nr =
8654 read_4_bytes (dbfd,
8655 htab->section_pool
8656 + (section_index + i) * sizeof (uint32_t));
8657
8658 if (section_nr == 0)
8659 break;
8660 if (section_nr >= dwp_file->num_sections)
8661 {
8662 error (_("Dwarf Error: bad DWP hash table, section number too large"
8663 " [in module %s]"),
8664 dwp_file->name);
8665 }
8666
8667 sectp = dwp_file->elf_sections[section_nr];
8668 if (! locate_virtual_dwo_sections (sectp, &sections))
8669 {
8670 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8671 " [in module %s]"),
8672 dwp_file->name);
8673 }
8674 }
8675
8676 if (i < 2
8677 || sections.info_or_types.asection == NULL
8678 || sections.abbrev.asection == NULL)
8679 {
8680 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8681 " [in module %s]"),
8682 dwp_file->name);
8683 }
8684 if (i == MAX_NR_DWO_SECTIONS)
8685 {
8686 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8687 " [in module %s]"),
8688 dwp_file->name);
8689 }
8690
8691 /* It's easier for the rest of the code if we fake a struct dwo_file and
8692 have dwo_unit "live" in that. At least for now.
8693
8694 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8695 However, for each CU + set of TUs that came from the same original DWO
8696 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8697 (fewer struct dwo_file objects to allocated). Remember that for really
8698 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8699
2792b94d
PM
8700 virtual_dwo_name =
8701 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8702 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8703 sections.line.asection ? sections.line.asection->id : 0,
8704 sections.loc.asection ? sections.loc.asection->id : 0,
8705 (sections.str_offsets.asection
8706 ? sections.str_offsets.asection->id
8707 : 0));
80626a55
DE
8708 make_cleanup (xfree, virtual_dwo_name);
8709 /* Can we use an existing virtual DWO file? */
8710 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8711 /* Create one if necessary. */
8712 if (*dwo_file_slot == NULL)
8713 {
8714 if (dwarf2_read_debug)
8715 {
8716 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8717 virtual_dwo_name);
8718 }
8719 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8720 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8721 virtual_dwo_name,
8722 strlen (virtual_dwo_name));
8723 dwo_file->sections.abbrev = sections.abbrev;
8724 dwo_file->sections.line = sections.line;
8725 dwo_file->sections.loc = sections.loc;
8726 dwo_file->sections.macinfo = sections.macinfo;
8727 dwo_file->sections.macro = sections.macro;
8728 dwo_file->sections.str_offsets = sections.str_offsets;
8729 /* The "str" section is global to the entire DWP file. */
8730 dwo_file->sections.str = dwp_file->sections.str;
8731 /* The info or types section is assigned later to dwo_unit,
8732 there's no need to record it in dwo_file.
8733 Also, we can't simply record type sections in dwo_file because
8734 we record a pointer into the vector in dwo_unit. As we collect more
8735 types we'll grow the vector and eventually have to reallocate space
8736 for it, invalidating all the pointers into the current copy. */
8737 *dwo_file_slot = dwo_file;
8738 }
8739 else
8740 {
8741 if (dwarf2_read_debug)
8742 {
8743 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8744 virtual_dwo_name);
8745 }
8746 dwo_file = *dwo_file_slot;
8747 }
8748 do_cleanups (cleanups);
8749
8750 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8751 dwo_unit->dwo_file = dwo_file;
8752 dwo_unit->signature = signature;
8753 dwo_unit->info_or_types_section =
8754 obstack_alloc (&objfile->objfile_obstack,
8755 sizeof (struct dwarf2_section_info));
8756 *dwo_unit->info_or_types_section = sections.info_or_types;
8757 /* offset, length, type_offset_in_tu are set later. */
8758
8759 return dwo_unit;
8760}
8761
8762/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8763
8764static struct dwo_unit *
8765lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8766 const struct dwp_hash_table *htab,
8767 ULONGEST signature, int is_debug_types)
8768{
8769 bfd *dbfd = dwp_file->dbfd;
8770 uint32_t mask = htab->nr_slots - 1;
8771 uint32_t hash = signature & mask;
8772 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8773 unsigned int i;
8774 void **slot;
8775 struct dwo_unit find_dwo_cu, *dwo_cu;
8776
8777 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8778 find_dwo_cu.signature = signature;
8779 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8780
8781 if (*slot != NULL)
8782 return *slot;
8783
8784 /* Use a for loop so that we don't loop forever on bad debug info. */
8785 for (i = 0; i < htab->nr_slots; ++i)
8786 {
8787 ULONGEST signature_in_table;
8788
8789 signature_in_table =
8790 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8791 if (signature_in_table == signature)
8792 {
8793 uint32_t section_index =
8794 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8795
8796 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8797 signature, is_debug_types);
8798 return *slot;
8799 }
8800 if (signature_in_table == 0)
8801 return NULL;
8802 hash = (hash + hash2) & mask;
8803 }
8804
8805 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8806 " [in module %s]"),
8807 dwp_file->name);
8808}
8809
8810/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8811 Open the file specified by FILE_NAME and hand it off to BFD for
8812 preliminary analysis. Return a newly initialized bfd *, which
8813 includes a canonicalized copy of FILE_NAME.
80626a55 8814 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8815 In case of trouble, return NULL.
8816 NOTE: This function is derived from symfile_bfd_open. */
8817
8818static bfd *
80626a55 8819try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8820{
8821 bfd *sym_bfd;
80626a55 8822 int desc, flags;
3019eac3 8823 char *absolute_name;
3019eac3 8824
80626a55
DE
8825 flags = OPF_TRY_CWD_FIRST;
8826 if (is_dwp)
8827 flags |= OPF_SEARCH_IN_PATH;
8828 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8829 O_RDONLY | O_BINARY, &absolute_name);
8830 if (desc < 0)
8831 return NULL;
8832
bb397797 8833 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8834 if (!sym_bfd)
8835 {
3019eac3
DE
8836 xfree (absolute_name);
8837 return NULL;
8838 }
a4453b7e 8839 xfree (absolute_name);
3019eac3
DE
8840 bfd_set_cacheable (sym_bfd, 1);
8841
8842 if (!bfd_check_format (sym_bfd, bfd_object))
8843 {
cbb099e8 8844 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8845 return NULL;
8846 }
8847
3019eac3
DE
8848 return sym_bfd;
8849}
8850
80626a55 8851/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8852 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8853 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8854 The result is the bfd handle of the file.
8855 If there is a problem finding or opening the file, return NULL.
8856 Upon success, the canonicalized path of the file is stored in the bfd,
8857 same as symfile_bfd_open. */
8858
8859static bfd *
80626a55 8860open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8861{
8862 bfd *abfd;
3019eac3 8863
80626a55
DE
8864 if (IS_ABSOLUTE_PATH (file_name))
8865 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8866
8867 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8868
8869 if (comp_dir != NULL)
8870 {
80626a55 8871 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8872
8873 /* NOTE: If comp_dir is a relative path, this will also try the
8874 search path, which seems useful. */
80626a55 8875 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8876 xfree (path_to_try);
8877 if (abfd != NULL)
8878 return abfd;
8879 }
8880
8881 /* That didn't work, try debug-file-directory, which, despite its name,
8882 is a list of paths. */
8883
8884 if (*debug_file_directory == '\0')
8885 return NULL;
8886
80626a55 8887 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8888}
8889
80626a55
DE
8890/* This function is mapped across the sections and remembers the offset and
8891 size of each of the DWO debugging sections we are interested in. */
8892
8893static void
8894dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8895{
8896 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8897 const struct dwop_section_names *names = &dwop_section_names;
8898
8899 if (section_is_p (sectp->name, &names->abbrev_dwo))
8900 {
8901 dwo_sections->abbrev.asection = sectp;
8902 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8903 }
8904 else if (section_is_p (sectp->name, &names->info_dwo))
8905 {
8906 dwo_sections->info.asection = sectp;
8907 dwo_sections->info.size = bfd_get_section_size (sectp);
8908 }
8909 else if (section_is_p (sectp->name, &names->line_dwo))
8910 {
8911 dwo_sections->line.asection = sectp;
8912 dwo_sections->line.size = bfd_get_section_size (sectp);
8913 }
8914 else if (section_is_p (sectp->name, &names->loc_dwo))
8915 {
8916 dwo_sections->loc.asection = sectp;
8917 dwo_sections->loc.size = bfd_get_section_size (sectp);
8918 }
8919 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8920 {
8921 dwo_sections->macinfo.asection = sectp;
8922 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8923 }
8924 else if (section_is_p (sectp->name, &names->macro_dwo))
8925 {
8926 dwo_sections->macro.asection = sectp;
8927 dwo_sections->macro.size = bfd_get_section_size (sectp);
8928 }
8929 else if (section_is_p (sectp->name, &names->str_dwo))
8930 {
8931 dwo_sections->str.asection = sectp;
8932 dwo_sections->str.size = bfd_get_section_size (sectp);
8933 }
8934 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8935 {
8936 dwo_sections->str_offsets.asection = sectp;
8937 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8938 }
8939 else if (section_is_p (sectp->name, &names->types_dwo))
8940 {
8941 struct dwarf2_section_info type_section;
8942
8943 memset (&type_section, 0, sizeof (type_section));
8944 type_section.asection = sectp;
8945 type_section.size = bfd_get_section_size (sectp);
8946 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8947 &type_section);
8948 }
8949}
8950
8951/* Initialize the use of the DWO file specified by DWO_NAME.
8952 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8953
8954static struct dwo_file *
80626a55 8955open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8956{
8957 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8958 struct dwo_file *dwo_file;
8959 bfd *dbfd;
3019eac3
DE
8960 struct cleanup *cleanups;
8961
80626a55
DE
8962 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8963 if (dbfd == NULL)
8964 {
8965 if (dwarf2_read_debug)
8966 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8967 return NULL;
8968 }
8969 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8970 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8971 dwo_name, strlen (dwo_name));
8972 dwo_file->dbfd = dbfd;
3019eac3
DE
8973
8974 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8975
80626a55 8976 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8977
80626a55 8978 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8979
8980 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8981 dwo_file->sections.types);
8982
8983 discard_cleanups (cleanups);
8984
80626a55
DE
8985 if (dwarf2_read_debug)
8986 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8987
3019eac3
DE
8988 return dwo_file;
8989}
8990
80626a55
DE
8991/* This function is mapped across the sections and remembers the offset and
8992 size of each of the DWP debugging sections we are interested in. */
3019eac3 8993
80626a55
DE
8994static void
8995dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8996{
80626a55
DE
8997 struct dwp_file *dwp_file = dwp_file_ptr;
8998 const struct dwop_section_names *names = &dwop_section_names;
8999 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9000
80626a55
DE
9001 /* Record the ELF section number for later lookup: this is what the
9002 .debug_cu_index,.debug_tu_index tables use. */
9003 gdb_assert (elf_section_nr < dwp_file->num_sections);
9004 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9005
80626a55
DE
9006 /* Look for specific sections that we need. */
9007 if (section_is_p (sectp->name, &names->str_dwo))
9008 {
9009 dwp_file->sections.str.asection = sectp;
9010 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9011 }
9012 else if (section_is_p (sectp->name, &names->cu_index))
9013 {
9014 dwp_file->sections.cu_index.asection = sectp;
9015 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9016 }
9017 else if (section_is_p (sectp->name, &names->tu_index))
9018 {
9019 dwp_file->sections.tu_index.asection = sectp;
9020 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9021 }
9022}
3019eac3 9023
80626a55 9024/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9025
80626a55
DE
9026static hashval_t
9027hash_dwp_loaded_cutus (const void *item)
9028{
9029 const struct dwo_unit *dwo_unit = item;
3019eac3 9030
80626a55
DE
9031 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9032 return dwo_unit->signature;
3019eac3
DE
9033}
9034
80626a55 9035/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9036
80626a55
DE
9037static int
9038eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9039{
80626a55
DE
9040 const struct dwo_unit *dua = a;
9041 const struct dwo_unit *dub = b;
3019eac3 9042
80626a55
DE
9043 return dua->signature == dub->signature;
9044}
3019eac3 9045
80626a55 9046/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9047
80626a55
DE
9048static htab_t
9049allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9050{
9051 return htab_create_alloc_ex (3,
9052 hash_dwp_loaded_cutus,
9053 eq_dwp_loaded_cutus,
9054 NULL,
9055 &objfile->objfile_obstack,
9056 hashtab_obstack_allocate,
9057 dummy_obstack_deallocate);
9058}
3019eac3 9059
80626a55
DE
9060/* Initialize the use of the DWP file for the current objfile.
9061 By convention the name of the DWP file is ${objfile}.dwp.
9062 The result is NULL if it can't be found. */
a766d390 9063
80626a55
DE
9064static struct dwp_file *
9065open_and_init_dwp_file (const char *comp_dir)
9066{
9067 struct objfile *objfile = dwarf2_per_objfile->objfile;
9068 struct dwp_file *dwp_file;
9069 char *dwp_name;
9070 bfd *dbfd;
9071 struct cleanup *cleanups;
9072
2792b94d 9073 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9074 cleanups = make_cleanup (xfree, dwp_name);
9075
9076 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9077 if (dbfd == NULL)
9078 {
9079 if (dwarf2_read_debug)
9080 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9081 do_cleanups (cleanups);
9082 return NULL;
3019eac3 9083 }
80626a55
DE
9084 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9085 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9086 dwp_name, strlen (dwp_name));
9087 dwp_file->dbfd = dbfd;
9088 do_cleanups (cleanups);
c906108c 9089
80626a55 9090 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9091
80626a55
DE
9092 /* +1: section 0 is unused */
9093 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9094 dwp_file->elf_sections =
9095 OBSTACK_CALLOC (&objfile->objfile_obstack,
9096 dwp_file->num_sections, asection *);
9097
9098 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9099
9100 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9101
9102 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9103
9104 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9105
9106 discard_cleanups (cleanups);
9107
9108 if (dwarf2_read_debug)
9109 {
9110 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9111 fprintf_unfiltered (gdb_stdlog,
9112 " %u CUs, %u TUs\n",
9113 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9114 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9115 }
9116
9117 return dwp_file;
3019eac3 9118}
c906108c 9119
80626a55
DE
9120/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9121 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9122 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9123 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9124 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9125
9126 This is called, for example, when wanting to read a variable with a
9127 complex location. Therefore we don't want to do file i/o for every call.
9128 Therefore we don't want to look for a DWO file on every call.
9129 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9130 then we check if we've already seen DWO_NAME, and only THEN do we check
9131 for a DWO file.
9132
1c658ad5 9133 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9134 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9135
3019eac3 9136static struct dwo_unit *
80626a55
DE
9137lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9138 const char *dwo_name, const char *comp_dir,
9139 ULONGEST signature, int is_debug_types)
3019eac3
DE
9140{
9141 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9142 const char *kind = is_debug_types ? "TU" : "CU";
9143 void **dwo_file_slot;
3019eac3 9144 struct dwo_file *dwo_file;
80626a55 9145 struct dwp_file *dwp_file;
cb1df416 9146
80626a55 9147 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9148
80626a55
DE
9149 if (! dwarf2_per_objfile->dwp_checked)
9150 {
9151 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9152 dwarf2_per_objfile->dwp_checked = 1;
9153 }
9154 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9155
80626a55 9156 if (dwp_file != NULL)
cf2c3c16 9157 {
80626a55
DE
9158 const struct dwp_hash_table *dwp_htab =
9159 is_debug_types ? dwp_file->tus : dwp_file->cus;
9160
9161 if (dwp_htab != NULL)
9162 {
9163 struct dwo_unit *dwo_cutu =
9164 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9165
9166 if (dwo_cutu != NULL)
9167 {
9168 if (dwarf2_read_debug)
9169 {
9170 fprintf_unfiltered (gdb_stdlog,
9171 "Virtual DWO %s %s found: @%s\n",
9172 kind, hex_string (signature),
9173 host_address_to_string (dwo_cutu));
9174 }
9175 return dwo_cutu;
9176 }
9177 }
9178 }
9179
9180 /* Have we already seen DWO_NAME? */
9181
9182 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9183 if (*dwo_file_slot == NULL)
9184 {
9185 /* Read in the file and build a table of the DWOs it contains. */
9186 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9187 }
9188 /* NOTE: This will be NULL if unable to open the file. */
9189 dwo_file = *dwo_file_slot;
9190
9191 if (dwo_file != NULL)
9192 {
9193 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9194
9195 if (htab != NULL)
9196 {
9197 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9198
80626a55
DE
9199 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9200 find_dwo_cutu.signature = signature;
9201 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9202
80626a55
DE
9203 if (dwo_cutu != NULL)
9204 {
9205 if (dwarf2_read_debug)
9206 {
9207 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9208 kind, dwo_name, hex_string (signature),
9209 host_address_to_string (dwo_cutu));
9210 }
9211 return dwo_cutu;
9212 }
9213 }
2e276125 9214 }
9cdd5dbd 9215
80626a55
DE
9216 /* We didn't find it. This could mean a dwo_id mismatch, or
9217 someone deleted the DWO/DWP file, or the search path isn't set up
9218 correctly to find the file. */
9219
9220 if (dwarf2_read_debug)
9221 {
9222 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9223 kind, dwo_name, hex_string (signature));
9224 }
3019eac3
DE
9225
9226 complaint (&symfile_complaints,
80626a55 9227 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9228 " [in module %s]"),
80626a55 9229 this_unit->offset.sect_off, objfile->name);
3019eac3 9230 return NULL;
5fb290d7
DJ
9231}
9232
80626a55
DE
9233/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9234 See lookup_dwo_cutu_unit for details. */
9235
9236static struct dwo_unit *
9237lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9238 const char *dwo_name, const char *comp_dir,
9239 ULONGEST signature)
9240{
9241 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9242}
9243
9244/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9245 See lookup_dwo_cutu_unit for details. */
9246
9247static struct dwo_unit *
9248lookup_dwo_type_unit (struct signatured_type *this_tu,
9249 const char *dwo_name, const char *comp_dir)
9250{
9251 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9252}
9253
3019eac3
DE
9254/* Free all resources associated with DWO_FILE.
9255 Close the DWO file and munmap the sections.
9256 All memory should be on the objfile obstack. */
348e048f
DE
9257
9258static void
3019eac3 9259free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9260{
3019eac3
DE
9261 int ix;
9262 struct dwarf2_section_info *section;
348e048f 9263
80626a55
DE
9264 gdb_assert (dwo_file->dbfd != objfile->obfd);
9265 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9266
3019eac3
DE
9267 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9268}
348e048f 9269
3019eac3 9270/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9271
3019eac3
DE
9272static void
9273free_dwo_file_cleanup (void *arg)
9274{
9275 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9276 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9277
3019eac3
DE
9278 free_dwo_file (dwo_file, objfile);
9279}
348e048f 9280
3019eac3 9281/* Traversal function for free_dwo_files. */
2ab95328 9282
3019eac3
DE
9283static int
9284free_dwo_file_from_slot (void **slot, void *info)
9285{
9286 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9287 struct objfile *objfile = (struct objfile *) info;
348e048f 9288
3019eac3 9289 free_dwo_file (dwo_file, objfile);
348e048f 9290
3019eac3
DE
9291 return 1;
9292}
348e048f 9293
3019eac3 9294/* Free all resources associated with DWO_FILES. */
348e048f 9295
3019eac3
DE
9296static void
9297free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9298{
9299 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9300}
3019eac3
DE
9301\f
9302/* Read in various DIEs. */
348e048f 9303
d389af10
JK
9304/* qsort helper for inherit_abstract_dies. */
9305
9306static int
9307unsigned_int_compar (const void *ap, const void *bp)
9308{
9309 unsigned int a = *(unsigned int *) ap;
9310 unsigned int b = *(unsigned int *) bp;
9311
9312 return (a > b) - (b > a);
9313}
9314
9315/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9316 Inherit only the children of the DW_AT_abstract_origin DIE not being
9317 already referenced by DW_AT_abstract_origin from the children of the
9318 current DIE. */
d389af10
JK
9319
9320static void
9321inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9322{
9323 struct die_info *child_die;
9324 unsigned die_children_count;
9325 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9326 sect_offset *offsets;
9327 sect_offset *offsets_end, *offsetp;
d389af10
JK
9328 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9329 struct die_info *origin_die;
9330 /* Iterator of the ORIGIN_DIE children. */
9331 struct die_info *origin_child_die;
9332 struct cleanup *cleanups;
9333 struct attribute *attr;
cd02d79d
PA
9334 struct dwarf2_cu *origin_cu;
9335 struct pending **origin_previous_list_in_scope;
d389af10
JK
9336
9337 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9338 if (!attr)
9339 return;
9340
cd02d79d
PA
9341 /* Note that following die references may follow to a die in a
9342 different cu. */
9343
9344 origin_cu = cu;
9345 origin_die = follow_die_ref (die, attr, &origin_cu);
9346
9347 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9348 symbols in. */
9349 origin_previous_list_in_scope = origin_cu->list_in_scope;
9350 origin_cu->list_in_scope = cu->list_in_scope;
9351
edb3359d
DJ
9352 if (die->tag != origin_die->tag
9353 && !(die->tag == DW_TAG_inlined_subroutine
9354 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9355 complaint (&symfile_complaints,
9356 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9357 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9358
9359 child_die = die->child;
9360 die_children_count = 0;
9361 while (child_die && child_die->tag)
9362 {
9363 child_die = sibling_die (child_die);
9364 die_children_count++;
9365 }
9366 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9367 cleanups = make_cleanup (xfree, offsets);
9368
9369 offsets_end = offsets;
9370 child_die = die->child;
9371 while (child_die && child_die->tag)
9372 {
c38f313d
DJ
9373 /* For each CHILD_DIE, find the corresponding child of
9374 ORIGIN_DIE. If there is more than one layer of
9375 DW_AT_abstract_origin, follow them all; there shouldn't be,
9376 but GCC versions at least through 4.4 generate this (GCC PR
9377 40573). */
9378 struct die_info *child_origin_die = child_die;
cd02d79d 9379 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9380
c38f313d
DJ
9381 while (1)
9382 {
cd02d79d
PA
9383 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9384 child_origin_cu);
c38f313d
DJ
9385 if (attr == NULL)
9386 break;
cd02d79d
PA
9387 child_origin_die = follow_die_ref (child_origin_die, attr,
9388 &child_origin_cu);
c38f313d
DJ
9389 }
9390
d389af10
JK
9391 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9392 counterpart may exist. */
c38f313d 9393 if (child_origin_die != child_die)
d389af10 9394 {
edb3359d
DJ
9395 if (child_die->tag != child_origin_die->tag
9396 && !(child_die->tag == DW_TAG_inlined_subroutine
9397 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9398 complaint (&symfile_complaints,
9399 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9400 "different tags"), child_die->offset.sect_off,
9401 child_origin_die->offset.sect_off);
c38f313d
DJ
9402 if (child_origin_die->parent != origin_die)
9403 complaint (&symfile_complaints,
9404 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9405 "different parents"), child_die->offset.sect_off,
9406 child_origin_die->offset.sect_off);
c38f313d
DJ
9407 else
9408 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9409 }
9410 child_die = sibling_die (child_die);
9411 }
9412 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9413 unsigned_int_compar);
9414 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9415 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9416 complaint (&symfile_complaints,
9417 _("Multiple children of DIE 0x%x refer "
9418 "to DIE 0x%x as their abstract origin"),
b64f50a1 9419 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9420
9421 offsetp = offsets;
9422 origin_child_die = origin_die->child;
9423 while (origin_child_die && origin_child_die->tag)
9424 {
9425 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9426 while (offsetp < offsets_end
9427 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9428 offsetp++;
b64f50a1
JK
9429 if (offsetp >= offsets_end
9430 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9431 {
9432 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9433 process_die (origin_child_die, origin_cu);
d389af10
JK
9434 }
9435 origin_child_die = sibling_die (origin_child_die);
9436 }
cd02d79d 9437 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9438
9439 do_cleanups (cleanups);
9440}
9441
c906108c 9442static void
e7c27a73 9443read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9444{
e7c27a73 9445 struct objfile *objfile = cu->objfile;
52f0bd74 9446 struct context_stack *new;
c906108c
SS
9447 CORE_ADDR lowpc;
9448 CORE_ADDR highpc;
9449 struct die_info *child_die;
edb3359d 9450 struct attribute *attr, *call_line, *call_file;
c906108c 9451 char *name;
e142c38c 9452 CORE_ADDR baseaddr;
801e3a5b 9453 struct block *block;
edb3359d 9454 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9455 VEC (symbolp) *template_args = NULL;
9456 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9457
9458 if (inlined_func)
9459 {
9460 /* If we do not have call site information, we can't show the
9461 caller of this inlined function. That's too confusing, so
9462 only use the scope for local variables. */
9463 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9464 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9465 if (call_line == NULL || call_file == NULL)
9466 {
9467 read_lexical_block_scope (die, cu);
9468 return;
9469 }
9470 }
c906108c 9471
e142c38c
DJ
9472 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9473
94af9270 9474 name = dwarf2_name (die, cu);
c906108c 9475
e8d05480
JB
9476 /* Ignore functions with missing or empty names. These are actually
9477 illegal according to the DWARF standard. */
9478 if (name == NULL)
9479 {
9480 complaint (&symfile_complaints,
b64f50a1
JK
9481 _("missing name for subprogram DIE at %d"),
9482 die->offset.sect_off);
e8d05480
JB
9483 return;
9484 }
9485
9486 /* Ignore functions with missing or invalid low and high pc attributes. */
9487 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9488 {
ae4d0c03
PM
9489 attr = dwarf2_attr (die, DW_AT_external, cu);
9490 if (!attr || !DW_UNSND (attr))
9491 complaint (&symfile_complaints,
3e43a32a
MS
9492 _("cannot get low and high bounds "
9493 "for subprogram DIE at %d"),
b64f50a1 9494 die->offset.sect_off);
e8d05480
JB
9495 return;
9496 }
c906108c
SS
9497
9498 lowpc += baseaddr;
9499 highpc += baseaddr;
9500
34eaf542
TT
9501 /* If we have any template arguments, then we must allocate a
9502 different sort of symbol. */
9503 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9504 {
9505 if (child_die->tag == DW_TAG_template_type_param
9506 || child_die->tag == DW_TAG_template_value_param)
9507 {
9508 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9509 struct template_symbol);
9510 templ_func->base.is_cplus_template_function = 1;
9511 break;
9512 }
9513 }
9514
c906108c 9515 new = push_context (0, lowpc);
34eaf542
TT
9516 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9517 (struct symbol *) templ_func);
4c2df51b 9518
4cecd739
DJ
9519 /* If there is a location expression for DW_AT_frame_base, record
9520 it. */
e142c38c 9521 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9522 if (attr)
c034e007
AC
9523 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9524 expression is being recorded directly in the function's symbol
9525 and not in a separate frame-base object. I guess this hack is
9526 to avoid adding some sort of frame-base adjunct/annex to the
9527 function's symbol :-(. The problem with doing this is that it
9528 results in a function symbol with a location expression that
9529 has nothing to do with the location of the function, ouch! The
9530 relationship should be: a function's symbol has-a frame base; a
9531 frame-base has-a location expression. */
e7c27a73 9532 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9533
e142c38c 9534 cu->list_in_scope = &local_symbols;
c906108c 9535
639d11d3 9536 if (die->child != NULL)
c906108c 9537 {
639d11d3 9538 child_die = die->child;
c906108c
SS
9539 while (child_die && child_die->tag)
9540 {
34eaf542
TT
9541 if (child_die->tag == DW_TAG_template_type_param
9542 || child_die->tag == DW_TAG_template_value_param)
9543 {
9544 struct symbol *arg = new_symbol (child_die, NULL, cu);
9545
f1078f66
DJ
9546 if (arg != NULL)
9547 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9548 }
9549 else
9550 process_die (child_die, cu);
c906108c
SS
9551 child_die = sibling_die (child_die);
9552 }
9553 }
9554
d389af10
JK
9555 inherit_abstract_dies (die, cu);
9556
4a811a97
UW
9557 /* If we have a DW_AT_specification, we might need to import using
9558 directives from the context of the specification DIE. See the
9559 comment in determine_prefix. */
9560 if (cu->language == language_cplus
9561 && dwarf2_attr (die, DW_AT_specification, cu))
9562 {
9563 struct dwarf2_cu *spec_cu = cu;
9564 struct die_info *spec_die = die_specification (die, &spec_cu);
9565
9566 while (spec_die)
9567 {
9568 child_die = spec_die->child;
9569 while (child_die && child_die->tag)
9570 {
9571 if (child_die->tag == DW_TAG_imported_module)
9572 process_die (child_die, spec_cu);
9573 child_die = sibling_die (child_die);
9574 }
9575
9576 /* In some cases, GCC generates specification DIEs that
9577 themselves contain DW_AT_specification attributes. */
9578 spec_die = die_specification (spec_die, &spec_cu);
9579 }
9580 }
9581
c906108c
SS
9582 new = pop_context ();
9583 /* Make a block for the local symbols within. */
801e3a5b
JB
9584 block = finish_block (new->name, &local_symbols, new->old_blocks,
9585 lowpc, highpc, objfile);
9586
df8a16a1 9587 /* For C++, set the block's scope. */
f55ee35c 9588 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9589 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9590 determine_prefix (die, cu),
df8a16a1
DJ
9591 processing_has_namespace_info);
9592
801e3a5b
JB
9593 /* If we have address ranges, record them. */
9594 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9595
34eaf542
TT
9596 /* Attach template arguments to function. */
9597 if (! VEC_empty (symbolp, template_args))
9598 {
9599 gdb_assert (templ_func != NULL);
9600
9601 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9602 templ_func->template_arguments
9603 = obstack_alloc (&objfile->objfile_obstack,
9604 (templ_func->n_template_arguments
9605 * sizeof (struct symbol *)));
9606 memcpy (templ_func->template_arguments,
9607 VEC_address (symbolp, template_args),
9608 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9609 VEC_free (symbolp, template_args);
9610 }
9611
208d8187
JB
9612 /* In C++, we can have functions nested inside functions (e.g., when
9613 a function declares a class that has methods). This means that
9614 when we finish processing a function scope, we may need to go
9615 back to building a containing block's symbol lists. */
9616 local_symbols = new->locals;
27aa8d6a 9617 using_directives = new->using_directives;
208d8187 9618
921e78cf
JB
9619 /* If we've finished processing a top-level function, subsequent
9620 symbols go in the file symbol list. */
9621 if (outermost_context_p ())
e142c38c 9622 cu->list_in_scope = &file_symbols;
c906108c
SS
9623}
9624
9625/* Process all the DIES contained within a lexical block scope. Start
9626 a new scope, process the dies, and then close the scope. */
9627
9628static void
e7c27a73 9629read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9630{
e7c27a73 9631 struct objfile *objfile = cu->objfile;
52f0bd74 9632 struct context_stack *new;
c906108c
SS
9633 CORE_ADDR lowpc, highpc;
9634 struct die_info *child_die;
e142c38c
DJ
9635 CORE_ADDR baseaddr;
9636
9637 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9638
9639 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9640 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9641 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9642 be nasty. Might be easier to properly extend generic blocks to
af34e669 9643 describe ranges. */
d85a05f0 9644 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9645 return;
9646 lowpc += baseaddr;
9647 highpc += baseaddr;
9648
9649 push_context (0, lowpc);
639d11d3 9650 if (die->child != NULL)
c906108c 9651 {
639d11d3 9652 child_die = die->child;
c906108c
SS
9653 while (child_die && child_die->tag)
9654 {
e7c27a73 9655 process_die (child_die, cu);
c906108c
SS
9656 child_die = sibling_die (child_die);
9657 }
9658 }
9659 new = pop_context ();
9660
8540c487 9661 if (local_symbols != NULL || using_directives != NULL)
c906108c 9662 {
801e3a5b
JB
9663 struct block *block
9664 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9665 highpc, objfile);
9666
9667 /* Note that recording ranges after traversing children, as we
9668 do here, means that recording a parent's ranges entails
9669 walking across all its children's ranges as they appear in
9670 the address map, which is quadratic behavior.
9671
9672 It would be nicer to record the parent's ranges before
9673 traversing its children, simply overriding whatever you find
9674 there. But since we don't even decide whether to create a
9675 block until after we've traversed its children, that's hard
9676 to do. */
9677 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9678 }
9679 local_symbols = new->locals;
27aa8d6a 9680 using_directives = new->using_directives;
c906108c
SS
9681}
9682
96408a79
SA
9683/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9684
9685static void
9686read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9687{
9688 struct objfile *objfile = cu->objfile;
9689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9690 CORE_ADDR pc, baseaddr;
9691 struct attribute *attr;
9692 struct call_site *call_site, call_site_local;
9693 void **slot;
9694 int nparams;
9695 struct die_info *child_die;
9696
9697 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9698
9699 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9700 if (!attr)
9701 {
9702 complaint (&symfile_complaints,
9703 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9704 "DIE 0x%x [in module %s]"),
b64f50a1 9705 die->offset.sect_off, objfile->name);
96408a79
SA
9706 return;
9707 }
9708 pc = DW_ADDR (attr) + baseaddr;
9709
9710 if (cu->call_site_htab == NULL)
9711 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9712 NULL, &objfile->objfile_obstack,
9713 hashtab_obstack_allocate, NULL);
9714 call_site_local.pc = pc;
9715 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9716 if (*slot != NULL)
9717 {
9718 complaint (&symfile_complaints,
9719 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9720 "DIE 0x%x [in module %s]"),
b64f50a1 9721 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9722 return;
9723 }
9724
9725 /* Count parameters at the caller. */
9726
9727 nparams = 0;
9728 for (child_die = die->child; child_die && child_die->tag;
9729 child_die = sibling_die (child_die))
9730 {
9731 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9732 {
9733 complaint (&symfile_complaints,
9734 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9735 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9736 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9737 continue;
9738 }
9739
9740 nparams++;
9741 }
9742
9743 call_site = obstack_alloc (&objfile->objfile_obstack,
9744 (sizeof (*call_site)
9745 + (sizeof (*call_site->parameter)
9746 * (nparams - 1))));
9747 *slot = call_site;
9748 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9749 call_site->pc = pc;
9750
9751 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9752 {
9753 struct die_info *func_die;
9754
9755 /* Skip also over DW_TAG_inlined_subroutine. */
9756 for (func_die = die->parent;
9757 func_die && func_die->tag != DW_TAG_subprogram
9758 && func_die->tag != DW_TAG_subroutine_type;
9759 func_die = func_die->parent);
9760
9761 /* DW_AT_GNU_all_call_sites is a superset
9762 of DW_AT_GNU_all_tail_call_sites. */
9763 if (func_die
9764 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9765 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9766 {
9767 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9768 not complete. But keep CALL_SITE for look ups via call_site_htab,
9769 both the initial caller containing the real return address PC and
9770 the final callee containing the current PC of a chain of tail
9771 calls do not need to have the tail call list complete. But any
9772 function candidate for a virtual tail call frame searched via
9773 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9774 determined unambiguously. */
9775 }
9776 else
9777 {
9778 struct type *func_type = NULL;
9779
9780 if (func_die)
9781 func_type = get_die_type (func_die, cu);
9782 if (func_type != NULL)
9783 {
9784 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9785
9786 /* Enlist this call site to the function. */
9787 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9788 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9789 }
9790 else
9791 complaint (&symfile_complaints,
9792 _("Cannot find function owning DW_TAG_GNU_call_site "
9793 "DIE 0x%x [in module %s]"),
b64f50a1 9794 die->offset.sect_off, objfile->name);
96408a79
SA
9795 }
9796 }
9797
9798 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9799 if (attr == NULL)
9800 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9801 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9802 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9803 /* Keep NULL DWARF_BLOCK. */;
9804 else if (attr_form_is_block (attr))
9805 {
9806 struct dwarf2_locexpr_baton *dlbaton;
9807
9808 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9809 dlbaton->data = DW_BLOCK (attr)->data;
9810 dlbaton->size = DW_BLOCK (attr)->size;
9811 dlbaton->per_cu = cu->per_cu;
9812
9813 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9814 }
9815 else if (is_ref_attr (attr))
9816 {
96408a79
SA
9817 struct dwarf2_cu *target_cu = cu;
9818 struct die_info *target_die;
9819
9820 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9821 gdb_assert (target_cu->objfile == objfile);
9822 if (die_is_declaration (target_die, target_cu))
9823 {
9824 const char *target_physname;
9825
9826 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9827 if (target_physname == NULL)
9828 complaint (&symfile_complaints,
9829 _("DW_AT_GNU_call_site_target target DIE has invalid "
9830 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9831 die->offset.sect_off, objfile->name);
96408a79
SA
9832 else
9833 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9834 }
9835 else
9836 {
9837 CORE_ADDR lowpc;
9838
9839 /* DW_AT_entry_pc should be preferred. */
9840 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9841 complaint (&symfile_complaints,
9842 _("DW_AT_GNU_call_site_target target DIE has invalid "
9843 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9844 die->offset.sect_off, objfile->name);
96408a79
SA
9845 else
9846 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9847 }
9848 }
9849 else
9850 complaint (&symfile_complaints,
9851 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9852 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9853 die->offset.sect_off, objfile->name);
96408a79
SA
9854
9855 call_site->per_cu = cu->per_cu;
9856
9857 for (child_die = die->child;
9858 child_die && child_die->tag;
9859 child_die = sibling_die (child_die))
9860 {
96408a79 9861 struct call_site_parameter *parameter;
1788b2d3 9862 struct attribute *loc, *origin;
96408a79
SA
9863
9864 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9865 {
9866 /* Already printed the complaint above. */
9867 continue;
9868 }
9869
9870 gdb_assert (call_site->parameter_count < nparams);
9871 parameter = &call_site->parameter[call_site->parameter_count];
9872
1788b2d3
JK
9873 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9874 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9875 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9876
24c5c679 9877 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9878 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9879 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9880 {
9881 sect_offset offset;
9882
9883 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9884 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9885 if (!offset_in_cu_p (&cu->header, offset))
9886 {
9887 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9888 binding can be done only inside one CU. Such referenced DIE
9889 therefore cannot be even moved to DW_TAG_partial_unit. */
9890 complaint (&symfile_complaints,
9891 _("DW_AT_abstract_origin offset is not in CU for "
9892 "DW_TAG_GNU_call_site child DIE 0x%x "
9893 "[in module %s]"),
9894 child_die->offset.sect_off, objfile->name);
9895 continue;
9896 }
1788b2d3
JK
9897 parameter->u.param_offset.cu_off = (offset.sect_off
9898 - cu->header.offset.sect_off);
9899 }
9900 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9901 {
9902 complaint (&symfile_complaints,
9903 _("No DW_FORM_block* DW_AT_location for "
9904 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9905 child_die->offset.sect_off, objfile->name);
96408a79
SA
9906 continue;
9907 }
24c5c679 9908 else
96408a79 9909 {
24c5c679
JK
9910 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9911 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9912 if (parameter->u.dwarf_reg != -1)
9913 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9914 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9915 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9916 &parameter->u.fb_offset))
9917 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9918 else
9919 {
9920 complaint (&symfile_complaints,
9921 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9922 "for DW_FORM_block* DW_AT_location is supported for "
9923 "DW_TAG_GNU_call_site child DIE 0x%x "
9924 "[in module %s]"),
9925 child_die->offset.sect_off, objfile->name);
9926 continue;
9927 }
96408a79
SA
9928 }
9929
9930 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9931 if (!attr_form_is_block (attr))
9932 {
9933 complaint (&symfile_complaints,
9934 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9935 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9936 child_die->offset.sect_off, objfile->name);
96408a79
SA
9937 continue;
9938 }
9939 parameter->value = DW_BLOCK (attr)->data;
9940 parameter->value_size = DW_BLOCK (attr)->size;
9941
9942 /* Parameters are not pre-cleared by memset above. */
9943 parameter->data_value = NULL;
9944 parameter->data_value_size = 0;
9945 call_site->parameter_count++;
9946
9947 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9948 if (attr)
9949 {
9950 if (!attr_form_is_block (attr))
9951 complaint (&symfile_complaints,
9952 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9953 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9954 child_die->offset.sect_off, objfile->name);
96408a79
SA
9955 else
9956 {
9957 parameter->data_value = DW_BLOCK (attr)->data;
9958 parameter->data_value_size = DW_BLOCK (attr)->size;
9959 }
9960 }
9961 }
9962}
9963
43039443 9964/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9965 Return 1 if the attributes are present and valid, otherwise, return 0.
9966 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9967
9968static int
9969dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9970 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9971 struct partial_symtab *ranges_pst)
43039443
JK
9972{
9973 struct objfile *objfile = cu->objfile;
9974 struct comp_unit_head *cu_header = &cu->header;
9975 bfd *obfd = objfile->obfd;
9976 unsigned int addr_size = cu_header->addr_size;
9977 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9978 /* Base address selection entry. */
9979 CORE_ADDR base;
9980 int found_base;
9981 unsigned int dummy;
9982 gdb_byte *buffer;
9983 CORE_ADDR marker;
9984 int low_set;
9985 CORE_ADDR low = 0;
9986 CORE_ADDR high = 0;
ff013f42 9987 CORE_ADDR baseaddr;
43039443 9988
d00adf39
DE
9989 found_base = cu->base_known;
9990 base = cu->base_address;
43039443 9991
be391dca 9992 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9993 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9994 {
9995 complaint (&symfile_complaints,
9996 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9997 offset);
9998 return 0;
9999 }
dce234bc 10000 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10001
10002 /* Read in the largest possible address. */
10003 marker = read_address (obfd, buffer, cu, &dummy);
10004 if ((marker & mask) == mask)
10005 {
10006 /* If we found the largest possible address, then
10007 read the base address. */
10008 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10009 buffer += 2 * addr_size;
10010 offset += 2 * addr_size;
10011 found_base = 1;
10012 }
10013
10014 low_set = 0;
10015
e7030f15 10016 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10017
43039443
JK
10018 while (1)
10019 {
10020 CORE_ADDR range_beginning, range_end;
10021
10022 range_beginning = read_address (obfd, buffer, cu, &dummy);
10023 buffer += addr_size;
10024 range_end = read_address (obfd, buffer, cu, &dummy);
10025 buffer += addr_size;
10026 offset += 2 * addr_size;
10027
10028 /* An end of list marker is a pair of zero addresses. */
10029 if (range_beginning == 0 && range_end == 0)
10030 /* Found the end of list entry. */
10031 break;
10032
10033 /* Each base address selection entry is a pair of 2 values.
10034 The first is the largest possible address, the second is
10035 the base address. Check for a base address here. */
10036 if ((range_beginning & mask) == mask)
10037 {
10038 /* If we found the largest possible address, then
10039 read the base address. */
10040 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10041 found_base = 1;
10042 continue;
10043 }
10044
10045 if (!found_base)
10046 {
10047 /* We have no valid base address for the ranges
10048 data. */
10049 complaint (&symfile_complaints,
10050 _("Invalid .debug_ranges data (no base address)"));
10051 return 0;
10052 }
10053
9277c30c
UW
10054 if (range_beginning > range_end)
10055 {
10056 /* Inverted range entries are invalid. */
10057 complaint (&symfile_complaints,
10058 _("Invalid .debug_ranges data (inverted range)"));
10059 return 0;
10060 }
10061
10062 /* Empty range entries have no effect. */
10063 if (range_beginning == range_end)
10064 continue;
10065
43039443
JK
10066 range_beginning += base;
10067 range_end += base;
10068
01093045
DE
10069 /* A not-uncommon case of bad debug info.
10070 Don't pollute the addrmap with bad data. */
10071 if (range_beginning + baseaddr == 0
10072 && !dwarf2_per_objfile->has_section_at_zero)
10073 {
10074 complaint (&symfile_complaints,
10075 _(".debug_ranges entry has start address of zero"
10076 " [in module %s]"), objfile->name);
10077 continue;
10078 }
10079
9277c30c 10080 if (ranges_pst != NULL)
ff013f42 10081 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10082 range_beginning + baseaddr,
10083 range_end - 1 + baseaddr,
ff013f42
JK
10084 ranges_pst);
10085
43039443
JK
10086 /* FIXME: This is recording everything as a low-high
10087 segment of consecutive addresses. We should have a
10088 data structure for discontiguous block ranges
10089 instead. */
10090 if (! low_set)
10091 {
10092 low = range_beginning;
10093 high = range_end;
10094 low_set = 1;
10095 }
10096 else
10097 {
10098 if (range_beginning < low)
10099 low = range_beginning;
10100 if (range_end > high)
10101 high = range_end;
10102 }
10103 }
10104
10105 if (! low_set)
10106 /* If the first entry is an end-of-list marker, the range
10107 describes an empty scope, i.e. no instructions. */
10108 return 0;
10109
10110 if (low_return)
10111 *low_return = low;
10112 if (high_return)
10113 *high_return = high;
10114 return 1;
10115}
10116
af34e669
DJ
10117/* Get low and high pc attributes from a die. Return 1 if the attributes
10118 are present and valid, otherwise, return 0. Return -1 if the range is
10119 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10120
c906108c 10121static int
af34e669 10122dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10123 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10124 struct partial_symtab *pst)
c906108c
SS
10125{
10126 struct attribute *attr;
91da1414 10127 struct attribute *attr_high;
af34e669
DJ
10128 CORE_ADDR low = 0;
10129 CORE_ADDR high = 0;
10130 int ret = 0;
c906108c 10131
91da1414
MW
10132 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10133 if (attr_high)
af34e669 10134 {
e142c38c 10135 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10136 if (attr)
91da1414
MW
10137 {
10138 low = DW_ADDR (attr);
3019eac3
DE
10139 if (attr_high->form == DW_FORM_addr
10140 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10141 high = DW_ADDR (attr_high);
10142 else
10143 high = low + DW_UNSND (attr_high);
10144 }
af34e669
DJ
10145 else
10146 /* Found high w/o low attribute. */
10147 return 0;
10148
10149 /* Found consecutive range of addresses. */
10150 ret = 1;
10151 }
c906108c 10152 else
af34e669 10153 {
e142c38c 10154 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10155 if (attr != NULL)
10156 {
ab435259
DE
10157 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10158 We take advantage of the fact that DW_AT_ranges does not appear
10159 in DW_TAG_compile_unit of DWO files. */
10160 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10161 unsigned int ranges_offset = (DW_UNSND (attr)
10162 + (need_ranges_base
10163 ? cu->ranges_base
10164 : 0));
2e3cf129 10165
af34e669 10166 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10167 .debug_ranges section. */
2e3cf129 10168 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10169 return 0;
43039443 10170 /* Found discontinuous range of addresses. */
af34e669
DJ
10171 ret = -1;
10172 }
10173 }
c906108c 10174
9373cf26
JK
10175 /* read_partial_die has also the strict LOW < HIGH requirement. */
10176 if (high <= low)
c906108c
SS
10177 return 0;
10178
10179 /* When using the GNU linker, .gnu.linkonce. sections are used to
10180 eliminate duplicate copies of functions and vtables and such.
10181 The linker will arbitrarily choose one and discard the others.
10182 The AT_*_pc values for such functions refer to local labels in
10183 these sections. If the section from that file was discarded, the
10184 labels are not in the output, so the relocs get a value of 0.
10185 If this is a discarded function, mark the pc bounds as invalid,
10186 so that GDB will ignore it. */
72dca2f5 10187 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10188 return 0;
10189
10190 *lowpc = low;
96408a79
SA
10191 if (highpc)
10192 *highpc = high;
af34e669 10193 return ret;
c906108c
SS
10194}
10195
b084d499
JB
10196/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10197 its low and high PC addresses. Do nothing if these addresses could not
10198 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10199 and HIGHPC to the high address if greater than HIGHPC. */
10200
10201static void
10202dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10203 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10204 struct dwarf2_cu *cu)
10205{
10206 CORE_ADDR low, high;
10207 struct die_info *child = die->child;
10208
d85a05f0 10209 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10210 {
10211 *lowpc = min (*lowpc, low);
10212 *highpc = max (*highpc, high);
10213 }
10214
10215 /* If the language does not allow nested subprograms (either inside
10216 subprograms or lexical blocks), we're done. */
10217 if (cu->language != language_ada)
10218 return;
6e70227d 10219
b084d499
JB
10220 /* Check all the children of the given DIE. If it contains nested
10221 subprograms, then check their pc bounds. Likewise, we need to
10222 check lexical blocks as well, as they may also contain subprogram
10223 definitions. */
10224 while (child && child->tag)
10225 {
10226 if (child->tag == DW_TAG_subprogram
10227 || child->tag == DW_TAG_lexical_block)
10228 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10229 child = sibling_die (child);
10230 }
10231}
10232
fae299cd
DC
10233/* Get the low and high pc's represented by the scope DIE, and store
10234 them in *LOWPC and *HIGHPC. If the correct values can't be
10235 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10236
10237static void
10238get_scope_pc_bounds (struct die_info *die,
10239 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10240 struct dwarf2_cu *cu)
10241{
10242 CORE_ADDR best_low = (CORE_ADDR) -1;
10243 CORE_ADDR best_high = (CORE_ADDR) 0;
10244 CORE_ADDR current_low, current_high;
10245
d85a05f0 10246 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10247 {
10248 best_low = current_low;
10249 best_high = current_high;
10250 }
10251 else
10252 {
10253 struct die_info *child = die->child;
10254
10255 while (child && child->tag)
10256 {
10257 switch (child->tag) {
10258 case DW_TAG_subprogram:
b084d499 10259 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10260 break;
10261 case DW_TAG_namespace:
f55ee35c 10262 case DW_TAG_module:
fae299cd
DC
10263 /* FIXME: carlton/2004-01-16: Should we do this for
10264 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10265 that current GCC's always emit the DIEs corresponding
10266 to definitions of methods of classes as children of a
10267 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10268 the DIEs giving the declarations, which could be
10269 anywhere). But I don't see any reason why the
10270 standards says that they have to be there. */
10271 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10272
10273 if (current_low != ((CORE_ADDR) -1))
10274 {
10275 best_low = min (best_low, current_low);
10276 best_high = max (best_high, current_high);
10277 }
10278 break;
10279 default:
0963b4bd 10280 /* Ignore. */
fae299cd
DC
10281 break;
10282 }
10283
10284 child = sibling_die (child);
10285 }
10286 }
10287
10288 *lowpc = best_low;
10289 *highpc = best_high;
10290}
10291
801e3a5b
JB
10292/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10293 in DIE. */
380bca97 10294
801e3a5b
JB
10295static void
10296dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10297 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10298{
bb5ed363 10299 struct objfile *objfile = cu->objfile;
801e3a5b 10300 struct attribute *attr;
91da1414 10301 struct attribute *attr_high;
801e3a5b 10302
91da1414
MW
10303 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10304 if (attr_high)
801e3a5b 10305 {
801e3a5b
JB
10306 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10307 if (attr)
10308 {
10309 CORE_ADDR low = DW_ADDR (attr);
91da1414 10310 CORE_ADDR high;
3019eac3
DE
10311 if (attr_high->form == DW_FORM_addr
10312 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10313 high = DW_ADDR (attr_high);
10314 else
10315 high = low + DW_UNSND (attr_high);
9a619af0 10316
801e3a5b
JB
10317 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10318 }
10319 }
10320
10321 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10322 if (attr)
10323 {
bb5ed363 10324 bfd *obfd = objfile->obfd;
ab435259
DE
10325 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10326 We take advantage of the fact that DW_AT_ranges does not appear
10327 in DW_TAG_compile_unit of DWO files. */
10328 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10329
10330 /* The value of the DW_AT_ranges attribute is the offset of the
10331 address range list in the .debug_ranges section. */
ab435259
DE
10332 unsigned long offset = (DW_UNSND (attr)
10333 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10334 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10335
10336 /* For some target architectures, but not others, the
10337 read_address function sign-extends the addresses it returns.
10338 To recognize base address selection entries, we need a
10339 mask. */
10340 unsigned int addr_size = cu->header.addr_size;
10341 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10342
10343 /* The base address, to which the next pair is relative. Note
10344 that this 'base' is a DWARF concept: most entries in a range
10345 list are relative, to reduce the number of relocs against the
10346 debugging information. This is separate from this function's
10347 'baseaddr' argument, which GDB uses to relocate debugging
10348 information from a shared library based on the address at
10349 which the library was loaded. */
d00adf39
DE
10350 CORE_ADDR base = cu->base_address;
10351 int base_known = cu->base_known;
801e3a5b 10352
be391dca 10353 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10354 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10355 {
10356 complaint (&symfile_complaints,
10357 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10358 offset);
10359 return;
10360 }
10361
10362 for (;;)
10363 {
10364 unsigned int bytes_read;
10365 CORE_ADDR start, end;
10366
10367 start = read_address (obfd, buffer, cu, &bytes_read);
10368 buffer += bytes_read;
10369 end = read_address (obfd, buffer, cu, &bytes_read);
10370 buffer += bytes_read;
10371
10372 /* Did we find the end of the range list? */
10373 if (start == 0 && end == 0)
10374 break;
10375
10376 /* Did we find a base address selection entry? */
10377 else if ((start & base_select_mask) == base_select_mask)
10378 {
10379 base = end;
10380 base_known = 1;
10381 }
10382
10383 /* We found an ordinary address range. */
10384 else
10385 {
10386 if (!base_known)
10387 {
10388 complaint (&symfile_complaints,
3e43a32a
MS
10389 _("Invalid .debug_ranges data "
10390 "(no base address)"));
801e3a5b
JB
10391 return;
10392 }
10393
9277c30c
UW
10394 if (start > end)
10395 {
10396 /* Inverted range entries are invalid. */
10397 complaint (&symfile_complaints,
10398 _("Invalid .debug_ranges data "
10399 "(inverted range)"));
10400 return;
10401 }
10402
10403 /* Empty range entries have no effect. */
10404 if (start == end)
10405 continue;
10406
01093045
DE
10407 start += base + baseaddr;
10408 end += base + baseaddr;
10409
10410 /* A not-uncommon case of bad debug info.
10411 Don't pollute the addrmap with bad data. */
10412 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10413 {
10414 complaint (&symfile_complaints,
10415 _(".debug_ranges entry has start address of zero"
10416 " [in module %s]"), objfile->name);
10417 continue;
10418 }
10419
10420 record_block_range (block, start, end - 1);
801e3a5b
JB
10421 }
10422 }
10423 }
10424}
10425
685b1105
JK
10426/* Check whether the producer field indicates either of GCC < 4.6, or the
10427 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10428
685b1105
JK
10429static void
10430check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10431{
10432 const char *cs;
10433 int major, minor, release;
10434
10435 if (cu->producer == NULL)
10436 {
10437 /* For unknown compilers expect their behavior is DWARF version
10438 compliant.
10439
10440 GCC started to support .debug_types sections by -gdwarf-4 since
10441 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10442 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10443 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10444 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10445 }
685b1105 10446 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10447 {
685b1105
JK
10448 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10449
ba919b58
TT
10450 cs = &cu->producer[strlen ("GNU ")];
10451 while (*cs && !isdigit (*cs))
10452 cs++;
10453 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10454 {
10455 /* Not recognized as GCC. */
10456 }
10457 else
1b80a9fa
JK
10458 {
10459 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10460 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10461 }
685b1105
JK
10462 }
10463 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10464 cu->producer_is_icc = 1;
10465 else
10466 {
10467 /* For other non-GCC compilers, expect their behavior is DWARF version
10468 compliant. */
60d5a603
JK
10469 }
10470
ba919b58 10471 cu->checked_producer = 1;
685b1105 10472}
ba919b58 10473
685b1105
JK
10474/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10475 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10476 during 4.6.0 experimental. */
10477
10478static int
10479producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10480{
10481 if (!cu->checked_producer)
10482 check_producer (cu);
10483
10484 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10485}
10486
10487/* Return the default accessibility type if it is not overriden by
10488 DW_AT_accessibility. */
10489
10490static enum dwarf_access_attribute
10491dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10492{
10493 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10494 {
10495 /* The default DWARF 2 accessibility for members is public, the default
10496 accessibility for inheritance is private. */
10497
10498 if (die->tag != DW_TAG_inheritance)
10499 return DW_ACCESS_public;
10500 else
10501 return DW_ACCESS_private;
10502 }
10503 else
10504 {
10505 /* DWARF 3+ defines the default accessibility a different way. The same
10506 rules apply now for DW_TAG_inheritance as for the members and it only
10507 depends on the container kind. */
10508
10509 if (die->parent->tag == DW_TAG_class_type)
10510 return DW_ACCESS_private;
10511 else
10512 return DW_ACCESS_public;
10513 }
10514}
10515
74ac6d43
TT
10516/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10517 offset. If the attribute was not found return 0, otherwise return
10518 1. If it was found but could not properly be handled, set *OFFSET
10519 to 0. */
10520
10521static int
10522handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10523 LONGEST *offset)
10524{
10525 struct attribute *attr;
10526
10527 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10528 if (attr != NULL)
10529 {
10530 *offset = 0;
10531
10532 /* Note that we do not check for a section offset first here.
10533 This is because DW_AT_data_member_location is new in DWARF 4,
10534 so if we see it, we can assume that a constant form is really
10535 a constant and not a section offset. */
10536 if (attr_form_is_constant (attr))
10537 *offset = dwarf2_get_attr_constant_value (attr, 0);
10538 else if (attr_form_is_section_offset (attr))
10539 dwarf2_complex_location_expr_complaint ();
10540 else if (attr_form_is_block (attr))
10541 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10542 else
10543 dwarf2_complex_location_expr_complaint ();
10544
10545 return 1;
10546 }
10547
10548 return 0;
10549}
10550
c906108c
SS
10551/* Add an aggregate field to the field list. */
10552
10553static void
107d2387 10554dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10555 struct dwarf2_cu *cu)
6e70227d 10556{
e7c27a73 10557 struct objfile *objfile = cu->objfile;
5e2b427d 10558 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10559 struct nextfield *new_field;
10560 struct attribute *attr;
10561 struct field *fp;
10562 char *fieldname = "";
10563
10564 /* Allocate a new field list entry and link it in. */
10565 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10566 make_cleanup (xfree, new_field);
c906108c 10567 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10568
10569 if (die->tag == DW_TAG_inheritance)
10570 {
10571 new_field->next = fip->baseclasses;
10572 fip->baseclasses = new_field;
10573 }
10574 else
10575 {
10576 new_field->next = fip->fields;
10577 fip->fields = new_field;
10578 }
c906108c
SS
10579 fip->nfields++;
10580
e142c38c 10581 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10582 if (attr)
10583 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10584 else
10585 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10586 if (new_field->accessibility != DW_ACCESS_public)
10587 fip->non_public_fields = 1;
60d5a603 10588
e142c38c 10589 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10590 if (attr)
10591 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10592 else
10593 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10594
10595 fp = &new_field->field;
a9a9bd0f 10596
e142c38c 10597 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10598 {
74ac6d43
TT
10599 LONGEST offset;
10600
a9a9bd0f 10601 /* Data member other than a C++ static data member. */
6e70227d 10602
c906108c 10603 /* Get type of field. */
e7c27a73 10604 fp->type = die_type (die, cu);
c906108c 10605
d6a843b5 10606 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10607
c906108c 10608 /* Get bit size of field (zero if none). */
e142c38c 10609 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10610 if (attr)
10611 {
10612 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10613 }
10614 else
10615 {
10616 FIELD_BITSIZE (*fp) = 0;
10617 }
10618
10619 /* Get bit offset of field. */
74ac6d43
TT
10620 if (handle_data_member_location (die, cu, &offset))
10621 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10622 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10623 if (attr)
10624 {
5e2b427d 10625 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10626 {
10627 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10628 additional bit offset from the MSB of the containing
10629 anonymous object to the MSB of the field. We don't
10630 have to do anything special since we don't need to
10631 know the size of the anonymous object. */
f41f5e61 10632 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10633 }
10634 else
10635 {
10636 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10637 MSB of the anonymous object, subtract off the number of
10638 bits from the MSB of the field to the MSB of the
10639 object, and then subtract off the number of bits of
10640 the field itself. The result is the bit offset of
10641 the LSB of the field. */
c906108c
SS
10642 int anonymous_size;
10643 int bit_offset = DW_UNSND (attr);
10644
e142c38c 10645 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10646 if (attr)
10647 {
10648 /* The size of the anonymous object containing
10649 the bit field is explicit, so use the
10650 indicated size (in bytes). */
10651 anonymous_size = DW_UNSND (attr);
10652 }
10653 else
10654 {
10655 /* The size of the anonymous object containing
10656 the bit field must be inferred from the type
10657 attribute of the data member containing the
10658 bit field. */
10659 anonymous_size = TYPE_LENGTH (fp->type);
10660 }
f41f5e61
PA
10661 SET_FIELD_BITPOS (*fp,
10662 (FIELD_BITPOS (*fp)
10663 + anonymous_size * bits_per_byte
10664 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10665 }
10666 }
10667
10668 /* Get name of field. */
39cbfefa
DJ
10669 fieldname = dwarf2_name (die, cu);
10670 if (fieldname == NULL)
10671 fieldname = "";
d8151005
DJ
10672
10673 /* The name is already allocated along with this objfile, so we don't
10674 need to duplicate it for the type. */
10675 fp->name = fieldname;
c906108c
SS
10676
10677 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10678 pointer or virtual base class pointer) to private. */
e142c38c 10679 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10680 {
d48cc9dd 10681 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10682 new_field->accessibility = DW_ACCESS_private;
10683 fip->non_public_fields = 1;
10684 }
10685 }
a9a9bd0f 10686 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10687 {
a9a9bd0f
DC
10688 /* C++ static member. */
10689
10690 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10691 is a declaration, but all versions of G++ as of this writing
10692 (so through at least 3.2.1) incorrectly generate
10693 DW_TAG_variable tags. */
6e70227d 10694
ff355380 10695 const char *physname;
c906108c 10696
a9a9bd0f 10697 /* Get name of field. */
39cbfefa
DJ
10698 fieldname = dwarf2_name (die, cu);
10699 if (fieldname == NULL)
c906108c
SS
10700 return;
10701
254e6b9e 10702 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10703 if (attr
10704 /* Only create a symbol if this is an external value.
10705 new_symbol checks this and puts the value in the global symbol
10706 table, which we want. If it is not external, new_symbol
10707 will try to put the value in cu->list_in_scope which is wrong. */
10708 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10709 {
10710 /* A static const member, not much different than an enum as far as
10711 we're concerned, except that we can support more types. */
10712 new_symbol (die, NULL, cu);
10713 }
10714
2df3850c 10715 /* Get physical name. */
ff355380 10716 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10717
d8151005
DJ
10718 /* The name is already allocated along with this objfile, so we don't
10719 need to duplicate it for the type. */
10720 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10721 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10722 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10723 }
10724 else if (die->tag == DW_TAG_inheritance)
10725 {
74ac6d43 10726 LONGEST offset;
d4b96c9a 10727
74ac6d43
TT
10728 /* C++ base class field. */
10729 if (handle_data_member_location (die, cu, &offset))
10730 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10731 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10732 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10733 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10734 fip->nbaseclasses++;
10735 }
10736}
10737
98751a41
JK
10738/* Add a typedef defined in the scope of the FIP's class. */
10739
10740static void
10741dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10742 struct dwarf2_cu *cu)
6e70227d 10743{
98751a41 10744 struct objfile *objfile = cu->objfile;
98751a41
JK
10745 struct typedef_field_list *new_field;
10746 struct attribute *attr;
10747 struct typedef_field *fp;
10748 char *fieldname = "";
10749
10750 /* Allocate a new field list entry and link it in. */
10751 new_field = xzalloc (sizeof (*new_field));
10752 make_cleanup (xfree, new_field);
10753
10754 gdb_assert (die->tag == DW_TAG_typedef);
10755
10756 fp = &new_field->field;
10757
10758 /* Get name of field. */
10759 fp->name = dwarf2_name (die, cu);
10760 if (fp->name == NULL)
10761 return;
10762
10763 fp->type = read_type_die (die, cu);
10764
10765 new_field->next = fip->typedef_field_list;
10766 fip->typedef_field_list = new_field;
10767 fip->typedef_field_list_count++;
10768}
10769
c906108c
SS
10770/* Create the vector of fields, and attach it to the type. */
10771
10772static void
fba45db2 10773dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10774 struct dwarf2_cu *cu)
c906108c
SS
10775{
10776 int nfields = fip->nfields;
10777
10778 /* Record the field count, allocate space for the array of fields,
10779 and create blank accessibility bitfields if necessary. */
10780 TYPE_NFIELDS (type) = nfields;
10781 TYPE_FIELDS (type) = (struct field *)
10782 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10783 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10784
b4ba55a1 10785 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10786 {
10787 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10788
10789 TYPE_FIELD_PRIVATE_BITS (type) =
10790 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10791 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10792
10793 TYPE_FIELD_PROTECTED_BITS (type) =
10794 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10795 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10796
774b6a14
TT
10797 TYPE_FIELD_IGNORE_BITS (type) =
10798 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10799 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10800 }
10801
10802 /* If the type has baseclasses, allocate and clear a bit vector for
10803 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10804 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10805 {
10806 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10807 unsigned char *pointer;
c906108c
SS
10808
10809 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10810 pointer = TYPE_ALLOC (type, num_bytes);
10811 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10812 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10813 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10814 }
10815
3e43a32a
MS
10816 /* Copy the saved-up fields into the field vector. Start from the head of
10817 the list, adding to the tail of the field array, so that they end up in
10818 the same order in the array in which they were added to the list. */
c906108c
SS
10819 while (nfields-- > 0)
10820 {
7d0ccb61
DJ
10821 struct nextfield *fieldp;
10822
10823 if (fip->fields)
10824 {
10825 fieldp = fip->fields;
10826 fip->fields = fieldp->next;
10827 }
10828 else
10829 {
10830 fieldp = fip->baseclasses;
10831 fip->baseclasses = fieldp->next;
10832 }
10833
10834 TYPE_FIELD (type, nfields) = fieldp->field;
10835 switch (fieldp->accessibility)
c906108c 10836 {
c5aa993b 10837 case DW_ACCESS_private:
b4ba55a1
JB
10838 if (cu->language != language_ada)
10839 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10840 break;
c906108c 10841
c5aa993b 10842 case DW_ACCESS_protected:
b4ba55a1
JB
10843 if (cu->language != language_ada)
10844 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10845 break;
c906108c 10846
c5aa993b
JM
10847 case DW_ACCESS_public:
10848 break;
c906108c 10849
c5aa993b
JM
10850 default:
10851 /* Unknown accessibility. Complain and treat it as public. */
10852 {
e2e0b3e5 10853 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10854 fieldp->accessibility);
c5aa993b
JM
10855 }
10856 break;
c906108c
SS
10857 }
10858 if (nfields < fip->nbaseclasses)
10859 {
7d0ccb61 10860 switch (fieldp->virtuality)
c906108c 10861 {
c5aa993b
JM
10862 case DW_VIRTUALITY_virtual:
10863 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10864 if (cu->language == language_ada)
a73c6dcd 10865 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10866 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10867 break;
c906108c
SS
10868 }
10869 }
c906108c
SS
10870 }
10871}
10872
7d27a96d
TT
10873/* Return true if this member function is a constructor, false
10874 otherwise. */
10875
10876static int
10877dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10878{
10879 const char *fieldname;
10880 const char *typename;
10881 int len;
10882
10883 if (die->parent == NULL)
10884 return 0;
10885
10886 if (die->parent->tag != DW_TAG_structure_type
10887 && die->parent->tag != DW_TAG_union_type
10888 && die->parent->tag != DW_TAG_class_type)
10889 return 0;
10890
10891 fieldname = dwarf2_name (die, cu);
10892 typename = dwarf2_name (die->parent, cu);
10893 if (fieldname == NULL || typename == NULL)
10894 return 0;
10895
10896 len = strlen (fieldname);
10897 return (strncmp (fieldname, typename, len) == 0
10898 && (typename[len] == '\0' || typename[len] == '<'));
10899}
10900
c906108c
SS
10901/* Add a member function to the proper fieldlist. */
10902
10903static void
107d2387 10904dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10905 struct type *type, struct dwarf2_cu *cu)
c906108c 10906{
e7c27a73 10907 struct objfile *objfile = cu->objfile;
c906108c
SS
10908 struct attribute *attr;
10909 struct fnfieldlist *flp;
10910 int i;
10911 struct fn_field *fnp;
10912 char *fieldname;
c906108c 10913 struct nextfnfield *new_fnfield;
f792889a 10914 struct type *this_type;
60d5a603 10915 enum dwarf_access_attribute accessibility;
c906108c 10916
b4ba55a1 10917 if (cu->language == language_ada)
a73c6dcd 10918 error (_("unexpected member function in Ada type"));
b4ba55a1 10919
2df3850c 10920 /* Get name of member function. */
39cbfefa
DJ
10921 fieldname = dwarf2_name (die, cu);
10922 if (fieldname == NULL)
2df3850c 10923 return;
c906108c 10924
c906108c
SS
10925 /* Look up member function name in fieldlist. */
10926 for (i = 0; i < fip->nfnfields; i++)
10927 {
27bfe10e 10928 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10929 break;
10930 }
10931
10932 /* Create new list element if necessary. */
10933 if (i < fip->nfnfields)
10934 flp = &fip->fnfieldlists[i];
10935 else
10936 {
10937 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10938 {
10939 fip->fnfieldlists = (struct fnfieldlist *)
10940 xrealloc (fip->fnfieldlists,
10941 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10942 * sizeof (struct fnfieldlist));
c906108c 10943 if (fip->nfnfields == 0)
c13c43fd 10944 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10945 }
10946 flp = &fip->fnfieldlists[fip->nfnfields];
10947 flp->name = fieldname;
10948 flp->length = 0;
10949 flp->head = NULL;
3da10d80 10950 i = fip->nfnfields++;
c906108c
SS
10951 }
10952
10953 /* Create a new member function field and chain it to the field list
0963b4bd 10954 entry. */
c906108c 10955 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10956 make_cleanup (xfree, new_fnfield);
c906108c
SS
10957 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10958 new_fnfield->next = flp->head;
10959 flp->head = new_fnfield;
10960 flp->length++;
10961
10962 /* Fill in the member function field info. */
10963 fnp = &new_fnfield->fnfield;
3da10d80
KS
10964
10965 /* Delay processing of the physname until later. */
10966 if (cu->language == language_cplus || cu->language == language_java)
10967 {
10968 add_to_method_list (type, i, flp->length - 1, fieldname,
10969 die, cu);
10970 }
10971 else
10972 {
1d06ead6 10973 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10974 fnp->physname = physname ? physname : "";
10975 }
10976
c906108c 10977 fnp->type = alloc_type (objfile);
f792889a
DJ
10978 this_type = read_type_die (die, cu);
10979 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10980 {
f792889a 10981 int nparams = TYPE_NFIELDS (this_type);
c906108c 10982
f792889a 10983 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10984 of the method itself (TYPE_CODE_METHOD). */
10985 smash_to_method_type (fnp->type, type,
f792889a
DJ
10986 TYPE_TARGET_TYPE (this_type),
10987 TYPE_FIELDS (this_type),
10988 TYPE_NFIELDS (this_type),
10989 TYPE_VARARGS (this_type));
c906108c
SS
10990
10991 /* Handle static member functions.
c5aa993b 10992 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10993 member functions. G++ helps GDB by marking the first
10994 parameter for non-static member functions (which is the this
10995 pointer) as artificial. We obtain this information from
10996 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10997 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10998 fnp->voffset = VOFFSET_STATIC;
10999 }
11000 else
e2e0b3e5 11001 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11002 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11003
11004 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11005 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11006 fnp->fcontext = die_containing_type (die, cu);
c906108c 11007
3e43a32a
MS
11008 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11009 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11010
11011 /* Get accessibility. */
e142c38c 11012 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11013 if (attr)
60d5a603
JK
11014 accessibility = DW_UNSND (attr);
11015 else
11016 accessibility = dwarf2_default_access_attribute (die, cu);
11017 switch (accessibility)
c906108c 11018 {
60d5a603
JK
11019 case DW_ACCESS_private:
11020 fnp->is_private = 1;
11021 break;
11022 case DW_ACCESS_protected:
11023 fnp->is_protected = 1;
11024 break;
c906108c
SS
11025 }
11026
b02dede2 11027 /* Check for artificial methods. */
e142c38c 11028 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11029 if (attr && DW_UNSND (attr) != 0)
11030 fnp->is_artificial = 1;
11031
7d27a96d
TT
11032 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11033
0d564a31 11034 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11035 function. For older versions of GCC, this is an offset in the
11036 appropriate virtual table, as specified by DW_AT_containing_type.
11037 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11038 to the object address. */
11039
e142c38c 11040 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11041 if (attr)
8e19ed76 11042 {
aec5aa8b 11043 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11044 {
aec5aa8b
TT
11045 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11046 {
11047 /* Old-style GCC. */
11048 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11049 }
11050 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11051 || (DW_BLOCK (attr)->size > 1
11052 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11053 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11054 {
11055 struct dwarf_block blk;
11056 int offset;
11057
11058 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11059 ? 1 : 2);
11060 blk.size = DW_BLOCK (attr)->size - offset;
11061 blk.data = DW_BLOCK (attr)->data + offset;
11062 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11063 if ((fnp->voffset % cu->header.addr_size) != 0)
11064 dwarf2_complex_location_expr_complaint ();
11065 else
11066 fnp->voffset /= cu->header.addr_size;
11067 fnp->voffset += 2;
11068 }
11069 else
11070 dwarf2_complex_location_expr_complaint ();
11071
11072 if (!fnp->fcontext)
11073 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11074 }
3690dd37 11075 else if (attr_form_is_section_offset (attr))
8e19ed76 11076 {
4d3c2250 11077 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11078 }
11079 else
11080 {
4d3c2250
KB
11081 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11082 fieldname);
8e19ed76 11083 }
0d564a31 11084 }
d48cc9dd
DJ
11085 else
11086 {
11087 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11088 if (attr && DW_UNSND (attr))
11089 {
11090 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11091 complaint (&symfile_complaints,
3e43a32a
MS
11092 _("Member function \"%s\" (offset %d) is virtual "
11093 "but the vtable offset is not specified"),
b64f50a1 11094 fieldname, die->offset.sect_off);
9655fd1a 11095 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11096 TYPE_CPLUS_DYNAMIC (type) = 1;
11097 }
11098 }
c906108c
SS
11099}
11100
11101/* Create the vector of member function fields, and attach it to the type. */
11102
11103static void
fba45db2 11104dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11105 struct dwarf2_cu *cu)
c906108c
SS
11106{
11107 struct fnfieldlist *flp;
c906108c
SS
11108 int i;
11109
b4ba55a1 11110 if (cu->language == language_ada)
a73c6dcd 11111 error (_("unexpected member functions in Ada type"));
b4ba55a1 11112
c906108c
SS
11113 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11114 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11115 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11116
11117 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11118 {
11119 struct nextfnfield *nfp = flp->head;
11120 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11121 int k;
11122
11123 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11124 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11125 fn_flp->fn_fields = (struct fn_field *)
11126 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11127 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11128 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11129 }
11130
11131 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11132}
11133
1168df01
JB
11134/* Returns non-zero if NAME is the name of a vtable member in CU's
11135 language, zero otherwise. */
11136static int
11137is_vtable_name (const char *name, struct dwarf2_cu *cu)
11138{
11139 static const char vptr[] = "_vptr";
987504bb 11140 static const char vtable[] = "vtable";
1168df01 11141
987504bb
JJ
11142 /* Look for the C++ and Java forms of the vtable. */
11143 if ((cu->language == language_java
11144 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11145 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11146 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11147 return 1;
11148
11149 return 0;
11150}
11151
c0dd20ea 11152/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11153 functions, with the ABI-specified layout. If TYPE describes
11154 such a structure, smash it into a member function type.
61049d3b
DJ
11155
11156 GCC shouldn't do this; it should just output pointer to member DIEs.
11157 This is GCC PR debug/28767. */
c0dd20ea 11158
0b92b5bb
TT
11159static void
11160quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11161{
0b92b5bb 11162 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11163
11164 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11165 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11166 return;
c0dd20ea
DJ
11167
11168 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11169 if (TYPE_FIELD_NAME (type, 0) == NULL
11170 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11171 || TYPE_FIELD_NAME (type, 1) == NULL
11172 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11173 return;
c0dd20ea
DJ
11174
11175 /* Find the type of the method. */
0b92b5bb 11176 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11177 if (pfn_type == NULL
11178 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11179 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11180 return;
c0dd20ea
DJ
11181
11182 /* Look for the "this" argument. */
11183 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11184 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11185 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11186 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11187 return;
c0dd20ea
DJ
11188
11189 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11190 new_type = alloc_type (objfile);
11191 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11192 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11193 TYPE_VARARGS (pfn_type));
0b92b5bb 11194 smash_to_methodptr_type (type, new_type);
c0dd20ea 11195}
1168df01 11196
685b1105
JK
11197/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11198 (icc). */
11199
11200static int
11201producer_is_icc (struct dwarf2_cu *cu)
11202{
11203 if (!cu->checked_producer)
11204 check_producer (cu);
11205
11206 return cu->producer_is_icc;
11207}
11208
c906108c 11209/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11210 (definition) to create a type for the structure or union. Fill in
11211 the type's name and general properties; the members will not be
11212 processed until process_structure_type.
c906108c 11213
c767944b
DJ
11214 NOTE: we need to call these functions regardless of whether or not the
11215 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11216 structure or union. This gets the type entered into our set of
11217 user defined types.
11218
11219 However, if the structure is incomplete (an opaque struct/union)
11220 then suppress creating a symbol table entry for it since gdb only
11221 wants to find the one with the complete definition. Note that if
11222 it is complete, we just call new_symbol, which does it's own
11223 checking about whether the struct/union is anonymous or not (and
11224 suppresses creating a symbol table entry itself). */
11225
f792889a 11226static struct type *
134d01f1 11227read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11228{
e7c27a73 11229 struct objfile *objfile = cu->objfile;
c906108c
SS
11230 struct type *type;
11231 struct attribute *attr;
39cbfefa 11232 char *name;
c906108c 11233
348e048f
DE
11234 /* If the definition of this type lives in .debug_types, read that type.
11235 Don't follow DW_AT_specification though, that will take us back up
11236 the chain and we want to go down. */
45e58e77 11237 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11238 if (attr)
11239 {
11240 struct dwarf2_cu *type_cu = cu;
11241 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11242
348e048f
DE
11243 /* We could just recurse on read_structure_type, but we need to call
11244 get_die_type to ensure only one type for this DIE is created.
11245 This is important, for example, because for c++ classes we need
11246 TYPE_NAME set which is only done by new_symbol. Blech. */
11247 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11248
11249 /* TYPE_CU may not be the same as CU.
11250 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11251 return set_die_type (die, type, cu);
11252 }
11253
c0dd20ea 11254 type = alloc_type (objfile);
c906108c 11255 INIT_CPLUS_SPECIFIC (type);
93311388 11256
39cbfefa
DJ
11257 name = dwarf2_name (die, cu);
11258 if (name != NULL)
c906108c 11259 {
987504bb
JJ
11260 if (cu->language == language_cplus
11261 || cu->language == language_java)
63d06c5c 11262 {
3da10d80
KS
11263 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11264
11265 /* dwarf2_full_name might have already finished building the DIE's
11266 type. If so, there is no need to continue. */
11267 if (get_die_type (die, cu) != NULL)
11268 return get_die_type (die, cu);
11269
11270 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11271 if (die->tag == DW_TAG_structure_type
11272 || die->tag == DW_TAG_class_type)
11273 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11274 }
11275 else
11276 {
d8151005
DJ
11277 /* The name is already allocated along with this objfile, so
11278 we don't need to duplicate it for the type. */
94af9270
KS
11279 TYPE_TAG_NAME (type) = (char *) name;
11280 if (die->tag == DW_TAG_class_type)
11281 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11282 }
c906108c
SS
11283 }
11284
11285 if (die->tag == DW_TAG_structure_type)
11286 {
11287 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11288 }
11289 else if (die->tag == DW_TAG_union_type)
11290 {
11291 TYPE_CODE (type) = TYPE_CODE_UNION;
11292 }
11293 else
11294 {
c906108c
SS
11295 TYPE_CODE (type) = TYPE_CODE_CLASS;
11296 }
11297
0cc2414c
TT
11298 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11299 TYPE_DECLARED_CLASS (type) = 1;
11300
e142c38c 11301 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11302 if (attr)
11303 {
11304 TYPE_LENGTH (type) = DW_UNSND (attr);
11305 }
11306 else
11307 {
11308 TYPE_LENGTH (type) = 0;
11309 }
11310
685b1105
JK
11311 if (producer_is_icc (cu))
11312 {
11313 /* ICC does not output the required DW_AT_declaration
11314 on incomplete types, but gives them a size of zero. */
11315 }
11316 else
11317 TYPE_STUB_SUPPORTED (type) = 1;
11318
dc718098 11319 if (die_is_declaration (die, cu))
876cecd0 11320 TYPE_STUB (type) = 1;
a6c727b2
DJ
11321 else if (attr == NULL && die->child == NULL
11322 && producer_is_realview (cu->producer))
11323 /* RealView does not output the required DW_AT_declaration
11324 on incomplete types. */
11325 TYPE_STUB (type) = 1;
dc718098 11326
c906108c
SS
11327 /* We need to add the type field to the die immediately so we don't
11328 infinitely recurse when dealing with pointers to the structure
0963b4bd 11329 type within the structure itself. */
1c379e20 11330 set_die_type (die, type, cu);
c906108c 11331
7e314c57
JK
11332 /* set_die_type should be already done. */
11333 set_descriptive_type (type, die, cu);
11334
c767944b
DJ
11335 return type;
11336}
11337
11338/* Finish creating a structure or union type, including filling in
11339 its members and creating a symbol for it. */
11340
11341static void
11342process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11343{
11344 struct objfile *objfile = cu->objfile;
11345 struct die_info *child_die = die->child;
11346 struct type *type;
11347
11348 type = get_die_type (die, cu);
11349 if (type == NULL)
11350 type = read_structure_type (die, cu);
11351
e142c38c 11352 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11353 {
11354 struct field_info fi;
11355 struct die_info *child_die;
34eaf542 11356 VEC (symbolp) *template_args = NULL;
c767944b 11357 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11358
11359 memset (&fi, 0, sizeof (struct field_info));
11360
639d11d3 11361 child_die = die->child;
c906108c
SS
11362
11363 while (child_die && child_die->tag)
11364 {
a9a9bd0f
DC
11365 if (child_die->tag == DW_TAG_member
11366 || child_die->tag == DW_TAG_variable)
c906108c 11367 {
a9a9bd0f
DC
11368 /* NOTE: carlton/2002-11-05: A C++ static data member
11369 should be a DW_TAG_member that is a declaration, but
11370 all versions of G++ as of this writing (so through at
11371 least 3.2.1) incorrectly generate DW_TAG_variable
11372 tags for them instead. */
e7c27a73 11373 dwarf2_add_field (&fi, child_die, cu);
c906108c 11374 }
8713b1b1 11375 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11376 {
0963b4bd 11377 /* C++ member function. */
e7c27a73 11378 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11379 }
11380 else if (child_die->tag == DW_TAG_inheritance)
11381 {
11382 /* C++ base class field. */
e7c27a73 11383 dwarf2_add_field (&fi, child_die, cu);
c906108c 11384 }
98751a41
JK
11385 else if (child_die->tag == DW_TAG_typedef)
11386 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11387 else if (child_die->tag == DW_TAG_template_type_param
11388 || child_die->tag == DW_TAG_template_value_param)
11389 {
11390 struct symbol *arg = new_symbol (child_die, NULL, cu);
11391
f1078f66
DJ
11392 if (arg != NULL)
11393 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11394 }
11395
c906108c
SS
11396 child_die = sibling_die (child_die);
11397 }
11398
34eaf542
TT
11399 /* Attach template arguments to type. */
11400 if (! VEC_empty (symbolp, template_args))
11401 {
11402 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11403 TYPE_N_TEMPLATE_ARGUMENTS (type)
11404 = VEC_length (symbolp, template_args);
11405 TYPE_TEMPLATE_ARGUMENTS (type)
11406 = obstack_alloc (&objfile->objfile_obstack,
11407 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11408 * sizeof (struct symbol *)));
11409 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11410 VEC_address (symbolp, template_args),
11411 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11412 * sizeof (struct symbol *)));
11413 VEC_free (symbolp, template_args);
11414 }
11415
c906108c
SS
11416 /* Attach fields and member functions to the type. */
11417 if (fi.nfields)
e7c27a73 11418 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11419 if (fi.nfnfields)
11420 {
e7c27a73 11421 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11422
c5aa993b 11423 /* Get the type which refers to the base class (possibly this
c906108c 11424 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11425 class from the DW_AT_containing_type attribute. This use of
11426 DW_AT_containing_type is a GNU extension. */
c906108c 11427
e142c38c 11428 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11429 {
e7c27a73 11430 struct type *t = die_containing_type (die, cu);
c906108c
SS
11431
11432 TYPE_VPTR_BASETYPE (type) = t;
11433 if (type == t)
11434 {
c906108c
SS
11435 int i;
11436
11437 /* Our own class provides vtbl ptr. */
11438 for (i = TYPE_NFIELDS (t) - 1;
11439 i >= TYPE_N_BASECLASSES (t);
11440 --i)
11441 {
0d5cff50 11442 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11443
1168df01 11444 if (is_vtable_name (fieldname, cu))
c906108c
SS
11445 {
11446 TYPE_VPTR_FIELDNO (type) = i;
11447 break;
11448 }
11449 }
11450
11451 /* Complain if virtual function table field not found. */
11452 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11453 complaint (&symfile_complaints,
3e43a32a
MS
11454 _("virtual function table pointer "
11455 "not found when defining class '%s'"),
4d3c2250
KB
11456 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11457 "");
c906108c
SS
11458 }
11459 else
11460 {
11461 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11462 }
11463 }
f6235d4c
EZ
11464 else if (cu->producer
11465 && strncmp (cu->producer,
11466 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11467 {
11468 /* The IBM XLC compiler does not provide direct indication
11469 of the containing type, but the vtable pointer is
11470 always named __vfp. */
11471
11472 int i;
11473
11474 for (i = TYPE_NFIELDS (type) - 1;
11475 i >= TYPE_N_BASECLASSES (type);
11476 --i)
11477 {
11478 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11479 {
11480 TYPE_VPTR_FIELDNO (type) = i;
11481 TYPE_VPTR_BASETYPE (type) = type;
11482 break;
11483 }
11484 }
11485 }
c906108c 11486 }
98751a41
JK
11487
11488 /* Copy fi.typedef_field_list linked list elements content into the
11489 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11490 if (fi.typedef_field_list)
11491 {
11492 int i = fi.typedef_field_list_count;
11493
a0d7a4ff 11494 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11495 TYPE_TYPEDEF_FIELD_ARRAY (type)
11496 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11497 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11498
11499 /* Reverse the list order to keep the debug info elements order. */
11500 while (--i >= 0)
11501 {
11502 struct typedef_field *dest, *src;
6e70227d 11503
98751a41
JK
11504 dest = &TYPE_TYPEDEF_FIELD (type, i);
11505 src = &fi.typedef_field_list->field;
11506 fi.typedef_field_list = fi.typedef_field_list->next;
11507 *dest = *src;
11508 }
11509 }
c767944b
DJ
11510
11511 do_cleanups (back_to);
eb2a6f42
TT
11512
11513 if (HAVE_CPLUS_STRUCT (type))
11514 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11515 }
63d06c5c 11516
bb5ed363 11517 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11518
90aeadfc
DC
11519 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11520 snapshots) has been known to create a die giving a declaration
11521 for a class that has, as a child, a die giving a definition for a
11522 nested class. So we have to process our children even if the
11523 current die is a declaration. Normally, of course, a declaration
11524 won't have any children at all. */
134d01f1 11525
90aeadfc
DC
11526 while (child_die != NULL && child_die->tag)
11527 {
11528 if (child_die->tag == DW_TAG_member
11529 || child_die->tag == DW_TAG_variable
34eaf542
TT
11530 || child_die->tag == DW_TAG_inheritance
11531 || child_die->tag == DW_TAG_template_value_param
11532 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11533 {
90aeadfc 11534 /* Do nothing. */
134d01f1 11535 }
90aeadfc
DC
11536 else
11537 process_die (child_die, cu);
134d01f1 11538
90aeadfc 11539 child_die = sibling_die (child_die);
134d01f1
DJ
11540 }
11541
fa4028e9
JB
11542 /* Do not consider external references. According to the DWARF standard,
11543 these DIEs are identified by the fact that they have no byte_size
11544 attribute, and a declaration attribute. */
11545 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11546 || !die_is_declaration (die, cu))
c767944b 11547 new_symbol (die, type, cu);
134d01f1
DJ
11548}
11549
11550/* Given a DW_AT_enumeration_type die, set its type. We do not
11551 complete the type's fields yet, or create any symbols. */
c906108c 11552
f792889a 11553static struct type *
134d01f1 11554read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11555{
e7c27a73 11556 struct objfile *objfile = cu->objfile;
c906108c 11557 struct type *type;
c906108c 11558 struct attribute *attr;
0114d602 11559 const char *name;
134d01f1 11560
348e048f
DE
11561 /* If the definition of this type lives in .debug_types, read that type.
11562 Don't follow DW_AT_specification though, that will take us back up
11563 the chain and we want to go down. */
45e58e77 11564 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11565 if (attr)
11566 {
11567 struct dwarf2_cu *type_cu = cu;
11568 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11569
348e048f 11570 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11571
11572 /* TYPE_CU may not be the same as CU.
11573 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11574 return set_die_type (die, type, cu);
11575 }
11576
c906108c
SS
11577 type = alloc_type (objfile);
11578
11579 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11580 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11581 if (name != NULL)
0114d602 11582 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11583
e142c38c 11584 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11585 if (attr)
11586 {
11587 TYPE_LENGTH (type) = DW_UNSND (attr);
11588 }
11589 else
11590 {
11591 TYPE_LENGTH (type) = 0;
11592 }
11593
137033e9
JB
11594 /* The enumeration DIE can be incomplete. In Ada, any type can be
11595 declared as private in the package spec, and then defined only
11596 inside the package body. Such types are known as Taft Amendment
11597 Types. When another package uses such a type, an incomplete DIE
11598 may be generated by the compiler. */
02eb380e 11599 if (die_is_declaration (die, cu))
876cecd0 11600 TYPE_STUB (type) = 1;
02eb380e 11601
f792889a 11602 return set_die_type (die, type, cu);
134d01f1
DJ
11603}
11604
11605/* Given a pointer to a die which begins an enumeration, process all
11606 the dies that define the members of the enumeration, and create the
11607 symbol for the enumeration type.
11608
11609 NOTE: We reverse the order of the element list. */
11610
11611static void
11612process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11613{
f792889a 11614 struct type *this_type;
134d01f1 11615
f792889a
DJ
11616 this_type = get_die_type (die, cu);
11617 if (this_type == NULL)
11618 this_type = read_enumeration_type (die, cu);
9dc481d3 11619
639d11d3 11620 if (die->child != NULL)
c906108c 11621 {
9dc481d3
DE
11622 struct die_info *child_die;
11623 struct symbol *sym;
11624 struct field *fields = NULL;
11625 int num_fields = 0;
11626 int unsigned_enum = 1;
11627 char *name;
cafec441
TT
11628 int flag_enum = 1;
11629 ULONGEST mask = 0;
9dc481d3 11630
639d11d3 11631 child_die = die->child;
c906108c
SS
11632 while (child_die && child_die->tag)
11633 {
11634 if (child_die->tag != DW_TAG_enumerator)
11635 {
e7c27a73 11636 process_die (child_die, cu);
c906108c
SS
11637 }
11638 else
11639 {
39cbfefa
DJ
11640 name = dwarf2_name (child_die, cu);
11641 if (name)
c906108c 11642 {
f792889a 11643 sym = new_symbol (child_die, this_type, cu);
c906108c 11644 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11645 {
11646 unsigned_enum = 0;
11647 flag_enum = 0;
11648 }
11649 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11650 flag_enum = 0;
11651 else
11652 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11653
11654 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11655 {
11656 fields = (struct field *)
11657 xrealloc (fields,
11658 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11659 * sizeof (struct field));
c906108c
SS
11660 }
11661
3567439c 11662 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11663 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11664 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11665 FIELD_BITSIZE (fields[num_fields]) = 0;
11666
11667 num_fields++;
11668 }
11669 }
11670
11671 child_die = sibling_die (child_die);
11672 }
11673
11674 if (num_fields)
11675 {
f792889a
DJ
11676 TYPE_NFIELDS (this_type) = num_fields;
11677 TYPE_FIELDS (this_type) = (struct field *)
11678 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11679 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11680 sizeof (struct field) * num_fields);
b8c9b27d 11681 xfree (fields);
c906108c
SS
11682 }
11683 if (unsigned_enum)
876cecd0 11684 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11685 if (flag_enum)
11686 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11687 }
134d01f1 11688
6c83ed52
TT
11689 /* If we are reading an enum from a .debug_types unit, and the enum
11690 is a declaration, and the enum is not the signatured type in the
11691 unit, then we do not want to add a symbol for it. Adding a
11692 symbol would in some cases obscure the true definition of the
11693 enum, giving users an incomplete type when the definition is
11694 actually available. Note that we do not want to do this for all
11695 enums which are just declarations, because C++0x allows forward
11696 enum declarations. */
3019eac3 11697 if (cu->per_cu->is_debug_types
6c83ed52
TT
11698 && die_is_declaration (die, cu))
11699 {
52dc124a 11700 struct signatured_type *sig_type;
6c83ed52 11701
52dc124a 11702 sig_type
6c83ed52 11703 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11704 cu->per_cu->info_or_types_section,
6c83ed52 11705 cu->per_cu->offset);
3019eac3
DE
11706 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11707 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11708 return;
11709 }
11710
f792889a 11711 new_symbol (die, this_type, cu);
c906108c
SS
11712}
11713
11714/* Extract all information from a DW_TAG_array_type DIE and put it in
11715 the DIE's type field. For now, this only handles one dimensional
11716 arrays. */
11717
f792889a 11718static struct type *
e7c27a73 11719read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11720{
e7c27a73 11721 struct objfile *objfile = cu->objfile;
c906108c 11722 struct die_info *child_die;
7e314c57 11723 struct type *type;
c906108c
SS
11724 struct type *element_type, *range_type, *index_type;
11725 struct type **range_types = NULL;
11726 struct attribute *attr;
11727 int ndim = 0;
11728 struct cleanup *back_to;
39cbfefa 11729 char *name;
c906108c 11730
e7c27a73 11731 element_type = die_type (die, cu);
c906108c 11732
7e314c57
JK
11733 /* The die_type call above may have already set the type for this DIE. */
11734 type = get_die_type (die, cu);
11735 if (type)
11736 return type;
11737
c906108c
SS
11738 /* Irix 6.2 native cc creates array types without children for
11739 arrays with unspecified length. */
639d11d3 11740 if (die->child == NULL)
c906108c 11741 {
46bf5051 11742 index_type = objfile_type (objfile)->builtin_int;
c906108c 11743 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11744 type = create_array_type (NULL, element_type, range_type);
11745 return set_die_type (die, type, cu);
c906108c
SS
11746 }
11747
11748 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11749 child_die = die->child;
c906108c
SS
11750 while (child_die && child_die->tag)
11751 {
11752 if (child_die->tag == DW_TAG_subrange_type)
11753 {
f792889a 11754 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11755
f792889a 11756 if (child_type != NULL)
a02abb62 11757 {
0963b4bd
MS
11758 /* The range type was succesfully read. Save it for the
11759 array type creation. */
a02abb62
JB
11760 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11761 {
11762 range_types = (struct type **)
11763 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11764 * sizeof (struct type *));
11765 if (ndim == 0)
11766 make_cleanup (free_current_contents, &range_types);
11767 }
f792889a 11768 range_types[ndim++] = child_type;
a02abb62 11769 }
c906108c
SS
11770 }
11771 child_die = sibling_die (child_die);
11772 }
11773
11774 /* Dwarf2 dimensions are output from left to right, create the
11775 necessary array types in backwards order. */
7ca2d3a3 11776
c906108c 11777 type = element_type;
7ca2d3a3
DL
11778
11779 if (read_array_order (die, cu) == DW_ORD_col_major)
11780 {
11781 int i = 0;
9a619af0 11782
7ca2d3a3
DL
11783 while (i < ndim)
11784 type = create_array_type (NULL, type, range_types[i++]);
11785 }
11786 else
11787 {
11788 while (ndim-- > 0)
11789 type = create_array_type (NULL, type, range_types[ndim]);
11790 }
c906108c 11791
f5f8a009
EZ
11792 /* Understand Dwarf2 support for vector types (like they occur on
11793 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11794 array type. This is not part of the Dwarf2/3 standard yet, but a
11795 custom vendor extension. The main difference between a regular
11796 array and the vector variant is that vectors are passed by value
11797 to functions. */
e142c38c 11798 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11799 if (attr)
ea37ba09 11800 make_vector_type (type);
f5f8a009 11801
dbc98a8b
KW
11802 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11803 implementation may choose to implement triple vectors using this
11804 attribute. */
11805 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11806 if (attr)
11807 {
11808 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11809 TYPE_LENGTH (type) = DW_UNSND (attr);
11810 else
3e43a32a
MS
11811 complaint (&symfile_complaints,
11812 _("DW_AT_byte_size for array type smaller "
11813 "than the total size of elements"));
dbc98a8b
KW
11814 }
11815
39cbfefa
DJ
11816 name = dwarf2_name (die, cu);
11817 if (name)
11818 TYPE_NAME (type) = name;
6e70227d 11819
0963b4bd 11820 /* Install the type in the die. */
7e314c57
JK
11821 set_die_type (die, type, cu);
11822
11823 /* set_die_type should be already done. */
b4ba55a1
JB
11824 set_descriptive_type (type, die, cu);
11825
c906108c
SS
11826 do_cleanups (back_to);
11827
7e314c57 11828 return type;
c906108c
SS
11829}
11830
7ca2d3a3 11831static enum dwarf_array_dim_ordering
6e70227d 11832read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11833{
11834 struct attribute *attr;
11835
11836 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11837
11838 if (attr) return DW_SND (attr);
11839
0963b4bd
MS
11840 /* GNU F77 is a special case, as at 08/2004 array type info is the
11841 opposite order to the dwarf2 specification, but data is still
11842 laid out as per normal fortran.
7ca2d3a3 11843
0963b4bd
MS
11844 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11845 version checking. */
7ca2d3a3 11846
905e0470
PM
11847 if (cu->language == language_fortran
11848 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11849 {
11850 return DW_ORD_row_major;
11851 }
11852
6e70227d 11853 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11854 {
11855 case array_column_major:
11856 return DW_ORD_col_major;
11857 case array_row_major:
11858 default:
11859 return DW_ORD_row_major;
11860 };
11861}
11862
72019c9c 11863/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11864 the DIE's type field. */
72019c9c 11865
f792889a 11866static struct type *
72019c9c
GM
11867read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11868{
7e314c57
JK
11869 struct type *domain_type, *set_type;
11870 struct attribute *attr;
f792889a 11871
7e314c57
JK
11872 domain_type = die_type (die, cu);
11873
11874 /* The die_type call above may have already set the type for this DIE. */
11875 set_type = get_die_type (die, cu);
11876 if (set_type)
11877 return set_type;
11878
11879 set_type = create_set_type (NULL, domain_type);
11880
11881 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11882 if (attr)
11883 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11884
f792889a 11885 return set_die_type (die, set_type, cu);
72019c9c 11886}
7ca2d3a3 11887
0971de02
TT
11888/* A helper for read_common_block that creates a locexpr baton.
11889 SYM is the symbol which we are marking as computed.
11890 COMMON_DIE is the DIE for the common block.
11891 COMMON_LOC is the location expression attribute for the common
11892 block itself.
11893 MEMBER_LOC is the location expression attribute for the particular
11894 member of the common block that we are processing.
11895 CU is the CU from which the above come. */
11896
11897static void
11898mark_common_block_symbol_computed (struct symbol *sym,
11899 struct die_info *common_die,
11900 struct attribute *common_loc,
11901 struct attribute *member_loc,
11902 struct dwarf2_cu *cu)
11903{
11904 struct objfile *objfile = dwarf2_per_objfile->objfile;
11905 struct dwarf2_locexpr_baton *baton;
11906 gdb_byte *ptr;
11907 unsigned int cu_off;
11908 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11909 LONGEST offset = 0;
11910
11911 gdb_assert (common_loc && member_loc);
11912 gdb_assert (attr_form_is_block (common_loc));
11913 gdb_assert (attr_form_is_block (member_loc)
11914 || attr_form_is_constant (member_loc));
11915
11916 baton = obstack_alloc (&objfile->objfile_obstack,
11917 sizeof (struct dwarf2_locexpr_baton));
11918 baton->per_cu = cu->per_cu;
11919 gdb_assert (baton->per_cu);
11920
11921 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11922
11923 if (attr_form_is_constant (member_loc))
11924 {
11925 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11926 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11927 }
11928 else
11929 baton->size += DW_BLOCK (member_loc)->size;
11930
11931 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11932 baton->data = ptr;
11933
11934 *ptr++ = DW_OP_call4;
11935 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11936 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11937 ptr += 4;
11938
11939 if (attr_form_is_constant (member_loc))
11940 {
11941 *ptr++ = DW_OP_addr;
11942 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11943 ptr += cu->header.addr_size;
11944 }
11945 else
11946 {
11947 /* We have to copy the data here, because DW_OP_call4 will only
11948 use a DW_AT_location attribute. */
11949 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11950 ptr += DW_BLOCK (member_loc)->size;
11951 }
11952
11953 *ptr++ = DW_OP_plus;
11954 gdb_assert (ptr - baton->data == baton->size);
11955
11956 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11957 SYMBOL_LOCATION_BATON (sym) = baton;
11958 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11959}
11960
4357ac6c
TT
11961/* Create appropriate locally-scoped variables for all the
11962 DW_TAG_common_block entries. Also create a struct common_block
11963 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11964 is used to sepate the common blocks name namespace from regular
11965 variable names. */
c906108c
SS
11966
11967static void
e7c27a73 11968read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11969{
0971de02
TT
11970 struct attribute *attr;
11971
11972 attr = dwarf2_attr (die, DW_AT_location, cu);
11973 if (attr)
11974 {
11975 /* Support the .debug_loc offsets. */
11976 if (attr_form_is_block (attr))
11977 {
11978 /* Ok. */
11979 }
11980 else if (attr_form_is_section_offset (attr))
11981 {
11982 dwarf2_complex_location_expr_complaint ();
11983 attr = NULL;
11984 }
11985 else
11986 {
11987 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11988 "common block member");
11989 attr = NULL;
11990 }
11991 }
11992
639d11d3 11993 if (die->child != NULL)
c906108c 11994 {
4357ac6c
TT
11995 struct objfile *objfile = cu->objfile;
11996 struct die_info *child_die;
11997 size_t n_entries = 0, size;
11998 struct common_block *common_block;
11999 struct symbol *sym;
74ac6d43 12000
4357ac6c
TT
12001 for (child_die = die->child;
12002 child_die && child_die->tag;
12003 child_die = sibling_die (child_die))
12004 ++n_entries;
12005
12006 size = (sizeof (struct common_block)
12007 + (n_entries - 1) * sizeof (struct symbol *));
12008 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12009 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12010 common_block->n_entries = 0;
12011
12012 for (child_die = die->child;
12013 child_die && child_die->tag;
12014 child_die = sibling_die (child_die))
12015 {
12016 /* Create the symbol in the DW_TAG_common_block block in the current
12017 symbol scope. */
e7c27a73 12018 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12019 if (sym != NULL)
12020 {
12021 struct attribute *member_loc;
12022
12023 common_block->contents[common_block->n_entries++] = sym;
12024
12025 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12026 cu);
12027 if (member_loc)
12028 {
12029 /* GDB has handled this for a long time, but it is
12030 not specified by DWARF. It seems to have been
12031 emitted by gfortran at least as recently as:
12032 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12033 complaint (&symfile_complaints,
12034 _("Variable in common block has "
12035 "DW_AT_data_member_location "
12036 "- DIE at 0x%x [in module %s]"),
12037 child_die->offset.sect_off, cu->objfile->name);
12038
12039 if (attr_form_is_section_offset (member_loc))
12040 dwarf2_complex_location_expr_complaint ();
12041 else if (attr_form_is_constant (member_loc)
12042 || attr_form_is_block (member_loc))
12043 {
12044 if (attr)
12045 mark_common_block_symbol_computed (sym, die, attr,
12046 member_loc, cu);
12047 }
12048 else
12049 dwarf2_complex_location_expr_complaint ();
12050 }
12051 }
c906108c 12052 }
4357ac6c
TT
12053
12054 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12055 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12056 }
12057}
12058
0114d602 12059/* Create a type for a C++ namespace. */
d9fa45fe 12060
0114d602
DJ
12061static struct type *
12062read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12063{
e7c27a73 12064 struct objfile *objfile = cu->objfile;
0114d602 12065 const char *previous_prefix, *name;
9219021c 12066 int is_anonymous;
0114d602
DJ
12067 struct type *type;
12068
12069 /* For extensions, reuse the type of the original namespace. */
12070 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12071 {
12072 struct die_info *ext_die;
12073 struct dwarf2_cu *ext_cu = cu;
9a619af0 12074
0114d602
DJ
12075 ext_die = dwarf2_extension (die, &ext_cu);
12076 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12077
12078 /* EXT_CU may not be the same as CU.
12079 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12080 return set_die_type (die, type, cu);
12081 }
9219021c 12082
e142c38c 12083 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12084
12085 /* Now build the name of the current namespace. */
12086
0114d602
DJ
12087 previous_prefix = determine_prefix (die, cu);
12088 if (previous_prefix[0] != '\0')
12089 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12090 previous_prefix, name, 0, cu);
0114d602
DJ
12091
12092 /* Create the type. */
12093 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12094 objfile);
12095 TYPE_NAME (type) = (char *) name;
12096 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12097
60531b24 12098 return set_die_type (die, type, cu);
0114d602
DJ
12099}
12100
12101/* Read a C++ namespace. */
12102
12103static void
12104read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12105{
12106 struct objfile *objfile = cu->objfile;
0114d602 12107 int is_anonymous;
9219021c 12108
5c4e30ca
DC
12109 /* Add a symbol associated to this if we haven't seen the namespace
12110 before. Also, add a using directive if it's an anonymous
12111 namespace. */
9219021c 12112
f2f0e013 12113 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12114 {
12115 struct type *type;
12116
0114d602 12117 type = read_type_die (die, cu);
e7c27a73 12118 new_symbol (die, type, cu);
5c4e30ca 12119
e8e80198 12120 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12121 if (is_anonymous)
0114d602
DJ
12122 {
12123 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12124
c0cc3a76 12125 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 12126 NULL, NULL, &objfile->objfile_obstack);
0114d602 12127 }
5c4e30ca 12128 }
9219021c 12129
639d11d3 12130 if (die->child != NULL)
d9fa45fe 12131 {
639d11d3 12132 struct die_info *child_die = die->child;
6e70227d 12133
d9fa45fe
DC
12134 while (child_die && child_die->tag)
12135 {
e7c27a73 12136 process_die (child_die, cu);
d9fa45fe
DC
12137 child_die = sibling_die (child_die);
12138 }
12139 }
38d518c9
EZ
12140}
12141
f55ee35c
JK
12142/* Read a Fortran module as type. This DIE can be only a declaration used for
12143 imported module. Still we need that type as local Fortran "use ... only"
12144 declaration imports depend on the created type in determine_prefix. */
12145
12146static struct type *
12147read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12148{
12149 struct objfile *objfile = cu->objfile;
12150 char *module_name;
12151 struct type *type;
12152
12153 module_name = dwarf2_name (die, cu);
12154 if (!module_name)
3e43a32a
MS
12155 complaint (&symfile_complaints,
12156 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12157 die->offset.sect_off);
f55ee35c
JK
12158 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12159
12160 /* determine_prefix uses TYPE_TAG_NAME. */
12161 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12162
12163 return set_die_type (die, type, cu);
12164}
12165
5d7cb8df
JK
12166/* Read a Fortran module. */
12167
12168static void
12169read_module (struct die_info *die, struct dwarf2_cu *cu)
12170{
12171 struct die_info *child_die = die->child;
12172
5d7cb8df
JK
12173 while (child_die && child_die->tag)
12174 {
12175 process_die (child_die, cu);
12176 child_die = sibling_die (child_die);
12177 }
12178}
12179
38d518c9
EZ
12180/* Return the name of the namespace represented by DIE. Set
12181 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12182 namespace. */
12183
12184static const char *
e142c38c 12185namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12186{
12187 struct die_info *current_die;
12188 const char *name = NULL;
12189
12190 /* Loop through the extensions until we find a name. */
12191
12192 for (current_die = die;
12193 current_die != NULL;
f2f0e013 12194 current_die = dwarf2_extension (die, &cu))
38d518c9 12195 {
e142c38c 12196 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12197 if (name != NULL)
12198 break;
12199 }
12200
12201 /* Is it an anonymous namespace? */
12202
12203 *is_anonymous = (name == NULL);
12204 if (*is_anonymous)
2b1dbab0 12205 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12206
12207 return name;
d9fa45fe
DC
12208}
12209
c906108c
SS
12210/* Extract all information from a DW_TAG_pointer_type DIE and add to
12211 the user defined type vector. */
12212
f792889a 12213static struct type *
e7c27a73 12214read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12215{
5e2b427d 12216 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12217 struct comp_unit_head *cu_header = &cu->header;
c906108c 12218 struct type *type;
8b2dbe47
KB
12219 struct attribute *attr_byte_size;
12220 struct attribute *attr_address_class;
12221 int byte_size, addr_class;
7e314c57
JK
12222 struct type *target_type;
12223
12224 target_type = die_type (die, cu);
c906108c 12225
7e314c57
JK
12226 /* The die_type call above may have already set the type for this DIE. */
12227 type = get_die_type (die, cu);
12228 if (type)
12229 return type;
12230
12231 type = lookup_pointer_type (target_type);
8b2dbe47 12232
e142c38c 12233 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12234 if (attr_byte_size)
12235 byte_size = DW_UNSND (attr_byte_size);
c906108c 12236 else
8b2dbe47
KB
12237 byte_size = cu_header->addr_size;
12238
e142c38c 12239 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12240 if (attr_address_class)
12241 addr_class = DW_UNSND (attr_address_class);
12242 else
12243 addr_class = DW_ADDR_none;
12244
12245 /* If the pointer size or address class is different than the
12246 default, create a type variant marked as such and set the
12247 length accordingly. */
12248 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12249 {
5e2b427d 12250 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12251 {
12252 int type_flags;
12253
849957d9 12254 type_flags = gdbarch_address_class_type_flags
5e2b427d 12255 (gdbarch, byte_size, addr_class);
876cecd0
TT
12256 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12257 == 0);
8b2dbe47
KB
12258 type = make_type_with_address_space (type, type_flags);
12259 }
12260 else if (TYPE_LENGTH (type) != byte_size)
12261 {
3e43a32a
MS
12262 complaint (&symfile_complaints,
12263 _("invalid pointer size %d"), byte_size);
8b2dbe47 12264 }
6e70227d 12265 else
9a619af0
MS
12266 {
12267 /* Should we also complain about unhandled address classes? */
12268 }
c906108c 12269 }
8b2dbe47
KB
12270
12271 TYPE_LENGTH (type) = byte_size;
f792889a 12272 return set_die_type (die, type, cu);
c906108c
SS
12273}
12274
12275/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12276 the user defined type vector. */
12277
f792889a 12278static struct type *
e7c27a73 12279read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12280{
12281 struct type *type;
12282 struct type *to_type;
12283 struct type *domain;
12284
e7c27a73
DJ
12285 to_type = die_type (die, cu);
12286 domain = die_containing_type (die, cu);
0d5de010 12287
7e314c57
JK
12288 /* The calls above may have already set the type for this DIE. */
12289 type = get_die_type (die, cu);
12290 if (type)
12291 return type;
12292
0d5de010
DJ
12293 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12294 type = lookup_methodptr_type (to_type);
12295 else
12296 type = lookup_memberptr_type (to_type, domain);
c906108c 12297
f792889a 12298 return set_die_type (die, type, cu);
c906108c
SS
12299}
12300
12301/* Extract all information from a DW_TAG_reference_type DIE and add to
12302 the user defined type vector. */
12303
f792889a 12304static struct type *
e7c27a73 12305read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12306{
e7c27a73 12307 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12308 struct type *type, *target_type;
c906108c
SS
12309 struct attribute *attr;
12310
7e314c57
JK
12311 target_type = die_type (die, cu);
12312
12313 /* The die_type call above may have already set the type for this DIE. */
12314 type = get_die_type (die, cu);
12315 if (type)
12316 return type;
12317
12318 type = lookup_reference_type (target_type);
e142c38c 12319 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12320 if (attr)
12321 {
12322 TYPE_LENGTH (type) = DW_UNSND (attr);
12323 }
12324 else
12325 {
107d2387 12326 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12327 }
f792889a 12328 return set_die_type (die, type, cu);
c906108c
SS
12329}
12330
f792889a 12331static struct type *
e7c27a73 12332read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12333{
f792889a 12334 struct type *base_type, *cv_type;
c906108c 12335
e7c27a73 12336 base_type = die_type (die, cu);
7e314c57
JK
12337
12338 /* The die_type call above may have already set the type for this DIE. */
12339 cv_type = get_die_type (die, cu);
12340 if (cv_type)
12341 return cv_type;
12342
2f608a3a
KW
12343 /* In case the const qualifier is applied to an array type, the element type
12344 is so qualified, not the array type (section 6.7.3 of C99). */
12345 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12346 {
12347 struct type *el_type, *inner_array;
12348
12349 base_type = copy_type (base_type);
12350 inner_array = base_type;
12351
12352 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12353 {
12354 TYPE_TARGET_TYPE (inner_array) =
12355 copy_type (TYPE_TARGET_TYPE (inner_array));
12356 inner_array = TYPE_TARGET_TYPE (inner_array);
12357 }
12358
12359 el_type = TYPE_TARGET_TYPE (inner_array);
12360 TYPE_TARGET_TYPE (inner_array) =
12361 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12362
12363 return set_die_type (die, base_type, cu);
12364 }
12365
f792889a
DJ
12366 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12367 return set_die_type (die, cv_type, cu);
c906108c
SS
12368}
12369
f792889a 12370static struct type *
e7c27a73 12371read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12372{
f792889a 12373 struct type *base_type, *cv_type;
c906108c 12374
e7c27a73 12375 base_type = die_type (die, cu);
7e314c57
JK
12376
12377 /* The die_type call above may have already set the type for this DIE. */
12378 cv_type = get_die_type (die, cu);
12379 if (cv_type)
12380 return cv_type;
12381
f792889a
DJ
12382 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12383 return set_die_type (die, cv_type, cu);
c906108c
SS
12384}
12385
06d66ee9
TT
12386/* Handle DW_TAG_restrict_type. */
12387
12388static struct type *
12389read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12390{
12391 struct type *base_type, *cv_type;
12392
12393 base_type = die_type (die, cu);
12394
12395 /* The die_type call above may have already set the type for this DIE. */
12396 cv_type = get_die_type (die, cu);
12397 if (cv_type)
12398 return cv_type;
12399
12400 cv_type = make_restrict_type (base_type);
12401 return set_die_type (die, cv_type, cu);
12402}
12403
c906108c
SS
12404/* Extract all information from a DW_TAG_string_type DIE and add to
12405 the user defined type vector. It isn't really a user defined type,
12406 but it behaves like one, with other DIE's using an AT_user_def_type
12407 attribute to reference it. */
12408
f792889a 12409static struct type *
e7c27a73 12410read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12411{
e7c27a73 12412 struct objfile *objfile = cu->objfile;
3b7538c0 12413 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12414 struct type *type, *range_type, *index_type, *char_type;
12415 struct attribute *attr;
12416 unsigned int length;
12417
e142c38c 12418 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12419 if (attr)
12420 {
12421 length = DW_UNSND (attr);
12422 }
12423 else
12424 {
0963b4bd 12425 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12426 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12427 if (attr)
12428 {
12429 length = DW_UNSND (attr);
12430 }
12431 else
12432 {
12433 length = 1;
12434 }
c906108c 12435 }
6ccb9162 12436
46bf5051 12437 index_type = objfile_type (objfile)->builtin_int;
c906108c 12438 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12439 char_type = language_string_char_type (cu->language_defn, gdbarch);
12440 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12441
f792889a 12442 return set_die_type (die, type, cu);
c906108c
SS
12443}
12444
12445/* Handle DIES due to C code like:
12446
12447 struct foo
c5aa993b
JM
12448 {
12449 int (*funcp)(int a, long l);
12450 int b;
12451 };
c906108c 12452
0963b4bd 12453 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12454
f792889a 12455static struct type *
e7c27a73 12456read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12457{
bb5ed363 12458 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12459 struct type *type; /* Type that this function returns. */
12460 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12461 struct attribute *attr;
12462
e7c27a73 12463 type = die_type (die, cu);
7e314c57
JK
12464
12465 /* The die_type call above may have already set the type for this DIE. */
12466 ftype = get_die_type (die, cu);
12467 if (ftype)
12468 return ftype;
12469
0c8b41f1 12470 ftype = lookup_function_type (type);
c906108c 12471
5b8101ae 12472 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12473 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12474 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12475 || cu->language == language_cplus
5b8101ae
PM
12476 || cu->language == language_java
12477 || cu->language == language_pascal)
876cecd0 12478 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12479 else if (producer_is_realview (cu->producer))
12480 /* RealView does not emit DW_AT_prototyped. We can not
12481 distinguish prototyped and unprototyped functions; default to
12482 prototyped, since that is more common in modern code (and
12483 RealView warns about unprototyped functions). */
12484 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12485
c055b101
CV
12486 /* Store the calling convention in the type if it's available in
12487 the subroutine die. Otherwise set the calling convention to
12488 the default value DW_CC_normal. */
12489 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12490 if (attr)
12491 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12492 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12493 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12494 else
12495 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12496
12497 /* We need to add the subroutine type to the die immediately so
12498 we don't infinitely recurse when dealing with parameters
0963b4bd 12499 declared as the same subroutine type. */
76c10ea2 12500 set_die_type (die, ftype, cu);
6e70227d 12501
639d11d3 12502 if (die->child != NULL)
c906108c 12503 {
bb5ed363 12504 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12505 struct die_info *child_die;
8072405b 12506 int nparams, iparams;
c906108c
SS
12507
12508 /* Count the number of parameters.
12509 FIXME: GDB currently ignores vararg functions, but knows about
12510 vararg member functions. */
8072405b 12511 nparams = 0;
639d11d3 12512 child_die = die->child;
c906108c
SS
12513 while (child_die && child_die->tag)
12514 {
12515 if (child_die->tag == DW_TAG_formal_parameter)
12516 nparams++;
12517 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12518 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12519 child_die = sibling_die (child_die);
12520 }
12521
12522 /* Allocate storage for parameters and fill them in. */
12523 TYPE_NFIELDS (ftype) = nparams;
12524 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12525 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12526
8072405b
JK
12527 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12528 even if we error out during the parameters reading below. */
12529 for (iparams = 0; iparams < nparams; iparams++)
12530 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12531
12532 iparams = 0;
639d11d3 12533 child_die = die->child;
c906108c
SS
12534 while (child_die && child_die->tag)
12535 {
12536 if (child_die->tag == DW_TAG_formal_parameter)
12537 {
3ce3b1ba
PA
12538 struct type *arg_type;
12539
12540 /* DWARF version 2 has no clean way to discern C++
12541 static and non-static member functions. G++ helps
12542 GDB by marking the first parameter for non-static
12543 member functions (which is the this pointer) as
12544 artificial. We pass this information to
12545 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12546
12547 DWARF version 3 added DW_AT_object_pointer, which GCC
12548 4.5 does not yet generate. */
e142c38c 12549 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12550 if (attr)
12551 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12552 else
418835cc
KS
12553 {
12554 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12555
12556 /* GCC/43521: In java, the formal parameter
12557 "this" is sometimes not marked with DW_AT_artificial. */
12558 if (cu->language == language_java)
12559 {
12560 const char *name = dwarf2_name (child_die, cu);
9a619af0 12561
418835cc
KS
12562 if (name && !strcmp (name, "this"))
12563 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12564 }
12565 }
3ce3b1ba
PA
12566 arg_type = die_type (child_die, cu);
12567
12568 /* RealView does not mark THIS as const, which the testsuite
12569 expects. GCC marks THIS as const in method definitions,
12570 but not in the class specifications (GCC PR 43053). */
12571 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12572 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12573 {
12574 int is_this = 0;
12575 struct dwarf2_cu *arg_cu = cu;
12576 const char *name = dwarf2_name (child_die, cu);
12577
12578 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12579 if (attr)
12580 {
12581 /* If the compiler emits this, use it. */
12582 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12583 is_this = 1;
12584 }
12585 else if (name && strcmp (name, "this") == 0)
12586 /* Function definitions will have the argument names. */
12587 is_this = 1;
12588 else if (name == NULL && iparams == 0)
12589 /* Declarations may not have the names, so like
12590 elsewhere in GDB, assume an artificial first
12591 argument is "this". */
12592 is_this = 1;
12593
12594 if (is_this)
12595 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12596 arg_type, 0);
12597 }
12598
12599 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12600 iparams++;
12601 }
12602 child_die = sibling_die (child_die);
12603 }
12604 }
12605
76c10ea2 12606 return ftype;
c906108c
SS
12607}
12608
f792889a 12609static struct type *
e7c27a73 12610read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12611{
e7c27a73 12612 struct objfile *objfile = cu->objfile;
0114d602 12613 const char *name = NULL;
3c8e0968 12614 struct type *this_type, *target_type;
c906108c 12615
94af9270 12616 name = dwarf2_full_name (NULL, die, cu);
f792889a 12617 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12618 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12619 TYPE_NAME (this_type) = (char *) name;
f792889a 12620 set_die_type (die, this_type, cu);
3c8e0968
DE
12621 target_type = die_type (die, cu);
12622 if (target_type != this_type)
12623 TYPE_TARGET_TYPE (this_type) = target_type;
12624 else
12625 {
12626 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12627 spec and cause infinite loops in GDB. */
12628 complaint (&symfile_complaints,
12629 _("Self-referential DW_TAG_typedef "
12630 "- DIE at 0x%x [in module %s]"),
b64f50a1 12631 die->offset.sect_off, objfile->name);
3c8e0968
DE
12632 TYPE_TARGET_TYPE (this_type) = NULL;
12633 }
f792889a 12634 return this_type;
c906108c
SS
12635}
12636
12637/* Find a representation of a given base type and install
12638 it in the TYPE field of the die. */
12639
f792889a 12640static struct type *
e7c27a73 12641read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12642{
e7c27a73 12643 struct objfile *objfile = cu->objfile;
c906108c
SS
12644 struct type *type;
12645 struct attribute *attr;
12646 int encoding = 0, size = 0;
39cbfefa 12647 char *name;
6ccb9162
UW
12648 enum type_code code = TYPE_CODE_INT;
12649 int type_flags = 0;
12650 struct type *target_type = NULL;
c906108c 12651
e142c38c 12652 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12653 if (attr)
12654 {
12655 encoding = DW_UNSND (attr);
12656 }
e142c38c 12657 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12658 if (attr)
12659 {
12660 size = DW_UNSND (attr);
12661 }
39cbfefa 12662 name = dwarf2_name (die, cu);
6ccb9162 12663 if (!name)
c906108c 12664 {
6ccb9162
UW
12665 complaint (&symfile_complaints,
12666 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12667 }
6ccb9162
UW
12668
12669 switch (encoding)
c906108c 12670 {
6ccb9162
UW
12671 case DW_ATE_address:
12672 /* Turn DW_ATE_address into a void * pointer. */
12673 code = TYPE_CODE_PTR;
12674 type_flags |= TYPE_FLAG_UNSIGNED;
12675 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12676 break;
12677 case DW_ATE_boolean:
12678 code = TYPE_CODE_BOOL;
12679 type_flags |= TYPE_FLAG_UNSIGNED;
12680 break;
12681 case DW_ATE_complex_float:
12682 code = TYPE_CODE_COMPLEX;
12683 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12684 break;
12685 case DW_ATE_decimal_float:
12686 code = TYPE_CODE_DECFLOAT;
12687 break;
12688 case DW_ATE_float:
12689 code = TYPE_CODE_FLT;
12690 break;
12691 case DW_ATE_signed:
12692 break;
12693 case DW_ATE_unsigned:
12694 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12695 if (cu->language == language_fortran
12696 && name
12697 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12698 code = TYPE_CODE_CHAR;
6ccb9162
UW
12699 break;
12700 case DW_ATE_signed_char:
6e70227d 12701 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12702 || cu->language == language_pascal
12703 || cu->language == language_fortran)
6ccb9162
UW
12704 code = TYPE_CODE_CHAR;
12705 break;
12706 case DW_ATE_unsigned_char:
868a0084 12707 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12708 || cu->language == language_pascal
12709 || cu->language == language_fortran)
6ccb9162
UW
12710 code = TYPE_CODE_CHAR;
12711 type_flags |= TYPE_FLAG_UNSIGNED;
12712 break;
75079b2b
TT
12713 case DW_ATE_UTF:
12714 /* We just treat this as an integer and then recognize the
12715 type by name elsewhere. */
12716 break;
12717
6ccb9162
UW
12718 default:
12719 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12720 dwarf_type_encoding_name (encoding));
12721 break;
c906108c 12722 }
6ccb9162 12723
0114d602
DJ
12724 type = init_type (code, size, type_flags, NULL, objfile);
12725 TYPE_NAME (type) = name;
6ccb9162
UW
12726 TYPE_TARGET_TYPE (type) = target_type;
12727
0114d602 12728 if (name && strcmp (name, "char") == 0)
876cecd0 12729 TYPE_NOSIGN (type) = 1;
0114d602 12730
f792889a 12731 return set_die_type (die, type, cu);
c906108c
SS
12732}
12733
a02abb62
JB
12734/* Read the given DW_AT_subrange DIE. */
12735
f792889a 12736static struct type *
a02abb62
JB
12737read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12738{
12739 struct type *base_type;
12740 struct type *range_type;
12741 struct attribute *attr;
4fae6e18
JK
12742 LONGEST low, high;
12743 int low_default_is_valid;
39cbfefa 12744 char *name;
43bbcdc2 12745 LONGEST negative_mask;
e77813c8 12746
a02abb62 12747 base_type = die_type (die, cu);
953ac07e
JK
12748 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12749 check_typedef (base_type);
a02abb62 12750
7e314c57
JK
12751 /* The die_type call above may have already set the type for this DIE. */
12752 range_type = get_die_type (die, cu);
12753 if (range_type)
12754 return range_type;
12755
4fae6e18
JK
12756 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12757 omitting DW_AT_lower_bound. */
12758 switch (cu->language)
6e70227d 12759 {
4fae6e18
JK
12760 case language_c:
12761 case language_cplus:
12762 low = 0;
12763 low_default_is_valid = 1;
12764 break;
12765 case language_fortran:
12766 low = 1;
12767 low_default_is_valid = 1;
12768 break;
12769 case language_d:
12770 case language_java:
12771 case language_objc:
12772 low = 0;
12773 low_default_is_valid = (cu->header.version >= 4);
12774 break;
12775 case language_ada:
12776 case language_m2:
12777 case language_pascal:
a02abb62 12778 low = 1;
4fae6e18
JK
12779 low_default_is_valid = (cu->header.version >= 4);
12780 break;
12781 default:
12782 low = 0;
12783 low_default_is_valid = 0;
12784 break;
a02abb62
JB
12785 }
12786
dd5e6932
DJ
12787 /* FIXME: For variable sized arrays either of these could be
12788 a variable rather than a constant value. We'll allow it,
12789 but we don't know how to handle it. */
e142c38c 12790 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12791 if (attr)
4fae6e18
JK
12792 low = dwarf2_get_attr_constant_value (attr, low);
12793 else if (!low_default_is_valid)
12794 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12795 "- DIE at 0x%x [in module %s]"),
12796 die->offset.sect_off, cu->objfile->name);
a02abb62 12797
e142c38c 12798 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12799 if (attr)
6e70227d 12800 {
d48323d8 12801 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12802 {
12803 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12804 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12805 FIXME: GDB does not yet know how to handle dynamic
12806 arrays properly, treat them as arrays with unspecified
12807 length for now.
12808
12809 FIXME: jimb/2003-09-22: GDB does not really know
12810 how to handle arrays of unspecified length
12811 either; we just represent them as zero-length
12812 arrays. Choose an appropriate upper bound given
12813 the lower bound we've computed above. */
12814 high = low - 1;
12815 }
12816 else
12817 high = dwarf2_get_attr_constant_value (attr, 1);
12818 }
e77813c8
PM
12819 else
12820 {
12821 attr = dwarf2_attr (die, DW_AT_count, cu);
12822 if (attr)
12823 {
12824 int count = dwarf2_get_attr_constant_value (attr, 1);
12825 high = low + count - 1;
12826 }
c2ff108b
JK
12827 else
12828 {
12829 /* Unspecified array length. */
12830 high = low - 1;
12831 }
e77813c8
PM
12832 }
12833
12834 /* Dwarf-2 specifications explicitly allows to create subrange types
12835 without specifying a base type.
12836 In that case, the base type must be set to the type of
12837 the lower bound, upper bound or count, in that order, if any of these
12838 three attributes references an object that has a type.
12839 If no base type is found, the Dwarf-2 specifications say that
12840 a signed integer type of size equal to the size of an address should
12841 be used.
12842 For the following C code: `extern char gdb_int [];'
12843 GCC produces an empty range DIE.
12844 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12845 high bound or count are not yet handled by this code. */
e77813c8
PM
12846 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12847 {
12848 struct objfile *objfile = cu->objfile;
12849 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12850 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12851 struct type *int_type = objfile_type (objfile)->builtin_int;
12852
12853 /* Test "int", "long int", and "long long int" objfile types,
12854 and select the first one having a size above or equal to the
12855 architecture address size. */
12856 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12857 base_type = int_type;
12858 else
12859 {
12860 int_type = objfile_type (objfile)->builtin_long;
12861 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12862 base_type = int_type;
12863 else
12864 {
12865 int_type = objfile_type (objfile)->builtin_long_long;
12866 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12867 base_type = int_type;
12868 }
12869 }
12870 }
a02abb62 12871
6e70227d 12872 negative_mask =
43bbcdc2
PH
12873 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12874 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12875 low |= negative_mask;
12876 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12877 high |= negative_mask;
12878
a02abb62
JB
12879 range_type = create_range_type (NULL, base_type, low, high);
12880
bbb0eef6
JK
12881 /* Mark arrays with dynamic length at least as an array of unspecified
12882 length. GDB could check the boundary but before it gets implemented at
12883 least allow accessing the array elements. */
d48323d8 12884 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12885 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12886
c2ff108b
JK
12887 /* Ada expects an empty array on no boundary attributes. */
12888 if (attr == NULL && cu->language != language_ada)
12889 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12890
39cbfefa
DJ
12891 name = dwarf2_name (die, cu);
12892 if (name)
12893 TYPE_NAME (range_type) = name;
6e70227d 12894
e142c38c 12895 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12896 if (attr)
12897 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12898
7e314c57
JK
12899 set_die_type (die, range_type, cu);
12900
12901 /* set_die_type should be already done. */
b4ba55a1
JB
12902 set_descriptive_type (range_type, die, cu);
12903
7e314c57 12904 return range_type;
a02abb62 12905}
6e70227d 12906
f792889a 12907static struct type *
81a17f79
JB
12908read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12909{
12910 struct type *type;
81a17f79 12911
81a17f79
JB
12912 /* For now, we only support the C meaning of an unspecified type: void. */
12913
0114d602
DJ
12914 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12915 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12916
f792889a 12917 return set_die_type (die, type, cu);
81a17f79 12918}
a02abb62 12919
639d11d3
DC
12920/* Read a single die and all its descendents. Set the die's sibling
12921 field to NULL; set other fields in the die correctly, and set all
12922 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12923 location of the info_ptr after reading all of those dies. PARENT
12924 is the parent of the die in question. */
12925
12926static struct die_info *
dee91e82
DE
12927read_die_and_children (const struct die_reader_specs *reader,
12928 gdb_byte *info_ptr,
12929 gdb_byte **new_info_ptr,
12930 struct die_info *parent)
639d11d3
DC
12931{
12932 struct die_info *die;
fe1b8b76 12933 gdb_byte *cur_ptr;
639d11d3
DC
12934 int has_children;
12935
93311388 12936 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12937 if (die == NULL)
12938 {
12939 *new_info_ptr = cur_ptr;
12940 return NULL;
12941 }
93311388 12942 store_in_ref_table (die, reader->cu);
639d11d3
DC
12943
12944 if (has_children)
348e048f 12945 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12946 else
12947 {
12948 die->child = NULL;
12949 *new_info_ptr = cur_ptr;
12950 }
12951
12952 die->sibling = NULL;
12953 die->parent = parent;
12954 return die;
12955}
12956
12957/* Read a die, all of its descendents, and all of its siblings; set
12958 all of the fields of all of the dies correctly. Arguments are as
12959 in read_die_and_children. */
12960
12961static struct die_info *
93311388
DE
12962read_die_and_siblings (const struct die_reader_specs *reader,
12963 gdb_byte *info_ptr,
fe1b8b76 12964 gdb_byte **new_info_ptr,
639d11d3
DC
12965 struct die_info *parent)
12966{
12967 struct die_info *first_die, *last_sibling;
fe1b8b76 12968 gdb_byte *cur_ptr;
639d11d3 12969
c906108c 12970 cur_ptr = info_ptr;
639d11d3
DC
12971 first_die = last_sibling = NULL;
12972
12973 while (1)
c906108c 12974 {
639d11d3 12975 struct die_info *die
dee91e82 12976 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12977
1d325ec1 12978 if (die == NULL)
c906108c 12979 {
639d11d3
DC
12980 *new_info_ptr = cur_ptr;
12981 return first_die;
c906108c 12982 }
1d325ec1
DJ
12983
12984 if (!first_die)
12985 first_die = die;
c906108c 12986 else
1d325ec1
DJ
12987 last_sibling->sibling = die;
12988
12989 last_sibling = die;
c906108c 12990 }
c906108c
SS
12991}
12992
3019eac3
DE
12993/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12994 attributes.
12995 The caller is responsible for filling in the extra attributes
12996 and updating (*DIEP)->num_attrs.
12997 Set DIEP to point to a newly allocated die with its information,
12998 except for its child, sibling, and parent fields.
12999 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
13000
13001static gdb_byte *
3019eac3
DE
13002read_full_die_1 (const struct die_reader_specs *reader,
13003 struct die_info **diep, gdb_byte *info_ptr,
13004 int *has_children, int num_extra_attrs)
93311388 13005{
b64f50a1
JK
13006 unsigned int abbrev_number, bytes_read, i;
13007 sect_offset offset;
93311388
DE
13008 struct abbrev_info *abbrev;
13009 struct die_info *die;
13010 struct dwarf2_cu *cu = reader->cu;
13011 bfd *abfd = reader->abfd;
13012
b64f50a1 13013 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13014 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13015 info_ptr += bytes_read;
13016 if (!abbrev_number)
13017 {
13018 *diep = NULL;
13019 *has_children = 0;
13020 return info_ptr;
13021 }
13022
433df2d4 13023 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13024 if (!abbrev)
348e048f
DE
13025 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13026 abbrev_number,
13027 bfd_get_filename (abfd));
13028
3019eac3 13029 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13030 die->offset = offset;
13031 die->tag = abbrev->tag;
13032 die->abbrev = abbrev_number;
13033
3019eac3
DE
13034 /* Make the result usable.
13035 The caller needs to update num_attrs after adding the extra
13036 attributes. */
93311388
DE
13037 die->num_attrs = abbrev->num_attrs;
13038
13039 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13040 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13041 info_ptr);
93311388
DE
13042
13043 *diep = die;
13044 *has_children = abbrev->has_children;
13045 return info_ptr;
13046}
13047
3019eac3
DE
13048/* Read a die and all its attributes.
13049 Set DIEP to point to a newly allocated die with its information,
13050 except for its child, sibling, and parent fields.
13051 Set HAS_CHILDREN to tell whether the die has children or not. */
13052
13053static gdb_byte *
13054read_full_die (const struct die_reader_specs *reader,
13055 struct die_info **diep, gdb_byte *info_ptr,
13056 int *has_children)
13057{
13058 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13059}
433df2d4
DE
13060\f
13061/* Abbreviation tables.
3019eac3 13062
433df2d4 13063 In DWARF version 2, the description of the debugging information is
c906108c
SS
13064 stored in a separate .debug_abbrev section. Before we read any
13065 dies from a section we read in all abbreviations and install them
433df2d4
DE
13066 in a hash table. */
13067
13068/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13069
13070static struct abbrev_info *
13071abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13072{
13073 struct abbrev_info *abbrev;
13074
13075 abbrev = (struct abbrev_info *)
13076 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13077 memset (abbrev, 0, sizeof (struct abbrev_info));
13078 return abbrev;
13079}
13080
13081/* Add an abbreviation to the table. */
c906108c
SS
13082
13083static void
433df2d4
DE
13084abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13085 unsigned int abbrev_number,
13086 struct abbrev_info *abbrev)
13087{
13088 unsigned int hash_number;
13089
13090 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13091 abbrev->next = abbrev_table->abbrevs[hash_number];
13092 abbrev_table->abbrevs[hash_number] = abbrev;
13093}
dee91e82 13094
433df2d4
DE
13095/* Look up an abbrev in the table.
13096 Returns NULL if the abbrev is not found. */
13097
13098static struct abbrev_info *
13099abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13100 unsigned int abbrev_number)
c906108c 13101{
433df2d4
DE
13102 unsigned int hash_number;
13103 struct abbrev_info *abbrev;
13104
13105 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13106 abbrev = abbrev_table->abbrevs[hash_number];
13107
13108 while (abbrev)
13109 {
13110 if (abbrev->number == abbrev_number)
13111 return abbrev;
13112 abbrev = abbrev->next;
13113 }
13114 return NULL;
13115}
13116
13117/* Read in an abbrev table. */
13118
13119static struct abbrev_table *
13120abbrev_table_read_table (struct dwarf2_section_info *section,
13121 sect_offset offset)
13122{
13123 struct objfile *objfile = dwarf2_per_objfile->objfile;
13124 bfd *abfd = section->asection->owner;
13125 struct abbrev_table *abbrev_table;
fe1b8b76 13126 gdb_byte *abbrev_ptr;
c906108c
SS
13127 struct abbrev_info *cur_abbrev;
13128 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13129 unsigned int abbrev_form;
f3dd6933
DJ
13130 struct attr_abbrev *cur_attrs;
13131 unsigned int allocated_attrs;
c906108c 13132
433df2d4 13133 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13134 abbrev_table->offset = offset;
433df2d4
DE
13135 obstack_init (&abbrev_table->abbrev_obstack);
13136 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13137 (ABBREV_HASH_SIZE
13138 * sizeof (struct abbrev_info *)));
13139 memset (abbrev_table->abbrevs, 0,
13140 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13141
433df2d4
DE
13142 dwarf2_read_section (objfile, section);
13143 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13144 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13145 abbrev_ptr += bytes_read;
13146
f3dd6933
DJ
13147 allocated_attrs = ATTR_ALLOC_CHUNK;
13148 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13149
0963b4bd 13150 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13151 while (abbrev_number)
13152 {
433df2d4 13153 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13154
13155 /* read in abbrev header */
13156 cur_abbrev->number = abbrev_number;
13157 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13158 abbrev_ptr += bytes_read;
13159 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13160 abbrev_ptr += 1;
13161
13162 /* now read in declarations */
13163 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13164 abbrev_ptr += bytes_read;
13165 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13166 abbrev_ptr += bytes_read;
13167 while (abbrev_name)
13168 {
f3dd6933 13169 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13170 {
f3dd6933
DJ
13171 allocated_attrs += ATTR_ALLOC_CHUNK;
13172 cur_attrs
13173 = xrealloc (cur_attrs, (allocated_attrs
13174 * sizeof (struct attr_abbrev)));
c906108c 13175 }
ae038cb0 13176
f3dd6933
DJ
13177 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13178 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13179 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13180 abbrev_ptr += bytes_read;
13181 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13182 abbrev_ptr += bytes_read;
13183 }
13184
433df2d4 13185 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13186 (cur_abbrev->num_attrs
13187 * sizeof (struct attr_abbrev)));
13188 memcpy (cur_abbrev->attrs, cur_attrs,
13189 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13190
433df2d4 13191 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13192
13193 /* Get next abbreviation.
13194 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13195 always properly terminated with an abbrev number of 0.
13196 Exit loop if we encounter an abbreviation which we have
13197 already read (which means we are about to read the abbreviations
13198 for the next compile unit) or if the end of the abbreviation
13199 table is reached. */
433df2d4 13200 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13201 break;
13202 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13203 abbrev_ptr += bytes_read;
433df2d4 13204 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13205 break;
13206 }
f3dd6933
DJ
13207
13208 xfree (cur_attrs);
433df2d4 13209 return abbrev_table;
c906108c
SS
13210}
13211
433df2d4 13212/* Free the resources held by ABBREV_TABLE. */
c906108c 13213
c906108c 13214static void
433df2d4 13215abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13216{
433df2d4
DE
13217 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13218 xfree (abbrev_table);
c906108c
SS
13219}
13220
f4dc4d17
DE
13221/* Same as abbrev_table_free but as a cleanup.
13222 We pass in a pointer to the pointer to the table so that we can
13223 set the pointer to NULL when we're done. It also simplifies
13224 build_type_unit_groups. */
13225
13226static void
13227abbrev_table_free_cleanup (void *table_ptr)
13228{
13229 struct abbrev_table **abbrev_table_ptr = table_ptr;
13230
13231 if (*abbrev_table_ptr != NULL)
13232 abbrev_table_free (*abbrev_table_ptr);
13233 *abbrev_table_ptr = NULL;
13234}
13235
433df2d4
DE
13236/* Read the abbrev table for CU from ABBREV_SECTION. */
13237
13238static void
13239dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13240 struct dwarf2_section_info *abbrev_section)
c906108c 13241{
433df2d4
DE
13242 cu->abbrev_table =
13243 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13244}
c906108c 13245
433df2d4 13246/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13247
433df2d4
DE
13248static void
13249dwarf2_free_abbrev_table (void *ptr_to_cu)
13250{
13251 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13252
433df2d4
DE
13253 abbrev_table_free (cu->abbrev_table);
13254 /* Set this to NULL so that we SEGV if we try to read it later,
13255 and also because free_comp_unit verifies this is NULL. */
13256 cu->abbrev_table = NULL;
13257}
13258\f
72bf9492
DJ
13259/* Returns nonzero if TAG represents a type that we might generate a partial
13260 symbol for. */
13261
13262static int
13263is_type_tag_for_partial (int tag)
13264{
13265 switch (tag)
13266 {
13267#if 0
13268 /* Some types that would be reasonable to generate partial symbols for,
13269 that we don't at present. */
13270 case DW_TAG_array_type:
13271 case DW_TAG_file_type:
13272 case DW_TAG_ptr_to_member_type:
13273 case DW_TAG_set_type:
13274 case DW_TAG_string_type:
13275 case DW_TAG_subroutine_type:
13276#endif
13277 case DW_TAG_base_type:
13278 case DW_TAG_class_type:
680b30c7 13279 case DW_TAG_interface_type:
72bf9492
DJ
13280 case DW_TAG_enumeration_type:
13281 case DW_TAG_structure_type:
13282 case DW_TAG_subrange_type:
13283 case DW_TAG_typedef:
13284 case DW_TAG_union_type:
13285 return 1;
13286 default:
13287 return 0;
13288 }
13289}
13290
13291/* Load all DIEs that are interesting for partial symbols into memory. */
13292
13293static struct partial_die_info *
dee91e82
DE
13294load_partial_dies (const struct die_reader_specs *reader,
13295 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13296{
dee91e82 13297 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13298 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13299 struct partial_die_info *part_die;
13300 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13301 struct abbrev_info *abbrev;
13302 unsigned int bytes_read;
5afb4e99 13303 unsigned int load_all = 0;
72bf9492
DJ
13304 int nesting_level = 1;
13305
13306 parent_die = NULL;
13307 last_die = NULL;
13308
7adf1e79
DE
13309 gdb_assert (cu->per_cu != NULL);
13310 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13311 load_all = 1;
13312
72bf9492
DJ
13313 cu->partial_dies
13314 = htab_create_alloc_ex (cu->header.length / 12,
13315 partial_die_hash,
13316 partial_die_eq,
13317 NULL,
13318 &cu->comp_unit_obstack,
13319 hashtab_obstack_allocate,
13320 dummy_obstack_deallocate);
13321
13322 part_die = obstack_alloc (&cu->comp_unit_obstack,
13323 sizeof (struct partial_die_info));
13324
13325 while (1)
13326 {
13327 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13328
13329 /* A NULL abbrev means the end of a series of children. */
13330 if (abbrev == NULL)
13331 {
13332 if (--nesting_level == 0)
13333 {
13334 /* PART_DIE was probably the last thing allocated on the
13335 comp_unit_obstack, so we could call obstack_free
13336 here. We don't do that because the waste is small,
13337 and will be cleaned up when we're done with this
13338 compilation unit. This way, we're also more robust
13339 against other users of the comp_unit_obstack. */
13340 return first_die;
13341 }
13342 info_ptr += bytes_read;
13343 last_die = parent_die;
13344 parent_die = parent_die->die_parent;
13345 continue;
13346 }
13347
98bfdba5
PA
13348 /* Check for template arguments. We never save these; if
13349 they're seen, we just mark the parent, and go on our way. */
13350 if (parent_die != NULL
13351 && cu->language == language_cplus
13352 && (abbrev->tag == DW_TAG_template_type_param
13353 || abbrev->tag == DW_TAG_template_value_param))
13354 {
13355 parent_die->has_template_arguments = 1;
13356
13357 if (!load_all)
13358 {
13359 /* We don't need a partial DIE for the template argument. */
dee91e82 13360 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13361 continue;
13362 }
13363 }
13364
0d99eb77 13365 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13366 Skip their other children. */
13367 if (!load_all
13368 && cu->language == language_cplus
13369 && parent_die != NULL
13370 && parent_die->tag == DW_TAG_subprogram)
13371 {
dee91e82 13372 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13373 continue;
13374 }
13375
5afb4e99
DJ
13376 /* Check whether this DIE is interesting enough to save. Normally
13377 we would not be interested in members here, but there may be
13378 later variables referencing them via DW_AT_specification (for
13379 static members). */
13380 if (!load_all
13381 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13382 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13383 && abbrev->tag != DW_TAG_enumerator
13384 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13385 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13386 && abbrev->tag != DW_TAG_variable
5afb4e99 13387 && abbrev->tag != DW_TAG_namespace
f55ee35c 13388 && abbrev->tag != DW_TAG_module
95554aad
TT
13389 && abbrev->tag != DW_TAG_member
13390 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13391 {
13392 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13393 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13394 continue;
13395 }
13396
dee91e82
DE
13397 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13398 info_ptr);
72bf9492
DJ
13399
13400 /* This two-pass algorithm for processing partial symbols has a
13401 high cost in cache pressure. Thus, handle some simple cases
13402 here which cover the majority of C partial symbols. DIEs
13403 which neither have specification tags in them, nor could have
13404 specification tags elsewhere pointing at them, can simply be
13405 processed and discarded.
13406
13407 This segment is also optional; scan_partial_symbols and
13408 add_partial_symbol will handle these DIEs if we chain
13409 them in normally. When compilers which do not emit large
13410 quantities of duplicate debug information are more common,
13411 this code can probably be removed. */
13412
13413 /* Any complete simple types at the top level (pretty much all
13414 of them, for a language without namespaces), can be processed
13415 directly. */
13416 if (parent_die == NULL
13417 && part_die->has_specification == 0
13418 && part_die->is_declaration == 0
d8228535 13419 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13420 || part_die->tag == DW_TAG_base_type
13421 || part_die->tag == DW_TAG_subrange_type))
13422 {
13423 if (building_psymtab && part_die->name != NULL)
04a679b8 13424 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13425 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13426 &objfile->static_psymbols,
13427 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13428 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13429 continue;
13430 }
13431
d8228535
JK
13432 /* The exception for DW_TAG_typedef with has_children above is
13433 a workaround of GCC PR debug/47510. In the case of this complaint
13434 type_name_no_tag_or_error will error on such types later.
13435
13436 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13437 it could not find the child DIEs referenced later, this is checked
13438 above. In correct DWARF DW_TAG_typedef should have no children. */
13439
13440 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13441 complaint (&symfile_complaints,
13442 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13443 "- DIE at 0x%x [in module %s]"),
b64f50a1 13444 part_die->offset.sect_off, objfile->name);
d8228535 13445
72bf9492
DJ
13446 /* If we're at the second level, and we're an enumerator, and
13447 our parent has no specification (meaning possibly lives in a
13448 namespace elsewhere), then we can add the partial symbol now
13449 instead of queueing it. */
13450 if (part_die->tag == DW_TAG_enumerator
13451 && parent_die != NULL
13452 && parent_die->die_parent == NULL
13453 && parent_die->tag == DW_TAG_enumeration_type
13454 && parent_die->has_specification == 0)
13455 {
13456 if (part_die->name == NULL)
3e43a32a
MS
13457 complaint (&symfile_complaints,
13458 _("malformed enumerator DIE ignored"));
72bf9492 13459 else if (building_psymtab)
04a679b8 13460 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13461 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13462 (cu->language == language_cplus
13463 || cu->language == language_java)
bb5ed363
DE
13464 ? &objfile->global_psymbols
13465 : &objfile->static_psymbols,
13466 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13467
dee91e82 13468 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13469 continue;
13470 }
13471
13472 /* We'll save this DIE so link it in. */
13473 part_die->die_parent = parent_die;
13474 part_die->die_sibling = NULL;
13475 part_die->die_child = NULL;
13476
13477 if (last_die && last_die == parent_die)
13478 last_die->die_child = part_die;
13479 else if (last_die)
13480 last_die->die_sibling = part_die;
13481
13482 last_die = part_die;
13483
13484 if (first_die == NULL)
13485 first_die = part_die;
13486
13487 /* Maybe add the DIE to the hash table. Not all DIEs that we
13488 find interesting need to be in the hash table, because we
13489 also have the parent/sibling/child chains; only those that we
13490 might refer to by offset later during partial symbol reading.
13491
13492 For now this means things that might have be the target of a
13493 DW_AT_specification, DW_AT_abstract_origin, or
13494 DW_AT_extension. DW_AT_extension will refer only to
13495 namespaces; DW_AT_abstract_origin refers to functions (and
13496 many things under the function DIE, but we do not recurse
13497 into function DIEs during partial symbol reading) and
13498 possibly variables as well; DW_AT_specification refers to
13499 declarations. Declarations ought to have the DW_AT_declaration
13500 flag. It happens that GCC forgets to put it in sometimes, but
13501 only for functions, not for types.
13502
13503 Adding more things than necessary to the hash table is harmless
13504 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13505 wasted time in find_partial_die, when we reread the compilation
13506 unit with load_all_dies set. */
72bf9492 13507
5afb4e99 13508 if (load_all
72929c62 13509 || abbrev->tag == DW_TAG_constant
5afb4e99 13510 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13511 || abbrev->tag == DW_TAG_variable
13512 || abbrev->tag == DW_TAG_namespace
13513 || part_die->is_declaration)
13514 {
13515 void **slot;
13516
13517 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13518 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13519 *slot = part_die;
13520 }
13521
13522 part_die = obstack_alloc (&cu->comp_unit_obstack,
13523 sizeof (struct partial_die_info));
13524
13525 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13526 we have no reason to follow the children of structures; for other
98bfdba5
PA
13527 languages we have to, so that we can get at method physnames
13528 to infer fully qualified class names, for DW_AT_specification,
13529 and for C++ template arguments. For C++, we also look one level
13530 inside functions to find template arguments (if the name of the
13531 function does not already contain the template arguments).
bc30ff58
JB
13532
13533 For Ada, we need to scan the children of subprograms and lexical
13534 blocks as well because Ada allows the definition of nested
13535 entities that could be interesting for the debugger, such as
13536 nested subprograms for instance. */
72bf9492 13537 if (last_die->has_children
5afb4e99
DJ
13538 && (load_all
13539 || last_die->tag == DW_TAG_namespace
f55ee35c 13540 || last_die->tag == DW_TAG_module
72bf9492 13541 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13542 || (cu->language == language_cplus
13543 && last_die->tag == DW_TAG_subprogram
13544 && (last_die->name == NULL
13545 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13546 || (cu->language != language_c
13547 && (last_die->tag == DW_TAG_class_type
680b30c7 13548 || last_die->tag == DW_TAG_interface_type
72bf9492 13549 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13550 || last_die->tag == DW_TAG_union_type))
13551 || (cu->language == language_ada
13552 && (last_die->tag == DW_TAG_subprogram
13553 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13554 {
13555 nesting_level++;
13556 parent_die = last_die;
13557 continue;
13558 }
13559
13560 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13561 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13562
13563 /* Back to the top, do it again. */
13564 }
13565}
13566
c906108c
SS
13567/* Read a minimal amount of information into the minimal die structure. */
13568
fe1b8b76 13569static gdb_byte *
dee91e82
DE
13570read_partial_die (const struct die_reader_specs *reader,
13571 struct partial_die_info *part_die,
13572 struct abbrev_info *abbrev, unsigned int abbrev_len,
13573 gdb_byte *info_ptr)
c906108c 13574{
dee91e82 13575 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13576 struct objfile *objfile = cu->objfile;
dee91e82 13577 gdb_byte *buffer = reader->buffer;
fa238c03 13578 unsigned int i;
c906108c 13579 struct attribute attr;
c5aa993b 13580 int has_low_pc_attr = 0;
c906108c 13581 int has_high_pc_attr = 0;
91da1414 13582 int high_pc_relative = 0;
c906108c 13583
72bf9492 13584 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13585
b64f50a1 13586 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13587
13588 info_ptr += abbrev_len;
13589
13590 if (abbrev == NULL)
13591 return info_ptr;
13592
c906108c
SS
13593 part_die->tag = abbrev->tag;
13594 part_die->has_children = abbrev->has_children;
c906108c
SS
13595
13596 for (i = 0; i < abbrev->num_attrs; ++i)
13597 {
dee91e82 13598 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13599
13600 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13601 partial symbol table. */
c906108c
SS
13602 switch (attr.name)
13603 {
13604 case DW_AT_name:
71c25dea
TT
13605 switch (part_die->tag)
13606 {
13607 case DW_TAG_compile_unit:
95554aad 13608 case DW_TAG_partial_unit:
348e048f 13609 case DW_TAG_type_unit:
71c25dea
TT
13610 /* Compilation units have a DW_AT_name that is a filename, not
13611 a source language identifier. */
13612 case DW_TAG_enumeration_type:
13613 case DW_TAG_enumerator:
13614 /* These tags always have simple identifiers already; no need
13615 to canonicalize them. */
13616 part_die->name = DW_STRING (&attr);
13617 break;
13618 default:
13619 part_die->name
13620 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13621 &objfile->objfile_obstack);
71c25dea
TT
13622 break;
13623 }
c906108c 13624 break;
31ef98ae 13625 case DW_AT_linkage_name:
c906108c 13626 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13627 /* Note that both forms of linkage name might appear. We
13628 assume they will be the same, and we only store the last
13629 one we see. */
94af9270
KS
13630 if (cu->language == language_ada)
13631 part_die->name = DW_STRING (&attr);
abc72ce4 13632 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13633 break;
13634 case DW_AT_low_pc:
13635 has_low_pc_attr = 1;
13636 part_die->lowpc = DW_ADDR (&attr);
13637 break;
13638 case DW_AT_high_pc:
13639 has_high_pc_attr = 1;
3019eac3
DE
13640 if (attr.form == DW_FORM_addr
13641 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13642 part_die->highpc = DW_ADDR (&attr);
13643 else
13644 {
13645 high_pc_relative = 1;
13646 part_die->highpc = DW_UNSND (&attr);
13647 }
c906108c
SS
13648 break;
13649 case DW_AT_location:
0963b4bd 13650 /* Support the .debug_loc offsets. */
8e19ed76
PS
13651 if (attr_form_is_block (&attr))
13652 {
95554aad 13653 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13654 }
3690dd37 13655 else if (attr_form_is_section_offset (&attr))
8e19ed76 13656 {
4d3c2250 13657 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13658 }
13659 else
13660 {
4d3c2250
KB
13661 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13662 "partial symbol information");
8e19ed76 13663 }
c906108c 13664 break;
c906108c
SS
13665 case DW_AT_external:
13666 part_die->is_external = DW_UNSND (&attr);
13667 break;
13668 case DW_AT_declaration:
13669 part_die->is_declaration = DW_UNSND (&attr);
13670 break;
13671 case DW_AT_type:
13672 part_die->has_type = 1;
13673 break;
13674 case DW_AT_abstract_origin:
13675 case DW_AT_specification:
72bf9492
DJ
13676 case DW_AT_extension:
13677 part_die->has_specification = 1;
c764a876 13678 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13679 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13680 || cu->per_cu->is_dwz);
c906108c
SS
13681 break;
13682 case DW_AT_sibling:
13683 /* Ignore absolute siblings, they might point outside of
13684 the current compile unit. */
13685 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13686 complaint (&symfile_complaints,
13687 _("ignoring absolute DW_AT_sibling"));
c906108c 13688 else
b64f50a1 13689 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13690 break;
fa4028e9
JB
13691 case DW_AT_byte_size:
13692 part_die->has_byte_size = 1;
13693 break;
68511cec
CES
13694 case DW_AT_calling_convention:
13695 /* DWARF doesn't provide a way to identify a program's source-level
13696 entry point. DW_AT_calling_convention attributes are only meant
13697 to describe functions' calling conventions.
13698
13699 However, because it's a necessary piece of information in
13700 Fortran, and because DW_CC_program is the only piece of debugging
13701 information whose definition refers to a 'main program' at all,
13702 several compilers have begun marking Fortran main programs with
13703 DW_CC_program --- even when those functions use the standard
13704 calling conventions.
13705
13706 So until DWARF specifies a way to provide this information and
13707 compilers pick up the new representation, we'll support this
13708 practice. */
13709 if (DW_UNSND (&attr) == DW_CC_program
13710 && cu->language == language_fortran)
01f8c46d
JK
13711 {
13712 set_main_name (part_die->name);
13713
13714 /* As this DIE has a static linkage the name would be difficult
13715 to look up later. */
13716 language_of_main = language_fortran;
13717 }
68511cec 13718 break;
481860b3
GB
13719 case DW_AT_inline:
13720 if (DW_UNSND (&attr) == DW_INL_inlined
13721 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13722 part_die->may_be_inlined = 1;
13723 break;
95554aad
TT
13724
13725 case DW_AT_import:
13726 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13727 {
13728 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13729 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13730 || cu->per_cu->is_dwz);
13731 }
95554aad
TT
13732 break;
13733
c906108c
SS
13734 default:
13735 break;
13736 }
13737 }
13738
91da1414
MW
13739 if (high_pc_relative)
13740 part_die->highpc += part_die->lowpc;
13741
9373cf26
JK
13742 if (has_low_pc_attr && has_high_pc_attr)
13743 {
13744 /* When using the GNU linker, .gnu.linkonce. sections are used to
13745 eliminate duplicate copies of functions and vtables and such.
13746 The linker will arbitrarily choose one and discard the others.
13747 The AT_*_pc values for such functions refer to local labels in
13748 these sections. If the section from that file was discarded, the
13749 labels are not in the output, so the relocs get a value of 0.
13750 If this is a discarded function, mark the pc bounds as invalid,
13751 so that GDB will ignore it. */
13752 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13753 {
bb5ed363 13754 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13755
13756 complaint (&symfile_complaints,
13757 _("DW_AT_low_pc %s is zero "
13758 "for DIE at 0x%x [in module %s]"),
13759 paddress (gdbarch, part_die->lowpc),
b64f50a1 13760 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13761 }
13762 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13763 else if (part_die->lowpc >= part_die->highpc)
13764 {
bb5ed363 13765 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13766
13767 complaint (&symfile_complaints,
13768 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13769 "for DIE at 0x%x [in module %s]"),
13770 paddress (gdbarch, part_die->lowpc),
13771 paddress (gdbarch, part_die->highpc),
b64f50a1 13772 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13773 }
13774 else
13775 part_die->has_pc_info = 1;
13776 }
85cbf3d3 13777
c906108c
SS
13778 return info_ptr;
13779}
13780
72bf9492
DJ
13781/* Find a cached partial DIE at OFFSET in CU. */
13782
13783static struct partial_die_info *
b64f50a1 13784find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13785{
13786 struct partial_die_info *lookup_die = NULL;
13787 struct partial_die_info part_die;
13788
13789 part_die.offset = offset;
b64f50a1
JK
13790 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13791 offset.sect_off);
72bf9492 13792
72bf9492
DJ
13793 return lookup_die;
13794}
13795
348e048f
DE
13796/* Find a partial DIE at OFFSET, which may or may not be in CU,
13797 except in the case of .debug_types DIEs which do not reference
13798 outside their CU (they do however referencing other types via
55f1336d 13799 DW_FORM_ref_sig8). */
72bf9492
DJ
13800
13801static struct partial_die_info *
36586728 13802find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13803{
bb5ed363 13804 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13805 struct dwarf2_per_cu_data *per_cu = NULL;
13806 struct partial_die_info *pd = NULL;
72bf9492 13807
36586728
TT
13808 if (offset_in_dwz == cu->per_cu->is_dwz
13809 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13810 {
13811 pd = find_partial_die_in_comp_unit (offset, cu);
13812 if (pd != NULL)
13813 return pd;
0d99eb77
DE
13814 /* We missed recording what we needed.
13815 Load all dies and try again. */
13816 per_cu = cu->per_cu;
5afb4e99 13817 }
0d99eb77
DE
13818 else
13819 {
13820 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13821 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13822 {
13823 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13824 " external reference to offset 0x%lx [in module %s].\n"),
13825 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13826 bfd_get_filename (objfile->obfd));
13827 }
36586728
TT
13828 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13829 objfile);
72bf9492 13830
0d99eb77
DE
13831 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13832 load_partial_comp_unit (per_cu);
ae038cb0 13833
0d99eb77
DE
13834 per_cu->cu->last_used = 0;
13835 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13836 }
5afb4e99 13837
dee91e82
DE
13838 /* If we didn't find it, and not all dies have been loaded,
13839 load them all and try again. */
13840
5afb4e99
DJ
13841 if (pd == NULL && per_cu->load_all_dies == 0)
13842 {
5afb4e99 13843 per_cu->load_all_dies = 1;
fd820528
DE
13844
13845 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13846 THIS_CU->cu may already be in use. So we can't just free it and
13847 replace its DIEs with the ones we read in. Instead, we leave those
13848 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13849 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13850 set. */
dee91e82 13851 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13852
13853 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13854 }
13855
13856 if (pd == NULL)
13857 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13858 _("could not find partial DIE 0x%x "
13859 "in cache [from module %s]\n"),
b64f50a1 13860 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13861 return pd;
72bf9492
DJ
13862}
13863
abc72ce4
DE
13864/* See if we can figure out if the class lives in a namespace. We do
13865 this by looking for a member function; its demangled name will
13866 contain namespace info, if there is any. */
13867
13868static void
13869guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13870 struct dwarf2_cu *cu)
13871{
13872 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13873 what template types look like, because the demangler
13874 frequently doesn't give the same name as the debug info. We
13875 could fix this by only using the demangled name to get the
13876 prefix (but see comment in read_structure_type). */
13877
13878 struct partial_die_info *real_pdi;
13879 struct partial_die_info *child_pdi;
13880
13881 /* If this DIE (this DIE's specification, if any) has a parent, then
13882 we should not do this. We'll prepend the parent's fully qualified
13883 name when we create the partial symbol. */
13884
13885 real_pdi = struct_pdi;
13886 while (real_pdi->has_specification)
36586728
TT
13887 real_pdi = find_partial_die (real_pdi->spec_offset,
13888 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13889
13890 if (real_pdi->die_parent != NULL)
13891 return;
13892
13893 for (child_pdi = struct_pdi->die_child;
13894 child_pdi != NULL;
13895 child_pdi = child_pdi->die_sibling)
13896 {
13897 if (child_pdi->tag == DW_TAG_subprogram
13898 && child_pdi->linkage_name != NULL)
13899 {
13900 char *actual_class_name
13901 = language_class_name_from_physname (cu->language_defn,
13902 child_pdi->linkage_name);
13903 if (actual_class_name != NULL)
13904 {
13905 struct_pdi->name
13906 = obsavestring (actual_class_name,
13907 strlen (actual_class_name),
13908 &cu->objfile->objfile_obstack);
13909 xfree (actual_class_name);
13910 }
13911 break;
13912 }
13913 }
13914}
13915
72bf9492
DJ
13916/* Adjust PART_DIE before generating a symbol for it. This function
13917 may set the is_external flag or change the DIE's name. */
13918
13919static void
13920fixup_partial_die (struct partial_die_info *part_die,
13921 struct dwarf2_cu *cu)
13922{
abc72ce4
DE
13923 /* Once we've fixed up a die, there's no point in doing so again.
13924 This also avoids a memory leak if we were to call
13925 guess_partial_die_structure_name multiple times. */
13926 if (part_die->fixup_called)
13927 return;
13928
72bf9492
DJ
13929 /* If we found a reference attribute and the DIE has no name, try
13930 to find a name in the referred to DIE. */
13931
13932 if (part_die->name == NULL && part_die->has_specification)
13933 {
13934 struct partial_die_info *spec_die;
72bf9492 13935
36586728
TT
13936 spec_die = find_partial_die (part_die->spec_offset,
13937 part_die->spec_is_dwz, cu);
72bf9492 13938
10b3939b 13939 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13940
13941 if (spec_die->name)
13942 {
13943 part_die->name = spec_die->name;
13944
13945 /* Copy DW_AT_external attribute if it is set. */
13946 if (spec_die->is_external)
13947 part_die->is_external = spec_die->is_external;
13948 }
13949 }
13950
13951 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13952
13953 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13954 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13955
abc72ce4
DE
13956 /* If there is no parent die to provide a namespace, and there are
13957 children, see if we can determine the namespace from their linkage
122d1940 13958 name. */
abc72ce4 13959 if (cu->language == language_cplus
8b70b953 13960 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13961 && part_die->die_parent == NULL
13962 && part_die->has_children
13963 && (part_die->tag == DW_TAG_class_type
13964 || part_die->tag == DW_TAG_structure_type
13965 || part_die->tag == DW_TAG_union_type))
13966 guess_partial_die_structure_name (part_die, cu);
13967
53832f31
TT
13968 /* GCC might emit a nameless struct or union that has a linkage
13969 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13970 if (part_die->name == NULL
96408a79
SA
13971 && (part_die->tag == DW_TAG_class_type
13972 || part_die->tag == DW_TAG_interface_type
13973 || part_die->tag == DW_TAG_structure_type
13974 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13975 && part_die->linkage_name != NULL)
13976 {
13977 char *demangled;
13978
13979 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13980 if (demangled)
13981 {
96408a79
SA
13982 const char *base;
13983
13984 /* Strip any leading namespaces/classes, keep only the base name.
13985 DW_AT_name for named DIEs does not contain the prefixes. */
13986 base = strrchr (demangled, ':');
13987 if (base && base > demangled && base[-1] == ':')
13988 base++;
13989 else
13990 base = demangled;
13991
13992 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13993 &cu->objfile->objfile_obstack);
13994 xfree (demangled);
13995 }
13996 }
13997
abc72ce4 13998 part_die->fixup_called = 1;
72bf9492
DJ
13999}
14000
a8329558 14001/* Read an attribute value described by an attribute form. */
c906108c 14002
fe1b8b76 14003static gdb_byte *
dee91e82
DE
14004read_attribute_value (const struct die_reader_specs *reader,
14005 struct attribute *attr, unsigned form,
14006 gdb_byte *info_ptr)
c906108c 14007{
dee91e82
DE
14008 struct dwarf2_cu *cu = reader->cu;
14009 bfd *abfd = reader->abfd;
e7c27a73 14010 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14011 unsigned int bytes_read;
14012 struct dwarf_block *blk;
14013
a8329558
KW
14014 attr->form = form;
14015 switch (form)
c906108c 14016 {
c906108c 14017 case DW_FORM_ref_addr:
ae411497 14018 if (cu->header.version == 2)
4568ecf9 14019 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14020 else
4568ecf9
DE
14021 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14022 &cu->header, &bytes_read);
ae411497
TT
14023 info_ptr += bytes_read;
14024 break;
36586728
TT
14025 case DW_FORM_GNU_ref_alt:
14026 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14027 info_ptr += bytes_read;
14028 break;
ae411497 14029 case DW_FORM_addr:
e7c27a73 14030 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14031 info_ptr += bytes_read;
c906108c
SS
14032 break;
14033 case DW_FORM_block2:
7b5a2f43 14034 blk = dwarf_alloc_block (cu);
c906108c
SS
14035 blk->size = read_2_bytes (abfd, info_ptr);
14036 info_ptr += 2;
14037 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14038 info_ptr += blk->size;
14039 DW_BLOCK (attr) = blk;
14040 break;
14041 case DW_FORM_block4:
7b5a2f43 14042 blk = dwarf_alloc_block (cu);
c906108c
SS
14043 blk->size = read_4_bytes (abfd, info_ptr);
14044 info_ptr += 4;
14045 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14046 info_ptr += blk->size;
14047 DW_BLOCK (attr) = blk;
14048 break;
14049 case DW_FORM_data2:
14050 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14051 info_ptr += 2;
14052 break;
14053 case DW_FORM_data4:
14054 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14055 info_ptr += 4;
14056 break;
14057 case DW_FORM_data8:
14058 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14059 info_ptr += 8;
14060 break;
2dc7f7b3
TT
14061 case DW_FORM_sec_offset:
14062 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14063 info_ptr += bytes_read;
14064 break;
c906108c 14065 case DW_FORM_string:
9b1c24c8 14066 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14067 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14068 info_ptr += bytes_read;
14069 break;
4bdf3d34 14070 case DW_FORM_strp:
36586728
TT
14071 if (!cu->per_cu->is_dwz)
14072 {
14073 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14074 &bytes_read);
14075 DW_STRING_IS_CANONICAL (attr) = 0;
14076 info_ptr += bytes_read;
14077 break;
14078 }
14079 /* FALLTHROUGH */
14080 case DW_FORM_GNU_strp_alt:
14081 {
14082 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14083 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14084 &bytes_read);
14085
14086 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14087 DW_STRING_IS_CANONICAL (attr) = 0;
14088 info_ptr += bytes_read;
14089 }
4bdf3d34 14090 break;
2dc7f7b3 14091 case DW_FORM_exprloc:
c906108c 14092 case DW_FORM_block:
7b5a2f43 14093 blk = dwarf_alloc_block (cu);
c906108c
SS
14094 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14095 info_ptr += bytes_read;
14096 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14097 info_ptr += blk->size;
14098 DW_BLOCK (attr) = blk;
14099 break;
14100 case DW_FORM_block1:
7b5a2f43 14101 blk = dwarf_alloc_block (cu);
c906108c
SS
14102 blk->size = read_1_byte (abfd, info_ptr);
14103 info_ptr += 1;
14104 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14105 info_ptr += blk->size;
14106 DW_BLOCK (attr) = blk;
14107 break;
14108 case DW_FORM_data1:
14109 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14110 info_ptr += 1;
14111 break;
14112 case DW_FORM_flag:
14113 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14114 info_ptr += 1;
14115 break;
2dc7f7b3
TT
14116 case DW_FORM_flag_present:
14117 DW_UNSND (attr) = 1;
14118 break;
c906108c
SS
14119 case DW_FORM_sdata:
14120 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14121 info_ptr += bytes_read;
14122 break;
14123 case DW_FORM_udata:
14124 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14125 info_ptr += bytes_read;
14126 break;
14127 case DW_FORM_ref1:
4568ecf9
DE
14128 DW_UNSND (attr) = (cu->header.offset.sect_off
14129 + read_1_byte (abfd, info_ptr));
c906108c
SS
14130 info_ptr += 1;
14131 break;
14132 case DW_FORM_ref2:
4568ecf9
DE
14133 DW_UNSND (attr) = (cu->header.offset.sect_off
14134 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14135 info_ptr += 2;
14136 break;
14137 case DW_FORM_ref4:
4568ecf9
DE
14138 DW_UNSND (attr) = (cu->header.offset.sect_off
14139 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14140 info_ptr += 4;
14141 break;
613e1657 14142 case DW_FORM_ref8:
4568ecf9
DE
14143 DW_UNSND (attr) = (cu->header.offset.sect_off
14144 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14145 info_ptr += 8;
14146 break;
55f1336d 14147 case DW_FORM_ref_sig8:
348e048f
DE
14148 /* Convert the signature to something we can record in DW_UNSND
14149 for later lookup.
14150 NOTE: This is NULL if the type wasn't found. */
14151 DW_SIGNATURED_TYPE (attr) =
e319fa28 14152 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14153 info_ptr += 8;
14154 break;
c906108c 14155 case DW_FORM_ref_udata:
4568ecf9
DE
14156 DW_UNSND (attr) = (cu->header.offset.sect_off
14157 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14158 info_ptr += bytes_read;
14159 break;
c906108c 14160 case DW_FORM_indirect:
a8329558
KW
14161 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14162 info_ptr += bytes_read;
dee91e82 14163 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14164 break;
3019eac3
DE
14165 case DW_FORM_GNU_addr_index:
14166 if (reader->dwo_file == NULL)
14167 {
14168 /* For now flag a hard error.
14169 Later we can turn this into a complaint. */
14170 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14171 dwarf_form_name (form),
14172 bfd_get_filename (abfd));
14173 }
14174 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14175 info_ptr += bytes_read;
14176 break;
14177 case DW_FORM_GNU_str_index:
14178 if (reader->dwo_file == NULL)
14179 {
14180 /* For now flag a hard error.
14181 Later we can turn this into a complaint if warranted. */
14182 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14183 dwarf_form_name (form),
14184 bfd_get_filename (abfd));
14185 }
14186 {
14187 ULONGEST str_index =
14188 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14189
14190 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14191 DW_STRING_IS_CANONICAL (attr) = 0;
14192 info_ptr += bytes_read;
14193 }
14194 break;
c906108c 14195 default:
8a3fe4f8 14196 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14197 dwarf_form_name (form),
14198 bfd_get_filename (abfd));
c906108c 14199 }
28e94949 14200
36586728
TT
14201 /* Super hack. */
14202 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14203 attr->form = DW_FORM_GNU_ref_alt;
14204
28e94949
JB
14205 /* We have seen instances where the compiler tried to emit a byte
14206 size attribute of -1 which ended up being encoded as an unsigned
14207 0xffffffff. Although 0xffffffff is technically a valid size value,
14208 an object of this size seems pretty unlikely so we can relatively
14209 safely treat these cases as if the size attribute was invalid and
14210 treat them as zero by default. */
14211 if (attr->name == DW_AT_byte_size
14212 && form == DW_FORM_data4
14213 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14214 {
14215 complaint
14216 (&symfile_complaints,
43bbcdc2
PH
14217 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14218 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14219 DW_UNSND (attr) = 0;
14220 }
28e94949 14221
c906108c
SS
14222 return info_ptr;
14223}
14224
a8329558
KW
14225/* Read an attribute described by an abbreviated attribute. */
14226
fe1b8b76 14227static gdb_byte *
dee91e82
DE
14228read_attribute (const struct die_reader_specs *reader,
14229 struct attribute *attr, struct attr_abbrev *abbrev,
14230 gdb_byte *info_ptr)
a8329558
KW
14231{
14232 attr->name = abbrev->name;
dee91e82 14233 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14234}
14235
0963b4bd 14236/* Read dwarf information from a buffer. */
c906108c
SS
14237
14238static unsigned int
a1855c1d 14239read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14240{
fe1b8b76 14241 return bfd_get_8 (abfd, buf);
c906108c
SS
14242}
14243
14244static int
a1855c1d 14245read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14246{
fe1b8b76 14247 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14248}
14249
14250static unsigned int
a1855c1d 14251read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14252{
fe1b8b76 14253 return bfd_get_16 (abfd, buf);
c906108c
SS
14254}
14255
21ae7a4d 14256static int
a1855c1d 14257read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14258{
14259 return bfd_get_signed_16 (abfd, buf);
14260}
14261
c906108c 14262static unsigned int
a1855c1d 14263read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14264{
fe1b8b76 14265 return bfd_get_32 (abfd, buf);
c906108c
SS
14266}
14267
21ae7a4d 14268static int
a1855c1d 14269read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14270{
14271 return bfd_get_signed_32 (abfd, buf);
14272}
14273
93311388 14274static ULONGEST
a1855c1d 14275read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14276{
fe1b8b76 14277 return bfd_get_64 (abfd, buf);
c906108c
SS
14278}
14279
14280static CORE_ADDR
fe1b8b76 14281read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14282 unsigned int *bytes_read)
c906108c 14283{
e7c27a73 14284 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14285 CORE_ADDR retval = 0;
14286
107d2387 14287 if (cu_header->signed_addr_p)
c906108c 14288 {
107d2387
AC
14289 switch (cu_header->addr_size)
14290 {
14291 case 2:
fe1b8b76 14292 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14293 break;
14294 case 4:
fe1b8b76 14295 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14296 break;
14297 case 8:
fe1b8b76 14298 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14299 break;
14300 default:
8e65ff28 14301 internal_error (__FILE__, __LINE__,
e2e0b3e5 14302 _("read_address: bad switch, signed [in module %s]"),
659b0389 14303 bfd_get_filename (abfd));
107d2387
AC
14304 }
14305 }
14306 else
14307 {
14308 switch (cu_header->addr_size)
14309 {
14310 case 2:
fe1b8b76 14311 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14312 break;
14313 case 4:
fe1b8b76 14314 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14315 break;
14316 case 8:
fe1b8b76 14317 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14318 break;
14319 default:
8e65ff28 14320 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14321 _("read_address: bad switch, "
14322 "unsigned [in module %s]"),
659b0389 14323 bfd_get_filename (abfd));
107d2387 14324 }
c906108c 14325 }
64367e0a 14326
107d2387
AC
14327 *bytes_read = cu_header->addr_size;
14328 return retval;
c906108c
SS
14329}
14330
f7ef9339 14331/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14332 specification allows the initial length to take up either 4 bytes
14333 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14334 bytes describe the length and all offsets will be 8 bytes in length
14335 instead of 4.
14336
f7ef9339
KB
14337 An older, non-standard 64-bit format is also handled by this
14338 function. The older format in question stores the initial length
14339 as an 8-byte quantity without an escape value. Lengths greater
14340 than 2^32 aren't very common which means that the initial 4 bytes
14341 is almost always zero. Since a length value of zero doesn't make
14342 sense for the 32-bit format, this initial zero can be considered to
14343 be an escape value which indicates the presence of the older 64-bit
14344 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14345 greater than 4GB. If it becomes necessary to handle lengths
14346 somewhat larger than 4GB, we could allow other small values (such
14347 as the non-sensical values of 1, 2, and 3) to also be used as
14348 escape values indicating the presence of the old format.
f7ef9339 14349
917c78fc
MK
14350 The value returned via bytes_read should be used to increment the
14351 relevant pointer after calling read_initial_length().
c764a876 14352
613e1657
KB
14353 [ Note: read_initial_length() and read_offset() are based on the
14354 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14355 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14356 from:
14357
f7ef9339 14358 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14359
613e1657
KB
14360 This document is only a draft and is subject to change. (So beware.)
14361
f7ef9339 14362 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14363 determined empirically by examining 64-bit ELF files produced by
14364 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14365
14366 - Kevin, July 16, 2002
613e1657
KB
14367 ] */
14368
14369static LONGEST
c764a876 14370read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14371{
fe1b8b76 14372 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14373
dd373385 14374 if (length == 0xffffffff)
613e1657 14375 {
fe1b8b76 14376 length = bfd_get_64 (abfd, buf + 4);
613e1657 14377 *bytes_read = 12;
613e1657 14378 }
dd373385 14379 else if (length == 0)
f7ef9339 14380 {
dd373385 14381 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14382 length = bfd_get_64 (abfd, buf);
f7ef9339 14383 *bytes_read = 8;
f7ef9339 14384 }
613e1657
KB
14385 else
14386 {
14387 *bytes_read = 4;
613e1657
KB
14388 }
14389
c764a876
DE
14390 return length;
14391}
dd373385 14392
c764a876
DE
14393/* Cover function for read_initial_length.
14394 Returns the length of the object at BUF, and stores the size of the
14395 initial length in *BYTES_READ and stores the size that offsets will be in
14396 *OFFSET_SIZE.
14397 If the initial length size is not equivalent to that specified in
14398 CU_HEADER then issue a complaint.
14399 This is useful when reading non-comp-unit headers. */
dd373385 14400
c764a876
DE
14401static LONGEST
14402read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14403 const struct comp_unit_head *cu_header,
14404 unsigned int *bytes_read,
14405 unsigned int *offset_size)
14406{
14407 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14408
14409 gdb_assert (cu_header->initial_length_size == 4
14410 || cu_header->initial_length_size == 8
14411 || cu_header->initial_length_size == 12);
14412
14413 if (cu_header->initial_length_size != *bytes_read)
14414 complaint (&symfile_complaints,
14415 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14416
c764a876 14417 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14418 return length;
613e1657
KB
14419}
14420
14421/* Read an offset from the data stream. The size of the offset is
917c78fc 14422 given by cu_header->offset_size. */
613e1657
KB
14423
14424static LONGEST
fe1b8b76 14425read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14426 unsigned int *bytes_read)
c764a876
DE
14427{
14428 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14429
c764a876
DE
14430 *bytes_read = cu_header->offset_size;
14431 return offset;
14432}
14433
14434/* Read an offset from the data stream. */
14435
14436static LONGEST
14437read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14438{
14439 LONGEST retval = 0;
14440
c764a876 14441 switch (offset_size)
613e1657
KB
14442 {
14443 case 4:
fe1b8b76 14444 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14445 break;
14446 case 8:
fe1b8b76 14447 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14448 break;
14449 default:
8e65ff28 14450 internal_error (__FILE__, __LINE__,
c764a876 14451 _("read_offset_1: bad switch [in module %s]"),
659b0389 14452 bfd_get_filename (abfd));
613e1657
KB
14453 }
14454
917c78fc 14455 return retval;
613e1657
KB
14456}
14457
fe1b8b76
JB
14458static gdb_byte *
14459read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14460{
14461 /* If the size of a host char is 8 bits, we can return a pointer
14462 to the buffer, otherwise we have to copy the data to a buffer
14463 allocated on the temporary obstack. */
4bdf3d34 14464 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14465 return buf;
c906108c
SS
14466}
14467
14468static char *
9b1c24c8 14469read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14470{
14471 /* If the size of a host char is 8 bits, we can return a pointer
14472 to the string, otherwise we have to copy the string to a buffer
14473 allocated on the temporary obstack. */
4bdf3d34 14474 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14475 if (*buf == '\0')
14476 {
14477 *bytes_read_ptr = 1;
14478 return NULL;
14479 }
fe1b8b76
JB
14480 *bytes_read_ptr = strlen ((char *) buf) + 1;
14481 return (char *) buf;
4bdf3d34
JJ
14482}
14483
14484static char *
cf2c3c16 14485read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14486{
be391dca 14487 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14488 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14489 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14490 bfd_get_filename (abfd));
dce234bc 14491 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14492 error (_("DW_FORM_strp pointing outside of "
14493 ".debug_str section [in module %s]"),
14494 bfd_get_filename (abfd));
4bdf3d34 14495 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14496 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14497 return NULL;
dce234bc 14498 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14499}
14500
36586728
TT
14501/* Read a string at offset STR_OFFSET in the .debug_str section from
14502 the .dwz file DWZ. Throw an error if the offset is too large. If
14503 the string consists of a single NUL byte, return NULL; otherwise
14504 return a pointer to the string. */
14505
14506static char *
14507read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14508{
14509 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14510
14511 if (dwz->str.buffer == NULL)
14512 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14513 "section [in module %s]"),
14514 bfd_get_filename (dwz->dwz_bfd));
14515 if (str_offset >= dwz->str.size)
14516 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14517 ".debug_str section [in module %s]"),
14518 bfd_get_filename (dwz->dwz_bfd));
14519 gdb_assert (HOST_CHAR_BIT == 8);
14520 if (dwz->str.buffer[str_offset] == '\0')
14521 return NULL;
14522 return (char *) (dwz->str.buffer + str_offset);
14523}
14524
cf2c3c16
TT
14525static char *
14526read_indirect_string (bfd *abfd, gdb_byte *buf,
14527 const struct comp_unit_head *cu_header,
14528 unsigned int *bytes_read_ptr)
14529{
14530 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14531
14532 return read_indirect_string_at_offset (abfd, str_offset);
14533}
14534
12df843f 14535static ULONGEST
fe1b8b76 14536read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14537{
12df843f 14538 ULONGEST result;
ce5d95e1 14539 unsigned int num_read;
c906108c
SS
14540 int i, shift;
14541 unsigned char byte;
14542
14543 result = 0;
14544 shift = 0;
14545 num_read = 0;
14546 i = 0;
14547 while (1)
14548 {
fe1b8b76 14549 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14550 buf++;
14551 num_read++;
12df843f 14552 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14553 if ((byte & 128) == 0)
14554 {
14555 break;
14556 }
14557 shift += 7;
14558 }
14559 *bytes_read_ptr = num_read;
14560 return result;
14561}
14562
12df843f 14563static LONGEST
fe1b8b76 14564read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14565{
12df843f 14566 LONGEST result;
77e0b926 14567 int i, shift, num_read;
c906108c
SS
14568 unsigned char byte;
14569
14570 result = 0;
14571 shift = 0;
c906108c
SS
14572 num_read = 0;
14573 i = 0;
14574 while (1)
14575 {
fe1b8b76 14576 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14577 buf++;
14578 num_read++;
12df843f 14579 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14580 shift += 7;
14581 if ((byte & 128) == 0)
14582 {
14583 break;
14584 }
14585 }
77e0b926 14586 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14587 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14588 *bytes_read_ptr = num_read;
14589 return result;
14590}
14591
3019eac3
DE
14592/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14593 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14594 ADDR_SIZE is the size of addresses from the CU header. */
14595
14596static CORE_ADDR
14597read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14598{
14599 struct objfile *objfile = dwarf2_per_objfile->objfile;
14600 bfd *abfd = objfile->obfd;
14601 const gdb_byte *info_ptr;
14602
14603 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14604 if (dwarf2_per_objfile->addr.buffer == NULL)
14605 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14606 objfile->name);
14607 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14608 error (_("DW_FORM_addr_index pointing outside of "
14609 ".debug_addr section [in module %s]"),
14610 objfile->name);
14611 info_ptr = (dwarf2_per_objfile->addr.buffer
14612 + addr_base + addr_index * addr_size);
14613 if (addr_size == 4)
14614 return bfd_get_32 (abfd, info_ptr);
14615 else
14616 return bfd_get_64 (abfd, info_ptr);
14617}
14618
14619/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14620
14621static CORE_ADDR
14622read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14623{
14624 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14625}
14626
14627/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14628
14629static CORE_ADDR
14630read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14631 unsigned int *bytes_read)
14632{
14633 bfd *abfd = cu->objfile->obfd;
14634 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14635
14636 return read_addr_index (cu, addr_index);
14637}
14638
14639/* Data structure to pass results from dwarf2_read_addr_index_reader
14640 back to dwarf2_read_addr_index. */
14641
14642struct dwarf2_read_addr_index_data
14643{
14644 ULONGEST addr_base;
14645 int addr_size;
14646};
14647
14648/* die_reader_func for dwarf2_read_addr_index. */
14649
14650static void
14651dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14652 gdb_byte *info_ptr,
14653 struct die_info *comp_unit_die,
14654 int has_children,
14655 void *data)
14656{
14657 struct dwarf2_cu *cu = reader->cu;
14658 struct dwarf2_read_addr_index_data *aidata =
14659 (struct dwarf2_read_addr_index_data *) data;
14660
14661 aidata->addr_base = cu->addr_base;
14662 aidata->addr_size = cu->header.addr_size;
14663}
14664
14665/* Given an index in .debug_addr, fetch the value.
14666 NOTE: This can be called during dwarf expression evaluation,
14667 long after the debug information has been read, and thus per_cu->cu
14668 may no longer exist. */
14669
14670CORE_ADDR
14671dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14672 unsigned int addr_index)
14673{
14674 struct objfile *objfile = per_cu->objfile;
14675 struct dwarf2_cu *cu = per_cu->cu;
14676 ULONGEST addr_base;
14677 int addr_size;
14678
14679 /* This is intended to be called from outside this file. */
14680 dw2_setup (objfile);
14681
14682 /* We need addr_base and addr_size.
14683 If we don't have PER_CU->cu, we have to get it.
14684 Nasty, but the alternative is storing the needed info in PER_CU,
14685 which at this point doesn't seem justified: it's not clear how frequently
14686 it would get used and it would increase the size of every PER_CU.
14687 Entry points like dwarf2_per_cu_addr_size do a similar thing
14688 so we're not in uncharted territory here.
14689 Alas we need to be a bit more complicated as addr_base is contained
14690 in the DIE.
14691
14692 We don't need to read the entire CU(/TU).
14693 We just need the header and top level die.
a1b64ce1 14694
3019eac3 14695 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14696 For now we skip this optimization. */
3019eac3
DE
14697
14698 if (cu != NULL)
14699 {
14700 addr_base = cu->addr_base;
14701 addr_size = cu->header.addr_size;
14702 }
14703 else
14704 {
14705 struct dwarf2_read_addr_index_data aidata;
14706
a1b64ce1
DE
14707 /* Note: We can't use init_cutu_and_read_dies_simple here,
14708 we need addr_base. */
14709 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14710 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14711 addr_base = aidata.addr_base;
14712 addr_size = aidata.addr_size;
14713 }
14714
14715 return read_addr_index_1 (addr_index, addr_base, addr_size);
14716}
14717
14718/* Given a DW_AT_str_index, fetch the string. */
14719
14720static char *
14721read_str_index (const struct die_reader_specs *reader,
14722 struct dwarf2_cu *cu, ULONGEST str_index)
14723{
14724 struct objfile *objfile = dwarf2_per_objfile->objfile;
14725 const char *dwo_name = objfile->name;
14726 bfd *abfd = objfile->obfd;
14727 struct dwo_sections *sections = &reader->dwo_file->sections;
14728 gdb_byte *info_ptr;
14729 ULONGEST str_offset;
14730
14731 dwarf2_read_section (objfile, &sections->str);
14732 dwarf2_read_section (objfile, &sections->str_offsets);
14733 if (sections->str.buffer == NULL)
14734 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14735 " in CU at offset 0x%lx [in module %s]"),
14736 (long) cu->header.offset.sect_off, dwo_name);
14737 if (sections->str_offsets.buffer == NULL)
14738 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14739 " in CU at offset 0x%lx [in module %s]"),
14740 (long) cu->header.offset.sect_off, dwo_name);
14741 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14742 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14743 " section in CU at offset 0x%lx [in module %s]"),
14744 (long) cu->header.offset.sect_off, dwo_name);
14745 info_ptr = (sections->str_offsets.buffer
14746 + str_index * cu->header.offset_size);
14747 if (cu->header.offset_size == 4)
14748 str_offset = bfd_get_32 (abfd, info_ptr);
14749 else
14750 str_offset = bfd_get_64 (abfd, info_ptr);
14751 if (str_offset >= sections->str.size)
14752 error (_("Offset from DW_FORM_str_index pointing outside of"
14753 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14754 (long) cu->header.offset.sect_off, dwo_name);
14755 return (char *) (sections->str.buffer + str_offset);
14756}
14757
3019eac3
DE
14758/* Return the length of an LEB128 number in BUF. */
14759
14760static int
14761leb128_size (const gdb_byte *buf)
14762{
14763 const gdb_byte *begin = buf;
14764 gdb_byte byte;
14765
14766 while (1)
14767 {
14768 byte = *buf++;
14769 if ((byte & 128) == 0)
14770 return buf - begin;
14771 }
14772}
14773
c906108c 14774static void
e142c38c 14775set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14776{
14777 switch (lang)
14778 {
14779 case DW_LANG_C89:
76bee0cc 14780 case DW_LANG_C99:
c906108c 14781 case DW_LANG_C:
e142c38c 14782 cu->language = language_c;
c906108c
SS
14783 break;
14784 case DW_LANG_C_plus_plus:
e142c38c 14785 cu->language = language_cplus;
c906108c 14786 break;
6aecb9c2
JB
14787 case DW_LANG_D:
14788 cu->language = language_d;
14789 break;
c906108c
SS
14790 case DW_LANG_Fortran77:
14791 case DW_LANG_Fortran90:
b21b22e0 14792 case DW_LANG_Fortran95:
e142c38c 14793 cu->language = language_fortran;
c906108c 14794 break;
a766d390
DE
14795 case DW_LANG_Go:
14796 cu->language = language_go;
14797 break;
c906108c 14798 case DW_LANG_Mips_Assembler:
e142c38c 14799 cu->language = language_asm;
c906108c 14800 break;
bebd888e 14801 case DW_LANG_Java:
e142c38c 14802 cu->language = language_java;
bebd888e 14803 break;
c906108c 14804 case DW_LANG_Ada83:
8aaf0b47 14805 case DW_LANG_Ada95:
bc5f45f8
JB
14806 cu->language = language_ada;
14807 break;
72019c9c
GM
14808 case DW_LANG_Modula2:
14809 cu->language = language_m2;
14810 break;
fe8e67fd
PM
14811 case DW_LANG_Pascal83:
14812 cu->language = language_pascal;
14813 break;
22566fbd
DJ
14814 case DW_LANG_ObjC:
14815 cu->language = language_objc;
14816 break;
c906108c
SS
14817 case DW_LANG_Cobol74:
14818 case DW_LANG_Cobol85:
c906108c 14819 default:
e142c38c 14820 cu->language = language_minimal;
c906108c
SS
14821 break;
14822 }
e142c38c 14823 cu->language_defn = language_def (cu->language);
c906108c
SS
14824}
14825
14826/* Return the named attribute or NULL if not there. */
14827
14828static struct attribute *
e142c38c 14829dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14830{
a48e046c 14831 for (;;)
c906108c 14832 {
a48e046c
TT
14833 unsigned int i;
14834 struct attribute *spec = NULL;
14835
14836 for (i = 0; i < die->num_attrs; ++i)
14837 {
14838 if (die->attrs[i].name == name)
14839 return &die->attrs[i];
14840 if (die->attrs[i].name == DW_AT_specification
14841 || die->attrs[i].name == DW_AT_abstract_origin)
14842 spec = &die->attrs[i];
14843 }
14844
14845 if (!spec)
14846 break;
c906108c 14847
f2f0e013 14848 die = follow_die_ref (die, spec, &cu);
f2f0e013 14849 }
c5aa993b 14850
c906108c
SS
14851 return NULL;
14852}
14853
348e048f
DE
14854/* Return the named attribute or NULL if not there,
14855 but do not follow DW_AT_specification, etc.
14856 This is for use in contexts where we're reading .debug_types dies.
14857 Following DW_AT_specification, DW_AT_abstract_origin will take us
14858 back up the chain, and we want to go down. */
14859
14860static struct attribute *
45e58e77 14861dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14862{
14863 unsigned int i;
14864
14865 for (i = 0; i < die->num_attrs; ++i)
14866 if (die->attrs[i].name == name)
14867 return &die->attrs[i];
14868
14869 return NULL;
14870}
14871
05cf31d1
JB
14872/* Return non-zero iff the attribute NAME is defined for the given DIE,
14873 and holds a non-zero value. This function should only be used for
2dc7f7b3 14874 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14875
14876static int
14877dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14878{
14879 struct attribute *attr = dwarf2_attr (die, name, cu);
14880
14881 return (attr && DW_UNSND (attr));
14882}
14883
3ca72b44 14884static int
e142c38c 14885die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14886{
05cf31d1
JB
14887 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14888 which value is non-zero. However, we have to be careful with
14889 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14890 (via dwarf2_flag_true_p) follows this attribute. So we may
14891 end up accidently finding a declaration attribute that belongs
14892 to a different DIE referenced by the specification attribute,
14893 even though the given DIE does not have a declaration attribute. */
14894 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14895 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14896}
14897
63d06c5c 14898/* Return the die giving the specification for DIE, if there is
f2f0e013 14899 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14900 containing the return value on output. If there is no
14901 specification, but there is an abstract origin, that is
14902 returned. */
63d06c5c
DC
14903
14904static struct die_info *
f2f0e013 14905die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14906{
f2f0e013
DJ
14907 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14908 *spec_cu);
63d06c5c 14909
edb3359d
DJ
14910 if (spec_attr == NULL)
14911 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14912
63d06c5c
DC
14913 if (spec_attr == NULL)
14914 return NULL;
14915 else
f2f0e013 14916 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14917}
c906108c 14918
debd256d 14919/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14920 refers to.
14921 NOTE: This is also used as a "cleanup" function. */
14922
debd256d
JB
14923static void
14924free_line_header (struct line_header *lh)
14925{
14926 if (lh->standard_opcode_lengths)
a8bc7b56 14927 xfree (lh->standard_opcode_lengths);
debd256d
JB
14928
14929 /* Remember that all the lh->file_names[i].name pointers are
14930 pointers into debug_line_buffer, and don't need to be freed. */
14931 if (lh->file_names)
a8bc7b56 14932 xfree (lh->file_names);
debd256d
JB
14933
14934 /* Similarly for the include directory names. */
14935 if (lh->include_dirs)
a8bc7b56 14936 xfree (lh->include_dirs);
debd256d 14937
a8bc7b56 14938 xfree (lh);
debd256d
JB
14939}
14940
debd256d 14941/* Add an entry to LH's include directory table. */
ae2de4f8 14942
debd256d
JB
14943static void
14944add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14945{
debd256d
JB
14946 /* Grow the array if necessary. */
14947 if (lh->include_dirs_size == 0)
c5aa993b 14948 {
debd256d
JB
14949 lh->include_dirs_size = 1; /* for testing */
14950 lh->include_dirs = xmalloc (lh->include_dirs_size
14951 * sizeof (*lh->include_dirs));
14952 }
14953 else if (lh->num_include_dirs >= lh->include_dirs_size)
14954 {
14955 lh->include_dirs_size *= 2;
14956 lh->include_dirs = xrealloc (lh->include_dirs,
14957 (lh->include_dirs_size
14958 * sizeof (*lh->include_dirs)));
c5aa993b 14959 }
c906108c 14960
debd256d
JB
14961 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14962}
6e70227d 14963
debd256d 14964/* Add an entry to LH's file name table. */
ae2de4f8 14965
debd256d
JB
14966static void
14967add_file_name (struct line_header *lh,
14968 char *name,
14969 unsigned int dir_index,
14970 unsigned int mod_time,
14971 unsigned int length)
14972{
14973 struct file_entry *fe;
14974
14975 /* Grow the array if necessary. */
14976 if (lh->file_names_size == 0)
14977 {
14978 lh->file_names_size = 1; /* for testing */
14979 lh->file_names = xmalloc (lh->file_names_size
14980 * sizeof (*lh->file_names));
14981 }
14982 else if (lh->num_file_names >= lh->file_names_size)
14983 {
14984 lh->file_names_size *= 2;
14985 lh->file_names = xrealloc (lh->file_names,
14986 (lh->file_names_size
14987 * sizeof (*lh->file_names)));
14988 }
14989
14990 fe = &lh->file_names[lh->num_file_names++];
14991 fe->name = name;
14992 fe->dir_index = dir_index;
14993 fe->mod_time = mod_time;
14994 fe->length = length;
aaa75496 14995 fe->included_p = 0;
cb1df416 14996 fe->symtab = NULL;
debd256d 14997}
6e70227d 14998
36586728
TT
14999/* A convenience function to find the proper .debug_line section for a
15000 CU. */
15001
15002static struct dwarf2_section_info *
15003get_debug_line_section (struct dwarf2_cu *cu)
15004{
15005 struct dwarf2_section_info *section;
15006
15007 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15008 DWO file. */
15009 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15010 section = &cu->dwo_unit->dwo_file->sections.line;
15011 else if (cu->per_cu->is_dwz)
15012 {
15013 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15014
15015 section = &dwz->line;
15016 }
15017 else
15018 section = &dwarf2_per_objfile->line;
15019
15020 return section;
15021}
15022
debd256d 15023/* Read the statement program header starting at OFFSET in
3019eac3 15024 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15025 to a struct line_header, allocated using xmalloc.
debd256d
JB
15026
15027 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15028 the returned object point into the dwarf line section buffer,
15029 and must not be freed. */
ae2de4f8 15030
debd256d 15031static struct line_header *
3019eac3 15032dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15033{
15034 struct cleanup *back_to;
15035 struct line_header *lh;
fe1b8b76 15036 gdb_byte *line_ptr;
c764a876 15037 unsigned int bytes_read, offset_size;
debd256d
JB
15038 int i;
15039 char *cur_dir, *cur_file;
3019eac3
DE
15040 struct dwarf2_section_info *section;
15041 bfd *abfd;
15042
36586728 15043 section = get_debug_line_section (cu);
3019eac3
DE
15044 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15045 if (section->buffer == NULL)
debd256d 15046 {
3019eac3
DE
15047 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15048 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15049 else
15050 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15051 return 0;
15052 }
15053
fceca515
DE
15054 /* We can't do this until we know the section is non-empty.
15055 Only then do we know we have such a section. */
15056 abfd = section->asection->owner;
15057
a738430d
MK
15058 /* Make sure that at least there's room for the total_length field.
15059 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15060 if (offset + 4 >= section->size)
debd256d 15061 {
4d3c2250 15062 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15063 return 0;
15064 }
15065
15066 lh = xmalloc (sizeof (*lh));
15067 memset (lh, 0, sizeof (*lh));
15068 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15069 (void *) lh);
15070
3019eac3 15071 line_ptr = section->buffer + offset;
debd256d 15072
a738430d 15073 /* Read in the header. */
6e70227d 15074 lh->total_length =
c764a876
DE
15075 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15076 &bytes_read, &offset_size);
debd256d 15077 line_ptr += bytes_read;
3019eac3 15078 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15079 {
4d3c2250 15080 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15081 return 0;
15082 }
15083 lh->statement_program_end = line_ptr + lh->total_length;
15084 lh->version = read_2_bytes (abfd, line_ptr);
15085 line_ptr += 2;
c764a876
DE
15086 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15087 line_ptr += offset_size;
debd256d
JB
15088 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15089 line_ptr += 1;
2dc7f7b3
TT
15090 if (lh->version >= 4)
15091 {
15092 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15093 line_ptr += 1;
15094 }
15095 else
15096 lh->maximum_ops_per_instruction = 1;
15097
15098 if (lh->maximum_ops_per_instruction == 0)
15099 {
15100 lh->maximum_ops_per_instruction = 1;
15101 complaint (&symfile_complaints,
3e43a32a
MS
15102 _("invalid maximum_ops_per_instruction "
15103 "in `.debug_line' section"));
2dc7f7b3
TT
15104 }
15105
debd256d
JB
15106 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15107 line_ptr += 1;
15108 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15109 line_ptr += 1;
15110 lh->line_range = read_1_byte (abfd, line_ptr);
15111 line_ptr += 1;
15112 lh->opcode_base = read_1_byte (abfd, line_ptr);
15113 line_ptr += 1;
15114 lh->standard_opcode_lengths
fe1b8b76 15115 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15116
15117 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15118 for (i = 1; i < lh->opcode_base; ++i)
15119 {
15120 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15121 line_ptr += 1;
15122 }
15123
a738430d 15124 /* Read directory table. */
9b1c24c8 15125 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15126 {
15127 line_ptr += bytes_read;
15128 add_include_dir (lh, cur_dir);
15129 }
15130 line_ptr += bytes_read;
15131
a738430d 15132 /* Read file name table. */
9b1c24c8 15133 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15134 {
15135 unsigned int dir_index, mod_time, length;
15136
15137 line_ptr += bytes_read;
15138 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15139 line_ptr += bytes_read;
15140 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15141 line_ptr += bytes_read;
15142 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15143 line_ptr += bytes_read;
15144
15145 add_file_name (lh, cur_file, dir_index, mod_time, length);
15146 }
15147 line_ptr += bytes_read;
6e70227d 15148 lh->statement_program_start = line_ptr;
debd256d 15149
3019eac3 15150 if (line_ptr > (section->buffer + section->size))
4d3c2250 15151 complaint (&symfile_complaints,
3e43a32a
MS
15152 _("line number info header doesn't "
15153 "fit in `.debug_line' section"));
debd256d
JB
15154
15155 discard_cleanups (back_to);
15156 return lh;
15157}
c906108c 15158
c6da4cef
DE
15159/* Subroutine of dwarf_decode_lines to simplify it.
15160 Return the file name of the psymtab for included file FILE_INDEX
15161 in line header LH of PST.
15162 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15163 If space for the result is malloc'd, it will be freed by a cleanup.
15164 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15165
15166static char *
15167psymtab_include_file_name (const struct line_header *lh, int file_index,
15168 const struct partial_symtab *pst,
15169 const char *comp_dir)
15170{
15171 const struct file_entry fe = lh->file_names [file_index];
15172 char *include_name = fe.name;
15173 char *include_name_to_compare = include_name;
15174 char *dir_name = NULL;
72b9f47f
TT
15175 const char *pst_filename;
15176 char *copied_name = NULL;
c6da4cef
DE
15177 int file_is_pst;
15178
15179 if (fe.dir_index)
15180 dir_name = lh->include_dirs[fe.dir_index - 1];
15181
15182 if (!IS_ABSOLUTE_PATH (include_name)
15183 && (dir_name != NULL || comp_dir != NULL))
15184 {
15185 /* Avoid creating a duplicate psymtab for PST.
15186 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15187 Before we do the comparison, however, we need to account
15188 for DIR_NAME and COMP_DIR.
15189 First prepend dir_name (if non-NULL). If we still don't
15190 have an absolute path prepend comp_dir (if non-NULL).
15191 However, the directory we record in the include-file's
15192 psymtab does not contain COMP_DIR (to match the
15193 corresponding symtab(s)).
15194
15195 Example:
15196
15197 bash$ cd /tmp
15198 bash$ gcc -g ./hello.c
15199 include_name = "hello.c"
15200 dir_name = "."
15201 DW_AT_comp_dir = comp_dir = "/tmp"
15202 DW_AT_name = "./hello.c" */
15203
15204 if (dir_name != NULL)
15205 {
15206 include_name = concat (dir_name, SLASH_STRING,
15207 include_name, (char *)NULL);
15208 include_name_to_compare = include_name;
15209 make_cleanup (xfree, include_name);
15210 }
15211 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15212 {
15213 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15214 include_name, (char *)NULL);
15215 }
15216 }
15217
15218 pst_filename = pst->filename;
15219 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15220 {
72b9f47f
TT
15221 copied_name = concat (pst->dirname, SLASH_STRING,
15222 pst_filename, (char *)NULL);
15223 pst_filename = copied_name;
c6da4cef
DE
15224 }
15225
1e3fad37 15226 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15227
15228 if (include_name_to_compare != include_name)
15229 xfree (include_name_to_compare);
72b9f47f
TT
15230 if (copied_name != NULL)
15231 xfree (copied_name);
c6da4cef
DE
15232
15233 if (file_is_pst)
15234 return NULL;
15235 return include_name;
15236}
15237
c91513d8
PP
15238/* Ignore this record_line request. */
15239
15240static void
15241noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15242{
15243 return;
15244}
15245
f3f5162e
DE
15246/* Subroutine of dwarf_decode_lines to simplify it.
15247 Process the line number information in LH. */
debd256d 15248
c906108c 15249static void
f3f5162e
DE
15250dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15251 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15252{
a8c50c1f 15253 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15254 gdb_byte *line_end;
a8c50c1f 15255 unsigned int bytes_read, extended_len;
c906108c 15256 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15257 CORE_ADDR baseaddr;
15258 struct objfile *objfile = cu->objfile;
f3f5162e 15259 bfd *abfd = objfile->obfd;
fbf65064 15260 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15261 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15262 struct subfile *last_subfile = NULL;
c91513d8
PP
15263 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15264 = record_line;
e142c38c
DJ
15265
15266 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15267
debd256d
JB
15268 line_ptr = lh->statement_program_start;
15269 line_end = lh->statement_program_end;
c906108c
SS
15270
15271 /* Read the statement sequences until there's nothing left. */
15272 while (line_ptr < line_end)
15273 {
15274 /* state machine registers */
15275 CORE_ADDR address = 0;
15276 unsigned int file = 1;
15277 unsigned int line = 1;
15278 unsigned int column = 0;
debd256d 15279 int is_stmt = lh->default_is_stmt;
c906108c
SS
15280 int basic_block = 0;
15281 int end_sequence = 0;
fbf65064 15282 CORE_ADDR addr;
2dc7f7b3 15283 unsigned char op_index = 0;
c906108c 15284
aaa75496 15285 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15286 {
aaa75496 15287 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15288 /* lh->include_dirs and lh->file_names are 0-based, but the
15289 directory and file name numbers in the statement program
15290 are 1-based. */
15291 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15292 char *dir = NULL;
a738430d 15293
debd256d
JB
15294 if (fe->dir_index)
15295 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15296
15297 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15298 }
15299
a738430d 15300 /* Decode the table. */
c5aa993b 15301 while (!end_sequence)
c906108c
SS
15302 {
15303 op_code = read_1_byte (abfd, line_ptr);
15304 line_ptr += 1;
59205f5a
JB
15305 if (line_ptr > line_end)
15306 {
15307 dwarf2_debug_line_missing_end_sequence_complaint ();
15308 break;
15309 }
9aa1fe7e 15310
debd256d 15311 if (op_code >= lh->opcode_base)
6e70227d 15312 {
a738430d 15313 /* Special operand. */
debd256d 15314 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15315 address += (((op_index + (adj_opcode / lh->line_range))
15316 / lh->maximum_ops_per_instruction)
15317 * lh->minimum_instruction_length);
15318 op_index = ((op_index + (adj_opcode / lh->line_range))
15319 % lh->maximum_ops_per_instruction);
debd256d 15320 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15321 if (lh->num_file_names < file || file == 0)
25e43795 15322 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15323 /* For now we ignore lines not starting on an
15324 instruction boundary. */
15325 else if (op_index == 0)
25e43795
DJ
15326 {
15327 lh->file_names[file - 1].included_p = 1;
ca5f395d 15328 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15329 {
15330 if (last_subfile != current_subfile)
15331 {
15332 addr = gdbarch_addr_bits_remove (gdbarch, address);
15333 if (last_subfile)
c91513d8 15334 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15335 last_subfile = current_subfile;
15336 }
25e43795 15337 /* Append row to matrix using current values. */
7019d805 15338 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15339 (*p_record_line) (current_subfile, line, addr);
366da635 15340 }
25e43795 15341 }
ca5f395d 15342 basic_block = 0;
9aa1fe7e
GK
15343 }
15344 else switch (op_code)
c906108c
SS
15345 {
15346 case DW_LNS_extended_op:
3e43a32a
MS
15347 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15348 &bytes_read);
473b7be6 15349 line_ptr += bytes_read;
a8c50c1f 15350 extended_end = line_ptr + extended_len;
c906108c
SS
15351 extended_op = read_1_byte (abfd, line_ptr);
15352 line_ptr += 1;
15353 switch (extended_op)
15354 {
15355 case DW_LNE_end_sequence:
c91513d8 15356 p_record_line = record_line;
c906108c 15357 end_sequence = 1;
c906108c
SS
15358 break;
15359 case DW_LNE_set_address:
e7c27a73 15360 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15361
15362 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15363 {
15364 /* This line table is for a function which has been
15365 GCd by the linker. Ignore it. PR gdb/12528 */
15366
15367 long line_offset
36586728 15368 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15369
15370 complaint (&symfile_complaints,
15371 _(".debug_line address at offset 0x%lx is 0 "
15372 "[in module %s]"),
bb5ed363 15373 line_offset, objfile->name);
c91513d8
PP
15374 p_record_line = noop_record_line;
15375 }
15376
2dc7f7b3 15377 op_index = 0;
107d2387
AC
15378 line_ptr += bytes_read;
15379 address += baseaddr;
c906108c
SS
15380 break;
15381 case DW_LNE_define_file:
debd256d
JB
15382 {
15383 char *cur_file;
15384 unsigned int dir_index, mod_time, length;
6e70227d 15385
3e43a32a
MS
15386 cur_file = read_direct_string (abfd, line_ptr,
15387 &bytes_read);
debd256d
JB
15388 line_ptr += bytes_read;
15389 dir_index =
15390 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15391 line_ptr += bytes_read;
15392 mod_time =
15393 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15394 line_ptr += bytes_read;
15395 length =
15396 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15397 line_ptr += bytes_read;
15398 add_file_name (lh, cur_file, dir_index, mod_time, length);
15399 }
c906108c 15400 break;
d0c6ba3d
CC
15401 case DW_LNE_set_discriminator:
15402 /* The discriminator is not interesting to the debugger;
15403 just ignore it. */
15404 line_ptr = extended_end;
15405 break;
c906108c 15406 default:
4d3c2250 15407 complaint (&symfile_complaints,
e2e0b3e5 15408 _("mangled .debug_line section"));
debd256d 15409 return;
c906108c 15410 }
a8c50c1f
DJ
15411 /* Make sure that we parsed the extended op correctly. If e.g.
15412 we expected a different address size than the producer used,
15413 we may have read the wrong number of bytes. */
15414 if (line_ptr != extended_end)
15415 {
15416 complaint (&symfile_complaints,
15417 _("mangled .debug_line section"));
15418 return;
15419 }
c906108c
SS
15420 break;
15421 case DW_LNS_copy:
59205f5a 15422 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15423 dwarf2_debug_line_missing_file_complaint ();
15424 else
366da635 15425 {
25e43795 15426 lh->file_names[file - 1].included_p = 1;
ca5f395d 15427 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15428 {
15429 if (last_subfile != current_subfile)
15430 {
15431 addr = gdbarch_addr_bits_remove (gdbarch, address);
15432 if (last_subfile)
c91513d8 15433 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15434 last_subfile = current_subfile;
15435 }
7019d805 15436 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15437 (*p_record_line) (current_subfile, line, addr);
fbf65064 15438 }
366da635 15439 }
c906108c
SS
15440 basic_block = 0;
15441 break;
15442 case DW_LNS_advance_pc:
2dc7f7b3
TT
15443 {
15444 CORE_ADDR adjust
15445 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15446
15447 address += (((op_index + adjust)
15448 / lh->maximum_ops_per_instruction)
15449 * lh->minimum_instruction_length);
15450 op_index = ((op_index + adjust)
15451 % lh->maximum_ops_per_instruction);
15452 line_ptr += bytes_read;
15453 }
c906108c
SS
15454 break;
15455 case DW_LNS_advance_line:
15456 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15457 line_ptr += bytes_read;
15458 break;
15459 case DW_LNS_set_file:
debd256d 15460 {
a738430d
MK
15461 /* The arrays lh->include_dirs and lh->file_names are
15462 0-based, but the directory and file name numbers in
15463 the statement program are 1-based. */
debd256d 15464 struct file_entry *fe;
4f1520fb 15465 char *dir = NULL;
a738430d 15466
debd256d
JB
15467 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15468 line_ptr += bytes_read;
59205f5a 15469 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15470 dwarf2_debug_line_missing_file_complaint ();
15471 else
15472 {
15473 fe = &lh->file_names[file - 1];
15474 if (fe->dir_index)
15475 dir = lh->include_dirs[fe->dir_index - 1];
15476 if (!decode_for_pst_p)
15477 {
15478 last_subfile = current_subfile;
15479 dwarf2_start_subfile (fe->name, dir, comp_dir);
15480 }
15481 }
debd256d 15482 }
c906108c
SS
15483 break;
15484 case DW_LNS_set_column:
15485 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15486 line_ptr += bytes_read;
15487 break;
15488 case DW_LNS_negate_stmt:
15489 is_stmt = (!is_stmt);
15490 break;
15491 case DW_LNS_set_basic_block:
15492 basic_block = 1;
15493 break;
c2c6d25f
JM
15494 /* Add to the address register of the state machine the
15495 address increment value corresponding to special opcode
a738430d
MK
15496 255. I.e., this value is scaled by the minimum
15497 instruction length since special opcode 255 would have
b021a221 15498 scaled the increment. */
c906108c 15499 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15500 {
15501 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15502
15503 address += (((op_index + adjust)
15504 / lh->maximum_ops_per_instruction)
15505 * lh->minimum_instruction_length);
15506 op_index = ((op_index + adjust)
15507 % lh->maximum_ops_per_instruction);
15508 }
c906108c
SS
15509 break;
15510 case DW_LNS_fixed_advance_pc:
15511 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15512 op_index = 0;
c906108c
SS
15513 line_ptr += 2;
15514 break;
9aa1fe7e 15515 default:
a738430d
MK
15516 {
15517 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15518 int i;
a738430d 15519
debd256d 15520 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15521 {
15522 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15523 line_ptr += bytes_read;
15524 }
15525 }
c906108c
SS
15526 }
15527 }
59205f5a
JB
15528 if (lh->num_file_names < file || file == 0)
15529 dwarf2_debug_line_missing_file_complaint ();
15530 else
15531 {
15532 lh->file_names[file - 1].included_p = 1;
15533 if (!decode_for_pst_p)
fbf65064
UW
15534 {
15535 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15536 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15537 }
59205f5a 15538 }
c906108c 15539 }
f3f5162e
DE
15540}
15541
15542/* Decode the Line Number Program (LNP) for the given line_header
15543 structure and CU. The actual information extracted and the type
15544 of structures created from the LNP depends on the value of PST.
15545
15546 1. If PST is NULL, then this procedure uses the data from the program
15547 to create all necessary symbol tables, and their linetables.
15548
15549 2. If PST is not NULL, this procedure reads the program to determine
15550 the list of files included by the unit represented by PST, and
15551 builds all the associated partial symbol tables.
15552
15553 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15554 It is used for relative paths in the line table.
15555 NOTE: When processing partial symtabs (pst != NULL),
15556 comp_dir == pst->dirname.
15557
15558 NOTE: It is important that psymtabs have the same file name (via strcmp)
15559 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15560 symtab we don't use it in the name of the psymtabs we create.
15561 E.g. expand_line_sal requires this when finding psymtabs to expand.
15562 A good testcase for this is mb-inline.exp. */
15563
15564static void
15565dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15566 struct dwarf2_cu *cu, struct partial_symtab *pst,
15567 int want_line_info)
15568{
15569 struct objfile *objfile = cu->objfile;
15570 const int decode_for_pst_p = (pst != NULL);
15571 struct subfile *first_subfile = current_subfile;
15572
15573 if (want_line_info)
15574 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15575
15576 if (decode_for_pst_p)
15577 {
15578 int file_index;
15579
15580 /* Now that we're done scanning the Line Header Program, we can
15581 create the psymtab of each included file. */
15582 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15583 if (lh->file_names[file_index].included_p == 1)
15584 {
c6da4cef
DE
15585 char *include_name =
15586 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15587 if (include_name != NULL)
aaa75496
JB
15588 dwarf2_create_include_psymtab (include_name, pst, objfile);
15589 }
15590 }
cb1df416
DJ
15591 else
15592 {
15593 /* Make sure a symtab is created for every file, even files
15594 which contain only variables (i.e. no code with associated
15595 line numbers). */
cb1df416 15596 int i;
cb1df416
DJ
15597
15598 for (i = 0; i < lh->num_file_names; i++)
15599 {
15600 char *dir = NULL;
f3f5162e 15601 struct file_entry *fe;
9a619af0 15602
cb1df416
DJ
15603 fe = &lh->file_names[i];
15604 if (fe->dir_index)
15605 dir = lh->include_dirs[fe->dir_index - 1];
15606 dwarf2_start_subfile (fe->name, dir, comp_dir);
15607
15608 /* Skip the main file; we don't need it, and it must be
15609 allocated last, so that it will show up before the
15610 non-primary symtabs in the objfile's symtab list. */
15611 if (current_subfile == first_subfile)
15612 continue;
15613
15614 if (current_subfile->symtab == NULL)
15615 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15616 objfile);
cb1df416
DJ
15617 fe->symtab = current_subfile->symtab;
15618 }
15619 }
c906108c
SS
15620}
15621
15622/* Start a subfile for DWARF. FILENAME is the name of the file and
15623 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15624 or NULL if not known. COMP_DIR is the compilation directory for the
15625 linetable's compilation unit or NULL if not known.
c906108c
SS
15626 This routine tries to keep line numbers from identical absolute and
15627 relative file names in a common subfile.
15628
15629 Using the `list' example from the GDB testsuite, which resides in
15630 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15631 of /srcdir/list0.c yields the following debugging information for list0.c:
15632
c5aa993b
JM
15633 DW_AT_name: /srcdir/list0.c
15634 DW_AT_comp_dir: /compdir
357e46e7 15635 files.files[0].name: list0.h
c5aa993b 15636 files.files[0].dir: /srcdir
357e46e7 15637 files.files[1].name: list0.c
c5aa993b 15638 files.files[1].dir: /srcdir
c906108c
SS
15639
15640 The line number information for list0.c has to end up in a single
4f1520fb
FR
15641 subfile, so that `break /srcdir/list0.c:1' works as expected.
15642 start_subfile will ensure that this happens provided that we pass the
15643 concatenation of files.files[1].dir and files.files[1].name as the
15644 subfile's name. */
c906108c
SS
15645
15646static void
3e43a32a
MS
15647dwarf2_start_subfile (char *filename, const char *dirname,
15648 const char *comp_dir)
c906108c 15649{
4f1520fb
FR
15650 char *fullname;
15651
15652 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15653 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15654 second argument to start_subfile. To be consistent, we do the
15655 same here. In order not to lose the line information directory,
15656 we concatenate it to the filename when it makes sense.
15657 Note that the Dwarf3 standard says (speaking of filenames in line
15658 information): ``The directory index is ignored for file names
15659 that represent full path names''. Thus ignoring dirname in the
15660 `else' branch below isn't an issue. */
c906108c 15661
d5166ae1 15662 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15663 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15664 else
15665 fullname = filename;
c906108c 15666
4f1520fb
FR
15667 start_subfile (fullname, comp_dir);
15668
15669 if (fullname != filename)
15670 xfree (fullname);
c906108c
SS
15671}
15672
f4dc4d17
DE
15673/* Start a symtab for DWARF.
15674 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15675
15676static void
15677dwarf2_start_symtab (struct dwarf2_cu *cu,
15678 char *name, char *comp_dir, CORE_ADDR low_pc)
15679{
15680 start_symtab (name, comp_dir, low_pc);
15681 record_debugformat ("DWARF 2");
15682 record_producer (cu->producer);
15683
15684 /* We assume that we're processing GCC output. */
15685 processing_gcc_compilation = 2;
15686
15687 processing_has_namespace_info = 0;
15688}
15689
4c2df51b
DJ
15690static void
15691var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15692 struct dwarf2_cu *cu)
4c2df51b 15693{
e7c27a73
DJ
15694 struct objfile *objfile = cu->objfile;
15695 struct comp_unit_head *cu_header = &cu->header;
15696
4c2df51b
DJ
15697 /* NOTE drow/2003-01-30: There used to be a comment and some special
15698 code here to turn a symbol with DW_AT_external and a
15699 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15700 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15701 with some versions of binutils) where shared libraries could have
15702 relocations against symbols in their debug information - the
15703 minimal symbol would have the right address, but the debug info
15704 would not. It's no longer necessary, because we will explicitly
15705 apply relocations when we read in the debug information now. */
15706
15707 /* A DW_AT_location attribute with no contents indicates that a
15708 variable has been optimized away. */
15709 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15710 {
15711 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15712 return;
15713 }
15714
15715 /* Handle one degenerate form of location expression specially, to
15716 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15717 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15718 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15719
15720 if (attr_form_is_block (attr)
3019eac3
DE
15721 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15722 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15723 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15724 && (DW_BLOCK (attr)->size
15725 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15726 {
891d2f0b 15727 unsigned int dummy;
4c2df51b 15728
3019eac3
DE
15729 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15730 SYMBOL_VALUE_ADDRESS (sym) =
15731 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15732 else
15733 SYMBOL_VALUE_ADDRESS (sym) =
15734 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15735 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15736 fixup_symbol_section (sym, objfile);
15737 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15738 SYMBOL_SECTION (sym));
4c2df51b
DJ
15739 return;
15740 }
15741
15742 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15743 expression evaluator, and use LOC_COMPUTED only when necessary
15744 (i.e. when the value of a register or memory location is
15745 referenced, or a thread-local block, etc.). Then again, it might
15746 not be worthwhile. I'm assuming that it isn't unless performance
15747 or memory numbers show me otherwise. */
15748
e7c27a73 15749 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15750 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15751
15752 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15753 cu->has_loclist = 1;
4c2df51b
DJ
15754}
15755
c906108c
SS
15756/* Given a pointer to a DWARF information entry, figure out if we need
15757 to make a symbol table entry for it, and if so, create a new entry
15758 and return a pointer to it.
15759 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15760 used the passed type.
15761 If SPACE is not NULL, use it to hold the new symbol. If it is
15762 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15763
15764static struct symbol *
34eaf542
TT
15765new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15766 struct symbol *space)
c906108c 15767{
e7c27a73 15768 struct objfile *objfile = cu->objfile;
c906108c
SS
15769 struct symbol *sym = NULL;
15770 char *name;
15771 struct attribute *attr = NULL;
15772 struct attribute *attr2 = NULL;
e142c38c 15773 CORE_ADDR baseaddr;
e37fd15a
SW
15774 struct pending **list_to_add = NULL;
15775
edb3359d 15776 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15777
15778 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15779
94af9270 15780 name = dwarf2_name (die, cu);
c906108c
SS
15781 if (name)
15782 {
94af9270 15783 const char *linkagename;
34eaf542 15784 int suppress_add = 0;
94af9270 15785
34eaf542
TT
15786 if (space)
15787 sym = space;
15788 else
15789 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15790 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15791
15792 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15793 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15794 linkagename = dwarf2_physname (name, die, cu);
15795 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15796
f55ee35c
JK
15797 /* Fortran does not have mangling standard and the mangling does differ
15798 between gfortran, iFort etc. */
15799 if (cu->language == language_fortran
b250c185 15800 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
15801 symbol_set_demangled_name (&(sym->ginfo),
15802 (char *) dwarf2_full_name (name, die, cu),
15803 NULL);
f55ee35c 15804
c906108c 15805 /* Default assumptions.
c5aa993b 15806 Use the passed type or decode it from the die. */
176620f1 15807 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15808 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15809 if (type != NULL)
15810 SYMBOL_TYPE (sym) = type;
15811 else
e7c27a73 15812 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15813 attr = dwarf2_attr (die,
15814 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15815 cu);
c906108c
SS
15816 if (attr)
15817 {
15818 SYMBOL_LINE (sym) = DW_UNSND (attr);
15819 }
cb1df416 15820
edb3359d
DJ
15821 attr = dwarf2_attr (die,
15822 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15823 cu);
cb1df416
DJ
15824 if (attr)
15825 {
15826 int file_index = DW_UNSND (attr);
9a619af0 15827
cb1df416
DJ
15828 if (cu->line_header == NULL
15829 || file_index > cu->line_header->num_file_names)
15830 complaint (&symfile_complaints,
15831 _("file index out of range"));
1c3d648d 15832 else if (file_index > 0)
cb1df416
DJ
15833 {
15834 struct file_entry *fe;
9a619af0 15835
cb1df416
DJ
15836 fe = &cu->line_header->file_names[file_index - 1];
15837 SYMBOL_SYMTAB (sym) = fe->symtab;
15838 }
15839 }
15840
c906108c
SS
15841 switch (die->tag)
15842 {
15843 case DW_TAG_label:
e142c38c 15844 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15845 if (attr)
15846 {
15847 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15848 }
0f5238ed
TT
15849 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15850 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15851 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15852 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15853 break;
15854 case DW_TAG_subprogram:
15855 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15856 finish_block. */
15857 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15858 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15859 if ((attr2 && (DW_UNSND (attr2) != 0))
15860 || cu->language == language_ada)
c906108c 15861 {
2cfa0c8d
JB
15862 /* Subprograms marked external are stored as a global symbol.
15863 Ada subprograms, whether marked external or not, are always
15864 stored as a global symbol, because we want to be able to
15865 access them globally. For instance, we want to be able
15866 to break on a nested subprogram without having to
15867 specify the context. */
e37fd15a 15868 list_to_add = &global_symbols;
c906108c
SS
15869 }
15870 else
15871 {
e37fd15a 15872 list_to_add = cu->list_in_scope;
c906108c
SS
15873 }
15874 break;
edb3359d
DJ
15875 case DW_TAG_inlined_subroutine:
15876 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15877 finish_block. */
15878 SYMBOL_CLASS (sym) = LOC_BLOCK;
15879 SYMBOL_INLINED (sym) = 1;
481860b3 15880 list_to_add = cu->list_in_scope;
edb3359d 15881 break;
34eaf542
TT
15882 case DW_TAG_template_value_param:
15883 suppress_add = 1;
15884 /* Fall through. */
72929c62 15885 case DW_TAG_constant:
c906108c 15886 case DW_TAG_variable:
254e6b9e 15887 case DW_TAG_member:
0963b4bd
MS
15888 /* Compilation with minimal debug info may result in
15889 variables with missing type entries. Change the
15890 misleading `void' type to something sensible. */
c906108c 15891 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15892 SYMBOL_TYPE (sym)
46bf5051 15893 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15894
e142c38c 15895 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15896 /* In the case of DW_TAG_member, we should only be called for
15897 static const members. */
15898 if (die->tag == DW_TAG_member)
15899 {
3863f96c
DE
15900 /* dwarf2_add_field uses die_is_declaration,
15901 so we do the same. */
254e6b9e
DE
15902 gdb_assert (die_is_declaration (die, cu));
15903 gdb_assert (attr);
15904 }
c906108c
SS
15905 if (attr)
15906 {
e7c27a73 15907 dwarf2_const_value (attr, sym, cu);
e142c38c 15908 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15909 if (!suppress_add)
34eaf542
TT
15910 {
15911 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15912 list_to_add = &global_symbols;
34eaf542 15913 else
e37fd15a 15914 list_to_add = cu->list_in_scope;
34eaf542 15915 }
c906108c
SS
15916 break;
15917 }
e142c38c 15918 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15919 if (attr)
15920 {
e7c27a73 15921 var_decode_location (attr, sym, cu);
e142c38c 15922 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15923
15924 /* Fortran explicitly imports any global symbols to the local
15925 scope by DW_TAG_common_block. */
15926 if (cu->language == language_fortran && die->parent
15927 && die->parent->tag == DW_TAG_common_block)
15928 attr2 = NULL;
15929
caac4577
JG
15930 if (SYMBOL_CLASS (sym) == LOC_STATIC
15931 && SYMBOL_VALUE_ADDRESS (sym) == 0
15932 && !dwarf2_per_objfile->has_section_at_zero)
15933 {
15934 /* When a static variable is eliminated by the linker,
15935 the corresponding debug information is not stripped
15936 out, but the variable address is set to null;
15937 do not add such variables into symbol table. */
15938 }
15939 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15940 {
f55ee35c
JK
15941 /* Workaround gfortran PR debug/40040 - it uses
15942 DW_AT_location for variables in -fPIC libraries which may
15943 get overriden by other libraries/executable and get
15944 a different address. Resolve it by the minimal symbol
15945 which may come from inferior's executable using copy
15946 relocation. Make this workaround only for gfortran as for
15947 other compilers GDB cannot guess the minimal symbol
15948 Fortran mangling kind. */
15949 if (cu->language == language_fortran && die->parent
15950 && die->parent->tag == DW_TAG_module
15951 && cu->producer
15952 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15953 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15954
1c809c68
TT
15955 /* A variable with DW_AT_external is never static,
15956 but it may be block-scoped. */
15957 list_to_add = (cu->list_in_scope == &file_symbols
15958 ? &global_symbols : cu->list_in_scope);
1c809c68 15959 }
c906108c 15960 else
e37fd15a 15961 list_to_add = cu->list_in_scope;
c906108c
SS
15962 }
15963 else
15964 {
15965 /* We do not know the address of this symbol.
c5aa993b
JM
15966 If it is an external symbol and we have type information
15967 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15968 The address of the variable will then be determined from
15969 the minimal symbol table whenever the variable is
15970 referenced. */
e142c38c 15971 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15972
15973 /* Fortran explicitly imports any global symbols to the local
15974 scope by DW_TAG_common_block. */
15975 if (cu->language == language_fortran && die->parent
15976 && die->parent->tag == DW_TAG_common_block)
15977 {
15978 /* SYMBOL_CLASS doesn't matter here because
15979 read_common_block is going to reset it. */
15980 if (!suppress_add)
15981 list_to_add = cu->list_in_scope;
15982 }
15983 else if (attr2 && (DW_UNSND (attr2) != 0)
15984 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15985 {
0fe7935b
DJ
15986 /* A variable with DW_AT_external is never static, but it
15987 may be block-scoped. */
15988 list_to_add = (cu->list_in_scope == &file_symbols
15989 ? &global_symbols : cu->list_in_scope);
15990
c906108c 15991 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15992 }
442ddf59
JK
15993 else if (!die_is_declaration (die, cu))
15994 {
15995 /* Use the default LOC_OPTIMIZED_OUT class. */
15996 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15997 if (!suppress_add)
15998 list_to_add = cu->list_in_scope;
442ddf59 15999 }
c906108c
SS
16000 }
16001 break;
16002 case DW_TAG_formal_parameter:
edb3359d
DJ
16003 /* If we are inside a function, mark this as an argument. If
16004 not, we might be looking at an argument to an inlined function
16005 when we do not have enough information to show inlined frames;
16006 pretend it's a local variable in that case so that the user can
16007 still see it. */
16008 if (context_stack_depth > 0
16009 && context_stack[context_stack_depth - 1].name != NULL)
16010 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16011 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16012 if (attr)
16013 {
e7c27a73 16014 var_decode_location (attr, sym, cu);
c906108c 16015 }
e142c38c 16016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16017 if (attr)
16018 {
e7c27a73 16019 dwarf2_const_value (attr, sym, cu);
c906108c 16020 }
f346a30d 16021
e37fd15a 16022 list_to_add = cu->list_in_scope;
c906108c
SS
16023 break;
16024 case DW_TAG_unspecified_parameters:
16025 /* From varargs functions; gdb doesn't seem to have any
16026 interest in this information, so just ignore it for now.
16027 (FIXME?) */
16028 break;
34eaf542
TT
16029 case DW_TAG_template_type_param:
16030 suppress_add = 1;
16031 /* Fall through. */
c906108c 16032 case DW_TAG_class_type:
680b30c7 16033 case DW_TAG_interface_type:
c906108c
SS
16034 case DW_TAG_structure_type:
16035 case DW_TAG_union_type:
72019c9c 16036 case DW_TAG_set_type:
c906108c
SS
16037 case DW_TAG_enumeration_type:
16038 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16039 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16040
63d06c5c 16041 {
987504bb 16042 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16043 really ever be static objects: otherwise, if you try
16044 to, say, break of a class's method and you're in a file
16045 which doesn't mention that class, it won't work unless
16046 the check for all static symbols in lookup_symbol_aux
16047 saves you. See the OtherFileClass tests in
16048 gdb.c++/namespace.exp. */
16049
e37fd15a 16050 if (!suppress_add)
34eaf542 16051 {
34eaf542
TT
16052 list_to_add = (cu->list_in_scope == &file_symbols
16053 && (cu->language == language_cplus
16054 || cu->language == language_java)
16055 ? &global_symbols : cu->list_in_scope);
63d06c5c 16056
64382290
TT
16057 /* The semantics of C++ state that "struct foo {
16058 ... }" also defines a typedef for "foo". A Java
16059 class declaration also defines a typedef for the
16060 class. */
16061 if (cu->language == language_cplus
16062 || cu->language == language_java
16063 || cu->language == language_ada)
16064 {
16065 /* The symbol's name is already allocated along
16066 with this objfile, so we don't need to
16067 duplicate it for the type. */
16068 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16069 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16070 }
63d06c5c
DC
16071 }
16072 }
c906108c
SS
16073 break;
16074 case DW_TAG_typedef:
63d06c5c
DC
16075 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16076 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16077 list_to_add = cu->list_in_scope;
63d06c5c 16078 break;
c906108c 16079 case DW_TAG_base_type:
a02abb62 16080 case DW_TAG_subrange_type:
c906108c 16081 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16082 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16083 list_to_add = cu->list_in_scope;
c906108c
SS
16084 break;
16085 case DW_TAG_enumerator:
e142c38c 16086 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16087 if (attr)
16088 {
e7c27a73 16089 dwarf2_const_value (attr, sym, cu);
c906108c 16090 }
63d06c5c
DC
16091 {
16092 /* NOTE: carlton/2003-11-10: See comment above in the
16093 DW_TAG_class_type, etc. block. */
16094
e142c38c 16095 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16096 && (cu->language == language_cplus
16097 || cu->language == language_java)
e142c38c 16098 ? &global_symbols : cu->list_in_scope);
63d06c5c 16099 }
c906108c 16100 break;
5c4e30ca
DC
16101 case DW_TAG_namespace:
16102 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 16103 list_to_add = &global_symbols;
5c4e30ca 16104 break;
4357ac6c
TT
16105 case DW_TAG_common_block:
16106 SYMBOL_CLASS (sym) = LOC_STATIC;
16107 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16108 add_symbol_to_list (sym, cu->list_in_scope);
16109 break;
c906108c
SS
16110 default:
16111 /* Not a tag we recognize. Hopefully we aren't processing
16112 trash data, but since we must specifically ignore things
16113 we don't recognize, there is nothing else we should do at
0963b4bd 16114 this point. */
e2e0b3e5 16115 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16116 dwarf_tag_name (die->tag));
c906108c
SS
16117 break;
16118 }
df8a16a1 16119
e37fd15a
SW
16120 if (suppress_add)
16121 {
16122 sym->hash_next = objfile->template_symbols;
16123 objfile->template_symbols = sym;
16124 list_to_add = NULL;
16125 }
16126
16127 if (list_to_add != NULL)
16128 add_symbol_to_list (sym, list_to_add);
16129
df8a16a1
DJ
16130 /* For the benefit of old versions of GCC, check for anonymous
16131 namespaces based on the demangled name. */
16132 if (!processing_has_namespace_info
94af9270 16133 && cu->language == language_cplus)
a10964d1 16134 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16135 }
16136 return (sym);
16137}
16138
34eaf542
TT
16139/* A wrapper for new_symbol_full that always allocates a new symbol. */
16140
16141static struct symbol *
16142new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16143{
16144 return new_symbol_full (die, type, cu, NULL);
16145}
16146
98bfdba5
PA
16147/* Given an attr with a DW_FORM_dataN value in host byte order,
16148 zero-extend it as appropriate for the symbol's type. The DWARF
16149 standard (v4) is not entirely clear about the meaning of using
16150 DW_FORM_dataN for a constant with a signed type, where the type is
16151 wider than the data. The conclusion of a discussion on the DWARF
16152 list was that this is unspecified. We choose to always zero-extend
16153 because that is the interpretation long in use by GCC. */
c906108c 16154
98bfdba5
PA
16155static gdb_byte *
16156dwarf2_const_value_data (struct attribute *attr, struct type *type,
16157 const char *name, struct obstack *obstack,
12df843f 16158 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16159{
e7c27a73 16160 struct objfile *objfile = cu->objfile;
e17a4113
UW
16161 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16162 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16163 LONGEST l = DW_UNSND (attr);
16164
16165 if (bits < sizeof (*value) * 8)
16166 {
16167 l &= ((LONGEST) 1 << bits) - 1;
16168 *value = l;
16169 }
16170 else if (bits == sizeof (*value) * 8)
16171 *value = l;
16172 else
16173 {
16174 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16175 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16176 return bytes;
16177 }
16178
16179 return NULL;
16180}
16181
16182/* Read a constant value from an attribute. Either set *VALUE, or if
16183 the value does not fit in *VALUE, set *BYTES - either already
16184 allocated on the objfile obstack, or newly allocated on OBSTACK,
16185 or, set *BATON, if we translated the constant to a location
16186 expression. */
16187
16188static void
16189dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16190 const char *name, struct obstack *obstack,
16191 struct dwarf2_cu *cu,
12df843f 16192 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16193 struct dwarf2_locexpr_baton **baton)
16194{
16195 struct objfile *objfile = cu->objfile;
16196 struct comp_unit_head *cu_header = &cu->header;
c906108c 16197 struct dwarf_block *blk;
98bfdba5
PA
16198 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16199 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16200
16201 *value = 0;
16202 *bytes = NULL;
16203 *baton = NULL;
c906108c
SS
16204
16205 switch (attr->form)
16206 {
16207 case DW_FORM_addr:
3019eac3 16208 case DW_FORM_GNU_addr_index:
ac56253d 16209 {
ac56253d
TT
16210 gdb_byte *data;
16211
98bfdba5
PA
16212 if (TYPE_LENGTH (type) != cu_header->addr_size)
16213 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16214 cu_header->addr_size,
98bfdba5 16215 TYPE_LENGTH (type));
ac56253d
TT
16216 /* Symbols of this form are reasonably rare, so we just
16217 piggyback on the existing location code rather than writing
16218 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16219 *baton = obstack_alloc (&objfile->objfile_obstack,
16220 sizeof (struct dwarf2_locexpr_baton));
16221 (*baton)->per_cu = cu->per_cu;
16222 gdb_assert ((*baton)->per_cu);
ac56253d 16223
98bfdba5
PA
16224 (*baton)->size = 2 + cu_header->addr_size;
16225 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16226 (*baton)->data = data;
ac56253d
TT
16227
16228 data[0] = DW_OP_addr;
16229 store_unsigned_integer (&data[1], cu_header->addr_size,
16230 byte_order, DW_ADDR (attr));
16231 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16232 }
c906108c 16233 break;
4ac36638 16234 case DW_FORM_string:
93b5768b 16235 case DW_FORM_strp:
3019eac3 16236 case DW_FORM_GNU_str_index:
36586728 16237 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16238 /* DW_STRING is already allocated on the objfile obstack, point
16239 directly to it. */
16240 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16241 break;
c906108c
SS
16242 case DW_FORM_block1:
16243 case DW_FORM_block2:
16244 case DW_FORM_block4:
16245 case DW_FORM_block:
2dc7f7b3 16246 case DW_FORM_exprloc:
c906108c 16247 blk = DW_BLOCK (attr);
98bfdba5
PA
16248 if (TYPE_LENGTH (type) != blk->size)
16249 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16250 TYPE_LENGTH (type));
16251 *bytes = blk->data;
c906108c 16252 break;
2df3850c
JM
16253
16254 /* The DW_AT_const_value attributes are supposed to carry the
16255 symbol's value "represented as it would be on the target
16256 architecture." By the time we get here, it's already been
16257 converted to host endianness, so we just need to sign- or
16258 zero-extend it as appropriate. */
16259 case DW_FORM_data1:
3e43a32a
MS
16260 *bytes = dwarf2_const_value_data (attr, type, name,
16261 obstack, cu, value, 8);
2df3850c 16262 break;
c906108c 16263 case DW_FORM_data2:
3e43a32a
MS
16264 *bytes = dwarf2_const_value_data (attr, type, name,
16265 obstack, cu, value, 16);
2df3850c 16266 break;
c906108c 16267 case DW_FORM_data4:
3e43a32a
MS
16268 *bytes = dwarf2_const_value_data (attr, type, name,
16269 obstack, cu, value, 32);
2df3850c 16270 break;
c906108c 16271 case DW_FORM_data8:
3e43a32a
MS
16272 *bytes = dwarf2_const_value_data (attr, type, name,
16273 obstack, cu, value, 64);
2df3850c
JM
16274 break;
16275
c906108c 16276 case DW_FORM_sdata:
98bfdba5 16277 *value = DW_SND (attr);
2df3850c
JM
16278 break;
16279
c906108c 16280 case DW_FORM_udata:
98bfdba5 16281 *value = DW_UNSND (attr);
c906108c 16282 break;
2df3850c 16283
c906108c 16284 default:
4d3c2250 16285 complaint (&symfile_complaints,
e2e0b3e5 16286 _("unsupported const value attribute form: '%s'"),
4d3c2250 16287 dwarf_form_name (attr->form));
98bfdba5 16288 *value = 0;
c906108c
SS
16289 break;
16290 }
16291}
16292
2df3850c 16293
98bfdba5
PA
16294/* Copy constant value from an attribute to a symbol. */
16295
2df3850c 16296static void
98bfdba5
PA
16297dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16298 struct dwarf2_cu *cu)
2df3850c 16299{
98bfdba5
PA
16300 struct objfile *objfile = cu->objfile;
16301 struct comp_unit_head *cu_header = &cu->header;
12df843f 16302 LONGEST value;
98bfdba5
PA
16303 gdb_byte *bytes;
16304 struct dwarf2_locexpr_baton *baton;
2df3850c 16305
98bfdba5
PA
16306 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16307 SYMBOL_PRINT_NAME (sym),
16308 &objfile->objfile_obstack, cu,
16309 &value, &bytes, &baton);
2df3850c 16310
98bfdba5
PA
16311 if (baton != NULL)
16312 {
16313 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16314 SYMBOL_LOCATION_BATON (sym) = baton;
16315 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16316 }
16317 else if (bytes != NULL)
16318 {
16319 SYMBOL_VALUE_BYTES (sym) = bytes;
16320 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16321 }
16322 else
16323 {
16324 SYMBOL_VALUE (sym) = value;
16325 SYMBOL_CLASS (sym) = LOC_CONST;
16326 }
2df3850c
JM
16327}
16328
c906108c
SS
16329/* Return the type of the die in question using its DW_AT_type attribute. */
16330
16331static struct type *
e7c27a73 16332die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16333{
c906108c 16334 struct attribute *type_attr;
c906108c 16335
e142c38c 16336 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16337 if (!type_attr)
16338 {
16339 /* A missing DW_AT_type represents a void type. */
46bf5051 16340 return objfile_type (cu->objfile)->builtin_void;
c906108c 16341 }
348e048f 16342
673bfd45 16343 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16344}
16345
b4ba55a1
JB
16346/* True iff CU's producer generates GNAT Ada auxiliary information
16347 that allows to find parallel types through that information instead
16348 of having to do expensive parallel lookups by type name. */
16349
16350static int
16351need_gnat_info (struct dwarf2_cu *cu)
16352{
16353 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16354 of GNAT produces this auxiliary information, without any indication
16355 that it is produced. Part of enhancing the FSF version of GNAT
16356 to produce that information will be to put in place an indicator
16357 that we can use in order to determine whether the descriptive type
16358 info is available or not. One suggestion that has been made is
16359 to use a new attribute, attached to the CU die. For now, assume
16360 that the descriptive type info is not available. */
16361 return 0;
16362}
16363
b4ba55a1
JB
16364/* Return the auxiliary type of the die in question using its
16365 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16366 attribute is not present. */
16367
16368static struct type *
16369die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16370{
b4ba55a1 16371 struct attribute *type_attr;
b4ba55a1
JB
16372
16373 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16374 if (!type_attr)
16375 return NULL;
16376
673bfd45 16377 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16378}
16379
16380/* If DIE has a descriptive_type attribute, then set the TYPE's
16381 descriptive type accordingly. */
16382
16383static void
16384set_descriptive_type (struct type *type, struct die_info *die,
16385 struct dwarf2_cu *cu)
16386{
16387 struct type *descriptive_type = die_descriptive_type (die, cu);
16388
16389 if (descriptive_type)
16390 {
16391 ALLOCATE_GNAT_AUX_TYPE (type);
16392 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16393 }
16394}
16395
c906108c
SS
16396/* Return the containing type of the die in question using its
16397 DW_AT_containing_type attribute. */
16398
16399static struct type *
e7c27a73 16400die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16401{
c906108c 16402 struct attribute *type_attr;
c906108c 16403
e142c38c 16404 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16405 if (!type_attr)
16406 error (_("Dwarf Error: Problem turning containing type into gdb type "
16407 "[in module %s]"), cu->objfile->name);
16408
673bfd45 16409 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16410}
16411
673bfd45
DE
16412/* Look up the type of DIE in CU using its type attribute ATTR.
16413 If there is no type substitute an error marker. */
16414
c906108c 16415static struct type *
673bfd45
DE
16416lookup_die_type (struct die_info *die, struct attribute *attr,
16417 struct dwarf2_cu *cu)
c906108c 16418{
bb5ed363 16419 struct objfile *objfile = cu->objfile;
f792889a
DJ
16420 struct type *this_type;
16421
673bfd45
DE
16422 /* First see if we have it cached. */
16423
36586728
TT
16424 if (attr->form == DW_FORM_GNU_ref_alt)
16425 {
16426 struct dwarf2_per_cu_data *per_cu;
16427 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16428
16429 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16430 this_type = get_die_type_at_offset (offset, per_cu);
16431 }
16432 else if (is_ref_attr (attr))
673bfd45 16433 {
b64f50a1 16434 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16435
16436 this_type = get_die_type_at_offset (offset, cu->per_cu);
16437 }
55f1336d 16438 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16439 {
16440 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16441
16442 /* sig_type will be NULL if the signatured type is missing from
16443 the debug info. */
16444 if (sig_type == NULL)
16445 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16446 "at 0x%x [in module %s]"),
b64f50a1 16447 die->offset.sect_off, objfile->name);
673bfd45 16448
3019eac3
DE
16449 gdb_assert (sig_type->per_cu.is_debug_types);
16450 /* If we haven't filled in type_offset_in_section yet, then we
16451 haven't read the type in yet. */
16452 this_type = NULL;
16453 if (sig_type->type_offset_in_section.sect_off != 0)
16454 {
16455 this_type =
16456 get_die_type_at_offset (sig_type->type_offset_in_section,
16457 &sig_type->per_cu);
16458 }
673bfd45
DE
16459 }
16460 else
16461 {
16462 dump_die_for_error (die);
16463 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16464 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16465 }
16466
16467 /* If not cached we need to read it in. */
16468
16469 if (this_type == NULL)
16470 {
16471 struct die_info *type_die;
16472 struct dwarf2_cu *type_cu = cu;
16473
16474 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16475 /* If we found the type now, it's probably because the type came
16476 from an inter-CU reference and the type's CU got expanded before
16477 ours. */
16478 this_type = get_die_type (type_die, type_cu);
16479 if (this_type == NULL)
16480 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16481 }
16482
16483 /* If we still don't have a type use an error marker. */
16484
16485 if (this_type == NULL)
c906108c 16486 {
b00fdb78
TT
16487 char *message, *saved;
16488
16489 /* read_type_die already issued a complaint. */
16490 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16491 objfile->name,
b64f50a1
JK
16492 cu->header.offset.sect_off,
16493 die->offset.sect_off);
bb5ed363 16494 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16495 message, strlen (message));
16496 xfree (message);
16497
bb5ed363 16498 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16499 }
673bfd45 16500
f792889a 16501 return this_type;
c906108c
SS
16502}
16503
673bfd45
DE
16504/* Return the type in DIE, CU.
16505 Returns NULL for invalid types.
16506
16507 This first does a lookup in the appropriate type_hash table,
16508 and only reads the die in if necessary.
16509
16510 NOTE: This can be called when reading in partial or full symbols. */
16511
f792889a 16512static struct type *
e7c27a73 16513read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16514{
f792889a
DJ
16515 struct type *this_type;
16516
16517 this_type = get_die_type (die, cu);
16518 if (this_type)
16519 return this_type;
16520
673bfd45
DE
16521 return read_type_die_1 (die, cu);
16522}
16523
16524/* Read the type in DIE, CU.
16525 Returns NULL for invalid types. */
16526
16527static struct type *
16528read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16529{
16530 struct type *this_type = NULL;
16531
c906108c
SS
16532 switch (die->tag)
16533 {
16534 case DW_TAG_class_type:
680b30c7 16535 case DW_TAG_interface_type:
c906108c
SS
16536 case DW_TAG_structure_type:
16537 case DW_TAG_union_type:
f792889a 16538 this_type = read_structure_type (die, cu);
c906108c
SS
16539 break;
16540 case DW_TAG_enumeration_type:
f792889a 16541 this_type = read_enumeration_type (die, cu);
c906108c
SS
16542 break;
16543 case DW_TAG_subprogram:
16544 case DW_TAG_subroutine_type:
edb3359d 16545 case DW_TAG_inlined_subroutine:
f792889a 16546 this_type = read_subroutine_type (die, cu);
c906108c
SS
16547 break;
16548 case DW_TAG_array_type:
f792889a 16549 this_type = read_array_type (die, cu);
c906108c 16550 break;
72019c9c 16551 case DW_TAG_set_type:
f792889a 16552 this_type = read_set_type (die, cu);
72019c9c 16553 break;
c906108c 16554 case DW_TAG_pointer_type:
f792889a 16555 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16556 break;
16557 case DW_TAG_ptr_to_member_type:
f792889a 16558 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16559 break;
16560 case DW_TAG_reference_type:
f792889a 16561 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16562 break;
16563 case DW_TAG_const_type:
f792889a 16564 this_type = read_tag_const_type (die, cu);
c906108c
SS
16565 break;
16566 case DW_TAG_volatile_type:
f792889a 16567 this_type = read_tag_volatile_type (die, cu);
c906108c 16568 break;
06d66ee9
TT
16569 case DW_TAG_restrict_type:
16570 this_type = read_tag_restrict_type (die, cu);
16571 break;
c906108c 16572 case DW_TAG_string_type:
f792889a 16573 this_type = read_tag_string_type (die, cu);
c906108c
SS
16574 break;
16575 case DW_TAG_typedef:
f792889a 16576 this_type = read_typedef (die, cu);
c906108c 16577 break;
a02abb62 16578 case DW_TAG_subrange_type:
f792889a 16579 this_type = read_subrange_type (die, cu);
a02abb62 16580 break;
c906108c 16581 case DW_TAG_base_type:
f792889a 16582 this_type = read_base_type (die, cu);
c906108c 16583 break;
81a17f79 16584 case DW_TAG_unspecified_type:
f792889a 16585 this_type = read_unspecified_type (die, cu);
81a17f79 16586 break;
0114d602
DJ
16587 case DW_TAG_namespace:
16588 this_type = read_namespace_type (die, cu);
16589 break;
f55ee35c
JK
16590 case DW_TAG_module:
16591 this_type = read_module_type (die, cu);
16592 break;
c906108c 16593 default:
3e43a32a
MS
16594 complaint (&symfile_complaints,
16595 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16596 dwarf_tag_name (die->tag));
c906108c
SS
16597 break;
16598 }
63d06c5c 16599
f792889a 16600 return this_type;
63d06c5c
DC
16601}
16602
abc72ce4
DE
16603/* See if we can figure out if the class lives in a namespace. We do
16604 this by looking for a member function; its demangled name will
16605 contain namespace info, if there is any.
16606 Return the computed name or NULL.
16607 Space for the result is allocated on the objfile's obstack.
16608 This is the full-die version of guess_partial_die_structure_name.
16609 In this case we know DIE has no useful parent. */
16610
16611static char *
16612guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16613{
16614 struct die_info *spec_die;
16615 struct dwarf2_cu *spec_cu;
16616 struct die_info *child;
16617
16618 spec_cu = cu;
16619 spec_die = die_specification (die, &spec_cu);
16620 if (spec_die != NULL)
16621 {
16622 die = spec_die;
16623 cu = spec_cu;
16624 }
16625
16626 for (child = die->child;
16627 child != NULL;
16628 child = child->sibling)
16629 {
16630 if (child->tag == DW_TAG_subprogram)
16631 {
16632 struct attribute *attr;
16633
16634 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16635 if (attr == NULL)
16636 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16637 if (attr != NULL)
16638 {
16639 char *actual_name
16640 = language_class_name_from_physname (cu->language_defn,
16641 DW_STRING (attr));
16642 char *name = NULL;
16643
16644 if (actual_name != NULL)
16645 {
16646 char *die_name = dwarf2_name (die, cu);
16647
16648 if (die_name != NULL
16649 && strcmp (die_name, actual_name) != 0)
16650 {
16651 /* Strip off the class name from the full name.
16652 We want the prefix. */
16653 int die_name_len = strlen (die_name);
16654 int actual_name_len = strlen (actual_name);
16655
16656 /* Test for '::' as a sanity check. */
16657 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16658 && actual_name[actual_name_len
16659 - die_name_len - 1] == ':')
abc72ce4
DE
16660 name =
16661 obsavestring (actual_name,
16662 actual_name_len - die_name_len - 2,
16663 &cu->objfile->objfile_obstack);
16664 }
16665 }
16666 xfree (actual_name);
16667 return name;
16668 }
16669 }
16670 }
16671
16672 return NULL;
16673}
16674
96408a79
SA
16675/* GCC might emit a nameless typedef that has a linkage name. Determine the
16676 prefix part in such case. See
16677 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16678
16679static char *
16680anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16681{
16682 struct attribute *attr;
16683 char *base;
16684
16685 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16686 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16687 return NULL;
16688
16689 attr = dwarf2_attr (die, DW_AT_name, cu);
16690 if (attr != NULL && DW_STRING (attr) != NULL)
16691 return NULL;
16692
16693 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16694 if (attr == NULL)
16695 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16696 if (attr == NULL || DW_STRING (attr) == NULL)
16697 return NULL;
16698
16699 /* dwarf2_name had to be already called. */
16700 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16701
16702 /* Strip the base name, keep any leading namespaces/classes. */
16703 base = strrchr (DW_STRING (attr), ':');
16704 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16705 return "";
16706
16707 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16708 &cu->objfile->objfile_obstack);
16709}
16710
fdde2d81 16711/* Return the name of the namespace/class that DIE is defined within,
0114d602 16712 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16713
0114d602
DJ
16714 For example, if we're within the method foo() in the following
16715 code:
16716
16717 namespace N {
16718 class C {
16719 void foo () {
16720 }
16721 };
16722 }
16723
16724 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16725
0d5cff50 16726static const char *
e142c38c 16727determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16728{
0114d602
DJ
16729 struct die_info *parent, *spec_die;
16730 struct dwarf2_cu *spec_cu;
16731 struct type *parent_type;
96408a79 16732 char *retval;
63d06c5c 16733
f55ee35c
JK
16734 if (cu->language != language_cplus && cu->language != language_java
16735 && cu->language != language_fortran)
0114d602
DJ
16736 return "";
16737
96408a79
SA
16738 retval = anonymous_struct_prefix (die, cu);
16739 if (retval)
16740 return retval;
16741
0114d602
DJ
16742 /* We have to be careful in the presence of DW_AT_specification.
16743 For example, with GCC 3.4, given the code
16744
16745 namespace N {
16746 void foo() {
16747 // Definition of N::foo.
16748 }
16749 }
16750
16751 then we'll have a tree of DIEs like this:
16752
16753 1: DW_TAG_compile_unit
16754 2: DW_TAG_namespace // N
16755 3: DW_TAG_subprogram // declaration of N::foo
16756 4: DW_TAG_subprogram // definition of N::foo
16757 DW_AT_specification // refers to die #3
16758
16759 Thus, when processing die #4, we have to pretend that we're in
16760 the context of its DW_AT_specification, namely the contex of die
16761 #3. */
16762 spec_cu = cu;
16763 spec_die = die_specification (die, &spec_cu);
16764 if (spec_die == NULL)
16765 parent = die->parent;
16766 else
63d06c5c 16767 {
0114d602
DJ
16768 parent = spec_die->parent;
16769 cu = spec_cu;
63d06c5c 16770 }
0114d602
DJ
16771
16772 if (parent == NULL)
16773 return "";
98bfdba5
PA
16774 else if (parent->building_fullname)
16775 {
16776 const char *name;
16777 const char *parent_name;
16778
16779 /* It has been seen on RealView 2.2 built binaries,
16780 DW_TAG_template_type_param types actually _defined_ as
16781 children of the parent class:
16782
16783 enum E {};
16784 template class <class Enum> Class{};
16785 Class<enum E> class_e;
16786
16787 1: DW_TAG_class_type (Class)
16788 2: DW_TAG_enumeration_type (E)
16789 3: DW_TAG_enumerator (enum1:0)
16790 3: DW_TAG_enumerator (enum2:1)
16791 ...
16792 2: DW_TAG_template_type_param
16793 DW_AT_type DW_FORM_ref_udata (E)
16794
16795 Besides being broken debug info, it can put GDB into an
16796 infinite loop. Consider:
16797
16798 When we're building the full name for Class<E>, we'll start
16799 at Class, and go look over its template type parameters,
16800 finding E. We'll then try to build the full name of E, and
16801 reach here. We're now trying to build the full name of E,
16802 and look over the parent DIE for containing scope. In the
16803 broken case, if we followed the parent DIE of E, we'd again
16804 find Class, and once again go look at its template type
16805 arguments, etc., etc. Simply don't consider such parent die
16806 as source-level parent of this die (it can't be, the language
16807 doesn't allow it), and break the loop here. */
16808 name = dwarf2_name (die, cu);
16809 parent_name = dwarf2_name (parent, cu);
16810 complaint (&symfile_complaints,
16811 _("template param type '%s' defined within parent '%s'"),
16812 name ? name : "<unknown>",
16813 parent_name ? parent_name : "<unknown>");
16814 return "";
16815 }
63d06c5c 16816 else
0114d602
DJ
16817 switch (parent->tag)
16818 {
63d06c5c 16819 case DW_TAG_namespace:
0114d602 16820 parent_type = read_type_die (parent, cu);
acebe513
UW
16821 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16822 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16823 Work around this problem here. */
16824 if (cu->language == language_cplus
16825 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16826 return "";
0114d602
DJ
16827 /* We give a name to even anonymous namespaces. */
16828 return TYPE_TAG_NAME (parent_type);
63d06c5c 16829 case DW_TAG_class_type:
680b30c7 16830 case DW_TAG_interface_type:
63d06c5c 16831 case DW_TAG_structure_type:
0114d602 16832 case DW_TAG_union_type:
f55ee35c 16833 case DW_TAG_module:
0114d602
DJ
16834 parent_type = read_type_die (parent, cu);
16835 if (TYPE_TAG_NAME (parent_type) != NULL)
16836 return TYPE_TAG_NAME (parent_type);
16837 else
16838 /* An anonymous structure is only allowed non-static data
16839 members; no typedefs, no member functions, et cetera.
16840 So it does not need a prefix. */
16841 return "";
abc72ce4 16842 case DW_TAG_compile_unit:
95554aad 16843 case DW_TAG_partial_unit:
abc72ce4
DE
16844 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16845 if (cu->language == language_cplus
8b70b953 16846 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16847 && die->child != NULL
16848 && (die->tag == DW_TAG_class_type
16849 || die->tag == DW_TAG_structure_type
16850 || die->tag == DW_TAG_union_type))
16851 {
16852 char *name = guess_full_die_structure_name (die, cu);
16853 if (name != NULL)
16854 return name;
16855 }
16856 return "";
63d06c5c 16857 default:
8176b9b8 16858 return determine_prefix (parent, cu);
63d06c5c 16859 }
63d06c5c
DC
16860}
16861
3e43a32a
MS
16862/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16863 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16864 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16865 an obconcat, otherwise allocate storage for the result. The CU argument is
16866 used to determine the language and hence, the appropriate separator. */
987504bb 16867
f55ee35c 16868#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16869
16870static char *
f55ee35c
JK
16871typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16872 int physname, struct dwarf2_cu *cu)
63d06c5c 16873{
f55ee35c 16874 const char *lead = "";
5c315b68 16875 const char *sep;
63d06c5c 16876
3e43a32a
MS
16877 if (suffix == NULL || suffix[0] == '\0'
16878 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16879 sep = "";
16880 else if (cu->language == language_java)
16881 sep = ".";
f55ee35c
JK
16882 else if (cu->language == language_fortran && physname)
16883 {
16884 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16885 DW_AT_MIPS_linkage_name is preferred and used instead. */
16886
16887 lead = "__";
16888 sep = "_MOD_";
16889 }
987504bb
JJ
16890 else
16891 sep = "::";
63d06c5c 16892
6dd47d34
DE
16893 if (prefix == NULL)
16894 prefix = "";
16895 if (suffix == NULL)
16896 suffix = "";
16897
987504bb
JJ
16898 if (obs == NULL)
16899 {
3e43a32a
MS
16900 char *retval
16901 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16902
f55ee35c
JK
16903 strcpy (retval, lead);
16904 strcat (retval, prefix);
6dd47d34
DE
16905 strcat (retval, sep);
16906 strcat (retval, suffix);
63d06c5c
DC
16907 return retval;
16908 }
987504bb
JJ
16909 else
16910 {
16911 /* We have an obstack. */
f55ee35c 16912 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16913 }
63d06c5c
DC
16914}
16915
c906108c
SS
16916/* Return sibling of die, NULL if no sibling. */
16917
f9aca02d 16918static struct die_info *
fba45db2 16919sibling_die (struct die_info *die)
c906108c 16920{
639d11d3 16921 return die->sibling;
c906108c
SS
16922}
16923
71c25dea
TT
16924/* Get name of a die, return NULL if not found. */
16925
16926static char *
16927dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16928 struct obstack *obstack)
16929{
16930 if (name && cu->language == language_cplus)
16931 {
16932 char *canon_name = cp_canonicalize_string (name);
16933
16934 if (canon_name != NULL)
16935 {
16936 if (strcmp (canon_name, name) != 0)
16937 name = obsavestring (canon_name, strlen (canon_name),
16938 obstack);
16939 xfree (canon_name);
16940 }
16941 }
16942
16943 return name;
c906108c
SS
16944}
16945
9219021c
DC
16946/* Get name of a die, return NULL if not found. */
16947
16948static char *
e142c38c 16949dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16950{
16951 struct attribute *attr;
16952
e142c38c 16953 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16954 if ((!attr || !DW_STRING (attr))
16955 && die->tag != DW_TAG_class_type
16956 && die->tag != DW_TAG_interface_type
16957 && die->tag != DW_TAG_structure_type
16958 && die->tag != DW_TAG_union_type)
71c25dea
TT
16959 return NULL;
16960
16961 switch (die->tag)
16962 {
16963 case DW_TAG_compile_unit:
95554aad 16964 case DW_TAG_partial_unit:
71c25dea
TT
16965 /* Compilation units have a DW_AT_name that is a filename, not
16966 a source language identifier. */
16967 case DW_TAG_enumeration_type:
16968 case DW_TAG_enumerator:
16969 /* These tags always have simple identifiers already; no need
16970 to canonicalize them. */
16971 return DW_STRING (attr);
907af001 16972
418835cc
KS
16973 case DW_TAG_subprogram:
16974 /* Java constructors will all be named "<init>", so return
16975 the class name when we see this special case. */
16976 if (cu->language == language_java
16977 && DW_STRING (attr) != NULL
16978 && strcmp (DW_STRING (attr), "<init>") == 0)
16979 {
16980 struct dwarf2_cu *spec_cu = cu;
16981 struct die_info *spec_die;
16982
16983 /* GCJ will output '<init>' for Java constructor names.
16984 For this special case, return the name of the parent class. */
16985
16986 /* GCJ may output suprogram DIEs with AT_specification set.
16987 If so, use the name of the specified DIE. */
16988 spec_die = die_specification (die, &spec_cu);
16989 if (spec_die != NULL)
16990 return dwarf2_name (spec_die, spec_cu);
16991
16992 do
16993 {
16994 die = die->parent;
16995 if (die->tag == DW_TAG_class_type)
16996 return dwarf2_name (die, cu);
16997 }
95554aad
TT
16998 while (die->tag != DW_TAG_compile_unit
16999 && die->tag != DW_TAG_partial_unit);
418835cc 17000 }
907af001
UW
17001 break;
17002
17003 case DW_TAG_class_type:
17004 case DW_TAG_interface_type:
17005 case DW_TAG_structure_type:
17006 case DW_TAG_union_type:
17007 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17008 structures or unions. These were of the form "._%d" in GCC 4.1,
17009 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17010 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17011 if (attr && DW_STRING (attr)
17012 && (strncmp (DW_STRING (attr), "._", 2) == 0
17013 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17014 return NULL;
53832f31
TT
17015
17016 /* GCC might emit a nameless typedef that has a linkage name. See
17017 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17018 if (!attr || DW_STRING (attr) == NULL)
17019 {
df5c6c50 17020 char *demangled = NULL;
53832f31
TT
17021
17022 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17023 if (attr == NULL)
17024 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17025
17026 if (attr == NULL || DW_STRING (attr) == NULL)
17027 return NULL;
17028
df5c6c50
JK
17029 /* Avoid demangling DW_STRING (attr) the second time on a second
17030 call for the same DIE. */
17031 if (!DW_STRING_IS_CANONICAL (attr))
17032 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17033
17034 if (demangled)
17035 {
96408a79
SA
17036 char *base;
17037
53832f31 17038 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
17039 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
17040 &cu->objfile->objfile_obstack);
53832f31
TT
17041 DW_STRING_IS_CANONICAL (attr) = 1;
17042 xfree (demangled);
96408a79
SA
17043
17044 /* Strip any leading namespaces/classes, keep only the base name.
17045 DW_AT_name for named DIEs does not contain the prefixes. */
17046 base = strrchr (DW_STRING (attr), ':');
17047 if (base && base > DW_STRING (attr) && base[-1] == ':')
17048 return &base[1];
17049 else
17050 return DW_STRING (attr);
53832f31
TT
17051 }
17052 }
907af001
UW
17053 break;
17054
71c25dea 17055 default:
907af001
UW
17056 break;
17057 }
17058
17059 if (!DW_STRING_IS_CANONICAL (attr))
17060 {
17061 DW_STRING (attr)
17062 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17063 &cu->objfile->objfile_obstack);
17064 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17065 }
907af001 17066 return DW_STRING (attr);
9219021c
DC
17067}
17068
17069/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17070 is none. *EXT_CU is the CU containing DIE on input, and the CU
17071 containing the return value on output. */
9219021c
DC
17072
17073static struct die_info *
f2f0e013 17074dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17075{
17076 struct attribute *attr;
9219021c 17077
f2f0e013 17078 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17079 if (attr == NULL)
17080 return NULL;
17081
f2f0e013 17082 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17083}
17084
c906108c
SS
17085/* Convert a DIE tag into its string name. */
17086
f39c6ffd 17087static const char *
aa1ee363 17088dwarf_tag_name (unsigned tag)
c906108c 17089{
f39c6ffd
TT
17090 const char *name = get_DW_TAG_name (tag);
17091
17092 if (name == NULL)
17093 return "DW_TAG_<unknown>";
17094
17095 return name;
c906108c
SS
17096}
17097
17098/* Convert a DWARF attribute code into its string name. */
17099
f39c6ffd 17100static const char *
aa1ee363 17101dwarf_attr_name (unsigned attr)
c906108c 17102{
f39c6ffd
TT
17103 const char *name;
17104
c764a876 17105#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17106 if (attr == DW_AT_MIPS_fde)
17107 return "DW_AT_MIPS_fde";
17108#else
17109 if (attr == DW_AT_HP_block_index)
17110 return "DW_AT_HP_block_index";
c764a876 17111#endif
f39c6ffd
TT
17112
17113 name = get_DW_AT_name (attr);
17114
17115 if (name == NULL)
17116 return "DW_AT_<unknown>";
17117
17118 return name;
c906108c
SS
17119}
17120
17121/* Convert a DWARF value form code into its string name. */
17122
f39c6ffd 17123static const char *
aa1ee363 17124dwarf_form_name (unsigned form)
c906108c 17125{
f39c6ffd
TT
17126 const char *name = get_DW_FORM_name (form);
17127
17128 if (name == NULL)
17129 return "DW_FORM_<unknown>";
17130
17131 return name;
c906108c
SS
17132}
17133
17134static char *
fba45db2 17135dwarf_bool_name (unsigned mybool)
c906108c
SS
17136{
17137 if (mybool)
17138 return "TRUE";
17139 else
17140 return "FALSE";
17141}
17142
17143/* Convert a DWARF type code into its string name. */
17144
f39c6ffd 17145static const char *
aa1ee363 17146dwarf_type_encoding_name (unsigned enc)
c906108c 17147{
f39c6ffd 17148 const char *name = get_DW_ATE_name (enc);
c906108c 17149
f39c6ffd
TT
17150 if (name == NULL)
17151 return "DW_ATE_<unknown>";
c906108c 17152
f39c6ffd 17153 return name;
c906108c 17154}
c906108c 17155
f9aca02d 17156static void
d97bc12b 17157dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17158{
17159 unsigned int i;
17160
d97bc12b
DE
17161 print_spaces (indent, f);
17162 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17163 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17164
17165 if (die->parent != NULL)
17166 {
17167 print_spaces (indent, f);
17168 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17169 die->parent->offset.sect_off);
d97bc12b
DE
17170 }
17171
17172 print_spaces (indent, f);
17173 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17174 dwarf_bool_name (die->child != NULL));
c906108c 17175
d97bc12b
DE
17176 print_spaces (indent, f);
17177 fprintf_unfiltered (f, " attributes:\n");
17178
c906108c
SS
17179 for (i = 0; i < die->num_attrs; ++i)
17180 {
d97bc12b
DE
17181 print_spaces (indent, f);
17182 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17183 dwarf_attr_name (die->attrs[i].name),
17184 dwarf_form_name (die->attrs[i].form));
d97bc12b 17185
c906108c
SS
17186 switch (die->attrs[i].form)
17187 {
c906108c 17188 case DW_FORM_addr:
3019eac3 17189 case DW_FORM_GNU_addr_index:
d97bc12b 17190 fprintf_unfiltered (f, "address: ");
5af949e3 17191 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17192 break;
17193 case DW_FORM_block2:
17194 case DW_FORM_block4:
17195 case DW_FORM_block:
17196 case DW_FORM_block1:
56eb65bd
SP
17197 fprintf_unfiltered (f, "block: size %s",
17198 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17199 break;
2dc7f7b3 17200 case DW_FORM_exprloc:
56eb65bd
SP
17201 fprintf_unfiltered (f, "expression: size %s",
17202 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17203 break;
4568ecf9
DE
17204 case DW_FORM_ref_addr:
17205 fprintf_unfiltered (f, "ref address: ");
17206 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17207 break;
36586728
TT
17208 case DW_FORM_GNU_ref_alt:
17209 fprintf_unfiltered (f, "alt ref address: ");
17210 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17211 break;
10b3939b
DJ
17212 case DW_FORM_ref1:
17213 case DW_FORM_ref2:
17214 case DW_FORM_ref4:
4568ecf9
DE
17215 case DW_FORM_ref8:
17216 case DW_FORM_ref_udata:
d97bc12b 17217 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17218 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17219 break;
c906108c
SS
17220 case DW_FORM_data1:
17221 case DW_FORM_data2:
17222 case DW_FORM_data4:
ce5d95e1 17223 case DW_FORM_data8:
c906108c
SS
17224 case DW_FORM_udata:
17225 case DW_FORM_sdata:
43bbcdc2
PH
17226 fprintf_unfiltered (f, "constant: %s",
17227 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17228 break;
2dc7f7b3
TT
17229 case DW_FORM_sec_offset:
17230 fprintf_unfiltered (f, "section offset: %s",
17231 pulongest (DW_UNSND (&die->attrs[i])));
17232 break;
55f1336d 17233 case DW_FORM_ref_sig8:
348e048f
DE
17234 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17235 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17236 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17237 else
17238 fprintf_unfiltered (f, "signatured type, offset: unknown");
17239 break;
c906108c 17240 case DW_FORM_string:
4bdf3d34 17241 case DW_FORM_strp:
3019eac3 17242 case DW_FORM_GNU_str_index:
36586728 17243 case DW_FORM_GNU_strp_alt:
8285870a 17244 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17245 DW_STRING (&die->attrs[i])
8285870a
JK
17246 ? DW_STRING (&die->attrs[i]) : "",
17247 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17248 break;
17249 case DW_FORM_flag:
17250 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17251 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17252 else
d97bc12b 17253 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17254 break;
2dc7f7b3
TT
17255 case DW_FORM_flag_present:
17256 fprintf_unfiltered (f, "flag: TRUE");
17257 break;
a8329558 17258 case DW_FORM_indirect:
0963b4bd
MS
17259 /* The reader will have reduced the indirect form to
17260 the "base form" so this form should not occur. */
3e43a32a
MS
17261 fprintf_unfiltered (f,
17262 "unexpected attribute form: DW_FORM_indirect");
a8329558 17263 break;
c906108c 17264 default:
d97bc12b 17265 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17266 die->attrs[i].form);
d97bc12b 17267 break;
c906108c 17268 }
d97bc12b 17269 fprintf_unfiltered (f, "\n");
c906108c
SS
17270 }
17271}
17272
f9aca02d 17273static void
d97bc12b 17274dump_die_for_error (struct die_info *die)
c906108c 17275{
d97bc12b
DE
17276 dump_die_shallow (gdb_stderr, 0, die);
17277}
17278
17279static void
17280dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17281{
17282 int indent = level * 4;
17283
17284 gdb_assert (die != NULL);
17285
17286 if (level >= max_level)
17287 return;
17288
17289 dump_die_shallow (f, indent, die);
17290
17291 if (die->child != NULL)
c906108c 17292 {
d97bc12b
DE
17293 print_spaces (indent, f);
17294 fprintf_unfiltered (f, " Children:");
17295 if (level + 1 < max_level)
17296 {
17297 fprintf_unfiltered (f, "\n");
17298 dump_die_1 (f, level + 1, max_level, die->child);
17299 }
17300 else
17301 {
3e43a32a
MS
17302 fprintf_unfiltered (f,
17303 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17304 }
17305 }
17306
17307 if (die->sibling != NULL && level > 0)
17308 {
17309 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17310 }
17311}
17312
d97bc12b
DE
17313/* This is called from the pdie macro in gdbinit.in.
17314 It's not static so gcc will keep a copy callable from gdb. */
17315
17316void
17317dump_die (struct die_info *die, int max_level)
17318{
17319 dump_die_1 (gdb_stdlog, 0, max_level, die);
17320}
17321
f9aca02d 17322static void
51545339 17323store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17324{
51545339 17325 void **slot;
c906108c 17326
b64f50a1
JK
17327 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17328 INSERT);
51545339
DJ
17329
17330 *slot = die;
c906108c
SS
17331}
17332
b64f50a1
JK
17333/* DW_ADDR is always stored already as sect_offset; despite for the forms
17334 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17335
93311388
DE
17336static int
17337is_ref_attr (struct attribute *attr)
c906108c 17338{
c906108c
SS
17339 switch (attr->form)
17340 {
17341 case DW_FORM_ref_addr:
c906108c
SS
17342 case DW_FORM_ref1:
17343 case DW_FORM_ref2:
17344 case DW_FORM_ref4:
613e1657 17345 case DW_FORM_ref8:
c906108c 17346 case DW_FORM_ref_udata:
36586728 17347 case DW_FORM_GNU_ref_alt:
93311388 17348 return 1;
c906108c 17349 default:
93311388 17350 return 0;
c906108c 17351 }
93311388
DE
17352}
17353
b64f50a1
JK
17354/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17355 required kind. */
17356
17357static sect_offset
93311388
DE
17358dwarf2_get_ref_die_offset (struct attribute *attr)
17359{
4568ecf9 17360 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17361
93311388 17362 if (is_ref_attr (attr))
b64f50a1 17363 return retval;
93311388 17364
b64f50a1 17365 retval.sect_off = 0;
93311388
DE
17366 complaint (&symfile_complaints,
17367 _("unsupported die ref attribute form: '%s'"),
17368 dwarf_form_name (attr->form));
b64f50a1 17369 return retval;
c906108c
SS
17370}
17371
43bbcdc2
PH
17372/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17373 * the value held by the attribute is not constant. */
a02abb62 17374
43bbcdc2 17375static LONGEST
a02abb62
JB
17376dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17377{
17378 if (attr->form == DW_FORM_sdata)
17379 return DW_SND (attr);
17380 else if (attr->form == DW_FORM_udata
17381 || attr->form == DW_FORM_data1
17382 || attr->form == DW_FORM_data2
17383 || attr->form == DW_FORM_data4
17384 || attr->form == DW_FORM_data8)
17385 return DW_UNSND (attr);
17386 else
17387 {
3e43a32a
MS
17388 complaint (&symfile_complaints,
17389 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17390 dwarf_form_name (attr->form));
17391 return default_value;
17392 }
17393}
17394
348e048f
DE
17395/* Follow reference or signature attribute ATTR of SRC_DIE.
17396 On entry *REF_CU is the CU of SRC_DIE.
17397 On exit *REF_CU is the CU of the result. */
17398
17399static struct die_info *
17400follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17401 struct dwarf2_cu **ref_cu)
17402{
17403 struct die_info *die;
17404
17405 if (is_ref_attr (attr))
17406 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17407 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17408 die = follow_die_sig (src_die, attr, ref_cu);
17409 else
17410 {
17411 dump_die_for_error (src_die);
17412 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17413 (*ref_cu)->objfile->name);
17414 }
17415
17416 return die;
03dd20cc
DJ
17417}
17418
5c631832 17419/* Follow reference OFFSET.
673bfd45
DE
17420 On entry *REF_CU is the CU of the source die referencing OFFSET.
17421 On exit *REF_CU is the CU of the result.
17422 Returns NULL if OFFSET is invalid. */
f504f079 17423
f9aca02d 17424static struct die_info *
36586728
TT
17425follow_die_offset (sect_offset offset, int offset_in_dwz,
17426 struct dwarf2_cu **ref_cu)
c906108c 17427{
10b3939b 17428 struct die_info temp_die;
f2f0e013 17429 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17430
348e048f
DE
17431 gdb_assert (cu->per_cu != NULL);
17432
98bfdba5
PA
17433 target_cu = cu;
17434
3019eac3 17435 if (cu->per_cu->is_debug_types)
348e048f
DE
17436 {
17437 /* .debug_types CUs cannot reference anything outside their CU.
17438 If they need to, they have to reference a signatured type via
55f1336d 17439 DW_FORM_ref_sig8. */
348e048f 17440 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17441 return NULL;
348e048f 17442 }
36586728
TT
17443 else if (offset_in_dwz != cu->per_cu->is_dwz
17444 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17445 {
17446 struct dwarf2_per_cu_data *per_cu;
9a619af0 17447
36586728
TT
17448 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17449 cu->objfile);
03dd20cc
DJ
17450
17451 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17452 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17453 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17454
10b3939b
DJ
17455 target_cu = per_cu->cu;
17456 }
98bfdba5
PA
17457 else if (cu->dies == NULL)
17458 {
17459 /* We're loading full DIEs during partial symbol reading. */
17460 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17461 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17462 }
c906108c 17463
f2f0e013 17464 *ref_cu = target_cu;
51545339 17465 temp_die.offset = offset;
b64f50a1 17466 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17467}
10b3939b 17468
5c631832
JK
17469/* Follow reference attribute ATTR of SRC_DIE.
17470 On entry *REF_CU is the CU of SRC_DIE.
17471 On exit *REF_CU is the CU of the result. */
17472
17473static struct die_info *
17474follow_die_ref (struct die_info *src_die, struct attribute *attr,
17475 struct dwarf2_cu **ref_cu)
17476{
b64f50a1 17477 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17478 struct dwarf2_cu *cu = *ref_cu;
17479 struct die_info *die;
17480
36586728
TT
17481 die = follow_die_offset (offset,
17482 (attr->form == DW_FORM_GNU_ref_alt
17483 || cu->per_cu->is_dwz),
17484 ref_cu);
5c631832
JK
17485 if (!die)
17486 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17487 "at 0x%x [in module %s]"),
b64f50a1 17488 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17489
5c631832
JK
17490 return die;
17491}
17492
d83e736b
JK
17493/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17494 Returned value is intended for DW_OP_call*. Returned
17495 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17496
17497struct dwarf2_locexpr_baton
8b9737bf
TT
17498dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17499 struct dwarf2_per_cu_data *per_cu,
17500 CORE_ADDR (*get_frame_pc) (void *baton),
17501 void *baton)
5c631832 17502{
918dd910 17503 struct dwarf2_cu *cu;
5c631832
JK
17504 struct die_info *die;
17505 struct attribute *attr;
17506 struct dwarf2_locexpr_baton retval;
17507
8cf6f0b1
TT
17508 dw2_setup (per_cu->objfile);
17509
918dd910
JK
17510 if (per_cu->cu == NULL)
17511 load_cu (per_cu);
17512 cu = per_cu->cu;
17513
36586728 17514 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17515 if (!die)
17516 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17517 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17518
17519 attr = dwarf2_attr (die, DW_AT_location, cu);
17520 if (!attr)
17521 {
e103e986
JK
17522 /* DWARF: "If there is no such attribute, then there is no effect.".
17523 DATA is ignored if SIZE is 0. */
5c631832 17524
e103e986 17525 retval.data = NULL;
5c631832
JK
17526 retval.size = 0;
17527 }
8cf6f0b1
TT
17528 else if (attr_form_is_section_offset (attr))
17529 {
17530 struct dwarf2_loclist_baton loclist_baton;
17531 CORE_ADDR pc = (*get_frame_pc) (baton);
17532 size_t size;
17533
17534 fill_in_loclist_baton (cu, &loclist_baton, attr);
17535
17536 retval.data = dwarf2_find_location_expression (&loclist_baton,
17537 &size, pc);
17538 retval.size = size;
17539 }
5c631832
JK
17540 else
17541 {
17542 if (!attr_form_is_block (attr))
17543 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17544 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17545 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17546
17547 retval.data = DW_BLOCK (attr)->data;
17548 retval.size = DW_BLOCK (attr)->size;
17549 }
17550 retval.per_cu = cu->per_cu;
918dd910 17551
918dd910
JK
17552 age_cached_comp_units ();
17553
5c631832 17554 return retval;
348e048f
DE
17555}
17556
8b9737bf
TT
17557/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17558 offset. */
17559
17560struct dwarf2_locexpr_baton
17561dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17562 struct dwarf2_per_cu_data *per_cu,
17563 CORE_ADDR (*get_frame_pc) (void *baton),
17564 void *baton)
17565{
17566 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17567
17568 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17569}
17570
8a9b8146
TT
17571/* Return the type of the DIE at DIE_OFFSET in the CU named by
17572 PER_CU. */
17573
17574struct type *
b64f50a1 17575dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17576 struct dwarf2_per_cu_data *per_cu)
17577{
b64f50a1
JK
17578 sect_offset die_offset_sect;
17579
8a9b8146 17580 dw2_setup (per_cu->objfile);
b64f50a1
JK
17581
17582 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17583 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17584}
17585
348e048f
DE
17586/* Follow the signature attribute ATTR in SRC_DIE.
17587 On entry *REF_CU is the CU of SRC_DIE.
17588 On exit *REF_CU is the CU of the result. */
17589
17590static struct die_info *
17591follow_die_sig (struct die_info *src_die, struct attribute *attr,
17592 struct dwarf2_cu **ref_cu)
17593{
17594 struct objfile *objfile = (*ref_cu)->objfile;
17595 struct die_info temp_die;
17596 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17597 struct dwarf2_cu *sig_cu;
17598 struct die_info *die;
17599
17600 /* sig_type will be NULL if the signatured type is missing from
17601 the debug info. */
17602 if (sig_type == NULL)
17603 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17604 "at 0x%x [in module %s]"),
b64f50a1 17605 src_die->offset.sect_off, objfile->name);
348e048f
DE
17606
17607 /* If necessary, add it to the queue and load its DIEs. */
17608
95554aad 17609 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17610 read_signatured_type (sig_type);
348e048f
DE
17611
17612 gdb_assert (sig_type->per_cu.cu != NULL);
17613
17614 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17615 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17616 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17617 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17618 temp_die.offset.sect_off);
348e048f
DE
17619 if (die)
17620 {
17621 *ref_cu = sig_cu;
17622 return die;
17623 }
17624
3e43a32a
MS
17625 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17626 "from DIE at 0x%x [in module %s]"),
b64f50a1 17627 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17628}
17629
17630/* Given an offset of a signatured type, return its signatured_type. */
17631
17632static struct signatured_type *
8b70b953
TT
17633lookup_signatured_type_at_offset (struct objfile *objfile,
17634 struct dwarf2_section_info *section,
b64f50a1 17635 sect_offset offset)
348e048f 17636{
b64f50a1 17637 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17638 unsigned int length, initial_length_size;
17639 unsigned int sig_offset;
52dc124a 17640 struct signatured_type find_entry, *sig_type;
348e048f
DE
17641
17642 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17643 sig_offset = (initial_length_size
17644 + 2 /*version*/
17645 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17646 + 1 /*address_size*/);
17647 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17648 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17649
17650 /* This is only used to lookup previously recorded types.
17651 If we didn't find it, it's our bug. */
52dc124a
DE
17652 gdb_assert (sig_type != NULL);
17653 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17654
52dc124a 17655 return sig_type;
348e048f
DE
17656}
17657
e5fe5e75 17658/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17659
17660static void
e5fe5e75 17661load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17662{
52dc124a 17663 struct signatured_type *sig_type;
348e048f 17664
f4dc4d17
DE
17665 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17666 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17667
6721b2ec
DE
17668 /* We have the per_cu, but we need the signatured_type.
17669 Fortunately this is an easy translation. */
17670 gdb_assert (per_cu->is_debug_types);
17671 sig_type = (struct signatured_type *) per_cu;
348e048f 17672
6721b2ec 17673 gdb_assert (per_cu->cu == NULL);
348e048f 17674
52dc124a 17675 read_signatured_type (sig_type);
348e048f 17676
6721b2ec 17677 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17678}
17679
dee91e82
DE
17680/* die_reader_func for read_signatured_type.
17681 This is identical to load_full_comp_unit_reader,
17682 but is kept separate for now. */
348e048f
DE
17683
17684static void
dee91e82
DE
17685read_signatured_type_reader (const struct die_reader_specs *reader,
17686 gdb_byte *info_ptr,
17687 struct die_info *comp_unit_die,
17688 int has_children,
17689 void *data)
348e048f 17690{
dee91e82 17691 struct dwarf2_cu *cu = reader->cu;
348e048f 17692
dee91e82
DE
17693 gdb_assert (cu->die_hash == NULL);
17694 cu->die_hash =
17695 htab_create_alloc_ex (cu->header.length / 12,
17696 die_hash,
17697 die_eq,
17698 NULL,
17699 &cu->comp_unit_obstack,
17700 hashtab_obstack_allocate,
17701 dummy_obstack_deallocate);
348e048f 17702
dee91e82
DE
17703 if (has_children)
17704 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17705 &info_ptr, comp_unit_die);
17706 cu->dies = comp_unit_die;
17707 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17708
17709 /* We try not to read any attributes in this function, because not
9cdd5dbd 17710 all CUs needed for references have been loaded yet, and symbol
348e048f 17711 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17712 or we won't be able to build types correctly.
17713 Similarly, if we do not read the producer, we can not apply
17714 producer-specific interpretation. */
95554aad 17715 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17716}
348e048f 17717
3019eac3
DE
17718/* Read in a signatured type and build its CU and DIEs.
17719 If the type is a stub for the real type in a DWO file,
17720 read in the real type from the DWO file as well. */
dee91e82
DE
17721
17722static void
17723read_signatured_type (struct signatured_type *sig_type)
17724{
17725 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17726
3019eac3 17727 gdb_assert (per_cu->is_debug_types);
dee91e82 17728 gdb_assert (per_cu->cu == NULL);
348e048f 17729
f4dc4d17
DE
17730 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17731 read_signatured_type_reader, NULL);
c906108c
SS
17732}
17733
c906108c
SS
17734/* Decode simple location descriptions.
17735 Given a pointer to a dwarf block that defines a location, compute
17736 the location and return the value.
17737
4cecd739
DJ
17738 NOTE drow/2003-11-18: This function is called in two situations
17739 now: for the address of static or global variables (partial symbols
17740 only) and for offsets into structures which are expected to be
17741 (more or less) constant. The partial symbol case should go away,
17742 and only the constant case should remain. That will let this
17743 function complain more accurately. A few special modes are allowed
17744 without complaint for global variables (for instance, global
17745 register values and thread-local values).
c906108c
SS
17746
17747 A location description containing no operations indicates that the
4cecd739 17748 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17749 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17750 callers will only want a very basic result and this can become a
21ae7a4d
JK
17751 complaint.
17752
17753 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17754
17755static CORE_ADDR
e7c27a73 17756decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17757{
e7c27a73 17758 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17759 size_t i;
17760 size_t size = blk->size;
21ae7a4d
JK
17761 gdb_byte *data = blk->data;
17762 CORE_ADDR stack[64];
17763 int stacki;
17764 unsigned int bytes_read, unsnd;
17765 gdb_byte op;
c906108c 17766
21ae7a4d
JK
17767 i = 0;
17768 stacki = 0;
17769 stack[stacki] = 0;
17770 stack[++stacki] = 0;
17771
17772 while (i < size)
17773 {
17774 op = data[i++];
17775 switch (op)
17776 {
17777 case DW_OP_lit0:
17778 case DW_OP_lit1:
17779 case DW_OP_lit2:
17780 case DW_OP_lit3:
17781 case DW_OP_lit4:
17782 case DW_OP_lit5:
17783 case DW_OP_lit6:
17784 case DW_OP_lit7:
17785 case DW_OP_lit8:
17786 case DW_OP_lit9:
17787 case DW_OP_lit10:
17788 case DW_OP_lit11:
17789 case DW_OP_lit12:
17790 case DW_OP_lit13:
17791 case DW_OP_lit14:
17792 case DW_OP_lit15:
17793 case DW_OP_lit16:
17794 case DW_OP_lit17:
17795 case DW_OP_lit18:
17796 case DW_OP_lit19:
17797 case DW_OP_lit20:
17798 case DW_OP_lit21:
17799 case DW_OP_lit22:
17800 case DW_OP_lit23:
17801 case DW_OP_lit24:
17802 case DW_OP_lit25:
17803 case DW_OP_lit26:
17804 case DW_OP_lit27:
17805 case DW_OP_lit28:
17806 case DW_OP_lit29:
17807 case DW_OP_lit30:
17808 case DW_OP_lit31:
17809 stack[++stacki] = op - DW_OP_lit0;
17810 break;
f1bea926 17811
21ae7a4d
JK
17812 case DW_OP_reg0:
17813 case DW_OP_reg1:
17814 case DW_OP_reg2:
17815 case DW_OP_reg3:
17816 case DW_OP_reg4:
17817 case DW_OP_reg5:
17818 case DW_OP_reg6:
17819 case DW_OP_reg7:
17820 case DW_OP_reg8:
17821 case DW_OP_reg9:
17822 case DW_OP_reg10:
17823 case DW_OP_reg11:
17824 case DW_OP_reg12:
17825 case DW_OP_reg13:
17826 case DW_OP_reg14:
17827 case DW_OP_reg15:
17828 case DW_OP_reg16:
17829 case DW_OP_reg17:
17830 case DW_OP_reg18:
17831 case DW_OP_reg19:
17832 case DW_OP_reg20:
17833 case DW_OP_reg21:
17834 case DW_OP_reg22:
17835 case DW_OP_reg23:
17836 case DW_OP_reg24:
17837 case DW_OP_reg25:
17838 case DW_OP_reg26:
17839 case DW_OP_reg27:
17840 case DW_OP_reg28:
17841 case DW_OP_reg29:
17842 case DW_OP_reg30:
17843 case DW_OP_reg31:
17844 stack[++stacki] = op - DW_OP_reg0;
17845 if (i < size)
17846 dwarf2_complex_location_expr_complaint ();
17847 break;
c906108c 17848
21ae7a4d
JK
17849 case DW_OP_regx:
17850 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17851 i += bytes_read;
17852 stack[++stacki] = unsnd;
17853 if (i < size)
17854 dwarf2_complex_location_expr_complaint ();
17855 break;
c906108c 17856
21ae7a4d
JK
17857 case DW_OP_addr:
17858 stack[++stacki] = read_address (objfile->obfd, &data[i],
17859 cu, &bytes_read);
17860 i += bytes_read;
17861 break;
d53d4ac5 17862
21ae7a4d
JK
17863 case DW_OP_const1u:
17864 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17865 i += 1;
17866 break;
17867
17868 case DW_OP_const1s:
17869 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17870 i += 1;
17871 break;
17872
17873 case DW_OP_const2u:
17874 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17875 i += 2;
17876 break;
17877
17878 case DW_OP_const2s:
17879 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17880 i += 2;
17881 break;
d53d4ac5 17882
21ae7a4d
JK
17883 case DW_OP_const4u:
17884 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17885 i += 4;
17886 break;
17887
17888 case DW_OP_const4s:
17889 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17890 i += 4;
17891 break;
17892
585861ea
JK
17893 case DW_OP_const8u:
17894 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17895 i += 8;
17896 break;
17897
21ae7a4d
JK
17898 case DW_OP_constu:
17899 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17900 &bytes_read);
17901 i += bytes_read;
17902 break;
17903
17904 case DW_OP_consts:
17905 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17906 i += bytes_read;
17907 break;
17908
17909 case DW_OP_dup:
17910 stack[stacki + 1] = stack[stacki];
17911 stacki++;
17912 break;
17913
17914 case DW_OP_plus:
17915 stack[stacki - 1] += stack[stacki];
17916 stacki--;
17917 break;
17918
17919 case DW_OP_plus_uconst:
17920 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17921 &bytes_read);
17922 i += bytes_read;
17923 break;
17924
17925 case DW_OP_minus:
17926 stack[stacki - 1] -= stack[stacki];
17927 stacki--;
17928 break;
17929
17930 case DW_OP_deref:
17931 /* If we're not the last op, then we definitely can't encode
17932 this using GDB's address_class enum. This is valid for partial
17933 global symbols, although the variable's address will be bogus
17934 in the psymtab. */
17935 if (i < size)
17936 dwarf2_complex_location_expr_complaint ();
17937 break;
17938
17939 case DW_OP_GNU_push_tls_address:
17940 /* The top of the stack has the offset from the beginning
17941 of the thread control block at which the variable is located. */
17942 /* Nothing should follow this operator, so the top of stack would
17943 be returned. */
17944 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17945 address will be bogus in the psymtab. Make it always at least
17946 non-zero to not look as a variable garbage collected by linker
17947 which have DW_OP_addr 0. */
21ae7a4d
JK
17948 if (i < size)
17949 dwarf2_complex_location_expr_complaint ();
585861ea 17950 stack[stacki]++;
21ae7a4d
JK
17951 break;
17952
17953 case DW_OP_GNU_uninit:
17954 break;
17955
3019eac3 17956 case DW_OP_GNU_addr_index:
49f6c839 17957 case DW_OP_GNU_const_index:
3019eac3
DE
17958 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17959 &bytes_read);
17960 i += bytes_read;
17961 break;
17962
21ae7a4d
JK
17963 default:
17964 {
f39c6ffd 17965 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17966
17967 if (name)
17968 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17969 name);
17970 else
17971 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17972 op);
17973 }
17974
17975 return (stack[stacki]);
d53d4ac5 17976 }
3c6e0cb3 17977
21ae7a4d
JK
17978 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17979 outside of the allocated space. Also enforce minimum>0. */
17980 if (stacki >= ARRAY_SIZE (stack) - 1)
17981 {
17982 complaint (&symfile_complaints,
17983 _("location description stack overflow"));
17984 return 0;
17985 }
17986
17987 if (stacki <= 0)
17988 {
17989 complaint (&symfile_complaints,
17990 _("location description stack underflow"));
17991 return 0;
17992 }
17993 }
17994 return (stack[stacki]);
c906108c
SS
17995}
17996
17997/* memory allocation interface */
17998
c906108c 17999static struct dwarf_block *
7b5a2f43 18000dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18001{
18002 struct dwarf_block *blk;
18003
18004 blk = (struct dwarf_block *)
7b5a2f43 18005 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18006 return (blk);
18007}
18008
c906108c 18009static struct die_info *
b60c80d6 18010dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18011{
18012 struct die_info *die;
b60c80d6
DJ
18013 size_t size = sizeof (struct die_info);
18014
18015 if (num_attrs > 1)
18016 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18017
b60c80d6 18018 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18019 memset (die, 0, sizeof (struct die_info));
18020 return (die);
18021}
2e276125
JB
18022
18023\f
18024/* Macro support. */
18025
2e276125
JB
18026/* Return the full name of file number I in *LH's file name table.
18027 Use COMP_DIR as the name of the current directory of the
18028 compilation. The result is allocated using xmalloc; the caller is
18029 responsible for freeing it. */
18030static char *
18031file_full_name (int file, struct line_header *lh, const char *comp_dir)
18032{
6a83a1e6
EZ
18033 /* Is the file number a valid index into the line header's file name
18034 table? Remember that file numbers start with one, not zero. */
18035 if (1 <= file && file <= lh->num_file_names)
18036 {
18037 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18038
6a83a1e6
EZ
18039 if (IS_ABSOLUTE_PATH (fe->name))
18040 return xstrdup (fe->name);
18041 else
18042 {
18043 const char *dir;
18044 int dir_len;
18045 char *full_name;
18046
18047 if (fe->dir_index)
18048 dir = lh->include_dirs[fe->dir_index - 1];
18049 else
18050 dir = comp_dir;
18051
18052 if (dir)
18053 {
18054 dir_len = strlen (dir);
18055 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
18056 strcpy (full_name, dir);
18057 full_name[dir_len] = '/';
18058 strcpy (full_name + dir_len + 1, fe->name);
18059 return full_name;
18060 }
18061 else
18062 return xstrdup (fe->name);
18063 }
18064 }
2e276125
JB
18065 else
18066 {
6a83a1e6
EZ
18067 /* The compiler produced a bogus file number. We can at least
18068 record the macro definitions made in the file, even if we
18069 won't be able to find the file by name. */
18070 char fake_name[80];
9a619af0 18071
8c042590
PM
18072 xsnprintf (fake_name, sizeof (fake_name),
18073 "<bad macro file number %d>", file);
2e276125 18074
6e70227d 18075 complaint (&symfile_complaints,
6a83a1e6
EZ
18076 _("bad file number in macro information (%d)"),
18077 file);
2e276125 18078
6a83a1e6 18079 return xstrdup (fake_name);
2e276125
JB
18080 }
18081}
18082
18083
18084static struct macro_source_file *
18085macro_start_file (int file, int line,
18086 struct macro_source_file *current_file,
18087 const char *comp_dir,
18088 struct line_header *lh, struct objfile *objfile)
18089{
18090 /* The full name of this source file. */
18091 char *full_name = file_full_name (file, lh, comp_dir);
18092
18093 /* We don't create a macro table for this compilation unit
18094 at all until we actually get a filename. */
18095 if (! pending_macros)
6532ff36
TT
18096 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18097 objfile->per_bfd->macro_cache);
2e276125
JB
18098
18099 if (! current_file)
abc9d0dc
TT
18100 {
18101 /* If we have no current file, then this must be the start_file
18102 directive for the compilation unit's main source file. */
18103 current_file = macro_set_main (pending_macros, full_name);
18104 macro_define_special (pending_macros);
18105 }
2e276125
JB
18106 else
18107 current_file = macro_include (current_file, line, full_name);
18108
18109 xfree (full_name);
6e70227d 18110
2e276125
JB
18111 return current_file;
18112}
18113
18114
18115/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18116 followed by a null byte. */
18117static char *
18118copy_string (const char *buf, int len)
18119{
18120 char *s = xmalloc (len + 1);
9a619af0 18121
2e276125
JB
18122 memcpy (s, buf, len);
18123 s[len] = '\0';
2e276125
JB
18124 return s;
18125}
18126
18127
18128static const char *
18129consume_improper_spaces (const char *p, const char *body)
18130{
18131 if (*p == ' ')
18132 {
4d3c2250 18133 complaint (&symfile_complaints,
3e43a32a
MS
18134 _("macro definition contains spaces "
18135 "in formal argument list:\n`%s'"),
4d3c2250 18136 body);
2e276125
JB
18137
18138 while (*p == ' ')
18139 p++;
18140 }
18141
18142 return p;
18143}
18144
18145
18146static void
18147parse_macro_definition (struct macro_source_file *file, int line,
18148 const char *body)
18149{
18150 const char *p;
18151
18152 /* The body string takes one of two forms. For object-like macro
18153 definitions, it should be:
18154
18155 <macro name> " " <definition>
18156
18157 For function-like macro definitions, it should be:
18158
18159 <macro name> "() " <definition>
18160 or
18161 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18162
18163 Spaces may appear only where explicitly indicated, and in the
18164 <definition>.
18165
18166 The Dwarf 2 spec says that an object-like macro's name is always
18167 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18168 the space when the macro's definition is the empty string.
2e276125
JB
18169
18170 The Dwarf 2 spec says that there should be no spaces between the
18171 formal arguments in a function-like macro's formal argument list,
18172 but versions of GCC around March 2002 include spaces after the
18173 commas. */
18174
18175
18176 /* Find the extent of the macro name. The macro name is terminated
18177 by either a space or null character (for an object-like macro) or
18178 an opening paren (for a function-like macro). */
18179 for (p = body; *p; p++)
18180 if (*p == ' ' || *p == '(')
18181 break;
18182
18183 if (*p == ' ' || *p == '\0')
18184 {
18185 /* It's an object-like macro. */
18186 int name_len = p - body;
18187 char *name = copy_string (body, name_len);
18188 const char *replacement;
18189
18190 if (*p == ' ')
18191 replacement = body + name_len + 1;
18192 else
18193 {
4d3c2250 18194 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18195 replacement = body + name_len;
18196 }
6e70227d 18197
2e276125
JB
18198 macro_define_object (file, line, name, replacement);
18199
18200 xfree (name);
18201 }
18202 else if (*p == '(')
18203 {
18204 /* It's a function-like macro. */
18205 char *name = copy_string (body, p - body);
18206 int argc = 0;
18207 int argv_size = 1;
18208 char **argv = xmalloc (argv_size * sizeof (*argv));
18209
18210 p++;
18211
18212 p = consume_improper_spaces (p, body);
18213
18214 /* Parse the formal argument list. */
18215 while (*p && *p != ')')
18216 {
18217 /* Find the extent of the current argument name. */
18218 const char *arg_start = p;
18219
18220 while (*p && *p != ',' && *p != ')' && *p != ' ')
18221 p++;
18222
18223 if (! *p || p == arg_start)
4d3c2250 18224 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18225 else
18226 {
18227 /* Make sure argv has room for the new argument. */
18228 if (argc >= argv_size)
18229 {
18230 argv_size *= 2;
18231 argv = xrealloc (argv, argv_size * sizeof (*argv));
18232 }
18233
18234 argv[argc++] = copy_string (arg_start, p - arg_start);
18235 }
18236
18237 p = consume_improper_spaces (p, body);
18238
18239 /* Consume the comma, if present. */
18240 if (*p == ',')
18241 {
18242 p++;
18243
18244 p = consume_improper_spaces (p, body);
18245 }
18246 }
18247
18248 if (*p == ')')
18249 {
18250 p++;
18251
18252 if (*p == ' ')
18253 /* Perfectly formed definition, no complaints. */
18254 macro_define_function (file, line, name,
6e70227d 18255 argc, (const char **) argv,
2e276125
JB
18256 p + 1);
18257 else if (*p == '\0')
18258 {
18259 /* Complain, but do define it. */
4d3c2250 18260 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18261 macro_define_function (file, line, name,
6e70227d 18262 argc, (const char **) argv,
2e276125
JB
18263 p);
18264 }
18265 else
18266 /* Just complain. */
4d3c2250 18267 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18268 }
18269 else
18270 /* Just complain. */
4d3c2250 18271 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18272
18273 xfree (name);
18274 {
18275 int i;
18276
18277 for (i = 0; i < argc; i++)
18278 xfree (argv[i]);
18279 }
18280 xfree (argv);
18281 }
18282 else
4d3c2250 18283 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18284}
18285
cf2c3c16
TT
18286/* Skip some bytes from BYTES according to the form given in FORM.
18287 Returns the new pointer. */
2e276125 18288
cf2c3c16 18289static gdb_byte *
f664829e 18290skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18291 enum dwarf_form form,
18292 unsigned int offset_size,
18293 struct dwarf2_section_info *section)
2e276125 18294{
cf2c3c16 18295 unsigned int bytes_read;
2e276125 18296
cf2c3c16 18297 switch (form)
2e276125 18298 {
cf2c3c16
TT
18299 case DW_FORM_data1:
18300 case DW_FORM_flag:
18301 ++bytes;
18302 break;
18303
18304 case DW_FORM_data2:
18305 bytes += 2;
18306 break;
18307
18308 case DW_FORM_data4:
18309 bytes += 4;
18310 break;
18311
18312 case DW_FORM_data8:
18313 bytes += 8;
18314 break;
18315
18316 case DW_FORM_string:
18317 read_direct_string (abfd, bytes, &bytes_read);
18318 bytes += bytes_read;
18319 break;
18320
18321 case DW_FORM_sec_offset:
18322 case DW_FORM_strp:
36586728 18323 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18324 bytes += offset_size;
18325 break;
18326
18327 case DW_FORM_block:
18328 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18329 bytes += bytes_read;
18330 break;
18331
18332 case DW_FORM_block1:
18333 bytes += 1 + read_1_byte (abfd, bytes);
18334 break;
18335 case DW_FORM_block2:
18336 bytes += 2 + read_2_bytes (abfd, bytes);
18337 break;
18338 case DW_FORM_block4:
18339 bytes += 4 + read_4_bytes (abfd, bytes);
18340 break;
18341
18342 case DW_FORM_sdata:
18343 case DW_FORM_udata:
3019eac3
DE
18344 case DW_FORM_GNU_addr_index:
18345 case DW_FORM_GNU_str_index:
f664829e
DE
18346 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18347 if (bytes == NULL)
18348 {
18349 dwarf2_section_buffer_overflow_complaint (section);
18350 return NULL;
18351 }
cf2c3c16
TT
18352 break;
18353
18354 default:
18355 {
18356 complain:
18357 complaint (&symfile_complaints,
18358 _("invalid form 0x%x in `%s'"),
18359 form,
18360 section->asection->name);
18361 return NULL;
18362 }
2e276125
JB
18363 }
18364
cf2c3c16
TT
18365 return bytes;
18366}
757a13d0 18367
cf2c3c16
TT
18368/* A helper for dwarf_decode_macros that handles skipping an unknown
18369 opcode. Returns an updated pointer to the macro data buffer; or,
18370 on error, issues a complaint and returns NULL. */
757a13d0 18371
cf2c3c16
TT
18372static gdb_byte *
18373skip_unknown_opcode (unsigned int opcode,
18374 gdb_byte **opcode_definitions,
f664829e 18375 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18376 bfd *abfd,
18377 unsigned int offset_size,
18378 struct dwarf2_section_info *section)
18379{
18380 unsigned int bytes_read, i;
18381 unsigned long arg;
18382 gdb_byte *defn;
2e276125 18383
cf2c3c16 18384 if (opcode_definitions[opcode] == NULL)
2e276125 18385 {
cf2c3c16
TT
18386 complaint (&symfile_complaints,
18387 _("unrecognized DW_MACFINO opcode 0x%x"),
18388 opcode);
18389 return NULL;
18390 }
2e276125 18391
cf2c3c16
TT
18392 defn = opcode_definitions[opcode];
18393 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18394 defn += bytes_read;
2e276125 18395
cf2c3c16
TT
18396 for (i = 0; i < arg; ++i)
18397 {
f664829e
DE
18398 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18399 section);
cf2c3c16
TT
18400 if (mac_ptr == NULL)
18401 {
18402 /* skip_form_bytes already issued the complaint. */
18403 return NULL;
18404 }
18405 }
757a13d0 18406
cf2c3c16
TT
18407 return mac_ptr;
18408}
757a13d0 18409
cf2c3c16
TT
18410/* A helper function which parses the header of a macro section.
18411 If the macro section is the extended (for now called "GNU") type,
18412 then this updates *OFFSET_SIZE. Returns a pointer to just after
18413 the header, or issues a complaint and returns NULL on error. */
757a13d0 18414
cf2c3c16
TT
18415static gdb_byte *
18416dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18417 bfd *abfd,
18418 gdb_byte *mac_ptr,
18419 unsigned int *offset_size,
18420 int section_is_gnu)
18421{
18422 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18423
cf2c3c16
TT
18424 if (section_is_gnu)
18425 {
18426 unsigned int version, flags;
757a13d0 18427
cf2c3c16
TT
18428 version = read_2_bytes (abfd, mac_ptr);
18429 if (version != 4)
18430 {
18431 complaint (&symfile_complaints,
18432 _("unrecognized version `%d' in .debug_macro section"),
18433 version);
18434 return NULL;
18435 }
18436 mac_ptr += 2;
757a13d0 18437
cf2c3c16
TT
18438 flags = read_1_byte (abfd, mac_ptr);
18439 ++mac_ptr;
18440 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18441
cf2c3c16
TT
18442 if ((flags & 2) != 0)
18443 /* We don't need the line table offset. */
18444 mac_ptr += *offset_size;
757a13d0 18445
cf2c3c16
TT
18446 /* Vendor opcode descriptions. */
18447 if ((flags & 4) != 0)
18448 {
18449 unsigned int i, count;
757a13d0 18450
cf2c3c16
TT
18451 count = read_1_byte (abfd, mac_ptr);
18452 ++mac_ptr;
18453 for (i = 0; i < count; ++i)
18454 {
18455 unsigned int opcode, bytes_read;
18456 unsigned long arg;
18457
18458 opcode = read_1_byte (abfd, mac_ptr);
18459 ++mac_ptr;
18460 opcode_definitions[opcode] = mac_ptr;
18461 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18462 mac_ptr += bytes_read;
18463 mac_ptr += arg;
18464 }
757a13d0 18465 }
cf2c3c16 18466 }
757a13d0 18467
cf2c3c16
TT
18468 return mac_ptr;
18469}
757a13d0 18470
cf2c3c16 18471/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18472 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18473
18474static void
18475dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18476 struct macro_source_file *current_file,
18477 struct line_header *lh, char *comp_dir,
18478 struct dwarf2_section_info *section,
36586728 18479 int section_is_gnu, int section_is_dwz,
cf2c3c16 18480 unsigned int offset_size,
8fc3fc34
TT
18481 struct objfile *objfile,
18482 htab_t include_hash)
cf2c3c16
TT
18483{
18484 enum dwarf_macro_record_type macinfo_type;
18485 int at_commandline;
18486 gdb_byte *opcode_definitions[256];
757a13d0 18487
cf2c3c16
TT
18488 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18489 &offset_size, section_is_gnu);
18490 if (mac_ptr == NULL)
18491 {
18492 /* We already issued a complaint. */
18493 return;
18494 }
757a13d0
JK
18495
18496 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18497 GDB is still reading the definitions from command line. First
18498 DW_MACINFO_start_file will need to be ignored as it was already executed
18499 to create CURRENT_FILE for the main source holding also the command line
18500 definitions. On first met DW_MACINFO_start_file this flag is reset to
18501 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18502
18503 at_commandline = 1;
18504
18505 do
18506 {
18507 /* Do we at least have room for a macinfo type byte? */
18508 if (mac_ptr >= mac_end)
18509 {
f664829e 18510 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18511 break;
18512 }
18513
18514 macinfo_type = read_1_byte (abfd, mac_ptr);
18515 mac_ptr++;
18516
cf2c3c16
TT
18517 /* Note that we rely on the fact that the corresponding GNU and
18518 DWARF constants are the same. */
757a13d0
JK
18519 switch (macinfo_type)
18520 {
18521 /* A zero macinfo type indicates the end of the macro
18522 information. */
18523 case 0:
18524 break;
2e276125 18525
cf2c3c16
TT
18526 case DW_MACRO_GNU_define:
18527 case DW_MACRO_GNU_undef:
18528 case DW_MACRO_GNU_define_indirect:
18529 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18530 case DW_MACRO_GNU_define_indirect_alt:
18531 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18532 {
891d2f0b 18533 unsigned int bytes_read;
2e276125
JB
18534 int line;
18535 char *body;
cf2c3c16 18536 int is_define;
2e276125 18537
cf2c3c16
TT
18538 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18539 mac_ptr += bytes_read;
18540
18541 if (macinfo_type == DW_MACRO_GNU_define
18542 || macinfo_type == DW_MACRO_GNU_undef)
18543 {
18544 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18545 mac_ptr += bytes_read;
18546 }
18547 else
18548 {
18549 LONGEST str_offset;
18550
18551 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18552 mac_ptr += offset_size;
2e276125 18553
36586728 18554 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18555 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18556 || section_is_dwz)
36586728
TT
18557 {
18558 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18559
18560 body = read_indirect_string_from_dwz (dwz, str_offset);
18561 }
18562 else
18563 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18564 }
18565
18566 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18567 || macinfo_type == DW_MACRO_GNU_define_indirect
18568 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18569 if (! current_file)
757a13d0
JK
18570 {
18571 /* DWARF violation as no main source is present. */
18572 complaint (&symfile_complaints,
18573 _("debug info with no main source gives macro %s "
18574 "on line %d: %s"),
cf2c3c16
TT
18575 is_define ? _("definition") : _("undefinition"),
18576 line, body);
757a13d0
JK
18577 break;
18578 }
3e43a32a
MS
18579 if ((line == 0 && !at_commandline)
18580 || (line != 0 && at_commandline))
4d3c2250 18581 complaint (&symfile_complaints,
757a13d0
JK
18582 _("debug info gives %s macro %s with %s line %d: %s"),
18583 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18584 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18585 line == 0 ? _("zero") : _("non-zero"), line, body);
18586
cf2c3c16 18587 if (is_define)
757a13d0 18588 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18589 else
18590 {
18591 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18592 || macinfo_type == DW_MACRO_GNU_undef_indirect
18593 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18594 macro_undef (current_file, line, body);
18595 }
2e276125
JB
18596 }
18597 break;
18598
cf2c3c16 18599 case DW_MACRO_GNU_start_file:
2e276125 18600 {
891d2f0b 18601 unsigned int bytes_read;
2e276125
JB
18602 int line, file;
18603
18604 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18605 mac_ptr += bytes_read;
18606 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18607 mac_ptr += bytes_read;
18608
3e43a32a
MS
18609 if ((line == 0 && !at_commandline)
18610 || (line != 0 && at_commandline))
757a13d0
JK
18611 complaint (&symfile_complaints,
18612 _("debug info gives source %d included "
18613 "from %s at %s line %d"),
18614 file, at_commandline ? _("command-line") : _("file"),
18615 line == 0 ? _("zero") : _("non-zero"), line);
18616
18617 if (at_commandline)
18618 {
cf2c3c16
TT
18619 /* This DW_MACRO_GNU_start_file was executed in the
18620 pass one. */
757a13d0
JK
18621 at_commandline = 0;
18622 }
18623 else
18624 current_file = macro_start_file (file, line,
18625 current_file, comp_dir,
cf2c3c16 18626 lh, objfile);
2e276125
JB
18627 }
18628 break;
18629
cf2c3c16 18630 case DW_MACRO_GNU_end_file:
2e276125 18631 if (! current_file)
4d3c2250 18632 complaint (&symfile_complaints,
3e43a32a
MS
18633 _("macro debug info has an unmatched "
18634 "`close_file' directive"));
2e276125
JB
18635 else
18636 {
18637 current_file = current_file->included_by;
18638 if (! current_file)
18639 {
cf2c3c16 18640 enum dwarf_macro_record_type next_type;
2e276125
JB
18641
18642 /* GCC circa March 2002 doesn't produce the zero
18643 type byte marking the end of the compilation
18644 unit. Complain if it's not there, but exit no
18645 matter what. */
18646
18647 /* Do we at least have room for a macinfo type byte? */
18648 if (mac_ptr >= mac_end)
18649 {
f664829e 18650 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18651 return;
18652 }
18653
18654 /* We don't increment mac_ptr here, so this is just
18655 a look-ahead. */
18656 next_type = read_1_byte (abfd, mac_ptr);
18657 if (next_type != 0)
4d3c2250 18658 complaint (&symfile_complaints,
3e43a32a
MS
18659 _("no terminating 0-type entry for "
18660 "macros in `.debug_macinfo' section"));
2e276125
JB
18661
18662 return;
18663 }
18664 }
18665 break;
18666
cf2c3c16 18667 case DW_MACRO_GNU_transparent_include:
36586728 18668 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18669 {
18670 LONGEST offset;
8fc3fc34 18671 void **slot;
a036ba48
TT
18672 bfd *include_bfd = abfd;
18673 struct dwarf2_section_info *include_section = section;
18674 struct dwarf2_section_info alt_section;
18675 gdb_byte *include_mac_end = mac_end;
18676 int is_dwz = section_is_dwz;
18677 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18678
18679 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18680 mac_ptr += offset_size;
18681
a036ba48
TT
18682 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18683 {
18684 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18685
18686 dwarf2_read_section (dwarf2_per_objfile->objfile,
18687 &dwz->macro);
18688
18689 include_bfd = dwz->macro.asection->owner;
18690 include_section = &dwz->macro;
18691 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18692 is_dwz = 1;
18693 }
18694
18695 new_mac_ptr = include_section->buffer + offset;
18696 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18697
8fc3fc34
TT
18698 if (*slot != NULL)
18699 {
18700 /* This has actually happened; see
18701 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18702 complaint (&symfile_complaints,
18703 _("recursive DW_MACRO_GNU_transparent_include in "
18704 ".debug_macro section"));
18705 }
18706 else
18707 {
a036ba48 18708 *slot = new_mac_ptr;
36586728 18709
a036ba48 18710 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18711 include_mac_end, current_file,
8fc3fc34 18712 lh, comp_dir,
36586728 18713 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18714 offset_size, objfile, include_hash);
18715
a036ba48 18716 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18717 }
cf2c3c16
TT
18718 }
18719 break;
18720
2e276125 18721 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18722 if (!section_is_gnu)
18723 {
18724 unsigned int bytes_read;
18725 int constant;
2e276125 18726
cf2c3c16
TT
18727 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18728 mac_ptr += bytes_read;
18729 read_direct_string (abfd, mac_ptr, &bytes_read);
18730 mac_ptr += bytes_read;
2e276125 18731
cf2c3c16
TT
18732 /* We don't recognize any vendor extensions. */
18733 break;
18734 }
18735 /* FALLTHROUGH */
18736
18737 default:
18738 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18739 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18740 section);
18741 if (mac_ptr == NULL)
18742 return;
18743 break;
2e276125 18744 }
757a13d0 18745 } while (macinfo_type != 0);
2e276125 18746}
8e19ed76 18747
cf2c3c16 18748static void
09262596
DE
18749dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18750 char *comp_dir, int section_is_gnu)
cf2c3c16 18751{
bb5ed363 18752 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18753 struct line_header *lh = cu->line_header;
18754 bfd *abfd;
cf2c3c16
TT
18755 gdb_byte *mac_ptr, *mac_end;
18756 struct macro_source_file *current_file = 0;
18757 enum dwarf_macro_record_type macinfo_type;
18758 unsigned int offset_size = cu->header.offset_size;
18759 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18760 struct cleanup *cleanup;
18761 htab_t include_hash;
18762 void **slot;
09262596
DE
18763 struct dwarf2_section_info *section;
18764 const char *section_name;
18765
18766 if (cu->dwo_unit != NULL)
18767 {
18768 if (section_is_gnu)
18769 {
18770 section = &cu->dwo_unit->dwo_file->sections.macro;
18771 section_name = ".debug_macro.dwo";
18772 }
18773 else
18774 {
18775 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18776 section_name = ".debug_macinfo.dwo";
18777 }
18778 }
18779 else
18780 {
18781 if (section_is_gnu)
18782 {
18783 section = &dwarf2_per_objfile->macro;
18784 section_name = ".debug_macro";
18785 }
18786 else
18787 {
18788 section = &dwarf2_per_objfile->macinfo;
18789 section_name = ".debug_macinfo";
18790 }
18791 }
cf2c3c16 18792
bb5ed363 18793 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18794 if (section->buffer == NULL)
18795 {
fceca515 18796 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18797 return;
18798 }
09262596 18799 abfd = section->asection->owner;
cf2c3c16
TT
18800
18801 /* First pass: Find the name of the base filename.
18802 This filename is needed in order to process all macros whose definition
18803 (or undefinition) comes from the command line. These macros are defined
18804 before the first DW_MACINFO_start_file entry, and yet still need to be
18805 associated to the base file.
18806
18807 To determine the base file name, we scan the macro definitions until we
18808 reach the first DW_MACINFO_start_file entry. We then initialize
18809 CURRENT_FILE accordingly so that any macro definition found before the
18810 first DW_MACINFO_start_file can still be associated to the base file. */
18811
18812 mac_ptr = section->buffer + offset;
18813 mac_end = section->buffer + section->size;
18814
18815 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18816 &offset_size, section_is_gnu);
18817 if (mac_ptr == NULL)
18818 {
18819 /* We already issued a complaint. */
18820 return;
18821 }
18822
18823 do
18824 {
18825 /* Do we at least have room for a macinfo type byte? */
18826 if (mac_ptr >= mac_end)
18827 {
18828 /* Complaint is printed during the second pass as GDB will probably
18829 stop the first pass earlier upon finding
18830 DW_MACINFO_start_file. */
18831 break;
18832 }
18833
18834 macinfo_type = read_1_byte (abfd, mac_ptr);
18835 mac_ptr++;
18836
18837 /* Note that we rely on the fact that the corresponding GNU and
18838 DWARF constants are the same. */
18839 switch (macinfo_type)
18840 {
18841 /* A zero macinfo type indicates the end of the macro
18842 information. */
18843 case 0:
18844 break;
18845
18846 case DW_MACRO_GNU_define:
18847 case DW_MACRO_GNU_undef:
18848 /* Only skip the data by MAC_PTR. */
18849 {
18850 unsigned int bytes_read;
18851
18852 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18853 mac_ptr += bytes_read;
18854 read_direct_string (abfd, mac_ptr, &bytes_read);
18855 mac_ptr += bytes_read;
18856 }
18857 break;
18858
18859 case DW_MACRO_GNU_start_file:
18860 {
18861 unsigned int bytes_read;
18862 int line, file;
18863
18864 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18865 mac_ptr += bytes_read;
18866 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18867 mac_ptr += bytes_read;
18868
18869 current_file = macro_start_file (file, line, current_file,
bb5ed363 18870 comp_dir, lh, objfile);
cf2c3c16
TT
18871 }
18872 break;
18873
18874 case DW_MACRO_GNU_end_file:
18875 /* No data to skip by MAC_PTR. */
18876 break;
18877
18878 case DW_MACRO_GNU_define_indirect:
18879 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18880 case DW_MACRO_GNU_define_indirect_alt:
18881 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18882 {
18883 unsigned int bytes_read;
18884
18885 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18886 mac_ptr += bytes_read;
18887 mac_ptr += offset_size;
18888 }
18889 break;
18890
18891 case DW_MACRO_GNU_transparent_include:
f7a35f02 18892 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18893 /* Note that, according to the spec, a transparent include
18894 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18895 skip this opcode. */
18896 mac_ptr += offset_size;
18897 break;
18898
18899 case DW_MACINFO_vendor_ext:
18900 /* Only skip the data by MAC_PTR. */
18901 if (!section_is_gnu)
18902 {
18903 unsigned int bytes_read;
18904
18905 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18906 mac_ptr += bytes_read;
18907 read_direct_string (abfd, mac_ptr, &bytes_read);
18908 mac_ptr += bytes_read;
18909 }
18910 /* FALLTHROUGH */
18911
18912 default:
18913 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18914 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18915 section);
18916 if (mac_ptr == NULL)
18917 return;
18918 break;
18919 }
18920 } while (macinfo_type != 0 && current_file == NULL);
18921
18922 /* Second pass: Process all entries.
18923
18924 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18925 command-line macro definitions/undefinitions. This flag is unset when we
18926 reach the first DW_MACINFO_start_file entry. */
18927
8fc3fc34
TT
18928 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18929 NULL, xcalloc, xfree);
18930 cleanup = make_cleanup_htab_delete (include_hash);
18931 mac_ptr = section->buffer + offset;
18932 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18933 *slot = mac_ptr;
18934 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18935 current_file, lh, comp_dir, section,
18936 section_is_gnu, 0,
8fc3fc34
TT
18937 offset_size, objfile, include_hash);
18938 do_cleanups (cleanup);
cf2c3c16
TT
18939}
18940
8e19ed76 18941/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18942 if so return true else false. */
380bca97 18943
8e19ed76
PS
18944static int
18945attr_form_is_block (struct attribute *attr)
18946{
18947 return (attr == NULL ? 0 :
18948 attr->form == DW_FORM_block1
18949 || attr->form == DW_FORM_block2
18950 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18951 || attr->form == DW_FORM_block
18952 || attr->form == DW_FORM_exprloc);
8e19ed76 18953}
4c2df51b 18954
c6a0999f
JB
18955/* Return non-zero if ATTR's value is a section offset --- classes
18956 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18957 You may use DW_UNSND (attr) to retrieve such offsets.
18958
18959 Section 7.5.4, "Attribute Encodings", explains that no attribute
18960 may have a value that belongs to more than one of these classes; it
18961 would be ambiguous if we did, because we use the same forms for all
18962 of them. */
380bca97 18963
3690dd37
JB
18964static int
18965attr_form_is_section_offset (struct attribute *attr)
18966{
18967 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18968 || attr->form == DW_FORM_data8
18969 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18970}
18971
3690dd37
JB
18972/* Return non-zero if ATTR's value falls in the 'constant' class, or
18973 zero otherwise. When this function returns true, you can apply
18974 dwarf2_get_attr_constant_value to it.
18975
18976 However, note that for some attributes you must check
18977 attr_form_is_section_offset before using this test. DW_FORM_data4
18978 and DW_FORM_data8 are members of both the constant class, and of
18979 the classes that contain offsets into other debug sections
18980 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18981 that, if an attribute's can be either a constant or one of the
18982 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18983 taken as section offsets, not constants. */
380bca97 18984
3690dd37
JB
18985static int
18986attr_form_is_constant (struct attribute *attr)
18987{
18988 switch (attr->form)
18989 {
18990 case DW_FORM_sdata:
18991 case DW_FORM_udata:
18992 case DW_FORM_data1:
18993 case DW_FORM_data2:
18994 case DW_FORM_data4:
18995 case DW_FORM_data8:
18996 return 1;
18997 default:
18998 return 0;
18999 }
19000}
19001
3019eac3
DE
19002/* Return the .debug_loc section to use for CU.
19003 For DWO files use .debug_loc.dwo. */
19004
19005static struct dwarf2_section_info *
19006cu_debug_loc_section (struct dwarf2_cu *cu)
19007{
19008 if (cu->dwo_unit)
19009 return &cu->dwo_unit->dwo_file->sections.loc;
19010 return &dwarf2_per_objfile->loc;
19011}
19012
8cf6f0b1
TT
19013/* A helper function that fills in a dwarf2_loclist_baton. */
19014
19015static void
19016fill_in_loclist_baton (struct dwarf2_cu *cu,
19017 struct dwarf2_loclist_baton *baton,
19018 struct attribute *attr)
19019{
3019eac3
DE
19020 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19021
19022 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19023
19024 baton->per_cu = cu->per_cu;
19025 gdb_assert (baton->per_cu);
19026 /* We don't know how long the location list is, but make sure we
19027 don't run off the edge of the section. */
3019eac3
DE
19028 baton->size = section->size - DW_UNSND (attr);
19029 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19030 baton->base_address = cu->base_address;
f664829e 19031 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19032}
19033
4c2df51b
DJ
19034static void
19035dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 19036 struct dwarf2_cu *cu)
4c2df51b 19037{
bb5ed363 19038 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19039 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19040
3690dd37 19041 if (attr_form_is_section_offset (attr)
3019eac3 19042 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19043 the section. If so, fall through to the complaint in the
19044 other branch. */
3019eac3 19045 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19046 {
0d53c4c4 19047 struct dwarf2_loclist_baton *baton;
4c2df51b 19048
bb5ed363 19049 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19050 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19051
8cf6f0b1 19052 fill_in_loclist_baton (cu, baton, attr);
be391dca 19053
d00adf39 19054 if (cu->base_known == 0)
0d53c4c4 19055 complaint (&symfile_complaints,
3e43a32a
MS
19056 _("Location list used without "
19057 "specifying the CU base address."));
4c2df51b 19058
768a979c 19059 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
19060 SYMBOL_LOCATION_BATON (sym) = baton;
19061 }
19062 else
19063 {
19064 struct dwarf2_locexpr_baton *baton;
19065
bb5ed363 19066 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19067 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19068 baton->per_cu = cu->per_cu;
19069 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19070
19071 if (attr_form_is_block (attr))
19072 {
19073 /* Note that we're just copying the block's data pointer
19074 here, not the actual data. We're still pointing into the
6502dd73
DJ
19075 info_buffer for SYM's objfile; right now we never release
19076 that buffer, but when we do clean up properly this may
19077 need to change. */
0d53c4c4
DJ
19078 baton->size = DW_BLOCK (attr)->size;
19079 baton->data = DW_BLOCK (attr)->data;
19080 }
19081 else
19082 {
19083 dwarf2_invalid_attrib_class_complaint ("location description",
19084 SYMBOL_NATURAL_NAME (sym));
19085 baton->size = 0;
0d53c4c4 19086 }
6e70227d 19087
768a979c 19088 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
19089 SYMBOL_LOCATION_BATON (sym) = baton;
19090 }
4c2df51b 19091}
6502dd73 19092
9aa1f1e3
TT
19093/* Return the OBJFILE associated with the compilation unit CU. If CU
19094 came from a separate debuginfo file, then the master objfile is
19095 returned. */
ae0d2f24
UW
19096
19097struct objfile *
19098dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19099{
9291a0cd 19100 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19101
19102 /* Return the master objfile, so that we can report and look up the
19103 correct file containing this variable. */
19104 if (objfile->separate_debug_objfile_backlink)
19105 objfile = objfile->separate_debug_objfile_backlink;
19106
19107 return objfile;
19108}
19109
96408a79
SA
19110/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19111 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19112 CU_HEADERP first. */
19113
19114static const struct comp_unit_head *
19115per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19116 struct dwarf2_per_cu_data *per_cu)
19117{
96408a79
SA
19118 gdb_byte *info_ptr;
19119
19120 if (per_cu->cu)
19121 return &per_cu->cu->header;
19122
0bc3a05c 19123 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19124
19125 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19126 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19127
19128 return cu_headerp;
19129}
19130
ae0d2f24
UW
19131/* Return the address size given in the compilation unit header for CU. */
19132
98714339 19133int
ae0d2f24
UW
19134dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19135{
96408a79
SA
19136 struct comp_unit_head cu_header_local;
19137 const struct comp_unit_head *cu_headerp;
c471e790 19138
96408a79
SA
19139 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19140
19141 return cu_headerp->addr_size;
ae0d2f24
UW
19142}
19143
9eae7c52
TT
19144/* Return the offset size given in the compilation unit header for CU. */
19145
19146int
19147dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19148{
96408a79
SA
19149 struct comp_unit_head cu_header_local;
19150 const struct comp_unit_head *cu_headerp;
9c6c53f7 19151
96408a79
SA
19152 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19153
19154 return cu_headerp->offset_size;
19155}
19156
19157/* See its dwarf2loc.h declaration. */
19158
19159int
19160dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19161{
19162 struct comp_unit_head cu_header_local;
19163 const struct comp_unit_head *cu_headerp;
19164
19165 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19166
19167 if (cu_headerp->version == 2)
19168 return cu_headerp->addr_size;
19169 else
19170 return cu_headerp->offset_size;
181cebd4
JK
19171}
19172
9aa1f1e3
TT
19173/* Return the text offset of the CU. The returned offset comes from
19174 this CU's objfile. If this objfile came from a separate debuginfo
19175 file, then the offset may be different from the corresponding
19176 offset in the parent objfile. */
19177
19178CORE_ADDR
19179dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19180{
bb3fa9d0 19181 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19182
19183 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19184}
19185
348e048f
DE
19186/* Locate the .debug_info compilation unit from CU's objfile which contains
19187 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19188
19189static struct dwarf2_per_cu_data *
b64f50a1 19190dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19191 unsigned int offset_in_dwz,
ae038cb0
DJ
19192 struct objfile *objfile)
19193{
19194 struct dwarf2_per_cu_data *this_cu;
19195 int low, high;
36586728 19196 const sect_offset *cu_off;
ae038cb0 19197
ae038cb0
DJ
19198 low = 0;
19199 high = dwarf2_per_objfile->n_comp_units - 1;
19200 while (high > low)
19201 {
36586728 19202 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19203 int mid = low + (high - low) / 2;
9a619af0 19204
36586728
TT
19205 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19206 cu_off = &mid_cu->offset;
19207 if (mid_cu->is_dwz > offset_in_dwz
19208 || (mid_cu->is_dwz == offset_in_dwz
19209 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19210 high = mid;
19211 else
19212 low = mid + 1;
19213 }
19214 gdb_assert (low == high);
36586728
TT
19215 this_cu = dwarf2_per_objfile->all_comp_units[low];
19216 cu_off = &this_cu->offset;
19217 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19218 {
36586728 19219 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19220 error (_("Dwarf Error: could not find partial DIE containing "
19221 "offset 0x%lx [in module %s]"),
b64f50a1 19222 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19223
b64f50a1
JK
19224 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19225 <= offset.sect_off);
ae038cb0
DJ
19226 return dwarf2_per_objfile->all_comp_units[low-1];
19227 }
19228 else
19229 {
19230 this_cu = dwarf2_per_objfile->all_comp_units[low];
19231 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19232 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19233 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19234 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19235 return this_cu;
19236 }
19237}
19238
23745b47 19239/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19240
9816fde3 19241static void
23745b47 19242init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19243{
9816fde3 19244 memset (cu, 0, sizeof (*cu));
23745b47
DE
19245 per_cu->cu = cu;
19246 cu->per_cu = per_cu;
19247 cu->objfile = per_cu->objfile;
93311388 19248 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19249}
19250
19251/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19252
19253static void
95554aad
TT
19254prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19255 enum language pretend_language)
9816fde3
JK
19256{
19257 struct attribute *attr;
19258
19259 /* Set the language we're debugging. */
19260 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19261 if (attr)
19262 set_cu_language (DW_UNSND (attr), cu);
19263 else
9cded63f 19264 {
95554aad 19265 cu->language = pretend_language;
9cded63f
TT
19266 cu->language_defn = language_def (cu->language);
19267 }
dee91e82
DE
19268
19269 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19270 if (attr)
19271 cu->producer = DW_STRING (attr);
93311388
DE
19272}
19273
ae038cb0
DJ
19274/* Release one cached compilation unit, CU. We unlink it from the tree
19275 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19276 the caller is responsible for that.
19277 NOTE: DATA is a void * because this function is also used as a
19278 cleanup routine. */
ae038cb0
DJ
19279
19280static void
68dc6402 19281free_heap_comp_unit (void *data)
ae038cb0
DJ
19282{
19283 struct dwarf2_cu *cu = data;
19284
23745b47
DE
19285 gdb_assert (cu->per_cu != NULL);
19286 cu->per_cu->cu = NULL;
ae038cb0
DJ
19287 cu->per_cu = NULL;
19288
19289 obstack_free (&cu->comp_unit_obstack, NULL);
19290
19291 xfree (cu);
19292}
19293
72bf9492 19294/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19295 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19296 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19297
19298static void
19299free_stack_comp_unit (void *data)
19300{
19301 struct dwarf2_cu *cu = data;
19302
23745b47
DE
19303 gdb_assert (cu->per_cu != NULL);
19304 cu->per_cu->cu = NULL;
19305 cu->per_cu = NULL;
19306
72bf9492
DJ
19307 obstack_free (&cu->comp_unit_obstack, NULL);
19308 cu->partial_dies = NULL;
ae038cb0
DJ
19309}
19310
19311/* Free all cached compilation units. */
19312
19313static void
19314free_cached_comp_units (void *data)
19315{
19316 struct dwarf2_per_cu_data *per_cu, **last_chain;
19317
19318 per_cu = dwarf2_per_objfile->read_in_chain;
19319 last_chain = &dwarf2_per_objfile->read_in_chain;
19320 while (per_cu != NULL)
19321 {
19322 struct dwarf2_per_cu_data *next_cu;
19323
19324 next_cu = per_cu->cu->read_in_chain;
19325
68dc6402 19326 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19327 *last_chain = next_cu;
19328
19329 per_cu = next_cu;
19330 }
19331}
19332
19333/* Increase the age counter on each cached compilation unit, and free
19334 any that are too old. */
19335
19336static void
19337age_cached_comp_units (void)
19338{
19339 struct dwarf2_per_cu_data *per_cu, **last_chain;
19340
19341 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19342 per_cu = dwarf2_per_objfile->read_in_chain;
19343 while (per_cu != NULL)
19344 {
19345 per_cu->cu->last_used ++;
19346 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19347 dwarf2_mark (per_cu->cu);
19348 per_cu = per_cu->cu->read_in_chain;
19349 }
19350
19351 per_cu = dwarf2_per_objfile->read_in_chain;
19352 last_chain = &dwarf2_per_objfile->read_in_chain;
19353 while (per_cu != NULL)
19354 {
19355 struct dwarf2_per_cu_data *next_cu;
19356
19357 next_cu = per_cu->cu->read_in_chain;
19358
19359 if (!per_cu->cu->mark)
19360 {
68dc6402 19361 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19362 *last_chain = next_cu;
19363 }
19364 else
19365 last_chain = &per_cu->cu->read_in_chain;
19366
19367 per_cu = next_cu;
19368 }
19369}
19370
19371/* Remove a single compilation unit from the cache. */
19372
19373static void
dee91e82 19374free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19375{
19376 struct dwarf2_per_cu_data *per_cu, **last_chain;
19377
19378 per_cu = dwarf2_per_objfile->read_in_chain;
19379 last_chain = &dwarf2_per_objfile->read_in_chain;
19380 while (per_cu != NULL)
19381 {
19382 struct dwarf2_per_cu_data *next_cu;
19383
19384 next_cu = per_cu->cu->read_in_chain;
19385
dee91e82 19386 if (per_cu == target_per_cu)
ae038cb0 19387 {
68dc6402 19388 free_heap_comp_unit (per_cu->cu);
dee91e82 19389 per_cu->cu = NULL;
ae038cb0
DJ
19390 *last_chain = next_cu;
19391 break;
19392 }
19393 else
19394 last_chain = &per_cu->cu->read_in_chain;
19395
19396 per_cu = next_cu;
19397 }
19398}
19399
fe3e1990
DJ
19400/* Release all extra memory associated with OBJFILE. */
19401
19402void
19403dwarf2_free_objfile (struct objfile *objfile)
19404{
19405 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19406
19407 if (dwarf2_per_objfile == NULL)
19408 return;
19409
19410 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19411 free_cached_comp_units (NULL);
19412
7b9f3c50
DE
19413 if (dwarf2_per_objfile->quick_file_names_table)
19414 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19415
fe3e1990
DJ
19416 /* Everything else should be on the objfile obstack. */
19417}
19418
dee91e82
DE
19419/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19420 We store these in a hash table separate from the DIEs, and preserve them
19421 when the DIEs are flushed out of cache.
19422
19423 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19424 uniquely identify the type. A file may have multiple .debug_types sections,
19425 or the type may come from a DWO file. We have to use something in
19426 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19427 routine, get_die_type_at_offset, from outside this file, and thus won't
19428 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19429 of the objfile. */
1c379e20 19430
dee91e82 19431struct dwarf2_per_cu_offset_and_type
1c379e20 19432{
dee91e82 19433 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19434 sect_offset offset;
1c379e20
DJ
19435 struct type *type;
19436};
19437
dee91e82 19438/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19439
19440static hashval_t
dee91e82 19441per_cu_offset_and_type_hash (const void *item)
1c379e20 19442{
dee91e82 19443 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19444
dee91e82 19445 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19446}
19447
dee91e82 19448/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19449
19450static int
dee91e82 19451per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19452{
dee91e82
DE
19453 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19454 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19455
dee91e82
DE
19456 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19457 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19458}
19459
19460/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19461 table if necessary. For convenience, return TYPE.
19462
19463 The DIEs reading must have careful ordering to:
19464 * Not cause infite loops trying to read in DIEs as a prerequisite for
19465 reading current DIE.
19466 * Not trying to dereference contents of still incompletely read in types
19467 while reading in other DIEs.
19468 * Enable referencing still incompletely read in types just by a pointer to
19469 the type without accessing its fields.
19470
19471 Therefore caller should follow these rules:
19472 * Try to fetch any prerequisite types we may need to build this DIE type
19473 before building the type and calling set_die_type.
e71ec853 19474 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19475 possible before fetching more types to complete the current type.
19476 * Make the type as complete as possible before fetching more types. */
1c379e20 19477
f792889a 19478static struct type *
1c379e20
DJ
19479set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19480{
dee91e82 19481 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19482 struct objfile *objfile = cu->objfile;
1c379e20 19483
b4ba55a1
JB
19484 /* For Ada types, make sure that the gnat-specific data is always
19485 initialized (if not already set). There are a few types where
19486 we should not be doing so, because the type-specific area is
19487 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19488 where the type-specific area is used to store the floatformat).
19489 But this is not a problem, because the gnat-specific information
19490 is actually not needed for these types. */
19491 if (need_gnat_info (cu)
19492 && TYPE_CODE (type) != TYPE_CODE_FUNC
19493 && TYPE_CODE (type) != TYPE_CODE_FLT
19494 && !HAVE_GNAT_AUX_INFO (type))
19495 INIT_GNAT_SPECIFIC (type);
19496
dee91e82 19497 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19498 {
dee91e82
DE
19499 dwarf2_per_objfile->die_type_hash =
19500 htab_create_alloc_ex (127,
19501 per_cu_offset_and_type_hash,
19502 per_cu_offset_and_type_eq,
19503 NULL,
19504 &objfile->objfile_obstack,
19505 hashtab_obstack_allocate,
19506 dummy_obstack_deallocate);
f792889a 19507 }
1c379e20 19508
dee91e82 19509 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19510 ofs.offset = die->offset;
19511 ofs.type = type;
dee91e82
DE
19512 slot = (struct dwarf2_per_cu_offset_and_type **)
19513 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19514 if (*slot)
19515 complaint (&symfile_complaints,
19516 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19517 die->offset.sect_off);
673bfd45 19518 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19519 **slot = ofs;
f792889a 19520 return type;
1c379e20
DJ
19521}
19522
380bca97 19523/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19524 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19525
19526static struct type *
b64f50a1 19527get_die_type_at_offset (sect_offset offset,
673bfd45 19528 struct dwarf2_per_cu_data *per_cu)
1c379e20 19529{
dee91e82 19530 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19531
dee91e82 19532 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19533 return NULL;
1c379e20 19534
dee91e82 19535 ofs.per_cu = per_cu;
673bfd45 19536 ofs.offset = offset;
dee91e82 19537 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19538 if (slot)
19539 return slot->type;
19540 else
19541 return NULL;
19542}
19543
673bfd45
DE
19544/* Look up the type for DIE in the appropriate type_hash table,
19545 or return NULL if DIE does not have a saved type. */
19546
19547static struct type *
19548get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19549{
19550 return get_die_type_at_offset (die->offset, cu->per_cu);
19551}
19552
10b3939b
DJ
19553/* Add a dependence relationship from CU to REF_PER_CU. */
19554
19555static void
19556dwarf2_add_dependence (struct dwarf2_cu *cu,
19557 struct dwarf2_per_cu_data *ref_per_cu)
19558{
19559 void **slot;
19560
19561 if (cu->dependencies == NULL)
19562 cu->dependencies
19563 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19564 NULL, &cu->comp_unit_obstack,
19565 hashtab_obstack_allocate,
19566 dummy_obstack_deallocate);
19567
19568 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19569 if (*slot == NULL)
19570 *slot = ref_per_cu;
19571}
1c379e20 19572
f504f079
DE
19573/* Subroutine of dwarf2_mark to pass to htab_traverse.
19574 Set the mark field in every compilation unit in the
ae038cb0
DJ
19575 cache that we must keep because we are keeping CU. */
19576
10b3939b
DJ
19577static int
19578dwarf2_mark_helper (void **slot, void *data)
19579{
19580 struct dwarf2_per_cu_data *per_cu;
19581
19582 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19583
19584 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19585 reading of the chain. As such dependencies remain valid it is not much
19586 useful to track and undo them during QUIT cleanups. */
19587 if (per_cu->cu == NULL)
19588 return 1;
19589
10b3939b
DJ
19590 if (per_cu->cu->mark)
19591 return 1;
19592 per_cu->cu->mark = 1;
19593
19594 if (per_cu->cu->dependencies != NULL)
19595 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19596
19597 return 1;
19598}
19599
f504f079
DE
19600/* Set the mark field in CU and in every other compilation unit in the
19601 cache that we must keep because we are keeping CU. */
19602
ae038cb0
DJ
19603static void
19604dwarf2_mark (struct dwarf2_cu *cu)
19605{
19606 if (cu->mark)
19607 return;
19608 cu->mark = 1;
10b3939b
DJ
19609 if (cu->dependencies != NULL)
19610 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19611}
19612
19613static void
19614dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19615{
19616 while (per_cu)
19617 {
19618 per_cu->cu->mark = 0;
19619 per_cu = per_cu->cu->read_in_chain;
19620 }
72bf9492
DJ
19621}
19622
72bf9492
DJ
19623/* Trivial hash function for partial_die_info: the hash value of a DIE
19624 is its offset in .debug_info for this objfile. */
19625
19626static hashval_t
19627partial_die_hash (const void *item)
19628{
19629 const struct partial_die_info *part_die = item;
9a619af0 19630
b64f50a1 19631 return part_die->offset.sect_off;
72bf9492
DJ
19632}
19633
19634/* Trivial comparison function for partial_die_info structures: two DIEs
19635 are equal if they have the same offset. */
19636
19637static int
19638partial_die_eq (const void *item_lhs, const void *item_rhs)
19639{
19640 const struct partial_die_info *part_die_lhs = item_lhs;
19641 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19642
b64f50a1 19643 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19644}
19645
ae038cb0
DJ
19646static struct cmd_list_element *set_dwarf2_cmdlist;
19647static struct cmd_list_element *show_dwarf2_cmdlist;
19648
19649static void
19650set_dwarf2_cmd (char *args, int from_tty)
19651{
19652 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19653}
19654
19655static void
19656show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19657{
ae038cb0
DJ
19658 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19659}
19660
4bf44c1c 19661/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19662
19663static void
c1bd65d0 19664dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19665{
19666 struct dwarf2_per_objfile *data = d;
8b70b953 19667 int ix;
8b70b953 19668
95554aad
TT
19669 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19670 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19671 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19672
8b70b953 19673 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19674
19675 if (data->dwo_files)
19676 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19677
19678 if (data->dwz_file && data->dwz_file->dwz_bfd)
19679 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19680}
19681
19682\f
ae2de4f8 19683/* The "save gdb-index" command. */
9291a0cd
TT
19684
19685/* The contents of the hash table we create when building the string
19686 table. */
19687struct strtab_entry
19688{
19689 offset_type offset;
19690 const char *str;
19691};
19692
559a7a62
JK
19693/* Hash function for a strtab_entry.
19694
19695 Function is used only during write_hash_table so no index format backward
19696 compatibility is needed. */
b89be57b 19697
9291a0cd
TT
19698static hashval_t
19699hash_strtab_entry (const void *e)
19700{
19701 const struct strtab_entry *entry = e;
559a7a62 19702 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19703}
19704
19705/* Equality function for a strtab_entry. */
b89be57b 19706
9291a0cd
TT
19707static int
19708eq_strtab_entry (const void *a, const void *b)
19709{
19710 const struct strtab_entry *ea = a;
19711 const struct strtab_entry *eb = b;
19712 return !strcmp (ea->str, eb->str);
19713}
19714
19715/* Create a strtab_entry hash table. */
b89be57b 19716
9291a0cd
TT
19717static htab_t
19718create_strtab (void)
19719{
19720 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19721 xfree, xcalloc, xfree);
19722}
19723
19724/* Add a string to the constant pool. Return the string's offset in
19725 host order. */
b89be57b 19726
9291a0cd
TT
19727static offset_type
19728add_string (htab_t table, struct obstack *cpool, const char *str)
19729{
19730 void **slot;
19731 struct strtab_entry entry;
19732 struct strtab_entry *result;
19733
19734 entry.str = str;
19735 slot = htab_find_slot (table, &entry, INSERT);
19736 if (*slot)
19737 result = *slot;
19738 else
19739 {
19740 result = XNEW (struct strtab_entry);
19741 result->offset = obstack_object_size (cpool);
19742 result->str = str;
19743 obstack_grow_str0 (cpool, str);
19744 *slot = result;
19745 }
19746 return result->offset;
19747}
19748
19749/* An entry in the symbol table. */
19750struct symtab_index_entry
19751{
19752 /* The name of the symbol. */
19753 const char *name;
19754 /* The offset of the name in the constant pool. */
19755 offset_type index_offset;
19756 /* A sorted vector of the indices of all the CUs that hold an object
19757 of this name. */
19758 VEC (offset_type) *cu_indices;
19759};
19760
19761/* The symbol table. This is a power-of-2-sized hash table. */
19762struct mapped_symtab
19763{
19764 offset_type n_elements;
19765 offset_type size;
19766 struct symtab_index_entry **data;
19767};
19768
19769/* Hash function for a symtab_index_entry. */
b89be57b 19770
9291a0cd
TT
19771static hashval_t
19772hash_symtab_entry (const void *e)
19773{
19774 const struct symtab_index_entry *entry = e;
19775 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19776 sizeof (offset_type) * VEC_length (offset_type,
19777 entry->cu_indices),
19778 0);
19779}
19780
19781/* Equality function for a symtab_index_entry. */
b89be57b 19782
9291a0cd
TT
19783static int
19784eq_symtab_entry (const void *a, const void *b)
19785{
19786 const struct symtab_index_entry *ea = a;
19787 const struct symtab_index_entry *eb = b;
19788 int len = VEC_length (offset_type, ea->cu_indices);
19789 if (len != VEC_length (offset_type, eb->cu_indices))
19790 return 0;
19791 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19792 VEC_address (offset_type, eb->cu_indices),
19793 sizeof (offset_type) * len);
19794}
19795
19796/* Destroy a symtab_index_entry. */
b89be57b 19797
9291a0cd
TT
19798static void
19799delete_symtab_entry (void *p)
19800{
19801 struct symtab_index_entry *entry = p;
19802 VEC_free (offset_type, entry->cu_indices);
19803 xfree (entry);
19804}
19805
19806/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19807
9291a0cd 19808static htab_t
3876f04e 19809create_symbol_hash_table (void)
9291a0cd
TT
19810{
19811 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19812 delete_symtab_entry, xcalloc, xfree);
19813}
19814
19815/* Create a new mapped symtab object. */
b89be57b 19816
9291a0cd
TT
19817static struct mapped_symtab *
19818create_mapped_symtab (void)
19819{
19820 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19821 symtab->n_elements = 0;
19822 symtab->size = 1024;
19823 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19824 return symtab;
19825}
19826
19827/* Destroy a mapped_symtab. */
b89be57b 19828
9291a0cd
TT
19829static void
19830cleanup_mapped_symtab (void *p)
19831{
19832 struct mapped_symtab *symtab = p;
19833 /* The contents of the array are freed when the other hash table is
19834 destroyed. */
19835 xfree (symtab->data);
19836 xfree (symtab);
19837}
19838
19839/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19840 the slot.
19841
19842 Function is used only during write_hash_table so no index format backward
19843 compatibility is needed. */
b89be57b 19844
9291a0cd
TT
19845static struct symtab_index_entry **
19846find_slot (struct mapped_symtab *symtab, const char *name)
19847{
559a7a62 19848 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19849
19850 index = hash & (symtab->size - 1);
19851 step = ((hash * 17) & (symtab->size - 1)) | 1;
19852
19853 for (;;)
19854 {
19855 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19856 return &symtab->data[index];
19857 index = (index + step) & (symtab->size - 1);
19858 }
19859}
19860
19861/* Expand SYMTAB's hash table. */
b89be57b 19862
9291a0cd
TT
19863static void
19864hash_expand (struct mapped_symtab *symtab)
19865{
19866 offset_type old_size = symtab->size;
19867 offset_type i;
19868 struct symtab_index_entry **old_entries = symtab->data;
19869
19870 symtab->size *= 2;
19871 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19872
19873 for (i = 0; i < old_size; ++i)
19874 {
19875 if (old_entries[i])
19876 {
19877 struct symtab_index_entry **slot = find_slot (symtab,
19878 old_entries[i]->name);
19879 *slot = old_entries[i];
19880 }
19881 }
19882
19883 xfree (old_entries);
19884}
19885
156942c7
DE
19886/* Add an entry to SYMTAB. NAME is the name of the symbol.
19887 CU_INDEX is the index of the CU in which the symbol appears.
19888 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19889
9291a0cd
TT
19890static void
19891add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19892 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19893 offset_type cu_index)
19894{
19895 struct symtab_index_entry **slot;
156942c7 19896 offset_type cu_index_and_attrs;
9291a0cd
TT
19897
19898 ++symtab->n_elements;
19899 if (4 * symtab->n_elements / 3 >= symtab->size)
19900 hash_expand (symtab);
19901
19902 slot = find_slot (symtab, name);
19903 if (!*slot)
19904 {
19905 *slot = XNEW (struct symtab_index_entry);
19906 (*slot)->name = name;
156942c7 19907 /* index_offset is set later. */
9291a0cd
TT
19908 (*slot)->cu_indices = NULL;
19909 }
156942c7
DE
19910
19911 cu_index_and_attrs = 0;
19912 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19913 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19914 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19915
19916 /* We don't want to record an index value twice as we want to avoid the
19917 duplication.
19918 We process all global symbols and then all static symbols
19919 (which would allow us to avoid the duplication by only having to check
19920 the last entry pushed), but a symbol could have multiple kinds in one CU.
19921 To keep things simple we don't worry about the duplication here and
19922 sort and uniqufy the list after we've processed all symbols. */
19923 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19924}
19925
19926/* qsort helper routine for uniquify_cu_indices. */
19927
19928static int
19929offset_type_compare (const void *ap, const void *bp)
19930{
19931 offset_type a = *(offset_type *) ap;
19932 offset_type b = *(offset_type *) bp;
19933
19934 return (a > b) - (b > a);
19935}
19936
19937/* Sort and remove duplicates of all symbols' cu_indices lists. */
19938
19939static void
19940uniquify_cu_indices (struct mapped_symtab *symtab)
19941{
19942 int i;
19943
19944 for (i = 0; i < symtab->size; ++i)
19945 {
19946 struct symtab_index_entry *entry = symtab->data[i];
19947
19948 if (entry
19949 && entry->cu_indices != NULL)
19950 {
19951 unsigned int next_to_insert, next_to_check;
19952 offset_type last_value;
19953
19954 qsort (VEC_address (offset_type, entry->cu_indices),
19955 VEC_length (offset_type, entry->cu_indices),
19956 sizeof (offset_type), offset_type_compare);
19957
19958 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19959 next_to_insert = 1;
19960 for (next_to_check = 1;
19961 next_to_check < VEC_length (offset_type, entry->cu_indices);
19962 ++next_to_check)
19963 {
19964 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19965 != last_value)
19966 {
19967 last_value = VEC_index (offset_type, entry->cu_indices,
19968 next_to_check);
19969 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19970 last_value);
19971 ++next_to_insert;
19972 }
19973 }
19974 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19975 }
19976 }
9291a0cd
TT
19977}
19978
19979/* Add a vector of indices to the constant pool. */
b89be57b 19980
9291a0cd 19981static offset_type
3876f04e 19982add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19983 struct symtab_index_entry *entry)
19984{
19985 void **slot;
19986
3876f04e 19987 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19988 if (!*slot)
19989 {
19990 offset_type len = VEC_length (offset_type, entry->cu_indices);
19991 offset_type val = MAYBE_SWAP (len);
19992 offset_type iter;
19993 int i;
19994
19995 *slot = entry;
19996 entry->index_offset = obstack_object_size (cpool);
19997
19998 obstack_grow (cpool, &val, sizeof (val));
19999 for (i = 0;
20000 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20001 ++i)
20002 {
20003 val = MAYBE_SWAP (iter);
20004 obstack_grow (cpool, &val, sizeof (val));
20005 }
20006 }
20007 else
20008 {
20009 struct symtab_index_entry *old_entry = *slot;
20010 entry->index_offset = old_entry->index_offset;
20011 entry = old_entry;
20012 }
20013 return entry->index_offset;
20014}
20015
20016/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20017 constant pool entries going into the obstack CPOOL. */
b89be57b 20018
9291a0cd
TT
20019static void
20020write_hash_table (struct mapped_symtab *symtab,
20021 struct obstack *output, struct obstack *cpool)
20022{
20023 offset_type i;
3876f04e 20024 htab_t symbol_hash_table;
9291a0cd
TT
20025 htab_t str_table;
20026
3876f04e 20027 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20028 str_table = create_strtab ();
3876f04e 20029
9291a0cd
TT
20030 /* We add all the index vectors to the constant pool first, to
20031 ensure alignment is ok. */
20032 for (i = 0; i < symtab->size; ++i)
20033 {
20034 if (symtab->data[i])
3876f04e 20035 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20036 }
20037
20038 /* Now write out the hash table. */
20039 for (i = 0; i < symtab->size; ++i)
20040 {
20041 offset_type str_off, vec_off;
20042
20043 if (symtab->data[i])
20044 {
20045 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20046 vec_off = symtab->data[i]->index_offset;
20047 }
20048 else
20049 {
20050 /* While 0 is a valid constant pool index, it is not valid
20051 to have 0 for both offsets. */
20052 str_off = 0;
20053 vec_off = 0;
20054 }
20055
20056 str_off = MAYBE_SWAP (str_off);
20057 vec_off = MAYBE_SWAP (vec_off);
20058
20059 obstack_grow (output, &str_off, sizeof (str_off));
20060 obstack_grow (output, &vec_off, sizeof (vec_off));
20061 }
20062
20063 htab_delete (str_table);
3876f04e 20064 htab_delete (symbol_hash_table);
9291a0cd
TT
20065}
20066
0a5429f6
DE
20067/* Struct to map psymtab to CU index in the index file. */
20068struct psymtab_cu_index_map
20069{
20070 struct partial_symtab *psymtab;
20071 unsigned int cu_index;
20072};
20073
20074static hashval_t
20075hash_psymtab_cu_index (const void *item)
20076{
20077 const struct psymtab_cu_index_map *map = item;
20078
20079 return htab_hash_pointer (map->psymtab);
20080}
20081
20082static int
20083eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20084{
20085 const struct psymtab_cu_index_map *lhs = item_lhs;
20086 const struct psymtab_cu_index_map *rhs = item_rhs;
20087
20088 return lhs->psymtab == rhs->psymtab;
20089}
20090
20091/* Helper struct for building the address table. */
20092struct addrmap_index_data
20093{
20094 struct objfile *objfile;
20095 struct obstack *addr_obstack;
20096 htab_t cu_index_htab;
20097
20098 /* Non-zero if the previous_* fields are valid.
20099 We can't write an entry until we see the next entry (since it is only then
20100 that we know the end of the entry). */
20101 int previous_valid;
20102 /* Index of the CU in the table of all CUs in the index file. */
20103 unsigned int previous_cu_index;
0963b4bd 20104 /* Start address of the CU. */
0a5429f6
DE
20105 CORE_ADDR previous_cu_start;
20106};
20107
20108/* Write an address entry to OBSTACK. */
b89be57b 20109
9291a0cd 20110static void
0a5429f6
DE
20111add_address_entry (struct objfile *objfile, struct obstack *obstack,
20112 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20113{
0a5429f6 20114 offset_type cu_index_to_write;
9291a0cd
TT
20115 char addr[8];
20116 CORE_ADDR baseaddr;
20117
20118 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20119
0a5429f6
DE
20120 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20121 obstack_grow (obstack, addr, 8);
20122 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20123 obstack_grow (obstack, addr, 8);
20124 cu_index_to_write = MAYBE_SWAP (cu_index);
20125 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20126}
20127
20128/* Worker function for traversing an addrmap to build the address table. */
20129
20130static int
20131add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20132{
20133 struct addrmap_index_data *data = datap;
20134 struct partial_symtab *pst = obj;
0a5429f6
DE
20135
20136 if (data->previous_valid)
20137 add_address_entry (data->objfile, data->addr_obstack,
20138 data->previous_cu_start, start_addr,
20139 data->previous_cu_index);
20140
20141 data->previous_cu_start = start_addr;
20142 if (pst != NULL)
20143 {
20144 struct psymtab_cu_index_map find_map, *map;
20145 find_map.psymtab = pst;
20146 map = htab_find (data->cu_index_htab, &find_map);
20147 gdb_assert (map != NULL);
20148 data->previous_cu_index = map->cu_index;
20149 data->previous_valid = 1;
20150 }
20151 else
20152 data->previous_valid = 0;
20153
20154 return 0;
20155}
20156
20157/* Write OBJFILE's address map to OBSTACK.
20158 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20159 in the index file. */
20160
20161static void
20162write_address_map (struct objfile *objfile, struct obstack *obstack,
20163 htab_t cu_index_htab)
20164{
20165 struct addrmap_index_data addrmap_index_data;
20166
20167 /* When writing the address table, we have to cope with the fact that
20168 the addrmap iterator only provides the start of a region; we have to
20169 wait until the next invocation to get the start of the next region. */
20170
20171 addrmap_index_data.objfile = objfile;
20172 addrmap_index_data.addr_obstack = obstack;
20173 addrmap_index_data.cu_index_htab = cu_index_htab;
20174 addrmap_index_data.previous_valid = 0;
20175
20176 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20177 &addrmap_index_data);
20178
20179 /* It's highly unlikely the last entry (end address = 0xff...ff)
20180 is valid, but we should still handle it.
20181 The end address is recorded as the start of the next region, but that
20182 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20183 anyway. */
20184 if (addrmap_index_data.previous_valid)
20185 add_address_entry (objfile, obstack,
20186 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20187 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20188}
20189
156942c7
DE
20190/* Return the symbol kind of PSYM. */
20191
20192static gdb_index_symbol_kind
20193symbol_kind (struct partial_symbol *psym)
20194{
20195 domain_enum domain = PSYMBOL_DOMAIN (psym);
20196 enum address_class aclass = PSYMBOL_CLASS (psym);
20197
20198 switch (domain)
20199 {
20200 case VAR_DOMAIN:
20201 switch (aclass)
20202 {
20203 case LOC_BLOCK:
20204 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20205 case LOC_TYPEDEF:
20206 return GDB_INDEX_SYMBOL_KIND_TYPE;
20207 case LOC_COMPUTED:
20208 case LOC_CONST_BYTES:
20209 case LOC_OPTIMIZED_OUT:
20210 case LOC_STATIC:
20211 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20212 case LOC_CONST:
20213 /* Note: It's currently impossible to recognize psyms as enum values
20214 short of reading the type info. For now punt. */
20215 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20216 default:
20217 /* There are other LOC_FOO values that one might want to classify
20218 as variables, but dwarf2read.c doesn't currently use them. */
20219 return GDB_INDEX_SYMBOL_KIND_OTHER;
20220 }
20221 case STRUCT_DOMAIN:
20222 return GDB_INDEX_SYMBOL_KIND_TYPE;
20223 default:
20224 return GDB_INDEX_SYMBOL_KIND_OTHER;
20225 }
20226}
20227
9291a0cd 20228/* Add a list of partial symbols to SYMTAB. */
b89be57b 20229
9291a0cd
TT
20230static void
20231write_psymbols (struct mapped_symtab *symtab,
987d643c 20232 htab_t psyms_seen,
9291a0cd
TT
20233 struct partial_symbol **psymp,
20234 int count,
987d643c
TT
20235 offset_type cu_index,
20236 int is_static)
9291a0cd
TT
20237{
20238 for (; count-- > 0; ++psymp)
20239 {
156942c7
DE
20240 struct partial_symbol *psym = *psymp;
20241 void **slot;
987d643c 20242
156942c7 20243 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20244 error (_("Ada is not currently supported by the index"));
987d643c 20245
987d643c 20246 /* Only add a given psymbol once. */
156942c7 20247 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20248 if (!*slot)
20249 {
156942c7
DE
20250 gdb_index_symbol_kind kind = symbol_kind (psym);
20251
20252 *slot = psym;
20253 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20254 is_static, kind, cu_index);
987d643c 20255 }
9291a0cd
TT
20256 }
20257}
20258
20259/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20260 exception if there is an error. */
b89be57b 20261
9291a0cd
TT
20262static void
20263write_obstack (FILE *file, struct obstack *obstack)
20264{
20265 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20266 file)
20267 != obstack_object_size (obstack))
20268 error (_("couldn't data write to file"));
20269}
20270
20271/* Unlink a file if the argument is not NULL. */
b89be57b 20272
9291a0cd
TT
20273static void
20274unlink_if_set (void *p)
20275{
20276 char **filename = p;
20277 if (*filename)
20278 unlink (*filename);
20279}
20280
1fd400ff
TT
20281/* A helper struct used when iterating over debug_types. */
20282struct signatured_type_index_data
20283{
20284 struct objfile *objfile;
20285 struct mapped_symtab *symtab;
20286 struct obstack *types_list;
987d643c 20287 htab_t psyms_seen;
1fd400ff
TT
20288 int cu_index;
20289};
20290
20291/* A helper function that writes a single signatured_type to an
20292 obstack. */
b89be57b 20293
1fd400ff
TT
20294static int
20295write_one_signatured_type (void **slot, void *d)
20296{
20297 struct signatured_type_index_data *info = d;
20298 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20299 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20300 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20301 gdb_byte val[8];
20302
20303 write_psymbols (info->symtab,
987d643c 20304 info->psyms_seen,
3e43a32a
MS
20305 info->objfile->global_psymbols.list
20306 + psymtab->globals_offset,
987d643c
TT
20307 psymtab->n_global_syms, info->cu_index,
20308 0);
1fd400ff 20309 write_psymbols (info->symtab,
987d643c 20310 info->psyms_seen,
3e43a32a
MS
20311 info->objfile->static_psymbols.list
20312 + psymtab->statics_offset,
987d643c
TT
20313 psymtab->n_static_syms, info->cu_index,
20314 1);
1fd400ff 20315
b64f50a1
JK
20316 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20317 entry->per_cu.offset.sect_off);
1fd400ff 20318 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20319 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20320 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20321 obstack_grow (info->types_list, val, 8);
20322 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20323 obstack_grow (info->types_list, val, 8);
20324
20325 ++info->cu_index;
20326
20327 return 1;
20328}
20329
95554aad
TT
20330/* Recurse into all "included" dependencies and write their symbols as
20331 if they appeared in this psymtab. */
20332
20333static void
20334recursively_write_psymbols (struct objfile *objfile,
20335 struct partial_symtab *psymtab,
20336 struct mapped_symtab *symtab,
20337 htab_t psyms_seen,
20338 offset_type cu_index)
20339{
20340 int i;
20341
20342 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20343 if (psymtab->dependencies[i]->user != NULL)
20344 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20345 symtab, psyms_seen, cu_index);
20346
20347 write_psymbols (symtab,
20348 psyms_seen,
20349 objfile->global_psymbols.list + psymtab->globals_offset,
20350 psymtab->n_global_syms, cu_index,
20351 0);
20352 write_psymbols (symtab,
20353 psyms_seen,
20354 objfile->static_psymbols.list + psymtab->statics_offset,
20355 psymtab->n_static_syms, cu_index,
20356 1);
20357}
20358
9291a0cd 20359/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20360
9291a0cd
TT
20361static void
20362write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20363{
20364 struct cleanup *cleanup;
20365 char *filename, *cleanup_filename;
1fd400ff
TT
20366 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20367 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20368 int i;
20369 FILE *out_file;
20370 struct mapped_symtab *symtab;
20371 offset_type val, size_of_contents, total_len;
20372 struct stat st;
987d643c 20373 htab_t psyms_seen;
0a5429f6
DE
20374 htab_t cu_index_htab;
20375 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20376
b4f2f049 20377 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20378 return;
b4f2f049 20379
9291a0cd
TT
20380 if (dwarf2_per_objfile->using_index)
20381 error (_("Cannot use an index to create the index"));
20382
8b70b953
TT
20383 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20384 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20385
9291a0cd 20386 if (stat (objfile->name, &st) < 0)
7e17e088 20387 perror_with_name (objfile->name);
9291a0cd
TT
20388
20389 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20390 INDEX_SUFFIX, (char *) NULL);
20391 cleanup = make_cleanup (xfree, filename);
20392
20393 out_file = fopen (filename, "wb");
20394 if (!out_file)
20395 error (_("Can't open `%s' for writing"), filename);
20396
20397 cleanup_filename = filename;
20398 make_cleanup (unlink_if_set, &cleanup_filename);
20399
20400 symtab = create_mapped_symtab ();
20401 make_cleanup (cleanup_mapped_symtab, symtab);
20402
20403 obstack_init (&addr_obstack);
20404 make_cleanup_obstack_free (&addr_obstack);
20405
20406 obstack_init (&cu_list);
20407 make_cleanup_obstack_free (&cu_list);
20408
1fd400ff
TT
20409 obstack_init (&types_cu_list);
20410 make_cleanup_obstack_free (&types_cu_list);
20411
987d643c
TT
20412 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20413 NULL, xcalloc, xfree);
96408a79 20414 make_cleanup_htab_delete (psyms_seen);
987d643c 20415
0a5429f6
DE
20416 /* While we're scanning CU's create a table that maps a psymtab pointer
20417 (which is what addrmap records) to its index (which is what is recorded
20418 in the index file). This will later be needed to write the address
20419 table. */
20420 cu_index_htab = htab_create_alloc (100,
20421 hash_psymtab_cu_index,
20422 eq_psymtab_cu_index,
20423 NULL, xcalloc, xfree);
96408a79 20424 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20425 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20426 xmalloc (sizeof (struct psymtab_cu_index_map)
20427 * dwarf2_per_objfile->n_comp_units);
20428 make_cleanup (xfree, psymtab_cu_index_map);
20429
20430 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20431 work here. Also, the debug_types entries do not appear in
20432 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20433 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20434 {
3e43a32a
MS
20435 struct dwarf2_per_cu_data *per_cu
20436 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20437 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20438 gdb_byte val[8];
0a5429f6
DE
20439 struct psymtab_cu_index_map *map;
20440 void **slot;
9291a0cd 20441
95554aad
TT
20442 if (psymtab->user == NULL)
20443 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20444
0a5429f6
DE
20445 map = &psymtab_cu_index_map[i];
20446 map->psymtab = psymtab;
20447 map->cu_index = i;
20448 slot = htab_find_slot (cu_index_htab, map, INSERT);
20449 gdb_assert (slot != NULL);
20450 gdb_assert (*slot == NULL);
20451 *slot = map;
9291a0cd 20452
b64f50a1
JK
20453 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20454 per_cu->offset.sect_off);
9291a0cd 20455 obstack_grow (&cu_list, val, 8);
e254ef6a 20456 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20457 obstack_grow (&cu_list, val, 8);
20458 }
20459
0a5429f6
DE
20460 /* Dump the address map. */
20461 write_address_map (objfile, &addr_obstack, cu_index_htab);
20462
1fd400ff
TT
20463 /* Write out the .debug_type entries, if any. */
20464 if (dwarf2_per_objfile->signatured_types)
20465 {
20466 struct signatured_type_index_data sig_data;
20467
20468 sig_data.objfile = objfile;
20469 sig_data.symtab = symtab;
20470 sig_data.types_list = &types_cu_list;
987d643c 20471 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20472 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20473 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20474 write_one_signatured_type, &sig_data);
20475 }
20476
156942c7
DE
20477 /* Now that we've processed all symbols we can shrink their cu_indices
20478 lists. */
20479 uniquify_cu_indices (symtab);
20480
9291a0cd
TT
20481 obstack_init (&constant_pool);
20482 make_cleanup_obstack_free (&constant_pool);
20483 obstack_init (&symtab_obstack);
20484 make_cleanup_obstack_free (&symtab_obstack);
20485 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20486
20487 obstack_init (&contents);
20488 make_cleanup_obstack_free (&contents);
1fd400ff 20489 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20490 total_len = size_of_contents;
20491
20492 /* The version number. */
156942c7 20493 val = MAYBE_SWAP (7);
9291a0cd
TT
20494 obstack_grow (&contents, &val, sizeof (val));
20495
20496 /* The offset of the CU list from the start of the file. */
20497 val = MAYBE_SWAP (total_len);
20498 obstack_grow (&contents, &val, sizeof (val));
20499 total_len += obstack_object_size (&cu_list);
20500
1fd400ff
TT
20501 /* The offset of the types CU list from the start of the file. */
20502 val = MAYBE_SWAP (total_len);
20503 obstack_grow (&contents, &val, sizeof (val));
20504 total_len += obstack_object_size (&types_cu_list);
20505
9291a0cd
TT
20506 /* The offset of the address table from the start of the file. */
20507 val = MAYBE_SWAP (total_len);
20508 obstack_grow (&contents, &val, sizeof (val));
20509 total_len += obstack_object_size (&addr_obstack);
20510
20511 /* The offset of the symbol table from the start of the file. */
20512 val = MAYBE_SWAP (total_len);
20513 obstack_grow (&contents, &val, sizeof (val));
20514 total_len += obstack_object_size (&symtab_obstack);
20515
20516 /* The offset of the constant pool from the start of the file. */
20517 val = MAYBE_SWAP (total_len);
20518 obstack_grow (&contents, &val, sizeof (val));
20519 total_len += obstack_object_size (&constant_pool);
20520
20521 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20522
20523 write_obstack (out_file, &contents);
20524 write_obstack (out_file, &cu_list);
1fd400ff 20525 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20526 write_obstack (out_file, &addr_obstack);
20527 write_obstack (out_file, &symtab_obstack);
20528 write_obstack (out_file, &constant_pool);
20529
20530 fclose (out_file);
20531
20532 /* We want to keep the file, so we set cleanup_filename to NULL
20533 here. See unlink_if_set. */
20534 cleanup_filename = NULL;
20535
20536 do_cleanups (cleanup);
20537}
20538
90476074
TT
20539/* Implementation of the `save gdb-index' command.
20540
20541 Note that the file format used by this command is documented in the
20542 GDB manual. Any changes here must be documented there. */
11570e71 20543
9291a0cd
TT
20544static void
20545save_gdb_index_command (char *arg, int from_tty)
20546{
20547 struct objfile *objfile;
20548
20549 if (!arg || !*arg)
96d19272 20550 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20551
20552 ALL_OBJFILES (objfile)
20553 {
20554 struct stat st;
20555
20556 /* If the objfile does not correspond to an actual file, skip it. */
20557 if (stat (objfile->name, &st) < 0)
20558 continue;
20559
20560 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20561 if (dwarf2_per_objfile)
20562 {
20563 volatile struct gdb_exception except;
20564
20565 TRY_CATCH (except, RETURN_MASK_ERROR)
20566 {
20567 write_psymtabs_to_index (objfile, arg);
20568 }
20569 if (except.reason < 0)
20570 exception_fprintf (gdb_stderr, except,
20571 _("Error while writing index for `%s': "),
20572 objfile->name);
20573 }
20574 }
dce234bc
PP
20575}
20576
9291a0cd
TT
20577\f
20578
9eae7c52
TT
20579int dwarf2_always_disassemble;
20580
20581static void
20582show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20583 struct cmd_list_element *c, const char *value)
20584{
3e43a32a
MS
20585 fprintf_filtered (file,
20586 _("Whether to always disassemble "
20587 "DWARF expressions is %s.\n"),
9eae7c52
TT
20588 value);
20589}
20590
900e11f9
JK
20591static void
20592show_check_physname (struct ui_file *file, int from_tty,
20593 struct cmd_list_element *c, const char *value)
20594{
20595 fprintf_filtered (file,
20596 _("Whether to check \"physname\" is %s.\n"),
20597 value);
20598}
20599
6502dd73
DJ
20600void _initialize_dwarf2_read (void);
20601
20602void
20603_initialize_dwarf2_read (void)
20604{
96d19272
JK
20605 struct cmd_list_element *c;
20606
dce234bc 20607 dwarf2_objfile_data_key
c1bd65d0 20608 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20609
1bedd215
AC
20610 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20611Set DWARF 2 specific variables.\n\
20612Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20613 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20614 0/*allow-unknown*/, &maintenance_set_cmdlist);
20615
1bedd215
AC
20616 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20617Show DWARF 2 specific variables\n\
20618Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20619 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20620 0/*allow-unknown*/, &maintenance_show_cmdlist);
20621
20622 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20623 &dwarf2_max_cache_age, _("\
20624Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20625Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20626A higher limit means that cached compilation units will be stored\n\
20627in memory longer, and more total memory will be used. Zero disables\n\
20628caching, which can slow down startup."),
2c5b56ce 20629 NULL,
920d2a44 20630 show_dwarf2_max_cache_age,
2c5b56ce 20631 &set_dwarf2_cmdlist,
ae038cb0 20632 &show_dwarf2_cmdlist);
d97bc12b 20633
9eae7c52
TT
20634 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20635 &dwarf2_always_disassemble, _("\
20636Set whether `info address' always disassembles DWARF expressions."), _("\
20637Show whether `info address' always disassembles DWARF expressions."), _("\
20638When enabled, DWARF expressions are always printed in an assembly-like\n\
20639syntax. When disabled, expressions will be printed in a more\n\
20640conversational style, when possible."),
20641 NULL,
20642 show_dwarf2_always_disassemble,
20643 &set_dwarf2_cmdlist,
20644 &show_dwarf2_cmdlist);
20645
45cfd468
DE
20646 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20647Set debugging of the dwarf2 reader."), _("\
20648Show debugging of the dwarf2 reader."), _("\
20649When enabled, debugging messages are printed during dwarf2 reading\n\
20650and symtab expansion."),
20651 NULL,
20652 NULL,
20653 &setdebuglist, &showdebuglist);
20654
ccce17b0 20655 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20656Set debugging of the dwarf2 DIE reader."), _("\
20657Show debugging of the dwarf2 DIE reader."), _("\
20658When enabled (non-zero), DIEs are dumped after they are read in.\n\
20659The value is the maximum depth to print."),
ccce17b0
YQ
20660 NULL,
20661 NULL,
20662 &setdebuglist, &showdebuglist);
9291a0cd 20663
900e11f9
JK
20664 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20665Set cross-checking of \"physname\" code against demangler."), _("\
20666Show cross-checking of \"physname\" code against demangler."), _("\
20667When enabled, GDB's internal \"physname\" code is checked against\n\
20668the demangler."),
20669 NULL, show_check_physname,
20670 &setdebuglist, &showdebuglist);
20671
e615022a
DE
20672 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20673 no_class, &use_deprecated_index_sections, _("\
20674Set whether to use deprecated gdb_index sections."), _("\
20675Show whether to use deprecated gdb_index sections."), _("\
20676When enabled, deprecated .gdb_index sections are used anyway.\n\
20677Normally they are ignored either because of a missing feature or\n\
20678performance issue.\n\
20679Warning: This option must be enabled before gdb reads the file."),
20680 NULL,
20681 NULL,
20682 &setlist, &showlist);
20683
96d19272 20684 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20685 _("\
fc1a9d6e 20686Save a gdb-index file.\n\
11570e71 20687Usage: save gdb-index DIRECTORY"),
96d19272
JK
20688 &save_cmdlist);
20689 set_cmd_completer (c, filename_completer);
6502dd73 20690}
This page took 6.403456 seconds and 4 git commands to generate.