* dbxread.c (process_one_symbol): Constify section_offsets parameter.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
4c2df51b 72
c906108c
SS
73#include <fcntl.h>
74#include "gdb_string.h"
4bdf3d34 75#include "gdb_assert.h"
c906108c 76#include <sys/types.h>
d8151005 77
34eaf542
TT
78typedef struct symbol *symbolp;
79DEF_VEC_P (symbolp);
80
45cfd468
DE
81/* When non-zero, print basic high level tracing messages.
82 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
83static int dwarf2_read_debug = 0;
84
d97bc12b 85/* When non-zero, dump DIEs after they are read in. */
ccce17b0 86static unsigned int dwarf2_die_debug = 0;
d97bc12b 87
900e11f9
JK
88/* When non-zero, cross-check physname against demangler. */
89static int check_physname = 0;
90
481860b3 91/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 92static int use_deprecated_index_sections = 0;
481860b3 93
6502dd73
DJ
94static const struct objfile_data *dwarf2_objfile_data_key;
95
f1e6e072
TT
96/* The "aclass" indices for various kinds of computed DWARF symbols. */
97
98static int dwarf2_locexpr_index;
99static int dwarf2_loclist_index;
100static int dwarf2_locexpr_block_index;
101static int dwarf2_loclist_block_index;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
d521ce57 106 const gdb_byte *buffer;
dce234bc 107 bfd_size_type size;
be391dca
TT
108 /* True if we have tried to read this section. */
109 int readin;
dce234bc
PP
110};
111
8b70b953
TT
112typedef struct dwarf2_section_info dwarf2_section_info_def;
113DEF_VEC_O (dwarf2_section_info_def);
114
9291a0cd
TT
115/* All offsets in the index are of this type. It must be
116 architecture-independent. */
117typedef uint32_t offset_type;
118
119DEF_VEC_I (offset_type);
120
156942c7
DE
121/* Ensure only legit values are used. */
122#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 do { \
124 gdb_assert ((unsigned int) (value) <= 1); \
125 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128/* Ensure only legit values are used. */
129#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
132 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
133 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
134 } while (0)
135
136/* Ensure we don't use more than the alloted nuber of bits for the CU. */
137#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 do { \
139 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
140 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
141 } while (0)
142
9291a0cd
TT
143/* A description of the mapped index. The file format is described in
144 a comment by the code that writes the index. */
145struct mapped_index
146{
559a7a62
JK
147 /* Index data format version. */
148 int version;
149
9291a0cd
TT
150 /* The total length of the buffer. */
151 off_t total_size;
b11b1f88 152
9291a0cd
TT
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
b11b1f88 155
9291a0cd
TT
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
b11b1f88 158
3876f04e
DE
159 /* The symbol table, implemented as a hash table. */
160 const offset_type *symbol_table;
b11b1f88 161
9291a0cd 162 /* Size in slots, each slot is 2 offset_types. */
3876f04e 163 offset_type symbol_table_slots;
b11b1f88 164
9291a0cd
TT
165 /* A pointer to the constant pool. */
166 const char *constant_pool;
167};
168
95554aad
TT
169typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
170DEF_VEC_P (dwarf2_per_cu_ptr);
171
9cdd5dbd
DE
172/* Collection of data recorded per objfile.
173 This hangs off of dwarf2_objfile_data_key. */
174
6502dd73
DJ
175struct dwarf2_per_objfile
176{
dce234bc
PP
177 struct dwarf2_section_info info;
178 struct dwarf2_section_info abbrev;
179 struct dwarf2_section_info line;
dce234bc
PP
180 struct dwarf2_section_info loc;
181 struct dwarf2_section_info macinfo;
cf2c3c16 182 struct dwarf2_section_info macro;
dce234bc
PP
183 struct dwarf2_section_info str;
184 struct dwarf2_section_info ranges;
3019eac3 185 struct dwarf2_section_info addr;
dce234bc
PP
186 struct dwarf2_section_info frame;
187 struct dwarf2_section_info eh_frame;
9291a0cd 188 struct dwarf2_section_info gdb_index;
ae038cb0 189
8b70b953
TT
190 VEC (dwarf2_section_info_def) *types;
191
be391dca
TT
192 /* Back link. */
193 struct objfile *objfile;
194
d467dd73 195 /* Table of all the compilation units. This is used to locate
10b3939b 196 the target compilation unit of a particular reference. */
ae038cb0
DJ
197 struct dwarf2_per_cu_data **all_comp_units;
198
199 /* The number of compilation units in ALL_COMP_UNITS. */
200 int n_comp_units;
201
1fd400ff 202 /* The number of .debug_types-related CUs. */
d467dd73 203 int n_type_units;
1fd400ff 204
d467dd73 205 /* The .debug_types-related CUs (TUs). */
b4dd5633 206 struct signatured_type **all_type_units;
1fd400ff 207
f4dc4d17
DE
208 /* The number of entries in all_type_unit_groups. */
209 int n_type_unit_groups;
210
211 /* Table of type unit groups.
212 This exists to make it easy to iterate over all CUs and TU groups. */
213 struct type_unit_group **all_type_unit_groups;
214
215 /* Table of struct type_unit_group objects.
216 The hash key is the DW_AT_stmt_list value. */
217 htab_t type_unit_groups;
72dca2f5 218
348e048f
DE
219 /* A table mapping .debug_types signatures to its signatured_type entry.
220 This is NULL if the .debug_types section hasn't been read in yet. */
221 htab_t signatured_types;
222
f4dc4d17
DE
223 /* Type unit statistics, to see how well the scaling improvements
224 are doing. */
225 struct tu_stats
226 {
227 int nr_uniq_abbrev_tables;
228 int nr_symtabs;
229 int nr_symtab_sharers;
230 int nr_stmt_less_type_units;
231 } tu_stats;
232
233 /* A chain of compilation units that are currently read in, so that
234 they can be freed later. */
235 struct dwarf2_per_cu_data *read_in_chain;
236
3019eac3
DE
237 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
238 This is NULL if the table hasn't been allocated yet. */
239 htab_t dwo_files;
240
80626a55
DE
241 /* Non-zero if we've check for whether there is a DWP file. */
242 int dwp_checked;
243
244 /* The DWP file if there is one, or NULL. */
245 struct dwp_file *dwp_file;
246
36586728
TT
247 /* The shared '.dwz' file, if one exists. This is used when the
248 original data was compressed using 'dwz -m'. */
249 struct dwz_file *dwz_file;
250
72dca2f5
FR
251 /* A flag indicating wether this objfile has a section loaded at a
252 VMA of 0. */
253 int has_section_at_zero;
9291a0cd 254
ae2de4f8
DE
255 /* True if we are using the mapped index,
256 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
257 unsigned char using_index;
258
ae2de4f8 259 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 260 struct mapped_index *index_table;
98bfdba5 261
7b9f3c50 262 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
263 TUs typically share line table entries with a CU, so we maintain a
264 separate table of all line table entries to support the sharing.
265 Note that while there can be way more TUs than CUs, we've already
266 sorted all the TUs into "type unit groups", grouped by their
267 DW_AT_stmt_list value. Therefore the only sharing done here is with a
268 CU and its associated TU group if there is one. */
7b9f3c50
DE
269 htab_t quick_file_names_table;
270
98bfdba5
PA
271 /* Set during partial symbol reading, to prevent queueing of full
272 symbols. */
273 int reading_partial_symbols;
673bfd45 274
dee91e82 275 /* Table mapping type DIEs to their struct type *.
673bfd45 276 This is NULL if not allocated yet.
02142a6c 277 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 278 htab_t die_type_hash;
95554aad
TT
279
280 /* The CUs we recently read. */
281 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
282};
283
284static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 285
251d32d9 286/* Default names of the debugging sections. */
c906108c 287
233a11ab
CS
288/* Note that if the debugging section has been compressed, it might
289 have a name like .zdebug_info. */
290
9cdd5dbd
DE
291static const struct dwarf2_debug_sections dwarf2_elf_names =
292{
251d32d9
TG
293 { ".debug_info", ".zdebug_info" },
294 { ".debug_abbrev", ".zdebug_abbrev" },
295 { ".debug_line", ".zdebug_line" },
296 { ".debug_loc", ".zdebug_loc" },
297 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 298 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
299 { ".debug_str", ".zdebug_str" },
300 { ".debug_ranges", ".zdebug_ranges" },
301 { ".debug_types", ".zdebug_types" },
3019eac3 302 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
303 { ".debug_frame", ".zdebug_frame" },
304 { ".eh_frame", NULL },
24d3216f
TT
305 { ".gdb_index", ".zgdb_index" },
306 23
251d32d9 307};
c906108c 308
80626a55 309/* List of DWO/DWP sections. */
3019eac3 310
80626a55 311static const struct dwop_section_names
3019eac3
DE
312{
313 struct dwarf2_section_names abbrev_dwo;
314 struct dwarf2_section_names info_dwo;
315 struct dwarf2_section_names line_dwo;
316 struct dwarf2_section_names loc_dwo;
09262596
DE
317 struct dwarf2_section_names macinfo_dwo;
318 struct dwarf2_section_names macro_dwo;
3019eac3
DE
319 struct dwarf2_section_names str_dwo;
320 struct dwarf2_section_names str_offsets_dwo;
321 struct dwarf2_section_names types_dwo;
80626a55
DE
322 struct dwarf2_section_names cu_index;
323 struct dwarf2_section_names tu_index;
3019eac3 324}
80626a55 325dwop_section_names =
3019eac3
DE
326{
327 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
328 { ".debug_info.dwo", ".zdebug_info.dwo" },
329 { ".debug_line.dwo", ".zdebug_line.dwo" },
330 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
331 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
332 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
333 { ".debug_str.dwo", ".zdebug_str.dwo" },
334 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
335 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
336 { ".debug_cu_index", ".zdebug_cu_index" },
337 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
338};
339
c906108c
SS
340/* local data types */
341
107d2387
AC
342/* The data in a compilation unit header, after target2host
343 translation, looks like this. */
c906108c 344struct comp_unit_head
a738430d 345{
c764a876 346 unsigned int length;
a738430d 347 short version;
a738430d
MK
348 unsigned char addr_size;
349 unsigned char signed_addr_p;
b64f50a1 350 sect_offset abbrev_offset;
57349743 351
a738430d
MK
352 /* Size of file offsets; either 4 or 8. */
353 unsigned int offset_size;
57349743 354
a738430d
MK
355 /* Size of the length field; either 4 or 12. */
356 unsigned int initial_length_size;
57349743 357
a738430d
MK
358 /* Offset to the first byte of this compilation unit header in the
359 .debug_info section, for resolving relative reference dies. */
b64f50a1 360 sect_offset offset;
57349743 361
d00adf39
DE
362 /* Offset to first die in this cu from the start of the cu.
363 This will be the first byte following the compilation unit header. */
b64f50a1 364 cu_offset first_die_offset;
a738430d 365};
c906108c 366
3da10d80
KS
367/* Type used for delaying computation of method physnames.
368 See comments for compute_delayed_physnames. */
369struct delayed_method_info
370{
371 /* The type to which the method is attached, i.e., its parent class. */
372 struct type *type;
373
374 /* The index of the method in the type's function fieldlists. */
375 int fnfield_index;
376
377 /* The index of the method in the fieldlist. */
378 int index;
379
380 /* The name of the DIE. */
381 const char *name;
382
383 /* The DIE associated with this method. */
384 struct die_info *die;
385};
386
387typedef struct delayed_method_info delayed_method_info;
388DEF_VEC_O (delayed_method_info);
389
e7c27a73
DJ
390/* Internal state when decoding a particular compilation unit. */
391struct dwarf2_cu
392{
393 /* The objfile containing this compilation unit. */
394 struct objfile *objfile;
395
d00adf39 396 /* The header of the compilation unit. */
e7c27a73 397 struct comp_unit_head header;
e142c38c 398
d00adf39
DE
399 /* Base address of this compilation unit. */
400 CORE_ADDR base_address;
401
402 /* Non-zero if base_address has been set. */
403 int base_known;
404
e142c38c
DJ
405 /* The language we are debugging. */
406 enum language language;
407 const struct language_defn *language_defn;
408
b0f35d58
DL
409 const char *producer;
410
e142c38c
DJ
411 /* The generic symbol table building routines have separate lists for
412 file scope symbols and all all other scopes (local scopes). So
413 we need to select the right one to pass to add_symbol_to_list().
414 We do it by keeping a pointer to the correct list in list_in_scope.
415
416 FIXME: The original dwarf code just treated the file scope as the
417 first local scope, and all other local scopes as nested local
418 scopes, and worked fine. Check to see if we really need to
419 distinguish these in buildsym.c. */
420 struct pending **list_in_scope;
421
433df2d4
DE
422 /* The abbrev table for this CU.
423 Normally this points to the abbrev table in the objfile.
424 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
425 struct abbrev_table *abbrev_table;
72bf9492 426
b64f50a1
JK
427 /* Hash table holding all the loaded partial DIEs
428 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
429 htab_t partial_dies;
430
431 /* Storage for things with the same lifetime as this read-in compilation
432 unit, including partial DIEs. */
433 struct obstack comp_unit_obstack;
434
ae038cb0
DJ
435 /* When multiple dwarf2_cu structures are living in memory, this field
436 chains them all together, so that they can be released efficiently.
437 We will probably also want a generation counter so that most-recently-used
438 compilation units are cached... */
439 struct dwarf2_per_cu_data *read_in_chain;
440
441 /* Backchain to our per_cu entry if the tree has been built. */
442 struct dwarf2_per_cu_data *per_cu;
443
444 /* How many compilation units ago was this CU last referenced? */
445 int last_used;
446
b64f50a1
JK
447 /* A hash table of DIE cu_offset for following references with
448 die_info->offset.sect_off as hash. */
51545339 449 htab_t die_hash;
10b3939b
DJ
450
451 /* Full DIEs if read in. */
452 struct die_info *dies;
453
454 /* A set of pointers to dwarf2_per_cu_data objects for compilation
455 units referenced by this one. Only set during full symbol processing;
456 partial symbol tables do not have dependencies. */
457 htab_t dependencies;
458
cb1df416
DJ
459 /* Header data from the line table, during full symbol processing. */
460 struct line_header *line_header;
461
3da10d80
KS
462 /* A list of methods which need to have physnames computed
463 after all type information has been read. */
464 VEC (delayed_method_info) *method_list;
465
96408a79
SA
466 /* To be copied to symtab->call_site_htab. */
467 htab_t call_site_htab;
468
034e5797
DE
469 /* Non-NULL if this CU came from a DWO file.
470 There is an invariant here that is important to remember:
471 Except for attributes copied from the top level DIE in the "main"
472 (or "stub") file in preparation for reading the DWO file
473 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
474 Either there isn't a DWO file (in which case this is NULL and the point
475 is moot), or there is and either we're not going to read it (in which
476 case this is NULL) or there is and we are reading it (in which case this
477 is non-NULL). */
3019eac3
DE
478 struct dwo_unit *dwo_unit;
479
480 /* The DW_AT_addr_base attribute if present, zero otherwise
481 (zero is a valid value though).
482 Note this value comes from the stub CU/TU's DIE. */
483 ULONGEST addr_base;
484
2e3cf129
DE
485 /* The DW_AT_ranges_base attribute if present, zero otherwise
486 (zero is a valid value though).
487 Note this value comes from the stub CU/TU's DIE.
488 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
489 be used without needing to know whether DWO files are in use or not.
490 N.B. This does not apply to DW_AT_ranges appearing in
491 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
492 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
493 DW_AT_ranges_base *would* have to be applied, and we'd have to care
494 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
495 ULONGEST ranges_base;
496
ae038cb0
DJ
497 /* Mark used when releasing cached dies. */
498 unsigned int mark : 1;
499
8be455d7
JK
500 /* This CU references .debug_loc. See the symtab->locations_valid field.
501 This test is imperfect as there may exist optimized debug code not using
502 any location list and still facing inlining issues if handled as
503 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 504 unsigned int has_loclist : 1;
ba919b58 505
1b80a9fa
JK
506 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
507 if all the producer_is_* fields are valid. This information is cached
508 because profiling CU expansion showed excessive time spent in
509 producer_is_gxx_lt_4_6. */
ba919b58
TT
510 unsigned int checked_producer : 1;
511 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 512 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 513 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
514
515 /* When set, the file that we're processing is known to have
516 debugging info for C++ namespaces. GCC 3.3.x did not produce
517 this information, but later versions do. */
518
519 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
520};
521
10b3939b
DJ
522/* Persistent data held for a compilation unit, even when not
523 processing it. We put a pointer to this structure in the
28dee7f5 524 read_symtab_private field of the psymtab. */
10b3939b 525
ae038cb0
DJ
526struct dwarf2_per_cu_data
527{
36586728 528 /* The start offset and length of this compilation unit.
45452591 529 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
530 initial_length_size.
531 If the DIE refers to a DWO file, this is always of the original die,
532 not the DWO file. */
b64f50a1 533 sect_offset offset;
36586728 534 unsigned int length;
ae038cb0
DJ
535
536 /* Flag indicating this compilation unit will be read in before
537 any of the current compilation units are processed. */
c764a876 538 unsigned int queued : 1;
ae038cb0 539
0d99eb77
DE
540 /* This flag will be set when reading partial DIEs if we need to load
541 absolutely all DIEs for this compilation unit, instead of just the ones
542 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
543 hash table and don't find it. */
544 unsigned int load_all_dies : 1;
545
0186c6a7
DE
546 /* Non-zero if this CU is from .debug_types.
547 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
548 this is non-zero. */
3019eac3
DE
549 unsigned int is_debug_types : 1;
550
36586728
TT
551 /* Non-zero if this CU is from the .dwz file. */
552 unsigned int is_dwz : 1;
553
3019eac3
DE
554 /* The section this CU/TU lives in.
555 If the DIE refers to a DWO file, this is always the original die,
556 not the DWO file. */
8a0459fd 557 struct dwarf2_section_info *section;
348e048f 558
17ea53c3
JK
559 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
560 of the CU cache it gets reset to NULL again. */
ae038cb0 561 struct dwarf2_cu *cu;
1c379e20 562
9cdd5dbd
DE
563 /* The corresponding objfile.
564 Normally we can get the objfile from dwarf2_per_objfile.
565 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
566 struct objfile *objfile;
567
568 /* When using partial symbol tables, the 'psymtab' field is active.
569 Otherwise the 'quick' field is active. */
570 union
571 {
572 /* The partial symbol table associated with this compilation unit,
95554aad 573 or NULL for unread partial units. */
9291a0cd
TT
574 struct partial_symtab *psymtab;
575
576 /* Data needed by the "quick" functions. */
577 struct dwarf2_per_cu_quick_data *quick;
578 } v;
95554aad 579
796a7ff8
DE
580 /* The CUs we import using DW_TAG_imported_unit. This is filled in
581 while reading psymtabs, used to compute the psymtab dependencies,
582 and then cleared. Then it is filled in again while reading full
583 symbols, and only deleted when the objfile is destroyed.
584
585 This is also used to work around a difference between the way gold
586 generates .gdb_index version <=7 and the way gdb does. Arguably this
587 is a gold bug. For symbols coming from TUs, gold records in the index
588 the CU that includes the TU instead of the TU itself. This breaks
589 dw2_lookup_symbol: It assumes that if the index says symbol X lives
590 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
591 will find X. Alas TUs live in their own symtab, so after expanding CU Y
592 we need to look in TU Z to find X. Fortunately, this is akin to
593 DW_TAG_imported_unit, so we just use the same mechanism: For
594 .gdb_index version <=7 this also records the TUs that the CU referred
595 to. Concurrently with this change gdb was modified to emit version 8
596 indices so we only pay a price for gold generated indices. */
597 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
598};
599
348e048f
DE
600/* Entry in the signatured_types hash table. */
601
602struct signatured_type
603{
42e7ad6c 604 /* The "per_cu" object of this type.
ac9ec31b 605 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
606 N.B.: This is the first member so that it's easy to convert pointers
607 between them. */
608 struct dwarf2_per_cu_data per_cu;
609
3019eac3 610 /* The type's signature. */
348e048f
DE
611 ULONGEST signature;
612
3019eac3 613 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
614 If this TU is a DWO stub and the definition lives in a DWO file
615 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
616 cu_offset type_offset_in_tu;
617
618 /* Offset in the section of the type's DIE.
619 If the definition lives in a DWO file, this is the offset in the
620 .debug_types.dwo section.
621 The value is zero until the actual value is known.
622 Zero is otherwise not a valid section offset. */
623 sect_offset type_offset_in_section;
0186c6a7
DE
624
625 /* Type units are grouped by their DW_AT_stmt_list entry so that they
626 can share them. This points to the containing symtab. */
627 struct type_unit_group *type_unit_group;
ac9ec31b
DE
628
629 /* The type.
630 The first time we encounter this type we fully read it in and install it
631 in the symbol tables. Subsequent times we only need the type. */
632 struct type *type;
348e048f
DE
633};
634
0186c6a7
DE
635typedef struct signatured_type *sig_type_ptr;
636DEF_VEC_P (sig_type_ptr);
637
094b34ac
DE
638/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
639 This includes type_unit_group and quick_file_names. */
640
641struct stmt_list_hash
642{
643 /* The DWO unit this table is from or NULL if there is none. */
644 struct dwo_unit *dwo_unit;
645
646 /* Offset in .debug_line or .debug_line.dwo. */
647 sect_offset line_offset;
648};
649
f4dc4d17
DE
650/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
651 an object of this type. */
652
653struct type_unit_group
654{
0186c6a7 655 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
656 To simplify things we create an artificial CU that "includes" all the
657 type units using this stmt_list so that the rest of the code still has
658 a "per_cu" handle on the symtab.
659 This PER_CU is recognized by having no section. */
8a0459fd 660#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
661 struct dwarf2_per_cu_data per_cu;
662
0186c6a7
DE
663 /* The TUs that share this DW_AT_stmt_list entry.
664 This is added to while parsing type units to build partial symtabs,
665 and is deleted afterwards and not used again. */
666 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
667
668 /* The primary symtab.
094b34ac
DE
669 Type units in a group needn't all be defined in the same source file,
670 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
671 struct symtab *primary_symtab;
672
094b34ac
DE
673 /* The data used to construct the hash key. */
674 struct stmt_list_hash hash;
f4dc4d17
DE
675
676 /* The number of symtabs from the line header.
677 The value here must match line_header.num_file_names. */
678 unsigned int num_symtabs;
679
680 /* The symbol tables for this TU (obtained from the files listed in
681 DW_AT_stmt_list).
682 WARNING: The order of entries here must match the order of entries
683 in the line header. After the first TU using this type_unit_group, the
684 line header for the subsequent TUs is recreated from this. This is done
685 because we need to use the same symtabs for each TU using the same
686 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
687 there's no guarantee the line header doesn't have duplicate entries. */
688 struct symtab **symtabs;
689};
690
80626a55 691/* These sections are what may appear in a DWO file. */
3019eac3
DE
692
693struct dwo_sections
694{
695 struct dwarf2_section_info abbrev;
3019eac3
DE
696 struct dwarf2_section_info line;
697 struct dwarf2_section_info loc;
09262596
DE
698 struct dwarf2_section_info macinfo;
699 struct dwarf2_section_info macro;
3019eac3
DE
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
80626a55
DE
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
3019eac3
DE
704 VEC (dwarf2_section_info_def) *types;
705};
706
c88ee1f0 707/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
708
709struct dwo_unit
710{
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 720 struct dwarf2_section_info *section;
3019eac3
DE
721
722 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
723 sect_offset offset;
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728};
729
80626a55
DE
730/* Data for one DWO file.
731 This includes virtual DWO files that have been packaged into a
732 DWP file. */
3019eac3
DE
733
734struct dwo_file
735{
0ac5b59e 736 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
737 For virtual DWO files the name is constructed from the section offsets
738 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
739 from related CU+TUs. */
0ac5b59e
DE
740 const char *dwo_name;
741
742 /* The DW_AT_comp_dir attribute. */
743 const char *comp_dir;
3019eac3 744
80626a55
DE
745 /* The bfd, when the file is open. Otherwise this is NULL.
746 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
747 bfd *dbfd;
3019eac3
DE
748
749 /* Section info for this file. */
750 struct dwo_sections sections;
751
19c3d4c9
DE
752 /* The CU in the file.
753 We only support one because having more than one requires hacking the
754 dwo_name of each to match, which is highly unlikely to happen.
755 Doing this means all TUs can share comp_dir: We also assume that
756 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
757 struct dwo_unit *cu;
3019eac3
DE
758
759 /* Table of TUs in the file.
760 Each element is a struct dwo_unit. */
761 htab_t tus;
762};
763
80626a55
DE
764/* These sections are what may appear in a DWP file. */
765
766struct dwp_sections
767{
768 struct dwarf2_section_info str;
769 struct dwarf2_section_info cu_index;
770 struct dwarf2_section_info tu_index;
771 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
772 by section number. We don't need to record them here. */
773};
774
775/* These sections are what may appear in a virtual DWO file. */
776
777struct virtual_dwo_sections
778{
779 struct dwarf2_section_info abbrev;
780 struct dwarf2_section_info line;
781 struct dwarf2_section_info loc;
782 struct dwarf2_section_info macinfo;
783 struct dwarf2_section_info macro;
784 struct dwarf2_section_info str_offsets;
785 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 786 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
787 struct dwarf2_section_info info_or_types;
788};
789
790/* Contents of DWP hash tables. */
791
792struct dwp_hash_table
793{
794 uint32_t nr_units, nr_slots;
795 const gdb_byte *hash_table, *unit_table, *section_pool;
796};
797
798/* Data for one DWP file. */
799
800struct dwp_file
801{
802 /* Name of the file. */
803 const char *name;
804
805 /* The bfd, when the file is open. Otherwise this is NULL. */
806 bfd *dbfd;
807
808 /* Section info for this file. */
809 struct dwp_sections sections;
810
811 /* Table of CUs in the file. */
812 const struct dwp_hash_table *cus;
813
814 /* Table of TUs in the file. */
815 const struct dwp_hash_table *tus;
816
817 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
818 htab_t loaded_cutus;
819
820 /* Table to map ELF section numbers to their sections. */
821 unsigned int num_sections;
822 asection **elf_sections;
823};
824
36586728
TT
825/* This represents a '.dwz' file. */
826
827struct dwz_file
828{
829 /* A dwz file can only contain a few sections. */
830 struct dwarf2_section_info abbrev;
831 struct dwarf2_section_info info;
832 struct dwarf2_section_info str;
833 struct dwarf2_section_info line;
834 struct dwarf2_section_info macro;
2ec9a5e0 835 struct dwarf2_section_info gdb_index;
36586728
TT
836
837 /* The dwz's BFD. */
838 bfd *dwz_bfd;
839};
840
0963b4bd
MS
841/* Struct used to pass misc. parameters to read_die_and_children, et
842 al. which are used for both .debug_info and .debug_types dies.
843 All parameters here are unchanging for the life of the call. This
dee91e82 844 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
845
846struct die_reader_specs
847{
dee91e82 848 /* die_section->asection->owner. */
93311388
DE
849 bfd* abfd;
850
851 /* The CU of the DIE we are parsing. */
852 struct dwarf2_cu *cu;
853
80626a55 854 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
855 struct dwo_file *dwo_file;
856
dee91e82 857 /* The section the die comes from.
3019eac3 858 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
859 struct dwarf2_section_info *die_section;
860
861 /* die_section->buffer. */
d521ce57 862 const gdb_byte *buffer;
f664829e
DE
863
864 /* The end of the buffer. */
865 const gdb_byte *buffer_end;
93311388
DE
866};
867
fd820528 868/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 869typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 870 const gdb_byte *info_ptr,
dee91e82
DE
871 struct die_info *comp_unit_die,
872 int has_children,
873 void *data);
874
debd256d
JB
875/* The line number information for a compilation unit (found in the
876 .debug_line section) begins with a "statement program header",
877 which contains the following information. */
878struct line_header
879{
880 unsigned int total_length;
881 unsigned short version;
882 unsigned int header_length;
883 unsigned char minimum_instruction_length;
2dc7f7b3 884 unsigned char maximum_ops_per_instruction;
debd256d
JB
885 unsigned char default_is_stmt;
886 int line_base;
887 unsigned char line_range;
888 unsigned char opcode_base;
889
890 /* standard_opcode_lengths[i] is the number of operands for the
891 standard opcode whose value is i. This means that
892 standard_opcode_lengths[0] is unused, and the last meaningful
893 element is standard_opcode_lengths[opcode_base - 1]. */
894 unsigned char *standard_opcode_lengths;
895
896 /* The include_directories table. NOTE! These strings are not
897 allocated with xmalloc; instead, they are pointers into
898 debug_line_buffer. If you try to free them, `free' will get
899 indigestion. */
900 unsigned int num_include_dirs, include_dirs_size;
d521ce57 901 const char **include_dirs;
debd256d
JB
902
903 /* The file_names table. NOTE! These strings are not allocated
904 with xmalloc; instead, they are pointers into debug_line_buffer.
905 Don't try to free them directly. */
906 unsigned int num_file_names, file_names_size;
907 struct file_entry
c906108c 908 {
d521ce57 909 const char *name;
debd256d
JB
910 unsigned int dir_index;
911 unsigned int mod_time;
912 unsigned int length;
aaa75496 913 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 914 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
915 } *file_names;
916
917 /* The start and end of the statement program following this
6502dd73 918 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 919 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 920};
c906108c
SS
921
922/* When we construct a partial symbol table entry we only
0963b4bd 923 need this much information. */
c906108c
SS
924struct partial_die_info
925 {
72bf9492 926 /* Offset of this DIE. */
b64f50a1 927 sect_offset offset;
72bf9492
DJ
928
929 /* DWARF-2 tag for this DIE. */
930 ENUM_BITFIELD(dwarf_tag) tag : 16;
931
72bf9492
DJ
932 /* Assorted flags describing the data found in this DIE. */
933 unsigned int has_children : 1;
934 unsigned int is_external : 1;
935 unsigned int is_declaration : 1;
936 unsigned int has_type : 1;
937 unsigned int has_specification : 1;
938 unsigned int has_pc_info : 1;
481860b3 939 unsigned int may_be_inlined : 1;
72bf9492
DJ
940
941 /* Flag set if the SCOPE field of this structure has been
942 computed. */
943 unsigned int scope_set : 1;
944
fa4028e9
JB
945 /* Flag set if the DIE has a byte_size attribute. */
946 unsigned int has_byte_size : 1;
947
98bfdba5
PA
948 /* Flag set if any of the DIE's children are template arguments. */
949 unsigned int has_template_arguments : 1;
950
abc72ce4
DE
951 /* Flag set if fixup_partial_die has been called on this die. */
952 unsigned int fixup_called : 1;
953
36586728
TT
954 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
955 unsigned int is_dwz : 1;
956
957 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
958 unsigned int spec_is_dwz : 1;
959
72bf9492 960 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 961 sometimes a default name for unnamed DIEs. */
15d034d0 962 const char *name;
72bf9492 963
abc72ce4
DE
964 /* The linkage name, if present. */
965 const char *linkage_name;
966
72bf9492
DJ
967 /* The scope to prepend to our children. This is generally
968 allocated on the comp_unit_obstack, so will disappear
969 when this compilation unit leaves the cache. */
15d034d0 970 const char *scope;
72bf9492 971
95554aad
TT
972 /* Some data associated with the partial DIE. The tag determines
973 which field is live. */
974 union
975 {
976 /* The location description associated with this DIE, if any. */
977 struct dwarf_block *locdesc;
978 /* The offset of an import, for DW_TAG_imported_unit. */
979 sect_offset offset;
980 } d;
72bf9492
DJ
981
982 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
983 CORE_ADDR lowpc;
984 CORE_ADDR highpc;
72bf9492 985
93311388 986 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 987 DW_AT_sibling, if any. */
abc72ce4
DE
988 /* NOTE: This member isn't strictly necessary, read_partial_die could
989 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 990 const gdb_byte *sibling;
72bf9492
DJ
991
992 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
993 DW_AT_specification (or DW_AT_abstract_origin or
994 DW_AT_extension). */
b64f50a1 995 sect_offset spec_offset;
72bf9492
DJ
996
997 /* Pointers to this DIE's parent, first child, and next sibling,
998 if any. */
999 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1000 };
1001
0963b4bd 1002/* This data structure holds the information of an abbrev. */
c906108c
SS
1003struct abbrev_info
1004 {
1005 unsigned int number; /* number identifying abbrev */
1006 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1007 unsigned short has_children; /* boolean */
1008 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1009 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1010 struct abbrev_info *next; /* next in chain */
1011 };
1012
1013struct attr_abbrev
1014 {
9d25dd43
DE
1015 ENUM_BITFIELD(dwarf_attribute) name : 16;
1016 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1017 };
1018
433df2d4
DE
1019/* Size of abbrev_table.abbrev_hash_table. */
1020#define ABBREV_HASH_SIZE 121
1021
1022/* Top level data structure to contain an abbreviation table. */
1023
1024struct abbrev_table
1025{
f4dc4d17
DE
1026 /* Where the abbrev table came from.
1027 This is used as a sanity check when the table is used. */
433df2d4
DE
1028 sect_offset offset;
1029
1030 /* Storage for the abbrev table. */
1031 struct obstack abbrev_obstack;
1032
1033 /* Hash table of abbrevs.
1034 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1035 It could be statically allocated, but the previous code didn't so we
1036 don't either. */
1037 struct abbrev_info **abbrevs;
1038};
1039
0963b4bd 1040/* Attributes have a name and a value. */
b60c80d6
DJ
1041struct attribute
1042 {
9d25dd43 1043 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1044 ENUM_BITFIELD(dwarf_form) form : 15;
1045
1046 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1047 field should be in u.str (existing only for DW_STRING) but it is kept
1048 here for better struct attribute alignment. */
1049 unsigned int string_is_canonical : 1;
1050
b60c80d6
DJ
1051 union
1052 {
15d034d0 1053 const char *str;
b60c80d6 1054 struct dwarf_block *blk;
43bbcdc2
PH
1055 ULONGEST unsnd;
1056 LONGEST snd;
b60c80d6 1057 CORE_ADDR addr;
ac9ec31b 1058 ULONGEST signature;
b60c80d6
DJ
1059 }
1060 u;
1061 };
1062
0963b4bd 1063/* This data structure holds a complete die structure. */
c906108c
SS
1064struct die_info
1065 {
76815b17
DE
1066 /* DWARF-2 tag for this DIE. */
1067 ENUM_BITFIELD(dwarf_tag) tag : 16;
1068
1069 /* Number of attributes */
98bfdba5
PA
1070 unsigned char num_attrs;
1071
1072 /* True if we're presently building the full type name for the
1073 type derived from this DIE. */
1074 unsigned char building_fullname : 1;
76815b17
DE
1075
1076 /* Abbrev number */
1077 unsigned int abbrev;
1078
93311388 1079 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1080 sect_offset offset;
78ba4af6
JB
1081
1082 /* The dies in a compilation unit form an n-ary tree. PARENT
1083 points to this die's parent; CHILD points to the first child of
1084 this node; and all the children of a given node are chained
4950bc1c 1085 together via their SIBLING fields. */
639d11d3
DC
1086 struct die_info *child; /* Its first child, if any. */
1087 struct die_info *sibling; /* Its next sibling, if any. */
1088 struct die_info *parent; /* Its parent, if any. */
c906108c 1089
b60c80d6
DJ
1090 /* An array of attributes, with NUM_ATTRS elements. There may be
1091 zero, but it's not common and zero-sized arrays are not
1092 sufficiently portable C. */
1093 struct attribute attrs[1];
c906108c
SS
1094 };
1095
0963b4bd 1096/* Get at parts of an attribute structure. */
c906108c
SS
1097
1098#define DW_STRING(attr) ((attr)->u.str)
8285870a 1099#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1100#define DW_UNSND(attr) ((attr)->u.unsnd)
1101#define DW_BLOCK(attr) ((attr)->u.blk)
1102#define DW_SND(attr) ((attr)->u.snd)
1103#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1104#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1105
0963b4bd 1106/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1107struct dwarf_block
1108 {
56eb65bd 1109 size_t size;
1d6edc3c
JK
1110
1111 /* Valid only if SIZE is not zero. */
d521ce57 1112 const gdb_byte *data;
c906108c
SS
1113 };
1114
c906108c
SS
1115#ifndef ATTR_ALLOC_CHUNK
1116#define ATTR_ALLOC_CHUNK 4
1117#endif
1118
c906108c
SS
1119/* Allocate fields for structs, unions and enums in this size. */
1120#ifndef DW_FIELD_ALLOC_CHUNK
1121#define DW_FIELD_ALLOC_CHUNK 4
1122#endif
1123
c906108c
SS
1124/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1125 but this would require a corresponding change in unpack_field_as_long
1126 and friends. */
1127static int bits_per_byte = 8;
1128
1129/* The routines that read and process dies for a C struct or C++ class
1130 pass lists of data member fields and lists of member function fields
1131 in an instance of a field_info structure, as defined below. */
1132struct field_info
c5aa993b 1133 {
0963b4bd 1134 /* List of data member and baseclasses fields. */
c5aa993b
JM
1135 struct nextfield
1136 {
1137 struct nextfield *next;
1138 int accessibility;
1139 int virtuality;
1140 struct field field;
1141 }
7d0ccb61 1142 *fields, *baseclasses;
c906108c 1143
7d0ccb61 1144 /* Number of fields (including baseclasses). */
c5aa993b 1145 int nfields;
c906108c 1146
c5aa993b
JM
1147 /* Number of baseclasses. */
1148 int nbaseclasses;
c906108c 1149
c5aa993b
JM
1150 /* Set if the accesibility of one of the fields is not public. */
1151 int non_public_fields;
c906108c 1152
c5aa993b
JM
1153 /* Member function fields array, entries are allocated in the order they
1154 are encountered in the object file. */
1155 struct nextfnfield
1156 {
1157 struct nextfnfield *next;
1158 struct fn_field fnfield;
1159 }
1160 *fnfields;
c906108c 1161
c5aa993b
JM
1162 /* Member function fieldlist array, contains name of possibly overloaded
1163 member function, number of overloaded member functions and a pointer
1164 to the head of the member function field chain. */
1165 struct fnfieldlist
1166 {
15d034d0 1167 const char *name;
c5aa993b
JM
1168 int length;
1169 struct nextfnfield *head;
1170 }
1171 *fnfieldlists;
c906108c 1172
c5aa993b
JM
1173 /* Number of entries in the fnfieldlists array. */
1174 int nfnfields;
98751a41
JK
1175
1176 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1177 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1178 struct typedef_field_list
1179 {
1180 struct typedef_field field;
1181 struct typedef_field_list *next;
1182 }
1183 *typedef_field_list;
1184 unsigned typedef_field_list_count;
c5aa993b 1185 };
c906108c 1186
10b3939b
DJ
1187/* One item on the queue of compilation units to read in full symbols
1188 for. */
1189struct dwarf2_queue_item
1190{
1191 struct dwarf2_per_cu_data *per_cu;
95554aad 1192 enum language pretend_language;
10b3939b
DJ
1193 struct dwarf2_queue_item *next;
1194};
1195
1196/* The current queue. */
1197static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1198
ae038cb0
DJ
1199/* Loaded secondary compilation units are kept in memory until they
1200 have not been referenced for the processing of this many
1201 compilation units. Set this to zero to disable caching. Cache
1202 sizes of up to at least twenty will improve startup time for
1203 typical inter-CU-reference binaries, at an obvious memory cost. */
1204static int dwarf2_max_cache_age = 5;
920d2a44
AC
1205static void
1206show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1207 struct cmd_list_element *c, const char *value)
1208{
3e43a32a
MS
1209 fprintf_filtered (file, _("The upper bound on the age of cached "
1210 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1211 value);
1212}
1213
ae038cb0 1214
0963b4bd 1215/* Various complaints about symbol reading that don't abort the process. */
c906108c 1216
4d3c2250
KB
1217static void
1218dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1219{
4d3c2250 1220 complaint (&symfile_complaints,
e2e0b3e5 1221 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1222}
1223
25e43795
DJ
1224static void
1225dwarf2_debug_line_missing_file_complaint (void)
1226{
1227 complaint (&symfile_complaints,
1228 _(".debug_line section has line data without a file"));
1229}
1230
59205f5a
JB
1231static void
1232dwarf2_debug_line_missing_end_sequence_complaint (void)
1233{
1234 complaint (&symfile_complaints,
3e43a32a
MS
1235 _(".debug_line section has line "
1236 "program sequence without an end"));
59205f5a
JB
1237}
1238
4d3c2250
KB
1239static void
1240dwarf2_complex_location_expr_complaint (void)
2e276125 1241{
e2e0b3e5 1242 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1243}
1244
4d3c2250
KB
1245static void
1246dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1247 int arg3)
2e276125 1248{
4d3c2250 1249 complaint (&symfile_complaints,
3e43a32a
MS
1250 _("const value length mismatch for '%s', got %d, expected %d"),
1251 arg1, arg2, arg3);
4d3c2250
KB
1252}
1253
1254static void
f664829e 1255dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1256{
4d3c2250 1257 complaint (&symfile_complaints,
f664829e
DE
1258 _("debug info runs off end of %s section"
1259 " [in module %s]"),
1260 section->asection->name,
1261 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1262}
1263
1264static void
1265dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1266{
4d3c2250 1267 complaint (&symfile_complaints,
3e43a32a
MS
1268 _("macro debug info contains a "
1269 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1270 arg1);
1271}
1272
1273static void
1274dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1275{
4d3c2250 1276 complaint (&symfile_complaints,
3e43a32a
MS
1277 _("invalid attribute class or form for '%s' in '%s'"),
1278 arg1, arg2);
4d3c2250 1279}
c906108c 1280
c906108c
SS
1281/* local function prototypes */
1282
4efb68b1 1283static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1284
918dd910
JK
1285static void dwarf2_find_base_address (struct die_info *die,
1286 struct dwarf2_cu *cu);
1287
0018ea6f
DE
1288static struct partial_symtab *create_partial_symtab
1289 (struct dwarf2_per_cu_data *per_cu, const char *name);
1290
c67a9c90 1291static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1292
72bf9492
DJ
1293static void scan_partial_symbols (struct partial_die_info *,
1294 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1295 int, struct dwarf2_cu *);
c906108c 1296
72bf9492
DJ
1297static void add_partial_symbol (struct partial_die_info *,
1298 struct dwarf2_cu *);
63d06c5c 1299
72bf9492
DJ
1300static void add_partial_namespace (struct partial_die_info *pdi,
1301 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1302 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1303
5d7cb8df
JK
1304static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1305 CORE_ADDR *highpc, int need_pc,
1306 struct dwarf2_cu *cu);
1307
72bf9492
DJ
1308static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1309 struct dwarf2_cu *cu);
91c24f0a 1310
bc30ff58
JB
1311static void add_partial_subprogram (struct partial_die_info *pdi,
1312 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1313 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1314
257e7a09
YQ
1315static void dwarf2_read_symtab (struct partial_symtab *,
1316 struct objfile *);
c906108c 1317
a14ed312 1318static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1319
433df2d4
DE
1320static struct abbrev_info *abbrev_table_lookup_abbrev
1321 (const struct abbrev_table *, unsigned int);
1322
1323static struct abbrev_table *abbrev_table_read_table
1324 (struct dwarf2_section_info *, sect_offset);
1325
1326static void abbrev_table_free (struct abbrev_table *);
1327
f4dc4d17
DE
1328static void abbrev_table_free_cleanup (void *);
1329
dee91e82
DE
1330static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1331 struct dwarf2_section_info *);
c906108c 1332
f3dd6933 1333static void dwarf2_free_abbrev_table (void *);
c906108c 1334
d521ce57 1335static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1336
dee91e82 1337static struct partial_die_info *load_partial_dies
d521ce57 1338 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1339
d521ce57
TT
1340static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1341 struct partial_die_info *,
1342 struct abbrev_info *,
1343 unsigned int,
1344 const gdb_byte *);
c906108c 1345
36586728 1346static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1347 struct dwarf2_cu *);
72bf9492
DJ
1348
1349static void fixup_partial_die (struct partial_die_info *,
1350 struct dwarf2_cu *);
1351
d521ce57
TT
1352static const gdb_byte *read_attribute (const struct die_reader_specs *,
1353 struct attribute *, struct attr_abbrev *,
1354 const gdb_byte *);
a8329558 1355
a1855c1d 1356static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1357
a1855c1d 1358static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1359
a1855c1d 1360static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1361
a1855c1d 1362static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1363
a1855c1d 1364static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1365
d521ce57 1366static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1367 unsigned int *);
c906108c 1368
d521ce57 1369static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1370
1371static LONGEST read_checked_initial_length_and_offset
d521ce57 1372 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1373 unsigned int *, unsigned int *);
613e1657 1374
d521ce57
TT
1375static LONGEST read_offset (bfd *, const gdb_byte *,
1376 const struct comp_unit_head *,
c764a876
DE
1377 unsigned int *);
1378
d521ce57 1379static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1380
f4dc4d17
DE
1381static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1382 sect_offset);
1383
d521ce57 1384static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1385
d521ce57 1386static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1387
d521ce57
TT
1388static const char *read_indirect_string (bfd *, const gdb_byte *,
1389 const struct comp_unit_head *,
1390 unsigned int *);
4bdf3d34 1391
d521ce57 1392static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1393
d521ce57 1394static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1395
d521ce57 1396static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1397
d521ce57
TT
1398static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1399 const gdb_byte *,
3019eac3
DE
1400 unsigned int *);
1401
d521ce57
TT
1402static const char *read_str_index (const struct die_reader_specs *reader,
1403 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1404
e142c38c 1405static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1406
e142c38c
DJ
1407static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1408 struct dwarf2_cu *);
c906108c 1409
348e048f 1410static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1411 unsigned int);
348e048f 1412
05cf31d1
JB
1413static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1414 struct dwarf2_cu *cu);
1415
e142c38c 1416static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1417
e142c38c 1418static struct die_info *die_specification (struct die_info *die,
f2f0e013 1419 struct dwarf2_cu **);
63d06c5c 1420
debd256d
JB
1421static void free_line_header (struct line_header *lh);
1422
3019eac3
DE
1423static struct line_header *dwarf_decode_line_header (unsigned int offset,
1424 struct dwarf2_cu *cu);
debd256d 1425
f3f5162e
DE
1426static void dwarf_decode_lines (struct line_header *, const char *,
1427 struct dwarf2_cu *, struct partial_symtab *,
1428 int);
c906108c 1429
d521ce57 1430static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1431
f4dc4d17 1432static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1433 const char *, const char *, CORE_ADDR);
f4dc4d17 1434
a14ed312 1435static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1436 struct dwarf2_cu *);
c906108c 1437
34eaf542
TT
1438static struct symbol *new_symbol_full (struct die_info *, struct type *,
1439 struct dwarf2_cu *, struct symbol *);
1440
a14ed312 1441static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1442 struct dwarf2_cu *);
c906108c 1443
98bfdba5
PA
1444static void dwarf2_const_value_attr (struct attribute *attr,
1445 struct type *type,
1446 const char *name,
1447 struct obstack *obstack,
12df843f 1448 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1449 const gdb_byte **bytes,
98bfdba5 1450 struct dwarf2_locexpr_baton **baton);
2df3850c 1451
e7c27a73 1452static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1453
b4ba55a1
JB
1454static int need_gnat_info (struct dwarf2_cu *);
1455
3e43a32a
MS
1456static struct type *die_descriptive_type (struct die_info *,
1457 struct dwarf2_cu *);
b4ba55a1
JB
1458
1459static void set_descriptive_type (struct type *, struct die_info *,
1460 struct dwarf2_cu *);
1461
e7c27a73
DJ
1462static struct type *die_containing_type (struct die_info *,
1463 struct dwarf2_cu *);
c906108c 1464
673bfd45
DE
1465static struct type *lookup_die_type (struct die_info *, struct attribute *,
1466 struct dwarf2_cu *);
c906108c 1467
f792889a 1468static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1469
673bfd45
DE
1470static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1471
0d5cff50 1472static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1473
6e70227d 1474static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1475 const char *suffix, int physname,
1476 struct dwarf2_cu *cu);
63d06c5c 1477
e7c27a73 1478static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1479
348e048f
DE
1480static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1481
e7c27a73 1482static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1483
e7c27a73 1484static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1485
96408a79
SA
1486static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1487
ff013f42
JK
1488static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1489 struct dwarf2_cu *, struct partial_symtab *);
1490
a14ed312 1491static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1492 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1493 struct partial_symtab *);
c906108c 1494
fae299cd
DC
1495static void get_scope_pc_bounds (struct die_info *,
1496 CORE_ADDR *, CORE_ADDR *,
1497 struct dwarf2_cu *);
1498
801e3a5b
JB
1499static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1500 CORE_ADDR, struct dwarf2_cu *);
1501
a14ed312 1502static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1503 struct dwarf2_cu *);
c906108c 1504
a14ed312 1505static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1506 struct type *, struct dwarf2_cu *);
c906108c 1507
a14ed312 1508static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1509 struct die_info *, struct type *,
e7c27a73 1510 struct dwarf2_cu *);
c906108c 1511
a14ed312 1512static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1513 struct type *,
1514 struct dwarf2_cu *);
c906108c 1515
134d01f1 1516static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1517
e7c27a73 1518static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1519
e7c27a73 1520static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1521
5d7cb8df
JK
1522static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1523
27aa8d6a
SW
1524static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1525
f55ee35c
JK
1526static struct type *read_module_type (struct die_info *die,
1527 struct dwarf2_cu *cu);
1528
38d518c9 1529static const char *namespace_name (struct die_info *die,
e142c38c 1530 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1531
134d01f1 1532static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1533
e7c27a73 1534static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1535
6e70227d 1536static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1537 struct dwarf2_cu *);
1538
bf6af496 1539static struct die_info *read_die_and_siblings_1
d521ce57 1540 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1541 struct die_info *);
639d11d3 1542
dee91e82 1543static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1544 const gdb_byte *info_ptr,
1545 const gdb_byte **new_info_ptr,
639d11d3
DC
1546 struct die_info *parent);
1547
d521ce57
TT
1548static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1549 struct die_info **, const gdb_byte *,
1550 int *, int);
3019eac3 1551
d521ce57
TT
1552static const gdb_byte *read_full_die (const struct die_reader_specs *,
1553 struct die_info **, const gdb_byte *,
1554 int *);
93311388 1555
e7c27a73 1556static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1557
15d034d0
TT
1558static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1559 struct obstack *);
71c25dea 1560
15d034d0 1561static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1562
15d034d0 1563static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1564 struct die_info *die,
1565 struct dwarf2_cu *cu);
1566
ca69b9e6
DE
1567static const char *dwarf2_physname (const char *name, struct die_info *die,
1568 struct dwarf2_cu *cu);
1569
e142c38c 1570static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1571 struct dwarf2_cu **);
9219021c 1572
f39c6ffd 1573static const char *dwarf_tag_name (unsigned int);
c906108c 1574
f39c6ffd 1575static const char *dwarf_attr_name (unsigned int);
c906108c 1576
f39c6ffd 1577static const char *dwarf_form_name (unsigned int);
c906108c 1578
a14ed312 1579static char *dwarf_bool_name (unsigned int);
c906108c 1580
f39c6ffd 1581static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1582
f9aca02d 1583static struct die_info *sibling_die (struct die_info *);
c906108c 1584
d97bc12b
DE
1585static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1586
1587static void dump_die_for_error (struct die_info *);
1588
1589static void dump_die_1 (struct ui_file *, int level, int max_level,
1590 struct die_info *);
c906108c 1591
d97bc12b 1592/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1593
51545339 1594static void store_in_ref_table (struct die_info *,
10b3939b 1595 struct dwarf2_cu *);
c906108c 1596
93311388
DE
1597static int is_ref_attr (struct attribute *);
1598
b64f50a1 1599static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1600
43bbcdc2 1601static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1602
348e048f
DE
1603static struct die_info *follow_die_ref_or_sig (struct die_info *,
1604 struct attribute *,
1605 struct dwarf2_cu **);
1606
10b3939b
DJ
1607static struct die_info *follow_die_ref (struct die_info *,
1608 struct attribute *,
f2f0e013 1609 struct dwarf2_cu **);
c906108c 1610
348e048f
DE
1611static struct die_info *follow_die_sig (struct die_info *,
1612 struct attribute *,
1613 struct dwarf2_cu **);
1614
ac9ec31b
DE
1615static struct type *get_signatured_type (struct die_info *, ULONGEST,
1616 struct dwarf2_cu *);
1617
1618static struct type *get_DW_AT_signature_type (struct die_info *,
1619 struct attribute *,
1620 struct dwarf2_cu *);
1621
e5fe5e75 1622static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1623
52dc124a 1624static void read_signatured_type (struct signatured_type *);
348e048f 1625
f4dc4d17 1626static struct type_unit_group *get_type_unit_group
094b34ac 1627 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1628
1629static void build_type_unit_groups (die_reader_func_ftype *, void *);
1630
c906108c
SS
1631/* memory allocation interface */
1632
7b5a2f43 1633static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1634
b60c80d6 1635static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1636
09262596 1637static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1638 const char *, int);
2e276125 1639
8e19ed76
PS
1640static int attr_form_is_block (struct attribute *);
1641
3690dd37
JB
1642static int attr_form_is_section_offset (struct attribute *);
1643
1644static int attr_form_is_constant (struct attribute *);
1645
8cf6f0b1
TT
1646static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1647 struct dwarf2_loclist_baton *baton,
1648 struct attribute *attr);
1649
93e7bd98
DJ
1650static void dwarf2_symbol_mark_computed (struct attribute *attr,
1651 struct symbol *sym,
f1e6e072
TT
1652 struct dwarf2_cu *cu,
1653 int is_block);
4c2df51b 1654
d521ce57
TT
1655static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1656 const gdb_byte *info_ptr,
1657 struct abbrev_info *abbrev);
4bb7a0a7 1658
72bf9492
DJ
1659static void free_stack_comp_unit (void *);
1660
72bf9492
DJ
1661static hashval_t partial_die_hash (const void *item);
1662
1663static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1664
ae038cb0 1665static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1666 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1667
9816fde3 1668static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1669 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1670
1671static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1672 struct die_info *comp_unit_die,
1673 enum language pretend_language);
93311388 1674
68dc6402 1675static void free_heap_comp_unit (void *);
ae038cb0
DJ
1676
1677static void free_cached_comp_units (void *);
1678
1679static void age_cached_comp_units (void);
1680
dee91e82 1681static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1682
f792889a
DJ
1683static struct type *set_die_type (struct die_info *, struct type *,
1684 struct dwarf2_cu *);
1c379e20 1685
ae038cb0
DJ
1686static void create_all_comp_units (struct objfile *);
1687
0e50663e 1688static int create_all_type_units (struct objfile *);
1fd400ff 1689
95554aad
TT
1690static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1691 enum language);
10b3939b 1692
95554aad
TT
1693static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1694 enum language);
10b3939b 1695
f4dc4d17
DE
1696static void process_full_type_unit (struct dwarf2_per_cu_data *,
1697 enum language);
1698
10b3939b
DJ
1699static void dwarf2_add_dependence (struct dwarf2_cu *,
1700 struct dwarf2_per_cu_data *);
1701
ae038cb0
DJ
1702static void dwarf2_mark (struct dwarf2_cu *);
1703
1704static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1705
b64f50a1 1706static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1707 struct dwarf2_per_cu_data *);
673bfd45 1708
f792889a 1709static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1710
9291a0cd
TT
1711static void dwarf2_release_queue (void *dummy);
1712
95554aad
TT
1713static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1714 enum language pretend_language);
1715
1716static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1717 struct dwarf2_per_cu_data *per_cu,
1718 enum language pretend_language);
9291a0cd 1719
a0f42c21 1720static void process_queue (void);
9291a0cd
TT
1721
1722static void find_file_and_directory (struct die_info *die,
1723 struct dwarf2_cu *cu,
15d034d0 1724 const char **name, const char **comp_dir);
9291a0cd
TT
1725
1726static char *file_full_name (int file, struct line_header *lh,
1727 const char *comp_dir);
1728
d521ce57 1729static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1730 (struct comp_unit_head *header,
1731 struct dwarf2_section_info *section,
d521ce57 1732 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1733 int is_debug_types_section);
1734
fd820528 1735static void init_cutu_and_read_dies
f4dc4d17
DE
1736 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1737 int use_existing_cu, int keep,
3019eac3
DE
1738 die_reader_func_ftype *die_reader_func, void *data);
1739
dee91e82
DE
1740static void init_cutu_and_read_dies_simple
1741 (struct dwarf2_per_cu_data *this_cu,
1742 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1743
673bfd45 1744static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1745
3019eac3
DE
1746static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1747
1748static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1749 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1750
1751static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1752 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1753
1754static void free_dwo_file_cleanup (void *);
1755
95554aad
TT
1756static void process_cu_includes (void);
1757
1b80a9fa
JK
1758static void check_producer (struct dwarf2_cu *cu);
1759
9291a0cd
TT
1760#if WORDS_BIGENDIAN
1761
1762/* Convert VALUE between big- and little-endian. */
1763static offset_type
1764byte_swap (offset_type value)
1765{
1766 offset_type result;
1767
1768 result = (value & 0xff) << 24;
1769 result |= (value & 0xff00) << 8;
1770 result |= (value & 0xff0000) >> 8;
1771 result |= (value & 0xff000000) >> 24;
1772 return result;
1773}
1774
1775#define MAYBE_SWAP(V) byte_swap (V)
1776
1777#else
1778#define MAYBE_SWAP(V) (V)
1779#endif /* WORDS_BIGENDIAN */
1780
1781/* The suffix for an index file. */
1782#define INDEX_SUFFIX ".gdb-index"
1783
c906108c 1784/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1785 information and return true if we have enough to do something.
1786 NAMES points to the dwarf2 section names, or is NULL if the standard
1787 ELF names are used. */
c906108c
SS
1788
1789int
251d32d9
TG
1790dwarf2_has_info (struct objfile *objfile,
1791 const struct dwarf2_debug_sections *names)
c906108c 1792{
be391dca
TT
1793 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1794 if (!dwarf2_per_objfile)
1795 {
1796 /* Initialize per-objfile state. */
1797 struct dwarf2_per_objfile *data
1798 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1799
be391dca
TT
1800 memset (data, 0, sizeof (*data));
1801 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1802 dwarf2_per_objfile = data;
6502dd73 1803
251d32d9
TG
1804 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1805 (void *) names);
be391dca
TT
1806 dwarf2_per_objfile->objfile = objfile;
1807 }
1808 return (dwarf2_per_objfile->info.asection != NULL
1809 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1810}
1811
251d32d9
TG
1812/* When loading sections, we look either for uncompressed section or for
1813 compressed section names. */
233a11ab
CS
1814
1815static int
251d32d9
TG
1816section_is_p (const char *section_name,
1817 const struct dwarf2_section_names *names)
233a11ab 1818{
251d32d9
TG
1819 if (names->normal != NULL
1820 && strcmp (section_name, names->normal) == 0)
1821 return 1;
1822 if (names->compressed != NULL
1823 && strcmp (section_name, names->compressed) == 0)
1824 return 1;
1825 return 0;
233a11ab
CS
1826}
1827
c906108c
SS
1828/* This function is mapped across the sections and remembers the
1829 offset and size of each of the debugging sections we are interested
1830 in. */
1831
1832static void
251d32d9 1833dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1834{
251d32d9 1835 const struct dwarf2_debug_sections *names;
dc7650b8 1836 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1837
1838 if (vnames == NULL)
1839 names = &dwarf2_elf_names;
1840 else
1841 names = (const struct dwarf2_debug_sections *) vnames;
1842
dc7650b8
JK
1843 if ((aflag & SEC_HAS_CONTENTS) == 0)
1844 {
1845 }
1846 else if (section_is_p (sectp->name, &names->info))
c906108c 1847 {
dce234bc
PP
1848 dwarf2_per_objfile->info.asection = sectp;
1849 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1850 }
251d32d9 1851 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1852 {
dce234bc
PP
1853 dwarf2_per_objfile->abbrev.asection = sectp;
1854 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1855 }
251d32d9 1856 else if (section_is_p (sectp->name, &names->line))
c906108c 1857 {
dce234bc
PP
1858 dwarf2_per_objfile->line.asection = sectp;
1859 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1860 }
251d32d9 1861 else if (section_is_p (sectp->name, &names->loc))
c906108c 1862 {
dce234bc
PP
1863 dwarf2_per_objfile->loc.asection = sectp;
1864 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1865 }
251d32d9 1866 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1867 {
dce234bc
PP
1868 dwarf2_per_objfile->macinfo.asection = sectp;
1869 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1870 }
cf2c3c16
TT
1871 else if (section_is_p (sectp->name, &names->macro))
1872 {
1873 dwarf2_per_objfile->macro.asection = sectp;
1874 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1875 }
251d32d9 1876 else if (section_is_p (sectp->name, &names->str))
c906108c 1877 {
dce234bc
PP
1878 dwarf2_per_objfile->str.asection = sectp;
1879 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1880 }
3019eac3
DE
1881 else if (section_is_p (sectp->name, &names->addr))
1882 {
1883 dwarf2_per_objfile->addr.asection = sectp;
1884 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1885 }
251d32d9 1886 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1887 {
dce234bc
PP
1888 dwarf2_per_objfile->frame.asection = sectp;
1889 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1890 }
251d32d9 1891 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1892 {
dc7650b8
JK
1893 dwarf2_per_objfile->eh_frame.asection = sectp;
1894 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1895 }
251d32d9 1896 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1897 {
dce234bc
PP
1898 dwarf2_per_objfile->ranges.asection = sectp;
1899 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1900 }
251d32d9 1901 else if (section_is_p (sectp->name, &names->types))
348e048f 1902 {
8b70b953
TT
1903 struct dwarf2_section_info type_section;
1904
1905 memset (&type_section, 0, sizeof (type_section));
1906 type_section.asection = sectp;
1907 type_section.size = bfd_get_section_size (sectp);
1908
1909 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1910 &type_section);
348e048f 1911 }
251d32d9 1912 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1913 {
1914 dwarf2_per_objfile->gdb_index.asection = sectp;
1915 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1916 }
dce234bc 1917
72dca2f5
FR
1918 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1919 && bfd_section_vma (abfd, sectp) == 0)
1920 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1921}
1922
fceca515
DE
1923/* A helper function that decides whether a section is empty,
1924 or not present. */
9e0ac564
TT
1925
1926static int
1927dwarf2_section_empty_p (struct dwarf2_section_info *info)
1928{
1929 return info->asection == NULL || info->size == 0;
1930}
1931
3019eac3
DE
1932/* Read the contents of the section INFO.
1933 OBJFILE is the main object file, but not necessarily the file where
1934 the section comes from. E.g., for DWO files INFO->asection->owner
1935 is the bfd of the DWO file.
dce234bc 1936 If the section is compressed, uncompress it before returning. */
c906108c 1937
dce234bc
PP
1938static void
1939dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1940{
dce234bc 1941 asection *sectp = info->asection;
3019eac3 1942 bfd *abfd;
dce234bc
PP
1943 gdb_byte *buf, *retbuf;
1944 unsigned char header[4];
c906108c 1945
be391dca
TT
1946 if (info->readin)
1947 return;
dce234bc 1948 info->buffer = NULL;
be391dca 1949 info->readin = 1;
188dd5d6 1950
9e0ac564 1951 if (dwarf2_section_empty_p (info))
dce234bc 1952 return;
c906108c 1953
3019eac3
DE
1954 abfd = sectp->owner;
1955
4bf44c1c
TT
1956 /* If the section has relocations, we must read it ourselves.
1957 Otherwise we attach it to the BFD. */
1958 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1959 {
d521ce57 1960 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1961 return;
dce234bc 1962 }
dce234bc 1963
4bf44c1c
TT
1964 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1965 info->buffer = buf;
dce234bc
PP
1966
1967 /* When debugging .o files, we may need to apply relocations; see
1968 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1969 We never compress sections in .o files, so we only need to
1970 try this when the section is not compressed. */
ac8035ab 1971 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1972 if (retbuf != NULL)
1973 {
1974 info->buffer = retbuf;
1975 return;
1976 }
1977
1978 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1979 || bfd_bread (buf, info->size, abfd) != info->size)
1980 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1981 bfd_get_filename (abfd));
1982}
1983
9e0ac564
TT
1984/* A helper function that returns the size of a section in a safe way.
1985 If you are positive that the section has been read before using the
1986 size, then it is safe to refer to the dwarf2_section_info object's
1987 "size" field directly. In other cases, you must call this
1988 function, because for compressed sections the size field is not set
1989 correctly until the section has been read. */
1990
1991static bfd_size_type
1992dwarf2_section_size (struct objfile *objfile,
1993 struct dwarf2_section_info *info)
1994{
1995 if (!info->readin)
1996 dwarf2_read_section (objfile, info);
1997 return info->size;
1998}
1999
dce234bc 2000/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2001 SECTION_NAME. */
af34e669 2002
dce234bc 2003void
3017a003
TG
2004dwarf2_get_section_info (struct objfile *objfile,
2005 enum dwarf2_section_enum sect,
d521ce57 2006 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2007 bfd_size_type *sizep)
2008{
2009 struct dwarf2_per_objfile *data
2010 = objfile_data (objfile, dwarf2_objfile_data_key);
2011 struct dwarf2_section_info *info;
a3b2a86b
TT
2012
2013 /* We may see an objfile without any DWARF, in which case we just
2014 return nothing. */
2015 if (data == NULL)
2016 {
2017 *sectp = NULL;
2018 *bufp = NULL;
2019 *sizep = 0;
2020 return;
2021 }
3017a003
TG
2022 switch (sect)
2023 {
2024 case DWARF2_DEBUG_FRAME:
2025 info = &data->frame;
2026 break;
2027 case DWARF2_EH_FRAME:
2028 info = &data->eh_frame;
2029 break;
2030 default:
2031 gdb_assert_not_reached ("unexpected section");
2032 }
dce234bc 2033
9e0ac564 2034 dwarf2_read_section (objfile, info);
dce234bc
PP
2035
2036 *sectp = info->asection;
2037 *bufp = info->buffer;
2038 *sizep = info->size;
2039}
2040
36586728
TT
2041/* A helper function to find the sections for a .dwz file. */
2042
2043static void
2044locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2045{
2046 struct dwz_file *dwz_file = arg;
2047
2048 /* Note that we only support the standard ELF names, because .dwz
2049 is ELF-only (at the time of writing). */
2050 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2051 {
2052 dwz_file->abbrev.asection = sectp;
2053 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2054 }
2055 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2056 {
2057 dwz_file->info.asection = sectp;
2058 dwz_file->info.size = bfd_get_section_size (sectp);
2059 }
2060 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2061 {
2062 dwz_file->str.asection = sectp;
2063 dwz_file->str.size = bfd_get_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2066 {
2067 dwz_file->line.asection = sectp;
2068 dwz_file->line.size = bfd_get_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2071 {
2072 dwz_file->macro.asection = sectp;
2073 dwz_file->macro.size = bfd_get_section_size (sectp);
2074 }
2ec9a5e0
TT
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2076 {
2077 dwz_file->gdb_index.asection = sectp;
2078 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2079 }
36586728
TT
2080}
2081
2082/* Open the separate '.dwz' debug file, if needed. Error if the file
2083 cannot be found. */
2084
2085static struct dwz_file *
2086dwarf2_get_dwz_file (void)
2087{
2088 bfd *abfd, *dwz_bfd;
2089 asection *section;
2090 gdb_byte *data;
2091 struct cleanup *cleanup;
2092 const char *filename;
2093 struct dwz_file *result;
2094
2095 if (dwarf2_per_objfile->dwz_file != NULL)
2096 return dwarf2_per_objfile->dwz_file;
2097
2098 abfd = dwarf2_per_objfile->objfile->obfd;
2099 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2100 if (section == NULL)
2101 error (_("could not find '.gnu_debugaltlink' section"));
2102 if (!bfd_malloc_and_get_section (abfd, section, &data))
2103 error (_("could not read '.gnu_debugaltlink' section: %s"),
2104 bfd_errmsg (bfd_get_error ()));
2105 cleanup = make_cleanup (xfree, data);
2106
f9d83a0b 2107 filename = (const char *) data;
36586728
TT
2108 if (!IS_ABSOLUTE_PATH (filename))
2109 {
2110 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2111 char *rel;
2112
2113 make_cleanup (xfree, abs);
2114 abs = ldirname (abs);
2115 make_cleanup (xfree, abs);
2116
2117 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2118 make_cleanup (xfree, rel);
2119 filename = rel;
2120 }
2121
2122 /* The format is just a NUL-terminated file name, followed by the
2123 build-id. For now, though, we ignore the build-id. */
2124 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2125 if (dwz_bfd == NULL)
2126 error (_("could not read '%s': %s"), filename,
2127 bfd_errmsg (bfd_get_error ()));
2128
2129 if (!bfd_check_format (dwz_bfd, bfd_object))
2130 {
2131 gdb_bfd_unref (dwz_bfd);
2132 error (_("file '%s' was not usable: %s"), filename,
2133 bfd_errmsg (bfd_get_error ()));
2134 }
2135
2136 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2137 struct dwz_file);
2138 result->dwz_bfd = dwz_bfd;
2139
2140 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2141
2142 do_cleanups (cleanup);
2143
8d2cc612 2144 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2145 return result;
2146}
9291a0cd 2147\f
7b9f3c50
DE
2148/* DWARF quick_symbols_functions support. */
2149
2150/* TUs can share .debug_line entries, and there can be a lot more TUs than
2151 unique line tables, so we maintain a separate table of all .debug_line
2152 derived entries to support the sharing.
2153 All the quick functions need is the list of file names. We discard the
2154 line_header when we're done and don't need to record it here. */
2155struct quick_file_names
2156{
094b34ac
DE
2157 /* The data used to construct the hash key. */
2158 struct stmt_list_hash hash;
7b9f3c50
DE
2159
2160 /* The number of entries in file_names, real_names. */
2161 unsigned int num_file_names;
2162
2163 /* The file names from the line table, after being run through
2164 file_full_name. */
2165 const char **file_names;
2166
2167 /* The file names from the line table after being run through
2168 gdb_realpath. These are computed lazily. */
2169 const char **real_names;
2170};
2171
2172/* When using the index (and thus not using psymtabs), each CU has an
2173 object of this type. This is used to hold information needed by
2174 the various "quick" methods. */
2175struct dwarf2_per_cu_quick_data
2176{
2177 /* The file table. This can be NULL if there was no file table
2178 or it's currently not read in.
2179 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2180 struct quick_file_names *file_names;
2181
2182 /* The corresponding symbol table. This is NULL if symbols for this
2183 CU have not yet been read. */
2184 struct symtab *symtab;
2185
2186 /* A temporary mark bit used when iterating over all CUs in
2187 expand_symtabs_matching. */
2188 unsigned int mark : 1;
2189
2190 /* True if we've tried to read the file table and found there isn't one.
2191 There will be no point in trying to read it again next time. */
2192 unsigned int no_file_data : 1;
2193};
2194
094b34ac
DE
2195/* Utility hash function for a stmt_list_hash. */
2196
2197static hashval_t
2198hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2199{
2200 hashval_t v = 0;
2201
2202 if (stmt_list_hash->dwo_unit != NULL)
2203 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2204 v += stmt_list_hash->line_offset.sect_off;
2205 return v;
2206}
2207
2208/* Utility equality function for a stmt_list_hash. */
2209
2210static int
2211eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2212 const struct stmt_list_hash *rhs)
2213{
2214 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2215 return 0;
2216 if (lhs->dwo_unit != NULL
2217 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2218 return 0;
2219
2220 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2221}
2222
7b9f3c50
DE
2223/* Hash function for a quick_file_names. */
2224
2225static hashval_t
2226hash_file_name_entry (const void *e)
2227{
2228 const struct quick_file_names *file_data = e;
2229
094b34ac 2230 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2231}
2232
2233/* Equality function for a quick_file_names. */
2234
2235static int
2236eq_file_name_entry (const void *a, const void *b)
2237{
2238 const struct quick_file_names *ea = a;
2239 const struct quick_file_names *eb = b;
2240
094b34ac 2241 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2242}
2243
2244/* Delete function for a quick_file_names. */
2245
2246static void
2247delete_file_name_entry (void *e)
2248{
2249 struct quick_file_names *file_data = e;
2250 int i;
2251
2252 for (i = 0; i < file_data->num_file_names; ++i)
2253 {
2254 xfree ((void*) file_data->file_names[i]);
2255 if (file_data->real_names)
2256 xfree ((void*) file_data->real_names[i]);
2257 }
2258
2259 /* The space for the struct itself lives on objfile_obstack,
2260 so we don't free it here. */
2261}
2262
2263/* Create a quick_file_names hash table. */
2264
2265static htab_t
2266create_quick_file_names_table (unsigned int nr_initial_entries)
2267{
2268 return htab_create_alloc (nr_initial_entries,
2269 hash_file_name_entry, eq_file_name_entry,
2270 delete_file_name_entry, xcalloc, xfree);
2271}
9291a0cd 2272
918dd910
JK
2273/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2274 have to be created afterwards. You should call age_cached_comp_units after
2275 processing PER_CU->CU. dw2_setup must have been already called. */
2276
2277static void
2278load_cu (struct dwarf2_per_cu_data *per_cu)
2279{
3019eac3 2280 if (per_cu->is_debug_types)
e5fe5e75 2281 load_full_type_unit (per_cu);
918dd910 2282 else
95554aad 2283 load_full_comp_unit (per_cu, language_minimal);
918dd910 2284
918dd910 2285 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2286
2287 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2288}
2289
a0f42c21 2290/* Read in the symbols for PER_CU. */
2fdf6df6 2291
9291a0cd 2292static void
a0f42c21 2293dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2294{
2295 struct cleanup *back_to;
2296
f4dc4d17
DE
2297 /* Skip type_unit_groups, reading the type units they contain
2298 is handled elsewhere. */
2299 if (IS_TYPE_UNIT_GROUP (per_cu))
2300 return;
2301
9291a0cd
TT
2302 back_to = make_cleanup (dwarf2_release_queue, NULL);
2303
95554aad
TT
2304 if (dwarf2_per_objfile->using_index
2305 ? per_cu->v.quick->symtab == NULL
2306 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2307 {
2308 queue_comp_unit (per_cu, language_minimal);
2309 load_cu (per_cu);
2310 }
9291a0cd 2311
a0f42c21 2312 process_queue ();
9291a0cd
TT
2313
2314 /* Age the cache, releasing compilation units that have not
2315 been used recently. */
2316 age_cached_comp_units ();
2317
2318 do_cleanups (back_to);
2319}
2320
2321/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2322 the objfile from which this CU came. Returns the resulting symbol
2323 table. */
2fdf6df6 2324
9291a0cd 2325static struct symtab *
a0f42c21 2326dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2327{
95554aad 2328 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2329 if (!per_cu->v.quick->symtab)
2330 {
2331 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2332 increment_reading_symtab ();
a0f42c21 2333 dw2_do_instantiate_symtab (per_cu);
95554aad 2334 process_cu_includes ();
9291a0cd
TT
2335 do_cleanups (back_to);
2336 }
2337 return per_cu->v.quick->symtab;
2338}
2339
f4dc4d17
DE
2340/* Return the CU given its index.
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_units); ++i)
2346 {
2347 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2348
2349 ...;
2350 }
2351*/
2fdf6df6 2352
1fd400ff
TT
2353static struct dwarf2_per_cu_data *
2354dw2_get_cu (int index)
2355{
2356 if (index >= dwarf2_per_objfile->n_comp_units)
2357 {
f4dc4d17 2358 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2359 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2360 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2361 }
2362
2363 return dwarf2_per_objfile->all_comp_units[index];
2364}
2365
2366/* Return the primary CU given its index.
2367 The difference between this function and dw2_get_cu is in the handling
2368 of type units (TUs). Here we return the type_unit_group object.
2369
2370 This is intended for loops like:
2371
2372 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2373 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2374 {
2375 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2376
2377 ...;
2378 }
2379*/
2380
2381static struct dwarf2_per_cu_data *
2382dw2_get_primary_cu (int index)
2383{
2384 if (index >= dwarf2_per_objfile->n_comp_units)
2385 {
1fd400ff 2386 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2387 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2388 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2389 }
f4dc4d17 2390
1fd400ff
TT
2391 return dwarf2_per_objfile->all_comp_units[index];
2392}
2393
2ec9a5e0
TT
2394/* A helper for create_cus_from_index that handles a given list of
2395 CUs. */
2fdf6df6 2396
74a0d9f6 2397static void
2ec9a5e0
TT
2398create_cus_from_index_list (struct objfile *objfile,
2399 const gdb_byte *cu_list, offset_type n_elements,
2400 struct dwarf2_section_info *section,
2401 int is_dwz,
2402 int base_offset)
9291a0cd
TT
2403{
2404 offset_type i;
9291a0cd 2405
2ec9a5e0 2406 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2407 {
2408 struct dwarf2_per_cu_data *the_cu;
2409 ULONGEST offset, length;
2410
74a0d9f6
JK
2411 gdb_static_assert (sizeof (ULONGEST) >= 8);
2412 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2413 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2414 cu_list += 2 * 8;
2415
2416 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2417 struct dwarf2_per_cu_data);
b64f50a1 2418 the_cu->offset.sect_off = offset;
9291a0cd
TT
2419 the_cu->length = length;
2420 the_cu->objfile = objfile;
8a0459fd 2421 the_cu->section = section;
9291a0cd
TT
2422 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2423 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2424 the_cu->is_dwz = is_dwz;
2425 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2426 }
9291a0cd
TT
2427}
2428
2ec9a5e0 2429/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2430 the CU objects for this objfile. */
2ec9a5e0 2431
74a0d9f6 2432static void
2ec9a5e0
TT
2433create_cus_from_index (struct objfile *objfile,
2434 const gdb_byte *cu_list, offset_type cu_list_elements,
2435 const gdb_byte *dwz_list, offset_type dwz_elements)
2436{
2437 struct dwz_file *dwz;
2438
2439 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2440 dwarf2_per_objfile->all_comp_units
2441 = obstack_alloc (&objfile->objfile_obstack,
2442 dwarf2_per_objfile->n_comp_units
2443 * sizeof (struct dwarf2_per_cu_data *));
2444
74a0d9f6
JK
2445 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2446 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2447
2448 if (dwz_elements == 0)
74a0d9f6 2449 return;
2ec9a5e0
TT
2450
2451 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2452 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2453 cu_list_elements / 2);
2ec9a5e0
TT
2454}
2455
1fd400ff 2456/* Create the signatured type hash table from the index. */
673bfd45 2457
74a0d9f6 2458static void
673bfd45 2459create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2460 struct dwarf2_section_info *section,
673bfd45
DE
2461 const gdb_byte *bytes,
2462 offset_type elements)
1fd400ff
TT
2463{
2464 offset_type i;
673bfd45 2465 htab_t sig_types_hash;
1fd400ff 2466
d467dd73
DE
2467 dwarf2_per_objfile->n_type_units = elements / 3;
2468 dwarf2_per_objfile->all_type_units
1fd400ff 2469 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2470 dwarf2_per_objfile->n_type_units
b4dd5633 2471 * sizeof (struct signatured_type *));
1fd400ff 2472
673bfd45 2473 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2474
2475 for (i = 0; i < elements; i += 3)
2476 {
52dc124a
DE
2477 struct signatured_type *sig_type;
2478 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2479 void **slot;
2480
74a0d9f6
JK
2481 gdb_static_assert (sizeof (ULONGEST) >= 8);
2482 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2483 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2484 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2485 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2486 bytes += 3 * 8;
2487
52dc124a 2488 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2489 struct signatured_type);
52dc124a 2490 sig_type->signature = signature;
3019eac3
DE
2491 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2492 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2493 sig_type->per_cu.section = section;
52dc124a
DE
2494 sig_type->per_cu.offset.sect_off = offset;
2495 sig_type->per_cu.objfile = objfile;
2496 sig_type->per_cu.v.quick
1fd400ff
TT
2497 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2498 struct dwarf2_per_cu_quick_data);
2499
52dc124a
DE
2500 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2501 *slot = sig_type;
1fd400ff 2502
b4dd5633 2503 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2504 }
2505
673bfd45 2506 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2507}
2508
9291a0cd
TT
2509/* Read the address map data from the mapped index, and use it to
2510 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2511
9291a0cd
TT
2512static void
2513create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2514{
2515 const gdb_byte *iter, *end;
2516 struct obstack temp_obstack;
2517 struct addrmap *mutable_map;
2518 struct cleanup *cleanup;
2519 CORE_ADDR baseaddr;
2520
2521 obstack_init (&temp_obstack);
2522 cleanup = make_cleanup_obstack_free (&temp_obstack);
2523 mutable_map = addrmap_create_mutable (&temp_obstack);
2524
2525 iter = index->address_table;
2526 end = iter + index->address_table_size;
2527
2528 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2529
2530 while (iter < end)
2531 {
2532 ULONGEST hi, lo, cu_index;
2533 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2534 iter += 8;
2535 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2536 iter += 8;
2537 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2538 iter += 4;
f652bce2
DE
2539
2540 if (cu_index < dwarf2_per_objfile->n_comp_units)
2541 {
2542 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2543 dw2_get_cu (cu_index));
2544 }
2545 else
2546 {
2547 complaint (&symfile_complaints,
2548 _(".gdb_index address table has invalid CU number %u"),
2549 (unsigned) cu_index);
2550 }
9291a0cd
TT
2551 }
2552
2553 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2554 &objfile->objfile_obstack);
2555 do_cleanups (cleanup);
2556}
2557
59d7bcaf
JK
2558/* The hash function for strings in the mapped index. This is the same as
2559 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2560 implementation. This is necessary because the hash function is tied to the
2561 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2562 SYMBOL_HASH_NEXT.
2563
2564 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2565
9291a0cd 2566static hashval_t
559a7a62 2567mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2568{
2569 const unsigned char *str = (const unsigned char *) p;
2570 hashval_t r = 0;
2571 unsigned char c;
2572
2573 while ((c = *str++) != 0)
559a7a62
JK
2574 {
2575 if (index_version >= 5)
2576 c = tolower (c);
2577 r = r * 67 + c - 113;
2578 }
9291a0cd
TT
2579
2580 return r;
2581}
2582
2583/* Find a slot in the mapped index INDEX for the object named NAME.
2584 If NAME is found, set *VEC_OUT to point to the CU vector in the
2585 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2586
9291a0cd
TT
2587static int
2588find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2589 offset_type **vec_out)
2590{
0cf03b49
JK
2591 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2592 offset_type hash;
9291a0cd 2593 offset_type slot, step;
559a7a62 2594 int (*cmp) (const char *, const char *);
9291a0cd 2595
0cf03b49
JK
2596 if (current_language->la_language == language_cplus
2597 || current_language->la_language == language_java
2598 || current_language->la_language == language_fortran)
2599 {
2600 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2601 not contain any. */
2602 const char *paren = strchr (name, '(');
2603
2604 if (paren)
2605 {
2606 char *dup;
2607
2608 dup = xmalloc (paren - name + 1);
2609 memcpy (dup, name, paren - name);
2610 dup[paren - name] = 0;
2611
2612 make_cleanup (xfree, dup);
2613 name = dup;
2614 }
2615 }
2616
559a7a62 2617 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2618 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2619 simulate our NAME being searched is also lowercased. */
2620 hash = mapped_index_string_hash ((index->version == 4
2621 && case_sensitivity == case_sensitive_off
2622 ? 5 : index->version),
2623 name);
2624
3876f04e
DE
2625 slot = hash & (index->symbol_table_slots - 1);
2626 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2627 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2628
2629 for (;;)
2630 {
2631 /* Convert a slot number to an offset into the table. */
2632 offset_type i = 2 * slot;
2633 const char *str;
3876f04e 2634 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2635 {
2636 do_cleanups (back_to);
2637 return 0;
2638 }
9291a0cd 2639
3876f04e 2640 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2641 if (!cmp (name, str))
9291a0cd
TT
2642 {
2643 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2644 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2645 do_cleanups (back_to);
9291a0cd
TT
2646 return 1;
2647 }
2648
3876f04e 2649 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2650 }
2651}
2652
2ec9a5e0
TT
2653/* A helper function that reads the .gdb_index from SECTION and fills
2654 in MAP. FILENAME is the name of the file containing the section;
2655 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2656 ok to use deprecated sections.
2657
2658 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2659 out parameters that are filled in with information about the CU and
2660 TU lists in the section.
2661
2662 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2663
9291a0cd 2664static int
2ec9a5e0
TT
2665read_index_from_section (struct objfile *objfile,
2666 const char *filename,
2667 int deprecated_ok,
2668 struct dwarf2_section_info *section,
2669 struct mapped_index *map,
2670 const gdb_byte **cu_list,
2671 offset_type *cu_list_elements,
2672 const gdb_byte **types_list,
2673 offset_type *types_list_elements)
9291a0cd 2674{
948f8e3d 2675 const gdb_byte *addr;
2ec9a5e0 2676 offset_type version;
b3b272e1 2677 offset_type *metadata;
1fd400ff 2678 int i;
9291a0cd 2679
2ec9a5e0 2680 if (dwarf2_section_empty_p (section))
9291a0cd 2681 return 0;
82430852
JK
2682
2683 /* Older elfutils strip versions could keep the section in the main
2684 executable while splitting it for the separate debug info file. */
2ec9a5e0 2685 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2686 return 0;
2687
2ec9a5e0 2688 dwarf2_read_section (objfile, section);
9291a0cd 2689
2ec9a5e0 2690 addr = section->buffer;
9291a0cd 2691 /* Version check. */
1fd400ff 2692 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2693 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2694 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2695 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2696 indices. */
831adc1f 2697 if (version < 4)
481860b3
GB
2698 {
2699 static int warning_printed = 0;
2700 if (!warning_printed)
2701 {
2702 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2703 filename);
481860b3
GB
2704 warning_printed = 1;
2705 }
2706 return 0;
2707 }
2708 /* Index version 4 uses a different hash function than index version
2709 5 and later.
2710
2711 Versions earlier than 6 did not emit psymbols for inlined
2712 functions. Using these files will cause GDB not to be able to
2713 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2714 indices unless the user has done
2715 "set use-deprecated-index-sections on". */
2ec9a5e0 2716 if (version < 6 && !deprecated_ok)
481860b3
GB
2717 {
2718 static int warning_printed = 0;
2719 if (!warning_printed)
2720 {
e615022a
DE
2721 warning (_("\
2722Skipping deprecated .gdb_index section in %s.\n\
2723Do \"set use-deprecated-index-sections on\" before the file is read\n\
2724to use the section anyway."),
2ec9a5e0 2725 filename);
481860b3
GB
2726 warning_printed = 1;
2727 }
2728 return 0;
2729 }
796a7ff8
DE
2730 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2731 of the TU (for symbols coming from TUs). It's just a performance bug, and
2732 we can't distinguish gdb-generated indices from gold-generated ones, so
2733 nothing to do here. */
2734
481860b3 2735 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2736 longer backward compatible. */
796a7ff8 2737 if (version > 8)
594e8718 2738 return 0;
9291a0cd 2739
559a7a62 2740 map->version = version;
2ec9a5e0 2741 map->total_size = section->size;
9291a0cd
TT
2742
2743 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2744
2745 i = 0;
2ec9a5e0
TT
2746 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2747 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2748 / 8);
1fd400ff
TT
2749 ++i;
2750
2ec9a5e0
TT
2751 *types_list = addr + MAYBE_SWAP (metadata[i]);
2752 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2753 - MAYBE_SWAP (metadata[i]))
2754 / 8);
987d643c 2755 ++i;
1fd400ff
TT
2756
2757 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2758 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2759 - MAYBE_SWAP (metadata[i]));
2760 ++i;
2761
3876f04e
DE
2762 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2763 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2764 - MAYBE_SWAP (metadata[i]))
2765 / (2 * sizeof (offset_type)));
1fd400ff 2766 ++i;
9291a0cd 2767
f9d83a0b 2768 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2769
2ec9a5e0
TT
2770 return 1;
2771}
2772
2773
2774/* Read the index file. If everything went ok, initialize the "quick"
2775 elements of all the CUs and return 1. Otherwise, return 0. */
2776
2777static int
2778dwarf2_read_index (struct objfile *objfile)
2779{
2780 struct mapped_index local_map, *map;
2781 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2782 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2783
2784 if (!read_index_from_section (objfile, objfile->name,
2785 use_deprecated_index_sections,
2786 &dwarf2_per_objfile->gdb_index, &local_map,
2787 &cu_list, &cu_list_elements,
2788 &types_list, &types_list_elements))
2789 return 0;
2790
0fefef59 2791 /* Don't use the index if it's empty. */
2ec9a5e0 2792 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2793 return 0;
2794
2ec9a5e0
TT
2795 /* If there is a .dwz file, read it so we can get its CU list as
2796 well. */
2797 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2798 {
2799 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2800 struct mapped_index dwz_map;
2801 const gdb_byte *dwz_types_ignore;
2802 offset_type dwz_types_elements_ignore;
2803
2804 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2805 1,
2806 &dwz->gdb_index, &dwz_map,
2807 &dwz_list, &dwz_list_elements,
2808 &dwz_types_ignore,
2809 &dwz_types_elements_ignore))
2810 {
2811 warning (_("could not read '.gdb_index' section from %s; skipping"),
2812 bfd_get_filename (dwz->dwz_bfd));
2813 return 0;
2814 }
2815 }
2816
74a0d9f6
JK
2817 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2818 dwz_list_elements);
1fd400ff 2819
8b70b953
TT
2820 if (types_list_elements)
2821 {
2822 struct dwarf2_section_info *section;
2823
2824 /* We can only handle a single .debug_types when we have an
2825 index. */
2826 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2827 return 0;
2828
2829 section = VEC_index (dwarf2_section_info_def,
2830 dwarf2_per_objfile->types, 0);
2831
74a0d9f6
JK
2832 create_signatured_type_table_from_index (objfile, section, types_list,
2833 types_list_elements);
8b70b953 2834 }
9291a0cd 2835
2ec9a5e0
TT
2836 create_addrmap_from_index (objfile, &local_map);
2837
2838 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2839 *map = local_map;
9291a0cd
TT
2840
2841 dwarf2_per_objfile->index_table = map;
2842 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2843 dwarf2_per_objfile->quick_file_names_table =
2844 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2845
2846 return 1;
2847}
2848
2849/* A helper for the "quick" functions which sets the global
2850 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2851
9291a0cd
TT
2852static void
2853dw2_setup (struct objfile *objfile)
2854{
2855 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2856 gdb_assert (dwarf2_per_objfile);
2857}
2858
dee91e82 2859/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2860
dee91e82
DE
2861static void
2862dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2863 const gdb_byte *info_ptr,
dee91e82
DE
2864 struct die_info *comp_unit_die,
2865 int has_children,
2866 void *data)
9291a0cd 2867{
dee91e82
DE
2868 struct dwarf2_cu *cu = reader->cu;
2869 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2870 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2871 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2872 struct line_header *lh;
9291a0cd 2873 struct attribute *attr;
dee91e82 2874 int i;
15d034d0 2875 const char *name, *comp_dir;
7b9f3c50
DE
2876 void **slot;
2877 struct quick_file_names *qfn;
2878 unsigned int line_offset;
9291a0cd 2879
0186c6a7
DE
2880 gdb_assert (! this_cu->is_debug_types);
2881
07261596
TT
2882 /* Our callers never want to match partial units -- instead they
2883 will match the enclosing full CU. */
2884 if (comp_unit_die->tag == DW_TAG_partial_unit)
2885 {
2886 this_cu->v.quick->no_file_data = 1;
2887 return;
2888 }
2889
0186c6a7 2890 lh_cu = this_cu;
7b9f3c50
DE
2891 lh = NULL;
2892 slot = NULL;
2893 line_offset = 0;
dee91e82
DE
2894
2895 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2896 if (attr)
2897 {
7b9f3c50
DE
2898 struct quick_file_names find_entry;
2899
2900 line_offset = DW_UNSND (attr);
2901
2902 /* We may have already read in this line header (TU line header sharing).
2903 If we have we're done. */
094b34ac
DE
2904 find_entry.hash.dwo_unit = cu->dwo_unit;
2905 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2906 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2907 &find_entry, INSERT);
2908 if (*slot != NULL)
2909 {
094b34ac 2910 lh_cu->v.quick->file_names = *slot;
dee91e82 2911 return;
7b9f3c50
DE
2912 }
2913
3019eac3 2914 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2915 }
2916 if (lh == NULL)
2917 {
094b34ac 2918 lh_cu->v.quick->no_file_data = 1;
dee91e82 2919 return;
9291a0cd
TT
2920 }
2921
7b9f3c50 2922 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2923 qfn->hash.dwo_unit = cu->dwo_unit;
2924 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2925 gdb_assert (slot != NULL);
2926 *slot = qfn;
9291a0cd 2927
dee91e82 2928 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2929
7b9f3c50
DE
2930 qfn->num_file_names = lh->num_file_names;
2931 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2932 lh->num_file_names * sizeof (char *));
9291a0cd 2933 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2934 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2935 qfn->real_names = NULL;
9291a0cd 2936
7b9f3c50 2937 free_line_header (lh);
7b9f3c50 2938
094b34ac 2939 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2940}
2941
2942/* A helper for the "quick" functions which attempts to read the line
2943 table for THIS_CU. */
2944
2945static struct quick_file_names *
e4a48d9d 2946dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2947{
0186c6a7
DE
2948 /* This should never be called for TUs. */
2949 gdb_assert (! this_cu->is_debug_types);
2950 /* Nor type unit groups. */
2951 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2952
dee91e82
DE
2953 if (this_cu->v.quick->file_names != NULL)
2954 return this_cu->v.quick->file_names;
2955 /* If we know there is no line data, no point in looking again. */
2956 if (this_cu->v.quick->no_file_data)
2957 return NULL;
2958
0186c6a7 2959 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2960
2961 if (this_cu->v.quick->no_file_data)
2962 return NULL;
2963 return this_cu->v.quick->file_names;
9291a0cd
TT
2964}
2965
2966/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2967 real path for a given file name from the line table. */
2fdf6df6 2968
9291a0cd 2969static const char *
7b9f3c50
DE
2970dw2_get_real_path (struct objfile *objfile,
2971 struct quick_file_names *qfn, int index)
9291a0cd 2972{
7b9f3c50
DE
2973 if (qfn->real_names == NULL)
2974 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2975 qfn->num_file_names, sizeof (char *));
9291a0cd 2976
7b9f3c50
DE
2977 if (qfn->real_names[index] == NULL)
2978 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2979
7b9f3c50 2980 return qfn->real_names[index];
9291a0cd
TT
2981}
2982
2983static struct symtab *
2984dw2_find_last_source_symtab (struct objfile *objfile)
2985{
2986 int index;
ae2de4f8 2987
9291a0cd
TT
2988 dw2_setup (objfile);
2989 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2990 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2991}
2992
7b9f3c50
DE
2993/* Traversal function for dw2_forget_cached_source_info. */
2994
2995static int
2996dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2997{
7b9f3c50 2998 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2999
7b9f3c50 3000 if (file_data->real_names)
9291a0cd 3001 {
7b9f3c50 3002 int i;
9291a0cd 3003
7b9f3c50 3004 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3005 {
7b9f3c50
DE
3006 xfree ((void*) file_data->real_names[i]);
3007 file_data->real_names[i] = NULL;
9291a0cd
TT
3008 }
3009 }
7b9f3c50
DE
3010
3011 return 1;
3012}
3013
3014static void
3015dw2_forget_cached_source_info (struct objfile *objfile)
3016{
3017 dw2_setup (objfile);
3018
3019 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3020 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3021}
3022
f8eba3c6
TT
3023/* Helper function for dw2_map_symtabs_matching_filename that expands
3024 the symtabs and calls the iterator. */
3025
3026static int
3027dw2_map_expand_apply (struct objfile *objfile,
3028 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3029 const char *name, const char *real_path,
f8eba3c6
TT
3030 int (*callback) (struct symtab *, void *),
3031 void *data)
3032{
3033 struct symtab *last_made = objfile->symtabs;
3034
3035 /* Don't visit already-expanded CUs. */
3036 if (per_cu->v.quick->symtab)
3037 return 0;
3038
3039 /* This may expand more than one symtab, and we want to iterate over
3040 all of them. */
a0f42c21 3041 dw2_instantiate_symtab (per_cu);
f8eba3c6 3042
f5b95b50 3043 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3044 objfile->symtabs, last_made);
3045}
3046
3047/* Implementation of the map_symtabs_matching_filename method. */
3048
9291a0cd 3049static int
f8eba3c6 3050dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3051 const char *real_path,
f8eba3c6
TT
3052 int (*callback) (struct symtab *, void *),
3053 void *data)
9291a0cd
TT
3054{
3055 int i;
c011a4f4 3056 const char *name_basename = lbasename (name);
9291a0cd
TT
3057
3058 dw2_setup (objfile);
ae2de4f8 3059
848e3e78
DE
3060 /* The rule is CUs specify all the files, including those used by
3061 any TU, so there's no need to scan TUs here. */
f4dc4d17 3062
848e3e78 3063 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3064 {
3065 int j;
f4dc4d17 3066 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3067 struct quick_file_names *file_data;
9291a0cd 3068
3d7bb9d9 3069 /* We only need to look at symtabs not already expanded. */
e254ef6a 3070 if (per_cu->v.quick->symtab)
9291a0cd
TT
3071 continue;
3072
e4a48d9d 3073 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3074 if (file_data == NULL)
9291a0cd
TT
3075 continue;
3076
7b9f3c50 3077 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3078 {
7b9f3c50 3079 const char *this_name = file_data->file_names[j];
da235a7c 3080 const char *this_real_name;
9291a0cd 3081
af529f8f 3082 if (compare_filenames_for_search (this_name, name))
9291a0cd 3083 {
f5b95b50 3084 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3085 callback, data))
3086 return 1;
288e77a7 3087 continue;
4aac40c8 3088 }
9291a0cd 3089
c011a4f4
DE
3090 /* Before we invoke realpath, which can get expensive when many
3091 files are involved, do a quick comparison of the basenames. */
3092 if (! basenames_may_differ
3093 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3094 continue;
3095
da235a7c
JK
3096 this_real_name = dw2_get_real_path (objfile, file_data, j);
3097 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3098 {
da235a7c
JK
3099 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3100 callback, data))
3101 return 1;
288e77a7 3102 continue;
da235a7c 3103 }
9291a0cd 3104
da235a7c
JK
3105 if (real_path != NULL)
3106 {
af529f8f
JK
3107 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3108 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3109 if (this_real_name != NULL
af529f8f 3110 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3111 {
f5b95b50 3112 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3113 callback, data))
3114 return 1;
288e77a7 3115 continue;
9291a0cd
TT
3116 }
3117 }
3118 }
3119 }
3120
9291a0cd
TT
3121 return 0;
3122}
3123
da51c347
DE
3124/* Struct used to manage iterating over all CUs looking for a symbol. */
3125
3126struct dw2_symtab_iterator
9291a0cd 3127{
da51c347
DE
3128 /* The internalized form of .gdb_index. */
3129 struct mapped_index *index;
3130 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3131 int want_specific_block;
3132 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3133 Unused if !WANT_SPECIFIC_BLOCK. */
3134 int block_index;
3135 /* The kind of symbol we're looking for. */
3136 domain_enum domain;
3137 /* The list of CUs from the index entry of the symbol,
3138 or NULL if not found. */
3139 offset_type *vec;
3140 /* The next element in VEC to look at. */
3141 int next;
3142 /* The number of elements in VEC, or zero if there is no match. */
3143 int length;
3144};
9291a0cd 3145
da51c347
DE
3146/* Initialize the index symtab iterator ITER.
3147 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3148 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3149
9291a0cd 3150static void
da51c347
DE
3151dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3152 struct mapped_index *index,
3153 int want_specific_block,
3154 int block_index,
3155 domain_enum domain,
3156 const char *name)
3157{
3158 iter->index = index;
3159 iter->want_specific_block = want_specific_block;
3160 iter->block_index = block_index;
3161 iter->domain = domain;
3162 iter->next = 0;
3163
3164 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3165 iter->length = MAYBE_SWAP (*iter->vec);
3166 else
3167 {
3168 iter->vec = NULL;
3169 iter->length = 0;
3170 }
3171}
3172
3173/* Return the next matching CU or NULL if there are no more. */
3174
3175static struct dwarf2_per_cu_data *
3176dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3177{
3178 for ( ; iter->next < iter->length; ++iter->next)
3179 {
3180 offset_type cu_index_and_attrs =
3181 MAYBE_SWAP (iter->vec[iter->next + 1]);
3182 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3183 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3184 int want_static = iter->block_index != GLOBAL_BLOCK;
3185 /* This value is only valid for index versions >= 7. */
3186 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3187 gdb_index_symbol_kind symbol_kind =
3188 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3189 /* Only check the symbol attributes if they're present.
3190 Indices prior to version 7 don't record them,
3191 and indices >= 7 may elide them for certain symbols
3192 (gold does this). */
3193 int attrs_valid =
3194 (iter->index->version >= 7
3195 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3196
3197 /* Skip if already read in. */
3198 if (per_cu->v.quick->symtab)
3199 continue;
3200
3201 if (attrs_valid
3202 && iter->want_specific_block
3203 && want_static != is_static)
3204 continue;
3205
3206 /* Only check the symbol's kind if it has one. */
3207 if (attrs_valid)
3208 {
3209 switch (iter->domain)
3210 {
3211 case VAR_DOMAIN:
3212 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3213 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3214 /* Some types are also in VAR_DOMAIN. */
3215 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3216 continue;
3217 break;
3218 case STRUCT_DOMAIN:
3219 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3220 continue;
3221 break;
3222 case LABEL_DOMAIN:
3223 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3224 continue;
3225 break;
3226 default:
3227 break;
3228 }
3229 }
3230
3231 ++iter->next;
3232 return per_cu;
3233 }
3234
3235 return NULL;
3236}
3237
3238static struct symtab *
3239dw2_lookup_symbol (struct objfile *objfile, int block_index,
3240 const char *name, domain_enum domain)
9291a0cd 3241{
da51c347 3242 struct symtab *stab_best = NULL;
156942c7
DE
3243 struct mapped_index *index;
3244
9291a0cd
TT
3245 dw2_setup (objfile);
3246
156942c7
DE
3247 index = dwarf2_per_objfile->index_table;
3248
da51c347 3249 /* index is NULL if OBJF_READNOW. */
156942c7 3250 if (index)
9291a0cd 3251 {
da51c347
DE
3252 struct dw2_symtab_iterator iter;
3253 struct dwarf2_per_cu_data *per_cu;
3254
3255 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3256
da51c347 3257 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3258 {
da51c347
DE
3259 struct symbol *sym = NULL;
3260 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3261
3262 /* Some caution must be observed with overloaded functions
3263 and methods, since the index will not contain any overload
3264 information (but NAME might contain it). */
3265 if (stab->primary)
9291a0cd 3266 {
da51c347
DE
3267 struct blockvector *bv = BLOCKVECTOR (stab);
3268 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3269
da51c347
DE
3270 sym = lookup_block_symbol (block, name, domain);
3271 }
1fd400ff 3272
da51c347
DE
3273 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3274 {
3275 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3276 return stab;
3277
3278 stab_best = stab;
9291a0cd 3279 }
da51c347
DE
3280
3281 /* Keep looking through other CUs. */
9291a0cd
TT
3282 }
3283 }
9291a0cd 3284
da51c347 3285 return stab_best;
9291a0cd
TT
3286}
3287
3288static void
3289dw2_print_stats (struct objfile *objfile)
3290{
e4a48d9d 3291 int i, total, count;
9291a0cd
TT
3292
3293 dw2_setup (objfile);
e4a48d9d 3294 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3295 count = 0;
e4a48d9d 3296 for (i = 0; i < total; ++i)
9291a0cd 3297 {
e254ef6a 3298 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3299
e254ef6a 3300 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3301 ++count;
3302 }
e4a48d9d 3303 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3304 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3305}
3306
3307static void
3308dw2_dump (struct objfile *objfile)
3309{
3310 /* Nothing worth printing. */
3311}
3312
3313static void
3189cb12
DE
3314dw2_relocate (struct objfile *objfile,
3315 const struct section_offsets *new_offsets,
3316 const struct section_offsets *delta)
9291a0cd
TT
3317{
3318 /* There's nothing to relocate here. */
3319}
3320
3321static void
3322dw2_expand_symtabs_for_function (struct objfile *objfile,
3323 const char *func_name)
3324{
da51c347
DE
3325 struct mapped_index *index;
3326
3327 dw2_setup (objfile);
3328
3329 index = dwarf2_per_objfile->index_table;
3330
3331 /* index is NULL if OBJF_READNOW. */
3332 if (index)
3333 {
3334 struct dw2_symtab_iterator iter;
3335 struct dwarf2_per_cu_data *per_cu;
3336
3337 /* Note: It doesn't matter what we pass for block_index here. */
3338 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3339 func_name);
3340
3341 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3342 dw2_instantiate_symtab (per_cu);
3343 }
9291a0cd
TT
3344}
3345
3346static void
3347dw2_expand_all_symtabs (struct objfile *objfile)
3348{
3349 int i;
3350
3351 dw2_setup (objfile);
1fd400ff
TT
3352
3353 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3354 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3355 {
e254ef6a 3356 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3357
a0f42c21 3358 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3359 }
3360}
3361
3362static void
652a8996
JK
3363dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3364 const char *fullname)
9291a0cd
TT
3365{
3366 int i;
3367
3368 dw2_setup (objfile);
d4637a04
DE
3369
3370 /* We don't need to consider type units here.
3371 This is only called for examining code, e.g. expand_line_sal.
3372 There can be an order of magnitude (or more) more type units
3373 than comp units, and we avoid them if we can. */
3374
3375 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3376 {
3377 int j;
e254ef6a 3378 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3379 struct quick_file_names *file_data;
9291a0cd 3380
3d7bb9d9 3381 /* We only need to look at symtabs not already expanded. */
e254ef6a 3382 if (per_cu->v.quick->symtab)
9291a0cd
TT
3383 continue;
3384
e4a48d9d 3385 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3386 if (file_data == NULL)
9291a0cd
TT
3387 continue;
3388
7b9f3c50 3389 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3390 {
652a8996
JK
3391 const char *this_fullname = file_data->file_names[j];
3392
3393 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3394 {
a0f42c21 3395 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3396 break;
3397 }
3398 }
3399 }
3400}
3401
356d9f9d
TT
3402/* A helper function for dw2_find_symbol_file that finds the primary
3403 file name for a given CU. This is a die_reader_func. */
3404
3405static void
3406dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
d521ce57 3407 const gdb_byte *info_ptr,
356d9f9d
TT
3408 struct die_info *comp_unit_die,
3409 int has_children,
3410 void *data)
3411{
3412 const char **result_ptr = data;
3413 struct dwarf2_cu *cu = reader->cu;
3414 struct attribute *attr;
3415
3416 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3417 if (attr == NULL)
3418 *result_ptr = NULL;
3419 else
3420 *result_ptr = DW_STRING (attr);
3421}
3422
dd786858 3423static const char *
9291a0cd
TT
3424dw2_find_symbol_file (struct objfile *objfile, const char *name)
3425{
e254ef6a 3426 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3427 offset_type *vec;
356d9f9d 3428 const char *filename;
9291a0cd
TT
3429
3430 dw2_setup (objfile);
3431
ae2de4f8 3432 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3433 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3434 {
3435 struct symtab *s;
3436
d790cf0a
DE
3437 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3438 {
3439 struct blockvector *bv = BLOCKVECTOR (s);
3440 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3441 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3442
3443 if (sym)
652a8996
JK
3444 {
3445 /* Only file extension of returned filename is recognized. */
3446 return SYMBOL_SYMTAB (sym)->filename;
3447 }
d790cf0a 3448 }
96408a79
SA
3449 return NULL;
3450 }
9291a0cd
TT
3451
3452 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3453 name, &vec))
3454 return NULL;
3455
3456 /* Note that this just looks at the very first one named NAME -- but
3457 actually we are looking for a function. find_main_filename
3458 should be rewritten so that it doesn't require a custom hook. It
3459 could just use the ordinary symbol tables. */
3460 /* vec[0] is the length, which must always be >0. */
156942c7 3461 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3462
356d9f9d 3463 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3464 {
3465 /* Only file extension of returned filename is recognized. */
3466 return per_cu->v.quick->symtab->filename;
3467 }
356d9f9d 3468
a98c29a0
DE
3469 /* Initialize filename in case there's a problem reading the DWARF,
3470 dw2_get_primary_filename_reader may not get called. */
3471 filename = NULL;
f4dc4d17
DE
3472 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3473 dw2_get_primary_filename_reader, &filename);
9291a0cd 3474
652a8996 3475 /* Only file extension of returned filename is recognized. */
356d9f9d 3476 return filename;
9291a0cd
TT
3477}
3478
3479static void
40658b94
PH
3480dw2_map_matching_symbols (const char * name, domain_enum namespace,
3481 struct objfile *objfile, int global,
3482 int (*callback) (struct block *,
3483 struct symbol *, void *),
2edb89d3
JK
3484 void *data, symbol_compare_ftype *match,
3485 symbol_compare_ftype *ordered_compare)
9291a0cd 3486{
40658b94 3487 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3488 current language is Ada for a non-Ada objfile using GNU index. As Ada
3489 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3490}
3491
3492static void
f8eba3c6
TT
3493dw2_expand_symtabs_matching
3494 (struct objfile *objfile,
fbd9ab74 3495 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3496 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3497 enum search_domain kind,
3498 void *data)
9291a0cd
TT
3499{
3500 int i;
3501 offset_type iter;
4b5246aa 3502 struct mapped_index *index;
9291a0cd
TT
3503
3504 dw2_setup (objfile);
ae2de4f8
DE
3505
3506 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3507 if (!dwarf2_per_objfile->index_table)
3508 return;
4b5246aa 3509 index = dwarf2_per_objfile->index_table;
9291a0cd 3510
7b08b9eb 3511 if (file_matcher != NULL)
24c79950
TT
3512 {
3513 struct cleanup *cleanup;
3514 htab_t visited_found, visited_not_found;
3515
3516 visited_found = htab_create_alloc (10,
3517 htab_hash_pointer, htab_eq_pointer,
3518 NULL, xcalloc, xfree);
3519 cleanup = make_cleanup_htab_delete (visited_found);
3520 visited_not_found = htab_create_alloc (10,
3521 htab_hash_pointer, htab_eq_pointer,
3522 NULL, xcalloc, xfree);
3523 make_cleanup_htab_delete (visited_not_found);
3524
848e3e78
DE
3525 /* The rule is CUs specify all the files, including those used by
3526 any TU, so there's no need to scan TUs here. */
3527
3528 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3529 {
3530 int j;
f4dc4d17 3531 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3532 struct quick_file_names *file_data;
3533 void **slot;
7b08b9eb 3534
24c79950 3535 per_cu->v.quick->mark = 0;
3d7bb9d9 3536
24c79950
TT
3537 /* We only need to look at symtabs not already expanded. */
3538 if (per_cu->v.quick->symtab)
3539 continue;
7b08b9eb 3540
e4a48d9d 3541 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3542 if (file_data == NULL)
3543 continue;
7b08b9eb 3544
24c79950
TT
3545 if (htab_find (visited_not_found, file_data) != NULL)
3546 continue;
3547 else if (htab_find (visited_found, file_data) != NULL)
3548 {
3549 per_cu->v.quick->mark = 1;
3550 continue;
3551 }
3552
3553 for (j = 0; j < file_data->num_file_names; ++j)
3554 {
da235a7c
JK
3555 const char *this_real_name;
3556
fbd9ab74 3557 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3558 {
3559 per_cu->v.quick->mark = 1;
3560 break;
3561 }
da235a7c
JK
3562
3563 /* Before we invoke realpath, which can get expensive when many
3564 files are involved, do a quick comparison of the basenames. */
3565 if (!basenames_may_differ
3566 && !file_matcher (lbasename (file_data->file_names[j]),
3567 data, 1))
3568 continue;
3569
3570 this_real_name = dw2_get_real_path (objfile, file_data, j);
3571 if (file_matcher (this_real_name, data, 0))
3572 {
3573 per_cu->v.quick->mark = 1;
3574 break;
3575 }
24c79950
TT
3576 }
3577
3578 slot = htab_find_slot (per_cu->v.quick->mark
3579 ? visited_found
3580 : visited_not_found,
3581 file_data, INSERT);
3582 *slot = file_data;
3583 }
3584
3585 do_cleanups (cleanup);
3586 }
9291a0cd 3587
3876f04e 3588 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3589 {
3590 offset_type idx = 2 * iter;
3591 const char *name;
3592 offset_type *vec, vec_len, vec_idx;
3593
3876f04e 3594 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3595 continue;
3596
3876f04e 3597 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3598
e078317b 3599 if (! (*name_matcher) (name, data))
9291a0cd
TT
3600 continue;
3601
3602 /* The name was matched, now expand corresponding CUs that were
3603 marked. */
4b5246aa 3604 vec = (offset_type *) (index->constant_pool
3876f04e 3605 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3606 vec_len = MAYBE_SWAP (vec[0]);
3607 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3608 {
e254ef6a 3609 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3610 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3611 gdb_index_symbol_kind symbol_kind =
3612 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3613 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3614
3615 /* Don't crash on bad data. */
3616 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3617 + dwarf2_per_objfile->n_type_units))
156942c7 3618 continue;
1fd400ff 3619
156942c7
DE
3620 /* Only check the symbol's kind if it has one.
3621 Indices prior to version 7 don't record it. */
3622 if (index->version >= 7)
3623 {
3624 switch (kind)
3625 {
3626 case VARIABLES_DOMAIN:
3627 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3628 continue;
3629 break;
3630 case FUNCTIONS_DOMAIN:
3631 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3632 continue;
3633 break;
3634 case TYPES_DOMAIN:
3635 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3636 continue;
3637 break;
3638 default:
3639 break;
3640 }
3641 }
3642
3643 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3644 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3645 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3646 }
3647 }
3648}
3649
9703b513
TT
3650/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3651 symtab. */
3652
3653static struct symtab *
3654recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3655{
3656 int i;
3657
3658 if (BLOCKVECTOR (symtab) != NULL
3659 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3660 return symtab;
3661
a3ec0bb1
DE
3662 if (symtab->includes == NULL)
3663 return NULL;
3664
9703b513
TT
3665 for (i = 0; symtab->includes[i]; ++i)
3666 {
a3ec0bb1 3667 struct symtab *s = symtab->includes[i];
9703b513
TT
3668
3669 s = recursively_find_pc_sect_symtab (s, pc);
3670 if (s != NULL)
3671 return s;
3672 }
3673
3674 return NULL;
3675}
3676
9291a0cd
TT
3677static struct symtab *
3678dw2_find_pc_sect_symtab (struct objfile *objfile,
3679 struct minimal_symbol *msymbol,
3680 CORE_ADDR pc,
3681 struct obj_section *section,
3682 int warn_if_readin)
3683{
3684 struct dwarf2_per_cu_data *data;
9703b513 3685 struct symtab *result;
9291a0cd
TT
3686
3687 dw2_setup (objfile);
3688
3689 if (!objfile->psymtabs_addrmap)
3690 return NULL;
3691
3692 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3693 if (!data)
3694 return NULL;
3695
3696 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3697 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3698 paddress (get_objfile_arch (objfile), pc));
3699
9703b513
TT
3700 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3701 gdb_assert (result != NULL);
3702 return result;
9291a0cd
TT
3703}
3704
9291a0cd 3705static void
44b13c5a 3706dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3707 void *data, int need_fullname)
9291a0cd
TT
3708{
3709 int i;
24c79950
TT
3710 struct cleanup *cleanup;
3711 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3712 NULL, xcalloc, xfree);
9291a0cd 3713
24c79950 3714 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3715 dw2_setup (objfile);
ae2de4f8 3716
848e3e78
DE
3717 /* The rule is CUs specify all the files, including those used by
3718 any TU, so there's no need to scan TUs here.
3719 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3720
848e3e78 3721 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3722 {
3723 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3724
3725 if (per_cu->v.quick->symtab)
3726 {
3727 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3728 INSERT);
3729
3730 *slot = per_cu->v.quick->file_names;
3731 }
3732 }
3733
848e3e78 3734 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3735 {
3736 int j;
f4dc4d17 3737 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3738 struct quick_file_names *file_data;
24c79950 3739 void **slot;
9291a0cd 3740
3d7bb9d9 3741 /* We only need to look at symtabs not already expanded. */
e254ef6a 3742 if (per_cu->v.quick->symtab)
9291a0cd
TT
3743 continue;
3744
e4a48d9d 3745 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3746 if (file_data == NULL)
9291a0cd
TT
3747 continue;
3748
24c79950
TT
3749 slot = htab_find_slot (visited, file_data, INSERT);
3750 if (*slot)
3751 {
3752 /* Already visited. */
3753 continue;
3754 }
3755 *slot = file_data;
3756
7b9f3c50 3757 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3758 {
74e2f255
DE
3759 const char *this_real_name;
3760
3761 if (need_fullname)
3762 this_real_name = dw2_get_real_path (objfile, file_data, j);
3763 else
3764 this_real_name = NULL;
7b9f3c50 3765 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3766 }
3767 }
24c79950
TT
3768
3769 do_cleanups (cleanup);
9291a0cd
TT
3770}
3771
3772static int
3773dw2_has_symbols (struct objfile *objfile)
3774{
3775 return 1;
3776}
3777
3778const struct quick_symbol_functions dwarf2_gdb_index_functions =
3779{
3780 dw2_has_symbols,
3781 dw2_find_last_source_symtab,
3782 dw2_forget_cached_source_info,
f8eba3c6 3783 dw2_map_symtabs_matching_filename,
9291a0cd 3784 dw2_lookup_symbol,
9291a0cd
TT
3785 dw2_print_stats,
3786 dw2_dump,
3787 dw2_relocate,
3788 dw2_expand_symtabs_for_function,
3789 dw2_expand_all_symtabs,
652a8996 3790 dw2_expand_symtabs_with_fullname,
9291a0cd 3791 dw2_find_symbol_file,
40658b94 3792 dw2_map_matching_symbols,
9291a0cd
TT
3793 dw2_expand_symtabs_matching,
3794 dw2_find_pc_sect_symtab,
9291a0cd
TT
3795 dw2_map_symbol_filenames
3796};
3797
3798/* Initialize for reading DWARF for this objfile. Return 0 if this
3799 file will use psymtabs, or 1 if using the GNU index. */
3800
3801int
3802dwarf2_initialize_objfile (struct objfile *objfile)
3803{
3804 /* If we're about to read full symbols, don't bother with the
3805 indices. In this case we also don't care if some other debug
3806 format is making psymtabs, because they are all about to be
3807 expanded anyway. */
3808 if ((objfile->flags & OBJF_READNOW))
3809 {
3810 int i;
3811
3812 dwarf2_per_objfile->using_index = 1;
3813 create_all_comp_units (objfile);
0e50663e 3814 create_all_type_units (objfile);
7b9f3c50
DE
3815 dwarf2_per_objfile->quick_file_names_table =
3816 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3817
1fd400ff 3818 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3819 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3820 {
e254ef6a 3821 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3822
e254ef6a
DE
3823 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3824 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3825 }
3826
3827 /* Return 1 so that gdb sees the "quick" functions. However,
3828 these functions will be no-ops because we will have expanded
3829 all symtabs. */
3830 return 1;
3831 }
3832
3833 if (dwarf2_read_index (objfile))
3834 return 1;
3835
9291a0cd
TT
3836 return 0;
3837}
3838
3839\f
3840
dce234bc
PP
3841/* Build a partial symbol table. */
3842
3843void
f29dff0a 3844dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3845{
c9bf0622
TT
3846 volatile struct gdb_exception except;
3847
f29dff0a 3848 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3849 {
3850 init_psymbol_list (objfile, 1024);
3851 }
3852
c9bf0622
TT
3853 TRY_CATCH (except, RETURN_MASK_ERROR)
3854 {
3855 /* This isn't really ideal: all the data we allocate on the
3856 objfile's obstack is still uselessly kept around. However,
3857 freeing it seems unsafe. */
3858 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3859
3860 dwarf2_build_psymtabs_hard (objfile);
3861 discard_cleanups (cleanups);
3862 }
3863 if (except.reason < 0)
3864 exception_print (gdb_stderr, except);
c906108c 3865}
c906108c 3866
1ce1cefd
DE
3867/* Return the total length of the CU described by HEADER. */
3868
3869static unsigned int
3870get_cu_length (const struct comp_unit_head *header)
3871{
3872 return header->initial_length_size + header->length;
3873}
3874
45452591
DE
3875/* Return TRUE if OFFSET is within CU_HEADER. */
3876
3877static inline int
b64f50a1 3878offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3879{
b64f50a1 3880 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3881 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3882
b64f50a1 3883 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3884}
3885
3b80fe9b
DE
3886/* Find the base address of the compilation unit for range lists and
3887 location lists. It will normally be specified by DW_AT_low_pc.
3888 In DWARF-3 draft 4, the base address could be overridden by
3889 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3890 compilation units with discontinuous ranges. */
3891
3892static void
3893dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3894{
3895 struct attribute *attr;
3896
3897 cu->base_known = 0;
3898 cu->base_address = 0;
3899
3900 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3901 if (attr)
3902 {
3903 cu->base_address = DW_ADDR (attr);
3904 cu->base_known = 1;
3905 }
3906 else
3907 {
3908 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3909 if (attr)
3910 {
3911 cu->base_address = DW_ADDR (attr);
3912 cu->base_known = 1;
3913 }
3914 }
3915}
3916
93311388
DE
3917/* Read in the comp unit header information from the debug_info at info_ptr.
3918 NOTE: This leaves members offset, first_die_offset to be filled in
3919 by the caller. */
107d2387 3920
d521ce57 3921static const gdb_byte *
107d2387 3922read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3923 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3924{
3925 int signed_addr;
891d2f0b 3926 unsigned int bytes_read;
c764a876
DE
3927
3928 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3929 cu_header->initial_length_size = bytes_read;
3930 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3931 info_ptr += bytes_read;
107d2387
AC
3932 cu_header->version = read_2_bytes (abfd, info_ptr);
3933 info_ptr += 2;
b64f50a1
JK
3934 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3935 &bytes_read);
613e1657 3936 info_ptr += bytes_read;
107d2387
AC
3937 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3938 info_ptr += 1;
3939 signed_addr = bfd_get_sign_extend_vma (abfd);
3940 if (signed_addr < 0)
8e65ff28 3941 internal_error (__FILE__, __LINE__,
e2e0b3e5 3942 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3943 cu_header->signed_addr_p = signed_addr;
c764a876 3944
107d2387
AC
3945 return info_ptr;
3946}
3947
36586728
TT
3948/* Helper function that returns the proper abbrev section for
3949 THIS_CU. */
3950
3951static struct dwarf2_section_info *
3952get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3953{
3954 struct dwarf2_section_info *abbrev;
3955
3956 if (this_cu->is_dwz)
3957 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3958 else
3959 abbrev = &dwarf2_per_objfile->abbrev;
3960
3961 return abbrev;
3962}
3963
9ff913ba
DE
3964/* Subroutine of read_and_check_comp_unit_head and
3965 read_and_check_type_unit_head to simplify them.
3966 Perform various error checking on the header. */
3967
3968static void
3969error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3970 struct dwarf2_section_info *section,
3971 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3972{
3973 bfd *abfd = section->asection->owner;
3974 const char *filename = bfd_get_filename (abfd);
3975
3976 if (header->version != 2 && header->version != 3 && header->version != 4)
3977 error (_("Dwarf Error: wrong version in compilation unit header "
3978 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3979 filename);
3980
b64f50a1 3981 if (header->abbrev_offset.sect_off
36586728 3982 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3983 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3984 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3985 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3986 filename);
3987
3988 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3989 avoid potential 32-bit overflow. */
1ce1cefd 3990 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3991 > section->size)
3992 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3993 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3994 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3995 filename);
3996}
3997
3998/* Read in a CU/TU header and perform some basic error checking.
3999 The contents of the header are stored in HEADER.
4000 The result is a pointer to the start of the first DIE. */
adabb602 4001
d521ce57 4002static const gdb_byte *
9ff913ba
DE
4003read_and_check_comp_unit_head (struct comp_unit_head *header,
4004 struct dwarf2_section_info *section,
4bdcc0c1 4005 struct dwarf2_section_info *abbrev_section,
d521ce57 4006 const gdb_byte *info_ptr,
9ff913ba 4007 int is_debug_types_section)
72bf9492 4008{
d521ce57 4009 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4010 bfd *abfd = section->asection->owner;
72bf9492 4011
b64f50a1 4012 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4013
72bf9492
DJ
4014 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4015
460c1c54
CC
4016 /* If we're reading a type unit, skip over the signature and
4017 type_offset fields. */
b0df02fd 4018 if (is_debug_types_section)
460c1c54
CC
4019 info_ptr += 8 /*signature*/ + header->offset_size;
4020
b64f50a1 4021 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4022
4bdcc0c1 4023 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4024
4025 return info_ptr;
4026}
4027
348e048f
DE
4028/* Read in the types comp unit header information from .debug_types entry at
4029 types_ptr. The result is a pointer to one past the end of the header. */
4030
d521ce57 4031static const gdb_byte *
9ff913ba
DE
4032read_and_check_type_unit_head (struct comp_unit_head *header,
4033 struct dwarf2_section_info *section,
4bdcc0c1 4034 struct dwarf2_section_info *abbrev_section,
d521ce57 4035 const gdb_byte *info_ptr,
dee91e82
DE
4036 ULONGEST *signature,
4037 cu_offset *type_offset_in_tu)
348e048f 4038{
d521ce57 4039 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4040 bfd *abfd = section->asection->owner;
348e048f 4041
b64f50a1 4042 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4043
9ff913ba 4044 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4045
9ff913ba
DE
4046 /* If we're reading a type unit, skip over the signature and
4047 type_offset fields. */
4048 if (signature != NULL)
4049 *signature = read_8_bytes (abfd, info_ptr);
4050 info_ptr += 8;
dee91e82
DE
4051 if (type_offset_in_tu != NULL)
4052 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4053 header->offset_size);
9ff913ba
DE
4054 info_ptr += header->offset_size;
4055
b64f50a1 4056 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4057
4bdcc0c1 4058 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4059
4060 return info_ptr;
348e048f
DE
4061}
4062
f4dc4d17
DE
4063/* Fetch the abbreviation table offset from a comp or type unit header. */
4064
4065static sect_offset
4066read_abbrev_offset (struct dwarf2_section_info *section,
4067 sect_offset offset)
4068{
4069 bfd *abfd = section->asection->owner;
d521ce57 4070 const gdb_byte *info_ptr;
f4dc4d17
DE
4071 unsigned int length, initial_length_size, offset_size;
4072 sect_offset abbrev_offset;
4073
4074 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4075 info_ptr = section->buffer + offset.sect_off;
4076 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4077 offset_size = initial_length_size == 4 ? 4 : 8;
4078 info_ptr += initial_length_size + 2 /*version*/;
4079 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4080 return abbrev_offset;
4081}
4082
aaa75496
JB
4083/* Allocate a new partial symtab for file named NAME and mark this new
4084 partial symtab as being an include of PST. */
4085
4086static void
d521ce57 4087dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4088 struct objfile *objfile)
4089{
4090 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4091
fbd9ab74
JK
4092 if (!IS_ABSOLUTE_PATH (subpst->filename))
4093 {
4094 /* It shares objfile->objfile_obstack. */
4095 subpst->dirname = pst->dirname;
4096 }
4097
aaa75496
JB
4098 subpst->section_offsets = pst->section_offsets;
4099 subpst->textlow = 0;
4100 subpst->texthigh = 0;
4101
4102 subpst->dependencies = (struct partial_symtab **)
4103 obstack_alloc (&objfile->objfile_obstack,
4104 sizeof (struct partial_symtab *));
4105 subpst->dependencies[0] = pst;
4106 subpst->number_of_dependencies = 1;
4107
4108 subpst->globals_offset = 0;
4109 subpst->n_global_syms = 0;
4110 subpst->statics_offset = 0;
4111 subpst->n_static_syms = 0;
4112 subpst->symtab = NULL;
4113 subpst->read_symtab = pst->read_symtab;
4114 subpst->readin = 0;
4115
4116 /* No private part is necessary for include psymtabs. This property
4117 can be used to differentiate between such include psymtabs and
10b3939b 4118 the regular ones. */
58a9656e 4119 subpst->read_symtab_private = NULL;
aaa75496
JB
4120}
4121
4122/* Read the Line Number Program data and extract the list of files
4123 included by the source file represented by PST. Build an include
d85a05f0 4124 partial symtab for each of these included files. */
aaa75496
JB
4125
4126static void
4127dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4128 struct die_info *die,
4129 struct partial_symtab *pst)
aaa75496 4130{
d85a05f0
DJ
4131 struct line_header *lh = NULL;
4132 struct attribute *attr;
aaa75496 4133
d85a05f0
DJ
4134 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4135 if (attr)
3019eac3 4136 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4137 if (lh == NULL)
4138 return; /* No linetable, so no includes. */
4139
c6da4cef 4140 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4141 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4142
4143 free_line_header (lh);
4144}
4145
348e048f 4146static hashval_t
52dc124a 4147hash_signatured_type (const void *item)
348e048f 4148{
52dc124a 4149 const struct signatured_type *sig_type = item;
9a619af0 4150
348e048f 4151 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4152 return sig_type->signature;
348e048f
DE
4153}
4154
4155static int
52dc124a 4156eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4157{
4158 const struct signatured_type *lhs = item_lhs;
4159 const struct signatured_type *rhs = item_rhs;
9a619af0 4160
348e048f
DE
4161 return lhs->signature == rhs->signature;
4162}
4163
1fd400ff
TT
4164/* Allocate a hash table for signatured types. */
4165
4166static htab_t
673bfd45 4167allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4168{
4169 return htab_create_alloc_ex (41,
52dc124a
DE
4170 hash_signatured_type,
4171 eq_signatured_type,
1fd400ff
TT
4172 NULL,
4173 &objfile->objfile_obstack,
4174 hashtab_obstack_allocate,
4175 dummy_obstack_deallocate);
4176}
4177
d467dd73 4178/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4179
4180static int
d467dd73 4181add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4182{
4183 struct signatured_type *sigt = *slot;
b4dd5633 4184 struct signatured_type ***datap = datum;
1fd400ff 4185
b4dd5633 4186 **datap = sigt;
1fd400ff
TT
4187 ++*datap;
4188
4189 return 1;
4190}
4191
c88ee1f0
DE
4192/* Create the hash table of all entries in the .debug_types
4193 (or .debug_types.dwo) section(s).
4194 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4195 otherwise it is NULL.
4196
4197 The result is a pointer to the hash table or NULL if there are no types.
4198
4199 Note: This function processes DWO files only, not DWP files. */
348e048f 4200
3019eac3
DE
4201static htab_t
4202create_debug_types_hash_table (struct dwo_file *dwo_file,
4203 VEC (dwarf2_section_info_def) *types)
348e048f 4204{
3019eac3 4205 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4206 htab_t types_htab = NULL;
8b70b953
TT
4207 int ix;
4208 struct dwarf2_section_info *section;
4bdcc0c1 4209 struct dwarf2_section_info *abbrev_section;
348e048f 4210
3019eac3
DE
4211 if (VEC_empty (dwarf2_section_info_def, types))
4212 return NULL;
348e048f 4213
4bdcc0c1
DE
4214 abbrev_section = (dwo_file != NULL
4215 ? &dwo_file->sections.abbrev
4216 : &dwarf2_per_objfile->abbrev);
4217
09406207
DE
4218 if (dwarf2_read_debug)
4219 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4220 dwo_file ? ".dwo" : "",
4221 bfd_get_filename (abbrev_section->asection->owner));
4222
8b70b953 4223 for (ix = 0;
3019eac3 4224 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4225 ++ix)
4226 {
3019eac3 4227 bfd *abfd;
d521ce57 4228 const gdb_byte *info_ptr, *end_ptr;
36586728 4229 struct dwarf2_section_info *abbrev_section;
348e048f 4230
8b70b953
TT
4231 dwarf2_read_section (objfile, section);
4232 info_ptr = section->buffer;
348e048f 4233
8b70b953
TT
4234 if (info_ptr == NULL)
4235 continue;
348e048f 4236
3019eac3
DE
4237 /* We can't set abfd until now because the section may be empty or
4238 not present, in which case section->asection will be NULL. */
4239 abfd = section->asection->owner;
4240
36586728
TT
4241 if (dwo_file)
4242 abbrev_section = &dwo_file->sections.abbrev;
4243 else
4244 abbrev_section = &dwarf2_per_objfile->abbrev;
4245
dee91e82
DE
4246 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4247 because we don't need to read any dies: the signature is in the
4248 header. */
8b70b953
TT
4249
4250 end_ptr = info_ptr + section->size;
4251 while (info_ptr < end_ptr)
4252 {
b64f50a1 4253 sect_offset offset;
3019eac3 4254 cu_offset type_offset_in_tu;
8b70b953 4255 ULONGEST signature;
52dc124a 4256 struct signatured_type *sig_type;
3019eac3 4257 struct dwo_unit *dwo_tu;
8b70b953 4258 void **slot;
d521ce57 4259 const gdb_byte *ptr = info_ptr;
9ff913ba 4260 struct comp_unit_head header;
dee91e82 4261 unsigned int length;
348e048f 4262
b64f50a1 4263 offset.sect_off = ptr - section->buffer;
348e048f 4264
8b70b953 4265 /* We need to read the type's signature in order to build the hash
9ff913ba 4266 table, but we don't need anything else just yet. */
348e048f 4267
4bdcc0c1
DE
4268 ptr = read_and_check_type_unit_head (&header, section,
4269 abbrev_section, ptr,
3019eac3 4270 &signature, &type_offset_in_tu);
6caca83c 4271
1ce1cefd 4272 length = get_cu_length (&header);
dee91e82 4273
6caca83c 4274 /* Skip dummy type units. */
dee91e82
DE
4275 if (ptr >= info_ptr + length
4276 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4277 {
1ce1cefd 4278 info_ptr += length;
6caca83c
CC
4279 continue;
4280 }
8b70b953 4281
0349ea22
DE
4282 if (types_htab == NULL)
4283 {
4284 if (dwo_file)
4285 types_htab = allocate_dwo_unit_table (objfile);
4286 else
4287 types_htab = allocate_signatured_type_table (objfile);
4288 }
4289
3019eac3
DE
4290 if (dwo_file)
4291 {
4292 sig_type = NULL;
4293 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4294 struct dwo_unit);
4295 dwo_tu->dwo_file = dwo_file;
4296 dwo_tu->signature = signature;
4297 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4298 dwo_tu->section = section;
3019eac3
DE
4299 dwo_tu->offset = offset;
4300 dwo_tu->length = length;
4301 }
4302 else
4303 {
4304 /* N.B.: type_offset is not usable if this type uses a DWO file.
4305 The real type_offset is in the DWO file. */
4306 dwo_tu = NULL;
4307 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4308 struct signatured_type);
4309 sig_type->signature = signature;
4310 sig_type->type_offset_in_tu = type_offset_in_tu;
4311 sig_type->per_cu.objfile = objfile;
4312 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4313 sig_type->per_cu.section = section;
3019eac3
DE
4314 sig_type->per_cu.offset = offset;
4315 sig_type->per_cu.length = length;
4316 }
8b70b953 4317
3019eac3
DE
4318 slot = htab_find_slot (types_htab,
4319 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4320 INSERT);
8b70b953
TT
4321 gdb_assert (slot != NULL);
4322 if (*slot != NULL)
4323 {
3019eac3
DE
4324 sect_offset dup_offset;
4325
4326 if (dwo_file)
4327 {
4328 const struct dwo_unit *dup_tu = *slot;
4329
4330 dup_offset = dup_tu->offset;
4331 }
4332 else
4333 {
4334 const struct signatured_type *dup_tu = *slot;
4335
4336 dup_offset = dup_tu->per_cu.offset;
4337 }
b3c8eb43 4338
8b70b953 4339 complaint (&symfile_complaints,
c88ee1f0 4340 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4341 " the entry at offset 0x%x, signature %s"),
3019eac3 4342 offset.sect_off, dup_offset.sect_off,
4031ecc5 4343 hex_string (signature));
8b70b953 4344 }
3019eac3 4345 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4346
09406207 4347 if (dwarf2_read_debug)
4031ecc5 4348 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4349 offset.sect_off,
4031ecc5 4350 hex_string (signature));
348e048f 4351
dee91e82 4352 info_ptr += length;
8b70b953 4353 }
348e048f
DE
4354 }
4355
3019eac3
DE
4356 return types_htab;
4357}
4358
4359/* Create the hash table of all entries in the .debug_types section,
4360 and initialize all_type_units.
4361 The result is zero if there is an error (e.g. missing .debug_types section),
4362 otherwise non-zero. */
4363
4364static int
4365create_all_type_units (struct objfile *objfile)
4366{
4367 htab_t types_htab;
b4dd5633 4368 struct signatured_type **iter;
3019eac3
DE
4369
4370 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4371 if (types_htab == NULL)
4372 {
4373 dwarf2_per_objfile->signatured_types = NULL;
4374 return 0;
4375 }
4376
348e048f
DE
4377 dwarf2_per_objfile->signatured_types = types_htab;
4378
d467dd73
DE
4379 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4380 dwarf2_per_objfile->all_type_units
1fd400ff 4381 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4382 dwarf2_per_objfile->n_type_units
b4dd5633 4383 * sizeof (struct signatured_type *));
d467dd73
DE
4384 iter = &dwarf2_per_objfile->all_type_units[0];
4385 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4386 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4387 == dwarf2_per_objfile->n_type_units);
1fd400ff 4388
348e048f
DE
4389 return 1;
4390}
4391
380bca97 4392/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4393 Returns NULL if signature SIG is not present in the table.
4394 It is up to the caller to complain about this. */
348e048f
DE
4395
4396static struct signatured_type *
e319fa28 4397lookup_signatured_type (ULONGEST sig)
348e048f
DE
4398{
4399 struct signatured_type find_entry, *entry;
4400
4401 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4402 return NULL;
348e048f
DE
4403 find_entry.signature = sig;
4404 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4405 return entry;
4406}
42e7ad6c
DE
4407\f
4408/* Low level DIE reading support. */
348e048f 4409
d85a05f0
DJ
4410/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4411
4412static void
4413init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4414 struct dwarf2_cu *cu,
3019eac3
DE
4415 struct dwarf2_section_info *section,
4416 struct dwo_file *dwo_file)
d85a05f0 4417{
fceca515 4418 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4419 reader->abfd = section->asection->owner;
d85a05f0 4420 reader->cu = cu;
3019eac3 4421 reader->dwo_file = dwo_file;
dee91e82
DE
4422 reader->die_section = section;
4423 reader->buffer = section->buffer;
f664829e 4424 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4425}
4426
b0c7bfa9
DE
4427/* Subroutine of init_cutu_and_read_dies to simplify it.
4428 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4429 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4430 already.
4431
4432 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4433 from it to the DIE in the DWO. If NULL we are skipping the stub.
4434 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4435 are filled in with the info of the DIE from the DWO file.
4436 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4437 provided an abbrev table to use.
4438 The result is non-zero if a valid (non-dummy) DIE was found. */
4439
4440static int
4441read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4442 struct dwo_unit *dwo_unit,
4443 int abbrev_table_provided,
4444 struct die_info *stub_comp_unit_die,
4445 struct die_reader_specs *result_reader,
d521ce57 4446 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4447 struct die_info **result_comp_unit_die,
4448 int *result_has_children)
4449{
4450 struct objfile *objfile = dwarf2_per_objfile->objfile;
4451 struct dwarf2_cu *cu = this_cu->cu;
4452 struct dwarf2_section_info *section;
4453 bfd *abfd;
d521ce57 4454 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4455 const char *comp_dir_string;
4456 ULONGEST signature; /* Or dwo_id. */
4457 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4458 int i,num_extra_attrs;
4459 struct dwarf2_section_info *dwo_abbrev_section;
4460 struct attribute *attr;
4461 struct die_info *comp_unit_die;
4462
4463 /* These attributes aren't processed until later:
4464 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4465 However, the attribute is found in the stub which we won't have later.
4466 In order to not impose this complication on the rest of the code,
4467 we read them here and copy them to the DWO CU/TU die. */
4468
4469 stmt_list = NULL;
4470 low_pc = NULL;
4471 high_pc = NULL;
4472 ranges = NULL;
4473 comp_dir = NULL;
4474
4475 if (stub_comp_unit_die != NULL)
4476 {
4477 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4478 DWO file. */
4479 if (! this_cu->is_debug_types)
4480 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4481 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4482 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4483 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4484 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4485
4486 /* There should be a DW_AT_addr_base attribute here (if needed).
4487 We need the value before we can process DW_FORM_GNU_addr_index. */
4488 cu->addr_base = 0;
4489 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4490 if (attr)
4491 cu->addr_base = DW_UNSND (attr);
4492
4493 /* There should be a DW_AT_ranges_base attribute here (if needed).
4494 We need the value before we can process DW_AT_ranges. */
4495 cu->ranges_base = 0;
4496 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4497 if (attr)
4498 cu->ranges_base = DW_UNSND (attr);
4499 }
4500
4501 /* Set up for reading the DWO CU/TU. */
4502 cu->dwo_unit = dwo_unit;
4503 section = dwo_unit->section;
4504 dwarf2_read_section (objfile, section);
4505 abfd = section->asection->owner;
4506 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4507 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4508 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4509
4510 if (this_cu->is_debug_types)
4511 {
4512 ULONGEST header_signature;
4513 cu_offset type_offset_in_tu;
4514 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4515
4516 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4517 dwo_abbrev_section,
4518 info_ptr,
4519 &header_signature,
4520 &type_offset_in_tu);
4521 gdb_assert (sig_type->signature == header_signature);
4522 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4523 /* For DWOs coming from DWP files, we don't know the CU length
4524 nor the type's offset in the TU until now. */
4525 dwo_unit->length = get_cu_length (&cu->header);
4526 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4527
4528 /* Establish the type offset that can be used to lookup the type.
4529 For DWO files, we don't know it until now. */
4530 sig_type->type_offset_in_section.sect_off =
4531 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4532 }
4533 else
4534 {
4535 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4536 dwo_abbrev_section,
4537 info_ptr, 0);
4538 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4539 /* For DWOs coming from DWP files, we don't know the CU length
4540 until now. */
4541 dwo_unit->length = get_cu_length (&cu->header);
4542 }
4543
02142a6c
DE
4544 /* Replace the CU's original abbrev table with the DWO's.
4545 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4546 if (abbrev_table_provided)
4547 {
4548 /* Don't free the provided abbrev table, the caller of
4549 init_cutu_and_read_dies owns it. */
4550 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4551 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4552 make_cleanup (dwarf2_free_abbrev_table, cu);
4553 }
4554 else
4555 {
4556 dwarf2_free_abbrev_table (cu);
4557 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4558 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4559 }
4560
4561 /* Read in the die, but leave space to copy over the attributes
4562 from the stub. This has the benefit of simplifying the rest of
4563 the code - all the work to maintain the illusion of a single
4564 DW_TAG_{compile,type}_unit DIE is done here. */
4565 num_extra_attrs = ((stmt_list != NULL)
4566 + (low_pc != NULL)
4567 + (high_pc != NULL)
4568 + (ranges != NULL)
4569 + (comp_dir != NULL));
4570 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4571 result_has_children, num_extra_attrs);
4572
4573 /* Copy over the attributes from the stub to the DIE we just read in. */
4574 comp_unit_die = *result_comp_unit_die;
4575 i = comp_unit_die->num_attrs;
4576 if (stmt_list != NULL)
4577 comp_unit_die->attrs[i++] = *stmt_list;
4578 if (low_pc != NULL)
4579 comp_unit_die->attrs[i++] = *low_pc;
4580 if (high_pc != NULL)
4581 comp_unit_die->attrs[i++] = *high_pc;
4582 if (ranges != NULL)
4583 comp_unit_die->attrs[i++] = *ranges;
4584 if (comp_dir != NULL)
4585 comp_unit_die->attrs[i++] = *comp_dir;
4586 comp_unit_die->num_attrs += num_extra_attrs;
4587
bf6af496
DE
4588 if (dwarf2_die_debug)
4589 {
4590 fprintf_unfiltered (gdb_stdlog,
4591 "Read die from %s@0x%x of %s:\n",
4592 bfd_section_name (abfd, section->asection),
4593 (unsigned) (begin_info_ptr - section->buffer),
4594 bfd_get_filename (abfd));
4595 dump_die (comp_unit_die, dwarf2_die_debug);
4596 }
4597
b0c7bfa9
DE
4598 /* Skip dummy compilation units. */
4599 if (info_ptr >= begin_info_ptr + dwo_unit->length
4600 || peek_abbrev_code (abfd, info_ptr) == 0)
4601 return 0;
4602
4603 *result_info_ptr = info_ptr;
4604 return 1;
4605}
4606
4607/* Subroutine of init_cutu_and_read_dies to simplify it.
4608 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4609 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4610
4611static struct dwo_unit *
4612lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4613 struct die_info *comp_unit_die)
4614{
4615 struct dwarf2_cu *cu = this_cu->cu;
4616 struct attribute *attr;
4617 ULONGEST signature;
4618 struct dwo_unit *dwo_unit;
4619 const char *comp_dir, *dwo_name;
4620
4621 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4622 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4623 gdb_assert (attr != NULL);
4624 dwo_name = DW_STRING (attr);
4625 comp_dir = NULL;
4626 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4627 if (attr)
4628 comp_dir = DW_STRING (attr);
4629
4630 if (this_cu->is_debug_types)
4631 {
4632 struct signatured_type *sig_type;
4633
4634 /* Since this_cu is the first member of struct signatured_type,
4635 we can go from a pointer to one to a pointer to the other. */
4636 sig_type = (struct signatured_type *) this_cu;
4637 signature = sig_type->signature;
4638 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4639 }
4640 else
4641 {
4642 struct attribute *attr;
4643
4644 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4645 if (! attr)
4646 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4647 " [in module %s]"),
4648 dwo_name, this_cu->objfile->name);
4649 signature = DW_UNSND (attr);
4650 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4651 signature);
4652 }
4653
b0c7bfa9
DE
4654 return dwo_unit;
4655}
4656
fd820528 4657/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4658 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4659
f4dc4d17
DE
4660 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4661 Otherwise the table specified in the comp unit header is read in and used.
4662 This is an optimization for when we already have the abbrev table.
4663
dee91e82
DE
4664 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4665 Otherwise, a new CU is allocated with xmalloc.
4666
4667 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4668 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4669
4670 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4671 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4672
70221824 4673static void
fd820528 4674init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4675 struct abbrev_table *abbrev_table,
fd820528
DE
4676 int use_existing_cu, int keep,
4677 die_reader_func_ftype *die_reader_func,
4678 void *data)
c906108c 4679{
dee91e82 4680 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4681 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4682 bfd *abfd = section->asection->owner;
dee91e82 4683 struct dwarf2_cu *cu;
d521ce57 4684 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4685 struct die_reader_specs reader;
d85a05f0 4686 struct die_info *comp_unit_die;
dee91e82 4687 int has_children;
d85a05f0 4688 struct attribute *attr;
dee91e82
DE
4689 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4690 struct signatured_type *sig_type = NULL;
4bdcc0c1 4691 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4692 /* Non-zero if CU currently points to a DWO file and we need to
4693 reread it. When this happens we need to reread the skeleton die
4694 before we can reread the DWO file. */
4695 int rereading_dwo_cu = 0;
c906108c 4696
09406207
DE
4697 if (dwarf2_die_debug)
4698 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4699 this_cu->is_debug_types ? "type" : "comp",
4700 this_cu->offset.sect_off);
4701
dee91e82
DE
4702 if (use_existing_cu)
4703 gdb_assert (keep);
23745b47 4704
dee91e82
DE
4705 cleanups = make_cleanup (null_cleanup, NULL);
4706
4707 /* This is cheap if the section is already read in. */
4708 dwarf2_read_section (objfile, section);
4709
4710 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4711
4712 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4713
4714 if (use_existing_cu && this_cu->cu != NULL)
4715 {
4716 cu = this_cu->cu;
42e7ad6c
DE
4717
4718 /* If this CU is from a DWO file we need to start over, we need to
4719 refetch the attributes from the skeleton CU.
4720 This could be optimized by retrieving those attributes from when we
4721 were here the first time: the previous comp_unit_die was stored in
4722 comp_unit_obstack. But there's no data yet that we need this
4723 optimization. */
4724 if (cu->dwo_unit != NULL)
4725 rereading_dwo_cu = 1;
dee91e82
DE
4726 }
4727 else
4728 {
4729 /* If !use_existing_cu, this_cu->cu must be NULL. */
4730 gdb_assert (this_cu->cu == NULL);
4731
4732 cu = xmalloc (sizeof (*cu));
4733 init_one_comp_unit (cu, this_cu);
4734
4735 /* If an error occurs while loading, release our storage. */
4736 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4737 }
dee91e82 4738
b0c7bfa9 4739 /* Get the header. */
42e7ad6c
DE
4740 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4741 {
4742 /* We already have the header, there's no need to read it in again. */
4743 info_ptr += cu->header.first_die_offset.cu_off;
4744 }
4745 else
4746 {
3019eac3 4747 if (this_cu->is_debug_types)
dee91e82
DE
4748 {
4749 ULONGEST signature;
42e7ad6c 4750 cu_offset type_offset_in_tu;
dee91e82 4751
4bdcc0c1
DE
4752 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4753 abbrev_section, info_ptr,
42e7ad6c
DE
4754 &signature,
4755 &type_offset_in_tu);
dee91e82 4756
42e7ad6c
DE
4757 /* Since per_cu is the first member of struct signatured_type,
4758 we can go from a pointer to one to a pointer to the other. */
4759 sig_type = (struct signatured_type *) this_cu;
4760 gdb_assert (sig_type->signature == signature);
4761 gdb_assert (sig_type->type_offset_in_tu.cu_off
4762 == type_offset_in_tu.cu_off);
dee91e82
DE
4763 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4764
42e7ad6c
DE
4765 /* LENGTH has not been set yet for type units if we're
4766 using .gdb_index. */
1ce1cefd 4767 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4768
4769 /* Establish the type offset that can be used to lookup the type. */
4770 sig_type->type_offset_in_section.sect_off =
4771 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4772 }
4773 else
4774 {
4bdcc0c1
DE
4775 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4776 abbrev_section,
4777 info_ptr, 0);
dee91e82
DE
4778
4779 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4780 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4781 }
4782 }
10b3939b 4783
6caca83c 4784 /* Skip dummy compilation units. */
dee91e82 4785 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4786 || peek_abbrev_code (abfd, info_ptr) == 0)
4787 {
dee91e82 4788 do_cleanups (cleanups);
21b2bd31 4789 return;
6caca83c
CC
4790 }
4791
433df2d4
DE
4792 /* If we don't have them yet, read the abbrevs for this compilation unit.
4793 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4794 done. Note that it's important that if the CU had an abbrev table
4795 on entry we don't free it when we're done: Somewhere up the call stack
4796 it may be in use. */
f4dc4d17
DE
4797 if (abbrev_table != NULL)
4798 {
4799 gdb_assert (cu->abbrev_table == NULL);
4800 gdb_assert (cu->header.abbrev_offset.sect_off
4801 == abbrev_table->offset.sect_off);
4802 cu->abbrev_table = abbrev_table;
4803 }
4804 else if (cu->abbrev_table == NULL)
dee91e82 4805 {
4bdcc0c1 4806 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4807 make_cleanup (dwarf2_free_abbrev_table, cu);
4808 }
42e7ad6c
DE
4809 else if (rereading_dwo_cu)
4810 {
4811 dwarf2_free_abbrev_table (cu);
4812 dwarf2_read_abbrevs (cu, abbrev_section);
4813 }
af703f96 4814
dee91e82 4815 /* Read the top level CU/TU die. */
3019eac3 4816 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4817 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4818
b0c7bfa9
DE
4819 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4820 from the DWO file.
4821 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4822 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
4823 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4824 if (attr)
4825 {
3019eac3 4826 struct dwo_unit *dwo_unit;
b0c7bfa9 4827 struct die_info *dwo_comp_unit_die;
3019eac3
DE
4828
4829 if (has_children)
6a506a2d
DE
4830 {
4831 complaint (&symfile_complaints,
4832 _("compilation unit with DW_AT_GNU_dwo_name"
4833 " has children (offset 0x%x) [in module %s]"),
4834 this_cu->offset.sect_off, bfd_get_filename (abfd));
4835 }
b0c7bfa9 4836 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 4837 if (dwo_unit != NULL)
3019eac3 4838 {
6a506a2d
DE
4839 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4840 abbrev_table != NULL,
4841 comp_unit_die,
4842 &reader, &info_ptr,
4843 &dwo_comp_unit_die, &has_children) == 0)
4844 {
4845 /* Dummy die. */
4846 do_cleanups (cleanups);
4847 return;
4848 }
4849 comp_unit_die = dwo_comp_unit_die;
4850 }
4851 else
4852 {
4853 /* Yikes, we couldn't find the rest of the DIE, we only have
4854 the stub. A complaint has already been logged. There's
4855 not much more we can do except pass on the stub DIE to
4856 die_reader_func. We don't want to throw an error on bad
4857 debug info. */
3019eac3
DE
4858 }
4859 }
4860
b0c7bfa9 4861 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
4862 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4863
b0c7bfa9 4864 /* Done, clean up. */
dee91e82 4865 if (free_cu_cleanup != NULL)
348e048f 4866 {
dee91e82
DE
4867 if (keep)
4868 {
4869 /* We've successfully allocated this compilation unit. Let our
4870 caller clean it up when finished with it. */
4871 discard_cleanups (free_cu_cleanup);
4872
4873 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4874 So we have to manually free the abbrev table. */
4875 dwarf2_free_abbrev_table (cu);
4876
4877 /* Link this CU into read_in_chain. */
4878 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4879 dwarf2_per_objfile->read_in_chain = this_cu;
4880 }
4881 else
4882 do_cleanups (free_cu_cleanup);
348e048f 4883 }
dee91e82
DE
4884
4885 do_cleanups (cleanups);
4886}
4887
3019eac3
DE
4888/* Read CU/TU THIS_CU in section SECTION,
4889 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4890 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4891 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4892
4893 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4894 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4895
4896 We fill in THIS_CU->length.
4897
4898 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4899 linker) then DIE_READER_FUNC will not get called.
4900
4901 THIS_CU->cu is always freed when done.
3019eac3
DE
4902 This is done in order to not leave THIS_CU->cu in a state where we have
4903 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4904
4905static void
4906init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4907 struct dwarf2_section_info *abbrev_section,
3019eac3 4908 struct dwo_file *dwo_file,
dee91e82
DE
4909 die_reader_func_ftype *die_reader_func,
4910 void *data)
4911{
4912 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4913 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4914 bfd *abfd = section->asection->owner;
dee91e82 4915 struct dwarf2_cu cu;
d521ce57 4916 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
4917 struct die_reader_specs reader;
4918 struct cleanup *cleanups;
4919 struct die_info *comp_unit_die;
4920 int has_children;
4921
09406207
DE
4922 if (dwarf2_die_debug)
4923 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4924 this_cu->is_debug_types ? "type" : "comp",
4925 this_cu->offset.sect_off);
4926
dee91e82
DE
4927 gdb_assert (this_cu->cu == NULL);
4928
dee91e82
DE
4929 /* This is cheap if the section is already read in. */
4930 dwarf2_read_section (objfile, section);
4931
4932 init_one_comp_unit (&cu, this_cu);
4933
4934 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4935
4936 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4937 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4938 abbrev_section, info_ptr,
3019eac3 4939 this_cu->is_debug_types);
dee91e82 4940
1ce1cefd 4941 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4942
4943 /* Skip dummy compilation units. */
4944 if (info_ptr >= begin_info_ptr + this_cu->length
4945 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4946 {
dee91e82 4947 do_cleanups (cleanups);
21b2bd31 4948 return;
93311388 4949 }
72bf9492 4950
dee91e82
DE
4951 dwarf2_read_abbrevs (&cu, abbrev_section);
4952 make_cleanup (dwarf2_free_abbrev_table, &cu);
4953
3019eac3 4954 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4955 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4956
4957 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4958
4959 do_cleanups (cleanups);
4960}
4961
3019eac3
DE
4962/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4963 does not lookup the specified DWO file.
4964 This cannot be used to read DWO files.
dee91e82
DE
4965
4966 THIS_CU->cu is always freed when done.
3019eac3
DE
4967 This is done in order to not leave THIS_CU->cu in a state where we have
4968 to care whether it refers to the "main" CU or the DWO CU.
4969 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4970
4971static void
4972init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4973 die_reader_func_ftype *die_reader_func,
4974 void *data)
4975{
4976 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4977 get_abbrev_section_for_cu (this_cu),
3019eac3 4978 NULL,
dee91e82
DE
4979 die_reader_func, data);
4980}
0018ea6f
DE
4981\f
4982/* Type Unit Groups.
dee91e82 4983
0018ea6f
DE
4984 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4985 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4986 so that all types coming from the same compilation (.o file) are grouped
4987 together. A future step could be to put the types in the same symtab as
4988 the CU the types ultimately came from. */
ff013f42 4989
f4dc4d17
DE
4990static hashval_t
4991hash_type_unit_group (const void *item)
4992{
094b34ac 4993 const struct type_unit_group *tu_group = item;
f4dc4d17 4994
094b34ac 4995 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4996}
348e048f
DE
4997
4998static int
f4dc4d17 4999eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5000{
f4dc4d17
DE
5001 const struct type_unit_group *lhs = item_lhs;
5002 const struct type_unit_group *rhs = item_rhs;
348e048f 5003
094b34ac 5004 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5005}
348e048f 5006
f4dc4d17
DE
5007/* Allocate a hash table for type unit groups. */
5008
5009static htab_t
5010allocate_type_unit_groups_table (void)
5011{
5012 return htab_create_alloc_ex (3,
5013 hash_type_unit_group,
5014 eq_type_unit_group,
5015 NULL,
5016 &dwarf2_per_objfile->objfile->objfile_obstack,
5017 hashtab_obstack_allocate,
5018 dummy_obstack_deallocate);
5019}
dee91e82 5020
f4dc4d17
DE
5021/* Type units that don't have DW_AT_stmt_list are grouped into their own
5022 partial symtabs. We combine several TUs per psymtab to not let the size
5023 of any one psymtab grow too big. */
5024#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5025#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5026
094b34ac 5027/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5028 Create the type_unit_group object used to hold one or more TUs. */
5029
5030static struct type_unit_group *
094b34ac 5031create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5032{
5033 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5034 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5035 struct type_unit_group *tu_group;
f4dc4d17
DE
5036
5037 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5038 struct type_unit_group);
094b34ac 5039 per_cu = &tu_group->per_cu;
f4dc4d17 5040 per_cu->objfile = objfile;
f4dc4d17 5041
094b34ac
DE
5042 if (dwarf2_per_objfile->using_index)
5043 {
5044 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5045 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5046 }
5047 else
5048 {
5049 unsigned int line_offset = line_offset_struct.sect_off;
5050 struct partial_symtab *pst;
5051 char *name;
5052
5053 /* Give the symtab a useful name for debug purposes. */
5054 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5055 name = xstrprintf ("<type_units_%d>",
5056 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5057 else
5058 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5059
5060 pst = create_partial_symtab (per_cu, name);
5061 pst->anonymous = 1;
f4dc4d17 5062
094b34ac
DE
5063 xfree (name);
5064 }
f4dc4d17 5065
094b34ac
DE
5066 tu_group->hash.dwo_unit = cu->dwo_unit;
5067 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5068
5069 return tu_group;
5070}
5071
094b34ac
DE
5072/* Look up the type_unit_group for type unit CU, and create it if necessary.
5073 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5074
5075static struct type_unit_group *
094b34ac 5076get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5077{
5078 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5079 struct type_unit_group *tu_group;
5080 void **slot;
5081 unsigned int line_offset;
5082 struct type_unit_group type_unit_group_for_lookup;
5083
5084 if (dwarf2_per_objfile->type_unit_groups == NULL)
5085 {
5086 dwarf2_per_objfile->type_unit_groups =
5087 allocate_type_unit_groups_table ();
5088 }
5089
5090 /* Do we need to create a new group, or can we use an existing one? */
5091
5092 if (stmt_list)
5093 {
5094 line_offset = DW_UNSND (stmt_list);
5095 ++tu_stats->nr_symtab_sharers;
5096 }
5097 else
5098 {
5099 /* Ugh, no stmt_list. Rare, but we have to handle it.
5100 We can do various things here like create one group per TU or
5101 spread them over multiple groups to split up the expansion work.
5102 To avoid worst case scenarios (too many groups or too large groups)
5103 we, umm, group them in bunches. */
5104 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5105 | (tu_stats->nr_stmt_less_type_units
5106 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5107 ++tu_stats->nr_stmt_less_type_units;
5108 }
5109
094b34ac
DE
5110 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5111 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5112 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5113 &type_unit_group_for_lookup, INSERT);
5114 if (*slot != NULL)
5115 {
5116 tu_group = *slot;
5117 gdb_assert (tu_group != NULL);
5118 }
5119 else
5120 {
5121 sect_offset line_offset_struct;
5122
5123 line_offset_struct.sect_off = line_offset;
094b34ac 5124 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5125 *slot = tu_group;
5126 ++tu_stats->nr_symtabs;
5127 }
5128
5129 return tu_group;
5130}
5131
5132/* Struct used to sort TUs by their abbreviation table offset. */
5133
5134struct tu_abbrev_offset
5135{
5136 struct signatured_type *sig_type;
5137 sect_offset abbrev_offset;
5138};
5139
5140/* Helper routine for build_type_unit_groups, passed to qsort. */
5141
5142static int
5143sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5144{
5145 const struct tu_abbrev_offset * const *a = ap;
5146 const struct tu_abbrev_offset * const *b = bp;
5147 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5148 unsigned int boff = (*b)->abbrev_offset.sect_off;
5149
5150 return (aoff > boff) - (aoff < boff);
5151}
5152
5153/* A helper function to add a type_unit_group to a table. */
5154
5155static int
5156add_type_unit_group_to_table (void **slot, void *datum)
5157{
5158 struct type_unit_group *tu_group = *slot;
5159 struct type_unit_group ***datap = datum;
5160
5161 **datap = tu_group;
5162 ++*datap;
5163
5164 return 1;
5165}
5166
5167/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5168 each one passing FUNC,DATA.
5169
5170 The efficiency is because we sort TUs by the abbrev table they use and
5171 only read each abbrev table once. In one program there are 200K TUs
5172 sharing 8K abbrev tables.
5173
5174 The main purpose of this function is to support building the
5175 dwarf2_per_objfile->type_unit_groups table.
5176 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5177 can collapse the search space by grouping them by stmt_list.
5178 The savings can be significant, in the same program from above the 200K TUs
5179 share 8K stmt_list tables.
5180
5181 FUNC is expected to call get_type_unit_group, which will create the
5182 struct type_unit_group if necessary and add it to
5183 dwarf2_per_objfile->type_unit_groups. */
5184
5185static void
5186build_type_unit_groups (die_reader_func_ftype *func, void *data)
5187{
5188 struct objfile *objfile = dwarf2_per_objfile->objfile;
5189 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5190 struct cleanup *cleanups;
5191 struct abbrev_table *abbrev_table;
5192 sect_offset abbrev_offset;
5193 struct tu_abbrev_offset *sorted_by_abbrev;
5194 struct type_unit_group **iter;
5195 int i;
5196
5197 /* It's up to the caller to not call us multiple times. */
5198 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5199
5200 if (dwarf2_per_objfile->n_type_units == 0)
5201 return;
5202
5203 /* TUs typically share abbrev tables, and there can be way more TUs than
5204 abbrev tables. Sort by abbrev table to reduce the number of times we
5205 read each abbrev table in.
5206 Alternatives are to punt or to maintain a cache of abbrev tables.
5207 This is simpler and efficient enough for now.
5208
5209 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5210 symtab to use). Typically TUs with the same abbrev offset have the same
5211 stmt_list value too so in practice this should work well.
5212
5213 The basic algorithm here is:
5214
5215 sort TUs by abbrev table
5216 for each TU with same abbrev table:
5217 read abbrev table if first user
5218 read TU top level DIE
5219 [IWBN if DWO skeletons had DW_AT_stmt_list]
5220 call FUNC */
5221
5222 if (dwarf2_read_debug)
5223 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5224
5225 /* Sort in a separate table to maintain the order of all_type_units
5226 for .gdb_index: TU indices directly index all_type_units. */
5227 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5228 dwarf2_per_objfile->n_type_units);
5229 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5230 {
5231 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5232
5233 sorted_by_abbrev[i].sig_type = sig_type;
5234 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5235 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5236 sig_type->per_cu.offset);
5237 }
5238 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5239 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5240 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5241
094b34ac
DE
5242 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5243 called any number of times, so we don't reset tu_stats here. */
5244
f4dc4d17
DE
5245 abbrev_offset.sect_off = ~(unsigned) 0;
5246 abbrev_table = NULL;
5247 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5248
5249 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5250 {
5251 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5252
5253 /* Switch to the next abbrev table if necessary. */
5254 if (abbrev_table == NULL
5255 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5256 {
5257 if (abbrev_table != NULL)
5258 {
5259 abbrev_table_free (abbrev_table);
5260 /* Reset to NULL in case abbrev_table_read_table throws
5261 an error: abbrev_table_free_cleanup will get called. */
5262 abbrev_table = NULL;
5263 }
5264 abbrev_offset = tu->abbrev_offset;
5265 abbrev_table =
5266 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5267 abbrev_offset);
5268 ++tu_stats->nr_uniq_abbrev_tables;
5269 }
5270
5271 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5272 func, data);
5273 }
5274
5275 /* Create a vector of pointers to primary type units to make it easy to
5276 iterate over them and CUs. See dw2_get_primary_cu. */
5277 dwarf2_per_objfile->n_type_unit_groups =
5278 htab_elements (dwarf2_per_objfile->type_unit_groups);
5279 dwarf2_per_objfile->all_type_unit_groups =
5280 obstack_alloc (&objfile->objfile_obstack,
5281 dwarf2_per_objfile->n_type_unit_groups
5282 * sizeof (struct type_unit_group *));
5283 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5284 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5285 add_type_unit_group_to_table, &iter);
5286 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5287 == dwarf2_per_objfile->n_type_unit_groups);
5288
5289 do_cleanups (cleanups);
5290
5291 if (dwarf2_read_debug)
5292 {
5293 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5294 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5295 dwarf2_per_objfile->n_type_units);
5296 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5297 tu_stats->nr_uniq_abbrev_tables);
5298 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5299 tu_stats->nr_symtabs);
5300 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5301 tu_stats->nr_symtab_sharers);
5302 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5303 tu_stats->nr_stmt_less_type_units);
5304 }
5305}
0018ea6f
DE
5306\f
5307/* Partial symbol tables. */
5308
5309/* Create a psymtab named NAME and assign it to PER_CU.
5310
5311 The caller must fill in the following details:
5312 dirname, textlow, texthigh. */
5313
5314static struct partial_symtab *
5315create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5316{
5317 struct objfile *objfile = per_cu->objfile;
5318 struct partial_symtab *pst;
5319
5320 pst = start_psymtab_common (objfile, objfile->section_offsets,
5321 name, 0,
5322 objfile->global_psymbols.next,
5323 objfile->static_psymbols.next);
5324
5325 pst->psymtabs_addrmap_supported = 1;
5326
5327 /* This is the glue that links PST into GDB's symbol API. */
5328 pst->read_symtab_private = per_cu;
5329 pst->read_symtab = dwarf2_read_symtab;
5330 per_cu->v.psymtab = pst;
5331
5332 return pst;
5333}
5334
5335/* die_reader_func for process_psymtab_comp_unit. */
5336
5337static void
5338process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5339 const gdb_byte *info_ptr,
0018ea6f
DE
5340 struct die_info *comp_unit_die,
5341 int has_children,
5342 void *data)
5343{
5344 struct dwarf2_cu *cu = reader->cu;
5345 struct objfile *objfile = cu->objfile;
5346 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5347 struct attribute *attr;
5348 CORE_ADDR baseaddr;
5349 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5350 struct partial_symtab *pst;
5351 int has_pc_info;
5352 const char *filename;
5353 int *want_partial_unit_ptr = data;
5354
5355 if (comp_unit_die->tag == DW_TAG_partial_unit
5356 && (want_partial_unit_ptr == NULL
5357 || !*want_partial_unit_ptr))
5358 return;
5359
5360 gdb_assert (! per_cu->is_debug_types);
5361
5362 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5363
5364 cu->list_in_scope = &file_symbols;
5365
5366 /* Allocate a new partial symbol table structure. */
5367 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5368 if (attr == NULL || !DW_STRING (attr))
5369 filename = "";
5370 else
5371 filename = DW_STRING (attr);
5372
5373 pst = create_partial_symtab (per_cu, filename);
5374
5375 /* This must be done before calling dwarf2_build_include_psymtabs. */
5376 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5377 if (attr != NULL)
5378 pst->dirname = DW_STRING (attr);
5379
5380 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5381
5382 dwarf2_find_base_address (comp_unit_die, cu);
5383
5384 /* Possibly set the default values of LOWPC and HIGHPC from
5385 `DW_AT_ranges'. */
5386 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5387 &best_highpc, cu, pst);
5388 if (has_pc_info == 1 && best_lowpc < best_highpc)
5389 /* Store the contiguous range if it is not empty; it can be empty for
5390 CUs with no code. */
5391 addrmap_set_empty (objfile->psymtabs_addrmap,
5392 best_lowpc + baseaddr,
5393 best_highpc + baseaddr - 1, pst);
5394
5395 /* Check if comp unit has_children.
5396 If so, read the rest of the partial symbols from this comp unit.
5397 If not, there's no more debug_info for this comp unit. */
5398 if (has_children)
5399 {
5400 struct partial_die_info *first_die;
5401 CORE_ADDR lowpc, highpc;
5402
5403 lowpc = ((CORE_ADDR) -1);
5404 highpc = ((CORE_ADDR) 0);
5405
5406 first_die = load_partial_dies (reader, info_ptr, 1);
5407
5408 scan_partial_symbols (first_die, &lowpc, &highpc,
5409 ! has_pc_info, cu);
5410
5411 /* If we didn't find a lowpc, set it to highpc to avoid
5412 complaints from `maint check'. */
5413 if (lowpc == ((CORE_ADDR) -1))
5414 lowpc = highpc;
5415
5416 /* If the compilation unit didn't have an explicit address range,
5417 then use the information extracted from its child dies. */
5418 if (! has_pc_info)
5419 {
5420 best_lowpc = lowpc;
5421 best_highpc = highpc;
5422 }
5423 }
5424 pst->textlow = best_lowpc + baseaddr;
5425 pst->texthigh = best_highpc + baseaddr;
5426
5427 pst->n_global_syms = objfile->global_psymbols.next -
5428 (objfile->global_psymbols.list + pst->globals_offset);
5429 pst->n_static_syms = objfile->static_psymbols.next -
5430 (objfile->static_psymbols.list + pst->statics_offset);
5431 sort_pst_symbols (objfile, pst);
5432
5433 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5434 {
5435 int i;
5436 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5437 struct dwarf2_per_cu_data *iter;
5438
5439 /* Fill in 'dependencies' here; we fill in 'users' in a
5440 post-pass. */
5441 pst->number_of_dependencies = len;
5442 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5443 len * sizeof (struct symtab *));
5444 for (i = 0;
5445 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5446 i, iter);
5447 ++i)
5448 pst->dependencies[i] = iter->v.psymtab;
5449
5450 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5451 }
5452
5453 /* Get the list of files included in the current compilation unit,
5454 and build a psymtab for each of them. */
5455 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5456
5457 if (dwarf2_read_debug)
5458 {
5459 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5460
5461 fprintf_unfiltered (gdb_stdlog,
5462 "Psymtab for %s unit @0x%x: %s - %s"
5463 ", %d global, %d static syms\n",
5464 per_cu->is_debug_types ? "type" : "comp",
5465 per_cu->offset.sect_off,
5466 paddress (gdbarch, pst->textlow),
5467 paddress (gdbarch, pst->texthigh),
5468 pst->n_global_syms, pst->n_static_syms);
5469 }
5470}
5471
5472/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5473 Process compilation unit THIS_CU for a psymtab. */
5474
5475static void
5476process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5477 int want_partial_unit)
5478{
5479 /* If this compilation unit was already read in, free the
5480 cached copy in order to read it in again. This is
5481 necessary because we skipped some symbols when we first
5482 read in the compilation unit (see load_partial_dies).
5483 This problem could be avoided, but the benefit is unclear. */
5484 if (this_cu->cu != NULL)
5485 free_one_cached_comp_unit (this_cu);
5486
5487 gdb_assert (! this_cu->is_debug_types);
5488 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5489 process_psymtab_comp_unit_reader,
5490 &want_partial_unit);
5491
5492 /* Age out any secondary CUs. */
5493 age_cached_comp_units ();
5494}
f4dc4d17
DE
5495
5496/* Reader function for build_type_psymtabs. */
5497
5498static void
5499build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5500 const gdb_byte *info_ptr,
f4dc4d17
DE
5501 struct die_info *type_unit_die,
5502 int has_children,
5503 void *data)
5504{
5505 struct objfile *objfile = dwarf2_per_objfile->objfile;
5506 struct dwarf2_cu *cu = reader->cu;
5507 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5508 struct signatured_type *sig_type;
f4dc4d17
DE
5509 struct type_unit_group *tu_group;
5510 struct attribute *attr;
5511 struct partial_die_info *first_die;
5512 CORE_ADDR lowpc, highpc;
5513 struct partial_symtab *pst;
5514
5515 gdb_assert (data == NULL);
0186c6a7
DE
5516 gdb_assert (per_cu->is_debug_types);
5517 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5518
5519 if (! has_children)
5520 return;
5521
5522 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5523 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5524
0186c6a7 5525 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5526
5527 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5528 cu->list_in_scope = &file_symbols;
5529 pst = create_partial_symtab (per_cu, "");
5530 pst->anonymous = 1;
5531
5532 first_die = load_partial_dies (reader, info_ptr, 1);
5533
5534 lowpc = (CORE_ADDR) -1;
5535 highpc = (CORE_ADDR) 0;
5536 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5537
5538 pst->n_global_syms = objfile->global_psymbols.next -
5539 (objfile->global_psymbols.list + pst->globals_offset);
5540 pst->n_static_syms = objfile->static_psymbols.next -
5541 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5542 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5543}
5544
5545/* Traversal function for build_type_psymtabs. */
5546
5547static int
5548build_type_psymtab_dependencies (void **slot, void *info)
5549{
5550 struct objfile *objfile = dwarf2_per_objfile->objfile;
5551 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5552 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5553 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5554 int len = VEC_length (sig_type_ptr, tu_group->tus);
5555 struct signatured_type *iter;
f4dc4d17
DE
5556 int i;
5557
5558 gdb_assert (len > 0);
0186c6a7 5559 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5560
5561 pst->number_of_dependencies = len;
5562 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5563 len * sizeof (struct psymtab *));
5564 for (i = 0;
0186c6a7 5565 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5566 ++i)
5567 {
0186c6a7
DE
5568 gdb_assert (iter->per_cu.is_debug_types);
5569 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5570 iter->type_unit_group = tu_group;
f4dc4d17
DE
5571 }
5572
0186c6a7 5573 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5574
5575 return 1;
5576}
5577
5578/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5579 Build partial symbol tables for the .debug_types comp-units. */
5580
5581static void
5582build_type_psymtabs (struct objfile *objfile)
5583{
0e50663e 5584 if (! create_all_type_units (objfile))
348e048f
DE
5585 return;
5586
f4dc4d17
DE
5587 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5588
5589 /* Now that all TUs have been processed we can fill in the dependencies. */
5590 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5591 build_type_psymtab_dependencies, NULL);
348e048f
DE
5592}
5593
60606b2c
TT
5594/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5595
5596static void
5597psymtabs_addrmap_cleanup (void *o)
5598{
5599 struct objfile *objfile = o;
ec61707d 5600
60606b2c
TT
5601 objfile->psymtabs_addrmap = NULL;
5602}
5603
95554aad
TT
5604/* Compute the 'user' field for each psymtab in OBJFILE. */
5605
5606static void
5607set_partial_user (struct objfile *objfile)
5608{
5609 int i;
5610
5611 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5612 {
5613 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5614 struct partial_symtab *pst = per_cu->v.psymtab;
5615 int j;
5616
36586728
TT
5617 if (pst == NULL)
5618 continue;
5619
95554aad
TT
5620 for (j = 0; j < pst->number_of_dependencies; ++j)
5621 {
5622 /* Set the 'user' field only if it is not already set. */
5623 if (pst->dependencies[j]->user == NULL)
5624 pst->dependencies[j]->user = pst;
5625 }
5626 }
5627}
5628
93311388
DE
5629/* Build the partial symbol table by doing a quick pass through the
5630 .debug_info and .debug_abbrev sections. */
72bf9492 5631
93311388 5632static void
c67a9c90 5633dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5634{
60606b2c
TT
5635 struct cleanup *back_to, *addrmap_cleanup;
5636 struct obstack temp_obstack;
21b2bd31 5637 int i;
93311388 5638
45cfd468
DE
5639 if (dwarf2_read_debug)
5640 {
5641 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5642 objfile->name);
5643 }
5644
98bfdba5
PA
5645 dwarf2_per_objfile->reading_partial_symbols = 1;
5646
be391dca 5647 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5648
93311388
DE
5649 /* Any cached compilation units will be linked by the per-objfile
5650 read_in_chain. Make sure to free them when we're done. */
5651 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5652
348e048f
DE
5653 build_type_psymtabs (objfile);
5654
93311388 5655 create_all_comp_units (objfile);
c906108c 5656
60606b2c
TT
5657 /* Create a temporary address map on a temporary obstack. We later
5658 copy this to the final obstack. */
5659 obstack_init (&temp_obstack);
5660 make_cleanup_obstack_free (&temp_obstack);
5661 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5662 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5663
21b2bd31 5664 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5665 {
21b2bd31 5666 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5667
95554aad 5668 process_psymtab_comp_unit (per_cu, 0);
c906108c 5669 }
ff013f42 5670
95554aad
TT
5671 set_partial_user (objfile);
5672
ff013f42
JK
5673 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5674 &objfile->objfile_obstack);
60606b2c 5675 discard_cleanups (addrmap_cleanup);
ff013f42 5676
ae038cb0 5677 do_cleanups (back_to);
45cfd468
DE
5678
5679 if (dwarf2_read_debug)
5680 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5681 objfile->name);
ae038cb0
DJ
5682}
5683
3019eac3 5684/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5685
5686static void
dee91e82 5687load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5688 const gdb_byte *info_ptr,
dee91e82
DE
5689 struct die_info *comp_unit_die,
5690 int has_children,
5691 void *data)
ae038cb0 5692{
dee91e82 5693 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5694
95554aad 5695 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5696
ae038cb0
DJ
5697 /* Check if comp unit has_children.
5698 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5699 If not, there's no more debug_info for this comp unit. */
d85a05f0 5700 if (has_children)
dee91e82
DE
5701 load_partial_dies (reader, info_ptr, 0);
5702}
98bfdba5 5703
dee91e82
DE
5704/* Load the partial DIEs for a secondary CU into memory.
5705 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5706
dee91e82
DE
5707static void
5708load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5709{
f4dc4d17
DE
5710 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5711 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5712}
5713
ae038cb0 5714static void
36586728
TT
5715read_comp_units_from_section (struct objfile *objfile,
5716 struct dwarf2_section_info *section,
5717 unsigned int is_dwz,
5718 int *n_allocated,
5719 int *n_comp_units,
5720 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5721{
d521ce57 5722 const gdb_byte *info_ptr;
36586728 5723 bfd *abfd = section->asection->owner;
be391dca 5724
bf6af496
DE
5725 if (dwarf2_read_debug)
5726 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
5727 section->asection->name, bfd_get_filename (abfd));
5728
36586728 5729 dwarf2_read_section (objfile, section);
ae038cb0 5730
36586728 5731 info_ptr = section->buffer;
6e70227d 5732
36586728 5733 while (info_ptr < section->buffer + section->size)
ae038cb0 5734 {
c764a876 5735 unsigned int length, initial_length_size;
ae038cb0 5736 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5737 sect_offset offset;
ae038cb0 5738
36586728 5739 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5740
5741 /* Read just enough information to find out where the next
5742 compilation unit is. */
36586728 5743 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5744
5745 /* Save the compilation unit for later lookup. */
5746 this_cu = obstack_alloc (&objfile->objfile_obstack,
5747 sizeof (struct dwarf2_per_cu_data));
5748 memset (this_cu, 0, sizeof (*this_cu));
5749 this_cu->offset = offset;
c764a876 5750 this_cu->length = length + initial_length_size;
36586728 5751 this_cu->is_dwz = is_dwz;
9291a0cd 5752 this_cu->objfile = objfile;
8a0459fd 5753 this_cu->section = section;
ae038cb0 5754
36586728 5755 if (*n_comp_units == *n_allocated)
ae038cb0 5756 {
36586728
TT
5757 *n_allocated *= 2;
5758 *all_comp_units = xrealloc (*all_comp_units,
5759 *n_allocated
5760 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5761 }
36586728
TT
5762 (*all_comp_units)[*n_comp_units] = this_cu;
5763 ++*n_comp_units;
ae038cb0
DJ
5764
5765 info_ptr = info_ptr + this_cu->length;
5766 }
36586728
TT
5767}
5768
5769/* Create a list of all compilation units in OBJFILE.
5770 This is only done for -readnow and building partial symtabs. */
5771
5772static void
5773create_all_comp_units (struct objfile *objfile)
5774{
5775 int n_allocated;
5776 int n_comp_units;
5777 struct dwarf2_per_cu_data **all_comp_units;
5778
5779 n_comp_units = 0;
5780 n_allocated = 10;
5781 all_comp_units = xmalloc (n_allocated
5782 * sizeof (struct dwarf2_per_cu_data *));
5783
5784 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5785 &n_allocated, &n_comp_units, &all_comp_units);
5786
5787 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5788 {
5789 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5790
5791 read_comp_units_from_section (objfile, &dwz->info, 1,
5792 &n_allocated, &n_comp_units,
5793 &all_comp_units);
5794 }
ae038cb0
DJ
5795
5796 dwarf2_per_objfile->all_comp_units
5797 = obstack_alloc (&objfile->objfile_obstack,
5798 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5799 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5800 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5801 xfree (all_comp_units);
5802 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5803}
5804
5734ee8b
DJ
5805/* Process all loaded DIEs for compilation unit CU, starting at
5806 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5807 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5808 DW_AT_ranges). If NEED_PC is set, then this function will set
5809 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5810 and record the covered ranges in the addrmap. */
c906108c 5811
72bf9492
DJ
5812static void
5813scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5814 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5815{
72bf9492 5816 struct partial_die_info *pdi;
c906108c 5817
91c24f0a
DC
5818 /* Now, march along the PDI's, descending into ones which have
5819 interesting children but skipping the children of the other ones,
5820 until we reach the end of the compilation unit. */
c906108c 5821
72bf9492 5822 pdi = first_die;
91c24f0a 5823
72bf9492
DJ
5824 while (pdi != NULL)
5825 {
5826 fixup_partial_die (pdi, cu);
c906108c 5827
f55ee35c 5828 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5829 children, so we need to look at them. Ditto for anonymous
5830 enums. */
933c6fe4 5831
72bf9492 5832 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5833 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5834 || pdi->tag == DW_TAG_imported_unit)
c906108c 5835 {
72bf9492 5836 switch (pdi->tag)
c906108c
SS
5837 {
5838 case DW_TAG_subprogram:
5734ee8b 5839 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5840 break;
72929c62 5841 case DW_TAG_constant:
c906108c
SS
5842 case DW_TAG_variable:
5843 case DW_TAG_typedef:
91c24f0a 5844 case DW_TAG_union_type:
72bf9492 5845 if (!pdi->is_declaration)
63d06c5c 5846 {
72bf9492 5847 add_partial_symbol (pdi, cu);
63d06c5c
DC
5848 }
5849 break;
c906108c 5850 case DW_TAG_class_type:
680b30c7 5851 case DW_TAG_interface_type:
c906108c 5852 case DW_TAG_structure_type:
72bf9492 5853 if (!pdi->is_declaration)
c906108c 5854 {
72bf9492 5855 add_partial_symbol (pdi, cu);
c906108c
SS
5856 }
5857 break;
91c24f0a 5858 case DW_TAG_enumeration_type:
72bf9492
DJ
5859 if (!pdi->is_declaration)
5860 add_partial_enumeration (pdi, cu);
c906108c
SS
5861 break;
5862 case DW_TAG_base_type:
a02abb62 5863 case DW_TAG_subrange_type:
c906108c 5864 /* File scope base type definitions are added to the partial
c5aa993b 5865 symbol table. */
72bf9492 5866 add_partial_symbol (pdi, cu);
c906108c 5867 break;
d9fa45fe 5868 case DW_TAG_namespace:
5734ee8b 5869 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5870 break;
5d7cb8df
JK
5871 case DW_TAG_module:
5872 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5873 break;
95554aad
TT
5874 case DW_TAG_imported_unit:
5875 {
5876 struct dwarf2_per_cu_data *per_cu;
5877
f4dc4d17
DE
5878 /* For now we don't handle imported units in type units. */
5879 if (cu->per_cu->is_debug_types)
5880 {
5881 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5882 " supported in type units [in module %s]"),
5883 cu->objfile->name);
5884 }
5885
95554aad 5886 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5887 pdi->is_dwz,
95554aad
TT
5888 cu->objfile);
5889
5890 /* Go read the partial unit, if needed. */
5891 if (per_cu->v.psymtab == NULL)
5892 process_psymtab_comp_unit (per_cu, 1);
5893
f4dc4d17 5894 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5895 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5896 }
5897 break;
c906108c
SS
5898 default:
5899 break;
5900 }
5901 }
5902
72bf9492
DJ
5903 /* If the die has a sibling, skip to the sibling. */
5904
5905 pdi = pdi->die_sibling;
5906 }
5907}
5908
5909/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5910
72bf9492 5911 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5912 name is concatenated with "::" and the partial DIE's name. For
5913 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5914 Enumerators are an exception; they use the scope of their parent
5915 enumeration type, i.e. the name of the enumeration type is not
5916 prepended to the enumerator.
91c24f0a 5917
72bf9492
DJ
5918 There are two complexities. One is DW_AT_specification; in this
5919 case "parent" means the parent of the target of the specification,
5920 instead of the direct parent of the DIE. The other is compilers
5921 which do not emit DW_TAG_namespace; in this case we try to guess
5922 the fully qualified name of structure types from their members'
5923 linkage names. This must be done using the DIE's children rather
5924 than the children of any DW_AT_specification target. We only need
5925 to do this for structures at the top level, i.e. if the target of
5926 any DW_AT_specification (if any; otherwise the DIE itself) does not
5927 have a parent. */
5928
5929/* Compute the scope prefix associated with PDI's parent, in
5930 compilation unit CU. The result will be allocated on CU's
5931 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5932 field. NULL is returned if no prefix is necessary. */
15d034d0 5933static const char *
72bf9492
DJ
5934partial_die_parent_scope (struct partial_die_info *pdi,
5935 struct dwarf2_cu *cu)
5936{
15d034d0 5937 const char *grandparent_scope;
72bf9492 5938 struct partial_die_info *parent, *real_pdi;
91c24f0a 5939
72bf9492
DJ
5940 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5941 then this means the parent of the specification DIE. */
5942
5943 real_pdi = pdi;
72bf9492 5944 while (real_pdi->has_specification)
36586728
TT
5945 real_pdi = find_partial_die (real_pdi->spec_offset,
5946 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5947
5948 parent = real_pdi->die_parent;
5949 if (parent == NULL)
5950 return NULL;
5951
5952 if (parent->scope_set)
5953 return parent->scope;
5954
5955 fixup_partial_die (parent, cu);
5956
10b3939b 5957 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5958
acebe513
UW
5959 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5960 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5961 Work around this problem here. */
5962 if (cu->language == language_cplus
6e70227d 5963 && parent->tag == DW_TAG_namespace
acebe513
UW
5964 && strcmp (parent->name, "::") == 0
5965 && grandparent_scope == NULL)
5966 {
5967 parent->scope = NULL;
5968 parent->scope_set = 1;
5969 return NULL;
5970 }
5971
9c6c53f7
SA
5972 if (pdi->tag == DW_TAG_enumerator)
5973 /* Enumerators should not get the name of the enumeration as a prefix. */
5974 parent->scope = grandparent_scope;
5975 else if (parent->tag == DW_TAG_namespace
f55ee35c 5976 || parent->tag == DW_TAG_module
72bf9492
DJ
5977 || parent->tag == DW_TAG_structure_type
5978 || parent->tag == DW_TAG_class_type
680b30c7 5979 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5980 || parent->tag == DW_TAG_union_type
5981 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5982 {
5983 if (grandparent_scope == NULL)
5984 parent->scope = parent->name;
5985 else
3e43a32a
MS
5986 parent->scope = typename_concat (&cu->comp_unit_obstack,
5987 grandparent_scope,
f55ee35c 5988 parent->name, 0, cu);
72bf9492 5989 }
72bf9492
DJ
5990 else
5991 {
5992 /* FIXME drow/2004-04-01: What should we be doing with
5993 function-local names? For partial symbols, we should probably be
5994 ignoring them. */
5995 complaint (&symfile_complaints,
e2e0b3e5 5996 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5997 parent->tag, pdi->offset.sect_off);
72bf9492 5998 parent->scope = grandparent_scope;
c906108c
SS
5999 }
6000
72bf9492
DJ
6001 parent->scope_set = 1;
6002 return parent->scope;
6003}
6004
6005/* Return the fully scoped name associated with PDI, from compilation unit
6006 CU. The result will be allocated with malloc. */
4568ecf9 6007
72bf9492
DJ
6008static char *
6009partial_die_full_name (struct partial_die_info *pdi,
6010 struct dwarf2_cu *cu)
6011{
15d034d0 6012 const char *parent_scope;
72bf9492 6013
98bfdba5
PA
6014 /* If this is a template instantiation, we can not work out the
6015 template arguments from partial DIEs. So, unfortunately, we have
6016 to go through the full DIEs. At least any work we do building
6017 types here will be reused if full symbols are loaded later. */
6018 if (pdi->has_template_arguments)
6019 {
6020 fixup_partial_die (pdi, cu);
6021
6022 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6023 {
6024 struct die_info *die;
6025 struct attribute attr;
6026 struct dwarf2_cu *ref_cu = cu;
6027
b64f50a1 6028 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6029 attr.name = 0;
6030 attr.form = DW_FORM_ref_addr;
4568ecf9 6031 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6032 die = follow_die_ref (NULL, &attr, &ref_cu);
6033
6034 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6035 }
6036 }
6037
72bf9492
DJ
6038 parent_scope = partial_die_parent_scope (pdi, cu);
6039 if (parent_scope == NULL)
6040 return NULL;
6041 else
f55ee35c 6042 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6043}
6044
6045static void
72bf9492 6046add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6047{
e7c27a73 6048 struct objfile *objfile = cu->objfile;
c906108c 6049 CORE_ADDR addr = 0;
15d034d0 6050 const char *actual_name = NULL;
e142c38c 6051 CORE_ADDR baseaddr;
15d034d0 6052 char *built_actual_name;
e142c38c
DJ
6053
6054 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6055
15d034d0
TT
6056 built_actual_name = partial_die_full_name (pdi, cu);
6057 if (built_actual_name != NULL)
6058 actual_name = built_actual_name;
63d06c5c 6059
72bf9492
DJ
6060 if (actual_name == NULL)
6061 actual_name = pdi->name;
6062
c906108c
SS
6063 switch (pdi->tag)
6064 {
6065 case DW_TAG_subprogram:
2cfa0c8d 6066 if (pdi->is_external || cu->language == language_ada)
c906108c 6067 {
2cfa0c8d
JB
6068 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6069 of the global scope. But in Ada, we want to be able to access
6070 nested procedures globally. So all Ada subprograms are stored
6071 in the global scope. */
f47fb265 6072 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6073 mst_text, objfile); */
f47fb265 6074 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6075 built_actual_name != NULL,
f47fb265
MS
6076 VAR_DOMAIN, LOC_BLOCK,
6077 &objfile->global_psymbols,
6078 0, pdi->lowpc + baseaddr,
6079 cu->language, objfile);
c906108c
SS
6080 }
6081 else
6082 {
f47fb265 6083 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6084 mst_file_text, objfile); */
f47fb265 6085 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6086 built_actual_name != NULL,
f47fb265
MS
6087 VAR_DOMAIN, LOC_BLOCK,
6088 &objfile->static_psymbols,
6089 0, pdi->lowpc + baseaddr,
6090 cu->language, objfile);
c906108c
SS
6091 }
6092 break;
72929c62
JB
6093 case DW_TAG_constant:
6094 {
6095 struct psymbol_allocation_list *list;
6096
6097 if (pdi->is_external)
6098 list = &objfile->global_psymbols;
6099 else
6100 list = &objfile->static_psymbols;
f47fb265 6101 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6102 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6103 list, 0, 0, cu->language, objfile);
72929c62
JB
6104 }
6105 break;
c906108c 6106 case DW_TAG_variable:
95554aad
TT
6107 if (pdi->d.locdesc)
6108 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6109
95554aad 6110 if (pdi->d.locdesc
caac4577
JG
6111 && addr == 0
6112 && !dwarf2_per_objfile->has_section_at_zero)
6113 {
6114 /* A global or static variable may also have been stripped
6115 out by the linker if unused, in which case its address
6116 will be nullified; do not add such variables into partial
6117 symbol table then. */
6118 }
6119 else if (pdi->is_external)
c906108c
SS
6120 {
6121 /* Global Variable.
6122 Don't enter into the minimal symbol tables as there is
6123 a minimal symbol table entry from the ELF symbols already.
6124 Enter into partial symbol table if it has a location
6125 descriptor or a type.
6126 If the location descriptor is missing, new_symbol will create
6127 a LOC_UNRESOLVED symbol, the address of the variable will then
6128 be determined from the minimal symbol table whenever the variable
6129 is referenced.
6130 The address for the partial symbol table entry is not
6131 used by GDB, but it comes in handy for debugging partial symbol
6132 table building. */
6133
95554aad 6134 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6135 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6136 built_actual_name != NULL,
f47fb265
MS
6137 VAR_DOMAIN, LOC_STATIC,
6138 &objfile->global_psymbols,
6139 0, addr + baseaddr,
6140 cu->language, objfile);
c906108c
SS
6141 }
6142 else
6143 {
0963b4bd 6144 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6145 if (pdi->d.locdesc == NULL)
decbce07 6146 {
15d034d0 6147 xfree (built_actual_name);
decbce07
MS
6148 return;
6149 }
f47fb265 6150 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6151 mst_file_data, objfile); */
f47fb265 6152 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6153 built_actual_name != NULL,
f47fb265
MS
6154 VAR_DOMAIN, LOC_STATIC,
6155 &objfile->static_psymbols,
6156 0, addr + baseaddr,
6157 cu->language, objfile);
c906108c
SS
6158 }
6159 break;
6160 case DW_TAG_typedef:
6161 case DW_TAG_base_type:
a02abb62 6162 case DW_TAG_subrange_type:
38d518c9 6163 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6164 built_actual_name != NULL,
176620f1 6165 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6166 &objfile->static_psymbols,
e142c38c 6167 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6168 break;
72bf9492
DJ
6169 case DW_TAG_namespace:
6170 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6171 built_actual_name != NULL,
72bf9492
DJ
6172 VAR_DOMAIN, LOC_TYPEDEF,
6173 &objfile->global_psymbols,
6174 0, (CORE_ADDR) 0, cu->language, objfile);
6175 break;
c906108c 6176 case DW_TAG_class_type:
680b30c7 6177 case DW_TAG_interface_type:
c906108c
SS
6178 case DW_TAG_structure_type:
6179 case DW_TAG_union_type:
6180 case DW_TAG_enumeration_type:
fa4028e9
JB
6181 /* Skip external references. The DWARF standard says in the section
6182 about "Structure, Union, and Class Type Entries": "An incomplete
6183 structure, union or class type is represented by a structure,
6184 union or class entry that does not have a byte size attribute
6185 and that has a DW_AT_declaration attribute." */
6186 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6187 {
15d034d0 6188 xfree (built_actual_name);
decbce07
MS
6189 return;
6190 }
fa4028e9 6191
63d06c5c
DC
6192 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6193 static vs. global. */
38d518c9 6194 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6195 built_actual_name != NULL,
176620f1 6196 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6197 (cu->language == language_cplus
6198 || cu->language == language_java)
63d06c5c
DC
6199 ? &objfile->global_psymbols
6200 : &objfile->static_psymbols,
e142c38c 6201 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6202
c906108c
SS
6203 break;
6204 case DW_TAG_enumerator:
38d518c9 6205 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6206 built_actual_name != NULL,
176620f1 6207 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6208 (cu->language == language_cplus
6209 || cu->language == language_java)
f6fe98ef
DJ
6210 ? &objfile->global_psymbols
6211 : &objfile->static_psymbols,
e142c38c 6212 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6213 break;
6214 default:
6215 break;
6216 }
5c4e30ca 6217
15d034d0 6218 xfree (built_actual_name);
c906108c
SS
6219}
6220
5c4e30ca
DC
6221/* Read a partial die corresponding to a namespace; also, add a symbol
6222 corresponding to that namespace to the symbol table. NAMESPACE is
6223 the name of the enclosing namespace. */
91c24f0a 6224
72bf9492
DJ
6225static void
6226add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6227 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6228 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6229{
72bf9492 6230 /* Add a symbol for the namespace. */
e7c27a73 6231
72bf9492 6232 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6233
6234 /* Now scan partial symbols in that namespace. */
6235
91c24f0a 6236 if (pdi->has_children)
5734ee8b 6237 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6238}
6239
5d7cb8df
JK
6240/* Read a partial die corresponding to a Fortran module. */
6241
6242static void
6243add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6244 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6245{
f55ee35c 6246 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6247
6248 if (pdi->has_children)
6249 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6250}
6251
bc30ff58
JB
6252/* Read a partial die corresponding to a subprogram and create a partial
6253 symbol for that subprogram. When the CU language allows it, this
6254 routine also defines a partial symbol for each nested subprogram
6255 that this subprogram contains.
6e70227d 6256
bc30ff58
JB
6257 DIE my also be a lexical block, in which case we simply search
6258 recursively for suprograms defined inside that lexical block.
6259 Again, this is only performed when the CU language allows this
6260 type of definitions. */
6261
6262static void
6263add_partial_subprogram (struct partial_die_info *pdi,
6264 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6265 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6266{
6267 if (pdi->tag == DW_TAG_subprogram)
6268 {
6269 if (pdi->has_pc_info)
6270 {
6271 if (pdi->lowpc < *lowpc)
6272 *lowpc = pdi->lowpc;
6273 if (pdi->highpc > *highpc)
6274 *highpc = pdi->highpc;
5734ee8b
DJ
6275 if (need_pc)
6276 {
6277 CORE_ADDR baseaddr;
6278 struct objfile *objfile = cu->objfile;
6279
6280 baseaddr = ANOFFSET (objfile->section_offsets,
6281 SECT_OFF_TEXT (objfile));
6282 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6283 pdi->lowpc + baseaddr,
6284 pdi->highpc - 1 + baseaddr,
9291a0cd 6285 cu->per_cu->v.psymtab);
5734ee8b 6286 }
481860b3
GB
6287 }
6288
6289 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6290 {
bc30ff58 6291 if (!pdi->is_declaration)
e8d05480
JB
6292 /* Ignore subprogram DIEs that do not have a name, they are
6293 illegal. Do not emit a complaint at this point, we will
6294 do so when we convert this psymtab into a symtab. */
6295 if (pdi->name)
6296 add_partial_symbol (pdi, cu);
bc30ff58
JB
6297 }
6298 }
6e70227d 6299
bc30ff58
JB
6300 if (! pdi->has_children)
6301 return;
6302
6303 if (cu->language == language_ada)
6304 {
6305 pdi = pdi->die_child;
6306 while (pdi != NULL)
6307 {
6308 fixup_partial_die (pdi, cu);
6309 if (pdi->tag == DW_TAG_subprogram
6310 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6311 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6312 pdi = pdi->die_sibling;
6313 }
6314 }
6315}
6316
91c24f0a
DC
6317/* Read a partial die corresponding to an enumeration type. */
6318
72bf9492
DJ
6319static void
6320add_partial_enumeration (struct partial_die_info *enum_pdi,
6321 struct dwarf2_cu *cu)
91c24f0a 6322{
72bf9492 6323 struct partial_die_info *pdi;
91c24f0a
DC
6324
6325 if (enum_pdi->name != NULL)
72bf9492
DJ
6326 add_partial_symbol (enum_pdi, cu);
6327
6328 pdi = enum_pdi->die_child;
6329 while (pdi)
91c24f0a 6330 {
72bf9492 6331 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6332 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6333 else
72bf9492
DJ
6334 add_partial_symbol (pdi, cu);
6335 pdi = pdi->die_sibling;
91c24f0a 6336 }
91c24f0a
DC
6337}
6338
6caca83c
CC
6339/* Return the initial uleb128 in the die at INFO_PTR. */
6340
6341static unsigned int
d521ce57 6342peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6343{
6344 unsigned int bytes_read;
6345
6346 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6347}
6348
4bb7a0a7
DJ
6349/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6350 Return the corresponding abbrev, or NULL if the number is zero (indicating
6351 an empty DIE). In either case *BYTES_READ will be set to the length of
6352 the initial number. */
6353
6354static struct abbrev_info *
d521ce57 6355peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6356 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6357{
6358 bfd *abfd = cu->objfile->obfd;
6359 unsigned int abbrev_number;
6360 struct abbrev_info *abbrev;
6361
6362 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6363
6364 if (abbrev_number == 0)
6365 return NULL;
6366
433df2d4 6367 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6368 if (!abbrev)
6369 {
3e43a32a
MS
6370 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6371 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6372 }
6373
6374 return abbrev;
6375}
6376
93311388
DE
6377/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6378 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6379 DIE. Any children of the skipped DIEs will also be skipped. */
6380
d521ce57
TT
6381static const gdb_byte *
6382skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6383{
dee91e82 6384 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6385 struct abbrev_info *abbrev;
6386 unsigned int bytes_read;
6387
6388 while (1)
6389 {
6390 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6391 if (abbrev == NULL)
6392 return info_ptr + bytes_read;
6393 else
dee91e82 6394 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6395 }
6396}
6397
93311388
DE
6398/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6399 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6400 abbrev corresponding to that skipped uleb128 should be passed in
6401 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6402 children. */
6403
d521ce57
TT
6404static const gdb_byte *
6405skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6406 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6407{
6408 unsigned int bytes_read;
6409 struct attribute attr;
dee91e82
DE
6410 bfd *abfd = reader->abfd;
6411 struct dwarf2_cu *cu = reader->cu;
d521ce57 6412 const gdb_byte *buffer = reader->buffer;
f664829e 6413 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6414 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6415 unsigned int form, i;
6416
6417 for (i = 0; i < abbrev->num_attrs; i++)
6418 {
6419 /* The only abbrev we care about is DW_AT_sibling. */
6420 if (abbrev->attrs[i].name == DW_AT_sibling)
6421 {
dee91e82 6422 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6423 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6424 complaint (&symfile_complaints,
6425 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6426 else
b64f50a1 6427 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6428 }
6429
6430 /* If it isn't DW_AT_sibling, skip this attribute. */
6431 form = abbrev->attrs[i].form;
6432 skip_attribute:
6433 switch (form)
6434 {
4bb7a0a7 6435 case DW_FORM_ref_addr:
ae411497
TT
6436 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6437 and later it is offset sized. */
6438 if (cu->header.version == 2)
6439 info_ptr += cu->header.addr_size;
6440 else
6441 info_ptr += cu->header.offset_size;
6442 break;
36586728
TT
6443 case DW_FORM_GNU_ref_alt:
6444 info_ptr += cu->header.offset_size;
6445 break;
ae411497 6446 case DW_FORM_addr:
4bb7a0a7
DJ
6447 info_ptr += cu->header.addr_size;
6448 break;
6449 case DW_FORM_data1:
6450 case DW_FORM_ref1:
6451 case DW_FORM_flag:
6452 info_ptr += 1;
6453 break;
2dc7f7b3
TT
6454 case DW_FORM_flag_present:
6455 break;
4bb7a0a7
DJ
6456 case DW_FORM_data2:
6457 case DW_FORM_ref2:
6458 info_ptr += 2;
6459 break;
6460 case DW_FORM_data4:
6461 case DW_FORM_ref4:
6462 info_ptr += 4;
6463 break;
6464 case DW_FORM_data8:
6465 case DW_FORM_ref8:
55f1336d 6466 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6467 info_ptr += 8;
6468 break;
6469 case DW_FORM_string:
9b1c24c8 6470 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6471 info_ptr += bytes_read;
6472 break;
2dc7f7b3 6473 case DW_FORM_sec_offset:
4bb7a0a7 6474 case DW_FORM_strp:
36586728 6475 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6476 info_ptr += cu->header.offset_size;
6477 break;
2dc7f7b3 6478 case DW_FORM_exprloc:
4bb7a0a7
DJ
6479 case DW_FORM_block:
6480 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6481 info_ptr += bytes_read;
6482 break;
6483 case DW_FORM_block1:
6484 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6485 break;
6486 case DW_FORM_block2:
6487 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6488 break;
6489 case DW_FORM_block4:
6490 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6491 break;
6492 case DW_FORM_sdata:
6493 case DW_FORM_udata:
6494 case DW_FORM_ref_udata:
3019eac3
DE
6495 case DW_FORM_GNU_addr_index:
6496 case DW_FORM_GNU_str_index:
d521ce57 6497 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6498 break;
6499 case DW_FORM_indirect:
6500 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6501 info_ptr += bytes_read;
6502 /* We need to continue parsing from here, so just go back to
6503 the top. */
6504 goto skip_attribute;
6505
6506 default:
3e43a32a
MS
6507 error (_("Dwarf Error: Cannot handle %s "
6508 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6509 dwarf_form_name (form),
6510 bfd_get_filename (abfd));
6511 }
6512 }
6513
6514 if (abbrev->has_children)
dee91e82 6515 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6516 else
6517 return info_ptr;
6518}
6519
93311388 6520/* Locate ORIG_PDI's sibling.
dee91e82 6521 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6522
d521ce57 6523static const gdb_byte *
dee91e82
DE
6524locate_pdi_sibling (const struct die_reader_specs *reader,
6525 struct partial_die_info *orig_pdi,
d521ce57 6526 const gdb_byte *info_ptr)
91c24f0a
DC
6527{
6528 /* Do we know the sibling already? */
72bf9492 6529
91c24f0a
DC
6530 if (orig_pdi->sibling)
6531 return orig_pdi->sibling;
6532
6533 /* Are there any children to deal with? */
6534
6535 if (!orig_pdi->has_children)
6536 return info_ptr;
6537
4bb7a0a7 6538 /* Skip the children the long way. */
91c24f0a 6539
dee91e82 6540 return skip_children (reader, info_ptr);
91c24f0a
DC
6541}
6542
257e7a09 6543/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6544 not NULL. */
c906108c
SS
6545
6546static void
257e7a09
YQ
6547dwarf2_read_symtab (struct partial_symtab *self,
6548 struct objfile *objfile)
c906108c 6549{
257e7a09 6550 if (self->readin)
c906108c 6551 {
442e4d9c 6552 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6553 self->filename);
442e4d9c
YQ
6554 }
6555 else
6556 {
6557 if (info_verbose)
c906108c 6558 {
442e4d9c 6559 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6560 self->filename);
442e4d9c 6561 gdb_flush (gdb_stdout);
c906108c 6562 }
c906108c 6563
442e4d9c
YQ
6564 /* Restore our global data. */
6565 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6566
442e4d9c
YQ
6567 /* If this psymtab is constructed from a debug-only objfile, the
6568 has_section_at_zero flag will not necessarily be correct. We
6569 can get the correct value for this flag by looking at the data
6570 associated with the (presumably stripped) associated objfile. */
6571 if (objfile->separate_debug_objfile_backlink)
6572 {
6573 struct dwarf2_per_objfile *dpo_backlink
6574 = objfile_data (objfile->separate_debug_objfile_backlink,
6575 dwarf2_objfile_data_key);
9a619af0 6576
442e4d9c
YQ
6577 dwarf2_per_objfile->has_section_at_zero
6578 = dpo_backlink->has_section_at_zero;
6579 }
b2ab525c 6580
442e4d9c 6581 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6582
257e7a09 6583 psymtab_to_symtab_1 (self);
c906108c 6584
442e4d9c
YQ
6585 /* Finish up the debug error message. */
6586 if (info_verbose)
6587 printf_filtered (_("done.\n"));
c906108c 6588 }
95554aad
TT
6589
6590 process_cu_includes ();
c906108c 6591}
9cdd5dbd
DE
6592\f
6593/* Reading in full CUs. */
c906108c 6594
10b3939b
DJ
6595/* Add PER_CU to the queue. */
6596
6597static void
95554aad
TT
6598queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6599 enum language pretend_language)
10b3939b
DJ
6600{
6601 struct dwarf2_queue_item *item;
6602
6603 per_cu->queued = 1;
6604 item = xmalloc (sizeof (*item));
6605 item->per_cu = per_cu;
95554aad 6606 item->pretend_language = pretend_language;
10b3939b
DJ
6607 item->next = NULL;
6608
6609 if (dwarf2_queue == NULL)
6610 dwarf2_queue = item;
6611 else
6612 dwarf2_queue_tail->next = item;
6613
6614 dwarf2_queue_tail = item;
6615}
6616
0907af0c
DE
6617/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6618 unit and add it to our queue.
6619 The result is non-zero if PER_CU was queued, otherwise the result is zero
6620 meaning either PER_CU is already queued or it is already loaded. */
6621
6622static int
6623maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6624 struct dwarf2_per_cu_data *per_cu,
6625 enum language pretend_language)
6626{
6627 /* We may arrive here during partial symbol reading, if we need full
6628 DIEs to process an unusual case (e.g. template arguments). Do
6629 not queue PER_CU, just tell our caller to load its DIEs. */
6630 if (dwarf2_per_objfile->reading_partial_symbols)
6631 {
6632 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6633 return 1;
6634 return 0;
6635 }
6636
6637 /* Mark the dependence relation so that we don't flush PER_CU
6638 too early. */
6639 dwarf2_add_dependence (this_cu, per_cu);
6640
6641 /* If it's already on the queue, we have nothing to do. */
6642 if (per_cu->queued)
6643 return 0;
6644
6645 /* If the compilation unit is already loaded, just mark it as
6646 used. */
6647 if (per_cu->cu != NULL)
6648 {
6649 per_cu->cu->last_used = 0;
6650 return 0;
6651 }
6652
6653 /* Add it to the queue. */
6654 queue_comp_unit (per_cu, pretend_language);
6655
6656 return 1;
6657}
6658
10b3939b
DJ
6659/* Process the queue. */
6660
6661static void
a0f42c21 6662process_queue (void)
10b3939b
DJ
6663{
6664 struct dwarf2_queue_item *item, *next_item;
6665
45cfd468
DE
6666 if (dwarf2_read_debug)
6667 {
6668 fprintf_unfiltered (gdb_stdlog,
6669 "Expanding one or more symtabs of objfile %s ...\n",
6670 dwarf2_per_objfile->objfile->name);
6671 }
6672
03dd20cc
DJ
6673 /* The queue starts out with one item, but following a DIE reference
6674 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6675 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6676 {
9291a0cd
TT
6677 if (dwarf2_per_objfile->using_index
6678 ? !item->per_cu->v.quick->symtab
6679 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6680 {
6681 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6682
6683 if (dwarf2_read_debug)
6684 {
6685 fprintf_unfiltered (gdb_stdlog,
6686 "Expanding symtab of %s at offset 0x%x\n",
6687 per_cu->is_debug_types ? "TU" : "CU",
6688 per_cu->offset.sect_off);
6689 }
6690
6691 if (per_cu->is_debug_types)
6692 process_full_type_unit (per_cu, item->pretend_language);
6693 else
6694 process_full_comp_unit (per_cu, item->pretend_language);
6695
6696 if (dwarf2_read_debug)
6697 {
6698 fprintf_unfiltered (gdb_stdlog,
6699 "Done expanding %s at offset 0x%x\n",
6700 per_cu->is_debug_types ? "TU" : "CU",
6701 per_cu->offset.sect_off);
6702 }
6703 }
10b3939b
DJ
6704
6705 item->per_cu->queued = 0;
6706 next_item = item->next;
6707 xfree (item);
6708 }
6709
6710 dwarf2_queue_tail = NULL;
45cfd468
DE
6711
6712 if (dwarf2_read_debug)
6713 {
6714 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6715 dwarf2_per_objfile->objfile->name);
6716 }
10b3939b
DJ
6717}
6718
6719/* Free all allocated queue entries. This function only releases anything if
6720 an error was thrown; if the queue was processed then it would have been
6721 freed as we went along. */
6722
6723static void
6724dwarf2_release_queue (void *dummy)
6725{
6726 struct dwarf2_queue_item *item, *last;
6727
6728 item = dwarf2_queue;
6729 while (item)
6730 {
6731 /* Anything still marked queued is likely to be in an
6732 inconsistent state, so discard it. */
6733 if (item->per_cu->queued)
6734 {
6735 if (item->per_cu->cu != NULL)
dee91e82 6736 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6737 item->per_cu->queued = 0;
6738 }
6739
6740 last = item;
6741 item = item->next;
6742 xfree (last);
6743 }
6744
6745 dwarf2_queue = dwarf2_queue_tail = NULL;
6746}
6747
6748/* Read in full symbols for PST, and anything it depends on. */
6749
c906108c 6750static void
fba45db2 6751psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6752{
10b3939b 6753 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6754 int i;
6755
95554aad
TT
6756 if (pst->readin)
6757 return;
6758
aaa75496 6759 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6760 if (!pst->dependencies[i]->readin
6761 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6762 {
6763 /* Inform about additional files that need to be read in. */
6764 if (info_verbose)
6765 {
a3f17187 6766 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6767 fputs_filtered (" ", gdb_stdout);
6768 wrap_here ("");
6769 fputs_filtered ("and ", gdb_stdout);
6770 wrap_here ("");
6771 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6772 wrap_here (""); /* Flush output. */
aaa75496
JB
6773 gdb_flush (gdb_stdout);
6774 }
6775 psymtab_to_symtab_1 (pst->dependencies[i]);
6776 }
6777
e38df1d0 6778 per_cu = pst->read_symtab_private;
10b3939b
DJ
6779
6780 if (per_cu == NULL)
aaa75496
JB
6781 {
6782 /* It's an include file, no symbols to read for it.
6783 Everything is in the parent symtab. */
6784 pst->readin = 1;
6785 return;
6786 }
c906108c 6787
a0f42c21 6788 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6789}
6790
dee91e82
DE
6791/* Trivial hash function for die_info: the hash value of a DIE
6792 is its offset in .debug_info for this objfile. */
10b3939b 6793
dee91e82
DE
6794static hashval_t
6795die_hash (const void *item)
10b3939b 6796{
dee91e82 6797 const struct die_info *die = item;
6502dd73 6798
dee91e82
DE
6799 return die->offset.sect_off;
6800}
63d06c5c 6801
dee91e82
DE
6802/* Trivial comparison function for die_info structures: two DIEs
6803 are equal if they have the same offset. */
98bfdba5 6804
dee91e82
DE
6805static int
6806die_eq (const void *item_lhs, const void *item_rhs)
6807{
6808 const struct die_info *die_lhs = item_lhs;
6809 const struct die_info *die_rhs = item_rhs;
c906108c 6810
dee91e82
DE
6811 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6812}
c906108c 6813
dee91e82
DE
6814/* die_reader_func for load_full_comp_unit.
6815 This is identical to read_signatured_type_reader,
6816 but is kept separate for now. */
c906108c 6817
dee91e82
DE
6818static void
6819load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6820 const gdb_byte *info_ptr,
dee91e82
DE
6821 struct die_info *comp_unit_die,
6822 int has_children,
6823 void *data)
6824{
6825 struct dwarf2_cu *cu = reader->cu;
95554aad 6826 enum language *language_ptr = data;
6caca83c 6827
dee91e82
DE
6828 gdb_assert (cu->die_hash == NULL);
6829 cu->die_hash =
6830 htab_create_alloc_ex (cu->header.length / 12,
6831 die_hash,
6832 die_eq,
6833 NULL,
6834 &cu->comp_unit_obstack,
6835 hashtab_obstack_allocate,
6836 dummy_obstack_deallocate);
e142c38c 6837
dee91e82
DE
6838 if (has_children)
6839 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6840 &info_ptr, comp_unit_die);
6841 cu->dies = comp_unit_die;
6842 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6843
6844 /* We try not to read any attributes in this function, because not
9cdd5dbd 6845 all CUs needed for references have been loaded yet, and symbol
10b3939b 6846 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6847 or we won't be able to build types correctly.
6848 Similarly, if we do not read the producer, we can not apply
6849 producer-specific interpretation. */
95554aad 6850 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6851}
10b3939b 6852
dee91e82 6853/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6854
dee91e82 6855static void
95554aad
TT
6856load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6857 enum language pretend_language)
dee91e82 6858{
3019eac3 6859 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6860
f4dc4d17
DE
6861 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6862 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6863}
6864
3da10d80
KS
6865/* Add a DIE to the delayed physname list. */
6866
6867static void
6868add_to_method_list (struct type *type, int fnfield_index, int index,
6869 const char *name, struct die_info *die,
6870 struct dwarf2_cu *cu)
6871{
6872 struct delayed_method_info mi;
6873 mi.type = type;
6874 mi.fnfield_index = fnfield_index;
6875 mi.index = index;
6876 mi.name = name;
6877 mi.die = die;
6878 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6879}
6880
6881/* A cleanup for freeing the delayed method list. */
6882
6883static void
6884free_delayed_list (void *ptr)
6885{
6886 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6887 if (cu->method_list != NULL)
6888 {
6889 VEC_free (delayed_method_info, cu->method_list);
6890 cu->method_list = NULL;
6891 }
6892}
6893
6894/* Compute the physnames of any methods on the CU's method list.
6895
6896 The computation of method physnames is delayed in order to avoid the
6897 (bad) condition that one of the method's formal parameters is of an as yet
6898 incomplete type. */
6899
6900static void
6901compute_delayed_physnames (struct dwarf2_cu *cu)
6902{
6903 int i;
6904 struct delayed_method_info *mi;
6905 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6906 {
1d06ead6 6907 const char *physname;
3da10d80
KS
6908 struct fn_fieldlist *fn_flp
6909 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6910 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6911 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6912 }
6913}
6914
a766d390
DE
6915/* Go objects should be embedded in a DW_TAG_module DIE,
6916 and it's not clear if/how imported objects will appear.
6917 To keep Go support simple until that's worked out,
6918 go back through what we've read and create something usable.
6919 We could do this while processing each DIE, and feels kinda cleaner,
6920 but that way is more invasive.
6921 This is to, for example, allow the user to type "p var" or "b main"
6922 without having to specify the package name, and allow lookups
6923 of module.object to work in contexts that use the expression
6924 parser. */
6925
6926static void
6927fixup_go_packaging (struct dwarf2_cu *cu)
6928{
6929 char *package_name = NULL;
6930 struct pending *list;
6931 int i;
6932
6933 for (list = global_symbols; list != NULL; list = list->next)
6934 {
6935 for (i = 0; i < list->nsyms; ++i)
6936 {
6937 struct symbol *sym = list->symbol[i];
6938
6939 if (SYMBOL_LANGUAGE (sym) == language_go
6940 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6941 {
6942 char *this_package_name = go_symbol_package_name (sym);
6943
6944 if (this_package_name == NULL)
6945 continue;
6946 if (package_name == NULL)
6947 package_name = this_package_name;
6948 else
6949 {
6950 if (strcmp (package_name, this_package_name) != 0)
6951 complaint (&symfile_complaints,
6952 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6953 (SYMBOL_SYMTAB (sym)
05cba821 6954 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6955 : cu->objfile->name),
6956 this_package_name, package_name);
6957 xfree (this_package_name);
6958 }
6959 }
6960 }
6961 }
6962
6963 if (package_name != NULL)
6964 {
6965 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6966 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6967 package_name,
6968 strlen (package_name));
a766d390 6969 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6970 saved_package_name, objfile);
a766d390
DE
6971 struct symbol *sym;
6972
6973 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6974
e623cf5d 6975 sym = allocate_symbol (objfile);
f85f34ed 6976 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
6977 SYMBOL_SET_NAMES (sym, saved_package_name,
6978 strlen (saved_package_name), 0, objfile);
a766d390
DE
6979 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6980 e.g., "main" finds the "main" module and not C's main(). */
6981 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6982 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6983 SYMBOL_TYPE (sym) = type;
6984
6985 add_symbol_to_list (sym, &global_symbols);
6986
6987 xfree (package_name);
6988 }
6989}
6990
95554aad
TT
6991/* Return the symtab for PER_CU. This works properly regardless of
6992 whether we're using the index or psymtabs. */
6993
6994static struct symtab *
6995get_symtab (struct dwarf2_per_cu_data *per_cu)
6996{
6997 return (dwarf2_per_objfile->using_index
6998 ? per_cu->v.quick->symtab
6999 : per_cu->v.psymtab->symtab);
7000}
7001
7002/* A helper function for computing the list of all symbol tables
7003 included by PER_CU. */
7004
7005static void
7006recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
7007 htab_t all_children,
7008 struct dwarf2_per_cu_data *per_cu)
7009{
7010 void **slot;
7011 int ix;
7012 struct dwarf2_per_cu_data *iter;
7013
7014 slot = htab_find_slot (all_children, per_cu, INSERT);
7015 if (*slot != NULL)
7016 {
7017 /* This inclusion and its children have been processed. */
7018 return;
7019 }
7020
7021 *slot = per_cu;
7022 /* Only add a CU if it has a symbol table. */
7023 if (get_symtab (per_cu) != NULL)
7024 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7025
7026 for (ix = 0;
796a7ff8 7027 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7028 ++ix)
7029 recursively_compute_inclusions (result, all_children, iter);
7030}
7031
7032/* Compute the symtab 'includes' fields for the symtab related to
7033 PER_CU. */
7034
7035static void
7036compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7037{
f4dc4d17
DE
7038 gdb_assert (! per_cu->is_debug_types);
7039
796a7ff8 7040 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7041 {
7042 int ix, len;
7043 struct dwarf2_per_cu_data *iter;
7044 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7045 htab_t all_children;
7046 struct symtab *symtab = get_symtab (per_cu);
7047
7048 /* If we don't have a symtab, we can just skip this case. */
7049 if (symtab == NULL)
7050 return;
7051
7052 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7053 NULL, xcalloc, xfree);
7054
7055 for (ix = 0;
796a7ff8 7056 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7057 ix, iter);
7058 ++ix)
7059 recursively_compute_inclusions (&result_children, all_children, iter);
7060
796a7ff8
DE
7061 /* Now we have a transitive closure of all the included CUs, and
7062 for .gdb_index version 7 the included TUs, so we can convert it
7063 to a list of symtabs. */
95554aad
TT
7064 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7065 symtab->includes
7066 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7067 (len + 1) * sizeof (struct symtab *));
7068 for (ix = 0;
7069 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7070 ++ix)
7071 symtab->includes[ix] = get_symtab (iter);
7072 symtab->includes[len] = NULL;
7073
7074 VEC_free (dwarf2_per_cu_ptr, result_children);
7075 htab_delete (all_children);
7076 }
7077}
7078
7079/* Compute the 'includes' field for the symtabs of all the CUs we just
7080 read. */
7081
7082static void
7083process_cu_includes (void)
7084{
7085 int ix;
7086 struct dwarf2_per_cu_data *iter;
7087
7088 for (ix = 0;
7089 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7090 ix, iter);
7091 ++ix)
f4dc4d17
DE
7092 {
7093 if (! iter->is_debug_types)
7094 compute_symtab_includes (iter);
7095 }
95554aad
TT
7096
7097 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7098}
7099
9cdd5dbd 7100/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7101 already been loaded into memory. */
7102
7103static void
95554aad
TT
7104process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7105 enum language pretend_language)
10b3939b 7106{
10b3939b 7107 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7108 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7109 CORE_ADDR lowpc, highpc;
7110 struct symtab *symtab;
3da10d80 7111 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7112 CORE_ADDR baseaddr;
4359dff1 7113 struct block *static_block;
10b3939b
DJ
7114
7115 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7116
10b3939b
DJ
7117 buildsym_init ();
7118 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7119 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7120
7121 cu->list_in_scope = &file_symbols;
c906108c 7122
95554aad
TT
7123 cu->language = pretend_language;
7124 cu->language_defn = language_def (cu->language);
7125
c906108c 7126 /* Do line number decoding in read_file_scope () */
10b3939b 7127 process_die (cu->dies, cu);
c906108c 7128
a766d390
DE
7129 /* For now fudge the Go package. */
7130 if (cu->language == language_go)
7131 fixup_go_packaging (cu);
7132
3da10d80
KS
7133 /* Now that we have processed all the DIEs in the CU, all the types
7134 should be complete, and it should now be safe to compute all of the
7135 physnames. */
7136 compute_delayed_physnames (cu);
7137 do_cleanups (delayed_list_cleanup);
7138
fae299cd
DC
7139 /* Some compilers don't define a DW_AT_high_pc attribute for the
7140 compilation unit. If the DW_AT_high_pc is missing, synthesize
7141 it, by scanning the DIE's below the compilation unit. */
10b3939b 7142 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7143
36586728 7144 static_block
ff546935 7145 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7146
7147 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7148 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7149 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7150 addrmap to help ensure it has an accurate map of pc values belonging to
7151 this comp unit. */
7152 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7153
7154 symtab = end_symtab_from_static_block (static_block, objfile,
7155 SECT_OFF_TEXT (objfile), 0);
c906108c 7156
8be455d7 7157 if (symtab != NULL)
c906108c 7158 {
df15bd07 7159 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7160
8be455d7
JK
7161 /* Set symtab language to language from DW_AT_language. If the
7162 compilation is from a C file generated by language preprocessors, do
7163 not set the language if it was already deduced by start_subfile. */
7164 if (!(cu->language == language_c && symtab->language != language_c))
7165 symtab->language = cu->language;
7166
7167 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7168 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7169 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7170 there were bugs in prologue debug info, fixed later in GCC-4.5
7171 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7172
7173 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7174 needed, it would be wrong due to missing DW_AT_producer there.
7175
7176 Still one can confuse GDB by using non-standard GCC compilation
7177 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7178 */
ab260dad 7179 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7180 symtab->locations_valid = 1;
e0d00bc7
JK
7181
7182 if (gcc_4_minor >= 5)
7183 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7184
7185 symtab->call_site_htab = cu->call_site_htab;
c906108c 7186 }
9291a0cd
TT
7187
7188 if (dwarf2_per_objfile->using_index)
7189 per_cu->v.quick->symtab = symtab;
7190 else
7191 {
7192 struct partial_symtab *pst = per_cu->v.psymtab;
7193 pst->symtab = symtab;
7194 pst->readin = 1;
7195 }
c906108c 7196
95554aad
TT
7197 /* Push it for inclusion processing later. */
7198 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7199
c906108c 7200 do_cleanups (back_to);
f4dc4d17 7201}
45cfd468 7202
f4dc4d17
DE
7203/* Generate full symbol information for type unit PER_CU, whose DIEs have
7204 already been loaded into memory. */
7205
7206static void
7207process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7208 enum language pretend_language)
7209{
7210 struct dwarf2_cu *cu = per_cu->cu;
7211 struct objfile *objfile = per_cu->objfile;
7212 struct symtab *symtab;
7213 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7214 struct signatured_type *sig_type;
7215
7216 gdb_assert (per_cu->is_debug_types);
7217 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7218
7219 buildsym_init ();
7220 back_to = make_cleanup (really_free_pendings, NULL);
7221 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7222
7223 cu->list_in_scope = &file_symbols;
7224
7225 cu->language = pretend_language;
7226 cu->language_defn = language_def (cu->language);
7227
7228 /* The symbol tables are set up in read_type_unit_scope. */
7229 process_die (cu->dies, cu);
7230
7231 /* For now fudge the Go package. */
7232 if (cu->language == language_go)
7233 fixup_go_packaging (cu);
7234
7235 /* Now that we have processed all the DIEs in the CU, all the types
7236 should be complete, and it should now be safe to compute all of the
7237 physnames. */
7238 compute_delayed_physnames (cu);
7239 do_cleanups (delayed_list_cleanup);
7240
7241 /* TUs share symbol tables.
7242 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7243 of it with end_expandable_symtab. Otherwise, complete the addition of
7244 this TU's symbols to the existing symtab. */
0186c6a7 7245 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7246 {
f4dc4d17 7247 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7248 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7249
7250 if (symtab != NULL)
7251 {
7252 /* Set symtab language to language from DW_AT_language. If the
7253 compilation is from a C file generated by language preprocessors,
7254 do not set the language if it was already deduced by
7255 start_subfile. */
7256 if (!(cu->language == language_c && symtab->language != language_c))
7257 symtab->language = cu->language;
7258 }
7259 }
7260 else
7261 {
7262 augment_type_symtab (objfile,
0186c6a7
DE
7263 sig_type->type_unit_group->primary_symtab);
7264 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7265 }
7266
7267 if (dwarf2_per_objfile->using_index)
7268 per_cu->v.quick->symtab = symtab;
7269 else
7270 {
7271 struct partial_symtab *pst = per_cu->v.psymtab;
7272 pst->symtab = symtab;
7273 pst->readin = 1;
45cfd468 7274 }
f4dc4d17
DE
7275
7276 do_cleanups (back_to);
c906108c
SS
7277}
7278
95554aad
TT
7279/* Process an imported unit DIE. */
7280
7281static void
7282process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7283{
7284 struct attribute *attr;
7285
f4dc4d17
DE
7286 /* For now we don't handle imported units in type units. */
7287 if (cu->per_cu->is_debug_types)
7288 {
7289 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7290 " supported in type units [in module %s]"),
7291 cu->objfile->name);
7292 }
7293
95554aad
TT
7294 attr = dwarf2_attr (die, DW_AT_import, cu);
7295 if (attr != NULL)
7296 {
7297 struct dwarf2_per_cu_data *per_cu;
7298 struct symtab *imported_symtab;
7299 sect_offset offset;
36586728 7300 int is_dwz;
95554aad
TT
7301
7302 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7303 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7304 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7305
7306 /* Queue the unit, if needed. */
7307 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7308 load_full_comp_unit (per_cu, cu->language);
7309
796a7ff8 7310 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7311 per_cu);
7312 }
7313}
7314
c906108c
SS
7315/* Process a die and its children. */
7316
7317static void
e7c27a73 7318process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7319{
7320 switch (die->tag)
7321 {
7322 case DW_TAG_padding:
7323 break;
7324 case DW_TAG_compile_unit:
95554aad 7325 case DW_TAG_partial_unit:
e7c27a73 7326 read_file_scope (die, cu);
c906108c 7327 break;
348e048f
DE
7328 case DW_TAG_type_unit:
7329 read_type_unit_scope (die, cu);
7330 break;
c906108c 7331 case DW_TAG_subprogram:
c906108c 7332 case DW_TAG_inlined_subroutine:
edb3359d 7333 read_func_scope (die, cu);
c906108c
SS
7334 break;
7335 case DW_TAG_lexical_block:
14898363
L
7336 case DW_TAG_try_block:
7337 case DW_TAG_catch_block:
e7c27a73 7338 read_lexical_block_scope (die, cu);
c906108c 7339 break;
96408a79
SA
7340 case DW_TAG_GNU_call_site:
7341 read_call_site_scope (die, cu);
7342 break;
c906108c 7343 case DW_TAG_class_type:
680b30c7 7344 case DW_TAG_interface_type:
c906108c
SS
7345 case DW_TAG_structure_type:
7346 case DW_TAG_union_type:
134d01f1 7347 process_structure_scope (die, cu);
c906108c
SS
7348 break;
7349 case DW_TAG_enumeration_type:
134d01f1 7350 process_enumeration_scope (die, cu);
c906108c 7351 break;
134d01f1 7352
f792889a
DJ
7353 /* These dies have a type, but processing them does not create
7354 a symbol or recurse to process the children. Therefore we can
7355 read them on-demand through read_type_die. */
c906108c 7356 case DW_TAG_subroutine_type:
72019c9c 7357 case DW_TAG_set_type:
c906108c 7358 case DW_TAG_array_type:
c906108c 7359 case DW_TAG_pointer_type:
c906108c 7360 case DW_TAG_ptr_to_member_type:
c906108c 7361 case DW_TAG_reference_type:
c906108c 7362 case DW_TAG_string_type:
c906108c 7363 break;
134d01f1 7364
c906108c 7365 case DW_TAG_base_type:
a02abb62 7366 case DW_TAG_subrange_type:
cb249c71 7367 case DW_TAG_typedef:
134d01f1
DJ
7368 /* Add a typedef symbol for the type definition, if it has a
7369 DW_AT_name. */
f792889a 7370 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7371 break;
c906108c 7372 case DW_TAG_common_block:
e7c27a73 7373 read_common_block (die, cu);
c906108c
SS
7374 break;
7375 case DW_TAG_common_inclusion:
7376 break;
d9fa45fe 7377 case DW_TAG_namespace:
4d4ec4e5 7378 cu->processing_has_namespace_info = 1;
e7c27a73 7379 read_namespace (die, cu);
d9fa45fe 7380 break;
5d7cb8df 7381 case DW_TAG_module:
4d4ec4e5 7382 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7383 read_module (die, cu);
7384 break;
d9fa45fe
DC
7385 case DW_TAG_imported_declaration:
7386 case DW_TAG_imported_module:
4d4ec4e5 7387 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7388 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7389 || cu->language != language_fortran))
7390 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7391 dwarf_tag_name (die->tag));
7392 read_import_statement (die, cu);
d9fa45fe 7393 break;
95554aad
TT
7394
7395 case DW_TAG_imported_unit:
7396 process_imported_unit_die (die, cu);
7397 break;
7398
c906108c 7399 default:
e7c27a73 7400 new_symbol (die, NULL, cu);
c906108c
SS
7401 break;
7402 }
7403}
ca69b9e6
DE
7404\f
7405/* DWARF name computation. */
c906108c 7406
94af9270
KS
7407/* A helper function for dwarf2_compute_name which determines whether DIE
7408 needs to have the name of the scope prepended to the name listed in the
7409 die. */
7410
7411static int
7412die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7413{
1c809c68
TT
7414 struct attribute *attr;
7415
94af9270
KS
7416 switch (die->tag)
7417 {
7418 case DW_TAG_namespace:
7419 case DW_TAG_typedef:
7420 case DW_TAG_class_type:
7421 case DW_TAG_interface_type:
7422 case DW_TAG_structure_type:
7423 case DW_TAG_union_type:
7424 case DW_TAG_enumeration_type:
7425 case DW_TAG_enumerator:
7426 case DW_TAG_subprogram:
7427 case DW_TAG_member:
7428 return 1;
7429
7430 case DW_TAG_variable:
c2b0a229 7431 case DW_TAG_constant:
94af9270
KS
7432 /* We only need to prefix "globally" visible variables. These include
7433 any variable marked with DW_AT_external or any variable that
7434 lives in a namespace. [Variables in anonymous namespaces
7435 require prefixing, but they are not DW_AT_external.] */
7436
7437 if (dwarf2_attr (die, DW_AT_specification, cu))
7438 {
7439 struct dwarf2_cu *spec_cu = cu;
9a619af0 7440
94af9270
KS
7441 return die_needs_namespace (die_specification (die, &spec_cu),
7442 spec_cu);
7443 }
7444
1c809c68 7445 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7446 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7447 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7448 return 0;
7449 /* A variable in a lexical block of some kind does not need a
7450 namespace, even though in C++ such variables may be external
7451 and have a mangled name. */
7452 if (die->parent->tag == DW_TAG_lexical_block
7453 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7454 || die->parent->tag == DW_TAG_catch_block
7455 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7456 return 0;
7457 return 1;
94af9270
KS
7458
7459 default:
7460 return 0;
7461 }
7462}
7463
98bfdba5
PA
7464/* Retrieve the last character from a mem_file. */
7465
7466static void
7467do_ui_file_peek_last (void *object, const char *buffer, long length)
7468{
7469 char *last_char_p = (char *) object;
7470
7471 if (length > 0)
7472 *last_char_p = buffer[length - 1];
7473}
7474
94af9270 7475/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7476 compute the physname for the object, which include a method's:
7477 - formal parameters (C++/Java),
7478 - receiver type (Go),
7479 - return type (Java).
7480
7481 The term "physname" is a bit confusing.
7482 For C++, for example, it is the demangled name.
7483 For Go, for example, it's the mangled name.
94af9270 7484
af6b7be1
JB
7485 For Ada, return the DIE's linkage name rather than the fully qualified
7486 name. PHYSNAME is ignored..
7487
94af9270
KS
7488 The result is allocated on the objfile_obstack and canonicalized. */
7489
7490static const char *
15d034d0
TT
7491dwarf2_compute_name (const char *name,
7492 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7493 int physname)
7494{
bb5ed363
DE
7495 struct objfile *objfile = cu->objfile;
7496
94af9270
KS
7497 if (name == NULL)
7498 name = dwarf2_name (die, cu);
7499
f55ee35c
JK
7500 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7501 compute it by typename_concat inside GDB. */
7502 if (cu->language == language_ada
7503 || (cu->language == language_fortran && physname))
7504 {
7505 /* For Ada unit, we prefer the linkage name over the name, as
7506 the former contains the exported name, which the user expects
7507 to be able to reference. Ideally, we want the user to be able
7508 to reference this entity using either natural or linkage name,
7509 but we haven't started looking at this enhancement yet. */
7510 struct attribute *attr;
7511
7512 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7513 if (attr == NULL)
7514 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7515 if (attr && DW_STRING (attr))
7516 return DW_STRING (attr);
7517 }
7518
94af9270
KS
7519 /* These are the only languages we know how to qualify names in. */
7520 if (name != NULL
f55ee35c
JK
7521 && (cu->language == language_cplus || cu->language == language_java
7522 || cu->language == language_fortran))
94af9270
KS
7523 {
7524 if (die_needs_namespace (die, cu))
7525 {
7526 long length;
0d5cff50 7527 const char *prefix;
94af9270
KS
7528 struct ui_file *buf;
7529
7530 prefix = determine_prefix (die, cu);
7531 buf = mem_fileopen ();
7532 if (*prefix != '\0')
7533 {
f55ee35c
JK
7534 char *prefixed_name = typename_concat (NULL, prefix, name,
7535 physname, cu);
9a619af0 7536
94af9270
KS
7537 fputs_unfiltered (prefixed_name, buf);
7538 xfree (prefixed_name);
7539 }
7540 else
62d5b8da 7541 fputs_unfiltered (name, buf);
94af9270 7542
98bfdba5
PA
7543 /* Template parameters may be specified in the DIE's DW_AT_name, or
7544 as children with DW_TAG_template_type_param or
7545 DW_TAG_value_type_param. If the latter, add them to the name
7546 here. If the name already has template parameters, then
7547 skip this step; some versions of GCC emit both, and
7548 it is more efficient to use the pre-computed name.
7549
7550 Something to keep in mind about this process: it is very
7551 unlikely, or in some cases downright impossible, to produce
7552 something that will match the mangled name of a function.
7553 If the definition of the function has the same debug info,
7554 we should be able to match up with it anyway. But fallbacks
7555 using the minimal symbol, for instance to find a method
7556 implemented in a stripped copy of libstdc++, will not work.
7557 If we do not have debug info for the definition, we will have to
7558 match them up some other way.
7559
7560 When we do name matching there is a related problem with function
7561 templates; two instantiated function templates are allowed to
7562 differ only by their return types, which we do not add here. */
7563
7564 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7565 {
7566 struct attribute *attr;
7567 struct die_info *child;
7568 int first = 1;
7569
7570 die->building_fullname = 1;
7571
7572 for (child = die->child; child != NULL; child = child->sibling)
7573 {
7574 struct type *type;
12df843f 7575 LONGEST value;
d521ce57 7576 const gdb_byte *bytes;
98bfdba5
PA
7577 struct dwarf2_locexpr_baton *baton;
7578 struct value *v;
7579
7580 if (child->tag != DW_TAG_template_type_param
7581 && child->tag != DW_TAG_template_value_param)
7582 continue;
7583
7584 if (first)
7585 {
7586 fputs_unfiltered ("<", buf);
7587 first = 0;
7588 }
7589 else
7590 fputs_unfiltered (", ", buf);
7591
7592 attr = dwarf2_attr (child, DW_AT_type, cu);
7593 if (attr == NULL)
7594 {
7595 complaint (&symfile_complaints,
7596 _("template parameter missing DW_AT_type"));
7597 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7598 continue;
7599 }
7600 type = die_type (child, cu);
7601
7602 if (child->tag == DW_TAG_template_type_param)
7603 {
79d43c61 7604 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7605 continue;
7606 }
7607
7608 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7609 if (attr == NULL)
7610 {
7611 complaint (&symfile_complaints,
3e43a32a
MS
7612 _("template parameter missing "
7613 "DW_AT_const_value"));
98bfdba5
PA
7614 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7615 continue;
7616 }
7617
7618 dwarf2_const_value_attr (attr, type, name,
7619 &cu->comp_unit_obstack, cu,
7620 &value, &bytes, &baton);
7621
7622 if (TYPE_NOSIGN (type))
7623 /* GDB prints characters as NUMBER 'CHAR'. If that's
7624 changed, this can use value_print instead. */
7625 c_printchar (value, type, buf);
7626 else
7627 {
7628 struct value_print_options opts;
7629
7630 if (baton != NULL)
7631 v = dwarf2_evaluate_loc_desc (type, NULL,
7632 baton->data,
7633 baton->size,
7634 baton->per_cu);
7635 else if (bytes != NULL)
7636 {
7637 v = allocate_value (type);
7638 memcpy (value_contents_writeable (v), bytes,
7639 TYPE_LENGTH (type));
7640 }
7641 else
7642 v = value_from_longest (type, value);
7643
3e43a32a
MS
7644 /* Specify decimal so that we do not depend on
7645 the radix. */
98bfdba5
PA
7646 get_formatted_print_options (&opts, 'd');
7647 opts.raw = 1;
7648 value_print (v, buf, &opts);
7649 release_value (v);
7650 value_free (v);
7651 }
7652 }
7653
7654 die->building_fullname = 0;
7655
7656 if (!first)
7657 {
7658 /* Close the argument list, with a space if necessary
7659 (nested templates). */
7660 char last_char = '\0';
7661 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7662 if (last_char == '>')
7663 fputs_unfiltered (" >", buf);
7664 else
7665 fputs_unfiltered (">", buf);
7666 }
7667 }
7668
94af9270
KS
7669 /* For Java and C++ methods, append formal parameter type
7670 information, if PHYSNAME. */
6e70227d 7671
94af9270
KS
7672 if (physname && die->tag == DW_TAG_subprogram
7673 && (cu->language == language_cplus
7674 || cu->language == language_java))
7675 {
7676 struct type *type = read_type_die (die, cu);
7677
79d43c61
TT
7678 c_type_print_args (type, buf, 1, cu->language,
7679 &type_print_raw_options);
94af9270
KS
7680
7681 if (cu->language == language_java)
7682 {
7683 /* For java, we must append the return type to method
0963b4bd 7684 names. */
94af9270
KS
7685 if (die->tag == DW_TAG_subprogram)
7686 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7687 0, 0, &type_print_raw_options);
94af9270
KS
7688 }
7689 else if (cu->language == language_cplus)
7690 {
60430eff
DJ
7691 /* Assume that an artificial first parameter is
7692 "this", but do not crash if it is not. RealView
7693 marks unnamed (and thus unused) parameters as
7694 artificial; there is no way to differentiate
7695 the two cases. */
94af9270
KS
7696 if (TYPE_NFIELDS (type) > 0
7697 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7698 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7699 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7700 0))))
94af9270
KS
7701 fputs_unfiltered (" const", buf);
7702 }
7703 }
7704
bb5ed363 7705 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7706 &length);
7707 ui_file_delete (buf);
7708
7709 if (cu->language == language_cplus)
7710 {
15d034d0 7711 const char *cname
94af9270 7712 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7713 &objfile->objfile_obstack);
9a619af0 7714
94af9270
KS
7715 if (cname != NULL)
7716 name = cname;
7717 }
7718 }
7719 }
7720
7721 return name;
7722}
7723
0114d602
DJ
7724/* Return the fully qualified name of DIE, based on its DW_AT_name.
7725 If scope qualifiers are appropriate they will be added. The result
7726 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7727 not have a name. NAME may either be from a previous call to
7728 dwarf2_name or NULL.
7729
0963b4bd 7730 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7731
7732static const char *
15d034d0 7733dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7734{
94af9270
KS
7735 return dwarf2_compute_name (name, die, cu, 0);
7736}
0114d602 7737
94af9270
KS
7738/* Construct a physname for the given DIE in CU. NAME may either be
7739 from a previous call to dwarf2_name or NULL. The result will be
7740 allocated on the objfile_objstack or NULL if the DIE does not have a
7741 name.
0114d602 7742
94af9270 7743 The output string will be canonicalized (if C++/Java). */
0114d602 7744
94af9270 7745static const char *
15d034d0 7746dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7747{
bb5ed363 7748 struct objfile *objfile = cu->objfile;
900e11f9
JK
7749 struct attribute *attr;
7750 const char *retval, *mangled = NULL, *canon = NULL;
7751 struct cleanup *back_to;
7752 int need_copy = 1;
7753
7754 /* In this case dwarf2_compute_name is just a shortcut not building anything
7755 on its own. */
7756 if (!die_needs_namespace (die, cu))
7757 return dwarf2_compute_name (name, die, cu, 1);
7758
7759 back_to = make_cleanup (null_cleanup, NULL);
7760
7761 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7762 if (!attr)
7763 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7764
7765 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7766 has computed. */
7767 if (attr && DW_STRING (attr))
7768 {
7769 char *demangled;
7770
7771 mangled = DW_STRING (attr);
7772
7773 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7774 type. It is easier for GDB users to search for such functions as
7775 `name(params)' than `long name(params)'. In such case the minimal
7776 symbol names do not match the full symbol names but for template
7777 functions there is never a need to look up their definition from their
7778 declaration so the only disadvantage remains the minimal symbol
7779 variant `long name(params)' does not have the proper inferior type.
7780 */
7781
a766d390
DE
7782 if (cu->language == language_go)
7783 {
7784 /* This is a lie, but we already lie to the caller new_symbol_full.
7785 new_symbol_full assumes we return the mangled name.
7786 This just undoes that lie until things are cleaned up. */
7787 demangled = NULL;
7788 }
7789 else
7790 {
8de20a37
TT
7791 demangled = gdb_demangle (mangled,
7792 (DMGL_PARAMS | DMGL_ANSI
7793 | (cu->language == language_java
7794 ? DMGL_JAVA | DMGL_RET_POSTFIX
7795 : DMGL_RET_DROP)));
a766d390 7796 }
900e11f9
JK
7797 if (demangled)
7798 {
7799 make_cleanup (xfree, demangled);
7800 canon = demangled;
7801 }
7802 else
7803 {
7804 canon = mangled;
7805 need_copy = 0;
7806 }
7807 }
7808
7809 if (canon == NULL || check_physname)
7810 {
7811 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7812
7813 if (canon != NULL && strcmp (physname, canon) != 0)
7814 {
7815 /* It may not mean a bug in GDB. The compiler could also
7816 compute DW_AT_linkage_name incorrectly. But in such case
7817 GDB would need to be bug-to-bug compatible. */
7818
7819 complaint (&symfile_complaints,
7820 _("Computed physname <%s> does not match demangled <%s> "
7821 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7822 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7823
7824 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7825 is available here - over computed PHYSNAME. It is safer
7826 against both buggy GDB and buggy compilers. */
7827
7828 retval = canon;
7829 }
7830 else
7831 {
7832 retval = physname;
7833 need_copy = 0;
7834 }
7835 }
7836 else
7837 retval = canon;
7838
7839 if (need_copy)
10f0c4bb 7840 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7841
7842 do_cleanups (back_to);
7843 return retval;
0114d602
DJ
7844}
7845
27aa8d6a
SW
7846/* Read the import statement specified by the given die and record it. */
7847
7848static void
7849read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7850{
bb5ed363 7851 struct objfile *objfile = cu->objfile;
27aa8d6a 7852 struct attribute *import_attr;
32019081 7853 struct die_info *imported_die, *child_die;
de4affc9 7854 struct dwarf2_cu *imported_cu;
27aa8d6a 7855 const char *imported_name;
794684b6 7856 const char *imported_name_prefix;
13387711
SW
7857 const char *canonical_name;
7858 const char *import_alias;
7859 const char *imported_declaration = NULL;
794684b6 7860 const char *import_prefix;
32019081
JK
7861 VEC (const_char_ptr) *excludes = NULL;
7862 struct cleanup *cleanups;
13387711 7863
27aa8d6a
SW
7864 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7865 if (import_attr == NULL)
7866 {
7867 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7868 dwarf_tag_name (die->tag));
7869 return;
7870 }
7871
de4affc9
CC
7872 imported_cu = cu;
7873 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7874 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7875 if (imported_name == NULL)
7876 {
7877 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7878
7879 The import in the following code:
7880 namespace A
7881 {
7882 typedef int B;
7883 }
7884
7885 int main ()
7886 {
7887 using A::B;
7888 B b;
7889 return b;
7890 }
7891
7892 ...
7893 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7894 <52> DW_AT_decl_file : 1
7895 <53> DW_AT_decl_line : 6
7896 <54> DW_AT_import : <0x75>
7897 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7898 <59> DW_AT_name : B
7899 <5b> DW_AT_decl_file : 1
7900 <5c> DW_AT_decl_line : 2
7901 <5d> DW_AT_type : <0x6e>
7902 ...
7903 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7904 <76> DW_AT_byte_size : 4
7905 <77> DW_AT_encoding : 5 (signed)
7906
7907 imports the wrong die ( 0x75 instead of 0x58 ).
7908 This case will be ignored until the gcc bug is fixed. */
7909 return;
7910 }
7911
82856980
SW
7912 /* Figure out the local name after import. */
7913 import_alias = dwarf2_name (die, cu);
27aa8d6a 7914
794684b6
SW
7915 /* Figure out where the statement is being imported to. */
7916 import_prefix = determine_prefix (die, cu);
7917
7918 /* Figure out what the scope of the imported die is and prepend it
7919 to the name of the imported die. */
de4affc9 7920 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7921
f55ee35c
JK
7922 if (imported_die->tag != DW_TAG_namespace
7923 && imported_die->tag != DW_TAG_module)
794684b6 7924 {
13387711
SW
7925 imported_declaration = imported_name;
7926 canonical_name = imported_name_prefix;
794684b6 7927 }
13387711 7928 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7929 canonical_name = obconcat (&objfile->objfile_obstack,
7930 imported_name_prefix, "::", imported_name,
7931 (char *) NULL);
13387711
SW
7932 else
7933 canonical_name = imported_name;
794684b6 7934
32019081
JK
7935 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7936
7937 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7938 for (child_die = die->child; child_die && child_die->tag;
7939 child_die = sibling_die (child_die))
7940 {
7941 /* DWARF-4: A Fortran use statement with a “rename list” may be
7942 represented by an imported module entry with an import attribute
7943 referring to the module and owned entries corresponding to those
7944 entities that are renamed as part of being imported. */
7945
7946 if (child_die->tag != DW_TAG_imported_declaration)
7947 {
7948 complaint (&symfile_complaints,
7949 _("child DW_TAG_imported_declaration expected "
7950 "- DIE at 0x%x [in module %s]"),
b64f50a1 7951 child_die->offset.sect_off, objfile->name);
32019081
JK
7952 continue;
7953 }
7954
7955 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7956 if (import_attr == NULL)
7957 {
7958 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7959 dwarf_tag_name (child_die->tag));
7960 continue;
7961 }
7962
7963 imported_cu = cu;
7964 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7965 &imported_cu);
7966 imported_name = dwarf2_name (imported_die, imported_cu);
7967 if (imported_name == NULL)
7968 {
7969 complaint (&symfile_complaints,
7970 _("child DW_TAG_imported_declaration has unknown "
7971 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7972 child_die->offset.sect_off, objfile->name);
32019081
JK
7973 continue;
7974 }
7975
7976 VEC_safe_push (const_char_ptr, excludes, imported_name);
7977
7978 process_die (child_die, cu);
7979 }
7980
c0cc3a76
SW
7981 cp_add_using_directive (import_prefix,
7982 canonical_name,
7983 import_alias,
13387711 7984 imported_declaration,
32019081 7985 excludes,
12aaed36 7986 0,
bb5ed363 7987 &objfile->objfile_obstack);
32019081
JK
7988
7989 do_cleanups (cleanups);
27aa8d6a
SW
7990}
7991
f4dc4d17 7992/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7993
cb1df416
DJ
7994static void
7995free_cu_line_header (void *arg)
7996{
7997 struct dwarf2_cu *cu = arg;
7998
7999 free_line_header (cu->line_header);
8000 cu->line_header = NULL;
8001}
8002
1b80a9fa
JK
8003/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8004 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8005 this, it was first present in GCC release 4.3.0. */
8006
8007static int
8008producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8009{
8010 if (!cu->checked_producer)
8011 check_producer (cu);
8012
8013 return cu->producer_is_gcc_lt_4_3;
8014}
8015
9291a0cd
TT
8016static void
8017find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8018 const char **name, const char **comp_dir)
9291a0cd
TT
8019{
8020 struct attribute *attr;
8021
8022 *name = NULL;
8023 *comp_dir = NULL;
8024
8025 /* Find the filename. Do not use dwarf2_name here, since the filename
8026 is not a source language identifier. */
8027 attr = dwarf2_attr (die, DW_AT_name, cu);
8028 if (attr)
8029 {
8030 *name = DW_STRING (attr);
8031 }
8032
8033 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8034 if (attr)
8035 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8036 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8037 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8038 {
15d034d0
TT
8039 char *d = ldirname (*name);
8040
8041 *comp_dir = d;
8042 if (d != NULL)
8043 make_cleanup (xfree, d);
9291a0cd
TT
8044 }
8045 if (*comp_dir != NULL)
8046 {
8047 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8048 directory, get rid of it. */
8049 char *cp = strchr (*comp_dir, ':');
8050
8051 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8052 *comp_dir = cp + 1;
8053 }
8054
8055 if (*name == NULL)
8056 *name = "<unknown>";
8057}
8058
f4dc4d17
DE
8059/* Handle DW_AT_stmt_list for a compilation unit.
8060 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8061 COMP_DIR is the compilation directory.
8062 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8063
8064static void
8065handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8066 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8067{
8068 struct attribute *attr;
2ab95328 8069
f4dc4d17
DE
8070 gdb_assert (! cu->per_cu->is_debug_types);
8071
2ab95328
TT
8072 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8073 if (attr)
8074 {
8075 unsigned int line_offset = DW_UNSND (attr);
8076 struct line_header *line_header
3019eac3 8077 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8078
8079 if (line_header)
dee91e82
DE
8080 {
8081 cu->line_header = line_header;
8082 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8083 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8084 }
2ab95328
TT
8085 }
8086}
8087
95554aad 8088/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8089
c906108c 8090static void
e7c27a73 8091read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8092{
dee91e82 8093 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8094 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8095 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8096 CORE_ADDR highpc = ((CORE_ADDR) 0);
8097 struct attribute *attr;
15d034d0
TT
8098 const char *name = NULL;
8099 const char *comp_dir = NULL;
c906108c
SS
8100 struct die_info *child_die;
8101 bfd *abfd = objfile->obfd;
e142c38c 8102 CORE_ADDR baseaddr;
6e70227d 8103
e142c38c 8104 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8105
fae299cd 8106 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8107
8108 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8109 from finish_block. */
2acceee2 8110 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8111 lowpc = highpc;
8112 lowpc += baseaddr;
8113 highpc += baseaddr;
8114
9291a0cd 8115 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8116
95554aad 8117 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8118
f4b8a18d
KW
8119 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8120 standardised yet. As a workaround for the language detection we fall
8121 back to the DW_AT_producer string. */
8122 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8123 cu->language = language_opencl;
8124
3019eac3
DE
8125 /* Similar hack for Go. */
8126 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8127 set_cu_language (DW_LANG_Go, cu);
8128
f4dc4d17 8129 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8130
8131 /* Decode line number information if present. We do this before
8132 processing child DIEs, so that the line header table is available
8133 for DW_AT_decl_file. */
f4dc4d17 8134 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8135
8136 /* Process all dies in compilation unit. */
8137 if (die->child != NULL)
8138 {
8139 child_die = die->child;
8140 while (child_die && child_die->tag)
8141 {
8142 process_die (child_die, cu);
8143 child_die = sibling_die (child_die);
8144 }
8145 }
8146
8147 /* Decode macro information, if present. Dwarf 2 macro information
8148 refers to information in the line number info statement program
8149 header, so we can only read it if we've read the header
8150 successfully. */
8151 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8152 if (attr && cu->line_header)
8153 {
8154 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8155 complaint (&symfile_complaints,
8156 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8157
09262596 8158 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8159 }
8160 else
8161 {
8162 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8163 if (attr && cu->line_header)
8164 {
8165 unsigned int macro_offset = DW_UNSND (attr);
8166
09262596 8167 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8168 }
8169 }
8170
8171 do_cleanups (back_to);
8172}
8173
f4dc4d17
DE
8174/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8175 Create the set of symtabs used by this TU, or if this TU is sharing
8176 symtabs with another TU and the symtabs have already been created
8177 then restore those symtabs in the line header.
8178 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8179
8180static void
f4dc4d17 8181setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8182{
f4dc4d17
DE
8183 struct objfile *objfile = dwarf2_per_objfile->objfile;
8184 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8185 struct type_unit_group *tu_group;
8186 int first_time;
8187 struct line_header *lh;
3019eac3 8188 struct attribute *attr;
f4dc4d17 8189 unsigned int i, line_offset;
0186c6a7 8190 struct signatured_type *sig_type;
3019eac3 8191
f4dc4d17 8192 gdb_assert (per_cu->is_debug_types);
0186c6a7 8193 sig_type = (struct signatured_type *) per_cu;
3019eac3 8194
f4dc4d17 8195 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8196
f4dc4d17 8197 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8198 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8199 if (sig_type->type_unit_group == NULL)
8200 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8201 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8202
8203 /* If we've already processed this stmt_list there's no real need to
8204 do it again, we could fake it and just recreate the part we need
8205 (file name,index -> symtab mapping). If data shows this optimization
8206 is useful we can do it then. */
8207 first_time = tu_group->primary_symtab == NULL;
8208
8209 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8210 debug info. */
8211 lh = NULL;
8212 if (attr != NULL)
3019eac3 8213 {
f4dc4d17
DE
8214 line_offset = DW_UNSND (attr);
8215 lh = dwarf_decode_line_header (line_offset, cu);
8216 }
8217 if (lh == NULL)
8218 {
8219 if (first_time)
8220 dwarf2_start_symtab (cu, "", NULL, 0);
8221 else
8222 {
8223 gdb_assert (tu_group->symtabs == NULL);
8224 restart_symtab (0);
8225 }
8226 /* Note: The primary symtab will get allocated at the end. */
8227 return;
3019eac3
DE
8228 }
8229
f4dc4d17
DE
8230 cu->line_header = lh;
8231 make_cleanup (free_cu_line_header, cu);
3019eac3 8232
f4dc4d17
DE
8233 if (first_time)
8234 {
8235 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8236
f4dc4d17
DE
8237 tu_group->num_symtabs = lh->num_file_names;
8238 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8239
f4dc4d17
DE
8240 for (i = 0; i < lh->num_file_names; ++i)
8241 {
d521ce57 8242 const char *dir = NULL;
f4dc4d17 8243 struct file_entry *fe = &lh->file_names[i];
3019eac3 8244
f4dc4d17
DE
8245 if (fe->dir_index)
8246 dir = lh->include_dirs[fe->dir_index - 1];
8247 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8248
f4dc4d17
DE
8249 /* Note: We don't have to watch for the main subfile here, type units
8250 don't have DW_AT_name. */
3019eac3 8251
f4dc4d17
DE
8252 if (current_subfile->symtab == NULL)
8253 {
8254 /* NOTE: start_subfile will recognize when it's been passed
8255 a file it has already seen. So we can't assume there's a
8256 simple mapping from lh->file_names to subfiles,
8257 lh->file_names may contain dups. */
8258 current_subfile->symtab = allocate_symtab (current_subfile->name,
8259 objfile);
8260 }
8261
8262 fe->symtab = current_subfile->symtab;
8263 tu_group->symtabs[i] = fe->symtab;
8264 }
8265 }
8266 else
3019eac3 8267 {
f4dc4d17
DE
8268 restart_symtab (0);
8269
8270 for (i = 0; i < lh->num_file_names; ++i)
8271 {
8272 struct file_entry *fe = &lh->file_names[i];
8273
8274 fe->symtab = tu_group->symtabs[i];
8275 }
3019eac3
DE
8276 }
8277
f4dc4d17
DE
8278 /* The main symtab is allocated last. Type units don't have DW_AT_name
8279 so they don't have a "real" (so to speak) symtab anyway.
8280 There is later code that will assign the main symtab to all symbols
8281 that don't have one. We need to handle the case of a symbol with a
8282 missing symtab (DW_AT_decl_file) anyway. */
8283}
3019eac3 8284
f4dc4d17
DE
8285/* Process DW_TAG_type_unit.
8286 For TUs we want to skip the first top level sibling if it's not the
8287 actual type being defined by this TU. In this case the first top
8288 level sibling is there to provide context only. */
3019eac3 8289
f4dc4d17
DE
8290static void
8291read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8292{
8293 struct die_info *child_die;
3019eac3 8294
f4dc4d17
DE
8295 prepare_one_comp_unit (cu, die, language_minimal);
8296
8297 /* Initialize (or reinitialize) the machinery for building symtabs.
8298 We do this before processing child DIEs, so that the line header table
8299 is available for DW_AT_decl_file. */
8300 setup_type_unit_groups (die, cu);
8301
8302 if (die->child != NULL)
8303 {
8304 child_die = die->child;
8305 while (child_die && child_die->tag)
8306 {
8307 process_die (child_die, cu);
8308 child_die = sibling_die (child_die);
8309 }
8310 }
3019eac3
DE
8311}
8312\f
80626a55
DE
8313/* DWO/DWP files.
8314
8315 http://gcc.gnu.org/wiki/DebugFission
8316 http://gcc.gnu.org/wiki/DebugFissionDWP
8317
8318 To simplify handling of both DWO files ("object" files with the DWARF info)
8319 and DWP files (a file with the DWOs packaged up into one file), we treat
8320 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8321
8322static hashval_t
8323hash_dwo_file (const void *item)
8324{
8325 const struct dwo_file *dwo_file = item;
8326
0ac5b59e
DE
8327 return (htab_hash_string (dwo_file->dwo_name)
8328 + htab_hash_string (dwo_file->comp_dir));
3019eac3
DE
8329}
8330
8331static int
8332eq_dwo_file (const void *item_lhs, const void *item_rhs)
8333{
8334 const struct dwo_file *lhs = item_lhs;
8335 const struct dwo_file *rhs = item_rhs;
8336
0ac5b59e
DE
8337 return (strcmp (lhs->dwo_name, rhs->dwo_name) == 0
8338 && strcmp (lhs->comp_dir, rhs->comp_dir) == 0);
3019eac3
DE
8339}
8340
8341/* Allocate a hash table for DWO files. */
8342
8343static htab_t
8344allocate_dwo_file_hash_table (void)
8345{
8346 struct objfile *objfile = dwarf2_per_objfile->objfile;
8347
8348 return htab_create_alloc_ex (41,
8349 hash_dwo_file,
8350 eq_dwo_file,
8351 NULL,
8352 &objfile->objfile_obstack,
8353 hashtab_obstack_allocate,
8354 dummy_obstack_deallocate);
8355}
8356
80626a55
DE
8357/* Lookup DWO file DWO_NAME. */
8358
8359static void **
0ac5b59e 8360lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8361{
8362 struct dwo_file find_entry;
8363 void **slot;
8364
8365 if (dwarf2_per_objfile->dwo_files == NULL)
8366 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8367
8368 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8369 find_entry.dwo_name = dwo_name;
8370 find_entry.comp_dir = comp_dir;
80626a55
DE
8371 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8372
8373 return slot;
8374}
8375
3019eac3
DE
8376static hashval_t
8377hash_dwo_unit (const void *item)
8378{
8379 const struct dwo_unit *dwo_unit = item;
8380
8381 /* This drops the top 32 bits of the id, but is ok for a hash. */
8382 return dwo_unit->signature;
8383}
8384
8385static int
8386eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8387{
8388 const struct dwo_unit *lhs = item_lhs;
8389 const struct dwo_unit *rhs = item_rhs;
8390
8391 /* The signature is assumed to be unique within the DWO file.
8392 So while object file CU dwo_id's always have the value zero,
8393 that's OK, assuming each object file DWO file has only one CU,
8394 and that's the rule for now. */
8395 return lhs->signature == rhs->signature;
8396}
8397
8398/* Allocate a hash table for DWO CUs,TUs.
8399 There is one of these tables for each of CUs,TUs for each DWO file. */
8400
8401static htab_t
8402allocate_dwo_unit_table (struct objfile *objfile)
8403{
8404 /* Start out with a pretty small number.
8405 Generally DWO files contain only one CU and maybe some TUs. */
8406 return htab_create_alloc_ex (3,
8407 hash_dwo_unit,
8408 eq_dwo_unit,
8409 NULL,
8410 &objfile->objfile_obstack,
8411 hashtab_obstack_allocate,
8412 dummy_obstack_deallocate);
8413}
8414
80626a55 8415/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8416
19c3d4c9 8417struct create_dwo_cu_data
3019eac3
DE
8418{
8419 struct dwo_file *dwo_file;
19c3d4c9 8420 struct dwo_unit dwo_unit;
3019eac3
DE
8421};
8422
19c3d4c9 8423/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8424
8425static void
19c3d4c9
DE
8426create_dwo_cu_reader (const struct die_reader_specs *reader,
8427 const gdb_byte *info_ptr,
8428 struct die_info *comp_unit_die,
8429 int has_children,
8430 void *datap)
3019eac3
DE
8431{
8432 struct dwarf2_cu *cu = reader->cu;
8433 struct objfile *objfile = dwarf2_per_objfile->objfile;
8434 sect_offset offset = cu->per_cu->offset;
8a0459fd 8435 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8436 struct create_dwo_cu_data *data = datap;
3019eac3 8437 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8438 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8439 struct attribute *attr;
3019eac3
DE
8440
8441 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8442 if (attr == NULL)
8443 {
19c3d4c9
DE
8444 complaint (&symfile_complaints,
8445 _("Dwarf Error: debug entry at offset 0x%x is missing"
8446 " its dwo_id [in module %s]"),
8447 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8448 return;
8449 }
8450
3019eac3
DE
8451 dwo_unit->dwo_file = dwo_file;
8452 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8453 dwo_unit->section = section;
3019eac3
DE
8454 dwo_unit->offset = offset;
8455 dwo_unit->length = cu->per_cu->length;
8456
09406207 8457 if (dwarf2_read_debug)
4031ecc5
DE
8458 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8459 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8460}
8461
19c3d4c9
DE
8462/* Create the dwo_unit for the lone CU in DWO_FILE.
8463 Note: This function processes DWO files only, not DWP files. */
3019eac3 8464
19c3d4c9
DE
8465static struct dwo_unit *
8466create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8467{
8468 struct objfile *objfile = dwarf2_per_objfile->objfile;
8469 struct dwarf2_section_info *section = &dwo_file->sections.info;
8470 bfd *abfd;
8471 htab_t cu_htab;
d521ce57 8472 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8473 struct create_dwo_cu_data create_dwo_cu_data;
8474 struct dwo_unit *dwo_unit;
3019eac3
DE
8475
8476 dwarf2_read_section (objfile, section);
8477 info_ptr = section->buffer;
8478
8479 if (info_ptr == NULL)
8480 return NULL;
8481
8482 /* We can't set abfd until now because the section may be empty or
8483 not present, in which case section->asection will be NULL. */
8484 abfd = section->asection->owner;
8485
09406207 8486 if (dwarf2_read_debug)
19c3d4c9
DE
8487 {
8488 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8489 bfd_section_name (abfd, section->asection),
8490 bfd_get_filename (abfd));
8491 }
3019eac3 8492
19c3d4c9
DE
8493 create_dwo_cu_data.dwo_file = dwo_file;
8494 dwo_unit = NULL;
3019eac3
DE
8495
8496 end_ptr = info_ptr + section->size;
8497 while (info_ptr < end_ptr)
8498 {
8499 struct dwarf2_per_cu_data per_cu;
8500
19c3d4c9
DE
8501 memset (&create_dwo_cu_data.dwo_unit, 0,
8502 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8503 memset (&per_cu, 0, sizeof (per_cu));
8504 per_cu.objfile = objfile;
8505 per_cu.is_debug_types = 0;
8506 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8507 per_cu.section = section;
3019eac3
DE
8508
8509 init_cutu_and_read_dies_no_follow (&per_cu,
8510 &dwo_file->sections.abbrev,
8511 dwo_file,
19c3d4c9
DE
8512 create_dwo_cu_reader,
8513 &create_dwo_cu_data);
8514
8515 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8516 {
8517 /* If we've already found one, complain. We only support one
8518 because having more than one requires hacking the dwo_name of
8519 each to match, which is highly unlikely to happen. */
8520 if (dwo_unit != NULL)
8521 {
8522 complaint (&symfile_complaints,
8523 _("Multiple CUs in DWO file %s [in module %s]"),
8524 dwo_file->dwo_name, objfile->name);
8525 break;
8526 }
8527
8528 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8529 *dwo_unit = create_dwo_cu_data.dwo_unit;
8530 }
3019eac3
DE
8531
8532 info_ptr += per_cu.length;
8533 }
8534
19c3d4c9 8535 return dwo_unit;
3019eac3
DE
8536}
8537
80626a55
DE
8538/* DWP file .debug_{cu,tu}_index section format:
8539 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8540
8541 Both index sections have the same format, and serve to map a 64-bit
8542 signature to a set of section numbers. Each section begins with a header,
8543 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8544 indexes, and a pool of 32-bit section numbers. The index sections will be
8545 aligned at 8-byte boundaries in the file.
8546
8547 The index section header contains two unsigned 32-bit values (using the
8548 byte order of the application binary):
8549
8550 N, the number of compilation units or type units in the index
8551 M, the number of slots in the hash table
8552
8553 (We assume that N and M will not exceed 2^32 - 1.)
8554
8555 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8556
8557 The hash table begins at offset 8 in the section, and consists of an array
8558 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8559 order of the application binary). Unused slots in the hash table are 0.
8560 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8561
8562 The parallel table begins immediately after the hash table
8563 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8564 array of 32-bit indexes (using the byte order of the application binary),
8565 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8566 table contains a 32-bit index into the pool of section numbers. For unused
8567 hash table slots, the corresponding entry in the parallel table will be 0.
8568
8569 Given a 64-bit compilation unit signature or a type signature S, an entry
8570 in the hash table is located as follows:
8571
8572 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8573 the low-order k bits all set to 1.
8574
8575 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8576
8577 3) If the hash table entry at index H matches the signature, use that
8578 entry. If the hash table entry at index H is unused (all zeroes),
8579 terminate the search: the signature is not present in the table.
8580
8581 4) Let H = (H + H') modulo M. Repeat at Step 3.
8582
8583 Because M > N and H' and M are relatively prime, the search is guaranteed
8584 to stop at an unused slot or find the match.
8585
8586 The pool of section numbers begins immediately following the hash table
8587 (at offset 8 + 12 * M from the beginning of the section). The pool of
8588 section numbers consists of an array of 32-bit words (using the byte order
8589 of the application binary). Each item in the array is indexed starting
8590 from 0. The hash table entry provides the index of the first section
8591 number in the set. Additional section numbers in the set follow, and the
8592 set is terminated by a 0 entry (section number 0 is not used in ELF).
8593
8594 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8595 section must be the first entry in the set, and the .debug_abbrev.dwo must
8596 be the second entry. Other members of the set may follow in any order. */
8597
8598/* Create a hash table to map DWO IDs to their CU/TU entry in
8599 .debug_{info,types}.dwo in DWP_FILE.
8600 Returns NULL if there isn't one.
8601 Note: This function processes DWP files only, not DWO files. */
8602
8603static struct dwp_hash_table *
8604create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8605{
8606 struct objfile *objfile = dwarf2_per_objfile->objfile;
8607 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8608 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8609 struct dwarf2_section_info *index;
8610 uint32_t version, nr_units, nr_slots;
8611 struct dwp_hash_table *htab;
8612
8613 if (is_debug_types)
8614 index = &dwp_file->sections.tu_index;
8615 else
8616 index = &dwp_file->sections.cu_index;
8617
8618 if (dwarf2_section_empty_p (index))
8619 return NULL;
8620 dwarf2_read_section (objfile, index);
8621
8622 index_ptr = index->buffer;
8623 index_end = index_ptr + index->size;
8624
8625 version = read_4_bytes (dbfd, index_ptr);
8626 index_ptr += 8; /* Skip the unused word. */
8627 nr_units = read_4_bytes (dbfd, index_ptr);
8628 index_ptr += 4;
8629 nr_slots = read_4_bytes (dbfd, index_ptr);
8630 index_ptr += 4;
8631
8632 if (version != 1)
8633 {
8634 error (_("Dwarf Error: unsupported DWP file version (%u)"
8635 " [in module %s]"),
8636 version, dwp_file->name);
8637 }
8638 if (nr_slots != (nr_slots & -nr_slots))
8639 {
8640 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8641 " is not power of 2 [in module %s]"),
8642 nr_slots, dwp_file->name);
8643 }
8644
8645 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8646 htab->nr_units = nr_units;
8647 htab->nr_slots = nr_slots;
8648 htab->hash_table = index_ptr;
8649 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8650 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8651
8652 return htab;
8653}
8654
8655/* Update SECTIONS with the data from SECTP.
8656
8657 This function is like the other "locate" section routines that are
8658 passed to bfd_map_over_sections, but in this context the sections to
8659 read comes from the DWP hash table, not the full ELF section table.
8660
8661 The result is non-zero for success, or zero if an error was found. */
8662
8663static int
8664locate_virtual_dwo_sections (asection *sectp,
8665 struct virtual_dwo_sections *sections)
8666{
8667 const struct dwop_section_names *names = &dwop_section_names;
8668
8669 if (section_is_p (sectp->name, &names->abbrev_dwo))
8670 {
8671 /* There can be only one. */
8672 if (sections->abbrev.asection != NULL)
8673 return 0;
8674 sections->abbrev.asection = sectp;
8675 sections->abbrev.size = bfd_get_section_size (sectp);
8676 }
8677 else if (section_is_p (sectp->name, &names->info_dwo)
8678 || section_is_p (sectp->name, &names->types_dwo))
8679 {
8680 /* There can be only one. */
8681 if (sections->info_or_types.asection != NULL)
8682 return 0;
8683 sections->info_or_types.asection = sectp;
8684 sections->info_or_types.size = bfd_get_section_size (sectp);
8685 }
8686 else if (section_is_p (sectp->name, &names->line_dwo))
8687 {
8688 /* There can be only one. */
8689 if (sections->line.asection != NULL)
8690 return 0;
8691 sections->line.asection = sectp;
8692 sections->line.size = bfd_get_section_size (sectp);
8693 }
8694 else if (section_is_p (sectp->name, &names->loc_dwo))
8695 {
8696 /* There can be only one. */
8697 if (sections->loc.asection != NULL)
8698 return 0;
8699 sections->loc.asection = sectp;
8700 sections->loc.size = bfd_get_section_size (sectp);
8701 }
8702 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8703 {
8704 /* There can be only one. */
8705 if (sections->macinfo.asection != NULL)
8706 return 0;
8707 sections->macinfo.asection = sectp;
8708 sections->macinfo.size = bfd_get_section_size (sectp);
8709 }
8710 else if (section_is_p (sectp->name, &names->macro_dwo))
8711 {
8712 /* There can be only one. */
8713 if (sections->macro.asection != NULL)
8714 return 0;
8715 sections->macro.asection = sectp;
8716 sections->macro.size = bfd_get_section_size (sectp);
8717 }
8718 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8719 {
8720 /* There can be only one. */
8721 if (sections->str_offsets.asection != NULL)
8722 return 0;
8723 sections->str_offsets.asection = sectp;
8724 sections->str_offsets.size = bfd_get_section_size (sectp);
8725 }
8726 else
8727 {
8728 /* No other kind of section is valid. */
8729 return 0;
8730 }
8731
8732 return 1;
8733}
8734
8735/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8736 HTAB is the hash table from the DWP file.
0ac5b59e
DE
8737 SECTION_INDEX is the index of the DWO in HTAB.
8738 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
8739
8740static struct dwo_unit *
8741create_dwo_in_dwp (struct dwp_file *dwp_file,
8742 const struct dwp_hash_table *htab,
8743 uint32_t section_index,
0ac5b59e 8744 const char *comp_dir,
80626a55
DE
8745 ULONGEST signature, int is_debug_types)
8746{
8747 struct objfile *objfile = dwarf2_per_objfile->objfile;
8748 bfd *dbfd = dwp_file->dbfd;
8749 const char *kind = is_debug_types ? "TU" : "CU";
8750 struct dwo_file *dwo_file;
8751 struct dwo_unit *dwo_unit;
8752 struct virtual_dwo_sections sections;
8753 void **dwo_file_slot;
8754 char *virtual_dwo_name;
8755 struct dwarf2_section_info *cutu;
8756 struct cleanup *cleanups;
8757 int i;
8758
8759 if (dwarf2_read_debug)
8760 {
4031ecc5 8761 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/%s in DWP file: %s\n",
80626a55 8762 kind,
4031ecc5 8763 section_index, hex_string (signature),
80626a55
DE
8764 dwp_file->name);
8765 }
8766
8767 /* Fetch the sections of this DWO.
8768 Put a limit on the number of sections we look for so that bad data
8769 doesn't cause us to loop forever. */
8770
8771#define MAX_NR_DWO_SECTIONS \
8772 (1 /* .debug_info or .debug_types */ \
8773 + 1 /* .debug_abbrev */ \
8774 + 1 /* .debug_line */ \
8775 + 1 /* .debug_loc */ \
8776 + 1 /* .debug_str_offsets */ \
8777 + 1 /* .debug_macro */ \
8778 + 1 /* .debug_macinfo */ \
8779 + 1 /* trailing zero */)
8780
8781 memset (&sections, 0, sizeof (sections));
8782 cleanups = make_cleanup (null_cleanup, 0);
8783
8784 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8785 {
8786 asection *sectp;
8787 uint32_t section_nr =
8788 read_4_bytes (dbfd,
8789 htab->section_pool
8790 + (section_index + i) * sizeof (uint32_t));
8791
8792 if (section_nr == 0)
8793 break;
8794 if (section_nr >= dwp_file->num_sections)
8795 {
8796 error (_("Dwarf Error: bad DWP hash table, section number too large"
8797 " [in module %s]"),
8798 dwp_file->name);
8799 }
8800
8801 sectp = dwp_file->elf_sections[section_nr];
8802 if (! locate_virtual_dwo_sections (sectp, &sections))
8803 {
8804 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8805 " [in module %s]"),
8806 dwp_file->name);
8807 }
8808 }
8809
8810 if (i < 2
8811 || sections.info_or_types.asection == NULL
8812 || sections.abbrev.asection == NULL)
8813 {
8814 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8815 " [in module %s]"),
8816 dwp_file->name);
8817 }
8818 if (i == MAX_NR_DWO_SECTIONS)
8819 {
8820 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8821 " [in module %s]"),
8822 dwp_file->name);
8823 }
8824
8825 /* It's easier for the rest of the code if we fake a struct dwo_file and
8826 have dwo_unit "live" in that. At least for now.
8827
8828 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8829 However, for each CU + set of TUs that came from the same original DWO
8830 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8831 (fewer struct dwo_file objects to allocated). Remember that for really
8832 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8833
2792b94d
PM
8834 virtual_dwo_name =
8835 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8836 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8837 sections.line.asection ? sections.line.asection->id : 0,
8838 sections.loc.asection ? sections.loc.asection->id : 0,
8839 (sections.str_offsets.asection
8840 ? sections.str_offsets.asection->id
8841 : 0));
80626a55
DE
8842 make_cleanup (xfree, virtual_dwo_name);
8843 /* Can we use an existing virtual DWO file? */
0ac5b59e 8844 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
8845 /* Create one if necessary. */
8846 if (*dwo_file_slot == NULL)
8847 {
8848 if (dwarf2_read_debug)
8849 {
8850 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8851 virtual_dwo_name);
8852 }
8853 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
8854 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
8855 virtual_dwo_name,
8856 strlen (virtual_dwo_name));
8857 dwo_file->comp_dir = comp_dir;
80626a55
DE
8858 dwo_file->sections.abbrev = sections.abbrev;
8859 dwo_file->sections.line = sections.line;
8860 dwo_file->sections.loc = sections.loc;
8861 dwo_file->sections.macinfo = sections.macinfo;
8862 dwo_file->sections.macro = sections.macro;
8863 dwo_file->sections.str_offsets = sections.str_offsets;
8864 /* The "str" section is global to the entire DWP file. */
8865 dwo_file->sections.str = dwp_file->sections.str;
8866 /* The info or types section is assigned later to dwo_unit,
8867 there's no need to record it in dwo_file.
8868 Also, we can't simply record type sections in dwo_file because
8869 we record a pointer into the vector in dwo_unit. As we collect more
8870 types we'll grow the vector and eventually have to reallocate space
8871 for it, invalidating all the pointers into the current copy. */
8872 *dwo_file_slot = dwo_file;
8873 }
8874 else
8875 {
8876 if (dwarf2_read_debug)
8877 {
8878 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8879 virtual_dwo_name);
8880 }
8881 dwo_file = *dwo_file_slot;
8882 }
8883 do_cleanups (cleanups);
8884
8885 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8886 dwo_unit->dwo_file = dwo_file;
8887 dwo_unit->signature = signature;
8a0459fd
DE
8888 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8889 sizeof (struct dwarf2_section_info));
8890 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8891 /* offset, length, type_offset_in_tu are set later. */
8892
8893 return dwo_unit;
8894}
8895
8896/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8897
8898static struct dwo_unit *
8899lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8900 const struct dwp_hash_table *htab,
0ac5b59e 8901 const char *comp_dir,
80626a55
DE
8902 ULONGEST signature, int is_debug_types)
8903{
8904 bfd *dbfd = dwp_file->dbfd;
8905 uint32_t mask = htab->nr_slots - 1;
8906 uint32_t hash = signature & mask;
8907 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8908 unsigned int i;
8909 void **slot;
8910 struct dwo_unit find_dwo_cu, *dwo_cu;
8911
8912 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8913 find_dwo_cu.signature = signature;
8914 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8915
8916 if (*slot != NULL)
8917 return *slot;
8918
8919 /* Use a for loop so that we don't loop forever on bad debug info. */
8920 for (i = 0; i < htab->nr_slots; ++i)
8921 {
8922 ULONGEST signature_in_table;
8923
8924 signature_in_table =
8925 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8926 if (signature_in_table == signature)
8927 {
8928 uint32_t section_index =
8929 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8930
8931 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 8932 comp_dir, signature, is_debug_types);
80626a55
DE
8933 return *slot;
8934 }
8935 if (signature_in_table == 0)
8936 return NULL;
8937 hash = (hash + hash2) & mask;
8938 }
8939
8940 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8941 " [in module %s]"),
8942 dwp_file->name);
8943}
8944
ab5088bf 8945/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
8946 Open the file specified by FILE_NAME and hand it off to BFD for
8947 preliminary analysis. Return a newly initialized bfd *, which
8948 includes a canonicalized copy of FILE_NAME.
80626a55 8949 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8950 In case of trouble, return NULL.
8951 NOTE: This function is derived from symfile_bfd_open. */
8952
8953static bfd *
80626a55 8954try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8955{
8956 bfd *sym_bfd;
80626a55 8957 int desc, flags;
3019eac3 8958 char *absolute_name;
3019eac3 8959
80626a55
DE
8960 flags = OPF_TRY_CWD_FIRST;
8961 if (is_dwp)
8962 flags |= OPF_SEARCH_IN_PATH;
8963 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8964 O_RDONLY | O_BINARY, &absolute_name);
8965 if (desc < 0)
8966 return NULL;
8967
bb397797 8968 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8969 if (!sym_bfd)
8970 {
3019eac3
DE
8971 xfree (absolute_name);
8972 return NULL;
8973 }
a4453b7e 8974 xfree (absolute_name);
3019eac3
DE
8975 bfd_set_cacheable (sym_bfd, 1);
8976
8977 if (!bfd_check_format (sym_bfd, bfd_object))
8978 {
cbb099e8 8979 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8980 return NULL;
8981 }
8982
3019eac3
DE
8983 return sym_bfd;
8984}
8985
ab5088bf 8986/* Try to open DWO file FILE_NAME.
3019eac3
DE
8987 COMP_DIR is the DW_AT_comp_dir attribute.
8988 The result is the bfd handle of the file.
8989 If there is a problem finding or opening the file, return NULL.
8990 Upon success, the canonicalized path of the file is stored in the bfd,
8991 same as symfile_bfd_open. */
8992
8993static bfd *
ab5088bf 8994open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
8995{
8996 bfd *abfd;
3019eac3 8997
80626a55 8998 if (IS_ABSOLUTE_PATH (file_name))
ab5088bf 8999 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9000
9001 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9002
9003 if (comp_dir != NULL)
9004 {
80626a55 9005 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9006
9007 /* NOTE: If comp_dir is a relative path, this will also try the
9008 search path, which seems useful. */
ab5088bf 9009 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
3019eac3
DE
9010 xfree (path_to_try);
9011 if (abfd != NULL)
9012 return abfd;
9013 }
9014
9015 /* That didn't work, try debug-file-directory, which, despite its name,
9016 is a list of paths. */
9017
9018 if (*debug_file_directory == '\0')
9019 return NULL;
9020
ab5088bf 9021 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9022}
9023
80626a55
DE
9024/* This function is mapped across the sections and remembers the offset and
9025 size of each of the DWO debugging sections we are interested in. */
9026
9027static void
9028dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9029{
9030 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9031 const struct dwop_section_names *names = &dwop_section_names;
9032
9033 if (section_is_p (sectp->name, &names->abbrev_dwo))
9034 {
9035 dwo_sections->abbrev.asection = sectp;
9036 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9037 }
9038 else if (section_is_p (sectp->name, &names->info_dwo))
9039 {
9040 dwo_sections->info.asection = sectp;
9041 dwo_sections->info.size = bfd_get_section_size (sectp);
9042 }
9043 else if (section_is_p (sectp->name, &names->line_dwo))
9044 {
9045 dwo_sections->line.asection = sectp;
9046 dwo_sections->line.size = bfd_get_section_size (sectp);
9047 }
9048 else if (section_is_p (sectp->name, &names->loc_dwo))
9049 {
9050 dwo_sections->loc.asection = sectp;
9051 dwo_sections->loc.size = bfd_get_section_size (sectp);
9052 }
9053 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9054 {
9055 dwo_sections->macinfo.asection = sectp;
9056 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9057 }
9058 else if (section_is_p (sectp->name, &names->macro_dwo))
9059 {
9060 dwo_sections->macro.asection = sectp;
9061 dwo_sections->macro.size = bfd_get_section_size (sectp);
9062 }
9063 else if (section_is_p (sectp->name, &names->str_dwo))
9064 {
9065 dwo_sections->str.asection = sectp;
9066 dwo_sections->str.size = bfd_get_section_size (sectp);
9067 }
9068 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9069 {
9070 dwo_sections->str_offsets.asection = sectp;
9071 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9072 }
9073 else if (section_is_p (sectp->name, &names->types_dwo))
9074 {
9075 struct dwarf2_section_info type_section;
9076
9077 memset (&type_section, 0, sizeof (type_section));
9078 type_section.asection = sectp;
9079 type_section.size = bfd_get_section_size (sectp);
9080 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9081 &type_section);
9082 }
9083}
9084
ab5088bf 9085/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9086 by PER_CU. This is for the non-DWP case.
80626a55 9087 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9088
9089static struct dwo_file *
0ac5b59e
DE
9090open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9091 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9092{
9093 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9094 struct dwo_file *dwo_file;
9095 bfd *dbfd;
3019eac3
DE
9096 struct cleanup *cleanups;
9097
ab5088bf 9098 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9099 if (dbfd == NULL)
9100 {
9101 if (dwarf2_read_debug)
9102 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9103 return NULL;
9104 }
9105 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9106 dwo_file->dwo_name = dwo_name;
9107 dwo_file->comp_dir = comp_dir;
80626a55 9108 dwo_file->dbfd = dbfd;
3019eac3
DE
9109
9110 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9111
80626a55 9112 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9113
19c3d4c9 9114 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9115
9116 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9117 dwo_file->sections.types);
9118
9119 discard_cleanups (cleanups);
9120
80626a55
DE
9121 if (dwarf2_read_debug)
9122 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9123
3019eac3
DE
9124 return dwo_file;
9125}
9126
80626a55
DE
9127/* This function is mapped across the sections and remembers the offset and
9128 size of each of the DWP debugging sections we are interested in. */
3019eac3 9129
80626a55
DE
9130static void
9131dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9132{
80626a55
DE
9133 struct dwp_file *dwp_file = dwp_file_ptr;
9134 const struct dwop_section_names *names = &dwop_section_names;
9135 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9136
80626a55
DE
9137 /* Record the ELF section number for later lookup: this is what the
9138 .debug_cu_index,.debug_tu_index tables use. */
9139 gdb_assert (elf_section_nr < dwp_file->num_sections);
9140 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9141
80626a55
DE
9142 /* Look for specific sections that we need. */
9143 if (section_is_p (sectp->name, &names->str_dwo))
9144 {
9145 dwp_file->sections.str.asection = sectp;
9146 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9147 }
9148 else if (section_is_p (sectp->name, &names->cu_index))
9149 {
9150 dwp_file->sections.cu_index.asection = sectp;
9151 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9152 }
9153 else if (section_is_p (sectp->name, &names->tu_index))
9154 {
9155 dwp_file->sections.tu_index.asection = sectp;
9156 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9157 }
9158}
3019eac3 9159
80626a55 9160/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9161
80626a55
DE
9162static hashval_t
9163hash_dwp_loaded_cutus (const void *item)
9164{
9165 const struct dwo_unit *dwo_unit = item;
3019eac3 9166
80626a55
DE
9167 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9168 return dwo_unit->signature;
3019eac3
DE
9169}
9170
80626a55 9171/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9172
80626a55
DE
9173static int
9174eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9175{
80626a55
DE
9176 const struct dwo_unit *dua = a;
9177 const struct dwo_unit *dub = b;
3019eac3 9178
80626a55
DE
9179 return dua->signature == dub->signature;
9180}
3019eac3 9181
80626a55 9182/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9183
80626a55
DE
9184static htab_t
9185allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9186{
9187 return htab_create_alloc_ex (3,
9188 hash_dwp_loaded_cutus,
9189 eq_dwp_loaded_cutus,
9190 NULL,
9191 &objfile->objfile_obstack,
9192 hashtab_obstack_allocate,
9193 dummy_obstack_deallocate);
9194}
3019eac3 9195
ab5088bf
DE
9196/* Try to open DWP file FILE_NAME.
9197 The result is the bfd handle of the file.
9198 If there is a problem finding or opening the file, return NULL.
9199 Upon success, the canonicalized path of the file is stored in the bfd,
9200 same as symfile_bfd_open. */
9201
9202static bfd *
9203open_dwp_file (const char *file_name)
9204{
9205 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9206}
9207
80626a55
DE
9208/* Initialize the use of the DWP file for the current objfile.
9209 By convention the name of the DWP file is ${objfile}.dwp.
9210 The result is NULL if it can't be found. */
a766d390 9211
80626a55 9212static struct dwp_file *
ab5088bf 9213open_and_init_dwp_file (void)
80626a55
DE
9214{
9215 struct objfile *objfile = dwarf2_per_objfile->objfile;
9216 struct dwp_file *dwp_file;
9217 char *dwp_name;
9218 bfd *dbfd;
9219 struct cleanup *cleanups;
9220
2792b94d 9221 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9222 cleanups = make_cleanup (xfree, dwp_name);
9223
ab5088bf 9224 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9225 if (dbfd == NULL)
9226 {
9227 if (dwarf2_read_debug)
9228 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9229 do_cleanups (cleanups);
9230 return NULL;
3019eac3 9231 }
80626a55
DE
9232 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9233 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9234 dwp_name, strlen (dwp_name));
9235 dwp_file->dbfd = dbfd;
9236 do_cleanups (cleanups);
c906108c 9237
80626a55
DE
9238 /* +1: section 0 is unused */
9239 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9240 dwp_file->elf_sections =
9241 OBSTACK_CALLOC (&objfile->objfile_obstack,
9242 dwp_file->num_sections, asection *);
9243
9244 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9245
9246 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9247
9248 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9249
9250 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9251
80626a55
DE
9252 if (dwarf2_read_debug)
9253 {
9254 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9255 fprintf_unfiltered (gdb_stdlog,
9256 " %u CUs, %u TUs\n",
9257 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9258 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9259 }
9260
9261 return dwp_file;
3019eac3 9262}
c906108c 9263
ab5088bf
DE
9264/* Wrapper around open_and_init_dwp_file, only open it once. */
9265
9266static struct dwp_file *
9267get_dwp_file (void)
9268{
9269 if (! dwarf2_per_objfile->dwp_checked)
9270 {
9271 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9272 dwarf2_per_objfile->dwp_checked = 1;
9273 }
9274 return dwarf2_per_objfile->dwp_file;
9275}
9276
80626a55
DE
9277/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9278 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9279 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9280 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9281 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9282
9283 This is called, for example, when wanting to read a variable with a
9284 complex location. Therefore we don't want to do file i/o for every call.
9285 Therefore we don't want to look for a DWO file on every call.
9286 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9287 then we check if we've already seen DWO_NAME, and only THEN do we check
9288 for a DWO file.
9289
1c658ad5 9290 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9291 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9292
3019eac3 9293static struct dwo_unit *
80626a55
DE
9294lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9295 const char *dwo_name, const char *comp_dir,
9296 ULONGEST signature, int is_debug_types)
3019eac3
DE
9297{
9298 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9299 const char *kind = is_debug_types ? "TU" : "CU";
9300 void **dwo_file_slot;
3019eac3 9301 struct dwo_file *dwo_file;
80626a55 9302 struct dwp_file *dwp_file;
cb1df416 9303
6a506a2d
DE
9304 /* First see if there's a DWP file.
9305 If we have a DWP file but didn't find the DWO inside it, don't
9306 look for the original DWO file. It makes gdb behave differently
9307 depending on whether one is debugging in the build tree. */
cf2c3c16 9308
ab5088bf 9309 dwp_file = get_dwp_file ();
80626a55 9310 if (dwp_file != NULL)
cf2c3c16 9311 {
80626a55
DE
9312 const struct dwp_hash_table *dwp_htab =
9313 is_debug_types ? dwp_file->tus : dwp_file->cus;
9314
9315 if (dwp_htab != NULL)
9316 {
9317 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9318 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9319 signature, is_debug_types);
80626a55
DE
9320
9321 if (dwo_cutu != NULL)
9322 {
9323 if (dwarf2_read_debug)
9324 {
9325 fprintf_unfiltered (gdb_stdlog,
9326 "Virtual DWO %s %s found: @%s\n",
9327 kind, hex_string (signature),
9328 host_address_to_string (dwo_cutu));
9329 }
9330 return dwo_cutu;
9331 }
9332 }
9333 }
6a506a2d 9334 else
80626a55 9335 {
6a506a2d 9336 /* No DWP file, look for the DWO file. */
80626a55 9337
6a506a2d
DE
9338 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9339 if (*dwo_file_slot == NULL)
80626a55 9340 {
6a506a2d
DE
9341 /* Read in the file and build a table of the CUs/TUs it contains. */
9342 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9343 }
6a506a2d
DE
9344 /* NOTE: This will be NULL if unable to open the file. */
9345 dwo_file = *dwo_file_slot;
3019eac3 9346
6a506a2d 9347 if (dwo_file != NULL)
19c3d4c9 9348 {
6a506a2d
DE
9349 struct dwo_unit *dwo_cutu = NULL;
9350
9351 if (is_debug_types && dwo_file->tus)
9352 {
9353 struct dwo_unit find_dwo_cutu;
9354
9355 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9356 find_dwo_cutu.signature = signature;
9357 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9358 }
9359 else if (!is_debug_types && dwo_file->cu)
80626a55 9360 {
6a506a2d
DE
9361 if (signature == dwo_file->cu->signature)
9362 dwo_cutu = dwo_file->cu;
9363 }
9364
9365 if (dwo_cutu != NULL)
9366 {
9367 if (dwarf2_read_debug)
9368 {
9369 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9370 kind, dwo_name, hex_string (signature),
9371 host_address_to_string (dwo_cutu));
9372 }
9373 return dwo_cutu;
80626a55
DE
9374 }
9375 }
2e276125 9376 }
9cdd5dbd 9377
80626a55
DE
9378 /* We didn't find it. This could mean a dwo_id mismatch, or
9379 someone deleted the DWO/DWP file, or the search path isn't set up
9380 correctly to find the file. */
9381
9382 if (dwarf2_read_debug)
9383 {
9384 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9385 kind, dwo_name, hex_string (signature));
9386 }
3019eac3
DE
9387
9388 complaint (&symfile_complaints,
6a506a2d 9389 _("Could not find DWO %s %s(%s) referenced by CU at offset 0x%x"
3019eac3 9390 " [in module %s]"),
6a506a2d
DE
9391 kind, dwo_name, hex_string (signature),
9392 this_unit->offset.sect_off, objfile->name);
3019eac3 9393 return NULL;
5fb290d7
DJ
9394}
9395
80626a55
DE
9396/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9397 See lookup_dwo_cutu_unit for details. */
9398
9399static struct dwo_unit *
9400lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9401 const char *dwo_name, const char *comp_dir,
9402 ULONGEST signature)
9403{
9404 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9405}
9406
9407/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9408 See lookup_dwo_cutu_unit for details. */
9409
9410static struct dwo_unit *
9411lookup_dwo_type_unit (struct signatured_type *this_tu,
9412 const char *dwo_name, const char *comp_dir)
9413{
9414 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9415}
9416
3019eac3
DE
9417/* Free all resources associated with DWO_FILE.
9418 Close the DWO file and munmap the sections.
9419 All memory should be on the objfile obstack. */
348e048f
DE
9420
9421static void
3019eac3 9422free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9423{
3019eac3
DE
9424 int ix;
9425 struct dwarf2_section_info *section;
348e048f 9426
5c6fa7ab 9427 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9428 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9429
3019eac3
DE
9430 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9431}
348e048f 9432
3019eac3 9433/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9434
3019eac3
DE
9435static void
9436free_dwo_file_cleanup (void *arg)
9437{
9438 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9439 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9440
3019eac3
DE
9441 free_dwo_file (dwo_file, objfile);
9442}
348e048f 9443
3019eac3 9444/* Traversal function for free_dwo_files. */
2ab95328 9445
3019eac3
DE
9446static int
9447free_dwo_file_from_slot (void **slot, void *info)
9448{
9449 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9450 struct objfile *objfile = (struct objfile *) info;
348e048f 9451
3019eac3 9452 free_dwo_file (dwo_file, objfile);
348e048f 9453
3019eac3
DE
9454 return 1;
9455}
348e048f 9456
3019eac3 9457/* Free all resources associated with DWO_FILES. */
348e048f 9458
3019eac3
DE
9459static void
9460free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9461{
9462 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9463}
3019eac3
DE
9464\f
9465/* Read in various DIEs. */
348e048f 9466
d389af10
JK
9467/* qsort helper for inherit_abstract_dies. */
9468
9469static int
9470unsigned_int_compar (const void *ap, const void *bp)
9471{
9472 unsigned int a = *(unsigned int *) ap;
9473 unsigned int b = *(unsigned int *) bp;
9474
9475 return (a > b) - (b > a);
9476}
9477
9478/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9479 Inherit only the children of the DW_AT_abstract_origin DIE not being
9480 already referenced by DW_AT_abstract_origin from the children of the
9481 current DIE. */
d389af10
JK
9482
9483static void
9484inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9485{
9486 struct die_info *child_die;
9487 unsigned die_children_count;
9488 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9489 sect_offset *offsets;
9490 sect_offset *offsets_end, *offsetp;
d389af10
JK
9491 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9492 struct die_info *origin_die;
9493 /* Iterator of the ORIGIN_DIE children. */
9494 struct die_info *origin_child_die;
9495 struct cleanup *cleanups;
9496 struct attribute *attr;
cd02d79d
PA
9497 struct dwarf2_cu *origin_cu;
9498 struct pending **origin_previous_list_in_scope;
d389af10
JK
9499
9500 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9501 if (!attr)
9502 return;
9503
cd02d79d
PA
9504 /* Note that following die references may follow to a die in a
9505 different cu. */
9506
9507 origin_cu = cu;
9508 origin_die = follow_die_ref (die, attr, &origin_cu);
9509
9510 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9511 symbols in. */
9512 origin_previous_list_in_scope = origin_cu->list_in_scope;
9513 origin_cu->list_in_scope = cu->list_in_scope;
9514
edb3359d
DJ
9515 if (die->tag != origin_die->tag
9516 && !(die->tag == DW_TAG_inlined_subroutine
9517 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9518 complaint (&symfile_complaints,
9519 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9520 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9521
9522 child_die = die->child;
9523 die_children_count = 0;
9524 while (child_die && child_die->tag)
9525 {
9526 child_die = sibling_die (child_die);
9527 die_children_count++;
9528 }
9529 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9530 cleanups = make_cleanup (xfree, offsets);
9531
9532 offsets_end = offsets;
9533 child_die = die->child;
9534 while (child_die && child_die->tag)
9535 {
c38f313d
DJ
9536 /* For each CHILD_DIE, find the corresponding child of
9537 ORIGIN_DIE. If there is more than one layer of
9538 DW_AT_abstract_origin, follow them all; there shouldn't be,
9539 but GCC versions at least through 4.4 generate this (GCC PR
9540 40573). */
9541 struct die_info *child_origin_die = child_die;
cd02d79d 9542 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9543
c38f313d
DJ
9544 while (1)
9545 {
cd02d79d
PA
9546 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9547 child_origin_cu);
c38f313d
DJ
9548 if (attr == NULL)
9549 break;
cd02d79d
PA
9550 child_origin_die = follow_die_ref (child_origin_die, attr,
9551 &child_origin_cu);
c38f313d
DJ
9552 }
9553
d389af10
JK
9554 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9555 counterpart may exist. */
c38f313d 9556 if (child_origin_die != child_die)
d389af10 9557 {
edb3359d
DJ
9558 if (child_die->tag != child_origin_die->tag
9559 && !(child_die->tag == DW_TAG_inlined_subroutine
9560 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9561 complaint (&symfile_complaints,
9562 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9563 "different tags"), child_die->offset.sect_off,
9564 child_origin_die->offset.sect_off);
c38f313d
DJ
9565 if (child_origin_die->parent != origin_die)
9566 complaint (&symfile_complaints,
9567 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9568 "different parents"), child_die->offset.sect_off,
9569 child_origin_die->offset.sect_off);
c38f313d
DJ
9570 else
9571 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9572 }
9573 child_die = sibling_die (child_die);
9574 }
9575 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9576 unsigned_int_compar);
9577 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9578 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9579 complaint (&symfile_complaints,
9580 _("Multiple children of DIE 0x%x refer "
9581 "to DIE 0x%x as their abstract origin"),
b64f50a1 9582 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9583
9584 offsetp = offsets;
9585 origin_child_die = origin_die->child;
9586 while (origin_child_die && origin_child_die->tag)
9587 {
9588 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9589 while (offsetp < offsets_end
9590 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9591 offsetp++;
b64f50a1
JK
9592 if (offsetp >= offsets_end
9593 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9594 {
9595 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9596 process_die (origin_child_die, origin_cu);
d389af10
JK
9597 }
9598 origin_child_die = sibling_die (origin_child_die);
9599 }
cd02d79d 9600 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9601
9602 do_cleanups (cleanups);
9603}
9604
c906108c 9605static void
e7c27a73 9606read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9607{
e7c27a73 9608 struct objfile *objfile = cu->objfile;
52f0bd74 9609 struct context_stack *new;
c906108c
SS
9610 CORE_ADDR lowpc;
9611 CORE_ADDR highpc;
9612 struct die_info *child_die;
edb3359d 9613 struct attribute *attr, *call_line, *call_file;
15d034d0 9614 const char *name;
e142c38c 9615 CORE_ADDR baseaddr;
801e3a5b 9616 struct block *block;
edb3359d 9617 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9618 VEC (symbolp) *template_args = NULL;
9619 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9620
9621 if (inlined_func)
9622 {
9623 /* If we do not have call site information, we can't show the
9624 caller of this inlined function. That's too confusing, so
9625 only use the scope for local variables. */
9626 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9627 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9628 if (call_line == NULL || call_file == NULL)
9629 {
9630 read_lexical_block_scope (die, cu);
9631 return;
9632 }
9633 }
c906108c 9634
e142c38c
DJ
9635 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9636
94af9270 9637 name = dwarf2_name (die, cu);
c906108c 9638
e8d05480
JB
9639 /* Ignore functions with missing or empty names. These are actually
9640 illegal according to the DWARF standard. */
9641 if (name == NULL)
9642 {
9643 complaint (&symfile_complaints,
b64f50a1
JK
9644 _("missing name for subprogram DIE at %d"),
9645 die->offset.sect_off);
e8d05480
JB
9646 return;
9647 }
9648
9649 /* Ignore functions with missing or invalid low and high pc attributes. */
9650 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9651 {
ae4d0c03
PM
9652 attr = dwarf2_attr (die, DW_AT_external, cu);
9653 if (!attr || !DW_UNSND (attr))
9654 complaint (&symfile_complaints,
3e43a32a
MS
9655 _("cannot get low and high bounds "
9656 "for subprogram DIE at %d"),
b64f50a1 9657 die->offset.sect_off);
e8d05480
JB
9658 return;
9659 }
c906108c
SS
9660
9661 lowpc += baseaddr;
9662 highpc += baseaddr;
9663
34eaf542
TT
9664 /* If we have any template arguments, then we must allocate a
9665 different sort of symbol. */
9666 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9667 {
9668 if (child_die->tag == DW_TAG_template_type_param
9669 || child_die->tag == DW_TAG_template_value_param)
9670 {
e623cf5d 9671 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
9672 templ_func->base.is_cplus_template_function = 1;
9673 break;
9674 }
9675 }
9676
c906108c 9677 new = push_context (0, lowpc);
34eaf542
TT
9678 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9679 (struct symbol *) templ_func);
4c2df51b 9680
4cecd739
DJ
9681 /* If there is a location expression for DW_AT_frame_base, record
9682 it. */
e142c38c 9683 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9684 if (attr)
f1e6e072 9685 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9686
e142c38c 9687 cu->list_in_scope = &local_symbols;
c906108c 9688
639d11d3 9689 if (die->child != NULL)
c906108c 9690 {
639d11d3 9691 child_die = die->child;
c906108c
SS
9692 while (child_die && child_die->tag)
9693 {
34eaf542
TT
9694 if (child_die->tag == DW_TAG_template_type_param
9695 || child_die->tag == DW_TAG_template_value_param)
9696 {
9697 struct symbol *arg = new_symbol (child_die, NULL, cu);
9698
f1078f66
DJ
9699 if (arg != NULL)
9700 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9701 }
9702 else
9703 process_die (child_die, cu);
c906108c
SS
9704 child_die = sibling_die (child_die);
9705 }
9706 }
9707
d389af10
JK
9708 inherit_abstract_dies (die, cu);
9709
4a811a97
UW
9710 /* If we have a DW_AT_specification, we might need to import using
9711 directives from the context of the specification DIE. See the
9712 comment in determine_prefix. */
9713 if (cu->language == language_cplus
9714 && dwarf2_attr (die, DW_AT_specification, cu))
9715 {
9716 struct dwarf2_cu *spec_cu = cu;
9717 struct die_info *spec_die = die_specification (die, &spec_cu);
9718
9719 while (spec_die)
9720 {
9721 child_die = spec_die->child;
9722 while (child_die && child_die->tag)
9723 {
9724 if (child_die->tag == DW_TAG_imported_module)
9725 process_die (child_die, spec_cu);
9726 child_die = sibling_die (child_die);
9727 }
9728
9729 /* In some cases, GCC generates specification DIEs that
9730 themselves contain DW_AT_specification attributes. */
9731 spec_die = die_specification (spec_die, &spec_cu);
9732 }
9733 }
9734
c906108c
SS
9735 new = pop_context ();
9736 /* Make a block for the local symbols within. */
801e3a5b
JB
9737 block = finish_block (new->name, &local_symbols, new->old_blocks,
9738 lowpc, highpc, objfile);
9739
df8a16a1 9740 /* For C++, set the block's scope. */
195a3f6c 9741 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9742 && cu->processing_has_namespace_info)
195a3f6c
TT
9743 block_set_scope (block, determine_prefix (die, cu),
9744 &objfile->objfile_obstack);
df8a16a1 9745
801e3a5b
JB
9746 /* If we have address ranges, record them. */
9747 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9748
34eaf542
TT
9749 /* Attach template arguments to function. */
9750 if (! VEC_empty (symbolp, template_args))
9751 {
9752 gdb_assert (templ_func != NULL);
9753
9754 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9755 templ_func->template_arguments
9756 = obstack_alloc (&objfile->objfile_obstack,
9757 (templ_func->n_template_arguments
9758 * sizeof (struct symbol *)));
9759 memcpy (templ_func->template_arguments,
9760 VEC_address (symbolp, template_args),
9761 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9762 VEC_free (symbolp, template_args);
9763 }
9764
208d8187
JB
9765 /* In C++, we can have functions nested inside functions (e.g., when
9766 a function declares a class that has methods). This means that
9767 when we finish processing a function scope, we may need to go
9768 back to building a containing block's symbol lists. */
9769 local_symbols = new->locals;
27aa8d6a 9770 using_directives = new->using_directives;
208d8187 9771
921e78cf
JB
9772 /* If we've finished processing a top-level function, subsequent
9773 symbols go in the file symbol list. */
9774 if (outermost_context_p ())
e142c38c 9775 cu->list_in_scope = &file_symbols;
c906108c
SS
9776}
9777
9778/* Process all the DIES contained within a lexical block scope. Start
9779 a new scope, process the dies, and then close the scope. */
9780
9781static void
e7c27a73 9782read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9783{
e7c27a73 9784 struct objfile *objfile = cu->objfile;
52f0bd74 9785 struct context_stack *new;
c906108c
SS
9786 CORE_ADDR lowpc, highpc;
9787 struct die_info *child_die;
e142c38c
DJ
9788 CORE_ADDR baseaddr;
9789
9790 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9791
9792 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9793 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9794 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9795 be nasty. Might be easier to properly extend generic blocks to
af34e669 9796 describe ranges. */
d85a05f0 9797 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9798 return;
9799 lowpc += baseaddr;
9800 highpc += baseaddr;
9801
9802 push_context (0, lowpc);
639d11d3 9803 if (die->child != NULL)
c906108c 9804 {
639d11d3 9805 child_die = die->child;
c906108c
SS
9806 while (child_die && child_die->tag)
9807 {
e7c27a73 9808 process_die (child_die, cu);
c906108c
SS
9809 child_die = sibling_die (child_die);
9810 }
9811 }
9812 new = pop_context ();
9813
8540c487 9814 if (local_symbols != NULL || using_directives != NULL)
c906108c 9815 {
801e3a5b
JB
9816 struct block *block
9817 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9818 highpc, objfile);
9819
9820 /* Note that recording ranges after traversing children, as we
9821 do here, means that recording a parent's ranges entails
9822 walking across all its children's ranges as they appear in
9823 the address map, which is quadratic behavior.
9824
9825 It would be nicer to record the parent's ranges before
9826 traversing its children, simply overriding whatever you find
9827 there. But since we don't even decide whether to create a
9828 block until after we've traversed its children, that's hard
9829 to do. */
9830 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9831 }
9832 local_symbols = new->locals;
27aa8d6a 9833 using_directives = new->using_directives;
c906108c
SS
9834}
9835
96408a79
SA
9836/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9837
9838static void
9839read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9840{
9841 struct objfile *objfile = cu->objfile;
9842 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9843 CORE_ADDR pc, baseaddr;
9844 struct attribute *attr;
9845 struct call_site *call_site, call_site_local;
9846 void **slot;
9847 int nparams;
9848 struct die_info *child_die;
9849
9850 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9851
9852 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9853 if (!attr)
9854 {
9855 complaint (&symfile_complaints,
9856 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9857 "DIE 0x%x [in module %s]"),
b64f50a1 9858 die->offset.sect_off, objfile->name);
96408a79
SA
9859 return;
9860 }
9861 pc = DW_ADDR (attr) + baseaddr;
9862
9863 if (cu->call_site_htab == NULL)
9864 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9865 NULL, &objfile->objfile_obstack,
9866 hashtab_obstack_allocate, NULL);
9867 call_site_local.pc = pc;
9868 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9869 if (*slot != NULL)
9870 {
9871 complaint (&symfile_complaints,
9872 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9873 "DIE 0x%x [in module %s]"),
b64f50a1 9874 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9875 return;
9876 }
9877
9878 /* Count parameters at the caller. */
9879
9880 nparams = 0;
9881 for (child_die = die->child; child_die && child_die->tag;
9882 child_die = sibling_die (child_die))
9883 {
9884 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9885 {
9886 complaint (&symfile_complaints,
9887 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9888 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9889 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9890 continue;
9891 }
9892
9893 nparams++;
9894 }
9895
9896 call_site = obstack_alloc (&objfile->objfile_obstack,
9897 (sizeof (*call_site)
9898 + (sizeof (*call_site->parameter)
9899 * (nparams - 1))));
9900 *slot = call_site;
9901 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9902 call_site->pc = pc;
9903
9904 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9905 {
9906 struct die_info *func_die;
9907
9908 /* Skip also over DW_TAG_inlined_subroutine. */
9909 for (func_die = die->parent;
9910 func_die && func_die->tag != DW_TAG_subprogram
9911 && func_die->tag != DW_TAG_subroutine_type;
9912 func_die = func_die->parent);
9913
9914 /* DW_AT_GNU_all_call_sites is a superset
9915 of DW_AT_GNU_all_tail_call_sites. */
9916 if (func_die
9917 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9918 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9919 {
9920 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9921 not complete. But keep CALL_SITE for look ups via call_site_htab,
9922 both the initial caller containing the real return address PC and
9923 the final callee containing the current PC of a chain of tail
9924 calls do not need to have the tail call list complete. But any
9925 function candidate for a virtual tail call frame searched via
9926 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9927 determined unambiguously. */
9928 }
9929 else
9930 {
9931 struct type *func_type = NULL;
9932
9933 if (func_die)
9934 func_type = get_die_type (func_die, cu);
9935 if (func_type != NULL)
9936 {
9937 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9938
9939 /* Enlist this call site to the function. */
9940 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9941 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9942 }
9943 else
9944 complaint (&symfile_complaints,
9945 _("Cannot find function owning DW_TAG_GNU_call_site "
9946 "DIE 0x%x [in module %s]"),
b64f50a1 9947 die->offset.sect_off, objfile->name);
96408a79
SA
9948 }
9949 }
9950
9951 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9952 if (attr == NULL)
9953 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9954 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9955 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9956 /* Keep NULL DWARF_BLOCK. */;
9957 else if (attr_form_is_block (attr))
9958 {
9959 struct dwarf2_locexpr_baton *dlbaton;
9960
9961 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9962 dlbaton->data = DW_BLOCK (attr)->data;
9963 dlbaton->size = DW_BLOCK (attr)->size;
9964 dlbaton->per_cu = cu->per_cu;
9965
9966 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9967 }
9968 else if (is_ref_attr (attr))
9969 {
96408a79
SA
9970 struct dwarf2_cu *target_cu = cu;
9971 struct die_info *target_die;
9972
ac9ec31b 9973 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
9974 gdb_assert (target_cu->objfile == objfile);
9975 if (die_is_declaration (target_die, target_cu))
9976 {
9112db09
JK
9977 const char *target_physname = NULL;
9978 struct attribute *target_attr;
9979
9980 /* Prefer the mangled name; otherwise compute the demangled one. */
9981 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9982 if (target_attr == NULL)
9983 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9984 target_cu);
9985 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9986 target_physname = DW_STRING (target_attr);
9987 else
9988 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9989 if (target_physname == NULL)
9990 complaint (&symfile_complaints,
9991 _("DW_AT_GNU_call_site_target target DIE has invalid "
9992 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9993 die->offset.sect_off, objfile->name);
96408a79 9994 else
7d455152 9995 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9996 }
9997 else
9998 {
9999 CORE_ADDR lowpc;
10000
10001 /* DW_AT_entry_pc should be preferred. */
10002 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10003 complaint (&symfile_complaints,
10004 _("DW_AT_GNU_call_site_target target DIE has invalid "
10005 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10006 die->offset.sect_off, objfile->name);
96408a79
SA
10007 else
10008 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10009 }
10010 }
10011 else
10012 complaint (&symfile_complaints,
10013 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10014 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10015 die->offset.sect_off, objfile->name);
96408a79
SA
10016
10017 call_site->per_cu = cu->per_cu;
10018
10019 for (child_die = die->child;
10020 child_die && child_die->tag;
10021 child_die = sibling_die (child_die))
10022 {
96408a79 10023 struct call_site_parameter *parameter;
1788b2d3 10024 struct attribute *loc, *origin;
96408a79
SA
10025
10026 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10027 {
10028 /* Already printed the complaint above. */
10029 continue;
10030 }
10031
10032 gdb_assert (call_site->parameter_count < nparams);
10033 parameter = &call_site->parameter[call_site->parameter_count];
10034
1788b2d3
JK
10035 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10036 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10037 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10038
24c5c679 10039 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
10040 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10041 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10042 {
10043 sect_offset offset;
10044
10045 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10046 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10047 if (!offset_in_cu_p (&cu->header, offset))
10048 {
10049 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10050 binding can be done only inside one CU. Such referenced DIE
10051 therefore cannot be even moved to DW_TAG_partial_unit. */
10052 complaint (&symfile_complaints,
10053 _("DW_AT_abstract_origin offset is not in CU for "
10054 "DW_TAG_GNU_call_site child DIE 0x%x "
10055 "[in module %s]"),
10056 child_die->offset.sect_off, objfile->name);
10057 continue;
10058 }
1788b2d3
JK
10059 parameter->u.param_offset.cu_off = (offset.sect_off
10060 - cu->header.offset.sect_off);
10061 }
10062 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10063 {
10064 complaint (&symfile_complaints,
10065 _("No DW_FORM_block* DW_AT_location for "
10066 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10067 child_die->offset.sect_off, objfile->name);
96408a79
SA
10068 continue;
10069 }
24c5c679 10070 else
96408a79 10071 {
24c5c679
JK
10072 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10073 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10074 if (parameter->u.dwarf_reg != -1)
10075 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10076 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10077 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10078 &parameter->u.fb_offset))
10079 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10080 else
10081 {
10082 complaint (&symfile_complaints,
10083 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10084 "for DW_FORM_block* DW_AT_location is supported for "
10085 "DW_TAG_GNU_call_site child DIE 0x%x "
10086 "[in module %s]"),
10087 child_die->offset.sect_off, objfile->name);
10088 continue;
10089 }
96408a79
SA
10090 }
10091
10092 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10093 if (!attr_form_is_block (attr))
10094 {
10095 complaint (&symfile_complaints,
10096 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10097 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10098 child_die->offset.sect_off, objfile->name);
96408a79
SA
10099 continue;
10100 }
10101 parameter->value = DW_BLOCK (attr)->data;
10102 parameter->value_size = DW_BLOCK (attr)->size;
10103
10104 /* Parameters are not pre-cleared by memset above. */
10105 parameter->data_value = NULL;
10106 parameter->data_value_size = 0;
10107 call_site->parameter_count++;
10108
10109 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10110 if (attr)
10111 {
10112 if (!attr_form_is_block (attr))
10113 complaint (&symfile_complaints,
10114 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10115 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10116 child_die->offset.sect_off, objfile->name);
96408a79
SA
10117 else
10118 {
10119 parameter->data_value = DW_BLOCK (attr)->data;
10120 parameter->data_value_size = DW_BLOCK (attr)->size;
10121 }
10122 }
10123 }
10124}
10125
43039443 10126/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10127 Return 1 if the attributes are present and valid, otherwise, return 0.
10128 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10129
10130static int
10131dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10132 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10133 struct partial_symtab *ranges_pst)
43039443
JK
10134{
10135 struct objfile *objfile = cu->objfile;
10136 struct comp_unit_head *cu_header = &cu->header;
10137 bfd *obfd = objfile->obfd;
10138 unsigned int addr_size = cu_header->addr_size;
10139 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10140 /* Base address selection entry. */
10141 CORE_ADDR base;
10142 int found_base;
10143 unsigned int dummy;
d521ce57 10144 const gdb_byte *buffer;
43039443
JK
10145 CORE_ADDR marker;
10146 int low_set;
10147 CORE_ADDR low = 0;
10148 CORE_ADDR high = 0;
ff013f42 10149 CORE_ADDR baseaddr;
43039443 10150
d00adf39
DE
10151 found_base = cu->base_known;
10152 base = cu->base_address;
43039443 10153
be391dca 10154 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10155 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10156 {
10157 complaint (&symfile_complaints,
10158 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10159 offset);
10160 return 0;
10161 }
dce234bc 10162 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10163
10164 /* Read in the largest possible address. */
10165 marker = read_address (obfd, buffer, cu, &dummy);
10166 if ((marker & mask) == mask)
10167 {
10168 /* If we found the largest possible address, then
10169 read the base address. */
10170 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10171 buffer += 2 * addr_size;
10172 offset += 2 * addr_size;
10173 found_base = 1;
10174 }
10175
10176 low_set = 0;
10177
e7030f15 10178 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10179
43039443
JK
10180 while (1)
10181 {
10182 CORE_ADDR range_beginning, range_end;
10183
10184 range_beginning = read_address (obfd, buffer, cu, &dummy);
10185 buffer += addr_size;
10186 range_end = read_address (obfd, buffer, cu, &dummy);
10187 buffer += addr_size;
10188 offset += 2 * addr_size;
10189
10190 /* An end of list marker is a pair of zero addresses. */
10191 if (range_beginning == 0 && range_end == 0)
10192 /* Found the end of list entry. */
10193 break;
10194
10195 /* Each base address selection entry is a pair of 2 values.
10196 The first is the largest possible address, the second is
10197 the base address. Check for a base address here. */
10198 if ((range_beginning & mask) == mask)
10199 {
10200 /* If we found the largest possible address, then
10201 read the base address. */
10202 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10203 found_base = 1;
10204 continue;
10205 }
10206
10207 if (!found_base)
10208 {
10209 /* We have no valid base address for the ranges
10210 data. */
10211 complaint (&symfile_complaints,
10212 _("Invalid .debug_ranges data (no base address)"));
10213 return 0;
10214 }
10215
9277c30c
UW
10216 if (range_beginning > range_end)
10217 {
10218 /* Inverted range entries are invalid. */
10219 complaint (&symfile_complaints,
10220 _("Invalid .debug_ranges data (inverted range)"));
10221 return 0;
10222 }
10223
10224 /* Empty range entries have no effect. */
10225 if (range_beginning == range_end)
10226 continue;
10227
43039443
JK
10228 range_beginning += base;
10229 range_end += base;
10230
01093045
DE
10231 /* A not-uncommon case of bad debug info.
10232 Don't pollute the addrmap with bad data. */
10233 if (range_beginning + baseaddr == 0
10234 && !dwarf2_per_objfile->has_section_at_zero)
10235 {
10236 complaint (&symfile_complaints,
10237 _(".debug_ranges entry has start address of zero"
10238 " [in module %s]"), objfile->name);
10239 continue;
10240 }
10241
9277c30c 10242 if (ranges_pst != NULL)
ff013f42 10243 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10244 range_beginning + baseaddr,
10245 range_end - 1 + baseaddr,
ff013f42
JK
10246 ranges_pst);
10247
43039443
JK
10248 /* FIXME: This is recording everything as a low-high
10249 segment of consecutive addresses. We should have a
10250 data structure for discontiguous block ranges
10251 instead. */
10252 if (! low_set)
10253 {
10254 low = range_beginning;
10255 high = range_end;
10256 low_set = 1;
10257 }
10258 else
10259 {
10260 if (range_beginning < low)
10261 low = range_beginning;
10262 if (range_end > high)
10263 high = range_end;
10264 }
10265 }
10266
10267 if (! low_set)
10268 /* If the first entry is an end-of-list marker, the range
10269 describes an empty scope, i.e. no instructions. */
10270 return 0;
10271
10272 if (low_return)
10273 *low_return = low;
10274 if (high_return)
10275 *high_return = high;
10276 return 1;
10277}
10278
af34e669
DJ
10279/* Get low and high pc attributes from a die. Return 1 if the attributes
10280 are present and valid, otherwise, return 0. Return -1 if the range is
10281 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10282
c906108c 10283static int
af34e669 10284dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10285 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10286 struct partial_symtab *pst)
c906108c
SS
10287{
10288 struct attribute *attr;
91da1414 10289 struct attribute *attr_high;
af34e669
DJ
10290 CORE_ADDR low = 0;
10291 CORE_ADDR high = 0;
10292 int ret = 0;
c906108c 10293
91da1414
MW
10294 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10295 if (attr_high)
af34e669 10296 {
e142c38c 10297 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10298 if (attr)
91da1414
MW
10299 {
10300 low = DW_ADDR (attr);
3019eac3
DE
10301 if (attr_high->form == DW_FORM_addr
10302 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10303 high = DW_ADDR (attr_high);
10304 else
10305 high = low + DW_UNSND (attr_high);
10306 }
af34e669
DJ
10307 else
10308 /* Found high w/o low attribute. */
10309 return 0;
10310
10311 /* Found consecutive range of addresses. */
10312 ret = 1;
10313 }
c906108c 10314 else
af34e669 10315 {
e142c38c 10316 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10317 if (attr != NULL)
10318 {
ab435259
DE
10319 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10320 We take advantage of the fact that DW_AT_ranges does not appear
10321 in DW_TAG_compile_unit of DWO files. */
10322 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10323 unsigned int ranges_offset = (DW_UNSND (attr)
10324 + (need_ranges_base
10325 ? cu->ranges_base
10326 : 0));
2e3cf129 10327
af34e669 10328 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10329 .debug_ranges section. */
2e3cf129 10330 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10331 return 0;
43039443 10332 /* Found discontinuous range of addresses. */
af34e669
DJ
10333 ret = -1;
10334 }
10335 }
c906108c 10336
9373cf26
JK
10337 /* read_partial_die has also the strict LOW < HIGH requirement. */
10338 if (high <= low)
c906108c
SS
10339 return 0;
10340
10341 /* When using the GNU linker, .gnu.linkonce. sections are used to
10342 eliminate duplicate copies of functions and vtables and such.
10343 The linker will arbitrarily choose one and discard the others.
10344 The AT_*_pc values for such functions refer to local labels in
10345 these sections. If the section from that file was discarded, the
10346 labels are not in the output, so the relocs get a value of 0.
10347 If this is a discarded function, mark the pc bounds as invalid,
10348 so that GDB will ignore it. */
72dca2f5 10349 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10350 return 0;
10351
10352 *lowpc = low;
96408a79
SA
10353 if (highpc)
10354 *highpc = high;
af34e669 10355 return ret;
c906108c
SS
10356}
10357
b084d499
JB
10358/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10359 its low and high PC addresses. Do nothing if these addresses could not
10360 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10361 and HIGHPC to the high address if greater than HIGHPC. */
10362
10363static void
10364dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10365 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10366 struct dwarf2_cu *cu)
10367{
10368 CORE_ADDR low, high;
10369 struct die_info *child = die->child;
10370
d85a05f0 10371 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10372 {
10373 *lowpc = min (*lowpc, low);
10374 *highpc = max (*highpc, high);
10375 }
10376
10377 /* If the language does not allow nested subprograms (either inside
10378 subprograms or lexical blocks), we're done. */
10379 if (cu->language != language_ada)
10380 return;
6e70227d 10381
b084d499
JB
10382 /* Check all the children of the given DIE. If it contains nested
10383 subprograms, then check their pc bounds. Likewise, we need to
10384 check lexical blocks as well, as they may also contain subprogram
10385 definitions. */
10386 while (child && child->tag)
10387 {
10388 if (child->tag == DW_TAG_subprogram
10389 || child->tag == DW_TAG_lexical_block)
10390 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10391 child = sibling_die (child);
10392 }
10393}
10394
fae299cd
DC
10395/* Get the low and high pc's represented by the scope DIE, and store
10396 them in *LOWPC and *HIGHPC. If the correct values can't be
10397 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10398
10399static void
10400get_scope_pc_bounds (struct die_info *die,
10401 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10402 struct dwarf2_cu *cu)
10403{
10404 CORE_ADDR best_low = (CORE_ADDR) -1;
10405 CORE_ADDR best_high = (CORE_ADDR) 0;
10406 CORE_ADDR current_low, current_high;
10407
d85a05f0 10408 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10409 {
10410 best_low = current_low;
10411 best_high = current_high;
10412 }
10413 else
10414 {
10415 struct die_info *child = die->child;
10416
10417 while (child && child->tag)
10418 {
10419 switch (child->tag) {
10420 case DW_TAG_subprogram:
b084d499 10421 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10422 break;
10423 case DW_TAG_namespace:
f55ee35c 10424 case DW_TAG_module:
fae299cd
DC
10425 /* FIXME: carlton/2004-01-16: Should we do this for
10426 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10427 that current GCC's always emit the DIEs corresponding
10428 to definitions of methods of classes as children of a
10429 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10430 the DIEs giving the declarations, which could be
10431 anywhere). But I don't see any reason why the
10432 standards says that they have to be there. */
10433 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10434
10435 if (current_low != ((CORE_ADDR) -1))
10436 {
10437 best_low = min (best_low, current_low);
10438 best_high = max (best_high, current_high);
10439 }
10440 break;
10441 default:
0963b4bd 10442 /* Ignore. */
fae299cd
DC
10443 break;
10444 }
10445
10446 child = sibling_die (child);
10447 }
10448 }
10449
10450 *lowpc = best_low;
10451 *highpc = best_high;
10452}
10453
801e3a5b
JB
10454/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10455 in DIE. */
380bca97 10456
801e3a5b
JB
10457static void
10458dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10459 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10460{
bb5ed363 10461 struct objfile *objfile = cu->objfile;
801e3a5b 10462 struct attribute *attr;
91da1414 10463 struct attribute *attr_high;
801e3a5b 10464
91da1414
MW
10465 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10466 if (attr_high)
801e3a5b 10467 {
801e3a5b
JB
10468 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10469 if (attr)
10470 {
10471 CORE_ADDR low = DW_ADDR (attr);
91da1414 10472 CORE_ADDR high;
3019eac3
DE
10473 if (attr_high->form == DW_FORM_addr
10474 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10475 high = DW_ADDR (attr_high);
10476 else
10477 high = low + DW_UNSND (attr_high);
9a619af0 10478
801e3a5b
JB
10479 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10480 }
10481 }
10482
10483 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10484 if (attr)
10485 {
bb5ed363 10486 bfd *obfd = objfile->obfd;
ab435259
DE
10487 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10488 We take advantage of the fact that DW_AT_ranges does not appear
10489 in DW_TAG_compile_unit of DWO files. */
10490 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10491
10492 /* The value of the DW_AT_ranges attribute is the offset of the
10493 address range list in the .debug_ranges section. */
ab435259
DE
10494 unsigned long offset = (DW_UNSND (attr)
10495 + (need_ranges_base ? cu->ranges_base : 0));
d521ce57 10496 const gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10497
10498 /* For some target architectures, but not others, the
10499 read_address function sign-extends the addresses it returns.
10500 To recognize base address selection entries, we need a
10501 mask. */
10502 unsigned int addr_size = cu->header.addr_size;
10503 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10504
10505 /* The base address, to which the next pair is relative. Note
10506 that this 'base' is a DWARF concept: most entries in a range
10507 list are relative, to reduce the number of relocs against the
10508 debugging information. This is separate from this function's
10509 'baseaddr' argument, which GDB uses to relocate debugging
10510 information from a shared library based on the address at
10511 which the library was loaded. */
d00adf39
DE
10512 CORE_ADDR base = cu->base_address;
10513 int base_known = cu->base_known;
801e3a5b 10514
be391dca 10515 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10516 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10517 {
10518 complaint (&symfile_complaints,
10519 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10520 offset);
10521 return;
10522 }
10523
10524 for (;;)
10525 {
10526 unsigned int bytes_read;
10527 CORE_ADDR start, end;
10528
10529 start = read_address (obfd, buffer, cu, &bytes_read);
10530 buffer += bytes_read;
10531 end = read_address (obfd, buffer, cu, &bytes_read);
10532 buffer += bytes_read;
10533
10534 /* Did we find the end of the range list? */
10535 if (start == 0 && end == 0)
10536 break;
10537
10538 /* Did we find a base address selection entry? */
10539 else if ((start & base_select_mask) == base_select_mask)
10540 {
10541 base = end;
10542 base_known = 1;
10543 }
10544
10545 /* We found an ordinary address range. */
10546 else
10547 {
10548 if (!base_known)
10549 {
10550 complaint (&symfile_complaints,
3e43a32a
MS
10551 _("Invalid .debug_ranges data "
10552 "(no base address)"));
801e3a5b
JB
10553 return;
10554 }
10555
9277c30c
UW
10556 if (start > end)
10557 {
10558 /* Inverted range entries are invalid. */
10559 complaint (&symfile_complaints,
10560 _("Invalid .debug_ranges data "
10561 "(inverted range)"));
10562 return;
10563 }
10564
10565 /* Empty range entries have no effect. */
10566 if (start == end)
10567 continue;
10568
01093045
DE
10569 start += base + baseaddr;
10570 end += base + baseaddr;
10571
10572 /* A not-uncommon case of bad debug info.
10573 Don't pollute the addrmap with bad data. */
10574 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10575 {
10576 complaint (&symfile_complaints,
10577 _(".debug_ranges entry has start address of zero"
10578 " [in module %s]"), objfile->name);
10579 continue;
10580 }
10581
10582 record_block_range (block, start, end - 1);
801e3a5b
JB
10583 }
10584 }
10585 }
10586}
10587
685b1105
JK
10588/* Check whether the producer field indicates either of GCC < 4.6, or the
10589 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10590
685b1105
JK
10591static void
10592check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10593{
10594 const char *cs;
10595 int major, minor, release;
10596
10597 if (cu->producer == NULL)
10598 {
10599 /* For unknown compilers expect their behavior is DWARF version
10600 compliant.
10601
10602 GCC started to support .debug_types sections by -gdwarf-4 since
10603 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10604 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10605 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10606 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10607 }
685b1105 10608 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10609 {
685b1105
JK
10610 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10611
ba919b58
TT
10612 cs = &cu->producer[strlen ("GNU ")];
10613 while (*cs && !isdigit (*cs))
10614 cs++;
10615 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10616 {
10617 /* Not recognized as GCC. */
10618 }
10619 else
1b80a9fa
JK
10620 {
10621 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10622 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10623 }
685b1105
JK
10624 }
10625 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10626 cu->producer_is_icc = 1;
10627 else
10628 {
10629 /* For other non-GCC compilers, expect their behavior is DWARF version
10630 compliant. */
60d5a603
JK
10631 }
10632
ba919b58 10633 cu->checked_producer = 1;
685b1105 10634}
ba919b58 10635
685b1105
JK
10636/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10637 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10638 during 4.6.0 experimental. */
10639
10640static int
10641producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10642{
10643 if (!cu->checked_producer)
10644 check_producer (cu);
10645
10646 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10647}
10648
10649/* Return the default accessibility type if it is not overriden by
10650 DW_AT_accessibility. */
10651
10652static enum dwarf_access_attribute
10653dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10654{
10655 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10656 {
10657 /* The default DWARF 2 accessibility for members is public, the default
10658 accessibility for inheritance is private. */
10659
10660 if (die->tag != DW_TAG_inheritance)
10661 return DW_ACCESS_public;
10662 else
10663 return DW_ACCESS_private;
10664 }
10665 else
10666 {
10667 /* DWARF 3+ defines the default accessibility a different way. The same
10668 rules apply now for DW_TAG_inheritance as for the members and it only
10669 depends on the container kind. */
10670
10671 if (die->parent->tag == DW_TAG_class_type)
10672 return DW_ACCESS_private;
10673 else
10674 return DW_ACCESS_public;
10675 }
10676}
10677
74ac6d43
TT
10678/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10679 offset. If the attribute was not found return 0, otherwise return
10680 1. If it was found but could not properly be handled, set *OFFSET
10681 to 0. */
10682
10683static int
10684handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10685 LONGEST *offset)
10686{
10687 struct attribute *attr;
10688
10689 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10690 if (attr != NULL)
10691 {
10692 *offset = 0;
10693
10694 /* Note that we do not check for a section offset first here.
10695 This is because DW_AT_data_member_location is new in DWARF 4,
10696 so if we see it, we can assume that a constant form is really
10697 a constant and not a section offset. */
10698 if (attr_form_is_constant (attr))
10699 *offset = dwarf2_get_attr_constant_value (attr, 0);
10700 else if (attr_form_is_section_offset (attr))
10701 dwarf2_complex_location_expr_complaint ();
10702 else if (attr_form_is_block (attr))
10703 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10704 else
10705 dwarf2_complex_location_expr_complaint ();
10706
10707 return 1;
10708 }
10709
10710 return 0;
10711}
10712
c906108c
SS
10713/* Add an aggregate field to the field list. */
10714
10715static void
107d2387 10716dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10717 struct dwarf2_cu *cu)
6e70227d 10718{
e7c27a73 10719 struct objfile *objfile = cu->objfile;
5e2b427d 10720 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10721 struct nextfield *new_field;
10722 struct attribute *attr;
10723 struct field *fp;
15d034d0 10724 const char *fieldname = "";
c906108c
SS
10725
10726 /* Allocate a new field list entry and link it in. */
10727 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10728 make_cleanup (xfree, new_field);
c906108c 10729 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10730
10731 if (die->tag == DW_TAG_inheritance)
10732 {
10733 new_field->next = fip->baseclasses;
10734 fip->baseclasses = new_field;
10735 }
10736 else
10737 {
10738 new_field->next = fip->fields;
10739 fip->fields = new_field;
10740 }
c906108c
SS
10741 fip->nfields++;
10742
e142c38c 10743 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10744 if (attr)
10745 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10746 else
10747 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10748 if (new_field->accessibility != DW_ACCESS_public)
10749 fip->non_public_fields = 1;
60d5a603 10750
e142c38c 10751 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10752 if (attr)
10753 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10754 else
10755 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10756
10757 fp = &new_field->field;
a9a9bd0f 10758
e142c38c 10759 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10760 {
74ac6d43
TT
10761 LONGEST offset;
10762
a9a9bd0f 10763 /* Data member other than a C++ static data member. */
6e70227d 10764
c906108c 10765 /* Get type of field. */
e7c27a73 10766 fp->type = die_type (die, cu);
c906108c 10767
d6a843b5 10768 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10769
c906108c 10770 /* Get bit size of field (zero if none). */
e142c38c 10771 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10772 if (attr)
10773 {
10774 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10775 }
10776 else
10777 {
10778 FIELD_BITSIZE (*fp) = 0;
10779 }
10780
10781 /* Get bit offset of field. */
74ac6d43
TT
10782 if (handle_data_member_location (die, cu, &offset))
10783 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10784 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10785 if (attr)
10786 {
5e2b427d 10787 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10788 {
10789 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10790 additional bit offset from the MSB of the containing
10791 anonymous object to the MSB of the field. We don't
10792 have to do anything special since we don't need to
10793 know the size of the anonymous object. */
f41f5e61 10794 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10795 }
10796 else
10797 {
10798 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10799 MSB of the anonymous object, subtract off the number of
10800 bits from the MSB of the field to the MSB of the
10801 object, and then subtract off the number of bits of
10802 the field itself. The result is the bit offset of
10803 the LSB of the field. */
c906108c
SS
10804 int anonymous_size;
10805 int bit_offset = DW_UNSND (attr);
10806
e142c38c 10807 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10808 if (attr)
10809 {
10810 /* The size of the anonymous object containing
10811 the bit field is explicit, so use the
10812 indicated size (in bytes). */
10813 anonymous_size = DW_UNSND (attr);
10814 }
10815 else
10816 {
10817 /* The size of the anonymous object containing
10818 the bit field must be inferred from the type
10819 attribute of the data member containing the
10820 bit field. */
10821 anonymous_size = TYPE_LENGTH (fp->type);
10822 }
f41f5e61
PA
10823 SET_FIELD_BITPOS (*fp,
10824 (FIELD_BITPOS (*fp)
10825 + anonymous_size * bits_per_byte
10826 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10827 }
10828 }
10829
10830 /* Get name of field. */
39cbfefa
DJ
10831 fieldname = dwarf2_name (die, cu);
10832 if (fieldname == NULL)
10833 fieldname = "";
d8151005
DJ
10834
10835 /* The name is already allocated along with this objfile, so we don't
10836 need to duplicate it for the type. */
10837 fp->name = fieldname;
c906108c
SS
10838
10839 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10840 pointer or virtual base class pointer) to private. */
e142c38c 10841 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10842 {
d48cc9dd 10843 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10844 new_field->accessibility = DW_ACCESS_private;
10845 fip->non_public_fields = 1;
10846 }
10847 }
a9a9bd0f 10848 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10849 {
a9a9bd0f
DC
10850 /* C++ static member. */
10851
10852 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10853 is a declaration, but all versions of G++ as of this writing
10854 (so through at least 3.2.1) incorrectly generate
10855 DW_TAG_variable tags. */
6e70227d 10856
ff355380 10857 const char *physname;
c906108c 10858
a9a9bd0f 10859 /* Get name of field. */
39cbfefa
DJ
10860 fieldname = dwarf2_name (die, cu);
10861 if (fieldname == NULL)
c906108c
SS
10862 return;
10863
254e6b9e 10864 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10865 if (attr
10866 /* Only create a symbol if this is an external value.
10867 new_symbol checks this and puts the value in the global symbol
10868 table, which we want. If it is not external, new_symbol
10869 will try to put the value in cu->list_in_scope which is wrong. */
10870 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10871 {
10872 /* A static const member, not much different than an enum as far as
10873 we're concerned, except that we can support more types. */
10874 new_symbol (die, NULL, cu);
10875 }
10876
2df3850c 10877 /* Get physical name. */
ff355380 10878 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10879
d8151005
DJ
10880 /* The name is already allocated along with this objfile, so we don't
10881 need to duplicate it for the type. */
10882 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10883 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10884 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10885 }
10886 else if (die->tag == DW_TAG_inheritance)
10887 {
74ac6d43 10888 LONGEST offset;
d4b96c9a 10889
74ac6d43
TT
10890 /* C++ base class field. */
10891 if (handle_data_member_location (die, cu, &offset))
10892 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10893 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10894 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10895 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10896 fip->nbaseclasses++;
10897 }
10898}
10899
98751a41
JK
10900/* Add a typedef defined in the scope of the FIP's class. */
10901
10902static void
10903dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10904 struct dwarf2_cu *cu)
6e70227d 10905{
98751a41 10906 struct objfile *objfile = cu->objfile;
98751a41
JK
10907 struct typedef_field_list *new_field;
10908 struct attribute *attr;
10909 struct typedef_field *fp;
10910 char *fieldname = "";
10911
10912 /* Allocate a new field list entry and link it in. */
10913 new_field = xzalloc (sizeof (*new_field));
10914 make_cleanup (xfree, new_field);
10915
10916 gdb_assert (die->tag == DW_TAG_typedef);
10917
10918 fp = &new_field->field;
10919
10920 /* Get name of field. */
10921 fp->name = dwarf2_name (die, cu);
10922 if (fp->name == NULL)
10923 return;
10924
10925 fp->type = read_type_die (die, cu);
10926
10927 new_field->next = fip->typedef_field_list;
10928 fip->typedef_field_list = new_field;
10929 fip->typedef_field_list_count++;
10930}
10931
c906108c
SS
10932/* Create the vector of fields, and attach it to the type. */
10933
10934static void
fba45db2 10935dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10936 struct dwarf2_cu *cu)
c906108c
SS
10937{
10938 int nfields = fip->nfields;
10939
10940 /* Record the field count, allocate space for the array of fields,
10941 and create blank accessibility bitfields if necessary. */
10942 TYPE_NFIELDS (type) = nfields;
10943 TYPE_FIELDS (type) = (struct field *)
10944 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10945 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10946
b4ba55a1 10947 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10948 {
10949 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10950
10951 TYPE_FIELD_PRIVATE_BITS (type) =
10952 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10953 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10954
10955 TYPE_FIELD_PROTECTED_BITS (type) =
10956 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10957 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10958
774b6a14
TT
10959 TYPE_FIELD_IGNORE_BITS (type) =
10960 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10961 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10962 }
10963
10964 /* If the type has baseclasses, allocate and clear a bit vector for
10965 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10966 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10967 {
10968 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10969 unsigned char *pointer;
c906108c
SS
10970
10971 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10972 pointer = TYPE_ALLOC (type, num_bytes);
10973 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10974 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10975 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10976 }
10977
3e43a32a
MS
10978 /* Copy the saved-up fields into the field vector. Start from the head of
10979 the list, adding to the tail of the field array, so that they end up in
10980 the same order in the array in which they were added to the list. */
c906108c
SS
10981 while (nfields-- > 0)
10982 {
7d0ccb61
DJ
10983 struct nextfield *fieldp;
10984
10985 if (fip->fields)
10986 {
10987 fieldp = fip->fields;
10988 fip->fields = fieldp->next;
10989 }
10990 else
10991 {
10992 fieldp = fip->baseclasses;
10993 fip->baseclasses = fieldp->next;
10994 }
10995
10996 TYPE_FIELD (type, nfields) = fieldp->field;
10997 switch (fieldp->accessibility)
c906108c 10998 {
c5aa993b 10999 case DW_ACCESS_private:
b4ba55a1
JB
11000 if (cu->language != language_ada)
11001 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11002 break;
c906108c 11003
c5aa993b 11004 case DW_ACCESS_protected:
b4ba55a1
JB
11005 if (cu->language != language_ada)
11006 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11007 break;
c906108c 11008
c5aa993b
JM
11009 case DW_ACCESS_public:
11010 break;
c906108c 11011
c5aa993b
JM
11012 default:
11013 /* Unknown accessibility. Complain and treat it as public. */
11014 {
e2e0b3e5 11015 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11016 fieldp->accessibility);
c5aa993b
JM
11017 }
11018 break;
c906108c
SS
11019 }
11020 if (nfields < fip->nbaseclasses)
11021 {
7d0ccb61 11022 switch (fieldp->virtuality)
c906108c 11023 {
c5aa993b
JM
11024 case DW_VIRTUALITY_virtual:
11025 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11026 if (cu->language == language_ada)
a73c6dcd 11027 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11028 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11029 break;
c906108c
SS
11030 }
11031 }
c906108c
SS
11032 }
11033}
11034
7d27a96d
TT
11035/* Return true if this member function is a constructor, false
11036 otherwise. */
11037
11038static int
11039dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11040{
11041 const char *fieldname;
11042 const char *typename;
11043 int len;
11044
11045 if (die->parent == NULL)
11046 return 0;
11047
11048 if (die->parent->tag != DW_TAG_structure_type
11049 && die->parent->tag != DW_TAG_union_type
11050 && die->parent->tag != DW_TAG_class_type)
11051 return 0;
11052
11053 fieldname = dwarf2_name (die, cu);
11054 typename = dwarf2_name (die->parent, cu);
11055 if (fieldname == NULL || typename == NULL)
11056 return 0;
11057
11058 len = strlen (fieldname);
11059 return (strncmp (fieldname, typename, len) == 0
11060 && (typename[len] == '\0' || typename[len] == '<'));
11061}
11062
c906108c
SS
11063/* Add a member function to the proper fieldlist. */
11064
11065static void
107d2387 11066dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11067 struct type *type, struct dwarf2_cu *cu)
c906108c 11068{
e7c27a73 11069 struct objfile *objfile = cu->objfile;
c906108c
SS
11070 struct attribute *attr;
11071 struct fnfieldlist *flp;
11072 int i;
11073 struct fn_field *fnp;
15d034d0 11074 const char *fieldname;
c906108c 11075 struct nextfnfield *new_fnfield;
f792889a 11076 struct type *this_type;
60d5a603 11077 enum dwarf_access_attribute accessibility;
c906108c 11078
b4ba55a1 11079 if (cu->language == language_ada)
a73c6dcd 11080 error (_("unexpected member function in Ada type"));
b4ba55a1 11081
2df3850c 11082 /* Get name of member function. */
39cbfefa
DJ
11083 fieldname = dwarf2_name (die, cu);
11084 if (fieldname == NULL)
2df3850c 11085 return;
c906108c 11086
c906108c
SS
11087 /* Look up member function name in fieldlist. */
11088 for (i = 0; i < fip->nfnfields; i++)
11089 {
27bfe10e 11090 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11091 break;
11092 }
11093
11094 /* Create new list element if necessary. */
11095 if (i < fip->nfnfields)
11096 flp = &fip->fnfieldlists[i];
11097 else
11098 {
11099 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11100 {
11101 fip->fnfieldlists = (struct fnfieldlist *)
11102 xrealloc (fip->fnfieldlists,
11103 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11104 * sizeof (struct fnfieldlist));
c906108c 11105 if (fip->nfnfields == 0)
c13c43fd 11106 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11107 }
11108 flp = &fip->fnfieldlists[fip->nfnfields];
11109 flp->name = fieldname;
11110 flp->length = 0;
11111 flp->head = NULL;
3da10d80 11112 i = fip->nfnfields++;
c906108c
SS
11113 }
11114
11115 /* Create a new member function field and chain it to the field list
0963b4bd 11116 entry. */
c906108c 11117 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11118 make_cleanup (xfree, new_fnfield);
c906108c
SS
11119 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11120 new_fnfield->next = flp->head;
11121 flp->head = new_fnfield;
11122 flp->length++;
11123
11124 /* Fill in the member function field info. */
11125 fnp = &new_fnfield->fnfield;
3da10d80
KS
11126
11127 /* Delay processing of the physname until later. */
11128 if (cu->language == language_cplus || cu->language == language_java)
11129 {
11130 add_to_method_list (type, i, flp->length - 1, fieldname,
11131 die, cu);
11132 }
11133 else
11134 {
1d06ead6 11135 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11136 fnp->physname = physname ? physname : "";
11137 }
11138
c906108c 11139 fnp->type = alloc_type (objfile);
f792889a
DJ
11140 this_type = read_type_die (die, cu);
11141 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11142 {
f792889a 11143 int nparams = TYPE_NFIELDS (this_type);
c906108c 11144
f792889a 11145 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11146 of the method itself (TYPE_CODE_METHOD). */
11147 smash_to_method_type (fnp->type, type,
f792889a
DJ
11148 TYPE_TARGET_TYPE (this_type),
11149 TYPE_FIELDS (this_type),
11150 TYPE_NFIELDS (this_type),
11151 TYPE_VARARGS (this_type));
c906108c
SS
11152
11153 /* Handle static member functions.
c5aa993b 11154 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11155 member functions. G++ helps GDB by marking the first
11156 parameter for non-static member functions (which is the this
11157 pointer) as artificial. We obtain this information from
11158 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11159 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11160 fnp->voffset = VOFFSET_STATIC;
11161 }
11162 else
e2e0b3e5 11163 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11164 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11165
11166 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11167 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11168 fnp->fcontext = die_containing_type (die, cu);
c906108c 11169
3e43a32a
MS
11170 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11171 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11172
11173 /* Get accessibility. */
e142c38c 11174 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11175 if (attr)
60d5a603
JK
11176 accessibility = DW_UNSND (attr);
11177 else
11178 accessibility = dwarf2_default_access_attribute (die, cu);
11179 switch (accessibility)
c906108c 11180 {
60d5a603
JK
11181 case DW_ACCESS_private:
11182 fnp->is_private = 1;
11183 break;
11184 case DW_ACCESS_protected:
11185 fnp->is_protected = 1;
11186 break;
c906108c
SS
11187 }
11188
b02dede2 11189 /* Check for artificial methods. */
e142c38c 11190 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11191 if (attr && DW_UNSND (attr) != 0)
11192 fnp->is_artificial = 1;
11193
7d27a96d
TT
11194 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11195
0d564a31 11196 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11197 function. For older versions of GCC, this is an offset in the
11198 appropriate virtual table, as specified by DW_AT_containing_type.
11199 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11200 to the object address. */
11201
e142c38c 11202 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11203 if (attr)
8e19ed76 11204 {
aec5aa8b 11205 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11206 {
aec5aa8b
TT
11207 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11208 {
11209 /* Old-style GCC. */
11210 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11211 }
11212 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11213 || (DW_BLOCK (attr)->size > 1
11214 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11215 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11216 {
11217 struct dwarf_block blk;
11218 int offset;
11219
11220 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11221 ? 1 : 2);
11222 blk.size = DW_BLOCK (attr)->size - offset;
11223 blk.data = DW_BLOCK (attr)->data + offset;
11224 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11225 if ((fnp->voffset % cu->header.addr_size) != 0)
11226 dwarf2_complex_location_expr_complaint ();
11227 else
11228 fnp->voffset /= cu->header.addr_size;
11229 fnp->voffset += 2;
11230 }
11231 else
11232 dwarf2_complex_location_expr_complaint ();
11233
11234 if (!fnp->fcontext)
11235 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11236 }
3690dd37 11237 else if (attr_form_is_section_offset (attr))
8e19ed76 11238 {
4d3c2250 11239 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11240 }
11241 else
11242 {
4d3c2250
KB
11243 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11244 fieldname);
8e19ed76 11245 }
0d564a31 11246 }
d48cc9dd
DJ
11247 else
11248 {
11249 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11250 if (attr && DW_UNSND (attr))
11251 {
11252 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11253 complaint (&symfile_complaints,
3e43a32a
MS
11254 _("Member function \"%s\" (offset %d) is virtual "
11255 "but the vtable offset is not specified"),
b64f50a1 11256 fieldname, die->offset.sect_off);
9655fd1a 11257 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11258 TYPE_CPLUS_DYNAMIC (type) = 1;
11259 }
11260 }
c906108c
SS
11261}
11262
11263/* Create the vector of member function fields, and attach it to the type. */
11264
11265static void
fba45db2 11266dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11267 struct dwarf2_cu *cu)
c906108c
SS
11268{
11269 struct fnfieldlist *flp;
c906108c
SS
11270 int i;
11271
b4ba55a1 11272 if (cu->language == language_ada)
a73c6dcd 11273 error (_("unexpected member functions in Ada type"));
b4ba55a1 11274
c906108c
SS
11275 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11276 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11277 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11278
11279 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11280 {
11281 struct nextfnfield *nfp = flp->head;
11282 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11283 int k;
11284
11285 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11286 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11287 fn_flp->fn_fields = (struct fn_field *)
11288 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11289 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11290 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11291 }
11292
11293 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11294}
11295
1168df01
JB
11296/* Returns non-zero if NAME is the name of a vtable member in CU's
11297 language, zero otherwise. */
11298static int
11299is_vtable_name (const char *name, struct dwarf2_cu *cu)
11300{
11301 static const char vptr[] = "_vptr";
987504bb 11302 static const char vtable[] = "vtable";
1168df01 11303
987504bb
JJ
11304 /* Look for the C++ and Java forms of the vtable. */
11305 if ((cu->language == language_java
11306 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11307 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11308 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11309 return 1;
11310
11311 return 0;
11312}
11313
c0dd20ea 11314/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11315 functions, with the ABI-specified layout. If TYPE describes
11316 such a structure, smash it into a member function type.
61049d3b
DJ
11317
11318 GCC shouldn't do this; it should just output pointer to member DIEs.
11319 This is GCC PR debug/28767. */
c0dd20ea 11320
0b92b5bb
TT
11321static void
11322quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11323{
0b92b5bb 11324 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11325
11326 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11327 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11328 return;
c0dd20ea
DJ
11329
11330 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11331 if (TYPE_FIELD_NAME (type, 0) == NULL
11332 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11333 || TYPE_FIELD_NAME (type, 1) == NULL
11334 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11335 return;
c0dd20ea
DJ
11336
11337 /* Find the type of the method. */
0b92b5bb 11338 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11339 if (pfn_type == NULL
11340 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11341 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11342 return;
c0dd20ea
DJ
11343
11344 /* Look for the "this" argument. */
11345 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11346 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11347 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11348 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11349 return;
c0dd20ea
DJ
11350
11351 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11352 new_type = alloc_type (objfile);
11353 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11354 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11355 TYPE_VARARGS (pfn_type));
0b92b5bb 11356 smash_to_methodptr_type (type, new_type);
c0dd20ea 11357}
1168df01 11358
685b1105
JK
11359/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11360 (icc). */
11361
11362static int
11363producer_is_icc (struct dwarf2_cu *cu)
11364{
11365 if (!cu->checked_producer)
11366 check_producer (cu);
11367
11368 return cu->producer_is_icc;
11369}
11370
c906108c 11371/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11372 (definition) to create a type for the structure or union. Fill in
11373 the type's name and general properties; the members will not be
3d1d5ea3 11374 processed until process_structure_scope.
c906108c 11375
c767944b
DJ
11376 NOTE: we need to call these functions regardless of whether or not the
11377 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11378 structure or union. This gets the type entered into our set of
11379 user defined types.
11380
11381 However, if the structure is incomplete (an opaque struct/union)
11382 then suppress creating a symbol table entry for it since gdb only
11383 wants to find the one with the complete definition. Note that if
11384 it is complete, we just call new_symbol, which does it's own
11385 checking about whether the struct/union is anonymous or not (and
11386 suppresses creating a symbol table entry itself). */
11387
f792889a 11388static struct type *
134d01f1 11389read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11390{
e7c27a73 11391 struct objfile *objfile = cu->objfile;
c906108c
SS
11392 struct type *type;
11393 struct attribute *attr;
15d034d0 11394 const char *name;
c906108c 11395
348e048f
DE
11396 /* If the definition of this type lives in .debug_types, read that type.
11397 Don't follow DW_AT_specification though, that will take us back up
11398 the chain and we want to go down. */
45e58e77 11399 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11400 if (attr)
11401 {
ac9ec31b 11402 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11403
ac9ec31b 11404 /* The type's CU may not be the same as CU.
02142a6c 11405 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11406 return set_die_type (die, type, cu);
11407 }
11408
c0dd20ea 11409 type = alloc_type (objfile);
c906108c 11410 INIT_CPLUS_SPECIFIC (type);
93311388 11411
39cbfefa
DJ
11412 name = dwarf2_name (die, cu);
11413 if (name != NULL)
c906108c 11414 {
987504bb
JJ
11415 if (cu->language == language_cplus
11416 || cu->language == language_java)
63d06c5c 11417 {
15d034d0 11418 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11419
11420 /* dwarf2_full_name might have already finished building the DIE's
11421 type. If so, there is no need to continue. */
11422 if (get_die_type (die, cu) != NULL)
11423 return get_die_type (die, cu);
11424
11425 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11426 if (die->tag == DW_TAG_structure_type
11427 || die->tag == DW_TAG_class_type)
11428 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11429 }
11430 else
11431 {
d8151005
DJ
11432 /* The name is already allocated along with this objfile, so
11433 we don't need to duplicate it for the type. */
7d455152 11434 TYPE_TAG_NAME (type) = name;
94af9270
KS
11435 if (die->tag == DW_TAG_class_type)
11436 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11437 }
c906108c
SS
11438 }
11439
11440 if (die->tag == DW_TAG_structure_type)
11441 {
11442 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11443 }
11444 else if (die->tag == DW_TAG_union_type)
11445 {
11446 TYPE_CODE (type) = TYPE_CODE_UNION;
11447 }
11448 else
11449 {
c906108c
SS
11450 TYPE_CODE (type) = TYPE_CODE_CLASS;
11451 }
11452
0cc2414c
TT
11453 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11454 TYPE_DECLARED_CLASS (type) = 1;
11455
e142c38c 11456 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11457 if (attr)
11458 {
11459 TYPE_LENGTH (type) = DW_UNSND (attr);
11460 }
11461 else
11462 {
11463 TYPE_LENGTH (type) = 0;
11464 }
11465
685b1105
JK
11466 if (producer_is_icc (cu))
11467 {
11468 /* ICC does not output the required DW_AT_declaration
11469 on incomplete types, but gives them a size of zero. */
11470 }
11471 else
11472 TYPE_STUB_SUPPORTED (type) = 1;
11473
dc718098 11474 if (die_is_declaration (die, cu))
876cecd0 11475 TYPE_STUB (type) = 1;
a6c727b2
DJ
11476 else if (attr == NULL && die->child == NULL
11477 && producer_is_realview (cu->producer))
11478 /* RealView does not output the required DW_AT_declaration
11479 on incomplete types. */
11480 TYPE_STUB (type) = 1;
dc718098 11481
c906108c
SS
11482 /* We need to add the type field to the die immediately so we don't
11483 infinitely recurse when dealing with pointers to the structure
0963b4bd 11484 type within the structure itself. */
1c379e20 11485 set_die_type (die, type, cu);
c906108c 11486
7e314c57
JK
11487 /* set_die_type should be already done. */
11488 set_descriptive_type (type, die, cu);
11489
c767944b
DJ
11490 return type;
11491}
11492
11493/* Finish creating a structure or union type, including filling in
11494 its members and creating a symbol for it. */
11495
11496static void
11497process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11498{
11499 struct objfile *objfile = cu->objfile;
11500 struct die_info *child_die = die->child;
11501 struct type *type;
11502
11503 type = get_die_type (die, cu);
11504 if (type == NULL)
11505 type = read_structure_type (die, cu);
11506
e142c38c 11507 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11508 {
11509 struct field_info fi;
11510 struct die_info *child_die;
34eaf542 11511 VEC (symbolp) *template_args = NULL;
c767944b 11512 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11513
11514 memset (&fi, 0, sizeof (struct field_info));
11515
639d11d3 11516 child_die = die->child;
c906108c
SS
11517
11518 while (child_die && child_die->tag)
11519 {
a9a9bd0f
DC
11520 if (child_die->tag == DW_TAG_member
11521 || child_die->tag == DW_TAG_variable)
c906108c 11522 {
a9a9bd0f
DC
11523 /* NOTE: carlton/2002-11-05: A C++ static data member
11524 should be a DW_TAG_member that is a declaration, but
11525 all versions of G++ as of this writing (so through at
11526 least 3.2.1) incorrectly generate DW_TAG_variable
11527 tags for them instead. */
e7c27a73 11528 dwarf2_add_field (&fi, child_die, cu);
c906108c 11529 }
8713b1b1 11530 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11531 {
0963b4bd 11532 /* C++ member function. */
e7c27a73 11533 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11534 }
11535 else if (child_die->tag == DW_TAG_inheritance)
11536 {
11537 /* C++ base class field. */
e7c27a73 11538 dwarf2_add_field (&fi, child_die, cu);
c906108c 11539 }
98751a41
JK
11540 else if (child_die->tag == DW_TAG_typedef)
11541 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11542 else if (child_die->tag == DW_TAG_template_type_param
11543 || child_die->tag == DW_TAG_template_value_param)
11544 {
11545 struct symbol *arg = new_symbol (child_die, NULL, cu);
11546
f1078f66
DJ
11547 if (arg != NULL)
11548 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11549 }
11550
c906108c
SS
11551 child_die = sibling_die (child_die);
11552 }
11553
34eaf542
TT
11554 /* Attach template arguments to type. */
11555 if (! VEC_empty (symbolp, template_args))
11556 {
11557 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11558 TYPE_N_TEMPLATE_ARGUMENTS (type)
11559 = VEC_length (symbolp, template_args);
11560 TYPE_TEMPLATE_ARGUMENTS (type)
11561 = obstack_alloc (&objfile->objfile_obstack,
11562 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11563 * sizeof (struct symbol *)));
11564 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11565 VEC_address (symbolp, template_args),
11566 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11567 * sizeof (struct symbol *)));
11568 VEC_free (symbolp, template_args);
11569 }
11570
c906108c
SS
11571 /* Attach fields and member functions to the type. */
11572 if (fi.nfields)
e7c27a73 11573 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11574 if (fi.nfnfields)
11575 {
e7c27a73 11576 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11577
c5aa993b 11578 /* Get the type which refers to the base class (possibly this
c906108c 11579 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11580 class from the DW_AT_containing_type attribute. This use of
11581 DW_AT_containing_type is a GNU extension. */
c906108c 11582
e142c38c 11583 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11584 {
e7c27a73 11585 struct type *t = die_containing_type (die, cu);
c906108c
SS
11586
11587 TYPE_VPTR_BASETYPE (type) = t;
11588 if (type == t)
11589 {
c906108c
SS
11590 int i;
11591
11592 /* Our own class provides vtbl ptr. */
11593 for (i = TYPE_NFIELDS (t) - 1;
11594 i >= TYPE_N_BASECLASSES (t);
11595 --i)
11596 {
0d5cff50 11597 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11598
1168df01 11599 if (is_vtable_name (fieldname, cu))
c906108c
SS
11600 {
11601 TYPE_VPTR_FIELDNO (type) = i;
11602 break;
11603 }
11604 }
11605
11606 /* Complain if virtual function table field not found. */
11607 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11608 complaint (&symfile_complaints,
3e43a32a
MS
11609 _("virtual function table pointer "
11610 "not found when defining class '%s'"),
4d3c2250
KB
11611 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11612 "");
c906108c
SS
11613 }
11614 else
11615 {
11616 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11617 }
11618 }
f6235d4c
EZ
11619 else if (cu->producer
11620 && strncmp (cu->producer,
11621 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11622 {
11623 /* The IBM XLC compiler does not provide direct indication
11624 of the containing type, but the vtable pointer is
11625 always named __vfp. */
11626
11627 int i;
11628
11629 for (i = TYPE_NFIELDS (type) - 1;
11630 i >= TYPE_N_BASECLASSES (type);
11631 --i)
11632 {
11633 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11634 {
11635 TYPE_VPTR_FIELDNO (type) = i;
11636 TYPE_VPTR_BASETYPE (type) = type;
11637 break;
11638 }
11639 }
11640 }
c906108c 11641 }
98751a41
JK
11642
11643 /* Copy fi.typedef_field_list linked list elements content into the
11644 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11645 if (fi.typedef_field_list)
11646 {
11647 int i = fi.typedef_field_list_count;
11648
a0d7a4ff 11649 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11650 TYPE_TYPEDEF_FIELD_ARRAY (type)
11651 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11652 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11653
11654 /* Reverse the list order to keep the debug info elements order. */
11655 while (--i >= 0)
11656 {
11657 struct typedef_field *dest, *src;
6e70227d 11658
98751a41
JK
11659 dest = &TYPE_TYPEDEF_FIELD (type, i);
11660 src = &fi.typedef_field_list->field;
11661 fi.typedef_field_list = fi.typedef_field_list->next;
11662 *dest = *src;
11663 }
11664 }
c767944b
DJ
11665
11666 do_cleanups (back_to);
eb2a6f42
TT
11667
11668 if (HAVE_CPLUS_STRUCT (type))
11669 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11670 }
63d06c5c 11671
bb5ed363 11672 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11673
90aeadfc
DC
11674 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11675 snapshots) has been known to create a die giving a declaration
11676 for a class that has, as a child, a die giving a definition for a
11677 nested class. So we have to process our children even if the
11678 current die is a declaration. Normally, of course, a declaration
11679 won't have any children at all. */
134d01f1 11680
90aeadfc
DC
11681 while (child_die != NULL && child_die->tag)
11682 {
11683 if (child_die->tag == DW_TAG_member
11684 || child_die->tag == DW_TAG_variable
34eaf542
TT
11685 || child_die->tag == DW_TAG_inheritance
11686 || child_die->tag == DW_TAG_template_value_param
11687 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11688 {
90aeadfc 11689 /* Do nothing. */
134d01f1 11690 }
90aeadfc
DC
11691 else
11692 process_die (child_die, cu);
134d01f1 11693
90aeadfc 11694 child_die = sibling_die (child_die);
134d01f1
DJ
11695 }
11696
fa4028e9
JB
11697 /* Do not consider external references. According to the DWARF standard,
11698 these DIEs are identified by the fact that they have no byte_size
11699 attribute, and a declaration attribute. */
11700 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11701 || !die_is_declaration (die, cu))
c767944b 11702 new_symbol (die, type, cu);
134d01f1
DJ
11703}
11704
11705/* Given a DW_AT_enumeration_type die, set its type. We do not
11706 complete the type's fields yet, or create any symbols. */
c906108c 11707
f792889a 11708static struct type *
134d01f1 11709read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11710{
e7c27a73 11711 struct objfile *objfile = cu->objfile;
c906108c 11712 struct type *type;
c906108c 11713 struct attribute *attr;
0114d602 11714 const char *name;
134d01f1 11715
348e048f
DE
11716 /* If the definition of this type lives in .debug_types, read that type.
11717 Don't follow DW_AT_specification though, that will take us back up
11718 the chain and we want to go down. */
45e58e77 11719 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11720 if (attr)
11721 {
ac9ec31b 11722 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11723
ac9ec31b 11724 /* The type's CU may not be the same as CU.
02142a6c 11725 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11726 return set_die_type (die, type, cu);
11727 }
11728
c906108c
SS
11729 type = alloc_type (objfile);
11730
11731 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11732 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11733 if (name != NULL)
7d455152 11734 TYPE_TAG_NAME (type) = name;
c906108c 11735
e142c38c 11736 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11737 if (attr)
11738 {
11739 TYPE_LENGTH (type) = DW_UNSND (attr);
11740 }
11741 else
11742 {
11743 TYPE_LENGTH (type) = 0;
11744 }
11745
137033e9
JB
11746 /* The enumeration DIE can be incomplete. In Ada, any type can be
11747 declared as private in the package spec, and then defined only
11748 inside the package body. Such types are known as Taft Amendment
11749 Types. When another package uses such a type, an incomplete DIE
11750 may be generated by the compiler. */
02eb380e 11751 if (die_is_declaration (die, cu))
876cecd0 11752 TYPE_STUB (type) = 1;
02eb380e 11753
f792889a 11754 return set_die_type (die, type, cu);
134d01f1
DJ
11755}
11756
11757/* Given a pointer to a die which begins an enumeration, process all
11758 the dies that define the members of the enumeration, and create the
11759 symbol for the enumeration type.
11760
11761 NOTE: We reverse the order of the element list. */
11762
11763static void
11764process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11765{
f792889a 11766 struct type *this_type;
134d01f1 11767
f792889a
DJ
11768 this_type = get_die_type (die, cu);
11769 if (this_type == NULL)
11770 this_type = read_enumeration_type (die, cu);
9dc481d3 11771
639d11d3 11772 if (die->child != NULL)
c906108c 11773 {
9dc481d3
DE
11774 struct die_info *child_die;
11775 struct symbol *sym;
11776 struct field *fields = NULL;
11777 int num_fields = 0;
11778 int unsigned_enum = 1;
15d034d0 11779 const char *name;
cafec441
TT
11780 int flag_enum = 1;
11781 ULONGEST mask = 0;
9dc481d3 11782
639d11d3 11783 child_die = die->child;
c906108c
SS
11784 while (child_die && child_die->tag)
11785 {
11786 if (child_die->tag != DW_TAG_enumerator)
11787 {
e7c27a73 11788 process_die (child_die, cu);
c906108c
SS
11789 }
11790 else
11791 {
39cbfefa
DJ
11792 name = dwarf2_name (child_die, cu);
11793 if (name)
c906108c 11794 {
f792889a 11795 sym = new_symbol (child_die, this_type, cu);
c906108c 11796 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11797 {
11798 unsigned_enum = 0;
11799 flag_enum = 0;
11800 }
11801 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11802 flag_enum = 0;
11803 else
11804 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11805
11806 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11807 {
11808 fields = (struct field *)
11809 xrealloc (fields,
11810 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11811 * sizeof (struct field));
c906108c
SS
11812 }
11813
3567439c 11814 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11815 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11816 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11817 FIELD_BITSIZE (fields[num_fields]) = 0;
11818
11819 num_fields++;
11820 }
11821 }
11822
11823 child_die = sibling_die (child_die);
11824 }
11825
11826 if (num_fields)
11827 {
f792889a
DJ
11828 TYPE_NFIELDS (this_type) = num_fields;
11829 TYPE_FIELDS (this_type) = (struct field *)
11830 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11831 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11832 sizeof (struct field) * num_fields);
b8c9b27d 11833 xfree (fields);
c906108c
SS
11834 }
11835 if (unsigned_enum)
876cecd0 11836 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11837 if (flag_enum)
11838 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11839 }
134d01f1 11840
6c83ed52
TT
11841 /* If we are reading an enum from a .debug_types unit, and the enum
11842 is a declaration, and the enum is not the signatured type in the
11843 unit, then we do not want to add a symbol for it. Adding a
11844 symbol would in some cases obscure the true definition of the
11845 enum, giving users an incomplete type when the definition is
11846 actually available. Note that we do not want to do this for all
11847 enums which are just declarations, because C++0x allows forward
11848 enum declarations. */
3019eac3 11849 if (cu->per_cu->is_debug_types
6c83ed52
TT
11850 && die_is_declaration (die, cu))
11851 {
52dc124a 11852 struct signatured_type *sig_type;
6c83ed52 11853
c0f78cd4 11854 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
11855 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11856 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11857 return;
11858 }
11859
f792889a 11860 new_symbol (die, this_type, cu);
c906108c
SS
11861}
11862
11863/* Extract all information from a DW_TAG_array_type DIE and put it in
11864 the DIE's type field. For now, this only handles one dimensional
11865 arrays. */
11866
f792889a 11867static struct type *
e7c27a73 11868read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11869{
e7c27a73 11870 struct objfile *objfile = cu->objfile;
c906108c 11871 struct die_info *child_die;
7e314c57 11872 struct type *type;
c906108c
SS
11873 struct type *element_type, *range_type, *index_type;
11874 struct type **range_types = NULL;
11875 struct attribute *attr;
11876 int ndim = 0;
11877 struct cleanup *back_to;
15d034d0 11878 const char *name;
c906108c 11879
e7c27a73 11880 element_type = die_type (die, cu);
c906108c 11881
7e314c57
JK
11882 /* The die_type call above may have already set the type for this DIE. */
11883 type = get_die_type (die, cu);
11884 if (type)
11885 return type;
11886
c906108c
SS
11887 /* Irix 6.2 native cc creates array types without children for
11888 arrays with unspecified length. */
639d11d3 11889 if (die->child == NULL)
c906108c 11890 {
46bf5051 11891 index_type = objfile_type (objfile)->builtin_int;
c906108c 11892 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11893 type = create_array_type (NULL, element_type, range_type);
11894 return set_die_type (die, type, cu);
c906108c
SS
11895 }
11896
11897 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11898 child_die = die->child;
c906108c
SS
11899 while (child_die && child_die->tag)
11900 {
11901 if (child_die->tag == DW_TAG_subrange_type)
11902 {
f792889a 11903 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11904
f792889a 11905 if (child_type != NULL)
a02abb62 11906 {
0963b4bd
MS
11907 /* The range type was succesfully read. Save it for the
11908 array type creation. */
a02abb62
JB
11909 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11910 {
11911 range_types = (struct type **)
11912 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11913 * sizeof (struct type *));
11914 if (ndim == 0)
11915 make_cleanup (free_current_contents, &range_types);
11916 }
f792889a 11917 range_types[ndim++] = child_type;
a02abb62 11918 }
c906108c
SS
11919 }
11920 child_die = sibling_die (child_die);
11921 }
11922
11923 /* Dwarf2 dimensions are output from left to right, create the
11924 necessary array types in backwards order. */
7ca2d3a3 11925
c906108c 11926 type = element_type;
7ca2d3a3
DL
11927
11928 if (read_array_order (die, cu) == DW_ORD_col_major)
11929 {
11930 int i = 0;
9a619af0 11931
7ca2d3a3
DL
11932 while (i < ndim)
11933 type = create_array_type (NULL, type, range_types[i++]);
11934 }
11935 else
11936 {
11937 while (ndim-- > 0)
11938 type = create_array_type (NULL, type, range_types[ndim]);
11939 }
c906108c 11940
f5f8a009
EZ
11941 /* Understand Dwarf2 support for vector types (like they occur on
11942 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11943 array type. This is not part of the Dwarf2/3 standard yet, but a
11944 custom vendor extension. The main difference between a regular
11945 array and the vector variant is that vectors are passed by value
11946 to functions. */
e142c38c 11947 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11948 if (attr)
ea37ba09 11949 make_vector_type (type);
f5f8a009 11950
dbc98a8b
KW
11951 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11952 implementation may choose to implement triple vectors using this
11953 attribute. */
11954 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11955 if (attr)
11956 {
11957 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11958 TYPE_LENGTH (type) = DW_UNSND (attr);
11959 else
3e43a32a
MS
11960 complaint (&symfile_complaints,
11961 _("DW_AT_byte_size for array type smaller "
11962 "than the total size of elements"));
dbc98a8b
KW
11963 }
11964
39cbfefa
DJ
11965 name = dwarf2_name (die, cu);
11966 if (name)
11967 TYPE_NAME (type) = name;
6e70227d 11968
0963b4bd 11969 /* Install the type in the die. */
7e314c57
JK
11970 set_die_type (die, type, cu);
11971
11972 /* set_die_type should be already done. */
b4ba55a1
JB
11973 set_descriptive_type (type, die, cu);
11974
c906108c
SS
11975 do_cleanups (back_to);
11976
7e314c57 11977 return type;
c906108c
SS
11978}
11979
7ca2d3a3 11980static enum dwarf_array_dim_ordering
6e70227d 11981read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11982{
11983 struct attribute *attr;
11984
11985 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11986
11987 if (attr) return DW_SND (attr);
11988
0963b4bd
MS
11989 /* GNU F77 is a special case, as at 08/2004 array type info is the
11990 opposite order to the dwarf2 specification, but data is still
11991 laid out as per normal fortran.
7ca2d3a3 11992
0963b4bd
MS
11993 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11994 version checking. */
7ca2d3a3 11995
905e0470
PM
11996 if (cu->language == language_fortran
11997 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11998 {
11999 return DW_ORD_row_major;
12000 }
12001
6e70227d 12002 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12003 {
12004 case array_column_major:
12005 return DW_ORD_col_major;
12006 case array_row_major:
12007 default:
12008 return DW_ORD_row_major;
12009 };
12010}
12011
72019c9c 12012/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12013 the DIE's type field. */
72019c9c 12014
f792889a 12015static struct type *
72019c9c
GM
12016read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12017{
7e314c57
JK
12018 struct type *domain_type, *set_type;
12019 struct attribute *attr;
f792889a 12020
7e314c57
JK
12021 domain_type = die_type (die, cu);
12022
12023 /* The die_type call above may have already set the type for this DIE. */
12024 set_type = get_die_type (die, cu);
12025 if (set_type)
12026 return set_type;
12027
12028 set_type = create_set_type (NULL, domain_type);
12029
12030 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12031 if (attr)
12032 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12033
f792889a 12034 return set_die_type (die, set_type, cu);
72019c9c 12035}
7ca2d3a3 12036
0971de02
TT
12037/* A helper for read_common_block that creates a locexpr baton.
12038 SYM is the symbol which we are marking as computed.
12039 COMMON_DIE is the DIE for the common block.
12040 COMMON_LOC is the location expression attribute for the common
12041 block itself.
12042 MEMBER_LOC is the location expression attribute for the particular
12043 member of the common block that we are processing.
12044 CU is the CU from which the above come. */
12045
12046static void
12047mark_common_block_symbol_computed (struct symbol *sym,
12048 struct die_info *common_die,
12049 struct attribute *common_loc,
12050 struct attribute *member_loc,
12051 struct dwarf2_cu *cu)
12052{
12053 struct objfile *objfile = dwarf2_per_objfile->objfile;
12054 struct dwarf2_locexpr_baton *baton;
12055 gdb_byte *ptr;
12056 unsigned int cu_off;
12057 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12058 LONGEST offset = 0;
12059
12060 gdb_assert (common_loc && member_loc);
12061 gdb_assert (attr_form_is_block (common_loc));
12062 gdb_assert (attr_form_is_block (member_loc)
12063 || attr_form_is_constant (member_loc));
12064
12065 baton = obstack_alloc (&objfile->objfile_obstack,
12066 sizeof (struct dwarf2_locexpr_baton));
12067 baton->per_cu = cu->per_cu;
12068 gdb_assert (baton->per_cu);
12069
12070 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12071
12072 if (attr_form_is_constant (member_loc))
12073 {
12074 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12075 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12076 }
12077 else
12078 baton->size += DW_BLOCK (member_loc)->size;
12079
12080 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12081 baton->data = ptr;
12082
12083 *ptr++ = DW_OP_call4;
12084 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12085 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12086 ptr += 4;
12087
12088 if (attr_form_is_constant (member_loc))
12089 {
12090 *ptr++ = DW_OP_addr;
12091 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12092 ptr += cu->header.addr_size;
12093 }
12094 else
12095 {
12096 /* We have to copy the data here, because DW_OP_call4 will only
12097 use a DW_AT_location attribute. */
12098 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12099 ptr += DW_BLOCK (member_loc)->size;
12100 }
12101
12102 *ptr++ = DW_OP_plus;
12103 gdb_assert (ptr - baton->data == baton->size);
12104
0971de02 12105 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12106 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12107}
12108
4357ac6c
TT
12109/* Create appropriate locally-scoped variables for all the
12110 DW_TAG_common_block entries. Also create a struct common_block
12111 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12112 is used to sepate the common blocks name namespace from regular
12113 variable names. */
c906108c
SS
12114
12115static void
e7c27a73 12116read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12117{
0971de02
TT
12118 struct attribute *attr;
12119
12120 attr = dwarf2_attr (die, DW_AT_location, cu);
12121 if (attr)
12122 {
12123 /* Support the .debug_loc offsets. */
12124 if (attr_form_is_block (attr))
12125 {
12126 /* Ok. */
12127 }
12128 else if (attr_form_is_section_offset (attr))
12129 {
12130 dwarf2_complex_location_expr_complaint ();
12131 attr = NULL;
12132 }
12133 else
12134 {
12135 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12136 "common block member");
12137 attr = NULL;
12138 }
12139 }
12140
639d11d3 12141 if (die->child != NULL)
c906108c 12142 {
4357ac6c
TT
12143 struct objfile *objfile = cu->objfile;
12144 struct die_info *child_die;
12145 size_t n_entries = 0, size;
12146 struct common_block *common_block;
12147 struct symbol *sym;
74ac6d43 12148
4357ac6c
TT
12149 for (child_die = die->child;
12150 child_die && child_die->tag;
12151 child_die = sibling_die (child_die))
12152 ++n_entries;
12153
12154 size = (sizeof (struct common_block)
12155 + (n_entries - 1) * sizeof (struct symbol *));
12156 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12157 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12158 common_block->n_entries = 0;
12159
12160 for (child_die = die->child;
12161 child_die && child_die->tag;
12162 child_die = sibling_die (child_die))
12163 {
12164 /* Create the symbol in the DW_TAG_common_block block in the current
12165 symbol scope. */
e7c27a73 12166 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12167 if (sym != NULL)
12168 {
12169 struct attribute *member_loc;
12170
12171 common_block->contents[common_block->n_entries++] = sym;
12172
12173 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12174 cu);
12175 if (member_loc)
12176 {
12177 /* GDB has handled this for a long time, but it is
12178 not specified by DWARF. It seems to have been
12179 emitted by gfortran at least as recently as:
12180 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12181 complaint (&symfile_complaints,
12182 _("Variable in common block has "
12183 "DW_AT_data_member_location "
12184 "- DIE at 0x%x [in module %s]"),
12185 child_die->offset.sect_off, cu->objfile->name);
12186
12187 if (attr_form_is_section_offset (member_loc))
12188 dwarf2_complex_location_expr_complaint ();
12189 else if (attr_form_is_constant (member_loc)
12190 || attr_form_is_block (member_loc))
12191 {
12192 if (attr)
12193 mark_common_block_symbol_computed (sym, die, attr,
12194 member_loc, cu);
12195 }
12196 else
12197 dwarf2_complex_location_expr_complaint ();
12198 }
12199 }
c906108c 12200 }
4357ac6c
TT
12201
12202 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12203 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12204 }
12205}
12206
0114d602 12207/* Create a type for a C++ namespace. */
d9fa45fe 12208
0114d602
DJ
12209static struct type *
12210read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12211{
e7c27a73 12212 struct objfile *objfile = cu->objfile;
0114d602 12213 const char *previous_prefix, *name;
9219021c 12214 int is_anonymous;
0114d602
DJ
12215 struct type *type;
12216
12217 /* For extensions, reuse the type of the original namespace. */
12218 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12219 {
12220 struct die_info *ext_die;
12221 struct dwarf2_cu *ext_cu = cu;
9a619af0 12222
0114d602
DJ
12223 ext_die = dwarf2_extension (die, &ext_cu);
12224 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12225
12226 /* EXT_CU may not be the same as CU.
02142a6c 12227 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12228 return set_die_type (die, type, cu);
12229 }
9219021c 12230
e142c38c 12231 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12232
12233 /* Now build the name of the current namespace. */
12234
0114d602
DJ
12235 previous_prefix = determine_prefix (die, cu);
12236 if (previous_prefix[0] != '\0')
12237 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12238 previous_prefix, name, 0, cu);
0114d602
DJ
12239
12240 /* Create the type. */
12241 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12242 objfile);
abee88f2 12243 TYPE_NAME (type) = name;
0114d602
DJ
12244 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12245
60531b24 12246 return set_die_type (die, type, cu);
0114d602
DJ
12247}
12248
12249/* Read a C++ namespace. */
12250
12251static void
12252read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12253{
12254 struct objfile *objfile = cu->objfile;
0114d602 12255 int is_anonymous;
9219021c 12256
5c4e30ca
DC
12257 /* Add a symbol associated to this if we haven't seen the namespace
12258 before. Also, add a using directive if it's an anonymous
12259 namespace. */
9219021c 12260
f2f0e013 12261 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12262 {
12263 struct type *type;
12264
0114d602 12265 type = read_type_die (die, cu);
e7c27a73 12266 new_symbol (die, type, cu);
5c4e30ca 12267
e8e80198 12268 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12269 if (is_anonymous)
0114d602
DJ
12270 {
12271 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12272
c0cc3a76 12273 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12274 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12275 }
5c4e30ca 12276 }
9219021c 12277
639d11d3 12278 if (die->child != NULL)
d9fa45fe 12279 {
639d11d3 12280 struct die_info *child_die = die->child;
6e70227d 12281
d9fa45fe
DC
12282 while (child_die && child_die->tag)
12283 {
e7c27a73 12284 process_die (child_die, cu);
d9fa45fe
DC
12285 child_die = sibling_die (child_die);
12286 }
12287 }
38d518c9
EZ
12288}
12289
f55ee35c
JK
12290/* Read a Fortran module as type. This DIE can be only a declaration used for
12291 imported module. Still we need that type as local Fortran "use ... only"
12292 declaration imports depend on the created type in determine_prefix. */
12293
12294static struct type *
12295read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12296{
12297 struct objfile *objfile = cu->objfile;
15d034d0 12298 const char *module_name;
f55ee35c
JK
12299 struct type *type;
12300
12301 module_name = dwarf2_name (die, cu);
12302 if (!module_name)
3e43a32a
MS
12303 complaint (&symfile_complaints,
12304 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12305 die->offset.sect_off);
f55ee35c
JK
12306 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12307
12308 /* determine_prefix uses TYPE_TAG_NAME. */
12309 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12310
12311 return set_die_type (die, type, cu);
12312}
12313
5d7cb8df
JK
12314/* Read a Fortran module. */
12315
12316static void
12317read_module (struct die_info *die, struct dwarf2_cu *cu)
12318{
12319 struct die_info *child_die = die->child;
12320
5d7cb8df
JK
12321 while (child_die && child_die->tag)
12322 {
12323 process_die (child_die, cu);
12324 child_die = sibling_die (child_die);
12325 }
12326}
12327
38d518c9
EZ
12328/* Return the name of the namespace represented by DIE. Set
12329 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12330 namespace. */
12331
12332static const char *
e142c38c 12333namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12334{
12335 struct die_info *current_die;
12336 const char *name = NULL;
12337
12338 /* Loop through the extensions until we find a name. */
12339
12340 for (current_die = die;
12341 current_die != NULL;
f2f0e013 12342 current_die = dwarf2_extension (die, &cu))
38d518c9 12343 {
e142c38c 12344 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12345 if (name != NULL)
12346 break;
12347 }
12348
12349 /* Is it an anonymous namespace? */
12350
12351 *is_anonymous = (name == NULL);
12352 if (*is_anonymous)
2b1dbab0 12353 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12354
12355 return name;
d9fa45fe
DC
12356}
12357
c906108c
SS
12358/* Extract all information from a DW_TAG_pointer_type DIE and add to
12359 the user defined type vector. */
12360
f792889a 12361static struct type *
e7c27a73 12362read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12363{
5e2b427d 12364 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12365 struct comp_unit_head *cu_header = &cu->header;
c906108c 12366 struct type *type;
8b2dbe47
KB
12367 struct attribute *attr_byte_size;
12368 struct attribute *attr_address_class;
12369 int byte_size, addr_class;
7e314c57
JK
12370 struct type *target_type;
12371
12372 target_type = die_type (die, cu);
c906108c 12373
7e314c57
JK
12374 /* The die_type call above may have already set the type for this DIE. */
12375 type = get_die_type (die, cu);
12376 if (type)
12377 return type;
12378
12379 type = lookup_pointer_type (target_type);
8b2dbe47 12380
e142c38c 12381 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12382 if (attr_byte_size)
12383 byte_size = DW_UNSND (attr_byte_size);
c906108c 12384 else
8b2dbe47
KB
12385 byte_size = cu_header->addr_size;
12386
e142c38c 12387 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12388 if (attr_address_class)
12389 addr_class = DW_UNSND (attr_address_class);
12390 else
12391 addr_class = DW_ADDR_none;
12392
12393 /* If the pointer size or address class is different than the
12394 default, create a type variant marked as such and set the
12395 length accordingly. */
12396 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12397 {
5e2b427d 12398 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12399 {
12400 int type_flags;
12401
849957d9 12402 type_flags = gdbarch_address_class_type_flags
5e2b427d 12403 (gdbarch, byte_size, addr_class);
876cecd0
TT
12404 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12405 == 0);
8b2dbe47
KB
12406 type = make_type_with_address_space (type, type_flags);
12407 }
12408 else if (TYPE_LENGTH (type) != byte_size)
12409 {
3e43a32a
MS
12410 complaint (&symfile_complaints,
12411 _("invalid pointer size %d"), byte_size);
8b2dbe47 12412 }
6e70227d 12413 else
9a619af0
MS
12414 {
12415 /* Should we also complain about unhandled address classes? */
12416 }
c906108c 12417 }
8b2dbe47
KB
12418
12419 TYPE_LENGTH (type) = byte_size;
f792889a 12420 return set_die_type (die, type, cu);
c906108c
SS
12421}
12422
12423/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12424 the user defined type vector. */
12425
f792889a 12426static struct type *
e7c27a73 12427read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12428{
12429 struct type *type;
12430 struct type *to_type;
12431 struct type *domain;
12432
e7c27a73
DJ
12433 to_type = die_type (die, cu);
12434 domain = die_containing_type (die, cu);
0d5de010 12435
7e314c57
JK
12436 /* The calls above may have already set the type for this DIE. */
12437 type = get_die_type (die, cu);
12438 if (type)
12439 return type;
12440
0d5de010
DJ
12441 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12442 type = lookup_methodptr_type (to_type);
7078baeb
TT
12443 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12444 {
12445 struct type *new_type = alloc_type (cu->objfile);
12446
12447 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12448 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12449 TYPE_VARARGS (to_type));
12450 type = lookup_methodptr_type (new_type);
12451 }
0d5de010
DJ
12452 else
12453 type = lookup_memberptr_type (to_type, domain);
c906108c 12454
f792889a 12455 return set_die_type (die, type, cu);
c906108c
SS
12456}
12457
12458/* Extract all information from a DW_TAG_reference_type DIE and add to
12459 the user defined type vector. */
12460
f792889a 12461static struct type *
e7c27a73 12462read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12463{
e7c27a73 12464 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12465 struct type *type, *target_type;
c906108c
SS
12466 struct attribute *attr;
12467
7e314c57
JK
12468 target_type = die_type (die, cu);
12469
12470 /* The die_type call above may have already set the type for this DIE. */
12471 type = get_die_type (die, cu);
12472 if (type)
12473 return type;
12474
12475 type = lookup_reference_type (target_type);
e142c38c 12476 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12477 if (attr)
12478 {
12479 TYPE_LENGTH (type) = DW_UNSND (attr);
12480 }
12481 else
12482 {
107d2387 12483 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12484 }
f792889a 12485 return set_die_type (die, type, cu);
c906108c
SS
12486}
12487
f792889a 12488static struct type *
e7c27a73 12489read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12490{
f792889a 12491 struct type *base_type, *cv_type;
c906108c 12492
e7c27a73 12493 base_type = die_type (die, cu);
7e314c57
JK
12494
12495 /* The die_type call above may have already set the type for this DIE. */
12496 cv_type = get_die_type (die, cu);
12497 if (cv_type)
12498 return cv_type;
12499
2f608a3a
KW
12500 /* In case the const qualifier is applied to an array type, the element type
12501 is so qualified, not the array type (section 6.7.3 of C99). */
12502 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12503 {
12504 struct type *el_type, *inner_array;
12505
12506 base_type = copy_type (base_type);
12507 inner_array = base_type;
12508
12509 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12510 {
12511 TYPE_TARGET_TYPE (inner_array) =
12512 copy_type (TYPE_TARGET_TYPE (inner_array));
12513 inner_array = TYPE_TARGET_TYPE (inner_array);
12514 }
12515
12516 el_type = TYPE_TARGET_TYPE (inner_array);
12517 TYPE_TARGET_TYPE (inner_array) =
12518 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12519
12520 return set_die_type (die, base_type, cu);
12521 }
12522
f792889a
DJ
12523 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12524 return set_die_type (die, cv_type, cu);
c906108c
SS
12525}
12526
f792889a 12527static struct type *
e7c27a73 12528read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12529{
f792889a 12530 struct type *base_type, *cv_type;
c906108c 12531
e7c27a73 12532 base_type = die_type (die, cu);
7e314c57
JK
12533
12534 /* The die_type call above may have already set the type for this DIE. */
12535 cv_type = get_die_type (die, cu);
12536 if (cv_type)
12537 return cv_type;
12538
f792889a
DJ
12539 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12540 return set_die_type (die, cv_type, cu);
c906108c
SS
12541}
12542
06d66ee9
TT
12543/* Handle DW_TAG_restrict_type. */
12544
12545static struct type *
12546read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12547{
12548 struct type *base_type, *cv_type;
12549
12550 base_type = die_type (die, cu);
12551
12552 /* The die_type call above may have already set the type for this DIE. */
12553 cv_type = get_die_type (die, cu);
12554 if (cv_type)
12555 return cv_type;
12556
12557 cv_type = make_restrict_type (base_type);
12558 return set_die_type (die, cv_type, cu);
12559}
12560
c906108c
SS
12561/* Extract all information from a DW_TAG_string_type DIE and add to
12562 the user defined type vector. It isn't really a user defined type,
12563 but it behaves like one, with other DIE's using an AT_user_def_type
12564 attribute to reference it. */
12565
f792889a 12566static struct type *
e7c27a73 12567read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12568{
e7c27a73 12569 struct objfile *objfile = cu->objfile;
3b7538c0 12570 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12571 struct type *type, *range_type, *index_type, *char_type;
12572 struct attribute *attr;
12573 unsigned int length;
12574
e142c38c 12575 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12576 if (attr)
12577 {
12578 length = DW_UNSND (attr);
12579 }
12580 else
12581 {
0963b4bd 12582 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12583 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12584 if (attr)
12585 {
12586 length = DW_UNSND (attr);
12587 }
12588 else
12589 {
12590 length = 1;
12591 }
c906108c 12592 }
6ccb9162 12593
46bf5051 12594 index_type = objfile_type (objfile)->builtin_int;
c906108c 12595 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12596 char_type = language_string_char_type (cu->language_defn, gdbarch);
12597 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12598
f792889a 12599 return set_die_type (die, type, cu);
c906108c
SS
12600}
12601
12602/* Handle DIES due to C code like:
12603
12604 struct foo
c5aa993b
JM
12605 {
12606 int (*funcp)(int a, long l);
12607 int b;
12608 };
c906108c 12609
0963b4bd 12610 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12611
f792889a 12612static struct type *
e7c27a73 12613read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12614{
bb5ed363 12615 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12616 struct type *type; /* Type that this function returns. */
12617 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12618 struct attribute *attr;
12619
e7c27a73 12620 type = die_type (die, cu);
7e314c57
JK
12621
12622 /* The die_type call above may have already set the type for this DIE. */
12623 ftype = get_die_type (die, cu);
12624 if (ftype)
12625 return ftype;
12626
0c8b41f1 12627 ftype = lookup_function_type (type);
c906108c 12628
5b8101ae 12629 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12630 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12631 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12632 || cu->language == language_cplus
5b8101ae
PM
12633 || cu->language == language_java
12634 || cu->language == language_pascal)
876cecd0 12635 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12636 else if (producer_is_realview (cu->producer))
12637 /* RealView does not emit DW_AT_prototyped. We can not
12638 distinguish prototyped and unprototyped functions; default to
12639 prototyped, since that is more common in modern code (and
12640 RealView warns about unprototyped functions). */
12641 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12642
c055b101
CV
12643 /* Store the calling convention in the type if it's available in
12644 the subroutine die. Otherwise set the calling convention to
12645 the default value DW_CC_normal. */
12646 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12647 if (attr)
12648 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12649 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12650 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12651 else
12652 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12653
12654 /* We need to add the subroutine type to the die immediately so
12655 we don't infinitely recurse when dealing with parameters
0963b4bd 12656 declared as the same subroutine type. */
76c10ea2 12657 set_die_type (die, ftype, cu);
6e70227d 12658
639d11d3 12659 if (die->child != NULL)
c906108c 12660 {
bb5ed363 12661 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12662 struct die_info *child_die;
8072405b 12663 int nparams, iparams;
c906108c
SS
12664
12665 /* Count the number of parameters.
12666 FIXME: GDB currently ignores vararg functions, but knows about
12667 vararg member functions. */
8072405b 12668 nparams = 0;
639d11d3 12669 child_die = die->child;
c906108c
SS
12670 while (child_die && child_die->tag)
12671 {
12672 if (child_die->tag == DW_TAG_formal_parameter)
12673 nparams++;
12674 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12675 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12676 child_die = sibling_die (child_die);
12677 }
12678
12679 /* Allocate storage for parameters and fill them in. */
12680 TYPE_NFIELDS (ftype) = nparams;
12681 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12682 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12683
8072405b
JK
12684 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12685 even if we error out during the parameters reading below. */
12686 for (iparams = 0; iparams < nparams; iparams++)
12687 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12688
12689 iparams = 0;
639d11d3 12690 child_die = die->child;
c906108c
SS
12691 while (child_die && child_die->tag)
12692 {
12693 if (child_die->tag == DW_TAG_formal_parameter)
12694 {
3ce3b1ba
PA
12695 struct type *arg_type;
12696
12697 /* DWARF version 2 has no clean way to discern C++
12698 static and non-static member functions. G++ helps
12699 GDB by marking the first parameter for non-static
12700 member functions (which is the this pointer) as
12701 artificial. We pass this information to
12702 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12703
12704 DWARF version 3 added DW_AT_object_pointer, which GCC
12705 4.5 does not yet generate. */
e142c38c 12706 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12707 if (attr)
12708 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12709 else
418835cc
KS
12710 {
12711 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12712
12713 /* GCC/43521: In java, the formal parameter
12714 "this" is sometimes not marked with DW_AT_artificial. */
12715 if (cu->language == language_java)
12716 {
12717 const char *name = dwarf2_name (child_die, cu);
9a619af0 12718
418835cc
KS
12719 if (name && !strcmp (name, "this"))
12720 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12721 }
12722 }
3ce3b1ba
PA
12723 arg_type = die_type (child_die, cu);
12724
12725 /* RealView does not mark THIS as const, which the testsuite
12726 expects. GCC marks THIS as const in method definitions,
12727 but not in the class specifications (GCC PR 43053). */
12728 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12729 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12730 {
12731 int is_this = 0;
12732 struct dwarf2_cu *arg_cu = cu;
12733 const char *name = dwarf2_name (child_die, cu);
12734
12735 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12736 if (attr)
12737 {
12738 /* If the compiler emits this, use it. */
12739 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12740 is_this = 1;
12741 }
12742 else if (name && strcmp (name, "this") == 0)
12743 /* Function definitions will have the argument names. */
12744 is_this = 1;
12745 else if (name == NULL && iparams == 0)
12746 /* Declarations may not have the names, so like
12747 elsewhere in GDB, assume an artificial first
12748 argument is "this". */
12749 is_this = 1;
12750
12751 if (is_this)
12752 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12753 arg_type, 0);
12754 }
12755
12756 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12757 iparams++;
12758 }
12759 child_die = sibling_die (child_die);
12760 }
12761 }
12762
76c10ea2 12763 return ftype;
c906108c
SS
12764}
12765
f792889a 12766static struct type *
e7c27a73 12767read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12768{
e7c27a73 12769 struct objfile *objfile = cu->objfile;
0114d602 12770 const char *name = NULL;
3c8e0968 12771 struct type *this_type, *target_type;
c906108c 12772
94af9270 12773 name = dwarf2_full_name (NULL, die, cu);
f792889a 12774 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12775 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12776 TYPE_NAME (this_type) = name;
f792889a 12777 set_die_type (die, this_type, cu);
3c8e0968
DE
12778 target_type = die_type (die, cu);
12779 if (target_type != this_type)
12780 TYPE_TARGET_TYPE (this_type) = target_type;
12781 else
12782 {
12783 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12784 spec and cause infinite loops in GDB. */
12785 complaint (&symfile_complaints,
12786 _("Self-referential DW_TAG_typedef "
12787 "- DIE at 0x%x [in module %s]"),
b64f50a1 12788 die->offset.sect_off, objfile->name);
3c8e0968
DE
12789 TYPE_TARGET_TYPE (this_type) = NULL;
12790 }
f792889a 12791 return this_type;
c906108c
SS
12792}
12793
12794/* Find a representation of a given base type and install
12795 it in the TYPE field of the die. */
12796
f792889a 12797static struct type *
e7c27a73 12798read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12799{
e7c27a73 12800 struct objfile *objfile = cu->objfile;
c906108c
SS
12801 struct type *type;
12802 struct attribute *attr;
12803 int encoding = 0, size = 0;
15d034d0 12804 const char *name;
6ccb9162
UW
12805 enum type_code code = TYPE_CODE_INT;
12806 int type_flags = 0;
12807 struct type *target_type = NULL;
c906108c 12808
e142c38c 12809 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12810 if (attr)
12811 {
12812 encoding = DW_UNSND (attr);
12813 }
e142c38c 12814 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12815 if (attr)
12816 {
12817 size = DW_UNSND (attr);
12818 }
39cbfefa 12819 name = dwarf2_name (die, cu);
6ccb9162 12820 if (!name)
c906108c 12821 {
6ccb9162
UW
12822 complaint (&symfile_complaints,
12823 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12824 }
6ccb9162
UW
12825
12826 switch (encoding)
c906108c 12827 {
6ccb9162
UW
12828 case DW_ATE_address:
12829 /* Turn DW_ATE_address into a void * pointer. */
12830 code = TYPE_CODE_PTR;
12831 type_flags |= TYPE_FLAG_UNSIGNED;
12832 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12833 break;
12834 case DW_ATE_boolean:
12835 code = TYPE_CODE_BOOL;
12836 type_flags |= TYPE_FLAG_UNSIGNED;
12837 break;
12838 case DW_ATE_complex_float:
12839 code = TYPE_CODE_COMPLEX;
12840 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12841 break;
12842 case DW_ATE_decimal_float:
12843 code = TYPE_CODE_DECFLOAT;
12844 break;
12845 case DW_ATE_float:
12846 code = TYPE_CODE_FLT;
12847 break;
12848 case DW_ATE_signed:
12849 break;
12850 case DW_ATE_unsigned:
12851 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12852 if (cu->language == language_fortran
12853 && name
12854 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12855 code = TYPE_CODE_CHAR;
6ccb9162
UW
12856 break;
12857 case DW_ATE_signed_char:
6e70227d 12858 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12859 || cu->language == language_pascal
12860 || cu->language == language_fortran)
6ccb9162
UW
12861 code = TYPE_CODE_CHAR;
12862 break;
12863 case DW_ATE_unsigned_char:
868a0084 12864 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12865 || cu->language == language_pascal
12866 || cu->language == language_fortran)
6ccb9162
UW
12867 code = TYPE_CODE_CHAR;
12868 type_flags |= TYPE_FLAG_UNSIGNED;
12869 break;
75079b2b
TT
12870 case DW_ATE_UTF:
12871 /* We just treat this as an integer and then recognize the
12872 type by name elsewhere. */
12873 break;
12874
6ccb9162
UW
12875 default:
12876 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12877 dwarf_type_encoding_name (encoding));
12878 break;
c906108c 12879 }
6ccb9162 12880
0114d602
DJ
12881 type = init_type (code, size, type_flags, NULL, objfile);
12882 TYPE_NAME (type) = name;
6ccb9162
UW
12883 TYPE_TARGET_TYPE (type) = target_type;
12884
0114d602 12885 if (name && strcmp (name, "char") == 0)
876cecd0 12886 TYPE_NOSIGN (type) = 1;
0114d602 12887
f792889a 12888 return set_die_type (die, type, cu);
c906108c
SS
12889}
12890
a02abb62
JB
12891/* Read the given DW_AT_subrange DIE. */
12892
f792889a 12893static struct type *
a02abb62
JB
12894read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12895{
4c9ad8c2 12896 struct type *base_type, *orig_base_type;
a02abb62
JB
12897 struct type *range_type;
12898 struct attribute *attr;
4fae6e18
JK
12899 LONGEST low, high;
12900 int low_default_is_valid;
15d034d0 12901 const char *name;
43bbcdc2 12902 LONGEST negative_mask;
e77813c8 12903
4c9ad8c2
TT
12904 orig_base_type = die_type (die, cu);
12905 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12906 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12907 creating the range type, but we use the result of check_typedef
12908 when examining properties of the type. */
12909 base_type = check_typedef (orig_base_type);
a02abb62 12910
7e314c57
JK
12911 /* The die_type call above may have already set the type for this DIE. */
12912 range_type = get_die_type (die, cu);
12913 if (range_type)
12914 return range_type;
12915
4fae6e18
JK
12916 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12917 omitting DW_AT_lower_bound. */
12918 switch (cu->language)
6e70227d 12919 {
4fae6e18
JK
12920 case language_c:
12921 case language_cplus:
12922 low = 0;
12923 low_default_is_valid = 1;
12924 break;
12925 case language_fortran:
12926 low = 1;
12927 low_default_is_valid = 1;
12928 break;
12929 case language_d:
12930 case language_java:
12931 case language_objc:
12932 low = 0;
12933 low_default_is_valid = (cu->header.version >= 4);
12934 break;
12935 case language_ada:
12936 case language_m2:
12937 case language_pascal:
a02abb62 12938 low = 1;
4fae6e18
JK
12939 low_default_is_valid = (cu->header.version >= 4);
12940 break;
12941 default:
12942 low = 0;
12943 low_default_is_valid = 0;
12944 break;
a02abb62
JB
12945 }
12946
dd5e6932
DJ
12947 /* FIXME: For variable sized arrays either of these could be
12948 a variable rather than a constant value. We'll allow it,
12949 but we don't know how to handle it. */
e142c38c 12950 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12951 if (attr)
4fae6e18
JK
12952 low = dwarf2_get_attr_constant_value (attr, low);
12953 else if (!low_default_is_valid)
12954 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12955 "- DIE at 0x%x [in module %s]"),
12956 die->offset.sect_off, cu->objfile->name);
a02abb62 12957
e142c38c 12958 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12959 if (attr)
6e70227d 12960 {
d48323d8 12961 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12962 {
12963 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12964 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12965 FIXME: GDB does not yet know how to handle dynamic
12966 arrays properly, treat them as arrays with unspecified
12967 length for now.
12968
12969 FIXME: jimb/2003-09-22: GDB does not really know
12970 how to handle arrays of unspecified length
12971 either; we just represent them as zero-length
12972 arrays. Choose an appropriate upper bound given
12973 the lower bound we've computed above. */
12974 high = low - 1;
12975 }
12976 else
12977 high = dwarf2_get_attr_constant_value (attr, 1);
12978 }
e77813c8
PM
12979 else
12980 {
12981 attr = dwarf2_attr (die, DW_AT_count, cu);
12982 if (attr)
12983 {
12984 int count = dwarf2_get_attr_constant_value (attr, 1);
12985 high = low + count - 1;
12986 }
c2ff108b
JK
12987 else
12988 {
12989 /* Unspecified array length. */
12990 high = low - 1;
12991 }
e77813c8
PM
12992 }
12993
12994 /* Dwarf-2 specifications explicitly allows to create subrange types
12995 without specifying a base type.
12996 In that case, the base type must be set to the type of
12997 the lower bound, upper bound or count, in that order, if any of these
12998 three attributes references an object that has a type.
12999 If no base type is found, the Dwarf-2 specifications say that
13000 a signed integer type of size equal to the size of an address should
13001 be used.
13002 For the following C code: `extern char gdb_int [];'
13003 GCC produces an empty range DIE.
13004 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13005 high bound or count are not yet handled by this code. */
e77813c8
PM
13006 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13007 {
13008 struct objfile *objfile = cu->objfile;
13009 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13010 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13011 struct type *int_type = objfile_type (objfile)->builtin_int;
13012
13013 /* Test "int", "long int", and "long long int" objfile types,
13014 and select the first one having a size above or equal to the
13015 architecture address size. */
13016 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13017 base_type = int_type;
13018 else
13019 {
13020 int_type = objfile_type (objfile)->builtin_long;
13021 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13022 base_type = int_type;
13023 else
13024 {
13025 int_type = objfile_type (objfile)->builtin_long_long;
13026 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13027 base_type = int_type;
13028 }
13029 }
13030 }
a02abb62 13031
6e70227d 13032 negative_mask =
43bbcdc2
PH
13033 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13034 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13035 low |= negative_mask;
13036 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13037 high |= negative_mask;
13038
4c9ad8c2 13039 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13040
bbb0eef6
JK
13041 /* Mark arrays with dynamic length at least as an array of unspecified
13042 length. GDB could check the boundary but before it gets implemented at
13043 least allow accessing the array elements. */
d48323d8 13044 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13045 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13046
c2ff108b
JK
13047 /* Ada expects an empty array on no boundary attributes. */
13048 if (attr == NULL && cu->language != language_ada)
13049 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13050
39cbfefa
DJ
13051 name = dwarf2_name (die, cu);
13052 if (name)
13053 TYPE_NAME (range_type) = name;
6e70227d 13054
e142c38c 13055 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13056 if (attr)
13057 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13058
7e314c57
JK
13059 set_die_type (die, range_type, cu);
13060
13061 /* set_die_type should be already done. */
b4ba55a1
JB
13062 set_descriptive_type (range_type, die, cu);
13063
7e314c57 13064 return range_type;
a02abb62 13065}
6e70227d 13066
f792889a 13067static struct type *
81a17f79
JB
13068read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13069{
13070 struct type *type;
81a17f79 13071
81a17f79
JB
13072 /* For now, we only support the C meaning of an unspecified type: void. */
13073
0114d602
DJ
13074 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13075 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13076
f792889a 13077 return set_die_type (die, type, cu);
81a17f79 13078}
a02abb62 13079
639d11d3
DC
13080/* Read a single die and all its descendents. Set the die's sibling
13081 field to NULL; set other fields in the die correctly, and set all
13082 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13083 location of the info_ptr after reading all of those dies. PARENT
13084 is the parent of the die in question. */
13085
13086static struct die_info *
dee91e82 13087read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13088 const gdb_byte *info_ptr,
13089 const gdb_byte **new_info_ptr,
dee91e82 13090 struct die_info *parent)
639d11d3
DC
13091{
13092 struct die_info *die;
d521ce57 13093 const gdb_byte *cur_ptr;
639d11d3
DC
13094 int has_children;
13095
bf6af496 13096 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13097 if (die == NULL)
13098 {
13099 *new_info_ptr = cur_ptr;
13100 return NULL;
13101 }
93311388 13102 store_in_ref_table (die, reader->cu);
639d11d3
DC
13103
13104 if (has_children)
bf6af496 13105 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13106 else
13107 {
13108 die->child = NULL;
13109 *new_info_ptr = cur_ptr;
13110 }
13111
13112 die->sibling = NULL;
13113 die->parent = parent;
13114 return die;
13115}
13116
13117/* Read a die, all of its descendents, and all of its siblings; set
13118 all of the fields of all of the dies correctly. Arguments are as
13119 in read_die_and_children. */
13120
13121static struct die_info *
bf6af496 13122read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13123 const gdb_byte *info_ptr,
13124 const gdb_byte **new_info_ptr,
bf6af496 13125 struct die_info *parent)
639d11d3
DC
13126{
13127 struct die_info *first_die, *last_sibling;
d521ce57 13128 const gdb_byte *cur_ptr;
639d11d3 13129
c906108c 13130 cur_ptr = info_ptr;
639d11d3
DC
13131 first_die = last_sibling = NULL;
13132
13133 while (1)
c906108c 13134 {
639d11d3 13135 struct die_info *die
dee91e82 13136 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13137
1d325ec1 13138 if (die == NULL)
c906108c 13139 {
639d11d3
DC
13140 *new_info_ptr = cur_ptr;
13141 return first_die;
c906108c 13142 }
1d325ec1
DJ
13143
13144 if (!first_die)
13145 first_die = die;
c906108c 13146 else
1d325ec1
DJ
13147 last_sibling->sibling = die;
13148
13149 last_sibling = die;
c906108c 13150 }
c906108c
SS
13151}
13152
bf6af496
DE
13153/* Read a die, all of its descendents, and all of its siblings; set
13154 all of the fields of all of the dies correctly. Arguments are as
13155 in read_die_and_children.
13156 This the main entry point for reading a DIE and all its children. */
13157
13158static struct die_info *
13159read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13160 const gdb_byte *info_ptr,
13161 const gdb_byte **new_info_ptr,
bf6af496
DE
13162 struct die_info *parent)
13163{
13164 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13165 new_info_ptr, parent);
13166
13167 if (dwarf2_die_debug)
13168 {
13169 fprintf_unfiltered (gdb_stdlog,
13170 "Read die from %s@0x%x of %s:\n",
13171 bfd_section_name (reader->abfd,
13172 reader->die_section->asection),
13173 (unsigned) (info_ptr - reader->die_section->buffer),
13174 bfd_get_filename (reader->abfd));
13175 dump_die (die, dwarf2_die_debug);
13176 }
13177
13178 return die;
13179}
13180
3019eac3
DE
13181/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13182 attributes.
13183 The caller is responsible for filling in the extra attributes
13184 and updating (*DIEP)->num_attrs.
13185 Set DIEP to point to a newly allocated die with its information,
13186 except for its child, sibling, and parent fields.
13187 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13188
d521ce57 13189static const gdb_byte *
3019eac3 13190read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13191 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13192 int *has_children, int num_extra_attrs)
93311388 13193{
b64f50a1
JK
13194 unsigned int abbrev_number, bytes_read, i;
13195 sect_offset offset;
93311388
DE
13196 struct abbrev_info *abbrev;
13197 struct die_info *die;
13198 struct dwarf2_cu *cu = reader->cu;
13199 bfd *abfd = reader->abfd;
13200
b64f50a1 13201 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13202 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13203 info_ptr += bytes_read;
13204 if (!abbrev_number)
13205 {
13206 *diep = NULL;
13207 *has_children = 0;
13208 return info_ptr;
13209 }
13210
433df2d4 13211 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13212 if (!abbrev)
348e048f
DE
13213 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13214 abbrev_number,
13215 bfd_get_filename (abfd));
13216
3019eac3 13217 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13218 die->offset = offset;
13219 die->tag = abbrev->tag;
13220 die->abbrev = abbrev_number;
13221
3019eac3
DE
13222 /* Make the result usable.
13223 The caller needs to update num_attrs after adding the extra
13224 attributes. */
93311388
DE
13225 die->num_attrs = abbrev->num_attrs;
13226
13227 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13228 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13229 info_ptr);
93311388
DE
13230
13231 *diep = die;
13232 *has_children = abbrev->has_children;
13233 return info_ptr;
13234}
13235
3019eac3
DE
13236/* Read a die and all its attributes.
13237 Set DIEP to point to a newly allocated die with its information,
13238 except for its child, sibling, and parent fields.
13239 Set HAS_CHILDREN to tell whether the die has children or not. */
13240
d521ce57 13241static const gdb_byte *
3019eac3 13242read_full_die (const struct die_reader_specs *reader,
d521ce57 13243 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13244 int *has_children)
13245{
d521ce57 13246 const gdb_byte *result;
bf6af496
DE
13247
13248 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13249
13250 if (dwarf2_die_debug)
13251 {
13252 fprintf_unfiltered (gdb_stdlog,
13253 "Read die from %s@0x%x of %s:\n",
13254 bfd_section_name (reader->abfd,
13255 reader->die_section->asection),
13256 (unsigned) (info_ptr - reader->die_section->buffer),
13257 bfd_get_filename (reader->abfd));
13258 dump_die (*diep, dwarf2_die_debug);
13259 }
13260
13261 return result;
3019eac3 13262}
433df2d4
DE
13263\f
13264/* Abbreviation tables.
3019eac3 13265
433df2d4 13266 In DWARF version 2, the description of the debugging information is
c906108c
SS
13267 stored in a separate .debug_abbrev section. Before we read any
13268 dies from a section we read in all abbreviations and install them
433df2d4
DE
13269 in a hash table. */
13270
13271/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13272
13273static struct abbrev_info *
13274abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13275{
13276 struct abbrev_info *abbrev;
13277
13278 abbrev = (struct abbrev_info *)
13279 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13280 memset (abbrev, 0, sizeof (struct abbrev_info));
13281 return abbrev;
13282}
13283
13284/* Add an abbreviation to the table. */
c906108c
SS
13285
13286static void
433df2d4
DE
13287abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13288 unsigned int abbrev_number,
13289 struct abbrev_info *abbrev)
13290{
13291 unsigned int hash_number;
13292
13293 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13294 abbrev->next = abbrev_table->abbrevs[hash_number];
13295 abbrev_table->abbrevs[hash_number] = abbrev;
13296}
dee91e82 13297
433df2d4
DE
13298/* Look up an abbrev in the table.
13299 Returns NULL if the abbrev is not found. */
13300
13301static struct abbrev_info *
13302abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13303 unsigned int abbrev_number)
c906108c 13304{
433df2d4
DE
13305 unsigned int hash_number;
13306 struct abbrev_info *abbrev;
13307
13308 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13309 abbrev = abbrev_table->abbrevs[hash_number];
13310
13311 while (abbrev)
13312 {
13313 if (abbrev->number == abbrev_number)
13314 return abbrev;
13315 abbrev = abbrev->next;
13316 }
13317 return NULL;
13318}
13319
13320/* Read in an abbrev table. */
13321
13322static struct abbrev_table *
13323abbrev_table_read_table (struct dwarf2_section_info *section,
13324 sect_offset offset)
13325{
13326 struct objfile *objfile = dwarf2_per_objfile->objfile;
13327 bfd *abfd = section->asection->owner;
13328 struct abbrev_table *abbrev_table;
d521ce57 13329 const gdb_byte *abbrev_ptr;
c906108c
SS
13330 struct abbrev_info *cur_abbrev;
13331 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13332 unsigned int abbrev_form;
f3dd6933
DJ
13333 struct attr_abbrev *cur_attrs;
13334 unsigned int allocated_attrs;
c906108c 13335
433df2d4 13336 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13337 abbrev_table->offset = offset;
433df2d4
DE
13338 obstack_init (&abbrev_table->abbrev_obstack);
13339 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13340 (ABBREV_HASH_SIZE
13341 * sizeof (struct abbrev_info *)));
13342 memset (abbrev_table->abbrevs, 0,
13343 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13344
433df2d4
DE
13345 dwarf2_read_section (objfile, section);
13346 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13347 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13348 abbrev_ptr += bytes_read;
13349
f3dd6933
DJ
13350 allocated_attrs = ATTR_ALLOC_CHUNK;
13351 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13352
0963b4bd 13353 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13354 while (abbrev_number)
13355 {
433df2d4 13356 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13357
13358 /* read in abbrev header */
13359 cur_abbrev->number = abbrev_number;
13360 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13361 abbrev_ptr += bytes_read;
13362 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13363 abbrev_ptr += 1;
13364
13365 /* now read in declarations */
13366 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13367 abbrev_ptr += bytes_read;
13368 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13369 abbrev_ptr += bytes_read;
13370 while (abbrev_name)
13371 {
f3dd6933 13372 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13373 {
f3dd6933
DJ
13374 allocated_attrs += ATTR_ALLOC_CHUNK;
13375 cur_attrs
13376 = xrealloc (cur_attrs, (allocated_attrs
13377 * sizeof (struct attr_abbrev)));
c906108c 13378 }
ae038cb0 13379
f3dd6933
DJ
13380 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13381 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13382 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13383 abbrev_ptr += bytes_read;
13384 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13385 abbrev_ptr += bytes_read;
13386 }
13387
433df2d4 13388 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13389 (cur_abbrev->num_attrs
13390 * sizeof (struct attr_abbrev)));
13391 memcpy (cur_abbrev->attrs, cur_attrs,
13392 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13393
433df2d4 13394 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13395
13396 /* Get next abbreviation.
13397 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13398 always properly terminated with an abbrev number of 0.
13399 Exit loop if we encounter an abbreviation which we have
13400 already read (which means we are about to read the abbreviations
13401 for the next compile unit) or if the end of the abbreviation
13402 table is reached. */
433df2d4 13403 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13404 break;
13405 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13406 abbrev_ptr += bytes_read;
433df2d4 13407 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13408 break;
13409 }
f3dd6933
DJ
13410
13411 xfree (cur_attrs);
433df2d4 13412 return abbrev_table;
c906108c
SS
13413}
13414
433df2d4 13415/* Free the resources held by ABBREV_TABLE. */
c906108c 13416
c906108c 13417static void
433df2d4 13418abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13419{
433df2d4
DE
13420 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13421 xfree (abbrev_table);
c906108c
SS
13422}
13423
f4dc4d17
DE
13424/* Same as abbrev_table_free but as a cleanup.
13425 We pass in a pointer to the pointer to the table so that we can
13426 set the pointer to NULL when we're done. It also simplifies
13427 build_type_unit_groups. */
13428
13429static void
13430abbrev_table_free_cleanup (void *table_ptr)
13431{
13432 struct abbrev_table **abbrev_table_ptr = table_ptr;
13433
13434 if (*abbrev_table_ptr != NULL)
13435 abbrev_table_free (*abbrev_table_ptr);
13436 *abbrev_table_ptr = NULL;
13437}
13438
433df2d4
DE
13439/* Read the abbrev table for CU from ABBREV_SECTION. */
13440
13441static void
13442dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13443 struct dwarf2_section_info *abbrev_section)
c906108c 13444{
433df2d4
DE
13445 cu->abbrev_table =
13446 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13447}
c906108c 13448
433df2d4 13449/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13450
433df2d4
DE
13451static void
13452dwarf2_free_abbrev_table (void *ptr_to_cu)
13453{
13454 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13455
433df2d4
DE
13456 abbrev_table_free (cu->abbrev_table);
13457 /* Set this to NULL so that we SEGV if we try to read it later,
13458 and also because free_comp_unit verifies this is NULL. */
13459 cu->abbrev_table = NULL;
13460}
13461\f
72bf9492
DJ
13462/* Returns nonzero if TAG represents a type that we might generate a partial
13463 symbol for. */
13464
13465static int
13466is_type_tag_for_partial (int tag)
13467{
13468 switch (tag)
13469 {
13470#if 0
13471 /* Some types that would be reasonable to generate partial symbols for,
13472 that we don't at present. */
13473 case DW_TAG_array_type:
13474 case DW_TAG_file_type:
13475 case DW_TAG_ptr_to_member_type:
13476 case DW_TAG_set_type:
13477 case DW_TAG_string_type:
13478 case DW_TAG_subroutine_type:
13479#endif
13480 case DW_TAG_base_type:
13481 case DW_TAG_class_type:
680b30c7 13482 case DW_TAG_interface_type:
72bf9492
DJ
13483 case DW_TAG_enumeration_type:
13484 case DW_TAG_structure_type:
13485 case DW_TAG_subrange_type:
13486 case DW_TAG_typedef:
13487 case DW_TAG_union_type:
13488 return 1;
13489 default:
13490 return 0;
13491 }
13492}
13493
13494/* Load all DIEs that are interesting for partial symbols into memory. */
13495
13496static struct partial_die_info *
dee91e82 13497load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13498 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13499{
dee91e82 13500 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13501 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13502 struct partial_die_info *part_die;
13503 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13504 struct abbrev_info *abbrev;
13505 unsigned int bytes_read;
5afb4e99 13506 unsigned int load_all = 0;
72bf9492
DJ
13507 int nesting_level = 1;
13508
13509 parent_die = NULL;
13510 last_die = NULL;
13511
7adf1e79
DE
13512 gdb_assert (cu->per_cu != NULL);
13513 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13514 load_all = 1;
13515
72bf9492
DJ
13516 cu->partial_dies
13517 = htab_create_alloc_ex (cu->header.length / 12,
13518 partial_die_hash,
13519 partial_die_eq,
13520 NULL,
13521 &cu->comp_unit_obstack,
13522 hashtab_obstack_allocate,
13523 dummy_obstack_deallocate);
13524
13525 part_die = obstack_alloc (&cu->comp_unit_obstack,
13526 sizeof (struct partial_die_info));
13527
13528 while (1)
13529 {
13530 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13531
13532 /* A NULL abbrev means the end of a series of children. */
13533 if (abbrev == NULL)
13534 {
13535 if (--nesting_level == 0)
13536 {
13537 /* PART_DIE was probably the last thing allocated on the
13538 comp_unit_obstack, so we could call obstack_free
13539 here. We don't do that because the waste is small,
13540 and will be cleaned up when we're done with this
13541 compilation unit. This way, we're also more robust
13542 against other users of the comp_unit_obstack. */
13543 return first_die;
13544 }
13545 info_ptr += bytes_read;
13546 last_die = parent_die;
13547 parent_die = parent_die->die_parent;
13548 continue;
13549 }
13550
98bfdba5
PA
13551 /* Check for template arguments. We never save these; if
13552 they're seen, we just mark the parent, and go on our way. */
13553 if (parent_die != NULL
13554 && cu->language == language_cplus
13555 && (abbrev->tag == DW_TAG_template_type_param
13556 || abbrev->tag == DW_TAG_template_value_param))
13557 {
13558 parent_die->has_template_arguments = 1;
13559
13560 if (!load_all)
13561 {
13562 /* We don't need a partial DIE for the template argument. */
dee91e82 13563 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13564 continue;
13565 }
13566 }
13567
0d99eb77 13568 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13569 Skip their other children. */
13570 if (!load_all
13571 && cu->language == language_cplus
13572 && parent_die != NULL
13573 && parent_die->tag == DW_TAG_subprogram)
13574 {
dee91e82 13575 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13576 continue;
13577 }
13578
5afb4e99
DJ
13579 /* Check whether this DIE is interesting enough to save. Normally
13580 we would not be interested in members here, but there may be
13581 later variables referencing them via DW_AT_specification (for
13582 static members). */
13583 if (!load_all
13584 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13585 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13586 && abbrev->tag != DW_TAG_enumerator
13587 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13588 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13589 && abbrev->tag != DW_TAG_variable
5afb4e99 13590 && abbrev->tag != DW_TAG_namespace
f55ee35c 13591 && abbrev->tag != DW_TAG_module
95554aad
TT
13592 && abbrev->tag != DW_TAG_member
13593 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13594 {
13595 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13596 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13597 continue;
13598 }
13599
dee91e82
DE
13600 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13601 info_ptr);
72bf9492
DJ
13602
13603 /* This two-pass algorithm for processing partial symbols has a
13604 high cost in cache pressure. Thus, handle some simple cases
13605 here which cover the majority of C partial symbols. DIEs
13606 which neither have specification tags in them, nor could have
13607 specification tags elsewhere pointing at them, can simply be
13608 processed and discarded.
13609
13610 This segment is also optional; scan_partial_symbols and
13611 add_partial_symbol will handle these DIEs if we chain
13612 them in normally. When compilers which do not emit large
13613 quantities of duplicate debug information are more common,
13614 this code can probably be removed. */
13615
13616 /* Any complete simple types at the top level (pretty much all
13617 of them, for a language without namespaces), can be processed
13618 directly. */
13619 if (parent_die == NULL
13620 && part_die->has_specification == 0
13621 && part_die->is_declaration == 0
d8228535 13622 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13623 || part_die->tag == DW_TAG_base_type
13624 || part_die->tag == DW_TAG_subrange_type))
13625 {
13626 if (building_psymtab && part_die->name != NULL)
04a679b8 13627 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13628 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13629 &objfile->static_psymbols,
13630 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13631 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13632 continue;
13633 }
13634
d8228535
JK
13635 /* The exception for DW_TAG_typedef with has_children above is
13636 a workaround of GCC PR debug/47510. In the case of this complaint
13637 type_name_no_tag_or_error will error on such types later.
13638
13639 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13640 it could not find the child DIEs referenced later, this is checked
13641 above. In correct DWARF DW_TAG_typedef should have no children. */
13642
13643 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13644 complaint (&symfile_complaints,
13645 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13646 "- DIE at 0x%x [in module %s]"),
b64f50a1 13647 part_die->offset.sect_off, objfile->name);
d8228535 13648
72bf9492
DJ
13649 /* If we're at the second level, and we're an enumerator, and
13650 our parent has no specification (meaning possibly lives in a
13651 namespace elsewhere), then we can add the partial symbol now
13652 instead of queueing it. */
13653 if (part_die->tag == DW_TAG_enumerator
13654 && parent_die != NULL
13655 && parent_die->die_parent == NULL
13656 && parent_die->tag == DW_TAG_enumeration_type
13657 && parent_die->has_specification == 0)
13658 {
13659 if (part_die->name == NULL)
3e43a32a
MS
13660 complaint (&symfile_complaints,
13661 _("malformed enumerator DIE ignored"));
72bf9492 13662 else if (building_psymtab)
04a679b8 13663 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13664 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13665 (cu->language == language_cplus
13666 || cu->language == language_java)
bb5ed363
DE
13667 ? &objfile->global_psymbols
13668 : &objfile->static_psymbols,
13669 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13670
dee91e82 13671 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13672 continue;
13673 }
13674
13675 /* We'll save this DIE so link it in. */
13676 part_die->die_parent = parent_die;
13677 part_die->die_sibling = NULL;
13678 part_die->die_child = NULL;
13679
13680 if (last_die && last_die == parent_die)
13681 last_die->die_child = part_die;
13682 else if (last_die)
13683 last_die->die_sibling = part_die;
13684
13685 last_die = part_die;
13686
13687 if (first_die == NULL)
13688 first_die = part_die;
13689
13690 /* Maybe add the DIE to the hash table. Not all DIEs that we
13691 find interesting need to be in the hash table, because we
13692 also have the parent/sibling/child chains; only those that we
13693 might refer to by offset later during partial symbol reading.
13694
13695 For now this means things that might have be the target of a
13696 DW_AT_specification, DW_AT_abstract_origin, or
13697 DW_AT_extension. DW_AT_extension will refer only to
13698 namespaces; DW_AT_abstract_origin refers to functions (and
13699 many things under the function DIE, but we do not recurse
13700 into function DIEs during partial symbol reading) and
13701 possibly variables as well; DW_AT_specification refers to
13702 declarations. Declarations ought to have the DW_AT_declaration
13703 flag. It happens that GCC forgets to put it in sometimes, but
13704 only for functions, not for types.
13705
13706 Adding more things than necessary to the hash table is harmless
13707 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13708 wasted time in find_partial_die, when we reread the compilation
13709 unit with load_all_dies set. */
72bf9492 13710
5afb4e99 13711 if (load_all
72929c62 13712 || abbrev->tag == DW_TAG_constant
5afb4e99 13713 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13714 || abbrev->tag == DW_TAG_variable
13715 || abbrev->tag == DW_TAG_namespace
13716 || part_die->is_declaration)
13717 {
13718 void **slot;
13719
13720 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13721 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13722 *slot = part_die;
13723 }
13724
13725 part_die = obstack_alloc (&cu->comp_unit_obstack,
13726 sizeof (struct partial_die_info));
13727
13728 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13729 we have no reason to follow the children of structures; for other
98bfdba5
PA
13730 languages we have to, so that we can get at method physnames
13731 to infer fully qualified class names, for DW_AT_specification,
13732 and for C++ template arguments. For C++, we also look one level
13733 inside functions to find template arguments (if the name of the
13734 function does not already contain the template arguments).
bc30ff58
JB
13735
13736 For Ada, we need to scan the children of subprograms and lexical
13737 blocks as well because Ada allows the definition of nested
13738 entities that could be interesting for the debugger, such as
13739 nested subprograms for instance. */
72bf9492 13740 if (last_die->has_children
5afb4e99
DJ
13741 && (load_all
13742 || last_die->tag == DW_TAG_namespace
f55ee35c 13743 || last_die->tag == DW_TAG_module
72bf9492 13744 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13745 || (cu->language == language_cplus
13746 && last_die->tag == DW_TAG_subprogram
13747 && (last_die->name == NULL
13748 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13749 || (cu->language != language_c
13750 && (last_die->tag == DW_TAG_class_type
680b30c7 13751 || last_die->tag == DW_TAG_interface_type
72bf9492 13752 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13753 || last_die->tag == DW_TAG_union_type))
13754 || (cu->language == language_ada
13755 && (last_die->tag == DW_TAG_subprogram
13756 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13757 {
13758 nesting_level++;
13759 parent_die = last_die;
13760 continue;
13761 }
13762
13763 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13764 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13765
13766 /* Back to the top, do it again. */
13767 }
13768}
13769
c906108c
SS
13770/* Read a minimal amount of information into the minimal die structure. */
13771
d521ce57 13772static const gdb_byte *
dee91e82
DE
13773read_partial_die (const struct die_reader_specs *reader,
13774 struct partial_die_info *part_die,
13775 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 13776 const gdb_byte *info_ptr)
c906108c 13777{
dee91e82 13778 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13779 struct objfile *objfile = cu->objfile;
d521ce57 13780 const gdb_byte *buffer = reader->buffer;
fa238c03 13781 unsigned int i;
c906108c 13782 struct attribute attr;
c5aa993b 13783 int has_low_pc_attr = 0;
c906108c 13784 int has_high_pc_attr = 0;
91da1414 13785 int high_pc_relative = 0;
c906108c 13786
72bf9492 13787 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13788
b64f50a1 13789 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13790
13791 info_ptr += abbrev_len;
13792
13793 if (abbrev == NULL)
13794 return info_ptr;
13795
c906108c
SS
13796 part_die->tag = abbrev->tag;
13797 part_die->has_children = abbrev->has_children;
c906108c
SS
13798
13799 for (i = 0; i < abbrev->num_attrs; ++i)
13800 {
dee91e82 13801 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13802
13803 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13804 partial symbol table. */
c906108c
SS
13805 switch (attr.name)
13806 {
13807 case DW_AT_name:
71c25dea
TT
13808 switch (part_die->tag)
13809 {
13810 case DW_TAG_compile_unit:
95554aad 13811 case DW_TAG_partial_unit:
348e048f 13812 case DW_TAG_type_unit:
71c25dea
TT
13813 /* Compilation units have a DW_AT_name that is a filename, not
13814 a source language identifier. */
13815 case DW_TAG_enumeration_type:
13816 case DW_TAG_enumerator:
13817 /* These tags always have simple identifiers already; no need
13818 to canonicalize them. */
13819 part_die->name = DW_STRING (&attr);
13820 break;
13821 default:
13822 part_die->name
13823 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13824 &objfile->objfile_obstack);
71c25dea
TT
13825 break;
13826 }
c906108c 13827 break;
31ef98ae 13828 case DW_AT_linkage_name:
c906108c 13829 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13830 /* Note that both forms of linkage name might appear. We
13831 assume they will be the same, and we only store the last
13832 one we see. */
94af9270
KS
13833 if (cu->language == language_ada)
13834 part_die->name = DW_STRING (&attr);
abc72ce4 13835 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13836 break;
13837 case DW_AT_low_pc:
13838 has_low_pc_attr = 1;
13839 part_die->lowpc = DW_ADDR (&attr);
13840 break;
13841 case DW_AT_high_pc:
13842 has_high_pc_attr = 1;
3019eac3
DE
13843 if (attr.form == DW_FORM_addr
13844 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13845 part_die->highpc = DW_ADDR (&attr);
13846 else
13847 {
13848 high_pc_relative = 1;
13849 part_die->highpc = DW_UNSND (&attr);
13850 }
c906108c
SS
13851 break;
13852 case DW_AT_location:
0963b4bd 13853 /* Support the .debug_loc offsets. */
8e19ed76
PS
13854 if (attr_form_is_block (&attr))
13855 {
95554aad 13856 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13857 }
3690dd37 13858 else if (attr_form_is_section_offset (&attr))
8e19ed76 13859 {
4d3c2250 13860 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13861 }
13862 else
13863 {
4d3c2250
KB
13864 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13865 "partial symbol information");
8e19ed76 13866 }
c906108c 13867 break;
c906108c
SS
13868 case DW_AT_external:
13869 part_die->is_external = DW_UNSND (&attr);
13870 break;
13871 case DW_AT_declaration:
13872 part_die->is_declaration = DW_UNSND (&attr);
13873 break;
13874 case DW_AT_type:
13875 part_die->has_type = 1;
13876 break;
13877 case DW_AT_abstract_origin:
13878 case DW_AT_specification:
72bf9492
DJ
13879 case DW_AT_extension:
13880 part_die->has_specification = 1;
c764a876 13881 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13882 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13883 || cu->per_cu->is_dwz);
c906108c
SS
13884 break;
13885 case DW_AT_sibling:
13886 /* Ignore absolute siblings, they might point outside of
13887 the current compile unit. */
13888 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13889 complaint (&symfile_complaints,
13890 _("ignoring absolute DW_AT_sibling"));
c906108c 13891 else
b64f50a1 13892 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13893 break;
fa4028e9
JB
13894 case DW_AT_byte_size:
13895 part_die->has_byte_size = 1;
13896 break;
68511cec
CES
13897 case DW_AT_calling_convention:
13898 /* DWARF doesn't provide a way to identify a program's source-level
13899 entry point. DW_AT_calling_convention attributes are only meant
13900 to describe functions' calling conventions.
13901
13902 However, because it's a necessary piece of information in
13903 Fortran, and because DW_CC_program is the only piece of debugging
13904 information whose definition refers to a 'main program' at all,
13905 several compilers have begun marking Fortran main programs with
13906 DW_CC_program --- even when those functions use the standard
13907 calling conventions.
13908
13909 So until DWARF specifies a way to provide this information and
13910 compilers pick up the new representation, we'll support this
13911 practice. */
13912 if (DW_UNSND (&attr) == DW_CC_program
13913 && cu->language == language_fortran)
01f8c46d
JK
13914 {
13915 set_main_name (part_die->name);
13916
13917 /* As this DIE has a static linkage the name would be difficult
13918 to look up later. */
13919 language_of_main = language_fortran;
13920 }
68511cec 13921 break;
481860b3
GB
13922 case DW_AT_inline:
13923 if (DW_UNSND (&attr) == DW_INL_inlined
13924 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13925 part_die->may_be_inlined = 1;
13926 break;
95554aad
TT
13927
13928 case DW_AT_import:
13929 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13930 {
13931 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13932 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13933 || cu->per_cu->is_dwz);
13934 }
95554aad
TT
13935 break;
13936
c906108c
SS
13937 default:
13938 break;
13939 }
13940 }
13941
91da1414
MW
13942 if (high_pc_relative)
13943 part_die->highpc += part_die->lowpc;
13944
9373cf26
JK
13945 if (has_low_pc_attr && has_high_pc_attr)
13946 {
13947 /* When using the GNU linker, .gnu.linkonce. sections are used to
13948 eliminate duplicate copies of functions and vtables and such.
13949 The linker will arbitrarily choose one and discard the others.
13950 The AT_*_pc values for such functions refer to local labels in
13951 these sections. If the section from that file was discarded, the
13952 labels are not in the output, so the relocs get a value of 0.
13953 If this is a discarded function, mark the pc bounds as invalid,
13954 so that GDB will ignore it. */
13955 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13956 {
bb5ed363 13957 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13958
13959 complaint (&symfile_complaints,
13960 _("DW_AT_low_pc %s is zero "
13961 "for DIE at 0x%x [in module %s]"),
13962 paddress (gdbarch, part_die->lowpc),
b64f50a1 13963 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13964 }
13965 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13966 else if (part_die->lowpc >= part_die->highpc)
13967 {
bb5ed363 13968 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13969
13970 complaint (&symfile_complaints,
13971 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13972 "for DIE at 0x%x [in module %s]"),
13973 paddress (gdbarch, part_die->lowpc),
13974 paddress (gdbarch, part_die->highpc),
b64f50a1 13975 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13976 }
13977 else
13978 part_die->has_pc_info = 1;
13979 }
85cbf3d3 13980
c906108c
SS
13981 return info_ptr;
13982}
13983
72bf9492
DJ
13984/* Find a cached partial DIE at OFFSET in CU. */
13985
13986static struct partial_die_info *
b64f50a1 13987find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13988{
13989 struct partial_die_info *lookup_die = NULL;
13990 struct partial_die_info part_die;
13991
13992 part_die.offset = offset;
b64f50a1
JK
13993 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13994 offset.sect_off);
72bf9492 13995
72bf9492
DJ
13996 return lookup_die;
13997}
13998
348e048f
DE
13999/* Find a partial DIE at OFFSET, which may or may not be in CU,
14000 except in the case of .debug_types DIEs which do not reference
14001 outside their CU (they do however referencing other types via
55f1336d 14002 DW_FORM_ref_sig8). */
72bf9492
DJ
14003
14004static struct partial_die_info *
36586728 14005find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14006{
bb5ed363 14007 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14008 struct dwarf2_per_cu_data *per_cu = NULL;
14009 struct partial_die_info *pd = NULL;
72bf9492 14010
36586728
TT
14011 if (offset_in_dwz == cu->per_cu->is_dwz
14012 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14013 {
14014 pd = find_partial_die_in_comp_unit (offset, cu);
14015 if (pd != NULL)
14016 return pd;
0d99eb77
DE
14017 /* We missed recording what we needed.
14018 Load all dies and try again. */
14019 per_cu = cu->per_cu;
5afb4e99 14020 }
0d99eb77
DE
14021 else
14022 {
14023 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14024 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14025 {
14026 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14027 " external reference to offset 0x%lx [in module %s].\n"),
14028 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14029 bfd_get_filename (objfile->obfd));
14030 }
36586728
TT
14031 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14032 objfile);
72bf9492 14033
0d99eb77
DE
14034 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14035 load_partial_comp_unit (per_cu);
ae038cb0 14036
0d99eb77
DE
14037 per_cu->cu->last_used = 0;
14038 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14039 }
5afb4e99 14040
dee91e82
DE
14041 /* If we didn't find it, and not all dies have been loaded,
14042 load them all and try again. */
14043
5afb4e99
DJ
14044 if (pd == NULL && per_cu->load_all_dies == 0)
14045 {
5afb4e99 14046 per_cu->load_all_dies = 1;
fd820528
DE
14047
14048 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14049 THIS_CU->cu may already be in use. So we can't just free it and
14050 replace its DIEs with the ones we read in. Instead, we leave those
14051 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14052 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14053 set. */
dee91e82 14054 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14055
14056 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14057 }
14058
14059 if (pd == NULL)
14060 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14061 _("could not find partial DIE 0x%x "
14062 "in cache [from module %s]\n"),
b64f50a1 14063 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14064 return pd;
72bf9492
DJ
14065}
14066
abc72ce4
DE
14067/* See if we can figure out if the class lives in a namespace. We do
14068 this by looking for a member function; its demangled name will
14069 contain namespace info, if there is any. */
14070
14071static void
14072guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14073 struct dwarf2_cu *cu)
14074{
14075 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14076 what template types look like, because the demangler
14077 frequently doesn't give the same name as the debug info. We
14078 could fix this by only using the demangled name to get the
14079 prefix (but see comment in read_structure_type). */
14080
14081 struct partial_die_info *real_pdi;
14082 struct partial_die_info *child_pdi;
14083
14084 /* If this DIE (this DIE's specification, if any) has a parent, then
14085 we should not do this. We'll prepend the parent's fully qualified
14086 name when we create the partial symbol. */
14087
14088 real_pdi = struct_pdi;
14089 while (real_pdi->has_specification)
36586728
TT
14090 real_pdi = find_partial_die (real_pdi->spec_offset,
14091 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14092
14093 if (real_pdi->die_parent != NULL)
14094 return;
14095
14096 for (child_pdi = struct_pdi->die_child;
14097 child_pdi != NULL;
14098 child_pdi = child_pdi->die_sibling)
14099 {
14100 if (child_pdi->tag == DW_TAG_subprogram
14101 && child_pdi->linkage_name != NULL)
14102 {
14103 char *actual_class_name
14104 = language_class_name_from_physname (cu->language_defn,
14105 child_pdi->linkage_name);
14106 if (actual_class_name != NULL)
14107 {
14108 struct_pdi->name
10f0c4bb
TT
14109 = obstack_copy0 (&cu->objfile->objfile_obstack,
14110 actual_class_name,
14111 strlen (actual_class_name));
abc72ce4
DE
14112 xfree (actual_class_name);
14113 }
14114 break;
14115 }
14116 }
14117}
14118
72bf9492
DJ
14119/* Adjust PART_DIE before generating a symbol for it. This function
14120 may set the is_external flag or change the DIE's name. */
14121
14122static void
14123fixup_partial_die (struct partial_die_info *part_die,
14124 struct dwarf2_cu *cu)
14125{
abc72ce4
DE
14126 /* Once we've fixed up a die, there's no point in doing so again.
14127 This also avoids a memory leak if we were to call
14128 guess_partial_die_structure_name multiple times. */
14129 if (part_die->fixup_called)
14130 return;
14131
72bf9492
DJ
14132 /* If we found a reference attribute and the DIE has no name, try
14133 to find a name in the referred to DIE. */
14134
14135 if (part_die->name == NULL && part_die->has_specification)
14136 {
14137 struct partial_die_info *spec_die;
72bf9492 14138
36586728
TT
14139 spec_die = find_partial_die (part_die->spec_offset,
14140 part_die->spec_is_dwz, cu);
72bf9492 14141
10b3939b 14142 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14143
14144 if (spec_die->name)
14145 {
14146 part_die->name = spec_die->name;
14147
14148 /* Copy DW_AT_external attribute if it is set. */
14149 if (spec_die->is_external)
14150 part_die->is_external = spec_die->is_external;
14151 }
14152 }
14153
14154 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14155
14156 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14157 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14158
abc72ce4
DE
14159 /* If there is no parent die to provide a namespace, and there are
14160 children, see if we can determine the namespace from their linkage
122d1940 14161 name. */
abc72ce4 14162 if (cu->language == language_cplus
8b70b953 14163 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14164 && part_die->die_parent == NULL
14165 && part_die->has_children
14166 && (part_die->tag == DW_TAG_class_type
14167 || part_die->tag == DW_TAG_structure_type
14168 || part_die->tag == DW_TAG_union_type))
14169 guess_partial_die_structure_name (part_die, cu);
14170
53832f31
TT
14171 /* GCC might emit a nameless struct or union that has a linkage
14172 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14173 if (part_die->name == NULL
96408a79
SA
14174 && (part_die->tag == DW_TAG_class_type
14175 || part_die->tag == DW_TAG_interface_type
14176 || part_die->tag == DW_TAG_structure_type
14177 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14178 && part_die->linkage_name != NULL)
14179 {
14180 char *demangled;
14181
8de20a37 14182 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14183 if (demangled)
14184 {
96408a79
SA
14185 const char *base;
14186
14187 /* Strip any leading namespaces/classes, keep only the base name.
14188 DW_AT_name for named DIEs does not contain the prefixes. */
14189 base = strrchr (demangled, ':');
14190 if (base && base > demangled && base[-1] == ':')
14191 base++;
14192 else
14193 base = demangled;
14194
10f0c4bb
TT
14195 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14196 base, strlen (base));
53832f31
TT
14197 xfree (demangled);
14198 }
14199 }
14200
abc72ce4 14201 part_die->fixup_called = 1;
72bf9492
DJ
14202}
14203
a8329558 14204/* Read an attribute value described by an attribute form. */
c906108c 14205
d521ce57 14206static const gdb_byte *
dee91e82
DE
14207read_attribute_value (const struct die_reader_specs *reader,
14208 struct attribute *attr, unsigned form,
d521ce57 14209 const gdb_byte *info_ptr)
c906108c 14210{
dee91e82
DE
14211 struct dwarf2_cu *cu = reader->cu;
14212 bfd *abfd = reader->abfd;
e7c27a73 14213 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14214 unsigned int bytes_read;
14215 struct dwarf_block *blk;
14216
a8329558
KW
14217 attr->form = form;
14218 switch (form)
c906108c 14219 {
c906108c 14220 case DW_FORM_ref_addr:
ae411497 14221 if (cu->header.version == 2)
4568ecf9 14222 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14223 else
4568ecf9
DE
14224 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14225 &cu->header, &bytes_read);
ae411497
TT
14226 info_ptr += bytes_read;
14227 break;
36586728
TT
14228 case DW_FORM_GNU_ref_alt:
14229 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14230 info_ptr += bytes_read;
14231 break;
ae411497 14232 case DW_FORM_addr:
e7c27a73 14233 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14234 info_ptr += bytes_read;
c906108c
SS
14235 break;
14236 case DW_FORM_block2:
7b5a2f43 14237 blk = dwarf_alloc_block (cu);
c906108c
SS
14238 blk->size = read_2_bytes (abfd, info_ptr);
14239 info_ptr += 2;
14240 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14241 info_ptr += blk->size;
14242 DW_BLOCK (attr) = blk;
14243 break;
14244 case DW_FORM_block4:
7b5a2f43 14245 blk = dwarf_alloc_block (cu);
c906108c
SS
14246 blk->size = read_4_bytes (abfd, info_ptr);
14247 info_ptr += 4;
14248 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14249 info_ptr += blk->size;
14250 DW_BLOCK (attr) = blk;
14251 break;
14252 case DW_FORM_data2:
14253 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14254 info_ptr += 2;
14255 break;
14256 case DW_FORM_data4:
14257 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14258 info_ptr += 4;
14259 break;
14260 case DW_FORM_data8:
14261 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14262 info_ptr += 8;
14263 break;
2dc7f7b3
TT
14264 case DW_FORM_sec_offset:
14265 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14266 info_ptr += bytes_read;
14267 break;
c906108c 14268 case DW_FORM_string:
9b1c24c8 14269 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14270 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14271 info_ptr += bytes_read;
14272 break;
4bdf3d34 14273 case DW_FORM_strp:
36586728
TT
14274 if (!cu->per_cu->is_dwz)
14275 {
14276 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14277 &bytes_read);
14278 DW_STRING_IS_CANONICAL (attr) = 0;
14279 info_ptr += bytes_read;
14280 break;
14281 }
14282 /* FALLTHROUGH */
14283 case DW_FORM_GNU_strp_alt:
14284 {
14285 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14286 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14287 &bytes_read);
14288
14289 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14290 DW_STRING_IS_CANONICAL (attr) = 0;
14291 info_ptr += bytes_read;
14292 }
4bdf3d34 14293 break;
2dc7f7b3 14294 case DW_FORM_exprloc:
c906108c 14295 case DW_FORM_block:
7b5a2f43 14296 blk = dwarf_alloc_block (cu);
c906108c
SS
14297 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14298 info_ptr += bytes_read;
14299 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14300 info_ptr += blk->size;
14301 DW_BLOCK (attr) = blk;
14302 break;
14303 case DW_FORM_block1:
7b5a2f43 14304 blk = dwarf_alloc_block (cu);
c906108c
SS
14305 blk->size = read_1_byte (abfd, info_ptr);
14306 info_ptr += 1;
14307 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14308 info_ptr += blk->size;
14309 DW_BLOCK (attr) = blk;
14310 break;
14311 case DW_FORM_data1:
14312 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14313 info_ptr += 1;
14314 break;
14315 case DW_FORM_flag:
14316 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14317 info_ptr += 1;
14318 break;
2dc7f7b3
TT
14319 case DW_FORM_flag_present:
14320 DW_UNSND (attr) = 1;
14321 break;
c906108c
SS
14322 case DW_FORM_sdata:
14323 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14324 info_ptr += bytes_read;
14325 break;
14326 case DW_FORM_udata:
14327 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14328 info_ptr += bytes_read;
14329 break;
14330 case DW_FORM_ref1:
4568ecf9
DE
14331 DW_UNSND (attr) = (cu->header.offset.sect_off
14332 + read_1_byte (abfd, info_ptr));
c906108c
SS
14333 info_ptr += 1;
14334 break;
14335 case DW_FORM_ref2:
4568ecf9
DE
14336 DW_UNSND (attr) = (cu->header.offset.sect_off
14337 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14338 info_ptr += 2;
14339 break;
14340 case DW_FORM_ref4:
4568ecf9
DE
14341 DW_UNSND (attr) = (cu->header.offset.sect_off
14342 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14343 info_ptr += 4;
14344 break;
613e1657 14345 case DW_FORM_ref8:
4568ecf9
DE
14346 DW_UNSND (attr) = (cu->header.offset.sect_off
14347 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14348 info_ptr += 8;
14349 break;
55f1336d 14350 case DW_FORM_ref_sig8:
ac9ec31b 14351 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14352 info_ptr += 8;
14353 break;
c906108c 14354 case DW_FORM_ref_udata:
4568ecf9
DE
14355 DW_UNSND (attr) = (cu->header.offset.sect_off
14356 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14357 info_ptr += bytes_read;
14358 break;
c906108c 14359 case DW_FORM_indirect:
a8329558
KW
14360 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14361 info_ptr += bytes_read;
dee91e82 14362 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14363 break;
3019eac3
DE
14364 case DW_FORM_GNU_addr_index:
14365 if (reader->dwo_file == NULL)
14366 {
14367 /* For now flag a hard error.
14368 Later we can turn this into a complaint. */
14369 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14370 dwarf_form_name (form),
14371 bfd_get_filename (abfd));
14372 }
14373 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14374 info_ptr += bytes_read;
14375 break;
14376 case DW_FORM_GNU_str_index:
14377 if (reader->dwo_file == NULL)
14378 {
14379 /* For now flag a hard error.
14380 Later we can turn this into a complaint if warranted. */
14381 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14382 dwarf_form_name (form),
14383 bfd_get_filename (abfd));
14384 }
14385 {
14386 ULONGEST str_index =
14387 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14388
14389 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14390 DW_STRING_IS_CANONICAL (attr) = 0;
14391 info_ptr += bytes_read;
14392 }
14393 break;
c906108c 14394 default:
8a3fe4f8 14395 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14396 dwarf_form_name (form),
14397 bfd_get_filename (abfd));
c906108c 14398 }
28e94949 14399
36586728
TT
14400 /* Super hack. */
14401 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14402 attr->form = DW_FORM_GNU_ref_alt;
14403
28e94949
JB
14404 /* We have seen instances where the compiler tried to emit a byte
14405 size attribute of -1 which ended up being encoded as an unsigned
14406 0xffffffff. Although 0xffffffff is technically a valid size value,
14407 an object of this size seems pretty unlikely so we can relatively
14408 safely treat these cases as if the size attribute was invalid and
14409 treat them as zero by default. */
14410 if (attr->name == DW_AT_byte_size
14411 && form == DW_FORM_data4
14412 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14413 {
14414 complaint
14415 (&symfile_complaints,
43bbcdc2
PH
14416 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14417 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14418 DW_UNSND (attr) = 0;
14419 }
28e94949 14420
c906108c
SS
14421 return info_ptr;
14422}
14423
a8329558
KW
14424/* Read an attribute described by an abbreviated attribute. */
14425
d521ce57 14426static const gdb_byte *
dee91e82
DE
14427read_attribute (const struct die_reader_specs *reader,
14428 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14429 const gdb_byte *info_ptr)
a8329558
KW
14430{
14431 attr->name = abbrev->name;
dee91e82 14432 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14433}
14434
0963b4bd 14435/* Read dwarf information from a buffer. */
c906108c
SS
14436
14437static unsigned int
a1855c1d 14438read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14439{
fe1b8b76 14440 return bfd_get_8 (abfd, buf);
c906108c
SS
14441}
14442
14443static int
a1855c1d 14444read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14445{
fe1b8b76 14446 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14447}
14448
14449static unsigned int
a1855c1d 14450read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14451{
fe1b8b76 14452 return bfd_get_16 (abfd, buf);
c906108c
SS
14453}
14454
21ae7a4d 14455static int
a1855c1d 14456read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14457{
14458 return bfd_get_signed_16 (abfd, buf);
14459}
14460
c906108c 14461static unsigned int
a1855c1d 14462read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14463{
fe1b8b76 14464 return bfd_get_32 (abfd, buf);
c906108c
SS
14465}
14466
21ae7a4d 14467static int
a1855c1d 14468read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14469{
14470 return bfd_get_signed_32 (abfd, buf);
14471}
14472
93311388 14473static ULONGEST
a1855c1d 14474read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14475{
fe1b8b76 14476 return bfd_get_64 (abfd, buf);
c906108c
SS
14477}
14478
14479static CORE_ADDR
d521ce57 14480read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14481 unsigned int *bytes_read)
c906108c 14482{
e7c27a73 14483 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14484 CORE_ADDR retval = 0;
14485
107d2387 14486 if (cu_header->signed_addr_p)
c906108c 14487 {
107d2387
AC
14488 switch (cu_header->addr_size)
14489 {
14490 case 2:
fe1b8b76 14491 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14492 break;
14493 case 4:
fe1b8b76 14494 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14495 break;
14496 case 8:
fe1b8b76 14497 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14498 break;
14499 default:
8e65ff28 14500 internal_error (__FILE__, __LINE__,
e2e0b3e5 14501 _("read_address: bad switch, signed [in module %s]"),
659b0389 14502 bfd_get_filename (abfd));
107d2387
AC
14503 }
14504 }
14505 else
14506 {
14507 switch (cu_header->addr_size)
14508 {
14509 case 2:
fe1b8b76 14510 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14511 break;
14512 case 4:
fe1b8b76 14513 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14514 break;
14515 case 8:
fe1b8b76 14516 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14517 break;
14518 default:
8e65ff28 14519 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14520 _("read_address: bad switch, "
14521 "unsigned [in module %s]"),
659b0389 14522 bfd_get_filename (abfd));
107d2387 14523 }
c906108c 14524 }
64367e0a 14525
107d2387
AC
14526 *bytes_read = cu_header->addr_size;
14527 return retval;
c906108c
SS
14528}
14529
f7ef9339 14530/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14531 specification allows the initial length to take up either 4 bytes
14532 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14533 bytes describe the length and all offsets will be 8 bytes in length
14534 instead of 4.
14535
f7ef9339
KB
14536 An older, non-standard 64-bit format is also handled by this
14537 function. The older format in question stores the initial length
14538 as an 8-byte quantity without an escape value. Lengths greater
14539 than 2^32 aren't very common which means that the initial 4 bytes
14540 is almost always zero. Since a length value of zero doesn't make
14541 sense for the 32-bit format, this initial zero can be considered to
14542 be an escape value which indicates the presence of the older 64-bit
14543 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14544 greater than 4GB. If it becomes necessary to handle lengths
14545 somewhat larger than 4GB, we could allow other small values (such
14546 as the non-sensical values of 1, 2, and 3) to also be used as
14547 escape values indicating the presence of the old format.
f7ef9339 14548
917c78fc
MK
14549 The value returned via bytes_read should be used to increment the
14550 relevant pointer after calling read_initial_length().
c764a876 14551
613e1657
KB
14552 [ Note: read_initial_length() and read_offset() are based on the
14553 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14554 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14555 from:
14556
f7ef9339 14557 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14558
613e1657
KB
14559 This document is only a draft and is subject to change. (So beware.)
14560
f7ef9339 14561 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14562 determined empirically by examining 64-bit ELF files produced by
14563 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14564
14565 - Kevin, July 16, 2002
613e1657
KB
14566 ] */
14567
14568static LONGEST
d521ce57 14569read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 14570{
fe1b8b76 14571 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14572
dd373385 14573 if (length == 0xffffffff)
613e1657 14574 {
fe1b8b76 14575 length = bfd_get_64 (abfd, buf + 4);
613e1657 14576 *bytes_read = 12;
613e1657 14577 }
dd373385 14578 else if (length == 0)
f7ef9339 14579 {
dd373385 14580 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14581 length = bfd_get_64 (abfd, buf);
f7ef9339 14582 *bytes_read = 8;
f7ef9339 14583 }
613e1657
KB
14584 else
14585 {
14586 *bytes_read = 4;
613e1657
KB
14587 }
14588
c764a876
DE
14589 return length;
14590}
dd373385 14591
c764a876
DE
14592/* Cover function for read_initial_length.
14593 Returns the length of the object at BUF, and stores the size of the
14594 initial length in *BYTES_READ and stores the size that offsets will be in
14595 *OFFSET_SIZE.
14596 If the initial length size is not equivalent to that specified in
14597 CU_HEADER then issue a complaint.
14598 This is useful when reading non-comp-unit headers. */
dd373385 14599
c764a876 14600static LONGEST
d521ce57 14601read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
14602 const struct comp_unit_head *cu_header,
14603 unsigned int *bytes_read,
14604 unsigned int *offset_size)
14605{
14606 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14607
14608 gdb_assert (cu_header->initial_length_size == 4
14609 || cu_header->initial_length_size == 8
14610 || cu_header->initial_length_size == 12);
14611
14612 if (cu_header->initial_length_size != *bytes_read)
14613 complaint (&symfile_complaints,
14614 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14615
c764a876 14616 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14617 return length;
613e1657
KB
14618}
14619
14620/* Read an offset from the data stream. The size of the offset is
917c78fc 14621 given by cu_header->offset_size. */
613e1657
KB
14622
14623static LONGEST
d521ce57
TT
14624read_offset (bfd *abfd, const gdb_byte *buf,
14625 const struct comp_unit_head *cu_header,
891d2f0b 14626 unsigned int *bytes_read)
c764a876
DE
14627{
14628 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14629
c764a876
DE
14630 *bytes_read = cu_header->offset_size;
14631 return offset;
14632}
14633
14634/* Read an offset from the data stream. */
14635
14636static LONGEST
d521ce57 14637read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14638{
14639 LONGEST retval = 0;
14640
c764a876 14641 switch (offset_size)
613e1657
KB
14642 {
14643 case 4:
fe1b8b76 14644 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14645 break;
14646 case 8:
fe1b8b76 14647 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14648 break;
14649 default:
8e65ff28 14650 internal_error (__FILE__, __LINE__,
c764a876 14651 _("read_offset_1: bad switch [in module %s]"),
659b0389 14652 bfd_get_filename (abfd));
613e1657
KB
14653 }
14654
917c78fc 14655 return retval;
613e1657
KB
14656}
14657
d521ce57
TT
14658static const gdb_byte *
14659read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
14660{
14661 /* If the size of a host char is 8 bits, we can return a pointer
14662 to the buffer, otherwise we have to copy the data to a buffer
14663 allocated on the temporary obstack. */
4bdf3d34 14664 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14665 return buf;
c906108c
SS
14666}
14667
d521ce57
TT
14668static const char *
14669read_direct_string (bfd *abfd, const gdb_byte *buf,
14670 unsigned int *bytes_read_ptr)
c906108c
SS
14671{
14672 /* If the size of a host char is 8 bits, we can return a pointer
14673 to the string, otherwise we have to copy the string to a buffer
14674 allocated on the temporary obstack. */
4bdf3d34 14675 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14676 if (*buf == '\0')
14677 {
14678 *bytes_read_ptr = 1;
14679 return NULL;
14680 }
d521ce57
TT
14681 *bytes_read_ptr = strlen ((const char *) buf) + 1;
14682 return (const char *) buf;
4bdf3d34
JJ
14683}
14684
d521ce57 14685static const char *
cf2c3c16 14686read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14687{
be391dca 14688 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14689 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14690 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14691 bfd_get_filename (abfd));
dce234bc 14692 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14693 error (_("DW_FORM_strp pointing outside of "
14694 ".debug_str section [in module %s]"),
14695 bfd_get_filename (abfd));
4bdf3d34 14696 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14697 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14698 return NULL;
d521ce57 14699 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14700}
14701
36586728
TT
14702/* Read a string at offset STR_OFFSET in the .debug_str section from
14703 the .dwz file DWZ. Throw an error if the offset is too large. If
14704 the string consists of a single NUL byte, return NULL; otherwise
14705 return a pointer to the string. */
14706
d521ce57 14707static const char *
36586728
TT
14708read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14709{
14710 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14711
14712 if (dwz->str.buffer == NULL)
14713 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14714 "section [in module %s]"),
14715 bfd_get_filename (dwz->dwz_bfd));
14716 if (str_offset >= dwz->str.size)
14717 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14718 ".debug_str section [in module %s]"),
14719 bfd_get_filename (dwz->dwz_bfd));
14720 gdb_assert (HOST_CHAR_BIT == 8);
14721 if (dwz->str.buffer[str_offset] == '\0')
14722 return NULL;
d521ce57 14723 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
14724}
14725
d521ce57
TT
14726static const char *
14727read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
14728 const struct comp_unit_head *cu_header,
14729 unsigned int *bytes_read_ptr)
14730{
14731 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14732
14733 return read_indirect_string_at_offset (abfd, str_offset);
14734}
14735
12df843f 14736static ULONGEST
d521ce57
TT
14737read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
14738 unsigned int *bytes_read_ptr)
c906108c 14739{
12df843f 14740 ULONGEST result;
ce5d95e1 14741 unsigned int num_read;
c906108c
SS
14742 int i, shift;
14743 unsigned char byte;
14744
14745 result = 0;
14746 shift = 0;
14747 num_read = 0;
14748 i = 0;
14749 while (1)
14750 {
fe1b8b76 14751 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14752 buf++;
14753 num_read++;
12df843f 14754 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14755 if ((byte & 128) == 0)
14756 {
14757 break;
14758 }
14759 shift += 7;
14760 }
14761 *bytes_read_ptr = num_read;
14762 return result;
14763}
14764
12df843f 14765static LONGEST
d521ce57
TT
14766read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
14767 unsigned int *bytes_read_ptr)
c906108c 14768{
12df843f 14769 LONGEST result;
77e0b926 14770 int i, shift, num_read;
c906108c
SS
14771 unsigned char byte;
14772
14773 result = 0;
14774 shift = 0;
c906108c
SS
14775 num_read = 0;
14776 i = 0;
14777 while (1)
14778 {
fe1b8b76 14779 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14780 buf++;
14781 num_read++;
12df843f 14782 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14783 shift += 7;
14784 if ((byte & 128) == 0)
14785 {
14786 break;
14787 }
14788 }
77e0b926 14789 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14790 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14791 *bytes_read_ptr = num_read;
14792 return result;
14793}
14794
3019eac3
DE
14795/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14796 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14797 ADDR_SIZE is the size of addresses from the CU header. */
14798
14799static CORE_ADDR
14800read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14801{
14802 struct objfile *objfile = dwarf2_per_objfile->objfile;
14803 bfd *abfd = objfile->obfd;
14804 const gdb_byte *info_ptr;
14805
14806 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14807 if (dwarf2_per_objfile->addr.buffer == NULL)
14808 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14809 objfile->name);
14810 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14811 error (_("DW_FORM_addr_index pointing outside of "
14812 ".debug_addr section [in module %s]"),
14813 objfile->name);
14814 info_ptr = (dwarf2_per_objfile->addr.buffer
14815 + addr_base + addr_index * addr_size);
14816 if (addr_size == 4)
14817 return bfd_get_32 (abfd, info_ptr);
14818 else
14819 return bfd_get_64 (abfd, info_ptr);
14820}
14821
14822/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14823
14824static CORE_ADDR
14825read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14826{
14827 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14828}
14829
14830/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14831
14832static CORE_ADDR
d521ce57 14833read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
14834 unsigned int *bytes_read)
14835{
14836 bfd *abfd = cu->objfile->obfd;
14837 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14838
14839 return read_addr_index (cu, addr_index);
14840}
14841
14842/* Data structure to pass results from dwarf2_read_addr_index_reader
14843 back to dwarf2_read_addr_index. */
14844
14845struct dwarf2_read_addr_index_data
14846{
14847 ULONGEST addr_base;
14848 int addr_size;
14849};
14850
14851/* die_reader_func for dwarf2_read_addr_index. */
14852
14853static void
14854dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 14855 const gdb_byte *info_ptr,
3019eac3
DE
14856 struct die_info *comp_unit_die,
14857 int has_children,
14858 void *data)
14859{
14860 struct dwarf2_cu *cu = reader->cu;
14861 struct dwarf2_read_addr_index_data *aidata =
14862 (struct dwarf2_read_addr_index_data *) data;
14863
14864 aidata->addr_base = cu->addr_base;
14865 aidata->addr_size = cu->header.addr_size;
14866}
14867
14868/* Given an index in .debug_addr, fetch the value.
14869 NOTE: This can be called during dwarf expression evaluation,
14870 long after the debug information has been read, and thus per_cu->cu
14871 may no longer exist. */
14872
14873CORE_ADDR
14874dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14875 unsigned int addr_index)
14876{
14877 struct objfile *objfile = per_cu->objfile;
14878 struct dwarf2_cu *cu = per_cu->cu;
14879 ULONGEST addr_base;
14880 int addr_size;
14881
14882 /* This is intended to be called from outside this file. */
14883 dw2_setup (objfile);
14884
14885 /* We need addr_base and addr_size.
14886 If we don't have PER_CU->cu, we have to get it.
14887 Nasty, but the alternative is storing the needed info in PER_CU,
14888 which at this point doesn't seem justified: it's not clear how frequently
14889 it would get used and it would increase the size of every PER_CU.
14890 Entry points like dwarf2_per_cu_addr_size do a similar thing
14891 so we're not in uncharted territory here.
14892 Alas we need to be a bit more complicated as addr_base is contained
14893 in the DIE.
14894
14895 We don't need to read the entire CU(/TU).
14896 We just need the header and top level die.
a1b64ce1 14897
3019eac3 14898 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14899 For now we skip this optimization. */
3019eac3
DE
14900
14901 if (cu != NULL)
14902 {
14903 addr_base = cu->addr_base;
14904 addr_size = cu->header.addr_size;
14905 }
14906 else
14907 {
14908 struct dwarf2_read_addr_index_data aidata;
14909
a1b64ce1
DE
14910 /* Note: We can't use init_cutu_and_read_dies_simple here,
14911 we need addr_base. */
14912 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14913 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14914 addr_base = aidata.addr_base;
14915 addr_size = aidata.addr_size;
14916 }
14917
14918 return read_addr_index_1 (addr_index, addr_base, addr_size);
14919}
14920
14921/* Given a DW_AT_str_index, fetch the string. */
14922
d521ce57 14923static const char *
3019eac3
DE
14924read_str_index (const struct die_reader_specs *reader,
14925 struct dwarf2_cu *cu, ULONGEST str_index)
14926{
14927 struct objfile *objfile = dwarf2_per_objfile->objfile;
14928 const char *dwo_name = objfile->name;
14929 bfd *abfd = objfile->obfd;
14930 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 14931 const gdb_byte *info_ptr;
3019eac3
DE
14932 ULONGEST str_offset;
14933
14934 dwarf2_read_section (objfile, &sections->str);
14935 dwarf2_read_section (objfile, &sections->str_offsets);
14936 if (sections->str.buffer == NULL)
14937 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14938 " in CU at offset 0x%lx [in module %s]"),
14939 (long) cu->header.offset.sect_off, dwo_name);
14940 if (sections->str_offsets.buffer == NULL)
14941 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14942 " in CU at offset 0x%lx [in module %s]"),
14943 (long) cu->header.offset.sect_off, dwo_name);
14944 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14945 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14946 " section in CU at offset 0x%lx [in module %s]"),
14947 (long) cu->header.offset.sect_off, dwo_name);
14948 info_ptr = (sections->str_offsets.buffer
14949 + str_index * cu->header.offset_size);
14950 if (cu->header.offset_size == 4)
14951 str_offset = bfd_get_32 (abfd, info_ptr);
14952 else
14953 str_offset = bfd_get_64 (abfd, info_ptr);
14954 if (str_offset >= sections->str.size)
14955 error (_("Offset from DW_FORM_str_index pointing outside of"
14956 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14957 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 14958 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
14959}
14960
3019eac3
DE
14961/* Return the length of an LEB128 number in BUF. */
14962
14963static int
14964leb128_size (const gdb_byte *buf)
14965{
14966 const gdb_byte *begin = buf;
14967 gdb_byte byte;
14968
14969 while (1)
14970 {
14971 byte = *buf++;
14972 if ((byte & 128) == 0)
14973 return buf - begin;
14974 }
14975}
14976
c906108c 14977static void
e142c38c 14978set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14979{
14980 switch (lang)
14981 {
14982 case DW_LANG_C89:
76bee0cc 14983 case DW_LANG_C99:
c906108c 14984 case DW_LANG_C:
e142c38c 14985 cu->language = language_c;
c906108c
SS
14986 break;
14987 case DW_LANG_C_plus_plus:
e142c38c 14988 cu->language = language_cplus;
c906108c 14989 break;
6aecb9c2
JB
14990 case DW_LANG_D:
14991 cu->language = language_d;
14992 break;
c906108c
SS
14993 case DW_LANG_Fortran77:
14994 case DW_LANG_Fortran90:
b21b22e0 14995 case DW_LANG_Fortran95:
e142c38c 14996 cu->language = language_fortran;
c906108c 14997 break;
a766d390
DE
14998 case DW_LANG_Go:
14999 cu->language = language_go;
15000 break;
c906108c 15001 case DW_LANG_Mips_Assembler:
e142c38c 15002 cu->language = language_asm;
c906108c 15003 break;
bebd888e 15004 case DW_LANG_Java:
e142c38c 15005 cu->language = language_java;
bebd888e 15006 break;
c906108c 15007 case DW_LANG_Ada83:
8aaf0b47 15008 case DW_LANG_Ada95:
bc5f45f8
JB
15009 cu->language = language_ada;
15010 break;
72019c9c
GM
15011 case DW_LANG_Modula2:
15012 cu->language = language_m2;
15013 break;
fe8e67fd
PM
15014 case DW_LANG_Pascal83:
15015 cu->language = language_pascal;
15016 break;
22566fbd
DJ
15017 case DW_LANG_ObjC:
15018 cu->language = language_objc;
15019 break;
c906108c
SS
15020 case DW_LANG_Cobol74:
15021 case DW_LANG_Cobol85:
c906108c 15022 default:
e142c38c 15023 cu->language = language_minimal;
c906108c
SS
15024 break;
15025 }
e142c38c 15026 cu->language_defn = language_def (cu->language);
c906108c
SS
15027}
15028
15029/* Return the named attribute or NULL if not there. */
15030
15031static struct attribute *
e142c38c 15032dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15033{
a48e046c 15034 for (;;)
c906108c 15035 {
a48e046c
TT
15036 unsigned int i;
15037 struct attribute *spec = NULL;
15038
15039 for (i = 0; i < die->num_attrs; ++i)
15040 {
15041 if (die->attrs[i].name == name)
15042 return &die->attrs[i];
15043 if (die->attrs[i].name == DW_AT_specification
15044 || die->attrs[i].name == DW_AT_abstract_origin)
15045 spec = &die->attrs[i];
15046 }
15047
15048 if (!spec)
15049 break;
c906108c 15050
f2f0e013 15051 die = follow_die_ref (die, spec, &cu);
f2f0e013 15052 }
c5aa993b 15053
c906108c
SS
15054 return NULL;
15055}
15056
348e048f
DE
15057/* Return the named attribute or NULL if not there,
15058 but do not follow DW_AT_specification, etc.
15059 This is for use in contexts where we're reading .debug_types dies.
15060 Following DW_AT_specification, DW_AT_abstract_origin will take us
15061 back up the chain, and we want to go down. */
15062
15063static struct attribute *
45e58e77 15064dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15065{
15066 unsigned int i;
15067
15068 for (i = 0; i < die->num_attrs; ++i)
15069 if (die->attrs[i].name == name)
15070 return &die->attrs[i];
15071
15072 return NULL;
15073}
15074
05cf31d1
JB
15075/* Return non-zero iff the attribute NAME is defined for the given DIE,
15076 and holds a non-zero value. This function should only be used for
2dc7f7b3 15077 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15078
15079static int
15080dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15081{
15082 struct attribute *attr = dwarf2_attr (die, name, cu);
15083
15084 return (attr && DW_UNSND (attr));
15085}
15086
3ca72b44 15087static int
e142c38c 15088die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15089{
05cf31d1
JB
15090 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15091 which value is non-zero. However, we have to be careful with
15092 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15093 (via dwarf2_flag_true_p) follows this attribute. So we may
15094 end up accidently finding a declaration attribute that belongs
15095 to a different DIE referenced by the specification attribute,
15096 even though the given DIE does not have a declaration attribute. */
15097 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15098 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15099}
15100
63d06c5c 15101/* Return the die giving the specification for DIE, if there is
f2f0e013 15102 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15103 containing the return value on output. If there is no
15104 specification, but there is an abstract origin, that is
15105 returned. */
63d06c5c
DC
15106
15107static struct die_info *
f2f0e013 15108die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15109{
f2f0e013
DJ
15110 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15111 *spec_cu);
63d06c5c 15112
edb3359d
DJ
15113 if (spec_attr == NULL)
15114 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15115
63d06c5c
DC
15116 if (spec_attr == NULL)
15117 return NULL;
15118 else
f2f0e013 15119 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15120}
c906108c 15121
debd256d 15122/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15123 refers to.
15124 NOTE: This is also used as a "cleanup" function. */
15125
debd256d
JB
15126static void
15127free_line_header (struct line_header *lh)
15128{
15129 if (lh->standard_opcode_lengths)
a8bc7b56 15130 xfree (lh->standard_opcode_lengths);
debd256d
JB
15131
15132 /* Remember that all the lh->file_names[i].name pointers are
15133 pointers into debug_line_buffer, and don't need to be freed. */
15134 if (lh->file_names)
a8bc7b56 15135 xfree (lh->file_names);
debd256d
JB
15136
15137 /* Similarly for the include directory names. */
15138 if (lh->include_dirs)
a8bc7b56 15139 xfree (lh->include_dirs);
debd256d 15140
a8bc7b56 15141 xfree (lh);
debd256d
JB
15142}
15143
debd256d 15144/* Add an entry to LH's include directory table. */
ae2de4f8 15145
debd256d 15146static void
d521ce57 15147add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15148{
debd256d
JB
15149 /* Grow the array if necessary. */
15150 if (lh->include_dirs_size == 0)
c5aa993b 15151 {
debd256d
JB
15152 lh->include_dirs_size = 1; /* for testing */
15153 lh->include_dirs = xmalloc (lh->include_dirs_size
15154 * sizeof (*lh->include_dirs));
15155 }
15156 else if (lh->num_include_dirs >= lh->include_dirs_size)
15157 {
15158 lh->include_dirs_size *= 2;
15159 lh->include_dirs = xrealloc (lh->include_dirs,
15160 (lh->include_dirs_size
15161 * sizeof (*lh->include_dirs)));
c5aa993b 15162 }
c906108c 15163
debd256d
JB
15164 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15165}
6e70227d 15166
debd256d 15167/* Add an entry to LH's file name table. */
ae2de4f8 15168
debd256d
JB
15169static void
15170add_file_name (struct line_header *lh,
d521ce57 15171 const char *name,
debd256d
JB
15172 unsigned int dir_index,
15173 unsigned int mod_time,
15174 unsigned int length)
15175{
15176 struct file_entry *fe;
15177
15178 /* Grow the array if necessary. */
15179 if (lh->file_names_size == 0)
15180 {
15181 lh->file_names_size = 1; /* for testing */
15182 lh->file_names = xmalloc (lh->file_names_size
15183 * sizeof (*lh->file_names));
15184 }
15185 else if (lh->num_file_names >= lh->file_names_size)
15186 {
15187 lh->file_names_size *= 2;
15188 lh->file_names = xrealloc (lh->file_names,
15189 (lh->file_names_size
15190 * sizeof (*lh->file_names)));
15191 }
15192
15193 fe = &lh->file_names[lh->num_file_names++];
15194 fe->name = name;
15195 fe->dir_index = dir_index;
15196 fe->mod_time = mod_time;
15197 fe->length = length;
aaa75496 15198 fe->included_p = 0;
cb1df416 15199 fe->symtab = NULL;
debd256d 15200}
6e70227d 15201
36586728
TT
15202/* A convenience function to find the proper .debug_line section for a
15203 CU. */
15204
15205static struct dwarf2_section_info *
15206get_debug_line_section (struct dwarf2_cu *cu)
15207{
15208 struct dwarf2_section_info *section;
15209
15210 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15211 DWO file. */
15212 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15213 section = &cu->dwo_unit->dwo_file->sections.line;
15214 else if (cu->per_cu->is_dwz)
15215 {
15216 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15217
15218 section = &dwz->line;
15219 }
15220 else
15221 section = &dwarf2_per_objfile->line;
15222
15223 return section;
15224}
15225
debd256d 15226/* Read the statement program header starting at OFFSET in
3019eac3 15227 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15228 to a struct line_header, allocated using xmalloc.
debd256d
JB
15229
15230 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15231 the returned object point into the dwarf line section buffer,
15232 and must not be freed. */
ae2de4f8 15233
debd256d 15234static struct line_header *
3019eac3 15235dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15236{
15237 struct cleanup *back_to;
15238 struct line_header *lh;
d521ce57 15239 const gdb_byte *line_ptr;
c764a876 15240 unsigned int bytes_read, offset_size;
debd256d 15241 int i;
d521ce57 15242 const char *cur_dir, *cur_file;
3019eac3
DE
15243 struct dwarf2_section_info *section;
15244 bfd *abfd;
15245
36586728 15246 section = get_debug_line_section (cu);
3019eac3
DE
15247 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15248 if (section->buffer == NULL)
debd256d 15249 {
3019eac3
DE
15250 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15251 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15252 else
15253 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15254 return 0;
15255 }
15256
fceca515
DE
15257 /* We can't do this until we know the section is non-empty.
15258 Only then do we know we have such a section. */
15259 abfd = section->asection->owner;
15260
a738430d
MK
15261 /* Make sure that at least there's room for the total_length field.
15262 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15263 if (offset + 4 >= section->size)
debd256d 15264 {
4d3c2250 15265 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15266 return 0;
15267 }
15268
15269 lh = xmalloc (sizeof (*lh));
15270 memset (lh, 0, sizeof (*lh));
15271 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15272 (void *) lh);
15273
3019eac3 15274 line_ptr = section->buffer + offset;
debd256d 15275
a738430d 15276 /* Read in the header. */
6e70227d 15277 lh->total_length =
c764a876
DE
15278 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15279 &bytes_read, &offset_size);
debd256d 15280 line_ptr += bytes_read;
3019eac3 15281 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15282 {
4d3c2250 15283 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15284 return 0;
15285 }
15286 lh->statement_program_end = line_ptr + lh->total_length;
15287 lh->version = read_2_bytes (abfd, line_ptr);
15288 line_ptr += 2;
c764a876
DE
15289 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15290 line_ptr += offset_size;
debd256d
JB
15291 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15292 line_ptr += 1;
2dc7f7b3
TT
15293 if (lh->version >= 4)
15294 {
15295 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15296 line_ptr += 1;
15297 }
15298 else
15299 lh->maximum_ops_per_instruction = 1;
15300
15301 if (lh->maximum_ops_per_instruction == 0)
15302 {
15303 lh->maximum_ops_per_instruction = 1;
15304 complaint (&symfile_complaints,
3e43a32a
MS
15305 _("invalid maximum_ops_per_instruction "
15306 "in `.debug_line' section"));
2dc7f7b3
TT
15307 }
15308
debd256d
JB
15309 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15310 line_ptr += 1;
15311 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15312 line_ptr += 1;
15313 lh->line_range = read_1_byte (abfd, line_ptr);
15314 line_ptr += 1;
15315 lh->opcode_base = read_1_byte (abfd, line_ptr);
15316 line_ptr += 1;
15317 lh->standard_opcode_lengths
fe1b8b76 15318 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15319
15320 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15321 for (i = 1; i < lh->opcode_base; ++i)
15322 {
15323 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15324 line_ptr += 1;
15325 }
15326
a738430d 15327 /* Read directory table. */
9b1c24c8 15328 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15329 {
15330 line_ptr += bytes_read;
15331 add_include_dir (lh, cur_dir);
15332 }
15333 line_ptr += bytes_read;
15334
a738430d 15335 /* Read file name table. */
9b1c24c8 15336 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15337 {
15338 unsigned int dir_index, mod_time, length;
15339
15340 line_ptr += bytes_read;
15341 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15342 line_ptr += bytes_read;
15343 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15344 line_ptr += bytes_read;
15345 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15346 line_ptr += bytes_read;
15347
15348 add_file_name (lh, cur_file, dir_index, mod_time, length);
15349 }
15350 line_ptr += bytes_read;
6e70227d 15351 lh->statement_program_start = line_ptr;
debd256d 15352
3019eac3 15353 if (line_ptr > (section->buffer + section->size))
4d3c2250 15354 complaint (&symfile_complaints,
3e43a32a
MS
15355 _("line number info header doesn't "
15356 "fit in `.debug_line' section"));
debd256d
JB
15357
15358 discard_cleanups (back_to);
15359 return lh;
15360}
c906108c 15361
c6da4cef
DE
15362/* Subroutine of dwarf_decode_lines to simplify it.
15363 Return the file name of the psymtab for included file FILE_INDEX
15364 in line header LH of PST.
15365 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15366 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15367 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15368
15369 The function creates dangling cleanup registration. */
c6da4cef 15370
d521ce57 15371static const char *
c6da4cef
DE
15372psymtab_include_file_name (const struct line_header *lh, int file_index,
15373 const struct partial_symtab *pst,
15374 const char *comp_dir)
15375{
15376 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15377 const char *include_name = fe.name;
15378 const char *include_name_to_compare = include_name;
15379 const char *dir_name = NULL;
72b9f47f
TT
15380 const char *pst_filename;
15381 char *copied_name = NULL;
c6da4cef
DE
15382 int file_is_pst;
15383
15384 if (fe.dir_index)
15385 dir_name = lh->include_dirs[fe.dir_index - 1];
15386
15387 if (!IS_ABSOLUTE_PATH (include_name)
15388 && (dir_name != NULL || comp_dir != NULL))
15389 {
15390 /* Avoid creating a duplicate psymtab for PST.
15391 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15392 Before we do the comparison, however, we need to account
15393 for DIR_NAME and COMP_DIR.
15394 First prepend dir_name (if non-NULL). If we still don't
15395 have an absolute path prepend comp_dir (if non-NULL).
15396 However, the directory we record in the include-file's
15397 psymtab does not contain COMP_DIR (to match the
15398 corresponding symtab(s)).
15399
15400 Example:
15401
15402 bash$ cd /tmp
15403 bash$ gcc -g ./hello.c
15404 include_name = "hello.c"
15405 dir_name = "."
15406 DW_AT_comp_dir = comp_dir = "/tmp"
15407 DW_AT_name = "./hello.c" */
15408
15409 if (dir_name != NULL)
15410 {
d521ce57
TT
15411 char *tem = concat (dir_name, SLASH_STRING,
15412 include_name, (char *)NULL);
15413
15414 make_cleanup (xfree, tem);
15415 include_name = tem;
c6da4cef 15416 include_name_to_compare = include_name;
c6da4cef
DE
15417 }
15418 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15419 {
d521ce57
TT
15420 char *tem = concat (comp_dir, SLASH_STRING,
15421 include_name, (char *)NULL);
15422
15423 make_cleanup (xfree, tem);
15424 include_name_to_compare = tem;
c6da4cef
DE
15425 }
15426 }
15427
15428 pst_filename = pst->filename;
15429 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15430 {
72b9f47f
TT
15431 copied_name = concat (pst->dirname, SLASH_STRING,
15432 pst_filename, (char *)NULL);
15433 pst_filename = copied_name;
c6da4cef
DE
15434 }
15435
1e3fad37 15436 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15437
72b9f47f
TT
15438 if (copied_name != NULL)
15439 xfree (copied_name);
c6da4cef
DE
15440
15441 if (file_is_pst)
15442 return NULL;
15443 return include_name;
15444}
15445
c91513d8
PP
15446/* Ignore this record_line request. */
15447
15448static void
15449noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15450{
15451 return;
15452}
15453
f3f5162e
DE
15454/* Subroutine of dwarf_decode_lines to simplify it.
15455 Process the line number information in LH. */
debd256d 15456
c906108c 15457static void
f3f5162e
DE
15458dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15459 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15460{
d521ce57
TT
15461 const gdb_byte *line_ptr, *extended_end;
15462 const gdb_byte *line_end;
a8c50c1f 15463 unsigned int bytes_read, extended_len;
c906108c 15464 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15465 CORE_ADDR baseaddr;
15466 struct objfile *objfile = cu->objfile;
f3f5162e 15467 bfd *abfd = objfile->obfd;
fbf65064 15468 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15469 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15470 struct subfile *last_subfile = NULL;
c91513d8
PP
15471 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15472 = record_line;
e142c38c
DJ
15473
15474 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15475
debd256d
JB
15476 line_ptr = lh->statement_program_start;
15477 line_end = lh->statement_program_end;
c906108c
SS
15478
15479 /* Read the statement sequences until there's nothing left. */
15480 while (line_ptr < line_end)
15481 {
15482 /* state machine registers */
15483 CORE_ADDR address = 0;
15484 unsigned int file = 1;
15485 unsigned int line = 1;
15486 unsigned int column = 0;
debd256d 15487 int is_stmt = lh->default_is_stmt;
c906108c
SS
15488 int basic_block = 0;
15489 int end_sequence = 0;
fbf65064 15490 CORE_ADDR addr;
2dc7f7b3 15491 unsigned char op_index = 0;
c906108c 15492
aaa75496 15493 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15494 {
aaa75496 15495 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15496 /* lh->include_dirs and lh->file_names are 0-based, but the
15497 directory and file name numbers in the statement program
15498 are 1-based. */
15499 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15500 const char *dir = NULL;
a738430d 15501
debd256d
JB
15502 if (fe->dir_index)
15503 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15504
15505 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15506 }
15507
a738430d 15508 /* Decode the table. */
c5aa993b 15509 while (!end_sequence)
c906108c
SS
15510 {
15511 op_code = read_1_byte (abfd, line_ptr);
15512 line_ptr += 1;
59205f5a
JB
15513 if (line_ptr > line_end)
15514 {
15515 dwarf2_debug_line_missing_end_sequence_complaint ();
15516 break;
15517 }
9aa1fe7e 15518
debd256d 15519 if (op_code >= lh->opcode_base)
6e70227d 15520 {
a738430d 15521 /* Special operand. */
debd256d 15522 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15523 address += (((op_index + (adj_opcode / lh->line_range))
15524 / lh->maximum_ops_per_instruction)
15525 * lh->minimum_instruction_length);
15526 op_index = ((op_index + (adj_opcode / lh->line_range))
15527 % lh->maximum_ops_per_instruction);
debd256d 15528 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15529 if (lh->num_file_names < file || file == 0)
25e43795 15530 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15531 /* For now we ignore lines not starting on an
15532 instruction boundary. */
15533 else if (op_index == 0)
25e43795
DJ
15534 {
15535 lh->file_names[file - 1].included_p = 1;
ca5f395d 15536 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15537 {
15538 if (last_subfile != current_subfile)
15539 {
15540 addr = gdbarch_addr_bits_remove (gdbarch, address);
15541 if (last_subfile)
c91513d8 15542 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15543 last_subfile = current_subfile;
15544 }
25e43795 15545 /* Append row to matrix using current values. */
7019d805 15546 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15547 (*p_record_line) (current_subfile, line, addr);
366da635 15548 }
25e43795 15549 }
ca5f395d 15550 basic_block = 0;
9aa1fe7e
GK
15551 }
15552 else switch (op_code)
c906108c
SS
15553 {
15554 case DW_LNS_extended_op:
3e43a32a
MS
15555 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15556 &bytes_read);
473b7be6 15557 line_ptr += bytes_read;
a8c50c1f 15558 extended_end = line_ptr + extended_len;
c906108c
SS
15559 extended_op = read_1_byte (abfd, line_ptr);
15560 line_ptr += 1;
15561 switch (extended_op)
15562 {
15563 case DW_LNE_end_sequence:
c91513d8 15564 p_record_line = record_line;
c906108c 15565 end_sequence = 1;
c906108c
SS
15566 break;
15567 case DW_LNE_set_address:
e7c27a73 15568 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15569
15570 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15571 {
15572 /* This line table is for a function which has been
15573 GCd by the linker. Ignore it. PR gdb/12528 */
15574
15575 long line_offset
36586728 15576 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15577
15578 complaint (&symfile_complaints,
15579 _(".debug_line address at offset 0x%lx is 0 "
15580 "[in module %s]"),
bb5ed363 15581 line_offset, objfile->name);
c91513d8
PP
15582 p_record_line = noop_record_line;
15583 }
15584
2dc7f7b3 15585 op_index = 0;
107d2387
AC
15586 line_ptr += bytes_read;
15587 address += baseaddr;
c906108c
SS
15588 break;
15589 case DW_LNE_define_file:
debd256d 15590 {
d521ce57 15591 const char *cur_file;
debd256d 15592 unsigned int dir_index, mod_time, length;
6e70227d 15593
3e43a32a
MS
15594 cur_file = read_direct_string (abfd, line_ptr,
15595 &bytes_read);
debd256d
JB
15596 line_ptr += bytes_read;
15597 dir_index =
15598 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15599 line_ptr += bytes_read;
15600 mod_time =
15601 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15602 line_ptr += bytes_read;
15603 length =
15604 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15605 line_ptr += bytes_read;
15606 add_file_name (lh, cur_file, dir_index, mod_time, length);
15607 }
c906108c 15608 break;
d0c6ba3d
CC
15609 case DW_LNE_set_discriminator:
15610 /* The discriminator is not interesting to the debugger;
15611 just ignore it. */
15612 line_ptr = extended_end;
15613 break;
c906108c 15614 default:
4d3c2250 15615 complaint (&symfile_complaints,
e2e0b3e5 15616 _("mangled .debug_line section"));
debd256d 15617 return;
c906108c 15618 }
a8c50c1f
DJ
15619 /* Make sure that we parsed the extended op correctly. If e.g.
15620 we expected a different address size than the producer used,
15621 we may have read the wrong number of bytes. */
15622 if (line_ptr != extended_end)
15623 {
15624 complaint (&symfile_complaints,
15625 _("mangled .debug_line section"));
15626 return;
15627 }
c906108c
SS
15628 break;
15629 case DW_LNS_copy:
59205f5a 15630 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15631 dwarf2_debug_line_missing_file_complaint ();
15632 else
366da635 15633 {
25e43795 15634 lh->file_names[file - 1].included_p = 1;
ca5f395d 15635 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15636 {
15637 if (last_subfile != current_subfile)
15638 {
15639 addr = gdbarch_addr_bits_remove (gdbarch, address);
15640 if (last_subfile)
c91513d8 15641 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15642 last_subfile = current_subfile;
15643 }
7019d805 15644 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15645 (*p_record_line) (current_subfile, line, addr);
fbf65064 15646 }
366da635 15647 }
c906108c
SS
15648 basic_block = 0;
15649 break;
15650 case DW_LNS_advance_pc:
2dc7f7b3
TT
15651 {
15652 CORE_ADDR adjust
15653 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15654
15655 address += (((op_index + adjust)
15656 / lh->maximum_ops_per_instruction)
15657 * lh->minimum_instruction_length);
15658 op_index = ((op_index + adjust)
15659 % lh->maximum_ops_per_instruction);
15660 line_ptr += bytes_read;
15661 }
c906108c
SS
15662 break;
15663 case DW_LNS_advance_line:
15664 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15665 line_ptr += bytes_read;
15666 break;
15667 case DW_LNS_set_file:
debd256d 15668 {
a738430d
MK
15669 /* The arrays lh->include_dirs and lh->file_names are
15670 0-based, but the directory and file name numbers in
15671 the statement program are 1-based. */
debd256d 15672 struct file_entry *fe;
d521ce57 15673 const char *dir = NULL;
a738430d 15674
debd256d
JB
15675 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15676 line_ptr += bytes_read;
59205f5a 15677 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15678 dwarf2_debug_line_missing_file_complaint ();
15679 else
15680 {
15681 fe = &lh->file_names[file - 1];
15682 if (fe->dir_index)
15683 dir = lh->include_dirs[fe->dir_index - 1];
15684 if (!decode_for_pst_p)
15685 {
15686 last_subfile = current_subfile;
15687 dwarf2_start_subfile (fe->name, dir, comp_dir);
15688 }
15689 }
debd256d 15690 }
c906108c
SS
15691 break;
15692 case DW_LNS_set_column:
15693 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15694 line_ptr += bytes_read;
15695 break;
15696 case DW_LNS_negate_stmt:
15697 is_stmt = (!is_stmt);
15698 break;
15699 case DW_LNS_set_basic_block:
15700 basic_block = 1;
15701 break;
c2c6d25f
JM
15702 /* Add to the address register of the state machine the
15703 address increment value corresponding to special opcode
a738430d
MK
15704 255. I.e., this value is scaled by the minimum
15705 instruction length since special opcode 255 would have
b021a221 15706 scaled the increment. */
c906108c 15707 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15708 {
15709 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15710
15711 address += (((op_index + adjust)
15712 / lh->maximum_ops_per_instruction)
15713 * lh->minimum_instruction_length);
15714 op_index = ((op_index + adjust)
15715 % lh->maximum_ops_per_instruction);
15716 }
c906108c
SS
15717 break;
15718 case DW_LNS_fixed_advance_pc:
15719 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15720 op_index = 0;
c906108c
SS
15721 line_ptr += 2;
15722 break;
9aa1fe7e 15723 default:
a738430d
MK
15724 {
15725 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15726 int i;
a738430d 15727
debd256d 15728 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15729 {
15730 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15731 line_ptr += bytes_read;
15732 }
15733 }
c906108c
SS
15734 }
15735 }
59205f5a
JB
15736 if (lh->num_file_names < file || file == 0)
15737 dwarf2_debug_line_missing_file_complaint ();
15738 else
15739 {
15740 lh->file_names[file - 1].included_p = 1;
15741 if (!decode_for_pst_p)
fbf65064
UW
15742 {
15743 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15744 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15745 }
59205f5a 15746 }
c906108c 15747 }
f3f5162e
DE
15748}
15749
15750/* Decode the Line Number Program (LNP) for the given line_header
15751 structure and CU. The actual information extracted and the type
15752 of structures created from the LNP depends on the value of PST.
15753
15754 1. If PST is NULL, then this procedure uses the data from the program
15755 to create all necessary symbol tables, and their linetables.
15756
15757 2. If PST is not NULL, this procedure reads the program to determine
15758 the list of files included by the unit represented by PST, and
15759 builds all the associated partial symbol tables.
15760
15761 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15762 It is used for relative paths in the line table.
15763 NOTE: When processing partial symtabs (pst != NULL),
15764 comp_dir == pst->dirname.
15765
15766 NOTE: It is important that psymtabs have the same file name (via strcmp)
15767 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15768 symtab we don't use it in the name of the psymtabs we create.
15769 E.g. expand_line_sal requires this when finding psymtabs to expand.
15770 A good testcase for this is mb-inline.exp. */
15771
15772static void
15773dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15774 struct dwarf2_cu *cu, struct partial_symtab *pst,
15775 int want_line_info)
15776{
15777 struct objfile *objfile = cu->objfile;
15778 const int decode_for_pst_p = (pst != NULL);
15779 struct subfile *first_subfile = current_subfile;
15780
15781 if (want_line_info)
15782 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15783
15784 if (decode_for_pst_p)
15785 {
15786 int file_index;
15787
15788 /* Now that we're done scanning the Line Header Program, we can
15789 create the psymtab of each included file. */
15790 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15791 if (lh->file_names[file_index].included_p == 1)
15792 {
d521ce57 15793 const char *include_name =
c6da4cef
DE
15794 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15795 if (include_name != NULL)
aaa75496
JB
15796 dwarf2_create_include_psymtab (include_name, pst, objfile);
15797 }
15798 }
cb1df416
DJ
15799 else
15800 {
15801 /* Make sure a symtab is created for every file, even files
15802 which contain only variables (i.e. no code with associated
15803 line numbers). */
cb1df416 15804 int i;
cb1df416
DJ
15805
15806 for (i = 0; i < lh->num_file_names; i++)
15807 {
d521ce57 15808 const char *dir = NULL;
f3f5162e 15809 struct file_entry *fe;
9a619af0 15810
cb1df416
DJ
15811 fe = &lh->file_names[i];
15812 if (fe->dir_index)
15813 dir = lh->include_dirs[fe->dir_index - 1];
15814 dwarf2_start_subfile (fe->name, dir, comp_dir);
15815
15816 /* Skip the main file; we don't need it, and it must be
15817 allocated last, so that it will show up before the
15818 non-primary symtabs in the objfile's symtab list. */
15819 if (current_subfile == first_subfile)
15820 continue;
15821
15822 if (current_subfile->symtab == NULL)
15823 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15824 objfile);
cb1df416
DJ
15825 fe->symtab = current_subfile->symtab;
15826 }
15827 }
c906108c
SS
15828}
15829
15830/* Start a subfile for DWARF. FILENAME is the name of the file and
15831 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15832 or NULL if not known. COMP_DIR is the compilation directory for the
15833 linetable's compilation unit or NULL if not known.
c906108c
SS
15834 This routine tries to keep line numbers from identical absolute and
15835 relative file names in a common subfile.
15836
15837 Using the `list' example from the GDB testsuite, which resides in
15838 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15839 of /srcdir/list0.c yields the following debugging information for list0.c:
15840
c5aa993b
JM
15841 DW_AT_name: /srcdir/list0.c
15842 DW_AT_comp_dir: /compdir
357e46e7 15843 files.files[0].name: list0.h
c5aa993b 15844 files.files[0].dir: /srcdir
357e46e7 15845 files.files[1].name: list0.c
c5aa993b 15846 files.files[1].dir: /srcdir
c906108c
SS
15847
15848 The line number information for list0.c has to end up in a single
4f1520fb
FR
15849 subfile, so that `break /srcdir/list0.c:1' works as expected.
15850 start_subfile will ensure that this happens provided that we pass the
15851 concatenation of files.files[1].dir and files.files[1].name as the
15852 subfile's name. */
c906108c
SS
15853
15854static void
d521ce57 15855dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 15856 const char *comp_dir)
c906108c 15857{
d521ce57 15858 char *copy = NULL;
4f1520fb
FR
15859
15860 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15861 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15862 second argument to start_subfile. To be consistent, we do the
15863 same here. In order not to lose the line information directory,
15864 we concatenate it to the filename when it makes sense.
15865 Note that the Dwarf3 standard says (speaking of filenames in line
15866 information): ``The directory index is ignored for file names
15867 that represent full path names''. Thus ignoring dirname in the
15868 `else' branch below isn't an issue. */
c906108c 15869
d5166ae1 15870 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
15871 {
15872 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15873 filename = copy;
15874 }
c906108c 15875
d521ce57 15876 start_subfile (filename, comp_dir);
4f1520fb 15877
d521ce57
TT
15878 if (copy != NULL)
15879 xfree (copy);
c906108c
SS
15880}
15881
f4dc4d17
DE
15882/* Start a symtab for DWARF.
15883 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15884
15885static void
15886dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15887 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15888{
15889 start_symtab (name, comp_dir, low_pc);
15890 record_debugformat ("DWARF 2");
15891 record_producer (cu->producer);
15892
15893 /* We assume that we're processing GCC output. */
15894 processing_gcc_compilation = 2;
15895
4d4ec4e5 15896 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15897}
15898
4c2df51b
DJ
15899static void
15900var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15901 struct dwarf2_cu *cu)
4c2df51b 15902{
e7c27a73
DJ
15903 struct objfile *objfile = cu->objfile;
15904 struct comp_unit_head *cu_header = &cu->header;
15905
4c2df51b
DJ
15906 /* NOTE drow/2003-01-30: There used to be a comment and some special
15907 code here to turn a symbol with DW_AT_external and a
15908 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15909 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15910 with some versions of binutils) where shared libraries could have
15911 relocations against symbols in their debug information - the
15912 minimal symbol would have the right address, but the debug info
15913 would not. It's no longer necessary, because we will explicitly
15914 apply relocations when we read in the debug information now. */
15915
15916 /* A DW_AT_location attribute with no contents indicates that a
15917 variable has been optimized away. */
15918 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15919 {
f1e6e072 15920 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15921 return;
15922 }
15923
15924 /* Handle one degenerate form of location expression specially, to
15925 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15926 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15927 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15928
15929 if (attr_form_is_block (attr)
3019eac3
DE
15930 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15931 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15932 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15933 && (DW_BLOCK (attr)->size
15934 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15935 {
891d2f0b 15936 unsigned int dummy;
4c2df51b 15937
3019eac3
DE
15938 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15939 SYMBOL_VALUE_ADDRESS (sym) =
15940 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15941 else
15942 SYMBOL_VALUE_ADDRESS (sym) =
15943 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15944 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15945 fixup_symbol_section (sym, objfile);
15946 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15947 SYMBOL_SECTION (sym));
4c2df51b
DJ
15948 return;
15949 }
15950
15951 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15952 expression evaluator, and use LOC_COMPUTED only when necessary
15953 (i.e. when the value of a register or memory location is
15954 referenced, or a thread-local block, etc.). Then again, it might
15955 not be worthwhile. I'm assuming that it isn't unless performance
15956 or memory numbers show me otherwise. */
15957
f1e6e072 15958 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15959
f1e6e072 15960 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15961 cu->has_loclist = 1;
4c2df51b
DJ
15962}
15963
c906108c
SS
15964/* Given a pointer to a DWARF information entry, figure out if we need
15965 to make a symbol table entry for it, and if so, create a new entry
15966 and return a pointer to it.
15967 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15968 used the passed type.
15969 If SPACE is not NULL, use it to hold the new symbol. If it is
15970 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15971
15972static struct symbol *
34eaf542
TT
15973new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15974 struct symbol *space)
c906108c 15975{
e7c27a73 15976 struct objfile *objfile = cu->objfile;
c906108c 15977 struct symbol *sym = NULL;
15d034d0 15978 const char *name;
c906108c
SS
15979 struct attribute *attr = NULL;
15980 struct attribute *attr2 = NULL;
e142c38c 15981 CORE_ADDR baseaddr;
e37fd15a
SW
15982 struct pending **list_to_add = NULL;
15983
edb3359d 15984 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15985
15986 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15987
94af9270 15988 name = dwarf2_name (die, cu);
c906108c
SS
15989 if (name)
15990 {
94af9270 15991 const char *linkagename;
34eaf542 15992 int suppress_add = 0;
94af9270 15993
34eaf542
TT
15994 if (space)
15995 sym = space;
15996 else
e623cf5d 15997 sym = allocate_symbol (objfile);
c906108c 15998 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15999
16000 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16001 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16002 linkagename = dwarf2_physname (name, die, cu);
16003 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16004
f55ee35c
JK
16005 /* Fortran does not have mangling standard and the mangling does differ
16006 between gfortran, iFort etc. */
16007 if (cu->language == language_fortran
b250c185 16008 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16009 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16010 dwarf2_full_name (name, die, cu),
29df156d 16011 NULL);
f55ee35c 16012
c906108c 16013 /* Default assumptions.
c5aa993b 16014 Use the passed type or decode it from the die. */
176620f1 16015 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16016 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16017 if (type != NULL)
16018 SYMBOL_TYPE (sym) = type;
16019 else
e7c27a73 16020 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16021 attr = dwarf2_attr (die,
16022 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16023 cu);
c906108c
SS
16024 if (attr)
16025 {
16026 SYMBOL_LINE (sym) = DW_UNSND (attr);
16027 }
cb1df416 16028
edb3359d
DJ
16029 attr = dwarf2_attr (die,
16030 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16031 cu);
cb1df416
DJ
16032 if (attr)
16033 {
16034 int file_index = DW_UNSND (attr);
9a619af0 16035
cb1df416
DJ
16036 if (cu->line_header == NULL
16037 || file_index > cu->line_header->num_file_names)
16038 complaint (&symfile_complaints,
16039 _("file index out of range"));
1c3d648d 16040 else if (file_index > 0)
cb1df416
DJ
16041 {
16042 struct file_entry *fe;
9a619af0 16043
cb1df416
DJ
16044 fe = &cu->line_header->file_names[file_index - 1];
16045 SYMBOL_SYMTAB (sym) = fe->symtab;
16046 }
16047 }
16048
c906108c
SS
16049 switch (die->tag)
16050 {
16051 case DW_TAG_label:
e142c38c 16052 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16053 if (attr)
16054 {
16055 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16056 }
0f5238ed
TT
16057 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16058 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16059 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16060 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16061 break;
16062 case DW_TAG_subprogram:
16063 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16064 finish_block. */
f1e6e072 16065 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16066 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16067 if ((attr2 && (DW_UNSND (attr2) != 0))
16068 || cu->language == language_ada)
c906108c 16069 {
2cfa0c8d
JB
16070 /* Subprograms marked external are stored as a global symbol.
16071 Ada subprograms, whether marked external or not, are always
16072 stored as a global symbol, because we want to be able to
16073 access them globally. For instance, we want to be able
16074 to break on a nested subprogram without having to
16075 specify the context. */
e37fd15a 16076 list_to_add = &global_symbols;
c906108c
SS
16077 }
16078 else
16079 {
e37fd15a 16080 list_to_add = cu->list_in_scope;
c906108c
SS
16081 }
16082 break;
edb3359d
DJ
16083 case DW_TAG_inlined_subroutine:
16084 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16085 finish_block. */
f1e6e072 16086 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16087 SYMBOL_INLINED (sym) = 1;
481860b3 16088 list_to_add = cu->list_in_scope;
edb3359d 16089 break;
34eaf542
TT
16090 case DW_TAG_template_value_param:
16091 suppress_add = 1;
16092 /* Fall through. */
72929c62 16093 case DW_TAG_constant:
c906108c 16094 case DW_TAG_variable:
254e6b9e 16095 case DW_TAG_member:
0963b4bd
MS
16096 /* Compilation with minimal debug info may result in
16097 variables with missing type entries. Change the
16098 misleading `void' type to something sensible. */
c906108c 16099 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16100 SYMBOL_TYPE (sym)
46bf5051 16101 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16102
e142c38c 16103 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16104 /* In the case of DW_TAG_member, we should only be called for
16105 static const members. */
16106 if (die->tag == DW_TAG_member)
16107 {
3863f96c
DE
16108 /* dwarf2_add_field uses die_is_declaration,
16109 so we do the same. */
254e6b9e
DE
16110 gdb_assert (die_is_declaration (die, cu));
16111 gdb_assert (attr);
16112 }
c906108c
SS
16113 if (attr)
16114 {
e7c27a73 16115 dwarf2_const_value (attr, sym, cu);
e142c38c 16116 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16117 if (!suppress_add)
34eaf542
TT
16118 {
16119 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16120 list_to_add = &global_symbols;
34eaf542 16121 else
e37fd15a 16122 list_to_add = cu->list_in_scope;
34eaf542 16123 }
c906108c
SS
16124 break;
16125 }
e142c38c 16126 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16127 if (attr)
16128 {
e7c27a73 16129 var_decode_location (attr, sym, cu);
e142c38c 16130 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16131
16132 /* Fortran explicitly imports any global symbols to the local
16133 scope by DW_TAG_common_block. */
16134 if (cu->language == language_fortran && die->parent
16135 && die->parent->tag == DW_TAG_common_block)
16136 attr2 = NULL;
16137
caac4577
JG
16138 if (SYMBOL_CLASS (sym) == LOC_STATIC
16139 && SYMBOL_VALUE_ADDRESS (sym) == 0
16140 && !dwarf2_per_objfile->has_section_at_zero)
16141 {
16142 /* When a static variable is eliminated by the linker,
16143 the corresponding debug information is not stripped
16144 out, but the variable address is set to null;
16145 do not add such variables into symbol table. */
16146 }
16147 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16148 {
f55ee35c
JK
16149 /* Workaround gfortran PR debug/40040 - it uses
16150 DW_AT_location for variables in -fPIC libraries which may
16151 get overriden by other libraries/executable and get
16152 a different address. Resolve it by the minimal symbol
16153 which may come from inferior's executable using copy
16154 relocation. Make this workaround only for gfortran as for
16155 other compilers GDB cannot guess the minimal symbol
16156 Fortran mangling kind. */
16157 if (cu->language == language_fortran && die->parent
16158 && die->parent->tag == DW_TAG_module
16159 && cu->producer
16160 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16161 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16162
1c809c68
TT
16163 /* A variable with DW_AT_external is never static,
16164 but it may be block-scoped. */
16165 list_to_add = (cu->list_in_scope == &file_symbols
16166 ? &global_symbols : cu->list_in_scope);
1c809c68 16167 }
c906108c 16168 else
e37fd15a 16169 list_to_add = cu->list_in_scope;
c906108c
SS
16170 }
16171 else
16172 {
16173 /* We do not know the address of this symbol.
c5aa993b
JM
16174 If it is an external symbol and we have type information
16175 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16176 The address of the variable will then be determined from
16177 the minimal symbol table whenever the variable is
16178 referenced. */
e142c38c 16179 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16180
16181 /* Fortran explicitly imports any global symbols to the local
16182 scope by DW_TAG_common_block. */
16183 if (cu->language == language_fortran && die->parent
16184 && die->parent->tag == DW_TAG_common_block)
16185 {
16186 /* SYMBOL_CLASS doesn't matter here because
16187 read_common_block is going to reset it. */
16188 if (!suppress_add)
16189 list_to_add = cu->list_in_scope;
16190 }
16191 else if (attr2 && (DW_UNSND (attr2) != 0)
16192 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16193 {
0fe7935b
DJ
16194 /* A variable with DW_AT_external is never static, but it
16195 may be block-scoped. */
16196 list_to_add = (cu->list_in_scope == &file_symbols
16197 ? &global_symbols : cu->list_in_scope);
16198
f1e6e072 16199 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16200 }
442ddf59
JK
16201 else if (!die_is_declaration (die, cu))
16202 {
16203 /* Use the default LOC_OPTIMIZED_OUT class. */
16204 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16205 if (!suppress_add)
16206 list_to_add = cu->list_in_scope;
442ddf59 16207 }
c906108c
SS
16208 }
16209 break;
16210 case DW_TAG_formal_parameter:
edb3359d
DJ
16211 /* If we are inside a function, mark this as an argument. If
16212 not, we might be looking at an argument to an inlined function
16213 when we do not have enough information to show inlined frames;
16214 pretend it's a local variable in that case so that the user can
16215 still see it. */
16216 if (context_stack_depth > 0
16217 && context_stack[context_stack_depth - 1].name != NULL)
16218 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16219 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16220 if (attr)
16221 {
e7c27a73 16222 var_decode_location (attr, sym, cu);
c906108c 16223 }
e142c38c 16224 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16225 if (attr)
16226 {
e7c27a73 16227 dwarf2_const_value (attr, sym, cu);
c906108c 16228 }
f346a30d 16229
e37fd15a 16230 list_to_add = cu->list_in_scope;
c906108c
SS
16231 break;
16232 case DW_TAG_unspecified_parameters:
16233 /* From varargs functions; gdb doesn't seem to have any
16234 interest in this information, so just ignore it for now.
16235 (FIXME?) */
16236 break;
34eaf542
TT
16237 case DW_TAG_template_type_param:
16238 suppress_add = 1;
16239 /* Fall through. */
c906108c 16240 case DW_TAG_class_type:
680b30c7 16241 case DW_TAG_interface_type:
c906108c
SS
16242 case DW_TAG_structure_type:
16243 case DW_TAG_union_type:
72019c9c 16244 case DW_TAG_set_type:
c906108c 16245 case DW_TAG_enumeration_type:
f1e6e072 16246 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16247 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16248
63d06c5c 16249 {
987504bb 16250 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16251 really ever be static objects: otherwise, if you try
16252 to, say, break of a class's method and you're in a file
16253 which doesn't mention that class, it won't work unless
16254 the check for all static symbols in lookup_symbol_aux
16255 saves you. See the OtherFileClass tests in
16256 gdb.c++/namespace.exp. */
16257
e37fd15a 16258 if (!suppress_add)
34eaf542 16259 {
34eaf542
TT
16260 list_to_add = (cu->list_in_scope == &file_symbols
16261 && (cu->language == language_cplus
16262 || cu->language == language_java)
16263 ? &global_symbols : cu->list_in_scope);
63d06c5c 16264
64382290
TT
16265 /* The semantics of C++ state that "struct foo {
16266 ... }" also defines a typedef for "foo". A Java
16267 class declaration also defines a typedef for the
16268 class. */
16269 if (cu->language == language_cplus
16270 || cu->language == language_java
16271 || cu->language == language_ada)
16272 {
16273 /* The symbol's name is already allocated along
16274 with this objfile, so we don't need to
16275 duplicate it for the type. */
16276 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16277 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16278 }
63d06c5c
DC
16279 }
16280 }
c906108c
SS
16281 break;
16282 case DW_TAG_typedef:
f1e6e072 16283 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16284 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16285 list_to_add = cu->list_in_scope;
63d06c5c 16286 break;
c906108c 16287 case DW_TAG_base_type:
a02abb62 16288 case DW_TAG_subrange_type:
f1e6e072 16289 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16290 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16291 list_to_add = cu->list_in_scope;
c906108c
SS
16292 break;
16293 case DW_TAG_enumerator:
e142c38c 16294 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16295 if (attr)
16296 {
e7c27a73 16297 dwarf2_const_value (attr, sym, cu);
c906108c 16298 }
63d06c5c
DC
16299 {
16300 /* NOTE: carlton/2003-11-10: See comment above in the
16301 DW_TAG_class_type, etc. block. */
16302
e142c38c 16303 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16304 && (cu->language == language_cplus
16305 || cu->language == language_java)
e142c38c 16306 ? &global_symbols : cu->list_in_scope);
63d06c5c 16307 }
c906108c 16308 break;
5c4e30ca 16309 case DW_TAG_namespace:
f1e6e072 16310 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16311 list_to_add = &global_symbols;
5c4e30ca 16312 break;
4357ac6c 16313 case DW_TAG_common_block:
f1e6e072 16314 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16315 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16316 add_symbol_to_list (sym, cu->list_in_scope);
16317 break;
c906108c
SS
16318 default:
16319 /* Not a tag we recognize. Hopefully we aren't processing
16320 trash data, but since we must specifically ignore things
16321 we don't recognize, there is nothing else we should do at
0963b4bd 16322 this point. */
e2e0b3e5 16323 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16324 dwarf_tag_name (die->tag));
c906108c
SS
16325 break;
16326 }
df8a16a1 16327
e37fd15a
SW
16328 if (suppress_add)
16329 {
16330 sym->hash_next = objfile->template_symbols;
16331 objfile->template_symbols = sym;
16332 list_to_add = NULL;
16333 }
16334
16335 if (list_to_add != NULL)
16336 add_symbol_to_list (sym, list_to_add);
16337
df8a16a1
DJ
16338 /* For the benefit of old versions of GCC, check for anonymous
16339 namespaces based on the demangled name. */
4d4ec4e5 16340 if (!cu->processing_has_namespace_info
94af9270 16341 && cu->language == language_cplus)
a10964d1 16342 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16343 }
16344 return (sym);
16345}
16346
34eaf542
TT
16347/* A wrapper for new_symbol_full that always allocates a new symbol. */
16348
16349static struct symbol *
16350new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16351{
16352 return new_symbol_full (die, type, cu, NULL);
16353}
16354
98bfdba5
PA
16355/* Given an attr with a DW_FORM_dataN value in host byte order,
16356 zero-extend it as appropriate for the symbol's type. The DWARF
16357 standard (v4) is not entirely clear about the meaning of using
16358 DW_FORM_dataN for a constant with a signed type, where the type is
16359 wider than the data. The conclusion of a discussion on the DWARF
16360 list was that this is unspecified. We choose to always zero-extend
16361 because that is the interpretation long in use by GCC. */
c906108c 16362
98bfdba5
PA
16363static gdb_byte *
16364dwarf2_const_value_data (struct attribute *attr, struct type *type,
16365 const char *name, struct obstack *obstack,
12df843f 16366 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16367{
e7c27a73 16368 struct objfile *objfile = cu->objfile;
e17a4113
UW
16369 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16370 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16371 LONGEST l = DW_UNSND (attr);
16372
16373 if (bits < sizeof (*value) * 8)
16374 {
16375 l &= ((LONGEST) 1 << bits) - 1;
16376 *value = l;
16377 }
16378 else if (bits == sizeof (*value) * 8)
16379 *value = l;
16380 else
16381 {
16382 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16383 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16384 return bytes;
16385 }
16386
16387 return NULL;
16388}
16389
16390/* Read a constant value from an attribute. Either set *VALUE, or if
16391 the value does not fit in *VALUE, set *BYTES - either already
16392 allocated on the objfile obstack, or newly allocated on OBSTACK,
16393 or, set *BATON, if we translated the constant to a location
16394 expression. */
16395
16396static void
16397dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16398 const char *name, struct obstack *obstack,
16399 struct dwarf2_cu *cu,
d521ce57 16400 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16401 struct dwarf2_locexpr_baton **baton)
16402{
16403 struct objfile *objfile = cu->objfile;
16404 struct comp_unit_head *cu_header = &cu->header;
c906108c 16405 struct dwarf_block *blk;
98bfdba5
PA
16406 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16407 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16408
16409 *value = 0;
16410 *bytes = NULL;
16411 *baton = NULL;
c906108c
SS
16412
16413 switch (attr->form)
16414 {
16415 case DW_FORM_addr:
3019eac3 16416 case DW_FORM_GNU_addr_index:
ac56253d 16417 {
ac56253d
TT
16418 gdb_byte *data;
16419
98bfdba5
PA
16420 if (TYPE_LENGTH (type) != cu_header->addr_size)
16421 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16422 cu_header->addr_size,
98bfdba5 16423 TYPE_LENGTH (type));
ac56253d
TT
16424 /* Symbols of this form are reasonably rare, so we just
16425 piggyback on the existing location code rather than writing
16426 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16427 *baton = obstack_alloc (&objfile->objfile_obstack,
16428 sizeof (struct dwarf2_locexpr_baton));
16429 (*baton)->per_cu = cu->per_cu;
16430 gdb_assert ((*baton)->per_cu);
ac56253d 16431
98bfdba5
PA
16432 (*baton)->size = 2 + cu_header->addr_size;
16433 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16434 (*baton)->data = data;
ac56253d
TT
16435
16436 data[0] = DW_OP_addr;
16437 store_unsigned_integer (&data[1], cu_header->addr_size,
16438 byte_order, DW_ADDR (attr));
16439 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16440 }
c906108c 16441 break;
4ac36638 16442 case DW_FORM_string:
93b5768b 16443 case DW_FORM_strp:
3019eac3 16444 case DW_FORM_GNU_str_index:
36586728 16445 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16446 /* DW_STRING is already allocated on the objfile obstack, point
16447 directly to it. */
d521ce57 16448 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16449 break;
c906108c
SS
16450 case DW_FORM_block1:
16451 case DW_FORM_block2:
16452 case DW_FORM_block4:
16453 case DW_FORM_block:
2dc7f7b3 16454 case DW_FORM_exprloc:
c906108c 16455 blk = DW_BLOCK (attr);
98bfdba5
PA
16456 if (TYPE_LENGTH (type) != blk->size)
16457 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16458 TYPE_LENGTH (type));
16459 *bytes = blk->data;
c906108c 16460 break;
2df3850c
JM
16461
16462 /* The DW_AT_const_value attributes are supposed to carry the
16463 symbol's value "represented as it would be on the target
16464 architecture." By the time we get here, it's already been
16465 converted to host endianness, so we just need to sign- or
16466 zero-extend it as appropriate. */
16467 case DW_FORM_data1:
3e43a32a
MS
16468 *bytes = dwarf2_const_value_data (attr, type, name,
16469 obstack, cu, value, 8);
2df3850c 16470 break;
c906108c 16471 case DW_FORM_data2:
3e43a32a
MS
16472 *bytes = dwarf2_const_value_data (attr, type, name,
16473 obstack, cu, value, 16);
2df3850c 16474 break;
c906108c 16475 case DW_FORM_data4:
3e43a32a
MS
16476 *bytes = dwarf2_const_value_data (attr, type, name,
16477 obstack, cu, value, 32);
2df3850c 16478 break;
c906108c 16479 case DW_FORM_data8:
3e43a32a
MS
16480 *bytes = dwarf2_const_value_data (attr, type, name,
16481 obstack, cu, value, 64);
2df3850c
JM
16482 break;
16483
c906108c 16484 case DW_FORM_sdata:
98bfdba5 16485 *value = DW_SND (attr);
2df3850c
JM
16486 break;
16487
c906108c 16488 case DW_FORM_udata:
98bfdba5 16489 *value = DW_UNSND (attr);
c906108c 16490 break;
2df3850c 16491
c906108c 16492 default:
4d3c2250 16493 complaint (&symfile_complaints,
e2e0b3e5 16494 _("unsupported const value attribute form: '%s'"),
4d3c2250 16495 dwarf_form_name (attr->form));
98bfdba5 16496 *value = 0;
c906108c
SS
16497 break;
16498 }
16499}
16500
2df3850c 16501
98bfdba5
PA
16502/* Copy constant value from an attribute to a symbol. */
16503
2df3850c 16504static void
98bfdba5
PA
16505dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16506 struct dwarf2_cu *cu)
2df3850c 16507{
98bfdba5
PA
16508 struct objfile *objfile = cu->objfile;
16509 struct comp_unit_head *cu_header = &cu->header;
12df843f 16510 LONGEST value;
d521ce57 16511 const gdb_byte *bytes;
98bfdba5 16512 struct dwarf2_locexpr_baton *baton;
2df3850c 16513
98bfdba5
PA
16514 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16515 SYMBOL_PRINT_NAME (sym),
16516 &objfile->objfile_obstack, cu,
16517 &value, &bytes, &baton);
2df3850c 16518
98bfdba5
PA
16519 if (baton != NULL)
16520 {
98bfdba5 16521 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16522 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16523 }
16524 else if (bytes != NULL)
16525 {
16526 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16527 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16528 }
16529 else
16530 {
16531 SYMBOL_VALUE (sym) = value;
f1e6e072 16532 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16533 }
2df3850c
JM
16534}
16535
c906108c
SS
16536/* Return the type of the die in question using its DW_AT_type attribute. */
16537
16538static struct type *
e7c27a73 16539die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16540{
c906108c 16541 struct attribute *type_attr;
c906108c 16542
e142c38c 16543 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16544 if (!type_attr)
16545 {
16546 /* A missing DW_AT_type represents a void type. */
46bf5051 16547 return objfile_type (cu->objfile)->builtin_void;
c906108c 16548 }
348e048f 16549
673bfd45 16550 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16551}
16552
b4ba55a1
JB
16553/* True iff CU's producer generates GNAT Ada auxiliary information
16554 that allows to find parallel types through that information instead
16555 of having to do expensive parallel lookups by type name. */
16556
16557static int
16558need_gnat_info (struct dwarf2_cu *cu)
16559{
16560 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16561 of GNAT produces this auxiliary information, without any indication
16562 that it is produced. Part of enhancing the FSF version of GNAT
16563 to produce that information will be to put in place an indicator
16564 that we can use in order to determine whether the descriptive type
16565 info is available or not. One suggestion that has been made is
16566 to use a new attribute, attached to the CU die. For now, assume
16567 that the descriptive type info is not available. */
16568 return 0;
16569}
16570
b4ba55a1
JB
16571/* Return the auxiliary type of the die in question using its
16572 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16573 attribute is not present. */
16574
16575static struct type *
16576die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16577{
b4ba55a1 16578 struct attribute *type_attr;
b4ba55a1
JB
16579
16580 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16581 if (!type_attr)
16582 return NULL;
16583
673bfd45 16584 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16585}
16586
16587/* If DIE has a descriptive_type attribute, then set the TYPE's
16588 descriptive type accordingly. */
16589
16590static void
16591set_descriptive_type (struct type *type, struct die_info *die,
16592 struct dwarf2_cu *cu)
16593{
16594 struct type *descriptive_type = die_descriptive_type (die, cu);
16595
16596 if (descriptive_type)
16597 {
16598 ALLOCATE_GNAT_AUX_TYPE (type);
16599 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16600 }
16601}
16602
c906108c
SS
16603/* Return the containing type of the die in question using its
16604 DW_AT_containing_type attribute. */
16605
16606static struct type *
e7c27a73 16607die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16608{
c906108c 16609 struct attribute *type_attr;
c906108c 16610
e142c38c 16611 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16612 if (!type_attr)
16613 error (_("Dwarf Error: Problem turning containing type into gdb type "
16614 "[in module %s]"), cu->objfile->name);
16615
673bfd45 16616 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16617}
16618
ac9ec31b
DE
16619/* Return an error marker type to use for the ill formed type in DIE/CU. */
16620
16621static struct type *
16622build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
16623{
16624 struct objfile *objfile = dwarf2_per_objfile->objfile;
16625 char *message, *saved;
16626
16627 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16628 objfile->name,
16629 cu->header.offset.sect_off,
16630 die->offset.sect_off);
16631 saved = obstack_copy0 (&objfile->objfile_obstack,
16632 message, strlen (message));
16633 xfree (message);
16634
16635 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16636}
16637
673bfd45 16638/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
16639 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
16640 DW_AT_containing_type.
673bfd45
DE
16641 If there is no type substitute an error marker. */
16642
c906108c 16643static struct type *
673bfd45
DE
16644lookup_die_type (struct die_info *die, struct attribute *attr,
16645 struct dwarf2_cu *cu)
c906108c 16646{
bb5ed363 16647 struct objfile *objfile = cu->objfile;
f792889a
DJ
16648 struct type *this_type;
16649
ac9ec31b
DE
16650 gdb_assert (attr->name == DW_AT_type
16651 || attr->name == DW_AT_GNAT_descriptive_type
16652 || attr->name == DW_AT_containing_type);
16653
673bfd45
DE
16654 /* First see if we have it cached. */
16655
36586728
TT
16656 if (attr->form == DW_FORM_GNU_ref_alt)
16657 {
16658 struct dwarf2_per_cu_data *per_cu;
16659 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16660
16661 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16662 this_type = get_die_type_at_offset (offset, per_cu);
16663 }
16664 else if (is_ref_attr (attr))
673bfd45 16665 {
b64f50a1 16666 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16667
16668 this_type = get_die_type_at_offset (offset, cu->per_cu);
16669 }
55f1336d 16670 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 16671 {
ac9ec31b 16672 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 16673
ac9ec31b 16674 return get_signatured_type (die, signature, cu);
673bfd45
DE
16675 }
16676 else
16677 {
ac9ec31b
DE
16678 complaint (&symfile_complaints,
16679 _("Dwarf Error: Bad type attribute %s in DIE"
16680 " at 0x%x [in module %s]"),
16681 dwarf_attr_name (attr->name), die->offset.sect_off,
16682 objfile->name);
16683 return build_error_marker_type (cu, die);
673bfd45
DE
16684 }
16685
16686 /* If not cached we need to read it in. */
16687
16688 if (this_type == NULL)
16689 {
ac9ec31b 16690 struct die_info *type_die = NULL;
673bfd45
DE
16691 struct dwarf2_cu *type_cu = cu;
16692
ac9ec31b
DE
16693 if (is_ref_attr (attr))
16694 type_die = follow_die_ref (die, attr, &type_cu);
16695 if (type_die == NULL)
16696 return build_error_marker_type (cu, die);
16697 /* If we find the type now, it's probably because the type came
3019eac3
DE
16698 from an inter-CU reference and the type's CU got expanded before
16699 ours. */
ac9ec31b 16700 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
16701 }
16702
16703 /* If we still don't have a type use an error marker. */
16704
16705 if (this_type == NULL)
ac9ec31b 16706 return build_error_marker_type (cu, die);
673bfd45 16707
f792889a 16708 return this_type;
c906108c
SS
16709}
16710
673bfd45
DE
16711/* Return the type in DIE, CU.
16712 Returns NULL for invalid types.
16713
02142a6c 16714 This first does a lookup in die_type_hash,
673bfd45
DE
16715 and only reads the die in if necessary.
16716
16717 NOTE: This can be called when reading in partial or full symbols. */
16718
f792889a 16719static struct type *
e7c27a73 16720read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16721{
f792889a
DJ
16722 struct type *this_type;
16723
16724 this_type = get_die_type (die, cu);
16725 if (this_type)
16726 return this_type;
16727
673bfd45
DE
16728 return read_type_die_1 (die, cu);
16729}
16730
16731/* Read the type in DIE, CU.
16732 Returns NULL for invalid types. */
16733
16734static struct type *
16735read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16736{
16737 struct type *this_type = NULL;
16738
c906108c
SS
16739 switch (die->tag)
16740 {
16741 case DW_TAG_class_type:
680b30c7 16742 case DW_TAG_interface_type:
c906108c
SS
16743 case DW_TAG_structure_type:
16744 case DW_TAG_union_type:
f792889a 16745 this_type = read_structure_type (die, cu);
c906108c
SS
16746 break;
16747 case DW_TAG_enumeration_type:
f792889a 16748 this_type = read_enumeration_type (die, cu);
c906108c
SS
16749 break;
16750 case DW_TAG_subprogram:
16751 case DW_TAG_subroutine_type:
edb3359d 16752 case DW_TAG_inlined_subroutine:
f792889a 16753 this_type = read_subroutine_type (die, cu);
c906108c
SS
16754 break;
16755 case DW_TAG_array_type:
f792889a 16756 this_type = read_array_type (die, cu);
c906108c 16757 break;
72019c9c 16758 case DW_TAG_set_type:
f792889a 16759 this_type = read_set_type (die, cu);
72019c9c 16760 break;
c906108c 16761 case DW_TAG_pointer_type:
f792889a 16762 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16763 break;
16764 case DW_TAG_ptr_to_member_type:
f792889a 16765 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16766 break;
16767 case DW_TAG_reference_type:
f792889a 16768 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16769 break;
16770 case DW_TAG_const_type:
f792889a 16771 this_type = read_tag_const_type (die, cu);
c906108c
SS
16772 break;
16773 case DW_TAG_volatile_type:
f792889a 16774 this_type = read_tag_volatile_type (die, cu);
c906108c 16775 break;
06d66ee9
TT
16776 case DW_TAG_restrict_type:
16777 this_type = read_tag_restrict_type (die, cu);
16778 break;
c906108c 16779 case DW_TAG_string_type:
f792889a 16780 this_type = read_tag_string_type (die, cu);
c906108c
SS
16781 break;
16782 case DW_TAG_typedef:
f792889a 16783 this_type = read_typedef (die, cu);
c906108c 16784 break;
a02abb62 16785 case DW_TAG_subrange_type:
f792889a 16786 this_type = read_subrange_type (die, cu);
a02abb62 16787 break;
c906108c 16788 case DW_TAG_base_type:
f792889a 16789 this_type = read_base_type (die, cu);
c906108c 16790 break;
81a17f79 16791 case DW_TAG_unspecified_type:
f792889a 16792 this_type = read_unspecified_type (die, cu);
81a17f79 16793 break;
0114d602
DJ
16794 case DW_TAG_namespace:
16795 this_type = read_namespace_type (die, cu);
16796 break;
f55ee35c
JK
16797 case DW_TAG_module:
16798 this_type = read_module_type (die, cu);
16799 break;
c906108c 16800 default:
3e43a32a
MS
16801 complaint (&symfile_complaints,
16802 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16803 dwarf_tag_name (die->tag));
c906108c
SS
16804 break;
16805 }
63d06c5c 16806
f792889a 16807 return this_type;
63d06c5c
DC
16808}
16809
abc72ce4
DE
16810/* See if we can figure out if the class lives in a namespace. We do
16811 this by looking for a member function; its demangled name will
16812 contain namespace info, if there is any.
16813 Return the computed name or NULL.
16814 Space for the result is allocated on the objfile's obstack.
16815 This is the full-die version of guess_partial_die_structure_name.
16816 In this case we know DIE has no useful parent. */
16817
16818static char *
16819guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16820{
16821 struct die_info *spec_die;
16822 struct dwarf2_cu *spec_cu;
16823 struct die_info *child;
16824
16825 spec_cu = cu;
16826 spec_die = die_specification (die, &spec_cu);
16827 if (spec_die != NULL)
16828 {
16829 die = spec_die;
16830 cu = spec_cu;
16831 }
16832
16833 for (child = die->child;
16834 child != NULL;
16835 child = child->sibling)
16836 {
16837 if (child->tag == DW_TAG_subprogram)
16838 {
16839 struct attribute *attr;
16840
16841 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16842 if (attr == NULL)
16843 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16844 if (attr != NULL)
16845 {
16846 char *actual_name
16847 = language_class_name_from_physname (cu->language_defn,
16848 DW_STRING (attr));
16849 char *name = NULL;
16850
16851 if (actual_name != NULL)
16852 {
15d034d0 16853 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16854
16855 if (die_name != NULL
16856 && strcmp (die_name, actual_name) != 0)
16857 {
16858 /* Strip off the class name from the full name.
16859 We want the prefix. */
16860 int die_name_len = strlen (die_name);
16861 int actual_name_len = strlen (actual_name);
16862
16863 /* Test for '::' as a sanity check. */
16864 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16865 && actual_name[actual_name_len
16866 - die_name_len - 1] == ':')
abc72ce4 16867 name =
10f0c4bb
TT
16868 obstack_copy0 (&cu->objfile->objfile_obstack,
16869 actual_name,
16870 actual_name_len - die_name_len - 2);
abc72ce4
DE
16871 }
16872 }
16873 xfree (actual_name);
16874 return name;
16875 }
16876 }
16877 }
16878
16879 return NULL;
16880}
16881
96408a79
SA
16882/* GCC might emit a nameless typedef that has a linkage name. Determine the
16883 prefix part in such case. See
16884 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16885
16886static char *
16887anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16888{
16889 struct attribute *attr;
16890 char *base;
16891
16892 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16893 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16894 return NULL;
16895
16896 attr = dwarf2_attr (die, DW_AT_name, cu);
16897 if (attr != NULL && DW_STRING (attr) != NULL)
16898 return NULL;
16899
16900 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16901 if (attr == NULL)
16902 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16903 if (attr == NULL || DW_STRING (attr) == NULL)
16904 return NULL;
16905
16906 /* dwarf2_name had to be already called. */
16907 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16908
16909 /* Strip the base name, keep any leading namespaces/classes. */
16910 base = strrchr (DW_STRING (attr), ':');
16911 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16912 return "";
16913
10f0c4bb
TT
16914 return obstack_copy0 (&cu->objfile->objfile_obstack,
16915 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16916}
16917
fdde2d81 16918/* Return the name of the namespace/class that DIE is defined within,
0114d602 16919 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16920
0114d602
DJ
16921 For example, if we're within the method foo() in the following
16922 code:
16923
16924 namespace N {
16925 class C {
16926 void foo () {
16927 }
16928 };
16929 }
16930
16931 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16932
0d5cff50 16933static const char *
e142c38c 16934determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16935{
0114d602
DJ
16936 struct die_info *parent, *spec_die;
16937 struct dwarf2_cu *spec_cu;
16938 struct type *parent_type;
96408a79 16939 char *retval;
63d06c5c 16940
f55ee35c
JK
16941 if (cu->language != language_cplus && cu->language != language_java
16942 && cu->language != language_fortran)
0114d602
DJ
16943 return "";
16944
96408a79
SA
16945 retval = anonymous_struct_prefix (die, cu);
16946 if (retval)
16947 return retval;
16948
0114d602
DJ
16949 /* We have to be careful in the presence of DW_AT_specification.
16950 For example, with GCC 3.4, given the code
16951
16952 namespace N {
16953 void foo() {
16954 // Definition of N::foo.
16955 }
16956 }
16957
16958 then we'll have a tree of DIEs like this:
16959
16960 1: DW_TAG_compile_unit
16961 2: DW_TAG_namespace // N
16962 3: DW_TAG_subprogram // declaration of N::foo
16963 4: DW_TAG_subprogram // definition of N::foo
16964 DW_AT_specification // refers to die #3
16965
16966 Thus, when processing die #4, we have to pretend that we're in
16967 the context of its DW_AT_specification, namely the contex of die
16968 #3. */
16969 spec_cu = cu;
16970 spec_die = die_specification (die, &spec_cu);
16971 if (spec_die == NULL)
16972 parent = die->parent;
16973 else
63d06c5c 16974 {
0114d602
DJ
16975 parent = spec_die->parent;
16976 cu = spec_cu;
63d06c5c 16977 }
0114d602
DJ
16978
16979 if (parent == NULL)
16980 return "";
98bfdba5
PA
16981 else if (parent->building_fullname)
16982 {
16983 const char *name;
16984 const char *parent_name;
16985
16986 /* It has been seen on RealView 2.2 built binaries,
16987 DW_TAG_template_type_param types actually _defined_ as
16988 children of the parent class:
16989
16990 enum E {};
16991 template class <class Enum> Class{};
16992 Class<enum E> class_e;
16993
16994 1: DW_TAG_class_type (Class)
16995 2: DW_TAG_enumeration_type (E)
16996 3: DW_TAG_enumerator (enum1:0)
16997 3: DW_TAG_enumerator (enum2:1)
16998 ...
16999 2: DW_TAG_template_type_param
17000 DW_AT_type DW_FORM_ref_udata (E)
17001
17002 Besides being broken debug info, it can put GDB into an
17003 infinite loop. Consider:
17004
17005 When we're building the full name for Class<E>, we'll start
17006 at Class, and go look over its template type parameters,
17007 finding E. We'll then try to build the full name of E, and
17008 reach here. We're now trying to build the full name of E,
17009 and look over the parent DIE for containing scope. In the
17010 broken case, if we followed the parent DIE of E, we'd again
17011 find Class, and once again go look at its template type
17012 arguments, etc., etc. Simply don't consider such parent die
17013 as source-level parent of this die (it can't be, the language
17014 doesn't allow it), and break the loop here. */
17015 name = dwarf2_name (die, cu);
17016 parent_name = dwarf2_name (parent, cu);
17017 complaint (&symfile_complaints,
17018 _("template param type '%s' defined within parent '%s'"),
17019 name ? name : "<unknown>",
17020 parent_name ? parent_name : "<unknown>");
17021 return "";
17022 }
63d06c5c 17023 else
0114d602
DJ
17024 switch (parent->tag)
17025 {
63d06c5c 17026 case DW_TAG_namespace:
0114d602 17027 parent_type = read_type_die (parent, cu);
acebe513
UW
17028 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17029 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17030 Work around this problem here. */
17031 if (cu->language == language_cplus
17032 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17033 return "";
0114d602
DJ
17034 /* We give a name to even anonymous namespaces. */
17035 return TYPE_TAG_NAME (parent_type);
63d06c5c 17036 case DW_TAG_class_type:
680b30c7 17037 case DW_TAG_interface_type:
63d06c5c 17038 case DW_TAG_structure_type:
0114d602 17039 case DW_TAG_union_type:
f55ee35c 17040 case DW_TAG_module:
0114d602
DJ
17041 parent_type = read_type_die (parent, cu);
17042 if (TYPE_TAG_NAME (parent_type) != NULL)
17043 return TYPE_TAG_NAME (parent_type);
17044 else
17045 /* An anonymous structure is only allowed non-static data
17046 members; no typedefs, no member functions, et cetera.
17047 So it does not need a prefix. */
17048 return "";
abc72ce4 17049 case DW_TAG_compile_unit:
95554aad 17050 case DW_TAG_partial_unit:
abc72ce4
DE
17051 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17052 if (cu->language == language_cplus
8b70b953 17053 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17054 && die->child != NULL
17055 && (die->tag == DW_TAG_class_type
17056 || die->tag == DW_TAG_structure_type
17057 || die->tag == DW_TAG_union_type))
17058 {
17059 char *name = guess_full_die_structure_name (die, cu);
17060 if (name != NULL)
17061 return name;
17062 }
17063 return "";
63d06c5c 17064 default:
8176b9b8 17065 return determine_prefix (parent, cu);
63d06c5c 17066 }
63d06c5c
DC
17067}
17068
3e43a32a
MS
17069/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17070 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17071 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17072 an obconcat, otherwise allocate storage for the result. The CU argument is
17073 used to determine the language and hence, the appropriate separator. */
987504bb 17074
f55ee35c 17075#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17076
17077static char *
f55ee35c
JK
17078typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17079 int physname, struct dwarf2_cu *cu)
63d06c5c 17080{
f55ee35c 17081 const char *lead = "";
5c315b68 17082 const char *sep;
63d06c5c 17083
3e43a32a
MS
17084 if (suffix == NULL || suffix[0] == '\0'
17085 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17086 sep = "";
17087 else if (cu->language == language_java)
17088 sep = ".";
f55ee35c
JK
17089 else if (cu->language == language_fortran && physname)
17090 {
17091 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17092 DW_AT_MIPS_linkage_name is preferred and used instead. */
17093
17094 lead = "__";
17095 sep = "_MOD_";
17096 }
987504bb
JJ
17097 else
17098 sep = "::";
63d06c5c 17099
6dd47d34
DE
17100 if (prefix == NULL)
17101 prefix = "";
17102 if (suffix == NULL)
17103 suffix = "";
17104
987504bb
JJ
17105 if (obs == NULL)
17106 {
3e43a32a
MS
17107 char *retval
17108 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17109
f55ee35c
JK
17110 strcpy (retval, lead);
17111 strcat (retval, prefix);
6dd47d34
DE
17112 strcat (retval, sep);
17113 strcat (retval, suffix);
63d06c5c
DC
17114 return retval;
17115 }
987504bb
JJ
17116 else
17117 {
17118 /* We have an obstack. */
f55ee35c 17119 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17120 }
63d06c5c
DC
17121}
17122
c906108c
SS
17123/* Return sibling of die, NULL if no sibling. */
17124
f9aca02d 17125static struct die_info *
fba45db2 17126sibling_die (struct die_info *die)
c906108c 17127{
639d11d3 17128 return die->sibling;
c906108c
SS
17129}
17130
71c25dea
TT
17131/* Get name of a die, return NULL if not found. */
17132
15d034d0
TT
17133static const char *
17134dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17135 struct obstack *obstack)
17136{
17137 if (name && cu->language == language_cplus)
17138 {
17139 char *canon_name = cp_canonicalize_string (name);
17140
17141 if (canon_name != NULL)
17142 {
17143 if (strcmp (canon_name, name) != 0)
10f0c4bb 17144 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17145 xfree (canon_name);
17146 }
17147 }
17148
17149 return name;
c906108c
SS
17150}
17151
9219021c
DC
17152/* Get name of a die, return NULL if not found. */
17153
15d034d0 17154static const char *
e142c38c 17155dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17156{
17157 struct attribute *attr;
17158
e142c38c 17159 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17160 if ((!attr || !DW_STRING (attr))
17161 && die->tag != DW_TAG_class_type
17162 && die->tag != DW_TAG_interface_type
17163 && die->tag != DW_TAG_structure_type
17164 && die->tag != DW_TAG_union_type)
71c25dea
TT
17165 return NULL;
17166
17167 switch (die->tag)
17168 {
17169 case DW_TAG_compile_unit:
95554aad 17170 case DW_TAG_partial_unit:
71c25dea
TT
17171 /* Compilation units have a DW_AT_name that is a filename, not
17172 a source language identifier. */
17173 case DW_TAG_enumeration_type:
17174 case DW_TAG_enumerator:
17175 /* These tags always have simple identifiers already; no need
17176 to canonicalize them. */
17177 return DW_STRING (attr);
907af001 17178
418835cc
KS
17179 case DW_TAG_subprogram:
17180 /* Java constructors will all be named "<init>", so return
17181 the class name when we see this special case. */
17182 if (cu->language == language_java
17183 && DW_STRING (attr) != NULL
17184 && strcmp (DW_STRING (attr), "<init>") == 0)
17185 {
17186 struct dwarf2_cu *spec_cu = cu;
17187 struct die_info *spec_die;
17188
17189 /* GCJ will output '<init>' for Java constructor names.
17190 For this special case, return the name of the parent class. */
17191
17192 /* GCJ may output suprogram DIEs with AT_specification set.
17193 If so, use the name of the specified DIE. */
17194 spec_die = die_specification (die, &spec_cu);
17195 if (spec_die != NULL)
17196 return dwarf2_name (spec_die, spec_cu);
17197
17198 do
17199 {
17200 die = die->parent;
17201 if (die->tag == DW_TAG_class_type)
17202 return dwarf2_name (die, cu);
17203 }
95554aad
TT
17204 while (die->tag != DW_TAG_compile_unit
17205 && die->tag != DW_TAG_partial_unit);
418835cc 17206 }
907af001
UW
17207 break;
17208
17209 case DW_TAG_class_type:
17210 case DW_TAG_interface_type:
17211 case DW_TAG_structure_type:
17212 case DW_TAG_union_type:
17213 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17214 structures or unions. These were of the form "._%d" in GCC 4.1,
17215 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17216 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17217 if (attr && DW_STRING (attr)
17218 && (strncmp (DW_STRING (attr), "._", 2) == 0
17219 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17220 return NULL;
53832f31
TT
17221
17222 /* GCC might emit a nameless typedef that has a linkage name. See
17223 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17224 if (!attr || DW_STRING (attr) == NULL)
17225 {
df5c6c50 17226 char *demangled = NULL;
53832f31
TT
17227
17228 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17229 if (attr == NULL)
17230 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17231
17232 if (attr == NULL || DW_STRING (attr) == NULL)
17233 return NULL;
17234
df5c6c50
JK
17235 /* Avoid demangling DW_STRING (attr) the second time on a second
17236 call for the same DIE. */
17237 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17238 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17239
17240 if (demangled)
17241 {
96408a79
SA
17242 char *base;
17243
53832f31 17244 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17245 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17246 demangled, strlen (demangled));
53832f31
TT
17247 DW_STRING_IS_CANONICAL (attr) = 1;
17248 xfree (demangled);
96408a79
SA
17249
17250 /* Strip any leading namespaces/classes, keep only the base name.
17251 DW_AT_name for named DIEs does not contain the prefixes. */
17252 base = strrchr (DW_STRING (attr), ':');
17253 if (base && base > DW_STRING (attr) && base[-1] == ':')
17254 return &base[1];
17255 else
17256 return DW_STRING (attr);
53832f31
TT
17257 }
17258 }
907af001
UW
17259 break;
17260
71c25dea 17261 default:
907af001
UW
17262 break;
17263 }
17264
17265 if (!DW_STRING_IS_CANONICAL (attr))
17266 {
17267 DW_STRING (attr)
17268 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17269 &cu->objfile->objfile_obstack);
17270 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17271 }
907af001 17272 return DW_STRING (attr);
9219021c
DC
17273}
17274
17275/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17276 is none. *EXT_CU is the CU containing DIE on input, and the CU
17277 containing the return value on output. */
9219021c
DC
17278
17279static struct die_info *
f2f0e013 17280dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17281{
17282 struct attribute *attr;
9219021c 17283
f2f0e013 17284 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17285 if (attr == NULL)
17286 return NULL;
17287
f2f0e013 17288 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17289}
17290
c906108c
SS
17291/* Convert a DIE tag into its string name. */
17292
f39c6ffd 17293static const char *
aa1ee363 17294dwarf_tag_name (unsigned tag)
c906108c 17295{
f39c6ffd
TT
17296 const char *name = get_DW_TAG_name (tag);
17297
17298 if (name == NULL)
17299 return "DW_TAG_<unknown>";
17300
17301 return name;
c906108c
SS
17302}
17303
17304/* Convert a DWARF attribute code into its string name. */
17305
f39c6ffd 17306static const char *
aa1ee363 17307dwarf_attr_name (unsigned attr)
c906108c 17308{
f39c6ffd
TT
17309 const char *name;
17310
c764a876 17311#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17312 if (attr == DW_AT_MIPS_fde)
17313 return "DW_AT_MIPS_fde";
17314#else
17315 if (attr == DW_AT_HP_block_index)
17316 return "DW_AT_HP_block_index";
c764a876 17317#endif
f39c6ffd
TT
17318
17319 name = get_DW_AT_name (attr);
17320
17321 if (name == NULL)
17322 return "DW_AT_<unknown>";
17323
17324 return name;
c906108c
SS
17325}
17326
17327/* Convert a DWARF value form code into its string name. */
17328
f39c6ffd 17329static const char *
aa1ee363 17330dwarf_form_name (unsigned form)
c906108c 17331{
f39c6ffd
TT
17332 const char *name = get_DW_FORM_name (form);
17333
17334 if (name == NULL)
17335 return "DW_FORM_<unknown>";
17336
17337 return name;
c906108c
SS
17338}
17339
17340static char *
fba45db2 17341dwarf_bool_name (unsigned mybool)
c906108c
SS
17342{
17343 if (mybool)
17344 return "TRUE";
17345 else
17346 return "FALSE";
17347}
17348
17349/* Convert a DWARF type code into its string name. */
17350
f39c6ffd 17351static const char *
aa1ee363 17352dwarf_type_encoding_name (unsigned enc)
c906108c 17353{
f39c6ffd 17354 const char *name = get_DW_ATE_name (enc);
c906108c 17355
f39c6ffd
TT
17356 if (name == NULL)
17357 return "DW_ATE_<unknown>";
c906108c 17358
f39c6ffd 17359 return name;
c906108c 17360}
c906108c 17361
f9aca02d 17362static void
d97bc12b 17363dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17364{
17365 unsigned int i;
17366
d97bc12b
DE
17367 print_spaces (indent, f);
17368 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17369 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17370
17371 if (die->parent != NULL)
17372 {
17373 print_spaces (indent, f);
17374 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17375 die->parent->offset.sect_off);
d97bc12b
DE
17376 }
17377
17378 print_spaces (indent, f);
17379 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17380 dwarf_bool_name (die->child != NULL));
c906108c 17381
d97bc12b
DE
17382 print_spaces (indent, f);
17383 fprintf_unfiltered (f, " attributes:\n");
17384
c906108c
SS
17385 for (i = 0; i < die->num_attrs; ++i)
17386 {
d97bc12b
DE
17387 print_spaces (indent, f);
17388 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17389 dwarf_attr_name (die->attrs[i].name),
17390 dwarf_form_name (die->attrs[i].form));
d97bc12b 17391
c906108c
SS
17392 switch (die->attrs[i].form)
17393 {
c906108c 17394 case DW_FORM_addr:
3019eac3 17395 case DW_FORM_GNU_addr_index:
d97bc12b 17396 fprintf_unfiltered (f, "address: ");
5af949e3 17397 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17398 break;
17399 case DW_FORM_block2:
17400 case DW_FORM_block4:
17401 case DW_FORM_block:
17402 case DW_FORM_block1:
56eb65bd
SP
17403 fprintf_unfiltered (f, "block: size %s",
17404 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17405 break;
2dc7f7b3 17406 case DW_FORM_exprloc:
56eb65bd
SP
17407 fprintf_unfiltered (f, "expression: size %s",
17408 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17409 break;
4568ecf9
DE
17410 case DW_FORM_ref_addr:
17411 fprintf_unfiltered (f, "ref address: ");
17412 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17413 break;
36586728
TT
17414 case DW_FORM_GNU_ref_alt:
17415 fprintf_unfiltered (f, "alt ref address: ");
17416 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17417 break;
10b3939b
DJ
17418 case DW_FORM_ref1:
17419 case DW_FORM_ref2:
17420 case DW_FORM_ref4:
4568ecf9
DE
17421 case DW_FORM_ref8:
17422 case DW_FORM_ref_udata:
d97bc12b 17423 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17424 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17425 break;
c906108c
SS
17426 case DW_FORM_data1:
17427 case DW_FORM_data2:
17428 case DW_FORM_data4:
ce5d95e1 17429 case DW_FORM_data8:
c906108c
SS
17430 case DW_FORM_udata:
17431 case DW_FORM_sdata:
43bbcdc2
PH
17432 fprintf_unfiltered (f, "constant: %s",
17433 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17434 break;
2dc7f7b3
TT
17435 case DW_FORM_sec_offset:
17436 fprintf_unfiltered (f, "section offset: %s",
17437 pulongest (DW_UNSND (&die->attrs[i])));
17438 break;
55f1336d 17439 case DW_FORM_ref_sig8:
ac9ec31b
DE
17440 fprintf_unfiltered (f, "signature: %s",
17441 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17442 break;
c906108c 17443 case DW_FORM_string:
4bdf3d34 17444 case DW_FORM_strp:
3019eac3 17445 case DW_FORM_GNU_str_index:
36586728 17446 case DW_FORM_GNU_strp_alt:
8285870a 17447 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17448 DW_STRING (&die->attrs[i])
8285870a
JK
17449 ? DW_STRING (&die->attrs[i]) : "",
17450 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17451 break;
17452 case DW_FORM_flag:
17453 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17454 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17455 else
d97bc12b 17456 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17457 break;
2dc7f7b3
TT
17458 case DW_FORM_flag_present:
17459 fprintf_unfiltered (f, "flag: TRUE");
17460 break;
a8329558 17461 case DW_FORM_indirect:
0963b4bd
MS
17462 /* The reader will have reduced the indirect form to
17463 the "base form" so this form should not occur. */
3e43a32a
MS
17464 fprintf_unfiltered (f,
17465 "unexpected attribute form: DW_FORM_indirect");
a8329558 17466 break;
c906108c 17467 default:
d97bc12b 17468 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17469 die->attrs[i].form);
d97bc12b 17470 break;
c906108c 17471 }
d97bc12b 17472 fprintf_unfiltered (f, "\n");
c906108c
SS
17473 }
17474}
17475
f9aca02d 17476static void
d97bc12b 17477dump_die_for_error (struct die_info *die)
c906108c 17478{
d97bc12b
DE
17479 dump_die_shallow (gdb_stderr, 0, die);
17480}
17481
17482static void
17483dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17484{
17485 int indent = level * 4;
17486
17487 gdb_assert (die != NULL);
17488
17489 if (level >= max_level)
17490 return;
17491
17492 dump_die_shallow (f, indent, die);
17493
17494 if (die->child != NULL)
c906108c 17495 {
d97bc12b
DE
17496 print_spaces (indent, f);
17497 fprintf_unfiltered (f, " Children:");
17498 if (level + 1 < max_level)
17499 {
17500 fprintf_unfiltered (f, "\n");
17501 dump_die_1 (f, level + 1, max_level, die->child);
17502 }
17503 else
17504 {
3e43a32a
MS
17505 fprintf_unfiltered (f,
17506 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17507 }
17508 }
17509
17510 if (die->sibling != NULL && level > 0)
17511 {
17512 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17513 }
17514}
17515
d97bc12b
DE
17516/* This is called from the pdie macro in gdbinit.in.
17517 It's not static so gcc will keep a copy callable from gdb. */
17518
17519void
17520dump_die (struct die_info *die, int max_level)
17521{
17522 dump_die_1 (gdb_stdlog, 0, max_level, die);
17523}
17524
f9aca02d 17525static void
51545339 17526store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17527{
51545339 17528 void **slot;
c906108c 17529
b64f50a1
JK
17530 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17531 INSERT);
51545339
DJ
17532
17533 *slot = die;
c906108c
SS
17534}
17535
b64f50a1
JK
17536/* DW_ADDR is always stored already as sect_offset; despite for the forms
17537 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17538
93311388
DE
17539static int
17540is_ref_attr (struct attribute *attr)
c906108c 17541{
c906108c
SS
17542 switch (attr->form)
17543 {
17544 case DW_FORM_ref_addr:
c906108c
SS
17545 case DW_FORM_ref1:
17546 case DW_FORM_ref2:
17547 case DW_FORM_ref4:
613e1657 17548 case DW_FORM_ref8:
c906108c 17549 case DW_FORM_ref_udata:
36586728 17550 case DW_FORM_GNU_ref_alt:
93311388 17551 return 1;
c906108c 17552 default:
93311388 17553 return 0;
c906108c 17554 }
93311388
DE
17555}
17556
b64f50a1
JK
17557/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17558 required kind. */
17559
17560static sect_offset
93311388
DE
17561dwarf2_get_ref_die_offset (struct attribute *attr)
17562{
4568ecf9 17563 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17564
93311388 17565 if (is_ref_attr (attr))
b64f50a1 17566 return retval;
93311388 17567
b64f50a1 17568 retval.sect_off = 0;
93311388
DE
17569 complaint (&symfile_complaints,
17570 _("unsupported die ref attribute form: '%s'"),
17571 dwarf_form_name (attr->form));
b64f50a1 17572 return retval;
c906108c
SS
17573}
17574
43bbcdc2
PH
17575/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17576 * the value held by the attribute is not constant. */
a02abb62 17577
43bbcdc2 17578static LONGEST
a02abb62
JB
17579dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17580{
17581 if (attr->form == DW_FORM_sdata)
17582 return DW_SND (attr);
17583 else if (attr->form == DW_FORM_udata
17584 || attr->form == DW_FORM_data1
17585 || attr->form == DW_FORM_data2
17586 || attr->form == DW_FORM_data4
17587 || attr->form == DW_FORM_data8)
17588 return DW_UNSND (attr);
17589 else
17590 {
3e43a32a
MS
17591 complaint (&symfile_complaints,
17592 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17593 dwarf_form_name (attr->form));
17594 return default_value;
17595 }
17596}
17597
348e048f
DE
17598/* Follow reference or signature attribute ATTR of SRC_DIE.
17599 On entry *REF_CU is the CU of SRC_DIE.
17600 On exit *REF_CU is the CU of the result. */
17601
17602static struct die_info *
17603follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17604 struct dwarf2_cu **ref_cu)
17605{
17606 struct die_info *die;
17607
17608 if (is_ref_attr (attr))
17609 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17610 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17611 die = follow_die_sig (src_die, attr, ref_cu);
17612 else
17613 {
17614 dump_die_for_error (src_die);
17615 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17616 (*ref_cu)->objfile->name);
17617 }
17618
17619 return die;
03dd20cc
DJ
17620}
17621
5c631832 17622/* Follow reference OFFSET.
673bfd45
DE
17623 On entry *REF_CU is the CU of the source die referencing OFFSET.
17624 On exit *REF_CU is the CU of the result.
17625 Returns NULL if OFFSET is invalid. */
f504f079 17626
f9aca02d 17627static struct die_info *
36586728
TT
17628follow_die_offset (sect_offset offset, int offset_in_dwz,
17629 struct dwarf2_cu **ref_cu)
c906108c 17630{
10b3939b 17631 struct die_info temp_die;
f2f0e013 17632 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17633
348e048f
DE
17634 gdb_assert (cu->per_cu != NULL);
17635
98bfdba5
PA
17636 target_cu = cu;
17637
3019eac3 17638 if (cu->per_cu->is_debug_types)
348e048f
DE
17639 {
17640 /* .debug_types CUs cannot reference anything outside their CU.
17641 If they need to, they have to reference a signatured type via
55f1336d 17642 DW_FORM_ref_sig8. */
348e048f 17643 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17644 return NULL;
348e048f 17645 }
36586728
TT
17646 else if (offset_in_dwz != cu->per_cu->is_dwz
17647 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17648 {
17649 struct dwarf2_per_cu_data *per_cu;
9a619af0 17650
36586728
TT
17651 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17652 cu->objfile);
03dd20cc
DJ
17653
17654 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17655 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17656 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17657
10b3939b
DJ
17658 target_cu = per_cu->cu;
17659 }
98bfdba5
PA
17660 else if (cu->dies == NULL)
17661 {
17662 /* We're loading full DIEs during partial symbol reading. */
17663 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17664 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17665 }
c906108c 17666
f2f0e013 17667 *ref_cu = target_cu;
51545339 17668 temp_die.offset = offset;
b64f50a1 17669 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17670}
10b3939b 17671
5c631832
JK
17672/* Follow reference attribute ATTR of SRC_DIE.
17673 On entry *REF_CU is the CU of SRC_DIE.
17674 On exit *REF_CU is the CU of the result. */
17675
17676static struct die_info *
17677follow_die_ref (struct die_info *src_die, struct attribute *attr,
17678 struct dwarf2_cu **ref_cu)
17679{
b64f50a1 17680 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17681 struct dwarf2_cu *cu = *ref_cu;
17682 struct die_info *die;
17683
36586728
TT
17684 die = follow_die_offset (offset,
17685 (attr->form == DW_FORM_GNU_ref_alt
17686 || cu->per_cu->is_dwz),
17687 ref_cu);
5c631832
JK
17688 if (!die)
17689 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17690 "at 0x%x [in module %s]"),
b64f50a1 17691 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17692
5c631832
JK
17693 return die;
17694}
17695
d83e736b
JK
17696/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17697 Returned value is intended for DW_OP_call*. Returned
17698 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17699
17700struct dwarf2_locexpr_baton
8b9737bf
TT
17701dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17702 struct dwarf2_per_cu_data *per_cu,
17703 CORE_ADDR (*get_frame_pc) (void *baton),
17704 void *baton)
5c631832 17705{
918dd910 17706 struct dwarf2_cu *cu;
5c631832
JK
17707 struct die_info *die;
17708 struct attribute *attr;
17709 struct dwarf2_locexpr_baton retval;
17710
8cf6f0b1
TT
17711 dw2_setup (per_cu->objfile);
17712
918dd910
JK
17713 if (per_cu->cu == NULL)
17714 load_cu (per_cu);
17715 cu = per_cu->cu;
17716
36586728 17717 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17718 if (!die)
17719 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17720 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17721
17722 attr = dwarf2_attr (die, DW_AT_location, cu);
17723 if (!attr)
17724 {
e103e986
JK
17725 /* DWARF: "If there is no such attribute, then there is no effect.".
17726 DATA is ignored if SIZE is 0. */
5c631832 17727
e103e986 17728 retval.data = NULL;
5c631832
JK
17729 retval.size = 0;
17730 }
8cf6f0b1
TT
17731 else if (attr_form_is_section_offset (attr))
17732 {
17733 struct dwarf2_loclist_baton loclist_baton;
17734 CORE_ADDR pc = (*get_frame_pc) (baton);
17735 size_t size;
17736
17737 fill_in_loclist_baton (cu, &loclist_baton, attr);
17738
17739 retval.data = dwarf2_find_location_expression (&loclist_baton,
17740 &size, pc);
17741 retval.size = size;
17742 }
5c631832
JK
17743 else
17744 {
17745 if (!attr_form_is_block (attr))
17746 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17747 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17748 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17749
17750 retval.data = DW_BLOCK (attr)->data;
17751 retval.size = DW_BLOCK (attr)->size;
17752 }
17753 retval.per_cu = cu->per_cu;
918dd910 17754
918dd910
JK
17755 age_cached_comp_units ();
17756
5c631832 17757 return retval;
348e048f
DE
17758}
17759
8b9737bf
TT
17760/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17761 offset. */
17762
17763struct dwarf2_locexpr_baton
17764dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17765 struct dwarf2_per_cu_data *per_cu,
17766 CORE_ADDR (*get_frame_pc) (void *baton),
17767 void *baton)
17768{
17769 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17770
17771 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17772}
17773
8a9b8146
TT
17774/* Return the type of the DIE at DIE_OFFSET in the CU named by
17775 PER_CU. */
17776
17777struct type *
b64f50a1 17778dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17779 struct dwarf2_per_cu_data *per_cu)
17780{
b64f50a1
JK
17781 sect_offset die_offset_sect;
17782
8a9b8146 17783 dw2_setup (per_cu->objfile);
b64f50a1
JK
17784
17785 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17786 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17787}
17788
ac9ec31b 17789/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 17790 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
17791 On exit *REF_CU is the CU of the result.
17792 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
17793
17794static struct die_info *
ac9ec31b
DE
17795follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
17796 struct dwarf2_cu **ref_cu)
348e048f
DE
17797{
17798 struct objfile *objfile = (*ref_cu)->objfile;
17799 struct die_info temp_die;
348e048f
DE
17800 struct dwarf2_cu *sig_cu;
17801 struct die_info *die;
17802
ac9ec31b
DE
17803 /* While it might be nice to assert sig_type->type == NULL here,
17804 we can get here for DW_AT_imported_declaration where we need
17805 the DIE not the type. */
348e048f
DE
17806
17807 /* If necessary, add it to the queue and load its DIEs. */
17808
95554aad 17809 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17810 read_signatured_type (sig_type);
348e048f
DE
17811
17812 gdb_assert (sig_type->per_cu.cu != NULL);
17813
17814 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17815 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17816 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17817 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17818 temp_die.offset.sect_off);
348e048f
DE
17819 if (die)
17820 {
796a7ff8
DE
17821 /* For .gdb_index version 7 keep track of included TUs.
17822 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17823 if (dwarf2_per_objfile->index_table != NULL
17824 && dwarf2_per_objfile->index_table->version <= 7)
17825 {
17826 VEC_safe_push (dwarf2_per_cu_ptr,
17827 (*ref_cu)->per_cu->imported_symtabs,
17828 sig_cu->per_cu);
17829 }
17830
348e048f
DE
17831 *ref_cu = sig_cu;
17832 return die;
17833 }
17834
ac9ec31b
DE
17835 return NULL;
17836}
17837
17838/* Follow signatured type referenced by ATTR in SRC_DIE.
17839 On entry *REF_CU is the CU of SRC_DIE.
17840 On exit *REF_CU is the CU of the result.
17841 The result is the DIE of the type.
17842 If the referenced type cannot be found an error is thrown. */
17843
17844static struct die_info *
17845follow_die_sig (struct die_info *src_die, struct attribute *attr,
17846 struct dwarf2_cu **ref_cu)
17847{
17848 ULONGEST signature = DW_SIGNATURE (attr);
17849 struct signatured_type *sig_type;
17850 struct die_info *die;
17851
17852 gdb_assert (attr->form == DW_FORM_ref_sig8);
17853
17854 sig_type = lookup_signatured_type (signature);
17855 /* sig_type will be NULL if the signatured type is missing from
17856 the debug info. */
17857 if (sig_type == NULL)
17858 {
17859 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
17860 " from DIE at 0x%x [in module %s]"),
17861 hex_string (signature), src_die->offset.sect_off,
17862 (*ref_cu)->objfile->name);
17863 }
17864
17865 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
17866 if (die == NULL)
17867 {
17868 dump_die_for_error (src_die);
17869 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
17870 " from DIE at 0x%x [in module %s]"),
17871 hex_string (signature), src_die->offset.sect_off,
17872 (*ref_cu)->objfile->name);
17873 }
17874
17875 return die;
17876}
17877
17878/* Get the type specified by SIGNATURE referenced in DIE/CU,
17879 reading in and processing the type unit if necessary. */
17880
17881static struct type *
17882get_signatured_type (struct die_info *die, ULONGEST signature,
17883 struct dwarf2_cu *cu)
17884{
17885 struct signatured_type *sig_type;
17886 struct dwarf2_cu *type_cu;
17887 struct die_info *type_die;
17888 struct type *type;
17889
17890 sig_type = lookup_signatured_type (signature);
17891 /* sig_type will be NULL if the signatured type is missing from
17892 the debug info. */
17893 if (sig_type == NULL)
17894 {
17895 complaint (&symfile_complaints,
17896 _("Dwarf Error: Cannot find signatured DIE %s referenced"
17897 " from DIE at 0x%x [in module %s]"),
17898 hex_string (signature), die->offset.sect_off,
17899 dwarf2_per_objfile->objfile->name);
17900 return build_error_marker_type (cu, die);
17901 }
17902
17903 /* If we already know the type we're done. */
17904 if (sig_type->type != NULL)
17905 return sig_type->type;
17906
17907 type_cu = cu;
17908 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
17909 if (type_die != NULL)
17910 {
17911 /* N.B. We need to call get_die_type to ensure only one type for this DIE
17912 is created. This is important, for example, because for c++ classes
17913 we need TYPE_NAME set which is only done by new_symbol. Blech. */
17914 type = read_type_die (type_die, type_cu);
17915 if (type == NULL)
17916 {
17917 complaint (&symfile_complaints,
17918 _("Dwarf Error: Cannot build signatured type %s"
17919 " referenced from DIE at 0x%x [in module %s]"),
17920 hex_string (signature), die->offset.sect_off,
17921 dwarf2_per_objfile->objfile->name);
17922 type = build_error_marker_type (cu, die);
17923 }
17924 }
17925 else
17926 {
17927 complaint (&symfile_complaints,
17928 _("Dwarf Error: Problem reading signatured DIE %s referenced"
17929 " from DIE at 0x%x [in module %s]"),
17930 hex_string (signature), die->offset.sect_off,
17931 dwarf2_per_objfile->objfile->name);
17932 type = build_error_marker_type (cu, die);
17933 }
17934 sig_type->type = type;
17935
17936 return type;
17937}
17938
17939/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
17940 reading in and processing the type unit if necessary. */
17941
17942static struct type *
b385a60d
PM
17943get_DW_AT_signature_type (struct die_info *die, struct attribute *attr,
17944 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
17945{
17946 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
17947 if (is_ref_attr (attr))
17948 {
17949 struct dwarf2_cu *type_cu = cu;
17950 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
17951
17952 return read_type_die (type_die, type_cu);
17953 }
17954 else if (attr->form == DW_FORM_ref_sig8)
17955 {
17956 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
17957 }
17958 else
17959 {
17960 complaint (&symfile_complaints,
17961 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
17962 " at 0x%x [in module %s]"),
17963 dwarf_form_name (attr->form), die->offset.sect_off,
17964 dwarf2_per_objfile->objfile->name);
17965 return build_error_marker_type (cu, die);
17966 }
348e048f
DE
17967}
17968
e5fe5e75 17969/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17970
17971static void
e5fe5e75 17972load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17973{
52dc124a 17974 struct signatured_type *sig_type;
348e048f 17975
f4dc4d17
DE
17976 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17977 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17978
6721b2ec
DE
17979 /* We have the per_cu, but we need the signatured_type.
17980 Fortunately this is an easy translation. */
17981 gdb_assert (per_cu->is_debug_types);
17982 sig_type = (struct signatured_type *) per_cu;
348e048f 17983
6721b2ec 17984 gdb_assert (per_cu->cu == NULL);
348e048f 17985
52dc124a 17986 read_signatured_type (sig_type);
348e048f 17987
6721b2ec 17988 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17989}
17990
dee91e82
DE
17991/* die_reader_func for read_signatured_type.
17992 This is identical to load_full_comp_unit_reader,
17993 but is kept separate for now. */
348e048f
DE
17994
17995static void
dee91e82 17996read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 17997 const gdb_byte *info_ptr,
dee91e82
DE
17998 struct die_info *comp_unit_die,
17999 int has_children,
18000 void *data)
348e048f 18001{
dee91e82 18002 struct dwarf2_cu *cu = reader->cu;
348e048f 18003
dee91e82
DE
18004 gdb_assert (cu->die_hash == NULL);
18005 cu->die_hash =
18006 htab_create_alloc_ex (cu->header.length / 12,
18007 die_hash,
18008 die_eq,
18009 NULL,
18010 &cu->comp_unit_obstack,
18011 hashtab_obstack_allocate,
18012 dummy_obstack_deallocate);
348e048f 18013
dee91e82
DE
18014 if (has_children)
18015 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18016 &info_ptr, comp_unit_die);
18017 cu->dies = comp_unit_die;
18018 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18019
18020 /* We try not to read any attributes in this function, because not
9cdd5dbd 18021 all CUs needed for references have been loaded yet, and symbol
348e048f 18022 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18023 or we won't be able to build types correctly.
18024 Similarly, if we do not read the producer, we can not apply
18025 producer-specific interpretation. */
95554aad 18026 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18027}
348e048f 18028
3019eac3
DE
18029/* Read in a signatured type and build its CU and DIEs.
18030 If the type is a stub for the real type in a DWO file,
18031 read in the real type from the DWO file as well. */
dee91e82
DE
18032
18033static void
18034read_signatured_type (struct signatured_type *sig_type)
18035{
18036 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18037
3019eac3 18038 gdb_assert (per_cu->is_debug_types);
dee91e82 18039 gdb_assert (per_cu->cu == NULL);
348e048f 18040
f4dc4d17
DE
18041 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18042 read_signatured_type_reader, NULL);
c906108c
SS
18043}
18044
c906108c
SS
18045/* Decode simple location descriptions.
18046 Given a pointer to a dwarf block that defines a location, compute
18047 the location and return the value.
18048
4cecd739
DJ
18049 NOTE drow/2003-11-18: This function is called in two situations
18050 now: for the address of static or global variables (partial symbols
18051 only) and for offsets into structures which are expected to be
18052 (more or less) constant. The partial symbol case should go away,
18053 and only the constant case should remain. That will let this
18054 function complain more accurately. A few special modes are allowed
18055 without complaint for global variables (for instance, global
18056 register values and thread-local values).
c906108c
SS
18057
18058 A location description containing no operations indicates that the
4cecd739 18059 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18060 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18061 callers will only want a very basic result and this can become a
21ae7a4d
JK
18062 complaint.
18063
18064 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18065
18066static CORE_ADDR
e7c27a73 18067decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18068{
e7c27a73 18069 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18070 size_t i;
18071 size_t size = blk->size;
d521ce57 18072 const gdb_byte *data = blk->data;
21ae7a4d
JK
18073 CORE_ADDR stack[64];
18074 int stacki;
18075 unsigned int bytes_read, unsnd;
18076 gdb_byte op;
c906108c 18077
21ae7a4d
JK
18078 i = 0;
18079 stacki = 0;
18080 stack[stacki] = 0;
18081 stack[++stacki] = 0;
18082
18083 while (i < size)
18084 {
18085 op = data[i++];
18086 switch (op)
18087 {
18088 case DW_OP_lit0:
18089 case DW_OP_lit1:
18090 case DW_OP_lit2:
18091 case DW_OP_lit3:
18092 case DW_OP_lit4:
18093 case DW_OP_lit5:
18094 case DW_OP_lit6:
18095 case DW_OP_lit7:
18096 case DW_OP_lit8:
18097 case DW_OP_lit9:
18098 case DW_OP_lit10:
18099 case DW_OP_lit11:
18100 case DW_OP_lit12:
18101 case DW_OP_lit13:
18102 case DW_OP_lit14:
18103 case DW_OP_lit15:
18104 case DW_OP_lit16:
18105 case DW_OP_lit17:
18106 case DW_OP_lit18:
18107 case DW_OP_lit19:
18108 case DW_OP_lit20:
18109 case DW_OP_lit21:
18110 case DW_OP_lit22:
18111 case DW_OP_lit23:
18112 case DW_OP_lit24:
18113 case DW_OP_lit25:
18114 case DW_OP_lit26:
18115 case DW_OP_lit27:
18116 case DW_OP_lit28:
18117 case DW_OP_lit29:
18118 case DW_OP_lit30:
18119 case DW_OP_lit31:
18120 stack[++stacki] = op - DW_OP_lit0;
18121 break;
f1bea926 18122
21ae7a4d
JK
18123 case DW_OP_reg0:
18124 case DW_OP_reg1:
18125 case DW_OP_reg2:
18126 case DW_OP_reg3:
18127 case DW_OP_reg4:
18128 case DW_OP_reg5:
18129 case DW_OP_reg6:
18130 case DW_OP_reg7:
18131 case DW_OP_reg8:
18132 case DW_OP_reg9:
18133 case DW_OP_reg10:
18134 case DW_OP_reg11:
18135 case DW_OP_reg12:
18136 case DW_OP_reg13:
18137 case DW_OP_reg14:
18138 case DW_OP_reg15:
18139 case DW_OP_reg16:
18140 case DW_OP_reg17:
18141 case DW_OP_reg18:
18142 case DW_OP_reg19:
18143 case DW_OP_reg20:
18144 case DW_OP_reg21:
18145 case DW_OP_reg22:
18146 case DW_OP_reg23:
18147 case DW_OP_reg24:
18148 case DW_OP_reg25:
18149 case DW_OP_reg26:
18150 case DW_OP_reg27:
18151 case DW_OP_reg28:
18152 case DW_OP_reg29:
18153 case DW_OP_reg30:
18154 case DW_OP_reg31:
18155 stack[++stacki] = op - DW_OP_reg0;
18156 if (i < size)
18157 dwarf2_complex_location_expr_complaint ();
18158 break;
c906108c 18159
21ae7a4d
JK
18160 case DW_OP_regx:
18161 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18162 i += bytes_read;
18163 stack[++stacki] = unsnd;
18164 if (i < size)
18165 dwarf2_complex_location_expr_complaint ();
18166 break;
c906108c 18167
21ae7a4d
JK
18168 case DW_OP_addr:
18169 stack[++stacki] = read_address (objfile->obfd, &data[i],
18170 cu, &bytes_read);
18171 i += bytes_read;
18172 break;
d53d4ac5 18173
21ae7a4d
JK
18174 case DW_OP_const1u:
18175 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18176 i += 1;
18177 break;
18178
18179 case DW_OP_const1s:
18180 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18181 i += 1;
18182 break;
18183
18184 case DW_OP_const2u:
18185 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18186 i += 2;
18187 break;
18188
18189 case DW_OP_const2s:
18190 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18191 i += 2;
18192 break;
d53d4ac5 18193
21ae7a4d
JK
18194 case DW_OP_const4u:
18195 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18196 i += 4;
18197 break;
18198
18199 case DW_OP_const4s:
18200 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18201 i += 4;
18202 break;
18203
585861ea
JK
18204 case DW_OP_const8u:
18205 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18206 i += 8;
18207 break;
18208
21ae7a4d
JK
18209 case DW_OP_constu:
18210 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18211 &bytes_read);
18212 i += bytes_read;
18213 break;
18214
18215 case DW_OP_consts:
18216 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18217 i += bytes_read;
18218 break;
18219
18220 case DW_OP_dup:
18221 stack[stacki + 1] = stack[stacki];
18222 stacki++;
18223 break;
18224
18225 case DW_OP_plus:
18226 stack[stacki - 1] += stack[stacki];
18227 stacki--;
18228 break;
18229
18230 case DW_OP_plus_uconst:
18231 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18232 &bytes_read);
18233 i += bytes_read;
18234 break;
18235
18236 case DW_OP_minus:
18237 stack[stacki - 1] -= stack[stacki];
18238 stacki--;
18239 break;
18240
18241 case DW_OP_deref:
18242 /* If we're not the last op, then we definitely can't encode
18243 this using GDB's address_class enum. This is valid for partial
18244 global symbols, although the variable's address will be bogus
18245 in the psymtab. */
18246 if (i < size)
18247 dwarf2_complex_location_expr_complaint ();
18248 break;
18249
18250 case DW_OP_GNU_push_tls_address:
18251 /* The top of the stack has the offset from the beginning
18252 of the thread control block at which the variable is located. */
18253 /* Nothing should follow this operator, so the top of stack would
18254 be returned. */
18255 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18256 address will be bogus in the psymtab. Make it always at least
18257 non-zero to not look as a variable garbage collected by linker
18258 which have DW_OP_addr 0. */
21ae7a4d
JK
18259 if (i < size)
18260 dwarf2_complex_location_expr_complaint ();
585861ea 18261 stack[stacki]++;
21ae7a4d
JK
18262 break;
18263
18264 case DW_OP_GNU_uninit:
18265 break;
18266
3019eac3 18267 case DW_OP_GNU_addr_index:
49f6c839 18268 case DW_OP_GNU_const_index:
3019eac3
DE
18269 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18270 &bytes_read);
18271 i += bytes_read;
18272 break;
18273
21ae7a4d
JK
18274 default:
18275 {
f39c6ffd 18276 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18277
18278 if (name)
18279 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18280 name);
18281 else
18282 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18283 op);
18284 }
18285
18286 return (stack[stacki]);
d53d4ac5 18287 }
3c6e0cb3 18288
21ae7a4d
JK
18289 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18290 outside of the allocated space. Also enforce minimum>0. */
18291 if (stacki >= ARRAY_SIZE (stack) - 1)
18292 {
18293 complaint (&symfile_complaints,
18294 _("location description stack overflow"));
18295 return 0;
18296 }
18297
18298 if (stacki <= 0)
18299 {
18300 complaint (&symfile_complaints,
18301 _("location description stack underflow"));
18302 return 0;
18303 }
18304 }
18305 return (stack[stacki]);
c906108c
SS
18306}
18307
18308/* memory allocation interface */
18309
c906108c 18310static struct dwarf_block *
7b5a2f43 18311dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18312{
18313 struct dwarf_block *blk;
18314
18315 blk = (struct dwarf_block *)
7b5a2f43 18316 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18317 return (blk);
18318}
18319
c906108c 18320static struct die_info *
b60c80d6 18321dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18322{
18323 struct die_info *die;
b60c80d6
DJ
18324 size_t size = sizeof (struct die_info);
18325
18326 if (num_attrs > 1)
18327 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18328
b60c80d6 18329 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18330 memset (die, 0, sizeof (struct die_info));
18331 return (die);
18332}
2e276125
JB
18333
18334\f
18335/* Macro support. */
18336
233d95b5
JK
18337/* Return file name relative to the compilation directory of file number I in
18338 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18339 responsible for freeing it. */
233d95b5 18340
2e276125 18341static char *
233d95b5 18342file_file_name (int file, struct line_header *lh)
2e276125 18343{
6a83a1e6
EZ
18344 /* Is the file number a valid index into the line header's file name
18345 table? Remember that file numbers start with one, not zero. */
18346 if (1 <= file && file <= lh->num_file_names)
18347 {
18348 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18349
233d95b5 18350 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18351 return xstrdup (fe->name);
233d95b5
JK
18352 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18353 fe->name, NULL);
6a83a1e6 18354 }
2e276125
JB
18355 else
18356 {
6a83a1e6
EZ
18357 /* The compiler produced a bogus file number. We can at least
18358 record the macro definitions made in the file, even if we
18359 won't be able to find the file by name. */
18360 char fake_name[80];
9a619af0 18361
8c042590
PM
18362 xsnprintf (fake_name, sizeof (fake_name),
18363 "<bad macro file number %d>", file);
2e276125 18364
6e70227d 18365 complaint (&symfile_complaints,
6a83a1e6
EZ
18366 _("bad file number in macro information (%d)"),
18367 file);
2e276125 18368
6a83a1e6 18369 return xstrdup (fake_name);
2e276125
JB
18370 }
18371}
18372
233d95b5
JK
18373/* Return the full name of file number I in *LH's file name table.
18374 Use COMP_DIR as the name of the current directory of the
18375 compilation. The result is allocated using xmalloc; the caller is
18376 responsible for freeing it. */
18377static char *
18378file_full_name (int file, struct line_header *lh, const char *comp_dir)
18379{
18380 /* Is the file number a valid index into the line header's file name
18381 table? Remember that file numbers start with one, not zero. */
18382 if (1 <= file && file <= lh->num_file_names)
18383 {
18384 char *relative = file_file_name (file, lh);
18385
18386 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18387 return relative;
18388 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18389 }
18390 else
18391 return file_file_name (file, lh);
18392}
18393
2e276125
JB
18394
18395static struct macro_source_file *
18396macro_start_file (int file, int line,
18397 struct macro_source_file *current_file,
18398 const char *comp_dir,
18399 struct line_header *lh, struct objfile *objfile)
18400{
233d95b5
JK
18401 /* File name relative to the compilation directory of this source file. */
18402 char *file_name = file_file_name (file, lh);
2e276125
JB
18403
18404 /* We don't create a macro table for this compilation unit
18405 at all until we actually get a filename. */
18406 if (! pending_macros)
6532ff36 18407 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18408 objfile->per_bfd->macro_cache,
18409 comp_dir);
2e276125
JB
18410
18411 if (! current_file)
abc9d0dc
TT
18412 {
18413 /* If we have no current file, then this must be the start_file
18414 directive for the compilation unit's main source file. */
233d95b5 18415 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18416 macro_define_special (pending_macros);
18417 }
2e276125 18418 else
233d95b5 18419 current_file = macro_include (current_file, line, file_name);
2e276125 18420
233d95b5 18421 xfree (file_name);
6e70227d 18422
2e276125
JB
18423 return current_file;
18424}
18425
18426
18427/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18428 followed by a null byte. */
18429static char *
18430copy_string (const char *buf, int len)
18431{
18432 char *s = xmalloc (len + 1);
9a619af0 18433
2e276125
JB
18434 memcpy (s, buf, len);
18435 s[len] = '\0';
2e276125
JB
18436 return s;
18437}
18438
18439
18440static const char *
18441consume_improper_spaces (const char *p, const char *body)
18442{
18443 if (*p == ' ')
18444 {
4d3c2250 18445 complaint (&symfile_complaints,
3e43a32a
MS
18446 _("macro definition contains spaces "
18447 "in formal argument list:\n`%s'"),
4d3c2250 18448 body);
2e276125
JB
18449
18450 while (*p == ' ')
18451 p++;
18452 }
18453
18454 return p;
18455}
18456
18457
18458static void
18459parse_macro_definition (struct macro_source_file *file, int line,
18460 const char *body)
18461{
18462 const char *p;
18463
18464 /* The body string takes one of two forms. For object-like macro
18465 definitions, it should be:
18466
18467 <macro name> " " <definition>
18468
18469 For function-like macro definitions, it should be:
18470
18471 <macro name> "() " <definition>
18472 or
18473 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18474
18475 Spaces may appear only where explicitly indicated, and in the
18476 <definition>.
18477
18478 The Dwarf 2 spec says that an object-like macro's name is always
18479 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18480 the space when the macro's definition is the empty string.
2e276125
JB
18481
18482 The Dwarf 2 spec says that there should be no spaces between the
18483 formal arguments in a function-like macro's formal argument list,
18484 but versions of GCC around March 2002 include spaces after the
18485 commas. */
18486
18487
18488 /* Find the extent of the macro name. The macro name is terminated
18489 by either a space or null character (for an object-like macro) or
18490 an opening paren (for a function-like macro). */
18491 for (p = body; *p; p++)
18492 if (*p == ' ' || *p == '(')
18493 break;
18494
18495 if (*p == ' ' || *p == '\0')
18496 {
18497 /* It's an object-like macro. */
18498 int name_len = p - body;
18499 char *name = copy_string (body, name_len);
18500 const char *replacement;
18501
18502 if (*p == ' ')
18503 replacement = body + name_len + 1;
18504 else
18505 {
4d3c2250 18506 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18507 replacement = body + name_len;
18508 }
6e70227d 18509
2e276125
JB
18510 macro_define_object (file, line, name, replacement);
18511
18512 xfree (name);
18513 }
18514 else if (*p == '(')
18515 {
18516 /* It's a function-like macro. */
18517 char *name = copy_string (body, p - body);
18518 int argc = 0;
18519 int argv_size = 1;
18520 char **argv = xmalloc (argv_size * sizeof (*argv));
18521
18522 p++;
18523
18524 p = consume_improper_spaces (p, body);
18525
18526 /* Parse the formal argument list. */
18527 while (*p && *p != ')')
18528 {
18529 /* Find the extent of the current argument name. */
18530 const char *arg_start = p;
18531
18532 while (*p && *p != ',' && *p != ')' && *p != ' ')
18533 p++;
18534
18535 if (! *p || p == arg_start)
4d3c2250 18536 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18537 else
18538 {
18539 /* Make sure argv has room for the new argument. */
18540 if (argc >= argv_size)
18541 {
18542 argv_size *= 2;
18543 argv = xrealloc (argv, argv_size * sizeof (*argv));
18544 }
18545
18546 argv[argc++] = copy_string (arg_start, p - arg_start);
18547 }
18548
18549 p = consume_improper_spaces (p, body);
18550
18551 /* Consume the comma, if present. */
18552 if (*p == ',')
18553 {
18554 p++;
18555
18556 p = consume_improper_spaces (p, body);
18557 }
18558 }
18559
18560 if (*p == ')')
18561 {
18562 p++;
18563
18564 if (*p == ' ')
18565 /* Perfectly formed definition, no complaints. */
18566 macro_define_function (file, line, name,
6e70227d 18567 argc, (const char **) argv,
2e276125
JB
18568 p + 1);
18569 else if (*p == '\0')
18570 {
18571 /* Complain, but do define it. */
4d3c2250 18572 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18573 macro_define_function (file, line, name,
6e70227d 18574 argc, (const char **) argv,
2e276125
JB
18575 p);
18576 }
18577 else
18578 /* Just complain. */
4d3c2250 18579 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18580 }
18581 else
18582 /* Just complain. */
4d3c2250 18583 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18584
18585 xfree (name);
18586 {
18587 int i;
18588
18589 for (i = 0; i < argc; i++)
18590 xfree (argv[i]);
18591 }
18592 xfree (argv);
18593 }
18594 else
4d3c2250 18595 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18596}
18597
cf2c3c16
TT
18598/* Skip some bytes from BYTES according to the form given in FORM.
18599 Returns the new pointer. */
2e276125 18600
d521ce57
TT
18601static const gdb_byte *
18602skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
18603 enum dwarf_form form,
18604 unsigned int offset_size,
18605 struct dwarf2_section_info *section)
2e276125 18606{
cf2c3c16 18607 unsigned int bytes_read;
2e276125 18608
cf2c3c16 18609 switch (form)
2e276125 18610 {
cf2c3c16
TT
18611 case DW_FORM_data1:
18612 case DW_FORM_flag:
18613 ++bytes;
18614 break;
18615
18616 case DW_FORM_data2:
18617 bytes += 2;
18618 break;
18619
18620 case DW_FORM_data4:
18621 bytes += 4;
18622 break;
18623
18624 case DW_FORM_data8:
18625 bytes += 8;
18626 break;
18627
18628 case DW_FORM_string:
18629 read_direct_string (abfd, bytes, &bytes_read);
18630 bytes += bytes_read;
18631 break;
18632
18633 case DW_FORM_sec_offset:
18634 case DW_FORM_strp:
36586728 18635 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18636 bytes += offset_size;
18637 break;
18638
18639 case DW_FORM_block:
18640 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18641 bytes += bytes_read;
18642 break;
18643
18644 case DW_FORM_block1:
18645 bytes += 1 + read_1_byte (abfd, bytes);
18646 break;
18647 case DW_FORM_block2:
18648 bytes += 2 + read_2_bytes (abfd, bytes);
18649 break;
18650 case DW_FORM_block4:
18651 bytes += 4 + read_4_bytes (abfd, bytes);
18652 break;
18653
18654 case DW_FORM_sdata:
18655 case DW_FORM_udata:
3019eac3
DE
18656 case DW_FORM_GNU_addr_index:
18657 case DW_FORM_GNU_str_index:
d521ce57 18658 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
18659 if (bytes == NULL)
18660 {
18661 dwarf2_section_buffer_overflow_complaint (section);
18662 return NULL;
18663 }
cf2c3c16
TT
18664 break;
18665
18666 default:
18667 {
18668 complain:
18669 complaint (&symfile_complaints,
18670 _("invalid form 0x%x in `%s'"),
18671 form,
18672 section->asection->name);
18673 return NULL;
18674 }
2e276125
JB
18675 }
18676
cf2c3c16
TT
18677 return bytes;
18678}
757a13d0 18679
cf2c3c16
TT
18680/* A helper for dwarf_decode_macros that handles skipping an unknown
18681 opcode. Returns an updated pointer to the macro data buffer; or,
18682 on error, issues a complaint and returns NULL. */
757a13d0 18683
d521ce57 18684static const gdb_byte *
cf2c3c16 18685skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
18686 const gdb_byte **opcode_definitions,
18687 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
18688 bfd *abfd,
18689 unsigned int offset_size,
18690 struct dwarf2_section_info *section)
18691{
18692 unsigned int bytes_read, i;
18693 unsigned long arg;
d521ce57 18694 const gdb_byte *defn;
2e276125 18695
cf2c3c16 18696 if (opcode_definitions[opcode] == NULL)
2e276125 18697 {
cf2c3c16
TT
18698 complaint (&symfile_complaints,
18699 _("unrecognized DW_MACFINO opcode 0x%x"),
18700 opcode);
18701 return NULL;
18702 }
2e276125 18703
cf2c3c16
TT
18704 defn = opcode_definitions[opcode];
18705 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18706 defn += bytes_read;
2e276125 18707
cf2c3c16
TT
18708 for (i = 0; i < arg; ++i)
18709 {
f664829e
DE
18710 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18711 section);
cf2c3c16
TT
18712 if (mac_ptr == NULL)
18713 {
18714 /* skip_form_bytes already issued the complaint. */
18715 return NULL;
18716 }
18717 }
757a13d0 18718
cf2c3c16
TT
18719 return mac_ptr;
18720}
757a13d0 18721
cf2c3c16
TT
18722/* A helper function which parses the header of a macro section.
18723 If the macro section is the extended (for now called "GNU") type,
18724 then this updates *OFFSET_SIZE. Returns a pointer to just after
18725 the header, or issues a complaint and returns NULL on error. */
757a13d0 18726
d521ce57
TT
18727static const gdb_byte *
18728dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 18729 bfd *abfd,
d521ce57 18730 const gdb_byte *mac_ptr,
cf2c3c16
TT
18731 unsigned int *offset_size,
18732 int section_is_gnu)
18733{
18734 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18735
cf2c3c16
TT
18736 if (section_is_gnu)
18737 {
18738 unsigned int version, flags;
757a13d0 18739
cf2c3c16
TT
18740 version = read_2_bytes (abfd, mac_ptr);
18741 if (version != 4)
18742 {
18743 complaint (&symfile_complaints,
18744 _("unrecognized version `%d' in .debug_macro section"),
18745 version);
18746 return NULL;
18747 }
18748 mac_ptr += 2;
757a13d0 18749
cf2c3c16
TT
18750 flags = read_1_byte (abfd, mac_ptr);
18751 ++mac_ptr;
18752 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18753
cf2c3c16
TT
18754 if ((flags & 2) != 0)
18755 /* We don't need the line table offset. */
18756 mac_ptr += *offset_size;
757a13d0 18757
cf2c3c16
TT
18758 /* Vendor opcode descriptions. */
18759 if ((flags & 4) != 0)
18760 {
18761 unsigned int i, count;
757a13d0 18762
cf2c3c16
TT
18763 count = read_1_byte (abfd, mac_ptr);
18764 ++mac_ptr;
18765 for (i = 0; i < count; ++i)
18766 {
18767 unsigned int opcode, bytes_read;
18768 unsigned long arg;
18769
18770 opcode = read_1_byte (abfd, mac_ptr);
18771 ++mac_ptr;
18772 opcode_definitions[opcode] = mac_ptr;
18773 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18774 mac_ptr += bytes_read;
18775 mac_ptr += arg;
18776 }
757a13d0 18777 }
cf2c3c16 18778 }
757a13d0 18779
cf2c3c16
TT
18780 return mac_ptr;
18781}
757a13d0 18782
cf2c3c16 18783/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18784 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18785
18786static void
d521ce57
TT
18787dwarf_decode_macro_bytes (bfd *abfd,
18788 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 18789 struct macro_source_file *current_file,
15d034d0 18790 struct line_header *lh, const char *comp_dir,
cf2c3c16 18791 struct dwarf2_section_info *section,
36586728 18792 int section_is_gnu, int section_is_dwz,
cf2c3c16 18793 unsigned int offset_size,
8fc3fc34
TT
18794 struct objfile *objfile,
18795 htab_t include_hash)
cf2c3c16
TT
18796{
18797 enum dwarf_macro_record_type macinfo_type;
18798 int at_commandline;
d521ce57 18799 const gdb_byte *opcode_definitions[256];
757a13d0 18800
cf2c3c16
TT
18801 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18802 &offset_size, section_is_gnu);
18803 if (mac_ptr == NULL)
18804 {
18805 /* We already issued a complaint. */
18806 return;
18807 }
757a13d0
JK
18808
18809 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18810 GDB is still reading the definitions from command line. First
18811 DW_MACINFO_start_file will need to be ignored as it was already executed
18812 to create CURRENT_FILE for the main source holding also the command line
18813 definitions. On first met DW_MACINFO_start_file this flag is reset to
18814 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18815
18816 at_commandline = 1;
18817
18818 do
18819 {
18820 /* Do we at least have room for a macinfo type byte? */
18821 if (mac_ptr >= mac_end)
18822 {
f664829e 18823 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18824 break;
18825 }
18826
18827 macinfo_type = read_1_byte (abfd, mac_ptr);
18828 mac_ptr++;
18829
cf2c3c16
TT
18830 /* Note that we rely on the fact that the corresponding GNU and
18831 DWARF constants are the same. */
757a13d0
JK
18832 switch (macinfo_type)
18833 {
18834 /* A zero macinfo type indicates the end of the macro
18835 information. */
18836 case 0:
18837 break;
2e276125 18838
cf2c3c16
TT
18839 case DW_MACRO_GNU_define:
18840 case DW_MACRO_GNU_undef:
18841 case DW_MACRO_GNU_define_indirect:
18842 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18843 case DW_MACRO_GNU_define_indirect_alt:
18844 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18845 {
891d2f0b 18846 unsigned int bytes_read;
2e276125 18847 int line;
d521ce57 18848 const char *body;
cf2c3c16 18849 int is_define;
2e276125 18850
cf2c3c16
TT
18851 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18852 mac_ptr += bytes_read;
18853
18854 if (macinfo_type == DW_MACRO_GNU_define
18855 || macinfo_type == DW_MACRO_GNU_undef)
18856 {
18857 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18858 mac_ptr += bytes_read;
18859 }
18860 else
18861 {
18862 LONGEST str_offset;
18863
18864 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18865 mac_ptr += offset_size;
2e276125 18866
36586728 18867 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18868 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18869 || section_is_dwz)
36586728
TT
18870 {
18871 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18872
18873 body = read_indirect_string_from_dwz (dwz, str_offset);
18874 }
18875 else
18876 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18877 }
18878
18879 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18880 || macinfo_type == DW_MACRO_GNU_define_indirect
18881 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18882 if (! current_file)
757a13d0
JK
18883 {
18884 /* DWARF violation as no main source is present. */
18885 complaint (&symfile_complaints,
18886 _("debug info with no main source gives macro %s "
18887 "on line %d: %s"),
cf2c3c16
TT
18888 is_define ? _("definition") : _("undefinition"),
18889 line, body);
757a13d0
JK
18890 break;
18891 }
3e43a32a
MS
18892 if ((line == 0 && !at_commandline)
18893 || (line != 0 && at_commandline))
4d3c2250 18894 complaint (&symfile_complaints,
757a13d0
JK
18895 _("debug info gives %s macro %s with %s line %d: %s"),
18896 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18897 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18898 line == 0 ? _("zero") : _("non-zero"), line, body);
18899
cf2c3c16 18900 if (is_define)
757a13d0 18901 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18902 else
18903 {
18904 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18905 || macinfo_type == DW_MACRO_GNU_undef_indirect
18906 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18907 macro_undef (current_file, line, body);
18908 }
2e276125
JB
18909 }
18910 break;
18911
cf2c3c16 18912 case DW_MACRO_GNU_start_file:
2e276125 18913 {
891d2f0b 18914 unsigned int bytes_read;
2e276125
JB
18915 int line, file;
18916
18917 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18918 mac_ptr += bytes_read;
18919 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18920 mac_ptr += bytes_read;
18921
3e43a32a
MS
18922 if ((line == 0 && !at_commandline)
18923 || (line != 0 && at_commandline))
757a13d0
JK
18924 complaint (&symfile_complaints,
18925 _("debug info gives source %d included "
18926 "from %s at %s line %d"),
18927 file, at_commandline ? _("command-line") : _("file"),
18928 line == 0 ? _("zero") : _("non-zero"), line);
18929
18930 if (at_commandline)
18931 {
cf2c3c16
TT
18932 /* This DW_MACRO_GNU_start_file was executed in the
18933 pass one. */
757a13d0
JK
18934 at_commandline = 0;
18935 }
18936 else
18937 current_file = macro_start_file (file, line,
18938 current_file, comp_dir,
cf2c3c16 18939 lh, objfile);
2e276125
JB
18940 }
18941 break;
18942
cf2c3c16 18943 case DW_MACRO_GNU_end_file:
2e276125 18944 if (! current_file)
4d3c2250 18945 complaint (&symfile_complaints,
3e43a32a
MS
18946 _("macro debug info has an unmatched "
18947 "`close_file' directive"));
2e276125
JB
18948 else
18949 {
18950 current_file = current_file->included_by;
18951 if (! current_file)
18952 {
cf2c3c16 18953 enum dwarf_macro_record_type next_type;
2e276125
JB
18954
18955 /* GCC circa March 2002 doesn't produce the zero
18956 type byte marking the end of the compilation
18957 unit. Complain if it's not there, but exit no
18958 matter what. */
18959
18960 /* Do we at least have room for a macinfo type byte? */
18961 if (mac_ptr >= mac_end)
18962 {
f664829e 18963 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18964 return;
18965 }
18966
18967 /* We don't increment mac_ptr here, so this is just
18968 a look-ahead. */
18969 next_type = read_1_byte (abfd, mac_ptr);
18970 if (next_type != 0)
4d3c2250 18971 complaint (&symfile_complaints,
3e43a32a
MS
18972 _("no terminating 0-type entry for "
18973 "macros in `.debug_macinfo' section"));
2e276125
JB
18974
18975 return;
18976 }
18977 }
18978 break;
18979
cf2c3c16 18980 case DW_MACRO_GNU_transparent_include:
36586728 18981 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18982 {
18983 LONGEST offset;
8fc3fc34 18984 void **slot;
a036ba48
TT
18985 bfd *include_bfd = abfd;
18986 struct dwarf2_section_info *include_section = section;
18987 struct dwarf2_section_info alt_section;
d521ce57 18988 const gdb_byte *include_mac_end = mac_end;
a036ba48 18989 int is_dwz = section_is_dwz;
d521ce57 18990 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
18991
18992 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18993 mac_ptr += offset_size;
18994
a036ba48
TT
18995 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18996 {
18997 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18998
18999 dwarf2_read_section (dwarf2_per_objfile->objfile,
19000 &dwz->macro);
19001
19002 include_bfd = dwz->macro.asection->owner;
19003 include_section = &dwz->macro;
19004 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19005 is_dwz = 1;
19006 }
19007
19008 new_mac_ptr = include_section->buffer + offset;
19009 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19010
8fc3fc34
TT
19011 if (*slot != NULL)
19012 {
19013 /* This has actually happened; see
19014 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19015 complaint (&symfile_complaints,
19016 _("recursive DW_MACRO_GNU_transparent_include in "
19017 ".debug_macro section"));
19018 }
19019 else
19020 {
d521ce57 19021 *slot = (void *) new_mac_ptr;
36586728 19022
a036ba48 19023 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19024 include_mac_end, current_file,
8fc3fc34 19025 lh, comp_dir,
36586728 19026 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19027 offset_size, objfile, include_hash);
19028
d521ce57 19029 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19030 }
cf2c3c16
TT
19031 }
19032 break;
19033
2e276125 19034 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19035 if (!section_is_gnu)
19036 {
19037 unsigned int bytes_read;
19038 int constant;
2e276125 19039
cf2c3c16
TT
19040 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19041 mac_ptr += bytes_read;
19042 read_direct_string (abfd, mac_ptr, &bytes_read);
19043 mac_ptr += bytes_read;
2e276125 19044
cf2c3c16
TT
19045 /* We don't recognize any vendor extensions. */
19046 break;
19047 }
19048 /* FALLTHROUGH */
19049
19050 default:
19051 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19052 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19053 section);
19054 if (mac_ptr == NULL)
19055 return;
19056 break;
2e276125 19057 }
757a13d0 19058 } while (macinfo_type != 0);
2e276125 19059}
8e19ed76 19060
cf2c3c16 19061static void
09262596 19062dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19063 const char *comp_dir, int section_is_gnu)
cf2c3c16 19064{
bb5ed363 19065 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19066 struct line_header *lh = cu->line_header;
19067 bfd *abfd;
d521ce57 19068 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19069 struct macro_source_file *current_file = 0;
19070 enum dwarf_macro_record_type macinfo_type;
19071 unsigned int offset_size = cu->header.offset_size;
d521ce57 19072 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19073 struct cleanup *cleanup;
19074 htab_t include_hash;
19075 void **slot;
09262596
DE
19076 struct dwarf2_section_info *section;
19077 const char *section_name;
19078
19079 if (cu->dwo_unit != NULL)
19080 {
19081 if (section_is_gnu)
19082 {
19083 section = &cu->dwo_unit->dwo_file->sections.macro;
19084 section_name = ".debug_macro.dwo";
19085 }
19086 else
19087 {
19088 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19089 section_name = ".debug_macinfo.dwo";
19090 }
19091 }
19092 else
19093 {
19094 if (section_is_gnu)
19095 {
19096 section = &dwarf2_per_objfile->macro;
19097 section_name = ".debug_macro";
19098 }
19099 else
19100 {
19101 section = &dwarf2_per_objfile->macinfo;
19102 section_name = ".debug_macinfo";
19103 }
19104 }
cf2c3c16 19105
bb5ed363 19106 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19107 if (section->buffer == NULL)
19108 {
fceca515 19109 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19110 return;
19111 }
09262596 19112 abfd = section->asection->owner;
cf2c3c16
TT
19113
19114 /* First pass: Find the name of the base filename.
19115 This filename is needed in order to process all macros whose definition
19116 (or undefinition) comes from the command line. These macros are defined
19117 before the first DW_MACINFO_start_file entry, and yet still need to be
19118 associated to the base file.
19119
19120 To determine the base file name, we scan the macro definitions until we
19121 reach the first DW_MACINFO_start_file entry. We then initialize
19122 CURRENT_FILE accordingly so that any macro definition found before the
19123 first DW_MACINFO_start_file can still be associated to the base file. */
19124
19125 mac_ptr = section->buffer + offset;
19126 mac_end = section->buffer + section->size;
19127
19128 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19129 &offset_size, section_is_gnu);
19130 if (mac_ptr == NULL)
19131 {
19132 /* We already issued a complaint. */
19133 return;
19134 }
19135
19136 do
19137 {
19138 /* Do we at least have room for a macinfo type byte? */
19139 if (mac_ptr >= mac_end)
19140 {
19141 /* Complaint is printed during the second pass as GDB will probably
19142 stop the first pass earlier upon finding
19143 DW_MACINFO_start_file. */
19144 break;
19145 }
19146
19147 macinfo_type = read_1_byte (abfd, mac_ptr);
19148 mac_ptr++;
19149
19150 /* Note that we rely on the fact that the corresponding GNU and
19151 DWARF constants are the same. */
19152 switch (macinfo_type)
19153 {
19154 /* A zero macinfo type indicates the end of the macro
19155 information. */
19156 case 0:
19157 break;
19158
19159 case DW_MACRO_GNU_define:
19160 case DW_MACRO_GNU_undef:
19161 /* Only skip the data by MAC_PTR. */
19162 {
19163 unsigned int bytes_read;
19164
19165 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19166 mac_ptr += bytes_read;
19167 read_direct_string (abfd, mac_ptr, &bytes_read);
19168 mac_ptr += bytes_read;
19169 }
19170 break;
19171
19172 case DW_MACRO_GNU_start_file:
19173 {
19174 unsigned int bytes_read;
19175 int line, file;
19176
19177 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19178 mac_ptr += bytes_read;
19179 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19180 mac_ptr += bytes_read;
19181
19182 current_file = macro_start_file (file, line, current_file,
bb5ed363 19183 comp_dir, lh, objfile);
cf2c3c16
TT
19184 }
19185 break;
19186
19187 case DW_MACRO_GNU_end_file:
19188 /* No data to skip by MAC_PTR. */
19189 break;
19190
19191 case DW_MACRO_GNU_define_indirect:
19192 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19193 case DW_MACRO_GNU_define_indirect_alt:
19194 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19195 {
19196 unsigned int bytes_read;
19197
19198 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19199 mac_ptr += bytes_read;
19200 mac_ptr += offset_size;
19201 }
19202 break;
19203
19204 case DW_MACRO_GNU_transparent_include:
f7a35f02 19205 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19206 /* Note that, according to the spec, a transparent include
19207 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19208 skip this opcode. */
19209 mac_ptr += offset_size;
19210 break;
19211
19212 case DW_MACINFO_vendor_ext:
19213 /* Only skip the data by MAC_PTR. */
19214 if (!section_is_gnu)
19215 {
19216 unsigned int bytes_read;
19217
19218 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19219 mac_ptr += bytes_read;
19220 read_direct_string (abfd, mac_ptr, &bytes_read);
19221 mac_ptr += bytes_read;
19222 }
19223 /* FALLTHROUGH */
19224
19225 default:
19226 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19227 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19228 section);
19229 if (mac_ptr == NULL)
19230 return;
19231 break;
19232 }
19233 } while (macinfo_type != 0 && current_file == NULL);
19234
19235 /* Second pass: Process all entries.
19236
19237 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19238 command-line macro definitions/undefinitions. This flag is unset when we
19239 reach the first DW_MACINFO_start_file entry. */
19240
8fc3fc34
TT
19241 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19242 NULL, xcalloc, xfree);
19243 cleanup = make_cleanup_htab_delete (include_hash);
19244 mac_ptr = section->buffer + offset;
19245 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19246 *slot = (void *) mac_ptr;
8fc3fc34 19247 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19248 current_file, lh, comp_dir, section,
19249 section_is_gnu, 0,
8fc3fc34
TT
19250 offset_size, objfile, include_hash);
19251 do_cleanups (cleanup);
cf2c3c16
TT
19252}
19253
8e19ed76 19254/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19255 if so return true else false. */
380bca97 19256
8e19ed76
PS
19257static int
19258attr_form_is_block (struct attribute *attr)
19259{
19260 return (attr == NULL ? 0 :
19261 attr->form == DW_FORM_block1
19262 || attr->form == DW_FORM_block2
19263 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19264 || attr->form == DW_FORM_block
19265 || attr->form == DW_FORM_exprloc);
8e19ed76 19266}
4c2df51b 19267
c6a0999f
JB
19268/* Return non-zero if ATTR's value is a section offset --- classes
19269 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19270 You may use DW_UNSND (attr) to retrieve such offsets.
19271
19272 Section 7.5.4, "Attribute Encodings", explains that no attribute
19273 may have a value that belongs to more than one of these classes; it
19274 would be ambiguous if we did, because we use the same forms for all
19275 of them. */
380bca97 19276
3690dd37
JB
19277static int
19278attr_form_is_section_offset (struct attribute *attr)
19279{
19280 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19281 || attr->form == DW_FORM_data8
19282 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19283}
19284
3690dd37
JB
19285/* Return non-zero if ATTR's value falls in the 'constant' class, or
19286 zero otherwise. When this function returns true, you can apply
19287 dwarf2_get_attr_constant_value to it.
19288
19289 However, note that for some attributes you must check
19290 attr_form_is_section_offset before using this test. DW_FORM_data4
19291 and DW_FORM_data8 are members of both the constant class, and of
19292 the classes that contain offsets into other debug sections
19293 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19294 that, if an attribute's can be either a constant or one of the
19295 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19296 taken as section offsets, not constants. */
380bca97 19297
3690dd37
JB
19298static int
19299attr_form_is_constant (struct attribute *attr)
19300{
19301 switch (attr->form)
19302 {
19303 case DW_FORM_sdata:
19304 case DW_FORM_udata:
19305 case DW_FORM_data1:
19306 case DW_FORM_data2:
19307 case DW_FORM_data4:
19308 case DW_FORM_data8:
19309 return 1;
19310 default:
19311 return 0;
19312 }
19313}
19314
3019eac3
DE
19315/* Return the .debug_loc section to use for CU.
19316 For DWO files use .debug_loc.dwo. */
19317
19318static struct dwarf2_section_info *
19319cu_debug_loc_section (struct dwarf2_cu *cu)
19320{
19321 if (cu->dwo_unit)
19322 return &cu->dwo_unit->dwo_file->sections.loc;
19323 return &dwarf2_per_objfile->loc;
19324}
19325
8cf6f0b1
TT
19326/* A helper function that fills in a dwarf2_loclist_baton. */
19327
19328static void
19329fill_in_loclist_baton (struct dwarf2_cu *cu,
19330 struct dwarf2_loclist_baton *baton,
19331 struct attribute *attr)
19332{
3019eac3
DE
19333 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19334
19335 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19336
19337 baton->per_cu = cu->per_cu;
19338 gdb_assert (baton->per_cu);
19339 /* We don't know how long the location list is, but make sure we
19340 don't run off the edge of the section. */
3019eac3
DE
19341 baton->size = section->size - DW_UNSND (attr);
19342 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19343 baton->base_address = cu->base_address;
f664829e 19344 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19345}
19346
4c2df51b
DJ
19347static void
19348dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19349 struct dwarf2_cu *cu, int is_block)
4c2df51b 19350{
bb5ed363 19351 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19352 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19353
3690dd37 19354 if (attr_form_is_section_offset (attr)
3019eac3 19355 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19356 the section. If so, fall through to the complaint in the
19357 other branch. */
3019eac3 19358 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19359 {
0d53c4c4 19360 struct dwarf2_loclist_baton *baton;
4c2df51b 19361
bb5ed363 19362 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19363 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19364
8cf6f0b1 19365 fill_in_loclist_baton (cu, baton, attr);
be391dca 19366
d00adf39 19367 if (cu->base_known == 0)
0d53c4c4 19368 complaint (&symfile_complaints,
3e43a32a
MS
19369 _("Location list used without "
19370 "specifying the CU base address."));
4c2df51b 19371
f1e6e072
TT
19372 SYMBOL_ACLASS_INDEX (sym) = (is_block
19373 ? dwarf2_loclist_block_index
19374 : dwarf2_loclist_index);
0d53c4c4
DJ
19375 SYMBOL_LOCATION_BATON (sym) = baton;
19376 }
19377 else
19378 {
19379 struct dwarf2_locexpr_baton *baton;
19380
bb5ed363 19381 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19382 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19383 baton->per_cu = cu->per_cu;
19384 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19385
19386 if (attr_form_is_block (attr))
19387 {
19388 /* Note that we're just copying the block's data pointer
19389 here, not the actual data. We're still pointing into the
6502dd73
DJ
19390 info_buffer for SYM's objfile; right now we never release
19391 that buffer, but when we do clean up properly this may
19392 need to change. */
0d53c4c4
DJ
19393 baton->size = DW_BLOCK (attr)->size;
19394 baton->data = DW_BLOCK (attr)->data;
19395 }
19396 else
19397 {
19398 dwarf2_invalid_attrib_class_complaint ("location description",
19399 SYMBOL_NATURAL_NAME (sym));
19400 baton->size = 0;
0d53c4c4 19401 }
6e70227d 19402
f1e6e072
TT
19403 SYMBOL_ACLASS_INDEX (sym) = (is_block
19404 ? dwarf2_locexpr_block_index
19405 : dwarf2_locexpr_index);
0d53c4c4
DJ
19406 SYMBOL_LOCATION_BATON (sym) = baton;
19407 }
4c2df51b 19408}
6502dd73 19409
9aa1f1e3
TT
19410/* Return the OBJFILE associated with the compilation unit CU. If CU
19411 came from a separate debuginfo file, then the master objfile is
19412 returned. */
ae0d2f24
UW
19413
19414struct objfile *
19415dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19416{
9291a0cd 19417 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19418
19419 /* Return the master objfile, so that we can report and look up the
19420 correct file containing this variable. */
19421 if (objfile->separate_debug_objfile_backlink)
19422 objfile = objfile->separate_debug_objfile_backlink;
19423
19424 return objfile;
19425}
19426
96408a79
SA
19427/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19428 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19429 CU_HEADERP first. */
19430
19431static const struct comp_unit_head *
19432per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19433 struct dwarf2_per_cu_data *per_cu)
19434{
d521ce57 19435 const gdb_byte *info_ptr;
96408a79
SA
19436
19437 if (per_cu->cu)
19438 return &per_cu->cu->header;
19439
8a0459fd 19440 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19441
19442 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19443 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19444
19445 return cu_headerp;
19446}
19447
ae0d2f24
UW
19448/* Return the address size given in the compilation unit header for CU. */
19449
98714339 19450int
ae0d2f24
UW
19451dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19452{
96408a79
SA
19453 struct comp_unit_head cu_header_local;
19454 const struct comp_unit_head *cu_headerp;
c471e790 19455
96408a79
SA
19456 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19457
19458 return cu_headerp->addr_size;
ae0d2f24
UW
19459}
19460
9eae7c52
TT
19461/* Return the offset size given in the compilation unit header for CU. */
19462
19463int
19464dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19465{
96408a79
SA
19466 struct comp_unit_head cu_header_local;
19467 const struct comp_unit_head *cu_headerp;
9c6c53f7 19468
96408a79
SA
19469 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19470
19471 return cu_headerp->offset_size;
19472}
19473
19474/* See its dwarf2loc.h declaration. */
19475
19476int
19477dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19478{
19479 struct comp_unit_head cu_header_local;
19480 const struct comp_unit_head *cu_headerp;
19481
19482 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19483
19484 if (cu_headerp->version == 2)
19485 return cu_headerp->addr_size;
19486 else
19487 return cu_headerp->offset_size;
181cebd4
JK
19488}
19489
9aa1f1e3
TT
19490/* Return the text offset of the CU. The returned offset comes from
19491 this CU's objfile. If this objfile came from a separate debuginfo
19492 file, then the offset may be different from the corresponding
19493 offset in the parent objfile. */
19494
19495CORE_ADDR
19496dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19497{
bb3fa9d0 19498 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19499
19500 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19501}
19502
348e048f
DE
19503/* Locate the .debug_info compilation unit from CU's objfile which contains
19504 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19505
19506static struct dwarf2_per_cu_data *
b64f50a1 19507dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19508 unsigned int offset_in_dwz,
ae038cb0
DJ
19509 struct objfile *objfile)
19510{
19511 struct dwarf2_per_cu_data *this_cu;
19512 int low, high;
36586728 19513 const sect_offset *cu_off;
ae038cb0 19514
ae038cb0
DJ
19515 low = 0;
19516 high = dwarf2_per_objfile->n_comp_units - 1;
19517 while (high > low)
19518 {
36586728 19519 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19520 int mid = low + (high - low) / 2;
9a619af0 19521
36586728
TT
19522 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19523 cu_off = &mid_cu->offset;
19524 if (mid_cu->is_dwz > offset_in_dwz
19525 || (mid_cu->is_dwz == offset_in_dwz
19526 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19527 high = mid;
19528 else
19529 low = mid + 1;
19530 }
19531 gdb_assert (low == high);
36586728
TT
19532 this_cu = dwarf2_per_objfile->all_comp_units[low];
19533 cu_off = &this_cu->offset;
19534 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19535 {
36586728 19536 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19537 error (_("Dwarf Error: could not find partial DIE containing "
19538 "offset 0x%lx [in module %s]"),
b64f50a1 19539 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19540
b64f50a1
JK
19541 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19542 <= offset.sect_off);
ae038cb0
DJ
19543 return dwarf2_per_objfile->all_comp_units[low-1];
19544 }
19545 else
19546 {
19547 this_cu = dwarf2_per_objfile->all_comp_units[low];
19548 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19549 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19550 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19551 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19552 return this_cu;
19553 }
19554}
19555
23745b47 19556/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19557
9816fde3 19558static void
23745b47 19559init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19560{
9816fde3 19561 memset (cu, 0, sizeof (*cu));
23745b47
DE
19562 per_cu->cu = cu;
19563 cu->per_cu = per_cu;
19564 cu->objfile = per_cu->objfile;
93311388 19565 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19566}
19567
19568/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19569
19570static void
95554aad
TT
19571prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19572 enum language pretend_language)
9816fde3
JK
19573{
19574 struct attribute *attr;
19575
19576 /* Set the language we're debugging. */
19577 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19578 if (attr)
19579 set_cu_language (DW_UNSND (attr), cu);
19580 else
9cded63f 19581 {
95554aad 19582 cu->language = pretend_language;
9cded63f
TT
19583 cu->language_defn = language_def (cu->language);
19584 }
dee91e82
DE
19585
19586 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19587 if (attr)
19588 cu->producer = DW_STRING (attr);
93311388
DE
19589}
19590
ae038cb0
DJ
19591/* Release one cached compilation unit, CU. We unlink it from the tree
19592 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19593 the caller is responsible for that.
19594 NOTE: DATA is a void * because this function is also used as a
19595 cleanup routine. */
ae038cb0
DJ
19596
19597static void
68dc6402 19598free_heap_comp_unit (void *data)
ae038cb0
DJ
19599{
19600 struct dwarf2_cu *cu = data;
19601
23745b47
DE
19602 gdb_assert (cu->per_cu != NULL);
19603 cu->per_cu->cu = NULL;
ae038cb0
DJ
19604 cu->per_cu = NULL;
19605
19606 obstack_free (&cu->comp_unit_obstack, NULL);
19607
19608 xfree (cu);
19609}
19610
72bf9492 19611/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19612 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19613 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19614
19615static void
19616free_stack_comp_unit (void *data)
19617{
19618 struct dwarf2_cu *cu = data;
19619
23745b47
DE
19620 gdb_assert (cu->per_cu != NULL);
19621 cu->per_cu->cu = NULL;
19622 cu->per_cu = NULL;
19623
72bf9492
DJ
19624 obstack_free (&cu->comp_unit_obstack, NULL);
19625 cu->partial_dies = NULL;
ae038cb0
DJ
19626}
19627
19628/* Free all cached compilation units. */
19629
19630static void
19631free_cached_comp_units (void *data)
19632{
19633 struct dwarf2_per_cu_data *per_cu, **last_chain;
19634
19635 per_cu = dwarf2_per_objfile->read_in_chain;
19636 last_chain = &dwarf2_per_objfile->read_in_chain;
19637 while (per_cu != NULL)
19638 {
19639 struct dwarf2_per_cu_data *next_cu;
19640
19641 next_cu = per_cu->cu->read_in_chain;
19642
68dc6402 19643 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19644 *last_chain = next_cu;
19645
19646 per_cu = next_cu;
19647 }
19648}
19649
19650/* Increase the age counter on each cached compilation unit, and free
19651 any that are too old. */
19652
19653static void
19654age_cached_comp_units (void)
19655{
19656 struct dwarf2_per_cu_data *per_cu, **last_chain;
19657
19658 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19659 per_cu = dwarf2_per_objfile->read_in_chain;
19660 while (per_cu != NULL)
19661 {
19662 per_cu->cu->last_used ++;
19663 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19664 dwarf2_mark (per_cu->cu);
19665 per_cu = per_cu->cu->read_in_chain;
19666 }
19667
19668 per_cu = dwarf2_per_objfile->read_in_chain;
19669 last_chain = &dwarf2_per_objfile->read_in_chain;
19670 while (per_cu != NULL)
19671 {
19672 struct dwarf2_per_cu_data *next_cu;
19673
19674 next_cu = per_cu->cu->read_in_chain;
19675
19676 if (!per_cu->cu->mark)
19677 {
68dc6402 19678 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19679 *last_chain = next_cu;
19680 }
19681 else
19682 last_chain = &per_cu->cu->read_in_chain;
19683
19684 per_cu = next_cu;
19685 }
19686}
19687
19688/* Remove a single compilation unit from the cache. */
19689
19690static void
dee91e82 19691free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19692{
19693 struct dwarf2_per_cu_data *per_cu, **last_chain;
19694
19695 per_cu = dwarf2_per_objfile->read_in_chain;
19696 last_chain = &dwarf2_per_objfile->read_in_chain;
19697 while (per_cu != NULL)
19698 {
19699 struct dwarf2_per_cu_data *next_cu;
19700
19701 next_cu = per_cu->cu->read_in_chain;
19702
dee91e82 19703 if (per_cu == target_per_cu)
ae038cb0 19704 {
68dc6402 19705 free_heap_comp_unit (per_cu->cu);
dee91e82 19706 per_cu->cu = NULL;
ae038cb0
DJ
19707 *last_chain = next_cu;
19708 break;
19709 }
19710 else
19711 last_chain = &per_cu->cu->read_in_chain;
19712
19713 per_cu = next_cu;
19714 }
19715}
19716
fe3e1990
DJ
19717/* Release all extra memory associated with OBJFILE. */
19718
19719void
19720dwarf2_free_objfile (struct objfile *objfile)
19721{
19722 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19723
19724 if (dwarf2_per_objfile == NULL)
19725 return;
19726
19727 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19728 free_cached_comp_units (NULL);
19729
7b9f3c50
DE
19730 if (dwarf2_per_objfile->quick_file_names_table)
19731 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19732
fe3e1990
DJ
19733 /* Everything else should be on the objfile obstack. */
19734}
19735
dee91e82
DE
19736/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19737 We store these in a hash table separate from the DIEs, and preserve them
19738 when the DIEs are flushed out of cache.
19739
19740 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 19741 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
19742 or the type may come from a DWO file. Furthermore, while it's more logical
19743 to use per_cu->section+offset, with Fission the section with the data is in
19744 the DWO file but we don't know that section at the point we need it.
19745 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19746 because we can enter the lookup routine, get_die_type_at_offset, from
19747 outside this file, and thus won't necessarily have PER_CU->cu.
19748 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 19749
dee91e82 19750struct dwarf2_per_cu_offset_and_type
1c379e20 19751{
dee91e82 19752 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19753 sect_offset offset;
1c379e20
DJ
19754 struct type *type;
19755};
19756
dee91e82 19757/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19758
19759static hashval_t
dee91e82 19760per_cu_offset_and_type_hash (const void *item)
1c379e20 19761{
dee91e82 19762 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19763
dee91e82 19764 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19765}
19766
dee91e82 19767/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19768
19769static int
dee91e82 19770per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19771{
dee91e82
DE
19772 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19773 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19774
dee91e82
DE
19775 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19776 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19777}
19778
19779/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19780 table if necessary. For convenience, return TYPE.
19781
19782 The DIEs reading must have careful ordering to:
19783 * Not cause infite loops trying to read in DIEs as a prerequisite for
19784 reading current DIE.
19785 * Not trying to dereference contents of still incompletely read in types
19786 while reading in other DIEs.
19787 * Enable referencing still incompletely read in types just by a pointer to
19788 the type without accessing its fields.
19789
19790 Therefore caller should follow these rules:
19791 * Try to fetch any prerequisite types we may need to build this DIE type
19792 before building the type and calling set_die_type.
e71ec853 19793 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19794 possible before fetching more types to complete the current type.
19795 * Make the type as complete as possible before fetching more types. */
1c379e20 19796
f792889a 19797static struct type *
1c379e20
DJ
19798set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19799{
dee91e82 19800 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19801 struct objfile *objfile = cu->objfile;
1c379e20 19802
b4ba55a1
JB
19803 /* For Ada types, make sure that the gnat-specific data is always
19804 initialized (if not already set). There are a few types where
19805 we should not be doing so, because the type-specific area is
19806 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19807 where the type-specific area is used to store the floatformat).
19808 But this is not a problem, because the gnat-specific information
19809 is actually not needed for these types. */
19810 if (need_gnat_info (cu)
19811 && TYPE_CODE (type) != TYPE_CODE_FUNC
19812 && TYPE_CODE (type) != TYPE_CODE_FLT
19813 && !HAVE_GNAT_AUX_INFO (type))
19814 INIT_GNAT_SPECIFIC (type);
19815
dee91e82 19816 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19817 {
dee91e82
DE
19818 dwarf2_per_objfile->die_type_hash =
19819 htab_create_alloc_ex (127,
19820 per_cu_offset_and_type_hash,
19821 per_cu_offset_and_type_eq,
19822 NULL,
19823 &objfile->objfile_obstack,
19824 hashtab_obstack_allocate,
19825 dummy_obstack_deallocate);
f792889a 19826 }
1c379e20 19827
dee91e82 19828 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19829 ofs.offset = die->offset;
19830 ofs.type = type;
dee91e82
DE
19831 slot = (struct dwarf2_per_cu_offset_and_type **)
19832 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19833 if (*slot)
19834 complaint (&symfile_complaints,
19835 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19836 die->offset.sect_off);
673bfd45 19837 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19838 **slot = ofs;
f792889a 19839 return type;
1c379e20
DJ
19840}
19841
02142a6c
DE
19842/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
19843 or return NULL if the die does not have a saved type. */
1c379e20
DJ
19844
19845static struct type *
b64f50a1 19846get_die_type_at_offset (sect_offset offset,
673bfd45 19847 struct dwarf2_per_cu_data *per_cu)
1c379e20 19848{
dee91e82 19849 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19850
dee91e82 19851 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19852 return NULL;
1c379e20 19853
dee91e82 19854 ofs.per_cu = per_cu;
673bfd45 19855 ofs.offset = offset;
dee91e82 19856 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19857 if (slot)
19858 return slot->type;
19859 else
19860 return NULL;
19861}
19862
02142a6c 19863/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
19864 or return NULL if DIE does not have a saved type. */
19865
19866static struct type *
19867get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19868{
19869 return get_die_type_at_offset (die->offset, cu->per_cu);
19870}
19871
10b3939b
DJ
19872/* Add a dependence relationship from CU to REF_PER_CU. */
19873
19874static void
19875dwarf2_add_dependence (struct dwarf2_cu *cu,
19876 struct dwarf2_per_cu_data *ref_per_cu)
19877{
19878 void **slot;
19879
19880 if (cu->dependencies == NULL)
19881 cu->dependencies
19882 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19883 NULL, &cu->comp_unit_obstack,
19884 hashtab_obstack_allocate,
19885 dummy_obstack_deallocate);
19886
19887 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19888 if (*slot == NULL)
19889 *slot = ref_per_cu;
19890}
1c379e20 19891
f504f079
DE
19892/* Subroutine of dwarf2_mark to pass to htab_traverse.
19893 Set the mark field in every compilation unit in the
ae038cb0
DJ
19894 cache that we must keep because we are keeping CU. */
19895
10b3939b
DJ
19896static int
19897dwarf2_mark_helper (void **slot, void *data)
19898{
19899 struct dwarf2_per_cu_data *per_cu;
19900
19901 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19902
19903 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19904 reading of the chain. As such dependencies remain valid it is not much
19905 useful to track and undo them during QUIT cleanups. */
19906 if (per_cu->cu == NULL)
19907 return 1;
19908
10b3939b
DJ
19909 if (per_cu->cu->mark)
19910 return 1;
19911 per_cu->cu->mark = 1;
19912
19913 if (per_cu->cu->dependencies != NULL)
19914 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19915
19916 return 1;
19917}
19918
f504f079
DE
19919/* Set the mark field in CU and in every other compilation unit in the
19920 cache that we must keep because we are keeping CU. */
19921
ae038cb0
DJ
19922static void
19923dwarf2_mark (struct dwarf2_cu *cu)
19924{
19925 if (cu->mark)
19926 return;
19927 cu->mark = 1;
10b3939b
DJ
19928 if (cu->dependencies != NULL)
19929 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19930}
19931
19932static void
19933dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19934{
19935 while (per_cu)
19936 {
19937 per_cu->cu->mark = 0;
19938 per_cu = per_cu->cu->read_in_chain;
19939 }
72bf9492
DJ
19940}
19941
72bf9492
DJ
19942/* Trivial hash function for partial_die_info: the hash value of a DIE
19943 is its offset in .debug_info for this objfile. */
19944
19945static hashval_t
19946partial_die_hash (const void *item)
19947{
19948 const struct partial_die_info *part_die = item;
9a619af0 19949
b64f50a1 19950 return part_die->offset.sect_off;
72bf9492
DJ
19951}
19952
19953/* Trivial comparison function for partial_die_info structures: two DIEs
19954 are equal if they have the same offset. */
19955
19956static int
19957partial_die_eq (const void *item_lhs, const void *item_rhs)
19958{
19959 const struct partial_die_info *part_die_lhs = item_lhs;
19960 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19961
b64f50a1 19962 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19963}
19964
ae038cb0
DJ
19965static struct cmd_list_element *set_dwarf2_cmdlist;
19966static struct cmd_list_element *show_dwarf2_cmdlist;
19967
19968static void
19969set_dwarf2_cmd (char *args, int from_tty)
19970{
19971 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19972}
19973
19974static void
19975show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19976{
ae038cb0
DJ
19977 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19978}
19979
4bf44c1c 19980/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19981
19982static void
c1bd65d0 19983dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19984{
19985 struct dwarf2_per_objfile *data = d;
8b70b953 19986 int ix;
8b70b953 19987
95554aad
TT
19988 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19989 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19990 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19991
19992 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19993 VEC_free (dwarf2_per_cu_ptr,
19994 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19995
8b70b953 19996 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19997
19998 if (data->dwo_files)
19999 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20000 if (data->dwp_file)
20001 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20002
20003 if (data->dwz_file && data->dwz_file->dwz_bfd)
20004 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20005}
20006
20007\f
ae2de4f8 20008/* The "save gdb-index" command. */
9291a0cd
TT
20009
20010/* The contents of the hash table we create when building the string
20011 table. */
20012struct strtab_entry
20013{
20014 offset_type offset;
20015 const char *str;
20016};
20017
559a7a62
JK
20018/* Hash function for a strtab_entry.
20019
20020 Function is used only during write_hash_table so no index format backward
20021 compatibility is needed. */
b89be57b 20022
9291a0cd
TT
20023static hashval_t
20024hash_strtab_entry (const void *e)
20025{
20026 const struct strtab_entry *entry = e;
559a7a62 20027 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20028}
20029
20030/* Equality function for a strtab_entry. */
b89be57b 20031
9291a0cd
TT
20032static int
20033eq_strtab_entry (const void *a, const void *b)
20034{
20035 const struct strtab_entry *ea = a;
20036 const struct strtab_entry *eb = b;
20037 return !strcmp (ea->str, eb->str);
20038}
20039
20040/* Create a strtab_entry hash table. */
b89be57b 20041
9291a0cd
TT
20042static htab_t
20043create_strtab (void)
20044{
20045 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20046 xfree, xcalloc, xfree);
20047}
20048
20049/* Add a string to the constant pool. Return the string's offset in
20050 host order. */
b89be57b 20051
9291a0cd
TT
20052static offset_type
20053add_string (htab_t table, struct obstack *cpool, const char *str)
20054{
20055 void **slot;
20056 struct strtab_entry entry;
20057 struct strtab_entry *result;
20058
20059 entry.str = str;
20060 slot = htab_find_slot (table, &entry, INSERT);
20061 if (*slot)
20062 result = *slot;
20063 else
20064 {
20065 result = XNEW (struct strtab_entry);
20066 result->offset = obstack_object_size (cpool);
20067 result->str = str;
20068 obstack_grow_str0 (cpool, str);
20069 *slot = result;
20070 }
20071 return result->offset;
20072}
20073
20074/* An entry in the symbol table. */
20075struct symtab_index_entry
20076{
20077 /* The name of the symbol. */
20078 const char *name;
20079 /* The offset of the name in the constant pool. */
20080 offset_type index_offset;
20081 /* A sorted vector of the indices of all the CUs that hold an object
20082 of this name. */
20083 VEC (offset_type) *cu_indices;
20084};
20085
20086/* The symbol table. This is a power-of-2-sized hash table. */
20087struct mapped_symtab
20088{
20089 offset_type n_elements;
20090 offset_type size;
20091 struct symtab_index_entry **data;
20092};
20093
20094/* Hash function for a symtab_index_entry. */
b89be57b 20095
9291a0cd
TT
20096static hashval_t
20097hash_symtab_entry (const void *e)
20098{
20099 const struct symtab_index_entry *entry = e;
20100 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20101 sizeof (offset_type) * VEC_length (offset_type,
20102 entry->cu_indices),
20103 0);
20104}
20105
20106/* Equality function for a symtab_index_entry. */
b89be57b 20107
9291a0cd
TT
20108static int
20109eq_symtab_entry (const void *a, const void *b)
20110{
20111 const struct symtab_index_entry *ea = a;
20112 const struct symtab_index_entry *eb = b;
20113 int len = VEC_length (offset_type, ea->cu_indices);
20114 if (len != VEC_length (offset_type, eb->cu_indices))
20115 return 0;
20116 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20117 VEC_address (offset_type, eb->cu_indices),
20118 sizeof (offset_type) * len);
20119}
20120
20121/* Destroy a symtab_index_entry. */
b89be57b 20122
9291a0cd
TT
20123static void
20124delete_symtab_entry (void *p)
20125{
20126 struct symtab_index_entry *entry = p;
20127 VEC_free (offset_type, entry->cu_indices);
20128 xfree (entry);
20129}
20130
20131/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20132
9291a0cd 20133static htab_t
3876f04e 20134create_symbol_hash_table (void)
9291a0cd
TT
20135{
20136 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20137 delete_symtab_entry, xcalloc, xfree);
20138}
20139
20140/* Create a new mapped symtab object. */
b89be57b 20141
9291a0cd
TT
20142static struct mapped_symtab *
20143create_mapped_symtab (void)
20144{
20145 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20146 symtab->n_elements = 0;
20147 symtab->size = 1024;
20148 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20149 return symtab;
20150}
20151
20152/* Destroy a mapped_symtab. */
b89be57b 20153
9291a0cd
TT
20154static void
20155cleanup_mapped_symtab (void *p)
20156{
20157 struct mapped_symtab *symtab = p;
20158 /* The contents of the array are freed when the other hash table is
20159 destroyed. */
20160 xfree (symtab->data);
20161 xfree (symtab);
20162}
20163
20164/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20165 the slot.
20166
20167 Function is used only during write_hash_table so no index format backward
20168 compatibility is needed. */
b89be57b 20169
9291a0cd
TT
20170static struct symtab_index_entry **
20171find_slot (struct mapped_symtab *symtab, const char *name)
20172{
559a7a62 20173 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20174
20175 index = hash & (symtab->size - 1);
20176 step = ((hash * 17) & (symtab->size - 1)) | 1;
20177
20178 for (;;)
20179 {
20180 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20181 return &symtab->data[index];
20182 index = (index + step) & (symtab->size - 1);
20183 }
20184}
20185
20186/* Expand SYMTAB's hash table. */
b89be57b 20187
9291a0cd
TT
20188static void
20189hash_expand (struct mapped_symtab *symtab)
20190{
20191 offset_type old_size = symtab->size;
20192 offset_type i;
20193 struct symtab_index_entry **old_entries = symtab->data;
20194
20195 symtab->size *= 2;
20196 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20197
20198 for (i = 0; i < old_size; ++i)
20199 {
20200 if (old_entries[i])
20201 {
20202 struct symtab_index_entry **slot = find_slot (symtab,
20203 old_entries[i]->name);
20204 *slot = old_entries[i];
20205 }
20206 }
20207
20208 xfree (old_entries);
20209}
20210
156942c7
DE
20211/* Add an entry to SYMTAB. NAME is the name of the symbol.
20212 CU_INDEX is the index of the CU in which the symbol appears.
20213 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20214
9291a0cd
TT
20215static void
20216add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20217 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20218 offset_type cu_index)
20219{
20220 struct symtab_index_entry **slot;
156942c7 20221 offset_type cu_index_and_attrs;
9291a0cd
TT
20222
20223 ++symtab->n_elements;
20224 if (4 * symtab->n_elements / 3 >= symtab->size)
20225 hash_expand (symtab);
20226
20227 slot = find_slot (symtab, name);
20228 if (!*slot)
20229 {
20230 *slot = XNEW (struct symtab_index_entry);
20231 (*slot)->name = name;
156942c7 20232 /* index_offset is set later. */
9291a0cd
TT
20233 (*slot)->cu_indices = NULL;
20234 }
156942c7
DE
20235
20236 cu_index_and_attrs = 0;
20237 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20238 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20239 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20240
20241 /* We don't want to record an index value twice as we want to avoid the
20242 duplication.
20243 We process all global symbols and then all static symbols
20244 (which would allow us to avoid the duplication by only having to check
20245 the last entry pushed), but a symbol could have multiple kinds in one CU.
20246 To keep things simple we don't worry about the duplication here and
20247 sort and uniqufy the list after we've processed all symbols. */
20248 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20249}
20250
20251/* qsort helper routine for uniquify_cu_indices. */
20252
20253static int
20254offset_type_compare (const void *ap, const void *bp)
20255{
20256 offset_type a = *(offset_type *) ap;
20257 offset_type b = *(offset_type *) bp;
20258
20259 return (a > b) - (b > a);
20260}
20261
20262/* Sort and remove duplicates of all symbols' cu_indices lists. */
20263
20264static void
20265uniquify_cu_indices (struct mapped_symtab *symtab)
20266{
20267 int i;
20268
20269 for (i = 0; i < symtab->size; ++i)
20270 {
20271 struct symtab_index_entry *entry = symtab->data[i];
20272
20273 if (entry
20274 && entry->cu_indices != NULL)
20275 {
20276 unsigned int next_to_insert, next_to_check;
20277 offset_type last_value;
20278
20279 qsort (VEC_address (offset_type, entry->cu_indices),
20280 VEC_length (offset_type, entry->cu_indices),
20281 sizeof (offset_type), offset_type_compare);
20282
20283 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20284 next_to_insert = 1;
20285 for (next_to_check = 1;
20286 next_to_check < VEC_length (offset_type, entry->cu_indices);
20287 ++next_to_check)
20288 {
20289 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20290 != last_value)
20291 {
20292 last_value = VEC_index (offset_type, entry->cu_indices,
20293 next_to_check);
20294 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20295 last_value);
20296 ++next_to_insert;
20297 }
20298 }
20299 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20300 }
20301 }
9291a0cd
TT
20302}
20303
20304/* Add a vector of indices to the constant pool. */
b89be57b 20305
9291a0cd 20306static offset_type
3876f04e 20307add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20308 struct symtab_index_entry *entry)
20309{
20310 void **slot;
20311
3876f04e 20312 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20313 if (!*slot)
20314 {
20315 offset_type len = VEC_length (offset_type, entry->cu_indices);
20316 offset_type val = MAYBE_SWAP (len);
20317 offset_type iter;
20318 int i;
20319
20320 *slot = entry;
20321 entry->index_offset = obstack_object_size (cpool);
20322
20323 obstack_grow (cpool, &val, sizeof (val));
20324 for (i = 0;
20325 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20326 ++i)
20327 {
20328 val = MAYBE_SWAP (iter);
20329 obstack_grow (cpool, &val, sizeof (val));
20330 }
20331 }
20332 else
20333 {
20334 struct symtab_index_entry *old_entry = *slot;
20335 entry->index_offset = old_entry->index_offset;
20336 entry = old_entry;
20337 }
20338 return entry->index_offset;
20339}
20340
20341/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20342 constant pool entries going into the obstack CPOOL. */
b89be57b 20343
9291a0cd
TT
20344static void
20345write_hash_table (struct mapped_symtab *symtab,
20346 struct obstack *output, struct obstack *cpool)
20347{
20348 offset_type i;
3876f04e 20349 htab_t symbol_hash_table;
9291a0cd
TT
20350 htab_t str_table;
20351
3876f04e 20352 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20353 str_table = create_strtab ();
3876f04e 20354
9291a0cd
TT
20355 /* We add all the index vectors to the constant pool first, to
20356 ensure alignment is ok. */
20357 for (i = 0; i < symtab->size; ++i)
20358 {
20359 if (symtab->data[i])
3876f04e 20360 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20361 }
20362
20363 /* Now write out the hash table. */
20364 for (i = 0; i < symtab->size; ++i)
20365 {
20366 offset_type str_off, vec_off;
20367
20368 if (symtab->data[i])
20369 {
20370 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20371 vec_off = symtab->data[i]->index_offset;
20372 }
20373 else
20374 {
20375 /* While 0 is a valid constant pool index, it is not valid
20376 to have 0 for both offsets. */
20377 str_off = 0;
20378 vec_off = 0;
20379 }
20380
20381 str_off = MAYBE_SWAP (str_off);
20382 vec_off = MAYBE_SWAP (vec_off);
20383
20384 obstack_grow (output, &str_off, sizeof (str_off));
20385 obstack_grow (output, &vec_off, sizeof (vec_off));
20386 }
20387
20388 htab_delete (str_table);
3876f04e 20389 htab_delete (symbol_hash_table);
9291a0cd
TT
20390}
20391
0a5429f6
DE
20392/* Struct to map psymtab to CU index in the index file. */
20393struct psymtab_cu_index_map
20394{
20395 struct partial_symtab *psymtab;
20396 unsigned int cu_index;
20397};
20398
20399static hashval_t
20400hash_psymtab_cu_index (const void *item)
20401{
20402 const struct psymtab_cu_index_map *map = item;
20403
20404 return htab_hash_pointer (map->psymtab);
20405}
20406
20407static int
20408eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20409{
20410 const struct psymtab_cu_index_map *lhs = item_lhs;
20411 const struct psymtab_cu_index_map *rhs = item_rhs;
20412
20413 return lhs->psymtab == rhs->psymtab;
20414}
20415
20416/* Helper struct for building the address table. */
20417struct addrmap_index_data
20418{
20419 struct objfile *objfile;
20420 struct obstack *addr_obstack;
20421 htab_t cu_index_htab;
20422
20423 /* Non-zero if the previous_* fields are valid.
20424 We can't write an entry until we see the next entry (since it is only then
20425 that we know the end of the entry). */
20426 int previous_valid;
20427 /* Index of the CU in the table of all CUs in the index file. */
20428 unsigned int previous_cu_index;
0963b4bd 20429 /* Start address of the CU. */
0a5429f6
DE
20430 CORE_ADDR previous_cu_start;
20431};
20432
20433/* Write an address entry to OBSTACK. */
b89be57b 20434
9291a0cd 20435static void
0a5429f6
DE
20436add_address_entry (struct objfile *objfile, struct obstack *obstack,
20437 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20438{
0a5429f6 20439 offset_type cu_index_to_write;
948f8e3d 20440 gdb_byte addr[8];
9291a0cd
TT
20441 CORE_ADDR baseaddr;
20442
20443 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20444
0a5429f6
DE
20445 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20446 obstack_grow (obstack, addr, 8);
20447 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20448 obstack_grow (obstack, addr, 8);
20449 cu_index_to_write = MAYBE_SWAP (cu_index);
20450 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20451}
20452
20453/* Worker function for traversing an addrmap to build the address table. */
20454
20455static int
20456add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20457{
20458 struct addrmap_index_data *data = datap;
20459 struct partial_symtab *pst = obj;
0a5429f6
DE
20460
20461 if (data->previous_valid)
20462 add_address_entry (data->objfile, data->addr_obstack,
20463 data->previous_cu_start, start_addr,
20464 data->previous_cu_index);
20465
20466 data->previous_cu_start = start_addr;
20467 if (pst != NULL)
20468 {
20469 struct psymtab_cu_index_map find_map, *map;
20470 find_map.psymtab = pst;
20471 map = htab_find (data->cu_index_htab, &find_map);
20472 gdb_assert (map != NULL);
20473 data->previous_cu_index = map->cu_index;
20474 data->previous_valid = 1;
20475 }
20476 else
20477 data->previous_valid = 0;
20478
20479 return 0;
20480}
20481
20482/* Write OBJFILE's address map to OBSTACK.
20483 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20484 in the index file. */
20485
20486static void
20487write_address_map (struct objfile *objfile, struct obstack *obstack,
20488 htab_t cu_index_htab)
20489{
20490 struct addrmap_index_data addrmap_index_data;
20491
20492 /* When writing the address table, we have to cope with the fact that
20493 the addrmap iterator only provides the start of a region; we have to
20494 wait until the next invocation to get the start of the next region. */
20495
20496 addrmap_index_data.objfile = objfile;
20497 addrmap_index_data.addr_obstack = obstack;
20498 addrmap_index_data.cu_index_htab = cu_index_htab;
20499 addrmap_index_data.previous_valid = 0;
20500
20501 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20502 &addrmap_index_data);
20503
20504 /* It's highly unlikely the last entry (end address = 0xff...ff)
20505 is valid, but we should still handle it.
20506 The end address is recorded as the start of the next region, but that
20507 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20508 anyway. */
20509 if (addrmap_index_data.previous_valid)
20510 add_address_entry (objfile, obstack,
20511 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20512 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20513}
20514
156942c7
DE
20515/* Return the symbol kind of PSYM. */
20516
20517static gdb_index_symbol_kind
20518symbol_kind (struct partial_symbol *psym)
20519{
20520 domain_enum domain = PSYMBOL_DOMAIN (psym);
20521 enum address_class aclass = PSYMBOL_CLASS (psym);
20522
20523 switch (domain)
20524 {
20525 case VAR_DOMAIN:
20526 switch (aclass)
20527 {
20528 case LOC_BLOCK:
20529 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20530 case LOC_TYPEDEF:
20531 return GDB_INDEX_SYMBOL_KIND_TYPE;
20532 case LOC_COMPUTED:
20533 case LOC_CONST_BYTES:
20534 case LOC_OPTIMIZED_OUT:
20535 case LOC_STATIC:
20536 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20537 case LOC_CONST:
20538 /* Note: It's currently impossible to recognize psyms as enum values
20539 short of reading the type info. For now punt. */
20540 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20541 default:
20542 /* There are other LOC_FOO values that one might want to classify
20543 as variables, but dwarf2read.c doesn't currently use them. */
20544 return GDB_INDEX_SYMBOL_KIND_OTHER;
20545 }
20546 case STRUCT_DOMAIN:
20547 return GDB_INDEX_SYMBOL_KIND_TYPE;
20548 default:
20549 return GDB_INDEX_SYMBOL_KIND_OTHER;
20550 }
20551}
20552
9291a0cd 20553/* Add a list of partial symbols to SYMTAB. */
b89be57b 20554
9291a0cd
TT
20555static void
20556write_psymbols (struct mapped_symtab *symtab,
987d643c 20557 htab_t psyms_seen,
9291a0cd
TT
20558 struct partial_symbol **psymp,
20559 int count,
987d643c
TT
20560 offset_type cu_index,
20561 int is_static)
9291a0cd
TT
20562{
20563 for (; count-- > 0; ++psymp)
20564 {
156942c7
DE
20565 struct partial_symbol *psym = *psymp;
20566 void **slot;
987d643c 20567
156942c7 20568 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20569 error (_("Ada is not currently supported by the index"));
987d643c 20570
987d643c 20571 /* Only add a given psymbol once. */
156942c7 20572 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20573 if (!*slot)
20574 {
156942c7
DE
20575 gdb_index_symbol_kind kind = symbol_kind (psym);
20576
20577 *slot = psym;
20578 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20579 is_static, kind, cu_index);
987d643c 20580 }
9291a0cd
TT
20581 }
20582}
20583
20584/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20585 exception if there is an error. */
b89be57b 20586
9291a0cd
TT
20587static void
20588write_obstack (FILE *file, struct obstack *obstack)
20589{
20590 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20591 file)
20592 != obstack_object_size (obstack))
20593 error (_("couldn't data write to file"));
20594}
20595
20596/* Unlink a file if the argument is not NULL. */
b89be57b 20597
9291a0cd
TT
20598static void
20599unlink_if_set (void *p)
20600{
20601 char **filename = p;
20602 if (*filename)
20603 unlink (*filename);
20604}
20605
1fd400ff
TT
20606/* A helper struct used when iterating over debug_types. */
20607struct signatured_type_index_data
20608{
20609 struct objfile *objfile;
20610 struct mapped_symtab *symtab;
20611 struct obstack *types_list;
987d643c 20612 htab_t psyms_seen;
1fd400ff
TT
20613 int cu_index;
20614};
20615
20616/* A helper function that writes a single signatured_type to an
20617 obstack. */
b89be57b 20618
1fd400ff
TT
20619static int
20620write_one_signatured_type (void **slot, void *d)
20621{
20622 struct signatured_type_index_data *info = d;
20623 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 20624 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
20625 gdb_byte val[8];
20626
20627 write_psymbols (info->symtab,
987d643c 20628 info->psyms_seen,
3e43a32a
MS
20629 info->objfile->global_psymbols.list
20630 + psymtab->globals_offset,
987d643c
TT
20631 psymtab->n_global_syms, info->cu_index,
20632 0);
1fd400ff 20633 write_psymbols (info->symtab,
987d643c 20634 info->psyms_seen,
3e43a32a
MS
20635 info->objfile->static_psymbols.list
20636 + psymtab->statics_offset,
987d643c
TT
20637 psymtab->n_static_syms, info->cu_index,
20638 1);
1fd400ff 20639
b64f50a1
JK
20640 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20641 entry->per_cu.offset.sect_off);
1fd400ff 20642 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20643 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20644 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20645 obstack_grow (info->types_list, val, 8);
20646 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20647 obstack_grow (info->types_list, val, 8);
20648
20649 ++info->cu_index;
20650
20651 return 1;
20652}
20653
95554aad
TT
20654/* Recurse into all "included" dependencies and write their symbols as
20655 if they appeared in this psymtab. */
20656
20657static void
20658recursively_write_psymbols (struct objfile *objfile,
20659 struct partial_symtab *psymtab,
20660 struct mapped_symtab *symtab,
20661 htab_t psyms_seen,
20662 offset_type cu_index)
20663{
20664 int i;
20665
20666 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20667 if (psymtab->dependencies[i]->user != NULL)
20668 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20669 symtab, psyms_seen, cu_index);
20670
20671 write_psymbols (symtab,
20672 psyms_seen,
20673 objfile->global_psymbols.list + psymtab->globals_offset,
20674 psymtab->n_global_syms, cu_index,
20675 0);
20676 write_psymbols (symtab,
20677 psyms_seen,
20678 objfile->static_psymbols.list + psymtab->statics_offset,
20679 psymtab->n_static_syms, cu_index,
20680 1);
20681}
20682
9291a0cd 20683/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20684
9291a0cd
TT
20685static void
20686write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20687{
20688 struct cleanup *cleanup;
20689 char *filename, *cleanup_filename;
1fd400ff
TT
20690 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20691 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20692 int i;
20693 FILE *out_file;
20694 struct mapped_symtab *symtab;
20695 offset_type val, size_of_contents, total_len;
20696 struct stat st;
987d643c 20697 htab_t psyms_seen;
0a5429f6
DE
20698 htab_t cu_index_htab;
20699 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20700
b4f2f049 20701 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20702 return;
b4f2f049 20703
9291a0cd
TT
20704 if (dwarf2_per_objfile->using_index)
20705 error (_("Cannot use an index to create the index"));
20706
8b70b953
TT
20707 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20708 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20709
9291a0cd 20710 if (stat (objfile->name, &st) < 0)
7e17e088 20711 perror_with_name (objfile->name);
9291a0cd
TT
20712
20713 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20714 INDEX_SUFFIX, (char *) NULL);
20715 cleanup = make_cleanup (xfree, filename);
20716
614c279d 20717 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
20718 if (!out_file)
20719 error (_("Can't open `%s' for writing"), filename);
20720
20721 cleanup_filename = filename;
20722 make_cleanup (unlink_if_set, &cleanup_filename);
20723
20724 symtab = create_mapped_symtab ();
20725 make_cleanup (cleanup_mapped_symtab, symtab);
20726
20727 obstack_init (&addr_obstack);
20728 make_cleanup_obstack_free (&addr_obstack);
20729
20730 obstack_init (&cu_list);
20731 make_cleanup_obstack_free (&cu_list);
20732
1fd400ff
TT
20733 obstack_init (&types_cu_list);
20734 make_cleanup_obstack_free (&types_cu_list);
20735
987d643c
TT
20736 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20737 NULL, xcalloc, xfree);
96408a79 20738 make_cleanup_htab_delete (psyms_seen);
987d643c 20739
0a5429f6
DE
20740 /* While we're scanning CU's create a table that maps a psymtab pointer
20741 (which is what addrmap records) to its index (which is what is recorded
20742 in the index file). This will later be needed to write the address
20743 table. */
20744 cu_index_htab = htab_create_alloc (100,
20745 hash_psymtab_cu_index,
20746 eq_psymtab_cu_index,
20747 NULL, xcalloc, xfree);
96408a79 20748 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20749 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20750 xmalloc (sizeof (struct psymtab_cu_index_map)
20751 * dwarf2_per_objfile->n_comp_units);
20752 make_cleanup (xfree, psymtab_cu_index_map);
20753
20754 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20755 work here. Also, the debug_types entries do not appear in
20756 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20757 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20758 {
3e43a32a
MS
20759 struct dwarf2_per_cu_data *per_cu
20760 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20761 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20762 gdb_byte val[8];
0a5429f6
DE
20763 struct psymtab_cu_index_map *map;
20764 void **slot;
9291a0cd 20765
95554aad
TT
20766 if (psymtab->user == NULL)
20767 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20768
0a5429f6
DE
20769 map = &psymtab_cu_index_map[i];
20770 map->psymtab = psymtab;
20771 map->cu_index = i;
20772 slot = htab_find_slot (cu_index_htab, map, INSERT);
20773 gdb_assert (slot != NULL);
20774 gdb_assert (*slot == NULL);
20775 *slot = map;
9291a0cd 20776
b64f50a1
JK
20777 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20778 per_cu->offset.sect_off);
9291a0cd 20779 obstack_grow (&cu_list, val, 8);
e254ef6a 20780 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20781 obstack_grow (&cu_list, val, 8);
20782 }
20783
0a5429f6
DE
20784 /* Dump the address map. */
20785 write_address_map (objfile, &addr_obstack, cu_index_htab);
20786
1fd400ff
TT
20787 /* Write out the .debug_type entries, if any. */
20788 if (dwarf2_per_objfile->signatured_types)
20789 {
20790 struct signatured_type_index_data sig_data;
20791
20792 sig_data.objfile = objfile;
20793 sig_data.symtab = symtab;
20794 sig_data.types_list = &types_cu_list;
987d643c 20795 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20796 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20797 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20798 write_one_signatured_type, &sig_data);
20799 }
20800
156942c7
DE
20801 /* Now that we've processed all symbols we can shrink their cu_indices
20802 lists. */
20803 uniquify_cu_indices (symtab);
20804
9291a0cd
TT
20805 obstack_init (&constant_pool);
20806 make_cleanup_obstack_free (&constant_pool);
20807 obstack_init (&symtab_obstack);
20808 make_cleanup_obstack_free (&symtab_obstack);
20809 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20810
20811 obstack_init (&contents);
20812 make_cleanup_obstack_free (&contents);
1fd400ff 20813 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20814 total_len = size_of_contents;
20815
20816 /* The version number. */
796a7ff8 20817 val = MAYBE_SWAP (8);
9291a0cd
TT
20818 obstack_grow (&contents, &val, sizeof (val));
20819
20820 /* The offset of the CU list from the start of the file. */
20821 val = MAYBE_SWAP (total_len);
20822 obstack_grow (&contents, &val, sizeof (val));
20823 total_len += obstack_object_size (&cu_list);
20824
1fd400ff
TT
20825 /* The offset of the types CU list from the start of the file. */
20826 val = MAYBE_SWAP (total_len);
20827 obstack_grow (&contents, &val, sizeof (val));
20828 total_len += obstack_object_size (&types_cu_list);
20829
9291a0cd
TT
20830 /* The offset of the address table from the start of the file. */
20831 val = MAYBE_SWAP (total_len);
20832 obstack_grow (&contents, &val, sizeof (val));
20833 total_len += obstack_object_size (&addr_obstack);
20834
20835 /* The offset of the symbol table from the start of the file. */
20836 val = MAYBE_SWAP (total_len);
20837 obstack_grow (&contents, &val, sizeof (val));
20838 total_len += obstack_object_size (&symtab_obstack);
20839
20840 /* The offset of the constant pool from the start of the file. */
20841 val = MAYBE_SWAP (total_len);
20842 obstack_grow (&contents, &val, sizeof (val));
20843 total_len += obstack_object_size (&constant_pool);
20844
20845 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20846
20847 write_obstack (out_file, &contents);
20848 write_obstack (out_file, &cu_list);
1fd400ff 20849 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20850 write_obstack (out_file, &addr_obstack);
20851 write_obstack (out_file, &symtab_obstack);
20852 write_obstack (out_file, &constant_pool);
20853
20854 fclose (out_file);
20855
20856 /* We want to keep the file, so we set cleanup_filename to NULL
20857 here. See unlink_if_set. */
20858 cleanup_filename = NULL;
20859
20860 do_cleanups (cleanup);
20861}
20862
90476074
TT
20863/* Implementation of the `save gdb-index' command.
20864
20865 Note that the file format used by this command is documented in the
20866 GDB manual. Any changes here must be documented there. */
11570e71 20867
9291a0cd
TT
20868static void
20869save_gdb_index_command (char *arg, int from_tty)
20870{
20871 struct objfile *objfile;
20872
20873 if (!arg || !*arg)
96d19272 20874 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20875
20876 ALL_OBJFILES (objfile)
20877 {
20878 struct stat st;
20879
20880 /* If the objfile does not correspond to an actual file, skip it. */
20881 if (stat (objfile->name, &st) < 0)
20882 continue;
20883
20884 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20885 if (dwarf2_per_objfile)
20886 {
20887 volatile struct gdb_exception except;
20888
20889 TRY_CATCH (except, RETURN_MASK_ERROR)
20890 {
20891 write_psymtabs_to_index (objfile, arg);
20892 }
20893 if (except.reason < 0)
20894 exception_fprintf (gdb_stderr, except,
20895 _("Error while writing index for `%s': "),
20896 objfile->name);
20897 }
20898 }
dce234bc
PP
20899}
20900
9291a0cd
TT
20901\f
20902
9eae7c52
TT
20903int dwarf2_always_disassemble;
20904
20905static void
20906show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20907 struct cmd_list_element *c, const char *value)
20908{
3e43a32a
MS
20909 fprintf_filtered (file,
20910 _("Whether to always disassemble "
20911 "DWARF expressions is %s.\n"),
9eae7c52
TT
20912 value);
20913}
20914
900e11f9
JK
20915static void
20916show_check_physname (struct ui_file *file, int from_tty,
20917 struct cmd_list_element *c, const char *value)
20918{
20919 fprintf_filtered (file,
20920 _("Whether to check \"physname\" is %s.\n"),
20921 value);
20922}
20923
6502dd73
DJ
20924void _initialize_dwarf2_read (void);
20925
20926void
20927_initialize_dwarf2_read (void)
20928{
96d19272
JK
20929 struct cmd_list_element *c;
20930
dce234bc 20931 dwarf2_objfile_data_key
c1bd65d0 20932 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20933
1bedd215
AC
20934 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20935Set DWARF 2 specific variables.\n\
20936Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20937 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20938 0/*allow-unknown*/, &maintenance_set_cmdlist);
20939
1bedd215
AC
20940 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20941Show DWARF 2 specific variables\n\
20942Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20943 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20944 0/*allow-unknown*/, &maintenance_show_cmdlist);
20945
20946 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20947 &dwarf2_max_cache_age, _("\
20948Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20949Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20950A higher limit means that cached compilation units will be stored\n\
20951in memory longer, and more total memory will be used. Zero disables\n\
20952caching, which can slow down startup."),
2c5b56ce 20953 NULL,
920d2a44 20954 show_dwarf2_max_cache_age,
2c5b56ce 20955 &set_dwarf2_cmdlist,
ae038cb0 20956 &show_dwarf2_cmdlist);
d97bc12b 20957
9eae7c52
TT
20958 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20959 &dwarf2_always_disassemble, _("\
20960Set whether `info address' always disassembles DWARF expressions."), _("\
20961Show whether `info address' always disassembles DWARF expressions."), _("\
20962When enabled, DWARF expressions are always printed in an assembly-like\n\
20963syntax. When disabled, expressions will be printed in a more\n\
20964conversational style, when possible."),
20965 NULL,
20966 show_dwarf2_always_disassemble,
20967 &set_dwarf2_cmdlist,
20968 &show_dwarf2_cmdlist);
20969
45cfd468
DE
20970 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20971Set debugging of the dwarf2 reader."), _("\
20972Show debugging of the dwarf2 reader."), _("\
20973When enabled, debugging messages are printed during dwarf2 reading\n\
20974and symtab expansion."),
20975 NULL,
20976 NULL,
20977 &setdebuglist, &showdebuglist);
20978
ccce17b0 20979 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20980Set debugging of the dwarf2 DIE reader."), _("\
20981Show debugging of the dwarf2 DIE reader."), _("\
20982When enabled (non-zero), DIEs are dumped after they are read in.\n\
20983The value is the maximum depth to print."),
ccce17b0
YQ
20984 NULL,
20985 NULL,
20986 &setdebuglist, &showdebuglist);
9291a0cd 20987
900e11f9
JK
20988 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20989Set cross-checking of \"physname\" code against demangler."), _("\
20990Show cross-checking of \"physname\" code against demangler."), _("\
20991When enabled, GDB's internal \"physname\" code is checked against\n\
20992the demangler."),
20993 NULL, show_check_physname,
20994 &setdebuglist, &showdebuglist);
20995
e615022a
DE
20996 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20997 no_class, &use_deprecated_index_sections, _("\
20998Set whether to use deprecated gdb_index sections."), _("\
20999Show whether to use deprecated gdb_index sections."), _("\
21000When enabled, deprecated .gdb_index sections are used anyway.\n\
21001Normally they are ignored either because of a missing feature or\n\
21002performance issue.\n\
21003Warning: This option must be enabled before gdb reads the file."),
21004 NULL,
21005 NULL,
21006 &setlist, &showlist);
21007
96d19272 21008 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21009 _("\
fc1a9d6e 21010Save a gdb-index file.\n\
11570e71 21011Usage: save gdb-index DIRECTORY"),
96d19272
JK
21012 &save_cmdlist);
21013 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21014
21015 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21016 &dwarf2_locexpr_funcs);
21017 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21018 &dwarf2_loclist_funcs);
21019
21020 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21021 &dwarf2_block_frame_base_locexpr_funcs);
21022 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21023 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21024}
This page took 3.477541 seconds and 4 git commands to generate.