* dsrec.c (report_transfer_performance): Don't declare.
[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
2115 return result;
2116}
9291a0cd 2117\f
7b9f3c50
DE
2118/* DWARF quick_symbols_functions support. */
2119
2120/* TUs can share .debug_line entries, and there can be a lot more TUs than
2121 unique line tables, so we maintain a separate table of all .debug_line
2122 derived entries to support the sharing.
2123 All the quick functions need is the list of file names. We discard the
2124 line_header when we're done and don't need to record it here. */
2125struct quick_file_names
2126{
094b34ac
DE
2127 /* The data used to construct the hash key. */
2128 struct stmt_list_hash hash;
7b9f3c50
DE
2129
2130 /* The number of entries in file_names, real_names. */
2131 unsigned int num_file_names;
2132
2133 /* The file names from the line table, after being run through
2134 file_full_name. */
2135 const char **file_names;
2136
2137 /* The file names from the line table after being run through
2138 gdb_realpath. These are computed lazily. */
2139 const char **real_names;
2140};
2141
2142/* When using the index (and thus not using psymtabs), each CU has an
2143 object of this type. This is used to hold information needed by
2144 the various "quick" methods. */
2145struct dwarf2_per_cu_quick_data
2146{
2147 /* The file table. This can be NULL if there was no file table
2148 or it's currently not read in.
2149 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2150 struct quick_file_names *file_names;
2151
2152 /* The corresponding symbol table. This is NULL if symbols for this
2153 CU have not yet been read. */
2154 struct symtab *symtab;
2155
2156 /* A temporary mark bit used when iterating over all CUs in
2157 expand_symtabs_matching. */
2158 unsigned int mark : 1;
2159
2160 /* True if we've tried to read the file table and found there isn't one.
2161 There will be no point in trying to read it again next time. */
2162 unsigned int no_file_data : 1;
2163};
2164
094b34ac
DE
2165/* Utility hash function for a stmt_list_hash. */
2166
2167static hashval_t
2168hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2169{
2170 hashval_t v = 0;
2171
2172 if (stmt_list_hash->dwo_unit != NULL)
2173 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2174 v += stmt_list_hash->line_offset.sect_off;
2175 return v;
2176}
2177
2178/* Utility equality function for a stmt_list_hash. */
2179
2180static int
2181eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2182 const struct stmt_list_hash *rhs)
2183{
2184 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2185 return 0;
2186 if (lhs->dwo_unit != NULL
2187 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2188 return 0;
2189
2190 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2191}
2192
7b9f3c50
DE
2193/* Hash function for a quick_file_names. */
2194
2195static hashval_t
2196hash_file_name_entry (const void *e)
2197{
2198 const struct quick_file_names *file_data = e;
2199
094b34ac 2200 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2201}
2202
2203/* Equality function for a quick_file_names. */
2204
2205static int
2206eq_file_name_entry (const void *a, const void *b)
2207{
2208 const struct quick_file_names *ea = a;
2209 const struct quick_file_names *eb = b;
2210
094b34ac 2211 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2212}
2213
2214/* Delete function for a quick_file_names. */
2215
2216static void
2217delete_file_name_entry (void *e)
2218{
2219 struct quick_file_names *file_data = e;
2220 int i;
2221
2222 for (i = 0; i < file_data->num_file_names; ++i)
2223 {
2224 xfree ((void*) file_data->file_names[i]);
2225 if (file_data->real_names)
2226 xfree ((void*) file_data->real_names[i]);
2227 }
2228
2229 /* The space for the struct itself lives on objfile_obstack,
2230 so we don't free it here. */
2231}
2232
2233/* Create a quick_file_names hash table. */
2234
2235static htab_t
2236create_quick_file_names_table (unsigned int nr_initial_entries)
2237{
2238 return htab_create_alloc (nr_initial_entries,
2239 hash_file_name_entry, eq_file_name_entry,
2240 delete_file_name_entry, xcalloc, xfree);
2241}
9291a0cd 2242
918dd910
JK
2243/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2244 have to be created afterwards. You should call age_cached_comp_units after
2245 processing PER_CU->CU. dw2_setup must have been already called. */
2246
2247static void
2248load_cu (struct dwarf2_per_cu_data *per_cu)
2249{
3019eac3 2250 if (per_cu->is_debug_types)
e5fe5e75 2251 load_full_type_unit (per_cu);
918dd910 2252 else
95554aad 2253 load_full_comp_unit (per_cu, language_minimal);
918dd910 2254
918dd910 2255 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2256
2257 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2258}
2259
a0f42c21 2260/* Read in the symbols for PER_CU. */
2fdf6df6 2261
9291a0cd 2262static void
a0f42c21 2263dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2264{
2265 struct cleanup *back_to;
2266
f4dc4d17
DE
2267 /* Skip type_unit_groups, reading the type units they contain
2268 is handled elsewhere. */
2269 if (IS_TYPE_UNIT_GROUP (per_cu))
2270 return;
2271
9291a0cd
TT
2272 back_to = make_cleanup (dwarf2_release_queue, NULL);
2273
95554aad
TT
2274 if (dwarf2_per_objfile->using_index
2275 ? per_cu->v.quick->symtab == NULL
2276 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2277 {
2278 queue_comp_unit (per_cu, language_minimal);
2279 load_cu (per_cu);
2280 }
9291a0cd 2281
a0f42c21 2282 process_queue ();
9291a0cd
TT
2283
2284 /* Age the cache, releasing compilation units that have not
2285 been used recently. */
2286 age_cached_comp_units ();
2287
2288 do_cleanups (back_to);
2289}
2290
2291/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2292 the objfile from which this CU came. Returns the resulting symbol
2293 table. */
2fdf6df6 2294
9291a0cd 2295static struct symtab *
a0f42c21 2296dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2297{
95554aad 2298 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2299 if (!per_cu->v.quick->symtab)
2300 {
2301 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2302 increment_reading_symtab ();
a0f42c21 2303 dw2_do_instantiate_symtab (per_cu);
95554aad 2304 process_cu_includes ();
9291a0cd
TT
2305 do_cleanups (back_to);
2306 }
2307 return per_cu->v.quick->symtab;
2308}
2309
f4dc4d17
DE
2310/* Return the CU given its index.
2311
2312 This is intended for loops like:
2313
2314 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2315 + dwarf2_per_objfile->n_type_units); ++i)
2316 {
2317 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2318
2319 ...;
2320 }
2321*/
2fdf6df6 2322
1fd400ff
TT
2323static struct dwarf2_per_cu_data *
2324dw2_get_cu (int index)
2325{
2326 if (index >= dwarf2_per_objfile->n_comp_units)
2327 {
f4dc4d17 2328 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2329 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2330 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2331 }
2332
2333 return dwarf2_per_objfile->all_comp_units[index];
2334}
2335
2336/* Return the primary CU given its index.
2337 The difference between this function and dw2_get_cu is in the handling
2338 of type units (TUs). Here we return the type_unit_group object.
2339
2340 This is intended for loops like:
2341
2342 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2343 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2344 {
2345 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2346
2347 ...;
2348 }
2349*/
2350
2351static struct dwarf2_per_cu_data *
2352dw2_get_primary_cu (int index)
2353{
2354 if (index >= dwarf2_per_objfile->n_comp_units)
2355 {
1fd400ff 2356 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2357 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2358 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2359 }
f4dc4d17 2360
1fd400ff
TT
2361 return dwarf2_per_objfile->all_comp_units[index];
2362}
2363
2ec9a5e0
TT
2364/* A helper for create_cus_from_index that handles a given list of
2365 CUs. */
2fdf6df6 2366
74a0d9f6 2367static void
2ec9a5e0
TT
2368create_cus_from_index_list (struct objfile *objfile,
2369 const gdb_byte *cu_list, offset_type n_elements,
2370 struct dwarf2_section_info *section,
2371 int is_dwz,
2372 int base_offset)
9291a0cd
TT
2373{
2374 offset_type i;
9291a0cd 2375
2ec9a5e0 2376 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2377 {
2378 struct dwarf2_per_cu_data *the_cu;
2379 ULONGEST offset, length;
2380
74a0d9f6
JK
2381 gdb_static_assert (sizeof (ULONGEST) >= 8);
2382 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2383 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2384 cu_list += 2 * 8;
2385
2386 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2387 struct dwarf2_per_cu_data);
b64f50a1 2388 the_cu->offset.sect_off = offset;
9291a0cd
TT
2389 the_cu->length = length;
2390 the_cu->objfile = objfile;
2ec9a5e0 2391 the_cu->info_or_types_section = section;
9291a0cd
TT
2392 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2393 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2394 the_cu->is_dwz = is_dwz;
2395 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2396 }
9291a0cd
TT
2397}
2398
2ec9a5e0 2399/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2400 the CU objects for this objfile. */
2ec9a5e0 2401
74a0d9f6 2402static void
2ec9a5e0
TT
2403create_cus_from_index (struct objfile *objfile,
2404 const gdb_byte *cu_list, offset_type cu_list_elements,
2405 const gdb_byte *dwz_list, offset_type dwz_elements)
2406{
2407 struct dwz_file *dwz;
2408
2409 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2410 dwarf2_per_objfile->all_comp_units
2411 = obstack_alloc (&objfile->objfile_obstack,
2412 dwarf2_per_objfile->n_comp_units
2413 * sizeof (struct dwarf2_per_cu_data *));
2414
74a0d9f6
JK
2415 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2416 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2417
2418 if (dwz_elements == 0)
74a0d9f6 2419 return;
2ec9a5e0
TT
2420
2421 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2422 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2423 cu_list_elements / 2);
2ec9a5e0
TT
2424}
2425
1fd400ff 2426/* Create the signatured type hash table from the index. */
673bfd45 2427
74a0d9f6 2428static void
673bfd45 2429create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2430 struct dwarf2_section_info *section,
673bfd45
DE
2431 const gdb_byte *bytes,
2432 offset_type elements)
1fd400ff
TT
2433{
2434 offset_type i;
673bfd45 2435 htab_t sig_types_hash;
1fd400ff 2436
d467dd73
DE
2437 dwarf2_per_objfile->n_type_units = elements / 3;
2438 dwarf2_per_objfile->all_type_units
1fd400ff 2439 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2440 dwarf2_per_objfile->n_type_units
b4dd5633 2441 * sizeof (struct signatured_type *));
1fd400ff 2442
673bfd45 2443 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2444
2445 for (i = 0; i < elements; i += 3)
2446 {
52dc124a
DE
2447 struct signatured_type *sig_type;
2448 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2449 void **slot;
2450
74a0d9f6
JK
2451 gdb_static_assert (sizeof (ULONGEST) >= 8);
2452 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2453 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2454 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2455 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2456 bytes += 3 * 8;
2457
52dc124a 2458 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2459 struct signatured_type);
52dc124a 2460 sig_type->signature = signature;
3019eac3
DE
2461 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2462 sig_type->per_cu.is_debug_types = 1;
2463 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2464 sig_type->per_cu.offset.sect_off = offset;
2465 sig_type->per_cu.objfile = objfile;
2466 sig_type->per_cu.v.quick
1fd400ff
TT
2467 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2468 struct dwarf2_per_cu_quick_data);
2469
52dc124a
DE
2470 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2471 *slot = sig_type;
1fd400ff 2472
b4dd5633 2473 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2474 }
2475
673bfd45 2476 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2477}
2478
9291a0cd
TT
2479/* Read the address map data from the mapped index, and use it to
2480 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2481
9291a0cd
TT
2482static void
2483create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2484{
2485 const gdb_byte *iter, *end;
2486 struct obstack temp_obstack;
2487 struct addrmap *mutable_map;
2488 struct cleanup *cleanup;
2489 CORE_ADDR baseaddr;
2490
2491 obstack_init (&temp_obstack);
2492 cleanup = make_cleanup_obstack_free (&temp_obstack);
2493 mutable_map = addrmap_create_mutable (&temp_obstack);
2494
2495 iter = index->address_table;
2496 end = iter + index->address_table_size;
2497
2498 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2499
2500 while (iter < end)
2501 {
2502 ULONGEST hi, lo, cu_index;
2503 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2504 iter += 8;
2505 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2506 iter += 8;
2507 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2508 iter += 4;
2509
2510 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2511 dw2_get_cu (cu_index));
9291a0cd
TT
2512 }
2513
2514 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2515 &objfile->objfile_obstack);
2516 do_cleanups (cleanup);
2517}
2518
59d7bcaf
JK
2519/* The hash function for strings in the mapped index. This is the same as
2520 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2521 implementation. This is necessary because the hash function is tied to the
2522 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2523 SYMBOL_HASH_NEXT.
2524
2525 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2526
9291a0cd 2527static hashval_t
559a7a62 2528mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2529{
2530 const unsigned char *str = (const unsigned char *) p;
2531 hashval_t r = 0;
2532 unsigned char c;
2533
2534 while ((c = *str++) != 0)
559a7a62
JK
2535 {
2536 if (index_version >= 5)
2537 c = tolower (c);
2538 r = r * 67 + c - 113;
2539 }
9291a0cd
TT
2540
2541 return r;
2542}
2543
2544/* Find a slot in the mapped index INDEX for the object named NAME.
2545 If NAME is found, set *VEC_OUT to point to the CU vector in the
2546 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2547
9291a0cd
TT
2548static int
2549find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2550 offset_type **vec_out)
2551{
0cf03b49
JK
2552 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2553 offset_type hash;
9291a0cd 2554 offset_type slot, step;
559a7a62 2555 int (*cmp) (const char *, const char *);
9291a0cd 2556
0cf03b49
JK
2557 if (current_language->la_language == language_cplus
2558 || current_language->la_language == language_java
2559 || current_language->la_language == language_fortran)
2560 {
2561 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2562 not contain any. */
2563 const char *paren = strchr (name, '(');
2564
2565 if (paren)
2566 {
2567 char *dup;
2568
2569 dup = xmalloc (paren - name + 1);
2570 memcpy (dup, name, paren - name);
2571 dup[paren - name] = 0;
2572
2573 make_cleanup (xfree, dup);
2574 name = dup;
2575 }
2576 }
2577
559a7a62 2578 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2579 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2580 simulate our NAME being searched is also lowercased. */
2581 hash = mapped_index_string_hash ((index->version == 4
2582 && case_sensitivity == case_sensitive_off
2583 ? 5 : index->version),
2584 name);
2585
3876f04e
DE
2586 slot = hash & (index->symbol_table_slots - 1);
2587 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2588 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2589
2590 for (;;)
2591 {
2592 /* Convert a slot number to an offset into the table. */
2593 offset_type i = 2 * slot;
2594 const char *str;
3876f04e 2595 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2596 {
2597 do_cleanups (back_to);
2598 return 0;
2599 }
9291a0cd 2600
3876f04e 2601 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2602 if (!cmp (name, str))
9291a0cd
TT
2603 {
2604 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2605 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2606 do_cleanups (back_to);
9291a0cd
TT
2607 return 1;
2608 }
2609
3876f04e 2610 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2611 }
2612}
2613
2ec9a5e0
TT
2614/* A helper function that reads the .gdb_index from SECTION and fills
2615 in MAP. FILENAME is the name of the file containing the section;
2616 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2617 ok to use deprecated sections.
2618
2619 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2620 out parameters that are filled in with information about the CU and
2621 TU lists in the section.
2622
2623 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2624
9291a0cd 2625static int
2ec9a5e0
TT
2626read_index_from_section (struct objfile *objfile,
2627 const char *filename,
2628 int deprecated_ok,
2629 struct dwarf2_section_info *section,
2630 struct mapped_index *map,
2631 const gdb_byte **cu_list,
2632 offset_type *cu_list_elements,
2633 const gdb_byte **types_list,
2634 offset_type *types_list_elements)
9291a0cd 2635{
9291a0cd 2636 char *addr;
2ec9a5e0 2637 offset_type version;
b3b272e1 2638 offset_type *metadata;
1fd400ff 2639 int i;
9291a0cd 2640
2ec9a5e0 2641 if (dwarf2_section_empty_p (section))
9291a0cd 2642 return 0;
82430852
JK
2643
2644 /* Older elfutils strip versions could keep the section in the main
2645 executable while splitting it for the separate debug info file. */
2ec9a5e0 2646 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2647 return 0;
2648
2ec9a5e0 2649 dwarf2_read_section (objfile, section);
9291a0cd 2650
2ec9a5e0 2651 addr = section->buffer;
9291a0cd 2652 /* Version check. */
1fd400ff 2653 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2654 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2655 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2656 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2657 indices. */
831adc1f 2658 if (version < 4)
481860b3
GB
2659 {
2660 static int warning_printed = 0;
2661 if (!warning_printed)
2662 {
2663 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2664 filename);
481860b3
GB
2665 warning_printed = 1;
2666 }
2667 return 0;
2668 }
2669 /* Index version 4 uses a different hash function than index version
2670 5 and later.
2671
2672 Versions earlier than 6 did not emit psymbols for inlined
2673 functions. Using these files will cause GDB not to be able to
2674 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2675 indices unless the user has done
2676 "set use-deprecated-index-sections on". */
2ec9a5e0 2677 if (version < 6 && !deprecated_ok)
481860b3
GB
2678 {
2679 static int warning_printed = 0;
2680 if (!warning_printed)
2681 {
e615022a
DE
2682 warning (_("\
2683Skipping deprecated .gdb_index section in %s.\n\
2684Do \"set use-deprecated-index-sections on\" before the file is read\n\
2685to use the section anyway."),
2ec9a5e0 2686 filename);
481860b3
GB
2687 warning_printed = 1;
2688 }
2689 return 0;
2690 }
2691 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2692 longer backward compatible. */
156942c7 2693 if (version > 7)
594e8718 2694 return 0;
9291a0cd 2695
559a7a62 2696 map->version = version;
2ec9a5e0 2697 map->total_size = section->size;
9291a0cd
TT
2698
2699 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2700
2701 i = 0;
2ec9a5e0
TT
2702 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2703 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2704 / 8);
1fd400ff
TT
2705 ++i;
2706
2ec9a5e0
TT
2707 *types_list = addr + MAYBE_SWAP (metadata[i]);
2708 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2709 - MAYBE_SWAP (metadata[i]))
2710 / 8);
987d643c 2711 ++i;
1fd400ff
TT
2712
2713 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2714 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2715 - MAYBE_SWAP (metadata[i]));
2716 ++i;
2717
3876f04e
DE
2718 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2719 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2720 - MAYBE_SWAP (metadata[i]))
2721 / (2 * sizeof (offset_type)));
1fd400ff 2722 ++i;
9291a0cd 2723
1fd400ff
TT
2724 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2725
2ec9a5e0
TT
2726 return 1;
2727}
2728
2729
2730/* Read the index file. If everything went ok, initialize the "quick"
2731 elements of all the CUs and return 1. Otherwise, return 0. */
2732
2733static int
2734dwarf2_read_index (struct objfile *objfile)
2735{
2736 struct mapped_index local_map, *map;
2737 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2738 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2739
2740 if (!read_index_from_section (objfile, objfile->name,
2741 use_deprecated_index_sections,
2742 &dwarf2_per_objfile->gdb_index, &local_map,
2743 &cu_list, &cu_list_elements,
2744 &types_list, &types_list_elements))
2745 return 0;
2746
0fefef59 2747 /* Don't use the index if it's empty. */
2ec9a5e0 2748 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2749 return 0;
2750
2ec9a5e0
TT
2751 /* If there is a .dwz file, read it so we can get its CU list as
2752 well. */
2753 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2754 {
2755 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2756 struct mapped_index dwz_map;
2757 const gdb_byte *dwz_types_ignore;
2758 offset_type dwz_types_elements_ignore;
2759
2760 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2761 1,
2762 &dwz->gdb_index, &dwz_map,
2763 &dwz_list, &dwz_list_elements,
2764 &dwz_types_ignore,
2765 &dwz_types_elements_ignore))
2766 {
2767 warning (_("could not read '.gdb_index' section from %s; skipping"),
2768 bfd_get_filename (dwz->dwz_bfd));
2769 return 0;
2770 }
2771 }
2772
74a0d9f6
JK
2773 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2774 dwz_list_elements);
1fd400ff 2775
8b70b953
TT
2776 if (types_list_elements)
2777 {
2778 struct dwarf2_section_info *section;
2779
2780 /* We can only handle a single .debug_types when we have an
2781 index. */
2782 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2783 return 0;
2784
2785 section = VEC_index (dwarf2_section_info_def,
2786 dwarf2_per_objfile->types, 0);
2787
74a0d9f6
JK
2788 create_signatured_type_table_from_index (objfile, section, types_list,
2789 types_list_elements);
8b70b953 2790 }
9291a0cd 2791
2ec9a5e0
TT
2792 create_addrmap_from_index (objfile, &local_map);
2793
2794 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2795 *map = local_map;
9291a0cd
TT
2796
2797 dwarf2_per_objfile->index_table = map;
2798 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2799 dwarf2_per_objfile->quick_file_names_table =
2800 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2801
2802 return 1;
2803}
2804
2805/* A helper for the "quick" functions which sets the global
2806 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2807
9291a0cd
TT
2808static void
2809dw2_setup (struct objfile *objfile)
2810{
2811 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2812 gdb_assert (dwarf2_per_objfile);
2813}
2814
f4dc4d17
DE
2815/* Reader function for dw2_build_type_unit_groups. */
2816
2817static void
2818dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2819 gdb_byte *info_ptr,
2820 struct die_info *type_unit_die,
2821 int has_children,
2822 void *data)
2823{
2824 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2825 struct attribute *attr;
2826 struct type_unit_group *tu_group;
2827
2828 gdb_assert (data == NULL);
2829
2830 if (! has_children)
2831 return;
2832
2833 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2834 /* Call this for its side-effect of creating the associated
2835 struct type_unit_group if it doesn't already exist. */
094b34ac 2836 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2837}
2838
2839/* Build dwarf2_per_objfile->type_unit_groups.
2840 This function may be called multiple times. */
2841
2842static void
2843dw2_build_type_unit_groups (void)
2844{
2845 if (dwarf2_per_objfile->type_unit_groups == NULL)
2846 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2847}
2848
dee91e82 2849/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2850
dee91e82
DE
2851static void
2852dw2_get_file_names_reader (const struct die_reader_specs *reader,
2853 gdb_byte *info_ptr,
2854 struct die_info *comp_unit_die,
2855 int has_children,
2856 void *data)
9291a0cd 2857{
dee91e82
DE
2858 struct dwarf2_cu *cu = reader->cu;
2859 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2860 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2861 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2862 struct line_header *lh;
9291a0cd 2863 struct attribute *attr;
dee91e82 2864 int i;
9291a0cd 2865 char *name, *comp_dir;
7b9f3c50
DE
2866 void **slot;
2867 struct quick_file_names *qfn;
2868 unsigned int line_offset;
9291a0cd 2869
07261596
TT
2870 /* Our callers never want to match partial units -- instead they
2871 will match the enclosing full CU. */
2872 if (comp_unit_die->tag == DW_TAG_partial_unit)
2873 {
2874 this_cu->v.quick->no_file_data = 1;
2875 return;
2876 }
2877
094b34ac
DE
2878 /* If we're reading the line header for TUs, store it in the "per_cu"
2879 for tu_group. */
2880 if (this_cu->is_debug_types)
2881 {
2882 struct type_unit_group *tu_group = data;
2883
2884 gdb_assert (tu_group != NULL);
2885 lh_cu = &tu_group->per_cu;
2886 }
2887 else
2888 lh_cu = this_cu;
2889
7b9f3c50
DE
2890 lh = NULL;
2891 slot = NULL;
2892 line_offset = 0;
dee91e82
DE
2893
2894 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2895 if (attr)
2896 {
7b9f3c50
DE
2897 struct quick_file_names find_entry;
2898
2899 line_offset = DW_UNSND (attr);
2900
2901 /* We may have already read in this line header (TU line header sharing).
2902 If we have we're done. */
094b34ac
DE
2903 find_entry.hash.dwo_unit = cu->dwo_unit;
2904 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2905 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2906 &find_entry, INSERT);
2907 if (*slot != NULL)
2908 {
094b34ac 2909 lh_cu->v.quick->file_names = *slot;
dee91e82 2910 return;
7b9f3c50
DE
2911 }
2912
3019eac3 2913 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2914 }
2915 if (lh == NULL)
2916 {
094b34ac 2917 lh_cu->v.quick->no_file_data = 1;
dee91e82 2918 return;
9291a0cd
TT
2919 }
2920
7b9f3c50 2921 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2922 qfn->hash.dwo_unit = cu->dwo_unit;
2923 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2924 gdb_assert (slot != NULL);
2925 *slot = qfn;
9291a0cd 2926
dee91e82 2927 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2928
7b9f3c50
DE
2929 qfn->num_file_names = lh->num_file_names;
2930 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2931 lh->num_file_names * sizeof (char *));
9291a0cd 2932 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2933 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2934 qfn->real_names = NULL;
9291a0cd 2935
7b9f3c50 2936 free_line_header (lh);
7b9f3c50 2937
094b34ac 2938 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2939}
2940
2941/* A helper for the "quick" functions which attempts to read the line
2942 table for THIS_CU. */
2943
2944static struct quick_file_names *
2945dw2_get_file_names (struct objfile *objfile,
2946 struct dwarf2_per_cu_data *this_cu)
2947{
f4dc4d17
DE
2948 /* For TUs this should only be called on the parent group. */
2949 if (this_cu->is_debug_types)
2950 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2951
dee91e82
DE
2952 if (this_cu->v.quick->file_names != NULL)
2953 return this_cu->v.quick->file_names;
2954 /* If we know there is no line data, no point in looking again. */
2955 if (this_cu->v.quick->no_file_data)
2956 return NULL;
2957
3019eac3
DE
2958 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2959 in the stub for CUs, there's is no need to lookup the DWO file.
2960 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2961 DWO file. */
2962 if (this_cu->is_debug_types)
094b34ac
DE
2963 {
2964 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2965
2966 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2967 dw2_get_file_names_reader, tu_group);
2968 }
3019eac3
DE
2969 else
2970 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2971
2972 if (this_cu->v.quick->no_file_data)
2973 return NULL;
2974 return this_cu->v.quick->file_names;
9291a0cd
TT
2975}
2976
2977/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2978 real path for a given file name from the line table. */
2fdf6df6 2979
9291a0cd 2980static const char *
7b9f3c50
DE
2981dw2_get_real_path (struct objfile *objfile,
2982 struct quick_file_names *qfn, int index)
9291a0cd 2983{
7b9f3c50
DE
2984 if (qfn->real_names == NULL)
2985 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2986 qfn->num_file_names, sizeof (char *));
9291a0cd 2987
7b9f3c50
DE
2988 if (qfn->real_names[index] == NULL)
2989 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2990
7b9f3c50 2991 return qfn->real_names[index];
9291a0cd
TT
2992}
2993
2994static struct symtab *
2995dw2_find_last_source_symtab (struct objfile *objfile)
2996{
2997 int index;
ae2de4f8 2998
9291a0cd
TT
2999 dw2_setup (objfile);
3000 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3001 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3002}
3003
7b9f3c50
DE
3004/* Traversal function for dw2_forget_cached_source_info. */
3005
3006static int
3007dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3008{
7b9f3c50 3009 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3010
7b9f3c50 3011 if (file_data->real_names)
9291a0cd 3012 {
7b9f3c50 3013 int i;
9291a0cd 3014
7b9f3c50 3015 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3016 {
7b9f3c50
DE
3017 xfree ((void*) file_data->real_names[i]);
3018 file_data->real_names[i] = NULL;
9291a0cd
TT
3019 }
3020 }
7b9f3c50
DE
3021
3022 return 1;
3023}
3024
3025static void
3026dw2_forget_cached_source_info (struct objfile *objfile)
3027{
3028 dw2_setup (objfile);
3029
3030 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3031 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3032}
3033
f8eba3c6
TT
3034/* Helper function for dw2_map_symtabs_matching_filename that expands
3035 the symtabs and calls the iterator. */
3036
3037static int
3038dw2_map_expand_apply (struct objfile *objfile,
3039 struct dwarf2_per_cu_data *per_cu,
3040 const char *name,
3041 const char *full_path, const char *real_path,
3042 int (*callback) (struct symtab *, void *),
3043 void *data)
3044{
3045 struct symtab *last_made = objfile->symtabs;
3046
3047 /* Don't visit already-expanded CUs. */
3048 if (per_cu->v.quick->symtab)
3049 return 0;
3050
3051 /* This may expand more than one symtab, and we want to iterate over
3052 all of them. */
a0f42c21 3053 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3054
3055 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3056 objfile->symtabs, last_made);
3057}
3058
3059/* Implementation of the map_symtabs_matching_filename method. */
3060
9291a0cd 3061static int
f8eba3c6
TT
3062dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3063 const char *full_path, const char *real_path,
3064 int (*callback) (struct symtab *, void *),
3065 void *data)
9291a0cd
TT
3066{
3067 int i;
c011a4f4 3068 const char *name_basename = lbasename (name);
4aac40c8
TT
3069 int name_len = strlen (name);
3070 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3071
3072 dw2_setup (objfile);
ae2de4f8 3073
f4dc4d17
DE
3074 dw2_build_type_unit_groups ();
3075
1fd400ff 3076 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3077 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3078 {
3079 int j;
f4dc4d17 3080 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3081 struct quick_file_names *file_data;
9291a0cd 3082
3d7bb9d9 3083 /* We only need to look at symtabs not already expanded. */
e254ef6a 3084 if (per_cu->v.quick->symtab)
9291a0cd
TT
3085 continue;
3086
7b9f3c50
DE
3087 file_data = dw2_get_file_names (objfile, per_cu);
3088 if (file_data == NULL)
9291a0cd
TT
3089 continue;
3090
7b9f3c50 3091 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3092 {
7b9f3c50 3093 const char *this_name = file_data->file_names[j];
9291a0cd 3094
4aac40c8
TT
3095 if (FILENAME_CMP (name, this_name) == 0
3096 || (!is_abs && compare_filenames_for_search (this_name,
3097 name, name_len)))
9291a0cd 3098 {
f8eba3c6
TT
3099 if (dw2_map_expand_apply (objfile, per_cu,
3100 name, full_path, real_path,
3101 callback, data))
3102 return 1;
4aac40c8 3103 }
9291a0cd 3104
c011a4f4
DE
3105 /* Before we invoke realpath, which can get expensive when many
3106 files are involved, do a quick comparison of the basenames. */
3107 if (! basenames_may_differ
3108 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3109 continue;
3110
9291a0cd
TT
3111 if (full_path != NULL)
3112 {
7b9f3c50
DE
3113 const char *this_real_name = dw2_get_real_path (objfile,
3114 file_data, j);
9291a0cd 3115
7b9f3c50 3116 if (this_real_name != NULL
4aac40c8
TT
3117 && (FILENAME_CMP (full_path, this_real_name) == 0
3118 || (!is_abs
3119 && compare_filenames_for_search (this_real_name,
3120 name, name_len))))
9291a0cd 3121 {
f8eba3c6
TT
3122 if (dw2_map_expand_apply (objfile, per_cu,
3123 name, full_path, real_path,
3124 callback, data))
3125 return 1;
9291a0cd
TT
3126 }
3127 }
3128
3129 if (real_path != NULL)
3130 {
7b9f3c50
DE
3131 const char *this_real_name = dw2_get_real_path (objfile,
3132 file_data, j);
9291a0cd 3133
7b9f3c50 3134 if (this_real_name != NULL
4aac40c8
TT
3135 && (FILENAME_CMP (real_path, this_real_name) == 0
3136 || (!is_abs
3137 && compare_filenames_for_search (this_real_name,
3138 name, name_len))))
9291a0cd 3139 {
f8eba3c6
TT
3140 if (dw2_map_expand_apply (objfile, per_cu,
3141 name, full_path, real_path,
3142 callback, data))
3143 return 1;
9291a0cd
TT
3144 }
3145 }
3146 }
3147 }
3148
9291a0cd
TT
3149 return 0;
3150}
3151
3152static struct symtab *
3153dw2_lookup_symbol (struct objfile *objfile, int block_index,
3154 const char *name, domain_enum domain)
3155{
774b6a14 3156 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
3157 instead. */
3158 return NULL;
3159}
3160
3161/* A helper function that expands all symtabs that hold an object
156942c7
DE
3162 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3163 symbols in block BLOCK_KIND. */
2fdf6df6 3164
9291a0cd 3165static void
156942c7
DE
3166dw2_do_expand_symtabs_matching (struct objfile *objfile,
3167 int want_specific_block,
3168 enum block_enum block_kind,
3169 const char *name, domain_enum domain)
9291a0cd 3170{
156942c7
DE
3171 struct mapped_index *index;
3172
9291a0cd
TT
3173 dw2_setup (objfile);
3174
156942c7
DE
3175 index = dwarf2_per_objfile->index_table;
3176
ae2de4f8 3177 /* index_table is NULL if OBJF_READNOW. */
156942c7 3178 if (index)
9291a0cd
TT
3179 {
3180 offset_type *vec;
3181
156942c7 3182 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
3183 {
3184 offset_type i, len = MAYBE_SWAP (*vec);
3185 for (i = 0; i < len; ++i)
3186 {
156942c7
DE
3187 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3188 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 3189 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
3190 int want_static = block_kind != GLOBAL_BLOCK;
3191 /* This value is only valid for index versions >= 7. */
3192 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3193 gdb_index_symbol_kind symbol_kind =
3194 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
83a788b8
DE
3195 /* Only check the symbol attributes if they're present.
3196 Indices prior to version 7 don't record them,
3197 and indices >= 7 may elide them for certain symbols
3198 (gold does this). */
3199 int attrs_valid =
3200 (index->version >= 7
3201 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3202
3203 if (attrs_valid
3204 && want_specific_block
156942c7
DE
3205 && want_static != is_static)
3206 continue;
3207
83a788b8
DE
3208 /* Only check the symbol's kind if it has one. */
3209 if (attrs_valid)
156942c7
DE
3210 {
3211 switch (domain)
3212 {
3213 case VAR_DOMAIN:
3214 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3215 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3216 /* Some types are also in VAR_DOMAIN. */
3217 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3218 continue;
3219 break;
3220 case STRUCT_DOMAIN:
3221 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3222 continue;
3223 break;
3224 case LABEL_DOMAIN:
3225 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3226 continue;
3227 break;
3228 default:
3229 break;
3230 }
3231 }
1fd400ff 3232
a0f42c21 3233 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3234 }
3235 }
3236 }
3237}
3238
774b6a14
TT
3239static void
3240dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 3241 enum block_enum block_kind, const char *name,
774b6a14 3242 domain_enum domain)
9291a0cd 3243{
156942c7 3244 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
3245}
3246
3247static void
3248dw2_print_stats (struct objfile *objfile)
3249{
3250 int i, count;
3251
3252 dw2_setup (objfile);
3253 count = 0;
1fd400ff 3254 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3255 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3256 {
e254ef6a 3257 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3258
e254ef6a 3259 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3260 ++count;
3261 }
3262 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3263}
3264
3265static void
3266dw2_dump (struct objfile *objfile)
3267{
3268 /* Nothing worth printing. */
3269}
3270
3271static void
3272dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3273 struct section_offsets *delta)
3274{
3275 /* There's nothing to relocate here. */
3276}
3277
3278static void
3279dw2_expand_symtabs_for_function (struct objfile *objfile,
3280 const char *func_name)
3281{
156942c7
DE
3282 /* Note: It doesn't matter what we pass for block_kind here. */
3283 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3284 VAR_DOMAIN);
9291a0cd
TT
3285}
3286
3287static void
3288dw2_expand_all_symtabs (struct objfile *objfile)
3289{
3290 int i;
3291
3292 dw2_setup (objfile);
1fd400ff
TT
3293
3294 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3295 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3296 {
e254ef6a 3297 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3298
a0f42c21 3299 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3300 }
3301}
3302
3303static void
3304dw2_expand_symtabs_with_filename (struct objfile *objfile,
3305 const char *filename)
3306{
3307 int i;
3308
3309 dw2_setup (objfile);
d4637a04
DE
3310
3311 /* We don't need to consider type units here.
3312 This is only called for examining code, e.g. expand_line_sal.
3313 There can be an order of magnitude (or more) more type units
3314 than comp units, and we avoid them if we can. */
3315
3316 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3317 {
3318 int j;
e254ef6a 3319 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3320 struct quick_file_names *file_data;
9291a0cd 3321
3d7bb9d9 3322 /* We only need to look at symtabs not already expanded. */
e254ef6a 3323 if (per_cu->v.quick->symtab)
9291a0cd
TT
3324 continue;
3325
7b9f3c50
DE
3326 file_data = dw2_get_file_names (objfile, per_cu);
3327 if (file_data == NULL)
9291a0cd
TT
3328 continue;
3329
7b9f3c50 3330 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3331 {
7b9f3c50 3332 const char *this_name = file_data->file_names[j];
1ef75ecc 3333 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3334 {
a0f42c21 3335 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3336 break;
3337 }
3338 }
3339 }
3340}
3341
356d9f9d
TT
3342/* A helper function for dw2_find_symbol_file that finds the primary
3343 file name for a given CU. This is a die_reader_func. */
3344
3345static void
3346dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3347 gdb_byte *info_ptr,
3348 struct die_info *comp_unit_die,
3349 int has_children,
3350 void *data)
3351{
3352 const char **result_ptr = data;
3353 struct dwarf2_cu *cu = reader->cu;
3354 struct attribute *attr;
3355
3356 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3357 if (attr == NULL)
3358 *result_ptr = NULL;
3359 else
3360 *result_ptr = DW_STRING (attr);
3361}
3362
dd786858 3363static const char *
9291a0cd
TT
3364dw2_find_symbol_file (struct objfile *objfile, const char *name)
3365{
e254ef6a 3366 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3367 offset_type *vec;
356d9f9d 3368 const char *filename;
9291a0cd
TT
3369
3370 dw2_setup (objfile);
3371
ae2de4f8 3372 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3373 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3374 {
3375 struct symtab *s;
3376
d790cf0a
DE
3377 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3378 {
3379 struct blockvector *bv = BLOCKVECTOR (s);
3380 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3381 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3382
3383 if (sym)
210bbc17 3384 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3385 }
96408a79
SA
3386 return NULL;
3387 }
9291a0cd
TT
3388
3389 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3390 name, &vec))
3391 return NULL;
3392
3393 /* Note that this just looks at the very first one named NAME -- but
3394 actually we are looking for a function. find_main_filename
3395 should be rewritten so that it doesn't require a custom hook. It
3396 could just use the ordinary symbol tables. */
3397 /* vec[0] is the length, which must always be >0. */
156942c7 3398 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3399
356d9f9d
TT
3400 if (per_cu->v.quick->symtab != NULL)
3401 return per_cu->v.quick->symtab->filename;
3402
f4dc4d17
DE
3403 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3404 dw2_get_primary_filename_reader, &filename);
9291a0cd 3405
356d9f9d 3406 return filename;
9291a0cd
TT
3407}
3408
3409static void
40658b94
PH
3410dw2_map_matching_symbols (const char * name, domain_enum namespace,
3411 struct objfile *objfile, int global,
3412 int (*callback) (struct block *,
3413 struct symbol *, void *),
2edb89d3
JK
3414 void *data, symbol_compare_ftype *match,
3415 symbol_compare_ftype *ordered_compare)
9291a0cd 3416{
40658b94 3417 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3418 current language is Ada for a non-Ada objfile using GNU index. As Ada
3419 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3420}
3421
3422static void
f8eba3c6
TT
3423dw2_expand_symtabs_matching
3424 (struct objfile *objfile,
3425 int (*file_matcher) (const char *, void *),
e078317b 3426 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3427 enum search_domain kind,
3428 void *data)
9291a0cd
TT
3429{
3430 int i;
3431 offset_type iter;
4b5246aa 3432 struct mapped_index *index;
9291a0cd
TT
3433
3434 dw2_setup (objfile);
ae2de4f8
DE
3435
3436 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3437 if (!dwarf2_per_objfile->index_table)
3438 return;
4b5246aa 3439 index = dwarf2_per_objfile->index_table;
9291a0cd 3440
7b08b9eb 3441 if (file_matcher != NULL)
24c79950
TT
3442 {
3443 struct cleanup *cleanup;
3444 htab_t visited_found, visited_not_found;
3445
f4dc4d17
DE
3446 dw2_build_type_unit_groups ();
3447
24c79950
TT
3448 visited_found = htab_create_alloc (10,
3449 htab_hash_pointer, htab_eq_pointer,
3450 NULL, xcalloc, xfree);
3451 cleanup = make_cleanup_htab_delete (visited_found);
3452 visited_not_found = htab_create_alloc (10,
3453 htab_hash_pointer, htab_eq_pointer,
3454 NULL, xcalloc, xfree);
3455 make_cleanup_htab_delete (visited_not_found);
3456
3457 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3458 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3459 {
3460 int j;
f4dc4d17 3461 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3462 struct quick_file_names *file_data;
3463 void **slot;
7b08b9eb 3464
24c79950 3465 per_cu->v.quick->mark = 0;
3d7bb9d9 3466
24c79950
TT
3467 /* We only need to look at symtabs not already expanded. */
3468 if (per_cu->v.quick->symtab)
3469 continue;
7b08b9eb 3470
24c79950
TT
3471 file_data = dw2_get_file_names (objfile, per_cu);
3472 if (file_data == NULL)
3473 continue;
7b08b9eb 3474
24c79950
TT
3475 if (htab_find (visited_not_found, file_data) != NULL)
3476 continue;
3477 else if (htab_find (visited_found, file_data) != NULL)
3478 {
3479 per_cu->v.quick->mark = 1;
3480 continue;
3481 }
3482
3483 for (j = 0; j < file_data->num_file_names; ++j)
3484 {
3485 if (file_matcher (file_data->file_names[j], data))
3486 {
3487 per_cu->v.quick->mark = 1;
3488 break;
3489 }
3490 }
3491
3492 slot = htab_find_slot (per_cu->v.quick->mark
3493 ? visited_found
3494 : visited_not_found,
3495 file_data, INSERT);
3496 *slot = file_data;
3497 }
3498
3499 do_cleanups (cleanup);
3500 }
9291a0cd 3501
3876f04e 3502 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3503 {
3504 offset_type idx = 2 * iter;
3505 const char *name;
3506 offset_type *vec, vec_len, vec_idx;
3507
3876f04e 3508 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3509 continue;
3510
3876f04e 3511 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3512
e078317b 3513 if (! (*name_matcher) (name, data))
9291a0cd
TT
3514 continue;
3515
3516 /* The name was matched, now expand corresponding CUs that were
3517 marked. */
4b5246aa 3518 vec = (offset_type *) (index->constant_pool
3876f04e 3519 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3520 vec_len = MAYBE_SWAP (vec[0]);
3521 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3522 {
e254ef6a 3523 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3524 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3525 gdb_index_symbol_kind symbol_kind =
3526 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3527 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3528
3529 /* Don't crash on bad data. */
3530 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3531 + dwarf2_per_objfile->n_type_units))
156942c7 3532 continue;
1fd400ff 3533
156942c7
DE
3534 /* Only check the symbol's kind if it has one.
3535 Indices prior to version 7 don't record it. */
3536 if (index->version >= 7)
3537 {
3538 switch (kind)
3539 {
3540 case VARIABLES_DOMAIN:
3541 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3542 continue;
3543 break;
3544 case FUNCTIONS_DOMAIN:
3545 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3546 continue;
3547 break;
3548 case TYPES_DOMAIN:
3549 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3550 continue;
3551 break;
3552 default:
3553 break;
3554 }
3555 }
3556
3557 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3558 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3559 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3560 }
3561 }
3562}
3563
9703b513
TT
3564/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3565 symtab. */
3566
3567static struct symtab *
3568recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3569{
3570 int i;
3571
3572 if (BLOCKVECTOR (symtab) != NULL
3573 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3574 return symtab;
3575
a3ec0bb1
DE
3576 if (symtab->includes == NULL)
3577 return NULL;
3578
9703b513
TT
3579 for (i = 0; symtab->includes[i]; ++i)
3580 {
a3ec0bb1 3581 struct symtab *s = symtab->includes[i];
9703b513
TT
3582
3583 s = recursively_find_pc_sect_symtab (s, pc);
3584 if (s != NULL)
3585 return s;
3586 }
3587
3588 return NULL;
3589}
3590
9291a0cd
TT
3591static struct symtab *
3592dw2_find_pc_sect_symtab (struct objfile *objfile,
3593 struct minimal_symbol *msymbol,
3594 CORE_ADDR pc,
3595 struct obj_section *section,
3596 int warn_if_readin)
3597{
3598 struct dwarf2_per_cu_data *data;
9703b513 3599 struct symtab *result;
9291a0cd
TT
3600
3601 dw2_setup (objfile);
3602
3603 if (!objfile->psymtabs_addrmap)
3604 return NULL;
3605
3606 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3607 if (!data)
3608 return NULL;
3609
3610 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3611 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3612 paddress (get_objfile_arch (objfile), pc));
3613
9703b513
TT
3614 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3615 gdb_assert (result != NULL);
3616 return result;
9291a0cd
TT
3617}
3618
9291a0cd 3619static void
44b13c5a 3620dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3621 void *data, int need_fullname)
9291a0cd
TT
3622{
3623 int i;
24c79950
TT
3624 struct cleanup *cleanup;
3625 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3626 NULL, xcalloc, xfree);
9291a0cd 3627
24c79950 3628 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3629 dw2_setup (objfile);
ae2de4f8 3630
f4dc4d17
DE
3631 dw2_build_type_unit_groups ();
3632
24c79950
TT
3633 /* We can ignore file names coming from already-expanded CUs. */
3634 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3635 + dwarf2_per_objfile->n_type_units); ++i)
3636 {
3637 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3638
3639 if (per_cu->v.quick->symtab)
3640 {
3641 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3642 INSERT);
3643
3644 *slot = per_cu->v.quick->file_names;
3645 }
3646 }
3647
1fd400ff 3648 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3649 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3650 {
3651 int j;
f4dc4d17 3652 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3653 struct quick_file_names *file_data;
24c79950 3654 void **slot;
9291a0cd 3655
3d7bb9d9 3656 /* We only need to look at symtabs not already expanded. */
e254ef6a 3657 if (per_cu->v.quick->symtab)
9291a0cd
TT
3658 continue;
3659
7b9f3c50
DE
3660 file_data = dw2_get_file_names (objfile, per_cu);
3661 if (file_data == NULL)
9291a0cd
TT
3662 continue;
3663
24c79950
TT
3664 slot = htab_find_slot (visited, file_data, INSERT);
3665 if (*slot)
3666 {
3667 /* Already visited. */
3668 continue;
3669 }
3670 *slot = file_data;
3671
7b9f3c50 3672 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3673 {
74e2f255
DE
3674 const char *this_real_name;
3675
3676 if (need_fullname)
3677 this_real_name = dw2_get_real_path (objfile, file_data, j);
3678 else
3679 this_real_name = NULL;
7b9f3c50 3680 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3681 }
3682 }
24c79950
TT
3683
3684 do_cleanups (cleanup);
9291a0cd
TT
3685}
3686
3687static int
3688dw2_has_symbols (struct objfile *objfile)
3689{
3690 return 1;
3691}
3692
3693const struct quick_symbol_functions dwarf2_gdb_index_functions =
3694{
3695 dw2_has_symbols,
3696 dw2_find_last_source_symtab,
3697 dw2_forget_cached_source_info,
f8eba3c6 3698 dw2_map_symtabs_matching_filename,
9291a0cd 3699 dw2_lookup_symbol,
774b6a14 3700 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3701 dw2_print_stats,
3702 dw2_dump,
3703 dw2_relocate,
3704 dw2_expand_symtabs_for_function,
3705 dw2_expand_all_symtabs,
3706 dw2_expand_symtabs_with_filename,
3707 dw2_find_symbol_file,
40658b94 3708 dw2_map_matching_symbols,
9291a0cd
TT
3709 dw2_expand_symtabs_matching,
3710 dw2_find_pc_sect_symtab,
9291a0cd
TT
3711 dw2_map_symbol_filenames
3712};
3713
3714/* Initialize for reading DWARF for this objfile. Return 0 if this
3715 file will use psymtabs, or 1 if using the GNU index. */
3716
3717int
3718dwarf2_initialize_objfile (struct objfile *objfile)
3719{
3720 /* If we're about to read full symbols, don't bother with the
3721 indices. In this case we also don't care if some other debug
3722 format is making psymtabs, because they are all about to be
3723 expanded anyway. */
3724 if ((objfile->flags & OBJF_READNOW))
3725 {
3726 int i;
3727
3728 dwarf2_per_objfile->using_index = 1;
3729 create_all_comp_units (objfile);
0e50663e 3730 create_all_type_units (objfile);
7b9f3c50
DE
3731 dwarf2_per_objfile->quick_file_names_table =
3732 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3733
1fd400ff 3734 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3735 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3736 {
e254ef6a 3737 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3738
e254ef6a
DE
3739 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3740 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3741 }
3742
3743 /* Return 1 so that gdb sees the "quick" functions. However,
3744 these functions will be no-ops because we will have expanded
3745 all symtabs. */
3746 return 1;
3747 }
3748
3749 if (dwarf2_read_index (objfile))
3750 return 1;
3751
9291a0cd
TT
3752 return 0;
3753}
3754
3755\f
3756
dce234bc
PP
3757/* Build a partial symbol table. */
3758
3759void
f29dff0a 3760dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3761{
f29dff0a 3762 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3763 {
3764 init_psymbol_list (objfile, 1024);
3765 }
3766
d146bf1e 3767 dwarf2_build_psymtabs_hard (objfile);
c906108c 3768}
c906108c 3769
1ce1cefd
DE
3770/* Return the total length of the CU described by HEADER. */
3771
3772static unsigned int
3773get_cu_length (const struct comp_unit_head *header)
3774{
3775 return header->initial_length_size + header->length;
3776}
3777
45452591
DE
3778/* Return TRUE if OFFSET is within CU_HEADER. */
3779
3780static inline int
b64f50a1 3781offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3782{
b64f50a1 3783 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3784 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3785
b64f50a1 3786 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3787}
3788
3b80fe9b
DE
3789/* Find the base address of the compilation unit for range lists and
3790 location lists. It will normally be specified by DW_AT_low_pc.
3791 In DWARF-3 draft 4, the base address could be overridden by
3792 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3793 compilation units with discontinuous ranges. */
3794
3795static void
3796dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3797{
3798 struct attribute *attr;
3799
3800 cu->base_known = 0;
3801 cu->base_address = 0;
3802
3803 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3804 if (attr)
3805 {
3806 cu->base_address = DW_ADDR (attr);
3807 cu->base_known = 1;
3808 }
3809 else
3810 {
3811 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3812 if (attr)
3813 {
3814 cu->base_address = DW_ADDR (attr);
3815 cu->base_known = 1;
3816 }
3817 }
3818}
3819
93311388
DE
3820/* Read in the comp unit header information from the debug_info at info_ptr.
3821 NOTE: This leaves members offset, first_die_offset to be filled in
3822 by the caller. */
107d2387 3823
fe1b8b76 3824static gdb_byte *
107d2387 3825read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3826 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3827{
3828 int signed_addr;
891d2f0b 3829 unsigned int bytes_read;
c764a876
DE
3830
3831 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3832 cu_header->initial_length_size = bytes_read;
3833 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3834 info_ptr += bytes_read;
107d2387
AC
3835 cu_header->version = read_2_bytes (abfd, info_ptr);
3836 info_ptr += 2;
b64f50a1
JK
3837 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3838 &bytes_read);
613e1657 3839 info_ptr += bytes_read;
107d2387
AC
3840 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3841 info_ptr += 1;
3842 signed_addr = bfd_get_sign_extend_vma (abfd);
3843 if (signed_addr < 0)
8e65ff28 3844 internal_error (__FILE__, __LINE__,
e2e0b3e5 3845 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3846 cu_header->signed_addr_p = signed_addr;
c764a876 3847
107d2387
AC
3848 return info_ptr;
3849}
3850
36586728
TT
3851/* Helper function that returns the proper abbrev section for
3852 THIS_CU. */
3853
3854static struct dwarf2_section_info *
3855get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3856{
3857 struct dwarf2_section_info *abbrev;
3858
3859 if (this_cu->is_dwz)
3860 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3861 else
3862 abbrev = &dwarf2_per_objfile->abbrev;
3863
3864 return abbrev;
3865}
3866
9ff913ba
DE
3867/* Subroutine of read_and_check_comp_unit_head and
3868 read_and_check_type_unit_head to simplify them.
3869 Perform various error checking on the header. */
3870
3871static void
3872error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3873 struct dwarf2_section_info *section,
3874 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3875{
3876 bfd *abfd = section->asection->owner;
3877 const char *filename = bfd_get_filename (abfd);
3878
3879 if (header->version != 2 && header->version != 3 && header->version != 4)
3880 error (_("Dwarf Error: wrong version in compilation unit header "
3881 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3882 filename);
3883
b64f50a1 3884 if (header->abbrev_offset.sect_off
36586728 3885 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3886 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3887 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3888 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3889 filename);
3890
3891 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3892 avoid potential 32-bit overflow. */
1ce1cefd 3893 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3894 > section->size)
3895 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3896 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3897 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3898 filename);
3899}
3900
3901/* Read in a CU/TU header and perform some basic error checking.
3902 The contents of the header are stored in HEADER.
3903 The result is a pointer to the start of the first DIE. */
adabb602 3904
fe1b8b76 3905static gdb_byte *
9ff913ba
DE
3906read_and_check_comp_unit_head (struct comp_unit_head *header,
3907 struct dwarf2_section_info *section,
4bdcc0c1 3908 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3909 gdb_byte *info_ptr,
3910 int is_debug_types_section)
72bf9492 3911{
fe1b8b76 3912 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3913 bfd *abfd = section->asection->owner;
72bf9492 3914
b64f50a1 3915 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3916
72bf9492
DJ
3917 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3918
460c1c54
CC
3919 /* If we're reading a type unit, skip over the signature and
3920 type_offset fields. */
b0df02fd 3921 if (is_debug_types_section)
460c1c54
CC
3922 info_ptr += 8 /*signature*/ + header->offset_size;
3923
b64f50a1 3924 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3925
4bdcc0c1 3926 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3927
3928 return info_ptr;
3929}
3930
348e048f
DE
3931/* Read in the types comp unit header information from .debug_types entry at
3932 types_ptr. The result is a pointer to one past the end of the header. */
3933
3934static gdb_byte *
9ff913ba
DE
3935read_and_check_type_unit_head (struct comp_unit_head *header,
3936 struct dwarf2_section_info *section,
4bdcc0c1 3937 struct dwarf2_section_info *abbrev_section,
9ff913ba 3938 gdb_byte *info_ptr,
dee91e82
DE
3939 ULONGEST *signature,
3940 cu_offset *type_offset_in_tu)
348e048f 3941{
9ff913ba
DE
3942 gdb_byte *beg_of_comp_unit = info_ptr;
3943 bfd *abfd = section->asection->owner;
348e048f 3944
b64f50a1 3945 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3946
9ff913ba 3947 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3948
9ff913ba
DE
3949 /* If we're reading a type unit, skip over the signature and
3950 type_offset fields. */
3951 if (signature != NULL)
3952 *signature = read_8_bytes (abfd, info_ptr);
3953 info_ptr += 8;
dee91e82
DE
3954 if (type_offset_in_tu != NULL)
3955 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3956 header->offset_size);
9ff913ba
DE
3957 info_ptr += header->offset_size;
3958
b64f50a1 3959 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3960
4bdcc0c1 3961 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3962
3963 return info_ptr;
348e048f
DE
3964}
3965
f4dc4d17
DE
3966/* Fetch the abbreviation table offset from a comp or type unit header. */
3967
3968static sect_offset
3969read_abbrev_offset (struct dwarf2_section_info *section,
3970 sect_offset offset)
3971{
3972 bfd *abfd = section->asection->owner;
3973 gdb_byte *info_ptr;
3974 unsigned int length, initial_length_size, offset_size;
3975 sect_offset abbrev_offset;
3976
3977 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3978 info_ptr = section->buffer + offset.sect_off;
3979 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3980 offset_size = initial_length_size == 4 ? 4 : 8;
3981 info_ptr += initial_length_size + 2 /*version*/;
3982 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3983 return abbrev_offset;
3984}
3985
aaa75496
JB
3986/* Allocate a new partial symtab for file named NAME and mark this new
3987 partial symtab as being an include of PST. */
3988
3989static void
3990dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3991 struct objfile *objfile)
3992{
3993 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3994
3995 subpst->section_offsets = pst->section_offsets;
3996 subpst->textlow = 0;
3997 subpst->texthigh = 0;
3998
3999 subpst->dependencies = (struct partial_symtab **)
4000 obstack_alloc (&objfile->objfile_obstack,
4001 sizeof (struct partial_symtab *));
4002 subpst->dependencies[0] = pst;
4003 subpst->number_of_dependencies = 1;
4004
4005 subpst->globals_offset = 0;
4006 subpst->n_global_syms = 0;
4007 subpst->statics_offset = 0;
4008 subpst->n_static_syms = 0;
4009 subpst->symtab = NULL;
4010 subpst->read_symtab = pst->read_symtab;
4011 subpst->readin = 0;
4012
4013 /* No private part is necessary for include psymtabs. This property
4014 can be used to differentiate between such include psymtabs and
10b3939b 4015 the regular ones. */
58a9656e 4016 subpst->read_symtab_private = NULL;
aaa75496
JB
4017}
4018
4019/* Read the Line Number Program data and extract the list of files
4020 included by the source file represented by PST. Build an include
d85a05f0 4021 partial symtab for each of these included files. */
aaa75496
JB
4022
4023static void
4024dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4025 struct die_info *die,
4026 struct partial_symtab *pst)
aaa75496 4027{
d85a05f0
DJ
4028 struct line_header *lh = NULL;
4029 struct attribute *attr;
aaa75496 4030
d85a05f0
DJ
4031 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4032 if (attr)
3019eac3 4033 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4034 if (lh == NULL)
4035 return; /* No linetable, so no includes. */
4036
c6da4cef 4037 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4038 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4039
4040 free_line_header (lh);
4041}
4042
348e048f 4043static hashval_t
52dc124a 4044hash_signatured_type (const void *item)
348e048f 4045{
52dc124a 4046 const struct signatured_type *sig_type = item;
9a619af0 4047
348e048f 4048 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4049 return sig_type->signature;
348e048f
DE
4050}
4051
4052static int
52dc124a 4053eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4054{
4055 const struct signatured_type *lhs = item_lhs;
4056 const struct signatured_type *rhs = item_rhs;
9a619af0 4057
348e048f
DE
4058 return lhs->signature == rhs->signature;
4059}
4060
1fd400ff
TT
4061/* Allocate a hash table for signatured types. */
4062
4063static htab_t
673bfd45 4064allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4065{
4066 return htab_create_alloc_ex (41,
52dc124a
DE
4067 hash_signatured_type,
4068 eq_signatured_type,
1fd400ff
TT
4069 NULL,
4070 &objfile->objfile_obstack,
4071 hashtab_obstack_allocate,
4072 dummy_obstack_deallocate);
4073}
4074
d467dd73 4075/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4076
4077static int
d467dd73 4078add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4079{
4080 struct signatured_type *sigt = *slot;
b4dd5633 4081 struct signatured_type ***datap = datum;
1fd400ff 4082
b4dd5633 4083 **datap = sigt;
1fd400ff
TT
4084 ++*datap;
4085
4086 return 1;
4087}
4088
3019eac3 4089/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4090 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4091 NULL otherwise.
4092 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4093 The result is a pointer to the hash table or NULL if there are
4094 no types. */
348e048f 4095
3019eac3
DE
4096static htab_t
4097create_debug_types_hash_table (struct dwo_file *dwo_file,
4098 VEC (dwarf2_section_info_def) *types)
348e048f 4099{
3019eac3 4100 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4101 htab_t types_htab = NULL;
8b70b953
TT
4102 int ix;
4103 struct dwarf2_section_info *section;
4bdcc0c1 4104 struct dwarf2_section_info *abbrev_section;
348e048f 4105
3019eac3
DE
4106 if (VEC_empty (dwarf2_section_info_def, types))
4107 return NULL;
348e048f 4108
4bdcc0c1
DE
4109 abbrev_section = (dwo_file != NULL
4110 ? &dwo_file->sections.abbrev
4111 : &dwarf2_per_objfile->abbrev);
4112
09406207
DE
4113 if (dwarf2_read_debug)
4114 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4115 dwo_file ? ".dwo" : "",
4116 bfd_get_filename (abbrev_section->asection->owner));
4117
8b70b953 4118 for (ix = 0;
3019eac3 4119 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4120 ++ix)
4121 {
3019eac3 4122 bfd *abfd;
8b70b953 4123 gdb_byte *info_ptr, *end_ptr;
36586728 4124 struct dwarf2_section_info *abbrev_section;
348e048f 4125
8b70b953
TT
4126 dwarf2_read_section (objfile, section);
4127 info_ptr = section->buffer;
348e048f 4128
8b70b953
TT
4129 if (info_ptr == NULL)
4130 continue;
348e048f 4131
3019eac3
DE
4132 /* We can't set abfd until now because the section may be empty or
4133 not present, in which case section->asection will be NULL. */
4134 abfd = section->asection->owner;
4135
36586728
TT
4136 if (dwo_file)
4137 abbrev_section = &dwo_file->sections.abbrev;
4138 else
4139 abbrev_section = &dwarf2_per_objfile->abbrev;
4140
8b70b953 4141 if (types_htab == NULL)
3019eac3
DE
4142 {
4143 if (dwo_file)
4144 types_htab = allocate_dwo_unit_table (objfile);
4145 else
4146 types_htab = allocate_signatured_type_table (objfile);
4147 }
348e048f 4148
dee91e82
DE
4149 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4150 because we don't need to read any dies: the signature is in the
4151 header. */
8b70b953
TT
4152
4153 end_ptr = info_ptr + section->size;
4154 while (info_ptr < end_ptr)
4155 {
b64f50a1 4156 sect_offset offset;
3019eac3 4157 cu_offset type_offset_in_tu;
8b70b953 4158 ULONGEST signature;
52dc124a 4159 struct signatured_type *sig_type;
3019eac3 4160 struct dwo_unit *dwo_tu;
8b70b953
TT
4161 void **slot;
4162 gdb_byte *ptr = info_ptr;
9ff913ba 4163 struct comp_unit_head header;
dee91e82 4164 unsigned int length;
348e048f 4165
b64f50a1 4166 offset.sect_off = ptr - section->buffer;
348e048f 4167
8b70b953 4168 /* We need to read the type's signature in order to build the hash
9ff913ba 4169 table, but we don't need anything else just yet. */
348e048f 4170
4bdcc0c1
DE
4171 ptr = read_and_check_type_unit_head (&header, section,
4172 abbrev_section, ptr,
3019eac3 4173 &signature, &type_offset_in_tu);
6caca83c 4174
1ce1cefd 4175 length = get_cu_length (&header);
dee91e82 4176
6caca83c 4177 /* Skip dummy type units. */
dee91e82
DE
4178 if (ptr >= info_ptr + length
4179 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4180 {
1ce1cefd 4181 info_ptr += length;
6caca83c
CC
4182 continue;
4183 }
8b70b953 4184
3019eac3
DE
4185 if (dwo_file)
4186 {
4187 sig_type = NULL;
4188 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4189 struct dwo_unit);
4190 dwo_tu->dwo_file = dwo_file;
4191 dwo_tu->signature = signature;
4192 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4193 dwo_tu->info_or_types_section = section;
4194 dwo_tu->offset = offset;
4195 dwo_tu->length = length;
4196 }
4197 else
4198 {
4199 /* N.B.: type_offset is not usable if this type uses a DWO file.
4200 The real type_offset is in the DWO file. */
4201 dwo_tu = NULL;
4202 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4203 struct signatured_type);
4204 sig_type->signature = signature;
4205 sig_type->type_offset_in_tu = type_offset_in_tu;
4206 sig_type->per_cu.objfile = objfile;
4207 sig_type->per_cu.is_debug_types = 1;
4208 sig_type->per_cu.info_or_types_section = section;
4209 sig_type->per_cu.offset = offset;
4210 sig_type->per_cu.length = length;
4211 }
8b70b953 4212
3019eac3
DE
4213 slot = htab_find_slot (types_htab,
4214 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4215 INSERT);
8b70b953
TT
4216 gdb_assert (slot != NULL);
4217 if (*slot != NULL)
4218 {
3019eac3
DE
4219 sect_offset dup_offset;
4220
4221 if (dwo_file)
4222 {
4223 const struct dwo_unit *dup_tu = *slot;
4224
4225 dup_offset = dup_tu->offset;
4226 }
4227 else
4228 {
4229 const struct signatured_type *dup_tu = *slot;
4230
4231 dup_offset = dup_tu->per_cu.offset;
4232 }
b3c8eb43 4233
8b70b953
TT
4234 complaint (&symfile_complaints,
4235 _("debug type entry at offset 0x%x is duplicate to the "
4236 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4237 offset.sect_off, dup_offset.sect_off,
8b70b953 4238 phex (signature, sizeof (signature)));
8b70b953 4239 }
3019eac3 4240 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4241
09406207 4242 if (dwarf2_read_debug)
8b70b953 4243 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4244 offset.sect_off,
4245 phex (signature, sizeof (signature)));
348e048f 4246
dee91e82 4247 info_ptr += length;
8b70b953 4248 }
348e048f
DE
4249 }
4250
3019eac3
DE
4251 return types_htab;
4252}
4253
4254/* Create the hash table of all entries in the .debug_types section,
4255 and initialize all_type_units.
4256 The result is zero if there is an error (e.g. missing .debug_types section),
4257 otherwise non-zero. */
4258
4259static int
4260create_all_type_units (struct objfile *objfile)
4261{
4262 htab_t types_htab;
b4dd5633 4263 struct signatured_type **iter;
3019eac3
DE
4264
4265 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4266 if (types_htab == NULL)
4267 {
4268 dwarf2_per_objfile->signatured_types = NULL;
4269 return 0;
4270 }
4271
348e048f
DE
4272 dwarf2_per_objfile->signatured_types = types_htab;
4273
d467dd73
DE
4274 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4275 dwarf2_per_objfile->all_type_units
1fd400ff 4276 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4277 dwarf2_per_objfile->n_type_units
b4dd5633 4278 * sizeof (struct signatured_type *));
d467dd73
DE
4279 iter = &dwarf2_per_objfile->all_type_units[0];
4280 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4281 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4282 == dwarf2_per_objfile->n_type_units);
1fd400ff 4283
348e048f
DE
4284 return 1;
4285}
4286
380bca97 4287/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4288 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4289
4290static struct signatured_type *
e319fa28 4291lookup_signatured_type (ULONGEST sig)
348e048f
DE
4292{
4293 struct signatured_type find_entry, *entry;
4294
4295 if (dwarf2_per_objfile->signatured_types == NULL)
4296 {
4297 complaint (&symfile_complaints,
55f1336d 4298 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4299 return NULL;
348e048f
DE
4300 }
4301
4302 find_entry.signature = sig;
4303 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4304 return entry;
4305}
42e7ad6c
DE
4306\f
4307/* Low level DIE reading support. */
348e048f 4308
d85a05f0
DJ
4309/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4310
4311static void
4312init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4313 struct dwarf2_cu *cu,
3019eac3
DE
4314 struct dwarf2_section_info *section,
4315 struct dwo_file *dwo_file)
d85a05f0 4316{
fceca515 4317 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4318 reader->abfd = section->asection->owner;
d85a05f0 4319 reader->cu = cu;
3019eac3 4320 reader->dwo_file = dwo_file;
dee91e82
DE
4321 reader->die_section = section;
4322 reader->buffer = section->buffer;
f664829e 4323 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4324}
4325
fd820528 4326/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4327 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4328
f4dc4d17
DE
4329 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4330 Otherwise the table specified in the comp unit header is read in and used.
4331 This is an optimization for when we already have the abbrev table.
4332
dee91e82
DE
4333 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4334 Otherwise, a new CU is allocated with xmalloc.
4335
4336 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4337 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4338
4339 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4340 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4341
70221824 4342static void
fd820528 4343init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4344 struct abbrev_table *abbrev_table,
fd820528
DE
4345 int use_existing_cu, int keep,
4346 die_reader_func_ftype *die_reader_func,
4347 void *data)
c906108c 4348{
dee91e82 4349 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4350 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4351 bfd *abfd = section->asection->owner;
dee91e82
DE
4352 struct dwarf2_cu *cu;
4353 gdb_byte *begin_info_ptr, *info_ptr;
4354 struct die_reader_specs reader;
d85a05f0 4355 struct die_info *comp_unit_die;
dee91e82 4356 int has_children;
d85a05f0 4357 struct attribute *attr;
dee91e82
DE
4358 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4359 struct signatured_type *sig_type = NULL;
4bdcc0c1 4360 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4361 /* Non-zero if CU currently points to a DWO file and we need to
4362 reread it. When this happens we need to reread the skeleton die
4363 before we can reread the DWO file. */
4364 int rereading_dwo_cu = 0;
c906108c 4365
09406207
DE
4366 if (dwarf2_die_debug)
4367 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4368 this_cu->is_debug_types ? "type" : "comp",
4369 this_cu->offset.sect_off);
4370
dee91e82
DE
4371 if (use_existing_cu)
4372 gdb_assert (keep);
23745b47 4373
dee91e82
DE
4374 cleanups = make_cleanup (null_cleanup, NULL);
4375
4376 /* This is cheap if the section is already read in. */
4377 dwarf2_read_section (objfile, section);
4378
4379 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4380
4381 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4382
4383 if (use_existing_cu && this_cu->cu != NULL)
4384 {
4385 cu = this_cu->cu;
42e7ad6c
DE
4386
4387 /* If this CU is from a DWO file we need to start over, we need to
4388 refetch the attributes from the skeleton CU.
4389 This could be optimized by retrieving those attributes from when we
4390 were here the first time: the previous comp_unit_die was stored in
4391 comp_unit_obstack. But there's no data yet that we need this
4392 optimization. */
4393 if (cu->dwo_unit != NULL)
4394 rereading_dwo_cu = 1;
dee91e82
DE
4395 }
4396 else
4397 {
4398 /* If !use_existing_cu, this_cu->cu must be NULL. */
4399 gdb_assert (this_cu->cu == NULL);
4400
4401 cu = xmalloc (sizeof (*cu));
4402 init_one_comp_unit (cu, this_cu);
4403
4404 /* If an error occurs while loading, release our storage. */
4405 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4406 }
dee91e82 4407
42e7ad6c
DE
4408 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4409 {
4410 /* We already have the header, there's no need to read it in again. */
4411 info_ptr += cu->header.first_die_offset.cu_off;
4412 }
4413 else
4414 {
3019eac3 4415 if (this_cu->is_debug_types)
dee91e82
DE
4416 {
4417 ULONGEST signature;
42e7ad6c 4418 cu_offset type_offset_in_tu;
dee91e82 4419
4bdcc0c1
DE
4420 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4421 abbrev_section, info_ptr,
42e7ad6c
DE
4422 &signature,
4423 &type_offset_in_tu);
dee91e82 4424
42e7ad6c
DE
4425 /* Since per_cu is the first member of struct signatured_type,
4426 we can go from a pointer to one to a pointer to the other. */
4427 sig_type = (struct signatured_type *) this_cu;
4428 gdb_assert (sig_type->signature == signature);
4429 gdb_assert (sig_type->type_offset_in_tu.cu_off
4430 == type_offset_in_tu.cu_off);
dee91e82
DE
4431 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4432
42e7ad6c
DE
4433 /* LENGTH has not been set yet for type units if we're
4434 using .gdb_index. */
1ce1cefd 4435 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4436
4437 /* Establish the type offset that can be used to lookup the type. */
4438 sig_type->type_offset_in_section.sect_off =
4439 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4440 }
4441 else
4442 {
4bdcc0c1
DE
4443 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4444 abbrev_section,
4445 info_ptr, 0);
dee91e82
DE
4446
4447 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4448 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4449 }
4450 }
10b3939b 4451
6caca83c 4452 /* Skip dummy compilation units. */
dee91e82 4453 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4454 || peek_abbrev_code (abfd, info_ptr) == 0)
4455 {
dee91e82 4456 do_cleanups (cleanups);
21b2bd31 4457 return;
6caca83c
CC
4458 }
4459
433df2d4
DE
4460 /* If we don't have them yet, read the abbrevs for this compilation unit.
4461 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4462 done. Note that it's important that if the CU had an abbrev table
4463 on entry we don't free it when we're done: Somewhere up the call stack
4464 it may be in use. */
f4dc4d17
DE
4465 if (abbrev_table != NULL)
4466 {
4467 gdb_assert (cu->abbrev_table == NULL);
4468 gdb_assert (cu->header.abbrev_offset.sect_off
4469 == abbrev_table->offset.sect_off);
4470 cu->abbrev_table = abbrev_table;
4471 }
4472 else if (cu->abbrev_table == NULL)
dee91e82 4473 {
4bdcc0c1 4474 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4475 make_cleanup (dwarf2_free_abbrev_table, cu);
4476 }
42e7ad6c
DE
4477 else if (rereading_dwo_cu)
4478 {
4479 dwarf2_free_abbrev_table (cu);
4480 dwarf2_read_abbrevs (cu, abbrev_section);
4481 }
af703f96 4482
dee91e82 4483 /* Read the top level CU/TU die. */
3019eac3 4484 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4485 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4486
3019eac3
DE
4487 /* If we have a DWO stub, process it and then read in the DWO file.
4488 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4489 a DWO CU, that this test will fail. */
4490 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4491 if (attr)
4492 {
4493 char *dwo_name = DW_STRING (attr);
42e7ad6c 4494 const char *comp_dir_string;
3019eac3
DE
4495 struct dwo_unit *dwo_unit;
4496 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4497 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4498 int i,num_extra_attrs;
4bdcc0c1 4499 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4500
4501 if (has_children)
4502 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4503 " has children (offset 0x%x) [in module %s]"),
4504 this_cu->offset.sect_off, bfd_get_filename (abfd));
4505
4506 /* These attributes aren't processed until later:
4507 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4508 However, the attribute is found in the stub which we won't have later.
4509 In order to not impose this complication on the rest of the code,
4510 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4511
4512 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4513 DWO file. */
42e7ad6c 4514 stmt_list = NULL;
3019eac3
DE
4515 if (! this_cu->is_debug_types)
4516 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4517 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4518 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4519 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4520 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4521
4522 /* There should be a DW_AT_addr_base attribute here (if needed).
4523 We need the value before we can process DW_FORM_GNU_addr_index. */
4524 cu->addr_base = 0;
3019eac3
DE
4525 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4526 if (attr)
2e3cf129
DE
4527 cu->addr_base = DW_UNSND (attr);
4528
4529 /* There should be a DW_AT_ranges_base attribute here (if needed).
4530 We need the value before we can process DW_AT_ranges. */
4531 cu->ranges_base = 0;
4532 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4533 if (attr)
4534 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4535
4536 if (this_cu->is_debug_types)
4537 {
4538 gdb_assert (sig_type != NULL);
4539 signature = sig_type->signature;
4540 }
4541 else
4542 {
4543 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4544 if (! attr)
4545 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4546 dwo_name);
4547 signature = DW_UNSND (attr);
4548 }
4549
4550 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4551 comp_dir_string = NULL;
4552 if (comp_dir)
4553 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4554
4555 if (this_cu->is_debug_types)
42e7ad6c 4556 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4557 else
42e7ad6c 4558 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4559 signature);
4560
4561 if (dwo_unit == NULL)
4562 {
4563 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4564 " with ID %s [in module %s]"),
4565 this_cu->offset.sect_off,
4566 phex (signature, sizeof (signature)),
4567 objfile->name);
4568 }
4569
4570 /* Set up for reading the DWO CU/TU. */
4571 cu->dwo_unit = dwo_unit;
4572 section = dwo_unit->info_or_types_section;
80626a55 4573 dwarf2_read_section (objfile, section);
3019eac3 4574 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4575 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4576 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4577
4578 if (this_cu->is_debug_types)
4579 {
4580 ULONGEST signature;
80626a55 4581 cu_offset type_offset_in_tu;
3019eac3 4582
4bdcc0c1
DE
4583 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4584 dwo_abbrev_section,
4585 info_ptr,
80626a55
DE
4586 &signature,
4587 &type_offset_in_tu);
3019eac3
DE
4588 gdb_assert (sig_type->signature == signature);
4589 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4590 /* For DWOs coming from DWP files, we don't know the CU length
4591 nor the type's offset in the TU until now. */
4592 dwo_unit->length = get_cu_length (&cu->header);
4593 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4594
4595 /* Establish the type offset that can be used to lookup the type.
4596 For DWO files, we don't know it until now. */
4597 sig_type->type_offset_in_section.sect_off =
4598 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4599 }
4600 else
4601 {
4bdcc0c1
DE
4602 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4603 dwo_abbrev_section,
4604 info_ptr, 0);
3019eac3 4605 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4606 /* For DWOs coming from DWP files, we don't know the CU length
4607 until now. */
4608 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4609 }
4610
4611 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4612 if (abbrev_table == NULL)
4613 {
4614 dwarf2_free_abbrev_table (cu);
4615 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4616 }
4617 else
4618 {
4619 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4620 make_cleanup (dwarf2_free_abbrev_table, cu);
4621 }
3019eac3
DE
4622
4623 /* Read in the die, but leave space to copy over the attributes
4624 from the stub. This has the benefit of simplifying the rest of
4625 the code - all the real work is done here. */
4626 num_extra_attrs = ((stmt_list != NULL)
4627 + (low_pc != NULL)
4628 + (high_pc != NULL)
42e7ad6c
DE
4629 + (ranges != NULL)
4630 + (comp_dir != NULL));
3019eac3
DE
4631 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4632 &has_children, num_extra_attrs);
4633
4634 /* Copy over the attributes from the stub to the DWO die. */
4635 i = comp_unit_die->num_attrs;
4636 if (stmt_list != NULL)
4637 comp_unit_die->attrs[i++] = *stmt_list;
4638 if (low_pc != NULL)
4639 comp_unit_die->attrs[i++] = *low_pc;
4640 if (high_pc != NULL)
4641 comp_unit_die->attrs[i++] = *high_pc;
4642 if (ranges != NULL)
4643 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4644 if (comp_dir != NULL)
4645 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4646 comp_unit_die->num_attrs += num_extra_attrs;
4647
4648 /* Skip dummy compilation units. */
4649 if (info_ptr >= begin_info_ptr + dwo_unit->length
4650 || peek_abbrev_code (abfd, info_ptr) == 0)
4651 {
4652 do_cleanups (cleanups);
4653 return;
4654 }
4655 }
4656
dee91e82
DE
4657 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4658
4659 if (free_cu_cleanup != NULL)
348e048f 4660 {
dee91e82
DE
4661 if (keep)
4662 {
4663 /* We've successfully allocated this compilation unit. Let our
4664 caller clean it up when finished with it. */
4665 discard_cleanups (free_cu_cleanup);
4666
4667 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4668 So we have to manually free the abbrev table. */
4669 dwarf2_free_abbrev_table (cu);
4670
4671 /* Link this CU into read_in_chain. */
4672 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4673 dwarf2_per_objfile->read_in_chain = this_cu;
4674 }
4675 else
4676 do_cleanups (free_cu_cleanup);
348e048f 4677 }
dee91e82
DE
4678
4679 do_cleanups (cleanups);
4680}
4681
3019eac3
DE
4682/* Read CU/TU THIS_CU in section SECTION,
4683 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4684 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4685 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4686
4687 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4688 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4689
4690 We fill in THIS_CU->length.
4691
4692 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4693 linker) then DIE_READER_FUNC will not get called.
4694
4695 THIS_CU->cu is always freed when done.
3019eac3
DE
4696 This is done in order to not leave THIS_CU->cu in a state where we have
4697 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4698
4699static void
4700init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4701 struct dwarf2_section_info *abbrev_section,
3019eac3 4702 struct dwo_file *dwo_file,
dee91e82
DE
4703 die_reader_func_ftype *die_reader_func,
4704 void *data)
4705{
4706 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4707 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4708 bfd *abfd = section->asection->owner;
dee91e82
DE
4709 struct dwarf2_cu cu;
4710 gdb_byte *begin_info_ptr, *info_ptr;
4711 struct die_reader_specs reader;
4712 struct cleanup *cleanups;
4713 struct die_info *comp_unit_die;
4714 int has_children;
4715
09406207
DE
4716 if (dwarf2_die_debug)
4717 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4718 this_cu->is_debug_types ? "type" : "comp",
4719 this_cu->offset.sect_off);
4720
dee91e82
DE
4721 gdb_assert (this_cu->cu == NULL);
4722
dee91e82
DE
4723 /* This is cheap if the section is already read in. */
4724 dwarf2_read_section (objfile, section);
4725
4726 init_one_comp_unit (&cu, this_cu);
4727
4728 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4729
4730 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4731 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4732 abbrev_section, info_ptr,
3019eac3 4733 this_cu->is_debug_types);
dee91e82 4734
1ce1cefd 4735 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4736
4737 /* Skip dummy compilation units. */
4738 if (info_ptr >= begin_info_ptr + this_cu->length
4739 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4740 {
dee91e82 4741 do_cleanups (cleanups);
21b2bd31 4742 return;
93311388 4743 }
72bf9492 4744
dee91e82
DE
4745 dwarf2_read_abbrevs (&cu, abbrev_section);
4746 make_cleanup (dwarf2_free_abbrev_table, &cu);
4747
3019eac3 4748 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4749 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4750
4751 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4752
4753 do_cleanups (cleanups);
4754}
4755
3019eac3
DE
4756/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4757 does not lookup the specified DWO file.
4758 This cannot be used to read DWO files.
dee91e82
DE
4759
4760 THIS_CU->cu is always freed when done.
3019eac3
DE
4761 This is done in order to not leave THIS_CU->cu in a state where we have
4762 to care whether it refers to the "main" CU or the DWO CU.
4763 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4764
4765static void
4766init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4767 die_reader_func_ftype *die_reader_func,
4768 void *data)
4769{
4770 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4771 get_abbrev_section_for_cu (this_cu),
3019eac3 4772 NULL,
dee91e82
DE
4773 die_reader_func, data);
4774}
4775
f4dc4d17
DE
4776/* Create a psymtab named NAME and assign it to PER_CU.
4777
4778 The caller must fill in the following details:
4779 dirname, textlow, texthigh. */
4780
4781static struct partial_symtab *
4782create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4783{
4784 struct objfile *objfile = per_cu->objfile;
4785 struct partial_symtab *pst;
4786
4787 pst = start_psymtab_common (objfile, objfile->section_offsets,
4788 name, 0,
4789 objfile->global_psymbols.next,
4790 objfile->static_psymbols.next);
4791
4792 pst->psymtabs_addrmap_supported = 1;
4793
4794 /* This is the glue that links PST into GDB's symbol API. */
4795 pst->read_symtab_private = per_cu;
4796 pst->read_symtab = dwarf2_psymtab_to_symtab;
4797 per_cu->v.psymtab = pst;
4798
4799 return pst;
4800}
4801
dee91e82
DE
4802/* die_reader_func for process_psymtab_comp_unit. */
4803
4804static void
4805process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4806 gdb_byte *info_ptr,
4807 struct die_info *comp_unit_die,
4808 int has_children,
4809 void *data)
4810{
4811 struct dwarf2_cu *cu = reader->cu;
4812 struct objfile *objfile = cu->objfile;
4813 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4814 struct attribute *attr;
4815 CORE_ADDR baseaddr;
4816 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4817 struct partial_symtab *pst;
4818 int has_pc_info;
4819 const char *filename;
95554aad 4820 int *want_partial_unit_ptr = data;
dee91e82 4821
95554aad
TT
4822 if (comp_unit_die->tag == DW_TAG_partial_unit
4823 && (want_partial_unit_ptr == NULL
4824 || !*want_partial_unit_ptr))
dee91e82
DE
4825 return;
4826
f4dc4d17
DE
4827 gdb_assert (! per_cu->is_debug_types);
4828
95554aad 4829 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4830
4831 cu->list_in_scope = &file_symbols;
c906108c 4832
93311388 4833 /* Allocate a new partial symbol table structure. */
dee91e82 4834 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4835 if (attr == NULL || !DW_STRING (attr))
4836 filename = "";
4837 else
4838 filename = DW_STRING (attr);
72bf9492 4839
f4dc4d17
DE
4840 pst = create_partial_symtab (per_cu, filename);
4841
4842 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4843 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4844 if (attr != NULL)
4845 pst->dirname = DW_STRING (attr);
72bf9492 4846
93311388 4847 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4848
dee91e82 4849 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4850
93311388
DE
4851 /* Possibly set the default values of LOWPC and HIGHPC from
4852 `DW_AT_ranges'. */
d85a05f0 4853 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4854 &best_highpc, cu, pst);
d85a05f0 4855 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4856 /* Store the contiguous range if it is not empty; it can be empty for
4857 CUs with no code. */
4858 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4859 best_lowpc + baseaddr,
4860 best_highpc + baseaddr - 1, pst);
93311388
DE
4861
4862 /* Check if comp unit has_children.
4863 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4864 If not, there's no more debug_info for this comp unit. */
d85a05f0 4865 if (has_children)
93311388
DE
4866 {
4867 struct partial_die_info *first_die;
4868 CORE_ADDR lowpc, highpc;
31ffec48 4869
93311388
DE
4870 lowpc = ((CORE_ADDR) -1);
4871 highpc = ((CORE_ADDR) 0);
c906108c 4872
dee91e82 4873 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4874
93311388 4875 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4876 ! has_pc_info, cu);
57c22c6c 4877
93311388
DE
4878 /* If we didn't find a lowpc, set it to highpc to avoid
4879 complaints from `maint check'. */
4880 if (lowpc == ((CORE_ADDR) -1))
4881 lowpc = highpc;
10b3939b 4882
93311388
DE
4883 /* If the compilation unit didn't have an explicit address range,
4884 then use the information extracted from its child dies. */
d85a05f0 4885 if (! has_pc_info)
93311388 4886 {
d85a05f0
DJ
4887 best_lowpc = lowpc;
4888 best_highpc = highpc;
93311388
DE
4889 }
4890 }
d85a05f0
DJ
4891 pst->textlow = best_lowpc + baseaddr;
4892 pst->texthigh = best_highpc + baseaddr;
c906108c 4893
93311388
DE
4894 pst->n_global_syms = objfile->global_psymbols.next -
4895 (objfile->global_psymbols.list + pst->globals_offset);
4896 pst->n_static_syms = objfile->static_psymbols.next -
4897 (objfile->static_psymbols.list + pst->statics_offset);
4898 sort_pst_symbols (pst);
c906108c 4899
f4dc4d17 4900 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4901 {
4902 int i;
f4dc4d17 4903 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4904 struct dwarf2_per_cu_data *iter;
4905
4906 /* Fill in 'dependencies' here; we fill in 'users' in a
4907 post-pass. */
4908 pst->number_of_dependencies = len;
4909 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4910 len * sizeof (struct symtab *));
4911 for (i = 0;
f4dc4d17 4912 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4913 i, iter);
4914 ++i)
4915 pst->dependencies[i] = iter->v.psymtab;
4916
f4dc4d17 4917 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4918 }
4919
f4dc4d17
DE
4920 /* Get the list of files included in the current compilation unit,
4921 and build a psymtab for each of them. */
4922 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4923
4924 if (dwarf2_read_debug)
4925 {
4926 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4927
4928 fprintf_unfiltered (gdb_stdlog,
844226d6 4929 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4930 ", %d global, %d static syms\n",
4931 per_cu->is_debug_types ? "type" : "comp",
4932 per_cu->offset.sect_off,
4933 paddress (gdbarch, pst->textlow),
4934 paddress (gdbarch, pst->texthigh),
4935 pst->n_global_syms, pst->n_static_syms);
4936 }
dee91e82 4937}
ae038cb0 4938
dee91e82
DE
4939/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4940 Process compilation unit THIS_CU for a psymtab. */
4941
4942static void
95554aad
TT
4943process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4944 int want_partial_unit)
dee91e82
DE
4945{
4946 /* If this compilation unit was already read in, free the
4947 cached copy in order to read it in again. This is
4948 necessary because we skipped some symbols when we first
4949 read in the compilation unit (see load_partial_dies).
4950 This problem could be avoided, but the benefit is unclear. */
4951 if (this_cu->cu != NULL)
4952 free_one_cached_comp_unit (this_cu);
4953
3019eac3 4954 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
4955 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4956 process_psymtab_comp_unit_reader,
95554aad 4957 &want_partial_unit);
dee91e82
DE
4958
4959 /* Age out any secondary CUs. */
4960 age_cached_comp_units ();
93311388 4961}
ff013f42 4962
f4dc4d17
DE
4963static hashval_t
4964hash_type_unit_group (const void *item)
4965{
094b34ac 4966 const struct type_unit_group *tu_group = item;
f4dc4d17 4967
094b34ac 4968 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4969}
348e048f
DE
4970
4971static int
f4dc4d17 4972eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4973{
f4dc4d17
DE
4974 const struct type_unit_group *lhs = item_lhs;
4975 const struct type_unit_group *rhs = item_rhs;
348e048f 4976
094b34ac 4977 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4978}
348e048f 4979
f4dc4d17
DE
4980/* Allocate a hash table for type unit groups. */
4981
4982static htab_t
4983allocate_type_unit_groups_table (void)
4984{
4985 return htab_create_alloc_ex (3,
4986 hash_type_unit_group,
4987 eq_type_unit_group,
4988 NULL,
4989 &dwarf2_per_objfile->objfile->objfile_obstack,
4990 hashtab_obstack_allocate,
4991 dummy_obstack_deallocate);
4992}
dee91e82 4993
f4dc4d17
DE
4994/* Type units that don't have DW_AT_stmt_list are grouped into their own
4995 partial symtabs. We combine several TUs per psymtab to not let the size
4996 of any one psymtab grow too big. */
4997#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4998#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 4999
094b34ac 5000/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5001 Create the type_unit_group object used to hold one or more TUs. */
5002
5003static struct type_unit_group *
094b34ac 5004create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5005{
5006 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5007 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5008 struct type_unit_group *tu_group;
f4dc4d17
DE
5009
5010 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5011 struct type_unit_group);
094b34ac 5012 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5013 per_cu->objfile = objfile;
5014 per_cu->is_debug_types = 1;
5015 per_cu->s.type_unit_group = tu_group;
5016
094b34ac
DE
5017 if (dwarf2_per_objfile->using_index)
5018 {
5019 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5020 struct dwarf2_per_cu_quick_data);
5021 tu_group->t.first_tu = cu->per_cu;
5022 }
5023 else
5024 {
5025 unsigned int line_offset = line_offset_struct.sect_off;
5026 struct partial_symtab *pst;
5027 char *name;
5028
5029 /* Give the symtab a useful name for debug purposes. */
5030 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5031 name = xstrprintf ("<type_units_%d>",
5032 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5033 else
5034 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5035
5036 pst = create_partial_symtab (per_cu, name);
5037 pst->anonymous = 1;
f4dc4d17 5038
094b34ac
DE
5039 xfree (name);
5040 }
f4dc4d17 5041
094b34ac
DE
5042 tu_group->hash.dwo_unit = cu->dwo_unit;
5043 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5044
5045 return tu_group;
5046}
5047
094b34ac
DE
5048/* Look up the type_unit_group for type unit CU, and create it if necessary.
5049 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5050
5051static struct type_unit_group *
094b34ac 5052get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5053{
5054 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5055 struct type_unit_group *tu_group;
5056 void **slot;
5057 unsigned int line_offset;
5058 struct type_unit_group type_unit_group_for_lookup;
5059
5060 if (dwarf2_per_objfile->type_unit_groups == NULL)
5061 {
5062 dwarf2_per_objfile->type_unit_groups =
5063 allocate_type_unit_groups_table ();
5064 }
5065
5066 /* Do we need to create a new group, or can we use an existing one? */
5067
5068 if (stmt_list)
5069 {
5070 line_offset = DW_UNSND (stmt_list);
5071 ++tu_stats->nr_symtab_sharers;
5072 }
5073 else
5074 {
5075 /* Ugh, no stmt_list. Rare, but we have to handle it.
5076 We can do various things here like create one group per TU or
5077 spread them over multiple groups to split up the expansion work.
5078 To avoid worst case scenarios (too many groups or too large groups)
5079 we, umm, group them in bunches. */
5080 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5081 | (tu_stats->nr_stmt_less_type_units
5082 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5083 ++tu_stats->nr_stmt_less_type_units;
5084 }
5085
094b34ac
DE
5086 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5087 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5088 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5089 &type_unit_group_for_lookup, INSERT);
5090 if (*slot != NULL)
5091 {
5092 tu_group = *slot;
5093 gdb_assert (tu_group != NULL);
5094 }
5095 else
5096 {
5097 sect_offset line_offset_struct;
5098
5099 line_offset_struct.sect_off = line_offset;
094b34ac 5100 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5101 *slot = tu_group;
5102 ++tu_stats->nr_symtabs;
5103 }
5104
5105 return tu_group;
5106}
5107
5108/* Struct used to sort TUs by their abbreviation table offset. */
5109
5110struct tu_abbrev_offset
5111{
5112 struct signatured_type *sig_type;
5113 sect_offset abbrev_offset;
5114};
5115
5116/* Helper routine for build_type_unit_groups, passed to qsort. */
5117
5118static int
5119sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5120{
5121 const struct tu_abbrev_offset * const *a = ap;
5122 const struct tu_abbrev_offset * const *b = bp;
5123 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5124 unsigned int boff = (*b)->abbrev_offset.sect_off;
5125
5126 return (aoff > boff) - (aoff < boff);
5127}
5128
5129/* A helper function to add a type_unit_group to a table. */
5130
5131static int
5132add_type_unit_group_to_table (void **slot, void *datum)
5133{
5134 struct type_unit_group *tu_group = *slot;
5135 struct type_unit_group ***datap = datum;
5136
5137 **datap = tu_group;
5138 ++*datap;
5139
5140 return 1;
5141}
5142
5143/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5144 each one passing FUNC,DATA.
5145
5146 The efficiency is because we sort TUs by the abbrev table they use and
5147 only read each abbrev table once. In one program there are 200K TUs
5148 sharing 8K abbrev tables.
5149
5150 The main purpose of this function is to support building the
5151 dwarf2_per_objfile->type_unit_groups table.
5152 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5153 can collapse the search space by grouping them by stmt_list.
5154 The savings can be significant, in the same program from above the 200K TUs
5155 share 8K stmt_list tables.
5156
5157 FUNC is expected to call get_type_unit_group, which will create the
5158 struct type_unit_group if necessary and add it to
5159 dwarf2_per_objfile->type_unit_groups. */
5160
5161static void
5162build_type_unit_groups (die_reader_func_ftype *func, void *data)
5163{
5164 struct objfile *objfile = dwarf2_per_objfile->objfile;
5165 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5166 struct cleanup *cleanups;
5167 struct abbrev_table *abbrev_table;
5168 sect_offset abbrev_offset;
5169 struct tu_abbrev_offset *sorted_by_abbrev;
5170 struct type_unit_group **iter;
5171 int i;
5172
5173 /* It's up to the caller to not call us multiple times. */
5174 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5175
5176 if (dwarf2_per_objfile->n_type_units == 0)
5177 return;
5178
5179 /* TUs typically share abbrev tables, and there can be way more TUs than
5180 abbrev tables. Sort by abbrev table to reduce the number of times we
5181 read each abbrev table in.
5182 Alternatives are to punt or to maintain a cache of abbrev tables.
5183 This is simpler and efficient enough for now.
5184
5185 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5186 symtab to use). Typically TUs with the same abbrev offset have the same
5187 stmt_list value too so in practice this should work well.
5188
5189 The basic algorithm here is:
5190
5191 sort TUs by abbrev table
5192 for each TU with same abbrev table:
5193 read abbrev table if first user
5194 read TU top level DIE
5195 [IWBN if DWO skeletons had DW_AT_stmt_list]
5196 call FUNC */
5197
5198 if (dwarf2_read_debug)
5199 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5200
5201 /* Sort in a separate table to maintain the order of all_type_units
5202 for .gdb_index: TU indices directly index all_type_units. */
5203 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5204 dwarf2_per_objfile->n_type_units);
5205 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5206 {
5207 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5208
5209 sorted_by_abbrev[i].sig_type = sig_type;
5210 sorted_by_abbrev[i].abbrev_offset =
5211 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5212 sig_type->per_cu.offset);
5213 }
5214 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5215 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5216 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5217
094b34ac
DE
5218 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5219 called any number of times, so we don't reset tu_stats here. */
5220
f4dc4d17
DE
5221 abbrev_offset.sect_off = ~(unsigned) 0;
5222 abbrev_table = NULL;
5223 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5224
5225 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5226 {
5227 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5228
5229 /* Switch to the next abbrev table if necessary. */
5230 if (abbrev_table == NULL
5231 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5232 {
5233 if (abbrev_table != NULL)
5234 {
5235 abbrev_table_free (abbrev_table);
5236 /* Reset to NULL in case abbrev_table_read_table throws
5237 an error: abbrev_table_free_cleanup will get called. */
5238 abbrev_table = NULL;
5239 }
5240 abbrev_offset = tu->abbrev_offset;
5241 abbrev_table =
5242 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5243 abbrev_offset);
5244 ++tu_stats->nr_uniq_abbrev_tables;
5245 }
5246
5247 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5248 func, data);
5249 }
5250
5251 /* Create a vector of pointers to primary type units to make it easy to
5252 iterate over them and CUs. See dw2_get_primary_cu. */
5253 dwarf2_per_objfile->n_type_unit_groups =
5254 htab_elements (dwarf2_per_objfile->type_unit_groups);
5255 dwarf2_per_objfile->all_type_unit_groups =
5256 obstack_alloc (&objfile->objfile_obstack,
5257 dwarf2_per_objfile->n_type_unit_groups
5258 * sizeof (struct type_unit_group *));
5259 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5260 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5261 add_type_unit_group_to_table, &iter);
5262 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5263 == dwarf2_per_objfile->n_type_unit_groups);
5264
5265 do_cleanups (cleanups);
5266
5267 if (dwarf2_read_debug)
5268 {
5269 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5270 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5271 dwarf2_per_objfile->n_type_units);
5272 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5273 tu_stats->nr_uniq_abbrev_tables);
5274 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5275 tu_stats->nr_symtabs);
5276 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5277 tu_stats->nr_symtab_sharers);
5278 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5279 tu_stats->nr_stmt_less_type_units);
5280 }
5281}
5282
5283/* Reader function for build_type_psymtabs. */
5284
5285static void
5286build_type_psymtabs_reader (const struct die_reader_specs *reader,
5287 gdb_byte *info_ptr,
5288 struct die_info *type_unit_die,
5289 int has_children,
5290 void *data)
5291{
5292 struct objfile *objfile = dwarf2_per_objfile->objfile;
5293 struct dwarf2_cu *cu = reader->cu;
5294 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5295 struct type_unit_group *tu_group;
5296 struct attribute *attr;
5297 struct partial_die_info *first_die;
5298 CORE_ADDR lowpc, highpc;
5299 struct partial_symtab *pst;
5300
5301 gdb_assert (data == NULL);
5302
5303 if (! has_children)
5304 return;
5305
5306 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5307 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5308
094b34ac 5309 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5310
5311 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5312 cu->list_in_scope = &file_symbols;
5313 pst = create_partial_symtab (per_cu, "");
5314 pst->anonymous = 1;
5315
5316 first_die = load_partial_dies (reader, info_ptr, 1);
5317
5318 lowpc = (CORE_ADDR) -1;
5319 highpc = (CORE_ADDR) 0;
5320 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5321
5322 pst->n_global_syms = objfile->global_psymbols.next -
5323 (objfile->global_psymbols.list + pst->globals_offset);
5324 pst->n_static_syms = objfile->static_psymbols.next -
5325 (objfile->static_psymbols.list + pst->statics_offset);
5326 sort_pst_symbols (pst);
5327}
5328
5329/* Traversal function for build_type_psymtabs. */
5330
5331static int
5332build_type_psymtab_dependencies (void **slot, void *info)
5333{
5334 struct objfile *objfile = dwarf2_per_objfile->objfile;
5335 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5336 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5337 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5338 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5339 struct dwarf2_per_cu_data *iter;
5340 int i;
5341
5342 gdb_assert (len > 0);
5343
5344 pst->number_of_dependencies = len;
5345 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5346 len * sizeof (struct psymtab *));
5347 for (i = 0;
094b34ac 5348 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5349 ++i)
5350 {
5351 pst->dependencies[i] = iter->v.psymtab;
5352 iter->s.type_unit_group = tu_group;
5353 }
5354
094b34ac 5355 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5356
5357 return 1;
5358}
5359
5360/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5361 Build partial symbol tables for the .debug_types comp-units. */
5362
5363static void
5364build_type_psymtabs (struct objfile *objfile)
5365{
0e50663e 5366 if (! create_all_type_units (objfile))
348e048f
DE
5367 return;
5368
f4dc4d17
DE
5369 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5370
5371 /* Now that all TUs have been processed we can fill in the dependencies. */
5372 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5373 build_type_psymtab_dependencies, NULL);
348e048f
DE
5374}
5375
60606b2c
TT
5376/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5377
5378static void
5379psymtabs_addrmap_cleanup (void *o)
5380{
5381 struct objfile *objfile = o;
ec61707d 5382
60606b2c
TT
5383 objfile->psymtabs_addrmap = NULL;
5384}
5385
95554aad
TT
5386/* Compute the 'user' field for each psymtab in OBJFILE. */
5387
5388static void
5389set_partial_user (struct objfile *objfile)
5390{
5391 int i;
5392
5393 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5394 {
5395 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5396 struct partial_symtab *pst = per_cu->v.psymtab;
5397 int j;
5398
36586728
TT
5399 if (pst == NULL)
5400 continue;
5401
95554aad
TT
5402 for (j = 0; j < pst->number_of_dependencies; ++j)
5403 {
5404 /* Set the 'user' field only if it is not already set. */
5405 if (pst->dependencies[j]->user == NULL)
5406 pst->dependencies[j]->user = pst;
5407 }
5408 }
5409}
5410
93311388
DE
5411/* Build the partial symbol table by doing a quick pass through the
5412 .debug_info and .debug_abbrev sections. */
72bf9492 5413
93311388 5414static void
c67a9c90 5415dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5416{
60606b2c
TT
5417 struct cleanup *back_to, *addrmap_cleanup;
5418 struct obstack temp_obstack;
21b2bd31 5419 int i;
93311388 5420
45cfd468
DE
5421 if (dwarf2_read_debug)
5422 {
5423 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5424 objfile->name);
5425 }
5426
98bfdba5
PA
5427 dwarf2_per_objfile->reading_partial_symbols = 1;
5428
be391dca 5429 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5430
93311388
DE
5431 /* Any cached compilation units will be linked by the per-objfile
5432 read_in_chain. Make sure to free them when we're done. */
5433 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5434
348e048f
DE
5435 build_type_psymtabs (objfile);
5436
93311388 5437 create_all_comp_units (objfile);
c906108c 5438
60606b2c
TT
5439 /* Create a temporary address map on a temporary obstack. We later
5440 copy this to the final obstack. */
5441 obstack_init (&temp_obstack);
5442 make_cleanup_obstack_free (&temp_obstack);
5443 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5444 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5445
21b2bd31 5446 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5447 {
21b2bd31 5448 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5449
95554aad 5450 process_psymtab_comp_unit (per_cu, 0);
c906108c 5451 }
ff013f42 5452
95554aad
TT
5453 set_partial_user (objfile);
5454
ff013f42
JK
5455 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5456 &objfile->objfile_obstack);
60606b2c 5457 discard_cleanups (addrmap_cleanup);
ff013f42 5458
ae038cb0 5459 do_cleanups (back_to);
45cfd468
DE
5460
5461 if (dwarf2_read_debug)
5462 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5463 objfile->name);
ae038cb0
DJ
5464}
5465
3019eac3 5466/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5467
5468static void
dee91e82
DE
5469load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5470 gdb_byte *info_ptr,
5471 struct die_info *comp_unit_die,
5472 int has_children,
5473 void *data)
ae038cb0 5474{
dee91e82 5475 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5476
95554aad 5477 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5478
ae038cb0
DJ
5479 /* Check if comp unit has_children.
5480 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5481 If not, there's no more debug_info for this comp unit. */
d85a05f0 5482 if (has_children)
dee91e82
DE
5483 load_partial_dies (reader, info_ptr, 0);
5484}
98bfdba5 5485
dee91e82
DE
5486/* Load the partial DIEs for a secondary CU into memory.
5487 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5488
dee91e82
DE
5489static void
5490load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5491{
f4dc4d17
DE
5492 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5493 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5494}
5495
ae038cb0 5496static void
36586728
TT
5497read_comp_units_from_section (struct objfile *objfile,
5498 struct dwarf2_section_info *section,
5499 unsigned int is_dwz,
5500 int *n_allocated,
5501 int *n_comp_units,
5502 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5503{
be391dca 5504 gdb_byte *info_ptr;
36586728 5505 bfd *abfd = section->asection->owner;
be391dca 5506
36586728 5507 dwarf2_read_section (objfile, section);
ae038cb0 5508
36586728 5509 info_ptr = section->buffer;
6e70227d 5510
36586728 5511 while (info_ptr < section->buffer + section->size)
ae038cb0 5512 {
c764a876 5513 unsigned int length, initial_length_size;
ae038cb0 5514 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5515 sect_offset offset;
ae038cb0 5516
36586728 5517 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5518
5519 /* Read just enough information to find out where the next
5520 compilation unit is. */
36586728 5521 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5522
5523 /* Save the compilation unit for later lookup. */
5524 this_cu = obstack_alloc (&objfile->objfile_obstack,
5525 sizeof (struct dwarf2_per_cu_data));
5526 memset (this_cu, 0, sizeof (*this_cu));
5527 this_cu->offset = offset;
c764a876 5528 this_cu->length = length + initial_length_size;
36586728 5529 this_cu->is_dwz = is_dwz;
9291a0cd 5530 this_cu->objfile = objfile;
36586728 5531 this_cu->info_or_types_section = section;
ae038cb0 5532
36586728 5533 if (*n_comp_units == *n_allocated)
ae038cb0 5534 {
36586728
TT
5535 *n_allocated *= 2;
5536 *all_comp_units = xrealloc (*all_comp_units,
5537 *n_allocated
5538 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5539 }
36586728
TT
5540 (*all_comp_units)[*n_comp_units] = this_cu;
5541 ++*n_comp_units;
ae038cb0
DJ
5542
5543 info_ptr = info_ptr + this_cu->length;
5544 }
36586728
TT
5545}
5546
5547/* Create a list of all compilation units in OBJFILE.
5548 This is only done for -readnow and building partial symtabs. */
5549
5550static void
5551create_all_comp_units (struct objfile *objfile)
5552{
5553 int n_allocated;
5554 int n_comp_units;
5555 struct dwarf2_per_cu_data **all_comp_units;
5556
5557 n_comp_units = 0;
5558 n_allocated = 10;
5559 all_comp_units = xmalloc (n_allocated
5560 * sizeof (struct dwarf2_per_cu_data *));
5561
5562 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5563 &n_allocated, &n_comp_units, &all_comp_units);
5564
5565 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5566 {
5567 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5568
5569 read_comp_units_from_section (objfile, &dwz->info, 1,
5570 &n_allocated, &n_comp_units,
5571 &all_comp_units);
5572 }
ae038cb0
DJ
5573
5574 dwarf2_per_objfile->all_comp_units
5575 = obstack_alloc (&objfile->objfile_obstack,
5576 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5577 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5578 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5579 xfree (all_comp_units);
5580 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5581}
5582
5734ee8b
DJ
5583/* Process all loaded DIEs for compilation unit CU, starting at
5584 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5585 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5586 DW_AT_ranges). If NEED_PC is set, then this function will set
5587 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5588 and record the covered ranges in the addrmap. */
c906108c 5589
72bf9492
DJ
5590static void
5591scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5592 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5593{
72bf9492 5594 struct partial_die_info *pdi;
c906108c 5595
91c24f0a
DC
5596 /* Now, march along the PDI's, descending into ones which have
5597 interesting children but skipping the children of the other ones,
5598 until we reach the end of the compilation unit. */
c906108c 5599
72bf9492 5600 pdi = first_die;
91c24f0a 5601
72bf9492
DJ
5602 while (pdi != NULL)
5603 {
5604 fixup_partial_die (pdi, cu);
c906108c 5605
f55ee35c 5606 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5607 children, so we need to look at them. Ditto for anonymous
5608 enums. */
933c6fe4 5609
72bf9492 5610 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5611 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5612 || pdi->tag == DW_TAG_imported_unit)
c906108c 5613 {
72bf9492 5614 switch (pdi->tag)
c906108c
SS
5615 {
5616 case DW_TAG_subprogram:
5734ee8b 5617 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5618 break;
72929c62 5619 case DW_TAG_constant:
c906108c
SS
5620 case DW_TAG_variable:
5621 case DW_TAG_typedef:
91c24f0a 5622 case DW_TAG_union_type:
72bf9492 5623 if (!pdi->is_declaration)
63d06c5c 5624 {
72bf9492 5625 add_partial_symbol (pdi, cu);
63d06c5c
DC
5626 }
5627 break;
c906108c 5628 case DW_TAG_class_type:
680b30c7 5629 case DW_TAG_interface_type:
c906108c 5630 case DW_TAG_structure_type:
72bf9492 5631 if (!pdi->is_declaration)
c906108c 5632 {
72bf9492 5633 add_partial_symbol (pdi, cu);
c906108c
SS
5634 }
5635 break;
91c24f0a 5636 case DW_TAG_enumeration_type:
72bf9492
DJ
5637 if (!pdi->is_declaration)
5638 add_partial_enumeration (pdi, cu);
c906108c
SS
5639 break;
5640 case DW_TAG_base_type:
a02abb62 5641 case DW_TAG_subrange_type:
c906108c 5642 /* File scope base type definitions are added to the partial
c5aa993b 5643 symbol table. */
72bf9492 5644 add_partial_symbol (pdi, cu);
c906108c 5645 break;
d9fa45fe 5646 case DW_TAG_namespace:
5734ee8b 5647 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5648 break;
5d7cb8df
JK
5649 case DW_TAG_module:
5650 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5651 break;
95554aad
TT
5652 case DW_TAG_imported_unit:
5653 {
5654 struct dwarf2_per_cu_data *per_cu;
5655
f4dc4d17
DE
5656 /* For now we don't handle imported units in type units. */
5657 if (cu->per_cu->is_debug_types)
5658 {
5659 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5660 " supported in type units [in module %s]"),
5661 cu->objfile->name);
5662 }
5663
95554aad 5664 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5665 pdi->is_dwz,
95554aad
TT
5666 cu->objfile);
5667
5668 /* Go read the partial unit, if needed. */
5669 if (per_cu->v.psymtab == NULL)
5670 process_psymtab_comp_unit (per_cu, 1);
5671
f4dc4d17
DE
5672 VEC_safe_push (dwarf2_per_cu_ptr,
5673 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5674 }
5675 break;
c906108c
SS
5676 default:
5677 break;
5678 }
5679 }
5680
72bf9492
DJ
5681 /* If the die has a sibling, skip to the sibling. */
5682
5683 pdi = pdi->die_sibling;
5684 }
5685}
5686
5687/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5688
72bf9492 5689 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5690 name is concatenated with "::" and the partial DIE's name. For
5691 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5692 Enumerators are an exception; they use the scope of their parent
5693 enumeration type, i.e. the name of the enumeration type is not
5694 prepended to the enumerator.
91c24f0a 5695
72bf9492
DJ
5696 There are two complexities. One is DW_AT_specification; in this
5697 case "parent" means the parent of the target of the specification,
5698 instead of the direct parent of the DIE. The other is compilers
5699 which do not emit DW_TAG_namespace; in this case we try to guess
5700 the fully qualified name of structure types from their members'
5701 linkage names. This must be done using the DIE's children rather
5702 than the children of any DW_AT_specification target. We only need
5703 to do this for structures at the top level, i.e. if the target of
5704 any DW_AT_specification (if any; otherwise the DIE itself) does not
5705 have a parent. */
5706
5707/* Compute the scope prefix associated with PDI's parent, in
5708 compilation unit CU. The result will be allocated on CU's
5709 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5710 field. NULL is returned if no prefix is necessary. */
5711static char *
5712partial_die_parent_scope (struct partial_die_info *pdi,
5713 struct dwarf2_cu *cu)
5714{
5715 char *grandparent_scope;
5716 struct partial_die_info *parent, *real_pdi;
91c24f0a 5717
72bf9492
DJ
5718 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5719 then this means the parent of the specification DIE. */
5720
5721 real_pdi = pdi;
72bf9492 5722 while (real_pdi->has_specification)
36586728
TT
5723 real_pdi = find_partial_die (real_pdi->spec_offset,
5724 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5725
5726 parent = real_pdi->die_parent;
5727 if (parent == NULL)
5728 return NULL;
5729
5730 if (parent->scope_set)
5731 return parent->scope;
5732
5733 fixup_partial_die (parent, cu);
5734
10b3939b 5735 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5736
acebe513
UW
5737 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5738 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5739 Work around this problem here. */
5740 if (cu->language == language_cplus
6e70227d 5741 && parent->tag == DW_TAG_namespace
acebe513
UW
5742 && strcmp (parent->name, "::") == 0
5743 && grandparent_scope == NULL)
5744 {
5745 parent->scope = NULL;
5746 parent->scope_set = 1;
5747 return NULL;
5748 }
5749
9c6c53f7
SA
5750 if (pdi->tag == DW_TAG_enumerator)
5751 /* Enumerators should not get the name of the enumeration as a prefix. */
5752 parent->scope = grandparent_scope;
5753 else if (parent->tag == DW_TAG_namespace
f55ee35c 5754 || parent->tag == DW_TAG_module
72bf9492
DJ
5755 || parent->tag == DW_TAG_structure_type
5756 || parent->tag == DW_TAG_class_type
680b30c7 5757 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5758 || parent->tag == DW_TAG_union_type
5759 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5760 {
5761 if (grandparent_scope == NULL)
5762 parent->scope = parent->name;
5763 else
3e43a32a
MS
5764 parent->scope = typename_concat (&cu->comp_unit_obstack,
5765 grandparent_scope,
f55ee35c 5766 parent->name, 0, cu);
72bf9492 5767 }
72bf9492
DJ
5768 else
5769 {
5770 /* FIXME drow/2004-04-01: What should we be doing with
5771 function-local names? For partial symbols, we should probably be
5772 ignoring them. */
5773 complaint (&symfile_complaints,
e2e0b3e5 5774 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5775 parent->tag, pdi->offset.sect_off);
72bf9492 5776 parent->scope = grandparent_scope;
c906108c
SS
5777 }
5778
72bf9492
DJ
5779 parent->scope_set = 1;
5780 return parent->scope;
5781}
5782
5783/* Return the fully scoped name associated with PDI, from compilation unit
5784 CU. The result will be allocated with malloc. */
4568ecf9 5785
72bf9492
DJ
5786static char *
5787partial_die_full_name (struct partial_die_info *pdi,
5788 struct dwarf2_cu *cu)
5789{
5790 char *parent_scope;
5791
98bfdba5
PA
5792 /* If this is a template instantiation, we can not work out the
5793 template arguments from partial DIEs. So, unfortunately, we have
5794 to go through the full DIEs. At least any work we do building
5795 types here will be reused if full symbols are loaded later. */
5796 if (pdi->has_template_arguments)
5797 {
5798 fixup_partial_die (pdi, cu);
5799
5800 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5801 {
5802 struct die_info *die;
5803 struct attribute attr;
5804 struct dwarf2_cu *ref_cu = cu;
5805
b64f50a1 5806 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5807 attr.name = 0;
5808 attr.form = DW_FORM_ref_addr;
4568ecf9 5809 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5810 die = follow_die_ref (NULL, &attr, &ref_cu);
5811
5812 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5813 }
5814 }
5815
72bf9492
DJ
5816 parent_scope = partial_die_parent_scope (pdi, cu);
5817 if (parent_scope == NULL)
5818 return NULL;
5819 else
f55ee35c 5820 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5821}
5822
5823static void
72bf9492 5824add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5825{
e7c27a73 5826 struct objfile *objfile = cu->objfile;
c906108c 5827 CORE_ADDR addr = 0;
decbce07 5828 char *actual_name = NULL;
e142c38c 5829 CORE_ADDR baseaddr;
72bf9492 5830 int built_actual_name = 0;
e142c38c
DJ
5831
5832 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5833
94af9270
KS
5834 actual_name = partial_die_full_name (pdi, cu);
5835 if (actual_name)
5836 built_actual_name = 1;
63d06c5c 5837
72bf9492
DJ
5838 if (actual_name == NULL)
5839 actual_name = pdi->name;
5840
c906108c
SS
5841 switch (pdi->tag)
5842 {
5843 case DW_TAG_subprogram:
2cfa0c8d 5844 if (pdi->is_external || cu->language == language_ada)
c906108c 5845 {
2cfa0c8d
JB
5846 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5847 of the global scope. But in Ada, we want to be able to access
5848 nested procedures globally. So all Ada subprograms are stored
5849 in the global scope. */
f47fb265 5850 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5851 mst_text, objfile); */
f47fb265
MS
5852 add_psymbol_to_list (actual_name, strlen (actual_name),
5853 built_actual_name,
5854 VAR_DOMAIN, LOC_BLOCK,
5855 &objfile->global_psymbols,
5856 0, pdi->lowpc + baseaddr,
5857 cu->language, objfile);
c906108c
SS
5858 }
5859 else
5860 {
f47fb265 5861 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5862 mst_file_text, objfile); */
f47fb265
MS
5863 add_psymbol_to_list (actual_name, strlen (actual_name),
5864 built_actual_name,
5865 VAR_DOMAIN, LOC_BLOCK,
5866 &objfile->static_psymbols,
5867 0, pdi->lowpc + baseaddr,
5868 cu->language, objfile);
c906108c
SS
5869 }
5870 break;
72929c62
JB
5871 case DW_TAG_constant:
5872 {
5873 struct psymbol_allocation_list *list;
5874
5875 if (pdi->is_external)
5876 list = &objfile->global_psymbols;
5877 else
5878 list = &objfile->static_psymbols;
f47fb265
MS
5879 add_psymbol_to_list (actual_name, strlen (actual_name),
5880 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5881 list, 0, 0, cu->language, objfile);
72929c62
JB
5882 }
5883 break;
c906108c 5884 case DW_TAG_variable:
95554aad
TT
5885 if (pdi->d.locdesc)
5886 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5887
95554aad 5888 if (pdi->d.locdesc
caac4577
JG
5889 && addr == 0
5890 && !dwarf2_per_objfile->has_section_at_zero)
5891 {
5892 /* A global or static variable may also have been stripped
5893 out by the linker if unused, in which case its address
5894 will be nullified; do not add such variables into partial
5895 symbol table then. */
5896 }
5897 else if (pdi->is_external)
c906108c
SS
5898 {
5899 /* Global Variable.
5900 Don't enter into the minimal symbol tables as there is
5901 a minimal symbol table entry from the ELF symbols already.
5902 Enter into partial symbol table if it has a location
5903 descriptor or a type.
5904 If the location descriptor is missing, new_symbol will create
5905 a LOC_UNRESOLVED symbol, the address of the variable will then
5906 be determined from the minimal symbol table whenever the variable
5907 is referenced.
5908 The address for the partial symbol table entry is not
5909 used by GDB, but it comes in handy for debugging partial symbol
5910 table building. */
5911
95554aad 5912 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5913 add_psymbol_to_list (actual_name, strlen (actual_name),
5914 built_actual_name,
5915 VAR_DOMAIN, LOC_STATIC,
5916 &objfile->global_psymbols,
5917 0, addr + baseaddr,
5918 cu->language, objfile);
c906108c
SS
5919 }
5920 else
5921 {
0963b4bd 5922 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5923 if (pdi->d.locdesc == NULL)
decbce07
MS
5924 {
5925 if (built_actual_name)
5926 xfree (actual_name);
5927 return;
5928 }
f47fb265 5929 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5930 mst_file_data, objfile); */
f47fb265
MS
5931 add_psymbol_to_list (actual_name, strlen (actual_name),
5932 built_actual_name,
5933 VAR_DOMAIN, LOC_STATIC,
5934 &objfile->static_psymbols,
5935 0, addr + baseaddr,
5936 cu->language, objfile);
c906108c
SS
5937 }
5938 break;
5939 case DW_TAG_typedef:
5940 case DW_TAG_base_type:
a02abb62 5941 case DW_TAG_subrange_type:
38d518c9 5942 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5943 built_actual_name,
176620f1 5944 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5945 &objfile->static_psymbols,
e142c38c 5946 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5947 break;
72bf9492
DJ
5948 case DW_TAG_namespace:
5949 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5950 built_actual_name,
72bf9492
DJ
5951 VAR_DOMAIN, LOC_TYPEDEF,
5952 &objfile->global_psymbols,
5953 0, (CORE_ADDR) 0, cu->language, objfile);
5954 break;
c906108c 5955 case DW_TAG_class_type:
680b30c7 5956 case DW_TAG_interface_type:
c906108c
SS
5957 case DW_TAG_structure_type:
5958 case DW_TAG_union_type:
5959 case DW_TAG_enumeration_type:
fa4028e9
JB
5960 /* Skip external references. The DWARF standard says in the section
5961 about "Structure, Union, and Class Type Entries": "An incomplete
5962 structure, union or class type is represented by a structure,
5963 union or class entry that does not have a byte size attribute
5964 and that has a DW_AT_declaration attribute." */
5965 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5966 {
5967 if (built_actual_name)
5968 xfree (actual_name);
5969 return;
5970 }
fa4028e9 5971
63d06c5c
DC
5972 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5973 static vs. global. */
38d518c9 5974 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5975 built_actual_name,
176620f1 5976 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5977 (cu->language == language_cplus
5978 || cu->language == language_java)
63d06c5c
DC
5979 ? &objfile->global_psymbols
5980 : &objfile->static_psymbols,
e142c38c 5981 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5982
c906108c
SS
5983 break;
5984 case DW_TAG_enumerator:
38d518c9 5985 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5986 built_actual_name,
176620f1 5987 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5988 (cu->language == language_cplus
5989 || cu->language == language_java)
f6fe98ef
DJ
5990 ? &objfile->global_psymbols
5991 : &objfile->static_psymbols,
e142c38c 5992 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5993 break;
5994 default:
5995 break;
5996 }
5c4e30ca 5997
72bf9492
DJ
5998 if (built_actual_name)
5999 xfree (actual_name);
c906108c
SS
6000}
6001
5c4e30ca
DC
6002/* Read a partial die corresponding to a namespace; also, add a symbol
6003 corresponding to that namespace to the symbol table. NAMESPACE is
6004 the name of the enclosing namespace. */
91c24f0a 6005
72bf9492
DJ
6006static void
6007add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6008 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6009 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6010{
72bf9492 6011 /* Add a symbol for the namespace. */
e7c27a73 6012
72bf9492 6013 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6014
6015 /* Now scan partial symbols in that namespace. */
6016
91c24f0a 6017 if (pdi->has_children)
5734ee8b 6018 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6019}
6020
5d7cb8df
JK
6021/* Read a partial die corresponding to a Fortran module. */
6022
6023static void
6024add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6025 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6026{
f55ee35c 6027 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6028
6029 if (pdi->has_children)
6030 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6031}
6032
bc30ff58
JB
6033/* Read a partial die corresponding to a subprogram and create a partial
6034 symbol for that subprogram. When the CU language allows it, this
6035 routine also defines a partial symbol for each nested subprogram
6036 that this subprogram contains.
6e70227d 6037
bc30ff58
JB
6038 DIE my also be a lexical block, in which case we simply search
6039 recursively for suprograms defined inside that lexical block.
6040 Again, this is only performed when the CU language allows this
6041 type of definitions. */
6042
6043static void
6044add_partial_subprogram (struct partial_die_info *pdi,
6045 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6046 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6047{
6048 if (pdi->tag == DW_TAG_subprogram)
6049 {
6050 if (pdi->has_pc_info)
6051 {
6052 if (pdi->lowpc < *lowpc)
6053 *lowpc = pdi->lowpc;
6054 if (pdi->highpc > *highpc)
6055 *highpc = pdi->highpc;
5734ee8b
DJ
6056 if (need_pc)
6057 {
6058 CORE_ADDR baseaddr;
6059 struct objfile *objfile = cu->objfile;
6060
6061 baseaddr = ANOFFSET (objfile->section_offsets,
6062 SECT_OFF_TEXT (objfile));
6063 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6064 pdi->lowpc + baseaddr,
6065 pdi->highpc - 1 + baseaddr,
9291a0cd 6066 cu->per_cu->v.psymtab);
5734ee8b 6067 }
481860b3
GB
6068 }
6069
6070 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6071 {
bc30ff58 6072 if (!pdi->is_declaration)
e8d05480
JB
6073 /* Ignore subprogram DIEs that do not have a name, they are
6074 illegal. Do not emit a complaint at this point, we will
6075 do so when we convert this psymtab into a symtab. */
6076 if (pdi->name)
6077 add_partial_symbol (pdi, cu);
bc30ff58
JB
6078 }
6079 }
6e70227d 6080
bc30ff58
JB
6081 if (! pdi->has_children)
6082 return;
6083
6084 if (cu->language == language_ada)
6085 {
6086 pdi = pdi->die_child;
6087 while (pdi != NULL)
6088 {
6089 fixup_partial_die (pdi, cu);
6090 if (pdi->tag == DW_TAG_subprogram
6091 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6092 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6093 pdi = pdi->die_sibling;
6094 }
6095 }
6096}
6097
91c24f0a
DC
6098/* Read a partial die corresponding to an enumeration type. */
6099
72bf9492
DJ
6100static void
6101add_partial_enumeration (struct partial_die_info *enum_pdi,
6102 struct dwarf2_cu *cu)
91c24f0a 6103{
72bf9492 6104 struct partial_die_info *pdi;
91c24f0a
DC
6105
6106 if (enum_pdi->name != NULL)
72bf9492
DJ
6107 add_partial_symbol (enum_pdi, cu);
6108
6109 pdi = enum_pdi->die_child;
6110 while (pdi)
91c24f0a 6111 {
72bf9492 6112 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6113 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6114 else
72bf9492
DJ
6115 add_partial_symbol (pdi, cu);
6116 pdi = pdi->die_sibling;
91c24f0a 6117 }
91c24f0a
DC
6118}
6119
6caca83c
CC
6120/* Return the initial uleb128 in the die at INFO_PTR. */
6121
6122static unsigned int
6123peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6124{
6125 unsigned int bytes_read;
6126
6127 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6128}
6129
4bb7a0a7
DJ
6130/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6131 Return the corresponding abbrev, or NULL if the number is zero (indicating
6132 an empty DIE). In either case *BYTES_READ will be set to the length of
6133 the initial number. */
6134
6135static struct abbrev_info *
fe1b8b76 6136peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6137 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6138{
6139 bfd *abfd = cu->objfile->obfd;
6140 unsigned int abbrev_number;
6141 struct abbrev_info *abbrev;
6142
6143 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6144
6145 if (abbrev_number == 0)
6146 return NULL;
6147
433df2d4 6148 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6149 if (!abbrev)
6150 {
3e43a32a
MS
6151 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6152 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6153 }
6154
6155 return abbrev;
6156}
6157
93311388
DE
6158/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6159 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6160 DIE. Any children of the skipped DIEs will also be skipped. */
6161
fe1b8b76 6162static gdb_byte *
dee91e82 6163skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6164{
dee91e82 6165 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6166 struct abbrev_info *abbrev;
6167 unsigned int bytes_read;
6168
6169 while (1)
6170 {
6171 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6172 if (abbrev == NULL)
6173 return info_ptr + bytes_read;
6174 else
dee91e82 6175 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6176 }
6177}
6178
93311388
DE
6179/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6180 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6181 abbrev corresponding to that skipped uleb128 should be passed in
6182 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6183 children. */
6184
fe1b8b76 6185static gdb_byte *
dee91e82
DE
6186skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6187 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6188{
6189 unsigned int bytes_read;
6190 struct attribute attr;
dee91e82
DE
6191 bfd *abfd = reader->abfd;
6192 struct dwarf2_cu *cu = reader->cu;
6193 gdb_byte *buffer = reader->buffer;
f664829e
DE
6194 const gdb_byte *buffer_end = reader->buffer_end;
6195 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6196 unsigned int form, i;
6197
6198 for (i = 0; i < abbrev->num_attrs; i++)
6199 {
6200 /* The only abbrev we care about is DW_AT_sibling. */
6201 if (abbrev->attrs[i].name == DW_AT_sibling)
6202 {
dee91e82 6203 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6204 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6205 complaint (&symfile_complaints,
6206 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6207 else
b64f50a1 6208 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6209 }
6210
6211 /* If it isn't DW_AT_sibling, skip this attribute. */
6212 form = abbrev->attrs[i].form;
6213 skip_attribute:
6214 switch (form)
6215 {
4bb7a0a7 6216 case DW_FORM_ref_addr:
ae411497
TT
6217 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6218 and later it is offset sized. */
6219 if (cu->header.version == 2)
6220 info_ptr += cu->header.addr_size;
6221 else
6222 info_ptr += cu->header.offset_size;
6223 break;
36586728
TT
6224 case DW_FORM_GNU_ref_alt:
6225 info_ptr += cu->header.offset_size;
6226 break;
ae411497 6227 case DW_FORM_addr:
4bb7a0a7
DJ
6228 info_ptr += cu->header.addr_size;
6229 break;
6230 case DW_FORM_data1:
6231 case DW_FORM_ref1:
6232 case DW_FORM_flag:
6233 info_ptr += 1;
6234 break;
2dc7f7b3
TT
6235 case DW_FORM_flag_present:
6236 break;
4bb7a0a7
DJ
6237 case DW_FORM_data2:
6238 case DW_FORM_ref2:
6239 info_ptr += 2;
6240 break;
6241 case DW_FORM_data4:
6242 case DW_FORM_ref4:
6243 info_ptr += 4;
6244 break;
6245 case DW_FORM_data8:
6246 case DW_FORM_ref8:
55f1336d 6247 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6248 info_ptr += 8;
6249 break;
6250 case DW_FORM_string:
9b1c24c8 6251 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6252 info_ptr += bytes_read;
6253 break;
2dc7f7b3 6254 case DW_FORM_sec_offset:
4bb7a0a7 6255 case DW_FORM_strp:
36586728 6256 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6257 info_ptr += cu->header.offset_size;
6258 break;
2dc7f7b3 6259 case DW_FORM_exprloc:
4bb7a0a7
DJ
6260 case DW_FORM_block:
6261 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6262 info_ptr += bytes_read;
6263 break;
6264 case DW_FORM_block1:
6265 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6266 break;
6267 case DW_FORM_block2:
6268 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6269 break;
6270 case DW_FORM_block4:
6271 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6272 break;
6273 case DW_FORM_sdata:
6274 case DW_FORM_udata:
6275 case DW_FORM_ref_udata:
3019eac3
DE
6276 case DW_FORM_GNU_addr_index:
6277 case DW_FORM_GNU_str_index:
f664829e 6278 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6279 break;
6280 case DW_FORM_indirect:
6281 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6282 info_ptr += bytes_read;
6283 /* We need to continue parsing from here, so just go back to
6284 the top. */
6285 goto skip_attribute;
6286
6287 default:
3e43a32a
MS
6288 error (_("Dwarf Error: Cannot handle %s "
6289 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6290 dwarf_form_name (form),
6291 bfd_get_filename (abfd));
6292 }
6293 }
6294
6295 if (abbrev->has_children)
dee91e82 6296 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6297 else
6298 return info_ptr;
6299}
6300
93311388 6301/* Locate ORIG_PDI's sibling.
dee91e82 6302 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6303
fe1b8b76 6304static gdb_byte *
dee91e82
DE
6305locate_pdi_sibling (const struct die_reader_specs *reader,
6306 struct partial_die_info *orig_pdi,
6307 gdb_byte *info_ptr)
91c24f0a
DC
6308{
6309 /* Do we know the sibling already? */
72bf9492 6310
91c24f0a
DC
6311 if (orig_pdi->sibling)
6312 return orig_pdi->sibling;
6313
6314 /* Are there any children to deal with? */
6315
6316 if (!orig_pdi->has_children)
6317 return info_ptr;
6318
4bb7a0a7 6319 /* Skip the children the long way. */
91c24f0a 6320
dee91e82 6321 return skip_children (reader, info_ptr);
91c24f0a
DC
6322}
6323
c906108c
SS
6324/* Expand this partial symbol table into a full symbol table. */
6325
6326static void
fba45db2 6327dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 6328{
c906108c
SS
6329 if (pst != NULL)
6330 {
6331 if (pst->readin)
6332 {
3e43a32a
MS
6333 warning (_("bug: psymtab for %s is already read in."),
6334 pst->filename);
c906108c
SS
6335 }
6336 else
6337 {
6338 if (info_verbose)
6339 {
3e43a32a
MS
6340 printf_filtered (_("Reading in symbols for %s..."),
6341 pst->filename);
c906108c
SS
6342 gdb_flush (gdb_stdout);
6343 }
6344
10b3939b
DJ
6345 /* Restore our global data. */
6346 dwarf2_per_objfile = objfile_data (pst->objfile,
6347 dwarf2_objfile_data_key);
6348
b2ab525c
KB
6349 /* If this psymtab is constructed from a debug-only objfile, the
6350 has_section_at_zero flag will not necessarily be correct. We
6351 can get the correct value for this flag by looking at the data
6352 associated with the (presumably stripped) associated objfile. */
6353 if (pst->objfile->separate_debug_objfile_backlink)
6354 {
6355 struct dwarf2_per_objfile *dpo_backlink
6356 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6357 dwarf2_objfile_data_key);
9a619af0 6358
b2ab525c
KB
6359 dwarf2_per_objfile->has_section_at_zero
6360 = dpo_backlink->has_section_at_zero;
6361 }
6362
98bfdba5
PA
6363 dwarf2_per_objfile->reading_partial_symbols = 0;
6364
c906108c
SS
6365 psymtab_to_symtab_1 (pst);
6366
6367 /* Finish up the debug error message. */
6368 if (info_verbose)
a3f17187 6369 printf_filtered (_("done.\n"));
c906108c
SS
6370 }
6371 }
95554aad
TT
6372
6373 process_cu_includes ();
c906108c 6374}
9cdd5dbd
DE
6375\f
6376/* Reading in full CUs. */
c906108c 6377
10b3939b
DJ
6378/* Add PER_CU to the queue. */
6379
6380static void
95554aad
TT
6381queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6382 enum language pretend_language)
10b3939b
DJ
6383{
6384 struct dwarf2_queue_item *item;
6385
6386 per_cu->queued = 1;
6387 item = xmalloc (sizeof (*item));
6388 item->per_cu = per_cu;
95554aad 6389 item->pretend_language = pretend_language;
10b3939b
DJ
6390 item->next = NULL;
6391
6392 if (dwarf2_queue == NULL)
6393 dwarf2_queue = item;
6394 else
6395 dwarf2_queue_tail->next = item;
6396
6397 dwarf2_queue_tail = item;
6398}
6399
0907af0c
DE
6400/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6401 unit and add it to our queue.
6402 The result is non-zero if PER_CU was queued, otherwise the result is zero
6403 meaning either PER_CU is already queued or it is already loaded. */
6404
6405static int
6406maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6407 struct dwarf2_per_cu_data *per_cu,
6408 enum language pretend_language)
6409{
6410 /* We may arrive here during partial symbol reading, if we need full
6411 DIEs to process an unusual case (e.g. template arguments). Do
6412 not queue PER_CU, just tell our caller to load its DIEs. */
6413 if (dwarf2_per_objfile->reading_partial_symbols)
6414 {
6415 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6416 return 1;
6417 return 0;
6418 }
6419
6420 /* Mark the dependence relation so that we don't flush PER_CU
6421 too early. */
6422 dwarf2_add_dependence (this_cu, per_cu);
6423
6424 /* If it's already on the queue, we have nothing to do. */
6425 if (per_cu->queued)
6426 return 0;
6427
6428 /* If the compilation unit is already loaded, just mark it as
6429 used. */
6430 if (per_cu->cu != NULL)
6431 {
6432 per_cu->cu->last_used = 0;
6433 return 0;
6434 }
6435
6436 /* Add it to the queue. */
6437 queue_comp_unit (per_cu, pretend_language);
6438
6439 return 1;
6440}
6441
10b3939b
DJ
6442/* Process the queue. */
6443
6444static void
a0f42c21 6445process_queue (void)
10b3939b
DJ
6446{
6447 struct dwarf2_queue_item *item, *next_item;
6448
45cfd468
DE
6449 if (dwarf2_read_debug)
6450 {
6451 fprintf_unfiltered (gdb_stdlog,
6452 "Expanding one or more symtabs of objfile %s ...\n",
6453 dwarf2_per_objfile->objfile->name);
6454 }
6455
03dd20cc
DJ
6456 /* The queue starts out with one item, but following a DIE reference
6457 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6458 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6459 {
9291a0cd
TT
6460 if (dwarf2_per_objfile->using_index
6461 ? !item->per_cu->v.quick->symtab
6462 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6463 {
6464 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6465
6466 if (dwarf2_read_debug)
6467 {
6468 fprintf_unfiltered (gdb_stdlog,
6469 "Expanding symtab of %s at offset 0x%x\n",
6470 per_cu->is_debug_types ? "TU" : "CU",
6471 per_cu->offset.sect_off);
6472 }
6473
6474 if (per_cu->is_debug_types)
6475 process_full_type_unit (per_cu, item->pretend_language);
6476 else
6477 process_full_comp_unit (per_cu, item->pretend_language);
6478
6479 if (dwarf2_read_debug)
6480 {
6481 fprintf_unfiltered (gdb_stdlog,
6482 "Done expanding %s at offset 0x%x\n",
6483 per_cu->is_debug_types ? "TU" : "CU",
6484 per_cu->offset.sect_off);
6485 }
6486 }
10b3939b
DJ
6487
6488 item->per_cu->queued = 0;
6489 next_item = item->next;
6490 xfree (item);
6491 }
6492
6493 dwarf2_queue_tail = NULL;
45cfd468
DE
6494
6495 if (dwarf2_read_debug)
6496 {
6497 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6498 dwarf2_per_objfile->objfile->name);
6499 }
10b3939b
DJ
6500}
6501
6502/* Free all allocated queue entries. This function only releases anything if
6503 an error was thrown; if the queue was processed then it would have been
6504 freed as we went along. */
6505
6506static void
6507dwarf2_release_queue (void *dummy)
6508{
6509 struct dwarf2_queue_item *item, *last;
6510
6511 item = dwarf2_queue;
6512 while (item)
6513 {
6514 /* Anything still marked queued is likely to be in an
6515 inconsistent state, so discard it. */
6516 if (item->per_cu->queued)
6517 {
6518 if (item->per_cu->cu != NULL)
dee91e82 6519 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6520 item->per_cu->queued = 0;
6521 }
6522
6523 last = item;
6524 item = item->next;
6525 xfree (last);
6526 }
6527
6528 dwarf2_queue = dwarf2_queue_tail = NULL;
6529}
6530
6531/* Read in full symbols for PST, and anything it depends on. */
6532
c906108c 6533static void
fba45db2 6534psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6535{
10b3939b 6536 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6537 int i;
6538
95554aad
TT
6539 if (pst->readin)
6540 return;
6541
aaa75496 6542 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6543 if (!pst->dependencies[i]->readin
6544 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6545 {
6546 /* Inform about additional files that need to be read in. */
6547 if (info_verbose)
6548 {
a3f17187 6549 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6550 fputs_filtered (" ", gdb_stdout);
6551 wrap_here ("");
6552 fputs_filtered ("and ", gdb_stdout);
6553 wrap_here ("");
6554 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6555 wrap_here (""); /* Flush output. */
aaa75496
JB
6556 gdb_flush (gdb_stdout);
6557 }
6558 psymtab_to_symtab_1 (pst->dependencies[i]);
6559 }
6560
e38df1d0 6561 per_cu = pst->read_symtab_private;
10b3939b
DJ
6562
6563 if (per_cu == NULL)
aaa75496
JB
6564 {
6565 /* It's an include file, no symbols to read for it.
6566 Everything is in the parent symtab. */
6567 pst->readin = 1;
6568 return;
6569 }
c906108c 6570
a0f42c21 6571 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6572}
6573
dee91e82
DE
6574/* Trivial hash function for die_info: the hash value of a DIE
6575 is its offset in .debug_info for this objfile. */
10b3939b 6576
dee91e82
DE
6577static hashval_t
6578die_hash (const void *item)
10b3939b 6579{
dee91e82 6580 const struct die_info *die = item;
6502dd73 6581
dee91e82
DE
6582 return die->offset.sect_off;
6583}
63d06c5c 6584
dee91e82
DE
6585/* Trivial comparison function for die_info structures: two DIEs
6586 are equal if they have the same offset. */
98bfdba5 6587
dee91e82
DE
6588static int
6589die_eq (const void *item_lhs, const void *item_rhs)
6590{
6591 const struct die_info *die_lhs = item_lhs;
6592 const struct die_info *die_rhs = item_rhs;
c906108c 6593
dee91e82
DE
6594 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6595}
c906108c 6596
dee91e82
DE
6597/* die_reader_func for load_full_comp_unit.
6598 This is identical to read_signatured_type_reader,
6599 but is kept separate for now. */
c906108c 6600
dee91e82
DE
6601static void
6602load_full_comp_unit_reader (const struct die_reader_specs *reader,
6603 gdb_byte *info_ptr,
6604 struct die_info *comp_unit_die,
6605 int has_children,
6606 void *data)
6607{
6608 struct dwarf2_cu *cu = reader->cu;
95554aad 6609 enum language *language_ptr = data;
6caca83c 6610
dee91e82
DE
6611 gdb_assert (cu->die_hash == NULL);
6612 cu->die_hash =
6613 htab_create_alloc_ex (cu->header.length / 12,
6614 die_hash,
6615 die_eq,
6616 NULL,
6617 &cu->comp_unit_obstack,
6618 hashtab_obstack_allocate,
6619 dummy_obstack_deallocate);
e142c38c 6620
dee91e82
DE
6621 if (has_children)
6622 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6623 &info_ptr, comp_unit_die);
6624 cu->dies = comp_unit_die;
6625 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6626
6627 /* We try not to read any attributes in this function, because not
9cdd5dbd 6628 all CUs needed for references have been loaded yet, and symbol
10b3939b 6629 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6630 or we won't be able to build types correctly.
6631 Similarly, if we do not read the producer, we can not apply
6632 producer-specific interpretation. */
95554aad 6633 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6634}
10b3939b 6635
dee91e82 6636/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6637
dee91e82 6638static void
95554aad
TT
6639load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6640 enum language pretend_language)
dee91e82 6641{
3019eac3 6642 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6643
f4dc4d17
DE
6644 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6645 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6646}
6647
3da10d80
KS
6648/* Add a DIE to the delayed physname list. */
6649
6650static void
6651add_to_method_list (struct type *type, int fnfield_index, int index,
6652 const char *name, struct die_info *die,
6653 struct dwarf2_cu *cu)
6654{
6655 struct delayed_method_info mi;
6656 mi.type = type;
6657 mi.fnfield_index = fnfield_index;
6658 mi.index = index;
6659 mi.name = name;
6660 mi.die = die;
6661 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6662}
6663
6664/* A cleanup for freeing the delayed method list. */
6665
6666static void
6667free_delayed_list (void *ptr)
6668{
6669 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6670 if (cu->method_list != NULL)
6671 {
6672 VEC_free (delayed_method_info, cu->method_list);
6673 cu->method_list = NULL;
6674 }
6675}
6676
6677/* Compute the physnames of any methods on the CU's method list.
6678
6679 The computation of method physnames is delayed in order to avoid the
6680 (bad) condition that one of the method's formal parameters is of an as yet
6681 incomplete type. */
6682
6683static void
6684compute_delayed_physnames (struct dwarf2_cu *cu)
6685{
6686 int i;
6687 struct delayed_method_info *mi;
6688 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6689 {
1d06ead6 6690 const char *physname;
3da10d80
KS
6691 struct fn_fieldlist *fn_flp
6692 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6693 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6694 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6695 }
6696}
6697
a766d390
DE
6698/* Go objects should be embedded in a DW_TAG_module DIE,
6699 and it's not clear if/how imported objects will appear.
6700 To keep Go support simple until that's worked out,
6701 go back through what we've read and create something usable.
6702 We could do this while processing each DIE, and feels kinda cleaner,
6703 but that way is more invasive.
6704 This is to, for example, allow the user to type "p var" or "b main"
6705 without having to specify the package name, and allow lookups
6706 of module.object to work in contexts that use the expression
6707 parser. */
6708
6709static void
6710fixup_go_packaging (struct dwarf2_cu *cu)
6711{
6712 char *package_name = NULL;
6713 struct pending *list;
6714 int i;
6715
6716 for (list = global_symbols; list != NULL; list = list->next)
6717 {
6718 for (i = 0; i < list->nsyms; ++i)
6719 {
6720 struct symbol *sym = list->symbol[i];
6721
6722 if (SYMBOL_LANGUAGE (sym) == language_go
6723 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6724 {
6725 char *this_package_name = go_symbol_package_name (sym);
6726
6727 if (this_package_name == NULL)
6728 continue;
6729 if (package_name == NULL)
6730 package_name = this_package_name;
6731 else
6732 {
6733 if (strcmp (package_name, this_package_name) != 0)
6734 complaint (&symfile_complaints,
6735 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17
TT
6736 (SYMBOL_SYMTAB (sym)
6737 && SYMBOL_SYMTAB (sym)->filename
6738 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6739 : cu->objfile->name),
6740 this_package_name, package_name);
6741 xfree (this_package_name);
6742 }
6743 }
6744 }
6745 }
6746
6747 if (package_name != NULL)
6748 {
6749 struct objfile *objfile = cu->objfile;
6750 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6751 package_name, objfile);
6752 struct symbol *sym;
6753
6754 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6755
6756 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6757 SYMBOL_SET_LANGUAGE (sym, language_go);
6758 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6759 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6760 e.g., "main" finds the "main" module and not C's main(). */
6761 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6762 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6763 SYMBOL_TYPE (sym) = type;
6764
6765 add_symbol_to_list (sym, &global_symbols);
6766
6767 xfree (package_name);
6768 }
6769}
6770
95554aad
TT
6771static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6772
6773/* Return the symtab for PER_CU. This works properly regardless of
6774 whether we're using the index or psymtabs. */
6775
6776static struct symtab *
6777get_symtab (struct dwarf2_per_cu_data *per_cu)
6778{
6779 return (dwarf2_per_objfile->using_index
6780 ? per_cu->v.quick->symtab
6781 : per_cu->v.psymtab->symtab);
6782}
6783
6784/* A helper function for computing the list of all symbol tables
6785 included by PER_CU. */
6786
6787static void
6788recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6789 htab_t all_children,
6790 struct dwarf2_per_cu_data *per_cu)
6791{
6792 void **slot;
6793 int ix;
6794 struct dwarf2_per_cu_data *iter;
6795
6796 slot = htab_find_slot (all_children, per_cu, INSERT);
6797 if (*slot != NULL)
6798 {
6799 /* This inclusion and its children have been processed. */
6800 return;
6801 }
6802
6803 *slot = per_cu;
6804 /* Only add a CU if it has a symbol table. */
6805 if (get_symtab (per_cu) != NULL)
6806 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6807
6808 for (ix = 0;
f4dc4d17 6809 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6810 ++ix)
6811 recursively_compute_inclusions (result, all_children, iter);
6812}
6813
6814/* Compute the symtab 'includes' fields for the symtab related to
6815 PER_CU. */
6816
6817static void
6818compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6819{
f4dc4d17
DE
6820 gdb_assert (! per_cu->is_debug_types);
6821
6822 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6823 {
6824 int ix, len;
6825 struct dwarf2_per_cu_data *iter;
6826 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6827 htab_t all_children;
6828 struct symtab *symtab = get_symtab (per_cu);
6829
6830 /* If we don't have a symtab, we can just skip this case. */
6831 if (symtab == NULL)
6832 return;
6833
6834 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6835 NULL, xcalloc, xfree);
6836
6837 for (ix = 0;
f4dc4d17 6838 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6839 ix, iter);
6840 ++ix)
6841 recursively_compute_inclusions (&result_children, all_children, iter);
6842
6843 /* Now we have a transitive closure of all the included CUs, so
6844 we can convert it to a list of symtabs. */
6845 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6846 symtab->includes
6847 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6848 (len + 1) * sizeof (struct symtab *));
6849 for (ix = 0;
6850 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6851 ++ix)
6852 symtab->includes[ix] = get_symtab (iter);
6853 symtab->includes[len] = NULL;
6854
6855 VEC_free (dwarf2_per_cu_ptr, result_children);
6856 htab_delete (all_children);
6857 }
6858}
6859
6860/* Compute the 'includes' field for the symtabs of all the CUs we just
6861 read. */
6862
6863static void
6864process_cu_includes (void)
6865{
6866 int ix;
6867 struct dwarf2_per_cu_data *iter;
6868
6869 for (ix = 0;
6870 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6871 ix, iter);
6872 ++ix)
f4dc4d17
DE
6873 {
6874 if (! iter->is_debug_types)
6875 compute_symtab_includes (iter);
6876 }
95554aad
TT
6877
6878 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6879}
6880
9cdd5dbd 6881/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6882 already been loaded into memory. */
6883
6884static void
95554aad
TT
6885process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6886 enum language pretend_language)
10b3939b 6887{
10b3939b 6888 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6889 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6890 CORE_ADDR lowpc, highpc;
6891 struct symtab *symtab;
3da10d80 6892 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6893 CORE_ADDR baseaddr;
4359dff1 6894 struct block *static_block;
10b3939b
DJ
6895
6896 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6897
10b3939b
DJ
6898 buildsym_init ();
6899 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6900 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6901
6902 cu->list_in_scope = &file_symbols;
c906108c 6903
95554aad
TT
6904 cu->language = pretend_language;
6905 cu->language_defn = language_def (cu->language);
6906
c906108c 6907 /* Do line number decoding in read_file_scope () */
10b3939b 6908 process_die (cu->dies, cu);
c906108c 6909
a766d390
DE
6910 /* For now fudge the Go package. */
6911 if (cu->language == language_go)
6912 fixup_go_packaging (cu);
6913
3da10d80
KS
6914 /* Now that we have processed all the DIEs in the CU, all the types
6915 should be complete, and it should now be safe to compute all of the
6916 physnames. */
6917 compute_delayed_physnames (cu);
6918 do_cleanups (delayed_list_cleanup);
6919
fae299cd
DC
6920 /* Some compilers don't define a DW_AT_high_pc attribute for the
6921 compilation unit. If the DW_AT_high_pc is missing, synthesize
6922 it, by scanning the DIE's below the compilation unit. */
10b3939b 6923 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6924
36586728
TT
6925 static_block
6926 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6927 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6928
6929 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6930 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6931 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6932 addrmap to help ensure it has an accurate map of pc values belonging to
6933 this comp unit. */
6934 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6935
6936 symtab = end_symtab_from_static_block (static_block, objfile,
6937 SECT_OFF_TEXT (objfile), 0);
c906108c 6938
8be455d7 6939 if (symtab != NULL)
c906108c 6940 {
df15bd07 6941 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6942
8be455d7
JK
6943 /* Set symtab language to language from DW_AT_language. If the
6944 compilation is from a C file generated by language preprocessors, do
6945 not set the language if it was already deduced by start_subfile. */
6946 if (!(cu->language == language_c && symtab->language != language_c))
6947 symtab->language = cu->language;
6948
6949 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6950 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6951 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6952 there were bugs in prologue debug info, fixed later in GCC-4.5
6953 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6954
6955 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6956 needed, it would be wrong due to missing DW_AT_producer there.
6957
6958 Still one can confuse GDB by using non-standard GCC compilation
6959 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6960 */
ab260dad 6961 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6962 symtab->locations_valid = 1;
e0d00bc7
JK
6963
6964 if (gcc_4_minor >= 5)
6965 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6966
6967 symtab->call_site_htab = cu->call_site_htab;
c906108c 6968 }
9291a0cd
TT
6969
6970 if (dwarf2_per_objfile->using_index)
6971 per_cu->v.quick->symtab = symtab;
6972 else
6973 {
6974 struct partial_symtab *pst = per_cu->v.psymtab;
6975 pst->symtab = symtab;
6976 pst->readin = 1;
6977 }
c906108c 6978
95554aad
TT
6979 /* Push it for inclusion processing later. */
6980 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6981
c906108c 6982 do_cleanups (back_to);
f4dc4d17 6983}
45cfd468 6984
f4dc4d17
DE
6985/* Generate full symbol information for type unit PER_CU, whose DIEs have
6986 already been loaded into memory. */
6987
6988static void
6989process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6990 enum language pretend_language)
6991{
6992 struct dwarf2_cu *cu = per_cu->cu;
6993 struct objfile *objfile = per_cu->objfile;
6994 struct symtab *symtab;
6995 struct cleanup *back_to, *delayed_list_cleanup;
6996
6997 buildsym_init ();
6998 back_to = make_cleanup (really_free_pendings, NULL);
6999 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7000
7001 cu->list_in_scope = &file_symbols;
7002
7003 cu->language = pretend_language;
7004 cu->language_defn = language_def (cu->language);
7005
7006 /* The symbol tables are set up in read_type_unit_scope. */
7007 process_die (cu->dies, cu);
7008
7009 /* For now fudge the Go package. */
7010 if (cu->language == language_go)
7011 fixup_go_packaging (cu);
7012
7013 /* Now that we have processed all the DIEs in the CU, all the types
7014 should be complete, and it should now be safe to compute all of the
7015 physnames. */
7016 compute_delayed_physnames (cu);
7017 do_cleanups (delayed_list_cleanup);
7018
7019 /* TUs share symbol tables.
7020 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7021 of it with end_expandable_symtab. Otherwise, complete the addition of
7022 this TU's symbols to the existing symtab. */
f4dc4d17 7023 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7024 {
f4dc4d17
DE
7025 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7026 per_cu->s.type_unit_group->primary_symtab = symtab;
7027
7028 if (symtab != NULL)
7029 {
7030 /* Set symtab language to language from DW_AT_language. If the
7031 compilation is from a C file generated by language preprocessors,
7032 do not set the language if it was already deduced by
7033 start_subfile. */
7034 if (!(cu->language == language_c && symtab->language != language_c))
7035 symtab->language = cu->language;
7036 }
7037 }
7038 else
7039 {
7040 augment_type_symtab (objfile,
7041 per_cu->s.type_unit_group->primary_symtab);
7042 symtab = per_cu->s.type_unit_group->primary_symtab;
7043 }
7044
7045 if (dwarf2_per_objfile->using_index)
7046 per_cu->v.quick->symtab = symtab;
7047 else
7048 {
7049 struct partial_symtab *pst = per_cu->v.psymtab;
7050 pst->symtab = symtab;
7051 pst->readin = 1;
45cfd468 7052 }
f4dc4d17
DE
7053
7054 do_cleanups (back_to);
c906108c
SS
7055}
7056
95554aad
TT
7057/* Process an imported unit DIE. */
7058
7059static void
7060process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7061{
7062 struct attribute *attr;
7063
f4dc4d17
DE
7064 /* For now we don't handle imported units in type units. */
7065 if (cu->per_cu->is_debug_types)
7066 {
7067 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7068 " supported in type units [in module %s]"),
7069 cu->objfile->name);
7070 }
7071
95554aad
TT
7072 attr = dwarf2_attr (die, DW_AT_import, cu);
7073 if (attr != NULL)
7074 {
7075 struct dwarf2_per_cu_data *per_cu;
7076 struct symtab *imported_symtab;
7077 sect_offset offset;
36586728 7078 int is_dwz;
95554aad
TT
7079
7080 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7081 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7082 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7083
7084 /* Queue the unit, if needed. */
7085 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7086 load_full_comp_unit (per_cu, cu->language);
7087
f4dc4d17 7088 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7089 per_cu);
7090 }
7091}
7092
c906108c
SS
7093/* Process a die and its children. */
7094
7095static void
e7c27a73 7096process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7097{
7098 switch (die->tag)
7099 {
7100 case DW_TAG_padding:
7101 break;
7102 case DW_TAG_compile_unit:
95554aad 7103 case DW_TAG_partial_unit:
e7c27a73 7104 read_file_scope (die, cu);
c906108c 7105 break;
348e048f
DE
7106 case DW_TAG_type_unit:
7107 read_type_unit_scope (die, cu);
7108 break;
c906108c 7109 case DW_TAG_subprogram:
c906108c 7110 case DW_TAG_inlined_subroutine:
edb3359d 7111 read_func_scope (die, cu);
c906108c
SS
7112 break;
7113 case DW_TAG_lexical_block:
14898363
L
7114 case DW_TAG_try_block:
7115 case DW_TAG_catch_block:
e7c27a73 7116 read_lexical_block_scope (die, cu);
c906108c 7117 break;
96408a79
SA
7118 case DW_TAG_GNU_call_site:
7119 read_call_site_scope (die, cu);
7120 break;
c906108c 7121 case DW_TAG_class_type:
680b30c7 7122 case DW_TAG_interface_type:
c906108c
SS
7123 case DW_TAG_structure_type:
7124 case DW_TAG_union_type:
134d01f1 7125 process_structure_scope (die, cu);
c906108c
SS
7126 break;
7127 case DW_TAG_enumeration_type:
134d01f1 7128 process_enumeration_scope (die, cu);
c906108c 7129 break;
134d01f1 7130
f792889a
DJ
7131 /* These dies have a type, but processing them does not create
7132 a symbol or recurse to process the children. Therefore we can
7133 read them on-demand through read_type_die. */
c906108c 7134 case DW_TAG_subroutine_type:
72019c9c 7135 case DW_TAG_set_type:
c906108c 7136 case DW_TAG_array_type:
c906108c 7137 case DW_TAG_pointer_type:
c906108c 7138 case DW_TAG_ptr_to_member_type:
c906108c 7139 case DW_TAG_reference_type:
c906108c 7140 case DW_TAG_string_type:
c906108c 7141 break;
134d01f1 7142
c906108c 7143 case DW_TAG_base_type:
a02abb62 7144 case DW_TAG_subrange_type:
cb249c71 7145 case DW_TAG_typedef:
134d01f1
DJ
7146 /* Add a typedef symbol for the type definition, if it has a
7147 DW_AT_name. */
f792889a 7148 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7149 break;
c906108c 7150 case DW_TAG_common_block:
e7c27a73 7151 read_common_block (die, cu);
c906108c
SS
7152 break;
7153 case DW_TAG_common_inclusion:
7154 break;
d9fa45fe 7155 case DW_TAG_namespace:
63d06c5c 7156 processing_has_namespace_info = 1;
e7c27a73 7157 read_namespace (die, cu);
d9fa45fe 7158 break;
5d7cb8df 7159 case DW_TAG_module:
f55ee35c 7160 processing_has_namespace_info = 1;
5d7cb8df
JK
7161 read_module (die, cu);
7162 break;
d9fa45fe
DC
7163 case DW_TAG_imported_declaration:
7164 case DW_TAG_imported_module:
63d06c5c 7165 processing_has_namespace_info = 1;
27aa8d6a
SW
7166 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7167 || cu->language != language_fortran))
7168 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7169 dwarf_tag_name (die->tag));
7170 read_import_statement (die, cu);
d9fa45fe 7171 break;
95554aad
TT
7172
7173 case DW_TAG_imported_unit:
7174 process_imported_unit_die (die, cu);
7175 break;
7176
c906108c 7177 default:
e7c27a73 7178 new_symbol (die, NULL, cu);
c906108c
SS
7179 break;
7180 }
7181}
7182
94af9270
KS
7183/* A helper function for dwarf2_compute_name which determines whether DIE
7184 needs to have the name of the scope prepended to the name listed in the
7185 die. */
7186
7187static int
7188die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7189{
1c809c68
TT
7190 struct attribute *attr;
7191
94af9270
KS
7192 switch (die->tag)
7193 {
7194 case DW_TAG_namespace:
7195 case DW_TAG_typedef:
7196 case DW_TAG_class_type:
7197 case DW_TAG_interface_type:
7198 case DW_TAG_structure_type:
7199 case DW_TAG_union_type:
7200 case DW_TAG_enumeration_type:
7201 case DW_TAG_enumerator:
7202 case DW_TAG_subprogram:
7203 case DW_TAG_member:
7204 return 1;
7205
7206 case DW_TAG_variable:
c2b0a229 7207 case DW_TAG_constant:
94af9270
KS
7208 /* We only need to prefix "globally" visible variables. These include
7209 any variable marked with DW_AT_external or any variable that
7210 lives in a namespace. [Variables in anonymous namespaces
7211 require prefixing, but they are not DW_AT_external.] */
7212
7213 if (dwarf2_attr (die, DW_AT_specification, cu))
7214 {
7215 struct dwarf2_cu *spec_cu = cu;
9a619af0 7216
94af9270
KS
7217 return die_needs_namespace (die_specification (die, &spec_cu),
7218 spec_cu);
7219 }
7220
1c809c68 7221 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7222 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7223 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7224 return 0;
7225 /* A variable in a lexical block of some kind does not need a
7226 namespace, even though in C++ such variables may be external
7227 and have a mangled name. */
7228 if (die->parent->tag == DW_TAG_lexical_block
7229 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7230 || die->parent->tag == DW_TAG_catch_block
7231 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7232 return 0;
7233 return 1;
94af9270
KS
7234
7235 default:
7236 return 0;
7237 }
7238}
7239
98bfdba5
PA
7240/* Retrieve the last character from a mem_file. */
7241
7242static void
7243do_ui_file_peek_last (void *object, const char *buffer, long length)
7244{
7245 char *last_char_p = (char *) object;
7246
7247 if (length > 0)
7248 *last_char_p = buffer[length - 1];
7249}
7250
94af9270 7251/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7252 compute the physname for the object, which include a method's:
7253 - formal parameters (C++/Java),
7254 - receiver type (Go),
7255 - return type (Java).
7256
7257 The term "physname" is a bit confusing.
7258 For C++, for example, it is the demangled name.
7259 For Go, for example, it's the mangled name.
94af9270 7260
af6b7be1
JB
7261 For Ada, return the DIE's linkage name rather than the fully qualified
7262 name. PHYSNAME is ignored..
7263
94af9270
KS
7264 The result is allocated on the objfile_obstack and canonicalized. */
7265
7266static const char *
7267dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7268 int physname)
7269{
bb5ed363
DE
7270 struct objfile *objfile = cu->objfile;
7271
94af9270
KS
7272 if (name == NULL)
7273 name = dwarf2_name (die, cu);
7274
f55ee35c
JK
7275 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7276 compute it by typename_concat inside GDB. */
7277 if (cu->language == language_ada
7278 || (cu->language == language_fortran && physname))
7279 {
7280 /* For Ada unit, we prefer the linkage name over the name, as
7281 the former contains the exported name, which the user expects
7282 to be able to reference. Ideally, we want the user to be able
7283 to reference this entity using either natural or linkage name,
7284 but we haven't started looking at this enhancement yet. */
7285 struct attribute *attr;
7286
7287 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7288 if (attr == NULL)
7289 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7290 if (attr && DW_STRING (attr))
7291 return DW_STRING (attr);
7292 }
7293
94af9270
KS
7294 /* These are the only languages we know how to qualify names in. */
7295 if (name != NULL
f55ee35c
JK
7296 && (cu->language == language_cplus || cu->language == language_java
7297 || cu->language == language_fortran))
94af9270
KS
7298 {
7299 if (die_needs_namespace (die, cu))
7300 {
7301 long length;
0d5cff50 7302 const char *prefix;
94af9270
KS
7303 struct ui_file *buf;
7304
7305 prefix = determine_prefix (die, cu);
7306 buf = mem_fileopen ();
7307 if (*prefix != '\0')
7308 {
f55ee35c
JK
7309 char *prefixed_name = typename_concat (NULL, prefix, name,
7310 physname, cu);
9a619af0 7311
94af9270
KS
7312 fputs_unfiltered (prefixed_name, buf);
7313 xfree (prefixed_name);
7314 }
7315 else
62d5b8da 7316 fputs_unfiltered (name, buf);
94af9270 7317
98bfdba5
PA
7318 /* Template parameters may be specified in the DIE's DW_AT_name, or
7319 as children with DW_TAG_template_type_param or
7320 DW_TAG_value_type_param. If the latter, add them to the name
7321 here. If the name already has template parameters, then
7322 skip this step; some versions of GCC emit both, and
7323 it is more efficient to use the pre-computed name.
7324
7325 Something to keep in mind about this process: it is very
7326 unlikely, or in some cases downright impossible, to produce
7327 something that will match the mangled name of a function.
7328 If the definition of the function has the same debug info,
7329 we should be able to match up with it anyway. But fallbacks
7330 using the minimal symbol, for instance to find a method
7331 implemented in a stripped copy of libstdc++, will not work.
7332 If we do not have debug info for the definition, we will have to
7333 match them up some other way.
7334
7335 When we do name matching there is a related problem with function
7336 templates; two instantiated function templates are allowed to
7337 differ only by their return types, which we do not add here. */
7338
7339 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7340 {
7341 struct attribute *attr;
7342 struct die_info *child;
7343 int first = 1;
7344
7345 die->building_fullname = 1;
7346
7347 for (child = die->child; child != NULL; child = child->sibling)
7348 {
7349 struct type *type;
12df843f 7350 LONGEST value;
98bfdba5
PA
7351 gdb_byte *bytes;
7352 struct dwarf2_locexpr_baton *baton;
7353 struct value *v;
7354
7355 if (child->tag != DW_TAG_template_type_param
7356 && child->tag != DW_TAG_template_value_param)
7357 continue;
7358
7359 if (first)
7360 {
7361 fputs_unfiltered ("<", buf);
7362 first = 0;
7363 }
7364 else
7365 fputs_unfiltered (", ", buf);
7366
7367 attr = dwarf2_attr (child, DW_AT_type, cu);
7368 if (attr == NULL)
7369 {
7370 complaint (&symfile_complaints,
7371 _("template parameter missing DW_AT_type"));
7372 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7373 continue;
7374 }
7375 type = die_type (child, cu);
7376
7377 if (child->tag == DW_TAG_template_type_param)
7378 {
79d43c61 7379 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7380 continue;
7381 }
7382
7383 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7384 if (attr == NULL)
7385 {
7386 complaint (&symfile_complaints,
3e43a32a
MS
7387 _("template parameter missing "
7388 "DW_AT_const_value"));
98bfdba5
PA
7389 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7390 continue;
7391 }
7392
7393 dwarf2_const_value_attr (attr, type, name,
7394 &cu->comp_unit_obstack, cu,
7395 &value, &bytes, &baton);
7396
7397 if (TYPE_NOSIGN (type))
7398 /* GDB prints characters as NUMBER 'CHAR'. If that's
7399 changed, this can use value_print instead. */
7400 c_printchar (value, type, buf);
7401 else
7402 {
7403 struct value_print_options opts;
7404
7405 if (baton != NULL)
7406 v = dwarf2_evaluate_loc_desc (type, NULL,
7407 baton->data,
7408 baton->size,
7409 baton->per_cu);
7410 else if (bytes != NULL)
7411 {
7412 v = allocate_value (type);
7413 memcpy (value_contents_writeable (v), bytes,
7414 TYPE_LENGTH (type));
7415 }
7416 else
7417 v = value_from_longest (type, value);
7418
3e43a32a
MS
7419 /* Specify decimal so that we do not depend on
7420 the radix. */
98bfdba5
PA
7421 get_formatted_print_options (&opts, 'd');
7422 opts.raw = 1;
7423 value_print (v, buf, &opts);
7424 release_value (v);
7425 value_free (v);
7426 }
7427 }
7428
7429 die->building_fullname = 0;
7430
7431 if (!first)
7432 {
7433 /* Close the argument list, with a space if necessary
7434 (nested templates). */
7435 char last_char = '\0';
7436 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7437 if (last_char == '>')
7438 fputs_unfiltered (" >", buf);
7439 else
7440 fputs_unfiltered (">", buf);
7441 }
7442 }
7443
94af9270
KS
7444 /* For Java and C++ methods, append formal parameter type
7445 information, if PHYSNAME. */
6e70227d 7446
94af9270
KS
7447 if (physname && die->tag == DW_TAG_subprogram
7448 && (cu->language == language_cplus
7449 || cu->language == language_java))
7450 {
7451 struct type *type = read_type_die (die, cu);
7452
79d43c61
TT
7453 c_type_print_args (type, buf, 1, cu->language,
7454 &type_print_raw_options);
94af9270
KS
7455
7456 if (cu->language == language_java)
7457 {
7458 /* For java, we must append the return type to method
0963b4bd 7459 names. */
94af9270
KS
7460 if (die->tag == DW_TAG_subprogram)
7461 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7462 0, 0, &type_print_raw_options);
94af9270
KS
7463 }
7464 else if (cu->language == language_cplus)
7465 {
60430eff
DJ
7466 /* Assume that an artificial first parameter is
7467 "this", but do not crash if it is not. RealView
7468 marks unnamed (and thus unused) parameters as
7469 artificial; there is no way to differentiate
7470 the two cases. */
94af9270
KS
7471 if (TYPE_NFIELDS (type) > 0
7472 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7473 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7474 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7475 0))))
94af9270
KS
7476 fputs_unfiltered (" const", buf);
7477 }
7478 }
7479
bb5ed363 7480 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7481 &length);
7482 ui_file_delete (buf);
7483
7484 if (cu->language == language_cplus)
7485 {
7486 char *cname
7487 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7488 &objfile->objfile_obstack);
9a619af0 7489
94af9270
KS
7490 if (cname != NULL)
7491 name = cname;
7492 }
7493 }
7494 }
7495
7496 return name;
7497}
7498
0114d602
DJ
7499/* Return the fully qualified name of DIE, based on its DW_AT_name.
7500 If scope qualifiers are appropriate they will be added. The result
7501 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7502 not have a name. NAME may either be from a previous call to
7503 dwarf2_name or NULL.
7504
0963b4bd 7505 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7506
7507static const char *
94af9270 7508dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7509{
94af9270
KS
7510 return dwarf2_compute_name (name, die, cu, 0);
7511}
0114d602 7512
94af9270
KS
7513/* Construct a physname for the given DIE in CU. NAME may either be
7514 from a previous call to dwarf2_name or NULL. The result will be
7515 allocated on the objfile_objstack or NULL if the DIE does not have a
7516 name.
0114d602 7517
94af9270 7518 The output string will be canonicalized (if C++/Java). */
0114d602 7519
94af9270
KS
7520static const char *
7521dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7522{
bb5ed363 7523 struct objfile *objfile = cu->objfile;
900e11f9
JK
7524 struct attribute *attr;
7525 const char *retval, *mangled = NULL, *canon = NULL;
7526 struct cleanup *back_to;
7527 int need_copy = 1;
7528
7529 /* In this case dwarf2_compute_name is just a shortcut not building anything
7530 on its own. */
7531 if (!die_needs_namespace (die, cu))
7532 return dwarf2_compute_name (name, die, cu, 1);
7533
7534 back_to = make_cleanup (null_cleanup, NULL);
7535
7536 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7537 if (!attr)
7538 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7539
7540 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7541 has computed. */
7542 if (attr && DW_STRING (attr))
7543 {
7544 char *demangled;
7545
7546 mangled = DW_STRING (attr);
7547
7548 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7549 type. It is easier for GDB users to search for such functions as
7550 `name(params)' than `long name(params)'. In such case the minimal
7551 symbol names do not match the full symbol names but for template
7552 functions there is never a need to look up their definition from their
7553 declaration so the only disadvantage remains the minimal symbol
7554 variant `long name(params)' does not have the proper inferior type.
7555 */
7556
a766d390
DE
7557 if (cu->language == language_go)
7558 {
7559 /* This is a lie, but we already lie to the caller new_symbol_full.
7560 new_symbol_full assumes we return the mangled name.
7561 This just undoes that lie until things are cleaned up. */
7562 demangled = NULL;
7563 }
7564 else
7565 {
7566 demangled = cplus_demangle (mangled,
7567 (DMGL_PARAMS | DMGL_ANSI
7568 | (cu->language == language_java
7569 ? DMGL_JAVA | DMGL_RET_POSTFIX
7570 : DMGL_RET_DROP)));
7571 }
900e11f9
JK
7572 if (demangled)
7573 {
7574 make_cleanup (xfree, demangled);
7575 canon = demangled;
7576 }
7577 else
7578 {
7579 canon = mangled;
7580 need_copy = 0;
7581 }
7582 }
7583
7584 if (canon == NULL || check_physname)
7585 {
7586 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7587
7588 if (canon != NULL && strcmp (physname, canon) != 0)
7589 {
7590 /* It may not mean a bug in GDB. The compiler could also
7591 compute DW_AT_linkage_name incorrectly. But in such case
7592 GDB would need to be bug-to-bug compatible. */
7593
7594 complaint (&symfile_complaints,
7595 _("Computed physname <%s> does not match demangled <%s> "
7596 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7597 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7598
7599 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7600 is available here - over computed PHYSNAME. It is safer
7601 against both buggy GDB and buggy compilers. */
7602
7603 retval = canon;
7604 }
7605 else
7606 {
7607 retval = physname;
7608 need_copy = 0;
7609 }
7610 }
7611 else
7612 retval = canon;
7613
7614 if (need_copy)
7615 retval = obsavestring (retval, strlen (retval),
bb5ed363 7616 &objfile->objfile_obstack);
900e11f9
JK
7617
7618 do_cleanups (back_to);
7619 return retval;
0114d602
DJ
7620}
7621
27aa8d6a
SW
7622/* Read the import statement specified by the given die and record it. */
7623
7624static void
7625read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7626{
bb5ed363 7627 struct objfile *objfile = cu->objfile;
27aa8d6a 7628 struct attribute *import_attr;
32019081 7629 struct die_info *imported_die, *child_die;
de4affc9 7630 struct dwarf2_cu *imported_cu;
27aa8d6a 7631 const char *imported_name;
794684b6 7632 const char *imported_name_prefix;
13387711
SW
7633 const char *canonical_name;
7634 const char *import_alias;
7635 const char *imported_declaration = NULL;
794684b6 7636 const char *import_prefix;
32019081
JK
7637 VEC (const_char_ptr) *excludes = NULL;
7638 struct cleanup *cleanups;
13387711
SW
7639
7640 char *temp;
27aa8d6a
SW
7641
7642 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7643 if (import_attr == NULL)
7644 {
7645 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7646 dwarf_tag_name (die->tag));
7647 return;
7648 }
7649
de4affc9
CC
7650 imported_cu = cu;
7651 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7652 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7653 if (imported_name == NULL)
7654 {
7655 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7656
7657 The import in the following code:
7658 namespace A
7659 {
7660 typedef int B;
7661 }
7662
7663 int main ()
7664 {
7665 using A::B;
7666 B b;
7667 return b;
7668 }
7669
7670 ...
7671 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7672 <52> DW_AT_decl_file : 1
7673 <53> DW_AT_decl_line : 6
7674 <54> DW_AT_import : <0x75>
7675 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7676 <59> DW_AT_name : B
7677 <5b> DW_AT_decl_file : 1
7678 <5c> DW_AT_decl_line : 2
7679 <5d> DW_AT_type : <0x6e>
7680 ...
7681 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7682 <76> DW_AT_byte_size : 4
7683 <77> DW_AT_encoding : 5 (signed)
7684
7685 imports the wrong die ( 0x75 instead of 0x58 ).
7686 This case will be ignored until the gcc bug is fixed. */
7687 return;
7688 }
7689
82856980
SW
7690 /* Figure out the local name after import. */
7691 import_alias = dwarf2_name (die, cu);
27aa8d6a 7692
794684b6
SW
7693 /* Figure out where the statement is being imported to. */
7694 import_prefix = determine_prefix (die, cu);
7695
7696 /* Figure out what the scope of the imported die is and prepend it
7697 to the name of the imported die. */
de4affc9 7698 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7699
f55ee35c
JK
7700 if (imported_die->tag != DW_TAG_namespace
7701 && imported_die->tag != DW_TAG_module)
794684b6 7702 {
13387711
SW
7703 imported_declaration = imported_name;
7704 canonical_name = imported_name_prefix;
794684b6 7705 }
13387711 7706 else if (strlen (imported_name_prefix) > 0)
794684b6 7707 {
13387711
SW
7708 temp = alloca (strlen (imported_name_prefix)
7709 + 2 + strlen (imported_name) + 1);
7710 strcpy (temp, imported_name_prefix);
7711 strcat (temp, "::");
7712 strcat (temp, imported_name);
7713 canonical_name = temp;
794684b6 7714 }
13387711
SW
7715 else
7716 canonical_name = imported_name;
794684b6 7717
32019081
JK
7718 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7719
7720 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7721 for (child_die = die->child; child_die && child_die->tag;
7722 child_die = sibling_die (child_die))
7723 {
7724 /* DWARF-4: A Fortran use statement with a “rename list” may be
7725 represented by an imported module entry with an import attribute
7726 referring to the module and owned entries corresponding to those
7727 entities that are renamed as part of being imported. */
7728
7729 if (child_die->tag != DW_TAG_imported_declaration)
7730 {
7731 complaint (&symfile_complaints,
7732 _("child DW_TAG_imported_declaration expected "
7733 "- DIE at 0x%x [in module %s]"),
b64f50a1 7734 child_die->offset.sect_off, objfile->name);
32019081
JK
7735 continue;
7736 }
7737
7738 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7739 if (import_attr == NULL)
7740 {
7741 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7742 dwarf_tag_name (child_die->tag));
7743 continue;
7744 }
7745
7746 imported_cu = cu;
7747 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7748 &imported_cu);
7749 imported_name = dwarf2_name (imported_die, imported_cu);
7750 if (imported_name == NULL)
7751 {
7752 complaint (&symfile_complaints,
7753 _("child DW_TAG_imported_declaration has unknown "
7754 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7755 child_die->offset.sect_off, objfile->name);
32019081
JK
7756 continue;
7757 }
7758
7759 VEC_safe_push (const_char_ptr, excludes, imported_name);
7760
7761 process_die (child_die, cu);
7762 }
7763
c0cc3a76
SW
7764 cp_add_using_directive (import_prefix,
7765 canonical_name,
7766 import_alias,
13387711 7767 imported_declaration,
32019081 7768 excludes,
bb5ed363 7769 &objfile->objfile_obstack);
32019081
JK
7770
7771 do_cleanups (cleanups);
27aa8d6a
SW
7772}
7773
f4dc4d17 7774/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7775
cb1df416
DJ
7776static void
7777free_cu_line_header (void *arg)
7778{
7779 struct dwarf2_cu *cu = arg;
7780
7781 free_line_header (cu->line_header);
7782 cu->line_header = NULL;
7783}
7784
1b80a9fa
JK
7785/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7786 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7787 this, it was first present in GCC release 4.3.0. */
7788
7789static int
7790producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7791{
7792 if (!cu->checked_producer)
7793 check_producer (cu);
7794
7795 return cu->producer_is_gcc_lt_4_3;
7796}
7797
9291a0cd
TT
7798static void
7799find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7800 char **name, char **comp_dir)
7801{
7802 struct attribute *attr;
7803
7804 *name = NULL;
7805 *comp_dir = NULL;
7806
7807 /* Find the filename. Do not use dwarf2_name here, since the filename
7808 is not a source language identifier. */
7809 attr = dwarf2_attr (die, DW_AT_name, cu);
7810 if (attr)
7811 {
7812 *name = DW_STRING (attr);
7813 }
7814
7815 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7816 if (attr)
7817 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7818 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7819 && IS_ABSOLUTE_PATH (*name))
9291a0cd
TT
7820 {
7821 *comp_dir = ldirname (*name);
7822 if (*comp_dir != NULL)
7823 make_cleanup (xfree, *comp_dir);
7824 }
7825 if (*comp_dir != NULL)
7826 {
7827 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7828 directory, get rid of it. */
7829 char *cp = strchr (*comp_dir, ':');
7830
7831 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7832 *comp_dir = cp + 1;
7833 }
7834
7835 if (*name == NULL)
7836 *name = "<unknown>";
7837}
7838
f4dc4d17
DE
7839/* Handle DW_AT_stmt_list for a compilation unit.
7840 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7841 COMP_DIR is the compilation directory.
7842 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7843
7844static void
7845handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7846 const char *comp_dir)
2ab95328
TT
7847{
7848 struct attribute *attr;
2ab95328 7849
f4dc4d17
DE
7850 gdb_assert (! cu->per_cu->is_debug_types);
7851
2ab95328
TT
7852 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7853 if (attr)
7854 {
7855 unsigned int line_offset = DW_UNSND (attr);
7856 struct line_header *line_header
3019eac3 7857 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7858
7859 if (line_header)
dee91e82
DE
7860 {
7861 cu->line_header = line_header;
7862 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7863 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7864 }
2ab95328
TT
7865 }
7866}
7867
95554aad 7868/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7869
c906108c 7870static void
e7c27a73 7871read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7872{
dee91e82 7873 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7874 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7875 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7876 CORE_ADDR highpc = ((CORE_ADDR) 0);
7877 struct attribute *attr;
e1024ff1 7878 char *name = NULL;
c906108c
SS
7879 char *comp_dir = NULL;
7880 struct die_info *child_die;
7881 bfd *abfd = objfile->obfd;
e142c38c 7882 CORE_ADDR baseaddr;
6e70227d 7883
e142c38c 7884 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7885
fae299cd 7886 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7887
7888 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7889 from finish_block. */
2acceee2 7890 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7891 lowpc = highpc;
7892 lowpc += baseaddr;
7893 highpc += baseaddr;
7894
9291a0cd 7895 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7896
95554aad 7897 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7898
f4b8a18d
KW
7899 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7900 standardised yet. As a workaround for the language detection we fall
7901 back to the DW_AT_producer string. */
7902 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7903 cu->language = language_opencl;
7904
3019eac3
DE
7905 /* Similar hack for Go. */
7906 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7907 set_cu_language (DW_LANG_Go, cu);
7908
f4dc4d17 7909 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7910
7911 /* Decode line number information if present. We do this before
7912 processing child DIEs, so that the line header table is available
7913 for DW_AT_decl_file. */
f4dc4d17 7914 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7915
7916 /* Process all dies in compilation unit. */
7917 if (die->child != NULL)
7918 {
7919 child_die = die->child;
7920 while (child_die && child_die->tag)
7921 {
7922 process_die (child_die, cu);
7923 child_die = sibling_die (child_die);
7924 }
7925 }
7926
7927 /* Decode macro information, if present. Dwarf 2 macro information
7928 refers to information in the line number info statement program
7929 header, so we can only read it if we've read the header
7930 successfully. */
7931 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7932 if (attr && cu->line_header)
7933 {
7934 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7935 complaint (&symfile_complaints,
7936 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7937
09262596 7938 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7939 }
7940 else
7941 {
7942 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7943 if (attr && cu->line_header)
7944 {
7945 unsigned int macro_offset = DW_UNSND (attr);
7946
09262596 7947 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7948 }
7949 }
7950
7951 do_cleanups (back_to);
7952}
7953
f4dc4d17
DE
7954/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7955 Create the set of symtabs used by this TU, or if this TU is sharing
7956 symtabs with another TU and the symtabs have already been created
7957 then restore those symtabs in the line header.
7958 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7959
7960static void
f4dc4d17 7961setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7962{
f4dc4d17
DE
7963 struct objfile *objfile = dwarf2_per_objfile->objfile;
7964 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7965 struct type_unit_group *tu_group;
7966 int first_time;
7967 struct line_header *lh;
3019eac3 7968 struct attribute *attr;
f4dc4d17 7969 unsigned int i, line_offset;
3019eac3 7970
f4dc4d17 7971 gdb_assert (per_cu->is_debug_types);
3019eac3 7972
f4dc4d17 7973 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 7974
f4dc4d17
DE
7975 /* If we're using .gdb_index (includes -readnow) then
7976 per_cu->s.type_unit_group may not have been set up yet. */
7977 if (per_cu->s.type_unit_group == NULL)
094b34ac 7978 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
7979 tu_group = per_cu->s.type_unit_group;
7980
7981 /* If we've already processed this stmt_list there's no real need to
7982 do it again, we could fake it and just recreate the part we need
7983 (file name,index -> symtab mapping). If data shows this optimization
7984 is useful we can do it then. */
7985 first_time = tu_group->primary_symtab == NULL;
7986
7987 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7988 debug info. */
7989 lh = NULL;
7990 if (attr != NULL)
3019eac3 7991 {
f4dc4d17
DE
7992 line_offset = DW_UNSND (attr);
7993 lh = dwarf_decode_line_header (line_offset, cu);
7994 }
7995 if (lh == NULL)
7996 {
7997 if (first_time)
7998 dwarf2_start_symtab (cu, "", NULL, 0);
7999 else
8000 {
8001 gdb_assert (tu_group->symtabs == NULL);
8002 restart_symtab (0);
8003 }
8004 /* Note: The primary symtab will get allocated at the end. */
8005 return;
3019eac3
DE
8006 }
8007
f4dc4d17
DE
8008 cu->line_header = lh;
8009 make_cleanup (free_cu_line_header, cu);
3019eac3 8010
f4dc4d17
DE
8011 if (first_time)
8012 {
8013 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8014
f4dc4d17
DE
8015 tu_group->num_symtabs = lh->num_file_names;
8016 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8017
f4dc4d17
DE
8018 for (i = 0; i < lh->num_file_names; ++i)
8019 {
8020 char *dir = NULL;
8021 struct file_entry *fe = &lh->file_names[i];
3019eac3 8022
f4dc4d17
DE
8023 if (fe->dir_index)
8024 dir = lh->include_dirs[fe->dir_index - 1];
8025 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8026
f4dc4d17
DE
8027 /* Note: We don't have to watch for the main subfile here, type units
8028 don't have DW_AT_name. */
3019eac3 8029
f4dc4d17
DE
8030 if (current_subfile->symtab == NULL)
8031 {
8032 /* NOTE: start_subfile will recognize when it's been passed
8033 a file it has already seen. So we can't assume there's a
8034 simple mapping from lh->file_names to subfiles,
8035 lh->file_names may contain dups. */
8036 current_subfile->symtab = allocate_symtab (current_subfile->name,
8037 objfile);
8038 }
8039
8040 fe->symtab = current_subfile->symtab;
8041 tu_group->symtabs[i] = fe->symtab;
8042 }
8043 }
8044 else
3019eac3 8045 {
f4dc4d17
DE
8046 restart_symtab (0);
8047
8048 for (i = 0; i < lh->num_file_names; ++i)
8049 {
8050 struct file_entry *fe = &lh->file_names[i];
8051
8052 fe->symtab = tu_group->symtabs[i];
8053 }
3019eac3
DE
8054 }
8055
f4dc4d17
DE
8056 /* The main symtab is allocated last. Type units don't have DW_AT_name
8057 so they don't have a "real" (so to speak) symtab anyway.
8058 There is later code that will assign the main symtab to all symbols
8059 that don't have one. We need to handle the case of a symbol with a
8060 missing symtab (DW_AT_decl_file) anyway. */
8061}
3019eac3 8062
f4dc4d17
DE
8063/* Process DW_TAG_type_unit.
8064 For TUs we want to skip the first top level sibling if it's not the
8065 actual type being defined by this TU. In this case the first top
8066 level sibling is there to provide context only. */
3019eac3 8067
f4dc4d17
DE
8068static void
8069read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8070{
8071 struct die_info *child_die;
3019eac3 8072
f4dc4d17
DE
8073 prepare_one_comp_unit (cu, die, language_minimal);
8074
8075 /* Initialize (or reinitialize) the machinery for building symtabs.
8076 We do this before processing child DIEs, so that the line header table
8077 is available for DW_AT_decl_file. */
8078 setup_type_unit_groups (die, cu);
8079
8080 if (die->child != NULL)
8081 {
8082 child_die = die->child;
8083 while (child_die && child_die->tag)
8084 {
8085 process_die (child_die, cu);
8086 child_die = sibling_die (child_die);
8087 }
8088 }
3019eac3
DE
8089}
8090\f
80626a55
DE
8091/* DWO/DWP files.
8092
8093 http://gcc.gnu.org/wiki/DebugFission
8094 http://gcc.gnu.org/wiki/DebugFissionDWP
8095
8096 To simplify handling of both DWO files ("object" files with the DWARF info)
8097 and DWP files (a file with the DWOs packaged up into one file), we treat
8098 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8099
8100static hashval_t
8101hash_dwo_file (const void *item)
8102{
8103 const struct dwo_file *dwo_file = item;
8104
80626a55 8105 return htab_hash_string (dwo_file->name);
3019eac3
DE
8106}
8107
8108static int
8109eq_dwo_file (const void *item_lhs, const void *item_rhs)
8110{
8111 const struct dwo_file *lhs = item_lhs;
8112 const struct dwo_file *rhs = item_rhs;
8113
80626a55 8114 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8115}
8116
8117/* Allocate a hash table for DWO files. */
8118
8119static htab_t
8120allocate_dwo_file_hash_table (void)
8121{
8122 struct objfile *objfile = dwarf2_per_objfile->objfile;
8123
8124 return htab_create_alloc_ex (41,
8125 hash_dwo_file,
8126 eq_dwo_file,
8127 NULL,
8128 &objfile->objfile_obstack,
8129 hashtab_obstack_allocate,
8130 dummy_obstack_deallocate);
8131}
8132
80626a55
DE
8133/* Lookup DWO file DWO_NAME. */
8134
8135static void **
8136lookup_dwo_file_slot (const char *dwo_name)
8137{
8138 struct dwo_file find_entry;
8139 void **slot;
8140
8141 if (dwarf2_per_objfile->dwo_files == NULL)
8142 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8143
8144 memset (&find_entry, 0, sizeof (find_entry));
8145 find_entry.name = dwo_name;
8146 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8147
8148 return slot;
8149}
8150
3019eac3
DE
8151static hashval_t
8152hash_dwo_unit (const void *item)
8153{
8154 const struct dwo_unit *dwo_unit = item;
8155
8156 /* This drops the top 32 bits of the id, but is ok for a hash. */
8157 return dwo_unit->signature;
8158}
8159
8160static int
8161eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8162{
8163 const struct dwo_unit *lhs = item_lhs;
8164 const struct dwo_unit *rhs = item_rhs;
8165
8166 /* The signature is assumed to be unique within the DWO file.
8167 So while object file CU dwo_id's always have the value zero,
8168 that's OK, assuming each object file DWO file has only one CU,
8169 and that's the rule for now. */
8170 return lhs->signature == rhs->signature;
8171}
8172
8173/* Allocate a hash table for DWO CUs,TUs.
8174 There is one of these tables for each of CUs,TUs for each DWO file. */
8175
8176static htab_t
8177allocate_dwo_unit_table (struct objfile *objfile)
8178{
8179 /* Start out with a pretty small number.
8180 Generally DWO files contain only one CU and maybe some TUs. */
8181 return htab_create_alloc_ex (3,
8182 hash_dwo_unit,
8183 eq_dwo_unit,
8184 NULL,
8185 &objfile->objfile_obstack,
8186 hashtab_obstack_allocate,
8187 dummy_obstack_deallocate);
8188}
8189
80626a55 8190/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8191
8192struct create_dwo_info_table_data
8193{
8194 struct dwo_file *dwo_file;
8195 htab_t cu_htab;
8196};
8197
80626a55 8198/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8199
8200static void
80626a55
DE
8201create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8202 gdb_byte *info_ptr,
8203 struct die_info *comp_unit_die,
8204 int has_children,
8205 void *datap)
3019eac3
DE
8206{
8207 struct dwarf2_cu *cu = reader->cu;
8208 struct objfile *objfile = dwarf2_per_objfile->objfile;
8209 sect_offset offset = cu->per_cu->offset;
8210 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8211 struct create_dwo_info_table_data *data = datap;
8212 struct dwo_file *dwo_file = data->dwo_file;
8213 htab_t cu_htab = data->cu_htab;
8214 void **slot;
8215 struct attribute *attr;
8216 struct dwo_unit *dwo_unit;
8217
8218 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8219 if (attr == NULL)
8220 {
8221 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8222 " its dwo_id [in module %s]"),
80626a55 8223 offset.sect_off, dwo_file->name);
3019eac3
DE
8224 return;
8225 }
8226
8227 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8228 dwo_unit->dwo_file = dwo_file;
8229 dwo_unit->signature = DW_UNSND (attr);
8230 dwo_unit->info_or_types_section = section;
8231 dwo_unit->offset = offset;
8232 dwo_unit->length = cu->per_cu->length;
8233
8234 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8235 gdb_assert (slot != NULL);
8236 if (*slot != NULL)
8237 {
8238 const struct dwo_unit *dup_dwo_unit = *slot;
8239
8240 complaint (&symfile_complaints,
8241 _("debug entry at offset 0x%x is duplicate to the entry at"
8242 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8243 offset.sect_off, dup_dwo_unit->offset.sect_off,
8244 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8245 dwo_file->name);
3019eac3
DE
8246 }
8247 else
8248 *slot = dwo_unit;
8249
09406207 8250 if (dwarf2_read_debug)
3019eac3
DE
8251 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8252 offset.sect_off,
8253 phex (dwo_unit->signature,
8254 sizeof (dwo_unit->signature)));
8255}
8256
80626a55
DE
8257/* Create a hash table to map DWO IDs to their CU entry in
8258 .debug_info.dwo in DWO_FILE.
8259 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8260
8261static htab_t
80626a55 8262create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8263{
8264 struct objfile *objfile = dwarf2_per_objfile->objfile;
8265 struct dwarf2_section_info *section = &dwo_file->sections.info;
8266 bfd *abfd;
8267 htab_t cu_htab;
8268 gdb_byte *info_ptr, *end_ptr;
8269 struct create_dwo_info_table_data create_dwo_info_table_data;
8270
8271 dwarf2_read_section (objfile, section);
8272 info_ptr = section->buffer;
8273
8274 if (info_ptr == NULL)
8275 return NULL;
8276
8277 /* We can't set abfd until now because the section may be empty or
8278 not present, in which case section->asection will be NULL. */
8279 abfd = section->asection->owner;
8280
09406207 8281 if (dwarf2_read_debug)
3019eac3
DE
8282 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8283 bfd_get_filename (abfd));
8284
8285 cu_htab = allocate_dwo_unit_table (objfile);
8286
8287 create_dwo_info_table_data.dwo_file = dwo_file;
8288 create_dwo_info_table_data.cu_htab = cu_htab;
8289
8290 end_ptr = info_ptr + section->size;
8291 while (info_ptr < end_ptr)
8292 {
8293 struct dwarf2_per_cu_data per_cu;
8294
8295 memset (&per_cu, 0, sizeof (per_cu));
8296 per_cu.objfile = objfile;
8297 per_cu.is_debug_types = 0;
8298 per_cu.offset.sect_off = info_ptr - section->buffer;
8299 per_cu.info_or_types_section = section;
8300
8301 init_cutu_and_read_dies_no_follow (&per_cu,
8302 &dwo_file->sections.abbrev,
8303 dwo_file,
80626a55 8304 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8305 &create_dwo_info_table_data);
8306
8307 info_ptr += per_cu.length;
8308 }
8309
8310 return cu_htab;
8311}
8312
80626a55
DE
8313/* DWP file .debug_{cu,tu}_index section format:
8314 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8315
8316 Both index sections have the same format, and serve to map a 64-bit
8317 signature to a set of section numbers. Each section begins with a header,
8318 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8319 indexes, and a pool of 32-bit section numbers. The index sections will be
8320 aligned at 8-byte boundaries in the file.
8321
8322 The index section header contains two unsigned 32-bit values (using the
8323 byte order of the application binary):
8324
8325 N, the number of compilation units or type units in the index
8326 M, the number of slots in the hash table
8327
8328 (We assume that N and M will not exceed 2^32 - 1.)
8329
8330 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8331
8332 The hash table begins at offset 8 in the section, and consists of an array
8333 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8334 order of the application binary). Unused slots in the hash table are 0.
8335 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8336
8337 The parallel table begins immediately after the hash table
8338 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8339 array of 32-bit indexes (using the byte order of the application binary),
8340 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8341 table contains a 32-bit index into the pool of section numbers. For unused
8342 hash table slots, the corresponding entry in the parallel table will be 0.
8343
8344 Given a 64-bit compilation unit signature or a type signature S, an entry
8345 in the hash table is located as follows:
8346
8347 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8348 the low-order k bits all set to 1.
8349
8350 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8351
8352 3) If the hash table entry at index H matches the signature, use that
8353 entry. If the hash table entry at index H is unused (all zeroes),
8354 terminate the search: the signature is not present in the table.
8355
8356 4) Let H = (H + H') modulo M. Repeat at Step 3.
8357
8358 Because M > N and H' and M are relatively prime, the search is guaranteed
8359 to stop at an unused slot or find the match.
8360
8361 The pool of section numbers begins immediately following the hash table
8362 (at offset 8 + 12 * M from the beginning of the section). The pool of
8363 section numbers consists of an array of 32-bit words (using the byte order
8364 of the application binary). Each item in the array is indexed starting
8365 from 0. The hash table entry provides the index of the first section
8366 number in the set. Additional section numbers in the set follow, and the
8367 set is terminated by a 0 entry (section number 0 is not used in ELF).
8368
8369 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8370 section must be the first entry in the set, and the .debug_abbrev.dwo must
8371 be the second entry. Other members of the set may follow in any order. */
8372
8373/* Create a hash table to map DWO IDs to their CU/TU entry in
8374 .debug_{info,types}.dwo in DWP_FILE.
8375 Returns NULL if there isn't one.
8376 Note: This function processes DWP files only, not DWO files. */
8377
8378static struct dwp_hash_table *
8379create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8380{
8381 struct objfile *objfile = dwarf2_per_objfile->objfile;
8382 bfd *dbfd = dwp_file->dbfd;
8383 char *index_ptr, *index_end;
8384 struct dwarf2_section_info *index;
8385 uint32_t version, nr_units, nr_slots;
8386 struct dwp_hash_table *htab;
8387
8388 if (is_debug_types)
8389 index = &dwp_file->sections.tu_index;
8390 else
8391 index = &dwp_file->sections.cu_index;
8392
8393 if (dwarf2_section_empty_p (index))
8394 return NULL;
8395 dwarf2_read_section (objfile, index);
8396
8397 index_ptr = index->buffer;
8398 index_end = index_ptr + index->size;
8399
8400 version = read_4_bytes (dbfd, index_ptr);
8401 index_ptr += 8; /* Skip the unused word. */
8402 nr_units = read_4_bytes (dbfd, index_ptr);
8403 index_ptr += 4;
8404 nr_slots = read_4_bytes (dbfd, index_ptr);
8405 index_ptr += 4;
8406
8407 if (version != 1)
8408 {
8409 error (_("Dwarf Error: unsupported DWP file version (%u)"
8410 " [in module %s]"),
8411 version, dwp_file->name);
8412 }
8413 if (nr_slots != (nr_slots & -nr_slots))
8414 {
8415 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8416 " is not power of 2 [in module %s]"),
8417 nr_slots, dwp_file->name);
8418 }
8419
8420 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8421 htab->nr_units = nr_units;
8422 htab->nr_slots = nr_slots;
8423 htab->hash_table = index_ptr;
8424 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8425 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8426
8427 return htab;
8428}
8429
8430/* Update SECTIONS with the data from SECTP.
8431
8432 This function is like the other "locate" section routines that are
8433 passed to bfd_map_over_sections, but in this context the sections to
8434 read comes from the DWP hash table, not the full ELF section table.
8435
8436 The result is non-zero for success, or zero if an error was found. */
8437
8438static int
8439locate_virtual_dwo_sections (asection *sectp,
8440 struct virtual_dwo_sections *sections)
8441{
8442 const struct dwop_section_names *names = &dwop_section_names;
8443
8444 if (section_is_p (sectp->name, &names->abbrev_dwo))
8445 {
8446 /* There can be only one. */
8447 if (sections->abbrev.asection != NULL)
8448 return 0;
8449 sections->abbrev.asection = sectp;
8450 sections->abbrev.size = bfd_get_section_size (sectp);
8451 }
8452 else if (section_is_p (sectp->name, &names->info_dwo)
8453 || section_is_p (sectp->name, &names->types_dwo))
8454 {
8455 /* There can be only one. */
8456 if (sections->info_or_types.asection != NULL)
8457 return 0;
8458 sections->info_or_types.asection = sectp;
8459 sections->info_or_types.size = bfd_get_section_size (sectp);
8460 }
8461 else if (section_is_p (sectp->name, &names->line_dwo))
8462 {
8463 /* There can be only one. */
8464 if (sections->line.asection != NULL)
8465 return 0;
8466 sections->line.asection = sectp;
8467 sections->line.size = bfd_get_section_size (sectp);
8468 }
8469 else if (section_is_p (sectp->name, &names->loc_dwo))
8470 {
8471 /* There can be only one. */
8472 if (sections->loc.asection != NULL)
8473 return 0;
8474 sections->loc.asection = sectp;
8475 sections->loc.size = bfd_get_section_size (sectp);
8476 }
8477 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8478 {
8479 /* There can be only one. */
8480 if (sections->macinfo.asection != NULL)
8481 return 0;
8482 sections->macinfo.asection = sectp;
8483 sections->macinfo.size = bfd_get_section_size (sectp);
8484 }
8485 else if (section_is_p (sectp->name, &names->macro_dwo))
8486 {
8487 /* There can be only one. */
8488 if (sections->macro.asection != NULL)
8489 return 0;
8490 sections->macro.asection = sectp;
8491 sections->macro.size = bfd_get_section_size (sectp);
8492 }
8493 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8494 {
8495 /* There can be only one. */
8496 if (sections->str_offsets.asection != NULL)
8497 return 0;
8498 sections->str_offsets.asection = sectp;
8499 sections->str_offsets.size = bfd_get_section_size (sectp);
8500 }
8501 else
8502 {
8503 /* No other kind of section is valid. */
8504 return 0;
8505 }
8506
8507 return 1;
8508}
8509
8510/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8511 HTAB is the hash table from the DWP file.
8512 SECTION_INDEX is the index of the DWO in HTAB. */
8513
8514static struct dwo_unit *
8515create_dwo_in_dwp (struct dwp_file *dwp_file,
8516 const struct dwp_hash_table *htab,
8517 uint32_t section_index,
8518 ULONGEST signature, int is_debug_types)
8519{
8520 struct objfile *objfile = dwarf2_per_objfile->objfile;
8521 bfd *dbfd = dwp_file->dbfd;
8522 const char *kind = is_debug_types ? "TU" : "CU";
8523 struct dwo_file *dwo_file;
8524 struct dwo_unit *dwo_unit;
8525 struct virtual_dwo_sections sections;
8526 void **dwo_file_slot;
8527 char *virtual_dwo_name;
8528 struct dwarf2_section_info *cutu;
8529 struct cleanup *cleanups;
8530 int i;
8531
8532 if (dwarf2_read_debug)
8533 {
8534 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8535 kind,
8536 section_index, phex (signature, sizeof (signature)),
8537 dwp_file->name);
8538 }
8539
8540 /* Fetch the sections of this DWO.
8541 Put a limit on the number of sections we look for so that bad data
8542 doesn't cause us to loop forever. */
8543
8544#define MAX_NR_DWO_SECTIONS \
8545 (1 /* .debug_info or .debug_types */ \
8546 + 1 /* .debug_abbrev */ \
8547 + 1 /* .debug_line */ \
8548 + 1 /* .debug_loc */ \
8549 + 1 /* .debug_str_offsets */ \
8550 + 1 /* .debug_macro */ \
8551 + 1 /* .debug_macinfo */ \
8552 + 1 /* trailing zero */)
8553
8554 memset (&sections, 0, sizeof (sections));
8555 cleanups = make_cleanup (null_cleanup, 0);
8556
8557 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8558 {
8559 asection *sectp;
8560 uint32_t section_nr =
8561 read_4_bytes (dbfd,
8562 htab->section_pool
8563 + (section_index + i) * sizeof (uint32_t));
8564
8565 if (section_nr == 0)
8566 break;
8567 if (section_nr >= dwp_file->num_sections)
8568 {
8569 error (_("Dwarf Error: bad DWP hash table, section number too large"
8570 " [in module %s]"),
8571 dwp_file->name);
8572 }
8573
8574 sectp = dwp_file->elf_sections[section_nr];
8575 if (! locate_virtual_dwo_sections (sectp, &sections))
8576 {
8577 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8578 " [in module %s]"),
8579 dwp_file->name);
8580 }
8581 }
8582
8583 if (i < 2
8584 || sections.info_or_types.asection == NULL
8585 || sections.abbrev.asection == NULL)
8586 {
8587 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8588 " [in module %s]"),
8589 dwp_file->name);
8590 }
8591 if (i == MAX_NR_DWO_SECTIONS)
8592 {
8593 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8594 " [in module %s]"),
8595 dwp_file->name);
8596 }
8597
8598 /* It's easier for the rest of the code if we fake a struct dwo_file and
8599 have dwo_unit "live" in that. At least for now.
8600
8601 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8602 However, for each CU + set of TUs that came from the same original DWO
8603 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8604 (fewer struct dwo_file objects to allocated). Remember that for really
8605 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8606
2792b94d
PM
8607 virtual_dwo_name =
8608 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8609 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8610 sections.line.asection ? sections.line.asection->id : 0,
8611 sections.loc.asection ? sections.loc.asection->id : 0,
8612 (sections.str_offsets.asection
8613 ? sections.str_offsets.asection->id
8614 : 0));
80626a55
DE
8615 make_cleanup (xfree, virtual_dwo_name);
8616 /* Can we use an existing virtual DWO file? */
8617 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8618 /* Create one if necessary. */
8619 if (*dwo_file_slot == NULL)
8620 {
8621 if (dwarf2_read_debug)
8622 {
8623 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8624 virtual_dwo_name);
8625 }
8626 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8627 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8628 virtual_dwo_name,
8629 strlen (virtual_dwo_name));
8630 dwo_file->sections.abbrev = sections.abbrev;
8631 dwo_file->sections.line = sections.line;
8632 dwo_file->sections.loc = sections.loc;
8633 dwo_file->sections.macinfo = sections.macinfo;
8634 dwo_file->sections.macro = sections.macro;
8635 dwo_file->sections.str_offsets = sections.str_offsets;
8636 /* The "str" section is global to the entire DWP file. */
8637 dwo_file->sections.str = dwp_file->sections.str;
8638 /* The info or types section is assigned later to dwo_unit,
8639 there's no need to record it in dwo_file.
8640 Also, we can't simply record type sections in dwo_file because
8641 we record a pointer into the vector in dwo_unit. As we collect more
8642 types we'll grow the vector and eventually have to reallocate space
8643 for it, invalidating all the pointers into the current copy. */
8644 *dwo_file_slot = dwo_file;
8645 }
8646 else
8647 {
8648 if (dwarf2_read_debug)
8649 {
8650 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8651 virtual_dwo_name);
8652 }
8653 dwo_file = *dwo_file_slot;
8654 }
8655 do_cleanups (cleanups);
8656
8657 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8658 dwo_unit->dwo_file = dwo_file;
8659 dwo_unit->signature = signature;
8660 dwo_unit->info_or_types_section =
8661 obstack_alloc (&objfile->objfile_obstack,
8662 sizeof (struct dwarf2_section_info));
8663 *dwo_unit->info_or_types_section = sections.info_or_types;
8664 /* offset, length, type_offset_in_tu are set later. */
8665
8666 return dwo_unit;
8667}
8668
8669/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8670
8671static struct dwo_unit *
8672lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8673 const struct dwp_hash_table *htab,
8674 ULONGEST signature, int is_debug_types)
8675{
8676 bfd *dbfd = dwp_file->dbfd;
8677 uint32_t mask = htab->nr_slots - 1;
8678 uint32_t hash = signature & mask;
8679 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8680 unsigned int i;
8681 void **slot;
8682 struct dwo_unit find_dwo_cu, *dwo_cu;
8683
8684 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8685 find_dwo_cu.signature = signature;
8686 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8687
8688 if (*slot != NULL)
8689 return *slot;
8690
8691 /* Use a for loop so that we don't loop forever on bad debug info. */
8692 for (i = 0; i < htab->nr_slots; ++i)
8693 {
8694 ULONGEST signature_in_table;
8695
8696 signature_in_table =
8697 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8698 if (signature_in_table == signature)
8699 {
8700 uint32_t section_index =
8701 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8702
8703 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8704 signature, is_debug_types);
8705 return *slot;
8706 }
8707 if (signature_in_table == 0)
8708 return NULL;
8709 hash = (hash + hash2) & mask;
8710 }
8711
8712 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8713 " [in module %s]"),
8714 dwp_file->name);
8715}
8716
8717/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8718 Open the file specified by FILE_NAME and hand it off to BFD for
8719 preliminary analysis. Return a newly initialized bfd *, which
8720 includes a canonicalized copy of FILE_NAME.
80626a55 8721 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8722 In case of trouble, return NULL.
8723 NOTE: This function is derived from symfile_bfd_open. */
8724
8725static bfd *
80626a55 8726try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8727{
8728 bfd *sym_bfd;
80626a55 8729 int desc, flags;
3019eac3 8730 char *absolute_name;
3019eac3 8731
80626a55
DE
8732 flags = OPF_TRY_CWD_FIRST;
8733 if (is_dwp)
8734 flags |= OPF_SEARCH_IN_PATH;
8735 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8736 O_RDONLY | O_BINARY, &absolute_name);
8737 if (desc < 0)
8738 return NULL;
8739
bb397797 8740 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8741 if (!sym_bfd)
8742 {
3019eac3
DE
8743 xfree (absolute_name);
8744 return NULL;
8745 }
a4453b7e 8746 xfree (absolute_name);
3019eac3
DE
8747 bfd_set_cacheable (sym_bfd, 1);
8748
8749 if (!bfd_check_format (sym_bfd, bfd_object))
8750 {
cbb099e8 8751 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8752 return NULL;
8753 }
8754
3019eac3
DE
8755 return sym_bfd;
8756}
8757
80626a55 8758/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8759 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8760 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8761 The result is the bfd handle of the file.
8762 If there is a problem finding or opening the file, return NULL.
8763 Upon success, the canonicalized path of the file is stored in the bfd,
8764 same as symfile_bfd_open. */
8765
8766static bfd *
80626a55 8767open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8768{
8769 bfd *abfd;
3019eac3 8770
80626a55
DE
8771 if (IS_ABSOLUTE_PATH (file_name))
8772 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8773
8774 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8775
8776 if (comp_dir != NULL)
8777 {
80626a55 8778 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8779
8780 /* NOTE: If comp_dir is a relative path, this will also try the
8781 search path, which seems useful. */
80626a55 8782 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8783 xfree (path_to_try);
8784 if (abfd != NULL)
8785 return abfd;
8786 }
8787
8788 /* That didn't work, try debug-file-directory, which, despite its name,
8789 is a list of paths. */
8790
8791 if (*debug_file_directory == '\0')
8792 return NULL;
8793
80626a55 8794 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8795}
8796
80626a55
DE
8797/* This function is mapped across the sections and remembers the offset and
8798 size of each of the DWO debugging sections we are interested in. */
8799
8800static void
8801dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8802{
8803 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8804 const struct dwop_section_names *names = &dwop_section_names;
8805
8806 if (section_is_p (sectp->name, &names->abbrev_dwo))
8807 {
8808 dwo_sections->abbrev.asection = sectp;
8809 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8810 }
8811 else if (section_is_p (sectp->name, &names->info_dwo))
8812 {
8813 dwo_sections->info.asection = sectp;
8814 dwo_sections->info.size = bfd_get_section_size (sectp);
8815 }
8816 else if (section_is_p (sectp->name, &names->line_dwo))
8817 {
8818 dwo_sections->line.asection = sectp;
8819 dwo_sections->line.size = bfd_get_section_size (sectp);
8820 }
8821 else if (section_is_p (sectp->name, &names->loc_dwo))
8822 {
8823 dwo_sections->loc.asection = sectp;
8824 dwo_sections->loc.size = bfd_get_section_size (sectp);
8825 }
8826 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8827 {
8828 dwo_sections->macinfo.asection = sectp;
8829 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8830 }
8831 else if (section_is_p (sectp->name, &names->macro_dwo))
8832 {
8833 dwo_sections->macro.asection = sectp;
8834 dwo_sections->macro.size = bfd_get_section_size (sectp);
8835 }
8836 else if (section_is_p (sectp->name, &names->str_dwo))
8837 {
8838 dwo_sections->str.asection = sectp;
8839 dwo_sections->str.size = bfd_get_section_size (sectp);
8840 }
8841 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8842 {
8843 dwo_sections->str_offsets.asection = sectp;
8844 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8845 }
8846 else if (section_is_p (sectp->name, &names->types_dwo))
8847 {
8848 struct dwarf2_section_info type_section;
8849
8850 memset (&type_section, 0, sizeof (type_section));
8851 type_section.asection = sectp;
8852 type_section.size = bfd_get_section_size (sectp);
8853 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8854 &type_section);
8855 }
8856}
8857
8858/* Initialize the use of the DWO file specified by DWO_NAME.
8859 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8860
8861static struct dwo_file *
80626a55 8862open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8863{
8864 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8865 struct dwo_file *dwo_file;
8866 bfd *dbfd;
3019eac3
DE
8867 struct cleanup *cleanups;
8868
80626a55
DE
8869 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8870 if (dbfd == NULL)
8871 {
8872 if (dwarf2_read_debug)
8873 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8874 return NULL;
8875 }
8876 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8877 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8878 dwo_name, strlen (dwo_name));
8879 dwo_file->dbfd = dbfd;
3019eac3
DE
8880
8881 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8882
80626a55 8883 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8884
80626a55 8885 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8886
8887 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8888 dwo_file->sections.types);
8889
8890 discard_cleanups (cleanups);
8891
80626a55
DE
8892 if (dwarf2_read_debug)
8893 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8894
3019eac3
DE
8895 return dwo_file;
8896}
8897
80626a55
DE
8898/* This function is mapped across the sections and remembers the offset and
8899 size of each of the DWP debugging sections we are interested in. */
3019eac3 8900
80626a55
DE
8901static void
8902dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8903{
80626a55
DE
8904 struct dwp_file *dwp_file = dwp_file_ptr;
8905 const struct dwop_section_names *names = &dwop_section_names;
8906 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8907
80626a55
DE
8908 /* Record the ELF section number for later lookup: this is what the
8909 .debug_cu_index,.debug_tu_index tables use. */
8910 gdb_assert (elf_section_nr < dwp_file->num_sections);
8911 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8912
80626a55
DE
8913 /* Look for specific sections that we need. */
8914 if (section_is_p (sectp->name, &names->str_dwo))
8915 {
8916 dwp_file->sections.str.asection = sectp;
8917 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8918 }
8919 else if (section_is_p (sectp->name, &names->cu_index))
8920 {
8921 dwp_file->sections.cu_index.asection = sectp;
8922 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8923 }
8924 else if (section_is_p (sectp->name, &names->tu_index))
8925 {
8926 dwp_file->sections.tu_index.asection = sectp;
8927 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8928 }
8929}
3019eac3 8930
80626a55 8931/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8932
80626a55
DE
8933static hashval_t
8934hash_dwp_loaded_cutus (const void *item)
8935{
8936 const struct dwo_unit *dwo_unit = item;
3019eac3 8937
80626a55
DE
8938 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8939 return dwo_unit->signature;
3019eac3
DE
8940}
8941
80626a55 8942/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 8943
80626a55
DE
8944static int
8945eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 8946{
80626a55
DE
8947 const struct dwo_unit *dua = a;
8948 const struct dwo_unit *dub = b;
3019eac3 8949
80626a55
DE
8950 return dua->signature == dub->signature;
8951}
3019eac3 8952
80626a55 8953/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 8954
80626a55
DE
8955static htab_t
8956allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8957{
8958 return htab_create_alloc_ex (3,
8959 hash_dwp_loaded_cutus,
8960 eq_dwp_loaded_cutus,
8961 NULL,
8962 &objfile->objfile_obstack,
8963 hashtab_obstack_allocate,
8964 dummy_obstack_deallocate);
8965}
3019eac3 8966
80626a55
DE
8967/* Initialize the use of the DWP file for the current objfile.
8968 By convention the name of the DWP file is ${objfile}.dwp.
8969 The result is NULL if it can't be found. */
a766d390 8970
80626a55
DE
8971static struct dwp_file *
8972open_and_init_dwp_file (const char *comp_dir)
8973{
8974 struct objfile *objfile = dwarf2_per_objfile->objfile;
8975 struct dwp_file *dwp_file;
8976 char *dwp_name;
8977 bfd *dbfd;
8978 struct cleanup *cleanups;
8979
2792b94d 8980 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
8981 cleanups = make_cleanup (xfree, dwp_name);
8982
8983 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8984 if (dbfd == NULL)
8985 {
8986 if (dwarf2_read_debug)
8987 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8988 do_cleanups (cleanups);
8989 return NULL;
3019eac3 8990 }
80626a55
DE
8991 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
8992 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
8993 dwp_name, strlen (dwp_name));
8994 dwp_file->dbfd = dbfd;
8995 do_cleanups (cleanups);
c906108c 8996
80626a55 8997 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 8998
80626a55
DE
8999 /* +1: section 0 is unused */
9000 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9001 dwp_file->elf_sections =
9002 OBSTACK_CALLOC (&objfile->objfile_obstack,
9003 dwp_file->num_sections, asection *);
9004
9005 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9006
9007 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9008
9009 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9010
9011 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9012
9013 discard_cleanups (cleanups);
9014
9015 if (dwarf2_read_debug)
9016 {
9017 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9018 fprintf_unfiltered (gdb_stdlog,
9019 " %u CUs, %u TUs\n",
9020 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9021 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9022 }
9023
9024 return dwp_file;
3019eac3 9025}
c906108c 9026
80626a55
DE
9027/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9028 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9029 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9030 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9031 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9032
9033 This is called, for example, when wanting to read a variable with a
9034 complex location. Therefore we don't want to do file i/o for every call.
9035 Therefore we don't want to look for a DWO file on every call.
9036 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9037 then we check if we've already seen DWO_NAME, and only THEN do we check
9038 for a DWO file.
9039
1c658ad5 9040 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9041 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9042
3019eac3 9043static struct dwo_unit *
80626a55
DE
9044lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9045 const char *dwo_name, const char *comp_dir,
9046 ULONGEST signature, int is_debug_types)
3019eac3
DE
9047{
9048 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9049 const char *kind = is_debug_types ? "TU" : "CU";
9050 void **dwo_file_slot;
3019eac3 9051 struct dwo_file *dwo_file;
80626a55 9052 struct dwp_file *dwp_file;
cb1df416 9053
80626a55 9054 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9055
80626a55
DE
9056 if (! dwarf2_per_objfile->dwp_checked)
9057 {
9058 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9059 dwarf2_per_objfile->dwp_checked = 1;
9060 }
9061 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9062
80626a55 9063 if (dwp_file != NULL)
cf2c3c16 9064 {
80626a55
DE
9065 const struct dwp_hash_table *dwp_htab =
9066 is_debug_types ? dwp_file->tus : dwp_file->cus;
9067
9068 if (dwp_htab != NULL)
9069 {
9070 struct dwo_unit *dwo_cutu =
9071 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9072
9073 if (dwo_cutu != NULL)
9074 {
9075 if (dwarf2_read_debug)
9076 {
9077 fprintf_unfiltered (gdb_stdlog,
9078 "Virtual DWO %s %s found: @%s\n",
9079 kind, hex_string (signature),
9080 host_address_to_string (dwo_cutu));
9081 }
9082 return dwo_cutu;
9083 }
9084 }
9085 }
9086
9087 /* Have we already seen DWO_NAME? */
9088
9089 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9090 if (*dwo_file_slot == NULL)
9091 {
9092 /* Read in the file and build a table of the DWOs it contains. */
9093 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9094 }
9095 /* NOTE: This will be NULL if unable to open the file. */
9096 dwo_file = *dwo_file_slot;
9097
9098 if (dwo_file != NULL)
9099 {
9100 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9101
9102 if (htab != NULL)
9103 {
9104 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9105
80626a55
DE
9106 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9107 find_dwo_cutu.signature = signature;
9108 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9109
80626a55
DE
9110 if (dwo_cutu != NULL)
9111 {
9112 if (dwarf2_read_debug)
9113 {
9114 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9115 kind, dwo_name, hex_string (signature),
9116 host_address_to_string (dwo_cutu));
9117 }
9118 return dwo_cutu;
9119 }
9120 }
2e276125 9121 }
9cdd5dbd 9122
80626a55
DE
9123 /* We didn't find it. This could mean a dwo_id mismatch, or
9124 someone deleted the DWO/DWP file, or the search path isn't set up
9125 correctly to find the file. */
9126
9127 if (dwarf2_read_debug)
9128 {
9129 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9130 kind, dwo_name, hex_string (signature));
9131 }
3019eac3
DE
9132
9133 complaint (&symfile_complaints,
80626a55 9134 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9135 " [in module %s]"),
80626a55 9136 this_unit->offset.sect_off, objfile->name);
3019eac3 9137 return NULL;
5fb290d7
DJ
9138}
9139
80626a55
DE
9140/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9141 See lookup_dwo_cutu_unit for details. */
9142
9143static struct dwo_unit *
9144lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9145 const char *dwo_name, const char *comp_dir,
9146 ULONGEST signature)
9147{
9148 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9149}
9150
9151/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9152 See lookup_dwo_cutu_unit for details. */
9153
9154static struct dwo_unit *
9155lookup_dwo_type_unit (struct signatured_type *this_tu,
9156 const char *dwo_name, const char *comp_dir)
9157{
9158 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9159}
9160
3019eac3
DE
9161/* Free all resources associated with DWO_FILE.
9162 Close the DWO file and munmap the sections.
9163 All memory should be on the objfile obstack. */
348e048f
DE
9164
9165static void
3019eac3 9166free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9167{
3019eac3
DE
9168 int ix;
9169 struct dwarf2_section_info *section;
348e048f 9170
80626a55
DE
9171 gdb_assert (dwo_file->dbfd != objfile->obfd);
9172 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9173
3019eac3
DE
9174 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9175}
348e048f 9176
3019eac3 9177/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9178
3019eac3
DE
9179static void
9180free_dwo_file_cleanup (void *arg)
9181{
9182 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9183 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9184
3019eac3
DE
9185 free_dwo_file (dwo_file, objfile);
9186}
348e048f 9187
3019eac3 9188/* Traversal function for free_dwo_files. */
2ab95328 9189
3019eac3
DE
9190static int
9191free_dwo_file_from_slot (void **slot, void *info)
9192{
9193 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9194 struct objfile *objfile = (struct objfile *) info;
348e048f 9195
3019eac3 9196 free_dwo_file (dwo_file, objfile);
348e048f 9197
3019eac3
DE
9198 return 1;
9199}
348e048f 9200
3019eac3 9201/* Free all resources associated with DWO_FILES. */
348e048f 9202
3019eac3
DE
9203static void
9204free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9205{
9206 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9207}
3019eac3
DE
9208\f
9209/* Read in various DIEs. */
348e048f 9210
d389af10
JK
9211/* qsort helper for inherit_abstract_dies. */
9212
9213static int
9214unsigned_int_compar (const void *ap, const void *bp)
9215{
9216 unsigned int a = *(unsigned int *) ap;
9217 unsigned int b = *(unsigned int *) bp;
9218
9219 return (a > b) - (b > a);
9220}
9221
9222/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9223 Inherit only the children of the DW_AT_abstract_origin DIE not being
9224 already referenced by DW_AT_abstract_origin from the children of the
9225 current DIE. */
d389af10
JK
9226
9227static void
9228inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9229{
9230 struct die_info *child_die;
9231 unsigned die_children_count;
9232 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9233 sect_offset *offsets;
9234 sect_offset *offsets_end, *offsetp;
d389af10
JK
9235 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9236 struct die_info *origin_die;
9237 /* Iterator of the ORIGIN_DIE children. */
9238 struct die_info *origin_child_die;
9239 struct cleanup *cleanups;
9240 struct attribute *attr;
cd02d79d
PA
9241 struct dwarf2_cu *origin_cu;
9242 struct pending **origin_previous_list_in_scope;
d389af10
JK
9243
9244 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9245 if (!attr)
9246 return;
9247
cd02d79d
PA
9248 /* Note that following die references may follow to a die in a
9249 different cu. */
9250
9251 origin_cu = cu;
9252 origin_die = follow_die_ref (die, attr, &origin_cu);
9253
9254 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9255 symbols in. */
9256 origin_previous_list_in_scope = origin_cu->list_in_scope;
9257 origin_cu->list_in_scope = cu->list_in_scope;
9258
edb3359d
DJ
9259 if (die->tag != origin_die->tag
9260 && !(die->tag == DW_TAG_inlined_subroutine
9261 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9262 complaint (&symfile_complaints,
9263 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9264 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9265
9266 child_die = die->child;
9267 die_children_count = 0;
9268 while (child_die && child_die->tag)
9269 {
9270 child_die = sibling_die (child_die);
9271 die_children_count++;
9272 }
9273 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9274 cleanups = make_cleanup (xfree, offsets);
9275
9276 offsets_end = offsets;
9277 child_die = die->child;
9278 while (child_die && child_die->tag)
9279 {
c38f313d
DJ
9280 /* For each CHILD_DIE, find the corresponding child of
9281 ORIGIN_DIE. If there is more than one layer of
9282 DW_AT_abstract_origin, follow them all; there shouldn't be,
9283 but GCC versions at least through 4.4 generate this (GCC PR
9284 40573). */
9285 struct die_info *child_origin_die = child_die;
cd02d79d 9286 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9287
c38f313d
DJ
9288 while (1)
9289 {
cd02d79d
PA
9290 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9291 child_origin_cu);
c38f313d
DJ
9292 if (attr == NULL)
9293 break;
cd02d79d
PA
9294 child_origin_die = follow_die_ref (child_origin_die, attr,
9295 &child_origin_cu);
c38f313d
DJ
9296 }
9297
d389af10
JK
9298 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9299 counterpart may exist. */
c38f313d 9300 if (child_origin_die != child_die)
d389af10 9301 {
edb3359d
DJ
9302 if (child_die->tag != child_origin_die->tag
9303 && !(child_die->tag == DW_TAG_inlined_subroutine
9304 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9305 complaint (&symfile_complaints,
9306 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9307 "different tags"), child_die->offset.sect_off,
9308 child_origin_die->offset.sect_off);
c38f313d
DJ
9309 if (child_origin_die->parent != origin_die)
9310 complaint (&symfile_complaints,
9311 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9312 "different parents"), child_die->offset.sect_off,
9313 child_origin_die->offset.sect_off);
c38f313d
DJ
9314 else
9315 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9316 }
9317 child_die = sibling_die (child_die);
9318 }
9319 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9320 unsigned_int_compar);
9321 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9322 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9323 complaint (&symfile_complaints,
9324 _("Multiple children of DIE 0x%x refer "
9325 "to DIE 0x%x as their abstract origin"),
b64f50a1 9326 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9327
9328 offsetp = offsets;
9329 origin_child_die = origin_die->child;
9330 while (origin_child_die && origin_child_die->tag)
9331 {
9332 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9333 while (offsetp < offsets_end
9334 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9335 offsetp++;
b64f50a1
JK
9336 if (offsetp >= offsets_end
9337 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9338 {
9339 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9340 process_die (origin_child_die, origin_cu);
d389af10
JK
9341 }
9342 origin_child_die = sibling_die (origin_child_die);
9343 }
cd02d79d 9344 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9345
9346 do_cleanups (cleanups);
9347}
9348
c906108c 9349static void
e7c27a73 9350read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9351{
e7c27a73 9352 struct objfile *objfile = cu->objfile;
52f0bd74 9353 struct context_stack *new;
c906108c
SS
9354 CORE_ADDR lowpc;
9355 CORE_ADDR highpc;
9356 struct die_info *child_die;
edb3359d 9357 struct attribute *attr, *call_line, *call_file;
c906108c 9358 char *name;
e142c38c 9359 CORE_ADDR baseaddr;
801e3a5b 9360 struct block *block;
edb3359d 9361 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9362 VEC (symbolp) *template_args = NULL;
9363 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9364
9365 if (inlined_func)
9366 {
9367 /* If we do not have call site information, we can't show the
9368 caller of this inlined function. That's too confusing, so
9369 only use the scope for local variables. */
9370 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9371 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9372 if (call_line == NULL || call_file == NULL)
9373 {
9374 read_lexical_block_scope (die, cu);
9375 return;
9376 }
9377 }
c906108c 9378
e142c38c
DJ
9379 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9380
94af9270 9381 name = dwarf2_name (die, cu);
c906108c 9382
e8d05480
JB
9383 /* Ignore functions with missing or empty names. These are actually
9384 illegal according to the DWARF standard. */
9385 if (name == NULL)
9386 {
9387 complaint (&symfile_complaints,
b64f50a1
JK
9388 _("missing name for subprogram DIE at %d"),
9389 die->offset.sect_off);
e8d05480
JB
9390 return;
9391 }
9392
9393 /* Ignore functions with missing or invalid low and high pc attributes. */
9394 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9395 {
ae4d0c03
PM
9396 attr = dwarf2_attr (die, DW_AT_external, cu);
9397 if (!attr || !DW_UNSND (attr))
9398 complaint (&symfile_complaints,
3e43a32a
MS
9399 _("cannot get low and high bounds "
9400 "for subprogram DIE at %d"),
b64f50a1 9401 die->offset.sect_off);
e8d05480
JB
9402 return;
9403 }
c906108c
SS
9404
9405 lowpc += baseaddr;
9406 highpc += baseaddr;
9407
34eaf542
TT
9408 /* If we have any template arguments, then we must allocate a
9409 different sort of symbol. */
9410 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9411 {
9412 if (child_die->tag == DW_TAG_template_type_param
9413 || child_die->tag == DW_TAG_template_value_param)
9414 {
9415 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9416 struct template_symbol);
9417 templ_func->base.is_cplus_template_function = 1;
9418 break;
9419 }
9420 }
9421
c906108c 9422 new = push_context (0, lowpc);
34eaf542
TT
9423 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9424 (struct symbol *) templ_func);
4c2df51b 9425
4cecd739
DJ
9426 /* If there is a location expression for DW_AT_frame_base, record
9427 it. */
e142c38c 9428 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9429 if (attr)
c034e007
AC
9430 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9431 expression is being recorded directly in the function's symbol
9432 and not in a separate frame-base object. I guess this hack is
9433 to avoid adding some sort of frame-base adjunct/annex to the
9434 function's symbol :-(. The problem with doing this is that it
9435 results in a function symbol with a location expression that
9436 has nothing to do with the location of the function, ouch! The
9437 relationship should be: a function's symbol has-a frame base; a
9438 frame-base has-a location expression. */
e7c27a73 9439 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9440
e142c38c 9441 cu->list_in_scope = &local_symbols;
c906108c 9442
639d11d3 9443 if (die->child != NULL)
c906108c 9444 {
639d11d3 9445 child_die = die->child;
c906108c
SS
9446 while (child_die && child_die->tag)
9447 {
34eaf542
TT
9448 if (child_die->tag == DW_TAG_template_type_param
9449 || child_die->tag == DW_TAG_template_value_param)
9450 {
9451 struct symbol *arg = new_symbol (child_die, NULL, cu);
9452
f1078f66
DJ
9453 if (arg != NULL)
9454 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9455 }
9456 else
9457 process_die (child_die, cu);
c906108c
SS
9458 child_die = sibling_die (child_die);
9459 }
9460 }
9461
d389af10
JK
9462 inherit_abstract_dies (die, cu);
9463
4a811a97
UW
9464 /* If we have a DW_AT_specification, we might need to import using
9465 directives from the context of the specification DIE. See the
9466 comment in determine_prefix. */
9467 if (cu->language == language_cplus
9468 && dwarf2_attr (die, DW_AT_specification, cu))
9469 {
9470 struct dwarf2_cu *spec_cu = cu;
9471 struct die_info *spec_die = die_specification (die, &spec_cu);
9472
9473 while (spec_die)
9474 {
9475 child_die = spec_die->child;
9476 while (child_die && child_die->tag)
9477 {
9478 if (child_die->tag == DW_TAG_imported_module)
9479 process_die (child_die, spec_cu);
9480 child_die = sibling_die (child_die);
9481 }
9482
9483 /* In some cases, GCC generates specification DIEs that
9484 themselves contain DW_AT_specification attributes. */
9485 spec_die = die_specification (spec_die, &spec_cu);
9486 }
9487 }
9488
c906108c
SS
9489 new = pop_context ();
9490 /* Make a block for the local symbols within. */
801e3a5b
JB
9491 block = finish_block (new->name, &local_symbols, new->old_blocks,
9492 lowpc, highpc, objfile);
9493
df8a16a1 9494 /* For C++, set the block's scope. */
f55ee35c 9495 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9496 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9497 determine_prefix (die, cu),
df8a16a1
DJ
9498 processing_has_namespace_info);
9499
801e3a5b
JB
9500 /* If we have address ranges, record them. */
9501 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9502
34eaf542
TT
9503 /* Attach template arguments to function. */
9504 if (! VEC_empty (symbolp, template_args))
9505 {
9506 gdb_assert (templ_func != NULL);
9507
9508 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9509 templ_func->template_arguments
9510 = obstack_alloc (&objfile->objfile_obstack,
9511 (templ_func->n_template_arguments
9512 * sizeof (struct symbol *)));
9513 memcpy (templ_func->template_arguments,
9514 VEC_address (symbolp, template_args),
9515 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9516 VEC_free (symbolp, template_args);
9517 }
9518
208d8187
JB
9519 /* In C++, we can have functions nested inside functions (e.g., when
9520 a function declares a class that has methods). This means that
9521 when we finish processing a function scope, we may need to go
9522 back to building a containing block's symbol lists. */
9523 local_symbols = new->locals;
27aa8d6a 9524 using_directives = new->using_directives;
208d8187 9525
921e78cf
JB
9526 /* If we've finished processing a top-level function, subsequent
9527 symbols go in the file symbol list. */
9528 if (outermost_context_p ())
e142c38c 9529 cu->list_in_scope = &file_symbols;
c906108c
SS
9530}
9531
9532/* Process all the DIES contained within a lexical block scope. Start
9533 a new scope, process the dies, and then close the scope. */
9534
9535static void
e7c27a73 9536read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9537{
e7c27a73 9538 struct objfile *objfile = cu->objfile;
52f0bd74 9539 struct context_stack *new;
c906108c
SS
9540 CORE_ADDR lowpc, highpc;
9541 struct die_info *child_die;
e142c38c
DJ
9542 CORE_ADDR baseaddr;
9543
9544 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9545
9546 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9547 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9548 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9549 be nasty. Might be easier to properly extend generic blocks to
af34e669 9550 describe ranges. */
d85a05f0 9551 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9552 return;
9553 lowpc += baseaddr;
9554 highpc += baseaddr;
9555
9556 push_context (0, lowpc);
639d11d3 9557 if (die->child != NULL)
c906108c 9558 {
639d11d3 9559 child_die = die->child;
c906108c
SS
9560 while (child_die && child_die->tag)
9561 {
e7c27a73 9562 process_die (child_die, cu);
c906108c
SS
9563 child_die = sibling_die (child_die);
9564 }
9565 }
9566 new = pop_context ();
9567
8540c487 9568 if (local_symbols != NULL || using_directives != NULL)
c906108c 9569 {
801e3a5b
JB
9570 struct block *block
9571 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9572 highpc, objfile);
9573
9574 /* Note that recording ranges after traversing children, as we
9575 do here, means that recording a parent's ranges entails
9576 walking across all its children's ranges as they appear in
9577 the address map, which is quadratic behavior.
9578
9579 It would be nicer to record the parent's ranges before
9580 traversing its children, simply overriding whatever you find
9581 there. But since we don't even decide whether to create a
9582 block until after we've traversed its children, that's hard
9583 to do. */
9584 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9585 }
9586 local_symbols = new->locals;
27aa8d6a 9587 using_directives = new->using_directives;
c906108c
SS
9588}
9589
96408a79
SA
9590/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9591
9592static void
9593read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9594{
9595 struct objfile *objfile = cu->objfile;
9596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9597 CORE_ADDR pc, baseaddr;
9598 struct attribute *attr;
9599 struct call_site *call_site, call_site_local;
9600 void **slot;
9601 int nparams;
9602 struct die_info *child_die;
9603
9604 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9605
9606 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9607 if (!attr)
9608 {
9609 complaint (&symfile_complaints,
9610 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9611 "DIE 0x%x [in module %s]"),
b64f50a1 9612 die->offset.sect_off, objfile->name);
96408a79
SA
9613 return;
9614 }
9615 pc = DW_ADDR (attr) + baseaddr;
9616
9617 if (cu->call_site_htab == NULL)
9618 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9619 NULL, &objfile->objfile_obstack,
9620 hashtab_obstack_allocate, NULL);
9621 call_site_local.pc = pc;
9622 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9623 if (*slot != NULL)
9624 {
9625 complaint (&symfile_complaints,
9626 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9627 "DIE 0x%x [in module %s]"),
b64f50a1 9628 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9629 return;
9630 }
9631
9632 /* Count parameters at the caller. */
9633
9634 nparams = 0;
9635 for (child_die = die->child; child_die && child_die->tag;
9636 child_die = sibling_die (child_die))
9637 {
9638 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9639 {
9640 complaint (&symfile_complaints,
9641 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9642 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9643 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9644 continue;
9645 }
9646
9647 nparams++;
9648 }
9649
9650 call_site = obstack_alloc (&objfile->objfile_obstack,
9651 (sizeof (*call_site)
9652 + (sizeof (*call_site->parameter)
9653 * (nparams - 1))));
9654 *slot = call_site;
9655 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9656 call_site->pc = pc;
9657
9658 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9659 {
9660 struct die_info *func_die;
9661
9662 /* Skip also over DW_TAG_inlined_subroutine. */
9663 for (func_die = die->parent;
9664 func_die && func_die->tag != DW_TAG_subprogram
9665 && func_die->tag != DW_TAG_subroutine_type;
9666 func_die = func_die->parent);
9667
9668 /* DW_AT_GNU_all_call_sites is a superset
9669 of DW_AT_GNU_all_tail_call_sites. */
9670 if (func_die
9671 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9672 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9673 {
9674 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9675 not complete. But keep CALL_SITE for look ups via call_site_htab,
9676 both the initial caller containing the real return address PC and
9677 the final callee containing the current PC of a chain of tail
9678 calls do not need to have the tail call list complete. But any
9679 function candidate for a virtual tail call frame searched via
9680 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9681 determined unambiguously. */
9682 }
9683 else
9684 {
9685 struct type *func_type = NULL;
9686
9687 if (func_die)
9688 func_type = get_die_type (func_die, cu);
9689 if (func_type != NULL)
9690 {
9691 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9692
9693 /* Enlist this call site to the function. */
9694 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9695 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9696 }
9697 else
9698 complaint (&symfile_complaints,
9699 _("Cannot find function owning DW_TAG_GNU_call_site "
9700 "DIE 0x%x [in module %s]"),
b64f50a1 9701 die->offset.sect_off, objfile->name);
96408a79
SA
9702 }
9703 }
9704
9705 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9706 if (attr == NULL)
9707 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9708 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9709 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9710 /* Keep NULL DWARF_BLOCK. */;
9711 else if (attr_form_is_block (attr))
9712 {
9713 struct dwarf2_locexpr_baton *dlbaton;
9714
9715 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9716 dlbaton->data = DW_BLOCK (attr)->data;
9717 dlbaton->size = DW_BLOCK (attr)->size;
9718 dlbaton->per_cu = cu->per_cu;
9719
9720 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9721 }
9722 else if (is_ref_attr (attr))
9723 {
96408a79
SA
9724 struct dwarf2_cu *target_cu = cu;
9725 struct die_info *target_die;
9726
9727 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9728 gdb_assert (target_cu->objfile == objfile);
9729 if (die_is_declaration (target_die, target_cu))
9730 {
9731 const char *target_physname;
9732
9733 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9734 if (target_physname == NULL)
9735 complaint (&symfile_complaints,
9736 _("DW_AT_GNU_call_site_target target DIE has invalid "
9737 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9738 die->offset.sect_off, objfile->name);
96408a79
SA
9739 else
9740 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9741 }
9742 else
9743 {
9744 CORE_ADDR lowpc;
9745
9746 /* DW_AT_entry_pc should be preferred. */
9747 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9748 complaint (&symfile_complaints,
9749 _("DW_AT_GNU_call_site_target target DIE has invalid "
9750 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9751 die->offset.sect_off, objfile->name);
96408a79
SA
9752 else
9753 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9754 }
9755 }
9756 else
9757 complaint (&symfile_complaints,
9758 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9759 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9760 die->offset.sect_off, objfile->name);
96408a79
SA
9761
9762 call_site->per_cu = cu->per_cu;
9763
9764 for (child_die = die->child;
9765 child_die && child_die->tag;
9766 child_die = sibling_die (child_die))
9767 {
96408a79 9768 struct call_site_parameter *parameter;
1788b2d3 9769 struct attribute *loc, *origin;
96408a79
SA
9770
9771 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9772 {
9773 /* Already printed the complaint above. */
9774 continue;
9775 }
9776
9777 gdb_assert (call_site->parameter_count < nparams);
9778 parameter = &call_site->parameter[call_site->parameter_count];
9779
1788b2d3
JK
9780 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9781 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9782 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9783
24c5c679 9784 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9785 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9786 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9787 {
9788 sect_offset offset;
9789
9790 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9791 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9792 if (!offset_in_cu_p (&cu->header, offset))
9793 {
9794 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9795 binding can be done only inside one CU. Such referenced DIE
9796 therefore cannot be even moved to DW_TAG_partial_unit. */
9797 complaint (&symfile_complaints,
9798 _("DW_AT_abstract_origin offset is not in CU for "
9799 "DW_TAG_GNU_call_site child DIE 0x%x "
9800 "[in module %s]"),
9801 child_die->offset.sect_off, objfile->name);
9802 continue;
9803 }
1788b2d3
JK
9804 parameter->u.param_offset.cu_off = (offset.sect_off
9805 - cu->header.offset.sect_off);
9806 }
9807 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9808 {
9809 complaint (&symfile_complaints,
9810 _("No DW_FORM_block* DW_AT_location for "
9811 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9812 child_die->offset.sect_off, objfile->name);
96408a79
SA
9813 continue;
9814 }
24c5c679 9815 else
96408a79 9816 {
24c5c679
JK
9817 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9818 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9819 if (parameter->u.dwarf_reg != -1)
9820 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9821 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9822 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9823 &parameter->u.fb_offset))
9824 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9825 else
9826 {
9827 complaint (&symfile_complaints,
9828 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9829 "for DW_FORM_block* DW_AT_location is supported for "
9830 "DW_TAG_GNU_call_site child DIE 0x%x "
9831 "[in module %s]"),
9832 child_die->offset.sect_off, objfile->name);
9833 continue;
9834 }
96408a79
SA
9835 }
9836
9837 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9838 if (!attr_form_is_block (attr))
9839 {
9840 complaint (&symfile_complaints,
9841 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9842 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9843 child_die->offset.sect_off, objfile->name);
96408a79
SA
9844 continue;
9845 }
9846 parameter->value = DW_BLOCK (attr)->data;
9847 parameter->value_size = DW_BLOCK (attr)->size;
9848
9849 /* Parameters are not pre-cleared by memset above. */
9850 parameter->data_value = NULL;
9851 parameter->data_value_size = 0;
9852 call_site->parameter_count++;
9853
9854 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9855 if (attr)
9856 {
9857 if (!attr_form_is_block (attr))
9858 complaint (&symfile_complaints,
9859 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9860 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9861 child_die->offset.sect_off, objfile->name);
96408a79
SA
9862 else
9863 {
9864 parameter->data_value = DW_BLOCK (attr)->data;
9865 parameter->data_value_size = DW_BLOCK (attr)->size;
9866 }
9867 }
9868 }
9869}
9870
43039443 9871/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9872 Return 1 if the attributes are present and valid, otherwise, return 0.
9873 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9874
9875static int
9876dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9877 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9878 struct partial_symtab *ranges_pst)
43039443
JK
9879{
9880 struct objfile *objfile = cu->objfile;
9881 struct comp_unit_head *cu_header = &cu->header;
9882 bfd *obfd = objfile->obfd;
9883 unsigned int addr_size = cu_header->addr_size;
9884 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9885 /* Base address selection entry. */
9886 CORE_ADDR base;
9887 int found_base;
9888 unsigned int dummy;
9889 gdb_byte *buffer;
9890 CORE_ADDR marker;
9891 int low_set;
9892 CORE_ADDR low = 0;
9893 CORE_ADDR high = 0;
ff013f42 9894 CORE_ADDR baseaddr;
43039443 9895
d00adf39
DE
9896 found_base = cu->base_known;
9897 base = cu->base_address;
43039443 9898
be391dca 9899 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9900 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9901 {
9902 complaint (&symfile_complaints,
9903 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9904 offset);
9905 return 0;
9906 }
dce234bc 9907 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9908
9909 /* Read in the largest possible address. */
9910 marker = read_address (obfd, buffer, cu, &dummy);
9911 if ((marker & mask) == mask)
9912 {
9913 /* If we found the largest possible address, then
9914 read the base address. */
9915 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9916 buffer += 2 * addr_size;
9917 offset += 2 * addr_size;
9918 found_base = 1;
9919 }
9920
9921 low_set = 0;
9922
e7030f15 9923 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9924
43039443
JK
9925 while (1)
9926 {
9927 CORE_ADDR range_beginning, range_end;
9928
9929 range_beginning = read_address (obfd, buffer, cu, &dummy);
9930 buffer += addr_size;
9931 range_end = read_address (obfd, buffer, cu, &dummy);
9932 buffer += addr_size;
9933 offset += 2 * addr_size;
9934
9935 /* An end of list marker is a pair of zero addresses. */
9936 if (range_beginning == 0 && range_end == 0)
9937 /* Found the end of list entry. */
9938 break;
9939
9940 /* Each base address selection entry is a pair of 2 values.
9941 The first is the largest possible address, the second is
9942 the base address. Check for a base address here. */
9943 if ((range_beginning & mask) == mask)
9944 {
9945 /* If we found the largest possible address, then
9946 read the base address. */
9947 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9948 found_base = 1;
9949 continue;
9950 }
9951
9952 if (!found_base)
9953 {
9954 /* We have no valid base address for the ranges
9955 data. */
9956 complaint (&symfile_complaints,
9957 _("Invalid .debug_ranges data (no base address)"));
9958 return 0;
9959 }
9960
9277c30c
UW
9961 if (range_beginning > range_end)
9962 {
9963 /* Inverted range entries are invalid. */
9964 complaint (&symfile_complaints,
9965 _("Invalid .debug_ranges data (inverted range)"));
9966 return 0;
9967 }
9968
9969 /* Empty range entries have no effect. */
9970 if (range_beginning == range_end)
9971 continue;
9972
43039443
JK
9973 range_beginning += base;
9974 range_end += base;
9975
01093045
DE
9976 /* A not-uncommon case of bad debug info.
9977 Don't pollute the addrmap with bad data. */
9978 if (range_beginning + baseaddr == 0
9979 && !dwarf2_per_objfile->has_section_at_zero)
9980 {
9981 complaint (&symfile_complaints,
9982 _(".debug_ranges entry has start address of zero"
9983 " [in module %s]"), objfile->name);
9984 continue;
9985 }
9986
9277c30c 9987 if (ranges_pst != NULL)
ff013f42 9988 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
9989 range_beginning + baseaddr,
9990 range_end - 1 + baseaddr,
ff013f42
JK
9991 ranges_pst);
9992
43039443
JK
9993 /* FIXME: This is recording everything as a low-high
9994 segment of consecutive addresses. We should have a
9995 data structure for discontiguous block ranges
9996 instead. */
9997 if (! low_set)
9998 {
9999 low = range_beginning;
10000 high = range_end;
10001 low_set = 1;
10002 }
10003 else
10004 {
10005 if (range_beginning < low)
10006 low = range_beginning;
10007 if (range_end > high)
10008 high = range_end;
10009 }
10010 }
10011
10012 if (! low_set)
10013 /* If the first entry is an end-of-list marker, the range
10014 describes an empty scope, i.e. no instructions. */
10015 return 0;
10016
10017 if (low_return)
10018 *low_return = low;
10019 if (high_return)
10020 *high_return = high;
10021 return 1;
10022}
10023
af34e669
DJ
10024/* Get low and high pc attributes from a die. Return 1 if the attributes
10025 are present and valid, otherwise, return 0. Return -1 if the range is
10026 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10027
c906108c 10028static int
af34e669 10029dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10030 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10031 struct partial_symtab *pst)
c906108c
SS
10032{
10033 struct attribute *attr;
91da1414 10034 struct attribute *attr_high;
af34e669
DJ
10035 CORE_ADDR low = 0;
10036 CORE_ADDR high = 0;
10037 int ret = 0;
c906108c 10038
91da1414
MW
10039 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10040 if (attr_high)
af34e669 10041 {
e142c38c 10042 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10043 if (attr)
91da1414
MW
10044 {
10045 low = DW_ADDR (attr);
3019eac3
DE
10046 if (attr_high->form == DW_FORM_addr
10047 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10048 high = DW_ADDR (attr_high);
10049 else
10050 high = low + DW_UNSND (attr_high);
10051 }
af34e669
DJ
10052 else
10053 /* Found high w/o low attribute. */
10054 return 0;
10055
10056 /* Found consecutive range of addresses. */
10057 ret = 1;
10058 }
c906108c 10059 else
af34e669 10060 {
e142c38c 10061 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10062 if (attr != NULL)
10063 {
ab435259
DE
10064 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10065 We take advantage of the fact that DW_AT_ranges does not appear
10066 in DW_TAG_compile_unit of DWO files. */
10067 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10068 unsigned int ranges_offset = (DW_UNSND (attr)
10069 + (need_ranges_base
10070 ? cu->ranges_base
10071 : 0));
2e3cf129 10072
af34e669 10073 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10074 .debug_ranges section. */
2e3cf129 10075 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10076 return 0;
43039443 10077 /* Found discontinuous range of addresses. */
af34e669
DJ
10078 ret = -1;
10079 }
10080 }
c906108c 10081
9373cf26
JK
10082 /* read_partial_die has also the strict LOW < HIGH requirement. */
10083 if (high <= low)
c906108c
SS
10084 return 0;
10085
10086 /* When using the GNU linker, .gnu.linkonce. sections are used to
10087 eliminate duplicate copies of functions and vtables and such.
10088 The linker will arbitrarily choose one and discard the others.
10089 The AT_*_pc values for such functions refer to local labels in
10090 these sections. If the section from that file was discarded, the
10091 labels are not in the output, so the relocs get a value of 0.
10092 If this is a discarded function, mark the pc bounds as invalid,
10093 so that GDB will ignore it. */
72dca2f5 10094 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10095 return 0;
10096
10097 *lowpc = low;
96408a79
SA
10098 if (highpc)
10099 *highpc = high;
af34e669 10100 return ret;
c906108c
SS
10101}
10102
b084d499
JB
10103/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10104 its low and high PC addresses. Do nothing if these addresses could not
10105 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10106 and HIGHPC to the high address if greater than HIGHPC. */
10107
10108static void
10109dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10110 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10111 struct dwarf2_cu *cu)
10112{
10113 CORE_ADDR low, high;
10114 struct die_info *child = die->child;
10115
d85a05f0 10116 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10117 {
10118 *lowpc = min (*lowpc, low);
10119 *highpc = max (*highpc, high);
10120 }
10121
10122 /* If the language does not allow nested subprograms (either inside
10123 subprograms or lexical blocks), we're done. */
10124 if (cu->language != language_ada)
10125 return;
6e70227d 10126
b084d499
JB
10127 /* Check all the children of the given DIE. If it contains nested
10128 subprograms, then check their pc bounds. Likewise, we need to
10129 check lexical blocks as well, as they may also contain subprogram
10130 definitions. */
10131 while (child && child->tag)
10132 {
10133 if (child->tag == DW_TAG_subprogram
10134 || child->tag == DW_TAG_lexical_block)
10135 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10136 child = sibling_die (child);
10137 }
10138}
10139
fae299cd
DC
10140/* Get the low and high pc's represented by the scope DIE, and store
10141 them in *LOWPC and *HIGHPC. If the correct values can't be
10142 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10143
10144static void
10145get_scope_pc_bounds (struct die_info *die,
10146 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10147 struct dwarf2_cu *cu)
10148{
10149 CORE_ADDR best_low = (CORE_ADDR) -1;
10150 CORE_ADDR best_high = (CORE_ADDR) 0;
10151 CORE_ADDR current_low, current_high;
10152
d85a05f0 10153 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10154 {
10155 best_low = current_low;
10156 best_high = current_high;
10157 }
10158 else
10159 {
10160 struct die_info *child = die->child;
10161
10162 while (child && child->tag)
10163 {
10164 switch (child->tag) {
10165 case DW_TAG_subprogram:
b084d499 10166 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10167 break;
10168 case DW_TAG_namespace:
f55ee35c 10169 case DW_TAG_module:
fae299cd
DC
10170 /* FIXME: carlton/2004-01-16: Should we do this for
10171 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10172 that current GCC's always emit the DIEs corresponding
10173 to definitions of methods of classes as children of a
10174 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10175 the DIEs giving the declarations, which could be
10176 anywhere). But I don't see any reason why the
10177 standards says that they have to be there. */
10178 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10179
10180 if (current_low != ((CORE_ADDR) -1))
10181 {
10182 best_low = min (best_low, current_low);
10183 best_high = max (best_high, current_high);
10184 }
10185 break;
10186 default:
0963b4bd 10187 /* Ignore. */
fae299cd
DC
10188 break;
10189 }
10190
10191 child = sibling_die (child);
10192 }
10193 }
10194
10195 *lowpc = best_low;
10196 *highpc = best_high;
10197}
10198
801e3a5b
JB
10199/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10200 in DIE. */
380bca97 10201
801e3a5b
JB
10202static void
10203dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10204 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10205{
bb5ed363 10206 struct objfile *objfile = cu->objfile;
801e3a5b 10207 struct attribute *attr;
91da1414 10208 struct attribute *attr_high;
801e3a5b 10209
91da1414
MW
10210 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10211 if (attr_high)
801e3a5b 10212 {
801e3a5b
JB
10213 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10214 if (attr)
10215 {
10216 CORE_ADDR low = DW_ADDR (attr);
91da1414 10217 CORE_ADDR high;
3019eac3
DE
10218 if (attr_high->form == DW_FORM_addr
10219 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10220 high = DW_ADDR (attr_high);
10221 else
10222 high = low + DW_UNSND (attr_high);
9a619af0 10223
801e3a5b
JB
10224 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10225 }
10226 }
10227
10228 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10229 if (attr)
10230 {
bb5ed363 10231 bfd *obfd = objfile->obfd;
ab435259
DE
10232 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10233 We take advantage of the fact that DW_AT_ranges does not appear
10234 in DW_TAG_compile_unit of DWO files. */
10235 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10236
10237 /* The value of the DW_AT_ranges attribute is the offset of the
10238 address range list in the .debug_ranges section. */
ab435259
DE
10239 unsigned long offset = (DW_UNSND (attr)
10240 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10241 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10242
10243 /* For some target architectures, but not others, the
10244 read_address function sign-extends the addresses it returns.
10245 To recognize base address selection entries, we need a
10246 mask. */
10247 unsigned int addr_size = cu->header.addr_size;
10248 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10249
10250 /* The base address, to which the next pair is relative. Note
10251 that this 'base' is a DWARF concept: most entries in a range
10252 list are relative, to reduce the number of relocs against the
10253 debugging information. This is separate from this function's
10254 'baseaddr' argument, which GDB uses to relocate debugging
10255 information from a shared library based on the address at
10256 which the library was loaded. */
d00adf39
DE
10257 CORE_ADDR base = cu->base_address;
10258 int base_known = cu->base_known;
801e3a5b 10259
be391dca 10260 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10261 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10262 {
10263 complaint (&symfile_complaints,
10264 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10265 offset);
10266 return;
10267 }
10268
10269 for (;;)
10270 {
10271 unsigned int bytes_read;
10272 CORE_ADDR start, end;
10273
10274 start = read_address (obfd, buffer, cu, &bytes_read);
10275 buffer += bytes_read;
10276 end = read_address (obfd, buffer, cu, &bytes_read);
10277 buffer += bytes_read;
10278
10279 /* Did we find the end of the range list? */
10280 if (start == 0 && end == 0)
10281 break;
10282
10283 /* Did we find a base address selection entry? */
10284 else if ((start & base_select_mask) == base_select_mask)
10285 {
10286 base = end;
10287 base_known = 1;
10288 }
10289
10290 /* We found an ordinary address range. */
10291 else
10292 {
10293 if (!base_known)
10294 {
10295 complaint (&symfile_complaints,
3e43a32a
MS
10296 _("Invalid .debug_ranges data "
10297 "(no base address)"));
801e3a5b
JB
10298 return;
10299 }
10300
9277c30c
UW
10301 if (start > end)
10302 {
10303 /* Inverted range entries are invalid. */
10304 complaint (&symfile_complaints,
10305 _("Invalid .debug_ranges data "
10306 "(inverted range)"));
10307 return;
10308 }
10309
10310 /* Empty range entries have no effect. */
10311 if (start == end)
10312 continue;
10313
01093045
DE
10314 start += base + baseaddr;
10315 end += base + baseaddr;
10316
10317 /* A not-uncommon case of bad debug info.
10318 Don't pollute the addrmap with bad data. */
10319 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10320 {
10321 complaint (&symfile_complaints,
10322 _(".debug_ranges entry has start address of zero"
10323 " [in module %s]"), objfile->name);
10324 continue;
10325 }
10326
10327 record_block_range (block, start, end - 1);
801e3a5b
JB
10328 }
10329 }
10330 }
10331}
10332
685b1105
JK
10333/* Check whether the producer field indicates either of GCC < 4.6, or the
10334 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10335
685b1105
JK
10336static void
10337check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10338{
10339 const char *cs;
10340 int major, minor, release;
10341
10342 if (cu->producer == NULL)
10343 {
10344 /* For unknown compilers expect their behavior is DWARF version
10345 compliant.
10346
10347 GCC started to support .debug_types sections by -gdwarf-4 since
10348 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10349 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10350 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10351 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10352 }
685b1105 10353 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10354 {
685b1105
JK
10355 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10356
ba919b58
TT
10357 cs = &cu->producer[strlen ("GNU ")];
10358 while (*cs && !isdigit (*cs))
10359 cs++;
10360 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10361 {
10362 /* Not recognized as GCC. */
10363 }
10364 else
1b80a9fa
JK
10365 {
10366 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10367 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10368 }
685b1105
JK
10369 }
10370 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10371 cu->producer_is_icc = 1;
10372 else
10373 {
10374 /* For other non-GCC compilers, expect their behavior is DWARF version
10375 compliant. */
60d5a603
JK
10376 }
10377
ba919b58 10378 cu->checked_producer = 1;
685b1105 10379}
ba919b58 10380
685b1105
JK
10381/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10382 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10383 during 4.6.0 experimental. */
10384
10385static int
10386producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10387{
10388 if (!cu->checked_producer)
10389 check_producer (cu);
10390
10391 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10392}
10393
10394/* Return the default accessibility type if it is not overriden by
10395 DW_AT_accessibility. */
10396
10397static enum dwarf_access_attribute
10398dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10399{
10400 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10401 {
10402 /* The default DWARF 2 accessibility for members is public, the default
10403 accessibility for inheritance is private. */
10404
10405 if (die->tag != DW_TAG_inheritance)
10406 return DW_ACCESS_public;
10407 else
10408 return DW_ACCESS_private;
10409 }
10410 else
10411 {
10412 /* DWARF 3+ defines the default accessibility a different way. The same
10413 rules apply now for DW_TAG_inheritance as for the members and it only
10414 depends on the container kind. */
10415
10416 if (die->parent->tag == DW_TAG_class_type)
10417 return DW_ACCESS_private;
10418 else
10419 return DW_ACCESS_public;
10420 }
10421}
10422
74ac6d43
TT
10423/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10424 offset. If the attribute was not found return 0, otherwise return
10425 1. If it was found but could not properly be handled, set *OFFSET
10426 to 0. */
10427
10428static int
10429handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10430 LONGEST *offset)
10431{
10432 struct attribute *attr;
10433
10434 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10435 if (attr != NULL)
10436 {
10437 *offset = 0;
10438
10439 /* Note that we do not check for a section offset first here.
10440 This is because DW_AT_data_member_location is new in DWARF 4,
10441 so if we see it, we can assume that a constant form is really
10442 a constant and not a section offset. */
10443 if (attr_form_is_constant (attr))
10444 *offset = dwarf2_get_attr_constant_value (attr, 0);
10445 else if (attr_form_is_section_offset (attr))
10446 dwarf2_complex_location_expr_complaint ();
10447 else if (attr_form_is_block (attr))
10448 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10449 else
10450 dwarf2_complex_location_expr_complaint ();
10451
10452 return 1;
10453 }
10454
10455 return 0;
10456}
10457
c906108c
SS
10458/* Add an aggregate field to the field list. */
10459
10460static void
107d2387 10461dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10462 struct dwarf2_cu *cu)
6e70227d 10463{
e7c27a73 10464 struct objfile *objfile = cu->objfile;
5e2b427d 10465 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10466 struct nextfield *new_field;
10467 struct attribute *attr;
10468 struct field *fp;
10469 char *fieldname = "";
10470
10471 /* Allocate a new field list entry and link it in. */
10472 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10473 make_cleanup (xfree, new_field);
c906108c 10474 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10475
10476 if (die->tag == DW_TAG_inheritance)
10477 {
10478 new_field->next = fip->baseclasses;
10479 fip->baseclasses = new_field;
10480 }
10481 else
10482 {
10483 new_field->next = fip->fields;
10484 fip->fields = new_field;
10485 }
c906108c
SS
10486 fip->nfields++;
10487
e142c38c 10488 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10489 if (attr)
10490 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10491 else
10492 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10493 if (new_field->accessibility != DW_ACCESS_public)
10494 fip->non_public_fields = 1;
60d5a603 10495
e142c38c 10496 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10497 if (attr)
10498 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10499 else
10500 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10501
10502 fp = &new_field->field;
a9a9bd0f 10503
e142c38c 10504 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10505 {
74ac6d43
TT
10506 LONGEST offset;
10507
a9a9bd0f 10508 /* Data member other than a C++ static data member. */
6e70227d 10509
c906108c 10510 /* Get type of field. */
e7c27a73 10511 fp->type = die_type (die, cu);
c906108c 10512
d6a843b5 10513 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10514
c906108c 10515 /* Get bit size of field (zero if none). */
e142c38c 10516 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10517 if (attr)
10518 {
10519 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10520 }
10521 else
10522 {
10523 FIELD_BITSIZE (*fp) = 0;
10524 }
10525
10526 /* Get bit offset of field. */
74ac6d43
TT
10527 if (handle_data_member_location (die, cu, &offset))
10528 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10529 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10530 if (attr)
10531 {
5e2b427d 10532 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10533 {
10534 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10535 additional bit offset from the MSB of the containing
10536 anonymous object to the MSB of the field. We don't
10537 have to do anything special since we don't need to
10538 know the size of the anonymous object. */
f41f5e61 10539 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10540 }
10541 else
10542 {
10543 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10544 MSB of the anonymous object, subtract off the number of
10545 bits from the MSB of the field to the MSB of the
10546 object, and then subtract off the number of bits of
10547 the field itself. The result is the bit offset of
10548 the LSB of the field. */
c906108c
SS
10549 int anonymous_size;
10550 int bit_offset = DW_UNSND (attr);
10551
e142c38c 10552 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10553 if (attr)
10554 {
10555 /* The size of the anonymous object containing
10556 the bit field is explicit, so use the
10557 indicated size (in bytes). */
10558 anonymous_size = DW_UNSND (attr);
10559 }
10560 else
10561 {
10562 /* The size of the anonymous object containing
10563 the bit field must be inferred from the type
10564 attribute of the data member containing the
10565 bit field. */
10566 anonymous_size = TYPE_LENGTH (fp->type);
10567 }
f41f5e61
PA
10568 SET_FIELD_BITPOS (*fp,
10569 (FIELD_BITPOS (*fp)
10570 + anonymous_size * bits_per_byte
10571 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10572 }
10573 }
10574
10575 /* Get name of field. */
39cbfefa
DJ
10576 fieldname = dwarf2_name (die, cu);
10577 if (fieldname == NULL)
10578 fieldname = "";
d8151005
DJ
10579
10580 /* The name is already allocated along with this objfile, so we don't
10581 need to duplicate it for the type. */
10582 fp->name = fieldname;
c906108c
SS
10583
10584 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10585 pointer or virtual base class pointer) to private. */
e142c38c 10586 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10587 {
d48cc9dd 10588 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10589 new_field->accessibility = DW_ACCESS_private;
10590 fip->non_public_fields = 1;
10591 }
10592 }
a9a9bd0f 10593 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10594 {
a9a9bd0f
DC
10595 /* C++ static member. */
10596
10597 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10598 is a declaration, but all versions of G++ as of this writing
10599 (so through at least 3.2.1) incorrectly generate
10600 DW_TAG_variable tags. */
6e70227d 10601
ff355380 10602 const char *physname;
c906108c 10603
a9a9bd0f 10604 /* Get name of field. */
39cbfefa
DJ
10605 fieldname = dwarf2_name (die, cu);
10606 if (fieldname == NULL)
c906108c
SS
10607 return;
10608
254e6b9e 10609 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10610 if (attr
10611 /* Only create a symbol if this is an external value.
10612 new_symbol checks this and puts the value in the global symbol
10613 table, which we want. If it is not external, new_symbol
10614 will try to put the value in cu->list_in_scope which is wrong. */
10615 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10616 {
10617 /* A static const member, not much different than an enum as far as
10618 we're concerned, except that we can support more types. */
10619 new_symbol (die, NULL, cu);
10620 }
10621
2df3850c 10622 /* Get physical name. */
ff355380 10623 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10624
d8151005
DJ
10625 /* The name is already allocated along with this objfile, so we don't
10626 need to duplicate it for the type. */
10627 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10628 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10629 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10630 }
10631 else if (die->tag == DW_TAG_inheritance)
10632 {
74ac6d43 10633 LONGEST offset;
d4b96c9a 10634
74ac6d43
TT
10635 /* C++ base class field. */
10636 if (handle_data_member_location (die, cu, &offset))
10637 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10638 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10639 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10640 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10641 fip->nbaseclasses++;
10642 }
10643}
10644
98751a41
JK
10645/* Add a typedef defined in the scope of the FIP's class. */
10646
10647static void
10648dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10649 struct dwarf2_cu *cu)
6e70227d 10650{
98751a41 10651 struct objfile *objfile = cu->objfile;
98751a41
JK
10652 struct typedef_field_list *new_field;
10653 struct attribute *attr;
10654 struct typedef_field *fp;
10655 char *fieldname = "";
10656
10657 /* Allocate a new field list entry and link it in. */
10658 new_field = xzalloc (sizeof (*new_field));
10659 make_cleanup (xfree, new_field);
10660
10661 gdb_assert (die->tag == DW_TAG_typedef);
10662
10663 fp = &new_field->field;
10664
10665 /* Get name of field. */
10666 fp->name = dwarf2_name (die, cu);
10667 if (fp->name == NULL)
10668 return;
10669
10670 fp->type = read_type_die (die, cu);
10671
10672 new_field->next = fip->typedef_field_list;
10673 fip->typedef_field_list = new_field;
10674 fip->typedef_field_list_count++;
10675}
10676
c906108c
SS
10677/* Create the vector of fields, and attach it to the type. */
10678
10679static void
fba45db2 10680dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10681 struct dwarf2_cu *cu)
c906108c
SS
10682{
10683 int nfields = fip->nfields;
10684
10685 /* Record the field count, allocate space for the array of fields,
10686 and create blank accessibility bitfields if necessary. */
10687 TYPE_NFIELDS (type) = nfields;
10688 TYPE_FIELDS (type) = (struct field *)
10689 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10690 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10691
b4ba55a1 10692 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10693 {
10694 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10695
10696 TYPE_FIELD_PRIVATE_BITS (type) =
10697 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10698 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10699
10700 TYPE_FIELD_PROTECTED_BITS (type) =
10701 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10702 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10703
774b6a14
TT
10704 TYPE_FIELD_IGNORE_BITS (type) =
10705 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10706 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10707 }
10708
10709 /* If the type has baseclasses, allocate and clear a bit vector for
10710 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10711 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10712 {
10713 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10714 unsigned char *pointer;
c906108c
SS
10715
10716 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10717 pointer = TYPE_ALLOC (type, num_bytes);
10718 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10719 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10720 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10721 }
10722
3e43a32a
MS
10723 /* Copy the saved-up fields into the field vector. Start from the head of
10724 the list, adding to the tail of the field array, so that they end up in
10725 the same order in the array in which they were added to the list. */
c906108c
SS
10726 while (nfields-- > 0)
10727 {
7d0ccb61
DJ
10728 struct nextfield *fieldp;
10729
10730 if (fip->fields)
10731 {
10732 fieldp = fip->fields;
10733 fip->fields = fieldp->next;
10734 }
10735 else
10736 {
10737 fieldp = fip->baseclasses;
10738 fip->baseclasses = fieldp->next;
10739 }
10740
10741 TYPE_FIELD (type, nfields) = fieldp->field;
10742 switch (fieldp->accessibility)
c906108c 10743 {
c5aa993b 10744 case DW_ACCESS_private:
b4ba55a1
JB
10745 if (cu->language != language_ada)
10746 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10747 break;
c906108c 10748
c5aa993b 10749 case DW_ACCESS_protected:
b4ba55a1
JB
10750 if (cu->language != language_ada)
10751 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10752 break;
c906108c 10753
c5aa993b
JM
10754 case DW_ACCESS_public:
10755 break;
c906108c 10756
c5aa993b
JM
10757 default:
10758 /* Unknown accessibility. Complain and treat it as public. */
10759 {
e2e0b3e5 10760 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10761 fieldp->accessibility);
c5aa993b
JM
10762 }
10763 break;
c906108c
SS
10764 }
10765 if (nfields < fip->nbaseclasses)
10766 {
7d0ccb61 10767 switch (fieldp->virtuality)
c906108c 10768 {
c5aa993b
JM
10769 case DW_VIRTUALITY_virtual:
10770 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10771 if (cu->language == language_ada)
a73c6dcd 10772 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10773 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10774 break;
c906108c
SS
10775 }
10776 }
c906108c
SS
10777 }
10778}
10779
c906108c
SS
10780/* Add a member function to the proper fieldlist. */
10781
10782static void
107d2387 10783dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10784 struct type *type, struct dwarf2_cu *cu)
c906108c 10785{
e7c27a73 10786 struct objfile *objfile = cu->objfile;
c906108c
SS
10787 struct attribute *attr;
10788 struct fnfieldlist *flp;
10789 int i;
10790 struct fn_field *fnp;
10791 char *fieldname;
c906108c 10792 struct nextfnfield *new_fnfield;
f792889a 10793 struct type *this_type;
60d5a603 10794 enum dwarf_access_attribute accessibility;
c906108c 10795
b4ba55a1 10796 if (cu->language == language_ada)
a73c6dcd 10797 error (_("unexpected member function in Ada type"));
b4ba55a1 10798
2df3850c 10799 /* Get name of member function. */
39cbfefa
DJ
10800 fieldname = dwarf2_name (die, cu);
10801 if (fieldname == NULL)
2df3850c 10802 return;
c906108c 10803
c906108c
SS
10804 /* Look up member function name in fieldlist. */
10805 for (i = 0; i < fip->nfnfields; i++)
10806 {
27bfe10e 10807 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10808 break;
10809 }
10810
10811 /* Create new list element if necessary. */
10812 if (i < fip->nfnfields)
10813 flp = &fip->fnfieldlists[i];
10814 else
10815 {
10816 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10817 {
10818 fip->fnfieldlists = (struct fnfieldlist *)
10819 xrealloc (fip->fnfieldlists,
10820 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10821 * sizeof (struct fnfieldlist));
c906108c 10822 if (fip->nfnfields == 0)
c13c43fd 10823 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10824 }
10825 flp = &fip->fnfieldlists[fip->nfnfields];
10826 flp->name = fieldname;
10827 flp->length = 0;
10828 flp->head = NULL;
3da10d80 10829 i = fip->nfnfields++;
c906108c
SS
10830 }
10831
10832 /* Create a new member function field and chain it to the field list
0963b4bd 10833 entry. */
c906108c 10834 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10835 make_cleanup (xfree, new_fnfield);
c906108c
SS
10836 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10837 new_fnfield->next = flp->head;
10838 flp->head = new_fnfield;
10839 flp->length++;
10840
10841 /* Fill in the member function field info. */
10842 fnp = &new_fnfield->fnfield;
3da10d80
KS
10843
10844 /* Delay processing of the physname until later. */
10845 if (cu->language == language_cplus || cu->language == language_java)
10846 {
10847 add_to_method_list (type, i, flp->length - 1, fieldname,
10848 die, cu);
10849 }
10850 else
10851 {
1d06ead6 10852 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10853 fnp->physname = physname ? physname : "";
10854 }
10855
c906108c 10856 fnp->type = alloc_type (objfile);
f792889a
DJ
10857 this_type = read_type_die (die, cu);
10858 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10859 {
f792889a 10860 int nparams = TYPE_NFIELDS (this_type);
c906108c 10861
f792889a 10862 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10863 of the method itself (TYPE_CODE_METHOD). */
10864 smash_to_method_type (fnp->type, type,
f792889a
DJ
10865 TYPE_TARGET_TYPE (this_type),
10866 TYPE_FIELDS (this_type),
10867 TYPE_NFIELDS (this_type),
10868 TYPE_VARARGS (this_type));
c906108c
SS
10869
10870 /* Handle static member functions.
c5aa993b 10871 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10872 member functions. G++ helps GDB by marking the first
10873 parameter for non-static member functions (which is the this
10874 pointer) as artificial. We obtain this information from
10875 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10876 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10877 fnp->voffset = VOFFSET_STATIC;
10878 }
10879 else
e2e0b3e5 10880 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10881 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10882
10883 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10884 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10885 fnp->fcontext = die_containing_type (die, cu);
c906108c 10886
3e43a32a
MS
10887 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10888 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10889
10890 /* Get accessibility. */
e142c38c 10891 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10892 if (attr)
60d5a603
JK
10893 accessibility = DW_UNSND (attr);
10894 else
10895 accessibility = dwarf2_default_access_attribute (die, cu);
10896 switch (accessibility)
c906108c 10897 {
60d5a603
JK
10898 case DW_ACCESS_private:
10899 fnp->is_private = 1;
10900 break;
10901 case DW_ACCESS_protected:
10902 fnp->is_protected = 1;
10903 break;
c906108c
SS
10904 }
10905
b02dede2 10906 /* Check for artificial methods. */
e142c38c 10907 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10908 if (attr && DW_UNSND (attr) != 0)
10909 fnp->is_artificial = 1;
10910
0d564a31 10911 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10912 function. For older versions of GCC, this is an offset in the
10913 appropriate virtual table, as specified by DW_AT_containing_type.
10914 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10915 to the object address. */
10916
e142c38c 10917 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10918 if (attr)
8e19ed76 10919 {
aec5aa8b 10920 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10921 {
aec5aa8b
TT
10922 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10923 {
10924 /* Old-style GCC. */
10925 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10926 }
10927 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10928 || (DW_BLOCK (attr)->size > 1
10929 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10930 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10931 {
10932 struct dwarf_block blk;
10933 int offset;
10934
10935 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10936 ? 1 : 2);
10937 blk.size = DW_BLOCK (attr)->size - offset;
10938 blk.data = DW_BLOCK (attr)->data + offset;
10939 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10940 if ((fnp->voffset % cu->header.addr_size) != 0)
10941 dwarf2_complex_location_expr_complaint ();
10942 else
10943 fnp->voffset /= cu->header.addr_size;
10944 fnp->voffset += 2;
10945 }
10946 else
10947 dwarf2_complex_location_expr_complaint ();
10948
10949 if (!fnp->fcontext)
10950 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10951 }
3690dd37 10952 else if (attr_form_is_section_offset (attr))
8e19ed76 10953 {
4d3c2250 10954 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10955 }
10956 else
10957 {
4d3c2250
KB
10958 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10959 fieldname);
8e19ed76 10960 }
0d564a31 10961 }
d48cc9dd
DJ
10962 else
10963 {
10964 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10965 if (attr && DW_UNSND (attr))
10966 {
10967 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10968 complaint (&symfile_complaints,
3e43a32a
MS
10969 _("Member function \"%s\" (offset %d) is virtual "
10970 "but the vtable offset is not specified"),
b64f50a1 10971 fieldname, die->offset.sect_off);
9655fd1a 10972 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
10973 TYPE_CPLUS_DYNAMIC (type) = 1;
10974 }
10975 }
c906108c
SS
10976}
10977
10978/* Create the vector of member function fields, and attach it to the type. */
10979
10980static void
fba45db2 10981dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10982 struct dwarf2_cu *cu)
c906108c
SS
10983{
10984 struct fnfieldlist *flp;
c906108c
SS
10985 int i;
10986
b4ba55a1 10987 if (cu->language == language_ada)
a73c6dcd 10988 error (_("unexpected member functions in Ada type"));
b4ba55a1 10989
c906108c
SS
10990 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10991 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10992 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10993
10994 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10995 {
10996 struct nextfnfield *nfp = flp->head;
10997 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10998 int k;
10999
11000 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11001 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11002 fn_flp->fn_fields = (struct fn_field *)
11003 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11004 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11005 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11006 }
11007
11008 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11009}
11010
1168df01
JB
11011/* Returns non-zero if NAME is the name of a vtable member in CU's
11012 language, zero otherwise. */
11013static int
11014is_vtable_name (const char *name, struct dwarf2_cu *cu)
11015{
11016 static const char vptr[] = "_vptr";
987504bb 11017 static const char vtable[] = "vtable";
1168df01 11018
987504bb
JJ
11019 /* Look for the C++ and Java forms of the vtable. */
11020 if ((cu->language == language_java
11021 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11022 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11023 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11024 return 1;
11025
11026 return 0;
11027}
11028
c0dd20ea 11029/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11030 functions, with the ABI-specified layout. If TYPE describes
11031 such a structure, smash it into a member function type.
61049d3b
DJ
11032
11033 GCC shouldn't do this; it should just output pointer to member DIEs.
11034 This is GCC PR debug/28767. */
c0dd20ea 11035
0b92b5bb
TT
11036static void
11037quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11038{
0b92b5bb 11039 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11040
11041 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11042 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11043 return;
c0dd20ea
DJ
11044
11045 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11046 if (TYPE_FIELD_NAME (type, 0) == NULL
11047 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11048 || TYPE_FIELD_NAME (type, 1) == NULL
11049 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11050 return;
c0dd20ea
DJ
11051
11052 /* Find the type of the method. */
0b92b5bb 11053 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11054 if (pfn_type == NULL
11055 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11056 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11057 return;
c0dd20ea
DJ
11058
11059 /* Look for the "this" argument. */
11060 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11061 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11062 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11063 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11064 return;
c0dd20ea
DJ
11065
11066 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11067 new_type = alloc_type (objfile);
11068 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11069 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11070 TYPE_VARARGS (pfn_type));
0b92b5bb 11071 smash_to_methodptr_type (type, new_type);
c0dd20ea 11072}
1168df01 11073
685b1105
JK
11074/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11075 (icc). */
11076
11077static int
11078producer_is_icc (struct dwarf2_cu *cu)
11079{
11080 if (!cu->checked_producer)
11081 check_producer (cu);
11082
11083 return cu->producer_is_icc;
11084}
11085
c906108c 11086/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11087 (definition) to create a type for the structure or union. Fill in
11088 the type's name and general properties; the members will not be
11089 processed until process_structure_type.
c906108c 11090
c767944b
DJ
11091 NOTE: we need to call these functions regardless of whether or not the
11092 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11093 structure or union. This gets the type entered into our set of
11094 user defined types.
11095
11096 However, if the structure is incomplete (an opaque struct/union)
11097 then suppress creating a symbol table entry for it since gdb only
11098 wants to find the one with the complete definition. Note that if
11099 it is complete, we just call new_symbol, which does it's own
11100 checking about whether the struct/union is anonymous or not (and
11101 suppresses creating a symbol table entry itself). */
11102
f792889a 11103static struct type *
134d01f1 11104read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11105{
e7c27a73 11106 struct objfile *objfile = cu->objfile;
c906108c
SS
11107 struct type *type;
11108 struct attribute *attr;
39cbfefa 11109 char *name;
c906108c 11110
348e048f
DE
11111 /* If the definition of this type lives in .debug_types, read that type.
11112 Don't follow DW_AT_specification though, that will take us back up
11113 the chain and we want to go down. */
45e58e77 11114 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11115 if (attr)
11116 {
11117 struct dwarf2_cu *type_cu = cu;
11118 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11119
348e048f
DE
11120 /* We could just recurse on read_structure_type, but we need to call
11121 get_die_type to ensure only one type for this DIE is created.
11122 This is important, for example, because for c++ classes we need
11123 TYPE_NAME set which is only done by new_symbol. Blech. */
11124 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11125
11126 /* TYPE_CU may not be the same as CU.
11127 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11128 return set_die_type (die, type, cu);
11129 }
11130
c0dd20ea 11131 type = alloc_type (objfile);
c906108c 11132 INIT_CPLUS_SPECIFIC (type);
93311388 11133
39cbfefa
DJ
11134 name = dwarf2_name (die, cu);
11135 if (name != NULL)
c906108c 11136 {
987504bb
JJ
11137 if (cu->language == language_cplus
11138 || cu->language == language_java)
63d06c5c 11139 {
3da10d80
KS
11140 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11141
11142 /* dwarf2_full_name might have already finished building the DIE's
11143 type. If so, there is no need to continue. */
11144 if (get_die_type (die, cu) != NULL)
11145 return get_die_type (die, cu);
11146
11147 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11148 if (die->tag == DW_TAG_structure_type
11149 || die->tag == DW_TAG_class_type)
11150 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11151 }
11152 else
11153 {
d8151005
DJ
11154 /* The name is already allocated along with this objfile, so
11155 we don't need to duplicate it for the type. */
94af9270
KS
11156 TYPE_TAG_NAME (type) = (char *) name;
11157 if (die->tag == DW_TAG_class_type)
11158 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11159 }
c906108c
SS
11160 }
11161
11162 if (die->tag == DW_TAG_structure_type)
11163 {
11164 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11165 }
11166 else if (die->tag == DW_TAG_union_type)
11167 {
11168 TYPE_CODE (type) = TYPE_CODE_UNION;
11169 }
11170 else
11171 {
c906108c
SS
11172 TYPE_CODE (type) = TYPE_CODE_CLASS;
11173 }
11174
0cc2414c
TT
11175 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11176 TYPE_DECLARED_CLASS (type) = 1;
11177
e142c38c 11178 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11179 if (attr)
11180 {
11181 TYPE_LENGTH (type) = DW_UNSND (attr);
11182 }
11183 else
11184 {
11185 TYPE_LENGTH (type) = 0;
11186 }
11187
685b1105
JK
11188 if (producer_is_icc (cu))
11189 {
11190 /* ICC does not output the required DW_AT_declaration
11191 on incomplete types, but gives them a size of zero. */
11192 }
11193 else
11194 TYPE_STUB_SUPPORTED (type) = 1;
11195
dc718098 11196 if (die_is_declaration (die, cu))
876cecd0 11197 TYPE_STUB (type) = 1;
a6c727b2
DJ
11198 else if (attr == NULL && die->child == NULL
11199 && producer_is_realview (cu->producer))
11200 /* RealView does not output the required DW_AT_declaration
11201 on incomplete types. */
11202 TYPE_STUB (type) = 1;
dc718098 11203
c906108c
SS
11204 /* We need to add the type field to the die immediately so we don't
11205 infinitely recurse when dealing with pointers to the structure
0963b4bd 11206 type within the structure itself. */
1c379e20 11207 set_die_type (die, type, cu);
c906108c 11208
7e314c57
JK
11209 /* set_die_type should be already done. */
11210 set_descriptive_type (type, die, cu);
11211
c767944b
DJ
11212 return type;
11213}
11214
11215/* Finish creating a structure or union type, including filling in
11216 its members and creating a symbol for it. */
11217
11218static void
11219process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11220{
11221 struct objfile *objfile = cu->objfile;
11222 struct die_info *child_die = die->child;
11223 struct type *type;
11224
11225 type = get_die_type (die, cu);
11226 if (type == NULL)
11227 type = read_structure_type (die, cu);
11228
e142c38c 11229 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11230 {
11231 struct field_info fi;
11232 struct die_info *child_die;
34eaf542 11233 VEC (symbolp) *template_args = NULL;
c767944b 11234 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11235
11236 memset (&fi, 0, sizeof (struct field_info));
11237
639d11d3 11238 child_die = die->child;
c906108c
SS
11239
11240 while (child_die && child_die->tag)
11241 {
a9a9bd0f
DC
11242 if (child_die->tag == DW_TAG_member
11243 || child_die->tag == DW_TAG_variable)
c906108c 11244 {
a9a9bd0f
DC
11245 /* NOTE: carlton/2002-11-05: A C++ static data member
11246 should be a DW_TAG_member that is a declaration, but
11247 all versions of G++ as of this writing (so through at
11248 least 3.2.1) incorrectly generate DW_TAG_variable
11249 tags for them instead. */
e7c27a73 11250 dwarf2_add_field (&fi, child_die, cu);
c906108c 11251 }
8713b1b1 11252 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11253 {
0963b4bd 11254 /* C++ member function. */
e7c27a73 11255 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11256 }
11257 else if (child_die->tag == DW_TAG_inheritance)
11258 {
11259 /* C++ base class field. */
e7c27a73 11260 dwarf2_add_field (&fi, child_die, cu);
c906108c 11261 }
98751a41
JK
11262 else if (child_die->tag == DW_TAG_typedef)
11263 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11264 else if (child_die->tag == DW_TAG_template_type_param
11265 || child_die->tag == DW_TAG_template_value_param)
11266 {
11267 struct symbol *arg = new_symbol (child_die, NULL, cu);
11268
f1078f66
DJ
11269 if (arg != NULL)
11270 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11271 }
11272
c906108c
SS
11273 child_die = sibling_die (child_die);
11274 }
11275
34eaf542
TT
11276 /* Attach template arguments to type. */
11277 if (! VEC_empty (symbolp, template_args))
11278 {
11279 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11280 TYPE_N_TEMPLATE_ARGUMENTS (type)
11281 = VEC_length (symbolp, template_args);
11282 TYPE_TEMPLATE_ARGUMENTS (type)
11283 = obstack_alloc (&objfile->objfile_obstack,
11284 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11285 * sizeof (struct symbol *)));
11286 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11287 VEC_address (symbolp, template_args),
11288 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11289 * sizeof (struct symbol *)));
11290 VEC_free (symbolp, template_args);
11291 }
11292
c906108c
SS
11293 /* Attach fields and member functions to the type. */
11294 if (fi.nfields)
e7c27a73 11295 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11296 if (fi.nfnfields)
11297 {
e7c27a73 11298 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11299
c5aa993b 11300 /* Get the type which refers to the base class (possibly this
c906108c 11301 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11302 class from the DW_AT_containing_type attribute. This use of
11303 DW_AT_containing_type is a GNU extension. */
c906108c 11304
e142c38c 11305 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11306 {
e7c27a73 11307 struct type *t = die_containing_type (die, cu);
c906108c
SS
11308
11309 TYPE_VPTR_BASETYPE (type) = t;
11310 if (type == t)
11311 {
c906108c
SS
11312 int i;
11313
11314 /* Our own class provides vtbl ptr. */
11315 for (i = TYPE_NFIELDS (t) - 1;
11316 i >= TYPE_N_BASECLASSES (t);
11317 --i)
11318 {
0d5cff50 11319 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11320
1168df01 11321 if (is_vtable_name (fieldname, cu))
c906108c
SS
11322 {
11323 TYPE_VPTR_FIELDNO (type) = i;
11324 break;
11325 }
11326 }
11327
11328 /* Complain if virtual function table field not found. */
11329 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11330 complaint (&symfile_complaints,
3e43a32a
MS
11331 _("virtual function table pointer "
11332 "not found when defining class '%s'"),
4d3c2250
KB
11333 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11334 "");
c906108c
SS
11335 }
11336 else
11337 {
11338 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11339 }
11340 }
f6235d4c
EZ
11341 else if (cu->producer
11342 && strncmp (cu->producer,
11343 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11344 {
11345 /* The IBM XLC compiler does not provide direct indication
11346 of the containing type, but the vtable pointer is
11347 always named __vfp. */
11348
11349 int i;
11350
11351 for (i = TYPE_NFIELDS (type) - 1;
11352 i >= TYPE_N_BASECLASSES (type);
11353 --i)
11354 {
11355 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11356 {
11357 TYPE_VPTR_FIELDNO (type) = i;
11358 TYPE_VPTR_BASETYPE (type) = type;
11359 break;
11360 }
11361 }
11362 }
c906108c 11363 }
98751a41
JK
11364
11365 /* Copy fi.typedef_field_list linked list elements content into the
11366 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11367 if (fi.typedef_field_list)
11368 {
11369 int i = fi.typedef_field_list_count;
11370
a0d7a4ff 11371 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11372 TYPE_TYPEDEF_FIELD_ARRAY (type)
11373 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11374 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11375
11376 /* Reverse the list order to keep the debug info elements order. */
11377 while (--i >= 0)
11378 {
11379 struct typedef_field *dest, *src;
6e70227d 11380
98751a41
JK
11381 dest = &TYPE_TYPEDEF_FIELD (type, i);
11382 src = &fi.typedef_field_list->field;
11383 fi.typedef_field_list = fi.typedef_field_list->next;
11384 *dest = *src;
11385 }
11386 }
c767944b
DJ
11387
11388 do_cleanups (back_to);
eb2a6f42
TT
11389
11390 if (HAVE_CPLUS_STRUCT (type))
11391 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11392 }
63d06c5c 11393
bb5ed363 11394 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11395
90aeadfc
DC
11396 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11397 snapshots) has been known to create a die giving a declaration
11398 for a class that has, as a child, a die giving a definition for a
11399 nested class. So we have to process our children even if the
11400 current die is a declaration. Normally, of course, a declaration
11401 won't have any children at all. */
134d01f1 11402
90aeadfc
DC
11403 while (child_die != NULL && child_die->tag)
11404 {
11405 if (child_die->tag == DW_TAG_member
11406 || child_die->tag == DW_TAG_variable
34eaf542
TT
11407 || child_die->tag == DW_TAG_inheritance
11408 || child_die->tag == DW_TAG_template_value_param
11409 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11410 {
90aeadfc 11411 /* Do nothing. */
134d01f1 11412 }
90aeadfc
DC
11413 else
11414 process_die (child_die, cu);
134d01f1 11415
90aeadfc 11416 child_die = sibling_die (child_die);
134d01f1
DJ
11417 }
11418
fa4028e9
JB
11419 /* Do not consider external references. According to the DWARF standard,
11420 these DIEs are identified by the fact that they have no byte_size
11421 attribute, and a declaration attribute. */
11422 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11423 || !die_is_declaration (die, cu))
c767944b 11424 new_symbol (die, type, cu);
134d01f1
DJ
11425}
11426
11427/* Given a DW_AT_enumeration_type die, set its type. We do not
11428 complete the type's fields yet, or create any symbols. */
c906108c 11429
f792889a 11430static struct type *
134d01f1 11431read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11432{
e7c27a73 11433 struct objfile *objfile = cu->objfile;
c906108c 11434 struct type *type;
c906108c 11435 struct attribute *attr;
0114d602 11436 const char *name;
134d01f1 11437
348e048f
DE
11438 /* If the definition of this type lives in .debug_types, read that type.
11439 Don't follow DW_AT_specification though, that will take us back up
11440 the chain and we want to go down. */
45e58e77 11441 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11442 if (attr)
11443 {
11444 struct dwarf2_cu *type_cu = cu;
11445 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11446
348e048f 11447 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11448
11449 /* TYPE_CU may not be the same as CU.
11450 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11451 return set_die_type (die, type, cu);
11452 }
11453
c906108c
SS
11454 type = alloc_type (objfile);
11455
11456 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11457 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11458 if (name != NULL)
0114d602 11459 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11460
e142c38c 11461 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11462 if (attr)
11463 {
11464 TYPE_LENGTH (type) = DW_UNSND (attr);
11465 }
11466 else
11467 {
11468 TYPE_LENGTH (type) = 0;
11469 }
11470
137033e9
JB
11471 /* The enumeration DIE can be incomplete. In Ada, any type can be
11472 declared as private in the package spec, and then defined only
11473 inside the package body. Such types are known as Taft Amendment
11474 Types. When another package uses such a type, an incomplete DIE
11475 may be generated by the compiler. */
02eb380e 11476 if (die_is_declaration (die, cu))
876cecd0 11477 TYPE_STUB (type) = 1;
02eb380e 11478
f792889a 11479 return set_die_type (die, type, cu);
134d01f1
DJ
11480}
11481
11482/* Given a pointer to a die which begins an enumeration, process all
11483 the dies that define the members of the enumeration, and create the
11484 symbol for the enumeration type.
11485
11486 NOTE: We reverse the order of the element list. */
11487
11488static void
11489process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11490{
f792889a 11491 struct type *this_type;
134d01f1 11492
f792889a
DJ
11493 this_type = get_die_type (die, cu);
11494 if (this_type == NULL)
11495 this_type = read_enumeration_type (die, cu);
9dc481d3 11496
639d11d3 11497 if (die->child != NULL)
c906108c 11498 {
9dc481d3
DE
11499 struct die_info *child_die;
11500 struct symbol *sym;
11501 struct field *fields = NULL;
11502 int num_fields = 0;
11503 int unsigned_enum = 1;
11504 char *name;
cafec441
TT
11505 int flag_enum = 1;
11506 ULONGEST mask = 0;
9dc481d3 11507
639d11d3 11508 child_die = die->child;
c906108c
SS
11509 while (child_die && child_die->tag)
11510 {
11511 if (child_die->tag != DW_TAG_enumerator)
11512 {
e7c27a73 11513 process_die (child_die, cu);
c906108c
SS
11514 }
11515 else
11516 {
39cbfefa
DJ
11517 name = dwarf2_name (child_die, cu);
11518 if (name)
c906108c 11519 {
f792889a 11520 sym = new_symbol (child_die, this_type, cu);
c906108c 11521 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11522 {
11523 unsigned_enum = 0;
11524 flag_enum = 0;
11525 }
11526 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11527 flag_enum = 0;
11528 else
11529 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11530
11531 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11532 {
11533 fields = (struct field *)
11534 xrealloc (fields,
11535 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11536 * sizeof (struct field));
c906108c
SS
11537 }
11538
3567439c 11539 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11540 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11541 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11542 FIELD_BITSIZE (fields[num_fields]) = 0;
11543
11544 num_fields++;
11545 }
11546 }
11547
11548 child_die = sibling_die (child_die);
11549 }
11550
11551 if (num_fields)
11552 {
f792889a
DJ
11553 TYPE_NFIELDS (this_type) = num_fields;
11554 TYPE_FIELDS (this_type) = (struct field *)
11555 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11556 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11557 sizeof (struct field) * num_fields);
b8c9b27d 11558 xfree (fields);
c906108c
SS
11559 }
11560 if (unsigned_enum)
876cecd0 11561 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11562 if (flag_enum)
11563 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11564 }
134d01f1 11565
6c83ed52
TT
11566 /* If we are reading an enum from a .debug_types unit, and the enum
11567 is a declaration, and the enum is not the signatured type in the
11568 unit, then we do not want to add a symbol for it. Adding a
11569 symbol would in some cases obscure the true definition of the
11570 enum, giving users an incomplete type when the definition is
11571 actually available. Note that we do not want to do this for all
11572 enums which are just declarations, because C++0x allows forward
11573 enum declarations. */
3019eac3 11574 if (cu->per_cu->is_debug_types
6c83ed52
TT
11575 && die_is_declaration (die, cu))
11576 {
52dc124a 11577 struct signatured_type *sig_type;
6c83ed52 11578
52dc124a 11579 sig_type
6c83ed52 11580 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11581 cu->per_cu->info_or_types_section,
6c83ed52 11582 cu->per_cu->offset);
3019eac3
DE
11583 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11584 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11585 return;
11586 }
11587
f792889a 11588 new_symbol (die, this_type, cu);
c906108c
SS
11589}
11590
11591/* Extract all information from a DW_TAG_array_type DIE and put it in
11592 the DIE's type field. For now, this only handles one dimensional
11593 arrays. */
11594
f792889a 11595static struct type *
e7c27a73 11596read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11597{
e7c27a73 11598 struct objfile *objfile = cu->objfile;
c906108c 11599 struct die_info *child_die;
7e314c57 11600 struct type *type;
c906108c
SS
11601 struct type *element_type, *range_type, *index_type;
11602 struct type **range_types = NULL;
11603 struct attribute *attr;
11604 int ndim = 0;
11605 struct cleanup *back_to;
39cbfefa 11606 char *name;
c906108c 11607
e7c27a73 11608 element_type = die_type (die, cu);
c906108c 11609
7e314c57
JK
11610 /* The die_type call above may have already set the type for this DIE. */
11611 type = get_die_type (die, cu);
11612 if (type)
11613 return type;
11614
c906108c
SS
11615 /* Irix 6.2 native cc creates array types without children for
11616 arrays with unspecified length. */
639d11d3 11617 if (die->child == NULL)
c906108c 11618 {
46bf5051 11619 index_type = objfile_type (objfile)->builtin_int;
c906108c 11620 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11621 type = create_array_type (NULL, element_type, range_type);
11622 return set_die_type (die, type, cu);
c906108c
SS
11623 }
11624
11625 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11626 child_die = die->child;
c906108c
SS
11627 while (child_die && child_die->tag)
11628 {
11629 if (child_die->tag == DW_TAG_subrange_type)
11630 {
f792889a 11631 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11632
f792889a 11633 if (child_type != NULL)
a02abb62 11634 {
0963b4bd
MS
11635 /* The range type was succesfully read. Save it for the
11636 array type creation. */
a02abb62
JB
11637 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11638 {
11639 range_types = (struct type **)
11640 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11641 * sizeof (struct type *));
11642 if (ndim == 0)
11643 make_cleanup (free_current_contents, &range_types);
11644 }
f792889a 11645 range_types[ndim++] = child_type;
a02abb62 11646 }
c906108c
SS
11647 }
11648 child_die = sibling_die (child_die);
11649 }
11650
11651 /* Dwarf2 dimensions are output from left to right, create the
11652 necessary array types in backwards order. */
7ca2d3a3 11653
c906108c 11654 type = element_type;
7ca2d3a3
DL
11655
11656 if (read_array_order (die, cu) == DW_ORD_col_major)
11657 {
11658 int i = 0;
9a619af0 11659
7ca2d3a3
DL
11660 while (i < ndim)
11661 type = create_array_type (NULL, type, range_types[i++]);
11662 }
11663 else
11664 {
11665 while (ndim-- > 0)
11666 type = create_array_type (NULL, type, range_types[ndim]);
11667 }
c906108c 11668
f5f8a009
EZ
11669 /* Understand Dwarf2 support for vector types (like they occur on
11670 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11671 array type. This is not part of the Dwarf2/3 standard yet, but a
11672 custom vendor extension. The main difference between a regular
11673 array and the vector variant is that vectors are passed by value
11674 to functions. */
e142c38c 11675 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11676 if (attr)
ea37ba09 11677 make_vector_type (type);
f5f8a009 11678
dbc98a8b
KW
11679 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11680 implementation may choose to implement triple vectors using this
11681 attribute. */
11682 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11683 if (attr)
11684 {
11685 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11686 TYPE_LENGTH (type) = DW_UNSND (attr);
11687 else
3e43a32a
MS
11688 complaint (&symfile_complaints,
11689 _("DW_AT_byte_size for array type smaller "
11690 "than the total size of elements"));
dbc98a8b
KW
11691 }
11692
39cbfefa
DJ
11693 name = dwarf2_name (die, cu);
11694 if (name)
11695 TYPE_NAME (type) = name;
6e70227d 11696
0963b4bd 11697 /* Install the type in the die. */
7e314c57
JK
11698 set_die_type (die, type, cu);
11699
11700 /* set_die_type should be already done. */
b4ba55a1
JB
11701 set_descriptive_type (type, die, cu);
11702
c906108c
SS
11703 do_cleanups (back_to);
11704
7e314c57 11705 return type;
c906108c
SS
11706}
11707
7ca2d3a3 11708static enum dwarf_array_dim_ordering
6e70227d 11709read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11710{
11711 struct attribute *attr;
11712
11713 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11714
11715 if (attr) return DW_SND (attr);
11716
0963b4bd
MS
11717 /* GNU F77 is a special case, as at 08/2004 array type info is the
11718 opposite order to the dwarf2 specification, but data is still
11719 laid out as per normal fortran.
7ca2d3a3 11720
0963b4bd
MS
11721 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11722 version checking. */
7ca2d3a3 11723
905e0470
PM
11724 if (cu->language == language_fortran
11725 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11726 {
11727 return DW_ORD_row_major;
11728 }
11729
6e70227d 11730 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11731 {
11732 case array_column_major:
11733 return DW_ORD_col_major;
11734 case array_row_major:
11735 default:
11736 return DW_ORD_row_major;
11737 };
11738}
11739
72019c9c 11740/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11741 the DIE's type field. */
72019c9c 11742
f792889a 11743static struct type *
72019c9c
GM
11744read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11745{
7e314c57
JK
11746 struct type *domain_type, *set_type;
11747 struct attribute *attr;
f792889a 11748
7e314c57
JK
11749 domain_type = die_type (die, cu);
11750
11751 /* The die_type call above may have already set the type for this DIE. */
11752 set_type = get_die_type (die, cu);
11753 if (set_type)
11754 return set_type;
11755
11756 set_type = create_set_type (NULL, domain_type);
11757
11758 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11759 if (attr)
11760 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11761
f792889a 11762 return set_die_type (die, set_type, cu);
72019c9c 11763}
7ca2d3a3 11764
0971de02
TT
11765/* A helper for read_common_block that creates a locexpr baton.
11766 SYM is the symbol which we are marking as computed.
11767 COMMON_DIE is the DIE for the common block.
11768 COMMON_LOC is the location expression attribute for the common
11769 block itself.
11770 MEMBER_LOC is the location expression attribute for the particular
11771 member of the common block that we are processing.
11772 CU is the CU from which the above come. */
11773
11774static void
11775mark_common_block_symbol_computed (struct symbol *sym,
11776 struct die_info *common_die,
11777 struct attribute *common_loc,
11778 struct attribute *member_loc,
11779 struct dwarf2_cu *cu)
11780{
11781 struct objfile *objfile = dwarf2_per_objfile->objfile;
11782 struct dwarf2_locexpr_baton *baton;
11783 gdb_byte *ptr;
11784 unsigned int cu_off;
11785 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11786 LONGEST offset = 0;
11787
11788 gdb_assert (common_loc && member_loc);
11789 gdb_assert (attr_form_is_block (common_loc));
11790 gdb_assert (attr_form_is_block (member_loc)
11791 || attr_form_is_constant (member_loc));
11792
11793 baton = obstack_alloc (&objfile->objfile_obstack,
11794 sizeof (struct dwarf2_locexpr_baton));
11795 baton->per_cu = cu->per_cu;
11796 gdb_assert (baton->per_cu);
11797
11798 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11799
11800 if (attr_form_is_constant (member_loc))
11801 {
11802 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11803 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11804 }
11805 else
11806 baton->size += DW_BLOCK (member_loc)->size;
11807
11808 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11809 baton->data = ptr;
11810
11811 *ptr++ = DW_OP_call4;
11812 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11813 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11814 ptr += 4;
11815
11816 if (attr_form_is_constant (member_loc))
11817 {
11818 *ptr++ = DW_OP_addr;
11819 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11820 ptr += cu->header.addr_size;
11821 }
11822 else
11823 {
11824 /* We have to copy the data here, because DW_OP_call4 will only
11825 use a DW_AT_location attribute. */
11826 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11827 ptr += DW_BLOCK (member_loc)->size;
11828 }
11829
11830 *ptr++ = DW_OP_plus;
11831 gdb_assert (ptr - baton->data == baton->size);
11832
11833 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11834 SYMBOL_LOCATION_BATON (sym) = baton;
11835 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11836}
11837
4357ac6c
TT
11838/* Create appropriate locally-scoped variables for all the
11839 DW_TAG_common_block entries. Also create a struct common_block
11840 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11841 is used to sepate the common blocks name namespace from regular
11842 variable names. */
c906108c
SS
11843
11844static void
e7c27a73 11845read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11846{
0971de02
TT
11847 struct attribute *attr;
11848
11849 attr = dwarf2_attr (die, DW_AT_location, cu);
11850 if (attr)
11851 {
11852 /* Support the .debug_loc offsets. */
11853 if (attr_form_is_block (attr))
11854 {
11855 /* Ok. */
11856 }
11857 else if (attr_form_is_section_offset (attr))
11858 {
11859 dwarf2_complex_location_expr_complaint ();
11860 attr = NULL;
11861 }
11862 else
11863 {
11864 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11865 "common block member");
11866 attr = NULL;
11867 }
11868 }
11869
639d11d3 11870 if (die->child != NULL)
c906108c 11871 {
4357ac6c
TT
11872 struct objfile *objfile = cu->objfile;
11873 struct die_info *child_die;
11874 size_t n_entries = 0, size;
11875 struct common_block *common_block;
11876 struct symbol *sym;
74ac6d43 11877
4357ac6c
TT
11878 for (child_die = die->child;
11879 child_die && child_die->tag;
11880 child_die = sibling_die (child_die))
11881 ++n_entries;
11882
11883 size = (sizeof (struct common_block)
11884 + (n_entries - 1) * sizeof (struct symbol *));
11885 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11886 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11887 common_block->n_entries = 0;
11888
11889 for (child_die = die->child;
11890 child_die && child_die->tag;
11891 child_die = sibling_die (child_die))
11892 {
11893 /* Create the symbol in the DW_TAG_common_block block in the current
11894 symbol scope. */
e7c27a73 11895 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11896 if (sym != NULL)
11897 {
11898 struct attribute *member_loc;
11899
11900 common_block->contents[common_block->n_entries++] = sym;
11901
11902 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11903 cu);
11904 if (member_loc)
11905 {
11906 /* GDB has handled this for a long time, but it is
11907 not specified by DWARF. It seems to have been
11908 emitted by gfortran at least as recently as:
11909 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11910 complaint (&symfile_complaints,
11911 _("Variable in common block has "
11912 "DW_AT_data_member_location "
11913 "- DIE at 0x%x [in module %s]"),
11914 child_die->offset.sect_off, cu->objfile->name);
11915
11916 if (attr_form_is_section_offset (member_loc))
11917 dwarf2_complex_location_expr_complaint ();
11918 else if (attr_form_is_constant (member_loc)
11919 || attr_form_is_block (member_loc))
11920 {
11921 if (attr)
11922 mark_common_block_symbol_computed (sym, die, attr,
11923 member_loc, cu);
11924 }
11925 else
11926 dwarf2_complex_location_expr_complaint ();
11927 }
11928 }
c906108c 11929 }
4357ac6c
TT
11930
11931 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11932 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
11933 }
11934}
11935
0114d602 11936/* Create a type for a C++ namespace. */
d9fa45fe 11937
0114d602
DJ
11938static struct type *
11939read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11940{
e7c27a73 11941 struct objfile *objfile = cu->objfile;
0114d602 11942 const char *previous_prefix, *name;
9219021c 11943 int is_anonymous;
0114d602
DJ
11944 struct type *type;
11945
11946 /* For extensions, reuse the type of the original namespace. */
11947 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11948 {
11949 struct die_info *ext_die;
11950 struct dwarf2_cu *ext_cu = cu;
9a619af0 11951
0114d602
DJ
11952 ext_die = dwarf2_extension (die, &ext_cu);
11953 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11954
11955 /* EXT_CU may not be the same as CU.
11956 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11957 return set_die_type (die, type, cu);
11958 }
9219021c 11959
e142c38c 11960 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11961
11962 /* Now build the name of the current namespace. */
11963
0114d602
DJ
11964 previous_prefix = determine_prefix (die, cu);
11965 if (previous_prefix[0] != '\0')
11966 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11967 previous_prefix, name, 0, cu);
0114d602
DJ
11968
11969 /* Create the type. */
11970 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11971 objfile);
11972 TYPE_NAME (type) = (char *) name;
11973 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11974
60531b24 11975 return set_die_type (die, type, cu);
0114d602
DJ
11976}
11977
11978/* Read a C++ namespace. */
11979
11980static void
11981read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11982{
11983 struct objfile *objfile = cu->objfile;
0114d602 11984 int is_anonymous;
9219021c 11985
5c4e30ca
DC
11986 /* Add a symbol associated to this if we haven't seen the namespace
11987 before. Also, add a using directive if it's an anonymous
11988 namespace. */
9219021c 11989
f2f0e013 11990 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
11991 {
11992 struct type *type;
11993
0114d602 11994 type = read_type_die (die, cu);
e7c27a73 11995 new_symbol (die, type, cu);
5c4e30ca 11996
e8e80198 11997 namespace_name (die, &is_anonymous, cu);
5c4e30ca 11998 if (is_anonymous)
0114d602
DJ
11999 {
12000 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12001
c0cc3a76 12002 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 12003 NULL, NULL, &objfile->objfile_obstack);
0114d602 12004 }
5c4e30ca 12005 }
9219021c 12006
639d11d3 12007 if (die->child != NULL)
d9fa45fe 12008 {
639d11d3 12009 struct die_info *child_die = die->child;
6e70227d 12010
d9fa45fe
DC
12011 while (child_die && child_die->tag)
12012 {
e7c27a73 12013 process_die (child_die, cu);
d9fa45fe
DC
12014 child_die = sibling_die (child_die);
12015 }
12016 }
38d518c9
EZ
12017}
12018
f55ee35c
JK
12019/* Read a Fortran module as type. This DIE can be only a declaration used for
12020 imported module. Still we need that type as local Fortran "use ... only"
12021 declaration imports depend on the created type in determine_prefix. */
12022
12023static struct type *
12024read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12025{
12026 struct objfile *objfile = cu->objfile;
12027 char *module_name;
12028 struct type *type;
12029
12030 module_name = dwarf2_name (die, cu);
12031 if (!module_name)
3e43a32a
MS
12032 complaint (&symfile_complaints,
12033 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12034 die->offset.sect_off);
f55ee35c
JK
12035 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12036
12037 /* determine_prefix uses TYPE_TAG_NAME. */
12038 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12039
12040 return set_die_type (die, type, cu);
12041}
12042
5d7cb8df
JK
12043/* Read a Fortran module. */
12044
12045static void
12046read_module (struct die_info *die, struct dwarf2_cu *cu)
12047{
12048 struct die_info *child_die = die->child;
12049
5d7cb8df
JK
12050 while (child_die && child_die->tag)
12051 {
12052 process_die (child_die, cu);
12053 child_die = sibling_die (child_die);
12054 }
12055}
12056
38d518c9
EZ
12057/* Return the name of the namespace represented by DIE. Set
12058 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12059 namespace. */
12060
12061static const char *
e142c38c 12062namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12063{
12064 struct die_info *current_die;
12065 const char *name = NULL;
12066
12067 /* Loop through the extensions until we find a name. */
12068
12069 for (current_die = die;
12070 current_die != NULL;
f2f0e013 12071 current_die = dwarf2_extension (die, &cu))
38d518c9 12072 {
e142c38c 12073 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12074 if (name != NULL)
12075 break;
12076 }
12077
12078 /* Is it an anonymous namespace? */
12079
12080 *is_anonymous = (name == NULL);
12081 if (*is_anonymous)
2b1dbab0 12082 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12083
12084 return name;
d9fa45fe
DC
12085}
12086
c906108c
SS
12087/* Extract all information from a DW_TAG_pointer_type DIE and add to
12088 the user defined type vector. */
12089
f792889a 12090static struct type *
e7c27a73 12091read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12092{
5e2b427d 12093 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12094 struct comp_unit_head *cu_header = &cu->header;
c906108c 12095 struct type *type;
8b2dbe47
KB
12096 struct attribute *attr_byte_size;
12097 struct attribute *attr_address_class;
12098 int byte_size, addr_class;
7e314c57
JK
12099 struct type *target_type;
12100
12101 target_type = die_type (die, cu);
c906108c 12102
7e314c57
JK
12103 /* The die_type call above may have already set the type for this DIE. */
12104 type = get_die_type (die, cu);
12105 if (type)
12106 return type;
12107
12108 type = lookup_pointer_type (target_type);
8b2dbe47 12109
e142c38c 12110 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12111 if (attr_byte_size)
12112 byte_size = DW_UNSND (attr_byte_size);
c906108c 12113 else
8b2dbe47
KB
12114 byte_size = cu_header->addr_size;
12115
e142c38c 12116 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12117 if (attr_address_class)
12118 addr_class = DW_UNSND (attr_address_class);
12119 else
12120 addr_class = DW_ADDR_none;
12121
12122 /* If the pointer size or address class is different than the
12123 default, create a type variant marked as such and set the
12124 length accordingly. */
12125 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12126 {
5e2b427d 12127 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12128 {
12129 int type_flags;
12130
849957d9 12131 type_flags = gdbarch_address_class_type_flags
5e2b427d 12132 (gdbarch, byte_size, addr_class);
876cecd0
TT
12133 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12134 == 0);
8b2dbe47
KB
12135 type = make_type_with_address_space (type, type_flags);
12136 }
12137 else if (TYPE_LENGTH (type) != byte_size)
12138 {
3e43a32a
MS
12139 complaint (&symfile_complaints,
12140 _("invalid pointer size %d"), byte_size);
8b2dbe47 12141 }
6e70227d 12142 else
9a619af0
MS
12143 {
12144 /* Should we also complain about unhandled address classes? */
12145 }
c906108c 12146 }
8b2dbe47
KB
12147
12148 TYPE_LENGTH (type) = byte_size;
f792889a 12149 return set_die_type (die, type, cu);
c906108c
SS
12150}
12151
12152/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12153 the user defined type vector. */
12154
f792889a 12155static struct type *
e7c27a73 12156read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12157{
12158 struct type *type;
12159 struct type *to_type;
12160 struct type *domain;
12161
e7c27a73
DJ
12162 to_type = die_type (die, cu);
12163 domain = die_containing_type (die, cu);
0d5de010 12164
7e314c57
JK
12165 /* The calls above may have already set the type for this DIE. */
12166 type = get_die_type (die, cu);
12167 if (type)
12168 return type;
12169
0d5de010
DJ
12170 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12171 type = lookup_methodptr_type (to_type);
12172 else
12173 type = lookup_memberptr_type (to_type, domain);
c906108c 12174
f792889a 12175 return set_die_type (die, type, cu);
c906108c
SS
12176}
12177
12178/* Extract all information from a DW_TAG_reference_type DIE and add to
12179 the user defined type vector. */
12180
f792889a 12181static struct type *
e7c27a73 12182read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12183{
e7c27a73 12184 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12185 struct type *type, *target_type;
c906108c
SS
12186 struct attribute *attr;
12187
7e314c57
JK
12188 target_type = die_type (die, cu);
12189
12190 /* The die_type call above may have already set the type for this DIE. */
12191 type = get_die_type (die, cu);
12192 if (type)
12193 return type;
12194
12195 type = lookup_reference_type (target_type);
e142c38c 12196 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12197 if (attr)
12198 {
12199 TYPE_LENGTH (type) = DW_UNSND (attr);
12200 }
12201 else
12202 {
107d2387 12203 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12204 }
f792889a 12205 return set_die_type (die, type, cu);
c906108c
SS
12206}
12207
f792889a 12208static struct type *
e7c27a73 12209read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12210{
f792889a 12211 struct type *base_type, *cv_type;
c906108c 12212
e7c27a73 12213 base_type = die_type (die, cu);
7e314c57
JK
12214
12215 /* The die_type call above may have already set the type for this DIE. */
12216 cv_type = get_die_type (die, cu);
12217 if (cv_type)
12218 return cv_type;
12219
2f608a3a
KW
12220 /* In case the const qualifier is applied to an array type, the element type
12221 is so qualified, not the array type (section 6.7.3 of C99). */
12222 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12223 {
12224 struct type *el_type, *inner_array;
12225
12226 base_type = copy_type (base_type);
12227 inner_array = base_type;
12228
12229 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12230 {
12231 TYPE_TARGET_TYPE (inner_array) =
12232 copy_type (TYPE_TARGET_TYPE (inner_array));
12233 inner_array = TYPE_TARGET_TYPE (inner_array);
12234 }
12235
12236 el_type = TYPE_TARGET_TYPE (inner_array);
12237 TYPE_TARGET_TYPE (inner_array) =
12238 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12239
12240 return set_die_type (die, base_type, cu);
12241 }
12242
f792889a
DJ
12243 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12244 return set_die_type (die, cv_type, cu);
c906108c
SS
12245}
12246
f792889a 12247static struct type *
e7c27a73 12248read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12249{
f792889a 12250 struct type *base_type, *cv_type;
c906108c 12251
e7c27a73 12252 base_type = die_type (die, cu);
7e314c57
JK
12253
12254 /* The die_type call above may have already set the type for this DIE. */
12255 cv_type = get_die_type (die, cu);
12256 if (cv_type)
12257 return cv_type;
12258
f792889a
DJ
12259 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12260 return set_die_type (die, cv_type, cu);
c906108c
SS
12261}
12262
12263/* Extract all information from a DW_TAG_string_type DIE and add to
12264 the user defined type vector. It isn't really a user defined type,
12265 but it behaves like one, with other DIE's using an AT_user_def_type
12266 attribute to reference it. */
12267
f792889a 12268static struct type *
e7c27a73 12269read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12270{
e7c27a73 12271 struct objfile *objfile = cu->objfile;
3b7538c0 12272 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12273 struct type *type, *range_type, *index_type, *char_type;
12274 struct attribute *attr;
12275 unsigned int length;
12276
e142c38c 12277 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12278 if (attr)
12279 {
12280 length = DW_UNSND (attr);
12281 }
12282 else
12283 {
0963b4bd 12284 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12285 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12286 if (attr)
12287 {
12288 length = DW_UNSND (attr);
12289 }
12290 else
12291 {
12292 length = 1;
12293 }
c906108c 12294 }
6ccb9162 12295
46bf5051 12296 index_type = objfile_type (objfile)->builtin_int;
c906108c 12297 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12298 char_type = language_string_char_type (cu->language_defn, gdbarch);
12299 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12300
f792889a 12301 return set_die_type (die, type, cu);
c906108c
SS
12302}
12303
12304/* Handle DIES due to C code like:
12305
12306 struct foo
c5aa993b
JM
12307 {
12308 int (*funcp)(int a, long l);
12309 int b;
12310 };
c906108c 12311
0963b4bd 12312 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12313
f792889a 12314static struct type *
e7c27a73 12315read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12316{
bb5ed363 12317 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12318 struct type *type; /* Type that this function returns. */
12319 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12320 struct attribute *attr;
12321
e7c27a73 12322 type = die_type (die, cu);
7e314c57
JK
12323
12324 /* The die_type call above may have already set the type for this DIE. */
12325 ftype = get_die_type (die, cu);
12326 if (ftype)
12327 return ftype;
12328
0c8b41f1 12329 ftype = lookup_function_type (type);
c906108c 12330
5b8101ae 12331 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12332 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12333 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12334 || cu->language == language_cplus
5b8101ae
PM
12335 || cu->language == language_java
12336 || cu->language == language_pascal)
876cecd0 12337 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12338 else if (producer_is_realview (cu->producer))
12339 /* RealView does not emit DW_AT_prototyped. We can not
12340 distinguish prototyped and unprototyped functions; default to
12341 prototyped, since that is more common in modern code (and
12342 RealView warns about unprototyped functions). */
12343 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12344
c055b101
CV
12345 /* Store the calling convention in the type if it's available in
12346 the subroutine die. Otherwise set the calling convention to
12347 the default value DW_CC_normal. */
12348 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12349 if (attr)
12350 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12351 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12352 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12353 else
12354 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12355
12356 /* We need to add the subroutine type to the die immediately so
12357 we don't infinitely recurse when dealing with parameters
0963b4bd 12358 declared as the same subroutine type. */
76c10ea2 12359 set_die_type (die, ftype, cu);
6e70227d 12360
639d11d3 12361 if (die->child != NULL)
c906108c 12362 {
bb5ed363 12363 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12364 struct die_info *child_die;
8072405b 12365 int nparams, iparams;
c906108c
SS
12366
12367 /* Count the number of parameters.
12368 FIXME: GDB currently ignores vararg functions, but knows about
12369 vararg member functions. */
8072405b 12370 nparams = 0;
639d11d3 12371 child_die = die->child;
c906108c
SS
12372 while (child_die && child_die->tag)
12373 {
12374 if (child_die->tag == DW_TAG_formal_parameter)
12375 nparams++;
12376 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12377 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12378 child_die = sibling_die (child_die);
12379 }
12380
12381 /* Allocate storage for parameters and fill them in. */
12382 TYPE_NFIELDS (ftype) = nparams;
12383 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12384 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12385
8072405b
JK
12386 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12387 even if we error out during the parameters reading below. */
12388 for (iparams = 0; iparams < nparams; iparams++)
12389 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12390
12391 iparams = 0;
639d11d3 12392 child_die = die->child;
c906108c
SS
12393 while (child_die && child_die->tag)
12394 {
12395 if (child_die->tag == DW_TAG_formal_parameter)
12396 {
3ce3b1ba
PA
12397 struct type *arg_type;
12398
12399 /* DWARF version 2 has no clean way to discern C++
12400 static and non-static member functions. G++ helps
12401 GDB by marking the first parameter for non-static
12402 member functions (which is the this pointer) as
12403 artificial. We pass this information to
12404 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12405
12406 DWARF version 3 added DW_AT_object_pointer, which GCC
12407 4.5 does not yet generate. */
e142c38c 12408 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12409 if (attr)
12410 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12411 else
418835cc
KS
12412 {
12413 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12414
12415 /* GCC/43521: In java, the formal parameter
12416 "this" is sometimes not marked with DW_AT_artificial. */
12417 if (cu->language == language_java)
12418 {
12419 const char *name = dwarf2_name (child_die, cu);
9a619af0 12420
418835cc
KS
12421 if (name && !strcmp (name, "this"))
12422 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12423 }
12424 }
3ce3b1ba
PA
12425 arg_type = die_type (child_die, cu);
12426
12427 /* RealView does not mark THIS as const, which the testsuite
12428 expects. GCC marks THIS as const in method definitions,
12429 but not in the class specifications (GCC PR 43053). */
12430 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12431 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12432 {
12433 int is_this = 0;
12434 struct dwarf2_cu *arg_cu = cu;
12435 const char *name = dwarf2_name (child_die, cu);
12436
12437 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12438 if (attr)
12439 {
12440 /* If the compiler emits this, use it. */
12441 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12442 is_this = 1;
12443 }
12444 else if (name && strcmp (name, "this") == 0)
12445 /* Function definitions will have the argument names. */
12446 is_this = 1;
12447 else if (name == NULL && iparams == 0)
12448 /* Declarations may not have the names, so like
12449 elsewhere in GDB, assume an artificial first
12450 argument is "this". */
12451 is_this = 1;
12452
12453 if (is_this)
12454 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12455 arg_type, 0);
12456 }
12457
12458 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12459 iparams++;
12460 }
12461 child_die = sibling_die (child_die);
12462 }
12463 }
12464
76c10ea2 12465 return ftype;
c906108c
SS
12466}
12467
f792889a 12468static struct type *
e7c27a73 12469read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12470{
e7c27a73 12471 struct objfile *objfile = cu->objfile;
0114d602 12472 const char *name = NULL;
3c8e0968 12473 struct type *this_type, *target_type;
c906108c 12474
94af9270 12475 name = dwarf2_full_name (NULL, die, cu);
f792889a 12476 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12477 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12478 TYPE_NAME (this_type) = (char *) name;
f792889a 12479 set_die_type (die, this_type, cu);
3c8e0968
DE
12480 target_type = die_type (die, cu);
12481 if (target_type != this_type)
12482 TYPE_TARGET_TYPE (this_type) = target_type;
12483 else
12484 {
12485 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12486 spec and cause infinite loops in GDB. */
12487 complaint (&symfile_complaints,
12488 _("Self-referential DW_TAG_typedef "
12489 "- DIE at 0x%x [in module %s]"),
b64f50a1 12490 die->offset.sect_off, objfile->name);
3c8e0968
DE
12491 TYPE_TARGET_TYPE (this_type) = NULL;
12492 }
f792889a 12493 return this_type;
c906108c
SS
12494}
12495
12496/* Find a representation of a given base type and install
12497 it in the TYPE field of the die. */
12498
f792889a 12499static struct type *
e7c27a73 12500read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12501{
e7c27a73 12502 struct objfile *objfile = cu->objfile;
c906108c
SS
12503 struct type *type;
12504 struct attribute *attr;
12505 int encoding = 0, size = 0;
39cbfefa 12506 char *name;
6ccb9162
UW
12507 enum type_code code = TYPE_CODE_INT;
12508 int type_flags = 0;
12509 struct type *target_type = NULL;
c906108c 12510
e142c38c 12511 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12512 if (attr)
12513 {
12514 encoding = DW_UNSND (attr);
12515 }
e142c38c 12516 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12517 if (attr)
12518 {
12519 size = DW_UNSND (attr);
12520 }
39cbfefa 12521 name = dwarf2_name (die, cu);
6ccb9162 12522 if (!name)
c906108c 12523 {
6ccb9162
UW
12524 complaint (&symfile_complaints,
12525 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12526 }
6ccb9162
UW
12527
12528 switch (encoding)
c906108c 12529 {
6ccb9162
UW
12530 case DW_ATE_address:
12531 /* Turn DW_ATE_address into a void * pointer. */
12532 code = TYPE_CODE_PTR;
12533 type_flags |= TYPE_FLAG_UNSIGNED;
12534 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12535 break;
12536 case DW_ATE_boolean:
12537 code = TYPE_CODE_BOOL;
12538 type_flags |= TYPE_FLAG_UNSIGNED;
12539 break;
12540 case DW_ATE_complex_float:
12541 code = TYPE_CODE_COMPLEX;
12542 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12543 break;
12544 case DW_ATE_decimal_float:
12545 code = TYPE_CODE_DECFLOAT;
12546 break;
12547 case DW_ATE_float:
12548 code = TYPE_CODE_FLT;
12549 break;
12550 case DW_ATE_signed:
12551 break;
12552 case DW_ATE_unsigned:
12553 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12554 if (cu->language == language_fortran
12555 && name
12556 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12557 code = TYPE_CODE_CHAR;
6ccb9162
UW
12558 break;
12559 case DW_ATE_signed_char:
6e70227d 12560 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12561 || cu->language == language_pascal
12562 || cu->language == language_fortran)
6ccb9162
UW
12563 code = TYPE_CODE_CHAR;
12564 break;
12565 case DW_ATE_unsigned_char:
868a0084 12566 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12567 || cu->language == language_pascal
12568 || cu->language == language_fortran)
6ccb9162
UW
12569 code = TYPE_CODE_CHAR;
12570 type_flags |= TYPE_FLAG_UNSIGNED;
12571 break;
75079b2b
TT
12572 case DW_ATE_UTF:
12573 /* We just treat this as an integer and then recognize the
12574 type by name elsewhere. */
12575 break;
12576
6ccb9162
UW
12577 default:
12578 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12579 dwarf_type_encoding_name (encoding));
12580 break;
c906108c 12581 }
6ccb9162 12582
0114d602
DJ
12583 type = init_type (code, size, type_flags, NULL, objfile);
12584 TYPE_NAME (type) = name;
6ccb9162
UW
12585 TYPE_TARGET_TYPE (type) = target_type;
12586
0114d602 12587 if (name && strcmp (name, "char") == 0)
876cecd0 12588 TYPE_NOSIGN (type) = 1;
0114d602 12589
f792889a 12590 return set_die_type (die, type, cu);
c906108c
SS
12591}
12592
a02abb62
JB
12593/* Read the given DW_AT_subrange DIE. */
12594
f792889a 12595static struct type *
a02abb62
JB
12596read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12597{
12598 struct type *base_type;
12599 struct type *range_type;
12600 struct attribute *attr;
4fae6e18
JK
12601 LONGEST low, high;
12602 int low_default_is_valid;
39cbfefa 12603 char *name;
43bbcdc2 12604 LONGEST negative_mask;
e77813c8 12605
a02abb62 12606 base_type = die_type (die, cu);
953ac07e
JK
12607 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12608 check_typedef (base_type);
a02abb62 12609
7e314c57
JK
12610 /* The die_type call above may have already set the type for this DIE. */
12611 range_type = get_die_type (die, cu);
12612 if (range_type)
12613 return range_type;
12614
4fae6e18
JK
12615 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12616 omitting DW_AT_lower_bound. */
12617 switch (cu->language)
6e70227d 12618 {
4fae6e18
JK
12619 case language_c:
12620 case language_cplus:
12621 low = 0;
12622 low_default_is_valid = 1;
12623 break;
12624 case language_fortran:
12625 low = 1;
12626 low_default_is_valid = 1;
12627 break;
12628 case language_d:
12629 case language_java:
12630 case language_objc:
12631 low = 0;
12632 low_default_is_valid = (cu->header.version >= 4);
12633 break;
12634 case language_ada:
12635 case language_m2:
12636 case language_pascal:
a02abb62 12637 low = 1;
4fae6e18
JK
12638 low_default_is_valid = (cu->header.version >= 4);
12639 break;
12640 default:
12641 low = 0;
12642 low_default_is_valid = 0;
12643 break;
a02abb62
JB
12644 }
12645
dd5e6932
DJ
12646 /* FIXME: For variable sized arrays either of these could be
12647 a variable rather than a constant value. We'll allow it,
12648 but we don't know how to handle it. */
e142c38c 12649 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12650 if (attr)
4fae6e18
JK
12651 low = dwarf2_get_attr_constant_value (attr, low);
12652 else if (!low_default_is_valid)
12653 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12654 "- DIE at 0x%x [in module %s]"),
12655 die->offset.sect_off, cu->objfile->name);
a02abb62 12656
e142c38c 12657 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12658 if (attr)
6e70227d 12659 {
d48323d8 12660 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12661 {
12662 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12663 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12664 FIXME: GDB does not yet know how to handle dynamic
12665 arrays properly, treat them as arrays with unspecified
12666 length for now.
12667
12668 FIXME: jimb/2003-09-22: GDB does not really know
12669 how to handle arrays of unspecified length
12670 either; we just represent them as zero-length
12671 arrays. Choose an appropriate upper bound given
12672 the lower bound we've computed above. */
12673 high = low - 1;
12674 }
12675 else
12676 high = dwarf2_get_attr_constant_value (attr, 1);
12677 }
e77813c8
PM
12678 else
12679 {
12680 attr = dwarf2_attr (die, DW_AT_count, cu);
12681 if (attr)
12682 {
12683 int count = dwarf2_get_attr_constant_value (attr, 1);
12684 high = low + count - 1;
12685 }
c2ff108b
JK
12686 else
12687 {
12688 /* Unspecified array length. */
12689 high = low - 1;
12690 }
e77813c8
PM
12691 }
12692
12693 /* Dwarf-2 specifications explicitly allows to create subrange types
12694 without specifying a base type.
12695 In that case, the base type must be set to the type of
12696 the lower bound, upper bound or count, in that order, if any of these
12697 three attributes references an object that has a type.
12698 If no base type is found, the Dwarf-2 specifications say that
12699 a signed integer type of size equal to the size of an address should
12700 be used.
12701 For the following C code: `extern char gdb_int [];'
12702 GCC produces an empty range DIE.
12703 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12704 high bound or count are not yet handled by this code. */
e77813c8
PM
12705 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12706 {
12707 struct objfile *objfile = cu->objfile;
12708 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12709 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12710 struct type *int_type = objfile_type (objfile)->builtin_int;
12711
12712 /* Test "int", "long int", and "long long int" objfile types,
12713 and select the first one having a size above or equal to the
12714 architecture address size. */
12715 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12716 base_type = int_type;
12717 else
12718 {
12719 int_type = objfile_type (objfile)->builtin_long;
12720 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12721 base_type = int_type;
12722 else
12723 {
12724 int_type = objfile_type (objfile)->builtin_long_long;
12725 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12726 base_type = int_type;
12727 }
12728 }
12729 }
a02abb62 12730
6e70227d 12731 negative_mask =
43bbcdc2
PH
12732 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12733 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12734 low |= negative_mask;
12735 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12736 high |= negative_mask;
12737
a02abb62
JB
12738 range_type = create_range_type (NULL, base_type, low, high);
12739
bbb0eef6
JK
12740 /* Mark arrays with dynamic length at least as an array of unspecified
12741 length. GDB could check the boundary but before it gets implemented at
12742 least allow accessing the array elements. */
d48323d8 12743 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12744 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12745
c2ff108b
JK
12746 /* Ada expects an empty array on no boundary attributes. */
12747 if (attr == NULL && cu->language != language_ada)
12748 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12749
39cbfefa
DJ
12750 name = dwarf2_name (die, cu);
12751 if (name)
12752 TYPE_NAME (range_type) = name;
6e70227d 12753
e142c38c 12754 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12755 if (attr)
12756 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12757
7e314c57
JK
12758 set_die_type (die, range_type, cu);
12759
12760 /* set_die_type should be already done. */
b4ba55a1
JB
12761 set_descriptive_type (range_type, die, cu);
12762
7e314c57 12763 return range_type;
a02abb62 12764}
6e70227d 12765
f792889a 12766static struct type *
81a17f79
JB
12767read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12768{
12769 struct type *type;
81a17f79 12770
81a17f79
JB
12771 /* For now, we only support the C meaning of an unspecified type: void. */
12772
0114d602
DJ
12773 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12774 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12775
f792889a 12776 return set_die_type (die, type, cu);
81a17f79 12777}
a02abb62 12778
639d11d3
DC
12779/* Read a single die and all its descendents. Set the die's sibling
12780 field to NULL; set other fields in the die correctly, and set all
12781 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12782 location of the info_ptr after reading all of those dies. PARENT
12783 is the parent of the die in question. */
12784
12785static struct die_info *
dee91e82
DE
12786read_die_and_children (const struct die_reader_specs *reader,
12787 gdb_byte *info_ptr,
12788 gdb_byte **new_info_ptr,
12789 struct die_info *parent)
639d11d3
DC
12790{
12791 struct die_info *die;
fe1b8b76 12792 gdb_byte *cur_ptr;
639d11d3
DC
12793 int has_children;
12794
93311388 12795 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12796 if (die == NULL)
12797 {
12798 *new_info_ptr = cur_ptr;
12799 return NULL;
12800 }
93311388 12801 store_in_ref_table (die, reader->cu);
639d11d3
DC
12802
12803 if (has_children)
348e048f 12804 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12805 else
12806 {
12807 die->child = NULL;
12808 *new_info_ptr = cur_ptr;
12809 }
12810
12811 die->sibling = NULL;
12812 die->parent = parent;
12813 return die;
12814}
12815
12816/* Read a die, all of its descendents, and all of its siblings; set
12817 all of the fields of all of the dies correctly. Arguments are as
12818 in read_die_and_children. */
12819
12820static struct die_info *
93311388
DE
12821read_die_and_siblings (const struct die_reader_specs *reader,
12822 gdb_byte *info_ptr,
fe1b8b76 12823 gdb_byte **new_info_ptr,
639d11d3
DC
12824 struct die_info *parent)
12825{
12826 struct die_info *first_die, *last_sibling;
fe1b8b76 12827 gdb_byte *cur_ptr;
639d11d3 12828
c906108c 12829 cur_ptr = info_ptr;
639d11d3
DC
12830 first_die = last_sibling = NULL;
12831
12832 while (1)
c906108c 12833 {
639d11d3 12834 struct die_info *die
dee91e82 12835 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12836
1d325ec1 12837 if (die == NULL)
c906108c 12838 {
639d11d3
DC
12839 *new_info_ptr = cur_ptr;
12840 return first_die;
c906108c 12841 }
1d325ec1
DJ
12842
12843 if (!first_die)
12844 first_die = die;
c906108c 12845 else
1d325ec1
DJ
12846 last_sibling->sibling = die;
12847
12848 last_sibling = die;
c906108c 12849 }
c906108c
SS
12850}
12851
3019eac3
DE
12852/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12853 attributes.
12854 The caller is responsible for filling in the extra attributes
12855 and updating (*DIEP)->num_attrs.
12856 Set DIEP to point to a newly allocated die with its information,
12857 except for its child, sibling, and parent fields.
12858 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12859
12860static gdb_byte *
3019eac3
DE
12861read_full_die_1 (const struct die_reader_specs *reader,
12862 struct die_info **diep, gdb_byte *info_ptr,
12863 int *has_children, int num_extra_attrs)
93311388 12864{
b64f50a1
JK
12865 unsigned int abbrev_number, bytes_read, i;
12866 sect_offset offset;
93311388
DE
12867 struct abbrev_info *abbrev;
12868 struct die_info *die;
12869 struct dwarf2_cu *cu = reader->cu;
12870 bfd *abfd = reader->abfd;
12871
b64f50a1 12872 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12873 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12874 info_ptr += bytes_read;
12875 if (!abbrev_number)
12876 {
12877 *diep = NULL;
12878 *has_children = 0;
12879 return info_ptr;
12880 }
12881
433df2d4 12882 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12883 if (!abbrev)
348e048f
DE
12884 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12885 abbrev_number,
12886 bfd_get_filename (abfd));
12887
3019eac3 12888 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12889 die->offset = offset;
12890 die->tag = abbrev->tag;
12891 die->abbrev = abbrev_number;
12892
3019eac3
DE
12893 /* Make the result usable.
12894 The caller needs to update num_attrs after adding the extra
12895 attributes. */
93311388
DE
12896 die->num_attrs = abbrev->num_attrs;
12897
12898 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12899 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12900 info_ptr);
93311388
DE
12901
12902 *diep = die;
12903 *has_children = abbrev->has_children;
12904 return info_ptr;
12905}
12906
3019eac3
DE
12907/* Read a die and all its attributes.
12908 Set DIEP to point to a newly allocated die with its information,
12909 except for its child, sibling, and parent fields.
12910 Set HAS_CHILDREN to tell whether the die has children or not. */
12911
12912static gdb_byte *
12913read_full_die (const struct die_reader_specs *reader,
12914 struct die_info **diep, gdb_byte *info_ptr,
12915 int *has_children)
12916{
12917 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12918}
433df2d4
DE
12919\f
12920/* Abbreviation tables.
3019eac3 12921
433df2d4 12922 In DWARF version 2, the description of the debugging information is
c906108c
SS
12923 stored in a separate .debug_abbrev section. Before we read any
12924 dies from a section we read in all abbreviations and install them
433df2d4
DE
12925 in a hash table. */
12926
12927/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12928
12929static struct abbrev_info *
12930abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12931{
12932 struct abbrev_info *abbrev;
12933
12934 abbrev = (struct abbrev_info *)
12935 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12936 memset (abbrev, 0, sizeof (struct abbrev_info));
12937 return abbrev;
12938}
12939
12940/* Add an abbreviation to the table. */
c906108c
SS
12941
12942static void
433df2d4
DE
12943abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12944 unsigned int abbrev_number,
12945 struct abbrev_info *abbrev)
12946{
12947 unsigned int hash_number;
12948
12949 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12950 abbrev->next = abbrev_table->abbrevs[hash_number];
12951 abbrev_table->abbrevs[hash_number] = abbrev;
12952}
dee91e82 12953
433df2d4
DE
12954/* Look up an abbrev in the table.
12955 Returns NULL if the abbrev is not found. */
12956
12957static struct abbrev_info *
12958abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12959 unsigned int abbrev_number)
c906108c 12960{
433df2d4
DE
12961 unsigned int hash_number;
12962 struct abbrev_info *abbrev;
12963
12964 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12965 abbrev = abbrev_table->abbrevs[hash_number];
12966
12967 while (abbrev)
12968 {
12969 if (abbrev->number == abbrev_number)
12970 return abbrev;
12971 abbrev = abbrev->next;
12972 }
12973 return NULL;
12974}
12975
12976/* Read in an abbrev table. */
12977
12978static struct abbrev_table *
12979abbrev_table_read_table (struct dwarf2_section_info *section,
12980 sect_offset offset)
12981{
12982 struct objfile *objfile = dwarf2_per_objfile->objfile;
12983 bfd *abfd = section->asection->owner;
12984 struct abbrev_table *abbrev_table;
fe1b8b76 12985 gdb_byte *abbrev_ptr;
c906108c
SS
12986 struct abbrev_info *cur_abbrev;
12987 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 12988 unsigned int abbrev_form;
f3dd6933
DJ
12989 struct attr_abbrev *cur_attrs;
12990 unsigned int allocated_attrs;
c906108c 12991
433df2d4 12992 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 12993 abbrev_table->offset = offset;
433df2d4
DE
12994 obstack_init (&abbrev_table->abbrev_obstack);
12995 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12996 (ABBREV_HASH_SIZE
12997 * sizeof (struct abbrev_info *)));
12998 memset (abbrev_table->abbrevs, 0,
12999 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13000
433df2d4
DE
13001 dwarf2_read_section (objfile, section);
13002 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13003 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13004 abbrev_ptr += bytes_read;
13005
f3dd6933
DJ
13006 allocated_attrs = ATTR_ALLOC_CHUNK;
13007 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13008
0963b4bd 13009 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13010 while (abbrev_number)
13011 {
433df2d4 13012 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13013
13014 /* read in abbrev header */
13015 cur_abbrev->number = abbrev_number;
13016 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13017 abbrev_ptr += bytes_read;
13018 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13019 abbrev_ptr += 1;
13020
13021 /* now read in declarations */
13022 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13023 abbrev_ptr += bytes_read;
13024 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13025 abbrev_ptr += bytes_read;
13026 while (abbrev_name)
13027 {
f3dd6933 13028 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13029 {
f3dd6933
DJ
13030 allocated_attrs += ATTR_ALLOC_CHUNK;
13031 cur_attrs
13032 = xrealloc (cur_attrs, (allocated_attrs
13033 * sizeof (struct attr_abbrev)));
c906108c 13034 }
ae038cb0 13035
f3dd6933
DJ
13036 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13037 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13038 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13039 abbrev_ptr += bytes_read;
13040 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13041 abbrev_ptr += bytes_read;
13042 }
13043
433df2d4 13044 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13045 (cur_abbrev->num_attrs
13046 * sizeof (struct attr_abbrev)));
13047 memcpy (cur_abbrev->attrs, cur_attrs,
13048 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13049
433df2d4 13050 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13051
13052 /* Get next abbreviation.
13053 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13054 always properly terminated with an abbrev number of 0.
13055 Exit loop if we encounter an abbreviation which we have
13056 already read (which means we are about to read the abbreviations
13057 for the next compile unit) or if the end of the abbreviation
13058 table is reached. */
433df2d4 13059 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13060 break;
13061 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13062 abbrev_ptr += bytes_read;
433df2d4 13063 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13064 break;
13065 }
f3dd6933
DJ
13066
13067 xfree (cur_attrs);
433df2d4 13068 return abbrev_table;
c906108c
SS
13069}
13070
433df2d4 13071/* Free the resources held by ABBREV_TABLE. */
c906108c 13072
c906108c 13073static void
433df2d4 13074abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13075{
433df2d4
DE
13076 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13077 xfree (abbrev_table);
c906108c
SS
13078}
13079
f4dc4d17
DE
13080/* Same as abbrev_table_free but as a cleanup.
13081 We pass in a pointer to the pointer to the table so that we can
13082 set the pointer to NULL when we're done. It also simplifies
13083 build_type_unit_groups. */
13084
13085static void
13086abbrev_table_free_cleanup (void *table_ptr)
13087{
13088 struct abbrev_table **abbrev_table_ptr = table_ptr;
13089
13090 if (*abbrev_table_ptr != NULL)
13091 abbrev_table_free (*abbrev_table_ptr);
13092 *abbrev_table_ptr = NULL;
13093}
13094
433df2d4
DE
13095/* Read the abbrev table for CU from ABBREV_SECTION. */
13096
13097static void
13098dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13099 struct dwarf2_section_info *abbrev_section)
c906108c 13100{
433df2d4
DE
13101 cu->abbrev_table =
13102 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13103}
c906108c 13104
433df2d4 13105/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13106
433df2d4
DE
13107static void
13108dwarf2_free_abbrev_table (void *ptr_to_cu)
13109{
13110 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13111
433df2d4
DE
13112 abbrev_table_free (cu->abbrev_table);
13113 /* Set this to NULL so that we SEGV if we try to read it later,
13114 and also because free_comp_unit verifies this is NULL. */
13115 cu->abbrev_table = NULL;
13116}
13117\f
72bf9492
DJ
13118/* Returns nonzero if TAG represents a type that we might generate a partial
13119 symbol for. */
13120
13121static int
13122is_type_tag_for_partial (int tag)
13123{
13124 switch (tag)
13125 {
13126#if 0
13127 /* Some types that would be reasonable to generate partial symbols for,
13128 that we don't at present. */
13129 case DW_TAG_array_type:
13130 case DW_TAG_file_type:
13131 case DW_TAG_ptr_to_member_type:
13132 case DW_TAG_set_type:
13133 case DW_TAG_string_type:
13134 case DW_TAG_subroutine_type:
13135#endif
13136 case DW_TAG_base_type:
13137 case DW_TAG_class_type:
680b30c7 13138 case DW_TAG_interface_type:
72bf9492
DJ
13139 case DW_TAG_enumeration_type:
13140 case DW_TAG_structure_type:
13141 case DW_TAG_subrange_type:
13142 case DW_TAG_typedef:
13143 case DW_TAG_union_type:
13144 return 1;
13145 default:
13146 return 0;
13147 }
13148}
13149
13150/* Load all DIEs that are interesting for partial symbols into memory. */
13151
13152static struct partial_die_info *
dee91e82
DE
13153load_partial_dies (const struct die_reader_specs *reader,
13154 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13155{
dee91e82 13156 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13157 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13158 struct partial_die_info *part_die;
13159 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13160 struct abbrev_info *abbrev;
13161 unsigned int bytes_read;
5afb4e99 13162 unsigned int load_all = 0;
72bf9492
DJ
13163 int nesting_level = 1;
13164
13165 parent_die = NULL;
13166 last_die = NULL;
13167
7adf1e79
DE
13168 gdb_assert (cu->per_cu != NULL);
13169 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13170 load_all = 1;
13171
72bf9492
DJ
13172 cu->partial_dies
13173 = htab_create_alloc_ex (cu->header.length / 12,
13174 partial_die_hash,
13175 partial_die_eq,
13176 NULL,
13177 &cu->comp_unit_obstack,
13178 hashtab_obstack_allocate,
13179 dummy_obstack_deallocate);
13180
13181 part_die = obstack_alloc (&cu->comp_unit_obstack,
13182 sizeof (struct partial_die_info));
13183
13184 while (1)
13185 {
13186 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13187
13188 /* A NULL abbrev means the end of a series of children. */
13189 if (abbrev == NULL)
13190 {
13191 if (--nesting_level == 0)
13192 {
13193 /* PART_DIE was probably the last thing allocated on the
13194 comp_unit_obstack, so we could call obstack_free
13195 here. We don't do that because the waste is small,
13196 and will be cleaned up when we're done with this
13197 compilation unit. This way, we're also more robust
13198 against other users of the comp_unit_obstack. */
13199 return first_die;
13200 }
13201 info_ptr += bytes_read;
13202 last_die = parent_die;
13203 parent_die = parent_die->die_parent;
13204 continue;
13205 }
13206
98bfdba5
PA
13207 /* Check for template arguments. We never save these; if
13208 they're seen, we just mark the parent, and go on our way. */
13209 if (parent_die != NULL
13210 && cu->language == language_cplus
13211 && (abbrev->tag == DW_TAG_template_type_param
13212 || abbrev->tag == DW_TAG_template_value_param))
13213 {
13214 parent_die->has_template_arguments = 1;
13215
13216 if (!load_all)
13217 {
13218 /* We don't need a partial DIE for the template argument. */
dee91e82 13219 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13220 continue;
13221 }
13222 }
13223
0d99eb77 13224 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13225 Skip their other children. */
13226 if (!load_all
13227 && cu->language == language_cplus
13228 && parent_die != NULL
13229 && parent_die->tag == DW_TAG_subprogram)
13230 {
dee91e82 13231 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13232 continue;
13233 }
13234
5afb4e99
DJ
13235 /* Check whether this DIE is interesting enough to save. Normally
13236 we would not be interested in members here, but there may be
13237 later variables referencing them via DW_AT_specification (for
13238 static members). */
13239 if (!load_all
13240 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13241 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13242 && abbrev->tag != DW_TAG_enumerator
13243 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13244 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13245 && abbrev->tag != DW_TAG_variable
5afb4e99 13246 && abbrev->tag != DW_TAG_namespace
f55ee35c 13247 && abbrev->tag != DW_TAG_module
95554aad
TT
13248 && abbrev->tag != DW_TAG_member
13249 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13250 {
13251 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13252 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13253 continue;
13254 }
13255
dee91e82
DE
13256 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13257 info_ptr);
72bf9492
DJ
13258
13259 /* This two-pass algorithm for processing partial symbols has a
13260 high cost in cache pressure. Thus, handle some simple cases
13261 here which cover the majority of C partial symbols. DIEs
13262 which neither have specification tags in them, nor could have
13263 specification tags elsewhere pointing at them, can simply be
13264 processed and discarded.
13265
13266 This segment is also optional; scan_partial_symbols and
13267 add_partial_symbol will handle these DIEs if we chain
13268 them in normally. When compilers which do not emit large
13269 quantities of duplicate debug information are more common,
13270 this code can probably be removed. */
13271
13272 /* Any complete simple types at the top level (pretty much all
13273 of them, for a language without namespaces), can be processed
13274 directly. */
13275 if (parent_die == NULL
13276 && part_die->has_specification == 0
13277 && part_die->is_declaration == 0
d8228535 13278 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13279 || part_die->tag == DW_TAG_base_type
13280 || part_die->tag == DW_TAG_subrange_type))
13281 {
13282 if (building_psymtab && part_die->name != NULL)
04a679b8 13283 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13284 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13285 &objfile->static_psymbols,
13286 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13287 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13288 continue;
13289 }
13290
d8228535
JK
13291 /* The exception for DW_TAG_typedef with has_children above is
13292 a workaround of GCC PR debug/47510. In the case of this complaint
13293 type_name_no_tag_or_error will error on such types later.
13294
13295 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13296 it could not find the child DIEs referenced later, this is checked
13297 above. In correct DWARF DW_TAG_typedef should have no children. */
13298
13299 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13300 complaint (&symfile_complaints,
13301 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13302 "- DIE at 0x%x [in module %s]"),
b64f50a1 13303 part_die->offset.sect_off, objfile->name);
d8228535 13304
72bf9492
DJ
13305 /* If we're at the second level, and we're an enumerator, and
13306 our parent has no specification (meaning possibly lives in a
13307 namespace elsewhere), then we can add the partial symbol now
13308 instead of queueing it. */
13309 if (part_die->tag == DW_TAG_enumerator
13310 && parent_die != NULL
13311 && parent_die->die_parent == NULL
13312 && parent_die->tag == DW_TAG_enumeration_type
13313 && parent_die->has_specification == 0)
13314 {
13315 if (part_die->name == NULL)
3e43a32a
MS
13316 complaint (&symfile_complaints,
13317 _("malformed enumerator DIE ignored"));
72bf9492 13318 else if (building_psymtab)
04a679b8 13319 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13320 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13321 (cu->language == language_cplus
13322 || cu->language == language_java)
bb5ed363
DE
13323 ? &objfile->global_psymbols
13324 : &objfile->static_psymbols,
13325 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13326
dee91e82 13327 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13328 continue;
13329 }
13330
13331 /* We'll save this DIE so link it in. */
13332 part_die->die_parent = parent_die;
13333 part_die->die_sibling = NULL;
13334 part_die->die_child = NULL;
13335
13336 if (last_die && last_die == parent_die)
13337 last_die->die_child = part_die;
13338 else if (last_die)
13339 last_die->die_sibling = part_die;
13340
13341 last_die = part_die;
13342
13343 if (first_die == NULL)
13344 first_die = part_die;
13345
13346 /* Maybe add the DIE to the hash table. Not all DIEs that we
13347 find interesting need to be in the hash table, because we
13348 also have the parent/sibling/child chains; only those that we
13349 might refer to by offset later during partial symbol reading.
13350
13351 For now this means things that might have be the target of a
13352 DW_AT_specification, DW_AT_abstract_origin, or
13353 DW_AT_extension. DW_AT_extension will refer only to
13354 namespaces; DW_AT_abstract_origin refers to functions (and
13355 many things under the function DIE, but we do not recurse
13356 into function DIEs during partial symbol reading) and
13357 possibly variables as well; DW_AT_specification refers to
13358 declarations. Declarations ought to have the DW_AT_declaration
13359 flag. It happens that GCC forgets to put it in sometimes, but
13360 only for functions, not for types.
13361
13362 Adding more things than necessary to the hash table is harmless
13363 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13364 wasted time in find_partial_die, when we reread the compilation
13365 unit with load_all_dies set. */
72bf9492 13366
5afb4e99 13367 if (load_all
72929c62 13368 || abbrev->tag == DW_TAG_constant
5afb4e99 13369 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13370 || abbrev->tag == DW_TAG_variable
13371 || abbrev->tag == DW_TAG_namespace
13372 || part_die->is_declaration)
13373 {
13374 void **slot;
13375
13376 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13377 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13378 *slot = part_die;
13379 }
13380
13381 part_die = obstack_alloc (&cu->comp_unit_obstack,
13382 sizeof (struct partial_die_info));
13383
13384 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13385 we have no reason to follow the children of structures; for other
98bfdba5
PA
13386 languages we have to, so that we can get at method physnames
13387 to infer fully qualified class names, for DW_AT_specification,
13388 and for C++ template arguments. For C++, we also look one level
13389 inside functions to find template arguments (if the name of the
13390 function does not already contain the template arguments).
bc30ff58
JB
13391
13392 For Ada, we need to scan the children of subprograms and lexical
13393 blocks as well because Ada allows the definition of nested
13394 entities that could be interesting for the debugger, such as
13395 nested subprograms for instance. */
72bf9492 13396 if (last_die->has_children
5afb4e99
DJ
13397 && (load_all
13398 || last_die->tag == DW_TAG_namespace
f55ee35c 13399 || last_die->tag == DW_TAG_module
72bf9492 13400 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13401 || (cu->language == language_cplus
13402 && last_die->tag == DW_TAG_subprogram
13403 && (last_die->name == NULL
13404 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13405 || (cu->language != language_c
13406 && (last_die->tag == DW_TAG_class_type
680b30c7 13407 || last_die->tag == DW_TAG_interface_type
72bf9492 13408 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13409 || last_die->tag == DW_TAG_union_type))
13410 || (cu->language == language_ada
13411 && (last_die->tag == DW_TAG_subprogram
13412 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13413 {
13414 nesting_level++;
13415 parent_die = last_die;
13416 continue;
13417 }
13418
13419 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13420 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13421
13422 /* Back to the top, do it again. */
13423 }
13424}
13425
c906108c
SS
13426/* Read a minimal amount of information into the minimal die structure. */
13427
fe1b8b76 13428static gdb_byte *
dee91e82
DE
13429read_partial_die (const struct die_reader_specs *reader,
13430 struct partial_die_info *part_die,
13431 struct abbrev_info *abbrev, unsigned int abbrev_len,
13432 gdb_byte *info_ptr)
c906108c 13433{
dee91e82 13434 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13435 struct objfile *objfile = cu->objfile;
dee91e82 13436 gdb_byte *buffer = reader->buffer;
fa238c03 13437 unsigned int i;
c906108c 13438 struct attribute attr;
c5aa993b 13439 int has_low_pc_attr = 0;
c906108c 13440 int has_high_pc_attr = 0;
91da1414 13441 int high_pc_relative = 0;
c906108c 13442
72bf9492 13443 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13444
b64f50a1 13445 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13446
13447 info_ptr += abbrev_len;
13448
13449 if (abbrev == NULL)
13450 return info_ptr;
13451
c906108c
SS
13452 part_die->tag = abbrev->tag;
13453 part_die->has_children = abbrev->has_children;
c906108c
SS
13454
13455 for (i = 0; i < abbrev->num_attrs; ++i)
13456 {
dee91e82 13457 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13458
13459 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13460 partial symbol table. */
c906108c
SS
13461 switch (attr.name)
13462 {
13463 case DW_AT_name:
71c25dea
TT
13464 switch (part_die->tag)
13465 {
13466 case DW_TAG_compile_unit:
95554aad 13467 case DW_TAG_partial_unit:
348e048f 13468 case DW_TAG_type_unit:
71c25dea
TT
13469 /* Compilation units have a DW_AT_name that is a filename, not
13470 a source language identifier. */
13471 case DW_TAG_enumeration_type:
13472 case DW_TAG_enumerator:
13473 /* These tags always have simple identifiers already; no need
13474 to canonicalize them. */
13475 part_die->name = DW_STRING (&attr);
13476 break;
13477 default:
13478 part_die->name
13479 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13480 &objfile->objfile_obstack);
71c25dea
TT
13481 break;
13482 }
c906108c 13483 break;
31ef98ae 13484 case DW_AT_linkage_name:
c906108c 13485 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13486 /* Note that both forms of linkage name might appear. We
13487 assume they will be the same, and we only store the last
13488 one we see. */
94af9270
KS
13489 if (cu->language == language_ada)
13490 part_die->name = DW_STRING (&attr);
abc72ce4 13491 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13492 break;
13493 case DW_AT_low_pc:
13494 has_low_pc_attr = 1;
13495 part_die->lowpc = DW_ADDR (&attr);
13496 break;
13497 case DW_AT_high_pc:
13498 has_high_pc_attr = 1;
3019eac3
DE
13499 if (attr.form == DW_FORM_addr
13500 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13501 part_die->highpc = DW_ADDR (&attr);
13502 else
13503 {
13504 high_pc_relative = 1;
13505 part_die->highpc = DW_UNSND (&attr);
13506 }
c906108c
SS
13507 break;
13508 case DW_AT_location:
0963b4bd 13509 /* Support the .debug_loc offsets. */
8e19ed76
PS
13510 if (attr_form_is_block (&attr))
13511 {
95554aad 13512 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13513 }
3690dd37 13514 else if (attr_form_is_section_offset (&attr))
8e19ed76 13515 {
4d3c2250 13516 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13517 }
13518 else
13519 {
4d3c2250
KB
13520 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13521 "partial symbol information");
8e19ed76 13522 }
c906108c 13523 break;
c906108c
SS
13524 case DW_AT_external:
13525 part_die->is_external = DW_UNSND (&attr);
13526 break;
13527 case DW_AT_declaration:
13528 part_die->is_declaration = DW_UNSND (&attr);
13529 break;
13530 case DW_AT_type:
13531 part_die->has_type = 1;
13532 break;
13533 case DW_AT_abstract_origin:
13534 case DW_AT_specification:
72bf9492
DJ
13535 case DW_AT_extension:
13536 part_die->has_specification = 1;
c764a876 13537 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13538 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13539 || cu->per_cu->is_dwz);
c906108c
SS
13540 break;
13541 case DW_AT_sibling:
13542 /* Ignore absolute siblings, they might point outside of
13543 the current compile unit. */
13544 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13545 complaint (&symfile_complaints,
13546 _("ignoring absolute DW_AT_sibling"));
c906108c 13547 else
b64f50a1 13548 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13549 break;
fa4028e9
JB
13550 case DW_AT_byte_size:
13551 part_die->has_byte_size = 1;
13552 break;
68511cec
CES
13553 case DW_AT_calling_convention:
13554 /* DWARF doesn't provide a way to identify a program's source-level
13555 entry point. DW_AT_calling_convention attributes are only meant
13556 to describe functions' calling conventions.
13557
13558 However, because it's a necessary piece of information in
13559 Fortran, and because DW_CC_program is the only piece of debugging
13560 information whose definition refers to a 'main program' at all,
13561 several compilers have begun marking Fortran main programs with
13562 DW_CC_program --- even when those functions use the standard
13563 calling conventions.
13564
13565 So until DWARF specifies a way to provide this information and
13566 compilers pick up the new representation, we'll support this
13567 practice. */
13568 if (DW_UNSND (&attr) == DW_CC_program
13569 && cu->language == language_fortran)
01f8c46d
JK
13570 {
13571 set_main_name (part_die->name);
13572
13573 /* As this DIE has a static linkage the name would be difficult
13574 to look up later. */
13575 language_of_main = language_fortran;
13576 }
68511cec 13577 break;
481860b3
GB
13578 case DW_AT_inline:
13579 if (DW_UNSND (&attr) == DW_INL_inlined
13580 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13581 part_die->may_be_inlined = 1;
13582 break;
95554aad
TT
13583
13584 case DW_AT_import:
13585 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13586 {
13587 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13588 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13589 || cu->per_cu->is_dwz);
13590 }
95554aad
TT
13591 break;
13592
c906108c
SS
13593 default:
13594 break;
13595 }
13596 }
13597
91da1414
MW
13598 if (high_pc_relative)
13599 part_die->highpc += part_die->lowpc;
13600
9373cf26
JK
13601 if (has_low_pc_attr && has_high_pc_attr)
13602 {
13603 /* When using the GNU linker, .gnu.linkonce. sections are used to
13604 eliminate duplicate copies of functions and vtables and such.
13605 The linker will arbitrarily choose one and discard the others.
13606 The AT_*_pc values for such functions refer to local labels in
13607 these sections. If the section from that file was discarded, the
13608 labels are not in the output, so the relocs get a value of 0.
13609 If this is a discarded function, mark the pc bounds as invalid,
13610 so that GDB will ignore it. */
13611 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13612 {
bb5ed363 13613 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13614
13615 complaint (&symfile_complaints,
13616 _("DW_AT_low_pc %s is zero "
13617 "for DIE at 0x%x [in module %s]"),
13618 paddress (gdbarch, part_die->lowpc),
b64f50a1 13619 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13620 }
13621 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13622 else if (part_die->lowpc >= part_die->highpc)
13623 {
bb5ed363 13624 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13625
13626 complaint (&symfile_complaints,
13627 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13628 "for DIE at 0x%x [in module %s]"),
13629 paddress (gdbarch, part_die->lowpc),
13630 paddress (gdbarch, part_die->highpc),
b64f50a1 13631 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13632 }
13633 else
13634 part_die->has_pc_info = 1;
13635 }
85cbf3d3 13636
c906108c
SS
13637 return info_ptr;
13638}
13639
72bf9492
DJ
13640/* Find a cached partial DIE at OFFSET in CU. */
13641
13642static struct partial_die_info *
b64f50a1 13643find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13644{
13645 struct partial_die_info *lookup_die = NULL;
13646 struct partial_die_info part_die;
13647
13648 part_die.offset = offset;
b64f50a1
JK
13649 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13650 offset.sect_off);
72bf9492 13651
72bf9492
DJ
13652 return lookup_die;
13653}
13654
348e048f
DE
13655/* Find a partial DIE at OFFSET, which may or may not be in CU,
13656 except in the case of .debug_types DIEs which do not reference
13657 outside their CU (they do however referencing other types via
55f1336d 13658 DW_FORM_ref_sig8). */
72bf9492
DJ
13659
13660static struct partial_die_info *
36586728 13661find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13662{
bb5ed363 13663 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13664 struct dwarf2_per_cu_data *per_cu = NULL;
13665 struct partial_die_info *pd = NULL;
72bf9492 13666
36586728
TT
13667 if (offset_in_dwz == cu->per_cu->is_dwz
13668 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13669 {
13670 pd = find_partial_die_in_comp_unit (offset, cu);
13671 if (pd != NULL)
13672 return pd;
0d99eb77
DE
13673 /* We missed recording what we needed.
13674 Load all dies and try again. */
13675 per_cu = cu->per_cu;
5afb4e99 13676 }
0d99eb77
DE
13677 else
13678 {
13679 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13680 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13681 {
13682 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13683 " external reference to offset 0x%lx [in module %s].\n"),
13684 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13685 bfd_get_filename (objfile->obfd));
13686 }
36586728
TT
13687 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13688 objfile);
72bf9492 13689
0d99eb77
DE
13690 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13691 load_partial_comp_unit (per_cu);
ae038cb0 13692
0d99eb77
DE
13693 per_cu->cu->last_used = 0;
13694 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13695 }
5afb4e99 13696
dee91e82
DE
13697 /* If we didn't find it, and not all dies have been loaded,
13698 load them all and try again. */
13699
5afb4e99
DJ
13700 if (pd == NULL && per_cu->load_all_dies == 0)
13701 {
5afb4e99 13702 per_cu->load_all_dies = 1;
fd820528
DE
13703
13704 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13705 THIS_CU->cu may already be in use. So we can't just free it and
13706 replace its DIEs with the ones we read in. Instead, we leave those
13707 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13708 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13709 set. */
dee91e82 13710 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13711
13712 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13713 }
13714
13715 if (pd == NULL)
13716 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13717 _("could not find partial DIE 0x%x "
13718 "in cache [from module %s]\n"),
b64f50a1 13719 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13720 return pd;
72bf9492
DJ
13721}
13722
abc72ce4
DE
13723/* See if we can figure out if the class lives in a namespace. We do
13724 this by looking for a member function; its demangled name will
13725 contain namespace info, if there is any. */
13726
13727static void
13728guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13729 struct dwarf2_cu *cu)
13730{
13731 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13732 what template types look like, because the demangler
13733 frequently doesn't give the same name as the debug info. We
13734 could fix this by only using the demangled name to get the
13735 prefix (but see comment in read_structure_type). */
13736
13737 struct partial_die_info *real_pdi;
13738 struct partial_die_info *child_pdi;
13739
13740 /* If this DIE (this DIE's specification, if any) has a parent, then
13741 we should not do this. We'll prepend the parent's fully qualified
13742 name when we create the partial symbol. */
13743
13744 real_pdi = struct_pdi;
13745 while (real_pdi->has_specification)
36586728
TT
13746 real_pdi = find_partial_die (real_pdi->spec_offset,
13747 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13748
13749 if (real_pdi->die_parent != NULL)
13750 return;
13751
13752 for (child_pdi = struct_pdi->die_child;
13753 child_pdi != NULL;
13754 child_pdi = child_pdi->die_sibling)
13755 {
13756 if (child_pdi->tag == DW_TAG_subprogram
13757 && child_pdi->linkage_name != NULL)
13758 {
13759 char *actual_class_name
13760 = language_class_name_from_physname (cu->language_defn,
13761 child_pdi->linkage_name);
13762 if (actual_class_name != NULL)
13763 {
13764 struct_pdi->name
13765 = obsavestring (actual_class_name,
13766 strlen (actual_class_name),
13767 &cu->objfile->objfile_obstack);
13768 xfree (actual_class_name);
13769 }
13770 break;
13771 }
13772 }
13773}
13774
72bf9492
DJ
13775/* Adjust PART_DIE before generating a symbol for it. This function
13776 may set the is_external flag or change the DIE's name. */
13777
13778static void
13779fixup_partial_die (struct partial_die_info *part_die,
13780 struct dwarf2_cu *cu)
13781{
abc72ce4
DE
13782 /* Once we've fixed up a die, there's no point in doing so again.
13783 This also avoids a memory leak if we were to call
13784 guess_partial_die_structure_name multiple times. */
13785 if (part_die->fixup_called)
13786 return;
13787
72bf9492
DJ
13788 /* If we found a reference attribute and the DIE has no name, try
13789 to find a name in the referred to DIE. */
13790
13791 if (part_die->name == NULL && part_die->has_specification)
13792 {
13793 struct partial_die_info *spec_die;
72bf9492 13794
36586728
TT
13795 spec_die = find_partial_die (part_die->spec_offset,
13796 part_die->spec_is_dwz, cu);
72bf9492 13797
10b3939b 13798 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13799
13800 if (spec_die->name)
13801 {
13802 part_die->name = spec_die->name;
13803
13804 /* Copy DW_AT_external attribute if it is set. */
13805 if (spec_die->is_external)
13806 part_die->is_external = spec_die->is_external;
13807 }
13808 }
13809
13810 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13811
13812 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13813 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13814
abc72ce4
DE
13815 /* If there is no parent die to provide a namespace, and there are
13816 children, see if we can determine the namespace from their linkage
122d1940 13817 name. */
abc72ce4 13818 if (cu->language == language_cplus
8b70b953 13819 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13820 && part_die->die_parent == NULL
13821 && part_die->has_children
13822 && (part_die->tag == DW_TAG_class_type
13823 || part_die->tag == DW_TAG_structure_type
13824 || part_die->tag == DW_TAG_union_type))
13825 guess_partial_die_structure_name (part_die, cu);
13826
53832f31
TT
13827 /* GCC might emit a nameless struct or union that has a linkage
13828 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13829 if (part_die->name == NULL
96408a79
SA
13830 && (part_die->tag == DW_TAG_class_type
13831 || part_die->tag == DW_TAG_interface_type
13832 || part_die->tag == DW_TAG_structure_type
13833 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13834 && part_die->linkage_name != NULL)
13835 {
13836 char *demangled;
13837
13838 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13839 if (demangled)
13840 {
96408a79
SA
13841 const char *base;
13842
13843 /* Strip any leading namespaces/classes, keep only the base name.
13844 DW_AT_name for named DIEs does not contain the prefixes. */
13845 base = strrchr (demangled, ':');
13846 if (base && base > demangled && base[-1] == ':')
13847 base++;
13848 else
13849 base = demangled;
13850
13851 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13852 &cu->objfile->objfile_obstack);
13853 xfree (demangled);
13854 }
13855 }
13856
abc72ce4 13857 part_die->fixup_called = 1;
72bf9492
DJ
13858}
13859
a8329558 13860/* Read an attribute value described by an attribute form. */
c906108c 13861
fe1b8b76 13862static gdb_byte *
dee91e82
DE
13863read_attribute_value (const struct die_reader_specs *reader,
13864 struct attribute *attr, unsigned form,
13865 gdb_byte *info_ptr)
c906108c 13866{
dee91e82
DE
13867 struct dwarf2_cu *cu = reader->cu;
13868 bfd *abfd = reader->abfd;
e7c27a73 13869 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13870 unsigned int bytes_read;
13871 struct dwarf_block *blk;
13872
a8329558
KW
13873 attr->form = form;
13874 switch (form)
c906108c 13875 {
c906108c 13876 case DW_FORM_ref_addr:
ae411497 13877 if (cu->header.version == 2)
4568ecf9 13878 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13879 else
4568ecf9
DE
13880 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13881 &cu->header, &bytes_read);
ae411497
TT
13882 info_ptr += bytes_read;
13883 break;
36586728
TT
13884 case DW_FORM_GNU_ref_alt:
13885 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13886 info_ptr += bytes_read;
13887 break;
ae411497 13888 case DW_FORM_addr:
e7c27a73 13889 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13890 info_ptr += bytes_read;
c906108c
SS
13891 break;
13892 case DW_FORM_block2:
7b5a2f43 13893 blk = dwarf_alloc_block (cu);
c906108c
SS
13894 blk->size = read_2_bytes (abfd, info_ptr);
13895 info_ptr += 2;
13896 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13897 info_ptr += blk->size;
13898 DW_BLOCK (attr) = blk;
13899 break;
13900 case DW_FORM_block4:
7b5a2f43 13901 blk = dwarf_alloc_block (cu);
c906108c
SS
13902 blk->size = read_4_bytes (abfd, info_ptr);
13903 info_ptr += 4;
13904 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13905 info_ptr += blk->size;
13906 DW_BLOCK (attr) = blk;
13907 break;
13908 case DW_FORM_data2:
13909 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13910 info_ptr += 2;
13911 break;
13912 case DW_FORM_data4:
13913 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13914 info_ptr += 4;
13915 break;
13916 case DW_FORM_data8:
13917 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13918 info_ptr += 8;
13919 break;
2dc7f7b3
TT
13920 case DW_FORM_sec_offset:
13921 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13922 info_ptr += bytes_read;
13923 break;
c906108c 13924 case DW_FORM_string:
9b1c24c8 13925 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13926 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13927 info_ptr += bytes_read;
13928 break;
4bdf3d34 13929 case DW_FORM_strp:
36586728
TT
13930 if (!cu->per_cu->is_dwz)
13931 {
13932 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13933 &bytes_read);
13934 DW_STRING_IS_CANONICAL (attr) = 0;
13935 info_ptr += bytes_read;
13936 break;
13937 }
13938 /* FALLTHROUGH */
13939 case DW_FORM_GNU_strp_alt:
13940 {
13941 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13942 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13943 &bytes_read);
13944
13945 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13946 DW_STRING_IS_CANONICAL (attr) = 0;
13947 info_ptr += bytes_read;
13948 }
4bdf3d34 13949 break;
2dc7f7b3 13950 case DW_FORM_exprloc:
c906108c 13951 case DW_FORM_block:
7b5a2f43 13952 blk = dwarf_alloc_block (cu);
c906108c
SS
13953 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13954 info_ptr += bytes_read;
13955 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13956 info_ptr += blk->size;
13957 DW_BLOCK (attr) = blk;
13958 break;
13959 case DW_FORM_block1:
7b5a2f43 13960 blk = dwarf_alloc_block (cu);
c906108c
SS
13961 blk->size = read_1_byte (abfd, info_ptr);
13962 info_ptr += 1;
13963 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13964 info_ptr += blk->size;
13965 DW_BLOCK (attr) = blk;
13966 break;
13967 case DW_FORM_data1:
13968 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13969 info_ptr += 1;
13970 break;
13971 case DW_FORM_flag:
13972 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13973 info_ptr += 1;
13974 break;
2dc7f7b3
TT
13975 case DW_FORM_flag_present:
13976 DW_UNSND (attr) = 1;
13977 break;
c906108c
SS
13978 case DW_FORM_sdata:
13979 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13980 info_ptr += bytes_read;
13981 break;
13982 case DW_FORM_udata:
13983 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13984 info_ptr += bytes_read;
13985 break;
13986 case DW_FORM_ref1:
4568ecf9
DE
13987 DW_UNSND (attr) = (cu->header.offset.sect_off
13988 + read_1_byte (abfd, info_ptr));
c906108c
SS
13989 info_ptr += 1;
13990 break;
13991 case DW_FORM_ref2:
4568ecf9
DE
13992 DW_UNSND (attr) = (cu->header.offset.sect_off
13993 + read_2_bytes (abfd, info_ptr));
c906108c
SS
13994 info_ptr += 2;
13995 break;
13996 case DW_FORM_ref4:
4568ecf9
DE
13997 DW_UNSND (attr) = (cu->header.offset.sect_off
13998 + read_4_bytes (abfd, info_ptr));
c906108c
SS
13999 info_ptr += 4;
14000 break;
613e1657 14001 case DW_FORM_ref8:
4568ecf9
DE
14002 DW_UNSND (attr) = (cu->header.offset.sect_off
14003 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14004 info_ptr += 8;
14005 break;
55f1336d 14006 case DW_FORM_ref_sig8:
348e048f
DE
14007 /* Convert the signature to something we can record in DW_UNSND
14008 for later lookup.
14009 NOTE: This is NULL if the type wasn't found. */
14010 DW_SIGNATURED_TYPE (attr) =
e319fa28 14011 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14012 info_ptr += 8;
14013 break;
c906108c 14014 case DW_FORM_ref_udata:
4568ecf9
DE
14015 DW_UNSND (attr) = (cu->header.offset.sect_off
14016 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14017 info_ptr += bytes_read;
14018 break;
c906108c 14019 case DW_FORM_indirect:
a8329558
KW
14020 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14021 info_ptr += bytes_read;
dee91e82 14022 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14023 break;
3019eac3
DE
14024 case DW_FORM_GNU_addr_index:
14025 if (reader->dwo_file == NULL)
14026 {
14027 /* For now flag a hard error.
14028 Later we can turn this into a complaint. */
14029 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14030 dwarf_form_name (form),
14031 bfd_get_filename (abfd));
14032 }
14033 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14034 info_ptr += bytes_read;
14035 break;
14036 case DW_FORM_GNU_str_index:
14037 if (reader->dwo_file == NULL)
14038 {
14039 /* For now flag a hard error.
14040 Later we can turn this into a complaint if warranted. */
14041 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14042 dwarf_form_name (form),
14043 bfd_get_filename (abfd));
14044 }
14045 {
14046 ULONGEST str_index =
14047 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14048
14049 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14050 DW_STRING_IS_CANONICAL (attr) = 0;
14051 info_ptr += bytes_read;
14052 }
14053 break;
c906108c 14054 default:
8a3fe4f8 14055 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14056 dwarf_form_name (form),
14057 bfd_get_filename (abfd));
c906108c 14058 }
28e94949 14059
36586728
TT
14060 /* Super hack. */
14061 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14062 attr->form = DW_FORM_GNU_ref_alt;
14063
28e94949
JB
14064 /* We have seen instances where the compiler tried to emit a byte
14065 size attribute of -1 which ended up being encoded as an unsigned
14066 0xffffffff. Although 0xffffffff is technically a valid size value,
14067 an object of this size seems pretty unlikely so we can relatively
14068 safely treat these cases as if the size attribute was invalid and
14069 treat them as zero by default. */
14070 if (attr->name == DW_AT_byte_size
14071 && form == DW_FORM_data4
14072 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14073 {
14074 complaint
14075 (&symfile_complaints,
43bbcdc2
PH
14076 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14077 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14078 DW_UNSND (attr) = 0;
14079 }
28e94949 14080
c906108c
SS
14081 return info_ptr;
14082}
14083
a8329558
KW
14084/* Read an attribute described by an abbreviated attribute. */
14085
fe1b8b76 14086static gdb_byte *
dee91e82
DE
14087read_attribute (const struct die_reader_specs *reader,
14088 struct attribute *attr, struct attr_abbrev *abbrev,
14089 gdb_byte *info_ptr)
a8329558
KW
14090{
14091 attr->name = abbrev->name;
dee91e82 14092 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14093}
14094
0963b4bd 14095/* Read dwarf information from a buffer. */
c906108c
SS
14096
14097static unsigned int
a1855c1d 14098read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14099{
fe1b8b76 14100 return bfd_get_8 (abfd, buf);
c906108c
SS
14101}
14102
14103static int
a1855c1d 14104read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14105{
fe1b8b76 14106 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14107}
14108
14109static unsigned int
a1855c1d 14110read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14111{
fe1b8b76 14112 return bfd_get_16 (abfd, buf);
c906108c
SS
14113}
14114
21ae7a4d 14115static int
a1855c1d 14116read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14117{
14118 return bfd_get_signed_16 (abfd, buf);
14119}
14120
c906108c 14121static unsigned int
a1855c1d 14122read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14123{
fe1b8b76 14124 return bfd_get_32 (abfd, buf);
c906108c
SS
14125}
14126
21ae7a4d 14127static int
a1855c1d 14128read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14129{
14130 return bfd_get_signed_32 (abfd, buf);
14131}
14132
93311388 14133static ULONGEST
a1855c1d 14134read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14135{
fe1b8b76 14136 return bfd_get_64 (abfd, buf);
c906108c
SS
14137}
14138
14139static CORE_ADDR
fe1b8b76 14140read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14141 unsigned int *bytes_read)
c906108c 14142{
e7c27a73 14143 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14144 CORE_ADDR retval = 0;
14145
107d2387 14146 if (cu_header->signed_addr_p)
c906108c 14147 {
107d2387
AC
14148 switch (cu_header->addr_size)
14149 {
14150 case 2:
fe1b8b76 14151 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14152 break;
14153 case 4:
fe1b8b76 14154 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14155 break;
14156 case 8:
fe1b8b76 14157 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14158 break;
14159 default:
8e65ff28 14160 internal_error (__FILE__, __LINE__,
e2e0b3e5 14161 _("read_address: bad switch, signed [in module %s]"),
659b0389 14162 bfd_get_filename (abfd));
107d2387
AC
14163 }
14164 }
14165 else
14166 {
14167 switch (cu_header->addr_size)
14168 {
14169 case 2:
fe1b8b76 14170 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14171 break;
14172 case 4:
fe1b8b76 14173 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14174 break;
14175 case 8:
fe1b8b76 14176 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14177 break;
14178 default:
8e65ff28 14179 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14180 _("read_address: bad switch, "
14181 "unsigned [in module %s]"),
659b0389 14182 bfd_get_filename (abfd));
107d2387 14183 }
c906108c 14184 }
64367e0a 14185
107d2387
AC
14186 *bytes_read = cu_header->addr_size;
14187 return retval;
c906108c
SS
14188}
14189
f7ef9339 14190/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14191 specification allows the initial length to take up either 4 bytes
14192 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14193 bytes describe the length and all offsets will be 8 bytes in length
14194 instead of 4.
14195
f7ef9339
KB
14196 An older, non-standard 64-bit format is also handled by this
14197 function. The older format in question stores the initial length
14198 as an 8-byte quantity without an escape value. Lengths greater
14199 than 2^32 aren't very common which means that the initial 4 bytes
14200 is almost always zero. Since a length value of zero doesn't make
14201 sense for the 32-bit format, this initial zero can be considered to
14202 be an escape value which indicates the presence of the older 64-bit
14203 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14204 greater than 4GB. If it becomes necessary to handle lengths
14205 somewhat larger than 4GB, we could allow other small values (such
14206 as the non-sensical values of 1, 2, and 3) to also be used as
14207 escape values indicating the presence of the old format.
f7ef9339 14208
917c78fc
MK
14209 The value returned via bytes_read should be used to increment the
14210 relevant pointer after calling read_initial_length().
c764a876 14211
613e1657
KB
14212 [ Note: read_initial_length() and read_offset() are based on the
14213 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14214 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14215 from:
14216
f7ef9339 14217 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14218
613e1657
KB
14219 This document is only a draft and is subject to change. (So beware.)
14220
f7ef9339 14221 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14222 determined empirically by examining 64-bit ELF files produced by
14223 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14224
14225 - Kevin, July 16, 2002
613e1657
KB
14226 ] */
14227
14228static LONGEST
c764a876 14229read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14230{
fe1b8b76 14231 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14232
dd373385 14233 if (length == 0xffffffff)
613e1657 14234 {
fe1b8b76 14235 length = bfd_get_64 (abfd, buf + 4);
613e1657 14236 *bytes_read = 12;
613e1657 14237 }
dd373385 14238 else if (length == 0)
f7ef9339 14239 {
dd373385 14240 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14241 length = bfd_get_64 (abfd, buf);
f7ef9339 14242 *bytes_read = 8;
f7ef9339 14243 }
613e1657
KB
14244 else
14245 {
14246 *bytes_read = 4;
613e1657
KB
14247 }
14248
c764a876
DE
14249 return length;
14250}
dd373385 14251
c764a876
DE
14252/* Cover function for read_initial_length.
14253 Returns the length of the object at BUF, and stores the size of the
14254 initial length in *BYTES_READ and stores the size that offsets will be in
14255 *OFFSET_SIZE.
14256 If the initial length size is not equivalent to that specified in
14257 CU_HEADER then issue a complaint.
14258 This is useful when reading non-comp-unit headers. */
dd373385 14259
c764a876
DE
14260static LONGEST
14261read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14262 const struct comp_unit_head *cu_header,
14263 unsigned int *bytes_read,
14264 unsigned int *offset_size)
14265{
14266 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14267
14268 gdb_assert (cu_header->initial_length_size == 4
14269 || cu_header->initial_length_size == 8
14270 || cu_header->initial_length_size == 12);
14271
14272 if (cu_header->initial_length_size != *bytes_read)
14273 complaint (&symfile_complaints,
14274 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14275
c764a876 14276 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14277 return length;
613e1657
KB
14278}
14279
14280/* Read an offset from the data stream. The size of the offset is
917c78fc 14281 given by cu_header->offset_size. */
613e1657
KB
14282
14283static LONGEST
fe1b8b76 14284read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14285 unsigned int *bytes_read)
c764a876
DE
14286{
14287 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14288
c764a876
DE
14289 *bytes_read = cu_header->offset_size;
14290 return offset;
14291}
14292
14293/* Read an offset from the data stream. */
14294
14295static LONGEST
14296read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14297{
14298 LONGEST retval = 0;
14299
c764a876 14300 switch (offset_size)
613e1657
KB
14301 {
14302 case 4:
fe1b8b76 14303 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14304 break;
14305 case 8:
fe1b8b76 14306 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14307 break;
14308 default:
8e65ff28 14309 internal_error (__FILE__, __LINE__,
c764a876 14310 _("read_offset_1: bad switch [in module %s]"),
659b0389 14311 bfd_get_filename (abfd));
613e1657
KB
14312 }
14313
917c78fc 14314 return retval;
613e1657
KB
14315}
14316
fe1b8b76
JB
14317static gdb_byte *
14318read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14319{
14320 /* If the size of a host char is 8 bits, we can return a pointer
14321 to the buffer, otherwise we have to copy the data to a buffer
14322 allocated on the temporary obstack. */
4bdf3d34 14323 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14324 return buf;
c906108c
SS
14325}
14326
14327static char *
9b1c24c8 14328read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14329{
14330 /* If the size of a host char is 8 bits, we can return a pointer
14331 to the string, otherwise we have to copy the string to a buffer
14332 allocated on the temporary obstack. */
4bdf3d34 14333 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14334 if (*buf == '\0')
14335 {
14336 *bytes_read_ptr = 1;
14337 return NULL;
14338 }
fe1b8b76
JB
14339 *bytes_read_ptr = strlen ((char *) buf) + 1;
14340 return (char *) buf;
4bdf3d34
JJ
14341}
14342
14343static char *
cf2c3c16 14344read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14345{
be391dca 14346 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14347 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14348 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14349 bfd_get_filename (abfd));
dce234bc 14350 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14351 error (_("DW_FORM_strp pointing outside of "
14352 ".debug_str section [in module %s]"),
14353 bfd_get_filename (abfd));
4bdf3d34 14354 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14355 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14356 return NULL;
dce234bc 14357 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14358}
14359
36586728
TT
14360/* Read a string at offset STR_OFFSET in the .debug_str section from
14361 the .dwz file DWZ. Throw an error if the offset is too large. If
14362 the string consists of a single NUL byte, return NULL; otherwise
14363 return a pointer to the string. */
14364
14365static char *
14366read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14367{
14368 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14369
14370 if (dwz->str.buffer == NULL)
14371 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14372 "section [in module %s]"),
14373 bfd_get_filename (dwz->dwz_bfd));
14374 if (str_offset >= dwz->str.size)
14375 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14376 ".debug_str section [in module %s]"),
14377 bfd_get_filename (dwz->dwz_bfd));
14378 gdb_assert (HOST_CHAR_BIT == 8);
14379 if (dwz->str.buffer[str_offset] == '\0')
14380 return NULL;
14381 return (char *) (dwz->str.buffer + str_offset);
14382}
14383
cf2c3c16
TT
14384static char *
14385read_indirect_string (bfd *abfd, gdb_byte *buf,
14386 const struct comp_unit_head *cu_header,
14387 unsigned int *bytes_read_ptr)
14388{
14389 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14390
14391 return read_indirect_string_at_offset (abfd, str_offset);
14392}
14393
12df843f 14394static ULONGEST
fe1b8b76 14395read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14396{
12df843f 14397 ULONGEST result;
ce5d95e1 14398 unsigned int num_read;
c906108c
SS
14399 int i, shift;
14400 unsigned char byte;
14401
14402 result = 0;
14403 shift = 0;
14404 num_read = 0;
14405 i = 0;
14406 while (1)
14407 {
fe1b8b76 14408 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14409 buf++;
14410 num_read++;
12df843f 14411 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14412 if ((byte & 128) == 0)
14413 {
14414 break;
14415 }
14416 shift += 7;
14417 }
14418 *bytes_read_ptr = num_read;
14419 return result;
14420}
14421
12df843f 14422static LONGEST
fe1b8b76 14423read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14424{
12df843f 14425 LONGEST result;
77e0b926 14426 int i, shift, num_read;
c906108c
SS
14427 unsigned char byte;
14428
14429 result = 0;
14430 shift = 0;
c906108c
SS
14431 num_read = 0;
14432 i = 0;
14433 while (1)
14434 {
fe1b8b76 14435 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14436 buf++;
14437 num_read++;
12df843f 14438 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14439 shift += 7;
14440 if ((byte & 128) == 0)
14441 {
14442 break;
14443 }
14444 }
77e0b926 14445 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14446 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14447 *bytes_read_ptr = num_read;
14448 return result;
14449}
14450
3019eac3
DE
14451/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14452 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14453 ADDR_SIZE is the size of addresses from the CU header. */
14454
14455static CORE_ADDR
14456read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14457{
14458 struct objfile *objfile = dwarf2_per_objfile->objfile;
14459 bfd *abfd = objfile->obfd;
14460 const gdb_byte *info_ptr;
14461
14462 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14463 if (dwarf2_per_objfile->addr.buffer == NULL)
14464 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14465 objfile->name);
14466 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14467 error (_("DW_FORM_addr_index pointing outside of "
14468 ".debug_addr section [in module %s]"),
14469 objfile->name);
14470 info_ptr = (dwarf2_per_objfile->addr.buffer
14471 + addr_base + addr_index * addr_size);
14472 if (addr_size == 4)
14473 return bfd_get_32 (abfd, info_ptr);
14474 else
14475 return bfd_get_64 (abfd, info_ptr);
14476}
14477
14478/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14479
14480static CORE_ADDR
14481read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14482{
14483 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14484}
14485
14486/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14487
14488static CORE_ADDR
14489read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14490 unsigned int *bytes_read)
14491{
14492 bfd *abfd = cu->objfile->obfd;
14493 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14494
14495 return read_addr_index (cu, addr_index);
14496}
14497
14498/* Data structure to pass results from dwarf2_read_addr_index_reader
14499 back to dwarf2_read_addr_index. */
14500
14501struct dwarf2_read_addr_index_data
14502{
14503 ULONGEST addr_base;
14504 int addr_size;
14505};
14506
14507/* die_reader_func for dwarf2_read_addr_index. */
14508
14509static void
14510dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14511 gdb_byte *info_ptr,
14512 struct die_info *comp_unit_die,
14513 int has_children,
14514 void *data)
14515{
14516 struct dwarf2_cu *cu = reader->cu;
14517 struct dwarf2_read_addr_index_data *aidata =
14518 (struct dwarf2_read_addr_index_data *) data;
14519
14520 aidata->addr_base = cu->addr_base;
14521 aidata->addr_size = cu->header.addr_size;
14522}
14523
14524/* Given an index in .debug_addr, fetch the value.
14525 NOTE: This can be called during dwarf expression evaluation,
14526 long after the debug information has been read, and thus per_cu->cu
14527 may no longer exist. */
14528
14529CORE_ADDR
14530dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14531 unsigned int addr_index)
14532{
14533 struct objfile *objfile = per_cu->objfile;
14534 struct dwarf2_cu *cu = per_cu->cu;
14535 ULONGEST addr_base;
14536 int addr_size;
14537
14538 /* This is intended to be called from outside this file. */
14539 dw2_setup (objfile);
14540
14541 /* We need addr_base and addr_size.
14542 If we don't have PER_CU->cu, we have to get it.
14543 Nasty, but the alternative is storing the needed info in PER_CU,
14544 which at this point doesn't seem justified: it's not clear how frequently
14545 it would get used and it would increase the size of every PER_CU.
14546 Entry points like dwarf2_per_cu_addr_size do a similar thing
14547 so we're not in uncharted territory here.
14548 Alas we need to be a bit more complicated as addr_base is contained
14549 in the DIE.
14550
14551 We don't need to read the entire CU(/TU).
14552 We just need the header and top level die.
a1b64ce1 14553
3019eac3 14554 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14555 For now we skip this optimization. */
3019eac3
DE
14556
14557 if (cu != NULL)
14558 {
14559 addr_base = cu->addr_base;
14560 addr_size = cu->header.addr_size;
14561 }
14562 else
14563 {
14564 struct dwarf2_read_addr_index_data aidata;
14565
a1b64ce1
DE
14566 /* Note: We can't use init_cutu_and_read_dies_simple here,
14567 we need addr_base. */
14568 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14569 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14570 addr_base = aidata.addr_base;
14571 addr_size = aidata.addr_size;
14572 }
14573
14574 return read_addr_index_1 (addr_index, addr_base, addr_size);
14575}
14576
14577/* Given a DW_AT_str_index, fetch the string. */
14578
14579static char *
14580read_str_index (const struct die_reader_specs *reader,
14581 struct dwarf2_cu *cu, ULONGEST str_index)
14582{
14583 struct objfile *objfile = dwarf2_per_objfile->objfile;
14584 const char *dwo_name = objfile->name;
14585 bfd *abfd = objfile->obfd;
14586 struct dwo_sections *sections = &reader->dwo_file->sections;
14587 gdb_byte *info_ptr;
14588 ULONGEST str_offset;
14589
14590 dwarf2_read_section (objfile, &sections->str);
14591 dwarf2_read_section (objfile, &sections->str_offsets);
14592 if (sections->str.buffer == NULL)
14593 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14594 " in CU at offset 0x%lx [in module %s]"),
14595 (long) cu->header.offset.sect_off, dwo_name);
14596 if (sections->str_offsets.buffer == NULL)
14597 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14598 " in CU at offset 0x%lx [in module %s]"),
14599 (long) cu->header.offset.sect_off, dwo_name);
14600 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14601 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14602 " section in CU at offset 0x%lx [in module %s]"),
14603 (long) cu->header.offset.sect_off, dwo_name);
14604 info_ptr = (sections->str_offsets.buffer
14605 + str_index * cu->header.offset_size);
14606 if (cu->header.offset_size == 4)
14607 str_offset = bfd_get_32 (abfd, info_ptr);
14608 else
14609 str_offset = bfd_get_64 (abfd, info_ptr);
14610 if (str_offset >= sections->str.size)
14611 error (_("Offset from DW_FORM_str_index pointing outside of"
14612 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14613 (long) cu->header.offset.sect_off, dwo_name);
14614 return (char *) (sections->str.buffer + str_offset);
14615}
14616
3019eac3
DE
14617/* Return the length of an LEB128 number in BUF. */
14618
14619static int
14620leb128_size (const gdb_byte *buf)
14621{
14622 const gdb_byte *begin = buf;
14623 gdb_byte byte;
14624
14625 while (1)
14626 {
14627 byte = *buf++;
14628 if ((byte & 128) == 0)
14629 return buf - begin;
14630 }
14631}
14632
c906108c 14633static void
e142c38c 14634set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14635{
14636 switch (lang)
14637 {
14638 case DW_LANG_C89:
76bee0cc 14639 case DW_LANG_C99:
c906108c 14640 case DW_LANG_C:
e142c38c 14641 cu->language = language_c;
c906108c
SS
14642 break;
14643 case DW_LANG_C_plus_plus:
e142c38c 14644 cu->language = language_cplus;
c906108c 14645 break;
6aecb9c2
JB
14646 case DW_LANG_D:
14647 cu->language = language_d;
14648 break;
c906108c
SS
14649 case DW_LANG_Fortran77:
14650 case DW_LANG_Fortran90:
b21b22e0 14651 case DW_LANG_Fortran95:
e142c38c 14652 cu->language = language_fortran;
c906108c 14653 break;
a766d390
DE
14654 case DW_LANG_Go:
14655 cu->language = language_go;
14656 break;
c906108c 14657 case DW_LANG_Mips_Assembler:
e142c38c 14658 cu->language = language_asm;
c906108c 14659 break;
bebd888e 14660 case DW_LANG_Java:
e142c38c 14661 cu->language = language_java;
bebd888e 14662 break;
c906108c 14663 case DW_LANG_Ada83:
8aaf0b47 14664 case DW_LANG_Ada95:
bc5f45f8
JB
14665 cu->language = language_ada;
14666 break;
72019c9c
GM
14667 case DW_LANG_Modula2:
14668 cu->language = language_m2;
14669 break;
fe8e67fd
PM
14670 case DW_LANG_Pascal83:
14671 cu->language = language_pascal;
14672 break;
22566fbd
DJ
14673 case DW_LANG_ObjC:
14674 cu->language = language_objc;
14675 break;
c906108c
SS
14676 case DW_LANG_Cobol74:
14677 case DW_LANG_Cobol85:
c906108c 14678 default:
e142c38c 14679 cu->language = language_minimal;
c906108c
SS
14680 break;
14681 }
e142c38c 14682 cu->language_defn = language_def (cu->language);
c906108c
SS
14683}
14684
14685/* Return the named attribute or NULL if not there. */
14686
14687static struct attribute *
e142c38c 14688dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14689{
a48e046c 14690 for (;;)
c906108c 14691 {
a48e046c
TT
14692 unsigned int i;
14693 struct attribute *spec = NULL;
14694
14695 for (i = 0; i < die->num_attrs; ++i)
14696 {
14697 if (die->attrs[i].name == name)
14698 return &die->attrs[i];
14699 if (die->attrs[i].name == DW_AT_specification
14700 || die->attrs[i].name == DW_AT_abstract_origin)
14701 spec = &die->attrs[i];
14702 }
14703
14704 if (!spec)
14705 break;
c906108c 14706
f2f0e013 14707 die = follow_die_ref (die, spec, &cu);
f2f0e013 14708 }
c5aa993b 14709
c906108c
SS
14710 return NULL;
14711}
14712
348e048f
DE
14713/* Return the named attribute or NULL if not there,
14714 but do not follow DW_AT_specification, etc.
14715 This is for use in contexts where we're reading .debug_types dies.
14716 Following DW_AT_specification, DW_AT_abstract_origin will take us
14717 back up the chain, and we want to go down. */
14718
14719static struct attribute *
45e58e77 14720dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14721{
14722 unsigned int i;
14723
14724 for (i = 0; i < die->num_attrs; ++i)
14725 if (die->attrs[i].name == name)
14726 return &die->attrs[i];
14727
14728 return NULL;
14729}
14730
05cf31d1
JB
14731/* Return non-zero iff the attribute NAME is defined for the given DIE,
14732 and holds a non-zero value. This function should only be used for
2dc7f7b3 14733 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14734
14735static int
14736dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14737{
14738 struct attribute *attr = dwarf2_attr (die, name, cu);
14739
14740 return (attr && DW_UNSND (attr));
14741}
14742
3ca72b44 14743static int
e142c38c 14744die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14745{
05cf31d1
JB
14746 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14747 which value is non-zero. However, we have to be careful with
14748 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14749 (via dwarf2_flag_true_p) follows this attribute. So we may
14750 end up accidently finding a declaration attribute that belongs
14751 to a different DIE referenced by the specification attribute,
14752 even though the given DIE does not have a declaration attribute. */
14753 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14754 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14755}
14756
63d06c5c 14757/* Return the die giving the specification for DIE, if there is
f2f0e013 14758 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14759 containing the return value on output. If there is no
14760 specification, but there is an abstract origin, that is
14761 returned. */
63d06c5c
DC
14762
14763static struct die_info *
f2f0e013 14764die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14765{
f2f0e013
DJ
14766 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14767 *spec_cu);
63d06c5c 14768
edb3359d
DJ
14769 if (spec_attr == NULL)
14770 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14771
63d06c5c
DC
14772 if (spec_attr == NULL)
14773 return NULL;
14774 else
f2f0e013 14775 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14776}
c906108c 14777
debd256d 14778/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14779 refers to.
14780 NOTE: This is also used as a "cleanup" function. */
14781
debd256d
JB
14782static void
14783free_line_header (struct line_header *lh)
14784{
14785 if (lh->standard_opcode_lengths)
a8bc7b56 14786 xfree (lh->standard_opcode_lengths);
debd256d
JB
14787
14788 /* Remember that all the lh->file_names[i].name pointers are
14789 pointers into debug_line_buffer, and don't need to be freed. */
14790 if (lh->file_names)
a8bc7b56 14791 xfree (lh->file_names);
debd256d
JB
14792
14793 /* Similarly for the include directory names. */
14794 if (lh->include_dirs)
a8bc7b56 14795 xfree (lh->include_dirs);
debd256d 14796
a8bc7b56 14797 xfree (lh);
debd256d
JB
14798}
14799
debd256d 14800/* Add an entry to LH's include directory table. */
ae2de4f8 14801
debd256d
JB
14802static void
14803add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14804{
debd256d
JB
14805 /* Grow the array if necessary. */
14806 if (lh->include_dirs_size == 0)
c5aa993b 14807 {
debd256d
JB
14808 lh->include_dirs_size = 1; /* for testing */
14809 lh->include_dirs = xmalloc (lh->include_dirs_size
14810 * sizeof (*lh->include_dirs));
14811 }
14812 else if (lh->num_include_dirs >= lh->include_dirs_size)
14813 {
14814 lh->include_dirs_size *= 2;
14815 lh->include_dirs = xrealloc (lh->include_dirs,
14816 (lh->include_dirs_size
14817 * sizeof (*lh->include_dirs)));
c5aa993b 14818 }
c906108c 14819
debd256d
JB
14820 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14821}
6e70227d 14822
debd256d 14823/* Add an entry to LH's file name table. */
ae2de4f8 14824
debd256d
JB
14825static void
14826add_file_name (struct line_header *lh,
14827 char *name,
14828 unsigned int dir_index,
14829 unsigned int mod_time,
14830 unsigned int length)
14831{
14832 struct file_entry *fe;
14833
14834 /* Grow the array if necessary. */
14835 if (lh->file_names_size == 0)
14836 {
14837 lh->file_names_size = 1; /* for testing */
14838 lh->file_names = xmalloc (lh->file_names_size
14839 * sizeof (*lh->file_names));
14840 }
14841 else if (lh->num_file_names >= lh->file_names_size)
14842 {
14843 lh->file_names_size *= 2;
14844 lh->file_names = xrealloc (lh->file_names,
14845 (lh->file_names_size
14846 * sizeof (*lh->file_names)));
14847 }
14848
14849 fe = &lh->file_names[lh->num_file_names++];
14850 fe->name = name;
14851 fe->dir_index = dir_index;
14852 fe->mod_time = mod_time;
14853 fe->length = length;
aaa75496 14854 fe->included_p = 0;
cb1df416 14855 fe->symtab = NULL;
debd256d 14856}
6e70227d 14857
36586728
TT
14858/* A convenience function to find the proper .debug_line section for a
14859 CU. */
14860
14861static struct dwarf2_section_info *
14862get_debug_line_section (struct dwarf2_cu *cu)
14863{
14864 struct dwarf2_section_info *section;
14865
14866 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14867 DWO file. */
14868 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14869 section = &cu->dwo_unit->dwo_file->sections.line;
14870 else if (cu->per_cu->is_dwz)
14871 {
14872 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14873
14874 section = &dwz->line;
14875 }
14876 else
14877 section = &dwarf2_per_objfile->line;
14878
14879 return section;
14880}
14881
debd256d 14882/* Read the statement program header starting at OFFSET in
3019eac3 14883 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14884 to a struct line_header, allocated using xmalloc.
debd256d
JB
14885
14886 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14887 the returned object point into the dwarf line section buffer,
14888 and must not be freed. */
ae2de4f8 14889
debd256d 14890static struct line_header *
3019eac3 14891dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14892{
14893 struct cleanup *back_to;
14894 struct line_header *lh;
fe1b8b76 14895 gdb_byte *line_ptr;
c764a876 14896 unsigned int bytes_read, offset_size;
debd256d
JB
14897 int i;
14898 char *cur_dir, *cur_file;
3019eac3
DE
14899 struct dwarf2_section_info *section;
14900 bfd *abfd;
14901
36586728 14902 section = get_debug_line_section (cu);
3019eac3
DE
14903 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14904 if (section->buffer == NULL)
debd256d 14905 {
3019eac3
DE
14906 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14907 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14908 else
14909 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14910 return 0;
14911 }
14912
fceca515
DE
14913 /* We can't do this until we know the section is non-empty.
14914 Only then do we know we have such a section. */
14915 abfd = section->asection->owner;
14916
a738430d
MK
14917 /* Make sure that at least there's room for the total_length field.
14918 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14919 if (offset + 4 >= section->size)
debd256d 14920 {
4d3c2250 14921 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14922 return 0;
14923 }
14924
14925 lh = xmalloc (sizeof (*lh));
14926 memset (lh, 0, sizeof (*lh));
14927 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14928 (void *) lh);
14929
3019eac3 14930 line_ptr = section->buffer + offset;
debd256d 14931
a738430d 14932 /* Read in the header. */
6e70227d 14933 lh->total_length =
c764a876
DE
14934 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14935 &bytes_read, &offset_size);
debd256d 14936 line_ptr += bytes_read;
3019eac3 14937 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14938 {
4d3c2250 14939 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14940 return 0;
14941 }
14942 lh->statement_program_end = line_ptr + lh->total_length;
14943 lh->version = read_2_bytes (abfd, line_ptr);
14944 line_ptr += 2;
c764a876
DE
14945 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14946 line_ptr += offset_size;
debd256d
JB
14947 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14948 line_ptr += 1;
2dc7f7b3
TT
14949 if (lh->version >= 4)
14950 {
14951 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14952 line_ptr += 1;
14953 }
14954 else
14955 lh->maximum_ops_per_instruction = 1;
14956
14957 if (lh->maximum_ops_per_instruction == 0)
14958 {
14959 lh->maximum_ops_per_instruction = 1;
14960 complaint (&symfile_complaints,
3e43a32a
MS
14961 _("invalid maximum_ops_per_instruction "
14962 "in `.debug_line' section"));
2dc7f7b3
TT
14963 }
14964
debd256d
JB
14965 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14966 line_ptr += 1;
14967 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14968 line_ptr += 1;
14969 lh->line_range = read_1_byte (abfd, line_ptr);
14970 line_ptr += 1;
14971 lh->opcode_base = read_1_byte (abfd, line_ptr);
14972 line_ptr += 1;
14973 lh->standard_opcode_lengths
fe1b8b76 14974 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
14975
14976 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14977 for (i = 1; i < lh->opcode_base; ++i)
14978 {
14979 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14980 line_ptr += 1;
14981 }
14982
a738430d 14983 /* Read directory table. */
9b1c24c8 14984 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14985 {
14986 line_ptr += bytes_read;
14987 add_include_dir (lh, cur_dir);
14988 }
14989 line_ptr += bytes_read;
14990
a738430d 14991 /* Read file name table. */
9b1c24c8 14992 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14993 {
14994 unsigned int dir_index, mod_time, length;
14995
14996 line_ptr += bytes_read;
14997 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14998 line_ptr += bytes_read;
14999 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15000 line_ptr += bytes_read;
15001 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15002 line_ptr += bytes_read;
15003
15004 add_file_name (lh, cur_file, dir_index, mod_time, length);
15005 }
15006 line_ptr += bytes_read;
6e70227d 15007 lh->statement_program_start = line_ptr;
debd256d 15008
3019eac3 15009 if (line_ptr > (section->buffer + section->size))
4d3c2250 15010 complaint (&symfile_complaints,
3e43a32a
MS
15011 _("line number info header doesn't "
15012 "fit in `.debug_line' section"));
debd256d
JB
15013
15014 discard_cleanups (back_to);
15015 return lh;
15016}
c906108c 15017
c6da4cef
DE
15018/* Subroutine of dwarf_decode_lines to simplify it.
15019 Return the file name of the psymtab for included file FILE_INDEX
15020 in line header LH of PST.
15021 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15022 If space for the result is malloc'd, it will be freed by a cleanup.
15023 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15024
15025static char *
15026psymtab_include_file_name (const struct line_header *lh, int file_index,
15027 const struct partial_symtab *pst,
15028 const char *comp_dir)
15029{
15030 const struct file_entry fe = lh->file_names [file_index];
15031 char *include_name = fe.name;
15032 char *include_name_to_compare = include_name;
15033 char *dir_name = NULL;
72b9f47f
TT
15034 const char *pst_filename;
15035 char *copied_name = NULL;
c6da4cef
DE
15036 int file_is_pst;
15037
15038 if (fe.dir_index)
15039 dir_name = lh->include_dirs[fe.dir_index - 1];
15040
15041 if (!IS_ABSOLUTE_PATH (include_name)
15042 && (dir_name != NULL || comp_dir != NULL))
15043 {
15044 /* Avoid creating a duplicate psymtab for PST.
15045 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15046 Before we do the comparison, however, we need to account
15047 for DIR_NAME and COMP_DIR.
15048 First prepend dir_name (if non-NULL). If we still don't
15049 have an absolute path prepend comp_dir (if non-NULL).
15050 However, the directory we record in the include-file's
15051 psymtab does not contain COMP_DIR (to match the
15052 corresponding symtab(s)).
15053
15054 Example:
15055
15056 bash$ cd /tmp
15057 bash$ gcc -g ./hello.c
15058 include_name = "hello.c"
15059 dir_name = "."
15060 DW_AT_comp_dir = comp_dir = "/tmp"
15061 DW_AT_name = "./hello.c" */
15062
15063 if (dir_name != NULL)
15064 {
15065 include_name = concat (dir_name, SLASH_STRING,
15066 include_name, (char *)NULL);
15067 include_name_to_compare = include_name;
15068 make_cleanup (xfree, include_name);
15069 }
15070 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15071 {
15072 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15073 include_name, (char *)NULL);
15074 }
15075 }
15076
15077 pst_filename = pst->filename;
15078 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15079 {
72b9f47f
TT
15080 copied_name = concat (pst->dirname, SLASH_STRING,
15081 pst_filename, (char *)NULL);
15082 pst_filename = copied_name;
c6da4cef
DE
15083 }
15084
1e3fad37 15085 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15086
15087 if (include_name_to_compare != include_name)
15088 xfree (include_name_to_compare);
72b9f47f
TT
15089 if (copied_name != NULL)
15090 xfree (copied_name);
c6da4cef
DE
15091
15092 if (file_is_pst)
15093 return NULL;
15094 return include_name;
15095}
15096
c91513d8
PP
15097/* Ignore this record_line request. */
15098
15099static void
15100noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15101{
15102 return;
15103}
15104
f3f5162e
DE
15105/* Subroutine of dwarf_decode_lines to simplify it.
15106 Process the line number information in LH. */
debd256d 15107
c906108c 15108static void
f3f5162e
DE
15109dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15110 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15111{
a8c50c1f 15112 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15113 gdb_byte *line_end;
a8c50c1f 15114 unsigned int bytes_read, extended_len;
c906108c 15115 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15116 CORE_ADDR baseaddr;
15117 struct objfile *objfile = cu->objfile;
f3f5162e 15118 bfd *abfd = objfile->obfd;
fbf65064 15119 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15120 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15121 struct subfile *last_subfile = NULL;
c91513d8
PP
15122 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15123 = record_line;
e142c38c
DJ
15124
15125 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15126
debd256d
JB
15127 line_ptr = lh->statement_program_start;
15128 line_end = lh->statement_program_end;
c906108c
SS
15129
15130 /* Read the statement sequences until there's nothing left. */
15131 while (line_ptr < line_end)
15132 {
15133 /* state machine registers */
15134 CORE_ADDR address = 0;
15135 unsigned int file = 1;
15136 unsigned int line = 1;
15137 unsigned int column = 0;
debd256d 15138 int is_stmt = lh->default_is_stmt;
c906108c
SS
15139 int basic_block = 0;
15140 int end_sequence = 0;
fbf65064 15141 CORE_ADDR addr;
2dc7f7b3 15142 unsigned char op_index = 0;
c906108c 15143
aaa75496 15144 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15145 {
aaa75496 15146 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15147 /* lh->include_dirs and lh->file_names are 0-based, but the
15148 directory and file name numbers in the statement program
15149 are 1-based. */
15150 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15151 char *dir = NULL;
a738430d 15152
debd256d
JB
15153 if (fe->dir_index)
15154 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15155
15156 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15157 }
15158
a738430d 15159 /* Decode the table. */
c5aa993b 15160 while (!end_sequence)
c906108c
SS
15161 {
15162 op_code = read_1_byte (abfd, line_ptr);
15163 line_ptr += 1;
59205f5a
JB
15164 if (line_ptr > line_end)
15165 {
15166 dwarf2_debug_line_missing_end_sequence_complaint ();
15167 break;
15168 }
9aa1fe7e 15169
debd256d 15170 if (op_code >= lh->opcode_base)
6e70227d 15171 {
a738430d 15172 /* Special operand. */
debd256d 15173 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15174 address += (((op_index + (adj_opcode / lh->line_range))
15175 / lh->maximum_ops_per_instruction)
15176 * lh->minimum_instruction_length);
15177 op_index = ((op_index + (adj_opcode / lh->line_range))
15178 % lh->maximum_ops_per_instruction);
debd256d 15179 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15180 if (lh->num_file_names < file || file == 0)
25e43795 15181 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15182 /* For now we ignore lines not starting on an
15183 instruction boundary. */
15184 else if (op_index == 0)
25e43795
DJ
15185 {
15186 lh->file_names[file - 1].included_p = 1;
ca5f395d 15187 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15188 {
15189 if (last_subfile != current_subfile)
15190 {
15191 addr = gdbarch_addr_bits_remove (gdbarch, address);
15192 if (last_subfile)
c91513d8 15193 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15194 last_subfile = current_subfile;
15195 }
25e43795 15196 /* Append row to matrix using current values. */
7019d805 15197 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15198 (*p_record_line) (current_subfile, line, addr);
366da635 15199 }
25e43795 15200 }
ca5f395d 15201 basic_block = 0;
9aa1fe7e
GK
15202 }
15203 else switch (op_code)
c906108c
SS
15204 {
15205 case DW_LNS_extended_op:
3e43a32a
MS
15206 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15207 &bytes_read);
473b7be6 15208 line_ptr += bytes_read;
a8c50c1f 15209 extended_end = line_ptr + extended_len;
c906108c
SS
15210 extended_op = read_1_byte (abfd, line_ptr);
15211 line_ptr += 1;
15212 switch (extended_op)
15213 {
15214 case DW_LNE_end_sequence:
c91513d8 15215 p_record_line = record_line;
c906108c 15216 end_sequence = 1;
c906108c
SS
15217 break;
15218 case DW_LNE_set_address:
e7c27a73 15219 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15220
15221 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15222 {
15223 /* This line table is for a function which has been
15224 GCd by the linker. Ignore it. PR gdb/12528 */
15225
15226 long line_offset
36586728 15227 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15228
15229 complaint (&symfile_complaints,
15230 _(".debug_line address at offset 0x%lx is 0 "
15231 "[in module %s]"),
bb5ed363 15232 line_offset, objfile->name);
c91513d8
PP
15233 p_record_line = noop_record_line;
15234 }
15235
2dc7f7b3 15236 op_index = 0;
107d2387
AC
15237 line_ptr += bytes_read;
15238 address += baseaddr;
c906108c
SS
15239 break;
15240 case DW_LNE_define_file:
debd256d
JB
15241 {
15242 char *cur_file;
15243 unsigned int dir_index, mod_time, length;
6e70227d 15244
3e43a32a
MS
15245 cur_file = read_direct_string (abfd, line_ptr,
15246 &bytes_read);
debd256d
JB
15247 line_ptr += bytes_read;
15248 dir_index =
15249 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15250 line_ptr += bytes_read;
15251 mod_time =
15252 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15253 line_ptr += bytes_read;
15254 length =
15255 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15256 line_ptr += bytes_read;
15257 add_file_name (lh, cur_file, dir_index, mod_time, length);
15258 }
c906108c 15259 break;
d0c6ba3d
CC
15260 case DW_LNE_set_discriminator:
15261 /* The discriminator is not interesting to the debugger;
15262 just ignore it. */
15263 line_ptr = extended_end;
15264 break;
c906108c 15265 default:
4d3c2250 15266 complaint (&symfile_complaints,
e2e0b3e5 15267 _("mangled .debug_line section"));
debd256d 15268 return;
c906108c 15269 }
a8c50c1f
DJ
15270 /* Make sure that we parsed the extended op correctly. If e.g.
15271 we expected a different address size than the producer used,
15272 we may have read the wrong number of bytes. */
15273 if (line_ptr != extended_end)
15274 {
15275 complaint (&symfile_complaints,
15276 _("mangled .debug_line section"));
15277 return;
15278 }
c906108c
SS
15279 break;
15280 case DW_LNS_copy:
59205f5a 15281 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15282 dwarf2_debug_line_missing_file_complaint ();
15283 else
366da635 15284 {
25e43795 15285 lh->file_names[file - 1].included_p = 1;
ca5f395d 15286 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15287 {
15288 if (last_subfile != current_subfile)
15289 {
15290 addr = gdbarch_addr_bits_remove (gdbarch, address);
15291 if (last_subfile)
c91513d8 15292 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15293 last_subfile = current_subfile;
15294 }
7019d805 15295 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15296 (*p_record_line) (current_subfile, line, addr);
fbf65064 15297 }
366da635 15298 }
c906108c
SS
15299 basic_block = 0;
15300 break;
15301 case DW_LNS_advance_pc:
2dc7f7b3
TT
15302 {
15303 CORE_ADDR adjust
15304 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15305
15306 address += (((op_index + adjust)
15307 / lh->maximum_ops_per_instruction)
15308 * lh->minimum_instruction_length);
15309 op_index = ((op_index + adjust)
15310 % lh->maximum_ops_per_instruction);
15311 line_ptr += bytes_read;
15312 }
c906108c
SS
15313 break;
15314 case DW_LNS_advance_line:
15315 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15316 line_ptr += bytes_read;
15317 break;
15318 case DW_LNS_set_file:
debd256d 15319 {
a738430d
MK
15320 /* The arrays lh->include_dirs and lh->file_names are
15321 0-based, but the directory and file name numbers in
15322 the statement program are 1-based. */
debd256d 15323 struct file_entry *fe;
4f1520fb 15324 char *dir = NULL;
a738430d 15325
debd256d
JB
15326 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15327 line_ptr += bytes_read;
59205f5a 15328 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15329 dwarf2_debug_line_missing_file_complaint ();
15330 else
15331 {
15332 fe = &lh->file_names[file - 1];
15333 if (fe->dir_index)
15334 dir = lh->include_dirs[fe->dir_index - 1];
15335 if (!decode_for_pst_p)
15336 {
15337 last_subfile = current_subfile;
15338 dwarf2_start_subfile (fe->name, dir, comp_dir);
15339 }
15340 }
debd256d 15341 }
c906108c
SS
15342 break;
15343 case DW_LNS_set_column:
15344 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15345 line_ptr += bytes_read;
15346 break;
15347 case DW_LNS_negate_stmt:
15348 is_stmt = (!is_stmt);
15349 break;
15350 case DW_LNS_set_basic_block:
15351 basic_block = 1;
15352 break;
c2c6d25f
JM
15353 /* Add to the address register of the state machine the
15354 address increment value corresponding to special opcode
a738430d
MK
15355 255. I.e., this value is scaled by the minimum
15356 instruction length since special opcode 255 would have
b021a221 15357 scaled the increment. */
c906108c 15358 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15359 {
15360 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15361
15362 address += (((op_index + adjust)
15363 / lh->maximum_ops_per_instruction)
15364 * lh->minimum_instruction_length);
15365 op_index = ((op_index + adjust)
15366 % lh->maximum_ops_per_instruction);
15367 }
c906108c
SS
15368 break;
15369 case DW_LNS_fixed_advance_pc:
15370 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15371 op_index = 0;
c906108c
SS
15372 line_ptr += 2;
15373 break;
9aa1fe7e 15374 default:
a738430d
MK
15375 {
15376 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15377 int i;
a738430d 15378
debd256d 15379 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15380 {
15381 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15382 line_ptr += bytes_read;
15383 }
15384 }
c906108c
SS
15385 }
15386 }
59205f5a
JB
15387 if (lh->num_file_names < file || file == 0)
15388 dwarf2_debug_line_missing_file_complaint ();
15389 else
15390 {
15391 lh->file_names[file - 1].included_p = 1;
15392 if (!decode_for_pst_p)
fbf65064
UW
15393 {
15394 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15395 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15396 }
59205f5a 15397 }
c906108c 15398 }
f3f5162e
DE
15399}
15400
15401/* Decode the Line Number Program (LNP) for the given line_header
15402 structure and CU. The actual information extracted and the type
15403 of structures created from the LNP depends on the value of PST.
15404
15405 1. If PST is NULL, then this procedure uses the data from the program
15406 to create all necessary symbol tables, and their linetables.
15407
15408 2. If PST is not NULL, this procedure reads the program to determine
15409 the list of files included by the unit represented by PST, and
15410 builds all the associated partial symbol tables.
15411
15412 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15413 It is used for relative paths in the line table.
15414 NOTE: When processing partial symtabs (pst != NULL),
15415 comp_dir == pst->dirname.
15416
15417 NOTE: It is important that psymtabs have the same file name (via strcmp)
15418 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15419 symtab we don't use it in the name of the psymtabs we create.
15420 E.g. expand_line_sal requires this when finding psymtabs to expand.
15421 A good testcase for this is mb-inline.exp. */
15422
15423static void
15424dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15425 struct dwarf2_cu *cu, struct partial_symtab *pst,
15426 int want_line_info)
15427{
15428 struct objfile *objfile = cu->objfile;
15429 const int decode_for_pst_p = (pst != NULL);
15430 struct subfile *first_subfile = current_subfile;
15431
15432 if (want_line_info)
15433 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15434
15435 if (decode_for_pst_p)
15436 {
15437 int file_index;
15438
15439 /* Now that we're done scanning the Line Header Program, we can
15440 create the psymtab of each included file. */
15441 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15442 if (lh->file_names[file_index].included_p == 1)
15443 {
c6da4cef
DE
15444 char *include_name =
15445 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15446 if (include_name != NULL)
aaa75496
JB
15447 dwarf2_create_include_psymtab (include_name, pst, objfile);
15448 }
15449 }
cb1df416
DJ
15450 else
15451 {
15452 /* Make sure a symtab is created for every file, even files
15453 which contain only variables (i.e. no code with associated
15454 line numbers). */
cb1df416 15455 int i;
cb1df416
DJ
15456
15457 for (i = 0; i < lh->num_file_names; i++)
15458 {
15459 char *dir = NULL;
f3f5162e 15460 struct file_entry *fe;
9a619af0 15461
cb1df416
DJ
15462 fe = &lh->file_names[i];
15463 if (fe->dir_index)
15464 dir = lh->include_dirs[fe->dir_index - 1];
15465 dwarf2_start_subfile (fe->name, dir, comp_dir);
15466
15467 /* Skip the main file; we don't need it, and it must be
15468 allocated last, so that it will show up before the
15469 non-primary symtabs in the objfile's symtab list. */
15470 if (current_subfile == first_subfile)
15471 continue;
15472
15473 if (current_subfile->symtab == NULL)
15474 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15475 objfile);
cb1df416
DJ
15476 fe->symtab = current_subfile->symtab;
15477 }
15478 }
c906108c
SS
15479}
15480
15481/* Start a subfile for DWARF. FILENAME is the name of the file and
15482 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15483 or NULL if not known. COMP_DIR is the compilation directory for the
15484 linetable's compilation unit or NULL if not known.
c906108c
SS
15485 This routine tries to keep line numbers from identical absolute and
15486 relative file names in a common subfile.
15487
15488 Using the `list' example from the GDB testsuite, which resides in
15489 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15490 of /srcdir/list0.c yields the following debugging information for list0.c:
15491
c5aa993b
JM
15492 DW_AT_name: /srcdir/list0.c
15493 DW_AT_comp_dir: /compdir
357e46e7 15494 files.files[0].name: list0.h
c5aa993b 15495 files.files[0].dir: /srcdir
357e46e7 15496 files.files[1].name: list0.c
c5aa993b 15497 files.files[1].dir: /srcdir
c906108c
SS
15498
15499 The line number information for list0.c has to end up in a single
4f1520fb
FR
15500 subfile, so that `break /srcdir/list0.c:1' works as expected.
15501 start_subfile will ensure that this happens provided that we pass the
15502 concatenation of files.files[1].dir and files.files[1].name as the
15503 subfile's name. */
c906108c
SS
15504
15505static void
3e43a32a
MS
15506dwarf2_start_subfile (char *filename, const char *dirname,
15507 const char *comp_dir)
c906108c 15508{
4f1520fb
FR
15509 char *fullname;
15510
15511 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15512 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15513 second argument to start_subfile. To be consistent, we do the
15514 same here. In order not to lose the line information directory,
15515 we concatenate it to the filename when it makes sense.
15516 Note that the Dwarf3 standard says (speaking of filenames in line
15517 information): ``The directory index is ignored for file names
15518 that represent full path names''. Thus ignoring dirname in the
15519 `else' branch below isn't an issue. */
c906108c 15520
d5166ae1 15521 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15522 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15523 else
15524 fullname = filename;
c906108c 15525
4f1520fb
FR
15526 start_subfile (fullname, comp_dir);
15527
15528 if (fullname != filename)
15529 xfree (fullname);
c906108c
SS
15530}
15531
f4dc4d17
DE
15532/* Start a symtab for DWARF.
15533 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15534
15535static void
15536dwarf2_start_symtab (struct dwarf2_cu *cu,
15537 char *name, char *comp_dir, CORE_ADDR low_pc)
15538{
15539 start_symtab (name, comp_dir, low_pc);
15540 record_debugformat ("DWARF 2");
15541 record_producer (cu->producer);
15542
15543 /* We assume that we're processing GCC output. */
15544 processing_gcc_compilation = 2;
15545
15546 processing_has_namespace_info = 0;
15547}
15548
4c2df51b
DJ
15549static void
15550var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15551 struct dwarf2_cu *cu)
4c2df51b 15552{
e7c27a73
DJ
15553 struct objfile *objfile = cu->objfile;
15554 struct comp_unit_head *cu_header = &cu->header;
15555
4c2df51b
DJ
15556 /* NOTE drow/2003-01-30: There used to be a comment and some special
15557 code here to turn a symbol with DW_AT_external and a
15558 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15559 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15560 with some versions of binutils) where shared libraries could have
15561 relocations against symbols in their debug information - the
15562 minimal symbol would have the right address, but the debug info
15563 would not. It's no longer necessary, because we will explicitly
15564 apply relocations when we read in the debug information now. */
15565
15566 /* A DW_AT_location attribute with no contents indicates that a
15567 variable has been optimized away. */
15568 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15569 {
15570 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15571 return;
15572 }
15573
15574 /* Handle one degenerate form of location expression specially, to
15575 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15576 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15577 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15578
15579 if (attr_form_is_block (attr)
3019eac3
DE
15580 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15581 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15582 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15583 && (DW_BLOCK (attr)->size
15584 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15585 {
891d2f0b 15586 unsigned int dummy;
4c2df51b 15587
3019eac3
DE
15588 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15589 SYMBOL_VALUE_ADDRESS (sym) =
15590 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15591 else
15592 SYMBOL_VALUE_ADDRESS (sym) =
15593 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15594 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15595 fixup_symbol_section (sym, objfile);
15596 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15597 SYMBOL_SECTION (sym));
4c2df51b
DJ
15598 return;
15599 }
15600
15601 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15602 expression evaluator, and use LOC_COMPUTED only when necessary
15603 (i.e. when the value of a register or memory location is
15604 referenced, or a thread-local block, etc.). Then again, it might
15605 not be worthwhile. I'm assuming that it isn't unless performance
15606 or memory numbers show me otherwise. */
15607
e7c27a73 15608 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15609 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15610
15611 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15612 cu->has_loclist = 1;
4c2df51b
DJ
15613}
15614
c906108c
SS
15615/* Given a pointer to a DWARF information entry, figure out if we need
15616 to make a symbol table entry for it, and if so, create a new entry
15617 and return a pointer to it.
15618 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15619 used the passed type.
15620 If SPACE is not NULL, use it to hold the new symbol. If it is
15621 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15622
15623static struct symbol *
34eaf542
TT
15624new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15625 struct symbol *space)
c906108c 15626{
e7c27a73 15627 struct objfile *objfile = cu->objfile;
c906108c
SS
15628 struct symbol *sym = NULL;
15629 char *name;
15630 struct attribute *attr = NULL;
15631 struct attribute *attr2 = NULL;
e142c38c 15632 CORE_ADDR baseaddr;
e37fd15a
SW
15633 struct pending **list_to_add = NULL;
15634
edb3359d 15635 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15636
15637 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15638
94af9270 15639 name = dwarf2_name (die, cu);
c906108c
SS
15640 if (name)
15641 {
94af9270 15642 const char *linkagename;
34eaf542 15643 int suppress_add = 0;
94af9270 15644
34eaf542
TT
15645 if (space)
15646 sym = space;
15647 else
15648 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15649 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15650
15651 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15652 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15653 linkagename = dwarf2_physname (name, die, cu);
15654 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15655
f55ee35c
JK
15656 /* Fortran does not have mangling standard and the mangling does differ
15657 between gfortran, iFort etc. */
15658 if (cu->language == language_fortran
b250c185 15659 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
15660 symbol_set_demangled_name (&(sym->ginfo),
15661 (char *) dwarf2_full_name (name, die, cu),
15662 NULL);
f55ee35c 15663
c906108c 15664 /* Default assumptions.
c5aa993b 15665 Use the passed type or decode it from the die. */
176620f1 15666 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15667 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15668 if (type != NULL)
15669 SYMBOL_TYPE (sym) = type;
15670 else
e7c27a73 15671 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15672 attr = dwarf2_attr (die,
15673 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15674 cu);
c906108c
SS
15675 if (attr)
15676 {
15677 SYMBOL_LINE (sym) = DW_UNSND (attr);
15678 }
cb1df416 15679
edb3359d
DJ
15680 attr = dwarf2_attr (die,
15681 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15682 cu);
cb1df416
DJ
15683 if (attr)
15684 {
15685 int file_index = DW_UNSND (attr);
9a619af0 15686
cb1df416
DJ
15687 if (cu->line_header == NULL
15688 || file_index > cu->line_header->num_file_names)
15689 complaint (&symfile_complaints,
15690 _("file index out of range"));
1c3d648d 15691 else if (file_index > 0)
cb1df416
DJ
15692 {
15693 struct file_entry *fe;
9a619af0 15694
cb1df416
DJ
15695 fe = &cu->line_header->file_names[file_index - 1];
15696 SYMBOL_SYMTAB (sym) = fe->symtab;
15697 }
15698 }
15699
c906108c
SS
15700 switch (die->tag)
15701 {
15702 case DW_TAG_label:
e142c38c 15703 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15704 if (attr)
15705 {
15706 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15707 }
0f5238ed
TT
15708 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15709 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15710 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15711 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15712 break;
15713 case DW_TAG_subprogram:
15714 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15715 finish_block. */
15716 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15717 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15718 if ((attr2 && (DW_UNSND (attr2) != 0))
15719 || cu->language == language_ada)
c906108c 15720 {
2cfa0c8d
JB
15721 /* Subprograms marked external are stored as a global symbol.
15722 Ada subprograms, whether marked external or not, are always
15723 stored as a global symbol, because we want to be able to
15724 access them globally. For instance, we want to be able
15725 to break on a nested subprogram without having to
15726 specify the context. */
e37fd15a 15727 list_to_add = &global_symbols;
c906108c
SS
15728 }
15729 else
15730 {
e37fd15a 15731 list_to_add = cu->list_in_scope;
c906108c
SS
15732 }
15733 break;
edb3359d
DJ
15734 case DW_TAG_inlined_subroutine:
15735 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15736 finish_block. */
15737 SYMBOL_CLASS (sym) = LOC_BLOCK;
15738 SYMBOL_INLINED (sym) = 1;
481860b3 15739 list_to_add = cu->list_in_scope;
edb3359d 15740 break;
34eaf542
TT
15741 case DW_TAG_template_value_param:
15742 suppress_add = 1;
15743 /* Fall through. */
72929c62 15744 case DW_TAG_constant:
c906108c 15745 case DW_TAG_variable:
254e6b9e 15746 case DW_TAG_member:
0963b4bd
MS
15747 /* Compilation with minimal debug info may result in
15748 variables with missing type entries. Change the
15749 misleading `void' type to something sensible. */
c906108c 15750 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15751 SYMBOL_TYPE (sym)
46bf5051 15752 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15753
e142c38c 15754 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15755 /* In the case of DW_TAG_member, we should only be called for
15756 static const members. */
15757 if (die->tag == DW_TAG_member)
15758 {
3863f96c
DE
15759 /* dwarf2_add_field uses die_is_declaration,
15760 so we do the same. */
254e6b9e
DE
15761 gdb_assert (die_is_declaration (die, cu));
15762 gdb_assert (attr);
15763 }
c906108c
SS
15764 if (attr)
15765 {
e7c27a73 15766 dwarf2_const_value (attr, sym, cu);
e142c38c 15767 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15768 if (!suppress_add)
34eaf542
TT
15769 {
15770 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15771 list_to_add = &global_symbols;
34eaf542 15772 else
e37fd15a 15773 list_to_add = cu->list_in_scope;
34eaf542 15774 }
c906108c
SS
15775 break;
15776 }
e142c38c 15777 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15778 if (attr)
15779 {
e7c27a73 15780 var_decode_location (attr, sym, cu);
e142c38c 15781 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15782
15783 /* Fortran explicitly imports any global symbols to the local
15784 scope by DW_TAG_common_block. */
15785 if (cu->language == language_fortran && die->parent
15786 && die->parent->tag == DW_TAG_common_block)
15787 attr2 = NULL;
15788
caac4577
JG
15789 if (SYMBOL_CLASS (sym) == LOC_STATIC
15790 && SYMBOL_VALUE_ADDRESS (sym) == 0
15791 && !dwarf2_per_objfile->has_section_at_zero)
15792 {
15793 /* When a static variable is eliminated by the linker,
15794 the corresponding debug information is not stripped
15795 out, but the variable address is set to null;
15796 do not add such variables into symbol table. */
15797 }
15798 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15799 {
f55ee35c
JK
15800 /* Workaround gfortran PR debug/40040 - it uses
15801 DW_AT_location for variables in -fPIC libraries which may
15802 get overriden by other libraries/executable and get
15803 a different address. Resolve it by the minimal symbol
15804 which may come from inferior's executable using copy
15805 relocation. Make this workaround only for gfortran as for
15806 other compilers GDB cannot guess the minimal symbol
15807 Fortran mangling kind. */
15808 if (cu->language == language_fortran && die->parent
15809 && die->parent->tag == DW_TAG_module
15810 && cu->producer
15811 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15812 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15813
1c809c68
TT
15814 /* A variable with DW_AT_external is never static,
15815 but it may be block-scoped. */
15816 list_to_add = (cu->list_in_scope == &file_symbols
15817 ? &global_symbols : cu->list_in_scope);
1c809c68 15818 }
c906108c 15819 else
e37fd15a 15820 list_to_add = cu->list_in_scope;
c906108c
SS
15821 }
15822 else
15823 {
15824 /* We do not know the address of this symbol.
c5aa993b
JM
15825 If it is an external symbol and we have type information
15826 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15827 The address of the variable will then be determined from
15828 the minimal symbol table whenever the variable is
15829 referenced. */
e142c38c 15830 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15831
15832 /* Fortran explicitly imports any global symbols to the local
15833 scope by DW_TAG_common_block. */
15834 if (cu->language == language_fortran && die->parent
15835 && die->parent->tag == DW_TAG_common_block)
15836 {
15837 /* SYMBOL_CLASS doesn't matter here because
15838 read_common_block is going to reset it. */
15839 if (!suppress_add)
15840 list_to_add = cu->list_in_scope;
15841 }
15842 else if (attr2 && (DW_UNSND (attr2) != 0)
15843 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15844 {
0fe7935b
DJ
15845 /* A variable with DW_AT_external is never static, but it
15846 may be block-scoped. */
15847 list_to_add = (cu->list_in_scope == &file_symbols
15848 ? &global_symbols : cu->list_in_scope);
15849
c906108c 15850 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15851 }
442ddf59
JK
15852 else if (!die_is_declaration (die, cu))
15853 {
15854 /* Use the default LOC_OPTIMIZED_OUT class. */
15855 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15856 if (!suppress_add)
15857 list_to_add = cu->list_in_scope;
442ddf59 15858 }
c906108c
SS
15859 }
15860 break;
15861 case DW_TAG_formal_parameter:
edb3359d
DJ
15862 /* If we are inside a function, mark this as an argument. If
15863 not, we might be looking at an argument to an inlined function
15864 when we do not have enough information to show inlined frames;
15865 pretend it's a local variable in that case so that the user can
15866 still see it. */
15867 if (context_stack_depth > 0
15868 && context_stack[context_stack_depth - 1].name != NULL)
15869 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15870 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15871 if (attr)
15872 {
e7c27a73 15873 var_decode_location (attr, sym, cu);
c906108c 15874 }
e142c38c 15875 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15876 if (attr)
15877 {
e7c27a73 15878 dwarf2_const_value (attr, sym, cu);
c906108c 15879 }
f346a30d 15880
e37fd15a 15881 list_to_add = cu->list_in_scope;
c906108c
SS
15882 break;
15883 case DW_TAG_unspecified_parameters:
15884 /* From varargs functions; gdb doesn't seem to have any
15885 interest in this information, so just ignore it for now.
15886 (FIXME?) */
15887 break;
34eaf542
TT
15888 case DW_TAG_template_type_param:
15889 suppress_add = 1;
15890 /* Fall through. */
c906108c 15891 case DW_TAG_class_type:
680b30c7 15892 case DW_TAG_interface_type:
c906108c
SS
15893 case DW_TAG_structure_type:
15894 case DW_TAG_union_type:
72019c9c 15895 case DW_TAG_set_type:
c906108c
SS
15896 case DW_TAG_enumeration_type:
15897 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15898 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15899
63d06c5c 15900 {
987504bb 15901 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15902 really ever be static objects: otherwise, if you try
15903 to, say, break of a class's method and you're in a file
15904 which doesn't mention that class, it won't work unless
15905 the check for all static symbols in lookup_symbol_aux
15906 saves you. See the OtherFileClass tests in
15907 gdb.c++/namespace.exp. */
15908
e37fd15a 15909 if (!suppress_add)
34eaf542 15910 {
34eaf542
TT
15911 list_to_add = (cu->list_in_scope == &file_symbols
15912 && (cu->language == language_cplus
15913 || cu->language == language_java)
15914 ? &global_symbols : cu->list_in_scope);
63d06c5c 15915
64382290
TT
15916 /* The semantics of C++ state that "struct foo {
15917 ... }" also defines a typedef for "foo". A Java
15918 class declaration also defines a typedef for the
15919 class. */
15920 if (cu->language == language_cplus
15921 || cu->language == language_java
15922 || cu->language == language_ada)
15923 {
15924 /* The symbol's name is already allocated along
15925 with this objfile, so we don't need to
15926 duplicate it for the type. */
15927 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15928 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15929 }
63d06c5c
DC
15930 }
15931 }
c906108c
SS
15932 break;
15933 case DW_TAG_typedef:
63d06c5c
DC
15934 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15935 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15936 list_to_add = cu->list_in_scope;
63d06c5c 15937 break;
c906108c 15938 case DW_TAG_base_type:
a02abb62 15939 case DW_TAG_subrange_type:
c906108c 15940 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15941 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15942 list_to_add = cu->list_in_scope;
c906108c
SS
15943 break;
15944 case DW_TAG_enumerator:
e142c38c 15945 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15946 if (attr)
15947 {
e7c27a73 15948 dwarf2_const_value (attr, sym, cu);
c906108c 15949 }
63d06c5c
DC
15950 {
15951 /* NOTE: carlton/2003-11-10: See comment above in the
15952 DW_TAG_class_type, etc. block. */
15953
e142c38c 15954 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15955 && (cu->language == language_cplus
15956 || cu->language == language_java)
e142c38c 15957 ? &global_symbols : cu->list_in_scope);
63d06c5c 15958 }
c906108c 15959 break;
5c4e30ca
DC
15960 case DW_TAG_namespace:
15961 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15962 list_to_add = &global_symbols;
5c4e30ca 15963 break;
4357ac6c
TT
15964 case DW_TAG_common_block:
15965 SYMBOL_CLASS (sym) = LOC_STATIC;
15966 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15967 add_symbol_to_list (sym, cu->list_in_scope);
15968 break;
c906108c
SS
15969 default:
15970 /* Not a tag we recognize. Hopefully we aren't processing
15971 trash data, but since we must specifically ignore things
15972 we don't recognize, there is nothing else we should do at
0963b4bd 15973 this point. */
e2e0b3e5 15974 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 15975 dwarf_tag_name (die->tag));
c906108c
SS
15976 break;
15977 }
df8a16a1 15978
e37fd15a
SW
15979 if (suppress_add)
15980 {
15981 sym->hash_next = objfile->template_symbols;
15982 objfile->template_symbols = sym;
15983 list_to_add = NULL;
15984 }
15985
15986 if (list_to_add != NULL)
15987 add_symbol_to_list (sym, list_to_add);
15988
df8a16a1
DJ
15989 /* For the benefit of old versions of GCC, check for anonymous
15990 namespaces based on the demangled name. */
15991 if (!processing_has_namespace_info
94af9270 15992 && cu->language == language_cplus)
a10964d1 15993 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
15994 }
15995 return (sym);
15996}
15997
34eaf542
TT
15998/* A wrapper for new_symbol_full that always allocates a new symbol. */
15999
16000static struct symbol *
16001new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16002{
16003 return new_symbol_full (die, type, cu, NULL);
16004}
16005
98bfdba5
PA
16006/* Given an attr with a DW_FORM_dataN value in host byte order,
16007 zero-extend it as appropriate for the symbol's type. The DWARF
16008 standard (v4) is not entirely clear about the meaning of using
16009 DW_FORM_dataN for a constant with a signed type, where the type is
16010 wider than the data. The conclusion of a discussion on the DWARF
16011 list was that this is unspecified. We choose to always zero-extend
16012 because that is the interpretation long in use by GCC. */
c906108c 16013
98bfdba5
PA
16014static gdb_byte *
16015dwarf2_const_value_data (struct attribute *attr, struct type *type,
16016 const char *name, struct obstack *obstack,
12df843f 16017 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16018{
e7c27a73 16019 struct objfile *objfile = cu->objfile;
e17a4113
UW
16020 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16021 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16022 LONGEST l = DW_UNSND (attr);
16023
16024 if (bits < sizeof (*value) * 8)
16025 {
16026 l &= ((LONGEST) 1 << bits) - 1;
16027 *value = l;
16028 }
16029 else if (bits == sizeof (*value) * 8)
16030 *value = l;
16031 else
16032 {
16033 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16034 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16035 return bytes;
16036 }
16037
16038 return NULL;
16039}
16040
16041/* Read a constant value from an attribute. Either set *VALUE, or if
16042 the value does not fit in *VALUE, set *BYTES - either already
16043 allocated on the objfile obstack, or newly allocated on OBSTACK,
16044 or, set *BATON, if we translated the constant to a location
16045 expression. */
16046
16047static void
16048dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16049 const char *name, struct obstack *obstack,
16050 struct dwarf2_cu *cu,
12df843f 16051 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16052 struct dwarf2_locexpr_baton **baton)
16053{
16054 struct objfile *objfile = cu->objfile;
16055 struct comp_unit_head *cu_header = &cu->header;
c906108c 16056 struct dwarf_block *blk;
98bfdba5
PA
16057 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16058 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16059
16060 *value = 0;
16061 *bytes = NULL;
16062 *baton = NULL;
c906108c
SS
16063
16064 switch (attr->form)
16065 {
16066 case DW_FORM_addr:
3019eac3 16067 case DW_FORM_GNU_addr_index:
ac56253d 16068 {
ac56253d
TT
16069 gdb_byte *data;
16070
98bfdba5
PA
16071 if (TYPE_LENGTH (type) != cu_header->addr_size)
16072 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16073 cu_header->addr_size,
98bfdba5 16074 TYPE_LENGTH (type));
ac56253d
TT
16075 /* Symbols of this form are reasonably rare, so we just
16076 piggyback on the existing location code rather than writing
16077 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16078 *baton = obstack_alloc (&objfile->objfile_obstack,
16079 sizeof (struct dwarf2_locexpr_baton));
16080 (*baton)->per_cu = cu->per_cu;
16081 gdb_assert ((*baton)->per_cu);
ac56253d 16082
98bfdba5
PA
16083 (*baton)->size = 2 + cu_header->addr_size;
16084 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16085 (*baton)->data = data;
ac56253d
TT
16086
16087 data[0] = DW_OP_addr;
16088 store_unsigned_integer (&data[1], cu_header->addr_size,
16089 byte_order, DW_ADDR (attr));
16090 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16091 }
c906108c 16092 break;
4ac36638 16093 case DW_FORM_string:
93b5768b 16094 case DW_FORM_strp:
3019eac3 16095 case DW_FORM_GNU_str_index:
36586728 16096 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16097 /* DW_STRING is already allocated on the objfile obstack, point
16098 directly to it. */
16099 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16100 break;
c906108c
SS
16101 case DW_FORM_block1:
16102 case DW_FORM_block2:
16103 case DW_FORM_block4:
16104 case DW_FORM_block:
2dc7f7b3 16105 case DW_FORM_exprloc:
c906108c 16106 blk = DW_BLOCK (attr);
98bfdba5
PA
16107 if (TYPE_LENGTH (type) != blk->size)
16108 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16109 TYPE_LENGTH (type));
16110 *bytes = blk->data;
c906108c 16111 break;
2df3850c
JM
16112
16113 /* The DW_AT_const_value attributes are supposed to carry the
16114 symbol's value "represented as it would be on the target
16115 architecture." By the time we get here, it's already been
16116 converted to host endianness, so we just need to sign- or
16117 zero-extend it as appropriate. */
16118 case DW_FORM_data1:
3e43a32a
MS
16119 *bytes = dwarf2_const_value_data (attr, type, name,
16120 obstack, cu, value, 8);
2df3850c 16121 break;
c906108c 16122 case DW_FORM_data2:
3e43a32a
MS
16123 *bytes = dwarf2_const_value_data (attr, type, name,
16124 obstack, cu, value, 16);
2df3850c 16125 break;
c906108c 16126 case DW_FORM_data4:
3e43a32a
MS
16127 *bytes = dwarf2_const_value_data (attr, type, name,
16128 obstack, cu, value, 32);
2df3850c 16129 break;
c906108c 16130 case DW_FORM_data8:
3e43a32a
MS
16131 *bytes = dwarf2_const_value_data (attr, type, name,
16132 obstack, cu, value, 64);
2df3850c
JM
16133 break;
16134
c906108c 16135 case DW_FORM_sdata:
98bfdba5 16136 *value = DW_SND (attr);
2df3850c
JM
16137 break;
16138
c906108c 16139 case DW_FORM_udata:
98bfdba5 16140 *value = DW_UNSND (attr);
c906108c 16141 break;
2df3850c 16142
c906108c 16143 default:
4d3c2250 16144 complaint (&symfile_complaints,
e2e0b3e5 16145 _("unsupported const value attribute form: '%s'"),
4d3c2250 16146 dwarf_form_name (attr->form));
98bfdba5 16147 *value = 0;
c906108c
SS
16148 break;
16149 }
16150}
16151
2df3850c 16152
98bfdba5
PA
16153/* Copy constant value from an attribute to a symbol. */
16154
2df3850c 16155static void
98bfdba5
PA
16156dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16157 struct dwarf2_cu *cu)
2df3850c 16158{
98bfdba5
PA
16159 struct objfile *objfile = cu->objfile;
16160 struct comp_unit_head *cu_header = &cu->header;
12df843f 16161 LONGEST value;
98bfdba5
PA
16162 gdb_byte *bytes;
16163 struct dwarf2_locexpr_baton *baton;
2df3850c 16164
98bfdba5
PA
16165 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16166 SYMBOL_PRINT_NAME (sym),
16167 &objfile->objfile_obstack, cu,
16168 &value, &bytes, &baton);
2df3850c 16169
98bfdba5
PA
16170 if (baton != NULL)
16171 {
16172 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16173 SYMBOL_LOCATION_BATON (sym) = baton;
16174 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16175 }
16176 else if (bytes != NULL)
16177 {
16178 SYMBOL_VALUE_BYTES (sym) = bytes;
16179 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16180 }
16181 else
16182 {
16183 SYMBOL_VALUE (sym) = value;
16184 SYMBOL_CLASS (sym) = LOC_CONST;
16185 }
2df3850c
JM
16186}
16187
c906108c
SS
16188/* Return the type of the die in question using its DW_AT_type attribute. */
16189
16190static struct type *
e7c27a73 16191die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16192{
c906108c 16193 struct attribute *type_attr;
c906108c 16194
e142c38c 16195 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16196 if (!type_attr)
16197 {
16198 /* A missing DW_AT_type represents a void type. */
46bf5051 16199 return objfile_type (cu->objfile)->builtin_void;
c906108c 16200 }
348e048f 16201
673bfd45 16202 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16203}
16204
b4ba55a1
JB
16205/* True iff CU's producer generates GNAT Ada auxiliary information
16206 that allows to find parallel types through that information instead
16207 of having to do expensive parallel lookups by type name. */
16208
16209static int
16210need_gnat_info (struct dwarf2_cu *cu)
16211{
16212 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16213 of GNAT produces this auxiliary information, without any indication
16214 that it is produced. Part of enhancing the FSF version of GNAT
16215 to produce that information will be to put in place an indicator
16216 that we can use in order to determine whether the descriptive type
16217 info is available or not. One suggestion that has been made is
16218 to use a new attribute, attached to the CU die. For now, assume
16219 that the descriptive type info is not available. */
16220 return 0;
16221}
16222
b4ba55a1
JB
16223/* Return the auxiliary type of the die in question using its
16224 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16225 attribute is not present. */
16226
16227static struct type *
16228die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16229{
b4ba55a1 16230 struct attribute *type_attr;
b4ba55a1
JB
16231
16232 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16233 if (!type_attr)
16234 return NULL;
16235
673bfd45 16236 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16237}
16238
16239/* If DIE has a descriptive_type attribute, then set the TYPE's
16240 descriptive type accordingly. */
16241
16242static void
16243set_descriptive_type (struct type *type, struct die_info *die,
16244 struct dwarf2_cu *cu)
16245{
16246 struct type *descriptive_type = die_descriptive_type (die, cu);
16247
16248 if (descriptive_type)
16249 {
16250 ALLOCATE_GNAT_AUX_TYPE (type);
16251 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16252 }
16253}
16254
c906108c
SS
16255/* Return the containing type of the die in question using its
16256 DW_AT_containing_type attribute. */
16257
16258static struct type *
e7c27a73 16259die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16260{
c906108c 16261 struct attribute *type_attr;
c906108c 16262
e142c38c 16263 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16264 if (!type_attr)
16265 error (_("Dwarf Error: Problem turning containing type into gdb type "
16266 "[in module %s]"), cu->objfile->name);
16267
673bfd45 16268 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16269}
16270
673bfd45
DE
16271/* Look up the type of DIE in CU using its type attribute ATTR.
16272 If there is no type substitute an error marker. */
16273
c906108c 16274static struct type *
673bfd45
DE
16275lookup_die_type (struct die_info *die, struct attribute *attr,
16276 struct dwarf2_cu *cu)
c906108c 16277{
bb5ed363 16278 struct objfile *objfile = cu->objfile;
f792889a
DJ
16279 struct type *this_type;
16280
673bfd45
DE
16281 /* First see if we have it cached. */
16282
36586728
TT
16283 if (attr->form == DW_FORM_GNU_ref_alt)
16284 {
16285 struct dwarf2_per_cu_data *per_cu;
16286 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16287
16288 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16289 this_type = get_die_type_at_offset (offset, per_cu);
16290 }
16291 else if (is_ref_attr (attr))
673bfd45 16292 {
b64f50a1 16293 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16294
16295 this_type = get_die_type_at_offset (offset, cu->per_cu);
16296 }
55f1336d 16297 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16298 {
16299 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16300
16301 /* sig_type will be NULL if the signatured type is missing from
16302 the debug info. */
16303 if (sig_type == NULL)
16304 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16305 "at 0x%x [in module %s]"),
b64f50a1 16306 die->offset.sect_off, objfile->name);
673bfd45 16307
3019eac3
DE
16308 gdb_assert (sig_type->per_cu.is_debug_types);
16309 /* If we haven't filled in type_offset_in_section yet, then we
16310 haven't read the type in yet. */
16311 this_type = NULL;
16312 if (sig_type->type_offset_in_section.sect_off != 0)
16313 {
16314 this_type =
16315 get_die_type_at_offset (sig_type->type_offset_in_section,
16316 &sig_type->per_cu);
16317 }
673bfd45
DE
16318 }
16319 else
16320 {
16321 dump_die_for_error (die);
16322 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16323 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16324 }
16325
16326 /* If not cached we need to read it in. */
16327
16328 if (this_type == NULL)
16329 {
16330 struct die_info *type_die;
16331 struct dwarf2_cu *type_cu = cu;
16332
16333 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16334 /* If we found the type now, it's probably because the type came
16335 from an inter-CU reference and the type's CU got expanded before
16336 ours. */
16337 this_type = get_die_type (type_die, type_cu);
16338 if (this_type == NULL)
16339 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16340 }
16341
16342 /* If we still don't have a type use an error marker. */
16343
16344 if (this_type == NULL)
c906108c 16345 {
b00fdb78
TT
16346 char *message, *saved;
16347
16348 /* read_type_die already issued a complaint. */
16349 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16350 objfile->name,
b64f50a1
JK
16351 cu->header.offset.sect_off,
16352 die->offset.sect_off);
bb5ed363 16353 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16354 message, strlen (message));
16355 xfree (message);
16356
bb5ed363 16357 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16358 }
673bfd45 16359
f792889a 16360 return this_type;
c906108c
SS
16361}
16362
673bfd45
DE
16363/* Return the type in DIE, CU.
16364 Returns NULL for invalid types.
16365
16366 This first does a lookup in the appropriate type_hash table,
16367 and only reads the die in if necessary.
16368
16369 NOTE: This can be called when reading in partial or full symbols. */
16370
f792889a 16371static struct type *
e7c27a73 16372read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16373{
f792889a
DJ
16374 struct type *this_type;
16375
16376 this_type = get_die_type (die, cu);
16377 if (this_type)
16378 return this_type;
16379
673bfd45
DE
16380 return read_type_die_1 (die, cu);
16381}
16382
16383/* Read the type in DIE, CU.
16384 Returns NULL for invalid types. */
16385
16386static struct type *
16387read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16388{
16389 struct type *this_type = NULL;
16390
c906108c
SS
16391 switch (die->tag)
16392 {
16393 case DW_TAG_class_type:
680b30c7 16394 case DW_TAG_interface_type:
c906108c
SS
16395 case DW_TAG_structure_type:
16396 case DW_TAG_union_type:
f792889a 16397 this_type = read_structure_type (die, cu);
c906108c
SS
16398 break;
16399 case DW_TAG_enumeration_type:
f792889a 16400 this_type = read_enumeration_type (die, cu);
c906108c
SS
16401 break;
16402 case DW_TAG_subprogram:
16403 case DW_TAG_subroutine_type:
edb3359d 16404 case DW_TAG_inlined_subroutine:
f792889a 16405 this_type = read_subroutine_type (die, cu);
c906108c
SS
16406 break;
16407 case DW_TAG_array_type:
f792889a 16408 this_type = read_array_type (die, cu);
c906108c 16409 break;
72019c9c 16410 case DW_TAG_set_type:
f792889a 16411 this_type = read_set_type (die, cu);
72019c9c 16412 break;
c906108c 16413 case DW_TAG_pointer_type:
f792889a 16414 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16415 break;
16416 case DW_TAG_ptr_to_member_type:
f792889a 16417 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16418 break;
16419 case DW_TAG_reference_type:
f792889a 16420 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16421 break;
16422 case DW_TAG_const_type:
f792889a 16423 this_type = read_tag_const_type (die, cu);
c906108c
SS
16424 break;
16425 case DW_TAG_volatile_type:
f792889a 16426 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
16427 break;
16428 case DW_TAG_string_type:
f792889a 16429 this_type = read_tag_string_type (die, cu);
c906108c
SS
16430 break;
16431 case DW_TAG_typedef:
f792889a 16432 this_type = read_typedef (die, cu);
c906108c 16433 break;
a02abb62 16434 case DW_TAG_subrange_type:
f792889a 16435 this_type = read_subrange_type (die, cu);
a02abb62 16436 break;
c906108c 16437 case DW_TAG_base_type:
f792889a 16438 this_type = read_base_type (die, cu);
c906108c 16439 break;
81a17f79 16440 case DW_TAG_unspecified_type:
f792889a 16441 this_type = read_unspecified_type (die, cu);
81a17f79 16442 break;
0114d602
DJ
16443 case DW_TAG_namespace:
16444 this_type = read_namespace_type (die, cu);
16445 break;
f55ee35c
JK
16446 case DW_TAG_module:
16447 this_type = read_module_type (die, cu);
16448 break;
c906108c 16449 default:
3e43a32a
MS
16450 complaint (&symfile_complaints,
16451 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16452 dwarf_tag_name (die->tag));
c906108c
SS
16453 break;
16454 }
63d06c5c 16455
f792889a 16456 return this_type;
63d06c5c
DC
16457}
16458
abc72ce4
DE
16459/* See if we can figure out if the class lives in a namespace. We do
16460 this by looking for a member function; its demangled name will
16461 contain namespace info, if there is any.
16462 Return the computed name or NULL.
16463 Space for the result is allocated on the objfile's obstack.
16464 This is the full-die version of guess_partial_die_structure_name.
16465 In this case we know DIE has no useful parent. */
16466
16467static char *
16468guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16469{
16470 struct die_info *spec_die;
16471 struct dwarf2_cu *spec_cu;
16472 struct die_info *child;
16473
16474 spec_cu = cu;
16475 spec_die = die_specification (die, &spec_cu);
16476 if (spec_die != NULL)
16477 {
16478 die = spec_die;
16479 cu = spec_cu;
16480 }
16481
16482 for (child = die->child;
16483 child != NULL;
16484 child = child->sibling)
16485 {
16486 if (child->tag == DW_TAG_subprogram)
16487 {
16488 struct attribute *attr;
16489
16490 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16491 if (attr == NULL)
16492 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16493 if (attr != NULL)
16494 {
16495 char *actual_name
16496 = language_class_name_from_physname (cu->language_defn,
16497 DW_STRING (attr));
16498 char *name = NULL;
16499
16500 if (actual_name != NULL)
16501 {
16502 char *die_name = dwarf2_name (die, cu);
16503
16504 if (die_name != NULL
16505 && strcmp (die_name, actual_name) != 0)
16506 {
16507 /* Strip off the class name from the full name.
16508 We want the prefix. */
16509 int die_name_len = strlen (die_name);
16510 int actual_name_len = strlen (actual_name);
16511
16512 /* Test for '::' as a sanity check. */
16513 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16514 && actual_name[actual_name_len
16515 - die_name_len - 1] == ':')
abc72ce4
DE
16516 name =
16517 obsavestring (actual_name,
16518 actual_name_len - die_name_len - 2,
16519 &cu->objfile->objfile_obstack);
16520 }
16521 }
16522 xfree (actual_name);
16523 return name;
16524 }
16525 }
16526 }
16527
16528 return NULL;
16529}
16530
96408a79
SA
16531/* GCC might emit a nameless typedef that has a linkage name. Determine the
16532 prefix part in such case. See
16533 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16534
16535static char *
16536anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16537{
16538 struct attribute *attr;
16539 char *base;
16540
16541 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16542 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16543 return NULL;
16544
16545 attr = dwarf2_attr (die, DW_AT_name, cu);
16546 if (attr != NULL && DW_STRING (attr) != NULL)
16547 return NULL;
16548
16549 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16550 if (attr == NULL)
16551 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16552 if (attr == NULL || DW_STRING (attr) == NULL)
16553 return NULL;
16554
16555 /* dwarf2_name had to be already called. */
16556 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16557
16558 /* Strip the base name, keep any leading namespaces/classes. */
16559 base = strrchr (DW_STRING (attr), ':');
16560 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16561 return "";
16562
16563 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16564 &cu->objfile->objfile_obstack);
16565}
16566
fdde2d81 16567/* Return the name of the namespace/class that DIE is defined within,
0114d602 16568 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16569
0114d602
DJ
16570 For example, if we're within the method foo() in the following
16571 code:
16572
16573 namespace N {
16574 class C {
16575 void foo () {
16576 }
16577 };
16578 }
16579
16580 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16581
0d5cff50 16582static const char *
e142c38c 16583determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16584{
0114d602
DJ
16585 struct die_info *parent, *spec_die;
16586 struct dwarf2_cu *spec_cu;
16587 struct type *parent_type;
96408a79 16588 char *retval;
63d06c5c 16589
f55ee35c
JK
16590 if (cu->language != language_cplus && cu->language != language_java
16591 && cu->language != language_fortran)
0114d602
DJ
16592 return "";
16593
96408a79
SA
16594 retval = anonymous_struct_prefix (die, cu);
16595 if (retval)
16596 return retval;
16597
0114d602
DJ
16598 /* We have to be careful in the presence of DW_AT_specification.
16599 For example, with GCC 3.4, given the code
16600
16601 namespace N {
16602 void foo() {
16603 // Definition of N::foo.
16604 }
16605 }
16606
16607 then we'll have a tree of DIEs like this:
16608
16609 1: DW_TAG_compile_unit
16610 2: DW_TAG_namespace // N
16611 3: DW_TAG_subprogram // declaration of N::foo
16612 4: DW_TAG_subprogram // definition of N::foo
16613 DW_AT_specification // refers to die #3
16614
16615 Thus, when processing die #4, we have to pretend that we're in
16616 the context of its DW_AT_specification, namely the contex of die
16617 #3. */
16618 spec_cu = cu;
16619 spec_die = die_specification (die, &spec_cu);
16620 if (spec_die == NULL)
16621 parent = die->parent;
16622 else
63d06c5c 16623 {
0114d602
DJ
16624 parent = spec_die->parent;
16625 cu = spec_cu;
63d06c5c 16626 }
0114d602
DJ
16627
16628 if (parent == NULL)
16629 return "";
98bfdba5
PA
16630 else if (parent->building_fullname)
16631 {
16632 const char *name;
16633 const char *parent_name;
16634
16635 /* It has been seen on RealView 2.2 built binaries,
16636 DW_TAG_template_type_param types actually _defined_ as
16637 children of the parent class:
16638
16639 enum E {};
16640 template class <class Enum> Class{};
16641 Class<enum E> class_e;
16642
16643 1: DW_TAG_class_type (Class)
16644 2: DW_TAG_enumeration_type (E)
16645 3: DW_TAG_enumerator (enum1:0)
16646 3: DW_TAG_enumerator (enum2:1)
16647 ...
16648 2: DW_TAG_template_type_param
16649 DW_AT_type DW_FORM_ref_udata (E)
16650
16651 Besides being broken debug info, it can put GDB into an
16652 infinite loop. Consider:
16653
16654 When we're building the full name for Class<E>, we'll start
16655 at Class, and go look over its template type parameters,
16656 finding E. We'll then try to build the full name of E, and
16657 reach here. We're now trying to build the full name of E,
16658 and look over the parent DIE for containing scope. In the
16659 broken case, if we followed the parent DIE of E, we'd again
16660 find Class, and once again go look at its template type
16661 arguments, etc., etc. Simply don't consider such parent die
16662 as source-level parent of this die (it can't be, the language
16663 doesn't allow it), and break the loop here. */
16664 name = dwarf2_name (die, cu);
16665 parent_name = dwarf2_name (parent, cu);
16666 complaint (&symfile_complaints,
16667 _("template param type '%s' defined within parent '%s'"),
16668 name ? name : "<unknown>",
16669 parent_name ? parent_name : "<unknown>");
16670 return "";
16671 }
63d06c5c 16672 else
0114d602
DJ
16673 switch (parent->tag)
16674 {
63d06c5c 16675 case DW_TAG_namespace:
0114d602 16676 parent_type = read_type_die (parent, cu);
acebe513
UW
16677 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16678 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16679 Work around this problem here. */
16680 if (cu->language == language_cplus
16681 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16682 return "";
0114d602
DJ
16683 /* We give a name to even anonymous namespaces. */
16684 return TYPE_TAG_NAME (parent_type);
63d06c5c 16685 case DW_TAG_class_type:
680b30c7 16686 case DW_TAG_interface_type:
63d06c5c 16687 case DW_TAG_structure_type:
0114d602 16688 case DW_TAG_union_type:
f55ee35c 16689 case DW_TAG_module:
0114d602
DJ
16690 parent_type = read_type_die (parent, cu);
16691 if (TYPE_TAG_NAME (parent_type) != NULL)
16692 return TYPE_TAG_NAME (parent_type);
16693 else
16694 /* An anonymous structure is only allowed non-static data
16695 members; no typedefs, no member functions, et cetera.
16696 So it does not need a prefix. */
16697 return "";
abc72ce4 16698 case DW_TAG_compile_unit:
95554aad 16699 case DW_TAG_partial_unit:
abc72ce4
DE
16700 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16701 if (cu->language == language_cplus
8b70b953 16702 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16703 && die->child != NULL
16704 && (die->tag == DW_TAG_class_type
16705 || die->tag == DW_TAG_structure_type
16706 || die->tag == DW_TAG_union_type))
16707 {
16708 char *name = guess_full_die_structure_name (die, cu);
16709 if (name != NULL)
16710 return name;
16711 }
16712 return "";
63d06c5c 16713 default:
8176b9b8 16714 return determine_prefix (parent, cu);
63d06c5c 16715 }
63d06c5c
DC
16716}
16717
3e43a32a
MS
16718/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16719 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16720 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16721 an obconcat, otherwise allocate storage for the result. The CU argument is
16722 used to determine the language and hence, the appropriate separator. */
987504bb 16723
f55ee35c 16724#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16725
16726static char *
f55ee35c
JK
16727typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16728 int physname, struct dwarf2_cu *cu)
63d06c5c 16729{
f55ee35c 16730 const char *lead = "";
5c315b68 16731 const char *sep;
63d06c5c 16732
3e43a32a
MS
16733 if (suffix == NULL || suffix[0] == '\0'
16734 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16735 sep = "";
16736 else if (cu->language == language_java)
16737 sep = ".";
f55ee35c
JK
16738 else if (cu->language == language_fortran && physname)
16739 {
16740 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16741 DW_AT_MIPS_linkage_name is preferred and used instead. */
16742
16743 lead = "__";
16744 sep = "_MOD_";
16745 }
987504bb
JJ
16746 else
16747 sep = "::";
63d06c5c 16748
6dd47d34
DE
16749 if (prefix == NULL)
16750 prefix = "";
16751 if (suffix == NULL)
16752 suffix = "";
16753
987504bb
JJ
16754 if (obs == NULL)
16755 {
3e43a32a
MS
16756 char *retval
16757 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16758
f55ee35c
JK
16759 strcpy (retval, lead);
16760 strcat (retval, prefix);
6dd47d34
DE
16761 strcat (retval, sep);
16762 strcat (retval, suffix);
63d06c5c
DC
16763 return retval;
16764 }
987504bb
JJ
16765 else
16766 {
16767 /* We have an obstack. */
f55ee35c 16768 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16769 }
63d06c5c
DC
16770}
16771
c906108c
SS
16772/* Return sibling of die, NULL if no sibling. */
16773
f9aca02d 16774static struct die_info *
fba45db2 16775sibling_die (struct die_info *die)
c906108c 16776{
639d11d3 16777 return die->sibling;
c906108c
SS
16778}
16779
71c25dea
TT
16780/* Get name of a die, return NULL if not found. */
16781
16782static char *
16783dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16784 struct obstack *obstack)
16785{
16786 if (name && cu->language == language_cplus)
16787 {
16788 char *canon_name = cp_canonicalize_string (name);
16789
16790 if (canon_name != NULL)
16791 {
16792 if (strcmp (canon_name, name) != 0)
16793 name = obsavestring (canon_name, strlen (canon_name),
16794 obstack);
16795 xfree (canon_name);
16796 }
16797 }
16798
16799 return name;
c906108c
SS
16800}
16801
9219021c
DC
16802/* Get name of a die, return NULL if not found. */
16803
16804static char *
e142c38c 16805dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16806{
16807 struct attribute *attr;
16808
e142c38c 16809 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16810 if ((!attr || !DW_STRING (attr))
16811 && die->tag != DW_TAG_class_type
16812 && die->tag != DW_TAG_interface_type
16813 && die->tag != DW_TAG_structure_type
16814 && die->tag != DW_TAG_union_type)
71c25dea
TT
16815 return NULL;
16816
16817 switch (die->tag)
16818 {
16819 case DW_TAG_compile_unit:
95554aad 16820 case DW_TAG_partial_unit:
71c25dea
TT
16821 /* Compilation units have a DW_AT_name that is a filename, not
16822 a source language identifier. */
16823 case DW_TAG_enumeration_type:
16824 case DW_TAG_enumerator:
16825 /* These tags always have simple identifiers already; no need
16826 to canonicalize them. */
16827 return DW_STRING (attr);
907af001 16828
418835cc
KS
16829 case DW_TAG_subprogram:
16830 /* Java constructors will all be named "<init>", so return
16831 the class name when we see this special case. */
16832 if (cu->language == language_java
16833 && DW_STRING (attr) != NULL
16834 && strcmp (DW_STRING (attr), "<init>") == 0)
16835 {
16836 struct dwarf2_cu *spec_cu = cu;
16837 struct die_info *spec_die;
16838
16839 /* GCJ will output '<init>' for Java constructor names.
16840 For this special case, return the name of the parent class. */
16841
16842 /* GCJ may output suprogram DIEs with AT_specification set.
16843 If so, use the name of the specified DIE. */
16844 spec_die = die_specification (die, &spec_cu);
16845 if (spec_die != NULL)
16846 return dwarf2_name (spec_die, spec_cu);
16847
16848 do
16849 {
16850 die = die->parent;
16851 if (die->tag == DW_TAG_class_type)
16852 return dwarf2_name (die, cu);
16853 }
95554aad
TT
16854 while (die->tag != DW_TAG_compile_unit
16855 && die->tag != DW_TAG_partial_unit);
418835cc 16856 }
907af001
UW
16857 break;
16858
16859 case DW_TAG_class_type:
16860 case DW_TAG_interface_type:
16861 case DW_TAG_structure_type:
16862 case DW_TAG_union_type:
16863 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16864 structures or unions. These were of the form "._%d" in GCC 4.1,
16865 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16866 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16867 if (attr && DW_STRING (attr)
16868 && (strncmp (DW_STRING (attr), "._", 2) == 0
16869 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16870 return NULL;
53832f31
TT
16871
16872 /* GCC might emit a nameless typedef that has a linkage name. See
16873 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16874 if (!attr || DW_STRING (attr) == NULL)
16875 {
df5c6c50 16876 char *demangled = NULL;
53832f31
TT
16877
16878 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16879 if (attr == NULL)
16880 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16881
16882 if (attr == NULL || DW_STRING (attr) == NULL)
16883 return NULL;
16884
df5c6c50
JK
16885 /* Avoid demangling DW_STRING (attr) the second time on a second
16886 call for the same DIE. */
16887 if (!DW_STRING_IS_CANONICAL (attr))
16888 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16889
16890 if (demangled)
16891 {
96408a79
SA
16892 char *base;
16893
53832f31 16894 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16895 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16896 &cu->objfile->objfile_obstack);
53832f31
TT
16897 DW_STRING_IS_CANONICAL (attr) = 1;
16898 xfree (demangled);
96408a79
SA
16899
16900 /* Strip any leading namespaces/classes, keep only the base name.
16901 DW_AT_name for named DIEs does not contain the prefixes. */
16902 base = strrchr (DW_STRING (attr), ':');
16903 if (base && base > DW_STRING (attr) && base[-1] == ':')
16904 return &base[1];
16905 else
16906 return DW_STRING (attr);
53832f31
TT
16907 }
16908 }
907af001
UW
16909 break;
16910
71c25dea 16911 default:
907af001
UW
16912 break;
16913 }
16914
16915 if (!DW_STRING_IS_CANONICAL (attr))
16916 {
16917 DW_STRING (attr)
16918 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16919 &cu->objfile->objfile_obstack);
16920 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16921 }
907af001 16922 return DW_STRING (attr);
9219021c
DC
16923}
16924
16925/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16926 is none. *EXT_CU is the CU containing DIE on input, and the CU
16927 containing the return value on output. */
9219021c
DC
16928
16929static struct die_info *
f2f0e013 16930dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16931{
16932 struct attribute *attr;
9219021c 16933
f2f0e013 16934 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16935 if (attr == NULL)
16936 return NULL;
16937
f2f0e013 16938 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16939}
16940
c906108c
SS
16941/* Convert a DIE tag into its string name. */
16942
f39c6ffd 16943static const char *
aa1ee363 16944dwarf_tag_name (unsigned tag)
c906108c 16945{
f39c6ffd
TT
16946 const char *name = get_DW_TAG_name (tag);
16947
16948 if (name == NULL)
16949 return "DW_TAG_<unknown>";
16950
16951 return name;
c906108c
SS
16952}
16953
16954/* Convert a DWARF attribute code into its string name. */
16955
f39c6ffd 16956static const char *
aa1ee363 16957dwarf_attr_name (unsigned attr)
c906108c 16958{
f39c6ffd
TT
16959 const char *name;
16960
c764a876 16961#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16962 if (attr == DW_AT_MIPS_fde)
16963 return "DW_AT_MIPS_fde";
16964#else
16965 if (attr == DW_AT_HP_block_index)
16966 return "DW_AT_HP_block_index";
c764a876 16967#endif
f39c6ffd
TT
16968
16969 name = get_DW_AT_name (attr);
16970
16971 if (name == NULL)
16972 return "DW_AT_<unknown>";
16973
16974 return name;
c906108c
SS
16975}
16976
16977/* Convert a DWARF value form code into its string name. */
16978
f39c6ffd 16979static const char *
aa1ee363 16980dwarf_form_name (unsigned form)
c906108c 16981{
f39c6ffd
TT
16982 const char *name = get_DW_FORM_name (form);
16983
16984 if (name == NULL)
16985 return "DW_FORM_<unknown>";
16986
16987 return name;
c906108c
SS
16988}
16989
16990static char *
fba45db2 16991dwarf_bool_name (unsigned mybool)
c906108c
SS
16992{
16993 if (mybool)
16994 return "TRUE";
16995 else
16996 return "FALSE";
16997}
16998
16999/* Convert a DWARF type code into its string name. */
17000
f39c6ffd 17001static const char *
aa1ee363 17002dwarf_type_encoding_name (unsigned enc)
c906108c 17003{
f39c6ffd 17004 const char *name = get_DW_ATE_name (enc);
c906108c 17005
f39c6ffd
TT
17006 if (name == NULL)
17007 return "DW_ATE_<unknown>";
c906108c 17008
f39c6ffd 17009 return name;
c906108c 17010}
c906108c 17011
f9aca02d 17012static void
d97bc12b 17013dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17014{
17015 unsigned int i;
17016
d97bc12b
DE
17017 print_spaces (indent, f);
17018 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17019 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17020
17021 if (die->parent != NULL)
17022 {
17023 print_spaces (indent, f);
17024 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17025 die->parent->offset.sect_off);
d97bc12b
DE
17026 }
17027
17028 print_spaces (indent, f);
17029 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17030 dwarf_bool_name (die->child != NULL));
c906108c 17031
d97bc12b
DE
17032 print_spaces (indent, f);
17033 fprintf_unfiltered (f, " attributes:\n");
17034
c906108c
SS
17035 for (i = 0; i < die->num_attrs; ++i)
17036 {
d97bc12b
DE
17037 print_spaces (indent, f);
17038 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17039 dwarf_attr_name (die->attrs[i].name),
17040 dwarf_form_name (die->attrs[i].form));
d97bc12b 17041
c906108c
SS
17042 switch (die->attrs[i].form)
17043 {
c906108c 17044 case DW_FORM_addr:
3019eac3 17045 case DW_FORM_GNU_addr_index:
d97bc12b 17046 fprintf_unfiltered (f, "address: ");
5af949e3 17047 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17048 break;
17049 case DW_FORM_block2:
17050 case DW_FORM_block4:
17051 case DW_FORM_block:
17052 case DW_FORM_block1:
56eb65bd
SP
17053 fprintf_unfiltered (f, "block: size %s",
17054 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17055 break;
2dc7f7b3 17056 case DW_FORM_exprloc:
56eb65bd
SP
17057 fprintf_unfiltered (f, "expression: size %s",
17058 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17059 break;
4568ecf9
DE
17060 case DW_FORM_ref_addr:
17061 fprintf_unfiltered (f, "ref address: ");
17062 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17063 break;
36586728
TT
17064 case DW_FORM_GNU_ref_alt:
17065 fprintf_unfiltered (f, "alt ref address: ");
17066 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17067 break;
10b3939b
DJ
17068 case DW_FORM_ref1:
17069 case DW_FORM_ref2:
17070 case DW_FORM_ref4:
4568ecf9
DE
17071 case DW_FORM_ref8:
17072 case DW_FORM_ref_udata:
d97bc12b 17073 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17074 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17075 break;
c906108c
SS
17076 case DW_FORM_data1:
17077 case DW_FORM_data2:
17078 case DW_FORM_data4:
ce5d95e1 17079 case DW_FORM_data8:
c906108c
SS
17080 case DW_FORM_udata:
17081 case DW_FORM_sdata:
43bbcdc2
PH
17082 fprintf_unfiltered (f, "constant: %s",
17083 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17084 break;
2dc7f7b3
TT
17085 case DW_FORM_sec_offset:
17086 fprintf_unfiltered (f, "section offset: %s",
17087 pulongest (DW_UNSND (&die->attrs[i])));
17088 break;
55f1336d 17089 case DW_FORM_ref_sig8:
348e048f
DE
17090 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17091 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17092 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17093 else
17094 fprintf_unfiltered (f, "signatured type, offset: unknown");
17095 break;
c906108c 17096 case DW_FORM_string:
4bdf3d34 17097 case DW_FORM_strp:
3019eac3 17098 case DW_FORM_GNU_str_index:
36586728 17099 case DW_FORM_GNU_strp_alt:
8285870a 17100 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17101 DW_STRING (&die->attrs[i])
8285870a
JK
17102 ? DW_STRING (&die->attrs[i]) : "",
17103 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17104 break;
17105 case DW_FORM_flag:
17106 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17107 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17108 else
d97bc12b 17109 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17110 break;
2dc7f7b3
TT
17111 case DW_FORM_flag_present:
17112 fprintf_unfiltered (f, "flag: TRUE");
17113 break;
a8329558 17114 case DW_FORM_indirect:
0963b4bd
MS
17115 /* The reader will have reduced the indirect form to
17116 the "base form" so this form should not occur. */
3e43a32a
MS
17117 fprintf_unfiltered (f,
17118 "unexpected attribute form: DW_FORM_indirect");
a8329558 17119 break;
c906108c 17120 default:
d97bc12b 17121 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17122 die->attrs[i].form);
d97bc12b 17123 break;
c906108c 17124 }
d97bc12b 17125 fprintf_unfiltered (f, "\n");
c906108c
SS
17126 }
17127}
17128
f9aca02d 17129static void
d97bc12b 17130dump_die_for_error (struct die_info *die)
c906108c 17131{
d97bc12b
DE
17132 dump_die_shallow (gdb_stderr, 0, die);
17133}
17134
17135static void
17136dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17137{
17138 int indent = level * 4;
17139
17140 gdb_assert (die != NULL);
17141
17142 if (level >= max_level)
17143 return;
17144
17145 dump_die_shallow (f, indent, die);
17146
17147 if (die->child != NULL)
c906108c 17148 {
d97bc12b
DE
17149 print_spaces (indent, f);
17150 fprintf_unfiltered (f, " Children:");
17151 if (level + 1 < max_level)
17152 {
17153 fprintf_unfiltered (f, "\n");
17154 dump_die_1 (f, level + 1, max_level, die->child);
17155 }
17156 else
17157 {
3e43a32a
MS
17158 fprintf_unfiltered (f,
17159 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17160 }
17161 }
17162
17163 if (die->sibling != NULL && level > 0)
17164 {
17165 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17166 }
17167}
17168
d97bc12b
DE
17169/* This is called from the pdie macro in gdbinit.in.
17170 It's not static so gcc will keep a copy callable from gdb. */
17171
17172void
17173dump_die (struct die_info *die, int max_level)
17174{
17175 dump_die_1 (gdb_stdlog, 0, max_level, die);
17176}
17177
f9aca02d 17178static void
51545339 17179store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17180{
51545339 17181 void **slot;
c906108c 17182
b64f50a1
JK
17183 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17184 INSERT);
51545339
DJ
17185
17186 *slot = die;
c906108c
SS
17187}
17188
b64f50a1
JK
17189/* DW_ADDR is always stored already as sect_offset; despite for the forms
17190 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17191
93311388
DE
17192static int
17193is_ref_attr (struct attribute *attr)
c906108c 17194{
c906108c
SS
17195 switch (attr->form)
17196 {
17197 case DW_FORM_ref_addr:
c906108c
SS
17198 case DW_FORM_ref1:
17199 case DW_FORM_ref2:
17200 case DW_FORM_ref4:
613e1657 17201 case DW_FORM_ref8:
c906108c 17202 case DW_FORM_ref_udata:
36586728 17203 case DW_FORM_GNU_ref_alt:
93311388 17204 return 1;
c906108c 17205 default:
93311388 17206 return 0;
c906108c 17207 }
93311388
DE
17208}
17209
b64f50a1
JK
17210/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17211 required kind. */
17212
17213static sect_offset
93311388
DE
17214dwarf2_get_ref_die_offset (struct attribute *attr)
17215{
4568ecf9 17216 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17217
93311388 17218 if (is_ref_attr (attr))
b64f50a1 17219 return retval;
93311388 17220
b64f50a1 17221 retval.sect_off = 0;
93311388
DE
17222 complaint (&symfile_complaints,
17223 _("unsupported die ref attribute form: '%s'"),
17224 dwarf_form_name (attr->form));
b64f50a1 17225 return retval;
c906108c
SS
17226}
17227
43bbcdc2
PH
17228/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17229 * the value held by the attribute is not constant. */
a02abb62 17230
43bbcdc2 17231static LONGEST
a02abb62
JB
17232dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17233{
17234 if (attr->form == DW_FORM_sdata)
17235 return DW_SND (attr);
17236 else if (attr->form == DW_FORM_udata
17237 || attr->form == DW_FORM_data1
17238 || attr->form == DW_FORM_data2
17239 || attr->form == DW_FORM_data4
17240 || attr->form == DW_FORM_data8)
17241 return DW_UNSND (attr);
17242 else
17243 {
3e43a32a
MS
17244 complaint (&symfile_complaints,
17245 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17246 dwarf_form_name (attr->form));
17247 return default_value;
17248 }
17249}
17250
348e048f
DE
17251/* Follow reference or signature attribute ATTR of SRC_DIE.
17252 On entry *REF_CU is the CU of SRC_DIE.
17253 On exit *REF_CU is the CU of the result. */
17254
17255static struct die_info *
17256follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17257 struct dwarf2_cu **ref_cu)
17258{
17259 struct die_info *die;
17260
17261 if (is_ref_attr (attr))
17262 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17263 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17264 die = follow_die_sig (src_die, attr, ref_cu);
17265 else
17266 {
17267 dump_die_for_error (src_die);
17268 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17269 (*ref_cu)->objfile->name);
17270 }
17271
17272 return die;
03dd20cc
DJ
17273}
17274
5c631832 17275/* Follow reference OFFSET.
673bfd45
DE
17276 On entry *REF_CU is the CU of the source die referencing OFFSET.
17277 On exit *REF_CU is the CU of the result.
17278 Returns NULL if OFFSET is invalid. */
f504f079 17279
f9aca02d 17280static struct die_info *
36586728
TT
17281follow_die_offset (sect_offset offset, int offset_in_dwz,
17282 struct dwarf2_cu **ref_cu)
c906108c 17283{
10b3939b 17284 struct die_info temp_die;
f2f0e013 17285 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17286
348e048f
DE
17287 gdb_assert (cu->per_cu != NULL);
17288
98bfdba5
PA
17289 target_cu = cu;
17290
3019eac3 17291 if (cu->per_cu->is_debug_types)
348e048f
DE
17292 {
17293 /* .debug_types CUs cannot reference anything outside their CU.
17294 If they need to, they have to reference a signatured type via
55f1336d 17295 DW_FORM_ref_sig8. */
348e048f 17296 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17297 return NULL;
348e048f 17298 }
36586728
TT
17299 else if (offset_in_dwz != cu->per_cu->is_dwz
17300 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17301 {
17302 struct dwarf2_per_cu_data *per_cu;
9a619af0 17303
36586728
TT
17304 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17305 cu->objfile);
03dd20cc
DJ
17306
17307 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17308 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17309 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17310
10b3939b
DJ
17311 target_cu = per_cu->cu;
17312 }
98bfdba5
PA
17313 else if (cu->dies == NULL)
17314 {
17315 /* We're loading full DIEs during partial symbol reading. */
17316 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17317 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17318 }
c906108c 17319
f2f0e013 17320 *ref_cu = target_cu;
51545339 17321 temp_die.offset = offset;
b64f50a1 17322 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17323}
10b3939b 17324
5c631832
JK
17325/* Follow reference attribute ATTR of SRC_DIE.
17326 On entry *REF_CU is the CU of SRC_DIE.
17327 On exit *REF_CU is the CU of the result. */
17328
17329static struct die_info *
17330follow_die_ref (struct die_info *src_die, struct attribute *attr,
17331 struct dwarf2_cu **ref_cu)
17332{
b64f50a1 17333 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17334 struct dwarf2_cu *cu = *ref_cu;
17335 struct die_info *die;
17336
36586728
TT
17337 die = follow_die_offset (offset,
17338 (attr->form == DW_FORM_GNU_ref_alt
17339 || cu->per_cu->is_dwz),
17340 ref_cu);
5c631832
JK
17341 if (!die)
17342 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17343 "at 0x%x [in module %s]"),
b64f50a1 17344 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17345
5c631832
JK
17346 return die;
17347}
17348
d83e736b
JK
17349/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17350 Returned value is intended for DW_OP_call*. Returned
17351 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17352
17353struct dwarf2_locexpr_baton
8b9737bf
TT
17354dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17355 struct dwarf2_per_cu_data *per_cu,
17356 CORE_ADDR (*get_frame_pc) (void *baton),
17357 void *baton)
5c631832 17358{
918dd910 17359 struct dwarf2_cu *cu;
5c631832
JK
17360 struct die_info *die;
17361 struct attribute *attr;
17362 struct dwarf2_locexpr_baton retval;
17363
8cf6f0b1
TT
17364 dw2_setup (per_cu->objfile);
17365
918dd910
JK
17366 if (per_cu->cu == NULL)
17367 load_cu (per_cu);
17368 cu = per_cu->cu;
17369
36586728 17370 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17371 if (!die)
17372 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17373 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17374
17375 attr = dwarf2_attr (die, DW_AT_location, cu);
17376 if (!attr)
17377 {
e103e986
JK
17378 /* DWARF: "If there is no such attribute, then there is no effect.".
17379 DATA is ignored if SIZE is 0. */
5c631832 17380
e103e986 17381 retval.data = NULL;
5c631832
JK
17382 retval.size = 0;
17383 }
8cf6f0b1
TT
17384 else if (attr_form_is_section_offset (attr))
17385 {
17386 struct dwarf2_loclist_baton loclist_baton;
17387 CORE_ADDR pc = (*get_frame_pc) (baton);
17388 size_t size;
17389
17390 fill_in_loclist_baton (cu, &loclist_baton, attr);
17391
17392 retval.data = dwarf2_find_location_expression (&loclist_baton,
17393 &size, pc);
17394 retval.size = size;
17395 }
5c631832
JK
17396 else
17397 {
17398 if (!attr_form_is_block (attr))
17399 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17400 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17401 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17402
17403 retval.data = DW_BLOCK (attr)->data;
17404 retval.size = DW_BLOCK (attr)->size;
17405 }
17406 retval.per_cu = cu->per_cu;
918dd910 17407
918dd910
JK
17408 age_cached_comp_units ();
17409
5c631832 17410 return retval;
348e048f
DE
17411}
17412
8b9737bf
TT
17413/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17414 offset. */
17415
17416struct dwarf2_locexpr_baton
17417dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17418 struct dwarf2_per_cu_data *per_cu,
17419 CORE_ADDR (*get_frame_pc) (void *baton),
17420 void *baton)
17421{
17422 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17423
17424 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17425}
17426
8a9b8146
TT
17427/* Return the type of the DIE at DIE_OFFSET in the CU named by
17428 PER_CU. */
17429
17430struct type *
b64f50a1 17431dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17432 struct dwarf2_per_cu_data *per_cu)
17433{
b64f50a1
JK
17434 sect_offset die_offset_sect;
17435
8a9b8146 17436 dw2_setup (per_cu->objfile);
b64f50a1
JK
17437
17438 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17439 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17440}
17441
348e048f
DE
17442/* Follow the signature attribute ATTR in SRC_DIE.
17443 On entry *REF_CU is the CU of SRC_DIE.
17444 On exit *REF_CU is the CU of the result. */
17445
17446static struct die_info *
17447follow_die_sig (struct die_info *src_die, struct attribute *attr,
17448 struct dwarf2_cu **ref_cu)
17449{
17450 struct objfile *objfile = (*ref_cu)->objfile;
17451 struct die_info temp_die;
17452 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17453 struct dwarf2_cu *sig_cu;
17454 struct die_info *die;
17455
17456 /* sig_type will be NULL if the signatured type is missing from
17457 the debug info. */
17458 if (sig_type == NULL)
17459 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17460 "at 0x%x [in module %s]"),
b64f50a1 17461 src_die->offset.sect_off, objfile->name);
348e048f
DE
17462
17463 /* If necessary, add it to the queue and load its DIEs. */
17464
95554aad 17465 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17466 read_signatured_type (sig_type);
348e048f
DE
17467
17468 gdb_assert (sig_type->per_cu.cu != NULL);
17469
17470 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17471 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17472 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17473 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17474 temp_die.offset.sect_off);
348e048f
DE
17475 if (die)
17476 {
17477 *ref_cu = sig_cu;
17478 return die;
17479 }
17480
3e43a32a
MS
17481 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17482 "from DIE at 0x%x [in module %s]"),
b64f50a1 17483 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17484}
17485
17486/* Given an offset of a signatured type, return its signatured_type. */
17487
17488static struct signatured_type *
8b70b953
TT
17489lookup_signatured_type_at_offset (struct objfile *objfile,
17490 struct dwarf2_section_info *section,
b64f50a1 17491 sect_offset offset)
348e048f 17492{
b64f50a1 17493 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17494 unsigned int length, initial_length_size;
17495 unsigned int sig_offset;
52dc124a 17496 struct signatured_type find_entry, *sig_type;
348e048f
DE
17497
17498 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17499 sig_offset = (initial_length_size
17500 + 2 /*version*/
17501 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17502 + 1 /*address_size*/);
17503 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17504 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17505
17506 /* This is only used to lookup previously recorded types.
17507 If we didn't find it, it's our bug. */
52dc124a
DE
17508 gdb_assert (sig_type != NULL);
17509 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17510
52dc124a 17511 return sig_type;
348e048f
DE
17512}
17513
e5fe5e75 17514/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17515
17516static void
e5fe5e75 17517load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17518{
52dc124a 17519 struct signatured_type *sig_type;
348e048f 17520
f4dc4d17
DE
17521 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17522 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17523
6721b2ec
DE
17524 /* We have the per_cu, but we need the signatured_type.
17525 Fortunately this is an easy translation. */
17526 gdb_assert (per_cu->is_debug_types);
17527 sig_type = (struct signatured_type *) per_cu;
348e048f 17528
6721b2ec 17529 gdb_assert (per_cu->cu == NULL);
348e048f 17530
52dc124a 17531 read_signatured_type (sig_type);
348e048f 17532
6721b2ec 17533 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17534}
17535
dee91e82
DE
17536/* die_reader_func for read_signatured_type.
17537 This is identical to load_full_comp_unit_reader,
17538 but is kept separate for now. */
348e048f
DE
17539
17540static void
dee91e82
DE
17541read_signatured_type_reader (const struct die_reader_specs *reader,
17542 gdb_byte *info_ptr,
17543 struct die_info *comp_unit_die,
17544 int has_children,
17545 void *data)
348e048f 17546{
dee91e82 17547 struct dwarf2_cu *cu = reader->cu;
348e048f 17548
dee91e82
DE
17549 gdb_assert (cu->die_hash == NULL);
17550 cu->die_hash =
17551 htab_create_alloc_ex (cu->header.length / 12,
17552 die_hash,
17553 die_eq,
17554 NULL,
17555 &cu->comp_unit_obstack,
17556 hashtab_obstack_allocate,
17557 dummy_obstack_deallocate);
348e048f 17558
dee91e82
DE
17559 if (has_children)
17560 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17561 &info_ptr, comp_unit_die);
17562 cu->dies = comp_unit_die;
17563 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17564
17565 /* We try not to read any attributes in this function, because not
9cdd5dbd 17566 all CUs needed for references have been loaded yet, and symbol
348e048f 17567 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17568 or we won't be able to build types correctly.
17569 Similarly, if we do not read the producer, we can not apply
17570 producer-specific interpretation. */
95554aad 17571 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17572}
348e048f 17573
3019eac3
DE
17574/* Read in a signatured type and build its CU and DIEs.
17575 If the type is a stub for the real type in a DWO file,
17576 read in the real type from the DWO file as well. */
dee91e82
DE
17577
17578static void
17579read_signatured_type (struct signatured_type *sig_type)
17580{
17581 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17582
3019eac3 17583 gdb_assert (per_cu->is_debug_types);
dee91e82 17584 gdb_assert (per_cu->cu == NULL);
348e048f 17585
f4dc4d17
DE
17586 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17587 read_signatured_type_reader, NULL);
c906108c
SS
17588}
17589
c906108c
SS
17590/* Decode simple location descriptions.
17591 Given a pointer to a dwarf block that defines a location, compute
17592 the location and return the value.
17593
4cecd739
DJ
17594 NOTE drow/2003-11-18: This function is called in two situations
17595 now: for the address of static or global variables (partial symbols
17596 only) and for offsets into structures which are expected to be
17597 (more or less) constant. The partial symbol case should go away,
17598 and only the constant case should remain. That will let this
17599 function complain more accurately. A few special modes are allowed
17600 without complaint for global variables (for instance, global
17601 register values and thread-local values).
c906108c
SS
17602
17603 A location description containing no operations indicates that the
4cecd739 17604 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17605 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17606 callers will only want a very basic result and this can become a
21ae7a4d
JK
17607 complaint.
17608
17609 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17610
17611static CORE_ADDR
e7c27a73 17612decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17613{
e7c27a73 17614 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17615 size_t i;
17616 size_t size = blk->size;
21ae7a4d
JK
17617 gdb_byte *data = blk->data;
17618 CORE_ADDR stack[64];
17619 int stacki;
17620 unsigned int bytes_read, unsnd;
17621 gdb_byte op;
c906108c 17622
21ae7a4d
JK
17623 i = 0;
17624 stacki = 0;
17625 stack[stacki] = 0;
17626 stack[++stacki] = 0;
17627
17628 while (i < size)
17629 {
17630 op = data[i++];
17631 switch (op)
17632 {
17633 case DW_OP_lit0:
17634 case DW_OP_lit1:
17635 case DW_OP_lit2:
17636 case DW_OP_lit3:
17637 case DW_OP_lit4:
17638 case DW_OP_lit5:
17639 case DW_OP_lit6:
17640 case DW_OP_lit7:
17641 case DW_OP_lit8:
17642 case DW_OP_lit9:
17643 case DW_OP_lit10:
17644 case DW_OP_lit11:
17645 case DW_OP_lit12:
17646 case DW_OP_lit13:
17647 case DW_OP_lit14:
17648 case DW_OP_lit15:
17649 case DW_OP_lit16:
17650 case DW_OP_lit17:
17651 case DW_OP_lit18:
17652 case DW_OP_lit19:
17653 case DW_OP_lit20:
17654 case DW_OP_lit21:
17655 case DW_OP_lit22:
17656 case DW_OP_lit23:
17657 case DW_OP_lit24:
17658 case DW_OP_lit25:
17659 case DW_OP_lit26:
17660 case DW_OP_lit27:
17661 case DW_OP_lit28:
17662 case DW_OP_lit29:
17663 case DW_OP_lit30:
17664 case DW_OP_lit31:
17665 stack[++stacki] = op - DW_OP_lit0;
17666 break;
f1bea926 17667
21ae7a4d
JK
17668 case DW_OP_reg0:
17669 case DW_OP_reg1:
17670 case DW_OP_reg2:
17671 case DW_OP_reg3:
17672 case DW_OP_reg4:
17673 case DW_OP_reg5:
17674 case DW_OP_reg6:
17675 case DW_OP_reg7:
17676 case DW_OP_reg8:
17677 case DW_OP_reg9:
17678 case DW_OP_reg10:
17679 case DW_OP_reg11:
17680 case DW_OP_reg12:
17681 case DW_OP_reg13:
17682 case DW_OP_reg14:
17683 case DW_OP_reg15:
17684 case DW_OP_reg16:
17685 case DW_OP_reg17:
17686 case DW_OP_reg18:
17687 case DW_OP_reg19:
17688 case DW_OP_reg20:
17689 case DW_OP_reg21:
17690 case DW_OP_reg22:
17691 case DW_OP_reg23:
17692 case DW_OP_reg24:
17693 case DW_OP_reg25:
17694 case DW_OP_reg26:
17695 case DW_OP_reg27:
17696 case DW_OP_reg28:
17697 case DW_OP_reg29:
17698 case DW_OP_reg30:
17699 case DW_OP_reg31:
17700 stack[++stacki] = op - DW_OP_reg0;
17701 if (i < size)
17702 dwarf2_complex_location_expr_complaint ();
17703 break;
c906108c 17704
21ae7a4d
JK
17705 case DW_OP_regx:
17706 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17707 i += bytes_read;
17708 stack[++stacki] = unsnd;
17709 if (i < size)
17710 dwarf2_complex_location_expr_complaint ();
17711 break;
c906108c 17712
21ae7a4d
JK
17713 case DW_OP_addr:
17714 stack[++stacki] = read_address (objfile->obfd, &data[i],
17715 cu, &bytes_read);
17716 i += bytes_read;
17717 break;
d53d4ac5 17718
21ae7a4d
JK
17719 case DW_OP_const1u:
17720 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17721 i += 1;
17722 break;
17723
17724 case DW_OP_const1s:
17725 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17726 i += 1;
17727 break;
17728
17729 case DW_OP_const2u:
17730 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17731 i += 2;
17732 break;
17733
17734 case DW_OP_const2s:
17735 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17736 i += 2;
17737 break;
d53d4ac5 17738
21ae7a4d
JK
17739 case DW_OP_const4u:
17740 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17741 i += 4;
17742 break;
17743
17744 case DW_OP_const4s:
17745 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17746 i += 4;
17747 break;
17748
585861ea
JK
17749 case DW_OP_const8u:
17750 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17751 i += 8;
17752 break;
17753
21ae7a4d
JK
17754 case DW_OP_constu:
17755 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17756 &bytes_read);
17757 i += bytes_read;
17758 break;
17759
17760 case DW_OP_consts:
17761 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17762 i += bytes_read;
17763 break;
17764
17765 case DW_OP_dup:
17766 stack[stacki + 1] = stack[stacki];
17767 stacki++;
17768 break;
17769
17770 case DW_OP_plus:
17771 stack[stacki - 1] += stack[stacki];
17772 stacki--;
17773 break;
17774
17775 case DW_OP_plus_uconst:
17776 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17777 &bytes_read);
17778 i += bytes_read;
17779 break;
17780
17781 case DW_OP_minus:
17782 stack[stacki - 1] -= stack[stacki];
17783 stacki--;
17784 break;
17785
17786 case DW_OP_deref:
17787 /* If we're not the last op, then we definitely can't encode
17788 this using GDB's address_class enum. This is valid for partial
17789 global symbols, although the variable's address will be bogus
17790 in the psymtab. */
17791 if (i < size)
17792 dwarf2_complex_location_expr_complaint ();
17793 break;
17794
17795 case DW_OP_GNU_push_tls_address:
17796 /* The top of the stack has the offset from the beginning
17797 of the thread control block at which the variable is located. */
17798 /* Nothing should follow this operator, so the top of stack would
17799 be returned. */
17800 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17801 address will be bogus in the psymtab. Make it always at least
17802 non-zero to not look as a variable garbage collected by linker
17803 which have DW_OP_addr 0. */
21ae7a4d
JK
17804 if (i < size)
17805 dwarf2_complex_location_expr_complaint ();
585861ea 17806 stack[stacki]++;
21ae7a4d
JK
17807 break;
17808
17809 case DW_OP_GNU_uninit:
17810 break;
17811
3019eac3 17812 case DW_OP_GNU_addr_index:
49f6c839 17813 case DW_OP_GNU_const_index:
3019eac3
DE
17814 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17815 &bytes_read);
17816 i += bytes_read;
17817 break;
17818
21ae7a4d
JK
17819 default:
17820 {
f39c6ffd 17821 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17822
17823 if (name)
17824 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17825 name);
17826 else
17827 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17828 op);
17829 }
17830
17831 return (stack[stacki]);
d53d4ac5 17832 }
3c6e0cb3 17833
21ae7a4d
JK
17834 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17835 outside of the allocated space. Also enforce minimum>0. */
17836 if (stacki >= ARRAY_SIZE (stack) - 1)
17837 {
17838 complaint (&symfile_complaints,
17839 _("location description stack overflow"));
17840 return 0;
17841 }
17842
17843 if (stacki <= 0)
17844 {
17845 complaint (&symfile_complaints,
17846 _("location description stack underflow"));
17847 return 0;
17848 }
17849 }
17850 return (stack[stacki]);
c906108c
SS
17851}
17852
17853/* memory allocation interface */
17854
c906108c 17855static struct dwarf_block *
7b5a2f43 17856dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17857{
17858 struct dwarf_block *blk;
17859
17860 blk = (struct dwarf_block *)
7b5a2f43 17861 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17862 return (blk);
17863}
17864
c906108c 17865static struct die_info *
b60c80d6 17866dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17867{
17868 struct die_info *die;
b60c80d6
DJ
17869 size_t size = sizeof (struct die_info);
17870
17871 if (num_attrs > 1)
17872 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17873
b60c80d6 17874 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17875 memset (die, 0, sizeof (struct die_info));
17876 return (die);
17877}
2e276125
JB
17878
17879\f
17880/* Macro support. */
17881
2e276125
JB
17882/* Return the full name of file number I in *LH's file name table.
17883 Use COMP_DIR as the name of the current directory of the
17884 compilation. The result is allocated using xmalloc; the caller is
17885 responsible for freeing it. */
17886static char *
17887file_full_name (int file, struct line_header *lh, const char *comp_dir)
17888{
6a83a1e6
EZ
17889 /* Is the file number a valid index into the line header's file name
17890 table? Remember that file numbers start with one, not zero. */
17891 if (1 <= file && file <= lh->num_file_names)
17892 {
17893 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17894
6a83a1e6
EZ
17895 if (IS_ABSOLUTE_PATH (fe->name))
17896 return xstrdup (fe->name);
17897 else
17898 {
17899 const char *dir;
17900 int dir_len;
17901 char *full_name;
17902
17903 if (fe->dir_index)
17904 dir = lh->include_dirs[fe->dir_index - 1];
17905 else
17906 dir = comp_dir;
17907
17908 if (dir)
17909 {
17910 dir_len = strlen (dir);
17911 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17912 strcpy (full_name, dir);
17913 full_name[dir_len] = '/';
17914 strcpy (full_name + dir_len + 1, fe->name);
17915 return full_name;
17916 }
17917 else
17918 return xstrdup (fe->name);
17919 }
17920 }
2e276125
JB
17921 else
17922 {
6a83a1e6
EZ
17923 /* The compiler produced a bogus file number. We can at least
17924 record the macro definitions made in the file, even if we
17925 won't be able to find the file by name. */
17926 char fake_name[80];
9a619af0 17927
8c042590
PM
17928 xsnprintf (fake_name, sizeof (fake_name),
17929 "<bad macro file number %d>", file);
2e276125 17930
6e70227d 17931 complaint (&symfile_complaints,
6a83a1e6
EZ
17932 _("bad file number in macro information (%d)"),
17933 file);
2e276125 17934
6a83a1e6 17935 return xstrdup (fake_name);
2e276125
JB
17936 }
17937}
17938
17939
17940static struct macro_source_file *
17941macro_start_file (int file, int line,
17942 struct macro_source_file *current_file,
17943 const char *comp_dir,
17944 struct line_header *lh, struct objfile *objfile)
17945{
17946 /* The full name of this source file. */
17947 char *full_name = file_full_name (file, lh, comp_dir);
17948
17949 /* We don't create a macro table for this compilation unit
17950 at all until we actually get a filename. */
17951 if (! pending_macros)
6532ff36
TT
17952 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17953 objfile->per_bfd->macro_cache);
2e276125
JB
17954
17955 if (! current_file)
abc9d0dc
TT
17956 {
17957 /* If we have no current file, then this must be the start_file
17958 directive for the compilation unit's main source file. */
17959 current_file = macro_set_main (pending_macros, full_name);
17960 macro_define_special (pending_macros);
17961 }
2e276125
JB
17962 else
17963 current_file = macro_include (current_file, line, full_name);
17964
17965 xfree (full_name);
6e70227d 17966
2e276125
JB
17967 return current_file;
17968}
17969
17970
17971/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17972 followed by a null byte. */
17973static char *
17974copy_string (const char *buf, int len)
17975{
17976 char *s = xmalloc (len + 1);
9a619af0 17977
2e276125
JB
17978 memcpy (s, buf, len);
17979 s[len] = '\0';
2e276125
JB
17980 return s;
17981}
17982
17983
17984static const char *
17985consume_improper_spaces (const char *p, const char *body)
17986{
17987 if (*p == ' ')
17988 {
4d3c2250 17989 complaint (&symfile_complaints,
3e43a32a
MS
17990 _("macro definition contains spaces "
17991 "in formal argument list:\n`%s'"),
4d3c2250 17992 body);
2e276125
JB
17993
17994 while (*p == ' ')
17995 p++;
17996 }
17997
17998 return p;
17999}
18000
18001
18002static void
18003parse_macro_definition (struct macro_source_file *file, int line,
18004 const char *body)
18005{
18006 const char *p;
18007
18008 /* The body string takes one of two forms. For object-like macro
18009 definitions, it should be:
18010
18011 <macro name> " " <definition>
18012
18013 For function-like macro definitions, it should be:
18014
18015 <macro name> "() " <definition>
18016 or
18017 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18018
18019 Spaces may appear only where explicitly indicated, and in the
18020 <definition>.
18021
18022 The Dwarf 2 spec says that an object-like macro's name is always
18023 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18024 the space when the macro's definition is the empty string.
2e276125
JB
18025
18026 The Dwarf 2 spec says that there should be no spaces between the
18027 formal arguments in a function-like macro's formal argument list,
18028 but versions of GCC around March 2002 include spaces after the
18029 commas. */
18030
18031
18032 /* Find the extent of the macro name. The macro name is terminated
18033 by either a space or null character (for an object-like macro) or
18034 an opening paren (for a function-like macro). */
18035 for (p = body; *p; p++)
18036 if (*p == ' ' || *p == '(')
18037 break;
18038
18039 if (*p == ' ' || *p == '\0')
18040 {
18041 /* It's an object-like macro. */
18042 int name_len = p - body;
18043 char *name = copy_string (body, name_len);
18044 const char *replacement;
18045
18046 if (*p == ' ')
18047 replacement = body + name_len + 1;
18048 else
18049 {
4d3c2250 18050 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18051 replacement = body + name_len;
18052 }
6e70227d 18053
2e276125
JB
18054 macro_define_object (file, line, name, replacement);
18055
18056 xfree (name);
18057 }
18058 else if (*p == '(')
18059 {
18060 /* It's a function-like macro. */
18061 char *name = copy_string (body, p - body);
18062 int argc = 0;
18063 int argv_size = 1;
18064 char **argv = xmalloc (argv_size * sizeof (*argv));
18065
18066 p++;
18067
18068 p = consume_improper_spaces (p, body);
18069
18070 /* Parse the formal argument list. */
18071 while (*p && *p != ')')
18072 {
18073 /* Find the extent of the current argument name. */
18074 const char *arg_start = p;
18075
18076 while (*p && *p != ',' && *p != ')' && *p != ' ')
18077 p++;
18078
18079 if (! *p || p == arg_start)
4d3c2250 18080 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18081 else
18082 {
18083 /* Make sure argv has room for the new argument. */
18084 if (argc >= argv_size)
18085 {
18086 argv_size *= 2;
18087 argv = xrealloc (argv, argv_size * sizeof (*argv));
18088 }
18089
18090 argv[argc++] = copy_string (arg_start, p - arg_start);
18091 }
18092
18093 p = consume_improper_spaces (p, body);
18094
18095 /* Consume the comma, if present. */
18096 if (*p == ',')
18097 {
18098 p++;
18099
18100 p = consume_improper_spaces (p, body);
18101 }
18102 }
18103
18104 if (*p == ')')
18105 {
18106 p++;
18107
18108 if (*p == ' ')
18109 /* Perfectly formed definition, no complaints. */
18110 macro_define_function (file, line, name,
6e70227d 18111 argc, (const char **) argv,
2e276125
JB
18112 p + 1);
18113 else if (*p == '\0')
18114 {
18115 /* Complain, but do define it. */
4d3c2250 18116 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18117 macro_define_function (file, line, name,
6e70227d 18118 argc, (const char **) argv,
2e276125
JB
18119 p);
18120 }
18121 else
18122 /* Just complain. */
4d3c2250 18123 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18124 }
18125 else
18126 /* Just complain. */
4d3c2250 18127 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18128
18129 xfree (name);
18130 {
18131 int i;
18132
18133 for (i = 0; i < argc; i++)
18134 xfree (argv[i]);
18135 }
18136 xfree (argv);
18137 }
18138 else
4d3c2250 18139 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18140}
18141
cf2c3c16
TT
18142/* Skip some bytes from BYTES according to the form given in FORM.
18143 Returns the new pointer. */
2e276125 18144
cf2c3c16 18145static gdb_byte *
f664829e 18146skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18147 enum dwarf_form form,
18148 unsigned int offset_size,
18149 struct dwarf2_section_info *section)
2e276125 18150{
cf2c3c16 18151 unsigned int bytes_read;
2e276125 18152
cf2c3c16 18153 switch (form)
2e276125 18154 {
cf2c3c16
TT
18155 case DW_FORM_data1:
18156 case DW_FORM_flag:
18157 ++bytes;
18158 break;
18159
18160 case DW_FORM_data2:
18161 bytes += 2;
18162 break;
18163
18164 case DW_FORM_data4:
18165 bytes += 4;
18166 break;
18167
18168 case DW_FORM_data8:
18169 bytes += 8;
18170 break;
18171
18172 case DW_FORM_string:
18173 read_direct_string (abfd, bytes, &bytes_read);
18174 bytes += bytes_read;
18175 break;
18176
18177 case DW_FORM_sec_offset:
18178 case DW_FORM_strp:
36586728 18179 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18180 bytes += offset_size;
18181 break;
18182
18183 case DW_FORM_block:
18184 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18185 bytes += bytes_read;
18186 break;
18187
18188 case DW_FORM_block1:
18189 bytes += 1 + read_1_byte (abfd, bytes);
18190 break;
18191 case DW_FORM_block2:
18192 bytes += 2 + read_2_bytes (abfd, bytes);
18193 break;
18194 case DW_FORM_block4:
18195 bytes += 4 + read_4_bytes (abfd, bytes);
18196 break;
18197
18198 case DW_FORM_sdata:
18199 case DW_FORM_udata:
3019eac3
DE
18200 case DW_FORM_GNU_addr_index:
18201 case DW_FORM_GNU_str_index:
f664829e
DE
18202 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18203 if (bytes == NULL)
18204 {
18205 dwarf2_section_buffer_overflow_complaint (section);
18206 return NULL;
18207 }
cf2c3c16
TT
18208 break;
18209
18210 default:
18211 {
18212 complain:
18213 complaint (&symfile_complaints,
18214 _("invalid form 0x%x in `%s'"),
18215 form,
18216 section->asection->name);
18217 return NULL;
18218 }
2e276125
JB
18219 }
18220
cf2c3c16
TT
18221 return bytes;
18222}
757a13d0 18223
cf2c3c16
TT
18224/* A helper for dwarf_decode_macros that handles skipping an unknown
18225 opcode. Returns an updated pointer to the macro data buffer; or,
18226 on error, issues a complaint and returns NULL. */
757a13d0 18227
cf2c3c16
TT
18228static gdb_byte *
18229skip_unknown_opcode (unsigned int opcode,
18230 gdb_byte **opcode_definitions,
f664829e 18231 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18232 bfd *abfd,
18233 unsigned int offset_size,
18234 struct dwarf2_section_info *section)
18235{
18236 unsigned int bytes_read, i;
18237 unsigned long arg;
18238 gdb_byte *defn;
2e276125 18239
cf2c3c16 18240 if (opcode_definitions[opcode] == NULL)
2e276125 18241 {
cf2c3c16
TT
18242 complaint (&symfile_complaints,
18243 _("unrecognized DW_MACFINO opcode 0x%x"),
18244 opcode);
18245 return NULL;
18246 }
2e276125 18247
cf2c3c16
TT
18248 defn = opcode_definitions[opcode];
18249 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18250 defn += bytes_read;
2e276125 18251
cf2c3c16
TT
18252 for (i = 0; i < arg; ++i)
18253 {
f664829e
DE
18254 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18255 section);
cf2c3c16
TT
18256 if (mac_ptr == NULL)
18257 {
18258 /* skip_form_bytes already issued the complaint. */
18259 return NULL;
18260 }
18261 }
757a13d0 18262
cf2c3c16
TT
18263 return mac_ptr;
18264}
757a13d0 18265
cf2c3c16
TT
18266/* A helper function which parses the header of a macro section.
18267 If the macro section is the extended (for now called "GNU") type,
18268 then this updates *OFFSET_SIZE. Returns a pointer to just after
18269 the header, or issues a complaint and returns NULL on error. */
757a13d0 18270
cf2c3c16
TT
18271static gdb_byte *
18272dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18273 bfd *abfd,
18274 gdb_byte *mac_ptr,
18275 unsigned int *offset_size,
18276 int section_is_gnu)
18277{
18278 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18279
cf2c3c16
TT
18280 if (section_is_gnu)
18281 {
18282 unsigned int version, flags;
757a13d0 18283
cf2c3c16
TT
18284 version = read_2_bytes (abfd, mac_ptr);
18285 if (version != 4)
18286 {
18287 complaint (&symfile_complaints,
18288 _("unrecognized version `%d' in .debug_macro section"),
18289 version);
18290 return NULL;
18291 }
18292 mac_ptr += 2;
757a13d0 18293
cf2c3c16
TT
18294 flags = read_1_byte (abfd, mac_ptr);
18295 ++mac_ptr;
18296 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18297
cf2c3c16
TT
18298 if ((flags & 2) != 0)
18299 /* We don't need the line table offset. */
18300 mac_ptr += *offset_size;
757a13d0 18301
cf2c3c16
TT
18302 /* Vendor opcode descriptions. */
18303 if ((flags & 4) != 0)
18304 {
18305 unsigned int i, count;
757a13d0 18306
cf2c3c16
TT
18307 count = read_1_byte (abfd, mac_ptr);
18308 ++mac_ptr;
18309 for (i = 0; i < count; ++i)
18310 {
18311 unsigned int opcode, bytes_read;
18312 unsigned long arg;
18313
18314 opcode = read_1_byte (abfd, mac_ptr);
18315 ++mac_ptr;
18316 opcode_definitions[opcode] = mac_ptr;
18317 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18318 mac_ptr += bytes_read;
18319 mac_ptr += arg;
18320 }
757a13d0 18321 }
cf2c3c16 18322 }
757a13d0 18323
cf2c3c16
TT
18324 return mac_ptr;
18325}
757a13d0 18326
cf2c3c16 18327/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18328 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18329
18330static void
18331dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18332 struct macro_source_file *current_file,
18333 struct line_header *lh, char *comp_dir,
18334 struct dwarf2_section_info *section,
36586728 18335 int section_is_gnu, int section_is_dwz,
cf2c3c16 18336 unsigned int offset_size,
8fc3fc34
TT
18337 struct objfile *objfile,
18338 htab_t include_hash)
cf2c3c16
TT
18339{
18340 enum dwarf_macro_record_type macinfo_type;
18341 int at_commandline;
18342 gdb_byte *opcode_definitions[256];
757a13d0 18343
cf2c3c16
TT
18344 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18345 &offset_size, section_is_gnu);
18346 if (mac_ptr == NULL)
18347 {
18348 /* We already issued a complaint. */
18349 return;
18350 }
757a13d0
JK
18351
18352 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18353 GDB is still reading the definitions from command line. First
18354 DW_MACINFO_start_file will need to be ignored as it was already executed
18355 to create CURRENT_FILE for the main source holding also the command line
18356 definitions. On first met DW_MACINFO_start_file this flag is reset to
18357 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18358
18359 at_commandline = 1;
18360
18361 do
18362 {
18363 /* Do we at least have room for a macinfo type byte? */
18364 if (mac_ptr >= mac_end)
18365 {
f664829e 18366 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18367 break;
18368 }
18369
18370 macinfo_type = read_1_byte (abfd, mac_ptr);
18371 mac_ptr++;
18372
cf2c3c16
TT
18373 /* Note that we rely on the fact that the corresponding GNU and
18374 DWARF constants are the same. */
757a13d0
JK
18375 switch (macinfo_type)
18376 {
18377 /* A zero macinfo type indicates the end of the macro
18378 information. */
18379 case 0:
18380 break;
2e276125 18381
cf2c3c16
TT
18382 case DW_MACRO_GNU_define:
18383 case DW_MACRO_GNU_undef:
18384 case DW_MACRO_GNU_define_indirect:
18385 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18386 case DW_MACRO_GNU_define_indirect_alt:
18387 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18388 {
891d2f0b 18389 unsigned int bytes_read;
2e276125
JB
18390 int line;
18391 char *body;
cf2c3c16 18392 int is_define;
2e276125 18393
cf2c3c16
TT
18394 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18395 mac_ptr += bytes_read;
18396
18397 if (macinfo_type == DW_MACRO_GNU_define
18398 || macinfo_type == DW_MACRO_GNU_undef)
18399 {
18400 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18401 mac_ptr += bytes_read;
18402 }
18403 else
18404 {
18405 LONGEST str_offset;
18406
18407 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18408 mac_ptr += offset_size;
2e276125 18409
36586728 18410 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18411 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18412 || section_is_dwz)
36586728
TT
18413 {
18414 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18415
18416 body = read_indirect_string_from_dwz (dwz, str_offset);
18417 }
18418 else
18419 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18420 }
18421
18422 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18423 || macinfo_type == DW_MACRO_GNU_define_indirect
18424 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18425 if (! current_file)
757a13d0
JK
18426 {
18427 /* DWARF violation as no main source is present. */
18428 complaint (&symfile_complaints,
18429 _("debug info with no main source gives macro %s "
18430 "on line %d: %s"),
cf2c3c16
TT
18431 is_define ? _("definition") : _("undefinition"),
18432 line, body);
757a13d0
JK
18433 break;
18434 }
3e43a32a
MS
18435 if ((line == 0 && !at_commandline)
18436 || (line != 0 && at_commandline))
4d3c2250 18437 complaint (&symfile_complaints,
757a13d0
JK
18438 _("debug info gives %s macro %s with %s line %d: %s"),
18439 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18440 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18441 line == 0 ? _("zero") : _("non-zero"), line, body);
18442
cf2c3c16 18443 if (is_define)
757a13d0 18444 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18445 else
18446 {
18447 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18448 || macinfo_type == DW_MACRO_GNU_undef_indirect
18449 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18450 macro_undef (current_file, line, body);
18451 }
2e276125
JB
18452 }
18453 break;
18454
cf2c3c16 18455 case DW_MACRO_GNU_start_file:
2e276125 18456 {
891d2f0b 18457 unsigned int bytes_read;
2e276125
JB
18458 int line, file;
18459
18460 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18461 mac_ptr += bytes_read;
18462 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18463 mac_ptr += bytes_read;
18464
3e43a32a
MS
18465 if ((line == 0 && !at_commandline)
18466 || (line != 0 && at_commandline))
757a13d0
JK
18467 complaint (&symfile_complaints,
18468 _("debug info gives source %d included "
18469 "from %s at %s line %d"),
18470 file, at_commandline ? _("command-line") : _("file"),
18471 line == 0 ? _("zero") : _("non-zero"), line);
18472
18473 if (at_commandline)
18474 {
cf2c3c16
TT
18475 /* This DW_MACRO_GNU_start_file was executed in the
18476 pass one. */
757a13d0
JK
18477 at_commandline = 0;
18478 }
18479 else
18480 current_file = macro_start_file (file, line,
18481 current_file, comp_dir,
cf2c3c16 18482 lh, objfile);
2e276125
JB
18483 }
18484 break;
18485
cf2c3c16 18486 case DW_MACRO_GNU_end_file:
2e276125 18487 if (! current_file)
4d3c2250 18488 complaint (&symfile_complaints,
3e43a32a
MS
18489 _("macro debug info has an unmatched "
18490 "`close_file' directive"));
2e276125
JB
18491 else
18492 {
18493 current_file = current_file->included_by;
18494 if (! current_file)
18495 {
cf2c3c16 18496 enum dwarf_macro_record_type next_type;
2e276125
JB
18497
18498 /* GCC circa March 2002 doesn't produce the zero
18499 type byte marking the end of the compilation
18500 unit. Complain if it's not there, but exit no
18501 matter what. */
18502
18503 /* Do we at least have room for a macinfo type byte? */
18504 if (mac_ptr >= mac_end)
18505 {
f664829e 18506 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18507 return;
18508 }
18509
18510 /* We don't increment mac_ptr here, so this is just
18511 a look-ahead. */
18512 next_type = read_1_byte (abfd, mac_ptr);
18513 if (next_type != 0)
4d3c2250 18514 complaint (&symfile_complaints,
3e43a32a
MS
18515 _("no terminating 0-type entry for "
18516 "macros in `.debug_macinfo' section"));
2e276125
JB
18517
18518 return;
18519 }
18520 }
18521 break;
18522
cf2c3c16 18523 case DW_MACRO_GNU_transparent_include:
36586728 18524 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18525 {
18526 LONGEST offset;
8fc3fc34 18527 void **slot;
a036ba48
TT
18528 bfd *include_bfd = abfd;
18529 struct dwarf2_section_info *include_section = section;
18530 struct dwarf2_section_info alt_section;
18531 gdb_byte *include_mac_end = mac_end;
18532 int is_dwz = section_is_dwz;
18533 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18534
18535 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18536 mac_ptr += offset_size;
18537
a036ba48
TT
18538 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18539 {
18540 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18541
18542 dwarf2_read_section (dwarf2_per_objfile->objfile,
18543 &dwz->macro);
18544
18545 include_bfd = dwz->macro.asection->owner;
18546 include_section = &dwz->macro;
18547 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18548 is_dwz = 1;
18549 }
18550
18551 new_mac_ptr = include_section->buffer + offset;
18552 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18553
8fc3fc34
TT
18554 if (*slot != NULL)
18555 {
18556 /* This has actually happened; see
18557 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18558 complaint (&symfile_complaints,
18559 _("recursive DW_MACRO_GNU_transparent_include in "
18560 ".debug_macro section"));
18561 }
18562 else
18563 {
a036ba48 18564 *slot = new_mac_ptr;
36586728 18565
a036ba48 18566 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18567 include_mac_end, current_file,
8fc3fc34 18568 lh, comp_dir,
36586728 18569 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18570 offset_size, objfile, include_hash);
18571
a036ba48 18572 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18573 }
cf2c3c16
TT
18574 }
18575 break;
18576
2e276125 18577 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18578 if (!section_is_gnu)
18579 {
18580 unsigned int bytes_read;
18581 int constant;
2e276125 18582
cf2c3c16
TT
18583 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18584 mac_ptr += bytes_read;
18585 read_direct_string (abfd, mac_ptr, &bytes_read);
18586 mac_ptr += bytes_read;
2e276125 18587
cf2c3c16
TT
18588 /* We don't recognize any vendor extensions. */
18589 break;
18590 }
18591 /* FALLTHROUGH */
18592
18593 default:
18594 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18595 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18596 section);
18597 if (mac_ptr == NULL)
18598 return;
18599 break;
2e276125 18600 }
757a13d0 18601 } while (macinfo_type != 0);
2e276125 18602}
8e19ed76 18603
cf2c3c16 18604static void
09262596
DE
18605dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18606 char *comp_dir, int section_is_gnu)
cf2c3c16 18607{
bb5ed363 18608 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18609 struct line_header *lh = cu->line_header;
18610 bfd *abfd;
cf2c3c16
TT
18611 gdb_byte *mac_ptr, *mac_end;
18612 struct macro_source_file *current_file = 0;
18613 enum dwarf_macro_record_type macinfo_type;
18614 unsigned int offset_size = cu->header.offset_size;
18615 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18616 struct cleanup *cleanup;
18617 htab_t include_hash;
18618 void **slot;
09262596
DE
18619 struct dwarf2_section_info *section;
18620 const char *section_name;
18621
18622 if (cu->dwo_unit != NULL)
18623 {
18624 if (section_is_gnu)
18625 {
18626 section = &cu->dwo_unit->dwo_file->sections.macro;
18627 section_name = ".debug_macro.dwo";
18628 }
18629 else
18630 {
18631 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18632 section_name = ".debug_macinfo.dwo";
18633 }
18634 }
18635 else
18636 {
18637 if (section_is_gnu)
18638 {
18639 section = &dwarf2_per_objfile->macro;
18640 section_name = ".debug_macro";
18641 }
18642 else
18643 {
18644 section = &dwarf2_per_objfile->macinfo;
18645 section_name = ".debug_macinfo";
18646 }
18647 }
cf2c3c16 18648
bb5ed363 18649 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18650 if (section->buffer == NULL)
18651 {
fceca515 18652 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18653 return;
18654 }
09262596 18655 abfd = section->asection->owner;
cf2c3c16
TT
18656
18657 /* First pass: Find the name of the base filename.
18658 This filename is needed in order to process all macros whose definition
18659 (or undefinition) comes from the command line. These macros are defined
18660 before the first DW_MACINFO_start_file entry, and yet still need to be
18661 associated to the base file.
18662
18663 To determine the base file name, we scan the macro definitions until we
18664 reach the first DW_MACINFO_start_file entry. We then initialize
18665 CURRENT_FILE accordingly so that any macro definition found before the
18666 first DW_MACINFO_start_file can still be associated to the base file. */
18667
18668 mac_ptr = section->buffer + offset;
18669 mac_end = section->buffer + section->size;
18670
18671 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18672 &offset_size, section_is_gnu);
18673 if (mac_ptr == NULL)
18674 {
18675 /* We already issued a complaint. */
18676 return;
18677 }
18678
18679 do
18680 {
18681 /* Do we at least have room for a macinfo type byte? */
18682 if (mac_ptr >= mac_end)
18683 {
18684 /* Complaint is printed during the second pass as GDB will probably
18685 stop the first pass earlier upon finding
18686 DW_MACINFO_start_file. */
18687 break;
18688 }
18689
18690 macinfo_type = read_1_byte (abfd, mac_ptr);
18691 mac_ptr++;
18692
18693 /* Note that we rely on the fact that the corresponding GNU and
18694 DWARF constants are the same. */
18695 switch (macinfo_type)
18696 {
18697 /* A zero macinfo type indicates the end of the macro
18698 information. */
18699 case 0:
18700 break;
18701
18702 case DW_MACRO_GNU_define:
18703 case DW_MACRO_GNU_undef:
18704 /* Only skip the data by MAC_PTR. */
18705 {
18706 unsigned int bytes_read;
18707
18708 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18709 mac_ptr += bytes_read;
18710 read_direct_string (abfd, mac_ptr, &bytes_read);
18711 mac_ptr += bytes_read;
18712 }
18713 break;
18714
18715 case DW_MACRO_GNU_start_file:
18716 {
18717 unsigned int bytes_read;
18718 int line, file;
18719
18720 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18721 mac_ptr += bytes_read;
18722 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18723 mac_ptr += bytes_read;
18724
18725 current_file = macro_start_file (file, line, current_file,
bb5ed363 18726 comp_dir, lh, objfile);
cf2c3c16
TT
18727 }
18728 break;
18729
18730 case DW_MACRO_GNU_end_file:
18731 /* No data to skip by MAC_PTR. */
18732 break;
18733
18734 case DW_MACRO_GNU_define_indirect:
18735 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18736 case DW_MACRO_GNU_define_indirect_alt:
18737 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18738 {
18739 unsigned int bytes_read;
18740
18741 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18742 mac_ptr += bytes_read;
18743 mac_ptr += offset_size;
18744 }
18745 break;
18746
18747 case DW_MACRO_GNU_transparent_include:
f7a35f02 18748 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18749 /* Note that, according to the spec, a transparent include
18750 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18751 skip this opcode. */
18752 mac_ptr += offset_size;
18753 break;
18754
18755 case DW_MACINFO_vendor_ext:
18756 /* Only skip the data by MAC_PTR. */
18757 if (!section_is_gnu)
18758 {
18759 unsigned int bytes_read;
18760
18761 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18762 mac_ptr += bytes_read;
18763 read_direct_string (abfd, mac_ptr, &bytes_read);
18764 mac_ptr += bytes_read;
18765 }
18766 /* FALLTHROUGH */
18767
18768 default:
18769 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18770 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18771 section);
18772 if (mac_ptr == NULL)
18773 return;
18774 break;
18775 }
18776 } while (macinfo_type != 0 && current_file == NULL);
18777
18778 /* Second pass: Process all entries.
18779
18780 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18781 command-line macro definitions/undefinitions. This flag is unset when we
18782 reach the first DW_MACINFO_start_file entry. */
18783
8fc3fc34
TT
18784 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18785 NULL, xcalloc, xfree);
18786 cleanup = make_cleanup_htab_delete (include_hash);
18787 mac_ptr = section->buffer + offset;
18788 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18789 *slot = mac_ptr;
18790 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18791 current_file, lh, comp_dir, section,
18792 section_is_gnu, 0,
8fc3fc34
TT
18793 offset_size, objfile, include_hash);
18794 do_cleanups (cleanup);
cf2c3c16
TT
18795}
18796
8e19ed76 18797/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18798 if so return true else false. */
380bca97 18799
8e19ed76
PS
18800static int
18801attr_form_is_block (struct attribute *attr)
18802{
18803 return (attr == NULL ? 0 :
18804 attr->form == DW_FORM_block1
18805 || attr->form == DW_FORM_block2
18806 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18807 || attr->form == DW_FORM_block
18808 || attr->form == DW_FORM_exprloc);
8e19ed76 18809}
4c2df51b 18810
c6a0999f
JB
18811/* Return non-zero if ATTR's value is a section offset --- classes
18812 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18813 You may use DW_UNSND (attr) to retrieve such offsets.
18814
18815 Section 7.5.4, "Attribute Encodings", explains that no attribute
18816 may have a value that belongs to more than one of these classes; it
18817 would be ambiguous if we did, because we use the same forms for all
18818 of them. */
380bca97 18819
3690dd37
JB
18820static int
18821attr_form_is_section_offset (struct attribute *attr)
18822{
18823 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18824 || attr->form == DW_FORM_data8
18825 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18826}
18827
3690dd37
JB
18828/* Return non-zero if ATTR's value falls in the 'constant' class, or
18829 zero otherwise. When this function returns true, you can apply
18830 dwarf2_get_attr_constant_value to it.
18831
18832 However, note that for some attributes you must check
18833 attr_form_is_section_offset before using this test. DW_FORM_data4
18834 and DW_FORM_data8 are members of both the constant class, and of
18835 the classes that contain offsets into other debug sections
18836 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18837 that, if an attribute's can be either a constant or one of the
18838 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18839 taken as section offsets, not constants. */
380bca97 18840
3690dd37
JB
18841static int
18842attr_form_is_constant (struct attribute *attr)
18843{
18844 switch (attr->form)
18845 {
18846 case DW_FORM_sdata:
18847 case DW_FORM_udata:
18848 case DW_FORM_data1:
18849 case DW_FORM_data2:
18850 case DW_FORM_data4:
18851 case DW_FORM_data8:
18852 return 1;
18853 default:
18854 return 0;
18855 }
18856}
18857
3019eac3
DE
18858/* Return the .debug_loc section to use for CU.
18859 For DWO files use .debug_loc.dwo. */
18860
18861static struct dwarf2_section_info *
18862cu_debug_loc_section (struct dwarf2_cu *cu)
18863{
18864 if (cu->dwo_unit)
18865 return &cu->dwo_unit->dwo_file->sections.loc;
18866 return &dwarf2_per_objfile->loc;
18867}
18868
8cf6f0b1
TT
18869/* A helper function that fills in a dwarf2_loclist_baton. */
18870
18871static void
18872fill_in_loclist_baton (struct dwarf2_cu *cu,
18873 struct dwarf2_loclist_baton *baton,
18874 struct attribute *attr)
18875{
3019eac3
DE
18876 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18877
18878 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18879
18880 baton->per_cu = cu->per_cu;
18881 gdb_assert (baton->per_cu);
18882 /* We don't know how long the location list is, but make sure we
18883 don't run off the edge of the section. */
3019eac3
DE
18884 baton->size = section->size - DW_UNSND (attr);
18885 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18886 baton->base_address = cu->base_address;
f664829e 18887 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18888}
18889
4c2df51b
DJ
18890static void
18891dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18892 struct dwarf2_cu *cu)
4c2df51b 18893{
bb5ed363 18894 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18895 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18896
3690dd37 18897 if (attr_form_is_section_offset (attr)
3019eac3 18898 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18899 the section. If so, fall through to the complaint in the
18900 other branch. */
3019eac3 18901 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18902 {
0d53c4c4 18903 struct dwarf2_loclist_baton *baton;
4c2df51b 18904
bb5ed363 18905 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18906 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18907
8cf6f0b1 18908 fill_in_loclist_baton (cu, baton, attr);
be391dca 18909
d00adf39 18910 if (cu->base_known == 0)
0d53c4c4 18911 complaint (&symfile_complaints,
3e43a32a
MS
18912 _("Location list used without "
18913 "specifying the CU base address."));
4c2df51b 18914
768a979c 18915 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18916 SYMBOL_LOCATION_BATON (sym) = baton;
18917 }
18918 else
18919 {
18920 struct dwarf2_locexpr_baton *baton;
18921
bb5ed363 18922 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18923 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18924 baton->per_cu = cu->per_cu;
18925 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18926
18927 if (attr_form_is_block (attr))
18928 {
18929 /* Note that we're just copying the block's data pointer
18930 here, not the actual data. We're still pointing into the
6502dd73
DJ
18931 info_buffer for SYM's objfile; right now we never release
18932 that buffer, but when we do clean up properly this may
18933 need to change. */
0d53c4c4
DJ
18934 baton->size = DW_BLOCK (attr)->size;
18935 baton->data = DW_BLOCK (attr)->data;
18936 }
18937 else
18938 {
18939 dwarf2_invalid_attrib_class_complaint ("location description",
18940 SYMBOL_NATURAL_NAME (sym));
18941 baton->size = 0;
0d53c4c4 18942 }
6e70227d 18943
768a979c 18944 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18945 SYMBOL_LOCATION_BATON (sym) = baton;
18946 }
4c2df51b 18947}
6502dd73 18948
9aa1f1e3
TT
18949/* Return the OBJFILE associated with the compilation unit CU. If CU
18950 came from a separate debuginfo file, then the master objfile is
18951 returned. */
ae0d2f24
UW
18952
18953struct objfile *
18954dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18955{
9291a0cd 18956 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18957
18958 /* Return the master objfile, so that we can report and look up the
18959 correct file containing this variable. */
18960 if (objfile->separate_debug_objfile_backlink)
18961 objfile = objfile->separate_debug_objfile_backlink;
18962
18963 return objfile;
18964}
18965
96408a79
SA
18966/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18967 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18968 CU_HEADERP first. */
18969
18970static const struct comp_unit_head *
18971per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18972 struct dwarf2_per_cu_data *per_cu)
18973{
96408a79
SA
18974 gdb_byte *info_ptr;
18975
18976 if (per_cu->cu)
18977 return &per_cu->cu->header;
18978
0bc3a05c 18979 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
18980
18981 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 18982 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
18983
18984 return cu_headerp;
18985}
18986
ae0d2f24
UW
18987/* Return the address size given in the compilation unit header for CU. */
18988
98714339 18989int
ae0d2f24
UW
18990dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18991{
96408a79
SA
18992 struct comp_unit_head cu_header_local;
18993 const struct comp_unit_head *cu_headerp;
c471e790 18994
96408a79
SA
18995 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18996
18997 return cu_headerp->addr_size;
ae0d2f24
UW
18998}
18999
9eae7c52
TT
19000/* Return the offset size given in the compilation unit header for CU. */
19001
19002int
19003dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19004{
96408a79
SA
19005 struct comp_unit_head cu_header_local;
19006 const struct comp_unit_head *cu_headerp;
9c6c53f7 19007
96408a79
SA
19008 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19009
19010 return cu_headerp->offset_size;
19011}
19012
19013/* See its dwarf2loc.h declaration. */
19014
19015int
19016dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19017{
19018 struct comp_unit_head cu_header_local;
19019 const struct comp_unit_head *cu_headerp;
19020
19021 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19022
19023 if (cu_headerp->version == 2)
19024 return cu_headerp->addr_size;
19025 else
19026 return cu_headerp->offset_size;
181cebd4
JK
19027}
19028
9aa1f1e3
TT
19029/* Return the text offset of the CU. The returned offset comes from
19030 this CU's objfile. If this objfile came from a separate debuginfo
19031 file, then the offset may be different from the corresponding
19032 offset in the parent objfile. */
19033
19034CORE_ADDR
19035dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19036{
bb3fa9d0 19037 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19038
19039 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19040}
19041
348e048f
DE
19042/* Locate the .debug_info compilation unit from CU's objfile which contains
19043 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19044
19045static struct dwarf2_per_cu_data *
b64f50a1 19046dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19047 unsigned int offset_in_dwz,
ae038cb0
DJ
19048 struct objfile *objfile)
19049{
19050 struct dwarf2_per_cu_data *this_cu;
19051 int low, high;
36586728 19052 const sect_offset *cu_off;
ae038cb0 19053
ae038cb0
DJ
19054 low = 0;
19055 high = dwarf2_per_objfile->n_comp_units - 1;
19056 while (high > low)
19057 {
36586728 19058 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19059 int mid = low + (high - low) / 2;
9a619af0 19060
36586728
TT
19061 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19062 cu_off = &mid_cu->offset;
19063 if (mid_cu->is_dwz > offset_in_dwz
19064 || (mid_cu->is_dwz == offset_in_dwz
19065 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19066 high = mid;
19067 else
19068 low = mid + 1;
19069 }
19070 gdb_assert (low == high);
36586728
TT
19071 this_cu = dwarf2_per_objfile->all_comp_units[low];
19072 cu_off = &this_cu->offset;
19073 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19074 {
36586728 19075 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19076 error (_("Dwarf Error: could not find partial DIE containing "
19077 "offset 0x%lx [in module %s]"),
b64f50a1 19078 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19079
b64f50a1
JK
19080 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19081 <= offset.sect_off);
ae038cb0
DJ
19082 return dwarf2_per_objfile->all_comp_units[low-1];
19083 }
19084 else
19085 {
19086 this_cu = dwarf2_per_objfile->all_comp_units[low];
19087 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19088 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19089 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19090 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19091 return this_cu;
19092 }
19093}
19094
23745b47 19095/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19096
9816fde3 19097static void
23745b47 19098init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19099{
9816fde3 19100 memset (cu, 0, sizeof (*cu));
23745b47
DE
19101 per_cu->cu = cu;
19102 cu->per_cu = per_cu;
19103 cu->objfile = per_cu->objfile;
93311388 19104 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19105}
19106
19107/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19108
19109static void
95554aad
TT
19110prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19111 enum language pretend_language)
9816fde3
JK
19112{
19113 struct attribute *attr;
19114
19115 /* Set the language we're debugging. */
19116 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19117 if (attr)
19118 set_cu_language (DW_UNSND (attr), cu);
19119 else
9cded63f 19120 {
95554aad 19121 cu->language = pretend_language;
9cded63f
TT
19122 cu->language_defn = language_def (cu->language);
19123 }
dee91e82
DE
19124
19125 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19126 if (attr)
19127 cu->producer = DW_STRING (attr);
93311388
DE
19128}
19129
ae038cb0
DJ
19130/* Release one cached compilation unit, CU. We unlink it from the tree
19131 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19132 the caller is responsible for that.
19133 NOTE: DATA is a void * because this function is also used as a
19134 cleanup routine. */
ae038cb0
DJ
19135
19136static void
68dc6402 19137free_heap_comp_unit (void *data)
ae038cb0
DJ
19138{
19139 struct dwarf2_cu *cu = data;
19140
23745b47
DE
19141 gdb_assert (cu->per_cu != NULL);
19142 cu->per_cu->cu = NULL;
ae038cb0
DJ
19143 cu->per_cu = NULL;
19144
19145 obstack_free (&cu->comp_unit_obstack, NULL);
19146
19147 xfree (cu);
19148}
19149
72bf9492 19150/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19151 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19152 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19153
19154static void
19155free_stack_comp_unit (void *data)
19156{
19157 struct dwarf2_cu *cu = data;
19158
23745b47
DE
19159 gdb_assert (cu->per_cu != NULL);
19160 cu->per_cu->cu = NULL;
19161 cu->per_cu = NULL;
19162
72bf9492
DJ
19163 obstack_free (&cu->comp_unit_obstack, NULL);
19164 cu->partial_dies = NULL;
ae038cb0
DJ
19165}
19166
19167/* Free all cached compilation units. */
19168
19169static void
19170free_cached_comp_units (void *data)
19171{
19172 struct dwarf2_per_cu_data *per_cu, **last_chain;
19173
19174 per_cu = dwarf2_per_objfile->read_in_chain;
19175 last_chain = &dwarf2_per_objfile->read_in_chain;
19176 while (per_cu != NULL)
19177 {
19178 struct dwarf2_per_cu_data *next_cu;
19179
19180 next_cu = per_cu->cu->read_in_chain;
19181
68dc6402 19182 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19183 *last_chain = next_cu;
19184
19185 per_cu = next_cu;
19186 }
19187}
19188
19189/* Increase the age counter on each cached compilation unit, and free
19190 any that are too old. */
19191
19192static void
19193age_cached_comp_units (void)
19194{
19195 struct dwarf2_per_cu_data *per_cu, **last_chain;
19196
19197 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19198 per_cu = dwarf2_per_objfile->read_in_chain;
19199 while (per_cu != NULL)
19200 {
19201 per_cu->cu->last_used ++;
19202 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19203 dwarf2_mark (per_cu->cu);
19204 per_cu = per_cu->cu->read_in_chain;
19205 }
19206
19207 per_cu = dwarf2_per_objfile->read_in_chain;
19208 last_chain = &dwarf2_per_objfile->read_in_chain;
19209 while (per_cu != NULL)
19210 {
19211 struct dwarf2_per_cu_data *next_cu;
19212
19213 next_cu = per_cu->cu->read_in_chain;
19214
19215 if (!per_cu->cu->mark)
19216 {
68dc6402 19217 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19218 *last_chain = next_cu;
19219 }
19220 else
19221 last_chain = &per_cu->cu->read_in_chain;
19222
19223 per_cu = next_cu;
19224 }
19225}
19226
19227/* Remove a single compilation unit from the cache. */
19228
19229static void
dee91e82 19230free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19231{
19232 struct dwarf2_per_cu_data *per_cu, **last_chain;
19233
19234 per_cu = dwarf2_per_objfile->read_in_chain;
19235 last_chain = &dwarf2_per_objfile->read_in_chain;
19236 while (per_cu != NULL)
19237 {
19238 struct dwarf2_per_cu_data *next_cu;
19239
19240 next_cu = per_cu->cu->read_in_chain;
19241
dee91e82 19242 if (per_cu == target_per_cu)
ae038cb0 19243 {
68dc6402 19244 free_heap_comp_unit (per_cu->cu);
dee91e82 19245 per_cu->cu = NULL;
ae038cb0
DJ
19246 *last_chain = next_cu;
19247 break;
19248 }
19249 else
19250 last_chain = &per_cu->cu->read_in_chain;
19251
19252 per_cu = next_cu;
19253 }
19254}
19255
fe3e1990
DJ
19256/* Release all extra memory associated with OBJFILE. */
19257
19258void
19259dwarf2_free_objfile (struct objfile *objfile)
19260{
19261 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19262
19263 if (dwarf2_per_objfile == NULL)
19264 return;
19265
19266 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19267 free_cached_comp_units (NULL);
19268
7b9f3c50
DE
19269 if (dwarf2_per_objfile->quick_file_names_table)
19270 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19271
fe3e1990
DJ
19272 /* Everything else should be on the objfile obstack. */
19273}
19274
dee91e82
DE
19275/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19276 We store these in a hash table separate from the DIEs, and preserve them
19277 when the DIEs are flushed out of cache.
19278
19279 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19280 uniquely identify the type. A file may have multiple .debug_types sections,
19281 or the type may come from a DWO file. We have to use something in
19282 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19283 routine, get_die_type_at_offset, from outside this file, and thus won't
19284 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19285 of the objfile. */
1c379e20 19286
dee91e82 19287struct dwarf2_per_cu_offset_and_type
1c379e20 19288{
dee91e82 19289 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19290 sect_offset offset;
1c379e20
DJ
19291 struct type *type;
19292};
19293
dee91e82 19294/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19295
19296static hashval_t
dee91e82 19297per_cu_offset_and_type_hash (const void *item)
1c379e20 19298{
dee91e82 19299 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19300
dee91e82 19301 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19302}
19303
dee91e82 19304/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19305
19306static int
dee91e82 19307per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19308{
dee91e82
DE
19309 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19310 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19311
dee91e82
DE
19312 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19313 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19314}
19315
19316/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19317 table if necessary. For convenience, return TYPE.
19318
19319 The DIEs reading must have careful ordering to:
19320 * Not cause infite loops trying to read in DIEs as a prerequisite for
19321 reading current DIE.
19322 * Not trying to dereference contents of still incompletely read in types
19323 while reading in other DIEs.
19324 * Enable referencing still incompletely read in types just by a pointer to
19325 the type without accessing its fields.
19326
19327 Therefore caller should follow these rules:
19328 * Try to fetch any prerequisite types we may need to build this DIE type
19329 before building the type and calling set_die_type.
e71ec853 19330 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19331 possible before fetching more types to complete the current type.
19332 * Make the type as complete as possible before fetching more types. */
1c379e20 19333
f792889a 19334static struct type *
1c379e20
DJ
19335set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19336{
dee91e82 19337 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19338 struct objfile *objfile = cu->objfile;
1c379e20 19339
b4ba55a1
JB
19340 /* For Ada types, make sure that the gnat-specific data is always
19341 initialized (if not already set). There are a few types where
19342 we should not be doing so, because the type-specific area is
19343 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19344 where the type-specific area is used to store the floatformat).
19345 But this is not a problem, because the gnat-specific information
19346 is actually not needed for these types. */
19347 if (need_gnat_info (cu)
19348 && TYPE_CODE (type) != TYPE_CODE_FUNC
19349 && TYPE_CODE (type) != TYPE_CODE_FLT
19350 && !HAVE_GNAT_AUX_INFO (type))
19351 INIT_GNAT_SPECIFIC (type);
19352
dee91e82 19353 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19354 {
dee91e82
DE
19355 dwarf2_per_objfile->die_type_hash =
19356 htab_create_alloc_ex (127,
19357 per_cu_offset_and_type_hash,
19358 per_cu_offset_and_type_eq,
19359 NULL,
19360 &objfile->objfile_obstack,
19361 hashtab_obstack_allocate,
19362 dummy_obstack_deallocate);
f792889a 19363 }
1c379e20 19364
dee91e82 19365 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19366 ofs.offset = die->offset;
19367 ofs.type = type;
dee91e82
DE
19368 slot = (struct dwarf2_per_cu_offset_and_type **)
19369 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19370 if (*slot)
19371 complaint (&symfile_complaints,
19372 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19373 die->offset.sect_off);
673bfd45 19374 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19375 **slot = ofs;
f792889a 19376 return type;
1c379e20
DJ
19377}
19378
380bca97 19379/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19380 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19381
19382static struct type *
b64f50a1 19383get_die_type_at_offset (sect_offset offset,
673bfd45 19384 struct dwarf2_per_cu_data *per_cu)
1c379e20 19385{
dee91e82 19386 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19387
dee91e82 19388 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19389 return NULL;
1c379e20 19390
dee91e82 19391 ofs.per_cu = per_cu;
673bfd45 19392 ofs.offset = offset;
dee91e82 19393 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19394 if (slot)
19395 return slot->type;
19396 else
19397 return NULL;
19398}
19399
673bfd45
DE
19400/* Look up the type for DIE in the appropriate type_hash table,
19401 or return NULL if DIE does not have a saved type. */
19402
19403static struct type *
19404get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19405{
19406 return get_die_type_at_offset (die->offset, cu->per_cu);
19407}
19408
10b3939b
DJ
19409/* Add a dependence relationship from CU to REF_PER_CU. */
19410
19411static void
19412dwarf2_add_dependence (struct dwarf2_cu *cu,
19413 struct dwarf2_per_cu_data *ref_per_cu)
19414{
19415 void **slot;
19416
19417 if (cu->dependencies == NULL)
19418 cu->dependencies
19419 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19420 NULL, &cu->comp_unit_obstack,
19421 hashtab_obstack_allocate,
19422 dummy_obstack_deallocate);
19423
19424 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19425 if (*slot == NULL)
19426 *slot = ref_per_cu;
19427}
1c379e20 19428
f504f079
DE
19429/* Subroutine of dwarf2_mark to pass to htab_traverse.
19430 Set the mark field in every compilation unit in the
ae038cb0
DJ
19431 cache that we must keep because we are keeping CU. */
19432
10b3939b
DJ
19433static int
19434dwarf2_mark_helper (void **slot, void *data)
19435{
19436 struct dwarf2_per_cu_data *per_cu;
19437
19438 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19439
19440 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19441 reading of the chain. As such dependencies remain valid it is not much
19442 useful to track and undo them during QUIT cleanups. */
19443 if (per_cu->cu == NULL)
19444 return 1;
19445
10b3939b
DJ
19446 if (per_cu->cu->mark)
19447 return 1;
19448 per_cu->cu->mark = 1;
19449
19450 if (per_cu->cu->dependencies != NULL)
19451 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19452
19453 return 1;
19454}
19455
f504f079
DE
19456/* Set the mark field in CU and in every other compilation unit in the
19457 cache that we must keep because we are keeping CU. */
19458
ae038cb0
DJ
19459static void
19460dwarf2_mark (struct dwarf2_cu *cu)
19461{
19462 if (cu->mark)
19463 return;
19464 cu->mark = 1;
10b3939b
DJ
19465 if (cu->dependencies != NULL)
19466 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19467}
19468
19469static void
19470dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19471{
19472 while (per_cu)
19473 {
19474 per_cu->cu->mark = 0;
19475 per_cu = per_cu->cu->read_in_chain;
19476 }
72bf9492
DJ
19477}
19478
72bf9492
DJ
19479/* Trivial hash function for partial_die_info: the hash value of a DIE
19480 is its offset in .debug_info for this objfile. */
19481
19482static hashval_t
19483partial_die_hash (const void *item)
19484{
19485 const struct partial_die_info *part_die = item;
9a619af0 19486
b64f50a1 19487 return part_die->offset.sect_off;
72bf9492
DJ
19488}
19489
19490/* Trivial comparison function for partial_die_info structures: two DIEs
19491 are equal if they have the same offset. */
19492
19493static int
19494partial_die_eq (const void *item_lhs, const void *item_rhs)
19495{
19496 const struct partial_die_info *part_die_lhs = item_lhs;
19497 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19498
b64f50a1 19499 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19500}
19501
ae038cb0
DJ
19502static struct cmd_list_element *set_dwarf2_cmdlist;
19503static struct cmd_list_element *show_dwarf2_cmdlist;
19504
19505static void
19506set_dwarf2_cmd (char *args, int from_tty)
19507{
19508 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19509}
19510
19511static void
19512show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19513{
ae038cb0
DJ
19514 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19515}
19516
4bf44c1c 19517/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19518
19519static void
c1bd65d0 19520dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19521{
19522 struct dwarf2_per_objfile *data = d;
8b70b953 19523 int ix;
8b70b953 19524
95554aad
TT
19525 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19526 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19527 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19528
8b70b953 19529 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19530
19531 if (data->dwo_files)
19532 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19533
19534 if (data->dwz_file && data->dwz_file->dwz_bfd)
19535 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19536}
19537
19538\f
ae2de4f8 19539/* The "save gdb-index" command. */
9291a0cd
TT
19540
19541/* The contents of the hash table we create when building the string
19542 table. */
19543struct strtab_entry
19544{
19545 offset_type offset;
19546 const char *str;
19547};
19548
559a7a62
JK
19549/* Hash function for a strtab_entry.
19550
19551 Function is used only during write_hash_table so no index format backward
19552 compatibility is needed. */
b89be57b 19553
9291a0cd
TT
19554static hashval_t
19555hash_strtab_entry (const void *e)
19556{
19557 const struct strtab_entry *entry = e;
559a7a62 19558 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19559}
19560
19561/* Equality function for a strtab_entry. */
b89be57b 19562
9291a0cd
TT
19563static int
19564eq_strtab_entry (const void *a, const void *b)
19565{
19566 const struct strtab_entry *ea = a;
19567 const struct strtab_entry *eb = b;
19568 return !strcmp (ea->str, eb->str);
19569}
19570
19571/* Create a strtab_entry hash table. */
b89be57b 19572
9291a0cd
TT
19573static htab_t
19574create_strtab (void)
19575{
19576 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19577 xfree, xcalloc, xfree);
19578}
19579
19580/* Add a string to the constant pool. Return the string's offset in
19581 host order. */
b89be57b 19582
9291a0cd
TT
19583static offset_type
19584add_string (htab_t table, struct obstack *cpool, const char *str)
19585{
19586 void **slot;
19587 struct strtab_entry entry;
19588 struct strtab_entry *result;
19589
19590 entry.str = str;
19591 slot = htab_find_slot (table, &entry, INSERT);
19592 if (*slot)
19593 result = *slot;
19594 else
19595 {
19596 result = XNEW (struct strtab_entry);
19597 result->offset = obstack_object_size (cpool);
19598 result->str = str;
19599 obstack_grow_str0 (cpool, str);
19600 *slot = result;
19601 }
19602 return result->offset;
19603}
19604
19605/* An entry in the symbol table. */
19606struct symtab_index_entry
19607{
19608 /* The name of the symbol. */
19609 const char *name;
19610 /* The offset of the name in the constant pool. */
19611 offset_type index_offset;
19612 /* A sorted vector of the indices of all the CUs that hold an object
19613 of this name. */
19614 VEC (offset_type) *cu_indices;
19615};
19616
19617/* The symbol table. This is a power-of-2-sized hash table. */
19618struct mapped_symtab
19619{
19620 offset_type n_elements;
19621 offset_type size;
19622 struct symtab_index_entry **data;
19623};
19624
19625/* Hash function for a symtab_index_entry. */
b89be57b 19626
9291a0cd
TT
19627static hashval_t
19628hash_symtab_entry (const void *e)
19629{
19630 const struct symtab_index_entry *entry = e;
19631 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19632 sizeof (offset_type) * VEC_length (offset_type,
19633 entry->cu_indices),
19634 0);
19635}
19636
19637/* Equality function for a symtab_index_entry. */
b89be57b 19638
9291a0cd
TT
19639static int
19640eq_symtab_entry (const void *a, const void *b)
19641{
19642 const struct symtab_index_entry *ea = a;
19643 const struct symtab_index_entry *eb = b;
19644 int len = VEC_length (offset_type, ea->cu_indices);
19645 if (len != VEC_length (offset_type, eb->cu_indices))
19646 return 0;
19647 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19648 VEC_address (offset_type, eb->cu_indices),
19649 sizeof (offset_type) * len);
19650}
19651
19652/* Destroy a symtab_index_entry. */
b89be57b 19653
9291a0cd
TT
19654static void
19655delete_symtab_entry (void *p)
19656{
19657 struct symtab_index_entry *entry = p;
19658 VEC_free (offset_type, entry->cu_indices);
19659 xfree (entry);
19660}
19661
19662/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19663
9291a0cd 19664static htab_t
3876f04e 19665create_symbol_hash_table (void)
9291a0cd
TT
19666{
19667 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19668 delete_symtab_entry, xcalloc, xfree);
19669}
19670
19671/* Create a new mapped symtab object. */
b89be57b 19672
9291a0cd
TT
19673static struct mapped_symtab *
19674create_mapped_symtab (void)
19675{
19676 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19677 symtab->n_elements = 0;
19678 symtab->size = 1024;
19679 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19680 return symtab;
19681}
19682
19683/* Destroy a mapped_symtab. */
b89be57b 19684
9291a0cd
TT
19685static void
19686cleanup_mapped_symtab (void *p)
19687{
19688 struct mapped_symtab *symtab = p;
19689 /* The contents of the array are freed when the other hash table is
19690 destroyed. */
19691 xfree (symtab->data);
19692 xfree (symtab);
19693}
19694
19695/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19696 the slot.
19697
19698 Function is used only during write_hash_table so no index format backward
19699 compatibility is needed. */
b89be57b 19700
9291a0cd
TT
19701static struct symtab_index_entry **
19702find_slot (struct mapped_symtab *symtab, const char *name)
19703{
559a7a62 19704 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19705
19706 index = hash & (symtab->size - 1);
19707 step = ((hash * 17) & (symtab->size - 1)) | 1;
19708
19709 for (;;)
19710 {
19711 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19712 return &symtab->data[index];
19713 index = (index + step) & (symtab->size - 1);
19714 }
19715}
19716
19717/* Expand SYMTAB's hash table. */
b89be57b 19718
9291a0cd
TT
19719static void
19720hash_expand (struct mapped_symtab *symtab)
19721{
19722 offset_type old_size = symtab->size;
19723 offset_type i;
19724 struct symtab_index_entry **old_entries = symtab->data;
19725
19726 symtab->size *= 2;
19727 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19728
19729 for (i = 0; i < old_size; ++i)
19730 {
19731 if (old_entries[i])
19732 {
19733 struct symtab_index_entry **slot = find_slot (symtab,
19734 old_entries[i]->name);
19735 *slot = old_entries[i];
19736 }
19737 }
19738
19739 xfree (old_entries);
19740}
19741
156942c7
DE
19742/* Add an entry to SYMTAB. NAME is the name of the symbol.
19743 CU_INDEX is the index of the CU in which the symbol appears.
19744 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19745
9291a0cd
TT
19746static void
19747add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19748 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19749 offset_type cu_index)
19750{
19751 struct symtab_index_entry **slot;
156942c7 19752 offset_type cu_index_and_attrs;
9291a0cd
TT
19753
19754 ++symtab->n_elements;
19755 if (4 * symtab->n_elements / 3 >= symtab->size)
19756 hash_expand (symtab);
19757
19758 slot = find_slot (symtab, name);
19759 if (!*slot)
19760 {
19761 *slot = XNEW (struct symtab_index_entry);
19762 (*slot)->name = name;
156942c7 19763 /* index_offset is set later. */
9291a0cd
TT
19764 (*slot)->cu_indices = NULL;
19765 }
156942c7
DE
19766
19767 cu_index_and_attrs = 0;
19768 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19769 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19770 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19771
19772 /* We don't want to record an index value twice as we want to avoid the
19773 duplication.
19774 We process all global symbols and then all static symbols
19775 (which would allow us to avoid the duplication by only having to check
19776 the last entry pushed), but a symbol could have multiple kinds in one CU.
19777 To keep things simple we don't worry about the duplication here and
19778 sort and uniqufy the list after we've processed all symbols. */
19779 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19780}
19781
19782/* qsort helper routine for uniquify_cu_indices. */
19783
19784static int
19785offset_type_compare (const void *ap, const void *bp)
19786{
19787 offset_type a = *(offset_type *) ap;
19788 offset_type b = *(offset_type *) bp;
19789
19790 return (a > b) - (b > a);
19791}
19792
19793/* Sort and remove duplicates of all symbols' cu_indices lists. */
19794
19795static void
19796uniquify_cu_indices (struct mapped_symtab *symtab)
19797{
19798 int i;
19799
19800 for (i = 0; i < symtab->size; ++i)
19801 {
19802 struct symtab_index_entry *entry = symtab->data[i];
19803
19804 if (entry
19805 && entry->cu_indices != NULL)
19806 {
19807 unsigned int next_to_insert, next_to_check;
19808 offset_type last_value;
19809
19810 qsort (VEC_address (offset_type, entry->cu_indices),
19811 VEC_length (offset_type, entry->cu_indices),
19812 sizeof (offset_type), offset_type_compare);
19813
19814 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19815 next_to_insert = 1;
19816 for (next_to_check = 1;
19817 next_to_check < VEC_length (offset_type, entry->cu_indices);
19818 ++next_to_check)
19819 {
19820 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19821 != last_value)
19822 {
19823 last_value = VEC_index (offset_type, entry->cu_indices,
19824 next_to_check);
19825 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19826 last_value);
19827 ++next_to_insert;
19828 }
19829 }
19830 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19831 }
19832 }
9291a0cd
TT
19833}
19834
19835/* Add a vector of indices to the constant pool. */
b89be57b 19836
9291a0cd 19837static offset_type
3876f04e 19838add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19839 struct symtab_index_entry *entry)
19840{
19841 void **slot;
19842
3876f04e 19843 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19844 if (!*slot)
19845 {
19846 offset_type len = VEC_length (offset_type, entry->cu_indices);
19847 offset_type val = MAYBE_SWAP (len);
19848 offset_type iter;
19849 int i;
19850
19851 *slot = entry;
19852 entry->index_offset = obstack_object_size (cpool);
19853
19854 obstack_grow (cpool, &val, sizeof (val));
19855 for (i = 0;
19856 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19857 ++i)
19858 {
19859 val = MAYBE_SWAP (iter);
19860 obstack_grow (cpool, &val, sizeof (val));
19861 }
19862 }
19863 else
19864 {
19865 struct symtab_index_entry *old_entry = *slot;
19866 entry->index_offset = old_entry->index_offset;
19867 entry = old_entry;
19868 }
19869 return entry->index_offset;
19870}
19871
19872/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19873 constant pool entries going into the obstack CPOOL. */
b89be57b 19874
9291a0cd
TT
19875static void
19876write_hash_table (struct mapped_symtab *symtab,
19877 struct obstack *output, struct obstack *cpool)
19878{
19879 offset_type i;
3876f04e 19880 htab_t symbol_hash_table;
9291a0cd
TT
19881 htab_t str_table;
19882
3876f04e 19883 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19884 str_table = create_strtab ();
3876f04e 19885
9291a0cd
TT
19886 /* We add all the index vectors to the constant pool first, to
19887 ensure alignment is ok. */
19888 for (i = 0; i < symtab->size; ++i)
19889 {
19890 if (symtab->data[i])
3876f04e 19891 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19892 }
19893
19894 /* Now write out the hash table. */
19895 for (i = 0; i < symtab->size; ++i)
19896 {
19897 offset_type str_off, vec_off;
19898
19899 if (symtab->data[i])
19900 {
19901 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19902 vec_off = symtab->data[i]->index_offset;
19903 }
19904 else
19905 {
19906 /* While 0 is a valid constant pool index, it is not valid
19907 to have 0 for both offsets. */
19908 str_off = 0;
19909 vec_off = 0;
19910 }
19911
19912 str_off = MAYBE_SWAP (str_off);
19913 vec_off = MAYBE_SWAP (vec_off);
19914
19915 obstack_grow (output, &str_off, sizeof (str_off));
19916 obstack_grow (output, &vec_off, sizeof (vec_off));
19917 }
19918
19919 htab_delete (str_table);
3876f04e 19920 htab_delete (symbol_hash_table);
9291a0cd
TT
19921}
19922
0a5429f6
DE
19923/* Struct to map psymtab to CU index in the index file. */
19924struct psymtab_cu_index_map
19925{
19926 struct partial_symtab *psymtab;
19927 unsigned int cu_index;
19928};
19929
19930static hashval_t
19931hash_psymtab_cu_index (const void *item)
19932{
19933 const struct psymtab_cu_index_map *map = item;
19934
19935 return htab_hash_pointer (map->psymtab);
19936}
19937
19938static int
19939eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19940{
19941 const struct psymtab_cu_index_map *lhs = item_lhs;
19942 const struct psymtab_cu_index_map *rhs = item_rhs;
19943
19944 return lhs->psymtab == rhs->psymtab;
19945}
19946
19947/* Helper struct for building the address table. */
19948struct addrmap_index_data
19949{
19950 struct objfile *objfile;
19951 struct obstack *addr_obstack;
19952 htab_t cu_index_htab;
19953
19954 /* Non-zero if the previous_* fields are valid.
19955 We can't write an entry until we see the next entry (since it is only then
19956 that we know the end of the entry). */
19957 int previous_valid;
19958 /* Index of the CU in the table of all CUs in the index file. */
19959 unsigned int previous_cu_index;
0963b4bd 19960 /* Start address of the CU. */
0a5429f6
DE
19961 CORE_ADDR previous_cu_start;
19962};
19963
19964/* Write an address entry to OBSTACK. */
b89be57b 19965
9291a0cd 19966static void
0a5429f6
DE
19967add_address_entry (struct objfile *objfile, struct obstack *obstack,
19968 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 19969{
0a5429f6 19970 offset_type cu_index_to_write;
9291a0cd
TT
19971 char addr[8];
19972 CORE_ADDR baseaddr;
19973
19974 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19975
0a5429f6
DE
19976 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19977 obstack_grow (obstack, addr, 8);
19978 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19979 obstack_grow (obstack, addr, 8);
19980 cu_index_to_write = MAYBE_SWAP (cu_index);
19981 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19982}
19983
19984/* Worker function for traversing an addrmap to build the address table. */
19985
19986static int
19987add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19988{
19989 struct addrmap_index_data *data = datap;
19990 struct partial_symtab *pst = obj;
0a5429f6
DE
19991
19992 if (data->previous_valid)
19993 add_address_entry (data->objfile, data->addr_obstack,
19994 data->previous_cu_start, start_addr,
19995 data->previous_cu_index);
19996
19997 data->previous_cu_start = start_addr;
19998 if (pst != NULL)
19999 {
20000 struct psymtab_cu_index_map find_map, *map;
20001 find_map.psymtab = pst;
20002 map = htab_find (data->cu_index_htab, &find_map);
20003 gdb_assert (map != NULL);
20004 data->previous_cu_index = map->cu_index;
20005 data->previous_valid = 1;
20006 }
20007 else
20008 data->previous_valid = 0;
20009
20010 return 0;
20011}
20012
20013/* Write OBJFILE's address map to OBSTACK.
20014 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20015 in the index file. */
20016
20017static void
20018write_address_map (struct objfile *objfile, struct obstack *obstack,
20019 htab_t cu_index_htab)
20020{
20021 struct addrmap_index_data addrmap_index_data;
20022
20023 /* When writing the address table, we have to cope with the fact that
20024 the addrmap iterator only provides the start of a region; we have to
20025 wait until the next invocation to get the start of the next region. */
20026
20027 addrmap_index_data.objfile = objfile;
20028 addrmap_index_data.addr_obstack = obstack;
20029 addrmap_index_data.cu_index_htab = cu_index_htab;
20030 addrmap_index_data.previous_valid = 0;
20031
20032 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20033 &addrmap_index_data);
20034
20035 /* It's highly unlikely the last entry (end address = 0xff...ff)
20036 is valid, but we should still handle it.
20037 The end address is recorded as the start of the next region, but that
20038 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20039 anyway. */
20040 if (addrmap_index_data.previous_valid)
20041 add_address_entry (objfile, obstack,
20042 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20043 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20044}
20045
156942c7
DE
20046/* Return the symbol kind of PSYM. */
20047
20048static gdb_index_symbol_kind
20049symbol_kind (struct partial_symbol *psym)
20050{
20051 domain_enum domain = PSYMBOL_DOMAIN (psym);
20052 enum address_class aclass = PSYMBOL_CLASS (psym);
20053
20054 switch (domain)
20055 {
20056 case VAR_DOMAIN:
20057 switch (aclass)
20058 {
20059 case LOC_BLOCK:
20060 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20061 case LOC_TYPEDEF:
20062 return GDB_INDEX_SYMBOL_KIND_TYPE;
20063 case LOC_COMPUTED:
20064 case LOC_CONST_BYTES:
20065 case LOC_OPTIMIZED_OUT:
20066 case LOC_STATIC:
20067 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20068 case LOC_CONST:
20069 /* Note: It's currently impossible to recognize psyms as enum values
20070 short of reading the type info. For now punt. */
20071 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20072 default:
20073 /* There are other LOC_FOO values that one might want to classify
20074 as variables, but dwarf2read.c doesn't currently use them. */
20075 return GDB_INDEX_SYMBOL_KIND_OTHER;
20076 }
20077 case STRUCT_DOMAIN:
20078 return GDB_INDEX_SYMBOL_KIND_TYPE;
20079 default:
20080 return GDB_INDEX_SYMBOL_KIND_OTHER;
20081 }
20082}
20083
9291a0cd 20084/* Add a list of partial symbols to SYMTAB. */
b89be57b 20085
9291a0cd
TT
20086static void
20087write_psymbols (struct mapped_symtab *symtab,
987d643c 20088 htab_t psyms_seen,
9291a0cd
TT
20089 struct partial_symbol **psymp,
20090 int count,
987d643c
TT
20091 offset_type cu_index,
20092 int is_static)
9291a0cd
TT
20093{
20094 for (; count-- > 0; ++psymp)
20095 {
156942c7
DE
20096 struct partial_symbol *psym = *psymp;
20097 void **slot;
987d643c 20098
156942c7 20099 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20100 error (_("Ada is not currently supported by the index"));
987d643c 20101
987d643c 20102 /* Only add a given psymbol once. */
156942c7 20103 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20104 if (!*slot)
20105 {
156942c7
DE
20106 gdb_index_symbol_kind kind = symbol_kind (psym);
20107
20108 *slot = psym;
20109 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20110 is_static, kind, cu_index);
987d643c 20111 }
9291a0cd
TT
20112 }
20113}
20114
20115/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20116 exception if there is an error. */
b89be57b 20117
9291a0cd
TT
20118static void
20119write_obstack (FILE *file, struct obstack *obstack)
20120{
20121 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20122 file)
20123 != obstack_object_size (obstack))
20124 error (_("couldn't data write to file"));
20125}
20126
20127/* Unlink a file if the argument is not NULL. */
b89be57b 20128
9291a0cd
TT
20129static void
20130unlink_if_set (void *p)
20131{
20132 char **filename = p;
20133 if (*filename)
20134 unlink (*filename);
20135}
20136
1fd400ff
TT
20137/* A helper struct used when iterating over debug_types. */
20138struct signatured_type_index_data
20139{
20140 struct objfile *objfile;
20141 struct mapped_symtab *symtab;
20142 struct obstack *types_list;
987d643c 20143 htab_t psyms_seen;
1fd400ff
TT
20144 int cu_index;
20145};
20146
20147/* A helper function that writes a single signatured_type to an
20148 obstack. */
b89be57b 20149
1fd400ff
TT
20150static int
20151write_one_signatured_type (void **slot, void *d)
20152{
20153 struct signatured_type_index_data *info = d;
20154 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20155 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20156 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20157 gdb_byte val[8];
20158
20159 write_psymbols (info->symtab,
987d643c 20160 info->psyms_seen,
3e43a32a
MS
20161 info->objfile->global_psymbols.list
20162 + psymtab->globals_offset,
987d643c
TT
20163 psymtab->n_global_syms, info->cu_index,
20164 0);
1fd400ff 20165 write_psymbols (info->symtab,
987d643c 20166 info->psyms_seen,
3e43a32a
MS
20167 info->objfile->static_psymbols.list
20168 + psymtab->statics_offset,
987d643c
TT
20169 psymtab->n_static_syms, info->cu_index,
20170 1);
1fd400ff 20171
b64f50a1
JK
20172 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20173 entry->per_cu.offset.sect_off);
1fd400ff 20174 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20175 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20176 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20177 obstack_grow (info->types_list, val, 8);
20178 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20179 obstack_grow (info->types_list, val, 8);
20180
20181 ++info->cu_index;
20182
20183 return 1;
20184}
20185
95554aad
TT
20186/* Recurse into all "included" dependencies and write their symbols as
20187 if they appeared in this psymtab. */
20188
20189static void
20190recursively_write_psymbols (struct objfile *objfile,
20191 struct partial_symtab *psymtab,
20192 struct mapped_symtab *symtab,
20193 htab_t psyms_seen,
20194 offset_type cu_index)
20195{
20196 int i;
20197
20198 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20199 if (psymtab->dependencies[i]->user != NULL)
20200 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20201 symtab, psyms_seen, cu_index);
20202
20203 write_psymbols (symtab,
20204 psyms_seen,
20205 objfile->global_psymbols.list + psymtab->globals_offset,
20206 psymtab->n_global_syms, cu_index,
20207 0);
20208 write_psymbols (symtab,
20209 psyms_seen,
20210 objfile->static_psymbols.list + psymtab->statics_offset,
20211 psymtab->n_static_syms, cu_index,
20212 1);
20213}
20214
9291a0cd 20215/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20216
9291a0cd
TT
20217static void
20218write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20219{
20220 struct cleanup *cleanup;
20221 char *filename, *cleanup_filename;
1fd400ff
TT
20222 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20223 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20224 int i;
20225 FILE *out_file;
20226 struct mapped_symtab *symtab;
20227 offset_type val, size_of_contents, total_len;
20228 struct stat st;
987d643c 20229 htab_t psyms_seen;
0a5429f6
DE
20230 htab_t cu_index_htab;
20231 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20232
b4f2f049 20233 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20234 return;
b4f2f049 20235
9291a0cd
TT
20236 if (dwarf2_per_objfile->using_index)
20237 error (_("Cannot use an index to create the index"));
20238
8b70b953
TT
20239 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20240 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20241
9291a0cd 20242 if (stat (objfile->name, &st) < 0)
7e17e088 20243 perror_with_name (objfile->name);
9291a0cd
TT
20244
20245 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20246 INDEX_SUFFIX, (char *) NULL);
20247 cleanup = make_cleanup (xfree, filename);
20248
20249 out_file = fopen (filename, "wb");
20250 if (!out_file)
20251 error (_("Can't open `%s' for writing"), filename);
20252
20253 cleanup_filename = filename;
20254 make_cleanup (unlink_if_set, &cleanup_filename);
20255
20256 symtab = create_mapped_symtab ();
20257 make_cleanup (cleanup_mapped_symtab, symtab);
20258
20259 obstack_init (&addr_obstack);
20260 make_cleanup_obstack_free (&addr_obstack);
20261
20262 obstack_init (&cu_list);
20263 make_cleanup_obstack_free (&cu_list);
20264
1fd400ff
TT
20265 obstack_init (&types_cu_list);
20266 make_cleanup_obstack_free (&types_cu_list);
20267
987d643c
TT
20268 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20269 NULL, xcalloc, xfree);
96408a79 20270 make_cleanup_htab_delete (psyms_seen);
987d643c 20271
0a5429f6
DE
20272 /* While we're scanning CU's create a table that maps a psymtab pointer
20273 (which is what addrmap records) to its index (which is what is recorded
20274 in the index file). This will later be needed to write the address
20275 table. */
20276 cu_index_htab = htab_create_alloc (100,
20277 hash_psymtab_cu_index,
20278 eq_psymtab_cu_index,
20279 NULL, xcalloc, xfree);
96408a79 20280 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20281 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20282 xmalloc (sizeof (struct psymtab_cu_index_map)
20283 * dwarf2_per_objfile->n_comp_units);
20284 make_cleanup (xfree, psymtab_cu_index_map);
20285
20286 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20287 work here. Also, the debug_types entries do not appear in
20288 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20289 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20290 {
3e43a32a
MS
20291 struct dwarf2_per_cu_data *per_cu
20292 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20293 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20294 gdb_byte val[8];
0a5429f6
DE
20295 struct psymtab_cu_index_map *map;
20296 void **slot;
9291a0cd 20297
95554aad
TT
20298 if (psymtab->user == NULL)
20299 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20300
0a5429f6
DE
20301 map = &psymtab_cu_index_map[i];
20302 map->psymtab = psymtab;
20303 map->cu_index = i;
20304 slot = htab_find_slot (cu_index_htab, map, INSERT);
20305 gdb_assert (slot != NULL);
20306 gdb_assert (*slot == NULL);
20307 *slot = map;
9291a0cd 20308
b64f50a1
JK
20309 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20310 per_cu->offset.sect_off);
9291a0cd 20311 obstack_grow (&cu_list, val, 8);
e254ef6a 20312 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20313 obstack_grow (&cu_list, val, 8);
20314 }
20315
0a5429f6
DE
20316 /* Dump the address map. */
20317 write_address_map (objfile, &addr_obstack, cu_index_htab);
20318
1fd400ff
TT
20319 /* Write out the .debug_type entries, if any. */
20320 if (dwarf2_per_objfile->signatured_types)
20321 {
20322 struct signatured_type_index_data sig_data;
20323
20324 sig_data.objfile = objfile;
20325 sig_data.symtab = symtab;
20326 sig_data.types_list = &types_cu_list;
987d643c 20327 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20328 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20329 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20330 write_one_signatured_type, &sig_data);
20331 }
20332
156942c7
DE
20333 /* Now that we've processed all symbols we can shrink their cu_indices
20334 lists. */
20335 uniquify_cu_indices (symtab);
20336
9291a0cd
TT
20337 obstack_init (&constant_pool);
20338 make_cleanup_obstack_free (&constant_pool);
20339 obstack_init (&symtab_obstack);
20340 make_cleanup_obstack_free (&symtab_obstack);
20341 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20342
20343 obstack_init (&contents);
20344 make_cleanup_obstack_free (&contents);
1fd400ff 20345 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20346 total_len = size_of_contents;
20347
20348 /* The version number. */
156942c7 20349 val = MAYBE_SWAP (7);
9291a0cd
TT
20350 obstack_grow (&contents, &val, sizeof (val));
20351
20352 /* The offset of the CU list from the start of the file. */
20353 val = MAYBE_SWAP (total_len);
20354 obstack_grow (&contents, &val, sizeof (val));
20355 total_len += obstack_object_size (&cu_list);
20356
1fd400ff
TT
20357 /* The offset of the types CU list from the start of the file. */
20358 val = MAYBE_SWAP (total_len);
20359 obstack_grow (&contents, &val, sizeof (val));
20360 total_len += obstack_object_size (&types_cu_list);
20361
9291a0cd
TT
20362 /* The offset of the address table from the start of the file. */
20363 val = MAYBE_SWAP (total_len);
20364 obstack_grow (&contents, &val, sizeof (val));
20365 total_len += obstack_object_size (&addr_obstack);
20366
20367 /* The offset of the symbol table from the start of the file. */
20368 val = MAYBE_SWAP (total_len);
20369 obstack_grow (&contents, &val, sizeof (val));
20370 total_len += obstack_object_size (&symtab_obstack);
20371
20372 /* The offset of the constant pool from the start of the file. */
20373 val = MAYBE_SWAP (total_len);
20374 obstack_grow (&contents, &val, sizeof (val));
20375 total_len += obstack_object_size (&constant_pool);
20376
20377 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20378
20379 write_obstack (out_file, &contents);
20380 write_obstack (out_file, &cu_list);
1fd400ff 20381 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20382 write_obstack (out_file, &addr_obstack);
20383 write_obstack (out_file, &symtab_obstack);
20384 write_obstack (out_file, &constant_pool);
20385
20386 fclose (out_file);
20387
20388 /* We want to keep the file, so we set cleanup_filename to NULL
20389 here. See unlink_if_set. */
20390 cleanup_filename = NULL;
20391
20392 do_cleanups (cleanup);
20393}
20394
90476074
TT
20395/* Implementation of the `save gdb-index' command.
20396
20397 Note that the file format used by this command is documented in the
20398 GDB manual. Any changes here must be documented there. */
11570e71 20399
9291a0cd
TT
20400static void
20401save_gdb_index_command (char *arg, int from_tty)
20402{
20403 struct objfile *objfile;
20404
20405 if (!arg || !*arg)
96d19272 20406 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20407
20408 ALL_OBJFILES (objfile)
20409 {
20410 struct stat st;
20411
20412 /* If the objfile does not correspond to an actual file, skip it. */
20413 if (stat (objfile->name, &st) < 0)
20414 continue;
20415
20416 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20417 if (dwarf2_per_objfile)
20418 {
20419 volatile struct gdb_exception except;
20420
20421 TRY_CATCH (except, RETURN_MASK_ERROR)
20422 {
20423 write_psymtabs_to_index (objfile, arg);
20424 }
20425 if (except.reason < 0)
20426 exception_fprintf (gdb_stderr, except,
20427 _("Error while writing index for `%s': "),
20428 objfile->name);
20429 }
20430 }
dce234bc
PP
20431}
20432
9291a0cd
TT
20433\f
20434
9eae7c52
TT
20435int dwarf2_always_disassemble;
20436
20437static void
20438show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20439 struct cmd_list_element *c, const char *value)
20440{
3e43a32a
MS
20441 fprintf_filtered (file,
20442 _("Whether to always disassemble "
20443 "DWARF expressions is %s.\n"),
9eae7c52
TT
20444 value);
20445}
20446
900e11f9
JK
20447static void
20448show_check_physname (struct ui_file *file, int from_tty,
20449 struct cmd_list_element *c, const char *value)
20450{
20451 fprintf_filtered (file,
20452 _("Whether to check \"physname\" is %s.\n"),
20453 value);
20454}
20455
6502dd73
DJ
20456void _initialize_dwarf2_read (void);
20457
20458void
20459_initialize_dwarf2_read (void)
20460{
96d19272
JK
20461 struct cmd_list_element *c;
20462
dce234bc 20463 dwarf2_objfile_data_key
c1bd65d0 20464 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20465
1bedd215
AC
20466 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20467Set DWARF 2 specific variables.\n\
20468Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20469 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20470 0/*allow-unknown*/, &maintenance_set_cmdlist);
20471
1bedd215
AC
20472 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20473Show DWARF 2 specific variables\n\
20474Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20475 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20476 0/*allow-unknown*/, &maintenance_show_cmdlist);
20477
20478 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20479 &dwarf2_max_cache_age, _("\
20480Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20481Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20482A higher limit means that cached compilation units will be stored\n\
20483in memory longer, and more total memory will be used. Zero disables\n\
20484caching, which can slow down startup."),
2c5b56ce 20485 NULL,
920d2a44 20486 show_dwarf2_max_cache_age,
2c5b56ce 20487 &set_dwarf2_cmdlist,
ae038cb0 20488 &show_dwarf2_cmdlist);
d97bc12b 20489
9eae7c52
TT
20490 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20491 &dwarf2_always_disassemble, _("\
20492Set whether `info address' always disassembles DWARF expressions."), _("\
20493Show whether `info address' always disassembles DWARF expressions."), _("\
20494When enabled, DWARF expressions are always printed in an assembly-like\n\
20495syntax. When disabled, expressions will be printed in a more\n\
20496conversational style, when possible."),
20497 NULL,
20498 show_dwarf2_always_disassemble,
20499 &set_dwarf2_cmdlist,
20500 &show_dwarf2_cmdlist);
20501
45cfd468
DE
20502 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20503Set debugging of the dwarf2 reader."), _("\
20504Show debugging of the dwarf2 reader."), _("\
20505When enabled, debugging messages are printed during dwarf2 reading\n\
20506and symtab expansion."),
20507 NULL,
20508 NULL,
20509 &setdebuglist, &showdebuglist);
20510
ccce17b0 20511 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20512Set debugging of the dwarf2 DIE reader."), _("\
20513Show debugging of the dwarf2 DIE reader."), _("\
20514When enabled (non-zero), DIEs are dumped after they are read in.\n\
20515The value is the maximum depth to print."),
ccce17b0
YQ
20516 NULL,
20517 NULL,
20518 &setdebuglist, &showdebuglist);
9291a0cd 20519
900e11f9
JK
20520 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20521Set cross-checking of \"physname\" code against demangler."), _("\
20522Show cross-checking of \"physname\" code against demangler."), _("\
20523When enabled, GDB's internal \"physname\" code is checked against\n\
20524the demangler."),
20525 NULL, show_check_physname,
20526 &setdebuglist, &showdebuglist);
20527
e615022a
DE
20528 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20529 no_class, &use_deprecated_index_sections, _("\
20530Set whether to use deprecated gdb_index sections."), _("\
20531Show whether to use deprecated gdb_index sections."), _("\
20532When enabled, deprecated .gdb_index sections are used anyway.\n\
20533Normally they are ignored either because of a missing feature or\n\
20534performance issue.\n\
20535Warning: This option must be enabled before gdb reads the file."),
20536 NULL,
20537 NULL,
20538 &setlist, &showlist);
20539
96d19272 20540 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20541 _("\
fc1a9d6e 20542Save a gdb-index file.\n\
11570e71 20543Usage: save gdb-index DIRECTORY"),
96d19272
JK
20544 &save_cmdlist);
20545 set_cmd_completer (c, filename_completer);
6502dd73 20546}
This page took 4.250307 seconds and 4 git commands to generate.