Add support for Python 3.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
4c2df51b 70
c906108c
SS
71#include <fcntl.h>
72#include "gdb_string.h"
4bdf3d34 73#include "gdb_assert.h"
c906108c 74#include <sys/types.h>
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
45cfd468
DE
79/* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81static int dwarf2_read_debug = 0;
82
d97bc12b 83/* When non-zero, dump DIEs after they are read in. */
ccce17b0 84static unsigned int dwarf2_die_debug = 0;
d97bc12b 85
900e11f9
JK
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
481860b3 89/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 90static int use_deprecated_index_sections = 0;
481860b3 91
df8a16a1
DJ
92/* When set, the file that we're processing is known to have debugging
93 info for C++ namespaces. GCC 3.3.x did not produce this information,
94 but later versions do. */
95
96static int processing_has_namespace_info;
97
6502dd73
DJ
98static const struct objfile_data *dwarf2_objfile_data_key;
99
dce234bc
PP
100struct dwarf2_section_info
101{
102 asection *asection;
103 gdb_byte *buffer;
104 bfd_size_type size;
be391dca
TT
105 /* True if we have tried to read this section. */
106 int readin;
dce234bc
PP
107};
108
8b70b953
TT
109typedef struct dwarf2_section_info dwarf2_section_info_def;
110DEF_VEC_O (dwarf2_section_info_def);
111
9291a0cd
TT
112/* All offsets in the index are of this type. It must be
113 architecture-independent. */
114typedef uint32_t offset_type;
115
116DEF_VEC_I (offset_type);
117
156942c7
DE
118/* Ensure only legit values are used. */
119#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
120 do { \
121 gdb_assert ((unsigned int) (value) <= 1); \
122 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
123 } while (0)
124
125/* Ensure only legit values are used. */
126#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
127 do { \
128 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
129 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
130 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133/* Ensure we don't use more than the alloted nuber of bits for the CU. */
134#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
137 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
138 } while (0)
139
9291a0cd
TT
140/* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142struct mapped_index
143{
559a7a62
JK
144 /* Index data format version. */
145 int version;
146
9291a0cd
TT
147 /* The total length of the buffer. */
148 off_t total_size;
b11b1f88 149
9291a0cd
TT
150 /* A pointer to the address table data. */
151 const gdb_byte *address_table;
b11b1f88 152
9291a0cd
TT
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
b11b1f88 155
3876f04e
DE
156 /* The symbol table, implemented as a hash table. */
157 const offset_type *symbol_table;
b11b1f88 158
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
b11b1f88 161
9291a0cd
TT
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164};
165
95554aad
TT
166typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
167DEF_VEC_P (dwarf2_per_cu_ptr);
168
9cdd5dbd
DE
169/* Collection of data recorded per objfile.
170 This hangs off of dwarf2_objfile_data_key. */
171
6502dd73
DJ
172struct dwarf2_per_objfile
173{
dce234bc
PP
174 struct dwarf2_section_info info;
175 struct dwarf2_section_info abbrev;
176 struct dwarf2_section_info line;
dce234bc
PP
177 struct dwarf2_section_info loc;
178 struct dwarf2_section_info macinfo;
cf2c3c16 179 struct dwarf2_section_info macro;
dce234bc
PP
180 struct dwarf2_section_info str;
181 struct dwarf2_section_info ranges;
3019eac3 182 struct dwarf2_section_info addr;
dce234bc
PP
183 struct dwarf2_section_info frame;
184 struct dwarf2_section_info eh_frame;
9291a0cd 185 struct dwarf2_section_info gdb_index;
ae038cb0 186
8b70b953
TT
187 VEC (dwarf2_section_info_def) *types;
188
be391dca
TT
189 /* Back link. */
190 struct objfile *objfile;
191
d467dd73 192 /* Table of all the compilation units. This is used to locate
10b3939b 193 the target compilation unit of a particular reference. */
ae038cb0
DJ
194 struct dwarf2_per_cu_data **all_comp_units;
195
196 /* The number of compilation units in ALL_COMP_UNITS. */
197 int n_comp_units;
198
1fd400ff 199 /* The number of .debug_types-related CUs. */
d467dd73 200 int n_type_units;
1fd400ff 201
d467dd73 202 /* The .debug_types-related CUs (TUs). */
b4dd5633 203 struct signatured_type **all_type_units;
1fd400ff 204
f4dc4d17
DE
205 /* The number of entries in all_type_unit_groups. */
206 int n_type_unit_groups;
207
208 /* Table of type unit groups.
209 This exists to make it easy to iterate over all CUs and TU groups. */
210 struct type_unit_group **all_type_unit_groups;
211
212 /* Table of struct type_unit_group objects.
213 The hash key is the DW_AT_stmt_list value. */
214 htab_t type_unit_groups;
72dca2f5 215
348e048f
DE
216 /* A table mapping .debug_types signatures to its signatured_type entry.
217 This is NULL if the .debug_types section hasn't been read in yet. */
218 htab_t signatured_types;
219
f4dc4d17
DE
220 /* Type unit statistics, to see how well the scaling improvements
221 are doing. */
222 struct tu_stats
223 {
224 int nr_uniq_abbrev_tables;
225 int nr_symtabs;
226 int nr_symtab_sharers;
227 int nr_stmt_less_type_units;
228 } tu_stats;
229
230 /* A chain of compilation units that are currently read in, so that
231 they can be freed later. */
232 struct dwarf2_per_cu_data *read_in_chain;
233
3019eac3
DE
234 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
235 This is NULL if the table hasn't been allocated yet. */
236 htab_t dwo_files;
237
80626a55
DE
238 /* Non-zero if we've check for whether there is a DWP file. */
239 int dwp_checked;
240
241 /* The DWP file if there is one, or NULL. */
242 struct dwp_file *dwp_file;
243
36586728
TT
244 /* The shared '.dwz' file, if one exists. This is used when the
245 original data was compressed using 'dwz -m'. */
246 struct dwz_file *dwz_file;
247
72dca2f5
FR
248 /* A flag indicating wether this objfile has a section loaded at a
249 VMA of 0. */
250 int has_section_at_zero;
9291a0cd 251
ae2de4f8
DE
252 /* True if we are using the mapped index,
253 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
254 unsigned char using_index;
255
ae2de4f8 256 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 257 struct mapped_index *index_table;
98bfdba5 258
7b9f3c50 259 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
260 TUs typically share line table entries with a CU, so we maintain a
261 separate table of all line table entries to support the sharing.
262 Note that while there can be way more TUs than CUs, we've already
263 sorted all the TUs into "type unit groups", grouped by their
264 DW_AT_stmt_list value. Therefore the only sharing done here is with a
265 CU and its associated TU group if there is one. */
7b9f3c50
DE
266 htab_t quick_file_names_table;
267
98bfdba5
PA
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
673bfd45 271
dee91e82 272 /* Table mapping type DIEs to their struct type *.
673bfd45 273 This is NULL if not allocated yet.
dee91e82
DE
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
95554aad
TT
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
279};
280
281static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 282
251d32d9 283/* Default names of the debugging sections. */
c906108c 284
233a11ab
CS
285/* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
9cdd5dbd
DE
288static const struct dwarf2_debug_sections dwarf2_elf_names =
289{
251d32d9
TG
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 295 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
3019eac3 299 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
24d3216f
TT
302 { ".gdb_index", ".zgdb_index" },
303 23
251d32d9 304};
c906108c 305
80626a55 306/* List of DWO/DWP sections. */
3019eac3 307
80626a55 308static const struct dwop_section_names
3019eac3
DE
309{
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
09262596
DE
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
3019eac3
DE
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
80626a55
DE
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
3019eac3 321}
80626a55 322dwop_section_names =
3019eac3
DE
323{
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
335};
336
c906108c
SS
337/* local data types */
338
107d2387
AC
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
c906108c 341struct comp_unit_head
a738430d 342{
c764a876 343 unsigned int length;
a738430d 344 short version;
a738430d
MK
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
b64f50a1 347 sect_offset abbrev_offset;
57349743 348
a738430d
MK
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
57349743 351
a738430d
MK
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
57349743 354
a738430d
MK
355 /* Offset to the first byte of this compilation unit header in the
356 .debug_info section, for resolving relative reference dies. */
b64f50a1 357 sect_offset offset;
57349743 358
d00adf39
DE
359 /* Offset to first die in this cu from the start of the cu.
360 This will be the first byte following the compilation unit header. */
b64f50a1 361 cu_offset first_die_offset;
a738430d 362};
c906108c 363
3da10d80
KS
364/* Type used for delaying computation of method physnames.
365 See comments for compute_delayed_physnames. */
366struct delayed_method_info
367{
368 /* The type to which the method is attached, i.e., its parent class. */
369 struct type *type;
370
371 /* The index of the method in the type's function fieldlists. */
372 int fnfield_index;
373
374 /* The index of the method in the fieldlist. */
375 int index;
376
377 /* The name of the DIE. */
378 const char *name;
379
380 /* The DIE associated with this method. */
381 struct die_info *die;
382};
383
384typedef struct delayed_method_info delayed_method_info;
385DEF_VEC_O (delayed_method_info);
386
e7c27a73
DJ
387/* Internal state when decoding a particular compilation unit. */
388struct dwarf2_cu
389{
390 /* The objfile containing this compilation unit. */
391 struct objfile *objfile;
392
d00adf39 393 /* The header of the compilation unit. */
e7c27a73 394 struct comp_unit_head header;
e142c38c 395
d00adf39
DE
396 /* Base address of this compilation unit. */
397 CORE_ADDR base_address;
398
399 /* Non-zero if base_address has been set. */
400 int base_known;
401
e142c38c
DJ
402 /* The language we are debugging. */
403 enum language language;
404 const struct language_defn *language_defn;
405
b0f35d58
DL
406 const char *producer;
407
e142c38c
DJ
408 /* The generic symbol table building routines have separate lists for
409 file scope symbols and all all other scopes (local scopes). So
410 we need to select the right one to pass to add_symbol_to_list().
411 We do it by keeping a pointer to the correct list in list_in_scope.
412
413 FIXME: The original dwarf code just treated the file scope as the
414 first local scope, and all other local scopes as nested local
415 scopes, and worked fine. Check to see if we really need to
416 distinguish these in buildsym.c. */
417 struct pending **list_in_scope;
418
433df2d4
DE
419 /* The abbrev table for this CU.
420 Normally this points to the abbrev table in the objfile.
421 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
422 struct abbrev_table *abbrev_table;
72bf9492 423
b64f50a1
JK
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
426 htab_t partial_dies;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 struct obstack comp_unit_obstack;
431
ae038cb0
DJ
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain;
437
438 /* Backchain to our per_cu entry if the tree has been built. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used;
443
b64f50a1
JK
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
51545339 446 htab_t die_hash;
10b3939b
DJ
447
448 /* Full DIEs if read in. */
449 struct die_info *dies;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies;
455
cb1df416
DJ
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header;
458
3da10d80
KS
459 /* A list of methods which need to have physnames computed
460 after all type information has been read. */
461 VEC (delayed_method_info) *method_list;
462
96408a79
SA
463 /* To be copied to symtab->call_site_htab. */
464 htab_t call_site_htab;
465
034e5797
DE
466 /* Non-NULL if this CU came from a DWO file.
467 There is an invariant here that is important to remember:
468 Except for attributes copied from the top level DIE in the "main"
469 (or "stub") file in preparation for reading the DWO file
470 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
471 Either there isn't a DWO file (in which case this is NULL and the point
472 is moot), or there is and either we're not going to read it (in which
473 case this is NULL) or there is and we are reading it (in which case this
474 is non-NULL). */
3019eac3
DE
475 struct dwo_unit *dwo_unit;
476
477 /* The DW_AT_addr_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE. */
480 ULONGEST addr_base;
481
2e3cf129
DE
482 /* The DW_AT_ranges_base attribute if present, zero otherwise
483 (zero is a valid value though).
484 Note this value comes from the stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
486 be used without needing to know whether DWO files are in use or not.
487 N.B. This does not apply to DW_AT_ranges appearing in
488 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
489 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
490 DW_AT_ranges_base *would* have to be applied, and we'd have to care
491 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
492 ULONGEST ranges_base;
493
ae038cb0
DJ
494 /* Mark used when releasing cached dies. */
495 unsigned int mark : 1;
496
8be455d7
JK
497 /* This CU references .debug_loc. See the symtab->locations_valid field.
498 This test is imperfect as there may exist optimized debug code not using
499 any location list and still facing inlining issues if handled as
500 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 501 unsigned int has_loclist : 1;
ba919b58 502
1b80a9fa
JK
503 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
504 if all the producer_is_* fields are valid. This information is cached
505 because profiling CU expansion showed excessive time spent in
506 producer_is_gxx_lt_4_6. */
ba919b58
TT
507 unsigned int checked_producer : 1;
508 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 509 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 510 unsigned int producer_is_icc : 1;
e7c27a73
DJ
511};
512
10b3939b
DJ
513/* Persistent data held for a compilation unit, even when not
514 processing it. We put a pointer to this structure in the
28dee7f5 515 read_symtab_private field of the psymtab. */
10b3939b 516
ae038cb0
DJ
517struct dwarf2_per_cu_data
518{
36586728 519 /* The start offset and length of this compilation unit.
45452591 520 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
521 initial_length_size.
522 If the DIE refers to a DWO file, this is always of the original die,
523 not the DWO file. */
b64f50a1 524 sect_offset offset;
36586728 525 unsigned int length;
ae038cb0
DJ
526
527 /* Flag indicating this compilation unit will be read in before
528 any of the current compilation units are processed. */
c764a876 529 unsigned int queued : 1;
ae038cb0 530
0d99eb77
DE
531 /* This flag will be set when reading partial DIEs if we need to load
532 absolutely all DIEs for this compilation unit, instead of just the ones
533 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
534 hash table and don't find it. */
535 unsigned int load_all_dies : 1;
536
3019eac3
DE
537 /* Non-zero if this CU is from .debug_types. */
538 unsigned int is_debug_types : 1;
539
36586728
TT
540 /* Non-zero if this CU is from the .dwz file. */
541 unsigned int is_dwz : 1;
542
3019eac3
DE
543 /* The section this CU/TU lives in.
544 If the DIE refers to a DWO file, this is always the original die,
545 not the DWO file. */
546 struct dwarf2_section_info *info_or_types_section;
348e048f 547
17ea53c3
JK
548 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
549 of the CU cache it gets reset to NULL again. */
ae038cb0 550 struct dwarf2_cu *cu;
1c379e20 551
9cdd5dbd
DE
552 /* The corresponding objfile.
553 Normally we can get the objfile from dwarf2_per_objfile.
554 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
555 struct objfile *objfile;
556
557 /* When using partial symbol tables, the 'psymtab' field is active.
558 Otherwise the 'quick' field is active. */
559 union
560 {
561 /* The partial symbol table associated with this compilation unit,
95554aad 562 or NULL for unread partial units. */
9291a0cd
TT
563 struct partial_symtab *psymtab;
564
565 /* Data needed by the "quick" functions. */
566 struct dwarf2_per_cu_quick_data *quick;
567 } v;
95554aad 568
f4dc4d17
DE
569 union
570 {
571 /* The CUs we import using DW_TAG_imported_unit. This is filled in
572 while reading psymtabs, used to compute the psymtab dependencies,
573 and then cleared. Then it is filled in again while reading full
574 symbols, and only deleted when the objfile is destroyed. */
575 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
576
577 /* Type units are grouped by their DW_AT_stmt_list entry so that they
578 can share them. If this is a TU, this points to the containing
579 symtab. */
580 struct type_unit_group *type_unit_group;
581 } s;
ae038cb0
DJ
582};
583
348e048f
DE
584/* Entry in the signatured_types hash table. */
585
586struct signatured_type
587{
42e7ad6c
DE
588 /* The "per_cu" object of this type.
589 N.B.: This is the first member so that it's easy to convert pointers
590 between them. */
591 struct dwarf2_per_cu_data per_cu;
592
3019eac3 593 /* The type's signature. */
348e048f
DE
594 ULONGEST signature;
595
3019eac3
DE
596 /* Offset in the TU of the type's DIE, as read from the TU header.
597 If the definition lives in a DWO file, this value is unusable. */
598 cu_offset type_offset_in_tu;
599
600 /* Offset in the section of the type's DIE.
601 If the definition lives in a DWO file, this is the offset in the
602 .debug_types.dwo section.
603 The value is zero until the actual value is known.
604 Zero is otherwise not a valid section offset. */
605 sect_offset type_offset_in_section;
348e048f
DE
606};
607
094b34ac
DE
608/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
609 This includes type_unit_group and quick_file_names. */
610
611struct stmt_list_hash
612{
613 /* The DWO unit this table is from or NULL if there is none. */
614 struct dwo_unit *dwo_unit;
615
616 /* Offset in .debug_line or .debug_line.dwo. */
617 sect_offset line_offset;
618};
619
f4dc4d17
DE
620/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
621 an object of this type. */
622
623struct type_unit_group
624{
625 /* dwarf2read.c's main "handle" on the symtab.
626 To simplify things we create an artificial CU that "includes" all the
627 type units using this stmt_list so that the rest of the code still has
628 a "per_cu" handle on the symtab.
629 This PER_CU is recognized by having no section. */
630#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
631 struct dwarf2_per_cu_data per_cu;
632
633 union
634 {
635 /* The TUs that share this DW_AT_stmt_list entry.
636 This is added to while parsing type units to build partial symtabs,
637 and is deleted afterwards and not used again. */
638 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 639
094b34ac
DE
640 /* When reading the line table in "quick" functions, we need a real TU.
641 Any will do, we know they all share the same DW_AT_stmt_list entry.
642 For simplicity's sake, we pick the first one. */
643 struct dwarf2_per_cu_data *first_tu;
644 } t;
f4dc4d17
DE
645
646 /* The primary symtab.
094b34ac
DE
647 Type units in a group needn't all be defined in the same source file,
648 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
649 struct symtab *primary_symtab;
650
094b34ac
DE
651 /* The data used to construct the hash key. */
652 struct stmt_list_hash hash;
f4dc4d17
DE
653
654 /* The number of symtabs from the line header.
655 The value here must match line_header.num_file_names. */
656 unsigned int num_symtabs;
657
658 /* The symbol tables for this TU (obtained from the files listed in
659 DW_AT_stmt_list).
660 WARNING: The order of entries here must match the order of entries
661 in the line header. After the first TU using this type_unit_group, the
662 line header for the subsequent TUs is recreated from this. This is done
663 because we need to use the same symtabs for each TU using the same
664 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
665 there's no guarantee the line header doesn't have duplicate entries. */
666 struct symtab **symtabs;
667};
668
80626a55 669/* These sections are what may appear in a DWO file. */
3019eac3
DE
670
671struct dwo_sections
672{
673 struct dwarf2_section_info abbrev;
3019eac3
DE
674 struct dwarf2_section_info line;
675 struct dwarf2_section_info loc;
09262596
DE
676 struct dwarf2_section_info macinfo;
677 struct dwarf2_section_info macro;
3019eac3
DE
678 struct dwarf2_section_info str;
679 struct dwarf2_section_info str_offsets;
80626a55
DE
680 /* In the case of a virtual DWO file, these two are unused. */
681 struct dwarf2_section_info info;
3019eac3
DE
682 VEC (dwarf2_section_info_def) *types;
683};
684
685/* Common bits of DWO CUs/TUs. */
686
687struct dwo_unit
688{
689 /* Backlink to the containing struct dwo_file. */
690 struct dwo_file *dwo_file;
691
692 /* The "id" that distinguishes this CU/TU.
693 .debug_info calls this "dwo_id", .debug_types calls this "signature".
694 Since signatures came first, we stick with it for consistency. */
695 ULONGEST signature;
696
697 /* The section this CU/TU lives in, in the DWO file. */
698 struct dwarf2_section_info *info_or_types_section;
699
700 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
701 sect_offset offset;
702 unsigned int length;
703
704 /* For types, offset in the type's DIE of the type defined by this TU. */
705 cu_offset type_offset_in_tu;
706};
707
80626a55
DE
708/* Data for one DWO file.
709 This includes virtual DWO files that have been packaged into a
710 DWP file. */
3019eac3
DE
711
712struct dwo_file
713{
80626a55
DE
714 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
715 For virtual DWO files the name is constructed from the section offsets
716 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
717 from related CU+TUs. */
718 const char *name;
3019eac3 719
80626a55
DE
720 /* The bfd, when the file is open. Otherwise this is NULL.
721 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
722 bfd *dbfd;
3019eac3
DE
723
724 /* Section info for this file. */
725 struct dwo_sections sections;
726
727 /* Table of CUs in the file.
728 Each element is a struct dwo_unit. */
729 htab_t cus;
730
731 /* Table of TUs in the file.
732 Each element is a struct dwo_unit. */
733 htab_t tus;
734};
735
80626a55
DE
736/* These sections are what may appear in a DWP file. */
737
738struct dwp_sections
739{
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
744 by section number. We don't need to record them here. */
745};
746
747/* These sections are what may appear in a virtual DWO file. */
748
749struct virtual_dwo_sections
750{
751 struct dwarf2_section_info abbrev;
752 struct dwarf2_section_info line;
753 struct dwarf2_section_info loc;
754 struct dwarf2_section_info macinfo;
755 struct dwarf2_section_info macro;
756 struct dwarf2_section_info str_offsets;
757 /* Each DWP hash table entry records one CU or one TU.
758 That is recorded here, and copied to dwo_unit.info_or_types_section. */
759 struct dwarf2_section_info info_or_types;
760};
761
762/* Contents of DWP hash tables. */
763
764struct dwp_hash_table
765{
766 uint32_t nr_units, nr_slots;
767 const gdb_byte *hash_table, *unit_table, *section_pool;
768};
769
770/* Data for one DWP file. */
771
772struct dwp_file
773{
774 /* Name of the file. */
775 const char *name;
776
777 /* The bfd, when the file is open. Otherwise this is NULL. */
778 bfd *dbfd;
779
780 /* Section info for this file. */
781 struct dwp_sections sections;
782
783 /* Table of CUs in the file. */
784 const struct dwp_hash_table *cus;
785
786 /* Table of TUs in the file. */
787 const struct dwp_hash_table *tus;
788
789 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
790 htab_t loaded_cutus;
791
792 /* Table to map ELF section numbers to their sections. */
793 unsigned int num_sections;
794 asection **elf_sections;
795};
796
36586728
TT
797/* This represents a '.dwz' file. */
798
799struct dwz_file
800{
801 /* A dwz file can only contain a few sections. */
802 struct dwarf2_section_info abbrev;
803 struct dwarf2_section_info info;
804 struct dwarf2_section_info str;
805 struct dwarf2_section_info line;
806 struct dwarf2_section_info macro;
2ec9a5e0 807 struct dwarf2_section_info gdb_index;
36586728
TT
808
809 /* The dwz's BFD. */
810 bfd *dwz_bfd;
811};
812
0963b4bd
MS
813/* Struct used to pass misc. parameters to read_die_and_children, et
814 al. which are used for both .debug_info and .debug_types dies.
815 All parameters here are unchanging for the life of the call. This
dee91e82 816 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
817
818struct die_reader_specs
819{
dee91e82 820 /* die_section->asection->owner. */
93311388
DE
821 bfd* abfd;
822
823 /* The CU of the DIE we are parsing. */
824 struct dwarf2_cu *cu;
825
80626a55 826 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
827 struct dwo_file *dwo_file;
828
dee91e82 829 /* The section the die comes from.
3019eac3 830 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
831 struct dwarf2_section_info *die_section;
832
833 /* die_section->buffer. */
834 gdb_byte *buffer;
f664829e
DE
835
836 /* The end of the buffer. */
837 const gdb_byte *buffer_end;
93311388
DE
838};
839
fd820528 840/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
841typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
842 gdb_byte *info_ptr,
843 struct die_info *comp_unit_die,
844 int has_children,
845 void *data);
846
debd256d
JB
847/* The line number information for a compilation unit (found in the
848 .debug_line section) begins with a "statement program header",
849 which contains the following information. */
850struct line_header
851{
852 unsigned int total_length;
853 unsigned short version;
854 unsigned int header_length;
855 unsigned char minimum_instruction_length;
2dc7f7b3 856 unsigned char maximum_ops_per_instruction;
debd256d
JB
857 unsigned char default_is_stmt;
858 int line_base;
859 unsigned char line_range;
860 unsigned char opcode_base;
861
862 /* standard_opcode_lengths[i] is the number of operands for the
863 standard opcode whose value is i. This means that
864 standard_opcode_lengths[0] is unused, and the last meaningful
865 element is standard_opcode_lengths[opcode_base - 1]. */
866 unsigned char *standard_opcode_lengths;
867
868 /* The include_directories table. NOTE! These strings are not
869 allocated with xmalloc; instead, they are pointers into
870 debug_line_buffer. If you try to free them, `free' will get
871 indigestion. */
872 unsigned int num_include_dirs, include_dirs_size;
873 char **include_dirs;
874
875 /* The file_names table. NOTE! These strings are not allocated
876 with xmalloc; instead, they are pointers into debug_line_buffer.
877 Don't try to free them directly. */
878 unsigned int num_file_names, file_names_size;
879 struct file_entry
c906108c 880 {
debd256d
JB
881 char *name;
882 unsigned int dir_index;
883 unsigned int mod_time;
884 unsigned int length;
aaa75496 885 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 886 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
887 } *file_names;
888
889 /* The start and end of the statement program following this
6502dd73 890 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 891 gdb_byte *statement_program_start, *statement_program_end;
debd256d 892};
c906108c
SS
893
894/* When we construct a partial symbol table entry we only
0963b4bd 895 need this much information. */
c906108c
SS
896struct partial_die_info
897 {
72bf9492 898 /* Offset of this DIE. */
b64f50a1 899 sect_offset offset;
72bf9492
DJ
900
901 /* DWARF-2 tag for this DIE. */
902 ENUM_BITFIELD(dwarf_tag) tag : 16;
903
72bf9492
DJ
904 /* Assorted flags describing the data found in this DIE. */
905 unsigned int has_children : 1;
906 unsigned int is_external : 1;
907 unsigned int is_declaration : 1;
908 unsigned int has_type : 1;
909 unsigned int has_specification : 1;
910 unsigned int has_pc_info : 1;
481860b3 911 unsigned int may_be_inlined : 1;
72bf9492
DJ
912
913 /* Flag set if the SCOPE field of this structure has been
914 computed. */
915 unsigned int scope_set : 1;
916
fa4028e9
JB
917 /* Flag set if the DIE has a byte_size attribute. */
918 unsigned int has_byte_size : 1;
919
98bfdba5
PA
920 /* Flag set if any of the DIE's children are template arguments. */
921 unsigned int has_template_arguments : 1;
922
abc72ce4
DE
923 /* Flag set if fixup_partial_die has been called on this die. */
924 unsigned int fixup_called : 1;
925
36586728
TT
926 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
927 unsigned int is_dwz : 1;
928
929 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
930 unsigned int spec_is_dwz : 1;
931
72bf9492 932 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 933 sometimes a default name for unnamed DIEs. */
c906108c 934 char *name;
72bf9492 935
abc72ce4
DE
936 /* The linkage name, if present. */
937 const char *linkage_name;
938
72bf9492
DJ
939 /* The scope to prepend to our children. This is generally
940 allocated on the comp_unit_obstack, so will disappear
941 when this compilation unit leaves the cache. */
942 char *scope;
943
95554aad
TT
944 /* Some data associated with the partial DIE. The tag determines
945 which field is live. */
946 union
947 {
948 /* The location description associated with this DIE, if any. */
949 struct dwarf_block *locdesc;
950 /* The offset of an import, for DW_TAG_imported_unit. */
951 sect_offset offset;
952 } d;
72bf9492
DJ
953
954 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
955 CORE_ADDR lowpc;
956 CORE_ADDR highpc;
72bf9492 957
93311388 958 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 959 DW_AT_sibling, if any. */
abc72ce4
DE
960 /* NOTE: This member isn't strictly necessary, read_partial_die could
961 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 962 gdb_byte *sibling;
72bf9492
DJ
963
964 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
965 DW_AT_specification (or DW_AT_abstract_origin or
966 DW_AT_extension). */
b64f50a1 967 sect_offset spec_offset;
72bf9492
DJ
968
969 /* Pointers to this DIE's parent, first child, and next sibling,
970 if any. */
971 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
972 };
973
0963b4bd 974/* This data structure holds the information of an abbrev. */
c906108c
SS
975struct abbrev_info
976 {
977 unsigned int number; /* number identifying abbrev */
978 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
979 unsigned short has_children; /* boolean */
980 unsigned short num_attrs; /* number of attributes */
c906108c
SS
981 struct attr_abbrev *attrs; /* an array of attribute descriptions */
982 struct abbrev_info *next; /* next in chain */
983 };
984
985struct attr_abbrev
986 {
9d25dd43
DE
987 ENUM_BITFIELD(dwarf_attribute) name : 16;
988 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
989 };
990
433df2d4
DE
991/* Size of abbrev_table.abbrev_hash_table. */
992#define ABBREV_HASH_SIZE 121
993
994/* Top level data structure to contain an abbreviation table. */
995
996struct abbrev_table
997{
f4dc4d17
DE
998 /* Where the abbrev table came from.
999 This is used as a sanity check when the table is used. */
433df2d4
DE
1000 sect_offset offset;
1001
1002 /* Storage for the abbrev table. */
1003 struct obstack abbrev_obstack;
1004
1005 /* Hash table of abbrevs.
1006 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1007 It could be statically allocated, but the previous code didn't so we
1008 don't either. */
1009 struct abbrev_info **abbrevs;
1010};
1011
0963b4bd 1012/* Attributes have a name and a value. */
b60c80d6
DJ
1013struct attribute
1014 {
9d25dd43 1015 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1016 ENUM_BITFIELD(dwarf_form) form : 15;
1017
1018 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1019 field should be in u.str (existing only for DW_STRING) but it is kept
1020 here for better struct attribute alignment. */
1021 unsigned int string_is_canonical : 1;
1022
b60c80d6
DJ
1023 union
1024 {
1025 char *str;
1026 struct dwarf_block *blk;
43bbcdc2
PH
1027 ULONGEST unsnd;
1028 LONGEST snd;
b60c80d6 1029 CORE_ADDR addr;
348e048f 1030 struct signatured_type *signatured_type;
b60c80d6
DJ
1031 }
1032 u;
1033 };
1034
0963b4bd 1035/* This data structure holds a complete die structure. */
c906108c
SS
1036struct die_info
1037 {
76815b17
DE
1038 /* DWARF-2 tag for this DIE. */
1039 ENUM_BITFIELD(dwarf_tag) tag : 16;
1040
1041 /* Number of attributes */
98bfdba5
PA
1042 unsigned char num_attrs;
1043
1044 /* True if we're presently building the full type name for the
1045 type derived from this DIE. */
1046 unsigned char building_fullname : 1;
76815b17
DE
1047
1048 /* Abbrev number */
1049 unsigned int abbrev;
1050
93311388 1051 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1052 sect_offset offset;
78ba4af6
JB
1053
1054 /* The dies in a compilation unit form an n-ary tree. PARENT
1055 points to this die's parent; CHILD points to the first child of
1056 this node; and all the children of a given node are chained
4950bc1c 1057 together via their SIBLING fields. */
639d11d3
DC
1058 struct die_info *child; /* Its first child, if any. */
1059 struct die_info *sibling; /* Its next sibling, if any. */
1060 struct die_info *parent; /* Its parent, if any. */
c906108c 1061
b60c80d6
DJ
1062 /* An array of attributes, with NUM_ATTRS elements. There may be
1063 zero, but it's not common and zero-sized arrays are not
1064 sufficiently portable C. */
1065 struct attribute attrs[1];
c906108c
SS
1066 };
1067
0963b4bd 1068/* Get at parts of an attribute structure. */
c906108c
SS
1069
1070#define DW_STRING(attr) ((attr)->u.str)
8285870a 1071#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1072#define DW_UNSND(attr) ((attr)->u.unsnd)
1073#define DW_BLOCK(attr) ((attr)->u.blk)
1074#define DW_SND(attr) ((attr)->u.snd)
1075#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1076#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1077
0963b4bd 1078/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1079struct dwarf_block
1080 {
56eb65bd 1081 size_t size;
1d6edc3c
JK
1082
1083 /* Valid only if SIZE is not zero. */
fe1b8b76 1084 gdb_byte *data;
c906108c
SS
1085 };
1086
c906108c
SS
1087#ifndef ATTR_ALLOC_CHUNK
1088#define ATTR_ALLOC_CHUNK 4
1089#endif
1090
c906108c
SS
1091/* Allocate fields for structs, unions and enums in this size. */
1092#ifndef DW_FIELD_ALLOC_CHUNK
1093#define DW_FIELD_ALLOC_CHUNK 4
1094#endif
1095
c906108c
SS
1096/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099static int bits_per_byte = 8;
1100
1101/* The routines that read and process dies for a C struct or C++ class
1102 pass lists of data member fields and lists of member function fields
1103 in an instance of a field_info structure, as defined below. */
1104struct field_info
c5aa993b 1105 {
0963b4bd 1106 /* List of data member and baseclasses fields. */
c5aa993b
JM
1107 struct nextfield
1108 {
1109 struct nextfield *next;
1110 int accessibility;
1111 int virtuality;
1112 struct field field;
1113 }
7d0ccb61 1114 *fields, *baseclasses;
c906108c 1115
7d0ccb61 1116 /* Number of fields (including baseclasses). */
c5aa993b 1117 int nfields;
c906108c 1118
c5aa993b
JM
1119 /* Number of baseclasses. */
1120 int nbaseclasses;
c906108c 1121
c5aa993b
JM
1122 /* Set if the accesibility of one of the fields is not public. */
1123 int non_public_fields;
c906108c 1124
c5aa993b
JM
1125 /* Member function fields array, entries are allocated in the order they
1126 are encountered in the object file. */
1127 struct nextfnfield
1128 {
1129 struct nextfnfield *next;
1130 struct fn_field fnfield;
1131 }
1132 *fnfields;
c906108c 1133
c5aa993b
JM
1134 /* Member function fieldlist array, contains name of possibly overloaded
1135 member function, number of overloaded member functions and a pointer
1136 to the head of the member function field chain. */
1137 struct fnfieldlist
1138 {
1139 char *name;
1140 int length;
1141 struct nextfnfield *head;
1142 }
1143 *fnfieldlists;
c906108c 1144
c5aa993b
JM
1145 /* Number of entries in the fnfieldlists array. */
1146 int nfnfields;
98751a41
JK
1147
1148 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1149 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1150 struct typedef_field_list
1151 {
1152 struct typedef_field field;
1153 struct typedef_field_list *next;
1154 }
1155 *typedef_field_list;
1156 unsigned typedef_field_list_count;
c5aa993b 1157 };
c906108c 1158
10b3939b
DJ
1159/* One item on the queue of compilation units to read in full symbols
1160 for. */
1161struct dwarf2_queue_item
1162{
1163 struct dwarf2_per_cu_data *per_cu;
95554aad 1164 enum language pretend_language;
10b3939b
DJ
1165 struct dwarf2_queue_item *next;
1166};
1167
1168/* The current queue. */
1169static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1170
ae038cb0
DJ
1171/* Loaded secondary compilation units are kept in memory until they
1172 have not been referenced for the processing of this many
1173 compilation units. Set this to zero to disable caching. Cache
1174 sizes of up to at least twenty will improve startup time for
1175 typical inter-CU-reference binaries, at an obvious memory cost. */
1176static int dwarf2_max_cache_age = 5;
920d2a44
AC
1177static void
1178show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1179 struct cmd_list_element *c, const char *value)
1180{
3e43a32a
MS
1181 fprintf_filtered (file, _("The upper bound on the age of cached "
1182 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1183 value);
1184}
1185
ae038cb0 1186
0963b4bd 1187/* Various complaints about symbol reading that don't abort the process. */
c906108c 1188
4d3c2250
KB
1189static void
1190dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1191{
4d3c2250 1192 complaint (&symfile_complaints,
e2e0b3e5 1193 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1194}
1195
25e43795
DJ
1196static void
1197dwarf2_debug_line_missing_file_complaint (void)
1198{
1199 complaint (&symfile_complaints,
1200 _(".debug_line section has line data without a file"));
1201}
1202
59205f5a
JB
1203static void
1204dwarf2_debug_line_missing_end_sequence_complaint (void)
1205{
1206 complaint (&symfile_complaints,
3e43a32a
MS
1207 _(".debug_line section has line "
1208 "program sequence without an end"));
59205f5a
JB
1209}
1210
4d3c2250
KB
1211static void
1212dwarf2_complex_location_expr_complaint (void)
2e276125 1213{
e2e0b3e5 1214 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1215}
1216
4d3c2250
KB
1217static void
1218dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1219 int arg3)
2e276125 1220{
4d3c2250 1221 complaint (&symfile_complaints,
3e43a32a
MS
1222 _("const value length mismatch for '%s', got %d, expected %d"),
1223 arg1, arg2, arg3);
4d3c2250
KB
1224}
1225
1226static void
f664829e 1227dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1228{
4d3c2250 1229 complaint (&symfile_complaints,
f664829e
DE
1230 _("debug info runs off end of %s section"
1231 " [in module %s]"),
1232 section->asection->name,
1233 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1234}
1235
1236static void
1237dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1238{
4d3c2250 1239 complaint (&symfile_complaints,
3e43a32a
MS
1240 _("macro debug info contains a "
1241 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1242 arg1);
1243}
1244
1245static void
1246dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1247{
4d3c2250 1248 complaint (&symfile_complaints,
3e43a32a
MS
1249 _("invalid attribute class or form for '%s' in '%s'"),
1250 arg1, arg2);
4d3c2250 1251}
c906108c 1252
c906108c
SS
1253/* local function prototypes */
1254
4efb68b1 1255static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1256
aaa75496
JB
1257static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1258 struct objfile *);
1259
918dd910
JK
1260static void dwarf2_find_base_address (struct die_info *die,
1261 struct dwarf2_cu *cu);
1262
c67a9c90 1263static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1264
72bf9492
DJ
1265static void scan_partial_symbols (struct partial_die_info *,
1266 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1267 int, struct dwarf2_cu *);
c906108c 1268
72bf9492
DJ
1269static void add_partial_symbol (struct partial_die_info *,
1270 struct dwarf2_cu *);
63d06c5c 1271
72bf9492
DJ
1272static void add_partial_namespace (struct partial_die_info *pdi,
1273 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1274 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1275
5d7cb8df
JK
1276static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1277 CORE_ADDR *highpc, int need_pc,
1278 struct dwarf2_cu *cu);
1279
72bf9492
DJ
1280static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1281 struct dwarf2_cu *cu);
91c24f0a 1282
bc30ff58
JB
1283static void add_partial_subprogram (struct partial_die_info *pdi,
1284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1285 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1286
a14ed312 1287static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1288
a14ed312 1289static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1290
433df2d4
DE
1291static struct abbrev_info *abbrev_table_lookup_abbrev
1292 (const struct abbrev_table *, unsigned int);
1293
1294static struct abbrev_table *abbrev_table_read_table
1295 (struct dwarf2_section_info *, sect_offset);
1296
1297static void abbrev_table_free (struct abbrev_table *);
1298
f4dc4d17
DE
1299static void abbrev_table_free_cleanup (void *);
1300
dee91e82
DE
1301static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1302 struct dwarf2_section_info *);
c906108c 1303
f3dd6933 1304static void dwarf2_free_abbrev_table (void *);
c906108c 1305
6caca83c
CC
1306static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1307
dee91e82
DE
1308static struct partial_die_info *load_partial_dies
1309 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1310
dee91e82
DE
1311static gdb_byte *read_partial_die (const struct die_reader_specs *,
1312 struct partial_die_info *,
1313 struct abbrev_info *,
1314 unsigned int,
1315 gdb_byte *);
c906108c 1316
36586728 1317static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1318 struct dwarf2_cu *);
72bf9492
DJ
1319
1320static void fixup_partial_die (struct partial_die_info *,
1321 struct dwarf2_cu *);
1322
dee91e82
DE
1323static gdb_byte *read_attribute (const struct die_reader_specs *,
1324 struct attribute *, struct attr_abbrev *,
1325 gdb_byte *);
a8329558 1326
a1855c1d 1327static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1328
a1855c1d 1329static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1330
a1855c1d 1331static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1332
a1855c1d 1333static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1334
a1855c1d 1335static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1336
fe1b8b76 1337static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1338 unsigned int *);
c906108c 1339
c764a876
DE
1340static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1341
1342static LONGEST read_checked_initial_length_and_offset
1343 (bfd *, gdb_byte *, const struct comp_unit_head *,
1344 unsigned int *, unsigned int *);
613e1657 1345
fe1b8b76 1346static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1347 unsigned int *);
1348
1349static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1350
f4dc4d17
DE
1351static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1352 sect_offset);
1353
fe1b8b76 1354static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1355
9b1c24c8 1356static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1357
fe1b8b76
JB
1358static char *read_indirect_string (bfd *, gdb_byte *,
1359 const struct comp_unit_head *,
1360 unsigned int *);
4bdf3d34 1361
36586728
TT
1362static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1363
12df843f 1364static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1365
12df843f 1366static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1367
3019eac3
DE
1368static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1369 unsigned int *);
1370
1371static char *read_str_index (const struct die_reader_specs *reader,
1372 struct dwarf2_cu *cu, ULONGEST str_index);
1373
e142c38c 1374static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1375
e142c38c
DJ
1376static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1377 struct dwarf2_cu *);
c906108c 1378
348e048f 1379static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1380 unsigned int);
348e048f 1381
05cf31d1
JB
1382static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1383 struct dwarf2_cu *cu);
1384
e142c38c 1385static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1386
e142c38c 1387static struct die_info *die_specification (struct die_info *die,
f2f0e013 1388 struct dwarf2_cu **);
63d06c5c 1389
debd256d
JB
1390static void free_line_header (struct line_header *lh);
1391
aaa75496
JB
1392static void add_file_name (struct line_header *, char *, unsigned int,
1393 unsigned int, unsigned int);
1394
3019eac3
DE
1395static struct line_header *dwarf_decode_line_header (unsigned int offset,
1396 struct dwarf2_cu *cu);
debd256d 1397
f3f5162e
DE
1398static void dwarf_decode_lines (struct line_header *, const char *,
1399 struct dwarf2_cu *, struct partial_symtab *,
1400 int);
c906108c 1401
72b9f47f 1402static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1403
f4dc4d17
DE
1404static void dwarf2_start_symtab (struct dwarf2_cu *,
1405 char *, char *, CORE_ADDR);
1406
a14ed312 1407static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1408 struct dwarf2_cu *);
c906108c 1409
34eaf542
TT
1410static struct symbol *new_symbol_full (struct die_info *, struct type *,
1411 struct dwarf2_cu *, struct symbol *);
1412
a14ed312 1413static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1414 struct dwarf2_cu *);
c906108c 1415
98bfdba5
PA
1416static void dwarf2_const_value_attr (struct attribute *attr,
1417 struct type *type,
1418 const char *name,
1419 struct obstack *obstack,
12df843f 1420 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1421 gdb_byte **bytes,
1422 struct dwarf2_locexpr_baton **baton);
2df3850c 1423
e7c27a73 1424static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1425
b4ba55a1
JB
1426static int need_gnat_info (struct dwarf2_cu *);
1427
3e43a32a
MS
1428static struct type *die_descriptive_type (struct die_info *,
1429 struct dwarf2_cu *);
b4ba55a1
JB
1430
1431static void set_descriptive_type (struct type *, struct die_info *,
1432 struct dwarf2_cu *);
1433
e7c27a73
DJ
1434static struct type *die_containing_type (struct die_info *,
1435 struct dwarf2_cu *);
c906108c 1436
673bfd45
DE
1437static struct type *lookup_die_type (struct die_info *, struct attribute *,
1438 struct dwarf2_cu *);
c906108c 1439
f792889a 1440static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1441
673bfd45
DE
1442static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1443
0d5cff50 1444static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1445
6e70227d 1446static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1447 const char *suffix, int physname,
1448 struct dwarf2_cu *cu);
63d06c5c 1449
e7c27a73 1450static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1451
348e048f
DE
1452static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1453
e7c27a73 1454static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1455
e7c27a73 1456static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1457
96408a79
SA
1458static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1459
ff013f42
JK
1460static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1461 struct dwarf2_cu *, struct partial_symtab *);
1462
a14ed312 1463static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1464 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1465 struct partial_symtab *);
c906108c 1466
fae299cd
DC
1467static void get_scope_pc_bounds (struct die_info *,
1468 CORE_ADDR *, CORE_ADDR *,
1469 struct dwarf2_cu *);
1470
801e3a5b
JB
1471static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1472 CORE_ADDR, struct dwarf2_cu *);
1473
a14ed312 1474static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1475 struct dwarf2_cu *);
c906108c 1476
a14ed312 1477static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1478 struct type *, struct dwarf2_cu *);
c906108c 1479
a14ed312 1480static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1481 struct die_info *, struct type *,
e7c27a73 1482 struct dwarf2_cu *);
c906108c 1483
a14ed312 1484static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1485 struct type *,
1486 struct dwarf2_cu *);
c906108c 1487
134d01f1 1488static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1489
e7c27a73 1490static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1491
e7c27a73 1492static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1493
5d7cb8df
JK
1494static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1495
27aa8d6a
SW
1496static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1497
f55ee35c
JK
1498static struct type *read_module_type (struct die_info *die,
1499 struct dwarf2_cu *cu);
1500
38d518c9 1501static const char *namespace_name (struct die_info *die,
e142c38c 1502 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1503
134d01f1 1504static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1505
e7c27a73 1506static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1507
6e70227d 1508static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1509 struct dwarf2_cu *);
1510
dee91e82 1511static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1512 gdb_byte *info_ptr,
fe1b8b76 1513 gdb_byte **new_info_ptr,
639d11d3
DC
1514 struct die_info *parent);
1515
dee91e82 1516static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1517 gdb_byte *info_ptr,
fe1b8b76 1518 gdb_byte **new_info_ptr,
639d11d3
DC
1519 struct die_info *parent);
1520
3019eac3
DE
1521static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1522 struct die_info **, gdb_byte *, int *, int);
1523
dee91e82
DE
1524static gdb_byte *read_full_die (const struct die_reader_specs *,
1525 struct die_info **, gdb_byte *, int *);
93311388 1526
e7c27a73 1527static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1528
71c25dea
TT
1529static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1530 struct obstack *);
1531
e142c38c 1532static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1533
98bfdba5
PA
1534static const char *dwarf2_full_name (char *name,
1535 struct die_info *die,
1536 struct dwarf2_cu *cu);
1537
e142c38c 1538static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1539 struct dwarf2_cu **);
9219021c 1540
f39c6ffd 1541static const char *dwarf_tag_name (unsigned int);
c906108c 1542
f39c6ffd 1543static const char *dwarf_attr_name (unsigned int);
c906108c 1544
f39c6ffd 1545static const char *dwarf_form_name (unsigned int);
c906108c 1546
a14ed312 1547static char *dwarf_bool_name (unsigned int);
c906108c 1548
f39c6ffd 1549static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1550
f9aca02d 1551static struct die_info *sibling_die (struct die_info *);
c906108c 1552
d97bc12b
DE
1553static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1554
1555static void dump_die_for_error (struct die_info *);
1556
1557static void dump_die_1 (struct ui_file *, int level, int max_level,
1558 struct die_info *);
c906108c 1559
d97bc12b 1560/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1561
51545339 1562static void store_in_ref_table (struct die_info *,
10b3939b 1563 struct dwarf2_cu *);
c906108c 1564
93311388
DE
1565static int is_ref_attr (struct attribute *);
1566
b64f50a1 1567static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1568
43bbcdc2 1569static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1570
348e048f
DE
1571static struct die_info *follow_die_ref_or_sig (struct die_info *,
1572 struct attribute *,
1573 struct dwarf2_cu **);
1574
10b3939b
DJ
1575static struct die_info *follow_die_ref (struct die_info *,
1576 struct attribute *,
f2f0e013 1577 struct dwarf2_cu **);
c906108c 1578
348e048f
DE
1579static struct die_info *follow_die_sig (struct die_info *,
1580 struct attribute *,
1581 struct dwarf2_cu **);
1582
6c83ed52
TT
1583static struct signatured_type *lookup_signatured_type_at_offset
1584 (struct objfile *objfile,
b64f50a1 1585 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1586
e5fe5e75 1587static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1588
52dc124a 1589static void read_signatured_type (struct signatured_type *);
348e048f 1590
f4dc4d17 1591static struct type_unit_group *get_type_unit_group
094b34ac 1592 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1593
1594static void build_type_unit_groups (die_reader_func_ftype *, void *);
1595
c906108c
SS
1596/* memory allocation interface */
1597
7b5a2f43 1598static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1599
b60c80d6 1600static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1601
09262596
DE
1602static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1603 char *, int);
2e276125 1604
8e19ed76
PS
1605static int attr_form_is_block (struct attribute *);
1606
3690dd37
JB
1607static int attr_form_is_section_offset (struct attribute *);
1608
1609static int attr_form_is_constant (struct attribute *);
1610
8cf6f0b1
TT
1611static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1612 struct dwarf2_loclist_baton *baton,
1613 struct attribute *attr);
1614
93e7bd98
DJ
1615static void dwarf2_symbol_mark_computed (struct attribute *attr,
1616 struct symbol *sym,
1617 struct dwarf2_cu *cu);
4c2df51b 1618
dee91e82
DE
1619static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1620 gdb_byte *info_ptr,
1621 struct abbrev_info *abbrev);
4bb7a0a7 1622
72bf9492
DJ
1623static void free_stack_comp_unit (void *);
1624
72bf9492
DJ
1625static hashval_t partial_die_hash (const void *item);
1626
1627static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1628
ae038cb0 1629static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1630 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1631
9816fde3 1632static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1633 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1634
1635static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1636 struct die_info *comp_unit_die,
1637 enum language pretend_language);
93311388 1638
68dc6402 1639static void free_heap_comp_unit (void *);
ae038cb0
DJ
1640
1641static void free_cached_comp_units (void *);
1642
1643static void age_cached_comp_units (void);
1644
dee91e82 1645static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1646
f792889a
DJ
1647static struct type *set_die_type (struct die_info *, struct type *,
1648 struct dwarf2_cu *);
1c379e20 1649
ae038cb0
DJ
1650static void create_all_comp_units (struct objfile *);
1651
0e50663e 1652static int create_all_type_units (struct objfile *);
1fd400ff 1653
95554aad
TT
1654static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1655 enum language);
10b3939b 1656
95554aad
TT
1657static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1658 enum language);
10b3939b 1659
f4dc4d17
DE
1660static void process_full_type_unit (struct dwarf2_per_cu_data *,
1661 enum language);
1662
10b3939b
DJ
1663static void dwarf2_add_dependence (struct dwarf2_cu *,
1664 struct dwarf2_per_cu_data *);
1665
ae038cb0
DJ
1666static void dwarf2_mark (struct dwarf2_cu *);
1667
1668static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1669
b64f50a1 1670static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1671 struct dwarf2_per_cu_data *per_cu);
1672
f792889a 1673static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1674
9291a0cd
TT
1675static void dwarf2_release_queue (void *dummy);
1676
95554aad
TT
1677static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1678 enum language pretend_language);
1679
1680static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1681 struct dwarf2_per_cu_data *per_cu,
1682 enum language pretend_language);
9291a0cd 1683
a0f42c21 1684static void process_queue (void);
9291a0cd
TT
1685
1686static void find_file_and_directory (struct die_info *die,
1687 struct dwarf2_cu *cu,
1688 char **name, char **comp_dir);
1689
1690static char *file_full_name (int file, struct line_header *lh,
1691 const char *comp_dir);
1692
36586728
TT
1693static gdb_byte *read_and_check_comp_unit_head
1694 (struct comp_unit_head *header,
1695 struct dwarf2_section_info *section,
1696 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1697 int is_debug_types_section);
1698
fd820528 1699static void init_cutu_and_read_dies
f4dc4d17
DE
1700 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1701 int use_existing_cu, int keep,
3019eac3
DE
1702 die_reader_func_ftype *die_reader_func, void *data);
1703
dee91e82
DE
1704static void init_cutu_and_read_dies_simple
1705 (struct dwarf2_per_cu_data *this_cu,
1706 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1707
673bfd45 1708static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1709
3019eac3
DE
1710static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1711
1712static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1713 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1714
1715static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1716 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1717
1718static void free_dwo_file_cleanup (void *);
1719
95554aad
TT
1720static void process_cu_includes (void);
1721
1b80a9fa
JK
1722static void check_producer (struct dwarf2_cu *cu);
1723
9291a0cd
TT
1724#if WORDS_BIGENDIAN
1725
1726/* Convert VALUE between big- and little-endian. */
1727static offset_type
1728byte_swap (offset_type value)
1729{
1730 offset_type result;
1731
1732 result = (value & 0xff) << 24;
1733 result |= (value & 0xff00) << 8;
1734 result |= (value & 0xff0000) >> 8;
1735 result |= (value & 0xff000000) >> 24;
1736 return result;
1737}
1738
1739#define MAYBE_SWAP(V) byte_swap (V)
1740
1741#else
1742#define MAYBE_SWAP(V) (V)
1743#endif /* WORDS_BIGENDIAN */
1744
1745/* The suffix for an index file. */
1746#define INDEX_SUFFIX ".gdb-index"
1747
3da10d80
KS
1748static const char *dwarf2_physname (char *name, struct die_info *die,
1749 struct dwarf2_cu *cu);
1750
c906108c 1751/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1752 information and return true if we have enough to do something.
1753 NAMES points to the dwarf2 section names, or is NULL if the standard
1754 ELF names are used. */
c906108c
SS
1755
1756int
251d32d9
TG
1757dwarf2_has_info (struct objfile *objfile,
1758 const struct dwarf2_debug_sections *names)
c906108c 1759{
be391dca
TT
1760 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1761 if (!dwarf2_per_objfile)
1762 {
1763 /* Initialize per-objfile state. */
1764 struct dwarf2_per_objfile *data
1765 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1766
be391dca
TT
1767 memset (data, 0, sizeof (*data));
1768 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1769 dwarf2_per_objfile = data;
6502dd73 1770
251d32d9
TG
1771 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1772 (void *) names);
be391dca
TT
1773 dwarf2_per_objfile->objfile = objfile;
1774 }
1775 return (dwarf2_per_objfile->info.asection != NULL
1776 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1777}
1778
251d32d9
TG
1779/* When loading sections, we look either for uncompressed section or for
1780 compressed section names. */
233a11ab
CS
1781
1782static int
251d32d9
TG
1783section_is_p (const char *section_name,
1784 const struct dwarf2_section_names *names)
233a11ab 1785{
251d32d9
TG
1786 if (names->normal != NULL
1787 && strcmp (section_name, names->normal) == 0)
1788 return 1;
1789 if (names->compressed != NULL
1790 && strcmp (section_name, names->compressed) == 0)
1791 return 1;
1792 return 0;
233a11ab
CS
1793}
1794
c906108c
SS
1795/* This function is mapped across the sections and remembers the
1796 offset and size of each of the debugging sections we are interested
1797 in. */
1798
1799static void
251d32d9 1800dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1801{
251d32d9 1802 const struct dwarf2_debug_sections *names;
dc7650b8 1803 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1804
1805 if (vnames == NULL)
1806 names = &dwarf2_elf_names;
1807 else
1808 names = (const struct dwarf2_debug_sections *) vnames;
1809
dc7650b8
JK
1810 if ((aflag & SEC_HAS_CONTENTS) == 0)
1811 {
1812 }
1813 else if (section_is_p (sectp->name, &names->info))
c906108c 1814 {
dce234bc
PP
1815 dwarf2_per_objfile->info.asection = sectp;
1816 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1817 }
251d32d9 1818 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1819 {
dce234bc
PP
1820 dwarf2_per_objfile->abbrev.asection = sectp;
1821 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1822 }
251d32d9 1823 else if (section_is_p (sectp->name, &names->line))
c906108c 1824 {
dce234bc
PP
1825 dwarf2_per_objfile->line.asection = sectp;
1826 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1827 }
251d32d9 1828 else if (section_is_p (sectp->name, &names->loc))
c906108c 1829 {
dce234bc
PP
1830 dwarf2_per_objfile->loc.asection = sectp;
1831 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1832 }
251d32d9 1833 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1834 {
dce234bc
PP
1835 dwarf2_per_objfile->macinfo.asection = sectp;
1836 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1837 }
cf2c3c16
TT
1838 else if (section_is_p (sectp->name, &names->macro))
1839 {
1840 dwarf2_per_objfile->macro.asection = sectp;
1841 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1842 }
251d32d9 1843 else if (section_is_p (sectp->name, &names->str))
c906108c 1844 {
dce234bc
PP
1845 dwarf2_per_objfile->str.asection = sectp;
1846 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1847 }
3019eac3
DE
1848 else if (section_is_p (sectp->name, &names->addr))
1849 {
1850 dwarf2_per_objfile->addr.asection = sectp;
1851 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1852 }
251d32d9 1853 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1854 {
dce234bc
PP
1855 dwarf2_per_objfile->frame.asection = sectp;
1856 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1857 }
251d32d9 1858 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1859 {
dc7650b8
JK
1860 dwarf2_per_objfile->eh_frame.asection = sectp;
1861 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1862 }
251d32d9 1863 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1864 {
dce234bc
PP
1865 dwarf2_per_objfile->ranges.asection = sectp;
1866 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1867 }
251d32d9 1868 else if (section_is_p (sectp->name, &names->types))
348e048f 1869 {
8b70b953
TT
1870 struct dwarf2_section_info type_section;
1871
1872 memset (&type_section, 0, sizeof (type_section));
1873 type_section.asection = sectp;
1874 type_section.size = bfd_get_section_size (sectp);
1875
1876 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1877 &type_section);
348e048f 1878 }
251d32d9 1879 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1880 {
1881 dwarf2_per_objfile->gdb_index.asection = sectp;
1882 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1883 }
dce234bc 1884
72dca2f5
FR
1885 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1886 && bfd_section_vma (abfd, sectp) == 0)
1887 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1888}
1889
fceca515
DE
1890/* A helper function that decides whether a section is empty,
1891 or not present. */
9e0ac564
TT
1892
1893static int
1894dwarf2_section_empty_p (struct dwarf2_section_info *info)
1895{
1896 return info->asection == NULL || info->size == 0;
1897}
1898
3019eac3
DE
1899/* Read the contents of the section INFO.
1900 OBJFILE is the main object file, but not necessarily the file where
1901 the section comes from. E.g., for DWO files INFO->asection->owner
1902 is the bfd of the DWO file.
dce234bc 1903 If the section is compressed, uncompress it before returning. */
c906108c 1904
dce234bc
PP
1905static void
1906dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1907{
dce234bc 1908 asection *sectp = info->asection;
3019eac3 1909 bfd *abfd;
dce234bc
PP
1910 gdb_byte *buf, *retbuf;
1911 unsigned char header[4];
c906108c 1912
be391dca
TT
1913 if (info->readin)
1914 return;
dce234bc 1915 info->buffer = NULL;
be391dca 1916 info->readin = 1;
188dd5d6 1917
9e0ac564 1918 if (dwarf2_section_empty_p (info))
dce234bc 1919 return;
c906108c 1920
3019eac3
DE
1921 abfd = sectp->owner;
1922
4bf44c1c
TT
1923 /* If the section has relocations, we must read it ourselves.
1924 Otherwise we attach it to the BFD. */
1925 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1926 {
4bf44c1c 1927 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1928
4bf44c1c
TT
1929 /* We have to cast away const here for historical reasons.
1930 Fixing dwarf2read to be const-correct would be quite nice. */
1931 info->buffer = (gdb_byte *) bytes;
1932 return;
dce234bc 1933 }
dce234bc 1934
4bf44c1c
TT
1935 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1936 info->buffer = buf;
dce234bc
PP
1937
1938 /* When debugging .o files, we may need to apply relocations; see
1939 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1940 We never compress sections in .o files, so we only need to
1941 try this when the section is not compressed. */
ac8035ab 1942 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1943 if (retbuf != NULL)
1944 {
1945 info->buffer = retbuf;
1946 return;
1947 }
1948
1949 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1950 || bfd_bread (buf, info->size, abfd) != info->size)
1951 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1952 bfd_get_filename (abfd));
1953}
1954
9e0ac564
TT
1955/* A helper function that returns the size of a section in a safe way.
1956 If you are positive that the section has been read before using the
1957 size, then it is safe to refer to the dwarf2_section_info object's
1958 "size" field directly. In other cases, you must call this
1959 function, because for compressed sections the size field is not set
1960 correctly until the section has been read. */
1961
1962static bfd_size_type
1963dwarf2_section_size (struct objfile *objfile,
1964 struct dwarf2_section_info *info)
1965{
1966 if (!info->readin)
1967 dwarf2_read_section (objfile, info);
1968 return info->size;
1969}
1970
dce234bc 1971/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1972 SECTION_NAME. */
af34e669 1973
dce234bc 1974void
3017a003
TG
1975dwarf2_get_section_info (struct objfile *objfile,
1976 enum dwarf2_section_enum sect,
dce234bc
PP
1977 asection **sectp, gdb_byte **bufp,
1978 bfd_size_type *sizep)
1979{
1980 struct dwarf2_per_objfile *data
1981 = objfile_data (objfile, dwarf2_objfile_data_key);
1982 struct dwarf2_section_info *info;
a3b2a86b
TT
1983
1984 /* We may see an objfile without any DWARF, in which case we just
1985 return nothing. */
1986 if (data == NULL)
1987 {
1988 *sectp = NULL;
1989 *bufp = NULL;
1990 *sizep = 0;
1991 return;
1992 }
3017a003
TG
1993 switch (sect)
1994 {
1995 case DWARF2_DEBUG_FRAME:
1996 info = &data->frame;
1997 break;
1998 case DWARF2_EH_FRAME:
1999 info = &data->eh_frame;
2000 break;
2001 default:
2002 gdb_assert_not_reached ("unexpected section");
2003 }
dce234bc 2004
9e0ac564 2005 dwarf2_read_section (objfile, info);
dce234bc
PP
2006
2007 *sectp = info->asection;
2008 *bufp = info->buffer;
2009 *sizep = info->size;
2010}
2011
36586728
TT
2012/* A helper function to find the sections for a .dwz file. */
2013
2014static void
2015locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2016{
2017 struct dwz_file *dwz_file = arg;
2018
2019 /* Note that we only support the standard ELF names, because .dwz
2020 is ELF-only (at the time of writing). */
2021 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2022 {
2023 dwz_file->abbrev.asection = sectp;
2024 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2025 }
2026 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2027 {
2028 dwz_file->info.asection = sectp;
2029 dwz_file->info.size = bfd_get_section_size (sectp);
2030 }
2031 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2032 {
2033 dwz_file->str.asection = sectp;
2034 dwz_file->str.size = bfd_get_section_size (sectp);
2035 }
2036 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2037 {
2038 dwz_file->line.asection = sectp;
2039 dwz_file->line.size = bfd_get_section_size (sectp);
2040 }
2041 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2042 {
2043 dwz_file->macro.asection = sectp;
2044 dwz_file->macro.size = bfd_get_section_size (sectp);
2045 }
2ec9a5e0
TT
2046 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2047 {
2048 dwz_file->gdb_index.asection = sectp;
2049 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2050 }
36586728
TT
2051}
2052
2053/* Open the separate '.dwz' debug file, if needed. Error if the file
2054 cannot be found. */
2055
2056static struct dwz_file *
2057dwarf2_get_dwz_file (void)
2058{
2059 bfd *abfd, *dwz_bfd;
2060 asection *section;
2061 gdb_byte *data;
2062 struct cleanup *cleanup;
2063 const char *filename;
2064 struct dwz_file *result;
2065
2066 if (dwarf2_per_objfile->dwz_file != NULL)
2067 return dwarf2_per_objfile->dwz_file;
2068
2069 abfd = dwarf2_per_objfile->objfile->obfd;
2070 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2071 if (section == NULL)
2072 error (_("could not find '.gnu_debugaltlink' section"));
2073 if (!bfd_malloc_and_get_section (abfd, section, &data))
2074 error (_("could not read '.gnu_debugaltlink' section: %s"),
2075 bfd_errmsg (bfd_get_error ()));
2076 cleanup = make_cleanup (xfree, data);
2077
2078 filename = data;
2079 if (!IS_ABSOLUTE_PATH (filename))
2080 {
2081 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2082 char *rel;
2083
2084 make_cleanup (xfree, abs);
2085 abs = ldirname (abs);
2086 make_cleanup (xfree, abs);
2087
2088 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2089 make_cleanup (xfree, rel);
2090 filename = rel;
2091 }
2092
2093 /* The format is just a NUL-terminated file name, followed by the
2094 build-id. For now, though, we ignore the build-id. */
2095 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2096 if (dwz_bfd == NULL)
2097 error (_("could not read '%s': %s"), filename,
2098 bfd_errmsg (bfd_get_error ()));
2099
2100 if (!bfd_check_format (dwz_bfd, bfd_object))
2101 {
2102 gdb_bfd_unref (dwz_bfd);
2103 error (_("file '%s' was not usable: %s"), filename,
2104 bfd_errmsg (bfd_get_error ()));
2105 }
2106
2107 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2108 struct dwz_file);
2109 result->dwz_bfd = dwz_bfd;
2110
2111 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2112
2113 do_cleanups (cleanup);
2114
8d2cc612 2115 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2116 return result;
2117}
9291a0cd 2118\f
7b9f3c50
DE
2119/* DWARF quick_symbols_functions support. */
2120
2121/* TUs can share .debug_line entries, and there can be a lot more TUs than
2122 unique line tables, so we maintain a separate table of all .debug_line
2123 derived entries to support the sharing.
2124 All the quick functions need is the list of file names. We discard the
2125 line_header when we're done and don't need to record it here. */
2126struct quick_file_names
2127{
094b34ac
DE
2128 /* The data used to construct the hash key. */
2129 struct stmt_list_hash hash;
7b9f3c50
DE
2130
2131 /* The number of entries in file_names, real_names. */
2132 unsigned int num_file_names;
2133
2134 /* The file names from the line table, after being run through
2135 file_full_name. */
2136 const char **file_names;
2137
2138 /* The file names from the line table after being run through
2139 gdb_realpath. These are computed lazily. */
2140 const char **real_names;
2141};
2142
2143/* When using the index (and thus not using psymtabs), each CU has an
2144 object of this type. This is used to hold information needed by
2145 the various "quick" methods. */
2146struct dwarf2_per_cu_quick_data
2147{
2148 /* The file table. This can be NULL if there was no file table
2149 or it's currently not read in.
2150 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2151 struct quick_file_names *file_names;
2152
2153 /* The corresponding symbol table. This is NULL if symbols for this
2154 CU have not yet been read. */
2155 struct symtab *symtab;
2156
2157 /* A temporary mark bit used when iterating over all CUs in
2158 expand_symtabs_matching. */
2159 unsigned int mark : 1;
2160
2161 /* True if we've tried to read the file table and found there isn't one.
2162 There will be no point in trying to read it again next time. */
2163 unsigned int no_file_data : 1;
2164};
2165
094b34ac
DE
2166/* Utility hash function for a stmt_list_hash. */
2167
2168static hashval_t
2169hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2170{
2171 hashval_t v = 0;
2172
2173 if (stmt_list_hash->dwo_unit != NULL)
2174 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2175 v += stmt_list_hash->line_offset.sect_off;
2176 return v;
2177}
2178
2179/* Utility equality function for a stmt_list_hash. */
2180
2181static int
2182eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2183 const struct stmt_list_hash *rhs)
2184{
2185 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2186 return 0;
2187 if (lhs->dwo_unit != NULL
2188 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2189 return 0;
2190
2191 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2192}
2193
7b9f3c50
DE
2194/* Hash function for a quick_file_names. */
2195
2196static hashval_t
2197hash_file_name_entry (const void *e)
2198{
2199 const struct quick_file_names *file_data = e;
2200
094b34ac 2201 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2202}
2203
2204/* Equality function for a quick_file_names. */
2205
2206static int
2207eq_file_name_entry (const void *a, const void *b)
2208{
2209 const struct quick_file_names *ea = a;
2210 const struct quick_file_names *eb = b;
2211
094b34ac 2212 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2213}
2214
2215/* Delete function for a quick_file_names. */
2216
2217static void
2218delete_file_name_entry (void *e)
2219{
2220 struct quick_file_names *file_data = e;
2221 int i;
2222
2223 for (i = 0; i < file_data->num_file_names; ++i)
2224 {
2225 xfree ((void*) file_data->file_names[i]);
2226 if (file_data->real_names)
2227 xfree ((void*) file_data->real_names[i]);
2228 }
2229
2230 /* The space for the struct itself lives on objfile_obstack,
2231 so we don't free it here. */
2232}
2233
2234/* Create a quick_file_names hash table. */
2235
2236static htab_t
2237create_quick_file_names_table (unsigned int nr_initial_entries)
2238{
2239 return htab_create_alloc (nr_initial_entries,
2240 hash_file_name_entry, eq_file_name_entry,
2241 delete_file_name_entry, xcalloc, xfree);
2242}
9291a0cd 2243
918dd910
JK
2244/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2245 have to be created afterwards. You should call age_cached_comp_units after
2246 processing PER_CU->CU. dw2_setup must have been already called. */
2247
2248static void
2249load_cu (struct dwarf2_per_cu_data *per_cu)
2250{
3019eac3 2251 if (per_cu->is_debug_types)
e5fe5e75 2252 load_full_type_unit (per_cu);
918dd910 2253 else
95554aad 2254 load_full_comp_unit (per_cu, language_minimal);
918dd910 2255
918dd910 2256 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2257
2258 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2259}
2260
a0f42c21 2261/* Read in the symbols for PER_CU. */
2fdf6df6 2262
9291a0cd 2263static void
a0f42c21 2264dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2265{
2266 struct cleanup *back_to;
2267
f4dc4d17
DE
2268 /* Skip type_unit_groups, reading the type units they contain
2269 is handled elsewhere. */
2270 if (IS_TYPE_UNIT_GROUP (per_cu))
2271 return;
2272
9291a0cd
TT
2273 back_to = make_cleanup (dwarf2_release_queue, NULL);
2274
95554aad
TT
2275 if (dwarf2_per_objfile->using_index
2276 ? per_cu->v.quick->symtab == NULL
2277 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2278 {
2279 queue_comp_unit (per_cu, language_minimal);
2280 load_cu (per_cu);
2281 }
9291a0cd 2282
a0f42c21 2283 process_queue ();
9291a0cd
TT
2284
2285 /* Age the cache, releasing compilation units that have not
2286 been used recently. */
2287 age_cached_comp_units ();
2288
2289 do_cleanups (back_to);
2290}
2291
2292/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2293 the objfile from which this CU came. Returns the resulting symbol
2294 table. */
2fdf6df6 2295
9291a0cd 2296static struct symtab *
a0f42c21 2297dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2298{
95554aad 2299 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2300 if (!per_cu->v.quick->symtab)
2301 {
2302 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2303 increment_reading_symtab ();
a0f42c21 2304 dw2_do_instantiate_symtab (per_cu);
95554aad 2305 process_cu_includes ();
9291a0cd
TT
2306 do_cleanups (back_to);
2307 }
2308 return per_cu->v.quick->symtab;
2309}
2310
f4dc4d17
DE
2311/* Return the CU given its index.
2312
2313 This is intended for loops like:
2314
2315 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2316 + dwarf2_per_objfile->n_type_units); ++i)
2317 {
2318 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2319
2320 ...;
2321 }
2322*/
2fdf6df6 2323
1fd400ff
TT
2324static struct dwarf2_per_cu_data *
2325dw2_get_cu (int index)
2326{
2327 if (index >= dwarf2_per_objfile->n_comp_units)
2328 {
f4dc4d17 2329 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2330 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2331 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2332 }
2333
2334 return dwarf2_per_objfile->all_comp_units[index];
2335}
2336
2337/* Return the primary CU given its index.
2338 The difference between this function and dw2_get_cu is in the handling
2339 of type units (TUs). Here we return the type_unit_group object.
2340
2341 This is intended for loops like:
2342
2343 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2344 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2345 {
2346 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2347
2348 ...;
2349 }
2350*/
2351
2352static struct dwarf2_per_cu_data *
2353dw2_get_primary_cu (int index)
2354{
2355 if (index >= dwarf2_per_objfile->n_comp_units)
2356 {
1fd400ff 2357 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2358 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2359 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2360 }
f4dc4d17 2361
1fd400ff
TT
2362 return dwarf2_per_objfile->all_comp_units[index];
2363}
2364
2ec9a5e0
TT
2365/* A helper for create_cus_from_index that handles a given list of
2366 CUs. */
2fdf6df6 2367
74a0d9f6 2368static void
2ec9a5e0
TT
2369create_cus_from_index_list (struct objfile *objfile,
2370 const gdb_byte *cu_list, offset_type n_elements,
2371 struct dwarf2_section_info *section,
2372 int is_dwz,
2373 int base_offset)
9291a0cd
TT
2374{
2375 offset_type i;
9291a0cd 2376
2ec9a5e0 2377 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2378 {
2379 struct dwarf2_per_cu_data *the_cu;
2380 ULONGEST offset, length;
2381
74a0d9f6
JK
2382 gdb_static_assert (sizeof (ULONGEST) >= 8);
2383 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2384 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2385 cu_list += 2 * 8;
2386
2387 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2388 struct dwarf2_per_cu_data);
b64f50a1 2389 the_cu->offset.sect_off = offset;
9291a0cd
TT
2390 the_cu->length = length;
2391 the_cu->objfile = objfile;
2ec9a5e0 2392 the_cu->info_or_types_section = section;
9291a0cd
TT
2393 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2394 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2395 the_cu->is_dwz = is_dwz;
2396 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2397 }
9291a0cd
TT
2398}
2399
2ec9a5e0 2400/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2401 the CU objects for this objfile. */
2ec9a5e0 2402
74a0d9f6 2403static void
2ec9a5e0
TT
2404create_cus_from_index (struct objfile *objfile,
2405 const gdb_byte *cu_list, offset_type cu_list_elements,
2406 const gdb_byte *dwz_list, offset_type dwz_elements)
2407{
2408 struct dwz_file *dwz;
2409
2410 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2411 dwarf2_per_objfile->all_comp_units
2412 = obstack_alloc (&objfile->objfile_obstack,
2413 dwarf2_per_objfile->n_comp_units
2414 * sizeof (struct dwarf2_per_cu_data *));
2415
74a0d9f6
JK
2416 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2417 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2418
2419 if (dwz_elements == 0)
74a0d9f6 2420 return;
2ec9a5e0
TT
2421
2422 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2423 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2424 cu_list_elements / 2);
2ec9a5e0
TT
2425}
2426
1fd400ff 2427/* Create the signatured type hash table from the index. */
673bfd45 2428
74a0d9f6 2429static void
673bfd45 2430create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2431 struct dwarf2_section_info *section,
673bfd45
DE
2432 const gdb_byte *bytes,
2433 offset_type elements)
1fd400ff
TT
2434{
2435 offset_type i;
673bfd45 2436 htab_t sig_types_hash;
1fd400ff 2437
d467dd73
DE
2438 dwarf2_per_objfile->n_type_units = elements / 3;
2439 dwarf2_per_objfile->all_type_units
1fd400ff 2440 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2441 dwarf2_per_objfile->n_type_units
b4dd5633 2442 * sizeof (struct signatured_type *));
1fd400ff 2443
673bfd45 2444 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2445
2446 for (i = 0; i < elements; i += 3)
2447 {
52dc124a
DE
2448 struct signatured_type *sig_type;
2449 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2450 void **slot;
2451
74a0d9f6
JK
2452 gdb_static_assert (sizeof (ULONGEST) >= 8);
2453 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2454 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2455 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2456 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2457 bytes += 3 * 8;
2458
52dc124a 2459 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2460 struct signatured_type);
52dc124a 2461 sig_type->signature = signature;
3019eac3
DE
2462 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2463 sig_type->per_cu.is_debug_types = 1;
2464 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2465 sig_type->per_cu.offset.sect_off = offset;
2466 sig_type->per_cu.objfile = objfile;
2467 sig_type->per_cu.v.quick
1fd400ff
TT
2468 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2469 struct dwarf2_per_cu_quick_data);
2470
52dc124a
DE
2471 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2472 *slot = sig_type;
1fd400ff 2473
b4dd5633 2474 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2475 }
2476
673bfd45 2477 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2478}
2479
9291a0cd
TT
2480/* Read the address map data from the mapped index, and use it to
2481 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2482
9291a0cd
TT
2483static void
2484create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2485{
2486 const gdb_byte *iter, *end;
2487 struct obstack temp_obstack;
2488 struct addrmap *mutable_map;
2489 struct cleanup *cleanup;
2490 CORE_ADDR baseaddr;
2491
2492 obstack_init (&temp_obstack);
2493 cleanup = make_cleanup_obstack_free (&temp_obstack);
2494 mutable_map = addrmap_create_mutable (&temp_obstack);
2495
2496 iter = index->address_table;
2497 end = iter + index->address_table_size;
2498
2499 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2500
2501 while (iter < end)
2502 {
2503 ULONGEST hi, lo, cu_index;
2504 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2505 iter += 8;
2506 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2507 iter += 8;
2508 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2509 iter += 4;
2510
2511 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2512 dw2_get_cu (cu_index));
9291a0cd
TT
2513 }
2514
2515 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2516 &objfile->objfile_obstack);
2517 do_cleanups (cleanup);
2518}
2519
59d7bcaf
JK
2520/* The hash function for strings in the mapped index. This is the same as
2521 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2522 implementation. This is necessary because the hash function is tied to the
2523 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2524 SYMBOL_HASH_NEXT.
2525
2526 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2527
9291a0cd 2528static hashval_t
559a7a62 2529mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2530{
2531 const unsigned char *str = (const unsigned char *) p;
2532 hashval_t r = 0;
2533 unsigned char c;
2534
2535 while ((c = *str++) != 0)
559a7a62
JK
2536 {
2537 if (index_version >= 5)
2538 c = tolower (c);
2539 r = r * 67 + c - 113;
2540 }
9291a0cd
TT
2541
2542 return r;
2543}
2544
2545/* Find a slot in the mapped index INDEX for the object named NAME.
2546 If NAME is found, set *VEC_OUT to point to the CU vector in the
2547 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2548
9291a0cd
TT
2549static int
2550find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2551 offset_type **vec_out)
2552{
0cf03b49
JK
2553 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2554 offset_type hash;
9291a0cd 2555 offset_type slot, step;
559a7a62 2556 int (*cmp) (const char *, const char *);
9291a0cd 2557
0cf03b49
JK
2558 if (current_language->la_language == language_cplus
2559 || current_language->la_language == language_java
2560 || current_language->la_language == language_fortran)
2561 {
2562 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2563 not contain any. */
2564 const char *paren = strchr (name, '(');
2565
2566 if (paren)
2567 {
2568 char *dup;
2569
2570 dup = xmalloc (paren - name + 1);
2571 memcpy (dup, name, paren - name);
2572 dup[paren - name] = 0;
2573
2574 make_cleanup (xfree, dup);
2575 name = dup;
2576 }
2577 }
2578
559a7a62 2579 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2580 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2581 simulate our NAME being searched is also lowercased. */
2582 hash = mapped_index_string_hash ((index->version == 4
2583 && case_sensitivity == case_sensitive_off
2584 ? 5 : index->version),
2585 name);
2586
3876f04e
DE
2587 slot = hash & (index->symbol_table_slots - 1);
2588 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2589 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2590
2591 for (;;)
2592 {
2593 /* Convert a slot number to an offset into the table. */
2594 offset_type i = 2 * slot;
2595 const char *str;
3876f04e 2596 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2597 {
2598 do_cleanups (back_to);
2599 return 0;
2600 }
9291a0cd 2601
3876f04e 2602 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2603 if (!cmp (name, str))
9291a0cd
TT
2604 {
2605 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2606 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2607 do_cleanups (back_to);
9291a0cd
TT
2608 return 1;
2609 }
2610
3876f04e 2611 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2612 }
2613}
2614
2ec9a5e0
TT
2615/* A helper function that reads the .gdb_index from SECTION and fills
2616 in MAP. FILENAME is the name of the file containing the section;
2617 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2618 ok to use deprecated sections.
2619
2620 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2621 out parameters that are filled in with information about the CU and
2622 TU lists in the section.
2623
2624 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2625
9291a0cd 2626static int
2ec9a5e0
TT
2627read_index_from_section (struct objfile *objfile,
2628 const char *filename,
2629 int deprecated_ok,
2630 struct dwarf2_section_info *section,
2631 struct mapped_index *map,
2632 const gdb_byte **cu_list,
2633 offset_type *cu_list_elements,
2634 const gdb_byte **types_list,
2635 offset_type *types_list_elements)
9291a0cd 2636{
9291a0cd 2637 char *addr;
2ec9a5e0 2638 offset_type version;
b3b272e1 2639 offset_type *metadata;
1fd400ff 2640 int i;
9291a0cd 2641
2ec9a5e0 2642 if (dwarf2_section_empty_p (section))
9291a0cd 2643 return 0;
82430852
JK
2644
2645 /* Older elfutils strip versions could keep the section in the main
2646 executable while splitting it for the separate debug info file. */
2ec9a5e0 2647 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2648 return 0;
2649
2ec9a5e0 2650 dwarf2_read_section (objfile, section);
9291a0cd 2651
2ec9a5e0 2652 addr = section->buffer;
9291a0cd 2653 /* Version check. */
1fd400ff 2654 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2655 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2656 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2657 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2658 indices. */
831adc1f 2659 if (version < 4)
481860b3
GB
2660 {
2661 static int warning_printed = 0;
2662 if (!warning_printed)
2663 {
2664 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2665 filename);
481860b3
GB
2666 warning_printed = 1;
2667 }
2668 return 0;
2669 }
2670 /* Index version 4 uses a different hash function than index version
2671 5 and later.
2672
2673 Versions earlier than 6 did not emit psymbols for inlined
2674 functions. Using these files will cause GDB not to be able to
2675 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2676 indices unless the user has done
2677 "set use-deprecated-index-sections on". */
2ec9a5e0 2678 if (version < 6 && !deprecated_ok)
481860b3
GB
2679 {
2680 static int warning_printed = 0;
2681 if (!warning_printed)
2682 {
e615022a
DE
2683 warning (_("\
2684Skipping deprecated .gdb_index section in %s.\n\
2685Do \"set use-deprecated-index-sections on\" before the file is read\n\
2686to use the section anyway."),
2ec9a5e0 2687 filename);
481860b3
GB
2688 warning_printed = 1;
2689 }
2690 return 0;
2691 }
2692 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2693 longer backward compatible. */
156942c7 2694 if (version > 7)
594e8718 2695 return 0;
9291a0cd 2696
559a7a62 2697 map->version = version;
2ec9a5e0 2698 map->total_size = section->size;
9291a0cd
TT
2699
2700 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2701
2702 i = 0;
2ec9a5e0
TT
2703 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2704 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2705 / 8);
1fd400ff
TT
2706 ++i;
2707
2ec9a5e0
TT
2708 *types_list = addr + MAYBE_SWAP (metadata[i]);
2709 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2710 - MAYBE_SWAP (metadata[i]))
2711 / 8);
987d643c 2712 ++i;
1fd400ff
TT
2713
2714 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2715 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2716 - MAYBE_SWAP (metadata[i]));
2717 ++i;
2718
3876f04e
DE
2719 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2720 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2721 - MAYBE_SWAP (metadata[i]))
2722 / (2 * sizeof (offset_type)));
1fd400ff 2723 ++i;
9291a0cd 2724
1fd400ff
TT
2725 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2726
2ec9a5e0
TT
2727 return 1;
2728}
2729
2730
2731/* Read the index file. If everything went ok, initialize the "quick"
2732 elements of all the CUs and return 1. Otherwise, return 0. */
2733
2734static int
2735dwarf2_read_index (struct objfile *objfile)
2736{
2737 struct mapped_index local_map, *map;
2738 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2739 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2740
2741 if (!read_index_from_section (objfile, objfile->name,
2742 use_deprecated_index_sections,
2743 &dwarf2_per_objfile->gdb_index, &local_map,
2744 &cu_list, &cu_list_elements,
2745 &types_list, &types_list_elements))
2746 return 0;
2747
0fefef59 2748 /* Don't use the index if it's empty. */
2ec9a5e0 2749 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2750 return 0;
2751
2ec9a5e0
TT
2752 /* If there is a .dwz file, read it so we can get its CU list as
2753 well. */
2754 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2755 {
2756 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2757 struct mapped_index dwz_map;
2758 const gdb_byte *dwz_types_ignore;
2759 offset_type dwz_types_elements_ignore;
2760
2761 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2762 1,
2763 &dwz->gdb_index, &dwz_map,
2764 &dwz_list, &dwz_list_elements,
2765 &dwz_types_ignore,
2766 &dwz_types_elements_ignore))
2767 {
2768 warning (_("could not read '.gdb_index' section from %s; skipping"),
2769 bfd_get_filename (dwz->dwz_bfd));
2770 return 0;
2771 }
2772 }
2773
74a0d9f6
JK
2774 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2775 dwz_list_elements);
1fd400ff 2776
8b70b953
TT
2777 if (types_list_elements)
2778 {
2779 struct dwarf2_section_info *section;
2780
2781 /* We can only handle a single .debug_types when we have an
2782 index. */
2783 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2784 return 0;
2785
2786 section = VEC_index (dwarf2_section_info_def,
2787 dwarf2_per_objfile->types, 0);
2788
74a0d9f6
JK
2789 create_signatured_type_table_from_index (objfile, section, types_list,
2790 types_list_elements);
8b70b953 2791 }
9291a0cd 2792
2ec9a5e0
TT
2793 create_addrmap_from_index (objfile, &local_map);
2794
2795 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2796 *map = local_map;
9291a0cd
TT
2797
2798 dwarf2_per_objfile->index_table = map;
2799 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2800 dwarf2_per_objfile->quick_file_names_table =
2801 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2802
2803 return 1;
2804}
2805
2806/* A helper for the "quick" functions which sets the global
2807 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2808
9291a0cd
TT
2809static void
2810dw2_setup (struct objfile *objfile)
2811{
2812 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2813 gdb_assert (dwarf2_per_objfile);
2814}
2815
f4dc4d17
DE
2816/* Reader function for dw2_build_type_unit_groups. */
2817
2818static void
2819dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2820 gdb_byte *info_ptr,
2821 struct die_info *type_unit_die,
2822 int has_children,
2823 void *data)
2824{
2825 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2826 struct attribute *attr;
2827 struct type_unit_group *tu_group;
2828
2829 gdb_assert (data == NULL);
2830
2831 if (! has_children)
2832 return;
2833
2834 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2835 /* Call this for its side-effect of creating the associated
2836 struct type_unit_group if it doesn't already exist. */
094b34ac 2837 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2838}
2839
2840/* Build dwarf2_per_objfile->type_unit_groups.
2841 This function may be called multiple times. */
2842
2843static void
2844dw2_build_type_unit_groups (void)
2845{
2846 if (dwarf2_per_objfile->type_unit_groups == NULL)
2847 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2848}
2849
dee91e82 2850/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2851
dee91e82
DE
2852static void
2853dw2_get_file_names_reader (const struct die_reader_specs *reader,
2854 gdb_byte *info_ptr,
2855 struct die_info *comp_unit_die,
2856 int has_children,
2857 void *data)
9291a0cd 2858{
dee91e82
DE
2859 struct dwarf2_cu *cu = reader->cu;
2860 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2861 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2862 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2863 struct line_header *lh;
9291a0cd 2864 struct attribute *attr;
dee91e82 2865 int i;
9291a0cd 2866 char *name, *comp_dir;
7b9f3c50
DE
2867 void **slot;
2868 struct quick_file_names *qfn;
2869 unsigned int line_offset;
9291a0cd 2870
07261596
TT
2871 /* Our callers never want to match partial units -- instead they
2872 will match the enclosing full CU. */
2873 if (comp_unit_die->tag == DW_TAG_partial_unit)
2874 {
2875 this_cu->v.quick->no_file_data = 1;
2876 return;
2877 }
2878
094b34ac
DE
2879 /* If we're reading the line header for TUs, store it in the "per_cu"
2880 for tu_group. */
2881 if (this_cu->is_debug_types)
2882 {
2883 struct type_unit_group *tu_group = data;
2884
2885 gdb_assert (tu_group != NULL);
2886 lh_cu = &tu_group->per_cu;
2887 }
2888 else
2889 lh_cu = this_cu;
2890
7b9f3c50
DE
2891 lh = NULL;
2892 slot = NULL;
2893 line_offset = 0;
dee91e82
DE
2894
2895 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2896 if (attr)
2897 {
7b9f3c50
DE
2898 struct quick_file_names find_entry;
2899
2900 line_offset = DW_UNSND (attr);
2901
2902 /* We may have already read in this line header (TU line header sharing).
2903 If we have we're done. */
094b34ac
DE
2904 find_entry.hash.dwo_unit = cu->dwo_unit;
2905 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2906 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2907 &find_entry, INSERT);
2908 if (*slot != NULL)
2909 {
094b34ac 2910 lh_cu->v.quick->file_names = *slot;
dee91e82 2911 return;
7b9f3c50
DE
2912 }
2913
3019eac3 2914 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2915 }
2916 if (lh == NULL)
2917 {
094b34ac 2918 lh_cu->v.quick->no_file_data = 1;
dee91e82 2919 return;
9291a0cd
TT
2920 }
2921
7b9f3c50 2922 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2923 qfn->hash.dwo_unit = cu->dwo_unit;
2924 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2925 gdb_assert (slot != NULL);
2926 *slot = qfn;
9291a0cd 2927
dee91e82 2928 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2929
7b9f3c50
DE
2930 qfn->num_file_names = lh->num_file_names;
2931 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2932 lh->num_file_names * sizeof (char *));
9291a0cd 2933 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2934 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2935 qfn->real_names = NULL;
9291a0cd 2936
7b9f3c50 2937 free_line_header (lh);
7b9f3c50 2938
094b34ac 2939 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2940}
2941
2942/* A helper for the "quick" functions which attempts to read the line
2943 table for THIS_CU. */
2944
2945static struct quick_file_names *
2946dw2_get_file_names (struct objfile *objfile,
2947 struct dwarf2_per_cu_data *this_cu)
2948{
f4dc4d17
DE
2949 /* For TUs this should only be called on the parent group. */
2950 if (this_cu->is_debug_types)
2951 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2952
dee91e82
DE
2953 if (this_cu->v.quick->file_names != NULL)
2954 return this_cu->v.quick->file_names;
2955 /* If we know there is no line data, no point in looking again. */
2956 if (this_cu->v.quick->no_file_data)
2957 return NULL;
2958
3019eac3
DE
2959 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2960 in the stub for CUs, there's is no need to lookup the DWO file.
2961 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2962 DWO file. */
2963 if (this_cu->is_debug_types)
094b34ac
DE
2964 {
2965 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2966
2967 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2968 dw2_get_file_names_reader, tu_group);
2969 }
3019eac3
DE
2970 else
2971 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2972
2973 if (this_cu->v.quick->no_file_data)
2974 return NULL;
2975 return this_cu->v.quick->file_names;
9291a0cd
TT
2976}
2977
2978/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2979 real path for a given file name from the line table. */
2fdf6df6 2980
9291a0cd 2981static const char *
7b9f3c50
DE
2982dw2_get_real_path (struct objfile *objfile,
2983 struct quick_file_names *qfn, int index)
9291a0cd 2984{
7b9f3c50
DE
2985 if (qfn->real_names == NULL)
2986 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2987 qfn->num_file_names, sizeof (char *));
9291a0cd 2988
7b9f3c50
DE
2989 if (qfn->real_names[index] == NULL)
2990 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2991
7b9f3c50 2992 return qfn->real_names[index];
9291a0cd
TT
2993}
2994
2995static struct symtab *
2996dw2_find_last_source_symtab (struct objfile *objfile)
2997{
2998 int index;
ae2de4f8 2999
9291a0cd
TT
3000 dw2_setup (objfile);
3001 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3002 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3003}
3004
7b9f3c50
DE
3005/* Traversal function for dw2_forget_cached_source_info. */
3006
3007static int
3008dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3009{
7b9f3c50 3010 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3011
7b9f3c50 3012 if (file_data->real_names)
9291a0cd 3013 {
7b9f3c50 3014 int i;
9291a0cd 3015
7b9f3c50 3016 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3017 {
7b9f3c50
DE
3018 xfree ((void*) file_data->real_names[i]);
3019 file_data->real_names[i] = NULL;
9291a0cd
TT
3020 }
3021 }
7b9f3c50
DE
3022
3023 return 1;
3024}
3025
3026static void
3027dw2_forget_cached_source_info (struct objfile *objfile)
3028{
3029 dw2_setup (objfile);
3030
3031 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3032 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3033}
3034
f8eba3c6
TT
3035/* Helper function for dw2_map_symtabs_matching_filename that expands
3036 the symtabs and calls the iterator. */
3037
3038static int
3039dw2_map_expand_apply (struct objfile *objfile,
3040 struct dwarf2_per_cu_data *per_cu,
3041 const char *name,
3042 const char *full_path, const char *real_path,
3043 int (*callback) (struct symtab *, void *),
3044 void *data)
3045{
3046 struct symtab *last_made = objfile->symtabs;
3047
3048 /* Don't visit already-expanded CUs. */
3049 if (per_cu->v.quick->symtab)
3050 return 0;
3051
3052 /* This may expand more than one symtab, and we want to iterate over
3053 all of them. */
a0f42c21 3054 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3055
3056 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3057 objfile->symtabs, last_made);
3058}
3059
3060/* Implementation of the map_symtabs_matching_filename method. */
3061
9291a0cd 3062static int
f8eba3c6
TT
3063dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3064 const char *full_path, const char *real_path,
3065 int (*callback) (struct symtab *, void *),
3066 void *data)
9291a0cd
TT
3067{
3068 int i;
c011a4f4 3069 const char *name_basename = lbasename (name);
4aac40c8
TT
3070 int name_len = strlen (name);
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
TT
3096 if (FILENAME_CMP (name, this_name) == 0
3097 || (!is_abs && compare_filenames_for_search (this_name,
3098 name, name_len)))
9291a0cd 3099 {
f8eba3c6
TT
3100 if (dw2_map_expand_apply (objfile, per_cu,
3101 name, full_path, real_path,
3102 callback, data))
3103 return 1;
4aac40c8 3104 }
9291a0cd 3105
c011a4f4
DE
3106 /* Before we invoke realpath, which can get expensive when many
3107 files are involved, do a quick comparison of the basenames. */
3108 if (! basenames_may_differ
3109 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3110 continue;
3111
9291a0cd
TT
3112 if (full_path != NULL)
3113 {
7b9f3c50
DE
3114 const char *this_real_name = dw2_get_real_path (objfile,
3115 file_data, j);
9291a0cd 3116
7b9f3c50 3117 if (this_real_name != NULL
4aac40c8
TT
3118 && (FILENAME_CMP (full_path, this_real_name) == 0
3119 || (!is_abs
3120 && compare_filenames_for_search (this_real_name,
3121 name, name_len))))
9291a0cd 3122 {
f8eba3c6
TT
3123 if (dw2_map_expand_apply (objfile, per_cu,
3124 name, full_path, real_path,
3125 callback, data))
3126 return 1;
9291a0cd
TT
3127 }
3128 }
3129
3130 if (real_path != NULL)
3131 {
7b9f3c50
DE
3132 const char *this_real_name = dw2_get_real_path (objfile,
3133 file_data, j);
9291a0cd 3134
7b9f3c50 3135 if (this_real_name != NULL
4aac40c8
TT
3136 && (FILENAME_CMP (real_path, this_real_name) == 0
3137 || (!is_abs
3138 && compare_filenames_for_search (this_real_name,
3139 name, name_len))))
9291a0cd 3140 {
f8eba3c6
TT
3141 if (dw2_map_expand_apply (objfile, per_cu,
3142 name, full_path, real_path,
3143 callback, data))
3144 return 1;
9291a0cd
TT
3145 }
3146 }
3147 }
3148 }
3149
9291a0cd
TT
3150 return 0;
3151}
3152
3153static struct symtab *
3154dw2_lookup_symbol (struct objfile *objfile, int block_index,
3155 const char *name, domain_enum domain)
3156{
774b6a14 3157 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
3158 instead. */
3159 return NULL;
3160}
3161
3162/* A helper function that expands all symtabs that hold an object
156942c7
DE
3163 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3164 symbols in block BLOCK_KIND. */
2fdf6df6 3165
9291a0cd 3166static void
156942c7
DE
3167dw2_do_expand_symtabs_matching (struct objfile *objfile,
3168 int want_specific_block,
3169 enum block_enum block_kind,
3170 const char *name, domain_enum domain)
9291a0cd 3171{
156942c7
DE
3172 struct mapped_index *index;
3173
9291a0cd
TT
3174 dw2_setup (objfile);
3175
156942c7
DE
3176 index = dwarf2_per_objfile->index_table;
3177
ae2de4f8 3178 /* index_table is NULL if OBJF_READNOW. */
156942c7 3179 if (index)
9291a0cd
TT
3180 {
3181 offset_type *vec;
3182
156942c7 3183 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
3184 {
3185 offset_type i, len = MAYBE_SWAP (*vec);
3186 for (i = 0; i < len; ++i)
3187 {
156942c7
DE
3188 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3189 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 3190 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
3191 int want_static = block_kind != GLOBAL_BLOCK;
3192 /* This value is only valid for index versions >= 7. */
3193 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3194 gdb_index_symbol_kind symbol_kind =
3195 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
83a788b8
DE
3196 /* Only check the symbol attributes if they're present.
3197 Indices prior to version 7 don't record them,
3198 and indices >= 7 may elide them for certain symbols
3199 (gold does this). */
3200 int attrs_valid =
3201 (index->version >= 7
3202 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3203
3204 if (attrs_valid
3205 && want_specific_block
156942c7
DE
3206 && want_static != is_static)
3207 continue;
3208
83a788b8
DE
3209 /* Only check the symbol's kind if it has one. */
3210 if (attrs_valid)
156942c7
DE
3211 {
3212 switch (domain)
3213 {
3214 case VAR_DOMAIN:
3215 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3216 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3217 /* Some types are also in VAR_DOMAIN. */
3218 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3219 continue;
3220 break;
3221 case STRUCT_DOMAIN:
3222 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3223 continue;
3224 break;
3225 case LABEL_DOMAIN:
3226 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3227 continue;
3228 break;
3229 default:
3230 break;
3231 }
3232 }
1fd400ff 3233
a0f42c21 3234 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3235 }
3236 }
3237 }
3238}
3239
774b6a14
TT
3240static void
3241dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 3242 enum block_enum block_kind, const char *name,
774b6a14 3243 domain_enum domain)
9291a0cd 3244{
156942c7 3245 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
3246}
3247
3248static void
3249dw2_print_stats (struct objfile *objfile)
3250{
3251 int i, count;
3252
3253 dw2_setup (objfile);
3254 count = 0;
1fd400ff 3255 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3256 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3257 {
e254ef6a 3258 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3259
e254ef6a 3260 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3261 ++count;
3262 }
3263 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3264}
3265
3266static void
3267dw2_dump (struct objfile *objfile)
3268{
3269 /* Nothing worth printing. */
3270}
3271
3272static void
3273dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3274 struct section_offsets *delta)
3275{
3276 /* There's nothing to relocate here. */
3277}
3278
3279static void
3280dw2_expand_symtabs_for_function (struct objfile *objfile,
3281 const char *func_name)
3282{
156942c7
DE
3283 /* Note: It doesn't matter what we pass for block_kind here. */
3284 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3285 VAR_DOMAIN);
9291a0cd
TT
3286}
3287
3288static void
3289dw2_expand_all_symtabs (struct objfile *objfile)
3290{
3291 int i;
3292
3293 dw2_setup (objfile);
1fd400ff
TT
3294
3295 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3296 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3297 {
e254ef6a 3298 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3299
a0f42c21 3300 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3301 }
3302}
3303
3304static void
3305dw2_expand_symtabs_with_filename (struct objfile *objfile,
3306 const char *filename)
3307{
3308 int i;
3309
3310 dw2_setup (objfile);
d4637a04
DE
3311
3312 /* We don't need to consider type units here.
3313 This is only called for examining code, e.g. expand_line_sal.
3314 There can be an order of magnitude (or more) more type units
3315 than comp units, and we avoid them if we can. */
3316
3317 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3318 {
3319 int j;
e254ef6a 3320 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3321 struct quick_file_names *file_data;
9291a0cd 3322
3d7bb9d9 3323 /* We only need to look at symtabs not already expanded. */
e254ef6a 3324 if (per_cu->v.quick->symtab)
9291a0cd
TT
3325 continue;
3326
7b9f3c50
DE
3327 file_data = dw2_get_file_names (objfile, per_cu);
3328 if (file_data == NULL)
9291a0cd
TT
3329 continue;
3330
7b9f3c50 3331 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3332 {
7b9f3c50 3333 const char *this_name = file_data->file_names[j];
1ef75ecc 3334 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3335 {
a0f42c21 3336 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3337 break;
3338 }
3339 }
3340 }
3341}
3342
356d9f9d
TT
3343/* A helper function for dw2_find_symbol_file that finds the primary
3344 file name for a given CU. This is a die_reader_func. */
3345
3346static void
3347dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3348 gdb_byte *info_ptr,
3349 struct die_info *comp_unit_die,
3350 int has_children,
3351 void *data)
3352{
3353 const char **result_ptr = data;
3354 struct dwarf2_cu *cu = reader->cu;
3355 struct attribute *attr;
3356
3357 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3358 if (attr == NULL)
3359 *result_ptr = NULL;
3360 else
3361 *result_ptr = DW_STRING (attr);
3362}
3363
dd786858 3364static const char *
9291a0cd
TT
3365dw2_find_symbol_file (struct objfile *objfile, const char *name)
3366{
e254ef6a 3367 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3368 offset_type *vec;
356d9f9d 3369 const char *filename;
9291a0cd
TT
3370
3371 dw2_setup (objfile);
3372
ae2de4f8 3373 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3374 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3375 {
3376 struct symtab *s;
3377
d790cf0a
DE
3378 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3379 {
3380 struct blockvector *bv = BLOCKVECTOR (s);
3381 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3382 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3383
3384 if (sym)
210bbc17 3385 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3386 }
96408a79
SA
3387 return NULL;
3388 }
9291a0cd
TT
3389
3390 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3391 name, &vec))
3392 return NULL;
3393
3394 /* Note that this just looks at the very first one named NAME -- but
3395 actually we are looking for a function. find_main_filename
3396 should be rewritten so that it doesn't require a custom hook. It
3397 could just use the ordinary symbol tables. */
3398 /* vec[0] is the length, which must always be >0. */
156942c7 3399 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3400
356d9f9d
TT
3401 if (per_cu->v.quick->symtab != NULL)
3402 return per_cu->v.quick->symtab->filename;
3403
f4dc4d17
DE
3404 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3405 dw2_get_primary_filename_reader, &filename);
9291a0cd 3406
356d9f9d 3407 return filename;
9291a0cd
TT
3408}
3409
3410static void
40658b94
PH
3411dw2_map_matching_symbols (const char * name, domain_enum namespace,
3412 struct objfile *objfile, int global,
3413 int (*callback) (struct block *,
3414 struct symbol *, void *),
2edb89d3
JK
3415 void *data, symbol_compare_ftype *match,
3416 symbol_compare_ftype *ordered_compare)
9291a0cd 3417{
40658b94 3418 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3419 current language is Ada for a non-Ada objfile using GNU index. As Ada
3420 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3421}
3422
3423static void
f8eba3c6
TT
3424dw2_expand_symtabs_matching
3425 (struct objfile *objfile,
3426 int (*file_matcher) (const char *, void *),
e078317b 3427 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3428 enum search_domain kind,
3429 void *data)
9291a0cd
TT
3430{
3431 int i;
3432 offset_type iter;
4b5246aa 3433 struct mapped_index *index;
9291a0cd
TT
3434
3435 dw2_setup (objfile);
ae2de4f8
DE
3436
3437 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3438 if (!dwarf2_per_objfile->index_table)
3439 return;
4b5246aa 3440 index = dwarf2_per_objfile->index_table;
9291a0cd 3441
7b08b9eb 3442 if (file_matcher != NULL)
24c79950
TT
3443 {
3444 struct cleanup *cleanup;
3445 htab_t visited_found, visited_not_found;
3446
f4dc4d17
DE
3447 dw2_build_type_unit_groups ();
3448
24c79950
TT
3449 visited_found = htab_create_alloc (10,
3450 htab_hash_pointer, htab_eq_pointer,
3451 NULL, xcalloc, xfree);
3452 cleanup = make_cleanup_htab_delete (visited_found);
3453 visited_not_found = htab_create_alloc (10,
3454 htab_hash_pointer, htab_eq_pointer,
3455 NULL, xcalloc, xfree);
3456 make_cleanup_htab_delete (visited_not_found);
3457
3458 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3459 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3460 {
3461 int j;
f4dc4d17 3462 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3463 struct quick_file_names *file_data;
3464 void **slot;
7b08b9eb 3465
24c79950 3466 per_cu->v.quick->mark = 0;
3d7bb9d9 3467
24c79950
TT
3468 /* We only need to look at symtabs not already expanded. */
3469 if (per_cu->v.quick->symtab)
3470 continue;
7b08b9eb 3471
24c79950
TT
3472 file_data = dw2_get_file_names (objfile, per_cu);
3473 if (file_data == NULL)
3474 continue;
7b08b9eb 3475
24c79950
TT
3476 if (htab_find (visited_not_found, file_data) != NULL)
3477 continue;
3478 else if (htab_find (visited_found, file_data) != NULL)
3479 {
3480 per_cu->v.quick->mark = 1;
3481 continue;
3482 }
3483
3484 for (j = 0; j < file_data->num_file_names; ++j)
3485 {
3486 if (file_matcher (file_data->file_names[j], data))
3487 {
3488 per_cu->v.quick->mark = 1;
3489 break;
3490 }
3491 }
3492
3493 slot = htab_find_slot (per_cu->v.quick->mark
3494 ? visited_found
3495 : visited_not_found,
3496 file_data, INSERT);
3497 *slot = file_data;
3498 }
3499
3500 do_cleanups (cleanup);
3501 }
9291a0cd 3502
3876f04e 3503 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3504 {
3505 offset_type idx = 2 * iter;
3506 const char *name;
3507 offset_type *vec, vec_len, vec_idx;
3508
3876f04e 3509 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3510 continue;
3511
3876f04e 3512 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3513
e078317b 3514 if (! (*name_matcher) (name, data))
9291a0cd
TT
3515 continue;
3516
3517 /* The name was matched, now expand corresponding CUs that were
3518 marked. */
4b5246aa 3519 vec = (offset_type *) (index->constant_pool
3876f04e 3520 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3521 vec_len = MAYBE_SWAP (vec[0]);
3522 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3523 {
e254ef6a 3524 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3525 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3526 gdb_index_symbol_kind symbol_kind =
3527 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3528 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3529
3530 /* Don't crash on bad data. */
3531 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3532 + dwarf2_per_objfile->n_type_units))
156942c7 3533 continue;
1fd400ff 3534
156942c7
DE
3535 /* Only check the symbol's kind if it has one.
3536 Indices prior to version 7 don't record it. */
3537 if (index->version >= 7)
3538 {
3539 switch (kind)
3540 {
3541 case VARIABLES_DOMAIN:
3542 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3543 continue;
3544 break;
3545 case FUNCTIONS_DOMAIN:
3546 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3547 continue;
3548 break;
3549 case TYPES_DOMAIN:
3550 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3551 continue;
3552 break;
3553 default:
3554 break;
3555 }
3556 }
3557
3558 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3559 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3560 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3561 }
3562 }
3563}
3564
9703b513
TT
3565/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3566 symtab. */
3567
3568static struct symtab *
3569recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3570{
3571 int i;
3572
3573 if (BLOCKVECTOR (symtab) != NULL
3574 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3575 return symtab;
3576
a3ec0bb1
DE
3577 if (symtab->includes == NULL)
3578 return NULL;
3579
9703b513
TT
3580 for (i = 0; symtab->includes[i]; ++i)
3581 {
a3ec0bb1 3582 struct symtab *s = symtab->includes[i];
9703b513
TT
3583
3584 s = recursively_find_pc_sect_symtab (s, pc);
3585 if (s != NULL)
3586 return s;
3587 }
3588
3589 return NULL;
3590}
3591
9291a0cd
TT
3592static struct symtab *
3593dw2_find_pc_sect_symtab (struct objfile *objfile,
3594 struct minimal_symbol *msymbol,
3595 CORE_ADDR pc,
3596 struct obj_section *section,
3597 int warn_if_readin)
3598{
3599 struct dwarf2_per_cu_data *data;
9703b513 3600 struct symtab *result;
9291a0cd
TT
3601
3602 dw2_setup (objfile);
3603
3604 if (!objfile->psymtabs_addrmap)
3605 return NULL;
3606
3607 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3608 if (!data)
3609 return NULL;
3610
3611 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3612 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3613 paddress (get_objfile_arch (objfile), pc));
3614
9703b513
TT
3615 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3616 gdb_assert (result != NULL);
3617 return result;
9291a0cd
TT
3618}
3619
9291a0cd 3620static void
44b13c5a 3621dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3622 void *data, int need_fullname)
9291a0cd
TT
3623{
3624 int i;
24c79950
TT
3625 struct cleanup *cleanup;
3626 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3627 NULL, xcalloc, xfree);
9291a0cd 3628
24c79950 3629 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3630 dw2_setup (objfile);
ae2de4f8 3631
f4dc4d17
DE
3632 dw2_build_type_unit_groups ();
3633
24c79950
TT
3634 /* We can ignore file names coming from already-expanded CUs. */
3635 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3636 + dwarf2_per_objfile->n_type_units); ++i)
3637 {
3638 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3639
3640 if (per_cu->v.quick->symtab)
3641 {
3642 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3643 INSERT);
3644
3645 *slot = per_cu->v.quick->file_names;
3646 }
3647 }
3648
1fd400ff 3649 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3650 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3651 {
3652 int j;
f4dc4d17 3653 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3654 struct quick_file_names *file_data;
24c79950 3655 void **slot;
9291a0cd 3656
3d7bb9d9 3657 /* We only need to look at symtabs not already expanded. */
e254ef6a 3658 if (per_cu->v.quick->symtab)
9291a0cd
TT
3659 continue;
3660
7b9f3c50
DE
3661 file_data = dw2_get_file_names (objfile, per_cu);
3662 if (file_data == NULL)
9291a0cd
TT
3663 continue;
3664
24c79950
TT
3665 slot = htab_find_slot (visited, file_data, INSERT);
3666 if (*slot)
3667 {
3668 /* Already visited. */
3669 continue;
3670 }
3671 *slot = file_data;
3672
7b9f3c50 3673 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3674 {
74e2f255
DE
3675 const char *this_real_name;
3676
3677 if (need_fullname)
3678 this_real_name = dw2_get_real_path (objfile, file_data, j);
3679 else
3680 this_real_name = NULL;
7b9f3c50 3681 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3682 }
3683 }
24c79950
TT
3684
3685 do_cleanups (cleanup);
9291a0cd
TT
3686}
3687
3688static int
3689dw2_has_symbols (struct objfile *objfile)
3690{
3691 return 1;
3692}
3693
3694const struct quick_symbol_functions dwarf2_gdb_index_functions =
3695{
3696 dw2_has_symbols,
3697 dw2_find_last_source_symtab,
3698 dw2_forget_cached_source_info,
f8eba3c6 3699 dw2_map_symtabs_matching_filename,
9291a0cd 3700 dw2_lookup_symbol,
774b6a14 3701 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3702 dw2_print_stats,
3703 dw2_dump,
3704 dw2_relocate,
3705 dw2_expand_symtabs_for_function,
3706 dw2_expand_all_symtabs,
3707 dw2_expand_symtabs_with_filename,
3708 dw2_find_symbol_file,
40658b94 3709 dw2_map_matching_symbols,
9291a0cd
TT
3710 dw2_expand_symtabs_matching,
3711 dw2_find_pc_sect_symtab,
9291a0cd
TT
3712 dw2_map_symbol_filenames
3713};
3714
3715/* Initialize for reading DWARF for this objfile. Return 0 if this
3716 file will use psymtabs, or 1 if using the GNU index. */
3717
3718int
3719dwarf2_initialize_objfile (struct objfile *objfile)
3720{
3721 /* If we're about to read full symbols, don't bother with the
3722 indices. In this case we also don't care if some other debug
3723 format is making psymtabs, because they are all about to be
3724 expanded anyway. */
3725 if ((objfile->flags & OBJF_READNOW))
3726 {
3727 int i;
3728
3729 dwarf2_per_objfile->using_index = 1;
3730 create_all_comp_units (objfile);
0e50663e 3731 create_all_type_units (objfile);
7b9f3c50
DE
3732 dwarf2_per_objfile->quick_file_names_table =
3733 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3734
1fd400ff 3735 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3736 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3737 {
e254ef6a 3738 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3739
e254ef6a
DE
3740 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3741 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3742 }
3743
3744 /* Return 1 so that gdb sees the "quick" functions. However,
3745 these functions will be no-ops because we will have expanded
3746 all symtabs. */
3747 return 1;
3748 }
3749
3750 if (dwarf2_read_index (objfile))
3751 return 1;
3752
9291a0cd
TT
3753 return 0;
3754}
3755
3756\f
3757
dce234bc
PP
3758/* Build a partial symbol table. */
3759
3760void
f29dff0a 3761dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3762{
f29dff0a 3763 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3764 {
3765 init_psymbol_list (objfile, 1024);
3766 }
3767
d146bf1e 3768 dwarf2_build_psymtabs_hard (objfile);
c906108c 3769}
c906108c 3770
1ce1cefd
DE
3771/* Return the total length of the CU described by HEADER. */
3772
3773static unsigned int
3774get_cu_length (const struct comp_unit_head *header)
3775{
3776 return header->initial_length_size + header->length;
3777}
3778
45452591
DE
3779/* Return TRUE if OFFSET is within CU_HEADER. */
3780
3781static inline int
b64f50a1 3782offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3783{
b64f50a1 3784 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3785 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3786
b64f50a1 3787 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3788}
3789
3b80fe9b
DE
3790/* Find the base address of the compilation unit for range lists and
3791 location lists. It will normally be specified by DW_AT_low_pc.
3792 In DWARF-3 draft 4, the base address could be overridden by
3793 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3794 compilation units with discontinuous ranges. */
3795
3796static void
3797dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3798{
3799 struct attribute *attr;
3800
3801 cu->base_known = 0;
3802 cu->base_address = 0;
3803
3804 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3805 if (attr)
3806 {
3807 cu->base_address = DW_ADDR (attr);
3808 cu->base_known = 1;
3809 }
3810 else
3811 {
3812 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3813 if (attr)
3814 {
3815 cu->base_address = DW_ADDR (attr);
3816 cu->base_known = 1;
3817 }
3818 }
3819}
3820
93311388
DE
3821/* Read in the comp unit header information from the debug_info at info_ptr.
3822 NOTE: This leaves members offset, first_die_offset to be filled in
3823 by the caller. */
107d2387 3824
fe1b8b76 3825static gdb_byte *
107d2387 3826read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3827 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3828{
3829 int signed_addr;
891d2f0b 3830 unsigned int bytes_read;
c764a876
DE
3831
3832 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3833 cu_header->initial_length_size = bytes_read;
3834 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3835 info_ptr += bytes_read;
107d2387
AC
3836 cu_header->version = read_2_bytes (abfd, info_ptr);
3837 info_ptr += 2;
b64f50a1
JK
3838 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3839 &bytes_read);
613e1657 3840 info_ptr += bytes_read;
107d2387
AC
3841 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3842 info_ptr += 1;
3843 signed_addr = bfd_get_sign_extend_vma (abfd);
3844 if (signed_addr < 0)
8e65ff28 3845 internal_error (__FILE__, __LINE__,
e2e0b3e5 3846 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3847 cu_header->signed_addr_p = signed_addr;
c764a876 3848
107d2387
AC
3849 return info_ptr;
3850}
3851
36586728
TT
3852/* Helper function that returns the proper abbrev section for
3853 THIS_CU. */
3854
3855static struct dwarf2_section_info *
3856get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3857{
3858 struct dwarf2_section_info *abbrev;
3859
3860 if (this_cu->is_dwz)
3861 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3862 else
3863 abbrev = &dwarf2_per_objfile->abbrev;
3864
3865 return abbrev;
3866}
3867
9ff913ba
DE
3868/* Subroutine of read_and_check_comp_unit_head and
3869 read_and_check_type_unit_head to simplify them.
3870 Perform various error checking on the header. */
3871
3872static void
3873error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3874 struct dwarf2_section_info *section,
3875 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3876{
3877 bfd *abfd = section->asection->owner;
3878 const char *filename = bfd_get_filename (abfd);
3879
3880 if (header->version != 2 && header->version != 3 && header->version != 4)
3881 error (_("Dwarf Error: wrong version in compilation unit header "
3882 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3883 filename);
3884
b64f50a1 3885 if (header->abbrev_offset.sect_off
36586728 3886 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3887 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3888 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3889 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3890 filename);
3891
3892 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3893 avoid potential 32-bit overflow. */
1ce1cefd 3894 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3895 > section->size)
3896 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3897 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3898 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3899 filename);
3900}
3901
3902/* Read in a CU/TU header and perform some basic error checking.
3903 The contents of the header are stored in HEADER.
3904 The result is a pointer to the start of the first DIE. */
adabb602 3905
fe1b8b76 3906static gdb_byte *
9ff913ba
DE
3907read_and_check_comp_unit_head (struct comp_unit_head *header,
3908 struct dwarf2_section_info *section,
4bdcc0c1 3909 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3910 gdb_byte *info_ptr,
3911 int is_debug_types_section)
72bf9492 3912{
fe1b8b76 3913 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3914 bfd *abfd = section->asection->owner;
72bf9492 3915
b64f50a1 3916 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3917
72bf9492
DJ
3918 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3919
460c1c54
CC
3920 /* If we're reading a type unit, skip over the signature and
3921 type_offset fields. */
b0df02fd 3922 if (is_debug_types_section)
460c1c54
CC
3923 info_ptr += 8 /*signature*/ + header->offset_size;
3924
b64f50a1 3925 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3926
4bdcc0c1 3927 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3928
3929 return info_ptr;
3930}
3931
348e048f
DE
3932/* Read in the types comp unit header information from .debug_types entry at
3933 types_ptr. The result is a pointer to one past the end of the header. */
3934
3935static gdb_byte *
9ff913ba
DE
3936read_and_check_type_unit_head (struct comp_unit_head *header,
3937 struct dwarf2_section_info *section,
4bdcc0c1 3938 struct dwarf2_section_info *abbrev_section,
9ff913ba 3939 gdb_byte *info_ptr,
dee91e82
DE
3940 ULONGEST *signature,
3941 cu_offset *type_offset_in_tu)
348e048f 3942{
9ff913ba
DE
3943 gdb_byte *beg_of_comp_unit = info_ptr;
3944 bfd *abfd = section->asection->owner;
348e048f 3945
b64f50a1 3946 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3947
9ff913ba 3948 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3949
9ff913ba
DE
3950 /* If we're reading a type unit, skip over the signature and
3951 type_offset fields. */
3952 if (signature != NULL)
3953 *signature = read_8_bytes (abfd, info_ptr);
3954 info_ptr += 8;
dee91e82
DE
3955 if (type_offset_in_tu != NULL)
3956 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3957 header->offset_size);
9ff913ba
DE
3958 info_ptr += header->offset_size;
3959
b64f50a1 3960 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3961
4bdcc0c1 3962 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3963
3964 return info_ptr;
348e048f
DE
3965}
3966
f4dc4d17
DE
3967/* Fetch the abbreviation table offset from a comp or type unit header. */
3968
3969static sect_offset
3970read_abbrev_offset (struct dwarf2_section_info *section,
3971 sect_offset offset)
3972{
3973 bfd *abfd = section->asection->owner;
3974 gdb_byte *info_ptr;
3975 unsigned int length, initial_length_size, offset_size;
3976 sect_offset abbrev_offset;
3977
3978 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3979 info_ptr = section->buffer + offset.sect_off;
3980 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3981 offset_size = initial_length_size == 4 ? 4 : 8;
3982 info_ptr += initial_length_size + 2 /*version*/;
3983 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3984 return abbrev_offset;
3985}
3986
aaa75496
JB
3987/* Allocate a new partial symtab for file named NAME and mark this new
3988 partial symtab as being an include of PST. */
3989
3990static void
3991dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3992 struct objfile *objfile)
3993{
3994 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3995
3996 subpst->section_offsets = pst->section_offsets;
3997 subpst->textlow = 0;
3998 subpst->texthigh = 0;
3999
4000 subpst->dependencies = (struct partial_symtab **)
4001 obstack_alloc (&objfile->objfile_obstack,
4002 sizeof (struct partial_symtab *));
4003 subpst->dependencies[0] = pst;
4004 subpst->number_of_dependencies = 1;
4005
4006 subpst->globals_offset = 0;
4007 subpst->n_global_syms = 0;
4008 subpst->statics_offset = 0;
4009 subpst->n_static_syms = 0;
4010 subpst->symtab = NULL;
4011 subpst->read_symtab = pst->read_symtab;
4012 subpst->readin = 0;
4013
4014 /* No private part is necessary for include psymtabs. This property
4015 can be used to differentiate between such include psymtabs and
10b3939b 4016 the regular ones. */
58a9656e 4017 subpst->read_symtab_private = NULL;
aaa75496
JB
4018}
4019
4020/* Read the Line Number Program data and extract the list of files
4021 included by the source file represented by PST. Build an include
d85a05f0 4022 partial symtab for each of these included files. */
aaa75496
JB
4023
4024static void
4025dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4026 struct die_info *die,
4027 struct partial_symtab *pst)
aaa75496 4028{
d85a05f0
DJ
4029 struct line_header *lh = NULL;
4030 struct attribute *attr;
aaa75496 4031
d85a05f0
DJ
4032 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4033 if (attr)
3019eac3 4034 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4035 if (lh == NULL)
4036 return; /* No linetable, so no includes. */
4037
c6da4cef 4038 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4039 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4040
4041 free_line_header (lh);
4042}
4043
348e048f 4044static hashval_t
52dc124a 4045hash_signatured_type (const void *item)
348e048f 4046{
52dc124a 4047 const struct signatured_type *sig_type = item;
9a619af0 4048
348e048f 4049 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4050 return sig_type->signature;
348e048f
DE
4051}
4052
4053static int
52dc124a 4054eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4055{
4056 const struct signatured_type *lhs = item_lhs;
4057 const struct signatured_type *rhs = item_rhs;
9a619af0 4058
348e048f
DE
4059 return lhs->signature == rhs->signature;
4060}
4061
1fd400ff
TT
4062/* Allocate a hash table for signatured types. */
4063
4064static htab_t
673bfd45 4065allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4066{
4067 return htab_create_alloc_ex (41,
52dc124a
DE
4068 hash_signatured_type,
4069 eq_signatured_type,
1fd400ff
TT
4070 NULL,
4071 &objfile->objfile_obstack,
4072 hashtab_obstack_allocate,
4073 dummy_obstack_deallocate);
4074}
4075
d467dd73 4076/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4077
4078static int
d467dd73 4079add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4080{
4081 struct signatured_type *sigt = *slot;
b4dd5633 4082 struct signatured_type ***datap = datum;
1fd400ff 4083
b4dd5633 4084 **datap = sigt;
1fd400ff
TT
4085 ++*datap;
4086
4087 return 1;
4088}
4089
3019eac3 4090/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4091 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4092 NULL otherwise.
4093 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4094 The result is a pointer to the hash table or NULL if there are
4095 no types. */
348e048f 4096
3019eac3
DE
4097static htab_t
4098create_debug_types_hash_table (struct dwo_file *dwo_file,
4099 VEC (dwarf2_section_info_def) *types)
348e048f 4100{
3019eac3 4101 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4102 htab_t types_htab = NULL;
8b70b953
TT
4103 int ix;
4104 struct dwarf2_section_info *section;
4bdcc0c1 4105 struct dwarf2_section_info *abbrev_section;
348e048f 4106
3019eac3
DE
4107 if (VEC_empty (dwarf2_section_info_def, types))
4108 return NULL;
348e048f 4109
4bdcc0c1
DE
4110 abbrev_section = (dwo_file != NULL
4111 ? &dwo_file->sections.abbrev
4112 : &dwarf2_per_objfile->abbrev);
4113
09406207
DE
4114 if (dwarf2_read_debug)
4115 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4116 dwo_file ? ".dwo" : "",
4117 bfd_get_filename (abbrev_section->asection->owner));
4118
8b70b953 4119 for (ix = 0;
3019eac3 4120 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4121 ++ix)
4122 {
3019eac3 4123 bfd *abfd;
8b70b953 4124 gdb_byte *info_ptr, *end_ptr;
36586728 4125 struct dwarf2_section_info *abbrev_section;
348e048f 4126
8b70b953
TT
4127 dwarf2_read_section (objfile, section);
4128 info_ptr = section->buffer;
348e048f 4129
8b70b953
TT
4130 if (info_ptr == NULL)
4131 continue;
348e048f 4132
3019eac3
DE
4133 /* We can't set abfd until now because the section may be empty or
4134 not present, in which case section->asection will be NULL. */
4135 abfd = section->asection->owner;
4136
36586728
TT
4137 if (dwo_file)
4138 abbrev_section = &dwo_file->sections.abbrev;
4139 else
4140 abbrev_section = &dwarf2_per_objfile->abbrev;
4141
8b70b953 4142 if (types_htab == NULL)
3019eac3
DE
4143 {
4144 if (dwo_file)
4145 types_htab = allocate_dwo_unit_table (objfile);
4146 else
4147 types_htab = allocate_signatured_type_table (objfile);
4148 }
348e048f 4149
dee91e82
DE
4150 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4151 because we don't need to read any dies: the signature is in the
4152 header. */
8b70b953
TT
4153
4154 end_ptr = info_ptr + section->size;
4155 while (info_ptr < end_ptr)
4156 {
b64f50a1 4157 sect_offset offset;
3019eac3 4158 cu_offset type_offset_in_tu;
8b70b953 4159 ULONGEST signature;
52dc124a 4160 struct signatured_type *sig_type;
3019eac3 4161 struct dwo_unit *dwo_tu;
8b70b953
TT
4162 void **slot;
4163 gdb_byte *ptr = info_ptr;
9ff913ba 4164 struct comp_unit_head header;
dee91e82 4165 unsigned int length;
348e048f 4166
b64f50a1 4167 offset.sect_off = ptr - section->buffer;
348e048f 4168
8b70b953 4169 /* We need to read the type's signature in order to build the hash
9ff913ba 4170 table, but we don't need anything else just yet. */
348e048f 4171
4bdcc0c1
DE
4172 ptr = read_and_check_type_unit_head (&header, section,
4173 abbrev_section, ptr,
3019eac3 4174 &signature, &type_offset_in_tu);
6caca83c 4175
1ce1cefd 4176 length = get_cu_length (&header);
dee91e82 4177
6caca83c 4178 /* Skip dummy type units. */
dee91e82
DE
4179 if (ptr >= info_ptr + length
4180 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4181 {
1ce1cefd 4182 info_ptr += length;
6caca83c
CC
4183 continue;
4184 }
8b70b953 4185
3019eac3
DE
4186 if (dwo_file)
4187 {
4188 sig_type = NULL;
4189 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4190 struct dwo_unit);
4191 dwo_tu->dwo_file = dwo_file;
4192 dwo_tu->signature = signature;
4193 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4194 dwo_tu->info_or_types_section = section;
4195 dwo_tu->offset = offset;
4196 dwo_tu->length = length;
4197 }
4198 else
4199 {
4200 /* N.B.: type_offset is not usable if this type uses a DWO file.
4201 The real type_offset is in the DWO file. */
4202 dwo_tu = NULL;
4203 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4204 struct signatured_type);
4205 sig_type->signature = signature;
4206 sig_type->type_offset_in_tu = type_offset_in_tu;
4207 sig_type->per_cu.objfile = objfile;
4208 sig_type->per_cu.is_debug_types = 1;
4209 sig_type->per_cu.info_or_types_section = section;
4210 sig_type->per_cu.offset = offset;
4211 sig_type->per_cu.length = length;
4212 }
8b70b953 4213
3019eac3
DE
4214 slot = htab_find_slot (types_htab,
4215 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4216 INSERT);
8b70b953
TT
4217 gdb_assert (slot != NULL);
4218 if (*slot != NULL)
4219 {
3019eac3
DE
4220 sect_offset dup_offset;
4221
4222 if (dwo_file)
4223 {
4224 const struct dwo_unit *dup_tu = *slot;
4225
4226 dup_offset = dup_tu->offset;
4227 }
4228 else
4229 {
4230 const struct signatured_type *dup_tu = *slot;
4231
4232 dup_offset = dup_tu->per_cu.offset;
4233 }
b3c8eb43 4234
8b70b953
TT
4235 complaint (&symfile_complaints,
4236 _("debug type entry at offset 0x%x is duplicate to the "
4237 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4238 offset.sect_off, dup_offset.sect_off,
8b70b953 4239 phex (signature, sizeof (signature)));
8b70b953 4240 }
3019eac3 4241 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4242
09406207 4243 if (dwarf2_read_debug)
8b70b953 4244 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4245 offset.sect_off,
4246 phex (signature, sizeof (signature)));
348e048f 4247
dee91e82 4248 info_ptr += length;
8b70b953 4249 }
348e048f
DE
4250 }
4251
3019eac3
DE
4252 return types_htab;
4253}
4254
4255/* Create the hash table of all entries in the .debug_types section,
4256 and initialize all_type_units.
4257 The result is zero if there is an error (e.g. missing .debug_types section),
4258 otherwise non-zero. */
4259
4260static int
4261create_all_type_units (struct objfile *objfile)
4262{
4263 htab_t types_htab;
b4dd5633 4264 struct signatured_type **iter;
3019eac3
DE
4265
4266 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4267 if (types_htab == NULL)
4268 {
4269 dwarf2_per_objfile->signatured_types = NULL;
4270 return 0;
4271 }
4272
348e048f
DE
4273 dwarf2_per_objfile->signatured_types = types_htab;
4274
d467dd73
DE
4275 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4276 dwarf2_per_objfile->all_type_units
1fd400ff 4277 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4278 dwarf2_per_objfile->n_type_units
b4dd5633 4279 * sizeof (struct signatured_type *));
d467dd73
DE
4280 iter = &dwarf2_per_objfile->all_type_units[0];
4281 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4282 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4283 == dwarf2_per_objfile->n_type_units);
1fd400ff 4284
348e048f
DE
4285 return 1;
4286}
4287
380bca97 4288/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4289 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4290
4291static struct signatured_type *
e319fa28 4292lookup_signatured_type (ULONGEST sig)
348e048f
DE
4293{
4294 struct signatured_type find_entry, *entry;
4295
4296 if (dwarf2_per_objfile->signatured_types == NULL)
4297 {
4298 complaint (&symfile_complaints,
55f1336d 4299 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4300 return NULL;
348e048f
DE
4301 }
4302
4303 find_entry.signature = sig;
4304 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4305 return entry;
4306}
42e7ad6c
DE
4307\f
4308/* Low level DIE reading support. */
348e048f 4309
d85a05f0
DJ
4310/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4311
4312static void
4313init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4314 struct dwarf2_cu *cu,
3019eac3
DE
4315 struct dwarf2_section_info *section,
4316 struct dwo_file *dwo_file)
d85a05f0 4317{
fceca515 4318 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4319 reader->abfd = section->asection->owner;
d85a05f0 4320 reader->cu = cu;
3019eac3 4321 reader->dwo_file = dwo_file;
dee91e82
DE
4322 reader->die_section = section;
4323 reader->buffer = section->buffer;
f664829e 4324 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4325}
4326
fd820528 4327/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4328 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4329
f4dc4d17
DE
4330 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4331 Otherwise the table specified in the comp unit header is read in and used.
4332 This is an optimization for when we already have the abbrev table.
4333
dee91e82
DE
4334 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4335 Otherwise, a new CU is allocated with xmalloc.
4336
4337 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4338 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4339
4340 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4341 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4342
70221824 4343static void
fd820528 4344init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4345 struct abbrev_table *abbrev_table,
fd820528
DE
4346 int use_existing_cu, int keep,
4347 die_reader_func_ftype *die_reader_func,
4348 void *data)
c906108c 4349{
dee91e82 4350 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4351 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4352 bfd *abfd = section->asection->owner;
dee91e82
DE
4353 struct dwarf2_cu *cu;
4354 gdb_byte *begin_info_ptr, *info_ptr;
4355 struct die_reader_specs reader;
d85a05f0 4356 struct die_info *comp_unit_die;
dee91e82 4357 int has_children;
d85a05f0 4358 struct attribute *attr;
dee91e82
DE
4359 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4360 struct signatured_type *sig_type = NULL;
4bdcc0c1 4361 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4362 /* Non-zero if CU currently points to a DWO file and we need to
4363 reread it. When this happens we need to reread the skeleton die
4364 before we can reread the DWO file. */
4365 int rereading_dwo_cu = 0;
c906108c 4366
09406207
DE
4367 if (dwarf2_die_debug)
4368 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4369 this_cu->is_debug_types ? "type" : "comp",
4370 this_cu->offset.sect_off);
4371
dee91e82
DE
4372 if (use_existing_cu)
4373 gdb_assert (keep);
23745b47 4374
dee91e82
DE
4375 cleanups = make_cleanup (null_cleanup, NULL);
4376
4377 /* This is cheap if the section is already read in. */
4378 dwarf2_read_section (objfile, section);
4379
4380 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4381
4382 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4383
4384 if (use_existing_cu && this_cu->cu != NULL)
4385 {
4386 cu = this_cu->cu;
42e7ad6c
DE
4387
4388 /* If this CU is from a DWO file we need to start over, we need to
4389 refetch the attributes from the skeleton CU.
4390 This could be optimized by retrieving those attributes from when we
4391 were here the first time: the previous comp_unit_die was stored in
4392 comp_unit_obstack. But there's no data yet that we need this
4393 optimization. */
4394 if (cu->dwo_unit != NULL)
4395 rereading_dwo_cu = 1;
dee91e82
DE
4396 }
4397 else
4398 {
4399 /* If !use_existing_cu, this_cu->cu must be NULL. */
4400 gdb_assert (this_cu->cu == NULL);
4401
4402 cu = xmalloc (sizeof (*cu));
4403 init_one_comp_unit (cu, this_cu);
4404
4405 /* If an error occurs while loading, release our storage. */
4406 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4407 }
dee91e82 4408
42e7ad6c
DE
4409 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4410 {
4411 /* We already have the header, there's no need to read it in again. */
4412 info_ptr += cu->header.first_die_offset.cu_off;
4413 }
4414 else
4415 {
3019eac3 4416 if (this_cu->is_debug_types)
dee91e82
DE
4417 {
4418 ULONGEST signature;
42e7ad6c 4419 cu_offset type_offset_in_tu;
dee91e82 4420
4bdcc0c1
DE
4421 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4422 abbrev_section, info_ptr,
42e7ad6c
DE
4423 &signature,
4424 &type_offset_in_tu);
dee91e82 4425
42e7ad6c
DE
4426 /* Since per_cu is the first member of struct signatured_type,
4427 we can go from a pointer to one to a pointer to the other. */
4428 sig_type = (struct signatured_type *) this_cu;
4429 gdb_assert (sig_type->signature == signature);
4430 gdb_assert (sig_type->type_offset_in_tu.cu_off
4431 == type_offset_in_tu.cu_off);
dee91e82
DE
4432 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4433
42e7ad6c
DE
4434 /* LENGTH has not been set yet for type units if we're
4435 using .gdb_index. */
1ce1cefd 4436 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4437
4438 /* Establish the type offset that can be used to lookup the type. */
4439 sig_type->type_offset_in_section.sect_off =
4440 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4441 }
4442 else
4443 {
4bdcc0c1
DE
4444 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4445 abbrev_section,
4446 info_ptr, 0);
dee91e82
DE
4447
4448 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4449 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4450 }
4451 }
10b3939b 4452
6caca83c 4453 /* Skip dummy compilation units. */
dee91e82 4454 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4455 || peek_abbrev_code (abfd, info_ptr) == 0)
4456 {
dee91e82 4457 do_cleanups (cleanups);
21b2bd31 4458 return;
6caca83c
CC
4459 }
4460
433df2d4
DE
4461 /* If we don't have them yet, read the abbrevs for this compilation unit.
4462 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4463 done. Note that it's important that if the CU had an abbrev table
4464 on entry we don't free it when we're done: Somewhere up the call stack
4465 it may be in use. */
f4dc4d17
DE
4466 if (abbrev_table != NULL)
4467 {
4468 gdb_assert (cu->abbrev_table == NULL);
4469 gdb_assert (cu->header.abbrev_offset.sect_off
4470 == abbrev_table->offset.sect_off);
4471 cu->abbrev_table = abbrev_table;
4472 }
4473 else if (cu->abbrev_table == NULL)
dee91e82 4474 {
4bdcc0c1 4475 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4476 make_cleanup (dwarf2_free_abbrev_table, cu);
4477 }
42e7ad6c
DE
4478 else if (rereading_dwo_cu)
4479 {
4480 dwarf2_free_abbrev_table (cu);
4481 dwarf2_read_abbrevs (cu, abbrev_section);
4482 }
af703f96 4483
dee91e82 4484 /* Read the top level CU/TU die. */
3019eac3 4485 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4486 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4487
3019eac3
DE
4488 /* If we have a DWO stub, process it and then read in the DWO file.
4489 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4490 a DWO CU, that this test will fail. */
4491 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4492 if (attr)
4493 {
4494 char *dwo_name = DW_STRING (attr);
42e7ad6c 4495 const char *comp_dir_string;
3019eac3
DE
4496 struct dwo_unit *dwo_unit;
4497 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4498 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4499 int i,num_extra_attrs;
4bdcc0c1 4500 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4501
4502 if (has_children)
4503 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4504 " has children (offset 0x%x) [in module %s]"),
4505 this_cu->offset.sect_off, bfd_get_filename (abfd));
4506
4507 /* These attributes aren't processed until later:
4508 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4509 However, the attribute is found in the stub which we won't have later.
4510 In order to not impose this complication on the rest of the code,
4511 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4512
4513 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4514 DWO file. */
42e7ad6c 4515 stmt_list = NULL;
3019eac3
DE
4516 if (! this_cu->is_debug_types)
4517 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4518 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4519 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4520 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4521 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4522
4523 /* There should be a DW_AT_addr_base attribute here (if needed).
4524 We need the value before we can process DW_FORM_GNU_addr_index. */
4525 cu->addr_base = 0;
3019eac3
DE
4526 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4527 if (attr)
2e3cf129
DE
4528 cu->addr_base = DW_UNSND (attr);
4529
4530 /* There should be a DW_AT_ranges_base attribute here (if needed).
4531 We need the value before we can process DW_AT_ranges. */
4532 cu->ranges_base = 0;
4533 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4534 if (attr)
4535 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4536
4537 if (this_cu->is_debug_types)
4538 {
4539 gdb_assert (sig_type != NULL);
4540 signature = sig_type->signature;
4541 }
4542 else
4543 {
4544 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4545 if (! attr)
4546 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4547 dwo_name);
4548 signature = DW_UNSND (attr);
4549 }
4550
4551 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4552 comp_dir_string = NULL;
4553 if (comp_dir)
4554 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4555
4556 if (this_cu->is_debug_types)
42e7ad6c 4557 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4558 else
42e7ad6c 4559 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4560 signature);
4561
4562 if (dwo_unit == NULL)
4563 {
4564 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4565 " with ID %s [in module %s]"),
4566 this_cu->offset.sect_off,
4567 phex (signature, sizeof (signature)),
4568 objfile->name);
4569 }
4570
4571 /* Set up for reading the DWO CU/TU. */
4572 cu->dwo_unit = dwo_unit;
4573 section = dwo_unit->info_or_types_section;
80626a55 4574 dwarf2_read_section (objfile, section);
3019eac3 4575 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4576 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4577 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4578
4579 if (this_cu->is_debug_types)
4580 {
4581 ULONGEST signature;
80626a55 4582 cu_offset type_offset_in_tu;
3019eac3 4583
4bdcc0c1
DE
4584 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4585 dwo_abbrev_section,
4586 info_ptr,
80626a55
DE
4587 &signature,
4588 &type_offset_in_tu);
3019eac3
DE
4589 gdb_assert (sig_type->signature == signature);
4590 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4591 /* For DWOs coming from DWP files, we don't know the CU length
4592 nor the type's offset in the TU until now. */
4593 dwo_unit->length = get_cu_length (&cu->header);
4594 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4595
4596 /* Establish the type offset that can be used to lookup the type.
4597 For DWO files, we don't know it until now. */
4598 sig_type->type_offset_in_section.sect_off =
4599 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4600 }
4601 else
4602 {
4bdcc0c1
DE
4603 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4604 dwo_abbrev_section,
4605 info_ptr, 0);
3019eac3 4606 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4607 /* For DWOs coming from DWP files, we don't know the CU length
4608 until now. */
4609 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4610 }
4611
4612 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4613 if (abbrev_table == NULL)
4614 {
4615 dwarf2_free_abbrev_table (cu);
4616 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4617 }
4618 else
4619 {
4620 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4621 make_cleanup (dwarf2_free_abbrev_table, cu);
4622 }
3019eac3
DE
4623
4624 /* Read in the die, but leave space to copy over the attributes
4625 from the stub. This has the benefit of simplifying the rest of
4626 the code - all the real work is done here. */
4627 num_extra_attrs = ((stmt_list != NULL)
4628 + (low_pc != NULL)
4629 + (high_pc != NULL)
42e7ad6c
DE
4630 + (ranges != NULL)
4631 + (comp_dir != NULL));
3019eac3
DE
4632 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4633 &has_children, num_extra_attrs);
4634
4635 /* Copy over the attributes from the stub to the DWO die. */
4636 i = comp_unit_die->num_attrs;
4637 if (stmt_list != NULL)
4638 comp_unit_die->attrs[i++] = *stmt_list;
4639 if (low_pc != NULL)
4640 comp_unit_die->attrs[i++] = *low_pc;
4641 if (high_pc != NULL)
4642 comp_unit_die->attrs[i++] = *high_pc;
4643 if (ranges != NULL)
4644 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4645 if (comp_dir != NULL)
4646 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4647 comp_unit_die->num_attrs += num_extra_attrs;
4648
4649 /* Skip dummy compilation units. */
4650 if (info_ptr >= begin_info_ptr + dwo_unit->length
4651 || peek_abbrev_code (abfd, info_ptr) == 0)
4652 {
4653 do_cleanups (cleanups);
4654 return;
4655 }
4656 }
4657
dee91e82
DE
4658 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4659
4660 if (free_cu_cleanup != NULL)
348e048f 4661 {
dee91e82
DE
4662 if (keep)
4663 {
4664 /* We've successfully allocated this compilation unit. Let our
4665 caller clean it up when finished with it. */
4666 discard_cleanups (free_cu_cleanup);
4667
4668 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4669 So we have to manually free the abbrev table. */
4670 dwarf2_free_abbrev_table (cu);
4671
4672 /* Link this CU into read_in_chain. */
4673 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4674 dwarf2_per_objfile->read_in_chain = this_cu;
4675 }
4676 else
4677 do_cleanups (free_cu_cleanup);
348e048f 4678 }
dee91e82
DE
4679
4680 do_cleanups (cleanups);
4681}
4682
3019eac3
DE
4683/* Read CU/TU THIS_CU in section SECTION,
4684 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4685 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4686 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4687
4688 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4689 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4690
4691 We fill in THIS_CU->length.
4692
4693 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4694 linker) then DIE_READER_FUNC will not get called.
4695
4696 THIS_CU->cu is always freed when done.
3019eac3
DE
4697 This is done in order to not leave THIS_CU->cu in a state where we have
4698 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4699
4700static void
4701init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4702 struct dwarf2_section_info *abbrev_section,
3019eac3 4703 struct dwo_file *dwo_file,
dee91e82
DE
4704 die_reader_func_ftype *die_reader_func,
4705 void *data)
4706{
4707 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4708 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4709 bfd *abfd = section->asection->owner;
dee91e82
DE
4710 struct dwarf2_cu cu;
4711 gdb_byte *begin_info_ptr, *info_ptr;
4712 struct die_reader_specs reader;
4713 struct cleanup *cleanups;
4714 struct die_info *comp_unit_die;
4715 int has_children;
4716
09406207
DE
4717 if (dwarf2_die_debug)
4718 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4719 this_cu->is_debug_types ? "type" : "comp",
4720 this_cu->offset.sect_off);
4721
dee91e82
DE
4722 gdb_assert (this_cu->cu == NULL);
4723
dee91e82
DE
4724 /* This is cheap if the section is already read in. */
4725 dwarf2_read_section (objfile, section);
4726
4727 init_one_comp_unit (&cu, this_cu);
4728
4729 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4730
4731 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4732 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4733 abbrev_section, info_ptr,
3019eac3 4734 this_cu->is_debug_types);
dee91e82 4735
1ce1cefd 4736 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4737
4738 /* Skip dummy compilation units. */
4739 if (info_ptr >= begin_info_ptr + this_cu->length
4740 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4741 {
dee91e82 4742 do_cleanups (cleanups);
21b2bd31 4743 return;
93311388 4744 }
72bf9492 4745
dee91e82
DE
4746 dwarf2_read_abbrevs (&cu, abbrev_section);
4747 make_cleanup (dwarf2_free_abbrev_table, &cu);
4748
3019eac3 4749 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4750 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4751
4752 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4753
4754 do_cleanups (cleanups);
4755}
4756
3019eac3
DE
4757/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4758 does not lookup the specified DWO file.
4759 This cannot be used to read DWO files.
dee91e82
DE
4760
4761 THIS_CU->cu is always freed when done.
3019eac3
DE
4762 This is done in order to not leave THIS_CU->cu in a state where we have
4763 to care whether it refers to the "main" CU or the DWO CU.
4764 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4765
4766static void
4767init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4768 die_reader_func_ftype *die_reader_func,
4769 void *data)
4770{
4771 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4772 get_abbrev_section_for_cu (this_cu),
3019eac3 4773 NULL,
dee91e82
DE
4774 die_reader_func, data);
4775}
4776
f4dc4d17
DE
4777/* Create a psymtab named NAME and assign it to PER_CU.
4778
4779 The caller must fill in the following details:
4780 dirname, textlow, texthigh. */
4781
4782static struct partial_symtab *
4783create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4784{
4785 struct objfile *objfile = per_cu->objfile;
4786 struct partial_symtab *pst;
4787
4788 pst = start_psymtab_common (objfile, objfile->section_offsets,
4789 name, 0,
4790 objfile->global_psymbols.next,
4791 objfile->static_psymbols.next);
4792
4793 pst->psymtabs_addrmap_supported = 1;
4794
4795 /* This is the glue that links PST into GDB's symbol API. */
4796 pst->read_symtab_private = per_cu;
4797 pst->read_symtab = dwarf2_psymtab_to_symtab;
4798 per_cu->v.psymtab = pst;
4799
4800 return pst;
4801}
4802
dee91e82
DE
4803/* die_reader_func for process_psymtab_comp_unit. */
4804
4805static void
4806process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4807 gdb_byte *info_ptr,
4808 struct die_info *comp_unit_die,
4809 int has_children,
4810 void *data)
4811{
4812 struct dwarf2_cu *cu = reader->cu;
4813 struct objfile *objfile = cu->objfile;
4814 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4815 struct attribute *attr;
4816 CORE_ADDR baseaddr;
4817 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4818 struct partial_symtab *pst;
4819 int has_pc_info;
4820 const char *filename;
95554aad 4821 int *want_partial_unit_ptr = data;
dee91e82 4822
95554aad
TT
4823 if (comp_unit_die->tag == DW_TAG_partial_unit
4824 && (want_partial_unit_ptr == NULL
4825 || !*want_partial_unit_ptr))
dee91e82
DE
4826 return;
4827
f4dc4d17
DE
4828 gdb_assert (! per_cu->is_debug_types);
4829
95554aad 4830 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4831
4832 cu->list_in_scope = &file_symbols;
c906108c 4833
93311388 4834 /* Allocate a new partial symbol table structure. */
dee91e82 4835 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4836 if (attr == NULL || !DW_STRING (attr))
4837 filename = "";
4838 else
4839 filename = DW_STRING (attr);
72bf9492 4840
f4dc4d17
DE
4841 pst = create_partial_symtab (per_cu, filename);
4842
4843 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4844 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4845 if (attr != NULL)
4846 pst->dirname = DW_STRING (attr);
72bf9492 4847
93311388 4848 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4849
dee91e82 4850 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4851
93311388
DE
4852 /* Possibly set the default values of LOWPC and HIGHPC from
4853 `DW_AT_ranges'. */
d85a05f0 4854 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4855 &best_highpc, cu, pst);
d85a05f0 4856 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4857 /* Store the contiguous range if it is not empty; it can be empty for
4858 CUs with no code. */
4859 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4860 best_lowpc + baseaddr,
4861 best_highpc + baseaddr - 1, pst);
93311388
DE
4862
4863 /* Check if comp unit has_children.
4864 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4865 If not, there's no more debug_info for this comp unit. */
d85a05f0 4866 if (has_children)
93311388
DE
4867 {
4868 struct partial_die_info *first_die;
4869 CORE_ADDR lowpc, highpc;
31ffec48 4870
93311388
DE
4871 lowpc = ((CORE_ADDR) -1);
4872 highpc = ((CORE_ADDR) 0);
c906108c 4873
dee91e82 4874 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4875
93311388 4876 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4877 ! has_pc_info, cu);
57c22c6c 4878
93311388
DE
4879 /* If we didn't find a lowpc, set it to highpc to avoid
4880 complaints from `maint check'. */
4881 if (lowpc == ((CORE_ADDR) -1))
4882 lowpc = highpc;
10b3939b 4883
93311388
DE
4884 /* If the compilation unit didn't have an explicit address range,
4885 then use the information extracted from its child dies. */
d85a05f0 4886 if (! has_pc_info)
93311388 4887 {
d85a05f0
DJ
4888 best_lowpc = lowpc;
4889 best_highpc = highpc;
93311388
DE
4890 }
4891 }
d85a05f0
DJ
4892 pst->textlow = best_lowpc + baseaddr;
4893 pst->texthigh = best_highpc + baseaddr;
c906108c 4894
93311388
DE
4895 pst->n_global_syms = objfile->global_psymbols.next -
4896 (objfile->global_psymbols.list + pst->globals_offset);
4897 pst->n_static_syms = objfile->static_psymbols.next -
4898 (objfile->static_psymbols.list + pst->statics_offset);
4899 sort_pst_symbols (pst);
c906108c 4900
f4dc4d17 4901 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4902 {
4903 int i;
f4dc4d17 4904 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4905 struct dwarf2_per_cu_data *iter;
4906
4907 /* Fill in 'dependencies' here; we fill in 'users' in a
4908 post-pass. */
4909 pst->number_of_dependencies = len;
4910 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4911 len * sizeof (struct symtab *));
4912 for (i = 0;
f4dc4d17 4913 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4914 i, iter);
4915 ++i)
4916 pst->dependencies[i] = iter->v.psymtab;
4917
f4dc4d17 4918 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4919 }
4920
f4dc4d17
DE
4921 /* Get the list of files included in the current compilation unit,
4922 and build a psymtab for each of them. */
4923 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4924
4925 if (dwarf2_read_debug)
4926 {
4927 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4928
4929 fprintf_unfiltered (gdb_stdlog,
844226d6 4930 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4931 ", %d global, %d static syms\n",
4932 per_cu->is_debug_types ? "type" : "comp",
4933 per_cu->offset.sect_off,
4934 paddress (gdbarch, pst->textlow),
4935 paddress (gdbarch, pst->texthigh),
4936 pst->n_global_syms, pst->n_static_syms);
4937 }
dee91e82 4938}
ae038cb0 4939
dee91e82
DE
4940/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4941 Process compilation unit THIS_CU for a psymtab. */
4942
4943static void
95554aad
TT
4944process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4945 int want_partial_unit)
dee91e82
DE
4946{
4947 /* If this compilation unit was already read in, free the
4948 cached copy in order to read it in again. This is
4949 necessary because we skipped some symbols when we first
4950 read in the compilation unit (see load_partial_dies).
4951 This problem could be avoided, but the benefit is unclear. */
4952 if (this_cu->cu != NULL)
4953 free_one_cached_comp_unit (this_cu);
4954
3019eac3 4955 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
4956 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4957 process_psymtab_comp_unit_reader,
95554aad 4958 &want_partial_unit);
dee91e82
DE
4959
4960 /* Age out any secondary CUs. */
4961 age_cached_comp_units ();
93311388 4962}
ff013f42 4963
f4dc4d17
DE
4964static hashval_t
4965hash_type_unit_group (const void *item)
4966{
094b34ac 4967 const struct type_unit_group *tu_group = item;
f4dc4d17 4968
094b34ac 4969 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4970}
348e048f
DE
4971
4972static int
f4dc4d17 4973eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4974{
f4dc4d17
DE
4975 const struct type_unit_group *lhs = item_lhs;
4976 const struct type_unit_group *rhs = item_rhs;
348e048f 4977
094b34ac 4978 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4979}
348e048f 4980
f4dc4d17
DE
4981/* Allocate a hash table for type unit groups. */
4982
4983static htab_t
4984allocate_type_unit_groups_table (void)
4985{
4986 return htab_create_alloc_ex (3,
4987 hash_type_unit_group,
4988 eq_type_unit_group,
4989 NULL,
4990 &dwarf2_per_objfile->objfile->objfile_obstack,
4991 hashtab_obstack_allocate,
4992 dummy_obstack_deallocate);
4993}
dee91e82 4994
f4dc4d17
DE
4995/* Type units that don't have DW_AT_stmt_list are grouped into their own
4996 partial symtabs. We combine several TUs per psymtab to not let the size
4997 of any one psymtab grow too big. */
4998#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4999#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5000
094b34ac 5001/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5002 Create the type_unit_group object used to hold one or more TUs. */
5003
5004static struct type_unit_group *
094b34ac 5005create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5006{
5007 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5008 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5009 struct type_unit_group *tu_group;
f4dc4d17
DE
5010
5011 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5012 struct type_unit_group);
094b34ac 5013 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5014 per_cu->objfile = objfile;
5015 per_cu->is_debug_types = 1;
5016 per_cu->s.type_unit_group = tu_group;
5017
094b34ac
DE
5018 if (dwarf2_per_objfile->using_index)
5019 {
5020 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5021 struct dwarf2_per_cu_quick_data);
5022 tu_group->t.first_tu = cu->per_cu;
5023 }
5024 else
5025 {
5026 unsigned int line_offset = line_offset_struct.sect_off;
5027 struct partial_symtab *pst;
5028 char *name;
5029
5030 /* Give the symtab a useful name for debug purposes. */
5031 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5032 name = xstrprintf ("<type_units_%d>",
5033 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5034 else
5035 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5036
5037 pst = create_partial_symtab (per_cu, name);
5038 pst->anonymous = 1;
f4dc4d17 5039
094b34ac
DE
5040 xfree (name);
5041 }
f4dc4d17 5042
094b34ac
DE
5043 tu_group->hash.dwo_unit = cu->dwo_unit;
5044 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5045
5046 return tu_group;
5047}
5048
094b34ac
DE
5049/* Look up the type_unit_group for type unit CU, and create it if necessary.
5050 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5051
5052static struct type_unit_group *
094b34ac 5053get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5054{
5055 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5056 struct type_unit_group *tu_group;
5057 void **slot;
5058 unsigned int line_offset;
5059 struct type_unit_group type_unit_group_for_lookup;
5060
5061 if (dwarf2_per_objfile->type_unit_groups == NULL)
5062 {
5063 dwarf2_per_objfile->type_unit_groups =
5064 allocate_type_unit_groups_table ();
5065 }
5066
5067 /* Do we need to create a new group, or can we use an existing one? */
5068
5069 if (stmt_list)
5070 {
5071 line_offset = DW_UNSND (stmt_list);
5072 ++tu_stats->nr_symtab_sharers;
5073 }
5074 else
5075 {
5076 /* Ugh, no stmt_list. Rare, but we have to handle it.
5077 We can do various things here like create one group per TU or
5078 spread them over multiple groups to split up the expansion work.
5079 To avoid worst case scenarios (too many groups or too large groups)
5080 we, umm, group them in bunches. */
5081 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5082 | (tu_stats->nr_stmt_less_type_units
5083 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5084 ++tu_stats->nr_stmt_less_type_units;
5085 }
5086
094b34ac
DE
5087 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5088 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5089 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5090 &type_unit_group_for_lookup, INSERT);
5091 if (*slot != NULL)
5092 {
5093 tu_group = *slot;
5094 gdb_assert (tu_group != NULL);
5095 }
5096 else
5097 {
5098 sect_offset line_offset_struct;
5099
5100 line_offset_struct.sect_off = line_offset;
094b34ac 5101 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5102 *slot = tu_group;
5103 ++tu_stats->nr_symtabs;
5104 }
5105
5106 return tu_group;
5107}
5108
5109/* Struct used to sort TUs by their abbreviation table offset. */
5110
5111struct tu_abbrev_offset
5112{
5113 struct signatured_type *sig_type;
5114 sect_offset abbrev_offset;
5115};
5116
5117/* Helper routine for build_type_unit_groups, passed to qsort. */
5118
5119static int
5120sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5121{
5122 const struct tu_abbrev_offset * const *a = ap;
5123 const struct tu_abbrev_offset * const *b = bp;
5124 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5125 unsigned int boff = (*b)->abbrev_offset.sect_off;
5126
5127 return (aoff > boff) - (aoff < boff);
5128}
5129
5130/* A helper function to add a type_unit_group to a table. */
5131
5132static int
5133add_type_unit_group_to_table (void **slot, void *datum)
5134{
5135 struct type_unit_group *tu_group = *slot;
5136 struct type_unit_group ***datap = datum;
5137
5138 **datap = tu_group;
5139 ++*datap;
5140
5141 return 1;
5142}
5143
5144/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5145 each one passing FUNC,DATA.
5146
5147 The efficiency is because we sort TUs by the abbrev table they use and
5148 only read each abbrev table once. In one program there are 200K TUs
5149 sharing 8K abbrev tables.
5150
5151 The main purpose of this function is to support building the
5152 dwarf2_per_objfile->type_unit_groups table.
5153 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5154 can collapse the search space by grouping them by stmt_list.
5155 The savings can be significant, in the same program from above the 200K TUs
5156 share 8K stmt_list tables.
5157
5158 FUNC is expected to call get_type_unit_group, which will create the
5159 struct type_unit_group if necessary and add it to
5160 dwarf2_per_objfile->type_unit_groups. */
5161
5162static void
5163build_type_unit_groups (die_reader_func_ftype *func, void *data)
5164{
5165 struct objfile *objfile = dwarf2_per_objfile->objfile;
5166 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5167 struct cleanup *cleanups;
5168 struct abbrev_table *abbrev_table;
5169 sect_offset abbrev_offset;
5170 struct tu_abbrev_offset *sorted_by_abbrev;
5171 struct type_unit_group **iter;
5172 int i;
5173
5174 /* It's up to the caller to not call us multiple times. */
5175 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5176
5177 if (dwarf2_per_objfile->n_type_units == 0)
5178 return;
5179
5180 /* TUs typically share abbrev tables, and there can be way more TUs than
5181 abbrev tables. Sort by abbrev table to reduce the number of times we
5182 read each abbrev table in.
5183 Alternatives are to punt or to maintain a cache of abbrev tables.
5184 This is simpler and efficient enough for now.
5185
5186 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5187 symtab to use). Typically TUs with the same abbrev offset have the same
5188 stmt_list value too so in practice this should work well.
5189
5190 The basic algorithm here is:
5191
5192 sort TUs by abbrev table
5193 for each TU with same abbrev table:
5194 read abbrev table if first user
5195 read TU top level DIE
5196 [IWBN if DWO skeletons had DW_AT_stmt_list]
5197 call FUNC */
5198
5199 if (dwarf2_read_debug)
5200 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5201
5202 /* Sort in a separate table to maintain the order of all_type_units
5203 for .gdb_index: TU indices directly index all_type_units. */
5204 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5205 dwarf2_per_objfile->n_type_units);
5206 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5207 {
5208 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5209
5210 sorted_by_abbrev[i].sig_type = sig_type;
5211 sorted_by_abbrev[i].abbrev_offset =
5212 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5213 sig_type->per_cu.offset);
5214 }
5215 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5216 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5217 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5218
094b34ac
DE
5219 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5220 called any number of times, so we don't reset tu_stats here. */
5221
f4dc4d17
DE
5222 abbrev_offset.sect_off = ~(unsigned) 0;
5223 abbrev_table = NULL;
5224 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5225
5226 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5227 {
5228 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5229
5230 /* Switch to the next abbrev table if necessary. */
5231 if (abbrev_table == NULL
5232 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5233 {
5234 if (abbrev_table != NULL)
5235 {
5236 abbrev_table_free (abbrev_table);
5237 /* Reset to NULL in case abbrev_table_read_table throws
5238 an error: abbrev_table_free_cleanup will get called. */
5239 abbrev_table = NULL;
5240 }
5241 abbrev_offset = tu->abbrev_offset;
5242 abbrev_table =
5243 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5244 abbrev_offset);
5245 ++tu_stats->nr_uniq_abbrev_tables;
5246 }
5247
5248 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5249 func, data);
5250 }
5251
5252 /* Create a vector of pointers to primary type units to make it easy to
5253 iterate over them and CUs. See dw2_get_primary_cu. */
5254 dwarf2_per_objfile->n_type_unit_groups =
5255 htab_elements (dwarf2_per_objfile->type_unit_groups);
5256 dwarf2_per_objfile->all_type_unit_groups =
5257 obstack_alloc (&objfile->objfile_obstack,
5258 dwarf2_per_objfile->n_type_unit_groups
5259 * sizeof (struct type_unit_group *));
5260 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5261 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5262 add_type_unit_group_to_table, &iter);
5263 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5264 == dwarf2_per_objfile->n_type_unit_groups);
5265
5266 do_cleanups (cleanups);
5267
5268 if (dwarf2_read_debug)
5269 {
5270 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5271 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5272 dwarf2_per_objfile->n_type_units);
5273 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5274 tu_stats->nr_uniq_abbrev_tables);
5275 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5276 tu_stats->nr_symtabs);
5277 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5278 tu_stats->nr_symtab_sharers);
5279 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5280 tu_stats->nr_stmt_less_type_units);
5281 }
5282}
5283
5284/* Reader function for build_type_psymtabs. */
5285
5286static void
5287build_type_psymtabs_reader (const struct die_reader_specs *reader,
5288 gdb_byte *info_ptr,
5289 struct die_info *type_unit_die,
5290 int has_children,
5291 void *data)
5292{
5293 struct objfile *objfile = dwarf2_per_objfile->objfile;
5294 struct dwarf2_cu *cu = reader->cu;
5295 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5296 struct type_unit_group *tu_group;
5297 struct attribute *attr;
5298 struct partial_die_info *first_die;
5299 CORE_ADDR lowpc, highpc;
5300 struct partial_symtab *pst;
5301
5302 gdb_assert (data == NULL);
5303
5304 if (! has_children)
5305 return;
5306
5307 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5308 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5309
094b34ac 5310 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5311
5312 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5313 cu->list_in_scope = &file_symbols;
5314 pst = create_partial_symtab (per_cu, "");
5315 pst->anonymous = 1;
5316
5317 first_die = load_partial_dies (reader, info_ptr, 1);
5318
5319 lowpc = (CORE_ADDR) -1;
5320 highpc = (CORE_ADDR) 0;
5321 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5322
5323 pst->n_global_syms = objfile->global_psymbols.next -
5324 (objfile->global_psymbols.list + pst->globals_offset);
5325 pst->n_static_syms = objfile->static_psymbols.next -
5326 (objfile->static_psymbols.list + pst->statics_offset);
5327 sort_pst_symbols (pst);
5328}
5329
5330/* Traversal function for build_type_psymtabs. */
5331
5332static int
5333build_type_psymtab_dependencies (void **slot, void *info)
5334{
5335 struct objfile *objfile = dwarf2_per_objfile->objfile;
5336 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5337 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5338 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5339 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5340 struct dwarf2_per_cu_data *iter;
5341 int i;
5342
5343 gdb_assert (len > 0);
5344
5345 pst->number_of_dependencies = len;
5346 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5347 len * sizeof (struct psymtab *));
5348 for (i = 0;
094b34ac 5349 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5350 ++i)
5351 {
5352 pst->dependencies[i] = iter->v.psymtab;
5353 iter->s.type_unit_group = tu_group;
5354 }
5355
094b34ac 5356 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5357
5358 return 1;
5359}
5360
5361/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5362 Build partial symbol tables for the .debug_types comp-units. */
5363
5364static void
5365build_type_psymtabs (struct objfile *objfile)
5366{
0e50663e 5367 if (! create_all_type_units (objfile))
348e048f
DE
5368 return;
5369
f4dc4d17
DE
5370 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5371
5372 /* Now that all TUs have been processed we can fill in the dependencies. */
5373 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5374 build_type_psymtab_dependencies, NULL);
348e048f
DE
5375}
5376
60606b2c
TT
5377/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5378
5379static void
5380psymtabs_addrmap_cleanup (void *o)
5381{
5382 struct objfile *objfile = o;
ec61707d 5383
60606b2c
TT
5384 objfile->psymtabs_addrmap = NULL;
5385}
5386
95554aad
TT
5387/* Compute the 'user' field for each psymtab in OBJFILE. */
5388
5389static void
5390set_partial_user (struct objfile *objfile)
5391{
5392 int i;
5393
5394 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5395 {
5396 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5397 struct partial_symtab *pst = per_cu->v.psymtab;
5398 int j;
5399
36586728
TT
5400 if (pst == NULL)
5401 continue;
5402
95554aad
TT
5403 for (j = 0; j < pst->number_of_dependencies; ++j)
5404 {
5405 /* Set the 'user' field only if it is not already set. */
5406 if (pst->dependencies[j]->user == NULL)
5407 pst->dependencies[j]->user = pst;
5408 }
5409 }
5410}
5411
93311388
DE
5412/* Build the partial symbol table by doing a quick pass through the
5413 .debug_info and .debug_abbrev sections. */
72bf9492 5414
93311388 5415static void
c67a9c90 5416dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5417{
60606b2c
TT
5418 struct cleanup *back_to, *addrmap_cleanup;
5419 struct obstack temp_obstack;
21b2bd31 5420 int i;
93311388 5421
45cfd468
DE
5422 if (dwarf2_read_debug)
5423 {
5424 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5425 objfile->name);
5426 }
5427
98bfdba5
PA
5428 dwarf2_per_objfile->reading_partial_symbols = 1;
5429
be391dca 5430 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5431
93311388
DE
5432 /* Any cached compilation units will be linked by the per-objfile
5433 read_in_chain. Make sure to free them when we're done. */
5434 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5435
348e048f
DE
5436 build_type_psymtabs (objfile);
5437
93311388 5438 create_all_comp_units (objfile);
c906108c 5439
60606b2c
TT
5440 /* Create a temporary address map on a temporary obstack. We later
5441 copy this to the final obstack. */
5442 obstack_init (&temp_obstack);
5443 make_cleanup_obstack_free (&temp_obstack);
5444 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5445 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5446
21b2bd31 5447 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5448 {
21b2bd31 5449 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5450
95554aad 5451 process_psymtab_comp_unit (per_cu, 0);
c906108c 5452 }
ff013f42 5453
95554aad
TT
5454 set_partial_user (objfile);
5455
ff013f42
JK
5456 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5457 &objfile->objfile_obstack);
60606b2c 5458 discard_cleanups (addrmap_cleanup);
ff013f42 5459
ae038cb0 5460 do_cleanups (back_to);
45cfd468
DE
5461
5462 if (dwarf2_read_debug)
5463 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5464 objfile->name);
ae038cb0
DJ
5465}
5466
3019eac3 5467/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5468
5469static void
dee91e82
DE
5470load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5471 gdb_byte *info_ptr,
5472 struct die_info *comp_unit_die,
5473 int has_children,
5474 void *data)
ae038cb0 5475{
dee91e82 5476 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5477
95554aad 5478 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5479
ae038cb0
DJ
5480 /* Check if comp unit has_children.
5481 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5482 If not, there's no more debug_info for this comp unit. */
d85a05f0 5483 if (has_children)
dee91e82
DE
5484 load_partial_dies (reader, info_ptr, 0);
5485}
98bfdba5 5486
dee91e82
DE
5487/* Load the partial DIEs for a secondary CU into memory.
5488 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5489
dee91e82
DE
5490static void
5491load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5492{
f4dc4d17
DE
5493 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5494 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5495}
5496
ae038cb0 5497static void
36586728
TT
5498read_comp_units_from_section (struct objfile *objfile,
5499 struct dwarf2_section_info *section,
5500 unsigned int is_dwz,
5501 int *n_allocated,
5502 int *n_comp_units,
5503 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5504{
be391dca 5505 gdb_byte *info_ptr;
36586728 5506 bfd *abfd = section->asection->owner;
be391dca 5507
36586728 5508 dwarf2_read_section (objfile, section);
ae038cb0 5509
36586728 5510 info_ptr = section->buffer;
6e70227d 5511
36586728 5512 while (info_ptr < section->buffer + section->size)
ae038cb0 5513 {
c764a876 5514 unsigned int length, initial_length_size;
ae038cb0 5515 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5516 sect_offset offset;
ae038cb0 5517
36586728 5518 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5519
5520 /* Read just enough information to find out where the next
5521 compilation unit is. */
36586728 5522 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5523
5524 /* Save the compilation unit for later lookup. */
5525 this_cu = obstack_alloc (&objfile->objfile_obstack,
5526 sizeof (struct dwarf2_per_cu_data));
5527 memset (this_cu, 0, sizeof (*this_cu));
5528 this_cu->offset = offset;
c764a876 5529 this_cu->length = length + initial_length_size;
36586728 5530 this_cu->is_dwz = is_dwz;
9291a0cd 5531 this_cu->objfile = objfile;
36586728 5532 this_cu->info_or_types_section = section;
ae038cb0 5533
36586728 5534 if (*n_comp_units == *n_allocated)
ae038cb0 5535 {
36586728
TT
5536 *n_allocated *= 2;
5537 *all_comp_units = xrealloc (*all_comp_units,
5538 *n_allocated
5539 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5540 }
36586728
TT
5541 (*all_comp_units)[*n_comp_units] = this_cu;
5542 ++*n_comp_units;
ae038cb0
DJ
5543
5544 info_ptr = info_ptr + this_cu->length;
5545 }
36586728
TT
5546}
5547
5548/* Create a list of all compilation units in OBJFILE.
5549 This is only done for -readnow and building partial symtabs. */
5550
5551static void
5552create_all_comp_units (struct objfile *objfile)
5553{
5554 int n_allocated;
5555 int n_comp_units;
5556 struct dwarf2_per_cu_data **all_comp_units;
5557
5558 n_comp_units = 0;
5559 n_allocated = 10;
5560 all_comp_units = xmalloc (n_allocated
5561 * sizeof (struct dwarf2_per_cu_data *));
5562
5563 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5564 &n_allocated, &n_comp_units, &all_comp_units);
5565
5566 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5567 {
5568 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5569
5570 read_comp_units_from_section (objfile, &dwz->info, 1,
5571 &n_allocated, &n_comp_units,
5572 &all_comp_units);
5573 }
ae038cb0
DJ
5574
5575 dwarf2_per_objfile->all_comp_units
5576 = obstack_alloc (&objfile->objfile_obstack,
5577 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5578 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5579 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5580 xfree (all_comp_units);
5581 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5582}
5583
5734ee8b
DJ
5584/* Process all loaded DIEs for compilation unit CU, starting at
5585 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5586 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5587 DW_AT_ranges). If NEED_PC is set, then this function will set
5588 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5589 and record the covered ranges in the addrmap. */
c906108c 5590
72bf9492
DJ
5591static void
5592scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5593 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5594{
72bf9492 5595 struct partial_die_info *pdi;
c906108c 5596
91c24f0a
DC
5597 /* Now, march along the PDI's, descending into ones which have
5598 interesting children but skipping the children of the other ones,
5599 until we reach the end of the compilation unit. */
c906108c 5600
72bf9492 5601 pdi = first_die;
91c24f0a 5602
72bf9492
DJ
5603 while (pdi != NULL)
5604 {
5605 fixup_partial_die (pdi, cu);
c906108c 5606
f55ee35c 5607 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5608 children, so we need to look at them. Ditto for anonymous
5609 enums. */
933c6fe4 5610
72bf9492 5611 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5612 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5613 || pdi->tag == DW_TAG_imported_unit)
c906108c 5614 {
72bf9492 5615 switch (pdi->tag)
c906108c
SS
5616 {
5617 case DW_TAG_subprogram:
5734ee8b 5618 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5619 break;
72929c62 5620 case DW_TAG_constant:
c906108c
SS
5621 case DW_TAG_variable:
5622 case DW_TAG_typedef:
91c24f0a 5623 case DW_TAG_union_type:
72bf9492 5624 if (!pdi->is_declaration)
63d06c5c 5625 {
72bf9492 5626 add_partial_symbol (pdi, cu);
63d06c5c
DC
5627 }
5628 break;
c906108c 5629 case DW_TAG_class_type:
680b30c7 5630 case DW_TAG_interface_type:
c906108c 5631 case DW_TAG_structure_type:
72bf9492 5632 if (!pdi->is_declaration)
c906108c 5633 {
72bf9492 5634 add_partial_symbol (pdi, cu);
c906108c
SS
5635 }
5636 break;
91c24f0a 5637 case DW_TAG_enumeration_type:
72bf9492
DJ
5638 if (!pdi->is_declaration)
5639 add_partial_enumeration (pdi, cu);
c906108c
SS
5640 break;
5641 case DW_TAG_base_type:
a02abb62 5642 case DW_TAG_subrange_type:
c906108c 5643 /* File scope base type definitions are added to the partial
c5aa993b 5644 symbol table. */
72bf9492 5645 add_partial_symbol (pdi, cu);
c906108c 5646 break;
d9fa45fe 5647 case DW_TAG_namespace:
5734ee8b 5648 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5649 break;
5d7cb8df
JK
5650 case DW_TAG_module:
5651 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5652 break;
95554aad
TT
5653 case DW_TAG_imported_unit:
5654 {
5655 struct dwarf2_per_cu_data *per_cu;
5656
f4dc4d17
DE
5657 /* For now we don't handle imported units in type units. */
5658 if (cu->per_cu->is_debug_types)
5659 {
5660 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5661 " supported in type units [in module %s]"),
5662 cu->objfile->name);
5663 }
5664
95554aad 5665 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5666 pdi->is_dwz,
95554aad
TT
5667 cu->objfile);
5668
5669 /* Go read the partial unit, if needed. */
5670 if (per_cu->v.psymtab == NULL)
5671 process_psymtab_comp_unit (per_cu, 1);
5672
f4dc4d17
DE
5673 VEC_safe_push (dwarf2_per_cu_ptr,
5674 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5675 }
5676 break;
c906108c
SS
5677 default:
5678 break;
5679 }
5680 }
5681
72bf9492
DJ
5682 /* If the die has a sibling, skip to the sibling. */
5683
5684 pdi = pdi->die_sibling;
5685 }
5686}
5687
5688/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5689
72bf9492 5690 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5691 name is concatenated with "::" and the partial DIE's name. For
5692 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5693 Enumerators are an exception; they use the scope of their parent
5694 enumeration type, i.e. the name of the enumeration type is not
5695 prepended to the enumerator.
91c24f0a 5696
72bf9492
DJ
5697 There are two complexities. One is DW_AT_specification; in this
5698 case "parent" means the parent of the target of the specification,
5699 instead of the direct parent of the DIE. The other is compilers
5700 which do not emit DW_TAG_namespace; in this case we try to guess
5701 the fully qualified name of structure types from their members'
5702 linkage names. This must be done using the DIE's children rather
5703 than the children of any DW_AT_specification target. We only need
5704 to do this for structures at the top level, i.e. if the target of
5705 any DW_AT_specification (if any; otherwise the DIE itself) does not
5706 have a parent. */
5707
5708/* Compute the scope prefix associated with PDI's parent, in
5709 compilation unit CU. The result will be allocated on CU's
5710 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5711 field. NULL is returned if no prefix is necessary. */
5712static char *
5713partial_die_parent_scope (struct partial_die_info *pdi,
5714 struct dwarf2_cu *cu)
5715{
5716 char *grandparent_scope;
5717 struct partial_die_info *parent, *real_pdi;
91c24f0a 5718
72bf9492
DJ
5719 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5720 then this means the parent of the specification DIE. */
5721
5722 real_pdi = pdi;
72bf9492 5723 while (real_pdi->has_specification)
36586728
TT
5724 real_pdi = find_partial_die (real_pdi->spec_offset,
5725 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5726
5727 parent = real_pdi->die_parent;
5728 if (parent == NULL)
5729 return NULL;
5730
5731 if (parent->scope_set)
5732 return parent->scope;
5733
5734 fixup_partial_die (parent, cu);
5735
10b3939b 5736 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5737
acebe513
UW
5738 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5739 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5740 Work around this problem here. */
5741 if (cu->language == language_cplus
6e70227d 5742 && parent->tag == DW_TAG_namespace
acebe513
UW
5743 && strcmp (parent->name, "::") == 0
5744 && grandparent_scope == NULL)
5745 {
5746 parent->scope = NULL;
5747 parent->scope_set = 1;
5748 return NULL;
5749 }
5750
9c6c53f7
SA
5751 if (pdi->tag == DW_TAG_enumerator)
5752 /* Enumerators should not get the name of the enumeration as a prefix. */
5753 parent->scope = grandparent_scope;
5754 else if (parent->tag == DW_TAG_namespace
f55ee35c 5755 || parent->tag == DW_TAG_module
72bf9492
DJ
5756 || parent->tag == DW_TAG_structure_type
5757 || parent->tag == DW_TAG_class_type
680b30c7 5758 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5759 || parent->tag == DW_TAG_union_type
5760 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5761 {
5762 if (grandparent_scope == NULL)
5763 parent->scope = parent->name;
5764 else
3e43a32a
MS
5765 parent->scope = typename_concat (&cu->comp_unit_obstack,
5766 grandparent_scope,
f55ee35c 5767 parent->name, 0, cu);
72bf9492 5768 }
72bf9492
DJ
5769 else
5770 {
5771 /* FIXME drow/2004-04-01: What should we be doing with
5772 function-local names? For partial symbols, we should probably be
5773 ignoring them. */
5774 complaint (&symfile_complaints,
e2e0b3e5 5775 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5776 parent->tag, pdi->offset.sect_off);
72bf9492 5777 parent->scope = grandparent_scope;
c906108c
SS
5778 }
5779
72bf9492
DJ
5780 parent->scope_set = 1;
5781 return parent->scope;
5782}
5783
5784/* Return the fully scoped name associated with PDI, from compilation unit
5785 CU. The result will be allocated with malloc. */
4568ecf9 5786
72bf9492
DJ
5787static char *
5788partial_die_full_name (struct partial_die_info *pdi,
5789 struct dwarf2_cu *cu)
5790{
5791 char *parent_scope;
5792
98bfdba5
PA
5793 /* If this is a template instantiation, we can not work out the
5794 template arguments from partial DIEs. So, unfortunately, we have
5795 to go through the full DIEs. At least any work we do building
5796 types here will be reused if full symbols are loaded later. */
5797 if (pdi->has_template_arguments)
5798 {
5799 fixup_partial_die (pdi, cu);
5800
5801 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5802 {
5803 struct die_info *die;
5804 struct attribute attr;
5805 struct dwarf2_cu *ref_cu = cu;
5806
b64f50a1 5807 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5808 attr.name = 0;
5809 attr.form = DW_FORM_ref_addr;
4568ecf9 5810 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5811 die = follow_die_ref (NULL, &attr, &ref_cu);
5812
5813 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5814 }
5815 }
5816
72bf9492
DJ
5817 parent_scope = partial_die_parent_scope (pdi, cu);
5818 if (parent_scope == NULL)
5819 return NULL;
5820 else
f55ee35c 5821 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5822}
5823
5824static void
72bf9492 5825add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5826{
e7c27a73 5827 struct objfile *objfile = cu->objfile;
c906108c 5828 CORE_ADDR addr = 0;
decbce07 5829 char *actual_name = NULL;
e142c38c 5830 CORE_ADDR baseaddr;
72bf9492 5831 int built_actual_name = 0;
e142c38c
DJ
5832
5833 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5834
94af9270
KS
5835 actual_name = partial_die_full_name (pdi, cu);
5836 if (actual_name)
5837 built_actual_name = 1;
63d06c5c 5838
72bf9492
DJ
5839 if (actual_name == NULL)
5840 actual_name = pdi->name;
5841
c906108c
SS
5842 switch (pdi->tag)
5843 {
5844 case DW_TAG_subprogram:
2cfa0c8d 5845 if (pdi->is_external || cu->language == language_ada)
c906108c 5846 {
2cfa0c8d
JB
5847 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5848 of the global scope. But in Ada, we want to be able to access
5849 nested procedures globally. So all Ada subprograms are stored
5850 in the global scope. */
f47fb265 5851 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5852 mst_text, objfile); */
f47fb265
MS
5853 add_psymbol_to_list (actual_name, strlen (actual_name),
5854 built_actual_name,
5855 VAR_DOMAIN, LOC_BLOCK,
5856 &objfile->global_psymbols,
5857 0, pdi->lowpc + baseaddr,
5858 cu->language, objfile);
c906108c
SS
5859 }
5860 else
5861 {
f47fb265 5862 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5863 mst_file_text, objfile); */
f47fb265
MS
5864 add_psymbol_to_list (actual_name, strlen (actual_name),
5865 built_actual_name,
5866 VAR_DOMAIN, LOC_BLOCK,
5867 &objfile->static_psymbols,
5868 0, pdi->lowpc + baseaddr,
5869 cu->language, objfile);
c906108c
SS
5870 }
5871 break;
72929c62
JB
5872 case DW_TAG_constant:
5873 {
5874 struct psymbol_allocation_list *list;
5875
5876 if (pdi->is_external)
5877 list = &objfile->global_psymbols;
5878 else
5879 list = &objfile->static_psymbols;
f47fb265
MS
5880 add_psymbol_to_list (actual_name, strlen (actual_name),
5881 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5882 list, 0, 0, cu->language, objfile);
72929c62
JB
5883 }
5884 break;
c906108c 5885 case DW_TAG_variable:
95554aad
TT
5886 if (pdi->d.locdesc)
5887 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5888
95554aad 5889 if (pdi->d.locdesc
caac4577
JG
5890 && addr == 0
5891 && !dwarf2_per_objfile->has_section_at_zero)
5892 {
5893 /* A global or static variable may also have been stripped
5894 out by the linker if unused, in which case its address
5895 will be nullified; do not add such variables into partial
5896 symbol table then. */
5897 }
5898 else if (pdi->is_external)
c906108c
SS
5899 {
5900 /* Global Variable.
5901 Don't enter into the minimal symbol tables as there is
5902 a minimal symbol table entry from the ELF symbols already.
5903 Enter into partial symbol table if it has a location
5904 descriptor or a type.
5905 If the location descriptor is missing, new_symbol will create
5906 a LOC_UNRESOLVED symbol, the address of the variable will then
5907 be determined from the minimal symbol table whenever the variable
5908 is referenced.
5909 The address for the partial symbol table entry is not
5910 used by GDB, but it comes in handy for debugging partial symbol
5911 table building. */
5912
95554aad 5913 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5914 add_psymbol_to_list (actual_name, strlen (actual_name),
5915 built_actual_name,
5916 VAR_DOMAIN, LOC_STATIC,
5917 &objfile->global_psymbols,
5918 0, addr + baseaddr,
5919 cu->language, objfile);
c906108c
SS
5920 }
5921 else
5922 {
0963b4bd 5923 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5924 if (pdi->d.locdesc == NULL)
decbce07
MS
5925 {
5926 if (built_actual_name)
5927 xfree (actual_name);
5928 return;
5929 }
f47fb265 5930 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5931 mst_file_data, objfile); */
f47fb265
MS
5932 add_psymbol_to_list (actual_name, strlen (actual_name),
5933 built_actual_name,
5934 VAR_DOMAIN, LOC_STATIC,
5935 &objfile->static_psymbols,
5936 0, addr + baseaddr,
5937 cu->language, objfile);
c906108c
SS
5938 }
5939 break;
5940 case DW_TAG_typedef:
5941 case DW_TAG_base_type:
a02abb62 5942 case DW_TAG_subrange_type:
38d518c9 5943 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5944 built_actual_name,
176620f1 5945 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5946 &objfile->static_psymbols,
e142c38c 5947 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5948 break;
72bf9492
DJ
5949 case DW_TAG_namespace:
5950 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5951 built_actual_name,
72bf9492
DJ
5952 VAR_DOMAIN, LOC_TYPEDEF,
5953 &objfile->global_psymbols,
5954 0, (CORE_ADDR) 0, cu->language, objfile);
5955 break;
c906108c 5956 case DW_TAG_class_type:
680b30c7 5957 case DW_TAG_interface_type:
c906108c
SS
5958 case DW_TAG_structure_type:
5959 case DW_TAG_union_type:
5960 case DW_TAG_enumeration_type:
fa4028e9
JB
5961 /* Skip external references. The DWARF standard says in the section
5962 about "Structure, Union, and Class Type Entries": "An incomplete
5963 structure, union or class type is represented by a structure,
5964 union or class entry that does not have a byte size attribute
5965 and that has a DW_AT_declaration attribute." */
5966 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5967 {
5968 if (built_actual_name)
5969 xfree (actual_name);
5970 return;
5971 }
fa4028e9 5972
63d06c5c
DC
5973 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5974 static vs. global. */
38d518c9 5975 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5976 built_actual_name,
176620f1 5977 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5978 (cu->language == language_cplus
5979 || cu->language == language_java)
63d06c5c
DC
5980 ? &objfile->global_psymbols
5981 : &objfile->static_psymbols,
e142c38c 5982 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5983
c906108c
SS
5984 break;
5985 case DW_TAG_enumerator:
38d518c9 5986 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5987 built_actual_name,
176620f1 5988 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5989 (cu->language == language_cplus
5990 || cu->language == language_java)
f6fe98ef
DJ
5991 ? &objfile->global_psymbols
5992 : &objfile->static_psymbols,
e142c38c 5993 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5994 break;
5995 default:
5996 break;
5997 }
5c4e30ca 5998
72bf9492
DJ
5999 if (built_actual_name)
6000 xfree (actual_name);
c906108c
SS
6001}
6002
5c4e30ca
DC
6003/* Read a partial die corresponding to a namespace; also, add a symbol
6004 corresponding to that namespace to the symbol table. NAMESPACE is
6005 the name of the enclosing namespace. */
91c24f0a 6006
72bf9492
DJ
6007static void
6008add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6009 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6010 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6011{
72bf9492 6012 /* Add a symbol for the namespace. */
e7c27a73 6013
72bf9492 6014 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6015
6016 /* Now scan partial symbols in that namespace. */
6017
91c24f0a 6018 if (pdi->has_children)
5734ee8b 6019 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6020}
6021
5d7cb8df
JK
6022/* Read a partial die corresponding to a Fortran module. */
6023
6024static void
6025add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6026 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6027{
f55ee35c 6028 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6029
6030 if (pdi->has_children)
6031 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6032}
6033
bc30ff58
JB
6034/* Read a partial die corresponding to a subprogram and create a partial
6035 symbol for that subprogram. When the CU language allows it, this
6036 routine also defines a partial symbol for each nested subprogram
6037 that this subprogram contains.
6e70227d 6038
bc30ff58
JB
6039 DIE my also be a lexical block, in which case we simply search
6040 recursively for suprograms defined inside that lexical block.
6041 Again, this is only performed when the CU language allows this
6042 type of definitions. */
6043
6044static void
6045add_partial_subprogram (struct partial_die_info *pdi,
6046 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6047 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6048{
6049 if (pdi->tag == DW_TAG_subprogram)
6050 {
6051 if (pdi->has_pc_info)
6052 {
6053 if (pdi->lowpc < *lowpc)
6054 *lowpc = pdi->lowpc;
6055 if (pdi->highpc > *highpc)
6056 *highpc = pdi->highpc;
5734ee8b
DJ
6057 if (need_pc)
6058 {
6059 CORE_ADDR baseaddr;
6060 struct objfile *objfile = cu->objfile;
6061
6062 baseaddr = ANOFFSET (objfile->section_offsets,
6063 SECT_OFF_TEXT (objfile));
6064 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6065 pdi->lowpc + baseaddr,
6066 pdi->highpc - 1 + baseaddr,
9291a0cd 6067 cu->per_cu->v.psymtab);
5734ee8b 6068 }
481860b3
GB
6069 }
6070
6071 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6072 {
bc30ff58 6073 if (!pdi->is_declaration)
e8d05480
JB
6074 /* Ignore subprogram DIEs that do not have a name, they are
6075 illegal. Do not emit a complaint at this point, we will
6076 do so when we convert this psymtab into a symtab. */
6077 if (pdi->name)
6078 add_partial_symbol (pdi, cu);
bc30ff58
JB
6079 }
6080 }
6e70227d 6081
bc30ff58
JB
6082 if (! pdi->has_children)
6083 return;
6084
6085 if (cu->language == language_ada)
6086 {
6087 pdi = pdi->die_child;
6088 while (pdi != NULL)
6089 {
6090 fixup_partial_die (pdi, cu);
6091 if (pdi->tag == DW_TAG_subprogram
6092 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6093 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6094 pdi = pdi->die_sibling;
6095 }
6096 }
6097}
6098
91c24f0a
DC
6099/* Read a partial die corresponding to an enumeration type. */
6100
72bf9492
DJ
6101static void
6102add_partial_enumeration (struct partial_die_info *enum_pdi,
6103 struct dwarf2_cu *cu)
91c24f0a 6104{
72bf9492 6105 struct partial_die_info *pdi;
91c24f0a
DC
6106
6107 if (enum_pdi->name != NULL)
72bf9492
DJ
6108 add_partial_symbol (enum_pdi, cu);
6109
6110 pdi = enum_pdi->die_child;
6111 while (pdi)
91c24f0a 6112 {
72bf9492 6113 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6114 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6115 else
72bf9492
DJ
6116 add_partial_symbol (pdi, cu);
6117 pdi = pdi->die_sibling;
91c24f0a 6118 }
91c24f0a
DC
6119}
6120
6caca83c
CC
6121/* Return the initial uleb128 in the die at INFO_PTR. */
6122
6123static unsigned int
6124peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6125{
6126 unsigned int bytes_read;
6127
6128 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6129}
6130
4bb7a0a7
DJ
6131/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6132 Return the corresponding abbrev, or NULL if the number is zero (indicating
6133 an empty DIE). In either case *BYTES_READ will be set to the length of
6134 the initial number. */
6135
6136static struct abbrev_info *
fe1b8b76 6137peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6138 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6139{
6140 bfd *abfd = cu->objfile->obfd;
6141 unsigned int abbrev_number;
6142 struct abbrev_info *abbrev;
6143
6144 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6145
6146 if (abbrev_number == 0)
6147 return NULL;
6148
433df2d4 6149 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6150 if (!abbrev)
6151 {
3e43a32a
MS
6152 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6153 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6154 }
6155
6156 return abbrev;
6157}
6158
93311388
DE
6159/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6160 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6161 DIE. Any children of the skipped DIEs will also be skipped. */
6162
fe1b8b76 6163static gdb_byte *
dee91e82 6164skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6165{
dee91e82 6166 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6167 struct abbrev_info *abbrev;
6168 unsigned int bytes_read;
6169
6170 while (1)
6171 {
6172 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6173 if (abbrev == NULL)
6174 return info_ptr + bytes_read;
6175 else
dee91e82 6176 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6177 }
6178}
6179
93311388
DE
6180/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6181 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6182 abbrev corresponding to that skipped uleb128 should be passed in
6183 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6184 children. */
6185
fe1b8b76 6186static gdb_byte *
dee91e82
DE
6187skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6188 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6189{
6190 unsigned int bytes_read;
6191 struct attribute attr;
dee91e82
DE
6192 bfd *abfd = reader->abfd;
6193 struct dwarf2_cu *cu = reader->cu;
6194 gdb_byte *buffer = reader->buffer;
f664829e
DE
6195 const gdb_byte *buffer_end = reader->buffer_end;
6196 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6197 unsigned int form, i;
6198
6199 for (i = 0; i < abbrev->num_attrs; i++)
6200 {
6201 /* The only abbrev we care about is DW_AT_sibling. */
6202 if (abbrev->attrs[i].name == DW_AT_sibling)
6203 {
dee91e82 6204 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6205 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6206 complaint (&symfile_complaints,
6207 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6208 else
b64f50a1 6209 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6210 }
6211
6212 /* If it isn't DW_AT_sibling, skip this attribute. */
6213 form = abbrev->attrs[i].form;
6214 skip_attribute:
6215 switch (form)
6216 {
4bb7a0a7 6217 case DW_FORM_ref_addr:
ae411497
TT
6218 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6219 and later it is offset sized. */
6220 if (cu->header.version == 2)
6221 info_ptr += cu->header.addr_size;
6222 else
6223 info_ptr += cu->header.offset_size;
6224 break;
36586728
TT
6225 case DW_FORM_GNU_ref_alt:
6226 info_ptr += cu->header.offset_size;
6227 break;
ae411497 6228 case DW_FORM_addr:
4bb7a0a7
DJ
6229 info_ptr += cu->header.addr_size;
6230 break;
6231 case DW_FORM_data1:
6232 case DW_FORM_ref1:
6233 case DW_FORM_flag:
6234 info_ptr += 1;
6235 break;
2dc7f7b3
TT
6236 case DW_FORM_flag_present:
6237 break;
4bb7a0a7
DJ
6238 case DW_FORM_data2:
6239 case DW_FORM_ref2:
6240 info_ptr += 2;
6241 break;
6242 case DW_FORM_data4:
6243 case DW_FORM_ref4:
6244 info_ptr += 4;
6245 break;
6246 case DW_FORM_data8:
6247 case DW_FORM_ref8:
55f1336d 6248 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6249 info_ptr += 8;
6250 break;
6251 case DW_FORM_string:
9b1c24c8 6252 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6253 info_ptr += bytes_read;
6254 break;
2dc7f7b3 6255 case DW_FORM_sec_offset:
4bb7a0a7 6256 case DW_FORM_strp:
36586728 6257 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6258 info_ptr += cu->header.offset_size;
6259 break;
2dc7f7b3 6260 case DW_FORM_exprloc:
4bb7a0a7
DJ
6261 case DW_FORM_block:
6262 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6263 info_ptr += bytes_read;
6264 break;
6265 case DW_FORM_block1:
6266 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6267 break;
6268 case DW_FORM_block2:
6269 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6270 break;
6271 case DW_FORM_block4:
6272 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6273 break;
6274 case DW_FORM_sdata:
6275 case DW_FORM_udata:
6276 case DW_FORM_ref_udata:
3019eac3
DE
6277 case DW_FORM_GNU_addr_index:
6278 case DW_FORM_GNU_str_index:
f664829e 6279 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6280 break;
6281 case DW_FORM_indirect:
6282 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6283 info_ptr += bytes_read;
6284 /* We need to continue parsing from here, so just go back to
6285 the top. */
6286 goto skip_attribute;
6287
6288 default:
3e43a32a
MS
6289 error (_("Dwarf Error: Cannot handle %s "
6290 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6291 dwarf_form_name (form),
6292 bfd_get_filename (abfd));
6293 }
6294 }
6295
6296 if (abbrev->has_children)
dee91e82 6297 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6298 else
6299 return info_ptr;
6300}
6301
93311388 6302/* Locate ORIG_PDI's sibling.
dee91e82 6303 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6304
fe1b8b76 6305static gdb_byte *
dee91e82
DE
6306locate_pdi_sibling (const struct die_reader_specs *reader,
6307 struct partial_die_info *orig_pdi,
6308 gdb_byte *info_ptr)
91c24f0a
DC
6309{
6310 /* Do we know the sibling already? */
72bf9492 6311
91c24f0a
DC
6312 if (orig_pdi->sibling)
6313 return orig_pdi->sibling;
6314
6315 /* Are there any children to deal with? */
6316
6317 if (!orig_pdi->has_children)
6318 return info_ptr;
6319
4bb7a0a7 6320 /* Skip the children the long way. */
91c24f0a 6321
dee91e82 6322 return skip_children (reader, info_ptr);
91c24f0a
DC
6323}
6324
c906108c
SS
6325/* Expand this partial symbol table into a full symbol table. */
6326
6327static void
fba45db2 6328dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 6329{
c906108c
SS
6330 if (pst != NULL)
6331 {
6332 if (pst->readin)
6333 {
3e43a32a
MS
6334 warning (_("bug: psymtab for %s is already read in."),
6335 pst->filename);
c906108c
SS
6336 }
6337 else
6338 {
6339 if (info_verbose)
6340 {
3e43a32a
MS
6341 printf_filtered (_("Reading in symbols for %s..."),
6342 pst->filename);
c906108c
SS
6343 gdb_flush (gdb_stdout);
6344 }
6345
10b3939b
DJ
6346 /* Restore our global data. */
6347 dwarf2_per_objfile = objfile_data (pst->objfile,
6348 dwarf2_objfile_data_key);
6349
b2ab525c
KB
6350 /* If this psymtab is constructed from a debug-only objfile, the
6351 has_section_at_zero flag will not necessarily be correct. We
6352 can get the correct value for this flag by looking at the data
6353 associated with the (presumably stripped) associated objfile. */
6354 if (pst->objfile->separate_debug_objfile_backlink)
6355 {
6356 struct dwarf2_per_objfile *dpo_backlink
6357 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6358 dwarf2_objfile_data_key);
9a619af0 6359
b2ab525c
KB
6360 dwarf2_per_objfile->has_section_at_zero
6361 = dpo_backlink->has_section_at_zero;
6362 }
6363
98bfdba5
PA
6364 dwarf2_per_objfile->reading_partial_symbols = 0;
6365
c906108c
SS
6366 psymtab_to_symtab_1 (pst);
6367
6368 /* Finish up the debug error message. */
6369 if (info_verbose)
a3f17187 6370 printf_filtered (_("done.\n"));
c906108c
SS
6371 }
6372 }
95554aad
TT
6373
6374 process_cu_includes ();
c906108c 6375}
9cdd5dbd
DE
6376\f
6377/* Reading in full CUs. */
c906108c 6378
10b3939b
DJ
6379/* Add PER_CU to the queue. */
6380
6381static void
95554aad
TT
6382queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6383 enum language pretend_language)
10b3939b
DJ
6384{
6385 struct dwarf2_queue_item *item;
6386
6387 per_cu->queued = 1;
6388 item = xmalloc (sizeof (*item));
6389 item->per_cu = per_cu;
95554aad 6390 item->pretend_language = pretend_language;
10b3939b
DJ
6391 item->next = NULL;
6392
6393 if (dwarf2_queue == NULL)
6394 dwarf2_queue = item;
6395 else
6396 dwarf2_queue_tail->next = item;
6397
6398 dwarf2_queue_tail = item;
6399}
6400
0907af0c
DE
6401/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6402 unit and add it to our queue.
6403 The result is non-zero if PER_CU was queued, otherwise the result is zero
6404 meaning either PER_CU is already queued or it is already loaded. */
6405
6406static int
6407maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6408 struct dwarf2_per_cu_data *per_cu,
6409 enum language pretend_language)
6410{
6411 /* We may arrive here during partial symbol reading, if we need full
6412 DIEs to process an unusual case (e.g. template arguments). Do
6413 not queue PER_CU, just tell our caller to load its DIEs. */
6414 if (dwarf2_per_objfile->reading_partial_symbols)
6415 {
6416 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6417 return 1;
6418 return 0;
6419 }
6420
6421 /* Mark the dependence relation so that we don't flush PER_CU
6422 too early. */
6423 dwarf2_add_dependence (this_cu, per_cu);
6424
6425 /* If it's already on the queue, we have nothing to do. */
6426 if (per_cu->queued)
6427 return 0;
6428
6429 /* If the compilation unit is already loaded, just mark it as
6430 used. */
6431 if (per_cu->cu != NULL)
6432 {
6433 per_cu->cu->last_used = 0;
6434 return 0;
6435 }
6436
6437 /* Add it to the queue. */
6438 queue_comp_unit (per_cu, pretend_language);
6439
6440 return 1;
6441}
6442
10b3939b
DJ
6443/* Process the queue. */
6444
6445static void
a0f42c21 6446process_queue (void)
10b3939b
DJ
6447{
6448 struct dwarf2_queue_item *item, *next_item;
6449
45cfd468
DE
6450 if (dwarf2_read_debug)
6451 {
6452 fprintf_unfiltered (gdb_stdlog,
6453 "Expanding one or more symtabs of objfile %s ...\n",
6454 dwarf2_per_objfile->objfile->name);
6455 }
6456
03dd20cc
DJ
6457 /* The queue starts out with one item, but following a DIE reference
6458 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6459 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6460 {
9291a0cd
TT
6461 if (dwarf2_per_objfile->using_index
6462 ? !item->per_cu->v.quick->symtab
6463 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6464 {
6465 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6466
6467 if (dwarf2_read_debug)
6468 {
6469 fprintf_unfiltered (gdb_stdlog,
6470 "Expanding symtab of %s at offset 0x%x\n",
6471 per_cu->is_debug_types ? "TU" : "CU",
6472 per_cu->offset.sect_off);
6473 }
6474
6475 if (per_cu->is_debug_types)
6476 process_full_type_unit (per_cu, item->pretend_language);
6477 else
6478 process_full_comp_unit (per_cu, item->pretend_language);
6479
6480 if (dwarf2_read_debug)
6481 {
6482 fprintf_unfiltered (gdb_stdlog,
6483 "Done expanding %s at offset 0x%x\n",
6484 per_cu->is_debug_types ? "TU" : "CU",
6485 per_cu->offset.sect_off);
6486 }
6487 }
10b3939b
DJ
6488
6489 item->per_cu->queued = 0;
6490 next_item = item->next;
6491 xfree (item);
6492 }
6493
6494 dwarf2_queue_tail = NULL;
45cfd468
DE
6495
6496 if (dwarf2_read_debug)
6497 {
6498 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6499 dwarf2_per_objfile->objfile->name);
6500 }
10b3939b
DJ
6501}
6502
6503/* Free all allocated queue entries. This function only releases anything if
6504 an error was thrown; if the queue was processed then it would have been
6505 freed as we went along. */
6506
6507static void
6508dwarf2_release_queue (void *dummy)
6509{
6510 struct dwarf2_queue_item *item, *last;
6511
6512 item = dwarf2_queue;
6513 while (item)
6514 {
6515 /* Anything still marked queued is likely to be in an
6516 inconsistent state, so discard it. */
6517 if (item->per_cu->queued)
6518 {
6519 if (item->per_cu->cu != NULL)
dee91e82 6520 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6521 item->per_cu->queued = 0;
6522 }
6523
6524 last = item;
6525 item = item->next;
6526 xfree (last);
6527 }
6528
6529 dwarf2_queue = dwarf2_queue_tail = NULL;
6530}
6531
6532/* Read in full symbols for PST, and anything it depends on. */
6533
c906108c 6534static void
fba45db2 6535psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6536{
10b3939b 6537 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6538 int i;
6539
95554aad
TT
6540 if (pst->readin)
6541 return;
6542
aaa75496 6543 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6544 if (!pst->dependencies[i]->readin
6545 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6546 {
6547 /* Inform about additional files that need to be read in. */
6548 if (info_verbose)
6549 {
a3f17187 6550 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6551 fputs_filtered (" ", gdb_stdout);
6552 wrap_here ("");
6553 fputs_filtered ("and ", gdb_stdout);
6554 wrap_here ("");
6555 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6556 wrap_here (""); /* Flush output. */
aaa75496
JB
6557 gdb_flush (gdb_stdout);
6558 }
6559 psymtab_to_symtab_1 (pst->dependencies[i]);
6560 }
6561
e38df1d0 6562 per_cu = pst->read_symtab_private;
10b3939b
DJ
6563
6564 if (per_cu == NULL)
aaa75496
JB
6565 {
6566 /* It's an include file, no symbols to read for it.
6567 Everything is in the parent symtab. */
6568 pst->readin = 1;
6569 return;
6570 }
c906108c 6571
a0f42c21 6572 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6573}
6574
dee91e82
DE
6575/* Trivial hash function for die_info: the hash value of a DIE
6576 is its offset in .debug_info for this objfile. */
10b3939b 6577
dee91e82
DE
6578static hashval_t
6579die_hash (const void *item)
10b3939b 6580{
dee91e82 6581 const struct die_info *die = item;
6502dd73 6582
dee91e82
DE
6583 return die->offset.sect_off;
6584}
63d06c5c 6585
dee91e82
DE
6586/* Trivial comparison function for die_info structures: two DIEs
6587 are equal if they have the same offset. */
98bfdba5 6588
dee91e82
DE
6589static int
6590die_eq (const void *item_lhs, const void *item_rhs)
6591{
6592 const struct die_info *die_lhs = item_lhs;
6593 const struct die_info *die_rhs = item_rhs;
c906108c 6594
dee91e82
DE
6595 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6596}
c906108c 6597
dee91e82
DE
6598/* die_reader_func for load_full_comp_unit.
6599 This is identical to read_signatured_type_reader,
6600 but is kept separate for now. */
c906108c 6601
dee91e82
DE
6602static void
6603load_full_comp_unit_reader (const struct die_reader_specs *reader,
6604 gdb_byte *info_ptr,
6605 struct die_info *comp_unit_die,
6606 int has_children,
6607 void *data)
6608{
6609 struct dwarf2_cu *cu = reader->cu;
95554aad 6610 enum language *language_ptr = data;
6caca83c 6611
dee91e82
DE
6612 gdb_assert (cu->die_hash == NULL);
6613 cu->die_hash =
6614 htab_create_alloc_ex (cu->header.length / 12,
6615 die_hash,
6616 die_eq,
6617 NULL,
6618 &cu->comp_unit_obstack,
6619 hashtab_obstack_allocate,
6620 dummy_obstack_deallocate);
e142c38c 6621
dee91e82
DE
6622 if (has_children)
6623 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6624 &info_ptr, comp_unit_die);
6625 cu->dies = comp_unit_die;
6626 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6627
6628 /* We try not to read any attributes in this function, because not
9cdd5dbd 6629 all CUs needed for references have been loaded yet, and symbol
10b3939b 6630 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6631 or we won't be able to build types correctly.
6632 Similarly, if we do not read the producer, we can not apply
6633 producer-specific interpretation. */
95554aad 6634 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6635}
10b3939b 6636
dee91e82 6637/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6638
dee91e82 6639static void
95554aad
TT
6640load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6641 enum language pretend_language)
dee91e82 6642{
3019eac3 6643 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6644
f4dc4d17
DE
6645 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6646 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6647}
6648
3da10d80
KS
6649/* Add a DIE to the delayed physname list. */
6650
6651static void
6652add_to_method_list (struct type *type, int fnfield_index, int index,
6653 const char *name, struct die_info *die,
6654 struct dwarf2_cu *cu)
6655{
6656 struct delayed_method_info mi;
6657 mi.type = type;
6658 mi.fnfield_index = fnfield_index;
6659 mi.index = index;
6660 mi.name = name;
6661 mi.die = die;
6662 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6663}
6664
6665/* A cleanup for freeing the delayed method list. */
6666
6667static void
6668free_delayed_list (void *ptr)
6669{
6670 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6671 if (cu->method_list != NULL)
6672 {
6673 VEC_free (delayed_method_info, cu->method_list);
6674 cu->method_list = NULL;
6675 }
6676}
6677
6678/* Compute the physnames of any methods on the CU's method list.
6679
6680 The computation of method physnames is delayed in order to avoid the
6681 (bad) condition that one of the method's formal parameters is of an as yet
6682 incomplete type. */
6683
6684static void
6685compute_delayed_physnames (struct dwarf2_cu *cu)
6686{
6687 int i;
6688 struct delayed_method_info *mi;
6689 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6690 {
1d06ead6 6691 const char *physname;
3da10d80
KS
6692 struct fn_fieldlist *fn_flp
6693 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6694 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6695 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6696 }
6697}
6698
a766d390
DE
6699/* Go objects should be embedded in a DW_TAG_module DIE,
6700 and it's not clear if/how imported objects will appear.
6701 To keep Go support simple until that's worked out,
6702 go back through what we've read and create something usable.
6703 We could do this while processing each DIE, and feels kinda cleaner,
6704 but that way is more invasive.
6705 This is to, for example, allow the user to type "p var" or "b main"
6706 without having to specify the package name, and allow lookups
6707 of module.object to work in contexts that use the expression
6708 parser. */
6709
6710static void
6711fixup_go_packaging (struct dwarf2_cu *cu)
6712{
6713 char *package_name = NULL;
6714 struct pending *list;
6715 int i;
6716
6717 for (list = global_symbols; list != NULL; list = list->next)
6718 {
6719 for (i = 0; i < list->nsyms; ++i)
6720 {
6721 struct symbol *sym = list->symbol[i];
6722
6723 if (SYMBOL_LANGUAGE (sym) == language_go
6724 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6725 {
6726 char *this_package_name = go_symbol_package_name (sym);
6727
6728 if (this_package_name == NULL)
6729 continue;
6730 if (package_name == NULL)
6731 package_name = this_package_name;
6732 else
6733 {
6734 if (strcmp (package_name, this_package_name) != 0)
6735 complaint (&symfile_complaints,
6736 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17
TT
6737 (SYMBOL_SYMTAB (sym)
6738 && SYMBOL_SYMTAB (sym)->filename
6739 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6740 : cu->objfile->name),
6741 this_package_name, package_name);
6742 xfree (this_package_name);
6743 }
6744 }
6745 }
6746 }
6747
6748 if (package_name != NULL)
6749 {
6750 struct objfile *objfile = cu->objfile;
6751 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6752 package_name, objfile);
6753 struct symbol *sym;
6754
6755 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6756
6757 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6758 SYMBOL_SET_LANGUAGE (sym, language_go);
6759 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6760 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6761 e.g., "main" finds the "main" module and not C's main(). */
6762 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6763 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6764 SYMBOL_TYPE (sym) = type;
6765
6766 add_symbol_to_list (sym, &global_symbols);
6767
6768 xfree (package_name);
6769 }
6770}
6771
95554aad
TT
6772static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6773
6774/* Return the symtab for PER_CU. This works properly regardless of
6775 whether we're using the index or psymtabs. */
6776
6777static struct symtab *
6778get_symtab (struct dwarf2_per_cu_data *per_cu)
6779{
6780 return (dwarf2_per_objfile->using_index
6781 ? per_cu->v.quick->symtab
6782 : per_cu->v.psymtab->symtab);
6783}
6784
6785/* A helper function for computing the list of all symbol tables
6786 included by PER_CU. */
6787
6788static void
6789recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6790 htab_t all_children,
6791 struct dwarf2_per_cu_data *per_cu)
6792{
6793 void **slot;
6794 int ix;
6795 struct dwarf2_per_cu_data *iter;
6796
6797 slot = htab_find_slot (all_children, per_cu, INSERT);
6798 if (*slot != NULL)
6799 {
6800 /* This inclusion and its children have been processed. */
6801 return;
6802 }
6803
6804 *slot = per_cu;
6805 /* Only add a CU if it has a symbol table. */
6806 if (get_symtab (per_cu) != NULL)
6807 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6808
6809 for (ix = 0;
f4dc4d17 6810 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6811 ++ix)
6812 recursively_compute_inclusions (result, all_children, iter);
6813}
6814
6815/* Compute the symtab 'includes' fields for the symtab related to
6816 PER_CU. */
6817
6818static void
6819compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6820{
f4dc4d17
DE
6821 gdb_assert (! per_cu->is_debug_types);
6822
6823 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6824 {
6825 int ix, len;
6826 struct dwarf2_per_cu_data *iter;
6827 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6828 htab_t all_children;
6829 struct symtab *symtab = get_symtab (per_cu);
6830
6831 /* If we don't have a symtab, we can just skip this case. */
6832 if (symtab == NULL)
6833 return;
6834
6835 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6836 NULL, xcalloc, xfree);
6837
6838 for (ix = 0;
f4dc4d17 6839 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6840 ix, iter);
6841 ++ix)
6842 recursively_compute_inclusions (&result_children, all_children, iter);
6843
6844 /* Now we have a transitive closure of all the included CUs, so
6845 we can convert it to a list of symtabs. */
6846 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6847 symtab->includes
6848 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6849 (len + 1) * sizeof (struct symtab *));
6850 for (ix = 0;
6851 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6852 ++ix)
6853 symtab->includes[ix] = get_symtab (iter);
6854 symtab->includes[len] = NULL;
6855
6856 VEC_free (dwarf2_per_cu_ptr, result_children);
6857 htab_delete (all_children);
6858 }
6859}
6860
6861/* Compute the 'includes' field for the symtabs of all the CUs we just
6862 read. */
6863
6864static void
6865process_cu_includes (void)
6866{
6867 int ix;
6868 struct dwarf2_per_cu_data *iter;
6869
6870 for (ix = 0;
6871 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6872 ix, iter);
6873 ++ix)
f4dc4d17
DE
6874 {
6875 if (! iter->is_debug_types)
6876 compute_symtab_includes (iter);
6877 }
95554aad
TT
6878
6879 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6880}
6881
9cdd5dbd 6882/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6883 already been loaded into memory. */
6884
6885static void
95554aad
TT
6886process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6887 enum language pretend_language)
10b3939b 6888{
10b3939b 6889 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6890 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6891 CORE_ADDR lowpc, highpc;
6892 struct symtab *symtab;
3da10d80 6893 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6894 CORE_ADDR baseaddr;
4359dff1 6895 struct block *static_block;
10b3939b
DJ
6896
6897 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6898
10b3939b
DJ
6899 buildsym_init ();
6900 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6901 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6902
6903 cu->list_in_scope = &file_symbols;
c906108c 6904
95554aad
TT
6905 cu->language = pretend_language;
6906 cu->language_defn = language_def (cu->language);
6907
c906108c 6908 /* Do line number decoding in read_file_scope () */
10b3939b 6909 process_die (cu->dies, cu);
c906108c 6910
a766d390
DE
6911 /* For now fudge the Go package. */
6912 if (cu->language == language_go)
6913 fixup_go_packaging (cu);
6914
3da10d80
KS
6915 /* Now that we have processed all the DIEs in the CU, all the types
6916 should be complete, and it should now be safe to compute all of the
6917 physnames. */
6918 compute_delayed_physnames (cu);
6919 do_cleanups (delayed_list_cleanup);
6920
fae299cd
DC
6921 /* Some compilers don't define a DW_AT_high_pc attribute for the
6922 compilation unit. If the DW_AT_high_pc is missing, synthesize
6923 it, by scanning the DIE's below the compilation unit. */
10b3939b 6924 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6925
36586728
TT
6926 static_block
6927 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6928 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6929
6930 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6931 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6932 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6933 addrmap to help ensure it has an accurate map of pc values belonging to
6934 this comp unit. */
6935 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6936
6937 symtab = end_symtab_from_static_block (static_block, objfile,
6938 SECT_OFF_TEXT (objfile), 0);
c906108c 6939
8be455d7 6940 if (symtab != NULL)
c906108c 6941 {
df15bd07 6942 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6943
8be455d7
JK
6944 /* Set symtab language to language from DW_AT_language. If the
6945 compilation is from a C file generated by language preprocessors, do
6946 not set the language if it was already deduced by start_subfile. */
6947 if (!(cu->language == language_c && symtab->language != language_c))
6948 symtab->language = cu->language;
6949
6950 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6951 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6952 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6953 there were bugs in prologue debug info, fixed later in GCC-4.5
6954 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6955
6956 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6957 needed, it would be wrong due to missing DW_AT_producer there.
6958
6959 Still one can confuse GDB by using non-standard GCC compilation
6960 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6961 */
ab260dad 6962 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6963 symtab->locations_valid = 1;
e0d00bc7
JK
6964
6965 if (gcc_4_minor >= 5)
6966 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6967
6968 symtab->call_site_htab = cu->call_site_htab;
c906108c 6969 }
9291a0cd
TT
6970
6971 if (dwarf2_per_objfile->using_index)
6972 per_cu->v.quick->symtab = symtab;
6973 else
6974 {
6975 struct partial_symtab *pst = per_cu->v.psymtab;
6976 pst->symtab = symtab;
6977 pst->readin = 1;
6978 }
c906108c 6979
95554aad
TT
6980 /* Push it for inclusion processing later. */
6981 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6982
c906108c 6983 do_cleanups (back_to);
f4dc4d17 6984}
45cfd468 6985
f4dc4d17
DE
6986/* Generate full symbol information for type unit PER_CU, whose DIEs have
6987 already been loaded into memory. */
6988
6989static void
6990process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6991 enum language pretend_language)
6992{
6993 struct dwarf2_cu *cu = per_cu->cu;
6994 struct objfile *objfile = per_cu->objfile;
6995 struct symtab *symtab;
6996 struct cleanup *back_to, *delayed_list_cleanup;
6997
6998 buildsym_init ();
6999 back_to = make_cleanup (really_free_pendings, NULL);
7000 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7001
7002 cu->list_in_scope = &file_symbols;
7003
7004 cu->language = pretend_language;
7005 cu->language_defn = language_def (cu->language);
7006
7007 /* The symbol tables are set up in read_type_unit_scope. */
7008 process_die (cu->dies, cu);
7009
7010 /* For now fudge the Go package. */
7011 if (cu->language == language_go)
7012 fixup_go_packaging (cu);
7013
7014 /* Now that we have processed all the DIEs in the CU, all the types
7015 should be complete, and it should now be safe to compute all of the
7016 physnames. */
7017 compute_delayed_physnames (cu);
7018 do_cleanups (delayed_list_cleanup);
7019
7020 /* TUs share symbol tables.
7021 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7022 of it with end_expandable_symtab. Otherwise, complete the addition of
7023 this TU's symbols to the existing symtab. */
f4dc4d17 7024 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7025 {
f4dc4d17
DE
7026 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7027 per_cu->s.type_unit_group->primary_symtab = symtab;
7028
7029 if (symtab != NULL)
7030 {
7031 /* Set symtab language to language from DW_AT_language. If the
7032 compilation is from a C file generated by language preprocessors,
7033 do not set the language if it was already deduced by
7034 start_subfile. */
7035 if (!(cu->language == language_c && symtab->language != language_c))
7036 symtab->language = cu->language;
7037 }
7038 }
7039 else
7040 {
7041 augment_type_symtab (objfile,
7042 per_cu->s.type_unit_group->primary_symtab);
7043 symtab = per_cu->s.type_unit_group->primary_symtab;
7044 }
7045
7046 if (dwarf2_per_objfile->using_index)
7047 per_cu->v.quick->symtab = symtab;
7048 else
7049 {
7050 struct partial_symtab *pst = per_cu->v.psymtab;
7051 pst->symtab = symtab;
7052 pst->readin = 1;
45cfd468 7053 }
f4dc4d17
DE
7054
7055 do_cleanups (back_to);
c906108c
SS
7056}
7057
95554aad
TT
7058/* Process an imported unit DIE. */
7059
7060static void
7061process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7062{
7063 struct attribute *attr;
7064
f4dc4d17
DE
7065 /* For now we don't handle imported units in type units. */
7066 if (cu->per_cu->is_debug_types)
7067 {
7068 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7069 " supported in type units [in module %s]"),
7070 cu->objfile->name);
7071 }
7072
95554aad
TT
7073 attr = dwarf2_attr (die, DW_AT_import, cu);
7074 if (attr != NULL)
7075 {
7076 struct dwarf2_per_cu_data *per_cu;
7077 struct symtab *imported_symtab;
7078 sect_offset offset;
36586728 7079 int is_dwz;
95554aad
TT
7080
7081 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7082 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7083 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7084
7085 /* Queue the unit, if needed. */
7086 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7087 load_full_comp_unit (per_cu, cu->language);
7088
f4dc4d17 7089 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7090 per_cu);
7091 }
7092}
7093
c906108c
SS
7094/* Process a die and its children. */
7095
7096static void
e7c27a73 7097process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7098{
7099 switch (die->tag)
7100 {
7101 case DW_TAG_padding:
7102 break;
7103 case DW_TAG_compile_unit:
95554aad 7104 case DW_TAG_partial_unit:
e7c27a73 7105 read_file_scope (die, cu);
c906108c 7106 break;
348e048f
DE
7107 case DW_TAG_type_unit:
7108 read_type_unit_scope (die, cu);
7109 break;
c906108c 7110 case DW_TAG_subprogram:
c906108c 7111 case DW_TAG_inlined_subroutine:
edb3359d 7112 read_func_scope (die, cu);
c906108c
SS
7113 break;
7114 case DW_TAG_lexical_block:
14898363
L
7115 case DW_TAG_try_block:
7116 case DW_TAG_catch_block:
e7c27a73 7117 read_lexical_block_scope (die, cu);
c906108c 7118 break;
96408a79
SA
7119 case DW_TAG_GNU_call_site:
7120 read_call_site_scope (die, cu);
7121 break;
c906108c 7122 case DW_TAG_class_type:
680b30c7 7123 case DW_TAG_interface_type:
c906108c
SS
7124 case DW_TAG_structure_type:
7125 case DW_TAG_union_type:
134d01f1 7126 process_structure_scope (die, cu);
c906108c
SS
7127 break;
7128 case DW_TAG_enumeration_type:
134d01f1 7129 process_enumeration_scope (die, cu);
c906108c 7130 break;
134d01f1 7131
f792889a
DJ
7132 /* These dies have a type, but processing them does not create
7133 a symbol or recurse to process the children. Therefore we can
7134 read them on-demand through read_type_die. */
c906108c 7135 case DW_TAG_subroutine_type:
72019c9c 7136 case DW_TAG_set_type:
c906108c 7137 case DW_TAG_array_type:
c906108c 7138 case DW_TAG_pointer_type:
c906108c 7139 case DW_TAG_ptr_to_member_type:
c906108c 7140 case DW_TAG_reference_type:
c906108c 7141 case DW_TAG_string_type:
c906108c 7142 break;
134d01f1 7143
c906108c 7144 case DW_TAG_base_type:
a02abb62 7145 case DW_TAG_subrange_type:
cb249c71 7146 case DW_TAG_typedef:
134d01f1
DJ
7147 /* Add a typedef symbol for the type definition, if it has a
7148 DW_AT_name. */
f792889a 7149 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7150 break;
c906108c 7151 case DW_TAG_common_block:
e7c27a73 7152 read_common_block (die, cu);
c906108c
SS
7153 break;
7154 case DW_TAG_common_inclusion:
7155 break;
d9fa45fe 7156 case DW_TAG_namespace:
63d06c5c 7157 processing_has_namespace_info = 1;
e7c27a73 7158 read_namespace (die, cu);
d9fa45fe 7159 break;
5d7cb8df 7160 case DW_TAG_module:
f55ee35c 7161 processing_has_namespace_info = 1;
5d7cb8df
JK
7162 read_module (die, cu);
7163 break;
d9fa45fe
DC
7164 case DW_TAG_imported_declaration:
7165 case DW_TAG_imported_module:
63d06c5c 7166 processing_has_namespace_info = 1;
27aa8d6a
SW
7167 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7168 || cu->language != language_fortran))
7169 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7170 dwarf_tag_name (die->tag));
7171 read_import_statement (die, cu);
d9fa45fe 7172 break;
95554aad
TT
7173
7174 case DW_TAG_imported_unit:
7175 process_imported_unit_die (die, cu);
7176 break;
7177
c906108c 7178 default:
e7c27a73 7179 new_symbol (die, NULL, cu);
c906108c
SS
7180 break;
7181 }
7182}
7183
94af9270
KS
7184/* A helper function for dwarf2_compute_name which determines whether DIE
7185 needs to have the name of the scope prepended to the name listed in the
7186 die. */
7187
7188static int
7189die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7190{
1c809c68
TT
7191 struct attribute *attr;
7192
94af9270
KS
7193 switch (die->tag)
7194 {
7195 case DW_TAG_namespace:
7196 case DW_TAG_typedef:
7197 case DW_TAG_class_type:
7198 case DW_TAG_interface_type:
7199 case DW_TAG_structure_type:
7200 case DW_TAG_union_type:
7201 case DW_TAG_enumeration_type:
7202 case DW_TAG_enumerator:
7203 case DW_TAG_subprogram:
7204 case DW_TAG_member:
7205 return 1;
7206
7207 case DW_TAG_variable:
c2b0a229 7208 case DW_TAG_constant:
94af9270
KS
7209 /* We only need to prefix "globally" visible variables. These include
7210 any variable marked with DW_AT_external or any variable that
7211 lives in a namespace. [Variables in anonymous namespaces
7212 require prefixing, but they are not DW_AT_external.] */
7213
7214 if (dwarf2_attr (die, DW_AT_specification, cu))
7215 {
7216 struct dwarf2_cu *spec_cu = cu;
9a619af0 7217
94af9270
KS
7218 return die_needs_namespace (die_specification (die, &spec_cu),
7219 spec_cu);
7220 }
7221
1c809c68 7222 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7223 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7224 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7225 return 0;
7226 /* A variable in a lexical block of some kind does not need a
7227 namespace, even though in C++ such variables may be external
7228 and have a mangled name. */
7229 if (die->parent->tag == DW_TAG_lexical_block
7230 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7231 || die->parent->tag == DW_TAG_catch_block
7232 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7233 return 0;
7234 return 1;
94af9270
KS
7235
7236 default:
7237 return 0;
7238 }
7239}
7240
98bfdba5
PA
7241/* Retrieve the last character from a mem_file. */
7242
7243static void
7244do_ui_file_peek_last (void *object, const char *buffer, long length)
7245{
7246 char *last_char_p = (char *) object;
7247
7248 if (length > 0)
7249 *last_char_p = buffer[length - 1];
7250}
7251
94af9270 7252/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7253 compute the physname for the object, which include a method's:
7254 - formal parameters (C++/Java),
7255 - receiver type (Go),
7256 - return type (Java).
7257
7258 The term "physname" is a bit confusing.
7259 For C++, for example, it is the demangled name.
7260 For Go, for example, it's the mangled name.
94af9270 7261
af6b7be1
JB
7262 For Ada, return the DIE's linkage name rather than the fully qualified
7263 name. PHYSNAME is ignored..
7264
94af9270
KS
7265 The result is allocated on the objfile_obstack and canonicalized. */
7266
7267static const char *
7268dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7269 int physname)
7270{
bb5ed363
DE
7271 struct objfile *objfile = cu->objfile;
7272
94af9270
KS
7273 if (name == NULL)
7274 name = dwarf2_name (die, cu);
7275
f55ee35c
JK
7276 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7277 compute it by typename_concat inside GDB. */
7278 if (cu->language == language_ada
7279 || (cu->language == language_fortran && physname))
7280 {
7281 /* For Ada unit, we prefer the linkage name over the name, as
7282 the former contains the exported name, which the user expects
7283 to be able to reference. Ideally, we want the user to be able
7284 to reference this entity using either natural or linkage name,
7285 but we haven't started looking at this enhancement yet. */
7286 struct attribute *attr;
7287
7288 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7289 if (attr == NULL)
7290 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7291 if (attr && DW_STRING (attr))
7292 return DW_STRING (attr);
7293 }
7294
94af9270
KS
7295 /* These are the only languages we know how to qualify names in. */
7296 if (name != NULL
f55ee35c
JK
7297 && (cu->language == language_cplus || cu->language == language_java
7298 || cu->language == language_fortran))
94af9270
KS
7299 {
7300 if (die_needs_namespace (die, cu))
7301 {
7302 long length;
0d5cff50 7303 const char *prefix;
94af9270
KS
7304 struct ui_file *buf;
7305
7306 prefix = determine_prefix (die, cu);
7307 buf = mem_fileopen ();
7308 if (*prefix != '\0')
7309 {
f55ee35c
JK
7310 char *prefixed_name = typename_concat (NULL, prefix, name,
7311 physname, cu);
9a619af0 7312
94af9270
KS
7313 fputs_unfiltered (prefixed_name, buf);
7314 xfree (prefixed_name);
7315 }
7316 else
62d5b8da 7317 fputs_unfiltered (name, buf);
94af9270 7318
98bfdba5
PA
7319 /* Template parameters may be specified in the DIE's DW_AT_name, or
7320 as children with DW_TAG_template_type_param or
7321 DW_TAG_value_type_param. If the latter, add them to the name
7322 here. If the name already has template parameters, then
7323 skip this step; some versions of GCC emit both, and
7324 it is more efficient to use the pre-computed name.
7325
7326 Something to keep in mind about this process: it is very
7327 unlikely, or in some cases downright impossible, to produce
7328 something that will match the mangled name of a function.
7329 If the definition of the function has the same debug info,
7330 we should be able to match up with it anyway. But fallbacks
7331 using the minimal symbol, for instance to find a method
7332 implemented in a stripped copy of libstdc++, will not work.
7333 If we do not have debug info for the definition, we will have to
7334 match them up some other way.
7335
7336 When we do name matching there is a related problem with function
7337 templates; two instantiated function templates are allowed to
7338 differ only by their return types, which we do not add here. */
7339
7340 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7341 {
7342 struct attribute *attr;
7343 struct die_info *child;
7344 int first = 1;
7345
7346 die->building_fullname = 1;
7347
7348 for (child = die->child; child != NULL; child = child->sibling)
7349 {
7350 struct type *type;
12df843f 7351 LONGEST value;
98bfdba5
PA
7352 gdb_byte *bytes;
7353 struct dwarf2_locexpr_baton *baton;
7354 struct value *v;
7355
7356 if (child->tag != DW_TAG_template_type_param
7357 && child->tag != DW_TAG_template_value_param)
7358 continue;
7359
7360 if (first)
7361 {
7362 fputs_unfiltered ("<", buf);
7363 first = 0;
7364 }
7365 else
7366 fputs_unfiltered (", ", buf);
7367
7368 attr = dwarf2_attr (child, DW_AT_type, cu);
7369 if (attr == NULL)
7370 {
7371 complaint (&symfile_complaints,
7372 _("template parameter missing DW_AT_type"));
7373 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7374 continue;
7375 }
7376 type = die_type (child, cu);
7377
7378 if (child->tag == DW_TAG_template_type_param)
7379 {
79d43c61 7380 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7381 continue;
7382 }
7383
7384 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7385 if (attr == NULL)
7386 {
7387 complaint (&symfile_complaints,
3e43a32a
MS
7388 _("template parameter missing "
7389 "DW_AT_const_value"));
98bfdba5
PA
7390 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7391 continue;
7392 }
7393
7394 dwarf2_const_value_attr (attr, type, name,
7395 &cu->comp_unit_obstack, cu,
7396 &value, &bytes, &baton);
7397
7398 if (TYPE_NOSIGN (type))
7399 /* GDB prints characters as NUMBER 'CHAR'. If that's
7400 changed, this can use value_print instead. */
7401 c_printchar (value, type, buf);
7402 else
7403 {
7404 struct value_print_options opts;
7405
7406 if (baton != NULL)
7407 v = dwarf2_evaluate_loc_desc (type, NULL,
7408 baton->data,
7409 baton->size,
7410 baton->per_cu);
7411 else if (bytes != NULL)
7412 {
7413 v = allocate_value (type);
7414 memcpy (value_contents_writeable (v), bytes,
7415 TYPE_LENGTH (type));
7416 }
7417 else
7418 v = value_from_longest (type, value);
7419
3e43a32a
MS
7420 /* Specify decimal so that we do not depend on
7421 the radix. */
98bfdba5
PA
7422 get_formatted_print_options (&opts, 'd');
7423 opts.raw = 1;
7424 value_print (v, buf, &opts);
7425 release_value (v);
7426 value_free (v);
7427 }
7428 }
7429
7430 die->building_fullname = 0;
7431
7432 if (!first)
7433 {
7434 /* Close the argument list, with a space if necessary
7435 (nested templates). */
7436 char last_char = '\0';
7437 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7438 if (last_char == '>')
7439 fputs_unfiltered (" >", buf);
7440 else
7441 fputs_unfiltered (">", buf);
7442 }
7443 }
7444
94af9270
KS
7445 /* For Java and C++ methods, append formal parameter type
7446 information, if PHYSNAME. */
6e70227d 7447
94af9270
KS
7448 if (physname && die->tag == DW_TAG_subprogram
7449 && (cu->language == language_cplus
7450 || cu->language == language_java))
7451 {
7452 struct type *type = read_type_die (die, cu);
7453
79d43c61
TT
7454 c_type_print_args (type, buf, 1, cu->language,
7455 &type_print_raw_options);
94af9270
KS
7456
7457 if (cu->language == language_java)
7458 {
7459 /* For java, we must append the return type to method
0963b4bd 7460 names. */
94af9270
KS
7461 if (die->tag == DW_TAG_subprogram)
7462 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7463 0, 0, &type_print_raw_options);
94af9270
KS
7464 }
7465 else if (cu->language == language_cplus)
7466 {
60430eff
DJ
7467 /* Assume that an artificial first parameter is
7468 "this", but do not crash if it is not. RealView
7469 marks unnamed (and thus unused) parameters as
7470 artificial; there is no way to differentiate
7471 the two cases. */
94af9270
KS
7472 if (TYPE_NFIELDS (type) > 0
7473 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7474 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7475 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7476 0))))
94af9270
KS
7477 fputs_unfiltered (" const", buf);
7478 }
7479 }
7480
bb5ed363 7481 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7482 &length);
7483 ui_file_delete (buf);
7484
7485 if (cu->language == language_cplus)
7486 {
7487 char *cname
7488 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7489 &objfile->objfile_obstack);
9a619af0 7490
94af9270
KS
7491 if (cname != NULL)
7492 name = cname;
7493 }
7494 }
7495 }
7496
7497 return name;
7498}
7499
0114d602
DJ
7500/* Return the fully qualified name of DIE, based on its DW_AT_name.
7501 If scope qualifiers are appropriate they will be added. The result
7502 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7503 not have a name. NAME may either be from a previous call to
7504 dwarf2_name or NULL.
7505
0963b4bd 7506 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7507
7508static const char *
94af9270 7509dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7510{
94af9270
KS
7511 return dwarf2_compute_name (name, die, cu, 0);
7512}
0114d602 7513
94af9270
KS
7514/* Construct a physname for the given DIE in CU. NAME may either be
7515 from a previous call to dwarf2_name or NULL. The result will be
7516 allocated on the objfile_objstack or NULL if the DIE does not have a
7517 name.
0114d602 7518
94af9270 7519 The output string will be canonicalized (if C++/Java). */
0114d602 7520
94af9270
KS
7521static const char *
7522dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7523{
bb5ed363 7524 struct objfile *objfile = cu->objfile;
900e11f9
JK
7525 struct attribute *attr;
7526 const char *retval, *mangled = NULL, *canon = NULL;
7527 struct cleanup *back_to;
7528 int need_copy = 1;
7529
7530 /* In this case dwarf2_compute_name is just a shortcut not building anything
7531 on its own. */
7532 if (!die_needs_namespace (die, cu))
7533 return dwarf2_compute_name (name, die, cu, 1);
7534
7535 back_to = make_cleanup (null_cleanup, NULL);
7536
7537 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7538 if (!attr)
7539 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7540
7541 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7542 has computed. */
7543 if (attr && DW_STRING (attr))
7544 {
7545 char *demangled;
7546
7547 mangled = DW_STRING (attr);
7548
7549 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7550 type. It is easier for GDB users to search for such functions as
7551 `name(params)' than `long name(params)'. In such case the minimal
7552 symbol names do not match the full symbol names but for template
7553 functions there is never a need to look up their definition from their
7554 declaration so the only disadvantage remains the minimal symbol
7555 variant `long name(params)' does not have the proper inferior type.
7556 */
7557
a766d390
DE
7558 if (cu->language == language_go)
7559 {
7560 /* This is a lie, but we already lie to the caller new_symbol_full.
7561 new_symbol_full assumes we return the mangled name.
7562 This just undoes that lie until things are cleaned up. */
7563 demangled = NULL;
7564 }
7565 else
7566 {
7567 demangled = cplus_demangle (mangled,
7568 (DMGL_PARAMS | DMGL_ANSI
7569 | (cu->language == language_java
7570 ? DMGL_JAVA | DMGL_RET_POSTFIX
7571 : DMGL_RET_DROP)));
7572 }
900e11f9
JK
7573 if (demangled)
7574 {
7575 make_cleanup (xfree, demangled);
7576 canon = demangled;
7577 }
7578 else
7579 {
7580 canon = mangled;
7581 need_copy = 0;
7582 }
7583 }
7584
7585 if (canon == NULL || check_physname)
7586 {
7587 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7588
7589 if (canon != NULL && strcmp (physname, canon) != 0)
7590 {
7591 /* It may not mean a bug in GDB. The compiler could also
7592 compute DW_AT_linkage_name incorrectly. But in such case
7593 GDB would need to be bug-to-bug compatible. */
7594
7595 complaint (&symfile_complaints,
7596 _("Computed physname <%s> does not match demangled <%s> "
7597 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7598 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7599
7600 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7601 is available here - over computed PHYSNAME. It is safer
7602 against both buggy GDB and buggy compilers. */
7603
7604 retval = canon;
7605 }
7606 else
7607 {
7608 retval = physname;
7609 need_copy = 0;
7610 }
7611 }
7612 else
7613 retval = canon;
7614
7615 if (need_copy)
7616 retval = obsavestring (retval, strlen (retval),
bb5ed363 7617 &objfile->objfile_obstack);
900e11f9
JK
7618
7619 do_cleanups (back_to);
7620 return retval;
0114d602
DJ
7621}
7622
27aa8d6a
SW
7623/* Read the import statement specified by the given die and record it. */
7624
7625static void
7626read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7627{
bb5ed363 7628 struct objfile *objfile = cu->objfile;
27aa8d6a 7629 struct attribute *import_attr;
32019081 7630 struct die_info *imported_die, *child_die;
de4affc9 7631 struct dwarf2_cu *imported_cu;
27aa8d6a 7632 const char *imported_name;
794684b6 7633 const char *imported_name_prefix;
13387711
SW
7634 const char *canonical_name;
7635 const char *import_alias;
7636 const char *imported_declaration = NULL;
794684b6 7637 const char *import_prefix;
32019081
JK
7638 VEC (const_char_ptr) *excludes = NULL;
7639 struct cleanup *cleanups;
13387711
SW
7640
7641 char *temp;
27aa8d6a
SW
7642
7643 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7644 if (import_attr == NULL)
7645 {
7646 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7647 dwarf_tag_name (die->tag));
7648 return;
7649 }
7650
de4affc9
CC
7651 imported_cu = cu;
7652 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7653 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7654 if (imported_name == NULL)
7655 {
7656 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7657
7658 The import in the following code:
7659 namespace A
7660 {
7661 typedef int B;
7662 }
7663
7664 int main ()
7665 {
7666 using A::B;
7667 B b;
7668 return b;
7669 }
7670
7671 ...
7672 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7673 <52> DW_AT_decl_file : 1
7674 <53> DW_AT_decl_line : 6
7675 <54> DW_AT_import : <0x75>
7676 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7677 <59> DW_AT_name : B
7678 <5b> DW_AT_decl_file : 1
7679 <5c> DW_AT_decl_line : 2
7680 <5d> DW_AT_type : <0x6e>
7681 ...
7682 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7683 <76> DW_AT_byte_size : 4
7684 <77> DW_AT_encoding : 5 (signed)
7685
7686 imports the wrong die ( 0x75 instead of 0x58 ).
7687 This case will be ignored until the gcc bug is fixed. */
7688 return;
7689 }
7690
82856980
SW
7691 /* Figure out the local name after import. */
7692 import_alias = dwarf2_name (die, cu);
27aa8d6a 7693
794684b6
SW
7694 /* Figure out where the statement is being imported to. */
7695 import_prefix = determine_prefix (die, cu);
7696
7697 /* Figure out what the scope of the imported die is and prepend it
7698 to the name of the imported die. */
de4affc9 7699 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7700
f55ee35c
JK
7701 if (imported_die->tag != DW_TAG_namespace
7702 && imported_die->tag != DW_TAG_module)
794684b6 7703 {
13387711
SW
7704 imported_declaration = imported_name;
7705 canonical_name = imported_name_prefix;
794684b6 7706 }
13387711 7707 else if (strlen (imported_name_prefix) > 0)
794684b6 7708 {
13387711
SW
7709 temp = alloca (strlen (imported_name_prefix)
7710 + 2 + strlen (imported_name) + 1);
7711 strcpy (temp, imported_name_prefix);
7712 strcat (temp, "::");
7713 strcat (temp, imported_name);
7714 canonical_name = temp;
794684b6 7715 }
13387711
SW
7716 else
7717 canonical_name = imported_name;
794684b6 7718
32019081
JK
7719 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7720
7721 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7722 for (child_die = die->child; child_die && child_die->tag;
7723 child_die = sibling_die (child_die))
7724 {
7725 /* DWARF-4: A Fortran use statement with a “rename list” may be
7726 represented by an imported module entry with an import attribute
7727 referring to the module and owned entries corresponding to those
7728 entities that are renamed as part of being imported. */
7729
7730 if (child_die->tag != DW_TAG_imported_declaration)
7731 {
7732 complaint (&symfile_complaints,
7733 _("child DW_TAG_imported_declaration expected "
7734 "- DIE at 0x%x [in module %s]"),
b64f50a1 7735 child_die->offset.sect_off, objfile->name);
32019081
JK
7736 continue;
7737 }
7738
7739 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7740 if (import_attr == NULL)
7741 {
7742 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7743 dwarf_tag_name (child_die->tag));
7744 continue;
7745 }
7746
7747 imported_cu = cu;
7748 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7749 &imported_cu);
7750 imported_name = dwarf2_name (imported_die, imported_cu);
7751 if (imported_name == NULL)
7752 {
7753 complaint (&symfile_complaints,
7754 _("child DW_TAG_imported_declaration has unknown "
7755 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7756 child_die->offset.sect_off, objfile->name);
32019081
JK
7757 continue;
7758 }
7759
7760 VEC_safe_push (const_char_ptr, excludes, imported_name);
7761
7762 process_die (child_die, cu);
7763 }
7764
c0cc3a76
SW
7765 cp_add_using_directive (import_prefix,
7766 canonical_name,
7767 import_alias,
13387711 7768 imported_declaration,
32019081 7769 excludes,
bb5ed363 7770 &objfile->objfile_obstack);
32019081
JK
7771
7772 do_cleanups (cleanups);
27aa8d6a
SW
7773}
7774
f4dc4d17 7775/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7776
cb1df416
DJ
7777static void
7778free_cu_line_header (void *arg)
7779{
7780 struct dwarf2_cu *cu = arg;
7781
7782 free_line_header (cu->line_header);
7783 cu->line_header = NULL;
7784}
7785
1b80a9fa
JK
7786/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7787 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7788 this, it was first present in GCC release 4.3.0. */
7789
7790static int
7791producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7792{
7793 if (!cu->checked_producer)
7794 check_producer (cu);
7795
7796 return cu->producer_is_gcc_lt_4_3;
7797}
7798
9291a0cd
TT
7799static void
7800find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7801 char **name, char **comp_dir)
7802{
7803 struct attribute *attr;
7804
7805 *name = NULL;
7806 *comp_dir = NULL;
7807
7808 /* Find the filename. Do not use dwarf2_name here, since the filename
7809 is not a source language identifier. */
7810 attr = dwarf2_attr (die, DW_AT_name, cu);
7811 if (attr)
7812 {
7813 *name = DW_STRING (attr);
7814 }
7815
7816 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7817 if (attr)
7818 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7819 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7820 && IS_ABSOLUTE_PATH (*name))
9291a0cd
TT
7821 {
7822 *comp_dir = ldirname (*name);
7823 if (*comp_dir != NULL)
7824 make_cleanup (xfree, *comp_dir);
7825 }
7826 if (*comp_dir != NULL)
7827 {
7828 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7829 directory, get rid of it. */
7830 char *cp = strchr (*comp_dir, ':');
7831
7832 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7833 *comp_dir = cp + 1;
7834 }
7835
7836 if (*name == NULL)
7837 *name = "<unknown>";
7838}
7839
f4dc4d17
DE
7840/* Handle DW_AT_stmt_list for a compilation unit.
7841 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7842 COMP_DIR is the compilation directory.
7843 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7844
7845static void
7846handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7847 const char *comp_dir)
2ab95328
TT
7848{
7849 struct attribute *attr;
2ab95328 7850
f4dc4d17
DE
7851 gdb_assert (! cu->per_cu->is_debug_types);
7852
2ab95328
TT
7853 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7854 if (attr)
7855 {
7856 unsigned int line_offset = DW_UNSND (attr);
7857 struct line_header *line_header
3019eac3 7858 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7859
7860 if (line_header)
dee91e82
DE
7861 {
7862 cu->line_header = line_header;
7863 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7864 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7865 }
2ab95328
TT
7866 }
7867}
7868
95554aad 7869/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7870
c906108c 7871static void
e7c27a73 7872read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7873{
dee91e82 7874 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7875 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7876 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7877 CORE_ADDR highpc = ((CORE_ADDR) 0);
7878 struct attribute *attr;
e1024ff1 7879 char *name = NULL;
c906108c
SS
7880 char *comp_dir = NULL;
7881 struct die_info *child_die;
7882 bfd *abfd = objfile->obfd;
e142c38c 7883 CORE_ADDR baseaddr;
6e70227d 7884
e142c38c 7885 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7886
fae299cd 7887 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7888
7889 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7890 from finish_block. */
2acceee2 7891 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7892 lowpc = highpc;
7893 lowpc += baseaddr;
7894 highpc += baseaddr;
7895
9291a0cd 7896 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7897
95554aad 7898 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7899
f4b8a18d
KW
7900 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7901 standardised yet. As a workaround for the language detection we fall
7902 back to the DW_AT_producer string. */
7903 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7904 cu->language = language_opencl;
7905
3019eac3
DE
7906 /* Similar hack for Go. */
7907 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7908 set_cu_language (DW_LANG_Go, cu);
7909
f4dc4d17 7910 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7911
7912 /* Decode line number information if present. We do this before
7913 processing child DIEs, so that the line header table is available
7914 for DW_AT_decl_file. */
f4dc4d17 7915 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7916
7917 /* Process all dies in compilation unit. */
7918 if (die->child != NULL)
7919 {
7920 child_die = die->child;
7921 while (child_die && child_die->tag)
7922 {
7923 process_die (child_die, cu);
7924 child_die = sibling_die (child_die);
7925 }
7926 }
7927
7928 /* Decode macro information, if present. Dwarf 2 macro information
7929 refers to information in the line number info statement program
7930 header, so we can only read it if we've read the header
7931 successfully. */
7932 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7933 if (attr && cu->line_header)
7934 {
7935 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7936 complaint (&symfile_complaints,
7937 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7938
09262596 7939 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7940 }
7941 else
7942 {
7943 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7944 if (attr && cu->line_header)
7945 {
7946 unsigned int macro_offset = DW_UNSND (attr);
7947
09262596 7948 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7949 }
7950 }
7951
7952 do_cleanups (back_to);
7953}
7954
f4dc4d17
DE
7955/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7956 Create the set of symtabs used by this TU, or if this TU is sharing
7957 symtabs with another TU and the symtabs have already been created
7958 then restore those symtabs in the line header.
7959 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7960
7961static void
f4dc4d17 7962setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7963{
f4dc4d17
DE
7964 struct objfile *objfile = dwarf2_per_objfile->objfile;
7965 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7966 struct type_unit_group *tu_group;
7967 int first_time;
7968 struct line_header *lh;
3019eac3 7969 struct attribute *attr;
f4dc4d17 7970 unsigned int i, line_offset;
3019eac3 7971
f4dc4d17 7972 gdb_assert (per_cu->is_debug_types);
3019eac3 7973
f4dc4d17 7974 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 7975
f4dc4d17
DE
7976 /* If we're using .gdb_index (includes -readnow) then
7977 per_cu->s.type_unit_group may not have been set up yet. */
7978 if (per_cu->s.type_unit_group == NULL)
094b34ac 7979 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
7980 tu_group = per_cu->s.type_unit_group;
7981
7982 /* If we've already processed this stmt_list there's no real need to
7983 do it again, we could fake it and just recreate the part we need
7984 (file name,index -> symtab mapping). If data shows this optimization
7985 is useful we can do it then. */
7986 first_time = tu_group->primary_symtab == NULL;
7987
7988 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7989 debug info. */
7990 lh = NULL;
7991 if (attr != NULL)
3019eac3 7992 {
f4dc4d17
DE
7993 line_offset = DW_UNSND (attr);
7994 lh = dwarf_decode_line_header (line_offset, cu);
7995 }
7996 if (lh == NULL)
7997 {
7998 if (first_time)
7999 dwarf2_start_symtab (cu, "", NULL, 0);
8000 else
8001 {
8002 gdb_assert (tu_group->symtabs == NULL);
8003 restart_symtab (0);
8004 }
8005 /* Note: The primary symtab will get allocated at the end. */
8006 return;
3019eac3
DE
8007 }
8008
f4dc4d17
DE
8009 cu->line_header = lh;
8010 make_cleanup (free_cu_line_header, cu);
3019eac3 8011
f4dc4d17
DE
8012 if (first_time)
8013 {
8014 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8015
f4dc4d17
DE
8016 tu_group->num_symtabs = lh->num_file_names;
8017 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8018
f4dc4d17
DE
8019 for (i = 0; i < lh->num_file_names; ++i)
8020 {
8021 char *dir = NULL;
8022 struct file_entry *fe = &lh->file_names[i];
3019eac3 8023
f4dc4d17
DE
8024 if (fe->dir_index)
8025 dir = lh->include_dirs[fe->dir_index - 1];
8026 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8027
f4dc4d17
DE
8028 /* Note: We don't have to watch for the main subfile here, type units
8029 don't have DW_AT_name. */
3019eac3 8030
f4dc4d17
DE
8031 if (current_subfile->symtab == NULL)
8032 {
8033 /* NOTE: start_subfile will recognize when it's been passed
8034 a file it has already seen. So we can't assume there's a
8035 simple mapping from lh->file_names to subfiles,
8036 lh->file_names may contain dups. */
8037 current_subfile->symtab = allocate_symtab (current_subfile->name,
8038 objfile);
8039 }
8040
8041 fe->symtab = current_subfile->symtab;
8042 tu_group->symtabs[i] = fe->symtab;
8043 }
8044 }
8045 else
3019eac3 8046 {
f4dc4d17
DE
8047 restart_symtab (0);
8048
8049 for (i = 0; i < lh->num_file_names; ++i)
8050 {
8051 struct file_entry *fe = &lh->file_names[i];
8052
8053 fe->symtab = tu_group->symtabs[i];
8054 }
3019eac3
DE
8055 }
8056
f4dc4d17
DE
8057 /* The main symtab is allocated last. Type units don't have DW_AT_name
8058 so they don't have a "real" (so to speak) symtab anyway.
8059 There is later code that will assign the main symtab to all symbols
8060 that don't have one. We need to handle the case of a symbol with a
8061 missing symtab (DW_AT_decl_file) anyway. */
8062}
3019eac3 8063
f4dc4d17
DE
8064/* Process DW_TAG_type_unit.
8065 For TUs we want to skip the first top level sibling if it's not the
8066 actual type being defined by this TU. In this case the first top
8067 level sibling is there to provide context only. */
3019eac3 8068
f4dc4d17
DE
8069static void
8070read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8071{
8072 struct die_info *child_die;
3019eac3 8073
f4dc4d17
DE
8074 prepare_one_comp_unit (cu, die, language_minimal);
8075
8076 /* Initialize (or reinitialize) the machinery for building symtabs.
8077 We do this before processing child DIEs, so that the line header table
8078 is available for DW_AT_decl_file. */
8079 setup_type_unit_groups (die, cu);
8080
8081 if (die->child != NULL)
8082 {
8083 child_die = die->child;
8084 while (child_die && child_die->tag)
8085 {
8086 process_die (child_die, cu);
8087 child_die = sibling_die (child_die);
8088 }
8089 }
3019eac3
DE
8090}
8091\f
80626a55
DE
8092/* DWO/DWP files.
8093
8094 http://gcc.gnu.org/wiki/DebugFission
8095 http://gcc.gnu.org/wiki/DebugFissionDWP
8096
8097 To simplify handling of both DWO files ("object" files with the DWARF info)
8098 and DWP files (a file with the DWOs packaged up into one file), we treat
8099 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8100
8101static hashval_t
8102hash_dwo_file (const void *item)
8103{
8104 const struct dwo_file *dwo_file = item;
8105
80626a55 8106 return htab_hash_string (dwo_file->name);
3019eac3
DE
8107}
8108
8109static int
8110eq_dwo_file (const void *item_lhs, const void *item_rhs)
8111{
8112 const struct dwo_file *lhs = item_lhs;
8113 const struct dwo_file *rhs = item_rhs;
8114
80626a55 8115 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8116}
8117
8118/* Allocate a hash table for DWO files. */
8119
8120static htab_t
8121allocate_dwo_file_hash_table (void)
8122{
8123 struct objfile *objfile = dwarf2_per_objfile->objfile;
8124
8125 return htab_create_alloc_ex (41,
8126 hash_dwo_file,
8127 eq_dwo_file,
8128 NULL,
8129 &objfile->objfile_obstack,
8130 hashtab_obstack_allocate,
8131 dummy_obstack_deallocate);
8132}
8133
80626a55
DE
8134/* Lookup DWO file DWO_NAME. */
8135
8136static void **
8137lookup_dwo_file_slot (const char *dwo_name)
8138{
8139 struct dwo_file find_entry;
8140 void **slot;
8141
8142 if (dwarf2_per_objfile->dwo_files == NULL)
8143 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8144
8145 memset (&find_entry, 0, sizeof (find_entry));
8146 find_entry.name = dwo_name;
8147 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8148
8149 return slot;
8150}
8151
3019eac3
DE
8152static hashval_t
8153hash_dwo_unit (const void *item)
8154{
8155 const struct dwo_unit *dwo_unit = item;
8156
8157 /* This drops the top 32 bits of the id, but is ok for a hash. */
8158 return dwo_unit->signature;
8159}
8160
8161static int
8162eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8163{
8164 const struct dwo_unit *lhs = item_lhs;
8165 const struct dwo_unit *rhs = item_rhs;
8166
8167 /* The signature is assumed to be unique within the DWO file.
8168 So while object file CU dwo_id's always have the value zero,
8169 that's OK, assuming each object file DWO file has only one CU,
8170 and that's the rule for now. */
8171 return lhs->signature == rhs->signature;
8172}
8173
8174/* Allocate a hash table for DWO CUs,TUs.
8175 There is one of these tables for each of CUs,TUs for each DWO file. */
8176
8177static htab_t
8178allocate_dwo_unit_table (struct objfile *objfile)
8179{
8180 /* Start out with a pretty small number.
8181 Generally DWO files contain only one CU and maybe some TUs. */
8182 return htab_create_alloc_ex (3,
8183 hash_dwo_unit,
8184 eq_dwo_unit,
8185 NULL,
8186 &objfile->objfile_obstack,
8187 hashtab_obstack_allocate,
8188 dummy_obstack_deallocate);
8189}
8190
80626a55 8191/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8192
8193struct create_dwo_info_table_data
8194{
8195 struct dwo_file *dwo_file;
8196 htab_t cu_htab;
8197};
8198
80626a55 8199/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8200
8201static void
80626a55
DE
8202create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8203 gdb_byte *info_ptr,
8204 struct die_info *comp_unit_die,
8205 int has_children,
8206 void *datap)
3019eac3
DE
8207{
8208 struct dwarf2_cu *cu = reader->cu;
8209 struct objfile *objfile = dwarf2_per_objfile->objfile;
8210 sect_offset offset = cu->per_cu->offset;
8211 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8212 struct create_dwo_info_table_data *data = datap;
8213 struct dwo_file *dwo_file = data->dwo_file;
8214 htab_t cu_htab = data->cu_htab;
8215 void **slot;
8216 struct attribute *attr;
8217 struct dwo_unit *dwo_unit;
8218
8219 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8220 if (attr == NULL)
8221 {
8222 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8223 " its dwo_id [in module %s]"),
80626a55 8224 offset.sect_off, dwo_file->name);
3019eac3
DE
8225 return;
8226 }
8227
8228 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8229 dwo_unit->dwo_file = dwo_file;
8230 dwo_unit->signature = DW_UNSND (attr);
8231 dwo_unit->info_or_types_section = section;
8232 dwo_unit->offset = offset;
8233 dwo_unit->length = cu->per_cu->length;
8234
8235 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8236 gdb_assert (slot != NULL);
8237 if (*slot != NULL)
8238 {
8239 const struct dwo_unit *dup_dwo_unit = *slot;
8240
8241 complaint (&symfile_complaints,
8242 _("debug entry at offset 0x%x is duplicate to the entry at"
8243 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8244 offset.sect_off, dup_dwo_unit->offset.sect_off,
8245 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8246 dwo_file->name);
3019eac3
DE
8247 }
8248 else
8249 *slot = dwo_unit;
8250
09406207 8251 if (dwarf2_read_debug)
3019eac3
DE
8252 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8253 offset.sect_off,
8254 phex (dwo_unit->signature,
8255 sizeof (dwo_unit->signature)));
8256}
8257
80626a55
DE
8258/* Create a hash table to map DWO IDs to their CU entry in
8259 .debug_info.dwo in DWO_FILE.
8260 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8261
8262static htab_t
80626a55 8263create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8264{
8265 struct objfile *objfile = dwarf2_per_objfile->objfile;
8266 struct dwarf2_section_info *section = &dwo_file->sections.info;
8267 bfd *abfd;
8268 htab_t cu_htab;
8269 gdb_byte *info_ptr, *end_ptr;
8270 struct create_dwo_info_table_data create_dwo_info_table_data;
8271
8272 dwarf2_read_section (objfile, section);
8273 info_ptr = section->buffer;
8274
8275 if (info_ptr == NULL)
8276 return NULL;
8277
8278 /* We can't set abfd until now because the section may be empty or
8279 not present, in which case section->asection will be NULL. */
8280 abfd = section->asection->owner;
8281
09406207 8282 if (dwarf2_read_debug)
3019eac3
DE
8283 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8284 bfd_get_filename (abfd));
8285
8286 cu_htab = allocate_dwo_unit_table (objfile);
8287
8288 create_dwo_info_table_data.dwo_file = dwo_file;
8289 create_dwo_info_table_data.cu_htab = cu_htab;
8290
8291 end_ptr = info_ptr + section->size;
8292 while (info_ptr < end_ptr)
8293 {
8294 struct dwarf2_per_cu_data per_cu;
8295
8296 memset (&per_cu, 0, sizeof (per_cu));
8297 per_cu.objfile = objfile;
8298 per_cu.is_debug_types = 0;
8299 per_cu.offset.sect_off = info_ptr - section->buffer;
8300 per_cu.info_or_types_section = section;
8301
8302 init_cutu_and_read_dies_no_follow (&per_cu,
8303 &dwo_file->sections.abbrev,
8304 dwo_file,
80626a55 8305 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8306 &create_dwo_info_table_data);
8307
8308 info_ptr += per_cu.length;
8309 }
8310
8311 return cu_htab;
8312}
8313
80626a55
DE
8314/* DWP file .debug_{cu,tu}_index section format:
8315 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8316
8317 Both index sections have the same format, and serve to map a 64-bit
8318 signature to a set of section numbers. Each section begins with a header,
8319 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8320 indexes, and a pool of 32-bit section numbers. The index sections will be
8321 aligned at 8-byte boundaries in the file.
8322
8323 The index section header contains two unsigned 32-bit values (using the
8324 byte order of the application binary):
8325
8326 N, the number of compilation units or type units in the index
8327 M, the number of slots in the hash table
8328
8329 (We assume that N and M will not exceed 2^32 - 1.)
8330
8331 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8332
8333 The hash table begins at offset 8 in the section, and consists of an array
8334 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8335 order of the application binary). Unused slots in the hash table are 0.
8336 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8337
8338 The parallel table begins immediately after the hash table
8339 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8340 array of 32-bit indexes (using the byte order of the application binary),
8341 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8342 table contains a 32-bit index into the pool of section numbers. For unused
8343 hash table slots, the corresponding entry in the parallel table will be 0.
8344
8345 Given a 64-bit compilation unit signature or a type signature S, an entry
8346 in the hash table is located as follows:
8347
8348 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8349 the low-order k bits all set to 1.
8350
8351 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8352
8353 3) If the hash table entry at index H matches the signature, use that
8354 entry. If the hash table entry at index H is unused (all zeroes),
8355 terminate the search: the signature is not present in the table.
8356
8357 4) Let H = (H + H') modulo M. Repeat at Step 3.
8358
8359 Because M > N and H' and M are relatively prime, the search is guaranteed
8360 to stop at an unused slot or find the match.
8361
8362 The pool of section numbers begins immediately following the hash table
8363 (at offset 8 + 12 * M from the beginning of the section). The pool of
8364 section numbers consists of an array of 32-bit words (using the byte order
8365 of the application binary). Each item in the array is indexed starting
8366 from 0. The hash table entry provides the index of the first section
8367 number in the set. Additional section numbers in the set follow, and the
8368 set is terminated by a 0 entry (section number 0 is not used in ELF).
8369
8370 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8371 section must be the first entry in the set, and the .debug_abbrev.dwo must
8372 be the second entry. Other members of the set may follow in any order. */
8373
8374/* Create a hash table to map DWO IDs to their CU/TU entry in
8375 .debug_{info,types}.dwo in DWP_FILE.
8376 Returns NULL if there isn't one.
8377 Note: This function processes DWP files only, not DWO files. */
8378
8379static struct dwp_hash_table *
8380create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8381{
8382 struct objfile *objfile = dwarf2_per_objfile->objfile;
8383 bfd *dbfd = dwp_file->dbfd;
8384 char *index_ptr, *index_end;
8385 struct dwarf2_section_info *index;
8386 uint32_t version, nr_units, nr_slots;
8387 struct dwp_hash_table *htab;
8388
8389 if (is_debug_types)
8390 index = &dwp_file->sections.tu_index;
8391 else
8392 index = &dwp_file->sections.cu_index;
8393
8394 if (dwarf2_section_empty_p (index))
8395 return NULL;
8396 dwarf2_read_section (objfile, index);
8397
8398 index_ptr = index->buffer;
8399 index_end = index_ptr + index->size;
8400
8401 version = read_4_bytes (dbfd, index_ptr);
8402 index_ptr += 8; /* Skip the unused word. */
8403 nr_units = read_4_bytes (dbfd, index_ptr);
8404 index_ptr += 4;
8405 nr_slots = read_4_bytes (dbfd, index_ptr);
8406 index_ptr += 4;
8407
8408 if (version != 1)
8409 {
8410 error (_("Dwarf Error: unsupported DWP file version (%u)"
8411 " [in module %s]"),
8412 version, dwp_file->name);
8413 }
8414 if (nr_slots != (nr_slots & -nr_slots))
8415 {
8416 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8417 " is not power of 2 [in module %s]"),
8418 nr_slots, dwp_file->name);
8419 }
8420
8421 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8422 htab->nr_units = nr_units;
8423 htab->nr_slots = nr_slots;
8424 htab->hash_table = index_ptr;
8425 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8426 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8427
8428 return htab;
8429}
8430
8431/* Update SECTIONS with the data from SECTP.
8432
8433 This function is like the other "locate" section routines that are
8434 passed to bfd_map_over_sections, but in this context the sections to
8435 read comes from the DWP hash table, not the full ELF section table.
8436
8437 The result is non-zero for success, or zero if an error was found. */
8438
8439static int
8440locate_virtual_dwo_sections (asection *sectp,
8441 struct virtual_dwo_sections *sections)
8442{
8443 const struct dwop_section_names *names = &dwop_section_names;
8444
8445 if (section_is_p (sectp->name, &names->abbrev_dwo))
8446 {
8447 /* There can be only one. */
8448 if (sections->abbrev.asection != NULL)
8449 return 0;
8450 sections->abbrev.asection = sectp;
8451 sections->abbrev.size = bfd_get_section_size (sectp);
8452 }
8453 else if (section_is_p (sectp->name, &names->info_dwo)
8454 || section_is_p (sectp->name, &names->types_dwo))
8455 {
8456 /* There can be only one. */
8457 if (sections->info_or_types.asection != NULL)
8458 return 0;
8459 sections->info_or_types.asection = sectp;
8460 sections->info_or_types.size = bfd_get_section_size (sectp);
8461 }
8462 else if (section_is_p (sectp->name, &names->line_dwo))
8463 {
8464 /* There can be only one. */
8465 if (sections->line.asection != NULL)
8466 return 0;
8467 sections->line.asection = sectp;
8468 sections->line.size = bfd_get_section_size (sectp);
8469 }
8470 else if (section_is_p (sectp->name, &names->loc_dwo))
8471 {
8472 /* There can be only one. */
8473 if (sections->loc.asection != NULL)
8474 return 0;
8475 sections->loc.asection = sectp;
8476 sections->loc.size = bfd_get_section_size (sectp);
8477 }
8478 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8479 {
8480 /* There can be only one. */
8481 if (sections->macinfo.asection != NULL)
8482 return 0;
8483 sections->macinfo.asection = sectp;
8484 sections->macinfo.size = bfd_get_section_size (sectp);
8485 }
8486 else if (section_is_p (sectp->name, &names->macro_dwo))
8487 {
8488 /* There can be only one. */
8489 if (sections->macro.asection != NULL)
8490 return 0;
8491 sections->macro.asection = sectp;
8492 sections->macro.size = bfd_get_section_size (sectp);
8493 }
8494 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8495 {
8496 /* There can be only one. */
8497 if (sections->str_offsets.asection != NULL)
8498 return 0;
8499 sections->str_offsets.asection = sectp;
8500 sections->str_offsets.size = bfd_get_section_size (sectp);
8501 }
8502 else
8503 {
8504 /* No other kind of section is valid. */
8505 return 0;
8506 }
8507
8508 return 1;
8509}
8510
8511/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8512 HTAB is the hash table from the DWP file.
8513 SECTION_INDEX is the index of the DWO in HTAB. */
8514
8515static struct dwo_unit *
8516create_dwo_in_dwp (struct dwp_file *dwp_file,
8517 const struct dwp_hash_table *htab,
8518 uint32_t section_index,
8519 ULONGEST signature, int is_debug_types)
8520{
8521 struct objfile *objfile = dwarf2_per_objfile->objfile;
8522 bfd *dbfd = dwp_file->dbfd;
8523 const char *kind = is_debug_types ? "TU" : "CU";
8524 struct dwo_file *dwo_file;
8525 struct dwo_unit *dwo_unit;
8526 struct virtual_dwo_sections sections;
8527 void **dwo_file_slot;
8528 char *virtual_dwo_name;
8529 struct dwarf2_section_info *cutu;
8530 struct cleanup *cleanups;
8531 int i;
8532
8533 if (dwarf2_read_debug)
8534 {
8535 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8536 kind,
8537 section_index, phex (signature, sizeof (signature)),
8538 dwp_file->name);
8539 }
8540
8541 /* Fetch the sections of this DWO.
8542 Put a limit on the number of sections we look for so that bad data
8543 doesn't cause us to loop forever. */
8544
8545#define MAX_NR_DWO_SECTIONS \
8546 (1 /* .debug_info or .debug_types */ \
8547 + 1 /* .debug_abbrev */ \
8548 + 1 /* .debug_line */ \
8549 + 1 /* .debug_loc */ \
8550 + 1 /* .debug_str_offsets */ \
8551 + 1 /* .debug_macro */ \
8552 + 1 /* .debug_macinfo */ \
8553 + 1 /* trailing zero */)
8554
8555 memset (&sections, 0, sizeof (sections));
8556 cleanups = make_cleanup (null_cleanup, 0);
8557
8558 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8559 {
8560 asection *sectp;
8561 uint32_t section_nr =
8562 read_4_bytes (dbfd,
8563 htab->section_pool
8564 + (section_index + i) * sizeof (uint32_t));
8565
8566 if (section_nr == 0)
8567 break;
8568 if (section_nr >= dwp_file->num_sections)
8569 {
8570 error (_("Dwarf Error: bad DWP hash table, section number too large"
8571 " [in module %s]"),
8572 dwp_file->name);
8573 }
8574
8575 sectp = dwp_file->elf_sections[section_nr];
8576 if (! locate_virtual_dwo_sections (sectp, &sections))
8577 {
8578 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8579 " [in module %s]"),
8580 dwp_file->name);
8581 }
8582 }
8583
8584 if (i < 2
8585 || sections.info_or_types.asection == NULL
8586 || sections.abbrev.asection == NULL)
8587 {
8588 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8589 " [in module %s]"),
8590 dwp_file->name);
8591 }
8592 if (i == MAX_NR_DWO_SECTIONS)
8593 {
8594 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8595 " [in module %s]"),
8596 dwp_file->name);
8597 }
8598
8599 /* It's easier for the rest of the code if we fake a struct dwo_file and
8600 have dwo_unit "live" in that. At least for now.
8601
8602 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8603 However, for each CU + set of TUs that came from the same original DWO
8604 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8605 (fewer struct dwo_file objects to allocated). Remember that for really
8606 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8607
2792b94d
PM
8608 virtual_dwo_name =
8609 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8610 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8611 sections.line.asection ? sections.line.asection->id : 0,
8612 sections.loc.asection ? sections.loc.asection->id : 0,
8613 (sections.str_offsets.asection
8614 ? sections.str_offsets.asection->id
8615 : 0));
80626a55
DE
8616 make_cleanup (xfree, virtual_dwo_name);
8617 /* Can we use an existing virtual DWO file? */
8618 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8619 /* Create one if necessary. */
8620 if (*dwo_file_slot == NULL)
8621 {
8622 if (dwarf2_read_debug)
8623 {
8624 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8625 virtual_dwo_name);
8626 }
8627 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8628 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8629 virtual_dwo_name,
8630 strlen (virtual_dwo_name));
8631 dwo_file->sections.abbrev = sections.abbrev;
8632 dwo_file->sections.line = sections.line;
8633 dwo_file->sections.loc = sections.loc;
8634 dwo_file->sections.macinfo = sections.macinfo;
8635 dwo_file->sections.macro = sections.macro;
8636 dwo_file->sections.str_offsets = sections.str_offsets;
8637 /* The "str" section is global to the entire DWP file. */
8638 dwo_file->sections.str = dwp_file->sections.str;
8639 /* The info or types section is assigned later to dwo_unit,
8640 there's no need to record it in dwo_file.
8641 Also, we can't simply record type sections in dwo_file because
8642 we record a pointer into the vector in dwo_unit. As we collect more
8643 types we'll grow the vector and eventually have to reallocate space
8644 for it, invalidating all the pointers into the current copy. */
8645 *dwo_file_slot = dwo_file;
8646 }
8647 else
8648 {
8649 if (dwarf2_read_debug)
8650 {
8651 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8652 virtual_dwo_name);
8653 }
8654 dwo_file = *dwo_file_slot;
8655 }
8656 do_cleanups (cleanups);
8657
8658 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8659 dwo_unit->dwo_file = dwo_file;
8660 dwo_unit->signature = signature;
8661 dwo_unit->info_or_types_section =
8662 obstack_alloc (&objfile->objfile_obstack,
8663 sizeof (struct dwarf2_section_info));
8664 *dwo_unit->info_or_types_section = sections.info_or_types;
8665 /* offset, length, type_offset_in_tu are set later. */
8666
8667 return dwo_unit;
8668}
8669
8670/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8671
8672static struct dwo_unit *
8673lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8674 const struct dwp_hash_table *htab,
8675 ULONGEST signature, int is_debug_types)
8676{
8677 bfd *dbfd = dwp_file->dbfd;
8678 uint32_t mask = htab->nr_slots - 1;
8679 uint32_t hash = signature & mask;
8680 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8681 unsigned int i;
8682 void **slot;
8683 struct dwo_unit find_dwo_cu, *dwo_cu;
8684
8685 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8686 find_dwo_cu.signature = signature;
8687 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8688
8689 if (*slot != NULL)
8690 return *slot;
8691
8692 /* Use a for loop so that we don't loop forever on bad debug info. */
8693 for (i = 0; i < htab->nr_slots; ++i)
8694 {
8695 ULONGEST signature_in_table;
8696
8697 signature_in_table =
8698 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8699 if (signature_in_table == signature)
8700 {
8701 uint32_t section_index =
8702 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8703
8704 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8705 signature, is_debug_types);
8706 return *slot;
8707 }
8708 if (signature_in_table == 0)
8709 return NULL;
8710 hash = (hash + hash2) & mask;
8711 }
8712
8713 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8714 " [in module %s]"),
8715 dwp_file->name);
8716}
8717
8718/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8719 Open the file specified by FILE_NAME and hand it off to BFD for
8720 preliminary analysis. Return a newly initialized bfd *, which
8721 includes a canonicalized copy of FILE_NAME.
80626a55 8722 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8723 In case of trouble, return NULL.
8724 NOTE: This function is derived from symfile_bfd_open. */
8725
8726static bfd *
80626a55 8727try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8728{
8729 bfd *sym_bfd;
80626a55 8730 int desc, flags;
3019eac3 8731 char *absolute_name;
3019eac3 8732
80626a55
DE
8733 flags = OPF_TRY_CWD_FIRST;
8734 if (is_dwp)
8735 flags |= OPF_SEARCH_IN_PATH;
8736 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8737 O_RDONLY | O_BINARY, &absolute_name);
8738 if (desc < 0)
8739 return NULL;
8740
bb397797 8741 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8742 if (!sym_bfd)
8743 {
3019eac3
DE
8744 xfree (absolute_name);
8745 return NULL;
8746 }
a4453b7e 8747 xfree (absolute_name);
3019eac3
DE
8748 bfd_set_cacheable (sym_bfd, 1);
8749
8750 if (!bfd_check_format (sym_bfd, bfd_object))
8751 {
cbb099e8 8752 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8753 return NULL;
8754 }
8755
3019eac3
DE
8756 return sym_bfd;
8757}
8758
80626a55 8759/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8760 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8761 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8762 The result is the bfd handle of the file.
8763 If there is a problem finding or opening the file, return NULL.
8764 Upon success, the canonicalized path of the file is stored in the bfd,
8765 same as symfile_bfd_open. */
8766
8767static bfd *
80626a55 8768open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8769{
8770 bfd *abfd;
3019eac3 8771
80626a55
DE
8772 if (IS_ABSOLUTE_PATH (file_name))
8773 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8774
8775 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8776
8777 if (comp_dir != NULL)
8778 {
80626a55 8779 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8780
8781 /* NOTE: If comp_dir is a relative path, this will also try the
8782 search path, which seems useful. */
80626a55 8783 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8784 xfree (path_to_try);
8785 if (abfd != NULL)
8786 return abfd;
8787 }
8788
8789 /* That didn't work, try debug-file-directory, which, despite its name,
8790 is a list of paths. */
8791
8792 if (*debug_file_directory == '\0')
8793 return NULL;
8794
80626a55 8795 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8796}
8797
80626a55
DE
8798/* This function is mapped across the sections and remembers the offset and
8799 size of each of the DWO debugging sections we are interested in. */
8800
8801static void
8802dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8803{
8804 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8805 const struct dwop_section_names *names = &dwop_section_names;
8806
8807 if (section_is_p (sectp->name, &names->abbrev_dwo))
8808 {
8809 dwo_sections->abbrev.asection = sectp;
8810 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8811 }
8812 else if (section_is_p (sectp->name, &names->info_dwo))
8813 {
8814 dwo_sections->info.asection = sectp;
8815 dwo_sections->info.size = bfd_get_section_size (sectp);
8816 }
8817 else if (section_is_p (sectp->name, &names->line_dwo))
8818 {
8819 dwo_sections->line.asection = sectp;
8820 dwo_sections->line.size = bfd_get_section_size (sectp);
8821 }
8822 else if (section_is_p (sectp->name, &names->loc_dwo))
8823 {
8824 dwo_sections->loc.asection = sectp;
8825 dwo_sections->loc.size = bfd_get_section_size (sectp);
8826 }
8827 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8828 {
8829 dwo_sections->macinfo.asection = sectp;
8830 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8831 }
8832 else if (section_is_p (sectp->name, &names->macro_dwo))
8833 {
8834 dwo_sections->macro.asection = sectp;
8835 dwo_sections->macro.size = bfd_get_section_size (sectp);
8836 }
8837 else if (section_is_p (sectp->name, &names->str_dwo))
8838 {
8839 dwo_sections->str.asection = sectp;
8840 dwo_sections->str.size = bfd_get_section_size (sectp);
8841 }
8842 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8843 {
8844 dwo_sections->str_offsets.asection = sectp;
8845 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8846 }
8847 else if (section_is_p (sectp->name, &names->types_dwo))
8848 {
8849 struct dwarf2_section_info type_section;
8850
8851 memset (&type_section, 0, sizeof (type_section));
8852 type_section.asection = sectp;
8853 type_section.size = bfd_get_section_size (sectp);
8854 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8855 &type_section);
8856 }
8857}
8858
8859/* Initialize the use of the DWO file specified by DWO_NAME.
8860 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8861
8862static struct dwo_file *
80626a55 8863open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8864{
8865 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8866 struct dwo_file *dwo_file;
8867 bfd *dbfd;
3019eac3
DE
8868 struct cleanup *cleanups;
8869
80626a55
DE
8870 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8871 if (dbfd == NULL)
8872 {
8873 if (dwarf2_read_debug)
8874 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8875 return NULL;
8876 }
8877 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8878 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8879 dwo_name, strlen (dwo_name));
8880 dwo_file->dbfd = dbfd;
3019eac3
DE
8881
8882 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8883
80626a55 8884 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8885
80626a55 8886 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8887
8888 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8889 dwo_file->sections.types);
8890
8891 discard_cleanups (cleanups);
8892
80626a55
DE
8893 if (dwarf2_read_debug)
8894 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8895
3019eac3
DE
8896 return dwo_file;
8897}
8898
80626a55
DE
8899/* This function is mapped across the sections and remembers the offset and
8900 size of each of the DWP debugging sections we are interested in. */
3019eac3 8901
80626a55
DE
8902static void
8903dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8904{
80626a55
DE
8905 struct dwp_file *dwp_file = dwp_file_ptr;
8906 const struct dwop_section_names *names = &dwop_section_names;
8907 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8908
80626a55
DE
8909 /* Record the ELF section number for later lookup: this is what the
8910 .debug_cu_index,.debug_tu_index tables use. */
8911 gdb_assert (elf_section_nr < dwp_file->num_sections);
8912 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8913
80626a55
DE
8914 /* Look for specific sections that we need. */
8915 if (section_is_p (sectp->name, &names->str_dwo))
8916 {
8917 dwp_file->sections.str.asection = sectp;
8918 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8919 }
8920 else if (section_is_p (sectp->name, &names->cu_index))
8921 {
8922 dwp_file->sections.cu_index.asection = sectp;
8923 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8924 }
8925 else if (section_is_p (sectp->name, &names->tu_index))
8926 {
8927 dwp_file->sections.tu_index.asection = sectp;
8928 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8929 }
8930}
3019eac3 8931
80626a55 8932/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8933
80626a55
DE
8934static hashval_t
8935hash_dwp_loaded_cutus (const void *item)
8936{
8937 const struct dwo_unit *dwo_unit = item;
3019eac3 8938
80626a55
DE
8939 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8940 return dwo_unit->signature;
3019eac3
DE
8941}
8942
80626a55 8943/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 8944
80626a55
DE
8945static int
8946eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 8947{
80626a55
DE
8948 const struct dwo_unit *dua = a;
8949 const struct dwo_unit *dub = b;
3019eac3 8950
80626a55
DE
8951 return dua->signature == dub->signature;
8952}
3019eac3 8953
80626a55 8954/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 8955
80626a55
DE
8956static htab_t
8957allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8958{
8959 return htab_create_alloc_ex (3,
8960 hash_dwp_loaded_cutus,
8961 eq_dwp_loaded_cutus,
8962 NULL,
8963 &objfile->objfile_obstack,
8964 hashtab_obstack_allocate,
8965 dummy_obstack_deallocate);
8966}
3019eac3 8967
80626a55
DE
8968/* Initialize the use of the DWP file for the current objfile.
8969 By convention the name of the DWP file is ${objfile}.dwp.
8970 The result is NULL if it can't be found. */
a766d390 8971
80626a55
DE
8972static struct dwp_file *
8973open_and_init_dwp_file (const char *comp_dir)
8974{
8975 struct objfile *objfile = dwarf2_per_objfile->objfile;
8976 struct dwp_file *dwp_file;
8977 char *dwp_name;
8978 bfd *dbfd;
8979 struct cleanup *cleanups;
8980
2792b94d 8981 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
8982 cleanups = make_cleanup (xfree, dwp_name);
8983
8984 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8985 if (dbfd == NULL)
8986 {
8987 if (dwarf2_read_debug)
8988 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8989 do_cleanups (cleanups);
8990 return NULL;
3019eac3 8991 }
80626a55
DE
8992 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
8993 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
8994 dwp_name, strlen (dwp_name));
8995 dwp_file->dbfd = dbfd;
8996 do_cleanups (cleanups);
c906108c 8997
80626a55 8998 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 8999
80626a55
DE
9000 /* +1: section 0 is unused */
9001 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9002 dwp_file->elf_sections =
9003 OBSTACK_CALLOC (&objfile->objfile_obstack,
9004 dwp_file->num_sections, asection *);
9005
9006 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9007
9008 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9009
9010 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9011
9012 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9013
9014 discard_cleanups (cleanups);
9015
9016 if (dwarf2_read_debug)
9017 {
9018 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9019 fprintf_unfiltered (gdb_stdlog,
9020 " %u CUs, %u TUs\n",
9021 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9022 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9023 }
9024
9025 return dwp_file;
3019eac3 9026}
c906108c 9027
80626a55
DE
9028/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9029 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9030 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9031 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9032 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9033
9034 This is called, for example, when wanting to read a variable with a
9035 complex location. Therefore we don't want to do file i/o for every call.
9036 Therefore we don't want to look for a DWO file on every call.
9037 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9038 then we check if we've already seen DWO_NAME, and only THEN do we check
9039 for a DWO file.
9040
1c658ad5 9041 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9042 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9043
3019eac3 9044static struct dwo_unit *
80626a55
DE
9045lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9046 const char *dwo_name, const char *comp_dir,
9047 ULONGEST signature, int is_debug_types)
3019eac3
DE
9048{
9049 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9050 const char *kind = is_debug_types ? "TU" : "CU";
9051 void **dwo_file_slot;
3019eac3 9052 struct dwo_file *dwo_file;
80626a55 9053 struct dwp_file *dwp_file;
cb1df416 9054
80626a55 9055 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9056
80626a55
DE
9057 if (! dwarf2_per_objfile->dwp_checked)
9058 {
9059 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9060 dwarf2_per_objfile->dwp_checked = 1;
9061 }
9062 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9063
80626a55 9064 if (dwp_file != NULL)
cf2c3c16 9065 {
80626a55
DE
9066 const struct dwp_hash_table *dwp_htab =
9067 is_debug_types ? dwp_file->tus : dwp_file->cus;
9068
9069 if (dwp_htab != NULL)
9070 {
9071 struct dwo_unit *dwo_cutu =
9072 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9073
9074 if (dwo_cutu != NULL)
9075 {
9076 if (dwarf2_read_debug)
9077 {
9078 fprintf_unfiltered (gdb_stdlog,
9079 "Virtual DWO %s %s found: @%s\n",
9080 kind, hex_string (signature),
9081 host_address_to_string (dwo_cutu));
9082 }
9083 return dwo_cutu;
9084 }
9085 }
9086 }
9087
9088 /* Have we already seen DWO_NAME? */
9089
9090 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9091 if (*dwo_file_slot == NULL)
9092 {
9093 /* Read in the file and build a table of the DWOs it contains. */
9094 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9095 }
9096 /* NOTE: This will be NULL if unable to open the file. */
9097 dwo_file = *dwo_file_slot;
9098
9099 if (dwo_file != NULL)
9100 {
9101 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9102
9103 if (htab != NULL)
9104 {
9105 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9106
80626a55
DE
9107 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9108 find_dwo_cutu.signature = signature;
9109 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9110
80626a55
DE
9111 if (dwo_cutu != NULL)
9112 {
9113 if (dwarf2_read_debug)
9114 {
9115 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9116 kind, dwo_name, hex_string (signature),
9117 host_address_to_string (dwo_cutu));
9118 }
9119 return dwo_cutu;
9120 }
9121 }
2e276125 9122 }
9cdd5dbd 9123
80626a55
DE
9124 /* We didn't find it. This could mean a dwo_id mismatch, or
9125 someone deleted the DWO/DWP file, or the search path isn't set up
9126 correctly to find the file. */
9127
9128 if (dwarf2_read_debug)
9129 {
9130 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9131 kind, dwo_name, hex_string (signature));
9132 }
3019eac3
DE
9133
9134 complaint (&symfile_complaints,
80626a55 9135 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9136 " [in module %s]"),
80626a55 9137 this_unit->offset.sect_off, objfile->name);
3019eac3 9138 return NULL;
5fb290d7
DJ
9139}
9140
80626a55
DE
9141/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9142 See lookup_dwo_cutu_unit for details. */
9143
9144static struct dwo_unit *
9145lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9146 const char *dwo_name, const char *comp_dir,
9147 ULONGEST signature)
9148{
9149 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9150}
9151
9152/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9153 See lookup_dwo_cutu_unit for details. */
9154
9155static struct dwo_unit *
9156lookup_dwo_type_unit (struct signatured_type *this_tu,
9157 const char *dwo_name, const char *comp_dir)
9158{
9159 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9160}
9161
3019eac3
DE
9162/* Free all resources associated with DWO_FILE.
9163 Close the DWO file and munmap the sections.
9164 All memory should be on the objfile obstack. */
348e048f
DE
9165
9166static void
3019eac3 9167free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9168{
3019eac3
DE
9169 int ix;
9170 struct dwarf2_section_info *section;
348e048f 9171
80626a55
DE
9172 gdb_assert (dwo_file->dbfd != objfile->obfd);
9173 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9174
3019eac3
DE
9175 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9176}
348e048f 9177
3019eac3 9178/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9179
3019eac3
DE
9180static void
9181free_dwo_file_cleanup (void *arg)
9182{
9183 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9184 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9185
3019eac3
DE
9186 free_dwo_file (dwo_file, objfile);
9187}
348e048f 9188
3019eac3 9189/* Traversal function for free_dwo_files. */
2ab95328 9190
3019eac3
DE
9191static int
9192free_dwo_file_from_slot (void **slot, void *info)
9193{
9194 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9195 struct objfile *objfile = (struct objfile *) info;
348e048f 9196
3019eac3 9197 free_dwo_file (dwo_file, objfile);
348e048f 9198
3019eac3
DE
9199 return 1;
9200}
348e048f 9201
3019eac3 9202/* Free all resources associated with DWO_FILES. */
348e048f 9203
3019eac3
DE
9204static void
9205free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9206{
9207 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9208}
3019eac3
DE
9209\f
9210/* Read in various DIEs. */
348e048f 9211
d389af10
JK
9212/* qsort helper for inherit_abstract_dies. */
9213
9214static int
9215unsigned_int_compar (const void *ap, const void *bp)
9216{
9217 unsigned int a = *(unsigned int *) ap;
9218 unsigned int b = *(unsigned int *) bp;
9219
9220 return (a > b) - (b > a);
9221}
9222
9223/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9224 Inherit only the children of the DW_AT_abstract_origin DIE not being
9225 already referenced by DW_AT_abstract_origin from the children of the
9226 current DIE. */
d389af10
JK
9227
9228static void
9229inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9230{
9231 struct die_info *child_die;
9232 unsigned die_children_count;
9233 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9234 sect_offset *offsets;
9235 sect_offset *offsets_end, *offsetp;
d389af10
JK
9236 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9237 struct die_info *origin_die;
9238 /* Iterator of the ORIGIN_DIE children. */
9239 struct die_info *origin_child_die;
9240 struct cleanup *cleanups;
9241 struct attribute *attr;
cd02d79d
PA
9242 struct dwarf2_cu *origin_cu;
9243 struct pending **origin_previous_list_in_scope;
d389af10
JK
9244
9245 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9246 if (!attr)
9247 return;
9248
cd02d79d
PA
9249 /* Note that following die references may follow to a die in a
9250 different cu. */
9251
9252 origin_cu = cu;
9253 origin_die = follow_die_ref (die, attr, &origin_cu);
9254
9255 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9256 symbols in. */
9257 origin_previous_list_in_scope = origin_cu->list_in_scope;
9258 origin_cu->list_in_scope = cu->list_in_scope;
9259
edb3359d
DJ
9260 if (die->tag != origin_die->tag
9261 && !(die->tag == DW_TAG_inlined_subroutine
9262 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9263 complaint (&symfile_complaints,
9264 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9265 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9266
9267 child_die = die->child;
9268 die_children_count = 0;
9269 while (child_die && child_die->tag)
9270 {
9271 child_die = sibling_die (child_die);
9272 die_children_count++;
9273 }
9274 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9275 cleanups = make_cleanup (xfree, offsets);
9276
9277 offsets_end = offsets;
9278 child_die = die->child;
9279 while (child_die && child_die->tag)
9280 {
c38f313d
DJ
9281 /* For each CHILD_DIE, find the corresponding child of
9282 ORIGIN_DIE. If there is more than one layer of
9283 DW_AT_abstract_origin, follow them all; there shouldn't be,
9284 but GCC versions at least through 4.4 generate this (GCC PR
9285 40573). */
9286 struct die_info *child_origin_die = child_die;
cd02d79d 9287 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9288
c38f313d
DJ
9289 while (1)
9290 {
cd02d79d
PA
9291 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9292 child_origin_cu);
c38f313d
DJ
9293 if (attr == NULL)
9294 break;
cd02d79d
PA
9295 child_origin_die = follow_die_ref (child_origin_die, attr,
9296 &child_origin_cu);
c38f313d
DJ
9297 }
9298
d389af10
JK
9299 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9300 counterpart may exist. */
c38f313d 9301 if (child_origin_die != child_die)
d389af10 9302 {
edb3359d
DJ
9303 if (child_die->tag != child_origin_die->tag
9304 && !(child_die->tag == DW_TAG_inlined_subroutine
9305 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9306 complaint (&symfile_complaints,
9307 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9308 "different tags"), child_die->offset.sect_off,
9309 child_origin_die->offset.sect_off);
c38f313d
DJ
9310 if (child_origin_die->parent != origin_die)
9311 complaint (&symfile_complaints,
9312 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9313 "different parents"), child_die->offset.sect_off,
9314 child_origin_die->offset.sect_off);
c38f313d
DJ
9315 else
9316 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9317 }
9318 child_die = sibling_die (child_die);
9319 }
9320 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9321 unsigned_int_compar);
9322 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9323 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9324 complaint (&symfile_complaints,
9325 _("Multiple children of DIE 0x%x refer "
9326 "to DIE 0x%x as their abstract origin"),
b64f50a1 9327 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9328
9329 offsetp = offsets;
9330 origin_child_die = origin_die->child;
9331 while (origin_child_die && origin_child_die->tag)
9332 {
9333 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9334 while (offsetp < offsets_end
9335 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9336 offsetp++;
b64f50a1
JK
9337 if (offsetp >= offsets_end
9338 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9339 {
9340 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9341 process_die (origin_child_die, origin_cu);
d389af10
JK
9342 }
9343 origin_child_die = sibling_die (origin_child_die);
9344 }
cd02d79d 9345 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9346
9347 do_cleanups (cleanups);
9348}
9349
c906108c 9350static void
e7c27a73 9351read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9352{
e7c27a73 9353 struct objfile *objfile = cu->objfile;
52f0bd74 9354 struct context_stack *new;
c906108c
SS
9355 CORE_ADDR lowpc;
9356 CORE_ADDR highpc;
9357 struct die_info *child_die;
edb3359d 9358 struct attribute *attr, *call_line, *call_file;
c906108c 9359 char *name;
e142c38c 9360 CORE_ADDR baseaddr;
801e3a5b 9361 struct block *block;
edb3359d 9362 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9363 VEC (symbolp) *template_args = NULL;
9364 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9365
9366 if (inlined_func)
9367 {
9368 /* If we do not have call site information, we can't show the
9369 caller of this inlined function. That's too confusing, so
9370 only use the scope for local variables. */
9371 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9372 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9373 if (call_line == NULL || call_file == NULL)
9374 {
9375 read_lexical_block_scope (die, cu);
9376 return;
9377 }
9378 }
c906108c 9379
e142c38c
DJ
9380 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9381
94af9270 9382 name = dwarf2_name (die, cu);
c906108c 9383
e8d05480
JB
9384 /* Ignore functions with missing or empty names. These are actually
9385 illegal according to the DWARF standard. */
9386 if (name == NULL)
9387 {
9388 complaint (&symfile_complaints,
b64f50a1
JK
9389 _("missing name for subprogram DIE at %d"),
9390 die->offset.sect_off);
e8d05480
JB
9391 return;
9392 }
9393
9394 /* Ignore functions with missing or invalid low and high pc attributes. */
9395 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9396 {
ae4d0c03
PM
9397 attr = dwarf2_attr (die, DW_AT_external, cu);
9398 if (!attr || !DW_UNSND (attr))
9399 complaint (&symfile_complaints,
3e43a32a
MS
9400 _("cannot get low and high bounds "
9401 "for subprogram DIE at %d"),
b64f50a1 9402 die->offset.sect_off);
e8d05480
JB
9403 return;
9404 }
c906108c
SS
9405
9406 lowpc += baseaddr;
9407 highpc += baseaddr;
9408
34eaf542
TT
9409 /* If we have any template arguments, then we must allocate a
9410 different sort of symbol. */
9411 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9412 {
9413 if (child_die->tag == DW_TAG_template_type_param
9414 || child_die->tag == DW_TAG_template_value_param)
9415 {
9416 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9417 struct template_symbol);
9418 templ_func->base.is_cplus_template_function = 1;
9419 break;
9420 }
9421 }
9422
c906108c 9423 new = push_context (0, lowpc);
34eaf542
TT
9424 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9425 (struct symbol *) templ_func);
4c2df51b 9426
4cecd739
DJ
9427 /* If there is a location expression for DW_AT_frame_base, record
9428 it. */
e142c38c 9429 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9430 if (attr)
c034e007
AC
9431 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9432 expression is being recorded directly in the function's symbol
9433 and not in a separate frame-base object. I guess this hack is
9434 to avoid adding some sort of frame-base adjunct/annex to the
9435 function's symbol :-(. The problem with doing this is that it
9436 results in a function symbol with a location expression that
9437 has nothing to do with the location of the function, ouch! The
9438 relationship should be: a function's symbol has-a frame base; a
9439 frame-base has-a location expression. */
e7c27a73 9440 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9441
e142c38c 9442 cu->list_in_scope = &local_symbols;
c906108c 9443
639d11d3 9444 if (die->child != NULL)
c906108c 9445 {
639d11d3 9446 child_die = die->child;
c906108c
SS
9447 while (child_die && child_die->tag)
9448 {
34eaf542
TT
9449 if (child_die->tag == DW_TAG_template_type_param
9450 || child_die->tag == DW_TAG_template_value_param)
9451 {
9452 struct symbol *arg = new_symbol (child_die, NULL, cu);
9453
f1078f66
DJ
9454 if (arg != NULL)
9455 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9456 }
9457 else
9458 process_die (child_die, cu);
c906108c
SS
9459 child_die = sibling_die (child_die);
9460 }
9461 }
9462
d389af10
JK
9463 inherit_abstract_dies (die, cu);
9464
4a811a97
UW
9465 /* If we have a DW_AT_specification, we might need to import using
9466 directives from the context of the specification DIE. See the
9467 comment in determine_prefix. */
9468 if (cu->language == language_cplus
9469 && dwarf2_attr (die, DW_AT_specification, cu))
9470 {
9471 struct dwarf2_cu *spec_cu = cu;
9472 struct die_info *spec_die = die_specification (die, &spec_cu);
9473
9474 while (spec_die)
9475 {
9476 child_die = spec_die->child;
9477 while (child_die && child_die->tag)
9478 {
9479 if (child_die->tag == DW_TAG_imported_module)
9480 process_die (child_die, spec_cu);
9481 child_die = sibling_die (child_die);
9482 }
9483
9484 /* In some cases, GCC generates specification DIEs that
9485 themselves contain DW_AT_specification attributes. */
9486 spec_die = die_specification (spec_die, &spec_cu);
9487 }
9488 }
9489
c906108c
SS
9490 new = pop_context ();
9491 /* Make a block for the local symbols within. */
801e3a5b
JB
9492 block = finish_block (new->name, &local_symbols, new->old_blocks,
9493 lowpc, highpc, objfile);
9494
df8a16a1 9495 /* For C++, set the block's scope. */
f55ee35c 9496 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9497 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9498 determine_prefix (die, cu),
df8a16a1
DJ
9499 processing_has_namespace_info);
9500
801e3a5b
JB
9501 /* If we have address ranges, record them. */
9502 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9503
34eaf542
TT
9504 /* Attach template arguments to function. */
9505 if (! VEC_empty (symbolp, template_args))
9506 {
9507 gdb_assert (templ_func != NULL);
9508
9509 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9510 templ_func->template_arguments
9511 = obstack_alloc (&objfile->objfile_obstack,
9512 (templ_func->n_template_arguments
9513 * sizeof (struct symbol *)));
9514 memcpy (templ_func->template_arguments,
9515 VEC_address (symbolp, template_args),
9516 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9517 VEC_free (symbolp, template_args);
9518 }
9519
208d8187
JB
9520 /* In C++, we can have functions nested inside functions (e.g., when
9521 a function declares a class that has methods). This means that
9522 when we finish processing a function scope, we may need to go
9523 back to building a containing block's symbol lists. */
9524 local_symbols = new->locals;
27aa8d6a 9525 using_directives = new->using_directives;
208d8187 9526
921e78cf
JB
9527 /* If we've finished processing a top-level function, subsequent
9528 symbols go in the file symbol list. */
9529 if (outermost_context_p ())
e142c38c 9530 cu->list_in_scope = &file_symbols;
c906108c
SS
9531}
9532
9533/* Process all the DIES contained within a lexical block scope. Start
9534 a new scope, process the dies, and then close the scope. */
9535
9536static void
e7c27a73 9537read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9538{
e7c27a73 9539 struct objfile *objfile = cu->objfile;
52f0bd74 9540 struct context_stack *new;
c906108c
SS
9541 CORE_ADDR lowpc, highpc;
9542 struct die_info *child_die;
e142c38c
DJ
9543 CORE_ADDR baseaddr;
9544
9545 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9546
9547 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9548 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9549 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9550 be nasty. Might be easier to properly extend generic blocks to
af34e669 9551 describe ranges. */
d85a05f0 9552 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9553 return;
9554 lowpc += baseaddr;
9555 highpc += baseaddr;
9556
9557 push_context (0, lowpc);
639d11d3 9558 if (die->child != NULL)
c906108c 9559 {
639d11d3 9560 child_die = die->child;
c906108c
SS
9561 while (child_die && child_die->tag)
9562 {
e7c27a73 9563 process_die (child_die, cu);
c906108c
SS
9564 child_die = sibling_die (child_die);
9565 }
9566 }
9567 new = pop_context ();
9568
8540c487 9569 if (local_symbols != NULL || using_directives != NULL)
c906108c 9570 {
801e3a5b
JB
9571 struct block *block
9572 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9573 highpc, objfile);
9574
9575 /* Note that recording ranges after traversing children, as we
9576 do here, means that recording a parent's ranges entails
9577 walking across all its children's ranges as they appear in
9578 the address map, which is quadratic behavior.
9579
9580 It would be nicer to record the parent's ranges before
9581 traversing its children, simply overriding whatever you find
9582 there. But since we don't even decide whether to create a
9583 block until after we've traversed its children, that's hard
9584 to do. */
9585 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9586 }
9587 local_symbols = new->locals;
27aa8d6a 9588 using_directives = new->using_directives;
c906108c
SS
9589}
9590
96408a79
SA
9591/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9592
9593static void
9594read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9595{
9596 struct objfile *objfile = cu->objfile;
9597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9598 CORE_ADDR pc, baseaddr;
9599 struct attribute *attr;
9600 struct call_site *call_site, call_site_local;
9601 void **slot;
9602 int nparams;
9603 struct die_info *child_die;
9604
9605 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9606
9607 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9608 if (!attr)
9609 {
9610 complaint (&symfile_complaints,
9611 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9612 "DIE 0x%x [in module %s]"),
b64f50a1 9613 die->offset.sect_off, objfile->name);
96408a79
SA
9614 return;
9615 }
9616 pc = DW_ADDR (attr) + baseaddr;
9617
9618 if (cu->call_site_htab == NULL)
9619 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9620 NULL, &objfile->objfile_obstack,
9621 hashtab_obstack_allocate, NULL);
9622 call_site_local.pc = pc;
9623 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9624 if (*slot != NULL)
9625 {
9626 complaint (&symfile_complaints,
9627 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9628 "DIE 0x%x [in module %s]"),
b64f50a1 9629 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9630 return;
9631 }
9632
9633 /* Count parameters at the caller. */
9634
9635 nparams = 0;
9636 for (child_die = die->child; child_die && child_die->tag;
9637 child_die = sibling_die (child_die))
9638 {
9639 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9640 {
9641 complaint (&symfile_complaints,
9642 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9643 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9644 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9645 continue;
9646 }
9647
9648 nparams++;
9649 }
9650
9651 call_site = obstack_alloc (&objfile->objfile_obstack,
9652 (sizeof (*call_site)
9653 + (sizeof (*call_site->parameter)
9654 * (nparams - 1))));
9655 *slot = call_site;
9656 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9657 call_site->pc = pc;
9658
9659 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9660 {
9661 struct die_info *func_die;
9662
9663 /* Skip also over DW_TAG_inlined_subroutine. */
9664 for (func_die = die->parent;
9665 func_die && func_die->tag != DW_TAG_subprogram
9666 && func_die->tag != DW_TAG_subroutine_type;
9667 func_die = func_die->parent);
9668
9669 /* DW_AT_GNU_all_call_sites is a superset
9670 of DW_AT_GNU_all_tail_call_sites. */
9671 if (func_die
9672 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9673 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9674 {
9675 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9676 not complete. But keep CALL_SITE for look ups via call_site_htab,
9677 both the initial caller containing the real return address PC and
9678 the final callee containing the current PC of a chain of tail
9679 calls do not need to have the tail call list complete. But any
9680 function candidate for a virtual tail call frame searched via
9681 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9682 determined unambiguously. */
9683 }
9684 else
9685 {
9686 struct type *func_type = NULL;
9687
9688 if (func_die)
9689 func_type = get_die_type (func_die, cu);
9690 if (func_type != NULL)
9691 {
9692 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9693
9694 /* Enlist this call site to the function. */
9695 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9696 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9697 }
9698 else
9699 complaint (&symfile_complaints,
9700 _("Cannot find function owning DW_TAG_GNU_call_site "
9701 "DIE 0x%x [in module %s]"),
b64f50a1 9702 die->offset.sect_off, objfile->name);
96408a79
SA
9703 }
9704 }
9705
9706 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9707 if (attr == NULL)
9708 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9709 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9710 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9711 /* Keep NULL DWARF_BLOCK. */;
9712 else if (attr_form_is_block (attr))
9713 {
9714 struct dwarf2_locexpr_baton *dlbaton;
9715
9716 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9717 dlbaton->data = DW_BLOCK (attr)->data;
9718 dlbaton->size = DW_BLOCK (attr)->size;
9719 dlbaton->per_cu = cu->per_cu;
9720
9721 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9722 }
9723 else if (is_ref_attr (attr))
9724 {
96408a79
SA
9725 struct dwarf2_cu *target_cu = cu;
9726 struct die_info *target_die;
9727
9728 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9729 gdb_assert (target_cu->objfile == objfile);
9730 if (die_is_declaration (target_die, target_cu))
9731 {
9732 const char *target_physname;
9733
9734 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9735 if (target_physname == NULL)
9736 complaint (&symfile_complaints,
9737 _("DW_AT_GNU_call_site_target target DIE has invalid "
9738 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9739 die->offset.sect_off, objfile->name);
96408a79
SA
9740 else
9741 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9742 }
9743 else
9744 {
9745 CORE_ADDR lowpc;
9746
9747 /* DW_AT_entry_pc should be preferred. */
9748 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9749 complaint (&symfile_complaints,
9750 _("DW_AT_GNU_call_site_target target DIE has invalid "
9751 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9752 die->offset.sect_off, objfile->name);
96408a79
SA
9753 else
9754 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9755 }
9756 }
9757 else
9758 complaint (&symfile_complaints,
9759 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9760 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9761 die->offset.sect_off, objfile->name);
96408a79
SA
9762
9763 call_site->per_cu = cu->per_cu;
9764
9765 for (child_die = die->child;
9766 child_die && child_die->tag;
9767 child_die = sibling_die (child_die))
9768 {
96408a79 9769 struct call_site_parameter *parameter;
1788b2d3 9770 struct attribute *loc, *origin;
96408a79
SA
9771
9772 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9773 {
9774 /* Already printed the complaint above. */
9775 continue;
9776 }
9777
9778 gdb_assert (call_site->parameter_count < nparams);
9779 parameter = &call_site->parameter[call_site->parameter_count];
9780
1788b2d3
JK
9781 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9782 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9783 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9784
24c5c679 9785 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9786 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9787 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9788 {
9789 sect_offset offset;
9790
9791 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9792 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9793 if (!offset_in_cu_p (&cu->header, offset))
9794 {
9795 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9796 binding can be done only inside one CU. Such referenced DIE
9797 therefore cannot be even moved to DW_TAG_partial_unit. */
9798 complaint (&symfile_complaints,
9799 _("DW_AT_abstract_origin offset is not in CU for "
9800 "DW_TAG_GNU_call_site child DIE 0x%x "
9801 "[in module %s]"),
9802 child_die->offset.sect_off, objfile->name);
9803 continue;
9804 }
1788b2d3
JK
9805 parameter->u.param_offset.cu_off = (offset.sect_off
9806 - cu->header.offset.sect_off);
9807 }
9808 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9809 {
9810 complaint (&symfile_complaints,
9811 _("No DW_FORM_block* DW_AT_location for "
9812 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9813 child_die->offset.sect_off, objfile->name);
96408a79
SA
9814 continue;
9815 }
24c5c679 9816 else
96408a79 9817 {
24c5c679
JK
9818 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9819 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9820 if (parameter->u.dwarf_reg != -1)
9821 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9822 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9823 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9824 &parameter->u.fb_offset))
9825 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9826 else
9827 {
9828 complaint (&symfile_complaints,
9829 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9830 "for DW_FORM_block* DW_AT_location is supported for "
9831 "DW_TAG_GNU_call_site child DIE 0x%x "
9832 "[in module %s]"),
9833 child_die->offset.sect_off, objfile->name);
9834 continue;
9835 }
96408a79
SA
9836 }
9837
9838 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9839 if (!attr_form_is_block (attr))
9840 {
9841 complaint (&symfile_complaints,
9842 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9843 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9844 child_die->offset.sect_off, objfile->name);
96408a79
SA
9845 continue;
9846 }
9847 parameter->value = DW_BLOCK (attr)->data;
9848 parameter->value_size = DW_BLOCK (attr)->size;
9849
9850 /* Parameters are not pre-cleared by memset above. */
9851 parameter->data_value = NULL;
9852 parameter->data_value_size = 0;
9853 call_site->parameter_count++;
9854
9855 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9856 if (attr)
9857 {
9858 if (!attr_form_is_block (attr))
9859 complaint (&symfile_complaints,
9860 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9861 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9862 child_die->offset.sect_off, objfile->name);
96408a79
SA
9863 else
9864 {
9865 parameter->data_value = DW_BLOCK (attr)->data;
9866 parameter->data_value_size = DW_BLOCK (attr)->size;
9867 }
9868 }
9869 }
9870}
9871
43039443 9872/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9873 Return 1 if the attributes are present and valid, otherwise, return 0.
9874 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9875
9876static int
9877dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9878 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9879 struct partial_symtab *ranges_pst)
43039443
JK
9880{
9881 struct objfile *objfile = cu->objfile;
9882 struct comp_unit_head *cu_header = &cu->header;
9883 bfd *obfd = objfile->obfd;
9884 unsigned int addr_size = cu_header->addr_size;
9885 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9886 /* Base address selection entry. */
9887 CORE_ADDR base;
9888 int found_base;
9889 unsigned int dummy;
9890 gdb_byte *buffer;
9891 CORE_ADDR marker;
9892 int low_set;
9893 CORE_ADDR low = 0;
9894 CORE_ADDR high = 0;
ff013f42 9895 CORE_ADDR baseaddr;
43039443 9896
d00adf39
DE
9897 found_base = cu->base_known;
9898 base = cu->base_address;
43039443 9899
be391dca 9900 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9901 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9902 {
9903 complaint (&symfile_complaints,
9904 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9905 offset);
9906 return 0;
9907 }
dce234bc 9908 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9909
9910 /* Read in the largest possible address. */
9911 marker = read_address (obfd, buffer, cu, &dummy);
9912 if ((marker & mask) == mask)
9913 {
9914 /* If we found the largest possible address, then
9915 read the base address. */
9916 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9917 buffer += 2 * addr_size;
9918 offset += 2 * addr_size;
9919 found_base = 1;
9920 }
9921
9922 low_set = 0;
9923
e7030f15 9924 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9925
43039443
JK
9926 while (1)
9927 {
9928 CORE_ADDR range_beginning, range_end;
9929
9930 range_beginning = read_address (obfd, buffer, cu, &dummy);
9931 buffer += addr_size;
9932 range_end = read_address (obfd, buffer, cu, &dummy);
9933 buffer += addr_size;
9934 offset += 2 * addr_size;
9935
9936 /* An end of list marker is a pair of zero addresses. */
9937 if (range_beginning == 0 && range_end == 0)
9938 /* Found the end of list entry. */
9939 break;
9940
9941 /* Each base address selection entry is a pair of 2 values.
9942 The first is the largest possible address, the second is
9943 the base address. Check for a base address here. */
9944 if ((range_beginning & mask) == mask)
9945 {
9946 /* If we found the largest possible address, then
9947 read the base address. */
9948 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9949 found_base = 1;
9950 continue;
9951 }
9952
9953 if (!found_base)
9954 {
9955 /* We have no valid base address for the ranges
9956 data. */
9957 complaint (&symfile_complaints,
9958 _("Invalid .debug_ranges data (no base address)"));
9959 return 0;
9960 }
9961
9277c30c
UW
9962 if (range_beginning > range_end)
9963 {
9964 /* Inverted range entries are invalid. */
9965 complaint (&symfile_complaints,
9966 _("Invalid .debug_ranges data (inverted range)"));
9967 return 0;
9968 }
9969
9970 /* Empty range entries have no effect. */
9971 if (range_beginning == range_end)
9972 continue;
9973
43039443
JK
9974 range_beginning += base;
9975 range_end += base;
9976
01093045
DE
9977 /* A not-uncommon case of bad debug info.
9978 Don't pollute the addrmap with bad data. */
9979 if (range_beginning + baseaddr == 0
9980 && !dwarf2_per_objfile->has_section_at_zero)
9981 {
9982 complaint (&symfile_complaints,
9983 _(".debug_ranges entry has start address of zero"
9984 " [in module %s]"), objfile->name);
9985 continue;
9986 }
9987
9277c30c 9988 if (ranges_pst != NULL)
ff013f42 9989 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
9990 range_beginning + baseaddr,
9991 range_end - 1 + baseaddr,
ff013f42
JK
9992 ranges_pst);
9993
43039443
JK
9994 /* FIXME: This is recording everything as a low-high
9995 segment of consecutive addresses. We should have a
9996 data structure for discontiguous block ranges
9997 instead. */
9998 if (! low_set)
9999 {
10000 low = range_beginning;
10001 high = range_end;
10002 low_set = 1;
10003 }
10004 else
10005 {
10006 if (range_beginning < low)
10007 low = range_beginning;
10008 if (range_end > high)
10009 high = range_end;
10010 }
10011 }
10012
10013 if (! low_set)
10014 /* If the first entry is an end-of-list marker, the range
10015 describes an empty scope, i.e. no instructions. */
10016 return 0;
10017
10018 if (low_return)
10019 *low_return = low;
10020 if (high_return)
10021 *high_return = high;
10022 return 1;
10023}
10024
af34e669
DJ
10025/* Get low and high pc attributes from a die. Return 1 if the attributes
10026 are present and valid, otherwise, return 0. Return -1 if the range is
10027 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10028
c906108c 10029static int
af34e669 10030dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10031 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10032 struct partial_symtab *pst)
c906108c
SS
10033{
10034 struct attribute *attr;
91da1414 10035 struct attribute *attr_high;
af34e669
DJ
10036 CORE_ADDR low = 0;
10037 CORE_ADDR high = 0;
10038 int ret = 0;
c906108c 10039
91da1414
MW
10040 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10041 if (attr_high)
af34e669 10042 {
e142c38c 10043 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10044 if (attr)
91da1414
MW
10045 {
10046 low = DW_ADDR (attr);
3019eac3
DE
10047 if (attr_high->form == DW_FORM_addr
10048 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10049 high = DW_ADDR (attr_high);
10050 else
10051 high = low + DW_UNSND (attr_high);
10052 }
af34e669
DJ
10053 else
10054 /* Found high w/o low attribute. */
10055 return 0;
10056
10057 /* Found consecutive range of addresses. */
10058 ret = 1;
10059 }
c906108c 10060 else
af34e669 10061 {
e142c38c 10062 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10063 if (attr != NULL)
10064 {
ab435259
DE
10065 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10066 We take advantage of the fact that DW_AT_ranges does not appear
10067 in DW_TAG_compile_unit of DWO files. */
10068 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10069 unsigned int ranges_offset = (DW_UNSND (attr)
10070 + (need_ranges_base
10071 ? cu->ranges_base
10072 : 0));
2e3cf129 10073
af34e669 10074 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10075 .debug_ranges section. */
2e3cf129 10076 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10077 return 0;
43039443 10078 /* Found discontinuous range of addresses. */
af34e669
DJ
10079 ret = -1;
10080 }
10081 }
c906108c 10082
9373cf26
JK
10083 /* read_partial_die has also the strict LOW < HIGH requirement. */
10084 if (high <= low)
c906108c
SS
10085 return 0;
10086
10087 /* When using the GNU linker, .gnu.linkonce. sections are used to
10088 eliminate duplicate copies of functions and vtables and such.
10089 The linker will arbitrarily choose one and discard the others.
10090 The AT_*_pc values for such functions refer to local labels in
10091 these sections. If the section from that file was discarded, the
10092 labels are not in the output, so the relocs get a value of 0.
10093 If this is a discarded function, mark the pc bounds as invalid,
10094 so that GDB will ignore it. */
72dca2f5 10095 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10096 return 0;
10097
10098 *lowpc = low;
96408a79
SA
10099 if (highpc)
10100 *highpc = high;
af34e669 10101 return ret;
c906108c
SS
10102}
10103
b084d499
JB
10104/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10105 its low and high PC addresses. Do nothing if these addresses could not
10106 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10107 and HIGHPC to the high address if greater than HIGHPC. */
10108
10109static void
10110dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10111 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10112 struct dwarf2_cu *cu)
10113{
10114 CORE_ADDR low, high;
10115 struct die_info *child = die->child;
10116
d85a05f0 10117 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10118 {
10119 *lowpc = min (*lowpc, low);
10120 *highpc = max (*highpc, high);
10121 }
10122
10123 /* If the language does not allow nested subprograms (either inside
10124 subprograms or lexical blocks), we're done. */
10125 if (cu->language != language_ada)
10126 return;
6e70227d 10127
b084d499
JB
10128 /* Check all the children of the given DIE. If it contains nested
10129 subprograms, then check their pc bounds. Likewise, we need to
10130 check lexical blocks as well, as they may also contain subprogram
10131 definitions. */
10132 while (child && child->tag)
10133 {
10134 if (child->tag == DW_TAG_subprogram
10135 || child->tag == DW_TAG_lexical_block)
10136 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10137 child = sibling_die (child);
10138 }
10139}
10140
fae299cd
DC
10141/* Get the low and high pc's represented by the scope DIE, and store
10142 them in *LOWPC and *HIGHPC. If the correct values can't be
10143 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10144
10145static void
10146get_scope_pc_bounds (struct die_info *die,
10147 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10148 struct dwarf2_cu *cu)
10149{
10150 CORE_ADDR best_low = (CORE_ADDR) -1;
10151 CORE_ADDR best_high = (CORE_ADDR) 0;
10152 CORE_ADDR current_low, current_high;
10153
d85a05f0 10154 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10155 {
10156 best_low = current_low;
10157 best_high = current_high;
10158 }
10159 else
10160 {
10161 struct die_info *child = die->child;
10162
10163 while (child && child->tag)
10164 {
10165 switch (child->tag) {
10166 case DW_TAG_subprogram:
b084d499 10167 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10168 break;
10169 case DW_TAG_namespace:
f55ee35c 10170 case DW_TAG_module:
fae299cd
DC
10171 /* FIXME: carlton/2004-01-16: Should we do this for
10172 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10173 that current GCC's always emit the DIEs corresponding
10174 to definitions of methods of classes as children of a
10175 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10176 the DIEs giving the declarations, which could be
10177 anywhere). But I don't see any reason why the
10178 standards says that they have to be there. */
10179 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10180
10181 if (current_low != ((CORE_ADDR) -1))
10182 {
10183 best_low = min (best_low, current_low);
10184 best_high = max (best_high, current_high);
10185 }
10186 break;
10187 default:
0963b4bd 10188 /* Ignore. */
fae299cd
DC
10189 break;
10190 }
10191
10192 child = sibling_die (child);
10193 }
10194 }
10195
10196 *lowpc = best_low;
10197 *highpc = best_high;
10198}
10199
801e3a5b
JB
10200/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10201 in DIE. */
380bca97 10202
801e3a5b
JB
10203static void
10204dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10205 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10206{
bb5ed363 10207 struct objfile *objfile = cu->objfile;
801e3a5b 10208 struct attribute *attr;
91da1414 10209 struct attribute *attr_high;
801e3a5b 10210
91da1414
MW
10211 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10212 if (attr_high)
801e3a5b 10213 {
801e3a5b
JB
10214 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10215 if (attr)
10216 {
10217 CORE_ADDR low = DW_ADDR (attr);
91da1414 10218 CORE_ADDR high;
3019eac3
DE
10219 if (attr_high->form == DW_FORM_addr
10220 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10221 high = DW_ADDR (attr_high);
10222 else
10223 high = low + DW_UNSND (attr_high);
9a619af0 10224
801e3a5b
JB
10225 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10226 }
10227 }
10228
10229 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10230 if (attr)
10231 {
bb5ed363 10232 bfd *obfd = objfile->obfd;
ab435259
DE
10233 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10234 We take advantage of the fact that DW_AT_ranges does not appear
10235 in DW_TAG_compile_unit of DWO files. */
10236 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10237
10238 /* The value of the DW_AT_ranges attribute is the offset of the
10239 address range list in the .debug_ranges section. */
ab435259
DE
10240 unsigned long offset = (DW_UNSND (attr)
10241 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10242 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10243
10244 /* For some target architectures, but not others, the
10245 read_address function sign-extends the addresses it returns.
10246 To recognize base address selection entries, we need a
10247 mask. */
10248 unsigned int addr_size = cu->header.addr_size;
10249 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10250
10251 /* The base address, to which the next pair is relative. Note
10252 that this 'base' is a DWARF concept: most entries in a range
10253 list are relative, to reduce the number of relocs against the
10254 debugging information. This is separate from this function's
10255 'baseaddr' argument, which GDB uses to relocate debugging
10256 information from a shared library based on the address at
10257 which the library was loaded. */
d00adf39
DE
10258 CORE_ADDR base = cu->base_address;
10259 int base_known = cu->base_known;
801e3a5b 10260
be391dca 10261 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10262 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10263 {
10264 complaint (&symfile_complaints,
10265 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10266 offset);
10267 return;
10268 }
10269
10270 for (;;)
10271 {
10272 unsigned int bytes_read;
10273 CORE_ADDR start, end;
10274
10275 start = read_address (obfd, buffer, cu, &bytes_read);
10276 buffer += bytes_read;
10277 end = read_address (obfd, buffer, cu, &bytes_read);
10278 buffer += bytes_read;
10279
10280 /* Did we find the end of the range list? */
10281 if (start == 0 && end == 0)
10282 break;
10283
10284 /* Did we find a base address selection entry? */
10285 else if ((start & base_select_mask) == base_select_mask)
10286 {
10287 base = end;
10288 base_known = 1;
10289 }
10290
10291 /* We found an ordinary address range. */
10292 else
10293 {
10294 if (!base_known)
10295 {
10296 complaint (&symfile_complaints,
3e43a32a
MS
10297 _("Invalid .debug_ranges data "
10298 "(no base address)"));
801e3a5b
JB
10299 return;
10300 }
10301
9277c30c
UW
10302 if (start > end)
10303 {
10304 /* Inverted range entries are invalid. */
10305 complaint (&symfile_complaints,
10306 _("Invalid .debug_ranges data "
10307 "(inverted range)"));
10308 return;
10309 }
10310
10311 /* Empty range entries have no effect. */
10312 if (start == end)
10313 continue;
10314
01093045
DE
10315 start += base + baseaddr;
10316 end += base + baseaddr;
10317
10318 /* A not-uncommon case of bad debug info.
10319 Don't pollute the addrmap with bad data. */
10320 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10321 {
10322 complaint (&symfile_complaints,
10323 _(".debug_ranges entry has start address of zero"
10324 " [in module %s]"), objfile->name);
10325 continue;
10326 }
10327
10328 record_block_range (block, start, end - 1);
801e3a5b
JB
10329 }
10330 }
10331 }
10332}
10333
685b1105
JK
10334/* Check whether the producer field indicates either of GCC < 4.6, or the
10335 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10336
685b1105
JK
10337static void
10338check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10339{
10340 const char *cs;
10341 int major, minor, release;
10342
10343 if (cu->producer == NULL)
10344 {
10345 /* For unknown compilers expect their behavior is DWARF version
10346 compliant.
10347
10348 GCC started to support .debug_types sections by -gdwarf-4 since
10349 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10350 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10351 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10352 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10353 }
685b1105 10354 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10355 {
685b1105
JK
10356 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10357
ba919b58
TT
10358 cs = &cu->producer[strlen ("GNU ")];
10359 while (*cs && !isdigit (*cs))
10360 cs++;
10361 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10362 {
10363 /* Not recognized as GCC. */
10364 }
10365 else
1b80a9fa
JK
10366 {
10367 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10368 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10369 }
685b1105
JK
10370 }
10371 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10372 cu->producer_is_icc = 1;
10373 else
10374 {
10375 /* For other non-GCC compilers, expect their behavior is DWARF version
10376 compliant. */
60d5a603
JK
10377 }
10378
ba919b58 10379 cu->checked_producer = 1;
685b1105 10380}
ba919b58 10381
685b1105
JK
10382/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10383 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10384 during 4.6.0 experimental. */
10385
10386static int
10387producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10388{
10389 if (!cu->checked_producer)
10390 check_producer (cu);
10391
10392 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10393}
10394
10395/* Return the default accessibility type if it is not overriden by
10396 DW_AT_accessibility. */
10397
10398static enum dwarf_access_attribute
10399dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10400{
10401 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10402 {
10403 /* The default DWARF 2 accessibility for members is public, the default
10404 accessibility for inheritance is private. */
10405
10406 if (die->tag != DW_TAG_inheritance)
10407 return DW_ACCESS_public;
10408 else
10409 return DW_ACCESS_private;
10410 }
10411 else
10412 {
10413 /* DWARF 3+ defines the default accessibility a different way. The same
10414 rules apply now for DW_TAG_inheritance as for the members and it only
10415 depends on the container kind. */
10416
10417 if (die->parent->tag == DW_TAG_class_type)
10418 return DW_ACCESS_private;
10419 else
10420 return DW_ACCESS_public;
10421 }
10422}
10423
74ac6d43
TT
10424/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10425 offset. If the attribute was not found return 0, otherwise return
10426 1. If it was found but could not properly be handled, set *OFFSET
10427 to 0. */
10428
10429static int
10430handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10431 LONGEST *offset)
10432{
10433 struct attribute *attr;
10434
10435 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10436 if (attr != NULL)
10437 {
10438 *offset = 0;
10439
10440 /* Note that we do not check for a section offset first here.
10441 This is because DW_AT_data_member_location is new in DWARF 4,
10442 so if we see it, we can assume that a constant form is really
10443 a constant and not a section offset. */
10444 if (attr_form_is_constant (attr))
10445 *offset = dwarf2_get_attr_constant_value (attr, 0);
10446 else if (attr_form_is_section_offset (attr))
10447 dwarf2_complex_location_expr_complaint ();
10448 else if (attr_form_is_block (attr))
10449 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10450 else
10451 dwarf2_complex_location_expr_complaint ();
10452
10453 return 1;
10454 }
10455
10456 return 0;
10457}
10458
c906108c
SS
10459/* Add an aggregate field to the field list. */
10460
10461static void
107d2387 10462dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10463 struct dwarf2_cu *cu)
6e70227d 10464{
e7c27a73 10465 struct objfile *objfile = cu->objfile;
5e2b427d 10466 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10467 struct nextfield *new_field;
10468 struct attribute *attr;
10469 struct field *fp;
10470 char *fieldname = "";
10471
10472 /* Allocate a new field list entry and link it in. */
10473 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10474 make_cleanup (xfree, new_field);
c906108c 10475 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10476
10477 if (die->tag == DW_TAG_inheritance)
10478 {
10479 new_field->next = fip->baseclasses;
10480 fip->baseclasses = new_field;
10481 }
10482 else
10483 {
10484 new_field->next = fip->fields;
10485 fip->fields = new_field;
10486 }
c906108c
SS
10487 fip->nfields++;
10488
e142c38c 10489 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10490 if (attr)
10491 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10492 else
10493 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10494 if (new_field->accessibility != DW_ACCESS_public)
10495 fip->non_public_fields = 1;
60d5a603 10496
e142c38c 10497 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10498 if (attr)
10499 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10500 else
10501 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10502
10503 fp = &new_field->field;
a9a9bd0f 10504
e142c38c 10505 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10506 {
74ac6d43
TT
10507 LONGEST offset;
10508
a9a9bd0f 10509 /* Data member other than a C++ static data member. */
6e70227d 10510
c906108c 10511 /* Get type of field. */
e7c27a73 10512 fp->type = die_type (die, cu);
c906108c 10513
d6a843b5 10514 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10515
c906108c 10516 /* Get bit size of field (zero if none). */
e142c38c 10517 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10518 if (attr)
10519 {
10520 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10521 }
10522 else
10523 {
10524 FIELD_BITSIZE (*fp) = 0;
10525 }
10526
10527 /* Get bit offset of field. */
74ac6d43
TT
10528 if (handle_data_member_location (die, cu, &offset))
10529 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10530 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10531 if (attr)
10532 {
5e2b427d 10533 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10534 {
10535 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10536 additional bit offset from the MSB of the containing
10537 anonymous object to the MSB of the field. We don't
10538 have to do anything special since we don't need to
10539 know the size of the anonymous object. */
f41f5e61 10540 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10541 }
10542 else
10543 {
10544 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10545 MSB of the anonymous object, subtract off the number of
10546 bits from the MSB of the field to the MSB of the
10547 object, and then subtract off the number of bits of
10548 the field itself. The result is the bit offset of
10549 the LSB of the field. */
c906108c
SS
10550 int anonymous_size;
10551 int bit_offset = DW_UNSND (attr);
10552
e142c38c 10553 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10554 if (attr)
10555 {
10556 /* The size of the anonymous object containing
10557 the bit field is explicit, so use the
10558 indicated size (in bytes). */
10559 anonymous_size = DW_UNSND (attr);
10560 }
10561 else
10562 {
10563 /* The size of the anonymous object containing
10564 the bit field must be inferred from the type
10565 attribute of the data member containing the
10566 bit field. */
10567 anonymous_size = TYPE_LENGTH (fp->type);
10568 }
f41f5e61
PA
10569 SET_FIELD_BITPOS (*fp,
10570 (FIELD_BITPOS (*fp)
10571 + anonymous_size * bits_per_byte
10572 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10573 }
10574 }
10575
10576 /* Get name of field. */
39cbfefa
DJ
10577 fieldname = dwarf2_name (die, cu);
10578 if (fieldname == NULL)
10579 fieldname = "";
d8151005
DJ
10580
10581 /* The name is already allocated along with this objfile, so we don't
10582 need to duplicate it for the type. */
10583 fp->name = fieldname;
c906108c
SS
10584
10585 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10586 pointer or virtual base class pointer) to private. */
e142c38c 10587 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10588 {
d48cc9dd 10589 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10590 new_field->accessibility = DW_ACCESS_private;
10591 fip->non_public_fields = 1;
10592 }
10593 }
a9a9bd0f 10594 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10595 {
a9a9bd0f
DC
10596 /* C++ static member. */
10597
10598 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10599 is a declaration, but all versions of G++ as of this writing
10600 (so through at least 3.2.1) incorrectly generate
10601 DW_TAG_variable tags. */
6e70227d 10602
ff355380 10603 const char *physname;
c906108c 10604
a9a9bd0f 10605 /* Get name of field. */
39cbfefa
DJ
10606 fieldname = dwarf2_name (die, cu);
10607 if (fieldname == NULL)
c906108c
SS
10608 return;
10609
254e6b9e 10610 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10611 if (attr
10612 /* Only create a symbol if this is an external value.
10613 new_symbol checks this and puts the value in the global symbol
10614 table, which we want. If it is not external, new_symbol
10615 will try to put the value in cu->list_in_scope which is wrong. */
10616 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10617 {
10618 /* A static const member, not much different than an enum as far as
10619 we're concerned, except that we can support more types. */
10620 new_symbol (die, NULL, cu);
10621 }
10622
2df3850c 10623 /* Get physical name. */
ff355380 10624 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10625
d8151005
DJ
10626 /* The name is already allocated along with this objfile, so we don't
10627 need to duplicate it for the type. */
10628 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10629 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10630 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10631 }
10632 else if (die->tag == DW_TAG_inheritance)
10633 {
74ac6d43 10634 LONGEST offset;
d4b96c9a 10635
74ac6d43
TT
10636 /* C++ base class field. */
10637 if (handle_data_member_location (die, cu, &offset))
10638 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10639 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10640 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10641 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10642 fip->nbaseclasses++;
10643 }
10644}
10645
98751a41
JK
10646/* Add a typedef defined in the scope of the FIP's class. */
10647
10648static void
10649dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10650 struct dwarf2_cu *cu)
6e70227d 10651{
98751a41 10652 struct objfile *objfile = cu->objfile;
98751a41
JK
10653 struct typedef_field_list *new_field;
10654 struct attribute *attr;
10655 struct typedef_field *fp;
10656 char *fieldname = "";
10657
10658 /* Allocate a new field list entry and link it in. */
10659 new_field = xzalloc (sizeof (*new_field));
10660 make_cleanup (xfree, new_field);
10661
10662 gdb_assert (die->tag == DW_TAG_typedef);
10663
10664 fp = &new_field->field;
10665
10666 /* Get name of field. */
10667 fp->name = dwarf2_name (die, cu);
10668 if (fp->name == NULL)
10669 return;
10670
10671 fp->type = read_type_die (die, cu);
10672
10673 new_field->next = fip->typedef_field_list;
10674 fip->typedef_field_list = new_field;
10675 fip->typedef_field_list_count++;
10676}
10677
c906108c
SS
10678/* Create the vector of fields, and attach it to the type. */
10679
10680static void
fba45db2 10681dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10682 struct dwarf2_cu *cu)
c906108c
SS
10683{
10684 int nfields = fip->nfields;
10685
10686 /* Record the field count, allocate space for the array of fields,
10687 and create blank accessibility bitfields if necessary. */
10688 TYPE_NFIELDS (type) = nfields;
10689 TYPE_FIELDS (type) = (struct field *)
10690 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10691 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10692
b4ba55a1 10693 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10694 {
10695 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10696
10697 TYPE_FIELD_PRIVATE_BITS (type) =
10698 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10699 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10700
10701 TYPE_FIELD_PROTECTED_BITS (type) =
10702 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10703 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10704
774b6a14
TT
10705 TYPE_FIELD_IGNORE_BITS (type) =
10706 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10707 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10708 }
10709
10710 /* If the type has baseclasses, allocate and clear a bit vector for
10711 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10712 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10713 {
10714 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10715 unsigned char *pointer;
c906108c
SS
10716
10717 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10718 pointer = TYPE_ALLOC (type, num_bytes);
10719 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10720 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10721 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10722 }
10723
3e43a32a
MS
10724 /* Copy the saved-up fields into the field vector. Start from the head of
10725 the list, adding to the tail of the field array, so that they end up in
10726 the same order in the array in which they were added to the list. */
c906108c
SS
10727 while (nfields-- > 0)
10728 {
7d0ccb61
DJ
10729 struct nextfield *fieldp;
10730
10731 if (fip->fields)
10732 {
10733 fieldp = fip->fields;
10734 fip->fields = fieldp->next;
10735 }
10736 else
10737 {
10738 fieldp = fip->baseclasses;
10739 fip->baseclasses = fieldp->next;
10740 }
10741
10742 TYPE_FIELD (type, nfields) = fieldp->field;
10743 switch (fieldp->accessibility)
c906108c 10744 {
c5aa993b 10745 case DW_ACCESS_private:
b4ba55a1
JB
10746 if (cu->language != language_ada)
10747 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10748 break;
c906108c 10749
c5aa993b 10750 case DW_ACCESS_protected:
b4ba55a1
JB
10751 if (cu->language != language_ada)
10752 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10753 break;
c906108c 10754
c5aa993b
JM
10755 case DW_ACCESS_public:
10756 break;
c906108c 10757
c5aa993b
JM
10758 default:
10759 /* Unknown accessibility. Complain and treat it as public. */
10760 {
e2e0b3e5 10761 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10762 fieldp->accessibility);
c5aa993b
JM
10763 }
10764 break;
c906108c
SS
10765 }
10766 if (nfields < fip->nbaseclasses)
10767 {
7d0ccb61 10768 switch (fieldp->virtuality)
c906108c 10769 {
c5aa993b
JM
10770 case DW_VIRTUALITY_virtual:
10771 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10772 if (cu->language == language_ada)
a73c6dcd 10773 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10774 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10775 break;
c906108c
SS
10776 }
10777 }
c906108c
SS
10778 }
10779}
10780
c906108c
SS
10781/* Add a member function to the proper fieldlist. */
10782
10783static void
107d2387 10784dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10785 struct type *type, struct dwarf2_cu *cu)
c906108c 10786{
e7c27a73 10787 struct objfile *objfile = cu->objfile;
c906108c
SS
10788 struct attribute *attr;
10789 struct fnfieldlist *flp;
10790 int i;
10791 struct fn_field *fnp;
10792 char *fieldname;
c906108c 10793 struct nextfnfield *new_fnfield;
f792889a 10794 struct type *this_type;
60d5a603 10795 enum dwarf_access_attribute accessibility;
c906108c 10796
b4ba55a1 10797 if (cu->language == language_ada)
a73c6dcd 10798 error (_("unexpected member function in Ada type"));
b4ba55a1 10799
2df3850c 10800 /* Get name of member function. */
39cbfefa
DJ
10801 fieldname = dwarf2_name (die, cu);
10802 if (fieldname == NULL)
2df3850c 10803 return;
c906108c 10804
c906108c
SS
10805 /* Look up member function name in fieldlist. */
10806 for (i = 0; i < fip->nfnfields; i++)
10807 {
27bfe10e 10808 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10809 break;
10810 }
10811
10812 /* Create new list element if necessary. */
10813 if (i < fip->nfnfields)
10814 flp = &fip->fnfieldlists[i];
10815 else
10816 {
10817 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10818 {
10819 fip->fnfieldlists = (struct fnfieldlist *)
10820 xrealloc (fip->fnfieldlists,
10821 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10822 * sizeof (struct fnfieldlist));
c906108c 10823 if (fip->nfnfields == 0)
c13c43fd 10824 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10825 }
10826 flp = &fip->fnfieldlists[fip->nfnfields];
10827 flp->name = fieldname;
10828 flp->length = 0;
10829 flp->head = NULL;
3da10d80 10830 i = fip->nfnfields++;
c906108c
SS
10831 }
10832
10833 /* Create a new member function field and chain it to the field list
0963b4bd 10834 entry. */
c906108c 10835 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10836 make_cleanup (xfree, new_fnfield);
c906108c
SS
10837 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10838 new_fnfield->next = flp->head;
10839 flp->head = new_fnfield;
10840 flp->length++;
10841
10842 /* Fill in the member function field info. */
10843 fnp = &new_fnfield->fnfield;
3da10d80
KS
10844
10845 /* Delay processing of the physname until later. */
10846 if (cu->language == language_cplus || cu->language == language_java)
10847 {
10848 add_to_method_list (type, i, flp->length - 1, fieldname,
10849 die, cu);
10850 }
10851 else
10852 {
1d06ead6 10853 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10854 fnp->physname = physname ? physname : "";
10855 }
10856
c906108c 10857 fnp->type = alloc_type (objfile);
f792889a
DJ
10858 this_type = read_type_die (die, cu);
10859 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10860 {
f792889a 10861 int nparams = TYPE_NFIELDS (this_type);
c906108c 10862
f792889a 10863 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10864 of the method itself (TYPE_CODE_METHOD). */
10865 smash_to_method_type (fnp->type, type,
f792889a
DJ
10866 TYPE_TARGET_TYPE (this_type),
10867 TYPE_FIELDS (this_type),
10868 TYPE_NFIELDS (this_type),
10869 TYPE_VARARGS (this_type));
c906108c
SS
10870
10871 /* Handle static member functions.
c5aa993b 10872 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10873 member functions. G++ helps GDB by marking the first
10874 parameter for non-static member functions (which is the this
10875 pointer) as artificial. We obtain this information from
10876 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10877 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10878 fnp->voffset = VOFFSET_STATIC;
10879 }
10880 else
e2e0b3e5 10881 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10882 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10883
10884 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10885 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10886 fnp->fcontext = die_containing_type (die, cu);
c906108c 10887
3e43a32a
MS
10888 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10889 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10890
10891 /* Get accessibility. */
e142c38c 10892 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10893 if (attr)
60d5a603
JK
10894 accessibility = DW_UNSND (attr);
10895 else
10896 accessibility = dwarf2_default_access_attribute (die, cu);
10897 switch (accessibility)
c906108c 10898 {
60d5a603
JK
10899 case DW_ACCESS_private:
10900 fnp->is_private = 1;
10901 break;
10902 case DW_ACCESS_protected:
10903 fnp->is_protected = 1;
10904 break;
c906108c
SS
10905 }
10906
b02dede2 10907 /* Check for artificial methods. */
e142c38c 10908 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10909 if (attr && DW_UNSND (attr) != 0)
10910 fnp->is_artificial = 1;
10911
0d564a31 10912 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10913 function. For older versions of GCC, this is an offset in the
10914 appropriate virtual table, as specified by DW_AT_containing_type.
10915 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10916 to the object address. */
10917
e142c38c 10918 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10919 if (attr)
8e19ed76 10920 {
aec5aa8b 10921 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10922 {
aec5aa8b
TT
10923 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10924 {
10925 /* Old-style GCC. */
10926 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10927 }
10928 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10929 || (DW_BLOCK (attr)->size > 1
10930 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10931 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10932 {
10933 struct dwarf_block blk;
10934 int offset;
10935
10936 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10937 ? 1 : 2);
10938 blk.size = DW_BLOCK (attr)->size - offset;
10939 blk.data = DW_BLOCK (attr)->data + offset;
10940 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10941 if ((fnp->voffset % cu->header.addr_size) != 0)
10942 dwarf2_complex_location_expr_complaint ();
10943 else
10944 fnp->voffset /= cu->header.addr_size;
10945 fnp->voffset += 2;
10946 }
10947 else
10948 dwarf2_complex_location_expr_complaint ();
10949
10950 if (!fnp->fcontext)
10951 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10952 }
3690dd37 10953 else if (attr_form_is_section_offset (attr))
8e19ed76 10954 {
4d3c2250 10955 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10956 }
10957 else
10958 {
4d3c2250
KB
10959 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10960 fieldname);
8e19ed76 10961 }
0d564a31 10962 }
d48cc9dd
DJ
10963 else
10964 {
10965 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10966 if (attr && DW_UNSND (attr))
10967 {
10968 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10969 complaint (&symfile_complaints,
3e43a32a
MS
10970 _("Member function \"%s\" (offset %d) is virtual "
10971 "but the vtable offset is not specified"),
b64f50a1 10972 fieldname, die->offset.sect_off);
9655fd1a 10973 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
10974 TYPE_CPLUS_DYNAMIC (type) = 1;
10975 }
10976 }
c906108c
SS
10977}
10978
10979/* Create the vector of member function fields, and attach it to the type. */
10980
10981static void
fba45db2 10982dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10983 struct dwarf2_cu *cu)
c906108c
SS
10984{
10985 struct fnfieldlist *flp;
c906108c
SS
10986 int i;
10987
b4ba55a1 10988 if (cu->language == language_ada)
a73c6dcd 10989 error (_("unexpected member functions in Ada type"));
b4ba55a1 10990
c906108c
SS
10991 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10992 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10993 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10994
10995 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10996 {
10997 struct nextfnfield *nfp = flp->head;
10998 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10999 int k;
11000
11001 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11002 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11003 fn_flp->fn_fields = (struct fn_field *)
11004 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11005 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11006 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11007 }
11008
11009 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11010}
11011
1168df01
JB
11012/* Returns non-zero if NAME is the name of a vtable member in CU's
11013 language, zero otherwise. */
11014static int
11015is_vtable_name (const char *name, struct dwarf2_cu *cu)
11016{
11017 static const char vptr[] = "_vptr";
987504bb 11018 static const char vtable[] = "vtable";
1168df01 11019
987504bb
JJ
11020 /* Look for the C++ and Java forms of the vtable. */
11021 if ((cu->language == language_java
11022 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11023 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11024 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11025 return 1;
11026
11027 return 0;
11028}
11029
c0dd20ea 11030/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11031 functions, with the ABI-specified layout. If TYPE describes
11032 such a structure, smash it into a member function type.
61049d3b
DJ
11033
11034 GCC shouldn't do this; it should just output pointer to member DIEs.
11035 This is GCC PR debug/28767. */
c0dd20ea 11036
0b92b5bb
TT
11037static void
11038quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11039{
0b92b5bb 11040 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11041
11042 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11043 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11044 return;
c0dd20ea
DJ
11045
11046 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11047 if (TYPE_FIELD_NAME (type, 0) == NULL
11048 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11049 || TYPE_FIELD_NAME (type, 1) == NULL
11050 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11051 return;
c0dd20ea
DJ
11052
11053 /* Find the type of the method. */
0b92b5bb 11054 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11055 if (pfn_type == NULL
11056 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11057 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11058 return;
c0dd20ea
DJ
11059
11060 /* Look for the "this" argument. */
11061 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11062 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11063 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11064 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11065 return;
c0dd20ea
DJ
11066
11067 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11068 new_type = alloc_type (objfile);
11069 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11070 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11071 TYPE_VARARGS (pfn_type));
0b92b5bb 11072 smash_to_methodptr_type (type, new_type);
c0dd20ea 11073}
1168df01 11074
685b1105
JK
11075/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11076 (icc). */
11077
11078static int
11079producer_is_icc (struct dwarf2_cu *cu)
11080{
11081 if (!cu->checked_producer)
11082 check_producer (cu);
11083
11084 return cu->producer_is_icc;
11085}
11086
c906108c 11087/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11088 (definition) to create a type for the structure or union. Fill in
11089 the type's name and general properties; the members will not be
11090 processed until process_structure_type.
c906108c 11091
c767944b
DJ
11092 NOTE: we need to call these functions regardless of whether or not the
11093 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11094 structure or union. This gets the type entered into our set of
11095 user defined types.
11096
11097 However, if the structure is incomplete (an opaque struct/union)
11098 then suppress creating a symbol table entry for it since gdb only
11099 wants to find the one with the complete definition. Note that if
11100 it is complete, we just call new_symbol, which does it's own
11101 checking about whether the struct/union is anonymous or not (and
11102 suppresses creating a symbol table entry itself). */
11103
f792889a 11104static struct type *
134d01f1 11105read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11106{
e7c27a73 11107 struct objfile *objfile = cu->objfile;
c906108c
SS
11108 struct type *type;
11109 struct attribute *attr;
39cbfefa 11110 char *name;
c906108c 11111
348e048f
DE
11112 /* If the definition of this type lives in .debug_types, read that type.
11113 Don't follow DW_AT_specification though, that will take us back up
11114 the chain and we want to go down. */
45e58e77 11115 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11116 if (attr)
11117 {
11118 struct dwarf2_cu *type_cu = cu;
11119 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11120
348e048f
DE
11121 /* We could just recurse on read_structure_type, but we need to call
11122 get_die_type to ensure only one type for this DIE is created.
11123 This is important, for example, because for c++ classes we need
11124 TYPE_NAME set which is only done by new_symbol. Blech. */
11125 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11126
11127 /* TYPE_CU may not be the same as CU.
11128 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11129 return set_die_type (die, type, cu);
11130 }
11131
c0dd20ea 11132 type = alloc_type (objfile);
c906108c 11133 INIT_CPLUS_SPECIFIC (type);
93311388 11134
39cbfefa
DJ
11135 name = dwarf2_name (die, cu);
11136 if (name != NULL)
c906108c 11137 {
987504bb
JJ
11138 if (cu->language == language_cplus
11139 || cu->language == language_java)
63d06c5c 11140 {
3da10d80
KS
11141 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11142
11143 /* dwarf2_full_name might have already finished building the DIE's
11144 type. If so, there is no need to continue. */
11145 if (get_die_type (die, cu) != NULL)
11146 return get_die_type (die, cu);
11147
11148 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11149 if (die->tag == DW_TAG_structure_type
11150 || die->tag == DW_TAG_class_type)
11151 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11152 }
11153 else
11154 {
d8151005
DJ
11155 /* The name is already allocated along with this objfile, so
11156 we don't need to duplicate it for the type. */
94af9270
KS
11157 TYPE_TAG_NAME (type) = (char *) name;
11158 if (die->tag == DW_TAG_class_type)
11159 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11160 }
c906108c
SS
11161 }
11162
11163 if (die->tag == DW_TAG_structure_type)
11164 {
11165 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11166 }
11167 else if (die->tag == DW_TAG_union_type)
11168 {
11169 TYPE_CODE (type) = TYPE_CODE_UNION;
11170 }
11171 else
11172 {
c906108c
SS
11173 TYPE_CODE (type) = TYPE_CODE_CLASS;
11174 }
11175
0cc2414c
TT
11176 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11177 TYPE_DECLARED_CLASS (type) = 1;
11178
e142c38c 11179 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11180 if (attr)
11181 {
11182 TYPE_LENGTH (type) = DW_UNSND (attr);
11183 }
11184 else
11185 {
11186 TYPE_LENGTH (type) = 0;
11187 }
11188
685b1105
JK
11189 if (producer_is_icc (cu))
11190 {
11191 /* ICC does not output the required DW_AT_declaration
11192 on incomplete types, but gives them a size of zero. */
11193 }
11194 else
11195 TYPE_STUB_SUPPORTED (type) = 1;
11196
dc718098 11197 if (die_is_declaration (die, cu))
876cecd0 11198 TYPE_STUB (type) = 1;
a6c727b2
DJ
11199 else if (attr == NULL && die->child == NULL
11200 && producer_is_realview (cu->producer))
11201 /* RealView does not output the required DW_AT_declaration
11202 on incomplete types. */
11203 TYPE_STUB (type) = 1;
dc718098 11204
c906108c
SS
11205 /* We need to add the type field to the die immediately so we don't
11206 infinitely recurse when dealing with pointers to the structure
0963b4bd 11207 type within the structure itself. */
1c379e20 11208 set_die_type (die, type, cu);
c906108c 11209
7e314c57
JK
11210 /* set_die_type should be already done. */
11211 set_descriptive_type (type, die, cu);
11212
c767944b
DJ
11213 return type;
11214}
11215
11216/* Finish creating a structure or union type, including filling in
11217 its members and creating a symbol for it. */
11218
11219static void
11220process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11221{
11222 struct objfile *objfile = cu->objfile;
11223 struct die_info *child_die = die->child;
11224 struct type *type;
11225
11226 type = get_die_type (die, cu);
11227 if (type == NULL)
11228 type = read_structure_type (die, cu);
11229
e142c38c 11230 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11231 {
11232 struct field_info fi;
11233 struct die_info *child_die;
34eaf542 11234 VEC (symbolp) *template_args = NULL;
c767944b 11235 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11236
11237 memset (&fi, 0, sizeof (struct field_info));
11238
639d11d3 11239 child_die = die->child;
c906108c
SS
11240
11241 while (child_die && child_die->tag)
11242 {
a9a9bd0f
DC
11243 if (child_die->tag == DW_TAG_member
11244 || child_die->tag == DW_TAG_variable)
c906108c 11245 {
a9a9bd0f
DC
11246 /* NOTE: carlton/2002-11-05: A C++ static data member
11247 should be a DW_TAG_member that is a declaration, but
11248 all versions of G++ as of this writing (so through at
11249 least 3.2.1) incorrectly generate DW_TAG_variable
11250 tags for them instead. */
e7c27a73 11251 dwarf2_add_field (&fi, child_die, cu);
c906108c 11252 }
8713b1b1 11253 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11254 {
0963b4bd 11255 /* C++ member function. */
e7c27a73 11256 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11257 }
11258 else if (child_die->tag == DW_TAG_inheritance)
11259 {
11260 /* C++ base class field. */
e7c27a73 11261 dwarf2_add_field (&fi, child_die, cu);
c906108c 11262 }
98751a41
JK
11263 else if (child_die->tag == DW_TAG_typedef)
11264 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11265 else if (child_die->tag == DW_TAG_template_type_param
11266 || child_die->tag == DW_TAG_template_value_param)
11267 {
11268 struct symbol *arg = new_symbol (child_die, NULL, cu);
11269
f1078f66
DJ
11270 if (arg != NULL)
11271 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11272 }
11273
c906108c
SS
11274 child_die = sibling_die (child_die);
11275 }
11276
34eaf542
TT
11277 /* Attach template arguments to type. */
11278 if (! VEC_empty (symbolp, template_args))
11279 {
11280 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11281 TYPE_N_TEMPLATE_ARGUMENTS (type)
11282 = VEC_length (symbolp, template_args);
11283 TYPE_TEMPLATE_ARGUMENTS (type)
11284 = obstack_alloc (&objfile->objfile_obstack,
11285 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11286 * sizeof (struct symbol *)));
11287 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11288 VEC_address (symbolp, template_args),
11289 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11290 * sizeof (struct symbol *)));
11291 VEC_free (symbolp, template_args);
11292 }
11293
c906108c
SS
11294 /* Attach fields and member functions to the type. */
11295 if (fi.nfields)
e7c27a73 11296 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11297 if (fi.nfnfields)
11298 {
e7c27a73 11299 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11300
c5aa993b 11301 /* Get the type which refers to the base class (possibly this
c906108c 11302 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11303 class from the DW_AT_containing_type attribute. This use of
11304 DW_AT_containing_type is a GNU extension. */
c906108c 11305
e142c38c 11306 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11307 {
e7c27a73 11308 struct type *t = die_containing_type (die, cu);
c906108c
SS
11309
11310 TYPE_VPTR_BASETYPE (type) = t;
11311 if (type == t)
11312 {
c906108c
SS
11313 int i;
11314
11315 /* Our own class provides vtbl ptr. */
11316 for (i = TYPE_NFIELDS (t) - 1;
11317 i >= TYPE_N_BASECLASSES (t);
11318 --i)
11319 {
0d5cff50 11320 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11321
1168df01 11322 if (is_vtable_name (fieldname, cu))
c906108c
SS
11323 {
11324 TYPE_VPTR_FIELDNO (type) = i;
11325 break;
11326 }
11327 }
11328
11329 /* Complain if virtual function table field not found. */
11330 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11331 complaint (&symfile_complaints,
3e43a32a
MS
11332 _("virtual function table pointer "
11333 "not found when defining class '%s'"),
4d3c2250
KB
11334 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11335 "");
c906108c
SS
11336 }
11337 else
11338 {
11339 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11340 }
11341 }
f6235d4c
EZ
11342 else if (cu->producer
11343 && strncmp (cu->producer,
11344 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11345 {
11346 /* The IBM XLC compiler does not provide direct indication
11347 of the containing type, but the vtable pointer is
11348 always named __vfp. */
11349
11350 int i;
11351
11352 for (i = TYPE_NFIELDS (type) - 1;
11353 i >= TYPE_N_BASECLASSES (type);
11354 --i)
11355 {
11356 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11357 {
11358 TYPE_VPTR_FIELDNO (type) = i;
11359 TYPE_VPTR_BASETYPE (type) = type;
11360 break;
11361 }
11362 }
11363 }
c906108c 11364 }
98751a41
JK
11365
11366 /* Copy fi.typedef_field_list linked list elements content into the
11367 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11368 if (fi.typedef_field_list)
11369 {
11370 int i = fi.typedef_field_list_count;
11371
a0d7a4ff 11372 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11373 TYPE_TYPEDEF_FIELD_ARRAY (type)
11374 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11375 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11376
11377 /* Reverse the list order to keep the debug info elements order. */
11378 while (--i >= 0)
11379 {
11380 struct typedef_field *dest, *src;
6e70227d 11381
98751a41
JK
11382 dest = &TYPE_TYPEDEF_FIELD (type, i);
11383 src = &fi.typedef_field_list->field;
11384 fi.typedef_field_list = fi.typedef_field_list->next;
11385 *dest = *src;
11386 }
11387 }
c767944b
DJ
11388
11389 do_cleanups (back_to);
eb2a6f42
TT
11390
11391 if (HAVE_CPLUS_STRUCT (type))
11392 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11393 }
63d06c5c 11394
bb5ed363 11395 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11396
90aeadfc
DC
11397 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11398 snapshots) has been known to create a die giving a declaration
11399 for a class that has, as a child, a die giving a definition for a
11400 nested class. So we have to process our children even if the
11401 current die is a declaration. Normally, of course, a declaration
11402 won't have any children at all. */
134d01f1 11403
90aeadfc
DC
11404 while (child_die != NULL && child_die->tag)
11405 {
11406 if (child_die->tag == DW_TAG_member
11407 || child_die->tag == DW_TAG_variable
34eaf542
TT
11408 || child_die->tag == DW_TAG_inheritance
11409 || child_die->tag == DW_TAG_template_value_param
11410 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11411 {
90aeadfc 11412 /* Do nothing. */
134d01f1 11413 }
90aeadfc
DC
11414 else
11415 process_die (child_die, cu);
134d01f1 11416
90aeadfc 11417 child_die = sibling_die (child_die);
134d01f1
DJ
11418 }
11419
fa4028e9
JB
11420 /* Do not consider external references. According to the DWARF standard,
11421 these DIEs are identified by the fact that they have no byte_size
11422 attribute, and a declaration attribute. */
11423 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11424 || !die_is_declaration (die, cu))
c767944b 11425 new_symbol (die, type, cu);
134d01f1
DJ
11426}
11427
11428/* Given a DW_AT_enumeration_type die, set its type. We do not
11429 complete the type's fields yet, or create any symbols. */
c906108c 11430
f792889a 11431static struct type *
134d01f1 11432read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11433{
e7c27a73 11434 struct objfile *objfile = cu->objfile;
c906108c 11435 struct type *type;
c906108c 11436 struct attribute *attr;
0114d602 11437 const char *name;
134d01f1 11438
348e048f
DE
11439 /* If the definition of this type lives in .debug_types, read that type.
11440 Don't follow DW_AT_specification though, that will take us back up
11441 the chain and we want to go down. */
45e58e77 11442 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11443 if (attr)
11444 {
11445 struct dwarf2_cu *type_cu = cu;
11446 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11447
348e048f 11448 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11449
11450 /* TYPE_CU may not be the same as CU.
11451 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11452 return set_die_type (die, type, cu);
11453 }
11454
c906108c
SS
11455 type = alloc_type (objfile);
11456
11457 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11458 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11459 if (name != NULL)
0114d602 11460 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11461
e142c38c 11462 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11463 if (attr)
11464 {
11465 TYPE_LENGTH (type) = DW_UNSND (attr);
11466 }
11467 else
11468 {
11469 TYPE_LENGTH (type) = 0;
11470 }
11471
137033e9
JB
11472 /* The enumeration DIE can be incomplete. In Ada, any type can be
11473 declared as private in the package spec, and then defined only
11474 inside the package body. Such types are known as Taft Amendment
11475 Types. When another package uses such a type, an incomplete DIE
11476 may be generated by the compiler. */
02eb380e 11477 if (die_is_declaration (die, cu))
876cecd0 11478 TYPE_STUB (type) = 1;
02eb380e 11479
f792889a 11480 return set_die_type (die, type, cu);
134d01f1
DJ
11481}
11482
11483/* Given a pointer to a die which begins an enumeration, process all
11484 the dies that define the members of the enumeration, and create the
11485 symbol for the enumeration type.
11486
11487 NOTE: We reverse the order of the element list. */
11488
11489static void
11490process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11491{
f792889a 11492 struct type *this_type;
134d01f1 11493
f792889a
DJ
11494 this_type = get_die_type (die, cu);
11495 if (this_type == NULL)
11496 this_type = read_enumeration_type (die, cu);
9dc481d3 11497
639d11d3 11498 if (die->child != NULL)
c906108c 11499 {
9dc481d3
DE
11500 struct die_info *child_die;
11501 struct symbol *sym;
11502 struct field *fields = NULL;
11503 int num_fields = 0;
11504 int unsigned_enum = 1;
11505 char *name;
cafec441
TT
11506 int flag_enum = 1;
11507 ULONGEST mask = 0;
9dc481d3 11508
639d11d3 11509 child_die = die->child;
c906108c
SS
11510 while (child_die && child_die->tag)
11511 {
11512 if (child_die->tag != DW_TAG_enumerator)
11513 {
e7c27a73 11514 process_die (child_die, cu);
c906108c
SS
11515 }
11516 else
11517 {
39cbfefa
DJ
11518 name = dwarf2_name (child_die, cu);
11519 if (name)
c906108c 11520 {
f792889a 11521 sym = new_symbol (child_die, this_type, cu);
c906108c 11522 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11523 {
11524 unsigned_enum = 0;
11525 flag_enum = 0;
11526 }
11527 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11528 flag_enum = 0;
11529 else
11530 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11531
11532 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11533 {
11534 fields = (struct field *)
11535 xrealloc (fields,
11536 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11537 * sizeof (struct field));
c906108c
SS
11538 }
11539
3567439c 11540 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11541 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11542 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11543 FIELD_BITSIZE (fields[num_fields]) = 0;
11544
11545 num_fields++;
11546 }
11547 }
11548
11549 child_die = sibling_die (child_die);
11550 }
11551
11552 if (num_fields)
11553 {
f792889a
DJ
11554 TYPE_NFIELDS (this_type) = num_fields;
11555 TYPE_FIELDS (this_type) = (struct field *)
11556 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11557 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11558 sizeof (struct field) * num_fields);
b8c9b27d 11559 xfree (fields);
c906108c
SS
11560 }
11561 if (unsigned_enum)
876cecd0 11562 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11563 if (flag_enum)
11564 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11565 }
134d01f1 11566
6c83ed52
TT
11567 /* If we are reading an enum from a .debug_types unit, and the enum
11568 is a declaration, and the enum is not the signatured type in the
11569 unit, then we do not want to add a symbol for it. Adding a
11570 symbol would in some cases obscure the true definition of the
11571 enum, giving users an incomplete type when the definition is
11572 actually available. Note that we do not want to do this for all
11573 enums which are just declarations, because C++0x allows forward
11574 enum declarations. */
3019eac3 11575 if (cu->per_cu->is_debug_types
6c83ed52
TT
11576 && die_is_declaration (die, cu))
11577 {
52dc124a 11578 struct signatured_type *sig_type;
6c83ed52 11579
52dc124a 11580 sig_type
6c83ed52 11581 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11582 cu->per_cu->info_or_types_section,
6c83ed52 11583 cu->per_cu->offset);
3019eac3
DE
11584 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11585 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11586 return;
11587 }
11588
f792889a 11589 new_symbol (die, this_type, cu);
c906108c
SS
11590}
11591
11592/* Extract all information from a DW_TAG_array_type DIE and put it in
11593 the DIE's type field. For now, this only handles one dimensional
11594 arrays. */
11595
f792889a 11596static struct type *
e7c27a73 11597read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11598{
e7c27a73 11599 struct objfile *objfile = cu->objfile;
c906108c 11600 struct die_info *child_die;
7e314c57 11601 struct type *type;
c906108c
SS
11602 struct type *element_type, *range_type, *index_type;
11603 struct type **range_types = NULL;
11604 struct attribute *attr;
11605 int ndim = 0;
11606 struct cleanup *back_to;
39cbfefa 11607 char *name;
c906108c 11608
e7c27a73 11609 element_type = die_type (die, cu);
c906108c 11610
7e314c57
JK
11611 /* The die_type call above may have already set the type for this DIE. */
11612 type = get_die_type (die, cu);
11613 if (type)
11614 return type;
11615
c906108c
SS
11616 /* Irix 6.2 native cc creates array types without children for
11617 arrays with unspecified length. */
639d11d3 11618 if (die->child == NULL)
c906108c 11619 {
46bf5051 11620 index_type = objfile_type (objfile)->builtin_int;
c906108c 11621 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11622 type = create_array_type (NULL, element_type, range_type);
11623 return set_die_type (die, type, cu);
c906108c
SS
11624 }
11625
11626 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11627 child_die = die->child;
c906108c
SS
11628 while (child_die && child_die->tag)
11629 {
11630 if (child_die->tag == DW_TAG_subrange_type)
11631 {
f792889a 11632 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11633
f792889a 11634 if (child_type != NULL)
a02abb62 11635 {
0963b4bd
MS
11636 /* The range type was succesfully read. Save it for the
11637 array type creation. */
a02abb62
JB
11638 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11639 {
11640 range_types = (struct type **)
11641 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11642 * sizeof (struct type *));
11643 if (ndim == 0)
11644 make_cleanup (free_current_contents, &range_types);
11645 }
f792889a 11646 range_types[ndim++] = child_type;
a02abb62 11647 }
c906108c
SS
11648 }
11649 child_die = sibling_die (child_die);
11650 }
11651
11652 /* Dwarf2 dimensions are output from left to right, create the
11653 necessary array types in backwards order. */
7ca2d3a3 11654
c906108c 11655 type = element_type;
7ca2d3a3
DL
11656
11657 if (read_array_order (die, cu) == DW_ORD_col_major)
11658 {
11659 int i = 0;
9a619af0 11660
7ca2d3a3
DL
11661 while (i < ndim)
11662 type = create_array_type (NULL, type, range_types[i++]);
11663 }
11664 else
11665 {
11666 while (ndim-- > 0)
11667 type = create_array_type (NULL, type, range_types[ndim]);
11668 }
c906108c 11669
f5f8a009
EZ
11670 /* Understand Dwarf2 support for vector types (like they occur on
11671 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11672 array type. This is not part of the Dwarf2/3 standard yet, but a
11673 custom vendor extension. The main difference between a regular
11674 array and the vector variant is that vectors are passed by value
11675 to functions. */
e142c38c 11676 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11677 if (attr)
ea37ba09 11678 make_vector_type (type);
f5f8a009 11679
dbc98a8b
KW
11680 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11681 implementation may choose to implement triple vectors using this
11682 attribute. */
11683 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11684 if (attr)
11685 {
11686 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11687 TYPE_LENGTH (type) = DW_UNSND (attr);
11688 else
3e43a32a
MS
11689 complaint (&symfile_complaints,
11690 _("DW_AT_byte_size for array type smaller "
11691 "than the total size of elements"));
dbc98a8b
KW
11692 }
11693
39cbfefa
DJ
11694 name = dwarf2_name (die, cu);
11695 if (name)
11696 TYPE_NAME (type) = name;
6e70227d 11697
0963b4bd 11698 /* Install the type in the die. */
7e314c57
JK
11699 set_die_type (die, type, cu);
11700
11701 /* set_die_type should be already done. */
b4ba55a1
JB
11702 set_descriptive_type (type, die, cu);
11703
c906108c
SS
11704 do_cleanups (back_to);
11705
7e314c57 11706 return type;
c906108c
SS
11707}
11708
7ca2d3a3 11709static enum dwarf_array_dim_ordering
6e70227d 11710read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11711{
11712 struct attribute *attr;
11713
11714 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11715
11716 if (attr) return DW_SND (attr);
11717
0963b4bd
MS
11718 /* GNU F77 is a special case, as at 08/2004 array type info is the
11719 opposite order to the dwarf2 specification, but data is still
11720 laid out as per normal fortran.
7ca2d3a3 11721
0963b4bd
MS
11722 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11723 version checking. */
7ca2d3a3 11724
905e0470
PM
11725 if (cu->language == language_fortran
11726 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11727 {
11728 return DW_ORD_row_major;
11729 }
11730
6e70227d 11731 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11732 {
11733 case array_column_major:
11734 return DW_ORD_col_major;
11735 case array_row_major:
11736 default:
11737 return DW_ORD_row_major;
11738 };
11739}
11740
72019c9c 11741/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11742 the DIE's type field. */
72019c9c 11743
f792889a 11744static struct type *
72019c9c
GM
11745read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11746{
7e314c57
JK
11747 struct type *domain_type, *set_type;
11748 struct attribute *attr;
f792889a 11749
7e314c57
JK
11750 domain_type = die_type (die, cu);
11751
11752 /* The die_type call above may have already set the type for this DIE. */
11753 set_type = get_die_type (die, cu);
11754 if (set_type)
11755 return set_type;
11756
11757 set_type = create_set_type (NULL, domain_type);
11758
11759 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11760 if (attr)
11761 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11762
f792889a 11763 return set_die_type (die, set_type, cu);
72019c9c 11764}
7ca2d3a3 11765
0971de02
TT
11766/* A helper for read_common_block that creates a locexpr baton.
11767 SYM is the symbol which we are marking as computed.
11768 COMMON_DIE is the DIE for the common block.
11769 COMMON_LOC is the location expression attribute for the common
11770 block itself.
11771 MEMBER_LOC is the location expression attribute for the particular
11772 member of the common block that we are processing.
11773 CU is the CU from which the above come. */
11774
11775static void
11776mark_common_block_symbol_computed (struct symbol *sym,
11777 struct die_info *common_die,
11778 struct attribute *common_loc,
11779 struct attribute *member_loc,
11780 struct dwarf2_cu *cu)
11781{
11782 struct objfile *objfile = dwarf2_per_objfile->objfile;
11783 struct dwarf2_locexpr_baton *baton;
11784 gdb_byte *ptr;
11785 unsigned int cu_off;
11786 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11787 LONGEST offset = 0;
11788
11789 gdb_assert (common_loc && member_loc);
11790 gdb_assert (attr_form_is_block (common_loc));
11791 gdb_assert (attr_form_is_block (member_loc)
11792 || attr_form_is_constant (member_loc));
11793
11794 baton = obstack_alloc (&objfile->objfile_obstack,
11795 sizeof (struct dwarf2_locexpr_baton));
11796 baton->per_cu = cu->per_cu;
11797 gdb_assert (baton->per_cu);
11798
11799 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11800
11801 if (attr_form_is_constant (member_loc))
11802 {
11803 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11804 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11805 }
11806 else
11807 baton->size += DW_BLOCK (member_loc)->size;
11808
11809 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11810 baton->data = ptr;
11811
11812 *ptr++ = DW_OP_call4;
11813 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11814 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11815 ptr += 4;
11816
11817 if (attr_form_is_constant (member_loc))
11818 {
11819 *ptr++ = DW_OP_addr;
11820 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11821 ptr += cu->header.addr_size;
11822 }
11823 else
11824 {
11825 /* We have to copy the data here, because DW_OP_call4 will only
11826 use a DW_AT_location attribute. */
11827 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11828 ptr += DW_BLOCK (member_loc)->size;
11829 }
11830
11831 *ptr++ = DW_OP_plus;
11832 gdb_assert (ptr - baton->data == baton->size);
11833
11834 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11835 SYMBOL_LOCATION_BATON (sym) = baton;
11836 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11837}
11838
4357ac6c
TT
11839/* Create appropriate locally-scoped variables for all the
11840 DW_TAG_common_block entries. Also create a struct common_block
11841 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11842 is used to sepate the common blocks name namespace from regular
11843 variable names. */
c906108c
SS
11844
11845static void
e7c27a73 11846read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11847{
0971de02
TT
11848 struct attribute *attr;
11849
11850 attr = dwarf2_attr (die, DW_AT_location, cu);
11851 if (attr)
11852 {
11853 /* Support the .debug_loc offsets. */
11854 if (attr_form_is_block (attr))
11855 {
11856 /* Ok. */
11857 }
11858 else if (attr_form_is_section_offset (attr))
11859 {
11860 dwarf2_complex_location_expr_complaint ();
11861 attr = NULL;
11862 }
11863 else
11864 {
11865 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11866 "common block member");
11867 attr = NULL;
11868 }
11869 }
11870
639d11d3 11871 if (die->child != NULL)
c906108c 11872 {
4357ac6c
TT
11873 struct objfile *objfile = cu->objfile;
11874 struct die_info *child_die;
11875 size_t n_entries = 0, size;
11876 struct common_block *common_block;
11877 struct symbol *sym;
74ac6d43 11878
4357ac6c
TT
11879 for (child_die = die->child;
11880 child_die && child_die->tag;
11881 child_die = sibling_die (child_die))
11882 ++n_entries;
11883
11884 size = (sizeof (struct common_block)
11885 + (n_entries - 1) * sizeof (struct symbol *));
11886 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11887 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11888 common_block->n_entries = 0;
11889
11890 for (child_die = die->child;
11891 child_die && child_die->tag;
11892 child_die = sibling_die (child_die))
11893 {
11894 /* Create the symbol in the DW_TAG_common_block block in the current
11895 symbol scope. */
e7c27a73 11896 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11897 if (sym != NULL)
11898 {
11899 struct attribute *member_loc;
11900
11901 common_block->contents[common_block->n_entries++] = sym;
11902
11903 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11904 cu);
11905 if (member_loc)
11906 {
11907 /* GDB has handled this for a long time, but it is
11908 not specified by DWARF. It seems to have been
11909 emitted by gfortran at least as recently as:
11910 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11911 complaint (&symfile_complaints,
11912 _("Variable in common block has "
11913 "DW_AT_data_member_location "
11914 "- DIE at 0x%x [in module %s]"),
11915 child_die->offset.sect_off, cu->objfile->name);
11916
11917 if (attr_form_is_section_offset (member_loc))
11918 dwarf2_complex_location_expr_complaint ();
11919 else if (attr_form_is_constant (member_loc)
11920 || attr_form_is_block (member_loc))
11921 {
11922 if (attr)
11923 mark_common_block_symbol_computed (sym, die, attr,
11924 member_loc, cu);
11925 }
11926 else
11927 dwarf2_complex_location_expr_complaint ();
11928 }
11929 }
c906108c 11930 }
4357ac6c
TT
11931
11932 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11933 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
11934 }
11935}
11936
0114d602 11937/* Create a type for a C++ namespace. */
d9fa45fe 11938
0114d602
DJ
11939static struct type *
11940read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11941{
e7c27a73 11942 struct objfile *objfile = cu->objfile;
0114d602 11943 const char *previous_prefix, *name;
9219021c 11944 int is_anonymous;
0114d602
DJ
11945 struct type *type;
11946
11947 /* For extensions, reuse the type of the original namespace. */
11948 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11949 {
11950 struct die_info *ext_die;
11951 struct dwarf2_cu *ext_cu = cu;
9a619af0 11952
0114d602
DJ
11953 ext_die = dwarf2_extension (die, &ext_cu);
11954 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11955
11956 /* EXT_CU may not be the same as CU.
11957 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11958 return set_die_type (die, type, cu);
11959 }
9219021c 11960
e142c38c 11961 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11962
11963 /* Now build the name of the current namespace. */
11964
0114d602
DJ
11965 previous_prefix = determine_prefix (die, cu);
11966 if (previous_prefix[0] != '\0')
11967 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11968 previous_prefix, name, 0, cu);
0114d602
DJ
11969
11970 /* Create the type. */
11971 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11972 objfile);
11973 TYPE_NAME (type) = (char *) name;
11974 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11975
60531b24 11976 return set_die_type (die, type, cu);
0114d602
DJ
11977}
11978
11979/* Read a C++ namespace. */
11980
11981static void
11982read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11983{
11984 struct objfile *objfile = cu->objfile;
0114d602 11985 int is_anonymous;
9219021c 11986
5c4e30ca
DC
11987 /* Add a symbol associated to this if we haven't seen the namespace
11988 before. Also, add a using directive if it's an anonymous
11989 namespace. */
9219021c 11990
f2f0e013 11991 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
11992 {
11993 struct type *type;
11994
0114d602 11995 type = read_type_die (die, cu);
e7c27a73 11996 new_symbol (die, type, cu);
5c4e30ca 11997
e8e80198 11998 namespace_name (die, &is_anonymous, cu);
5c4e30ca 11999 if (is_anonymous)
0114d602
DJ
12000 {
12001 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12002
c0cc3a76 12003 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 12004 NULL, NULL, &objfile->objfile_obstack);
0114d602 12005 }
5c4e30ca 12006 }
9219021c 12007
639d11d3 12008 if (die->child != NULL)
d9fa45fe 12009 {
639d11d3 12010 struct die_info *child_die = die->child;
6e70227d 12011
d9fa45fe
DC
12012 while (child_die && child_die->tag)
12013 {
e7c27a73 12014 process_die (child_die, cu);
d9fa45fe
DC
12015 child_die = sibling_die (child_die);
12016 }
12017 }
38d518c9
EZ
12018}
12019
f55ee35c
JK
12020/* Read a Fortran module as type. This DIE can be only a declaration used for
12021 imported module. Still we need that type as local Fortran "use ... only"
12022 declaration imports depend on the created type in determine_prefix. */
12023
12024static struct type *
12025read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12026{
12027 struct objfile *objfile = cu->objfile;
12028 char *module_name;
12029 struct type *type;
12030
12031 module_name = dwarf2_name (die, cu);
12032 if (!module_name)
3e43a32a
MS
12033 complaint (&symfile_complaints,
12034 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12035 die->offset.sect_off);
f55ee35c
JK
12036 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12037
12038 /* determine_prefix uses TYPE_TAG_NAME. */
12039 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12040
12041 return set_die_type (die, type, cu);
12042}
12043
5d7cb8df
JK
12044/* Read a Fortran module. */
12045
12046static void
12047read_module (struct die_info *die, struct dwarf2_cu *cu)
12048{
12049 struct die_info *child_die = die->child;
12050
5d7cb8df
JK
12051 while (child_die && child_die->tag)
12052 {
12053 process_die (child_die, cu);
12054 child_die = sibling_die (child_die);
12055 }
12056}
12057
38d518c9
EZ
12058/* Return the name of the namespace represented by DIE. Set
12059 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12060 namespace. */
12061
12062static const char *
e142c38c 12063namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12064{
12065 struct die_info *current_die;
12066 const char *name = NULL;
12067
12068 /* Loop through the extensions until we find a name. */
12069
12070 for (current_die = die;
12071 current_die != NULL;
f2f0e013 12072 current_die = dwarf2_extension (die, &cu))
38d518c9 12073 {
e142c38c 12074 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12075 if (name != NULL)
12076 break;
12077 }
12078
12079 /* Is it an anonymous namespace? */
12080
12081 *is_anonymous = (name == NULL);
12082 if (*is_anonymous)
2b1dbab0 12083 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12084
12085 return name;
d9fa45fe
DC
12086}
12087
c906108c
SS
12088/* Extract all information from a DW_TAG_pointer_type DIE and add to
12089 the user defined type vector. */
12090
f792889a 12091static struct type *
e7c27a73 12092read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12093{
5e2b427d 12094 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12095 struct comp_unit_head *cu_header = &cu->header;
c906108c 12096 struct type *type;
8b2dbe47
KB
12097 struct attribute *attr_byte_size;
12098 struct attribute *attr_address_class;
12099 int byte_size, addr_class;
7e314c57
JK
12100 struct type *target_type;
12101
12102 target_type = die_type (die, cu);
c906108c 12103
7e314c57
JK
12104 /* The die_type call above may have already set the type for this DIE. */
12105 type = get_die_type (die, cu);
12106 if (type)
12107 return type;
12108
12109 type = lookup_pointer_type (target_type);
8b2dbe47 12110
e142c38c 12111 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12112 if (attr_byte_size)
12113 byte_size = DW_UNSND (attr_byte_size);
c906108c 12114 else
8b2dbe47
KB
12115 byte_size = cu_header->addr_size;
12116
e142c38c 12117 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12118 if (attr_address_class)
12119 addr_class = DW_UNSND (attr_address_class);
12120 else
12121 addr_class = DW_ADDR_none;
12122
12123 /* If the pointer size or address class is different than the
12124 default, create a type variant marked as such and set the
12125 length accordingly. */
12126 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12127 {
5e2b427d 12128 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12129 {
12130 int type_flags;
12131
849957d9 12132 type_flags = gdbarch_address_class_type_flags
5e2b427d 12133 (gdbarch, byte_size, addr_class);
876cecd0
TT
12134 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12135 == 0);
8b2dbe47
KB
12136 type = make_type_with_address_space (type, type_flags);
12137 }
12138 else if (TYPE_LENGTH (type) != byte_size)
12139 {
3e43a32a
MS
12140 complaint (&symfile_complaints,
12141 _("invalid pointer size %d"), byte_size);
8b2dbe47 12142 }
6e70227d 12143 else
9a619af0
MS
12144 {
12145 /* Should we also complain about unhandled address classes? */
12146 }
c906108c 12147 }
8b2dbe47
KB
12148
12149 TYPE_LENGTH (type) = byte_size;
f792889a 12150 return set_die_type (die, type, cu);
c906108c
SS
12151}
12152
12153/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12154 the user defined type vector. */
12155
f792889a 12156static struct type *
e7c27a73 12157read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12158{
12159 struct type *type;
12160 struct type *to_type;
12161 struct type *domain;
12162
e7c27a73
DJ
12163 to_type = die_type (die, cu);
12164 domain = die_containing_type (die, cu);
0d5de010 12165
7e314c57
JK
12166 /* The calls above may have already set the type for this DIE. */
12167 type = get_die_type (die, cu);
12168 if (type)
12169 return type;
12170
0d5de010
DJ
12171 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12172 type = lookup_methodptr_type (to_type);
12173 else
12174 type = lookup_memberptr_type (to_type, domain);
c906108c 12175
f792889a 12176 return set_die_type (die, type, cu);
c906108c
SS
12177}
12178
12179/* Extract all information from a DW_TAG_reference_type DIE and add to
12180 the user defined type vector. */
12181
f792889a 12182static struct type *
e7c27a73 12183read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12184{
e7c27a73 12185 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12186 struct type *type, *target_type;
c906108c
SS
12187 struct attribute *attr;
12188
7e314c57
JK
12189 target_type = die_type (die, cu);
12190
12191 /* The die_type call above may have already set the type for this DIE. */
12192 type = get_die_type (die, cu);
12193 if (type)
12194 return type;
12195
12196 type = lookup_reference_type (target_type);
e142c38c 12197 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12198 if (attr)
12199 {
12200 TYPE_LENGTH (type) = DW_UNSND (attr);
12201 }
12202 else
12203 {
107d2387 12204 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12205 }
f792889a 12206 return set_die_type (die, type, cu);
c906108c
SS
12207}
12208
f792889a 12209static struct type *
e7c27a73 12210read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12211{
f792889a 12212 struct type *base_type, *cv_type;
c906108c 12213
e7c27a73 12214 base_type = die_type (die, cu);
7e314c57
JK
12215
12216 /* The die_type call above may have already set the type for this DIE. */
12217 cv_type = get_die_type (die, cu);
12218 if (cv_type)
12219 return cv_type;
12220
2f608a3a
KW
12221 /* In case the const qualifier is applied to an array type, the element type
12222 is so qualified, not the array type (section 6.7.3 of C99). */
12223 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12224 {
12225 struct type *el_type, *inner_array;
12226
12227 base_type = copy_type (base_type);
12228 inner_array = base_type;
12229
12230 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12231 {
12232 TYPE_TARGET_TYPE (inner_array) =
12233 copy_type (TYPE_TARGET_TYPE (inner_array));
12234 inner_array = TYPE_TARGET_TYPE (inner_array);
12235 }
12236
12237 el_type = TYPE_TARGET_TYPE (inner_array);
12238 TYPE_TARGET_TYPE (inner_array) =
12239 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12240
12241 return set_die_type (die, base_type, cu);
12242 }
12243
f792889a
DJ
12244 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12245 return set_die_type (die, cv_type, cu);
c906108c
SS
12246}
12247
f792889a 12248static struct type *
e7c27a73 12249read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12250{
f792889a 12251 struct type *base_type, *cv_type;
c906108c 12252
e7c27a73 12253 base_type = die_type (die, cu);
7e314c57
JK
12254
12255 /* The die_type call above may have already set the type for this DIE. */
12256 cv_type = get_die_type (die, cu);
12257 if (cv_type)
12258 return cv_type;
12259
f792889a
DJ
12260 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12261 return set_die_type (die, cv_type, cu);
c906108c
SS
12262}
12263
12264/* Extract all information from a DW_TAG_string_type DIE and add to
12265 the user defined type vector. It isn't really a user defined type,
12266 but it behaves like one, with other DIE's using an AT_user_def_type
12267 attribute to reference it. */
12268
f792889a 12269static struct type *
e7c27a73 12270read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12271{
e7c27a73 12272 struct objfile *objfile = cu->objfile;
3b7538c0 12273 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12274 struct type *type, *range_type, *index_type, *char_type;
12275 struct attribute *attr;
12276 unsigned int length;
12277
e142c38c 12278 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12279 if (attr)
12280 {
12281 length = DW_UNSND (attr);
12282 }
12283 else
12284 {
0963b4bd 12285 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12286 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12287 if (attr)
12288 {
12289 length = DW_UNSND (attr);
12290 }
12291 else
12292 {
12293 length = 1;
12294 }
c906108c 12295 }
6ccb9162 12296
46bf5051 12297 index_type = objfile_type (objfile)->builtin_int;
c906108c 12298 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12299 char_type = language_string_char_type (cu->language_defn, gdbarch);
12300 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12301
f792889a 12302 return set_die_type (die, type, cu);
c906108c
SS
12303}
12304
12305/* Handle DIES due to C code like:
12306
12307 struct foo
c5aa993b
JM
12308 {
12309 int (*funcp)(int a, long l);
12310 int b;
12311 };
c906108c 12312
0963b4bd 12313 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12314
f792889a 12315static struct type *
e7c27a73 12316read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12317{
bb5ed363 12318 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12319 struct type *type; /* Type that this function returns. */
12320 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12321 struct attribute *attr;
12322
e7c27a73 12323 type = die_type (die, cu);
7e314c57
JK
12324
12325 /* The die_type call above may have already set the type for this DIE. */
12326 ftype = get_die_type (die, cu);
12327 if (ftype)
12328 return ftype;
12329
0c8b41f1 12330 ftype = lookup_function_type (type);
c906108c 12331
5b8101ae 12332 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12333 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12334 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12335 || cu->language == language_cplus
5b8101ae
PM
12336 || cu->language == language_java
12337 || cu->language == language_pascal)
876cecd0 12338 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12339 else if (producer_is_realview (cu->producer))
12340 /* RealView does not emit DW_AT_prototyped. We can not
12341 distinguish prototyped and unprototyped functions; default to
12342 prototyped, since that is more common in modern code (and
12343 RealView warns about unprototyped functions). */
12344 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12345
c055b101
CV
12346 /* Store the calling convention in the type if it's available in
12347 the subroutine die. Otherwise set the calling convention to
12348 the default value DW_CC_normal. */
12349 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12350 if (attr)
12351 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12352 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12353 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12354 else
12355 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12356
12357 /* We need to add the subroutine type to the die immediately so
12358 we don't infinitely recurse when dealing with parameters
0963b4bd 12359 declared as the same subroutine type. */
76c10ea2 12360 set_die_type (die, ftype, cu);
6e70227d 12361
639d11d3 12362 if (die->child != NULL)
c906108c 12363 {
bb5ed363 12364 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12365 struct die_info *child_die;
8072405b 12366 int nparams, iparams;
c906108c
SS
12367
12368 /* Count the number of parameters.
12369 FIXME: GDB currently ignores vararg functions, but knows about
12370 vararg member functions. */
8072405b 12371 nparams = 0;
639d11d3 12372 child_die = die->child;
c906108c
SS
12373 while (child_die && child_die->tag)
12374 {
12375 if (child_die->tag == DW_TAG_formal_parameter)
12376 nparams++;
12377 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12378 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12379 child_die = sibling_die (child_die);
12380 }
12381
12382 /* Allocate storage for parameters and fill them in. */
12383 TYPE_NFIELDS (ftype) = nparams;
12384 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12385 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12386
8072405b
JK
12387 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12388 even if we error out during the parameters reading below. */
12389 for (iparams = 0; iparams < nparams; iparams++)
12390 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12391
12392 iparams = 0;
639d11d3 12393 child_die = die->child;
c906108c
SS
12394 while (child_die && child_die->tag)
12395 {
12396 if (child_die->tag == DW_TAG_formal_parameter)
12397 {
3ce3b1ba
PA
12398 struct type *arg_type;
12399
12400 /* DWARF version 2 has no clean way to discern C++
12401 static and non-static member functions. G++ helps
12402 GDB by marking the first parameter for non-static
12403 member functions (which is the this pointer) as
12404 artificial. We pass this information to
12405 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12406
12407 DWARF version 3 added DW_AT_object_pointer, which GCC
12408 4.5 does not yet generate. */
e142c38c 12409 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12410 if (attr)
12411 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12412 else
418835cc
KS
12413 {
12414 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12415
12416 /* GCC/43521: In java, the formal parameter
12417 "this" is sometimes not marked with DW_AT_artificial. */
12418 if (cu->language == language_java)
12419 {
12420 const char *name = dwarf2_name (child_die, cu);
9a619af0 12421
418835cc
KS
12422 if (name && !strcmp (name, "this"))
12423 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12424 }
12425 }
3ce3b1ba
PA
12426 arg_type = die_type (child_die, cu);
12427
12428 /* RealView does not mark THIS as const, which the testsuite
12429 expects. GCC marks THIS as const in method definitions,
12430 but not in the class specifications (GCC PR 43053). */
12431 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12432 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12433 {
12434 int is_this = 0;
12435 struct dwarf2_cu *arg_cu = cu;
12436 const char *name = dwarf2_name (child_die, cu);
12437
12438 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12439 if (attr)
12440 {
12441 /* If the compiler emits this, use it. */
12442 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12443 is_this = 1;
12444 }
12445 else if (name && strcmp (name, "this") == 0)
12446 /* Function definitions will have the argument names. */
12447 is_this = 1;
12448 else if (name == NULL && iparams == 0)
12449 /* Declarations may not have the names, so like
12450 elsewhere in GDB, assume an artificial first
12451 argument is "this". */
12452 is_this = 1;
12453
12454 if (is_this)
12455 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12456 arg_type, 0);
12457 }
12458
12459 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12460 iparams++;
12461 }
12462 child_die = sibling_die (child_die);
12463 }
12464 }
12465
76c10ea2 12466 return ftype;
c906108c
SS
12467}
12468
f792889a 12469static struct type *
e7c27a73 12470read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12471{
e7c27a73 12472 struct objfile *objfile = cu->objfile;
0114d602 12473 const char *name = NULL;
3c8e0968 12474 struct type *this_type, *target_type;
c906108c 12475
94af9270 12476 name = dwarf2_full_name (NULL, die, cu);
f792889a 12477 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12478 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12479 TYPE_NAME (this_type) = (char *) name;
f792889a 12480 set_die_type (die, this_type, cu);
3c8e0968
DE
12481 target_type = die_type (die, cu);
12482 if (target_type != this_type)
12483 TYPE_TARGET_TYPE (this_type) = target_type;
12484 else
12485 {
12486 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12487 spec and cause infinite loops in GDB. */
12488 complaint (&symfile_complaints,
12489 _("Self-referential DW_TAG_typedef "
12490 "- DIE at 0x%x [in module %s]"),
b64f50a1 12491 die->offset.sect_off, objfile->name);
3c8e0968
DE
12492 TYPE_TARGET_TYPE (this_type) = NULL;
12493 }
f792889a 12494 return this_type;
c906108c
SS
12495}
12496
12497/* Find a representation of a given base type and install
12498 it in the TYPE field of the die. */
12499
f792889a 12500static struct type *
e7c27a73 12501read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12502{
e7c27a73 12503 struct objfile *objfile = cu->objfile;
c906108c
SS
12504 struct type *type;
12505 struct attribute *attr;
12506 int encoding = 0, size = 0;
39cbfefa 12507 char *name;
6ccb9162
UW
12508 enum type_code code = TYPE_CODE_INT;
12509 int type_flags = 0;
12510 struct type *target_type = NULL;
c906108c 12511
e142c38c 12512 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12513 if (attr)
12514 {
12515 encoding = DW_UNSND (attr);
12516 }
e142c38c 12517 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12518 if (attr)
12519 {
12520 size = DW_UNSND (attr);
12521 }
39cbfefa 12522 name = dwarf2_name (die, cu);
6ccb9162 12523 if (!name)
c906108c 12524 {
6ccb9162
UW
12525 complaint (&symfile_complaints,
12526 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12527 }
6ccb9162
UW
12528
12529 switch (encoding)
c906108c 12530 {
6ccb9162
UW
12531 case DW_ATE_address:
12532 /* Turn DW_ATE_address into a void * pointer. */
12533 code = TYPE_CODE_PTR;
12534 type_flags |= TYPE_FLAG_UNSIGNED;
12535 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12536 break;
12537 case DW_ATE_boolean:
12538 code = TYPE_CODE_BOOL;
12539 type_flags |= TYPE_FLAG_UNSIGNED;
12540 break;
12541 case DW_ATE_complex_float:
12542 code = TYPE_CODE_COMPLEX;
12543 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12544 break;
12545 case DW_ATE_decimal_float:
12546 code = TYPE_CODE_DECFLOAT;
12547 break;
12548 case DW_ATE_float:
12549 code = TYPE_CODE_FLT;
12550 break;
12551 case DW_ATE_signed:
12552 break;
12553 case DW_ATE_unsigned:
12554 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12555 if (cu->language == language_fortran
12556 && name
12557 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12558 code = TYPE_CODE_CHAR;
6ccb9162
UW
12559 break;
12560 case DW_ATE_signed_char:
6e70227d 12561 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12562 || cu->language == language_pascal
12563 || cu->language == language_fortran)
6ccb9162
UW
12564 code = TYPE_CODE_CHAR;
12565 break;
12566 case DW_ATE_unsigned_char:
868a0084 12567 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12568 || cu->language == language_pascal
12569 || cu->language == language_fortran)
6ccb9162
UW
12570 code = TYPE_CODE_CHAR;
12571 type_flags |= TYPE_FLAG_UNSIGNED;
12572 break;
75079b2b
TT
12573 case DW_ATE_UTF:
12574 /* We just treat this as an integer and then recognize the
12575 type by name elsewhere. */
12576 break;
12577
6ccb9162
UW
12578 default:
12579 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12580 dwarf_type_encoding_name (encoding));
12581 break;
c906108c 12582 }
6ccb9162 12583
0114d602
DJ
12584 type = init_type (code, size, type_flags, NULL, objfile);
12585 TYPE_NAME (type) = name;
6ccb9162
UW
12586 TYPE_TARGET_TYPE (type) = target_type;
12587
0114d602 12588 if (name && strcmp (name, "char") == 0)
876cecd0 12589 TYPE_NOSIGN (type) = 1;
0114d602 12590
f792889a 12591 return set_die_type (die, type, cu);
c906108c
SS
12592}
12593
a02abb62
JB
12594/* Read the given DW_AT_subrange DIE. */
12595
f792889a 12596static struct type *
a02abb62
JB
12597read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12598{
12599 struct type *base_type;
12600 struct type *range_type;
12601 struct attribute *attr;
4fae6e18
JK
12602 LONGEST low, high;
12603 int low_default_is_valid;
39cbfefa 12604 char *name;
43bbcdc2 12605 LONGEST negative_mask;
e77813c8 12606
a02abb62 12607 base_type = die_type (die, cu);
953ac07e
JK
12608 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12609 check_typedef (base_type);
a02abb62 12610
7e314c57
JK
12611 /* The die_type call above may have already set the type for this DIE. */
12612 range_type = get_die_type (die, cu);
12613 if (range_type)
12614 return range_type;
12615
4fae6e18
JK
12616 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12617 omitting DW_AT_lower_bound. */
12618 switch (cu->language)
6e70227d 12619 {
4fae6e18
JK
12620 case language_c:
12621 case language_cplus:
12622 low = 0;
12623 low_default_is_valid = 1;
12624 break;
12625 case language_fortran:
12626 low = 1;
12627 low_default_is_valid = 1;
12628 break;
12629 case language_d:
12630 case language_java:
12631 case language_objc:
12632 low = 0;
12633 low_default_is_valid = (cu->header.version >= 4);
12634 break;
12635 case language_ada:
12636 case language_m2:
12637 case language_pascal:
a02abb62 12638 low = 1;
4fae6e18
JK
12639 low_default_is_valid = (cu->header.version >= 4);
12640 break;
12641 default:
12642 low = 0;
12643 low_default_is_valid = 0;
12644 break;
a02abb62
JB
12645 }
12646
dd5e6932
DJ
12647 /* FIXME: For variable sized arrays either of these could be
12648 a variable rather than a constant value. We'll allow it,
12649 but we don't know how to handle it. */
e142c38c 12650 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12651 if (attr)
4fae6e18
JK
12652 low = dwarf2_get_attr_constant_value (attr, low);
12653 else if (!low_default_is_valid)
12654 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12655 "- DIE at 0x%x [in module %s]"),
12656 die->offset.sect_off, cu->objfile->name);
a02abb62 12657
e142c38c 12658 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12659 if (attr)
6e70227d 12660 {
d48323d8 12661 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12662 {
12663 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12664 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12665 FIXME: GDB does not yet know how to handle dynamic
12666 arrays properly, treat them as arrays with unspecified
12667 length for now.
12668
12669 FIXME: jimb/2003-09-22: GDB does not really know
12670 how to handle arrays of unspecified length
12671 either; we just represent them as zero-length
12672 arrays. Choose an appropriate upper bound given
12673 the lower bound we've computed above. */
12674 high = low - 1;
12675 }
12676 else
12677 high = dwarf2_get_attr_constant_value (attr, 1);
12678 }
e77813c8
PM
12679 else
12680 {
12681 attr = dwarf2_attr (die, DW_AT_count, cu);
12682 if (attr)
12683 {
12684 int count = dwarf2_get_attr_constant_value (attr, 1);
12685 high = low + count - 1;
12686 }
c2ff108b
JK
12687 else
12688 {
12689 /* Unspecified array length. */
12690 high = low - 1;
12691 }
e77813c8
PM
12692 }
12693
12694 /* Dwarf-2 specifications explicitly allows to create subrange types
12695 without specifying a base type.
12696 In that case, the base type must be set to the type of
12697 the lower bound, upper bound or count, in that order, if any of these
12698 three attributes references an object that has a type.
12699 If no base type is found, the Dwarf-2 specifications say that
12700 a signed integer type of size equal to the size of an address should
12701 be used.
12702 For the following C code: `extern char gdb_int [];'
12703 GCC produces an empty range DIE.
12704 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12705 high bound or count are not yet handled by this code. */
e77813c8
PM
12706 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12707 {
12708 struct objfile *objfile = cu->objfile;
12709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12710 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12711 struct type *int_type = objfile_type (objfile)->builtin_int;
12712
12713 /* Test "int", "long int", and "long long int" objfile types,
12714 and select the first one having a size above or equal to the
12715 architecture address size. */
12716 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12717 base_type = int_type;
12718 else
12719 {
12720 int_type = objfile_type (objfile)->builtin_long;
12721 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12722 base_type = int_type;
12723 else
12724 {
12725 int_type = objfile_type (objfile)->builtin_long_long;
12726 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12727 base_type = int_type;
12728 }
12729 }
12730 }
a02abb62 12731
6e70227d 12732 negative_mask =
43bbcdc2
PH
12733 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12734 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12735 low |= negative_mask;
12736 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12737 high |= negative_mask;
12738
a02abb62
JB
12739 range_type = create_range_type (NULL, base_type, low, high);
12740
bbb0eef6
JK
12741 /* Mark arrays with dynamic length at least as an array of unspecified
12742 length. GDB could check the boundary but before it gets implemented at
12743 least allow accessing the array elements. */
d48323d8 12744 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12745 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12746
c2ff108b
JK
12747 /* Ada expects an empty array on no boundary attributes. */
12748 if (attr == NULL && cu->language != language_ada)
12749 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12750
39cbfefa
DJ
12751 name = dwarf2_name (die, cu);
12752 if (name)
12753 TYPE_NAME (range_type) = name;
6e70227d 12754
e142c38c 12755 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12756 if (attr)
12757 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12758
7e314c57
JK
12759 set_die_type (die, range_type, cu);
12760
12761 /* set_die_type should be already done. */
b4ba55a1
JB
12762 set_descriptive_type (range_type, die, cu);
12763
7e314c57 12764 return range_type;
a02abb62 12765}
6e70227d 12766
f792889a 12767static struct type *
81a17f79
JB
12768read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12769{
12770 struct type *type;
81a17f79 12771
81a17f79
JB
12772 /* For now, we only support the C meaning of an unspecified type: void. */
12773
0114d602
DJ
12774 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12775 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12776
f792889a 12777 return set_die_type (die, type, cu);
81a17f79 12778}
a02abb62 12779
639d11d3
DC
12780/* Read a single die and all its descendents. Set the die's sibling
12781 field to NULL; set other fields in the die correctly, and set all
12782 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12783 location of the info_ptr after reading all of those dies. PARENT
12784 is the parent of the die in question. */
12785
12786static struct die_info *
dee91e82
DE
12787read_die_and_children (const struct die_reader_specs *reader,
12788 gdb_byte *info_ptr,
12789 gdb_byte **new_info_ptr,
12790 struct die_info *parent)
639d11d3
DC
12791{
12792 struct die_info *die;
fe1b8b76 12793 gdb_byte *cur_ptr;
639d11d3
DC
12794 int has_children;
12795
93311388 12796 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12797 if (die == NULL)
12798 {
12799 *new_info_ptr = cur_ptr;
12800 return NULL;
12801 }
93311388 12802 store_in_ref_table (die, reader->cu);
639d11d3
DC
12803
12804 if (has_children)
348e048f 12805 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12806 else
12807 {
12808 die->child = NULL;
12809 *new_info_ptr = cur_ptr;
12810 }
12811
12812 die->sibling = NULL;
12813 die->parent = parent;
12814 return die;
12815}
12816
12817/* Read a die, all of its descendents, and all of its siblings; set
12818 all of the fields of all of the dies correctly. Arguments are as
12819 in read_die_and_children. */
12820
12821static struct die_info *
93311388
DE
12822read_die_and_siblings (const struct die_reader_specs *reader,
12823 gdb_byte *info_ptr,
fe1b8b76 12824 gdb_byte **new_info_ptr,
639d11d3
DC
12825 struct die_info *parent)
12826{
12827 struct die_info *first_die, *last_sibling;
fe1b8b76 12828 gdb_byte *cur_ptr;
639d11d3 12829
c906108c 12830 cur_ptr = info_ptr;
639d11d3
DC
12831 first_die = last_sibling = NULL;
12832
12833 while (1)
c906108c 12834 {
639d11d3 12835 struct die_info *die
dee91e82 12836 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12837
1d325ec1 12838 if (die == NULL)
c906108c 12839 {
639d11d3
DC
12840 *new_info_ptr = cur_ptr;
12841 return first_die;
c906108c 12842 }
1d325ec1
DJ
12843
12844 if (!first_die)
12845 first_die = die;
c906108c 12846 else
1d325ec1
DJ
12847 last_sibling->sibling = die;
12848
12849 last_sibling = die;
c906108c 12850 }
c906108c
SS
12851}
12852
3019eac3
DE
12853/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12854 attributes.
12855 The caller is responsible for filling in the extra attributes
12856 and updating (*DIEP)->num_attrs.
12857 Set DIEP to point to a newly allocated die with its information,
12858 except for its child, sibling, and parent fields.
12859 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12860
12861static gdb_byte *
3019eac3
DE
12862read_full_die_1 (const struct die_reader_specs *reader,
12863 struct die_info **diep, gdb_byte *info_ptr,
12864 int *has_children, int num_extra_attrs)
93311388 12865{
b64f50a1
JK
12866 unsigned int abbrev_number, bytes_read, i;
12867 sect_offset offset;
93311388
DE
12868 struct abbrev_info *abbrev;
12869 struct die_info *die;
12870 struct dwarf2_cu *cu = reader->cu;
12871 bfd *abfd = reader->abfd;
12872
b64f50a1 12873 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12874 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12875 info_ptr += bytes_read;
12876 if (!abbrev_number)
12877 {
12878 *diep = NULL;
12879 *has_children = 0;
12880 return info_ptr;
12881 }
12882
433df2d4 12883 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12884 if (!abbrev)
348e048f
DE
12885 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12886 abbrev_number,
12887 bfd_get_filename (abfd));
12888
3019eac3 12889 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12890 die->offset = offset;
12891 die->tag = abbrev->tag;
12892 die->abbrev = abbrev_number;
12893
3019eac3
DE
12894 /* Make the result usable.
12895 The caller needs to update num_attrs after adding the extra
12896 attributes. */
93311388
DE
12897 die->num_attrs = abbrev->num_attrs;
12898
12899 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12900 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12901 info_ptr);
93311388
DE
12902
12903 *diep = die;
12904 *has_children = abbrev->has_children;
12905 return info_ptr;
12906}
12907
3019eac3
DE
12908/* Read a die and all its attributes.
12909 Set DIEP to point to a newly allocated die with its information,
12910 except for its child, sibling, and parent fields.
12911 Set HAS_CHILDREN to tell whether the die has children or not. */
12912
12913static gdb_byte *
12914read_full_die (const struct die_reader_specs *reader,
12915 struct die_info **diep, gdb_byte *info_ptr,
12916 int *has_children)
12917{
12918 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12919}
433df2d4
DE
12920\f
12921/* Abbreviation tables.
3019eac3 12922
433df2d4 12923 In DWARF version 2, the description of the debugging information is
c906108c
SS
12924 stored in a separate .debug_abbrev section. Before we read any
12925 dies from a section we read in all abbreviations and install them
433df2d4
DE
12926 in a hash table. */
12927
12928/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12929
12930static struct abbrev_info *
12931abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12932{
12933 struct abbrev_info *abbrev;
12934
12935 abbrev = (struct abbrev_info *)
12936 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12937 memset (abbrev, 0, sizeof (struct abbrev_info));
12938 return abbrev;
12939}
12940
12941/* Add an abbreviation to the table. */
c906108c
SS
12942
12943static void
433df2d4
DE
12944abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12945 unsigned int abbrev_number,
12946 struct abbrev_info *abbrev)
12947{
12948 unsigned int hash_number;
12949
12950 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12951 abbrev->next = abbrev_table->abbrevs[hash_number];
12952 abbrev_table->abbrevs[hash_number] = abbrev;
12953}
dee91e82 12954
433df2d4
DE
12955/* Look up an abbrev in the table.
12956 Returns NULL if the abbrev is not found. */
12957
12958static struct abbrev_info *
12959abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12960 unsigned int abbrev_number)
c906108c 12961{
433df2d4
DE
12962 unsigned int hash_number;
12963 struct abbrev_info *abbrev;
12964
12965 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12966 abbrev = abbrev_table->abbrevs[hash_number];
12967
12968 while (abbrev)
12969 {
12970 if (abbrev->number == abbrev_number)
12971 return abbrev;
12972 abbrev = abbrev->next;
12973 }
12974 return NULL;
12975}
12976
12977/* Read in an abbrev table. */
12978
12979static struct abbrev_table *
12980abbrev_table_read_table (struct dwarf2_section_info *section,
12981 sect_offset offset)
12982{
12983 struct objfile *objfile = dwarf2_per_objfile->objfile;
12984 bfd *abfd = section->asection->owner;
12985 struct abbrev_table *abbrev_table;
fe1b8b76 12986 gdb_byte *abbrev_ptr;
c906108c
SS
12987 struct abbrev_info *cur_abbrev;
12988 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 12989 unsigned int abbrev_form;
f3dd6933
DJ
12990 struct attr_abbrev *cur_attrs;
12991 unsigned int allocated_attrs;
c906108c 12992
433df2d4 12993 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 12994 abbrev_table->offset = offset;
433df2d4
DE
12995 obstack_init (&abbrev_table->abbrev_obstack);
12996 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12997 (ABBREV_HASH_SIZE
12998 * sizeof (struct abbrev_info *)));
12999 memset (abbrev_table->abbrevs, 0,
13000 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13001
433df2d4
DE
13002 dwarf2_read_section (objfile, section);
13003 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13004 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13005 abbrev_ptr += bytes_read;
13006
f3dd6933
DJ
13007 allocated_attrs = ATTR_ALLOC_CHUNK;
13008 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13009
0963b4bd 13010 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13011 while (abbrev_number)
13012 {
433df2d4 13013 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13014
13015 /* read in abbrev header */
13016 cur_abbrev->number = abbrev_number;
13017 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13018 abbrev_ptr += bytes_read;
13019 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13020 abbrev_ptr += 1;
13021
13022 /* now read in declarations */
13023 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13024 abbrev_ptr += bytes_read;
13025 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13026 abbrev_ptr += bytes_read;
13027 while (abbrev_name)
13028 {
f3dd6933 13029 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13030 {
f3dd6933
DJ
13031 allocated_attrs += ATTR_ALLOC_CHUNK;
13032 cur_attrs
13033 = xrealloc (cur_attrs, (allocated_attrs
13034 * sizeof (struct attr_abbrev)));
c906108c 13035 }
ae038cb0 13036
f3dd6933
DJ
13037 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13038 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13039 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13040 abbrev_ptr += bytes_read;
13041 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13042 abbrev_ptr += bytes_read;
13043 }
13044
433df2d4 13045 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13046 (cur_abbrev->num_attrs
13047 * sizeof (struct attr_abbrev)));
13048 memcpy (cur_abbrev->attrs, cur_attrs,
13049 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13050
433df2d4 13051 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13052
13053 /* Get next abbreviation.
13054 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13055 always properly terminated with an abbrev number of 0.
13056 Exit loop if we encounter an abbreviation which we have
13057 already read (which means we are about to read the abbreviations
13058 for the next compile unit) or if the end of the abbreviation
13059 table is reached. */
433df2d4 13060 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13061 break;
13062 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13063 abbrev_ptr += bytes_read;
433df2d4 13064 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13065 break;
13066 }
f3dd6933
DJ
13067
13068 xfree (cur_attrs);
433df2d4 13069 return abbrev_table;
c906108c
SS
13070}
13071
433df2d4 13072/* Free the resources held by ABBREV_TABLE. */
c906108c 13073
c906108c 13074static void
433df2d4 13075abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13076{
433df2d4
DE
13077 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13078 xfree (abbrev_table);
c906108c
SS
13079}
13080
f4dc4d17
DE
13081/* Same as abbrev_table_free but as a cleanup.
13082 We pass in a pointer to the pointer to the table so that we can
13083 set the pointer to NULL when we're done. It also simplifies
13084 build_type_unit_groups. */
13085
13086static void
13087abbrev_table_free_cleanup (void *table_ptr)
13088{
13089 struct abbrev_table **abbrev_table_ptr = table_ptr;
13090
13091 if (*abbrev_table_ptr != NULL)
13092 abbrev_table_free (*abbrev_table_ptr);
13093 *abbrev_table_ptr = NULL;
13094}
13095
433df2d4
DE
13096/* Read the abbrev table for CU from ABBREV_SECTION. */
13097
13098static void
13099dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13100 struct dwarf2_section_info *abbrev_section)
c906108c 13101{
433df2d4
DE
13102 cu->abbrev_table =
13103 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13104}
c906108c 13105
433df2d4 13106/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13107
433df2d4
DE
13108static void
13109dwarf2_free_abbrev_table (void *ptr_to_cu)
13110{
13111 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13112
433df2d4
DE
13113 abbrev_table_free (cu->abbrev_table);
13114 /* Set this to NULL so that we SEGV if we try to read it later,
13115 and also because free_comp_unit verifies this is NULL. */
13116 cu->abbrev_table = NULL;
13117}
13118\f
72bf9492
DJ
13119/* Returns nonzero if TAG represents a type that we might generate a partial
13120 symbol for. */
13121
13122static int
13123is_type_tag_for_partial (int tag)
13124{
13125 switch (tag)
13126 {
13127#if 0
13128 /* Some types that would be reasonable to generate partial symbols for,
13129 that we don't at present. */
13130 case DW_TAG_array_type:
13131 case DW_TAG_file_type:
13132 case DW_TAG_ptr_to_member_type:
13133 case DW_TAG_set_type:
13134 case DW_TAG_string_type:
13135 case DW_TAG_subroutine_type:
13136#endif
13137 case DW_TAG_base_type:
13138 case DW_TAG_class_type:
680b30c7 13139 case DW_TAG_interface_type:
72bf9492
DJ
13140 case DW_TAG_enumeration_type:
13141 case DW_TAG_structure_type:
13142 case DW_TAG_subrange_type:
13143 case DW_TAG_typedef:
13144 case DW_TAG_union_type:
13145 return 1;
13146 default:
13147 return 0;
13148 }
13149}
13150
13151/* Load all DIEs that are interesting for partial symbols into memory. */
13152
13153static struct partial_die_info *
dee91e82
DE
13154load_partial_dies (const struct die_reader_specs *reader,
13155 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13156{
dee91e82 13157 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13158 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13159 struct partial_die_info *part_die;
13160 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13161 struct abbrev_info *abbrev;
13162 unsigned int bytes_read;
5afb4e99 13163 unsigned int load_all = 0;
72bf9492
DJ
13164 int nesting_level = 1;
13165
13166 parent_die = NULL;
13167 last_die = NULL;
13168
7adf1e79
DE
13169 gdb_assert (cu->per_cu != NULL);
13170 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13171 load_all = 1;
13172
72bf9492
DJ
13173 cu->partial_dies
13174 = htab_create_alloc_ex (cu->header.length / 12,
13175 partial_die_hash,
13176 partial_die_eq,
13177 NULL,
13178 &cu->comp_unit_obstack,
13179 hashtab_obstack_allocate,
13180 dummy_obstack_deallocate);
13181
13182 part_die = obstack_alloc (&cu->comp_unit_obstack,
13183 sizeof (struct partial_die_info));
13184
13185 while (1)
13186 {
13187 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13188
13189 /* A NULL abbrev means the end of a series of children. */
13190 if (abbrev == NULL)
13191 {
13192 if (--nesting_level == 0)
13193 {
13194 /* PART_DIE was probably the last thing allocated on the
13195 comp_unit_obstack, so we could call obstack_free
13196 here. We don't do that because the waste is small,
13197 and will be cleaned up when we're done with this
13198 compilation unit. This way, we're also more robust
13199 against other users of the comp_unit_obstack. */
13200 return first_die;
13201 }
13202 info_ptr += bytes_read;
13203 last_die = parent_die;
13204 parent_die = parent_die->die_parent;
13205 continue;
13206 }
13207
98bfdba5
PA
13208 /* Check for template arguments. We never save these; if
13209 they're seen, we just mark the parent, and go on our way. */
13210 if (parent_die != NULL
13211 && cu->language == language_cplus
13212 && (abbrev->tag == DW_TAG_template_type_param
13213 || abbrev->tag == DW_TAG_template_value_param))
13214 {
13215 parent_die->has_template_arguments = 1;
13216
13217 if (!load_all)
13218 {
13219 /* We don't need a partial DIE for the template argument. */
dee91e82 13220 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13221 continue;
13222 }
13223 }
13224
0d99eb77 13225 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13226 Skip their other children. */
13227 if (!load_all
13228 && cu->language == language_cplus
13229 && parent_die != NULL
13230 && parent_die->tag == DW_TAG_subprogram)
13231 {
dee91e82 13232 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13233 continue;
13234 }
13235
5afb4e99
DJ
13236 /* Check whether this DIE is interesting enough to save. Normally
13237 we would not be interested in members here, but there may be
13238 later variables referencing them via DW_AT_specification (for
13239 static members). */
13240 if (!load_all
13241 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13242 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13243 && abbrev->tag != DW_TAG_enumerator
13244 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13245 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13246 && abbrev->tag != DW_TAG_variable
5afb4e99 13247 && abbrev->tag != DW_TAG_namespace
f55ee35c 13248 && abbrev->tag != DW_TAG_module
95554aad
TT
13249 && abbrev->tag != DW_TAG_member
13250 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13251 {
13252 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13253 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13254 continue;
13255 }
13256
dee91e82
DE
13257 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13258 info_ptr);
72bf9492
DJ
13259
13260 /* This two-pass algorithm for processing partial symbols has a
13261 high cost in cache pressure. Thus, handle some simple cases
13262 here which cover the majority of C partial symbols. DIEs
13263 which neither have specification tags in them, nor could have
13264 specification tags elsewhere pointing at them, can simply be
13265 processed and discarded.
13266
13267 This segment is also optional; scan_partial_symbols and
13268 add_partial_symbol will handle these DIEs if we chain
13269 them in normally. When compilers which do not emit large
13270 quantities of duplicate debug information are more common,
13271 this code can probably be removed. */
13272
13273 /* Any complete simple types at the top level (pretty much all
13274 of them, for a language without namespaces), can be processed
13275 directly. */
13276 if (parent_die == NULL
13277 && part_die->has_specification == 0
13278 && part_die->is_declaration == 0
d8228535 13279 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13280 || part_die->tag == DW_TAG_base_type
13281 || part_die->tag == DW_TAG_subrange_type))
13282 {
13283 if (building_psymtab && part_die->name != NULL)
04a679b8 13284 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13285 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13286 &objfile->static_psymbols,
13287 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13288 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13289 continue;
13290 }
13291
d8228535
JK
13292 /* The exception for DW_TAG_typedef with has_children above is
13293 a workaround of GCC PR debug/47510. In the case of this complaint
13294 type_name_no_tag_or_error will error on such types later.
13295
13296 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13297 it could not find the child DIEs referenced later, this is checked
13298 above. In correct DWARF DW_TAG_typedef should have no children. */
13299
13300 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13301 complaint (&symfile_complaints,
13302 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13303 "- DIE at 0x%x [in module %s]"),
b64f50a1 13304 part_die->offset.sect_off, objfile->name);
d8228535 13305
72bf9492
DJ
13306 /* If we're at the second level, and we're an enumerator, and
13307 our parent has no specification (meaning possibly lives in a
13308 namespace elsewhere), then we can add the partial symbol now
13309 instead of queueing it. */
13310 if (part_die->tag == DW_TAG_enumerator
13311 && parent_die != NULL
13312 && parent_die->die_parent == NULL
13313 && parent_die->tag == DW_TAG_enumeration_type
13314 && parent_die->has_specification == 0)
13315 {
13316 if (part_die->name == NULL)
3e43a32a
MS
13317 complaint (&symfile_complaints,
13318 _("malformed enumerator DIE ignored"));
72bf9492 13319 else if (building_psymtab)
04a679b8 13320 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13321 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13322 (cu->language == language_cplus
13323 || cu->language == language_java)
bb5ed363
DE
13324 ? &objfile->global_psymbols
13325 : &objfile->static_psymbols,
13326 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13327
dee91e82 13328 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13329 continue;
13330 }
13331
13332 /* We'll save this DIE so link it in. */
13333 part_die->die_parent = parent_die;
13334 part_die->die_sibling = NULL;
13335 part_die->die_child = NULL;
13336
13337 if (last_die && last_die == parent_die)
13338 last_die->die_child = part_die;
13339 else if (last_die)
13340 last_die->die_sibling = part_die;
13341
13342 last_die = part_die;
13343
13344 if (first_die == NULL)
13345 first_die = part_die;
13346
13347 /* Maybe add the DIE to the hash table. Not all DIEs that we
13348 find interesting need to be in the hash table, because we
13349 also have the parent/sibling/child chains; only those that we
13350 might refer to by offset later during partial symbol reading.
13351
13352 For now this means things that might have be the target of a
13353 DW_AT_specification, DW_AT_abstract_origin, or
13354 DW_AT_extension. DW_AT_extension will refer only to
13355 namespaces; DW_AT_abstract_origin refers to functions (and
13356 many things under the function DIE, but we do not recurse
13357 into function DIEs during partial symbol reading) and
13358 possibly variables as well; DW_AT_specification refers to
13359 declarations. Declarations ought to have the DW_AT_declaration
13360 flag. It happens that GCC forgets to put it in sometimes, but
13361 only for functions, not for types.
13362
13363 Adding more things than necessary to the hash table is harmless
13364 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13365 wasted time in find_partial_die, when we reread the compilation
13366 unit with load_all_dies set. */
72bf9492 13367
5afb4e99 13368 if (load_all
72929c62 13369 || abbrev->tag == DW_TAG_constant
5afb4e99 13370 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13371 || abbrev->tag == DW_TAG_variable
13372 || abbrev->tag == DW_TAG_namespace
13373 || part_die->is_declaration)
13374 {
13375 void **slot;
13376
13377 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13378 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13379 *slot = part_die;
13380 }
13381
13382 part_die = obstack_alloc (&cu->comp_unit_obstack,
13383 sizeof (struct partial_die_info));
13384
13385 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13386 we have no reason to follow the children of structures; for other
98bfdba5
PA
13387 languages we have to, so that we can get at method physnames
13388 to infer fully qualified class names, for DW_AT_specification,
13389 and for C++ template arguments. For C++, we also look one level
13390 inside functions to find template arguments (if the name of the
13391 function does not already contain the template arguments).
bc30ff58
JB
13392
13393 For Ada, we need to scan the children of subprograms and lexical
13394 blocks as well because Ada allows the definition of nested
13395 entities that could be interesting for the debugger, such as
13396 nested subprograms for instance. */
72bf9492 13397 if (last_die->has_children
5afb4e99
DJ
13398 && (load_all
13399 || last_die->tag == DW_TAG_namespace
f55ee35c 13400 || last_die->tag == DW_TAG_module
72bf9492 13401 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13402 || (cu->language == language_cplus
13403 && last_die->tag == DW_TAG_subprogram
13404 && (last_die->name == NULL
13405 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13406 || (cu->language != language_c
13407 && (last_die->tag == DW_TAG_class_type
680b30c7 13408 || last_die->tag == DW_TAG_interface_type
72bf9492 13409 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13410 || last_die->tag == DW_TAG_union_type))
13411 || (cu->language == language_ada
13412 && (last_die->tag == DW_TAG_subprogram
13413 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13414 {
13415 nesting_level++;
13416 parent_die = last_die;
13417 continue;
13418 }
13419
13420 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13421 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13422
13423 /* Back to the top, do it again. */
13424 }
13425}
13426
c906108c
SS
13427/* Read a minimal amount of information into the minimal die structure. */
13428
fe1b8b76 13429static gdb_byte *
dee91e82
DE
13430read_partial_die (const struct die_reader_specs *reader,
13431 struct partial_die_info *part_die,
13432 struct abbrev_info *abbrev, unsigned int abbrev_len,
13433 gdb_byte *info_ptr)
c906108c 13434{
dee91e82 13435 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13436 struct objfile *objfile = cu->objfile;
dee91e82 13437 gdb_byte *buffer = reader->buffer;
fa238c03 13438 unsigned int i;
c906108c 13439 struct attribute attr;
c5aa993b 13440 int has_low_pc_attr = 0;
c906108c 13441 int has_high_pc_attr = 0;
91da1414 13442 int high_pc_relative = 0;
c906108c 13443
72bf9492 13444 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13445
b64f50a1 13446 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13447
13448 info_ptr += abbrev_len;
13449
13450 if (abbrev == NULL)
13451 return info_ptr;
13452
c906108c
SS
13453 part_die->tag = abbrev->tag;
13454 part_die->has_children = abbrev->has_children;
c906108c
SS
13455
13456 for (i = 0; i < abbrev->num_attrs; ++i)
13457 {
dee91e82 13458 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13459
13460 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13461 partial symbol table. */
c906108c
SS
13462 switch (attr.name)
13463 {
13464 case DW_AT_name:
71c25dea
TT
13465 switch (part_die->tag)
13466 {
13467 case DW_TAG_compile_unit:
95554aad 13468 case DW_TAG_partial_unit:
348e048f 13469 case DW_TAG_type_unit:
71c25dea
TT
13470 /* Compilation units have a DW_AT_name that is a filename, not
13471 a source language identifier. */
13472 case DW_TAG_enumeration_type:
13473 case DW_TAG_enumerator:
13474 /* These tags always have simple identifiers already; no need
13475 to canonicalize them. */
13476 part_die->name = DW_STRING (&attr);
13477 break;
13478 default:
13479 part_die->name
13480 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13481 &objfile->objfile_obstack);
71c25dea
TT
13482 break;
13483 }
c906108c 13484 break;
31ef98ae 13485 case DW_AT_linkage_name:
c906108c 13486 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13487 /* Note that both forms of linkage name might appear. We
13488 assume they will be the same, and we only store the last
13489 one we see. */
94af9270
KS
13490 if (cu->language == language_ada)
13491 part_die->name = DW_STRING (&attr);
abc72ce4 13492 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13493 break;
13494 case DW_AT_low_pc:
13495 has_low_pc_attr = 1;
13496 part_die->lowpc = DW_ADDR (&attr);
13497 break;
13498 case DW_AT_high_pc:
13499 has_high_pc_attr = 1;
3019eac3
DE
13500 if (attr.form == DW_FORM_addr
13501 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13502 part_die->highpc = DW_ADDR (&attr);
13503 else
13504 {
13505 high_pc_relative = 1;
13506 part_die->highpc = DW_UNSND (&attr);
13507 }
c906108c
SS
13508 break;
13509 case DW_AT_location:
0963b4bd 13510 /* Support the .debug_loc offsets. */
8e19ed76
PS
13511 if (attr_form_is_block (&attr))
13512 {
95554aad 13513 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13514 }
3690dd37 13515 else if (attr_form_is_section_offset (&attr))
8e19ed76 13516 {
4d3c2250 13517 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13518 }
13519 else
13520 {
4d3c2250
KB
13521 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13522 "partial symbol information");
8e19ed76 13523 }
c906108c 13524 break;
c906108c
SS
13525 case DW_AT_external:
13526 part_die->is_external = DW_UNSND (&attr);
13527 break;
13528 case DW_AT_declaration:
13529 part_die->is_declaration = DW_UNSND (&attr);
13530 break;
13531 case DW_AT_type:
13532 part_die->has_type = 1;
13533 break;
13534 case DW_AT_abstract_origin:
13535 case DW_AT_specification:
72bf9492
DJ
13536 case DW_AT_extension:
13537 part_die->has_specification = 1;
c764a876 13538 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13539 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13540 || cu->per_cu->is_dwz);
c906108c
SS
13541 break;
13542 case DW_AT_sibling:
13543 /* Ignore absolute siblings, they might point outside of
13544 the current compile unit. */
13545 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13546 complaint (&symfile_complaints,
13547 _("ignoring absolute DW_AT_sibling"));
c906108c 13548 else
b64f50a1 13549 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13550 break;
fa4028e9
JB
13551 case DW_AT_byte_size:
13552 part_die->has_byte_size = 1;
13553 break;
68511cec
CES
13554 case DW_AT_calling_convention:
13555 /* DWARF doesn't provide a way to identify a program's source-level
13556 entry point. DW_AT_calling_convention attributes are only meant
13557 to describe functions' calling conventions.
13558
13559 However, because it's a necessary piece of information in
13560 Fortran, and because DW_CC_program is the only piece of debugging
13561 information whose definition refers to a 'main program' at all,
13562 several compilers have begun marking Fortran main programs with
13563 DW_CC_program --- even when those functions use the standard
13564 calling conventions.
13565
13566 So until DWARF specifies a way to provide this information and
13567 compilers pick up the new representation, we'll support this
13568 practice. */
13569 if (DW_UNSND (&attr) == DW_CC_program
13570 && cu->language == language_fortran)
01f8c46d
JK
13571 {
13572 set_main_name (part_die->name);
13573
13574 /* As this DIE has a static linkage the name would be difficult
13575 to look up later. */
13576 language_of_main = language_fortran;
13577 }
68511cec 13578 break;
481860b3
GB
13579 case DW_AT_inline:
13580 if (DW_UNSND (&attr) == DW_INL_inlined
13581 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13582 part_die->may_be_inlined = 1;
13583 break;
95554aad
TT
13584
13585 case DW_AT_import:
13586 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13587 {
13588 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13589 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13590 || cu->per_cu->is_dwz);
13591 }
95554aad
TT
13592 break;
13593
c906108c
SS
13594 default:
13595 break;
13596 }
13597 }
13598
91da1414
MW
13599 if (high_pc_relative)
13600 part_die->highpc += part_die->lowpc;
13601
9373cf26
JK
13602 if (has_low_pc_attr && has_high_pc_attr)
13603 {
13604 /* When using the GNU linker, .gnu.linkonce. sections are used to
13605 eliminate duplicate copies of functions and vtables and such.
13606 The linker will arbitrarily choose one and discard the others.
13607 The AT_*_pc values for such functions refer to local labels in
13608 these sections. If the section from that file was discarded, the
13609 labels are not in the output, so the relocs get a value of 0.
13610 If this is a discarded function, mark the pc bounds as invalid,
13611 so that GDB will ignore it. */
13612 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13613 {
bb5ed363 13614 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13615
13616 complaint (&symfile_complaints,
13617 _("DW_AT_low_pc %s is zero "
13618 "for DIE at 0x%x [in module %s]"),
13619 paddress (gdbarch, part_die->lowpc),
b64f50a1 13620 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13621 }
13622 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13623 else if (part_die->lowpc >= part_die->highpc)
13624 {
bb5ed363 13625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13626
13627 complaint (&symfile_complaints,
13628 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13629 "for DIE at 0x%x [in module %s]"),
13630 paddress (gdbarch, part_die->lowpc),
13631 paddress (gdbarch, part_die->highpc),
b64f50a1 13632 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13633 }
13634 else
13635 part_die->has_pc_info = 1;
13636 }
85cbf3d3 13637
c906108c
SS
13638 return info_ptr;
13639}
13640
72bf9492
DJ
13641/* Find a cached partial DIE at OFFSET in CU. */
13642
13643static struct partial_die_info *
b64f50a1 13644find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13645{
13646 struct partial_die_info *lookup_die = NULL;
13647 struct partial_die_info part_die;
13648
13649 part_die.offset = offset;
b64f50a1
JK
13650 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13651 offset.sect_off);
72bf9492 13652
72bf9492
DJ
13653 return lookup_die;
13654}
13655
348e048f
DE
13656/* Find a partial DIE at OFFSET, which may or may not be in CU,
13657 except in the case of .debug_types DIEs which do not reference
13658 outside their CU (they do however referencing other types via
55f1336d 13659 DW_FORM_ref_sig8). */
72bf9492
DJ
13660
13661static struct partial_die_info *
36586728 13662find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13663{
bb5ed363 13664 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13665 struct dwarf2_per_cu_data *per_cu = NULL;
13666 struct partial_die_info *pd = NULL;
72bf9492 13667
36586728
TT
13668 if (offset_in_dwz == cu->per_cu->is_dwz
13669 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13670 {
13671 pd = find_partial_die_in_comp_unit (offset, cu);
13672 if (pd != NULL)
13673 return pd;
0d99eb77
DE
13674 /* We missed recording what we needed.
13675 Load all dies and try again. */
13676 per_cu = cu->per_cu;
5afb4e99 13677 }
0d99eb77
DE
13678 else
13679 {
13680 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13681 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13682 {
13683 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13684 " external reference to offset 0x%lx [in module %s].\n"),
13685 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13686 bfd_get_filename (objfile->obfd));
13687 }
36586728
TT
13688 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13689 objfile);
72bf9492 13690
0d99eb77
DE
13691 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13692 load_partial_comp_unit (per_cu);
ae038cb0 13693
0d99eb77
DE
13694 per_cu->cu->last_used = 0;
13695 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13696 }
5afb4e99 13697
dee91e82
DE
13698 /* If we didn't find it, and not all dies have been loaded,
13699 load them all and try again. */
13700
5afb4e99
DJ
13701 if (pd == NULL && per_cu->load_all_dies == 0)
13702 {
5afb4e99 13703 per_cu->load_all_dies = 1;
fd820528
DE
13704
13705 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13706 THIS_CU->cu may already be in use. So we can't just free it and
13707 replace its DIEs with the ones we read in. Instead, we leave those
13708 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13709 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13710 set. */
dee91e82 13711 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13712
13713 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13714 }
13715
13716 if (pd == NULL)
13717 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13718 _("could not find partial DIE 0x%x "
13719 "in cache [from module %s]\n"),
b64f50a1 13720 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13721 return pd;
72bf9492
DJ
13722}
13723
abc72ce4
DE
13724/* See if we can figure out if the class lives in a namespace. We do
13725 this by looking for a member function; its demangled name will
13726 contain namespace info, if there is any. */
13727
13728static void
13729guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13730 struct dwarf2_cu *cu)
13731{
13732 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13733 what template types look like, because the demangler
13734 frequently doesn't give the same name as the debug info. We
13735 could fix this by only using the demangled name to get the
13736 prefix (but see comment in read_structure_type). */
13737
13738 struct partial_die_info *real_pdi;
13739 struct partial_die_info *child_pdi;
13740
13741 /* If this DIE (this DIE's specification, if any) has a parent, then
13742 we should not do this. We'll prepend the parent's fully qualified
13743 name when we create the partial symbol. */
13744
13745 real_pdi = struct_pdi;
13746 while (real_pdi->has_specification)
36586728
TT
13747 real_pdi = find_partial_die (real_pdi->spec_offset,
13748 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13749
13750 if (real_pdi->die_parent != NULL)
13751 return;
13752
13753 for (child_pdi = struct_pdi->die_child;
13754 child_pdi != NULL;
13755 child_pdi = child_pdi->die_sibling)
13756 {
13757 if (child_pdi->tag == DW_TAG_subprogram
13758 && child_pdi->linkage_name != NULL)
13759 {
13760 char *actual_class_name
13761 = language_class_name_from_physname (cu->language_defn,
13762 child_pdi->linkage_name);
13763 if (actual_class_name != NULL)
13764 {
13765 struct_pdi->name
13766 = obsavestring (actual_class_name,
13767 strlen (actual_class_name),
13768 &cu->objfile->objfile_obstack);
13769 xfree (actual_class_name);
13770 }
13771 break;
13772 }
13773 }
13774}
13775
72bf9492
DJ
13776/* Adjust PART_DIE before generating a symbol for it. This function
13777 may set the is_external flag or change the DIE's name. */
13778
13779static void
13780fixup_partial_die (struct partial_die_info *part_die,
13781 struct dwarf2_cu *cu)
13782{
abc72ce4
DE
13783 /* Once we've fixed up a die, there's no point in doing so again.
13784 This also avoids a memory leak if we were to call
13785 guess_partial_die_structure_name multiple times. */
13786 if (part_die->fixup_called)
13787 return;
13788
72bf9492
DJ
13789 /* If we found a reference attribute and the DIE has no name, try
13790 to find a name in the referred to DIE. */
13791
13792 if (part_die->name == NULL && part_die->has_specification)
13793 {
13794 struct partial_die_info *spec_die;
72bf9492 13795
36586728
TT
13796 spec_die = find_partial_die (part_die->spec_offset,
13797 part_die->spec_is_dwz, cu);
72bf9492 13798
10b3939b 13799 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13800
13801 if (spec_die->name)
13802 {
13803 part_die->name = spec_die->name;
13804
13805 /* Copy DW_AT_external attribute if it is set. */
13806 if (spec_die->is_external)
13807 part_die->is_external = spec_die->is_external;
13808 }
13809 }
13810
13811 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13812
13813 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13814 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13815
abc72ce4
DE
13816 /* If there is no parent die to provide a namespace, and there are
13817 children, see if we can determine the namespace from their linkage
122d1940 13818 name. */
abc72ce4 13819 if (cu->language == language_cplus
8b70b953 13820 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13821 && part_die->die_parent == NULL
13822 && part_die->has_children
13823 && (part_die->tag == DW_TAG_class_type
13824 || part_die->tag == DW_TAG_structure_type
13825 || part_die->tag == DW_TAG_union_type))
13826 guess_partial_die_structure_name (part_die, cu);
13827
53832f31
TT
13828 /* GCC might emit a nameless struct or union that has a linkage
13829 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13830 if (part_die->name == NULL
96408a79
SA
13831 && (part_die->tag == DW_TAG_class_type
13832 || part_die->tag == DW_TAG_interface_type
13833 || part_die->tag == DW_TAG_structure_type
13834 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13835 && part_die->linkage_name != NULL)
13836 {
13837 char *demangled;
13838
13839 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13840 if (demangled)
13841 {
96408a79
SA
13842 const char *base;
13843
13844 /* Strip any leading namespaces/classes, keep only the base name.
13845 DW_AT_name for named DIEs does not contain the prefixes. */
13846 base = strrchr (demangled, ':');
13847 if (base && base > demangled && base[-1] == ':')
13848 base++;
13849 else
13850 base = demangled;
13851
13852 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13853 &cu->objfile->objfile_obstack);
13854 xfree (demangled);
13855 }
13856 }
13857
abc72ce4 13858 part_die->fixup_called = 1;
72bf9492
DJ
13859}
13860
a8329558 13861/* Read an attribute value described by an attribute form. */
c906108c 13862
fe1b8b76 13863static gdb_byte *
dee91e82
DE
13864read_attribute_value (const struct die_reader_specs *reader,
13865 struct attribute *attr, unsigned form,
13866 gdb_byte *info_ptr)
c906108c 13867{
dee91e82
DE
13868 struct dwarf2_cu *cu = reader->cu;
13869 bfd *abfd = reader->abfd;
e7c27a73 13870 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13871 unsigned int bytes_read;
13872 struct dwarf_block *blk;
13873
a8329558
KW
13874 attr->form = form;
13875 switch (form)
c906108c 13876 {
c906108c 13877 case DW_FORM_ref_addr:
ae411497 13878 if (cu->header.version == 2)
4568ecf9 13879 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13880 else
4568ecf9
DE
13881 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13882 &cu->header, &bytes_read);
ae411497
TT
13883 info_ptr += bytes_read;
13884 break;
36586728
TT
13885 case DW_FORM_GNU_ref_alt:
13886 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13887 info_ptr += bytes_read;
13888 break;
ae411497 13889 case DW_FORM_addr:
e7c27a73 13890 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13891 info_ptr += bytes_read;
c906108c
SS
13892 break;
13893 case DW_FORM_block2:
7b5a2f43 13894 blk = dwarf_alloc_block (cu);
c906108c
SS
13895 blk->size = read_2_bytes (abfd, info_ptr);
13896 info_ptr += 2;
13897 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13898 info_ptr += blk->size;
13899 DW_BLOCK (attr) = blk;
13900 break;
13901 case DW_FORM_block4:
7b5a2f43 13902 blk = dwarf_alloc_block (cu);
c906108c
SS
13903 blk->size = read_4_bytes (abfd, info_ptr);
13904 info_ptr += 4;
13905 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13906 info_ptr += blk->size;
13907 DW_BLOCK (attr) = blk;
13908 break;
13909 case DW_FORM_data2:
13910 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13911 info_ptr += 2;
13912 break;
13913 case DW_FORM_data4:
13914 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13915 info_ptr += 4;
13916 break;
13917 case DW_FORM_data8:
13918 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13919 info_ptr += 8;
13920 break;
2dc7f7b3
TT
13921 case DW_FORM_sec_offset:
13922 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13923 info_ptr += bytes_read;
13924 break;
c906108c 13925 case DW_FORM_string:
9b1c24c8 13926 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13927 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13928 info_ptr += bytes_read;
13929 break;
4bdf3d34 13930 case DW_FORM_strp:
36586728
TT
13931 if (!cu->per_cu->is_dwz)
13932 {
13933 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13934 &bytes_read);
13935 DW_STRING_IS_CANONICAL (attr) = 0;
13936 info_ptr += bytes_read;
13937 break;
13938 }
13939 /* FALLTHROUGH */
13940 case DW_FORM_GNU_strp_alt:
13941 {
13942 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13943 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13944 &bytes_read);
13945
13946 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13947 DW_STRING_IS_CANONICAL (attr) = 0;
13948 info_ptr += bytes_read;
13949 }
4bdf3d34 13950 break;
2dc7f7b3 13951 case DW_FORM_exprloc:
c906108c 13952 case DW_FORM_block:
7b5a2f43 13953 blk = dwarf_alloc_block (cu);
c906108c
SS
13954 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13955 info_ptr += bytes_read;
13956 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13957 info_ptr += blk->size;
13958 DW_BLOCK (attr) = blk;
13959 break;
13960 case DW_FORM_block1:
7b5a2f43 13961 blk = dwarf_alloc_block (cu);
c906108c
SS
13962 blk->size = read_1_byte (abfd, info_ptr);
13963 info_ptr += 1;
13964 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13965 info_ptr += blk->size;
13966 DW_BLOCK (attr) = blk;
13967 break;
13968 case DW_FORM_data1:
13969 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13970 info_ptr += 1;
13971 break;
13972 case DW_FORM_flag:
13973 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13974 info_ptr += 1;
13975 break;
2dc7f7b3
TT
13976 case DW_FORM_flag_present:
13977 DW_UNSND (attr) = 1;
13978 break;
c906108c
SS
13979 case DW_FORM_sdata:
13980 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13981 info_ptr += bytes_read;
13982 break;
13983 case DW_FORM_udata:
13984 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13985 info_ptr += bytes_read;
13986 break;
13987 case DW_FORM_ref1:
4568ecf9
DE
13988 DW_UNSND (attr) = (cu->header.offset.sect_off
13989 + read_1_byte (abfd, info_ptr));
c906108c
SS
13990 info_ptr += 1;
13991 break;
13992 case DW_FORM_ref2:
4568ecf9
DE
13993 DW_UNSND (attr) = (cu->header.offset.sect_off
13994 + read_2_bytes (abfd, info_ptr));
c906108c
SS
13995 info_ptr += 2;
13996 break;
13997 case DW_FORM_ref4:
4568ecf9
DE
13998 DW_UNSND (attr) = (cu->header.offset.sect_off
13999 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14000 info_ptr += 4;
14001 break;
613e1657 14002 case DW_FORM_ref8:
4568ecf9
DE
14003 DW_UNSND (attr) = (cu->header.offset.sect_off
14004 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14005 info_ptr += 8;
14006 break;
55f1336d 14007 case DW_FORM_ref_sig8:
348e048f
DE
14008 /* Convert the signature to something we can record in DW_UNSND
14009 for later lookup.
14010 NOTE: This is NULL if the type wasn't found. */
14011 DW_SIGNATURED_TYPE (attr) =
e319fa28 14012 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14013 info_ptr += 8;
14014 break;
c906108c 14015 case DW_FORM_ref_udata:
4568ecf9
DE
14016 DW_UNSND (attr) = (cu->header.offset.sect_off
14017 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14018 info_ptr += bytes_read;
14019 break;
c906108c 14020 case DW_FORM_indirect:
a8329558
KW
14021 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14022 info_ptr += bytes_read;
dee91e82 14023 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14024 break;
3019eac3
DE
14025 case DW_FORM_GNU_addr_index:
14026 if (reader->dwo_file == NULL)
14027 {
14028 /* For now flag a hard error.
14029 Later we can turn this into a complaint. */
14030 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14031 dwarf_form_name (form),
14032 bfd_get_filename (abfd));
14033 }
14034 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14035 info_ptr += bytes_read;
14036 break;
14037 case DW_FORM_GNU_str_index:
14038 if (reader->dwo_file == NULL)
14039 {
14040 /* For now flag a hard error.
14041 Later we can turn this into a complaint if warranted. */
14042 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14043 dwarf_form_name (form),
14044 bfd_get_filename (abfd));
14045 }
14046 {
14047 ULONGEST str_index =
14048 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14049
14050 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14051 DW_STRING_IS_CANONICAL (attr) = 0;
14052 info_ptr += bytes_read;
14053 }
14054 break;
c906108c 14055 default:
8a3fe4f8 14056 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14057 dwarf_form_name (form),
14058 bfd_get_filename (abfd));
c906108c 14059 }
28e94949 14060
36586728
TT
14061 /* Super hack. */
14062 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14063 attr->form = DW_FORM_GNU_ref_alt;
14064
28e94949
JB
14065 /* We have seen instances where the compiler tried to emit a byte
14066 size attribute of -1 which ended up being encoded as an unsigned
14067 0xffffffff. Although 0xffffffff is technically a valid size value,
14068 an object of this size seems pretty unlikely so we can relatively
14069 safely treat these cases as if the size attribute was invalid and
14070 treat them as zero by default. */
14071 if (attr->name == DW_AT_byte_size
14072 && form == DW_FORM_data4
14073 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14074 {
14075 complaint
14076 (&symfile_complaints,
43bbcdc2
PH
14077 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14078 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14079 DW_UNSND (attr) = 0;
14080 }
28e94949 14081
c906108c
SS
14082 return info_ptr;
14083}
14084
a8329558
KW
14085/* Read an attribute described by an abbreviated attribute. */
14086
fe1b8b76 14087static gdb_byte *
dee91e82
DE
14088read_attribute (const struct die_reader_specs *reader,
14089 struct attribute *attr, struct attr_abbrev *abbrev,
14090 gdb_byte *info_ptr)
a8329558
KW
14091{
14092 attr->name = abbrev->name;
dee91e82 14093 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14094}
14095
0963b4bd 14096/* Read dwarf information from a buffer. */
c906108c
SS
14097
14098static unsigned int
a1855c1d 14099read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14100{
fe1b8b76 14101 return bfd_get_8 (abfd, buf);
c906108c
SS
14102}
14103
14104static int
a1855c1d 14105read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14106{
fe1b8b76 14107 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14108}
14109
14110static unsigned int
a1855c1d 14111read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14112{
fe1b8b76 14113 return bfd_get_16 (abfd, buf);
c906108c
SS
14114}
14115
21ae7a4d 14116static int
a1855c1d 14117read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14118{
14119 return bfd_get_signed_16 (abfd, buf);
14120}
14121
c906108c 14122static unsigned int
a1855c1d 14123read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14124{
fe1b8b76 14125 return bfd_get_32 (abfd, buf);
c906108c
SS
14126}
14127
21ae7a4d 14128static int
a1855c1d 14129read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14130{
14131 return bfd_get_signed_32 (abfd, buf);
14132}
14133
93311388 14134static ULONGEST
a1855c1d 14135read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14136{
fe1b8b76 14137 return bfd_get_64 (abfd, buf);
c906108c
SS
14138}
14139
14140static CORE_ADDR
fe1b8b76 14141read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14142 unsigned int *bytes_read)
c906108c 14143{
e7c27a73 14144 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14145 CORE_ADDR retval = 0;
14146
107d2387 14147 if (cu_header->signed_addr_p)
c906108c 14148 {
107d2387
AC
14149 switch (cu_header->addr_size)
14150 {
14151 case 2:
fe1b8b76 14152 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14153 break;
14154 case 4:
fe1b8b76 14155 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14156 break;
14157 case 8:
fe1b8b76 14158 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14159 break;
14160 default:
8e65ff28 14161 internal_error (__FILE__, __LINE__,
e2e0b3e5 14162 _("read_address: bad switch, signed [in module %s]"),
659b0389 14163 bfd_get_filename (abfd));
107d2387
AC
14164 }
14165 }
14166 else
14167 {
14168 switch (cu_header->addr_size)
14169 {
14170 case 2:
fe1b8b76 14171 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14172 break;
14173 case 4:
fe1b8b76 14174 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14175 break;
14176 case 8:
fe1b8b76 14177 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14178 break;
14179 default:
8e65ff28 14180 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14181 _("read_address: bad switch, "
14182 "unsigned [in module %s]"),
659b0389 14183 bfd_get_filename (abfd));
107d2387 14184 }
c906108c 14185 }
64367e0a 14186
107d2387
AC
14187 *bytes_read = cu_header->addr_size;
14188 return retval;
c906108c
SS
14189}
14190
f7ef9339 14191/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14192 specification allows the initial length to take up either 4 bytes
14193 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14194 bytes describe the length and all offsets will be 8 bytes in length
14195 instead of 4.
14196
f7ef9339
KB
14197 An older, non-standard 64-bit format is also handled by this
14198 function. The older format in question stores the initial length
14199 as an 8-byte quantity without an escape value. Lengths greater
14200 than 2^32 aren't very common which means that the initial 4 bytes
14201 is almost always zero. Since a length value of zero doesn't make
14202 sense for the 32-bit format, this initial zero can be considered to
14203 be an escape value which indicates the presence of the older 64-bit
14204 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14205 greater than 4GB. If it becomes necessary to handle lengths
14206 somewhat larger than 4GB, we could allow other small values (such
14207 as the non-sensical values of 1, 2, and 3) to also be used as
14208 escape values indicating the presence of the old format.
f7ef9339 14209
917c78fc
MK
14210 The value returned via bytes_read should be used to increment the
14211 relevant pointer after calling read_initial_length().
c764a876 14212
613e1657
KB
14213 [ Note: read_initial_length() and read_offset() are based on the
14214 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14215 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14216 from:
14217
f7ef9339 14218 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14219
613e1657
KB
14220 This document is only a draft and is subject to change. (So beware.)
14221
f7ef9339 14222 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14223 determined empirically by examining 64-bit ELF files produced by
14224 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14225
14226 - Kevin, July 16, 2002
613e1657
KB
14227 ] */
14228
14229static LONGEST
c764a876 14230read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14231{
fe1b8b76 14232 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14233
dd373385 14234 if (length == 0xffffffff)
613e1657 14235 {
fe1b8b76 14236 length = bfd_get_64 (abfd, buf + 4);
613e1657 14237 *bytes_read = 12;
613e1657 14238 }
dd373385 14239 else if (length == 0)
f7ef9339 14240 {
dd373385 14241 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14242 length = bfd_get_64 (abfd, buf);
f7ef9339 14243 *bytes_read = 8;
f7ef9339 14244 }
613e1657
KB
14245 else
14246 {
14247 *bytes_read = 4;
613e1657
KB
14248 }
14249
c764a876
DE
14250 return length;
14251}
dd373385 14252
c764a876
DE
14253/* Cover function for read_initial_length.
14254 Returns the length of the object at BUF, and stores the size of the
14255 initial length in *BYTES_READ and stores the size that offsets will be in
14256 *OFFSET_SIZE.
14257 If the initial length size is not equivalent to that specified in
14258 CU_HEADER then issue a complaint.
14259 This is useful when reading non-comp-unit headers. */
dd373385 14260
c764a876
DE
14261static LONGEST
14262read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14263 const struct comp_unit_head *cu_header,
14264 unsigned int *bytes_read,
14265 unsigned int *offset_size)
14266{
14267 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14268
14269 gdb_assert (cu_header->initial_length_size == 4
14270 || cu_header->initial_length_size == 8
14271 || cu_header->initial_length_size == 12);
14272
14273 if (cu_header->initial_length_size != *bytes_read)
14274 complaint (&symfile_complaints,
14275 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14276
c764a876 14277 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14278 return length;
613e1657
KB
14279}
14280
14281/* Read an offset from the data stream. The size of the offset is
917c78fc 14282 given by cu_header->offset_size. */
613e1657
KB
14283
14284static LONGEST
fe1b8b76 14285read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14286 unsigned int *bytes_read)
c764a876
DE
14287{
14288 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14289
c764a876
DE
14290 *bytes_read = cu_header->offset_size;
14291 return offset;
14292}
14293
14294/* Read an offset from the data stream. */
14295
14296static LONGEST
14297read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14298{
14299 LONGEST retval = 0;
14300
c764a876 14301 switch (offset_size)
613e1657
KB
14302 {
14303 case 4:
fe1b8b76 14304 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14305 break;
14306 case 8:
fe1b8b76 14307 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14308 break;
14309 default:
8e65ff28 14310 internal_error (__FILE__, __LINE__,
c764a876 14311 _("read_offset_1: bad switch [in module %s]"),
659b0389 14312 bfd_get_filename (abfd));
613e1657
KB
14313 }
14314
917c78fc 14315 return retval;
613e1657
KB
14316}
14317
fe1b8b76
JB
14318static gdb_byte *
14319read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14320{
14321 /* If the size of a host char is 8 bits, we can return a pointer
14322 to the buffer, otherwise we have to copy the data to a buffer
14323 allocated on the temporary obstack. */
4bdf3d34 14324 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14325 return buf;
c906108c
SS
14326}
14327
14328static char *
9b1c24c8 14329read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14330{
14331 /* If the size of a host char is 8 bits, we can return a pointer
14332 to the string, otherwise we have to copy the string to a buffer
14333 allocated on the temporary obstack. */
4bdf3d34 14334 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14335 if (*buf == '\0')
14336 {
14337 *bytes_read_ptr = 1;
14338 return NULL;
14339 }
fe1b8b76
JB
14340 *bytes_read_ptr = strlen ((char *) buf) + 1;
14341 return (char *) buf;
4bdf3d34
JJ
14342}
14343
14344static char *
cf2c3c16 14345read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14346{
be391dca 14347 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14348 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14349 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14350 bfd_get_filename (abfd));
dce234bc 14351 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14352 error (_("DW_FORM_strp pointing outside of "
14353 ".debug_str section [in module %s]"),
14354 bfd_get_filename (abfd));
4bdf3d34 14355 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14356 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14357 return NULL;
dce234bc 14358 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14359}
14360
36586728
TT
14361/* Read a string at offset STR_OFFSET in the .debug_str section from
14362 the .dwz file DWZ. Throw an error if the offset is too large. If
14363 the string consists of a single NUL byte, return NULL; otherwise
14364 return a pointer to the string. */
14365
14366static char *
14367read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14368{
14369 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14370
14371 if (dwz->str.buffer == NULL)
14372 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14373 "section [in module %s]"),
14374 bfd_get_filename (dwz->dwz_bfd));
14375 if (str_offset >= dwz->str.size)
14376 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14377 ".debug_str section [in module %s]"),
14378 bfd_get_filename (dwz->dwz_bfd));
14379 gdb_assert (HOST_CHAR_BIT == 8);
14380 if (dwz->str.buffer[str_offset] == '\0')
14381 return NULL;
14382 return (char *) (dwz->str.buffer + str_offset);
14383}
14384
cf2c3c16
TT
14385static char *
14386read_indirect_string (bfd *abfd, gdb_byte *buf,
14387 const struct comp_unit_head *cu_header,
14388 unsigned int *bytes_read_ptr)
14389{
14390 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14391
14392 return read_indirect_string_at_offset (abfd, str_offset);
14393}
14394
12df843f 14395static ULONGEST
fe1b8b76 14396read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14397{
12df843f 14398 ULONGEST result;
ce5d95e1 14399 unsigned int num_read;
c906108c
SS
14400 int i, shift;
14401 unsigned char byte;
14402
14403 result = 0;
14404 shift = 0;
14405 num_read = 0;
14406 i = 0;
14407 while (1)
14408 {
fe1b8b76 14409 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14410 buf++;
14411 num_read++;
12df843f 14412 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14413 if ((byte & 128) == 0)
14414 {
14415 break;
14416 }
14417 shift += 7;
14418 }
14419 *bytes_read_ptr = num_read;
14420 return result;
14421}
14422
12df843f 14423static LONGEST
fe1b8b76 14424read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14425{
12df843f 14426 LONGEST result;
77e0b926 14427 int i, shift, num_read;
c906108c
SS
14428 unsigned char byte;
14429
14430 result = 0;
14431 shift = 0;
c906108c
SS
14432 num_read = 0;
14433 i = 0;
14434 while (1)
14435 {
fe1b8b76 14436 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14437 buf++;
14438 num_read++;
12df843f 14439 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14440 shift += 7;
14441 if ((byte & 128) == 0)
14442 {
14443 break;
14444 }
14445 }
77e0b926 14446 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14447 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14448 *bytes_read_ptr = num_read;
14449 return result;
14450}
14451
3019eac3
DE
14452/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14453 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14454 ADDR_SIZE is the size of addresses from the CU header. */
14455
14456static CORE_ADDR
14457read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14458{
14459 struct objfile *objfile = dwarf2_per_objfile->objfile;
14460 bfd *abfd = objfile->obfd;
14461 const gdb_byte *info_ptr;
14462
14463 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14464 if (dwarf2_per_objfile->addr.buffer == NULL)
14465 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14466 objfile->name);
14467 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14468 error (_("DW_FORM_addr_index pointing outside of "
14469 ".debug_addr section [in module %s]"),
14470 objfile->name);
14471 info_ptr = (dwarf2_per_objfile->addr.buffer
14472 + addr_base + addr_index * addr_size);
14473 if (addr_size == 4)
14474 return bfd_get_32 (abfd, info_ptr);
14475 else
14476 return bfd_get_64 (abfd, info_ptr);
14477}
14478
14479/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14480
14481static CORE_ADDR
14482read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14483{
14484 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14485}
14486
14487/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14488
14489static CORE_ADDR
14490read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14491 unsigned int *bytes_read)
14492{
14493 bfd *abfd = cu->objfile->obfd;
14494 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14495
14496 return read_addr_index (cu, addr_index);
14497}
14498
14499/* Data structure to pass results from dwarf2_read_addr_index_reader
14500 back to dwarf2_read_addr_index. */
14501
14502struct dwarf2_read_addr_index_data
14503{
14504 ULONGEST addr_base;
14505 int addr_size;
14506};
14507
14508/* die_reader_func for dwarf2_read_addr_index. */
14509
14510static void
14511dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14512 gdb_byte *info_ptr,
14513 struct die_info *comp_unit_die,
14514 int has_children,
14515 void *data)
14516{
14517 struct dwarf2_cu *cu = reader->cu;
14518 struct dwarf2_read_addr_index_data *aidata =
14519 (struct dwarf2_read_addr_index_data *) data;
14520
14521 aidata->addr_base = cu->addr_base;
14522 aidata->addr_size = cu->header.addr_size;
14523}
14524
14525/* Given an index in .debug_addr, fetch the value.
14526 NOTE: This can be called during dwarf expression evaluation,
14527 long after the debug information has been read, and thus per_cu->cu
14528 may no longer exist. */
14529
14530CORE_ADDR
14531dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14532 unsigned int addr_index)
14533{
14534 struct objfile *objfile = per_cu->objfile;
14535 struct dwarf2_cu *cu = per_cu->cu;
14536 ULONGEST addr_base;
14537 int addr_size;
14538
14539 /* This is intended to be called from outside this file. */
14540 dw2_setup (objfile);
14541
14542 /* We need addr_base and addr_size.
14543 If we don't have PER_CU->cu, we have to get it.
14544 Nasty, but the alternative is storing the needed info in PER_CU,
14545 which at this point doesn't seem justified: it's not clear how frequently
14546 it would get used and it would increase the size of every PER_CU.
14547 Entry points like dwarf2_per_cu_addr_size do a similar thing
14548 so we're not in uncharted territory here.
14549 Alas we need to be a bit more complicated as addr_base is contained
14550 in the DIE.
14551
14552 We don't need to read the entire CU(/TU).
14553 We just need the header and top level die.
a1b64ce1 14554
3019eac3 14555 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14556 For now we skip this optimization. */
3019eac3
DE
14557
14558 if (cu != NULL)
14559 {
14560 addr_base = cu->addr_base;
14561 addr_size = cu->header.addr_size;
14562 }
14563 else
14564 {
14565 struct dwarf2_read_addr_index_data aidata;
14566
a1b64ce1
DE
14567 /* Note: We can't use init_cutu_and_read_dies_simple here,
14568 we need addr_base. */
14569 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14570 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14571 addr_base = aidata.addr_base;
14572 addr_size = aidata.addr_size;
14573 }
14574
14575 return read_addr_index_1 (addr_index, addr_base, addr_size);
14576}
14577
14578/* Given a DW_AT_str_index, fetch the string. */
14579
14580static char *
14581read_str_index (const struct die_reader_specs *reader,
14582 struct dwarf2_cu *cu, ULONGEST str_index)
14583{
14584 struct objfile *objfile = dwarf2_per_objfile->objfile;
14585 const char *dwo_name = objfile->name;
14586 bfd *abfd = objfile->obfd;
14587 struct dwo_sections *sections = &reader->dwo_file->sections;
14588 gdb_byte *info_ptr;
14589 ULONGEST str_offset;
14590
14591 dwarf2_read_section (objfile, &sections->str);
14592 dwarf2_read_section (objfile, &sections->str_offsets);
14593 if (sections->str.buffer == NULL)
14594 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14595 " in CU at offset 0x%lx [in module %s]"),
14596 (long) cu->header.offset.sect_off, dwo_name);
14597 if (sections->str_offsets.buffer == NULL)
14598 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14599 " in CU at offset 0x%lx [in module %s]"),
14600 (long) cu->header.offset.sect_off, dwo_name);
14601 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14602 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14603 " section in CU at offset 0x%lx [in module %s]"),
14604 (long) cu->header.offset.sect_off, dwo_name);
14605 info_ptr = (sections->str_offsets.buffer
14606 + str_index * cu->header.offset_size);
14607 if (cu->header.offset_size == 4)
14608 str_offset = bfd_get_32 (abfd, info_ptr);
14609 else
14610 str_offset = bfd_get_64 (abfd, info_ptr);
14611 if (str_offset >= sections->str.size)
14612 error (_("Offset from DW_FORM_str_index pointing outside of"
14613 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14614 (long) cu->header.offset.sect_off, dwo_name);
14615 return (char *) (sections->str.buffer + str_offset);
14616}
14617
3019eac3
DE
14618/* Return the length of an LEB128 number in BUF. */
14619
14620static int
14621leb128_size (const gdb_byte *buf)
14622{
14623 const gdb_byte *begin = buf;
14624 gdb_byte byte;
14625
14626 while (1)
14627 {
14628 byte = *buf++;
14629 if ((byte & 128) == 0)
14630 return buf - begin;
14631 }
14632}
14633
c906108c 14634static void
e142c38c 14635set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14636{
14637 switch (lang)
14638 {
14639 case DW_LANG_C89:
76bee0cc 14640 case DW_LANG_C99:
c906108c 14641 case DW_LANG_C:
e142c38c 14642 cu->language = language_c;
c906108c
SS
14643 break;
14644 case DW_LANG_C_plus_plus:
e142c38c 14645 cu->language = language_cplus;
c906108c 14646 break;
6aecb9c2
JB
14647 case DW_LANG_D:
14648 cu->language = language_d;
14649 break;
c906108c
SS
14650 case DW_LANG_Fortran77:
14651 case DW_LANG_Fortran90:
b21b22e0 14652 case DW_LANG_Fortran95:
e142c38c 14653 cu->language = language_fortran;
c906108c 14654 break;
a766d390
DE
14655 case DW_LANG_Go:
14656 cu->language = language_go;
14657 break;
c906108c 14658 case DW_LANG_Mips_Assembler:
e142c38c 14659 cu->language = language_asm;
c906108c 14660 break;
bebd888e 14661 case DW_LANG_Java:
e142c38c 14662 cu->language = language_java;
bebd888e 14663 break;
c906108c 14664 case DW_LANG_Ada83:
8aaf0b47 14665 case DW_LANG_Ada95:
bc5f45f8
JB
14666 cu->language = language_ada;
14667 break;
72019c9c
GM
14668 case DW_LANG_Modula2:
14669 cu->language = language_m2;
14670 break;
fe8e67fd
PM
14671 case DW_LANG_Pascal83:
14672 cu->language = language_pascal;
14673 break;
22566fbd
DJ
14674 case DW_LANG_ObjC:
14675 cu->language = language_objc;
14676 break;
c906108c
SS
14677 case DW_LANG_Cobol74:
14678 case DW_LANG_Cobol85:
c906108c 14679 default:
e142c38c 14680 cu->language = language_minimal;
c906108c
SS
14681 break;
14682 }
e142c38c 14683 cu->language_defn = language_def (cu->language);
c906108c
SS
14684}
14685
14686/* Return the named attribute or NULL if not there. */
14687
14688static struct attribute *
e142c38c 14689dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14690{
a48e046c 14691 for (;;)
c906108c 14692 {
a48e046c
TT
14693 unsigned int i;
14694 struct attribute *spec = NULL;
14695
14696 for (i = 0; i < die->num_attrs; ++i)
14697 {
14698 if (die->attrs[i].name == name)
14699 return &die->attrs[i];
14700 if (die->attrs[i].name == DW_AT_specification
14701 || die->attrs[i].name == DW_AT_abstract_origin)
14702 spec = &die->attrs[i];
14703 }
14704
14705 if (!spec)
14706 break;
c906108c 14707
f2f0e013 14708 die = follow_die_ref (die, spec, &cu);
f2f0e013 14709 }
c5aa993b 14710
c906108c
SS
14711 return NULL;
14712}
14713
348e048f
DE
14714/* Return the named attribute or NULL if not there,
14715 but do not follow DW_AT_specification, etc.
14716 This is for use in contexts where we're reading .debug_types dies.
14717 Following DW_AT_specification, DW_AT_abstract_origin will take us
14718 back up the chain, and we want to go down. */
14719
14720static struct attribute *
45e58e77 14721dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14722{
14723 unsigned int i;
14724
14725 for (i = 0; i < die->num_attrs; ++i)
14726 if (die->attrs[i].name == name)
14727 return &die->attrs[i];
14728
14729 return NULL;
14730}
14731
05cf31d1
JB
14732/* Return non-zero iff the attribute NAME is defined for the given DIE,
14733 and holds a non-zero value. This function should only be used for
2dc7f7b3 14734 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14735
14736static int
14737dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14738{
14739 struct attribute *attr = dwarf2_attr (die, name, cu);
14740
14741 return (attr && DW_UNSND (attr));
14742}
14743
3ca72b44 14744static int
e142c38c 14745die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14746{
05cf31d1
JB
14747 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14748 which value is non-zero. However, we have to be careful with
14749 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14750 (via dwarf2_flag_true_p) follows this attribute. So we may
14751 end up accidently finding a declaration attribute that belongs
14752 to a different DIE referenced by the specification attribute,
14753 even though the given DIE does not have a declaration attribute. */
14754 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14755 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14756}
14757
63d06c5c 14758/* Return the die giving the specification for DIE, if there is
f2f0e013 14759 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14760 containing the return value on output. If there is no
14761 specification, but there is an abstract origin, that is
14762 returned. */
63d06c5c
DC
14763
14764static struct die_info *
f2f0e013 14765die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14766{
f2f0e013
DJ
14767 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14768 *spec_cu);
63d06c5c 14769
edb3359d
DJ
14770 if (spec_attr == NULL)
14771 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14772
63d06c5c
DC
14773 if (spec_attr == NULL)
14774 return NULL;
14775 else
f2f0e013 14776 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14777}
c906108c 14778
debd256d 14779/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14780 refers to.
14781 NOTE: This is also used as a "cleanup" function. */
14782
debd256d
JB
14783static void
14784free_line_header (struct line_header *lh)
14785{
14786 if (lh->standard_opcode_lengths)
a8bc7b56 14787 xfree (lh->standard_opcode_lengths);
debd256d
JB
14788
14789 /* Remember that all the lh->file_names[i].name pointers are
14790 pointers into debug_line_buffer, and don't need to be freed. */
14791 if (lh->file_names)
a8bc7b56 14792 xfree (lh->file_names);
debd256d
JB
14793
14794 /* Similarly for the include directory names. */
14795 if (lh->include_dirs)
a8bc7b56 14796 xfree (lh->include_dirs);
debd256d 14797
a8bc7b56 14798 xfree (lh);
debd256d
JB
14799}
14800
debd256d 14801/* Add an entry to LH's include directory table. */
ae2de4f8 14802
debd256d
JB
14803static void
14804add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14805{
debd256d
JB
14806 /* Grow the array if necessary. */
14807 if (lh->include_dirs_size == 0)
c5aa993b 14808 {
debd256d
JB
14809 lh->include_dirs_size = 1; /* for testing */
14810 lh->include_dirs = xmalloc (lh->include_dirs_size
14811 * sizeof (*lh->include_dirs));
14812 }
14813 else if (lh->num_include_dirs >= lh->include_dirs_size)
14814 {
14815 lh->include_dirs_size *= 2;
14816 lh->include_dirs = xrealloc (lh->include_dirs,
14817 (lh->include_dirs_size
14818 * sizeof (*lh->include_dirs)));
c5aa993b 14819 }
c906108c 14820
debd256d
JB
14821 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14822}
6e70227d 14823
debd256d 14824/* Add an entry to LH's file name table. */
ae2de4f8 14825
debd256d
JB
14826static void
14827add_file_name (struct line_header *lh,
14828 char *name,
14829 unsigned int dir_index,
14830 unsigned int mod_time,
14831 unsigned int length)
14832{
14833 struct file_entry *fe;
14834
14835 /* Grow the array if necessary. */
14836 if (lh->file_names_size == 0)
14837 {
14838 lh->file_names_size = 1; /* for testing */
14839 lh->file_names = xmalloc (lh->file_names_size
14840 * sizeof (*lh->file_names));
14841 }
14842 else if (lh->num_file_names >= lh->file_names_size)
14843 {
14844 lh->file_names_size *= 2;
14845 lh->file_names = xrealloc (lh->file_names,
14846 (lh->file_names_size
14847 * sizeof (*lh->file_names)));
14848 }
14849
14850 fe = &lh->file_names[lh->num_file_names++];
14851 fe->name = name;
14852 fe->dir_index = dir_index;
14853 fe->mod_time = mod_time;
14854 fe->length = length;
aaa75496 14855 fe->included_p = 0;
cb1df416 14856 fe->symtab = NULL;
debd256d 14857}
6e70227d 14858
36586728
TT
14859/* A convenience function to find the proper .debug_line section for a
14860 CU. */
14861
14862static struct dwarf2_section_info *
14863get_debug_line_section (struct dwarf2_cu *cu)
14864{
14865 struct dwarf2_section_info *section;
14866
14867 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14868 DWO file. */
14869 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14870 section = &cu->dwo_unit->dwo_file->sections.line;
14871 else if (cu->per_cu->is_dwz)
14872 {
14873 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14874
14875 section = &dwz->line;
14876 }
14877 else
14878 section = &dwarf2_per_objfile->line;
14879
14880 return section;
14881}
14882
debd256d 14883/* Read the statement program header starting at OFFSET in
3019eac3 14884 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14885 to a struct line_header, allocated using xmalloc.
debd256d
JB
14886
14887 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14888 the returned object point into the dwarf line section buffer,
14889 and must not be freed. */
ae2de4f8 14890
debd256d 14891static struct line_header *
3019eac3 14892dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14893{
14894 struct cleanup *back_to;
14895 struct line_header *lh;
fe1b8b76 14896 gdb_byte *line_ptr;
c764a876 14897 unsigned int bytes_read, offset_size;
debd256d
JB
14898 int i;
14899 char *cur_dir, *cur_file;
3019eac3
DE
14900 struct dwarf2_section_info *section;
14901 bfd *abfd;
14902
36586728 14903 section = get_debug_line_section (cu);
3019eac3
DE
14904 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14905 if (section->buffer == NULL)
debd256d 14906 {
3019eac3
DE
14907 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14908 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14909 else
14910 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14911 return 0;
14912 }
14913
fceca515
DE
14914 /* We can't do this until we know the section is non-empty.
14915 Only then do we know we have such a section. */
14916 abfd = section->asection->owner;
14917
a738430d
MK
14918 /* Make sure that at least there's room for the total_length field.
14919 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14920 if (offset + 4 >= section->size)
debd256d 14921 {
4d3c2250 14922 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14923 return 0;
14924 }
14925
14926 lh = xmalloc (sizeof (*lh));
14927 memset (lh, 0, sizeof (*lh));
14928 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14929 (void *) lh);
14930
3019eac3 14931 line_ptr = section->buffer + offset;
debd256d 14932
a738430d 14933 /* Read in the header. */
6e70227d 14934 lh->total_length =
c764a876
DE
14935 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14936 &bytes_read, &offset_size);
debd256d 14937 line_ptr += bytes_read;
3019eac3 14938 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14939 {
4d3c2250 14940 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14941 return 0;
14942 }
14943 lh->statement_program_end = line_ptr + lh->total_length;
14944 lh->version = read_2_bytes (abfd, line_ptr);
14945 line_ptr += 2;
c764a876
DE
14946 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14947 line_ptr += offset_size;
debd256d
JB
14948 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14949 line_ptr += 1;
2dc7f7b3
TT
14950 if (lh->version >= 4)
14951 {
14952 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14953 line_ptr += 1;
14954 }
14955 else
14956 lh->maximum_ops_per_instruction = 1;
14957
14958 if (lh->maximum_ops_per_instruction == 0)
14959 {
14960 lh->maximum_ops_per_instruction = 1;
14961 complaint (&symfile_complaints,
3e43a32a
MS
14962 _("invalid maximum_ops_per_instruction "
14963 "in `.debug_line' section"));
2dc7f7b3
TT
14964 }
14965
debd256d
JB
14966 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14967 line_ptr += 1;
14968 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14969 line_ptr += 1;
14970 lh->line_range = read_1_byte (abfd, line_ptr);
14971 line_ptr += 1;
14972 lh->opcode_base = read_1_byte (abfd, line_ptr);
14973 line_ptr += 1;
14974 lh->standard_opcode_lengths
fe1b8b76 14975 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
14976
14977 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14978 for (i = 1; i < lh->opcode_base; ++i)
14979 {
14980 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14981 line_ptr += 1;
14982 }
14983
a738430d 14984 /* Read directory table. */
9b1c24c8 14985 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14986 {
14987 line_ptr += bytes_read;
14988 add_include_dir (lh, cur_dir);
14989 }
14990 line_ptr += bytes_read;
14991
a738430d 14992 /* Read file name table. */
9b1c24c8 14993 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14994 {
14995 unsigned int dir_index, mod_time, length;
14996
14997 line_ptr += bytes_read;
14998 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14999 line_ptr += bytes_read;
15000 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15001 line_ptr += bytes_read;
15002 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15003 line_ptr += bytes_read;
15004
15005 add_file_name (lh, cur_file, dir_index, mod_time, length);
15006 }
15007 line_ptr += bytes_read;
6e70227d 15008 lh->statement_program_start = line_ptr;
debd256d 15009
3019eac3 15010 if (line_ptr > (section->buffer + section->size))
4d3c2250 15011 complaint (&symfile_complaints,
3e43a32a
MS
15012 _("line number info header doesn't "
15013 "fit in `.debug_line' section"));
debd256d
JB
15014
15015 discard_cleanups (back_to);
15016 return lh;
15017}
c906108c 15018
c6da4cef
DE
15019/* Subroutine of dwarf_decode_lines to simplify it.
15020 Return the file name of the psymtab for included file FILE_INDEX
15021 in line header LH of PST.
15022 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15023 If space for the result is malloc'd, it will be freed by a cleanup.
15024 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15025
15026static char *
15027psymtab_include_file_name (const struct line_header *lh, int file_index,
15028 const struct partial_symtab *pst,
15029 const char *comp_dir)
15030{
15031 const struct file_entry fe = lh->file_names [file_index];
15032 char *include_name = fe.name;
15033 char *include_name_to_compare = include_name;
15034 char *dir_name = NULL;
72b9f47f
TT
15035 const char *pst_filename;
15036 char *copied_name = NULL;
c6da4cef
DE
15037 int file_is_pst;
15038
15039 if (fe.dir_index)
15040 dir_name = lh->include_dirs[fe.dir_index - 1];
15041
15042 if (!IS_ABSOLUTE_PATH (include_name)
15043 && (dir_name != NULL || comp_dir != NULL))
15044 {
15045 /* Avoid creating a duplicate psymtab for PST.
15046 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15047 Before we do the comparison, however, we need to account
15048 for DIR_NAME and COMP_DIR.
15049 First prepend dir_name (if non-NULL). If we still don't
15050 have an absolute path prepend comp_dir (if non-NULL).
15051 However, the directory we record in the include-file's
15052 psymtab does not contain COMP_DIR (to match the
15053 corresponding symtab(s)).
15054
15055 Example:
15056
15057 bash$ cd /tmp
15058 bash$ gcc -g ./hello.c
15059 include_name = "hello.c"
15060 dir_name = "."
15061 DW_AT_comp_dir = comp_dir = "/tmp"
15062 DW_AT_name = "./hello.c" */
15063
15064 if (dir_name != NULL)
15065 {
15066 include_name = concat (dir_name, SLASH_STRING,
15067 include_name, (char *)NULL);
15068 include_name_to_compare = include_name;
15069 make_cleanup (xfree, include_name);
15070 }
15071 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15072 {
15073 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15074 include_name, (char *)NULL);
15075 }
15076 }
15077
15078 pst_filename = pst->filename;
15079 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15080 {
72b9f47f
TT
15081 copied_name = concat (pst->dirname, SLASH_STRING,
15082 pst_filename, (char *)NULL);
15083 pst_filename = copied_name;
c6da4cef
DE
15084 }
15085
1e3fad37 15086 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15087
15088 if (include_name_to_compare != include_name)
15089 xfree (include_name_to_compare);
72b9f47f
TT
15090 if (copied_name != NULL)
15091 xfree (copied_name);
c6da4cef
DE
15092
15093 if (file_is_pst)
15094 return NULL;
15095 return include_name;
15096}
15097
c91513d8
PP
15098/* Ignore this record_line request. */
15099
15100static void
15101noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15102{
15103 return;
15104}
15105
f3f5162e
DE
15106/* Subroutine of dwarf_decode_lines to simplify it.
15107 Process the line number information in LH. */
debd256d 15108
c906108c 15109static void
f3f5162e
DE
15110dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15111 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15112{
a8c50c1f 15113 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15114 gdb_byte *line_end;
a8c50c1f 15115 unsigned int bytes_read, extended_len;
c906108c 15116 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15117 CORE_ADDR baseaddr;
15118 struct objfile *objfile = cu->objfile;
f3f5162e 15119 bfd *abfd = objfile->obfd;
fbf65064 15120 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15121 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15122 struct subfile *last_subfile = NULL;
c91513d8
PP
15123 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15124 = record_line;
e142c38c
DJ
15125
15126 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15127
debd256d
JB
15128 line_ptr = lh->statement_program_start;
15129 line_end = lh->statement_program_end;
c906108c
SS
15130
15131 /* Read the statement sequences until there's nothing left. */
15132 while (line_ptr < line_end)
15133 {
15134 /* state machine registers */
15135 CORE_ADDR address = 0;
15136 unsigned int file = 1;
15137 unsigned int line = 1;
15138 unsigned int column = 0;
debd256d 15139 int is_stmt = lh->default_is_stmt;
c906108c
SS
15140 int basic_block = 0;
15141 int end_sequence = 0;
fbf65064 15142 CORE_ADDR addr;
2dc7f7b3 15143 unsigned char op_index = 0;
c906108c 15144
aaa75496 15145 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15146 {
aaa75496 15147 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15148 /* lh->include_dirs and lh->file_names are 0-based, but the
15149 directory and file name numbers in the statement program
15150 are 1-based. */
15151 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15152 char *dir = NULL;
a738430d 15153
debd256d
JB
15154 if (fe->dir_index)
15155 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15156
15157 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15158 }
15159
a738430d 15160 /* Decode the table. */
c5aa993b 15161 while (!end_sequence)
c906108c
SS
15162 {
15163 op_code = read_1_byte (abfd, line_ptr);
15164 line_ptr += 1;
59205f5a
JB
15165 if (line_ptr > line_end)
15166 {
15167 dwarf2_debug_line_missing_end_sequence_complaint ();
15168 break;
15169 }
9aa1fe7e 15170
debd256d 15171 if (op_code >= lh->opcode_base)
6e70227d 15172 {
a738430d 15173 /* Special operand. */
debd256d 15174 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15175 address += (((op_index + (adj_opcode / lh->line_range))
15176 / lh->maximum_ops_per_instruction)
15177 * lh->minimum_instruction_length);
15178 op_index = ((op_index + (adj_opcode / lh->line_range))
15179 % lh->maximum_ops_per_instruction);
debd256d 15180 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15181 if (lh->num_file_names < file || file == 0)
25e43795 15182 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15183 /* For now we ignore lines not starting on an
15184 instruction boundary. */
15185 else if (op_index == 0)
25e43795
DJ
15186 {
15187 lh->file_names[file - 1].included_p = 1;
ca5f395d 15188 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15189 {
15190 if (last_subfile != current_subfile)
15191 {
15192 addr = gdbarch_addr_bits_remove (gdbarch, address);
15193 if (last_subfile)
c91513d8 15194 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15195 last_subfile = current_subfile;
15196 }
25e43795 15197 /* Append row to matrix using current values. */
7019d805 15198 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15199 (*p_record_line) (current_subfile, line, addr);
366da635 15200 }
25e43795 15201 }
ca5f395d 15202 basic_block = 0;
9aa1fe7e
GK
15203 }
15204 else switch (op_code)
c906108c
SS
15205 {
15206 case DW_LNS_extended_op:
3e43a32a
MS
15207 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15208 &bytes_read);
473b7be6 15209 line_ptr += bytes_read;
a8c50c1f 15210 extended_end = line_ptr + extended_len;
c906108c
SS
15211 extended_op = read_1_byte (abfd, line_ptr);
15212 line_ptr += 1;
15213 switch (extended_op)
15214 {
15215 case DW_LNE_end_sequence:
c91513d8 15216 p_record_line = record_line;
c906108c 15217 end_sequence = 1;
c906108c
SS
15218 break;
15219 case DW_LNE_set_address:
e7c27a73 15220 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15221
15222 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15223 {
15224 /* This line table is for a function which has been
15225 GCd by the linker. Ignore it. PR gdb/12528 */
15226
15227 long line_offset
36586728 15228 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15229
15230 complaint (&symfile_complaints,
15231 _(".debug_line address at offset 0x%lx is 0 "
15232 "[in module %s]"),
bb5ed363 15233 line_offset, objfile->name);
c91513d8
PP
15234 p_record_line = noop_record_line;
15235 }
15236
2dc7f7b3 15237 op_index = 0;
107d2387
AC
15238 line_ptr += bytes_read;
15239 address += baseaddr;
c906108c
SS
15240 break;
15241 case DW_LNE_define_file:
debd256d
JB
15242 {
15243 char *cur_file;
15244 unsigned int dir_index, mod_time, length;
6e70227d 15245
3e43a32a
MS
15246 cur_file = read_direct_string (abfd, line_ptr,
15247 &bytes_read);
debd256d
JB
15248 line_ptr += bytes_read;
15249 dir_index =
15250 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15251 line_ptr += bytes_read;
15252 mod_time =
15253 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15254 line_ptr += bytes_read;
15255 length =
15256 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15257 line_ptr += bytes_read;
15258 add_file_name (lh, cur_file, dir_index, mod_time, length);
15259 }
c906108c 15260 break;
d0c6ba3d
CC
15261 case DW_LNE_set_discriminator:
15262 /* The discriminator is not interesting to the debugger;
15263 just ignore it. */
15264 line_ptr = extended_end;
15265 break;
c906108c 15266 default:
4d3c2250 15267 complaint (&symfile_complaints,
e2e0b3e5 15268 _("mangled .debug_line section"));
debd256d 15269 return;
c906108c 15270 }
a8c50c1f
DJ
15271 /* Make sure that we parsed the extended op correctly. If e.g.
15272 we expected a different address size than the producer used,
15273 we may have read the wrong number of bytes. */
15274 if (line_ptr != extended_end)
15275 {
15276 complaint (&symfile_complaints,
15277 _("mangled .debug_line section"));
15278 return;
15279 }
c906108c
SS
15280 break;
15281 case DW_LNS_copy:
59205f5a 15282 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15283 dwarf2_debug_line_missing_file_complaint ();
15284 else
366da635 15285 {
25e43795 15286 lh->file_names[file - 1].included_p = 1;
ca5f395d 15287 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15288 {
15289 if (last_subfile != current_subfile)
15290 {
15291 addr = gdbarch_addr_bits_remove (gdbarch, address);
15292 if (last_subfile)
c91513d8 15293 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15294 last_subfile = current_subfile;
15295 }
7019d805 15296 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15297 (*p_record_line) (current_subfile, line, addr);
fbf65064 15298 }
366da635 15299 }
c906108c
SS
15300 basic_block = 0;
15301 break;
15302 case DW_LNS_advance_pc:
2dc7f7b3
TT
15303 {
15304 CORE_ADDR adjust
15305 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15306
15307 address += (((op_index + adjust)
15308 / lh->maximum_ops_per_instruction)
15309 * lh->minimum_instruction_length);
15310 op_index = ((op_index + adjust)
15311 % lh->maximum_ops_per_instruction);
15312 line_ptr += bytes_read;
15313 }
c906108c
SS
15314 break;
15315 case DW_LNS_advance_line:
15316 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15317 line_ptr += bytes_read;
15318 break;
15319 case DW_LNS_set_file:
debd256d 15320 {
a738430d
MK
15321 /* The arrays lh->include_dirs and lh->file_names are
15322 0-based, but the directory and file name numbers in
15323 the statement program are 1-based. */
debd256d 15324 struct file_entry *fe;
4f1520fb 15325 char *dir = NULL;
a738430d 15326
debd256d
JB
15327 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15328 line_ptr += bytes_read;
59205f5a 15329 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15330 dwarf2_debug_line_missing_file_complaint ();
15331 else
15332 {
15333 fe = &lh->file_names[file - 1];
15334 if (fe->dir_index)
15335 dir = lh->include_dirs[fe->dir_index - 1];
15336 if (!decode_for_pst_p)
15337 {
15338 last_subfile = current_subfile;
15339 dwarf2_start_subfile (fe->name, dir, comp_dir);
15340 }
15341 }
debd256d 15342 }
c906108c
SS
15343 break;
15344 case DW_LNS_set_column:
15345 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15346 line_ptr += bytes_read;
15347 break;
15348 case DW_LNS_negate_stmt:
15349 is_stmt = (!is_stmt);
15350 break;
15351 case DW_LNS_set_basic_block:
15352 basic_block = 1;
15353 break;
c2c6d25f
JM
15354 /* Add to the address register of the state machine the
15355 address increment value corresponding to special opcode
a738430d
MK
15356 255. I.e., this value is scaled by the minimum
15357 instruction length since special opcode 255 would have
b021a221 15358 scaled the increment. */
c906108c 15359 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15360 {
15361 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15362
15363 address += (((op_index + adjust)
15364 / lh->maximum_ops_per_instruction)
15365 * lh->minimum_instruction_length);
15366 op_index = ((op_index + adjust)
15367 % lh->maximum_ops_per_instruction);
15368 }
c906108c
SS
15369 break;
15370 case DW_LNS_fixed_advance_pc:
15371 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15372 op_index = 0;
c906108c
SS
15373 line_ptr += 2;
15374 break;
9aa1fe7e 15375 default:
a738430d
MK
15376 {
15377 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15378 int i;
a738430d 15379
debd256d 15380 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15381 {
15382 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15383 line_ptr += bytes_read;
15384 }
15385 }
c906108c
SS
15386 }
15387 }
59205f5a
JB
15388 if (lh->num_file_names < file || file == 0)
15389 dwarf2_debug_line_missing_file_complaint ();
15390 else
15391 {
15392 lh->file_names[file - 1].included_p = 1;
15393 if (!decode_for_pst_p)
fbf65064
UW
15394 {
15395 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15396 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15397 }
59205f5a 15398 }
c906108c 15399 }
f3f5162e
DE
15400}
15401
15402/* Decode the Line Number Program (LNP) for the given line_header
15403 structure and CU. The actual information extracted and the type
15404 of structures created from the LNP depends on the value of PST.
15405
15406 1. If PST is NULL, then this procedure uses the data from the program
15407 to create all necessary symbol tables, and their linetables.
15408
15409 2. If PST is not NULL, this procedure reads the program to determine
15410 the list of files included by the unit represented by PST, and
15411 builds all the associated partial symbol tables.
15412
15413 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15414 It is used for relative paths in the line table.
15415 NOTE: When processing partial symtabs (pst != NULL),
15416 comp_dir == pst->dirname.
15417
15418 NOTE: It is important that psymtabs have the same file name (via strcmp)
15419 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15420 symtab we don't use it in the name of the psymtabs we create.
15421 E.g. expand_line_sal requires this when finding psymtabs to expand.
15422 A good testcase for this is mb-inline.exp. */
15423
15424static void
15425dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15426 struct dwarf2_cu *cu, struct partial_symtab *pst,
15427 int want_line_info)
15428{
15429 struct objfile *objfile = cu->objfile;
15430 const int decode_for_pst_p = (pst != NULL);
15431 struct subfile *first_subfile = current_subfile;
15432
15433 if (want_line_info)
15434 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15435
15436 if (decode_for_pst_p)
15437 {
15438 int file_index;
15439
15440 /* Now that we're done scanning the Line Header Program, we can
15441 create the psymtab of each included file. */
15442 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15443 if (lh->file_names[file_index].included_p == 1)
15444 {
c6da4cef
DE
15445 char *include_name =
15446 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15447 if (include_name != NULL)
aaa75496
JB
15448 dwarf2_create_include_psymtab (include_name, pst, objfile);
15449 }
15450 }
cb1df416
DJ
15451 else
15452 {
15453 /* Make sure a symtab is created for every file, even files
15454 which contain only variables (i.e. no code with associated
15455 line numbers). */
cb1df416 15456 int i;
cb1df416
DJ
15457
15458 for (i = 0; i < lh->num_file_names; i++)
15459 {
15460 char *dir = NULL;
f3f5162e 15461 struct file_entry *fe;
9a619af0 15462
cb1df416
DJ
15463 fe = &lh->file_names[i];
15464 if (fe->dir_index)
15465 dir = lh->include_dirs[fe->dir_index - 1];
15466 dwarf2_start_subfile (fe->name, dir, comp_dir);
15467
15468 /* Skip the main file; we don't need it, and it must be
15469 allocated last, so that it will show up before the
15470 non-primary symtabs in the objfile's symtab list. */
15471 if (current_subfile == first_subfile)
15472 continue;
15473
15474 if (current_subfile->symtab == NULL)
15475 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15476 objfile);
cb1df416
DJ
15477 fe->symtab = current_subfile->symtab;
15478 }
15479 }
c906108c
SS
15480}
15481
15482/* Start a subfile for DWARF. FILENAME is the name of the file and
15483 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15484 or NULL if not known. COMP_DIR is the compilation directory for the
15485 linetable's compilation unit or NULL if not known.
c906108c
SS
15486 This routine tries to keep line numbers from identical absolute and
15487 relative file names in a common subfile.
15488
15489 Using the `list' example from the GDB testsuite, which resides in
15490 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15491 of /srcdir/list0.c yields the following debugging information for list0.c:
15492
c5aa993b
JM
15493 DW_AT_name: /srcdir/list0.c
15494 DW_AT_comp_dir: /compdir
357e46e7 15495 files.files[0].name: list0.h
c5aa993b 15496 files.files[0].dir: /srcdir
357e46e7 15497 files.files[1].name: list0.c
c5aa993b 15498 files.files[1].dir: /srcdir
c906108c
SS
15499
15500 The line number information for list0.c has to end up in a single
4f1520fb
FR
15501 subfile, so that `break /srcdir/list0.c:1' works as expected.
15502 start_subfile will ensure that this happens provided that we pass the
15503 concatenation of files.files[1].dir and files.files[1].name as the
15504 subfile's name. */
c906108c
SS
15505
15506static void
3e43a32a
MS
15507dwarf2_start_subfile (char *filename, const char *dirname,
15508 const char *comp_dir)
c906108c 15509{
4f1520fb
FR
15510 char *fullname;
15511
15512 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15513 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15514 second argument to start_subfile. To be consistent, we do the
15515 same here. In order not to lose the line information directory,
15516 we concatenate it to the filename when it makes sense.
15517 Note that the Dwarf3 standard says (speaking of filenames in line
15518 information): ``The directory index is ignored for file names
15519 that represent full path names''. Thus ignoring dirname in the
15520 `else' branch below isn't an issue. */
c906108c 15521
d5166ae1 15522 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15523 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15524 else
15525 fullname = filename;
c906108c 15526
4f1520fb
FR
15527 start_subfile (fullname, comp_dir);
15528
15529 if (fullname != filename)
15530 xfree (fullname);
c906108c
SS
15531}
15532
f4dc4d17
DE
15533/* Start a symtab for DWARF.
15534 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15535
15536static void
15537dwarf2_start_symtab (struct dwarf2_cu *cu,
15538 char *name, char *comp_dir, CORE_ADDR low_pc)
15539{
15540 start_symtab (name, comp_dir, low_pc);
15541 record_debugformat ("DWARF 2");
15542 record_producer (cu->producer);
15543
15544 /* We assume that we're processing GCC output. */
15545 processing_gcc_compilation = 2;
15546
15547 processing_has_namespace_info = 0;
15548}
15549
4c2df51b
DJ
15550static void
15551var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15552 struct dwarf2_cu *cu)
4c2df51b 15553{
e7c27a73
DJ
15554 struct objfile *objfile = cu->objfile;
15555 struct comp_unit_head *cu_header = &cu->header;
15556
4c2df51b
DJ
15557 /* NOTE drow/2003-01-30: There used to be a comment and some special
15558 code here to turn a symbol with DW_AT_external and a
15559 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15560 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15561 with some versions of binutils) where shared libraries could have
15562 relocations against symbols in their debug information - the
15563 minimal symbol would have the right address, but the debug info
15564 would not. It's no longer necessary, because we will explicitly
15565 apply relocations when we read in the debug information now. */
15566
15567 /* A DW_AT_location attribute with no contents indicates that a
15568 variable has been optimized away. */
15569 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15570 {
15571 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15572 return;
15573 }
15574
15575 /* Handle one degenerate form of location expression specially, to
15576 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15577 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15578 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15579
15580 if (attr_form_is_block (attr)
3019eac3
DE
15581 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15582 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15583 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15584 && (DW_BLOCK (attr)->size
15585 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15586 {
891d2f0b 15587 unsigned int dummy;
4c2df51b 15588
3019eac3
DE
15589 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15590 SYMBOL_VALUE_ADDRESS (sym) =
15591 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15592 else
15593 SYMBOL_VALUE_ADDRESS (sym) =
15594 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15595 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15596 fixup_symbol_section (sym, objfile);
15597 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15598 SYMBOL_SECTION (sym));
4c2df51b
DJ
15599 return;
15600 }
15601
15602 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15603 expression evaluator, and use LOC_COMPUTED only when necessary
15604 (i.e. when the value of a register or memory location is
15605 referenced, or a thread-local block, etc.). Then again, it might
15606 not be worthwhile. I'm assuming that it isn't unless performance
15607 or memory numbers show me otherwise. */
15608
e7c27a73 15609 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15610 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15611
15612 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15613 cu->has_loclist = 1;
4c2df51b
DJ
15614}
15615
c906108c
SS
15616/* Given a pointer to a DWARF information entry, figure out if we need
15617 to make a symbol table entry for it, and if so, create a new entry
15618 and return a pointer to it.
15619 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15620 used the passed type.
15621 If SPACE is not NULL, use it to hold the new symbol. If it is
15622 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15623
15624static struct symbol *
34eaf542
TT
15625new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15626 struct symbol *space)
c906108c 15627{
e7c27a73 15628 struct objfile *objfile = cu->objfile;
c906108c
SS
15629 struct symbol *sym = NULL;
15630 char *name;
15631 struct attribute *attr = NULL;
15632 struct attribute *attr2 = NULL;
e142c38c 15633 CORE_ADDR baseaddr;
e37fd15a
SW
15634 struct pending **list_to_add = NULL;
15635
edb3359d 15636 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15637
15638 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15639
94af9270 15640 name = dwarf2_name (die, cu);
c906108c
SS
15641 if (name)
15642 {
94af9270 15643 const char *linkagename;
34eaf542 15644 int suppress_add = 0;
94af9270 15645
34eaf542
TT
15646 if (space)
15647 sym = space;
15648 else
15649 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15650 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15651
15652 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15653 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15654 linkagename = dwarf2_physname (name, die, cu);
15655 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15656
f55ee35c
JK
15657 /* Fortran does not have mangling standard and the mangling does differ
15658 between gfortran, iFort etc. */
15659 if (cu->language == language_fortran
b250c185 15660 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
15661 symbol_set_demangled_name (&(sym->ginfo),
15662 (char *) dwarf2_full_name (name, die, cu),
15663 NULL);
f55ee35c 15664
c906108c 15665 /* Default assumptions.
c5aa993b 15666 Use the passed type or decode it from the die. */
176620f1 15667 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15668 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15669 if (type != NULL)
15670 SYMBOL_TYPE (sym) = type;
15671 else
e7c27a73 15672 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15673 attr = dwarf2_attr (die,
15674 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15675 cu);
c906108c
SS
15676 if (attr)
15677 {
15678 SYMBOL_LINE (sym) = DW_UNSND (attr);
15679 }
cb1df416 15680
edb3359d
DJ
15681 attr = dwarf2_attr (die,
15682 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15683 cu);
cb1df416
DJ
15684 if (attr)
15685 {
15686 int file_index = DW_UNSND (attr);
9a619af0 15687
cb1df416
DJ
15688 if (cu->line_header == NULL
15689 || file_index > cu->line_header->num_file_names)
15690 complaint (&symfile_complaints,
15691 _("file index out of range"));
1c3d648d 15692 else if (file_index > 0)
cb1df416
DJ
15693 {
15694 struct file_entry *fe;
9a619af0 15695
cb1df416
DJ
15696 fe = &cu->line_header->file_names[file_index - 1];
15697 SYMBOL_SYMTAB (sym) = fe->symtab;
15698 }
15699 }
15700
c906108c
SS
15701 switch (die->tag)
15702 {
15703 case DW_TAG_label:
e142c38c 15704 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15705 if (attr)
15706 {
15707 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15708 }
0f5238ed
TT
15709 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15710 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15711 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15712 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15713 break;
15714 case DW_TAG_subprogram:
15715 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15716 finish_block. */
15717 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15718 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15719 if ((attr2 && (DW_UNSND (attr2) != 0))
15720 || cu->language == language_ada)
c906108c 15721 {
2cfa0c8d
JB
15722 /* Subprograms marked external are stored as a global symbol.
15723 Ada subprograms, whether marked external or not, are always
15724 stored as a global symbol, because we want to be able to
15725 access them globally. For instance, we want to be able
15726 to break on a nested subprogram without having to
15727 specify the context. */
e37fd15a 15728 list_to_add = &global_symbols;
c906108c
SS
15729 }
15730 else
15731 {
e37fd15a 15732 list_to_add = cu->list_in_scope;
c906108c
SS
15733 }
15734 break;
edb3359d
DJ
15735 case DW_TAG_inlined_subroutine:
15736 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15737 finish_block. */
15738 SYMBOL_CLASS (sym) = LOC_BLOCK;
15739 SYMBOL_INLINED (sym) = 1;
481860b3 15740 list_to_add = cu->list_in_scope;
edb3359d 15741 break;
34eaf542
TT
15742 case DW_TAG_template_value_param:
15743 suppress_add = 1;
15744 /* Fall through. */
72929c62 15745 case DW_TAG_constant:
c906108c 15746 case DW_TAG_variable:
254e6b9e 15747 case DW_TAG_member:
0963b4bd
MS
15748 /* Compilation with minimal debug info may result in
15749 variables with missing type entries. Change the
15750 misleading `void' type to something sensible. */
c906108c 15751 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15752 SYMBOL_TYPE (sym)
46bf5051 15753 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15754
e142c38c 15755 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15756 /* In the case of DW_TAG_member, we should only be called for
15757 static const members. */
15758 if (die->tag == DW_TAG_member)
15759 {
3863f96c
DE
15760 /* dwarf2_add_field uses die_is_declaration,
15761 so we do the same. */
254e6b9e
DE
15762 gdb_assert (die_is_declaration (die, cu));
15763 gdb_assert (attr);
15764 }
c906108c
SS
15765 if (attr)
15766 {
e7c27a73 15767 dwarf2_const_value (attr, sym, cu);
e142c38c 15768 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15769 if (!suppress_add)
34eaf542
TT
15770 {
15771 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15772 list_to_add = &global_symbols;
34eaf542 15773 else
e37fd15a 15774 list_to_add = cu->list_in_scope;
34eaf542 15775 }
c906108c
SS
15776 break;
15777 }
e142c38c 15778 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15779 if (attr)
15780 {
e7c27a73 15781 var_decode_location (attr, sym, cu);
e142c38c 15782 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15783
15784 /* Fortran explicitly imports any global symbols to the local
15785 scope by DW_TAG_common_block. */
15786 if (cu->language == language_fortran && die->parent
15787 && die->parent->tag == DW_TAG_common_block)
15788 attr2 = NULL;
15789
caac4577
JG
15790 if (SYMBOL_CLASS (sym) == LOC_STATIC
15791 && SYMBOL_VALUE_ADDRESS (sym) == 0
15792 && !dwarf2_per_objfile->has_section_at_zero)
15793 {
15794 /* When a static variable is eliminated by the linker,
15795 the corresponding debug information is not stripped
15796 out, but the variable address is set to null;
15797 do not add such variables into symbol table. */
15798 }
15799 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15800 {
f55ee35c
JK
15801 /* Workaround gfortran PR debug/40040 - it uses
15802 DW_AT_location for variables in -fPIC libraries which may
15803 get overriden by other libraries/executable and get
15804 a different address. Resolve it by the minimal symbol
15805 which may come from inferior's executable using copy
15806 relocation. Make this workaround only for gfortran as for
15807 other compilers GDB cannot guess the minimal symbol
15808 Fortran mangling kind. */
15809 if (cu->language == language_fortran && die->parent
15810 && die->parent->tag == DW_TAG_module
15811 && cu->producer
15812 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15813 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15814
1c809c68
TT
15815 /* A variable with DW_AT_external is never static,
15816 but it may be block-scoped. */
15817 list_to_add = (cu->list_in_scope == &file_symbols
15818 ? &global_symbols : cu->list_in_scope);
1c809c68 15819 }
c906108c 15820 else
e37fd15a 15821 list_to_add = cu->list_in_scope;
c906108c
SS
15822 }
15823 else
15824 {
15825 /* We do not know the address of this symbol.
c5aa993b
JM
15826 If it is an external symbol and we have type information
15827 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15828 The address of the variable will then be determined from
15829 the minimal symbol table whenever the variable is
15830 referenced. */
e142c38c 15831 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15832
15833 /* Fortran explicitly imports any global symbols to the local
15834 scope by DW_TAG_common_block. */
15835 if (cu->language == language_fortran && die->parent
15836 && die->parent->tag == DW_TAG_common_block)
15837 {
15838 /* SYMBOL_CLASS doesn't matter here because
15839 read_common_block is going to reset it. */
15840 if (!suppress_add)
15841 list_to_add = cu->list_in_scope;
15842 }
15843 else if (attr2 && (DW_UNSND (attr2) != 0)
15844 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15845 {
0fe7935b
DJ
15846 /* A variable with DW_AT_external is never static, but it
15847 may be block-scoped. */
15848 list_to_add = (cu->list_in_scope == &file_symbols
15849 ? &global_symbols : cu->list_in_scope);
15850
c906108c 15851 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15852 }
442ddf59
JK
15853 else if (!die_is_declaration (die, cu))
15854 {
15855 /* Use the default LOC_OPTIMIZED_OUT class. */
15856 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15857 if (!suppress_add)
15858 list_to_add = cu->list_in_scope;
442ddf59 15859 }
c906108c
SS
15860 }
15861 break;
15862 case DW_TAG_formal_parameter:
edb3359d
DJ
15863 /* If we are inside a function, mark this as an argument. If
15864 not, we might be looking at an argument to an inlined function
15865 when we do not have enough information to show inlined frames;
15866 pretend it's a local variable in that case so that the user can
15867 still see it. */
15868 if (context_stack_depth > 0
15869 && context_stack[context_stack_depth - 1].name != NULL)
15870 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15871 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15872 if (attr)
15873 {
e7c27a73 15874 var_decode_location (attr, sym, cu);
c906108c 15875 }
e142c38c 15876 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15877 if (attr)
15878 {
e7c27a73 15879 dwarf2_const_value (attr, sym, cu);
c906108c 15880 }
f346a30d 15881
e37fd15a 15882 list_to_add = cu->list_in_scope;
c906108c
SS
15883 break;
15884 case DW_TAG_unspecified_parameters:
15885 /* From varargs functions; gdb doesn't seem to have any
15886 interest in this information, so just ignore it for now.
15887 (FIXME?) */
15888 break;
34eaf542
TT
15889 case DW_TAG_template_type_param:
15890 suppress_add = 1;
15891 /* Fall through. */
c906108c 15892 case DW_TAG_class_type:
680b30c7 15893 case DW_TAG_interface_type:
c906108c
SS
15894 case DW_TAG_structure_type:
15895 case DW_TAG_union_type:
72019c9c 15896 case DW_TAG_set_type:
c906108c
SS
15897 case DW_TAG_enumeration_type:
15898 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15899 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15900
63d06c5c 15901 {
987504bb 15902 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15903 really ever be static objects: otherwise, if you try
15904 to, say, break of a class's method and you're in a file
15905 which doesn't mention that class, it won't work unless
15906 the check for all static symbols in lookup_symbol_aux
15907 saves you. See the OtherFileClass tests in
15908 gdb.c++/namespace.exp. */
15909
e37fd15a 15910 if (!suppress_add)
34eaf542 15911 {
34eaf542
TT
15912 list_to_add = (cu->list_in_scope == &file_symbols
15913 && (cu->language == language_cplus
15914 || cu->language == language_java)
15915 ? &global_symbols : cu->list_in_scope);
63d06c5c 15916
64382290
TT
15917 /* The semantics of C++ state that "struct foo {
15918 ... }" also defines a typedef for "foo". A Java
15919 class declaration also defines a typedef for the
15920 class. */
15921 if (cu->language == language_cplus
15922 || cu->language == language_java
15923 || cu->language == language_ada)
15924 {
15925 /* The symbol's name is already allocated along
15926 with this objfile, so we don't need to
15927 duplicate it for the type. */
15928 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15929 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15930 }
63d06c5c
DC
15931 }
15932 }
c906108c
SS
15933 break;
15934 case DW_TAG_typedef:
63d06c5c
DC
15935 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15936 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15937 list_to_add = cu->list_in_scope;
63d06c5c 15938 break;
c906108c 15939 case DW_TAG_base_type:
a02abb62 15940 case DW_TAG_subrange_type:
c906108c 15941 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15942 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15943 list_to_add = cu->list_in_scope;
c906108c
SS
15944 break;
15945 case DW_TAG_enumerator:
e142c38c 15946 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15947 if (attr)
15948 {
e7c27a73 15949 dwarf2_const_value (attr, sym, cu);
c906108c 15950 }
63d06c5c
DC
15951 {
15952 /* NOTE: carlton/2003-11-10: See comment above in the
15953 DW_TAG_class_type, etc. block. */
15954
e142c38c 15955 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15956 && (cu->language == language_cplus
15957 || cu->language == language_java)
e142c38c 15958 ? &global_symbols : cu->list_in_scope);
63d06c5c 15959 }
c906108c 15960 break;
5c4e30ca
DC
15961 case DW_TAG_namespace:
15962 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15963 list_to_add = &global_symbols;
5c4e30ca 15964 break;
4357ac6c
TT
15965 case DW_TAG_common_block:
15966 SYMBOL_CLASS (sym) = LOC_STATIC;
15967 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15968 add_symbol_to_list (sym, cu->list_in_scope);
15969 break;
c906108c
SS
15970 default:
15971 /* Not a tag we recognize. Hopefully we aren't processing
15972 trash data, but since we must specifically ignore things
15973 we don't recognize, there is nothing else we should do at
0963b4bd 15974 this point. */
e2e0b3e5 15975 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 15976 dwarf_tag_name (die->tag));
c906108c
SS
15977 break;
15978 }
df8a16a1 15979
e37fd15a
SW
15980 if (suppress_add)
15981 {
15982 sym->hash_next = objfile->template_symbols;
15983 objfile->template_symbols = sym;
15984 list_to_add = NULL;
15985 }
15986
15987 if (list_to_add != NULL)
15988 add_symbol_to_list (sym, list_to_add);
15989
df8a16a1
DJ
15990 /* For the benefit of old versions of GCC, check for anonymous
15991 namespaces based on the demangled name. */
15992 if (!processing_has_namespace_info
94af9270 15993 && cu->language == language_cplus)
a10964d1 15994 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
15995 }
15996 return (sym);
15997}
15998
34eaf542
TT
15999/* A wrapper for new_symbol_full that always allocates a new symbol. */
16000
16001static struct symbol *
16002new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16003{
16004 return new_symbol_full (die, type, cu, NULL);
16005}
16006
98bfdba5
PA
16007/* Given an attr with a DW_FORM_dataN value in host byte order,
16008 zero-extend it as appropriate for the symbol's type. The DWARF
16009 standard (v4) is not entirely clear about the meaning of using
16010 DW_FORM_dataN for a constant with a signed type, where the type is
16011 wider than the data. The conclusion of a discussion on the DWARF
16012 list was that this is unspecified. We choose to always zero-extend
16013 because that is the interpretation long in use by GCC. */
c906108c 16014
98bfdba5
PA
16015static gdb_byte *
16016dwarf2_const_value_data (struct attribute *attr, struct type *type,
16017 const char *name, struct obstack *obstack,
12df843f 16018 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16019{
e7c27a73 16020 struct objfile *objfile = cu->objfile;
e17a4113
UW
16021 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16022 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16023 LONGEST l = DW_UNSND (attr);
16024
16025 if (bits < sizeof (*value) * 8)
16026 {
16027 l &= ((LONGEST) 1 << bits) - 1;
16028 *value = l;
16029 }
16030 else if (bits == sizeof (*value) * 8)
16031 *value = l;
16032 else
16033 {
16034 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16035 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16036 return bytes;
16037 }
16038
16039 return NULL;
16040}
16041
16042/* Read a constant value from an attribute. Either set *VALUE, or if
16043 the value does not fit in *VALUE, set *BYTES - either already
16044 allocated on the objfile obstack, or newly allocated on OBSTACK,
16045 or, set *BATON, if we translated the constant to a location
16046 expression. */
16047
16048static void
16049dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16050 const char *name, struct obstack *obstack,
16051 struct dwarf2_cu *cu,
12df843f 16052 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16053 struct dwarf2_locexpr_baton **baton)
16054{
16055 struct objfile *objfile = cu->objfile;
16056 struct comp_unit_head *cu_header = &cu->header;
c906108c 16057 struct dwarf_block *blk;
98bfdba5
PA
16058 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16059 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16060
16061 *value = 0;
16062 *bytes = NULL;
16063 *baton = NULL;
c906108c
SS
16064
16065 switch (attr->form)
16066 {
16067 case DW_FORM_addr:
3019eac3 16068 case DW_FORM_GNU_addr_index:
ac56253d 16069 {
ac56253d
TT
16070 gdb_byte *data;
16071
98bfdba5
PA
16072 if (TYPE_LENGTH (type) != cu_header->addr_size)
16073 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16074 cu_header->addr_size,
98bfdba5 16075 TYPE_LENGTH (type));
ac56253d
TT
16076 /* Symbols of this form are reasonably rare, so we just
16077 piggyback on the existing location code rather than writing
16078 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16079 *baton = obstack_alloc (&objfile->objfile_obstack,
16080 sizeof (struct dwarf2_locexpr_baton));
16081 (*baton)->per_cu = cu->per_cu;
16082 gdb_assert ((*baton)->per_cu);
ac56253d 16083
98bfdba5
PA
16084 (*baton)->size = 2 + cu_header->addr_size;
16085 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16086 (*baton)->data = data;
ac56253d
TT
16087
16088 data[0] = DW_OP_addr;
16089 store_unsigned_integer (&data[1], cu_header->addr_size,
16090 byte_order, DW_ADDR (attr));
16091 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16092 }
c906108c 16093 break;
4ac36638 16094 case DW_FORM_string:
93b5768b 16095 case DW_FORM_strp:
3019eac3 16096 case DW_FORM_GNU_str_index:
36586728 16097 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16098 /* DW_STRING is already allocated on the objfile obstack, point
16099 directly to it. */
16100 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16101 break;
c906108c
SS
16102 case DW_FORM_block1:
16103 case DW_FORM_block2:
16104 case DW_FORM_block4:
16105 case DW_FORM_block:
2dc7f7b3 16106 case DW_FORM_exprloc:
c906108c 16107 blk = DW_BLOCK (attr);
98bfdba5
PA
16108 if (TYPE_LENGTH (type) != blk->size)
16109 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16110 TYPE_LENGTH (type));
16111 *bytes = blk->data;
c906108c 16112 break;
2df3850c
JM
16113
16114 /* The DW_AT_const_value attributes are supposed to carry the
16115 symbol's value "represented as it would be on the target
16116 architecture." By the time we get here, it's already been
16117 converted to host endianness, so we just need to sign- or
16118 zero-extend it as appropriate. */
16119 case DW_FORM_data1:
3e43a32a
MS
16120 *bytes = dwarf2_const_value_data (attr, type, name,
16121 obstack, cu, value, 8);
2df3850c 16122 break;
c906108c 16123 case DW_FORM_data2:
3e43a32a
MS
16124 *bytes = dwarf2_const_value_data (attr, type, name,
16125 obstack, cu, value, 16);
2df3850c 16126 break;
c906108c 16127 case DW_FORM_data4:
3e43a32a
MS
16128 *bytes = dwarf2_const_value_data (attr, type, name,
16129 obstack, cu, value, 32);
2df3850c 16130 break;
c906108c 16131 case DW_FORM_data8:
3e43a32a
MS
16132 *bytes = dwarf2_const_value_data (attr, type, name,
16133 obstack, cu, value, 64);
2df3850c
JM
16134 break;
16135
c906108c 16136 case DW_FORM_sdata:
98bfdba5 16137 *value = DW_SND (attr);
2df3850c
JM
16138 break;
16139
c906108c 16140 case DW_FORM_udata:
98bfdba5 16141 *value = DW_UNSND (attr);
c906108c 16142 break;
2df3850c 16143
c906108c 16144 default:
4d3c2250 16145 complaint (&symfile_complaints,
e2e0b3e5 16146 _("unsupported const value attribute form: '%s'"),
4d3c2250 16147 dwarf_form_name (attr->form));
98bfdba5 16148 *value = 0;
c906108c
SS
16149 break;
16150 }
16151}
16152
2df3850c 16153
98bfdba5
PA
16154/* Copy constant value from an attribute to a symbol. */
16155
2df3850c 16156static void
98bfdba5
PA
16157dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16158 struct dwarf2_cu *cu)
2df3850c 16159{
98bfdba5
PA
16160 struct objfile *objfile = cu->objfile;
16161 struct comp_unit_head *cu_header = &cu->header;
12df843f 16162 LONGEST value;
98bfdba5
PA
16163 gdb_byte *bytes;
16164 struct dwarf2_locexpr_baton *baton;
2df3850c 16165
98bfdba5
PA
16166 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16167 SYMBOL_PRINT_NAME (sym),
16168 &objfile->objfile_obstack, cu,
16169 &value, &bytes, &baton);
2df3850c 16170
98bfdba5
PA
16171 if (baton != NULL)
16172 {
16173 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16174 SYMBOL_LOCATION_BATON (sym) = baton;
16175 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16176 }
16177 else if (bytes != NULL)
16178 {
16179 SYMBOL_VALUE_BYTES (sym) = bytes;
16180 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16181 }
16182 else
16183 {
16184 SYMBOL_VALUE (sym) = value;
16185 SYMBOL_CLASS (sym) = LOC_CONST;
16186 }
2df3850c
JM
16187}
16188
c906108c
SS
16189/* Return the type of the die in question using its DW_AT_type attribute. */
16190
16191static struct type *
e7c27a73 16192die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16193{
c906108c 16194 struct attribute *type_attr;
c906108c 16195
e142c38c 16196 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16197 if (!type_attr)
16198 {
16199 /* A missing DW_AT_type represents a void type. */
46bf5051 16200 return objfile_type (cu->objfile)->builtin_void;
c906108c 16201 }
348e048f 16202
673bfd45 16203 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16204}
16205
b4ba55a1
JB
16206/* True iff CU's producer generates GNAT Ada auxiliary information
16207 that allows to find parallel types through that information instead
16208 of having to do expensive parallel lookups by type name. */
16209
16210static int
16211need_gnat_info (struct dwarf2_cu *cu)
16212{
16213 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16214 of GNAT produces this auxiliary information, without any indication
16215 that it is produced. Part of enhancing the FSF version of GNAT
16216 to produce that information will be to put in place an indicator
16217 that we can use in order to determine whether the descriptive type
16218 info is available or not. One suggestion that has been made is
16219 to use a new attribute, attached to the CU die. For now, assume
16220 that the descriptive type info is not available. */
16221 return 0;
16222}
16223
b4ba55a1
JB
16224/* Return the auxiliary type of the die in question using its
16225 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16226 attribute is not present. */
16227
16228static struct type *
16229die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16230{
b4ba55a1 16231 struct attribute *type_attr;
b4ba55a1
JB
16232
16233 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16234 if (!type_attr)
16235 return NULL;
16236
673bfd45 16237 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16238}
16239
16240/* If DIE has a descriptive_type attribute, then set the TYPE's
16241 descriptive type accordingly. */
16242
16243static void
16244set_descriptive_type (struct type *type, struct die_info *die,
16245 struct dwarf2_cu *cu)
16246{
16247 struct type *descriptive_type = die_descriptive_type (die, cu);
16248
16249 if (descriptive_type)
16250 {
16251 ALLOCATE_GNAT_AUX_TYPE (type);
16252 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16253 }
16254}
16255
c906108c
SS
16256/* Return the containing type of the die in question using its
16257 DW_AT_containing_type attribute. */
16258
16259static struct type *
e7c27a73 16260die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16261{
c906108c 16262 struct attribute *type_attr;
c906108c 16263
e142c38c 16264 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16265 if (!type_attr)
16266 error (_("Dwarf Error: Problem turning containing type into gdb type "
16267 "[in module %s]"), cu->objfile->name);
16268
673bfd45 16269 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16270}
16271
673bfd45
DE
16272/* Look up the type of DIE in CU using its type attribute ATTR.
16273 If there is no type substitute an error marker. */
16274
c906108c 16275static struct type *
673bfd45
DE
16276lookup_die_type (struct die_info *die, struct attribute *attr,
16277 struct dwarf2_cu *cu)
c906108c 16278{
bb5ed363 16279 struct objfile *objfile = cu->objfile;
f792889a
DJ
16280 struct type *this_type;
16281
673bfd45
DE
16282 /* First see if we have it cached. */
16283
36586728
TT
16284 if (attr->form == DW_FORM_GNU_ref_alt)
16285 {
16286 struct dwarf2_per_cu_data *per_cu;
16287 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16288
16289 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16290 this_type = get_die_type_at_offset (offset, per_cu);
16291 }
16292 else if (is_ref_attr (attr))
673bfd45 16293 {
b64f50a1 16294 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16295
16296 this_type = get_die_type_at_offset (offset, cu->per_cu);
16297 }
55f1336d 16298 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16299 {
16300 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16301
16302 /* sig_type will be NULL if the signatured type is missing from
16303 the debug info. */
16304 if (sig_type == NULL)
16305 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16306 "at 0x%x [in module %s]"),
b64f50a1 16307 die->offset.sect_off, objfile->name);
673bfd45 16308
3019eac3
DE
16309 gdb_assert (sig_type->per_cu.is_debug_types);
16310 /* If we haven't filled in type_offset_in_section yet, then we
16311 haven't read the type in yet. */
16312 this_type = NULL;
16313 if (sig_type->type_offset_in_section.sect_off != 0)
16314 {
16315 this_type =
16316 get_die_type_at_offset (sig_type->type_offset_in_section,
16317 &sig_type->per_cu);
16318 }
673bfd45
DE
16319 }
16320 else
16321 {
16322 dump_die_for_error (die);
16323 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16324 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16325 }
16326
16327 /* If not cached we need to read it in. */
16328
16329 if (this_type == NULL)
16330 {
16331 struct die_info *type_die;
16332 struct dwarf2_cu *type_cu = cu;
16333
16334 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16335 /* If we found the type now, it's probably because the type came
16336 from an inter-CU reference and the type's CU got expanded before
16337 ours. */
16338 this_type = get_die_type (type_die, type_cu);
16339 if (this_type == NULL)
16340 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16341 }
16342
16343 /* If we still don't have a type use an error marker. */
16344
16345 if (this_type == NULL)
c906108c 16346 {
b00fdb78
TT
16347 char *message, *saved;
16348
16349 /* read_type_die already issued a complaint. */
16350 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16351 objfile->name,
b64f50a1
JK
16352 cu->header.offset.sect_off,
16353 die->offset.sect_off);
bb5ed363 16354 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16355 message, strlen (message));
16356 xfree (message);
16357
bb5ed363 16358 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16359 }
673bfd45 16360
f792889a 16361 return this_type;
c906108c
SS
16362}
16363
673bfd45
DE
16364/* Return the type in DIE, CU.
16365 Returns NULL for invalid types.
16366
16367 This first does a lookup in the appropriate type_hash table,
16368 and only reads the die in if necessary.
16369
16370 NOTE: This can be called when reading in partial or full symbols. */
16371
f792889a 16372static struct type *
e7c27a73 16373read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16374{
f792889a
DJ
16375 struct type *this_type;
16376
16377 this_type = get_die_type (die, cu);
16378 if (this_type)
16379 return this_type;
16380
673bfd45
DE
16381 return read_type_die_1 (die, cu);
16382}
16383
16384/* Read the type in DIE, CU.
16385 Returns NULL for invalid types. */
16386
16387static struct type *
16388read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16389{
16390 struct type *this_type = NULL;
16391
c906108c
SS
16392 switch (die->tag)
16393 {
16394 case DW_TAG_class_type:
680b30c7 16395 case DW_TAG_interface_type:
c906108c
SS
16396 case DW_TAG_structure_type:
16397 case DW_TAG_union_type:
f792889a 16398 this_type = read_structure_type (die, cu);
c906108c
SS
16399 break;
16400 case DW_TAG_enumeration_type:
f792889a 16401 this_type = read_enumeration_type (die, cu);
c906108c
SS
16402 break;
16403 case DW_TAG_subprogram:
16404 case DW_TAG_subroutine_type:
edb3359d 16405 case DW_TAG_inlined_subroutine:
f792889a 16406 this_type = read_subroutine_type (die, cu);
c906108c
SS
16407 break;
16408 case DW_TAG_array_type:
f792889a 16409 this_type = read_array_type (die, cu);
c906108c 16410 break;
72019c9c 16411 case DW_TAG_set_type:
f792889a 16412 this_type = read_set_type (die, cu);
72019c9c 16413 break;
c906108c 16414 case DW_TAG_pointer_type:
f792889a 16415 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16416 break;
16417 case DW_TAG_ptr_to_member_type:
f792889a 16418 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16419 break;
16420 case DW_TAG_reference_type:
f792889a 16421 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16422 break;
16423 case DW_TAG_const_type:
f792889a 16424 this_type = read_tag_const_type (die, cu);
c906108c
SS
16425 break;
16426 case DW_TAG_volatile_type:
f792889a 16427 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
16428 break;
16429 case DW_TAG_string_type:
f792889a 16430 this_type = read_tag_string_type (die, cu);
c906108c
SS
16431 break;
16432 case DW_TAG_typedef:
f792889a 16433 this_type = read_typedef (die, cu);
c906108c 16434 break;
a02abb62 16435 case DW_TAG_subrange_type:
f792889a 16436 this_type = read_subrange_type (die, cu);
a02abb62 16437 break;
c906108c 16438 case DW_TAG_base_type:
f792889a 16439 this_type = read_base_type (die, cu);
c906108c 16440 break;
81a17f79 16441 case DW_TAG_unspecified_type:
f792889a 16442 this_type = read_unspecified_type (die, cu);
81a17f79 16443 break;
0114d602
DJ
16444 case DW_TAG_namespace:
16445 this_type = read_namespace_type (die, cu);
16446 break;
f55ee35c
JK
16447 case DW_TAG_module:
16448 this_type = read_module_type (die, cu);
16449 break;
c906108c 16450 default:
3e43a32a
MS
16451 complaint (&symfile_complaints,
16452 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16453 dwarf_tag_name (die->tag));
c906108c
SS
16454 break;
16455 }
63d06c5c 16456
f792889a 16457 return this_type;
63d06c5c
DC
16458}
16459
abc72ce4
DE
16460/* See if we can figure out if the class lives in a namespace. We do
16461 this by looking for a member function; its demangled name will
16462 contain namespace info, if there is any.
16463 Return the computed name or NULL.
16464 Space for the result is allocated on the objfile's obstack.
16465 This is the full-die version of guess_partial_die_structure_name.
16466 In this case we know DIE has no useful parent. */
16467
16468static char *
16469guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16470{
16471 struct die_info *spec_die;
16472 struct dwarf2_cu *spec_cu;
16473 struct die_info *child;
16474
16475 spec_cu = cu;
16476 spec_die = die_specification (die, &spec_cu);
16477 if (spec_die != NULL)
16478 {
16479 die = spec_die;
16480 cu = spec_cu;
16481 }
16482
16483 for (child = die->child;
16484 child != NULL;
16485 child = child->sibling)
16486 {
16487 if (child->tag == DW_TAG_subprogram)
16488 {
16489 struct attribute *attr;
16490
16491 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16492 if (attr == NULL)
16493 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16494 if (attr != NULL)
16495 {
16496 char *actual_name
16497 = language_class_name_from_physname (cu->language_defn,
16498 DW_STRING (attr));
16499 char *name = NULL;
16500
16501 if (actual_name != NULL)
16502 {
16503 char *die_name = dwarf2_name (die, cu);
16504
16505 if (die_name != NULL
16506 && strcmp (die_name, actual_name) != 0)
16507 {
16508 /* Strip off the class name from the full name.
16509 We want the prefix. */
16510 int die_name_len = strlen (die_name);
16511 int actual_name_len = strlen (actual_name);
16512
16513 /* Test for '::' as a sanity check. */
16514 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16515 && actual_name[actual_name_len
16516 - die_name_len - 1] == ':')
abc72ce4
DE
16517 name =
16518 obsavestring (actual_name,
16519 actual_name_len - die_name_len - 2,
16520 &cu->objfile->objfile_obstack);
16521 }
16522 }
16523 xfree (actual_name);
16524 return name;
16525 }
16526 }
16527 }
16528
16529 return NULL;
16530}
16531
96408a79
SA
16532/* GCC might emit a nameless typedef that has a linkage name. Determine the
16533 prefix part in such case. See
16534 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16535
16536static char *
16537anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16538{
16539 struct attribute *attr;
16540 char *base;
16541
16542 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16543 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16544 return NULL;
16545
16546 attr = dwarf2_attr (die, DW_AT_name, cu);
16547 if (attr != NULL && DW_STRING (attr) != NULL)
16548 return NULL;
16549
16550 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16551 if (attr == NULL)
16552 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16553 if (attr == NULL || DW_STRING (attr) == NULL)
16554 return NULL;
16555
16556 /* dwarf2_name had to be already called. */
16557 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16558
16559 /* Strip the base name, keep any leading namespaces/classes. */
16560 base = strrchr (DW_STRING (attr), ':');
16561 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16562 return "";
16563
16564 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16565 &cu->objfile->objfile_obstack);
16566}
16567
fdde2d81 16568/* Return the name of the namespace/class that DIE is defined within,
0114d602 16569 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16570
0114d602
DJ
16571 For example, if we're within the method foo() in the following
16572 code:
16573
16574 namespace N {
16575 class C {
16576 void foo () {
16577 }
16578 };
16579 }
16580
16581 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16582
0d5cff50 16583static const char *
e142c38c 16584determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16585{
0114d602
DJ
16586 struct die_info *parent, *spec_die;
16587 struct dwarf2_cu *spec_cu;
16588 struct type *parent_type;
96408a79 16589 char *retval;
63d06c5c 16590
f55ee35c
JK
16591 if (cu->language != language_cplus && cu->language != language_java
16592 && cu->language != language_fortran)
0114d602
DJ
16593 return "";
16594
96408a79
SA
16595 retval = anonymous_struct_prefix (die, cu);
16596 if (retval)
16597 return retval;
16598
0114d602
DJ
16599 /* We have to be careful in the presence of DW_AT_specification.
16600 For example, with GCC 3.4, given the code
16601
16602 namespace N {
16603 void foo() {
16604 // Definition of N::foo.
16605 }
16606 }
16607
16608 then we'll have a tree of DIEs like this:
16609
16610 1: DW_TAG_compile_unit
16611 2: DW_TAG_namespace // N
16612 3: DW_TAG_subprogram // declaration of N::foo
16613 4: DW_TAG_subprogram // definition of N::foo
16614 DW_AT_specification // refers to die #3
16615
16616 Thus, when processing die #4, we have to pretend that we're in
16617 the context of its DW_AT_specification, namely the contex of die
16618 #3. */
16619 spec_cu = cu;
16620 spec_die = die_specification (die, &spec_cu);
16621 if (spec_die == NULL)
16622 parent = die->parent;
16623 else
63d06c5c 16624 {
0114d602
DJ
16625 parent = spec_die->parent;
16626 cu = spec_cu;
63d06c5c 16627 }
0114d602
DJ
16628
16629 if (parent == NULL)
16630 return "";
98bfdba5
PA
16631 else if (parent->building_fullname)
16632 {
16633 const char *name;
16634 const char *parent_name;
16635
16636 /* It has been seen on RealView 2.2 built binaries,
16637 DW_TAG_template_type_param types actually _defined_ as
16638 children of the parent class:
16639
16640 enum E {};
16641 template class <class Enum> Class{};
16642 Class<enum E> class_e;
16643
16644 1: DW_TAG_class_type (Class)
16645 2: DW_TAG_enumeration_type (E)
16646 3: DW_TAG_enumerator (enum1:0)
16647 3: DW_TAG_enumerator (enum2:1)
16648 ...
16649 2: DW_TAG_template_type_param
16650 DW_AT_type DW_FORM_ref_udata (E)
16651
16652 Besides being broken debug info, it can put GDB into an
16653 infinite loop. Consider:
16654
16655 When we're building the full name for Class<E>, we'll start
16656 at Class, and go look over its template type parameters,
16657 finding E. We'll then try to build the full name of E, and
16658 reach here. We're now trying to build the full name of E,
16659 and look over the parent DIE for containing scope. In the
16660 broken case, if we followed the parent DIE of E, we'd again
16661 find Class, and once again go look at its template type
16662 arguments, etc., etc. Simply don't consider such parent die
16663 as source-level parent of this die (it can't be, the language
16664 doesn't allow it), and break the loop here. */
16665 name = dwarf2_name (die, cu);
16666 parent_name = dwarf2_name (parent, cu);
16667 complaint (&symfile_complaints,
16668 _("template param type '%s' defined within parent '%s'"),
16669 name ? name : "<unknown>",
16670 parent_name ? parent_name : "<unknown>");
16671 return "";
16672 }
63d06c5c 16673 else
0114d602
DJ
16674 switch (parent->tag)
16675 {
63d06c5c 16676 case DW_TAG_namespace:
0114d602 16677 parent_type = read_type_die (parent, cu);
acebe513
UW
16678 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16679 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16680 Work around this problem here. */
16681 if (cu->language == language_cplus
16682 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16683 return "";
0114d602
DJ
16684 /* We give a name to even anonymous namespaces. */
16685 return TYPE_TAG_NAME (parent_type);
63d06c5c 16686 case DW_TAG_class_type:
680b30c7 16687 case DW_TAG_interface_type:
63d06c5c 16688 case DW_TAG_structure_type:
0114d602 16689 case DW_TAG_union_type:
f55ee35c 16690 case DW_TAG_module:
0114d602
DJ
16691 parent_type = read_type_die (parent, cu);
16692 if (TYPE_TAG_NAME (parent_type) != NULL)
16693 return TYPE_TAG_NAME (parent_type);
16694 else
16695 /* An anonymous structure is only allowed non-static data
16696 members; no typedefs, no member functions, et cetera.
16697 So it does not need a prefix. */
16698 return "";
abc72ce4 16699 case DW_TAG_compile_unit:
95554aad 16700 case DW_TAG_partial_unit:
abc72ce4
DE
16701 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16702 if (cu->language == language_cplus
8b70b953 16703 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16704 && die->child != NULL
16705 && (die->tag == DW_TAG_class_type
16706 || die->tag == DW_TAG_structure_type
16707 || die->tag == DW_TAG_union_type))
16708 {
16709 char *name = guess_full_die_structure_name (die, cu);
16710 if (name != NULL)
16711 return name;
16712 }
16713 return "";
63d06c5c 16714 default:
8176b9b8 16715 return determine_prefix (parent, cu);
63d06c5c 16716 }
63d06c5c
DC
16717}
16718
3e43a32a
MS
16719/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16720 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16721 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16722 an obconcat, otherwise allocate storage for the result. The CU argument is
16723 used to determine the language and hence, the appropriate separator. */
987504bb 16724
f55ee35c 16725#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16726
16727static char *
f55ee35c
JK
16728typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16729 int physname, struct dwarf2_cu *cu)
63d06c5c 16730{
f55ee35c 16731 const char *lead = "";
5c315b68 16732 const char *sep;
63d06c5c 16733
3e43a32a
MS
16734 if (suffix == NULL || suffix[0] == '\0'
16735 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16736 sep = "";
16737 else if (cu->language == language_java)
16738 sep = ".";
f55ee35c
JK
16739 else if (cu->language == language_fortran && physname)
16740 {
16741 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16742 DW_AT_MIPS_linkage_name is preferred and used instead. */
16743
16744 lead = "__";
16745 sep = "_MOD_";
16746 }
987504bb
JJ
16747 else
16748 sep = "::";
63d06c5c 16749
6dd47d34
DE
16750 if (prefix == NULL)
16751 prefix = "";
16752 if (suffix == NULL)
16753 suffix = "";
16754
987504bb
JJ
16755 if (obs == NULL)
16756 {
3e43a32a
MS
16757 char *retval
16758 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16759
f55ee35c
JK
16760 strcpy (retval, lead);
16761 strcat (retval, prefix);
6dd47d34
DE
16762 strcat (retval, sep);
16763 strcat (retval, suffix);
63d06c5c
DC
16764 return retval;
16765 }
987504bb
JJ
16766 else
16767 {
16768 /* We have an obstack. */
f55ee35c 16769 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16770 }
63d06c5c
DC
16771}
16772
c906108c
SS
16773/* Return sibling of die, NULL if no sibling. */
16774
f9aca02d 16775static struct die_info *
fba45db2 16776sibling_die (struct die_info *die)
c906108c 16777{
639d11d3 16778 return die->sibling;
c906108c
SS
16779}
16780
71c25dea
TT
16781/* Get name of a die, return NULL if not found. */
16782
16783static char *
16784dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16785 struct obstack *obstack)
16786{
16787 if (name && cu->language == language_cplus)
16788 {
16789 char *canon_name = cp_canonicalize_string (name);
16790
16791 if (canon_name != NULL)
16792 {
16793 if (strcmp (canon_name, name) != 0)
16794 name = obsavestring (canon_name, strlen (canon_name),
16795 obstack);
16796 xfree (canon_name);
16797 }
16798 }
16799
16800 return name;
c906108c
SS
16801}
16802
9219021c
DC
16803/* Get name of a die, return NULL if not found. */
16804
16805static char *
e142c38c 16806dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16807{
16808 struct attribute *attr;
16809
e142c38c 16810 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16811 if ((!attr || !DW_STRING (attr))
16812 && die->tag != DW_TAG_class_type
16813 && die->tag != DW_TAG_interface_type
16814 && die->tag != DW_TAG_structure_type
16815 && die->tag != DW_TAG_union_type)
71c25dea
TT
16816 return NULL;
16817
16818 switch (die->tag)
16819 {
16820 case DW_TAG_compile_unit:
95554aad 16821 case DW_TAG_partial_unit:
71c25dea
TT
16822 /* Compilation units have a DW_AT_name that is a filename, not
16823 a source language identifier. */
16824 case DW_TAG_enumeration_type:
16825 case DW_TAG_enumerator:
16826 /* These tags always have simple identifiers already; no need
16827 to canonicalize them. */
16828 return DW_STRING (attr);
907af001 16829
418835cc
KS
16830 case DW_TAG_subprogram:
16831 /* Java constructors will all be named "<init>", so return
16832 the class name when we see this special case. */
16833 if (cu->language == language_java
16834 && DW_STRING (attr) != NULL
16835 && strcmp (DW_STRING (attr), "<init>") == 0)
16836 {
16837 struct dwarf2_cu *spec_cu = cu;
16838 struct die_info *spec_die;
16839
16840 /* GCJ will output '<init>' for Java constructor names.
16841 For this special case, return the name of the parent class. */
16842
16843 /* GCJ may output suprogram DIEs with AT_specification set.
16844 If so, use the name of the specified DIE. */
16845 spec_die = die_specification (die, &spec_cu);
16846 if (spec_die != NULL)
16847 return dwarf2_name (spec_die, spec_cu);
16848
16849 do
16850 {
16851 die = die->parent;
16852 if (die->tag == DW_TAG_class_type)
16853 return dwarf2_name (die, cu);
16854 }
95554aad
TT
16855 while (die->tag != DW_TAG_compile_unit
16856 && die->tag != DW_TAG_partial_unit);
418835cc 16857 }
907af001
UW
16858 break;
16859
16860 case DW_TAG_class_type:
16861 case DW_TAG_interface_type:
16862 case DW_TAG_structure_type:
16863 case DW_TAG_union_type:
16864 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16865 structures or unions. These were of the form "._%d" in GCC 4.1,
16866 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16867 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16868 if (attr && DW_STRING (attr)
16869 && (strncmp (DW_STRING (attr), "._", 2) == 0
16870 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16871 return NULL;
53832f31
TT
16872
16873 /* GCC might emit a nameless typedef that has a linkage name. See
16874 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16875 if (!attr || DW_STRING (attr) == NULL)
16876 {
df5c6c50 16877 char *demangled = NULL;
53832f31
TT
16878
16879 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16880 if (attr == NULL)
16881 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16882
16883 if (attr == NULL || DW_STRING (attr) == NULL)
16884 return NULL;
16885
df5c6c50
JK
16886 /* Avoid demangling DW_STRING (attr) the second time on a second
16887 call for the same DIE. */
16888 if (!DW_STRING_IS_CANONICAL (attr))
16889 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16890
16891 if (demangled)
16892 {
96408a79
SA
16893 char *base;
16894
53832f31 16895 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16896 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16897 &cu->objfile->objfile_obstack);
53832f31
TT
16898 DW_STRING_IS_CANONICAL (attr) = 1;
16899 xfree (demangled);
96408a79
SA
16900
16901 /* Strip any leading namespaces/classes, keep only the base name.
16902 DW_AT_name for named DIEs does not contain the prefixes. */
16903 base = strrchr (DW_STRING (attr), ':');
16904 if (base && base > DW_STRING (attr) && base[-1] == ':')
16905 return &base[1];
16906 else
16907 return DW_STRING (attr);
53832f31
TT
16908 }
16909 }
907af001
UW
16910 break;
16911
71c25dea 16912 default:
907af001
UW
16913 break;
16914 }
16915
16916 if (!DW_STRING_IS_CANONICAL (attr))
16917 {
16918 DW_STRING (attr)
16919 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16920 &cu->objfile->objfile_obstack);
16921 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16922 }
907af001 16923 return DW_STRING (attr);
9219021c
DC
16924}
16925
16926/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16927 is none. *EXT_CU is the CU containing DIE on input, and the CU
16928 containing the return value on output. */
9219021c
DC
16929
16930static struct die_info *
f2f0e013 16931dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16932{
16933 struct attribute *attr;
9219021c 16934
f2f0e013 16935 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16936 if (attr == NULL)
16937 return NULL;
16938
f2f0e013 16939 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16940}
16941
c906108c
SS
16942/* Convert a DIE tag into its string name. */
16943
f39c6ffd 16944static const char *
aa1ee363 16945dwarf_tag_name (unsigned tag)
c906108c 16946{
f39c6ffd
TT
16947 const char *name = get_DW_TAG_name (tag);
16948
16949 if (name == NULL)
16950 return "DW_TAG_<unknown>";
16951
16952 return name;
c906108c
SS
16953}
16954
16955/* Convert a DWARF attribute code into its string name. */
16956
f39c6ffd 16957static const char *
aa1ee363 16958dwarf_attr_name (unsigned attr)
c906108c 16959{
f39c6ffd
TT
16960 const char *name;
16961
c764a876 16962#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16963 if (attr == DW_AT_MIPS_fde)
16964 return "DW_AT_MIPS_fde";
16965#else
16966 if (attr == DW_AT_HP_block_index)
16967 return "DW_AT_HP_block_index";
c764a876 16968#endif
f39c6ffd
TT
16969
16970 name = get_DW_AT_name (attr);
16971
16972 if (name == NULL)
16973 return "DW_AT_<unknown>";
16974
16975 return name;
c906108c
SS
16976}
16977
16978/* Convert a DWARF value form code into its string name. */
16979
f39c6ffd 16980static const char *
aa1ee363 16981dwarf_form_name (unsigned form)
c906108c 16982{
f39c6ffd
TT
16983 const char *name = get_DW_FORM_name (form);
16984
16985 if (name == NULL)
16986 return "DW_FORM_<unknown>";
16987
16988 return name;
c906108c
SS
16989}
16990
16991static char *
fba45db2 16992dwarf_bool_name (unsigned mybool)
c906108c
SS
16993{
16994 if (mybool)
16995 return "TRUE";
16996 else
16997 return "FALSE";
16998}
16999
17000/* Convert a DWARF type code into its string name. */
17001
f39c6ffd 17002static const char *
aa1ee363 17003dwarf_type_encoding_name (unsigned enc)
c906108c 17004{
f39c6ffd 17005 const char *name = get_DW_ATE_name (enc);
c906108c 17006
f39c6ffd
TT
17007 if (name == NULL)
17008 return "DW_ATE_<unknown>";
c906108c 17009
f39c6ffd 17010 return name;
c906108c 17011}
c906108c 17012
f9aca02d 17013static void
d97bc12b 17014dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17015{
17016 unsigned int i;
17017
d97bc12b
DE
17018 print_spaces (indent, f);
17019 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17020 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17021
17022 if (die->parent != NULL)
17023 {
17024 print_spaces (indent, f);
17025 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17026 die->parent->offset.sect_off);
d97bc12b
DE
17027 }
17028
17029 print_spaces (indent, f);
17030 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17031 dwarf_bool_name (die->child != NULL));
c906108c 17032
d97bc12b
DE
17033 print_spaces (indent, f);
17034 fprintf_unfiltered (f, " attributes:\n");
17035
c906108c
SS
17036 for (i = 0; i < die->num_attrs; ++i)
17037 {
d97bc12b
DE
17038 print_spaces (indent, f);
17039 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17040 dwarf_attr_name (die->attrs[i].name),
17041 dwarf_form_name (die->attrs[i].form));
d97bc12b 17042
c906108c
SS
17043 switch (die->attrs[i].form)
17044 {
c906108c 17045 case DW_FORM_addr:
3019eac3 17046 case DW_FORM_GNU_addr_index:
d97bc12b 17047 fprintf_unfiltered (f, "address: ");
5af949e3 17048 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17049 break;
17050 case DW_FORM_block2:
17051 case DW_FORM_block4:
17052 case DW_FORM_block:
17053 case DW_FORM_block1:
56eb65bd
SP
17054 fprintf_unfiltered (f, "block: size %s",
17055 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17056 break;
2dc7f7b3 17057 case DW_FORM_exprloc:
56eb65bd
SP
17058 fprintf_unfiltered (f, "expression: size %s",
17059 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17060 break;
4568ecf9
DE
17061 case DW_FORM_ref_addr:
17062 fprintf_unfiltered (f, "ref address: ");
17063 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17064 break;
36586728
TT
17065 case DW_FORM_GNU_ref_alt:
17066 fprintf_unfiltered (f, "alt ref address: ");
17067 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17068 break;
10b3939b
DJ
17069 case DW_FORM_ref1:
17070 case DW_FORM_ref2:
17071 case DW_FORM_ref4:
4568ecf9
DE
17072 case DW_FORM_ref8:
17073 case DW_FORM_ref_udata:
d97bc12b 17074 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17075 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17076 break;
c906108c
SS
17077 case DW_FORM_data1:
17078 case DW_FORM_data2:
17079 case DW_FORM_data4:
ce5d95e1 17080 case DW_FORM_data8:
c906108c
SS
17081 case DW_FORM_udata:
17082 case DW_FORM_sdata:
43bbcdc2
PH
17083 fprintf_unfiltered (f, "constant: %s",
17084 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17085 break;
2dc7f7b3
TT
17086 case DW_FORM_sec_offset:
17087 fprintf_unfiltered (f, "section offset: %s",
17088 pulongest (DW_UNSND (&die->attrs[i])));
17089 break;
55f1336d 17090 case DW_FORM_ref_sig8:
348e048f
DE
17091 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17092 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17093 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17094 else
17095 fprintf_unfiltered (f, "signatured type, offset: unknown");
17096 break;
c906108c 17097 case DW_FORM_string:
4bdf3d34 17098 case DW_FORM_strp:
3019eac3 17099 case DW_FORM_GNU_str_index:
36586728 17100 case DW_FORM_GNU_strp_alt:
8285870a 17101 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17102 DW_STRING (&die->attrs[i])
8285870a
JK
17103 ? DW_STRING (&die->attrs[i]) : "",
17104 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17105 break;
17106 case DW_FORM_flag:
17107 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17108 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17109 else
d97bc12b 17110 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17111 break;
2dc7f7b3
TT
17112 case DW_FORM_flag_present:
17113 fprintf_unfiltered (f, "flag: TRUE");
17114 break;
a8329558 17115 case DW_FORM_indirect:
0963b4bd
MS
17116 /* The reader will have reduced the indirect form to
17117 the "base form" so this form should not occur. */
3e43a32a
MS
17118 fprintf_unfiltered (f,
17119 "unexpected attribute form: DW_FORM_indirect");
a8329558 17120 break;
c906108c 17121 default:
d97bc12b 17122 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17123 die->attrs[i].form);
d97bc12b 17124 break;
c906108c 17125 }
d97bc12b 17126 fprintf_unfiltered (f, "\n");
c906108c
SS
17127 }
17128}
17129
f9aca02d 17130static void
d97bc12b 17131dump_die_for_error (struct die_info *die)
c906108c 17132{
d97bc12b
DE
17133 dump_die_shallow (gdb_stderr, 0, die);
17134}
17135
17136static void
17137dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17138{
17139 int indent = level * 4;
17140
17141 gdb_assert (die != NULL);
17142
17143 if (level >= max_level)
17144 return;
17145
17146 dump_die_shallow (f, indent, die);
17147
17148 if (die->child != NULL)
c906108c 17149 {
d97bc12b
DE
17150 print_spaces (indent, f);
17151 fprintf_unfiltered (f, " Children:");
17152 if (level + 1 < max_level)
17153 {
17154 fprintf_unfiltered (f, "\n");
17155 dump_die_1 (f, level + 1, max_level, die->child);
17156 }
17157 else
17158 {
3e43a32a
MS
17159 fprintf_unfiltered (f,
17160 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17161 }
17162 }
17163
17164 if (die->sibling != NULL && level > 0)
17165 {
17166 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17167 }
17168}
17169
d97bc12b
DE
17170/* This is called from the pdie macro in gdbinit.in.
17171 It's not static so gcc will keep a copy callable from gdb. */
17172
17173void
17174dump_die (struct die_info *die, int max_level)
17175{
17176 dump_die_1 (gdb_stdlog, 0, max_level, die);
17177}
17178
f9aca02d 17179static void
51545339 17180store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17181{
51545339 17182 void **slot;
c906108c 17183
b64f50a1
JK
17184 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17185 INSERT);
51545339
DJ
17186
17187 *slot = die;
c906108c
SS
17188}
17189
b64f50a1
JK
17190/* DW_ADDR is always stored already as sect_offset; despite for the forms
17191 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17192
93311388
DE
17193static int
17194is_ref_attr (struct attribute *attr)
c906108c 17195{
c906108c
SS
17196 switch (attr->form)
17197 {
17198 case DW_FORM_ref_addr:
c906108c
SS
17199 case DW_FORM_ref1:
17200 case DW_FORM_ref2:
17201 case DW_FORM_ref4:
613e1657 17202 case DW_FORM_ref8:
c906108c 17203 case DW_FORM_ref_udata:
36586728 17204 case DW_FORM_GNU_ref_alt:
93311388 17205 return 1;
c906108c 17206 default:
93311388 17207 return 0;
c906108c 17208 }
93311388
DE
17209}
17210
b64f50a1
JK
17211/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17212 required kind. */
17213
17214static sect_offset
93311388
DE
17215dwarf2_get_ref_die_offset (struct attribute *attr)
17216{
4568ecf9 17217 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17218
93311388 17219 if (is_ref_attr (attr))
b64f50a1 17220 return retval;
93311388 17221
b64f50a1 17222 retval.sect_off = 0;
93311388
DE
17223 complaint (&symfile_complaints,
17224 _("unsupported die ref attribute form: '%s'"),
17225 dwarf_form_name (attr->form));
b64f50a1 17226 return retval;
c906108c
SS
17227}
17228
43bbcdc2
PH
17229/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17230 * the value held by the attribute is not constant. */
a02abb62 17231
43bbcdc2 17232static LONGEST
a02abb62
JB
17233dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17234{
17235 if (attr->form == DW_FORM_sdata)
17236 return DW_SND (attr);
17237 else if (attr->form == DW_FORM_udata
17238 || attr->form == DW_FORM_data1
17239 || attr->form == DW_FORM_data2
17240 || attr->form == DW_FORM_data4
17241 || attr->form == DW_FORM_data8)
17242 return DW_UNSND (attr);
17243 else
17244 {
3e43a32a
MS
17245 complaint (&symfile_complaints,
17246 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17247 dwarf_form_name (attr->form));
17248 return default_value;
17249 }
17250}
17251
348e048f
DE
17252/* Follow reference or signature attribute ATTR of SRC_DIE.
17253 On entry *REF_CU is the CU of SRC_DIE.
17254 On exit *REF_CU is the CU of the result. */
17255
17256static struct die_info *
17257follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17258 struct dwarf2_cu **ref_cu)
17259{
17260 struct die_info *die;
17261
17262 if (is_ref_attr (attr))
17263 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17264 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17265 die = follow_die_sig (src_die, attr, ref_cu);
17266 else
17267 {
17268 dump_die_for_error (src_die);
17269 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17270 (*ref_cu)->objfile->name);
17271 }
17272
17273 return die;
03dd20cc
DJ
17274}
17275
5c631832 17276/* Follow reference OFFSET.
673bfd45
DE
17277 On entry *REF_CU is the CU of the source die referencing OFFSET.
17278 On exit *REF_CU is the CU of the result.
17279 Returns NULL if OFFSET is invalid. */
f504f079 17280
f9aca02d 17281static struct die_info *
36586728
TT
17282follow_die_offset (sect_offset offset, int offset_in_dwz,
17283 struct dwarf2_cu **ref_cu)
c906108c 17284{
10b3939b 17285 struct die_info temp_die;
f2f0e013 17286 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17287
348e048f
DE
17288 gdb_assert (cu->per_cu != NULL);
17289
98bfdba5
PA
17290 target_cu = cu;
17291
3019eac3 17292 if (cu->per_cu->is_debug_types)
348e048f
DE
17293 {
17294 /* .debug_types CUs cannot reference anything outside their CU.
17295 If they need to, they have to reference a signatured type via
55f1336d 17296 DW_FORM_ref_sig8. */
348e048f 17297 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17298 return NULL;
348e048f 17299 }
36586728
TT
17300 else if (offset_in_dwz != cu->per_cu->is_dwz
17301 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17302 {
17303 struct dwarf2_per_cu_data *per_cu;
9a619af0 17304
36586728
TT
17305 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17306 cu->objfile);
03dd20cc
DJ
17307
17308 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17309 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17310 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17311
10b3939b
DJ
17312 target_cu = per_cu->cu;
17313 }
98bfdba5
PA
17314 else if (cu->dies == NULL)
17315 {
17316 /* We're loading full DIEs during partial symbol reading. */
17317 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17318 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17319 }
c906108c 17320
f2f0e013 17321 *ref_cu = target_cu;
51545339 17322 temp_die.offset = offset;
b64f50a1 17323 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17324}
10b3939b 17325
5c631832
JK
17326/* Follow reference attribute ATTR of SRC_DIE.
17327 On entry *REF_CU is the CU of SRC_DIE.
17328 On exit *REF_CU is the CU of the result. */
17329
17330static struct die_info *
17331follow_die_ref (struct die_info *src_die, struct attribute *attr,
17332 struct dwarf2_cu **ref_cu)
17333{
b64f50a1 17334 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17335 struct dwarf2_cu *cu = *ref_cu;
17336 struct die_info *die;
17337
36586728
TT
17338 die = follow_die_offset (offset,
17339 (attr->form == DW_FORM_GNU_ref_alt
17340 || cu->per_cu->is_dwz),
17341 ref_cu);
5c631832
JK
17342 if (!die)
17343 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17344 "at 0x%x [in module %s]"),
b64f50a1 17345 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17346
5c631832
JK
17347 return die;
17348}
17349
d83e736b
JK
17350/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17351 Returned value is intended for DW_OP_call*. Returned
17352 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17353
17354struct dwarf2_locexpr_baton
8b9737bf
TT
17355dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17356 struct dwarf2_per_cu_data *per_cu,
17357 CORE_ADDR (*get_frame_pc) (void *baton),
17358 void *baton)
5c631832 17359{
918dd910 17360 struct dwarf2_cu *cu;
5c631832
JK
17361 struct die_info *die;
17362 struct attribute *attr;
17363 struct dwarf2_locexpr_baton retval;
17364
8cf6f0b1
TT
17365 dw2_setup (per_cu->objfile);
17366
918dd910
JK
17367 if (per_cu->cu == NULL)
17368 load_cu (per_cu);
17369 cu = per_cu->cu;
17370
36586728 17371 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17372 if (!die)
17373 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17374 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17375
17376 attr = dwarf2_attr (die, DW_AT_location, cu);
17377 if (!attr)
17378 {
e103e986
JK
17379 /* DWARF: "If there is no such attribute, then there is no effect.".
17380 DATA is ignored if SIZE is 0. */
5c631832 17381
e103e986 17382 retval.data = NULL;
5c631832
JK
17383 retval.size = 0;
17384 }
8cf6f0b1
TT
17385 else if (attr_form_is_section_offset (attr))
17386 {
17387 struct dwarf2_loclist_baton loclist_baton;
17388 CORE_ADDR pc = (*get_frame_pc) (baton);
17389 size_t size;
17390
17391 fill_in_loclist_baton (cu, &loclist_baton, attr);
17392
17393 retval.data = dwarf2_find_location_expression (&loclist_baton,
17394 &size, pc);
17395 retval.size = size;
17396 }
5c631832
JK
17397 else
17398 {
17399 if (!attr_form_is_block (attr))
17400 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17401 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17402 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17403
17404 retval.data = DW_BLOCK (attr)->data;
17405 retval.size = DW_BLOCK (attr)->size;
17406 }
17407 retval.per_cu = cu->per_cu;
918dd910 17408
918dd910
JK
17409 age_cached_comp_units ();
17410
5c631832 17411 return retval;
348e048f
DE
17412}
17413
8b9737bf
TT
17414/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17415 offset. */
17416
17417struct dwarf2_locexpr_baton
17418dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17419 struct dwarf2_per_cu_data *per_cu,
17420 CORE_ADDR (*get_frame_pc) (void *baton),
17421 void *baton)
17422{
17423 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17424
17425 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17426}
17427
8a9b8146
TT
17428/* Return the type of the DIE at DIE_OFFSET in the CU named by
17429 PER_CU. */
17430
17431struct type *
b64f50a1 17432dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17433 struct dwarf2_per_cu_data *per_cu)
17434{
b64f50a1
JK
17435 sect_offset die_offset_sect;
17436
8a9b8146 17437 dw2_setup (per_cu->objfile);
b64f50a1
JK
17438
17439 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17440 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17441}
17442
348e048f
DE
17443/* Follow the signature attribute ATTR in SRC_DIE.
17444 On entry *REF_CU is the CU of SRC_DIE.
17445 On exit *REF_CU is the CU of the result. */
17446
17447static struct die_info *
17448follow_die_sig (struct die_info *src_die, struct attribute *attr,
17449 struct dwarf2_cu **ref_cu)
17450{
17451 struct objfile *objfile = (*ref_cu)->objfile;
17452 struct die_info temp_die;
17453 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17454 struct dwarf2_cu *sig_cu;
17455 struct die_info *die;
17456
17457 /* sig_type will be NULL if the signatured type is missing from
17458 the debug info. */
17459 if (sig_type == NULL)
17460 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17461 "at 0x%x [in module %s]"),
b64f50a1 17462 src_die->offset.sect_off, objfile->name);
348e048f
DE
17463
17464 /* If necessary, add it to the queue and load its DIEs. */
17465
95554aad 17466 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17467 read_signatured_type (sig_type);
348e048f
DE
17468
17469 gdb_assert (sig_type->per_cu.cu != NULL);
17470
17471 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17472 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17473 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17474 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17475 temp_die.offset.sect_off);
348e048f
DE
17476 if (die)
17477 {
17478 *ref_cu = sig_cu;
17479 return die;
17480 }
17481
3e43a32a
MS
17482 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17483 "from DIE at 0x%x [in module %s]"),
b64f50a1 17484 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17485}
17486
17487/* Given an offset of a signatured type, return its signatured_type. */
17488
17489static struct signatured_type *
8b70b953
TT
17490lookup_signatured_type_at_offset (struct objfile *objfile,
17491 struct dwarf2_section_info *section,
b64f50a1 17492 sect_offset offset)
348e048f 17493{
b64f50a1 17494 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17495 unsigned int length, initial_length_size;
17496 unsigned int sig_offset;
52dc124a 17497 struct signatured_type find_entry, *sig_type;
348e048f
DE
17498
17499 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17500 sig_offset = (initial_length_size
17501 + 2 /*version*/
17502 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17503 + 1 /*address_size*/);
17504 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17505 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17506
17507 /* This is only used to lookup previously recorded types.
17508 If we didn't find it, it's our bug. */
52dc124a
DE
17509 gdb_assert (sig_type != NULL);
17510 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17511
52dc124a 17512 return sig_type;
348e048f
DE
17513}
17514
e5fe5e75 17515/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17516
17517static void
e5fe5e75 17518load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17519{
52dc124a 17520 struct signatured_type *sig_type;
348e048f 17521
f4dc4d17
DE
17522 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17523 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17524
6721b2ec
DE
17525 /* We have the per_cu, but we need the signatured_type.
17526 Fortunately this is an easy translation. */
17527 gdb_assert (per_cu->is_debug_types);
17528 sig_type = (struct signatured_type *) per_cu;
348e048f 17529
6721b2ec 17530 gdb_assert (per_cu->cu == NULL);
348e048f 17531
52dc124a 17532 read_signatured_type (sig_type);
348e048f 17533
6721b2ec 17534 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17535}
17536
dee91e82
DE
17537/* die_reader_func for read_signatured_type.
17538 This is identical to load_full_comp_unit_reader,
17539 but is kept separate for now. */
348e048f
DE
17540
17541static void
dee91e82
DE
17542read_signatured_type_reader (const struct die_reader_specs *reader,
17543 gdb_byte *info_ptr,
17544 struct die_info *comp_unit_die,
17545 int has_children,
17546 void *data)
348e048f 17547{
dee91e82 17548 struct dwarf2_cu *cu = reader->cu;
348e048f 17549
dee91e82
DE
17550 gdb_assert (cu->die_hash == NULL);
17551 cu->die_hash =
17552 htab_create_alloc_ex (cu->header.length / 12,
17553 die_hash,
17554 die_eq,
17555 NULL,
17556 &cu->comp_unit_obstack,
17557 hashtab_obstack_allocate,
17558 dummy_obstack_deallocate);
348e048f 17559
dee91e82
DE
17560 if (has_children)
17561 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17562 &info_ptr, comp_unit_die);
17563 cu->dies = comp_unit_die;
17564 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17565
17566 /* We try not to read any attributes in this function, because not
9cdd5dbd 17567 all CUs needed for references have been loaded yet, and symbol
348e048f 17568 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17569 or we won't be able to build types correctly.
17570 Similarly, if we do not read the producer, we can not apply
17571 producer-specific interpretation. */
95554aad 17572 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17573}
348e048f 17574
3019eac3
DE
17575/* Read in a signatured type and build its CU and DIEs.
17576 If the type is a stub for the real type in a DWO file,
17577 read in the real type from the DWO file as well. */
dee91e82
DE
17578
17579static void
17580read_signatured_type (struct signatured_type *sig_type)
17581{
17582 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17583
3019eac3 17584 gdb_assert (per_cu->is_debug_types);
dee91e82 17585 gdb_assert (per_cu->cu == NULL);
348e048f 17586
f4dc4d17
DE
17587 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17588 read_signatured_type_reader, NULL);
c906108c
SS
17589}
17590
c906108c
SS
17591/* Decode simple location descriptions.
17592 Given a pointer to a dwarf block that defines a location, compute
17593 the location and return the value.
17594
4cecd739
DJ
17595 NOTE drow/2003-11-18: This function is called in two situations
17596 now: for the address of static or global variables (partial symbols
17597 only) and for offsets into structures which are expected to be
17598 (more or less) constant. The partial symbol case should go away,
17599 and only the constant case should remain. That will let this
17600 function complain more accurately. A few special modes are allowed
17601 without complaint for global variables (for instance, global
17602 register values and thread-local values).
c906108c
SS
17603
17604 A location description containing no operations indicates that the
4cecd739 17605 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17606 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17607 callers will only want a very basic result and this can become a
21ae7a4d
JK
17608 complaint.
17609
17610 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17611
17612static CORE_ADDR
e7c27a73 17613decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17614{
e7c27a73 17615 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17616 size_t i;
17617 size_t size = blk->size;
21ae7a4d
JK
17618 gdb_byte *data = blk->data;
17619 CORE_ADDR stack[64];
17620 int stacki;
17621 unsigned int bytes_read, unsnd;
17622 gdb_byte op;
c906108c 17623
21ae7a4d
JK
17624 i = 0;
17625 stacki = 0;
17626 stack[stacki] = 0;
17627 stack[++stacki] = 0;
17628
17629 while (i < size)
17630 {
17631 op = data[i++];
17632 switch (op)
17633 {
17634 case DW_OP_lit0:
17635 case DW_OP_lit1:
17636 case DW_OP_lit2:
17637 case DW_OP_lit3:
17638 case DW_OP_lit4:
17639 case DW_OP_lit5:
17640 case DW_OP_lit6:
17641 case DW_OP_lit7:
17642 case DW_OP_lit8:
17643 case DW_OP_lit9:
17644 case DW_OP_lit10:
17645 case DW_OP_lit11:
17646 case DW_OP_lit12:
17647 case DW_OP_lit13:
17648 case DW_OP_lit14:
17649 case DW_OP_lit15:
17650 case DW_OP_lit16:
17651 case DW_OP_lit17:
17652 case DW_OP_lit18:
17653 case DW_OP_lit19:
17654 case DW_OP_lit20:
17655 case DW_OP_lit21:
17656 case DW_OP_lit22:
17657 case DW_OP_lit23:
17658 case DW_OP_lit24:
17659 case DW_OP_lit25:
17660 case DW_OP_lit26:
17661 case DW_OP_lit27:
17662 case DW_OP_lit28:
17663 case DW_OP_lit29:
17664 case DW_OP_lit30:
17665 case DW_OP_lit31:
17666 stack[++stacki] = op - DW_OP_lit0;
17667 break;
f1bea926 17668
21ae7a4d
JK
17669 case DW_OP_reg0:
17670 case DW_OP_reg1:
17671 case DW_OP_reg2:
17672 case DW_OP_reg3:
17673 case DW_OP_reg4:
17674 case DW_OP_reg5:
17675 case DW_OP_reg6:
17676 case DW_OP_reg7:
17677 case DW_OP_reg8:
17678 case DW_OP_reg9:
17679 case DW_OP_reg10:
17680 case DW_OP_reg11:
17681 case DW_OP_reg12:
17682 case DW_OP_reg13:
17683 case DW_OP_reg14:
17684 case DW_OP_reg15:
17685 case DW_OP_reg16:
17686 case DW_OP_reg17:
17687 case DW_OP_reg18:
17688 case DW_OP_reg19:
17689 case DW_OP_reg20:
17690 case DW_OP_reg21:
17691 case DW_OP_reg22:
17692 case DW_OP_reg23:
17693 case DW_OP_reg24:
17694 case DW_OP_reg25:
17695 case DW_OP_reg26:
17696 case DW_OP_reg27:
17697 case DW_OP_reg28:
17698 case DW_OP_reg29:
17699 case DW_OP_reg30:
17700 case DW_OP_reg31:
17701 stack[++stacki] = op - DW_OP_reg0;
17702 if (i < size)
17703 dwarf2_complex_location_expr_complaint ();
17704 break;
c906108c 17705
21ae7a4d
JK
17706 case DW_OP_regx:
17707 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17708 i += bytes_read;
17709 stack[++stacki] = unsnd;
17710 if (i < size)
17711 dwarf2_complex_location_expr_complaint ();
17712 break;
c906108c 17713
21ae7a4d
JK
17714 case DW_OP_addr:
17715 stack[++stacki] = read_address (objfile->obfd, &data[i],
17716 cu, &bytes_read);
17717 i += bytes_read;
17718 break;
d53d4ac5 17719
21ae7a4d
JK
17720 case DW_OP_const1u:
17721 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17722 i += 1;
17723 break;
17724
17725 case DW_OP_const1s:
17726 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17727 i += 1;
17728 break;
17729
17730 case DW_OP_const2u:
17731 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17732 i += 2;
17733 break;
17734
17735 case DW_OP_const2s:
17736 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17737 i += 2;
17738 break;
d53d4ac5 17739
21ae7a4d
JK
17740 case DW_OP_const4u:
17741 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17742 i += 4;
17743 break;
17744
17745 case DW_OP_const4s:
17746 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17747 i += 4;
17748 break;
17749
585861ea
JK
17750 case DW_OP_const8u:
17751 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17752 i += 8;
17753 break;
17754
21ae7a4d
JK
17755 case DW_OP_constu:
17756 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17757 &bytes_read);
17758 i += bytes_read;
17759 break;
17760
17761 case DW_OP_consts:
17762 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17763 i += bytes_read;
17764 break;
17765
17766 case DW_OP_dup:
17767 stack[stacki + 1] = stack[stacki];
17768 stacki++;
17769 break;
17770
17771 case DW_OP_plus:
17772 stack[stacki - 1] += stack[stacki];
17773 stacki--;
17774 break;
17775
17776 case DW_OP_plus_uconst:
17777 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17778 &bytes_read);
17779 i += bytes_read;
17780 break;
17781
17782 case DW_OP_minus:
17783 stack[stacki - 1] -= stack[stacki];
17784 stacki--;
17785 break;
17786
17787 case DW_OP_deref:
17788 /* If we're not the last op, then we definitely can't encode
17789 this using GDB's address_class enum. This is valid for partial
17790 global symbols, although the variable's address will be bogus
17791 in the psymtab. */
17792 if (i < size)
17793 dwarf2_complex_location_expr_complaint ();
17794 break;
17795
17796 case DW_OP_GNU_push_tls_address:
17797 /* The top of the stack has the offset from the beginning
17798 of the thread control block at which the variable is located. */
17799 /* Nothing should follow this operator, so the top of stack would
17800 be returned. */
17801 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17802 address will be bogus in the psymtab. Make it always at least
17803 non-zero to not look as a variable garbage collected by linker
17804 which have DW_OP_addr 0. */
21ae7a4d
JK
17805 if (i < size)
17806 dwarf2_complex_location_expr_complaint ();
585861ea 17807 stack[stacki]++;
21ae7a4d
JK
17808 break;
17809
17810 case DW_OP_GNU_uninit:
17811 break;
17812
3019eac3 17813 case DW_OP_GNU_addr_index:
49f6c839 17814 case DW_OP_GNU_const_index:
3019eac3
DE
17815 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17816 &bytes_read);
17817 i += bytes_read;
17818 break;
17819
21ae7a4d
JK
17820 default:
17821 {
f39c6ffd 17822 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17823
17824 if (name)
17825 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17826 name);
17827 else
17828 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17829 op);
17830 }
17831
17832 return (stack[stacki]);
d53d4ac5 17833 }
3c6e0cb3 17834
21ae7a4d
JK
17835 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17836 outside of the allocated space. Also enforce minimum>0. */
17837 if (stacki >= ARRAY_SIZE (stack) - 1)
17838 {
17839 complaint (&symfile_complaints,
17840 _("location description stack overflow"));
17841 return 0;
17842 }
17843
17844 if (stacki <= 0)
17845 {
17846 complaint (&symfile_complaints,
17847 _("location description stack underflow"));
17848 return 0;
17849 }
17850 }
17851 return (stack[stacki]);
c906108c
SS
17852}
17853
17854/* memory allocation interface */
17855
c906108c 17856static struct dwarf_block *
7b5a2f43 17857dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17858{
17859 struct dwarf_block *blk;
17860
17861 blk = (struct dwarf_block *)
7b5a2f43 17862 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17863 return (blk);
17864}
17865
c906108c 17866static struct die_info *
b60c80d6 17867dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17868{
17869 struct die_info *die;
b60c80d6
DJ
17870 size_t size = sizeof (struct die_info);
17871
17872 if (num_attrs > 1)
17873 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17874
b60c80d6 17875 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17876 memset (die, 0, sizeof (struct die_info));
17877 return (die);
17878}
2e276125
JB
17879
17880\f
17881/* Macro support. */
17882
2e276125
JB
17883/* Return the full name of file number I in *LH's file name table.
17884 Use COMP_DIR as the name of the current directory of the
17885 compilation. The result is allocated using xmalloc; the caller is
17886 responsible for freeing it. */
17887static char *
17888file_full_name (int file, struct line_header *lh, const char *comp_dir)
17889{
6a83a1e6
EZ
17890 /* Is the file number a valid index into the line header's file name
17891 table? Remember that file numbers start with one, not zero. */
17892 if (1 <= file && file <= lh->num_file_names)
17893 {
17894 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17895
6a83a1e6
EZ
17896 if (IS_ABSOLUTE_PATH (fe->name))
17897 return xstrdup (fe->name);
17898 else
17899 {
17900 const char *dir;
17901 int dir_len;
17902 char *full_name;
17903
17904 if (fe->dir_index)
17905 dir = lh->include_dirs[fe->dir_index - 1];
17906 else
17907 dir = comp_dir;
17908
17909 if (dir)
17910 {
17911 dir_len = strlen (dir);
17912 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17913 strcpy (full_name, dir);
17914 full_name[dir_len] = '/';
17915 strcpy (full_name + dir_len + 1, fe->name);
17916 return full_name;
17917 }
17918 else
17919 return xstrdup (fe->name);
17920 }
17921 }
2e276125
JB
17922 else
17923 {
6a83a1e6
EZ
17924 /* The compiler produced a bogus file number. We can at least
17925 record the macro definitions made in the file, even if we
17926 won't be able to find the file by name. */
17927 char fake_name[80];
9a619af0 17928
8c042590
PM
17929 xsnprintf (fake_name, sizeof (fake_name),
17930 "<bad macro file number %d>", file);
2e276125 17931
6e70227d 17932 complaint (&symfile_complaints,
6a83a1e6
EZ
17933 _("bad file number in macro information (%d)"),
17934 file);
2e276125 17935
6a83a1e6 17936 return xstrdup (fake_name);
2e276125
JB
17937 }
17938}
17939
17940
17941static struct macro_source_file *
17942macro_start_file (int file, int line,
17943 struct macro_source_file *current_file,
17944 const char *comp_dir,
17945 struct line_header *lh, struct objfile *objfile)
17946{
17947 /* The full name of this source file. */
17948 char *full_name = file_full_name (file, lh, comp_dir);
17949
17950 /* We don't create a macro table for this compilation unit
17951 at all until we actually get a filename. */
17952 if (! pending_macros)
6532ff36
TT
17953 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17954 objfile->per_bfd->macro_cache);
2e276125
JB
17955
17956 if (! current_file)
abc9d0dc
TT
17957 {
17958 /* If we have no current file, then this must be the start_file
17959 directive for the compilation unit's main source file. */
17960 current_file = macro_set_main (pending_macros, full_name);
17961 macro_define_special (pending_macros);
17962 }
2e276125
JB
17963 else
17964 current_file = macro_include (current_file, line, full_name);
17965
17966 xfree (full_name);
6e70227d 17967
2e276125
JB
17968 return current_file;
17969}
17970
17971
17972/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17973 followed by a null byte. */
17974static char *
17975copy_string (const char *buf, int len)
17976{
17977 char *s = xmalloc (len + 1);
9a619af0 17978
2e276125
JB
17979 memcpy (s, buf, len);
17980 s[len] = '\0';
2e276125
JB
17981 return s;
17982}
17983
17984
17985static const char *
17986consume_improper_spaces (const char *p, const char *body)
17987{
17988 if (*p == ' ')
17989 {
4d3c2250 17990 complaint (&symfile_complaints,
3e43a32a
MS
17991 _("macro definition contains spaces "
17992 "in formal argument list:\n`%s'"),
4d3c2250 17993 body);
2e276125
JB
17994
17995 while (*p == ' ')
17996 p++;
17997 }
17998
17999 return p;
18000}
18001
18002
18003static void
18004parse_macro_definition (struct macro_source_file *file, int line,
18005 const char *body)
18006{
18007 const char *p;
18008
18009 /* The body string takes one of two forms. For object-like macro
18010 definitions, it should be:
18011
18012 <macro name> " " <definition>
18013
18014 For function-like macro definitions, it should be:
18015
18016 <macro name> "() " <definition>
18017 or
18018 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18019
18020 Spaces may appear only where explicitly indicated, and in the
18021 <definition>.
18022
18023 The Dwarf 2 spec says that an object-like macro's name is always
18024 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18025 the space when the macro's definition is the empty string.
2e276125
JB
18026
18027 The Dwarf 2 spec says that there should be no spaces between the
18028 formal arguments in a function-like macro's formal argument list,
18029 but versions of GCC around March 2002 include spaces after the
18030 commas. */
18031
18032
18033 /* Find the extent of the macro name. The macro name is terminated
18034 by either a space or null character (for an object-like macro) or
18035 an opening paren (for a function-like macro). */
18036 for (p = body; *p; p++)
18037 if (*p == ' ' || *p == '(')
18038 break;
18039
18040 if (*p == ' ' || *p == '\0')
18041 {
18042 /* It's an object-like macro. */
18043 int name_len = p - body;
18044 char *name = copy_string (body, name_len);
18045 const char *replacement;
18046
18047 if (*p == ' ')
18048 replacement = body + name_len + 1;
18049 else
18050 {
4d3c2250 18051 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18052 replacement = body + name_len;
18053 }
6e70227d 18054
2e276125
JB
18055 macro_define_object (file, line, name, replacement);
18056
18057 xfree (name);
18058 }
18059 else if (*p == '(')
18060 {
18061 /* It's a function-like macro. */
18062 char *name = copy_string (body, p - body);
18063 int argc = 0;
18064 int argv_size = 1;
18065 char **argv = xmalloc (argv_size * sizeof (*argv));
18066
18067 p++;
18068
18069 p = consume_improper_spaces (p, body);
18070
18071 /* Parse the formal argument list. */
18072 while (*p && *p != ')')
18073 {
18074 /* Find the extent of the current argument name. */
18075 const char *arg_start = p;
18076
18077 while (*p && *p != ',' && *p != ')' && *p != ' ')
18078 p++;
18079
18080 if (! *p || p == arg_start)
4d3c2250 18081 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18082 else
18083 {
18084 /* Make sure argv has room for the new argument. */
18085 if (argc >= argv_size)
18086 {
18087 argv_size *= 2;
18088 argv = xrealloc (argv, argv_size * sizeof (*argv));
18089 }
18090
18091 argv[argc++] = copy_string (arg_start, p - arg_start);
18092 }
18093
18094 p = consume_improper_spaces (p, body);
18095
18096 /* Consume the comma, if present. */
18097 if (*p == ',')
18098 {
18099 p++;
18100
18101 p = consume_improper_spaces (p, body);
18102 }
18103 }
18104
18105 if (*p == ')')
18106 {
18107 p++;
18108
18109 if (*p == ' ')
18110 /* Perfectly formed definition, no complaints. */
18111 macro_define_function (file, line, name,
6e70227d 18112 argc, (const char **) argv,
2e276125
JB
18113 p + 1);
18114 else if (*p == '\0')
18115 {
18116 /* Complain, but do define it. */
4d3c2250 18117 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18118 macro_define_function (file, line, name,
6e70227d 18119 argc, (const char **) argv,
2e276125
JB
18120 p);
18121 }
18122 else
18123 /* Just complain. */
4d3c2250 18124 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18125 }
18126 else
18127 /* Just complain. */
4d3c2250 18128 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18129
18130 xfree (name);
18131 {
18132 int i;
18133
18134 for (i = 0; i < argc; i++)
18135 xfree (argv[i]);
18136 }
18137 xfree (argv);
18138 }
18139 else
4d3c2250 18140 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18141}
18142
cf2c3c16
TT
18143/* Skip some bytes from BYTES according to the form given in FORM.
18144 Returns the new pointer. */
2e276125 18145
cf2c3c16 18146static gdb_byte *
f664829e 18147skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18148 enum dwarf_form form,
18149 unsigned int offset_size,
18150 struct dwarf2_section_info *section)
2e276125 18151{
cf2c3c16 18152 unsigned int bytes_read;
2e276125 18153
cf2c3c16 18154 switch (form)
2e276125 18155 {
cf2c3c16
TT
18156 case DW_FORM_data1:
18157 case DW_FORM_flag:
18158 ++bytes;
18159 break;
18160
18161 case DW_FORM_data2:
18162 bytes += 2;
18163 break;
18164
18165 case DW_FORM_data4:
18166 bytes += 4;
18167 break;
18168
18169 case DW_FORM_data8:
18170 bytes += 8;
18171 break;
18172
18173 case DW_FORM_string:
18174 read_direct_string (abfd, bytes, &bytes_read);
18175 bytes += bytes_read;
18176 break;
18177
18178 case DW_FORM_sec_offset:
18179 case DW_FORM_strp:
36586728 18180 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18181 bytes += offset_size;
18182 break;
18183
18184 case DW_FORM_block:
18185 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18186 bytes += bytes_read;
18187 break;
18188
18189 case DW_FORM_block1:
18190 bytes += 1 + read_1_byte (abfd, bytes);
18191 break;
18192 case DW_FORM_block2:
18193 bytes += 2 + read_2_bytes (abfd, bytes);
18194 break;
18195 case DW_FORM_block4:
18196 bytes += 4 + read_4_bytes (abfd, bytes);
18197 break;
18198
18199 case DW_FORM_sdata:
18200 case DW_FORM_udata:
3019eac3
DE
18201 case DW_FORM_GNU_addr_index:
18202 case DW_FORM_GNU_str_index:
f664829e
DE
18203 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18204 if (bytes == NULL)
18205 {
18206 dwarf2_section_buffer_overflow_complaint (section);
18207 return NULL;
18208 }
cf2c3c16
TT
18209 break;
18210
18211 default:
18212 {
18213 complain:
18214 complaint (&symfile_complaints,
18215 _("invalid form 0x%x in `%s'"),
18216 form,
18217 section->asection->name);
18218 return NULL;
18219 }
2e276125
JB
18220 }
18221
cf2c3c16
TT
18222 return bytes;
18223}
757a13d0 18224
cf2c3c16
TT
18225/* A helper for dwarf_decode_macros that handles skipping an unknown
18226 opcode. Returns an updated pointer to the macro data buffer; or,
18227 on error, issues a complaint and returns NULL. */
757a13d0 18228
cf2c3c16
TT
18229static gdb_byte *
18230skip_unknown_opcode (unsigned int opcode,
18231 gdb_byte **opcode_definitions,
f664829e 18232 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18233 bfd *abfd,
18234 unsigned int offset_size,
18235 struct dwarf2_section_info *section)
18236{
18237 unsigned int bytes_read, i;
18238 unsigned long arg;
18239 gdb_byte *defn;
2e276125 18240
cf2c3c16 18241 if (opcode_definitions[opcode] == NULL)
2e276125 18242 {
cf2c3c16
TT
18243 complaint (&symfile_complaints,
18244 _("unrecognized DW_MACFINO opcode 0x%x"),
18245 opcode);
18246 return NULL;
18247 }
2e276125 18248
cf2c3c16
TT
18249 defn = opcode_definitions[opcode];
18250 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18251 defn += bytes_read;
2e276125 18252
cf2c3c16
TT
18253 for (i = 0; i < arg; ++i)
18254 {
f664829e
DE
18255 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18256 section);
cf2c3c16
TT
18257 if (mac_ptr == NULL)
18258 {
18259 /* skip_form_bytes already issued the complaint. */
18260 return NULL;
18261 }
18262 }
757a13d0 18263
cf2c3c16
TT
18264 return mac_ptr;
18265}
757a13d0 18266
cf2c3c16
TT
18267/* A helper function which parses the header of a macro section.
18268 If the macro section is the extended (for now called "GNU") type,
18269 then this updates *OFFSET_SIZE. Returns a pointer to just after
18270 the header, or issues a complaint and returns NULL on error. */
757a13d0 18271
cf2c3c16
TT
18272static gdb_byte *
18273dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18274 bfd *abfd,
18275 gdb_byte *mac_ptr,
18276 unsigned int *offset_size,
18277 int section_is_gnu)
18278{
18279 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18280
cf2c3c16
TT
18281 if (section_is_gnu)
18282 {
18283 unsigned int version, flags;
757a13d0 18284
cf2c3c16
TT
18285 version = read_2_bytes (abfd, mac_ptr);
18286 if (version != 4)
18287 {
18288 complaint (&symfile_complaints,
18289 _("unrecognized version `%d' in .debug_macro section"),
18290 version);
18291 return NULL;
18292 }
18293 mac_ptr += 2;
757a13d0 18294
cf2c3c16
TT
18295 flags = read_1_byte (abfd, mac_ptr);
18296 ++mac_ptr;
18297 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18298
cf2c3c16
TT
18299 if ((flags & 2) != 0)
18300 /* We don't need the line table offset. */
18301 mac_ptr += *offset_size;
757a13d0 18302
cf2c3c16
TT
18303 /* Vendor opcode descriptions. */
18304 if ((flags & 4) != 0)
18305 {
18306 unsigned int i, count;
757a13d0 18307
cf2c3c16
TT
18308 count = read_1_byte (abfd, mac_ptr);
18309 ++mac_ptr;
18310 for (i = 0; i < count; ++i)
18311 {
18312 unsigned int opcode, bytes_read;
18313 unsigned long arg;
18314
18315 opcode = read_1_byte (abfd, mac_ptr);
18316 ++mac_ptr;
18317 opcode_definitions[opcode] = mac_ptr;
18318 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18319 mac_ptr += bytes_read;
18320 mac_ptr += arg;
18321 }
757a13d0 18322 }
cf2c3c16 18323 }
757a13d0 18324
cf2c3c16
TT
18325 return mac_ptr;
18326}
757a13d0 18327
cf2c3c16 18328/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18329 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18330
18331static void
18332dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18333 struct macro_source_file *current_file,
18334 struct line_header *lh, char *comp_dir,
18335 struct dwarf2_section_info *section,
36586728 18336 int section_is_gnu, int section_is_dwz,
cf2c3c16 18337 unsigned int offset_size,
8fc3fc34
TT
18338 struct objfile *objfile,
18339 htab_t include_hash)
cf2c3c16
TT
18340{
18341 enum dwarf_macro_record_type macinfo_type;
18342 int at_commandline;
18343 gdb_byte *opcode_definitions[256];
757a13d0 18344
cf2c3c16
TT
18345 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18346 &offset_size, section_is_gnu);
18347 if (mac_ptr == NULL)
18348 {
18349 /* We already issued a complaint. */
18350 return;
18351 }
757a13d0
JK
18352
18353 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18354 GDB is still reading the definitions from command line. First
18355 DW_MACINFO_start_file will need to be ignored as it was already executed
18356 to create CURRENT_FILE for the main source holding also the command line
18357 definitions. On first met DW_MACINFO_start_file this flag is reset to
18358 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18359
18360 at_commandline = 1;
18361
18362 do
18363 {
18364 /* Do we at least have room for a macinfo type byte? */
18365 if (mac_ptr >= mac_end)
18366 {
f664829e 18367 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18368 break;
18369 }
18370
18371 macinfo_type = read_1_byte (abfd, mac_ptr);
18372 mac_ptr++;
18373
cf2c3c16
TT
18374 /* Note that we rely on the fact that the corresponding GNU and
18375 DWARF constants are the same. */
757a13d0
JK
18376 switch (macinfo_type)
18377 {
18378 /* A zero macinfo type indicates the end of the macro
18379 information. */
18380 case 0:
18381 break;
2e276125 18382
cf2c3c16
TT
18383 case DW_MACRO_GNU_define:
18384 case DW_MACRO_GNU_undef:
18385 case DW_MACRO_GNU_define_indirect:
18386 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18387 case DW_MACRO_GNU_define_indirect_alt:
18388 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18389 {
891d2f0b 18390 unsigned int bytes_read;
2e276125
JB
18391 int line;
18392 char *body;
cf2c3c16 18393 int is_define;
2e276125 18394
cf2c3c16
TT
18395 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18396 mac_ptr += bytes_read;
18397
18398 if (macinfo_type == DW_MACRO_GNU_define
18399 || macinfo_type == DW_MACRO_GNU_undef)
18400 {
18401 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18402 mac_ptr += bytes_read;
18403 }
18404 else
18405 {
18406 LONGEST str_offset;
18407
18408 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18409 mac_ptr += offset_size;
2e276125 18410
36586728 18411 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18412 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18413 || section_is_dwz)
36586728
TT
18414 {
18415 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18416
18417 body = read_indirect_string_from_dwz (dwz, str_offset);
18418 }
18419 else
18420 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18421 }
18422
18423 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18424 || macinfo_type == DW_MACRO_GNU_define_indirect
18425 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18426 if (! current_file)
757a13d0
JK
18427 {
18428 /* DWARF violation as no main source is present. */
18429 complaint (&symfile_complaints,
18430 _("debug info with no main source gives macro %s "
18431 "on line %d: %s"),
cf2c3c16
TT
18432 is_define ? _("definition") : _("undefinition"),
18433 line, body);
757a13d0
JK
18434 break;
18435 }
3e43a32a
MS
18436 if ((line == 0 && !at_commandline)
18437 || (line != 0 && at_commandline))
4d3c2250 18438 complaint (&symfile_complaints,
757a13d0
JK
18439 _("debug info gives %s macro %s with %s line %d: %s"),
18440 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18441 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18442 line == 0 ? _("zero") : _("non-zero"), line, body);
18443
cf2c3c16 18444 if (is_define)
757a13d0 18445 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18446 else
18447 {
18448 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18449 || macinfo_type == DW_MACRO_GNU_undef_indirect
18450 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18451 macro_undef (current_file, line, body);
18452 }
2e276125
JB
18453 }
18454 break;
18455
cf2c3c16 18456 case DW_MACRO_GNU_start_file:
2e276125 18457 {
891d2f0b 18458 unsigned int bytes_read;
2e276125
JB
18459 int line, file;
18460
18461 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18462 mac_ptr += bytes_read;
18463 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18464 mac_ptr += bytes_read;
18465
3e43a32a
MS
18466 if ((line == 0 && !at_commandline)
18467 || (line != 0 && at_commandline))
757a13d0
JK
18468 complaint (&symfile_complaints,
18469 _("debug info gives source %d included "
18470 "from %s at %s line %d"),
18471 file, at_commandline ? _("command-line") : _("file"),
18472 line == 0 ? _("zero") : _("non-zero"), line);
18473
18474 if (at_commandline)
18475 {
cf2c3c16
TT
18476 /* This DW_MACRO_GNU_start_file was executed in the
18477 pass one. */
757a13d0
JK
18478 at_commandline = 0;
18479 }
18480 else
18481 current_file = macro_start_file (file, line,
18482 current_file, comp_dir,
cf2c3c16 18483 lh, objfile);
2e276125
JB
18484 }
18485 break;
18486
cf2c3c16 18487 case DW_MACRO_GNU_end_file:
2e276125 18488 if (! current_file)
4d3c2250 18489 complaint (&symfile_complaints,
3e43a32a
MS
18490 _("macro debug info has an unmatched "
18491 "`close_file' directive"));
2e276125
JB
18492 else
18493 {
18494 current_file = current_file->included_by;
18495 if (! current_file)
18496 {
cf2c3c16 18497 enum dwarf_macro_record_type next_type;
2e276125
JB
18498
18499 /* GCC circa March 2002 doesn't produce the zero
18500 type byte marking the end of the compilation
18501 unit. Complain if it's not there, but exit no
18502 matter what. */
18503
18504 /* Do we at least have room for a macinfo type byte? */
18505 if (mac_ptr >= mac_end)
18506 {
f664829e 18507 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18508 return;
18509 }
18510
18511 /* We don't increment mac_ptr here, so this is just
18512 a look-ahead. */
18513 next_type = read_1_byte (abfd, mac_ptr);
18514 if (next_type != 0)
4d3c2250 18515 complaint (&symfile_complaints,
3e43a32a
MS
18516 _("no terminating 0-type entry for "
18517 "macros in `.debug_macinfo' section"));
2e276125
JB
18518
18519 return;
18520 }
18521 }
18522 break;
18523
cf2c3c16 18524 case DW_MACRO_GNU_transparent_include:
36586728 18525 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18526 {
18527 LONGEST offset;
8fc3fc34 18528 void **slot;
a036ba48
TT
18529 bfd *include_bfd = abfd;
18530 struct dwarf2_section_info *include_section = section;
18531 struct dwarf2_section_info alt_section;
18532 gdb_byte *include_mac_end = mac_end;
18533 int is_dwz = section_is_dwz;
18534 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18535
18536 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18537 mac_ptr += offset_size;
18538
a036ba48
TT
18539 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18540 {
18541 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18542
18543 dwarf2_read_section (dwarf2_per_objfile->objfile,
18544 &dwz->macro);
18545
18546 include_bfd = dwz->macro.asection->owner;
18547 include_section = &dwz->macro;
18548 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18549 is_dwz = 1;
18550 }
18551
18552 new_mac_ptr = include_section->buffer + offset;
18553 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18554
8fc3fc34
TT
18555 if (*slot != NULL)
18556 {
18557 /* This has actually happened; see
18558 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18559 complaint (&symfile_complaints,
18560 _("recursive DW_MACRO_GNU_transparent_include in "
18561 ".debug_macro section"));
18562 }
18563 else
18564 {
a036ba48 18565 *slot = new_mac_ptr;
36586728 18566
a036ba48 18567 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18568 include_mac_end, current_file,
8fc3fc34 18569 lh, comp_dir,
36586728 18570 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18571 offset_size, objfile, include_hash);
18572
a036ba48 18573 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18574 }
cf2c3c16
TT
18575 }
18576 break;
18577
2e276125 18578 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18579 if (!section_is_gnu)
18580 {
18581 unsigned int bytes_read;
18582 int constant;
2e276125 18583
cf2c3c16
TT
18584 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18585 mac_ptr += bytes_read;
18586 read_direct_string (abfd, mac_ptr, &bytes_read);
18587 mac_ptr += bytes_read;
2e276125 18588
cf2c3c16
TT
18589 /* We don't recognize any vendor extensions. */
18590 break;
18591 }
18592 /* FALLTHROUGH */
18593
18594 default:
18595 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18596 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18597 section);
18598 if (mac_ptr == NULL)
18599 return;
18600 break;
2e276125 18601 }
757a13d0 18602 } while (macinfo_type != 0);
2e276125 18603}
8e19ed76 18604
cf2c3c16 18605static void
09262596
DE
18606dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18607 char *comp_dir, int section_is_gnu)
cf2c3c16 18608{
bb5ed363 18609 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18610 struct line_header *lh = cu->line_header;
18611 bfd *abfd;
cf2c3c16
TT
18612 gdb_byte *mac_ptr, *mac_end;
18613 struct macro_source_file *current_file = 0;
18614 enum dwarf_macro_record_type macinfo_type;
18615 unsigned int offset_size = cu->header.offset_size;
18616 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18617 struct cleanup *cleanup;
18618 htab_t include_hash;
18619 void **slot;
09262596
DE
18620 struct dwarf2_section_info *section;
18621 const char *section_name;
18622
18623 if (cu->dwo_unit != NULL)
18624 {
18625 if (section_is_gnu)
18626 {
18627 section = &cu->dwo_unit->dwo_file->sections.macro;
18628 section_name = ".debug_macro.dwo";
18629 }
18630 else
18631 {
18632 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18633 section_name = ".debug_macinfo.dwo";
18634 }
18635 }
18636 else
18637 {
18638 if (section_is_gnu)
18639 {
18640 section = &dwarf2_per_objfile->macro;
18641 section_name = ".debug_macro";
18642 }
18643 else
18644 {
18645 section = &dwarf2_per_objfile->macinfo;
18646 section_name = ".debug_macinfo";
18647 }
18648 }
cf2c3c16 18649
bb5ed363 18650 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18651 if (section->buffer == NULL)
18652 {
fceca515 18653 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18654 return;
18655 }
09262596 18656 abfd = section->asection->owner;
cf2c3c16
TT
18657
18658 /* First pass: Find the name of the base filename.
18659 This filename is needed in order to process all macros whose definition
18660 (or undefinition) comes from the command line. These macros are defined
18661 before the first DW_MACINFO_start_file entry, and yet still need to be
18662 associated to the base file.
18663
18664 To determine the base file name, we scan the macro definitions until we
18665 reach the first DW_MACINFO_start_file entry. We then initialize
18666 CURRENT_FILE accordingly so that any macro definition found before the
18667 first DW_MACINFO_start_file can still be associated to the base file. */
18668
18669 mac_ptr = section->buffer + offset;
18670 mac_end = section->buffer + section->size;
18671
18672 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18673 &offset_size, section_is_gnu);
18674 if (mac_ptr == NULL)
18675 {
18676 /* We already issued a complaint. */
18677 return;
18678 }
18679
18680 do
18681 {
18682 /* Do we at least have room for a macinfo type byte? */
18683 if (mac_ptr >= mac_end)
18684 {
18685 /* Complaint is printed during the second pass as GDB will probably
18686 stop the first pass earlier upon finding
18687 DW_MACINFO_start_file. */
18688 break;
18689 }
18690
18691 macinfo_type = read_1_byte (abfd, mac_ptr);
18692 mac_ptr++;
18693
18694 /* Note that we rely on the fact that the corresponding GNU and
18695 DWARF constants are the same. */
18696 switch (macinfo_type)
18697 {
18698 /* A zero macinfo type indicates the end of the macro
18699 information. */
18700 case 0:
18701 break;
18702
18703 case DW_MACRO_GNU_define:
18704 case DW_MACRO_GNU_undef:
18705 /* Only skip the data by MAC_PTR. */
18706 {
18707 unsigned int bytes_read;
18708
18709 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18710 mac_ptr += bytes_read;
18711 read_direct_string (abfd, mac_ptr, &bytes_read);
18712 mac_ptr += bytes_read;
18713 }
18714 break;
18715
18716 case DW_MACRO_GNU_start_file:
18717 {
18718 unsigned int bytes_read;
18719 int line, file;
18720
18721 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18722 mac_ptr += bytes_read;
18723 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18724 mac_ptr += bytes_read;
18725
18726 current_file = macro_start_file (file, line, current_file,
bb5ed363 18727 comp_dir, lh, objfile);
cf2c3c16
TT
18728 }
18729 break;
18730
18731 case DW_MACRO_GNU_end_file:
18732 /* No data to skip by MAC_PTR. */
18733 break;
18734
18735 case DW_MACRO_GNU_define_indirect:
18736 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18737 case DW_MACRO_GNU_define_indirect_alt:
18738 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18739 {
18740 unsigned int bytes_read;
18741
18742 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18743 mac_ptr += bytes_read;
18744 mac_ptr += offset_size;
18745 }
18746 break;
18747
18748 case DW_MACRO_GNU_transparent_include:
f7a35f02 18749 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18750 /* Note that, according to the spec, a transparent include
18751 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18752 skip this opcode. */
18753 mac_ptr += offset_size;
18754 break;
18755
18756 case DW_MACINFO_vendor_ext:
18757 /* Only skip the data by MAC_PTR. */
18758 if (!section_is_gnu)
18759 {
18760 unsigned int bytes_read;
18761
18762 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18763 mac_ptr += bytes_read;
18764 read_direct_string (abfd, mac_ptr, &bytes_read);
18765 mac_ptr += bytes_read;
18766 }
18767 /* FALLTHROUGH */
18768
18769 default:
18770 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18771 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18772 section);
18773 if (mac_ptr == NULL)
18774 return;
18775 break;
18776 }
18777 } while (macinfo_type != 0 && current_file == NULL);
18778
18779 /* Second pass: Process all entries.
18780
18781 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18782 command-line macro definitions/undefinitions. This flag is unset when we
18783 reach the first DW_MACINFO_start_file entry. */
18784
8fc3fc34
TT
18785 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18786 NULL, xcalloc, xfree);
18787 cleanup = make_cleanup_htab_delete (include_hash);
18788 mac_ptr = section->buffer + offset;
18789 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18790 *slot = mac_ptr;
18791 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18792 current_file, lh, comp_dir, section,
18793 section_is_gnu, 0,
8fc3fc34
TT
18794 offset_size, objfile, include_hash);
18795 do_cleanups (cleanup);
cf2c3c16
TT
18796}
18797
8e19ed76 18798/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18799 if so return true else false. */
380bca97 18800
8e19ed76
PS
18801static int
18802attr_form_is_block (struct attribute *attr)
18803{
18804 return (attr == NULL ? 0 :
18805 attr->form == DW_FORM_block1
18806 || attr->form == DW_FORM_block2
18807 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18808 || attr->form == DW_FORM_block
18809 || attr->form == DW_FORM_exprloc);
8e19ed76 18810}
4c2df51b 18811
c6a0999f
JB
18812/* Return non-zero if ATTR's value is a section offset --- classes
18813 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18814 You may use DW_UNSND (attr) to retrieve such offsets.
18815
18816 Section 7.5.4, "Attribute Encodings", explains that no attribute
18817 may have a value that belongs to more than one of these classes; it
18818 would be ambiguous if we did, because we use the same forms for all
18819 of them. */
380bca97 18820
3690dd37
JB
18821static int
18822attr_form_is_section_offset (struct attribute *attr)
18823{
18824 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18825 || attr->form == DW_FORM_data8
18826 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18827}
18828
3690dd37
JB
18829/* Return non-zero if ATTR's value falls in the 'constant' class, or
18830 zero otherwise. When this function returns true, you can apply
18831 dwarf2_get_attr_constant_value to it.
18832
18833 However, note that for some attributes you must check
18834 attr_form_is_section_offset before using this test. DW_FORM_data4
18835 and DW_FORM_data8 are members of both the constant class, and of
18836 the classes that contain offsets into other debug sections
18837 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18838 that, if an attribute's can be either a constant or one of the
18839 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18840 taken as section offsets, not constants. */
380bca97 18841
3690dd37
JB
18842static int
18843attr_form_is_constant (struct attribute *attr)
18844{
18845 switch (attr->form)
18846 {
18847 case DW_FORM_sdata:
18848 case DW_FORM_udata:
18849 case DW_FORM_data1:
18850 case DW_FORM_data2:
18851 case DW_FORM_data4:
18852 case DW_FORM_data8:
18853 return 1;
18854 default:
18855 return 0;
18856 }
18857}
18858
3019eac3
DE
18859/* Return the .debug_loc section to use for CU.
18860 For DWO files use .debug_loc.dwo. */
18861
18862static struct dwarf2_section_info *
18863cu_debug_loc_section (struct dwarf2_cu *cu)
18864{
18865 if (cu->dwo_unit)
18866 return &cu->dwo_unit->dwo_file->sections.loc;
18867 return &dwarf2_per_objfile->loc;
18868}
18869
8cf6f0b1
TT
18870/* A helper function that fills in a dwarf2_loclist_baton. */
18871
18872static void
18873fill_in_loclist_baton (struct dwarf2_cu *cu,
18874 struct dwarf2_loclist_baton *baton,
18875 struct attribute *attr)
18876{
3019eac3
DE
18877 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18878
18879 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18880
18881 baton->per_cu = cu->per_cu;
18882 gdb_assert (baton->per_cu);
18883 /* We don't know how long the location list is, but make sure we
18884 don't run off the edge of the section. */
3019eac3
DE
18885 baton->size = section->size - DW_UNSND (attr);
18886 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18887 baton->base_address = cu->base_address;
f664829e 18888 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18889}
18890
4c2df51b
DJ
18891static void
18892dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18893 struct dwarf2_cu *cu)
4c2df51b 18894{
bb5ed363 18895 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18896 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18897
3690dd37 18898 if (attr_form_is_section_offset (attr)
3019eac3 18899 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18900 the section. If so, fall through to the complaint in the
18901 other branch. */
3019eac3 18902 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18903 {
0d53c4c4 18904 struct dwarf2_loclist_baton *baton;
4c2df51b 18905
bb5ed363 18906 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18907 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18908
8cf6f0b1 18909 fill_in_loclist_baton (cu, baton, attr);
be391dca 18910
d00adf39 18911 if (cu->base_known == 0)
0d53c4c4 18912 complaint (&symfile_complaints,
3e43a32a
MS
18913 _("Location list used without "
18914 "specifying the CU base address."));
4c2df51b 18915
768a979c 18916 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18917 SYMBOL_LOCATION_BATON (sym) = baton;
18918 }
18919 else
18920 {
18921 struct dwarf2_locexpr_baton *baton;
18922
bb5ed363 18923 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18924 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18925 baton->per_cu = cu->per_cu;
18926 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18927
18928 if (attr_form_is_block (attr))
18929 {
18930 /* Note that we're just copying the block's data pointer
18931 here, not the actual data. We're still pointing into the
6502dd73
DJ
18932 info_buffer for SYM's objfile; right now we never release
18933 that buffer, but when we do clean up properly this may
18934 need to change. */
0d53c4c4
DJ
18935 baton->size = DW_BLOCK (attr)->size;
18936 baton->data = DW_BLOCK (attr)->data;
18937 }
18938 else
18939 {
18940 dwarf2_invalid_attrib_class_complaint ("location description",
18941 SYMBOL_NATURAL_NAME (sym));
18942 baton->size = 0;
0d53c4c4 18943 }
6e70227d 18944
768a979c 18945 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18946 SYMBOL_LOCATION_BATON (sym) = baton;
18947 }
4c2df51b 18948}
6502dd73 18949
9aa1f1e3
TT
18950/* Return the OBJFILE associated with the compilation unit CU. If CU
18951 came from a separate debuginfo file, then the master objfile is
18952 returned. */
ae0d2f24
UW
18953
18954struct objfile *
18955dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18956{
9291a0cd 18957 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18958
18959 /* Return the master objfile, so that we can report and look up the
18960 correct file containing this variable. */
18961 if (objfile->separate_debug_objfile_backlink)
18962 objfile = objfile->separate_debug_objfile_backlink;
18963
18964 return objfile;
18965}
18966
96408a79
SA
18967/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18968 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18969 CU_HEADERP first. */
18970
18971static const struct comp_unit_head *
18972per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18973 struct dwarf2_per_cu_data *per_cu)
18974{
96408a79
SA
18975 gdb_byte *info_ptr;
18976
18977 if (per_cu->cu)
18978 return &per_cu->cu->header;
18979
0bc3a05c 18980 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
18981
18982 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 18983 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
18984
18985 return cu_headerp;
18986}
18987
ae0d2f24
UW
18988/* Return the address size given in the compilation unit header for CU. */
18989
98714339 18990int
ae0d2f24
UW
18991dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18992{
96408a79
SA
18993 struct comp_unit_head cu_header_local;
18994 const struct comp_unit_head *cu_headerp;
c471e790 18995
96408a79
SA
18996 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18997
18998 return cu_headerp->addr_size;
ae0d2f24
UW
18999}
19000
9eae7c52
TT
19001/* Return the offset size given in the compilation unit header for CU. */
19002
19003int
19004dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19005{
96408a79
SA
19006 struct comp_unit_head cu_header_local;
19007 const struct comp_unit_head *cu_headerp;
9c6c53f7 19008
96408a79
SA
19009 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19010
19011 return cu_headerp->offset_size;
19012}
19013
19014/* See its dwarf2loc.h declaration. */
19015
19016int
19017dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19018{
19019 struct comp_unit_head cu_header_local;
19020 const struct comp_unit_head *cu_headerp;
19021
19022 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19023
19024 if (cu_headerp->version == 2)
19025 return cu_headerp->addr_size;
19026 else
19027 return cu_headerp->offset_size;
181cebd4
JK
19028}
19029
9aa1f1e3
TT
19030/* Return the text offset of the CU. The returned offset comes from
19031 this CU's objfile. If this objfile came from a separate debuginfo
19032 file, then the offset may be different from the corresponding
19033 offset in the parent objfile. */
19034
19035CORE_ADDR
19036dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19037{
bb3fa9d0 19038 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19039
19040 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19041}
19042
348e048f
DE
19043/* Locate the .debug_info compilation unit from CU's objfile which contains
19044 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19045
19046static struct dwarf2_per_cu_data *
b64f50a1 19047dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19048 unsigned int offset_in_dwz,
ae038cb0
DJ
19049 struct objfile *objfile)
19050{
19051 struct dwarf2_per_cu_data *this_cu;
19052 int low, high;
36586728 19053 const sect_offset *cu_off;
ae038cb0 19054
ae038cb0
DJ
19055 low = 0;
19056 high = dwarf2_per_objfile->n_comp_units - 1;
19057 while (high > low)
19058 {
36586728 19059 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19060 int mid = low + (high - low) / 2;
9a619af0 19061
36586728
TT
19062 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19063 cu_off = &mid_cu->offset;
19064 if (mid_cu->is_dwz > offset_in_dwz
19065 || (mid_cu->is_dwz == offset_in_dwz
19066 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19067 high = mid;
19068 else
19069 low = mid + 1;
19070 }
19071 gdb_assert (low == high);
36586728
TT
19072 this_cu = dwarf2_per_objfile->all_comp_units[low];
19073 cu_off = &this_cu->offset;
19074 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19075 {
36586728 19076 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19077 error (_("Dwarf Error: could not find partial DIE containing "
19078 "offset 0x%lx [in module %s]"),
b64f50a1 19079 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19080
b64f50a1
JK
19081 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19082 <= offset.sect_off);
ae038cb0
DJ
19083 return dwarf2_per_objfile->all_comp_units[low-1];
19084 }
19085 else
19086 {
19087 this_cu = dwarf2_per_objfile->all_comp_units[low];
19088 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19089 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19090 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19091 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19092 return this_cu;
19093 }
19094}
19095
23745b47 19096/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19097
9816fde3 19098static void
23745b47 19099init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19100{
9816fde3 19101 memset (cu, 0, sizeof (*cu));
23745b47
DE
19102 per_cu->cu = cu;
19103 cu->per_cu = per_cu;
19104 cu->objfile = per_cu->objfile;
93311388 19105 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19106}
19107
19108/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19109
19110static void
95554aad
TT
19111prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19112 enum language pretend_language)
9816fde3
JK
19113{
19114 struct attribute *attr;
19115
19116 /* Set the language we're debugging. */
19117 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19118 if (attr)
19119 set_cu_language (DW_UNSND (attr), cu);
19120 else
9cded63f 19121 {
95554aad 19122 cu->language = pretend_language;
9cded63f
TT
19123 cu->language_defn = language_def (cu->language);
19124 }
dee91e82
DE
19125
19126 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19127 if (attr)
19128 cu->producer = DW_STRING (attr);
93311388
DE
19129}
19130
ae038cb0
DJ
19131/* Release one cached compilation unit, CU. We unlink it from the tree
19132 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19133 the caller is responsible for that.
19134 NOTE: DATA is a void * because this function is also used as a
19135 cleanup routine. */
ae038cb0
DJ
19136
19137static void
68dc6402 19138free_heap_comp_unit (void *data)
ae038cb0
DJ
19139{
19140 struct dwarf2_cu *cu = data;
19141
23745b47
DE
19142 gdb_assert (cu->per_cu != NULL);
19143 cu->per_cu->cu = NULL;
ae038cb0
DJ
19144 cu->per_cu = NULL;
19145
19146 obstack_free (&cu->comp_unit_obstack, NULL);
19147
19148 xfree (cu);
19149}
19150
72bf9492 19151/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19152 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19153 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19154
19155static void
19156free_stack_comp_unit (void *data)
19157{
19158 struct dwarf2_cu *cu = data;
19159
23745b47
DE
19160 gdb_assert (cu->per_cu != NULL);
19161 cu->per_cu->cu = NULL;
19162 cu->per_cu = NULL;
19163
72bf9492
DJ
19164 obstack_free (&cu->comp_unit_obstack, NULL);
19165 cu->partial_dies = NULL;
ae038cb0
DJ
19166}
19167
19168/* Free all cached compilation units. */
19169
19170static void
19171free_cached_comp_units (void *data)
19172{
19173 struct dwarf2_per_cu_data *per_cu, **last_chain;
19174
19175 per_cu = dwarf2_per_objfile->read_in_chain;
19176 last_chain = &dwarf2_per_objfile->read_in_chain;
19177 while (per_cu != NULL)
19178 {
19179 struct dwarf2_per_cu_data *next_cu;
19180
19181 next_cu = per_cu->cu->read_in_chain;
19182
68dc6402 19183 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19184 *last_chain = next_cu;
19185
19186 per_cu = next_cu;
19187 }
19188}
19189
19190/* Increase the age counter on each cached compilation unit, and free
19191 any that are too old. */
19192
19193static void
19194age_cached_comp_units (void)
19195{
19196 struct dwarf2_per_cu_data *per_cu, **last_chain;
19197
19198 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19199 per_cu = dwarf2_per_objfile->read_in_chain;
19200 while (per_cu != NULL)
19201 {
19202 per_cu->cu->last_used ++;
19203 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19204 dwarf2_mark (per_cu->cu);
19205 per_cu = per_cu->cu->read_in_chain;
19206 }
19207
19208 per_cu = dwarf2_per_objfile->read_in_chain;
19209 last_chain = &dwarf2_per_objfile->read_in_chain;
19210 while (per_cu != NULL)
19211 {
19212 struct dwarf2_per_cu_data *next_cu;
19213
19214 next_cu = per_cu->cu->read_in_chain;
19215
19216 if (!per_cu->cu->mark)
19217 {
68dc6402 19218 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19219 *last_chain = next_cu;
19220 }
19221 else
19222 last_chain = &per_cu->cu->read_in_chain;
19223
19224 per_cu = next_cu;
19225 }
19226}
19227
19228/* Remove a single compilation unit from the cache. */
19229
19230static void
dee91e82 19231free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19232{
19233 struct dwarf2_per_cu_data *per_cu, **last_chain;
19234
19235 per_cu = dwarf2_per_objfile->read_in_chain;
19236 last_chain = &dwarf2_per_objfile->read_in_chain;
19237 while (per_cu != NULL)
19238 {
19239 struct dwarf2_per_cu_data *next_cu;
19240
19241 next_cu = per_cu->cu->read_in_chain;
19242
dee91e82 19243 if (per_cu == target_per_cu)
ae038cb0 19244 {
68dc6402 19245 free_heap_comp_unit (per_cu->cu);
dee91e82 19246 per_cu->cu = NULL;
ae038cb0
DJ
19247 *last_chain = next_cu;
19248 break;
19249 }
19250 else
19251 last_chain = &per_cu->cu->read_in_chain;
19252
19253 per_cu = next_cu;
19254 }
19255}
19256
fe3e1990
DJ
19257/* Release all extra memory associated with OBJFILE. */
19258
19259void
19260dwarf2_free_objfile (struct objfile *objfile)
19261{
19262 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19263
19264 if (dwarf2_per_objfile == NULL)
19265 return;
19266
19267 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19268 free_cached_comp_units (NULL);
19269
7b9f3c50
DE
19270 if (dwarf2_per_objfile->quick_file_names_table)
19271 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19272
fe3e1990
DJ
19273 /* Everything else should be on the objfile obstack. */
19274}
19275
dee91e82
DE
19276/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19277 We store these in a hash table separate from the DIEs, and preserve them
19278 when the DIEs are flushed out of cache.
19279
19280 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19281 uniquely identify the type. A file may have multiple .debug_types sections,
19282 or the type may come from a DWO file. We have to use something in
19283 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19284 routine, get_die_type_at_offset, from outside this file, and thus won't
19285 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19286 of the objfile. */
1c379e20 19287
dee91e82 19288struct dwarf2_per_cu_offset_and_type
1c379e20 19289{
dee91e82 19290 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19291 sect_offset offset;
1c379e20
DJ
19292 struct type *type;
19293};
19294
dee91e82 19295/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19296
19297static hashval_t
dee91e82 19298per_cu_offset_and_type_hash (const void *item)
1c379e20 19299{
dee91e82 19300 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19301
dee91e82 19302 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19303}
19304
dee91e82 19305/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19306
19307static int
dee91e82 19308per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19309{
dee91e82
DE
19310 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19311 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19312
dee91e82
DE
19313 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19314 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19315}
19316
19317/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19318 table if necessary. For convenience, return TYPE.
19319
19320 The DIEs reading must have careful ordering to:
19321 * Not cause infite loops trying to read in DIEs as a prerequisite for
19322 reading current DIE.
19323 * Not trying to dereference contents of still incompletely read in types
19324 while reading in other DIEs.
19325 * Enable referencing still incompletely read in types just by a pointer to
19326 the type without accessing its fields.
19327
19328 Therefore caller should follow these rules:
19329 * Try to fetch any prerequisite types we may need to build this DIE type
19330 before building the type and calling set_die_type.
e71ec853 19331 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19332 possible before fetching more types to complete the current type.
19333 * Make the type as complete as possible before fetching more types. */
1c379e20 19334
f792889a 19335static struct type *
1c379e20
DJ
19336set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19337{
dee91e82 19338 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19339 struct objfile *objfile = cu->objfile;
1c379e20 19340
b4ba55a1
JB
19341 /* For Ada types, make sure that the gnat-specific data is always
19342 initialized (if not already set). There are a few types where
19343 we should not be doing so, because the type-specific area is
19344 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19345 where the type-specific area is used to store the floatformat).
19346 But this is not a problem, because the gnat-specific information
19347 is actually not needed for these types. */
19348 if (need_gnat_info (cu)
19349 && TYPE_CODE (type) != TYPE_CODE_FUNC
19350 && TYPE_CODE (type) != TYPE_CODE_FLT
19351 && !HAVE_GNAT_AUX_INFO (type))
19352 INIT_GNAT_SPECIFIC (type);
19353
dee91e82 19354 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19355 {
dee91e82
DE
19356 dwarf2_per_objfile->die_type_hash =
19357 htab_create_alloc_ex (127,
19358 per_cu_offset_and_type_hash,
19359 per_cu_offset_and_type_eq,
19360 NULL,
19361 &objfile->objfile_obstack,
19362 hashtab_obstack_allocate,
19363 dummy_obstack_deallocate);
f792889a 19364 }
1c379e20 19365
dee91e82 19366 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19367 ofs.offset = die->offset;
19368 ofs.type = type;
dee91e82
DE
19369 slot = (struct dwarf2_per_cu_offset_and_type **)
19370 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19371 if (*slot)
19372 complaint (&symfile_complaints,
19373 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19374 die->offset.sect_off);
673bfd45 19375 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19376 **slot = ofs;
f792889a 19377 return type;
1c379e20
DJ
19378}
19379
380bca97 19380/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19381 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19382
19383static struct type *
b64f50a1 19384get_die_type_at_offset (sect_offset offset,
673bfd45 19385 struct dwarf2_per_cu_data *per_cu)
1c379e20 19386{
dee91e82 19387 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19388
dee91e82 19389 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19390 return NULL;
1c379e20 19391
dee91e82 19392 ofs.per_cu = per_cu;
673bfd45 19393 ofs.offset = offset;
dee91e82 19394 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19395 if (slot)
19396 return slot->type;
19397 else
19398 return NULL;
19399}
19400
673bfd45
DE
19401/* Look up the type for DIE in the appropriate type_hash table,
19402 or return NULL if DIE does not have a saved type. */
19403
19404static struct type *
19405get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19406{
19407 return get_die_type_at_offset (die->offset, cu->per_cu);
19408}
19409
10b3939b
DJ
19410/* Add a dependence relationship from CU to REF_PER_CU. */
19411
19412static void
19413dwarf2_add_dependence (struct dwarf2_cu *cu,
19414 struct dwarf2_per_cu_data *ref_per_cu)
19415{
19416 void **slot;
19417
19418 if (cu->dependencies == NULL)
19419 cu->dependencies
19420 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19421 NULL, &cu->comp_unit_obstack,
19422 hashtab_obstack_allocate,
19423 dummy_obstack_deallocate);
19424
19425 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19426 if (*slot == NULL)
19427 *slot = ref_per_cu;
19428}
1c379e20 19429
f504f079
DE
19430/* Subroutine of dwarf2_mark to pass to htab_traverse.
19431 Set the mark field in every compilation unit in the
ae038cb0
DJ
19432 cache that we must keep because we are keeping CU. */
19433
10b3939b
DJ
19434static int
19435dwarf2_mark_helper (void **slot, void *data)
19436{
19437 struct dwarf2_per_cu_data *per_cu;
19438
19439 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19440
19441 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19442 reading of the chain. As such dependencies remain valid it is not much
19443 useful to track and undo them during QUIT cleanups. */
19444 if (per_cu->cu == NULL)
19445 return 1;
19446
10b3939b
DJ
19447 if (per_cu->cu->mark)
19448 return 1;
19449 per_cu->cu->mark = 1;
19450
19451 if (per_cu->cu->dependencies != NULL)
19452 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19453
19454 return 1;
19455}
19456
f504f079
DE
19457/* Set the mark field in CU and in every other compilation unit in the
19458 cache that we must keep because we are keeping CU. */
19459
ae038cb0
DJ
19460static void
19461dwarf2_mark (struct dwarf2_cu *cu)
19462{
19463 if (cu->mark)
19464 return;
19465 cu->mark = 1;
10b3939b
DJ
19466 if (cu->dependencies != NULL)
19467 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19468}
19469
19470static void
19471dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19472{
19473 while (per_cu)
19474 {
19475 per_cu->cu->mark = 0;
19476 per_cu = per_cu->cu->read_in_chain;
19477 }
72bf9492
DJ
19478}
19479
72bf9492
DJ
19480/* Trivial hash function for partial_die_info: the hash value of a DIE
19481 is its offset in .debug_info for this objfile. */
19482
19483static hashval_t
19484partial_die_hash (const void *item)
19485{
19486 const struct partial_die_info *part_die = item;
9a619af0 19487
b64f50a1 19488 return part_die->offset.sect_off;
72bf9492
DJ
19489}
19490
19491/* Trivial comparison function for partial_die_info structures: two DIEs
19492 are equal if they have the same offset. */
19493
19494static int
19495partial_die_eq (const void *item_lhs, const void *item_rhs)
19496{
19497 const struct partial_die_info *part_die_lhs = item_lhs;
19498 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19499
b64f50a1 19500 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19501}
19502
ae038cb0
DJ
19503static struct cmd_list_element *set_dwarf2_cmdlist;
19504static struct cmd_list_element *show_dwarf2_cmdlist;
19505
19506static void
19507set_dwarf2_cmd (char *args, int from_tty)
19508{
19509 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19510}
19511
19512static void
19513show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19514{
ae038cb0
DJ
19515 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19516}
19517
4bf44c1c 19518/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19519
19520static void
c1bd65d0 19521dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19522{
19523 struct dwarf2_per_objfile *data = d;
8b70b953 19524 int ix;
8b70b953 19525
95554aad
TT
19526 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19527 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19528 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19529
8b70b953 19530 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19531
19532 if (data->dwo_files)
19533 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19534
19535 if (data->dwz_file && data->dwz_file->dwz_bfd)
19536 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19537}
19538
19539\f
ae2de4f8 19540/* The "save gdb-index" command. */
9291a0cd
TT
19541
19542/* The contents of the hash table we create when building the string
19543 table. */
19544struct strtab_entry
19545{
19546 offset_type offset;
19547 const char *str;
19548};
19549
559a7a62
JK
19550/* Hash function for a strtab_entry.
19551
19552 Function is used only during write_hash_table so no index format backward
19553 compatibility is needed. */
b89be57b 19554
9291a0cd
TT
19555static hashval_t
19556hash_strtab_entry (const void *e)
19557{
19558 const struct strtab_entry *entry = e;
559a7a62 19559 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19560}
19561
19562/* Equality function for a strtab_entry. */
b89be57b 19563
9291a0cd
TT
19564static int
19565eq_strtab_entry (const void *a, const void *b)
19566{
19567 const struct strtab_entry *ea = a;
19568 const struct strtab_entry *eb = b;
19569 return !strcmp (ea->str, eb->str);
19570}
19571
19572/* Create a strtab_entry hash table. */
b89be57b 19573
9291a0cd
TT
19574static htab_t
19575create_strtab (void)
19576{
19577 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19578 xfree, xcalloc, xfree);
19579}
19580
19581/* Add a string to the constant pool. Return the string's offset in
19582 host order. */
b89be57b 19583
9291a0cd
TT
19584static offset_type
19585add_string (htab_t table, struct obstack *cpool, const char *str)
19586{
19587 void **slot;
19588 struct strtab_entry entry;
19589 struct strtab_entry *result;
19590
19591 entry.str = str;
19592 slot = htab_find_slot (table, &entry, INSERT);
19593 if (*slot)
19594 result = *slot;
19595 else
19596 {
19597 result = XNEW (struct strtab_entry);
19598 result->offset = obstack_object_size (cpool);
19599 result->str = str;
19600 obstack_grow_str0 (cpool, str);
19601 *slot = result;
19602 }
19603 return result->offset;
19604}
19605
19606/* An entry in the symbol table. */
19607struct symtab_index_entry
19608{
19609 /* The name of the symbol. */
19610 const char *name;
19611 /* The offset of the name in the constant pool. */
19612 offset_type index_offset;
19613 /* A sorted vector of the indices of all the CUs that hold an object
19614 of this name. */
19615 VEC (offset_type) *cu_indices;
19616};
19617
19618/* The symbol table. This is a power-of-2-sized hash table. */
19619struct mapped_symtab
19620{
19621 offset_type n_elements;
19622 offset_type size;
19623 struct symtab_index_entry **data;
19624};
19625
19626/* Hash function for a symtab_index_entry. */
b89be57b 19627
9291a0cd
TT
19628static hashval_t
19629hash_symtab_entry (const void *e)
19630{
19631 const struct symtab_index_entry *entry = e;
19632 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19633 sizeof (offset_type) * VEC_length (offset_type,
19634 entry->cu_indices),
19635 0);
19636}
19637
19638/* Equality function for a symtab_index_entry. */
b89be57b 19639
9291a0cd
TT
19640static int
19641eq_symtab_entry (const void *a, const void *b)
19642{
19643 const struct symtab_index_entry *ea = a;
19644 const struct symtab_index_entry *eb = b;
19645 int len = VEC_length (offset_type, ea->cu_indices);
19646 if (len != VEC_length (offset_type, eb->cu_indices))
19647 return 0;
19648 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19649 VEC_address (offset_type, eb->cu_indices),
19650 sizeof (offset_type) * len);
19651}
19652
19653/* Destroy a symtab_index_entry. */
b89be57b 19654
9291a0cd
TT
19655static void
19656delete_symtab_entry (void *p)
19657{
19658 struct symtab_index_entry *entry = p;
19659 VEC_free (offset_type, entry->cu_indices);
19660 xfree (entry);
19661}
19662
19663/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19664
9291a0cd 19665static htab_t
3876f04e 19666create_symbol_hash_table (void)
9291a0cd
TT
19667{
19668 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19669 delete_symtab_entry, xcalloc, xfree);
19670}
19671
19672/* Create a new mapped symtab object. */
b89be57b 19673
9291a0cd
TT
19674static struct mapped_symtab *
19675create_mapped_symtab (void)
19676{
19677 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19678 symtab->n_elements = 0;
19679 symtab->size = 1024;
19680 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19681 return symtab;
19682}
19683
19684/* Destroy a mapped_symtab. */
b89be57b 19685
9291a0cd
TT
19686static void
19687cleanup_mapped_symtab (void *p)
19688{
19689 struct mapped_symtab *symtab = p;
19690 /* The contents of the array are freed when the other hash table is
19691 destroyed. */
19692 xfree (symtab->data);
19693 xfree (symtab);
19694}
19695
19696/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19697 the slot.
19698
19699 Function is used only during write_hash_table so no index format backward
19700 compatibility is needed. */
b89be57b 19701
9291a0cd
TT
19702static struct symtab_index_entry **
19703find_slot (struct mapped_symtab *symtab, const char *name)
19704{
559a7a62 19705 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19706
19707 index = hash & (symtab->size - 1);
19708 step = ((hash * 17) & (symtab->size - 1)) | 1;
19709
19710 for (;;)
19711 {
19712 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19713 return &symtab->data[index];
19714 index = (index + step) & (symtab->size - 1);
19715 }
19716}
19717
19718/* Expand SYMTAB's hash table. */
b89be57b 19719
9291a0cd
TT
19720static void
19721hash_expand (struct mapped_symtab *symtab)
19722{
19723 offset_type old_size = symtab->size;
19724 offset_type i;
19725 struct symtab_index_entry **old_entries = symtab->data;
19726
19727 symtab->size *= 2;
19728 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19729
19730 for (i = 0; i < old_size; ++i)
19731 {
19732 if (old_entries[i])
19733 {
19734 struct symtab_index_entry **slot = find_slot (symtab,
19735 old_entries[i]->name);
19736 *slot = old_entries[i];
19737 }
19738 }
19739
19740 xfree (old_entries);
19741}
19742
156942c7
DE
19743/* Add an entry to SYMTAB. NAME is the name of the symbol.
19744 CU_INDEX is the index of the CU in which the symbol appears.
19745 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19746
9291a0cd
TT
19747static void
19748add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19749 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19750 offset_type cu_index)
19751{
19752 struct symtab_index_entry **slot;
156942c7 19753 offset_type cu_index_and_attrs;
9291a0cd
TT
19754
19755 ++symtab->n_elements;
19756 if (4 * symtab->n_elements / 3 >= symtab->size)
19757 hash_expand (symtab);
19758
19759 slot = find_slot (symtab, name);
19760 if (!*slot)
19761 {
19762 *slot = XNEW (struct symtab_index_entry);
19763 (*slot)->name = name;
156942c7 19764 /* index_offset is set later. */
9291a0cd
TT
19765 (*slot)->cu_indices = NULL;
19766 }
156942c7
DE
19767
19768 cu_index_and_attrs = 0;
19769 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19770 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19771 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19772
19773 /* We don't want to record an index value twice as we want to avoid the
19774 duplication.
19775 We process all global symbols and then all static symbols
19776 (which would allow us to avoid the duplication by only having to check
19777 the last entry pushed), but a symbol could have multiple kinds in one CU.
19778 To keep things simple we don't worry about the duplication here and
19779 sort and uniqufy the list after we've processed all symbols. */
19780 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19781}
19782
19783/* qsort helper routine for uniquify_cu_indices. */
19784
19785static int
19786offset_type_compare (const void *ap, const void *bp)
19787{
19788 offset_type a = *(offset_type *) ap;
19789 offset_type b = *(offset_type *) bp;
19790
19791 return (a > b) - (b > a);
19792}
19793
19794/* Sort and remove duplicates of all symbols' cu_indices lists. */
19795
19796static void
19797uniquify_cu_indices (struct mapped_symtab *symtab)
19798{
19799 int i;
19800
19801 for (i = 0; i < symtab->size; ++i)
19802 {
19803 struct symtab_index_entry *entry = symtab->data[i];
19804
19805 if (entry
19806 && entry->cu_indices != NULL)
19807 {
19808 unsigned int next_to_insert, next_to_check;
19809 offset_type last_value;
19810
19811 qsort (VEC_address (offset_type, entry->cu_indices),
19812 VEC_length (offset_type, entry->cu_indices),
19813 sizeof (offset_type), offset_type_compare);
19814
19815 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19816 next_to_insert = 1;
19817 for (next_to_check = 1;
19818 next_to_check < VEC_length (offset_type, entry->cu_indices);
19819 ++next_to_check)
19820 {
19821 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19822 != last_value)
19823 {
19824 last_value = VEC_index (offset_type, entry->cu_indices,
19825 next_to_check);
19826 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19827 last_value);
19828 ++next_to_insert;
19829 }
19830 }
19831 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19832 }
19833 }
9291a0cd
TT
19834}
19835
19836/* Add a vector of indices to the constant pool. */
b89be57b 19837
9291a0cd 19838static offset_type
3876f04e 19839add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19840 struct symtab_index_entry *entry)
19841{
19842 void **slot;
19843
3876f04e 19844 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19845 if (!*slot)
19846 {
19847 offset_type len = VEC_length (offset_type, entry->cu_indices);
19848 offset_type val = MAYBE_SWAP (len);
19849 offset_type iter;
19850 int i;
19851
19852 *slot = entry;
19853 entry->index_offset = obstack_object_size (cpool);
19854
19855 obstack_grow (cpool, &val, sizeof (val));
19856 for (i = 0;
19857 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19858 ++i)
19859 {
19860 val = MAYBE_SWAP (iter);
19861 obstack_grow (cpool, &val, sizeof (val));
19862 }
19863 }
19864 else
19865 {
19866 struct symtab_index_entry *old_entry = *slot;
19867 entry->index_offset = old_entry->index_offset;
19868 entry = old_entry;
19869 }
19870 return entry->index_offset;
19871}
19872
19873/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19874 constant pool entries going into the obstack CPOOL. */
b89be57b 19875
9291a0cd
TT
19876static void
19877write_hash_table (struct mapped_symtab *symtab,
19878 struct obstack *output, struct obstack *cpool)
19879{
19880 offset_type i;
3876f04e 19881 htab_t symbol_hash_table;
9291a0cd
TT
19882 htab_t str_table;
19883
3876f04e 19884 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19885 str_table = create_strtab ();
3876f04e 19886
9291a0cd
TT
19887 /* We add all the index vectors to the constant pool first, to
19888 ensure alignment is ok. */
19889 for (i = 0; i < symtab->size; ++i)
19890 {
19891 if (symtab->data[i])
3876f04e 19892 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19893 }
19894
19895 /* Now write out the hash table. */
19896 for (i = 0; i < symtab->size; ++i)
19897 {
19898 offset_type str_off, vec_off;
19899
19900 if (symtab->data[i])
19901 {
19902 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19903 vec_off = symtab->data[i]->index_offset;
19904 }
19905 else
19906 {
19907 /* While 0 is a valid constant pool index, it is not valid
19908 to have 0 for both offsets. */
19909 str_off = 0;
19910 vec_off = 0;
19911 }
19912
19913 str_off = MAYBE_SWAP (str_off);
19914 vec_off = MAYBE_SWAP (vec_off);
19915
19916 obstack_grow (output, &str_off, sizeof (str_off));
19917 obstack_grow (output, &vec_off, sizeof (vec_off));
19918 }
19919
19920 htab_delete (str_table);
3876f04e 19921 htab_delete (symbol_hash_table);
9291a0cd
TT
19922}
19923
0a5429f6
DE
19924/* Struct to map psymtab to CU index in the index file. */
19925struct psymtab_cu_index_map
19926{
19927 struct partial_symtab *psymtab;
19928 unsigned int cu_index;
19929};
19930
19931static hashval_t
19932hash_psymtab_cu_index (const void *item)
19933{
19934 const struct psymtab_cu_index_map *map = item;
19935
19936 return htab_hash_pointer (map->psymtab);
19937}
19938
19939static int
19940eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19941{
19942 const struct psymtab_cu_index_map *lhs = item_lhs;
19943 const struct psymtab_cu_index_map *rhs = item_rhs;
19944
19945 return lhs->psymtab == rhs->psymtab;
19946}
19947
19948/* Helper struct for building the address table. */
19949struct addrmap_index_data
19950{
19951 struct objfile *objfile;
19952 struct obstack *addr_obstack;
19953 htab_t cu_index_htab;
19954
19955 /* Non-zero if the previous_* fields are valid.
19956 We can't write an entry until we see the next entry (since it is only then
19957 that we know the end of the entry). */
19958 int previous_valid;
19959 /* Index of the CU in the table of all CUs in the index file. */
19960 unsigned int previous_cu_index;
0963b4bd 19961 /* Start address of the CU. */
0a5429f6
DE
19962 CORE_ADDR previous_cu_start;
19963};
19964
19965/* Write an address entry to OBSTACK. */
b89be57b 19966
9291a0cd 19967static void
0a5429f6
DE
19968add_address_entry (struct objfile *objfile, struct obstack *obstack,
19969 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 19970{
0a5429f6 19971 offset_type cu_index_to_write;
9291a0cd
TT
19972 char addr[8];
19973 CORE_ADDR baseaddr;
19974
19975 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19976
0a5429f6
DE
19977 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19978 obstack_grow (obstack, addr, 8);
19979 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19980 obstack_grow (obstack, addr, 8);
19981 cu_index_to_write = MAYBE_SWAP (cu_index);
19982 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19983}
19984
19985/* Worker function for traversing an addrmap to build the address table. */
19986
19987static int
19988add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19989{
19990 struct addrmap_index_data *data = datap;
19991 struct partial_symtab *pst = obj;
0a5429f6
DE
19992
19993 if (data->previous_valid)
19994 add_address_entry (data->objfile, data->addr_obstack,
19995 data->previous_cu_start, start_addr,
19996 data->previous_cu_index);
19997
19998 data->previous_cu_start = start_addr;
19999 if (pst != NULL)
20000 {
20001 struct psymtab_cu_index_map find_map, *map;
20002 find_map.psymtab = pst;
20003 map = htab_find (data->cu_index_htab, &find_map);
20004 gdb_assert (map != NULL);
20005 data->previous_cu_index = map->cu_index;
20006 data->previous_valid = 1;
20007 }
20008 else
20009 data->previous_valid = 0;
20010
20011 return 0;
20012}
20013
20014/* Write OBJFILE's address map to OBSTACK.
20015 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20016 in the index file. */
20017
20018static void
20019write_address_map (struct objfile *objfile, struct obstack *obstack,
20020 htab_t cu_index_htab)
20021{
20022 struct addrmap_index_data addrmap_index_data;
20023
20024 /* When writing the address table, we have to cope with the fact that
20025 the addrmap iterator only provides the start of a region; we have to
20026 wait until the next invocation to get the start of the next region. */
20027
20028 addrmap_index_data.objfile = objfile;
20029 addrmap_index_data.addr_obstack = obstack;
20030 addrmap_index_data.cu_index_htab = cu_index_htab;
20031 addrmap_index_data.previous_valid = 0;
20032
20033 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20034 &addrmap_index_data);
20035
20036 /* It's highly unlikely the last entry (end address = 0xff...ff)
20037 is valid, but we should still handle it.
20038 The end address is recorded as the start of the next region, but that
20039 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20040 anyway. */
20041 if (addrmap_index_data.previous_valid)
20042 add_address_entry (objfile, obstack,
20043 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20044 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20045}
20046
156942c7
DE
20047/* Return the symbol kind of PSYM. */
20048
20049static gdb_index_symbol_kind
20050symbol_kind (struct partial_symbol *psym)
20051{
20052 domain_enum domain = PSYMBOL_DOMAIN (psym);
20053 enum address_class aclass = PSYMBOL_CLASS (psym);
20054
20055 switch (domain)
20056 {
20057 case VAR_DOMAIN:
20058 switch (aclass)
20059 {
20060 case LOC_BLOCK:
20061 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20062 case LOC_TYPEDEF:
20063 return GDB_INDEX_SYMBOL_KIND_TYPE;
20064 case LOC_COMPUTED:
20065 case LOC_CONST_BYTES:
20066 case LOC_OPTIMIZED_OUT:
20067 case LOC_STATIC:
20068 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20069 case LOC_CONST:
20070 /* Note: It's currently impossible to recognize psyms as enum values
20071 short of reading the type info. For now punt. */
20072 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20073 default:
20074 /* There are other LOC_FOO values that one might want to classify
20075 as variables, but dwarf2read.c doesn't currently use them. */
20076 return GDB_INDEX_SYMBOL_KIND_OTHER;
20077 }
20078 case STRUCT_DOMAIN:
20079 return GDB_INDEX_SYMBOL_KIND_TYPE;
20080 default:
20081 return GDB_INDEX_SYMBOL_KIND_OTHER;
20082 }
20083}
20084
9291a0cd 20085/* Add a list of partial symbols to SYMTAB. */
b89be57b 20086
9291a0cd
TT
20087static void
20088write_psymbols (struct mapped_symtab *symtab,
987d643c 20089 htab_t psyms_seen,
9291a0cd
TT
20090 struct partial_symbol **psymp,
20091 int count,
987d643c
TT
20092 offset_type cu_index,
20093 int is_static)
9291a0cd
TT
20094{
20095 for (; count-- > 0; ++psymp)
20096 {
156942c7
DE
20097 struct partial_symbol *psym = *psymp;
20098 void **slot;
987d643c 20099
156942c7 20100 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20101 error (_("Ada is not currently supported by the index"));
987d643c 20102
987d643c 20103 /* Only add a given psymbol once. */
156942c7 20104 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20105 if (!*slot)
20106 {
156942c7
DE
20107 gdb_index_symbol_kind kind = symbol_kind (psym);
20108
20109 *slot = psym;
20110 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20111 is_static, kind, cu_index);
987d643c 20112 }
9291a0cd
TT
20113 }
20114}
20115
20116/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20117 exception if there is an error. */
b89be57b 20118
9291a0cd
TT
20119static void
20120write_obstack (FILE *file, struct obstack *obstack)
20121{
20122 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20123 file)
20124 != obstack_object_size (obstack))
20125 error (_("couldn't data write to file"));
20126}
20127
20128/* Unlink a file if the argument is not NULL. */
b89be57b 20129
9291a0cd
TT
20130static void
20131unlink_if_set (void *p)
20132{
20133 char **filename = p;
20134 if (*filename)
20135 unlink (*filename);
20136}
20137
1fd400ff
TT
20138/* A helper struct used when iterating over debug_types. */
20139struct signatured_type_index_data
20140{
20141 struct objfile *objfile;
20142 struct mapped_symtab *symtab;
20143 struct obstack *types_list;
987d643c 20144 htab_t psyms_seen;
1fd400ff
TT
20145 int cu_index;
20146};
20147
20148/* A helper function that writes a single signatured_type to an
20149 obstack. */
b89be57b 20150
1fd400ff
TT
20151static int
20152write_one_signatured_type (void **slot, void *d)
20153{
20154 struct signatured_type_index_data *info = d;
20155 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20156 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20157 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20158 gdb_byte val[8];
20159
20160 write_psymbols (info->symtab,
987d643c 20161 info->psyms_seen,
3e43a32a
MS
20162 info->objfile->global_psymbols.list
20163 + psymtab->globals_offset,
987d643c
TT
20164 psymtab->n_global_syms, info->cu_index,
20165 0);
1fd400ff 20166 write_psymbols (info->symtab,
987d643c 20167 info->psyms_seen,
3e43a32a
MS
20168 info->objfile->static_psymbols.list
20169 + psymtab->statics_offset,
987d643c
TT
20170 psymtab->n_static_syms, info->cu_index,
20171 1);
1fd400ff 20172
b64f50a1
JK
20173 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20174 entry->per_cu.offset.sect_off);
1fd400ff 20175 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20176 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20177 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20178 obstack_grow (info->types_list, val, 8);
20179 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20180 obstack_grow (info->types_list, val, 8);
20181
20182 ++info->cu_index;
20183
20184 return 1;
20185}
20186
95554aad
TT
20187/* Recurse into all "included" dependencies and write their symbols as
20188 if they appeared in this psymtab. */
20189
20190static void
20191recursively_write_psymbols (struct objfile *objfile,
20192 struct partial_symtab *psymtab,
20193 struct mapped_symtab *symtab,
20194 htab_t psyms_seen,
20195 offset_type cu_index)
20196{
20197 int i;
20198
20199 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20200 if (psymtab->dependencies[i]->user != NULL)
20201 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20202 symtab, psyms_seen, cu_index);
20203
20204 write_psymbols (symtab,
20205 psyms_seen,
20206 objfile->global_psymbols.list + psymtab->globals_offset,
20207 psymtab->n_global_syms, cu_index,
20208 0);
20209 write_psymbols (symtab,
20210 psyms_seen,
20211 objfile->static_psymbols.list + psymtab->statics_offset,
20212 psymtab->n_static_syms, cu_index,
20213 1);
20214}
20215
9291a0cd 20216/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20217
9291a0cd
TT
20218static void
20219write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20220{
20221 struct cleanup *cleanup;
20222 char *filename, *cleanup_filename;
1fd400ff
TT
20223 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20224 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20225 int i;
20226 FILE *out_file;
20227 struct mapped_symtab *symtab;
20228 offset_type val, size_of_contents, total_len;
20229 struct stat st;
987d643c 20230 htab_t psyms_seen;
0a5429f6
DE
20231 htab_t cu_index_htab;
20232 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20233
b4f2f049 20234 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20235 return;
b4f2f049 20236
9291a0cd
TT
20237 if (dwarf2_per_objfile->using_index)
20238 error (_("Cannot use an index to create the index"));
20239
8b70b953
TT
20240 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20241 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20242
9291a0cd 20243 if (stat (objfile->name, &st) < 0)
7e17e088 20244 perror_with_name (objfile->name);
9291a0cd
TT
20245
20246 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20247 INDEX_SUFFIX, (char *) NULL);
20248 cleanup = make_cleanup (xfree, filename);
20249
20250 out_file = fopen (filename, "wb");
20251 if (!out_file)
20252 error (_("Can't open `%s' for writing"), filename);
20253
20254 cleanup_filename = filename;
20255 make_cleanup (unlink_if_set, &cleanup_filename);
20256
20257 symtab = create_mapped_symtab ();
20258 make_cleanup (cleanup_mapped_symtab, symtab);
20259
20260 obstack_init (&addr_obstack);
20261 make_cleanup_obstack_free (&addr_obstack);
20262
20263 obstack_init (&cu_list);
20264 make_cleanup_obstack_free (&cu_list);
20265
1fd400ff
TT
20266 obstack_init (&types_cu_list);
20267 make_cleanup_obstack_free (&types_cu_list);
20268
987d643c
TT
20269 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20270 NULL, xcalloc, xfree);
96408a79 20271 make_cleanup_htab_delete (psyms_seen);
987d643c 20272
0a5429f6
DE
20273 /* While we're scanning CU's create a table that maps a psymtab pointer
20274 (which is what addrmap records) to its index (which is what is recorded
20275 in the index file). This will later be needed to write the address
20276 table. */
20277 cu_index_htab = htab_create_alloc (100,
20278 hash_psymtab_cu_index,
20279 eq_psymtab_cu_index,
20280 NULL, xcalloc, xfree);
96408a79 20281 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20282 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20283 xmalloc (sizeof (struct psymtab_cu_index_map)
20284 * dwarf2_per_objfile->n_comp_units);
20285 make_cleanup (xfree, psymtab_cu_index_map);
20286
20287 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20288 work here. Also, the debug_types entries do not appear in
20289 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20290 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20291 {
3e43a32a
MS
20292 struct dwarf2_per_cu_data *per_cu
20293 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20294 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20295 gdb_byte val[8];
0a5429f6
DE
20296 struct psymtab_cu_index_map *map;
20297 void **slot;
9291a0cd 20298
95554aad
TT
20299 if (psymtab->user == NULL)
20300 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20301
0a5429f6
DE
20302 map = &psymtab_cu_index_map[i];
20303 map->psymtab = psymtab;
20304 map->cu_index = i;
20305 slot = htab_find_slot (cu_index_htab, map, INSERT);
20306 gdb_assert (slot != NULL);
20307 gdb_assert (*slot == NULL);
20308 *slot = map;
9291a0cd 20309
b64f50a1
JK
20310 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20311 per_cu->offset.sect_off);
9291a0cd 20312 obstack_grow (&cu_list, val, 8);
e254ef6a 20313 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20314 obstack_grow (&cu_list, val, 8);
20315 }
20316
0a5429f6
DE
20317 /* Dump the address map. */
20318 write_address_map (objfile, &addr_obstack, cu_index_htab);
20319
1fd400ff
TT
20320 /* Write out the .debug_type entries, if any. */
20321 if (dwarf2_per_objfile->signatured_types)
20322 {
20323 struct signatured_type_index_data sig_data;
20324
20325 sig_data.objfile = objfile;
20326 sig_data.symtab = symtab;
20327 sig_data.types_list = &types_cu_list;
987d643c 20328 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20329 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20330 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20331 write_one_signatured_type, &sig_data);
20332 }
20333
156942c7
DE
20334 /* Now that we've processed all symbols we can shrink their cu_indices
20335 lists. */
20336 uniquify_cu_indices (symtab);
20337
9291a0cd
TT
20338 obstack_init (&constant_pool);
20339 make_cleanup_obstack_free (&constant_pool);
20340 obstack_init (&symtab_obstack);
20341 make_cleanup_obstack_free (&symtab_obstack);
20342 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20343
20344 obstack_init (&contents);
20345 make_cleanup_obstack_free (&contents);
1fd400ff 20346 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20347 total_len = size_of_contents;
20348
20349 /* The version number. */
156942c7 20350 val = MAYBE_SWAP (7);
9291a0cd
TT
20351 obstack_grow (&contents, &val, sizeof (val));
20352
20353 /* The offset of the CU list from the start of the file. */
20354 val = MAYBE_SWAP (total_len);
20355 obstack_grow (&contents, &val, sizeof (val));
20356 total_len += obstack_object_size (&cu_list);
20357
1fd400ff
TT
20358 /* The offset of the types CU list from the start of the file. */
20359 val = MAYBE_SWAP (total_len);
20360 obstack_grow (&contents, &val, sizeof (val));
20361 total_len += obstack_object_size (&types_cu_list);
20362
9291a0cd
TT
20363 /* The offset of the address table from the start of the file. */
20364 val = MAYBE_SWAP (total_len);
20365 obstack_grow (&contents, &val, sizeof (val));
20366 total_len += obstack_object_size (&addr_obstack);
20367
20368 /* The offset of the symbol table from the start of the file. */
20369 val = MAYBE_SWAP (total_len);
20370 obstack_grow (&contents, &val, sizeof (val));
20371 total_len += obstack_object_size (&symtab_obstack);
20372
20373 /* The offset of the constant pool from the start of the file. */
20374 val = MAYBE_SWAP (total_len);
20375 obstack_grow (&contents, &val, sizeof (val));
20376 total_len += obstack_object_size (&constant_pool);
20377
20378 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20379
20380 write_obstack (out_file, &contents);
20381 write_obstack (out_file, &cu_list);
1fd400ff 20382 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20383 write_obstack (out_file, &addr_obstack);
20384 write_obstack (out_file, &symtab_obstack);
20385 write_obstack (out_file, &constant_pool);
20386
20387 fclose (out_file);
20388
20389 /* We want to keep the file, so we set cleanup_filename to NULL
20390 here. See unlink_if_set. */
20391 cleanup_filename = NULL;
20392
20393 do_cleanups (cleanup);
20394}
20395
90476074
TT
20396/* Implementation of the `save gdb-index' command.
20397
20398 Note that the file format used by this command is documented in the
20399 GDB manual. Any changes here must be documented there. */
11570e71 20400
9291a0cd
TT
20401static void
20402save_gdb_index_command (char *arg, int from_tty)
20403{
20404 struct objfile *objfile;
20405
20406 if (!arg || !*arg)
96d19272 20407 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20408
20409 ALL_OBJFILES (objfile)
20410 {
20411 struct stat st;
20412
20413 /* If the objfile does not correspond to an actual file, skip it. */
20414 if (stat (objfile->name, &st) < 0)
20415 continue;
20416
20417 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20418 if (dwarf2_per_objfile)
20419 {
20420 volatile struct gdb_exception except;
20421
20422 TRY_CATCH (except, RETURN_MASK_ERROR)
20423 {
20424 write_psymtabs_to_index (objfile, arg);
20425 }
20426 if (except.reason < 0)
20427 exception_fprintf (gdb_stderr, except,
20428 _("Error while writing index for `%s': "),
20429 objfile->name);
20430 }
20431 }
dce234bc
PP
20432}
20433
9291a0cd
TT
20434\f
20435
9eae7c52
TT
20436int dwarf2_always_disassemble;
20437
20438static void
20439show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20440 struct cmd_list_element *c, const char *value)
20441{
3e43a32a
MS
20442 fprintf_filtered (file,
20443 _("Whether to always disassemble "
20444 "DWARF expressions is %s.\n"),
9eae7c52
TT
20445 value);
20446}
20447
900e11f9
JK
20448static void
20449show_check_physname (struct ui_file *file, int from_tty,
20450 struct cmd_list_element *c, const char *value)
20451{
20452 fprintf_filtered (file,
20453 _("Whether to check \"physname\" is %s.\n"),
20454 value);
20455}
20456
6502dd73
DJ
20457void _initialize_dwarf2_read (void);
20458
20459void
20460_initialize_dwarf2_read (void)
20461{
96d19272
JK
20462 struct cmd_list_element *c;
20463
dce234bc 20464 dwarf2_objfile_data_key
c1bd65d0 20465 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20466
1bedd215
AC
20467 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20468Set DWARF 2 specific variables.\n\
20469Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20470 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20471 0/*allow-unknown*/, &maintenance_set_cmdlist);
20472
1bedd215
AC
20473 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20474Show DWARF 2 specific variables\n\
20475Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20476 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20477 0/*allow-unknown*/, &maintenance_show_cmdlist);
20478
20479 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20480 &dwarf2_max_cache_age, _("\
20481Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20482Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20483A higher limit means that cached compilation units will be stored\n\
20484in memory longer, and more total memory will be used. Zero disables\n\
20485caching, which can slow down startup."),
2c5b56ce 20486 NULL,
920d2a44 20487 show_dwarf2_max_cache_age,
2c5b56ce 20488 &set_dwarf2_cmdlist,
ae038cb0 20489 &show_dwarf2_cmdlist);
d97bc12b 20490
9eae7c52
TT
20491 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20492 &dwarf2_always_disassemble, _("\
20493Set whether `info address' always disassembles DWARF expressions."), _("\
20494Show whether `info address' always disassembles DWARF expressions."), _("\
20495When enabled, DWARF expressions are always printed in an assembly-like\n\
20496syntax. When disabled, expressions will be printed in a more\n\
20497conversational style, when possible."),
20498 NULL,
20499 show_dwarf2_always_disassemble,
20500 &set_dwarf2_cmdlist,
20501 &show_dwarf2_cmdlist);
20502
45cfd468
DE
20503 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20504Set debugging of the dwarf2 reader."), _("\
20505Show debugging of the dwarf2 reader."), _("\
20506When enabled, debugging messages are printed during dwarf2 reading\n\
20507and symtab expansion."),
20508 NULL,
20509 NULL,
20510 &setdebuglist, &showdebuglist);
20511
ccce17b0 20512 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20513Set debugging of the dwarf2 DIE reader."), _("\
20514Show debugging of the dwarf2 DIE reader."), _("\
20515When enabled (non-zero), DIEs are dumped after they are read in.\n\
20516The value is the maximum depth to print."),
ccce17b0
YQ
20517 NULL,
20518 NULL,
20519 &setdebuglist, &showdebuglist);
9291a0cd 20520
900e11f9
JK
20521 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20522Set cross-checking of \"physname\" code against demangler."), _("\
20523Show cross-checking of \"physname\" code against demangler."), _("\
20524When enabled, GDB's internal \"physname\" code is checked against\n\
20525the demangler."),
20526 NULL, show_check_physname,
20527 &setdebuglist, &showdebuglist);
20528
e615022a
DE
20529 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20530 no_class, &use_deprecated_index_sections, _("\
20531Set whether to use deprecated gdb_index sections."), _("\
20532Show whether to use deprecated gdb_index sections."), _("\
20533When enabled, deprecated .gdb_index sections are used anyway.\n\
20534Normally they are ignored either because of a missing feature or\n\
20535performance issue.\n\
20536Warning: This option must be enabled before gdb reads the file."),
20537 NULL,
20538 NULL,
20539 &setlist, &showlist);
20540
96d19272 20541 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20542 _("\
fc1a9d6e 20543Save a gdb-index file.\n\
11570e71 20544Usage: save gdb-index DIRECTORY"),
96d19272
JK
20545 &save_cmdlist);
20546 set_cmd_completer (c, filename_completer);
6502dd73 20547}
This page took 2.95221 seconds and 4 git commands to generate.