gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
4c2df51b 70
c906108c
SS
71#include <fcntl.h>
72#include "gdb_string.h"
4bdf3d34 73#include "gdb_assert.h"
c906108c 74#include <sys/types.h>
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
45cfd468
DE
79/* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81static int dwarf2_read_debug = 0;
82
d97bc12b 83/* When non-zero, dump DIEs after they are read in. */
ccce17b0 84static unsigned int dwarf2_die_debug = 0;
d97bc12b 85
900e11f9
JK
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
481860b3 89/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 90static int use_deprecated_index_sections = 0;
481860b3 91
df8a16a1
DJ
92/* When set, the file that we're processing is known to have debugging
93 info for C++ namespaces. GCC 3.3.x did not produce this information,
94 but later versions do. */
95
96static int processing_has_namespace_info;
97
6502dd73
DJ
98static const struct objfile_data *dwarf2_objfile_data_key;
99
dce234bc
PP
100struct dwarf2_section_info
101{
102 asection *asection;
103 gdb_byte *buffer;
104 bfd_size_type size;
be391dca
TT
105 /* True if we have tried to read this section. */
106 int readin;
dce234bc
PP
107};
108
8b70b953
TT
109typedef struct dwarf2_section_info dwarf2_section_info_def;
110DEF_VEC_O (dwarf2_section_info_def);
111
9291a0cd
TT
112/* All offsets in the index are of this type. It must be
113 architecture-independent. */
114typedef uint32_t offset_type;
115
116DEF_VEC_I (offset_type);
117
156942c7
DE
118/* Ensure only legit values are used. */
119#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
120 do { \
121 gdb_assert ((unsigned int) (value) <= 1); \
122 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
123 } while (0)
124
125/* Ensure only legit values are used. */
126#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
127 do { \
128 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
129 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
130 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133/* Ensure we don't use more than the alloted nuber of bits for the CU. */
134#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
137 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
138 } while (0)
139
9291a0cd
TT
140/* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142struct mapped_index
143{
559a7a62
JK
144 /* Index data format version. */
145 int version;
146
9291a0cd
TT
147 /* The total length of the buffer. */
148 off_t total_size;
b11b1f88 149
9291a0cd
TT
150 /* A pointer to the address table data. */
151 const gdb_byte *address_table;
b11b1f88 152
9291a0cd
TT
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
b11b1f88 155
3876f04e
DE
156 /* The symbol table, implemented as a hash table. */
157 const offset_type *symbol_table;
b11b1f88 158
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
b11b1f88 161
9291a0cd
TT
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164};
165
95554aad
TT
166typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
167DEF_VEC_P (dwarf2_per_cu_ptr);
168
9cdd5dbd
DE
169/* Collection of data recorded per objfile.
170 This hangs off of dwarf2_objfile_data_key. */
171
6502dd73
DJ
172struct dwarf2_per_objfile
173{
dce234bc
PP
174 struct dwarf2_section_info info;
175 struct dwarf2_section_info abbrev;
176 struct dwarf2_section_info line;
dce234bc
PP
177 struct dwarf2_section_info loc;
178 struct dwarf2_section_info macinfo;
cf2c3c16 179 struct dwarf2_section_info macro;
dce234bc
PP
180 struct dwarf2_section_info str;
181 struct dwarf2_section_info ranges;
3019eac3 182 struct dwarf2_section_info addr;
dce234bc
PP
183 struct dwarf2_section_info frame;
184 struct dwarf2_section_info eh_frame;
9291a0cd 185 struct dwarf2_section_info gdb_index;
ae038cb0 186
8b70b953
TT
187 VEC (dwarf2_section_info_def) *types;
188
be391dca
TT
189 /* Back link. */
190 struct objfile *objfile;
191
d467dd73 192 /* Table of all the compilation units. This is used to locate
10b3939b 193 the target compilation unit of a particular reference. */
ae038cb0
DJ
194 struct dwarf2_per_cu_data **all_comp_units;
195
196 /* The number of compilation units in ALL_COMP_UNITS. */
197 int n_comp_units;
198
1fd400ff 199 /* The number of .debug_types-related CUs. */
d467dd73 200 int n_type_units;
1fd400ff 201
d467dd73 202 /* The .debug_types-related CUs (TUs). */
b4dd5633 203 struct signatured_type **all_type_units;
1fd400ff 204
f4dc4d17
DE
205 /* The number of entries in all_type_unit_groups. */
206 int n_type_unit_groups;
207
208 /* Table of type unit groups.
209 This exists to make it easy to iterate over all CUs and TU groups. */
210 struct type_unit_group **all_type_unit_groups;
211
212 /* Table of struct type_unit_group objects.
213 The hash key is the DW_AT_stmt_list value. */
214 htab_t type_unit_groups;
72dca2f5 215
348e048f
DE
216 /* A table mapping .debug_types signatures to its signatured_type entry.
217 This is NULL if the .debug_types section hasn't been read in yet. */
218 htab_t signatured_types;
219
f4dc4d17
DE
220 /* Type unit statistics, to see how well the scaling improvements
221 are doing. */
222 struct tu_stats
223 {
224 int nr_uniq_abbrev_tables;
225 int nr_symtabs;
226 int nr_symtab_sharers;
227 int nr_stmt_less_type_units;
228 } tu_stats;
229
230 /* A chain of compilation units that are currently read in, so that
231 they can be freed later. */
232 struct dwarf2_per_cu_data *read_in_chain;
233
3019eac3
DE
234 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
235 This is NULL if the table hasn't been allocated yet. */
236 htab_t dwo_files;
237
80626a55
DE
238 /* Non-zero if we've check for whether there is a DWP file. */
239 int dwp_checked;
240
241 /* The DWP file if there is one, or NULL. */
242 struct dwp_file *dwp_file;
243
36586728
TT
244 /* The shared '.dwz' file, if one exists. This is used when the
245 original data was compressed using 'dwz -m'. */
246 struct dwz_file *dwz_file;
247
72dca2f5
FR
248 /* A flag indicating wether this objfile has a section loaded at a
249 VMA of 0. */
250 int has_section_at_zero;
9291a0cd 251
ae2de4f8
DE
252 /* True if we are using the mapped index,
253 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
254 unsigned char using_index;
255
ae2de4f8 256 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 257 struct mapped_index *index_table;
98bfdba5 258
7b9f3c50 259 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
260 TUs typically share line table entries with a CU, so we maintain a
261 separate table of all line table entries to support the sharing.
262 Note that while there can be way more TUs than CUs, we've already
263 sorted all the TUs into "type unit groups", grouped by their
264 DW_AT_stmt_list value. Therefore the only sharing done here is with a
265 CU and its associated TU group if there is one. */
7b9f3c50
DE
266 htab_t quick_file_names_table;
267
98bfdba5
PA
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
673bfd45 271
dee91e82 272 /* Table mapping type DIEs to their struct type *.
673bfd45 273 This is NULL if not allocated yet.
dee91e82
DE
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
95554aad
TT
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
279};
280
281static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 282
251d32d9 283/* Default names of the debugging sections. */
c906108c 284
233a11ab
CS
285/* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
9cdd5dbd
DE
288static const struct dwarf2_debug_sections dwarf2_elf_names =
289{
251d32d9
TG
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 295 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
3019eac3 299 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
24d3216f
TT
302 { ".gdb_index", ".zgdb_index" },
303 23
251d32d9 304};
c906108c 305
80626a55 306/* List of DWO/DWP sections. */
3019eac3 307
80626a55 308static const struct dwop_section_names
3019eac3
DE
309{
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
09262596
DE
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
3019eac3
DE
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
80626a55
DE
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
3019eac3 321}
80626a55 322dwop_section_names =
3019eac3
DE
323{
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
335};
336
c906108c
SS
337/* local data types */
338
107d2387
AC
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
c906108c 341struct comp_unit_head
a738430d 342{
c764a876 343 unsigned int length;
a738430d 344 short version;
a738430d
MK
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
b64f50a1 347 sect_offset abbrev_offset;
57349743 348
a738430d
MK
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
57349743 351
a738430d
MK
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
57349743 354
a738430d
MK
355 /* Offset to the first byte of this compilation unit header in the
356 .debug_info section, for resolving relative reference dies. */
b64f50a1 357 sect_offset offset;
57349743 358
d00adf39
DE
359 /* Offset to first die in this cu from the start of the cu.
360 This will be the first byte following the compilation unit header. */
b64f50a1 361 cu_offset first_die_offset;
a738430d 362};
c906108c 363
3da10d80
KS
364/* Type used for delaying computation of method physnames.
365 See comments for compute_delayed_physnames. */
366struct delayed_method_info
367{
368 /* The type to which the method is attached, i.e., its parent class. */
369 struct type *type;
370
371 /* The index of the method in the type's function fieldlists. */
372 int fnfield_index;
373
374 /* The index of the method in the fieldlist. */
375 int index;
376
377 /* The name of the DIE. */
378 const char *name;
379
380 /* The DIE associated with this method. */
381 struct die_info *die;
382};
383
384typedef struct delayed_method_info delayed_method_info;
385DEF_VEC_O (delayed_method_info);
386
e7c27a73
DJ
387/* Internal state when decoding a particular compilation unit. */
388struct dwarf2_cu
389{
390 /* The objfile containing this compilation unit. */
391 struct objfile *objfile;
392
d00adf39 393 /* The header of the compilation unit. */
e7c27a73 394 struct comp_unit_head header;
e142c38c 395
d00adf39
DE
396 /* Base address of this compilation unit. */
397 CORE_ADDR base_address;
398
399 /* Non-zero if base_address has been set. */
400 int base_known;
401
e142c38c
DJ
402 /* The language we are debugging. */
403 enum language language;
404 const struct language_defn *language_defn;
405
b0f35d58
DL
406 const char *producer;
407
e142c38c
DJ
408 /* The generic symbol table building routines have separate lists for
409 file scope symbols and all all other scopes (local scopes). So
410 we need to select the right one to pass to add_symbol_to_list().
411 We do it by keeping a pointer to the correct list in list_in_scope.
412
413 FIXME: The original dwarf code just treated the file scope as the
414 first local scope, and all other local scopes as nested local
415 scopes, and worked fine. Check to see if we really need to
416 distinguish these in buildsym.c. */
417 struct pending **list_in_scope;
418
433df2d4
DE
419 /* The abbrev table for this CU.
420 Normally this points to the abbrev table in the objfile.
421 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
422 struct abbrev_table *abbrev_table;
72bf9492 423
b64f50a1
JK
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
426 htab_t partial_dies;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 struct obstack comp_unit_obstack;
431
ae038cb0
DJ
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain;
437
438 /* Backchain to our per_cu entry if the tree has been built. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used;
443
b64f50a1
JK
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
51545339 446 htab_t die_hash;
10b3939b
DJ
447
448 /* Full DIEs if read in. */
449 struct die_info *dies;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies;
455
cb1df416
DJ
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header;
458
3da10d80
KS
459 /* A list of methods which need to have physnames computed
460 after all type information has been read. */
461 VEC (delayed_method_info) *method_list;
462
96408a79
SA
463 /* To be copied to symtab->call_site_htab. */
464 htab_t call_site_htab;
465
034e5797
DE
466 /* Non-NULL if this CU came from a DWO file.
467 There is an invariant here that is important to remember:
468 Except for attributes copied from the top level DIE in the "main"
469 (or "stub") file in preparation for reading the DWO file
470 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
471 Either there isn't a DWO file (in which case this is NULL and the point
472 is moot), or there is and either we're not going to read it (in which
473 case this is NULL) or there is and we are reading it (in which case this
474 is non-NULL). */
3019eac3
DE
475 struct dwo_unit *dwo_unit;
476
477 /* The DW_AT_addr_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE. */
480 ULONGEST addr_base;
481
2e3cf129
DE
482 /* The DW_AT_ranges_base attribute if present, zero otherwise
483 (zero is a valid value though).
484 Note this value comes from the stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
486 be used without needing to know whether DWO files are in use or not.
487 N.B. This does not apply to DW_AT_ranges appearing in
488 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
489 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
490 DW_AT_ranges_base *would* have to be applied, and we'd have to care
491 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
492 ULONGEST ranges_base;
493
ae038cb0
DJ
494 /* Mark used when releasing cached dies. */
495 unsigned int mark : 1;
496
8be455d7
JK
497 /* This CU references .debug_loc. See the symtab->locations_valid field.
498 This test is imperfect as there may exist optimized debug code not using
499 any location list and still facing inlining issues if handled as
500 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 501 unsigned int has_loclist : 1;
ba919b58 502
1b80a9fa
JK
503 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
504 if all the producer_is_* fields are valid. This information is cached
505 because profiling CU expansion showed excessive time spent in
506 producer_is_gxx_lt_4_6. */
ba919b58
TT
507 unsigned int checked_producer : 1;
508 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 509 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 510 unsigned int producer_is_icc : 1;
e7c27a73
DJ
511};
512
10b3939b
DJ
513/* Persistent data held for a compilation unit, even when not
514 processing it. We put a pointer to this structure in the
28dee7f5 515 read_symtab_private field of the psymtab. */
10b3939b 516
ae038cb0
DJ
517struct dwarf2_per_cu_data
518{
36586728 519 /* The start offset and length of this compilation unit.
45452591 520 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
521 initial_length_size.
522 If the DIE refers to a DWO file, this is always of the original die,
523 not the DWO file. */
b64f50a1 524 sect_offset offset;
36586728 525 unsigned int length;
ae038cb0
DJ
526
527 /* Flag indicating this compilation unit will be read in before
528 any of the current compilation units are processed. */
c764a876 529 unsigned int queued : 1;
ae038cb0 530
0d99eb77
DE
531 /* This flag will be set when reading partial DIEs if we need to load
532 absolutely all DIEs for this compilation unit, instead of just the ones
533 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
534 hash table and don't find it. */
535 unsigned int load_all_dies : 1;
536
3019eac3
DE
537 /* Non-zero if this CU is from .debug_types. */
538 unsigned int is_debug_types : 1;
539
36586728
TT
540 /* Non-zero if this CU is from the .dwz file. */
541 unsigned int is_dwz : 1;
542
3019eac3
DE
543 /* The section this CU/TU lives in.
544 If the DIE refers to a DWO file, this is always the original die,
545 not the DWO file. */
546 struct dwarf2_section_info *info_or_types_section;
348e048f 547
17ea53c3
JK
548 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
549 of the CU cache it gets reset to NULL again. */
ae038cb0 550 struct dwarf2_cu *cu;
1c379e20 551
9cdd5dbd
DE
552 /* The corresponding objfile.
553 Normally we can get the objfile from dwarf2_per_objfile.
554 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
555 struct objfile *objfile;
556
557 /* When using partial symbol tables, the 'psymtab' field is active.
558 Otherwise the 'quick' field is active. */
559 union
560 {
561 /* The partial symbol table associated with this compilation unit,
95554aad 562 or NULL for unread partial units. */
9291a0cd
TT
563 struct partial_symtab *psymtab;
564
565 /* Data needed by the "quick" functions. */
566 struct dwarf2_per_cu_quick_data *quick;
567 } v;
95554aad 568
f4dc4d17
DE
569 union
570 {
571 /* The CUs we import using DW_TAG_imported_unit. This is filled in
572 while reading psymtabs, used to compute the psymtab dependencies,
573 and then cleared. Then it is filled in again while reading full
574 symbols, and only deleted when the objfile is destroyed. */
575 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
576
577 /* Type units are grouped by their DW_AT_stmt_list entry so that they
578 can share them. If this is a TU, this points to the containing
579 symtab. */
580 struct type_unit_group *type_unit_group;
581 } s;
ae038cb0
DJ
582};
583
348e048f
DE
584/* Entry in the signatured_types hash table. */
585
586struct signatured_type
587{
42e7ad6c
DE
588 /* The "per_cu" object of this type.
589 N.B.: This is the first member so that it's easy to convert pointers
590 between them. */
591 struct dwarf2_per_cu_data per_cu;
592
3019eac3 593 /* The type's signature. */
348e048f
DE
594 ULONGEST signature;
595
3019eac3
DE
596 /* Offset in the TU of the type's DIE, as read from the TU header.
597 If the definition lives in a DWO file, this value is unusable. */
598 cu_offset type_offset_in_tu;
599
600 /* Offset in the section of the type's DIE.
601 If the definition lives in a DWO file, this is the offset in the
602 .debug_types.dwo section.
603 The value is zero until the actual value is known.
604 Zero is otherwise not a valid section offset. */
605 sect_offset type_offset_in_section;
348e048f
DE
606};
607
094b34ac
DE
608/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
609 This includes type_unit_group and quick_file_names. */
610
611struct stmt_list_hash
612{
613 /* The DWO unit this table is from or NULL if there is none. */
614 struct dwo_unit *dwo_unit;
615
616 /* Offset in .debug_line or .debug_line.dwo. */
617 sect_offset line_offset;
618};
619
f4dc4d17
DE
620/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
621 an object of this type. */
622
623struct type_unit_group
624{
625 /* dwarf2read.c's main "handle" on the symtab.
626 To simplify things we create an artificial CU that "includes" all the
627 type units using this stmt_list so that the rest of the code still has
628 a "per_cu" handle on the symtab.
629 This PER_CU is recognized by having no section. */
630#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
631 struct dwarf2_per_cu_data per_cu;
632
633 union
634 {
635 /* The TUs that share this DW_AT_stmt_list entry.
636 This is added to while parsing type units to build partial symtabs,
637 and is deleted afterwards and not used again. */
638 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 639
094b34ac
DE
640 /* When reading the line table in "quick" functions, we need a real TU.
641 Any will do, we know they all share the same DW_AT_stmt_list entry.
642 For simplicity's sake, we pick the first one. */
643 struct dwarf2_per_cu_data *first_tu;
644 } t;
f4dc4d17
DE
645
646 /* The primary symtab.
094b34ac
DE
647 Type units in a group needn't all be defined in the same source file,
648 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
649 struct symtab *primary_symtab;
650
094b34ac
DE
651 /* The data used to construct the hash key. */
652 struct stmt_list_hash hash;
f4dc4d17
DE
653
654 /* The number of symtabs from the line header.
655 The value here must match line_header.num_file_names. */
656 unsigned int num_symtabs;
657
658 /* The symbol tables for this TU (obtained from the files listed in
659 DW_AT_stmt_list).
660 WARNING: The order of entries here must match the order of entries
661 in the line header. After the first TU using this type_unit_group, the
662 line header for the subsequent TUs is recreated from this. This is done
663 because we need to use the same symtabs for each TU using the same
664 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
665 there's no guarantee the line header doesn't have duplicate entries. */
666 struct symtab **symtabs;
667};
668
80626a55 669/* These sections are what may appear in a DWO file. */
3019eac3
DE
670
671struct dwo_sections
672{
673 struct dwarf2_section_info abbrev;
3019eac3
DE
674 struct dwarf2_section_info line;
675 struct dwarf2_section_info loc;
09262596
DE
676 struct dwarf2_section_info macinfo;
677 struct dwarf2_section_info macro;
3019eac3
DE
678 struct dwarf2_section_info str;
679 struct dwarf2_section_info str_offsets;
80626a55
DE
680 /* In the case of a virtual DWO file, these two are unused. */
681 struct dwarf2_section_info info;
3019eac3
DE
682 VEC (dwarf2_section_info_def) *types;
683};
684
685/* Common bits of DWO CUs/TUs. */
686
687struct dwo_unit
688{
689 /* Backlink to the containing struct dwo_file. */
690 struct dwo_file *dwo_file;
691
692 /* The "id" that distinguishes this CU/TU.
693 .debug_info calls this "dwo_id", .debug_types calls this "signature".
694 Since signatures came first, we stick with it for consistency. */
695 ULONGEST signature;
696
697 /* The section this CU/TU lives in, in the DWO file. */
698 struct dwarf2_section_info *info_or_types_section;
699
700 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
701 sect_offset offset;
702 unsigned int length;
703
704 /* For types, offset in the type's DIE of the type defined by this TU. */
705 cu_offset type_offset_in_tu;
706};
707
80626a55
DE
708/* Data for one DWO file.
709 This includes virtual DWO files that have been packaged into a
710 DWP file. */
3019eac3
DE
711
712struct dwo_file
713{
80626a55
DE
714 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
715 For virtual DWO files the name is constructed from the section offsets
716 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
717 from related CU+TUs. */
718 const char *name;
3019eac3 719
80626a55
DE
720 /* The bfd, when the file is open. Otherwise this is NULL.
721 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
722 bfd *dbfd;
3019eac3
DE
723
724 /* Section info for this file. */
725 struct dwo_sections sections;
726
727 /* Table of CUs in the file.
728 Each element is a struct dwo_unit. */
729 htab_t cus;
730
731 /* Table of TUs in the file.
732 Each element is a struct dwo_unit. */
733 htab_t tus;
734};
735
80626a55
DE
736/* These sections are what may appear in a DWP file. */
737
738struct dwp_sections
739{
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
744 by section number. We don't need to record them here. */
745};
746
747/* These sections are what may appear in a virtual DWO file. */
748
749struct virtual_dwo_sections
750{
751 struct dwarf2_section_info abbrev;
752 struct dwarf2_section_info line;
753 struct dwarf2_section_info loc;
754 struct dwarf2_section_info macinfo;
755 struct dwarf2_section_info macro;
756 struct dwarf2_section_info str_offsets;
757 /* Each DWP hash table entry records one CU or one TU.
758 That is recorded here, and copied to dwo_unit.info_or_types_section. */
759 struct dwarf2_section_info info_or_types;
760};
761
762/* Contents of DWP hash tables. */
763
764struct dwp_hash_table
765{
766 uint32_t nr_units, nr_slots;
767 const gdb_byte *hash_table, *unit_table, *section_pool;
768};
769
770/* Data for one DWP file. */
771
772struct dwp_file
773{
774 /* Name of the file. */
775 const char *name;
776
777 /* The bfd, when the file is open. Otherwise this is NULL. */
778 bfd *dbfd;
779
780 /* Section info for this file. */
781 struct dwp_sections sections;
782
783 /* Table of CUs in the file. */
784 const struct dwp_hash_table *cus;
785
786 /* Table of TUs in the file. */
787 const struct dwp_hash_table *tus;
788
789 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
790 htab_t loaded_cutus;
791
792 /* Table to map ELF section numbers to their sections. */
793 unsigned int num_sections;
794 asection **elf_sections;
795};
796
36586728
TT
797/* This represents a '.dwz' file. */
798
799struct dwz_file
800{
801 /* A dwz file can only contain a few sections. */
802 struct dwarf2_section_info abbrev;
803 struct dwarf2_section_info info;
804 struct dwarf2_section_info str;
805 struct dwarf2_section_info line;
806 struct dwarf2_section_info macro;
2ec9a5e0 807 struct dwarf2_section_info gdb_index;
36586728
TT
808
809 /* The dwz's BFD. */
810 bfd *dwz_bfd;
811};
812
0963b4bd
MS
813/* Struct used to pass misc. parameters to read_die_and_children, et
814 al. which are used for both .debug_info and .debug_types dies.
815 All parameters here are unchanging for the life of the call. This
dee91e82 816 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
817
818struct die_reader_specs
819{
dee91e82 820 /* die_section->asection->owner. */
93311388
DE
821 bfd* abfd;
822
823 /* The CU of the DIE we are parsing. */
824 struct dwarf2_cu *cu;
825
80626a55 826 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
827 struct dwo_file *dwo_file;
828
dee91e82 829 /* The section the die comes from.
3019eac3 830 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
831 struct dwarf2_section_info *die_section;
832
833 /* die_section->buffer. */
834 gdb_byte *buffer;
f664829e
DE
835
836 /* The end of the buffer. */
837 const gdb_byte *buffer_end;
93311388
DE
838};
839
fd820528 840/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
841typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
842 gdb_byte *info_ptr,
843 struct die_info *comp_unit_die,
844 int has_children,
845 void *data);
846
debd256d
JB
847/* The line number information for a compilation unit (found in the
848 .debug_line section) begins with a "statement program header",
849 which contains the following information. */
850struct line_header
851{
852 unsigned int total_length;
853 unsigned short version;
854 unsigned int header_length;
855 unsigned char minimum_instruction_length;
2dc7f7b3 856 unsigned char maximum_ops_per_instruction;
debd256d
JB
857 unsigned char default_is_stmt;
858 int line_base;
859 unsigned char line_range;
860 unsigned char opcode_base;
861
862 /* standard_opcode_lengths[i] is the number of operands for the
863 standard opcode whose value is i. This means that
864 standard_opcode_lengths[0] is unused, and the last meaningful
865 element is standard_opcode_lengths[opcode_base - 1]. */
866 unsigned char *standard_opcode_lengths;
867
868 /* The include_directories table. NOTE! These strings are not
869 allocated with xmalloc; instead, they are pointers into
870 debug_line_buffer. If you try to free them, `free' will get
871 indigestion. */
872 unsigned int num_include_dirs, include_dirs_size;
873 char **include_dirs;
874
875 /* The file_names table. NOTE! These strings are not allocated
876 with xmalloc; instead, they are pointers into debug_line_buffer.
877 Don't try to free them directly. */
878 unsigned int num_file_names, file_names_size;
879 struct file_entry
c906108c 880 {
debd256d
JB
881 char *name;
882 unsigned int dir_index;
883 unsigned int mod_time;
884 unsigned int length;
aaa75496 885 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 886 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
887 } *file_names;
888
889 /* The start and end of the statement program following this
6502dd73 890 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 891 gdb_byte *statement_program_start, *statement_program_end;
debd256d 892};
c906108c
SS
893
894/* When we construct a partial symbol table entry we only
0963b4bd 895 need this much information. */
c906108c
SS
896struct partial_die_info
897 {
72bf9492 898 /* Offset of this DIE. */
b64f50a1 899 sect_offset offset;
72bf9492
DJ
900
901 /* DWARF-2 tag for this DIE. */
902 ENUM_BITFIELD(dwarf_tag) tag : 16;
903
72bf9492
DJ
904 /* Assorted flags describing the data found in this DIE. */
905 unsigned int has_children : 1;
906 unsigned int is_external : 1;
907 unsigned int is_declaration : 1;
908 unsigned int has_type : 1;
909 unsigned int has_specification : 1;
910 unsigned int has_pc_info : 1;
481860b3 911 unsigned int may_be_inlined : 1;
72bf9492
DJ
912
913 /* Flag set if the SCOPE field of this structure has been
914 computed. */
915 unsigned int scope_set : 1;
916
fa4028e9
JB
917 /* Flag set if the DIE has a byte_size attribute. */
918 unsigned int has_byte_size : 1;
919
98bfdba5
PA
920 /* Flag set if any of the DIE's children are template arguments. */
921 unsigned int has_template_arguments : 1;
922
abc72ce4
DE
923 /* Flag set if fixup_partial_die has been called on this die. */
924 unsigned int fixup_called : 1;
925
36586728
TT
926 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
927 unsigned int is_dwz : 1;
928
929 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
930 unsigned int spec_is_dwz : 1;
931
72bf9492 932 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 933 sometimes a default name for unnamed DIEs. */
c906108c 934 char *name;
72bf9492 935
abc72ce4
DE
936 /* The linkage name, if present. */
937 const char *linkage_name;
938
72bf9492
DJ
939 /* The scope to prepend to our children. This is generally
940 allocated on the comp_unit_obstack, so will disappear
941 when this compilation unit leaves the cache. */
942 char *scope;
943
95554aad
TT
944 /* Some data associated with the partial DIE. The tag determines
945 which field is live. */
946 union
947 {
948 /* The location description associated with this DIE, if any. */
949 struct dwarf_block *locdesc;
950 /* The offset of an import, for DW_TAG_imported_unit. */
951 sect_offset offset;
952 } d;
72bf9492
DJ
953
954 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
955 CORE_ADDR lowpc;
956 CORE_ADDR highpc;
72bf9492 957
93311388 958 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 959 DW_AT_sibling, if any. */
abc72ce4
DE
960 /* NOTE: This member isn't strictly necessary, read_partial_die could
961 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 962 gdb_byte *sibling;
72bf9492
DJ
963
964 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
965 DW_AT_specification (or DW_AT_abstract_origin or
966 DW_AT_extension). */
b64f50a1 967 sect_offset spec_offset;
72bf9492
DJ
968
969 /* Pointers to this DIE's parent, first child, and next sibling,
970 if any. */
971 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
972 };
973
0963b4bd 974/* This data structure holds the information of an abbrev. */
c906108c
SS
975struct abbrev_info
976 {
977 unsigned int number; /* number identifying abbrev */
978 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
979 unsigned short has_children; /* boolean */
980 unsigned short num_attrs; /* number of attributes */
c906108c
SS
981 struct attr_abbrev *attrs; /* an array of attribute descriptions */
982 struct abbrev_info *next; /* next in chain */
983 };
984
985struct attr_abbrev
986 {
9d25dd43
DE
987 ENUM_BITFIELD(dwarf_attribute) name : 16;
988 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
989 };
990
433df2d4
DE
991/* Size of abbrev_table.abbrev_hash_table. */
992#define ABBREV_HASH_SIZE 121
993
994/* Top level data structure to contain an abbreviation table. */
995
996struct abbrev_table
997{
f4dc4d17
DE
998 /* Where the abbrev table came from.
999 This is used as a sanity check when the table is used. */
433df2d4
DE
1000 sect_offset offset;
1001
1002 /* Storage for the abbrev table. */
1003 struct obstack abbrev_obstack;
1004
1005 /* Hash table of abbrevs.
1006 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1007 It could be statically allocated, but the previous code didn't so we
1008 don't either. */
1009 struct abbrev_info **abbrevs;
1010};
1011
0963b4bd 1012/* Attributes have a name and a value. */
b60c80d6
DJ
1013struct attribute
1014 {
9d25dd43 1015 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1016 ENUM_BITFIELD(dwarf_form) form : 15;
1017
1018 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1019 field should be in u.str (existing only for DW_STRING) but it is kept
1020 here for better struct attribute alignment. */
1021 unsigned int string_is_canonical : 1;
1022
b60c80d6
DJ
1023 union
1024 {
1025 char *str;
1026 struct dwarf_block *blk;
43bbcdc2
PH
1027 ULONGEST unsnd;
1028 LONGEST snd;
b60c80d6 1029 CORE_ADDR addr;
348e048f 1030 struct signatured_type *signatured_type;
b60c80d6
DJ
1031 }
1032 u;
1033 };
1034
0963b4bd 1035/* This data structure holds a complete die structure. */
c906108c
SS
1036struct die_info
1037 {
76815b17
DE
1038 /* DWARF-2 tag for this DIE. */
1039 ENUM_BITFIELD(dwarf_tag) tag : 16;
1040
1041 /* Number of attributes */
98bfdba5
PA
1042 unsigned char num_attrs;
1043
1044 /* True if we're presently building the full type name for the
1045 type derived from this DIE. */
1046 unsigned char building_fullname : 1;
76815b17
DE
1047
1048 /* Abbrev number */
1049 unsigned int abbrev;
1050
93311388 1051 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1052 sect_offset offset;
78ba4af6
JB
1053
1054 /* The dies in a compilation unit form an n-ary tree. PARENT
1055 points to this die's parent; CHILD points to the first child of
1056 this node; and all the children of a given node are chained
4950bc1c 1057 together via their SIBLING fields. */
639d11d3
DC
1058 struct die_info *child; /* Its first child, if any. */
1059 struct die_info *sibling; /* Its next sibling, if any. */
1060 struct die_info *parent; /* Its parent, if any. */
c906108c 1061
b60c80d6
DJ
1062 /* An array of attributes, with NUM_ATTRS elements. There may be
1063 zero, but it's not common and zero-sized arrays are not
1064 sufficiently portable C. */
1065 struct attribute attrs[1];
c906108c
SS
1066 };
1067
0963b4bd 1068/* Get at parts of an attribute structure. */
c906108c
SS
1069
1070#define DW_STRING(attr) ((attr)->u.str)
8285870a 1071#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1072#define DW_UNSND(attr) ((attr)->u.unsnd)
1073#define DW_BLOCK(attr) ((attr)->u.blk)
1074#define DW_SND(attr) ((attr)->u.snd)
1075#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1076#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1077
0963b4bd 1078/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1079struct dwarf_block
1080 {
56eb65bd 1081 size_t size;
1d6edc3c
JK
1082
1083 /* Valid only if SIZE is not zero. */
fe1b8b76 1084 gdb_byte *data;
c906108c
SS
1085 };
1086
c906108c
SS
1087#ifndef ATTR_ALLOC_CHUNK
1088#define ATTR_ALLOC_CHUNK 4
1089#endif
1090
c906108c
SS
1091/* Allocate fields for structs, unions and enums in this size. */
1092#ifndef DW_FIELD_ALLOC_CHUNK
1093#define DW_FIELD_ALLOC_CHUNK 4
1094#endif
1095
c906108c
SS
1096/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099static int bits_per_byte = 8;
1100
1101/* The routines that read and process dies for a C struct or C++ class
1102 pass lists of data member fields and lists of member function fields
1103 in an instance of a field_info structure, as defined below. */
1104struct field_info
c5aa993b 1105 {
0963b4bd 1106 /* List of data member and baseclasses fields. */
c5aa993b
JM
1107 struct nextfield
1108 {
1109 struct nextfield *next;
1110 int accessibility;
1111 int virtuality;
1112 struct field field;
1113 }
7d0ccb61 1114 *fields, *baseclasses;
c906108c 1115
7d0ccb61 1116 /* Number of fields (including baseclasses). */
c5aa993b 1117 int nfields;
c906108c 1118
c5aa993b
JM
1119 /* Number of baseclasses. */
1120 int nbaseclasses;
c906108c 1121
c5aa993b
JM
1122 /* Set if the accesibility of one of the fields is not public. */
1123 int non_public_fields;
c906108c 1124
c5aa993b
JM
1125 /* Member function fields array, entries are allocated in the order they
1126 are encountered in the object file. */
1127 struct nextfnfield
1128 {
1129 struct nextfnfield *next;
1130 struct fn_field fnfield;
1131 }
1132 *fnfields;
c906108c 1133
c5aa993b
JM
1134 /* Member function fieldlist array, contains name of possibly overloaded
1135 member function, number of overloaded member functions and a pointer
1136 to the head of the member function field chain. */
1137 struct fnfieldlist
1138 {
1139 char *name;
1140 int length;
1141 struct nextfnfield *head;
1142 }
1143 *fnfieldlists;
c906108c 1144
c5aa993b
JM
1145 /* Number of entries in the fnfieldlists array. */
1146 int nfnfields;
98751a41
JK
1147
1148 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1149 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1150 struct typedef_field_list
1151 {
1152 struct typedef_field field;
1153 struct typedef_field_list *next;
1154 }
1155 *typedef_field_list;
1156 unsigned typedef_field_list_count;
c5aa993b 1157 };
c906108c 1158
10b3939b
DJ
1159/* One item on the queue of compilation units to read in full symbols
1160 for. */
1161struct dwarf2_queue_item
1162{
1163 struct dwarf2_per_cu_data *per_cu;
95554aad 1164 enum language pretend_language;
10b3939b
DJ
1165 struct dwarf2_queue_item *next;
1166};
1167
1168/* The current queue. */
1169static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1170
ae038cb0
DJ
1171/* Loaded secondary compilation units are kept in memory until they
1172 have not been referenced for the processing of this many
1173 compilation units. Set this to zero to disable caching. Cache
1174 sizes of up to at least twenty will improve startup time for
1175 typical inter-CU-reference binaries, at an obvious memory cost. */
1176static int dwarf2_max_cache_age = 5;
920d2a44
AC
1177static void
1178show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1179 struct cmd_list_element *c, const char *value)
1180{
3e43a32a
MS
1181 fprintf_filtered (file, _("The upper bound on the age of cached "
1182 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1183 value);
1184}
1185
ae038cb0 1186
0963b4bd 1187/* Various complaints about symbol reading that don't abort the process. */
c906108c 1188
4d3c2250
KB
1189static void
1190dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1191{
4d3c2250 1192 complaint (&symfile_complaints,
e2e0b3e5 1193 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1194}
1195
25e43795
DJ
1196static void
1197dwarf2_debug_line_missing_file_complaint (void)
1198{
1199 complaint (&symfile_complaints,
1200 _(".debug_line section has line data without a file"));
1201}
1202
59205f5a
JB
1203static void
1204dwarf2_debug_line_missing_end_sequence_complaint (void)
1205{
1206 complaint (&symfile_complaints,
3e43a32a
MS
1207 _(".debug_line section has line "
1208 "program sequence without an end"));
59205f5a
JB
1209}
1210
4d3c2250
KB
1211static void
1212dwarf2_complex_location_expr_complaint (void)
2e276125 1213{
e2e0b3e5 1214 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1215}
1216
4d3c2250
KB
1217static void
1218dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1219 int arg3)
2e276125 1220{
4d3c2250 1221 complaint (&symfile_complaints,
3e43a32a
MS
1222 _("const value length mismatch for '%s', got %d, expected %d"),
1223 arg1, arg2, arg3);
4d3c2250
KB
1224}
1225
1226static void
f664829e 1227dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1228{
4d3c2250 1229 complaint (&symfile_complaints,
f664829e
DE
1230 _("debug info runs off end of %s section"
1231 " [in module %s]"),
1232 section->asection->name,
1233 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1234}
1235
1236static void
1237dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1238{
4d3c2250 1239 complaint (&symfile_complaints,
3e43a32a
MS
1240 _("macro debug info contains a "
1241 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1242 arg1);
1243}
1244
1245static void
1246dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1247{
4d3c2250 1248 complaint (&symfile_complaints,
3e43a32a
MS
1249 _("invalid attribute class or form for '%s' in '%s'"),
1250 arg1, arg2);
4d3c2250 1251}
c906108c 1252
c906108c
SS
1253/* local function prototypes */
1254
4efb68b1 1255static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1256
aaa75496
JB
1257static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1258 struct objfile *);
1259
918dd910
JK
1260static void dwarf2_find_base_address (struct die_info *die,
1261 struct dwarf2_cu *cu);
1262
c67a9c90 1263static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1264
72bf9492
DJ
1265static void scan_partial_symbols (struct partial_die_info *,
1266 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1267 int, struct dwarf2_cu *);
c906108c 1268
72bf9492
DJ
1269static void add_partial_symbol (struct partial_die_info *,
1270 struct dwarf2_cu *);
63d06c5c 1271
72bf9492
DJ
1272static void add_partial_namespace (struct partial_die_info *pdi,
1273 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1274 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1275
5d7cb8df
JK
1276static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1277 CORE_ADDR *highpc, int need_pc,
1278 struct dwarf2_cu *cu);
1279
72bf9492
DJ
1280static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1281 struct dwarf2_cu *cu);
91c24f0a 1282
bc30ff58
JB
1283static void add_partial_subprogram (struct partial_die_info *pdi,
1284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1285 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1286
5c80ed9d
TT
1287static void dwarf2_psymtab_to_symtab (struct objfile *,
1288 struct partial_symtab *);
c906108c 1289
a14ed312 1290static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1291
433df2d4
DE
1292static struct abbrev_info *abbrev_table_lookup_abbrev
1293 (const struct abbrev_table *, unsigned int);
1294
1295static struct abbrev_table *abbrev_table_read_table
1296 (struct dwarf2_section_info *, sect_offset);
1297
1298static void abbrev_table_free (struct abbrev_table *);
1299
f4dc4d17
DE
1300static void abbrev_table_free_cleanup (void *);
1301
dee91e82
DE
1302static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1303 struct dwarf2_section_info *);
c906108c 1304
f3dd6933 1305static void dwarf2_free_abbrev_table (void *);
c906108c 1306
6caca83c
CC
1307static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1308
dee91e82
DE
1309static struct partial_die_info *load_partial_dies
1310 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1311
dee91e82
DE
1312static gdb_byte *read_partial_die (const struct die_reader_specs *,
1313 struct partial_die_info *,
1314 struct abbrev_info *,
1315 unsigned int,
1316 gdb_byte *);
c906108c 1317
36586728 1318static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1319 struct dwarf2_cu *);
72bf9492
DJ
1320
1321static void fixup_partial_die (struct partial_die_info *,
1322 struct dwarf2_cu *);
1323
dee91e82
DE
1324static gdb_byte *read_attribute (const struct die_reader_specs *,
1325 struct attribute *, struct attr_abbrev *,
1326 gdb_byte *);
a8329558 1327
a1855c1d 1328static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1329
a1855c1d 1330static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1331
a1855c1d 1332static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1333
a1855c1d 1334static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1335
a1855c1d 1336static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1337
fe1b8b76 1338static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1339 unsigned int *);
c906108c 1340
c764a876
DE
1341static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1342
1343static LONGEST read_checked_initial_length_and_offset
1344 (bfd *, gdb_byte *, const struct comp_unit_head *,
1345 unsigned int *, unsigned int *);
613e1657 1346
fe1b8b76 1347static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1348 unsigned int *);
1349
1350static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1351
f4dc4d17
DE
1352static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1353 sect_offset);
1354
fe1b8b76 1355static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1356
9b1c24c8 1357static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1358
fe1b8b76
JB
1359static char *read_indirect_string (bfd *, gdb_byte *,
1360 const struct comp_unit_head *,
1361 unsigned int *);
4bdf3d34 1362
36586728
TT
1363static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1364
12df843f 1365static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1366
12df843f 1367static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1368
3019eac3
DE
1369static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1370 unsigned int *);
1371
1372static char *read_str_index (const struct die_reader_specs *reader,
1373 struct dwarf2_cu *cu, ULONGEST str_index);
1374
e142c38c 1375static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1376
e142c38c
DJ
1377static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1378 struct dwarf2_cu *);
c906108c 1379
348e048f 1380static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1381 unsigned int);
348e048f 1382
05cf31d1
JB
1383static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
e142c38c 1386static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1387
e142c38c 1388static struct die_info *die_specification (struct die_info *die,
f2f0e013 1389 struct dwarf2_cu **);
63d06c5c 1390
debd256d
JB
1391static void free_line_header (struct line_header *lh);
1392
aaa75496
JB
1393static void add_file_name (struct line_header *, char *, unsigned int,
1394 unsigned int, unsigned int);
1395
3019eac3
DE
1396static struct line_header *dwarf_decode_line_header (unsigned int offset,
1397 struct dwarf2_cu *cu);
debd256d 1398
f3f5162e
DE
1399static void dwarf_decode_lines (struct line_header *, const char *,
1400 struct dwarf2_cu *, struct partial_symtab *,
1401 int);
c906108c 1402
72b9f47f 1403static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1404
f4dc4d17
DE
1405static void dwarf2_start_symtab (struct dwarf2_cu *,
1406 char *, char *, CORE_ADDR);
1407
a14ed312 1408static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1409 struct dwarf2_cu *);
c906108c 1410
34eaf542
TT
1411static struct symbol *new_symbol_full (struct die_info *, struct type *,
1412 struct dwarf2_cu *, struct symbol *);
1413
a14ed312 1414static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1415 struct dwarf2_cu *);
c906108c 1416
98bfdba5
PA
1417static void dwarf2_const_value_attr (struct attribute *attr,
1418 struct type *type,
1419 const char *name,
1420 struct obstack *obstack,
12df843f 1421 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1422 gdb_byte **bytes,
1423 struct dwarf2_locexpr_baton **baton);
2df3850c 1424
e7c27a73 1425static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1426
b4ba55a1
JB
1427static int need_gnat_info (struct dwarf2_cu *);
1428
3e43a32a
MS
1429static struct type *die_descriptive_type (struct die_info *,
1430 struct dwarf2_cu *);
b4ba55a1
JB
1431
1432static void set_descriptive_type (struct type *, struct die_info *,
1433 struct dwarf2_cu *);
1434
e7c27a73
DJ
1435static struct type *die_containing_type (struct die_info *,
1436 struct dwarf2_cu *);
c906108c 1437
673bfd45
DE
1438static struct type *lookup_die_type (struct die_info *, struct attribute *,
1439 struct dwarf2_cu *);
c906108c 1440
f792889a 1441static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1442
673bfd45
DE
1443static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1444
0d5cff50 1445static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1446
6e70227d 1447static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1448 const char *suffix, int physname,
1449 struct dwarf2_cu *cu);
63d06c5c 1450
e7c27a73 1451static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1452
348e048f
DE
1453static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1454
e7c27a73 1455static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1456
e7c27a73 1457static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1458
96408a79
SA
1459static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1460
ff013f42
JK
1461static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1462 struct dwarf2_cu *, struct partial_symtab *);
1463
a14ed312 1464static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1465 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1466 struct partial_symtab *);
c906108c 1467
fae299cd
DC
1468static void get_scope_pc_bounds (struct die_info *,
1469 CORE_ADDR *, CORE_ADDR *,
1470 struct dwarf2_cu *);
1471
801e3a5b
JB
1472static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1473 CORE_ADDR, struct dwarf2_cu *);
1474
a14ed312 1475static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1476 struct dwarf2_cu *);
c906108c 1477
a14ed312 1478static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1479 struct type *, struct dwarf2_cu *);
c906108c 1480
a14ed312 1481static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1482 struct die_info *, struct type *,
e7c27a73 1483 struct dwarf2_cu *);
c906108c 1484
a14ed312 1485static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1486 struct type *,
1487 struct dwarf2_cu *);
c906108c 1488
134d01f1 1489static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1490
e7c27a73 1491static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1492
e7c27a73 1493static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1494
5d7cb8df
JK
1495static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1496
27aa8d6a
SW
1497static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1498
f55ee35c
JK
1499static struct type *read_module_type (struct die_info *die,
1500 struct dwarf2_cu *cu);
1501
38d518c9 1502static const char *namespace_name (struct die_info *die,
e142c38c 1503 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1504
134d01f1 1505static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1506
e7c27a73 1507static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1508
6e70227d 1509static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1510 struct dwarf2_cu *);
1511
dee91e82 1512static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1513 gdb_byte *info_ptr,
fe1b8b76 1514 gdb_byte **new_info_ptr,
639d11d3
DC
1515 struct die_info *parent);
1516
dee91e82 1517static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1518 gdb_byte *info_ptr,
fe1b8b76 1519 gdb_byte **new_info_ptr,
639d11d3
DC
1520 struct die_info *parent);
1521
3019eac3
DE
1522static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1523 struct die_info **, gdb_byte *, int *, int);
1524
dee91e82
DE
1525static gdb_byte *read_full_die (const struct die_reader_specs *,
1526 struct die_info **, gdb_byte *, int *);
93311388 1527
e7c27a73 1528static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1529
71c25dea
TT
1530static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1531 struct obstack *);
1532
e142c38c 1533static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1534
98bfdba5
PA
1535static const char *dwarf2_full_name (char *name,
1536 struct die_info *die,
1537 struct dwarf2_cu *cu);
1538
e142c38c 1539static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1540 struct dwarf2_cu **);
9219021c 1541
f39c6ffd 1542static const char *dwarf_tag_name (unsigned int);
c906108c 1543
f39c6ffd 1544static const char *dwarf_attr_name (unsigned int);
c906108c 1545
f39c6ffd 1546static const char *dwarf_form_name (unsigned int);
c906108c 1547
a14ed312 1548static char *dwarf_bool_name (unsigned int);
c906108c 1549
f39c6ffd 1550static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1551
f9aca02d 1552static struct die_info *sibling_die (struct die_info *);
c906108c 1553
d97bc12b
DE
1554static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1555
1556static void dump_die_for_error (struct die_info *);
1557
1558static void dump_die_1 (struct ui_file *, int level, int max_level,
1559 struct die_info *);
c906108c 1560
d97bc12b 1561/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1562
51545339 1563static void store_in_ref_table (struct die_info *,
10b3939b 1564 struct dwarf2_cu *);
c906108c 1565
93311388
DE
1566static int is_ref_attr (struct attribute *);
1567
b64f50a1 1568static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1569
43bbcdc2 1570static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1571
348e048f
DE
1572static struct die_info *follow_die_ref_or_sig (struct die_info *,
1573 struct attribute *,
1574 struct dwarf2_cu **);
1575
10b3939b
DJ
1576static struct die_info *follow_die_ref (struct die_info *,
1577 struct attribute *,
f2f0e013 1578 struct dwarf2_cu **);
c906108c 1579
348e048f
DE
1580static struct die_info *follow_die_sig (struct die_info *,
1581 struct attribute *,
1582 struct dwarf2_cu **);
1583
6c83ed52
TT
1584static struct signatured_type *lookup_signatured_type_at_offset
1585 (struct objfile *objfile,
b64f50a1 1586 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1587
e5fe5e75 1588static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1589
52dc124a 1590static void read_signatured_type (struct signatured_type *);
348e048f 1591
f4dc4d17 1592static struct type_unit_group *get_type_unit_group
094b34ac 1593 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1594
1595static void build_type_unit_groups (die_reader_func_ftype *, void *);
1596
c906108c
SS
1597/* memory allocation interface */
1598
7b5a2f43 1599static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1600
b60c80d6 1601static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1602
09262596
DE
1603static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1604 char *, int);
2e276125 1605
8e19ed76
PS
1606static int attr_form_is_block (struct attribute *);
1607
3690dd37
JB
1608static int attr_form_is_section_offset (struct attribute *);
1609
1610static int attr_form_is_constant (struct attribute *);
1611
8cf6f0b1
TT
1612static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1613 struct dwarf2_loclist_baton *baton,
1614 struct attribute *attr);
1615
93e7bd98
DJ
1616static void dwarf2_symbol_mark_computed (struct attribute *attr,
1617 struct symbol *sym,
1618 struct dwarf2_cu *cu);
4c2df51b 1619
dee91e82
DE
1620static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1621 gdb_byte *info_ptr,
1622 struct abbrev_info *abbrev);
4bb7a0a7 1623
72bf9492
DJ
1624static void free_stack_comp_unit (void *);
1625
72bf9492
DJ
1626static hashval_t partial_die_hash (const void *item);
1627
1628static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1629
ae038cb0 1630static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1631 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1632
9816fde3 1633static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1634 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1635
1636static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1637 struct die_info *comp_unit_die,
1638 enum language pretend_language);
93311388 1639
68dc6402 1640static void free_heap_comp_unit (void *);
ae038cb0
DJ
1641
1642static void free_cached_comp_units (void *);
1643
1644static void age_cached_comp_units (void);
1645
dee91e82 1646static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1647
f792889a
DJ
1648static struct type *set_die_type (struct die_info *, struct type *,
1649 struct dwarf2_cu *);
1c379e20 1650
ae038cb0
DJ
1651static void create_all_comp_units (struct objfile *);
1652
0e50663e 1653static int create_all_type_units (struct objfile *);
1fd400ff 1654
95554aad
TT
1655static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1656 enum language);
10b3939b 1657
95554aad
TT
1658static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1659 enum language);
10b3939b 1660
f4dc4d17
DE
1661static void process_full_type_unit (struct dwarf2_per_cu_data *,
1662 enum language);
1663
10b3939b
DJ
1664static void dwarf2_add_dependence (struct dwarf2_cu *,
1665 struct dwarf2_per_cu_data *);
1666
ae038cb0
DJ
1667static void dwarf2_mark (struct dwarf2_cu *);
1668
1669static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1670
b64f50a1 1671static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1672 struct dwarf2_per_cu_data *per_cu);
1673
f792889a 1674static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1675
9291a0cd
TT
1676static void dwarf2_release_queue (void *dummy);
1677
95554aad
TT
1678static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1679 enum language pretend_language);
1680
1681static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1682 struct dwarf2_per_cu_data *per_cu,
1683 enum language pretend_language);
9291a0cd 1684
a0f42c21 1685static void process_queue (void);
9291a0cd
TT
1686
1687static void find_file_and_directory (struct die_info *die,
1688 struct dwarf2_cu *cu,
1689 char **name, char **comp_dir);
1690
1691static char *file_full_name (int file, struct line_header *lh,
1692 const char *comp_dir);
1693
36586728
TT
1694static gdb_byte *read_and_check_comp_unit_head
1695 (struct comp_unit_head *header,
1696 struct dwarf2_section_info *section,
1697 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1698 int is_debug_types_section);
1699
fd820528 1700static void init_cutu_and_read_dies
f4dc4d17
DE
1701 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1702 int use_existing_cu, int keep,
3019eac3
DE
1703 die_reader_func_ftype *die_reader_func, void *data);
1704
dee91e82
DE
1705static void init_cutu_and_read_dies_simple
1706 (struct dwarf2_per_cu_data *this_cu,
1707 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1708
673bfd45 1709static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1710
3019eac3
DE
1711static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1712
1713static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1714 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1715
1716static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1717 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1718
1719static void free_dwo_file_cleanup (void *);
1720
95554aad
TT
1721static void process_cu_includes (void);
1722
1b80a9fa
JK
1723static void check_producer (struct dwarf2_cu *cu);
1724
9291a0cd
TT
1725#if WORDS_BIGENDIAN
1726
1727/* Convert VALUE between big- and little-endian. */
1728static offset_type
1729byte_swap (offset_type value)
1730{
1731 offset_type result;
1732
1733 result = (value & 0xff) << 24;
1734 result |= (value & 0xff00) << 8;
1735 result |= (value & 0xff0000) >> 8;
1736 result |= (value & 0xff000000) >> 24;
1737 return result;
1738}
1739
1740#define MAYBE_SWAP(V) byte_swap (V)
1741
1742#else
1743#define MAYBE_SWAP(V) (V)
1744#endif /* WORDS_BIGENDIAN */
1745
1746/* The suffix for an index file. */
1747#define INDEX_SUFFIX ".gdb-index"
1748
3da10d80
KS
1749static const char *dwarf2_physname (char *name, struct die_info *die,
1750 struct dwarf2_cu *cu);
1751
c906108c 1752/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1753 information and return true if we have enough to do something.
1754 NAMES points to the dwarf2 section names, or is NULL if the standard
1755 ELF names are used. */
c906108c
SS
1756
1757int
251d32d9
TG
1758dwarf2_has_info (struct objfile *objfile,
1759 const struct dwarf2_debug_sections *names)
c906108c 1760{
be391dca
TT
1761 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1762 if (!dwarf2_per_objfile)
1763 {
1764 /* Initialize per-objfile state. */
1765 struct dwarf2_per_objfile *data
1766 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1767
be391dca
TT
1768 memset (data, 0, sizeof (*data));
1769 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1770 dwarf2_per_objfile = data;
6502dd73 1771
251d32d9
TG
1772 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1773 (void *) names);
be391dca
TT
1774 dwarf2_per_objfile->objfile = objfile;
1775 }
1776 return (dwarf2_per_objfile->info.asection != NULL
1777 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1778}
1779
251d32d9
TG
1780/* When loading sections, we look either for uncompressed section or for
1781 compressed section names. */
233a11ab
CS
1782
1783static int
251d32d9
TG
1784section_is_p (const char *section_name,
1785 const struct dwarf2_section_names *names)
233a11ab 1786{
251d32d9
TG
1787 if (names->normal != NULL
1788 && strcmp (section_name, names->normal) == 0)
1789 return 1;
1790 if (names->compressed != NULL
1791 && strcmp (section_name, names->compressed) == 0)
1792 return 1;
1793 return 0;
233a11ab
CS
1794}
1795
c906108c
SS
1796/* This function is mapped across the sections and remembers the
1797 offset and size of each of the debugging sections we are interested
1798 in. */
1799
1800static void
251d32d9 1801dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1802{
251d32d9 1803 const struct dwarf2_debug_sections *names;
dc7650b8 1804 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1805
1806 if (vnames == NULL)
1807 names = &dwarf2_elf_names;
1808 else
1809 names = (const struct dwarf2_debug_sections *) vnames;
1810
dc7650b8
JK
1811 if ((aflag & SEC_HAS_CONTENTS) == 0)
1812 {
1813 }
1814 else if (section_is_p (sectp->name, &names->info))
c906108c 1815 {
dce234bc
PP
1816 dwarf2_per_objfile->info.asection = sectp;
1817 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1818 }
251d32d9 1819 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1820 {
dce234bc
PP
1821 dwarf2_per_objfile->abbrev.asection = sectp;
1822 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1823 }
251d32d9 1824 else if (section_is_p (sectp->name, &names->line))
c906108c 1825 {
dce234bc
PP
1826 dwarf2_per_objfile->line.asection = sectp;
1827 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1828 }
251d32d9 1829 else if (section_is_p (sectp->name, &names->loc))
c906108c 1830 {
dce234bc
PP
1831 dwarf2_per_objfile->loc.asection = sectp;
1832 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1833 }
251d32d9 1834 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1835 {
dce234bc
PP
1836 dwarf2_per_objfile->macinfo.asection = sectp;
1837 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1838 }
cf2c3c16
TT
1839 else if (section_is_p (sectp->name, &names->macro))
1840 {
1841 dwarf2_per_objfile->macro.asection = sectp;
1842 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1843 }
251d32d9 1844 else if (section_is_p (sectp->name, &names->str))
c906108c 1845 {
dce234bc
PP
1846 dwarf2_per_objfile->str.asection = sectp;
1847 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1848 }
3019eac3
DE
1849 else if (section_is_p (sectp->name, &names->addr))
1850 {
1851 dwarf2_per_objfile->addr.asection = sectp;
1852 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1853 }
251d32d9 1854 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1855 {
dce234bc
PP
1856 dwarf2_per_objfile->frame.asection = sectp;
1857 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1858 }
251d32d9 1859 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1860 {
dc7650b8
JK
1861 dwarf2_per_objfile->eh_frame.asection = sectp;
1862 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1863 }
251d32d9 1864 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1865 {
dce234bc
PP
1866 dwarf2_per_objfile->ranges.asection = sectp;
1867 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1868 }
251d32d9 1869 else if (section_is_p (sectp->name, &names->types))
348e048f 1870 {
8b70b953
TT
1871 struct dwarf2_section_info type_section;
1872
1873 memset (&type_section, 0, sizeof (type_section));
1874 type_section.asection = sectp;
1875 type_section.size = bfd_get_section_size (sectp);
1876
1877 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1878 &type_section);
348e048f 1879 }
251d32d9 1880 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1881 {
1882 dwarf2_per_objfile->gdb_index.asection = sectp;
1883 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1884 }
dce234bc 1885
72dca2f5
FR
1886 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1887 && bfd_section_vma (abfd, sectp) == 0)
1888 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1889}
1890
fceca515
DE
1891/* A helper function that decides whether a section is empty,
1892 or not present. */
9e0ac564
TT
1893
1894static int
1895dwarf2_section_empty_p (struct dwarf2_section_info *info)
1896{
1897 return info->asection == NULL || info->size == 0;
1898}
1899
3019eac3
DE
1900/* Read the contents of the section INFO.
1901 OBJFILE is the main object file, but not necessarily the file where
1902 the section comes from. E.g., for DWO files INFO->asection->owner
1903 is the bfd of the DWO file.
dce234bc 1904 If the section is compressed, uncompress it before returning. */
c906108c 1905
dce234bc
PP
1906static void
1907dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1908{
dce234bc 1909 asection *sectp = info->asection;
3019eac3 1910 bfd *abfd;
dce234bc
PP
1911 gdb_byte *buf, *retbuf;
1912 unsigned char header[4];
c906108c 1913
be391dca
TT
1914 if (info->readin)
1915 return;
dce234bc 1916 info->buffer = NULL;
be391dca 1917 info->readin = 1;
188dd5d6 1918
9e0ac564 1919 if (dwarf2_section_empty_p (info))
dce234bc 1920 return;
c906108c 1921
3019eac3
DE
1922 abfd = sectp->owner;
1923
4bf44c1c
TT
1924 /* If the section has relocations, we must read it ourselves.
1925 Otherwise we attach it to the BFD. */
1926 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1927 {
4bf44c1c 1928 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1929
4bf44c1c
TT
1930 /* We have to cast away const here for historical reasons.
1931 Fixing dwarf2read to be const-correct would be quite nice. */
1932 info->buffer = (gdb_byte *) bytes;
1933 return;
dce234bc 1934 }
dce234bc 1935
4bf44c1c
TT
1936 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1937 info->buffer = buf;
dce234bc
PP
1938
1939 /* When debugging .o files, we may need to apply relocations; see
1940 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1941 We never compress sections in .o files, so we only need to
1942 try this when the section is not compressed. */
ac8035ab 1943 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1944 if (retbuf != NULL)
1945 {
1946 info->buffer = retbuf;
1947 return;
1948 }
1949
1950 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1951 || bfd_bread (buf, info->size, abfd) != info->size)
1952 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1953 bfd_get_filename (abfd));
1954}
1955
9e0ac564
TT
1956/* A helper function that returns the size of a section in a safe way.
1957 If you are positive that the section has been read before using the
1958 size, then it is safe to refer to the dwarf2_section_info object's
1959 "size" field directly. In other cases, you must call this
1960 function, because for compressed sections the size field is not set
1961 correctly until the section has been read. */
1962
1963static bfd_size_type
1964dwarf2_section_size (struct objfile *objfile,
1965 struct dwarf2_section_info *info)
1966{
1967 if (!info->readin)
1968 dwarf2_read_section (objfile, info);
1969 return info->size;
1970}
1971
dce234bc 1972/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1973 SECTION_NAME. */
af34e669 1974
dce234bc 1975void
3017a003
TG
1976dwarf2_get_section_info (struct objfile *objfile,
1977 enum dwarf2_section_enum sect,
dce234bc
PP
1978 asection **sectp, gdb_byte **bufp,
1979 bfd_size_type *sizep)
1980{
1981 struct dwarf2_per_objfile *data
1982 = objfile_data (objfile, dwarf2_objfile_data_key);
1983 struct dwarf2_section_info *info;
a3b2a86b
TT
1984
1985 /* We may see an objfile without any DWARF, in which case we just
1986 return nothing. */
1987 if (data == NULL)
1988 {
1989 *sectp = NULL;
1990 *bufp = NULL;
1991 *sizep = 0;
1992 return;
1993 }
3017a003
TG
1994 switch (sect)
1995 {
1996 case DWARF2_DEBUG_FRAME:
1997 info = &data->frame;
1998 break;
1999 case DWARF2_EH_FRAME:
2000 info = &data->eh_frame;
2001 break;
2002 default:
2003 gdb_assert_not_reached ("unexpected section");
2004 }
dce234bc 2005
9e0ac564 2006 dwarf2_read_section (objfile, info);
dce234bc
PP
2007
2008 *sectp = info->asection;
2009 *bufp = info->buffer;
2010 *sizep = info->size;
2011}
2012
36586728
TT
2013/* A helper function to find the sections for a .dwz file. */
2014
2015static void
2016locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2017{
2018 struct dwz_file *dwz_file = arg;
2019
2020 /* Note that we only support the standard ELF names, because .dwz
2021 is ELF-only (at the time of writing). */
2022 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2023 {
2024 dwz_file->abbrev.asection = sectp;
2025 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2026 }
2027 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2028 {
2029 dwz_file->info.asection = sectp;
2030 dwz_file->info.size = bfd_get_section_size (sectp);
2031 }
2032 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2033 {
2034 dwz_file->str.asection = sectp;
2035 dwz_file->str.size = bfd_get_section_size (sectp);
2036 }
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2038 {
2039 dwz_file->line.asection = sectp;
2040 dwz_file->line.size = bfd_get_section_size (sectp);
2041 }
2042 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2043 {
2044 dwz_file->macro.asection = sectp;
2045 dwz_file->macro.size = bfd_get_section_size (sectp);
2046 }
2ec9a5e0
TT
2047 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2048 {
2049 dwz_file->gdb_index.asection = sectp;
2050 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2051 }
36586728
TT
2052}
2053
2054/* Open the separate '.dwz' debug file, if needed. Error if the file
2055 cannot be found. */
2056
2057static struct dwz_file *
2058dwarf2_get_dwz_file (void)
2059{
2060 bfd *abfd, *dwz_bfd;
2061 asection *section;
2062 gdb_byte *data;
2063 struct cleanup *cleanup;
2064 const char *filename;
2065 struct dwz_file *result;
2066
2067 if (dwarf2_per_objfile->dwz_file != NULL)
2068 return dwarf2_per_objfile->dwz_file;
2069
2070 abfd = dwarf2_per_objfile->objfile->obfd;
2071 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2072 if (section == NULL)
2073 error (_("could not find '.gnu_debugaltlink' section"));
2074 if (!bfd_malloc_and_get_section (abfd, section, &data))
2075 error (_("could not read '.gnu_debugaltlink' section: %s"),
2076 bfd_errmsg (bfd_get_error ()));
2077 cleanup = make_cleanup (xfree, data);
2078
2079 filename = data;
2080 if (!IS_ABSOLUTE_PATH (filename))
2081 {
2082 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2083 char *rel;
2084
2085 make_cleanup (xfree, abs);
2086 abs = ldirname (abs);
2087 make_cleanup (xfree, abs);
2088
2089 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2090 make_cleanup (xfree, rel);
2091 filename = rel;
2092 }
2093
2094 /* The format is just a NUL-terminated file name, followed by the
2095 build-id. For now, though, we ignore the build-id. */
2096 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2097 if (dwz_bfd == NULL)
2098 error (_("could not read '%s': %s"), filename,
2099 bfd_errmsg (bfd_get_error ()));
2100
2101 if (!bfd_check_format (dwz_bfd, bfd_object))
2102 {
2103 gdb_bfd_unref (dwz_bfd);
2104 error (_("file '%s' was not usable: %s"), filename,
2105 bfd_errmsg (bfd_get_error ()));
2106 }
2107
2108 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2109 struct dwz_file);
2110 result->dwz_bfd = dwz_bfd;
2111
2112 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2113
2114 do_cleanups (cleanup);
2115
8d2cc612 2116 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2117 return result;
2118}
9291a0cd 2119\f
7b9f3c50
DE
2120/* DWARF quick_symbols_functions support. */
2121
2122/* TUs can share .debug_line entries, and there can be a lot more TUs than
2123 unique line tables, so we maintain a separate table of all .debug_line
2124 derived entries to support the sharing.
2125 All the quick functions need is the list of file names. We discard the
2126 line_header when we're done and don't need to record it here. */
2127struct quick_file_names
2128{
094b34ac
DE
2129 /* The data used to construct the hash key. */
2130 struct stmt_list_hash hash;
7b9f3c50
DE
2131
2132 /* The number of entries in file_names, real_names. */
2133 unsigned int num_file_names;
2134
2135 /* The file names from the line table, after being run through
2136 file_full_name. */
2137 const char **file_names;
2138
2139 /* The file names from the line table after being run through
2140 gdb_realpath. These are computed lazily. */
2141 const char **real_names;
2142};
2143
2144/* When using the index (and thus not using psymtabs), each CU has an
2145 object of this type. This is used to hold information needed by
2146 the various "quick" methods. */
2147struct dwarf2_per_cu_quick_data
2148{
2149 /* The file table. This can be NULL if there was no file table
2150 or it's currently not read in.
2151 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2152 struct quick_file_names *file_names;
2153
2154 /* The corresponding symbol table. This is NULL if symbols for this
2155 CU have not yet been read. */
2156 struct symtab *symtab;
2157
2158 /* A temporary mark bit used when iterating over all CUs in
2159 expand_symtabs_matching. */
2160 unsigned int mark : 1;
2161
2162 /* True if we've tried to read the file table and found there isn't one.
2163 There will be no point in trying to read it again next time. */
2164 unsigned int no_file_data : 1;
2165};
2166
094b34ac
DE
2167/* Utility hash function for a stmt_list_hash. */
2168
2169static hashval_t
2170hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2171{
2172 hashval_t v = 0;
2173
2174 if (stmt_list_hash->dwo_unit != NULL)
2175 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2176 v += stmt_list_hash->line_offset.sect_off;
2177 return v;
2178}
2179
2180/* Utility equality function for a stmt_list_hash. */
2181
2182static int
2183eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2184 const struct stmt_list_hash *rhs)
2185{
2186 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2187 return 0;
2188 if (lhs->dwo_unit != NULL
2189 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2190 return 0;
2191
2192 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2193}
2194
7b9f3c50
DE
2195/* Hash function for a quick_file_names. */
2196
2197static hashval_t
2198hash_file_name_entry (const void *e)
2199{
2200 const struct quick_file_names *file_data = e;
2201
094b34ac 2202 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2203}
2204
2205/* Equality function for a quick_file_names. */
2206
2207static int
2208eq_file_name_entry (const void *a, const void *b)
2209{
2210 const struct quick_file_names *ea = a;
2211 const struct quick_file_names *eb = b;
2212
094b34ac 2213 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2214}
2215
2216/* Delete function for a quick_file_names. */
2217
2218static void
2219delete_file_name_entry (void *e)
2220{
2221 struct quick_file_names *file_data = e;
2222 int i;
2223
2224 for (i = 0; i < file_data->num_file_names; ++i)
2225 {
2226 xfree ((void*) file_data->file_names[i]);
2227 if (file_data->real_names)
2228 xfree ((void*) file_data->real_names[i]);
2229 }
2230
2231 /* The space for the struct itself lives on objfile_obstack,
2232 so we don't free it here. */
2233}
2234
2235/* Create a quick_file_names hash table. */
2236
2237static htab_t
2238create_quick_file_names_table (unsigned int nr_initial_entries)
2239{
2240 return htab_create_alloc (nr_initial_entries,
2241 hash_file_name_entry, eq_file_name_entry,
2242 delete_file_name_entry, xcalloc, xfree);
2243}
9291a0cd 2244
918dd910
JK
2245/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2246 have to be created afterwards. You should call age_cached_comp_units after
2247 processing PER_CU->CU. dw2_setup must have been already called. */
2248
2249static void
2250load_cu (struct dwarf2_per_cu_data *per_cu)
2251{
3019eac3 2252 if (per_cu->is_debug_types)
e5fe5e75 2253 load_full_type_unit (per_cu);
918dd910 2254 else
95554aad 2255 load_full_comp_unit (per_cu, language_minimal);
918dd910 2256
918dd910 2257 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2258
2259 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2260}
2261
a0f42c21 2262/* Read in the symbols for PER_CU. */
2fdf6df6 2263
9291a0cd 2264static void
a0f42c21 2265dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2266{
2267 struct cleanup *back_to;
2268
f4dc4d17
DE
2269 /* Skip type_unit_groups, reading the type units they contain
2270 is handled elsewhere. */
2271 if (IS_TYPE_UNIT_GROUP (per_cu))
2272 return;
2273
9291a0cd
TT
2274 back_to = make_cleanup (dwarf2_release_queue, NULL);
2275
95554aad
TT
2276 if (dwarf2_per_objfile->using_index
2277 ? per_cu->v.quick->symtab == NULL
2278 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2279 {
2280 queue_comp_unit (per_cu, language_minimal);
2281 load_cu (per_cu);
2282 }
9291a0cd 2283
a0f42c21 2284 process_queue ();
9291a0cd
TT
2285
2286 /* Age the cache, releasing compilation units that have not
2287 been used recently. */
2288 age_cached_comp_units ();
2289
2290 do_cleanups (back_to);
2291}
2292
2293/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2294 the objfile from which this CU came. Returns the resulting symbol
2295 table. */
2fdf6df6 2296
9291a0cd 2297static struct symtab *
a0f42c21 2298dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2299{
95554aad 2300 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2301 if (!per_cu->v.quick->symtab)
2302 {
2303 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2304 increment_reading_symtab ();
a0f42c21 2305 dw2_do_instantiate_symtab (per_cu);
95554aad 2306 process_cu_includes ();
9291a0cd
TT
2307 do_cleanups (back_to);
2308 }
2309 return per_cu->v.quick->symtab;
2310}
2311
f4dc4d17
DE
2312/* Return the CU given its index.
2313
2314 This is intended for loops like:
2315
2316 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2317 + dwarf2_per_objfile->n_type_units); ++i)
2318 {
2319 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2320
2321 ...;
2322 }
2323*/
2fdf6df6 2324
1fd400ff
TT
2325static struct dwarf2_per_cu_data *
2326dw2_get_cu (int index)
2327{
2328 if (index >= dwarf2_per_objfile->n_comp_units)
2329 {
f4dc4d17 2330 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2331 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2332 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2333 }
2334
2335 return dwarf2_per_objfile->all_comp_units[index];
2336}
2337
2338/* Return the primary CU given its index.
2339 The difference between this function and dw2_get_cu is in the handling
2340 of type units (TUs). Here we return the type_unit_group object.
2341
2342 This is intended for loops like:
2343
2344 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2345 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2346 {
2347 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2348
2349 ...;
2350 }
2351*/
2352
2353static struct dwarf2_per_cu_data *
2354dw2_get_primary_cu (int index)
2355{
2356 if (index >= dwarf2_per_objfile->n_comp_units)
2357 {
1fd400ff 2358 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2359 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2360 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2361 }
f4dc4d17 2362
1fd400ff
TT
2363 return dwarf2_per_objfile->all_comp_units[index];
2364}
2365
2ec9a5e0
TT
2366/* A helper for create_cus_from_index that handles a given list of
2367 CUs. */
2fdf6df6 2368
74a0d9f6 2369static void
2ec9a5e0
TT
2370create_cus_from_index_list (struct objfile *objfile,
2371 const gdb_byte *cu_list, offset_type n_elements,
2372 struct dwarf2_section_info *section,
2373 int is_dwz,
2374 int base_offset)
9291a0cd
TT
2375{
2376 offset_type i;
9291a0cd 2377
2ec9a5e0 2378 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2379 {
2380 struct dwarf2_per_cu_data *the_cu;
2381 ULONGEST offset, length;
2382
74a0d9f6
JK
2383 gdb_static_assert (sizeof (ULONGEST) >= 8);
2384 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2385 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2386 cu_list += 2 * 8;
2387
2388 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2389 struct dwarf2_per_cu_data);
b64f50a1 2390 the_cu->offset.sect_off = offset;
9291a0cd
TT
2391 the_cu->length = length;
2392 the_cu->objfile = objfile;
2ec9a5e0 2393 the_cu->info_or_types_section = section;
9291a0cd
TT
2394 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2395 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2396 the_cu->is_dwz = is_dwz;
2397 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2398 }
9291a0cd
TT
2399}
2400
2ec9a5e0 2401/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2402 the CU objects for this objfile. */
2ec9a5e0 2403
74a0d9f6 2404static void
2ec9a5e0
TT
2405create_cus_from_index (struct objfile *objfile,
2406 const gdb_byte *cu_list, offset_type cu_list_elements,
2407 const gdb_byte *dwz_list, offset_type dwz_elements)
2408{
2409 struct dwz_file *dwz;
2410
2411 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2412 dwarf2_per_objfile->all_comp_units
2413 = obstack_alloc (&objfile->objfile_obstack,
2414 dwarf2_per_objfile->n_comp_units
2415 * sizeof (struct dwarf2_per_cu_data *));
2416
74a0d9f6
JK
2417 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2418 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2419
2420 if (dwz_elements == 0)
74a0d9f6 2421 return;
2ec9a5e0
TT
2422
2423 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2424 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2425 cu_list_elements / 2);
2ec9a5e0
TT
2426}
2427
1fd400ff 2428/* Create the signatured type hash table from the index. */
673bfd45 2429
74a0d9f6 2430static void
673bfd45 2431create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2432 struct dwarf2_section_info *section,
673bfd45
DE
2433 const gdb_byte *bytes,
2434 offset_type elements)
1fd400ff
TT
2435{
2436 offset_type i;
673bfd45 2437 htab_t sig_types_hash;
1fd400ff 2438
d467dd73
DE
2439 dwarf2_per_objfile->n_type_units = elements / 3;
2440 dwarf2_per_objfile->all_type_units
1fd400ff 2441 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2442 dwarf2_per_objfile->n_type_units
b4dd5633 2443 * sizeof (struct signatured_type *));
1fd400ff 2444
673bfd45 2445 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2446
2447 for (i = 0; i < elements; i += 3)
2448 {
52dc124a
DE
2449 struct signatured_type *sig_type;
2450 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2451 void **slot;
2452
74a0d9f6
JK
2453 gdb_static_assert (sizeof (ULONGEST) >= 8);
2454 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2455 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2456 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2457 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2458 bytes += 3 * 8;
2459
52dc124a 2460 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2461 struct signatured_type);
52dc124a 2462 sig_type->signature = signature;
3019eac3
DE
2463 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2464 sig_type->per_cu.is_debug_types = 1;
2465 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2466 sig_type->per_cu.offset.sect_off = offset;
2467 sig_type->per_cu.objfile = objfile;
2468 sig_type->per_cu.v.quick
1fd400ff
TT
2469 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2470 struct dwarf2_per_cu_quick_data);
2471
52dc124a
DE
2472 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2473 *slot = sig_type;
1fd400ff 2474
b4dd5633 2475 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2476 }
2477
673bfd45 2478 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2479}
2480
9291a0cd
TT
2481/* Read the address map data from the mapped index, and use it to
2482 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2483
9291a0cd
TT
2484static void
2485create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2486{
2487 const gdb_byte *iter, *end;
2488 struct obstack temp_obstack;
2489 struct addrmap *mutable_map;
2490 struct cleanup *cleanup;
2491 CORE_ADDR baseaddr;
2492
2493 obstack_init (&temp_obstack);
2494 cleanup = make_cleanup_obstack_free (&temp_obstack);
2495 mutable_map = addrmap_create_mutable (&temp_obstack);
2496
2497 iter = index->address_table;
2498 end = iter + index->address_table_size;
2499
2500 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2501
2502 while (iter < end)
2503 {
2504 ULONGEST hi, lo, cu_index;
2505 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2506 iter += 8;
2507 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2508 iter += 8;
2509 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2510 iter += 4;
2511
2512 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2513 dw2_get_cu (cu_index));
9291a0cd
TT
2514 }
2515
2516 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2517 &objfile->objfile_obstack);
2518 do_cleanups (cleanup);
2519}
2520
59d7bcaf
JK
2521/* The hash function for strings in the mapped index. This is the same as
2522 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2523 implementation. This is necessary because the hash function is tied to the
2524 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2525 SYMBOL_HASH_NEXT.
2526
2527 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2528
9291a0cd 2529static hashval_t
559a7a62 2530mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2531{
2532 const unsigned char *str = (const unsigned char *) p;
2533 hashval_t r = 0;
2534 unsigned char c;
2535
2536 while ((c = *str++) != 0)
559a7a62
JK
2537 {
2538 if (index_version >= 5)
2539 c = tolower (c);
2540 r = r * 67 + c - 113;
2541 }
9291a0cd
TT
2542
2543 return r;
2544}
2545
2546/* Find a slot in the mapped index INDEX for the object named NAME.
2547 If NAME is found, set *VEC_OUT to point to the CU vector in the
2548 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2549
9291a0cd
TT
2550static int
2551find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2552 offset_type **vec_out)
2553{
0cf03b49
JK
2554 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2555 offset_type hash;
9291a0cd 2556 offset_type slot, step;
559a7a62 2557 int (*cmp) (const char *, const char *);
9291a0cd 2558
0cf03b49
JK
2559 if (current_language->la_language == language_cplus
2560 || current_language->la_language == language_java
2561 || current_language->la_language == language_fortran)
2562 {
2563 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2564 not contain any. */
2565 const char *paren = strchr (name, '(');
2566
2567 if (paren)
2568 {
2569 char *dup;
2570
2571 dup = xmalloc (paren - name + 1);
2572 memcpy (dup, name, paren - name);
2573 dup[paren - name] = 0;
2574
2575 make_cleanup (xfree, dup);
2576 name = dup;
2577 }
2578 }
2579
559a7a62 2580 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2581 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2582 simulate our NAME being searched is also lowercased. */
2583 hash = mapped_index_string_hash ((index->version == 4
2584 && case_sensitivity == case_sensitive_off
2585 ? 5 : index->version),
2586 name);
2587
3876f04e
DE
2588 slot = hash & (index->symbol_table_slots - 1);
2589 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2590 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2591
2592 for (;;)
2593 {
2594 /* Convert a slot number to an offset into the table. */
2595 offset_type i = 2 * slot;
2596 const char *str;
3876f04e 2597 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2598 {
2599 do_cleanups (back_to);
2600 return 0;
2601 }
9291a0cd 2602
3876f04e 2603 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2604 if (!cmp (name, str))
9291a0cd
TT
2605 {
2606 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2607 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2608 do_cleanups (back_to);
9291a0cd
TT
2609 return 1;
2610 }
2611
3876f04e 2612 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2613 }
2614}
2615
2ec9a5e0
TT
2616/* A helper function that reads the .gdb_index from SECTION and fills
2617 in MAP. FILENAME is the name of the file containing the section;
2618 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2619 ok to use deprecated sections.
2620
2621 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2622 out parameters that are filled in with information about the CU and
2623 TU lists in the section.
2624
2625 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2626
9291a0cd 2627static int
2ec9a5e0
TT
2628read_index_from_section (struct objfile *objfile,
2629 const char *filename,
2630 int deprecated_ok,
2631 struct dwarf2_section_info *section,
2632 struct mapped_index *map,
2633 const gdb_byte **cu_list,
2634 offset_type *cu_list_elements,
2635 const gdb_byte **types_list,
2636 offset_type *types_list_elements)
9291a0cd 2637{
9291a0cd 2638 char *addr;
2ec9a5e0 2639 offset_type version;
b3b272e1 2640 offset_type *metadata;
1fd400ff 2641 int i;
9291a0cd 2642
2ec9a5e0 2643 if (dwarf2_section_empty_p (section))
9291a0cd 2644 return 0;
82430852
JK
2645
2646 /* Older elfutils strip versions could keep the section in the main
2647 executable while splitting it for the separate debug info file. */
2ec9a5e0 2648 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2649 return 0;
2650
2ec9a5e0 2651 dwarf2_read_section (objfile, section);
9291a0cd 2652
2ec9a5e0 2653 addr = section->buffer;
9291a0cd 2654 /* Version check. */
1fd400ff 2655 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2656 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2657 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2658 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2659 indices. */
831adc1f 2660 if (version < 4)
481860b3
GB
2661 {
2662 static int warning_printed = 0;
2663 if (!warning_printed)
2664 {
2665 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2666 filename);
481860b3
GB
2667 warning_printed = 1;
2668 }
2669 return 0;
2670 }
2671 /* Index version 4 uses a different hash function than index version
2672 5 and later.
2673
2674 Versions earlier than 6 did not emit psymbols for inlined
2675 functions. Using these files will cause GDB not to be able to
2676 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2677 indices unless the user has done
2678 "set use-deprecated-index-sections on". */
2ec9a5e0 2679 if (version < 6 && !deprecated_ok)
481860b3
GB
2680 {
2681 static int warning_printed = 0;
2682 if (!warning_printed)
2683 {
e615022a
DE
2684 warning (_("\
2685Skipping deprecated .gdb_index section in %s.\n\
2686Do \"set use-deprecated-index-sections on\" before the file is read\n\
2687to use the section anyway."),
2ec9a5e0 2688 filename);
481860b3
GB
2689 warning_printed = 1;
2690 }
2691 return 0;
2692 }
2693 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2694 longer backward compatible. */
156942c7 2695 if (version > 7)
594e8718 2696 return 0;
9291a0cd 2697
559a7a62 2698 map->version = version;
2ec9a5e0 2699 map->total_size = section->size;
9291a0cd
TT
2700
2701 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2702
2703 i = 0;
2ec9a5e0
TT
2704 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2705 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2706 / 8);
1fd400ff
TT
2707 ++i;
2708
2ec9a5e0
TT
2709 *types_list = addr + MAYBE_SWAP (metadata[i]);
2710 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2711 - MAYBE_SWAP (metadata[i]))
2712 / 8);
987d643c 2713 ++i;
1fd400ff
TT
2714
2715 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2716 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2717 - MAYBE_SWAP (metadata[i]));
2718 ++i;
2719
3876f04e
DE
2720 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2721 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2722 - MAYBE_SWAP (metadata[i]))
2723 / (2 * sizeof (offset_type)));
1fd400ff 2724 ++i;
9291a0cd 2725
1fd400ff
TT
2726 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2727
2ec9a5e0
TT
2728 return 1;
2729}
2730
2731
2732/* Read the index file. If everything went ok, initialize the "quick"
2733 elements of all the CUs and return 1. Otherwise, return 0. */
2734
2735static int
2736dwarf2_read_index (struct objfile *objfile)
2737{
2738 struct mapped_index local_map, *map;
2739 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2740 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2741
2742 if (!read_index_from_section (objfile, objfile->name,
2743 use_deprecated_index_sections,
2744 &dwarf2_per_objfile->gdb_index, &local_map,
2745 &cu_list, &cu_list_elements,
2746 &types_list, &types_list_elements))
2747 return 0;
2748
0fefef59 2749 /* Don't use the index if it's empty. */
2ec9a5e0 2750 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2751 return 0;
2752
2ec9a5e0
TT
2753 /* If there is a .dwz file, read it so we can get its CU list as
2754 well. */
2755 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2756 {
2757 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2758 struct mapped_index dwz_map;
2759 const gdb_byte *dwz_types_ignore;
2760 offset_type dwz_types_elements_ignore;
2761
2762 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2763 1,
2764 &dwz->gdb_index, &dwz_map,
2765 &dwz_list, &dwz_list_elements,
2766 &dwz_types_ignore,
2767 &dwz_types_elements_ignore))
2768 {
2769 warning (_("could not read '.gdb_index' section from %s; skipping"),
2770 bfd_get_filename (dwz->dwz_bfd));
2771 return 0;
2772 }
2773 }
2774
74a0d9f6
JK
2775 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2776 dwz_list_elements);
1fd400ff 2777
8b70b953
TT
2778 if (types_list_elements)
2779 {
2780 struct dwarf2_section_info *section;
2781
2782 /* We can only handle a single .debug_types when we have an
2783 index. */
2784 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2785 return 0;
2786
2787 section = VEC_index (dwarf2_section_info_def,
2788 dwarf2_per_objfile->types, 0);
2789
74a0d9f6
JK
2790 create_signatured_type_table_from_index (objfile, section, types_list,
2791 types_list_elements);
8b70b953 2792 }
9291a0cd 2793
2ec9a5e0
TT
2794 create_addrmap_from_index (objfile, &local_map);
2795
2796 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2797 *map = local_map;
9291a0cd
TT
2798
2799 dwarf2_per_objfile->index_table = map;
2800 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2801 dwarf2_per_objfile->quick_file_names_table =
2802 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2803
2804 return 1;
2805}
2806
2807/* A helper for the "quick" functions which sets the global
2808 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2809
9291a0cd
TT
2810static void
2811dw2_setup (struct objfile *objfile)
2812{
2813 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2814 gdb_assert (dwarf2_per_objfile);
2815}
2816
f4dc4d17
DE
2817/* Reader function for dw2_build_type_unit_groups. */
2818
2819static void
2820dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2821 gdb_byte *info_ptr,
2822 struct die_info *type_unit_die,
2823 int has_children,
2824 void *data)
2825{
2826 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2827 struct attribute *attr;
2828 struct type_unit_group *tu_group;
2829
2830 gdb_assert (data == NULL);
2831
2832 if (! has_children)
2833 return;
2834
2835 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2836 /* Call this for its side-effect of creating the associated
2837 struct type_unit_group if it doesn't already exist. */
094b34ac 2838 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2839}
2840
2841/* Build dwarf2_per_objfile->type_unit_groups.
2842 This function may be called multiple times. */
2843
2844static void
2845dw2_build_type_unit_groups (void)
2846{
2847 if (dwarf2_per_objfile->type_unit_groups == NULL)
2848 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2849}
2850
dee91e82 2851/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2852
dee91e82
DE
2853static void
2854dw2_get_file_names_reader (const struct die_reader_specs *reader,
2855 gdb_byte *info_ptr,
2856 struct die_info *comp_unit_die,
2857 int has_children,
2858 void *data)
9291a0cd 2859{
dee91e82
DE
2860 struct dwarf2_cu *cu = reader->cu;
2861 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2862 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2863 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2864 struct line_header *lh;
9291a0cd 2865 struct attribute *attr;
dee91e82 2866 int i;
9291a0cd 2867 char *name, *comp_dir;
7b9f3c50
DE
2868 void **slot;
2869 struct quick_file_names *qfn;
2870 unsigned int line_offset;
9291a0cd 2871
07261596
TT
2872 /* Our callers never want to match partial units -- instead they
2873 will match the enclosing full CU. */
2874 if (comp_unit_die->tag == DW_TAG_partial_unit)
2875 {
2876 this_cu->v.quick->no_file_data = 1;
2877 return;
2878 }
2879
094b34ac
DE
2880 /* If we're reading the line header for TUs, store it in the "per_cu"
2881 for tu_group. */
2882 if (this_cu->is_debug_types)
2883 {
2884 struct type_unit_group *tu_group = data;
2885
2886 gdb_assert (tu_group != NULL);
2887 lh_cu = &tu_group->per_cu;
2888 }
2889 else
2890 lh_cu = this_cu;
2891
7b9f3c50
DE
2892 lh = NULL;
2893 slot = NULL;
2894 line_offset = 0;
dee91e82
DE
2895
2896 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2897 if (attr)
2898 {
7b9f3c50
DE
2899 struct quick_file_names find_entry;
2900
2901 line_offset = DW_UNSND (attr);
2902
2903 /* We may have already read in this line header (TU line header sharing).
2904 If we have we're done. */
094b34ac
DE
2905 find_entry.hash.dwo_unit = cu->dwo_unit;
2906 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2907 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2908 &find_entry, INSERT);
2909 if (*slot != NULL)
2910 {
094b34ac 2911 lh_cu->v.quick->file_names = *slot;
dee91e82 2912 return;
7b9f3c50
DE
2913 }
2914
3019eac3 2915 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2916 }
2917 if (lh == NULL)
2918 {
094b34ac 2919 lh_cu->v.quick->no_file_data = 1;
dee91e82 2920 return;
9291a0cd
TT
2921 }
2922
7b9f3c50 2923 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2924 qfn->hash.dwo_unit = cu->dwo_unit;
2925 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2926 gdb_assert (slot != NULL);
2927 *slot = qfn;
9291a0cd 2928
dee91e82 2929 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2930
7b9f3c50
DE
2931 qfn->num_file_names = lh->num_file_names;
2932 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2933 lh->num_file_names * sizeof (char *));
9291a0cd 2934 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2935 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2936 qfn->real_names = NULL;
9291a0cd 2937
7b9f3c50 2938 free_line_header (lh);
7b9f3c50 2939
094b34ac 2940 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2941}
2942
2943/* A helper for the "quick" functions which attempts to read the line
2944 table for THIS_CU. */
2945
2946static struct quick_file_names *
2947dw2_get_file_names (struct objfile *objfile,
2948 struct dwarf2_per_cu_data *this_cu)
2949{
f4dc4d17
DE
2950 /* For TUs this should only be called on the parent group. */
2951 if (this_cu->is_debug_types)
2952 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2953
dee91e82
DE
2954 if (this_cu->v.quick->file_names != NULL)
2955 return this_cu->v.quick->file_names;
2956 /* If we know there is no line data, no point in looking again. */
2957 if (this_cu->v.quick->no_file_data)
2958 return NULL;
2959
3019eac3
DE
2960 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2961 in the stub for CUs, there's is no need to lookup the DWO file.
2962 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2963 DWO file. */
2964 if (this_cu->is_debug_types)
094b34ac
DE
2965 {
2966 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2967
2968 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2969 dw2_get_file_names_reader, tu_group);
2970 }
3019eac3
DE
2971 else
2972 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2973
2974 if (this_cu->v.quick->no_file_data)
2975 return NULL;
2976 return this_cu->v.quick->file_names;
9291a0cd
TT
2977}
2978
2979/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2980 real path for a given file name from the line table. */
2fdf6df6 2981
9291a0cd 2982static const char *
7b9f3c50
DE
2983dw2_get_real_path (struct objfile *objfile,
2984 struct quick_file_names *qfn, int index)
9291a0cd 2985{
7b9f3c50
DE
2986 if (qfn->real_names == NULL)
2987 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2988 qfn->num_file_names, sizeof (char *));
9291a0cd 2989
7b9f3c50
DE
2990 if (qfn->real_names[index] == NULL)
2991 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2992
7b9f3c50 2993 return qfn->real_names[index];
9291a0cd
TT
2994}
2995
2996static struct symtab *
2997dw2_find_last_source_symtab (struct objfile *objfile)
2998{
2999 int index;
ae2de4f8 3000
9291a0cd
TT
3001 dw2_setup (objfile);
3002 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3003 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3004}
3005
7b9f3c50
DE
3006/* Traversal function for dw2_forget_cached_source_info. */
3007
3008static int
3009dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3010{
7b9f3c50 3011 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3012
7b9f3c50 3013 if (file_data->real_names)
9291a0cd 3014 {
7b9f3c50 3015 int i;
9291a0cd 3016
7b9f3c50 3017 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3018 {
7b9f3c50
DE
3019 xfree ((void*) file_data->real_names[i]);
3020 file_data->real_names[i] = NULL;
9291a0cd
TT
3021 }
3022 }
7b9f3c50
DE
3023
3024 return 1;
3025}
3026
3027static void
3028dw2_forget_cached_source_info (struct objfile *objfile)
3029{
3030 dw2_setup (objfile);
3031
3032 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3033 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3034}
3035
f8eba3c6
TT
3036/* Helper function for dw2_map_symtabs_matching_filename that expands
3037 the symtabs and calls the iterator. */
3038
3039static int
3040dw2_map_expand_apply (struct objfile *objfile,
3041 struct dwarf2_per_cu_data *per_cu,
3042 const char *name,
3043 const char *full_path, const char *real_path,
3044 int (*callback) (struct symtab *, void *),
3045 void *data)
3046{
3047 struct symtab *last_made = objfile->symtabs;
3048
3049 /* Don't visit already-expanded CUs. */
3050 if (per_cu->v.quick->symtab)
3051 return 0;
3052
3053 /* This may expand more than one symtab, and we want to iterate over
3054 all of them. */
a0f42c21 3055 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3056
3057 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3058 objfile->symtabs, last_made);
3059}
3060
3061/* Implementation of the map_symtabs_matching_filename method. */
3062
9291a0cd 3063static int
f8eba3c6
TT
3064dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3065 const char *full_path, const char *real_path,
3066 int (*callback) (struct symtab *, void *),
3067 void *data)
9291a0cd
TT
3068{
3069 int i;
c011a4f4 3070 const char *name_basename = lbasename (name);
4aac40c8 3071 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3072
3073 dw2_setup (objfile);
ae2de4f8 3074
f4dc4d17
DE
3075 dw2_build_type_unit_groups ();
3076
1fd400ff 3077 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3078 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3079 {
3080 int j;
f4dc4d17 3081 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3082 struct quick_file_names *file_data;
9291a0cd 3083
3d7bb9d9 3084 /* We only need to look at symtabs not already expanded. */
e254ef6a 3085 if (per_cu->v.quick->symtab)
9291a0cd
TT
3086 continue;
3087
7b9f3c50
DE
3088 file_data = dw2_get_file_names (objfile, per_cu);
3089 if (file_data == NULL)
9291a0cd
TT
3090 continue;
3091
7b9f3c50 3092 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3093 {
7b9f3c50 3094 const char *this_name = file_data->file_names[j];
9291a0cd 3095
4aac40c8 3096 if (FILENAME_CMP (name, this_name) == 0
b57a636e 3097 || (!is_abs && compare_filenames_for_search (this_name, name)))
9291a0cd 3098 {
f8eba3c6
TT
3099 if (dw2_map_expand_apply (objfile, per_cu,
3100 name, full_path, real_path,
3101 callback, data))
3102 return 1;
4aac40c8 3103 }
9291a0cd 3104
c011a4f4
DE
3105 /* Before we invoke realpath, which can get expensive when many
3106 files are involved, do a quick comparison of the basenames. */
3107 if (! basenames_may_differ
3108 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3109 continue;
3110
9291a0cd
TT
3111 if (full_path != NULL)
3112 {
7b9f3c50
DE
3113 const char *this_real_name = dw2_get_real_path (objfile,
3114 file_data, j);
9291a0cd 3115
7b9f3c50 3116 if (this_real_name != NULL
4aac40c8
TT
3117 && (FILENAME_CMP (full_path, this_real_name) == 0
3118 || (!is_abs
3119 && compare_filenames_for_search (this_real_name,
b57a636e 3120 name))))
9291a0cd 3121 {
f8eba3c6
TT
3122 if (dw2_map_expand_apply (objfile, per_cu,
3123 name, full_path, real_path,
3124 callback, data))
3125 return 1;
9291a0cd
TT
3126 }
3127 }
3128
3129 if (real_path != NULL)
3130 {
7b9f3c50
DE
3131 const char *this_real_name = dw2_get_real_path (objfile,
3132 file_data, j);
9291a0cd 3133
7b9f3c50 3134 if (this_real_name != NULL
4aac40c8
TT
3135 && (FILENAME_CMP (real_path, this_real_name) == 0
3136 || (!is_abs
3137 && compare_filenames_for_search (this_real_name,
b57a636e 3138 name))))
9291a0cd 3139 {
f8eba3c6
TT
3140 if (dw2_map_expand_apply (objfile, per_cu,
3141 name, full_path, real_path,
3142 callback, data))
3143 return 1;
9291a0cd
TT
3144 }
3145 }
3146 }
3147 }
3148
9291a0cd
TT
3149 return 0;
3150}
3151
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);
5c80ed9d 4898 sort_pst_symbols (objfile, 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);
5c80ed9d 5326 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
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
5c80ed9d 6327dwarf2_psymtab_to_symtab (struct objfile *objfile, 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 6345 /* Restore our global data. */
5c80ed9d 6346 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6347
b2ab525c
KB
6348 /* If this psymtab is constructed from a debug-only objfile, the
6349 has_section_at_zero flag will not necessarily be correct. We
6350 can get the correct value for this flag by looking at the data
6351 associated with the (presumably stripped) associated objfile. */
5c80ed9d 6352 if (objfile->separate_debug_objfile_backlink)
b2ab525c
KB
6353 {
6354 struct dwarf2_per_objfile *dpo_backlink
5c80ed9d 6355 = objfile_data (objfile->separate_debug_objfile_backlink,
b2ab525c 6356 dwarf2_objfile_data_key);
9a619af0 6357
b2ab525c
KB
6358 dwarf2_per_objfile->has_section_at_zero
6359 = dpo_backlink->has_section_at_zero;
6360 }
6361
98bfdba5
PA
6362 dwarf2_per_objfile->reading_partial_symbols = 0;
6363
c906108c
SS
6364 psymtab_to_symtab_1 (pst);
6365
6366 /* Finish up the debug error message. */
6367 if (info_verbose)
a3f17187 6368 printf_filtered (_("done.\n"));
c906108c
SS
6369 }
6370 }
95554aad
TT
6371
6372 process_cu_includes ();
c906108c 6373}
9cdd5dbd
DE
6374\f
6375/* Reading in full CUs. */
c906108c 6376
10b3939b
DJ
6377/* Add PER_CU to the queue. */
6378
6379static void
95554aad
TT
6380queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6381 enum language pretend_language)
10b3939b
DJ
6382{
6383 struct dwarf2_queue_item *item;
6384
6385 per_cu->queued = 1;
6386 item = xmalloc (sizeof (*item));
6387 item->per_cu = per_cu;
95554aad 6388 item->pretend_language = pretend_language;
10b3939b
DJ
6389 item->next = NULL;
6390
6391 if (dwarf2_queue == NULL)
6392 dwarf2_queue = item;
6393 else
6394 dwarf2_queue_tail->next = item;
6395
6396 dwarf2_queue_tail = item;
6397}
6398
0907af0c
DE
6399/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6400 unit and add it to our queue.
6401 The result is non-zero if PER_CU was queued, otherwise the result is zero
6402 meaning either PER_CU is already queued or it is already loaded. */
6403
6404static int
6405maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6406 struct dwarf2_per_cu_data *per_cu,
6407 enum language pretend_language)
6408{
6409 /* We may arrive here during partial symbol reading, if we need full
6410 DIEs to process an unusual case (e.g. template arguments). Do
6411 not queue PER_CU, just tell our caller to load its DIEs. */
6412 if (dwarf2_per_objfile->reading_partial_symbols)
6413 {
6414 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6415 return 1;
6416 return 0;
6417 }
6418
6419 /* Mark the dependence relation so that we don't flush PER_CU
6420 too early. */
6421 dwarf2_add_dependence (this_cu, per_cu);
6422
6423 /* If it's already on the queue, we have nothing to do. */
6424 if (per_cu->queued)
6425 return 0;
6426
6427 /* If the compilation unit is already loaded, just mark it as
6428 used. */
6429 if (per_cu->cu != NULL)
6430 {
6431 per_cu->cu->last_used = 0;
6432 return 0;
6433 }
6434
6435 /* Add it to the queue. */
6436 queue_comp_unit (per_cu, pretend_language);
6437
6438 return 1;
6439}
6440
10b3939b
DJ
6441/* Process the queue. */
6442
6443static void
a0f42c21 6444process_queue (void)
10b3939b
DJ
6445{
6446 struct dwarf2_queue_item *item, *next_item;
6447
45cfd468
DE
6448 if (dwarf2_read_debug)
6449 {
6450 fprintf_unfiltered (gdb_stdlog,
6451 "Expanding one or more symtabs of objfile %s ...\n",
6452 dwarf2_per_objfile->objfile->name);
6453 }
6454
03dd20cc
DJ
6455 /* The queue starts out with one item, but following a DIE reference
6456 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6457 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6458 {
9291a0cd
TT
6459 if (dwarf2_per_objfile->using_index
6460 ? !item->per_cu->v.quick->symtab
6461 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6462 {
6463 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6464
6465 if (dwarf2_read_debug)
6466 {
6467 fprintf_unfiltered (gdb_stdlog,
6468 "Expanding symtab of %s at offset 0x%x\n",
6469 per_cu->is_debug_types ? "TU" : "CU",
6470 per_cu->offset.sect_off);
6471 }
6472
6473 if (per_cu->is_debug_types)
6474 process_full_type_unit (per_cu, item->pretend_language);
6475 else
6476 process_full_comp_unit (per_cu, item->pretend_language);
6477
6478 if (dwarf2_read_debug)
6479 {
6480 fprintf_unfiltered (gdb_stdlog,
6481 "Done expanding %s at offset 0x%x\n",
6482 per_cu->is_debug_types ? "TU" : "CU",
6483 per_cu->offset.sect_off);
6484 }
6485 }
10b3939b
DJ
6486
6487 item->per_cu->queued = 0;
6488 next_item = item->next;
6489 xfree (item);
6490 }
6491
6492 dwarf2_queue_tail = NULL;
45cfd468
DE
6493
6494 if (dwarf2_read_debug)
6495 {
6496 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6497 dwarf2_per_objfile->objfile->name);
6498 }
10b3939b
DJ
6499}
6500
6501/* Free all allocated queue entries. This function only releases anything if
6502 an error was thrown; if the queue was processed then it would have been
6503 freed as we went along. */
6504
6505static void
6506dwarf2_release_queue (void *dummy)
6507{
6508 struct dwarf2_queue_item *item, *last;
6509
6510 item = dwarf2_queue;
6511 while (item)
6512 {
6513 /* Anything still marked queued is likely to be in an
6514 inconsistent state, so discard it. */
6515 if (item->per_cu->queued)
6516 {
6517 if (item->per_cu->cu != NULL)
dee91e82 6518 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6519 item->per_cu->queued = 0;
6520 }
6521
6522 last = item;
6523 item = item->next;
6524 xfree (last);
6525 }
6526
6527 dwarf2_queue = dwarf2_queue_tail = NULL;
6528}
6529
6530/* Read in full symbols for PST, and anything it depends on. */
6531
c906108c 6532static void
fba45db2 6533psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6534{
10b3939b 6535 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6536 int i;
6537
95554aad
TT
6538 if (pst->readin)
6539 return;
6540
aaa75496 6541 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6542 if (!pst->dependencies[i]->readin
6543 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6544 {
6545 /* Inform about additional files that need to be read in. */
6546 if (info_verbose)
6547 {
a3f17187 6548 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6549 fputs_filtered (" ", gdb_stdout);
6550 wrap_here ("");
6551 fputs_filtered ("and ", gdb_stdout);
6552 wrap_here ("");
6553 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6554 wrap_here (""); /* Flush output. */
aaa75496
JB
6555 gdb_flush (gdb_stdout);
6556 }
6557 psymtab_to_symtab_1 (pst->dependencies[i]);
6558 }
6559
e38df1d0 6560 per_cu = pst->read_symtab_private;
10b3939b
DJ
6561
6562 if (per_cu == NULL)
aaa75496
JB
6563 {
6564 /* It's an include file, no symbols to read for it.
6565 Everything is in the parent symtab. */
6566 pst->readin = 1;
6567 return;
6568 }
c906108c 6569
a0f42c21 6570 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6571}
6572
dee91e82
DE
6573/* Trivial hash function for die_info: the hash value of a DIE
6574 is its offset in .debug_info for this objfile. */
10b3939b 6575
dee91e82
DE
6576static hashval_t
6577die_hash (const void *item)
10b3939b 6578{
dee91e82 6579 const struct die_info *die = item;
6502dd73 6580
dee91e82
DE
6581 return die->offset.sect_off;
6582}
63d06c5c 6583
dee91e82
DE
6584/* Trivial comparison function for die_info structures: two DIEs
6585 are equal if they have the same offset. */
98bfdba5 6586
dee91e82
DE
6587static int
6588die_eq (const void *item_lhs, const void *item_rhs)
6589{
6590 const struct die_info *die_lhs = item_lhs;
6591 const struct die_info *die_rhs = item_rhs;
c906108c 6592
dee91e82
DE
6593 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6594}
c906108c 6595
dee91e82
DE
6596/* die_reader_func for load_full_comp_unit.
6597 This is identical to read_signatured_type_reader,
6598 but is kept separate for now. */
c906108c 6599
dee91e82
DE
6600static void
6601load_full_comp_unit_reader (const struct die_reader_specs *reader,
6602 gdb_byte *info_ptr,
6603 struct die_info *comp_unit_die,
6604 int has_children,
6605 void *data)
6606{
6607 struct dwarf2_cu *cu = reader->cu;
95554aad 6608 enum language *language_ptr = data;
6caca83c 6609
dee91e82
DE
6610 gdb_assert (cu->die_hash == NULL);
6611 cu->die_hash =
6612 htab_create_alloc_ex (cu->header.length / 12,
6613 die_hash,
6614 die_eq,
6615 NULL,
6616 &cu->comp_unit_obstack,
6617 hashtab_obstack_allocate,
6618 dummy_obstack_deallocate);
e142c38c 6619
dee91e82
DE
6620 if (has_children)
6621 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6622 &info_ptr, comp_unit_die);
6623 cu->dies = comp_unit_die;
6624 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6625
6626 /* We try not to read any attributes in this function, because not
9cdd5dbd 6627 all CUs needed for references have been loaded yet, and symbol
10b3939b 6628 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6629 or we won't be able to build types correctly.
6630 Similarly, if we do not read the producer, we can not apply
6631 producer-specific interpretation. */
95554aad 6632 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6633}
10b3939b 6634
dee91e82 6635/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6636
dee91e82 6637static void
95554aad
TT
6638load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6639 enum language pretend_language)
dee91e82 6640{
3019eac3 6641 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6642
f4dc4d17
DE
6643 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6644 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6645}
6646
3da10d80
KS
6647/* Add a DIE to the delayed physname list. */
6648
6649static void
6650add_to_method_list (struct type *type, int fnfield_index, int index,
6651 const char *name, struct die_info *die,
6652 struct dwarf2_cu *cu)
6653{
6654 struct delayed_method_info mi;
6655 mi.type = type;
6656 mi.fnfield_index = fnfield_index;
6657 mi.index = index;
6658 mi.name = name;
6659 mi.die = die;
6660 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6661}
6662
6663/* A cleanup for freeing the delayed method list. */
6664
6665static void
6666free_delayed_list (void *ptr)
6667{
6668 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6669 if (cu->method_list != NULL)
6670 {
6671 VEC_free (delayed_method_info, cu->method_list);
6672 cu->method_list = NULL;
6673 }
6674}
6675
6676/* Compute the physnames of any methods on the CU's method list.
6677
6678 The computation of method physnames is delayed in order to avoid the
6679 (bad) condition that one of the method's formal parameters is of an as yet
6680 incomplete type. */
6681
6682static void
6683compute_delayed_physnames (struct dwarf2_cu *cu)
6684{
6685 int i;
6686 struct delayed_method_info *mi;
6687 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6688 {
1d06ead6 6689 const char *physname;
3da10d80
KS
6690 struct fn_fieldlist *fn_flp
6691 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6692 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6693 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6694 }
6695}
6696
a766d390
DE
6697/* Go objects should be embedded in a DW_TAG_module DIE,
6698 and it's not clear if/how imported objects will appear.
6699 To keep Go support simple until that's worked out,
6700 go back through what we've read and create something usable.
6701 We could do this while processing each DIE, and feels kinda cleaner,
6702 but that way is more invasive.
6703 This is to, for example, allow the user to type "p var" or "b main"
6704 without having to specify the package name, and allow lookups
6705 of module.object to work in contexts that use the expression
6706 parser. */
6707
6708static void
6709fixup_go_packaging (struct dwarf2_cu *cu)
6710{
6711 char *package_name = NULL;
6712 struct pending *list;
6713 int i;
6714
6715 for (list = global_symbols; list != NULL; list = list->next)
6716 {
6717 for (i = 0; i < list->nsyms; ++i)
6718 {
6719 struct symbol *sym = list->symbol[i];
6720
6721 if (SYMBOL_LANGUAGE (sym) == language_go
6722 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6723 {
6724 char *this_package_name = go_symbol_package_name (sym);
6725
6726 if (this_package_name == NULL)
6727 continue;
6728 if (package_name == NULL)
6729 package_name = this_package_name;
6730 else
6731 {
6732 if (strcmp (package_name, this_package_name) != 0)
6733 complaint (&symfile_complaints,
6734 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17
TT
6735 (SYMBOL_SYMTAB (sym)
6736 && SYMBOL_SYMTAB (sym)->filename
6737 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6738 : cu->objfile->name),
6739 this_package_name, package_name);
6740 xfree (this_package_name);
6741 }
6742 }
6743 }
6744 }
6745
6746 if (package_name != NULL)
6747 {
6748 struct objfile *objfile = cu->objfile;
6749 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6750 package_name, objfile);
6751 struct symbol *sym;
6752
6753 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6754
6755 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6756 SYMBOL_SET_LANGUAGE (sym, language_go);
6757 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6758 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6759 e.g., "main" finds the "main" module and not C's main(). */
6760 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6761 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6762 SYMBOL_TYPE (sym) = type;
6763
6764 add_symbol_to_list (sym, &global_symbols);
6765
6766 xfree (package_name);
6767 }
6768}
6769
95554aad
TT
6770static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6771
6772/* Return the symtab for PER_CU. This works properly regardless of
6773 whether we're using the index or psymtabs. */
6774
6775static struct symtab *
6776get_symtab (struct dwarf2_per_cu_data *per_cu)
6777{
6778 return (dwarf2_per_objfile->using_index
6779 ? per_cu->v.quick->symtab
6780 : per_cu->v.psymtab->symtab);
6781}
6782
6783/* A helper function for computing the list of all symbol tables
6784 included by PER_CU. */
6785
6786static void
6787recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6788 htab_t all_children,
6789 struct dwarf2_per_cu_data *per_cu)
6790{
6791 void **slot;
6792 int ix;
6793 struct dwarf2_per_cu_data *iter;
6794
6795 slot = htab_find_slot (all_children, per_cu, INSERT);
6796 if (*slot != NULL)
6797 {
6798 /* This inclusion and its children have been processed. */
6799 return;
6800 }
6801
6802 *slot = per_cu;
6803 /* Only add a CU if it has a symbol table. */
6804 if (get_symtab (per_cu) != NULL)
6805 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6806
6807 for (ix = 0;
f4dc4d17 6808 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6809 ++ix)
6810 recursively_compute_inclusions (result, all_children, iter);
6811}
6812
6813/* Compute the symtab 'includes' fields for the symtab related to
6814 PER_CU. */
6815
6816static void
6817compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6818{
f4dc4d17
DE
6819 gdb_assert (! per_cu->is_debug_types);
6820
6821 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6822 {
6823 int ix, len;
6824 struct dwarf2_per_cu_data *iter;
6825 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6826 htab_t all_children;
6827 struct symtab *symtab = get_symtab (per_cu);
6828
6829 /* If we don't have a symtab, we can just skip this case. */
6830 if (symtab == NULL)
6831 return;
6832
6833 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6834 NULL, xcalloc, xfree);
6835
6836 for (ix = 0;
f4dc4d17 6837 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6838 ix, iter);
6839 ++ix)
6840 recursively_compute_inclusions (&result_children, all_children, iter);
6841
6842 /* Now we have a transitive closure of all the included CUs, so
6843 we can convert it to a list of symtabs. */
6844 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6845 symtab->includes
6846 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6847 (len + 1) * sizeof (struct symtab *));
6848 for (ix = 0;
6849 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6850 ++ix)
6851 symtab->includes[ix] = get_symtab (iter);
6852 symtab->includes[len] = NULL;
6853
6854 VEC_free (dwarf2_per_cu_ptr, result_children);
6855 htab_delete (all_children);
6856 }
6857}
6858
6859/* Compute the 'includes' field for the symtabs of all the CUs we just
6860 read. */
6861
6862static void
6863process_cu_includes (void)
6864{
6865 int ix;
6866 struct dwarf2_per_cu_data *iter;
6867
6868 for (ix = 0;
6869 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6870 ix, iter);
6871 ++ix)
f4dc4d17
DE
6872 {
6873 if (! iter->is_debug_types)
6874 compute_symtab_includes (iter);
6875 }
95554aad
TT
6876
6877 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6878}
6879
9cdd5dbd 6880/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6881 already been loaded into memory. */
6882
6883static void
95554aad
TT
6884process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6885 enum language pretend_language)
10b3939b 6886{
10b3939b 6887 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6888 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6889 CORE_ADDR lowpc, highpc;
6890 struct symtab *symtab;
3da10d80 6891 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6892 CORE_ADDR baseaddr;
4359dff1 6893 struct block *static_block;
10b3939b
DJ
6894
6895 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6896
10b3939b
DJ
6897 buildsym_init ();
6898 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6899 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6900
6901 cu->list_in_scope = &file_symbols;
c906108c 6902
95554aad
TT
6903 cu->language = pretend_language;
6904 cu->language_defn = language_def (cu->language);
6905
c906108c 6906 /* Do line number decoding in read_file_scope () */
10b3939b 6907 process_die (cu->dies, cu);
c906108c 6908
a766d390
DE
6909 /* For now fudge the Go package. */
6910 if (cu->language == language_go)
6911 fixup_go_packaging (cu);
6912
3da10d80
KS
6913 /* Now that we have processed all the DIEs in the CU, all the types
6914 should be complete, and it should now be safe to compute all of the
6915 physnames. */
6916 compute_delayed_physnames (cu);
6917 do_cleanups (delayed_list_cleanup);
6918
fae299cd
DC
6919 /* Some compilers don't define a DW_AT_high_pc attribute for the
6920 compilation unit. If the DW_AT_high_pc is missing, synthesize
6921 it, by scanning the DIE's below the compilation unit. */
10b3939b 6922 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6923
36586728
TT
6924 static_block
6925 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6926 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6927
6928 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6929 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6930 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6931 addrmap to help ensure it has an accurate map of pc values belonging to
6932 this comp unit. */
6933 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6934
6935 symtab = end_symtab_from_static_block (static_block, objfile,
6936 SECT_OFF_TEXT (objfile), 0);
c906108c 6937
8be455d7 6938 if (symtab != NULL)
c906108c 6939 {
df15bd07 6940 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6941
8be455d7
JK
6942 /* Set symtab language to language from DW_AT_language. If the
6943 compilation is from a C file generated by language preprocessors, do
6944 not set the language if it was already deduced by start_subfile. */
6945 if (!(cu->language == language_c && symtab->language != language_c))
6946 symtab->language = cu->language;
6947
6948 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6949 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6950 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6951 there were bugs in prologue debug info, fixed later in GCC-4.5
6952 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6953
6954 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6955 needed, it would be wrong due to missing DW_AT_producer there.
6956
6957 Still one can confuse GDB by using non-standard GCC compilation
6958 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6959 */
ab260dad 6960 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6961 symtab->locations_valid = 1;
e0d00bc7
JK
6962
6963 if (gcc_4_minor >= 5)
6964 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6965
6966 symtab->call_site_htab = cu->call_site_htab;
c906108c 6967 }
9291a0cd
TT
6968
6969 if (dwarf2_per_objfile->using_index)
6970 per_cu->v.quick->symtab = symtab;
6971 else
6972 {
6973 struct partial_symtab *pst = per_cu->v.psymtab;
6974 pst->symtab = symtab;
6975 pst->readin = 1;
6976 }
c906108c 6977
95554aad
TT
6978 /* Push it for inclusion processing later. */
6979 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6980
c906108c 6981 do_cleanups (back_to);
f4dc4d17 6982}
45cfd468 6983
f4dc4d17
DE
6984/* Generate full symbol information for type unit PER_CU, whose DIEs have
6985 already been loaded into memory. */
6986
6987static void
6988process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6989 enum language pretend_language)
6990{
6991 struct dwarf2_cu *cu = per_cu->cu;
6992 struct objfile *objfile = per_cu->objfile;
6993 struct symtab *symtab;
6994 struct cleanup *back_to, *delayed_list_cleanup;
6995
6996 buildsym_init ();
6997 back_to = make_cleanup (really_free_pendings, NULL);
6998 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6999
7000 cu->list_in_scope = &file_symbols;
7001
7002 cu->language = pretend_language;
7003 cu->language_defn = language_def (cu->language);
7004
7005 /* The symbol tables are set up in read_type_unit_scope. */
7006 process_die (cu->dies, cu);
7007
7008 /* For now fudge the Go package. */
7009 if (cu->language == language_go)
7010 fixup_go_packaging (cu);
7011
7012 /* Now that we have processed all the DIEs in the CU, all the types
7013 should be complete, and it should now be safe to compute all of the
7014 physnames. */
7015 compute_delayed_physnames (cu);
7016 do_cleanups (delayed_list_cleanup);
7017
7018 /* TUs share symbol tables.
7019 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7020 of it with end_expandable_symtab. Otherwise, complete the addition of
7021 this TU's symbols to the existing symtab. */
f4dc4d17 7022 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7023 {
f4dc4d17
DE
7024 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7025 per_cu->s.type_unit_group->primary_symtab = symtab;
7026
7027 if (symtab != NULL)
7028 {
7029 /* Set symtab language to language from DW_AT_language. If the
7030 compilation is from a C file generated by language preprocessors,
7031 do not set the language if it was already deduced by
7032 start_subfile. */
7033 if (!(cu->language == language_c && symtab->language != language_c))
7034 symtab->language = cu->language;
7035 }
7036 }
7037 else
7038 {
7039 augment_type_symtab (objfile,
7040 per_cu->s.type_unit_group->primary_symtab);
7041 symtab = per_cu->s.type_unit_group->primary_symtab;
7042 }
7043
7044 if (dwarf2_per_objfile->using_index)
7045 per_cu->v.quick->symtab = symtab;
7046 else
7047 {
7048 struct partial_symtab *pst = per_cu->v.psymtab;
7049 pst->symtab = symtab;
7050 pst->readin = 1;
45cfd468 7051 }
f4dc4d17
DE
7052
7053 do_cleanups (back_to);
c906108c
SS
7054}
7055
95554aad
TT
7056/* Process an imported unit DIE. */
7057
7058static void
7059process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7060{
7061 struct attribute *attr;
7062
f4dc4d17
DE
7063 /* For now we don't handle imported units in type units. */
7064 if (cu->per_cu->is_debug_types)
7065 {
7066 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7067 " supported in type units [in module %s]"),
7068 cu->objfile->name);
7069 }
7070
95554aad
TT
7071 attr = dwarf2_attr (die, DW_AT_import, cu);
7072 if (attr != NULL)
7073 {
7074 struct dwarf2_per_cu_data *per_cu;
7075 struct symtab *imported_symtab;
7076 sect_offset offset;
36586728 7077 int is_dwz;
95554aad
TT
7078
7079 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7080 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7081 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7082
7083 /* Queue the unit, if needed. */
7084 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7085 load_full_comp_unit (per_cu, cu->language);
7086
f4dc4d17 7087 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7088 per_cu);
7089 }
7090}
7091
c906108c
SS
7092/* Process a die and its children. */
7093
7094static void
e7c27a73 7095process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7096{
7097 switch (die->tag)
7098 {
7099 case DW_TAG_padding:
7100 break;
7101 case DW_TAG_compile_unit:
95554aad 7102 case DW_TAG_partial_unit:
e7c27a73 7103 read_file_scope (die, cu);
c906108c 7104 break;
348e048f
DE
7105 case DW_TAG_type_unit:
7106 read_type_unit_scope (die, cu);
7107 break;
c906108c 7108 case DW_TAG_subprogram:
c906108c 7109 case DW_TAG_inlined_subroutine:
edb3359d 7110 read_func_scope (die, cu);
c906108c
SS
7111 break;
7112 case DW_TAG_lexical_block:
14898363
L
7113 case DW_TAG_try_block:
7114 case DW_TAG_catch_block:
e7c27a73 7115 read_lexical_block_scope (die, cu);
c906108c 7116 break;
96408a79
SA
7117 case DW_TAG_GNU_call_site:
7118 read_call_site_scope (die, cu);
7119 break;
c906108c 7120 case DW_TAG_class_type:
680b30c7 7121 case DW_TAG_interface_type:
c906108c
SS
7122 case DW_TAG_structure_type:
7123 case DW_TAG_union_type:
134d01f1 7124 process_structure_scope (die, cu);
c906108c
SS
7125 break;
7126 case DW_TAG_enumeration_type:
134d01f1 7127 process_enumeration_scope (die, cu);
c906108c 7128 break;
134d01f1 7129
f792889a
DJ
7130 /* These dies have a type, but processing them does not create
7131 a symbol or recurse to process the children. Therefore we can
7132 read them on-demand through read_type_die. */
c906108c 7133 case DW_TAG_subroutine_type:
72019c9c 7134 case DW_TAG_set_type:
c906108c 7135 case DW_TAG_array_type:
c906108c 7136 case DW_TAG_pointer_type:
c906108c 7137 case DW_TAG_ptr_to_member_type:
c906108c 7138 case DW_TAG_reference_type:
c906108c 7139 case DW_TAG_string_type:
c906108c 7140 break;
134d01f1 7141
c906108c 7142 case DW_TAG_base_type:
a02abb62 7143 case DW_TAG_subrange_type:
cb249c71 7144 case DW_TAG_typedef:
134d01f1
DJ
7145 /* Add a typedef symbol for the type definition, if it has a
7146 DW_AT_name. */
f792889a 7147 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7148 break;
c906108c 7149 case DW_TAG_common_block:
e7c27a73 7150 read_common_block (die, cu);
c906108c
SS
7151 break;
7152 case DW_TAG_common_inclusion:
7153 break;
d9fa45fe 7154 case DW_TAG_namespace:
63d06c5c 7155 processing_has_namespace_info = 1;
e7c27a73 7156 read_namespace (die, cu);
d9fa45fe 7157 break;
5d7cb8df 7158 case DW_TAG_module:
f55ee35c 7159 processing_has_namespace_info = 1;
5d7cb8df
JK
7160 read_module (die, cu);
7161 break;
d9fa45fe
DC
7162 case DW_TAG_imported_declaration:
7163 case DW_TAG_imported_module:
63d06c5c 7164 processing_has_namespace_info = 1;
27aa8d6a
SW
7165 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7166 || cu->language != language_fortran))
7167 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7168 dwarf_tag_name (die->tag));
7169 read_import_statement (die, cu);
d9fa45fe 7170 break;
95554aad
TT
7171
7172 case DW_TAG_imported_unit:
7173 process_imported_unit_die (die, cu);
7174 break;
7175
c906108c 7176 default:
e7c27a73 7177 new_symbol (die, NULL, cu);
c906108c
SS
7178 break;
7179 }
7180}
7181
94af9270
KS
7182/* A helper function for dwarf2_compute_name which determines whether DIE
7183 needs to have the name of the scope prepended to the name listed in the
7184 die. */
7185
7186static int
7187die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7188{
1c809c68
TT
7189 struct attribute *attr;
7190
94af9270
KS
7191 switch (die->tag)
7192 {
7193 case DW_TAG_namespace:
7194 case DW_TAG_typedef:
7195 case DW_TAG_class_type:
7196 case DW_TAG_interface_type:
7197 case DW_TAG_structure_type:
7198 case DW_TAG_union_type:
7199 case DW_TAG_enumeration_type:
7200 case DW_TAG_enumerator:
7201 case DW_TAG_subprogram:
7202 case DW_TAG_member:
7203 return 1;
7204
7205 case DW_TAG_variable:
c2b0a229 7206 case DW_TAG_constant:
94af9270
KS
7207 /* We only need to prefix "globally" visible variables. These include
7208 any variable marked with DW_AT_external or any variable that
7209 lives in a namespace. [Variables in anonymous namespaces
7210 require prefixing, but they are not DW_AT_external.] */
7211
7212 if (dwarf2_attr (die, DW_AT_specification, cu))
7213 {
7214 struct dwarf2_cu *spec_cu = cu;
9a619af0 7215
94af9270
KS
7216 return die_needs_namespace (die_specification (die, &spec_cu),
7217 spec_cu);
7218 }
7219
1c809c68 7220 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7221 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7222 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7223 return 0;
7224 /* A variable in a lexical block of some kind does not need a
7225 namespace, even though in C++ such variables may be external
7226 and have a mangled name. */
7227 if (die->parent->tag == DW_TAG_lexical_block
7228 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7229 || die->parent->tag == DW_TAG_catch_block
7230 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7231 return 0;
7232 return 1;
94af9270
KS
7233
7234 default:
7235 return 0;
7236 }
7237}
7238
98bfdba5
PA
7239/* Retrieve the last character from a mem_file. */
7240
7241static void
7242do_ui_file_peek_last (void *object, const char *buffer, long length)
7243{
7244 char *last_char_p = (char *) object;
7245
7246 if (length > 0)
7247 *last_char_p = buffer[length - 1];
7248}
7249
94af9270 7250/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7251 compute the physname for the object, which include a method's:
7252 - formal parameters (C++/Java),
7253 - receiver type (Go),
7254 - return type (Java).
7255
7256 The term "physname" is a bit confusing.
7257 For C++, for example, it is the demangled name.
7258 For Go, for example, it's the mangled name.
94af9270 7259
af6b7be1
JB
7260 For Ada, return the DIE's linkage name rather than the fully qualified
7261 name. PHYSNAME is ignored..
7262
94af9270
KS
7263 The result is allocated on the objfile_obstack and canonicalized. */
7264
7265static const char *
7266dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7267 int physname)
7268{
bb5ed363
DE
7269 struct objfile *objfile = cu->objfile;
7270
94af9270
KS
7271 if (name == NULL)
7272 name = dwarf2_name (die, cu);
7273
f55ee35c
JK
7274 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7275 compute it by typename_concat inside GDB. */
7276 if (cu->language == language_ada
7277 || (cu->language == language_fortran && physname))
7278 {
7279 /* For Ada unit, we prefer the linkage name over the name, as
7280 the former contains the exported name, which the user expects
7281 to be able to reference. Ideally, we want the user to be able
7282 to reference this entity using either natural or linkage name,
7283 but we haven't started looking at this enhancement yet. */
7284 struct attribute *attr;
7285
7286 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7287 if (attr == NULL)
7288 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7289 if (attr && DW_STRING (attr))
7290 return DW_STRING (attr);
7291 }
7292
94af9270
KS
7293 /* These are the only languages we know how to qualify names in. */
7294 if (name != NULL
f55ee35c
JK
7295 && (cu->language == language_cplus || cu->language == language_java
7296 || cu->language == language_fortran))
94af9270
KS
7297 {
7298 if (die_needs_namespace (die, cu))
7299 {
7300 long length;
0d5cff50 7301 const char *prefix;
94af9270
KS
7302 struct ui_file *buf;
7303
7304 prefix = determine_prefix (die, cu);
7305 buf = mem_fileopen ();
7306 if (*prefix != '\0')
7307 {
f55ee35c
JK
7308 char *prefixed_name = typename_concat (NULL, prefix, name,
7309 physname, cu);
9a619af0 7310
94af9270
KS
7311 fputs_unfiltered (prefixed_name, buf);
7312 xfree (prefixed_name);
7313 }
7314 else
62d5b8da 7315 fputs_unfiltered (name, buf);
94af9270 7316
98bfdba5
PA
7317 /* Template parameters may be specified in the DIE's DW_AT_name, or
7318 as children with DW_TAG_template_type_param or
7319 DW_TAG_value_type_param. If the latter, add them to the name
7320 here. If the name already has template parameters, then
7321 skip this step; some versions of GCC emit both, and
7322 it is more efficient to use the pre-computed name.
7323
7324 Something to keep in mind about this process: it is very
7325 unlikely, or in some cases downright impossible, to produce
7326 something that will match the mangled name of a function.
7327 If the definition of the function has the same debug info,
7328 we should be able to match up with it anyway. But fallbacks
7329 using the minimal symbol, for instance to find a method
7330 implemented in a stripped copy of libstdc++, will not work.
7331 If we do not have debug info for the definition, we will have to
7332 match them up some other way.
7333
7334 When we do name matching there is a related problem with function
7335 templates; two instantiated function templates are allowed to
7336 differ only by their return types, which we do not add here. */
7337
7338 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7339 {
7340 struct attribute *attr;
7341 struct die_info *child;
7342 int first = 1;
7343
7344 die->building_fullname = 1;
7345
7346 for (child = die->child; child != NULL; child = child->sibling)
7347 {
7348 struct type *type;
12df843f 7349 LONGEST value;
98bfdba5
PA
7350 gdb_byte *bytes;
7351 struct dwarf2_locexpr_baton *baton;
7352 struct value *v;
7353
7354 if (child->tag != DW_TAG_template_type_param
7355 && child->tag != DW_TAG_template_value_param)
7356 continue;
7357
7358 if (first)
7359 {
7360 fputs_unfiltered ("<", buf);
7361 first = 0;
7362 }
7363 else
7364 fputs_unfiltered (", ", buf);
7365
7366 attr = dwarf2_attr (child, DW_AT_type, cu);
7367 if (attr == NULL)
7368 {
7369 complaint (&symfile_complaints,
7370 _("template parameter missing DW_AT_type"));
7371 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7372 continue;
7373 }
7374 type = die_type (child, cu);
7375
7376 if (child->tag == DW_TAG_template_type_param)
7377 {
79d43c61 7378 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7379 continue;
7380 }
7381
7382 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7383 if (attr == NULL)
7384 {
7385 complaint (&symfile_complaints,
3e43a32a
MS
7386 _("template parameter missing "
7387 "DW_AT_const_value"));
98bfdba5
PA
7388 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7389 continue;
7390 }
7391
7392 dwarf2_const_value_attr (attr, type, name,
7393 &cu->comp_unit_obstack, cu,
7394 &value, &bytes, &baton);
7395
7396 if (TYPE_NOSIGN (type))
7397 /* GDB prints characters as NUMBER 'CHAR'. If that's
7398 changed, this can use value_print instead. */
7399 c_printchar (value, type, buf);
7400 else
7401 {
7402 struct value_print_options opts;
7403
7404 if (baton != NULL)
7405 v = dwarf2_evaluate_loc_desc (type, NULL,
7406 baton->data,
7407 baton->size,
7408 baton->per_cu);
7409 else if (bytes != NULL)
7410 {
7411 v = allocate_value (type);
7412 memcpy (value_contents_writeable (v), bytes,
7413 TYPE_LENGTH (type));
7414 }
7415 else
7416 v = value_from_longest (type, value);
7417
3e43a32a
MS
7418 /* Specify decimal so that we do not depend on
7419 the radix. */
98bfdba5
PA
7420 get_formatted_print_options (&opts, 'd');
7421 opts.raw = 1;
7422 value_print (v, buf, &opts);
7423 release_value (v);
7424 value_free (v);
7425 }
7426 }
7427
7428 die->building_fullname = 0;
7429
7430 if (!first)
7431 {
7432 /* Close the argument list, with a space if necessary
7433 (nested templates). */
7434 char last_char = '\0';
7435 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7436 if (last_char == '>')
7437 fputs_unfiltered (" >", buf);
7438 else
7439 fputs_unfiltered (">", buf);
7440 }
7441 }
7442
94af9270
KS
7443 /* For Java and C++ methods, append formal parameter type
7444 information, if PHYSNAME. */
6e70227d 7445
94af9270
KS
7446 if (physname && die->tag == DW_TAG_subprogram
7447 && (cu->language == language_cplus
7448 || cu->language == language_java))
7449 {
7450 struct type *type = read_type_die (die, cu);
7451
79d43c61
TT
7452 c_type_print_args (type, buf, 1, cu->language,
7453 &type_print_raw_options);
94af9270
KS
7454
7455 if (cu->language == language_java)
7456 {
7457 /* For java, we must append the return type to method
0963b4bd 7458 names. */
94af9270
KS
7459 if (die->tag == DW_TAG_subprogram)
7460 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7461 0, 0, &type_print_raw_options);
94af9270
KS
7462 }
7463 else if (cu->language == language_cplus)
7464 {
60430eff
DJ
7465 /* Assume that an artificial first parameter is
7466 "this", but do not crash if it is not. RealView
7467 marks unnamed (and thus unused) parameters as
7468 artificial; there is no way to differentiate
7469 the two cases. */
94af9270
KS
7470 if (TYPE_NFIELDS (type) > 0
7471 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7472 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7473 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7474 0))))
94af9270
KS
7475 fputs_unfiltered (" const", buf);
7476 }
7477 }
7478
bb5ed363 7479 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7480 &length);
7481 ui_file_delete (buf);
7482
7483 if (cu->language == language_cplus)
7484 {
7485 char *cname
7486 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7487 &objfile->objfile_obstack);
9a619af0 7488
94af9270
KS
7489 if (cname != NULL)
7490 name = cname;
7491 }
7492 }
7493 }
7494
7495 return name;
7496}
7497
0114d602
DJ
7498/* Return the fully qualified name of DIE, based on its DW_AT_name.
7499 If scope qualifiers are appropriate they will be added. The result
7500 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7501 not have a name. NAME may either be from a previous call to
7502 dwarf2_name or NULL.
7503
0963b4bd 7504 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7505
7506static const char *
94af9270 7507dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7508{
94af9270
KS
7509 return dwarf2_compute_name (name, die, cu, 0);
7510}
0114d602 7511
94af9270
KS
7512/* Construct a physname for the given DIE in CU. NAME may either be
7513 from a previous call to dwarf2_name or NULL. The result will be
7514 allocated on the objfile_objstack or NULL if the DIE does not have a
7515 name.
0114d602 7516
94af9270 7517 The output string will be canonicalized (if C++/Java). */
0114d602 7518
94af9270
KS
7519static const char *
7520dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7521{
bb5ed363 7522 struct objfile *objfile = cu->objfile;
900e11f9
JK
7523 struct attribute *attr;
7524 const char *retval, *mangled = NULL, *canon = NULL;
7525 struct cleanup *back_to;
7526 int need_copy = 1;
7527
7528 /* In this case dwarf2_compute_name is just a shortcut not building anything
7529 on its own. */
7530 if (!die_needs_namespace (die, cu))
7531 return dwarf2_compute_name (name, die, cu, 1);
7532
7533 back_to = make_cleanup (null_cleanup, NULL);
7534
7535 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7536 if (!attr)
7537 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7538
7539 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7540 has computed. */
7541 if (attr && DW_STRING (attr))
7542 {
7543 char *demangled;
7544
7545 mangled = DW_STRING (attr);
7546
7547 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7548 type. It is easier for GDB users to search for such functions as
7549 `name(params)' than `long name(params)'. In such case the minimal
7550 symbol names do not match the full symbol names but for template
7551 functions there is never a need to look up their definition from their
7552 declaration so the only disadvantage remains the minimal symbol
7553 variant `long name(params)' does not have the proper inferior type.
7554 */
7555
a766d390
DE
7556 if (cu->language == language_go)
7557 {
7558 /* This is a lie, but we already lie to the caller new_symbol_full.
7559 new_symbol_full assumes we return the mangled name.
7560 This just undoes that lie until things are cleaned up. */
7561 demangled = NULL;
7562 }
7563 else
7564 {
7565 demangled = cplus_demangle (mangled,
7566 (DMGL_PARAMS | DMGL_ANSI
7567 | (cu->language == language_java
7568 ? DMGL_JAVA | DMGL_RET_POSTFIX
7569 : DMGL_RET_DROP)));
7570 }
900e11f9
JK
7571 if (demangled)
7572 {
7573 make_cleanup (xfree, demangled);
7574 canon = demangled;
7575 }
7576 else
7577 {
7578 canon = mangled;
7579 need_copy = 0;
7580 }
7581 }
7582
7583 if (canon == NULL || check_physname)
7584 {
7585 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7586
7587 if (canon != NULL && strcmp (physname, canon) != 0)
7588 {
7589 /* It may not mean a bug in GDB. The compiler could also
7590 compute DW_AT_linkage_name incorrectly. But in such case
7591 GDB would need to be bug-to-bug compatible. */
7592
7593 complaint (&symfile_complaints,
7594 _("Computed physname <%s> does not match demangled <%s> "
7595 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7596 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7597
7598 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7599 is available here - over computed PHYSNAME. It is safer
7600 against both buggy GDB and buggy compilers. */
7601
7602 retval = canon;
7603 }
7604 else
7605 {
7606 retval = physname;
7607 need_copy = 0;
7608 }
7609 }
7610 else
7611 retval = canon;
7612
7613 if (need_copy)
7614 retval = obsavestring (retval, strlen (retval),
bb5ed363 7615 &objfile->objfile_obstack);
900e11f9
JK
7616
7617 do_cleanups (back_to);
7618 return retval;
0114d602
DJ
7619}
7620
27aa8d6a
SW
7621/* Read the import statement specified by the given die and record it. */
7622
7623static void
7624read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7625{
bb5ed363 7626 struct objfile *objfile = cu->objfile;
27aa8d6a 7627 struct attribute *import_attr;
32019081 7628 struct die_info *imported_die, *child_die;
de4affc9 7629 struct dwarf2_cu *imported_cu;
27aa8d6a 7630 const char *imported_name;
794684b6 7631 const char *imported_name_prefix;
13387711
SW
7632 const char *canonical_name;
7633 const char *import_alias;
7634 const char *imported_declaration = NULL;
794684b6 7635 const char *import_prefix;
32019081
JK
7636 VEC (const_char_ptr) *excludes = NULL;
7637 struct cleanup *cleanups;
13387711
SW
7638
7639 char *temp;
27aa8d6a
SW
7640
7641 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7642 if (import_attr == NULL)
7643 {
7644 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7645 dwarf_tag_name (die->tag));
7646 return;
7647 }
7648
de4affc9
CC
7649 imported_cu = cu;
7650 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7651 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7652 if (imported_name == NULL)
7653 {
7654 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7655
7656 The import in the following code:
7657 namespace A
7658 {
7659 typedef int B;
7660 }
7661
7662 int main ()
7663 {
7664 using A::B;
7665 B b;
7666 return b;
7667 }
7668
7669 ...
7670 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7671 <52> DW_AT_decl_file : 1
7672 <53> DW_AT_decl_line : 6
7673 <54> DW_AT_import : <0x75>
7674 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7675 <59> DW_AT_name : B
7676 <5b> DW_AT_decl_file : 1
7677 <5c> DW_AT_decl_line : 2
7678 <5d> DW_AT_type : <0x6e>
7679 ...
7680 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7681 <76> DW_AT_byte_size : 4
7682 <77> DW_AT_encoding : 5 (signed)
7683
7684 imports the wrong die ( 0x75 instead of 0x58 ).
7685 This case will be ignored until the gcc bug is fixed. */
7686 return;
7687 }
7688
82856980
SW
7689 /* Figure out the local name after import. */
7690 import_alias = dwarf2_name (die, cu);
27aa8d6a 7691
794684b6
SW
7692 /* Figure out where the statement is being imported to. */
7693 import_prefix = determine_prefix (die, cu);
7694
7695 /* Figure out what the scope of the imported die is and prepend it
7696 to the name of the imported die. */
de4affc9 7697 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7698
f55ee35c
JK
7699 if (imported_die->tag != DW_TAG_namespace
7700 && imported_die->tag != DW_TAG_module)
794684b6 7701 {
13387711
SW
7702 imported_declaration = imported_name;
7703 canonical_name = imported_name_prefix;
794684b6 7704 }
13387711 7705 else if (strlen (imported_name_prefix) > 0)
794684b6 7706 {
13387711
SW
7707 temp = alloca (strlen (imported_name_prefix)
7708 + 2 + strlen (imported_name) + 1);
7709 strcpy (temp, imported_name_prefix);
7710 strcat (temp, "::");
7711 strcat (temp, imported_name);
7712 canonical_name = temp;
794684b6 7713 }
13387711
SW
7714 else
7715 canonical_name = imported_name;
794684b6 7716
32019081
JK
7717 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7718
7719 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7720 for (child_die = die->child; child_die && child_die->tag;
7721 child_die = sibling_die (child_die))
7722 {
7723 /* DWARF-4: A Fortran use statement with a “rename list” may be
7724 represented by an imported module entry with an import attribute
7725 referring to the module and owned entries corresponding to those
7726 entities that are renamed as part of being imported. */
7727
7728 if (child_die->tag != DW_TAG_imported_declaration)
7729 {
7730 complaint (&symfile_complaints,
7731 _("child DW_TAG_imported_declaration expected "
7732 "- DIE at 0x%x [in module %s]"),
b64f50a1 7733 child_die->offset.sect_off, objfile->name);
32019081
JK
7734 continue;
7735 }
7736
7737 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7738 if (import_attr == NULL)
7739 {
7740 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7741 dwarf_tag_name (child_die->tag));
7742 continue;
7743 }
7744
7745 imported_cu = cu;
7746 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7747 &imported_cu);
7748 imported_name = dwarf2_name (imported_die, imported_cu);
7749 if (imported_name == NULL)
7750 {
7751 complaint (&symfile_complaints,
7752 _("child DW_TAG_imported_declaration has unknown "
7753 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7754 child_die->offset.sect_off, objfile->name);
32019081
JK
7755 continue;
7756 }
7757
7758 VEC_safe_push (const_char_ptr, excludes, imported_name);
7759
7760 process_die (child_die, cu);
7761 }
7762
c0cc3a76
SW
7763 cp_add_using_directive (import_prefix,
7764 canonical_name,
7765 import_alias,
13387711 7766 imported_declaration,
32019081 7767 excludes,
bb5ed363 7768 &objfile->objfile_obstack);
32019081
JK
7769
7770 do_cleanups (cleanups);
27aa8d6a
SW
7771}
7772
f4dc4d17 7773/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7774
cb1df416
DJ
7775static void
7776free_cu_line_header (void *arg)
7777{
7778 struct dwarf2_cu *cu = arg;
7779
7780 free_line_header (cu->line_header);
7781 cu->line_header = NULL;
7782}
7783
1b80a9fa
JK
7784/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7785 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7786 this, it was first present in GCC release 4.3.0. */
7787
7788static int
7789producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7790{
7791 if (!cu->checked_producer)
7792 check_producer (cu);
7793
7794 return cu->producer_is_gcc_lt_4_3;
7795}
7796
9291a0cd
TT
7797static void
7798find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7799 char **name, char **comp_dir)
7800{
7801 struct attribute *attr;
7802
7803 *name = NULL;
7804 *comp_dir = NULL;
7805
7806 /* Find the filename. Do not use dwarf2_name here, since the filename
7807 is not a source language identifier. */
7808 attr = dwarf2_attr (die, DW_AT_name, cu);
7809 if (attr)
7810 {
7811 *name = DW_STRING (attr);
7812 }
7813
7814 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7815 if (attr)
7816 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7817 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7818 && IS_ABSOLUTE_PATH (*name))
9291a0cd
TT
7819 {
7820 *comp_dir = ldirname (*name);
7821 if (*comp_dir != NULL)
7822 make_cleanup (xfree, *comp_dir);
7823 }
7824 if (*comp_dir != NULL)
7825 {
7826 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7827 directory, get rid of it. */
7828 char *cp = strchr (*comp_dir, ':');
7829
7830 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7831 *comp_dir = cp + 1;
7832 }
7833
7834 if (*name == NULL)
7835 *name = "<unknown>";
7836}
7837
f4dc4d17
DE
7838/* Handle DW_AT_stmt_list for a compilation unit.
7839 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7840 COMP_DIR is the compilation directory.
7841 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7842
7843static void
7844handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7845 const char *comp_dir)
2ab95328
TT
7846{
7847 struct attribute *attr;
2ab95328 7848
f4dc4d17
DE
7849 gdb_assert (! cu->per_cu->is_debug_types);
7850
2ab95328
TT
7851 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7852 if (attr)
7853 {
7854 unsigned int line_offset = DW_UNSND (attr);
7855 struct line_header *line_header
3019eac3 7856 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7857
7858 if (line_header)
dee91e82
DE
7859 {
7860 cu->line_header = line_header;
7861 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7862 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7863 }
2ab95328
TT
7864 }
7865}
7866
95554aad 7867/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7868
c906108c 7869static void
e7c27a73 7870read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7871{
dee91e82 7872 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7873 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7874 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7875 CORE_ADDR highpc = ((CORE_ADDR) 0);
7876 struct attribute *attr;
e1024ff1 7877 char *name = NULL;
c906108c
SS
7878 char *comp_dir = NULL;
7879 struct die_info *child_die;
7880 bfd *abfd = objfile->obfd;
e142c38c 7881 CORE_ADDR baseaddr;
6e70227d 7882
e142c38c 7883 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7884
fae299cd 7885 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7886
7887 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7888 from finish_block. */
2acceee2 7889 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7890 lowpc = highpc;
7891 lowpc += baseaddr;
7892 highpc += baseaddr;
7893
9291a0cd 7894 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7895
95554aad 7896 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7897
f4b8a18d
KW
7898 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7899 standardised yet. As a workaround for the language detection we fall
7900 back to the DW_AT_producer string. */
7901 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7902 cu->language = language_opencl;
7903
3019eac3
DE
7904 /* Similar hack for Go. */
7905 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7906 set_cu_language (DW_LANG_Go, cu);
7907
f4dc4d17 7908 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7909
7910 /* Decode line number information if present. We do this before
7911 processing child DIEs, so that the line header table is available
7912 for DW_AT_decl_file. */
f4dc4d17 7913 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7914
7915 /* Process all dies in compilation unit. */
7916 if (die->child != NULL)
7917 {
7918 child_die = die->child;
7919 while (child_die && child_die->tag)
7920 {
7921 process_die (child_die, cu);
7922 child_die = sibling_die (child_die);
7923 }
7924 }
7925
7926 /* Decode macro information, if present. Dwarf 2 macro information
7927 refers to information in the line number info statement program
7928 header, so we can only read it if we've read the header
7929 successfully. */
7930 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7931 if (attr && cu->line_header)
7932 {
7933 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7934 complaint (&symfile_complaints,
7935 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7936
09262596 7937 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7938 }
7939 else
7940 {
7941 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7942 if (attr && cu->line_header)
7943 {
7944 unsigned int macro_offset = DW_UNSND (attr);
7945
09262596 7946 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7947 }
7948 }
7949
7950 do_cleanups (back_to);
7951}
7952
f4dc4d17
DE
7953/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7954 Create the set of symtabs used by this TU, or if this TU is sharing
7955 symtabs with another TU and the symtabs have already been created
7956 then restore those symtabs in the line header.
7957 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7958
7959static void
f4dc4d17 7960setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7961{
f4dc4d17
DE
7962 struct objfile *objfile = dwarf2_per_objfile->objfile;
7963 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7964 struct type_unit_group *tu_group;
7965 int first_time;
7966 struct line_header *lh;
3019eac3 7967 struct attribute *attr;
f4dc4d17 7968 unsigned int i, line_offset;
3019eac3 7969
f4dc4d17 7970 gdb_assert (per_cu->is_debug_types);
3019eac3 7971
f4dc4d17 7972 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 7973
f4dc4d17
DE
7974 /* If we're using .gdb_index (includes -readnow) then
7975 per_cu->s.type_unit_group may not have been set up yet. */
7976 if (per_cu->s.type_unit_group == NULL)
094b34ac 7977 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
7978 tu_group = per_cu->s.type_unit_group;
7979
7980 /* If we've already processed this stmt_list there's no real need to
7981 do it again, we could fake it and just recreate the part we need
7982 (file name,index -> symtab mapping). If data shows this optimization
7983 is useful we can do it then. */
7984 first_time = tu_group->primary_symtab == NULL;
7985
7986 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7987 debug info. */
7988 lh = NULL;
7989 if (attr != NULL)
3019eac3 7990 {
f4dc4d17
DE
7991 line_offset = DW_UNSND (attr);
7992 lh = dwarf_decode_line_header (line_offset, cu);
7993 }
7994 if (lh == NULL)
7995 {
7996 if (first_time)
7997 dwarf2_start_symtab (cu, "", NULL, 0);
7998 else
7999 {
8000 gdb_assert (tu_group->symtabs == NULL);
8001 restart_symtab (0);
8002 }
8003 /* Note: The primary symtab will get allocated at the end. */
8004 return;
3019eac3
DE
8005 }
8006
f4dc4d17
DE
8007 cu->line_header = lh;
8008 make_cleanup (free_cu_line_header, cu);
3019eac3 8009
f4dc4d17
DE
8010 if (first_time)
8011 {
8012 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8013
f4dc4d17
DE
8014 tu_group->num_symtabs = lh->num_file_names;
8015 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8016
f4dc4d17
DE
8017 for (i = 0; i < lh->num_file_names; ++i)
8018 {
8019 char *dir = NULL;
8020 struct file_entry *fe = &lh->file_names[i];
3019eac3 8021
f4dc4d17
DE
8022 if (fe->dir_index)
8023 dir = lh->include_dirs[fe->dir_index - 1];
8024 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8025
f4dc4d17
DE
8026 /* Note: We don't have to watch for the main subfile here, type units
8027 don't have DW_AT_name. */
3019eac3 8028
f4dc4d17
DE
8029 if (current_subfile->symtab == NULL)
8030 {
8031 /* NOTE: start_subfile will recognize when it's been passed
8032 a file it has already seen. So we can't assume there's a
8033 simple mapping from lh->file_names to subfiles,
8034 lh->file_names may contain dups. */
8035 current_subfile->symtab = allocate_symtab (current_subfile->name,
8036 objfile);
8037 }
8038
8039 fe->symtab = current_subfile->symtab;
8040 tu_group->symtabs[i] = fe->symtab;
8041 }
8042 }
8043 else
3019eac3 8044 {
f4dc4d17
DE
8045 restart_symtab (0);
8046
8047 for (i = 0; i < lh->num_file_names; ++i)
8048 {
8049 struct file_entry *fe = &lh->file_names[i];
8050
8051 fe->symtab = tu_group->symtabs[i];
8052 }
3019eac3
DE
8053 }
8054
f4dc4d17
DE
8055 /* The main symtab is allocated last. Type units don't have DW_AT_name
8056 so they don't have a "real" (so to speak) symtab anyway.
8057 There is later code that will assign the main symtab to all symbols
8058 that don't have one. We need to handle the case of a symbol with a
8059 missing symtab (DW_AT_decl_file) anyway. */
8060}
3019eac3 8061
f4dc4d17
DE
8062/* Process DW_TAG_type_unit.
8063 For TUs we want to skip the first top level sibling if it's not the
8064 actual type being defined by this TU. In this case the first top
8065 level sibling is there to provide context only. */
3019eac3 8066
f4dc4d17
DE
8067static void
8068read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8069{
8070 struct die_info *child_die;
3019eac3 8071
f4dc4d17
DE
8072 prepare_one_comp_unit (cu, die, language_minimal);
8073
8074 /* Initialize (or reinitialize) the machinery for building symtabs.
8075 We do this before processing child DIEs, so that the line header table
8076 is available for DW_AT_decl_file. */
8077 setup_type_unit_groups (die, cu);
8078
8079 if (die->child != NULL)
8080 {
8081 child_die = die->child;
8082 while (child_die && child_die->tag)
8083 {
8084 process_die (child_die, cu);
8085 child_die = sibling_die (child_die);
8086 }
8087 }
3019eac3
DE
8088}
8089\f
80626a55
DE
8090/* DWO/DWP files.
8091
8092 http://gcc.gnu.org/wiki/DebugFission
8093 http://gcc.gnu.org/wiki/DebugFissionDWP
8094
8095 To simplify handling of both DWO files ("object" files with the DWARF info)
8096 and DWP files (a file with the DWOs packaged up into one file), we treat
8097 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8098
8099static hashval_t
8100hash_dwo_file (const void *item)
8101{
8102 const struct dwo_file *dwo_file = item;
8103
80626a55 8104 return htab_hash_string (dwo_file->name);
3019eac3
DE
8105}
8106
8107static int
8108eq_dwo_file (const void *item_lhs, const void *item_rhs)
8109{
8110 const struct dwo_file *lhs = item_lhs;
8111 const struct dwo_file *rhs = item_rhs;
8112
80626a55 8113 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8114}
8115
8116/* Allocate a hash table for DWO files. */
8117
8118static htab_t
8119allocate_dwo_file_hash_table (void)
8120{
8121 struct objfile *objfile = dwarf2_per_objfile->objfile;
8122
8123 return htab_create_alloc_ex (41,
8124 hash_dwo_file,
8125 eq_dwo_file,
8126 NULL,
8127 &objfile->objfile_obstack,
8128 hashtab_obstack_allocate,
8129 dummy_obstack_deallocate);
8130}
8131
80626a55
DE
8132/* Lookup DWO file DWO_NAME. */
8133
8134static void **
8135lookup_dwo_file_slot (const char *dwo_name)
8136{
8137 struct dwo_file find_entry;
8138 void **slot;
8139
8140 if (dwarf2_per_objfile->dwo_files == NULL)
8141 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8142
8143 memset (&find_entry, 0, sizeof (find_entry));
8144 find_entry.name = dwo_name;
8145 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8146
8147 return slot;
8148}
8149
3019eac3
DE
8150static hashval_t
8151hash_dwo_unit (const void *item)
8152{
8153 const struct dwo_unit *dwo_unit = item;
8154
8155 /* This drops the top 32 bits of the id, but is ok for a hash. */
8156 return dwo_unit->signature;
8157}
8158
8159static int
8160eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8161{
8162 const struct dwo_unit *lhs = item_lhs;
8163 const struct dwo_unit *rhs = item_rhs;
8164
8165 /* The signature is assumed to be unique within the DWO file.
8166 So while object file CU dwo_id's always have the value zero,
8167 that's OK, assuming each object file DWO file has only one CU,
8168 and that's the rule for now. */
8169 return lhs->signature == rhs->signature;
8170}
8171
8172/* Allocate a hash table for DWO CUs,TUs.
8173 There is one of these tables for each of CUs,TUs for each DWO file. */
8174
8175static htab_t
8176allocate_dwo_unit_table (struct objfile *objfile)
8177{
8178 /* Start out with a pretty small number.
8179 Generally DWO files contain only one CU and maybe some TUs. */
8180 return htab_create_alloc_ex (3,
8181 hash_dwo_unit,
8182 eq_dwo_unit,
8183 NULL,
8184 &objfile->objfile_obstack,
8185 hashtab_obstack_allocate,
8186 dummy_obstack_deallocate);
8187}
8188
80626a55 8189/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8190
8191struct create_dwo_info_table_data
8192{
8193 struct dwo_file *dwo_file;
8194 htab_t cu_htab;
8195};
8196
80626a55 8197/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8198
8199static void
80626a55
DE
8200create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8201 gdb_byte *info_ptr,
8202 struct die_info *comp_unit_die,
8203 int has_children,
8204 void *datap)
3019eac3
DE
8205{
8206 struct dwarf2_cu *cu = reader->cu;
8207 struct objfile *objfile = dwarf2_per_objfile->objfile;
8208 sect_offset offset = cu->per_cu->offset;
8209 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8210 struct create_dwo_info_table_data *data = datap;
8211 struct dwo_file *dwo_file = data->dwo_file;
8212 htab_t cu_htab = data->cu_htab;
8213 void **slot;
8214 struct attribute *attr;
8215 struct dwo_unit *dwo_unit;
8216
8217 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8218 if (attr == NULL)
8219 {
8220 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8221 " its dwo_id [in module %s]"),
80626a55 8222 offset.sect_off, dwo_file->name);
3019eac3
DE
8223 return;
8224 }
8225
8226 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8227 dwo_unit->dwo_file = dwo_file;
8228 dwo_unit->signature = DW_UNSND (attr);
8229 dwo_unit->info_or_types_section = section;
8230 dwo_unit->offset = offset;
8231 dwo_unit->length = cu->per_cu->length;
8232
8233 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8234 gdb_assert (slot != NULL);
8235 if (*slot != NULL)
8236 {
8237 const struct dwo_unit *dup_dwo_unit = *slot;
8238
8239 complaint (&symfile_complaints,
8240 _("debug entry at offset 0x%x is duplicate to the entry at"
8241 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8242 offset.sect_off, dup_dwo_unit->offset.sect_off,
8243 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8244 dwo_file->name);
3019eac3
DE
8245 }
8246 else
8247 *slot = dwo_unit;
8248
09406207 8249 if (dwarf2_read_debug)
3019eac3
DE
8250 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8251 offset.sect_off,
8252 phex (dwo_unit->signature,
8253 sizeof (dwo_unit->signature)));
8254}
8255
80626a55
DE
8256/* Create a hash table to map DWO IDs to their CU entry in
8257 .debug_info.dwo in DWO_FILE.
8258 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8259
8260static htab_t
80626a55 8261create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8262{
8263 struct objfile *objfile = dwarf2_per_objfile->objfile;
8264 struct dwarf2_section_info *section = &dwo_file->sections.info;
8265 bfd *abfd;
8266 htab_t cu_htab;
8267 gdb_byte *info_ptr, *end_ptr;
8268 struct create_dwo_info_table_data create_dwo_info_table_data;
8269
8270 dwarf2_read_section (objfile, section);
8271 info_ptr = section->buffer;
8272
8273 if (info_ptr == NULL)
8274 return NULL;
8275
8276 /* We can't set abfd until now because the section may be empty or
8277 not present, in which case section->asection will be NULL. */
8278 abfd = section->asection->owner;
8279
09406207 8280 if (dwarf2_read_debug)
3019eac3
DE
8281 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8282 bfd_get_filename (abfd));
8283
8284 cu_htab = allocate_dwo_unit_table (objfile);
8285
8286 create_dwo_info_table_data.dwo_file = dwo_file;
8287 create_dwo_info_table_data.cu_htab = cu_htab;
8288
8289 end_ptr = info_ptr + section->size;
8290 while (info_ptr < end_ptr)
8291 {
8292 struct dwarf2_per_cu_data per_cu;
8293
8294 memset (&per_cu, 0, sizeof (per_cu));
8295 per_cu.objfile = objfile;
8296 per_cu.is_debug_types = 0;
8297 per_cu.offset.sect_off = info_ptr - section->buffer;
8298 per_cu.info_or_types_section = section;
8299
8300 init_cutu_and_read_dies_no_follow (&per_cu,
8301 &dwo_file->sections.abbrev,
8302 dwo_file,
80626a55 8303 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8304 &create_dwo_info_table_data);
8305
8306 info_ptr += per_cu.length;
8307 }
8308
8309 return cu_htab;
8310}
8311
80626a55
DE
8312/* DWP file .debug_{cu,tu}_index section format:
8313 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8314
8315 Both index sections have the same format, and serve to map a 64-bit
8316 signature to a set of section numbers. Each section begins with a header,
8317 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8318 indexes, and a pool of 32-bit section numbers. The index sections will be
8319 aligned at 8-byte boundaries in the file.
8320
8321 The index section header contains two unsigned 32-bit values (using the
8322 byte order of the application binary):
8323
8324 N, the number of compilation units or type units in the index
8325 M, the number of slots in the hash table
8326
8327 (We assume that N and M will not exceed 2^32 - 1.)
8328
8329 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8330
8331 The hash table begins at offset 8 in the section, and consists of an array
8332 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8333 order of the application binary). Unused slots in the hash table are 0.
8334 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8335
8336 The parallel table begins immediately after the hash table
8337 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8338 array of 32-bit indexes (using the byte order of the application binary),
8339 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8340 table contains a 32-bit index into the pool of section numbers. For unused
8341 hash table slots, the corresponding entry in the parallel table will be 0.
8342
8343 Given a 64-bit compilation unit signature or a type signature S, an entry
8344 in the hash table is located as follows:
8345
8346 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8347 the low-order k bits all set to 1.
8348
8349 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8350
8351 3) If the hash table entry at index H matches the signature, use that
8352 entry. If the hash table entry at index H is unused (all zeroes),
8353 terminate the search: the signature is not present in the table.
8354
8355 4) Let H = (H + H') modulo M. Repeat at Step 3.
8356
8357 Because M > N and H' and M are relatively prime, the search is guaranteed
8358 to stop at an unused slot or find the match.
8359
8360 The pool of section numbers begins immediately following the hash table
8361 (at offset 8 + 12 * M from the beginning of the section). The pool of
8362 section numbers consists of an array of 32-bit words (using the byte order
8363 of the application binary). Each item in the array is indexed starting
8364 from 0. The hash table entry provides the index of the first section
8365 number in the set. Additional section numbers in the set follow, and the
8366 set is terminated by a 0 entry (section number 0 is not used in ELF).
8367
8368 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8369 section must be the first entry in the set, and the .debug_abbrev.dwo must
8370 be the second entry. Other members of the set may follow in any order. */
8371
8372/* Create a hash table to map DWO IDs to their CU/TU entry in
8373 .debug_{info,types}.dwo in DWP_FILE.
8374 Returns NULL if there isn't one.
8375 Note: This function processes DWP files only, not DWO files. */
8376
8377static struct dwp_hash_table *
8378create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8379{
8380 struct objfile *objfile = dwarf2_per_objfile->objfile;
8381 bfd *dbfd = dwp_file->dbfd;
8382 char *index_ptr, *index_end;
8383 struct dwarf2_section_info *index;
8384 uint32_t version, nr_units, nr_slots;
8385 struct dwp_hash_table *htab;
8386
8387 if (is_debug_types)
8388 index = &dwp_file->sections.tu_index;
8389 else
8390 index = &dwp_file->sections.cu_index;
8391
8392 if (dwarf2_section_empty_p (index))
8393 return NULL;
8394 dwarf2_read_section (objfile, index);
8395
8396 index_ptr = index->buffer;
8397 index_end = index_ptr + index->size;
8398
8399 version = read_4_bytes (dbfd, index_ptr);
8400 index_ptr += 8; /* Skip the unused word. */
8401 nr_units = read_4_bytes (dbfd, index_ptr);
8402 index_ptr += 4;
8403 nr_slots = read_4_bytes (dbfd, index_ptr);
8404 index_ptr += 4;
8405
8406 if (version != 1)
8407 {
8408 error (_("Dwarf Error: unsupported DWP file version (%u)"
8409 " [in module %s]"),
8410 version, dwp_file->name);
8411 }
8412 if (nr_slots != (nr_slots & -nr_slots))
8413 {
8414 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8415 " is not power of 2 [in module %s]"),
8416 nr_slots, dwp_file->name);
8417 }
8418
8419 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8420 htab->nr_units = nr_units;
8421 htab->nr_slots = nr_slots;
8422 htab->hash_table = index_ptr;
8423 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8424 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8425
8426 return htab;
8427}
8428
8429/* Update SECTIONS with the data from SECTP.
8430
8431 This function is like the other "locate" section routines that are
8432 passed to bfd_map_over_sections, but in this context the sections to
8433 read comes from the DWP hash table, not the full ELF section table.
8434
8435 The result is non-zero for success, or zero if an error was found. */
8436
8437static int
8438locate_virtual_dwo_sections (asection *sectp,
8439 struct virtual_dwo_sections *sections)
8440{
8441 const struct dwop_section_names *names = &dwop_section_names;
8442
8443 if (section_is_p (sectp->name, &names->abbrev_dwo))
8444 {
8445 /* There can be only one. */
8446 if (sections->abbrev.asection != NULL)
8447 return 0;
8448 sections->abbrev.asection = sectp;
8449 sections->abbrev.size = bfd_get_section_size (sectp);
8450 }
8451 else if (section_is_p (sectp->name, &names->info_dwo)
8452 || section_is_p (sectp->name, &names->types_dwo))
8453 {
8454 /* There can be only one. */
8455 if (sections->info_or_types.asection != NULL)
8456 return 0;
8457 sections->info_or_types.asection = sectp;
8458 sections->info_or_types.size = bfd_get_section_size (sectp);
8459 }
8460 else if (section_is_p (sectp->name, &names->line_dwo))
8461 {
8462 /* There can be only one. */
8463 if (sections->line.asection != NULL)
8464 return 0;
8465 sections->line.asection = sectp;
8466 sections->line.size = bfd_get_section_size (sectp);
8467 }
8468 else if (section_is_p (sectp->name, &names->loc_dwo))
8469 {
8470 /* There can be only one. */
8471 if (sections->loc.asection != NULL)
8472 return 0;
8473 sections->loc.asection = sectp;
8474 sections->loc.size = bfd_get_section_size (sectp);
8475 }
8476 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8477 {
8478 /* There can be only one. */
8479 if (sections->macinfo.asection != NULL)
8480 return 0;
8481 sections->macinfo.asection = sectp;
8482 sections->macinfo.size = bfd_get_section_size (sectp);
8483 }
8484 else if (section_is_p (sectp->name, &names->macro_dwo))
8485 {
8486 /* There can be only one. */
8487 if (sections->macro.asection != NULL)
8488 return 0;
8489 sections->macro.asection = sectp;
8490 sections->macro.size = bfd_get_section_size (sectp);
8491 }
8492 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8493 {
8494 /* There can be only one. */
8495 if (sections->str_offsets.asection != NULL)
8496 return 0;
8497 sections->str_offsets.asection = sectp;
8498 sections->str_offsets.size = bfd_get_section_size (sectp);
8499 }
8500 else
8501 {
8502 /* No other kind of section is valid. */
8503 return 0;
8504 }
8505
8506 return 1;
8507}
8508
8509/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8510 HTAB is the hash table from the DWP file.
8511 SECTION_INDEX is the index of the DWO in HTAB. */
8512
8513static struct dwo_unit *
8514create_dwo_in_dwp (struct dwp_file *dwp_file,
8515 const struct dwp_hash_table *htab,
8516 uint32_t section_index,
8517 ULONGEST signature, int is_debug_types)
8518{
8519 struct objfile *objfile = dwarf2_per_objfile->objfile;
8520 bfd *dbfd = dwp_file->dbfd;
8521 const char *kind = is_debug_types ? "TU" : "CU";
8522 struct dwo_file *dwo_file;
8523 struct dwo_unit *dwo_unit;
8524 struct virtual_dwo_sections sections;
8525 void **dwo_file_slot;
8526 char *virtual_dwo_name;
8527 struct dwarf2_section_info *cutu;
8528 struct cleanup *cleanups;
8529 int i;
8530
8531 if (dwarf2_read_debug)
8532 {
8533 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8534 kind,
8535 section_index, phex (signature, sizeof (signature)),
8536 dwp_file->name);
8537 }
8538
8539 /* Fetch the sections of this DWO.
8540 Put a limit on the number of sections we look for so that bad data
8541 doesn't cause us to loop forever. */
8542
8543#define MAX_NR_DWO_SECTIONS \
8544 (1 /* .debug_info or .debug_types */ \
8545 + 1 /* .debug_abbrev */ \
8546 + 1 /* .debug_line */ \
8547 + 1 /* .debug_loc */ \
8548 + 1 /* .debug_str_offsets */ \
8549 + 1 /* .debug_macro */ \
8550 + 1 /* .debug_macinfo */ \
8551 + 1 /* trailing zero */)
8552
8553 memset (&sections, 0, sizeof (sections));
8554 cleanups = make_cleanup (null_cleanup, 0);
8555
8556 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8557 {
8558 asection *sectp;
8559 uint32_t section_nr =
8560 read_4_bytes (dbfd,
8561 htab->section_pool
8562 + (section_index + i) * sizeof (uint32_t));
8563
8564 if (section_nr == 0)
8565 break;
8566 if (section_nr >= dwp_file->num_sections)
8567 {
8568 error (_("Dwarf Error: bad DWP hash table, section number too large"
8569 " [in module %s]"),
8570 dwp_file->name);
8571 }
8572
8573 sectp = dwp_file->elf_sections[section_nr];
8574 if (! locate_virtual_dwo_sections (sectp, &sections))
8575 {
8576 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8577 " [in module %s]"),
8578 dwp_file->name);
8579 }
8580 }
8581
8582 if (i < 2
8583 || sections.info_or_types.asection == NULL
8584 || sections.abbrev.asection == NULL)
8585 {
8586 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8587 " [in module %s]"),
8588 dwp_file->name);
8589 }
8590 if (i == MAX_NR_DWO_SECTIONS)
8591 {
8592 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8593 " [in module %s]"),
8594 dwp_file->name);
8595 }
8596
8597 /* It's easier for the rest of the code if we fake a struct dwo_file and
8598 have dwo_unit "live" in that. At least for now.
8599
8600 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8601 However, for each CU + set of TUs that came from the same original DWO
8602 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8603 (fewer struct dwo_file objects to allocated). Remember that for really
8604 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8605
2792b94d
PM
8606 virtual_dwo_name =
8607 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8608 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8609 sections.line.asection ? sections.line.asection->id : 0,
8610 sections.loc.asection ? sections.loc.asection->id : 0,
8611 (sections.str_offsets.asection
8612 ? sections.str_offsets.asection->id
8613 : 0));
80626a55
DE
8614 make_cleanup (xfree, virtual_dwo_name);
8615 /* Can we use an existing virtual DWO file? */
8616 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8617 /* Create one if necessary. */
8618 if (*dwo_file_slot == NULL)
8619 {
8620 if (dwarf2_read_debug)
8621 {
8622 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8623 virtual_dwo_name);
8624 }
8625 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8626 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8627 virtual_dwo_name,
8628 strlen (virtual_dwo_name));
8629 dwo_file->sections.abbrev = sections.abbrev;
8630 dwo_file->sections.line = sections.line;
8631 dwo_file->sections.loc = sections.loc;
8632 dwo_file->sections.macinfo = sections.macinfo;
8633 dwo_file->sections.macro = sections.macro;
8634 dwo_file->sections.str_offsets = sections.str_offsets;
8635 /* The "str" section is global to the entire DWP file. */
8636 dwo_file->sections.str = dwp_file->sections.str;
8637 /* The info or types section is assigned later to dwo_unit,
8638 there's no need to record it in dwo_file.
8639 Also, we can't simply record type sections in dwo_file because
8640 we record a pointer into the vector in dwo_unit. As we collect more
8641 types we'll grow the vector and eventually have to reallocate space
8642 for it, invalidating all the pointers into the current copy. */
8643 *dwo_file_slot = dwo_file;
8644 }
8645 else
8646 {
8647 if (dwarf2_read_debug)
8648 {
8649 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8650 virtual_dwo_name);
8651 }
8652 dwo_file = *dwo_file_slot;
8653 }
8654 do_cleanups (cleanups);
8655
8656 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8657 dwo_unit->dwo_file = dwo_file;
8658 dwo_unit->signature = signature;
8659 dwo_unit->info_or_types_section =
8660 obstack_alloc (&objfile->objfile_obstack,
8661 sizeof (struct dwarf2_section_info));
8662 *dwo_unit->info_or_types_section = sections.info_or_types;
8663 /* offset, length, type_offset_in_tu are set later. */
8664
8665 return dwo_unit;
8666}
8667
8668/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8669
8670static struct dwo_unit *
8671lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8672 const struct dwp_hash_table *htab,
8673 ULONGEST signature, int is_debug_types)
8674{
8675 bfd *dbfd = dwp_file->dbfd;
8676 uint32_t mask = htab->nr_slots - 1;
8677 uint32_t hash = signature & mask;
8678 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8679 unsigned int i;
8680 void **slot;
8681 struct dwo_unit find_dwo_cu, *dwo_cu;
8682
8683 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8684 find_dwo_cu.signature = signature;
8685 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8686
8687 if (*slot != NULL)
8688 return *slot;
8689
8690 /* Use a for loop so that we don't loop forever on bad debug info. */
8691 for (i = 0; i < htab->nr_slots; ++i)
8692 {
8693 ULONGEST signature_in_table;
8694
8695 signature_in_table =
8696 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8697 if (signature_in_table == signature)
8698 {
8699 uint32_t section_index =
8700 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8701
8702 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8703 signature, is_debug_types);
8704 return *slot;
8705 }
8706 if (signature_in_table == 0)
8707 return NULL;
8708 hash = (hash + hash2) & mask;
8709 }
8710
8711 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8712 " [in module %s]"),
8713 dwp_file->name);
8714}
8715
8716/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8717 Open the file specified by FILE_NAME and hand it off to BFD for
8718 preliminary analysis. Return a newly initialized bfd *, which
8719 includes a canonicalized copy of FILE_NAME.
80626a55 8720 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8721 In case of trouble, return NULL.
8722 NOTE: This function is derived from symfile_bfd_open. */
8723
8724static bfd *
80626a55 8725try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8726{
8727 bfd *sym_bfd;
80626a55 8728 int desc, flags;
3019eac3 8729 char *absolute_name;
3019eac3 8730
80626a55
DE
8731 flags = OPF_TRY_CWD_FIRST;
8732 if (is_dwp)
8733 flags |= OPF_SEARCH_IN_PATH;
8734 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8735 O_RDONLY | O_BINARY, &absolute_name);
8736 if (desc < 0)
8737 return NULL;
8738
bb397797 8739 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8740 if (!sym_bfd)
8741 {
3019eac3
DE
8742 xfree (absolute_name);
8743 return NULL;
8744 }
a4453b7e 8745 xfree (absolute_name);
3019eac3
DE
8746 bfd_set_cacheable (sym_bfd, 1);
8747
8748 if (!bfd_check_format (sym_bfd, bfd_object))
8749 {
cbb099e8 8750 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8751 return NULL;
8752 }
8753
3019eac3
DE
8754 return sym_bfd;
8755}
8756
80626a55 8757/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8758 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8759 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8760 The result is the bfd handle of the file.
8761 If there is a problem finding or opening the file, return NULL.
8762 Upon success, the canonicalized path of the file is stored in the bfd,
8763 same as symfile_bfd_open. */
8764
8765static bfd *
80626a55 8766open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8767{
8768 bfd *abfd;
3019eac3 8769
80626a55
DE
8770 if (IS_ABSOLUTE_PATH (file_name))
8771 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8772
8773 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8774
8775 if (comp_dir != NULL)
8776 {
80626a55 8777 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8778
8779 /* NOTE: If comp_dir is a relative path, this will also try the
8780 search path, which seems useful. */
80626a55 8781 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8782 xfree (path_to_try);
8783 if (abfd != NULL)
8784 return abfd;
8785 }
8786
8787 /* That didn't work, try debug-file-directory, which, despite its name,
8788 is a list of paths. */
8789
8790 if (*debug_file_directory == '\0')
8791 return NULL;
8792
80626a55 8793 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8794}
8795
80626a55
DE
8796/* This function is mapped across the sections and remembers the offset and
8797 size of each of the DWO debugging sections we are interested in. */
8798
8799static void
8800dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8801{
8802 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8803 const struct dwop_section_names *names = &dwop_section_names;
8804
8805 if (section_is_p (sectp->name, &names->abbrev_dwo))
8806 {
8807 dwo_sections->abbrev.asection = sectp;
8808 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8809 }
8810 else if (section_is_p (sectp->name, &names->info_dwo))
8811 {
8812 dwo_sections->info.asection = sectp;
8813 dwo_sections->info.size = bfd_get_section_size (sectp);
8814 }
8815 else if (section_is_p (sectp->name, &names->line_dwo))
8816 {
8817 dwo_sections->line.asection = sectp;
8818 dwo_sections->line.size = bfd_get_section_size (sectp);
8819 }
8820 else if (section_is_p (sectp->name, &names->loc_dwo))
8821 {
8822 dwo_sections->loc.asection = sectp;
8823 dwo_sections->loc.size = bfd_get_section_size (sectp);
8824 }
8825 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8826 {
8827 dwo_sections->macinfo.asection = sectp;
8828 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8829 }
8830 else if (section_is_p (sectp->name, &names->macro_dwo))
8831 {
8832 dwo_sections->macro.asection = sectp;
8833 dwo_sections->macro.size = bfd_get_section_size (sectp);
8834 }
8835 else if (section_is_p (sectp->name, &names->str_dwo))
8836 {
8837 dwo_sections->str.asection = sectp;
8838 dwo_sections->str.size = bfd_get_section_size (sectp);
8839 }
8840 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8841 {
8842 dwo_sections->str_offsets.asection = sectp;
8843 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8844 }
8845 else if (section_is_p (sectp->name, &names->types_dwo))
8846 {
8847 struct dwarf2_section_info type_section;
8848
8849 memset (&type_section, 0, sizeof (type_section));
8850 type_section.asection = sectp;
8851 type_section.size = bfd_get_section_size (sectp);
8852 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8853 &type_section);
8854 }
8855}
8856
8857/* Initialize the use of the DWO file specified by DWO_NAME.
8858 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8859
8860static struct dwo_file *
80626a55 8861open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8862{
8863 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8864 struct dwo_file *dwo_file;
8865 bfd *dbfd;
3019eac3
DE
8866 struct cleanup *cleanups;
8867
80626a55
DE
8868 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8869 if (dbfd == NULL)
8870 {
8871 if (dwarf2_read_debug)
8872 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8873 return NULL;
8874 }
8875 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8876 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8877 dwo_name, strlen (dwo_name));
8878 dwo_file->dbfd = dbfd;
3019eac3
DE
8879
8880 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8881
80626a55 8882 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8883
80626a55 8884 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8885
8886 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8887 dwo_file->sections.types);
8888
8889 discard_cleanups (cleanups);
8890
80626a55
DE
8891 if (dwarf2_read_debug)
8892 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8893
3019eac3
DE
8894 return dwo_file;
8895}
8896
80626a55
DE
8897/* This function is mapped across the sections and remembers the offset and
8898 size of each of the DWP debugging sections we are interested in. */
3019eac3 8899
80626a55
DE
8900static void
8901dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8902{
80626a55
DE
8903 struct dwp_file *dwp_file = dwp_file_ptr;
8904 const struct dwop_section_names *names = &dwop_section_names;
8905 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8906
80626a55
DE
8907 /* Record the ELF section number for later lookup: this is what the
8908 .debug_cu_index,.debug_tu_index tables use. */
8909 gdb_assert (elf_section_nr < dwp_file->num_sections);
8910 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8911
80626a55
DE
8912 /* Look for specific sections that we need. */
8913 if (section_is_p (sectp->name, &names->str_dwo))
8914 {
8915 dwp_file->sections.str.asection = sectp;
8916 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8917 }
8918 else if (section_is_p (sectp->name, &names->cu_index))
8919 {
8920 dwp_file->sections.cu_index.asection = sectp;
8921 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8922 }
8923 else if (section_is_p (sectp->name, &names->tu_index))
8924 {
8925 dwp_file->sections.tu_index.asection = sectp;
8926 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8927 }
8928}
3019eac3 8929
80626a55 8930/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8931
80626a55
DE
8932static hashval_t
8933hash_dwp_loaded_cutus (const void *item)
8934{
8935 const struct dwo_unit *dwo_unit = item;
3019eac3 8936
80626a55
DE
8937 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8938 return dwo_unit->signature;
3019eac3
DE
8939}
8940
80626a55 8941/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 8942
80626a55
DE
8943static int
8944eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 8945{
80626a55
DE
8946 const struct dwo_unit *dua = a;
8947 const struct dwo_unit *dub = b;
3019eac3 8948
80626a55
DE
8949 return dua->signature == dub->signature;
8950}
3019eac3 8951
80626a55 8952/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 8953
80626a55
DE
8954static htab_t
8955allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8956{
8957 return htab_create_alloc_ex (3,
8958 hash_dwp_loaded_cutus,
8959 eq_dwp_loaded_cutus,
8960 NULL,
8961 &objfile->objfile_obstack,
8962 hashtab_obstack_allocate,
8963 dummy_obstack_deallocate);
8964}
3019eac3 8965
80626a55
DE
8966/* Initialize the use of the DWP file for the current objfile.
8967 By convention the name of the DWP file is ${objfile}.dwp.
8968 The result is NULL if it can't be found. */
a766d390 8969
80626a55
DE
8970static struct dwp_file *
8971open_and_init_dwp_file (const char *comp_dir)
8972{
8973 struct objfile *objfile = dwarf2_per_objfile->objfile;
8974 struct dwp_file *dwp_file;
8975 char *dwp_name;
8976 bfd *dbfd;
8977 struct cleanup *cleanups;
8978
2792b94d 8979 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
8980 cleanups = make_cleanup (xfree, dwp_name);
8981
8982 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8983 if (dbfd == NULL)
8984 {
8985 if (dwarf2_read_debug)
8986 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8987 do_cleanups (cleanups);
8988 return NULL;
3019eac3 8989 }
80626a55
DE
8990 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
8991 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
8992 dwp_name, strlen (dwp_name));
8993 dwp_file->dbfd = dbfd;
8994 do_cleanups (cleanups);
c906108c 8995
80626a55 8996 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 8997
80626a55
DE
8998 /* +1: section 0 is unused */
8999 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9000 dwp_file->elf_sections =
9001 OBSTACK_CALLOC (&objfile->objfile_obstack,
9002 dwp_file->num_sections, asection *);
9003
9004 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9005
9006 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9007
9008 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9009
9010 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9011
9012 discard_cleanups (cleanups);
9013
9014 if (dwarf2_read_debug)
9015 {
9016 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9017 fprintf_unfiltered (gdb_stdlog,
9018 " %u CUs, %u TUs\n",
9019 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9020 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9021 }
9022
9023 return dwp_file;
3019eac3 9024}
c906108c 9025
80626a55
DE
9026/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9027 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9028 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9029 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9030 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9031
9032 This is called, for example, when wanting to read a variable with a
9033 complex location. Therefore we don't want to do file i/o for every call.
9034 Therefore we don't want to look for a DWO file on every call.
9035 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9036 then we check if we've already seen DWO_NAME, and only THEN do we check
9037 for a DWO file.
9038
1c658ad5 9039 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9040 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9041
3019eac3 9042static struct dwo_unit *
80626a55
DE
9043lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9044 const char *dwo_name, const char *comp_dir,
9045 ULONGEST signature, int is_debug_types)
3019eac3
DE
9046{
9047 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9048 const char *kind = is_debug_types ? "TU" : "CU";
9049 void **dwo_file_slot;
3019eac3 9050 struct dwo_file *dwo_file;
80626a55 9051 struct dwp_file *dwp_file;
cb1df416 9052
80626a55 9053 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9054
80626a55
DE
9055 if (! dwarf2_per_objfile->dwp_checked)
9056 {
9057 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9058 dwarf2_per_objfile->dwp_checked = 1;
9059 }
9060 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9061
80626a55 9062 if (dwp_file != NULL)
cf2c3c16 9063 {
80626a55
DE
9064 const struct dwp_hash_table *dwp_htab =
9065 is_debug_types ? dwp_file->tus : dwp_file->cus;
9066
9067 if (dwp_htab != NULL)
9068 {
9069 struct dwo_unit *dwo_cutu =
9070 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9071
9072 if (dwo_cutu != NULL)
9073 {
9074 if (dwarf2_read_debug)
9075 {
9076 fprintf_unfiltered (gdb_stdlog,
9077 "Virtual DWO %s %s found: @%s\n",
9078 kind, hex_string (signature),
9079 host_address_to_string (dwo_cutu));
9080 }
9081 return dwo_cutu;
9082 }
9083 }
9084 }
9085
9086 /* Have we already seen DWO_NAME? */
9087
9088 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9089 if (*dwo_file_slot == NULL)
9090 {
9091 /* Read in the file and build a table of the DWOs it contains. */
9092 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9093 }
9094 /* NOTE: This will be NULL if unable to open the file. */
9095 dwo_file = *dwo_file_slot;
9096
9097 if (dwo_file != NULL)
9098 {
9099 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9100
9101 if (htab != NULL)
9102 {
9103 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9104
80626a55
DE
9105 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9106 find_dwo_cutu.signature = signature;
9107 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9108
80626a55
DE
9109 if (dwo_cutu != NULL)
9110 {
9111 if (dwarf2_read_debug)
9112 {
9113 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9114 kind, dwo_name, hex_string (signature),
9115 host_address_to_string (dwo_cutu));
9116 }
9117 return dwo_cutu;
9118 }
9119 }
2e276125 9120 }
9cdd5dbd 9121
80626a55
DE
9122 /* We didn't find it. This could mean a dwo_id mismatch, or
9123 someone deleted the DWO/DWP file, or the search path isn't set up
9124 correctly to find the file. */
9125
9126 if (dwarf2_read_debug)
9127 {
9128 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9129 kind, dwo_name, hex_string (signature));
9130 }
3019eac3
DE
9131
9132 complaint (&symfile_complaints,
80626a55 9133 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9134 " [in module %s]"),
80626a55 9135 this_unit->offset.sect_off, objfile->name);
3019eac3 9136 return NULL;
5fb290d7
DJ
9137}
9138
80626a55
DE
9139/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9140 See lookup_dwo_cutu_unit for details. */
9141
9142static struct dwo_unit *
9143lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9144 const char *dwo_name, const char *comp_dir,
9145 ULONGEST signature)
9146{
9147 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9148}
9149
9150/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9151 See lookup_dwo_cutu_unit for details. */
9152
9153static struct dwo_unit *
9154lookup_dwo_type_unit (struct signatured_type *this_tu,
9155 const char *dwo_name, const char *comp_dir)
9156{
9157 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9158}
9159
3019eac3
DE
9160/* Free all resources associated with DWO_FILE.
9161 Close the DWO file and munmap the sections.
9162 All memory should be on the objfile obstack. */
348e048f
DE
9163
9164static void
3019eac3 9165free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9166{
3019eac3
DE
9167 int ix;
9168 struct dwarf2_section_info *section;
348e048f 9169
80626a55
DE
9170 gdb_assert (dwo_file->dbfd != objfile->obfd);
9171 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9172
3019eac3
DE
9173 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9174}
348e048f 9175
3019eac3 9176/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9177
3019eac3
DE
9178static void
9179free_dwo_file_cleanup (void *arg)
9180{
9181 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9182 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9183
3019eac3
DE
9184 free_dwo_file (dwo_file, objfile);
9185}
348e048f 9186
3019eac3 9187/* Traversal function for free_dwo_files. */
2ab95328 9188
3019eac3
DE
9189static int
9190free_dwo_file_from_slot (void **slot, void *info)
9191{
9192 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9193 struct objfile *objfile = (struct objfile *) info;
348e048f 9194
3019eac3 9195 free_dwo_file (dwo_file, objfile);
348e048f 9196
3019eac3
DE
9197 return 1;
9198}
348e048f 9199
3019eac3 9200/* Free all resources associated with DWO_FILES. */
348e048f 9201
3019eac3
DE
9202static void
9203free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9204{
9205 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9206}
3019eac3
DE
9207\f
9208/* Read in various DIEs. */
348e048f 9209
d389af10
JK
9210/* qsort helper for inherit_abstract_dies. */
9211
9212static int
9213unsigned_int_compar (const void *ap, const void *bp)
9214{
9215 unsigned int a = *(unsigned int *) ap;
9216 unsigned int b = *(unsigned int *) bp;
9217
9218 return (a > b) - (b > a);
9219}
9220
9221/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9222 Inherit only the children of the DW_AT_abstract_origin DIE not being
9223 already referenced by DW_AT_abstract_origin from the children of the
9224 current DIE. */
d389af10
JK
9225
9226static void
9227inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9228{
9229 struct die_info *child_die;
9230 unsigned die_children_count;
9231 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9232 sect_offset *offsets;
9233 sect_offset *offsets_end, *offsetp;
d389af10
JK
9234 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9235 struct die_info *origin_die;
9236 /* Iterator of the ORIGIN_DIE children. */
9237 struct die_info *origin_child_die;
9238 struct cleanup *cleanups;
9239 struct attribute *attr;
cd02d79d
PA
9240 struct dwarf2_cu *origin_cu;
9241 struct pending **origin_previous_list_in_scope;
d389af10
JK
9242
9243 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9244 if (!attr)
9245 return;
9246
cd02d79d
PA
9247 /* Note that following die references may follow to a die in a
9248 different cu. */
9249
9250 origin_cu = cu;
9251 origin_die = follow_die_ref (die, attr, &origin_cu);
9252
9253 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9254 symbols in. */
9255 origin_previous_list_in_scope = origin_cu->list_in_scope;
9256 origin_cu->list_in_scope = cu->list_in_scope;
9257
edb3359d
DJ
9258 if (die->tag != origin_die->tag
9259 && !(die->tag == DW_TAG_inlined_subroutine
9260 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9261 complaint (&symfile_complaints,
9262 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9263 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9264
9265 child_die = die->child;
9266 die_children_count = 0;
9267 while (child_die && child_die->tag)
9268 {
9269 child_die = sibling_die (child_die);
9270 die_children_count++;
9271 }
9272 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9273 cleanups = make_cleanup (xfree, offsets);
9274
9275 offsets_end = offsets;
9276 child_die = die->child;
9277 while (child_die && child_die->tag)
9278 {
c38f313d
DJ
9279 /* For each CHILD_DIE, find the corresponding child of
9280 ORIGIN_DIE. If there is more than one layer of
9281 DW_AT_abstract_origin, follow them all; there shouldn't be,
9282 but GCC versions at least through 4.4 generate this (GCC PR
9283 40573). */
9284 struct die_info *child_origin_die = child_die;
cd02d79d 9285 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9286
c38f313d
DJ
9287 while (1)
9288 {
cd02d79d
PA
9289 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9290 child_origin_cu);
c38f313d
DJ
9291 if (attr == NULL)
9292 break;
cd02d79d
PA
9293 child_origin_die = follow_die_ref (child_origin_die, attr,
9294 &child_origin_cu);
c38f313d
DJ
9295 }
9296
d389af10
JK
9297 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9298 counterpart may exist. */
c38f313d 9299 if (child_origin_die != child_die)
d389af10 9300 {
edb3359d
DJ
9301 if (child_die->tag != child_origin_die->tag
9302 && !(child_die->tag == DW_TAG_inlined_subroutine
9303 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9304 complaint (&symfile_complaints,
9305 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9306 "different tags"), child_die->offset.sect_off,
9307 child_origin_die->offset.sect_off);
c38f313d
DJ
9308 if (child_origin_die->parent != origin_die)
9309 complaint (&symfile_complaints,
9310 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9311 "different parents"), child_die->offset.sect_off,
9312 child_origin_die->offset.sect_off);
c38f313d
DJ
9313 else
9314 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9315 }
9316 child_die = sibling_die (child_die);
9317 }
9318 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9319 unsigned_int_compar);
9320 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9321 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9322 complaint (&symfile_complaints,
9323 _("Multiple children of DIE 0x%x refer "
9324 "to DIE 0x%x as their abstract origin"),
b64f50a1 9325 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9326
9327 offsetp = offsets;
9328 origin_child_die = origin_die->child;
9329 while (origin_child_die && origin_child_die->tag)
9330 {
9331 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9332 while (offsetp < offsets_end
9333 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9334 offsetp++;
b64f50a1
JK
9335 if (offsetp >= offsets_end
9336 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9337 {
9338 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9339 process_die (origin_child_die, origin_cu);
d389af10
JK
9340 }
9341 origin_child_die = sibling_die (origin_child_die);
9342 }
cd02d79d 9343 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9344
9345 do_cleanups (cleanups);
9346}
9347
c906108c 9348static void
e7c27a73 9349read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9350{
e7c27a73 9351 struct objfile *objfile = cu->objfile;
52f0bd74 9352 struct context_stack *new;
c906108c
SS
9353 CORE_ADDR lowpc;
9354 CORE_ADDR highpc;
9355 struct die_info *child_die;
edb3359d 9356 struct attribute *attr, *call_line, *call_file;
c906108c 9357 char *name;
e142c38c 9358 CORE_ADDR baseaddr;
801e3a5b 9359 struct block *block;
edb3359d 9360 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9361 VEC (symbolp) *template_args = NULL;
9362 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9363
9364 if (inlined_func)
9365 {
9366 /* If we do not have call site information, we can't show the
9367 caller of this inlined function. That's too confusing, so
9368 only use the scope for local variables. */
9369 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9370 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9371 if (call_line == NULL || call_file == NULL)
9372 {
9373 read_lexical_block_scope (die, cu);
9374 return;
9375 }
9376 }
c906108c 9377
e142c38c
DJ
9378 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9379
94af9270 9380 name = dwarf2_name (die, cu);
c906108c 9381
e8d05480
JB
9382 /* Ignore functions with missing or empty names. These are actually
9383 illegal according to the DWARF standard. */
9384 if (name == NULL)
9385 {
9386 complaint (&symfile_complaints,
b64f50a1
JK
9387 _("missing name for subprogram DIE at %d"),
9388 die->offset.sect_off);
e8d05480
JB
9389 return;
9390 }
9391
9392 /* Ignore functions with missing or invalid low and high pc attributes. */
9393 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9394 {
ae4d0c03
PM
9395 attr = dwarf2_attr (die, DW_AT_external, cu);
9396 if (!attr || !DW_UNSND (attr))
9397 complaint (&symfile_complaints,
3e43a32a
MS
9398 _("cannot get low and high bounds "
9399 "for subprogram DIE at %d"),
b64f50a1 9400 die->offset.sect_off);
e8d05480
JB
9401 return;
9402 }
c906108c
SS
9403
9404 lowpc += baseaddr;
9405 highpc += baseaddr;
9406
34eaf542
TT
9407 /* If we have any template arguments, then we must allocate a
9408 different sort of symbol. */
9409 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9410 {
9411 if (child_die->tag == DW_TAG_template_type_param
9412 || child_die->tag == DW_TAG_template_value_param)
9413 {
9414 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9415 struct template_symbol);
9416 templ_func->base.is_cplus_template_function = 1;
9417 break;
9418 }
9419 }
9420
c906108c 9421 new = push_context (0, lowpc);
34eaf542
TT
9422 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9423 (struct symbol *) templ_func);
4c2df51b 9424
4cecd739
DJ
9425 /* If there is a location expression for DW_AT_frame_base, record
9426 it. */
e142c38c 9427 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9428 if (attr)
c034e007
AC
9429 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9430 expression is being recorded directly in the function's symbol
9431 and not in a separate frame-base object. I guess this hack is
9432 to avoid adding some sort of frame-base adjunct/annex to the
9433 function's symbol :-(. The problem with doing this is that it
9434 results in a function symbol with a location expression that
9435 has nothing to do with the location of the function, ouch! The
9436 relationship should be: a function's symbol has-a frame base; a
9437 frame-base has-a location expression. */
e7c27a73 9438 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9439
e142c38c 9440 cu->list_in_scope = &local_symbols;
c906108c 9441
639d11d3 9442 if (die->child != NULL)
c906108c 9443 {
639d11d3 9444 child_die = die->child;
c906108c
SS
9445 while (child_die && child_die->tag)
9446 {
34eaf542
TT
9447 if (child_die->tag == DW_TAG_template_type_param
9448 || child_die->tag == DW_TAG_template_value_param)
9449 {
9450 struct symbol *arg = new_symbol (child_die, NULL, cu);
9451
f1078f66
DJ
9452 if (arg != NULL)
9453 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9454 }
9455 else
9456 process_die (child_die, cu);
c906108c
SS
9457 child_die = sibling_die (child_die);
9458 }
9459 }
9460
d389af10
JK
9461 inherit_abstract_dies (die, cu);
9462
4a811a97
UW
9463 /* If we have a DW_AT_specification, we might need to import using
9464 directives from the context of the specification DIE. See the
9465 comment in determine_prefix. */
9466 if (cu->language == language_cplus
9467 && dwarf2_attr (die, DW_AT_specification, cu))
9468 {
9469 struct dwarf2_cu *spec_cu = cu;
9470 struct die_info *spec_die = die_specification (die, &spec_cu);
9471
9472 while (spec_die)
9473 {
9474 child_die = spec_die->child;
9475 while (child_die && child_die->tag)
9476 {
9477 if (child_die->tag == DW_TAG_imported_module)
9478 process_die (child_die, spec_cu);
9479 child_die = sibling_die (child_die);
9480 }
9481
9482 /* In some cases, GCC generates specification DIEs that
9483 themselves contain DW_AT_specification attributes. */
9484 spec_die = die_specification (spec_die, &spec_cu);
9485 }
9486 }
9487
c906108c
SS
9488 new = pop_context ();
9489 /* Make a block for the local symbols within. */
801e3a5b
JB
9490 block = finish_block (new->name, &local_symbols, new->old_blocks,
9491 lowpc, highpc, objfile);
9492
df8a16a1 9493 /* For C++, set the block's scope. */
f55ee35c 9494 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9495 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9496 determine_prefix (die, cu),
df8a16a1
DJ
9497 processing_has_namespace_info);
9498
801e3a5b
JB
9499 /* If we have address ranges, record them. */
9500 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9501
34eaf542
TT
9502 /* Attach template arguments to function. */
9503 if (! VEC_empty (symbolp, template_args))
9504 {
9505 gdb_assert (templ_func != NULL);
9506
9507 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9508 templ_func->template_arguments
9509 = obstack_alloc (&objfile->objfile_obstack,
9510 (templ_func->n_template_arguments
9511 * sizeof (struct symbol *)));
9512 memcpy (templ_func->template_arguments,
9513 VEC_address (symbolp, template_args),
9514 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9515 VEC_free (symbolp, template_args);
9516 }
9517
208d8187
JB
9518 /* In C++, we can have functions nested inside functions (e.g., when
9519 a function declares a class that has methods). This means that
9520 when we finish processing a function scope, we may need to go
9521 back to building a containing block's symbol lists. */
9522 local_symbols = new->locals;
27aa8d6a 9523 using_directives = new->using_directives;
208d8187 9524
921e78cf
JB
9525 /* If we've finished processing a top-level function, subsequent
9526 symbols go in the file symbol list. */
9527 if (outermost_context_p ())
e142c38c 9528 cu->list_in_scope = &file_symbols;
c906108c
SS
9529}
9530
9531/* Process all the DIES contained within a lexical block scope. Start
9532 a new scope, process the dies, and then close the scope. */
9533
9534static void
e7c27a73 9535read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9536{
e7c27a73 9537 struct objfile *objfile = cu->objfile;
52f0bd74 9538 struct context_stack *new;
c906108c
SS
9539 CORE_ADDR lowpc, highpc;
9540 struct die_info *child_die;
e142c38c
DJ
9541 CORE_ADDR baseaddr;
9542
9543 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9544
9545 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9546 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9547 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9548 be nasty. Might be easier to properly extend generic blocks to
af34e669 9549 describe ranges. */
d85a05f0 9550 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9551 return;
9552 lowpc += baseaddr;
9553 highpc += baseaddr;
9554
9555 push_context (0, lowpc);
639d11d3 9556 if (die->child != NULL)
c906108c 9557 {
639d11d3 9558 child_die = die->child;
c906108c
SS
9559 while (child_die && child_die->tag)
9560 {
e7c27a73 9561 process_die (child_die, cu);
c906108c
SS
9562 child_die = sibling_die (child_die);
9563 }
9564 }
9565 new = pop_context ();
9566
8540c487 9567 if (local_symbols != NULL || using_directives != NULL)
c906108c 9568 {
801e3a5b
JB
9569 struct block *block
9570 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9571 highpc, objfile);
9572
9573 /* Note that recording ranges after traversing children, as we
9574 do here, means that recording a parent's ranges entails
9575 walking across all its children's ranges as they appear in
9576 the address map, which is quadratic behavior.
9577
9578 It would be nicer to record the parent's ranges before
9579 traversing its children, simply overriding whatever you find
9580 there. But since we don't even decide whether to create a
9581 block until after we've traversed its children, that's hard
9582 to do. */
9583 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9584 }
9585 local_symbols = new->locals;
27aa8d6a 9586 using_directives = new->using_directives;
c906108c
SS
9587}
9588
96408a79
SA
9589/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9590
9591static void
9592read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9593{
9594 struct objfile *objfile = cu->objfile;
9595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9596 CORE_ADDR pc, baseaddr;
9597 struct attribute *attr;
9598 struct call_site *call_site, call_site_local;
9599 void **slot;
9600 int nparams;
9601 struct die_info *child_die;
9602
9603 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9604
9605 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9606 if (!attr)
9607 {
9608 complaint (&symfile_complaints,
9609 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9610 "DIE 0x%x [in module %s]"),
b64f50a1 9611 die->offset.sect_off, objfile->name);
96408a79
SA
9612 return;
9613 }
9614 pc = DW_ADDR (attr) + baseaddr;
9615
9616 if (cu->call_site_htab == NULL)
9617 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9618 NULL, &objfile->objfile_obstack,
9619 hashtab_obstack_allocate, NULL);
9620 call_site_local.pc = pc;
9621 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9622 if (*slot != NULL)
9623 {
9624 complaint (&symfile_complaints,
9625 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9626 "DIE 0x%x [in module %s]"),
b64f50a1 9627 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9628 return;
9629 }
9630
9631 /* Count parameters at the caller. */
9632
9633 nparams = 0;
9634 for (child_die = die->child; child_die && child_die->tag;
9635 child_die = sibling_die (child_die))
9636 {
9637 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9638 {
9639 complaint (&symfile_complaints,
9640 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9641 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9642 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9643 continue;
9644 }
9645
9646 nparams++;
9647 }
9648
9649 call_site = obstack_alloc (&objfile->objfile_obstack,
9650 (sizeof (*call_site)
9651 + (sizeof (*call_site->parameter)
9652 * (nparams - 1))));
9653 *slot = call_site;
9654 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9655 call_site->pc = pc;
9656
9657 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9658 {
9659 struct die_info *func_die;
9660
9661 /* Skip also over DW_TAG_inlined_subroutine. */
9662 for (func_die = die->parent;
9663 func_die && func_die->tag != DW_TAG_subprogram
9664 && func_die->tag != DW_TAG_subroutine_type;
9665 func_die = func_die->parent);
9666
9667 /* DW_AT_GNU_all_call_sites is a superset
9668 of DW_AT_GNU_all_tail_call_sites. */
9669 if (func_die
9670 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9671 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9672 {
9673 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9674 not complete. But keep CALL_SITE for look ups via call_site_htab,
9675 both the initial caller containing the real return address PC and
9676 the final callee containing the current PC of a chain of tail
9677 calls do not need to have the tail call list complete. But any
9678 function candidate for a virtual tail call frame searched via
9679 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9680 determined unambiguously. */
9681 }
9682 else
9683 {
9684 struct type *func_type = NULL;
9685
9686 if (func_die)
9687 func_type = get_die_type (func_die, cu);
9688 if (func_type != NULL)
9689 {
9690 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9691
9692 /* Enlist this call site to the function. */
9693 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9694 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9695 }
9696 else
9697 complaint (&symfile_complaints,
9698 _("Cannot find function owning DW_TAG_GNU_call_site "
9699 "DIE 0x%x [in module %s]"),
b64f50a1 9700 die->offset.sect_off, objfile->name);
96408a79
SA
9701 }
9702 }
9703
9704 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9705 if (attr == NULL)
9706 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9707 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9708 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9709 /* Keep NULL DWARF_BLOCK. */;
9710 else if (attr_form_is_block (attr))
9711 {
9712 struct dwarf2_locexpr_baton *dlbaton;
9713
9714 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9715 dlbaton->data = DW_BLOCK (attr)->data;
9716 dlbaton->size = DW_BLOCK (attr)->size;
9717 dlbaton->per_cu = cu->per_cu;
9718
9719 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9720 }
9721 else if (is_ref_attr (attr))
9722 {
96408a79
SA
9723 struct dwarf2_cu *target_cu = cu;
9724 struct die_info *target_die;
9725
9726 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9727 gdb_assert (target_cu->objfile == objfile);
9728 if (die_is_declaration (target_die, target_cu))
9729 {
9730 const char *target_physname;
9731
9732 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9733 if (target_physname == NULL)
9734 complaint (&symfile_complaints,
9735 _("DW_AT_GNU_call_site_target target DIE has invalid "
9736 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9737 die->offset.sect_off, objfile->name);
96408a79
SA
9738 else
9739 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9740 }
9741 else
9742 {
9743 CORE_ADDR lowpc;
9744
9745 /* DW_AT_entry_pc should be preferred. */
9746 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9747 complaint (&symfile_complaints,
9748 _("DW_AT_GNU_call_site_target target DIE has invalid "
9749 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9750 die->offset.sect_off, objfile->name);
96408a79
SA
9751 else
9752 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9753 }
9754 }
9755 else
9756 complaint (&symfile_complaints,
9757 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9758 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9759 die->offset.sect_off, objfile->name);
96408a79
SA
9760
9761 call_site->per_cu = cu->per_cu;
9762
9763 for (child_die = die->child;
9764 child_die && child_die->tag;
9765 child_die = sibling_die (child_die))
9766 {
96408a79 9767 struct call_site_parameter *parameter;
1788b2d3 9768 struct attribute *loc, *origin;
96408a79
SA
9769
9770 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9771 {
9772 /* Already printed the complaint above. */
9773 continue;
9774 }
9775
9776 gdb_assert (call_site->parameter_count < nparams);
9777 parameter = &call_site->parameter[call_site->parameter_count];
9778
1788b2d3
JK
9779 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9780 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9781 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9782
24c5c679 9783 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9784 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9785 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9786 {
9787 sect_offset offset;
9788
9789 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9790 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9791 if (!offset_in_cu_p (&cu->header, offset))
9792 {
9793 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9794 binding can be done only inside one CU. Such referenced DIE
9795 therefore cannot be even moved to DW_TAG_partial_unit. */
9796 complaint (&symfile_complaints,
9797 _("DW_AT_abstract_origin offset is not in CU for "
9798 "DW_TAG_GNU_call_site child DIE 0x%x "
9799 "[in module %s]"),
9800 child_die->offset.sect_off, objfile->name);
9801 continue;
9802 }
1788b2d3
JK
9803 parameter->u.param_offset.cu_off = (offset.sect_off
9804 - cu->header.offset.sect_off);
9805 }
9806 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9807 {
9808 complaint (&symfile_complaints,
9809 _("No DW_FORM_block* DW_AT_location for "
9810 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9811 child_die->offset.sect_off, objfile->name);
96408a79
SA
9812 continue;
9813 }
24c5c679 9814 else
96408a79 9815 {
24c5c679
JK
9816 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9817 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9818 if (parameter->u.dwarf_reg != -1)
9819 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9820 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9821 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9822 &parameter->u.fb_offset))
9823 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9824 else
9825 {
9826 complaint (&symfile_complaints,
9827 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9828 "for DW_FORM_block* DW_AT_location is supported for "
9829 "DW_TAG_GNU_call_site child DIE 0x%x "
9830 "[in module %s]"),
9831 child_die->offset.sect_off, objfile->name);
9832 continue;
9833 }
96408a79
SA
9834 }
9835
9836 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9837 if (!attr_form_is_block (attr))
9838 {
9839 complaint (&symfile_complaints,
9840 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9841 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9842 child_die->offset.sect_off, objfile->name);
96408a79
SA
9843 continue;
9844 }
9845 parameter->value = DW_BLOCK (attr)->data;
9846 parameter->value_size = DW_BLOCK (attr)->size;
9847
9848 /* Parameters are not pre-cleared by memset above. */
9849 parameter->data_value = NULL;
9850 parameter->data_value_size = 0;
9851 call_site->parameter_count++;
9852
9853 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9854 if (attr)
9855 {
9856 if (!attr_form_is_block (attr))
9857 complaint (&symfile_complaints,
9858 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9859 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9860 child_die->offset.sect_off, objfile->name);
96408a79
SA
9861 else
9862 {
9863 parameter->data_value = DW_BLOCK (attr)->data;
9864 parameter->data_value_size = DW_BLOCK (attr)->size;
9865 }
9866 }
9867 }
9868}
9869
43039443 9870/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9871 Return 1 if the attributes are present and valid, otherwise, return 0.
9872 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9873
9874static int
9875dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9876 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9877 struct partial_symtab *ranges_pst)
43039443
JK
9878{
9879 struct objfile *objfile = cu->objfile;
9880 struct comp_unit_head *cu_header = &cu->header;
9881 bfd *obfd = objfile->obfd;
9882 unsigned int addr_size = cu_header->addr_size;
9883 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9884 /* Base address selection entry. */
9885 CORE_ADDR base;
9886 int found_base;
9887 unsigned int dummy;
9888 gdb_byte *buffer;
9889 CORE_ADDR marker;
9890 int low_set;
9891 CORE_ADDR low = 0;
9892 CORE_ADDR high = 0;
ff013f42 9893 CORE_ADDR baseaddr;
43039443 9894
d00adf39
DE
9895 found_base = cu->base_known;
9896 base = cu->base_address;
43039443 9897
be391dca 9898 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9899 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9900 {
9901 complaint (&symfile_complaints,
9902 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9903 offset);
9904 return 0;
9905 }
dce234bc 9906 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9907
9908 /* Read in the largest possible address. */
9909 marker = read_address (obfd, buffer, cu, &dummy);
9910 if ((marker & mask) == mask)
9911 {
9912 /* If we found the largest possible address, then
9913 read the base address. */
9914 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9915 buffer += 2 * addr_size;
9916 offset += 2 * addr_size;
9917 found_base = 1;
9918 }
9919
9920 low_set = 0;
9921
e7030f15 9922 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9923
43039443
JK
9924 while (1)
9925 {
9926 CORE_ADDR range_beginning, range_end;
9927
9928 range_beginning = read_address (obfd, buffer, cu, &dummy);
9929 buffer += addr_size;
9930 range_end = read_address (obfd, buffer, cu, &dummy);
9931 buffer += addr_size;
9932 offset += 2 * addr_size;
9933
9934 /* An end of list marker is a pair of zero addresses. */
9935 if (range_beginning == 0 && range_end == 0)
9936 /* Found the end of list entry. */
9937 break;
9938
9939 /* Each base address selection entry is a pair of 2 values.
9940 The first is the largest possible address, the second is
9941 the base address. Check for a base address here. */
9942 if ((range_beginning & mask) == mask)
9943 {
9944 /* If we found the largest possible address, then
9945 read the base address. */
9946 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9947 found_base = 1;
9948 continue;
9949 }
9950
9951 if (!found_base)
9952 {
9953 /* We have no valid base address for the ranges
9954 data. */
9955 complaint (&symfile_complaints,
9956 _("Invalid .debug_ranges data (no base address)"));
9957 return 0;
9958 }
9959
9277c30c
UW
9960 if (range_beginning > range_end)
9961 {
9962 /* Inverted range entries are invalid. */
9963 complaint (&symfile_complaints,
9964 _("Invalid .debug_ranges data (inverted range)"));
9965 return 0;
9966 }
9967
9968 /* Empty range entries have no effect. */
9969 if (range_beginning == range_end)
9970 continue;
9971
43039443
JK
9972 range_beginning += base;
9973 range_end += base;
9974
01093045
DE
9975 /* A not-uncommon case of bad debug info.
9976 Don't pollute the addrmap with bad data. */
9977 if (range_beginning + baseaddr == 0
9978 && !dwarf2_per_objfile->has_section_at_zero)
9979 {
9980 complaint (&symfile_complaints,
9981 _(".debug_ranges entry has start address of zero"
9982 " [in module %s]"), objfile->name);
9983 continue;
9984 }
9985
9277c30c 9986 if (ranges_pst != NULL)
ff013f42 9987 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
9988 range_beginning + baseaddr,
9989 range_end - 1 + baseaddr,
ff013f42
JK
9990 ranges_pst);
9991
43039443
JK
9992 /* FIXME: This is recording everything as a low-high
9993 segment of consecutive addresses. We should have a
9994 data structure for discontiguous block ranges
9995 instead. */
9996 if (! low_set)
9997 {
9998 low = range_beginning;
9999 high = range_end;
10000 low_set = 1;
10001 }
10002 else
10003 {
10004 if (range_beginning < low)
10005 low = range_beginning;
10006 if (range_end > high)
10007 high = range_end;
10008 }
10009 }
10010
10011 if (! low_set)
10012 /* If the first entry is an end-of-list marker, the range
10013 describes an empty scope, i.e. no instructions. */
10014 return 0;
10015
10016 if (low_return)
10017 *low_return = low;
10018 if (high_return)
10019 *high_return = high;
10020 return 1;
10021}
10022
af34e669
DJ
10023/* Get low and high pc attributes from a die. Return 1 if the attributes
10024 are present and valid, otherwise, return 0. Return -1 if the range is
10025 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10026
c906108c 10027static int
af34e669 10028dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10029 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10030 struct partial_symtab *pst)
c906108c
SS
10031{
10032 struct attribute *attr;
91da1414 10033 struct attribute *attr_high;
af34e669
DJ
10034 CORE_ADDR low = 0;
10035 CORE_ADDR high = 0;
10036 int ret = 0;
c906108c 10037
91da1414
MW
10038 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10039 if (attr_high)
af34e669 10040 {
e142c38c 10041 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10042 if (attr)
91da1414
MW
10043 {
10044 low = DW_ADDR (attr);
3019eac3
DE
10045 if (attr_high->form == DW_FORM_addr
10046 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10047 high = DW_ADDR (attr_high);
10048 else
10049 high = low + DW_UNSND (attr_high);
10050 }
af34e669
DJ
10051 else
10052 /* Found high w/o low attribute. */
10053 return 0;
10054
10055 /* Found consecutive range of addresses. */
10056 ret = 1;
10057 }
c906108c 10058 else
af34e669 10059 {
e142c38c 10060 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10061 if (attr != NULL)
10062 {
ab435259
DE
10063 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10064 We take advantage of the fact that DW_AT_ranges does not appear
10065 in DW_TAG_compile_unit of DWO files. */
10066 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10067 unsigned int ranges_offset = (DW_UNSND (attr)
10068 + (need_ranges_base
10069 ? cu->ranges_base
10070 : 0));
2e3cf129 10071
af34e669 10072 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10073 .debug_ranges section. */
2e3cf129 10074 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10075 return 0;
43039443 10076 /* Found discontinuous range of addresses. */
af34e669
DJ
10077 ret = -1;
10078 }
10079 }
c906108c 10080
9373cf26
JK
10081 /* read_partial_die has also the strict LOW < HIGH requirement. */
10082 if (high <= low)
c906108c
SS
10083 return 0;
10084
10085 /* When using the GNU linker, .gnu.linkonce. sections are used to
10086 eliminate duplicate copies of functions and vtables and such.
10087 The linker will arbitrarily choose one and discard the others.
10088 The AT_*_pc values for such functions refer to local labels in
10089 these sections. If the section from that file was discarded, the
10090 labels are not in the output, so the relocs get a value of 0.
10091 If this is a discarded function, mark the pc bounds as invalid,
10092 so that GDB will ignore it. */
72dca2f5 10093 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10094 return 0;
10095
10096 *lowpc = low;
96408a79
SA
10097 if (highpc)
10098 *highpc = high;
af34e669 10099 return ret;
c906108c
SS
10100}
10101
b084d499
JB
10102/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10103 its low and high PC addresses. Do nothing if these addresses could not
10104 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10105 and HIGHPC to the high address if greater than HIGHPC. */
10106
10107static void
10108dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10109 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10110 struct dwarf2_cu *cu)
10111{
10112 CORE_ADDR low, high;
10113 struct die_info *child = die->child;
10114
d85a05f0 10115 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10116 {
10117 *lowpc = min (*lowpc, low);
10118 *highpc = max (*highpc, high);
10119 }
10120
10121 /* If the language does not allow nested subprograms (either inside
10122 subprograms or lexical blocks), we're done. */
10123 if (cu->language != language_ada)
10124 return;
6e70227d 10125
b084d499
JB
10126 /* Check all the children of the given DIE. If it contains nested
10127 subprograms, then check their pc bounds. Likewise, we need to
10128 check lexical blocks as well, as they may also contain subprogram
10129 definitions. */
10130 while (child && child->tag)
10131 {
10132 if (child->tag == DW_TAG_subprogram
10133 || child->tag == DW_TAG_lexical_block)
10134 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10135 child = sibling_die (child);
10136 }
10137}
10138
fae299cd
DC
10139/* Get the low and high pc's represented by the scope DIE, and store
10140 them in *LOWPC and *HIGHPC. If the correct values can't be
10141 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10142
10143static void
10144get_scope_pc_bounds (struct die_info *die,
10145 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10146 struct dwarf2_cu *cu)
10147{
10148 CORE_ADDR best_low = (CORE_ADDR) -1;
10149 CORE_ADDR best_high = (CORE_ADDR) 0;
10150 CORE_ADDR current_low, current_high;
10151
d85a05f0 10152 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10153 {
10154 best_low = current_low;
10155 best_high = current_high;
10156 }
10157 else
10158 {
10159 struct die_info *child = die->child;
10160
10161 while (child && child->tag)
10162 {
10163 switch (child->tag) {
10164 case DW_TAG_subprogram:
b084d499 10165 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10166 break;
10167 case DW_TAG_namespace:
f55ee35c 10168 case DW_TAG_module:
fae299cd
DC
10169 /* FIXME: carlton/2004-01-16: Should we do this for
10170 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10171 that current GCC's always emit the DIEs corresponding
10172 to definitions of methods of classes as children of a
10173 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10174 the DIEs giving the declarations, which could be
10175 anywhere). But I don't see any reason why the
10176 standards says that they have to be there. */
10177 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10178
10179 if (current_low != ((CORE_ADDR) -1))
10180 {
10181 best_low = min (best_low, current_low);
10182 best_high = max (best_high, current_high);
10183 }
10184 break;
10185 default:
0963b4bd 10186 /* Ignore. */
fae299cd
DC
10187 break;
10188 }
10189
10190 child = sibling_die (child);
10191 }
10192 }
10193
10194 *lowpc = best_low;
10195 *highpc = best_high;
10196}
10197
801e3a5b
JB
10198/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10199 in DIE. */
380bca97 10200
801e3a5b
JB
10201static void
10202dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10203 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10204{
bb5ed363 10205 struct objfile *objfile = cu->objfile;
801e3a5b 10206 struct attribute *attr;
91da1414 10207 struct attribute *attr_high;
801e3a5b 10208
91da1414
MW
10209 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10210 if (attr_high)
801e3a5b 10211 {
801e3a5b
JB
10212 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10213 if (attr)
10214 {
10215 CORE_ADDR low = DW_ADDR (attr);
91da1414 10216 CORE_ADDR high;
3019eac3
DE
10217 if (attr_high->form == DW_FORM_addr
10218 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10219 high = DW_ADDR (attr_high);
10220 else
10221 high = low + DW_UNSND (attr_high);
9a619af0 10222
801e3a5b
JB
10223 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10224 }
10225 }
10226
10227 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10228 if (attr)
10229 {
bb5ed363 10230 bfd *obfd = objfile->obfd;
ab435259
DE
10231 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10232 We take advantage of the fact that DW_AT_ranges does not appear
10233 in DW_TAG_compile_unit of DWO files. */
10234 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10235
10236 /* The value of the DW_AT_ranges attribute is the offset of the
10237 address range list in the .debug_ranges section. */
ab435259
DE
10238 unsigned long offset = (DW_UNSND (attr)
10239 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10240 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10241
10242 /* For some target architectures, but not others, the
10243 read_address function sign-extends the addresses it returns.
10244 To recognize base address selection entries, we need a
10245 mask. */
10246 unsigned int addr_size = cu->header.addr_size;
10247 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10248
10249 /* The base address, to which the next pair is relative. Note
10250 that this 'base' is a DWARF concept: most entries in a range
10251 list are relative, to reduce the number of relocs against the
10252 debugging information. This is separate from this function's
10253 'baseaddr' argument, which GDB uses to relocate debugging
10254 information from a shared library based on the address at
10255 which the library was loaded. */
d00adf39
DE
10256 CORE_ADDR base = cu->base_address;
10257 int base_known = cu->base_known;
801e3a5b 10258
be391dca 10259 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10260 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10261 {
10262 complaint (&symfile_complaints,
10263 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10264 offset);
10265 return;
10266 }
10267
10268 for (;;)
10269 {
10270 unsigned int bytes_read;
10271 CORE_ADDR start, end;
10272
10273 start = read_address (obfd, buffer, cu, &bytes_read);
10274 buffer += bytes_read;
10275 end = read_address (obfd, buffer, cu, &bytes_read);
10276 buffer += bytes_read;
10277
10278 /* Did we find the end of the range list? */
10279 if (start == 0 && end == 0)
10280 break;
10281
10282 /* Did we find a base address selection entry? */
10283 else if ((start & base_select_mask) == base_select_mask)
10284 {
10285 base = end;
10286 base_known = 1;
10287 }
10288
10289 /* We found an ordinary address range. */
10290 else
10291 {
10292 if (!base_known)
10293 {
10294 complaint (&symfile_complaints,
3e43a32a
MS
10295 _("Invalid .debug_ranges data "
10296 "(no base address)"));
801e3a5b
JB
10297 return;
10298 }
10299
9277c30c
UW
10300 if (start > end)
10301 {
10302 /* Inverted range entries are invalid. */
10303 complaint (&symfile_complaints,
10304 _("Invalid .debug_ranges data "
10305 "(inverted range)"));
10306 return;
10307 }
10308
10309 /* Empty range entries have no effect. */
10310 if (start == end)
10311 continue;
10312
01093045
DE
10313 start += base + baseaddr;
10314 end += base + baseaddr;
10315
10316 /* A not-uncommon case of bad debug info.
10317 Don't pollute the addrmap with bad data. */
10318 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10319 {
10320 complaint (&symfile_complaints,
10321 _(".debug_ranges entry has start address of zero"
10322 " [in module %s]"), objfile->name);
10323 continue;
10324 }
10325
10326 record_block_range (block, start, end - 1);
801e3a5b
JB
10327 }
10328 }
10329 }
10330}
10331
685b1105
JK
10332/* Check whether the producer field indicates either of GCC < 4.6, or the
10333 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10334
685b1105
JK
10335static void
10336check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10337{
10338 const char *cs;
10339 int major, minor, release;
10340
10341 if (cu->producer == NULL)
10342 {
10343 /* For unknown compilers expect their behavior is DWARF version
10344 compliant.
10345
10346 GCC started to support .debug_types sections by -gdwarf-4 since
10347 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10348 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10349 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10350 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10351 }
685b1105 10352 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10353 {
685b1105
JK
10354 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10355
ba919b58
TT
10356 cs = &cu->producer[strlen ("GNU ")];
10357 while (*cs && !isdigit (*cs))
10358 cs++;
10359 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10360 {
10361 /* Not recognized as GCC. */
10362 }
10363 else
1b80a9fa
JK
10364 {
10365 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10366 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10367 }
685b1105
JK
10368 }
10369 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10370 cu->producer_is_icc = 1;
10371 else
10372 {
10373 /* For other non-GCC compilers, expect their behavior is DWARF version
10374 compliant. */
60d5a603
JK
10375 }
10376
ba919b58 10377 cu->checked_producer = 1;
685b1105 10378}
ba919b58 10379
685b1105
JK
10380/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10381 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10382 during 4.6.0 experimental. */
10383
10384static int
10385producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10386{
10387 if (!cu->checked_producer)
10388 check_producer (cu);
10389
10390 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10391}
10392
10393/* Return the default accessibility type if it is not overriden by
10394 DW_AT_accessibility. */
10395
10396static enum dwarf_access_attribute
10397dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10398{
10399 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10400 {
10401 /* The default DWARF 2 accessibility for members is public, the default
10402 accessibility for inheritance is private. */
10403
10404 if (die->tag != DW_TAG_inheritance)
10405 return DW_ACCESS_public;
10406 else
10407 return DW_ACCESS_private;
10408 }
10409 else
10410 {
10411 /* DWARF 3+ defines the default accessibility a different way. The same
10412 rules apply now for DW_TAG_inheritance as for the members and it only
10413 depends on the container kind. */
10414
10415 if (die->parent->tag == DW_TAG_class_type)
10416 return DW_ACCESS_private;
10417 else
10418 return DW_ACCESS_public;
10419 }
10420}
10421
74ac6d43
TT
10422/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10423 offset. If the attribute was not found return 0, otherwise return
10424 1. If it was found but could not properly be handled, set *OFFSET
10425 to 0. */
10426
10427static int
10428handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10429 LONGEST *offset)
10430{
10431 struct attribute *attr;
10432
10433 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10434 if (attr != NULL)
10435 {
10436 *offset = 0;
10437
10438 /* Note that we do not check for a section offset first here.
10439 This is because DW_AT_data_member_location is new in DWARF 4,
10440 so if we see it, we can assume that a constant form is really
10441 a constant and not a section offset. */
10442 if (attr_form_is_constant (attr))
10443 *offset = dwarf2_get_attr_constant_value (attr, 0);
10444 else if (attr_form_is_section_offset (attr))
10445 dwarf2_complex_location_expr_complaint ();
10446 else if (attr_form_is_block (attr))
10447 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10448 else
10449 dwarf2_complex_location_expr_complaint ();
10450
10451 return 1;
10452 }
10453
10454 return 0;
10455}
10456
c906108c
SS
10457/* Add an aggregate field to the field list. */
10458
10459static void
107d2387 10460dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10461 struct dwarf2_cu *cu)
6e70227d 10462{
e7c27a73 10463 struct objfile *objfile = cu->objfile;
5e2b427d 10464 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10465 struct nextfield *new_field;
10466 struct attribute *attr;
10467 struct field *fp;
10468 char *fieldname = "";
10469
10470 /* Allocate a new field list entry and link it in. */
10471 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10472 make_cleanup (xfree, new_field);
c906108c 10473 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10474
10475 if (die->tag == DW_TAG_inheritance)
10476 {
10477 new_field->next = fip->baseclasses;
10478 fip->baseclasses = new_field;
10479 }
10480 else
10481 {
10482 new_field->next = fip->fields;
10483 fip->fields = new_field;
10484 }
c906108c
SS
10485 fip->nfields++;
10486
e142c38c 10487 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10488 if (attr)
10489 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10490 else
10491 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10492 if (new_field->accessibility != DW_ACCESS_public)
10493 fip->non_public_fields = 1;
60d5a603 10494
e142c38c 10495 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10496 if (attr)
10497 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10498 else
10499 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10500
10501 fp = &new_field->field;
a9a9bd0f 10502
e142c38c 10503 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10504 {
74ac6d43
TT
10505 LONGEST offset;
10506
a9a9bd0f 10507 /* Data member other than a C++ static data member. */
6e70227d 10508
c906108c 10509 /* Get type of field. */
e7c27a73 10510 fp->type = die_type (die, cu);
c906108c 10511
d6a843b5 10512 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10513
c906108c 10514 /* Get bit size of field (zero if none). */
e142c38c 10515 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10516 if (attr)
10517 {
10518 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10519 }
10520 else
10521 {
10522 FIELD_BITSIZE (*fp) = 0;
10523 }
10524
10525 /* Get bit offset of field. */
74ac6d43
TT
10526 if (handle_data_member_location (die, cu, &offset))
10527 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10528 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10529 if (attr)
10530 {
5e2b427d 10531 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10532 {
10533 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10534 additional bit offset from the MSB of the containing
10535 anonymous object to the MSB of the field. We don't
10536 have to do anything special since we don't need to
10537 know the size of the anonymous object. */
f41f5e61 10538 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10539 }
10540 else
10541 {
10542 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10543 MSB of the anonymous object, subtract off the number of
10544 bits from the MSB of the field to the MSB of the
10545 object, and then subtract off the number of bits of
10546 the field itself. The result is the bit offset of
10547 the LSB of the field. */
c906108c
SS
10548 int anonymous_size;
10549 int bit_offset = DW_UNSND (attr);
10550
e142c38c 10551 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10552 if (attr)
10553 {
10554 /* The size of the anonymous object containing
10555 the bit field is explicit, so use the
10556 indicated size (in bytes). */
10557 anonymous_size = DW_UNSND (attr);
10558 }
10559 else
10560 {
10561 /* The size of the anonymous object containing
10562 the bit field must be inferred from the type
10563 attribute of the data member containing the
10564 bit field. */
10565 anonymous_size = TYPE_LENGTH (fp->type);
10566 }
f41f5e61
PA
10567 SET_FIELD_BITPOS (*fp,
10568 (FIELD_BITPOS (*fp)
10569 + anonymous_size * bits_per_byte
10570 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10571 }
10572 }
10573
10574 /* Get name of field. */
39cbfefa
DJ
10575 fieldname = dwarf2_name (die, cu);
10576 if (fieldname == NULL)
10577 fieldname = "";
d8151005
DJ
10578
10579 /* The name is already allocated along with this objfile, so we don't
10580 need to duplicate it for the type. */
10581 fp->name = fieldname;
c906108c
SS
10582
10583 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10584 pointer or virtual base class pointer) to private. */
e142c38c 10585 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10586 {
d48cc9dd 10587 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10588 new_field->accessibility = DW_ACCESS_private;
10589 fip->non_public_fields = 1;
10590 }
10591 }
a9a9bd0f 10592 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10593 {
a9a9bd0f
DC
10594 /* C++ static member. */
10595
10596 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10597 is a declaration, but all versions of G++ as of this writing
10598 (so through at least 3.2.1) incorrectly generate
10599 DW_TAG_variable tags. */
6e70227d 10600
ff355380 10601 const char *physname;
c906108c 10602
a9a9bd0f 10603 /* Get name of field. */
39cbfefa
DJ
10604 fieldname = dwarf2_name (die, cu);
10605 if (fieldname == NULL)
c906108c
SS
10606 return;
10607
254e6b9e 10608 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10609 if (attr
10610 /* Only create a symbol if this is an external value.
10611 new_symbol checks this and puts the value in the global symbol
10612 table, which we want. If it is not external, new_symbol
10613 will try to put the value in cu->list_in_scope which is wrong. */
10614 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10615 {
10616 /* A static const member, not much different than an enum as far as
10617 we're concerned, except that we can support more types. */
10618 new_symbol (die, NULL, cu);
10619 }
10620
2df3850c 10621 /* Get physical name. */
ff355380 10622 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10623
d8151005
DJ
10624 /* The name is already allocated along with this objfile, so we don't
10625 need to duplicate it for the type. */
10626 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10627 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10628 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10629 }
10630 else if (die->tag == DW_TAG_inheritance)
10631 {
74ac6d43 10632 LONGEST offset;
d4b96c9a 10633
74ac6d43
TT
10634 /* C++ base class field. */
10635 if (handle_data_member_location (die, cu, &offset))
10636 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10637 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10638 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10639 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10640 fip->nbaseclasses++;
10641 }
10642}
10643
98751a41
JK
10644/* Add a typedef defined in the scope of the FIP's class. */
10645
10646static void
10647dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10648 struct dwarf2_cu *cu)
6e70227d 10649{
98751a41 10650 struct objfile *objfile = cu->objfile;
98751a41
JK
10651 struct typedef_field_list *new_field;
10652 struct attribute *attr;
10653 struct typedef_field *fp;
10654 char *fieldname = "";
10655
10656 /* Allocate a new field list entry and link it in. */
10657 new_field = xzalloc (sizeof (*new_field));
10658 make_cleanup (xfree, new_field);
10659
10660 gdb_assert (die->tag == DW_TAG_typedef);
10661
10662 fp = &new_field->field;
10663
10664 /* Get name of field. */
10665 fp->name = dwarf2_name (die, cu);
10666 if (fp->name == NULL)
10667 return;
10668
10669 fp->type = read_type_die (die, cu);
10670
10671 new_field->next = fip->typedef_field_list;
10672 fip->typedef_field_list = new_field;
10673 fip->typedef_field_list_count++;
10674}
10675
c906108c
SS
10676/* Create the vector of fields, and attach it to the type. */
10677
10678static void
fba45db2 10679dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10680 struct dwarf2_cu *cu)
c906108c
SS
10681{
10682 int nfields = fip->nfields;
10683
10684 /* Record the field count, allocate space for the array of fields,
10685 and create blank accessibility bitfields if necessary. */
10686 TYPE_NFIELDS (type) = nfields;
10687 TYPE_FIELDS (type) = (struct field *)
10688 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10689 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10690
b4ba55a1 10691 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10692 {
10693 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10694
10695 TYPE_FIELD_PRIVATE_BITS (type) =
10696 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10697 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10698
10699 TYPE_FIELD_PROTECTED_BITS (type) =
10700 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10701 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10702
774b6a14
TT
10703 TYPE_FIELD_IGNORE_BITS (type) =
10704 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10705 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10706 }
10707
10708 /* If the type has baseclasses, allocate and clear a bit vector for
10709 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10710 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10711 {
10712 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10713 unsigned char *pointer;
c906108c
SS
10714
10715 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10716 pointer = TYPE_ALLOC (type, num_bytes);
10717 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10718 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10719 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10720 }
10721
3e43a32a
MS
10722 /* Copy the saved-up fields into the field vector. Start from the head of
10723 the list, adding to the tail of the field array, so that they end up in
10724 the same order in the array in which they were added to the list. */
c906108c
SS
10725 while (nfields-- > 0)
10726 {
7d0ccb61
DJ
10727 struct nextfield *fieldp;
10728
10729 if (fip->fields)
10730 {
10731 fieldp = fip->fields;
10732 fip->fields = fieldp->next;
10733 }
10734 else
10735 {
10736 fieldp = fip->baseclasses;
10737 fip->baseclasses = fieldp->next;
10738 }
10739
10740 TYPE_FIELD (type, nfields) = fieldp->field;
10741 switch (fieldp->accessibility)
c906108c 10742 {
c5aa993b 10743 case DW_ACCESS_private:
b4ba55a1
JB
10744 if (cu->language != language_ada)
10745 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10746 break;
c906108c 10747
c5aa993b 10748 case DW_ACCESS_protected:
b4ba55a1
JB
10749 if (cu->language != language_ada)
10750 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10751 break;
c906108c 10752
c5aa993b
JM
10753 case DW_ACCESS_public:
10754 break;
c906108c 10755
c5aa993b
JM
10756 default:
10757 /* Unknown accessibility. Complain and treat it as public. */
10758 {
e2e0b3e5 10759 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10760 fieldp->accessibility);
c5aa993b
JM
10761 }
10762 break;
c906108c
SS
10763 }
10764 if (nfields < fip->nbaseclasses)
10765 {
7d0ccb61 10766 switch (fieldp->virtuality)
c906108c 10767 {
c5aa993b
JM
10768 case DW_VIRTUALITY_virtual:
10769 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10770 if (cu->language == language_ada)
a73c6dcd 10771 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10772 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10773 break;
c906108c
SS
10774 }
10775 }
c906108c
SS
10776 }
10777}
10778
7d27a96d
TT
10779/* Return true if this member function is a constructor, false
10780 otherwise. */
10781
10782static int
10783dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10784{
10785 const char *fieldname;
10786 const char *typename;
10787 int len;
10788
10789 if (die->parent == NULL)
10790 return 0;
10791
10792 if (die->parent->tag != DW_TAG_structure_type
10793 && die->parent->tag != DW_TAG_union_type
10794 && die->parent->tag != DW_TAG_class_type)
10795 return 0;
10796
10797 fieldname = dwarf2_name (die, cu);
10798 typename = dwarf2_name (die->parent, cu);
10799 if (fieldname == NULL || typename == NULL)
10800 return 0;
10801
10802 len = strlen (fieldname);
10803 return (strncmp (fieldname, typename, len) == 0
10804 && (typename[len] == '\0' || typename[len] == '<'));
10805}
10806
c906108c
SS
10807/* Add a member function to the proper fieldlist. */
10808
10809static void
107d2387 10810dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10811 struct type *type, struct dwarf2_cu *cu)
c906108c 10812{
e7c27a73 10813 struct objfile *objfile = cu->objfile;
c906108c
SS
10814 struct attribute *attr;
10815 struct fnfieldlist *flp;
10816 int i;
10817 struct fn_field *fnp;
10818 char *fieldname;
c906108c 10819 struct nextfnfield *new_fnfield;
f792889a 10820 struct type *this_type;
60d5a603 10821 enum dwarf_access_attribute accessibility;
c906108c 10822
b4ba55a1 10823 if (cu->language == language_ada)
a73c6dcd 10824 error (_("unexpected member function in Ada type"));
b4ba55a1 10825
2df3850c 10826 /* Get name of member function. */
39cbfefa
DJ
10827 fieldname = dwarf2_name (die, cu);
10828 if (fieldname == NULL)
2df3850c 10829 return;
c906108c 10830
c906108c
SS
10831 /* Look up member function name in fieldlist. */
10832 for (i = 0; i < fip->nfnfields; i++)
10833 {
27bfe10e 10834 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10835 break;
10836 }
10837
10838 /* Create new list element if necessary. */
10839 if (i < fip->nfnfields)
10840 flp = &fip->fnfieldlists[i];
10841 else
10842 {
10843 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10844 {
10845 fip->fnfieldlists = (struct fnfieldlist *)
10846 xrealloc (fip->fnfieldlists,
10847 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10848 * sizeof (struct fnfieldlist));
c906108c 10849 if (fip->nfnfields == 0)
c13c43fd 10850 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10851 }
10852 flp = &fip->fnfieldlists[fip->nfnfields];
10853 flp->name = fieldname;
10854 flp->length = 0;
10855 flp->head = NULL;
3da10d80 10856 i = fip->nfnfields++;
c906108c
SS
10857 }
10858
10859 /* Create a new member function field and chain it to the field list
0963b4bd 10860 entry. */
c906108c 10861 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10862 make_cleanup (xfree, new_fnfield);
c906108c
SS
10863 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10864 new_fnfield->next = flp->head;
10865 flp->head = new_fnfield;
10866 flp->length++;
10867
10868 /* Fill in the member function field info. */
10869 fnp = &new_fnfield->fnfield;
3da10d80
KS
10870
10871 /* Delay processing of the physname until later. */
10872 if (cu->language == language_cplus || cu->language == language_java)
10873 {
10874 add_to_method_list (type, i, flp->length - 1, fieldname,
10875 die, cu);
10876 }
10877 else
10878 {
1d06ead6 10879 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10880 fnp->physname = physname ? physname : "";
10881 }
10882
c906108c 10883 fnp->type = alloc_type (objfile);
f792889a
DJ
10884 this_type = read_type_die (die, cu);
10885 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10886 {
f792889a 10887 int nparams = TYPE_NFIELDS (this_type);
c906108c 10888
f792889a 10889 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10890 of the method itself (TYPE_CODE_METHOD). */
10891 smash_to_method_type (fnp->type, type,
f792889a
DJ
10892 TYPE_TARGET_TYPE (this_type),
10893 TYPE_FIELDS (this_type),
10894 TYPE_NFIELDS (this_type),
10895 TYPE_VARARGS (this_type));
c906108c
SS
10896
10897 /* Handle static member functions.
c5aa993b 10898 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10899 member functions. G++ helps GDB by marking the first
10900 parameter for non-static member functions (which is the this
10901 pointer) as artificial. We obtain this information from
10902 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10903 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10904 fnp->voffset = VOFFSET_STATIC;
10905 }
10906 else
e2e0b3e5 10907 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10908 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10909
10910 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10911 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10912 fnp->fcontext = die_containing_type (die, cu);
c906108c 10913
3e43a32a
MS
10914 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10915 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10916
10917 /* Get accessibility. */
e142c38c 10918 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10919 if (attr)
60d5a603
JK
10920 accessibility = DW_UNSND (attr);
10921 else
10922 accessibility = dwarf2_default_access_attribute (die, cu);
10923 switch (accessibility)
c906108c 10924 {
60d5a603
JK
10925 case DW_ACCESS_private:
10926 fnp->is_private = 1;
10927 break;
10928 case DW_ACCESS_protected:
10929 fnp->is_protected = 1;
10930 break;
c906108c
SS
10931 }
10932
b02dede2 10933 /* Check for artificial methods. */
e142c38c 10934 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10935 if (attr && DW_UNSND (attr) != 0)
10936 fnp->is_artificial = 1;
10937
7d27a96d
TT
10938 fnp->is_constructor = dwarf2_is_constructor (die, cu);
10939
0d564a31 10940 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10941 function. For older versions of GCC, this is an offset in the
10942 appropriate virtual table, as specified by DW_AT_containing_type.
10943 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10944 to the object address. */
10945
e142c38c 10946 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10947 if (attr)
8e19ed76 10948 {
aec5aa8b 10949 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10950 {
aec5aa8b
TT
10951 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10952 {
10953 /* Old-style GCC. */
10954 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10955 }
10956 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10957 || (DW_BLOCK (attr)->size > 1
10958 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10959 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10960 {
10961 struct dwarf_block blk;
10962 int offset;
10963
10964 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10965 ? 1 : 2);
10966 blk.size = DW_BLOCK (attr)->size - offset;
10967 blk.data = DW_BLOCK (attr)->data + offset;
10968 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10969 if ((fnp->voffset % cu->header.addr_size) != 0)
10970 dwarf2_complex_location_expr_complaint ();
10971 else
10972 fnp->voffset /= cu->header.addr_size;
10973 fnp->voffset += 2;
10974 }
10975 else
10976 dwarf2_complex_location_expr_complaint ();
10977
10978 if (!fnp->fcontext)
10979 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10980 }
3690dd37 10981 else if (attr_form_is_section_offset (attr))
8e19ed76 10982 {
4d3c2250 10983 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10984 }
10985 else
10986 {
4d3c2250
KB
10987 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10988 fieldname);
8e19ed76 10989 }
0d564a31 10990 }
d48cc9dd
DJ
10991 else
10992 {
10993 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10994 if (attr && DW_UNSND (attr))
10995 {
10996 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10997 complaint (&symfile_complaints,
3e43a32a
MS
10998 _("Member function \"%s\" (offset %d) is virtual "
10999 "but the vtable offset is not specified"),
b64f50a1 11000 fieldname, die->offset.sect_off);
9655fd1a 11001 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11002 TYPE_CPLUS_DYNAMIC (type) = 1;
11003 }
11004 }
c906108c
SS
11005}
11006
11007/* Create the vector of member function fields, and attach it to the type. */
11008
11009static void
fba45db2 11010dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11011 struct dwarf2_cu *cu)
c906108c
SS
11012{
11013 struct fnfieldlist *flp;
c906108c
SS
11014 int i;
11015
b4ba55a1 11016 if (cu->language == language_ada)
a73c6dcd 11017 error (_("unexpected member functions in Ada type"));
b4ba55a1 11018
c906108c
SS
11019 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11020 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11021 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11022
11023 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11024 {
11025 struct nextfnfield *nfp = flp->head;
11026 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11027 int k;
11028
11029 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11030 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11031 fn_flp->fn_fields = (struct fn_field *)
11032 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11033 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11034 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11035 }
11036
11037 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11038}
11039
1168df01
JB
11040/* Returns non-zero if NAME is the name of a vtable member in CU's
11041 language, zero otherwise. */
11042static int
11043is_vtable_name (const char *name, struct dwarf2_cu *cu)
11044{
11045 static const char vptr[] = "_vptr";
987504bb 11046 static const char vtable[] = "vtable";
1168df01 11047
987504bb
JJ
11048 /* Look for the C++ and Java forms of the vtable. */
11049 if ((cu->language == language_java
11050 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11051 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11052 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11053 return 1;
11054
11055 return 0;
11056}
11057
c0dd20ea 11058/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11059 functions, with the ABI-specified layout. If TYPE describes
11060 such a structure, smash it into a member function type.
61049d3b
DJ
11061
11062 GCC shouldn't do this; it should just output pointer to member DIEs.
11063 This is GCC PR debug/28767. */
c0dd20ea 11064
0b92b5bb
TT
11065static void
11066quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11067{
0b92b5bb 11068 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11069
11070 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11071 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11072 return;
c0dd20ea
DJ
11073
11074 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11075 if (TYPE_FIELD_NAME (type, 0) == NULL
11076 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11077 || TYPE_FIELD_NAME (type, 1) == NULL
11078 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11079 return;
c0dd20ea
DJ
11080
11081 /* Find the type of the method. */
0b92b5bb 11082 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11083 if (pfn_type == NULL
11084 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11085 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11086 return;
c0dd20ea
DJ
11087
11088 /* Look for the "this" argument. */
11089 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11090 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11091 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11092 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11093 return;
c0dd20ea
DJ
11094
11095 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11096 new_type = alloc_type (objfile);
11097 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11098 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11099 TYPE_VARARGS (pfn_type));
0b92b5bb 11100 smash_to_methodptr_type (type, new_type);
c0dd20ea 11101}
1168df01 11102
685b1105
JK
11103/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11104 (icc). */
11105
11106static int
11107producer_is_icc (struct dwarf2_cu *cu)
11108{
11109 if (!cu->checked_producer)
11110 check_producer (cu);
11111
11112 return cu->producer_is_icc;
11113}
11114
c906108c 11115/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11116 (definition) to create a type for the structure or union. Fill in
11117 the type's name and general properties; the members will not be
11118 processed until process_structure_type.
c906108c 11119
c767944b
DJ
11120 NOTE: we need to call these functions regardless of whether or not the
11121 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11122 structure or union. This gets the type entered into our set of
11123 user defined types.
11124
11125 However, if the structure is incomplete (an opaque struct/union)
11126 then suppress creating a symbol table entry for it since gdb only
11127 wants to find the one with the complete definition. Note that if
11128 it is complete, we just call new_symbol, which does it's own
11129 checking about whether the struct/union is anonymous or not (and
11130 suppresses creating a symbol table entry itself). */
11131
f792889a 11132static struct type *
134d01f1 11133read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11134{
e7c27a73 11135 struct objfile *objfile = cu->objfile;
c906108c
SS
11136 struct type *type;
11137 struct attribute *attr;
39cbfefa 11138 char *name;
c906108c 11139
348e048f
DE
11140 /* If the definition of this type lives in .debug_types, read that type.
11141 Don't follow DW_AT_specification though, that will take us back up
11142 the chain and we want to go down. */
45e58e77 11143 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11144 if (attr)
11145 {
11146 struct dwarf2_cu *type_cu = cu;
11147 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11148
348e048f
DE
11149 /* We could just recurse on read_structure_type, but we need to call
11150 get_die_type to ensure only one type for this DIE is created.
11151 This is important, for example, because for c++ classes we need
11152 TYPE_NAME set which is only done by new_symbol. Blech. */
11153 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11154
11155 /* TYPE_CU may not be the same as CU.
11156 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11157 return set_die_type (die, type, cu);
11158 }
11159
c0dd20ea 11160 type = alloc_type (objfile);
c906108c 11161 INIT_CPLUS_SPECIFIC (type);
93311388 11162
39cbfefa
DJ
11163 name = dwarf2_name (die, cu);
11164 if (name != NULL)
c906108c 11165 {
987504bb
JJ
11166 if (cu->language == language_cplus
11167 || cu->language == language_java)
63d06c5c 11168 {
3da10d80
KS
11169 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11170
11171 /* dwarf2_full_name might have already finished building the DIE's
11172 type. If so, there is no need to continue. */
11173 if (get_die_type (die, cu) != NULL)
11174 return get_die_type (die, cu);
11175
11176 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11177 if (die->tag == DW_TAG_structure_type
11178 || die->tag == DW_TAG_class_type)
11179 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11180 }
11181 else
11182 {
d8151005
DJ
11183 /* The name is already allocated along with this objfile, so
11184 we don't need to duplicate it for the type. */
94af9270
KS
11185 TYPE_TAG_NAME (type) = (char *) name;
11186 if (die->tag == DW_TAG_class_type)
11187 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11188 }
c906108c
SS
11189 }
11190
11191 if (die->tag == DW_TAG_structure_type)
11192 {
11193 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11194 }
11195 else if (die->tag == DW_TAG_union_type)
11196 {
11197 TYPE_CODE (type) = TYPE_CODE_UNION;
11198 }
11199 else
11200 {
c906108c
SS
11201 TYPE_CODE (type) = TYPE_CODE_CLASS;
11202 }
11203
0cc2414c
TT
11204 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11205 TYPE_DECLARED_CLASS (type) = 1;
11206
e142c38c 11207 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11208 if (attr)
11209 {
11210 TYPE_LENGTH (type) = DW_UNSND (attr);
11211 }
11212 else
11213 {
11214 TYPE_LENGTH (type) = 0;
11215 }
11216
685b1105
JK
11217 if (producer_is_icc (cu))
11218 {
11219 /* ICC does not output the required DW_AT_declaration
11220 on incomplete types, but gives them a size of zero. */
11221 }
11222 else
11223 TYPE_STUB_SUPPORTED (type) = 1;
11224
dc718098 11225 if (die_is_declaration (die, cu))
876cecd0 11226 TYPE_STUB (type) = 1;
a6c727b2
DJ
11227 else if (attr == NULL && die->child == NULL
11228 && producer_is_realview (cu->producer))
11229 /* RealView does not output the required DW_AT_declaration
11230 on incomplete types. */
11231 TYPE_STUB (type) = 1;
dc718098 11232
c906108c
SS
11233 /* We need to add the type field to the die immediately so we don't
11234 infinitely recurse when dealing with pointers to the structure
0963b4bd 11235 type within the structure itself. */
1c379e20 11236 set_die_type (die, type, cu);
c906108c 11237
7e314c57
JK
11238 /* set_die_type should be already done. */
11239 set_descriptive_type (type, die, cu);
11240
c767944b
DJ
11241 return type;
11242}
11243
11244/* Finish creating a structure or union type, including filling in
11245 its members and creating a symbol for it. */
11246
11247static void
11248process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11249{
11250 struct objfile *objfile = cu->objfile;
11251 struct die_info *child_die = die->child;
11252 struct type *type;
11253
11254 type = get_die_type (die, cu);
11255 if (type == NULL)
11256 type = read_structure_type (die, cu);
11257
e142c38c 11258 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11259 {
11260 struct field_info fi;
11261 struct die_info *child_die;
34eaf542 11262 VEC (symbolp) *template_args = NULL;
c767944b 11263 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11264
11265 memset (&fi, 0, sizeof (struct field_info));
11266
639d11d3 11267 child_die = die->child;
c906108c
SS
11268
11269 while (child_die && child_die->tag)
11270 {
a9a9bd0f
DC
11271 if (child_die->tag == DW_TAG_member
11272 || child_die->tag == DW_TAG_variable)
c906108c 11273 {
a9a9bd0f
DC
11274 /* NOTE: carlton/2002-11-05: A C++ static data member
11275 should be a DW_TAG_member that is a declaration, but
11276 all versions of G++ as of this writing (so through at
11277 least 3.2.1) incorrectly generate DW_TAG_variable
11278 tags for them instead. */
e7c27a73 11279 dwarf2_add_field (&fi, child_die, cu);
c906108c 11280 }
8713b1b1 11281 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11282 {
0963b4bd 11283 /* C++ member function. */
e7c27a73 11284 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11285 }
11286 else if (child_die->tag == DW_TAG_inheritance)
11287 {
11288 /* C++ base class field. */
e7c27a73 11289 dwarf2_add_field (&fi, child_die, cu);
c906108c 11290 }
98751a41
JK
11291 else if (child_die->tag == DW_TAG_typedef)
11292 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11293 else if (child_die->tag == DW_TAG_template_type_param
11294 || child_die->tag == DW_TAG_template_value_param)
11295 {
11296 struct symbol *arg = new_symbol (child_die, NULL, cu);
11297
f1078f66
DJ
11298 if (arg != NULL)
11299 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11300 }
11301
c906108c
SS
11302 child_die = sibling_die (child_die);
11303 }
11304
34eaf542
TT
11305 /* Attach template arguments to type. */
11306 if (! VEC_empty (symbolp, template_args))
11307 {
11308 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11309 TYPE_N_TEMPLATE_ARGUMENTS (type)
11310 = VEC_length (symbolp, template_args);
11311 TYPE_TEMPLATE_ARGUMENTS (type)
11312 = obstack_alloc (&objfile->objfile_obstack,
11313 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11314 * sizeof (struct symbol *)));
11315 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11316 VEC_address (symbolp, template_args),
11317 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11318 * sizeof (struct symbol *)));
11319 VEC_free (symbolp, template_args);
11320 }
11321
c906108c
SS
11322 /* Attach fields and member functions to the type. */
11323 if (fi.nfields)
e7c27a73 11324 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11325 if (fi.nfnfields)
11326 {
e7c27a73 11327 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11328
c5aa993b 11329 /* Get the type which refers to the base class (possibly this
c906108c 11330 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11331 class from the DW_AT_containing_type attribute. This use of
11332 DW_AT_containing_type is a GNU extension. */
c906108c 11333
e142c38c 11334 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11335 {
e7c27a73 11336 struct type *t = die_containing_type (die, cu);
c906108c
SS
11337
11338 TYPE_VPTR_BASETYPE (type) = t;
11339 if (type == t)
11340 {
c906108c
SS
11341 int i;
11342
11343 /* Our own class provides vtbl ptr. */
11344 for (i = TYPE_NFIELDS (t) - 1;
11345 i >= TYPE_N_BASECLASSES (t);
11346 --i)
11347 {
0d5cff50 11348 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11349
1168df01 11350 if (is_vtable_name (fieldname, cu))
c906108c
SS
11351 {
11352 TYPE_VPTR_FIELDNO (type) = i;
11353 break;
11354 }
11355 }
11356
11357 /* Complain if virtual function table field not found. */
11358 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11359 complaint (&symfile_complaints,
3e43a32a
MS
11360 _("virtual function table pointer "
11361 "not found when defining class '%s'"),
4d3c2250
KB
11362 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11363 "");
c906108c
SS
11364 }
11365 else
11366 {
11367 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11368 }
11369 }
f6235d4c
EZ
11370 else if (cu->producer
11371 && strncmp (cu->producer,
11372 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11373 {
11374 /* The IBM XLC compiler does not provide direct indication
11375 of the containing type, but the vtable pointer is
11376 always named __vfp. */
11377
11378 int i;
11379
11380 for (i = TYPE_NFIELDS (type) - 1;
11381 i >= TYPE_N_BASECLASSES (type);
11382 --i)
11383 {
11384 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11385 {
11386 TYPE_VPTR_FIELDNO (type) = i;
11387 TYPE_VPTR_BASETYPE (type) = type;
11388 break;
11389 }
11390 }
11391 }
c906108c 11392 }
98751a41
JK
11393
11394 /* Copy fi.typedef_field_list linked list elements content into the
11395 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11396 if (fi.typedef_field_list)
11397 {
11398 int i = fi.typedef_field_list_count;
11399
a0d7a4ff 11400 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11401 TYPE_TYPEDEF_FIELD_ARRAY (type)
11402 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11403 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11404
11405 /* Reverse the list order to keep the debug info elements order. */
11406 while (--i >= 0)
11407 {
11408 struct typedef_field *dest, *src;
6e70227d 11409
98751a41
JK
11410 dest = &TYPE_TYPEDEF_FIELD (type, i);
11411 src = &fi.typedef_field_list->field;
11412 fi.typedef_field_list = fi.typedef_field_list->next;
11413 *dest = *src;
11414 }
11415 }
c767944b
DJ
11416
11417 do_cleanups (back_to);
eb2a6f42
TT
11418
11419 if (HAVE_CPLUS_STRUCT (type))
11420 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11421 }
63d06c5c 11422
bb5ed363 11423 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11424
90aeadfc
DC
11425 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11426 snapshots) has been known to create a die giving a declaration
11427 for a class that has, as a child, a die giving a definition for a
11428 nested class. So we have to process our children even if the
11429 current die is a declaration. Normally, of course, a declaration
11430 won't have any children at all. */
134d01f1 11431
90aeadfc
DC
11432 while (child_die != NULL && child_die->tag)
11433 {
11434 if (child_die->tag == DW_TAG_member
11435 || child_die->tag == DW_TAG_variable
34eaf542
TT
11436 || child_die->tag == DW_TAG_inheritance
11437 || child_die->tag == DW_TAG_template_value_param
11438 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11439 {
90aeadfc 11440 /* Do nothing. */
134d01f1 11441 }
90aeadfc
DC
11442 else
11443 process_die (child_die, cu);
134d01f1 11444
90aeadfc 11445 child_die = sibling_die (child_die);
134d01f1
DJ
11446 }
11447
fa4028e9
JB
11448 /* Do not consider external references. According to the DWARF standard,
11449 these DIEs are identified by the fact that they have no byte_size
11450 attribute, and a declaration attribute. */
11451 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11452 || !die_is_declaration (die, cu))
c767944b 11453 new_symbol (die, type, cu);
134d01f1
DJ
11454}
11455
11456/* Given a DW_AT_enumeration_type die, set its type. We do not
11457 complete the type's fields yet, or create any symbols. */
c906108c 11458
f792889a 11459static struct type *
134d01f1 11460read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11461{
e7c27a73 11462 struct objfile *objfile = cu->objfile;
c906108c 11463 struct type *type;
c906108c 11464 struct attribute *attr;
0114d602 11465 const char *name;
134d01f1 11466
348e048f
DE
11467 /* If the definition of this type lives in .debug_types, read that type.
11468 Don't follow DW_AT_specification though, that will take us back up
11469 the chain and we want to go down. */
45e58e77 11470 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11471 if (attr)
11472 {
11473 struct dwarf2_cu *type_cu = cu;
11474 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11475
348e048f 11476 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11477
11478 /* TYPE_CU may not be the same as CU.
11479 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11480 return set_die_type (die, type, cu);
11481 }
11482
c906108c
SS
11483 type = alloc_type (objfile);
11484
11485 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11486 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11487 if (name != NULL)
0114d602 11488 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11489
e142c38c 11490 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11491 if (attr)
11492 {
11493 TYPE_LENGTH (type) = DW_UNSND (attr);
11494 }
11495 else
11496 {
11497 TYPE_LENGTH (type) = 0;
11498 }
11499
137033e9
JB
11500 /* The enumeration DIE can be incomplete. In Ada, any type can be
11501 declared as private in the package spec, and then defined only
11502 inside the package body. Such types are known as Taft Amendment
11503 Types. When another package uses such a type, an incomplete DIE
11504 may be generated by the compiler. */
02eb380e 11505 if (die_is_declaration (die, cu))
876cecd0 11506 TYPE_STUB (type) = 1;
02eb380e 11507
f792889a 11508 return set_die_type (die, type, cu);
134d01f1
DJ
11509}
11510
11511/* Given a pointer to a die which begins an enumeration, process all
11512 the dies that define the members of the enumeration, and create the
11513 symbol for the enumeration type.
11514
11515 NOTE: We reverse the order of the element list. */
11516
11517static void
11518process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11519{
f792889a 11520 struct type *this_type;
134d01f1 11521
f792889a
DJ
11522 this_type = get_die_type (die, cu);
11523 if (this_type == NULL)
11524 this_type = read_enumeration_type (die, cu);
9dc481d3 11525
639d11d3 11526 if (die->child != NULL)
c906108c 11527 {
9dc481d3
DE
11528 struct die_info *child_die;
11529 struct symbol *sym;
11530 struct field *fields = NULL;
11531 int num_fields = 0;
11532 int unsigned_enum = 1;
11533 char *name;
cafec441
TT
11534 int flag_enum = 1;
11535 ULONGEST mask = 0;
9dc481d3 11536
639d11d3 11537 child_die = die->child;
c906108c
SS
11538 while (child_die && child_die->tag)
11539 {
11540 if (child_die->tag != DW_TAG_enumerator)
11541 {
e7c27a73 11542 process_die (child_die, cu);
c906108c
SS
11543 }
11544 else
11545 {
39cbfefa
DJ
11546 name = dwarf2_name (child_die, cu);
11547 if (name)
c906108c 11548 {
f792889a 11549 sym = new_symbol (child_die, this_type, cu);
c906108c 11550 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11551 {
11552 unsigned_enum = 0;
11553 flag_enum = 0;
11554 }
11555 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11556 flag_enum = 0;
11557 else
11558 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11559
11560 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11561 {
11562 fields = (struct field *)
11563 xrealloc (fields,
11564 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11565 * sizeof (struct field));
c906108c
SS
11566 }
11567
3567439c 11568 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11569 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11570 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11571 FIELD_BITSIZE (fields[num_fields]) = 0;
11572
11573 num_fields++;
11574 }
11575 }
11576
11577 child_die = sibling_die (child_die);
11578 }
11579
11580 if (num_fields)
11581 {
f792889a
DJ
11582 TYPE_NFIELDS (this_type) = num_fields;
11583 TYPE_FIELDS (this_type) = (struct field *)
11584 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11585 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11586 sizeof (struct field) * num_fields);
b8c9b27d 11587 xfree (fields);
c906108c
SS
11588 }
11589 if (unsigned_enum)
876cecd0 11590 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11591 if (flag_enum)
11592 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11593 }
134d01f1 11594
6c83ed52
TT
11595 /* If we are reading an enum from a .debug_types unit, and the enum
11596 is a declaration, and the enum is not the signatured type in the
11597 unit, then we do not want to add a symbol for it. Adding a
11598 symbol would in some cases obscure the true definition of the
11599 enum, giving users an incomplete type when the definition is
11600 actually available. Note that we do not want to do this for all
11601 enums which are just declarations, because C++0x allows forward
11602 enum declarations. */
3019eac3 11603 if (cu->per_cu->is_debug_types
6c83ed52
TT
11604 && die_is_declaration (die, cu))
11605 {
52dc124a 11606 struct signatured_type *sig_type;
6c83ed52 11607
52dc124a 11608 sig_type
6c83ed52 11609 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11610 cu->per_cu->info_or_types_section,
6c83ed52 11611 cu->per_cu->offset);
3019eac3
DE
11612 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11613 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11614 return;
11615 }
11616
f792889a 11617 new_symbol (die, this_type, cu);
c906108c
SS
11618}
11619
11620/* Extract all information from a DW_TAG_array_type DIE and put it in
11621 the DIE's type field. For now, this only handles one dimensional
11622 arrays. */
11623
f792889a 11624static struct type *
e7c27a73 11625read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11626{
e7c27a73 11627 struct objfile *objfile = cu->objfile;
c906108c 11628 struct die_info *child_die;
7e314c57 11629 struct type *type;
c906108c
SS
11630 struct type *element_type, *range_type, *index_type;
11631 struct type **range_types = NULL;
11632 struct attribute *attr;
11633 int ndim = 0;
11634 struct cleanup *back_to;
39cbfefa 11635 char *name;
c906108c 11636
e7c27a73 11637 element_type = die_type (die, cu);
c906108c 11638
7e314c57
JK
11639 /* The die_type call above may have already set the type for this DIE. */
11640 type = get_die_type (die, cu);
11641 if (type)
11642 return type;
11643
c906108c
SS
11644 /* Irix 6.2 native cc creates array types without children for
11645 arrays with unspecified length. */
639d11d3 11646 if (die->child == NULL)
c906108c 11647 {
46bf5051 11648 index_type = objfile_type (objfile)->builtin_int;
c906108c 11649 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11650 type = create_array_type (NULL, element_type, range_type);
11651 return set_die_type (die, type, cu);
c906108c
SS
11652 }
11653
11654 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11655 child_die = die->child;
c906108c
SS
11656 while (child_die && child_die->tag)
11657 {
11658 if (child_die->tag == DW_TAG_subrange_type)
11659 {
f792889a 11660 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11661
f792889a 11662 if (child_type != NULL)
a02abb62 11663 {
0963b4bd
MS
11664 /* The range type was succesfully read. Save it for the
11665 array type creation. */
a02abb62
JB
11666 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11667 {
11668 range_types = (struct type **)
11669 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11670 * sizeof (struct type *));
11671 if (ndim == 0)
11672 make_cleanup (free_current_contents, &range_types);
11673 }
f792889a 11674 range_types[ndim++] = child_type;
a02abb62 11675 }
c906108c
SS
11676 }
11677 child_die = sibling_die (child_die);
11678 }
11679
11680 /* Dwarf2 dimensions are output from left to right, create the
11681 necessary array types in backwards order. */
7ca2d3a3 11682
c906108c 11683 type = element_type;
7ca2d3a3
DL
11684
11685 if (read_array_order (die, cu) == DW_ORD_col_major)
11686 {
11687 int i = 0;
9a619af0 11688
7ca2d3a3
DL
11689 while (i < ndim)
11690 type = create_array_type (NULL, type, range_types[i++]);
11691 }
11692 else
11693 {
11694 while (ndim-- > 0)
11695 type = create_array_type (NULL, type, range_types[ndim]);
11696 }
c906108c 11697
f5f8a009
EZ
11698 /* Understand Dwarf2 support for vector types (like they occur on
11699 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11700 array type. This is not part of the Dwarf2/3 standard yet, but a
11701 custom vendor extension. The main difference between a regular
11702 array and the vector variant is that vectors are passed by value
11703 to functions. */
e142c38c 11704 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11705 if (attr)
ea37ba09 11706 make_vector_type (type);
f5f8a009 11707
dbc98a8b
KW
11708 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11709 implementation may choose to implement triple vectors using this
11710 attribute. */
11711 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11712 if (attr)
11713 {
11714 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11715 TYPE_LENGTH (type) = DW_UNSND (attr);
11716 else
3e43a32a
MS
11717 complaint (&symfile_complaints,
11718 _("DW_AT_byte_size for array type smaller "
11719 "than the total size of elements"));
dbc98a8b
KW
11720 }
11721
39cbfefa
DJ
11722 name = dwarf2_name (die, cu);
11723 if (name)
11724 TYPE_NAME (type) = name;
6e70227d 11725
0963b4bd 11726 /* Install the type in the die. */
7e314c57
JK
11727 set_die_type (die, type, cu);
11728
11729 /* set_die_type should be already done. */
b4ba55a1
JB
11730 set_descriptive_type (type, die, cu);
11731
c906108c
SS
11732 do_cleanups (back_to);
11733
7e314c57 11734 return type;
c906108c
SS
11735}
11736
7ca2d3a3 11737static enum dwarf_array_dim_ordering
6e70227d 11738read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11739{
11740 struct attribute *attr;
11741
11742 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11743
11744 if (attr) return DW_SND (attr);
11745
0963b4bd
MS
11746 /* GNU F77 is a special case, as at 08/2004 array type info is the
11747 opposite order to the dwarf2 specification, but data is still
11748 laid out as per normal fortran.
7ca2d3a3 11749
0963b4bd
MS
11750 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11751 version checking. */
7ca2d3a3 11752
905e0470
PM
11753 if (cu->language == language_fortran
11754 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11755 {
11756 return DW_ORD_row_major;
11757 }
11758
6e70227d 11759 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11760 {
11761 case array_column_major:
11762 return DW_ORD_col_major;
11763 case array_row_major:
11764 default:
11765 return DW_ORD_row_major;
11766 };
11767}
11768
72019c9c 11769/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11770 the DIE's type field. */
72019c9c 11771
f792889a 11772static struct type *
72019c9c
GM
11773read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11774{
7e314c57
JK
11775 struct type *domain_type, *set_type;
11776 struct attribute *attr;
f792889a 11777
7e314c57
JK
11778 domain_type = die_type (die, cu);
11779
11780 /* The die_type call above may have already set the type for this DIE. */
11781 set_type = get_die_type (die, cu);
11782 if (set_type)
11783 return set_type;
11784
11785 set_type = create_set_type (NULL, domain_type);
11786
11787 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11788 if (attr)
11789 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11790
f792889a 11791 return set_die_type (die, set_type, cu);
72019c9c 11792}
7ca2d3a3 11793
0971de02
TT
11794/* A helper for read_common_block that creates a locexpr baton.
11795 SYM is the symbol which we are marking as computed.
11796 COMMON_DIE is the DIE for the common block.
11797 COMMON_LOC is the location expression attribute for the common
11798 block itself.
11799 MEMBER_LOC is the location expression attribute for the particular
11800 member of the common block that we are processing.
11801 CU is the CU from which the above come. */
11802
11803static void
11804mark_common_block_symbol_computed (struct symbol *sym,
11805 struct die_info *common_die,
11806 struct attribute *common_loc,
11807 struct attribute *member_loc,
11808 struct dwarf2_cu *cu)
11809{
11810 struct objfile *objfile = dwarf2_per_objfile->objfile;
11811 struct dwarf2_locexpr_baton *baton;
11812 gdb_byte *ptr;
11813 unsigned int cu_off;
11814 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11815 LONGEST offset = 0;
11816
11817 gdb_assert (common_loc && member_loc);
11818 gdb_assert (attr_form_is_block (common_loc));
11819 gdb_assert (attr_form_is_block (member_loc)
11820 || attr_form_is_constant (member_loc));
11821
11822 baton = obstack_alloc (&objfile->objfile_obstack,
11823 sizeof (struct dwarf2_locexpr_baton));
11824 baton->per_cu = cu->per_cu;
11825 gdb_assert (baton->per_cu);
11826
11827 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11828
11829 if (attr_form_is_constant (member_loc))
11830 {
11831 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11832 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11833 }
11834 else
11835 baton->size += DW_BLOCK (member_loc)->size;
11836
11837 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11838 baton->data = ptr;
11839
11840 *ptr++ = DW_OP_call4;
11841 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11842 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11843 ptr += 4;
11844
11845 if (attr_form_is_constant (member_loc))
11846 {
11847 *ptr++ = DW_OP_addr;
11848 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11849 ptr += cu->header.addr_size;
11850 }
11851 else
11852 {
11853 /* We have to copy the data here, because DW_OP_call4 will only
11854 use a DW_AT_location attribute. */
11855 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11856 ptr += DW_BLOCK (member_loc)->size;
11857 }
11858
11859 *ptr++ = DW_OP_plus;
11860 gdb_assert (ptr - baton->data == baton->size);
11861
11862 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11863 SYMBOL_LOCATION_BATON (sym) = baton;
11864 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11865}
11866
4357ac6c
TT
11867/* Create appropriate locally-scoped variables for all the
11868 DW_TAG_common_block entries. Also create a struct common_block
11869 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11870 is used to sepate the common blocks name namespace from regular
11871 variable names. */
c906108c
SS
11872
11873static void
e7c27a73 11874read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11875{
0971de02
TT
11876 struct attribute *attr;
11877
11878 attr = dwarf2_attr (die, DW_AT_location, cu);
11879 if (attr)
11880 {
11881 /* Support the .debug_loc offsets. */
11882 if (attr_form_is_block (attr))
11883 {
11884 /* Ok. */
11885 }
11886 else if (attr_form_is_section_offset (attr))
11887 {
11888 dwarf2_complex_location_expr_complaint ();
11889 attr = NULL;
11890 }
11891 else
11892 {
11893 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11894 "common block member");
11895 attr = NULL;
11896 }
11897 }
11898
639d11d3 11899 if (die->child != NULL)
c906108c 11900 {
4357ac6c
TT
11901 struct objfile *objfile = cu->objfile;
11902 struct die_info *child_die;
11903 size_t n_entries = 0, size;
11904 struct common_block *common_block;
11905 struct symbol *sym;
74ac6d43 11906
4357ac6c
TT
11907 for (child_die = die->child;
11908 child_die && child_die->tag;
11909 child_die = sibling_die (child_die))
11910 ++n_entries;
11911
11912 size = (sizeof (struct common_block)
11913 + (n_entries - 1) * sizeof (struct symbol *));
11914 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11915 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11916 common_block->n_entries = 0;
11917
11918 for (child_die = die->child;
11919 child_die && child_die->tag;
11920 child_die = sibling_die (child_die))
11921 {
11922 /* Create the symbol in the DW_TAG_common_block block in the current
11923 symbol scope. */
e7c27a73 11924 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11925 if (sym != NULL)
11926 {
11927 struct attribute *member_loc;
11928
11929 common_block->contents[common_block->n_entries++] = sym;
11930
11931 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11932 cu);
11933 if (member_loc)
11934 {
11935 /* GDB has handled this for a long time, but it is
11936 not specified by DWARF. It seems to have been
11937 emitted by gfortran at least as recently as:
11938 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11939 complaint (&symfile_complaints,
11940 _("Variable in common block has "
11941 "DW_AT_data_member_location "
11942 "- DIE at 0x%x [in module %s]"),
11943 child_die->offset.sect_off, cu->objfile->name);
11944
11945 if (attr_form_is_section_offset (member_loc))
11946 dwarf2_complex_location_expr_complaint ();
11947 else if (attr_form_is_constant (member_loc)
11948 || attr_form_is_block (member_loc))
11949 {
11950 if (attr)
11951 mark_common_block_symbol_computed (sym, die, attr,
11952 member_loc, cu);
11953 }
11954 else
11955 dwarf2_complex_location_expr_complaint ();
11956 }
11957 }
c906108c 11958 }
4357ac6c
TT
11959
11960 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11961 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
11962 }
11963}
11964
0114d602 11965/* Create a type for a C++ namespace. */
d9fa45fe 11966
0114d602
DJ
11967static struct type *
11968read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11969{
e7c27a73 11970 struct objfile *objfile = cu->objfile;
0114d602 11971 const char *previous_prefix, *name;
9219021c 11972 int is_anonymous;
0114d602
DJ
11973 struct type *type;
11974
11975 /* For extensions, reuse the type of the original namespace. */
11976 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11977 {
11978 struct die_info *ext_die;
11979 struct dwarf2_cu *ext_cu = cu;
9a619af0 11980
0114d602
DJ
11981 ext_die = dwarf2_extension (die, &ext_cu);
11982 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11983
11984 /* EXT_CU may not be the same as CU.
11985 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11986 return set_die_type (die, type, cu);
11987 }
9219021c 11988
e142c38c 11989 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11990
11991 /* Now build the name of the current namespace. */
11992
0114d602
DJ
11993 previous_prefix = determine_prefix (die, cu);
11994 if (previous_prefix[0] != '\0')
11995 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11996 previous_prefix, name, 0, cu);
0114d602
DJ
11997
11998 /* Create the type. */
11999 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12000 objfile);
12001 TYPE_NAME (type) = (char *) name;
12002 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12003
60531b24 12004 return set_die_type (die, type, cu);
0114d602
DJ
12005}
12006
12007/* Read a C++ namespace. */
12008
12009static void
12010read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12011{
12012 struct objfile *objfile = cu->objfile;
0114d602 12013 int is_anonymous;
9219021c 12014
5c4e30ca
DC
12015 /* Add a symbol associated to this if we haven't seen the namespace
12016 before. Also, add a using directive if it's an anonymous
12017 namespace. */
9219021c 12018
f2f0e013 12019 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12020 {
12021 struct type *type;
12022
0114d602 12023 type = read_type_die (die, cu);
e7c27a73 12024 new_symbol (die, type, cu);
5c4e30ca 12025
e8e80198 12026 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12027 if (is_anonymous)
0114d602
DJ
12028 {
12029 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12030
c0cc3a76 12031 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 12032 NULL, NULL, &objfile->objfile_obstack);
0114d602 12033 }
5c4e30ca 12034 }
9219021c 12035
639d11d3 12036 if (die->child != NULL)
d9fa45fe 12037 {
639d11d3 12038 struct die_info *child_die = die->child;
6e70227d 12039
d9fa45fe
DC
12040 while (child_die && child_die->tag)
12041 {
e7c27a73 12042 process_die (child_die, cu);
d9fa45fe
DC
12043 child_die = sibling_die (child_die);
12044 }
12045 }
38d518c9
EZ
12046}
12047
f55ee35c
JK
12048/* Read a Fortran module as type. This DIE can be only a declaration used for
12049 imported module. Still we need that type as local Fortran "use ... only"
12050 declaration imports depend on the created type in determine_prefix. */
12051
12052static struct type *
12053read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12054{
12055 struct objfile *objfile = cu->objfile;
12056 char *module_name;
12057 struct type *type;
12058
12059 module_name = dwarf2_name (die, cu);
12060 if (!module_name)
3e43a32a
MS
12061 complaint (&symfile_complaints,
12062 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12063 die->offset.sect_off);
f55ee35c
JK
12064 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12065
12066 /* determine_prefix uses TYPE_TAG_NAME. */
12067 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12068
12069 return set_die_type (die, type, cu);
12070}
12071
5d7cb8df
JK
12072/* Read a Fortran module. */
12073
12074static void
12075read_module (struct die_info *die, struct dwarf2_cu *cu)
12076{
12077 struct die_info *child_die = die->child;
12078
5d7cb8df
JK
12079 while (child_die && child_die->tag)
12080 {
12081 process_die (child_die, cu);
12082 child_die = sibling_die (child_die);
12083 }
12084}
12085
38d518c9
EZ
12086/* Return the name of the namespace represented by DIE. Set
12087 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12088 namespace. */
12089
12090static const char *
e142c38c 12091namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12092{
12093 struct die_info *current_die;
12094 const char *name = NULL;
12095
12096 /* Loop through the extensions until we find a name. */
12097
12098 for (current_die = die;
12099 current_die != NULL;
f2f0e013 12100 current_die = dwarf2_extension (die, &cu))
38d518c9 12101 {
e142c38c 12102 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12103 if (name != NULL)
12104 break;
12105 }
12106
12107 /* Is it an anonymous namespace? */
12108
12109 *is_anonymous = (name == NULL);
12110 if (*is_anonymous)
2b1dbab0 12111 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12112
12113 return name;
d9fa45fe
DC
12114}
12115
c906108c
SS
12116/* Extract all information from a DW_TAG_pointer_type DIE and add to
12117 the user defined type vector. */
12118
f792889a 12119static struct type *
e7c27a73 12120read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12121{
5e2b427d 12122 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12123 struct comp_unit_head *cu_header = &cu->header;
c906108c 12124 struct type *type;
8b2dbe47
KB
12125 struct attribute *attr_byte_size;
12126 struct attribute *attr_address_class;
12127 int byte_size, addr_class;
7e314c57
JK
12128 struct type *target_type;
12129
12130 target_type = die_type (die, cu);
c906108c 12131
7e314c57
JK
12132 /* The die_type call above may have already set the type for this DIE. */
12133 type = get_die_type (die, cu);
12134 if (type)
12135 return type;
12136
12137 type = lookup_pointer_type (target_type);
8b2dbe47 12138
e142c38c 12139 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12140 if (attr_byte_size)
12141 byte_size = DW_UNSND (attr_byte_size);
c906108c 12142 else
8b2dbe47
KB
12143 byte_size = cu_header->addr_size;
12144
e142c38c 12145 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12146 if (attr_address_class)
12147 addr_class = DW_UNSND (attr_address_class);
12148 else
12149 addr_class = DW_ADDR_none;
12150
12151 /* If the pointer size or address class is different than the
12152 default, create a type variant marked as such and set the
12153 length accordingly. */
12154 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12155 {
5e2b427d 12156 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12157 {
12158 int type_flags;
12159
849957d9 12160 type_flags = gdbarch_address_class_type_flags
5e2b427d 12161 (gdbarch, byte_size, addr_class);
876cecd0
TT
12162 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12163 == 0);
8b2dbe47
KB
12164 type = make_type_with_address_space (type, type_flags);
12165 }
12166 else if (TYPE_LENGTH (type) != byte_size)
12167 {
3e43a32a
MS
12168 complaint (&symfile_complaints,
12169 _("invalid pointer size %d"), byte_size);
8b2dbe47 12170 }
6e70227d 12171 else
9a619af0
MS
12172 {
12173 /* Should we also complain about unhandled address classes? */
12174 }
c906108c 12175 }
8b2dbe47
KB
12176
12177 TYPE_LENGTH (type) = byte_size;
f792889a 12178 return set_die_type (die, type, cu);
c906108c
SS
12179}
12180
12181/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12182 the user defined type vector. */
12183
f792889a 12184static struct type *
e7c27a73 12185read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12186{
12187 struct type *type;
12188 struct type *to_type;
12189 struct type *domain;
12190
e7c27a73
DJ
12191 to_type = die_type (die, cu);
12192 domain = die_containing_type (die, cu);
0d5de010 12193
7e314c57
JK
12194 /* The calls above may have already set the type for this DIE. */
12195 type = get_die_type (die, cu);
12196 if (type)
12197 return type;
12198
0d5de010
DJ
12199 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12200 type = lookup_methodptr_type (to_type);
12201 else
12202 type = lookup_memberptr_type (to_type, domain);
c906108c 12203
f792889a 12204 return set_die_type (die, type, cu);
c906108c
SS
12205}
12206
12207/* Extract all information from a DW_TAG_reference_type DIE and add to
12208 the user defined type vector. */
12209
f792889a 12210static struct type *
e7c27a73 12211read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12212{
e7c27a73 12213 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12214 struct type *type, *target_type;
c906108c
SS
12215 struct attribute *attr;
12216
7e314c57
JK
12217 target_type = die_type (die, cu);
12218
12219 /* The die_type call above may have already set the type for this DIE. */
12220 type = get_die_type (die, cu);
12221 if (type)
12222 return type;
12223
12224 type = lookup_reference_type (target_type);
e142c38c 12225 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12226 if (attr)
12227 {
12228 TYPE_LENGTH (type) = DW_UNSND (attr);
12229 }
12230 else
12231 {
107d2387 12232 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12233 }
f792889a 12234 return set_die_type (die, type, cu);
c906108c
SS
12235}
12236
f792889a 12237static struct type *
e7c27a73 12238read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12239{
f792889a 12240 struct type *base_type, *cv_type;
c906108c 12241
e7c27a73 12242 base_type = die_type (die, cu);
7e314c57
JK
12243
12244 /* The die_type call above may have already set the type for this DIE. */
12245 cv_type = get_die_type (die, cu);
12246 if (cv_type)
12247 return cv_type;
12248
2f608a3a
KW
12249 /* In case the const qualifier is applied to an array type, the element type
12250 is so qualified, not the array type (section 6.7.3 of C99). */
12251 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12252 {
12253 struct type *el_type, *inner_array;
12254
12255 base_type = copy_type (base_type);
12256 inner_array = base_type;
12257
12258 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12259 {
12260 TYPE_TARGET_TYPE (inner_array) =
12261 copy_type (TYPE_TARGET_TYPE (inner_array));
12262 inner_array = TYPE_TARGET_TYPE (inner_array);
12263 }
12264
12265 el_type = TYPE_TARGET_TYPE (inner_array);
12266 TYPE_TARGET_TYPE (inner_array) =
12267 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12268
12269 return set_die_type (die, base_type, cu);
12270 }
12271
f792889a
DJ
12272 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12273 return set_die_type (die, cv_type, cu);
c906108c
SS
12274}
12275
f792889a 12276static struct type *
e7c27a73 12277read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12278{
f792889a 12279 struct type *base_type, *cv_type;
c906108c 12280
e7c27a73 12281 base_type = die_type (die, cu);
7e314c57
JK
12282
12283 /* The die_type call above may have already set the type for this DIE. */
12284 cv_type = get_die_type (die, cu);
12285 if (cv_type)
12286 return cv_type;
12287
f792889a
DJ
12288 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12289 return set_die_type (die, cv_type, cu);
c906108c
SS
12290}
12291
12292/* Extract all information from a DW_TAG_string_type DIE and add to
12293 the user defined type vector. It isn't really a user defined type,
12294 but it behaves like one, with other DIE's using an AT_user_def_type
12295 attribute to reference it. */
12296
f792889a 12297static struct type *
e7c27a73 12298read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12299{
e7c27a73 12300 struct objfile *objfile = cu->objfile;
3b7538c0 12301 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12302 struct type *type, *range_type, *index_type, *char_type;
12303 struct attribute *attr;
12304 unsigned int length;
12305
e142c38c 12306 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12307 if (attr)
12308 {
12309 length = DW_UNSND (attr);
12310 }
12311 else
12312 {
0963b4bd 12313 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12314 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12315 if (attr)
12316 {
12317 length = DW_UNSND (attr);
12318 }
12319 else
12320 {
12321 length = 1;
12322 }
c906108c 12323 }
6ccb9162 12324
46bf5051 12325 index_type = objfile_type (objfile)->builtin_int;
c906108c 12326 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12327 char_type = language_string_char_type (cu->language_defn, gdbarch);
12328 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12329
f792889a 12330 return set_die_type (die, type, cu);
c906108c
SS
12331}
12332
12333/* Handle DIES due to C code like:
12334
12335 struct foo
c5aa993b
JM
12336 {
12337 int (*funcp)(int a, long l);
12338 int b;
12339 };
c906108c 12340
0963b4bd 12341 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12342
f792889a 12343static struct type *
e7c27a73 12344read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12345{
bb5ed363 12346 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12347 struct type *type; /* Type that this function returns. */
12348 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12349 struct attribute *attr;
12350
e7c27a73 12351 type = die_type (die, cu);
7e314c57
JK
12352
12353 /* The die_type call above may have already set the type for this DIE. */
12354 ftype = get_die_type (die, cu);
12355 if (ftype)
12356 return ftype;
12357
0c8b41f1 12358 ftype = lookup_function_type (type);
c906108c 12359
5b8101ae 12360 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12361 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12362 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12363 || cu->language == language_cplus
5b8101ae
PM
12364 || cu->language == language_java
12365 || cu->language == language_pascal)
876cecd0 12366 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12367 else if (producer_is_realview (cu->producer))
12368 /* RealView does not emit DW_AT_prototyped. We can not
12369 distinguish prototyped and unprototyped functions; default to
12370 prototyped, since that is more common in modern code (and
12371 RealView warns about unprototyped functions). */
12372 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12373
c055b101
CV
12374 /* Store the calling convention in the type if it's available in
12375 the subroutine die. Otherwise set the calling convention to
12376 the default value DW_CC_normal. */
12377 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12378 if (attr)
12379 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12380 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12381 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12382 else
12383 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12384
12385 /* We need to add the subroutine type to the die immediately so
12386 we don't infinitely recurse when dealing with parameters
0963b4bd 12387 declared as the same subroutine type. */
76c10ea2 12388 set_die_type (die, ftype, cu);
6e70227d 12389
639d11d3 12390 if (die->child != NULL)
c906108c 12391 {
bb5ed363 12392 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12393 struct die_info *child_die;
8072405b 12394 int nparams, iparams;
c906108c
SS
12395
12396 /* Count the number of parameters.
12397 FIXME: GDB currently ignores vararg functions, but knows about
12398 vararg member functions. */
8072405b 12399 nparams = 0;
639d11d3 12400 child_die = die->child;
c906108c
SS
12401 while (child_die && child_die->tag)
12402 {
12403 if (child_die->tag == DW_TAG_formal_parameter)
12404 nparams++;
12405 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12406 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12407 child_die = sibling_die (child_die);
12408 }
12409
12410 /* Allocate storage for parameters and fill them in. */
12411 TYPE_NFIELDS (ftype) = nparams;
12412 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12413 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12414
8072405b
JK
12415 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12416 even if we error out during the parameters reading below. */
12417 for (iparams = 0; iparams < nparams; iparams++)
12418 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12419
12420 iparams = 0;
639d11d3 12421 child_die = die->child;
c906108c
SS
12422 while (child_die && child_die->tag)
12423 {
12424 if (child_die->tag == DW_TAG_formal_parameter)
12425 {
3ce3b1ba
PA
12426 struct type *arg_type;
12427
12428 /* DWARF version 2 has no clean way to discern C++
12429 static and non-static member functions. G++ helps
12430 GDB by marking the first parameter for non-static
12431 member functions (which is the this pointer) as
12432 artificial. We pass this information to
12433 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12434
12435 DWARF version 3 added DW_AT_object_pointer, which GCC
12436 4.5 does not yet generate. */
e142c38c 12437 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12438 if (attr)
12439 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12440 else
418835cc
KS
12441 {
12442 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12443
12444 /* GCC/43521: In java, the formal parameter
12445 "this" is sometimes not marked with DW_AT_artificial. */
12446 if (cu->language == language_java)
12447 {
12448 const char *name = dwarf2_name (child_die, cu);
9a619af0 12449
418835cc
KS
12450 if (name && !strcmp (name, "this"))
12451 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12452 }
12453 }
3ce3b1ba
PA
12454 arg_type = die_type (child_die, cu);
12455
12456 /* RealView does not mark THIS as const, which the testsuite
12457 expects. GCC marks THIS as const in method definitions,
12458 but not in the class specifications (GCC PR 43053). */
12459 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12460 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12461 {
12462 int is_this = 0;
12463 struct dwarf2_cu *arg_cu = cu;
12464 const char *name = dwarf2_name (child_die, cu);
12465
12466 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12467 if (attr)
12468 {
12469 /* If the compiler emits this, use it. */
12470 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12471 is_this = 1;
12472 }
12473 else if (name && strcmp (name, "this") == 0)
12474 /* Function definitions will have the argument names. */
12475 is_this = 1;
12476 else if (name == NULL && iparams == 0)
12477 /* Declarations may not have the names, so like
12478 elsewhere in GDB, assume an artificial first
12479 argument is "this". */
12480 is_this = 1;
12481
12482 if (is_this)
12483 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12484 arg_type, 0);
12485 }
12486
12487 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12488 iparams++;
12489 }
12490 child_die = sibling_die (child_die);
12491 }
12492 }
12493
76c10ea2 12494 return ftype;
c906108c
SS
12495}
12496
f792889a 12497static struct type *
e7c27a73 12498read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12499{
e7c27a73 12500 struct objfile *objfile = cu->objfile;
0114d602 12501 const char *name = NULL;
3c8e0968 12502 struct type *this_type, *target_type;
c906108c 12503
94af9270 12504 name = dwarf2_full_name (NULL, die, cu);
f792889a 12505 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12506 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12507 TYPE_NAME (this_type) = (char *) name;
f792889a 12508 set_die_type (die, this_type, cu);
3c8e0968
DE
12509 target_type = die_type (die, cu);
12510 if (target_type != this_type)
12511 TYPE_TARGET_TYPE (this_type) = target_type;
12512 else
12513 {
12514 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12515 spec and cause infinite loops in GDB. */
12516 complaint (&symfile_complaints,
12517 _("Self-referential DW_TAG_typedef "
12518 "- DIE at 0x%x [in module %s]"),
b64f50a1 12519 die->offset.sect_off, objfile->name);
3c8e0968
DE
12520 TYPE_TARGET_TYPE (this_type) = NULL;
12521 }
f792889a 12522 return this_type;
c906108c
SS
12523}
12524
12525/* Find a representation of a given base type and install
12526 it in the TYPE field of the die. */
12527
f792889a 12528static struct type *
e7c27a73 12529read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12530{
e7c27a73 12531 struct objfile *objfile = cu->objfile;
c906108c
SS
12532 struct type *type;
12533 struct attribute *attr;
12534 int encoding = 0, size = 0;
39cbfefa 12535 char *name;
6ccb9162
UW
12536 enum type_code code = TYPE_CODE_INT;
12537 int type_flags = 0;
12538 struct type *target_type = NULL;
c906108c 12539
e142c38c 12540 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12541 if (attr)
12542 {
12543 encoding = DW_UNSND (attr);
12544 }
e142c38c 12545 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12546 if (attr)
12547 {
12548 size = DW_UNSND (attr);
12549 }
39cbfefa 12550 name = dwarf2_name (die, cu);
6ccb9162 12551 if (!name)
c906108c 12552 {
6ccb9162
UW
12553 complaint (&symfile_complaints,
12554 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12555 }
6ccb9162
UW
12556
12557 switch (encoding)
c906108c 12558 {
6ccb9162
UW
12559 case DW_ATE_address:
12560 /* Turn DW_ATE_address into a void * pointer. */
12561 code = TYPE_CODE_PTR;
12562 type_flags |= TYPE_FLAG_UNSIGNED;
12563 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12564 break;
12565 case DW_ATE_boolean:
12566 code = TYPE_CODE_BOOL;
12567 type_flags |= TYPE_FLAG_UNSIGNED;
12568 break;
12569 case DW_ATE_complex_float:
12570 code = TYPE_CODE_COMPLEX;
12571 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12572 break;
12573 case DW_ATE_decimal_float:
12574 code = TYPE_CODE_DECFLOAT;
12575 break;
12576 case DW_ATE_float:
12577 code = TYPE_CODE_FLT;
12578 break;
12579 case DW_ATE_signed:
12580 break;
12581 case DW_ATE_unsigned:
12582 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12583 if (cu->language == language_fortran
12584 && name
12585 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12586 code = TYPE_CODE_CHAR;
6ccb9162
UW
12587 break;
12588 case DW_ATE_signed_char:
6e70227d 12589 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12590 || cu->language == language_pascal
12591 || cu->language == language_fortran)
6ccb9162
UW
12592 code = TYPE_CODE_CHAR;
12593 break;
12594 case DW_ATE_unsigned_char:
868a0084 12595 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12596 || cu->language == language_pascal
12597 || cu->language == language_fortran)
6ccb9162
UW
12598 code = TYPE_CODE_CHAR;
12599 type_flags |= TYPE_FLAG_UNSIGNED;
12600 break;
75079b2b
TT
12601 case DW_ATE_UTF:
12602 /* We just treat this as an integer and then recognize the
12603 type by name elsewhere. */
12604 break;
12605
6ccb9162
UW
12606 default:
12607 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12608 dwarf_type_encoding_name (encoding));
12609 break;
c906108c 12610 }
6ccb9162 12611
0114d602
DJ
12612 type = init_type (code, size, type_flags, NULL, objfile);
12613 TYPE_NAME (type) = name;
6ccb9162
UW
12614 TYPE_TARGET_TYPE (type) = target_type;
12615
0114d602 12616 if (name && strcmp (name, "char") == 0)
876cecd0 12617 TYPE_NOSIGN (type) = 1;
0114d602 12618
f792889a 12619 return set_die_type (die, type, cu);
c906108c
SS
12620}
12621
a02abb62
JB
12622/* Read the given DW_AT_subrange DIE. */
12623
f792889a 12624static struct type *
a02abb62
JB
12625read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12626{
12627 struct type *base_type;
12628 struct type *range_type;
12629 struct attribute *attr;
4fae6e18
JK
12630 LONGEST low, high;
12631 int low_default_is_valid;
39cbfefa 12632 char *name;
43bbcdc2 12633 LONGEST negative_mask;
e77813c8 12634
a02abb62 12635 base_type = die_type (die, cu);
953ac07e
JK
12636 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12637 check_typedef (base_type);
a02abb62 12638
7e314c57
JK
12639 /* The die_type call above may have already set the type for this DIE. */
12640 range_type = get_die_type (die, cu);
12641 if (range_type)
12642 return range_type;
12643
4fae6e18
JK
12644 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12645 omitting DW_AT_lower_bound. */
12646 switch (cu->language)
6e70227d 12647 {
4fae6e18
JK
12648 case language_c:
12649 case language_cplus:
12650 low = 0;
12651 low_default_is_valid = 1;
12652 break;
12653 case language_fortran:
12654 low = 1;
12655 low_default_is_valid = 1;
12656 break;
12657 case language_d:
12658 case language_java:
12659 case language_objc:
12660 low = 0;
12661 low_default_is_valid = (cu->header.version >= 4);
12662 break;
12663 case language_ada:
12664 case language_m2:
12665 case language_pascal:
a02abb62 12666 low = 1;
4fae6e18
JK
12667 low_default_is_valid = (cu->header.version >= 4);
12668 break;
12669 default:
12670 low = 0;
12671 low_default_is_valid = 0;
12672 break;
a02abb62
JB
12673 }
12674
dd5e6932
DJ
12675 /* FIXME: For variable sized arrays either of these could be
12676 a variable rather than a constant value. We'll allow it,
12677 but we don't know how to handle it. */
e142c38c 12678 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12679 if (attr)
4fae6e18
JK
12680 low = dwarf2_get_attr_constant_value (attr, low);
12681 else if (!low_default_is_valid)
12682 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12683 "- DIE at 0x%x [in module %s]"),
12684 die->offset.sect_off, cu->objfile->name);
a02abb62 12685
e142c38c 12686 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12687 if (attr)
6e70227d 12688 {
d48323d8 12689 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12690 {
12691 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12692 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12693 FIXME: GDB does not yet know how to handle dynamic
12694 arrays properly, treat them as arrays with unspecified
12695 length for now.
12696
12697 FIXME: jimb/2003-09-22: GDB does not really know
12698 how to handle arrays of unspecified length
12699 either; we just represent them as zero-length
12700 arrays. Choose an appropriate upper bound given
12701 the lower bound we've computed above. */
12702 high = low - 1;
12703 }
12704 else
12705 high = dwarf2_get_attr_constant_value (attr, 1);
12706 }
e77813c8
PM
12707 else
12708 {
12709 attr = dwarf2_attr (die, DW_AT_count, cu);
12710 if (attr)
12711 {
12712 int count = dwarf2_get_attr_constant_value (attr, 1);
12713 high = low + count - 1;
12714 }
c2ff108b
JK
12715 else
12716 {
12717 /* Unspecified array length. */
12718 high = low - 1;
12719 }
e77813c8
PM
12720 }
12721
12722 /* Dwarf-2 specifications explicitly allows to create subrange types
12723 without specifying a base type.
12724 In that case, the base type must be set to the type of
12725 the lower bound, upper bound or count, in that order, if any of these
12726 three attributes references an object that has a type.
12727 If no base type is found, the Dwarf-2 specifications say that
12728 a signed integer type of size equal to the size of an address should
12729 be used.
12730 For the following C code: `extern char gdb_int [];'
12731 GCC produces an empty range DIE.
12732 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12733 high bound or count are not yet handled by this code. */
e77813c8
PM
12734 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12735 {
12736 struct objfile *objfile = cu->objfile;
12737 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12738 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12739 struct type *int_type = objfile_type (objfile)->builtin_int;
12740
12741 /* Test "int", "long int", and "long long int" objfile types,
12742 and select the first one having a size above or equal to the
12743 architecture address size. */
12744 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12745 base_type = int_type;
12746 else
12747 {
12748 int_type = objfile_type (objfile)->builtin_long;
12749 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12750 base_type = int_type;
12751 else
12752 {
12753 int_type = objfile_type (objfile)->builtin_long_long;
12754 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12755 base_type = int_type;
12756 }
12757 }
12758 }
a02abb62 12759
6e70227d 12760 negative_mask =
43bbcdc2
PH
12761 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12762 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12763 low |= negative_mask;
12764 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12765 high |= negative_mask;
12766
a02abb62
JB
12767 range_type = create_range_type (NULL, base_type, low, high);
12768
bbb0eef6
JK
12769 /* Mark arrays with dynamic length at least as an array of unspecified
12770 length. GDB could check the boundary but before it gets implemented at
12771 least allow accessing the array elements. */
d48323d8 12772 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12773 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12774
c2ff108b
JK
12775 /* Ada expects an empty array on no boundary attributes. */
12776 if (attr == NULL && cu->language != language_ada)
12777 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12778
39cbfefa
DJ
12779 name = dwarf2_name (die, cu);
12780 if (name)
12781 TYPE_NAME (range_type) = name;
6e70227d 12782
e142c38c 12783 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12784 if (attr)
12785 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12786
7e314c57
JK
12787 set_die_type (die, range_type, cu);
12788
12789 /* set_die_type should be already done. */
b4ba55a1
JB
12790 set_descriptive_type (range_type, die, cu);
12791
7e314c57 12792 return range_type;
a02abb62 12793}
6e70227d 12794
f792889a 12795static struct type *
81a17f79
JB
12796read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12797{
12798 struct type *type;
81a17f79 12799
81a17f79
JB
12800 /* For now, we only support the C meaning of an unspecified type: void. */
12801
0114d602
DJ
12802 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12803 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12804
f792889a 12805 return set_die_type (die, type, cu);
81a17f79 12806}
a02abb62 12807
639d11d3
DC
12808/* Read a single die and all its descendents. Set the die's sibling
12809 field to NULL; set other fields in the die correctly, and set all
12810 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12811 location of the info_ptr after reading all of those dies. PARENT
12812 is the parent of the die in question. */
12813
12814static struct die_info *
dee91e82
DE
12815read_die_and_children (const struct die_reader_specs *reader,
12816 gdb_byte *info_ptr,
12817 gdb_byte **new_info_ptr,
12818 struct die_info *parent)
639d11d3
DC
12819{
12820 struct die_info *die;
fe1b8b76 12821 gdb_byte *cur_ptr;
639d11d3
DC
12822 int has_children;
12823
93311388 12824 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12825 if (die == NULL)
12826 {
12827 *new_info_ptr = cur_ptr;
12828 return NULL;
12829 }
93311388 12830 store_in_ref_table (die, reader->cu);
639d11d3
DC
12831
12832 if (has_children)
348e048f 12833 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12834 else
12835 {
12836 die->child = NULL;
12837 *new_info_ptr = cur_ptr;
12838 }
12839
12840 die->sibling = NULL;
12841 die->parent = parent;
12842 return die;
12843}
12844
12845/* Read a die, all of its descendents, and all of its siblings; set
12846 all of the fields of all of the dies correctly. Arguments are as
12847 in read_die_and_children. */
12848
12849static struct die_info *
93311388
DE
12850read_die_and_siblings (const struct die_reader_specs *reader,
12851 gdb_byte *info_ptr,
fe1b8b76 12852 gdb_byte **new_info_ptr,
639d11d3
DC
12853 struct die_info *parent)
12854{
12855 struct die_info *first_die, *last_sibling;
fe1b8b76 12856 gdb_byte *cur_ptr;
639d11d3 12857
c906108c 12858 cur_ptr = info_ptr;
639d11d3
DC
12859 first_die = last_sibling = NULL;
12860
12861 while (1)
c906108c 12862 {
639d11d3 12863 struct die_info *die
dee91e82 12864 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12865
1d325ec1 12866 if (die == NULL)
c906108c 12867 {
639d11d3
DC
12868 *new_info_ptr = cur_ptr;
12869 return first_die;
c906108c 12870 }
1d325ec1
DJ
12871
12872 if (!first_die)
12873 first_die = die;
c906108c 12874 else
1d325ec1
DJ
12875 last_sibling->sibling = die;
12876
12877 last_sibling = die;
c906108c 12878 }
c906108c
SS
12879}
12880
3019eac3
DE
12881/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12882 attributes.
12883 The caller is responsible for filling in the extra attributes
12884 and updating (*DIEP)->num_attrs.
12885 Set DIEP to point to a newly allocated die with its information,
12886 except for its child, sibling, and parent fields.
12887 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12888
12889static gdb_byte *
3019eac3
DE
12890read_full_die_1 (const struct die_reader_specs *reader,
12891 struct die_info **diep, gdb_byte *info_ptr,
12892 int *has_children, int num_extra_attrs)
93311388 12893{
b64f50a1
JK
12894 unsigned int abbrev_number, bytes_read, i;
12895 sect_offset offset;
93311388
DE
12896 struct abbrev_info *abbrev;
12897 struct die_info *die;
12898 struct dwarf2_cu *cu = reader->cu;
12899 bfd *abfd = reader->abfd;
12900
b64f50a1 12901 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12902 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12903 info_ptr += bytes_read;
12904 if (!abbrev_number)
12905 {
12906 *diep = NULL;
12907 *has_children = 0;
12908 return info_ptr;
12909 }
12910
433df2d4 12911 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12912 if (!abbrev)
348e048f
DE
12913 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12914 abbrev_number,
12915 bfd_get_filename (abfd));
12916
3019eac3 12917 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12918 die->offset = offset;
12919 die->tag = abbrev->tag;
12920 die->abbrev = abbrev_number;
12921
3019eac3
DE
12922 /* Make the result usable.
12923 The caller needs to update num_attrs after adding the extra
12924 attributes. */
93311388
DE
12925 die->num_attrs = abbrev->num_attrs;
12926
12927 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12928 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12929 info_ptr);
93311388
DE
12930
12931 *diep = die;
12932 *has_children = abbrev->has_children;
12933 return info_ptr;
12934}
12935
3019eac3
DE
12936/* Read a die and all its attributes.
12937 Set DIEP to point to a newly allocated die with its information,
12938 except for its child, sibling, and parent fields.
12939 Set HAS_CHILDREN to tell whether the die has children or not. */
12940
12941static gdb_byte *
12942read_full_die (const struct die_reader_specs *reader,
12943 struct die_info **diep, gdb_byte *info_ptr,
12944 int *has_children)
12945{
12946 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12947}
433df2d4
DE
12948\f
12949/* Abbreviation tables.
3019eac3 12950
433df2d4 12951 In DWARF version 2, the description of the debugging information is
c906108c
SS
12952 stored in a separate .debug_abbrev section. Before we read any
12953 dies from a section we read in all abbreviations and install them
433df2d4
DE
12954 in a hash table. */
12955
12956/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12957
12958static struct abbrev_info *
12959abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12960{
12961 struct abbrev_info *abbrev;
12962
12963 abbrev = (struct abbrev_info *)
12964 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12965 memset (abbrev, 0, sizeof (struct abbrev_info));
12966 return abbrev;
12967}
12968
12969/* Add an abbreviation to the table. */
c906108c
SS
12970
12971static void
433df2d4
DE
12972abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12973 unsigned int abbrev_number,
12974 struct abbrev_info *abbrev)
12975{
12976 unsigned int hash_number;
12977
12978 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12979 abbrev->next = abbrev_table->abbrevs[hash_number];
12980 abbrev_table->abbrevs[hash_number] = abbrev;
12981}
dee91e82 12982
433df2d4
DE
12983/* Look up an abbrev in the table.
12984 Returns NULL if the abbrev is not found. */
12985
12986static struct abbrev_info *
12987abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12988 unsigned int abbrev_number)
c906108c 12989{
433df2d4
DE
12990 unsigned int hash_number;
12991 struct abbrev_info *abbrev;
12992
12993 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12994 abbrev = abbrev_table->abbrevs[hash_number];
12995
12996 while (abbrev)
12997 {
12998 if (abbrev->number == abbrev_number)
12999 return abbrev;
13000 abbrev = abbrev->next;
13001 }
13002 return NULL;
13003}
13004
13005/* Read in an abbrev table. */
13006
13007static struct abbrev_table *
13008abbrev_table_read_table (struct dwarf2_section_info *section,
13009 sect_offset offset)
13010{
13011 struct objfile *objfile = dwarf2_per_objfile->objfile;
13012 bfd *abfd = section->asection->owner;
13013 struct abbrev_table *abbrev_table;
fe1b8b76 13014 gdb_byte *abbrev_ptr;
c906108c
SS
13015 struct abbrev_info *cur_abbrev;
13016 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13017 unsigned int abbrev_form;
f3dd6933
DJ
13018 struct attr_abbrev *cur_attrs;
13019 unsigned int allocated_attrs;
c906108c 13020
433df2d4 13021 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13022 abbrev_table->offset = offset;
433df2d4
DE
13023 obstack_init (&abbrev_table->abbrev_obstack);
13024 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13025 (ABBREV_HASH_SIZE
13026 * sizeof (struct abbrev_info *)));
13027 memset (abbrev_table->abbrevs, 0,
13028 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13029
433df2d4
DE
13030 dwarf2_read_section (objfile, section);
13031 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13032 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13033 abbrev_ptr += bytes_read;
13034
f3dd6933
DJ
13035 allocated_attrs = ATTR_ALLOC_CHUNK;
13036 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13037
0963b4bd 13038 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13039 while (abbrev_number)
13040 {
433df2d4 13041 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13042
13043 /* read in abbrev header */
13044 cur_abbrev->number = abbrev_number;
13045 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13046 abbrev_ptr += bytes_read;
13047 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13048 abbrev_ptr += 1;
13049
13050 /* now read in declarations */
13051 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13052 abbrev_ptr += bytes_read;
13053 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13054 abbrev_ptr += bytes_read;
13055 while (abbrev_name)
13056 {
f3dd6933 13057 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13058 {
f3dd6933
DJ
13059 allocated_attrs += ATTR_ALLOC_CHUNK;
13060 cur_attrs
13061 = xrealloc (cur_attrs, (allocated_attrs
13062 * sizeof (struct attr_abbrev)));
c906108c 13063 }
ae038cb0 13064
f3dd6933
DJ
13065 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13066 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13067 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13068 abbrev_ptr += bytes_read;
13069 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13070 abbrev_ptr += bytes_read;
13071 }
13072
433df2d4 13073 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13074 (cur_abbrev->num_attrs
13075 * sizeof (struct attr_abbrev)));
13076 memcpy (cur_abbrev->attrs, cur_attrs,
13077 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13078
433df2d4 13079 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13080
13081 /* Get next abbreviation.
13082 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13083 always properly terminated with an abbrev number of 0.
13084 Exit loop if we encounter an abbreviation which we have
13085 already read (which means we are about to read the abbreviations
13086 for the next compile unit) or if the end of the abbreviation
13087 table is reached. */
433df2d4 13088 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13089 break;
13090 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13091 abbrev_ptr += bytes_read;
433df2d4 13092 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13093 break;
13094 }
f3dd6933
DJ
13095
13096 xfree (cur_attrs);
433df2d4 13097 return abbrev_table;
c906108c
SS
13098}
13099
433df2d4 13100/* Free the resources held by ABBREV_TABLE. */
c906108c 13101
c906108c 13102static void
433df2d4 13103abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13104{
433df2d4
DE
13105 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13106 xfree (abbrev_table);
c906108c
SS
13107}
13108
f4dc4d17
DE
13109/* Same as abbrev_table_free but as a cleanup.
13110 We pass in a pointer to the pointer to the table so that we can
13111 set the pointer to NULL when we're done. It also simplifies
13112 build_type_unit_groups. */
13113
13114static void
13115abbrev_table_free_cleanup (void *table_ptr)
13116{
13117 struct abbrev_table **abbrev_table_ptr = table_ptr;
13118
13119 if (*abbrev_table_ptr != NULL)
13120 abbrev_table_free (*abbrev_table_ptr);
13121 *abbrev_table_ptr = NULL;
13122}
13123
433df2d4
DE
13124/* Read the abbrev table for CU from ABBREV_SECTION. */
13125
13126static void
13127dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13128 struct dwarf2_section_info *abbrev_section)
c906108c 13129{
433df2d4
DE
13130 cu->abbrev_table =
13131 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13132}
c906108c 13133
433df2d4 13134/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13135
433df2d4
DE
13136static void
13137dwarf2_free_abbrev_table (void *ptr_to_cu)
13138{
13139 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13140
433df2d4
DE
13141 abbrev_table_free (cu->abbrev_table);
13142 /* Set this to NULL so that we SEGV if we try to read it later,
13143 and also because free_comp_unit verifies this is NULL. */
13144 cu->abbrev_table = NULL;
13145}
13146\f
72bf9492
DJ
13147/* Returns nonzero if TAG represents a type that we might generate a partial
13148 symbol for. */
13149
13150static int
13151is_type_tag_for_partial (int tag)
13152{
13153 switch (tag)
13154 {
13155#if 0
13156 /* Some types that would be reasonable to generate partial symbols for,
13157 that we don't at present. */
13158 case DW_TAG_array_type:
13159 case DW_TAG_file_type:
13160 case DW_TAG_ptr_to_member_type:
13161 case DW_TAG_set_type:
13162 case DW_TAG_string_type:
13163 case DW_TAG_subroutine_type:
13164#endif
13165 case DW_TAG_base_type:
13166 case DW_TAG_class_type:
680b30c7 13167 case DW_TAG_interface_type:
72bf9492
DJ
13168 case DW_TAG_enumeration_type:
13169 case DW_TAG_structure_type:
13170 case DW_TAG_subrange_type:
13171 case DW_TAG_typedef:
13172 case DW_TAG_union_type:
13173 return 1;
13174 default:
13175 return 0;
13176 }
13177}
13178
13179/* Load all DIEs that are interesting for partial symbols into memory. */
13180
13181static struct partial_die_info *
dee91e82
DE
13182load_partial_dies (const struct die_reader_specs *reader,
13183 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13184{
dee91e82 13185 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13186 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13187 struct partial_die_info *part_die;
13188 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13189 struct abbrev_info *abbrev;
13190 unsigned int bytes_read;
5afb4e99 13191 unsigned int load_all = 0;
72bf9492
DJ
13192 int nesting_level = 1;
13193
13194 parent_die = NULL;
13195 last_die = NULL;
13196
7adf1e79
DE
13197 gdb_assert (cu->per_cu != NULL);
13198 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13199 load_all = 1;
13200
72bf9492
DJ
13201 cu->partial_dies
13202 = htab_create_alloc_ex (cu->header.length / 12,
13203 partial_die_hash,
13204 partial_die_eq,
13205 NULL,
13206 &cu->comp_unit_obstack,
13207 hashtab_obstack_allocate,
13208 dummy_obstack_deallocate);
13209
13210 part_die = obstack_alloc (&cu->comp_unit_obstack,
13211 sizeof (struct partial_die_info));
13212
13213 while (1)
13214 {
13215 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13216
13217 /* A NULL abbrev means the end of a series of children. */
13218 if (abbrev == NULL)
13219 {
13220 if (--nesting_level == 0)
13221 {
13222 /* PART_DIE was probably the last thing allocated on the
13223 comp_unit_obstack, so we could call obstack_free
13224 here. We don't do that because the waste is small,
13225 and will be cleaned up when we're done with this
13226 compilation unit. This way, we're also more robust
13227 against other users of the comp_unit_obstack. */
13228 return first_die;
13229 }
13230 info_ptr += bytes_read;
13231 last_die = parent_die;
13232 parent_die = parent_die->die_parent;
13233 continue;
13234 }
13235
98bfdba5
PA
13236 /* Check for template arguments. We never save these; if
13237 they're seen, we just mark the parent, and go on our way. */
13238 if (parent_die != NULL
13239 && cu->language == language_cplus
13240 && (abbrev->tag == DW_TAG_template_type_param
13241 || abbrev->tag == DW_TAG_template_value_param))
13242 {
13243 parent_die->has_template_arguments = 1;
13244
13245 if (!load_all)
13246 {
13247 /* We don't need a partial DIE for the template argument. */
dee91e82 13248 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13249 continue;
13250 }
13251 }
13252
0d99eb77 13253 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13254 Skip their other children. */
13255 if (!load_all
13256 && cu->language == language_cplus
13257 && parent_die != NULL
13258 && parent_die->tag == DW_TAG_subprogram)
13259 {
dee91e82 13260 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13261 continue;
13262 }
13263
5afb4e99
DJ
13264 /* Check whether this DIE is interesting enough to save. Normally
13265 we would not be interested in members here, but there may be
13266 later variables referencing them via DW_AT_specification (for
13267 static members). */
13268 if (!load_all
13269 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13270 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13271 && abbrev->tag != DW_TAG_enumerator
13272 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13273 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13274 && abbrev->tag != DW_TAG_variable
5afb4e99 13275 && abbrev->tag != DW_TAG_namespace
f55ee35c 13276 && abbrev->tag != DW_TAG_module
95554aad
TT
13277 && abbrev->tag != DW_TAG_member
13278 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13279 {
13280 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13281 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13282 continue;
13283 }
13284
dee91e82
DE
13285 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13286 info_ptr);
72bf9492
DJ
13287
13288 /* This two-pass algorithm for processing partial symbols has a
13289 high cost in cache pressure. Thus, handle some simple cases
13290 here which cover the majority of C partial symbols. DIEs
13291 which neither have specification tags in them, nor could have
13292 specification tags elsewhere pointing at them, can simply be
13293 processed and discarded.
13294
13295 This segment is also optional; scan_partial_symbols and
13296 add_partial_symbol will handle these DIEs if we chain
13297 them in normally. When compilers which do not emit large
13298 quantities of duplicate debug information are more common,
13299 this code can probably be removed. */
13300
13301 /* Any complete simple types at the top level (pretty much all
13302 of them, for a language without namespaces), can be processed
13303 directly. */
13304 if (parent_die == NULL
13305 && part_die->has_specification == 0
13306 && part_die->is_declaration == 0
d8228535 13307 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13308 || part_die->tag == DW_TAG_base_type
13309 || part_die->tag == DW_TAG_subrange_type))
13310 {
13311 if (building_psymtab && part_die->name != NULL)
04a679b8 13312 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13313 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13314 &objfile->static_psymbols,
13315 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13316 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13317 continue;
13318 }
13319
d8228535
JK
13320 /* The exception for DW_TAG_typedef with has_children above is
13321 a workaround of GCC PR debug/47510. In the case of this complaint
13322 type_name_no_tag_or_error will error on such types later.
13323
13324 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13325 it could not find the child DIEs referenced later, this is checked
13326 above. In correct DWARF DW_TAG_typedef should have no children. */
13327
13328 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13329 complaint (&symfile_complaints,
13330 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13331 "- DIE at 0x%x [in module %s]"),
b64f50a1 13332 part_die->offset.sect_off, objfile->name);
d8228535 13333
72bf9492
DJ
13334 /* If we're at the second level, and we're an enumerator, and
13335 our parent has no specification (meaning possibly lives in a
13336 namespace elsewhere), then we can add the partial symbol now
13337 instead of queueing it. */
13338 if (part_die->tag == DW_TAG_enumerator
13339 && parent_die != NULL
13340 && parent_die->die_parent == NULL
13341 && parent_die->tag == DW_TAG_enumeration_type
13342 && parent_die->has_specification == 0)
13343 {
13344 if (part_die->name == NULL)
3e43a32a
MS
13345 complaint (&symfile_complaints,
13346 _("malformed enumerator DIE ignored"));
72bf9492 13347 else if (building_psymtab)
04a679b8 13348 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13349 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13350 (cu->language == language_cplus
13351 || cu->language == language_java)
bb5ed363
DE
13352 ? &objfile->global_psymbols
13353 : &objfile->static_psymbols,
13354 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13355
dee91e82 13356 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13357 continue;
13358 }
13359
13360 /* We'll save this DIE so link it in. */
13361 part_die->die_parent = parent_die;
13362 part_die->die_sibling = NULL;
13363 part_die->die_child = NULL;
13364
13365 if (last_die && last_die == parent_die)
13366 last_die->die_child = part_die;
13367 else if (last_die)
13368 last_die->die_sibling = part_die;
13369
13370 last_die = part_die;
13371
13372 if (first_die == NULL)
13373 first_die = part_die;
13374
13375 /* Maybe add the DIE to the hash table. Not all DIEs that we
13376 find interesting need to be in the hash table, because we
13377 also have the parent/sibling/child chains; only those that we
13378 might refer to by offset later during partial symbol reading.
13379
13380 For now this means things that might have be the target of a
13381 DW_AT_specification, DW_AT_abstract_origin, or
13382 DW_AT_extension. DW_AT_extension will refer only to
13383 namespaces; DW_AT_abstract_origin refers to functions (and
13384 many things under the function DIE, but we do not recurse
13385 into function DIEs during partial symbol reading) and
13386 possibly variables as well; DW_AT_specification refers to
13387 declarations. Declarations ought to have the DW_AT_declaration
13388 flag. It happens that GCC forgets to put it in sometimes, but
13389 only for functions, not for types.
13390
13391 Adding more things than necessary to the hash table is harmless
13392 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13393 wasted time in find_partial_die, when we reread the compilation
13394 unit with load_all_dies set. */
72bf9492 13395
5afb4e99 13396 if (load_all
72929c62 13397 || abbrev->tag == DW_TAG_constant
5afb4e99 13398 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13399 || abbrev->tag == DW_TAG_variable
13400 || abbrev->tag == DW_TAG_namespace
13401 || part_die->is_declaration)
13402 {
13403 void **slot;
13404
13405 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13406 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13407 *slot = part_die;
13408 }
13409
13410 part_die = obstack_alloc (&cu->comp_unit_obstack,
13411 sizeof (struct partial_die_info));
13412
13413 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13414 we have no reason to follow the children of structures; for other
98bfdba5
PA
13415 languages we have to, so that we can get at method physnames
13416 to infer fully qualified class names, for DW_AT_specification,
13417 and for C++ template arguments. For C++, we also look one level
13418 inside functions to find template arguments (if the name of the
13419 function does not already contain the template arguments).
bc30ff58
JB
13420
13421 For Ada, we need to scan the children of subprograms and lexical
13422 blocks as well because Ada allows the definition of nested
13423 entities that could be interesting for the debugger, such as
13424 nested subprograms for instance. */
72bf9492 13425 if (last_die->has_children
5afb4e99
DJ
13426 && (load_all
13427 || last_die->tag == DW_TAG_namespace
f55ee35c 13428 || last_die->tag == DW_TAG_module
72bf9492 13429 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13430 || (cu->language == language_cplus
13431 && last_die->tag == DW_TAG_subprogram
13432 && (last_die->name == NULL
13433 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13434 || (cu->language != language_c
13435 && (last_die->tag == DW_TAG_class_type
680b30c7 13436 || last_die->tag == DW_TAG_interface_type
72bf9492 13437 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13438 || last_die->tag == DW_TAG_union_type))
13439 || (cu->language == language_ada
13440 && (last_die->tag == DW_TAG_subprogram
13441 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13442 {
13443 nesting_level++;
13444 parent_die = last_die;
13445 continue;
13446 }
13447
13448 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13449 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13450
13451 /* Back to the top, do it again. */
13452 }
13453}
13454
c906108c
SS
13455/* Read a minimal amount of information into the minimal die structure. */
13456
fe1b8b76 13457static gdb_byte *
dee91e82
DE
13458read_partial_die (const struct die_reader_specs *reader,
13459 struct partial_die_info *part_die,
13460 struct abbrev_info *abbrev, unsigned int abbrev_len,
13461 gdb_byte *info_ptr)
c906108c 13462{
dee91e82 13463 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13464 struct objfile *objfile = cu->objfile;
dee91e82 13465 gdb_byte *buffer = reader->buffer;
fa238c03 13466 unsigned int i;
c906108c 13467 struct attribute attr;
c5aa993b 13468 int has_low_pc_attr = 0;
c906108c 13469 int has_high_pc_attr = 0;
91da1414 13470 int high_pc_relative = 0;
c906108c 13471
72bf9492 13472 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13473
b64f50a1 13474 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13475
13476 info_ptr += abbrev_len;
13477
13478 if (abbrev == NULL)
13479 return info_ptr;
13480
c906108c
SS
13481 part_die->tag = abbrev->tag;
13482 part_die->has_children = abbrev->has_children;
c906108c
SS
13483
13484 for (i = 0; i < abbrev->num_attrs; ++i)
13485 {
dee91e82 13486 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13487
13488 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13489 partial symbol table. */
c906108c
SS
13490 switch (attr.name)
13491 {
13492 case DW_AT_name:
71c25dea
TT
13493 switch (part_die->tag)
13494 {
13495 case DW_TAG_compile_unit:
95554aad 13496 case DW_TAG_partial_unit:
348e048f 13497 case DW_TAG_type_unit:
71c25dea
TT
13498 /* Compilation units have a DW_AT_name that is a filename, not
13499 a source language identifier. */
13500 case DW_TAG_enumeration_type:
13501 case DW_TAG_enumerator:
13502 /* These tags always have simple identifiers already; no need
13503 to canonicalize them. */
13504 part_die->name = DW_STRING (&attr);
13505 break;
13506 default:
13507 part_die->name
13508 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13509 &objfile->objfile_obstack);
71c25dea
TT
13510 break;
13511 }
c906108c 13512 break;
31ef98ae 13513 case DW_AT_linkage_name:
c906108c 13514 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13515 /* Note that both forms of linkage name might appear. We
13516 assume they will be the same, and we only store the last
13517 one we see. */
94af9270
KS
13518 if (cu->language == language_ada)
13519 part_die->name = DW_STRING (&attr);
abc72ce4 13520 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13521 break;
13522 case DW_AT_low_pc:
13523 has_low_pc_attr = 1;
13524 part_die->lowpc = DW_ADDR (&attr);
13525 break;
13526 case DW_AT_high_pc:
13527 has_high_pc_attr = 1;
3019eac3
DE
13528 if (attr.form == DW_FORM_addr
13529 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13530 part_die->highpc = DW_ADDR (&attr);
13531 else
13532 {
13533 high_pc_relative = 1;
13534 part_die->highpc = DW_UNSND (&attr);
13535 }
c906108c
SS
13536 break;
13537 case DW_AT_location:
0963b4bd 13538 /* Support the .debug_loc offsets. */
8e19ed76
PS
13539 if (attr_form_is_block (&attr))
13540 {
95554aad 13541 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13542 }
3690dd37 13543 else if (attr_form_is_section_offset (&attr))
8e19ed76 13544 {
4d3c2250 13545 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13546 }
13547 else
13548 {
4d3c2250
KB
13549 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13550 "partial symbol information");
8e19ed76 13551 }
c906108c 13552 break;
c906108c
SS
13553 case DW_AT_external:
13554 part_die->is_external = DW_UNSND (&attr);
13555 break;
13556 case DW_AT_declaration:
13557 part_die->is_declaration = DW_UNSND (&attr);
13558 break;
13559 case DW_AT_type:
13560 part_die->has_type = 1;
13561 break;
13562 case DW_AT_abstract_origin:
13563 case DW_AT_specification:
72bf9492
DJ
13564 case DW_AT_extension:
13565 part_die->has_specification = 1;
c764a876 13566 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13567 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13568 || cu->per_cu->is_dwz);
c906108c
SS
13569 break;
13570 case DW_AT_sibling:
13571 /* Ignore absolute siblings, they might point outside of
13572 the current compile unit. */
13573 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13574 complaint (&symfile_complaints,
13575 _("ignoring absolute DW_AT_sibling"));
c906108c 13576 else
b64f50a1 13577 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13578 break;
fa4028e9
JB
13579 case DW_AT_byte_size:
13580 part_die->has_byte_size = 1;
13581 break;
68511cec
CES
13582 case DW_AT_calling_convention:
13583 /* DWARF doesn't provide a way to identify a program's source-level
13584 entry point. DW_AT_calling_convention attributes are only meant
13585 to describe functions' calling conventions.
13586
13587 However, because it's a necessary piece of information in
13588 Fortran, and because DW_CC_program is the only piece of debugging
13589 information whose definition refers to a 'main program' at all,
13590 several compilers have begun marking Fortran main programs with
13591 DW_CC_program --- even when those functions use the standard
13592 calling conventions.
13593
13594 So until DWARF specifies a way to provide this information and
13595 compilers pick up the new representation, we'll support this
13596 practice. */
13597 if (DW_UNSND (&attr) == DW_CC_program
13598 && cu->language == language_fortran)
01f8c46d
JK
13599 {
13600 set_main_name (part_die->name);
13601
13602 /* As this DIE has a static linkage the name would be difficult
13603 to look up later. */
13604 language_of_main = language_fortran;
13605 }
68511cec 13606 break;
481860b3
GB
13607 case DW_AT_inline:
13608 if (DW_UNSND (&attr) == DW_INL_inlined
13609 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13610 part_die->may_be_inlined = 1;
13611 break;
95554aad
TT
13612
13613 case DW_AT_import:
13614 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13615 {
13616 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13617 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13618 || cu->per_cu->is_dwz);
13619 }
95554aad
TT
13620 break;
13621
c906108c
SS
13622 default:
13623 break;
13624 }
13625 }
13626
91da1414
MW
13627 if (high_pc_relative)
13628 part_die->highpc += part_die->lowpc;
13629
9373cf26
JK
13630 if (has_low_pc_attr && has_high_pc_attr)
13631 {
13632 /* When using the GNU linker, .gnu.linkonce. sections are used to
13633 eliminate duplicate copies of functions and vtables and such.
13634 The linker will arbitrarily choose one and discard the others.
13635 The AT_*_pc values for such functions refer to local labels in
13636 these sections. If the section from that file was discarded, the
13637 labels are not in the output, so the relocs get a value of 0.
13638 If this is a discarded function, mark the pc bounds as invalid,
13639 so that GDB will ignore it. */
13640 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13641 {
bb5ed363 13642 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13643
13644 complaint (&symfile_complaints,
13645 _("DW_AT_low_pc %s is zero "
13646 "for DIE at 0x%x [in module %s]"),
13647 paddress (gdbarch, part_die->lowpc),
b64f50a1 13648 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13649 }
13650 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13651 else if (part_die->lowpc >= part_die->highpc)
13652 {
bb5ed363 13653 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13654
13655 complaint (&symfile_complaints,
13656 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13657 "for DIE at 0x%x [in module %s]"),
13658 paddress (gdbarch, part_die->lowpc),
13659 paddress (gdbarch, part_die->highpc),
b64f50a1 13660 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13661 }
13662 else
13663 part_die->has_pc_info = 1;
13664 }
85cbf3d3 13665
c906108c
SS
13666 return info_ptr;
13667}
13668
72bf9492
DJ
13669/* Find a cached partial DIE at OFFSET in CU. */
13670
13671static struct partial_die_info *
b64f50a1 13672find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13673{
13674 struct partial_die_info *lookup_die = NULL;
13675 struct partial_die_info part_die;
13676
13677 part_die.offset = offset;
b64f50a1
JK
13678 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13679 offset.sect_off);
72bf9492 13680
72bf9492
DJ
13681 return lookup_die;
13682}
13683
348e048f
DE
13684/* Find a partial DIE at OFFSET, which may or may not be in CU,
13685 except in the case of .debug_types DIEs which do not reference
13686 outside their CU (they do however referencing other types via
55f1336d 13687 DW_FORM_ref_sig8). */
72bf9492
DJ
13688
13689static struct partial_die_info *
36586728 13690find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13691{
bb5ed363 13692 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13693 struct dwarf2_per_cu_data *per_cu = NULL;
13694 struct partial_die_info *pd = NULL;
72bf9492 13695
36586728
TT
13696 if (offset_in_dwz == cu->per_cu->is_dwz
13697 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13698 {
13699 pd = find_partial_die_in_comp_unit (offset, cu);
13700 if (pd != NULL)
13701 return pd;
0d99eb77
DE
13702 /* We missed recording what we needed.
13703 Load all dies and try again. */
13704 per_cu = cu->per_cu;
5afb4e99 13705 }
0d99eb77
DE
13706 else
13707 {
13708 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13709 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13710 {
13711 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13712 " external reference to offset 0x%lx [in module %s].\n"),
13713 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13714 bfd_get_filename (objfile->obfd));
13715 }
36586728
TT
13716 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13717 objfile);
72bf9492 13718
0d99eb77
DE
13719 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13720 load_partial_comp_unit (per_cu);
ae038cb0 13721
0d99eb77
DE
13722 per_cu->cu->last_used = 0;
13723 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13724 }
5afb4e99 13725
dee91e82
DE
13726 /* If we didn't find it, and not all dies have been loaded,
13727 load them all and try again. */
13728
5afb4e99
DJ
13729 if (pd == NULL && per_cu->load_all_dies == 0)
13730 {
5afb4e99 13731 per_cu->load_all_dies = 1;
fd820528
DE
13732
13733 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13734 THIS_CU->cu may already be in use. So we can't just free it and
13735 replace its DIEs with the ones we read in. Instead, we leave those
13736 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13737 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13738 set. */
dee91e82 13739 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13740
13741 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13742 }
13743
13744 if (pd == NULL)
13745 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13746 _("could not find partial DIE 0x%x "
13747 "in cache [from module %s]\n"),
b64f50a1 13748 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13749 return pd;
72bf9492
DJ
13750}
13751
abc72ce4
DE
13752/* See if we can figure out if the class lives in a namespace. We do
13753 this by looking for a member function; its demangled name will
13754 contain namespace info, if there is any. */
13755
13756static void
13757guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13758 struct dwarf2_cu *cu)
13759{
13760 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13761 what template types look like, because the demangler
13762 frequently doesn't give the same name as the debug info. We
13763 could fix this by only using the demangled name to get the
13764 prefix (but see comment in read_structure_type). */
13765
13766 struct partial_die_info *real_pdi;
13767 struct partial_die_info *child_pdi;
13768
13769 /* If this DIE (this DIE's specification, if any) has a parent, then
13770 we should not do this. We'll prepend the parent's fully qualified
13771 name when we create the partial symbol. */
13772
13773 real_pdi = struct_pdi;
13774 while (real_pdi->has_specification)
36586728
TT
13775 real_pdi = find_partial_die (real_pdi->spec_offset,
13776 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13777
13778 if (real_pdi->die_parent != NULL)
13779 return;
13780
13781 for (child_pdi = struct_pdi->die_child;
13782 child_pdi != NULL;
13783 child_pdi = child_pdi->die_sibling)
13784 {
13785 if (child_pdi->tag == DW_TAG_subprogram
13786 && child_pdi->linkage_name != NULL)
13787 {
13788 char *actual_class_name
13789 = language_class_name_from_physname (cu->language_defn,
13790 child_pdi->linkage_name);
13791 if (actual_class_name != NULL)
13792 {
13793 struct_pdi->name
13794 = obsavestring (actual_class_name,
13795 strlen (actual_class_name),
13796 &cu->objfile->objfile_obstack);
13797 xfree (actual_class_name);
13798 }
13799 break;
13800 }
13801 }
13802}
13803
72bf9492
DJ
13804/* Adjust PART_DIE before generating a symbol for it. This function
13805 may set the is_external flag or change the DIE's name. */
13806
13807static void
13808fixup_partial_die (struct partial_die_info *part_die,
13809 struct dwarf2_cu *cu)
13810{
abc72ce4
DE
13811 /* Once we've fixed up a die, there's no point in doing so again.
13812 This also avoids a memory leak if we were to call
13813 guess_partial_die_structure_name multiple times. */
13814 if (part_die->fixup_called)
13815 return;
13816
72bf9492
DJ
13817 /* If we found a reference attribute and the DIE has no name, try
13818 to find a name in the referred to DIE. */
13819
13820 if (part_die->name == NULL && part_die->has_specification)
13821 {
13822 struct partial_die_info *spec_die;
72bf9492 13823
36586728
TT
13824 spec_die = find_partial_die (part_die->spec_offset,
13825 part_die->spec_is_dwz, cu);
72bf9492 13826
10b3939b 13827 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13828
13829 if (spec_die->name)
13830 {
13831 part_die->name = spec_die->name;
13832
13833 /* Copy DW_AT_external attribute if it is set. */
13834 if (spec_die->is_external)
13835 part_die->is_external = spec_die->is_external;
13836 }
13837 }
13838
13839 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13840
13841 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13842 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13843
abc72ce4
DE
13844 /* If there is no parent die to provide a namespace, and there are
13845 children, see if we can determine the namespace from their linkage
122d1940 13846 name. */
abc72ce4 13847 if (cu->language == language_cplus
8b70b953 13848 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13849 && part_die->die_parent == NULL
13850 && part_die->has_children
13851 && (part_die->tag == DW_TAG_class_type
13852 || part_die->tag == DW_TAG_structure_type
13853 || part_die->tag == DW_TAG_union_type))
13854 guess_partial_die_structure_name (part_die, cu);
13855
53832f31
TT
13856 /* GCC might emit a nameless struct or union that has a linkage
13857 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13858 if (part_die->name == NULL
96408a79
SA
13859 && (part_die->tag == DW_TAG_class_type
13860 || part_die->tag == DW_TAG_interface_type
13861 || part_die->tag == DW_TAG_structure_type
13862 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13863 && part_die->linkage_name != NULL)
13864 {
13865 char *demangled;
13866
13867 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13868 if (demangled)
13869 {
96408a79
SA
13870 const char *base;
13871
13872 /* Strip any leading namespaces/classes, keep only the base name.
13873 DW_AT_name for named DIEs does not contain the prefixes. */
13874 base = strrchr (demangled, ':');
13875 if (base && base > demangled && base[-1] == ':')
13876 base++;
13877 else
13878 base = demangled;
13879
13880 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13881 &cu->objfile->objfile_obstack);
13882 xfree (demangled);
13883 }
13884 }
13885
abc72ce4 13886 part_die->fixup_called = 1;
72bf9492
DJ
13887}
13888
a8329558 13889/* Read an attribute value described by an attribute form. */
c906108c 13890
fe1b8b76 13891static gdb_byte *
dee91e82
DE
13892read_attribute_value (const struct die_reader_specs *reader,
13893 struct attribute *attr, unsigned form,
13894 gdb_byte *info_ptr)
c906108c 13895{
dee91e82
DE
13896 struct dwarf2_cu *cu = reader->cu;
13897 bfd *abfd = reader->abfd;
e7c27a73 13898 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13899 unsigned int bytes_read;
13900 struct dwarf_block *blk;
13901
a8329558
KW
13902 attr->form = form;
13903 switch (form)
c906108c 13904 {
c906108c 13905 case DW_FORM_ref_addr:
ae411497 13906 if (cu->header.version == 2)
4568ecf9 13907 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13908 else
4568ecf9
DE
13909 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13910 &cu->header, &bytes_read);
ae411497
TT
13911 info_ptr += bytes_read;
13912 break;
36586728
TT
13913 case DW_FORM_GNU_ref_alt:
13914 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13915 info_ptr += bytes_read;
13916 break;
ae411497 13917 case DW_FORM_addr:
e7c27a73 13918 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13919 info_ptr += bytes_read;
c906108c
SS
13920 break;
13921 case DW_FORM_block2:
7b5a2f43 13922 blk = dwarf_alloc_block (cu);
c906108c
SS
13923 blk->size = read_2_bytes (abfd, info_ptr);
13924 info_ptr += 2;
13925 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13926 info_ptr += blk->size;
13927 DW_BLOCK (attr) = blk;
13928 break;
13929 case DW_FORM_block4:
7b5a2f43 13930 blk = dwarf_alloc_block (cu);
c906108c
SS
13931 blk->size = read_4_bytes (abfd, info_ptr);
13932 info_ptr += 4;
13933 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13934 info_ptr += blk->size;
13935 DW_BLOCK (attr) = blk;
13936 break;
13937 case DW_FORM_data2:
13938 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13939 info_ptr += 2;
13940 break;
13941 case DW_FORM_data4:
13942 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13943 info_ptr += 4;
13944 break;
13945 case DW_FORM_data8:
13946 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13947 info_ptr += 8;
13948 break;
2dc7f7b3
TT
13949 case DW_FORM_sec_offset:
13950 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13951 info_ptr += bytes_read;
13952 break;
c906108c 13953 case DW_FORM_string:
9b1c24c8 13954 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13955 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13956 info_ptr += bytes_read;
13957 break;
4bdf3d34 13958 case DW_FORM_strp:
36586728
TT
13959 if (!cu->per_cu->is_dwz)
13960 {
13961 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13962 &bytes_read);
13963 DW_STRING_IS_CANONICAL (attr) = 0;
13964 info_ptr += bytes_read;
13965 break;
13966 }
13967 /* FALLTHROUGH */
13968 case DW_FORM_GNU_strp_alt:
13969 {
13970 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13971 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13972 &bytes_read);
13973
13974 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13975 DW_STRING_IS_CANONICAL (attr) = 0;
13976 info_ptr += bytes_read;
13977 }
4bdf3d34 13978 break;
2dc7f7b3 13979 case DW_FORM_exprloc:
c906108c 13980 case DW_FORM_block:
7b5a2f43 13981 blk = dwarf_alloc_block (cu);
c906108c
SS
13982 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13983 info_ptr += bytes_read;
13984 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13985 info_ptr += blk->size;
13986 DW_BLOCK (attr) = blk;
13987 break;
13988 case DW_FORM_block1:
7b5a2f43 13989 blk = dwarf_alloc_block (cu);
c906108c
SS
13990 blk->size = read_1_byte (abfd, info_ptr);
13991 info_ptr += 1;
13992 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13993 info_ptr += blk->size;
13994 DW_BLOCK (attr) = blk;
13995 break;
13996 case DW_FORM_data1:
13997 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13998 info_ptr += 1;
13999 break;
14000 case DW_FORM_flag:
14001 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14002 info_ptr += 1;
14003 break;
2dc7f7b3
TT
14004 case DW_FORM_flag_present:
14005 DW_UNSND (attr) = 1;
14006 break;
c906108c
SS
14007 case DW_FORM_sdata:
14008 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14009 info_ptr += bytes_read;
14010 break;
14011 case DW_FORM_udata:
14012 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14013 info_ptr += bytes_read;
14014 break;
14015 case DW_FORM_ref1:
4568ecf9
DE
14016 DW_UNSND (attr) = (cu->header.offset.sect_off
14017 + read_1_byte (abfd, info_ptr));
c906108c
SS
14018 info_ptr += 1;
14019 break;
14020 case DW_FORM_ref2:
4568ecf9
DE
14021 DW_UNSND (attr) = (cu->header.offset.sect_off
14022 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14023 info_ptr += 2;
14024 break;
14025 case DW_FORM_ref4:
4568ecf9
DE
14026 DW_UNSND (attr) = (cu->header.offset.sect_off
14027 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14028 info_ptr += 4;
14029 break;
613e1657 14030 case DW_FORM_ref8:
4568ecf9
DE
14031 DW_UNSND (attr) = (cu->header.offset.sect_off
14032 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14033 info_ptr += 8;
14034 break;
55f1336d 14035 case DW_FORM_ref_sig8:
348e048f
DE
14036 /* Convert the signature to something we can record in DW_UNSND
14037 for later lookup.
14038 NOTE: This is NULL if the type wasn't found. */
14039 DW_SIGNATURED_TYPE (attr) =
e319fa28 14040 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14041 info_ptr += 8;
14042 break;
c906108c 14043 case DW_FORM_ref_udata:
4568ecf9
DE
14044 DW_UNSND (attr) = (cu->header.offset.sect_off
14045 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14046 info_ptr += bytes_read;
14047 break;
c906108c 14048 case DW_FORM_indirect:
a8329558
KW
14049 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14050 info_ptr += bytes_read;
dee91e82 14051 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14052 break;
3019eac3
DE
14053 case DW_FORM_GNU_addr_index:
14054 if (reader->dwo_file == NULL)
14055 {
14056 /* For now flag a hard error.
14057 Later we can turn this into a complaint. */
14058 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14059 dwarf_form_name (form),
14060 bfd_get_filename (abfd));
14061 }
14062 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14063 info_ptr += bytes_read;
14064 break;
14065 case DW_FORM_GNU_str_index:
14066 if (reader->dwo_file == NULL)
14067 {
14068 /* For now flag a hard error.
14069 Later we can turn this into a complaint if warranted. */
14070 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14071 dwarf_form_name (form),
14072 bfd_get_filename (abfd));
14073 }
14074 {
14075 ULONGEST str_index =
14076 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14077
14078 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14079 DW_STRING_IS_CANONICAL (attr) = 0;
14080 info_ptr += bytes_read;
14081 }
14082 break;
c906108c 14083 default:
8a3fe4f8 14084 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14085 dwarf_form_name (form),
14086 bfd_get_filename (abfd));
c906108c 14087 }
28e94949 14088
36586728
TT
14089 /* Super hack. */
14090 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14091 attr->form = DW_FORM_GNU_ref_alt;
14092
28e94949
JB
14093 /* We have seen instances where the compiler tried to emit a byte
14094 size attribute of -1 which ended up being encoded as an unsigned
14095 0xffffffff. Although 0xffffffff is technically a valid size value,
14096 an object of this size seems pretty unlikely so we can relatively
14097 safely treat these cases as if the size attribute was invalid and
14098 treat them as zero by default. */
14099 if (attr->name == DW_AT_byte_size
14100 && form == DW_FORM_data4
14101 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14102 {
14103 complaint
14104 (&symfile_complaints,
43bbcdc2
PH
14105 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14106 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14107 DW_UNSND (attr) = 0;
14108 }
28e94949 14109
c906108c
SS
14110 return info_ptr;
14111}
14112
a8329558
KW
14113/* Read an attribute described by an abbreviated attribute. */
14114
fe1b8b76 14115static gdb_byte *
dee91e82
DE
14116read_attribute (const struct die_reader_specs *reader,
14117 struct attribute *attr, struct attr_abbrev *abbrev,
14118 gdb_byte *info_ptr)
a8329558
KW
14119{
14120 attr->name = abbrev->name;
dee91e82 14121 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14122}
14123
0963b4bd 14124/* Read dwarf information from a buffer. */
c906108c
SS
14125
14126static unsigned int
a1855c1d 14127read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14128{
fe1b8b76 14129 return bfd_get_8 (abfd, buf);
c906108c
SS
14130}
14131
14132static int
a1855c1d 14133read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14134{
fe1b8b76 14135 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14136}
14137
14138static unsigned int
a1855c1d 14139read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14140{
fe1b8b76 14141 return bfd_get_16 (abfd, buf);
c906108c
SS
14142}
14143
21ae7a4d 14144static int
a1855c1d 14145read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14146{
14147 return bfd_get_signed_16 (abfd, buf);
14148}
14149
c906108c 14150static unsigned int
a1855c1d 14151read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14152{
fe1b8b76 14153 return bfd_get_32 (abfd, buf);
c906108c
SS
14154}
14155
21ae7a4d 14156static int
a1855c1d 14157read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14158{
14159 return bfd_get_signed_32 (abfd, buf);
14160}
14161
93311388 14162static ULONGEST
a1855c1d 14163read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14164{
fe1b8b76 14165 return bfd_get_64 (abfd, buf);
c906108c
SS
14166}
14167
14168static CORE_ADDR
fe1b8b76 14169read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14170 unsigned int *bytes_read)
c906108c 14171{
e7c27a73 14172 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14173 CORE_ADDR retval = 0;
14174
107d2387 14175 if (cu_header->signed_addr_p)
c906108c 14176 {
107d2387
AC
14177 switch (cu_header->addr_size)
14178 {
14179 case 2:
fe1b8b76 14180 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14181 break;
14182 case 4:
fe1b8b76 14183 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14184 break;
14185 case 8:
fe1b8b76 14186 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14187 break;
14188 default:
8e65ff28 14189 internal_error (__FILE__, __LINE__,
e2e0b3e5 14190 _("read_address: bad switch, signed [in module %s]"),
659b0389 14191 bfd_get_filename (abfd));
107d2387
AC
14192 }
14193 }
14194 else
14195 {
14196 switch (cu_header->addr_size)
14197 {
14198 case 2:
fe1b8b76 14199 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14200 break;
14201 case 4:
fe1b8b76 14202 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14203 break;
14204 case 8:
fe1b8b76 14205 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14206 break;
14207 default:
8e65ff28 14208 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14209 _("read_address: bad switch, "
14210 "unsigned [in module %s]"),
659b0389 14211 bfd_get_filename (abfd));
107d2387 14212 }
c906108c 14213 }
64367e0a 14214
107d2387
AC
14215 *bytes_read = cu_header->addr_size;
14216 return retval;
c906108c
SS
14217}
14218
f7ef9339 14219/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14220 specification allows the initial length to take up either 4 bytes
14221 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14222 bytes describe the length and all offsets will be 8 bytes in length
14223 instead of 4.
14224
f7ef9339
KB
14225 An older, non-standard 64-bit format is also handled by this
14226 function. The older format in question stores the initial length
14227 as an 8-byte quantity without an escape value. Lengths greater
14228 than 2^32 aren't very common which means that the initial 4 bytes
14229 is almost always zero. Since a length value of zero doesn't make
14230 sense for the 32-bit format, this initial zero can be considered to
14231 be an escape value which indicates the presence of the older 64-bit
14232 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14233 greater than 4GB. If it becomes necessary to handle lengths
14234 somewhat larger than 4GB, we could allow other small values (such
14235 as the non-sensical values of 1, 2, and 3) to also be used as
14236 escape values indicating the presence of the old format.
f7ef9339 14237
917c78fc
MK
14238 The value returned via bytes_read should be used to increment the
14239 relevant pointer after calling read_initial_length().
c764a876 14240
613e1657
KB
14241 [ Note: read_initial_length() and read_offset() are based on the
14242 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14243 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14244 from:
14245
f7ef9339 14246 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14247
613e1657
KB
14248 This document is only a draft and is subject to change. (So beware.)
14249
f7ef9339 14250 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14251 determined empirically by examining 64-bit ELF files produced by
14252 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14253
14254 - Kevin, July 16, 2002
613e1657
KB
14255 ] */
14256
14257static LONGEST
c764a876 14258read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14259{
fe1b8b76 14260 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14261
dd373385 14262 if (length == 0xffffffff)
613e1657 14263 {
fe1b8b76 14264 length = bfd_get_64 (abfd, buf + 4);
613e1657 14265 *bytes_read = 12;
613e1657 14266 }
dd373385 14267 else if (length == 0)
f7ef9339 14268 {
dd373385 14269 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14270 length = bfd_get_64 (abfd, buf);
f7ef9339 14271 *bytes_read = 8;
f7ef9339 14272 }
613e1657
KB
14273 else
14274 {
14275 *bytes_read = 4;
613e1657
KB
14276 }
14277
c764a876
DE
14278 return length;
14279}
dd373385 14280
c764a876
DE
14281/* Cover function for read_initial_length.
14282 Returns the length of the object at BUF, and stores the size of the
14283 initial length in *BYTES_READ and stores the size that offsets will be in
14284 *OFFSET_SIZE.
14285 If the initial length size is not equivalent to that specified in
14286 CU_HEADER then issue a complaint.
14287 This is useful when reading non-comp-unit headers. */
dd373385 14288
c764a876
DE
14289static LONGEST
14290read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14291 const struct comp_unit_head *cu_header,
14292 unsigned int *bytes_read,
14293 unsigned int *offset_size)
14294{
14295 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14296
14297 gdb_assert (cu_header->initial_length_size == 4
14298 || cu_header->initial_length_size == 8
14299 || cu_header->initial_length_size == 12);
14300
14301 if (cu_header->initial_length_size != *bytes_read)
14302 complaint (&symfile_complaints,
14303 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14304
c764a876 14305 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14306 return length;
613e1657
KB
14307}
14308
14309/* Read an offset from the data stream. The size of the offset is
917c78fc 14310 given by cu_header->offset_size. */
613e1657
KB
14311
14312static LONGEST
fe1b8b76 14313read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14314 unsigned int *bytes_read)
c764a876
DE
14315{
14316 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14317
c764a876
DE
14318 *bytes_read = cu_header->offset_size;
14319 return offset;
14320}
14321
14322/* Read an offset from the data stream. */
14323
14324static LONGEST
14325read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14326{
14327 LONGEST retval = 0;
14328
c764a876 14329 switch (offset_size)
613e1657
KB
14330 {
14331 case 4:
fe1b8b76 14332 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14333 break;
14334 case 8:
fe1b8b76 14335 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14336 break;
14337 default:
8e65ff28 14338 internal_error (__FILE__, __LINE__,
c764a876 14339 _("read_offset_1: bad switch [in module %s]"),
659b0389 14340 bfd_get_filename (abfd));
613e1657
KB
14341 }
14342
917c78fc 14343 return retval;
613e1657
KB
14344}
14345
fe1b8b76
JB
14346static gdb_byte *
14347read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14348{
14349 /* If the size of a host char is 8 bits, we can return a pointer
14350 to the buffer, otherwise we have to copy the data to a buffer
14351 allocated on the temporary obstack. */
4bdf3d34 14352 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14353 return buf;
c906108c
SS
14354}
14355
14356static char *
9b1c24c8 14357read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14358{
14359 /* If the size of a host char is 8 bits, we can return a pointer
14360 to the string, otherwise we have to copy the string to a buffer
14361 allocated on the temporary obstack. */
4bdf3d34 14362 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14363 if (*buf == '\0')
14364 {
14365 *bytes_read_ptr = 1;
14366 return NULL;
14367 }
fe1b8b76
JB
14368 *bytes_read_ptr = strlen ((char *) buf) + 1;
14369 return (char *) buf;
4bdf3d34
JJ
14370}
14371
14372static char *
cf2c3c16 14373read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14374{
be391dca 14375 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14376 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14377 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14378 bfd_get_filename (abfd));
dce234bc 14379 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14380 error (_("DW_FORM_strp pointing outside of "
14381 ".debug_str section [in module %s]"),
14382 bfd_get_filename (abfd));
4bdf3d34 14383 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14384 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14385 return NULL;
dce234bc 14386 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14387}
14388
36586728
TT
14389/* Read a string at offset STR_OFFSET in the .debug_str section from
14390 the .dwz file DWZ. Throw an error if the offset is too large. If
14391 the string consists of a single NUL byte, return NULL; otherwise
14392 return a pointer to the string. */
14393
14394static char *
14395read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14396{
14397 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14398
14399 if (dwz->str.buffer == NULL)
14400 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14401 "section [in module %s]"),
14402 bfd_get_filename (dwz->dwz_bfd));
14403 if (str_offset >= dwz->str.size)
14404 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14405 ".debug_str section [in module %s]"),
14406 bfd_get_filename (dwz->dwz_bfd));
14407 gdb_assert (HOST_CHAR_BIT == 8);
14408 if (dwz->str.buffer[str_offset] == '\0')
14409 return NULL;
14410 return (char *) (dwz->str.buffer + str_offset);
14411}
14412
cf2c3c16
TT
14413static char *
14414read_indirect_string (bfd *abfd, gdb_byte *buf,
14415 const struct comp_unit_head *cu_header,
14416 unsigned int *bytes_read_ptr)
14417{
14418 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14419
14420 return read_indirect_string_at_offset (abfd, str_offset);
14421}
14422
12df843f 14423static ULONGEST
fe1b8b76 14424read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14425{
12df843f 14426 ULONGEST result;
ce5d95e1 14427 unsigned int num_read;
c906108c
SS
14428 int i, shift;
14429 unsigned char byte;
14430
14431 result = 0;
14432 shift = 0;
14433 num_read = 0;
14434 i = 0;
14435 while (1)
14436 {
fe1b8b76 14437 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14438 buf++;
14439 num_read++;
12df843f 14440 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14441 if ((byte & 128) == 0)
14442 {
14443 break;
14444 }
14445 shift += 7;
14446 }
14447 *bytes_read_ptr = num_read;
14448 return result;
14449}
14450
12df843f 14451static LONGEST
fe1b8b76 14452read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14453{
12df843f 14454 LONGEST result;
77e0b926 14455 int i, shift, num_read;
c906108c
SS
14456 unsigned char byte;
14457
14458 result = 0;
14459 shift = 0;
c906108c
SS
14460 num_read = 0;
14461 i = 0;
14462 while (1)
14463 {
fe1b8b76 14464 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14465 buf++;
14466 num_read++;
12df843f 14467 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14468 shift += 7;
14469 if ((byte & 128) == 0)
14470 {
14471 break;
14472 }
14473 }
77e0b926 14474 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14475 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14476 *bytes_read_ptr = num_read;
14477 return result;
14478}
14479
3019eac3
DE
14480/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14481 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14482 ADDR_SIZE is the size of addresses from the CU header. */
14483
14484static CORE_ADDR
14485read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14486{
14487 struct objfile *objfile = dwarf2_per_objfile->objfile;
14488 bfd *abfd = objfile->obfd;
14489 const gdb_byte *info_ptr;
14490
14491 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14492 if (dwarf2_per_objfile->addr.buffer == NULL)
14493 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14494 objfile->name);
14495 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14496 error (_("DW_FORM_addr_index pointing outside of "
14497 ".debug_addr section [in module %s]"),
14498 objfile->name);
14499 info_ptr = (dwarf2_per_objfile->addr.buffer
14500 + addr_base + addr_index * addr_size);
14501 if (addr_size == 4)
14502 return bfd_get_32 (abfd, info_ptr);
14503 else
14504 return bfd_get_64 (abfd, info_ptr);
14505}
14506
14507/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14508
14509static CORE_ADDR
14510read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14511{
14512 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14513}
14514
14515/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14516
14517static CORE_ADDR
14518read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14519 unsigned int *bytes_read)
14520{
14521 bfd *abfd = cu->objfile->obfd;
14522 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14523
14524 return read_addr_index (cu, addr_index);
14525}
14526
14527/* Data structure to pass results from dwarf2_read_addr_index_reader
14528 back to dwarf2_read_addr_index. */
14529
14530struct dwarf2_read_addr_index_data
14531{
14532 ULONGEST addr_base;
14533 int addr_size;
14534};
14535
14536/* die_reader_func for dwarf2_read_addr_index. */
14537
14538static void
14539dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14540 gdb_byte *info_ptr,
14541 struct die_info *comp_unit_die,
14542 int has_children,
14543 void *data)
14544{
14545 struct dwarf2_cu *cu = reader->cu;
14546 struct dwarf2_read_addr_index_data *aidata =
14547 (struct dwarf2_read_addr_index_data *) data;
14548
14549 aidata->addr_base = cu->addr_base;
14550 aidata->addr_size = cu->header.addr_size;
14551}
14552
14553/* Given an index in .debug_addr, fetch the value.
14554 NOTE: This can be called during dwarf expression evaluation,
14555 long after the debug information has been read, and thus per_cu->cu
14556 may no longer exist. */
14557
14558CORE_ADDR
14559dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14560 unsigned int addr_index)
14561{
14562 struct objfile *objfile = per_cu->objfile;
14563 struct dwarf2_cu *cu = per_cu->cu;
14564 ULONGEST addr_base;
14565 int addr_size;
14566
14567 /* This is intended to be called from outside this file. */
14568 dw2_setup (objfile);
14569
14570 /* We need addr_base and addr_size.
14571 If we don't have PER_CU->cu, we have to get it.
14572 Nasty, but the alternative is storing the needed info in PER_CU,
14573 which at this point doesn't seem justified: it's not clear how frequently
14574 it would get used and it would increase the size of every PER_CU.
14575 Entry points like dwarf2_per_cu_addr_size do a similar thing
14576 so we're not in uncharted territory here.
14577 Alas we need to be a bit more complicated as addr_base is contained
14578 in the DIE.
14579
14580 We don't need to read the entire CU(/TU).
14581 We just need the header and top level die.
a1b64ce1 14582
3019eac3 14583 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14584 For now we skip this optimization. */
3019eac3
DE
14585
14586 if (cu != NULL)
14587 {
14588 addr_base = cu->addr_base;
14589 addr_size = cu->header.addr_size;
14590 }
14591 else
14592 {
14593 struct dwarf2_read_addr_index_data aidata;
14594
a1b64ce1
DE
14595 /* Note: We can't use init_cutu_and_read_dies_simple here,
14596 we need addr_base. */
14597 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14598 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14599 addr_base = aidata.addr_base;
14600 addr_size = aidata.addr_size;
14601 }
14602
14603 return read_addr_index_1 (addr_index, addr_base, addr_size);
14604}
14605
14606/* Given a DW_AT_str_index, fetch the string. */
14607
14608static char *
14609read_str_index (const struct die_reader_specs *reader,
14610 struct dwarf2_cu *cu, ULONGEST str_index)
14611{
14612 struct objfile *objfile = dwarf2_per_objfile->objfile;
14613 const char *dwo_name = objfile->name;
14614 bfd *abfd = objfile->obfd;
14615 struct dwo_sections *sections = &reader->dwo_file->sections;
14616 gdb_byte *info_ptr;
14617 ULONGEST str_offset;
14618
14619 dwarf2_read_section (objfile, &sections->str);
14620 dwarf2_read_section (objfile, &sections->str_offsets);
14621 if (sections->str.buffer == NULL)
14622 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14623 " in CU at offset 0x%lx [in module %s]"),
14624 (long) cu->header.offset.sect_off, dwo_name);
14625 if (sections->str_offsets.buffer == NULL)
14626 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14627 " in CU at offset 0x%lx [in module %s]"),
14628 (long) cu->header.offset.sect_off, dwo_name);
14629 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14630 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14631 " section in CU at offset 0x%lx [in module %s]"),
14632 (long) cu->header.offset.sect_off, dwo_name);
14633 info_ptr = (sections->str_offsets.buffer
14634 + str_index * cu->header.offset_size);
14635 if (cu->header.offset_size == 4)
14636 str_offset = bfd_get_32 (abfd, info_ptr);
14637 else
14638 str_offset = bfd_get_64 (abfd, info_ptr);
14639 if (str_offset >= sections->str.size)
14640 error (_("Offset from DW_FORM_str_index pointing outside of"
14641 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14642 (long) cu->header.offset.sect_off, dwo_name);
14643 return (char *) (sections->str.buffer + str_offset);
14644}
14645
3019eac3
DE
14646/* Return the length of an LEB128 number in BUF. */
14647
14648static int
14649leb128_size (const gdb_byte *buf)
14650{
14651 const gdb_byte *begin = buf;
14652 gdb_byte byte;
14653
14654 while (1)
14655 {
14656 byte = *buf++;
14657 if ((byte & 128) == 0)
14658 return buf - begin;
14659 }
14660}
14661
c906108c 14662static void
e142c38c 14663set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14664{
14665 switch (lang)
14666 {
14667 case DW_LANG_C89:
76bee0cc 14668 case DW_LANG_C99:
c906108c 14669 case DW_LANG_C:
e142c38c 14670 cu->language = language_c;
c906108c
SS
14671 break;
14672 case DW_LANG_C_plus_plus:
e142c38c 14673 cu->language = language_cplus;
c906108c 14674 break;
6aecb9c2
JB
14675 case DW_LANG_D:
14676 cu->language = language_d;
14677 break;
c906108c
SS
14678 case DW_LANG_Fortran77:
14679 case DW_LANG_Fortran90:
b21b22e0 14680 case DW_LANG_Fortran95:
e142c38c 14681 cu->language = language_fortran;
c906108c 14682 break;
a766d390
DE
14683 case DW_LANG_Go:
14684 cu->language = language_go;
14685 break;
c906108c 14686 case DW_LANG_Mips_Assembler:
e142c38c 14687 cu->language = language_asm;
c906108c 14688 break;
bebd888e 14689 case DW_LANG_Java:
e142c38c 14690 cu->language = language_java;
bebd888e 14691 break;
c906108c 14692 case DW_LANG_Ada83:
8aaf0b47 14693 case DW_LANG_Ada95:
bc5f45f8
JB
14694 cu->language = language_ada;
14695 break;
72019c9c
GM
14696 case DW_LANG_Modula2:
14697 cu->language = language_m2;
14698 break;
fe8e67fd
PM
14699 case DW_LANG_Pascal83:
14700 cu->language = language_pascal;
14701 break;
22566fbd
DJ
14702 case DW_LANG_ObjC:
14703 cu->language = language_objc;
14704 break;
c906108c
SS
14705 case DW_LANG_Cobol74:
14706 case DW_LANG_Cobol85:
c906108c 14707 default:
e142c38c 14708 cu->language = language_minimal;
c906108c
SS
14709 break;
14710 }
e142c38c 14711 cu->language_defn = language_def (cu->language);
c906108c
SS
14712}
14713
14714/* Return the named attribute or NULL if not there. */
14715
14716static struct attribute *
e142c38c 14717dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14718{
a48e046c 14719 for (;;)
c906108c 14720 {
a48e046c
TT
14721 unsigned int i;
14722 struct attribute *spec = NULL;
14723
14724 for (i = 0; i < die->num_attrs; ++i)
14725 {
14726 if (die->attrs[i].name == name)
14727 return &die->attrs[i];
14728 if (die->attrs[i].name == DW_AT_specification
14729 || die->attrs[i].name == DW_AT_abstract_origin)
14730 spec = &die->attrs[i];
14731 }
14732
14733 if (!spec)
14734 break;
c906108c 14735
f2f0e013 14736 die = follow_die_ref (die, spec, &cu);
f2f0e013 14737 }
c5aa993b 14738
c906108c
SS
14739 return NULL;
14740}
14741
348e048f
DE
14742/* Return the named attribute or NULL if not there,
14743 but do not follow DW_AT_specification, etc.
14744 This is for use in contexts where we're reading .debug_types dies.
14745 Following DW_AT_specification, DW_AT_abstract_origin will take us
14746 back up the chain, and we want to go down. */
14747
14748static struct attribute *
45e58e77 14749dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14750{
14751 unsigned int i;
14752
14753 for (i = 0; i < die->num_attrs; ++i)
14754 if (die->attrs[i].name == name)
14755 return &die->attrs[i];
14756
14757 return NULL;
14758}
14759
05cf31d1
JB
14760/* Return non-zero iff the attribute NAME is defined for the given DIE,
14761 and holds a non-zero value. This function should only be used for
2dc7f7b3 14762 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14763
14764static int
14765dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14766{
14767 struct attribute *attr = dwarf2_attr (die, name, cu);
14768
14769 return (attr && DW_UNSND (attr));
14770}
14771
3ca72b44 14772static int
e142c38c 14773die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14774{
05cf31d1
JB
14775 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14776 which value is non-zero. However, we have to be careful with
14777 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14778 (via dwarf2_flag_true_p) follows this attribute. So we may
14779 end up accidently finding a declaration attribute that belongs
14780 to a different DIE referenced by the specification attribute,
14781 even though the given DIE does not have a declaration attribute. */
14782 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14783 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14784}
14785
63d06c5c 14786/* Return the die giving the specification for DIE, if there is
f2f0e013 14787 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14788 containing the return value on output. If there is no
14789 specification, but there is an abstract origin, that is
14790 returned. */
63d06c5c
DC
14791
14792static struct die_info *
f2f0e013 14793die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14794{
f2f0e013
DJ
14795 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14796 *spec_cu);
63d06c5c 14797
edb3359d
DJ
14798 if (spec_attr == NULL)
14799 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14800
63d06c5c
DC
14801 if (spec_attr == NULL)
14802 return NULL;
14803 else
f2f0e013 14804 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14805}
c906108c 14806
debd256d 14807/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14808 refers to.
14809 NOTE: This is also used as a "cleanup" function. */
14810
debd256d
JB
14811static void
14812free_line_header (struct line_header *lh)
14813{
14814 if (lh->standard_opcode_lengths)
a8bc7b56 14815 xfree (lh->standard_opcode_lengths);
debd256d
JB
14816
14817 /* Remember that all the lh->file_names[i].name pointers are
14818 pointers into debug_line_buffer, and don't need to be freed. */
14819 if (lh->file_names)
a8bc7b56 14820 xfree (lh->file_names);
debd256d
JB
14821
14822 /* Similarly for the include directory names. */
14823 if (lh->include_dirs)
a8bc7b56 14824 xfree (lh->include_dirs);
debd256d 14825
a8bc7b56 14826 xfree (lh);
debd256d
JB
14827}
14828
debd256d 14829/* Add an entry to LH's include directory table. */
ae2de4f8 14830
debd256d
JB
14831static void
14832add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14833{
debd256d
JB
14834 /* Grow the array if necessary. */
14835 if (lh->include_dirs_size == 0)
c5aa993b 14836 {
debd256d
JB
14837 lh->include_dirs_size = 1; /* for testing */
14838 lh->include_dirs = xmalloc (lh->include_dirs_size
14839 * sizeof (*lh->include_dirs));
14840 }
14841 else if (lh->num_include_dirs >= lh->include_dirs_size)
14842 {
14843 lh->include_dirs_size *= 2;
14844 lh->include_dirs = xrealloc (lh->include_dirs,
14845 (lh->include_dirs_size
14846 * sizeof (*lh->include_dirs)));
c5aa993b 14847 }
c906108c 14848
debd256d
JB
14849 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14850}
6e70227d 14851
debd256d 14852/* Add an entry to LH's file name table. */
ae2de4f8 14853
debd256d
JB
14854static void
14855add_file_name (struct line_header *lh,
14856 char *name,
14857 unsigned int dir_index,
14858 unsigned int mod_time,
14859 unsigned int length)
14860{
14861 struct file_entry *fe;
14862
14863 /* Grow the array if necessary. */
14864 if (lh->file_names_size == 0)
14865 {
14866 lh->file_names_size = 1; /* for testing */
14867 lh->file_names = xmalloc (lh->file_names_size
14868 * sizeof (*lh->file_names));
14869 }
14870 else if (lh->num_file_names >= lh->file_names_size)
14871 {
14872 lh->file_names_size *= 2;
14873 lh->file_names = xrealloc (lh->file_names,
14874 (lh->file_names_size
14875 * sizeof (*lh->file_names)));
14876 }
14877
14878 fe = &lh->file_names[lh->num_file_names++];
14879 fe->name = name;
14880 fe->dir_index = dir_index;
14881 fe->mod_time = mod_time;
14882 fe->length = length;
aaa75496 14883 fe->included_p = 0;
cb1df416 14884 fe->symtab = NULL;
debd256d 14885}
6e70227d 14886
36586728
TT
14887/* A convenience function to find the proper .debug_line section for a
14888 CU. */
14889
14890static struct dwarf2_section_info *
14891get_debug_line_section (struct dwarf2_cu *cu)
14892{
14893 struct dwarf2_section_info *section;
14894
14895 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14896 DWO file. */
14897 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14898 section = &cu->dwo_unit->dwo_file->sections.line;
14899 else if (cu->per_cu->is_dwz)
14900 {
14901 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14902
14903 section = &dwz->line;
14904 }
14905 else
14906 section = &dwarf2_per_objfile->line;
14907
14908 return section;
14909}
14910
debd256d 14911/* Read the statement program header starting at OFFSET in
3019eac3 14912 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14913 to a struct line_header, allocated using xmalloc.
debd256d
JB
14914
14915 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14916 the returned object point into the dwarf line section buffer,
14917 and must not be freed. */
ae2de4f8 14918
debd256d 14919static struct line_header *
3019eac3 14920dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14921{
14922 struct cleanup *back_to;
14923 struct line_header *lh;
fe1b8b76 14924 gdb_byte *line_ptr;
c764a876 14925 unsigned int bytes_read, offset_size;
debd256d
JB
14926 int i;
14927 char *cur_dir, *cur_file;
3019eac3
DE
14928 struct dwarf2_section_info *section;
14929 bfd *abfd;
14930
36586728 14931 section = get_debug_line_section (cu);
3019eac3
DE
14932 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14933 if (section->buffer == NULL)
debd256d 14934 {
3019eac3
DE
14935 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14936 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14937 else
14938 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14939 return 0;
14940 }
14941
fceca515
DE
14942 /* We can't do this until we know the section is non-empty.
14943 Only then do we know we have such a section. */
14944 abfd = section->asection->owner;
14945
a738430d
MK
14946 /* Make sure that at least there's room for the total_length field.
14947 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14948 if (offset + 4 >= section->size)
debd256d 14949 {
4d3c2250 14950 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14951 return 0;
14952 }
14953
14954 lh = xmalloc (sizeof (*lh));
14955 memset (lh, 0, sizeof (*lh));
14956 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14957 (void *) lh);
14958
3019eac3 14959 line_ptr = section->buffer + offset;
debd256d 14960
a738430d 14961 /* Read in the header. */
6e70227d 14962 lh->total_length =
c764a876
DE
14963 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14964 &bytes_read, &offset_size);
debd256d 14965 line_ptr += bytes_read;
3019eac3 14966 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14967 {
4d3c2250 14968 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14969 return 0;
14970 }
14971 lh->statement_program_end = line_ptr + lh->total_length;
14972 lh->version = read_2_bytes (abfd, line_ptr);
14973 line_ptr += 2;
c764a876
DE
14974 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14975 line_ptr += offset_size;
debd256d
JB
14976 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14977 line_ptr += 1;
2dc7f7b3
TT
14978 if (lh->version >= 4)
14979 {
14980 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14981 line_ptr += 1;
14982 }
14983 else
14984 lh->maximum_ops_per_instruction = 1;
14985
14986 if (lh->maximum_ops_per_instruction == 0)
14987 {
14988 lh->maximum_ops_per_instruction = 1;
14989 complaint (&symfile_complaints,
3e43a32a
MS
14990 _("invalid maximum_ops_per_instruction "
14991 "in `.debug_line' section"));
2dc7f7b3
TT
14992 }
14993
debd256d
JB
14994 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14995 line_ptr += 1;
14996 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14997 line_ptr += 1;
14998 lh->line_range = read_1_byte (abfd, line_ptr);
14999 line_ptr += 1;
15000 lh->opcode_base = read_1_byte (abfd, line_ptr);
15001 line_ptr += 1;
15002 lh->standard_opcode_lengths
fe1b8b76 15003 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15004
15005 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15006 for (i = 1; i < lh->opcode_base; ++i)
15007 {
15008 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15009 line_ptr += 1;
15010 }
15011
a738430d 15012 /* Read directory table. */
9b1c24c8 15013 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15014 {
15015 line_ptr += bytes_read;
15016 add_include_dir (lh, cur_dir);
15017 }
15018 line_ptr += bytes_read;
15019
a738430d 15020 /* Read file name table. */
9b1c24c8 15021 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15022 {
15023 unsigned int dir_index, mod_time, length;
15024
15025 line_ptr += bytes_read;
15026 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15027 line_ptr += bytes_read;
15028 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15029 line_ptr += bytes_read;
15030 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15031 line_ptr += bytes_read;
15032
15033 add_file_name (lh, cur_file, dir_index, mod_time, length);
15034 }
15035 line_ptr += bytes_read;
6e70227d 15036 lh->statement_program_start = line_ptr;
debd256d 15037
3019eac3 15038 if (line_ptr > (section->buffer + section->size))
4d3c2250 15039 complaint (&symfile_complaints,
3e43a32a
MS
15040 _("line number info header doesn't "
15041 "fit in `.debug_line' section"));
debd256d
JB
15042
15043 discard_cleanups (back_to);
15044 return lh;
15045}
c906108c 15046
c6da4cef
DE
15047/* Subroutine of dwarf_decode_lines to simplify it.
15048 Return the file name of the psymtab for included file FILE_INDEX
15049 in line header LH of PST.
15050 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15051 If space for the result is malloc'd, it will be freed by a cleanup.
15052 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15053
15054static char *
15055psymtab_include_file_name (const struct line_header *lh, int file_index,
15056 const struct partial_symtab *pst,
15057 const char *comp_dir)
15058{
15059 const struct file_entry fe = lh->file_names [file_index];
15060 char *include_name = fe.name;
15061 char *include_name_to_compare = include_name;
15062 char *dir_name = NULL;
72b9f47f
TT
15063 const char *pst_filename;
15064 char *copied_name = NULL;
c6da4cef
DE
15065 int file_is_pst;
15066
15067 if (fe.dir_index)
15068 dir_name = lh->include_dirs[fe.dir_index - 1];
15069
15070 if (!IS_ABSOLUTE_PATH (include_name)
15071 && (dir_name != NULL || comp_dir != NULL))
15072 {
15073 /* Avoid creating a duplicate psymtab for PST.
15074 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15075 Before we do the comparison, however, we need to account
15076 for DIR_NAME and COMP_DIR.
15077 First prepend dir_name (if non-NULL). If we still don't
15078 have an absolute path prepend comp_dir (if non-NULL).
15079 However, the directory we record in the include-file's
15080 psymtab does not contain COMP_DIR (to match the
15081 corresponding symtab(s)).
15082
15083 Example:
15084
15085 bash$ cd /tmp
15086 bash$ gcc -g ./hello.c
15087 include_name = "hello.c"
15088 dir_name = "."
15089 DW_AT_comp_dir = comp_dir = "/tmp"
15090 DW_AT_name = "./hello.c" */
15091
15092 if (dir_name != NULL)
15093 {
15094 include_name = concat (dir_name, SLASH_STRING,
15095 include_name, (char *)NULL);
15096 include_name_to_compare = include_name;
15097 make_cleanup (xfree, include_name);
15098 }
15099 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15100 {
15101 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15102 include_name, (char *)NULL);
15103 }
15104 }
15105
15106 pst_filename = pst->filename;
15107 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15108 {
72b9f47f
TT
15109 copied_name = concat (pst->dirname, SLASH_STRING,
15110 pst_filename, (char *)NULL);
15111 pst_filename = copied_name;
c6da4cef
DE
15112 }
15113
1e3fad37 15114 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15115
15116 if (include_name_to_compare != include_name)
15117 xfree (include_name_to_compare);
72b9f47f
TT
15118 if (copied_name != NULL)
15119 xfree (copied_name);
c6da4cef
DE
15120
15121 if (file_is_pst)
15122 return NULL;
15123 return include_name;
15124}
15125
c91513d8
PP
15126/* Ignore this record_line request. */
15127
15128static void
15129noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15130{
15131 return;
15132}
15133
f3f5162e
DE
15134/* Subroutine of dwarf_decode_lines to simplify it.
15135 Process the line number information in LH. */
debd256d 15136
c906108c 15137static void
f3f5162e
DE
15138dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15139 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15140{
a8c50c1f 15141 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15142 gdb_byte *line_end;
a8c50c1f 15143 unsigned int bytes_read, extended_len;
c906108c 15144 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15145 CORE_ADDR baseaddr;
15146 struct objfile *objfile = cu->objfile;
f3f5162e 15147 bfd *abfd = objfile->obfd;
fbf65064 15148 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15149 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15150 struct subfile *last_subfile = NULL;
c91513d8
PP
15151 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15152 = record_line;
e142c38c
DJ
15153
15154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15155
debd256d
JB
15156 line_ptr = lh->statement_program_start;
15157 line_end = lh->statement_program_end;
c906108c
SS
15158
15159 /* Read the statement sequences until there's nothing left. */
15160 while (line_ptr < line_end)
15161 {
15162 /* state machine registers */
15163 CORE_ADDR address = 0;
15164 unsigned int file = 1;
15165 unsigned int line = 1;
15166 unsigned int column = 0;
debd256d 15167 int is_stmt = lh->default_is_stmt;
c906108c
SS
15168 int basic_block = 0;
15169 int end_sequence = 0;
fbf65064 15170 CORE_ADDR addr;
2dc7f7b3 15171 unsigned char op_index = 0;
c906108c 15172
aaa75496 15173 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15174 {
aaa75496 15175 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15176 /* lh->include_dirs and lh->file_names are 0-based, but the
15177 directory and file name numbers in the statement program
15178 are 1-based. */
15179 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15180 char *dir = NULL;
a738430d 15181
debd256d
JB
15182 if (fe->dir_index)
15183 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15184
15185 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15186 }
15187
a738430d 15188 /* Decode the table. */
c5aa993b 15189 while (!end_sequence)
c906108c
SS
15190 {
15191 op_code = read_1_byte (abfd, line_ptr);
15192 line_ptr += 1;
59205f5a
JB
15193 if (line_ptr > line_end)
15194 {
15195 dwarf2_debug_line_missing_end_sequence_complaint ();
15196 break;
15197 }
9aa1fe7e 15198
debd256d 15199 if (op_code >= lh->opcode_base)
6e70227d 15200 {
a738430d 15201 /* Special operand. */
debd256d 15202 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15203 address += (((op_index + (adj_opcode / lh->line_range))
15204 / lh->maximum_ops_per_instruction)
15205 * lh->minimum_instruction_length);
15206 op_index = ((op_index + (adj_opcode / lh->line_range))
15207 % lh->maximum_ops_per_instruction);
debd256d 15208 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15209 if (lh->num_file_names < file || file == 0)
25e43795 15210 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15211 /* For now we ignore lines not starting on an
15212 instruction boundary. */
15213 else if (op_index == 0)
25e43795
DJ
15214 {
15215 lh->file_names[file - 1].included_p = 1;
ca5f395d 15216 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15217 {
15218 if (last_subfile != current_subfile)
15219 {
15220 addr = gdbarch_addr_bits_remove (gdbarch, address);
15221 if (last_subfile)
c91513d8 15222 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15223 last_subfile = current_subfile;
15224 }
25e43795 15225 /* Append row to matrix using current values. */
7019d805 15226 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15227 (*p_record_line) (current_subfile, line, addr);
366da635 15228 }
25e43795 15229 }
ca5f395d 15230 basic_block = 0;
9aa1fe7e
GK
15231 }
15232 else switch (op_code)
c906108c
SS
15233 {
15234 case DW_LNS_extended_op:
3e43a32a
MS
15235 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15236 &bytes_read);
473b7be6 15237 line_ptr += bytes_read;
a8c50c1f 15238 extended_end = line_ptr + extended_len;
c906108c
SS
15239 extended_op = read_1_byte (abfd, line_ptr);
15240 line_ptr += 1;
15241 switch (extended_op)
15242 {
15243 case DW_LNE_end_sequence:
c91513d8 15244 p_record_line = record_line;
c906108c 15245 end_sequence = 1;
c906108c
SS
15246 break;
15247 case DW_LNE_set_address:
e7c27a73 15248 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15249
15250 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15251 {
15252 /* This line table is for a function which has been
15253 GCd by the linker. Ignore it. PR gdb/12528 */
15254
15255 long line_offset
36586728 15256 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15257
15258 complaint (&symfile_complaints,
15259 _(".debug_line address at offset 0x%lx is 0 "
15260 "[in module %s]"),
bb5ed363 15261 line_offset, objfile->name);
c91513d8
PP
15262 p_record_line = noop_record_line;
15263 }
15264
2dc7f7b3 15265 op_index = 0;
107d2387
AC
15266 line_ptr += bytes_read;
15267 address += baseaddr;
c906108c
SS
15268 break;
15269 case DW_LNE_define_file:
debd256d
JB
15270 {
15271 char *cur_file;
15272 unsigned int dir_index, mod_time, length;
6e70227d 15273
3e43a32a
MS
15274 cur_file = read_direct_string (abfd, line_ptr,
15275 &bytes_read);
debd256d
JB
15276 line_ptr += bytes_read;
15277 dir_index =
15278 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15279 line_ptr += bytes_read;
15280 mod_time =
15281 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15282 line_ptr += bytes_read;
15283 length =
15284 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15285 line_ptr += bytes_read;
15286 add_file_name (lh, cur_file, dir_index, mod_time, length);
15287 }
c906108c 15288 break;
d0c6ba3d
CC
15289 case DW_LNE_set_discriminator:
15290 /* The discriminator is not interesting to the debugger;
15291 just ignore it. */
15292 line_ptr = extended_end;
15293 break;
c906108c 15294 default:
4d3c2250 15295 complaint (&symfile_complaints,
e2e0b3e5 15296 _("mangled .debug_line section"));
debd256d 15297 return;
c906108c 15298 }
a8c50c1f
DJ
15299 /* Make sure that we parsed the extended op correctly. If e.g.
15300 we expected a different address size than the producer used,
15301 we may have read the wrong number of bytes. */
15302 if (line_ptr != extended_end)
15303 {
15304 complaint (&symfile_complaints,
15305 _("mangled .debug_line section"));
15306 return;
15307 }
c906108c
SS
15308 break;
15309 case DW_LNS_copy:
59205f5a 15310 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15311 dwarf2_debug_line_missing_file_complaint ();
15312 else
366da635 15313 {
25e43795 15314 lh->file_names[file - 1].included_p = 1;
ca5f395d 15315 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15316 {
15317 if (last_subfile != current_subfile)
15318 {
15319 addr = gdbarch_addr_bits_remove (gdbarch, address);
15320 if (last_subfile)
c91513d8 15321 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15322 last_subfile = current_subfile;
15323 }
7019d805 15324 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15325 (*p_record_line) (current_subfile, line, addr);
fbf65064 15326 }
366da635 15327 }
c906108c
SS
15328 basic_block = 0;
15329 break;
15330 case DW_LNS_advance_pc:
2dc7f7b3
TT
15331 {
15332 CORE_ADDR adjust
15333 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15334
15335 address += (((op_index + adjust)
15336 / lh->maximum_ops_per_instruction)
15337 * lh->minimum_instruction_length);
15338 op_index = ((op_index + adjust)
15339 % lh->maximum_ops_per_instruction);
15340 line_ptr += bytes_read;
15341 }
c906108c
SS
15342 break;
15343 case DW_LNS_advance_line:
15344 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15345 line_ptr += bytes_read;
15346 break;
15347 case DW_LNS_set_file:
debd256d 15348 {
a738430d
MK
15349 /* The arrays lh->include_dirs and lh->file_names are
15350 0-based, but the directory and file name numbers in
15351 the statement program are 1-based. */
debd256d 15352 struct file_entry *fe;
4f1520fb 15353 char *dir = NULL;
a738430d 15354
debd256d
JB
15355 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15356 line_ptr += bytes_read;
59205f5a 15357 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15358 dwarf2_debug_line_missing_file_complaint ();
15359 else
15360 {
15361 fe = &lh->file_names[file - 1];
15362 if (fe->dir_index)
15363 dir = lh->include_dirs[fe->dir_index - 1];
15364 if (!decode_for_pst_p)
15365 {
15366 last_subfile = current_subfile;
15367 dwarf2_start_subfile (fe->name, dir, comp_dir);
15368 }
15369 }
debd256d 15370 }
c906108c
SS
15371 break;
15372 case DW_LNS_set_column:
15373 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15374 line_ptr += bytes_read;
15375 break;
15376 case DW_LNS_negate_stmt:
15377 is_stmt = (!is_stmt);
15378 break;
15379 case DW_LNS_set_basic_block:
15380 basic_block = 1;
15381 break;
c2c6d25f
JM
15382 /* Add to the address register of the state machine the
15383 address increment value corresponding to special opcode
a738430d
MK
15384 255. I.e., this value is scaled by the minimum
15385 instruction length since special opcode 255 would have
b021a221 15386 scaled the increment. */
c906108c 15387 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15388 {
15389 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15390
15391 address += (((op_index + adjust)
15392 / lh->maximum_ops_per_instruction)
15393 * lh->minimum_instruction_length);
15394 op_index = ((op_index + adjust)
15395 % lh->maximum_ops_per_instruction);
15396 }
c906108c
SS
15397 break;
15398 case DW_LNS_fixed_advance_pc:
15399 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15400 op_index = 0;
c906108c
SS
15401 line_ptr += 2;
15402 break;
9aa1fe7e 15403 default:
a738430d
MK
15404 {
15405 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15406 int i;
a738430d 15407
debd256d 15408 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15409 {
15410 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15411 line_ptr += bytes_read;
15412 }
15413 }
c906108c
SS
15414 }
15415 }
59205f5a
JB
15416 if (lh->num_file_names < file || file == 0)
15417 dwarf2_debug_line_missing_file_complaint ();
15418 else
15419 {
15420 lh->file_names[file - 1].included_p = 1;
15421 if (!decode_for_pst_p)
fbf65064
UW
15422 {
15423 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15424 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15425 }
59205f5a 15426 }
c906108c 15427 }
f3f5162e
DE
15428}
15429
15430/* Decode the Line Number Program (LNP) for the given line_header
15431 structure and CU. The actual information extracted and the type
15432 of structures created from the LNP depends on the value of PST.
15433
15434 1. If PST is NULL, then this procedure uses the data from the program
15435 to create all necessary symbol tables, and their linetables.
15436
15437 2. If PST is not NULL, this procedure reads the program to determine
15438 the list of files included by the unit represented by PST, and
15439 builds all the associated partial symbol tables.
15440
15441 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15442 It is used for relative paths in the line table.
15443 NOTE: When processing partial symtabs (pst != NULL),
15444 comp_dir == pst->dirname.
15445
15446 NOTE: It is important that psymtabs have the same file name (via strcmp)
15447 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15448 symtab we don't use it in the name of the psymtabs we create.
15449 E.g. expand_line_sal requires this when finding psymtabs to expand.
15450 A good testcase for this is mb-inline.exp. */
15451
15452static void
15453dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15454 struct dwarf2_cu *cu, struct partial_symtab *pst,
15455 int want_line_info)
15456{
15457 struct objfile *objfile = cu->objfile;
15458 const int decode_for_pst_p = (pst != NULL);
15459 struct subfile *first_subfile = current_subfile;
15460
15461 if (want_line_info)
15462 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15463
15464 if (decode_for_pst_p)
15465 {
15466 int file_index;
15467
15468 /* Now that we're done scanning the Line Header Program, we can
15469 create the psymtab of each included file. */
15470 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15471 if (lh->file_names[file_index].included_p == 1)
15472 {
c6da4cef
DE
15473 char *include_name =
15474 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15475 if (include_name != NULL)
aaa75496
JB
15476 dwarf2_create_include_psymtab (include_name, pst, objfile);
15477 }
15478 }
cb1df416
DJ
15479 else
15480 {
15481 /* Make sure a symtab is created for every file, even files
15482 which contain only variables (i.e. no code with associated
15483 line numbers). */
cb1df416 15484 int i;
cb1df416
DJ
15485
15486 for (i = 0; i < lh->num_file_names; i++)
15487 {
15488 char *dir = NULL;
f3f5162e 15489 struct file_entry *fe;
9a619af0 15490
cb1df416
DJ
15491 fe = &lh->file_names[i];
15492 if (fe->dir_index)
15493 dir = lh->include_dirs[fe->dir_index - 1];
15494 dwarf2_start_subfile (fe->name, dir, comp_dir);
15495
15496 /* Skip the main file; we don't need it, and it must be
15497 allocated last, so that it will show up before the
15498 non-primary symtabs in the objfile's symtab list. */
15499 if (current_subfile == first_subfile)
15500 continue;
15501
15502 if (current_subfile->symtab == NULL)
15503 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15504 objfile);
cb1df416
DJ
15505 fe->symtab = current_subfile->symtab;
15506 }
15507 }
c906108c
SS
15508}
15509
15510/* Start a subfile for DWARF. FILENAME is the name of the file and
15511 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15512 or NULL if not known. COMP_DIR is the compilation directory for the
15513 linetable's compilation unit or NULL if not known.
c906108c
SS
15514 This routine tries to keep line numbers from identical absolute and
15515 relative file names in a common subfile.
15516
15517 Using the `list' example from the GDB testsuite, which resides in
15518 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15519 of /srcdir/list0.c yields the following debugging information for list0.c:
15520
c5aa993b
JM
15521 DW_AT_name: /srcdir/list0.c
15522 DW_AT_comp_dir: /compdir
357e46e7 15523 files.files[0].name: list0.h
c5aa993b 15524 files.files[0].dir: /srcdir
357e46e7 15525 files.files[1].name: list0.c
c5aa993b 15526 files.files[1].dir: /srcdir
c906108c
SS
15527
15528 The line number information for list0.c has to end up in a single
4f1520fb
FR
15529 subfile, so that `break /srcdir/list0.c:1' works as expected.
15530 start_subfile will ensure that this happens provided that we pass the
15531 concatenation of files.files[1].dir and files.files[1].name as the
15532 subfile's name. */
c906108c
SS
15533
15534static void
3e43a32a
MS
15535dwarf2_start_subfile (char *filename, const char *dirname,
15536 const char *comp_dir)
c906108c 15537{
4f1520fb
FR
15538 char *fullname;
15539
15540 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15541 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15542 second argument to start_subfile. To be consistent, we do the
15543 same here. In order not to lose the line information directory,
15544 we concatenate it to the filename when it makes sense.
15545 Note that the Dwarf3 standard says (speaking of filenames in line
15546 information): ``The directory index is ignored for file names
15547 that represent full path names''. Thus ignoring dirname in the
15548 `else' branch below isn't an issue. */
c906108c 15549
d5166ae1 15550 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15551 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15552 else
15553 fullname = filename;
c906108c 15554
4f1520fb
FR
15555 start_subfile (fullname, comp_dir);
15556
15557 if (fullname != filename)
15558 xfree (fullname);
c906108c
SS
15559}
15560
f4dc4d17
DE
15561/* Start a symtab for DWARF.
15562 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15563
15564static void
15565dwarf2_start_symtab (struct dwarf2_cu *cu,
15566 char *name, char *comp_dir, CORE_ADDR low_pc)
15567{
15568 start_symtab (name, comp_dir, low_pc);
15569 record_debugformat ("DWARF 2");
15570 record_producer (cu->producer);
15571
15572 /* We assume that we're processing GCC output. */
15573 processing_gcc_compilation = 2;
15574
15575 processing_has_namespace_info = 0;
15576}
15577
4c2df51b
DJ
15578static void
15579var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15580 struct dwarf2_cu *cu)
4c2df51b 15581{
e7c27a73
DJ
15582 struct objfile *objfile = cu->objfile;
15583 struct comp_unit_head *cu_header = &cu->header;
15584
4c2df51b
DJ
15585 /* NOTE drow/2003-01-30: There used to be a comment and some special
15586 code here to turn a symbol with DW_AT_external and a
15587 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15588 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15589 with some versions of binutils) where shared libraries could have
15590 relocations against symbols in their debug information - the
15591 minimal symbol would have the right address, but the debug info
15592 would not. It's no longer necessary, because we will explicitly
15593 apply relocations when we read in the debug information now. */
15594
15595 /* A DW_AT_location attribute with no contents indicates that a
15596 variable has been optimized away. */
15597 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15598 {
15599 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15600 return;
15601 }
15602
15603 /* Handle one degenerate form of location expression specially, to
15604 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15605 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15606 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15607
15608 if (attr_form_is_block (attr)
3019eac3
DE
15609 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15610 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15611 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15612 && (DW_BLOCK (attr)->size
15613 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15614 {
891d2f0b 15615 unsigned int dummy;
4c2df51b 15616
3019eac3
DE
15617 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15618 SYMBOL_VALUE_ADDRESS (sym) =
15619 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15620 else
15621 SYMBOL_VALUE_ADDRESS (sym) =
15622 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15623 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15624 fixup_symbol_section (sym, objfile);
15625 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15626 SYMBOL_SECTION (sym));
4c2df51b
DJ
15627 return;
15628 }
15629
15630 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15631 expression evaluator, and use LOC_COMPUTED only when necessary
15632 (i.e. when the value of a register or memory location is
15633 referenced, or a thread-local block, etc.). Then again, it might
15634 not be worthwhile. I'm assuming that it isn't unless performance
15635 or memory numbers show me otherwise. */
15636
e7c27a73 15637 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15638 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15639
15640 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15641 cu->has_loclist = 1;
4c2df51b
DJ
15642}
15643
c906108c
SS
15644/* Given a pointer to a DWARF information entry, figure out if we need
15645 to make a symbol table entry for it, and if so, create a new entry
15646 and return a pointer to it.
15647 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15648 used the passed type.
15649 If SPACE is not NULL, use it to hold the new symbol. If it is
15650 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15651
15652static struct symbol *
34eaf542
TT
15653new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15654 struct symbol *space)
c906108c 15655{
e7c27a73 15656 struct objfile *objfile = cu->objfile;
c906108c
SS
15657 struct symbol *sym = NULL;
15658 char *name;
15659 struct attribute *attr = NULL;
15660 struct attribute *attr2 = NULL;
e142c38c 15661 CORE_ADDR baseaddr;
e37fd15a
SW
15662 struct pending **list_to_add = NULL;
15663
edb3359d 15664 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15665
15666 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15667
94af9270 15668 name = dwarf2_name (die, cu);
c906108c
SS
15669 if (name)
15670 {
94af9270 15671 const char *linkagename;
34eaf542 15672 int suppress_add = 0;
94af9270 15673
34eaf542
TT
15674 if (space)
15675 sym = space;
15676 else
15677 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15678 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15679
15680 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15681 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15682 linkagename = dwarf2_physname (name, die, cu);
15683 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15684
f55ee35c
JK
15685 /* Fortran does not have mangling standard and the mangling does differ
15686 between gfortran, iFort etc. */
15687 if (cu->language == language_fortran
b250c185 15688 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
15689 symbol_set_demangled_name (&(sym->ginfo),
15690 (char *) dwarf2_full_name (name, die, cu),
15691 NULL);
f55ee35c 15692
c906108c 15693 /* Default assumptions.
c5aa993b 15694 Use the passed type or decode it from the die. */
176620f1 15695 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15696 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15697 if (type != NULL)
15698 SYMBOL_TYPE (sym) = type;
15699 else
e7c27a73 15700 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15701 attr = dwarf2_attr (die,
15702 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15703 cu);
c906108c
SS
15704 if (attr)
15705 {
15706 SYMBOL_LINE (sym) = DW_UNSND (attr);
15707 }
cb1df416 15708
edb3359d
DJ
15709 attr = dwarf2_attr (die,
15710 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15711 cu);
cb1df416
DJ
15712 if (attr)
15713 {
15714 int file_index = DW_UNSND (attr);
9a619af0 15715
cb1df416
DJ
15716 if (cu->line_header == NULL
15717 || file_index > cu->line_header->num_file_names)
15718 complaint (&symfile_complaints,
15719 _("file index out of range"));
1c3d648d 15720 else if (file_index > 0)
cb1df416
DJ
15721 {
15722 struct file_entry *fe;
9a619af0 15723
cb1df416
DJ
15724 fe = &cu->line_header->file_names[file_index - 1];
15725 SYMBOL_SYMTAB (sym) = fe->symtab;
15726 }
15727 }
15728
c906108c
SS
15729 switch (die->tag)
15730 {
15731 case DW_TAG_label:
e142c38c 15732 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15733 if (attr)
15734 {
15735 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15736 }
0f5238ed
TT
15737 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15738 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15739 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15740 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15741 break;
15742 case DW_TAG_subprogram:
15743 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15744 finish_block. */
15745 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15746 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15747 if ((attr2 && (DW_UNSND (attr2) != 0))
15748 || cu->language == language_ada)
c906108c 15749 {
2cfa0c8d
JB
15750 /* Subprograms marked external are stored as a global symbol.
15751 Ada subprograms, whether marked external or not, are always
15752 stored as a global symbol, because we want to be able to
15753 access them globally. For instance, we want to be able
15754 to break on a nested subprogram without having to
15755 specify the context. */
e37fd15a 15756 list_to_add = &global_symbols;
c906108c
SS
15757 }
15758 else
15759 {
e37fd15a 15760 list_to_add = cu->list_in_scope;
c906108c
SS
15761 }
15762 break;
edb3359d
DJ
15763 case DW_TAG_inlined_subroutine:
15764 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15765 finish_block. */
15766 SYMBOL_CLASS (sym) = LOC_BLOCK;
15767 SYMBOL_INLINED (sym) = 1;
481860b3 15768 list_to_add = cu->list_in_scope;
edb3359d 15769 break;
34eaf542
TT
15770 case DW_TAG_template_value_param:
15771 suppress_add = 1;
15772 /* Fall through. */
72929c62 15773 case DW_TAG_constant:
c906108c 15774 case DW_TAG_variable:
254e6b9e 15775 case DW_TAG_member:
0963b4bd
MS
15776 /* Compilation with minimal debug info may result in
15777 variables with missing type entries. Change the
15778 misleading `void' type to something sensible. */
c906108c 15779 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15780 SYMBOL_TYPE (sym)
46bf5051 15781 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15782
e142c38c 15783 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15784 /* In the case of DW_TAG_member, we should only be called for
15785 static const members. */
15786 if (die->tag == DW_TAG_member)
15787 {
3863f96c
DE
15788 /* dwarf2_add_field uses die_is_declaration,
15789 so we do the same. */
254e6b9e
DE
15790 gdb_assert (die_is_declaration (die, cu));
15791 gdb_assert (attr);
15792 }
c906108c
SS
15793 if (attr)
15794 {
e7c27a73 15795 dwarf2_const_value (attr, sym, cu);
e142c38c 15796 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15797 if (!suppress_add)
34eaf542
TT
15798 {
15799 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15800 list_to_add = &global_symbols;
34eaf542 15801 else
e37fd15a 15802 list_to_add = cu->list_in_scope;
34eaf542 15803 }
c906108c
SS
15804 break;
15805 }
e142c38c 15806 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15807 if (attr)
15808 {
e7c27a73 15809 var_decode_location (attr, sym, cu);
e142c38c 15810 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15811
15812 /* Fortran explicitly imports any global symbols to the local
15813 scope by DW_TAG_common_block. */
15814 if (cu->language == language_fortran && die->parent
15815 && die->parent->tag == DW_TAG_common_block)
15816 attr2 = NULL;
15817
caac4577
JG
15818 if (SYMBOL_CLASS (sym) == LOC_STATIC
15819 && SYMBOL_VALUE_ADDRESS (sym) == 0
15820 && !dwarf2_per_objfile->has_section_at_zero)
15821 {
15822 /* When a static variable is eliminated by the linker,
15823 the corresponding debug information is not stripped
15824 out, but the variable address is set to null;
15825 do not add such variables into symbol table. */
15826 }
15827 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15828 {
f55ee35c
JK
15829 /* Workaround gfortran PR debug/40040 - it uses
15830 DW_AT_location for variables in -fPIC libraries which may
15831 get overriden by other libraries/executable and get
15832 a different address. Resolve it by the minimal symbol
15833 which may come from inferior's executable using copy
15834 relocation. Make this workaround only for gfortran as for
15835 other compilers GDB cannot guess the minimal symbol
15836 Fortran mangling kind. */
15837 if (cu->language == language_fortran && die->parent
15838 && die->parent->tag == DW_TAG_module
15839 && cu->producer
15840 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15841 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15842
1c809c68
TT
15843 /* A variable with DW_AT_external is never static,
15844 but it may be block-scoped. */
15845 list_to_add = (cu->list_in_scope == &file_symbols
15846 ? &global_symbols : cu->list_in_scope);
1c809c68 15847 }
c906108c 15848 else
e37fd15a 15849 list_to_add = cu->list_in_scope;
c906108c
SS
15850 }
15851 else
15852 {
15853 /* We do not know the address of this symbol.
c5aa993b
JM
15854 If it is an external symbol and we have type information
15855 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15856 The address of the variable will then be determined from
15857 the minimal symbol table whenever the variable is
15858 referenced. */
e142c38c 15859 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15860
15861 /* Fortran explicitly imports any global symbols to the local
15862 scope by DW_TAG_common_block. */
15863 if (cu->language == language_fortran && die->parent
15864 && die->parent->tag == DW_TAG_common_block)
15865 {
15866 /* SYMBOL_CLASS doesn't matter here because
15867 read_common_block is going to reset it. */
15868 if (!suppress_add)
15869 list_to_add = cu->list_in_scope;
15870 }
15871 else if (attr2 && (DW_UNSND (attr2) != 0)
15872 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15873 {
0fe7935b
DJ
15874 /* A variable with DW_AT_external is never static, but it
15875 may be block-scoped. */
15876 list_to_add = (cu->list_in_scope == &file_symbols
15877 ? &global_symbols : cu->list_in_scope);
15878
c906108c 15879 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15880 }
442ddf59
JK
15881 else if (!die_is_declaration (die, cu))
15882 {
15883 /* Use the default LOC_OPTIMIZED_OUT class. */
15884 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15885 if (!suppress_add)
15886 list_to_add = cu->list_in_scope;
442ddf59 15887 }
c906108c
SS
15888 }
15889 break;
15890 case DW_TAG_formal_parameter:
edb3359d
DJ
15891 /* If we are inside a function, mark this as an argument. If
15892 not, we might be looking at an argument to an inlined function
15893 when we do not have enough information to show inlined frames;
15894 pretend it's a local variable in that case so that the user can
15895 still see it. */
15896 if (context_stack_depth > 0
15897 && context_stack[context_stack_depth - 1].name != NULL)
15898 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15899 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15900 if (attr)
15901 {
e7c27a73 15902 var_decode_location (attr, sym, cu);
c906108c 15903 }
e142c38c 15904 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15905 if (attr)
15906 {
e7c27a73 15907 dwarf2_const_value (attr, sym, cu);
c906108c 15908 }
f346a30d 15909
e37fd15a 15910 list_to_add = cu->list_in_scope;
c906108c
SS
15911 break;
15912 case DW_TAG_unspecified_parameters:
15913 /* From varargs functions; gdb doesn't seem to have any
15914 interest in this information, so just ignore it for now.
15915 (FIXME?) */
15916 break;
34eaf542
TT
15917 case DW_TAG_template_type_param:
15918 suppress_add = 1;
15919 /* Fall through. */
c906108c 15920 case DW_TAG_class_type:
680b30c7 15921 case DW_TAG_interface_type:
c906108c
SS
15922 case DW_TAG_structure_type:
15923 case DW_TAG_union_type:
72019c9c 15924 case DW_TAG_set_type:
c906108c
SS
15925 case DW_TAG_enumeration_type:
15926 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15927 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15928
63d06c5c 15929 {
987504bb 15930 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15931 really ever be static objects: otherwise, if you try
15932 to, say, break of a class's method and you're in a file
15933 which doesn't mention that class, it won't work unless
15934 the check for all static symbols in lookup_symbol_aux
15935 saves you. See the OtherFileClass tests in
15936 gdb.c++/namespace.exp. */
15937
e37fd15a 15938 if (!suppress_add)
34eaf542 15939 {
34eaf542
TT
15940 list_to_add = (cu->list_in_scope == &file_symbols
15941 && (cu->language == language_cplus
15942 || cu->language == language_java)
15943 ? &global_symbols : cu->list_in_scope);
63d06c5c 15944
64382290
TT
15945 /* The semantics of C++ state that "struct foo {
15946 ... }" also defines a typedef for "foo". A Java
15947 class declaration also defines a typedef for the
15948 class. */
15949 if (cu->language == language_cplus
15950 || cu->language == language_java
15951 || cu->language == language_ada)
15952 {
15953 /* The symbol's name is already allocated along
15954 with this objfile, so we don't need to
15955 duplicate it for the type. */
15956 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15957 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15958 }
63d06c5c
DC
15959 }
15960 }
c906108c
SS
15961 break;
15962 case DW_TAG_typedef:
63d06c5c
DC
15963 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15964 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15965 list_to_add = cu->list_in_scope;
63d06c5c 15966 break;
c906108c 15967 case DW_TAG_base_type:
a02abb62 15968 case DW_TAG_subrange_type:
c906108c 15969 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15970 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15971 list_to_add = cu->list_in_scope;
c906108c
SS
15972 break;
15973 case DW_TAG_enumerator:
e142c38c 15974 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15975 if (attr)
15976 {
e7c27a73 15977 dwarf2_const_value (attr, sym, cu);
c906108c 15978 }
63d06c5c
DC
15979 {
15980 /* NOTE: carlton/2003-11-10: See comment above in the
15981 DW_TAG_class_type, etc. block. */
15982
e142c38c 15983 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15984 && (cu->language == language_cplus
15985 || cu->language == language_java)
e142c38c 15986 ? &global_symbols : cu->list_in_scope);
63d06c5c 15987 }
c906108c 15988 break;
5c4e30ca
DC
15989 case DW_TAG_namespace:
15990 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15991 list_to_add = &global_symbols;
5c4e30ca 15992 break;
4357ac6c
TT
15993 case DW_TAG_common_block:
15994 SYMBOL_CLASS (sym) = LOC_STATIC;
15995 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15996 add_symbol_to_list (sym, cu->list_in_scope);
15997 break;
c906108c
SS
15998 default:
15999 /* Not a tag we recognize. Hopefully we aren't processing
16000 trash data, but since we must specifically ignore things
16001 we don't recognize, there is nothing else we should do at
0963b4bd 16002 this point. */
e2e0b3e5 16003 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16004 dwarf_tag_name (die->tag));
c906108c
SS
16005 break;
16006 }
df8a16a1 16007
e37fd15a
SW
16008 if (suppress_add)
16009 {
16010 sym->hash_next = objfile->template_symbols;
16011 objfile->template_symbols = sym;
16012 list_to_add = NULL;
16013 }
16014
16015 if (list_to_add != NULL)
16016 add_symbol_to_list (sym, list_to_add);
16017
df8a16a1
DJ
16018 /* For the benefit of old versions of GCC, check for anonymous
16019 namespaces based on the demangled name. */
16020 if (!processing_has_namespace_info
94af9270 16021 && cu->language == language_cplus)
a10964d1 16022 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16023 }
16024 return (sym);
16025}
16026
34eaf542
TT
16027/* A wrapper for new_symbol_full that always allocates a new symbol. */
16028
16029static struct symbol *
16030new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16031{
16032 return new_symbol_full (die, type, cu, NULL);
16033}
16034
98bfdba5
PA
16035/* Given an attr with a DW_FORM_dataN value in host byte order,
16036 zero-extend it as appropriate for the symbol's type. The DWARF
16037 standard (v4) is not entirely clear about the meaning of using
16038 DW_FORM_dataN for a constant with a signed type, where the type is
16039 wider than the data. The conclusion of a discussion on the DWARF
16040 list was that this is unspecified. We choose to always zero-extend
16041 because that is the interpretation long in use by GCC. */
c906108c 16042
98bfdba5
PA
16043static gdb_byte *
16044dwarf2_const_value_data (struct attribute *attr, struct type *type,
16045 const char *name, struct obstack *obstack,
12df843f 16046 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16047{
e7c27a73 16048 struct objfile *objfile = cu->objfile;
e17a4113
UW
16049 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16050 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16051 LONGEST l = DW_UNSND (attr);
16052
16053 if (bits < sizeof (*value) * 8)
16054 {
16055 l &= ((LONGEST) 1 << bits) - 1;
16056 *value = l;
16057 }
16058 else if (bits == sizeof (*value) * 8)
16059 *value = l;
16060 else
16061 {
16062 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16063 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16064 return bytes;
16065 }
16066
16067 return NULL;
16068}
16069
16070/* Read a constant value from an attribute. Either set *VALUE, or if
16071 the value does not fit in *VALUE, set *BYTES - either already
16072 allocated on the objfile obstack, or newly allocated on OBSTACK,
16073 or, set *BATON, if we translated the constant to a location
16074 expression. */
16075
16076static void
16077dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16078 const char *name, struct obstack *obstack,
16079 struct dwarf2_cu *cu,
12df843f 16080 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16081 struct dwarf2_locexpr_baton **baton)
16082{
16083 struct objfile *objfile = cu->objfile;
16084 struct comp_unit_head *cu_header = &cu->header;
c906108c 16085 struct dwarf_block *blk;
98bfdba5
PA
16086 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16087 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16088
16089 *value = 0;
16090 *bytes = NULL;
16091 *baton = NULL;
c906108c
SS
16092
16093 switch (attr->form)
16094 {
16095 case DW_FORM_addr:
3019eac3 16096 case DW_FORM_GNU_addr_index:
ac56253d 16097 {
ac56253d
TT
16098 gdb_byte *data;
16099
98bfdba5
PA
16100 if (TYPE_LENGTH (type) != cu_header->addr_size)
16101 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16102 cu_header->addr_size,
98bfdba5 16103 TYPE_LENGTH (type));
ac56253d
TT
16104 /* Symbols of this form are reasonably rare, so we just
16105 piggyback on the existing location code rather than writing
16106 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16107 *baton = obstack_alloc (&objfile->objfile_obstack,
16108 sizeof (struct dwarf2_locexpr_baton));
16109 (*baton)->per_cu = cu->per_cu;
16110 gdb_assert ((*baton)->per_cu);
ac56253d 16111
98bfdba5
PA
16112 (*baton)->size = 2 + cu_header->addr_size;
16113 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16114 (*baton)->data = data;
ac56253d
TT
16115
16116 data[0] = DW_OP_addr;
16117 store_unsigned_integer (&data[1], cu_header->addr_size,
16118 byte_order, DW_ADDR (attr));
16119 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16120 }
c906108c 16121 break;
4ac36638 16122 case DW_FORM_string:
93b5768b 16123 case DW_FORM_strp:
3019eac3 16124 case DW_FORM_GNU_str_index:
36586728 16125 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16126 /* DW_STRING is already allocated on the objfile obstack, point
16127 directly to it. */
16128 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16129 break;
c906108c
SS
16130 case DW_FORM_block1:
16131 case DW_FORM_block2:
16132 case DW_FORM_block4:
16133 case DW_FORM_block:
2dc7f7b3 16134 case DW_FORM_exprloc:
c906108c 16135 blk = DW_BLOCK (attr);
98bfdba5
PA
16136 if (TYPE_LENGTH (type) != blk->size)
16137 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16138 TYPE_LENGTH (type));
16139 *bytes = blk->data;
c906108c 16140 break;
2df3850c
JM
16141
16142 /* The DW_AT_const_value attributes are supposed to carry the
16143 symbol's value "represented as it would be on the target
16144 architecture." By the time we get here, it's already been
16145 converted to host endianness, so we just need to sign- or
16146 zero-extend it as appropriate. */
16147 case DW_FORM_data1:
3e43a32a
MS
16148 *bytes = dwarf2_const_value_data (attr, type, name,
16149 obstack, cu, value, 8);
2df3850c 16150 break;
c906108c 16151 case DW_FORM_data2:
3e43a32a
MS
16152 *bytes = dwarf2_const_value_data (attr, type, name,
16153 obstack, cu, value, 16);
2df3850c 16154 break;
c906108c 16155 case DW_FORM_data4:
3e43a32a
MS
16156 *bytes = dwarf2_const_value_data (attr, type, name,
16157 obstack, cu, value, 32);
2df3850c 16158 break;
c906108c 16159 case DW_FORM_data8:
3e43a32a
MS
16160 *bytes = dwarf2_const_value_data (attr, type, name,
16161 obstack, cu, value, 64);
2df3850c
JM
16162 break;
16163
c906108c 16164 case DW_FORM_sdata:
98bfdba5 16165 *value = DW_SND (attr);
2df3850c
JM
16166 break;
16167
c906108c 16168 case DW_FORM_udata:
98bfdba5 16169 *value = DW_UNSND (attr);
c906108c 16170 break;
2df3850c 16171
c906108c 16172 default:
4d3c2250 16173 complaint (&symfile_complaints,
e2e0b3e5 16174 _("unsupported const value attribute form: '%s'"),
4d3c2250 16175 dwarf_form_name (attr->form));
98bfdba5 16176 *value = 0;
c906108c
SS
16177 break;
16178 }
16179}
16180
2df3850c 16181
98bfdba5
PA
16182/* Copy constant value from an attribute to a symbol. */
16183
2df3850c 16184static void
98bfdba5
PA
16185dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16186 struct dwarf2_cu *cu)
2df3850c 16187{
98bfdba5
PA
16188 struct objfile *objfile = cu->objfile;
16189 struct comp_unit_head *cu_header = &cu->header;
12df843f 16190 LONGEST value;
98bfdba5
PA
16191 gdb_byte *bytes;
16192 struct dwarf2_locexpr_baton *baton;
2df3850c 16193
98bfdba5
PA
16194 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16195 SYMBOL_PRINT_NAME (sym),
16196 &objfile->objfile_obstack, cu,
16197 &value, &bytes, &baton);
2df3850c 16198
98bfdba5
PA
16199 if (baton != NULL)
16200 {
16201 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16202 SYMBOL_LOCATION_BATON (sym) = baton;
16203 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16204 }
16205 else if (bytes != NULL)
16206 {
16207 SYMBOL_VALUE_BYTES (sym) = bytes;
16208 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16209 }
16210 else
16211 {
16212 SYMBOL_VALUE (sym) = value;
16213 SYMBOL_CLASS (sym) = LOC_CONST;
16214 }
2df3850c
JM
16215}
16216
c906108c
SS
16217/* Return the type of the die in question using its DW_AT_type attribute. */
16218
16219static struct type *
e7c27a73 16220die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16221{
c906108c 16222 struct attribute *type_attr;
c906108c 16223
e142c38c 16224 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16225 if (!type_attr)
16226 {
16227 /* A missing DW_AT_type represents a void type. */
46bf5051 16228 return objfile_type (cu->objfile)->builtin_void;
c906108c 16229 }
348e048f 16230
673bfd45 16231 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16232}
16233
b4ba55a1
JB
16234/* True iff CU's producer generates GNAT Ada auxiliary information
16235 that allows to find parallel types through that information instead
16236 of having to do expensive parallel lookups by type name. */
16237
16238static int
16239need_gnat_info (struct dwarf2_cu *cu)
16240{
16241 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16242 of GNAT produces this auxiliary information, without any indication
16243 that it is produced. Part of enhancing the FSF version of GNAT
16244 to produce that information will be to put in place an indicator
16245 that we can use in order to determine whether the descriptive type
16246 info is available or not. One suggestion that has been made is
16247 to use a new attribute, attached to the CU die. For now, assume
16248 that the descriptive type info is not available. */
16249 return 0;
16250}
16251
b4ba55a1
JB
16252/* Return the auxiliary type of the die in question using its
16253 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16254 attribute is not present. */
16255
16256static struct type *
16257die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16258{
b4ba55a1 16259 struct attribute *type_attr;
b4ba55a1
JB
16260
16261 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16262 if (!type_attr)
16263 return NULL;
16264
673bfd45 16265 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16266}
16267
16268/* If DIE has a descriptive_type attribute, then set the TYPE's
16269 descriptive type accordingly. */
16270
16271static void
16272set_descriptive_type (struct type *type, struct die_info *die,
16273 struct dwarf2_cu *cu)
16274{
16275 struct type *descriptive_type = die_descriptive_type (die, cu);
16276
16277 if (descriptive_type)
16278 {
16279 ALLOCATE_GNAT_AUX_TYPE (type);
16280 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16281 }
16282}
16283
c906108c
SS
16284/* Return the containing type of the die in question using its
16285 DW_AT_containing_type attribute. */
16286
16287static struct type *
e7c27a73 16288die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16289{
c906108c 16290 struct attribute *type_attr;
c906108c 16291
e142c38c 16292 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16293 if (!type_attr)
16294 error (_("Dwarf Error: Problem turning containing type into gdb type "
16295 "[in module %s]"), cu->objfile->name);
16296
673bfd45 16297 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16298}
16299
673bfd45
DE
16300/* Look up the type of DIE in CU using its type attribute ATTR.
16301 If there is no type substitute an error marker. */
16302
c906108c 16303static struct type *
673bfd45
DE
16304lookup_die_type (struct die_info *die, struct attribute *attr,
16305 struct dwarf2_cu *cu)
c906108c 16306{
bb5ed363 16307 struct objfile *objfile = cu->objfile;
f792889a
DJ
16308 struct type *this_type;
16309
673bfd45
DE
16310 /* First see if we have it cached. */
16311
36586728
TT
16312 if (attr->form == DW_FORM_GNU_ref_alt)
16313 {
16314 struct dwarf2_per_cu_data *per_cu;
16315 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16316
16317 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16318 this_type = get_die_type_at_offset (offset, per_cu);
16319 }
16320 else if (is_ref_attr (attr))
673bfd45 16321 {
b64f50a1 16322 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16323
16324 this_type = get_die_type_at_offset (offset, cu->per_cu);
16325 }
55f1336d 16326 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16327 {
16328 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16329
16330 /* sig_type will be NULL if the signatured type is missing from
16331 the debug info. */
16332 if (sig_type == NULL)
16333 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16334 "at 0x%x [in module %s]"),
b64f50a1 16335 die->offset.sect_off, objfile->name);
673bfd45 16336
3019eac3
DE
16337 gdb_assert (sig_type->per_cu.is_debug_types);
16338 /* If we haven't filled in type_offset_in_section yet, then we
16339 haven't read the type in yet. */
16340 this_type = NULL;
16341 if (sig_type->type_offset_in_section.sect_off != 0)
16342 {
16343 this_type =
16344 get_die_type_at_offset (sig_type->type_offset_in_section,
16345 &sig_type->per_cu);
16346 }
673bfd45
DE
16347 }
16348 else
16349 {
16350 dump_die_for_error (die);
16351 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16352 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16353 }
16354
16355 /* If not cached we need to read it in. */
16356
16357 if (this_type == NULL)
16358 {
16359 struct die_info *type_die;
16360 struct dwarf2_cu *type_cu = cu;
16361
16362 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16363 /* If we found the type now, it's probably because the type came
16364 from an inter-CU reference and the type's CU got expanded before
16365 ours. */
16366 this_type = get_die_type (type_die, type_cu);
16367 if (this_type == NULL)
16368 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16369 }
16370
16371 /* If we still don't have a type use an error marker. */
16372
16373 if (this_type == NULL)
c906108c 16374 {
b00fdb78
TT
16375 char *message, *saved;
16376
16377 /* read_type_die already issued a complaint. */
16378 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16379 objfile->name,
b64f50a1
JK
16380 cu->header.offset.sect_off,
16381 die->offset.sect_off);
bb5ed363 16382 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16383 message, strlen (message));
16384 xfree (message);
16385
bb5ed363 16386 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16387 }
673bfd45 16388
f792889a 16389 return this_type;
c906108c
SS
16390}
16391
673bfd45
DE
16392/* Return the type in DIE, CU.
16393 Returns NULL for invalid types.
16394
16395 This first does a lookup in the appropriate type_hash table,
16396 and only reads the die in if necessary.
16397
16398 NOTE: This can be called when reading in partial or full symbols. */
16399
f792889a 16400static struct type *
e7c27a73 16401read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16402{
f792889a
DJ
16403 struct type *this_type;
16404
16405 this_type = get_die_type (die, cu);
16406 if (this_type)
16407 return this_type;
16408
673bfd45
DE
16409 return read_type_die_1 (die, cu);
16410}
16411
16412/* Read the type in DIE, CU.
16413 Returns NULL for invalid types. */
16414
16415static struct type *
16416read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16417{
16418 struct type *this_type = NULL;
16419
c906108c
SS
16420 switch (die->tag)
16421 {
16422 case DW_TAG_class_type:
680b30c7 16423 case DW_TAG_interface_type:
c906108c
SS
16424 case DW_TAG_structure_type:
16425 case DW_TAG_union_type:
f792889a 16426 this_type = read_structure_type (die, cu);
c906108c
SS
16427 break;
16428 case DW_TAG_enumeration_type:
f792889a 16429 this_type = read_enumeration_type (die, cu);
c906108c
SS
16430 break;
16431 case DW_TAG_subprogram:
16432 case DW_TAG_subroutine_type:
edb3359d 16433 case DW_TAG_inlined_subroutine:
f792889a 16434 this_type = read_subroutine_type (die, cu);
c906108c
SS
16435 break;
16436 case DW_TAG_array_type:
f792889a 16437 this_type = read_array_type (die, cu);
c906108c 16438 break;
72019c9c 16439 case DW_TAG_set_type:
f792889a 16440 this_type = read_set_type (die, cu);
72019c9c 16441 break;
c906108c 16442 case DW_TAG_pointer_type:
f792889a 16443 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16444 break;
16445 case DW_TAG_ptr_to_member_type:
f792889a 16446 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16447 break;
16448 case DW_TAG_reference_type:
f792889a 16449 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16450 break;
16451 case DW_TAG_const_type:
f792889a 16452 this_type = read_tag_const_type (die, cu);
c906108c
SS
16453 break;
16454 case DW_TAG_volatile_type:
f792889a 16455 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
16456 break;
16457 case DW_TAG_string_type:
f792889a 16458 this_type = read_tag_string_type (die, cu);
c906108c
SS
16459 break;
16460 case DW_TAG_typedef:
f792889a 16461 this_type = read_typedef (die, cu);
c906108c 16462 break;
a02abb62 16463 case DW_TAG_subrange_type:
f792889a 16464 this_type = read_subrange_type (die, cu);
a02abb62 16465 break;
c906108c 16466 case DW_TAG_base_type:
f792889a 16467 this_type = read_base_type (die, cu);
c906108c 16468 break;
81a17f79 16469 case DW_TAG_unspecified_type:
f792889a 16470 this_type = read_unspecified_type (die, cu);
81a17f79 16471 break;
0114d602
DJ
16472 case DW_TAG_namespace:
16473 this_type = read_namespace_type (die, cu);
16474 break;
f55ee35c
JK
16475 case DW_TAG_module:
16476 this_type = read_module_type (die, cu);
16477 break;
c906108c 16478 default:
3e43a32a
MS
16479 complaint (&symfile_complaints,
16480 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16481 dwarf_tag_name (die->tag));
c906108c
SS
16482 break;
16483 }
63d06c5c 16484
f792889a 16485 return this_type;
63d06c5c
DC
16486}
16487
abc72ce4
DE
16488/* See if we can figure out if the class lives in a namespace. We do
16489 this by looking for a member function; its demangled name will
16490 contain namespace info, if there is any.
16491 Return the computed name or NULL.
16492 Space for the result is allocated on the objfile's obstack.
16493 This is the full-die version of guess_partial_die_structure_name.
16494 In this case we know DIE has no useful parent. */
16495
16496static char *
16497guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16498{
16499 struct die_info *spec_die;
16500 struct dwarf2_cu *spec_cu;
16501 struct die_info *child;
16502
16503 spec_cu = cu;
16504 spec_die = die_specification (die, &spec_cu);
16505 if (spec_die != NULL)
16506 {
16507 die = spec_die;
16508 cu = spec_cu;
16509 }
16510
16511 for (child = die->child;
16512 child != NULL;
16513 child = child->sibling)
16514 {
16515 if (child->tag == DW_TAG_subprogram)
16516 {
16517 struct attribute *attr;
16518
16519 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16520 if (attr == NULL)
16521 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16522 if (attr != NULL)
16523 {
16524 char *actual_name
16525 = language_class_name_from_physname (cu->language_defn,
16526 DW_STRING (attr));
16527 char *name = NULL;
16528
16529 if (actual_name != NULL)
16530 {
16531 char *die_name = dwarf2_name (die, cu);
16532
16533 if (die_name != NULL
16534 && strcmp (die_name, actual_name) != 0)
16535 {
16536 /* Strip off the class name from the full name.
16537 We want the prefix. */
16538 int die_name_len = strlen (die_name);
16539 int actual_name_len = strlen (actual_name);
16540
16541 /* Test for '::' as a sanity check. */
16542 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16543 && actual_name[actual_name_len
16544 - die_name_len - 1] == ':')
abc72ce4
DE
16545 name =
16546 obsavestring (actual_name,
16547 actual_name_len - die_name_len - 2,
16548 &cu->objfile->objfile_obstack);
16549 }
16550 }
16551 xfree (actual_name);
16552 return name;
16553 }
16554 }
16555 }
16556
16557 return NULL;
16558}
16559
96408a79
SA
16560/* GCC might emit a nameless typedef that has a linkage name. Determine the
16561 prefix part in such case. See
16562 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16563
16564static char *
16565anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16566{
16567 struct attribute *attr;
16568 char *base;
16569
16570 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16571 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16572 return NULL;
16573
16574 attr = dwarf2_attr (die, DW_AT_name, cu);
16575 if (attr != NULL && DW_STRING (attr) != NULL)
16576 return NULL;
16577
16578 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16579 if (attr == NULL)
16580 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16581 if (attr == NULL || DW_STRING (attr) == NULL)
16582 return NULL;
16583
16584 /* dwarf2_name had to be already called. */
16585 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16586
16587 /* Strip the base name, keep any leading namespaces/classes. */
16588 base = strrchr (DW_STRING (attr), ':');
16589 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16590 return "";
16591
16592 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16593 &cu->objfile->objfile_obstack);
16594}
16595
fdde2d81 16596/* Return the name of the namespace/class that DIE is defined within,
0114d602 16597 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16598
0114d602
DJ
16599 For example, if we're within the method foo() in the following
16600 code:
16601
16602 namespace N {
16603 class C {
16604 void foo () {
16605 }
16606 };
16607 }
16608
16609 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16610
0d5cff50 16611static const char *
e142c38c 16612determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16613{
0114d602
DJ
16614 struct die_info *parent, *spec_die;
16615 struct dwarf2_cu *spec_cu;
16616 struct type *parent_type;
96408a79 16617 char *retval;
63d06c5c 16618
f55ee35c
JK
16619 if (cu->language != language_cplus && cu->language != language_java
16620 && cu->language != language_fortran)
0114d602
DJ
16621 return "";
16622
96408a79
SA
16623 retval = anonymous_struct_prefix (die, cu);
16624 if (retval)
16625 return retval;
16626
0114d602
DJ
16627 /* We have to be careful in the presence of DW_AT_specification.
16628 For example, with GCC 3.4, given the code
16629
16630 namespace N {
16631 void foo() {
16632 // Definition of N::foo.
16633 }
16634 }
16635
16636 then we'll have a tree of DIEs like this:
16637
16638 1: DW_TAG_compile_unit
16639 2: DW_TAG_namespace // N
16640 3: DW_TAG_subprogram // declaration of N::foo
16641 4: DW_TAG_subprogram // definition of N::foo
16642 DW_AT_specification // refers to die #3
16643
16644 Thus, when processing die #4, we have to pretend that we're in
16645 the context of its DW_AT_specification, namely the contex of die
16646 #3. */
16647 spec_cu = cu;
16648 spec_die = die_specification (die, &spec_cu);
16649 if (spec_die == NULL)
16650 parent = die->parent;
16651 else
63d06c5c 16652 {
0114d602
DJ
16653 parent = spec_die->parent;
16654 cu = spec_cu;
63d06c5c 16655 }
0114d602
DJ
16656
16657 if (parent == NULL)
16658 return "";
98bfdba5
PA
16659 else if (parent->building_fullname)
16660 {
16661 const char *name;
16662 const char *parent_name;
16663
16664 /* It has been seen on RealView 2.2 built binaries,
16665 DW_TAG_template_type_param types actually _defined_ as
16666 children of the parent class:
16667
16668 enum E {};
16669 template class <class Enum> Class{};
16670 Class<enum E> class_e;
16671
16672 1: DW_TAG_class_type (Class)
16673 2: DW_TAG_enumeration_type (E)
16674 3: DW_TAG_enumerator (enum1:0)
16675 3: DW_TAG_enumerator (enum2:1)
16676 ...
16677 2: DW_TAG_template_type_param
16678 DW_AT_type DW_FORM_ref_udata (E)
16679
16680 Besides being broken debug info, it can put GDB into an
16681 infinite loop. Consider:
16682
16683 When we're building the full name for Class<E>, we'll start
16684 at Class, and go look over its template type parameters,
16685 finding E. We'll then try to build the full name of E, and
16686 reach here. We're now trying to build the full name of E,
16687 and look over the parent DIE for containing scope. In the
16688 broken case, if we followed the parent DIE of E, we'd again
16689 find Class, and once again go look at its template type
16690 arguments, etc., etc. Simply don't consider such parent die
16691 as source-level parent of this die (it can't be, the language
16692 doesn't allow it), and break the loop here. */
16693 name = dwarf2_name (die, cu);
16694 parent_name = dwarf2_name (parent, cu);
16695 complaint (&symfile_complaints,
16696 _("template param type '%s' defined within parent '%s'"),
16697 name ? name : "<unknown>",
16698 parent_name ? parent_name : "<unknown>");
16699 return "";
16700 }
63d06c5c 16701 else
0114d602
DJ
16702 switch (parent->tag)
16703 {
63d06c5c 16704 case DW_TAG_namespace:
0114d602 16705 parent_type = read_type_die (parent, cu);
acebe513
UW
16706 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16707 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16708 Work around this problem here. */
16709 if (cu->language == language_cplus
16710 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16711 return "";
0114d602
DJ
16712 /* We give a name to even anonymous namespaces. */
16713 return TYPE_TAG_NAME (parent_type);
63d06c5c 16714 case DW_TAG_class_type:
680b30c7 16715 case DW_TAG_interface_type:
63d06c5c 16716 case DW_TAG_structure_type:
0114d602 16717 case DW_TAG_union_type:
f55ee35c 16718 case DW_TAG_module:
0114d602
DJ
16719 parent_type = read_type_die (parent, cu);
16720 if (TYPE_TAG_NAME (parent_type) != NULL)
16721 return TYPE_TAG_NAME (parent_type);
16722 else
16723 /* An anonymous structure is only allowed non-static data
16724 members; no typedefs, no member functions, et cetera.
16725 So it does not need a prefix. */
16726 return "";
abc72ce4 16727 case DW_TAG_compile_unit:
95554aad 16728 case DW_TAG_partial_unit:
abc72ce4
DE
16729 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16730 if (cu->language == language_cplus
8b70b953 16731 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16732 && die->child != NULL
16733 && (die->tag == DW_TAG_class_type
16734 || die->tag == DW_TAG_structure_type
16735 || die->tag == DW_TAG_union_type))
16736 {
16737 char *name = guess_full_die_structure_name (die, cu);
16738 if (name != NULL)
16739 return name;
16740 }
16741 return "";
63d06c5c 16742 default:
8176b9b8 16743 return determine_prefix (parent, cu);
63d06c5c 16744 }
63d06c5c
DC
16745}
16746
3e43a32a
MS
16747/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16748 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16749 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16750 an obconcat, otherwise allocate storage for the result. The CU argument is
16751 used to determine the language and hence, the appropriate separator. */
987504bb 16752
f55ee35c 16753#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16754
16755static char *
f55ee35c
JK
16756typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16757 int physname, struct dwarf2_cu *cu)
63d06c5c 16758{
f55ee35c 16759 const char *lead = "";
5c315b68 16760 const char *sep;
63d06c5c 16761
3e43a32a
MS
16762 if (suffix == NULL || suffix[0] == '\0'
16763 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16764 sep = "";
16765 else if (cu->language == language_java)
16766 sep = ".";
f55ee35c
JK
16767 else if (cu->language == language_fortran && physname)
16768 {
16769 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16770 DW_AT_MIPS_linkage_name is preferred and used instead. */
16771
16772 lead = "__";
16773 sep = "_MOD_";
16774 }
987504bb
JJ
16775 else
16776 sep = "::";
63d06c5c 16777
6dd47d34
DE
16778 if (prefix == NULL)
16779 prefix = "";
16780 if (suffix == NULL)
16781 suffix = "";
16782
987504bb
JJ
16783 if (obs == NULL)
16784 {
3e43a32a
MS
16785 char *retval
16786 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16787
f55ee35c
JK
16788 strcpy (retval, lead);
16789 strcat (retval, prefix);
6dd47d34
DE
16790 strcat (retval, sep);
16791 strcat (retval, suffix);
63d06c5c
DC
16792 return retval;
16793 }
987504bb
JJ
16794 else
16795 {
16796 /* We have an obstack. */
f55ee35c 16797 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16798 }
63d06c5c
DC
16799}
16800
c906108c
SS
16801/* Return sibling of die, NULL if no sibling. */
16802
f9aca02d 16803static struct die_info *
fba45db2 16804sibling_die (struct die_info *die)
c906108c 16805{
639d11d3 16806 return die->sibling;
c906108c
SS
16807}
16808
71c25dea
TT
16809/* Get name of a die, return NULL if not found. */
16810
16811static char *
16812dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16813 struct obstack *obstack)
16814{
16815 if (name && cu->language == language_cplus)
16816 {
16817 char *canon_name = cp_canonicalize_string (name);
16818
16819 if (canon_name != NULL)
16820 {
16821 if (strcmp (canon_name, name) != 0)
16822 name = obsavestring (canon_name, strlen (canon_name),
16823 obstack);
16824 xfree (canon_name);
16825 }
16826 }
16827
16828 return name;
c906108c
SS
16829}
16830
9219021c
DC
16831/* Get name of a die, return NULL if not found. */
16832
16833static char *
e142c38c 16834dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16835{
16836 struct attribute *attr;
16837
e142c38c 16838 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16839 if ((!attr || !DW_STRING (attr))
16840 && die->tag != DW_TAG_class_type
16841 && die->tag != DW_TAG_interface_type
16842 && die->tag != DW_TAG_structure_type
16843 && die->tag != DW_TAG_union_type)
71c25dea
TT
16844 return NULL;
16845
16846 switch (die->tag)
16847 {
16848 case DW_TAG_compile_unit:
95554aad 16849 case DW_TAG_partial_unit:
71c25dea
TT
16850 /* Compilation units have a DW_AT_name that is a filename, not
16851 a source language identifier. */
16852 case DW_TAG_enumeration_type:
16853 case DW_TAG_enumerator:
16854 /* These tags always have simple identifiers already; no need
16855 to canonicalize them. */
16856 return DW_STRING (attr);
907af001 16857
418835cc
KS
16858 case DW_TAG_subprogram:
16859 /* Java constructors will all be named "<init>", so return
16860 the class name when we see this special case. */
16861 if (cu->language == language_java
16862 && DW_STRING (attr) != NULL
16863 && strcmp (DW_STRING (attr), "<init>") == 0)
16864 {
16865 struct dwarf2_cu *spec_cu = cu;
16866 struct die_info *spec_die;
16867
16868 /* GCJ will output '<init>' for Java constructor names.
16869 For this special case, return the name of the parent class. */
16870
16871 /* GCJ may output suprogram DIEs with AT_specification set.
16872 If so, use the name of the specified DIE. */
16873 spec_die = die_specification (die, &spec_cu);
16874 if (spec_die != NULL)
16875 return dwarf2_name (spec_die, spec_cu);
16876
16877 do
16878 {
16879 die = die->parent;
16880 if (die->tag == DW_TAG_class_type)
16881 return dwarf2_name (die, cu);
16882 }
95554aad
TT
16883 while (die->tag != DW_TAG_compile_unit
16884 && die->tag != DW_TAG_partial_unit);
418835cc 16885 }
907af001
UW
16886 break;
16887
16888 case DW_TAG_class_type:
16889 case DW_TAG_interface_type:
16890 case DW_TAG_structure_type:
16891 case DW_TAG_union_type:
16892 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16893 structures or unions. These were of the form "._%d" in GCC 4.1,
16894 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16895 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16896 if (attr && DW_STRING (attr)
16897 && (strncmp (DW_STRING (attr), "._", 2) == 0
16898 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16899 return NULL;
53832f31
TT
16900
16901 /* GCC might emit a nameless typedef that has a linkage name. See
16902 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16903 if (!attr || DW_STRING (attr) == NULL)
16904 {
df5c6c50 16905 char *demangled = NULL;
53832f31
TT
16906
16907 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16908 if (attr == NULL)
16909 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16910
16911 if (attr == NULL || DW_STRING (attr) == NULL)
16912 return NULL;
16913
df5c6c50
JK
16914 /* Avoid demangling DW_STRING (attr) the second time on a second
16915 call for the same DIE. */
16916 if (!DW_STRING_IS_CANONICAL (attr))
16917 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16918
16919 if (demangled)
16920 {
96408a79
SA
16921 char *base;
16922
53832f31 16923 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16924 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16925 &cu->objfile->objfile_obstack);
53832f31
TT
16926 DW_STRING_IS_CANONICAL (attr) = 1;
16927 xfree (demangled);
96408a79
SA
16928
16929 /* Strip any leading namespaces/classes, keep only the base name.
16930 DW_AT_name for named DIEs does not contain the prefixes. */
16931 base = strrchr (DW_STRING (attr), ':');
16932 if (base && base > DW_STRING (attr) && base[-1] == ':')
16933 return &base[1];
16934 else
16935 return DW_STRING (attr);
53832f31
TT
16936 }
16937 }
907af001
UW
16938 break;
16939
71c25dea 16940 default:
907af001
UW
16941 break;
16942 }
16943
16944 if (!DW_STRING_IS_CANONICAL (attr))
16945 {
16946 DW_STRING (attr)
16947 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16948 &cu->objfile->objfile_obstack);
16949 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16950 }
907af001 16951 return DW_STRING (attr);
9219021c
DC
16952}
16953
16954/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16955 is none. *EXT_CU is the CU containing DIE on input, and the CU
16956 containing the return value on output. */
9219021c
DC
16957
16958static struct die_info *
f2f0e013 16959dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16960{
16961 struct attribute *attr;
9219021c 16962
f2f0e013 16963 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16964 if (attr == NULL)
16965 return NULL;
16966
f2f0e013 16967 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16968}
16969
c906108c
SS
16970/* Convert a DIE tag into its string name. */
16971
f39c6ffd 16972static const char *
aa1ee363 16973dwarf_tag_name (unsigned tag)
c906108c 16974{
f39c6ffd
TT
16975 const char *name = get_DW_TAG_name (tag);
16976
16977 if (name == NULL)
16978 return "DW_TAG_<unknown>";
16979
16980 return name;
c906108c
SS
16981}
16982
16983/* Convert a DWARF attribute code into its string name. */
16984
f39c6ffd 16985static const char *
aa1ee363 16986dwarf_attr_name (unsigned attr)
c906108c 16987{
f39c6ffd
TT
16988 const char *name;
16989
c764a876 16990#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16991 if (attr == DW_AT_MIPS_fde)
16992 return "DW_AT_MIPS_fde";
16993#else
16994 if (attr == DW_AT_HP_block_index)
16995 return "DW_AT_HP_block_index";
c764a876 16996#endif
f39c6ffd
TT
16997
16998 name = get_DW_AT_name (attr);
16999
17000 if (name == NULL)
17001 return "DW_AT_<unknown>";
17002
17003 return name;
c906108c
SS
17004}
17005
17006/* Convert a DWARF value form code into its string name. */
17007
f39c6ffd 17008static const char *
aa1ee363 17009dwarf_form_name (unsigned form)
c906108c 17010{
f39c6ffd
TT
17011 const char *name = get_DW_FORM_name (form);
17012
17013 if (name == NULL)
17014 return "DW_FORM_<unknown>";
17015
17016 return name;
c906108c
SS
17017}
17018
17019static char *
fba45db2 17020dwarf_bool_name (unsigned mybool)
c906108c
SS
17021{
17022 if (mybool)
17023 return "TRUE";
17024 else
17025 return "FALSE";
17026}
17027
17028/* Convert a DWARF type code into its string name. */
17029
f39c6ffd 17030static const char *
aa1ee363 17031dwarf_type_encoding_name (unsigned enc)
c906108c 17032{
f39c6ffd 17033 const char *name = get_DW_ATE_name (enc);
c906108c 17034
f39c6ffd
TT
17035 if (name == NULL)
17036 return "DW_ATE_<unknown>";
c906108c 17037
f39c6ffd 17038 return name;
c906108c 17039}
c906108c 17040
f9aca02d 17041static void
d97bc12b 17042dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17043{
17044 unsigned int i;
17045
d97bc12b
DE
17046 print_spaces (indent, f);
17047 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17048 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17049
17050 if (die->parent != NULL)
17051 {
17052 print_spaces (indent, f);
17053 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17054 die->parent->offset.sect_off);
d97bc12b
DE
17055 }
17056
17057 print_spaces (indent, f);
17058 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17059 dwarf_bool_name (die->child != NULL));
c906108c 17060
d97bc12b
DE
17061 print_spaces (indent, f);
17062 fprintf_unfiltered (f, " attributes:\n");
17063
c906108c
SS
17064 for (i = 0; i < die->num_attrs; ++i)
17065 {
d97bc12b
DE
17066 print_spaces (indent, f);
17067 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17068 dwarf_attr_name (die->attrs[i].name),
17069 dwarf_form_name (die->attrs[i].form));
d97bc12b 17070
c906108c
SS
17071 switch (die->attrs[i].form)
17072 {
c906108c 17073 case DW_FORM_addr:
3019eac3 17074 case DW_FORM_GNU_addr_index:
d97bc12b 17075 fprintf_unfiltered (f, "address: ");
5af949e3 17076 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17077 break;
17078 case DW_FORM_block2:
17079 case DW_FORM_block4:
17080 case DW_FORM_block:
17081 case DW_FORM_block1:
56eb65bd
SP
17082 fprintf_unfiltered (f, "block: size %s",
17083 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17084 break;
2dc7f7b3 17085 case DW_FORM_exprloc:
56eb65bd
SP
17086 fprintf_unfiltered (f, "expression: size %s",
17087 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17088 break;
4568ecf9
DE
17089 case DW_FORM_ref_addr:
17090 fprintf_unfiltered (f, "ref address: ");
17091 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17092 break;
36586728
TT
17093 case DW_FORM_GNU_ref_alt:
17094 fprintf_unfiltered (f, "alt ref address: ");
17095 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17096 break;
10b3939b
DJ
17097 case DW_FORM_ref1:
17098 case DW_FORM_ref2:
17099 case DW_FORM_ref4:
4568ecf9
DE
17100 case DW_FORM_ref8:
17101 case DW_FORM_ref_udata:
d97bc12b 17102 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17103 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17104 break;
c906108c
SS
17105 case DW_FORM_data1:
17106 case DW_FORM_data2:
17107 case DW_FORM_data4:
ce5d95e1 17108 case DW_FORM_data8:
c906108c
SS
17109 case DW_FORM_udata:
17110 case DW_FORM_sdata:
43bbcdc2
PH
17111 fprintf_unfiltered (f, "constant: %s",
17112 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17113 break;
2dc7f7b3
TT
17114 case DW_FORM_sec_offset:
17115 fprintf_unfiltered (f, "section offset: %s",
17116 pulongest (DW_UNSND (&die->attrs[i])));
17117 break;
55f1336d 17118 case DW_FORM_ref_sig8:
348e048f
DE
17119 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17120 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17121 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17122 else
17123 fprintf_unfiltered (f, "signatured type, offset: unknown");
17124 break;
c906108c 17125 case DW_FORM_string:
4bdf3d34 17126 case DW_FORM_strp:
3019eac3 17127 case DW_FORM_GNU_str_index:
36586728 17128 case DW_FORM_GNU_strp_alt:
8285870a 17129 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17130 DW_STRING (&die->attrs[i])
8285870a
JK
17131 ? DW_STRING (&die->attrs[i]) : "",
17132 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17133 break;
17134 case DW_FORM_flag:
17135 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17136 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17137 else
d97bc12b 17138 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17139 break;
2dc7f7b3
TT
17140 case DW_FORM_flag_present:
17141 fprintf_unfiltered (f, "flag: TRUE");
17142 break;
a8329558 17143 case DW_FORM_indirect:
0963b4bd
MS
17144 /* The reader will have reduced the indirect form to
17145 the "base form" so this form should not occur. */
3e43a32a
MS
17146 fprintf_unfiltered (f,
17147 "unexpected attribute form: DW_FORM_indirect");
a8329558 17148 break;
c906108c 17149 default:
d97bc12b 17150 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17151 die->attrs[i].form);
d97bc12b 17152 break;
c906108c 17153 }
d97bc12b 17154 fprintf_unfiltered (f, "\n");
c906108c
SS
17155 }
17156}
17157
f9aca02d 17158static void
d97bc12b 17159dump_die_for_error (struct die_info *die)
c906108c 17160{
d97bc12b
DE
17161 dump_die_shallow (gdb_stderr, 0, die);
17162}
17163
17164static void
17165dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17166{
17167 int indent = level * 4;
17168
17169 gdb_assert (die != NULL);
17170
17171 if (level >= max_level)
17172 return;
17173
17174 dump_die_shallow (f, indent, die);
17175
17176 if (die->child != NULL)
c906108c 17177 {
d97bc12b
DE
17178 print_spaces (indent, f);
17179 fprintf_unfiltered (f, " Children:");
17180 if (level + 1 < max_level)
17181 {
17182 fprintf_unfiltered (f, "\n");
17183 dump_die_1 (f, level + 1, max_level, die->child);
17184 }
17185 else
17186 {
3e43a32a
MS
17187 fprintf_unfiltered (f,
17188 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17189 }
17190 }
17191
17192 if (die->sibling != NULL && level > 0)
17193 {
17194 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17195 }
17196}
17197
d97bc12b
DE
17198/* This is called from the pdie macro in gdbinit.in.
17199 It's not static so gcc will keep a copy callable from gdb. */
17200
17201void
17202dump_die (struct die_info *die, int max_level)
17203{
17204 dump_die_1 (gdb_stdlog, 0, max_level, die);
17205}
17206
f9aca02d 17207static void
51545339 17208store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17209{
51545339 17210 void **slot;
c906108c 17211
b64f50a1
JK
17212 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17213 INSERT);
51545339
DJ
17214
17215 *slot = die;
c906108c
SS
17216}
17217
b64f50a1
JK
17218/* DW_ADDR is always stored already as sect_offset; despite for the forms
17219 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17220
93311388
DE
17221static int
17222is_ref_attr (struct attribute *attr)
c906108c 17223{
c906108c
SS
17224 switch (attr->form)
17225 {
17226 case DW_FORM_ref_addr:
c906108c
SS
17227 case DW_FORM_ref1:
17228 case DW_FORM_ref2:
17229 case DW_FORM_ref4:
613e1657 17230 case DW_FORM_ref8:
c906108c 17231 case DW_FORM_ref_udata:
36586728 17232 case DW_FORM_GNU_ref_alt:
93311388 17233 return 1;
c906108c 17234 default:
93311388 17235 return 0;
c906108c 17236 }
93311388
DE
17237}
17238
b64f50a1
JK
17239/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17240 required kind. */
17241
17242static sect_offset
93311388
DE
17243dwarf2_get_ref_die_offset (struct attribute *attr)
17244{
4568ecf9 17245 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17246
93311388 17247 if (is_ref_attr (attr))
b64f50a1 17248 return retval;
93311388 17249
b64f50a1 17250 retval.sect_off = 0;
93311388
DE
17251 complaint (&symfile_complaints,
17252 _("unsupported die ref attribute form: '%s'"),
17253 dwarf_form_name (attr->form));
b64f50a1 17254 return retval;
c906108c
SS
17255}
17256
43bbcdc2
PH
17257/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17258 * the value held by the attribute is not constant. */
a02abb62 17259
43bbcdc2 17260static LONGEST
a02abb62
JB
17261dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17262{
17263 if (attr->form == DW_FORM_sdata)
17264 return DW_SND (attr);
17265 else if (attr->form == DW_FORM_udata
17266 || attr->form == DW_FORM_data1
17267 || attr->form == DW_FORM_data2
17268 || attr->form == DW_FORM_data4
17269 || attr->form == DW_FORM_data8)
17270 return DW_UNSND (attr);
17271 else
17272 {
3e43a32a
MS
17273 complaint (&symfile_complaints,
17274 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17275 dwarf_form_name (attr->form));
17276 return default_value;
17277 }
17278}
17279
348e048f
DE
17280/* Follow reference or signature attribute ATTR of SRC_DIE.
17281 On entry *REF_CU is the CU of SRC_DIE.
17282 On exit *REF_CU is the CU of the result. */
17283
17284static struct die_info *
17285follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17286 struct dwarf2_cu **ref_cu)
17287{
17288 struct die_info *die;
17289
17290 if (is_ref_attr (attr))
17291 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17292 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17293 die = follow_die_sig (src_die, attr, ref_cu);
17294 else
17295 {
17296 dump_die_for_error (src_die);
17297 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17298 (*ref_cu)->objfile->name);
17299 }
17300
17301 return die;
03dd20cc
DJ
17302}
17303
5c631832 17304/* Follow reference OFFSET.
673bfd45
DE
17305 On entry *REF_CU is the CU of the source die referencing OFFSET.
17306 On exit *REF_CU is the CU of the result.
17307 Returns NULL if OFFSET is invalid. */
f504f079 17308
f9aca02d 17309static struct die_info *
36586728
TT
17310follow_die_offset (sect_offset offset, int offset_in_dwz,
17311 struct dwarf2_cu **ref_cu)
c906108c 17312{
10b3939b 17313 struct die_info temp_die;
f2f0e013 17314 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17315
348e048f
DE
17316 gdb_assert (cu->per_cu != NULL);
17317
98bfdba5
PA
17318 target_cu = cu;
17319
3019eac3 17320 if (cu->per_cu->is_debug_types)
348e048f
DE
17321 {
17322 /* .debug_types CUs cannot reference anything outside their CU.
17323 If they need to, they have to reference a signatured type via
55f1336d 17324 DW_FORM_ref_sig8. */
348e048f 17325 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17326 return NULL;
348e048f 17327 }
36586728
TT
17328 else if (offset_in_dwz != cu->per_cu->is_dwz
17329 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17330 {
17331 struct dwarf2_per_cu_data *per_cu;
9a619af0 17332
36586728
TT
17333 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17334 cu->objfile);
03dd20cc
DJ
17335
17336 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17337 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17338 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17339
10b3939b
DJ
17340 target_cu = per_cu->cu;
17341 }
98bfdba5
PA
17342 else if (cu->dies == NULL)
17343 {
17344 /* We're loading full DIEs during partial symbol reading. */
17345 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17346 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17347 }
c906108c 17348
f2f0e013 17349 *ref_cu = target_cu;
51545339 17350 temp_die.offset = offset;
b64f50a1 17351 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17352}
10b3939b 17353
5c631832
JK
17354/* Follow reference attribute ATTR of SRC_DIE.
17355 On entry *REF_CU is the CU of SRC_DIE.
17356 On exit *REF_CU is the CU of the result. */
17357
17358static struct die_info *
17359follow_die_ref (struct die_info *src_die, struct attribute *attr,
17360 struct dwarf2_cu **ref_cu)
17361{
b64f50a1 17362 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17363 struct dwarf2_cu *cu = *ref_cu;
17364 struct die_info *die;
17365
36586728
TT
17366 die = follow_die_offset (offset,
17367 (attr->form == DW_FORM_GNU_ref_alt
17368 || cu->per_cu->is_dwz),
17369 ref_cu);
5c631832
JK
17370 if (!die)
17371 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17372 "at 0x%x [in module %s]"),
b64f50a1 17373 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17374
5c631832
JK
17375 return die;
17376}
17377
d83e736b
JK
17378/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17379 Returned value is intended for DW_OP_call*. Returned
17380 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17381
17382struct dwarf2_locexpr_baton
8b9737bf
TT
17383dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17384 struct dwarf2_per_cu_data *per_cu,
17385 CORE_ADDR (*get_frame_pc) (void *baton),
17386 void *baton)
5c631832 17387{
918dd910 17388 struct dwarf2_cu *cu;
5c631832
JK
17389 struct die_info *die;
17390 struct attribute *attr;
17391 struct dwarf2_locexpr_baton retval;
17392
8cf6f0b1
TT
17393 dw2_setup (per_cu->objfile);
17394
918dd910
JK
17395 if (per_cu->cu == NULL)
17396 load_cu (per_cu);
17397 cu = per_cu->cu;
17398
36586728 17399 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17400 if (!die)
17401 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17402 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17403
17404 attr = dwarf2_attr (die, DW_AT_location, cu);
17405 if (!attr)
17406 {
e103e986
JK
17407 /* DWARF: "If there is no such attribute, then there is no effect.".
17408 DATA is ignored if SIZE is 0. */
5c631832 17409
e103e986 17410 retval.data = NULL;
5c631832
JK
17411 retval.size = 0;
17412 }
8cf6f0b1
TT
17413 else if (attr_form_is_section_offset (attr))
17414 {
17415 struct dwarf2_loclist_baton loclist_baton;
17416 CORE_ADDR pc = (*get_frame_pc) (baton);
17417 size_t size;
17418
17419 fill_in_loclist_baton (cu, &loclist_baton, attr);
17420
17421 retval.data = dwarf2_find_location_expression (&loclist_baton,
17422 &size, pc);
17423 retval.size = size;
17424 }
5c631832
JK
17425 else
17426 {
17427 if (!attr_form_is_block (attr))
17428 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17429 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17430 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17431
17432 retval.data = DW_BLOCK (attr)->data;
17433 retval.size = DW_BLOCK (attr)->size;
17434 }
17435 retval.per_cu = cu->per_cu;
918dd910 17436
918dd910
JK
17437 age_cached_comp_units ();
17438
5c631832 17439 return retval;
348e048f
DE
17440}
17441
8b9737bf
TT
17442/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17443 offset. */
17444
17445struct dwarf2_locexpr_baton
17446dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17447 struct dwarf2_per_cu_data *per_cu,
17448 CORE_ADDR (*get_frame_pc) (void *baton),
17449 void *baton)
17450{
17451 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17452
17453 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17454}
17455
8a9b8146
TT
17456/* Return the type of the DIE at DIE_OFFSET in the CU named by
17457 PER_CU. */
17458
17459struct type *
b64f50a1 17460dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17461 struct dwarf2_per_cu_data *per_cu)
17462{
b64f50a1
JK
17463 sect_offset die_offset_sect;
17464
8a9b8146 17465 dw2_setup (per_cu->objfile);
b64f50a1
JK
17466
17467 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17468 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17469}
17470
348e048f
DE
17471/* Follow the signature attribute ATTR in SRC_DIE.
17472 On entry *REF_CU is the CU of SRC_DIE.
17473 On exit *REF_CU is the CU of the result. */
17474
17475static struct die_info *
17476follow_die_sig (struct die_info *src_die, struct attribute *attr,
17477 struct dwarf2_cu **ref_cu)
17478{
17479 struct objfile *objfile = (*ref_cu)->objfile;
17480 struct die_info temp_die;
17481 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17482 struct dwarf2_cu *sig_cu;
17483 struct die_info *die;
17484
17485 /* sig_type will be NULL if the signatured type is missing from
17486 the debug info. */
17487 if (sig_type == NULL)
17488 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17489 "at 0x%x [in module %s]"),
b64f50a1 17490 src_die->offset.sect_off, objfile->name);
348e048f
DE
17491
17492 /* If necessary, add it to the queue and load its DIEs. */
17493
95554aad 17494 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17495 read_signatured_type (sig_type);
348e048f
DE
17496
17497 gdb_assert (sig_type->per_cu.cu != NULL);
17498
17499 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17500 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17501 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17502 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17503 temp_die.offset.sect_off);
348e048f
DE
17504 if (die)
17505 {
17506 *ref_cu = sig_cu;
17507 return die;
17508 }
17509
3e43a32a
MS
17510 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17511 "from DIE at 0x%x [in module %s]"),
b64f50a1 17512 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17513}
17514
17515/* Given an offset of a signatured type, return its signatured_type. */
17516
17517static struct signatured_type *
8b70b953
TT
17518lookup_signatured_type_at_offset (struct objfile *objfile,
17519 struct dwarf2_section_info *section,
b64f50a1 17520 sect_offset offset)
348e048f 17521{
b64f50a1 17522 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17523 unsigned int length, initial_length_size;
17524 unsigned int sig_offset;
52dc124a 17525 struct signatured_type find_entry, *sig_type;
348e048f
DE
17526
17527 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17528 sig_offset = (initial_length_size
17529 + 2 /*version*/
17530 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17531 + 1 /*address_size*/);
17532 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17533 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17534
17535 /* This is only used to lookup previously recorded types.
17536 If we didn't find it, it's our bug. */
52dc124a
DE
17537 gdb_assert (sig_type != NULL);
17538 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17539
52dc124a 17540 return sig_type;
348e048f
DE
17541}
17542
e5fe5e75 17543/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17544
17545static void
e5fe5e75 17546load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17547{
52dc124a 17548 struct signatured_type *sig_type;
348e048f 17549
f4dc4d17
DE
17550 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17551 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17552
6721b2ec
DE
17553 /* We have the per_cu, but we need the signatured_type.
17554 Fortunately this is an easy translation. */
17555 gdb_assert (per_cu->is_debug_types);
17556 sig_type = (struct signatured_type *) per_cu;
348e048f 17557
6721b2ec 17558 gdb_assert (per_cu->cu == NULL);
348e048f 17559
52dc124a 17560 read_signatured_type (sig_type);
348e048f 17561
6721b2ec 17562 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17563}
17564
dee91e82
DE
17565/* die_reader_func for read_signatured_type.
17566 This is identical to load_full_comp_unit_reader,
17567 but is kept separate for now. */
348e048f
DE
17568
17569static void
dee91e82
DE
17570read_signatured_type_reader (const struct die_reader_specs *reader,
17571 gdb_byte *info_ptr,
17572 struct die_info *comp_unit_die,
17573 int has_children,
17574 void *data)
348e048f 17575{
dee91e82 17576 struct dwarf2_cu *cu = reader->cu;
348e048f 17577
dee91e82
DE
17578 gdb_assert (cu->die_hash == NULL);
17579 cu->die_hash =
17580 htab_create_alloc_ex (cu->header.length / 12,
17581 die_hash,
17582 die_eq,
17583 NULL,
17584 &cu->comp_unit_obstack,
17585 hashtab_obstack_allocate,
17586 dummy_obstack_deallocate);
348e048f 17587
dee91e82
DE
17588 if (has_children)
17589 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17590 &info_ptr, comp_unit_die);
17591 cu->dies = comp_unit_die;
17592 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17593
17594 /* We try not to read any attributes in this function, because not
9cdd5dbd 17595 all CUs needed for references have been loaded yet, and symbol
348e048f 17596 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17597 or we won't be able to build types correctly.
17598 Similarly, if we do not read the producer, we can not apply
17599 producer-specific interpretation. */
95554aad 17600 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17601}
348e048f 17602
3019eac3
DE
17603/* Read in a signatured type and build its CU and DIEs.
17604 If the type is a stub for the real type in a DWO file,
17605 read in the real type from the DWO file as well. */
dee91e82
DE
17606
17607static void
17608read_signatured_type (struct signatured_type *sig_type)
17609{
17610 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17611
3019eac3 17612 gdb_assert (per_cu->is_debug_types);
dee91e82 17613 gdb_assert (per_cu->cu == NULL);
348e048f 17614
f4dc4d17
DE
17615 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17616 read_signatured_type_reader, NULL);
c906108c
SS
17617}
17618
c906108c
SS
17619/* Decode simple location descriptions.
17620 Given a pointer to a dwarf block that defines a location, compute
17621 the location and return the value.
17622
4cecd739
DJ
17623 NOTE drow/2003-11-18: This function is called in two situations
17624 now: for the address of static or global variables (partial symbols
17625 only) and for offsets into structures which are expected to be
17626 (more or less) constant. The partial symbol case should go away,
17627 and only the constant case should remain. That will let this
17628 function complain more accurately. A few special modes are allowed
17629 without complaint for global variables (for instance, global
17630 register values and thread-local values).
c906108c
SS
17631
17632 A location description containing no operations indicates that the
4cecd739 17633 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17634 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17635 callers will only want a very basic result and this can become a
21ae7a4d
JK
17636 complaint.
17637
17638 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17639
17640static CORE_ADDR
e7c27a73 17641decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17642{
e7c27a73 17643 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17644 size_t i;
17645 size_t size = blk->size;
21ae7a4d
JK
17646 gdb_byte *data = blk->data;
17647 CORE_ADDR stack[64];
17648 int stacki;
17649 unsigned int bytes_read, unsnd;
17650 gdb_byte op;
c906108c 17651
21ae7a4d
JK
17652 i = 0;
17653 stacki = 0;
17654 stack[stacki] = 0;
17655 stack[++stacki] = 0;
17656
17657 while (i < size)
17658 {
17659 op = data[i++];
17660 switch (op)
17661 {
17662 case DW_OP_lit0:
17663 case DW_OP_lit1:
17664 case DW_OP_lit2:
17665 case DW_OP_lit3:
17666 case DW_OP_lit4:
17667 case DW_OP_lit5:
17668 case DW_OP_lit6:
17669 case DW_OP_lit7:
17670 case DW_OP_lit8:
17671 case DW_OP_lit9:
17672 case DW_OP_lit10:
17673 case DW_OP_lit11:
17674 case DW_OP_lit12:
17675 case DW_OP_lit13:
17676 case DW_OP_lit14:
17677 case DW_OP_lit15:
17678 case DW_OP_lit16:
17679 case DW_OP_lit17:
17680 case DW_OP_lit18:
17681 case DW_OP_lit19:
17682 case DW_OP_lit20:
17683 case DW_OP_lit21:
17684 case DW_OP_lit22:
17685 case DW_OP_lit23:
17686 case DW_OP_lit24:
17687 case DW_OP_lit25:
17688 case DW_OP_lit26:
17689 case DW_OP_lit27:
17690 case DW_OP_lit28:
17691 case DW_OP_lit29:
17692 case DW_OP_lit30:
17693 case DW_OP_lit31:
17694 stack[++stacki] = op - DW_OP_lit0;
17695 break;
f1bea926 17696
21ae7a4d
JK
17697 case DW_OP_reg0:
17698 case DW_OP_reg1:
17699 case DW_OP_reg2:
17700 case DW_OP_reg3:
17701 case DW_OP_reg4:
17702 case DW_OP_reg5:
17703 case DW_OP_reg6:
17704 case DW_OP_reg7:
17705 case DW_OP_reg8:
17706 case DW_OP_reg9:
17707 case DW_OP_reg10:
17708 case DW_OP_reg11:
17709 case DW_OP_reg12:
17710 case DW_OP_reg13:
17711 case DW_OP_reg14:
17712 case DW_OP_reg15:
17713 case DW_OP_reg16:
17714 case DW_OP_reg17:
17715 case DW_OP_reg18:
17716 case DW_OP_reg19:
17717 case DW_OP_reg20:
17718 case DW_OP_reg21:
17719 case DW_OP_reg22:
17720 case DW_OP_reg23:
17721 case DW_OP_reg24:
17722 case DW_OP_reg25:
17723 case DW_OP_reg26:
17724 case DW_OP_reg27:
17725 case DW_OP_reg28:
17726 case DW_OP_reg29:
17727 case DW_OP_reg30:
17728 case DW_OP_reg31:
17729 stack[++stacki] = op - DW_OP_reg0;
17730 if (i < size)
17731 dwarf2_complex_location_expr_complaint ();
17732 break;
c906108c 17733
21ae7a4d
JK
17734 case DW_OP_regx:
17735 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17736 i += bytes_read;
17737 stack[++stacki] = unsnd;
17738 if (i < size)
17739 dwarf2_complex_location_expr_complaint ();
17740 break;
c906108c 17741
21ae7a4d
JK
17742 case DW_OP_addr:
17743 stack[++stacki] = read_address (objfile->obfd, &data[i],
17744 cu, &bytes_read);
17745 i += bytes_read;
17746 break;
d53d4ac5 17747
21ae7a4d
JK
17748 case DW_OP_const1u:
17749 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17750 i += 1;
17751 break;
17752
17753 case DW_OP_const1s:
17754 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17755 i += 1;
17756 break;
17757
17758 case DW_OP_const2u:
17759 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17760 i += 2;
17761 break;
17762
17763 case DW_OP_const2s:
17764 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17765 i += 2;
17766 break;
d53d4ac5 17767
21ae7a4d
JK
17768 case DW_OP_const4u:
17769 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17770 i += 4;
17771 break;
17772
17773 case DW_OP_const4s:
17774 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17775 i += 4;
17776 break;
17777
585861ea
JK
17778 case DW_OP_const8u:
17779 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17780 i += 8;
17781 break;
17782
21ae7a4d
JK
17783 case DW_OP_constu:
17784 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17785 &bytes_read);
17786 i += bytes_read;
17787 break;
17788
17789 case DW_OP_consts:
17790 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17791 i += bytes_read;
17792 break;
17793
17794 case DW_OP_dup:
17795 stack[stacki + 1] = stack[stacki];
17796 stacki++;
17797 break;
17798
17799 case DW_OP_plus:
17800 stack[stacki - 1] += stack[stacki];
17801 stacki--;
17802 break;
17803
17804 case DW_OP_plus_uconst:
17805 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17806 &bytes_read);
17807 i += bytes_read;
17808 break;
17809
17810 case DW_OP_minus:
17811 stack[stacki - 1] -= stack[stacki];
17812 stacki--;
17813 break;
17814
17815 case DW_OP_deref:
17816 /* If we're not the last op, then we definitely can't encode
17817 this using GDB's address_class enum. This is valid for partial
17818 global symbols, although the variable's address will be bogus
17819 in the psymtab. */
17820 if (i < size)
17821 dwarf2_complex_location_expr_complaint ();
17822 break;
17823
17824 case DW_OP_GNU_push_tls_address:
17825 /* The top of the stack has the offset from the beginning
17826 of the thread control block at which the variable is located. */
17827 /* Nothing should follow this operator, so the top of stack would
17828 be returned. */
17829 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17830 address will be bogus in the psymtab. Make it always at least
17831 non-zero to not look as a variable garbage collected by linker
17832 which have DW_OP_addr 0. */
21ae7a4d
JK
17833 if (i < size)
17834 dwarf2_complex_location_expr_complaint ();
585861ea 17835 stack[stacki]++;
21ae7a4d
JK
17836 break;
17837
17838 case DW_OP_GNU_uninit:
17839 break;
17840
3019eac3 17841 case DW_OP_GNU_addr_index:
49f6c839 17842 case DW_OP_GNU_const_index:
3019eac3
DE
17843 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17844 &bytes_read);
17845 i += bytes_read;
17846 break;
17847
21ae7a4d
JK
17848 default:
17849 {
f39c6ffd 17850 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17851
17852 if (name)
17853 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17854 name);
17855 else
17856 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17857 op);
17858 }
17859
17860 return (stack[stacki]);
d53d4ac5 17861 }
3c6e0cb3 17862
21ae7a4d
JK
17863 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17864 outside of the allocated space. Also enforce minimum>0. */
17865 if (stacki >= ARRAY_SIZE (stack) - 1)
17866 {
17867 complaint (&symfile_complaints,
17868 _("location description stack overflow"));
17869 return 0;
17870 }
17871
17872 if (stacki <= 0)
17873 {
17874 complaint (&symfile_complaints,
17875 _("location description stack underflow"));
17876 return 0;
17877 }
17878 }
17879 return (stack[stacki]);
c906108c
SS
17880}
17881
17882/* memory allocation interface */
17883
c906108c 17884static struct dwarf_block *
7b5a2f43 17885dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17886{
17887 struct dwarf_block *blk;
17888
17889 blk = (struct dwarf_block *)
7b5a2f43 17890 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17891 return (blk);
17892}
17893
c906108c 17894static struct die_info *
b60c80d6 17895dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17896{
17897 struct die_info *die;
b60c80d6
DJ
17898 size_t size = sizeof (struct die_info);
17899
17900 if (num_attrs > 1)
17901 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17902
b60c80d6 17903 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17904 memset (die, 0, sizeof (struct die_info));
17905 return (die);
17906}
2e276125
JB
17907
17908\f
17909/* Macro support. */
17910
2e276125
JB
17911/* Return the full name of file number I in *LH's file name table.
17912 Use COMP_DIR as the name of the current directory of the
17913 compilation. The result is allocated using xmalloc; the caller is
17914 responsible for freeing it. */
17915static char *
17916file_full_name (int file, struct line_header *lh, const char *comp_dir)
17917{
6a83a1e6
EZ
17918 /* Is the file number a valid index into the line header's file name
17919 table? Remember that file numbers start with one, not zero. */
17920 if (1 <= file && file <= lh->num_file_names)
17921 {
17922 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17923
6a83a1e6
EZ
17924 if (IS_ABSOLUTE_PATH (fe->name))
17925 return xstrdup (fe->name);
17926 else
17927 {
17928 const char *dir;
17929 int dir_len;
17930 char *full_name;
17931
17932 if (fe->dir_index)
17933 dir = lh->include_dirs[fe->dir_index - 1];
17934 else
17935 dir = comp_dir;
17936
17937 if (dir)
17938 {
17939 dir_len = strlen (dir);
17940 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17941 strcpy (full_name, dir);
17942 full_name[dir_len] = '/';
17943 strcpy (full_name + dir_len + 1, fe->name);
17944 return full_name;
17945 }
17946 else
17947 return xstrdup (fe->name);
17948 }
17949 }
2e276125
JB
17950 else
17951 {
6a83a1e6
EZ
17952 /* The compiler produced a bogus file number. We can at least
17953 record the macro definitions made in the file, even if we
17954 won't be able to find the file by name. */
17955 char fake_name[80];
9a619af0 17956
8c042590
PM
17957 xsnprintf (fake_name, sizeof (fake_name),
17958 "<bad macro file number %d>", file);
2e276125 17959
6e70227d 17960 complaint (&symfile_complaints,
6a83a1e6
EZ
17961 _("bad file number in macro information (%d)"),
17962 file);
2e276125 17963
6a83a1e6 17964 return xstrdup (fake_name);
2e276125
JB
17965 }
17966}
17967
17968
17969static struct macro_source_file *
17970macro_start_file (int file, int line,
17971 struct macro_source_file *current_file,
17972 const char *comp_dir,
17973 struct line_header *lh, struct objfile *objfile)
17974{
17975 /* The full name of this source file. */
17976 char *full_name = file_full_name (file, lh, comp_dir);
17977
17978 /* We don't create a macro table for this compilation unit
17979 at all until we actually get a filename. */
17980 if (! pending_macros)
6532ff36
TT
17981 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17982 objfile->per_bfd->macro_cache);
2e276125
JB
17983
17984 if (! current_file)
abc9d0dc
TT
17985 {
17986 /* If we have no current file, then this must be the start_file
17987 directive for the compilation unit's main source file. */
17988 current_file = macro_set_main (pending_macros, full_name);
17989 macro_define_special (pending_macros);
17990 }
2e276125
JB
17991 else
17992 current_file = macro_include (current_file, line, full_name);
17993
17994 xfree (full_name);
6e70227d 17995
2e276125
JB
17996 return current_file;
17997}
17998
17999
18000/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18001 followed by a null byte. */
18002static char *
18003copy_string (const char *buf, int len)
18004{
18005 char *s = xmalloc (len + 1);
9a619af0 18006
2e276125
JB
18007 memcpy (s, buf, len);
18008 s[len] = '\0';
2e276125
JB
18009 return s;
18010}
18011
18012
18013static const char *
18014consume_improper_spaces (const char *p, const char *body)
18015{
18016 if (*p == ' ')
18017 {
4d3c2250 18018 complaint (&symfile_complaints,
3e43a32a
MS
18019 _("macro definition contains spaces "
18020 "in formal argument list:\n`%s'"),
4d3c2250 18021 body);
2e276125
JB
18022
18023 while (*p == ' ')
18024 p++;
18025 }
18026
18027 return p;
18028}
18029
18030
18031static void
18032parse_macro_definition (struct macro_source_file *file, int line,
18033 const char *body)
18034{
18035 const char *p;
18036
18037 /* The body string takes one of two forms. For object-like macro
18038 definitions, it should be:
18039
18040 <macro name> " " <definition>
18041
18042 For function-like macro definitions, it should be:
18043
18044 <macro name> "() " <definition>
18045 or
18046 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18047
18048 Spaces may appear only where explicitly indicated, and in the
18049 <definition>.
18050
18051 The Dwarf 2 spec says that an object-like macro's name is always
18052 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18053 the space when the macro's definition is the empty string.
2e276125
JB
18054
18055 The Dwarf 2 spec says that there should be no spaces between the
18056 formal arguments in a function-like macro's formal argument list,
18057 but versions of GCC around March 2002 include spaces after the
18058 commas. */
18059
18060
18061 /* Find the extent of the macro name. The macro name is terminated
18062 by either a space or null character (for an object-like macro) or
18063 an opening paren (for a function-like macro). */
18064 for (p = body; *p; p++)
18065 if (*p == ' ' || *p == '(')
18066 break;
18067
18068 if (*p == ' ' || *p == '\0')
18069 {
18070 /* It's an object-like macro. */
18071 int name_len = p - body;
18072 char *name = copy_string (body, name_len);
18073 const char *replacement;
18074
18075 if (*p == ' ')
18076 replacement = body + name_len + 1;
18077 else
18078 {
4d3c2250 18079 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18080 replacement = body + name_len;
18081 }
6e70227d 18082
2e276125
JB
18083 macro_define_object (file, line, name, replacement);
18084
18085 xfree (name);
18086 }
18087 else if (*p == '(')
18088 {
18089 /* It's a function-like macro. */
18090 char *name = copy_string (body, p - body);
18091 int argc = 0;
18092 int argv_size = 1;
18093 char **argv = xmalloc (argv_size * sizeof (*argv));
18094
18095 p++;
18096
18097 p = consume_improper_spaces (p, body);
18098
18099 /* Parse the formal argument list. */
18100 while (*p && *p != ')')
18101 {
18102 /* Find the extent of the current argument name. */
18103 const char *arg_start = p;
18104
18105 while (*p && *p != ',' && *p != ')' && *p != ' ')
18106 p++;
18107
18108 if (! *p || p == arg_start)
4d3c2250 18109 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18110 else
18111 {
18112 /* Make sure argv has room for the new argument. */
18113 if (argc >= argv_size)
18114 {
18115 argv_size *= 2;
18116 argv = xrealloc (argv, argv_size * sizeof (*argv));
18117 }
18118
18119 argv[argc++] = copy_string (arg_start, p - arg_start);
18120 }
18121
18122 p = consume_improper_spaces (p, body);
18123
18124 /* Consume the comma, if present. */
18125 if (*p == ',')
18126 {
18127 p++;
18128
18129 p = consume_improper_spaces (p, body);
18130 }
18131 }
18132
18133 if (*p == ')')
18134 {
18135 p++;
18136
18137 if (*p == ' ')
18138 /* Perfectly formed definition, no complaints. */
18139 macro_define_function (file, line, name,
6e70227d 18140 argc, (const char **) argv,
2e276125
JB
18141 p + 1);
18142 else if (*p == '\0')
18143 {
18144 /* Complain, but do define it. */
4d3c2250 18145 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18146 macro_define_function (file, line, name,
6e70227d 18147 argc, (const char **) argv,
2e276125
JB
18148 p);
18149 }
18150 else
18151 /* Just complain. */
4d3c2250 18152 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18153 }
18154 else
18155 /* Just complain. */
4d3c2250 18156 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18157
18158 xfree (name);
18159 {
18160 int i;
18161
18162 for (i = 0; i < argc; i++)
18163 xfree (argv[i]);
18164 }
18165 xfree (argv);
18166 }
18167 else
4d3c2250 18168 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18169}
18170
cf2c3c16
TT
18171/* Skip some bytes from BYTES according to the form given in FORM.
18172 Returns the new pointer. */
2e276125 18173
cf2c3c16 18174static gdb_byte *
f664829e 18175skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18176 enum dwarf_form form,
18177 unsigned int offset_size,
18178 struct dwarf2_section_info *section)
2e276125 18179{
cf2c3c16 18180 unsigned int bytes_read;
2e276125 18181
cf2c3c16 18182 switch (form)
2e276125 18183 {
cf2c3c16
TT
18184 case DW_FORM_data1:
18185 case DW_FORM_flag:
18186 ++bytes;
18187 break;
18188
18189 case DW_FORM_data2:
18190 bytes += 2;
18191 break;
18192
18193 case DW_FORM_data4:
18194 bytes += 4;
18195 break;
18196
18197 case DW_FORM_data8:
18198 bytes += 8;
18199 break;
18200
18201 case DW_FORM_string:
18202 read_direct_string (abfd, bytes, &bytes_read);
18203 bytes += bytes_read;
18204 break;
18205
18206 case DW_FORM_sec_offset:
18207 case DW_FORM_strp:
36586728 18208 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18209 bytes += offset_size;
18210 break;
18211
18212 case DW_FORM_block:
18213 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18214 bytes += bytes_read;
18215 break;
18216
18217 case DW_FORM_block1:
18218 bytes += 1 + read_1_byte (abfd, bytes);
18219 break;
18220 case DW_FORM_block2:
18221 bytes += 2 + read_2_bytes (abfd, bytes);
18222 break;
18223 case DW_FORM_block4:
18224 bytes += 4 + read_4_bytes (abfd, bytes);
18225 break;
18226
18227 case DW_FORM_sdata:
18228 case DW_FORM_udata:
3019eac3
DE
18229 case DW_FORM_GNU_addr_index:
18230 case DW_FORM_GNU_str_index:
f664829e
DE
18231 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18232 if (bytes == NULL)
18233 {
18234 dwarf2_section_buffer_overflow_complaint (section);
18235 return NULL;
18236 }
cf2c3c16
TT
18237 break;
18238
18239 default:
18240 {
18241 complain:
18242 complaint (&symfile_complaints,
18243 _("invalid form 0x%x in `%s'"),
18244 form,
18245 section->asection->name);
18246 return NULL;
18247 }
2e276125
JB
18248 }
18249
cf2c3c16
TT
18250 return bytes;
18251}
757a13d0 18252
cf2c3c16
TT
18253/* A helper for dwarf_decode_macros that handles skipping an unknown
18254 opcode. Returns an updated pointer to the macro data buffer; or,
18255 on error, issues a complaint and returns NULL. */
757a13d0 18256
cf2c3c16
TT
18257static gdb_byte *
18258skip_unknown_opcode (unsigned int opcode,
18259 gdb_byte **opcode_definitions,
f664829e 18260 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18261 bfd *abfd,
18262 unsigned int offset_size,
18263 struct dwarf2_section_info *section)
18264{
18265 unsigned int bytes_read, i;
18266 unsigned long arg;
18267 gdb_byte *defn;
2e276125 18268
cf2c3c16 18269 if (opcode_definitions[opcode] == NULL)
2e276125 18270 {
cf2c3c16
TT
18271 complaint (&symfile_complaints,
18272 _("unrecognized DW_MACFINO opcode 0x%x"),
18273 opcode);
18274 return NULL;
18275 }
2e276125 18276
cf2c3c16
TT
18277 defn = opcode_definitions[opcode];
18278 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18279 defn += bytes_read;
2e276125 18280
cf2c3c16
TT
18281 for (i = 0; i < arg; ++i)
18282 {
f664829e
DE
18283 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18284 section);
cf2c3c16
TT
18285 if (mac_ptr == NULL)
18286 {
18287 /* skip_form_bytes already issued the complaint. */
18288 return NULL;
18289 }
18290 }
757a13d0 18291
cf2c3c16
TT
18292 return mac_ptr;
18293}
757a13d0 18294
cf2c3c16
TT
18295/* A helper function which parses the header of a macro section.
18296 If the macro section is the extended (for now called "GNU") type,
18297 then this updates *OFFSET_SIZE. Returns a pointer to just after
18298 the header, or issues a complaint and returns NULL on error. */
757a13d0 18299
cf2c3c16
TT
18300static gdb_byte *
18301dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18302 bfd *abfd,
18303 gdb_byte *mac_ptr,
18304 unsigned int *offset_size,
18305 int section_is_gnu)
18306{
18307 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18308
cf2c3c16
TT
18309 if (section_is_gnu)
18310 {
18311 unsigned int version, flags;
757a13d0 18312
cf2c3c16
TT
18313 version = read_2_bytes (abfd, mac_ptr);
18314 if (version != 4)
18315 {
18316 complaint (&symfile_complaints,
18317 _("unrecognized version `%d' in .debug_macro section"),
18318 version);
18319 return NULL;
18320 }
18321 mac_ptr += 2;
757a13d0 18322
cf2c3c16
TT
18323 flags = read_1_byte (abfd, mac_ptr);
18324 ++mac_ptr;
18325 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18326
cf2c3c16
TT
18327 if ((flags & 2) != 0)
18328 /* We don't need the line table offset. */
18329 mac_ptr += *offset_size;
757a13d0 18330
cf2c3c16
TT
18331 /* Vendor opcode descriptions. */
18332 if ((flags & 4) != 0)
18333 {
18334 unsigned int i, count;
757a13d0 18335
cf2c3c16
TT
18336 count = read_1_byte (abfd, mac_ptr);
18337 ++mac_ptr;
18338 for (i = 0; i < count; ++i)
18339 {
18340 unsigned int opcode, bytes_read;
18341 unsigned long arg;
18342
18343 opcode = read_1_byte (abfd, mac_ptr);
18344 ++mac_ptr;
18345 opcode_definitions[opcode] = mac_ptr;
18346 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18347 mac_ptr += bytes_read;
18348 mac_ptr += arg;
18349 }
757a13d0 18350 }
cf2c3c16 18351 }
757a13d0 18352
cf2c3c16
TT
18353 return mac_ptr;
18354}
757a13d0 18355
cf2c3c16 18356/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18357 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18358
18359static void
18360dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18361 struct macro_source_file *current_file,
18362 struct line_header *lh, char *comp_dir,
18363 struct dwarf2_section_info *section,
36586728 18364 int section_is_gnu, int section_is_dwz,
cf2c3c16 18365 unsigned int offset_size,
8fc3fc34
TT
18366 struct objfile *objfile,
18367 htab_t include_hash)
cf2c3c16
TT
18368{
18369 enum dwarf_macro_record_type macinfo_type;
18370 int at_commandline;
18371 gdb_byte *opcode_definitions[256];
757a13d0 18372
cf2c3c16
TT
18373 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18374 &offset_size, section_is_gnu);
18375 if (mac_ptr == NULL)
18376 {
18377 /* We already issued a complaint. */
18378 return;
18379 }
757a13d0
JK
18380
18381 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18382 GDB is still reading the definitions from command line. First
18383 DW_MACINFO_start_file will need to be ignored as it was already executed
18384 to create CURRENT_FILE for the main source holding also the command line
18385 definitions. On first met DW_MACINFO_start_file this flag is reset to
18386 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18387
18388 at_commandline = 1;
18389
18390 do
18391 {
18392 /* Do we at least have room for a macinfo type byte? */
18393 if (mac_ptr >= mac_end)
18394 {
f664829e 18395 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18396 break;
18397 }
18398
18399 macinfo_type = read_1_byte (abfd, mac_ptr);
18400 mac_ptr++;
18401
cf2c3c16
TT
18402 /* Note that we rely on the fact that the corresponding GNU and
18403 DWARF constants are the same. */
757a13d0
JK
18404 switch (macinfo_type)
18405 {
18406 /* A zero macinfo type indicates the end of the macro
18407 information. */
18408 case 0:
18409 break;
2e276125 18410
cf2c3c16
TT
18411 case DW_MACRO_GNU_define:
18412 case DW_MACRO_GNU_undef:
18413 case DW_MACRO_GNU_define_indirect:
18414 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18415 case DW_MACRO_GNU_define_indirect_alt:
18416 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18417 {
891d2f0b 18418 unsigned int bytes_read;
2e276125
JB
18419 int line;
18420 char *body;
cf2c3c16 18421 int is_define;
2e276125 18422
cf2c3c16
TT
18423 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18424 mac_ptr += bytes_read;
18425
18426 if (macinfo_type == DW_MACRO_GNU_define
18427 || macinfo_type == DW_MACRO_GNU_undef)
18428 {
18429 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18430 mac_ptr += bytes_read;
18431 }
18432 else
18433 {
18434 LONGEST str_offset;
18435
18436 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18437 mac_ptr += offset_size;
2e276125 18438
36586728 18439 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18440 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18441 || section_is_dwz)
36586728
TT
18442 {
18443 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18444
18445 body = read_indirect_string_from_dwz (dwz, str_offset);
18446 }
18447 else
18448 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18449 }
18450
18451 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18452 || macinfo_type == DW_MACRO_GNU_define_indirect
18453 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18454 if (! current_file)
757a13d0
JK
18455 {
18456 /* DWARF violation as no main source is present. */
18457 complaint (&symfile_complaints,
18458 _("debug info with no main source gives macro %s "
18459 "on line %d: %s"),
cf2c3c16
TT
18460 is_define ? _("definition") : _("undefinition"),
18461 line, body);
757a13d0
JK
18462 break;
18463 }
3e43a32a
MS
18464 if ((line == 0 && !at_commandline)
18465 || (line != 0 && at_commandline))
4d3c2250 18466 complaint (&symfile_complaints,
757a13d0
JK
18467 _("debug info gives %s macro %s with %s line %d: %s"),
18468 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18469 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18470 line == 0 ? _("zero") : _("non-zero"), line, body);
18471
cf2c3c16 18472 if (is_define)
757a13d0 18473 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18474 else
18475 {
18476 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18477 || macinfo_type == DW_MACRO_GNU_undef_indirect
18478 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18479 macro_undef (current_file, line, body);
18480 }
2e276125
JB
18481 }
18482 break;
18483
cf2c3c16 18484 case DW_MACRO_GNU_start_file:
2e276125 18485 {
891d2f0b 18486 unsigned int bytes_read;
2e276125
JB
18487 int line, file;
18488
18489 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18490 mac_ptr += bytes_read;
18491 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18492 mac_ptr += bytes_read;
18493
3e43a32a
MS
18494 if ((line == 0 && !at_commandline)
18495 || (line != 0 && at_commandline))
757a13d0
JK
18496 complaint (&symfile_complaints,
18497 _("debug info gives source %d included "
18498 "from %s at %s line %d"),
18499 file, at_commandline ? _("command-line") : _("file"),
18500 line == 0 ? _("zero") : _("non-zero"), line);
18501
18502 if (at_commandline)
18503 {
cf2c3c16
TT
18504 /* This DW_MACRO_GNU_start_file was executed in the
18505 pass one. */
757a13d0
JK
18506 at_commandline = 0;
18507 }
18508 else
18509 current_file = macro_start_file (file, line,
18510 current_file, comp_dir,
cf2c3c16 18511 lh, objfile);
2e276125
JB
18512 }
18513 break;
18514
cf2c3c16 18515 case DW_MACRO_GNU_end_file:
2e276125 18516 if (! current_file)
4d3c2250 18517 complaint (&symfile_complaints,
3e43a32a
MS
18518 _("macro debug info has an unmatched "
18519 "`close_file' directive"));
2e276125
JB
18520 else
18521 {
18522 current_file = current_file->included_by;
18523 if (! current_file)
18524 {
cf2c3c16 18525 enum dwarf_macro_record_type next_type;
2e276125
JB
18526
18527 /* GCC circa March 2002 doesn't produce the zero
18528 type byte marking the end of the compilation
18529 unit. Complain if it's not there, but exit no
18530 matter what. */
18531
18532 /* Do we at least have room for a macinfo type byte? */
18533 if (mac_ptr >= mac_end)
18534 {
f664829e 18535 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18536 return;
18537 }
18538
18539 /* We don't increment mac_ptr here, so this is just
18540 a look-ahead. */
18541 next_type = read_1_byte (abfd, mac_ptr);
18542 if (next_type != 0)
4d3c2250 18543 complaint (&symfile_complaints,
3e43a32a
MS
18544 _("no terminating 0-type entry for "
18545 "macros in `.debug_macinfo' section"));
2e276125
JB
18546
18547 return;
18548 }
18549 }
18550 break;
18551
cf2c3c16 18552 case DW_MACRO_GNU_transparent_include:
36586728 18553 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18554 {
18555 LONGEST offset;
8fc3fc34 18556 void **slot;
a036ba48
TT
18557 bfd *include_bfd = abfd;
18558 struct dwarf2_section_info *include_section = section;
18559 struct dwarf2_section_info alt_section;
18560 gdb_byte *include_mac_end = mac_end;
18561 int is_dwz = section_is_dwz;
18562 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18563
18564 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18565 mac_ptr += offset_size;
18566
a036ba48
TT
18567 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18568 {
18569 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18570
18571 dwarf2_read_section (dwarf2_per_objfile->objfile,
18572 &dwz->macro);
18573
18574 include_bfd = dwz->macro.asection->owner;
18575 include_section = &dwz->macro;
18576 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18577 is_dwz = 1;
18578 }
18579
18580 new_mac_ptr = include_section->buffer + offset;
18581 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18582
8fc3fc34
TT
18583 if (*slot != NULL)
18584 {
18585 /* This has actually happened; see
18586 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18587 complaint (&symfile_complaints,
18588 _("recursive DW_MACRO_GNU_transparent_include in "
18589 ".debug_macro section"));
18590 }
18591 else
18592 {
a036ba48 18593 *slot = new_mac_ptr;
36586728 18594
a036ba48 18595 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18596 include_mac_end, current_file,
8fc3fc34 18597 lh, comp_dir,
36586728 18598 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18599 offset_size, objfile, include_hash);
18600
a036ba48 18601 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18602 }
cf2c3c16
TT
18603 }
18604 break;
18605
2e276125 18606 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18607 if (!section_is_gnu)
18608 {
18609 unsigned int bytes_read;
18610 int constant;
2e276125 18611
cf2c3c16
TT
18612 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18613 mac_ptr += bytes_read;
18614 read_direct_string (abfd, mac_ptr, &bytes_read);
18615 mac_ptr += bytes_read;
2e276125 18616
cf2c3c16
TT
18617 /* We don't recognize any vendor extensions. */
18618 break;
18619 }
18620 /* FALLTHROUGH */
18621
18622 default:
18623 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18624 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18625 section);
18626 if (mac_ptr == NULL)
18627 return;
18628 break;
2e276125 18629 }
757a13d0 18630 } while (macinfo_type != 0);
2e276125 18631}
8e19ed76 18632
cf2c3c16 18633static void
09262596
DE
18634dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18635 char *comp_dir, int section_is_gnu)
cf2c3c16 18636{
bb5ed363 18637 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18638 struct line_header *lh = cu->line_header;
18639 bfd *abfd;
cf2c3c16
TT
18640 gdb_byte *mac_ptr, *mac_end;
18641 struct macro_source_file *current_file = 0;
18642 enum dwarf_macro_record_type macinfo_type;
18643 unsigned int offset_size = cu->header.offset_size;
18644 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18645 struct cleanup *cleanup;
18646 htab_t include_hash;
18647 void **slot;
09262596
DE
18648 struct dwarf2_section_info *section;
18649 const char *section_name;
18650
18651 if (cu->dwo_unit != NULL)
18652 {
18653 if (section_is_gnu)
18654 {
18655 section = &cu->dwo_unit->dwo_file->sections.macro;
18656 section_name = ".debug_macro.dwo";
18657 }
18658 else
18659 {
18660 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18661 section_name = ".debug_macinfo.dwo";
18662 }
18663 }
18664 else
18665 {
18666 if (section_is_gnu)
18667 {
18668 section = &dwarf2_per_objfile->macro;
18669 section_name = ".debug_macro";
18670 }
18671 else
18672 {
18673 section = &dwarf2_per_objfile->macinfo;
18674 section_name = ".debug_macinfo";
18675 }
18676 }
cf2c3c16 18677
bb5ed363 18678 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18679 if (section->buffer == NULL)
18680 {
fceca515 18681 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18682 return;
18683 }
09262596 18684 abfd = section->asection->owner;
cf2c3c16
TT
18685
18686 /* First pass: Find the name of the base filename.
18687 This filename is needed in order to process all macros whose definition
18688 (or undefinition) comes from the command line. These macros are defined
18689 before the first DW_MACINFO_start_file entry, and yet still need to be
18690 associated to the base file.
18691
18692 To determine the base file name, we scan the macro definitions until we
18693 reach the first DW_MACINFO_start_file entry. We then initialize
18694 CURRENT_FILE accordingly so that any macro definition found before the
18695 first DW_MACINFO_start_file can still be associated to the base file. */
18696
18697 mac_ptr = section->buffer + offset;
18698 mac_end = section->buffer + section->size;
18699
18700 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18701 &offset_size, section_is_gnu);
18702 if (mac_ptr == NULL)
18703 {
18704 /* We already issued a complaint. */
18705 return;
18706 }
18707
18708 do
18709 {
18710 /* Do we at least have room for a macinfo type byte? */
18711 if (mac_ptr >= mac_end)
18712 {
18713 /* Complaint is printed during the second pass as GDB will probably
18714 stop the first pass earlier upon finding
18715 DW_MACINFO_start_file. */
18716 break;
18717 }
18718
18719 macinfo_type = read_1_byte (abfd, mac_ptr);
18720 mac_ptr++;
18721
18722 /* Note that we rely on the fact that the corresponding GNU and
18723 DWARF constants are the same. */
18724 switch (macinfo_type)
18725 {
18726 /* A zero macinfo type indicates the end of the macro
18727 information. */
18728 case 0:
18729 break;
18730
18731 case DW_MACRO_GNU_define:
18732 case DW_MACRO_GNU_undef:
18733 /* Only skip the data by MAC_PTR. */
18734 {
18735 unsigned int bytes_read;
18736
18737 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18738 mac_ptr += bytes_read;
18739 read_direct_string (abfd, mac_ptr, &bytes_read);
18740 mac_ptr += bytes_read;
18741 }
18742 break;
18743
18744 case DW_MACRO_GNU_start_file:
18745 {
18746 unsigned int bytes_read;
18747 int line, file;
18748
18749 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18750 mac_ptr += bytes_read;
18751 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18752 mac_ptr += bytes_read;
18753
18754 current_file = macro_start_file (file, line, current_file,
bb5ed363 18755 comp_dir, lh, objfile);
cf2c3c16
TT
18756 }
18757 break;
18758
18759 case DW_MACRO_GNU_end_file:
18760 /* No data to skip by MAC_PTR. */
18761 break;
18762
18763 case DW_MACRO_GNU_define_indirect:
18764 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18765 case DW_MACRO_GNU_define_indirect_alt:
18766 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18767 {
18768 unsigned int bytes_read;
18769
18770 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18771 mac_ptr += bytes_read;
18772 mac_ptr += offset_size;
18773 }
18774 break;
18775
18776 case DW_MACRO_GNU_transparent_include:
f7a35f02 18777 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18778 /* Note that, according to the spec, a transparent include
18779 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18780 skip this opcode. */
18781 mac_ptr += offset_size;
18782 break;
18783
18784 case DW_MACINFO_vendor_ext:
18785 /* Only skip the data by MAC_PTR. */
18786 if (!section_is_gnu)
18787 {
18788 unsigned int bytes_read;
18789
18790 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18791 mac_ptr += bytes_read;
18792 read_direct_string (abfd, mac_ptr, &bytes_read);
18793 mac_ptr += bytes_read;
18794 }
18795 /* FALLTHROUGH */
18796
18797 default:
18798 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18799 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18800 section);
18801 if (mac_ptr == NULL)
18802 return;
18803 break;
18804 }
18805 } while (macinfo_type != 0 && current_file == NULL);
18806
18807 /* Second pass: Process all entries.
18808
18809 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18810 command-line macro definitions/undefinitions. This flag is unset when we
18811 reach the first DW_MACINFO_start_file entry. */
18812
8fc3fc34
TT
18813 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18814 NULL, xcalloc, xfree);
18815 cleanup = make_cleanup_htab_delete (include_hash);
18816 mac_ptr = section->buffer + offset;
18817 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18818 *slot = mac_ptr;
18819 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18820 current_file, lh, comp_dir, section,
18821 section_is_gnu, 0,
8fc3fc34
TT
18822 offset_size, objfile, include_hash);
18823 do_cleanups (cleanup);
cf2c3c16
TT
18824}
18825
8e19ed76 18826/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18827 if so return true else false. */
380bca97 18828
8e19ed76
PS
18829static int
18830attr_form_is_block (struct attribute *attr)
18831{
18832 return (attr == NULL ? 0 :
18833 attr->form == DW_FORM_block1
18834 || attr->form == DW_FORM_block2
18835 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18836 || attr->form == DW_FORM_block
18837 || attr->form == DW_FORM_exprloc);
8e19ed76 18838}
4c2df51b 18839
c6a0999f
JB
18840/* Return non-zero if ATTR's value is a section offset --- classes
18841 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18842 You may use DW_UNSND (attr) to retrieve such offsets.
18843
18844 Section 7.5.4, "Attribute Encodings", explains that no attribute
18845 may have a value that belongs to more than one of these classes; it
18846 would be ambiguous if we did, because we use the same forms for all
18847 of them. */
380bca97 18848
3690dd37
JB
18849static int
18850attr_form_is_section_offset (struct attribute *attr)
18851{
18852 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18853 || attr->form == DW_FORM_data8
18854 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18855}
18856
3690dd37
JB
18857/* Return non-zero if ATTR's value falls in the 'constant' class, or
18858 zero otherwise. When this function returns true, you can apply
18859 dwarf2_get_attr_constant_value to it.
18860
18861 However, note that for some attributes you must check
18862 attr_form_is_section_offset before using this test. DW_FORM_data4
18863 and DW_FORM_data8 are members of both the constant class, and of
18864 the classes that contain offsets into other debug sections
18865 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18866 that, if an attribute's can be either a constant or one of the
18867 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18868 taken as section offsets, not constants. */
380bca97 18869
3690dd37
JB
18870static int
18871attr_form_is_constant (struct attribute *attr)
18872{
18873 switch (attr->form)
18874 {
18875 case DW_FORM_sdata:
18876 case DW_FORM_udata:
18877 case DW_FORM_data1:
18878 case DW_FORM_data2:
18879 case DW_FORM_data4:
18880 case DW_FORM_data8:
18881 return 1;
18882 default:
18883 return 0;
18884 }
18885}
18886
3019eac3
DE
18887/* Return the .debug_loc section to use for CU.
18888 For DWO files use .debug_loc.dwo. */
18889
18890static struct dwarf2_section_info *
18891cu_debug_loc_section (struct dwarf2_cu *cu)
18892{
18893 if (cu->dwo_unit)
18894 return &cu->dwo_unit->dwo_file->sections.loc;
18895 return &dwarf2_per_objfile->loc;
18896}
18897
8cf6f0b1
TT
18898/* A helper function that fills in a dwarf2_loclist_baton. */
18899
18900static void
18901fill_in_loclist_baton (struct dwarf2_cu *cu,
18902 struct dwarf2_loclist_baton *baton,
18903 struct attribute *attr)
18904{
3019eac3
DE
18905 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18906
18907 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18908
18909 baton->per_cu = cu->per_cu;
18910 gdb_assert (baton->per_cu);
18911 /* We don't know how long the location list is, but make sure we
18912 don't run off the edge of the section. */
3019eac3
DE
18913 baton->size = section->size - DW_UNSND (attr);
18914 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18915 baton->base_address = cu->base_address;
f664829e 18916 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18917}
18918
4c2df51b
DJ
18919static void
18920dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18921 struct dwarf2_cu *cu)
4c2df51b 18922{
bb5ed363 18923 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18924 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18925
3690dd37 18926 if (attr_form_is_section_offset (attr)
3019eac3 18927 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18928 the section. If so, fall through to the complaint in the
18929 other branch. */
3019eac3 18930 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18931 {
0d53c4c4 18932 struct dwarf2_loclist_baton *baton;
4c2df51b 18933
bb5ed363 18934 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18935 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18936
8cf6f0b1 18937 fill_in_loclist_baton (cu, baton, attr);
be391dca 18938
d00adf39 18939 if (cu->base_known == 0)
0d53c4c4 18940 complaint (&symfile_complaints,
3e43a32a
MS
18941 _("Location list used without "
18942 "specifying the CU base address."));
4c2df51b 18943
768a979c 18944 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18945 SYMBOL_LOCATION_BATON (sym) = baton;
18946 }
18947 else
18948 {
18949 struct dwarf2_locexpr_baton *baton;
18950
bb5ed363 18951 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18952 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18953 baton->per_cu = cu->per_cu;
18954 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18955
18956 if (attr_form_is_block (attr))
18957 {
18958 /* Note that we're just copying the block's data pointer
18959 here, not the actual data. We're still pointing into the
6502dd73
DJ
18960 info_buffer for SYM's objfile; right now we never release
18961 that buffer, but when we do clean up properly this may
18962 need to change. */
0d53c4c4
DJ
18963 baton->size = DW_BLOCK (attr)->size;
18964 baton->data = DW_BLOCK (attr)->data;
18965 }
18966 else
18967 {
18968 dwarf2_invalid_attrib_class_complaint ("location description",
18969 SYMBOL_NATURAL_NAME (sym));
18970 baton->size = 0;
0d53c4c4 18971 }
6e70227d 18972
768a979c 18973 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18974 SYMBOL_LOCATION_BATON (sym) = baton;
18975 }
4c2df51b 18976}
6502dd73 18977
9aa1f1e3
TT
18978/* Return the OBJFILE associated with the compilation unit CU. If CU
18979 came from a separate debuginfo file, then the master objfile is
18980 returned. */
ae0d2f24
UW
18981
18982struct objfile *
18983dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18984{
9291a0cd 18985 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18986
18987 /* Return the master objfile, so that we can report and look up the
18988 correct file containing this variable. */
18989 if (objfile->separate_debug_objfile_backlink)
18990 objfile = objfile->separate_debug_objfile_backlink;
18991
18992 return objfile;
18993}
18994
96408a79
SA
18995/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18996 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18997 CU_HEADERP first. */
18998
18999static const struct comp_unit_head *
19000per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19001 struct dwarf2_per_cu_data *per_cu)
19002{
96408a79
SA
19003 gdb_byte *info_ptr;
19004
19005 if (per_cu->cu)
19006 return &per_cu->cu->header;
19007
0bc3a05c 19008 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19009
19010 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19011 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19012
19013 return cu_headerp;
19014}
19015
ae0d2f24
UW
19016/* Return the address size given in the compilation unit header for CU. */
19017
98714339 19018int
ae0d2f24
UW
19019dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19020{
96408a79
SA
19021 struct comp_unit_head cu_header_local;
19022 const struct comp_unit_head *cu_headerp;
c471e790 19023
96408a79
SA
19024 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19025
19026 return cu_headerp->addr_size;
ae0d2f24
UW
19027}
19028
9eae7c52
TT
19029/* Return the offset size given in the compilation unit header for CU. */
19030
19031int
19032dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19033{
96408a79
SA
19034 struct comp_unit_head cu_header_local;
19035 const struct comp_unit_head *cu_headerp;
9c6c53f7 19036
96408a79
SA
19037 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19038
19039 return cu_headerp->offset_size;
19040}
19041
19042/* See its dwarf2loc.h declaration. */
19043
19044int
19045dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19046{
19047 struct comp_unit_head cu_header_local;
19048 const struct comp_unit_head *cu_headerp;
19049
19050 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19051
19052 if (cu_headerp->version == 2)
19053 return cu_headerp->addr_size;
19054 else
19055 return cu_headerp->offset_size;
181cebd4
JK
19056}
19057
9aa1f1e3
TT
19058/* Return the text offset of the CU. The returned offset comes from
19059 this CU's objfile. If this objfile came from a separate debuginfo
19060 file, then the offset may be different from the corresponding
19061 offset in the parent objfile. */
19062
19063CORE_ADDR
19064dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19065{
bb3fa9d0 19066 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19067
19068 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19069}
19070
348e048f
DE
19071/* Locate the .debug_info compilation unit from CU's objfile which contains
19072 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19073
19074static struct dwarf2_per_cu_data *
b64f50a1 19075dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19076 unsigned int offset_in_dwz,
ae038cb0
DJ
19077 struct objfile *objfile)
19078{
19079 struct dwarf2_per_cu_data *this_cu;
19080 int low, high;
36586728 19081 const sect_offset *cu_off;
ae038cb0 19082
ae038cb0
DJ
19083 low = 0;
19084 high = dwarf2_per_objfile->n_comp_units - 1;
19085 while (high > low)
19086 {
36586728 19087 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19088 int mid = low + (high - low) / 2;
9a619af0 19089
36586728
TT
19090 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19091 cu_off = &mid_cu->offset;
19092 if (mid_cu->is_dwz > offset_in_dwz
19093 || (mid_cu->is_dwz == offset_in_dwz
19094 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19095 high = mid;
19096 else
19097 low = mid + 1;
19098 }
19099 gdb_assert (low == high);
36586728
TT
19100 this_cu = dwarf2_per_objfile->all_comp_units[low];
19101 cu_off = &this_cu->offset;
19102 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19103 {
36586728 19104 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19105 error (_("Dwarf Error: could not find partial DIE containing "
19106 "offset 0x%lx [in module %s]"),
b64f50a1 19107 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19108
b64f50a1
JK
19109 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19110 <= offset.sect_off);
ae038cb0
DJ
19111 return dwarf2_per_objfile->all_comp_units[low-1];
19112 }
19113 else
19114 {
19115 this_cu = dwarf2_per_objfile->all_comp_units[low];
19116 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19117 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19118 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19119 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19120 return this_cu;
19121 }
19122}
19123
23745b47 19124/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19125
9816fde3 19126static void
23745b47 19127init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19128{
9816fde3 19129 memset (cu, 0, sizeof (*cu));
23745b47
DE
19130 per_cu->cu = cu;
19131 cu->per_cu = per_cu;
19132 cu->objfile = per_cu->objfile;
93311388 19133 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19134}
19135
19136/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19137
19138static void
95554aad
TT
19139prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19140 enum language pretend_language)
9816fde3
JK
19141{
19142 struct attribute *attr;
19143
19144 /* Set the language we're debugging. */
19145 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19146 if (attr)
19147 set_cu_language (DW_UNSND (attr), cu);
19148 else
9cded63f 19149 {
95554aad 19150 cu->language = pretend_language;
9cded63f
TT
19151 cu->language_defn = language_def (cu->language);
19152 }
dee91e82
DE
19153
19154 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19155 if (attr)
19156 cu->producer = DW_STRING (attr);
93311388
DE
19157}
19158
ae038cb0
DJ
19159/* Release one cached compilation unit, CU. We unlink it from the tree
19160 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19161 the caller is responsible for that.
19162 NOTE: DATA is a void * because this function is also used as a
19163 cleanup routine. */
ae038cb0
DJ
19164
19165static void
68dc6402 19166free_heap_comp_unit (void *data)
ae038cb0
DJ
19167{
19168 struct dwarf2_cu *cu = data;
19169
23745b47
DE
19170 gdb_assert (cu->per_cu != NULL);
19171 cu->per_cu->cu = NULL;
ae038cb0
DJ
19172 cu->per_cu = NULL;
19173
19174 obstack_free (&cu->comp_unit_obstack, NULL);
19175
19176 xfree (cu);
19177}
19178
72bf9492 19179/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19180 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19181 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19182
19183static void
19184free_stack_comp_unit (void *data)
19185{
19186 struct dwarf2_cu *cu = data;
19187
23745b47
DE
19188 gdb_assert (cu->per_cu != NULL);
19189 cu->per_cu->cu = NULL;
19190 cu->per_cu = NULL;
19191
72bf9492
DJ
19192 obstack_free (&cu->comp_unit_obstack, NULL);
19193 cu->partial_dies = NULL;
ae038cb0
DJ
19194}
19195
19196/* Free all cached compilation units. */
19197
19198static void
19199free_cached_comp_units (void *data)
19200{
19201 struct dwarf2_per_cu_data *per_cu, **last_chain;
19202
19203 per_cu = dwarf2_per_objfile->read_in_chain;
19204 last_chain = &dwarf2_per_objfile->read_in_chain;
19205 while (per_cu != NULL)
19206 {
19207 struct dwarf2_per_cu_data *next_cu;
19208
19209 next_cu = per_cu->cu->read_in_chain;
19210
68dc6402 19211 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19212 *last_chain = next_cu;
19213
19214 per_cu = next_cu;
19215 }
19216}
19217
19218/* Increase the age counter on each cached compilation unit, and free
19219 any that are too old. */
19220
19221static void
19222age_cached_comp_units (void)
19223{
19224 struct dwarf2_per_cu_data *per_cu, **last_chain;
19225
19226 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19227 per_cu = dwarf2_per_objfile->read_in_chain;
19228 while (per_cu != NULL)
19229 {
19230 per_cu->cu->last_used ++;
19231 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19232 dwarf2_mark (per_cu->cu);
19233 per_cu = per_cu->cu->read_in_chain;
19234 }
19235
19236 per_cu = dwarf2_per_objfile->read_in_chain;
19237 last_chain = &dwarf2_per_objfile->read_in_chain;
19238 while (per_cu != NULL)
19239 {
19240 struct dwarf2_per_cu_data *next_cu;
19241
19242 next_cu = per_cu->cu->read_in_chain;
19243
19244 if (!per_cu->cu->mark)
19245 {
68dc6402 19246 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19247 *last_chain = next_cu;
19248 }
19249 else
19250 last_chain = &per_cu->cu->read_in_chain;
19251
19252 per_cu = next_cu;
19253 }
19254}
19255
19256/* Remove a single compilation unit from the cache. */
19257
19258static void
dee91e82 19259free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19260{
19261 struct dwarf2_per_cu_data *per_cu, **last_chain;
19262
19263 per_cu = dwarf2_per_objfile->read_in_chain;
19264 last_chain = &dwarf2_per_objfile->read_in_chain;
19265 while (per_cu != NULL)
19266 {
19267 struct dwarf2_per_cu_data *next_cu;
19268
19269 next_cu = per_cu->cu->read_in_chain;
19270
dee91e82 19271 if (per_cu == target_per_cu)
ae038cb0 19272 {
68dc6402 19273 free_heap_comp_unit (per_cu->cu);
dee91e82 19274 per_cu->cu = NULL;
ae038cb0
DJ
19275 *last_chain = next_cu;
19276 break;
19277 }
19278 else
19279 last_chain = &per_cu->cu->read_in_chain;
19280
19281 per_cu = next_cu;
19282 }
19283}
19284
fe3e1990
DJ
19285/* Release all extra memory associated with OBJFILE. */
19286
19287void
19288dwarf2_free_objfile (struct objfile *objfile)
19289{
19290 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19291
19292 if (dwarf2_per_objfile == NULL)
19293 return;
19294
19295 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19296 free_cached_comp_units (NULL);
19297
7b9f3c50
DE
19298 if (dwarf2_per_objfile->quick_file_names_table)
19299 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19300
fe3e1990
DJ
19301 /* Everything else should be on the objfile obstack. */
19302}
19303
dee91e82
DE
19304/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19305 We store these in a hash table separate from the DIEs, and preserve them
19306 when the DIEs are flushed out of cache.
19307
19308 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19309 uniquely identify the type. A file may have multiple .debug_types sections,
19310 or the type may come from a DWO file. We have to use something in
19311 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19312 routine, get_die_type_at_offset, from outside this file, and thus won't
19313 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19314 of the objfile. */
1c379e20 19315
dee91e82 19316struct dwarf2_per_cu_offset_and_type
1c379e20 19317{
dee91e82 19318 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19319 sect_offset offset;
1c379e20
DJ
19320 struct type *type;
19321};
19322
dee91e82 19323/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19324
19325static hashval_t
dee91e82 19326per_cu_offset_and_type_hash (const void *item)
1c379e20 19327{
dee91e82 19328 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19329
dee91e82 19330 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19331}
19332
dee91e82 19333/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19334
19335static int
dee91e82 19336per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19337{
dee91e82
DE
19338 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19339 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19340
dee91e82
DE
19341 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19342 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19343}
19344
19345/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19346 table if necessary. For convenience, return TYPE.
19347
19348 The DIEs reading must have careful ordering to:
19349 * Not cause infite loops trying to read in DIEs as a prerequisite for
19350 reading current DIE.
19351 * Not trying to dereference contents of still incompletely read in types
19352 while reading in other DIEs.
19353 * Enable referencing still incompletely read in types just by a pointer to
19354 the type without accessing its fields.
19355
19356 Therefore caller should follow these rules:
19357 * Try to fetch any prerequisite types we may need to build this DIE type
19358 before building the type and calling set_die_type.
e71ec853 19359 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19360 possible before fetching more types to complete the current type.
19361 * Make the type as complete as possible before fetching more types. */
1c379e20 19362
f792889a 19363static struct type *
1c379e20
DJ
19364set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19365{
dee91e82 19366 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19367 struct objfile *objfile = cu->objfile;
1c379e20 19368
b4ba55a1
JB
19369 /* For Ada types, make sure that the gnat-specific data is always
19370 initialized (if not already set). There are a few types where
19371 we should not be doing so, because the type-specific area is
19372 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19373 where the type-specific area is used to store the floatformat).
19374 But this is not a problem, because the gnat-specific information
19375 is actually not needed for these types. */
19376 if (need_gnat_info (cu)
19377 && TYPE_CODE (type) != TYPE_CODE_FUNC
19378 && TYPE_CODE (type) != TYPE_CODE_FLT
19379 && !HAVE_GNAT_AUX_INFO (type))
19380 INIT_GNAT_SPECIFIC (type);
19381
dee91e82 19382 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19383 {
dee91e82
DE
19384 dwarf2_per_objfile->die_type_hash =
19385 htab_create_alloc_ex (127,
19386 per_cu_offset_and_type_hash,
19387 per_cu_offset_and_type_eq,
19388 NULL,
19389 &objfile->objfile_obstack,
19390 hashtab_obstack_allocate,
19391 dummy_obstack_deallocate);
f792889a 19392 }
1c379e20 19393
dee91e82 19394 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19395 ofs.offset = die->offset;
19396 ofs.type = type;
dee91e82
DE
19397 slot = (struct dwarf2_per_cu_offset_and_type **)
19398 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19399 if (*slot)
19400 complaint (&symfile_complaints,
19401 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19402 die->offset.sect_off);
673bfd45 19403 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19404 **slot = ofs;
f792889a 19405 return type;
1c379e20
DJ
19406}
19407
380bca97 19408/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19409 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19410
19411static struct type *
b64f50a1 19412get_die_type_at_offset (sect_offset offset,
673bfd45 19413 struct dwarf2_per_cu_data *per_cu)
1c379e20 19414{
dee91e82 19415 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19416
dee91e82 19417 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19418 return NULL;
1c379e20 19419
dee91e82 19420 ofs.per_cu = per_cu;
673bfd45 19421 ofs.offset = offset;
dee91e82 19422 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19423 if (slot)
19424 return slot->type;
19425 else
19426 return NULL;
19427}
19428
673bfd45
DE
19429/* Look up the type for DIE in the appropriate type_hash table,
19430 or return NULL if DIE does not have a saved type. */
19431
19432static struct type *
19433get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19434{
19435 return get_die_type_at_offset (die->offset, cu->per_cu);
19436}
19437
10b3939b
DJ
19438/* Add a dependence relationship from CU to REF_PER_CU. */
19439
19440static void
19441dwarf2_add_dependence (struct dwarf2_cu *cu,
19442 struct dwarf2_per_cu_data *ref_per_cu)
19443{
19444 void **slot;
19445
19446 if (cu->dependencies == NULL)
19447 cu->dependencies
19448 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19449 NULL, &cu->comp_unit_obstack,
19450 hashtab_obstack_allocate,
19451 dummy_obstack_deallocate);
19452
19453 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19454 if (*slot == NULL)
19455 *slot = ref_per_cu;
19456}
1c379e20 19457
f504f079
DE
19458/* Subroutine of dwarf2_mark to pass to htab_traverse.
19459 Set the mark field in every compilation unit in the
ae038cb0
DJ
19460 cache that we must keep because we are keeping CU. */
19461
10b3939b
DJ
19462static int
19463dwarf2_mark_helper (void **slot, void *data)
19464{
19465 struct dwarf2_per_cu_data *per_cu;
19466
19467 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19468
19469 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19470 reading of the chain. As such dependencies remain valid it is not much
19471 useful to track and undo them during QUIT cleanups. */
19472 if (per_cu->cu == NULL)
19473 return 1;
19474
10b3939b
DJ
19475 if (per_cu->cu->mark)
19476 return 1;
19477 per_cu->cu->mark = 1;
19478
19479 if (per_cu->cu->dependencies != NULL)
19480 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19481
19482 return 1;
19483}
19484
f504f079
DE
19485/* Set the mark field in CU and in every other compilation unit in the
19486 cache that we must keep because we are keeping CU. */
19487
ae038cb0
DJ
19488static void
19489dwarf2_mark (struct dwarf2_cu *cu)
19490{
19491 if (cu->mark)
19492 return;
19493 cu->mark = 1;
10b3939b
DJ
19494 if (cu->dependencies != NULL)
19495 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19496}
19497
19498static void
19499dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19500{
19501 while (per_cu)
19502 {
19503 per_cu->cu->mark = 0;
19504 per_cu = per_cu->cu->read_in_chain;
19505 }
72bf9492
DJ
19506}
19507
72bf9492
DJ
19508/* Trivial hash function for partial_die_info: the hash value of a DIE
19509 is its offset in .debug_info for this objfile. */
19510
19511static hashval_t
19512partial_die_hash (const void *item)
19513{
19514 const struct partial_die_info *part_die = item;
9a619af0 19515
b64f50a1 19516 return part_die->offset.sect_off;
72bf9492
DJ
19517}
19518
19519/* Trivial comparison function for partial_die_info structures: two DIEs
19520 are equal if they have the same offset. */
19521
19522static int
19523partial_die_eq (const void *item_lhs, const void *item_rhs)
19524{
19525 const struct partial_die_info *part_die_lhs = item_lhs;
19526 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19527
b64f50a1 19528 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19529}
19530
ae038cb0
DJ
19531static struct cmd_list_element *set_dwarf2_cmdlist;
19532static struct cmd_list_element *show_dwarf2_cmdlist;
19533
19534static void
19535set_dwarf2_cmd (char *args, int from_tty)
19536{
19537 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19538}
19539
19540static void
19541show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19542{
ae038cb0
DJ
19543 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19544}
19545
4bf44c1c 19546/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19547
19548static void
c1bd65d0 19549dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19550{
19551 struct dwarf2_per_objfile *data = d;
8b70b953 19552 int ix;
8b70b953 19553
95554aad
TT
19554 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19555 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19556 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19557
8b70b953 19558 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19559
19560 if (data->dwo_files)
19561 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19562
19563 if (data->dwz_file && data->dwz_file->dwz_bfd)
19564 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19565}
19566
19567\f
ae2de4f8 19568/* The "save gdb-index" command. */
9291a0cd
TT
19569
19570/* The contents of the hash table we create when building the string
19571 table. */
19572struct strtab_entry
19573{
19574 offset_type offset;
19575 const char *str;
19576};
19577
559a7a62
JK
19578/* Hash function for a strtab_entry.
19579
19580 Function is used only during write_hash_table so no index format backward
19581 compatibility is needed. */
b89be57b 19582
9291a0cd
TT
19583static hashval_t
19584hash_strtab_entry (const void *e)
19585{
19586 const struct strtab_entry *entry = e;
559a7a62 19587 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19588}
19589
19590/* Equality function for a strtab_entry. */
b89be57b 19591
9291a0cd
TT
19592static int
19593eq_strtab_entry (const void *a, const void *b)
19594{
19595 const struct strtab_entry *ea = a;
19596 const struct strtab_entry *eb = b;
19597 return !strcmp (ea->str, eb->str);
19598}
19599
19600/* Create a strtab_entry hash table. */
b89be57b 19601
9291a0cd
TT
19602static htab_t
19603create_strtab (void)
19604{
19605 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19606 xfree, xcalloc, xfree);
19607}
19608
19609/* Add a string to the constant pool. Return the string's offset in
19610 host order. */
b89be57b 19611
9291a0cd
TT
19612static offset_type
19613add_string (htab_t table, struct obstack *cpool, const char *str)
19614{
19615 void **slot;
19616 struct strtab_entry entry;
19617 struct strtab_entry *result;
19618
19619 entry.str = str;
19620 slot = htab_find_slot (table, &entry, INSERT);
19621 if (*slot)
19622 result = *slot;
19623 else
19624 {
19625 result = XNEW (struct strtab_entry);
19626 result->offset = obstack_object_size (cpool);
19627 result->str = str;
19628 obstack_grow_str0 (cpool, str);
19629 *slot = result;
19630 }
19631 return result->offset;
19632}
19633
19634/* An entry in the symbol table. */
19635struct symtab_index_entry
19636{
19637 /* The name of the symbol. */
19638 const char *name;
19639 /* The offset of the name in the constant pool. */
19640 offset_type index_offset;
19641 /* A sorted vector of the indices of all the CUs that hold an object
19642 of this name. */
19643 VEC (offset_type) *cu_indices;
19644};
19645
19646/* The symbol table. This is a power-of-2-sized hash table. */
19647struct mapped_symtab
19648{
19649 offset_type n_elements;
19650 offset_type size;
19651 struct symtab_index_entry **data;
19652};
19653
19654/* Hash function for a symtab_index_entry. */
b89be57b 19655
9291a0cd
TT
19656static hashval_t
19657hash_symtab_entry (const void *e)
19658{
19659 const struct symtab_index_entry *entry = e;
19660 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19661 sizeof (offset_type) * VEC_length (offset_type,
19662 entry->cu_indices),
19663 0);
19664}
19665
19666/* Equality function for a symtab_index_entry. */
b89be57b 19667
9291a0cd
TT
19668static int
19669eq_symtab_entry (const void *a, const void *b)
19670{
19671 const struct symtab_index_entry *ea = a;
19672 const struct symtab_index_entry *eb = b;
19673 int len = VEC_length (offset_type, ea->cu_indices);
19674 if (len != VEC_length (offset_type, eb->cu_indices))
19675 return 0;
19676 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19677 VEC_address (offset_type, eb->cu_indices),
19678 sizeof (offset_type) * len);
19679}
19680
19681/* Destroy a symtab_index_entry. */
b89be57b 19682
9291a0cd
TT
19683static void
19684delete_symtab_entry (void *p)
19685{
19686 struct symtab_index_entry *entry = p;
19687 VEC_free (offset_type, entry->cu_indices);
19688 xfree (entry);
19689}
19690
19691/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19692
9291a0cd 19693static htab_t
3876f04e 19694create_symbol_hash_table (void)
9291a0cd
TT
19695{
19696 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19697 delete_symtab_entry, xcalloc, xfree);
19698}
19699
19700/* Create a new mapped symtab object. */
b89be57b 19701
9291a0cd
TT
19702static struct mapped_symtab *
19703create_mapped_symtab (void)
19704{
19705 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19706 symtab->n_elements = 0;
19707 symtab->size = 1024;
19708 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19709 return symtab;
19710}
19711
19712/* Destroy a mapped_symtab. */
b89be57b 19713
9291a0cd
TT
19714static void
19715cleanup_mapped_symtab (void *p)
19716{
19717 struct mapped_symtab *symtab = p;
19718 /* The contents of the array are freed when the other hash table is
19719 destroyed. */
19720 xfree (symtab->data);
19721 xfree (symtab);
19722}
19723
19724/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19725 the slot.
19726
19727 Function is used only during write_hash_table so no index format backward
19728 compatibility is needed. */
b89be57b 19729
9291a0cd
TT
19730static struct symtab_index_entry **
19731find_slot (struct mapped_symtab *symtab, const char *name)
19732{
559a7a62 19733 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19734
19735 index = hash & (symtab->size - 1);
19736 step = ((hash * 17) & (symtab->size - 1)) | 1;
19737
19738 for (;;)
19739 {
19740 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19741 return &symtab->data[index];
19742 index = (index + step) & (symtab->size - 1);
19743 }
19744}
19745
19746/* Expand SYMTAB's hash table. */
b89be57b 19747
9291a0cd
TT
19748static void
19749hash_expand (struct mapped_symtab *symtab)
19750{
19751 offset_type old_size = symtab->size;
19752 offset_type i;
19753 struct symtab_index_entry **old_entries = symtab->data;
19754
19755 symtab->size *= 2;
19756 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19757
19758 for (i = 0; i < old_size; ++i)
19759 {
19760 if (old_entries[i])
19761 {
19762 struct symtab_index_entry **slot = find_slot (symtab,
19763 old_entries[i]->name);
19764 *slot = old_entries[i];
19765 }
19766 }
19767
19768 xfree (old_entries);
19769}
19770
156942c7
DE
19771/* Add an entry to SYMTAB. NAME is the name of the symbol.
19772 CU_INDEX is the index of the CU in which the symbol appears.
19773 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19774
9291a0cd
TT
19775static void
19776add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19777 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19778 offset_type cu_index)
19779{
19780 struct symtab_index_entry **slot;
156942c7 19781 offset_type cu_index_and_attrs;
9291a0cd
TT
19782
19783 ++symtab->n_elements;
19784 if (4 * symtab->n_elements / 3 >= symtab->size)
19785 hash_expand (symtab);
19786
19787 slot = find_slot (symtab, name);
19788 if (!*slot)
19789 {
19790 *slot = XNEW (struct symtab_index_entry);
19791 (*slot)->name = name;
156942c7 19792 /* index_offset is set later. */
9291a0cd
TT
19793 (*slot)->cu_indices = NULL;
19794 }
156942c7
DE
19795
19796 cu_index_and_attrs = 0;
19797 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19798 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19799 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19800
19801 /* We don't want to record an index value twice as we want to avoid the
19802 duplication.
19803 We process all global symbols and then all static symbols
19804 (which would allow us to avoid the duplication by only having to check
19805 the last entry pushed), but a symbol could have multiple kinds in one CU.
19806 To keep things simple we don't worry about the duplication here and
19807 sort and uniqufy the list after we've processed all symbols. */
19808 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19809}
19810
19811/* qsort helper routine for uniquify_cu_indices. */
19812
19813static int
19814offset_type_compare (const void *ap, const void *bp)
19815{
19816 offset_type a = *(offset_type *) ap;
19817 offset_type b = *(offset_type *) bp;
19818
19819 return (a > b) - (b > a);
19820}
19821
19822/* Sort and remove duplicates of all symbols' cu_indices lists. */
19823
19824static void
19825uniquify_cu_indices (struct mapped_symtab *symtab)
19826{
19827 int i;
19828
19829 for (i = 0; i < symtab->size; ++i)
19830 {
19831 struct symtab_index_entry *entry = symtab->data[i];
19832
19833 if (entry
19834 && entry->cu_indices != NULL)
19835 {
19836 unsigned int next_to_insert, next_to_check;
19837 offset_type last_value;
19838
19839 qsort (VEC_address (offset_type, entry->cu_indices),
19840 VEC_length (offset_type, entry->cu_indices),
19841 sizeof (offset_type), offset_type_compare);
19842
19843 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19844 next_to_insert = 1;
19845 for (next_to_check = 1;
19846 next_to_check < VEC_length (offset_type, entry->cu_indices);
19847 ++next_to_check)
19848 {
19849 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19850 != last_value)
19851 {
19852 last_value = VEC_index (offset_type, entry->cu_indices,
19853 next_to_check);
19854 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19855 last_value);
19856 ++next_to_insert;
19857 }
19858 }
19859 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19860 }
19861 }
9291a0cd
TT
19862}
19863
19864/* Add a vector of indices to the constant pool. */
b89be57b 19865
9291a0cd 19866static offset_type
3876f04e 19867add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19868 struct symtab_index_entry *entry)
19869{
19870 void **slot;
19871
3876f04e 19872 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19873 if (!*slot)
19874 {
19875 offset_type len = VEC_length (offset_type, entry->cu_indices);
19876 offset_type val = MAYBE_SWAP (len);
19877 offset_type iter;
19878 int i;
19879
19880 *slot = entry;
19881 entry->index_offset = obstack_object_size (cpool);
19882
19883 obstack_grow (cpool, &val, sizeof (val));
19884 for (i = 0;
19885 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19886 ++i)
19887 {
19888 val = MAYBE_SWAP (iter);
19889 obstack_grow (cpool, &val, sizeof (val));
19890 }
19891 }
19892 else
19893 {
19894 struct symtab_index_entry *old_entry = *slot;
19895 entry->index_offset = old_entry->index_offset;
19896 entry = old_entry;
19897 }
19898 return entry->index_offset;
19899}
19900
19901/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19902 constant pool entries going into the obstack CPOOL. */
b89be57b 19903
9291a0cd
TT
19904static void
19905write_hash_table (struct mapped_symtab *symtab,
19906 struct obstack *output, struct obstack *cpool)
19907{
19908 offset_type i;
3876f04e 19909 htab_t symbol_hash_table;
9291a0cd
TT
19910 htab_t str_table;
19911
3876f04e 19912 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19913 str_table = create_strtab ();
3876f04e 19914
9291a0cd
TT
19915 /* We add all the index vectors to the constant pool first, to
19916 ensure alignment is ok. */
19917 for (i = 0; i < symtab->size; ++i)
19918 {
19919 if (symtab->data[i])
3876f04e 19920 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19921 }
19922
19923 /* Now write out the hash table. */
19924 for (i = 0; i < symtab->size; ++i)
19925 {
19926 offset_type str_off, vec_off;
19927
19928 if (symtab->data[i])
19929 {
19930 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19931 vec_off = symtab->data[i]->index_offset;
19932 }
19933 else
19934 {
19935 /* While 0 is a valid constant pool index, it is not valid
19936 to have 0 for both offsets. */
19937 str_off = 0;
19938 vec_off = 0;
19939 }
19940
19941 str_off = MAYBE_SWAP (str_off);
19942 vec_off = MAYBE_SWAP (vec_off);
19943
19944 obstack_grow (output, &str_off, sizeof (str_off));
19945 obstack_grow (output, &vec_off, sizeof (vec_off));
19946 }
19947
19948 htab_delete (str_table);
3876f04e 19949 htab_delete (symbol_hash_table);
9291a0cd
TT
19950}
19951
0a5429f6
DE
19952/* Struct to map psymtab to CU index in the index file. */
19953struct psymtab_cu_index_map
19954{
19955 struct partial_symtab *psymtab;
19956 unsigned int cu_index;
19957};
19958
19959static hashval_t
19960hash_psymtab_cu_index (const void *item)
19961{
19962 const struct psymtab_cu_index_map *map = item;
19963
19964 return htab_hash_pointer (map->psymtab);
19965}
19966
19967static int
19968eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19969{
19970 const struct psymtab_cu_index_map *lhs = item_lhs;
19971 const struct psymtab_cu_index_map *rhs = item_rhs;
19972
19973 return lhs->psymtab == rhs->psymtab;
19974}
19975
19976/* Helper struct for building the address table. */
19977struct addrmap_index_data
19978{
19979 struct objfile *objfile;
19980 struct obstack *addr_obstack;
19981 htab_t cu_index_htab;
19982
19983 /* Non-zero if the previous_* fields are valid.
19984 We can't write an entry until we see the next entry (since it is only then
19985 that we know the end of the entry). */
19986 int previous_valid;
19987 /* Index of the CU in the table of all CUs in the index file. */
19988 unsigned int previous_cu_index;
0963b4bd 19989 /* Start address of the CU. */
0a5429f6
DE
19990 CORE_ADDR previous_cu_start;
19991};
19992
19993/* Write an address entry to OBSTACK. */
b89be57b 19994
9291a0cd 19995static void
0a5429f6
DE
19996add_address_entry (struct objfile *objfile, struct obstack *obstack,
19997 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 19998{
0a5429f6 19999 offset_type cu_index_to_write;
9291a0cd
TT
20000 char addr[8];
20001 CORE_ADDR baseaddr;
20002
20003 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20004
0a5429f6
DE
20005 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20006 obstack_grow (obstack, addr, 8);
20007 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20008 obstack_grow (obstack, addr, 8);
20009 cu_index_to_write = MAYBE_SWAP (cu_index);
20010 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20011}
20012
20013/* Worker function for traversing an addrmap to build the address table. */
20014
20015static int
20016add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20017{
20018 struct addrmap_index_data *data = datap;
20019 struct partial_symtab *pst = obj;
0a5429f6
DE
20020
20021 if (data->previous_valid)
20022 add_address_entry (data->objfile, data->addr_obstack,
20023 data->previous_cu_start, start_addr,
20024 data->previous_cu_index);
20025
20026 data->previous_cu_start = start_addr;
20027 if (pst != NULL)
20028 {
20029 struct psymtab_cu_index_map find_map, *map;
20030 find_map.psymtab = pst;
20031 map = htab_find (data->cu_index_htab, &find_map);
20032 gdb_assert (map != NULL);
20033 data->previous_cu_index = map->cu_index;
20034 data->previous_valid = 1;
20035 }
20036 else
20037 data->previous_valid = 0;
20038
20039 return 0;
20040}
20041
20042/* Write OBJFILE's address map to OBSTACK.
20043 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20044 in the index file. */
20045
20046static void
20047write_address_map (struct objfile *objfile, struct obstack *obstack,
20048 htab_t cu_index_htab)
20049{
20050 struct addrmap_index_data addrmap_index_data;
20051
20052 /* When writing the address table, we have to cope with the fact that
20053 the addrmap iterator only provides the start of a region; we have to
20054 wait until the next invocation to get the start of the next region. */
20055
20056 addrmap_index_data.objfile = objfile;
20057 addrmap_index_data.addr_obstack = obstack;
20058 addrmap_index_data.cu_index_htab = cu_index_htab;
20059 addrmap_index_data.previous_valid = 0;
20060
20061 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20062 &addrmap_index_data);
20063
20064 /* It's highly unlikely the last entry (end address = 0xff...ff)
20065 is valid, but we should still handle it.
20066 The end address is recorded as the start of the next region, but that
20067 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20068 anyway. */
20069 if (addrmap_index_data.previous_valid)
20070 add_address_entry (objfile, obstack,
20071 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20072 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20073}
20074
156942c7
DE
20075/* Return the symbol kind of PSYM. */
20076
20077static gdb_index_symbol_kind
20078symbol_kind (struct partial_symbol *psym)
20079{
20080 domain_enum domain = PSYMBOL_DOMAIN (psym);
20081 enum address_class aclass = PSYMBOL_CLASS (psym);
20082
20083 switch (domain)
20084 {
20085 case VAR_DOMAIN:
20086 switch (aclass)
20087 {
20088 case LOC_BLOCK:
20089 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20090 case LOC_TYPEDEF:
20091 return GDB_INDEX_SYMBOL_KIND_TYPE;
20092 case LOC_COMPUTED:
20093 case LOC_CONST_BYTES:
20094 case LOC_OPTIMIZED_OUT:
20095 case LOC_STATIC:
20096 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20097 case LOC_CONST:
20098 /* Note: It's currently impossible to recognize psyms as enum values
20099 short of reading the type info. For now punt. */
20100 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20101 default:
20102 /* There are other LOC_FOO values that one might want to classify
20103 as variables, but dwarf2read.c doesn't currently use them. */
20104 return GDB_INDEX_SYMBOL_KIND_OTHER;
20105 }
20106 case STRUCT_DOMAIN:
20107 return GDB_INDEX_SYMBOL_KIND_TYPE;
20108 default:
20109 return GDB_INDEX_SYMBOL_KIND_OTHER;
20110 }
20111}
20112
9291a0cd 20113/* Add a list of partial symbols to SYMTAB. */
b89be57b 20114
9291a0cd
TT
20115static void
20116write_psymbols (struct mapped_symtab *symtab,
987d643c 20117 htab_t psyms_seen,
9291a0cd
TT
20118 struct partial_symbol **psymp,
20119 int count,
987d643c
TT
20120 offset_type cu_index,
20121 int is_static)
9291a0cd
TT
20122{
20123 for (; count-- > 0; ++psymp)
20124 {
156942c7
DE
20125 struct partial_symbol *psym = *psymp;
20126 void **slot;
987d643c 20127
156942c7 20128 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20129 error (_("Ada is not currently supported by the index"));
987d643c 20130
987d643c 20131 /* Only add a given psymbol once. */
156942c7 20132 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20133 if (!*slot)
20134 {
156942c7
DE
20135 gdb_index_symbol_kind kind = symbol_kind (psym);
20136
20137 *slot = psym;
20138 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20139 is_static, kind, cu_index);
987d643c 20140 }
9291a0cd
TT
20141 }
20142}
20143
20144/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20145 exception if there is an error. */
b89be57b 20146
9291a0cd
TT
20147static void
20148write_obstack (FILE *file, struct obstack *obstack)
20149{
20150 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20151 file)
20152 != obstack_object_size (obstack))
20153 error (_("couldn't data write to file"));
20154}
20155
20156/* Unlink a file if the argument is not NULL. */
b89be57b 20157
9291a0cd
TT
20158static void
20159unlink_if_set (void *p)
20160{
20161 char **filename = p;
20162 if (*filename)
20163 unlink (*filename);
20164}
20165
1fd400ff
TT
20166/* A helper struct used when iterating over debug_types. */
20167struct signatured_type_index_data
20168{
20169 struct objfile *objfile;
20170 struct mapped_symtab *symtab;
20171 struct obstack *types_list;
987d643c 20172 htab_t psyms_seen;
1fd400ff
TT
20173 int cu_index;
20174};
20175
20176/* A helper function that writes a single signatured_type to an
20177 obstack. */
b89be57b 20178
1fd400ff
TT
20179static int
20180write_one_signatured_type (void **slot, void *d)
20181{
20182 struct signatured_type_index_data *info = d;
20183 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20184 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20185 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20186 gdb_byte val[8];
20187
20188 write_psymbols (info->symtab,
987d643c 20189 info->psyms_seen,
3e43a32a
MS
20190 info->objfile->global_psymbols.list
20191 + psymtab->globals_offset,
987d643c
TT
20192 psymtab->n_global_syms, info->cu_index,
20193 0);
1fd400ff 20194 write_psymbols (info->symtab,
987d643c 20195 info->psyms_seen,
3e43a32a
MS
20196 info->objfile->static_psymbols.list
20197 + psymtab->statics_offset,
987d643c
TT
20198 psymtab->n_static_syms, info->cu_index,
20199 1);
1fd400ff 20200
b64f50a1
JK
20201 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20202 entry->per_cu.offset.sect_off);
1fd400ff 20203 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20204 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20205 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20206 obstack_grow (info->types_list, val, 8);
20207 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20208 obstack_grow (info->types_list, val, 8);
20209
20210 ++info->cu_index;
20211
20212 return 1;
20213}
20214
95554aad
TT
20215/* Recurse into all "included" dependencies and write their symbols as
20216 if they appeared in this psymtab. */
20217
20218static void
20219recursively_write_psymbols (struct objfile *objfile,
20220 struct partial_symtab *psymtab,
20221 struct mapped_symtab *symtab,
20222 htab_t psyms_seen,
20223 offset_type cu_index)
20224{
20225 int i;
20226
20227 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20228 if (psymtab->dependencies[i]->user != NULL)
20229 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20230 symtab, psyms_seen, cu_index);
20231
20232 write_psymbols (symtab,
20233 psyms_seen,
20234 objfile->global_psymbols.list + psymtab->globals_offset,
20235 psymtab->n_global_syms, cu_index,
20236 0);
20237 write_psymbols (symtab,
20238 psyms_seen,
20239 objfile->static_psymbols.list + psymtab->statics_offset,
20240 psymtab->n_static_syms, cu_index,
20241 1);
20242}
20243
9291a0cd 20244/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20245
9291a0cd
TT
20246static void
20247write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20248{
20249 struct cleanup *cleanup;
20250 char *filename, *cleanup_filename;
1fd400ff
TT
20251 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20252 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20253 int i;
20254 FILE *out_file;
20255 struct mapped_symtab *symtab;
20256 offset_type val, size_of_contents, total_len;
20257 struct stat st;
987d643c 20258 htab_t psyms_seen;
0a5429f6
DE
20259 htab_t cu_index_htab;
20260 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20261
b4f2f049 20262 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20263 return;
b4f2f049 20264
9291a0cd
TT
20265 if (dwarf2_per_objfile->using_index)
20266 error (_("Cannot use an index to create the index"));
20267
8b70b953
TT
20268 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20269 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20270
9291a0cd 20271 if (stat (objfile->name, &st) < 0)
7e17e088 20272 perror_with_name (objfile->name);
9291a0cd
TT
20273
20274 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20275 INDEX_SUFFIX, (char *) NULL);
20276 cleanup = make_cleanup (xfree, filename);
20277
20278 out_file = fopen (filename, "wb");
20279 if (!out_file)
20280 error (_("Can't open `%s' for writing"), filename);
20281
20282 cleanup_filename = filename;
20283 make_cleanup (unlink_if_set, &cleanup_filename);
20284
20285 symtab = create_mapped_symtab ();
20286 make_cleanup (cleanup_mapped_symtab, symtab);
20287
20288 obstack_init (&addr_obstack);
20289 make_cleanup_obstack_free (&addr_obstack);
20290
20291 obstack_init (&cu_list);
20292 make_cleanup_obstack_free (&cu_list);
20293
1fd400ff
TT
20294 obstack_init (&types_cu_list);
20295 make_cleanup_obstack_free (&types_cu_list);
20296
987d643c
TT
20297 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20298 NULL, xcalloc, xfree);
96408a79 20299 make_cleanup_htab_delete (psyms_seen);
987d643c 20300
0a5429f6
DE
20301 /* While we're scanning CU's create a table that maps a psymtab pointer
20302 (which is what addrmap records) to its index (which is what is recorded
20303 in the index file). This will later be needed to write the address
20304 table. */
20305 cu_index_htab = htab_create_alloc (100,
20306 hash_psymtab_cu_index,
20307 eq_psymtab_cu_index,
20308 NULL, xcalloc, xfree);
96408a79 20309 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20310 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20311 xmalloc (sizeof (struct psymtab_cu_index_map)
20312 * dwarf2_per_objfile->n_comp_units);
20313 make_cleanup (xfree, psymtab_cu_index_map);
20314
20315 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20316 work here. Also, the debug_types entries do not appear in
20317 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20318 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20319 {
3e43a32a
MS
20320 struct dwarf2_per_cu_data *per_cu
20321 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20322 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20323 gdb_byte val[8];
0a5429f6
DE
20324 struct psymtab_cu_index_map *map;
20325 void **slot;
9291a0cd 20326
95554aad
TT
20327 if (psymtab->user == NULL)
20328 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20329
0a5429f6
DE
20330 map = &psymtab_cu_index_map[i];
20331 map->psymtab = psymtab;
20332 map->cu_index = i;
20333 slot = htab_find_slot (cu_index_htab, map, INSERT);
20334 gdb_assert (slot != NULL);
20335 gdb_assert (*slot == NULL);
20336 *slot = map;
9291a0cd 20337
b64f50a1
JK
20338 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20339 per_cu->offset.sect_off);
9291a0cd 20340 obstack_grow (&cu_list, val, 8);
e254ef6a 20341 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20342 obstack_grow (&cu_list, val, 8);
20343 }
20344
0a5429f6
DE
20345 /* Dump the address map. */
20346 write_address_map (objfile, &addr_obstack, cu_index_htab);
20347
1fd400ff
TT
20348 /* Write out the .debug_type entries, if any. */
20349 if (dwarf2_per_objfile->signatured_types)
20350 {
20351 struct signatured_type_index_data sig_data;
20352
20353 sig_data.objfile = objfile;
20354 sig_data.symtab = symtab;
20355 sig_data.types_list = &types_cu_list;
987d643c 20356 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20357 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20358 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20359 write_one_signatured_type, &sig_data);
20360 }
20361
156942c7
DE
20362 /* Now that we've processed all symbols we can shrink their cu_indices
20363 lists. */
20364 uniquify_cu_indices (symtab);
20365
9291a0cd
TT
20366 obstack_init (&constant_pool);
20367 make_cleanup_obstack_free (&constant_pool);
20368 obstack_init (&symtab_obstack);
20369 make_cleanup_obstack_free (&symtab_obstack);
20370 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20371
20372 obstack_init (&contents);
20373 make_cleanup_obstack_free (&contents);
1fd400ff 20374 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20375 total_len = size_of_contents;
20376
20377 /* The version number. */
156942c7 20378 val = MAYBE_SWAP (7);
9291a0cd
TT
20379 obstack_grow (&contents, &val, sizeof (val));
20380
20381 /* The offset of the CU list from the start of the file. */
20382 val = MAYBE_SWAP (total_len);
20383 obstack_grow (&contents, &val, sizeof (val));
20384 total_len += obstack_object_size (&cu_list);
20385
1fd400ff
TT
20386 /* The offset of the types CU list from the start of the file. */
20387 val = MAYBE_SWAP (total_len);
20388 obstack_grow (&contents, &val, sizeof (val));
20389 total_len += obstack_object_size (&types_cu_list);
20390
9291a0cd
TT
20391 /* The offset of the address table from the start of the file. */
20392 val = MAYBE_SWAP (total_len);
20393 obstack_grow (&contents, &val, sizeof (val));
20394 total_len += obstack_object_size (&addr_obstack);
20395
20396 /* The offset of the symbol table from the start of the file. */
20397 val = MAYBE_SWAP (total_len);
20398 obstack_grow (&contents, &val, sizeof (val));
20399 total_len += obstack_object_size (&symtab_obstack);
20400
20401 /* The offset of the constant pool from the start of the file. */
20402 val = MAYBE_SWAP (total_len);
20403 obstack_grow (&contents, &val, sizeof (val));
20404 total_len += obstack_object_size (&constant_pool);
20405
20406 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20407
20408 write_obstack (out_file, &contents);
20409 write_obstack (out_file, &cu_list);
1fd400ff 20410 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20411 write_obstack (out_file, &addr_obstack);
20412 write_obstack (out_file, &symtab_obstack);
20413 write_obstack (out_file, &constant_pool);
20414
20415 fclose (out_file);
20416
20417 /* We want to keep the file, so we set cleanup_filename to NULL
20418 here. See unlink_if_set. */
20419 cleanup_filename = NULL;
20420
20421 do_cleanups (cleanup);
20422}
20423
90476074
TT
20424/* Implementation of the `save gdb-index' command.
20425
20426 Note that the file format used by this command is documented in the
20427 GDB manual. Any changes here must be documented there. */
11570e71 20428
9291a0cd
TT
20429static void
20430save_gdb_index_command (char *arg, int from_tty)
20431{
20432 struct objfile *objfile;
20433
20434 if (!arg || !*arg)
96d19272 20435 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20436
20437 ALL_OBJFILES (objfile)
20438 {
20439 struct stat st;
20440
20441 /* If the objfile does not correspond to an actual file, skip it. */
20442 if (stat (objfile->name, &st) < 0)
20443 continue;
20444
20445 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20446 if (dwarf2_per_objfile)
20447 {
20448 volatile struct gdb_exception except;
20449
20450 TRY_CATCH (except, RETURN_MASK_ERROR)
20451 {
20452 write_psymtabs_to_index (objfile, arg);
20453 }
20454 if (except.reason < 0)
20455 exception_fprintf (gdb_stderr, except,
20456 _("Error while writing index for `%s': "),
20457 objfile->name);
20458 }
20459 }
dce234bc
PP
20460}
20461
9291a0cd
TT
20462\f
20463
9eae7c52
TT
20464int dwarf2_always_disassemble;
20465
20466static void
20467show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20468 struct cmd_list_element *c, const char *value)
20469{
3e43a32a
MS
20470 fprintf_filtered (file,
20471 _("Whether to always disassemble "
20472 "DWARF expressions is %s.\n"),
9eae7c52
TT
20473 value);
20474}
20475
900e11f9
JK
20476static void
20477show_check_physname (struct ui_file *file, int from_tty,
20478 struct cmd_list_element *c, const char *value)
20479{
20480 fprintf_filtered (file,
20481 _("Whether to check \"physname\" is %s.\n"),
20482 value);
20483}
20484
6502dd73
DJ
20485void _initialize_dwarf2_read (void);
20486
20487void
20488_initialize_dwarf2_read (void)
20489{
96d19272
JK
20490 struct cmd_list_element *c;
20491
dce234bc 20492 dwarf2_objfile_data_key
c1bd65d0 20493 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20494
1bedd215
AC
20495 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20496Set DWARF 2 specific variables.\n\
20497Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20498 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20499 0/*allow-unknown*/, &maintenance_set_cmdlist);
20500
1bedd215
AC
20501 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20502Show DWARF 2 specific variables\n\
20503Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20504 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20505 0/*allow-unknown*/, &maintenance_show_cmdlist);
20506
20507 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20508 &dwarf2_max_cache_age, _("\
20509Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20510Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20511A higher limit means that cached compilation units will be stored\n\
20512in memory longer, and more total memory will be used. Zero disables\n\
20513caching, which can slow down startup."),
2c5b56ce 20514 NULL,
920d2a44 20515 show_dwarf2_max_cache_age,
2c5b56ce 20516 &set_dwarf2_cmdlist,
ae038cb0 20517 &show_dwarf2_cmdlist);
d97bc12b 20518
9eae7c52
TT
20519 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20520 &dwarf2_always_disassemble, _("\
20521Set whether `info address' always disassembles DWARF expressions."), _("\
20522Show whether `info address' always disassembles DWARF expressions."), _("\
20523When enabled, DWARF expressions are always printed in an assembly-like\n\
20524syntax. When disabled, expressions will be printed in a more\n\
20525conversational style, when possible."),
20526 NULL,
20527 show_dwarf2_always_disassemble,
20528 &set_dwarf2_cmdlist,
20529 &show_dwarf2_cmdlist);
20530
45cfd468
DE
20531 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20532Set debugging of the dwarf2 reader."), _("\
20533Show debugging of the dwarf2 reader."), _("\
20534When enabled, debugging messages are printed during dwarf2 reading\n\
20535and symtab expansion."),
20536 NULL,
20537 NULL,
20538 &setdebuglist, &showdebuglist);
20539
ccce17b0 20540 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20541Set debugging of the dwarf2 DIE reader."), _("\
20542Show debugging of the dwarf2 DIE reader."), _("\
20543When enabled (non-zero), DIEs are dumped after they are read in.\n\
20544The value is the maximum depth to print."),
ccce17b0
YQ
20545 NULL,
20546 NULL,
20547 &setdebuglist, &showdebuglist);
9291a0cd 20548
900e11f9
JK
20549 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20550Set cross-checking of \"physname\" code against demangler."), _("\
20551Show cross-checking of \"physname\" code against demangler."), _("\
20552When enabled, GDB's internal \"physname\" code is checked against\n\
20553the demangler."),
20554 NULL, show_check_physname,
20555 &setdebuglist, &showdebuglist);
20556
e615022a
DE
20557 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20558 no_class, &use_deprecated_index_sections, _("\
20559Set whether to use deprecated gdb_index sections."), _("\
20560Show whether to use deprecated gdb_index sections."), _("\
20561When enabled, deprecated .gdb_index sections are used anyway.\n\
20562Normally they are ignored either because of a missing feature or\n\
20563performance issue.\n\
20564Warning: This option must be enabled before gdb reads the file."),
20565 NULL,
20566 NULL,
20567 &setlist, &showlist);
20568
96d19272 20569 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20570 _("\
fc1a9d6e 20571Save a gdb-index file.\n\
11570e71 20572Usage: save gdb-index DIRECTORY"),
96d19272
JK
20573 &save_cmdlist);
20574 set_cmd_completer (c, filename_completer);
6502dd73 20575}
This page took 2.75361 seconds and 4 git commands to generate.