Keep objfile original filename
[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
a2ce51a0
DE
205 /* The .debug_types-related CUs (TUs).
206 This is stored in malloc space because we may realloc it. */
b4dd5633 207 struct signatured_type **all_type_units;
1fd400ff 208
f4dc4d17
DE
209 /* The number of entries in all_type_unit_groups. */
210 int n_type_unit_groups;
211
212 /* Table of type unit groups.
213 This exists to make it easy to iterate over all CUs and TU groups. */
214 struct type_unit_group **all_type_unit_groups;
215
216 /* Table of struct type_unit_group objects.
217 The hash key is the DW_AT_stmt_list value. */
218 htab_t type_unit_groups;
72dca2f5 219
348e048f
DE
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
f4dc4d17
DE
224 /* Type unit statistics, to see how well the scaling improvements
225 are doing. */
226 struct tu_stats
227 {
228 int nr_uniq_abbrev_tables;
229 int nr_symtabs;
230 int nr_symtab_sharers;
231 int nr_stmt_less_type_units;
232 } tu_stats;
233
234 /* A chain of compilation units that are currently read in, so that
235 they can be freed later. */
236 struct dwarf2_per_cu_data *read_in_chain;
237
3019eac3
DE
238 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
239 This is NULL if the table hasn't been allocated yet. */
240 htab_t dwo_files;
241
80626a55
DE
242 /* Non-zero if we've check for whether there is a DWP file. */
243 int dwp_checked;
244
245 /* The DWP file if there is one, or NULL. */
246 struct dwp_file *dwp_file;
247
36586728
TT
248 /* The shared '.dwz' file, if one exists. This is used when the
249 original data was compressed using 'dwz -m'. */
250 struct dwz_file *dwz_file;
251
72dca2f5
FR
252 /* A flag indicating wether this objfile has a section loaded at a
253 VMA of 0. */
254 int has_section_at_zero;
9291a0cd 255
ae2de4f8
DE
256 /* True if we are using the mapped index,
257 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
258 unsigned char using_index;
259
ae2de4f8 260 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 261 struct mapped_index *index_table;
98bfdba5 262
7b9f3c50 263 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
264 TUs typically share line table entries with a CU, so we maintain a
265 separate table of all line table entries to support the sharing.
266 Note that while there can be way more TUs than CUs, we've already
267 sorted all the TUs into "type unit groups", grouped by their
268 DW_AT_stmt_list value. Therefore the only sharing done here is with a
269 CU and its associated TU group if there is one. */
7b9f3c50
DE
270 htab_t quick_file_names_table;
271
98bfdba5
PA
272 /* Set during partial symbol reading, to prevent queueing of full
273 symbols. */
274 int reading_partial_symbols;
673bfd45 275
dee91e82 276 /* Table mapping type DIEs to their struct type *.
673bfd45 277 This is NULL if not allocated yet.
02142a6c 278 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 279 htab_t die_type_hash;
95554aad
TT
280
281 /* The CUs we recently read. */
282 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
283};
284
285static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 286
251d32d9 287/* Default names of the debugging sections. */
c906108c 288
233a11ab
CS
289/* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
9cdd5dbd
DE
292static const struct dwarf2_debug_sections dwarf2_elf_names =
293{
251d32d9
TG
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 299 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
300 { ".debug_str", ".zdebug_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_types", ".zdebug_types" },
3019eac3 303 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
24d3216f
TT
306 { ".gdb_index", ".zgdb_index" },
307 23
251d32d9 308};
c906108c 309
80626a55 310/* List of DWO/DWP sections. */
3019eac3 311
80626a55 312static const struct dwop_section_names
3019eac3
DE
313{
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
09262596
DE
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
3019eac3
DE
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
80626a55
DE
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
3019eac3 325}
80626a55 326dwop_section_names =
3019eac3
DE
327{
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
339};
340
c906108c
SS
341/* local data types */
342
107d2387
AC
343/* The data in a compilation unit header, after target2host
344 translation, looks like this. */
c906108c 345struct comp_unit_head
a738430d 346{
c764a876 347 unsigned int length;
a738430d 348 short version;
a738430d
MK
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
b64f50a1 351 sect_offset abbrev_offset;
57349743 352
a738430d
MK
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
57349743 355
a738430d
MK
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
57349743 358
a738430d
MK
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
b64f50a1 361 sect_offset offset;
57349743 362
d00adf39
DE
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
b64f50a1 365 cu_offset first_die_offset;
a738430d 366};
c906108c 367
3da10d80
KS
368/* Type used for delaying computation of method physnames.
369 See comments for compute_delayed_physnames. */
370struct delayed_method_info
371{
372 /* The type to which the method is attached, i.e., its parent class. */
373 struct type *type;
374
375 /* The index of the method in the type's function fieldlists. */
376 int fnfield_index;
377
378 /* The index of the method in the fieldlist. */
379 int index;
380
381 /* The name of the DIE. */
382 const char *name;
383
384 /* The DIE associated with this method. */
385 struct die_info *die;
386};
387
388typedef struct delayed_method_info delayed_method_info;
389DEF_VEC_O (delayed_method_info);
390
e7c27a73
DJ
391/* Internal state when decoding a particular compilation unit. */
392struct dwarf2_cu
393{
394 /* The objfile containing this compilation unit. */
395 struct objfile *objfile;
396
d00adf39 397 /* The header of the compilation unit. */
e7c27a73 398 struct comp_unit_head header;
e142c38c 399
d00adf39
DE
400 /* Base address of this compilation unit. */
401 CORE_ADDR base_address;
402
403 /* Non-zero if base_address has been set. */
404 int base_known;
405
e142c38c
DJ
406 /* The language we are debugging. */
407 enum language language;
408 const struct language_defn *language_defn;
409
b0f35d58
DL
410 const char *producer;
411
e142c38c
DJ
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope;
422
433df2d4
DE
423 /* The abbrev table for this CU.
424 Normally this points to the abbrev table in the objfile.
425 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
426 struct abbrev_table *abbrev_table;
72bf9492 427
b64f50a1
JK
428 /* Hash table holding all the loaded partial DIEs
429 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
430 htab_t partial_dies;
431
432 /* Storage for things with the same lifetime as this read-in compilation
433 unit, including partial DIEs. */
434 struct obstack comp_unit_obstack;
435
ae038cb0
DJ
436 /* When multiple dwarf2_cu structures are living in memory, this field
437 chains them all together, so that they can be released efficiently.
438 We will probably also want a generation counter so that most-recently-used
439 compilation units are cached... */
440 struct dwarf2_per_cu_data *read_in_chain;
441
69d751e3 442 /* Backlink to our per_cu entry. */
ae038cb0
DJ
443 struct dwarf2_per_cu_data *per_cu;
444
445 /* How many compilation units ago was this CU last referenced? */
446 int last_used;
447
b64f50a1
JK
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
51545339 450 htab_t die_hash;
10b3939b
DJ
451
452 /* Full DIEs if read in. */
453 struct die_info *dies;
454
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
458 htab_t dependencies;
459
cb1df416
DJ
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
466
96408a79
SA
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
3019eac3
DE
479 struct dwo_unit *dwo_unit;
480
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
484 ULONGEST addr_base;
485
2e3cf129
DE
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
496 ULONGEST ranges_base;
497
ae038cb0
DJ
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
500
8be455d7
JK
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 505 unsigned int has_loclist : 1;
ba919b58 506
1b80a9fa
JK
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
ba919b58
TT
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 513 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 514 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
515
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
519
520 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
521};
522
10b3939b
DJ
523/* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
28dee7f5 525 read_symtab_private field of the psymtab. */
10b3939b 526
ae038cb0
DJ
527struct dwarf2_per_cu_data
528{
36586728 529 /* The start offset and length of this compilation unit.
45452591 530 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
531 initial_length_size.
532 If the DIE refers to a DWO file, this is always of the original die,
533 not the DWO file. */
b64f50a1 534 sect_offset offset;
36586728 535 unsigned int length;
ae038cb0
DJ
536
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
c764a876 539 unsigned int queued : 1;
ae038cb0 540
0d99eb77
DE
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
546
0186c6a7
DE
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
549 this is non-zero. */
3019eac3
DE
550 unsigned int is_debug_types : 1;
551
36586728
TT
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
554
a2ce51a0
DE
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
560
7ee85ab1
DE
561 /* Non-zero if the TU has been read.
562 This is used to assist the "Stay in DWO Optimization" for Fission:
563 When reading a DWO, it's faster to read TUs from the DWO instead of
564 fetching them from random other DWOs (due to comdat folding).
565 If the TU has already been read, the optimization is unnecessary
566 (and unwise - we don't want to change where gdb thinks the TU lives
567 "midflight").
568 This flag is only valid if is_debug_types is true. */
569 unsigned int tu_read : 1;
570
3019eac3
DE
571 /* The section this CU/TU lives in.
572 If the DIE refers to a DWO file, this is always the original die,
573 not the DWO file. */
8a0459fd 574 struct dwarf2_section_info *section;
348e048f 575
17ea53c3
JK
576 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
577 of the CU cache it gets reset to NULL again. */
ae038cb0 578 struct dwarf2_cu *cu;
1c379e20 579
9cdd5dbd
DE
580 /* The corresponding objfile.
581 Normally we can get the objfile from dwarf2_per_objfile.
582 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
583 struct objfile *objfile;
584
585 /* When using partial symbol tables, the 'psymtab' field is active.
586 Otherwise the 'quick' field is active. */
587 union
588 {
589 /* The partial symbol table associated with this compilation unit,
95554aad 590 or NULL for unread partial units. */
9291a0cd
TT
591 struct partial_symtab *psymtab;
592
593 /* Data needed by the "quick" functions. */
594 struct dwarf2_per_cu_quick_data *quick;
595 } v;
95554aad 596
796a7ff8
DE
597 /* The CUs we import using DW_TAG_imported_unit. This is filled in
598 while reading psymtabs, used to compute the psymtab dependencies,
599 and then cleared. Then it is filled in again while reading full
600 symbols, and only deleted when the objfile is destroyed.
601
602 This is also used to work around a difference between the way gold
603 generates .gdb_index version <=7 and the way gdb does. Arguably this
604 is a gold bug. For symbols coming from TUs, gold records in the index
605 the CU that includes the TU instead of the TU itself. This breaks
606 dw2_lookup_symbol: It assumes that if the index says symbol X lives
607 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
608 will find X. Alas TUs live in their own symtab, so after expanding CU Y
609 we need to look in TU Z to find X. Fortunately, this is akin to
610 DW_TAG_imported_unit, so we just use the same mechanism: For
611 .gdb_index version <=7 this also records the TUs that the CU referred
612 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
613 indices so we only pay a price for gold generated indices.
614 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 615 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
616};
617
348e048f
DE
618/* Entry in the signatured_types hash table. */
619
620struct signatured_type
621{
42e7ad6c 622 /* The "per_cu" object of this type.
ac9ec31b 623 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
624 N.B.: This is the first member so that it's easy to convert pointers
625 between them. */
626 struct dwarf2_per_cu_data per_cu;
627
3019eac3 628 /* The type's signature. */
348e048f
DE
629 ULONGEST signature;
630
3019eac3 631 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
632 If this TU is a DWO stub and the definition lives in a DWO file
633 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
634 cu_offset type_offset_in_tu;
635
636 /* Offset in the section of the type's DIE.
637 If the definition lives in a DWO file, this is the offset in the
638 .debug_types.dwo section.
639 The value is zero until the actual value is known.
640 Zero is otherwise not a valid section offset. */
641 sect_offset type_offset_in_section;
0186c6a7
DE
642
643 /* Type units are grouped by their DW_AT_stmt_list entry so that they
644 can share them. This points to the containing symtab. */
645 struct type_unit_group *type_unit_group;
ac9ec31b
DE
646
647 /* The type.
648 The first time we encounter this type we fully read it in and install it
649 in the symbol tables. Subsequent times we only need the type. */
650 struct type *type;
a2ce51a0
DE
651
652 /* Containing DWO unit.
653 This field is valid iff per_cu.reading_dwo_directly. */
654 struct dwo_unit *dwo_unit;
348e048f
DE
655};
656
0186c6a7
DE
657typedef struct signatured_type *sig_type_ptr;
658DEF_VEC_P (sig_type_ptr);
659
094b34ac
DE
660/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
661 This includes type_unit_group and quick_file_names. */
662
663struct stmt_list_hash
664{
665 /* The DWO unit this table is from or NULL if there is none. */
666 struct dwo_unit *dwo_unit;
667
668 /* Offset in .debug_line or .debug_line.dwo. */
669 sect_offset line_offset;
670};
671
f4dc4d17
DE
672/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
673 an object of this type. */
674
675struct type_unit_group
676{
0186c6a7 677 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
678 To simplify things we create an artificial CU that "includes" all the
679 type units using this stmt_list so that the rest of the code still has
680 a "per_cu" handle on the symtab.
681 This PER_CU is recognized by having no section. */
8a0459fd 682#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
683 struct dwarf2_per_cu_data per_cu;
684
0186c6a7
DE
685 /* The TUs that share this DW_AT_stmt_list entry.
686 This is added to while parsing type units to build partial symtabs,
687 and is deleted afterwards and not used again. */
688 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
689
690 /* The primary symtab.
094b34ac
DE
691 Type units in a group needn't all be defined in the same source file,
692 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
693 struct symtab *primary_symtab;
694
094b34ac
DE
695 /* The data used to construct the hash key. */
696 struct stmt_list_hash hash;
f4dc4d17
DE
697
698 /* The number of symtabs from the line header.
699 The value here must match line_header.num_file_names. */
700 unsigned int num_symtabs;
701
702 /* The symbol tables for this TU (obtained from the files listed in
703 DW_AT_stmt_list).
704 WARNING: The order of entries here must match the order of entries
705 in the line header. After the first TU using this type_unit_group, the
706 line header for the subsequent TUs is recreated from this. This is done
707 because we need to use the same symtabs for each TU using the same
708 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
709 there's no guarantee the line header doesn't have duplicate entries. */
710 struct symtab **symtabs;
711};
712
80626a55 713/* These sections are what may appear in a DWO file. */
3019eac3
DE
714
715struct dwo_sections
716{
717 struct dwarf2_section_info abbrev;
3019eac3
DE
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
09262596
DE
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
3019eac3
DE
722 struct dwarf2_section_info str;
723 struct dwarf2_section_info str_offsets;
80626a55
DE
724 /* In the case of a virtual DWO file, these two are unused. */
725 struct dwarf2_section_info info;
3019eac3
DE
726 VEC (dwarf2_section_info_def) *types;
727};
728
c88ee1f0 729/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
730
731struct dwo_unit
732{
733 /* Backlink to the containing struct dwo_file. */
734 struct dwo_file *dwo_file;
735
736 /* The "id" that distinguishes this CU/TU.
737 .debug_info calls this "dwo_id", .debug_types calls this "signature".
738 Since signatures came first, we stick with it for consistency. */
739 ULONGEST signature;
740
741 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 742 struct dwarf2_section_info *section;
3019eac3
DE
743
744 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
745 sect_offset offset;
746 unsigned int length;
747
748 /* For types, offset in the type's DIE of the type defined by this TU. */
749 cu_offset type_offset_in_tu;
750};
751
80626a55
DE
752/* Data for one DWO file.
753 This includes virtual DWO files that have been packaged into a
754 DWP file. */
3019eac3
DE
755
756struct dwo_file
757{
0ac5b59e 758 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
759 For virtual DWO files the name is constructed from the section offsets
760 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
761 from related CU+TUs. */
0ac5b59e
DE
762 const char *dwo_name;
763
764 /* The DW_AT_comp_dir attribute. */
765 const char *comp_dir;
3019eac3 766
80626a55
DE
767 /* The bfd, when the file is open. Otherwise this is NULL.
768 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
769 bfd *dbfd;
3019eac3
DE
770
771 /* Section info for this file. */
772 struct dwo_sections sections;
773
19c3d4c9
DE
774 /* The CU in the file.
775 We only support one because having more than one requires hacking the
776 dwo_name of each to match, which is highly unlikely to happen.
777 Doing this means all TUs can share comp_dir: We also assume that
778 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
779 struct dwo_unit *cu;
3019eac3
DE
780
781 /* Table of TUs in the file.
782 Each element is a struct dwo_unit. */
783 htab_t tus;
784};
785
80626a55
DE
786/* These sections are what may appear in a DWP file. */
787
788struct dwp_sections
789{
790 struct dwarf2_section_info str;
791 struct dwarf2_section_info cu_index;
792 struct dwarf2_section_info tu_index;
793 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
794 by section number. We don't need to record them here. */
795};
796
797/* These sections are what may appear in a virtual DWO file. */
798
799struct virtual_dwo_sections
800{
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info line;
803 struct dwarf2_section_info loc;
804 struct dwarf2_section_info macinfo;
805 struct dwarf2_section_info macro;
806 struct dwarf2_section_info str_offsets;
807 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 808 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
809 struct dwarf2_section_info info_or_types;
810};
811
812/* Contents of DWP hash tables. */
813
814struct dwp_hash_table
815{
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table, *section_pool;
818};
819
820/* Data for one DWP file. */
821
822struct dwp_file
823{
824 /* Name of the file. */
825 const char *name;
826
93417882 827 /* The bfd. */
80626a55
DE
828 bfd *dbfd;
829
830 /* Section info for this file. */
831 struct dwp_sections sections;
832
833 /* Table of CUs in the file. */
834 const struct dwp_hash_table *cus;
835
836 /* Table of TUs in the file. */
837 const struct dwp_hash_table *tus;
838
839 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
840 htab_t loaded_cutus;
841
842 /* Table to map ELF section numbers to their sections. */
843 unsigned int num_sections;
844 asection **elf_sections;
845};
846
36586728
TT
847/* This represents a '.dwz' file. */
848
849struct dwz_file
850{
851 /* A dwz file can only contain a few sections. */
852 struct dwarf2_section_info abbrev;
853 struct dwarf2_section_info info;
854 struct dwarf2_section_info str;
855 struct dwarf2_section_info line;
856 struct dwarf2_section_info macro;
2ec9a5e0 857 struct dwarf2_section_info gdb_index;
36586728
TT
858
859 /* The dwz's BFD. */
860 bfd *dwz_bfd;
861};
862
0963b4bd
MS
863/* Struct used to pass misc. parameters to read_die_and_children, et
864 al. which are used for both .debug_info and .debug_types dies.
865 All parameters here are unchanging for the life of the call. This
dee91e82 866 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
867
868struct die_reader_specs
869{
dee91e82 870 /* die_section->asection->owner. */
93311388
DE
871 bfd* abfd;
872
873 /* The CU of the DIE we are parsing. */
874 struct dwarf2_cu *cu;
875
80626a55 876 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
877 struct dwo_file *dwo_file;
878
dee91e82 879 /* The section the die comes from.
3019eac3 880 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
881 struct dwarf2_section_info *die_section;
882
883 /* die_section->buffer. */
d521ce57 884 const gdb_byte *buffer;
f664829e
DE
885
886 /* The end of the buffer. */
887 const gdb_byte *buffer_end;
a2ce51a0
DE
888
889 /* The value of the DW_AT_comp_dir attribute. */
890 const char *comp_dir;
93311388
DE
891};
892
fd820528 893/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 894typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 895 const gdb_byte *info_ptr,
dee91e82
DE
896 struct die_info *comp_unit_die,
897 int has_children,
898 void *data);
899
debd256d
JB
900/* The line number information for a compilation unit (found in the
901 .debug_line section) begins with a "statement program header",
902 which contains the following information. */
903struct line_header
904{
905 unsigned int total_length;
906 unsigned short version;
907 unsigned int header_length;
908 unsigned char minimum_instruction_length;
2dc7f7b3 909 unsigned char maximum_ops_per_instruction;
debd256d
JB
910 unsigned char default_is_stmt;
911 int line_base;
912 unsigned char line_range;
913 unsigned char opcode_base;
914
915 /* standard_opcode_lengths[i] is the number of operands for the
916 standard opcode whose value is i. This means that
917 standard_opcode_lengths[0] is unused, and the last meaningful
918 element is standard_opcode_lengths[opcode_base - 1]. */
919 unsigned char *standard_opcode_lengths;
920
921 /* The include_directories table. NOTE! These strings are not
922 allocated with xmalloc; instead, they are pointers into
923 debug_line_buffer. If you try to free them, `free' will get
924 indigestion. */
925 unsigned int num_include_dirs, include_dirs_size;
d521ce57 926 const char **include_dirs;
debd256d
JB
927
928 /* The file_names table. NOTE! These strings are not allocated
929 with xmalloc; instead, they are pointers into debug_line_buffer.
930 Don't try to free them directly. */
931 unsigned int num_file_names, file_names_size;
932 struct file_entry
c906108c 933 {
d521ce57 934 const char *name;
debd256d
JB
935 unsigned int dir_index;
936 unsigned int mod_time;
937 unsigned int length;
aaa75496 938 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 939 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
940 } *file_names;
941
942 /* The start and end of the statement program following this
6502dd73 943 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 944 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 945};
c906108c
SS
946
947/* When we construct a partial symbol table entry we only
0963b4bd 948 need this much information. */
c906108c
SS
949struct partial_die_info
950 {
72bf9492 951 /* Offset of this DIE. */
b64f50a1 952 sect_offset offset;
72bf9492
DJ
953
954 /* DWARF-2 tag for this DIE. */
955 ENUM_BITFIELD(dwarf_tag) tag : 16;
956
72bf9492
DJ
957 /* Assorted flags describing the data found in this DIE. */
958 unsigned int has_children : 1;
959 unsigned int is_external : 1;
960 unsigned int is_declaration : 1;
961 unsigned int has_type : 1;
962 unsigned int has_specification : 1;
963 unsigned int has_pc_info : 1;
481860b3 964 unsigned int may_be_inlined : 1;
72bf9492
DJ
965
966 /* Flag set if the SCOPE field of this structure has been
967 computed. */
968 unsigned int scope_set : 1;
969
fa4028e9
JB
970 /* Flag set if the DIE has a byte_size attribute. */
971 unsigned int has_byte_size : 1;
972
98bfdba5
PA
973 /* Flag set if any of the DIE's children are template arguments. */
974 unsigned int has_template_arguments : 1;
975
abc72ce4
DE
976 /* Flag set if fixup_partial_die has been called on this die. */
977 unsigned int fixup_called : 1;
978
36586728
TT
979 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
980 unsigned int is_dwz : 1;
981
982 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
983 unsigned int spec_is_dwz : 1;
984
72bf9492 985 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 986 sometimes a default name for unnamed DIEs. */
15d034d0 987 const char *name;
72bf9492 988
abc72ce4
DE
989 /* The linkage name, if present. */
990 const char *linkage_name;
991
72bf9492
DJ
992 /* The scope to prepend to our children. This is generally
993 allocated on the comp_unit_obstack, so will disappear
994 when this compilation unit leaves the cache. */
15d034d0 995 const char *scope;
72bf9492 996
95554aad
TT
997 /* Some data associated with the partial DIE. The tag determines
998 which field is live. */
999 union
1000 {
1001 /* The location description associated with this DIE, if any. */
1002 struct dwarf_block *locdesc;
1003 /* The offset of an import, for DW_TAG_imported_unit. */
1004 sect_offset offset;
1005 } d;
72bf9492
DJ
1006
1007 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1008 CORE_ADDR lowpc;
1009 CORE_ADDR highpc;
72bf9492 1010
93311388 1011 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1012 DW_AT_sibling, if any. */
abc72ce4
DE
1013 /* NOTE: This member isn't strictly necessary, read_partial_die could
1014 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1015 const gdb_byte *sibling;
72bf9492
DJ
1016
1017 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1018 DW_AT_specification (or DW_AT_abstract_origin or
1019 DW_AT_extension). */
b64f50a1 1020 sect_offset spec_offset;
72bf9492
DJ
1021
1022 /* Pointers to this DIE's parent, first child, and next sibling,
1023 if any. */
1024 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1025 };
1026
0963b4bd 1027/* This data structure holds the information of an abbrev. */
c906108c
SS
1028struct abbrev_info
1029 {
1030 unsigned int number; /* number identifying abbrev */
1031 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1032 unsigned short has_children; /* boolean */
1033 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1034 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1035 struct abbrev_info *next; /* next in chain */
1036 };
1037
1038struct attr_abbrev
1039 {
9d25dd43
DE
1040 ENUM_BITFIELD(dwarf_attribute) name : 16;
1041 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1042 };
1043
433df2d4
DE
1044/* Size of abbrev_table.abbrev_hash_table. */
1045#define ABBREV_HASH_SIZE 121
1046
1047/* Top level data structure to contain an abbreviation table. */
1048
1049struct abbrev_table
1050{
f4dc4d17
DE
1051 /* Where the abbrev table came from.
1052 This is used as a sanity check when the table is used. */
433df2d4
DE
1053 sect_offset offset;
1054
1055 /* Storage for the abbrev table. */
1056 struct obstack abbrev_obstack;
1057
1058 /* Hash table of abbrevs.
1059 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1060 It could be statically allocated, but the previous code didn't so we
1061 don't either. */
1062 struct abbrev_info **abbrevs;
1063};
1064
0963b4bd 1065/* Attributes have a name and a value. */
b60c80d6
DJ
1066struct attribute
1067 {
9d25dd43 1068 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1069 ENUM_BITFIELD(dwarf_form) form : 15;
1070
1071 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1072 field should be in u.str (existing only for DW_STRING) but it is kept
1073 here for better struct attribute alignment. */
1074 unsigned int string_is_canonical : 1;
1075
b60c80d6
DJ
1076 union
1077 {
15d034d0 1078 const char *str;
b60c80d6 1079 struct dwarf_block *blk;
43bbcdc2
PH
1080 ULONGEST unsnd;
1081 LONGEST snd;
b60c80d6 1082 CORE_ADDR addr;
ac9ec31b 1083 ULONGEST signature;
b60c80d6
DJ
1084 }
1085 u;
1086 };
1087
0963b4bd 1088/* This data structure holds a complete die structure. */
c906108c
SS
1089struct die_info
1090 {
76815b17
DE
1091 /* DWARF-2 tag for this DIE. */
1092 ENUM_BITFIELD(dwarf_tag) tag : 16;
1093
1094 /* Number of attributes */
98bfdba5
PA
1095 unsigned char num_attrs;
1096
1097 /* True if we're presently building the full type name for the
1098 type derived from this DIE. */
1099 unsigned char building_fullname : 1;
76815b17
DE
1100
1101 /* Abbrev number */
1102 unsigned int abbrev;
1103
93311388 1104 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1105 sect_offset offset;
78ba4af6
JB
1106
1107 /* The dies in a compilation unit form an n-ary tree. PARENT
1108 points to this die's parent; CHILD points to the first child of
1109 this node; and all the children of a given node are chained
4950bc1c 1110 together via their SIBLING fields. */
639d11d3
DC
1111 struct die_info *child; /* Its first child, if any. */
1112 struct die_info *sibling; /* Its next sibling, if any. */
1113 struct die_info *parent; /* Its parent, if any. */
c906108c 1114
b60c80d6
DJ
1115 /* An array of attributes, with NUM_ATTRS elements. There may be
1116 zero, but it's not common and zero-sized arrays are not
1117 sufficiently portable C. */
1118 struct attribute attrs[1];
c906108c
SS
1119 };
1120
0963b4bd 1121/* Get at parts of an attribute structure. */
c906108c
SS
1122
1123#define DW_STRING(attr) ((attr)->u.str)
8285870a 1124#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1125#define DW_UNSND(attr) ((attr)->u.unsnd)
1126#define DW_BLOCK(attr) ((attr)->u.blk)
1127#define DW_SND(attr) ((attr)->u.snd)
1128#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1129#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1130
0963b4bd 1131/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1132struct dwarf_block
1133 {
56eb65bd 1134 size_t size;
1d6edc3c
JK
1135
1136 /* Valid only if SIZE is not zero. */
d521ce57 1137 const gdb_byte *data;
c906108c
SS
1138 };
1139
c906108c
SS
1140#ifndef ATTR_ALLOC_CHUNK
1141#define ATTR_ALLOC_CHUNK 4
1142#endif
1143
c906108c
SS
1144/* Allocate fields for structs, unions and enums in this size. */
1145#ifndef DW_FIELD_ALLOC_CHUNK
1146#define DW_FIELD_ALLOC_CHUNK 4
1147#endif
1148
c906108c
SS
1149/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1150 but this would require a corresponding change in unpack_field_as_long
1151 and friends. */
1152static int bits_per_byte = 8;
1153
1154/* The routines that read and process dies for a C struct or C++ class
1155 pass lists of data member fields and lists of member function fields
1156 in an instance of a field_info structure, as defined below. */
1157struct field_info
c5aa993b 1158 {
0963b4bd 1159 /* List of data member and baseclasses fields. */
c5aa993b
JM
1160 struct nextfield
1161 {
1162 struct nextfield *next;
1163 int accessibility;
1164 int virtuality;
1165 struct field field;
1166 }
7d0ccb61 1167 *fields, *baseclasses;
c906108c 1168
7d0ccb61 1169 /* Number of fields (including baseclasses). */
c5aa993b 1170 int nfields;
c906108c 1171
c5aa993b
JM
1172 /* Number of baseclasses. */
1173 int nbaseclasses;
c906108c 1174
c5aa993b
JM
1175 /* Set if the accesibility of one of the fields is not public. */
1176 int non_public_fields;
c906108c 1177
c5aa993b
JM
1178 /* Member function fields array, entries are allocated in the order they
1179 are encountered in the object file. */
1180 struct nextfnfield
1181 {
1182 struct nextfnfield *next;
1183 struct fn_field fnfield;
1184 }
1185 *fnfields;
c906108c 1186
c5aa993b
JM
1187 /* Member function fieldlist array, contains name of possibly overloaded
1188 member function, number of overloaded member functions and a pointer
1189 to the head of the member function field chain. */
1190 struct fnfieldlist
1191 {
15d034d0 1192 const char *name;
c5aa993b
JM
1193 int length;
1194 struct nextfnfield *head;
1195 }
1196 *fnfieldlists;
c906108c 1197
c5aa993b
JM
1198 /* Number of entries in the fnfieldlists array. */
1199 int nfnfields;
98751a41
JK
1200
1201 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1202 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1203 struct typedef_field_list
1204 {
1205 struct typedef_field field;
1206 struct typedef_field_list *next;
1207 }
1208 *typedef_field_list;
1209 unsigned typedef_field_list_count;
c5aa993b 1210 };
c906108c 1211
10b3939b
DJ
1212/* One item on the queue of compilation units to read in full symbols
1213 for. */
1214struct dwarf2_queue_item
1215{
1216 struct dwarf2_per_cu_data *per_cu;
95554aad 1217 enum language pretend_language;
10b3939b
DJ
1218 struct dwarf2_queue_item *next;
1219};
1220
1221/* The current queue. */
1222static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1223
ae038cb0
DJ
1224/* Loaded secondary compilation units are kept in memory until they
1225 have not been referenced for the processing of this many
1226 compilation units. Set this to zero to disable caching. Cache
1227 sizes of up to at least twenty will improve startup time for
1228 typical inter-CU-reference binaries, at an obvious memory cost. */
1229static int dwarf2_max_cache_age = 5;
920d2a44
AC
1230static void
1231show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1232 struct cmd_list_element *c, const char *value)
1233{
3e43a32a
MS
1234 fprintf_filtered (file, _("The upper bound on the age of cached "
1235 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1236 value);
1237}
4390d890 1238\f
c906108c
SS
1239/* local function prototypes */
1240
4efb68b1 1241static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1242
918dd910
JK
1243static void dwarf2_find_base_address (struct die_info *die,
1244 struct dwarf2_cu *cu);
1245
0018ea6f
DE
1246static struct partial_symtab *create_partial_symtab
1247 (struct dwarf2_per_cu_data *per_cu, const char *name);
1248
c67a9c90 1249static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1250
72bf9492
DJ
1251static void scan_partial_symbols (struct partial_die_info *,
1252 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1253 int, struct dwarf2_cu *);
c906108c 1254
72bf9492
DJ
1255static void add_partial_symbol (struct partial_die_info *,
1256 struct dwarf2_cu *);
63d06c5c 1257
72bf9492
DJ
1258static void add_partial_namespace (struct partial_die_info *pdi,
1259 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1260 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1261
5d7cb8df
JK
1262static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1263 CORE_ADDR *highpc, int need_pc,
1264 struct dwarf2_cu *cu);
1265
72bf9492
DJ
1266static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1267 struct dwarf2_cu *cu);
91c24f0a 1268
bc30ff58
JB
1269static void add_partial_subprogram (struct partial_die_info *pdi,
1270 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1271 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1272
257e7a09
YQ
1273static void dwarf2_read_symtab (struct partial_symtab *,
1274 struct objfile *);
c906108c 1275
a14ed312 1276static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1277
433df2d4
DE
1278static struct abbrev_info *abbrev_table_lookup_abbrev
1279 (const struct abbrev_table *, unsigned int);
1280
1281static struct abbrev_table *abbrev_table_read_table
1282 (struct dwarf2_section_info *, sect_offset);
1283
1284static void abbrev_table_free (struct abbrev_table *);
1285
f4dc4d17
DE
1286static void abbrev_table_free_cleanup (void *);
1287
dee91e82
DE
1288static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1289 struct dwarf2_section_info *);
c906108c 1290
f3dd6933 1291static void dwarf2_free_abbrev_table (void *);
c906108c 1292
d521ce57 1293static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1294
dee91e82 1295static struct partial_die_info *load_partial_dies
d521ce57 1296 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1297
d521ce57
TT
1298static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1299 struct partial_die_info *,
1300 struct abbrev_info *,
1301 unsigned int,
1302 const gdb_byte *);
c906108c 1303
36586728 1304static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1305 struct dwarf2_cu *);
72bf9492
DJ
1306
1307static void fixup_partial_die (struct partial_die_info *,
1308 struct dwarf2_cu *);
1309
d521ce57
TT
1310static const gdb_byte *read_attribute (const struct die_reader_specs *,
1311 struct attribute *, struct attr_abbrev *,
1312 const gdb_byte *);
a8329558 1313
a1855c1d 1314static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1315
a1855c1d 1316static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1317
a1855c1d 1318static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1319
a1855c1d 1320static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1321
a1855c1d 1322static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1323
d521ce57 1324static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1325 unsigned int *);
c906108c 1326
d521ce57 1327static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1328
1329static LONGEST read_checked_initial_length_and_offset
d521ce57 1330 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1331 unsigned int *, unsigned int *);
613e1657 1332
d521ce57
TT
1333static LONGEST read_offset (bfd *, const gdb_byte *,
1334 const struct comp_unit_head *,
c764a876
DE
1335 unsigned int *);
1336
d521ce57 1337static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1338
f4dc4d17
DE
1339static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1340 sect_offset);
1341
d521ce57 1342static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1343
d521ce57 1344static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1345
d521ce57
TT
1346static const char *read_indirect_string (bfd *, const gdb_byte *,
1347 const struct comp_unit_head *,
1348 unsigned int *);
4bdf3d34 1349
d521ce57 1350static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1351
d521ce57 1352static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1353
d521ce57 1354static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1355
d521ce57
TT
1356static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1357 const gdb_byte *,
3019eac3
DE
1358 unsigned int *);
1359
d521ce57
TT
1360static const char *read_str_index (const struct die_reader_specs *reader,
1361 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1362
e142c38c 1363static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1364
e142c38c
DJ
1365static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1366 struct dwarf2_cu *);
c906108c 1367
348e048f 1368static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1369 unsigned int);
348e048f 1370
05cf31d1
JB
1371static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1372 struct dwarf2_cu *cu);
1373
e142c38c 1374static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1375
e142c38c 1376static struct die_info *die_specification (struct die_info *die,
f2f0e013 1377 struct dwarf2_cu **);
63d06c5c 1378
debd256d
JB
1379static void free_line_header (struct line_header *lh);
1380
3019eac3
DE
1381static struct line_header *dwarf_decode_line_header (unsigned int offset,
1382 struct dwarf2_cu *cu);
debd256d 1383
f3f5162e
DE
1384static void dwarf_decode_lines (struct line_header *, const char *,
1385 struct dwarf2_cu *, struct partial_symtab *,
1386 int);
c906108c 1387
d521ce57 1388static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1389
f4dc4d17 1390static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1391 const char *, const char *, CORE_ADDR);
f4dc4d17 1392
a14ed312 1393static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1394 struct dwarf2_cu *);
c906108c 1395
34eaf542
TT
1396static struct symbol *new_symbol_full (struct die_info *, struct type *,
1397 struct dwarf2_cu *, struct symbol *);
1398
ff39bb5e 1399static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1400 struct dwarf2_cu *);
c906108c 1401
ff39bb5e 1402static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1403 struct type *type,
1404 const char *name,
1405 struct obstack *obstack,
12df843f 1406 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1407 const gdb_byte **bytes,
98bfdba5 1408 struct dwarf2_locexpr_baton **baton);
2df3850c 1409
e7c27a73 1410static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1411
b4ba55a1
JB
1412static int need_gnat_info (struct dwarf2_cu *);
1413
3e43a32a
MS
1414static struct type *die_descriptive_type (struct die_info *,
1415 struct dwarf2_cu *);
b4ba55a1
JB
1416
1417static void set_descriptive_type (struct type *, struct die_info *,
1418 struct dwarf2_cu *);
1419
e7c27a73
DJ
1420static struct type *die_containing_type (struct die_info *,
1421 struct dwarf2_cu *);
c906108c 1422
ff39bb5e 1423static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1424 struct dwarf2_cu *);
c906108c 1425
f792889a 1426static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1427
673bfd45
DE
1428static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1429
0d5cff50 1430static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1431
6e70227d 1432static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1433 const char *suffix, int physname,
1434 struct dwarf2_cu *cu);
63d06c5c 1435
e7c27a73 1436static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1437
348e048f
DE
1438static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1439
e7c27a73 1440static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1441
e7c27a73 1442static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1443
96408a79
SA
1444static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1445
ff013f42
JK
1446static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1447 struct dwarf2_cu *, struct partial_symtab *);
1448
a14ed312 1449static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1450 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1451 struct partial_symtab *);
c906108c 1452
fae299cd
DC
1453static void get_scope_pc_bounds (struct die_info *,
1454 CORE_ADDR *, CORE_ADDR *,
1455 struct dwarf2_cu *);
1456
801e3a5b
JB
1457static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1458 CORE_ADDR, struct dwarf2_cu *);
1459
a14ed312 1460static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1461 struct dwarf2_cu *);
c906108c 1462
a14ed312 1463static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1464 struct type *, struct dwarf2_cu *);
c906108c 1465
a14ed312 1466static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1467 struct die_info *, struct type *,
e7c27a73 1468 struct dwarf2_cu *);
c906108c 1469
a14ed312 1470static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1471 struct type *,
1472 struct dwarf2_cu *);
c906108c 1473
134d01f1 1474static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1475
e7c27a73 1476static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1477
e7c27a73 1478static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1479
5d7cb8df
JK
1480static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1481
27aa8d6a
SW
1482static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1483
f55ee35c
JK
1484static struct type *read_module_type (struct die_info *die,
1485 struct dwarf2_cu *cu);
1486
38d518c9 1487static const char *namespace_name (struct die_info *die,
e142c38c 1488 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1489
134d01f1 1490static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1491
e7c27a73 1492static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1493
6e70227d 1494static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1495 struct dwarf2_cu *);
1496
bf6af496 1497static struct die_info *read_die_and_siblings_1
d521ce57 1498 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1499 struct die_info *);
639d11d3 1500
dee91e82 1501static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1502 const gdb_byte *info_ptr,
1503 const gdb_byte **new_info_ptr,
639d11d3
DC
1504 struct die_info *parent);
1505
d521ce57
TT
1506static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1507 struct die_info **, const gdb_byte *,
1508 int *, int);
3019eac3 1509
d521ce57
TT
1510static const gdb_byte *read_full_die (const struct die_reader_specs *,
1511 struct die_info **, const gdb_byte *,
1512 int *);
93311388 1513
e7c27a73 1514static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1515
15d034d0
TT
1516static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1517 struct obstack *);
71c25dea 1518
15d034d0 1519static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1520
15d034d0 1521static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1522 struct die_info *die,
1523 struct dwarf2_cu *cu);
1524
ca69b9e6
DE
1525static const char *dwarf2_physname (const char *name, struct die_info *die,
1526 struct dwarf2_cu *cu);
1527
e142c38c 1528static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1529 struct dwarf2_cu **);
9219021c 1530
f39c6ffd 1531static const char *dwarf_tag_name (unsigned int);
c906108c 1532
f39c6ffd 1533static const char *dwarf_attr_name (unsigned int);
c906108c 1534
f39c6ffd 1535static const char *dwarf_form_name (unsigned int);
c906108c 1536
a14ed312 1537static char *dwarf_bool_name (unsigned int);
c906108c 1538
f39c6ffd 1539static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1540
f9aca02d 1541static struct die_info *sibling_die (struct die_info *);
c906108c 1542
d97bc12b
DE
1543static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1544
1545static void dump_die_for_error (struct die_info *);
1546
1547static void dump_die_1 (struct ui_file *, int level, int max_level,
1548 struct die_info *);
c906108c 1549
d97bc12b 1550/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1551
51545339 1552static void store_in_ref_table (struct die_info *,
10b3939b 1553 struct dwarf2_cu *);
c906108c 1554
ff39bb5e 1555static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1556
ff39bb5e 1557static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1558
348e048f 1559static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1560 const struct attribute *,
348e048f
DE
1561 struct dwarf2_cu **);
1562
10b3939b 1563static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1564 const struct attribute *,
f2f0e013 1565 struct dwarf2_cu **);
c906108c 1566
348e048f 1567static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1568 const struct attribute *,
348e048f
DE
1569 struct dwarf2_cu **);
1570
ac9ec31b
DE
1571static struct type *get_signatured_type (struct die_info *, ULONGEST,
1572 struct dwarf2_cu *);
1573
1574static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1575 const struct attribute *,
ac9ec31b
DE
1576 struct dwarf2_cu *);
1577
e5fe5e75 1578static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1579
52dc124a 1580static void read_signatured_type (struct signatured_type *);
348e048f 1581
f4dc4d17 1582static struct type_unit_group *get_type_unit_group
ff39bb5e 1583 (struct dwarf2_cu *, const struct attribute *);
f4dc4d17
DE
1584
1585static void build_type_unit_groups (die_reader_func_ftype *, void *);
1586
c906108c
SS
1587/* memory allocation interface */
1588
7b5a2f43 1589static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1590
b60c80d6 1591static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1592
09262596 1593static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1594 const char *, int);
2e276125 1595
6e5a29e1 1596static int attr_form_is_block (const struct attribute *);
8e19ed76 1597
6e5a29e1 1598static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1599
6e5a29e1 1600static int attr_form_is_constant (const struct attribute *);
3690dd37 1601
6e5a29e1 1602static int attr_form_is_ref (const struct attribute *);
7771576e 1603
8cf6f0b1
TT
1604static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1605 struct dwarf2_loclist_baton *baton,
ff39bb5e 1606 const struct attribute *attr);
8cf6f0b1 1607
ff39bb5e 1608static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1609 struct symbol *sym,
f1e6e072
TT
1610 struct dwarf2_cu *cu,
1611 int is_block);
4c2df51b 1612
d521ce57
TT
1613static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1614 const gdb_byte *info_ptr,
1615 struct abbrev_info *abbrev);
4bb7a0a7 1616
72bf9492
DJ
1617static void free_stack_comp_unit (void *);
1618
72bf9492
DJ
1619static hashval_t partial_die_hash (const void *item);
1620
1621static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1622
ae038cb0 1623static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1624 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1625
9816fde3 1626static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1627 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1628
1629static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1630 struct die_info *comp_unit_die,
1631 enum language pretend_language);
93311388 1632
68dc6402 1633static void free_heap_comp_unit (void *);
ae038cb0
DJ
1634
1635static void free_cached_comp_units (void *);
1636
1637static void age_cached_comp_units (void);
1638
dee91e82 1639static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1640
f792889a
DJ
1641static struct type *set_die_type (struct die_info *, struct type *,
1642 struct dwarf2_cu *);
1c379e20 1643
ae038cb0
DJ
1644static void create_all_comp_units (struct objfile *);
1645
0e50663e 1646static int create_all_type_units (struct objfile *);
1fd400ff 1647
95554aad
TT
1648static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1649 enum language);
10b3939b 1650
95554aad
TT
1651static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1652 enum language);
10b3939b 1653
f4dc4d17
DE
1654static void process_full_type_unit (struct dwarf2_per_cu_data *,
1655 enum language);
1656
10b3939b
DJ
1657static void dwarf2_add_dependence (struct dwarf2_cu *,
1658 struct dwarf2_per_cu_data *);
1659
ae038cb0
DJ
1660static void dwarf2_mark (struct dwarf2_cu *);
1661
1662static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1663
b64f50a1 1664static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1665 struct dwarf2_per_cu_data *);
673bfd45 1666
f792889a 1667static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1668
9291a0cd
TT
1669static void dwarf2_release_queue (void *dummy);
1670
95554aad
TT
1671static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1672 enum language pretend_language);
1673
a0f42c21 1674static void process_queue (void);
9291a0cd
TT
1675
1676static void find_file_and_directory (struct die_info *die,
1677 struct dwarf2_cu *cu,
15d034d0 1678 const char **name, const char **comp_dir);
9291a0cd
TT
1679
1680static char *file_full_name (int file, struct line_header *lh,
1681 const char *comp_dir);
1682
d521ce57 1683static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1684 (struct comp_unit_head *header,
1685 struct dwarf2_section_info *section,
d521ce57 1686 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1687 int is_debug_types_section);
1688
fd820528 1689static void init_cutu_and_read_dies
f4dc4d17
DE
1690 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1691 int use_existing_cu, int keep,
3019eac3
DE
1692 die_reader_func_ftype *die_reader_func, void *data);
1693
dee91e82
DE
1694static void init_cutu_and_read_dies_simple
1695 (struct dwarf2_per_cu_data *this_cu,
1696 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1697
673bfd45 1698static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1699
3019eac3
DE
1700static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1701
a2ce51a0
DE
1702static struct dwo_unit *lookup_dwo_in_dwp
1703 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1704 const char *comp_dir, ULONGEST signature, int is_debug_types);
1705
1706static struct dwp_file *get_dwp_file (void);
1707
3019eac3 1708static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1709 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1710
1711static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1712 (struct signatured_type *, const char *, const char *);
3019eac3 1713
89e63ee4
DE
1714static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1715
3019eac3
DE
1716static void free_dwo_file_cleanup (void *);
1717
95554aad
TT
1718static void process_cu_includes (void);
1719
1b80a9fa 1720static void check_producer (struct dwarf2_cu *cu);
4390d890
DE
1721\f
1722/* Various complaints about symbol reading that don't abort the process. */
1723
1724static void
1725dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1726{
1727 complaint (&symfile_complaints,
1728 _("statement list doesn't fit in .debug_line section"));
1729}
1730
1731static void
1732dwarf2_debug_line_missing_file_complaint (void)
1733{
1734 complaint (&symfile_complaints,
1735 _(".debug_line section has line data without a file"));
1736}
1737
1738static void
1739dwarf2_debug_line_missing_end_sequence_complaint (void)
1740{
1741 complaint (&symfile_complaints,
1742 _(".debug_line section has line "
1743 "program sequence without an end"));
1744}
1745
1746static void
1747dwarf2_complex_location_expr_complaint (void)
1748{
1749 complaint (&symfile_complaints, _("location expression too complex"));
1750}
1751
1752static void
1753dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1754 int arg3)
1755{
1756 complaint (&symfile_complaints,
1757 _("const value length mismatch for '%s', got %d, expected %d"),
1758 arg1, arg2, arg3);
1759}
1760
1761static void
1762dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1763{
1764 complaint (&symfile_complaints,
1765 _("debug info runs off end of %s section"
1766 " [in module %s]"),
1767 section->asection->name,
1768 bfd_get_filename (section->asection->owner));
1769}
1b80a9fa 1770
4390d890
DE
1771static void
1772dwarf2_macro_malformed_definition_complaint (const char *arg1)
1773{
1774 complaint (&symfile_complaints,
1775 _("macro debug info contains a "
1776 "malformed macro definition:\n`%s'"),
1777 arg1);
1778}
1779
1780static void
1781dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1782{
1783 complaint (&symfile_complaints,
1784 _("invalid attribute class or form for '%s' in '%s'"),
1785 arg1, arg2);
1786}
1787\f
9291a0cd
TT
1788#if WORDS_BIGENDIAN
1789
1790/* Convert VALUE between big- and little-endian. */
1791static offset_type
1792byte_swap (offset_type value)
1793{
1794 offset_type result;
1795
1796 result = (value & 0xff) << 24;
1797 result |= (value & 0xff00) << 8;
1798 result |= (value & 0xff0000) >> 8;
1799 result |= (value & 0xff000000) >> 24;
1800 return result;
1801}
1802
1803#define MAYBE_SWAP(V) byte_swap (V)
1804
1805#else
1806#define MAYBE_SWAP(V) (V)
1807#endif /* WORDS_BIGENDIAN */
1808
1809/* The suffix for an index file. */
1810#define INDEX_SUFFIX ".gdb-index"
1811
c906108c 1812/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1813 information and return true if we have enough to do something.
1814 NAMES points to the dwarf2 section names, or is NULL if the standard
1815 ELF names are used. */
c906108c
SS
1816
1817int
251d32d9
TG
1818dwarf2_has_info (struct objfile *objfile,
1819 const struct dwarf2_debug_sections *names)
c906108c 1820{
be391dca
TT
1821 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1822 if (!dwarf2_per_objfile)
1823 {
1824 /* Initialize per-objfile state. */
1825 struct dwarf2_per_objfile *data
1826 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1827
be391dca
TT
1828 memset (data, 0, sizeof (*data));
1829 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1830 dwarf2_per_objfile = data;
6502dd73 1831
251d32d9
TG
1832 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1833 (void *) names);
be391dca
TT
1834 dwarf2_per_objfile->objfile = objfile;
1835 }
1836 return (dwarf2_per_objfile->info.asection != NULL
1837 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1838}
1839
251d32d9
TG
1840/* When loading sections, we look either for uncompressed section or for
1841 compressed section names. */
233a11ab
CS
1842
1843static int
251d32d9
TG
1844section_is_p (const char *section_name,
1845 const struct dwarf2_section_names *names)
233a11ab 1846{
251d32d9
TG
1847 if (names->normal != NULL
1848 && strcmp (section_name, names->normal) == 0)
1849 return 1;
1850 if (names->compressed != NULL
1851 && strcmp (section_name, names->compressed) == 0)
1852 return 1;
1853 return 0;
233a11ab
CS
1854}
1855
c906108c
SS
1856/* This function is mapped across the sections and remembers the
1857 offset and size of each of the debugging sections we are interested
1858 in. */
1859
1860static void
251d32d9 1861dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1862{
251d32d9 1863 const struct dwarf2_debug_sections *names;
dc7650b8 1864 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1865
1866 if (vnames == NULL)
1867 names = &dwarf2_elf_names;
1868 else
1869 names = (const struct dwarf2_debug_sections *) vnames;
1870
dc7650b8
JK
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1872 {
1873 }
1874 else if (section_is_p (sectp->name, &names->info))
c906108c 1875 {
dce234bc
PP
1876 dwarf2_per_objfile->info.asection = sectp;
1877 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1878 }
251d32d9 1879 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1880 {
dce234bc
PP
1881 dwarf2_per_objfile->abbrev.asection = sectp;
1882 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1883 }
251d32d9 1884 else if (section_is_p (sectp->name, &names->line))
c906108c 1885 {
dce234bc
PP
1886 dwarf2_per_objfile->line.asection = sectp;
1887 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1888 }
251d32d9 1889 else if (section_is_p (sectp->name, &names->loc))
c906108c 1890 {
dce234bc
PP
1891 dwarf2_per_objfile->loc.asection = sectp;
1892 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1893 }
251d32d9 1894 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1895 {
dce234bc
PP
1896 dwarf2_per_objfile->macinfo.asection = sectp;
1897 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1898 }
cf2c3c16
TT
1899 else if (section_is_p (sectp->name, &names->macro))
1900 {
1901 dwarf2_per_objfile->macro.asection = sectp;
1902 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1903 }
251d32d9 1904 else if (section_is_p (sectp->name, &names->str))
c906108c 1905 {
dce234bc
PP
1906 dwarf2_per_objfile->str.asection = sectp;
1907 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1908 }
3019eac3
DE
1909 else if (section_is_p (sectp->name, &names->addr))
1910 {
1911 dwarf2_per_objfile->addr.asection = sectp;
1912 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1913 }
251d32d9 1914 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1915 {
dce234bc
PP
1916 dwarf2_per_objfile->frame.asection = sectp;
1917 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1918 }
251d32d9 1919 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1920 {
dc7650b8
JK
1921 dwarf2_per_objfile->eh_frame.asection = sectp;
1922 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1923 }
251d32d9 1924 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1925 {
dce234bc
PP
1926 dwarf2_per_objfile->ranges.asection = sectp;
1927 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1928 }
251d32d9 1929 else if (section_is_p (sectp->name, &names->types))
348e048f 1930 {
8b70b953
TT
1931 struct dwarf2_section_info type_section;
1932
1933 memset (&type_section, 0, sizeof (type_section));
1934 type_section.asection = sectp;
1935 type_section.size = bfd_get_section_size (sectp);
1936
1937 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1938 &type_section);
348e048f 1939 }
251d32d9 1940 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1941 {
1942 dwarf2_per_objfile->gdb_index.asection = sectp;
1943 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1944 }
dce234bc 1945
72dca2f5
FR
1946 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1947 && bfd_section_vma (abfd, sectp) == 0)
1948 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1949}
1950
fceca515
DE
1951/* A helper function that decides whether a section is empty,
1952 or not present. */
9e0ac564
TT
1953
1954static int
1955dwarf2_section_empty_p (struct dwarf2_section_info *info)
1956{
1957 return info->asection == NULL || info->size == 0;
1958}
1959
3019eac3
DE
1960/* Read the contents of the section INFO.
1961 OBJFILE is the main object file, but not necessarily the file where
1962 the section comes from. E.g., for DWO files INFO->asection->owner
1963 is the bfd of the DWO file.
dce234bc 1964 If the section is compressed, uncompress it before returning. */
c906108c 1965
dce234bc
PP
1966static void
1967dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1968{
dce234bc 1969 asection *sectp = info->asection;
3019eac3 1970 bfd *abfd;
dce234bc
PP
1971 gdb_byte *buf, *retbuf;
1972 unsigned char header[4];
c906108c 1973
be391dca
TT
1974 if (info->readin)
1975 return;
dce234bc 1976 info->buffer = NULL;
be391dca 1977 info->readin = 1;
188dd5d6 1978
9e0ac564 1979 if (dwarf2_section_empty_p (info))
dce234bc 1980 return;
c906108c 1981
3019eac3
DE
1982 abfd = sectp->owner;
1983
4bf44c1c
TT
1984 /* If the section has relocations, we must read it ourselves.
1985 Otherwise we attach it to the BFD. */
1986 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1987 {
d521ce57 1988 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1989 return;
dce234bc 1990 }
dce234bc 1991
4bf44c1c
TT
1992 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1993 info->buffer = buf;
dce234bc
PP
1994
1995 /* When debugging .o files, we may need to apply relocations; see
1996 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1997 We never compress sections in .o files, so we only need to
1998 try this when the section is not compressed. */
ac8035ab 1999 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2000 if (retbuf != NULL)
2001 {
2002 info->buffer = retbuf;
2003 return;
2004 }
2005
2006 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2007 || bfd_bread (buf, info->size, abfd) != info->size)
2008 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2009 bfd_get_filename (abfd));
2010}
2011
9e0ac564
TT
2012/* A helper function that returns the size of a section in a safe way.
2013 If you are positive that the section has been read before using the
2014 size, then it is safe to refer to the dwarf2_section_info object's
2015 "size" field directly. In other cases, you must call this
2016 function, because for compressed sections the size field is not set
2017 correctly until the section has been read. */
2018
2019static bfd_size_type
2020dwarf2_section_size (struct objfile *objfile,
2021 struct dwarf2_section_info *info)
2022{
2023 if (!info->readin)
2024 dwarf2_read_section (objfile, info);
2025 return info->size;
2026}
2027
dce234bc 2028/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2029 SECTION_NAME. */
af34e669 2030
dce234bc 2031void
3017a003
TG
2032dwarf2_get_section_info (struct objfile *objfile,
2033 enum dwarf2_section_enum sect,
d521ce57 2034 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2035 bfd_size_type *sizep)
2036{
2037 struct dwarf2_per_objfile *data
2038 = objfile_data (objfile, dwarf2_objfile_data_key);
2039 struct dwarf2_section_info *info;
a3b2a86b
TT
2040
2041 /* We may see an objfile without any DWARF, in which case we just
2042 return nothing. */
2043 if (data == NULL)
2044 {
2045 *sectp = NULL;
2046 *bufp = NULL;
2047 *sizep = 0;
2048 return;
2049 }
3017a003
TG
2050 switch (sect)
2051 {
2052 case DWARF2_DEBUG_FRAME:
2053 info = &data->frame;
2054 break;
2055 case DWARF2_EH_FRAME:
2056 info = &data->eh_frame;
2057 break;
2058 default:
2059 gdb_assert_not_reached ("unexpected section");
2060 }
dce234bc 2061
9e0ac564 2062 dwarf2_read_section (objfile, info);
dce234bc
PP
2063
2064 *sectp = info->asection;
2065 *bufp = info->buffer;
2066 *sizep = info->size;
2067}
2068
36586728
TT
2069/* A helper function to find the sections for a .dwz file. */
2070
2071static void
2072locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2073{
2074 struct dwz_file *dwz_file = arg;
2075
2076 /* Note that we only support the standard ELF names, because .dwz
2077 is ELF-only (at the time of writing). */
2078 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2079 {
2080 dwz_file->abbrev.asection = sectp;
2081 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2082 }
2083 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2084 {
2085 dwz_file->info.asection = sectp;
2086 dwz_file->info.size = bfd_get_section_size (sectp);
2087 }
2088 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2089 {
2090 dwz_file->str.asection = sectp;
2091 dwz_file->str.size = bfd_get_section_size (sectp);
2092 }
2093 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2094 {
2095 dwz_file->line.asection = sectp;
2096 dwz_file->line.size = bfd_get_section_size (sectp);
2097 }
2098 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2099 {
2100 dwz_file->macro.asection = sectp;
2101 dwz_file->macro.size = bfd_get_section_size (sectp);
2102 }
2ec9a5e0
TT
2103 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2104 {
2105 dwz_file->gdb_index.asection = sectp;
2106 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2107 }
36586728
TT
2108}
2109
4db1a1dc
TT
2110/* Open the separate '.dwz' debug file, if needed. Return NULL if
2111 there is no .gnu_debugaltlink section in the file. Error if there
2112 is such a section but the file cannot be found. */
36586728
TT
2113
2114static struct dwz_file *
2115dwarf2_get_dwz_file (void)
2116{
4db1a1dc
TT
2117 bfd *dwz_bfd;
2118 char *data;
36586728
TT
2119 struct cleanup *cleanup;
2120 const char *filename;
2121 struct dwz_file *result;
4db1a1dc 2122 unsigned long buildid;
36586728
TT
2123
2124 if (dwarf2_per_objfile->dwz_file != NULL)
2125 return dwarf2_per_objfile->dwz_file;
2126
4db1a1dc
TT
2127 bfd_set_error (bfd_error_no_error);
2128 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2129 &buildid);
2130 if (data == NULL)
2131 {
2132 if (bfd_get_error () == bfd_error_no_error)
2133 return NULL;
2134 error (_("could not read '.gnu_debugaltlink' section: %s"),
2135 bfd_errmsg (bfd_get_error ()));
2136 }
36586728
TT
2137 cleanup = make_cleanup (xfree, data);
2138
f9d83a0b 2139 filename = (const char *) data;
36586728
TT
2140 if (!IS_ABSOLUTE_PATH (filename))
2141 {
4262abfb 2142 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2143 char *rel;
2144
2145 make_cleanup (xfree, abs);
2146 abs = ldirname (abs);
2147 make_cleanup (xfree, abs);
2148
2149 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2150 make_cleanup (xfree, rel);
2151 filename = rel;
2152 }
2153
2154 /* The format is just a NUL-terminated file name, followed by the
2155 build-id. For now, though, we ignore the build-id. */
2156 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2157 if (dwz_bfd == NULL)
2158 error (_("could not read '%s': %s"), filename,
2159 bfd_errmsg (bfd_get_error ()));
2160
2161 if (!bfd_check_format (dwz_bfd, bfd_object))
2162 {
2163 gdb_bfd_unref (dwz_bfd);
2164 error (_("file '%s' was not usable: %s"), filename,
2165 bfd_errmsg (bfd_get_error ()));
2166 }
2167
2168 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2169 struct dwz_file);
2170 result->dwz_bfd = dwz_bfd;
2171
2172 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2173
2174 do_cleanups (cleanup);
2175
8d2cc612 2176 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2177 return result;
2178}
9291a0cd 2179\f
7b9f3c50
DE
2180/* DWARF quick_symbols_functions support. */
2181
2182/* TUs can share .debug_line entries, and there can be a lot more TUs than
2183 unique line tables, so we maintain a separate table of all .debug_line
2184 derived entries to support the sharing.
2185 All the quick functions need is the list of file names. We discard the
2186 line_header when we're done and don't need to record it here. */
2187struct quick_file_names
2188{
094b34ac
DE
2189 /* The data used to construct the hash key. */
2190 struct stmt_list_hash hash;
7b9f3c50
DE
2191
2192 /* The number of entries in file_names, real_names. */
2193 unsigned int num_file_names;
2194
2195 /* The file names from the line table, after being run through
2196 file_full_name. */
2197 const char **file_names;
2198
2199 /* The file names from the line table after being run through
2200 gdb_realpath. These are computed lazily. */
2201 const char **real_names;
2202};
2203
2204/* When using the index (and thus not using psymtabs), each CU has an
2205 object of this type. This is used to hold information needed by
2206 the various "quick" methods. */
2207struct dwarf2_per_cu_quick_data
2208{
2209 /* The file table. This can be NULL if there was no file table
2210 or it's currently not read in.
2211 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2212 struct quick_file_names *file_names;
2213
2214 /* The corresponding symbol table. This is NULL if symbols for this
2215 CU have not yet been read. */
2216 struct symtab *symtab;
2217
2218 /* A temporary mark bit used when iterating over all CUs in
2219 expand_symtabs_matching. */
2220 unsigned int mark : 1;
2221
2222 /* True if we've tried to read the file table and found there isn't one.
2223 There will be no point in trying to read it again next time. */
2224 unsigned int no_file_data : 1;
2225};
2226
094b34ac
DE
2227/* Utility hash function for a stmt_list_hash. */
2228
2229static hashval_t
2230hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2231{
2232 hashval_t v = 0;
2233
2234 if (stmt_list_hash->dwo_unit != NULL)
2235 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2236 v += stmt_list_hash->line_offset.sect_off;
2237 return v;
2238}
2239
2240/* Utility equality function for a stmt_list_hash. */
2241
2242static int
2243eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2244 const struct stmt_list_hash *rhs)
2245{
2246 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2247 return 0;
2248 if (lhs->dwo_unit != NULL
2249 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2250 return 0;
2251
2252 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2253}
2254
7b9f3c50
DE
2255/* Hash function for a quick_file_names. */
2256
2257static hashval_t
2258hash_file_name_entry (const void *e)
2259{
2260 const struct quick_file_names *file_data = e;
2261
094b34ac 2262 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2263}
2264
2265/* Equality function for a quick_file_names. */
2266
2267static int
2268eq_file_name_entry (const void *a, const void *b)
2269{
2270 const struct quick_file_names *ea = a;
2271 const struct quick_file_names *eb = b;
2272
094b34ac 2273 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2274}
2275
2276/* Delete function for a quick_file_names. */
2277
2278static void
2279delete_file_name_entry (void *e)
2280{
2281 struct quick_file_names *file_data = e;
2282 int i;
2283
2284 for (i = 0; i < file_data->num_file_names; ++i)
2285 {
2286 xfree ((void*) file_data->file_names[i]);
2287 if (file_data->real_names)
2288 xfree ((void*) file_data->real_names[i]);
2289 }
2290
2291 /* The space for the struct itself lives on objfile_obstack,
2292 so we don't free it here. */
2293}
2294
2295/* Create a quick_file_names hash table. */
2296
2297static htab_t
2298create_quick_file_names_table (unsigned int nr_initial_entries)
2299{
2300 return htab_create_alloc (nr_initial_entries,
2301 hash_file_name_entry, eq_file_name_entry,
2302 delete_file_name_entry, xcalloc, xfree);
2303}
9291a0cd 2304
918dd910
JK
2305/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2306 have to be created afterwards. You should call age_cached_comp_units after
2307 processing PER_CU->CU. dw2_setup must have been already called. */
2308
2309static void
2310load_cu (struct dwarf2_per_cu_data *per_cu)
2311{
3019eac3 2312 if (per_cu->is_debug_types)
e5fe5e75 2313 load_full_type_unit (per_cu);
918dd910 2314 else
95554aad 2315 load_full_comp_unit (per_cu, language_minimal);
918dd910 2316
918dd910 2317 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2318
2319 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2320}
2321
a0f42c21 2322/* Read in the symbols for PER_CU. */
2fdf6df6 2323
9291a0cd 2324static void
a0f42c21 2325dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2326{
2327 struct cleanup *back_to;
2328
f4dc4d17
DE
2329 /* Skip type_unit_groups, reading the type units they contain
2330 is handled elsewhere. */
2331 if (IS_TYPE_UNIT_GROUP (per_cu))
2332 return;
2333
9291a0cd
TT
2334 back_to = make_cleanup (dwarf2_release_queue, NULL);
2335
95554aad
TT
2336 if (dwarf2_per_objfile->using_index
2337 ? per_cu->v.quick->symtab == NULL
2338 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2339 {
2340 queue_comp_unit (per_cu, language_minimal);
2341 load_cu (per_cu);
89e63ee4
DE
2342
2343 /* If we just loaded a CU from a DWO, and we're working with an index
2344 that may badly handle TUs, load all the TUs in that DWO as well.
2345 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2346 if (!per_cu->is_debug_types
2347 && per_cu->cu->dwo_unit != NULL
2348 && dwarf2_per_objfile->index_table != NULL
2349 && dwarf2_per_objfile->index_table->version <= 7
2350 /* DWP files aren't supported yet. */
2351 && get_dwp_file () == NULL)
2352 queue_and_load_all_dwo_tus (per_cu);
95554aad 2353 }
9291a0cd 2354
a0f42c21 2355 process_queue ();
9291a0cd
TT
2356
2357 /* Age the cache, releasing compilation units that have not
2358 been used recently. */
2359 age_cached_comp_units ();
2360
2361 do_cleanups (back_to);
2362}
2363
2364/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2365 the objfile from which this CU came. Returns the resulting symbol
2366 table. */
2fdf6df6 2367
9291a0cd 2368static struct symtab *
a0f42c21 2369dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2370{
95554aad 2371 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2372 if (!per_cu->v.quick->symtab)
2373 {
2374 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2375 increment_reading_symtab ();
a0f42c21 2376 dw2_do_instantiate_symtab (per_cu);
95554aad 2377 process_cu_includes ();
9291a0cd
TT
2378 do_cleanups (back_to);
2379 }
2380 return per_cu->v.quick->symtab;
2381}
2382
f4dc4d17
DE
2383/* Return the CU given its index.
2384
2385 This is intended for loops like:
2386
2387 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2388 + dwarf2_per_objfile->n_type_units); ++i)
2389 {
2390 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2391
2392 ...;
2393 }
2394*/
2fdf6df6 2395
1fd400ff
TT
2396static struct dwarf2_per_cu_data *
2397dw2_get_cu (int index)
2398{
2399 if (index >= dwarf2_per_objfile->n_comp_units)
2400 {
f4dc4d17 2401 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2402 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2403 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2404 }
2405
2406 return dwarf2_per_objfile->all_comp_units[index];
2407}
2408
2409/* Return the primary CU given its index.
2410 The difference between this function and dw2_get_cu is in the handling
2411 of type units (TUs). Here we return the type_unit_group object.
2412
2413 This is intended for loops like:
2414
2415 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2416 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2417 {
2418 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2419
2420 ...;
2421 }
2422*/
2423
2424static struct dwarf2_per_cu_data *
2425dw2_get_primary_cu (int index)
2426{
2427 if (index >= dwarf2_per_objfile->n_comp_units)
2428 {
1fd400ff 2429 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2430 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2431 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2432 }
f4dc4d17 2433
1fd400ff
TT
2434 return dwarf2_per_objfile->all_comp_units[index];
2435}
2436
2ec9a5e0
TT
2437/* A helper for create_cus_from_index that handles a given list of
2438 CUs. */
2fdf6df6 2439
74a0d9f6 2440static void
2ec9a5e0
TT
2441create_cus_from_index_list (struct objfile *objfile,
2442 const gdb_byte *cu_list, offset_type n_elements,
2443 struct dwarf2_section_info *section,
2444 int is_dwz,
2445 int base_offset)
9291a0cd
TT
2446{
2447 offset_type i;
9291a0cd 2448
2ec9a5e0 2449 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2450 {
2451 struct dwarf2_per_cu_data *the_cu;
2452 ULONGEST offset, length;
2453
74a0d9f6
JK
2454 gdb_static_assert (sizeof (ULONGEST) >= 8);
2455 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2456 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2457 cu_list += 2 * 8;
2458
2459 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2460 struct dwarf2_per_cu_data);
b64f50a1 2461 the_cu->offset.sect_off = offset;
9291a0cd
TT
2462 the_cu->length = length;
2463 the_cu->objfile = objfile;
8a0459fd 2464 the_cu->section = section;
9291a0cd
TT
2465 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2466 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2467 the_cu->is_dwz = is_dwz;
2468 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2469 }
9291a0cd
TT
2470}
2471
2ec9a5e0 2472/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2473 the CU objects for this objfile. */
2ec9a5e0 2474
74a0d9f6 2475static void
2ec9a5e0
TT
2476create_cus_from_index (struct objfile *objfile,
2477 const gdb_byte *cu_list, offset_type cu_list_elements,
2478 const gdb_byte *dwz_list, offset_type dwz_elements)
2479{
2480 struct dwz_file *dwz;
2481
2482 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2483 dwarf2_per_objfile->all_comp_units
2484 = obstack_alloc (&objfile->objfile_obstack,
2485 dwarf2_per_objfile->n_comp_units
2486 * sizeof (struct dwarf2_per_cu_data *));
2487
74a0d9f6
JK
2488 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2489 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2490
2491 if (dwz_elements == 0)
74a0d9f6 2492 return;
2ec9a5e0
TT
2493
2494 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2495 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2496 cu_list_elements / 2);
2ec9a5e0
TT
2497}
2498
1fd400ff 2499/* Create the signatured type hash table from the index. */
673bfd45 2500
74a0d9f6 2501static void
673bfd45 2502create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2503 struct dwarf2_section_info *section,
673bfd45
DE
2504 const gdb_byte *bytes,
2505 offset_type elements)
1fd400ff
TT
2506{
2507 offset_type i;
673bfd45 2508 htab_t sig_types_hash;
1fd400ff 2509
d467dd73
DE
2510 dwarf2_per_objfile->n_type_units = elements / 3;
2511 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2512 = xmalloc (dwarf2_per_objfile->n_type_units
2513 * sizeof (struct signatured_type *));
1fd400ff 2514
673bfd45 2515 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2516
2517 for (i = 0; i < elements; i += 3)
2518 {
52dc124a
DE
2519 struct signatured_type *sig_type;
2520 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2521 void **slot;
2522
74a0d9f6
JK
2523 gdb_static_assert (sizeof (ULONGEST) >= 8);
2524 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2525 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2526 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2527 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2528 bytes += 3 * 8;
2529
52dc124a 2530 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2531 struct signatured_type);
52dc124a 2532 sig_type->signature = signature;
3019eac3
DE
2533 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2534 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2535 sig_type->per_cu.section = section;
52dc124a
DE
2536 sig_type->per_cu.offset.sect_off = offset;
2537 sig_type->per_cu.objfile = objfile;
2538 sig_type->per_cu.v.quick
1fd400ff
TT
2539 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2540 struct dwarf2_per_cu_quick_data);
2541
52dc124a
DE
2542 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2543 *slot = sig_type;
1fd400ff 2544
b4dd5633 2545 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2546 }
2547
673bfd45 2548 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2549}
2550
9291a0cd
TT
2551/* Read the address map data from the mapped index, and use it to
2552 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2553
9291a0cd
TT
2554static void
2555create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2556{
2557 const gdb_byte *iter, *end;
2558 struct obstack temp_obstack;
2559 struct addrmap *mutable_map;
2560 struct cleanup *cleanup;
2561 CORE_ADDR baseaddr;
2562
2563 obstack_init (&temp_obstack);
2564 cleanup = make_cleanup_obstack_free (&temp_obstack);
2565 mutable_map = addrmap_create_mutable (&temp_obstack);
2566
2567 iter = index->address_table;
2568 end = iter + index->address_table_size;
2569
2570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2571
2572 while (iter < end)
2573 {
2574 ULONGEST hi, lo, cu_index;
2575 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2576 iter += 8;
2577 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2578 iter += 8;
2579 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2580 iter += 4;
f652bce2 2581
24a55014 2582 if (lo > hi)
f652bce2 2583 {
24a55014
DE
2584 complaint (&symfile_complaints,
2585 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2586 hex_string (lo), hex_string (hi));
24a55014 2587 continue;
f652bce2 2588 }
24a55014
DE
2589
2590 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2591 {
2592 complaint (&symfile_complaints,
2593 _(".gdb_index address table has invalid CU number %u"),
2594 (unsigned) cu_index);
24a55014 2595 continue;
f652bce2 2596 }
24a55014
DE
2597
2598 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2599 dw2_get_cu (cu_index));
9291a0cd
TT
2600 }
2601
2602 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2603 &objfile->objfile_obstack);
2604 do_cleanups (cleanup);
2605}
2606
59d7bcaf
JK
2607/* The hash function for strings in the mapped index. This is the same as
2608 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2609 implementation. This is necessary because the hash function is tied to the
2610 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2611 SYMBOL_HASH_NEXT.
2612
2613 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2614
9291a0cd 2615static hashval_t
559a7a62 2616mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2617{
2618 const unsigned char *str = (const unsigned char *) p;
2619 hashval_t r = 0;
2620 unsigned char c;
2621
2622 while ((c = *str++) != 0)
559a7a62
JK
2623 {
2624 if (index_version >= 5)
2625 c = tolower (c);
2626 r = r * 67 + c - 113;
2627 }
9291a0cd
TT
2628
2629 return r;
2630}
2631
2632/* Find a slot in the mapped index INDEX for the object named NAME.
2633 If NAME is found, set *VEC_OUT to point to the CU vector in the
2634 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2635
9291a0cd
TT
2636static int
2637find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2638 offset_type **vec_out)
2639{
0cf03b49
JK
2640 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2641 offset_type hash;
9291a0cd 2642 offset_type slot, step;
559a7a62 2643 int (*cmp) (const char *, const char *);
9291a0cd 2644
0cf03b49
JK
2645 if (current_language->la_language == language_cplus
2646 || current_language->la_language == language_java
2647 || current_language->la_language == language_fortran)
2648 {
2649 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2650 not contain any. */
2651 const char *paren = strchr (name, '(');
2652
2653 if (paren)
2654 {
2655 char *dup;
2656
2657 dup = xmalloc (paren - name + 1);
2658 memcpy (dup, name, paren - name);
2659 dup[paren - name] = 0;
2660
2661 make_cleanup (xfree, dup);
2662 name = dup;
2663 }
2664 }
2665
559a7a62 2666 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2667 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2668 simulate our NAME being searched is also lowercased. */
2669 hash = mapped_index_string_hash ((index->version == 4
2670 && case_sensitivity == case_sensitive_off
2671 ? 5 : index->version),
2672 name);
2673
3876f04e
DE
2674 slot = hash & (index->symbol_table_slots - 1);
2675 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2676 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2677
2678 for (;;)
2679 {
2680 /* Convert a slot number to an offset into the table. */
2681 offset_type i = 2 * slot;
2682 const char *str;
3876f04e 2683 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2684 {
2685 do_cleanups (back_to);
2686 return 0;
2687 }
9291a0cd 2688
3876f04e 2689 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2690 if (!cmp (name, str))
9291a0cd
TT
2691 {
2692 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2693 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2694 do_cleanups (back_to);
9291a0cd
TT
2695 return 1;
2696 }
2697
3876f04e 2698 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2699 }
2700}
2701
2ec9a5e0
TT
2702/* A helper function that reads the .gdb_index from SECTION and fills
2703 in MAP. FILENAME is the name of the file containing the section;
2704 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2705 ok to use deprecated sections.
2706
2707 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2708 out parameters that are filled in with information about the CU and
2709 TU lists in the section.
2710
2711 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2712
9291a0cd 2713static int
2ec9a5e0
TT
2714read_index_from_section (struct objfile *objfile,
2715 const char *filename,
2716 int deprecated_ok,
2717 struct dwarf2_section_info *section,
2718 struct mapped_index *map,
2719 const gdb_byte **cu_list,
2720 offset_type *cu_list_elements,
2721 const gdb_byte **types_list,
2722 offset_type *types_list_elements)
9291a0cd 2723{
948f8e3d 2724 const gdb_byte *addr;
2ec9a5e0 2725 offset_type version;
b3b272e1 2726 offset_type *metadata;
1fd400ff 2727 int i;
9291a0cd 2728
2ec9a5e0 2729 if (dwarf2_section_empty_p (section))
9291a0cd 2730 return 0;
82430852
JK
2731
2732 /* Older elfutils strip versions could keep the section in the main
2733 executable while splitting it for the separate debug info file. */
2ec9a5e0 2734 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2735 return 0;
2736
2ec9a5e0 2737 dwarf2_read_section (objfile, section);
9291a0cd 2738
2ec9a5e0 2739 addr = section->buffer;
9291a0cd 2740 /* Version check. */
1fd400ff 2741 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2742 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2743 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2744 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2745 indices. */
831adc1f 2746 if (version < 4)
481860b3
GB
2747 {
2748 static int warning_printed = 0;
2749 if (!warning_printed)
2750 {
2751 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2752 filename);
481860b3
GB
2753 warning_printed = 1;
2754 }
2755 return 0;
2756 }
2757 /* Index version 4 uses a different hash function than index version
2758 5 and later.
2759
2760 Versions earlier than 6 did not emit psymbols for inlined
2761 functions. Using these files will cause GDB not to be able to
2762 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2763 indices unless the user has done
2764 "set use-deprecated-index-sections on". */
2ec9a5e0 2765 if (version < 6 && !deprecated_ok)
481860b3
GB
2766 {
2767 static int warning_printed = 0;
2768 if (!warning_printed)
2769 {
e615022a
DE
2770 warning (_("\
2771Skipping deprecated .gdb_index section in %s.\n\
2772Do \"set use-deprecated-index-sections on\" before the file is read\n\
2773to use the section anyway."),
2ec9a5e0 2774 filename);
481860b3
GB
2775 warning_printed = 1;
2776 }
2777 return 0;
2778 }
796a7ff8
DE
2779 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2780 of the TU (for symbols coming from TUs). It's just a performance bug, and
2781 we can't distinguish gdb-generated indices from gold-generated ones, so
2782 nothing to do here. */
2783
481860b3 2784 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2785 longer backward compatible. */
796a7ff8 2786 if (version > 8)
594e8718 2787 return 0;
9291a0cd 2788
559a7a62 2789 map->version = version;
2ec9a5e0 2790 map->total_size = section->size;
9291a0cd
TT
2791
2792 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2793
2794 i = 0;
2ec9a5e0
TT
2795 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2796 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2797 / 8);
1fd400ff
TT
2798 ++i;
2799
2ec9a5e0
TT
2800 *types_list = addr + MAYBE_SWAP (metadata[i]);
2801 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2802 - MAYBE_SWAP (metadata[i]))
2803 / 8);
987d643c 2804 ++i;
1fd400ff
TT
2805
2806 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2807 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2808 - MAYBE_SWAP (metadata[i]));
2809 ++i;
2810
3876f04e
DE
2811 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2812 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2813 - MAYBE_SWAP (metadata[i]))
2814 / (2 * sizeof (offset_type)));
1fd400ff 2815 ++i;
9291a0cd 2816
f9d83a0b 2817 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2818
2ec9a5e0
TT
2819 return 1;
2820}
2821
2822
2823/* Read the index file. If everything went ok, initialize the "quick"
2824 elements of all the CUs and return 1. Otherwise, return 0. */
2825
2826static int
2827dwarf2_read_index (struct objfile *objfile)
2828{
2829 struct mapped_index local_map, *map;
2830 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2831 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2832 struct dwz_file *dwz;
2ec9a5e0 2833
4262abfb 2834 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
2835 use_deprecated_index_sections,
2836 &dwarf2_per_objfile->gdb_index, &local_map,
2837 &cu_list, &cu_list_elements,
2838 &types_list, &types_list_elements))
2839 return 0;
2840
0fefef59 2841 /* Don't use the index if it's empty. */
2ec9a5e0 2842 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2843 return 0;
2844
2ec9a5e0
TT
2845 /* If there is a .dwz file, read it so we can get its CU list as
2846 well. */
4db1a1dc
TT
2847 dwz = dwarf2_get_dwz_file ();
2848 if (dwz != NULL)
2ec9a5e0 2849 {
2ec9a5e0
TT
2850 struct mapped_index dwz_map;
2851 const gdb_byte *dwz_types_ignore;
2852 offset_type dwz_types_elements_ignore;
2853
2854 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2855 1,
2856 &dwz->gdb_index, &dwz_map,
2857 &dwz_list, &dwz_list_elements,
2858 &dwz_types_ignore,
2859 &dwz_types_elements_ignore))
2860 {
2861 warning (_("could not read '.gdb_index' section from %s; skipping"),
2862 bfd_get_filename (dwz->dwz_bfd));
2863 return 0;
2864 }
2865 }
2866
74a0d9f6
JK
2867 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2868 dwz_list_elements);
1fd400ff 2869
8b70b953
TT
2870 if (types_list_elements)
2871 {
2872 struct dwarf2_section_info *section;
2873
2874 /* We can only handle a single .debug_types when we have an
2875 index. */
2876 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2877 return 0;
2878
2879 section = VEC_index (dwarf2_section_info_def,
2880 dwarf2_per_objfile->types, 0);
2881
74a0d9f6
JK
2882 create_signatured_type_table_from_index (objfile, section, types_list,
2883 types_list_elements);
8b70b953 2884 }
9291a0cd 2885
2ec9a5e0
TT
2886 create_addrmap_from_index (objfile, &local_map);
2887
2888 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2889 *map = local_map;
9291a0cd
TT
2890
2891 dwarf2_per_objfile->index_table = map;
2892 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2893 dwarf2_per_objfile->quick_file_names_table =
2894 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2895
2896 return 1;
2897}
2898
2899/* A helper for the "quick" functions which sets the global
2900 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2901
9291a0cd
TT
2902static void
2903dw2_setup (struct objfile *objfile)
2904{
2905 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2906 gdb_assert (dwarf2_per_objfile);
2907}
2908
dee91e82 2909/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2910
dee91e82
DE
2911static void
2912dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2913 const gdb_byte *info_ptr,
dee91e82
DE
2914 struct die_info *comp_unit_die,
2915 int has_children,
2916 void *data)
9291a0cd 2917{
dee91e82
DE
2918 struct dwarf2_cu *cu = reader->cu;
2919 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2920 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2921 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2922 struct line_header *lh;
9291a0cd 2923 struct attribute *attr;
dee91e82 2924 int i;
15d034d0 2925 const char *name, *comp_dir;
7b9f3c50
DE
2926 void **slot;
2927 struct quick_file_names *qfn;
2928 unsigned int line_offset;
9291a0cd 2929
0186c6a7
DE
2930 gdb_assert (! this_cu->is_debug_types);
2931
07261596
TT
2932 /* Our callers never want to match partial units -- instead they
2933 will match the enclosing full CU. */
2934 if (comp_unit_die->tag == DW_TAG_partial_unit)
2935 {
2936 this_cu->v.quick->no_file_data = 1;
2937 return;
2938 }
2939
0186c6a7 2940 lh_cu = this_cu;
7b9f3c50
DE
2941 lh = NULL;
2942 slot = NULL;
2943 line_offset = 0;
dee91e82
DE
2944
2945 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2946 if (attr)
2947 {
7b9f3c50
DE
2948 struct quick_file_names find_entry;
2949
2950 line_offset = DW_UNSND (attr);
2951
2952 /* We may have already read in this line header (TU line header sharing).
2953 If we have we're done. */
094b34ac
DE
2954 find_entry.hash.dwo_unit = cu->dwo_unit;
2955 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2956 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2957 &find_entry, INSERT);
2958 if (*slot != NULL)
2959 {
094b34ac 2960 lh_cu->v.quick->file_names = *slot;
dee91e82 2961 return;
7b9f3c50
DE
2962 }
2963
3019eac3 2964 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2965 }
2966 if (lh == NULL)
2967 {
094b34ac 2968 lh_cu->v.quick->no_file_data = 1;
dee91e82 2969 return;
9291a0cd
TT
2970 }
2971
7b9f3c50 2972 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2973 qfn->hash.dwo_unit = cu->dwo_unit;
2974 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2975 gdb_assert (slot != NULL);
2976 *slot = qfn;
9291a0cd 2977
dee91e82 2978 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2979
7b9f3c50
DE
2980 qfn->num_file_names = lh->num_file_names;
2981 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2982 lh->num_file_names * sizeof (char *));
9291a0cd 2983 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2984 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2985 qfn->real_names = NULL;
9291a0cd 2986
7b9f3c50 2987 free_line_header (lh);
7b9f3c50 2988
094b34ac 2989 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2990}
2991
2992/* A helper for the "quick" functions which attempts to read the line
2993 table for THIS_CU. */
2994
2995static struct quick_file_names *
e4a48d9d 2996dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2997{
0186c6a7
DE
2998 /* This should never be called for TUs. */
2999 gdb_assert (! this_cu->is_debug_types);
3000 /* Nor type unit groups. */
3001 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3002
dee91e82
DE
3003 if (this_cu->v.quick->file_names != NULL)
3004 return this_cu->v.quick->file_names;
3005 /* If we know there is no line data, no point in looking again. */
3006 if (this_cu->v.quick->no_file_data)
3007 return NULL;
3008
0186c6a7 3009 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3010
3011 if (this_cu->v.quick->no_file_data)
3012 return NULL;
3013 return this_cu->v.quick->file_names;
9291a0cd
TT
3014}
3015
3016/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3017 real path for a given file name from the line table. */
2fdf6df6 3018
9291a0cd 3019static const char *
7b9f3c50
DE
3020dw2_get_real_path (struct objfile *objfile,
3021 struct quick_file_names *qfn, int index)
9291a0cd 3022{
7b9f3c50
DE
3023 if (qfn->real_names == NULL)
3024 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3025 qfn->num_file_names, sizeof (char *));
9291a0cd 3026
7b9f3c50
DE
3027 if (qfn->real_names[index] == NULL)
3028 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3029
7b9f3c50 3030 return qfn->real_names[index];
9291a0cd
TT
3031}
3032
3033static struct symtab *
3034dw2_find_last_source_symtab (struct objfile *objfile)
3035{
3036 int index;
ae2de4f8 3037
9291a0cd
TT
3038 dw2_setup (objfile);
3039 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3040 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3041}
3042
7b9f3c50
DE
3043/* Traversal function for dw2_forget_cached_source_info. */
3044
3045static int
3046dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3047{
7b9f3c50 3048 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3049
7b9f3c50 3050 if (file_data->real_names)
9291a0cd 3051 {
7b9f3c50 3052 int i;
9291a0cd 3053
7b9f3c50 3054 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3055 {
7b9f3c50
DE
3056 xfree ((void*) file_data->real_names[i]);
3057 file_data->real_names[i] = NULL;
9291a0cd
TT
3058 }
3059 }
7b9f3c50
DE
3060
3061 return 1;
3062}
3063
3064static void
3065dw2_forget_cached_source_info (struct objfile *objfile)
3066{
3067 dw2_setup (objfile);
3068
3069 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3070 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3071}
3072
f8eba3c6
TT
3073/* Helper function for dw2_map_symtabs_matching_filename that expands
3074 the symtabs and calls the iterator. */
3075
3076static int
3077dw2_map_expand_apply (struct objfile *objfile,
3078 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3079 const char *name, const char *real_path,
f8eba3c6
TT
3080 int (*callback) (struct symtab *, void *),
3081 void *data)
3082{
3083 struct symtab *last_made = objfile->symtabs;
3084
3085 /* Don't visit already-expanded CUs. */
3086 if (per_cu->v.quick->symtab)
3087 return 0;
3088
3089 /* This may expand more than one symtab, and we want to iterate over
3090 all of them. */
a0f42c21 3091 dw2_instantiate_symtab (per_cu);
f8eba3c6 3092
f5b95b50 3093 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3094 objfile->symtabs, last_made);
3095}
3096
3097/* Implementation of the map_symtabs_matching_filename method. */
3098
9291a0cd 3099static int
f8eba3c6 3100dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3101 const char *real_path,
f8eba3c6
TT
3102 int (*callback) (struct symtab *, void *),
3103 void *data)
9291a0cd
TT
3104{
3105 int i;
c011a4f4 3106 const char *name_basename = lbasename (name);
9291a0cd
TT
3107
3108 dw2_setup (objfile);
ae2de4f8 3109
848e3e78
DE
3110 /* The rule is CUs specify all the files, including those used by
3111 any TU, so there's no need to scan TUs here. */
f4dc4d17 3112
848e3e78 3113 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3114 {
3115 int j;
f4dc4d17 3116 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3117 struct quick_file_names *file_data;
9291a0cd 3118
3d7bb9d9 3119 /* We only need to look at symtabs not already expanded. */
e254ef6a 3120 if (per_cu->v.quick->symtab)
9291a0cd
TT
3121 continue;
3122
e4a48d9d 3123 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3124 if (file_data == NULL)
9291a0cd
TT
3125 continue;
3126
7b9f3c50 3127 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3128 {
7b9f3c50 3129 const char *this_name = file_data->file_names[j];
da235a7c 3130 const char *this_real_name;
9291a0cd 3131
af529f8f 3132 if (compare_filenames_for_search (this_name, name))
9291a0cd 3133 {
f5b95b50 3134 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3135 callback, data))
3136 return 1;
288e77a7 3137 continue;
4aac40c8 3138 }
9291a0cd 3139
c011a4f4
DE
3140 /* Before we invoke realpath, which can get expensive when many
3141 files are involved, do a quick comparison of the basenames. */
3142 if (! basenames_may_differ
3143 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3144 continue;
3145
da235a7c
JK
3146 this_real_name = dw2_get_real_path (objfile, file_data, j);
3147 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3148 {
da235a7c
JK
3149 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3150 callback, data))
3151 return 1;
288e77a7 3152 continue;
da235a7c 3153 }
9291a0cd 3154
da235a7c
JK
3155 if (real_path != NULL)
3156 {
af529f8f
JK
3157 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3158 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3159 if (this_real_name != NULL
af529f8f 3160 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3161 {
f5b95b50 3162 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3163 callback, data))
3164 return 1;
288e77a7 3165 continue;
9291a0cd
TT
3166 }
3167 }
3168 }
3169 }
3170
9291a0cd
TT
3171 return 0;
3172}
3173
da51c347
DE
3174/* Struct used to manage iterating over all CUs looking for a symbol. */
3175
3176struct dw2_symtab_iterator
9291a0cd 3177{
da51c347
DE
3178 /* The internalized form of .gdb_index. */
3179 struct mapped_index *index;
3180 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3181 int want_specific_block;
3182 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3183 Unused if !WANT_SPECIFIC_BLOCK. */
3184 int block_index;
3185 /* The kind of symbol we're looking for. */
3186 domain_enum domain;
3187 /* The list of CUs from the index entry of the symbol,
3188 or NULL if not found. */
3189 offset_type *vec;
3190 /* The next element in VEC to look at. */
3191 int next;
3192 /* The number of elements in VEC, or zero if there is no match. */
3193 int length;
3194};
9291a0cd 3195
da51c347
DE
3196/* Initialize the index symtab iterator ITER.
3197 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3198 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3199
9291a0cd 3200static void
da51c347
DE
3201dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3202 struct mapped_index *index,
3203 int want_specific_block,
3204 int block_index,
3205 domain_enum domain,
3206 const char *name)
3207{
3208 iter->index = index;
3209 iter->want_specific_block = want_specific_block;
3210 iter->block_index = block_index;
3211 iter->domain = domain;
3212 iter->next = 0;
3213
3214 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3215 iter->length = MAYBE_SWAP (*iter->vec);
3216 else
3217 {
3218 iter->vec = NULL;
3219 iter->length = 0;
3220 }
3221}
3222
3223/* Return the next matching CU or NULL if there are no more. */
3224
3225static struct dwarf2_per_cu_data *
3226dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3227{
3228 for ( ; iter->next < iter->length; ++iter->next)
3229 {
3230 offset_type cu_index_and_attrs =
3231 MAYBE_SWAP (iter->vec[iter->next + 1]);
3232 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3233 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3234 int want_static = iter->block_index != GLOBAL_BLOCK;
3235 /* This value is only valid for index versions >= 7. */
3236 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3237 gdb_index_symbol_kind symbol_kind =
3238 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3239 /* Only check the symbol attributes if they're present.
3240 Indices prior to version 7 don't record them,
3241 and indices >= 7 may elide them for certain symbols
3242 (gold does this). */
3243 int attrs_valid =
3244 (iter->index->version >= 7
3245 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3246
3190f0c6
DE
3247 /* Don't crash on bad data. */
3248 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3249 + dwarf2_per_objfile->n_type_units))
3250 {
3251 complaint (&symfile_complaints,
3252 _(".gdb_index entry has bad CU index"
4262abfb
JK
3253 " [in module %s]"),
3254 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3255 continue;
3256 }
3257
3258 per_cu = dw2_get_cu (cu_index);
3259
da51c347
DE
3260 /* Skip if already read in. */
3261 if (per_cu->v.quick->symtab)
3262 continue;
3263
3264 if (attrs_valid
3265 && iter->want_specific_block
3266 && want_static != is_static)
3267 continue;
3268
3269 /* Only check the symbol's kind if it has one. */
3270 if (attrs_valid)
3271 {
3272 switch (iter->domain)
3273 {
3274 case VAR_DOMAIN:
3275 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3276 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3277 /* Some types are also in VAR_DOMAIN. */
3278 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3279 continue;
3280 break;
3281 case STRUCT_DOMAIN:
3282 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3283 continue;
3284 break;
3285 case LABEL_DOMAIN:
3286 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3287 continue;
3288 break;
3289 default:
3290 break;
3291 }
3292 }
3293
3294 ++iter->next;
3295 return per_cu;
3296 }
3297
3298 return NULL;
3299}
3300
3301static struct symtab *
3302dw2_lookup_symbol (struct objfile *objfile, int block_index,
3303 const char *name, domain_enum domain)
9291a0cd 3304{
da51c347 3305 struct symtab *stab_best = NULL;
156942c7
DE
3306 struct mapped_index *index;
3307
9291a0cd
TT
3308 dw2_setup (objfile);
3309
156942c7
DE
3310 index = dwarf2_per_objfile->index_table;
3311
da51c347 3312 /* index is NULL if OBJF_READNOW. */
156942c7 3313 if (index)
9291a0cd 3314 {
da51c347
DE
3315 struct dw2_symtab_iterator iter;
3316 struct dwarf2_per_cu_data *per_cu;
3317
3318 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3319
da51c347 3320 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3321 {
da51c347
DE
3322 struct symbol *sym = NULL;
3323 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3324
3325 /* Some caution must be observed with overloaded functions
3326 and methods, since the index will not contain any overload
3327 information (but NAME might contain it). */
3328 if (stab->primary)
9291a0cd 3329 {
da51c347
DE
3330 struct blockvector *bv = BLOCKVECTOR (stab);
3331 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3332
da51c347
DE
3333 sym = lookup_block_symbol (block, name, domain);
3334 }
1fd400ff 3335
da51c347
DE
3336 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3337 {
3338 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3339 return stab;
3340
3341 stab_best = stab;
9291a0cd 3342 }
da51c347
DE
3343
3344 /* Keep looking through other CUs. */
9291a0cd
TT
3345 }
3346 }
9291a0cd 3347
da51c347 3348 return stab_best;
9291a0cd
TT
3349}
3350
3351static void
3352dw2_print_stats (struct objfile *objfile)
3353{
e4a48d9d 3354 int i, total, count;
9291a0cd
TT
3355
3356 dw2_setup (objfile);
e4a48d9d 3357 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3358 count = 0;
e4a48d9d 3359 for (i = 0; i < total; ++i)
9291a0cd 3360 {
e254ef6a 3361 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3362
e254ef6a 3363 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3364 ++count;
3365 }
e4a48d9d 3366 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3367 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3368}
3369
779bd270
DE
3370/* This dumps minimal information about the index.
3371 It is called via "mt print objfiles".
3372 One use is to verify .gdb_index has been loaded by the
3373 gdb.dwarf2/gdb-index.exp testcase. */
3374
9291a0cd
TT
3375static void
3376dw2_dump (struct objfile *objfile)
3377{
779bd270
DE
3378 dw2_setup (objfile);
3379 gdb_assert (dwarf2_per_objfile->using_index);
3380 printf_filtered (".gdb_index:");
3381 if (dwarf2_per_objfile->index_table != NULL)
3382 {
3383 printf_filtered (" version %d\n",
3384 dwarf2_per_objfile->index_table->version);
3385 }
3386 else
3387 printf_filtered (" faked for \"readnow\"\n");
3388 printf_filtered ("\n");
9291a0cd
TT
3389}
3390
3391static void
3189cb12
DE
3392dw2_relocate (struct objfile *objfile,
3393 const struct section_offsets *new_offsets,
3394 const struct section_offsets *delta)
9291a0cd
TT
3395{
3396 /* There's nothing to relocate here. */
3397}
3398
3399static void
3400dw2_expand_symtabs_for_function (struct objfile *objfile,
3401 const char *func_name)
3402{
da51c347
DE
3403 struct mapped_index *index;
3404
3405 dw2_setup (objfile);
3406
3407 index = dwarf2_per_objfile->index_table;
3408
3409 /* index is NULL if OBJF_READNOW. */
3410 if (index)
3411 {
3412 struct dw2_symtab_iterator iter;
3413 struct dwarf2_per_cu_data *per_cu;
3414
3415 /* Note: It doesn't matter what we pass for block_index here. */
3416 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3417 func_name);
3418
3419 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3420 dw2_instantiate_symtab (per_cu);
3421 }
9291a0cd
TT
3422}
3423
3424static void
3425dw2_expand_all_symtabs (struct objfile *objfile)
3426{
3427 int i;
3428
3429 dw2_setup (objfile);
1fd400ff
TT
3430
3431 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3432 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3433 {
e254ef6a 3434 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3435
a0f42c21 3436 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3437 }
3438}
3439
3440static void
652a8996
JK
3441dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3442 const char *fullname)
9291a0cd
TT
3443{
3444 int i;
3445
3446 dw2_setup (objfile);
d4637a04
DE
3447
3448 /* We don't need to consider type units here.
3449 This is only called for examining code, e.g. expand_line_sal.
3450 There can be an order of magnitude (or more) more type units
3451 than comp units, and we avoid them if we can. */
3452
3453 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3454 {
3455 int j;
e254ef6a 3456 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3457 struct quick_file_names *file_data;
9291a0cd 3458
3d7bb9d9 3459 /* We only need to look at symtabs not already expanded. */
e254ef6a 3460 if (per_cu->v.quick->symtab)
9291a0cd
TT
3461 continue;
3462
e4a48d9d 3463 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3464 if (file_data == NULL)
9291a0cd
TT
3465 continue;
3466
7b9f3c50 3467 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3468 {
652a8996
JK
3469 const char *this_fullname = file_data->file_names[j];
3470
3471 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3472 {
a0f42c21 3473 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3474 break;
3475 }
3476 }
3477 }
3478}
3479
9291a0cd 3480static void
40658b94
PH
3481dw2_map_matching_symbols (const char * name, domain_enum namespace,
3482 struct objfile *objfile, int global,
3483 int (*callback) (struct block *,
3484 struct symbol *, void *),
2edb89d3
JK
3485 void *data, symbol_compare_ftype *match,
3486 symbol_compare_ftype *ordered_compare)
9291a0cd 3487{
40658b94 3488 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3489 current language is Ada for a non-Ada objfile using GNU index. As Ada
3490 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3491}
3492
3493static void
f8eba3c6
TT
3494dw2_expand_symtabs_matching
3495 (struct objfile *objfile,
fbd9ab74 3496 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3497 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3498 enum search_domain kind,
3499 void *data)
9291a0cd
TT
3500{
3501 int i;
3502 offset_type iter;
4b5246aa 3503 struct mapped_index *index;
9291a0cd
TT
3504
3505 dw2_setup (objfile);
ae2de4f8
DE
3506
3507 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3508 if (!dwarf2_per_objfile->index_table)
3509 return;
4b5246aa 3510 index = dwarf2_per_objfile->index_table;
9291a0cd 3511
7b08b9eb 3512 if (file_matcher != NULL)
24c79950
TT
3513 {
3514 struct cleanup *cleanup;
3515 htab_t visited_found, visited_not_found;
3516
3517 visited_found = htab_create_alloc (10,
3518 htab_hash_pointer, htab_eq_pointer,
3519 NULL, xcalloc, xfree);
3520 cleanup = make_cleanup_htab_delete (visited_found);
3521 visited_not_found = htab_create_alloc (10,
3522 htab_hash_pointer, htab_eq_pointer,
3523 NULL, xcalloc, xfree);
3524 make_cleanup_htab_delete (visited_not_found);
3525
848e3e78
DE
3526 /* The rule is CUs specify all the files, including those used by
3527 any TU, so there's no need to scan TUs here. */
3528
3529 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3530 {
3531 int j;
f4dc4d17 3532 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3533 struct quick_file_names *file_data;
3534 void **slot;
7b08b9eb 3535
24c79950 3536 per_cu->v.quick->mark = 0;
3d7bb9d9 3537
24c79950
TT
3538 /* We only need to look at symtabs not already expanded. */
3539 if (per_cu->v.quick->symtab)
3540 continue;
7b08b9eb 3541
e4a48d9d 3542 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3543 if (file_data == NULL)
3544 continue;
7b08b9eb 3545
24c79950
TT
3546 if (htab_find (visited_not_found, file_data) != NULL)
3547 continue;
3548 else if (htab_find (visited_found, file_data) != NULL)
3549 {
3550 per_cu->v.quick->mark = 1;
3551 continue;
3552 }
3553
3554 for (j = 0; j < file_data->num_file_names; ++j)
3555 {
da235a7c
JK
3556 const char *this_real_name;
3557
fbd9ab74 3558 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3559 {
3560 per_cu->v.quick->mark = 1;
3561 break;
3562 }
da235a7c
JK
3563
3564 /* Before we invoke realpath, which can get expensive when many
3565 files are involved, do a quick comparison of the basenames. */
3566 if (!basenames_may_differ
3567 && !file_matcher (lbasename (file_data->file_names[j]),
3568 data, 1))
3569 continue;
3570
3571 this_real_name = dw2_get_real_path (objfile, file_data, j);
3572 if (file_matcher (this_real_name, data, 0))
3573 {
3574 per_cu->v.quick->mark = 1;
3575 break;
3576 }
24c79950
TT
3577 }
3578
3579 slot = htab_find_slot (per_cu->v.quick->mark
3580 ? visited_found
3581 : visited_not_found,
3582 file_data, INSERT);
3583 *slot = file_data;
3584 }
3585
3586 do_cleanups (cleanup);
3587 }
9291a0cd 3588
3876f04e 3589 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3590 {
3591 offset_type idx = 2 * iter;
3592 const char *name;
3593 offset_type *vec, vec_len, vec_idx;
3594
3876f04e 3595 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3596 continue;
3597
3876f04e 3598 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3599
e078317b 3600 if (! (*name_matcher) (name, data))
9291a0cd
TT
3601 continue;
3602
3603 /* The name was matched, now expand corresponding CUs that were
3604 marked. */
4b5246aa 3605 vec = (offset_type *) (index->constant_pool
3876f04e 3606 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3607 vec_len = MAYBE_SWAP (vec[0]);
3608 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3609 {
e254ef6a 3610 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3611 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3612 gdb_index_symbol_kind symbol_kind =
3613 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3614 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3615 /* Only check the symbol attributes if they're present.
3616 Indices prior to version 7 don't record them,
3617 and indices >= 7 may elide them for certain symbols
3618 (gold does this). */
3619 int attrs_valid =
3620 (index->version >= 7
3621 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3622
3623 /* Only check the symbol's kind if it has one. */
3624 if (attrs_valid)
156942c7
DE
3625 {
3626 switch (kind)
3627 {
3628 case VARIABLES_DOMAIN:
3629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3630 continue;
3631 break;
3632 case FUNCTIONS_DOMAIN:
3633 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3634 continue;
3635 break;
3636 case TYPES_DOMAIN:
3637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3638 continue;
3639 break;
3640 default:
3641 break;
3642 }
3643 }
3644
3190f0c6
DE
3645 /* Don't crash on bad data. */
3646 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3647 + dwarf2_per_objfile->n_type_units))
3648 {
3649 complaint (&symfile_complaints,
3650 _(".gdb_index entry has bad CU index"
4262abfb 3651 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
3652 continue;
3653 }
3654
156942c7 3655 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3656 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3657 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3658 }
3659 }
3660}
3661
9703b513
TT
3662/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3663 symtab. */
3664
3665static struct symtab *
3666recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3667{
3668 int i;
3669
3670 if (BLOCKVECTOR (symtab) != NULL
3671 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3672 return symtab;
3673
a3ec0bb1
DE
3674 if (symtab->includes == NULL)
3675 return NULL;
3676
9703b513
TT
3677 for (i = 0; symtab->includes[i]; ++i)
3678 {
a3ec0bb1 3679 struct symtab *s = symtab->includes[i];
9703b513
TT
3680
3681 s = recursively_find_pc_sect_symtab (s, pc);
3682 if (s != NULL)
3683 return s;
3684 }
3685
3686 return NULL;
3687}
3688
9291a0cd
TT
3689static struct symtab *
3690dw2_find_pc_sect_symtab (struct objfile *objfile,
3691 struct minimal_symbol *msymbol,
3692 CORE_ADDR pc,
3693 struct obj_section *section,
3694 int warn_if_readin)
3695{
3696 struct dwarf2_per_cu_data *data;
9703b513 3697 struct symtab *result;
9291a0cd
TT
3698
3699 dw2_setup (objfile);
3700
3701 if (!objfile->psymtabs_addrmap)
3702 return NULL;
3703
3704 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3705 if (!data)
3706 return NULL;
3707
3708 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3709 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3710 paddress (get_objfile_arch (objfile), pc));
3711
9703b513
TT
3712 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3713 gdb_assert (result != NULL);
3714 return result;
9291a0cd
TT
3715}
3716
9291a0cd 3717static void
44b13c5a 3718dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3719 void *data, int need_fullname)
9291a0cd
TT
3720{
3721 int i;
24c79950
TT
3722 struct cleanup *cleanup;
3723 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3724 NULL, xcalloc, xfree);
9291a0cd 3725
24c79950 3726 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3727 dw2_setup (objfile);
ae2de4f8 3728
848e3e78
DE
3729 /* The rule is CUs specify all the files, including those used by
3730 any TU, so there's no need to scan TUs here.
3731 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3732
848e3e78 3733 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3734 {
3735 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3736
3737 if (per_cu->v.quick->symtab)
3738 {
3739 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3740 INSERT);
3741
3742 *slot = per_cu->v.quick->file_names;
3743 }
3744 }
3745
848e3e78 3746 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3747 {
3748 int j;
f4dc4d17 3749 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3750 struct quick_file_names *file_data;
24c79950 3751 void **slot;
9291a0cd 3752
3d7bb9d9 3753 /* We only need to look at symtabs not already expanded. */
e254ef6a 3754 if (per_cu->v.quick->symtab)
9291a0cd
TT
3755 continue;
3756
e4a48d9d 3757 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3758 if (file_data == NULL)
9291a0cd
TT
3759 continue;
3760
24c79950
TT
3761 slot = htab_find_slot (visited, file_data, INSERT);
3762 if (*slot)
3763 {
3764 /* Already visited. */
3765 continue;
3766 }
3767 *slot = file_data;
3768
7b9f3c50 3769 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3770 {
74e2f255
DE
3771 const char *this_real_name;
3772
3773 if (need_fullname)
3774 this_real_name = dw2_get_real_path (objfile, file_data, j);
3775 else
3776 this_real_name = NULL;
7b9f3c50 3777 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3778 }
3779 }
24c79950
TT
3780
3781 do_cleanups (cleanup);
9291a0cd
TT
3782}
3783
3784static int
3785dw2_has_symbols (struct objfile *objfile)
3786{
3787 return 1;
3788}
3789
3790const struct quick_symbol_functions dwarf2_gdb_index_functions =
3791{
3792 dw2_has_symbols,
3793 dw2_find_last_source_symtab,
3794 dw2_forget_cached_source_info,
f8eba3c6 3795 dw2_map_symtabs_matching_filename,
9291a0cd 3796 dw2_lookup_symbol,
9291a0cd
TT
3797 dw2_print_stats,
3798 dw2_dump,
3799 dw2_relocate,
3800 dw2_expand_symtabs_for_function,
3801 dw2_expand_all_symtabs,
652a8996 3802 dw2_expand_symtabs_with_fullname,
40658b94 3803 dw2_map_matching_symbols,
9291a0cd
TT
3804 dw2_expand_symtabs_matching,
3805 dw2_find_pc_sect_symtab,
9291a0cd
TT
3806 dw2_map_symbol_filenames
3807};
3808
3809/* Initialize for reading DWARF for this objfile. Return 0 if this
3810 file will use psymtabs, or 1 if using the GNU index. */
3811
3812int
3813dwarf2_initialize_objfile (struct objfile *objfile)
3814{
3815 /* If we're about to read full symbols, don't bother with the
3816 indices. In this case we also don't care if some other debug
3817 format is making psymtabs, because they are all about to be
3818 expanded anyway. */
3819 if ((objfile->flags & OBJF_READNOW))
3820 {
3821 int i;
3822
3823 dwarf2_per_objfile->using_index = 1;
3824 create_all_comp_units (objfile);
0e50663e 3825 create_all_type_units (objfile);
7b9f3c50
DE
3826 dwarf2_per_objfile->quick_file_names_table =
3827 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3828
1fd400ff 3829 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3830 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3831 {
e254ef6a 3832 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3833
e254ef6a
DE
3834 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3835 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3836 }
3837
3838 /* Return 1 so that gdb sees the "quick" functions. However,
3839 these functions will be no-ops because we will have expanded
3840 all symtabs. */
3841 return 1;
3842 }
3843
3844 if (dwarf2_read_index (objfile))
3845 return 1;
3846
9291a0cd
TT
3847 return 0;
3848}
3849
3850\f
3851
dce234bc
PP
3852/* Build a partial symbol table. */
3853
3854void
f29dff0a 3855dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3856{
c9bf0622
TT
3857 volatile struct gdb_exception except;
3858
f29dff0a 3859 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3860 {
3861 init_psymbol_list (objfile, 1024);
3862 }
3863
c9bf0622
TT
3864 TRY_CATCH (except, RETURN_MASK_ERROR)
3865 {
3866 /* This isn't really ideal: all the data we allocate on the
3867 objfile's obstack is still uselessly kept around. However,
3868 freeing it seems unsafe. */
3869 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3870
3871 dwarf2_build_psymtabs_hard (objfile);
3872 discard_cleanups (cleanups);
3873 }
3874 if (except.reason < 0)
3875 exception_print (gdb_stderr, except);
c906108c 3876}
c906108c 3877
1ce1cefd
DE
3878/* Return the total length of the CU described by HEADER. */
3879
3880static unsigned int
3881get_cu_length (const struct comp_unit_head *header)
3882{
3883 return header->initial_length_size + header->length;
3884}
3885
45452591
DE
3886/* Return TRUE if OFFSET is within CU_HEADER. */
3887
3888static inline int
b64f50a1 3889offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3890{
b64f50a1 3891 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3892 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3893
b64f50a1 3894 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3895}
3896
3b80fe9b
DE
3897/* Find the base address of the compilation unit for range lists and
3898 location lists. It will normally be specified by DW_AT_low_pc.
3899 In DWARF-3 draft 4, the base address could be overridden by
3900 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3901 compilation units with discontinuous ranges. */
3902
3903static void
3904dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3905{
3906 struct attribute *attr;
3907
3908 cu->base_known = 0;
3909 cu->base_address = 0;
3910
3911 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3912 if (attr)
3913 {
3914 cu->base_address = DW_ADDR (attr);
3915 cu->base_known = 1;
3916 }
3917 else
3918 {
3919 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3920 if (attr)
3921 {
3922 cu->base_address = DW_ADDR (attr);
3923 cu->base_known = 1;
3924 }
3925 }
3926}
3927
93311388
DE
3928/* Read in the comp unit header information from the debug_info at info_ptr.
3929 NOTE: This leaves members offset, first_die_offset to be filled in
3930 by the caller. */
107d2387 3931
d521ce57 3932static const gdb_byte *
107d2387 3933read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3934 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3935{
3936 int signed_addr;
891d2f0b 3937 unsigned int bytes_read;
c764a876
DE
3938
3939 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3940 cu_header->initial_length_size = bytes_read;
3941 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3942 info_ptr += bytes_read;
107d2387
AC
3943 cu_header->version = read_2_bytes (abfd, info_ptr);
3944 info_ptr += 2;
b64f50a1
JK
3945 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3946 &bytes_read);
613e1657 3947 info_ptr += bytes_read;
107d2387
AC
3948 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3949 info_ptr += 1;
3950 signed_addr = bfd_get_sign_extend_vma (abfd);
3951 if (signed_addr < 0)
8e65ff28 3952 internal_error (__FILE__, __LINE__,
e2e0b3e5 3953 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3954 cu_header->signed_addr_p = signed_addr;
c764a876 3955
107d2387
AC
3956 return info_ptr;
3957}
3958
36586728
TT
3959/* Helper function that returns the proper abbrev section for
3960 THIS_CU. */
3961
3962static struct dwarf2_section_info *
3963get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3964{
3965 struct dwarf2_section_info *abbrev;
3966
3967 if (this_cu->is_dwz)
3968 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3969 else
3970 abbrev = &dwarf2_per_objfile->abbrev;
3971
3972 return abbrev;
3973}
3974
9ff913ba
DE
3975/* Subroutine of read_and_check_comp_unit_head and
3976 read_and_check_type_unit_head to simplify them.
3977 Perform various error checking on the header. */
3978
3979static void
3980error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3981 struct dwarf2_section_info *section,
3982 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3983{
3984 bfd *abfd = section->asection->owner;
3985 const char *filename = bfd_get_filename (abfd);
3986
3987 if (header->version != 2 && header->version != 3 && header->version != 4)
3988 error (_("Dwarf Error: wrong version in compilation unit header "
3989 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3990 filename);
3991
b64f50a1 3992 if (header->abbrev_offset.sect_off
36586728 3993 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3994 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3995 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3996 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3997 filename);
3998
3999 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4000 avoid potential 32-bit overflow. */
1ce1cefd 4001 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4002 > section->size)
4003 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4004 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4005 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4006 filename);
4007}
4008
4009/* Read in a CU/TU header and perform some basic error checking.
4010 The contents of the header are stored in HEADER.
4011 The result is a pointer to the start of the first DIE. */
adabb602 4012
d521ce57 4013static const gdb_byte *
9ff913ba
DE
4014read_and_check_comp_unit_head (struct comp_unit_head *header,
4015 struct dwarf2_section_info *section,
4bdcc0c1 4016 struct dwarf2_section_info *abbrev_section,
d521ce57 4017 const gdb_byte *info_ptr,
9ff913ba 4018 int is_debug_types_section)
72bf9492 4019{
d521ce57 4020 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4021 bfd *abfd = section->asection->owner;
72bf9492 4022
b64f50a1 4023 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4024
72bf9492
DJ
4025 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4026
460c1c54
CC
4027 /* If we're reading a type unit, skip over the signature and
4028 type_offset fields. */
b0df02fd 4029 if (is_debug_types_section)
460c1c54
CC
4030 info_ptr += 8 /*signature*/ + header->offset_size;
4031
b64f50a1 4032 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4033
4bdcc0c1 4034 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4035
4036 return info_ptr;
4037}
4038
348e048f
DE
4039/* Read in the types comp unit header information from .debug_types entry at
4040 types_ptr. The result is a pointer to one past the end of the header. */
4041
d521ce57 4042static const gdb_byte *
9ff913ba
DE
4043read_and_check_type_unit_head (struct comp_unit_head *header,
4044 struct dwarf2_section_info *section,
4bdcc0c1 4045 struct dwarf2_section_info *abbrev_section,
d521ce57 4046 const gdb_byte *info_ptr,
dee91e82
DE
4047 ULONGEST *signature,
4048 cu_offset *type_offset_in_tu)
348e048f 4049{
d521ce57 4050 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4051 bfd *abfd = section->asection->owner;
348e048f 4052
b64f50a1 4053 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4054
9ff913ba 4055 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4056
9ff913ba
DE
4057 /* If we're reading a type unit, skip over the signature and
4058 type_offset fields. */
4059 if (signature != NULL)
4060 *signature = read_8_bytes (abfd, info_ptr);
4061 info_ptr += 8;
dee91e82
DE
4062 if (type_offset_in_tu != NULL)
4063 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4064 header->offset_size);
9ff913ba
DE
4065 info_ptr += header->offset_size;
4066
b64f50a1 4067 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4068
4bdcc0c1 4069 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4070
4071 return info_ptr;
348e048f
DE
4072}
4073
f4dc4d17
DE
4074/* Fetch the abbreviation table offset from a comp or type unit header. */
4075
4076static sect_offset
4077read_abbrev_offset (struct dwarf2_section_info *section,
4078 sect_offset offset)
4079{
4080 bfd *abfd = section->asection->owner;
d521ce57 4081 const gdb_byte *info_ptr;
f4dc4d17
DE
4082 unsigned int length, initial_length_size, offset_size;
4083 sect_offset abbrev_offset;
4084
4085 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4086 info_ptr = section->buffer + offset.sect_off;
4087 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4088 offset_size = initial_length_size == 4 ? 4 : 8;
4089 info_ptr += initial_length_size + 2 /*version*/;
4090 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4091 return abbrev_offset;
4092}
4093
aaa75496
JB
4094/* Allocate a new partial symtab for file named NAME and mark this new
4095 partial symtab as being an include of PST. */
4096
4097static void
d521ce57 4098dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4099 struct objfile *objfile)
4100{
4101 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4102
fbd9ab74
JK
4103 if (!IS_ABSOLUTE_PATH (subpst->filename))
4104 {
4105 /* It shares objfile->objfile_obstack. */
4106 subpst->dirname = pst->dirname;
4107 }
4108
aaa75496
JB
4109 subpst->section_offsets = pst->section_offsets;
4110 subpst->textlow = 0;
4111 subpst->texthigh = 0;
4112
4113 subpst->dependencies = (struct partial_symtab **)
4114 obstack_alloc (&objfile->objfile_obstack,
4115 sizeof (struct partial_symtab *));
4116 subpst->dependencies[0] = pst;
4117 subpst->number_of_dependencies = 1;
4118
4119 subpst->globals_offset = 0;
4120 subpst->n_global_syms = 0;
4121 subpst->statics_offset = 0;
4122 subpst->n_static_syms = 0;
4123 subpst->symtab = NULL;
4124 subpst->read_symtab = pst->read_symtab;
4125 subpst->readin = 0;
4126
4127 /* No private part is necessary for include psymtabs. This property
4128 can be used to differentiate between such include psymtabs and
10b3939b 4129 the regular ones. */
58a9656e 4130 subpst->read_symtab_private = NULL;
aaa75496
JB
4131}
4132
4133/* Read the Line Number Program data and extract the list of files
4134 included by the source file represented by PST. Build an include
d85a05f0 4135 partial symtab for each of these included files. */
aaa75496
JB
4136
4137static void
4138dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4139 struct die_info *die,
4140 struct partial_symtab *pst)
aaa75496 4141{
d85a05f0
DJ
4142 struct line_header *lh = NULL;
4143 struct attribute *attr;
aaa75496 4144
d85a05f0
DJ
4145 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4146 if (attr)
3019eac3 4147 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4148 if (lh == NULL)
4149 return; /* No linetable, so no includes. */
4150
c6da4cef 4151 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4152 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4153
4154 free_line_header (lh);
4155}
4156
348e048f 4157static hashval_t
52dc124a 4158hash_signatured_type (const void *item)
348e048f 4159{
52dc124a 4160 const struct signatured_type *sig_type = item;
9a619af0 4161
348e048f 4162 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4163 return sig_type->signature;
348e048f
DE
4164}
4165
4166static int
52dc124a 4167eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4168{
4169 const struct signatured_type *lhs = item_lhs;
4170 const struct signatured_type *rhs = item_rhs;
9a619af0 4171
348e048f
DE
4172 return lhs->signature == rhs->signature;
4173}
4174
1fd400ff
TT
4175/* Allocate a hash table for signatured types. */
4176
4177static htab_t
673bfd45 4178allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4179{
4180 return htab_create_alloc_ex (41,
52dc124a
DE
4181 hash_signatured_type,
4182 eq_signatured_type,
1fd400ff
TT
4183 NULL,
4184 &objfile->objfile_obstack,
4185 hashtab_obstack_allocate,
4186 dummy_obstack_deallocate);
4187}
4188
d467dd73 4189/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4190
4191static int
d467dd73 4192add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4193{
4194 struct signatured_type *sigt = *slot;
b4dd5633 4195 struct signatured_type ***datap = datum;
1fd400ff 4196
b4dd5633 4197 **datap = sigt;
1fd400ff
TT
4198 ++*datap;
4199
4200 return 1;
4201}
4202
c88ee1f0
DE
4203/* Create the hash table of all entries in the .debug_types
4204 (or .debug_types.dwo) section(s).
4205 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4206 otherwise it is NULL.
4207
4208 The result is a pointer to the hash table or NULL if there are no types.
4209
4210 Note: This function processes DWO files only, not DWP files. */
348e048f 4211
3019eac3
DE
4212static htab_t
4213create_debug_types_hash_table (struct dwo_file *dwo_file,
4214 VEC (dwarf2_section_info_def) *types)
348e048f 4215{
3019eac3 4216 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4217 htab_t types_htab = NULL;
8b70b953
TT
4218 int ix;
4219 struct dwarf2_section_info *section;
4bdcc0c1 4220 struct dwarf2_section_info *abbrev_section;
348e048f 4221
3019eac3
DE
4222 if (VEC_empty (dwarf2_section_info_def, types))
4223 return NULL;
348e048f 4224
4bdcc0c1
DE
4225 abbrev_section = (dwo_file != NULL
4226 ? &dwo_file->sections.abbrev
4227 : &dwarf2_per_objfile->abbrev);
4228
09406207
DE
4229 if (dwarf2_read_debug)
4230 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4231 dwo_file ? ".dwo" : "",
4232 bfd_get_filename (abbrev_section->asection->owner));
4233
8b70b953 4234 for (ix = 0;
3019eac3 4235 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4236 ++ix)
4237 {
3019eac3 4238 bfd *abfd;
d521ce57 4239 const gdb_byte *info_ptr, *end_ptr;
36586728 4240 struct dwarf2_section_info *abbrev_section;
348e048f 4241
8b70b953
TT
4242 dwarf2_read_section (objfile, section);
4243 info_ptr = section->buffer;
348e048f 4244
8b70b953
TT
4245 if (info_ptr == NULL)
4246 continue;
348e048f 4247
3019eac3
DE
4248 /* We can't set abfd until now because the section may be empty or
4249 not present, in which case section->asection will be NULL. */
4250 abfd = section->asection->owner;
4251
36586728
TT
4252 if (dwo_file)
4253 abbrev_section = &dwo_file->sections.abbrev;
4254 else
4255 abbrev_section = &dwarf2_per_objfile->abbrev;
4256
dee91e82
DE
4257 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4258 because we don't need to read any dies: the signature is in the
4259 header. */
8b70b953
TT
4260
4261 end_ptr = info_ptr + section->size;
4262 while (info_ptr < end_ptr)
4263 {
b64f50a1 4264 sect_offset offset;
3019eac3 4265 cu_offset type_offset_in_tu;
8b70b953 4266 ULONGEST signature;
52dc124a 4267 struct signatured_type *sig_type;
3019eac3 4268 struct dwo_unit *dwo_tu;
8b70b953 4269 void **slot;
d521ce57 4270 const gdb_byte *ptr = info_ptr;
9ff913ba 4271 struct comp_unit_head header;
dee91e82 4272 unsigned int length;
348e048f 4273
b64f50a1 4274 offset.sect_off = ptr - section->buffer;
348e048f 4275
8b70b953 4276 /* We need to read the type's signature in order to build the hash
9ff913ba 4277 table, but we don't need anything else just yet. */
348e048f 4278
4bdcc0c1
DE
4279 ptr = read_and_check_type_unit_head (&header, section,
4280 abbrev_section, ptr,
3019eac3 4281 &signature, &type_offset_in_tu);
6caca83c 4282
1ce1cefd 4283 length = get_cu_length (&header);
dee91e82 4284
6caca83c 4285 /* Skip dummy type units. */
dee91e82
DE
4286 if (ptr >= info_ptr + length
4287 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4288 {
1ce1cefd 4289 info_ptr += length;
6caca83c
CC
4290 continue;
4291 }
8b70b953 4292
0349ea22
DE
4293 if (types_htab == NULL)
4294 {
4295 if (dwo_file)
4296 types_htab = allocate_dwo_unit_table (objfile);
4297 else
4298 types_htab = allocate_signatured_type_table (objfile);
4299 }
4300
3019eac3
DE
4301 if (dwo_file)
4302 {
4303 sig_type = NULL;
4304 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4305 struct dwo_unit);
4306 dwo_tu->dwo_file = dwo_file;
4307 dwo_tu->signature = signature;
4308 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4309 dwo_tu->section = section;
3019eac3
DE
4310 dwo_tu->offset = offset;
4311 dwo_tu->length = length;
4312 }
4313 else
4314 {
4315 /* N.B.: type_offset is not usable if this type uses a DWO file.
4316 The real type_offset is in the DWO file. */
4317 dwo_tu = NULL;
4318 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4319 struct signatured_type);
4320 sig_type->signature = signature;
4321 sig_type->type_offset_in_tu = type_offset_in_tu;
4322 sig_type->per_cu.objfile = objfile;
4323 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4324 sig_type->per_cu.section = section;
3019eac3
DE
4325 sig_type->per_cu.offset = offset;
4326 sig_type->per_cu.length = length;
4327 }
8b70b953 4328
3019eac3
DE
4329 slot = htab_find_slot (types_htab,
4330 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4331 INSERT);
8b70b953
TT
4332 gdb_assert (slot != NULL);
4333 if (*slot != NULL)
4334 {
3019eac3
DE
4335 sect_offset dup_offset;
4336
4337 if (dwo_file)
4338 {
4339 const struct dwo_unit *dup_tu = *slot;
4340
4341 dup_offset = dup_tu->offset;
4342 }
4343 else
4344 {
4345 const struct signatured_type *dup_tu = *slot;
4346
4347 dup_offset = dup_tu->per_cu.offset;
4348 }
b3c8eb43 4349
8b70b953 4350 complaint (&symfile_complaints,
c88ee1f0 4351 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4352 " the entry at offset 0x%x, signature %s"),
3019eac3 4353 offset.sect_off, dup_offset.sect_off,
4031ecc5 4354 hex_string (signature));
8b70b953 4355 }
3019eac3 4356 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4357
09406207 4358 if (dwarf2_read_debug)
4031ecc5 4359 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4360 offset.sect_off,
4031ecc5 4361 hex_string (signature));
348e048f 4362
dee91e82 4363 info_ptr += length;
8b70b953 4364 }
348e048f
DE
4365 }
4366
3019eac3
DE
4367 return types_htab;
4368}
4369
4370/* Create the hash table of all entries in the .debug_types section,
4371 and initialize all_type_units.
4372 The result is zero if there is an error (e.g. missing .debug_types section),
4373 otherwise non-zero. */
4374
4375static int
4376create_all_type_units (struct objfile *objfile)
4377{
4378 htab_t types_htab;
b4dd5633 4379 struct signatured_type **iter;
3019eac3
DE
4380
4381 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4382 if (types_htab == NULL)
4383 {
4384 dwarf2_per_objfile->signatured_types = NULL;
4385 return 0;
4386 }
4387
348e048f
DE
4388 dwarf2_per_objfile->signatured_types = types_htab;
4389
d467dd73
DE
4390 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4391 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4392 = xmalloc (dwarf2_per_objfile->n_type_units
4393 * sizeof (struct signatured_type *));
d467dd73
DE
4394 iter = &dwarf2_per_objfile->all_type_units[0];
4395 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4396 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4397 == dwarf2_per_objfile->n_type_units);
1fd400ff 4398
348e048f
DE
4399 return 1;
4400}
4401
a2ce51a0
DE
4402/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4403 Fill in SIG_ENTRY with DWO_ENTRY. */
4404
4405static void
4406fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4407 struct signatured_type *sig_entry,
4408 struct dwo_unit *dwo_entry)
4409{
7ee85ab1 4410 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4411 gdb_assert (! sig_entry->per_cu.queued);
4412 gdb_assert (sig_entry->per_cu.cu == NULL);
4413 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4414 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4415 gdb_assert (sig_entry->signature == dwo_entry->signature);
4416 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4417 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4418 gdb_assert (sig_entry->dwo_unit == NULL);
4419
4420 sig_entry->per_cu.section = dwo_entry->section;
4421 sig_entry->per_cu.offset = dwo_entry->offset;
4422 sig_entry->per_cu.length = dwo_entry->length;
4423 sig_entry->per_cu.reading_dwo_directly = 1;
4424 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4425 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4426 sig_entry->dwo_unit = dwo_entry;
4427}
4428
4429/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4430 If we haven't read the TU yet, create the signatured_type data structure
4431 for a TU to be read in directly from a DWO file, bypassing the stub.
4432 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4433 using .gdb_index, then when reading a CU we want to stay in the DWO file
4434 containing that CU. Otherwise we could end up reading several other DWO
4435 files (due to comdat folding) to process the transitive closure of all the
4436 mentioned TUs, and that can be slow. The current DWO file will have every
4437 type signature that it needs.
a2ce51a0
DE
4438 We only do this for .gdb_index because in the psymtab case we already have
4439 to read all the DWOs to build the type unit groups. */
4440
4441static struct signatured_type *
4442lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4443{
4444 struct objfile *objfile = dwarf2_per_objfile->objfile;
4445 struct dwo_file *dwo_file;
4446 struct dwo_unit find_dwo_entry, *dwo_entry;
4447 struct signatured_type find_sig_entry, *sig_entry;
4448
4449 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4450
4451 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4452 dwo_unit of the TU itself. */
4453 dwo_file = cu->dwo_unit->dwo_file;
4454
4455 /* We only ever need to read in one copy of a signatured type.
4456 Just use the global signatured_types array. If this is the first time
4457 we're reading this type, replace the recorded data from .gdb_index with
4458 this TU. */
4459
4460 if (dwarf2_per_objfile->signatured_types == NULL)
4461 return NULL;
4462 find_sig_entry.signature = sig;
4463 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4464 if (sig_entry == NULL)
4465 return NULL;
7ee85ab1
DE
4466
4467 /* We can get here with the TU already read, *or* in the process of being
4468 read. Don't reassign it if that's the case. Also note that if the TU is
4469 already being read, it may not have come from a DWO, the program may be
4470 a mix of Fission-compiled code and non-Fission-compiled code. */
a2ce51a0 4471 /* Have we already tried to read this TU? */
7ee85ab1 4472 if (sig_entry->per_cu.tu_read)
a2ce51a0
DE
4473 return sig_entry;
4474
4475 /* Ok, this is the first time we're reading this TU. */
4476 if (dwo_file->tus == NULL)
4477 return NULL;
4478 find_dwo_entry.signature = sig;
4479 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4480 if (dwo_entry == NULL)
4481 return NULL;
4482
4483 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 4484 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
4485 return sig_entry;
4486}
4487
4488/* Subroutine of lookup_dwp_signatured_type.
4489 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4490
4491static struct signatured_type *
4492add_type_unit (ULONGEST sig)
4493{
4494 struct objfile *objfile = dwarf2_per_objfile->objfile;
4495 int n_type_units = dwarf2_per_objfile->n_type_units;
4496 struct signatured_type *sig_type;
4497 void **slot;
4498
4499 ++n_type_units;
4500 dwarf2_per_objfile->all_type_units =
4501 xrealloc (dwarf2_per_objfile->all_type_units,
4502 n_type_units * sizeof (struct signatured_type *));
4503 dwarf2_per_objfile->n_type_units = n_type_units;
4504 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4505 struct signatured_type);
4506 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4507 sig_type->signature = sig;
4508 sig_type->per_cu.is_debug_types = 1;
4509 sig_type->per_cu.v.quick =
4510 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4511 struct dwarf2_per_cu_quick_data);
4512 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4513 sig_type, INSERT);
4514 gdb_assert (*slot == NULL);
4515 *slot = sig_type;
4516 /* The rest of sig_type must be filled in by the caller. */
4517 return sig_type;
4518}
4519
4520/* Subroutine of lookup_signatured_type.
4521 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4522 then try the DWP file.
4523 Normally this "can't happen", but if there's a bug in signature
4524 generation and/or the DWP file is built incorrectly, it can happen.
4525 Using the type directly from the DWP file means we don't have the stub
4526 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4527 not critical. [Eventually the stub may go away for type units anyway.] */
4528
4529static struct signatured_type *
4530lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4531{
4532 struct objfile *objfile = dwarf2_per_objfile->objfile;
4533 struct dwp_file *dwp_file = get_dwp_file ();
4534 struct dwo_unit *dwo_entry;
4535 struct signatured_type find_sig_entry, *sig_entry;
4536
4537 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4538 gdb_assert (dwp_file != NULL);
4539
4540 if (dwarf2_per_objfile->signatured_types != NULL)
4541 {
4542 find_sig_entry.signature = sig;
4543 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4544 &find_sig_entry);
4545 if (sig_entry != NULL)
4546 return sig_entry;
4547 }
4548
4549 /* This is the "shouldn't happen" case.
4550 Try the DWP file and hope for the best. */
4551 if (dwp_file->tus == NULL)
4552 return NULL;
4553 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4554 sig, 1 /* is_debug_types */);
4555 if (dwo_entry == NULL)
4556 return NULL;
4557
4558 sig_entry = add_type_unit (sig);
4559 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4560
4561 /* The caller will signal a complaint if we return NULL.
4562 Here we don't return NULL but we still want to complain. */
4563 complaint (&symfile_complaints,
4564 _("Bad type signature %s referenced by %s at 0x%x,"
4565 " coping by using copy in DWP [in module %s]"),
4566 hex_string (sig),
4567 cu->per_cu->is_debug_types ? "TU" : "CU",
4568 cu->per_cu->offset.sect_off,
4262abfb 4569 objfile_name (objfile));
a2ce51a0
DE
4570
4571 return sig_entry;
4572}
4573
380bca97 4574/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4575 Returns NULL if signature SIG is not present in the table.
4576 It is up to the caller to complain about this. */
348e048f
DE
4577
4578static struct signatured_type *
a2ce51a0 4579lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4580{
a2ce51a0
DE
4581 if (cu->dwo_unit
4582 && dwarf2_per_objfile->using_index)
4583 {
4584 /* We're in a DWO/DWP file, and we're using .gdb_index.
4585 These cases require special processing. */
4586 if (get_dwp_file () == NULL)
4587 return lookup_dwo_signatured_type (cu, sig);
4588 else
4589 return lookup_dwp_signatured_type (cu, sig);
4590 }
4591 else
4592 {
4593 struct signatured_type find_entry, *entry;
348e048f 4594
a2ce51a0
DE
4595 if (dwarf2_per_objfile->signatured_types == NULL)
4596 return NULL;
4597 find_entry.signature = sig;
4598 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4599 return entry;
4600 }
348e048f 4601}
42e7ad6c
DE
4602\f
4603/* Low level DIE reading support. */
348e048f 4604
d85a05f0
DJ
4605/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4606
4607static void
4608init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4609 struct dwarf2_cu *cu,
3019eac3
DE
4610 struct dwarf2_section_info *section,
4611 struct dwo_file *dwo_file)
d85a05f0 4612{
fceca515 4613 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4614 reader->abfd = section->asection->owner;
d85a05f0 4615 reader->cu = cu;
3019eac3 4616 reader->dwo_file = dwo_file;
dee91e82
DE
4617 reader->die_section = section;
4618 reader->buffer = section->buffer;
f664829e 4619 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4620 reader->comp_dir = NULL;
d85a05f0
DJ
4621}
4622
b0c7bfa9
DE
4623/* Subroutine of init_cutu_and_read_dies to simplify it.
4624 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4625 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4626 already.
4627
4628 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4629 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4630 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4631 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4632 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4633 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4634 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4635 are filled in with the info of the DIE from the DWO file.
4636 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4637 provided an abbrev table to use.
4638 The result is non-zero if a valid (non-dummy) DIE was found. */
4639
4640static int
4641read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4642 struct dwo_unit *dwo_unit,
4643 int abbrev_table_provided,
4644 struct die_info *stub_comp_unit_die,
a2ce51a0 4645 const char *stub_comp_dir,
b0c7bfa9 4646 struct die_reader_specs *result_reader,
d521ce57 4647 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4648 struct die_info **result_comp_unit_die,
4649 int *result_has_children)
4650{
4651 struct objfile *objfile = dwarf2_per_objfile->objfile;
4652 struct dwarf2_cu *cu = this_cu->cu;
4653 struct dwarf2_section_info *section;
4654 bfd *abfd;
d521ce57 4655 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4656 const char *comp_dir_string;
4657 ULONGEST signature; /* Or dwo_id. */
4658 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4659 int i,num_extra_attrs;
4660 struct dwarf2_section_info *dwo_abbrev_section;
4661 struct attribute *attr;
a2ce51a0 4662 struct attribute comp_dir_attr;
b0c7bfa9
DE
4663 struct die_info *comp_unit_die;
4664
a2ce51a0
DE
4665 /* Both can't be provided. */
4666 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4667
b0c7bfa9
DE
4668 /* These attributes aren't processed until later:
4669 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4670 However, the attribute is found in the stub which we won't have later.
4671 In order to not impose this complication on the rest of the code,
4672 we read them here and copy them to the DWO CU/TU die. */
4673
4674 stmt_list = NULL;
4675 low_pc = NULL;
4676 high_pc = NULL;
4677 ranges = NULL;
4678 comp_dir = NULL;
4679
4680 if (stub_comp_unit_die != NULL)
4681 {
4682 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4683 DWO file. */
4684 if (! this_cu->is_debug_types)
4685 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4686 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4687 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4688 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4689 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4690
4691 /* There should be a DW_AT_addr_base attribute here (if needed).
4692 We need the value before we can process DW_FORM_GNU_addr_index. */
4693 cu->addr_base = 0;
4694 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4695 if (attr)
4696 cu->addr_base = DW_UNSND (attr);
4697
4698 /* There should be a DW_AT_ranges_base attribute here (if needed).
4699 We need the value before we can process DW_AT_ranges. */
4700 cu->ranges_base = 0;
4701 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4702 if (attr)
4703 cu->ranges_base = DW_UNSND (attr);
4704 }
a2ce51a0
DE
4705 else if (stub_comp_dir != NULL)
4706 {
4707 /* Reconstruct the comp_dir attribute to simplify the code below. */
4708 comp_dir = (struct attribute *)
4709 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4710 comp_dir->name = DW_AT_comp_dir;
4711 comp_dir->form = DW_FORM_string;
4712 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4713 DW_STRING (comp_dir) = stub_comp_dir;
4714 }
b0c7bfa9
DE
4715
4716 /* Set up for reading the DWO CU/TU. */
4717 cu->dwo_unit = dwo_unit;
4718 section = dwo_unit->section;
4719 dwarf2_read_section (objfile, section);
4720 abfd = section->asection->owner;
4721 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4722 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4723 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4724
4725 if (this_cu->is_debug_types)
4726 {
4727 ULONGEST header_signature;
4728 cu_offset type_offset_in_tu;
4729 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4730
4731 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4732 dwo_abbrev_section,
4733 info_ptr,
4734 &header_signature,
4735 &type_offset_in_tu);
a2ce51a0
DE
4736 /* This is not an assert because it can be caused by bad debug info. */
4737 if (sig_type->signature != header_signature)
4738 {
4739 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4740 " TU at offset 0x%x [in module %s]"),
4741 hex_string (sig_type->signature),
4742 hex_string (header_signature),
4743 dwo_unit->offset.sect_off,
4744 bfd_get_filename (abfd));
4745 }
b0c7bfa9
DE
4746 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4747 /* For DWOs coming from DWP files, we don't know the CU length
4748 nor the type's offset in the TU until now. */
4749 dwo_unit->length = get_cu_length (&cu->header);
4750 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4751
4752 /* Establish the type offset that can be used to lookup the type.
4753 For DWO files, we don't know it until now. */
4754 sig_type->type_offset_in_section.sect_off =
4755 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4756 }
4757 else
4758 {
4759 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4760 dwo_abbrev_section,
4761 info_ptr, 0);
4762 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4763 /* For DWOs coming from DWP files, we don't know the CU length
4764 until now. */
4765 dwo_unit->length = get_cu_length (&cu->header);
4766 }
4767
02142a6c
DE
4768 /* Replace the CU's original abbrev table with the DWO's.
4769 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4770 if (abbrev_table_provided)
4771 {
4772 /* Don't free the provided abbrev table, the caller of
4773 init_cutu_and_read_dies owns it. */
4774 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4775 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4776 make_cleanup (dwarf2_free_abbrev_table, cu);
4777 }
4778 else
4779 {
4780 dwarf2_free_abbrev_table (cu);
4781 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4782 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4783 }
4784
4785 /* Read in the die, but leave space to copy over the attributes
4786 from the stub. This has the benefit of simplifying the rest of
4787 the code - all the work to maintain the illusion of a single
4788 DW_TAG_{compile,type}_unit DIE is done here. */
4789 num_extra_attrs = ((stmt_list != NULL)
4790 + (low_pc != NULL)
4791 + (high_pc != NULL)
4792 + (ranges != NULL)
4793 + (comp_dir != NULL));
4794 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4795 result_has_children, num_extra_attrs);
4796
4797 /* Copy over the attributes from the stub to the DIE we just read in. */
4798 comp_unit_die = *result_comp_unit_die;
4799 i = comp_unit_die->num_attrs;
4800 if (stmt_list != NULL)
4801 comp_unit_die->attrs[i++] = *stmt_list;
4802 if (low_pc != NULL)
4803 comp_unit_die->attrs[i++] = *low_pc;
4804 if (high_pc != NULL)
4805 comp_unit_die->attrs[i++] = *high_pc;
4806 if (ranges != NULL)
4807 comp_unit_die->attrs[i++] = *ranges;
4808 if (comp_dir != NULL)
4809 comp_unit_die->attrs[i++] = *comp_dir;
4810 comp_unit_die->num_attrs += num_extra_attrs;
4811
bf6af496
DE
4812 if (dwarf2_die_debug)
4813 {
4814 fprintf_unfiltered (gdb_stdlog,
4815 "Read die from %s@0x%x of %s:\n",
4816 bfd_section_name (abfd, section->asection),
4817 (unsigned) (begin_info_ptr - section->buffer),
4818 bfd_get_filename (abfd));
4819 dump_die (comp_unit_die, dwarf2_die_debug);
4820 }
4821
a2ce51a0
DE
4822 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4823 TUs by skipping the stub and going directly to the entry in the DWO file.
4824 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4825 to get it via circuitous means. Blech. */
4826 if (comp_dir != NULL)
4827 result_reader->comp_dir = DW_STRING (comp_dir);
4828
b0c7bfa9
DE
4829 /* Skip dummy compilation units. */
4830 if (info_ptr >= begin_info_ptr + dwo_unit->length
4831 || peek_abbrev_code (abfd, info_ptr) == 0)
4832 return 0;
4833
4834 *result_info_ptr = info_ptr;
4835 return 1;
4836}
4837
4838/* Subroutine of init_cutu_and_read_dies to simplify it.
4839 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4840 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4841
4842static struct dwo_unit *
4843lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4844 struct die_info *comp_unit_die)
4845{
4846 struct dwarf2_cu *cu = this_cu->cu;
4847 struct attribute *attr;
4848 ULONGEST signature;
4849 struct dwo_unit *dwo_unit;
4850 const char *comp_dir, *dwo_name;
4851
a2ce51a0
DE
4852 gdb_assert (cu != NULL);
4853
b0c7bfa9
DE
4854 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4855 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4856 gdb_assert (attr != NULL);
4857 dwo_name = DW_STRING (attr);
4858 comp_dir = NULL;
4859 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4860 if (attr)
4861 comp_dir = DW_STRING (attr);
4862
4863 if (this_cu->is_debug_types)
4864 {
4865 struct signatured_type *sig_type;
4866
4867 /* Since this_cu is the first member of struct signatured_type,
4868 we can go from a pointer to one to a pointer to the other. */
4869 sig_type = (struct signatured_type *) this_cu;
4870 signature = sig_type->signature;
4871 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4872 }
4873 else
4874 {
4875 struct attribute *attr;
4876
4877 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4878 if (! attr)
4879 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4880 " [in module %s]"),
4262abfb 4881 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
4882 signature = DW_UNSND (attr);
4883 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4884 signature);
4885 }
4886
b0c7bfa9
DE
4887 return dwo_unit;
4888}
4889
a2ce51a0
DE
4890/* Subroutine of init_cutu_and_read_dies to simplify it.
4891 Read a TU directly from a DWO file, bypassing the stub. */
4892
4893static void
4894init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4895 die_reader_func_ftype *die_reader_func,
4896 void *data)
4897{
4898 struct dwarf2_cu *cu;
4899 struct signatured_type *sig_type;
4900 struct cleanup *cleanups, *free_cu_cleanup;
4901 struct die_reader_specs reader;
4902 const gdb_byte *info_ptr;
4903 struct die_info *comp_unit_die;
4904 int has_children;
4905
4906 /* Verify we can do the following downcast, and that we have the
4907 data we need. */
4908 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4909 sig_type = (struct signatured_type *) this_cu;
4910 gdb_assert (sig_type->dwo_unit != NULL);
4911
4912 cleanups = make_cleanup (null_cleanup, NULL);
4913
4914 gdb_assert (this_cu->cu == NULL);
4915 cu = xmalloc (sizeof (*cu));
4916 init_one_comp_unit (cu, this_cu);
4917 /* If an error occurs while loading, release our storage. */
4918 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4919
4920 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4921 0 /* abbrev_table_provided */,
4922 NULL /* stub_comp_unit_die */,
4923 sig_type->dwo_unit->dwo_file->comp_dir,
4924 &reader, &info_ptr,
4925 &comp_unit_die, &has_children) == 0)
4926 {
4927 /* Dummy die. */
4928 do_cleanups (cleanups);
4929 return;
4930 }
4931
4932 /* All the "real" work is done here. */
4933 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4934
4935 /* This duplicates some code in init_cutu_and_read_dies,
4936 but the alternative is making the latter more complex.
4937 This function is only for the special case of using DWO files directly:
4938 no point in overly complicating the general case just to handle this. */
4939 if (keep)
4940 {
4941 /* We've successfully allocated this compilation unit. Let our
4942 caller clean it up when finished with it. */
4943 discard_cleanups (free_cu_cleanup);
4944
4945 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4946 So we have to manually free the abbrev table. */
4947 dwarf2_free_abbrev_table (cu);
4948
4949 /* Link this CU into read_in_chain. */
4950 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4951 dwarf2_per_objfile->read_in_chain = this_cu;
4952 }
4953 else
4954 do_cleanups (free_cu_cleanup);
4955
4956 do_cleanups (cleanups);
4957}
4958
fd820528 4959/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4960 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4961
f4dc4d17
DE
4962 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4963 Otherwise the table specified in the comp unit header is read in and used.
4964 This is an optimization for when we already have the abbrev table.
4965
dee91e82
DE
4966 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4967 Otherwise, a new CU is allocated with xmalloc.
4968
4969 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4970 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4971
4972 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4973 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4974
70221824 4975static void
fd820528 4976init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4977 struct abbrev_table *abbrev_table,
fd820528
DE
4978 int use_existing_cu, int keep,
4979 die_reader_func_ftype *die_reader_func,
4980 void *data)
c906108c 4981{
dee91e82 4982 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4983 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4984 bfd *abfd = section->asection->owner;
dee91e82 4985 struct dwarf2_cu *cu;
d521ce57 4986 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4987 struct die_reader_specs reader;
d85a05f0 4988 struct die_info *comp_unit_die;
dee91e82 4989 int has_children;
d85a05f0 4990 struct attribute *attr;
365156ad 4991 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 4992 struct signatured_type *sig_type = NULL;
4bdcc0c1 4993 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4994 /* Non-zero if CU currently points to a DWO file and we need to
4995 reread it. When this happens we need to reread the skeleton die
a2ce51a0 4996 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 4997 int rereading_dwo_cu = 0;
c906108c 4998
09406207
DE
4999 if (dwarf2_die_debug)
5000 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5001 this_cu->is_debug_types ? "type" : "comp",
5002 this_cu->offset.sect_off);
5003
dee91e82
DE
5004 if (use_existing_cu)
5005 gdb_assert (keep);
23745b47 5006
a2ce51a0
DE
5007 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5008 file (instead of going through the stub), short-circuit all of this. */
5009 if (this_cu->reading_dwo_directly)
5010 {
5011 /* Narrow down the scope of possibilities to have to understand. */
5012 gdb_assert (this_cu->is_debug_types);
5013 gdb_assert (abbrev_table == NULL);
5014 gdb_assert (!use_existing_cu);
5015 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5016 return;
5017 }
5018
dee91e82
DE
5019 cleanups = make_cleanup (null_cleanup, NULL);
5020
5021 /* This is cheap if the section is already read in. */
5022 dwarf2_read_section (objfile, section);
5023
5024 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5025
5026 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5027
5028 if (use_existing_cu && this_cu->cu != NULL)
5029 {
5030 cu = this_cu->cu;
42e7ad6c
DE
5031
5032 /* If this CU is from a DWO file we need to start over, we need to
5033 refetch the attributes from the skeleton CU.
5034 This could be optimized by retrieving those attributes from when we
5035 were here the first time: the previous comp_unit_die was stored in
5036 comp_unit_obstack. But there's no data yet that we need this
5037 optimization. */
5038 if (cu->dwo_unit != NULL)
5039 rereading_dwo_cu = 1;
dee91e82
DE
5040 }
5041 else
5042 {
5043 /* If !use_existing_cu, this_cu->cu must be NULL. */
5044 gdb_assert (this_cu->cu == NULL);
5045
5046 cu = xmalloc (sizeof (*cu));
5047 init_one_comp_unit (cu, this_cu);
5048
5049 /* If an error occurs while loading, release our storage. */
365156ad 5050 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5051 }
dee91e82 5052
b0c7bfa9 5053 /* Get the header. */
42e7ad6c
DE
5054 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5055 {
5056 /* We already have the header, there's no need to read it in again. */
5057 info_ptr += cu->header.first_die_offset.cu_off;
5058 }
5059 else
5060 {
3019eac3 5061 if (this_cu->is_debug_types)
dee91e82
DE
5062 {
5063 ULONGEST signature;
42e7ad6c 5064 cu_offset type_offset_in_tu;
dee91e82 5065
4bdcc0c1
DE
5066 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5067 abbrev_section, info_ptr,
42e7ad6c
DE
5068 &signature,
5069 &type_offset_in_tu);
dee91e82 5070
42e7ad6c
DE
5071 /* Since per_cu is the first member of struct signatured_type,
5072 we can go from a pointer to one to a pointer to the other. */
5073 sig_type = (struct signatured_type *) this_cu;
5074 gdb_assert (sig_type->signature == signature);
5075 gdb_assert (sig_type->type_offset_in_tu.cu_off
5076 == type_offset_in_tu.cu_off);
dee91e82
DE
5077 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5078
42e7ad6c
DE
5079 /* LENGTH has not been set yet for type units if we're
5080 using .gdb_index. */
1ce1cefd 5081 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5082
5083 /* Establish the type offset that can be used to lookup the type. */
5084 sig_type->type_offset_in_section.sect_off =
5085 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5086 }
5087 else
5088 {
4bdcc0c1
DE
5089 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5090 abbrev_section,
5091 info_ptr, 0);
dee91e82
DE
5092
5093 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5094 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5095 }
5096 }
10b3939b 5097
6caca83c 5098 /* Skip dummy compilation units. */
dee91e82 5099 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5100 || peek_abbrev_code (abfd, info_ptr) == 0)
5101 {
dee91e82 5102 do_cleanups (cleanups);
21b2bd31 5103 return;
6caca83c
CC
5104 }
5105
433df2d4
DE
5106 /* If we don't have them yet, read the abbrevs for this compilation unit.
5107 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5108 done. Note that it's important that if the CU had an abbrev table
5109 on entry we don't free it when we're done: Somewhere up the call stack
5110 it may be in use. */
f4dc4d17
DE
5111 if (abbrev_table != NULL)
5112 {
5113 gdb_assert (cu->abbrev_table == NULL);
5114 gdb_assert (cu->header.abbrev_offset.sect_off
5115 == abbrev_table->offset.sect_off);
5116 cu->abbrev_table = abbrev_table;
5117 }
5118 else if (cu->abbrev_table == NULL)
dee91e82 5119 {
4bdcc0c1 5120 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5121 make_cleanup (dwarf2_free_abbrev_table, cu);
5122 }
42e7ad6c
DE
5123 else if (rereading_dwo_cu)
5124 {
5125 dwarf2_free_abbrev_table (cu);
5126 dwarf2_read_abbrevs (cu, abbrev_section);
5127 }
af703f96 5128
dee91e82 5129 /* Read the top level CU/TU die. */
3019eac3 5130 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5131 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5132
b0c7bfa9
DE
5133 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5134 from the DWO file.
5135 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5136 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5137 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5138 if (attr)
5139 {
3019eac3 5140 struct dwo_unit *dwo_unit;
b0c7bfa9 5141 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5142
5143 if (has_children)
6a506a2d
DE
5144 {
5145 complaint (&symfile_complaints,
5146 _("compilation unit with DW_AT_GNU_dwo_name"
5147 " has children (offset 0x%x) [in module %s]"),
5148 this_cu->offset.sect_off, bfd_get_filename (abfd));
5149 }
b0c7bfa9 5150 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5151 if (dwo_unit != NULL)
3019eac3 5152 {
6a506a2d
DE
5153 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5154 abbrev_table != NULL,
a2ce51a0 5155 comp_unit_die, NULL,
6a506a2d
DE
5156 &reader, &info_ptr,
5157 &dwo_comp_unit_die, &has_children) == 0)
5158 {
5159 /* Dummy die. */
5160 do_cleanups (cleanups);
5161 return;
5162 }
5163 comp_unit_die = dwo_comp_unit_die;
5164 }
5165 else
5166 {
5167 /* Yikes, we couldn't find the rest of the DIE, we only have
5168 the stub. A complaint has already been logged. There's
5169 not much more we can do except pass on the stub DIE to
5170 die_reader_func. We don't want to throw an error on bad
5171 debug info. */
3019eac3
DE
5172 }
5173 }
5174
b0c7bfa9 5175 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5176 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5177
b0c7bfa9 5178 /* Done, clean up. */
365156ad 5179 if (free_cu_cleanup != NULL)
348e048f 5180 {
365156ad
TT
5181 if (keep)
5182 {
5183 /* We've successfully allocated this compilation unit. Let our
5184 caller clean it up when finished with it. */
5185 discard_cleanups (free_cu_cleanup);
dee91e82 5186
365156ad
TT
5187 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5188 So we have to manually free the abbrev table. */
5189 dwarf2_free_abbrev_table (cu);
dee91e82 5190
365156ad
TT
5191 /* Link this CU into read_in_chain. */
5192 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5193 dwarf2_per_objfile->read_in_chain = this_cu;
5194 }
5195 else
5196 do_cleanups (free_cu_cleanup);
348e048f 5197 }
365156ad
TT
5198
5199 do_cleanups (cleanups);
dee91e82
DE
5200}
5201
3019eac3
DE
5202/* Read CU/TU THIS_CU in section SECTION,
5203 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5204 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5205 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5206
5207 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5208 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5209
5210 We fill in THIS_CU->length.
5211
5212 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5213 linker) then DIE_READER_FUNC will not get called.
5214
5215 THIS_CU->cu is always freed when done.
3019eac3
DE
5216 This is done in order to not leave THIS_CU->cu in a state where we have
5217 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5218
5219static void
5220init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5221 struct dwarf2_section_info *abbrev_section,
3019eac3 5222 struct dwo_file *dwo_file,
dee91e82
DE
5223 die_reader_func_ftype *die_reader_func,
5224 void *data)
5225{
5226 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5227 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5228 bfd *abfd = section->asection->owner;
dee91e82 5229 struct dwarf2_cu cu;
d521ce57 5230 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5231 struct die_reader_specs reader;
5232 struct cleanup *cleanups;
5233 struct die_info *comp_unit_die;
5234 int has_children;
5235
09406207
DE
5236 if (dwarf2_die_debug)
5237 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5238 this_cu->is_debug_types ? "type" : "comp",
5239 this_cu->offset.sect_off);
5240
dee91e82
DE
5241 gdb_assert (this_cu->cu == NULL);
5242
dee91e82
DE
5243 /* This is cheap if the section is already read in. */
5244 dwarf2_read_section (objfile, section);
5245
5246 init_one_comp_unit (&cu, this_cu);
5247
5248 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5249
5250 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5251 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5252 abbrev_section, info_ptr,
3019eac3 5253 this_cu->is_debug_types);
dee91e82 5254
1ce1cefd 5255 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5256
5257 /* Skip dummy compilation units. */
5258 if (info_ptr >= begin_info_ptr + this_cu->length
5259 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5260 {
dee91e82 5261 do_cleanups (cleanups);
21b2bd31 5262 return;
93311388 5263 }
72bf9492 5264
dee91e82
DE
5265 dwarf2_read_abbrevs (&cu, abbrev_section);
5266 make_cleanup (dwarf2_free_abbrev_table, &cu);
5267
3019eac3 5268 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5269 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5270
5271 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5272
5273 do_cleanups (cleanups);
5274}
5275
3019eac3
DE
5276/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5277 does not lookup the specified DWO file.
5278 This cannot be used to read DWO files.
dee91e82
DE
5279
5280 THIS_CU->cu is always freed when done.
3019eac3
DE
5281 This is done in order to not leave THIS_CU->cu in a state where we have
5282 to care whether it refers to the "main" CU or the DWO CU.
5283 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5284
5285static void
5286init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5287 die_reader_func_ftype *die_reader_func,
5288 void *data)
5289{
5290 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5291 get_abbrev_section_for_cu (this_cu),
3019eac3 5292 NULL,
dee91e82
DE
5293 die_reader_func, data);
5294}
0018ea6f
DE
5295\f
5296/* Type Unit Groups.
dee91e82 5297
0018ea6f
DE
5298 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5299 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5300 so that all types coming from the same compilation (.o file) are grouped
5301 together. A future step could be to put the types in the same symtab as
5302 the CU the types ultimately came from. */
ff013f42 5303
f4dc4d17
DE
5304static hashval_t
5305hash_type_unit_group (const void *item)
5306{
094b34ac 5307 const struct type_unit_group *tu_group = item;
f4dc4d17 5308
094b34ac 5309 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5310}
348e048f
DE
5311
5312static int
f4dc4d17 5313eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5314{
f4dc4d17
DE
5315 const struct type_unit_group *lhs = item_lhs;
5316 const struct type_unit_group *rhs = item_rhs;
348e048f 5317
094b34ac 5318 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5319}
348e048f 5320
f4dc4d17
DE
5321/* Allocate a hash table for type unit groups. */
5322
5323static htab_t
5324allocate_type_unit_groups_table (void)
5325{
5326 return htab_create_alloc_ex (3,
5327 hash_type_unit_group,
5328 eq_type_unit_group,
5329 NULL,
5330 &dwarf2_per_objfile->objfile->objfile_obstack,
5331 hashtab_obstack_allocate,
5332 dummy_obstack_deallocate);
5333}
dee91e82 5334
f4dc4d17
DE
5335/* Type units that don't have DW_AT_stmt_list are grouped into their own
5336 partial symtabs. We combine several TUs per psymtab to not let the size
5337 of any one psymtab grow too big. */
5338#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5339#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5340
094b34ac 5341/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5342 Create the type_unit_group object used to hold one or more TUs. */
5343
5344static struct type_unit_group *
094b34ac 5345create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5346{
5347 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5348 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5349 struct type_unit_group *tu_group;
f4dc4d17
DE
5350
5351 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5352 struct type_unit_group);
094b34ac 5353 per_cu = &tu_group->per_cu;
f4dc4d17 5354 per_cu->objfile = objfile;
f4dc4d17 5355
094b34ac
DE
5356 if (dwarf2_per_objfile->using_index)
5357 {
5358 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5359 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5360 }
5361 else
5362 {
5363 unsigned int line_offset = line_offset_struct.sect_off;
5364 struct partial_symtab *pst;
5365 char *name;
5366
5367 /* Give the symtab a useful name for debug purposes. */
5368 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5369 name = xstrprintf ("<type_units_%d>",
5370 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5371 else
5372 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5373
5374 pst = create_partial_symtab (per_cu, name);
5375 pst->anonymous = 1;
f4dc4d17 5376
094b34ac
DE
5377 xfree (name);
5378 }
f4dc4d17 5379
094b34ac
DE
5380 tu_group->hash.dwo_unit = cu->dwo_unit;
5381 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5382
5383 return tu_group;
5384}
5385
094b34ac
DE
5386/* Look up the type_unit_group for type unit CU, and create it if necessary.
5387 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5388
5389static struct type_unit_group *
ff39bb5e 5390get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5391{
5392 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5393 struct type_unit_group *tu_group;
5394 void **slot;
5395 unsigned int line_offset;
5396 struct type_unit_group type_unit_group_for_lookup;
5397
5398 if (dwarf2_per_objfile->type_unit_groups == NULL)
5399 {
5400 dwarf2_per_objfile->type_unit_groups =
5401 allocate_type_unit_groups_table ();
5402 }
5403
5404 /* Do we need to create a new group, or can we use an existing one? */
5405
5406 if (stmt_list)
5407 {
5408 line_offset = DW_UNSND (stmt_list);
5409 ++tu_stats->nr_symtab_sharers;
5410 }
5411 else
5412 {
5413 /* Ugh, no stmt_list. Rare, but we have to handle it.
5414 We can do various things here like create one group per TU or
5415 spread them over multiple groups to split up the expansion work.
5416 To avoid worst case scenarios (too many groups or too large groups)
5417 we, umm, group them in bunches. */
5418 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5419 | (tu_stats->nr_stmt_less_type_units
5420 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5421 ++tu_stats->nr_stmt_less_type_units;
5422 }
5423
094b34ac
DE
5424 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5425 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5426 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5427 &type_unit_group_for_lookup, INSERT);
5428 if (*slot != NULL)
5429 {
5430 tu_group = *slot;
5431 gdb_assert (tu_group != NULL);
5432 }
5433 else
5434 {
5435 sect_offset line_offset_struct;
5436
5437 line_offset_struct.sect_off = line_offset;
094b34ac 5438 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5439 *slot = tu_group;
5440 ++tu_stats->nr_symtabs;
5441 }
5442
5443 return tu_group;
5444}
5445
5446/* Struct used to sort TUs by their abbreviation table offset. */
5447
5448struct tu_abbrev_offset
5449{
5450 struct signatured_type *sig_type;
5451 sect_offset abbrev_offset;
5452};
5453
5454/* Helper routine for build_type_unit_groups, passed to qsort. */
5455
5456static int
5457sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5458{
5459 const struct tu_abbrev_offset * const *a = ap;
5460 const struct tu_abbrev_offset * const *b = bp;
5461 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5462 unsigned int boff = (*b)->abbrev_offset.sect_off;
5463
5464 return (aoff > boff) - (aoff < boff);
5465}
5466
5467/* A helper function to add a type_unit_group to a table. */
5468
5469static int
5470add_type_unit_group_to_table (void **slot, void *datum)
5471{
5472 struct type_unit_group *tu_group = *slot;
5473 struct type_unit_group ***datap = datum;
5474
5475 **datap = tu_group;
5476 ++*datap;
5477
5478 return 1;
5479}
5480
5481/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5482 each one passing FUNC,DATA.
5483
5484 The efficiency is because we sort TUs by the abbrev table they use and
5485 only read each abbrev table once. In one program there are 200K TUs
5486 sharing 8K abbrev tables.
5487
5488 The main purpose of this function is to support building the
5489 dwarf2_per_objfile->type_unit_groups table.
5490 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5491 can collapse the search space by grouping them by stmt_list.
5492 The savings can be significant, in the same program from above the 200K TUs
5493 share 8K stmt_list tables.
5494
5495 FUNC is expected to call get_type_unit_group, which will create the
5496 struct type_unit_group if necessary and add it to
5497 dwarf2_per_objfile->type_unit_groups. */
5498
5499static void
5500build_type_unit_groups (die_reader_func_ftype *func, void *data)
5501{
5502 struct objfile *objfile = dwarf2_per_objfile->objfile;
5503 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5504 struct cleanup *cleanups;
5505 struct abbrev_table *abbrev_table;
5506 sect_offset abbrev_offset;
5507 struct tu_abbrev_offset *sorted_by_abbrev;
5508 struct type_unit_group **iter;
5509 int i;
5510
5511 /* It's up to the caller to not call us multiple times. */
5512 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5513
5514 if (dwarf2_per_objfile->n_type_units == 0)
5515 return;
5516
5517 /* TUs typically share abbrev tables, and there can be way more TUs than
5518 abbrev tables. Sort by abbrev table to reduce the number of times we
5519 read each abbrev table in.
5520 Alternatives are to punt or to maintain a cache of abbrev tables.
5521 This is simpler and efficient enough for now.
5522
5523 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5524 symtab to use). Typically TUs with the same abbrev offset have the same
5525 stmt_list value too so in practice this should work well.
5526
5527 The basic algorithm here is:
5528
5529 sort TUs by abbrev table
5530 for each TU with same abbrev table:
5531 read abbrev table if first user
5532 read TU top level DIE
5533 [IWBN if DWO skeletons had DW_AT_stmt_list]
5534 call FUNC */
5535
5536 if (dwarf2_read_debug)
5537 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5538
5539 /* Sort in a separate table to maintain the order of all_type_units
5540 for .gdb_index: TU indices directly index all_type_units. */
5541 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5542 dwarf2_per_objfile->n_type_units);
5543 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5544 {
5545 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5546
5547 sorted_by_abbrev[i].sig_type = sig_type;
5548 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5549 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5550 sig_type->per_cu.offset);
5551 }
5552 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5553 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5554 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5555
094b34ac
DE
5556 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5557 called any number of times, so we don't reset tu_stats here. */
5558
f4dc4d17
DE
5559 abbrev_offset.sect_off = ~(unsigned) 0;
5560 abbrev_table = NULL;
5561 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5562
5563 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5564 {
5565 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5566
5567 /* Switch to the next abbrev table if necessary. */
5568 if (abbrev_table == NULL
5569 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5570 {
5571 if (abbrev_table != NULL)
5572 {
5573 abbrev_table_free (abbrev_table);
5574 /* Reset to NULL in case abbrev_table_read_table throws
5575 an error: abbrev_table_free_cleanup will get called. */
5576 abbrev_table = NULL;
5577 }
5578 abbrev_offset = tu->abbrev_offset;
5579 abbrev_table =
5580 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5581 abbrev_offset);
5582 ++tu_stats->nr_uniq_abbrev_tables;
5583 }
5584
5585 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5586 func, data);
5587 }
5588
a2ce51a0
DE
5589 /* type_unit_groups can be NULL if there is an error in the debug info.
5590 Just create an empty table so the rest of gdb doesn't have to watch
5591 for this error case. */
5592 if (dwarf2_per_objfile->type_unit_groups == NULL)
5593 {
5594 dwarf2_per_objfile->type_unit_groups =
5595 allocate_type_unit_groups_table ();
5596 dwarf2_per_objfile->n_type_unit_groups = 0;
5597 }
5598
f4dc4d17
DE
5599 /* Create a vector of pointers to primary type units to make it easy to
5600 iterate over them and CUs. See dw2_get_primary_cu. */
5601 dwarf2_per_objfile->n_type_unit_groups =
5602 htab_elements (dwarf2_per_objfile->type_unit_groups);
5603 dwarf2_per_objfile->all_type_unit_groups =
5604 obstack_alloc (&objfile->objfile_obstack,
5605 dwarf2_per_objfile->n_type_unit_groups
5606 * sizeof (struct type_unit_group *));
5607 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5608 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5609 add_type_unit_group_to_table, &iter);
5610 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5611 == dwarf2_per_objfile->n_type_unit_groups);
5612
5613 do_cleanups (cleanups);
5614
5615 if (dwarf2_read_debug)
5616 {
5617 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5618 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5619 dwarf2_per_objfile->n_type_units);
5620 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5621 tu_stats->nr_uniq_abbrev_tables);
5622 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5623 tu_stats->nr_symtabs);
5624 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5625 tu_stats->nr_symtab_sharers);
5626 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5627 tu_stats->nr_stmt_less_type_units);
5628 }
5629}
0018ea6f
DE
5630\f
5631/* Partial symbol tables. */
5632
5633/* Create a psymtab named NAME and assign it to PER_CU.
5634
5635 The caller must fill in the following details:
5636 dirname, textlow, texthigh. */
5637
5638static struct partial_symtab *
5639create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5640{
5641 struct objfile *objfile = per_cu->objfile;
5642 struct partial_symtab *pst;
5643
5644 pst = start_psymtab_common (objfile, objfile->section_offsets,
5645 name, 0,
5646 objfile->global_psymbols.next,
5647 objfile->static_psymbols.next);
5648
5649 pst->psymtabs_addrmap_supported = 1;
5650
5651 /* This is the glue that links PST into GDB's symbol API. */
5652 pst->read_symtab_private = per_cu;
5653 pst->read_symtab = dwarf2_read_symtab;
5654 per_cu->v.psymtab = pst;
5655
5656 return pst;
5657}
5658
b93601f3
TT
5659/* The DATA object passed to process_psymtab_comp_unit_reader has this
5660 type. */
5661
5662struct process_psymtab_comp_unit_data
5663{
5664 /* True if we are reading a DW_TAG_partial_unit. */
5665
5666 int want_partial_unit;
5667
5668 /* The "pretend" language that is used if the CU doesn't declare a
5669 language. */
5670
5671 enum language pretend_language;
5672};
5673
0018ea6f
DE
5674/* die_reader_func for process_psymtab_comp_unit. */
5675
5676static void
5677process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5678 const gdb_byte *info_ptr,
0018ea6f
DE
5679 struct die_info *comp_unit_die,
5680 int has_children,
5681 void *data)
5682{
5683 struct dwarf2_cu *cu = reader->cu;
5684 struct objfile *objfile = cu->objfile;
5685 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5686 struct attribute *attr;
5687 CORE_ADDR baseaddr;
5688 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5689 struct partial_symtab *pst;
5690 int has_pc_info;
5691 const char *filename;
b93601f3 5692 struct process_psymtab_comp_unit_data *info = data;
0018ea6f 5693
b93601f3 5694 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
5695 return;
5696
5697 gdb_assert (! per_cu->is_debug_types);
5698
b93601f3 5699 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
5700
5701 cu->list_in_scope = &file_symbols;
5702
5703 /* Allocate a new partial symbol table structure. */
5704 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5705 if (attr == NULL || !DW_STRING (attr))
5706 filename = "";
5707 else
5708 filename = DW_STRING (attr);
5709
5710 pst = create_partial_symtab (per_cu, filename);
5711
5712 /* This must be done before calling dwarf2_build_include_psymtabs. */
5713 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5714 if (attr != NULL)
5715 pst->dirname = DW_STRING (attr);
5716
5717 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5718
5719 dwarf2_find_base_address (comp_unit_die, cu);
5720
5721 /* Possibly set the default values of LOWPC and HIGHPC from
5722 `DW_AT_ranges'. */
5723 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5724 &best_highpc, cu, pst);
5725 if (has_pc_info == 1 && best_lowpc < best_highpc)
5726 /* Store the contiguous range if it is not empty; it can be empty for
5727 CUs with no code. */
5728 addrmap_set_empty (objfile->psymtabs_addrmap,
5729 best_lowpc + baseaddr,
5730 best_highpc + baseaddr - 1, pst);
5731
5732 /* Check if comp unit has_children.
5733 If so, read the rest of the partial symbols from this comp unit.
5734 If not, there's no more debug_info for this comp unit. */
5735 if (has_children)
5736 {
5737 struct partial_die_info *first_die;
5738 CORE_ADDR lowpc, highpc;
5739
5740 lowpc = ((CORE_ADDR) -1);
5741 highpc = ((CORE_ADDR) 0);
5742
5743 first_die = load_partial_dies (reader, info_ptr, 1);
5744
5745 scan_partial_symbols (first_die, &lowpc, &highpc,
5746 ! has_pc_info, cu);
5747
5748 /* If we didn't find a lowpc, set it to highpc to avoid
5749 complaints from `maint check'. */
5750 if (lowpc == ((CORE_ADDR) -1))
5751 lowpc = highpc;
5752
5753 /* If the compilation unit didn't have an explicit address range,
5754 then use the information extracted from its child dies. */
5755 if (! has_pc_info)
5756 {
5757 best_lowpc = lowpc;
5758 best_highpc = highpc;
5759 }
5760 }
5761 pst->textlow = best_lowpc + baseaddr;
5762 pst->texthigh = best_highpc + baseaddr;
5763
5764 pst->n_global_syms = objfile->global_psymbols.next -
5765 (objfile->global_psymbols.list + pst->globals_offset);
5766 pst->n_static_syms = objfile->static_psymbols.next -
5767 (objfile->static_psymbols.list + pst->statics_offset);
5768 sort_pst_symbols (objfile, pst);
5769
5770 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5771 {
5772 int i;
5773 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5774 struct dwarf2_per_cu_data *iter;
5775
5776 /* Fill in 'dependencies' here; we fill in 'users' in a
5777 post-pass. */
5778 pst->number_of_dependencies = len;
5779 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5780 len * sizeof (struct symtab *));
5781 for (i = 0;
5782 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5783 i, iter);
5784 ++i)
5785 pst->dependencies[i] = iter->v.psymtab;
5786
5787 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5788 }
5789
5790 /* Get the list of files included in the current compilation unit,
5791 and build a psymtab for each of them. */
5792 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5793
5794 if (dwarf2_read_debug)
5795 {
5796 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5797
5798 fprintf_unfiltered (gdb_stdlog,
5799 "Psymtab for %s unit @0x%x: %s - %s"
5800 ", %d global, %d static syms\n",
5801 per_cu->is_debug_types ? "type" : "comp",
5802 per_cu->offset.sect_off,
5803 paddress (gdbarch, pst->textlow),
5804 paddress (gdbarch, pst->texthigh),
5805 pst->n_global_syms, pst->n_static_syms);
5806 }
5807}
5808
5809/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5810 Process compilation unit THIS_CU for a psymtab. */
5811
5812static void
5813process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
5814 int want_partial_unit,
5815 enum language pretend_language)
0018ea6f 5816{
b93601f3
TT
5817 struct process_psymtab_comp_unit_data info;
5818
0018ea6f
DE
5819 /* If this compilation unit was already read in, free the
5820 cached copy in order to read it in again. This is
5821 necessary because we skipped some symbols when we first
5822 read in the compilation unit (see load_partial_dies).
5823 This problem could be avoided, but the benefit is unclear. */
5824 if (this_cu->cu != NULL)
5825 free_one_cached_comp_unit (this_cu);
5826
5827 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
5828 info.want_partial_unit = want_partial_unit;
5829 info.pretend_language = pretend_language;
0018ea6f
DE
5830 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5831 process_psymtab_comp_unit_reader,
b93601f3 5832 &info);
0018ea6f
DE
5833
5834 /* Age out any secondary CUs. */
5835 age_cached_comp_units ();
5836}
f4dc4d17
DE
5837
5838/* Reader function for build_type_psymtabs. */
5839
5840static void
5841build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5842 const gdb_byte *info_ptr,
f4dc4d17
DE
5843 struct die_info *type_unit_die,
5844 int has_children,
5845 void *data)
5846{
5847 struct objfile *objfile = dwarf2_per_objfile->objfile;
5848 struct dwarf2_cu *cu = reader->cu;
5849 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5850 struct signatured_type *sig_type;
f4dc4d17
DE
5851 struct type_unit_group *tu_group;
5852 struct attribute *attr;
5853 struct partial_die_info *first_die;
5854 CORE_ADDR lowpc, highpc;
5855 struct partial_symtab *pst;
5856
5857 gdb_assert (data == NULL);
0186c6a7
DE
5858 gdb_assert (per_cu->is_debug_types);
5859 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5860
5861 if (! has_children)
5862 return;
5863
5864 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5865 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5866
0186c6a7 5867 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5868
5869 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5870 cu->list_in_scope = &file_symbols;
5871 pst = create_partial_symtab (per_cu, "");
5872 pst->anonymous = 1;
5873
5874 first_die = load_partial_dies (reader, info_ptr, 1);
5875
5876 lowpc = (CORE_ADDR) -1;
5877 highpc = (CORE_ADDR) 0;
5878 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5879
5880 pst->n_global_syms = objfile->global_psymbols.next -
5881 (objfile->global_psymbols.list + pst->globals_offset);
5882 pst->n_static_syms = objfile->static_psymbols.next -
5883 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5884 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5885}
5886
5887/* Traversal function for build_type_psymtabs. */
5888
5889static int
5890build_type_psymtab_dependencies (void **slot, void *info)
5891{
5892 struct objfile *objfile = dwarf2_per_objfile->objfile;
5893 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5894 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5895 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5896 int len = VEC_length (sig_type_ptr, tu_group->tus);
5897 struct signatured_type *iter;
f4dc4d17
DE
5898 int i;
5899
5900 gdb_assert (len > 0);
0186c6a7 5901 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5902
5903 pst->number_of_dependencies = len;
5904 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5905 len * sizeof (struct psymtab *));
5906 for (i = 0;
0186c6a7 5907 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5908 ++i)
5909 {
0186c6a7
DE
5910 gdb_assert (iter->per_cu.is_debug_types);
5911 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5912 iter->type_unit_group = tu_group;
f4dc4d17
DE
5913 }
5914
0186c6a7 5915 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5916
5917 return 1;
5918}
5919
5920/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5921 Build partial symbol tables for the .debug_types comp-units. */
5922
5923static void
5924build_type_psymtabs (struct objfile *objfile)
5925{
0e50663e 5926 if (! create_all_type_units (objfile))
348e048f
DE
5927 return;
5928
f4dc4d17
DE
5929 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5930
5931 /* Now that all TUs have been processed we can fill in the dependencies. */
5932 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5933 build_type_psymtab_dependencies, NULL);
348e048f
DE
5934}
5935
60606b2c
TT
5936/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5937
5938static void
5939psymtabs_addrmap_cleanup (void *o)
5940{
5941 struct objfile *objfile = o;
ec61707d 5942
60606b2c
TT
5943 objfile->psymtabs_addrmap = NULL;
5944}
5945
95554aad
TT
5946/* Compute the 'user' field for each psymtab in OBJFILE. */
5947
5948static void
5949set_partial_user (struct objfile *objfile)
5950{
5951 int i;
5952
5953 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5954 {
5955 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5956 struct partial_symtab *pst = per_cu->v.psymtab;
5957 int j;
5958
36586728
TT
5959 if (pst == NULL)
5960 continue;
5961
95554aad
TT
5962 for (j = 0; j < pst->number_of_dependencies; ++j)
5963 {
5964 /* Set the 'user' field only if it is not already set. */
5965 if (pst->dependencies[j]->user == NULL)
5966 pst->dependencies[j]->user = pst;
5967 }
5968 }
5969}
5970
93311388
DE
5971/* Build the partial symbol table by doing a quick pass through the
5972 .debug_info and .debug_abbrev sections. */
72bf9492 5973
93311388 5974static void
c67a9c90 5975dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5976{
60606b2c
TT
5977 struct cleanup *back_to, *addrmap_cleanup;
5978 struct obstack temp_obstack;
21b2bd31 5979 int i;
93311388 5980
45cfd468
DE
5981 if (dwarf2_read_debug)
5982 {
5983 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 5984 objfile_name (objfile));
45cfd468
DE
5985 }
5986
98bfdba5
PA
5987 dwarf2_per_objfile->reading_partial_symbols = 1;
5988
be391dca 5989 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5990
93311388
DE
5991 /* Any cached compilation units will be linked by the per-objfile
5992 read_in_chain. Make sure to free them when we're done. */
5993 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5994
348e048f
DE
5995 build_type_psymtabs (objfile);
5996
93311388 5997 create_all_comp_units (objfile);
c906108c 5998
60606b2c
TT
5999 /* Create a temporary address map on a temporary obstack. We later
6000 copy this to the final obstack. */
6001 obstack_init (&temp_obstack);
6002 make_cleanup_obstack_free (&temp_obstack);
6003 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6004 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6005
21b2bd31 6006 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6007 {
21b2bd31 6008 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 6009
b93601f3 6010 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6011 }
ff013f42 6012
95554aad
TT
6013 set_partial_user (objfile);
6014
ff013f42
JK
6015 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6016 &objfile->objfile_obstack);
60606b2c 6017 discard_cleanups (addrmap_cleanup);
ff013f42 6018
ae038cb0 6019 do_cleanups (back_to);
45cfd468
DE
6020
6021 if (dwarf2_read_debug)
6022 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6023 objfile_name (objfile));
ae038cb0
DJ
6024}
6025
3019eac3 6026/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6027
6028static void
dee91e82 6029load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6030 const gdb_byte *info_ptr,
dee91e82
DE
6031 struct die_info *comp_unit_die,
6032 int has_children,
6033 void *data)
ae038cb0 6034{
dee91e82 6035 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6036
95554aad 6037 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6038
ae038cb0
DJ
6039 /* Check if comp unit has_children.
6040 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6041 If not, there's no more debug_info for this comp unit. */
d85a05f0 6042 if (has_children)
dee91e82
DE
6043 load_partial_dies (reader, info_ptr, 0);
6044}
98bfdba5 6045
dee91e82
DE
6046/* Load the partial DIEs for a secondary CU into memory.
6047 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6048
dee91e82
DE
6049static void
6050load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6051{
f4dc4d17
DE
6052 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6053 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6054}
6055
ae038cb0 6056static void
36586728
TT
6057read_comp_units_from_section (struct objfile *objfile,
6058 struct dwarf2_section_info *section,
6059 unsigned int is_dwz,
6060 int *n_allocated,
6061 int *n_comp_units,
6062 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6063{
d521ce57 6064 const gdb_byte *info_ptr;
36586728 6065 bfd *abfd = section->asection->owner;
be391dca 6066
bf6af496
DE
6067 if (dwarf2_read_debug)
6068 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6069 section->asection->name, bfd_get_filename (abfd));
6070
36586728 6071 dwarf2_read_section (objfile, section);
ae038cb0 6072
36586728 6073 info_ptr = section->buffer;
6e70227d 6074
36586728 6075 while (info_ptr < section->buffer + section->size)
ae038cb0 6076 {
c764a876 6077 unsigned int length, initial_length_size;
ae038cb0 6078 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6079 sect_offset offset;
ae038cb0 6080
36586728 6081 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6082
6083 /* Read just enough information to find out where the next
6084 compilation unit is. */
36586728 6085 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6086
6087 /* Save the compilation unit for later lookup. */
6088 this_cu = obstack_alloc (&objfile->objfile_obstack,
6089 sizeof (struct dwarf2_per_cu_data));
6090 memset (this_cu, 0, sizeof (*this_cu));
6091 this_cu->offset = offset;
c764a876 6092 this_cu->length = length + initial_length_size;
36586728 6093 this_cu->is_dwz = is_dwz;
9291a0cd 6094 this_cu->objfile = objfile;
8a0459fd 6095 this_cu->section = section;
ae038cb0 6096
36586728 6097 if (*n_comp_units == *n_allocated)
ae038cb0 6098 {
36586728
TT
6099 *n_allocated *= 2;
6100 *all_comp_units = xrealloc (*all_comp_units,
6101 *n_allocated
6102 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6103 }
36586728
TT
6104 (*all_comp_units)[*n_comp_units] = this_cu;
6105 ++*n_comp_units;
ae038cb0
DJ
6106
6107 info_ptr = info_ptr + this_cu->length;
6108 }
36586728
TT
6109}
6110
6111/* Create a list of all compilation units in OBJFILE.
6112 This is only done for -readnow and building partial symtabs. */
6113
6114static void
6115create_all_comp_units (struct objfile *objfile)
6116{
6117 int n_allocated;
6118 int n_comp_units;
6119 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6120 struct dwz_file *dwz;
36586728
TT
6121
6122 n_comp_units = 0;
6123 n_allocated = 10;
6124 all_comp_units = xmalloc (n_allocated
6125 * sizeof (struct dwarf2_per_cu_data *));
6126
6127 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6128 &n_allocated, &n_comp_units, &all_comp_units);
6129
4db1a1dc
TT
6130 dwz = dwarf2_get_dwz_file ();
6131 if (dwz != NULL)
6132 read_comp_units_from_section (objfile, &dwz->info, 1,
6133 &n_allocated, &n_comp_units,
6134 &all_comp_units);
ae038cb0
DJ
6135
6136 dwarf2_per_objfile->all_comp_units
6137 = obstack_alloc (&objfile->objfile_obstack,
6138 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6139 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6140 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6141 xfree (all_comp_units);
6142 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6143}
6144
5734ee8b
DJ
6145/* Process all loaded DIEs for compilation unit CU, starting at
6146 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6147 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6148 DW_AT_ranges). If NEED_PC is set, then this function will set
6149 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6150 and record the covered ranges in the addrmap. */
c906108c 6151
72bf9492
DJ
6152static void
6153scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6154 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6155{
72bf9492 6156 struct partial_die_info *pdi;
c906108c 6157
91c24f0a
DC
6158 /* Now, march along the PDI's, descending into ones which have
6159 interesting children but skipping the children of the other ones,
6160 until we reach the end of the compilation unit. */
c906108c 6161
72bf9492 6162 pdi = first_die;
91c24f0a 6163
72bf9492
DJ
6164 while (pdi != NULL)
6165 {
6166 fixup_partial_die (pdi, cu);
c906108c 6167
f55ee35c 6168 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6169 children, so we need to look at them. Ditto for anonymous
6170 enums. */
933c6fe4 6171
72bf9492 6172 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6173 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6174 || pdi->tag == DW_TAG_imported_unit)
c906108c 6175 {
72bf9492 6176 switch (pdi->tag)
c906108c
SS
6177 {
6178 case DW_TAG_subprogram:
5734ee8b 6179 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6180 break;
72929c62 6181 case DW_TAG_constant:
c906108c
SS
6182 case DW_TAG_variable:
6183 case DW_TAG_typedef:
91c24f0a 6184 case DW_TAG_union_type:
72bf9492 6185 if (!pdi->is_declaration)
63d06c5c 6186 {
72bf9492 6187 add_partial_symbol (pdi, cu);
63d06c5c
DC
6188 }
6189 break;
c906108c 6190 case DW_TAG_class_type:
680b30c7 6191 case DW_TAG_interface_type:
c906108c 6192 case DW_TAG_structure_type:
72bf9492 6193 if (!pdi->is_declaration)
c906108c 6194 {
72bf9492 6195 add_partial_symbol (pdi, cu);
c906108c
SS
6196 }
6197 break;
91c24f0a 6198 case DW_TAG_enumeration_type:
72bf9492
DJ
6199 if (!pdi->is_declaration)
6200 add_partial_enumeration (pdi, cu);
c906108c
SS
6201 break;
6202 case DW_TAG_base_type:
a02abb62 6203 case DW_TAG_subrange_type:
c906108c 6204 /* File scope base type definitions are added to the partial
c5aa993b 6205 symbol table. */
72bf9492 6206 add_partial_symbol (pdi, cu);
c906108c 6207 break;
d9fa45fe 6208 case DW_TAG_namespace:
5734ee8b 6209 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6210 break;
5d7cb8df
JK
6211 case DW_TAG_module:
6212 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6213 break;
95554aad
TT
6214 case DW_TAG_imported_unit:
6215 {
6216 struct dwarf2_per_cu_data *per_cu;
6217
f4dc4d17
DE
6218 /* For now we don't handle imported units in type units. */
6219 if (cu->per_cu->is_debug_types)
6220 {
6221 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6222 " supported in type units [in module %s]"),
4262abfb 6223 objfile_name (cu->objfile));
f4dc4d17
DE
6224 }
6225
95554aad 6226 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6227 pdi->is_dwz,
95554aad
TT
6228 cu->objfile);
6229
6230 /* Go read the partial unit, if needed. */
6231 if (per_cu->v.psymtab == NULL)
b93601f3 6232 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6233
f4dc4d17 6234 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6235 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6236 }
6237 break;
c906108c
SS
6238 default:
6239 break;
6240 }
6241 }
6242
72bf9492
DJ
6243 /* If the die has a sibling, skip to the sibling. */
6244
6245 pdi = pdi->die_sibling;
6246 }
6247}
6248
6249/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6250
72bf9492 6251 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6252 name is concatenated with "::" and the partial DIE's name. For
6253 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6254 Enumerators are an exception; they use the scope of their parent
6255 enumeration type, i.e. the name of the enumeration type is not
6256 prepended to the enumerator.
91c24f0a 6257
72bf9492
DJ
6258 There are two complexities. One is DW_AT_specification; in this
6259 case "parent" means the parent of the target of the specification,
6260 instead of the direct parent of the DIE. The other is compilers
6261 which do not emit DW_TAG_namespace; in this case we try to guess
6262 the fully qualified name of structure types from their members'
6263 linkage names. This must be done using the DIE's children rather
6264 than the children of any DW_AT_specification target. We only need
6265 to do this for structures at the top level, i.e. if the target of
6266 any DW_AT_specification (if any; otherwise the DIE itself) does not
6267 have a parent. */
6268
6269/* Compute the scope prefix associated with PDI's parent, in
6270 compilation unit CU. The result will be allocated on CU's
6271 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6272 field. NULL is returned if no prefix is necessary. */
15d034d0 6273static const char *
72bf9492
DJ
6274partial_die_parent_scope (struct partial_die_info *pdi,
6275 struct dwarf2_cu *cu)
6276{
15d034d0 6277 const char *grandparent_scope;
72bf9492 6278 struct partial_die_info *parent, *real_pdi;
91c24f0a 6279
72bf9492
DJ
6280 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6281 then this means the parent of the specification DIE. */
6282
6283 real_pdi = pdi;
72bf9492 6284 while (real_pdi->has_specification)
36586728
TT
6285 real_pdi = find_partial_die (real_pdi->spec_offset,
6286 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6287
6288 parent = real_pdi->die_parent;
6289 if (parent == NULL)
6290 return NULL;
6291
6292 if (parent->scope_set)
6293 return parent->scope;
6294
6295 fixup_partial_die (parent, cu);
6296
10b3939b 6297 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6298
acebe513
UW
6299 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6300 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6301 Work around this problem here. */
6302 if (cu->language == language_cplus
6e70227d 6303 && parent->tag == DW_TAG_namespace
acebe513
UW
6304 && strcmp (parent->name, "::") == 0
6305 && grandparent_scope == NULL)
6306 {
6307 parent->scope = NULL;
6308 parent->scope_set = 1;
6309 return NULL;
6310 }
6311
9c6c53f7
SA
6312 if (pdi->tag == DW_TAG_enumerator)
6313 /* Enumerators should not get the name of the enumeration as a prefix. */
6314 parent->scope = grandparent_scope;
6315 else if (parent->tag == DW_TAG_namespace
f55ee35c 6316 || parent->tag == DW_TAG_module
72bf9492
DJ
6317 || parent->tag == DW_TAG_structure_type
6318 || parent->tag == DW_TAG_class_type
680b30c7 6319 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6320 || parent->tag == DW_TAG_union_type
6321 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6322 {
6323 if (grandparent_scope == NULL)
6324 parent->scope = parent->name;
6325 else
3e43a32a
MS
6326 parent->scope = typename_concat (&cu->comp_unit_obstack,
6327 grandparent_scope,
f55ee35c 6328 parent->name, 0, cu);
72bf9492 6329 }
72bf9492
DJ
6330 else
6331 {
6332 /* FIXME drow/2004-04-01: What should we be doing with
6333 function-local names? For partial symbols, we should probably be
6334 ignoring them. */
6335 complaint (&symfile_complaints,
e2e0b3e5 6336 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6337 parent->tag, pdi->offset.sect_off);
72bf9492 6338 parent->scope = grandparent_scope;
c906108c
SS
6339 }
6340
72bf9492
DJ
6341 parent->scope_set = 1;
6342 return parent->scope;
6343}
6344
6345/* Return the fully scoped name associated with PDI, from compilation unit
6346 CU. The result will be allocated with malloc. */
4568ecf9 6347
72bf9492
DJ
6348static char *
6349partial_die_full_name (struct partial_die_info *pdi,
6350 struct dwarf2_cu *cu)
6351{
15d034d0 6352 const char *parent_scope;
72bf9492 6353
98bfdba5
PA
6354 /* If this is a template instantiation, we can not work out the
6355 template arguments from partial DIEs. So, unfortunately, we have
6356 to go through the full DIEs. At least any work we do building
6357 types here will be reused if full symbols are loaded later. */
6358 if (pdi->has_template_arguments)
6359 {
6360 fixup_partial_die (pdi, cu);
6361
6362 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6363 {
6364 struct die_info *die;
6365 struct attribute attr;
6366 struct dwarf2_cu *ref_cu = cu;
6367
b64f50a1 6368 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6369 attr.name = 0;
6370 attr.form = DW_FORM_ref_addr;
4568ecf9 6371 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6372 die = follow_die_ref (NULL, &attr, &ref_cu);
6373
6374 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6375 }
6376 }
6377
72bf9492
DJ
6378 parent_scope = partial_die_parent_scope (pdi, cu);
6379 if (parent_scope == NULL)
6380 return NULL;
6381 else
f55ee35c 6382 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6383}
6384
6385static void
72bf9492 6386add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6387{
e7c27a73 6388 struct objfile *objfile = cu->objfile;
c906108c 6389 CORE_ADDR addr = 0;
15d034d0 6390 const char *actual_name = NULL;
e142c38c 6391 CORE_ADDR baseaddr;
15d034d0 6392 char *built_actual_name;
e142c38c
DJ
6393
6394 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6395
15d034d0
TT
6396 built_actual_name = partial_die_full_name (pdi, cu);
6397 if (built_actual_name != NULL)
6398 actual_name = built_actual_name;
63d06c5c 6399
72bf9492
DJ
6400 if (actual_name == NULL)
6401 actual_name = pdi->name;
6402
c906108c
SS
6403 switch (pdi->tag)
6404 {
6405 case DW_TAG_subprogram:
2cfa0c8d 6406 if (pdi->is_external || cu->language == language_ada)
c906108c 6407 {
2cfa0c8d
JB
6408 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6409 of the global scope. But in Ada, we want to be able to access
6410 nested procedures globally. So all Ada subprograms are stored
6411 in the global scope. */
f47fb265 6412 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6413 mst_text, objfile); */
f47fb265 6414 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6415 built_actual_name != NULL,
f47fb265
MS
6416 VAR_DOMAIN, LOC_BLOCK,
6417 &objfile->global_psymbols,
6418 0, pdi->lowpc + baseaddr,
6419 cu->language, objfile);
c906108c
SS
6420 }
6421 else
6422 {
f47fb265 6423 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6424 mst_file_text, objfile); */
f47fb265 6425 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6426 built_actual_name != NULL,
f47fb265
MS
6427 VAR_DOMAIN, LOC_BLOCK,
6428 &objfile->static_psymbols,
6429 0, pdi->lowpc + baseaddr,
6430 cu->language, objfile);
c906108c
SS
6431 }
6432 break;
72929c62
JB
6433 case DW_TAG_constant:
6434 {
6435 struct psymbol_allocation_list *list;
6436
6437 if (pdi->is_external)
6438 list = &objfile->global_psymbols;
6439 else
6440 list = &objfile->static_psymbols;
f47fb265 6441 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6442 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6443 list, 0, 0, cu->language, objfile);
72929c62
JB
6444 }
6445 break;
c906108c 6446 case DW_TAG_variable:
95554aad
TT
6447 if (pdi->d.locdesc)
6448 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6449
95554aad 6450 if (pdi->d.locdesc
caac4577
JG
6451 && addr == 0
6452 && !dwarf2_per_objfile->has_section_at_zero)
6453 {
6454 /* A global or static variable may also have been stripped
6455 out by the linker if unused, in which case its address
6456 will be nullified; do not add such variables into partial
6457 symbol table then. */
6458 }
6459 else if (pdi->is_external)
c906108c
SS
6460 {
6461 /* Global Variable.
6462 Don't enter into the minimal symbol tables as there is
6463 a minimal symbol table entry from the ELF symbols already.
6464 Enter into partial symbol table if it has a location
6465 descriptor or a type.
6466 If the location descriptor is missing, new_symbol will create
6467 a LOC_UNRESOLVED symbol, the address of the variable will then
6468 be determined from the minimal symbol table whenever the variable
6469 is referenced.
6470 The address for the partial symbol table entry is not
6471 used by GDB, but it comes in handy for debugging partial symbol
6472 table building. */
6473
95554aad 6474 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6475 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6476 built_actual_name != NULL,
f47fb265
MS
6477 VAR_DOMAIN, LOC_STATIC,
6478 &objfile->global_psymbols,
6479 0, addr + baseaddr,
6480 cu->language, objfile);
c906108c
SS
6481 }
6482 else
6483 {
0963b4bd 6484 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6485 if (pdi->d.locdesc == NULL)
decbce07 6486 {
15d034d0 6487 xfree (built_actual_name);
decbce07
MS
6488 return;
6489 }
f47fb265 6490 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6491 mst_file_data, objfile); */
f47fb265 6492 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6493 built_actual_name != NULL,
f47fb265
MS
6494 VAR_DOMAIN, LOC_STATIC,
6495 &objfile->static_psymbols,
6496 0, addr + baseaddr,
6497 cu->language, objfile);
c906108c
SS
6498 }
6499 break;
6500 case DW_TAG_typedef:
6501 case DW_TAG_base_type:
a02abb62 6502 case DW_TAG_subrange_type:
38d518c9 6503 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6504 built_actual_name != NULL,
176620f1 6505 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6506 &objfile->static_psymbols,
e142c38c 6507 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6508 break;
72bf9492
DJ
6509 case DW_TAG_namespace:
6510 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6511 built_actual_name != NULL,
72bf9492
DJ
6512 VAR_DOMAIN, LOC_TYPEDEF,
6513 &objfile->global_psymbols,
6514 0, (CORE_ADDR) 0, cu->language, objfile);
6515 break;
c906108c 6516 case DW_TAG_class_type:
680b30c7 6517 case DW_TAG_interface_type:
c906108c
SS
6518 case DW_TAG_structure_type:
6519 case DW_TAG_union_type:
6520 case DW_TAG_enumeration_type:
fa4028e9
JB
6521 /* Skip external references. The DWARF standard says in the section
6522 about "Structure, Union, and Class Type Entries": "An incomplete
6523 structure, union or class type is represented by a structure,
6524 union or class entry that does not have a byte size attribute
6525 and that has a DW_AT_declaration attribute." */
6526 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6527 {
15d034d0 6528 xfree (built_actual_name);
decbce07
MS
6529 return;
6530 }
fa4028e9 6531
63d06c5c
DC
6532 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6533 static vs. global. */
38d518c9 6534 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6535 built_actual_name != NULL,
176620f1 6536 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6537 (cu->language == language_cplus
6538 || cu->language == language_java)
63d06c5c
DC
6539 ? &objfile->global_psymbols
6540 : &objfile->static_psymbols,
e142c38c 6541 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6542
c906108c
SS
6543 break;
6544 case DW_TAG_enumerator:
38d518c9 6545 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6546 built_actual_name != NULL,
176620f1 6547 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6548 (cu->language == language_cplus
6549 || cu->language == language_java)
f6fe98ef
DJ
6550 ? &objfile->global_psymbols
6551 : &objfile->static_psymbols,
e142c38c 6552 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6553 break;
6554 default:
6555 break;
6556 }
5c4e30ca 6557
15d034d0 6558 xfree (built_actual_name);
c906108c
SS
6559}
6560
5c4e30ca
DC
6561/* Read a partial die corresponding to a namespace; also, add a symbol
6562 corresponding to that namespace to the symbol table. NAMESPACE is
6563 the name of the enclosing namespace. */
91c24f0a 6564
72bf9492
DJ
6565static void
6566add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6567 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6568 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6569{
72bf9492 6570 /* Add a symbol for the namespace. */
e7c27a73 6571
72bf9492 6572 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6573
6574 /* Now scan partial symbols in that namespace. */
6575
91c24f0a 6576 if (pdi->has_children)
5734ee8b 6577 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6578}
6579
5d7cb8df
JK
6580/* Read a partial die corresponding to a Fortran module. */
6581
6582static void
6583add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6584 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6585{
f55ee35c 6586 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6587
6588 if (pdi->has_children)
6589 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6590}
6591
bc30ff58
JB
6592/* Read a partial die corresponding to a subprogram and create a partial
6593 symbol for that subprogram. When the CU language allows it, this
6594 routine also defines a partial symbol for each nested subprogram
6595 that this subprogram contains.
6e70227d 6596
bc30ff58
JB
6597 DIE my also be a lexical block, in which case we simply search
6598 recursively for suprograms defined inside that lexical block.
6599 Again, this is only performed when the CU language allows this
6600 type of definitions. */
6601
6602static void
6603add_partial_subprogram (struct partial_die_info *pdi,
6604 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6605 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6606{
6607 if (pdi->tag == DW_TAG_subprogram)
6608 {
6609 if (pdi->has_pc_info)
6610 {
6611 if (pdi->lowpc < *lowpc)
6612 *lowpc = pdi->lowpc;
6613 if (pdi->highpc > *highpc)
6614 *highpc = pdi->highpc;
5734ee8b
DJ
6615 if (need_pc)
6616 {
6617 CORE_ADDR baseaddr;
6618 struct objfile *objfile = cu->objfile;
6619
6620 baseaddr = ANOFFSET (objfile->section_offsets,
6621 SECT_OFF_TEXT (objfile));
6622 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6623 pdi->lowpc + baseaddr,
6624 pdi->highpc - 1 + baseaddr,
9291a0cd 6625 cu->per_cu->v.psymtab);
5734ee8b 6626 }
481860b3
GB
6627 }
6628
6629 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6630 {
bc30ff58 6631 if (!pdi->is_declaration)
e8d05480
JB
6632 /* Ignore subprogram DIEs that do not have a name, they are
6633 illegal. Do not emit a complaint at this point, we will
6634 do so when we convert this psymtab into a symtab. */
6635 if (pdi->name)
6636 add_partial_symbol (pdi, cu);
bc30ff58
JB
6637 }
6638 }
6e70227d 6639
bc30ff58
JB
6640 if (! pdi->has_children)
6641 return;
6642
6643 if (cu->language == language_ada)
6644 {
6645 pdi = pdi->die_child;
6646 while (pdi != NULL)
6647 {
6648 fixup_partial_die (pdi, cu);
6649 if (pdi->tag == DW_TAG_subprogram
6650 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6651 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6652 pdi = pdi->die_sibling;
6653 }
6654 }
6655}
6656
91c24f0a
DC
6657/* Read a partial die corresponding to an enumeration type. */
6658
72bf9492
DJ
6659static void
6660add_partial_enumeration (struct partial_die_info *enum_pdi,
6661 struct dwarf2_cu *cu)
91c24f0a 6662{
72bf9492 6663 struct partial_die_info *pdi;
91c24f0a
DC
6664
6665 if (enum_pdi->name != NULL)
72bf9492
DJ
6666 add_partial_symbol (enum_pdi, cu);
6667
6668 pdi = enum_pdi->die_child;
6669 while (pdi)
91c24f0a 6670 {
72bf9492 6671 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6672 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6673 else
72bf9492
DJ
6674 add_partial_symbol (pdi, cu);
6675 pdi = pdi->die_sibling;
91c24f0a 6676 }
91c24f0a
DC
6677}
6678
6caca83c
CC
6679/* Return the initial uleb128 in the die at INFO_PTR. */
6680
6681static unsigned int
d521ce57 6682peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6683{
6684 unsigned int bytes_read;
6685
6686 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6687}
6688
4bb7a0a7
DJ
6689/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6690 Return the corresponding abbrev, or NULL if the number is zero (indicating
6691 an empty DIE). In either case *BYTES_READ will be set to the length of
6692 the initial number. */
6693
6694static struct abbrev_info *
d521ce57 6695peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6696 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6697{
6698 bfd *abfd = cu->objfile->obfd;
6699 unsigned int abbrev_number;
6700 struct abbrev_info *abbrev;
6701
6702 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6703
6704 if (abbrev_number == 0)
6705 return NULL;
6706
433df2d4 6707 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6708 if (!abbrev)
6709 {
3e43a32a
MS
6710 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6711 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6712 }
6713
6714 return abbrev;
6715}
6716
93311388
DE
6717/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6718 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6719 DIE. Any children of the skipped DIEs will also be skipped. */
6720
d521ce57
TT
6721static const gdb_byte *
6722skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6723{
dee91e82 6724 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6725 struct abbrev_info *abbrev;
6726 unsigned int bytes_read;
6727
6728 while (1)
6729 {
6730 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6731 if (abbrev == NULL)
6732 return info_ptr + bytes_read;
6733 else
dee91e82 6734 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6735 }
6736}
6737
93311388
DE
6738/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6739 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6740 abbrev corresponding to that skipped uleb128 should be passed in
6741 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6742 children. */
6743
d521ce57
TT
6744static const gdb_byte *
6745skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6746 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6747{
6748 unsigned int bytes_read;
6749 struct attribute attr;
dee91e82
DE
6750 bfd *abfd = reader->abfd;
6751 struct dwarf2_cu *cu = reader->cu;
d521ce57 6752 const gdb_byte *buffer = reader->buffer;
f664829e 6753 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6754 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6755 unsigned int form, i;
6756
6757 for (i = 0; i < abbrev->num_attrs; i++)
6758 {
6759 /* The only abbrev we care about is DW_AT_sibling. */
6760 if (abbrev->attrs[i].name == DW_AT_sibling)
6761 {
dee91e82 6762 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6763 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6764 complaint (&symfile_complaints,
6765 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6766 else
b64f50a1 6767 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6768 }
6769
6770 /* If it isn't DW_AT_sibling, skip this attribute. */
6771 form = abbrev->attrs[i].form;
6772 skip_attribute:
6773 switch (form)
6774 {
4bb7a0a7 6775 case DW_FORM_ref_addr:
ae411497
TT
6776 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6777 and later it is offset sized. */
6778 if (cu->header.version == 2)
6779 info_ptr += cu->header.addr_size;
6780 else
6781 info_ptr += cu->header.offset_size;
6782 break;
36586728
TT
6783 case DW_FORM_GNU_ref_alt:
6784 info_ptr += cu->header.offset_size;
6785 break;
ae411497 6786 case DW_FORM_addr:
4bb7a0a7
DJ
6787 info_ptr += cu->header.addr_size;
6788 break;
6789 case DW_FORM_data1:
6790 case DW_FORM_ref1:
6791 case DW_FORM_flag:
6792 info_ptr += 1;
6793 break;
2dc7f7b3
TT
6794 case DW_FORM_flag_present:
6795 break;
4bb7a0a7
DJ
6796 case DW_FORM_data2:
6797 case DW_FORM_ref2:
6798 info_ptr += 2;
6799 break;
6800 case DW_FORM_data4:
6801 case DW_FORM_ref4:
6802 info_ptr += 4;
6803 break;
6804 case DW_FORM_data8:
6805 case DW_FORM_ref8:
55f1336d 6806 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6807 info_ptr += 8;
6808 break;
6809 case DW_FORM_string:
9b1c24c8 6810 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6811 info_ptr += bytes_read;
6812 break;
2dc7f7b3 6813 case DW_FORM_sec_offset:
4bb7a0a7 6814 case DW_FORM_strp:
36586728 6815 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6816 info_ptr += cu->header.offset_size;
6817 break;
2dc7f7b3 6818 case DW_FORM_exprloc:
4bb7a0a7
DJ
6819 case DW_FORM_block:
6820 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6821 info_ptr += bytes_read;
6822 break;
6823 case DW_FORM_block1:
6824 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6825 break;
6826 case DW_FORM_block2:
6827 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6828 break;
6829 case DW_FORM_block4:
6830 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6831 break;
6832 case DW_FORM_sdata:
6833 case DW_FORM_udata:
6834 case DW_FORM_ref_udata:
3019eac3
DE
6835 case DW_FORM_GNU_addr_index:
6836 case DW_FORM_GNU_str_index:
d521ce57 6837 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6838 break;
6839 case DW_FORM_indirect:
6840 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6841 info_ptr += bytes_read;
6842 /* We need to continue parsing from here, so just go back to
6843 the top. */
6844 goto skip_attribute;
6845
6846 default:
3e43a32a
MS
6847 error (_("Dwarf Error: Cannot handle %s "
6848 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6849 dwarf_form_name (form),
6850 bfd_get_filename (abfd));
6851 }
6852 }
6853
6854 if (abbrev->has_children)
dee91e82 6855 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6856 else
6857 return info_ptr;
6858}
6859
93311388 6860/* Locate ORIG_PDI's sibling.
dee91e82 6861 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6862
d521ce57 6863static const gdb_byte *
dee91e82
DE
6864locate_pdi_sibling (const struct die_reader_specs *reader,
6865 struct partial_die_info *orig_pdi,
d521ce57 6866 const gdb_byte *info_ptr)
91c24f0a
DC
6867{
6868 /* Do we know the sibling already? */
72bf9492 6869
91c24f0a
DC
6870 if (orig_pdi->sibling)
6871 return orig_pdi->sibling;
6872
6873 /* Are there any children to deal with? */
6874
6875 if (!orig_pdi->has_children)
6876 return info_ptr;
6877
4bb7a0a7 6878 /* Skip the children the long way. */
91c24f0a 6879
dee91e82 6880 return skip_children (reader, info_ptr);
91c24f0a
DC
6881}
6882
257e7a09 6883/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6884 not NULL. */
c906108c
SS
6885
6886static void
257e7a09
YQ
6887dwarf2_read_symtab (struct partial_symtab *self,
6888 struct objfile *objfile)
c906108c 6889{
257e7a09 6890 if (self->readin)
c906108c 6891 {
442e4d9c 6892 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6893 self->filename);
442e4d9c
YQ
6894 }
6895 else
6896 {
6897 if (info_verbose)
c906108c 6898 {
442e4d9c 6899 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6900 self->filename);
442e4d9c 6901 gdb_flush (gdb_stdout);
c906108c 6902 }
c906108c 6903
442e4d9c
YQ
6904 /* Restore our global data. */
6905 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6906
442e4d9c
YQ
6907 /* If this psymtab is constructed from a debug-only objfile, the
6908 has_section_at_zero flag will not necessarily be correct. We
6909 can get the correct value for this flag by looking at the data
6910 associated with the (presumably stripped) associated objfile. */
6911 if (objfile->separate_debug_objfile_backlink)
6912 {
6913 struct dwarf2_per_objfile *dpo_backlink
6914 = objfile_data (objfile->separate_debug_objfile_backlink,
6915 dwarf2_objfile_data_key);
9a619af0 6916
442e4d9c
YQ
6917 dwarf2_per_objfile->has_section_at_zero
6918 = dpo_backlink->has_section_at_zero;
6919 }
b2ab525c 6920
442e4d9c 6921 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6922
257e7a09 6923 psymtab_to_symtab_1 (self);
c906108c 6924
442e4d9c
YQ
6925 /* Finish up the debug error message. */
6926 if (info_verbose)
6927 printf_filtered (_("done.\n"));
c906108c 6928 }
95554aad
TT
6929
6930 process_cu_includes ();
c906108c 6931}
9cdd5dbd
DE
6932\f
6933/* Reading in full CUs. */
c906108c 6934
10b3939b
DJ
6935/* Add PER_CU to the queue. */
6936
6937static void
95554aad
TT
6938queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6939 enum language pretend_language)
10b3939b
DJ
6940{
6941 struct dwarf2_queue_item *item;
6942
6943 per_cu->queued = 1;
6944 item = xmalloc (sizeof (*item));
6945 item->per_cu = per_cu;
95554aad 6946 item->pretend_language = pretend_language;
10b3939b
DJ
6947 item->next = NULL;
6948
6949 if (dwarf2_queue == NULL)
6950 dwarf2_queue = item;
6951 else
6952 dwarf2_queue_tail->next = item;
6953
6954 dwarf2_queue_tail = item;
6955}
6956
89e63ee4
DE
6957/* If PER_CU is not yet queued, add it to the queue.
6958 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
6959 dependency.
0907af0c 6960 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
6961 meaning either PER_CU is already queued or it is already loaded.
6962
6963 N.B. There is an invariant here that if a CU is queued then it is loaded.
6964 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
6965
6966static int
89e63ee4 6967maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
6968 struct dwarf2_per_cu_data *per_cu,
6969 enum language pretend_language)
6970{
6971 /* We may arrive here during partial symbol reading, if we need full
6972 DIEs to process an unusual case (e.g. template arguments). Do
6973 not queue PER_CU, just tell our caller to load its DIEs. */
6974 if (dwarf2_per_objfile->reading_partial_symbols)
6975 {
6976 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6977 return 1;
6978 return 0;
6979 }
6980
6981 /* Mark the dependence relation so that we don't flush PER_CU
6982 too early. */
89e63ee4
DE
6983 if (dependent_cu != NULL)
6984 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
6985
6986 /* If it's already on the queue, we have nothing to do. */
6987 if (per_cu->queued)
6988 return 0;
6989
6990 /* If the compilation unit is already loaded, just mark it as
6991 used. */
6992 if (per_cu->cu != NULL)
6993 {
6994 per_cu->cu->last_used = 0;
6995 return 0;
6996 }
6997
6998 /* Add it to the queue. */
6999 queue_comp_unit (per_cu, pretend_language);
7000
7001 return 1;
7002}
7003
10b3939b
DJ
7004/* Process the queue. */
7005
7006static void
a0f42c21 7007process_queue (void)
10b3939b
DJ
7008{
7009 struct dwarf2_queue_item *item, *next_item;
7010
45cfd468
DE
7011 if (dwarf2_read_debug)
7012 {
7013 fprintf_unfiltered (gdb_stdlog,
7014 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7015 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7016 }
7017
03dd20cc
DJ
7018 /* The queue starts out with one item, but following a DIE reference
7019 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7020 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7021 {
9291a0cd
TT
7022 if (dwarf2_per_objfile->using_index
7023 ? !item->per_cu->v.quick->symtab
7024 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
7025 {
7026 struct dwarf2_per_cu_data *per_cu = item->per_cu;
247f5c4f 7027 char buf[100];
f4dc4d17 7028
247f5c4f 7029 if (per_cu->is_debug_types)
f4dc4d17 7030 {
247f5c4f
DE
7031 struct signatured_type *sig_type =
7032 (struct signatured_type *) per_cu;
7033
7034 sprintf (buf, "TU %s at offset 0x%x",
7035 hex_string (sig_type->signature), per_cu->offset.sect_off);
f4dc4d17 7036 }
247f5c4f
DE
7037 else
7038 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7039
7040 if (dwarf2_read_debug)
7041 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7042
7043 if (per_cu->is_debug_types)
7044 process_full_type_unit (per_cu, item->pretend_language);
7045 else
7046 process_full_comp_unit (per_cu, item->pretend_language);
7047
7048 if (dwarf2_read_debug)
247f5c4f 7049 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7050 }
10b3939b
DJ
7051
7052 item->per_cu->queued = 0;
7053 next_item = item->next;
7054 xfree (item);
7055 }
7056
7057 dwarf2_queue_tail = NULL;
45cfd468
DE
7058
7059 if (dwarf2_read_debug)
7060 {
7061 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7062 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7063 }
10b3939b
DJ
7064}
7065
7066/* Free all allocated queue entries. This function only releases anything if
7067 an error was thrown; if the queue was processed then it would have been
7068 freed as we went along. */
7069
7070static void
7071dwarf2_release_queue (void *dummy)
7072{
7073 struct dwarf2_queue_item *item, *last;
7074
7075 item = dwarf2_queue;
7076 while (item)
7077 {
7078 /* Anything still marked queued is likely to be in an
7079 inconsistent state, so discard it. */
7080 if (item->per_cu->queued)
7081 {
7082 if (item->per_cu->cu != NULL)
dee91e82 7083 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7084 item->per_cu->queued = 0;
7085 }
7086
7087 last = item;
7088 item = item->next;
7089 xfree (last);
7090 }
7091
7092 dwarf2_queue = dwarf2_queue_tail = NULL;
7093}
7094
7095/* Read in full symbols for PST, and anything it depends on. */
7096
c906108c 7097static void
fba45db2 7098psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7099{
10b3939b 7100 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7101 int i;
7102
95554aad
TT
7103 if (pst->readin)
7104 return;
7105
aaa75496 7106 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7107 if (!pst->dependencies[i]->readin
7108 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7109 {
7110 /* Inform about additional files that need to be read in. */
7111 if (info_verbose)
7112 {
a3f17187 7113 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7114 fputs_filtered (" ", gdb_stdout);
7115 wrap_here ("");
7116 fputs_filtered ("and ", gdb_stdout);
7117 wrap_here ("");
7118 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7119 wrap_here (""); /* Flush output. */
aaa75496
JB
7120 gdb_flush (gdb_stdout);
7121 }
7122 psymtab_to_symtab_1 (pst->dependencies[i]);
7123 }
7124
e38df1d0 7125 per_cu = pst->read_symtab_private;
10b3939b
DJ
7126
7127 if (per_cu == NULL)
aaa75496
JB
7128 {
7129 /* It's an include file, no symbols to read for it.
7130 Everything is in the parent symtab. */
7131 pst->readin = 1;
7132 return;
7133 }
c906108c 7134
a0f42c21 7135 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7136}
7137
dee91e82
DE
7138/* Trivial hash function for die_info: the hash value of a DIE
7139 is its offset in .debug_info for this objfile. */
10b3939b 7140
dee91e82
DE
7141static hashval_t
7142die_hash (const void *item)
10b3939b 7143{
dee91e82 7144 const struct die_info *die = item;
6502dd73 7145
dee91e82
DE
7146 return die->offset.sect_off;
7147}
63d06c5c 7148
dee91e82
DE
7149/* Trivial comparison function for die_info structures: two DIEs
7150 are equal if they have the same offset. */
98bfdba5 7151
dee91e82
DE
7152static int
7153die_eq (const void *item_lhs, const void *item_rhs)
7154{
7155 const struct die_info *die_lhs = item_lhs;
7156 const struct die_info *die_rhs = item_rhs;
c906108c 7157
dee91e82
DE
7158 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7159}
c906108c 7160
dee91e82
DE
7161/* die_reader_func for load_full_comp_unit.
7162 This is identical to read_signatured_type_reader,
7163 but is kept separate for now. */
c906108c 7164
dee91e82
DE
7165static void
7166load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7167 const gdb_byte *info_ptr,
dee91e82
DE
7168 struct die_info *comp_unit_die,
7169 int has_children,
7170 void *data)
7171{
7172 struct dwarf2_cu *cu = reader->cu;
95554aad 7173 enum language *language_ptr = data;
6caca83c 7174
dee91e82
DE
7175 gdb_assert (cu->die_hash == NULL);
7176 cu->die_hash =
7177 htab_create_alloc_ex (cu->header.length / 12,
7178 die_hash,
7179 die_eq,
7180 NULL,
7181 &cu->comp_unit_obstack,
7182 hashtab_obstack_allocate,
7183 dummy_obstack_deallocate);
e142c38c 7184
dee91e82
DE
7185 if (has_children)
7186 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7187 &info_ptr, comp_unit_die);
7188 cu->dies = comp_unit_die;
7189 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7190
7191 /* We try not to read any attributes in this function, because not
9cdd5dbd 7192 all CUs needed for references have been loaded yet, and symbol
10b3939b 7193 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7194 or we won't be able to build types correctly.
7195 Similarly, if we do not read the producer, we can not apply
7196 producer-specific interpretation. */
95554aad 7197 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7198}
10b3939b 7199
dee91e82 7200/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7201
dee91e82 7202static void
95554aad
TT
7203load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7204 enum language pretend_language)
dee91e82 7205{
3019eac3 7206 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7207
f4dc4d17
DE
7208 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7209 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7210}
7211
3da10d80
KS
7212/* Add a DIE to the delayed physname list. */
7213
7214static void
7215add_to_method_list (struct type *type, int fnfield_index, int index,
7216 const char *name, struct die_info *die,
7217 struct dwarf2_cu *cu)
7218{
7219 struct delayed_method_info mi;
7220 mi.type = type;
7221 mi.fnfield_index = fnfield_index;
7222 mi.index = index;
7223 mi.name = name;
7224 mi.die = die;
7225 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7226}
7227
7228/* A cleanup for freeing the delayed method list. */
7229
7230static void
7231free_delayed_list (void *ptr)
7232{
7233 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7234 if (cu->method_list != NULL)
7235 {
7236 VEC_free (delayed_method_info, cu->method_list);
7237 cu->method_list = NULL;
7238 }
7239}
7240
7241/* Compute the physnames of any methods on the CU's method list.
7242
7243 The computation of method physnames is delayed in order to avoid the
7244 (bad) condition that one of the method's formal parameters is of an as yet
7245 incomplete type. */
7246
7247static void
7248compute_delayed_physnames (struct dwarf2_cu *cu)
7249{
7250 int i;
7251 struct delayed_method_info *mi;
7252 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7253 {
1d06ead6 7254 const char *physname;
3da10d80
KS
7255 struct fn_fieldlist *fn_flp
7256 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7257 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7258 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7259 }
7260}
7261
a766d390
DE
7262/* Go objects should be embedded in a DW_TAG_module DIE,
7263 and it's not clear if/how imported objects will appear.
7264 To keep Go support simple until that's worked out,
7265 go back through what we've read and create something usable.
7266 We could do this while processing each DIE, and feels kinda cleaner,
7267 but that way is more invasive.
7268 This is to, for example, allow the user to type "p var" or "b main"
7269 without having to specify the package name, and allow lookups
7270 of module.object to work in contexts that use the expression
7271 parser. */
7272
7273static void
7274fixup_go_packaging (struct dwarf2_cu *cu)
7275{
7276 char *package_name = NULL;
7277 struct pending *list;
7278 int i;
7279
7280 for (list = global_symbols; list != NULL; list = list->next)
7281 {
7282 for (i = 0; i < list->nsyms; ++i)
7283 {
7284 struct symbol *sym = list->symbol[i];
7285
7286 if (SYMBOL_LANGUAGE (sym) == language_go
7287 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7288 {
7289 char *this_package_name = go_symbol_package_name (sym);
7290
7291 if (this_package_name == NULL)
7292 continue;
7293 if (package_name == NULL)
7294 package_name = this_package_name;
7295 else
7296 {
7297 if (strcmp (package_name, this_package_name) != 0)
7298 complaint (&symfile_complaints,
7299 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7300 (SYMBOL_SYMTAB (sym)
05cba821 7301 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
4262abfb 7302 : objfile_name (cu->objfile)),
a766d390
DE
7303 this_package_name, package_name);
7304 xfree (this_package_name);
7305 }
7306 }
7307 }
7308 }
7309
7310 if (package_name != NULL)
7311 {
7312 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7313 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7314 package_name,
7315 strlen (package_name));
a766d390 7316 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7317 saved_package_name, objfile);
a766d390
DE
7318 struct symbol *sym;
7319
7320 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7321
e623cf5d 7322 sym = allocate_symbol (objfile);
f85f34ed 7323 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7324 SYMBOL_SET_NAMES (sym, saved_package_name,
7325 strlen (saved_package_name), 0, objfile);
a766d390
DE
7326 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7327 e.g., "main" finds the "main" module and not C's main(). */
7328 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7329 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7330 SYMBOL_TYPE (sym) = type;
7331
7332 add_symbol_to_list (sym, &global_symbols);
7333
7334 xfree (package_name);
7335 }
7336}
7337
95554aad
TT
7338/* Return the symtab for PER_CU. This works properly regardless of
7339 whether we're using the index or psymtabs. */
7340
7341static struct symtab *
7342get_symtab (struct dwarf2_per_cu_data *per_cu)
7343{
7344 return (dwarf2_per_objfile->using_index
7345 ? per_cu->v.quick->symtab
7346 : per_cu->v.psymtab->symtab);
7347}
7348
7349/* A helper function for computing the list of all symbol tables
7350 included by PER_CU. */
7351
7352static void
ec94af83
DE
7353recursively_compute_inclusions (VEC (symtab_ptr) **result,
7354 htab_t all_children, htab_t all_type_symtabs,
f9125b6c
TT
7355 struct dwarf2_per_cu_data *per_cu,
7356 struct symtab *immediate_parent)
95554aad
TT
7357{
7358 void **slot;
7359 int ix;
ec94af83 7360 struct symtab *symtab;
95554aad
TT
7361 struct dwarf2_per_cu_data *iter;
7362
7363 slot = htab_find_slot (all_children, per_cu, INSERT);
7364 if (*slot != NULL)
7365 {
7366 /* This inclusion and its children have been processed. */
7367 return;
7368 }
7369
7370 *slot = per_cu;
7371 /* Only add a CU if it has a symbol table. */
ec94af83
DE
7372 symtab = get_symtab (per_cu);
7373 if (symtab != NULL)
7374 {
7375 /* If this is a type unit only add its symbol table if we haven't
7376 seen it yet (type unit per_cu's can share symtabs). */
7377 if (per_cu->is_debug_types)
7378 {
7379 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7380 if (*slot == NULL)
7381 {
7382 *slot = symtab;
7383 VEC_safe_push (symtab_ptr, *result, symtab);
f9125b6c
TT
7384 if (symtab->user == NULL)
7385 symtab->user = immediate_parent;
ec94af83
DE
7386 }
7387 }
7388 else
f9125b6c
TT
7389 {
7390 VEC_safe_push (symtab_ptr, *result, symtab);
7391 if (symtab->user == NULL)
7392 symtab->user = immediate_parent;
7393 }
ec94af83 7394 }
95554aad
TT
7395
7396 for (ix = 0;
796a7ff8 7397 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7398 ++ix)
ec94af83
DE
7399 {
7400 recursively_compute_inclusions (result, all_children,
f9125b6c 7401 all_type_symtabs, iter, symtab);
ec94af83 7402 }
95554aad
TT
7403}
7404
7405/* Compute the symtab 'includes' fields for the symtab related to
7406 PER_CU. */
7407
7408static void
7409compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7410{
f4dc4d17
DE
7411 gdb_assert (! per_cu->is_debug_types);
7412
796a7ff8 7413 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7414 {
7415 int ix, len;
ec94af83
DE
7416 struct dwarf2_per_cu_data *per_cu_iter;
7417 struct symtab *symtab_iter;
7418 VEC (symtab_ptr) *result_symtabs = NULL;
7419 htab_t all_children, all_type_symtabs;
95554aad
TT
7420 struct symtab *symtab = get_symtab (per_cu);
7421
7422 /* If we don't have a symtab, we can just skip this case. */
7423 if (symtab == NULL)
7424 return;
7425
7426 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7427 NULL, xcalloc, xfree);
ec94af83
DE
7428 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7429 NULL, xcalloc, xfree);
95554aad
TT
7430
7431 for (ix = 0;
796a7ff8 7432 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7433 ix, per_cu_iter);
95554aad 7434 ++ix)
ec94af83
DE
7435 {
7436 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c
TT
7437 all_type_symtabs, per_cu_iter,
7438 symtab);
ec94af83 7439 }
95554aad 7440
ec94af83
DE
7441 /* Now we have a transitive closure of all the included symtabs. */
7442 len = VEC_length (symtab_ptr, result_symtabs);
95554aad
TT
7443 symtab->includes
7444 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7445 (len + 1) * sizeof (struct symtab *));
7446 for (ix = 0;
ec94af83 7447 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
95554aad 7448 ++ix)
ec94af83 7449 symtab->includes[ix] = symtab_iter;
95554aad
TT
7450 symtab->includes[len] = NULL;
7451
ec94af83 7452 VEC_free (symtab_ptr, result_symtabs);
95554aad 7453 htab_delete (all_children);
ec94af83 7454 htab_delete (all_type_symtabs);
95554aad
TT
7455 }
7456}
7457
7458/* Compute the 'includes' field for the symtabs of all the CUs we just
7459 read. */
7460
7461static void
7462process_cu_includes (void)
7463{
7464 int ix;
7465 struct dwarf2_per_cu_data *iter;
7466
7467 for (ix = 0;
7468 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7469 ix, iter);
7470 ++ix)
f4dc4d17
DE
7471 {
7472 if (! iter->is_debug_types)
7473 compute_symtab_includes (iter);
7474 }
95554aad
TT
7475
7476 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7477}
7478
9cdd5dbd 7479/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7480 already been loaded into memory. */
7481
7482static void
95554aad
TT
7483process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7484 enum language pretend_language)
10b3939b 7485{
10b3939b 7486 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7487 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7488 CORE_ADDR lowpc, highpc;
7489 struct symtab *symtab;
3da10d80 7490 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7491 CORE_ADDR baseaddr;
4359dff1 7492 struct block *static_block;
10b3939b
DJ
7493
7494 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7495
10b3939b
DJ
7496 buildsym_init ();
7497 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7498 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7499
7500 cu->list_in_scope = &file_symbols;
c906108c 7501
95554aad
TT
7502 cu->language = pretend_language;
7503 cu->language_defn = language_def (cu->language);
7504
c906108c 7505 /* Do line number decoding in read_file_scope () */
10b3939b 7506 process_die (cu->dies, cu);
c906108c 7507
a766d390
DE
7508 /* For now fudge the Go package. */
7509 if (cu->language == language_go)
7510 fixup_go_packaging (cu);
7511
3da10d80
KS
7512 /* Now that we have processed all the DIEs in the CU, all the types
7513 should be complete, and it should now be safe to compute all of the
7514 physnames. */
7515 compute_delayed_physnames (cu);
7516 do_cleanups (delayed_list_cleanup);
7517
fae299cd
DC
7518 /* Some compilers don't define a DW_AT_high_pc attribute for the
7519 compilation unit. If the DW_AT_high_pc is missing, synthesize
7520 it, by scanning the DIE's below the compilation unit. */
10b3939b 7521 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7522
36586728 7523 static_block
ff546935 7524 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7525
7526 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7527 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7528 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7529 addrmap to help ensure it has an accurate map of pc values belonging to
7530 this comp unit. */
7531 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7532
7533 symtab = end_symtab_from_static_block (static_block, objfile,
7534 SECT_OFF_TEXT (objfile), 0);
c906108c 7535
8be455d7 7536 if (symtab != NULL)
c906108c 7537 {
df15bd07 7538 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7539
8be455d7
JK
7540 /* Set symtab language to language from DW_AT_language. If the
7541 compilation is from a C file generated by language preprocessors, do
7542 not set the language if it was already deduced by start_subfile. */
7543 if (!(cu->language == language_c && symtab->language != language_c))
7544 symtab->language = cu->language;
7545
7546 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7547 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7548 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7549 there were bugs in prologue debug info, fixed later in GCC-4.5
7550 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7551
7552 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7553 needed, it would be wrong due to missing DW_AT_producer there.
7554
7555 Still one can confuse GDB by using non-standard GCC compilation
7556 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7557 */
ab260dad 7558 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7559 symtab->locations_valid = 1;
e0d00bc7
JK
7560
7561 if (gcc_4_minor >= 5)
7562 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7563
7564 symtab->call_site_htab = cu->call_site_htab;
c906108c 7565 }
9291a0cd
TT
7566
7567 if (dwarf2_per_objfile->using_index)
7568 per_cu->v.quick->symtab = symtab;
7569 else
7570 {
7571 struct partial_symtab *pst = per_cu->v.psymtab;
7572 pst->symtab = symtab;
7573 pst->readin = 1;
7574 }
c906108c 7575
95554aad
TT
7576 /* Push it for inclusion processing later. */
7577 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7578
c906108c 7579 do_cleanups (back_to);
f4dc4d17 7580}
45cfd468 7581
f4dc4d17
DE
7582/* Generate full symbol information for type unit PER_CU, whose DIEs have
7583 already been loaded into memory. */
7584
7585static void
7586process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7587 enum language pretend_language)
7588{
7589 struct dwarf2_cu *cu = per_cu->cu;
7590 struct objfile *objfile = per_cu->objfile;
7591 struct symtab *symtab;
7592 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7593 struct signatured_type *sig_type;
7594
7595 gdb_assert (per_cu->is_debug_types);
7596 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7597
7598 buildsym_init ();
7599 back_to = make_cleanup (really_free_pendings, NULL);
7600 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7601
7602 cu->list_in_scope = &file_symbols;
7603
7604 cu->language = pretend_language;
7605 cu->language_defn = language_def (cu->language);
7606
7607 /* The symbol tables are set up in read_type_unit_scope. */
7608 process_die (cu->dies, cu);
7609
7610 /* For now fudge the Go package. */
7611 if (cu->language == language_go)
7612 fixup_go_packaging (cu);
7613
7614 /* Now that we have processed all the DIEs in the CU, all the types
7615 should be complete, and it should now be safe to compute all of the
7616 physnames. */
7617 compute_delayed_physnames (cu);
7618 do_cleanups (delayed_list_cleanup);
7619
7620 /* TUs share symbol tables.
7621 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7622 of it with end_expandable_symtab. Otherwise, complete the addition of
7623 this TU's symbols to the existing symtab. */
0186c6a7 7624 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7625 {
f4dc4d17 7626 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7627 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7628
7629 if (symtab != NULL)
7630 {
7631 /* Set symtab language to language from DW_AT_language. If the
7632 compilation is from a C file generated by language preprocessors,
7633 do not set the language if it was already deduced by
7634 start_subfile. */
7635 if (!(cu->language == language_c && symtab->language != language_c))
7636 symtab->language = cu->language;
7637 }
7638 }
7639 else
7640 {
7641 augment_type_symtab (objfile,
0186c6a7
DE
7642 sig_type->type_unit_group->primary_symtab);
7643 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7644 }
7645
7646 if (dwarf2_per_objfile->using_index)
7647 per_cu->v.quick->symtab = symtab;
7648 else
7649 {
7650 struct partial_symtab *pst = per_cu->v.psymtab;
7651 pst->symtab = symtab;
7652 pst->readin = 1;
45cfd468 7653 }
f4dc4d17
DE
7654
7655 do_cleanups (back_to);
c906108c
SS
7656}
7657
95554aad
TT
7658/* Process an imported unit DIE. */
7659
7660static void
7661process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7662{
7663 struct attribute *attr;
7664
f4dc4d17
DE
7665 /* For now we don't handle imported units in type units. */
7666 if (cu->per_cu->is_debug_types)
7667 {
7668 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7669 " supported in type units [in module %s]"),
4262abfb 7670 objfile_name (cu->objfile));
f4dc4d17
DE
7671 }
7672
95554aad
TT
7673 attr = dwarf2_attr (die, DW_AT_import, cu);
7674 if (attr != NULL)
7675 {
7676 struct dwarf2_per_cu_data *per_cu;
7677 struct symtab *imported_symtab;
7678 sect_offset offset;
36586728 7679 int is_dwz;
95554aad
TT
7680
7681 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7682 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7683 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 7684
69d751e3 7685 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
7686 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7687 load_full_comp_unit (per_cu, cu->language);
7688
796a7ff8 7689 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7690 per_cu);
7691 }
7692}
7693
c906108c
SS
7694/* Process a die and its children. */
7695
7696static void
e7c27a73 7697process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7698{
7699 switch (die->tag)
7700 {
7701 case DW_TAG_padding:
7702 break;
7703 case DW_TAG_compile_unit:
95554aad 7704 case DW_TAG_partial_unit:
e7c27a73 7705 read_file_scope (die, cu);
c906108c 7706 break;
348e048f
DE
7707 case DW_TAG_type_unit:
7708 read_type_unit_scope (die, cu);
7709 break;
c906108c 7710 case DW_TAG_subprogram:
c906108c 7711 case DW_TAG_inlined_subroutine:
edb3359d 7712 read_func_scope (die, cu);
c906108c
SS
7713 break;
7714 case DW_TAG_lexical_block:
14898363
L
7715 case DW_TAG_try_block:
7716 case DW_TAG_catch_block:
e7c27a73 7717 read_lexical_block_scope (die, cu);
c906108c 7718 break;
96408a79
SA
7719 case DW_TAG_GNU_call_site:
7720 read_call_site_scope (die, cu);
7721 break;
c906108c 7722 case DW_TAG_class_type:
680b30c7 7723 case DW_TAG_interface_type:
c906108c
SS
7724 case DW_TAG_structure_type:
7725 case DW_TAG_union_type:
134d01f1 7726 process_structure_scope (die, cu);
c906108c
SS
7727 break;
7728 case DW_TAG_enumeration_type:
134d01f1 7729 process_enumeration_scope (die, cu);
c906108c 7730 break;
134d01f1 7731
f792889a
DJ
7732 /* These dies have a type, but processing them does not create
7733 a symbol or recurse to process the children. Therefore we can
7734 read them on-demand through read_type_die. */
c906108c 7735 case DW_TAG_subroutine_type:
72019c9c 7736 case DW_TAG_set_type:
c906108c 7737 case DW_TAG_array_type:
c906108c 7738 case DW_TAG_pointer_type:
c906108c 7739 case DW_TAG_ptr_to_member_type:
c906108c 7740 case DW_TAG_reference_type:
c906108c 7741 case DW_TAG_string_type:
c906108c 7742 break;
134d01f1 7743
c906108c 7744 case DW_TAG_base_type:
a02abb62 7745 case DW_TAG_subrange_type:
cb249c71 7746 case DW_TAG_typedef:
134d01f1
DJ
7747 /* Add a typedef symbol for the type definition, if it has a
7748 DW_AT_name. */
f792889a 7749 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7750 break;
c906108c 7751 case DW_TAG_common_block:
e7c27a73 7752 read_common_block (die, cu);
c906108c
SS
7753 break;
7754 case DW_TAG_common_inclusion:
7755 break;
d9fa45fe 7756 case DW_TAG_namespace:
4d4ec4e5 7757 cu->processing_has_namespace_info = 1;
e7c27a73 7758 read_namespace (die, cu);
d9fa45fe 7759 break;
5d7cb8df 7760 case DW_TAG_module:
4d4ec4e5 7761 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7762 read_module (die, cu);
7763 break;
d9fa45fe
DC
7764 case DW_TAG_imported_declaration:
7765 case DW_TAG_imported_module:
4d4ec4e5 7766 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7767 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7768 || cu->language != language_fortran))
7769 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7770 dwarf_tag_name (die->tag));
7771 read_import_statement (die, cu);
d9fa45fe 7772 break;
95554aad
TT
7773
7774 case DW_TAG_imported_unit:
7775 process_imported_unit_die (die, cu);
7776 break;
7777
c906108c 7778 default:
e7c27a73 7779 new_symbol (die, NULL, cu);
c906108c
SS
7780 break;
7781 }
7782}
ca69b9e6
DE
7783\f
7784/* DWARF name computation. */
c906108c 7785
94af9270
KS
7786/* A helper function for dwarf2_compute_name which determines whether DIE
7787 needs to have the name of the scope prepended to the name listed in the
7788 die. */
7789
7790static int
7791die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7792{
1c809c68
TT
7793 struct attribute *attr;
7794
94af9270
KS
7795 switch (die->tag)
7796 {
7797 case DW_TAG_namespace:
7798 case DW_TAG_typedef:
7799 case DW_TAG_class_type:
7800 case DW_TAG_interface_type:
7801 case DW_TAG_structure_type:
7802 case DW_TAG_union_type:
7803 case DW_TAG_enumeration_type:
7804 case DW_TAG_enumerator:
7805 case DW_TAG_subprogram:
7806 case DW_TAG_member:
7807 return 1;
7808
7809 case DW_TAG_variable:
c2b0a229 7810 case DW_TAG_constant:
94af9270
KS
7811 /* We only need to prefix "globally" visible variables. These include
7812 any variable marked with DW_AT_external or any variable that
7813 lives in a namespace. [Variables in anonymous namespaces
7814 require prefixing, but they are not DW_AT_external.] */
7815
7816 if (dwarf2_attr (die, DW_AT_specification, cu))
7817 {
7818 struct dwarf2_cu *spec_cu = cu;
9a619af0 7819
94af9270
KS
7820 return die_needs_namespace (die_specification (die, &spec_cu),
7821 spec_cu);
7822 }
7823
1c809c68 7824 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7825 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7826 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7827 return 0;
7828 /* A variable in a lexical block of some kind does not need a
7829 namespace, even though in C++ such variables may be external
7830 and have a mangled name. */
7831 if (die->parent->tag == DW_TAG_lexical_block
7832 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7833 || die->parent->tag == DW_TAG_catch_block
7834 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7835 return 0;
7836 return 1;
94af9270
KS
7837
7838 default:
7839 return 0;
7840 }
7841}
7842
98bfdba5
PA
7843/* Retrieve the last character from a mem_file. */
7844
7845static void
7846do_ui_file_peek_last (void *object, const char *buffer, long length)
7847{
7848 char *last_char_p = (char *) object;
7849
7850 if (length > 0)
7851 *last_char_p = buffer[length - 1];
7852}
7853
94af9270 7854/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7855 compute the physname for the object, which include a method's:
7856 - formal parameters (C++/Java),
7857 - receiver type (Go),
7858 - return type (Java).
7859
7860 The term "physname" is a bit confusing.
7861 For C++, for example, it is the demangled name.
7862 For Go, for example, it's the mangled name.
94af9270 7863
af6b7be1
JB
7864 For Ada, return the DIE's linkage name rather than the fully qualified
7865 name. PHYSNAME is ignored..
7866
94af9270
KS
7867 The result is allocated on the objfile_obstack and canonicalized. */
7868
7869static const char *
15d034d0
TT
7870dwarf2_compute_name (const char *name,
7871 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7872 int physname)
7873{
bb5ed363
DE
7874 struct objfile *objfile = cu->objfile;
7875
94af9270
KS
7876 if (name == NULL)
7877 name = dwarf2_name (die, cu);
7878
f55ee35c
JK
7879 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7880 compute it by typename_concat inside GDB. */
7881 if (cu->language == language_ada
7882 || (cu->language == language_fortran && physname))
7883 {
7884 /* For Ada unit, we prefer the linkage name over the name, as
7885 the former contains the exported name, which the user expects
7886 to be able to reference. Ideally, we want the user to be able
7887 to reference this entity using either natural or linkage name,
7888 but we haven't started looking at this enhancement yet. */
7889 struct attribute *attr;
7890
7891 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7892 if (attr == NULL)
7893 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7894 if (attr && DW_STRING (attr))
7895 return DW_STRING (attr);
7896 }
7897
94af9270
KS
7898 /* These are the only languages we know how to qualify names in. */
7899 if (name != NULL
f55ee35c
JK
7900 && (cu->language == language_cplus || cu->language == language_java
7901 || cu->language == language_fortran))
94af9270
KS
7902 {
7903 if (die_needs_namespace (die, cu))
7904 {
7905 long length;
0d5cff50 7906 const char *prefix;
94af9270
KS
7907 struct ui_file *buf;
7908
7909 prefix = determine_prefix (die, cu);
7910 buf = mem_fileopen ();
7911 if (*prefix != '\0')
7912 {
f55ee35c
JK
7913 char *prefixed_name = typename_concat (NULL, prefix, name,
7914 physname, cu);
9a619af0 7915
94af9270
KS
7916 fputs_unfiltered (prefixed_name, buf);
7917 xfree (prefixed_name);
7918 }
7919 else
62d5b8da 7920 fputs_unfiltered (name, buf);
94af9270 7921
98bfdba5
PA
7922 /* Template parameters may be specified in the DIE's DW_AT_name, or
7923 as children with DW_TAG_template_type_param or
7924 DW_TAG_value_type_param. If the latter, add them to the name
7925 here. If the name already has template parameters, then
7926 skip this step; some versions of GCC emit both, and
7927 it is more efficient to use the pre-computed name.
7928
7929 Something to keep in mind about this process: it is very
7930 unlikely, or in some cases downright impossible, to produce
7931 something that will match the mangled name of a function.
7932 If the definition of the function has the same debug info,
7933 we should be able to match up with it anyway. But fallbacks
7934 using the minimal symbol, for instance to find a method
7935 implemented in a stripped copy of libstdc++, will not work.
7936 If we do not have debug info for the definition, we will have to
7937 match them up some other way.
7938
7939 When we do name matching there is a related problem with function
7940 templates; two instantiated function templates are allowed to
7941 differ only by their return types, which we do not add here. */
7942
7943 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7944 {
7945 struct attribute *attr;
7946 struct die_info *child;
7947 int first = 1;
7948
7949 die->building_fullname = 1;
7950
7951 for (child = die->child; child != NULL; child = child->sibling)
7952 {
7953 struct type *type;
12df843f 7954 LONGEST value;
d521ce57 7955 const gdb_byte *bytes;
98bfdba5
PA
7956 struct dwarf2_locexpr_baton *baton;
7957 struct value *v;
7958
7959 if (child->tag != DW_TAG_template_type_param
7960 && child->tag != DW_TAG_template_value_param)
7961 continue;
7962
7963 if (first)
7964 {
7965 fputs_unfiltered ("<", buf);
7966 first = 0;
7967 }
7968 else
7969 fputs_unfiltered (", ", buf);
7970
7971 attr = dwarf2_attr (child, DW_AT_type, cu);
7972 if (attr == NULL)
7973 {
7974 complaint (&symfile_complaints,
7975 _("template parameter missing DW_AT_type"));
7976 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7977 continue;
7978 }
7979 type = die_type (child, cu);
7980
7981 if (child->tag == DW_TAG_template_type_param)
7982 {
79d43c61 7983 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7984 continue;
7985 }
7986
7987 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7988 if (attr == NULL)
7989 {
7990 complaint (&symfile_complaints,
3e43a32a
MS
7991 _("template parameter missing "
7992 "DW_AT_const_value"));
98bfdba5
PA
7993 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7994 continue;
7995 }
7996
7997 dwarf2_const_value_attr (attr, type, name,
7998 &cu->comp_unit_obstack, cu,
7999 &value, &bytes, &baton);
8000
8001 if (TYPE_NOSIGN (type))
8002 /* GDB prints characters as NUMBER 'CHAR'. If that's
8003 changed, this can use value_print instead. */
8004 c_printchar (value, type, buf);
8005 else
8006 {
8007 struct value_print_options opts;
8008
8009 if (baton != NULL)
8010 v = dwarf2_evaluate_loc_desc (type, NULL,
8011 baton->data,
8012 baton->size,
8013 baton->per_cu);
8014 else if (bytes != NULL)
8015 {
8016 v = allocate_value (type);
8017 memcpy (value_contents_writeable (v), bytes,
8018 TYPE_LENGTH (type));
8019 }
8020 else
8021 v = value_from_longest (type, value);
8022
3e43a32a
MS
8023 /* Specify decimal so that we do not depend on
8024 the radix. */
98bfdba5
PA
8025 get_formatted_print_options (&opts, 'd');
8026 opts.raw = 1;
8027 value_print (v, buf, &opts);
8028 release_value (v);
8029 value_free (v);
8030 }
8031 }
8032
8033 die->building_fullname = 0;
8034
8035 if (!first)
8036 {
8037 /* Close the argument list, with a space if necessary
8038 (nested templates). */
8039 char last_char = '\0';
8040 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8041 if (last_char == '>')
8042 fputs_unfiltered (" >", buf);
8043 else
8044 fputs_unfiltered (">", buf);
8045 }
8046 }
8047
94af9270
KS
8048 /* For Java and C++ methods, append formal parameter type
8049 information, if PHYSNAME. */
6e70227d 8050
94af9270
KS
8051 if (physname && die->tag == DW_TAG_subprogram
8052 && (cu->language == language_cplus
8053 || cu->language == language_java))
8054 {
8055 struct type *type = read_type_die (die, cu);
8056
79d43c61
TT
8057 c_type_print_args (type, buf, 1, cu->language,
8058 &type_print_raw_options);
94af9270
KS
8059
8060 if (cu->language == language_java)
8061 {
8062 /* For java, we must append the return type to method
0963b4bd 8063 names. */
94af9270
KS
8064 if (die->tag == DW_TAG_subprogram)
8065 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8066 0, 0, &type_print_raw_options);
94af9270
KS
8067 }
8068 else if (cu->language == language_cplus)
8069 {
60430eff
DJ
8070 /* Assume that an artificial first parameter is
8071 "this", but do not crash if it is not. RealView
8072 marks unnamed (and thus unused) parameters as
8073 artificial; there is no way to differentiate
8074 the two cases. */
94af9270
KS
8075 if (TYPE_NFIELDS (type) > 0
8076 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8077 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8078 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8079 0))))
94af9270
KS
8080 fputs_unfiltered (" const", buf);
8081 }
8082 }
8083
bb5ed363 8084 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8085 &length);
8086 ui_file_delete (buf);
8087
8088 if (cu->language == language_cplus)
8089 {
15d034d0 8090 const char *cname
94af9270 8091 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8092 &objfile->objfile_obstack);
9a619af0 8093
94af9270
KS
8094 if (cname != NULL)
8095 name = cname;
8096 }
8097 }
8098 }
8099
8100 return name;
8101}
8102
0114d602
DJ
8103/* Return the fully qualified name of DIE, based on its DW_AT_name.
8104 If scope qualifiers are appropriate they will be added. The result
8105 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8106 not have a name. NAME may either be from a previous call to
8107 dwarf2_name or NULL.
8108
0963b4bd 8109 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8110
8111static const char *
15d034d0 8112dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8113{
94af9270
KS
8114 return dwarf2_compute_name (name, die, cu, 0);
8115}
0114d602 8116
94af9270
KS
8117/* Construct a physname for the given DIE in CU. NAME may either be
8118 from a previous call to dwarf2_name or NULL. The result will be
8119 allocated on the objfile_objstack or NULL if the DIE does not have a
8120 name.
0114d602 8121
94af9270 8122 The output string will be canonicalized (if C++/Java). */
0114d602 8123
94af9270 8124static const char *
15d034d0 8125dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8126{
bb5ed363 8127 struct objfile *objfile = cu->objfile;
900e11f9
JK
8128 struct attribute *attr;
8129 const char *retval, *mangled = NULL, *canon = NULL;
8130 struct cleanup *back_to;
8131 int need_copy = 1;
8132
8133 /* In this case dwarf2_compute_name is just a shortcut not building anything
8134 on its own. */
8135 if (!die_needs_namespace (die, cu))
8136 return dwarf2_compute_name (name, die, cu, 1);
8137
8138 back_to = make_cleanup (null_cleanup, NULL);
8139
8140 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8141 if (!attr)
8142 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8143
8144 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8145 has computed. */
8146 if (attr && DW_STRING (attr))
8147 {
8148 char *demangled;
8149
8150 mangled = DW_STRING (attr);
8151
8152 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8153 type. It is easier for GDB users to search for such functions as
8154 `name(params)' than `long name(params)'. In such case the minimal
8155 symbol names do not match the full symbol names but for template
8156 functions there is never a need to look up their definition from their
8157 declaration so the only disadvantage remains the minimal symbol
8158 variant `long name(params)' does not have the proper inferior type.
8159 */
8160
a766d390
DE
8161 if (cu->language == language_go)
8162 {
8163 /* This is a lie, but we already lie to the caller new_symbol_full.
8164 new_symbol_full assumes we return the mangled name.
8165 This just undoes that lie until things are cleaned up. */
8166 demangled = NULL;
8167 }
8168 else
8169 {
8de20a37
TT
8170 demangled = gdb_demangle (mangled,
8171 (DMGL_PARAMS | DMGL_ANSI
8172 | (cu->language == language_java
8173 ? DMGL_JAVA | DMGL_RET_POSTFIX
8174 : DMGL_RET_DROP)));
a766d390 8175 }
900e11f9
JK
8176 if (demangled)
8177 {
8178 make_cleanup (xfree, demangled);
8179 canon = demangled;
8180 }
8181 else
8182 {
8183 canon = mangled;
8184 need_copy = 0;
8185 }
8186 }
8187
8188 if (canon == NULL || check_physname)
8189 {
8190 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8191
8192 if (canon != NULL && strcmp (physname, canon) != 0)
8193 {
8194 /* It may not mean a bug in GDB. The compiler could also
8195 compute DW_AT_linkage_name incorrectly. But in such case
8196 GDB would need to be bug-to-bug compatible. */
8197
8198 complaint (&symfile_complaints,
8199 _("Computed physname <%s> does not match demangled <%s> "
8200 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
4262abfb
JK
8201 physname, canon, mangled, die->offset.sect_off,
8202 objfile_name (objfile));
900e11f9
JK
8203
8204 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8205 is available here - over computed PHYSNAME. It is safer
8206 against both buggy GDB and buggy compilers. */
8207
8208 retval = canon;
8209 }
8210 else
8211 {
8212 retval = physname;
8213 need_copy = 0;
8214 }
8215 }
8216 else
8217 retval = canon;
8218
8219 if (need_copy)
10f0c4bb 8220 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8221
8222 do_cleanups (back_to);
8223 return retval;
0114d602
DJ
8224}
8225
27aa8d6a
SW
8226/* Read the import statement specified by the given die and record it. */
8227
8228static void
8229read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8230{
bb5ed363 8231 struct objfile *objfile = cu->objfile;
27aa8d6a 8232 struct attribute *import_attr;
32019081 8233 struct die_info *imported_die, *child_die;
de4affc9 8234 struct dwarf2_cu *imported_cu;
27aa8d6a 8235 const char *imported_name;
794684b6 8236 const char *imported_name_prefix;
13387711
SW
8237 const char *canonical_name;
8238 const char *import_alias;
8239 const char *imported_declaration = NULL;
794684b6 8240 const char *import_prefix;
32019081
JK
8241 VEC (const_char_ptr) *excludes = NULL;
8242 struct cleanup *cleanups;
13387711 8243
27aa8d6a
SW
8244 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8245 if (import_attr == NULL)
8246 {
8247 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8248 dwarf_tag_name (die->tag));
8249 return;
8250 }
8251
de4affc9
CC
8252 imported_cu = cu;
8253 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8254 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8255 if (imported_name == NULL)
8256 {
8257 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8258
8259 The import in the following code:
8260 namespace A
8261 {
8262 typedef int B;
8263 }
8264
8265 int main ()
8266 {
8267 using A::B;
8268 B b;
8269 return b;
8270 }
8271
8272 ...
8273 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8274 <52> DW_AT_decl_file : 1
8275 <53> DW_AT_decl_line : 6
8276 <54> DW_AT_import : <0x75>
8277 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8278 <59> DW_AT_name : B
8279 <5b> DW_AT_decl_file : 1
8280 <5c> DW_AT_decl_line : 2
8281 <5d> DW_AT_type : <0x6e>
8282 ...
8283 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8284 <76> DW_AT_byte_size : 4
8285 <77> DW_AT_encoding : 5 (signed)
8286
8287 imports the wrong die ( 0x75 instead of 0x58 ).
8288 This case will be ignored until the gcc bug is fixed. */
8289 return;
8290 }
8291
82856980
SW
8292 /* Figure out the local name after import. */
8293 import_alias = dwarf2_name (die, cu);
27aa8d6a 8294
794684b6
SW
8295 /* Figure out where the statement is being imported to. */
8296 import_prefix = determine_prefix (die, cu);
8297
8298 /* Figure out what the scope of the imported die is and prepend it
8299 to the name of the imported die. */
de4affc9 8300 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8301
f55ee35c
JK
8302 if (imported_die->tag != DW_TAG_namespace
8303 && imported_die->tag != DW_TAG_module)
794684b6 8304 {
13387711
SW
8305 imported_declaration = imported_name;
8306 canonical_name = imported_name_prefix;
794684b6 8307 }
13387711 8308 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8309 canonical_name = obconcat (&objfile->objfile_obstack,
8310 imported_name_prefix, "::", imported_name,
8311 (char *) NULL);
13387711
SW
8312 else
8313 canonical_name = imported_name;
794684b6 8314
32019081
JK
8315 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8316
8317 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8318 for (child_die = die->child; child_die && child_die->tag;
8319 child_die = sibling_die (child_die))
8320 {
8321 /* DWARF-4: A Fortran use statement with a “rename list” may be
8322 represented by an imported module entry with an import attribute
8323 referring to the module and owned entries corresponding to those
8324 entities that are renamed as part of being imported. */
8325
8326 if (child_die->tag != DW_TAG_imported_declaration)
8327 {
8328 complaint (&symfile_complaints,
8329 _("child DW_TAG_imported_declaration expected "
8330 "- DIE at 0x%x [in module %s]"),
4262abfb 8331 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
8332 continue;
8333 }
8334
8335 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8336 if (import_attr == NULL)
8337 {
8338 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8339 dwarf_tag_name (child_die->tag));
8340 continue;
8341 }
8342
8343 imported_cu = cu;
8344 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8345 &imported_cu);
8346 imported_name = dwarf2_name (imported_die, imported_cu);
8347 if (imported_name == NULL)
8348 {
8349 complaint (&symfile_complaints,
8350 _("child DW_TAG_imported_declaration has unknown "
8351 "imported name - DIE at 0x%x [in module %s]"),
4262abfb 8352 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
8353 continue;
8354 }
8355
8356 VEC_safe_push (const_char_ptr, excludes, imported_name);
8357
8358 process_die (child_die, cu);
8359 }
8360
c0cc3a76
SW
8361 cp_add_using_directive (import_prefix,
8362 canonical_name,
8363 import_alias,
13387711 8364 imported_declaration,
32019081 8365 excludes,
12aaed36 8366 0,
bb5ed363 8367 &objfile->objfile_obstack);
32019081
JK
8368
8369 do_cleanups (cleanups);
27aa8d6a
SW
8370}
8371
f4dc4d17 8372/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8373
cb1df416
DJ
8374static void
8375free_cu_line_header (void *arg)
8376{
8377 struct dwarf2_cu *cu = arg;
8378
8379 free_line_header (cu->line_header);
8380 cu->line_header = NULL;
8381}
8382
1b80a9fa
JK
8383/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8384 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8385 this, it was first present in GCC release 4.3.0. */
8386
8387static int
8388producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8389{
8390 if (!cu->checked_producer)
8391 check_producer (cu);
8392
8393 return cu->producer_is_gcc_lt_4_3;
8394}
8395
9291a0cd
TT
8396static void
8397find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8398 const char **name, const char **comp_dir)
9291a0cd
TT
8399{
8400 struct attribute *attr;
8401
8402 *name = NULL;
8403 *comp_dir = NULL;
8404
8405 /* Find the filename. Do not use dwarf2_name here, since the filename
8406 is not a source language identifier. */
8407 attr = dwarf2_attr (die, DW_AT_name, cu);
8408 if (attr)
8409 {
8410 *name = DW_STRING (attr);
8411 }
8412
8413 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8414 if (attr)
8415 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8416 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8417 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8418 {
15d034d0
TT
8419 char *d = ldirname (*name);
8420
8421 *comp_dir = d;
8422 if (d != NULL)
8423 make_cleanup (xfree, d);
9291a0cd
TT
8424 }
8425 if (*comp_dir != NULL)
8426 {
8427 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8428 directory, get rid of it. */
8429 char *cp = strchr (*comp_dir, ':');
8430
8431 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8432 *comp_dir = cp + 1;
8433 }
8434
8435 if (*name == NULL)
8436 *name = "<unknown>";
8437}
8438
f4dc4d17
DE
8439/* Handle DW_AT_stmt_list for a compilation unit.
8440 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8441 COMP_DIR is the compilation directory.
8442 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8443
8444static void
8445handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8446 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8447{
8448 struct attribute *attr;
2ab95328 8449
f4dc4d17
DE
8450 gdb_assert (! cu->per_cu->is_debug_types);
8451
2ab95328
TT
8452 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8453 if (attr)
8454 {
8455 unsigned int line_offset = DW_UNSND (attr);
8456 struct line_header *line_header
3019eac3 8457 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8458
8459 if (line_header)
dee91e82
DE
8460 {
8461 cu->line_header = line_header;
8462 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8463 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8464 }
2ab95328
TT
8465 }
8466}
8467
95554aad 8468/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8469
c906108c 8470static void
e7c27a73 8471read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8472{
dee91e82 8473 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8474 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8475 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8476 CORE_ADDR highpc = ((CORE_ADDR) 0);
8477 struct attribute *attr;
15d034d0
TT
8478 const char *name = NULL;
8479 const char *comp_dir = NULL;
c906108c
SS
8480 struct die_info *child_die;
8481 bfd *abfd = objfile->obfd;
e142c38c 8482 CORE_ADDR baseaddr;
6e70227d 8483
e142c38c 8484 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8485
fae299cd 8486 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8487
8488 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8489 from finish_block. */
2acceee2 8490 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8491 lowpc = highpc;
8492 lowpc += baseaddr;
8493 highpc += baseaddr;
8494
9291a0cd 8495 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8496
95554aad 8497 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8498
f4b8a18d
KW
8499 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8500 standardised yet. As a workaround for the language detection we fall
8501 back to the DW_AT_producer string. */
8502 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8503 cu->language = language_opencl;
8504
3019eac3
DE
8505 /* Similar hack for Go. */
8506 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8507 set_cu_language (DW_LANG_Go, cu);
8508
f4dc4d17 8509 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8510
8511 /* Decode line number information if present. We do this before
8512 processing child DIEs, so that the line header table is available
8513 for DW_AT_decl_file. */
f4dc4d17 8514 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8515
8516 /* Process all dies in compilation unit. */
8517 if (die->child != NULL)
8518 {
8519 child_die = die->child;
8520 while (child_die && child_die->tag)
8521 {
8522 process_die (child_die, cu);
8523 child_die = sibling_die (child_die);
8524 }
8525 }
8526
8527 /* Decode macro information, if present. Dwarf 2 macro information
8528 refers to information in the line number info statement program
8529 header, so we can only read it if we've read the header
8530 successfully. */
8531 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8532 if (attr && cu->line_header)
8533 {
8534 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8535 complaint (&symfile_complaints,
8536 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8537
09262596 8538 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8539 }
8540 else
8541 {
8542 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8543 if (attr && cu->line_header)
8544 {
8545 unsigned int macro_offset = DW_UNSND (attr);
8546
09262596 8547 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8548 }
8549 }
8550
8551 do_cleanups (back_to);
8552}
8553
f4dc4d17
DE
8554/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8555 Create the set of symtabs used by this TU, or if this TU is sharing
8556 symtabs with another TU and the symtabs have already been created
8557 then restore those symtabs in the line header.
8558 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8559
8560static void
f4dc4d17 8561setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8562{
f4dc4d17
DE
8563 struct objfile *objfile = dwarf2_per_objfile->objfile;
8564 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8565 struct type_unit_group *tu_group;
8566 int first_time;
8567 struct line_header *lh;
3019eac3 8568 struct attribute *attr;
f4dc4d17 8569 unsigned int i, line_offset;
0186c6a7 8570 struct signatured_type *sig_type;
3019eac3 8571
f4dc4d17 8572 gdb_assert (per_cu->is_debug_types);
0186c6a7 8573 sig_type = (struct signatured_type *) per_cu;
3019eac3 8574
f4dc4d17 8575 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8576
f4dc4d17 8577 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8578 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8579 if (sig_type->type_unit_group == NULL)
8580 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8581 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8582
8583 /* If we've already processed this stmt_list there's no real need to
8584 do it again, we could fake it and just recreate the part we need
8585 (file name,index -> symtab mapping). If data shows this optimization
8586 is useful we can do it then. */
8587 first_time = tu_group->primary_symtab == NULL;
8588
8589 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8590 debug info. */
8591 lh = NULL;
8592 if (attr != NULL)
3019eac3 8593 {
f4dc4d17
DE
8594 line_offset = DW_UNSND (attr);
8595 lh = dwarf_decode_line_header (line_offset, cu);
8596 }
8597 if (lh == NULL)
8598 {
8599 if (first_time)
8600 dwarf2_start_symtab (cu, "", NULL, 0);
8601 else
8602 {
8603 gdb_assert (tu_group->symtabs == NULL);
8604 restart_symtab (0);
8605 }
8606 /* Note: The primary symtab will get allocated at the end. */
8607 return;
3019eac3
DE
8608 }
8609
f4dc4d17
DE
8610 cu->line_header = lh;
8611 make_cleanup (free_cu_line_header, cu);
3019eac3 8612
f4dc4d17
DE
8613 if (first_time)
8614 {
8615 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8616
f4dc4d17
DE
8617 tu_group->num_symtabs = lh->num_file_names;
8618 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8619
f4dc4d17
DE
8620 for (i = 0; i < lh->num_file_names; ++i)
8621 {
d521ce57 8622 const char *dir = NULL;
f4dc4d17 8623 struct file_entry *fe = &lh->file_names[i];
3019eac3 8624
f4dc4d17
DE
8625 if (fe->dir_index)
8626 dir = lh->include_dirs[fe->dir_index - 1];
8627 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8628
f4dc4d17
DE
8629 /* Note: We don't have to watch for the main subfile here, type units
8630 don't have DW_AT_name. */
3019eac3 8631
f4dc4d17
DE
8632 if (current_subfile->symtab == NULL)
8633 {
8634 /* NOTE: start_subfile will recognize when it's been passed
8635 a file it has already seen. So we can't assume there's a
8636 simple mapping from lh->file_names to subfiles,
8637 lh->file_names may contain dups. */
8638 current_subfile->symtab = allocate_symtab (current_subfile->name,
8639 objfile);
8640 }
8641
8642 fe->symtab = current_subfile->symtab;
8643 tu_group->symtabs[i] = fe->symtab;
8644 }
8645 }
8646 else
3019eac3 8647 {
f4dc4d17
DE
8648 restart_symtab (0);
8649
8650 for (i = 0; i < lh->num_file_names; ++i)
8651 {
8652 struct file_entry *fe = &lh->file_names[i];
8653
8654 fe->symtab = tu_group->symtabs[i];
8655 }
3019eac3
DE
8656 }
8657
f4dc4d17
DE
8658 /* The main symtab is allocated last. Type units don't have DW_AT_name
8659 so they don't have a "real" (so to speak) symtab anyway.
8660 There is later code that will assign the main symtab to all symbols
8661 that don't have one. We need to handle the case of a symbol with a
8662 missing symtab (DW_AT_decl_file) anyway. */
8663}
3019eac3 8664
f4dc4d17
DE
8665/* Process DW_TAG_type_unit.
8666 For TUs we want to skip the first top level sibling if it's not the
8667 actual type being defined by this TU. In this case the first top
8668 level sibling is there to provide context only. */
3019eac3 8669
f4dc4d17
DE
8670static void
8671read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8672{
8673 struct die_info *child_die;
3019eac3 8674
f4dc4d17
DE
8675 prepare_one_comp_unit (cu, die, language_minimal);
8676
8677 /* Initialize (or reinitialize) the machinery for building symtabs.
8678 We do this before processing child DIEs, so that the line header table
8679 is available for DW_AT_decl_file. */
8680 setup_type_unit_groups (die, cu);
8681
8682 if (die->child != NULL)
8683 {
8684 child_die = die->child;
8685 while (child_die && child_die->tag)
8686 {
8687 process_die (child_die, cu);
8688 child_die = sibling_die (child_die);
8689 }
8690 }
3019eac3
DE
8691}
8692\f
80626a55
DE
8693/* DWO/DWP files.
8694
8695 http://gcc.gnu.org/wiki/DebugFission
8696 http://gcc.gnu.org/wiki/DebugFissionDWP
8697
8698 To simplify handling of both DWO files ("object" files with the DWARF info)
8699 and DWP files (a file with the DWOs packaged up into one file), we treat
8700 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8701
8702static hashval_t
8703hash_dwo_file (const void *item)
8704{
8705 const struct dwo_file *dwo_file = item;
a2ce51a0 8706 hashval_t hash;
3019eac3 8707
a2ce51a0
DE
8708 hash = htab_hash_string (dwo_file->dwo_name);
8709 if (dwo_file->comp_dir != NULL)
8710 hash += htab_hash_string (dwo_file->comp_dir);
8711 return hash;
3019eac3
DE
8712}
8713
8714static int
8715eq_dwo_file (const void *item_lhs, const void *item_rhs)
8716{
8717 const struct dwo_file *lhs = item_lhs;
8718 const struct dwo_file *rhs = item_rhs;
8719
a2ce51a0
DE
8720 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8721 return 0;
8722 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8723 return lhs->comp_dir == rhs->comp_dir;
8724 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8725}
8726
8727/* Allocate a hash table for DWO files. */
8728
8729static htab_t
8730allocate_dwo_file_hash_table (void)
8731{
8732 struct objfile *objfile = dwarf2_per_objfile->objfile;
8733
8734 return htab_create_alloc_ex (41,
8735 hash_dwo_file,
8736 eq_dwo_file,
8737 NULL,
8738 &objfile->objfile_obstack,
8739 hashtab_obstack_allocate,
8740 dummy_obstack_deallocate);
8741}
8742
80626a55
DE
8743/* Lookup DWO file DWO_NAME. */
8744
8745static void **
0ac5b59e 8746lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8747{
8748 struct dwo_file find_entry;
8749 void **slot;
8750
8751 if (dwarf2_per_objfile->dwo_files == NULL)
8752 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8753
8754 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8755 find_entry.dwo_name = dwo_name;
8756 find_entry.comp_dir = comp_dir;
80626a55
DE
8757 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8758
8759 return slot;
8760}
8761
3019eac3
DE
8762static hashval_t
8763hash_dwo_unit (const void *item)
8764{
8765 const struct dwo_unit *dwo_unit = item;
8766
8767 /* This drops the top 32 bits of the id, but is ok for a hash. */
8768 return dwo_unit->signature;
8769}
8770
8771static int
8772eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8773{
8774 const struct dwo_unit *lhs = item_lhs;
8775 const struct dwo_unit *rhs = item_rhs;
8776
8777 /* The signature is assumed to be unique within the DWO file.
8778 So while object file CU dwo_id's always have the value zero,
8779 that's OK, assuming each object file DWO file has only one CU,
8780 and that's the rule for now. */
8781 return lhs->signature == rhs->signature;
8782}
8783
8784/* Allocate a hash table for DWO CUs,TUs.
8785 There is one of these tables for each of CUs,TUs for each DWO file. */
8786
8787static htab_t
8788allocate_dwo_unit_table (struct objfile *objfile)
8789{
8790 /* Start out with a pretty small number.
8791 Generally DWO files contain only one CU and maybe some TUs. */
8792 return htab_create_alloc_ex (3,
8793 hash_dwo_unit,
8794 eq_dwo_unit,
8795 NULL,
8796 &objfile->objfile_obstack,
8797 hashtab_obstack_allocate,
8798 dummy_obstack_deallocate);
8799}
8800
80626a55 8801/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8802
19c3d4c9 8803struct create_dwo_cu_data
3019eac3
DE
8804{
8805 struct dwo_file *dwo_file;
19c3d4c9 8806 struct dwo_unit dwo_unit;
3019eac3
DE
8807};
8808
19c3d4c9 8809/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8810
8811static void
19c3d4c9
DE
8812create_dwo_cu_reader (const struct die_reader_specs *reader,
8813 const gdb_byte *info_ptr,
8814 struct die_info *comp_unit_die,
8815 int has_children,
8816 void *datap)
3019eac3
DE
8817{
8818 struct dwarf2_cu *cu = reader->cu;
8819 struct objfile *objfile = dwarf2_per_objfile->objfile;
8820 sect_offset offset = cu->per_cu->offset;
8a0459fd 8821 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8822 struct create_dwo_cu_data *data = datap;
3019eac3 8823 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8824 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8825 struct attribute *attr;
3019eac3
DE
8826
8827 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8828 if (attr == NULL)
8829 {
19c3d4c9
DE
8830 complaint (&symfile_complaints,
8831 _("Dwarf Error: debug entry at offset 0x%x is missing"
8832 " its dwo_id [in module %s]"),
8833 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8834 return;
8835 }
8836
3019eac3
DE
8837 dwo_unit->dwo_file = dwo_file;
8838 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8839 dwo_unit->section = section;
3019eac3
DE
8840 dwo_unit->offset = offset;
8841 dwo_unit->length = cu->per_cu->length;
8842
09406207 8843 if (dwarf2_read_debug)
4031ecc5
DE
8844 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8845 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8846}
8847
19c3d4c9
DE
8848/* Create the dwo_unit for the lone CU in DWO_FILE.
8849 Note: This function processes DWO files only, not DWP files. */
3019eac3 8850
19c3d4c9
DE
8851static struct dwo_unit *
8852create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8853{
8854 struct objfile *objfile = dwarf2_per_objfile->objfile;
8855 struct dwarf2_section_info *section = &dwo_file->sections.info;
8856 bfd *abfd;
8857 htab_t cu_htab;
d521ce57 8858 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8859 struct create_dwo_cu_data create_dwo_cu_data;
8860 struct dwo_unit *dwo_unit;
3019eac3
DE
8861
8862 dwarf2_read_section (objfile, section);
8863 info_ptr = section->buffer;
8864
8865 if (info_ptr == NULL)
8866 return NULL;
8867
8868 /* We can't set abfd until now because the section may be empty or
8869 not present, in which case section->asection will be NULL. */
8870 abfd = section->asection->owner;
8871
09406207 8872 if (dwarf2_read_debug)
19c3d4c9
DE
8873 {
8874 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8875 bfd_section_name (abfd, section->asection),
8876 bfd_get_filename (abfd));
8877 }
3019eac3 8878
19c3d4c9
DE
8879 create_dwo_cu_data.dwo_file = dwo_file;
8880 dwo_unit = NULL;
3019eac3
DE
8881
8882 end_ptr = info_ptr + section->size;
8883 while (info_ptr < end_ptr)
8884 {
8885 struct dwarf2_per_cu_data per_cu;
8886
19c3d4c9
DE
8887 memset (&create_dwo_cu_data.dwo_unit, 0,
8888 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8889 memset (&per_cu, 0, sizeof (per_cu));
8890 per_cu.objfile = objfile;
8891 per_cu.is_debug_types = 0;
8892 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8893 per_cu.section = section;
3019eac3
DE
8894
8895 init_cutu_and_read_dies_no_follow (&per_cu,
8896 &dwo_file->sections.abbrev,
8897 dwo_file,
19c3d4c9
DE
8898 create_dwo_cu_reader,
8899 &create_dwo_cu_data);
8900
8901 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8902 {
8903 /* If we've already found one, complain. We only support one
8904 because having more than one requires hacking the dwo_name of
8905 each to match, which is highly unlikely to happen. */
8906 if (dwo_unit != NULL)
8907 {
8908 complaint (&symfile_complaints,
8909 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 8910 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
8911 break;
8912 }
8913
8914 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8915 *dwo_unit = create_dwo_cu_data.dwo_unit;
8916 }
3019eac3
DE
8917
8918 info_ptr += per_cu.length;
8919 }
8920
19c3d4c9 8921 return dwo_unit;
3019eac3
DE
8922}
8923
80626a55
DE
8924/* DWP file .debug_{cu,tu}_index section format:
8925 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8926
d2415c6c
DE
8927 DWP Version 1:
8928
80626a55
DE
8929 Both index sections have the same format, and serve to map a 64-bit
8930 signature to a set of section numbers. Each section begins with a header,
8931 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8932 indexes, and a pool of 32-bit section numbers. The index sections will be
8933 aligned at 8-byte boundaries in the file.
8934
d2415c6c
DE
8935 The index section header consists of:
8936
8937 V, 32 bit version number
8938 -, 32 bits unused
8939 N, 32 bit number of compilation units or type units in the index
8940 M, 32 bit number of slots in the hash table
80626a55 8941
d2415c6c 8942 Numbers are recorded using the byte order of the application binary.
80626a55 8943
d2415c6c 8944 We assume that N and M will not exceed 2^32 - 1.
80626a55 8945
d2415c6c 8946 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
80626a55 8947
d2415c6c
DE
8948 The hash table begins at offset 16 in the section, and consists of an array
8949 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8950 order of the application binary). Unused slots in the hash table are 0.
8951 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 8952
d2415c6c
DE
8953 The parallel table begins immediately after the hash table
8954 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8955 array of 32-bit indexes (using the byte order of the application binary),
8956 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8957 table contains a 32-bit index into the pool of section numbers. For unused
8958 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 8959
d2415c6c
DE
8960 Given a 64-bit compilation unit signature or a type signature S, an entry
8961 in the hash table is located as follows:
80626a55 8962
d2415c6c
DE
8963 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8964 the low-order k bits all set to 1.
80626a55 8965
d2415c6c 8966 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 8967
d2415c6c
DE
8968 3) If the hash table entry at index H matches the signature, use that
8969 entry. If the hash table entry at index H is unused (all zeroes),
8970 terminate the search: the signature is not present in the table.
80626a55 8971
d2415c6c 8972 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 8973
d2415c6c
DE
8974 Because M > N and H' and M are relatively prime, the search is guaranteed
8975 to stop at an unused slot or find the match.
80626a55 8976
d2415c6c
DE
8977 The pool of section numbers begins immediately following the hash table
8978 (at offset 16 + 12 * M from the beginning of the section). The pool of
8979 section numbers consists of an array of 32-bit words (using the byte order
8980 of the application binary). Each item in the array is indexed starting
8981 from 0. The hash table entry provides the index of the first section
8982 number in the set. Additional section numbers in the set follow, and the
8983 set is terminated by a 0 entry (section number 0 is not used in ELF).
80626a55 8984
d2415c6c
DE
8985 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8986 section must be the first entry in the set, and the .debug_abbrev.dwo must
8987 be the second entry. Other members of the set may follow in any order. */
80626a55
DE
8988
8989/* Create a hash table to map DWO IDs to their CU/TU entry in
8990 .debug_{info,types}.dwo in DWP_FILE.
8991 Returns NULL if there isn't one.
8992 Note: This function processes DWP files only, not DWO files. */
8993
8994static struct dwp_hash_table *
8995create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8996{
8997 struct objfile *objfile = dwarf2_per_objfile->objfile;
8998 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8999 const gdb_byte *index_ptr, *index_end;
80626a55
DE
9000 struct dwarf2_section_info *index;
9001 uint32_t version, nr_units, nr_slots;
9002 struct dwp_hash_table *htab;
9003
9004 if (is_debug_types)
9005 index = &dwp_file->sections.tu_index;
9006 else
9007 index = &dwp_file->sections.cu_index;
9008
9009 if (dwarf2_section_empty_p (index))
9010 return NULL;
9011 dwarf2_read_section (objfile, index);
9012
9013 index_ptr = index->buffer;
9014 index_end = index_ptr + index->size;
9015
9016 version = read_4_bytes (dbfd, index_ptr);
9017 index_ptr += 8; /* Skip the unused word. */
9018 nr_units = read_4_bytes (dbfd, index_ptr);
9019 index_ptr += 4;
9020 nr_slots = read_4_bytes (dbfd, index_ptr);
9021 index_ptr += 4;
9022
9023 if (version != 1)
9024 {
21aa081e 9025 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9026 " [in module %s]"),
21aa081e 9027 pulongest (version), dwp_file->name);
80626a55
DE
9028 }
9029 if (nr_slots != (nr_slots & -nr_slots))
9030 {
21aa081e 9031 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9032 " is not power of 2 [in module %s]"),
21aa081e 9033 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9034 }
9035
9036 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9037 htab->nr_units = nr_units;
9038 htab->nr_slots = nr_slots;
9039 htab->hash_table = index_ptr;
9040 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9041 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9042
9043 return htab;
9044}
9045
9046/* Update SECTIONS with the data from SECTP.
9047
9048 This function is like the other "locate" section routines that are
9049 passed to bfd_map_over_sections, but in this context the sections to
9050 read comes from the DWP hash table, not the full ELF section table.
9051
9052 The result is non-zero for success, or zero if an error was found. */
9053
9054static int
9055locate_virtual_dwo_sections (asection *sectp,
9056 struct virtual_dwo_sections *sections)
9057{
9058 const struct dwop_section_names *names = &dwop_section_names;
9059
9060 if (section_is_p (sectp->name, &names->abbrev_dwo))
9061 {
9062 /* There can be only one. */
9063 if (sections->abbrev.asection != NULL)
9064 return 0;
9065 sections->abbrev.asection = sectp;
9066 sections->abbrev.size = bfd_get_section_size (sectp);
9067 }
9068 else if (section_is_p (sectp->name, &names->info_dwo)
9069 || section_is_p (sectp->name, &names->types_dwo))
9070 {
9071 /* There can be only one. */
9072 if (sections->info_or_types.asection != NULL)
9073 return 0;
9074 sections->info_or_types.asection = sectp;
9075 sections->info_or_types.size = bfd_get_section_size (sectp);
9076 }
9077 else if (section_is_p (sectp->name, &names->line_dwo))
9078 {
9079 /* There can be only one. */
9080 if (sections->line.asection != NULL)
9081 return 0;
9082 sections->line.asection = sectp;
9083 sections->line.size = bfd_get_section_size (sectp);
9084 }
9085 else if (section_is_p (sectp->name, &names->loc_dwo))
9086 {
9087 /* There can be only one. */
9088 if (sections->loc.asection != NULL)
9089 return 0;
9090 sections->loc.asection = sectp;
9091 sections->loc.size = bfd_get_section_size (sectp);
9092 }
9093 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9094 {
9095 /* There can be only one. */
9096 if (sections->macinfo.asection != NULL)
9097 return 0;
9098 sections->macinfo.asection = sectp;
9099 sections->macinfo.size = bfd_get_section_size (sectp);
9100 }
9101 else if (section_is_p (sectp->name, &names->macro_dwo))
9102 {
9103 /* There can be only one. */
9104 if (sections->macro.asection != NULL)
9105 return 0;
9106 sections->macro.asection = sectp;
9107 sections->macro.size = bfd_get_section_size (sectp);
9108 }
9109 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9110 {
9111 /* There can be only one. */
9112 if (sections->str_offsets.asection != NULL)
9113 return 0;
9114 sections->str_offsets.asection = sectp;
9115 sections->str_offsets.size = bfd_get_section_size (sectp);
9116 }
9117 else
9118 {
9119 /* No other kind of section is valid. */
9120 return 0;
9121 }
9122
9123 return 1;
9124}
9125
9126/* Create a dwo_unit object for the DWO with signature SIGNATURE.
9127 HTAB is the hash table from the DWP file.
0ac5b59e
DE
9128 SECTION_INDEX is the index of the DWO in HTAB.
9129 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
9130
9131static struct dwo_unit *
9132create_dwo_in_dwp (struct dwp_file *dwp_file,
9133 const struct dwp_hash_table *htab,
9134 uint32_t section_index,
0ac5b59e 9135 const char *comp_dir,
80626a55
DE
9136 ULONGEST signature, int is_debug_types)
9137{
9138 struct objfile *objfile = dwarf2_per_objfile->objfile;
9139 bfd *dbfd = dwp_file->dbfd;
9140 const char *kind = is_debug_types ? "TU" : "CU";
9141 struct dwo_file *dwo_file;
9142 struct dwo_unit *dwo_unit;
9143 struct virtual_dwo_sections sections;
9144 void **dwo_file_slot;
9145 char *virtual_dwo_name;
9146 struct dwarf2_section_info *cutu;
9147 struct cleanup *cleanups;
9148 int i;
9149
9150 if (dwarf2_read_debug)
9151 {
21aa081e 9152 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
80626a55 9153 kind,
21aa081e 9154 pulongest (section_index), hex_string (signature),
80626a55
DE
9155 dwp_file->name);
9156 }
9157
9158 /* Fetch the sections of this DWO.
9159 Put a limit on the number of sections we look for so that bad data
9160 doesn't cause us to loop forever. */
9161
9162#define MAX_NR_DWO_SECTIONS \
9163 (1 /* .debug_info or .debug_types */ \
9164 + 1 /* .debug_abbrev */ \
9165 + 1 /* .debug_line */ \
9166 + 1 /* .debug_loc */ \
9167 + 1 /* .debug_str_offsets */ \
9168 + 1 /* .debug_macro */ \
9169 + 1 /* .debug_macinfo */ \
9170 + 1 /* trailing zero */)
9171
9172 memset (&sections, 0, sizeof (sections));
9173 cleanups = make_cleanup (null_cleanup, 0);
9174
9175 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9176 {
9177 asection *sectp;
9178 uint32_t section_nr =
9179 read_4_bytes (dbfd,
9180 htab->section_pool
9181 + (section_index + i) * sizeof (uint32_t));
9182
9183 if (section_nr == 0)
9184 break;
9185 if (section_nr >= dwp_file->num_sections)
9186 {
9187 error (_("Dwarf Error: bad DWP hash table, section number too large"
9188 " [in module %s]"),
9189 dwp_file->name);
9190 }
9191
9192 sectp = dwp_file->elf_sections[section_nr];
9193 if (! locate_virtual_dwo_sections (sectp, &sections))
9194 {
9195 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9196 " [in module %s]"),
9197 dwp_file->name);
9198 }
9199 }
9200
9201 if (i < 2
9202 || sections.info_or_types.asection == NULL
9203 || sections.abbrev.asection == NULL)
9204 {
9205 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9206 " [in module %s]"),
9207 dwp_file->name);
9208 }
9209 if (i == MAX_NR_DWO_SECTIONS)
9210 {
9211 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9212 " [in module %s]"),
9213 dwp_file->name);
9214 }
9215
9216 /* It's easier for the rest of the code if we fake a struct dwo_file and
9217 have dwo_unit "live" in that. At least for now.
9218
9219 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
9220 However, for each CU + set of TUs that came from the same original DWO
9221 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
9222 (fewer struct dwo_file objects to allocated). Remember that for really
9223 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9224
2792b94d
PM
9225 virtual_dwo_name =
9226 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9227 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9228 sections.line.asection ? sections.line.asection->id : 0,
9229 sections.loc.asection ? sections.loc.asection->id : 0,
9230 (sections.str_offsets.asection
9231 ? sections.str_offsets.asection->id
9232 : 0));
80626a55
DE
9233 make_cleanup (xfree, virtual_dwo_name);
9234 /* Can we use an existing virtual DWO file? */
0ac5b59e 9235 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9236 /* Create one if necessary. */
9237 if (*dwo_file_slot == NULL)
9238 {
9239 if (dwarf2_read_debug)
9240 {
9241 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9242 virtual_dwo_name);
9243 }
9244 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9245 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9246 virtual_dwo_name,
9247 strlen (virtual_dwo_name));
9248 dwo_file->comp_dir = comp_dir;
80626a55
DE
9249 dwo_file->sections.abbrev = sections.abbrev;
9250 dwo_file->sections.line = sections.line;
9251 dwo_file->sections.loc = sections.loc;
9252 dwo_file->sections.macinfo = sections.macinfo;
9253 dwo_file->sections.macro = sections.macro;
9254 dwo_file->sections.str_offsets = sections.str_offsets;
9255 /* The "str" section is global to the entire DWP file. */
9256 dwo_file->sections.str = dwp_file->sections.str;
9257 /* The info or types section is assigned later to dwo_unit,
9258 there's no need to record it in dwo_file.
9259 Also, we can't simply record type sections in dwo_file because
9260 we record a pointer into the vector in dwo_unit. As we collect more
9261 types we'll grow the vector and eventually have to reallocate space
9262 for it, invalidating all the pointers into the current copy. */
9263 *dwo_file_slot = dwo_file;
9264 }
9265 else
9266 {
9267 if (dwarf2_read_debug)
9268 {
9269 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9270 virtual_dwo_name);
9271 }
9272 dwo_file = *dwo_file_slot;
9273 }
9274 do_cleanups (cleanups);
9275
9276 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9277 dwo_unit->dwo_file = dwo_file;
9278 dwo_unit->signature = signature;
8a0459fd
DE
9279 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9280 sizeof (struct dwarf2_section_info));
9281 *dwo_unit->section = sections.info_or_types;
80626a55
DE
9282 /* offset, length, type_offset_in_tu are set later. */
9283
9284 return dwo_unit;
9285}
9286
9287/* Lookup the DWO with SIGNATURE in DWP_FILE. */
9288
9289static struct dwo_unit *
9290lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9291 const struct dwp_hash_table *htab,
0ac5b59e 9292 const char *comp_dir,
80626a55
DE
9293 ULONGEST signature, int is_debug_types)
9294{
9295 bfd *dbfd = dwp_file->dbfd;
9296 uint32_t mask = htab->nr_slots - 1;
9297 uint32_t hash = signature & mask;
9298 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9299 unsigned int i;
9300 void **slot;
9301 struct dwo_unit find_dwo_cu, *dwo_cu;
9302
9303 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9304 find_dwo_cu.signature = signature;
9305 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9306
9307 if (*slot != NULL)
9308 return *slot;
9309
9310 /* Use a for loop so that we don't loop forever on bad debug info. */
9311 for (i = 0; i < htab->nr_slots; ++i)
9312 {
9313 ULONGEST signature_in_table;
9314
9315 signature_in_table =
9316 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9317 if (signature_in_table == signature)
9318 {
9319 uint32_t section_index =
9320 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9321
9322 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 9323 comp_dir, signature, is_debug_types);
80626a55
DE
9324 return *slot;
9325 }
9326 if (signature_in_table == 0)
9327 return NULL;
9328 hash = (hash + hash2) & mask;
9329 }
9330
9331 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9332 " [in module %s]"),
9333 dwp_file->name);
9334}
9335
ab5088bf 9336/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9337 Open the file specified by FILE_NAME and hand it off to BFD for
9338 preliminary analysis. Return a newly initialized bfd *, which
9339 includes a canonicalized copy of FILE_NAME.
80626a55 9340 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
9341 SEARCH_CWD is true if the current directory is to be searched.
9342 It will be searched before debug-file-directory.
9343 If unable to find/open the file, return NULL.
3019eac3
DE
9344 NOTE: This function is derived from symfile_bfd_open. */
9345
9346static bfd *
6ac97d4c 9347try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
9348{
9349 bfd *sym_bfd;
80626a55 9350 int desc, flags;
3019eac3 9351 char *absolute_name;
9c02c129
DE
9352 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9353 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9354 to debug_file_directory. */
9355 char *search_path;
9356 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9357
6ac97d4c
DE
9358 if (search_cwd)
9359 {
9360 if (*debug_file_directory != '\0')
9361 search_path = concat (".", dirname_separator_string,
9362 debug_file_directory, NULL);
9363 else
9364 search_path = xstrdup (".");
9365 }
9c02c129 9366 else
6ac97d4c 9367 search_path = xstrdup (debug_file_directory);
3019eac3 9368
492c0ab7 9369 flags = OPF_RETURN_REALPATH;
80626a55
DE
9370 if (is_dwp)
9371 flags |= OPF_SEARCH_IN_PATH;
9c02c129 9372 desc = openp (search_path, flags, file_name,
3019eac3 9373 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 9374 xfree (search_path);
3019eac3
DE
9375 if (desc < 0)
9376 return NULL;
9377
bb397797 9378 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 9379 xfree (absolute_name);
9c02c129
DE
9380 if (sym_bfd == NULL)
9381 return NULL;
3019eac3
DE
9382 bfd_set_cacheable (sym_bfd, 1);
9383
9384 if (!bfd_check_format (sym_bfd, bfd_object))
9385 {
cbb099e8 9386 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
9387 return NULL;
9388 }
9389
3019eac3
DE
9390 return sym_bfd;
9391}
9392
ab5088bf 9393/* Try to open DWO file FILE_NAME.
3019eac3
DE
9394 COMP_DIR is the DW_AT_comp_dir attribute.
9395 The result is the bfd handle of the file.
9396 If there is a problem finding or opening the file, return NULL.
9397 Upon success, the canonicalized path of the file is stored in the bfd,
9398 same as symfile_bfd_open. */
9399
9400static bfd *
ab5088bf 9401open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
9402{
9403 bfd *abfd;
3019eac3 9404
80626a55 9405 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 9406 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
9407
9408 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9409
9410 if (comp_dir != NULL)
9411 {
80626a55 9412 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9413
9414 /* NOTE: If comp_dir is a relative path, this will also try the
9415 search path, which seems useful. */
6ac97d4c 9416 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9417 xfree (path_to_try);
9418 if (abfd != NULL)
9419 return abfd;
9420 }
9421
9422 /* That didn't work, try debug-file-directory, which, despite its name,
9423 is a list of paths. */
9424
9425 if (*debug_file_directory == '\0')
9426 return NULL;
9427
6ac97d4c 9428 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9429}
9430
80626a55
DE
9431/* This function is mapped across the sections and remembers the offset and
9432 size of each of the DWO debugging sections we are interested in. */
9433
9434static void
9435dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9436{
9437 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9438 const struct dwop_section_names *names = &dwop_section_names;
9439
9440 if (section_is_p (sectp->name, &names->abbrev_dwo))
9441 {
9442 dwo_sections->abbrev.asection = sectp;
9443 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9444 }
9445 else if (section_is_p (sectp->name, &names->info_dwo))
9446 {
9447 dwo_sections->info.asection = sectp;
9448 dwo_sections->info.size = bfd_get_section_size (sectp);
9449 }
9450 else if (section_is_p (sectp->name, &names->line_dwo))
9451 {
9452 dwo_sections->line.asection = sectp;
9453 dwo_sections->line.size = bfd_get_section_size (sectp);
9454 }
9455 else if (section_is_p (sectp->name, &names->loc_dwo))
9456 {
9457 dwo_sections->loc.asection = sectp;
9458 dwo_sections->loc.size = bfd_get_section_size (sectp);
9459 }
9460 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9461 {
9462 dwo_sections->macinfo.asection = sectp;
9463 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9464 }
9465 else if (section_is_p (sectp->name, &names->macro_dwo))
9466 {
9467 dwo_sections->macro.asection = sectp;
9468 dwo_sections->macro.size = bfd_get_section_size (sectp);
9469 }
9470 else if (section_is_p (sectp->name, &names->str_dwo))
9471 {
9472 dwo_sections->str.asection = sectp;
9473 dwo_sections->str.size = bfd_get_section_size (sectp);
9474 }
9475 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9476 {
9477 dwo_sections->str_offsets.asection = sectp;
9478 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9479 }
9480 else if (section_is_p (sectp->name, &names->types_dwo))
9481 {
9482 struct dwarf2_section_info type_section;
9483
9484 memset (&type_section, 0, sizeof (type_section));
9485 type_section.asection = sectp;
9486 type_section.size = bfd_get_section_size (sectp);
9487 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9488 &type_section);
9489 }
9490}
9491
ab5088bf 9492/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9493 by PER_CU. This is for the non-DWP case.
80626a55 9494 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9495
9496static struct dwo_file *
0ac5b59e
DE
9497open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9498 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9499{
9500 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9501 struct dwo_file *dwo_file;
9502 bfd *dbfd;
3019eac3
DE
9503 struct cleanup *cleanups;
9504
ab5088bf 9505 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9506 if (dbfd == NULL)
9507 {
9508 if (dwarf2_read_debug)
9509 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9510 return NULL;
9511 }
9512 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9513 dwo_file->dwo_name = dwo_name;
9514 dwo_file->comp_dir = comp_dir;
80626a55 9515 dwo_file->dbfd = dbfd;
3019eac3
DE
9516
9517 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9518
80626a55 9519 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9520
19c3d4c9 9521 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9522
9523 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9524 dwo_file->sections.types);
9525
9526 discard_cleanups (cleanups);
9527
80626a55
DE
9528 if (dwarf2_read_debug)
9529 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9530
3019eac3
DE
9531 return dwo_file;
9532}
9533
80626a55
DE
9534/* This function is mapped across the sections and remembers the offset and
9535 size of each of the DWP debugging sections we are interested in. */
3019eac3 9536
80626a55
DE
9537static void
9538dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9539{
80626a55
DE
9540 struct dwp_file *dwp_file = dwp_file_ptr;
9541 const struct dwop_section_names *names = &dwop_section_names;
9542 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9543
80626a55
DE
9544 /* Record the ELF section number for later lookup: this is what the
9545 .debug_cu_index,.debug_tu_index tables use. */
9546 gdb_assert (elf_section_nr < dwp_file->num_sections);
9547 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9548
80626a55
DE
9549 /* Look for specific sections that we need. */
9550 if (section_is_p (sectp->name, &names->str_dwo))
9551 {
9552 dwp_file->sections.str.asection = sectp;
9553 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9554 }
9555 else if (section_is_p (sectp->name, &names->cu_index))
9556 {
9557 dwp_file->sections.cu_index.asection = sectp;
9558 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9559 }
9560 else if (section_is_p (sectp->name, &names->tu_index))
9561 {
9562 dwp_file->sections.tu_index.asection = sectp;
9563 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9564 }
9565}
3019eac3 9566
80626a55 9567/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9568
80626a55
DE
9569static hashval_t
9570hash_dwp_loaded_cutus (const void *item)
9571{
9572 const struct dwo_unit *dwo_unit = item;
3019eac3 9573
80626a55
DE
9574 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9575 return dwo_unit->signature;
3019eac3
DE
9576}
9577
80626a55 9578/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9579
80626a55
DE
9580static int
9581eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9582{
80626a55
DE
9583 const struct dwo_unit *dua = a;
9584 const struct dwo_unit *dub = b;
3019eac3 9585
80626a55
DE
9586 return dua->signature == dub->signature;
9587}
3019eac3 9588
80626a55 9589/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9590
80626a55
DE
9591static htab_t
9592allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9593{
9594 return htab_create_alloc_ex (3,
9595 hash_dwp_loaded_cutus,
9596 eq_dwp_loaded_cutus,
9597 NULL,
9598 &objfile->objfile_obstack,
9599 hashtab_obstack_allocate,
9600 dummy_obstack_deallocate);
9601}
3019eac3 9602
ab5088bf
DE
9603/* Try to open DWP file FILE_NAME.
9604 The result is the bfd handle of the file.
9605 If there is a problem finding or opening the file, return NULL.
9606 Upon success, the canonicalized path of the file is stored in the bfd,
9607 same as symfile_bfd_open. */
9608
9609static bfd *
9610open_dwp_file (const char *file_name)
9611{
6ac97d4c
DE
9612 bfd *abfd;
9613
9614 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9615 if (abfd != NULL)
9616 return abfd;
9617
9618 /* Work around upstream bug 15652.
9619 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9620 [Whether that's a "bug" is debatable, but it is getting in our way.]
9621 We have no real idea where the dwp file is, because gdb's realpath-ing
9622 of the executable's path may have discarded the needed info.
9623 [IWBN if the dwp file name was recorded in the executable, akin to
9624 .gnu_debuglink, but that doesn't exist yet.]
9625 Strip the directory from FILE_NAME and search again. */
9626 if (*debug_file_directory != '\0')
9627 {
9628 /* Don't implicitly search the current directory here.
9629 If the user wants to search "." to handle this case,
9630 it must be added to debug-file-directory. */
9631 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9632 0 /*search_cwd*/);
9633 }
9634
9635 return NULL;
ab5088bf
DE
9636}
9637
80626a55
DE
9638/* Initialize the use of the DWP file for the current objfile.
9639 By convention the name of the DWP file is ${objfile}.dwp.
9640 The result is NULL if it can't be found. */
a766d390 9641
80626a55 9642static struct dwp_file *
ab5088bf 9643open_and_init_dwp_file (void)
80626a55
DE
9644{
9645 struct objfile *objfile = dwarf2_per_objfile->objfile;
9646 struct dwp_file *dwp_file;
9647 char *dwp_name;
9648 bfd *dbfd;
9649 struct cleanup *cleanups;
9650
4262abfb 9651 dwp_name = xstrprintf ("%s.dwp", objfile_name (dwarf2_per_objfile->objfile));
80626a55
DE
9652 cleanups = make_cleanup (xfree, dwp_name);
9653
ab5088bf 9654 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9655 if (dbfd == NULL)
9656 {
9657 if (dwarf2_read_debug)
9658 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9659 do_cleanups (cleanups);
9660 return NULL;
3019eac3 9661 }
80626a55 9662 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 9663 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
9664 dwp_file->dbfd = dbfd;
9665 do_cleanups (cleanups);
c906108c 9666
80626a55
DE
9667 /* +1: section 0 is unused */
9668 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9669 dwp_file->elf_sections =
9670 OBSTACK_CALLOC (&objfile->objfile_obstack,
9671 dwp_file->num_sections, asection *);
9672
9673 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9674
9675 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9676
9677 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9678
9679 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9680
80626a55
DE
9681 if (dwarf2_read_debug)
9682 {
9683 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9684 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
9685 " %s CUs, %s TUs\n",
9686 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9687 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
9688 }
9689
9690 return dwp_file;
3019eac3 9691}
c906108c 9692
ab5088bf
DE
9693/* Wrapper around open_and_init_dwp_file, only open it once. */
9694
9695static struct dwp_file *
9696get_dwp_file (void)
9697{
9698 if (! dwarf2_per_objfile->dwp_checked)
9699 {
9700 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9701 dwarf2_per_objfile->dwp_checked = 1;
9702 }
9703 return dwarf2_per_objfile->dwp_file;
9704}
9705
80626a55
DE
9706/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9707 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9708 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9709 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9710 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9711
9712 This is called, for example, when wanting to read a variable with a
9713 complex location. Therefore we don't want to do file i/o for every call.
9714 Therefore we don't want to look for a DWO file on every call.
9715 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9716 then we check if we've already seen DWO_NAME, and only THEN do we check
9717 for a DWO file.
9718
1c658ad5 9719 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9720 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9721
3019eac3 9722static struct dwo_unit *
80626a55
DE
9723lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9724 const char *dwo_name, const char *comp_dir,
9725 ULONGEST signature, int is_debug_types)
3019eac3
DE
9726{
9727 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9728 const char *kind = is_debug_types ? "TU" : "CU";
9729 void **dwo_file_slot;
3019eac3 9730 struct dwo_file *dwo_file;
80626a55 9731 struct dwp_file *dwp_file;
cb1df416 9732
6a506a2d
DE
9733 /* First see if there's a DWP file.
9734 If we have a DWP file but didn't find the DWO inside it, don't
9735 look for the original DWO file. It makes gdb behave differently
9736 depending on whether one is debugging in the build tree. */
cf2c3c16 9737
ab5088bf 9738 dwp_file = get_dwp_file ();
80626a55 9739 if (dwp_file != NULL)
cf2c3c16 9740 {
80626a55
DE
9741 const struct dwp_hash_table *dwp_htab =
9742 is_debug_types ? dwp_file->tus : dwp_file->cus;
9743
9744 if (dwp_htab != NULL)
9745 {
9746 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9747 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9748 signature, is_debug_types);
80626a55
DE
9749
9750 if (dwo_cutu != NULL)
9751 {
9752 if (dwarf2_read_debug)
9753 {
9754 fprintf_unfiltered (gdb_stdlog,
9755 "Virtual DWO %s %s found: @%s\n",
9756 kind, hex_string (signature),
9757 host_address_to_string (dwo_cutu));
9758 }
9759 return dwo_cutu;
9760 }
9761 }
9762 }
6a506a2d 9763 else
80626a55 9764 {
6a506a2d 9765 /* No DWP file, look for the DWO file. */
80626a55 9766
6a506a2d
DE
9767 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9768 if (*dwo_file_slot == NULL)
80626a55 9769 {
6a506a2d
DE
9770 /* Read in the file and build a table of the CUs/TUs it contains. */
9771 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9772 }
6a506a2d
DE
9773 /* NOTE: This will be NULL if unable to open the file. */
9774 dwo_file = *dwo_file_slot;
3019eac3 9775
6a506a2d 9776 if (dwo_file != NULL)
19c3d4c9 9777 {
6a506a2d
DE
9778 struct dwo_unit *dwo_cutu = NULL;
9779
9780 if (is_debug_types && dwo_file->tus)
9781 {
9782 struct dwo_unit find_dwo_cutu;
9783
9784 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9785 find_dwo_cutu.signature = signature;
9786 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9787 }
9788 else if (!is_debug_types && dwo_file->cu)
80626a55 9789 {
6a506a2d
DE
9790 if (signature == dwo_file->cu->signature)
9791 dwo_cutu = dwo_file->cu;
9792 }
9793
9794 if (dwo_cutu != NULL)
9795 {
9796 if (dwarf2_read_debug)
9797 {
9798 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9799 kind, dwo_name, hex_string (signature),
9800 host_address_to_string (dwo_cutu));
9801 }
9802 return dwo_cutu;
80626a55
DE
9803 }
9804 }
2e276125 9805 }
9cdd5dbd 9806
80626a55
DE
9807 /* We didn't find it. This could mean a dwo_id mismatch, or
9808 someone deleted the DWO/DWP file, or the search path isn't set up
9809 correctly to find the file. */
9810
9811 if (dwarf2_read_debug)
9812 {
9813 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9814 kind, dwo_name, hex_string (signature));
9815 }
3019eac3 9816
6656a72d
DE
9817 /* This is a warning and not a complaint because it can be caused by
9818 pilot error (e.g., user accidentally deleting the DWO). */
9819 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9820 " [in module %s]"),
9821 kind, dwo_name, hex_string (signature),
9822 this_unit->is_debug_types ? "TU" : "CU",
4262abfb 9823 this_unit->offset.sect_off, objfile_name (objfile));
3019eac3 9824 return NULL;
5fb290d7
DJ
9825}
9826
80626a55
DE
9827/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9828 See lookup_dwo_cutu_unit for details. */
9829
9830static struct dwo_unit *
9831lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9832 const char *dwo_name, const char *comp_dir,
9833 ULONGEST signature)
9834{
9835 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9836}
9837
9838/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9839 See lookup_dwo_cutu_unit for details. */
9840
9841static struct dwo_unit *
9842lookup_dwo_type_unit (struct signatured_type *this_tu,
9843 const char *dwo_name, const char *comp_dir)
9844{
9845 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9846}
9847
89e63ee4
DE
9848/* Traversal function for queue_and_load_all_dwo_tus. */
9849
9850static int
9851queue_and_load_dwo_tu (void **slot, void *info)
9852{
9853 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9854 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
9855 ULONGEST signature = dwo_unit->signature;
9856 struct signatured_type *sig_type =
9857 lookup_dwo_signatured_type (per_cu->cu, signature);
9858
9859 if (sig_type != NULL)
9860 {
9861 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
9862
9863 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9864 a real dependency of PER_CU on SIG_TYPE. That is detected later
9865 while processing PER_CU. */
9866 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
9867 load_full_type_unit (sig_cu);
9868 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
9869 }
9870
9871 return 1;
9872}
9873
9874/* Queue all TUs contained in the DWO of PER_CU to be read in.
9875 The DWO may have the only definition of the type, though it may not be
9876 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9877 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9878
9879static void
9880queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
9881{
9882 struct dwo_unit *dwo_unit;
9883 struct dwo_file *dwo_file;
9884
9885 gdb_assert (!per_cu->is_debug_types);
9886 gdb_assert (get_dwp_file () == NULL);
9887 gdb_assert (per_cu->cu != NULL);
9888
9889 dwo_unit = per_cu->cu->dwo_unit;
9890 gdb_assert (dwo_unit != NULL);
9891
9892 dwo_file = dwo_unit->dwo_file;
9893 if (dwo_file->tus != NULL)
9894 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
9895}
9896
3019eac3
DE
9897/* Free all resources associated with DWO_FILE.
9898 Close the DWO file and munmap the sections.
9899 All memory should be on the objfile obstack. */
348e048f
DE
9900
9901static void
3019eac3 9902free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9903{
3019eac3
DE
9904 int ix;
9905 struct dwarf2_section_info *section;
348e048f 9906
5c6fa7ab 9907 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9908 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9909
3019eac3
DE
9910 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9911}
348e048f 9912
3019eac3 9913/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9914
3019eac3
DE
9915static void
9916free_dwo_file_cleanup (void *arg)
9917{
9918 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9919 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9920
3019eac3
DE
9921 free_dwo_file (dwo_file, objfile);
9922}
348e048f 9923
3019eac3 9924/* Traversal function for free_dwo_files. */
2ab95328 9925
3019eac3
DE
9926static int
9927free_dwo_file_from_slot (void **slot, void *info)
9928{
9929 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9930 struct objfile *objfile = (struct objfile *) info;
348e048f 9931
3019eac3 9932 free_dwo_file (dwo_file, objfile);
348e048f 9933
3019eac3
DE
9934 return 1;
9935}
348e048f 9936
3019eac3 9937/* Free all resources associated with DWO_FILES. */
348e048f 9938
3019eac3
DE
9939static void
9940free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9941{
9942 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9943}
3019eac3
DE
9944\f
9945/* Read in various DIEs. */
348e048f 9946
d389af10
JK
9947/* qsort helper for inherit_abstract_dies. */
9948
9949static int
9950unsigned_int_compar (const void *ap, const void *bp)
9951{
9952 unsigned int a = *(unsigned int *) ap;
9953 unsigned int b = *(unsigned int *) bp;
9954
9955 return (a > b) - (b > a);
9956}
9957
9958/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9959 Inherit only the children of the DW_AT_abstract_origin DIE not being
9960 already referenced by DW_AT_abstract_origin from the children of the
9961 current DIE. */
d389af10
JK
9962
9963static void
9964inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9965{
9966 struct die_info *child_die;
9967 unsigned die_children_count;
9968 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9969 sect_offset *offsets;
9970 sect_offset *offsets_end, *offsetp;
d389af10
JK
9971 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9972 struct die_info *origin_die;
9973 /* Iterator of the ORIGIN_DIE children. */
9974 struct die_info *origin_child_die;
9975 struct cleanup *cleanups;
9976 struct attribute *attr;
cd02d79d
PA
9977 struct dwarf2_cu *origin_cu;
9978 struct pending **origin_previous_list_in_scope;
d389af10
JK
9979
9980 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9981 if (!attr)
9982 return;
9983
cd02d79d
PA
9984 /* Note that following die references may follow to a die in a
9985 different cu. */
9986
9987 origin_cu = cu;
9988 origin_die = follow_die_ref (die, attr, &origin_cu);
9989
9990 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9991 symbols in. */
9992 origin_previous_list_in_scope = origin_cu->list_in_scope;
9993 origin_cu->list_in_scope = cu->list_in_scope;
9994
edb3359d
DJ
9995 if (die->tag != origin_die->tag
9996 && !(die->tag == DW_TAG_inlined_subroutine
9997 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9998 complaint (&symfile_complaints,
9999 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 10000 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
10001
10002 child_die = die->child;
10003 die_children_count = 0;
10004 while (child_die && child_die->tag)
10005 {
10006 child_die = sibling_die (child_die);
10007 die_children_count++;
10008 }
10009 offsets = xmalloc (sizeof (*offsets) * die_children_count);
10010 cleanups = make_cleanup (xfree, offsets);
10011
10012 offsets_end = offsets;
10013 child_die = die->child;
10014 while (child_die && child_die->tag)
10015 {
c38f313d
DJ
10016 /* For each CHILD_DIE, find the corresponding child of
10017 ORIGIN_DIE. If there is more than one layer of
10018 DW_AT_abstract_origin, follow them all; there shouldn't be,
10019 but GCC versions at least through 4.4 generate this (GCC PR
10020 40573). */
10021 struct die_info *child_origin_die = child_die;
cd02d79d 10022 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 10023
c38f313d
DJ
10024 while (1)
10025 {
cd02d79d
PA
10026 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
10027 child_origin_cu);
c38f313d
DJ
10028 if (attr == NULL)
10029 break;
cd02d79d
PA
10030 child_origin_die = follow_die_ref (child_origin_die, attr,
10031 &child_origin_cu);
c38f313d
DJ
10032 }
10033
d389af10
JK
10034 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
10035 counterpart may exist. */
c38f313d 10036 if (child_origin_die != child_die)
d389af10 10037 {
edb3359d
DJ
10038 if (child_die->tag != child_origin_die->tag
10039 && !(child_die->tag == DW_TAG_inlined_subroutine
10040 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
10041 complaint (&symfile_complaints,
10042 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10043 "different tags"), child_die->offset.sect_off,
10044 child_origin_die->offset.sect_off);
c38f313d
DJ
10045 if (child_origin_die->parent != origin_die)
10046 complaint (&symfile_complaints,
10047 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10048 "different parents"), child_die->offset.sect_off,
10049 child_origin_die->offset.sect_off);
c38f313d
DJ
10050 else
10051 *offsets_end++ = child_origin_die->offset;
d389af10
JK
10052 }
10053 child_die = sibling_die (child_die);
10054 }
10055 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10056 unsigned_int_compar);
10057 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 10058 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
10059 complaint (&symfile_complaints,
10060 _("Multiple children of DIE 0x%x refer "
10061 "to DIE 0x%x as their abstract origin"),
b64f50a1 10062 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
10063
10064 offsetp = offsets;
10065 origin_child_die = origin_die->child;
10066 while (origin_child_die && origin_child_die->tag)
10067 {
10068 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
10069 while (offsetp < offsets_end
10070 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 10071 offsetp++;
b64f50a1
JK
10072 if (offsetp >= offsets_end
10073 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
10074 {
10075 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 10076 process_die (origin_child_die, origin_cu);
d389af10
JK
10077 }
10078 origin_child_die = sibling_die (origin_child_die);
10079 }
cd02d79d 10080 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
10081
10082 do_cleanups (cleanups);
10083}
10084
c906108c 10085static void
e7c27a73 10086read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10087{
e7c27a73 10088 struct objfile *objfile = cu->objfile;
52f0bd74 10089 struct context_stack *new;
c906108c
SS
10090 CORE_ADDR lowpc;
10091 CORE_ADDR highpc;
10092 struct die_info *child_die;
edb3359d 10093 struct attribute *attr, *call_line, *call_file;
15d034d0 10094 const char *name;
e142c38c 10095 CORE_ADDR baseaddr;
801e3a5b 10096 struct block *block;
edb3359d 10097 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
10098 VEC (symbolp) *template_args = NULL;
10099 struct template_symbol *templ_func = NULL;
edb3359d
DJ
10100
10101 if (inlined_func)
10102 {
10103 /* If we do not have call site information, we can't show the
10104 caller of this inlined function. That's too confusing, so
10105 only use the scope for local variables. */
10106 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10107 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10108 if (call_line == NULL || call_file == NULL)
10109 {
10110 read_lexical_block_scope (die, cu);
10111 return;
10112 }
10113 }
c906108c 10114
e142c38c
DJ
10115 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10116
94af9270 10117 name = dwarf2_name (die, cu);
c906108c 10118
e8d05480
JB
10119 /* Ignore functions with missing or empty names. These are actually
10120 illegal according to the DWARF standard. */
10121 if (name == NULL)
10122 {
10123 complaint (&symfile_complaints,
b64f50a1
JK
10124 _("missing name for subprogram DIE at %d"),
10125 die->offset.sect_off);
e8d05480
JB
10126 return;
10127 }
10128
10129 /* Ignore functions with missing or invalid low and high pc attributes. */
10130 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10131 {
ae4d0c03
PM
10132 attr = dwarf2_attr (die, DW_AT_external, cu);
10133 if (!attr || !DW_UNSND (attr))
10134 complaint (&symfile_complaints,
3e43a32a
MS
10135 _("cannot get low and high bounds "
10136 "for subprogram DIE at %d"),
b64f50a1 10137 die->offset.sect_off);
e8d05480
JB
10138 return;
10139 }
c906108c
SS
10140
10141 lowpc += baseaddr;
10142 highpc += baseaddr;
10143
34eaf542
TT
10144 /* If we have any template arguments, then we must allocate a
10145 different sort of symbol. */
10146 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10147 {
10148 if (child_die->tag == DW_TAG_template_type_param
10149 || child_die->tag == DW_TAG_template_value_param)
10150 {
e623cf5d 10151 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10152 templ_func->base.is_cplus_template_function = 1;
10153 break;
10154 }
10155 }
10156
c906108c 10157 new = push_context (0, lowpc);
34eaf542
TT
10158 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10159 (struct symbol *) templ_func);
4c2df51b 10160
4cecd739
DJ
10161 /* If there is a location expression for DW_AT_frame_base, record
10162 it. */
e142c38c 10163 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10164 if (attr)
f1e6e072 10165 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10166
e142c38c 10167 cu->list_in_scope = &local_symbols;
c906108c 10168
639d11d3 10169 if (die->child != NULL)
c906108c 10170 {
639d11d3 10171 child_die = die->child;
c906108c
SS
10172 while (child_die && child_die->tag)
10173 {
34eaf542
TT
10174 if (child_die->tag == DW_TAG_template_type_param
10175 || child_die->tag == DW_TAG_template_value_param)
10176 {
10177 struct symbol *arg = new_symbol (child_die, NULL, cu);
10178
f1078f66
DJ
10179 if (arg != NULL)
10180 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10181 }
10182 else
10183 process_die (child_die, cu);
c906108c
SS
10184 child_die = sibling_die (child_die);
10185 }
10186 }
10187
d389af10
JK
10188 inherit_abstract_dies (die, cu);
10189
4a811a97
UW
10190 /* If we have a DW_AT_specification, we might need to import using
10191 directives from the context of the specification DIE. See the
10192 comment in determine_prefix. */
10193 if (cu->language == language_cplus
10194 && dwarf2_attr (die, DW_AT_specification, cu))
10195 {
10196 struct dwarf2_cu *spec_cu = cu;
10197 struct die_info *spec_die = die_specification (die, &spec_cu);
10198
10199 while (spec_die)
10200 {
10201 child_die = spec_die->child;
10202 while (child_die && child_die->tag)
10203 {
10204 if (child_die->tag == DW_TAG_imported_module)
10205 process_die (child_die, spec_cu);
10206 child_die = sibling_die (child_die);
10207 }
10208
10209 /* In some cases, GCC generates specification DIEs that
10210 themselves contain DW_AT_specification attributes. */
10211 spec_die = die_specification (spec_die, &spec_cu);
10212 }
10213 }
10214
c906108c
SS
10215 new = pop_context ();
10216 /* Make a block for the local symbols within. */
801e3a5b
JB
10217 block = finish_block (new->name, &local_symbols, new->old_blocks,
10218 lowpc, highpc, objfile);
10219
df8a16a1 10220 /* For C++, set the block's scope. */
195a3f6c 10221 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10222 && cu->processing_has_namespace_info)
195a3f6c
TT
10223 block_set_scope (block, determine_prefix (die, cu),
10224 &objfile->objfile_obstack);
df8a16a1 10225
801e3a5b
JB
10226 /* If we have address ranges, record them. */
10227 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10228
34eaf542
TT
10229 /* Attach template arguments to function. */
10230 if (! VEC_empty (symbolp, template_args))
10231 {
10232 gdb_assert (templ_func != NULL);
10233
10234 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10235 templ_func->template_arguments
10236 = obstack_alloc (&objfile->objfile_obstack,
10237 (templ_func->n_template_arguments
10238 * sizeof (struct symbol *)));
10239 memcpy (templ_func->template_arguments,
10240 VEC_address (symbolp, template_args),
10241 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10242 VEC_free (symbolp, template_args);
10243 }
10244
208d8187
JB
10245 /* In C++, we can have functions nested inside functions (e.g., when
10246 a function declares a class that has methods). This means that
10247 when we finish processing a function scope, we may need to go
10248 back to building a containing block's symbol lists. */
10249 local_symbols = new->locals;
27aa8d6a 10250 using_directives = new->using_directives;
208d8187 10251
921e78cf
JB
10252 /* If we've finished processing a top-level function, subsequent
10253 symbols go in the file symbol list. */
10254 if (outermost_context_p ())
e142c38c 10255 cu->list_in_scope = &file_symbols;
c906108c
SS
10256}
10257
10258/* Process all the DIES contained within a lexical block scope. Start
10259 a new scope, process the dies, and then close the scope. */
10260
10261static void
e7c27a73 10262read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10263{
e7c27a73 10264 struct objfile *objfile = cu->objfile;
52f0bd74 10265 struct context_stack *new;
c906108c
SS
10266 CORE_ADDR lowpc, highpc;
10267 struct die_info *child_die;
e142c38c
DJ
10268 CORE_ADDR baseaddr;
10269
10270 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
10271
10272 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
10273 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10274 as multiple lexical blocks? Handling children in a sane way would
6e70227d 10275 be nasty. Might be easier to properly extend generic blocks to
af34e669 10276 describe ranges. */
d85a05f0 10277 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
10278 return;
10279 lowpc += baseaddr;
10280 highpc += baseaddr;
10281
10282 push_context (0, lowpc);
639d11d3 10283 if (die->child != NULL)
c906108c 10284 {
639d11d3 10285 child_die = die->child;
c906108c
SS
10286 while (child_die && child_die->tag)
10287 {
e7c27a73 10288 process_die (child_die, cu);
c906108c
SS
10289 child_die = sibling_die (child_die);
10290 }
10291 }
10292 new = pop_context ();
10293
8540c487 10294 if (local_symbols != NULL || using_directives != NULL)
c906108c 10295 {
801e3a5b
JB
10296 struct block *block
10297 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10298 highpc, objfile);
10299
10300 /* Note that recording ranges after traversing children, as we
10301 do here, means that recording a parent's ranges entails
10302 walking across all its children's ranges as they appear in
10303 the address map, which is quadratic behavior.
10304
10305 It would be nicer to record the parent's ranges before
10306 traversing its children, simply overriding whatever you find
10307 there. But since we don't even decide whether to create a
10308 block until after we've traversed its children, that's hard
10309 to do. */
10310 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
10311 }
10312 local_symbols = new->locals;
27aa8d6a 10313 using_directives = new->using_directives;
c906108c
SS
10314}
10315
96408a79
SA
10316/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10317
10318static void
10319read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10320{
10321 struct objfile *objfile = cu->objfile;
10322 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10323 CORE_ADDR pc, baseaddr;
10324 struct attribute *attr;
10325 struct call_site *call_site, call_site_local;
10326 void **slot;
10327 int nparams;
10328 struct die_info *child_die;
10329
10330 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10331
10332 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10333 if (!attr)
10334 {
10335 complaint (&symfile_complaints,
10336 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10337 "DIE 0x%x [in module %s]"),
4262abfb 10338 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10339 return;
10340 }
10341 pc = DW_ADDR (attr) + baseaddr;
10342
10343 if (cu->call_site_htab == NULL)
10344 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10345 NULL, &objfile->objfile_obstack,
10346 hashtab_obstack_allocate, NULL);
10347 call_site_local.pc = pc;
10348 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10349 if (*slot != NULL)
10350 {
10351 complaint (&symfile_complaints,
10352 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10353 "DIE 0x%x [in module %s]"),
4262abfb
JK
10354 paddress (gdbarch, pc), die->offset.sect_off,
10355 objfile_name (objfile));
96408a79
SA
10356 return;
10357 }
10358
10359 /* Count parameters at the caller. */
10360
10361 nparams = 0;
10362 for (child_die = die->child; child_die && child_die->tag;
10363 child_die = sibling_die (child_die))
10364 {
10365 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10366 {
10367 complaint (&symfile_complaints,
10368 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10369 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb
JK
10370 child_die->tag, child_die->offset.sect_off,
10371 objfile_name (objfile));
96408a79
SA
10372 continue;
10373 }
10374
10375 nparams++;
10376 }
10377
10378 call_site = obstack_alloc (&objfile->objfile_obstack,
10379 (sizeof (*call_site)
10380 + (sizeof (*call_site->parameter)
10381 * (nparams - 1))));
10382 *slot = call_site;
10383 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10384 call_site->pc = pc;
10385
10386 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10387 {
10388 struct die_info *func_die;
10389
10390 /* Skip also over DW_TAG_inlined_subroutine. */
10391 for (func_die = die->parent;
10392 func_die && func_die->tag != DW_TAG_subprogram
10393 && func_die->tag != DW_TAG_subroutine_type;
10394 func_die = func_die->parent);
10395
10396 /* DW_AT_GNU_all_call_sites is a superset
10397 of DW_AT_GNU_all_tail_call_sites. */
10398 if (func_die
10399 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10400 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10401 {
10402 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10403 not complete. But keep CALL_SITE for look ups via call_site_htab,
10404 both the initial caller containing the real return address PC and
10405 the final callee containing the current PC of a chain of tail
10406 calls do not need to have the tail call list complete. But any
10407 function candidate for a virtual tail call frame searched via
10408 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10409 determined unambiguously. */
10410 }
10411 else
10412 {
10413 struct type *func_type = NULL;
10414
10415 if (func_die)
10416 func_type = get_die_type (func_die, cu);
10417 if (func_type != NULL)
10418 {
10419 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10420
10421 /* Enlist this call site to the function. */
10422 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10423 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10424 }
10425 else
10426 complaint (&symfile_complaints,
10427 _("Cannot find function owning DW_TAG_GNU_call_site "
10428 "DIE 0x%x [in module %s]"),
4262abfb 10429 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10430 }
10431 }
10432
10433 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10434 if (attr == NULL)
10435 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10436 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10437 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10438 /* Keep NULL DWARF_BLOCK. */;
10439 else if (attr_form_is_block (attr))
10440 {
10441 struct dwarf2_locexpr_baton *dlbaton;
10442
10443 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10444 dlbaton->data = DW_BLOCK (attr)->data;
10445 dlbaton->size = DW_BLOCK (attr)->size;
10446 dlbaton->per_cu = cu->per_cu;
10447
10448 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10449 }
7771576e 10450 else if (attr_form_is_ref (attr))
96408a79 10451 {
96408a79
SA
10452 struct dwarf2_cu *target_cu = cu;
10453 struct die_info *target_die;
10454
ac9ec31b 10455 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
10456 gdb_assert (target_cu->objfile == objfile);
10457 if (die_is_declaration (target_die, target_cu))
10458 {
9112db09
JK
10459 const char *target_physname = NULL;
10460 struct attribute *target_attr;
10461
10462 /* Prefer the mangled name; otherwise compute the demangled one. */
10463 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10464 if (target_attr == NULL)
10465 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10466 target_cu);
10467 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10468 target_physname = DW_STRING (target_attr);
10469 else
10470 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
10471 if (target_physname == NULL)
10472 complaint (&symfile_complaints,
10473 _("DW_AT_GNU_call_site_target target DIE has invalid "
10474 "physname, for referencing DIE 0x%x [in module %s]"),
4262abfb 10475 die->offset.sect_off, objfile_name (objfile));
96408a79 10476 else
7d455152 10477 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
10478 }
10479 else
10480 {
10481 CORE_ADDR lowpc;
10482
10483 /* DW_AT_entry_pc should be preferred. */
10484 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10485 complaint (&symfile_complaints,
10486 _("DW_AT_GNU_call_site_target target DIE has invalid "
10487 "low pc, for referencing DIE 0x%x [in module %s]"),
4262abfb 10488 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10489 else
10490 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10491 }
10492 }
10493 else
10494 complaint (&symfile_complaints,
10495 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10496 "block nor reference, for DIE 0x%x [in module %s]"),
4262abfb 10497 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10498
10499 call_site->per_cu = cu->per_cu;
10500
10501 for (child_die = die->child;
10502 child_die && child_die->tag;
10503 child_die = sibling_die (child_die))
10504 {
96408a79 10505 struct call_site_parameter *parameter;
1788b2d3 10506 struct attribute *loc, *origin;
96408a79
SA
10507
10508 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10509 {
10510 /* Already printed the complaint above. */
10511 continue;
10512 }
10513
10514 gdb_assert (call_site->parameter_count < nparams);
10515 parameter = &call_site->parameter[call_site->parameter_count];
10516
1788b2d3
JK
10517 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10518 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10519 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10520
24c5c679 10521 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 10522 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 10523 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
10524 {
10525 sect_offset offset;
10526
10527 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10528 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10529 if (!offset_in_cu_p (&cu->header, offset))
10530 {
10531 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10532 binding can be done only inside one CU. Such referenced DIE
10533 therefore cannot be even moved to DW_TAG_partial_unit. */
10534 complaint (&symfile_complaints,
10535 _("DW_AT_abstract_origin offset is not in CU for "
10536 "DW_TAG_GNU_call_site child DIE 0x%x "
10537 "[in module %s]"),
4262abfb 10538 child_die->offset.sect_off, objfile_name (objfile));
d76b7dbc
JK
10539 continue;
10540 }
1788b2d3
JK
10541 parameter->u.param_offset.cu_off = (offset.sect_off
10542 - cu->header.offset.sect_off);
10543 }
10544 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10545 {
10546 complaint (&symfile_complaints,
10547 _("No DW_FORM_block* DW_AT_location for "
10548 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 10549 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10550 continue;
10551 }
24c5c679 10552 else
96408a79 10553 {
24c5c679
JK
10554 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10555 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10556 if (parameter->u.dwarf_reg != -1)
10557 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10558 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10559 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10560 &parameter->u.fb_offset))
10561 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10562 else
10563 {
10564 complaint (&symfile_complaints,
10565 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10566 "for DW_FORM_block* DW_AT_location is supported for "
10567 "DW_TAG_GNU_call_site child DIE 0x%x "
10568 "[in module %s]"),
4262abfb 10569 child_die->offset.sect_off, objfile_name (objfile));
24c5c679
JK
10570 continue;
10571 }
96408a79
SA
10572 }
10573
10574 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10575 if (!attr_form_is_block (attr))
10576 {
10577 complaint (&symfile_complaints,
10578 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10579 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 10580 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10581 continue;
10582 }
10583 parameter->value = DW_BLOCK (attr)->data;
10584 parameter->value_size = DW_BLOCK (attr)->size;
10585
10586 /* Parameters are not pre-cleared by memset above. */
10587 parameter->data_value = NULL;
10588 parameter->data_value_size = 0;
10589 call_site->parameter_count++;
10590
10591 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10592 if (attr)
10593 {
10594 if (!attr_form_is_block (attr))
10595 complaint (&symfile_complaints,
10596 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10597 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 10598 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10599 else
10600 {
10601 parameter->data_value = DW_BLOCK (attr)->data;
10602 parameter->data_value_size = DW_BLOCK (attr)->size;
10603 }
10604 }
10605 }
10606}
10607
43039443 10608/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10609 Return 1 if the attributes are present and valid, otherwise, return 0.
10610 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10611
10612static int
10613dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10614 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10615 struct partial_symtab *ranges_pst)
43039443
JK
10616{
10617 struct objfile *objfile = cu->objfile;
10618 struct comp_unit_head *cu_header = &cu->header;
10619 bfd *obfd = objfile->obfd;
10620 unsigned int addr_size = cu_header->addr_size;
10621 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10622 /* Base address selection entry. */
10623 CORE_ADDR base;
10624 int found_base;
10625 unsigned int dummy;
d521ce57 10626 const gdb_byte *buffer;
43039443
JK
10627 CORE_ADDR marker;
10628 int low_set;
10629 CORE_ADDR low = 0;
10630 CORE_ADDR high = 0;
ff013f42 10631 CORE_ADDR baseaddr;
43039443 10632
d00adf39
DE
10633 found_base = cu->base_known;
10634 base = cu->base_address;
43039443 10635
be391dca 10636 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10637 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10638 {
10639 complaint (&symfile_complaints,
10640 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10641 offset);
10642 return 0;
10643 }
dce234bc 10644 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10645
10646 /* Read in the largest possible address. */
10647 marker = read_address (obfd, buffer, cu, &dummy);
10648 if ((marker & mask) == mask)
10649 {
10650 /* If we found the largest possible address, then
10651 read the base address. */
10652 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10653 buffer += 2 * addr_size;
10654 offset += 2 * addr_size;
10655 found_base = 1;
10656 }
10657
10658 low_set = 0;
10659
e7030f15 10660 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10661
43039443
JK
10662 while (1)
10663 {
10664 CORE_ADDR range_beginning, range_end;
10665
10666 range_beginning = read_address (obfd, buffer, cu, &dummy);
10667 buffer += addr_size;
10668 range_end = read_address (obfd, buffer, cu, &dummy);
10669 buffer += addr_size;
10670 offset += 2 * addr_size;
10671
10672 /* An end of list marker is a pair of zero addresses. */
10673 if (range_beginning == 0 && range_end == 0)
10674 /* Found the end of list entry. */
10675 break;
10676
10677 /* Each base address selection entry is a pair of 2 values.
10678 The first is the largest possible address, the second is
10679 the base address. Check for a base address here. */
10680 if ((range_beginning & mask) == mask)
10681 {
10682 /* If we found the largest possible address, then
10683 read the base address. */
10684 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10685 found_base = 1;
10686 continue;
10687 }
10688
10689 if (!found_base)
10690 {
10691 /* We have no valid base address for the ranges
10692 data. */
10693 complaint (&symfile_complaints,
10694 _("Invalid .debug_ranges data (no base address)"));
10695 return 0;
10696 }
10697
9277c30c
UW
10698 if (range_beginning > range_end)
10699 {
10700 /* Inverted range entries are invalid. */
10701 complaint (&symfile_complaints,
10702 _("Invalid .debug_ranges data (inverted range)"));
10703 return 0;
10704 }
10705
10706 /* Empty range entries have no effect. */
10707 if (range_beginning == range_end)
10708 continue;
10709
43039443
JK
10710 range_beginning += base;
10711 range_end += base;
10712
01093045
DE
10713 /* A not-uncommon case of bad debug info.
10714 Don't pollute the addrmap with bad data. */
10715 if (range_beginning + baseaddr == 0
10716 && !dwarf2_per_objfile->has_section_at_zero)
10717 {
10718 complaint (&symfile_complaints,
10719 _(".debug_ranges entry has start address of zero"
4262abfb 10720 " [in module %s]"), objfile_name (objfile));
01093045
DE
10721 continue;
10722 }
10723
9277c30c 10724 if (ranges_pst != NULL)
ff013f42 10725 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10726 range_beginning + baseaddr,
10727 range_end - 1 + baseaddr,
ff013f42
JK
10728 ranges_pst);
10729
43039443
JK
10730 /* FIXME: This is recording everything as a low-high
10731 segment of consecutive addresses. We should have a
10732 data structure for discontiguous block ranges
10733 instead. */
10734 if (! low_set)
10735 {
10736 low = range_beginning;
10737 high = range_end;
10738 low_set = 1;
10739 }
10740 else
10741 {
10742 if (range_beginning < low)
10743 low = range_beginning;
10744 if (range_end > high)
10745 high = range_end;
10746 }
10747 }
10748
10749 if (! low_set)
10750 /* If the first entry is an end-of-list marker, the range
10751 describes an empty scope, i.e. no instructions. */
10752 return 0;
10753
10754 if (low_return)
10755 *low_return = low;
10756 if (high_return)
10757 *high_return = high;
10758 return 1;
10759}
10760
af34e669
DJ
10761/* Get low and high pc attributes from a die. Return 1 if the attributes
10762 are present and valid, otherwise, return 0. Return -1 if the range is
10763 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10764
c906108c 10765static int
af34e669 10766dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10767 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10768 struct partial_symtab *pst)
c906108c
SS
10769{
10770 struct attribute *attr;
91da1414 10771 struct attribute *attr_high;
af34e669
DJ
10772 CORE_ADDR low = 0;
10773 CORE_ADDR high = 0;
10774 int ret = 0;
c906108c 10775
91da1414
MW
10776 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10777 if (attr_high)
af34e669 10778 {
e142c38c 10779 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10780 if (attr)
91da1414
MW
10781 {
10782 low = DW_ADDR (attr);
3019eac3
DE
10783 if (attr_high->form == DW_FORM_addr
10784 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10785 high = DW_ADDR (attr_high);
10786 else
10787 high = low + DW_UNSND (attr_high);
10788 }
af34e669
DJ
10789 else
10790 /* Found high w/o low attribute. */
10791 return 0;
10792
10793 /* Found consecutive range of addresses. */
10794 ret = 1;
10795 }
c906108c 10796 else
af34e669 10797 {
e142c38c 10798 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10799 if (attr != NULL)
10800 {
ab435259
DE
10801 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10802 We take advantage of the fact that DW_AT_ranges does not appear
10803 in DW_TAG_compile_unit of DWO files. */
10804 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10805 unsigned int ranges_offset = (DW_UNSND (attr)
10806 + (need_ranges_base
10807 ? cu->ranges_base
10808 : 0));
2e3cf129 10809
af34e669 10810 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10811 .debug_ranges section. */
2e3cf129 10812 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10813 return 0;
43039443 10814 /* Found discontinuous range of addresses. */
af34e669
DJ
10815 ret = -1;
10816 }
10817 }
c906108c 10818
9373cf26
JK
10819 /* read_partial_die has also the strict LOW < HIGH requirement. */
10820 if (high <= low)
c906108c
SS
10821 return 0;
10822
10823 /* When using the GNU linker, .gnu.linkonce. sections are used to
10824 eliminate duplicate copies of functions and vtables and such.
10825 The linker will arbitrarily choose one and discard the others.
10826 The AT_*_pc values for such functions refer to local labels in
10827 these sections. If the section from that file was discarded, the
10828 labels are not in the output, so the relocs get a value of 0.
10829 If this is a discarded function, mark the pc bounds as invalid,
10830 so that GDB will ignore it. */
72dca2f5 10831 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10832 return 0;
10833
10834 *lowpc = low;
96408a79
SA
10835 if (highpc)
10836 *highpc = high;
af34e669 10837 return ret;
c906108c
SS
10838}
10839
b084d499
JB
10840/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10841 its low and high PC addresses. Do nothing if these addresses could not
10842 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10843 and HIGHPC to the high address if greater than HIGHPC. */
10844
10845static void
10846dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10847 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10848 struct dwarf2_cu *cu)
10849{
10850 CORE_ADDR low, high;
10851 struct die_info *child = die->child;
10852
d85a05f0 10853 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10854 {
10855 *lowpc = min (*lowpc, low);
10856 *highpc = max (*highpc, high);
10857 }
10858
10859 /* If the language does not allow nested subprograms (either inside
10860 subprograms or lexical blocks), we're done. */
10861 if (cu->language != language_ada)
10862 return;
6e70227d 10863
b084d499
JB
10864 /* Check all the children of the given DIE. If it contains nested
10865 subprograms, then check their pc bounds. Likewise, we need to
10866 check lexical blocks as well, as they may also contain subprogram
10867 definitions. */
10868 while (child && child->tag)
10869 {
10870 if (child->tag == DW_TAG_subprogram
10871 || child->tag == DW_TAG_lexical_block)
10872 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10873 child = sibling_die (child);
10874 }
10875}
10876
fae299cd
DC
10877/* Get the low and high pc's represented by the scope DIE, and store
10878 them in *LOWPC and *HIGHPC. If the correct values can't be
10879 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10880
10881static void
10882get_scope_pc_bounds (struct die_info *die,
10883 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10884 struct dwarf2_cu *cu)
10885{
10886 CORE_ADDR best_low = (CORE_ADDR) -1;
10887 CORE_ADDR best_high = (CORE_ADDR) 0;
10888 CORE_ADDR current_low, current_high;
10889
d85a05f0 10890 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10891 {
10892 best_low = current_low;
10893 best_high = current_high;
10894 }
10895 else
10896 {
10897 struct die_info *child = die->child;
10898
10899 while (child && child->tag)
10900 {
10901 switch (child->tag) {
10902 case DW_TAG_subprogram:
b084d499 10903 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10904 break;
10905 case DW_TAG_namespace:
f55ee35c 10906 case DW_TAG_module:
fae299cd
DC
10907 /* FIXME: carlton/2004-01-16: Should we do this for
10908 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10909 that current GCC's always emit the DIEs corresponding
10910 to definitions of methods of classes as children of a
10911 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10912 the DIEs giving the declarations, which could be
10913 anywhere). But I don't see any reason why the
10914 standards says that they have to be there. */
10915 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10916
10917 if (current_low != ((CORE_ADDR) -1))
10918 {
10919 best_low = min (best_low, current_low);
10920 best_high = max (best_high, current_high);
10921 }
10922 break;
10923 default:
0963b4bd 10924 /* Ignore. */
fae299cd
DC
10925 break;
10926 }
10927
10928 child = sibling_die (child);
10929 }
10930 }
10931
10932 *lowpc = best_low;
10933 *highpc = best_high;
10934}
10935
801e3a5b
JB
10936/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10937 in DIE. */
380bca97 10938
801e3a5b
JB
10939static void
10940dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10941 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10942{
bb5ed363 10943 struct objfile *objfile = cu->objfile;
801e3a5b 10944 struct attribute *attr;
91da1414 10945 struct attribute *attr_high;
801e3a5b 10946
91da1414
MW
10947 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10948 if (attr_high)
801e3a5b 10949 {
801e3a5b
JB
10950 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10951 if (attr)
10952 {
10953 CORE_ADDR low = DW_ADDR (attr);
91da1414 10954 CORE_ADDR high;
3019eac3
DE
10955 if (attr_high->form == DW_FORM_addr
10956 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10957 high = DW_ADDR (attr_high);
10958 else
10959 high = low + DW_UNSND (attr_high);
9a619af0 10960
801e3a5b
JB
10961 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10962 }
10963 }
10964
10965 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10966 if (attr)
10967 {
bb5ed363 10968 bfd *obfd = objfile->obfd;
ab435259
DE
10969 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10970 We take advantage of the fact that DW_AT_ranges does not appear
10971 in DW_TAG_compile_unit of DWO files. */
10972 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10973
10974 /* The value of the DW_AT_ranges attribute is the offset of the
10975 address range list in the .debug_ranges section. */
ab435259
DE
10976 unsigned long offset = (DW_UNSND (attr)
10977 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 10978 const gdb_byte *buffer;
801e3a5b
JB
10979
10980 /* For some target architectures, but not others, the
10981 read_address function sign-extends the addresses it returns.
10982 To recognize base address selection entries, we need a
10983 mask. */
10984 unsigned int addr_size = cu->header.addr_size;
10985 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10986
10987 /* The base address, to which the next pair is relative. Note
10988 that this 'base' is a DWARF concept: most entries in a range
10989 list are relative, to reduce the number of relocs against the
10990 debugging information. This is separate from this function's
10991 'baseaddr' argument, which GDB uses to relocate debugging
10992 information from a shared library based on the address at
10993 which the library was loaded. */
d00adf39
DE
10994 CORE_ADDR base = cu->base_address;
10995 int base_known = cu->base_known;
801e3a5b 10996
d62bfeaf 10997 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10998 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10999 {
11000 complaint (&symfile_complaints,
11001 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
11002 offset);
11003 return;
11004 }
d62bfeaf 11005 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
11006
11007 for (;;)
11008 {
11009 unsigned int bytes_read;
11010 CORE_ADDR start, end;
11011
11012 start = read_address (obfd, buffer, cu, &bytes_read);
11013 buffer += bytes_read;
11014 end = read_address (obfd, buffer, cu, &bytes_read);
11015 buffer += bytes_read;
11016
11017 /* Did we find the end of the range list? */
11018 if (start == 0 && end == 0)
11019 break;
11020
11021 /* Did we find a base address selection entry? */
11022 else if ((start & base_select_mask) == base_select_mask)
11023 {
11024 base = end;
11025 base_known = 1;
11026 }
11027
11028 /* We found an ordinary address range. */
11029 else
11030 {
11031 if (!base_known)
11032 {
11033 complaint (&symfile_complaints,
3e43a32a
MS
11034 _("Invalid .debug_ranges data "
11035 "(no base address)"));
801e3a5b
JB
11036 return;
11037 }
11038
9277c30c
UW
11039 if (start > end)
11040 {
11041 /* Inverted range entries are invalid. */
11042 complaint (&symfile_complaints,
11043 _("Invalid .debug_ranges data "
11044 "(inverted range)"));
11045 return;
11046 }
11047
11048 /* Empty range entries have no effect. */
11049 if (start == end)
11050 continue;
11051
01093045
DE
11052 start += base + baseaddr;
11053 end += base + baseaddr;
11054
11055 /* A not-uncommon case of bad debug info.
11056 Don't pollute the addrmap with bad data. */
11057 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11058 {
11059 complaint (&symfile_complaints,
11060 _(".debug_ranges entry has start address of zero"
4262abfb 11061 " [in module %s]"), objfile_name (objfile));
01093045
DE
11062 continue;
11063 }
11064
11065 record_block_range (block, start, end - 1);
801e3a5b
JB
11066 }
11067 }
11068 }
11069}
11070
685b1105
JK
11071/* Check whether the producer field indicates either of GCC < 4.6, or the
11072 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 11073
685b1105
JK
11074static void
11075check_producer (struct dwarf2_cu *cu)
60d5a603
JK
11076{
11077 const char *cs;
11078 int major, minor, release;
11079
11080 if (cu->producer == NULL)
11081 {
11082 /* For unknown compilers expect their behavior is DWARF version
11083 compliant.
11084
11085 GCC started to support .debug_types sections by -gdwarf-4 since
11086 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11087 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11088 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11089 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 11090 }
685b1105 11091 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 11092 {
685b1105
JK
11093 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11094
ba919b58
TT
11095 cs = &cu->producer[strlen ("GNU ")];
11096 while (*cs && !isdigit (*cs))
11097 cs++;
11098 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11099 {
11100 /* Not recognized as GCC. */
11101 }
11102 else
1b80a9fa
JK
11103 {
11104 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11105 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11106 }
685b1105
JK
11107 }
11108 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11109 cu->producer_is_icc = 1;
11110 else
11111 {
11112 /* For other non-GCC compilers, expect their behavior is DWARF version
11113 compliant. */
60d5a603
JK
11114 }
11115
ba919b58 11116 cu->checked_producer = 1;
685b1105 11117}
ba919b58 11118
685b1105
JK
11119/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11120 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11121 during 4.6.0 experimental. */
11122
11123static int
11124producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11125{
11126 if (!cu->checked_producer)
11127 check_producer (cu);
11128
11129 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
11130}
11131
11132/* Return the default accessibility type if it is not overriden by
11133 DW_AT_accessibility. */
11134
11135static enum dwarf_access_attribute
11136dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11137{
11138 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11139 {
11140 /* The default DWARF 2 accessibility for members is public, the default
11141 accessibility for inheritance is private. */
11142
11143 if (die->tag != DW_TAG_inheritance)
11144 return DW_ACCESS_public;
11145 else
11146 return DW_ACCESS_private;
11147 }
11148 else
11149 {
11150 /* DWARF 3+ defines the default accessibility a different way. The same
11151 rules apply now for DW_TAG_inheritance as for the members and it only
11152 depends on the container kind. */
11153
11154 if (die->parent->tag == DW_TAG_class_type)
11155 return DW_ACCESS_private;
11156 else
11157 return DW_ACCESS_public;
11158 }
11159}
11160
74ac6d43
TT
11161/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11162 offset. If the attribute was not found return 0, otherwise return
11163 1. If it was found but could not properly be handled, set *OFFSET
11164 to 0. */
11165
11166static int
11167handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11168 LONGEST *offset)
11169{
11170 struct attribute *attr;
11171
11172 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11173 if (attr != NULL)
11174 {
11175 *offset = 0;
11176
11177 /* Note that we do not check for a section offset first here.
11178 This is because DW_AT_data_member_location is new in DWARF 4,
11179 so if we see it, we can assume that a constant form is really
11180 a constant and not a section offset. */
11181 if (attr_form_is_constant (attr))
11182 *offset = dwarf2_get_attr_constant_value (attr, 0);
11183 else if (attr_form_is_section_offset (attr))
11184 dwarf2_complex_location_expr_complaint ();
11185 else if (attr_form_is_block (attr))
11186 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11187 else
11188 dwarf2_complex_location_expr_complaint ();
11189
11190 return 1;
11191 }
11192
11193 return 0;
11194}
11195
c906108c
SS
11196/* Add an aggregate field to the field list. */
11197
11198static void
107d2387 11199dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11200 struct dwarf2_cu *cu)
6e70227d 11201{
e7c27a73 11202 struct objfile *objfile = cu->objfile;
5e2b427d 11203 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11204 struct nextfield *new_field;
11205 struct attribute *attr;
11206 struct field *fp;
15d034d0 11207 const char *fieldname = "";
c906108c
SS
11208
11209 /* Allocate a new field list entry and link it in. */
11210 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11211 make_cleanup (xfree, new_field);
c906108c 11212 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11213
11214 if (die->tag == DW_TAG_inheritance)
11215 {
11216 new_field->next = fip->baseclasses;
11217 fip->baseclasses = new_field;
11218 }
11219 else
11220 {
11221 new_field->next = fip->fields;
11222 fip->fields = new_field;
11223 }
c906108c
SS
11224 fip->nfields++;
11225
e142c38c 11226 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11227 if (attr)
11228 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11229 else
11230 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11231 if (new_field->accessibility != DW_ACCESS_public)
11232 fip->non_public_fields = 1;
60d5a603 11233
e142c38c 11234 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11235 if (attr)
11236 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11237 else
11238 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11239
11240 fp = &new_field->field;
a9a9bd0f 11241
e142c38c 11242 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11243 {
74ac6d43
TT
11244 LONGEST offset;
11245
a9a9bd0f 11246 /* Data member other than a C++ static data member. */
6e70227d 11247
c906108c 11248 /* Get type of field. */
e7c27a73 11249 fp->type = die_type (die, cu);
c906108c 11250
d6a843b5 11251 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 11252
c906108c 11253 /* Get bit size of field (zero if none). */
e142c38c 11254 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
11255 if (attr)
11256 {
11257 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11258 }
11259 else
11260 {
11261 FIELD_BITSIZE (*fp) = 0;
11262 }
11263
11264 /* Get bit offset of field. */
74ac6d43
TT
11265 if (handle_data_member_location (die, cu, &offset))
11266 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 11267 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
11268 if (attr)
11269 {
5e2b427d 11270 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
11271 {
11272 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
11273 additional bit offset from the MSB of the containing
11274 anonymous object to the MSB of the field. We don't
11275 have to do anything special since we don't need to
11276 know the size of the anonymous object. */
f41f5e61 11277 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
11278 }
11279 else
11280 {
11281 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
11282 MSB of the anonymous object, subtract off the number of
11283 bits from the MSB of the field to the MSB of the
11284 object, and then subtract off the number of bits of
11285 the field itself. The result is the bit offset of
11286 the LSB of the field. */
c906108c
SS
11287 int anonymous_size;
11288 int bit_offset = DW_UNSND (attr);
11289
e142c38c 11290 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11291 if (attr)
11292 {
11293 /* The size of the anonymous object containing
11294 the bit field is explicit, so use the
11295 indicated size (in bytes). */
11296 anonymous_size = DW_UNSND (attr);
11297 }
11298 else
11299 {
11300 /* The size of the anonymous object containing
11301 the bit field must be inferred from the type
11302 attribute of the data member containing the
11303 bit field. */
11304 anonymous_size = TYPE_LENGTH (fp->type);
11305 }
f41f5e61
PA
11306 SET_FIELD_BITPOS (*fp,
11307 (FIELD_BITPOS (*fp)
11308 + anonymous_size * bits_per_byte
11309 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
11310 }
11311 }
11312
11313 /* Get name of field. */
39cbfefa
DJ
11314 fieldname = dwarf2_name (die, cu);
11315 if (fieldname == NULL)
11316 fieldname = "";
d8151005
DJ
11317
11318 /* The name is already allocated along with this objfile, so we don't
11319 need to duplicate it for the type. */
11320 fp->name = fieldname;
c906108c
SS
11321
11322 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 11323 pointer or virtual base class pointer) to private. */
e142c38c 11324 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 11325 {
d48cc9dd 11326 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
11327 new_field->accessibility = DW_ACCESS_private;
11328 fip->non_public_fields = 1;
11329 }
11330 }
a9a9bd0f 11331 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 11332 {
a9a9bd0f
DC
11333 /* C++ static member. */
11334
11335 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11336 is a declaration, but all versions of G++ as of this writing
11337 (so through at least 3.2.1) incorrectly generate
11338 DW_TAG_variable tags. */
6e70227d 11339
ff355380 11340 const char *physname;
c906108c 11341
a9a9bd0f 11342 /* Get name of field. */
39cbfefa
DJ
11343 fieldname = dwarf2_name (die, cu);
11344 if (fieldname == NULL)
c906108c
SS
11345 return;
11346
254e6b9e 11347 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
11348 if (attr
11349 /* Only create a symbol if this is an external value.
11350 new_symbol checks this and puts the value in the global symbol
11351 table, which we want. If it is not external, new_symbol
11352 will try to put the value in cu->list_in_scope which is wrong. */
11353 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
11354 {
11355 /* A static const member, not much different than an enum as far as
11356 we're concerned, except that we can support more types. */
11357 new_symbol (die, NULL, cu);
11358 }
11359
2df3850c 11360 /* Get physical name. */
ff355380 11361 physname = dwarf2_physname (fieldname, die, cu);
c906108c 11362
d8151005
DJ
11363 /* The name is already allocated along with this objfile, so we don't
11364 need to duplicate it for the type. */
11365 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 11366 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 11367 FIELD_NAME (*fp) = fieldname;
c906108c
SS
11368 }
11369 else if (die->tag == DW_TAG_inheritance)
11370 {
74ac6d43 11371 LONGEST offset;
d4b96c9a 11372
74ac6d43
TT
11373 /* C++ base class field. */
11374 if (handle_data_member_location (die, cu, &offset))
11375 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 11376 FIELD_BITSIZE (*fp) = 0;
e7c27a73 11377 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
11378 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11379 fip->nbaseclasses++;
11380 }
11381}
11382
98751a41
JK
11383/* Add a typedef defined in the scope of the FIP's class. */
11384
11385static void
11386dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11387 struct dwarf2_cu *cu)
6e70227d 11388{
98751a41 11389 struct objfile *objfile = cu->objfile;
98751a41
JK
11390 struct typedef_field_list *new_field;
11391 struct attribute *attr;
11392 struct typedef_field *fp;
11393 char *fieldname = "";
11394
11395 /* Allocate a new field list entry and link it in. */
11396 new_field = xzalloc (sizeof (*new_field));
11397 make_cleanup (xfree, new_field);
11398
11399 gdb_assert (die->tag == DW_TAG_typedef);
11400
11401 fp = &new_field->field;
11402
11403 /* Get name of field. */
11404 fp->name = dwarf2_name (die, cu);
11405 if (fp->name == NULL)
11406 return;
11407
11408 fp->type = read_type_die (die, cu);
11409
11410 new_field->next = fip->typedef_field_list;
11411 fip->typedef_field_list = new_field;
11412 fip->typedef_field_list_count++;
11413}
11414
c906108c
SS
11415/* Create the vector of fields, and attach it to the type. */
11416
11417static void
fba45db2 11418dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11419 struct dwarf2_cu *cu)
c906108c
SS
11420{
11421 int nfields = fip->nfields;
11422
11423 /* Record the field count, allocate space for the array of fields,
11424 and create blank accessibility bitfields if necessary. */
11425 TYPE_NFIELDS (type) = nfields;
11426 TYPE_FIELDS (type) = (struct field *)
11427 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11428 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11429
b4ba55a1 11430 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
11431 {
11432 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11433
11434 TYPE_FIELD_PRIVATE_BITS (type) =
11435 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11436 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11437
11438 TYPE_FIELD_PROTECTED_BITS (type) =
11439 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11440 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11441
774b6a14
TT
11442 TYPE_FIELD_IGNORE_BITS (type) =
11443 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11444 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
11445 }
11446
11447 /* If the type has baseclasses, allocate and clear a bit vector for
11448 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 11449 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
11450 {
11451 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 11452 unsigned char *pointer;
c906108c
SS
11453
11454 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
11455 pointer = TYPE_ALLOC (type, num_bytes);
11456 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
11457 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11458 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11459 }
11460
3e43a32a
MS
11461 /* Copy the saved-up fields into the field vector. Start from the head of
11462 the list, adding to the tail of the field array, so that they end up in
11463 the same order in the array in which they were added to the list. */
c906108c
SS
11464 while (nfields-- > 0)
11465 {
7d0ccb61
DJ
11466 struct nextfield *fieldp;
11467
11468 if (fip->fields)
11469 {
11470 fieldp = fip->fields;
11471 fip->fields = fieldp->next;
11472 }
11473 else
11474 {
11475 fieldp = fip->baseclasses;
11476 fip->baseclasses = fieldp->next;
11477 }
11478
11479 TYPE_FIELD (type, nfields) = fieldp->field;
11480 switch (fieldp->accessibility)
c906108c 11481 {
c5aa993b 11482 case DW_ACCESS_private:
b4ba55a1
JB
11483 if (cu->language != language_ada)
11484 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11485 break;
c906108c 11486
c5aa993b 11487 case DW_ACCESS_protected:
b4ba55a1
JB
11488 if (cu->language != language_ada)
11489 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11490 break;
c906108c 11491
c5aa993b
JM
11492 case DW_ACCESS_public:
11493 break;
c906108c 11494
c5aa993b
JM
11495 default:
11496 /* Unknown accessibility. Complain and treat it as public. */
11497 {
e2e0b3e5 11498 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11499 fieldp->accessibility);
c5aa993b
JM
11500 }
11501 break;
c906108c
SS
11502 }
11503 if (nfields < fip->nbaseclasses)
11504 {
7d0ccb61 11505 switch (fieldp->virtuality)
c906108c 11506 {
c5aa993b
JM
11507 case DW_VIRTUALITY_virtual:
11508 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11509 if (cu->language == language_ada)
a73c6dcd 11510 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11511 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11512 break;
c906108c
SS
11513 }
11514 }
c906108c
SS
11515 }
11516}
11517
7d27a96d
TT
11518/* Return true if this member function is a constructor, false
11519 otherwise. */
11520
11521static int
11522dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11523{
11524 const char *fieldname;
11525 const char *typename;
11526 int len;
11527
11528 if (die->parent == NULL)
11529 return 0;
11530
11531 if (die->parent->tag != DW_TAG_structure_type
11532 && die->parent->tag != DW_TAG_union_type
11533 && die->parent->tag != DW_TAG_class_type)
11534 return 0;
11535
11536 fieldname = dwarf2_name (die, cu);
11537 typename = dwarf2_name (die->parent, cu);
11538 if (fieldname == NULL || typename == NULL)
11539 return 0;
11540
11541 len = strlen (fieldname);
11542 return (strncmp (fieldname, typename, len) == 0
11543 && (typename[len] == '\0' || typename[len] == '<'));
11544}
11545
c906108c
SS
11546/* Add a member function to the proper fieldlist. */
11547
11548static void
107d2387 11549dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11550 struct type *type, struct dwarf2_cu *cu)
c906108c 11551{
e7c27a73 11552 struct objfile *objfile = cu->objfile;
c906108c
SS
11553 struct attribute *attr;
11554 struct fnfieldlist *flp;
11555 int i;
11556 struct fn_field *fnp;
15d034d0 11557 const char *fieldname;
c906108c 11558 struct nextfnfield *new_fnfield;
f792889a 11559 struct type *this_type;
60d5a603 11560 enum dwarf_access_attribute accessibility;
c906108c 11561
b4ba55a1 11562 if (cu->language == language_ada)
a73c6dcd 11563 error (_("unexpected member function in Ada type"));
b4ba55a1 11564
2df3850c 11565 /* Get name of member function. */
39cbfefa
DJ
11566 fieldname = dwarf2_name (die, cu);
11567 if (fieldname == NULL)
2df3850c 11568 return;
c906108c 11569
c906108c
SS
11570 /* Look up member function name in fieldlist. */
11571 for (i = 0; i < fip->nfnfields; i++)
11572 {
27bfe10e 11573 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11574 break;
11575 }
11576
11577 /* Create new list element if necessary. */
11578 if (i < fip->nfnfields)
11579 flp = &fip->fnfieldlists[i];
11580 else
11581 {
11582 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11583 {
11584 fip->fnfieldlists = (struct fnfieldlist *)
11585 xrealloc (fip->fnfieldlists,
11586 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11587 * sizeof (struct fnfieldlist));
c906108c 11588 if (fip->nfnfields == 0)
c13c43fd 11589 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11590 }
11591 flp = &fip->fnfieldlists[fip->nfnfields];
11592 flp->name = fieldname;
11593 flp->length = 0;
11594 flp->head = NULL;
3da10d80 11595 i = fip->nfnfields++;
c906108c
SS
11596 }
11597
11598 /* Create a new member function field and chain it to the field list
0963b4bd 11599 entry. */
c906108c 11600 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11601 make_cleanup (xfree, new_fnfield);
c906108c
SS
11602 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11603 new_fnfield->next = flp->head;
11604 flp->head = new_fnfield;
11605 flp->length++;
11606
11607 /* Fill in the member function field info. */
11608 fnp = &new_fnfield->fnfield;
3da10d80
KS
11609
11610 /* Delay processing of the physname until later. */
11611 if (cu->language == language_cplus || cu->language == language_java)
11612 {
11613 add_to_method_list (type, i, flp->length - 1, fieldname,
11614 die, cu);
11615 }
11616 else
11617 {
1d06ead6 11618 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11619 fnp->physname = physname ? physname : "";
11620 }
11621
c906108c 11622 fnp->type = alloc_type (objfile);
f792889a
DJ
11623 this_type = read_type_die (die, cu);
11624 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11625 {
f792889a 11626 int nparams = TYPE_NFIELDS (this_type);
c906108c 11627
f792889a 11628 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11629 of the method itself (TYPE_CODE_METHOD). */
11630 smash_to_method_type (fnp->type, type,
f792889a
DJ
11631 TYPE_TARGET_TYPE (this_type),
11632 TYPE_FIELDS (this_type),
11633 TYPE_NFIELDS (this_type),
11634 TYPE_VARARGS (this_type));
c906108c
SS
11635
11636 /* Handle static member functions.
c5aa993b 11637 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11638 member functions. G++ helps GDB by marking the first
11639 parameter for non-static member functions (which is the this
11640 pointer) as artificial. We obtain this information from
11641 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11642 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11643 fnp->voffset = VOFFSET_STATIC;
11644 }
11645 else
e2e0b3e5 11646 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11647 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11648
11649 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11650 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11651 fnp->fcontext = die_containing_type (die, cu);
c906108c 11652
3e43a32a
MS
11653 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11654 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11655
11656 /* Get accessibility. */
e142c38c 11657 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11658 if (attr)
60d5a603
JK
11659 accessibility = DW_UNSND (attr);
11660 else
11661 accessibility = dwarf2_default_access_attribute (die, cu);
11662 switch (accessibility)
c906108c 11663 {
60d5a603
JK
11664 case DW_ACCESS_private:
11665 fnp->is_private = 1;
11666 break;
11667 case DW_ACCESS_protected:
11668 fnp->is_protected = 1;
11669 break;
c906108c
SS
11670 }
11671
b02dede2 11672 /* Check for artificial methods. */
e142c38c 11673 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11674 if (attr && DW_UNSND (attr) != 0)
11675 fnp->is_artificial = 1;
11676
7d27a96d
TT
11677 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11678
0d564a31 11679 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11680 function. For older versions of GCC, this is an offset in the
11681 appropriate virtual table, as specified by DW_AT_containing_type.
11682 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11683 to the object address. */
11684
e142c38c 11685 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11686 if (attr)
8e19ed76 11687 {
aec5aa8b 11688 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11689 {
aec5aa8b
TT
11690 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11691 {
11692 /* Old-style GCC. */
11693 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11694 }
11695 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11696 || (DW_BLOCK (attr)->size > 1
11697 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11698 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11699 {
11700 struct dwarf_block blk;
11701 int offset;
11702
11703 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11704 ? 1 : 2);
11705 blk.size = DW_BLOCK (attr)->size - offset;
11706 blk.data = DW_BLOCK (attr)->data + offset;
11707 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11708 if ((fnp->voffset % cu->header.addr_size) != 0)
11709 dwarf2_complex_location_expr_complaint ();
11710 else
11711 fnp->voffset /= cu->header.addr_size;
11712 fnp->voffset += 2;
11713 }
11714 else
11715 dwarf2_complex_location_expr_complaint ();
11716
11717 if (!fnp->fcontext)
11718 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11719 }
3690dd37 11720 else if (attr_form_is_section_offset (attr))
8e19ed76 11721 {
4d3c2250 11722 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11723 }
11724 else
11725 {
4d3c2250
KB
11726 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11727 fieldname);
8e19ed76 11728 }
0d564a31 11729 }
d48cc9dd
DJ
11730 else
11731 {
11732 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11733 if (attr && DW_UNSND (attr))
11734 {
11735 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11736 complaint (&symfile_complaints,
3e43a32a
MS
11737 _("Member function \"%s\" (offset %d) is virtual "
11738 "but the vtable offset is not specified"),
b64f50a1 11739 fieldname, die->offset.sect_off);
9655fd1a 11740 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11741 TYPE_CPLUS_DYNAMIC (type) = 1;
11742 }
11743 }
c906108c
SS
11744}
11745
11746/* Create the vector of member function fields, and attach it to the type. */
11747
11748static void
fba45db2 11749dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11750 struct dwarf2_cu *cu)
c906108c
SS
11751{
11752 struct fnfieldlist *flp;
c906108c
SS
11753 int i;
11754
b4ba55a1 11755 if (cu->language == language_ada)
a73c6dcd 11756 error (_("unexpected member functions in Ada type"));
b4ba55a1 11757
c906108c
SS
11758 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11759 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11760 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11761
11762 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11763 {
11764 struct nextfnfield *nfp = flp->head;
11765 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11766 int k;
11767
11768 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11769 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11770 fn_flp->fn_fields = (struct fn_field *)
11771 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11772 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11773 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11774 }
11775
11776 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11777}
11778
1168df01
JB
11779/* Returns non-zero if NAME is the name of a vtable member in CU's
11780 language, zero otherwise. */
11781static int
11782is_vtable_name (const char *name, struct dwarf2_cu *cu)
11783{
11784 static const char vptr[] = "_vptr";
987504bb 11785 static const char vtable[] = "vtable";
1168df01 11786
987504bb
JJ
11787 /* Look for the C++ and Java forms of the vtable. */
11788 if ((cu->language == language_java
11789 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11790 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11791 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11792 return 1;
11793
11794 return 0;
11795}
11796
c0dd20ea 11797/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11798 functions, with the ABI-specified layout. If TYPE describes
11799 such a structure, smash it into a member function type.
61049d3b
DJ
11800
11801 GCC shouldn't do this; it should just output pointer to member DIEs.
11802 This is GCC PR debug/28767. */
c0dd20ea 11803
0b92b5bb
TT
11804static void
11805quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11806{
0b92b5bb 11807 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11808
11809 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11810 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11811 return;
c0dd20ea
DJ
11812
11813 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11814 if (TYPE_FIELD_NAME (type, 0) == NULL
11815 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11816 || TYPE_FIELD_NAME (type, 1) == NULL
11817 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11818 return;
c0dd20ea
DJ
11819
11820 /* Find the type of the method. */
0b92b5bb 11821 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11822 if (pfn_type == NULL
11823 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11824 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11825 return;
c0dd20ea
DJ
11826
11827 /* Look for the "this" argument. */
11828 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11829 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11830 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11831 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11832 return;
c0dd20ea
DJ
11833
11834 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11835 new_type = alloc_type (objfile);
11836 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11837 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11838 TYPE_VARARGS (pfn_type));
0b92b5bb 11839 smash_to_methodptr_type (type, new_type);
c0dd20ea 11840}
1168df01 11841
685b1105
JK
11842/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11843 (icc). */
11844
11845static int
11846producer_is_icc (struct dwarf2_cu *cu)
11847{
11848 if (!cu->checked_producer)
11849 check_producer (cu);
11850
11851 return cu->producer_is_icc;
11852}
11853
c906108c 11854/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11855 (definition) to create a type for the structure or union. Fill in
11856 the type's name and general properties; the members will not be
3d1d5ea3 11857 processed until process_structure_scope.
c906108c 11858
c767944b
DJ
11859 NOTE: we need to call these functions regardless of whether or not the
11860 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11861 structure or union. This gets the type entered into our set of
11862 user defined types.
11863
11864 However, if the structure is incomplete (an opaque struct/union)
11865 then suppress creating a symbol table entry for it since gdb only
11866 wants to find the one with the complete definition. Note that if
11867 it is complete, we just call new_symbol, which does it's own
11868 checking about whether the struct/union is anonymous or not (and
11869 suppresses creating a symbol table entry itself). */
11870
f792889a 11871static struct type *
134d01f1 11872read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11873{
e7c27a73 11874 struct objfile *objfile = cu->objfile;
c906108c
SS
11875 struct type *type;
11876 struct attribute *attr;
15d034d0 11877 const char *name;
c906108c 11878
348e048f
DE
11879 /* If the definition of this type lives in .debug_types, read that type.
11880 Don't follow DW_AT_specification though, that will take us back up
11881 the chain and we want to go down. */
45e58e77 11882 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11883 if (attr)
11884 {
ac9ec31b 11885 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11886
ac9ec31b 11887 /* The type's CU may not be the same as CU.
02142a6c 11888 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11889 return set_die_type (die, type, cu);
11890 }
11891
c0dd20ea 11892 type = alloc_type (objfile);
c906108c 11893 INIT_CPLUS_SPECIFIC (type);
93311388 11894
39cbfefa
DJ
11895 name = dwarf2_name (die, cu);
11896 if (name != NULL)
c906108c 11897 {
987504bb
JJ
11898 if (cu->language == language_cplus
11899 || cu->language == language_java)
63d06c5c 11900 {
15d034d0 11901 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11902
11903 /* dwarf2_full_name might have already finished building the DIE's
11904 type. If so, there is no need to continue. */
11905 if (get_die_type (die, cu) != NULL)
11906 return get_die_type (die, cu);
11907
11908 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11909 if (die->tag == DW_TAG_structure_type
11910 || die->tag == DW_TAG_class_type)
11911 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11912 }
11913 else
11914 {
d8151005
DJ
11915 /* The name is already allocated along with this objfile, so
11916 we don't need to duplicate it for the type. */
7d455152 11917 TYPE_TAG_NAME (type) = name;
94af9270
KS
11918 if (die->tag == DW_TAG_class_type)
11919 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11920 }
c906108c
SS
11921 }
11922
11923 if (die->tag == DW_TAG_structure_type)
11924 {
11925 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11926 }
11927 else if (die->tag == DW_TAG_union_type)
11928 {
11929 TYPE_CODE (type) = TYPE_CODE_UNION;
11930 }
11931 else
11932 {
c906108c
SS
11933 TYPE_CODE (type) = TYPE_CODE_CLASS;
11934 }
11935
0cc2414c
TT
11936 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11937 TYPE_DECLARED_CLASS (type) = 1;
11938
e142c38c 11939 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11940 if (attr)
11941 {
11942 TYPE_LENGTH (type) = DW_UNSND (attr);
11943 }
11944 else
11945 {
11946 TYPE_LENGTH (type) = 0;
11947 }
11948
685b1105
JK
11949 if (producer_is_icc (cu))
11950 {
11951 /* ICC does not output the required DW_AT_declaration
11952 on incomplete types, but gives them a size of zero. */
11953 }
11954 else
11955 TYPE_STUB_SUPPORTED (type) = 1;
11956
dc718098 11957 if (die_is_declaration (die, cu))
876cecd0 11958 TYPE_STUB (type) = 1;
a6c727b2
DJ
11959 else if (attr == NULL && die->child == NULL
11960 && producer_is_realview (cu->producer))
11961 /* RealView does not output the required DW_AT_declaration
11962 on incomplete types. */
11963 TYPE_STUB (type) = 1;
dc718098 11964
c906108c
SS
11965 /* We need to add the type field to the die immediately so we don't
11966 infinitely recurse when dealing with pointers to the structure
0963b4bd 11967 type within the structure itself. */
1c379e20 11968 set_die_type (die, type, cu);
c906108c 11969
7e314c57
JK
11970 /* set_die_type should be already done. */
11971 set_descriptive_type (type, die, cu);
11972
c767944b
DJ
11973 return type;
11974}
11975
11976/* Finish creating a structure or union type, including filling in
11977 its members and creating a symbol for it. */
11978
11979static void
11980process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11981{
11982 struct objfile *objfile = cu->objfile;
11983 struct die_info *child_die = die->child;
11984 struct type *type;
11985
11986 type = get_die_type (die, cu);
11987 if (type == NULL)
11988 type = read_structure_type (die, cu);
11989
e142c38c 11990 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11991 {
11992 struct field_info fi;
11993 struct die_info *child_die;
34eaf542 11994 VEC (symbolp) *template_args = NULL;
c767944b 11995 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11996
11997 memset (&fi, 0, sizeof (struct field_info));
11998
639d11d3 11999 child_die = die->child;
c906108c
SS
12000
12001 while (child_die && child_die->tag)
12002 {
a9a9bd0f
DC
12003 if (child_die->tag == DW_TAG_member
12004 || child_die->tag == DW_TAG_variable)
c906108c 12005 {
a9a9bd0f
DC
12006 /* NOTE: carlton/2002-11-05: A C++ static data member
12007 should be a DW_TAG_member that is a declaration, but
12008 all versions of G++ as of this writing (so through at
12009 least 3.2.1) incorrectly generate DW_TAG_variable
12010 tags for them instead. */
e7c27a73 12011 dwarf2_add_field (&fi, child_die, cu);
c906108c 12012 }
8713b1b1 12013 else if (child_die->tag == DW_TAG_subprogram)
c906108c 12014 {
0963b4bd 12015 /* C++ member function. */
e7c27a73 12016 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
12017 }
12018 else if (child_die->tag == DW_TAG_inheritance)
12019 {
12020 /* C++ base class field. */
e7c27a73 12021 dwarf2_add_field (&fi, child_die, cu);
c906108c 12022 }
98751a41
JK
12023 else if (child_die->tag == DW_TAG_typedef)
12024 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
12025 else if (child_die->tag == DW_TAG_template_type_param
12026 || child_die->tag == DW_TAG_template_value_param)
12027 {
12028 struct symbol *arg = new_symbol (child_die, NULL, cu);
12029
f1078f66
DJ
12030 if (arg != NULL)
12031 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
12032 }
12033
c906108c
SS
12034 child_die = sibling_die (child_die);
12035 }
12036
34eaf542
TT
12037 /* Attach template arguments to type. */
12038 if (! VEC_empty (symbolp, template_args))
12039 {
12040 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12041 TYPE_N_TEMPLATE_ARGUMENTS (type)
12042 = VEC_length (symbolp, template_args);
12043 TYPE_TEMPLATE_ARGUMENTS (type)
12044 = obstack_alloc (&objfile->objfile_obstack,
12045 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12046 * sizeof (struct symbol *)));
12047 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12048 VEC_address (symbolp, template_args),
12049 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12050 * sizeof (struct symbol *)));
12051 VEC_free (symbolp, template_args);
12052 }
12053
c906108c
SS
12054 /* Attach fields and member functions to the type. */
12055 if (fi.nfields)
e7c27a73 12056 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
12057 if (fi.nfnfields)
12058 {
e7c27a73 12059 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 12060
c5aa993b 12061 /* Get the type which refers to the base class (possibly this
c906108c 12062 class itself) which contains the vtable pointer for the current
0d564a31
DJ
12063 class from the DW_AT_containing_type attribute. This use of
12064 DW_AT_containing_type is a GNU extension. */
c906108c 12065
e142c38c 12066 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 12067 {
e7c27a73 12068 struct type *t = die_containing_type (die, cu);
c906108c
SS
12069
12070 TYPE_VPTR_BASETYPE (type) = t;
12071 if (type == t)
12072 {
c906108c
SS
12073 int i;
12074
12075 /* Our own class provides vtbl ptr. */
12076 for (i = TYPE_NFIELDS (t) - 1;
12077 i >= TYPE_N_BASECLASSES (t);
12078 --i)
12079 {
0d5cff50 12080 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 12081
1168df01 12082 if (is_vtable_name (fieldname, cu))
c906108c
SS
12083 {
12084 TYPE_VPTR_FIELDNO (type) = i;
12085 break;
12086 }
12087 }
12088
12089 /* Complain if virtual function table field not found. */
12090 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 12091 complaint (&symfile_complaints,
3e43a32a
MS
12092 _("virtual function table pointer "
12093 "not found when defining class '%s'"),
4d3c2250
KB
12094 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12095 "");
c906108c
SS
12096 }
12097 else
12098 {
12099 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12100 }
12101 }
f6235d4c
EZ
12102 else if (cu->producer
12103 && strncmp (cu->producer,
12104 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12105 {
12106 /* The IBM XLC compiler does not provide direct indication
12107 of the containing type, but the vtable pointer is
12108 always named __vfp. */
12109
12110 int i;
12111
12112 for (i = TYPE_NFIELDS (type) - 1;
12113 i >= TYPE_N_BASECLASSES (type);
12114 --i)
12115 {
12116 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12117 {
12118 TYPE_VPTR_FIELDNO (type) = i;
12119 TYPE_VPTR_BASETYPE (type) = type;
12120 break;
12121 }
12122 }
12123 }
c906108c 12124 }
98751a41
JK
12125
12126 /* Copy fi.typedef_field_list linked list elements content into the
12127 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12128 if (fi.typedef_field_list)
12129 {
12130 int i = fi.typedef_field_list_count;
12131
a0d7a4ff 12132 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
12133 TYPE_TYPEDEF_FIELD_ARRAY (type)
12134 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12135 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12136
12137 /* Reverse the list order to keep the debug info elements order. */
12138 while (--i >= 0)
12139 {
12140 struct typedef_field *dest, *src;
6e70227d 12141
98751a41
JK
12142 dest = &TYPE_TYPEDEF_FIELD (type, i);
12143 src = &fi.typedef_field_list->field;
12144 fi.typedef_field_list = fi.typedef_field_list->next;
12145 *dest = *src;
12146 }
12147 }
c767944b
DJ
12148
12149 do_cleanups (back_to);
eb2a6f42
TT
12150
12151 if (HAVE_CPLUS_STRUCT (type))
12152 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12153 }
63d06c5c 12154
bb5ed363 12155 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12156
90aeadfc
DC
12157 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12158 snapshots) has been known to create a die giving a declaration
12159 for a class that has, as a child, a die giving a definition for a
12160 nested class. So we have to process our children even if the
12161 current die is a declaration. Normally, of course, a declaration
12162 won't have any children at all. */
134d01f1 12163
90aeadfc
DC
12164 while (child_die != NULL && child_die->tag)
12165 {
12166 if (child_die->tag == DW_TAG_member
12167 || child_die->tag == DW_TAG_variable
34eaf542
TT
12168 || child_die->tag == DW_TAG_inheritance
12169 || child_die->tag == DW_TAG_template_value_param
12170 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12171 {
90aeadfc 12172 /* Do nothing. */
134d01f1 12173 }
90aeadfc
DC
12174 else
12175 process_die (child_die, cu);
134d01f1 12176
90aeadfc 12177 child_die = sibling_die (child_die);
134d01f1
DJ
12178 }
12179
fa4028e9
JB
12180 /* Do not consider external references. According to the DWARF standard,
12181 these DIEs are identified by the fact that they have no byte_size
12182 attribute, and a declaration attribute. */
12183 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12184 || !die_is_declaration (die, cu))
c767944b 12185 new_symbol (die, type, cu);
134d01f1
DJ
12186}
12187
12188/* Given a DW_AT_enumeration_type die, set its type. We do not
12189 complete the type's fields yet, or create any symbols. */
c906108c 12190
f792889a 12191static struct type *
134d01f1 12192read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12193{
e7c27a73 12194 struct objfile *objfile = cu->objfile;
c906108c 12195 struct type *type;
c906108c 12196 struct attribute *attr;
0114d602 12197 const char *name;
134d01f1 12198
348e048f
DE
12199 /* If the definition of this type lives in .debug_types, read that type.
12200 Don't follow DW_AT_specification though, that will take us back up
12201 the chain and we want to go down. */
45e58e77 12202 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12203 if (attr)
12204 {
ac9ec31b 12205 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12206
ac9ec31b 12207 /* The type's CU may not be the same as CU.
02142a6c 12208 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12209 return set_die_type (die, type, cu);
12210 }
12211
c906108c
SS
12212 type = alloc_type (objfile);
12213
12214 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12215 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12216 if (name != NULL)
7d455152 12217 TYPE_TAG_NAME (type) = name;
c906108c 12218
e142c38c 12219 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12220 if (attr)
12221 {
12222 TYPE_LENGTH (type) = DW_UNSND (attr);
12223 }
12224 else
12225 {
12226 TYPE_LENGTH (type) = 0;
12227 }
12228
137033e9
JB
12229 /* The enumeration DIE can be incomplete. In Ada, any type can be
12230 declared as private in the package spec, and then defined only
12231 inside the package body. Such types are known as Taft Amendment
12232 Types. When another package uses such a type, an incomplete DIE
12233 may be generated by the compiler. */
02eb380e 12234 if (die_is_declaration (die, cu))
876cecd0 12235 TYPE_STUB (type) = 1;
02eb380e 12236
f792889a 12237 return set_die_type (die, type, cu);
134d01f1
DJ
12238}
12239
12240/* Given a pointer to a die which begins an enumeration, process all
12241 the dies that define the members of the enumeration, and create the
12242 symbol for the enumeration type.
12243
12244 NOTE: We reverse the order of the element list. */
12245
12246static void
12247process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12248{
f792889a 12249 struct type *this_type;
134d01f1 12250
f792889a
DJ
12251 this_type = get_die_type (die, cu);
12252 if (this_type == NULL)
12253 this_type = read_enumeration_type (die, cu);
9dc481d3 12254
639d11d3 12255 if (die->child != NULL)
c906108c 12256 {
9dc481d3
DE
12257 struct die_info *child_die;
12258 struct symbol *sym;
12259 struct field *fields = NULL;
12260 int num_fields = 0;
12261 int unsigned_enum = 1;
15d034d0 12262 const char *name;
cafec441
TT
12263 int flag_enum = 1;
12264 ULONGEST mask = 0;
9dc481d3 12265
639d11d3 12266 child_die = die->child;
c906108c
SS
12267 while (child_die && child_die->tag)
12268 {
12269 if (child_die->tag != DW_TAG_enumerator)
12270 {
e7c27a73 12271 process_die (child_die, cu);
c906108c
SS
12272 }
12273 else
12274 {
39cbfefa
DJ
12275 name = dwarf2_name (child_die, cu);
12276 if (name)
c906108c 12277 {
f792889a 12278 sym = new_symbol (child_die, this_type, cu);
c906108c 12279 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
12280 {
12281 unsigned_enum = 0;
12282 flag_enum = 0;
12283 }
12284 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12285 flag_enum = 0;
12286 else
12287 mask |= SYMBOL_VALUE (sym);
c906108c
SS
12288
12289 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12290 {
12291 fields = (struct field *)
12292 xrealloc (fields,
12293 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12294 * sizeof (struct field));
c906108c
SS
12295 }
12296
3567439c 12297 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 12298 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 12299 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
12300 FIELD_BITSIZE (fields[num_fields]) = 0;
12301
12302 num_fields++;
12303 }
12304 }
12305
12306 child_die = sibling_die (child_die);
12307 }
12308
12309 if (num_fields)
12310 {
f792889a
DJ
12311 TYPE_NFIELDS (this_type) = num_fields;
12312 TYPE_FIELDS (this_type) = (struct field *)
12313 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12314 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 12315 sizeof (struct field) * num_fields);
b8c9b27d 12316 xfree (fields);
c906108c
SS
12317 }
12318 if (unsigned_enum)
876cecd0 12319 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
12320 if (flag_enum)
12321 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 12322 }
134d01f1 12323
6c83ed52
TT
12324 /* If we are reading an enum from a .debug_types unit, and the enum
12325 is a declaration, and the enum is not the signatured type in the
12326 unit, then we do not want to add a symbol for it. Adding a
12327 symbol would in some cases obscure the true definition of the
12328 enum, giving users an incomplete type when the definition is
12329 actually available. Note that we do not want to do this for all
12330 enums which are just declarations, because C++0x allows forward
12331 enum declarations. */
3019eac3 12332 if (cu->per_cu->is_debug_types
6c83ed52
TT
12333 && die_is_declaration (die, cu))
12334 {
52dc124a 12335 struct signatured_type *sig_type;
6c83ed52 12336
c0f78cd4 12337 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
12338 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12339 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
12340 return;
12341 }
12342
f792889a 12343 new_symbol (die, this_type, cu);
c906108c
SS
12344}
12345
12346/* Extract all information from a DW_TAG_array_type DIE and put it in
12347 the DIE's type field. For now, this only handles one dimensional
12348 arrays. */
12349
f792889a 12350static struct type *
e7c27a73 12351read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12352{
e7c27a73 12353 struct objfile *objfile = cu->objfile;
c906108c 12354 struct die_info *child_die;
7e314c57 12355 struct type *type;
c906108c
SS
12356 struct type *element_type, *range_type, *index_type;
12357 struct type **range_types = NULL;
12358 struct attribute *attr;
12359 int ndim = 0;
12360 struct cleanup *back_to;
15d034d0 12361 const char *name;
c906108c 12362
e7c27a73 12363 element_type = die_type (die, cu);
c906108c 12364
7e314c57
JK
12365 /* The die_type call above may have already set the type for this DIE. */
12366 type = get_die_type (die, cu);
12367 if (type)
12368 return type;
12369
c906108c
SS
12370 /* Irix 6.2 native cc creates array types without children for
12371 arrays with unspecified length. */
639d11d3 12372 if (die->child == NULL)
c906108c 12373 {
46bf5051 12374 index_type = objfile_type (objfile)->builtin_int;
c906108c 12375 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
12376 type = create_array_type (NULL, element_type, range_type);
12377 return set_die_type (die, type, cu);
c906108c
SS
12378 }
12379
12380 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 12381 child_die = die->child;
c906108c
SS
12382 while (child_die && child_die->tag)
12383 {
12384 if (child_die->tag == DW_TAG_subrange_type)
12385 {
f792889a 12386 struct type *child_type = read_type_die (child_die, cu);
9a619af0 12387
f792889a 12388 if (child_type != NULL)
a02abb62 12389 {
0963b4bd
MS
12390 /* The range type was succesfully read. Save it for the
12391 array type creation. */
a02abb62
JB
12392 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12393 {
12394 range_types = (struct type **)
12395 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12396 * sizeof (struct type *));
12397 if (ndim == 0)
12398 make_cleanup (free_current_contents, &range_types);
12399 }
f792889a 12400 range_types[ndim++] = child_type;
a02abb62 12401 }
c906108c
SS
12402 }
12403 child_die = sibling_die (child_die);
12404 }
12405
12406 /* Dwarf2 dimensions are output from left to right, create the
12407 necessary array types in backwards order. */
7ca2d3a3 12408
c906108c 12409 type = element_type;
7ca2d3a3
DL
12410
12411 if (read_array_order (die, cu) == DW_ORD_col_major)
12412 {
12413 int i = 0;
9a619af0 12414
7ca2d3a3
DL
12415 while (i < ndim)
12416 type = create_array_type (NULL, type, range_types[i++]);
12417 }
12418 else
12419 {
12420 while (ndim-- > 0)
12421 type = create_array_type (NULL, type, range_types[ndim]);
12422 }
c906108c 12423
f5f8a009
EZ
12424 /* Understand Dwarf2 support for vector types (like they occur on
12425 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12426 array type. This is not part of the Dwarf2/3 standard yet, but a
12427 custom vendor extension. The main difference between a regular
12428 array and the vector variant is that vectors are passed by value
12429 to functions. */
e142c38c 12430 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 12431 if (attr)
ea37ba09 12432 make_vector_type (type);
f5f8a009 12433
dbc98a8b
KW
12434 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12435 implementation may choose to implement triple vectors using this
12436 attribute. */
12437 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12438 if (attr)
12439 {
12440 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12441 TYPE_LENGTH (type) = DW_UNSND (attr);
12442 else
3e43a32a
MS
12443 complaint (&symfile_complaints,
12444 _("DW_AT_byte_size for array type smaller "
12445 "than the total size of elements"));
dbc98a8b
KW
12446 }
12447
39cbfefa
DJ
12448 name = dwarf2_name (die, cu);
12449 if (name)
12450 TYPE_NAME (type) = name;
6e70227d 12451
0963b4bd 12452 /* Install the type in the die. */
7e314c57
JK
12453 set_die_type (die, type, cu);
12454
12455 /* set_die_type should be already done. */
b4ba55a1
JB
12456 set_descriptive_type (type, die, cu);
12457
c906108c
SS
12458 do_cleanups (back_to);
12459
7e314c57 12460 return type;
c906108c
SS
12461}
12462
7ca2d3a3 12463static enum dwarf_array_dim_ordering
6e70227d 12464read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
12465{
12466 struct attribute *attr;
12467
12468 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12469
12470 if (attr) return DW_SND (attr);
12471
0963b4bd
MS
12472 /* GNU F77 is a special case, as at 08/2004 array type info is the
12473 opposite order to the dwarf2 specification, but data is still
12474 laid out as per normal fortran.
7ca2d3a3 12475
0963b4bd
MS
12476 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12477 version checking. */
7ca2d3a3 12478
905e0470
PM
12479 if (cu->language == language_fortran
12480 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
12481 {
12482 return DW_ORD_row_major;
12483 }
12484
6e70227d 12485 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12486 {
12487 case array_column_major:
12488 return DW_ORD_col_major;
12489 case array_row_major:
12490 default:
12491 return DW_ORD_row_major;
12492 };
12493}
12494
72019c9c 12495/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12496 the DIE's type field. */
72019c9c 12497
f792889a 12498static struct type *
72019c9c
GM
12499read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12500{
7e314c57
JK
12501 struct type *domain_type, *set_type;
12502 struct attribute *attr;
f792889a 12503
7e314c57
JK
12504 domain_type = die_type (die, cu);
12505
12506 /* The die_type call above may have already set the type for this DIE. */
12507 set_type = get_die_type (die, cu);
12508 if (set_type)
12509 return set_type;
12510
12511 set_type = create_set_type (NULL, domain_type);
12512
12513 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12514 if (attr)
12515 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12516
f792889a 12517 return set_die_type (die, set_type, cu);
72019c9c 12518}
7ca2d3a3 12519
0971de02
TT
12520/* A helper for read_common_block that creates a locexpr baton.
12521 SYM is the symbol which we are marking as computed.
12522 COMMON_DIE is the DIE for the common block.
12523 COMMON_LOC is the location expression attribute for the common
12524 block itself.
12525 MEMBER_LOC is the location expression attribute for the particular
12526 member of the common block that we are processing.
12527 CU is the CU from which the above come. */
12528
12529static void
12530mark_common_block_symbol_computed (struct symbol *sym,
12531 struct die_info *common_die,
12532 struct attribute *common_loc,
12533 struct attribute *member_loc,
12534 struct dwarf2_cu *cu)
12535{
12536 struct objfile *objfile = dwarf2_per_objfile->objfile;
12537 struct dwarf2_locexpr_baton *baton;
12538 gdb_byte *ptr;
12539 unsigned int cu_off;
12540 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12541 LONGEST offset = 0;
12542
12543 gdb_assert (common_loc && member_loc);
12544 gdb_assert (attr_form_is_block (common_loc));
12545 gdb_assert (attr_form_is_block (member_loc)
12546 || attr_form_is_constant (member_loc));
12547
12548 baton = obstack_alloc (&objfile->objfile_obstack,
12549 sizeof (struct dwarf2_locexpr_baton));
12550 baton->per_cu = cu->per_cu;
12551 gdb_assert (baton->per_cu);
12552
12553 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12554
12555 if (attr_form_is_constant (member_loc))
12556 {
12557 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12558 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12559 }
12560 else
12561 baton->size += DW_BLOCK (member_loc)->size;
12562
12563 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12564 baton->data = ptr;
12565
12566 *ptr++ = DW_OP_call4;
12567 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12568 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12569 ptr += 4;
12570
12571 if (attr_form_is_constant (member_loc))
12572 {
12573 *ptr++ = DW_OP_addr;
12574 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12575 ptr += cu->header.addr_size;
12576 }
12577 else
12578 {
12579 /* We have to copy the data here, because DW_OP_call4 will only
12580 use a DW_AT_location attribute. */
12581 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12582 ptr += DW_BLOCK (member_loc)->size;
12583 }
12584
12585 *ptr++ = DW_OP_plus;
12586 gdb_assert (ptr - baton->data == baton->size);
12587
0971de02 12588 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12589 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12590}
12591
4357ac6c
TT
12592/* Create appropriate locally-scoped variables for all the
12593 DW_TAG_common_block entries. Also create a struct common_block
12594 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12595 is used to sepate the common blocks name namespace from regular
12596 variable names. */
c906108c
SS
12597
12598static void
e7c27a73 12599read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12600{
0971de02
TT
12601 struct attribute *attr;
12602
12603 attr = dwarf2_attr (die, DW_AT_location, cu);
12604 if (attr)
12605 {
12606 /* Support the .debug_loc offsets. */
12607 if (attr_form_is_block (attr))
12608 {
12609 /* Ok. */
12610 }
12611 else if (attr_form_is_section_offset (attr))
12612 {
12613 dwarf2_complex_location_expr_complaint ();
12614 attr = NULL;
12615 }
12616 else
12617 {
12618 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12619 "common block member");
12620 attr = NULL;
12621 }
12622 }
12623
639d11d3 12624 if (die->child != NULL)
c906108c 12625 {
4357ac6c
TT
12626 struct objfile *objfile = cu->objfile;
12627 struct die_info *child_die;
12628 size_t n_entries = 0, size;
12629 struct common_block *common_block;
12630 struct symbol *sym;
74ac6d43 12631
4357ac6c
TT
12632 for (child_die = die->child;
12633 child_die && child_die->tag;
12634 child_die = sibling_die (child_die))
12635 ++n_entries;
12636
12637 size = (sizeof (struct common_block)
12638 + (n_entries - 1) * sizeof (struct symbol *));
12639 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12640 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12641 common_block->n_entries = 0;
12642
12643 for (child_die = die->child;
12644 child_die && child_die->tag;
12645 child_die = sibling_die (child_die))
12646 {
12647 /* Create the symbol in the DW_TAG_common_block block in the current
12648 symbol scope. */
e7c27a73 12649 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12650 if (sym != NULL)
12651 {
12652 struct attribute *member_loc;
12653
12654 common_block->contents[common_block->n_entries++] = sym;
12655
12656 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12657 cu);
12658 if (member_loc)
12659 {
12660 /* GDB has handled this for a long time, but it is
12661 not specified by DWARF. It seems to have been
12662 emitted by gfortran at least as recently as:
12663 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12664 complaint (&symfile_complaints,
12665 _("Variable in common block has "
12666 "DW_AT_data_member_location "
12667 "- DIE at 0x%x [in module %s]"),
4262abfb
JK
12668 child_die->offset.sect_off,
12669 objfile_name (cu->objfile));
0971de02
TT
12670
12671 if (attr_form_is_section_offset (member_loc))
12672 dwarf2_complex_location_expr_complaint ();
12673 else if (attr_form_is_constant (member_loc)
12674 || attr_form_is_block (member_loc))
12675 {
12676 if (attr)
12677 mark_common_block_symbol_computed (sym, die, attr,
12678 member_loc, cu);
12679 }
12680 else
12681 dwarf2_complex_location_expr_complaint ();
12682 }
12683 }
c906108c 12684 }
4357ac6c
TT
12685
12686 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12687 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12688 }
12689}
12690
0114d602 12691/* Create a type for a C++ namespace. */
d9fa45fe 12692
0114d602
DJ
12693static struct type *
12694read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12695{
e7c27a73 12696 struct objfile *objfile = cu->objfile;
0114d602 12697 const char *previous_prefix, *name;
9219021c 12698 int is_anonymous;
0114d602
DJ
12699 struct type *type;
12700
12701 /* For extensions, reuse the type of the original namespace. */
12702 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12703 {
12704 struct die_info *ext_die;
12705 struct dwarf2_cu *ext_cu = cu;
9a619af0 12706
0114d602
DJ
12707 ext_die = dwarf2_extension (die, &ext_cu);
12708 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12709
12710 /* EXT_CU may not be the same as CU.
02142a6c 12711 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12712 return set_die_type (die, type, cu);
12713 }
9219021c 12714
e142c38c 12715 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12716
12717 /* Now build the name of the current namespace. */
12718
0114d602
DJ
12719 previous_prefix = determine_prefix (die, cu);
12720 if (previous_prefix[0] != '\0')
12721 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12722 previous_prefix, name, 0, cu);
0114d602
DJ
12723
12724 /* Create the type. */
12725 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12726 objfile);
abee88f2 12727 TYPE_NAME (type) = name;
0114d602
DJ
12728 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12729
60531b24 12730 return set_die_type (die, type, cu);
0114d602
DJ
12731}
12732
12733/* Read a C++ namespace. */
12734
12735static void
12736read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12737{
12738 struct objfile *objfile = cu->objfile;
0114d602 12739 int is_anonymous;
9219021c 12740
5c4e30ca
DC
12741 /* Add a symbol associated to this if we haven't seen the namespace
12742 before. Also, add a using directive if it's an anonymous
12743 namespace. */
9219021c 12744
f2f0e013 12745 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12746 {
12747 struct type *type;
12748
0114d602 12749 type = read_type_die (die, cu);
e7c27a73 12750 new_symbol (die, type, cu);
5c4e30ca 12751
e8e80198 12752 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12753 if (is_anonymous)
0114d602
DJ
12754 {
12755 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12756
c0cc3a76 12757 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12758 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12759 }
5c4e30ca 12760 }
9219021c 12761
639d11d3 12762 if (die->child != NULL)
d9fa45fe 12763 {
639d11d3 12764 struct die_info *child_die = die->child;
6e70227d 12765
d9fa45fe
DC
12766 while (child_die && child_die->tag)
12767 {
e7c27a73 12768 process_die (child_die, cu);
d9fa45fe
DC
12769 child_die = sibling_die (child_die);
12770 }
12771 }
38d518c9
EZ
12772}
12773
f55ee35c
JK
12774/* Read a Fortran module as type. This DIE can be only a declaration used for
12775 imported module. Still we need that type as local Fortran "use ... only"
12776 declaration imports depend on the created type in determine_prefix. */
12777
12778static struct type *
12779read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12780{
12781 struct objfile *objfile = cu->objfile;
15d034d0 12782 const char *module_name;
f55ee35c
JK
12783 struct type *type;
12784
12785 module_name = dwarf2_name (die, cu);
12786 if (!module_name)
3e43a32a
MS
12787 complaint (&symfile_complaints,
12788 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12789 die->offset.sect_off);
f55ee35c
JK
12790 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12791
12792 /* determine_prefix uses TYPE_TAG_NAME. */
12793 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12794
12795 return set_die_type (die, type, cu);
12796}
12797
5d7cb8df
JK
12798/* Read a Fortran module. */
12799
12800static void
12801read_module (struct die_info *die, struct dwarf2_cu *cu)
12802{
12803 struct die_info *child_die = die->child;
12804
5d7cb8df
JK
12805 while (child_die && child_die->tag)
12806 {
12807 process_die (child_die, cu);
12808 child_die = sibling_die (child_die);
12809 }
12810}
12811
38d518c9
EZ
12812/* Return the name of the namespace represented by DIE. Set
12813 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12814 namespace. */
12815
12816static const char *
e142c38c 12817namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12818{
12819 struct die_info *current_die;
12820 const char *name = NULL;
12821
12822 /* Loop through the extensions until we find a name. */
12823
12824 for (current_die = die;
12825 current_die != NULL;
f2f0e013 12826 current_die = dwarf2_extension (die, &cu))
38d518c9 12827 {
e142c38c 12828 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12829 if (name != NULL)
12830 break;
12831 }
12832
12833 /* Is it an anonymous namespace? */
12834
12835 *is_anonymous = (name == NULL);
12836 if (*is_anonymous)
2b1dbab0 12837 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12838
12839 return name;
d9fa45fe
DC
12840}
12841
c906108c
SS
12842/* Extract all information from a DW_TAG_pointer_type DIE and add to
12843 the user defined type vector. */
12844
f792889a 12845static struct type *
e7c27a73 12846read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12847{
5e2b427d 12848 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12849 struct comp_unit_head *cu_header = &cu->header;
c906108c 12850 struct type *type;
8b2dbe47
KB
12851 struct attribute *attr_byte_size;
12852 struct attribute *attr_address_class;
12853 int byte_size, addr_class;
7e314c57
JK
12854 struct type *target_type;
12855
12856 target_type = die_type (die, cu);
c906108c 12857
7e314c57
JK
12858 /* The die_type call above may have already set the type for this DIE. */
12859 type = get_die_type (die, cu);
12860 if (type)
12861 return type;
12862
12863 type = lookup_pointer_type (target_type);
8b2dbe47 12864
e142c38c 12865 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12866 if (attr_byte_size)
12867 byte_size = DW_UNSND (attr_byte_size);
c906108c 12868 else
8b2dbe47
KB
12869 byte_size = cu_header->addr_size;
12870
e142c38c 12871 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12872 if (attr_address_class)
12873 addr_class = DW_UNSND (attr_address_class);
12874 else
12875 addr_class = DW_ADDR_none;
12876
12877 /* If the pointer size or address class is different than the
12878 default, create a type variant marked as such and set the
12879 length accordingly. */
12880 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12881 {
5e2b427d 12882 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12883 {
12884 int type_flags;
12885
849957d9 12886 type_flags = gdbarch_address_class_type_flags
5e2b427d 12887 (gdbarch, byte_size, addr_class);
876cecd0
TT
12888 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12889 == 0);
8b2dbe47
KB
12890 type = make_type_with_address_space (type, type_flags);
12891 }
12892 else if (TYPE_LENGTH (type) != byte_size)
12893 {
3e43a32a
MS
12894 complaint (&symfile_complaints,
12895 _("invalid pointer size %d"), byte_size);
8b2dbe47 12896 }
6e70227d 12897 else
9a619af0
MS
12898 {
12899 /* Should we also complain about unhandled address classes? */
12900 }
c906108c 12901 }
8b2dbe47
KB
12902
12903 TYPE_LENGTH (type) = byte_size;
f792889a 12904 return set_die_type (die, type, cu);
c906108c
SS
12905}
12906
12907/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12908 the user defined type vector. */
12909
f792889a 12910static struct type *
e7c27a73 12911read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12912{
12913 struct type *type;
12914 struct type *to_type;
12915 struct type *domain;
12916
e7c27a73
DJ
12917 to_type = die_type (die, cu);
12918 domain = die_containing_type (die, cu);
0d5de010 12919
7e314c57
JK
12920 /* The calls above may have already set the type for this DIE. */
12921 type = get_die_type (die, cu);
12922 if (type)
12923 return type;
12924
0d5de010
DJ
12925 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12926 type = lookup_methodptr_type (to_type);
7078baeb
TT
12927 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12928 {
12929 struct type *new_type = alloc_type (cu->objfile);
12930
12931 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12932 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12933 TYPE_VARARGS (to_type));
12934 type = lookup_methodptr_type (new_type);
12935 }
0d5de010
DJ
12936 else
12937 type = lookup_memberptr_type (to_type, domain);
c906108c 12938
f792889a 12939 return set_die_type (die, type, cu);
c906108c
SS
12940}
12941
12942/* Extract all information from a DW_TAG_reference_type DIE and add to
12943 the user defined type vector. */
12944
f792889a 12945static struct type *
e7c27a73 12946read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12947{
e7c27a73 12948 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12949 struct type *type, *target_type;
c906108c
SS
12950 struct attribute *attr;
12951
7e314c57
JK
12952 target_type = die_type (die, cu);
12953
12954 /* The die_type call above may have already set the type for this DIE. */
12955 type = get_die_type (die, cu);
12956 if (type)
12957 return type;
12958
12959 type = lookup_reference_type (target_type);
e142c38c 12960 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12961 if (attr)
12962 {
12963 TYPE_LENGTH (type) = DW_UNSND (attr);
12964 }
12965 else
12966 {
107d2387 12967 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12968 }
f792889a 12969 return set_die_type (die, type, cu);
c906108c
SS
12970}
12971
f792889a 12972static struct type *
e7c27a73 12973read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12974{
f792889a 12975 struct type *base_type, *cv_type;
c906108c 12976
e7c27a73 12977 base_type = die_type (die, cu);
7e314c57
JK
12978
12979 /* The die_type call above may have already set the type for this DIE. */
12980 cv_type = get_die_type (die, cu);
12981 if (cv_type)
12982 return cv_type;
12983
2f608a3a
KW
12984 /* In case the const qualifier is applied to an array type, the element type
12985 is so qualified, not the array type (section 6.7.3 of C99). */
12986 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12987 {
12988 struct type *el_type, *inner_array;
12989
12990 base_type = copy_type (base_type);
12991 inner_array = base_type;
12992
12993 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12994 {
12995 TYPE_TARGET_TYPE (inner_array) =
12996 copy_type (TYPE_TARGET_TYPE (inner_array));
12997 inner_array = TYPE_TARGET_TYPE (inner_array);
12998 }
12999
13000 el_type = TYPE_TARGET_TYPE (inner_array);
13001 TYPE_TARGET_TYPE (inner_array) =
13002 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
13003
13004 return set_die_type (die, base_type, cu);
13005 }
13006
f792889a
DJ
13007 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
13008 return set_die_type (die, cv_type, cu);
c906108c
SS
13009}
13010
f792889a 13011static struct type *
e7c27a73 13012read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13013{
f792889a 13014 struct type *base_type, *cv_type;
c906108c 13015
e7c27a73 13016 base_type = die_type (die, cu);
7e314c57
JK
13017
13018 /* The die_type call above may have already set the type for this DIE. */
13019 cv_type = get_die_type (die, cu);
13020 if (cv_type)
13021 return cv_type;
13022
f792889a
DJ
13023 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
13024 return set_die_type (die, cv_type, cu);
c906108c
SS
13025}
13026
06d66ee9
TT
13027/* Handle DW_TAG_restrict_type. */
13028
13029static struct type *
13030read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13031{
13032 struct type *base_type, *cv_type;
13033
13034 base_type = die_type (die, cu);
13035
13036 /* The die_type call above may have already set the type for this DIE. */
13037 cv_type = get_die_type (die, cu);
13038 if (cv_type)
13039 return cv_type;
13040
13041 cv_type = make_restrict_type (base_type);
13042 return set_die_type (die, cv_type, cu);
13043}
13044
c906108c
SS
13045/* Extract all information from a DW_TAG_string_type DIE and add to
13046 the user defined type vector. It isn't really a user defined type,
13047 but it behaves like one, with other DIE's using an AT_user_def_type
13048 attribute to reference it. */
13049
f792889a 13050static struct type *
e7c27a73 13051read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13052{
e7c27a73 13053 struct objfile *objfile = cu->objfile;
3b7538c0 13054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13055 struct type *type, *range_type, *index_type, *char_type;
13056 struct attribute *attr;
13057 unsigned int length;
13058
e142c38c 13059 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
13060 if (attr)
13061 {
13062 length = DW_UNSND (attr);
13063 }
13064 else
13065 {
0963b4bd 13066 /* Check for the DW_AT_byte_size attribute. */
e142c38c 13067 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
13068 if (attr)
13069 {
13070 length = DW_UNSND (attr);
13071 }
13072 else
13073 {
13074 length = 1;
13075 }
c906108c 13076 }
6ccb9162 13077
46bf5051 13078 index_type = objfile_type (objfile)->builtin_int;
c906108c 13079 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
13080 char_type = language_string_char_type (cu->language_defn, gdbarch);
13081 type = create_string_type (NULL, char_type, range_type);
6ccb9162 13082
f792889a 13083 return set_die_type (die, type, cu);
c906108c
SS
13084}
13085
4d804846
JB
13086/* Assuming that DIE corresponds to a function, returns nonzero
13087 if the function is prototyped. */
13088
13089static int
13090prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13091{
13092 struct attribute *attr;
13093
13094 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13095 if (attr && (DW_UNSND (attr) != 0))
13096 return 1;
13097
13098 /* The DWARF standard implies that the DW_AT_prototyped attribute
13099 is only meaninful for C, but the concept also extends to other
13100 languages that allow unprototyped functions (Eg: Objective C).
13101 For all other languages, assume that functions are always
13102 prototyped. */
13103 if (cu->language != language_c
13104 && cu->language != language_objc
13105 && cu->language != language_opencl)
13106 return 1;
13107
13108 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13109 prototyped and unprototyped functions; default to prototyped,
13110 since that is more common in modern code (and RealView warns
13111 about unprototyped functions). */
13112 if (producer_is_realview (cu->producer))
13113 return 1;
13114
13115 return 0;
13116}
13117
c906108c
SS
13118/* Handle DIES due to C code like:
13119
13120 struct foo
c5aa993b
JM
13121 {
13122 int (*funcp)(int a, long l);
13123 int b;
13124 };
c906108c 13125
0963b4bd 13126 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 13127
f792889a 13128static struct type *
e7c27a73 13129read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13130{
bb5ed363 13131 struct objfile *objfile = cu->objfile;
0963b4bd
MS
13132 struct type *type; /* Type that this function returns. */
13133 struct type *ftype; /* Function that returns above type. */
c906108c
SS
13134 struct attribute *attr;
13135
e7c27a73 13136 type = die_type (die, cu);
7e314c57
JK
13137
13138 /* The die_type call above may have already set the type for this DIE. */
13139 ftype = get_die_type (die, cu);
13140 if (ftype)
13141 return ftype;
13142
0c8b41f1 13143 ftype = lookup_function_type (type);
c906108c 13144
4d804846 13145 if (prototyped_function_p (die, cu))
a6c727b2 13146 TYPE_PROTOTYPED (ftype) = 1;
c906108c 13147
c055b101
CV
13148 /* Store the calling convention in the type if it's available in
13149 the subroutine die. Otherwise set the calling convention to
13150 the default value DW_CC_normal. */
13151 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13152 if (attr)
13153 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13154 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13155 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13156 else
13157 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13158
13159 /* We need to add the subroutine type to the die immediately so
13160 we don't infinitely recurse when dealing with parameters
0963b4bd 13161 declared as the same subroutine type. */
76c10ea2 13162 set_die_type (die, ftype, cu);
6e70227d 13163
639d11d3 13164 if (die->child != NULL)
c906108c 13165 {
bb5ed363 13166 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13167 struct die_info *child_die;
8072405b 13168 int nparams, iparams;
c906108c
SS
13169
13170 /* Count the number of parameters.
13171 FIXME: GDB currently ignores vararg functions, but knows about
13172 vararg member functions. */
8072405b 13173 nparams = 0;
639d11d3 13174 child_die = die->child;
c906108c
SS
13175 while (child_die && child_die->tag)
13176 {
13177 if (child_die->tag == DW_TAG_formal_parameter)
13178 nparams++;
13179 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13180 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13181 child_die = sibling_die (child_die);
13182 }
13183
13184 /* Allocate storage for parameters and fill them in. */
13185 TYPE_NFIELDS (ftype) = nparams;
13186 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13187 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13188
8072405b
JK
13189 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13190 even if we error out during the parameters reading below. */
13191 for (iparams = 0; iparams < nparams; iparams++)
13192 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13193
13194 iparams = 0;
639d11d3 13195 child_die = die->child;
c906108c
SS
13196 while (child_die && child_die->tag)
13197 {
13198 if (child_die->tag == DW_TAG_formal_parameter)
13199 {
3ce3b1ba
PA
13200 struct type *arg_type;
13201
13202 /* DWARF version 2 has no clean way to discern C++
13203 static and non-static member functions. G++ helps
13204 GDB by marking the first parameter for non-static
13205 member functions (which is the this pointer) as
13206 artificial. We pass this information to
13207 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13208
13209 DWARF version 3 added DW_AT_object_pointer, which GCC
13210 4.5 does not yet generate. */
e142c38c 13211 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13212 if (attr)
13213 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13214 else
418835cc
KS
13215 {
13216 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13217
13218 /* GCC/43521: In java, the formal parameter
13219 "this" is sometimes not marked with DW_AT_artificial. */
13220 if (cu->language == language_java)
13221 {
13222 const char *name = dwarf2_name (child_die, cu);
9a619af0 13223
418835cc
KS
13224 if (name && !strcmp (name, "this"))
13225 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13226 }
13227 }
3ce3b1ba
PA
13228 arg_type = die_type (child_die, cu);
13229
13230 /* RealView does not mark THIS as const, which the testsuite
13231 expects. GCC marks THIS as const in method definitions,
13232 but not in the class specifications (GCC PR 43053). */
13233 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13234 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13235 {
13236 int is_this = 0;
13237 struct dwarf2_cu *arg_cu = cu;
13238 const char *name = dwarf2_name (child_die, cu);
13239
13240 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13241 if (attr)
13242 {
13243 /* If the compiler emits this, use it. */
13244 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13245 is_this = 1;
13246 }
13247 else if (name && strcmp (name, "this") == 0)
13248 /* Function definitions will have the argument names. */
13249 is_this = 1;
13250 else if (name == NULL && iparams == 0)
13251 /* Declarations may not have the names, so like
13252 elsewhere in GDB, assume an artificial first
13253 argument is "this". */
13254 is_this = 1;
13255
13256 if (is_this)
13257 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13258 arg_type, 0);
13259 }
13260
13261 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
13262 iparams++;
13263 }
13264 child_die = sibling_die (child_die);
13265 }
13266 }
13267
76c10ea2 13268 return ftype;
c906108c
SS
13269}
13270
f792889a 13271static struct type *
e7c27a73 13272read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13273{
e7c27a73 13274 struct objfile *objfile = cu->objfile;
0114d602 13275 const char *name = NULL;
3c8e0968 13276 struct type *this_type, *target_type;
c906108c 13277
94af9270 13278 name = dwarf2_full_name (NULL, die, cu);
f792889a 13279 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 13280 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 13281 TYPE_NAME (this_type) = name;
f792889a 13282 set_die_type (die, this_type, cu);
3c8e0968
DE
13283 target_type = die_type (die, cu);
13284 if (target_type != this_type)
13285 TYPE_TARGET_TYPE (this_type) = target_type;
13286 else
13287 {
13288 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13289 spec and cause infinite loops in GDB. */
13290 complaint (&symfile_complaints,
13291 _("Self-referential DW_TAG_typedef "
13292 "- DIE at 0x%x [in module %s]"),
4262abfb 13293 die->offset.sect_off, objfile_name (objfile));
3c8e0968
DE
13294 TYPE_TARGET_TYPE (this_type) = NULL;
13295 }
f792889a 13296 return this_type;
c906108c
SS
13297}
13298
13299/* Find a representation of a given base type and install
13300 it in the TYPE field of the die. */
13301
f792889a 13302static struct type *
e7c27a73 13303read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13304{
e7c27a73 13305 struct objfile *objfile = cu->objfile;
c906108c
SS
13306 struct type *type;
13307 struct attribute *attr;
13308 int encoding = 0, size = 0;
15d034d0 13309 const char *name;
6ccb9162
UW
13310 enum type_code code = TYPE_CODE_INT;
13311 int type_flags = 0;
13312 struct type *target_type = NULL;
c906108c 13313
e142c38c 13314 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
13315 if (attr)
13316 {
13317 encoding = DW_UNSND (attr);
13318 }
e142c38c 13319 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13320 if (attr)
13321 {
13322 size = DW_UNSND (attr);
13323 }
39cbfefa 13324 name = dwarf2_name (die, cu);
6ccb9162 13325 if (!name)
c906108c 13326 {
6ccb9162
UW
13327 complaint (&symfile_complaints,
13328 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 13329 }
6ccb9162
UW
13330
13331 switch (encoding)
c906108c 13332 {
6ccb9162
UW
13333 case DW_ATE_address:
13334 /* Turn DW_ATE_address into a void * pointer. */
13335 code = TYPE_CODE_PTR;
13336 type_flags |= TYPE_FLAG_UNSIGNED;
13337 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13338 break;
13339 case DW_ATE_boolean:
13340 code = TYPE_CODE_BOOL;
13341 type_flags |= TYPE_FLAG_UNSIGNED;
13342 break;
13343 case DW_ATE_complex_float:
13344 code = TYPE_CODE_COMPLEX;
13345 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13346 break;
13347 case DW_ATE_decimal_float:
13348 code = TYPE_CODE_DECFLOAT;
13349 break;
13350 case DW_ATE_float:
13351 code = TYPE_CODE_FLT;
13352 break;
13353 case DW_ATE_signed:
13354 break;
13355 case DW_ATE_unsigned:
13356 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
13357 if (cu->language == language_fortran
13358 && name
13359 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13360 code = TYPE_CODE_CHAR;
6ccb9162
UW
13361 break;
13362 case DW_ATE_signed_char:
6e70227d 13363 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13364 || cu->language == language_pascal
13365 || cu->language == language_fortran)
6ccb9162
UW
13366 code = TYPE_CODE_CHAR;
13367 break;
13368 case DW_ATE_unsigned_char:
868a0084 13369 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13370 || cu->language == language_pascal
13371 || cu->language == language_fortran)
6ccb9162
UW
13372 code = TYPE_CODE_CHAR;
13373 type_flags |= TYPE_FLAG_UNSIGNED;
13374 break;
75079b2b
TT
13375 case DW_ATE_UTF:
13376 /* We just treat this as an integer and then recognize the
13377 type by name elsewhere. */
13378 break;
13379
6ccb9162
UW
13380 default:
13381 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13382 dwarf_type_encoding_name (encoding));
13383 break;
c906108c 13384 }
6ccb9162 13385
0114d602
DJ
13386 type = init_type (code, size, type_flags, NULL, objfile);
13387 TYPE_NAME (type) = name;
6ccb9162
UW
13388 TYPE_TARGET_TYPE (type) = target_type;
13389
0114d602 13390 if (name && strcmp (name, "char") == 0)
876cecd0 13391 TYPE_NOSIGN (type) = 1;
0114d602 13392
f792889a 13393 return set_die_type (die, type, cu);
c906108c
SS
13394}
13395
a02abb62
JB
13396/* Read the given DW_AT_subrange DIE. */
13397
f792889a 13398static struct type *
a02abb62
JB
13399read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13400{
4c9ad8c2 13401 struct type *base_type, *orig_base_type;
a02abb62
JB
13402 struct type *range_type;
13403 struct attribute *attr;
4fae6e18
JK
13404 LONGEST low, high;
13405 int low_default_is_valid;
15d034d0 13406 const char *name;
43bbcdc2 13407 LONGEST negative_mask;
e77813c8 13408
4c9ad8c2
TT
13409 orig_base_type = die_type (die, cu);
13410 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13411 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13412 creating the range type, but we use the result of check_typedef
13413 when examining properties of the type. */
13414 base_type = check_typedef (orig_base_type);
a02abb62 13415
7e314c57
JK
13416 /* The die_type call above may have already set the type for this DIE. */
13417 range_type = get_die_type (die, cu);
13418 if (range_type)
13419 return range_type;
13420
4fae6e18
JK
13421 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13422 omitting DW_AT_lower_bound. */
13423 switch (cu->language)
6e70227d 13424 {
4fae6e18
JK
13425 case language_c:
13426 case language_cplus:
13427 low = 0;
13428 low_default_is_valid = 1;
13429 break;
13430 case language_fortran:
13431 low = 1;
13432 low_default_is_valid = 1;
13433 break;
13434 case language_d:
13435 case language_java:
13436 case language_objc:
13437 low = 0;
13438 low_default_is_valid = (cu->header.version >= 4);
13439 break;
13440 case language_ada:
13441 case language_m2:
13442 case language_pascal:
a02abb62 13443 low = 1;
4fae6e18
JK
13444 low_default_is_valid = (cu->header.version >= 4);
13445 break;
13446 default:
13447 low = 0;
13448 low_default_is_valid = 0;
13449 break;
a02abb62
JB
13450 }
13451
dd5e6932
DJ
13452 /* FIXME: For variable sized arrays either of these could be
13453 a variable rather than a constant value. We'll allow it,
13454 but we don't know how to handle it. */
e142c38c 13455 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 13456 if (attr)
4fae6e18
JK
13457 low = dwarf2_get_attr_constant_value (attr, low);
13458 else if (!low_default_is_valid)
13459 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13460 "- DIE at 0x%x [in module %s]"),
4262abfb 13461 die->offset.sect_off, objfile_name (cu->objfile));
a02abb62 13462
e142c38c 13463 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 13464 if (attr)
6e70227d 13465 {
7771576e 13466 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
a02abb62
JB
13467 {
13468 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 13469 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
13470 FIXME: GDB does not yet know how to handle dynamic
13471 arrays properly, treat them as arrays with unspecified
13472 length for now.
13473
13474 FIXME: jimb/2003-09-22: GDB does not really know
13475 how to handle arrays of unspecified length
13476 either; we just represent them as zero-length
13477 arrays. Choose an appropriate upper bound given
13478 the lower bound we've computed above. */
13479 high = low - 1;
13480 }
13481 else
13482 high = dwarf2_get_attr_constant_value (attr, 1);
13483 }
e77813c8
PM
13484 else
13485 {
13486 attr = dwarf2_attr (die, DW_AT_count, cu);
13487 if (attr)
13488 {
13489 int count = dwarf2_get_attr_constant_value (attr, 1);
13490 high = low + count - 1;
13491 }
c2ff108b
JK
13492 else
13493 {
13494 /* Unspecified array length. */
13495 high = low - 1;
13496 }
e77813c8
PM
13497 }
13498
13499 /* Dwarf-2 specifications explicitly allows to create subrange types
13500 without specifying a base type.
13501 In that case, the base type must be set to the type of
13502 the lower bound, upper bound or count, in that order, if any of these
13503 three attributes references an object that has a type.
13504 If no base type is found, the Dwarf-2 specifications say that
13505 a signed integer type of size equal to the size of an address should
13506 be used.
13507 For the following C code: `extern char gdb_int [];'
13508 GCC produces an empty range DIE.
13509 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13510 high bound or count are not yet handled by this code. */
e77813c8
PM
13511 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13512 {
13513 struct objfile *objfile = cu->objfile;
13514 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13515 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13516 struct type *int_type = objfile_type (objfile)->builtin_int;
13517
13518 /* Test "int", "long int", and "long long int" objfile types,
13519 and select the first one having a size above or equal to the
13520 architecture address size. */
13521 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13522 base_type = int_type;
13523 else
13524 {
13525 int_type = objfile_type (objfile)->builtin_long;
13526 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13527 base_type = int_type;
13528 else
13529 {
13530 int_type = objfile_type (objfile)->builtin_long_long;
13531 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13532 base_type = int_type;
13533 }
13534 }
13535 }
a02abb62 13536
6e70227d 13537 negative_mask =
43bbcdc2
PH
13538 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13539 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13540 low |= negative_mask;
13541 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13542 high |= negative_mask;
13543
4c9ad8c2 13544 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13545
bbb0eef6
JK
13546 /* Mark arrays with dynamic length at least as an array of unspecified
13547 length. GDB could check the boundary but before it gets implemented at
13548 least allow accessing the array elements. */
d48323d8 13549 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13550 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13551
c2ff108b
JK
13552 /* Ada expects an empty array on no boundary attributes. */
13553 if (attr == NULL && cu->language != language_ada)
13554 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13555
39cbfefa
DJ
13556 name = dwarf2_name (die, cu);
13557 if (name)
13558 TYPE_NAME (range_type) = name;
6e70227d 13559
e142c38c 13560 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13561 if (attr)
13562 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13563
7e314c57
JK
13564 set_die_type (die, range_type, cu);
13565
13566 /* set_die_type should be already done. */
b4ba55a1
JB
13567 set_descriptive_type (range_type, die, cu);
13568
7e314c57 13569 return range_type;
a02abb62 13570}
6e70227d 13571
f792889a 13572static struct type *
81a17f79
JB
13573read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13574{
13575 struct type *type;
81a17f79 13576
81a17f79
JB
13577 /* For now, we only support the C meaning of an unspecified type: void. */
13578
0114d602
DJ
13579 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13580 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13581
f792889a 13582 return set_die_type (die, type, cu);
81a17f79 13583}
a02abb62 13584
639d11d3
DC
13585/* Read a single die and all its descendents. Set the die's sibling
13586 field to NULL; set other fields in the die correctly, and set all
13587 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13588 location of the info_ptr after reading all of those dies. PARENT
13589 is the parent of the die in question. */
13590
13591static struct die_info *
dee91e82 13592read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13593 const gdb_byte *info_ptr,
13594 const gdb_byte **new_info_ptr,
dee91e82 13595 struct die_info *parent)
639d11d3
DC
13596{
13597 struct die_info *die;
d521ce57 13598 const gdb_byte *cur_ptr;
639d11d3
DC
13599 int has_children;
13600
bf6af496 13601 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13602 if (die == NULL)
13603 {
13604 *new_info_ptr = cur_ptr;
13605 return NULL;
13606 }
93311388 13607 store_in_ref_table (die, reader->cu);
639d11d3
DC
13608
13609 if (has_children)
bf6af496 13610 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13611 else
13612 {
13613 die->child = NULL;
13614 *new_info_ptr = cur_ptr;
13615 }
13616
13617 die->sibling = NULL;
13618 die->parent = parent;
13619 return die;
13620}
13621
13622/* Read a die, all of its descendents, and all of its siblings; set
13623 all of the fields of all of the dies correctly. Arguments are as
13624 in read_die_and_children. */
13625
13626static struct die_info *
bf6af496 13627read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13628 const gdb_byte *info_ptr,
13629 const gdb_byte **new_info_ptr,
bf6af496 13630 struct die_info *parent)
639d11d3
DC
13631{
13632 struct die_info *first_die, *last_sibling;
d521ce57 13633 const gdb_byte *cur_ptr;
639d11d3 13634
c906108c 13635 cur_ptr = info_ptr;
639d11d3
DC
13636 first_die = last_sibling = NULL;
13637
13638 while (1)
c906108c 13639 {
639d11d3 13640 struct die_info *die
dee91e82 13641 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13642
1d325ec1 13643 if (die == NULL)
c906108c 13644 {
639d11d3
DC
13645 *new_info_ptr = cur_ptr;
13646 return first_die;
c906108c 13647 }
1d325ec1
DJ
13648
13649 if (!first_die)
13650 first_die = die;
c906108c 13651 else
1d325ec1
DJ
13652 last_sibling->sibling = die;
13653
13654 last_sibling = die;
c906108c 13655 }
c906108c
SS
13656}
13657
bf6af496
DE
13658/* Read a die, all of its descendents, and all of its siblings; set
13659 all of the fields of all of the dies correctly. Arguments are as
13660 in read_die_and_children.
13661 This the main entry point for reading a DIE and all its children. */
13662
13663static struct die_info *
13664read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13665 const gdb_byte *info_ptr,
13666 const gdb_byte **new_info_ptr,
bf6af496
DE
13667 struct die_info *parent)
13668{
13669 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13670 new_info_ptr, parent);
13671
13672 if (dwarf2_die_debug)
13673 {
13674 fprintf_unfiltered (gdb_stdlog,
13675 "Read die from %s@0x%x of %s:\n",
13676 bfd_section_name (reader->abfd,
13677 reader->die_section->asection),
13678 (unsigned) (info_ptr - reader->die_section->buffer),
13679 bfd_get_filename (reader->abfd));
13680 dump_die (die, dwarf2_die_debug);
13681 }
13682
13683 return die;
13684}
13685
3019eac3
DE
13686/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13687 attributes.
13688 The caller is responsible for filling in the extra attributes
13689 and updating (*DIEP)->num_attrs.
13690 Set DIEP to point to a newly allocated die with its information,
13691 except for its child, sibling, and parent fields.
13692 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13693
d521ce57 13694static const gdb_byte *
3019eac3 13695read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13696 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13697 int *has_children, int num_extra_attrs)
93311388 13698{
b64f50a1
JK
13699 unsigned int abbrev_number, bytes_read, i;
13700 sect_offset offset;
93311388
DE
13701 struct abbrev_info *abbrev;
13702 struct die_info *die;
13703 struct dwarf2_cu *cu = reader->cu;
13704 bfd *abfd = reader->abfd;
13705
b64f50a1 13706 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13707 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13708 info_ptr += bytes_read;
13709 if (!abbrev_number)
13710 {
13711 *diep = NULL;
13712 *has_children = 0;
13713 return info_ptr;
13714 }
13715
433df2d4 13716 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13717 if (!abbrev)
348e048f
DE
13718 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13719 abbrev_number,
13720 bfd_get_filename (abfd));
13721
3019eac3 13722 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13723 die->offset = offset;
13724 die->tag = abbrev->tag;
13725 die->abbrev = abbrev_number;
13726
3019eac3
DE
13727 /* Make the result usable.
13728 The caller needs to update num_attrs after adding the extra
13729 attributes. */
93311388
DE
13730 die->num_attrs = abbrev->num_attrs;
13731
13732 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13733 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13734 info_ptr);
93311388
DE
13735
13736 *diep = die;
13737 *has_children = abbrev->has_children;
13738 return info_ptr;
13739}
13740
3019eac3
DE
13741/* Read a die and all its attributes.
13742 Set DIEP to point to a newly allocated die with its information,
13743 except for its child, sibling, and parent fields.
13744 Set HAS_CHILDREN to tell whether the die has children or not. */
13745
d521ce57 13746static const gdb_byte *
3019eac3 13747read_full_die (const struct die_reader_specs *reader,
d521ce57 13748 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13749 int *has_children)
13750{
d521ce57 13751 const gdb_byte *result;
bf6af496
DE
13752
13753 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13754
13755 if (dwarf2_die_debug)
13756 {
13757 fprintf_unfiltered (gdb_stdlog,
13758 "Read die from %s@0x%x of %s:\n",
13759 bfd_section_name (reader->abfd,
13760 reader->die_section->asection),
13761 (unsigned) (info_ptr - reader->die_section->buffer),
13762 bfd_get_filename (reader->abfd));
13763 dump_die (*diep, dwarf2_die_debug);
13764 }
13765
13766 return result;
3019eac3 13767}
433df2d4
DE
13768\f
13769/* Abbreviation tables.
3019eac3 13770
433df2d4 13771 In DWARF version 2, the description of the debugging information is
c906108c
SS
13772 stored in a separate .debug_abbrev section. Before we read any
13773 dies from a section we read in all abbreviations and install them
433df2d4
DE
13774 in a hash table. */
13775
13776/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13777
13778static struct abbrev_info *
13779abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13780{
13781 struct abbrev_info *abbrev;
13782
13783 abbrev = (struct abbrev_info *)
13784 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13785 memset (abbrev, 0, sizeof (struct abbrev_info));
13786 return abbrev;
13787}
13788
13789/* Add an abbreviation to the table. */
c906108c
SS
13790
13791static void
433df2d4
DE
13792abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13793 unsigned int abbrev_number,
13794 struct abbrev_info *abbrev)
13795{
13796 unsigned int hash_number;
13797
13798 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13799 abbrev->next = abbrev_table->abbrevs[hash_number];
13800 abbrev_table->abbrevs[hash_number] = abbrev;
13801}
dee91e82 13802
433df2d4
DE
13803/* Look up an abbrev in the table.
13804 Returns NULL if the abbrev is not found. */
13805
13806static struct abbrev_info *
13807abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13808 unsigned int abbrev_number)
c906108c 13809{
433df2d4
DE
13810 unsigned int hash_number;
13811 struct abbrev_info *abbrev;
13812
13813 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13814 abbrev = abbrev_table->abbrevs[hash_number];
13815
13816 while (abbrev)
13817 {
13818 if (abbrev->number == abbrev_number)
13819 return abbrev;
13820 abbrev = abbrev->next;
13821 }
13822 return NULL;
13823}
13824
13825/* Read in an abbrev table. */
13826
13827static struct abbrev_table *
13828abbrev_table_read_table (struct dwarf2_section_info *section,
13829 sect_offset offset)
13830{
13831 struct objfile *objfile = dwarf2_per_objfile->objfile;
13832 bfd *abfd = section->asection->owner;
13833 struct abbrev_table *abbrev_table;
d521ce57 13834 const gdb_byte *abbrev_ptr;
c906108c
SS
13835 struct abbrev_info *cur_abbrev;
13836 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13837 unsigned int abbrev_form;
f3dd6933
DJ
13838 struct attr_abbrev *cur_attrs;
13839 unsigned int allocated_attrs;
c906108c 13840
433df2d4 13841 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13842 abbrev_table->offset = offset;
433df2d4
DE
13843 obstack_init (&abbrev_table->abbrev_obstack);
13844 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13845 (ABBREV_HASH_SIZE
13846 * sizeof (struct abbrev_info *)));
13847 memset (abbrev_table->abbrevs, 0,
13848 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13849
433df2d4
DE
13850 dwarf2_read_section (objfile, section);
13851 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13852 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13853 abbrev_ptr += bytes_read;
13854
f3dd6933
DJ
13855 allocated_attrs = ATTR_ALLOC_CHUNK;
13856 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13857
0963b4bd 13858 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13859 while (abbrev_number)
13860 {
433df2d4 13861 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13862
13863 /* read in abbrev header */
13864 cur_abbrev->number = abbrev_number;
13865 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13866 abbrev_ptr += bytes_read;
13867 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13868 abbrev_ptr += 1;
13869
13870 /* now read in declarations */
13871 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13872 abbrev_ptr += bytes_read;
13873 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13874 abbrev_ptr += bytes_read;
13875 while (abbrev_name)
13876 {
f3dd6933 13877 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13878 {
f3dd6933
DJ
13879 allocated_attrs += ATTR_ALLOC_CHUNK;
13880 cur_attrs
13881 = xrealloc (cur_attrs, (allocated_attrs
13882 * sizeof (struct attr_abbrev)));
c906108c 13883 }
ae038cb0 13884
f3dd6933
DJ
13885 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13886 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13887 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13888 abbrev_ptr += bytes_read;
13889 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13890 abbrev_ptr += bytes_read;
13891 }
13892
433df2d4 13893 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13894 (cur_abbrev->num_attrs
13895 * sizeof (struct attr_abbrev)));
13896 memcpy (cur_abbrev->attrs, cur_attrs,
13897 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13898
433df2d4 13899 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13900
13901 /* Get next abbreviation.
13902 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13903 always properly terminated with an abbrev number of 0.
13904 Exit loop if we encounter an abbreviation which we have
13905 already read (which means we are about to read the abbreviations
13906 for the next compile unit) or if the end of the abbreviation
13907 table is reached. */
433df2d4 13908 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13909 break;
13910 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13911 abbrev_ptr += bytes_read;
433df2d4 13912 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13913 break;
13914 }
f3dd6933
DJ
13915
13916 xfree (cur_attrs);
433df2d4 13917 return abbrev_table;
c906108c
SS
13918}
13919
433df2d4 13920/* Free the resources held by ABBREV_TABLE. */
c906108c 13921
c906108c 13922static void
433df2d4 13923abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13924{
433df2d4
DE
13925 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13926 xfree (abbrev_table);
c906108c
SS
13927}
13928
f4dc4d17
DE
13929/* Same as abbrev_table_free but as a cleanup.
13930 We pass in a pointer to the pointer to the table so that we can
13931 set the pointer to NULL when we're done. It also simplifies
13932 build_type_unit_groups. */
13933
13934static void
13935abbrev_table_free_cleanup (void *table_ptr)
13936{
13937 struct abbrev_table **abbrev_table_ptr = table_ptr;
13938
13939 if (*abbrev_table_ptr != NULL)
13940 abbrev_table_free (*abbrev_table_ptr);
13941 *abbrev_table_ptr = NULL;
13942}
13943
433df2d4
DE
13944/* Read the abbrev table for CU from ABBREV_SECTION. */
13945
13946static void
13947dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13948 struct dwarf2_section_info *abbrev_section)
c906108c 13949{
433df2d4
DE
13950 cu->abbrev_table =
13951 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13952}
c906108c 13953
433df2d4 13954/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13955
433df2d4
DE
13956static void
13957dwarf2_free_abbrev_table (void *ptr_to_cu)
13958{
13959 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13960
a2ce51a0
DE
13961 if (cu->abbrev_table != NULL)
13962 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
13963 /* Set this to NULL so that we SEGV if we try to read it later,
13964 and also because free_comp_unit verifies this is NULL. */
13965 cu->abbrev_table = NULL;
13966}
13967\f
72bf9492
DJ
13968/* Returns nonzero if TAG represents a type that we might generate a partial
13969 symbol for. */
13970
13971static int
13972is_type_tag_for_partial (int tag)
13973{
13974 switch (tag)
13975 {
13976#if 0
13977 /* Some types that would be reasonable to generate partial symbols for,
13978 that we don't at present. */
13979 case DW_TAG_array_type:
13980 case DW_TAG_file_type:
13981 case DW_TAG_ptr_to_member_type:
13982 case DW_TAG_set_type:
13983 case DW_TAG_string_type:
13984 case DW_TAG_subroutine_type:
13985#endif
13986 case DW_TAG_base_type:
13987 case DW_TAG_class_type:
680b30c7 13988 case DW_TAG_interface_type:
72bf9492
DJ
13989 case DW_TAG_enumeration_type:
13990 case DW_TAG_structure_type:
13991 case DW_TAG_subrange_type:
13992 case DW_TAG_typedef:
13993 case DW_TAG_union_type:
13994 return 1;
13995 default:
13996 return 0;
13997 }
13998}
13999
14000/* Load all DIEs that are interesting for partial symbols into memory. */
14001
14002static struct partial_die_info *
dee91e82 14003load_partial_dies (const struct die_reader_specs *reader,
d521ce57 14004 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 14005{
dee91e82 14006 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14007 struct objfile *objfile = cu->objfile;
72bf9492
DJ
14008 struct partial_die_info *part_die;
14009 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
14010 struct abbrev_info *abbrev;
14011 unsigned int bytes_read;
5afb4e99 14012 unsigned int load_all = 0;
72bf9492
DJ
14013 int nesting_level = 1;
14014
14015 parent_die = NULL;
14016 last_die = NULL;
14017
7adf1e79
DE
14018 gdb_assert (cu->per_cu != NULL);
14019 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
14020 load_all = 1;
14021
72bf9492
DJ
14022 cu->partial_dies
14023 = htab_create_alloc_ex (cu->header.length / 12,
14024 partial_die_hash,
14025 partial_die_eq,
14026 NULL,
14027 &cu->comp_unit_obstack,
14028 hashtab_obstack_allocate,
14029 dummy_obstack_deallocate);
14030
14031 part_die = obstack_alloc (&cu->comp_unit_obstack,
14032 sizeof (struct partial_die_info));
14033
14034 while (1)
14035 {
14036 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
14037
14038 /* A NULL abbrev means the end of a series of children. */
14039 if (abbrev == NULL)
14040 {
14041 if (--nesting_level == 0)
14042 {
14043 /* PART_DIE was probably the last thing allocated on the
14044 comp_unit_obstack, so we could call obstack_free
14045 here. We don't do that because the waste is small,
14046 and will be cleaned up when we're done with this
14047 compilation unit. This way, we're also more robust
14048 against other users of the comp_unit_obstack. */
14049 return first_die;
14050 }
14051 info_ptr += bytes_read;
14052 last_die = parent_die;
14053 parent_die = parent_die->die_parent;
14054 continue;
14055 }
14056
98bfdba5
PA
14057 /* Check for template arguments. We never save these; if
14058 they're seen, we just mark the parent, and go on our way. */
14059 if (parent_die != NULL
14060 && cu->language == language_cplus
14061 && (abbrev->tag == DW_TAG_template_type_param
14062 || abbrev->tag == DW_TAG_template_value_param))
14063 {
14064 parent_die->has_template_arguments = 1;
14065
14066 if (!load_all)
14067 {
14068 /* We don't need a partial DIE for the template argument. */
dee91e82 14069 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14070 continue;
14071 }
14072 }
14073
0d99eb77 14074 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
14075 Skip their other children. */
14076 if (!load_all
14077 && cu->language == language_cplus
14078 && parent_die != NULL
14079 && parent_die->tag == DW_TAG_subprogram)
14080 {
dee91e82 14081 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14082 continue;
14083 }
14084
5afb4e99
DJ
14085 /* Check whether this DIE is interesting enough to save. Normally
14086 we would not be interested in members here, but there may be
14087 later variables referencing them via DW_AT_specification (for
14088 static members). */
14089 if (!load_all
14090 && !is_type_tag_for_partial (abbrev->tag)
72929c62 14091 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
14092 && abbrev->tag != DW_TAG_enumerator
14093 && abbrev->tag != DW_TAG_subprogram
bc30ff58 14094 && abbrev->tag != DW_TAG_lexical_block
72bf9492 14095 && abbrev->tag != DW_TAG_variable
5afb4e99 14096 && abbrev->tag != DW_TAG_namespace
f55ee35c 14097 && abbrev->tag != DW_TAG_module
95554aad
TT
14098 && abbrev->tag != DW_TAG_member
14099 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
14100 {
14101 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14102 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
14103 continue;
14104 }
14105
dee91e82
DE
14106 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14107 info_ptr);
72bf9492
DJ
14108
14109 /* This two-pass algorithm for processing partial symbols has a
14110 high cost in cache pressure. Thus, handle some simple cases
14111 here which cover the majority of C partial symbols. DIEs
14112 which neither have specification tags in them, nor could have
14113 specification tags elsewhere pointing at them, can simply be
14114 processed and discarded.
14115
14116 This segment is also optional; scan_partial_symbols and
14117 add_partial_symbol will handle these DIEs if we chain
14118 them in normally. When compilers which do not emit large
14119 quantities of duplicate debug information are more common,
14120 this code can probably be removed. */
14121
14122 /* Any complete simple types at the top level (pretty much all
14123 of them, for a language without namespaces), can be processed
14124 directly. */
14125 if (parent_die == NULL
14126 && part_die->has_specification == 0
14127 && part_die->is_declaration == 0
d8228535 14128 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
14129 || part_die->tag == DW_TAG_base_type
14130 || part_die->tag == DW_TAG_subrange_type))
14131 {
14132 if (building_psymtab && part_die->name != NULL)
04a679b8 14133 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14134 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
14135 &objfile->static_psymbols,
14136 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 14137 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14138 continue;
14139 }
14140
d8228535
JK
14141 /* The exception for DW_TAG_typedef with has_children above is
14142 a workaround of GCC PR debug/47510. In the case of this complaint
14143 type_name_no_tag_or_error will error on such types later.
14144
14145 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14146 it could not find the child DIEs referenced later, this is checked
14147 above. In correct DWARF DW_TAG_typedef should have no children. */
14148
14149 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14150 complaint (&symfile_complaints,
14151 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14152 "- DIE at 0x%x [in module %s]"),
4262abfb 14153 part_die->offset.sect_off, objfile_name (objfile));
d8228535 14154
72bf9492
DJ
14155 /* If we're at the second level, and we're an enumerator, and
14156 our parent has no specification (meaning possibly lives in a
14157 namespace elsewhere), then we can add the partial symbol now
14158 instead of queueing it. */
14159 if (part_die->tag == DW_TAG_enumerator
14160 && parent_die != NULL
14161 && parent_die->die_parent == NULL
14162 && parent_die->tag == DW_TAG_enumeration_type
14163 && parent_die->has_specification == 0)
14164 {
14165 if (part_die->name == NULL)
3e43a32a
MS
14166 complaint (&symfile_complaints,
14167 _("malformed enumerator DIE ignored"));
72bf9492 14168 else if (building_psymtab)
04a679b8 14169 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14170 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14171 (cu->language == language_cplus
14172 || cu->language == language_java)
bb5ed363
DE
14173 ? &objfile->global_psymbols
14174 : &objfile->static_psymbols,
14175 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14176
dee91e82 14177 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14178 continue;
14179 }
14180
14181 /* We'll save this DIE so link it in. */
14182 part_die->die_parent = parent_die;
14183 part_die->die_sibling = NULL;
14184 part_die->die_child = NULL;
14185
14186 if (last_die && last_die == parent_die)
14187 last_die->die_child = part_die;
14188 else if (last_die)
14189 last_die->die_sibling = part_die;
14190
14191 last_die = part_die;
14192
14193 if (first_die == NULL)
14194 first_die = part_die;
14195
14196 /* Maybe add the DIE to the hash table. Not all DIEs that we
14197 find interesting need to be in the hash table, because we
14198 also have the parent/sibling/child chains; only those that we
14199 might refer to by offset later during partial symbol reading.
14200
14201 For now this means things that might have be the target of a
14202 DW_AT_specification, DW_AT_abstract_origin, or
14203 DW_AT_extension. DW_AT_extension will refer only to
14204 namespaces; DW_AT_abstract_origin refers to functions (and
14205 many things under the function DIE, but we do not recurse
14206 into function DIEs during partial symbol reading) and
14207 possibly variables as well; DW_AT_specification refers to
14208 declarations. Declarations ought to have the DW_AT_declaration
14209 flag. It happens that GCC forgets to put it in sometimes, but
14210 only for functions, not for types.
14211
14212 Adding more things than necessary to the hash table is harmless
14213 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14214 wasted time in find_partial_die, when we reread the compilation
14215 unit with load_all_dies set. */
72bf9492 14216
5afb4e99 14217 if (load_all
72929c62 14218 || abbrev->tag == DW_TAG_constant
5afb4e99 14219 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14220 || abbrev->tag == DW_TAG_variable
14221 || abbrev->tag == DW_TAG_namespace
14222 || part_die->is_declaration)
14223 {
14224 void **slot;
14225
14226 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14227 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14228 *slot = part_die;
14229 }
14230
14231 part_die = obstack_alloc (&cu->comp_unit_obstack,
14232 sizeof (struct partial_die_info));
14233
14234 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14235 we have no reason to follow the children of structures; for other
98bfdba5
PA
14236 languages we have to, so that we can get at method physnames
14237 to infer fully qualified class names, for DW_AT_specification,
14238 and for C++ template arguments. For C++, we also look one level
14239 inside functions to find template arguments (if the name of the
14240 function does not already contain the template arguments).
bc30ff58
JB
14241
14242 For Ada, we need to scan the children of subprograms and lexical
14243 blocks as well because Ada allows the definition of nested
14244 entities that could be interesting for the debugger, such as
14245 nested subprograms for instance. */
72bf9492 14246 if (last_die->has_children
5afb4e99
DJ
14247 && (load_all
14248 || last_die->tag == DW_TAG_namespace
f55ee35c 14249 || last_die->tag == DW_TAG_module
72bf9492 14250 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14251 || (cu->language == language_cplus
14252 && last_die->tag == DW_TAG_subprogram
14253 && (last_die->name == NULL
14254 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
14255 || (cu->language != language_c
14256 && (last_die->tag == DW_TAG_class_type
680b30c7 14257 || last_die->tag == DW_TAG_interface_type
72bf9492 14258 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
14259 || last_die->tag == DW_TAG_union_type))
14260 || (cu->language == language_ada
14261 && (last_die->tag == DW_TAG_subprogram
14262 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
14263 {
14264 nesting_level++;
14265 parent_die = last_die;
14266 continue;
14267 }
14268
14269 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14270 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
14271
14272 /* Back to the top, do it again. */
14273 }
14274}
14275
c906108c
SS
14276/* Read a minimal amount of information into the minimal die structure. */
14277
d521ce57 14278static const gdb_byte *
dee91e82
DE
14279read_partial_die (const struct die_reader_specs *reader,
14280 struct partial_die_info *part_die,
14281 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 14282 const gdb_byte *info_ptr)
c906108c 14283{
dee91e82 14284 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14285 struct objfile *objfile = cu->objfile;
d521ce57 14286 const gdb_byte *buffer = reader->buffer;
fa238c03 14287 unsigned int i;
c906108c 14288 struct attribute attr;
c5aa993b 14289 int has_low_pc_attr = 0;
c906108c 14290 int has_high_pc_attr = 0;
91da1414 14291 int high_pc_relative = 0;
c906108c 14292
72bf9492 14293 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 14294
b64f50a1 14295 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
14296
14297 info_ptr += abbrev_len;
14298
14299 if (abbrev == NULL)
14300 return info_ptr;
14301
c906108c
SS
14302 part_die->tag = abbrev->tag;
14303 part_die->has_children = abbrev->has_children;
c906108c
SS
14304
14305 for (i = 0; i < abbrev->num_attrs; ++i)
14306 {
dee91e82 14307 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
14308
14309 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 14310 partial symbol table. */
c906108c
SS
14311 switch (attr.name)
14312 {
14313 case DW_AT_name:
71c25dea
TT
14314 switch (part_die->tag)
14315 {
14316 case DW_TAG_compile_unit:
95554aad 14317 case DW_TAG_partial_unit:
348e048f 14318 case DW_TAG_type_unit:
71c25dea
TT
14319 /* Compilation units have a DW_AT_name that is a filename, not
14320 a source language identifier. */
14321 case DW_TAG_enumeration_type:
14322 case DW_TAG_enumerator:
14323 /* These tags always have simple identifiers already; no need
14324 to canonicalize them. */
14325 part_die->name = DW_STRING (&attr);
14326 break;
14327 default:
14328 part_die->name
14329 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 14330 &objfile->objfile_obstack);
71c25dea
TT
14331 break;
14332 }
c906108c 14333 break;
31ef98ae 14334 case DW_AT_linkage_name:
c906108c 14335 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
14336 /* Note that both forms of linkage name might appear. We
14337 assume they will be the same, and we only store the last
14338 one we see. */
94af9270
KS
14339 if (cu->language == language_ada)
14340 part_die->name = DW_STRING (&attr);
abc72ce4 14341 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
14342 break;
14343 case DW_AT_low_pc:
14344 has_low_pc_attr = 1;
14345 part_die->lowpc = DW_ADDR (&attr);
14346 break;
14347 case DW_AT_high_pc:
14348 has_high_pc_attr = 1;
3019eac3
DE
14349 if (attr.form == DW_FORM_addr
14350 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
14351 part_die->highpc = DW_ADDR (&attr);
14352 else
14353 {
14354 high_pc_relative = 1;
14355 part_die->highpc = DW_UNSND (&attr);
14356 }
c906108c
SS
14357 break;
14358 case DW_AT_location:
0963b4bd 14359 /* Support the .debug_loc offsets. */
8e19ed76
PS
14360 if (attr_form_is_block (&attr))
14361 {
95554aad 14362 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 14363 }
3690dd37 14364 else if (attr_form_is_section_offset (&attr))
8e19ed76 14365 {
4d3c2250 14366 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14367 }
14368 else
14369 {
4d3c2250
KB
14370 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14371 "partial symbol information");
8e19ed76 14372 }
c906108c 14373 break;
c906108c
SS
14374 case DW_AT_external:
14375 part_die->is_external = DW_UNSND (&attr);
14376 break;
14377 case DW_AT_declaration:
14378 part_die->is_declaration = DW_UNSND (&attr);
14379 break;
14380 case DW_AT_type:
14381 part_die->has_type = 1;
14382 break;
14383 case DW_AT_abstract_origin:
14384 case DW_AT_specification:
72bf9492
DJ
14385 case DW_AT_extension:
14386 part_die->has_specification = 1;
c764a876 14387 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
14388 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14389 || cu->per_cu->is_dwz);
c906108c
SS
14390 break;
14391 case DW_AT_sibling:
14392 /* Ignore absolute siblings, they might point outside of
14393 the current compile unit. */
14394 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
14395 complaint (&symfile_complaints,
14396 _("ignoring absolute DW_AT_sibling"));
c906108c 14397 else
b64f50a1 14398 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 14399 break;
fa4028e9
JB
14400 case DW_AT_byte_size:
14401 part_die->has_byte_size = 1;
14402 break;
68511cec
CES
14403 case DW_AT_calling_convention:
14404 /* DWARF doesn't provide a way to identify a program's source-level
14405 entry point. DW_AT_calling_convention attributes are only meant
14406 to describe functions' calling conventions.
14407
14408 However, because it's a necessary piece of information in
14409 Fortran, and because DW_CC_program is the only piece of debugging
14410 information whose definition refers to a 'main program' at all,
14411 several compilers have begun marking Fortran main programs with
14412 DW_CC_program --- even when those functions use the standard
14413 calling conventions.
14414
14415 So until DWARF specifies a way to provide this information and
14416 compilers pick up the new representation, we'll support this
14417 practice. */
14418 if (DW_UNSND (&attr) == DW_CC_program
14419 && cu->language == language_fortran)
01f8c46d
JK
14420 {
14421 set_main_name (part_die->name);
14422
14423 /* As this DIE has a static linkage the name would be difficult
14424 to look up later. */
14425 language_of_main = language_fortran;
14426 }
68511cec 14427 break;
481860b3
GB
14428 case DW_AT_inline:
14429 if (DW_UNSND (&attr) == DW_INL_inlined
14430 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14431 part_die->may_be_inlined = 1;
14432 break;
95554aad
TT
14433
14434 case DW_AT_import:
14435 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
14436 {
14437 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14438 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14439 || cu->per_cu->is_dwz);
14440 }
95554aad
TT
14441 break;
14442
c906108c
SS
14443 default:
14444 break;
14445 }
14446 }
14447
91da1414
MW
14448 if (high_pc_relative)
14449 part_die->highpc += part_die->lowpc;
14450
9373cf26
JK
14451 if (has_low_pc_attr && has_high_pc_attr)
14452 {
14453 /* When using the GNU linker, .gnu.linkonce. sections are used to
14454 eliminate duplicate copies of functions and vtables and such.
14455 The linker will arbitrarily choose one and discard the others.
14456 The AT_*_pc values for such functions refer to local labels in
14457 these sections. If the section from that file was discarded, the
14458 labels are not in the output, so the relocs get a value of 0.
14459 If this is a discarded function, mark the pc bounds as invalid,
14460 so that GDB will ignore it. */
14461 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14462 {
bb5ed363 14463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14464
14465 complaint (&symfile_complaints,
14466 _("DW_AT_low_pc %s is zero "
14467 "for DIE at 0x%x [in module %s]"),
14468 paddress (gdbarch, part_die->lowpc),
4262abfb 14469 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
14470 }
14471 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14472 else if (part_die->lowpc >= part_die->highpc)
14473 {
bb5ed363 14474 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14475
14476 complaint (&symfile_complaints,
14477 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14478 "for DIE at 0x%x [in module %s]"),
14479 paddress (gdbarch, part_die->lowpc),
14480 paddress (gdbarch, part_die->highpc),
4262abfb 14481 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
14482 }
14483 else
14484 part_die->has_pc_info = 1;
14485 }
85cbf3d3 14486
c906108c
SS
14487 return info_ptr;
14488}
14489
72bf9492
DJ
14490/* Find a cached partial DIE at OFFSET in CU. */
14491
14492static struct partial_die_info *
b64f50a1 14493find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
14494{
14495 struct partial_die_info *lookup_die = NULL;
14496 struct partial_die_info part_die;
14497
14498 part_die.offset = offset;
b64f50a1
JK
14499 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14500 offset.sect_off);
72bf9492 14501
72bf9492
DJ
14502 return lookup_die;
14503}
14504
348e048f
DE
14505/* Find a partial DIE at OFFSET, which may or may not be in CU,
14506 except in the case of .debug_types DIEs which do not reference
14507 outside their CU (they do however referencing other types via
55f1336d 14508 DW_FORM_ref_sig8). */
72bf9492
DJ
14509
14510static struct partial_die_info *
36586728 14511find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14512{
bb5ed363 14513 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14514 struct dwarf2_per_cu_data *per_cu = NULL;
14515 struct partial_die_info *pd = NULL;
72bf9492 14516
36586728
TT
14517 if (offset_in_dwz == cu->per_cu->is_dwz
14518 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14519 {
14520 pd = find_partial_die_in_comp_unit (offset, cu);
14521 if (pd != NULL)
14522 return pd;
0d99eb77
DE
14523 /* We missed recording what we needed.
14524 Load all dies and try again. */
14525 per_cu = cu->per_cu;
5afb4e99 14526 }
0d99eb77
DE
14527 else
14528 {
14529 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14530 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14531 {
14532 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14533 " external reference to offset 0x%lx [in module %s].\n"),
14534 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14535 bfd_get_filename (objfile->obfd));
14536 }
36586728
TT
14537 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14538 objfile);
72bf9492 14539
0d99eb77
DE
14540 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14541 load_partial_comp_unit (per_cu);
ae038cb0 14542
0d99eb77
DE
14543 per_cu->cu->last_used = 0;
14544 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14545 }
5afb4e99 14546
dee91e82
DE
14547 /* If we didn't find it, and not all dies have been loaded,
14548 load them all and try again. */
14549
5afb4e99
DJ
14550 if (pd == NULL && per_cu->load_all_dies == 0)
14551 {
5afb4e99 14552 per_cu->load_all_dies = 1;
fd820528
DE
14553
14554 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14555 THIS_CU->cu may already be in use. So we can't just free it and
14556 replace its DIEs with the ones we read in. Instead, we leave those
14557 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14558 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14559 set. */
dee91e82 14560 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14561
14562 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14563 }
14564
14565 if (pd == NULL)
14566 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14567 _("could not find partial DIE 0x%x "
14568 "in cache [from module %s]\n"),
b64f50a1 14569 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14570 return pd;
72bf9492
DJ
14571}
14572
abc72ce4
DE
14573/* See if we can figure out if the class lives in a namespace. We do
14574 this by looking for a member function; its demangled name will
14575 contain namespace info, if there is any. */
14576
14577static void
14578guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14579 struct dwarf2_cu *cu)
14580{
14581 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14582 what template types look like, because the demangler
14583 frequently doesn't give the same name as the debug info. We
14584 could fix this by only using the demangled name to get the
14585 prefix (but see comment in read_structure_type). */
14586
14587 struct partial_die_info *real_pdi;
14588 struct partial_die_info *child_pdi;
14589
14590 /* If this DIE (this DIE's specification, if any) has a parent, then
14591 we should not do this. We'll prepend the parent's fully qualified
14592 name when we create the partial symbol. */
14593
14594 real_pdi = struct_pdi;
14595 while (real_pdi->has_specification)
36586728
TT
14596 real_pdi = find_partial_die (real_pdi->spec_offset,
14597 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14598
14599 if (real_pdi->die_parent != NULL)
14600 return;
14601
14602 for (child_pdi = struct_pdi->die_child;
14603 child_pdi != NULL;
14604 child_pdi = child_pdi->die_sibling)
14605 {
14606 if (child_pdi->tag == DW_TAG_subprogram
14607 && child_pdi->linkage_name != NULL)
14608 {
14609 char *actual_class_name
14610 = language_class_name_from_physname (cu->language_defn,
14611 child_pdi->linkage_name);
14612 if (actual_class_name != NULL)
14613 {
14614 struct_pdi->name
10f0c4bb
TT
14615 = obstack_copy0 (&cu->objfile->objfile_obstack,
14616 actual_class_name,
14617 strlen (actual_class_name));
abc72ce4
DE
14618 xfree (actual_class_name);
14619 }
14620 break;
14621 }
14622 }
14623}
14624
72bf9492
DJ
14625/* Adjust PART_DIE before generating a symbol for it. This function
14626 may set the is_external flag or change the DIE's name. */
14627
14628static void
14629fixup_partial_die (struct partial_die_info *part_die,
14630 struct dwarf2_cu *cu)
14631{
abc72ce4
DE
14632 /* Once we've fixed up a die, there's no point in doing so again.
14633 This also avoids a memory leak if we were to call
14634 guess_partial_die_structure_name multiple times. */
14635 if (part_die->fixup_called)
14636 return;
14637
72bf9492
DJ
14638 /* If we found a reference attribute and the DIE has no name, try
14639 to find a name in the referred to DIE. */
14640
14641 if (part_die->name == NULL && part_die->has_specification)
14642 {
14643 struct partial_die_info *spec_die;
72bf9492 14644
36586728
TT
14645 spec_die = find_partial_die (part_die->spec_offset,
14646 part_die->spec_is_dwz, cu);
72bf9492 14647
10b3939b 14648 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14649
14650 if (spec_die->name)
14651 {
14652 part_die->name = spec_die->name;
14653
14654 /* Copy DW_AT_external attribute if it is set. */
14655 if (spec_die->is_external)
14656 part_die->is_external = spec_die->is_external;
14657 }
14658 }
14659
14660 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14661
14662 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14663 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14664
abc72ce4
DE
14665 /* If there is no parent die to provide a namespace, and there are
14666 children, see if we can determine the namespace from their linkage
122d1940 14667 name. */
abc72ce4 14668 if (cu->language == language_cplus
8b70b953 14669 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14670 && part_die->die_parent == NULL
14671 && part_die->has_children
14672 && (part_die->tag == DW_TAG_class_type
14673 || part_die->tag == DW_TAG_structure_type
14674 || part_die->tag == DW_TAG_union_type))
14675 guess_partial_die_structure_name (part_die, cu);
14676
53832f31
TT
14677 /* GCC might emit a nameless struct or union that has a linkage
14678 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14679 if (part_die->name == NULL
96408a79
SA
14680 && (part_die->tag == DW_TAG_class_type
14681 || part_die->tag == DW_TAG_interface_type
14682 || part_die->tag == DW_TAG_structure_type
14683 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14684 && part_die->linkage_name != NULL)
14685 {
14686 char *demangled;
14687
8de20a37 14688 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14689 if (demangled)
14690 {
96408a79
SA
14691 const char *base;
14692
14693 /* Strip any leading namespaces/classes, keep only the base name.
14694 DW_AT_name for named DIEs does not contain the prefixes. */
14695 base = strrchr (demangled, ':');
14696 if (base && base > demangled && base[-1] == ':')
14697 base++;
14698 else
14699 base = demangled;
14700
10f0c4bb
TT
14701 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14702 base, strlen (base));
53832f31
TT
14703 xfree (demangled);
14704 }
14705 }
14706
abc72ce4 14707 part_die->fixup_called = 1;
72bf9492
DJ
14708}
14709
a8329558 14710/* Read an attribute value described by an attribute form. */
c906108c 14711
d521ce57 14712static const gdb_byte *
dee91e82
DE
14713read_attribute_value (const struct die_reader_specs *reader,
14714 struct attribute *attr, unsigned form,
d521ce57 14715 const gdb_byte *info_ptr)
c906108c 14716{
dee91e82
DE
14717 struct dwarf2_cu *cu = reader->cu;
14718 bfd *abfd = reader->abfd;
e7c27a73 14719 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14720 unsigned int bytes_read;
14721 struct dwarf_block *blk;
14722
a8329558
KW
14723 attr->form = form;
14724 switch (form)
c906108c 14725 {
c906108c 14726 case DW_FORM_ref_addr:
ae411497 14727 if (cu->header.version == 2)
4568ecf9 14728 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14729 else
4568ecf9
DE
14730 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14731 &cu->header, &bytes_read);
ae411497
TT
14732 info_ptr += bytes_read;
14733 break;
36586728
TT
14734 case DW_FORM_GNU_ref_alt:
14735 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14736 info_ptr += bytes_read;
14737 break;
ae411497 14738 case DW_FORM_addr:
e7c27a73 14739 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14740 info_ptr += bytes_read;
c906108c
SS
14741 break;
14742 case DW_FORM_block2:
7b5a2f43 14743 blk = dwarf_alloc_block (cu);
c906108c
SS
14744 blk->size = read_2_bytes (abfd, info_ptr);
14745 info_ptr += 2;
14746 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14747 info_ptr += blk->size;
14748 DW_BLOCK (attr) = blk;
14749 break;
14750 case DW_FORM_block4:
7b5a2f43 14751 blk = dwarf_alloc_block (cu);
c906108c
SS
14752 blk->size = read_4_bytes (abfd, info_ptr);
14753 info_ptr += 4;
14754 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14755 info_ptr += blk->size;
14756 DW_BLOCK (attr) = blk;
14757 break;
14758 case DW_FORM_data2:
14759 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14760 info_ptr += 2;
14761 break;
14762 case DW_FORM_data4:
14763 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14764 info_ptr += 4;
14765 break;
14766 case DW_FORM_data8:
14767 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14768 info_ptr += 8;
14769 break;
2dc7f7b3
TT
14770 case DW_FORM_sec_offset:
14771 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14772 info_ptr += bytes_read;
14773 break;
c906108c 14774 case DW_FORM_string:
9b1c24c8 14775 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14776 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14777 info_ptr += bytes_read;
14778 break;
4bdf3d34 14779 case DW_FORM_strp:
36586728
TT
14780 if (!cu->per_cu->is_dwz)
14781 {
14782 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14783 &bytes_read);
14784 DW_STRING_IS_CANONICAL (attr) = 0;
14785 info_ptr += bytes_read;
14786 break;
14787 }
14788 /* FALLTHROUGH */
14789 case DW_FORM_GNU_strp_alt:
14790 {
14791 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14792 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14793 &bytes_read);
14794
14795 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14796 DW_STRING_IS_CANONICAL (attr) = 0;
14797 info_ptr += bytes_read;
14798 }
4bdf3d34 14799 break;
2dc7f7b3 14800 case DW_FORM_exprloc:
c906108c 14801 case DW_FORM_block:
7b5a2f43 14802 blk = dwarf_alloc_block (cu);
c906108c
SS
14803 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14804 info_ptr += bytes_read;
14805 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14806 info_ptr += blk->size;
14807 DW_BLOCK (attr) = blk;
14808 break;
14809 case DW_FORM_block1:
7b5a2f43 14810 blk = dwarf_alloc_block (cu);
c906108c
SS
14811 blk->size = read_1_byte (abfd, info_ptr);
14812 info_ptr += 1;
14813 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14814 info_ptr += blk->size;
14815 DW_BLOCK (attr) = blk;
14816 break;
14817 case DW_FORM_data1:
14818 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14819 info_ptr += 1;
14820 break;
14821 case DW_FORM_flag:
14822 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14823 info_ptr += 1;
14824 break;
2dc7f7b3
TT
14825 case DW_FORM_flag_present:
14826 DW_UNSND (attr) = 1;
14827 break;
c906108c
SS
14828 case DW_FORM_sdata:
14829 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14830 info_ptr += bytes_read;
14831 break;
14832 case DW_FORM_udata:
14833 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14834 info_ptr += bytes_read;
14835 break;
14836 case DW_FORM_ref1:
4568ecf9
DE
14837 DW_UNSND (attr) = (cu->header.offset.sect_off
14838 + read_1_byte (abfd, info_ptr));
c906108c
SS
14839 info_ptr += 1;
14840 break;
14841 case DW_FORM_ref2:
4568ecf9
DE
14842 DW_UNSND (attr) = (cu->header.offset.sect_off
14843 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14844 info_ptr += 2;
14845 break;
14846 case DW_FORM_ref4:
4568ecf9
DE
14847 DW_UNSND (attr) = (cu->header.offset.sect_off
14848 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14849 info_ptr += 4;
14850 break;
613e1657 14851 case DW_FORM_ref8:
4568ecf9
DE
14852 DW_UNSND (attr) = (cu->header.offset.sect_off
14853 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14854 info_ptr += 8;
14855 break;
55f1336d 14856 case DW_FORM_ref_sig8:
ac9ec31b 14857 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14858 info_ptr += 8;
14859 break;
c906108c 14860 case DW_FORM_ref_udata:
4568ecf9
DE
14861 DW_UNSND (attr) = (cu->header.offset.sect_off
14862 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14863 info_ptr += bytes_read;
14864 break;
c906108c 14865 case DW_FORM_indirect:
a8329558
KW
14866 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14867 info_ptr += bytes_read;
dee91e82 14868 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14869 break;
3019eac3
DE
14870 case DW_FORM_GNU_addr_index:
14871 if (reader->dwo_file == NULL)
14872 {
14873 /* For now flag a hard error.
14874 Later we can turn this into a complaint. */
14875 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14876 dwarf_form_name (form),
14877 bfd_get_filename (abfd));
14878 }
14879 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14880 info_ptr += bytes_read;
14881 break;
14882 case DW_FORM_GNU_str_index:
14883 if (reader->dwo_file == NULL)
14884 {
14885 /* For now flag a hard error.
14886 Later we can turn this into a complaint if warranted. */
14887 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14888 dwarf_form_name (form),
14889 bfd_get_filename (abfd));
14890 }
14891 {
14892 ULONGEST str_index =
14893 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14894
14895 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14896 DW_STRING_IS_CANONICAL (attr) = 0;
14897 info_ptr += bytes_read;
14898 }
14899 break;
c906108c 14900 default:
8a3fe4f8 14901 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14902 dwarf_form_name (form),
14903 bfd_get_filename (abfd));
c906108c 14904 }
28e94949 14905
36586728 14906 /* Super hack. */
7771576e 14907 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
14908 attr->form = DW_FORM_GNU_ref_alt;
14909
28e94949
JB
14910 /* We have seen instances where the compiler tried to emit a byte
14911 size attribute of -1 which ended up being encoded as an unsigned
14912 0xffffffff. Although 0xffffffff is technically a valid size value,
14913 an object of this size seems pretty unlikely so we can relatively
14914 safely treat these cases as if the size attribute was invalid and
14915 treat them as zero by default. */
14916 if (attr->name == DW_AT_byte_size
14917 && form == DW_FORM_data4
14918 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14919 {
14920 complaint
14921 (&symfile_complaints,
43bbcdc2
PH
14922 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14923 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14924 DW_UNSND (attr) = 0;
14925 }
28e94949 14926
c906108c
SS
14927 return info_ptr;
14928}
14929
a8329558
KW
14930/* Read an attribute described by an abbreviated attribute. */
14931
d521ce57 14932static const gdb_byte *
dee91e82
DE
14933read_attribute (const struct die_reader_specs *reader,
14934 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14935 const gdb_byte *info_ptr)
a8329558
KW
14936{
14937 attr->name = abbrev->name;
dee91e82 14938 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14939}
14940
0963b4bd 14941/* Read dwarf information from a buffer. */
c906108c
SS
14942
14943static unsigned int
a1855c1d 14944read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14945{
fe1b8b76 14946 return bfd_get_8 (abfd, buf);
c906108c
SS
14947}
14948
14949static int
a1855c1d 14950read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14951{
fe1b8b76 14952 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14953}
14954
14955static unsigned int
a1855c1d 14956read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14957{
fe1b8b76 14958 return bfd_get_16 (abfd, buf);
c906108c
SS
14959}
14960
21ae7a4d 14961static int
a1855c1d 14962read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14963{
14964 return bfd_get_signed_16 (abfd, buf);
14965}
14966
c906108c 14967static unsigned int
a1855c1d 14968read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14969{
fe1b8b76 14970 return bfd_get_32 (abfd, buf);
c906108c
SS
14971}
14972
21ae7a4d 14973static int
a1855c1d 14974read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14975{
14976 return bfd_get_signed_32 (abfd, buf);
14977}
14978
93311388 14979static ULONGEST
a1855c1d 14980read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14981{
fe1b8b76 14982 return bfd_get_64 (abfd, buf);
c906108c
SS
14983}
14984
14985static CORE_ADDR
d521ce57 14986read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14987 unsigned int *bytes_read)
c906108c 14988{
e7c27a73 14989 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14990 CORE_ADDR retval = 0;
14991
107d2387 14992 if (cu_header->signed_addr_p)
c906108c 14993 {
107d2387
AC
14994 switch (cu_header->addr_size)
14995 {
14996 case 2:
fe1b8b76 14997 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14998 break;
14999 case 4:
fe1b8b76 15000 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
15001 break;
15002 case 8:
fe1b8b76 15003 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
15004 break;
15005 default:
8e65ff28 15006 internal_error (__FILE__, __LINE__,
e2e0b3e5 15007 _("read_address: bad switch, signed [in module %s]"),
659b0389 15008 bfd_get_filename (abfd));
107d2387
AC
15009 }
15010 }
15011 else
15012 {
15013 switch (cu_header->addr_size)
15014 {
15015 case 2:
fe1b8b76 15016 retval = bfd_get_16 (abfd, buf);
107d2387
AC
15017 break;
15018 case 4:
fe1b8b76 15019 retval = bfd_get_32 (abfd, buf);
107d2387
AC
15020 break;
15021 case 8:
fe1b8b76 15022 retval = bfd_get_64 (abfd, buf);
107d2387
AC
15023 break;
15024 default:
8e65ff28 15025 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
15026 _("read_address: bad switch, "
15027 "unsigned [in module %s]"),
659b0389 15028 bfd_get_filename (abfd));
107d2387 15029 }
c906108c 15030 }
64367e0a 15031
107d2387
AC
15032 *bytes_read = cu_header->addr_size;
15033 return retval;
c906108c
SS
15034}
15035
f7ef9339 15036/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
15037 specification allows the initial length to take up either 4 bytes
15038 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
15039 bytes describe the length and all offsets will be 8 bytes in length
15040 instead of 4.
15041
f7ef9339
KB
15042 An older, non-standard 64-bit format is also handled by this
15043 function. The older format in question stores the initial length
15044 as an 8-byte quantity without an escape value. Lengths greater
15045 than 2^32 aren't very common which means that the initial 4 bytes
15046 is almost always zero. Since a length value of zero doesn't make
15047 sense for the 32-bit format, this initial zero can be considered to
15048 be an escape value which indicates the presence of the older 64-bit
15049 format. As written, the code can't detect (old format) lengths
917c78fc
MK
15050 greater than 4GB. If it becomes necessary to handle lengths
15051 somewhat larger than 4GB, we could allow other small values (such
15052 as the non-sensical values of 1, 2, and 3) to also be used as
15053 escape values indicating the presence of the old format.
f7ef9339 15054
917c78fc
MK
15055 The value returned via bytes_read should be used to increment the
15056 relevant pointer after calling read_initial_length().
c764a876 15057
613e1657
KB
15058 [ Note: read_initial_length() and read_offset() are based on the
15059 document entitled "DWARF Debugging Information Format", revision
f7ef9339 15060 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
15061 from:
15062
f7ef9339 15063 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 15064
613e1657
KB
15065 This document is only a draft and is subject to change. (So beware.)
15066
f7ef9339 15067 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
15068 determined empirically by examining 64-bit ELF files produced by
15069 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
15070
15071 - Kevin, July 16, 2002
613e1657
KB
15072 ] */
15073
15074static LONGEST
d521ce57 15075read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 15076{
fe1b8b76 15077 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 15078
dd373385 15079 if (length == 0xffffffff)
613e1657 15080 {
fe1b8b76 15081 length = bfd_get_64 (abfd, buf + 4);
613e1657 15082 *bytes_read = 12;
613e1657 15083 }
dd373385 15084 else if (length == 0)
f7ef9339 15085 {
dd373385 15086 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 15087 length = bfd_get_64 (abfd, buf);
f7ef9339 15088 *bytes_read = 8;
f7ef9339 15089 }
613e1657
KB
15090 else
15091 {
15092 *bytes_read = 4;
613e1657
KB
15093 }
15094
c764a876
DE
15095 return length;
15096}
dd373385 15097
c764a876
DE
15098/* Cover function for read_initial_length.
15099 Returns the length of the object at BUF, and stores the size of the
15100 initial length in *BYTES_READ and stores the size that offsets will be in
15101 *OFFSET_SIZE.
15102 If the initial length size is not equivalent to that specified in
15103 CU_HEADER then issue a complaint.
15104 This is useful when reading non-comp-unit headers. */
dd373385 15105
c764a876 15106static LONGEST
d521ce57 15107read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
15108 const struct comp_unit_head *cu_header,
15109 unsigned int *bytes_read,
15110 unsigned int *offset_size)
15111{
15112 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15113
15114 gdb_assert (cu_header->initial_length_size == 4
15115 || cu_header->initial_length_size == 8
15116 || cu_header->initial_length_size == 12);
15117
15118 if (cu_header->initial_length_size != *bytes_read)
15119 complaint (&symfile_complaints,
15120 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 15121
c764a876 15122 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 15123 return length;
613e1657
KB
15124}
15125
15126/* Read an offset from the data stream. The size of the offset is
917c78fc 15127 given by cu_header->offset_size. */
613e1657
KB
15128
15129static LONGEST
d521ce57
TT
15130read_offset (bfd *abfd, const gdb_byte *buf,
15131 const struct comp_unit_head *cu_header,
891d2f0b 15132 unsigned int *bytes_read)
c764a876
DE
15133{
15134 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 15135
c764a876
DE
15136 *bytes_read = cu_header->offset_size;
15137 return offset;
15138}
15139
15140/* Read an offset from the data stream. */
15141
15142static LONGEST
d521ce57 15143read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
15144{
15145 LONGEST retval = 0;
15146
c764a876 15147 switch (offset_size)
613e1657
KB
15148 {
15149 case 4:
fe1b8b76 15150 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15151 break;
15152 case 8:
fe1b8b76 15153 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15154 break;
15155 default:
8e65ff28 15156 internal_error (__FILE__, __LINE__,
c764a876 15157 _("read_offset_1: bad switch [in module %s]"),
659b0389 15158 bfd_get_filename (abfd));
613e1657
KB
15159 }
15160
917c78fc 15161 return retval;
613e1657
KB
15162}
15163
d521ce57
TT
15164static const gdb_byte *
15165read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15166{
15167 /* If the size of a host char is 8 bits, we can return a pointer
15168 to the buffer, otherwise we have to copy the data to a buffer
15169 allocated on the temporary obstack. */
4bdf3d34 15170 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15171 return buf;
c906108c
SS
15172}
15173
d521ce57
TT
15174static const char *
15175read_direct_string (bfd *abfd, const gdb_byte *buf,
15176 unsigned int *bytes_read_ptr)
c906108c
SS
15177{
15178 /* If the size of a host char is 8 bits, we can return a pointer
15179 to the string, otherwise we have to copy the string to a buffer
15180 allocated on the temporary obstack. */
4bdf3d34 15181 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15182 if (*buf == '\0')
15183 {
15184 *bytes_read_ptr = 1;
15185 return NULL;
15186 }
d521ce57
TT
15187 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15188 return (const char *) buf;
4bdf3d34
JJ
15189}
15190
d521ce57 15191static const char *
cf2c3c16 15192read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15193{
be391dca 15194 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15195 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15196 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15197 bfd_get_filename (abfd));
dce234bc 15198 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15199 error (_("DW_FORM_strp pointing outside of "
15200 ".debug_str section [in module %s]"),
15201 bfd_get_filename (abfd));
4bdf3d34 15202 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15203 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15204 return NULL;
d521ce57 15205 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15206}
15207
36586728
TT
15208/* Read a string at offset STR_OFFSET in the .debug_str section from
15209 the .dwz file DWZ. Throw an error if the offset is too large. If
15210 the string consists of a single NUL byte, return NULL; otherwise
15211 return a pointer to the string. */
15212
d521ce57 15213static const char *
36586728
TT
15214read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15215{
15216 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15217
15218 if (dwz->str.buffer == NULL)
15219 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15220 "section [in module %s]"),
15221 bfd_get_filename (dwz->dwz_bfd));
15222 if (str_offset >= dwz->str.size)
15223 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15224 ".debug_str section [in module %s]"),
15225 bfd_get_filename (dwz->dwz_bfd));
15226 gdb_assert (HOST_CHAR_BIT == 8);
15227 if (dwz->str.buffer[str_offset] == '\0')
15228 return NULL;
d521ce57 15229 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15230}
15231
d521ce57
TT
15232static const char *
15233read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15234 const struct comp_unit_head *cu_header,
15235 unsigned int *bytes_read_ptr)
15236{
15237 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15238
15239 return read_indirect_string_at_offset (abfd, str_offset);
15240}
15241
12df843f 15242static ULONGEST
d521ce57
TT
15243read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15244 unsigned int *bytes_read_ptr)
c906108c 15245{
12df843f 15246 ULONGEST result;
ce5d95e1 15247 unsigned int num_read;
c906108c
SS
15248 int i, shift;
15249 unsigned char byte;
15250
15251 result = 0;
15252 shift = 0;
15253 num_read = 0;
15254 i = 0;
15255 while (1)
15256 {
fe1b8b76 15257 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15258 buf++;
15259 num_read++;
12df843f 15260 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
15261 if ((byte & 128) == 0)
15262 {
15263 break;
15264 }
15265 shift += 7;
15266 }
15267 *bytes_read_ptr = num_read;
15268 return result;
15269}
15270
12df843f 15271static LONGEST
d521ce57
TT
15272read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15273 unsigned int *bytes_read_ptr)
c906108c 15274{
12df843f 15275 LONGEST result;
77e0b926 15276 int i, shift, num_read;
c906108c
SS
15277 unsigned char byte;
15278
15279 result = 0;
15280 shift = 0;
c906108c
SS
15281 num_read = 0;
15282 i = 0;
15283 while (1)
15284 {
fe1b8b76 15285 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15286 buf++;
15287 num_read++;
12df843f 15288 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
15289 shift += 7;
15290 if ((byte & 128) == 0)
15291 {
15292 break;
15293 }
15294 }
77e0b926 15295 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 15296 result |= -(((LONGEST) 1) << shift);
c906108c
SS
15297 *bytes_read_ptr = num_read;
15298 return result;
15299}
15300
3019eac3
DE
15301/* Given index ADDR_INDEX in .debug_addr, fetch the value.
15302 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15303 ADDR_SIZE is the size of addresses from the CU header. */
15304
15305static CORE_ADDR
15306read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15307{
15308 struct objfile *objfile = dwarf2_per_objfile->objfile;
15309 bfd *abfd = objfile->obfd;
15310 const gdb_byte *info_ptr;
15311
15312 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15313 if (dwarf2_per_objfile->addr.buffer == NULL)
15314 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 15315 objfile_name (objfile));
3019eac3
DE
15316 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15317 error (_("DW_FORM_addr_index pointing outside of "
15318 ".debug_addr section [in module %s]"),
4262abfb 15319 objfile_name (objfile));
3019eac3
DE
15320 info_ptr = (dwarf2_per_objfile->addr.buffer
15321 + addr_base + addr_index * addr_size);
15322 if (addr_size == 4)
15323 return bfd_get_32 (abfd, info_ptr);
15324 else
15325 return bfd_get_64 (abfd, info_ptr);
15326}
15327
15328/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15329
15330static CORE_ADDR
15331read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15332{
15333 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15334}
15335
15336/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15337
15338static CORE_ADDR
d521ce57 15339read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
15340 unsigned int *bytes_read)
15341{
15342 bfd *abfd = cu->objfile->obfd;
15343 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15344
15345 return read_addr_index (cu, addr_index);
15346}
15347
15348/* Data structure to pass results from dwarf2_read_addr_index_reader
15349 back to dwarf2_read_addr_index. */
15350
15351struct dwarf2_read_addr_index_data
15352{
15353 ULONGEST addr_base;
15354 int addr_size;
15355};
15356
15357/* die_reader_func for dwarf2_read_addr_index. */
15358
15359static void
15360dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 15361 const gdb_byte *info_ptr,
3019eac3
DE
15362 struct die_info *comp_unit_die,
15363 int has_children,
15364 void *data)
15365{
15366 struct dwarf2_cu *cu = reader->cu;
15367 struct dwarf2_read_addr_index_data *aidata =
15368 (struct dwarf2_read_addr_index_data *) data;
15369
15370 aidata->addr_base = cu->addr_base;
15371 aidata->addr_size = cu->header.addr_size;
15372}
15373
15374/* Given an index in .debug_addr, fetch the value.
15375 NOTE: This can be called during dwarf expression evaluation,
15376 long after the debug information has been read, and thus per_cu->cu
15377 may no longer exist. */
15378
15379CORE_ADDR
15380dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15381 unsigned int addr_index)
15382{
15383 struct objfile *objfile = per_cu->objfile;
15384 struct dwarf2_cu *cu = per_cu->cu;
15385 ULONGEST addr_base;
15386 int addr_size;
15387
15388 /* This is intended to be called from outside this file. */
15389 dw2_setup (objfile);
15390
15391 /* We need addr_base and addr_size.
15392 If we don't have PER_CU->cu, we have to get it.
15393 Nasty, but the alternative is storing the needed info in PER_CU,
15394 which at this point doesn't seem justified: it's not clear how frequently
15395 it would get used and it would increase the size of every PER_CU.
15396 Entry points like dwarf2_per_cu_addr_size do a similar thing
15397 so we're not in uncharted territory here.
15398 Alas we need to be a bit more complicated as addr_base is contained
15399 in the DIE.
15400
15401 We don't need to read the entire CU(/TU).
15402 We just need the header and top level die.
a1b64ce1 15403
3019eac3 15404 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 15405 For now we skip this optimization. */
3019eac3
DE
15406
15407 if (cu != NULL)
15408 {
15409 addr_base = cu->addr_base;
15410 addr_size = cu->header.addr_size;
15411 }
15412 else
15413 {
15414 struct dwarf2_read_addr_index_data aidata;
15415
a1b64ce1
DE
15416 /* Note: We can't use init_cutu_and_read_dies_simple here,
15417 we need addr_base. */
15418 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15419 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
15420 addr_base = aidata.addr_base;
15421 addr_size = aidata.addr_size;
15422 }
15423
15424 return read_addr_index_1 (addr_index, addr_base, addr_size);
15425}
15426
15427/* Given a DW_AT_str_index, fetch the string. */
15428
d521ce57 15429static const char *
3019eac3
DE
15430read_str_index (const struct die_reader_specs *reader,
15431 struct dwarf2_cu *cu, ULONGEST str_index)
15432{
15433 struct objfile *objfile = dwarf2_per_objfile->objfile;
4262abfb 15434 const char *dwo_name = objfile_name (objfile);
3019eac3
DE
15435 bfd *abfd = objfile->obfd;
15436 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 15437 const gdb_byte *info_ptr;
3019eac3
DE
15438 ULONGEST str_offset;
15439
15440 dwarf2_read_section (objfile, &sections->str);
15441 dwarf2_read_section (objfile, &sections->str_offsets);
15442 if (sections->str.buffer == NULL)
15443 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15444 " in CU at offset 0x%lx [in module %s]"),
15445 (long) cu->header.offset.sect_off, dwo_name);
15446 if (sections->str_offsets.buffer == NULL)
15447 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15448 " in CU at offset 0x%lx [in module %s]"),
15449 (long) cu->header.offset.sect_off, dwo_name);
15450 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15451 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15452 " section in CU at offset 0x%lx [in module %s]"),
15453 (long) cu->header.offset.sect_off, dwo_name);
15454 info_ptr = (sections->str_offsets.buffer
15455 + str_index * cu->header.offset_size);
15456 if (cu->header.offset_size == 4)
15457 str_offset = bfd_get_32 (abfd, info_ptr);
15458 else
15459 str_offset = bfd_get_64 (abfd, info_ptr);
15460 if (str_offset >= sections->str.size)
15461 error (_("Offset from DW_FORM_str_index pointing outside of"
15462 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15463 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 15464 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
15465}
15466
3019eac3
DE
15467/* Return the length of an LEB128 number in BUF. */
15468
15469static int
15470leb128_size (const gdb_byte *buf)
15471{
15472 const gdb_byte *begin = buf;
15473 gdb_byte byte;
15474
15475 while (1)
15476 {
15477 byte = *buf++;
15478 if ((byte & 128) == 0)
15479 return buf - begin;
15480 }
15481}
15482
c906108c 15483static void
e142c38c 15484set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
15485{
15486 switch (lang)
15487 {
15488 case DW_LANG_C89:
76bee0cc 15489 case DW_LANG_C99:
c906108c 15490 case DW_LANG_C:
d1be3247 15491 case DW_LANG_UPC:
e142c38c 15492 cu->language = language_c;
c906108c
SS
15493 break;
15494 case DW_LANG_C_plus_plus:
e142c38c 15495 cu->language = language_cplus;
c906108c 15496 break;
6aecb9c2
JB
15497 case DW_LANG_D:
15498 cu->language = language_d;
15499 break;
c906108c
SS
15500 case DW_LANG_Fortran77:
15501 case DW_LANG_Fortran90:
b21b22e0 15502 case DW_LANG_Fortran95:
e142c38c 15503 cu->language = language_fortran;
c906108c 15504 break;
a766d390
DE
15505 case DW_LANG_Go:
15506 cu->language = language_go;
15507 break;
c906108c 15508 case DW_LANG_Mips_Assembler:
e142c38c 15509 cu->language = language_asm;
c906108c 15510 break;
bebd888e 15511 case DW_LANG_Java:
e142c38c 15512 cu->language = language_java;
bebd888e 15513 break;
c906108c 15514 case DW_LANG_Ada83:
8aaf0b47 15515 case DW_LANG_Ada95:
bc5f45f8
JB
15516 cu->language = language_ada;
15517 break;
72019c9c
GM
15518 case DW_LANG_Modula2:
15519 cu->language = language_m2;
15520 break;
fe8e67fd
PM
15521 case DW_LANG_Pascal83:
15522 cu->language = language_pascal;
15523 break;
22566fbd
DJ
15524 case DW_LANG_ObjC:
15525 cu->language = language_objc;
15526 break;
c906108c
SS
15527 case DW_LANG_Cobol74:
15528 case DW_LANG_Cobol85:
c906108c 15529 default:
e142c38c 15530 cu->language = language_minimal;
c906108c
SS
15531 break;
15532 }
e142c38c 15533 cu->language_defn = language_def (cu->language);
c906108c
SS
15534}
15535
15536/* Return the named attribute or NULL if not there. */
15537
15538static struct attribute *
e142c38c 15539dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15540{
a48e046c 15541 for (;;)
c906108c 15542 {
a48e046c
TT
15543 unsigned int i;
15544 struct attribute *spec = NULL;
15545
15546 for (i = 0; i < die->num_attrs; ++i)
15547 {
15548 if (die->attrs[i].name == name)
15549 return &die->attrs[i];
15550 if (die->attrs[i].name == DW_AT_specification
15551 || die->attrs[i].name == DW_AT_abstract_origin)
15552 spec = &die->attrs[i];
15553 }
15554
15555 if (!spec)
15556 break;
c906108c 15557
f2f0e013 15558 die = follow_die_ref (die, spec, &cu);
f2f0e013 15559 }
c5aa993b 15560
c906108c
SS
15561 return NULL;
15562}
15563
348e048f
DE
15564/* Return the named attribute or NULL if not there,
15565 but do not follow DW_AT_specification, etc.
15566 This is for use in contexts where we're reading .debug_types dies.
15567 Following DW_AT_specification, DW_AT_abstract_origin will take us
15568 back up the chain, and we want to go down. */
15569
15570static struct attribute *
45e58e77 15571dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15572{
15573 unsigned int i;
15574
15575 for (i = 0; i < die->num_attrs; ++i)
15576 if (die->attrs[i].name == name)
15577 return &die->attrs[i];
15578
15579 return NULL;
15580}
15581
05cf31d1
JB
15582/* Return non-zero iff the attribute NAME is defined for the given DIE,
15583 and holds a non-zero value. This function should only be used for
2dc7f7b3 15584 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15585
15586static int
15587dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15588{
15589 struct attribute *attr = dwarf2_attr (die, name, cu);
15590
15591 return (attr && DW_UNSND (attr));
15592}
15593
3ca72b44 15594static int
e142c38c 15595die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15596{
05cf31d1
JB
15597 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15598 which value is non-zero. However, we have to be careful with
15599 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15600 (via dwarf2_flag_true_p) follows this attribute. So we may
15601 end up accidently finding a declaration attribute that belongs
15602 to a different DIE referenced by the specification attribute,
15603 even though the given DIE does not have a declaration attribute. */
15604 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15605 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15606}
15607
63d06c5c 15608/* Return the die giving the specification for DIE, if there is
f2f0e013 15609 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15610 containing the return value on output. If there is no
15611 specification, but there is an abstract origin, that is
15612 returned. */
63d06c5c
DC
15613
15614static struct die_info *
f2f0e013 15615die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15616{
f2f0e013
DJ
15617 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15618 *spec_cu);
63d06c5c 15619
edb3359d
DJ
15620 if (spec_attr == NULL)
15621 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15622
63d06c5c
DC
15623 if (spec_attr == NULL)
15624 return NULL;
15625 else
f2f0e013 15626 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15627}
c906108c 15628
debd256d 15629/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15630 refers to.
15631 NOTE: This is also used as a "cleanup" function. */
15632
debd256d
JB
15633static void
15634free_line_header (struct line_header *lh)
15635{
15636 if (lh->standard_opcode_lengths)
a8bc7b56 15637 xfree (lh->standard_opcode_lengths);
debd256d
JB
15638
15639 /* Remember that all the lh->file_names[i].name pointers are
15640 pointers into debug_line_buffer, and don't need to be freed. */
15641 if (lh->file_names)
a8bc7b56 15642 xfree (lh->file_names);
debd256d
JB
15643
15644 /* Similarly for the include directory names. */
15645 if (lh->include_dirs)
a8bc7b56 15646 xfree (lh->include_dirs);
debd256d 15647
a8bc7b56 15648 xfree (lh);
debd256d
JB
15649}
15650
debd256d 15651/* Add an entry to LH's include directory table. */
ae2de4f8 15652
debd256d 15653static void
d521ce57 15654add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15655{
debd256d
JB
15656 /* Grow the array if necessary. */
15657 if (lh->include_dirs_size == 0)
c5aa993b 15658 {
debd256d
JB
15659 lh->include_dirs_size = 1; /* for testing */
15660 lh->include_dirs = xmalloc (lh->include_dirs_size
15661 * sizeof (*lh->include_dirs));
15662 }
15663 else if (lh->num_include_dirs >= lh->include_dirs_size)
15664 {
15665 lh->include_dirs_size *= 2;
15666 lh->include_dirs = xrealloc (lh->include_dirs,
15667 (lh->include_dirs_size
15668 * sizeof (*lh->include_dirs)));
c5aa993b 15669 }
c906108c 15670
debd256d
JB
15671 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15672}
6e70227d 15673
debd256d 15674/* Add an entry to LH's file name table. */
ae2de4f8 15675
debd256d
JB
15676static void
15677add_file_name (struct line_header *lh,
d521ce57 15678 const char *name,
debd256d
JB
15679 unsigned int dir_index,
15680 unsigned int mod_time,
15681 unsigned int length)
15682{
15683 struct file_entry *fe;
15684
15685 /* Grow the array if necessary. */
15686 if (lh->file_names_size == 0)
15687 {
15688 lh->file_names_size = 1; /* for testing */
15689 lh->file_names = xmalloc (lh->file_names_size
15690 * sizeof (*lh->file_names));
15691 }
15692 else if (lh->num_file_names >= lh->file_names_size)
15693 {
15694 lh->file_names_size *= 2;
15695 lh->file_names = xrealloc (lh->file_names,
15696 (lh->file_names_size
15697 * sizeof (*lh->file_names)));
15698 }
15699
15700 fe = &lh->file_names[lh->num_file_names++];
15701 fe->name = name;
15702 fe->dir_index = dir_index;
15703 fe->mod_time = mod_time;
15704 fe->length = length;
aaa75496 15705 fe->included_p = 0;
cb1df416 15706 fe->symtab = NULL;
debd256d 15707}
6e70227d 15708
36586728
TT
15709/* A convenience function to find the proper .debug_line section for a
15710 CU. */
15711
15712static struct dwarf2_section_info *
15713get_debug_line_section (struct dwarf2_cu *cu)
15714{
15715 struct dwarf2_section_info *section;
15716
15717 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15718 DWO file. */
15719 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15720 section = &cu->dwo_unit->dwo_file->sections.line;
15721 else if (cu->per_cu->is_dwz)
15722 {
15723 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15724
15725 section = &dwz->line;
15726 }
15727 else
15728 section = &dwarf2_per_objfile->line;
15729
15730 return section;
15731}
15732
debd256d 15733/* Read the statement program header starting at OFFSET in
3019eac3 15734 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15735 to a struct line_header, allocated using xmalloc.
debd256d
JB
15736
15737 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15738 the returned object point into the dwarf line section buffer,
15739 and must not be freed. */
ae2de4f8 15740
debd256d 15741static struct line_header *
3019eac3 15742dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15743{
15744 struct cleanup *back_to;
15745 struct line_header *lh;
d521ce57 15746 const gdb_byte *line_ptr;
c764a876 15747 unsigned int bytes_read, offset_size;
debd256d 15748 int i;
d521ce57 15749 const char *cur_dir, *cur_file;
3019eac3
DE
15750 struct dwarf2_section_info *section;
15751 bfd *abfd;
15752
36586728 15753 section = get_debug_line_section (cu);
3019eac3
DE
15754 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15755 if (section->buffer == NULL)
debd256d 15756 {
3019eac3
DE
15757 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15758 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15759 else
15760 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15761 return 0;
15762 }
15763
fceca515
DE
15764 /* We can't do this until we know the section is non-empty.
15765 Only then do we know we have such a section. */
15766 abfd = section->asection->owner;
15767
a738430d
MK
15768 /* Make sure that at least there's room for the total_length field.
15769 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15770 if (offset + 4 >= section->size)
debd256d 15771 {
4d3c2250 15772 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15773 return 0;
15774 }
15775
15776 lh = xmalloc (sizeof (*lh));
15777 memset (lh, 0, sizeof (*lh));
15778 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15779 (void *) lh);
15780
3019eac3 15781 line_ptr = section->buffer + offset;
debd256d 15782
a738430d 15783 /* Read in the header. */
6e70227d 15784 lh->total_length =
c764a876
DE
15785 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15786 &bytes_read, &offset_size);
debd256d 15787 line_ptr += bytes_read;
3019eac3 15788 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15789 {
4d3c2250 15790 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 15791 do_cleanups (back_to);
debd256d
JB
15792 return 0;
15793 }
15794 lh->statement_program_end = line_ptr + lh->total_length;
15795 lh->version = read_2_bytes (abfd, line_ptr);
15796 line_ptr += 2;
c764a876
DE
15797 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15798 line_ptr += offset_size;
debd256d
JB
15799 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15800 line_ptr += 1;
2dc7f7b3
TT
15801 if (lh->version >= 4)
15802 {
15803 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15804 line_ptr += 1;
15805 }
15806 else
15807 lh->maximum_ops_per_instruction = 1;
15808
15809 if (lh->maximum_ops_per_instruction == 0)
15810 {
15811 lh->maximum_ops_per_instruction = 1;
15812 complaint (&symfile_complaints,
3e43a32a
MS
15813 _("invalid maximum_ops_per_instruction "
15814 "in `.debug_line' section"));
2dc7f7b3
TT
15815 }
15816
debd256d
JB
15817 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15818 line_ptr += 1;
15819 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15820 line_ptr += 1;
15821 lh->line_range = read_1_byte (abfd, line_ptr);
15822 line_ptr += 1;
15823 lh->opcode_base = read_1_byte (abfd, line_ptr);
15824 line_ptr += 1;
15825 lh->standard_opcode_lengths
fe1b8b76 15826 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15827
15828 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15829 for (i = 1; i < lh->opcode_base; ++i)
15830 {
15831 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15832 line_ptr += 1;
15833 }
15834
a738430d 15835 /* Read directory table. */
9b1c24c8 15836 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15837 {
15838 line_ptr += bytes_read;
15839 add_include_dir (lh, cur_dir);
15840 }
15841 line_ptr += bytes_read;
15842
a738430d 15843 /* Read file name table. */
9b1c24c8 15844 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15845 {
15846 unsigned int dir_index, mod_time, length;
15847
15848 line_ptr += bytes_read;
15849 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15850 line_ptr += bytes_read;
15851 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15852 line_ptr += bytes_read;
15853 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15854 line_ptr += bytes_read;
15855
15856 add_file_name (lh, cur_file, dir_index, mod_time, length);
15857 }
15858 line_ptr += bytes_read;
6e70227d 15859 lh->statement_program_start = line_ptr;
debd256d 15860
3019eac3 15861 if (line_ptr > (section->buffer + section->size))
4d3c2250 15862 complaint (&symfile_complaints,
3e43a32a
MS
15863 _("line number info header doesn't "
15864 "fit in `.debug_line' section"));
debd256d
JB
15865
15866 discard_cleanups (back_to);
15867 return lh;
15868}
c906108c 15869
c6da4cef
DE
15870/* Subroutine of dwarf_decode_lines to simplify it.
15871 Return the file name of the psymtab for included file FILE_INDEX
15872 in line header LH of PST.
15873 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15874 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15875 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15876
15877 The function creates dangling cleanup registration. */
c6da4cef 15878
d521ce57 15879static const char *
c6da4cef
DE
15880psymtab_include_file_name (const struct line_header *lh, int file_index,
15881 const struct partial_symtab *pst,
15882 const char *comp_dir)
15883{
15884 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15885 const char *include_name = fe.name;
15886 const char *include_name_to_compare = include_name;
15887 const char *dir_name = NULL;
72b9f47f
TT
15888 const char *pst_filename;
15889 char *copied_name = NULL;
c6da4cef
DE
15890 int file_is_pst;
15891
15892 if (fe.dir_index)
15893 dir_name = lh->include_dirs[fe.dir_index - 1];
15894
15895 if (!IS_ABSOLUTE_PATH (include_name)
15896 && (dir_name != NULL || comp_dir != NULL))
15897 {
15898 /* Avoid creating a duplicate psymtab for PST.
15899 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15900 Before we do the comparison, however, we need to account
15901 for DIR_NAME and COMP_DIR.
15902 First prepend dir_name (if non-NULL). If we still don't
15903 have an absolute path prepend comp_dir (if non-NULL).
15904 However, the directory we record in the include-file's
15905 psymtab does not contain COMP_DIR (to match the
15906 corresponding symtab(s)).
15907
15908 Example:
15909
15910 bash$ cd /tmp
15911 bash$ gcc -g ./hello.c
15912 include_name = "hello.c"
15913 dir_name = "."
15914 DW_AT_comp_dir = comp_dir = "/tmp"
15915 DW_AT_name = "./hello.c" */
15916
15917 if (dir_name != NULL)
15918 {
d521ce57
TT
15919 char *tem = concat (dir_name, SLASH_STRING,
15920 include_name, (char *)NULL);
15921
15922 make_cleanup (xfree, tem);
15923 include_name = tem;
c6da4cef 15924 include_name_to_compare = include_name;
c6da4cef
DE
15925 }
15926 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15927 {
d521ce57
TT
15928 char *tem = concat (comp_dir, SLASH_STRING,
15929 include_name, (char *)NULL);
15930
15931 make_cleanup (xfree, tem);
15932 include_name_to_compare = tem;
c6da4cef
DE
15933 }
15934 }
15935
15936 pst_filename = pst->filename;
15937 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15938 {
72b9f47f
TT
15939 copied_name = concat (pst->dirname, SLASH_STRING,
15940 pst_filename, (char *)NULL);
15941 pst_filename = copied_name;
c6da4cef
DE
15942 }
15943
1e3fad37 15944 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15945
72b9f47f
TT
15946 if (copied_name != NULL)
15947 xfree (copied_name);
c6da4cef
DE
15948
15949 if (file_is_pst)
15950 return NULL;
15951 return include_name;
15952}
15953
c91513d8
PP
15954/* Ignore this record_line request. */
15955
15956static void
15957noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15958{
15959 return;
15960}
15961
f3f5162e
DE
15962/* Subroutine of dwarf_decode_lines to simplify it.
15963 Process the line number information in LH. */
debd256d 15964
c906108c 15965static void
f3f5162e
DE
15966dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15967 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15968{
d521ce57
TT
15969 const gdb_byte *line_ptr, *extended_end;
15970 const gdb_byte *line_end;
a8c50c1f 15971 unsigned int bytes_read, extended_len;
c906108c 15972 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15973 CORE_ADDR baseaddr;
15974 struct objfile *objfile = cu->objfile;
f3f5162e 15975 bfd *abfd = objfile->obfd;
fbf65064 15976 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15977 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15978 struct subfile *last_subfile = NULL;
c91513d8
PP
15979 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15980 = record_line;
e142c38c
DJ
15981
15982 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15983
debd256d
JB
15984 line_ptr = lh->statement_program_start;
15985 line_end = lh->statement_program_end;
c906108c
SS
15986
15987 /* Read the statement sequences until there's nothing left. */
15988 while (line_ptr < line_end)
15989 {
15990 /* state machine registers */
15991 CORE_ADDR address = 0;
15992 unsigned int file = 1;
15993 unsigned int line = 1;
15994 unsigned int column = 0;
debd256d 15995 int is_stmt = lh->default_is_stmt;
c906108c
SS
15996 int basic_block = 0;
15997 int end_sequence = 0;
fbf65064 15998 CORE_ADDR addr;
2dc7f7b3 15999 unsigned char op_index = 0;
c906108c 16000
aaa75496 16001 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 16002 {
aaa75496 16003 /* Start a subfile for the current file of the state machine. */
debd256d
JB
16004 /* lh->include_dirs and lh->file_names are 0-based, but the
16005 directory and file name numbers in the statement program
16006 are 1-based. */
16007 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 16008 const char *dir = NULL;
a738430d 16009
debd256d
JB
16010 if (fe->dir_index)
16011 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
16012
16013 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
16014 }
16015
a738430d 16016 /* Decode the table. */
c5aa993b 16017 while (!end_sequence)
c906108c
SS
16018 {
16019 op_code = read_1_byte (abfd, line_ptr);
16020 line_ptr += 1;
59205f5a
JB
16021 if (line_ptr > line_end)
16022 {
16023 dwarf2_debug_line_missing_end_sequence_complaint ();
16024 break;
16025 }
9aa1fe7e 16026
debd256d 16027 if (op_code >= lh->opcode_base)
6e70227d 16028 {
a738430d 16029 /* Special operand. */
debd256d 16030 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
16031 address += (((op_index + (adj_opcode / lh->line_range))
16032 / lh->maximum_ops_per_instruction)
16033 * lh->minimum_instruction_length);
16034 op_index = ((op_index + (adj_opcode / lh->line_range))
16035 % lh->maximum_ops_per_instruction);
debd256d 16036 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 16037 if (lh->num_file_names < file || file == 0)
25e43795 16038 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
16039 /* For now we ignore lines not starting on an
16040 instruction boundary. */
16041 else if (op_index == 0)
25e43795
DJ
16042 {
16043 lh->file_names[file - 1].included_p = 1;
ca5f395d 16044 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16045 {
16046 if (last_subfile != current_subfile)
16047 {
16048 addr = gdbarch_addr_bits_remove (gdbarch, address);
16049 if (last_subfile)
c91513d8 16050 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16051 last_subfile = current_subfile;
16052 }
25e43795 16053 /* Append row to matrix using current values. */
7019d805 16054 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16055 (*p_record_line) (current_subfile, line, addr);
366da635 16056 }
25e43795 16057 }
ca5f395d 16058 basic_block = 0;
9aa1fe7e
GK
16059 }
16060 else switch (op_code)
c906108c
SS
16061 {
16062 case DW_LNS_extended_op:
3e43a32a
MS
16063 extended_len = read_unsigned_leb128 (abfd, line_ptr,
16064 &bytes_read);
473b7be6 16065 line_ptr += bytes_read;
a8c50c1f 16066 extended_end = line_ptr + extended_len;
c906108c
SS
16067 extended_op = read_1_byte (abfd, line_ptr);
16068 line_ptr += 1;
16069 switch (extended_op)
16070 {
16071 case DW_LNE_end_sequence:
c91513d8 16072 p_record_line = record_line;
c906108c 16073 end_sequence = 1;
c906108c
SS
16074 break;
16075 case DW_LNE_set_address:
e7c27a73 16076 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
16077
16078 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16079 {
16080 /* This line table is for a function which has been
16081 GCd by the linker. Ignore it. PR gdb/12528 */
16082
16083 long line_offset
36586728 16084 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
16085
16086 complaint (&symfile_complaints,
16087 _(".debug_line address at offset 0x%lx is 0 "
16088 "[in module %s]"),
4262abfb 16089 line_offset, objfile_name (objfile));
c91513d8
PP
16090 p_record_line = noop_record_line;
16091 }
16092
2dc7f7b3 16093 op_index = 0;
107d2387
AC
16094 line_ptr += bytes_read;
16095 address += baseaddr;
c906108c
SS
16096 break;
16097 case DW_LNE_define_file:
debd256d 16098 {
d521ce57 16099 const char *cur_file;
debd256d 16100 unsigned int dir_index, mod_time, length;
6e70227d 16101
3e43a32a
MS
16102 cur_file = read_direct_string (abfd, line_ptr,
16103 &bytes_read);
debd256d
JB
16104 line_ptr += bytes_read;
16105 dir_index =
16106 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16107 line_ptr += bytes_read;
16108 mod_time =
16109 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16110 line_ptr += bytes_read;
16111 length =
16112 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16113 line_ptr += bytes_read;
16114 add_file_name (lh, cur_file, dir_index, mod_time, length);
16115 }
c906108c 16116 break;
d0c6ba3d
CC
16117 case DW_LNE_set_discriminator:
16118 /* The discriminator is not interesting to the debugger;
16119 just ignore it. */
16120 line_ptr = extended_end;
16121 break;
c906108c 16122 default:
4d3c2250 16123 complaint (&symfile_complaints,
e2e0b3e5 16124 _("mangled .debug_line section"));
debd256d 16125 return;
c906108c 16126 }
a8c50c1f
DJ
16127 /* Make sure that we parsed the extended op correctly. If e.g.
16128 we expected a different address size than the producer used,
16129 we may have read the wrong number of bytes. */
16130 if (line_ptr != extended_end)
16131 {
16132 complaint (&symfile_complaints,
16133 _("mangled .debug_line section"));
16134 return;
16135 }
c906108c
SS
16136 break;
16137 case DW_LNS_copy:
59205f5a 16138 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16139 dwarf2_debug_line_missing_file_complaint ();
16140 else
366da635 16141 {
25e43795 16142 lh->file_names[file - 1].included_p = 1;
ca5f395d 16143 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16144 {
16145 if (last_subfile != current_subfile)
16146 {
16147 addr = gdbarch_addr_bits_remove (gdbarch, address);
16148 if (last_subfile)
c91513d8 16149 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16150 last_subfile = current_subfile;
16151 }
7019d805 16152 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16153 (*p_record_line) (current_subfile, line, addr);
fbf65064 16154 }
366da635 16155 }
c906108c
SS
16156 basic_block = 0;
16157 break;
16158 case DW_LNS_advance_pc:
2dc7f7b3
TT
16159 {
16160 CORE_ADDR adjust
16161 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16162
16163 address += (((op_index + adjust)
16164 / lh->maximum_ops_per_instruction)
16165 * lh->minimum_instruction_length);
16166 op_index = ((op_index + adjust)
16167 % lh->maximum_ops_per_instruction);
16168 line_ptr += bytes_read;
16169 }
c906108c
SS
16170 break;
16171 case DW_LNS_advance_line:
16172 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16173 line_ptr += bytes_read;
16174 break;
16175 case DW_LNS_set_file:
debd256d 16176 {
a738430d
MK
16177 /* The arrays lh->include_dirs and lh->file_names are
16178 0-based, but the directory and file name numbers in
16179 the statement program are 1-based. */
debd256d 16180 struct file_entry *fe;
d521ce57 16181 const char *dir = NULL;
a738430d 16182
debd256d
JB
16183 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16184 line_ptr += bytes_read;
59205f5a 16185 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16186 dwarf2_debug_line_missing_file_complaint ();
16187 else
16188 {
16189 fe = &lh->file_names[file - 1];
16190 if (fe->dir_index)
16191 dir = lh->include_dirs[fe->dir_index - 1];
16192 if (!decode_for_pst_p)
16193 {
16194 last_subfile = current_subfile;
16195 dwarf2_start_subfile (fe->name, dir, comp_dir);
16196 }
16197 }
debd256d 16198 }
c906108c
SS
16199 break;
16200 case DW_LNS_set_column:
16201 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16202 line_ptr += bytes_read;
16203 break;
16204 case DW_LNS_negate_stmt:
16205 is_stmt = (!is_stmt);
16206 break;
16207 case DW_LNS_set_basic_block:
16208 basic_block = 1;
16209 break;
c2c6d25f
JM
16210 /* Add to the address register of the state machine the
16211 address increment value corresponding to special opcode
a738430d
MK
16212 255. I.e., this value is scaled by the minimum
16213 instruction length since special opcode 255 would have
b021a221 16214 scaled the increment. */
c906108c 16215 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16216 {
16217 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16218
16219 address += (((op_index + adjust)
16220 / lh->maximum_ops_per_instruction)
16221 * lh->minimum_instruction_length);
16222 op_index = ((op_index + adjust)
16223 % lh->maximum_ops_per_instruction);
16224 }
c906108c
SS
16225 break;
16226 case DW_LNS_fixed_advance_pc:
16227 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16228 op_index = 0;
c906108c
SS
16229 line_ptr += 2;
16230 break;
9aa1fe7e 16231 default:
a738430d
MK
16232 {
16233 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16234 int i;
a738430d 16235
debd256d 16236 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16237 {
16238 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16239 line_ptr += bytes_read;
16240 }
16241 }
c906108c
SS
16242 }
16243 }
59205f5a
JB
16244 if (lh->num_file_names < file || file == 0)
16245 dwarf2_debug_line_missing_file_complaint ();
16246 else
16247 {
16248 lh->file_names[file - 1].included_p = 1;
16249 if (!decode_for_pst_p)
fbf65064
UW
16250 {
16251 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16252 (*p_record_line) (current_subfile, 0, addr);
fbf65064 16253 }
59205f5a 16254 }
c906108c 16255 }
f3f5162e
DE
16256}
16257
16258/* Decode the Line Number Program (LNP) for the given line_header
16259 structure and CU. The actual information extracted and the type
16260 of structures created from the LNP depends on the value of PST.
16261
16262 1. If PST is NULL, then this procedure uses the data from the program
16263 to create all necessary symbol tables, and their linetables.
16264
16265 2. If PST is not NULL, this procedure reads the program to determine
16266 the list of files included by the unit represented by PST, and
16267 builds all the associated partial symbol tables.
16268
16269 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16270 It is used for relative paths in the line table.
16271 NOTE: When processing partial symtabs (pst != NULL),
16272 comp_dir == pst->dirname.
16273
16274 NOTE: It is important that psymtabs have the same file name (via strcmp)
16275 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16276 symtab we don't use it in the name of the psymtabs we create.
16277 E.g. expand_line_sal requires this when finding psymtabs to expand.
16278 A good testcase for this is mb-inline.exp. */
16279
16280static void
16281dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16282 struct dwarf2_cu *cu, struct partial_symtab *pst,
16283 int want_line_info)
16284{
16285 struct objfile *objfile = cu->objfile;
16286 const int decode_for_pst_p = (pst != NULL);
16287 struct subfile *first_subfile = current_subfile;
16288
16289 if (want_line_info)
16290 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
16291
16292 if (decode_for_pst_p)
16293 {
16294 int file_index;
16295
16296 /* Now that we're done scanning the Line Header Program, we can
16297 create the psymtab of each included file. */
16298 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16299 if (lh->file_names[file_index].included_p == 1)
16300 {
d521ce57 16301 const char *include_name =
c6da4cef
DE
16302 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16303 if (include_name != NULL)
aaa75496
JB
16304 dwarf2_create_include_psymtab (include_name, pst, objfile);
16305 }
16306 }
cb1df416
DJ
16307 else
16308 {
16309 /* Make sure a symtab is created for every file, even files
16310 which contain only variables (i.e. no code with associated
16311 line numbers). */
cb1df416 16312 int i;
cb1df416
DJ
16313
16314 for (i = 0; i < lh->num_file_names; i++)
16315 {
d521ce57 16316 const char *dir = NULL;
f3f5162e 16317 struct file_entry *fe;
9a619af0 16318
cb1df416
DJ
16319 fe = &lh->file_names[i];
16320 if (fe->dir_index)
16321 dir = lh->include_dirs[fe->dir_index - 1];
16322 dwarf2_start_subfile (fe->name, dir, comp_dir);
16323
16324 /* Skip the main file; we don't need it, and it must be
16325 allocated last, so that it will show up before the
16326 non-primary symtabs in the objfile's symtab list. */
16327 if (current_subfile == first_subfile)
16328 continue;
16329
16330 if (current_subfile->symtab == NULL)
16331 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 16332 objfile);
cb1df416
DJ
16333 fe->symtab = current_subfile->symtab;
16334 }
16335 }
c906108c
SS
16336}
16337
16338/* Start a subfile for DWARF. FILENAME is the name of the file and
16339 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
16340 or NULL if not known. COMP_DIR is the compilation directory for the
16341 linetable's compilation unit or NULL if not known.
c906108c
SS
16342 This routine tries to keep line numbers from identical absolute and
16343 relative file names in a common subfile.
16344
16345 Using the `list' example from the GDB testsuite, which resides in
16346 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16347 of /srcdir/list0.c yields the following debugging information for list0.c:
16348
c5aa993b
JM
16349 DW_AT_name: /srcdir/list0.c
16350 DW_AT_comp_dir: /compdir
357e46e7 16351 files.files[0].name: list0.h
c5aa993b 16352 files.files[0].dir: /srcdir
357e46e7 16353 files.files[1].name: list0.c
c5aa993b 16354 files.files[1].dir: /srcdir
c906108c
SS
16355
16356 The line number information for list0.c has to end up in a single
4f1520fb
FR
16357 subfile, so that `break /srcdir/list0.c:1' works as expected.
16358 start_subfile will ensure that this happens provided that we pass the
16359 concatenation of files.files[1].dir and files.files[1].name as the
16360 subfile's name. */
c906108c
SS
16361
16362static void
d521ce57 16363dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 16364 const char *comp_dir)
c906108c 16365{
d521ce57 16366 char *copy = NULL;
4f1520fb
FR
16367
16368 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16369 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16370 second argument to start_subfile. To be consistent, we do the
16371 same here. In order not to lose the line information directory,
16372 we concatenate it to the filename when it makes sense.
16373 Note that the Dwarf3 standard says (speaking of filenames in line
16374 information): ``The directory index is ignored for file names
16375 that represent full path names''. Thus ignoring dirname in the
16376 `else' branch below isn't an issue. */
c906108c 16377
d5166ae1 16378 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
16379 {
16380 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16381 filename = copy;
16382 }
c906108c 16383
d521ce57 16384 start_subfile (filename, comp_dir);
4f1520fb 16385
d521ce57
TT
16386 if (copy != NULL)
16387 xfree (copy);
c906108c
SS
16388}
16389
f4dc4d17
DE
16390/* Start a symtab for DWARF.
16391 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16392
16393static void
16394dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 16395 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
16396{
16397 start_symtab (name, comp_dir, low_pc);
16398 record_debugformat ("DWARF 2");
16399 record_producer (cu->producer);
16400
16401 /* We assume that we're processing GCC output. */
16402 processing_gcc_compilation = 2;
16403
4d4ec4e5 16404 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
16405}
16406
4c2df51b
DJ
16407static void
16408var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 16409 struct dwarf2_cu *cu)
4c2df51b 16410{
e7c27a73
DJ
16411 struct objfile *objfile = cu->objfile;
16412 struct comp_unit_head *cu_header = &cu->header;
16413
4c2df51b
DJ
16414 /* NOTE drow/2003-01-30: There used to be a comment and some special
16415 code here to turn a symbol with DW_AT_external and a
16416 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16417 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16418 with some versions of binutils) where shared libraries could have
16419 relocations against symbols in their debug information - the
16420 minimal symbol would have the right address, but the debug info
16421 would not. It's no longer necessary, because we will explicitly
16422 apply relocations when we read in the debug information now. */
16423
16424 /* A DW_AT_location attribute with no contents indicates that a
16425 variable has been optimized away. */
16426 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16427 {
f1e6e072 16428 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
16429 return;
16430 }
16431
16432 /* Handle one degenerate form of location expression specially, to
16433 preserve GDB's previous behavior when section offsets are
3019eac3
DE
16434 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16435 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
16436
16437 if (attr_form_is_block (attr)
3019eac3
DE
16438 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16439 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16440 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16441 && (DW_BLOCK (attr)->size
16442 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 16443 {
891d2f0b 16444 unsigned int dummy;
4c2df51b 16445
3019eac3
DE
16446 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16447 SYMBOL_VALUE_ADDRESS (sym) =
16448 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16449 else
16450 SYMBOL_VALUE_ADDRESS (sym) =
16451 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 16452 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
16453 fixup_symbol_section (sym, objfile);
16454 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16455 SYMBOL_SECTION (sym));
4c2df51b
DJ
16456 return;
16457 }
16458
16459 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16460 expression evaluator, and use LOC_COMPUTED only when necessary
16461 (i.e. when the value of a register or memory location is
16462 referenced, or a thread-local block, etc.). Then again, it might
16463 not be worthwhile. I'm assuming that it isn't unless performance
16464 or memory numbers show me otherwise. */
16465
f1e6e072 16466 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 16467
f1e6e072 16468 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 16469 cu->has_loclist = 1;
4c2df51b
DJ
16470}
16471
c906108c
SS
16472/* Given a pointer to a DWARF information entry, figure out if we need
16473 to make a symbol table entry for it, and if so, create a new entry
16474 and return a pointer to it.
16475 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
16476 used the passed type.
16477 If SPACE is not NULL, use it to hold the new symbol. If it is
16478 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
16479
16480static struct symbol *
34eaf542
TT
16481new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16482 struct symbol *space)
c906108c 16483{
e7c27a73 16484 struct objfile *objfile = cu->objfile;
c906108c 16485 struct symbol *sym = NULL;
15d034d0 16486 const char *name;
c906108c
SS
16487 struct attribute *attr = NULL;
16488 struct attribute *attr2 = NULL;
e142c38c 16489 CORE_ADDR baseaddr;
e37fd15a
SW
16490 struct pending **list_to_add = NULL;
16491
edb3359d 16492 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
16493
16494 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16495
94af9270 16496 name = dwarf2_name (die, cu);
c906108c
SS
16497 if (name)
16498 {
94af9270 16499 const char *linkagename;
34eaf542 16500 int suppress_add = 0;
94af9270 16501
34eaf542
TT
16502 if (space)
16503 sym = space;
16504 else
e623cf5d 16505 sym = allocate_symbol (objfile);
c906108c 16506 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16507
16508 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16509 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16510 linkagename = dwarf2_physname (name, die, cu);
16511 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16512
f55ee35c
JK
16513 /* Fortran does not have mangling standard and the mangling does differ
16514 between gfortran, iFort etc. */
16515 if (cu->language == language_fortran
b250c185 16516 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16517 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16518 dwarf2_full_name (name, die, cu),
29df156d 16519 NULL);
f55ee35c 16520
c906108c 16521 /* Default assumptions.
c5aa993b 16522 Use the passed type or decode it from the die. */
176620f1 16523 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16524 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16525 if (type != NULL)
16526 SYMBOL_TYPE (sym) = type;
16527 else
e7c27a73 16528 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16529 attr = dwarf2_attr (die,
16530 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16531 cu);
c906108c
SS
16532 if (attr)
16533 {
16534 SYMBOL_LINE (sym) = DW_UNSND (attr);
16535 }
cb1df416 16536
edb3359d
DJ
16537 attr = dwarf2_attr (die,
16538 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16539 cu);
cb1df416
DJ
16540 if (attr)
16541 {
16542 int file_index = DW_UNSND (attr);
9a619af0 16543
cb1df416
DJ
16544 if (cu->line_header == NULL
16545 || file_index > cu->line_header->num_file_names)
16546 complaint (&symfile_complaints,
16547 _("file index out of range"));
1c3d648d 16548 else if (file_index > 0)
cb1df416
DJ
16549 {
16550 struct file_entry *fe;
9a619af0 16551
cb1df416
DJ
16552 fe = &cu->line_header->file_names[file_index - 1];
16553 SYMBOL_SYMTAB (sym) = fe->symtab;
16554 }
16555 }
16556
c906108c
SS
16557 switch (die->tag)
16558 {
16559 case DW_TAG_label:
e142c38c 16560 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16561 if (attr)
16562 {
16563 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16564 }
0f5238ed
TT
16565 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16566 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16567 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16568 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16569 break;
16570 case DW_TAG_subprogram:
16571 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16572 finish_block. */
f1e6e072 16573 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16574 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16575 if ((attr2 && (DW_UNSND (attr2) != 0))
16576 || cu->language == language_ada)
c906108c 16577 {
2cfa0c8d
JB
16578 /* Subprograms marked external are stored as a global symbol.
16579 Ada subprograms, whether marked external or not, are always
16580 stored as a global symbol, because we want to be able to
16581 access them globally. For instance, we want to be able
16582 to break on a nested subprogram without having to
16583 specify the context. */
e37fd15a 16584 list_to_add = &global_symbols;
c906108c
SS
16585 }
16586 else
16587 {
e37fd15a 16588 list_to_add = cu->list_in_scope;
c906108c
SS
16589 }
16590 break;
edb3359d
DJ
16591 case DW_TAG_inlined_subroutine:
16592 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16593 finish_block. */
f1e6e072 16594 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16595 SYMBOL_INLINED (sym) = 1;
481860b3 16596 list_to_add = cu->list_in_scope;
edb3359d 16597 break;
34eaf542
TT
16598 case DW_TAG_template_value_param:
16599 suppress_add = 1;
16600 /* Fall through. */
72929c62 16601 case DW_TAG_constant:
c906108c 16602 case DW_TAG_variable:
254e6b9e 16603 case DW_TAG_member:
0963b4bd
MS
16604 /* Compilation with minimal debug info may result in
16605 variables with missing type entries. Change the
16606 misleading `void' type to something sensible. */
c906108c 16607 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16608 SYMBOL_TYPE (sym)
46bf5051 16609 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16610
e142c38c 16611 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16612 /* In the case of DW_TAG_member, we should only be called for
16613 static const members. */
16614 if (die->tag == DW_TAG_member)
16615 {
3863f96c
DE
16616 /* dwarf2_add_field uses die_is_declaration,
16617 so we do the same. */
254e6b9e
DE
16618 gdb_assert (die_is_declaration (die, cu));
16619 gdb_assert (attr);
16620 }
c906108c
SS
16621 if (attr)
16622 {
e7c27a73 16623 dwarf2_const_value (attr, sym, cu);
e142c38c 16624 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16625 if (!suppress_add)
34eaf542
TT
16626 {
16627 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16628 list_to_add = &global_symbols;
34eaf542 16629 else
e37fd15a 16630 list_to_add = cu->list_in_scope;
34eaf542 16631 }
c906108c
SS
16632 break;
16633 }
e142c38c 16634 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16635 if (attr)
16636 {
e7c27a73 16637 var_decode_location (attr, sym, cu);
e142c38c 16638 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16639
16640 /* Fortran explicitly imports any global symbols to the local
16641 scope by DW_TAG_common_block. */
16642 if (cu->language == language_fortran && die->parent
16643 && die->parent->tag == DW_TAG_common_block)
16644 attr2 = NULL;
16645
caac4577
JG
16646 if (SYMBOL_CLASS (sym) == LOC_STATIC
16647 && SYMBOL_VALUE_ADDRESS (sym) == 0
16648 && !dwarf2_per_objfile->has_section_at_zero)
16649 {
16650 /* When a static variable is eliminated by the linker,
16651 the corresponding debug information is not stripped
16652 out, but the variable address is set to null;
16653 do not add such variables into symbol table. */
16654 }
16655 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16656 {
f55ee35c
JK
16657 /* Workaround gfortran PR debug/40040 - it uses
16658 DW_AT_location for variables in -fPIC libraries which may
16659 get overriden by other libraries/executable and get
16660 a different address. Resolve it by the minimal symbol
16661 which may come from inferior's executable using copy
16662 relocation. Make this workaround only for gfortran as for
16663 other compilers GDB cannot guess the minimal symbol
16664 Fortran mangling kind. */
16665 if (cu->language == language_fortran && die->parent
16666 && die->parent->tag == DW_TAG_module
16667 && cu->producer
16668 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16669 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16670
1c809c68
TT
16671 /* A variable with DW_AT_external is never static,
16672 but it may be block-scoped. */
16673 list_to_add = (cu->list_in_scope == &file_symbols
16674 ? &global_symbols : cu->list_in_scope);
1c809c68 16675 }
c906108c 16676 else
e37fd15a 16677 list_to_add = cu->list_in_scope;
c906108c
SS
16678 }
16679 else
16680 {
16681 /* We do not know the address of this symbol.
c5aa993b
JM
16682 If it is an external symbol and we have type information
16683 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16684 The address of the variable will then be determined from
16685 the minimal symbol table whenever the variable is
16686 referenced. */
e142c38c 16687 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16688
16689 /* Fortran explicitly imports any global symbols to the local
16690 scope by DW_TAG_common_block. */
16691 if (cu->language == language_fortran && die->parent
16692 && die->parent->tag == DW_TAG_common_block)
16693 {
16694 /* SYMBOL_CLASS doesn't matter here because
16695 read_common_block is going to reset it. */
16696 if (!suppress_add)
16697 list_to_add = cu->list_in_scope;
16698 }
16699 else if (attr2 && (DW_UNSND (attr2) != 0)
16700 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16701 {
0fe7935b
DJ
16702 /* A variable with DW_AT_external is never static, but it
16703 may be block-scoped. */
16704 list_to_add = (cu->list_in_scope == &file_symbols
16705 ? &global_symbols : cu->list_in_scope);
16706
f1e6e072 16707 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16708 }
442ddf59
JK
16709 else if (!die_is_declaration (die, cu))
16710 {
16711 /* Use the default LOC_OPTIMIZED_OUT class. */
16712 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16713 if (!suppress_add)
16714 list_to_add = cu->list_in_scope;
442ddf59 16715 }
c906108c
SS
16716 }
16717 break;
16718 case DW_TAG_formal_parameter:
edb3359d
DJ
16719 /* If we are inside a function, mark this as an argument. If
16720 not, we might be looking at an argument to an inlined function
16721 when we do not have enough information to show inlined frames;
16722 pretend it's a local variable in that case so that the user can
16723 still see it. */
16724 if (context_stack_depth > 0
16725 && context_stack[context_stack_depth - 1].name != NULL)
16726 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16727 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16728 if (attr)
16729 {
e7c27a73 16730 var_decode_location (attr, sym, cu);
c906108c 16731 }
e142c38c 16732 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16733 if (attr)
16734 {
e7c27a73 16735 dwarf2_const_value (attr, sym, cu);
c906108c 16736 }
f346a30d 16737
e37fd15a 16738 list_to_add = cu->list_in_scope;
c906108c
SS
16739 break;
16740 case DW_TAG_unspecified_parameters:
16741 /* From varargs functions; gdb doesn't seem to have any
16742 interest in this information, so just ignore it for now.
16743 (FIXME?) */
16744 break;
34eaf542
TT
16745 case DW_TAG_template_type_param:
16746 suppress_add = 1;
16747 /* Fall through. */
c906108c 16748 case DW_TAG_class_type:
680b30c7 16749 case DW_TAG_interface_type:
c906108c
SS
16750 case DW_TAG_structure_type:
16751 case DW_TAG_union_type:
72019c9c 16752 case DW_TAG_set_type:
c906108c 16753 case DW_TAG_enumeration_type:
f1e6e072 16754 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16755 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16756
63d06c5c 16757 {
987504bb 16758 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16759 really ever be static objects: otherwise, if you try
16760 to, say, break of a class's method and you're in a file
16761 which doesn't mention that class, it won't work unless
16762 the check for all static symbols in lookup_symbol_aux
16763 saves you. See the OtherFileClass tests in
16764 gdb.c++/namespace.exp. */
16765
e37fd15a 16766 if (!suppress_add)
34eaf542 16767 {
34eaf542
TT
16768 list_to_add = (cu->list_in_scope == &file_symbols
16769 && (cu->language == language_cplus
16770 || cu->language == language_java)
16771 ? &global_symbols : cu->list_in_scope);
63d06c5c 16772
64382290
TT
16773 /* The semantics of C++ state that "struct foo {
16774 ... }" also defines a typedef for "foo". A Java
16775 class declaration also defines a typedef for the
16776 class. */
16777 if (cu->language == language_cplus
16778 || cu->language == language_java
16779 || cu->language == language_ada)
16780 {
16781 /* The symbol's name is already allocated along
16782 with this objfile, so we don't need to
16783 duplicate it for the type. */
16784 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16785 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16786 }
63d06c5c
DC
16787 }
16788 }
c906108c
SS
16789 break;
16790 case DW_TAG_typedef:
f1e6e072 16791 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16792 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16793 list_to_add = cu->list_in_scope;
63d06c5c 16794 break;
c906108c 16795 case DW_TAG_base_type:
a02abb62 16796 case DW_TAG_subrange_type:
f1e6e072 16797 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16798 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16799 list_to_add = cu->list_in_scope;
c906108c
SS
16800 break;
16801 case DW_TAG_enumerator:
e142c38c 16802 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16803 if (attr)
16804 {
e7c27a73 16805 dwarf2_const_value (attr, sym, cu);
c906108c 16806 }
63d06c5c
DC
16807 {
16808 /* NOTE: carlton/2003-11-10: See comment above in the
16809 DW_TAG_class_type, etc. block. */
16810
e142c38c 16811 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16812 && (cu->language == language_cplus
16813 || cu->language == language_java)
e142c38c 16814 ? &global_symbols : cu->list_in_scope);
63d06c5c 16815 }
c906108c 16816 break;
5c4e30ca 16817 case DW_TAG_namespace:
f1e6e072 16818 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16819 list_to_add = &global_symbols;
5c4e30ca 16820 break;
4357ac6c 16821 case DW_TAG_common_block:
f1e6e072 16822 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16823 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16824 add_symbol_to_list (sym, cu->list_in_scope);
16825 break;
c906108c
SS
16826 default:
16827 /* Not a tag we recognize. Hopefully we aren't processing
16828 trash data, but since we must specifically ignore things
16829 we don't recognize, there is nothing else we should do at
0963b4bd 16830 this point. */
e2e0b3e5 16831 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16832 dwarf_tag_name (die->tag));
c906108c
SS
16833 break;
16834 }
df8a16a1 16835
e37fd15a
SW
16836 if (suppress_add)
16837 {
16838 sym->hash_next = objfile->template_symbols;
16839 objfile->template_symbols = sym;
16840 list_to_add = NULL;
16841 }
16842
16843 if (list_to_add != NULL)
16844 add_symbol_to_list (sym, list_to_add);
16845
df8a16a1
DJ
16846 /* For the benefit of old versions of GCC, check for anonymous
16847 namespaces based on the demangled name. */
4d4ec4e5 16848 if (!cu->processing_has_namespace_info
94af9270 16849 && cu->language == language_cplus)
a10964d1 16850 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16851 }
16852 return (sym);
16853}
16854
34eaf542
TT
16855/* A wrapper for new_symbol_full that always allocates a new symbol. */
16856
16857static struct symbol *
16858new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16859{
16860 return new_symbol_full (die, type, cu, NULL);
16861}
16862
98bfdba5
PA
16863/* Given an attr with a DW_FORM_dataN value in host byte order,
16864 zero-extend it as appropriate for the symbol's type. The DWARF
16865 standard (v4) is not entirely clear about the meaning of using
16866 DW_FORM_dataN for a constant with a signed type, where the type is
16867 wider than the data. The conclusion of a discussion on the DWARF
16868 list was that this is unspecified. We choose to always zero-extend
16869 because that is the interpretation long in use by GCC. */
c906108c 16870
98bfdba5 16871static gdb_byte *
ff39bb5e 16872dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 16873 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16874{
e7c27a73 16875 struct objfile *objfile = cu->objfile;
e17a4113
UW
16876 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16877 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16878 LONGEST l = DW_UNSND (attr);
16879
16880 if (bits < sizeof (*value) * 8)
16881 {
16882 l &= ((LONGEST) 1 << bits) - 1;
16883 *value = l;
16884 }
16885 else if (bits == sizeof (*value) * 8)
16886 *value = l;
16887 else
16888 {
16889 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16890 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16891 return bytes;
16892 }
16893
16894 return NULL;
16895}
16896
16897/* Read a constant value from an attribute. Either set *VALUE, or if
16898 the value does not fit in *VALUE, set *BYTES - either already
16899 allocated on the objfile obstack, or newly allocated on OBSTACK,
16900 or, set *BATON, if we translated the constant to a location
16901 expression. */
16902
16903static void
ff39bb5e 16904dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
16905 const char *name, struct obstack *obstack,
16906 struct dwarf2_cu *cu,
d521ce57 16907 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16908 struct dwarf2_locexpr_baton **baton)
16909{
16910 struct objfile *objfile = cu->objfile;
16911 struct comp_unit_head *cu_header = &cu->header;
c906108c 16912 struct dwarf_block *blk;
98bfdba5
PA
16913 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16914 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16915
16916 *value = 0;
16917 *bytes = NULL;
16918 *baton = NULL;
c906108c
SS
16919
16920 switch (attr->form)
16921 {
16922 case DW_FORM_addr:
3019eac3 16923 case DW_FORM_GNU_addr_index:
ac56253d 16924 {
ac56253d
TT
16925 gdb_byte *data;
16926
98bfdba5
PA
16927 if (TYPE_LENGTH (type) != cu_header->addr_size)
16928 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16929 cu_header->addr_size,
98bfdba5 16930 TYPE_LENGTH (type));
ac56253d
TT
16931 /* Symbols of this form are reasonably rare, so we just
16932 piggyback on the existing location code rather than writing
16933 a new implementation of symbol_computed_ops. */
7919a973 16934 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
16935 (*baton)->per_cu = cu->per_cu;
16936 gdb_assert ((*baton)->per_cu);
ac56253d 16937
98bfdba5 16938 (*baton)->size = 2 + cu_header->addr_size;
7919a973 16939 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 16940 (*baton)->data = data;
ac56253d
TT
16941
16942 data[0] = DW_OP_addr;
16943 store_unsigned_integer (&data[1], cu_header->addr_size,
16944 byte_order, DW_ADDR (attr));
16945 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16946 }
c906108c 16947 break;
4ac36638 16948 case DW_FORM_string:
93b5768b 16949 case DW_FORM_strp:
3019eac3 16950 case DW_FORM_GNU_str_index:
36586728 16951 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16952 /* DW_STRING is already allocated on the objfile obstack, point
16953 directly to it. */
d521ce57 16954 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16955 break;
c906108c
SS
16956 case DW_FORM_block1:
16957 case DW_FORM_block2:
16958 case DW_FORM_block4:
16959 case DW_FORM_block:
2dc7f7b3 16960 case DW_FORM_exprloc:
c906108c 16961 blk = DW_BLOCK (attr);
98bfdba5
PA
16962 if (TYPE_LENGTH (type) != blk->size)
16963 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16964 TYPE_LENGTH (type));
16965 *bytes = blk->data;
c906108c 16966 break;
2df3850c
JM
16967
16968 /* The DW_AT_const_value attributes are supposed to carry the
16969 symbol's value "represented as it would be on the target
16970 architecture." By the time we get here, it's already been
16971 converted to host endianness, so we just need to sign- or
16972 zero-extend it as appropriate. */
16973 case DW_FORM_data1:
3aef2284 16974 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 16975 break;
c906108c 16976 case DW_FORM_data2:
3aef2284 16977 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 16978 break;
c906108c 16979 case DW_FORM_data4:
3aef2284 16980 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 16981 break;
c906108c 16982 case DW_FORM_data8:
3aef2284 16983 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
16984 break;
16985
c906108c 16986 case DW_FORM_sdata:
98bfdba5 16987 *value = DW_SND (attr);
2df3850c
JM
16988 break;
16989
c906108c 16990 case DW_FORM_udata:
98bfdba5 16991 *value = DW_UNSND (attr);
c906108c 16992 break;
2df3850c 16993
c906108c 16994 default:
4d3c2250 16995 complaint (&symfile_complaints,
e2e0b3e5 16996 _("unsupported const value attribute form: '%s'"),
4d3c2250 16997 dwarf_form_name (attr->form));
98bfdba5 16998 *value = 0;
c906108c
SS
16999 break;
17000 }
17001}
17002
2df3850c 17003
98bfdba5
PA
17004/* Copy constant value from an attribute to a symbol. */
17005
2df3850c 17006static void
ff39bb5e 17007dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 17008 struct dwarf2_cu *cu)
2df3850c 17009{
98bfdba5
PA
17010 struct objfile *objfile = cu->objfile;
17011 struct comp_unit_head *cu_header = &cu->header;
12df843f 17012 LONGEST value;
d521ce57 17013 const gdb_byte *bytes;
98bfdba5 17014 struct dwarf2_locexpr_baton *baton;
2df3850c 17015
98bfdba5
PA
17016 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
17017 SYMBOL_PRINT_NAME (sym),
17018 &objfile->objfile_obstack, cu,
17019 &value, &bytes, &baton);
2df3850c 17020
98bfdba5
PA
17021 if (baton != NULL)
17022 {
98bfdba5 17023 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17024 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
17025 }
17026 else if (bytes != NULL)
17027 {
17028 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 17029 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
17030 }
17031 else
17032 {
17033 SYMBOL_VALUE (sym) = value;
f1e6e072 17034 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 17035 }
2df3850c
JM
17036}
17037
c906108c
SS
17038/* Return the type of the die in question using its DW_AT_type attribute. */
17039
17040static struct type *
e7c27a73 17041die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17042{
c906108c 17043 struct attribute *type_attr;
c906108c 17044
e142c38c 17045 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
17046 if (!type_attr)
17047 {
17048 /* A missing DW_AT_type represents a void type. */
46bf5051 17049 return objfile_type (cu->objfile)->builtin_void;
c906108c 17050 }
348e048f 17051
673bfd45 17052 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17053}
17054
b4ba55a1
JB
17055/* True iff CU's producer generates GNAT Ada auxiliary information
17056 that allows to find parallel types through that information instead
17057 of having to do expensive parallel lookups by type name. */
17058
17059static int
17060need_gnat_info (struct dwarf2_cu *cu)
17061{
17062 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17063 of GNAT produces this auxiliary information, without any indication
17064 that it is produced. Part of enhancing the FSF version of GNAT
17065 to produce that information will be to put in place an indicator
17066 that we can use in order to determine whether the descriptive type
17067 info is available or not. One suggestion that has been made is
17068 to use a new attribute, attached to the CU die. For now, assume
17069 that the descriptive type info is not available. */
17070 return 0;
17071}
17072
b4ba55a1
JB
17073/* Return the auxiliary type of the die in question using its
17074 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17075 attribute is not present. */
17076
17077static struct type *
17078die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17079{
b4ba55a1 17080 struct attribute *type_attr;
b4ba55a1
JB
17081
17082 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17083 if (!type_attr)
17084 return NULL;
17085
673bfd45 17086 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
17087}
17088
17089/* If DIE has a descriptive_type attribute, then set the TYPE's
17090 descriptive type accordingly. */
17091
17092static void
17093set_descriptive_type (struct type *type, struct die_info *die,
17094 struct dwarf2_cu *cu)
17095{
17096 struct type *descriptive_type = die_descriptive_type (die, cu);
17097
17098 if (descriptive_type)
17099 {
17100 ALLOCATE_GNAT_AUX_TYPE (type);
17101 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17102 }
17103}
17104
c906108c
SS
17105/* Return the containing type of the die in question using its
17106 DW_AT_containing_type attribute. */
17107
17108static struct type *
e7c27a73 17109die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17110{
c906108c 17111 struct attribute *type_attr;
c906108c 17112
e142c38c 17113 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
17114 if (!type_attr)
17115 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 17116 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 17117
673bfd45 17118 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17119}
17120
ac9ec31b
DE
17121/* Return an error marker type to use for the ill formed type in DIE/CU. */
17122
17123static struct type *
17124build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17125{
17126 struct objfile *objfile = dwarf2_per_objfile->objfile;
17127 char *message, *saved;
17128
17129 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 17130 objfile_name (objfile),
ac9ec31b
DE
17131 cu->header.offset.sect_off,
17132 die->offset.sect_off);
17133 saved = obstack_copy0 (&objfile->objfile_obstack,
17134 message, strlen (message));
17135 xfree (message);
17136
17137 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17138}
17139
673bfd45 17140/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
17141 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17142 DW_AT_containing_type.
673bfd45
DE
17143 If there is no type substitute an error marker. */
17144
c906108c 17145static struct type *
ff39bb5e 17146lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 17147 struct dwarf2_cu *cu)
c906108c 17148{
bb5ed363 17149 struct objfile *objfile = cu->objfile;
f792889a
DJ
17150 struct type *this_type;
17151
ac9ec31b
DE
17152 gdb_assert (attr->name == DW_AT_type
17153 || attr->name == DW_AT_GNAT_descriptive_type
17154 || attr->name == DW_AT_containing_type);
17155
673bfd45
DE
17156 /* First see if we have it cached. */
17157
36586728
TT
17158 if (attr->form == DW_FORM_GNU_ref_alt)
17159 {
17160 struct dwarf2_per_cu_data *per_cu;
17161 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17162
17163 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17164 this_type = get_die_type_at_offset (offset, per_cu);
17165 }
7771576e 17166 else if (attr_form_is_ref (attr))
673bfd45 17167 {
b64f50a1 17168 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17169
17170 this_type = get_die_type_at_offset (offset, cu->per_cu);
17171 }
55f1336d 17172 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17173 {
ac9ec31b 17174 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17175
ac9ec31b 17176 return get_signatured_type (die, signature, cu);
673bfd45
DE
17177 }
17178 else
17179 {
ac9ec31b
DE
17180 complaint (&symfile_complaints,
17181 _("Dwarf Error: Bad type attribute %s in DIE"
17182 " at 0x%x [in module %s]"),
17183 dwarf_attr_name (attr->name), die->offset.sect_off,
4262abfb 17184 objfile_name (objfile));
ac9ec31b 17185 return build_error_marker_type (cu, die);
673bfd45
DE
17186 }
17187
17188 /* If not cached we need to read it in. */
17189
17190 if (this_type == NULL)
17191 {
ac9ec31b 17192 struct die_info *type_die = NULL;
673bfd45
DE
17193 struct dwarf2_cu *type_cu = cu;
17194
7771576e 17195 if (attr_form_is_ref (attr))
ac9ec31b
DE
17196 type_die = follow_die_ref (die, attr, &type_cu);
17197 if (type_die == NULL)
17198 return build_error_marker_type (cu, die);
17199 /* If we find the type now, it's probably because the type came
3019eac3
DE
17200 from an inter-CU reference and the type's CU got expanded before
17201 ours. */
ac9ec31b 17202 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17203 }
17204
17205 /* If we still don't have a type use an error marker. */
17206
17207 if (this_type == NULL)
ac9ec31b 17208 return build_error_marker_type (cu, die);
673bfd45 17209
f792889a 17210 return this_type;
c906108c
SS
17211}
17212
673bfd45
DE
17213/* Return the type in DIE, CU.
17214 Returns NULL for invalid types.
17215
02142a6c 17216 This first does a lookup in die_type_hash,
673bfd45
DE
17217 and only reads the die in if necessary.
17218
17219 NOTE: This can be called when reading in partial or full symbols. */
17220
f792889a 17221static struct type *
e7c27a73 17222read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17223{
f792889a
DJ
17224 struct type *this_type;
17225
17226 this_type = get_die_type (die, cu);
17227 if (this_type)
17228 return this_type;
17229
673bfd45
DE
17230 return read_type_die_1 (die, cu);
17231}
17232
17233/* Read the type in DIE, CU.
17234 Returns NULL for invalid types. */
17235
17236static struct type *
17237read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17238{
17239 struct type *this_type = NULL;
17240
c906108c
SS
17241 switch (die->tag)
17242 {
17243 case DW_TAG_class_type:
680b30c7 17244 case DW_TAG_interface_type:
c906108c
SS
17245 case DW_TAG_structure_type:
17246 case DW_TAG_union_type:
f792889a 17247 this_type = read_structure_type (die, cu);
c906108c
SS
17248 break;
17249 case DW_TAG_enumeration_type:
f792889a 17250 this_type = read_enumeration_type (die, cu);
c906108c
SS
17251 break;
17252 case DW_TAG_subprogram:
17253 case DW_TAG_subroutine_type:
edb3359d 17254 case DW_TAG_inlined_subroutine:
f792889a 17255 this_type = read_subroutine_type (die, cu);
c906108c
SS
17256 break;
17257 case DW_TAG_array_type:
f792889a 17258 this_type = read_array_type (die, cu);
c906108c 17259 break;
72019c9c 17260 case DW_TAG_set_type:
f792889a 17261 this_type = read_set_type (die, cu);
72019c9c 17262 break;
c906108c 17263 case DW_TAG_pointer_type:
f792889a 17264 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
17265 break;
17266 case DW_TAG_ptr_to_member_type:
f792889a 17267 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
17268 break;
17269 case DW_TAG_reference_type:
f792889a 17270 this_type = read_tag_reference_type (die, cu);
c906108c
SS
17271 break;
17272 case DW_TAG_const_type:
f792889a 17273 this_type = read_tag_const_type (die, cu);
c906108c
SS
17274 break;
17275 case DW_TAG_volatile_type:
f792889a 17276 this_type = read_tag_volatile_type (die, cu);
c906108c 17277 break;
06d66ee9
TT
17278 case DW_TAG_restrict_type:
17279 this_type = read_tag_restrict_type (die, cu);
17280 break;
c906108c 17281 case DW_TAG_string_type:
f792889a 17282 this_type = read_tag_string_type (die, cu);
c906108c
SS
17283 break;
17284 case DW_TAG_typedef:
f792889a 17285 this_type = read_typedef (die, cu);
c906108c 17286 break;
a02abb62 17287 case DW_TAG_subrange_type:
f792889a 17288 this_type = read_subrange_type (die, cu);
a02abb62 17289 break;
c906108c 17290 case DW_TAG_base_type:
f792889a 17291 this_type = read_base_type (die, cu);
c906108c 17292 break;
81a17f79 17293 case DW_TAG_unspecified_type:
f792889a 17294 this_type = read_unspecified_type (die, cu);
81a17f79 17295 break;
0114d602
DJ
17296 case DW_TAG_namespace:
17297 this_type = read_namespace_type (die, cu);
17298 break;
f55ee35c
JK
17299 case DW_TAG_module:
17300 this_type = read_module_type (die, cu);
17301 break;
c906108c 17302 default:
3e43a32a
MS
17303 complaint (&symfile_complaints,
17304 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 17305 dwarf_tag_name (die->tag));
c906108c
SS
17306 break;
17307 }
63d06c5c 17308
f792889a 17309 return this_type;
63d06c5c
DC
17310}
17311
abc72ce4
DE
17312/* See if we can figure out if the class lives in a namespace. We do
17313 this by looking for a member function; its demangled name will
17314 contain namespace info, if there is any.
17315 Return the computed name or NULL.
17316 Space for the result is allocated on the objfile's obstack.
17317 This is the full-die version of guess_partial_die_structure_name.
17318 In this case we know DIE has no useful parent. */
17319
17320static char *
17321guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17322{
17323 struct die_info *spec_die;
17324 struct dwarf2_cu *spec_cu;
17325 struct die_info *child;
17326
17327 spec_cu = cu;
17328 spec_die = die_specification (die, &spec_cu);
17329 if (spec_die != NULL)
17330 {
17331 die = spec_die;
17332 cu = spec_cu;
17333 }
17334
17335 for (child = die->child;
17336 child != NULL;
17337 child = child->sibling)
17338 {
17339 if (child->tag == DW_TAG_subprogram)
17340 {
17341 struct attribute *attr;
17342
17343 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17344 if (attr == NULL)
17345 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17346 if (attr != NULL)
17347 {
17348 char *actual_name
17349 = language_class_name_from_physname (cu->language_defn,
17350 DW_STRING (attr));
17351 char *name = NULL;
17352
17353 if (actual_name != NULL)
17354 {
15d034d0 17355 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
17356
17357 if (die_name != NULL
17358 && strcmp (die_name, actual_name) != 0)
17359 {
17360 /* Strip off the class name from the full name.
17361 We want the prefix. */
17362 int die_name_len = strlen (die_name);
17363 int actual_name_len = strlen (actual_name);
17364
17365 /* Test for '::' as a sanity check. */
17366 if (actual_name_len > die_name_len + 2
3e43a32a
MS
17367 && actual_name[actual_name_len
17368 - die_name_len - 1] == ':')
abc72ce4 17369 name =
10f0c4bb
TT
17370 obstack_copy0 (&cu->objfile->objfile_obstack,
17371 actual_name,
17372 actual_name_len - die_name_len - 2);
abc72ce4
DE
17373 }
17374 }
17375 xfree (actual_name);
17376 return name;
17377 }
17378 }
17379 }
17380
17381 return NULL;
17382}
17383
96408a79
SA
17384/* GCC might emit a nameless typedef that has a linkage name. Determine the
17385 prefix part in such case. See
17386 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17387
17388static char *
17389anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17390{
17391 struct attribute *attr;
17392 char *base;
17393
17394 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17395 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17396 return NULL;
17397
17398 attr = dwarf2_attr (die, DW_AT_name, cu);
17399 if (attr != NULL && DW_STRING (attr) != NULL)
17400 return NULL;
17401
17402 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17403 if (attr == NULL)
17404 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17405 if (attr == NULL || DW_STRING (attr) == NULL)
17406 return NULL;
17407
17408 /* dwarf2_name had to be already called. */
17409 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17410
17411 /* Strip the base name, keep any leading namespaces/classes. */
17412 base = strrchr (DW_STRING (attr), ':');
17413 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17414 return "";
17415
10f0c4bb
TT
17416 return obstack_copy0 (&cu->objfile->objfile_obstack,
17417 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
17418}
17419
fdde2d81 17420/* Return the name of the namespace/class that DIE is defined within,
0114d602 17421 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 17422
0114d602
DJ
17423 For example, if we're within the method foo() in the following
17424 code:
17425
17426 namespace N {
17427 class C {
17428 void foo () {
17429 }
17430 };
17431 }
17432
17433 then determine_prefix on foo's die will return "N::C". */
fdde2d81 17434
0d5cff50 17435static const char *
e142c38c 17436determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 17437{
0114d602
DJ
17438 struct die_info *parent, *spec_die;
17439 struct dwarf2_cu *spec_cu;
17440 struct type *parent_type;
96408a79 17441 char *retval;
63d06c5c 17442
f55ee35c
JK
17443 if (cu->language != language_cplus && cu->language != language_java
17444 && cu->language != language_fortran)
0114d602
DJ
17445 return "";
17446
96408a79
SA
17447 retval = anonymous_struct_prefix (die, cu);
17448 if (retval)
17449 return retval;
17450
0114d602
DJ
17451 /* We have to be careful in the presence of DW_AT_specification.
17452 For example, with GCC 3.4, given the code
17453
17454 namespace N {
17455 void foo() {
17456 // Definition of N::foo.
17457 }
17458 }
17459
17460 then we'll have a tree of DIEs like this:
17461
17462 1: DW_TAG_compile_unit
17463 2: DW_TAG_namespace // N
17464 3: DW_TAG_subprogram // declaration of N::foo
17465 4: DW_TAG_subprogram // definition of N::foo
17466 DW_AT_specification // refers to die #3
17467
17468 Thus, when processing die #4, we have to pretend that we're in
17469 the context of its DW_AT_specification, namely the contex of die
17470 #3. */
17471 spec_cu = cu;
17472 spec_die = die_specification (die, &spec_cu);
17473 if (spec_die == NULL)
17474 parent = die->parent;
17475 else
63d06c5c 17476 {
0114d602
DJ
17477 parent = spec_die->parent;
17478 cu = spec_cu;
63d06c5c 17479 }
0114d602
DJ
17480
17481 if (parent == NULL)
17482 return "";
98bfdba5
PA
17483 else if (parent->building_fullname)
17484 {
17485 const char *name;
17486 const char *parent_name;
17487
17488 /* It has been seen on RealView 2.2 built binaries,
17489 DW_TAG_template_type_param types actually _defined_ as
17490 children of the parent class:
17491
17492 enum E {};
17493 template class <class Enum> Class{};
17494 Class<enum E> class_e;
17495
17496 1: DW_TAG_class_type (Class)
17497 2: DW_TAG_enumeration_type (E)
17498 3: DW_TAG_enumerator (enum1:0)
17499 3: DW_TAG_enumerator (enum2:1)
17500 ...
17501 2: DW_TAG_template_type_param
17502 DW_AT_type DW_FORM_ref_udata (E)
17503
17504 Besides being broken debug info, it can put GDB into an
17505 infinite loop. Consider:
17506
17507 When we're building the full name for Class<E>, we'll start
17508 at Class, and go look over its template type parameters,
17509 finding E. We'll then try to build the full name of E, and
17510 reach here. We're now trying to build the full name of E,
17511 and look over the parent DIE for containing scope. In the
17512 broken case, if we followed the parent DIE of E, we'd again
17513 find Class, and once again go look at its template type
17514 arguments, etc., etc. Simply don't consider such parent die
17515 as source-level parent of this die (it can't be, the language
17516 doesn't allow it), and break the loop here. */
17517 name = dwarf2_name (die, cu);
17518 parent_name = dwarf2_name (parent, cu);
17519 complaint (&symfile_complaints,
17520 _("template param type '%s' defined within parent '%s'"),
17521 name ? name : "<unknown>",
17522 parent_name ? parent_name : "<unknown>");
17523 return "";
17524 }
63d06c5c 17525 else
0114d602
DJ
17526 switch (parent->tag)
17527 {
63d06c5c 17528 case DW_TAG_namespace:
0114d602 17529 parent_type = read_type_die (parent, cu);
acebe513
UW
17530 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17531 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17532 Work around this problem here. */
17533 if (cu->language == language_cplus
17534 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17535 return "";
0114d602
DJ
17536 /* We give a name to even anonymous namespaces. */
17537 return TYPE_TAG_NAME (parent_type);
63d06c5c 17538 case DW_TAG_class_type:
680b30c7 17539 case DW_TAG_interface_type:
63d06c5c 17540 case DW_TAG_structure_type:
0114d602 17541 case DW_TAG_union_type:
f55ee35c 17542 case DW_TAG_module:
0114d602
DJ
17543 parent_type = read_type_die (parent, cu);
17544 if (TYPE_TAG_NAME (parent_type) != NULL)
17545 return TYPE_TAG_NAME (parent_type);
17546 else
17547 /* An anonymous structure is only allowed non-static data
17548 members; no typedefs, no member functions, et cetera.
17549 So it does not need a prefix. */
17550 return "";
abc72ce4 17551 case DW_TAG_compile_unit:
95554aad 17552 case DW_TAG_partial_unit:
abc72ce4
DE
17553 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17554 if (cu->language == language_cplus
8b70b953 17555 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17556 && die->child != NULL
17557 && (die->tag == DW_TAG_class_type
17558 || die->tag == DW_TAG_structure_type
17559 || die->tag == DW_TAG_union_type))
17560 {
17561 char *name = guess_full_die_structure_name (die, cu);
17562 if (name != NULL)
17563 return name;
17564 }
17565 return "";
63d06c5c 17566 default:
8176b9b8 17567 return determine_prefix (parent, cu);
63d06c5c 17568 }
63d06c5c
DC
17569}
17570
3e43a32a
MS
17571/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17572 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17573 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17574 an obconcat, otherwise allocate storage for the result. The CU argument is
17575 used to determine the language and hence, the appropriate separator. */
987504bb 17576
f55ee35c 17577#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17578
17579static char *
f55ee35c
JK
17580typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17581 int physname, struct dwarf2_cu *cu)
63d06c5c 17582{
f55ee35c 17583 const char *lead = "";
5c315b68 17584 const char *sep;
63d06c5c 17585
3e43a32a
MS
17586 if (suffix == NULL || suffix[0] == '\0'
17587 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17588 sep = "";
17589 else if (cu->language == language_java)
17590 sep = ".";
f55ee35c
JK
17591 else if (cu->language == language_fortran && physname)
17592 {
17593 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17594 DW_AT_MIPS_linkage_name is preferred and used instead. */
17595
17596 lead = "__";
17597 sep = "_MOD_";
17598 }
987504bb
JJ
17599 else
17600 sep = "::";
63d06c5c 17601
6dd47d34
DE
17602 if (prefix == NULL)
17603 prefix = "";
17604 if (suffix == NULL)
17605 suffix = "";
17606
987504bb
JJ
17607 if (obs == NULL)
17608 {
3e43a32a
MS
17609 char *retval
17610 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17611
f55ee35c
JK
17612 strcpy (retval, lead);
17613 strcat (retval, prefix);
6dd47d34
DE
17614 strcat (retval, sep);
17615 strcat (retval, suffix);
63d06c5c
DC
17616 return retval;
17617 }
987504bb
JJ
17618 else
17619 {
17620 /* We have an obstack. */
f55ee35c 17621 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17622 }
63d06c5c
DC
17623}
17624
c906108c
SS
17625/* Return sibling of die, NULL if no sibling. */
17626
f9aca02d 17627static struct die_info *
fba45db2 17628sibling_die (struct die_info *die)
c906108c 17629{
639d11d3 17630 return die->sibling;
c906108c
SS
17631}
17632
71c25dea
TT
17633/* Get name of a die, return NULL if not found. */
17634
15d034d0
TT
17635static const char *
17636dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17637 struct obstack *obstack)
17638{
17639 if (name && cu->language == language_cplus)
17640 {
17641 char *canon_name = cp_canonicalize_string (name);
17642
17643 if (canon_name != NULL)
17644 {
17645 if (strcmp (canon_name, name) != 0)
10f0c4bb 17646 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17647 xfree (canon_name);
17648 }
17649 }
17650
17651 return name;
c906108c
SS
17652}
17653
9219021c
DC
17654/* Get name of a die, return NULL if not found. */
17655
15d034d0 17656static const char *
e142c38c 17657dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17658{
17659 struct attribute *attr;
17660
e142c38c 17661 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17662 if ((!attr || !DW_STRING (attr))
17663 && die->tag != DW_TAG_class_type
17664 && die->tag != DW_TAG_interface_type
17665 && die->tag != DW_TAG_structure_type
17666 && die->tag != DW_TAG_union_type)
71c25dea
TT
17667 return NULL;
17668
17669 switch (die->tag)
17670 {
17671 case DW_TAG_compile_unit:
95554aad 17672 case DW_TAG_partial_unit:
71c25dea
TT
17673 /* Compilation units have a DW_AT_name that is a filename, not
17674 a source language identifier. */
17675 case DW_TAG_enumeration_type:
17676 case DW_TAG_enumerator:
17677 /* These tags always have simple identifiers already; no need
17678 to canonicalize them. */
17679 return DW_STRING (attr);
907af001 17680
418835cc
KS
17681 case DW_TAG_subprogram:
17682 /* Java constructors will all be named "<init>", so return
17683 the class name when we see this special case. */
17684 if (cu->language == language_java
17685 && DW_STRING (attr) != NULL
17686 && strcmp (DW_STRING (attr), "<init>") == 0)
17687 {
17688 struct dwarf2_cu *spec_cu = cu;
17689 struct die_info *spec_die;
17690
17691 /* GCJ will output '<init>' for Java constructor names.
17692 For this special case, return the name of the parent class. */
17693
17694 /* GCJ may output suprogram DIEs with AT_specification set.
17695 If so, use the name of the specified DIE. */
17696 spec_die = die_specification (die, &spec_cu);
17697 if (spec_die != NULL)
17698 return dwarf2_name (spec_die, spec_cu);
17699
17700 do
17701 {
17702 die = die->parent;
17703 if (die->tag == DW_TAG_class_type)
17704 return dwarf2_name (die, cu);
17705 }
95554aad
TT
17706 while (die->tag != DW_TAG_compile_unit
17707 && die->tag != DW_TAG_partial_unit);
418835cc 17708 }
907af001
UW
17709 break;
17710
17711 case DW_TAG_class_type:
17712 case DW_TAG_interface_type:
17713 case DW_TAG_structure_type:
17714 case DW_TAG_union_type:
17715 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17716 structures or unions. These were of the form "._%d" in GCC 4.1,
17717 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17718 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17719 if (attr && DW_STRING (attr)
17720 && (strncmp (DW_STRING (attr), "._", 2) == 0
17721 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17722 return NULL;
53832f31
TT
17723
17724 /* GCC might emit a nameless typedef that has a linkage name. See
17725 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17726 if (!attr || DW_STRING (attr) == NULL)
17727 {
df5c6c50 17728 char *demangled = NULL;
53832f31
TT
17729
17730 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17731 if (attr == NULL)
17732 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17733
17734 if (attr == NULL || DW_STRING (attr) == NULL)
17735 return NULL;
17736
df5c6c50
JK
17737 /* Avoid demangling DW_STRING (attr) the second time on a second
17738 call for the same DIE. */
17739 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17740 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17741
17742 if (demangled)
17743 {
96408a79
SA
17744 char *base;
17745
53832f31 17746 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17747 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17748 demangled, strlen (demangled));
53832f31
TT
17749 DW_STRING_IS_CANONICAL (attr) = 1;
17750 xfree (demangled);
96408a79
SA
17751
17752 /* Strip any leading namespaces/classes, keep only the base name.
17753 DW_AT_name for named DIEs does not contain the prefixes. */
17754 base = strrchr (DW_STRING (attr), ':');
17755 if (base && base > DW_STRING (attr) && base[-1] == ':')
17756 return &base[1];
17757 else
17758 return DW_STRING (attr);
53832f31
TT
17759 }
17760 }
907af001
UW
17761 break;
17762
71c25dea 17763 default:
907af001
UW
17764 break;
17765 }
17766
17767 if (!DW_STRING_IS_CANONICAL (attr))
17768 {
17769 DW_STRING (attr)
17770 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17771 &cu->objfile->objfile_obstack);
17772 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17773 }
907af001 17774 return DW_STRING (attr);
9219021c
DC
17775}
17776
17777/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17778 is none. *EXT_CU is the CU containing DIE on input, and the CU
17779 containing the return value on output. */
9219021c
DC
17780
17781static struct die_info *
f2f0e013 17782dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17783{
17784 struct attribute *attr;
9219021c 17785
f2f0e013 17786 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17787 if (attr == NULL)
17788 return NULL;
17789
f2f0e013 17790 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17791}
17792
c906108c
SS
17793/* Convert a DIE tag into its string name. */
17794
f39c6ffd 17795static const char *
aa1ee363 17796dwarf_tag_name (unsigned tag)
c906108c 17797{
f39c6ffd
TT
17798 const char *name = get_DW_TAG_name (tag);
17799
17800 if (name == NULL)
17801 return "DW_TAG_<unknown>";
17802
17803 return name;
c906108c
SS
17804}
17805
17806/* Convert a DWARF attribute code into its string name. */
17807
f39c6ffd 17808static const char *
aa1ee363 17809dwarf_attr_name (unsigned attr)
c906108c 17810{
f39c6ffd
TT
17811 const char *name;
17812
c764a876 17813#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17814 if (attr == DW_AT_MIPS_fde)
17815 return "DW_AT_MIPS_fde";
17816#else
17817 if (attr == DW_AT_HP_block_index)
17818 return "DW_AT_HP_block_index";
c764a876 17819#endif
f39c6ffd
TT
17820
17821 name = get_DW_AT_name (attr);
17822
17823 if (name == NULL)
17824 return "DW_AT_<unknown>";
17825
17826 return name;
c906108c
SS
17827}
17828
17829/* Convert a DWARF value form code into its string name. */
17830
f39c6ffd 17831static const char *
aa1ee363 17832dwarf_form_name (unsigned form)
c906108c 17833{
f39c6ffd
TT
17834 const char *name = get_DW_FORM_name (form);
17835
17836 if (name == NULL)
17837 return "DW_FORM_<unknown>";
17838
17839 return name;
c906108c
SS
17840}
17841
17842static char *
fba45db2 17843dwarf_bool_name (unsigned mybool)
c906108c
SS
17844{
17845 if (mybool)
17846 return "TRUE";
17847 else
17848 return "FALSE";
17849}
17850
17851/* Convert a DWARF type code into its string name. */
17852
f39c6ffd 17853static const char *
aa1ee363 17854dwarf_type_encoding_name (unsigned enc)
c906108c 17855{
f39c6ffd 17856 const char *name = get_DW_ATE_name (enc);
c906108c 17857
f39c6ffd
TT
17858 if (name == NULL)
17859 return "DW_ATE_<unknown>";
c906108c 17860
f39c6ffd 17861 return name;
c906108c 17862}
c906108c 17863
f9aca02d 17864static void
d97bc12b 17865dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17866{
17867 unsigned int i;
17868
d97bc12b
DE
17869 print_spaces (indent, f);
17870 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17871 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17872
17873 if (die->parent != NULL)
17874 {
17875 print_spaces (indent, f);
17876 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17877 die->parent->offset.sect_off);
d97bc12b
DE
17878 }
17879
17880 print_spaces (indent, f);
17881 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17882 dwarf_bool_name (die->child != NULL));
c906108c 17883
d97bc12b
DE
17884 print_spaces (indent, f);
17885 fprintf_unfiltered (f, " attributes:\n");
17886
c906108c
SS
17887 for (i = 0; i < die->num_attrs; ++i)
17888 {
d97bc12b
DE
17889 print_spaces (indent, f);
17890 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17891 dwarf_attr_name (die->attrs[i].name),
17892 dwarf_form_name (die->attrs[i].form));
d97bc12b 17893
c906108c
SS
17894 switch (die->attrs[i].form)
17895 {
c906108c 17896 case DW_FORM_addr:
3019eac3 17897 case DW_FORM_GNU_addr_index:
d97bc12b 17898 fprintf_unfiltered (f, "address: ");
5af949e3 17899 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17900 break;
17901 case DW_FORM_block2:
17902 case DW_FORM_block4:
17903 case DW_FORM_block:
17904 case DW_FORM_block1:
56eb65bd
SP
17905 fprintf_unfiltered (f, "block: size %s",
17906 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17907 break;
2dc7f7b3 17908 case DW_FORM_exprloc:
56eb65bd
SP
17909 fprintf_unfiltered (f, "expression: size %s",
17910 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17911 break;
4568ecf9
DE
17912 case DW_FORM_ref_addr:
17913 fprintf_unfiltered (f, "ref address: ");
17914 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17915 break;
36586728
TT
17916 case DW_FORM_GNU_ref_alt:
17917 fprintf_unfiltered (f, "alt ref address: ");
17918 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17919 break;
10b3939b
DJ
17920 case DW_FORM_ref1:
17921 case DW_FORM_ref2:
17922 case DW_FORM_ref4:
4568ecf9
DE
17923 case DW_FORM_ref8:
17924 case DW_FORM_ref_udata:
d97bc12b 17925 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17926 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17927 break;
c906108c
SS
17928 case DW_FORM_data1:
17929 case DW_FORM_data2:
17930 case DW_FORM_data4:
ce5d95e1 17931 case DW_FORM_data8:
c906108c
SS
17932 case DW_FORM_udata:
17933 case DW_FORM_sdata:
43bbcdc2
PH
17934 fprintf_unfiltered (f, "constant: %s",
17935 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17936 break;
2dc7f7b3
TT
17937 case DW_FORM_sec_offset:
17938 fprintf_unfiltered (f, "section offset: %s",
17939 pulongest (DW_UNSND (&die->attrs[i])));
17940 break;
55f1336d 17941 case DW_FORM_ref_sig8:
ac9ec31b
DE
17942 fprintf_unfiltered (f, "signature: %s",
17943 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17944 break;
c906108c 17945 case DW_FORM_string:
4bdf3d34 17946 case DW_FORM_strp:
3019eac3 17947 case DW_FORM_GNU_str_index:
36586728 17948 case DW_FORM_GNU_strp_alt:
8285870a 17949 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17950 DW_STRING (&die->attrs[i])
8285870a
JK
17951 ? DW_STRING (&die->attrs[i]) : "",
17952 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17953 break;
17954 case DW_FORM_flag:
17955 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17956 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17957 else
d97bc12b 17958 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17959 break;
2dc7f7b3
TT
17960 case DW_FORM_flag_present:
17961 fprintf_unfiltered (f, "flag: TRUE");
17962 break;
a8329558 17963 case DW_FORM_indirect:
0963b4bd
MS
17964 /* The reader will have reduced the indirect form to
17965 the "base form" so this form should not occur. */
3e43a32a
MS
17966 fprintf_unfiltered (f,
17967 "unexpected attribute form: DW_FORM_indirect");
a8329558 17968 break;
c906108c 17969 default:
d97bc12b 17970 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17971 die->attrs[i].form);
d97bc12b 17972 break;
c906108c 17973 }
d97bc12b 17974 fprintf_unfiltered (f, "\n");
c906108c
SS
17975 }
17976}
17977
f9aca02d 17978static void
d97bc12b 17979dump_die_for_error (struct die_info *die)
c906108c 17980{
d97bc12b
DE
17981 dump_die_shallow (gdb_stderr, 0, die);
17982}
17983
17984static void
17985dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17986{
17987 int indent = level * 4;
17988
17989 gdb_assert (die != NULL);
17990
17991 if (level >= max_level)
17992 return;
17993
17994 dump_die_shallow (f, indent, die);
17995
17996 if (die->child != NULL)
c906108c 17997 {
d97bc12b
DE
17998 print_spaces (indent, f);
17999 fprintf_unfiltered (f, " Children:");
18000 if (level + 1 < max_level)
18001 {
18002 fprintf_unfiltered (f, "\n");
18003 dump_die_1 (f, level + 1, max_level, die->child);
18004 }
18005 else
18006 {
3e43a32a
MS
18007 fprintf_unfiltered (f,
18008 " [not printed, max nesting level reached]\n");
d97bc12b
DE
18009 }
18010 }
18011
18012 if (die->sibling != NULL && level > 0)
18013 {
18014 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
18015 }
18016}
18017
d97bc12b
DE
18018/* This is called from the pdie macro in gdbinit.in.
18019 It's not static so gcc will keep a copy callable from gdb. */
18020
18021void
18022dump_die (struct die_info *die, int max_level)
18023{
18024 dump_die_1 (gdb_stdlog, 0, max_level, die);
18025}
18026
f9aca02d 18027static void
51545339 18028store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18029{
51545339 18030 void **slot;
c906108c 18031
b64f50a1
JK
18032 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
18033 INSERT);
51545339
DJ
18034
18035 *slot = die;
c906108c
SS
18036}
18037
b64f50a1
JK
18038/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
18039 required kind. */
18040
18041static sect_offset
ff39bb5e 18042dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 18043{
4568ecf9 18044 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 18045
7771576e 18046 if (attr_form_is_ref (attr))
b64f50a1 18047 return retval;
93311388 18048
b64f50a1 18049 retval.sect_off = 0;
93311388
DE
18050 complaint (&symfile_complaints,
18051 _("unsupported die ref attribute form: '%s'"),
18052 dwarf_form_name (attr->form));
b64f50a1 18053 return retval;
c906108c
SS
18054}
18055
43bbcdc2
PH
18056/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
18057 * the value held by the attribute is not constant. */
a02abb62 18058
43bbcdc2 18059static LONGEST
ff39bb5e 18060dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
18061{
18062 if (attr->form == DW_FORM_sdata)
18063 return DW_SND (attr);
18064 else if (attr->form == DW_FORM_udata
18065 || attr->form == DW_FORM_data1
18066 || attr->form == DW_FORM_data2
18067 || attr->form == DW_FORM_data4
18068 || attr->form == DW_FORM_data8)
18069 return DW_UNSND (attr);
18070 else
18071 {
3e43a32a
MS
18072 complaint (&symfile_complaints,
18073 _("Attribute value is not a constant (%s)"),
a02abb62
JB
18074 dwarf_form_name (attr->form));
18075 return default_value;
18076 }
18077}
18078
348e048f
DE
18079/* Follow reference or signature attribute ATTR of SRC_DIE.
18080 On entry *REF_CU is the CU of SRC_DIE.
18081 On exit *REF_CU is the CU of the result. */
18082
18083static struct die_info *
ff39bb5e 18084follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
18085 struct dwarf2_cu **ref_cu)
18086{
18087 struct die_info *die;
18088
7771576e 18089 if (attr_form_is_ref (attr))
348e048f 18090 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 18091 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
18092 die = follow_die_sig (src_die, attr, ref_cu);
18093 else
18094 {
18095 dump_die_for_error (src_die);
18096 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 18097 objfile_name ((*ref_cu)->objfile));
348e048f
DE
18098 }
18099
18100 return die;
03dd20cc
DJ
18101}
18102
5c631832 18103/* Follow reference OFFSET.
673bfd45
DE
18104 On entry *REF_CU is the CU of the source die referencing OFFSET.
18105 On exit *REF_CU is the CU of the result.
18106 Returns NULL if OFFSET is invalid. */
f504f079 18107
f9aca02d 18108static struct die_info *
36586728
TT
18109follow_die_offset (sect_offset offset, int offset_in_dwz,
18110 struct dwarf2_cu **ref_cu)
c906108c 18111{
10b3939b 18112 struct die_info temp_die;
f2f0e013 18113 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 18114
348e048f
DE
18115 gdb_assert (cu->per_cu != NULL);
18116
98bfdba5
PA
18117 target_cu = cu;
18118
3019eac3 18119 if (cu->per_cu->is_debug_types)
348e048f
DE
18120 {
18121 /* .debug_types CUs cannot reference anything outside their CU.
18122 If they need to, they have to reference a signatured type via
55f1336d 18123 DW_FORM_ref_sig8. */
348e048f 18124 if (! offset_in_cu_p (&cu->header, offset))
5c631832 18125 return NULL;
348e048f 18126 }
36586728
TT
18127 else if (offset_in_dwz != cu->per_cu->is_dwz
18128 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18129 {
18130 struct dwarf2_per_cu_data *per_cu;
9a619af0 18131
36586728
TT
18132 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18133 cu->objfile);
03dd20cc
DJ
18134
18135 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18136 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18137 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18138
10b3939b
DJ
18139 target_cu = per_cu->cu;
18140 }
98bfdba5
PA
18141 else if (cu->dies == NULL)
18142 {
18143 /* We're loading full DIEs during partial symbol reading. */
18144 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18145 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18146 }
c906108c 18147
f2f0e013 18148 *ref_cu = target_cu;
51545339 18149 temp_die.offset = offset;
b64f50a1 18150 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18151}
10b3939b 18152
5c631832
JK
18153/* Follow reference attribute ATTR of SRC_DIE.
18154 On entry *REF_CU is the CU of SRC_DIE.
18155 On exit *REF_CU is the CU of the result. */
18156
18157static struct die_info *
ff39bb5e 18158follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
18159 struct dwarf2_cu **ref_cu)
18160{
b64f50a1 18161 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18162 struct dwarf2_cu *cu = *ref_cu;
18163 struct die_info *die;
18164
36586728
TT
18165 die = follow_die_offset (offset,
18166 (attr->form == DW_FORM_GNU_ref_alt
18167 || cu->per_cu->is_dwz),
18168 ref_cu);
5c631832
JK
18169 if (!die)
18170 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18171 "at 0x%x [in module %s]"),
4262abfb
JK
18172 offset.sect_off, src_die->offset.sect_off,
18173 objfile_name (cu->objfile));
348e048f 18174
5c631832
JK
18175 return die;
18176}
18177
d83e736b
JK
18178/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18179 Returned value is intended for DW_OP_call*. Returned
18180 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18181
18182struct dwarf2_locexpr_baton
8b9737bf
TT
18183dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18184 struct dwarf2_per_cu_data *per_cu,
18185 CORE_ADDR (*get_frame_pc) (void *baton),
18186 void *baton)
5c631832 18187{
918dd910 18188 struct dwarf2_cu *cu;
5c631832
JK
18189 struct die_info *die;
18190 struct attribute *attr;
18191 struct dwarf2_locexpr_baton retval;
18192
8cf6f0b1
TT
18193 dw2_setup (per_cu->objfile);
18194
918dd910
JK
18195 if (per_cu->cu == NULL)
18196 load_cu (per_cu);
18197 cu = per_cu->cu;
18198
36586728 18199 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18200 if (!die)
18201 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 18202 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
18203
18204 attr = dwarf2_attr (die, DW_AT_location, cu);
18205 if (!attr)
18206 {
e103e986
JK
18207 /* DWARF: "If there is no such attribute, then there is no effect.".
18208 DATA is ignored if SIZE is 0. */
5c631832 18209
e103e986 18210 retval.data = NULL;
5c631832
JK
18211 retval.size = 0;
18212 }
8cf6f0b1
TT
18213 else if (attr_form_is_section_offset (attr))
18214 {
18215 struct dwarf2_loclist_baton loclist_baton;
18216 CORE_ADDR pc = (*get_frame_pc) (baton);
18217 size_t size;
18218
18219 fill_in_loclist_baton (cu, &loclist_baton, attr);
18220
18221 retval.data = dwarf2_find_location_expression (&loclist_baton,
18222 &size, pc);
18223 retval.size = size;
18224 }
5c631832
JK
18225 else
18226 {
18227 if (!attr_form_is_block (attr))
18228 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18229 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
4262abfb 18230 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
18231
18232 retval.data = DW_BLOCK (attr)->data;
18233 retval.size = DW_BLOCK (attr)->size;
18234 }
18235 retval.per_cu = cu->per_cu;
918dd910 18236
918dd910
JK
18237 age_cached_comp_units ();
18238
5c631832 18239 return retval;
348e048f
DE
18240}
18241
8b9737bf
TT
18242/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18243 offset. */
18244
18245struct dwarf2_locexpr_baton
18246dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18247 struct dwarf2_per_cu_data *per_cu,
18248 CORE_ADDR (*get_frame_pc) (void *baton),
18249 void *baton)
18250{
18251 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18252
18253 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18254}
18255
b6807d98
TT
18256/* Write a constant of a given type as target-ordered bytes into
18257 OBSTACK. */
18258
18259static const gdb_byte *
18260write_constant_as_bytes (struct obstack *obstack,
18261 enum bfd_endian byte_order,
18262 struct type *type,
18263 ULONGEST value,
18264 LONGEST *len)
18265{
18266 gdb_byte *result;
18267
18268 *len = TYPE_LENGTH (type);
18269 result = obstack_alloc (obstack, *len);
18270 store_unsigned_integer (result, *len, byte_order, value);
18271
18272 return result;
18273}
18274
18275/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18276 pointer to the constant bytes and set LEN to the length of the
18277 data. If memory is needed, allocate it on OBSTACK. If the DIE
18278 does not have a DW_AT_const_value, return NULL. */
18279
18280const gdb_byte *
18281dwarf2_fetch_constant_bytes (sect_offset offset,
18282 struct dwarf2_per_cu_data *per_cu,
18283 struct obstack *obstack,
18284 LONGEST *len)
18285{
18286 struct dwarf2_cu *cu;
18287 struct die_info *die;
18288 struct attribute *attr;
18289 const gdb_byte *result = NULL;
18290 struct type *type;
18291 LONGEST value;
18292 enum bfd_endian byte_order;
18293
18294 dw2_setup (per_cu->objfile);
18295
18296 if (per_cu->cu == NULL)
18297 load_cu (per_cu);
18298 cu = per_cu->cu;
18299
18300 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18301 if (!die)
18302 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 18303 offset.sect_off, objfile_name (per_cu->objfile));
b6807d98
TT
18304
18305
18306 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18307 if (attr == NULL)
18308 return NULL;
18309
18310 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18311 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18312
18313 switch (attr->form)
18314 {
18315 case DW_FORM_addr:
18316 case DW_FORM_GNU_addr_index:
18317 {
18318 gdb_byte *tem;
18319
18320 *len = cu->header.addr_size;
18321 tem = obstack_alloc (obstack, *len);
18322 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18323 result = tem;
18324 }
18325 break;
18326 case DW_FORM_string:
18327 case DW_FORM_strp:
18328 case DW_FORM_GNU_str_index:
18329 case DW_FORM_GNU_strp_alt:
18330 /* DW_STRING is already allocated on the objfile obstack, point
18331 directly to it. */
18332 result = (const gdb_byte *) DW_STRING (attr);
18333 *len = strlen (DW_STRING (attr));
18334 break;
18335 case DW_FORM_block1:
18336 case DW_FORM_block2:
18337 case DW_FORM_block4:
18338 case DW_FORM_block:
18339 case DW_FORM_exprloc:
18340 result = DW_BLOCK (attr)->data;
18341 *len = DW_BLOCK (attr)->size;
18342 break;
18343
18344 /* The DW_AT_const_value attributes are supposed to carry the
18345 symbol's value "represented as it would be on the target
18346 architecture." By the time we get here, it's already been
18347 converted to host endianness, so we just need to sign- or
18348 zero-extend it as appropriate. */
18349 case DW_FORM_data1:
18350 type = die_type (die, cu);
18351 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18352 if (result == NULL)
18353 result = write_constant_as_bytes (obstack, byte_order,
18354 type, value, len);
18355 break;
18356 case DW_FORM_data2:
18357 type = die_type (die, cu);
18358 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18359 if (result == NULL)
18360 result = write_constant_as_bytes (obstack, byte_order,
18361 type, value, len);
18362 break;
18363 case DW_FORM_data4:
18364 type = die_type (die, cu);
18365 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18366 if (result == NULL)
18367 result = write_constant_as_bytes (obstack, byte_order,
18368 type, value, len);
18369 break;
18370 case DW_FORM_data8:
18371 type = die_type (die, cu);
18372 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18373 if (result == NULL)
18374 result = write_constant_as_bytes (obstack, byte_order,
18375 type, value, len);
18376 break;
18377
18378 case DW_FORM_sdata:
18379 type = die_type (die, cu);
18380 result = write_constant_as_bytes (obstack, byte_order,
18381 type, DW_SND (attr), len);
18382 break;
18383
18384 case DW_FORM_udata:
18385 type = die_type (die, cu);
18386 result = write_constant_as_bytes (obstack, byte_order,
18387 type, DW_UNSND (attr), len);
18388 break;
18389
18390 default:
18391 complaint (&symfile_complaints,
18392 _("unsupported const value attribute form: '%s'"),
18393 dwarf_form_name (attr->form));
18394 break;
18395 }
18396
18397 return result;
18398}
18399
8a9b8146
TT
18400/* Return the type of the DIE at DIE_OFFSET in the CU named by
18401 PER_CU. */
18402
18403struct type *
b64f50a1 18404dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
18405 struct dwarf2_per_cu_data *per_cu)
18406{
b64f50a1
JK
18407 sect_offset die_offset_sect;
18408
8a9b8146 18409 dw2_setup (per_cu->objfile);
b64f50a1
JK
18410
18411 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18412 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
18413}
18414
ac9ec31b 18415/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 18416 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
18417 On exit *REF_CU is the CU of the result.
18418 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
18419
18420static struct die_info *
ac9ec31b
DE
18421follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18422 struct dwarf2_cu **ref_cu)
348e048f
DE
18423{
18424 struct objfile *objfile = (*ref_cu)->objfile;
18425 struct die_info temp_die;
348e048f
DE
18426 struct dwarf2_cu *sig_cu;
18427 struct die_info *die;
18428
ac9ec31b
DE
18429 /* While it might be nice to assert sig_type->type == NULL here,
18430 we can get here for DW_AT_imported_declaration where we need
18431 the DIE not the type. */
348e048f
DE
18432
18433 /* If necessary, add it to the queue and load its DIEs. */
18434
95554aad 18435 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 18436 read_signatured_type (sig_type);
348e048f 18437
348e048f 18438 sig_cu = sig_type->per_cu.cu;
69d751e3 18439 gdb_assert (sig_cu != NULL);
3019eac3
DE
18440 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18441 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
18442 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18443 temp_die.offset.sect_off);
348e048f
DE
18444 if (die)
18445 {
796a7ff8
DE
18446 /* For .gdb_index version 7 keep track of included TUs.
18447 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18448 if (dwarf2_per_objfile->index_table != NULL
18449 && dwarf2_per_objfile->index_table->version <= 7)
18450 {
18451 VEC_safe_push (dwarf2_per_cu_ptr,
18452 (*ref_cu)->per_cu->imported_symtabs,
18453 sig_cu->per_cu);
18454 }
18455
348e048f
DE
18456 *ref_cu = sig_cu;
18457 return die;
18458 }
18459
ac9ec31b
DE
18460 return NULL;
18461}
18462
18463/* Follow signatured type referenced by ATTR in SRC_DIE.
18464 On entry *REF_CU is the CU of SRC_DIE.
18465 On exit *REF_CU is the CU of the result.
18466 The result is the DIE of the type.
18467 If the referenced type cannot be found an error is thrown. */
18468
18469static struct die_info *
ff39bb5e 18470follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
18471 struct dwarf2_cu **ref_cu)
18472{
18473 ULONGEST signature = DW_SIGNATURE (attr);
18474 struct signatured_type *sig_type;
18475 struct die_info *die;
18476
18477 gdb_assert (attr->form == DW_FORM_ref_sig8);
18478
a2ce51a0 18479 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
18480 /* sig_type will be NULL if the signatured type is missing from
18481 the debug info. */
18482 if (sig_type == NULL)
18483 {
18484 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18485 " from DIE at 0x%x [in module %s]"),
18486 hex_string (signature), src_die->offset.sect_off,
4262abfb 18487 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
18488 }
18489
18490 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18491 if (die == NULL)
18492 {
18493 dump_die_for_error (src_die);
18494 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18495 " from DIE at 0x%x [in module %s]"),
18496 hex_string (signature), src_die->offset.sect_off,
4262abfb 18497 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
18498 }
18499
18500 return die;
18501}
18502
18503/* Get the type specified by SIGNATURE referenced in DIE/CU,
18504 reading in and processing the type unit if necessary. */
18505
18506static struct type *
18507get_signatured_type (struct die_info *die, ULONGEST signature,
18508 struct dwarf2_cu *cu)
18509{
18510 struct signatured_type *sig_type;
18511 struct dwarf2_cu *type_cu;
18512 struct die_info *type_die;
18513 struct type *type;
18514
a2ce51a0 18515 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
18516 /* sig_type will be NULL if the signatured type is missing from
18517 the debug info. */
18518 if (sig_type == NULL)
18519 {
18520 complaint (&symfile_complaints,
18521 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18522 " from DIE at 0x%x [in module %s]"),
18523 hex_string (signature), die->offset.sect_off,
4262abfb 18524 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
18525 return build_error_marker_type (cu, die);
18526 }
18527
18528 /* If we already know the type we're done. */
18529 if (sig_type->type != NULL)
18530 return sig_type->type;
18531
18532 type_cu = cu;
18533 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18534 if (type_die != NULL)
18535 {
18536 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18537 is created. This is important, for example, because for c++ classes
18538 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18539 type = read_type_die (type_die, type_cu);
18540 if (type == NULL)
18541 {
18542 complaint (&symfile_complaints,
18543 _("Dwarf Error: Cannot build signatured type %s"
18544 " referenced from DIE at 0x%x [in module %s]"),
18545 hex_string (signature), die->offset.sect_off,
4262abfb 18546 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
18547 type = build_error_marker_type (cu, die);
18548 }
18549 }
18550 else
18551 {
18552 complaint (&symfile_complaints,
18553 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18554 " from DIE at 0x%x [in module %s]"),
18555 hex_string (signature), die->offset.sect_off,
4262abfb 18556 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
18557 type = build_error_marker_type (cu, die);
18558 }
18559 sig_type->type = type;
18560
18561 return type;
18562}
18563
18564/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18565 reading in and processing the type unit if necessary. */
18566
18567static struct type *
ff39bb5e 18568get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 18569 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18570{
18571 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 18572 if (attr_form_is_ref (attr))
ac9ec31b
DE
18573 {
18574 struct dwarf2_cu *type_cu = cu;
18575 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18576
18577 return read_type_die (type_die, type_cu);
18578 }
18579 else if (attr->form == DW_FORM_ref_sig8)
18580 {
18581 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18582 }
18583 else
18584 {
18585 complaint (&symfile_complaints,
18586 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18587 " at 0x%x [in module %s]"),
18588 dwarf_form_name (attr->form), die->offset.sect_off,
4262abfb 18589 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
18590 return build_error_marker_type (cu, die);
18591 }
348e048f
DE
18592}
18593
e5fe5e75 18594/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18595
18596static void
e5fe5e75 18597load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18598{
52dc124a 18599 struct signatured_type *sig_type;
348e048f 18600
f4dc4d17
DE
18601 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18602 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18603
6721b2ec
DE
18604 /* We have the per_cu, but we need the signatured_type.
18605 Fortunately this is an easy translation. */
18606 gdb_assert (per_cu->is_debug_types);
18607 sig_type = (struct signatured_type *) per_cu;
348e048f 18608
6721b2ec 18609 gdb_assert (per_cu->cu == NULL);
348e048f 18610
52dc124a 18611 read_signatured_type (sig_type);
348e048f 18612
6721b2ec 18613 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18614}
18615
dee91e82
DE
18616/* die_reader_func for read_signatured_type.
18617 This is identical to load_full_comp_unit_reader,
18618 but is kept separate for now. */
348e048f
DE
18619
18620static void
dee91e82 18621read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18622 const gdb_byte *info_ptr,
dee91e82
DE
18623 struct die_info *comp_unit_die,
18624 int has_children,
18625 void *data)
348e048f 18626{
dee91e82 18627 struct dwarf2_cu *cu = reader->cu;
348e048f 18628
dee91e82
DE
18629 gdb_assert (cu->die_hash == NULL);
18630 cu->die_hash =
18631 htab_create_alloc_ex (cu->header.length / 12,
18632 die_hash,
18633 die_eq,
18634 NULL,
18635 &cu->comp_unit_obstack,
18636 hashtab_obstack_allocate,
18637 dummy_obstack_deallocate);
348e048f 18638
dee91e82
DE
18639 if (has_children)
18640 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18641 &info_ptr, comp_unit_die);
18642 cu->dies = comp_unit_die;
18643 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18644
18645 /* We try not to read any attributes in this function, because not
9cdd5dbd 18646 all CUs needed for references have been loaded yet, and symbol
348e048f 18647 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18648 or we won't be able to build types correctly.
18649 Similarly, if we do not read the producer, we can not apply
18650 producer-specific interpretation. */
95554aad 18651 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18652}
348e048f 18653
3019eac3
DE
18654/* Read in a signatured type and build its CU and DIEs.
18655 If the type is a stub for the real type in a DWO file,
18656 read in the real type from the DWO file as well. */
dee91e82
DE
18657
18658static void
18659read_signatured_type (struct signatured_type *sig_type)
18660{
18661 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18662
3019eac3 18663 gdb_assert (per_cu->is_debug_types);
dee91e82 18664 gdb_assert (per_cu->cu == NULL);
348e048f 18665
f4dc4d17
DE
18666 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18667 read_signatured_type_reader, NULL);
7ee85ab1 18668 sig_type->per_cu.tu_read = 1;
c906108c
SS
18669}
18670
c906108c
SS
18671/* Decode simple location descriptions.
18672 Given a pointer to a dwarf block that defines a location, compute
18673 the location and return the value.
18674
4cecd739
DJ
18675 NOTE drow/2003-11-18: This function is called in two situations
18676 now: for the address of static or global variables (partial symbols
18677 only) and for offsets into structures which are expected to be
18678 (more or less) constant. The partial symbol case should go away,
18679 and only the constant case should remain. That will let this
18680 function complain more accurately. A few special modes are allowed
18681 without complaint for global variables (for instance, global
18682 register values and thread-local values).
c906108c
SS
18683
18684 A location description containing no operations indicates that the
4cecd739 18685 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18686 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18687 callers will only want a very basic result and this can become a
21ae7a4d
JK
18688 complaint.
18689
18690 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18691
18692static CORE_ADDR
e7c27a73 18693decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18694{
e7c27a73 18695 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18696 size_t i;
18697 size_t size = blk->size;
d521ce57 18698 const gdb_byte *data = blk->data;
21ae7a4d
JK
18699 CORE_ADDR stack[64];
18700 int stacki;
18701 unsigned int bytes_read, unsnd;
18702 gdb_byte op;
c906108c 18703
21ae7a4d
JK
18704 i = 0;
18705 stacki = 0;
18706 stack[stacki] = 0;
18707 stack[++stacki] = 0;
18708
18709 while (i < size)
18710 {
18711 op = data[i++];
18712 switch (op)
18713 {
18714 case DW_OP_lit0:
18715 case DW_OP_lit1:
18716 case DW_OP_lit2:
18717 case DW_OP_lit3:
18718 case DW_OP_lit4:
18719 case DW_OP_lit5:
18720 case DW_OP_lit6:
18721 case DW_OP_lit7:
18722 case DW_OP_lit8:
18723 case DW_OP_lit9:
18724 case DW_OP_lit10:
18725 case DW_OP_lit11:
18726 case DW_OP_lit12:
18727 case DW_OP_lit13:
18728 case DW_OP_lit14:
18729 case DW_OP_lit15:
18730 case DW_OP_lit16:
18731 case DW_OP_lit17:
18732 case DW_OP_lit18:
18733 case DW_OP_lit19:
18734 case DW_OP_lit20:
18735 case DW_OP_lit21:
18736 case DW_OP_lit22:
18737 case DW_OP_lit23:
18738 case DW_OP_lit24:
18739 case DW_OP_lit25:
18740 case DW_OP_lit26:
18741 case DW_OP_lit27:
18742 case DW_OP_lit28:
18743 case DW_OP_lit29:
18744 case DW_OP_lit30:
18745 case DW_OP_lit31:
18746 stack[++stacki] = op - DW_OP_lit0;
18747 break;
f1bea926 18748
21ae7a4d
JK
18749 case DW_OP_reg0:
18750 case DW_OP_reg1:
18751 case DW_OP_reg2:
18752 case DW_OP_reg3:
18753 case DW_OP_reg4:
18754 case DW_OP_reg5:
18755 case DW_OP_reg6:
18756 case DW_OP_reg7:
18757 case DW_OP_reg8:
18758 case DW_OP_reg9:
18759 case DW_OP_reg10:
18760 case DW_OP_reg11:
18761 case DW_OP_reg12:
18762 case DW_OP_reg13:
18763 case DW_OP_reg14:
18764 case DW_OP_reg15:
18765 case DW_OP_reg16:
18766 case DW_OP_reg17:
18767 case DW_OP_reg18:
18768 case DW_OP_reg19:
18769 case DW_OP_reg20:
18770 case DW_OP_reg21:
18771 case DW_OP_reg22:
18772 case DW_OP_reg23:
18773 case DW_OP_reg24:
18774 case DW_OP_reg25:
18775 case DW_OP_reg26:
18776 case DW_OP_reg27:
18777 case DW_OP_reg28:
18778 case DW_OP_reg29:
18779 case DW_OP_reg30:
18780 case DW_OP_reg31:
18781 stack[++stacki] = op - DW_OP_reg0;
18782 if (i < size)
18783 dwarf2_complex_location_expr_complaint ();
18784 break;
c906108c 18785
21ae7a4d
JK
18786 case DW_OP_regx:
18787 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18788 i += bytes_read;
18789 stack[++stacki] = unsnd;
18790 if (i < size)
18791 dwarf2_complex_location_expr_complaint ();
18792 break;
c906108c 18793
21ae7a4d
JK
18794 case DW_OP_addr:
18795 stack[++stacki] = read_address (objfile->obfd, &data[i],
18796 cu, &bytes_read);
18797 i += bytes_read;
18798 break;
d53d4ac5 18799
21ae7a4d
JK
18800 case DW_OP_const1u:
18801 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18802 i += 1;
18803 break;
18804
18805 case DW_OP_const1s:
18806 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18807 i += 1;
18808 break;
18809
18810 case DW_OP_const2u:
18811 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18812 i += 2;
18813 break;
18814
18815 case DW_OP_const2s:
18816 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18817 i += 2;
18818 break;
d53d4ac5 18819
21ae7a4d
JK
18820 case DW_OP_const4u:
18821 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18822 i += 4;
18823 break;
18824
18825 case DW_OP_const4s:
18826 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18827 i += 4;
18828 break;
18829
585861ea
JK
18830 case DW_OP_const8u:
18831 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18832 i += 8;
18833 break;
18834
21ae7a4d
JK
18835 case DW_OP_constu:
18836 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18837 &bytes_read);
18838 i += bytes_read;
18839 break;
18840
18841 case DW_OP_consts:
18842 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18843 i += bytes_read;
18844 break;
18845
18846 case DW_OP_dup:
18847 stack[stacki + 1] = stack[stacki];
18848 stacki++;
18849 break;
18850
18851 case DW_OP_plus:
18852 stack[stacki - 1] += stack[stacki];
18853 stacki--;
18854 break;
18855
18856 case DW_OP_plus_uconst:
18857 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18858 &bytes_read);
18859 i += bytes_read;
18860 break;
18861
18862 case DW_OP_minus:
18863 stack[stacki - 1] -= stack[stacki];
18864 stacki--;
18865 break;
18866
18867 case DW_OP_deref:
18868 /* If we're not the last op, then we definitely can't encode
18869 this using GDB's address_class enum. This is valid for partial
18870 global symbols, although the variable's address will be bogus
18871 in the psymtab. */
18872 if (i < size)
18873 dwarf2_complex_location_expr_complaint ();
18874 break;
18875
18876 case DW_OP_GNU_push_tls_address:
18877 /* The top of the stack has the offset from the beginning
18878 of the thread control block at which the variable is located. */
18879 /* Nothing should follow this operator, so the top of stack would
18880 be returned. */
18881 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18882 address will be bogus in the psymtab. Make it always at least
18883 non-zero to not look as a variable garbage collected by linker
18884 which have DW_OP_addr 0. */
21ae7a4d
JK
18885 if (i < size)
18886 dwarf2_complex_location_expr_complaint ();
585861ea 18887 stack[stacki]++;
21ae7a4d
JK
18888 break;
18889
18890 case DW_OP_GNU_uninit:
18891 break;
18892
3019eac3 18893 case DW_OP_GNU_addr_index:
49f6c839 18894 case DW_OP_GNU_const_index:
3019eac3
DE
18895 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18896 &bytes_read);
18897 i += bytes_read;
18898 break;
18899
21ae7a4d
JK
18900 default:
18901 {
f39c6ffd 18902 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18903
18904 if (name)
18905 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18906 name);
18907 else
18908 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18909 op);
18910 }
18911
18912 return (stack[stacki]);
d53d4ac5 18913 }
3c6e0cb3 18914
21ae7a4d
JK
18915 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18916 outside of the allocated space. Also enforce minimum>0. */
18917 if (stacki >= ARRAY_SIZE (stack) - 1)
18918 {
18919 complaint (&symfile_complaints,
18920 _("location description stack overflow"));
18921 return 0;
18922 }
18923
18924 if (stacki <= 0)
18925 {
18926 complaint (&symfile_complaints,
18927 _("location description stack underflow"));
18928 return 0;
18929 }
18930 }
18931 return (stack[stacki]);
c906108c
SS
18932}
18933
18934/* memory allocation interface */
18935
c906108c 18936static struct dwarf_block *
7b5a2f43 18937dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18938{
18939 struct dwarf_block *blk;
18940
18941 blk = (struct dwarf_block *)
7b5a2f43 18942 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18943 return (blk);
18944}
18945
c906108c 18946static struct die_info *
b60c80d6 18947dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18948{
18949 struct die_info *die;
b60c80d6
DJ
18950 size_t size = sizeof (struct die_info);
18951
18952 if (num_attrs > 1)
18953 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18954
b60c80d6 18955 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18956 memset (die, 0, sizeof (struct die_info));
18957 return (die);
18958}
2e276125
JB
18959
18960\f
18961/* Macro support. */
18962
233d95b5
JK
18963/* Return file name relative to the compilation directory of file number I in
18964 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18965 responsible for freeing it. */
233d95b5 18966
2e276125 18967static char *
233d95b5 18968file_file_name (int file, struct line_header *lh)
2e276125 18969{
6a83a1e6
EZ
18970 /* Is the file number a valid index into the line header's file name
18971 table? Remember that file numbers start with one, not zero. */
18972 if (1 <= file && file <= lh->num_file_names)
18973 {
18974 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18975
233d95b5 18976 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18977 return xstrdup (fe->name);
233d95b5
JK
18978 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18979 fe->name, NULL);
6a83a1e6 18980 }
2e276125
JB
18981 else
18982 {
6a83a1e6
EZ
18983 /* The compiler produced a bogus file number. We can at least
18984 record the macro definitions made in the file, even if we
18985 won't be able to find the file by name. */
18986 char fake_name[80];
9a619af0 18987
8c042590
PM
18988 xsnprintf (fake_name, sizeof (fake_name),
18989 "<bad macro file number %d>", file);
2e276125 18990
6e70227d 18991 complaint (&symfile_complaints,
6a83a1e6
EZ
18992 _("bad file number in macro information (%d)"),
18993 file);
2e276125 18994
6a83a1e6 18995 return xstrdup (fake_name);
2e276125
JB
18996 }
18997}
18998
233d95b5
JK
18999/* Return the full name of file number I in *LH's file name table.
19000 Use COMP_DIR as the name of the current directory of the
19001 compilation. The result is allocated using xmalloc; the caller is
19002 responsible for freeing it. */
19003static char *
19004file_full_name (int file, struct line_header *lh, const char *comp_dir)
19005{
19006 /* Is the file number a valid index into the line header's file name
19007 table? Remember that file numbers start with one, not zero. */
19008 if (1 <= file && file <= lh->num_file_names)
19009 {
19010 char *relative = file_file_name (file, lh);
19011
19012 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
19013 return relative;
19014 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
19015 }
19016 else
19017 return file_file_name (file, lh);
19018}
19019
2e276125
JB
19020
19021static struct macro_source_file *
19022macro_start_file (int file, int line,
19023 struct macro_source_file *current_file,
19024 const char *comp_dir,
19025 struct line_header *lh, struct objfile *objfile)
19026{
233d95b5
JK
19027 /* File name relative to the compilation directory of this source file. */
19028 char *file_name = file_file_name (file, lh);
2e276125 19029
2e276125 19030 if (! current_file)
abc9d0dc 19031 {
fc474241
DE
19032 /* Note: We don't create a macro table for this compilation unit
19033 at all until we actually get a filename. */
19034 struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
19035
abc9d0dc
TT
19036 /* If we have no current file, then this must be the start_file
19037 directive for the compilation unit's main source file. */
fc474241
DE
19038 current_file = macro_set_main (macro_table, file_name);
19039 macro_define_special (macro_table);
abc9d0dc 19040 }
2e276125 19041 else
233d95b5 19042 current_file = macro_include (current_file, line, file_name);
2e276125 19043
233d95b5 19044 xfree (file_name);
6e70227d 19045
2e276125
JB
19046 return current_file;
19047}
19048
19049
19050/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
19051 followed by a null byte. */
19052static char *
19053copy_string (const char *buf, int len)
19054{
19055 char *s = xmalloc (len + 1);
9a619af0 19056
2e276125
JB
19057 memcpy (s, buf, len);
19058 s[len] = '\0';
2e276125
JB
19059 return s;
19060}
19061
19062
19063static const char *
19064consume_improper_spaces (const char *p, const char *body)
19065{
19066 if (*p == ' ')
19067 {
4d3c2250 19068 complaint (&symfile_complaints,
3e43a32a
MS
19069 _("macro definition contains spaces "
19070 "in formal argument list:\n`%s'"),
4d3c2250 19071 body);
2e276125
JB
19072
19073 while (*p == ' ')
19074 p++;
19075 }
19076
19077 return p;
19078}
19079
19080
19081static void
19082parse_macro_definition (struct macro_source_file *file, int line,
19083 const char *body)
19084{
19085 const char *p;
19086
19087 /* The body string takes one of two forms. For object-like macro
19088 definitions, it should be:
19089
19090 <macro name> " " <definition>
19091
19092 For function-like macro definitions, it should be:
19093
19094 <macro name> "() " <definition>
19095 or
19096 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19097
19098 Spaces may appear only where explicitly indicated, and in the
19099 <definition>.
19100
19101 The Dwarf 2 spec says that an object-like macro's name is always
19102 followed by a space, but versions of GCC around March 2002 omit
6e70227d 19103 the space when the macro's definition is the empty string.
2e276125
JB
19104
19105 The Dwarf 2 spec says that there should be no spaces between the
19106 formal arguments in a function-like macro's formal argument list,
19107 but versions of GCC around March 2002 include spaces after the
19108 commas. */
19109
19110
19111 /* Find the extent of the macro name. The macro name is terminated
19112 by either a space or null character (for an object-like macro) or
19113 an opening paren (for a function-like macro). */
19114 for (p = body; *p; p++)
19115 if (*p == ' ' || *p == '(')
19116 break;
19117
19118 if (*p == ' ' || *p == '\0')
19119 {
19120 /* It's an object-like macro. */
19121 int name_len = p - body;
19122 char *name = copy_string (body, name_len);
19123 const char *replacement;
19124
19125 if (*p == ' ')
19126 replacement = body + name_len + 1;
19127 else
19128 {
4d3c2250 19129 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19130 replacement = body + name_len;
19131 }
6e70227d 19132
2e276125
JB
19133 macro_define_object (file, line, name, replacement);
19134
19135 xfree (name);
19136 }
19137 else if (*p == '(')
19138 {
19139 /* It's a function-like macro. */
19140 char *name = copy_string (body, p - body);
19141 int argc = 0;
19142 int argv_size = 1;
19143 char **argv = xmalloc (argv_size * sizeof (*argv));
19144
19145 p++;
19146
19147 p = consume_improper_spaces (p, body);
19148
19149 /* Parse the formal argument list. */
19150 while (*p && *p != ')')
19151 {
19152 /* Find the extent of the current argument name. */
19153 const char *arg_start = p;
19154
19155 while (*p && *p != ',' && *p != ')' && *p != ' ')
19156 p++;
19157
19158 if (! *p || p == arg_start)
4d3c2250 19159 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19160 else
19161 {
19162 /* Make sure argv has room for the new argument. */
19163 if (argc >= argv_size)
19164 {
19165 argv_size *= 2;
19166 argv = xrealloc (argv, argv_size * sizeof (*argv));
19167 }
19168
19169 argv[argc++] = copy_string (arg_start, p - arg_start);
19170 }
19171
19172 p = consume_improper_spaces (p, body);
19173
19174 /* Consume the comma, if present. */
19175 if (*p == ',')
19176 {
19177 p++;
19178
19179 p = consume_improper_spaces (p, body);
19180 }
19181 }
19182
19183 if (*p == ')')
19184 {
19185 p++;
19186
19187 if (*p == ' ')
19188 /* Perfectly formed definition, no complaints. */
19189 macro_define_function (file, line, name,
6e70227d 19190 argc, (const char **) argv,
2e276125
JB
19191 p + 1);
19192 else if (*p == '\0')
19193 {
19194 /* Complain, but do define it. */
4d3c2250 19195 dwarf2_macro_malformed_definition_complaint (body);
2e276125 19196 macro_define_function (file, line, name,
6e70227d 19197 argc, (const char **) argv,
2e276125
JB
19198 p);
19199 }
19200 else
19201 /* Just complain. */
4d3c2250 19202 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19203 }
19204 else
19205 /* Just complain. */
4d3c2250 19206 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19207
19208 xfree (name);
19209 {
19210 int i;
19211
19212 for (i = 0; i < argc; i++)
19213 xfree (argv[i]);
19214 }
19215 xfree (argv);
19216 }
19217 else
4d3c2250 19218 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19219}
19220
cf2c3c16
TT
19221/* Skip some bytes from BYTES according to the form given in FORM.
19222 Returns the new pointer. */
2e276125 19223
d521ce57
TT
19224static const gdb_byte *
19225skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
19226 enum dwarf_form form,
19227 unsigned int offset_size,
19228 struct dwarf2_section_info *section)
2e276125 19229{
cf2c3c16 19230 unsigned int bytes_read;
2e276125 19231
cf2c3c16 19232 switch (form)
2e276125 19233 {
cf2c3c16
TT
19234 case DW_FORM_data1:
19235 case DW_FORM_flag:
19236 ++bytes;
19237 break;
19238
19239 case DW_FORM_data2:
19240 bytes += 2;
19241 break;
19242
19243 case DW_FORM_data4:
19244 bytes += 4;
19245 break;
19246
19247 case DW_FORM_data8:
19248 bytes += 8;
19249 break;
19250
19251 case DW_FORM_string:
19252 read_direct_string (abfd, bytes, &bytes_read);
19253 bytes += bytes_read;
19254 break;
19255
19256 case DW_FORM_sec_offset:
19257 case DW_FORM_strp:
36586728 19258 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
19259 bytes += offset_size;
19260 break;
19261
19262 case DW_FORM_block:
19263 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19264 bytes += bytes_read;
19265 break;
19266
19267 case DW_FORM_block1:
19268 bytes += 1 + read_1_byte (abfd, bytes);
19269 break;
19270 case DW_FORM_block2:
19271 bytes += 2 + read_2_bytes (abfd, bytes);
19272 break;
19273 case DW_FORM_block4:
19274 bytes += 4 + read_4_bytes (abfd, bytes);
19275 break;
19276
19277 case DW_FORM_sdata:
19278 case DW_FORM_udata:
3019eac3
DE
19279 case DW_FORM_GNU_addr_index:
19280 case DW_FORM_GNU_str_index:
d521ce57 19281 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
19282 if (bytes == NULL)
19283 {
19284 dwarf2_section_buffer_overflow_complaint (section);
19285 return NULL;
19286 }
cf2c3c16
TT
19287 break;
19288
19289 default:
19290 {
19291 complain:
19292 complaint (&symfile_complaints,
19293 _("invalid form 0x%x in `%s'"),
19294 form,
19295 section->asection->name);
19296 return NULL;
19297 }
2e276125
JB
19298 }
19299
cf2c3c16
TT
19300 return bytes;
19301}
757a13d0 19302
cf2c3c16
TT
19303/* A helper for dwarf_decode_macros that handles skipping an unknown
19304 opcode. Returns an updated pointer to the macro data buffer; or,
19305 on error, issues a complaint and returns NULL. */
757a13d0 19306
d521ce57 19307static const gdb_byte *
cf2c3c16 19308skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
19309 const gdb_byte **opcode_definitions,
19310 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
19311 bfd *abfd,
19312 unsigned int offset_size,
19313 struct dwarf2_section_info *section)
19314{
19315 unsigned int bytes_read, i;
19316 unsigned long arg;
d521ce57 19317 const gdb_byte *defn;
2e276125 19318
cf2c3c16 19319 if (opcode_definitions[opcode] == NULL)
2e276125 19320 {
cf2c3c16
TT
19321 complaint (&symfile_complaints,
19322 _("unrecognized DW_MACFINO opcode 0x%x"),
19323 opcode);
19324 return NULL;
19325 }
2e276125 19326
cf2c3c16
TT
19327 defn = opcode_definitions[opcode];
19328 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19329 defn += bytes_read;
2e276125 19330
cf2c3c16
TT
19331 for (i = 0; i < arg; ++i)
19332 {
f664829e
DE
19333 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19334 section);
cf2c3c16
TT
19335 if (mac_ptr == NULL)
19336 {
19337 /* skip_form_bytes already issued the complaint. */
19338 return NULL;
19339 }
19340 }
757a13d0 19341
cf2c3c16
TT
19342 return mac_ptr;
19343}
757a13d0 19344
cf2c3c16
TT
19345/* A helper function which parses the header of a macro section.
19346 If the macro section is the extended (for now called "GNU") type,
19347 then this updates *OFFSET_SIZE. Returns a pointer to just after
19348 the header, or issues a complaint and returns NULL on error. */
757a13d0 19349
d521ce57
TT
19350static const gdb_byte *
19351dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 19352 bfd *abfd,
d521ce57 19353 const gdb_byte *mac_ptr,
cf2c3c16
TT
19354 unsigned int *offset_size,
19355 int section_is_gnu)
19356{
19357 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 19358
cf2c3c16
TT
19359 if (section_is_gnu)
19360 {
19361 unsigned int version, flags;
757a13d0 19362
cf2c3c16
TT
19363 version = read_2_bytes (abfd, mac_ptr);
19364 if (version != 4)
19365 {
19366 complaint (&symfile_complaints,
19367 _("unrecognized version `%d' in .debug_macro section"),
19368 version);
19369 return NULL;
19370 }
19371 mac_ptr += 2;
757a13d0 19372
cf2c3c16
TT
19373 flags = read_1_byte (abfd, mac_ptr);
19374 ++mac_ptr;
19375 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 19376
cf2c3c16
TT
19377 if ((flags & 2) != 0)
19378 /* We don't need the line table offset. */
19379 mac_ptr += *offset_size;
757a13d0 19380
cf2c3c16
TT
19381 /* Vendor opcode descriptions. */
19382 if ((flags & 4) != 0)
19383 {
19384 unsigned int i, count;
757a13d0 19385
cf2c3c16
TT
19386 count = read_1_byte (abfd, mac_ptr);
19387 ++mac_ptr;
19388 for (i = 0; i < count; ++i)
19389 {
19390 unsigned int opcode, bytes_read;
19391 unsigned long arg;
19392
19393 opcode = read_1_byte (abfd, mac_ptr);
19394 ++mac_ptr;
19395 opcode_definitions[opcode] = mac_ptr;
19396 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19397 mac_ptr += bytes_read;
19398 mac_ptr += arg;
19399 }
757a13d0 19400 }
cf2c3c16 19401 }
757a13d0 19402
cf2c3c16
TT
19403 return mac_ptr;
19404}
757a13d0 19405
cf2c3c16 19406/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 19407 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
19408
19409static void
d521ce57
TT
19410dwarf_decode_macro_bytes (bfd *abfd,
19411 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 19412 struct macro_source_file *current_file,
15d034d0 19413 struct line_header *lh, const char *comp_dir,
cf2c3c16 19414 struct dwarf2_section_info *section,
36586728 19415 int section_is_gnu, int section_is_dwz,
cf2c3c16 19416 unsigned int offset_size,
8fc3fc34
TT
19417 struct objfile *objfile,
19418 htab_t include_hash)
cf2c3c16
TT
19419{
19420 enum dwarf_macro_record_type macinfo_type;
19421 int at_commandline;
d521ce57 19422 const gdb_byte *opcode_definitions[256];
757a13d0 19423
cf2c3c16
TT
19424 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19425 &offset_size, section_is_gnu);
19426 if (mac_ptr == NULL)
19427 {
19428 /* We already issued a complaint. */
19429 return;
19430 }
757a13d0
JK
19431
19432 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19433 GDB is still reading the definitions from command line. First
19434 DW_MACINFO_start_file will need to be ignored as it was already executed
19435 to create CURRENT_FILE for the main source holding also the command line
19436 definitions. On first met DW_MACINFO_start_file this flag is reset to
19437 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19438
19439 at_commandline = 1;
19440
19441 do
19442 {
19443 /* Do we at least have room for a macinfo type byte? */
19444 if (mac_ptr >= mac_end)
19445 {
f664829e 19446 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
19447 break;
19448 }
19449
19450 macinfo_type = read_1_byte (abfd, mac_ptr);
19451 mac_ptr++;
19452
cf2c3c16
TT
19453 /* Note that we rely on the fact that the corresponding GNU and
19454 DWARF constants are the same. */
757a13d0
JK
19455 switch (macinfo_type)
19456 {
19457 /* A zero macinfo type indicates the end of the macro
19458 information. */
19459 case 0:
19460 break;
2e276125 19461
cf2c3c16
TT
19462 case DW_MACRO_GNU_define:
19463 case DW_MACRO_GNU_undef:
19464 case DW_MACRO_GNU_define_indirect:
19465 case DW_MACRO_GNU_undef_indirect:
36586728
TT
19466 case DW_MACRO_GNU_define_indirect_alt:
19467 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 19468 {
891d2f0b 19469 unsigned int bytes_read;
2e276125 19470 int line;
d521ce57 19471 const char *body;
cf2c3c16 19472 int is_define;
2e276125 19473
cf2c3c16
TT
19474 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19475 mac_ptr += bytes_read;
19476
19477 if (macinfo_type == DW_MACRO_GNU_define
19478 || macinfo_type == DW_MACRO_GNU_undef)
19479 {
19480 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19481 mac_ptr += bytes_read;
19482 }
19483 else
19484 {
19485 LONGEST str_offset;
19486
19487 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19488 mac_ptr += offset_size;
2e276125 19489
36586728 19490 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
19491 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19492 || section_is_dwz)
36586728
TT
19493 {
19494 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19495
19496 body = read_indirect_string_from_dwz (dwz, str_offset);
19497 }
19498 else
19499 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
19500 }
19501
19502 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
19503 || macinfo_type == DW_MACRO_GNU_define_indirect
19504 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 19505 if (! current_file)
757a13d0
JK
19506 {
19507 /* DWARF violation as no main source is present. */
19508 complaint (&symfile_complaints,
19509 _("debug info with no main source gives macro %s "
19510 "on line %d: %s"),
cf2c3c16
TT
19511 is_define ? _("definition") : _("undefinition"),
19512 line, body);
757a13d0
JK
19513 break;
19514 }
3e43a32a
MS
19515 if ((line == 0 && !at_commandline)
19516 || (line != 0 && at_commandline))
4d3c2250 19517 complaint (&symfile_complaints,
757a13d0
JK
19518 _("debug info gives %s macro %s with %s line %d: %s"),
19519 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 19520 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
19521 line == 0 ? _("zero") : _("non-zero"), line, body);
19522
cf2c3c16 19523 if (is_define)
757a13d0 19524 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
19525 else
19526 {
19527 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
19528 || macinfo_type == DW_MACRO_GNU_undef_indirect
19529 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
19530 macro_undef (current_file, line, body);
19531 }
2e276125
JB
19532 }
19533 break;
19534
cf2c3c16 19535 case DW_MACRO_GNU_start_file:
2e276125 19536 {
891d2f0b 19537 unsigned int bytes_read;
2e276125
JB
19538 int line, file;
19539
19540 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19541 mac_ptr += bytes_read;
19542 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19543 mac_ptr += bytes_read;
19544
3e43a32a
MS
19545 if ((line == 0 && !at_commandline)
19546 || (line != 0 && at_commandline))
757a13d0
JK
19547 complaint (&symfile_complaints,
19548 _("debug info gives source %d included "
19549 "from %s at %s line %d"),
19550 file, at_commandline ? _("command-line") : _("file"),
19551 line == 0 ? _("zero") : _("non-zero"), line);
19552
19553 if (at_commandline)
19554 {
cf2c3c16
TT
19555 /* This DW_MACRO_GNU_start_file was executed in the
19556 pass one. */
757a13d0
JK
19557 at_commandline = 0;
19558 }
19559 else
19560 current_file = macro_start_file (file, line,
19561 current_file, comp_dir,
cf2c3c16 19562 lh, objfile);
2e276125
JB
19563 }
19564 break;
19565
cf2c3c16 19566 case DW_MACRO_GNU_end_file:
2e276125 19567 if (! current_file)
4d3c2250 19568 complaint (&symfile_complaints,
3e43a32a
MS
19569 _("macro debug info has an unmatched "
19570 "`close_file' directive"));
2e276125
JB
19571 else
19572 {
19573 current_file = current_file->included_by;
19574 if (! current_file)
19575 {
cf2c3c16 19576 enum dwarf_macro_record_type next_type;
2e276125
JB
19577
19578 /* GCC circa March 2002 doesn't produce the zero
19579 type byte marking the end of the compilation
19580 unit. Complain if it's not there, but exit no
19581 matter what. */
19582
19583 /* Do we at least have room for a macinfo type byte? */
19584 if (mac_ptr >= mac_end)
19585 {
f664829e 19586 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
19587 return;
19588 }
19589
19590 /* We don't increment mac_ptr here, so this is just
19591 a look-ahead. */
19592 next_type = read_1_byte (abfd, mac_ptr);
19593 if (next_type != 0)
4d3c2250 19594 complaint (&symfile_complaints,
3e43a32a
MS
19595 _("no terminating 0-type entry for "
19596 "macros in `.debug_macinfo' section"));
2e276125
JB
19597
19598 return;
19599 }
19600 }
19601 break;
19602
cf2c3c16 19603 case DW_MACRO_GNU_transparent_include:
36586728 19604 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19605 {
19606 LONGEST offset;
8fc3fc34 19607 void **slot;
a036ba48
TT
19608 bfd *include_bfd = abfd;
19609 struct dwarf2_section_info *include_section = section;
19610 struct dwarf2_section_info alt_section;
d521ce57 19611 const gdb_byte *include_mac_end = mac_end;
a036ba48 19612 int is_dwz = section_is_dwz;
d521ce57 19613 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
19614
19615 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19616 mac_ptr += offset_size;
19617
a036ba48
TT
19618 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19619 {
19620 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19621
19622 dwarf2_read_section (dwarf2_per_objfile->objfile,
19623 &dwz->macro);
19624
19625 include_bfd = dwz->macro.asection->owner;
19626 include_section = &dwz->macro;
19627 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19628 is_dwz = 1;
19629 }
19630
19631 new_mac_ptr = include_section->buffer + offset;
19632 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19633
8fc3fc34
TT
19634 if (*slot != NULL)
19635 {
19636 /* This has actually happened; see
19637 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19638 complaint (&symfile_complaints,
19639 _("recursive DW_MACRO_GNU_transparent_include in "
19640 ".debug_macro section"));
19641 }
19642 else
19643 {
d521ce57 19644 *slot = (void *) new_mac_ptr;
36586728 19645
a036ba48 19646 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19647 include_mac_end, current_file,
8fc3fc34 19648 lh, comp_dir,
36586728 19649 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19650 offset_size, objfile, include_hash);
19651
d521ce57 19652 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19653 }
cf2c3c16
TT
19654 }
19655 break;
19656
2e276125 19657 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19658 if (!section_is_gnu)
19659 {
19660 unsigned int bytes_read;
19661 int constant;
2e276125 19662
cf2c3c16
TT
19663 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19664 mac_ptr += bytes_read;
19665 read_direct_string (abfd, mac_ptr, &bytes_read);
19666 mac_ptr += bytes_read;
2e276125 19667
cf2c3c16
TT
19668 /* We don't recognize any vendor extensions. */
19669 break;
19670 }
19671 /* FALLTHROUGH */
19672
19673 default:
19674 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19675 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19676 section);
19677 if (mac_ptr == NULL)
19678 return;
19679 break;
2e276125 19680 }
757a13d0 19681 } while (macinfo_type != 0);
2e276125 19682}
8e19ed76 19683
cf2c3c16 19684static void
09262596 19685dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19686 const char *comp_dir, int section_is_gnu)
cf2c3c16 19687{
bb5ed363 19688 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19689 struct line_header *lh = cu->line_header;
19690 bfd *abfd;
d521ce57 19691 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19692 struct macro_source_file *current_file = 0;
19693 enum dwarf_macro_record_type macinfo_type;
19694 unsigned int offset_size = cu->header.offset_size;
d521ce57 19695 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19696 struct cleanup *cleanup;
19697 htab_t include_hash;
19698 void **slot;
09262596
DE
19699 struct dwarf2_section_info *section;
19700 const char *section_name;
19701
19702 if (cu->dwo_unit != NULL)
19703 {
19704 if (section_is_gnu)
19705 {
19706 section = &cu->dwo_unit->dwo_file->sections.macro;
19707 section_name = ".debug_macro.dwo";
19708 }
19709 else
19710 {
19711 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19712 section_name = ".debug_macinfo.dwo";
19713 }
19714 }
19715 else
19716 {
19717 if (section_is_gnu)
19718 {
19719 section = &dwarf2_per_objfile->macro;
19720 section_name = ".debug_macro";
19721 }
19722 else
19723 {
19724 section = &dwarf2_per_objfile->macinfo;
19725 section_name = ".debug_macinfo";
19726 }
19727 }
cf2c3c16 19728
bb5ed363 19729 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19730 if (section->buffer == NULL)
19731 {
fceca515 19732 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19733 return;
19734 }
09262596 19735 abfd = section->asection->owner;
cf2c3c16
TT
19736
19737 /* First pass: Find the name of the base filename.
19738 This filename is needed in order to process all macros whose definition
19739 (or undefinition) comes from the command line. These macros are defined
19740 before the first DW_MACINFO_start_file entry, and yet still need to be
19741 associated to the base file.
19742
19743 To determine the base file name, we scan the macro definitions until we
19744 reach the first DW_MACINFO_start_file entry. We then initialize
19745 CURRENT_FILE accordingly so that any macro definition found before the
19746 first DW_MACINFO_start_file can still be associated to the base file. */
19747
19748 mac_ptr = section->buffer + offset;
19749 mac_end = section->buffer + section->size;
19750
19751 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19752 &offset_size, section_is_gnu);
19753 if (mac_ptr == NULL)
19754 {
19755 /* We already issued a complaint. */
19756 return;
19757 }
19758
19759 do
19760 {
19761 /* Do we at least have room for a macinfo type byte? */
19762 if (mac_ptr >= mac_end)
19763 {
19764 /* Complaint is printed during the second pass as GDB will probably
19765 stop the first pass earlier upon finding
19766 DW_MACINFO_start_file. */
19767 break;
19768 }
19769
19770 macinfo_type = read_1_byte (abfd, mac_ptr);
19771 mac_ptr++;
19772
19773 /* Note that we rely on the fact that the corresponding GNU and
19774 DWARF constants are the same. */
19775 switch (macinfo_type)
19776 {
19777 /* A zero macinfo type indicates the end of the macro
19778 information. */
19779 case 0:
19780 break;
19781
19782 case DW_MACRO_GNU_define:
19783 case DW_MACRO_GNU_undef:
19784 /* Only skip the data by MAC_PTR. */
19785 {
19786 unsigned int bytes_read;
19787
19788 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19789 mac_ptr += bytes_read;
19790 read_direct_string (abfd, mac_ptr, &bytes_read);
19791 mac_ptr += bytes_read;
19792 }
19793 break;
19794
19795 case DW_MACRO_GNU_start_file:
19796 {
19797 unsigned int bytes_read;
19798 int line, file;
19799
19800 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19801 mac_ptr += bytes_read;
19802 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19803 mac_ptr += bytes_read;
19804
19805 current_file = macro_start_file (file, line, current_file,
bb5ed363 19806 comp_dir, lh, objfile);
cf2c3c16
TT
19807 }
19808 break;
19809
19810 case DW_MACRO_GNU_end_file:
19811 /* No data to skip by MAC_PTR. */
19812 break;
19813
19814 case DW_MACRO_GNU_define_indirect:
19815 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19816 case DW_MACRO_GNU_define_indirect_alt:
19817 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19818 {
19819 unsigned int bytes_read;
19820
19821 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19822 mac_ptr += bytes_read;
19823 mac_ptr += offset_size;
19824 }
19825 break;
19826
19827 case DW_MACRO_GNU_transparent_include:
f7a35f02 19828 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19829 /* Note that, according to the spec, a transparent include
19830 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19831 skip this opcode. */
19832 mac_ptr += offset_size;
19833 break;
19834
19835 case DW_MACINFO_vendor_ext:
19836 /* Only skip the data by MAC_PTR. */
19837 if (!section_is_gnu)
19838 {
19839 unsigned int bytes_read;
19840
19841 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19842 mac_ptr += bytes_read;
19843 read_direct_string (abfd, mac_ptr, &bytes_read);
19844 mac_ptr += bytes_read;
19845 }
19846 /* FALLTHROUGH */
19847
19848 default:
19849 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19850 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19851 section);
19852 if (mac_ptr == NULL)
19853 return;
19854 break;
19855 }
19856 } while (macinfo_type != 0 && current_file == NULL);
19857
19858 /* Second pass: Process all entries.
19859
19860 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19861 command-line macro definitions/undefinitions. This flag is unset when we
19862 reach the first DW_MACINFO_start_file entry. */
19863
8fc3fc34
TT
19864 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19865 NULL, xcalloc, xfree);
19866 cleanup = make_cleanup_htab_delete (include_hash);
19867 mac_ptr = section->buffer + offset;
19868 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19869 *slot = (void *) mac_ptr;
8fc3fc34 19870 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19871 current_file, lh, comp_dir, section,
19872 section_is_gnu, 0,
8fc3fc34
TT
19873 offset_size, objfile, include_hash);
19874 do_cleanups (cleanup);
cf2c3c16
TT
19875}
19876
8e19ed76 19877/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19878 if so return true else false. */
380bca97 19879
8e19ed76 19880static int
6e5a29e1 19881attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
19882{
19883 return (attr == NULL ? 0 :
19884 attr->form == DW_FORM_block1
19885 || attr->form == DW_FORM_block2
19886 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19887 || attr->form == DW_FORM_block
19888 || attr->form == DW_FORM_exprloc);
8e19ed76 19889}
4c2df51b 19890
c6a0999f
JB
19891/* Return non-zero if ATTR's value is a section offset --- classes
19892 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19893 You may use DW_UNSND (attr) to retrieve such offsets.
19894
19895 Section 7.5.4, "Attribute Encodings", explains that no attribute
19896 may have a value that belongs to more than one of these classes; it
19897 would be ambiguous if we did, because we use the same forms for all
19898 of them. */
380bca97 19899
3690dd37 19900static int
6e5a29e1 19901attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
19902{
19903 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19904 || attr->form == DW_FORM_data8
19905 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19906}
19907
3690dd37
JB
19908/* Return non-zero if ATTR's value falls in the 'constant' class, or
19909 zero otherwise. When this function returns true, you can apply
19910 dwarf2_get_attr_constant_value to it.
19911
19912 However, note that for some attributes you must check
19913 attr_form_is_section_offset before using this test. DW_FORM_data4
19914 and DW_FORM_data8 are members of both the constant class, and of
19915 the classes that contain offsets into other debug sections
19916 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19917 that, if an attribute's can be either a constant or one of the
19918 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19919 taken as section offsets, not constants. */
380bca97 19920
3690dd37 19921static int
6e5a29e1 19922attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
19923{
19924 switch (attr->form)
19925 {
19926 case DW_FORM_sdata:
19927 case DW_FORM_udata:
19928 case DW_FORM_data1:
19929 case DW_FORM_data2:
19930 case DW_FORM_data4:
19931 case DW_FORM_data8:
19932 return 1;
19933 default:
19934 return 0;
19935 }
19936}
19937
7771576e
SA
19938
19939/* DW_ADDR is always stored already as sect_offset; despite for the forms
19940 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19941
19942static int
6e5a29e1 19943attr_form_is_ref (const struct attribute *attr)
7771576e
SA
19944{
19945 switch (attr->form)
19946 {
19947 case DW_FORM_ref_addr:
19948 case DW_FORM_ref1:
19949 case DW_FORM_ref2:
19950 case DW_FORM_ref4:
19951 case DW_FORM_ref8:
19952 case DW_FORM_ref_udata:
19953 case DW_FORM_GNU_ref_alt:
19954 return 1;
19955 default:
19956 return 0;
19957 }
19958}
19959
3019eac3
DE
19960/* Return the .debug_loc section to use for CU.
19961 For DWO files use .debug_loc.dwo. */
19962
19963static struct dwarf2_section_info *
19964cu_debug_loc_section (struct dwarf2_cu *cu)
19965{
19966 if (cu->dwo_unit)
19967 return &cu->dwo_unit->dwo_file->sections.loc;
19968 return &dwarf2_per_objfile->loc;
19969}
19970
8cf6f0b1
TT
19971/* A helper function that fills in a dwarf2_loclist_baton. */
19972
19973static void
19974fill_in_loclist_baton (struct dwarf2_cu *cu,
19975 struct dwarf2_loclist_baton *baton,
ff39bb5e 19976 const struct attribute *attr)
8cf6f0b1 19977{
3019eac3
DE
19978 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19979
19980 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19981
19982 baton->per_cu = cu->per_cu;
19983 gdb_assert (baton->per_cu);
19984 /* We don't know how long the location list is, but make sure we
19985 don't run off the edge of the section. */
3019eac3
DE
19986 baton->size = section->size - DW_UNSND (attr);
19987 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19988 baton->base_address = cu->base_address;
f664829e 19989 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19990}
19991
4c2df51b 19992static void
ff39bb5e 19993dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 19994 struct dwarf2_cu *cu, int is_block)
4c2df51b 19995{
bb5ed363 19996 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19997 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19998
3690dd37 19999 if (attr_form_is_section_offset (attr)
3019eac3 20000 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
20001 the section. If so, fall through to the complaint in the
20002 other branch. */
3019eac3 20003 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 20004 {
0d53c4c4 20005 struct dwarf2_loclist_baton *baton;
4c2df51b 20006
bb5ed363 20007 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 20008 sizeof (struct dwarf2_loclist_baton));
4c2df51b 20009
8cf6f0b1 20010 fill_in_loclist_baton (cu, baton, attr);
be391dca 20011
d00adf39 20012 if (cu->base_known == 0)
0d53c4c4 20013 complaint (&symfile_complaints,
3e43a32a
MS
20014 _("Location list used without "
20015 "specifying the CU base address."));
4c2df51b 20016
f1e6e072
TT
20017 SYMBOL_ACLASS_INDEX (sym) = (is_block
20018 ? dwarf2_loclist_block_index
20019 : dwarf2_loclist_index);
0d53c4c4
DJ
20020 SYMBOL_LOCATION_BATON (sym) = baton;
20021 }
20022 else
20023 {
20024 struct dwarf2_locexpr_baton *baton;
20025
bb5ed363 20026 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 20027 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
20028 baton->per_cu = cu->per_cu;
20029 gdb_assert (baton->per_cu);
0d53c4c4
DJ
20030
20031 if (attr_form_is_block (attr))
20032 {
20033 /* Note that we're just copying the block's data pointer
20034 here, not the actual data. We're still pointing into the
6502dd73
DJ
20035 info_buffer for SYM's objfile; right now we never release
20036 that buffer, but when we do clean up properly this may
20037 need to change. */
0d53c4c4
DJ
20038 baton->size = DW_BLOCK (attr)->size;
20039 baton->data = DW_BLOCK (attr)->data;
20040 }
20041 else
20042 {
20043 dwarf2_invalid_attrib_class_complaint ("location description",
20044 SYMBOL_NATURAL_NAME (sym));
20045 baton->size = 0;
0d53c4c4 20046 }
6e70227d 20047
f1e6e072
TT
20048 SYMBOL_ACLASS_INDEX (sym) = (is_block
20049 ? dwarf2_locexpr_block_index
20050 : dwarf2_locexpr_index);
0d53c4c4
DJ
20051 SYMBOL_LOCATION_BATON (sym) = baton;
20052 }
4c2df51b 20053}
6502dd73 20054
9aa1f1e3
TT
20055/* Return the OBJFILE associated with the compilation unit CU. If CU
20056 came from a separate debuginfo file, then the master objfile is
20057 returned. */
ae0d2f24
UW
20058
20059struct objfile *
20060dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20061{
9291a0cd 20062 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
20063
20064 /* Return the master objfile, so that we can report and look up the
20065 correct file containing this variable. */
20066 if (objfile->separate_debug_objfile_backlink)
20067 objfile = objfile->separate_debug_objfile_backlink;
20068
20069 return objfile;
20070}
20071
96408a79
SA
20072/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20073 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20074 CU_HEADERP first. */
20075
20076static const struct comp_unit_head *
20077per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20078 struct dwarf2_per_cu_data *per_cu)
20079{
d521ce57 20080 const gdb_byte *info_ptr;
96408a79
SA
20081
20082 if (per_cu->cu)
20083 return &per_cu->cu->header;
20084
8a0459fd 20085 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
20086
20087 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 20088 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
20089
20090 return cu_headerp;
20091}
20092
ae0d2f24
UW
20093/* Return the address size given in the compilation unit header for CU. */
20094
98714339 20095int
ae0d2f24
UW
20096dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20097{
96408a79
SA
20098 struct comp_unit_head cu_header_local;
20099 const struct comp_unit_head *cu_headerp;
c471e790 20100
96408a79
SA
20101 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20102
20103 return cu_headerp->addr_size;
ae0d2f24
UW
20104}
20105
9eae7c52
TT
20106/* Return the offset size given in the compilation unit header for CU. */
20107
20108int
20109dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20110{
96408a79
SA
20111 struct comp_unit_head cu_header_local;
20112 const struct comp_unit_head *cu_headerp;
9c6c53f7 20113
96408a79
SA
20114 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20115
20116 return cu_headerp->offset_size;
20117}
20118
20119/* See its dwarf2loc.h declaration. */
20120
20121int
20122dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20123{
20124 struct comp_unit_head cu_header_local;
20125 const struct comp_unit_head *cu_headerp;
20126
20127 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20128
20129 if (cu_headerp->version == 2)
20130 return cu_headerp->addr_size;
20131 else
20132 return cu_headerp->offset_size;
181cebd4
JK
20133}
20134
9aa1f1e3
TT
20135/* Return the text offset of the CU. The returned offset comes from
20136 this CU's objfile. If this objfile came from a separate debuginfo
20137 file, then the offset may be different from the corresponding
20138 offset in the parent objfile. */
20139
20140CORE_ADDR
20141dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20142{
bb3fa9d0 20143 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
20144
20145 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20146}
20147
348e048f
DE
20148/* Locate the .debug_info compilation unit from CU's objfile which contains
20149 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
20150
20151static struct dwarf2_per_cu_data *
b64f50a1 20152dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 20153 unsigned int offset_in_dwz,
ae038cb0
DJ
20154 struct objfile *objfile)
20155{
20156 struct dwarf2_per_cu_data *this_cu;
20157 int low, high;
36586728 20158 const sect_offset *cu_off;
ae038cb0 20159
ae038cb0
DJ
20160 low = 0;
20161 high = dwarf2_per_objfile->n_comp_units - 1;
20162 while (high > low)
20163 {
36586728 20164 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 20165 int mid = low + (high - low) / 2;
9a619af0 20166
36586728
TT
20167 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20168 cu_off = &mid_cu->offset;
20169 if (mid_cu->is_dwz > offset_in_dwz
20170 || (mid_cu->is_dwz == offset_in_dwz
20171 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
20172 high = mid;
20173 else
20174 low = mid + 1;
20175 }
20176 gdb_assert (low == high);
36586728
TT
20177 this_cu = dwarf2_per_objfile->all_comp_units[low];
20178 cu_off = &this_cu->offset;
20179 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 20180 {
36586728 20181 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
20182 error (_("Dwarf Error: could not find partial DIE containing "
20183 "offset 0x%lx [in module %s]"),
b64f50a1 20184 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 20185
b64f50a1
JK
20186 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20187 <= offset.sect_off);
ae038cb0
DJ
20188 return dwarf2_per_objfile->all_comp_units[low-1];
20189 }
20190 else
20191 {
20192 this_cu = dwarf2_per_objfile->all_comp_units[low];
20193 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
20194 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20195 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20196 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
20197 return this_cu;
20198 }
20199}
20200
23745b47 20201/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 20202
9816fde3 20203static void
23745b47 20204init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 20205{
9816fde3 20206 memset (cu, 0, sizeof (*cu));
23745b47
DE
20207 per_cu->cu = cu;
20208 cu->per_cu = per_cu;
20209 cu->objfile = per_cu->objfile;
93311388 20210 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
20211}
20212
20213/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20214
20215static void
95554aad
TT
20216prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20217 enum language pretend_language)
9816fde3
JK
20218{
20219 struct attribute *attr;
20220
20221 /* Set the language we're debugging. */
20222 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20223 if (attr)
20224 set_cu_language (DW_UNSND (attr), cu);
20225 else
9cded63f 20226 {
95554aad 20227 cu->language = pretend_language;
9cded63f
TT
20228 cu->language_defn = language_def (cu->language);
20229 }
dee91e82
DE
20230
20231 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20232 if (attr)
20233 cu->producer = DW_STRING (attr);
93311388
DE
20234}
20235
ae038cb0
DJ
20236/* Release one cached compilation unit, CU. We unlink it from the tree
20237 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
20238 the caller is responsible for that.
20239 NOTE: DATA is a void * because this function is also used as a
20240 cleanup routine. */
ae038cb0
DJ
20241
20242static void
68dc6402 20243free_heap_comp_unit (void *data)
ae038cb0
DJ
20244{
20245 struct dwarf2_cu *cu = data;
20246
23745b47
DE
20247 gdb_assert (cu->per_cu != NULL);
20248 cu->per_cu->cu = NULL;
ae038cb0
DJ
20249 cu->per_cu = NULL;
20250
20251 obstack_free (&cu->comp_unit_obstack, NULL);
20252
20253 xfree (cu);
20254}
20255
72bf9492 20256/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 20257 when we're finished with it. We can't free the pointer itself, but be
dee91e82 20258 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
20259
20260static void
20261free_stack_comp_unit (void *data)
20262{
20263 struct dwarf2_cu *cu = data;
20264
23745b47
DE
20265 gdb_assert (cu->per_cu != NULL);
20266 cu->per_cu->cu = NULL;
20267 cu->per_cu = NULL;
20268
72bf9492
DJ
20269 obstack_free (&cu->comp_unit_obstack, NULL);
20270 cu->partial_dies = NULL;
ae038cb0
DJ
20271}
20272
20273/* Free all cached compilation units. */
20274
20275static void
20276free_cached_comp_units (void *data)
20277{
20278 struct dwarf2_per_cu_data *per_cu, **last_chain;
20279
20280 per_cu = dwarf2_per_objfile->read_in_chain;
20281 last_chain = &dwarf2_per_objfile->read_in_chain;
20282 while (per_cu != NULL)
20283 {
20284 struct dwarf2_per_cu_data *next_cu;
20285
20286 next_cu = per_cu->cu->read_in_chain;
20287
68dc6402 20288 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20289 *last_chain = next_cu;
20290
20291 per_cu = next_cu;
20292 }
20293}
20294
20295/* Increase the age counter on each cached compilation unit, and free
20296 any that are too old. */
20297
20298static void
20299age_cached_comp_units (void)
20300{
20301 struct dwarf2_per_cu_data *per_cu, **last_chain;
20302
20303 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20304 per_cu = dwarf2_per_objfile->read_in_chain;
20305 while (per_cu != NULL)
20306 {
20307 per_cu->cu->last_used ++;
20308 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20309 dwarf2_mark (per_cu->cu);
20310 per_cu = per_cu->cu->read_in_chain;
20311 }
20312
20313 per_cu = dwarf2_per_objfile->read_in_chain;
20314 last_chain = &dwarf2_per_objfile->read_in_chain;
20315 while (per_cu != NULL)
20316 {
20317 struct dwarf2_per_cu_data *next_cu;
20318
20319 next_cu = per_cu->cu->read_in_chain;
20320
20321 if (!per_cu->cu->mark)
20322 {
68dc6402 20323 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20324 *last_chain = next_cu;
20325 }
20326 else
20327 last_chain = &per_cu->cu->read_in_chain;
20328
20329 per_cu = next_cu;
20330 }
20331}
20332
20333/* Remove a single compilation unit from the cache. */
20334
20335static void
dee91e82 20336free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
20337{
20338 struct dwarf2_per_cu_data *per_cu, **last_chain;
20339
20340 per_cu = dwarf2_per_objfile->read_in_chain;
20341 last_chain = &dwarf2_per_objfile->read_in_chain;
20342 while (per_cu != NULL)
20343 {
20344 struct dwarf2_per_cu_data *next_cu;
20345
20346 next_cu = per_cu->cu->read_in_chain;
20347
dee91e82 20348 if (per_cu == target_per_cu)
ae038cb0 20349 {
68dc6402 20350 free_heap_comp_unit (per_cu->cu);
dee91e82 20351 per_cu->cu = NULL;
ae038cb0
DJ
20352 *last_chain = next_cu;
20353 break;
20354 }
20355 else
20356 last_chain = &per_cu->cu->read_in_chain;
20357
20358 per_cu = next_cu;
20359 }
20360}
20361
fe3e1990
DJ
20362/* Release all extra memory associated with OBJFILE. */
20363
20364void
20365dwarf2_free_objfile (struct objfile *objfile)
20366{
20367 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20368
20369 if (dwarf2_per_objfile == NULL)
20370 return;
20371
20372 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20373 free_cached_comp_units (NULL);
20374
7b9f3c50
DE
20375 if (dwarf2_per_objfile->quick_file_names_table)
20376 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 20377
fe3e1990
DJ
20378 /* Everything else should be on the objfile obstack. */
20379}
20380
dee91e82
DE
20381/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20382 We store these in a hash table separate from the DIEs, and preserve them
20383 when the DIEs are flushed out of cache.
20384
20385 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 20386 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
20387 or the type may come from a DWO file. Furthermore, while it's more logical
20388 to use per_cu->section+offset, with Fission the section with the data is in
20389 the DWO file but we don't know that section at the point we need it.
20390 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20391 because we can enter the lookup routine, get_die_type_at_offset, from
20392 outside this file, and thus won't necessarily have PER_CU->cu.
20393 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 20394
dee91e82 20395struct dwarf2_per_cu_offset_and_type
1c379e20 20396{
dee91e82 20397 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 20398 sect_offset offset;
1c379e20
DJ
20399 struct type *type;
20400};
20401
dee91e82 20402/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20403
20404static hashval_t
dee91e82 20405per_cu_offset_and_type_hash (const void *item)
1c379e20 20406{
dee91e82 20407 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 20408
dee91e82 20409 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
20410}
20411
dee91e82 20412/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20413
20414static int
dee91e82 20415per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 20416{
dee91e82
DE
20417 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20418 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 20419
dee91e82
DE
20420 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20421 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
20422}
20423
20424/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
20425 table if necessary. For convenience, return TYPE.
20426
20427 The DIEs reading must have careful ordering to:
20428 * Not cause infite loops trying to read in DIEs as a prerequisite for
20429 reading current DIE.
20430 * Not trying to dereference contents of still incompletely read in types
20431 while reading in other DIEs.
20432 * Enable referencing still incompletely read in types just by a pointer to
20433 the type without accessing its fields.
20434
20435 Therefore caller should follow these rules:
20436 * Try to fetch any prerequisite types we may need to build this DIE type
20437 before building the type and calling set_die_type.
e71ec853 20438 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
20439 possible before fetching more types to complete the current type.
20440 * Make the type as complete as possible before fetching more types. */
1c379e20 20441
f792889a 20442static struct type *
1c379e20
DJ
20443set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20444{
dee91e82 20445 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 20446 struct objfile *objfile = cu->objfile;
1c379e20 20447
b4ba55a1
JB
20448 /* For Ada types, make sure that the gnat-specific data is always
20449 initialized (if not already set). There are a few types where
20450 we should not be doing so, because the type-specific area is
20451 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20452 where the type-specific area is used to store the floatformat).
20453 But this is not a problem, because the gnat-specific information
20454 is actually not needed for these types. */
20455 if (need_gnat_info (cu)
20456 && TYPE_CODE (type) != TYPE_CODE_FUNC
20457 && TYPE_CODE (type) != TYPE_CODE_FLT
20458 && !HAVE_GNAT_AUX_INFO (type))
20459 INIT_GNAT_SPECIFIC (type);
20460
dee91e82 20461 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20462 {
dee91e82
DE
20463 dwarf2_per_objfile->die_type_hash =
20464 htab_create_alloc_ex (127,
20465 per_cu_offset_and_type_hash,
20466 per_cu_offset_and_type_eq,
20467 NULL,
20468 &objfile->objfile_obstack,
20469 hashtab_obstack_allocate,
20470 dummy_obstack_deallocate);
f792889a 20471 }
1c379e20 20472
dee91e82 20473 ofs.per_cu = cu->per_cu;
1c379e20
DJ
20474 ofs.offset = die->offset;
20475 ofs.type = type;
dee91e82
DE
20476 slot = (struct dwarf2_per_cu_offset_and_type **)
20477 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
20478 if (*slot)
20479 complaint (&symfile_complaints,
20480 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 20481 die->offset.sect_off);
673bfd45 20482 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 20483 **slot = ofs;
f792889a 20484 return type;
1c379e20
DJ
20485}
20486
02142a6c
DE
20487/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20488 or return NULL if the die does not have a saved type. */
1c379e20
DJ
20489
20490static struct type *
b64f50a1 20491get_die_type_at_offset (sect_offset offset,
673bfd45 20492 struct dwarf2_per_cu_data *per_cu)
1c379e20 20493{
dee91e82 20494 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 20495
dee91e82 20496 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20497 return NULL;
1c379e20 20498
dee91e82 20499 ofs.per_cu = per_cu;
673bfd45 20500 ofs.offset = offset;
dee91e82 20501 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
20502 if (slot)
20503 return slot->type;
20504 else
20505 return NULL;
20506}
20507
02142a6c 20508/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
20509 or return NULL if DIE does not have a saved type. */
20510
20511static struct type *
20512get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20513{
20514 return get_die_type_at_offset (die->offset, cu->per_cu);
20515}
20516
10b3939b
DJ
20517/* Add a dependence relationship from CU to REF_PER_CU. */
20518
20519static void
20520dwarf2_add_dependence (struct dwarf2_cu *cu,
20521 struct dwarf2_per_cu_data *ref_per_cu)
20522{
20523 void **slot;
20524
20525 if (cu->dependencies == NULL)
20526 cu->dependencies
20527 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20528 NULL, &cu->comp_unit_obstack,
20529 hashtab_obstack_allocate,
20530 dummy_obstack_deallocate);
20531
20532 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20533 if (*slot == NULL)
20534 *slot = ref_per_cu;
20535}
1c379e20 20536
f504f079
DE
20537/* Subroutine of dwarf2_mark to pass to htab_traverse.
20538 Set the mark field in every compilation unit in the
ae038cb0
DJ
20539 cache that we must keep because we are keeping CU. */
20540
10b3939b
DJ
20541static int
20542dwarf2_mark_helper (void **slot, void *data)
20543{
20544 struct dwarf2_per_cu_data *per_cu;
20545
20546 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
20547
20548 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20549 reading of the chain. As such dependencies remain valid it is not much
20550 useful to track and undo them during QUIT cleanups. */
20551 if (per_cu->cu == NULL)
20552 return 1;
20553
10b3939b
DJ
20554 if (per_cu->cu->mark)
20555 return 1;
20556 per_cu->cu->mark = 1;
20557
20558 if (per_cu->cu->dependencies != NULL)
20559 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20560
20561 return 1;
20562}
20563
f504f079
DE
20564/* Set the mark field in CU and in every other compilation unit in the
20565 cache that we must keep because we are keeping CU. */
20566
ae038cb0
DJ
20567static void
20568dwarf2_mark (struct dwarf2_cu *cu)
20569{
20570 if (cu->mark)
20571 return;
20572 cu->mark = 1;
10b3939b
DJ
20573 if (cu->dependencies != NULL)
20574 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
20575}
20576
20577static void
20578dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20579{
20580 while (per_cu)
20581 {
20582 per_cu->cu->mark = 0;
20583 per_cu = per_cu->cu->read_in_chain;
20584 }
72bf9492
DJ
20585}
20586
72bf9492
DJ
20587/* Trivial hash function for partial_die_info: the hash value of a DIE
20588 is its offset in .debug_info for this objfile. */
20589
20590static hashval_t
20591partial_die_hash (const void *item)
20592{
20593 const struct partial_die_info *part_die = item;
9a619af0 20594
b64f50a1 20595 return part_die->offset.sect_off;
72bf9492
DJ
20596}
20597
20598/* Trivial comparison function for partial_die_info structures: two DIEs
20599 are equal if they have the same offset. */
20600
20601static int
20602partial_die_eq (const void *item_lhs, const void *item_rhs)
20603{
20604 const struct partial_die_info *part_die_lhs = item_lhs;
20605 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 20606
b64f50a1 20607 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
20608}
20609
ae038cb0
DJ
20610static struct cmd_list_element *set_dwarf2_cmdlist;
20611static struct cmd_list_element *show_dwarf2_cmdlist;
20612
20613static void
20614set_dwarf2_cmd (char *args, int from_tty)
20615{
20616 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20617}
20618
20619static void
20620show_dwarf2_cmd (char *args, int from_tty)
6e70227d 20621{
ae038cb0
DJ
20622 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20623}
20624
4bf44c1c 20625/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
20626
20627static void
c1bd65d0 20628dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
20629{
20630 struct dwarf2_per_objfile *data = d;
8b70b953 20631 int ix;
8b70b953 20632
626f2d1c
TT
20633 /* Make sure we don't accidentally use dwarf2_per_objfile while
20634 cleaning up. */
20635 dwarf2_per_objfile = NULL;
20636
59b0c7c1
JB
20637 for (ix = 0; ix < data->n_comp_units; ++ix)
20638 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 20639
59b0c7c1 20640 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 20641 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
20642 data->all_type_units[ix]->per_cu.imported_symtabs);
20643 xfree (data->all_type_units);
95554aad 20644
8b70b953 20645 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
20646
20647 if (data->dwo_files)
20648 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20649 if (data->dwp_file)
20650 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20651
20652 if (data->dwz_file && data->dwz_file->dwz_bfd)
20653 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20654}
20655
20656\f
ae2de4f8 20657/* The "save gdb-index" command. */
9291a0cd
TT
20658
20659/* The contents of the hash table we create when building the string
20660 table. */
20661struct strtab_entry
20662{
20663 offset_type offset;
20664 const char *str;
20665};
20666
559a7a62
JK
20667/* Hash function for a strtab_entry.
20668
20669 Function is used only during write_hash_table so no index format backward
20670 compatibility is needed. */
b89be57b 20671
9291a0cd
TT
20672static hashval_t
20673hash_strtab_entry (const void *e)
20674{
20675 const struct strtab_entry *entry = e;
559a7a62 20676 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20677}
20678
20679/* Equality function for a strtab_entry. */
b89be57b 20680
9291a0cd
TT
20681static int
20682eq_strtab_entry (const void *a, const void *b)
20683{
20684 const struct strtab_entry *ea = a;
20685 const struct strtab_entry *eb = b;
20686 return !strcmp (ea->str, eb->str);
20687}
20688
20689/* Create a strtab_entry hash table. */
b89be57b 20690
9291a0cd
TT
20691static htab_t
20692create_strtab (void)
20693{
20694 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20695 xfree, xcalloc, xfree);
20696}
20697
20698/* Add a string to the constant pool. Return the string's offset in
20699 host order. */
b89be57b 20700
9291a0cd
TT
20701static offset_type
20702add_string (htab_t table, struct obstack *cpool, const char *str)
20703{
20704 void **slot;
20705 struct strtab_entry entry;
20706 struct strtab_entry *result;
20707
20708 entry.str = str;
20709 slot = htab_find_slot (table, &entry, INSERT);
20710 if (*slot)
20711 result = *slot;
20712 else
20713 {
20714 result = XNEW (struct strtab_entry);
20715 result->offset = obstack_object_size (cpool);
20716 result->str = str;
20717 obstack_grow_str0 (cpool, str);
20718 *slot = result;
20719 }
20720 return result->offset;
20721}
20722
20723/* An entry in the symbol table. */
20724struct symtab_index_entry
20725{
20726 /* The name of the symbol. */
20727 const char *name;
20728 /* The offset of the name in the constant pool. */
20729 offset_type index_offset;
20730 /* A sorted vector of the indices of all the CUs that hold an object
20731 of this name. */
20732 VEC (offset_type) *cu_indices;
20733};
20734
20735/* The symbol table. This is a power-of-2-sized hash table. */
20736struct mapped_symtab
20737{
20738 offset_type n_elements;
20739 offset_type size;
20740 struct symtab_index_entry **data;
20741};
20742
20743/* Hash function for a symtab_index_entry. */
b89be57b 20744
9291a0cd
TT
20745static hashval_t
20746hash_symtab_entry (const void *e)
20747{
20748 const struct symtab_index_entry *entry = e;
20749 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20750 sizeof (offset_type) * VEC_length (offset_type,
20751 entry->cu_indices),
20752 0);
20753}
20754
20755/* Equality function for a symtab_index_entry. */
b89be57b 20756
9291a0cd
TT
20757static int
20758eq_symtab_entry (const void *a, const void *b)
20759{
20760 const struct symtab_index_entry *ea = a;
20761 const struct symtab_index_entry *eb = b;
20762 int len = VEC_length (offset_type, ea->cu_indices);
20763 if (len != VEC_length (offset_type, eb->cu_indices))
20764 return 0;
20765 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20766 VEC_address (offset_type, eb->cu_indices),
20767 sizeof (offset_type) * len);
20768}
20769
20770/* Destroy a symtab_index_entry. */
b89be57b 20771
9291a0cd
TT
20772static void
20773delete_symtab_entry (void *p)
20774{
20775 struct symtab_index_entry *entry = p;
20776 VEC_free (offset_type, entry->cu_indices);
20777 xfree (entry);
20778}
20779
20780/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20781
9291a0cd 20782static htab_t
3876f04e 20783create_symbol_hash_table (void)
9291a0cd
TT
20784{
20785 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20786 delete_symtab_entry, xcalloc, xfree);
20787}
20788
20789/* Create a new mapped symtab object. */
b89be57b 20790
9291a0cd
TT
20791static struct mapped_symtab *
20792create_mapped_symtab (void)
20793{
20794 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20795 symtab->n_elements = 0;
20796 symtab->size = 1024;
20797 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20798 return symtab;
20799}
20800
20801/* Destroy a mapped_symtab. */
b89be57b 20802
9291a0cd
TT
20803static void
20804cleanup_mapped_symtab (void *p)
20805{
20806 struct mapped_symtab *symtab = p;
20807 /* The contents of the array are freed when the other hash table is
20808 destroyed. */
20809 xfree (symtab->data);
20810 xfree (symtab);
20811}
20812
20813/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20814 the slot.
20815
20816 Function is used only during write_hash_table so no index format backward
20817 compatibility is needed. */
b89be57b 20818
9291a0cd
TT
20819static struct symtab_index_entry **
20820find_slot (struct mapped_symtab *symtab, const char *name)
20821{
559a7a62 20822 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20823
20824 index = hash & (symtab->size - 1);
20825 step = ((hash * 17) & (symtab->size - 1)) | 1;
20826
20827 for (;;)
20828 {
20829 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20830 return &symtab->data[index];
20831 index = (index + step) & (symtab->size - 1);
20832 }
20833}
20834
20835/* Expand SYMTAB's hash table. */
b89be57b 20836
9291a0cd
TT
20837static void
20838hash_expand (struct mapped_symtab *symtab)
20839{
20840 offset_type old_size = symtab->size;
20841 offset_type i;
20842 struct symtab_index_entry **old_entries = symtab->data;
20843
20844 symtab->size *= 2;
20845 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20846
20847 for (i = 0; i < old_size; ++i)
20848 {
20849 if (old_entries[i])
20850 {
20851 struct symtab_index_entry **slot = find_slot (symtab,
20852 old_entries[i]->name);
20853 *slot = old_entries[i];
20854 }
20855 }
20856
20857 xfree (old_entries);
20858}
20859
156942c7
DE
20860/* Add an entry to SYMTAB. NAME is the name of the symbol.
20861 CU_INDEX is the index of the CU in which the symbol appears.
20862 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20863
9291a0cd
TT
20864static void
20865add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20866 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20867 offset_type cu_index)
20868{
20869 struct symtab_index_entry **slot;
156942c7 20870 offset_type cu_index_and_attrs;
9291a0cd
TT
20871
20872 ++symtab->n_elements;
20873 if (4 * symtab->n_elements / 3 >= symtab->size)
20874 hash_expand (symtab);
20875
20876 slot = find_slot (symtab, name);
20877 if (!*slot)
20878 {
20879 *slot = XNEW (struct symtab_index_entry);
20880 (*slot)->name = name;
156942c7 20881 /* index_offset is set later. */
9291a0cd
TT
20882 (*slot)->cu_indices = NULL;
20883 }
156942c7
DE
20884
20885 cu_index_and_attrs = 0;
20886 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20887 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20888 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20889
20890 /* We don't want to record an index value twice as we want to avoid the
20891 duplication.
20892 We process all global symbols and then all static symbols
20893 (which would allow us to avoid the duplication by only having to check
20894 the last entry pushed), but a symbol could have multiple kinds in one CU.
20895 To keep things simple we don't worry about the duplication here and
20896 sort and uniqufy the list after we've processed all symbols. */
20897 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20898}
20899
20900/* qsort helper routine for uniquify_cu_indices. */
20901
20902static int
20903offset_type_compare (const void *ap, const void *bp)
20904{
20905 offset_type a = *(offset_type *) ap;
20906 offset_type b = *(offset_type *) bp;
20907
20908 return (a > b) - (b > a);
20909}
20910
20911/* Sort and remove duplicates of all symbols' cu_indices lists. */
20912
20913static void
20914uniquify_cu_indices (struct mapped_symtab *symtab)
20915{
20916 int i;
20917
20918 for (i = 0; i < symtab->size; ++i)
20919 {
20920 struct symtab_index_entry *entry = symtab->data[i];
20921
20922 if (entry
20923 && entry->cu_indices != NULL)
20924 {
20925 unsigned int next_to_insert, next_to_check;
20926 offset_type last_value;
20927
20928 qsort (VEC_address (offset_type, entry->cu_indices),
20929 VEC_length (offset_type, entry->cu_indices),
20930 sizeof (offset_type), offset_type_compare);
20931
20932 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20933 next_to_insert = 1;
20934 for (next_to_check = 1;
20935 next_to_check < VEC_length (offset_type, entry->cu_indices);
20936 ++next_to_check)
20937 {
20938 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20939 != last_value)
20940 {
20941 last_value = VEC_index (offset_type, entry->cu_indices,
20942 next_to_check);
20943 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20944 last_value);
20945 ++next_to_insert;
20946 }
20947 }
20948 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20949 }
20950 }
9291a0cd
TT
20951}
20952
20953/* Add a vector of indices to the constant pool. */
b89be57b 20954
9291a0cd 20955static offset_type
3876f04e 20956add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20957 struct symtab_index_entry *entry)
20958{
20959 void **slot;
20960
3876f04e 20961 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20962 if (!*slot)
20963 {
20964 offset_type len = VEC_length (offset_type, entry->cu_indices);
20965 offset_type val = MAYBE_SWAP (len);
20966 offset_type iter;
20967 int i;
20968
20969 *slot = entry;
20970 entry->index_offset = obstack_object_size (cpool);
20971
20972 obstack_grow (cpool, &val, sizeof (val));
20973 for (i = 0;
20974 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20975 ++i)
20976 {
20977 val = MAYBE_SWAP (iter);
20978 obstack_grow (cpool, &val, sizeof (val));
20979 }
20980 }
20981 else
20982 {
20983 struct symtab_index_entry *old_entry = *slot;
20984 entry->index_offset = old_entry->index_offset;
20985 entry = old_entry;
20986 }
20987 return entry->index_offset;
20988}
20989
20990/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20991 constant pool entries going into the obstack CPOOL. */
b89be57b 20992
9291a0cd
TT
20993static void
20994write_hash_table (struct mapped_symtab *symtab,
20995 struct obstack *output, struct obstack *cpool)
20996{
20997 offset_type i;
3876f04e 20998 htab_t symbol_hash_table;
9291a0cd
TT
20999 htab_t str_table;
21000
3876f04e 21001 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 21002 str_table = create_strtab ();
3876f04e 21003
9291a0cd
TT
21004 /* We add all the index vectors to the constant pool first, to
21005 ensure alignment is ok. */
21006 for (i = 0; i < symtab->size; ++i)
21007 {
21008 if (symtab->data[i])
3876f04e 21009 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
21010 }
21011
21012 /* Now write out the hash table. */
21013 for (i = 0; i < symtab->size; ++i)
21014 {
21015 offset_type str_off, vec_off;
21016
21017 if (symtab->data[i])
21018 {
21019 str_off = add_string (str_table, cpool, symtab->data[i]->name);
21020 vec_off = symtab->data[i]->index_offset;
21021 }
21022 else
21023 {
21024 /* While 0 is a valid constant pool index, it is not valid
21025 to have 0 for both offsets. */
21026 str_off = 0;
21027 vec_off = 0;
21028 }
21029
21030 str_off = MAYBE_SWAP (str_off);
21031 vec_off = MAYBE_SWAP (vec_off);
21032
21033 obstack_grow (output, &str_off, sizeof (str_off));
21034 obstack_grow (output, &vec_off, sizeof (vec_off));
21035 }
21036
21037 htab_delete (str_table);
3876f04e 21038 htab_delete (symbol_hash_table);
9291a0cd
TT
21039}
21040
0a5429f6
DE
21041/* Struct to map psymtab to CU index in the index file. */
21042struct psymtab_cu_index_map
21043{
21044 struct partial_symtab *psymtab;
21045 unsigned int cu_index;
21046};
21047
21048static hashval_t
21049hash_psymtab_cu_index (const void *item)
21050{
21051 const struct psymtab_cu_index_map *map = item;
21052
21053 return htab_hash_pointer (map->psymtab);
21054}
21055
21056static int
21057eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21058{
21059 const struct psymtab_cu_index_map *lhs = item_lhs;
21060 const struct psymtab_cu_index_map *rhs = item_rhs;
21061
21062 return lhs->psymtab == rhs->psymtab;
21063}
21064
21065/* Helper struct for building the address table. */
21066struct addrmap_index_data
21067{
21068 struct objfile *objfile;
21069 struct obstack *addr_obstack;
21070 htab_t cu_index_htab;
21071
21072 /* Non-zero if the previous_* fields are valid.
21073 We can't write an entry until we see the next entry (since it is only then
21074 that we know the end of the entry). */
21075 int previous_valid;
21076 /* Index of the CU in the table of all CUs in the index file. */
21077 unsigned int previous_cu_index;
0963b4bd 21078 /* Start address of the CU. */
0a5429f6
DE
21079 CORE_ADDR previous_cu_start;
21080};
21081
21082/* Write an address entry to OBSTACK. */
b89be57b 21083
9291a0cd 21084static void
0a5429f6
DE
21085add_address_entry (struct objfile *objfile, struct obstack *obstack,
21086 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 21087{
0a5429f6 21088 offset_type cu_index_to_write;
948f8e3d 21089 gdb_byte addr[8];
9291a0cd
TT
21090 CORE_ADDR baseaddr;
21091
21092 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21093
0a5429f6
DE
21094 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21095 obstack_grow (obstack, addr, 8);
21096 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21097 obstack_grow (obstack, addr, 8);
21098 cu_index_to_write = MAYBE_SWAP (cu_index);
21099 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21100}
21101
21102/* Worker function for traversing an addrmap to build the address table. */
21103
21104static int
21105add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21106{
21107 struct addrmap_index_data *data = datap;
21108 struct partial_symtab *pst = obj;
0a5429f6
DE
21109
21110 if (data->previous_valid)
21111 add_address_entry (data->objfile, data->addr_obstack,
21112 data->previous_cu_start, start_addr,
21113 data->previous_cu_index);
21114
21115 data->previous_cu_start = start_addr;
21116 if (pst != NULL)
21117 {
21118 struct psymtab_cu_index_map find_map, *map;
21119 find_map.psymtab = pst;
21120 map = htab_find (data->cu_index_htab, &find_map);
21121 gdb_assert (map != NULL);
21122 data->previous_cu_index = map->cu_index;
21123 data->previous_valid = 1;
21124 }
21125 else
21126 data->previous_valid = 0;
21127
21128 return 0;
21129}
21130
21131/* Write OBJFILE's address map to OBSTACK.
21132 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21133 in the index file. */
21134
21135static void
21136write_address_map (struct objfile *objfile, struct obstack *obstack,
21137 htab_t cu_index_htab)
21138{
21139 struct addrmap_index_data addrmap_index_data;
21140
21141 /* When writing the address table, we have to cope with the fact that
21142 the addrmap iterator only provides the start of a region; we have to
21143 wait until the next invocation to get the start of the next region. */
21144
21145 addrmap_index_data.objfile = objfile;
21146 addrmap_index_data.addr_obstack = obstack;
21147 addrmap_index_data.cu_index_htab = cu_index_htab;
21148 addrmap_index_data.previous_valid = 0;
21149
21150 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21151 &addrmap_index_data);
21152
21153 /* It's highly unlikely the last entry (end address = 0xff...ff)
21154 is valid, but we should still handle it.
21155 The end address is recorded as the start of the next region, but that
21156 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21157 anyway. */
21158 if (addrmap_index_data.previous_valid)
21159 add_address_entry (objfile, obstack,
21160 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21161 addrmap_index_data.previous_cu_index);
9291a0cd
TT
21162}
21163
156942c7
DE
21164/* Return the symbol kind of PSYM. */
21165
21166static gdb_index_symbol_kind
21167symbol_kind (struct partial_symbol *psym)
21168{
21169 domain_enum domain = PSYMBOL_DOMAIN (psym);
21170 enum address_class aclass = PSYMBOL_CLASS (psym);
21171
21172 switch (domain)
21173 {
21174 case VAR_DOMAIN:
21175 switch (aclass)
21176 {
21177 case LOC_BLOCK:
21178 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21179 case LOC_TYPEDEF:
21180 return GDB_INDEX_SYMBOL_KIND_TYPE;
21181 case LOC_COMPUTED:
21182 case LOC_CONST_BYTES:
21183 case LOC_OPTIMIZED_OUT:
21184 case LOC_STATIC:
21185 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21186 case LOC_CONST:
21187 /* Note: It's currently impossible to recognize psyms as enum values
21188 short of reading the type info. For now punt. */
21189 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21190 default:
21191 /* There are other LOC_FOO values that one might want to classify
21192 as variables, but dwarf2read.c doesn't currently use them. */
21193 return GDB_INDEX_SYMBOL_KIND_OTHER;
21194 }
21195 case STRUCT_DOMAIN:
21196 return GDB_INDEX_SYMBOL_KIND_TYPE;
21197 default:
21198 return GDB_INDEX_SYMBOL_KIND_OTHER;
21199 }
21200}
21201
9291a0cd 21202/* Add a list of partial symbols to SYMTAB. */
b89be57b 21203
9291a0cd
TT
21204static void
21205write_psymbols (struct mapped_symtab *symtab,
987d643c 21206 htab_t psyms_seen,
9291a0cd
TT
21207 struct partial_symbol **psymp,
21208 int count,
987d643c
TT
21209 offset_type cu_index,
21210 int is_static)
9291a0cd
TT
21211{
21212 for (; count-- > 0; ++psymp)
21213 {
156942c7
DE
21214 struct partial_symbol *psym = *psymp;
21215 void **slot;
987d643c 21216
156942c7 21217 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 21218 error (_("Ada is not currently supported by the index"));
987d643c 21219
987d643c 21220 /* Only add a given psymbol once. */
156942c7 21221 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
21222 if (!*slot)
21223 {
156942c7
DE
21224 gdb_index_symbol_kind kind = symbol_kind (psym);
21225
21226 *slot = psym;
21227 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21228 is_static, kind, cu_index);
987d643c 21229 }
9291a0cd
TT
21230 }
21231}
21232
21233/* Write the contents of an ("unfinished") obstack to FILE. Throw an
21234 exception if there is an error. */
b89be57b 21235
9291a0cd
TT
21236static void
21237write_obstack (FILE *file, struct obstack *obstack)
21238{
21239 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21240 file)
21241 != obstack_object_size (obstack))
21242 error (_("couldn't data write to file"));
21243}
21244
21245/* Unlink a file if the argument is not NULL. */
b89be57b 21246
9291a0cd
TT
21247static void
21248unlink_if_set (void *p)
21249{
21250 char **filename = p;
21251 if (*filename)
21252 unlink (*filename);
21253}
21254
1fd400ff
TT
21255/* A helper struct used when iterating over debug_types. */
21256struct signatured_type_index_data
21257{
21258 struct objfile *objfile;
21259 struct mapped_symtab *symtab;
21260 struct obstack *types_list;
987d643c 21261 htab_t psyms_seen;
1fd400ff
TT
21262 int cu_index;
21263};
21264
21265/* A helper function that writes a single signatured_type to an
21266 obstack. */
b89be57b 21267
1fd400ff
TT
21268static int
21269write_one_signatured_type (void **slot, void *d)
21270{
21271 struct signatured_type_index_data *info = d;
21272 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 21273 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
21274 gdb_byte val[8];
21275
21276 write_psymbols (info->symtab,
987d643c 21277 info->psyms_seen,
3e43a32a
MS
21278 info->objfile->global_psymbols.list
21279 + psymtab->globals_offset,
987d643c
TT
21280 psymtab->n_global_syms, info->cu_index,
21281 0);
1fd400ff 21282 write_psymbols (info->symtab,
987d643c 21283 info->psyms_seen,
3e43a32a
MS
21284 info->objfile->static_psymbols.list
21285 + psymtab->statics_offset,
987d643c
TT
21286 psymtab->n_static_syms, info->cu_index,
21287 1);
1fd400ff 21288
b64f50a1
JK
21289 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21290 entry->per_cu.offset.sect_off);
1fd400ff 21291 obstack_grow (info->types_list, val, 8);
3019eac3
DE
21292 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21293 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
21294 obstack_grow (info->types_list, val, 8);
21295 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21296 obstack_grow (info->types_list, val, 8);
21297
21298 ++info->cu_index;
21299
21300 return 1;
21301}
21302
95554aad
TT
21303/* Recurse into all "included" dependencies and write their symbols as
21304 if they appeared in this psymtab. */
21305
21306static void
21307recursively_write_psymbols (struct objfile *objfile,
21308 struct partial_symtab *psymtab,
21309 struct mapped_symtab *symtab,
21310 htab_t psyms_seen,
21311 offset_type cu_index)
21312{
21313 int i;
21314
21315 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21316 if (psymtab->dependencies[i]->user != NULL)
21317 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21318 symtab, psyms_seen, cu_index);
21319
21320 write_psymbols (symtab,
21321 psyms_seen,
21322 objfile->global_psymbols.list + psymtab->globals_offset,
21323 psymtab->n_global_syms, cu_index,
21324 0);
21325 write_psymbols (symtab,
21326 psyms_seen,
21327 objfile->static_psymbols.list + psymtab->statics_offset,
21328 psymtab->n_static_syms, cu_index,
21329 1);
21330}
21331
9291a0cd 21332/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 21333
9291a0cd
TT
21334static void
21335write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21336{
21337 struct cleanup *cleanup;
21338 char *filename, *cleanup_filename;
1fd400ff
TT
21339 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21340 struct obstack cu_list, types_cu_list;
9291a0cd
TT
21341 int i;
21342 FILE *out_file;
21343 struct mapped_symtab *symtab;
21344 offset_type val, size_of_contents, total_len;
21345 struct stat st;
987d643c 21346 htab_t psyms_seen;
0a5429f6
DE
21347 htab_t cu_index_htab;
21348 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 21349
9291a0cd
TT
21350 if (dwarf2_per_objfile->using_index)
21351 error (_("Cannot use an index to create the index"));
21352
8b70b953
TT
21353 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21354 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21355
260b681b
DE
21356 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21357 return;
21358
4262abfb
JK
21359 if (stat (objfile_name (objfile), &st) < 0)
21360 perror_with_name (objfile_name (objfile));
9291a0cd 21361
4262abfb 21362 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
21363 INDEX_SUFFIX, (char *) NULL);
21364 cleanup = make_cleanup (xfree, filename);
21365
614c279d 21366 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
21367 if (!out_file)
21368 error (_("Can't open `%s' for writing"), filename);
21369
21370 cleanup_filename = filename;
21371 make_cleanup (unlink_if_set, &cleanup_filename);
21372
21373 symtab = create_mapped_symtab ();
21374 make_cleanup (cleanup_mapped_symtab, symtab);
21375
21376 obstack_init (&addr_obstack);
21377 make_cleanup_obstack_free (&addr_obstack);
21378
21379 obstack_init (&cu_list);
21380 make_cleanup_obstack_free (&cu_list);
21381
1fd400ff
TT
21382 obstack_init (&types_cu_list);
21383 make_cleanup_obstack_free (&types_cu_list);
21384
987d643c
TT
21385 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21386 NULL, xcalloc, xfree);
96408a79 21387 make_cleanup_htab_delete (psyms_seen);
987d643c 21388
0a5429f6
DE
21389 /* While we're scanning CU's create a table that maps a psymtab pointer
21390 (which is what addrmap records) to its index (which is what is recorded
21391 in the index file). This will later be needed to write the address
21392 table. */
21393 cu_index_htab = htab_create_alloc (100,
21394 hash_psymtab_cu_index,
21395 eq_psymtab_cu_index,
21396 NULL, xcalloc, xfree);
96408a79 21397 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
21398 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21399 xmalloc (sizeof (struct psymtab_cu_index_map)
21400 * dwarf2_per_objfile->n_comp_units);
21401 make_cleanup (xfree, psymtab_cu_index_map);
21402
21403 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
21404 work here. Also, the debug_types entries do not appear in
21405 all_comp_units, but only in their own hash table. */
9291a0cd
TT
21406 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21407 {
3e43a32a
MS
21408 struct dwarf2_per_cu_data *per_cu
21409 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 21410 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 21411 gdb_byte val[8];
0a5429f6
DE
21412 struct psymtab_cu_index_map *map;
21413 void **slot;
9291a0cd 21414
92fac807
JK
21415 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21416 It may be referenced from a local scope but in such case it does not
21417 need to be present in .gdb_index. */
21418 if (psymtab == NULL)
21419 continue;
21420
95554aad
TT
21421 if (psymtab->user == NULL)
21422 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 21423
0a5429f6
DE
21424 map = &psymtab_cu_index_map[i];
21425 map->psymtab = psymtab;
21426 map->cu_index = i;
21427 slot = htab_find_slot (cu_index_htab, map, INSERT);
21428 gdb_assert (slot != NULL);
21429 gdb_assert (*slot == NULL);
21430 *slot = map;
9291a0cd 21431
b64f50a1
JK
21432 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21433 per_cu->offset.sect_off);
9291a0cd 21434 obstack_grow (&cu_list, val, 8);
e254ef6a 21435 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
21436 obstack_grow (&cu_list, val, 8);
21437 }
21438
0a5429f6
DE
21439 /* Dump the address map. */
21440 write_address_map (objfile, &addr_obstack, cu_index_htab);
21441
1fd400ff
TT
21442 /* Write out the .debug_type entries, if any. */
21443 if (dwarf2_per_objfile->signatured_types)
21444 {
21445 struct signatured_type_index_data sig_data;
21446
21447 sig_data.objfile = objfile;
21448 sig_data.symtab = symtab;
21449 sig_data.types_list = &types_cu_list;
987d643c 21450 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
21451 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21452 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21453 write_one_signatured_type, &sig_data);
21454 }
21455
156942c7
DE
21456 /* Now that we've processed all symbols we can shrink their cu_indices
21457 lists. */
21458 uniquify_cu_indices (symtab);
21459
9291a0cd
TT
21460 obstack_init (&constant_pool);
21461 make_cleanup_obstack_free (&constant_pool);
21462 obstack_init (&symtab_obstack);
21463 make_cleanup_obstack_free (&symtab_obstack);
21464 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21465
21466 obstack_init (&contents);
21467 make_cleanup_obstack_free (&contents);
1fd400ff 21468 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
21469 total_len = size_of_contents;
21470
21471 /* The version number. */
796a7ff8 21472 val = MAYBE_SWAP (8);
9291a0cd
TT
21473 obstack_grow (&contents, &val, sizeof (val));
21474
21475 /* The offset of the CU list from the start of the file. */
21476 val = MAYBE_SWAP (total_len);
21477 obstack_grow (&contents, &val, sizeof (val));
21478 total_len += obstack_object_size (&cu_list);
21479
1fd400ff
TT
21480 /* The offset of the types CU list from the start of the file. */
21481 val = MAYBE_SWAP (total_len);
21482 obstack_grow (&contents, &val, sizeof (val));
21483 total_len += obstack_object_size (&types_cu_list);
21484
9291a0cd
TT
21485 /* The offset of the address table from the start of the file. */
21486 val = MAYBE_SWAP (total_len);
21487 obstack_grow (&contents, &val, sizeof (val));
21488 total_len += obstack_object_size (&addr_obstack);
21489
21490 /* The offset of the symbol table from the start of the file. */
21491 val = MAYBE_SWAP (total_len);
21492 obstack_grow (&contents, &val, sizeof (val));
21493 total_len += obstack_object_size (&symtab_obstack);
21494
21495 /* The offset of the constant pool from the start of the file. */
21496 val = MAYBE_SWAP (total_len);
21497 obstack_grow (&contents, &val, sizeof (val));
21498 total_len += obstack_object_size (&constant_pool);
21499
21500 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21501
21502 write_obstack (out_file, &contents);
21503 write_obstack (out_file, &cu_list);
1fd400ff 21504 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
21505 write_obstack (out_file, &addr_obstack);
21506 write_obstack (out_file, &symtab_obstack);
21507 write_obstack (out_file, &constant_pool);
21508
21509 fclose (out_file);
21510
21511 /* We want to keep the file, so we set cleanup_filename to NULL
21512 here. See unlink_if_set. */
21513 cleanup_filename = NULL;
21514
21515 do_cleanups (cleanup);
21516}
21517
90476074
TT
21518/* Implementation of the `save gdb-index' command.
21519
21520 Note that the file format used by this command is documented in the
21521 GDB manual. Any changes here must be documented there. */
11570e71 21522
9291a0cd
TT
21523static void
21524save_gdb_index_command (char *arg, int from_tty)
21525{
21526 struct objfile *objfile;
21527
21528 if (!arg || !*arg)
96d19272 21529 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
21530
21531 ALL_OBJFILES (objfile)
21532 {
21533 struct stat st;
21534
21535 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 21536 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
21537 continue;
21538
21539 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21540 if (dwarf2_per_objfile)
21541 {
21542 volatile struct gdb_exception except;
21543
21544 TRY_CATCH (except, RETURN_MASK_ERROR)
21545 {
21546 write_psymtabs_to_index (objfile, arg);
21547 }
21548 if (except.reason < 0)
21549 exception_fprintf (gdb_stderr, except,
21550 _("Error while writing index for `%s': "),
4262abfb 21551 objfile_name (objfile));
9291a0cd
TT
21552 }
21553 }
dce234bc
PP
21554}
21555
9291a0cd
TT
21556\f
21557
9eae7c52
TT
21558int dwarf2_always_disassemble;
21559
21560static void
21561show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21562 struct cmd_list_element *c, const char *value)
21563{
3e43a32a
MS
21564 fprintf_filtered (file,
21565 _("Whether to always disassemble "
21566 "DWARF expressions is %s.\n"),
9eae7c52
TT
21567 value);
21568}
21569
900e11f9
JK
21570static void
21571show_check_physname (struct ui_file *file, int from_tty,
21572 struct cmd_list_element *c, const char *value)
21573{
21574 fprintf_filtered (file,
21575 _("Whether to check \"physname\" is %s.\n"),
21576 value);
21577}
21578
6502dd73
DJ
21579void _initialize_dwarf2_read (void);
21580
21581void
21582_initialize_dwarf2_read (void)
21583{
96d19272
JK
21584 struct cmd_list_element *c;
21585
dce234bc 21586 dwarf2_objfile_data_key
c1bd65d0 21587 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 21588
1bedd215
AC
21589 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21590Set DWARF 2 specific variables.\n\
21591Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21592 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21593 0/*allow-unknown*/, &maintenance_set_cmdlist);
21594
1bedd215
AC
21595 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21596Show DWARF 2 specific variables\n\
21597Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21598 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21599 0/*allow-unknown*/, &maintenance_show_cmdlist);
21600
21601 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
21602 &dwarf2_max_cache_age, _("\
21603Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21604Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21605A higher limit means that cached compilation units will be stored\n\
21606in memory longer, and more total memory will be used. Zero disables\n\
21607caching, which can slow down startup."),
2c5b56ce 21608 NULL,
920d2a44 21609 show_dwarf2_max_cache_age,
2c5b56ce 21610 &set_dwarf2_cmdlist,
ae038cb0 21611 &show_dwarf2_cmdlist);
d97bc12b 21612
9eae7c52
TT
21613 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21614 &dwarf2_always_disassemble, _("\
21615Set whether `info address' always disassembles DWARF expressions."), _("\
21616Show whether `info address' always disassembles DWARF expressions."), _("\
21617When enabled, DWARF expressions are always printed in an assembly-like\n\
21618syntax. When disabled, expressions will be printed in a more\n\
21619conversational style, when possible."),
21620 NULL,
21621 show_dwarf2_always_disassemble,
21622 &set_dwarf2_cmdlist,
21623 &show_dwarf2_cmdlist);
21624
45cfd468
DE
21625 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21626Set debugging of the dwarf2 reader."), _("\
21627Show debugging of the dwarf2 reader."), _("\
21628When enabled, debugging messages are printed during dwarf2 reading\n\
21629and symtab expansion."),
21630 NULL,
21631 NULL,
21632 &setdebuglist, &showdebuglist);
21633
ccce17b0 21634 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
21635Set debugging of the dwarf2 DIE reader."), _("\
21636Show debugging of the dwarf2 DIE reader."), _("\
21637When enabled (non-zero), DIEs are dumped after they are read in.\n\
21638The value is the maximum depth to print."),
ccce17b0
YQ
21639 NULL,
21640 NULL,
21641 &setdebuglist, &showdebuglist);
9291a0cd 21642
900e11f9
JK
21643 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21644Set cross-checking of \"physname\" code against demangler."), _("\
21645Show cross-checking of \"physname\" code against demangler."), _("\
21646When enabled, GDB's internal \"physname\" code is checked against\n\
21647the demangler."),
21648 NULL, show_check_physname,
21649 &setdebuglist, &showdebuglist);
21650
e615022a
DE
21651 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21652 no_class, &use_deprecated_index_sections, _("\
21653Set whether to use deprecated gdb_index sections."), _("\
21654Show whether to use deprecated gdb_index sections."), _("\
21655When enabled, deprecated .gdb_index sections are used anyway.\n\
21656Normally they are ignored either because of a missing feature or\n\
21657performance issue.\n\
21658Warning: This option must be enabled before gdb reads the file."),
21659 NULL,
21660 NULL,
21661 &setlist, &showlist);
21662
96d19272 21663 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21664 _("\
fc1a9d6e 21665Save a gdb-index file.\n\
11570e71 21666Usage: save gdb-index DIRECTORY"),
96d19272
JK
21667 &save_cmdlist);
21668 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21669
21670 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21671 &dwarf2_locexpr_funcs);
21672 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21673 &dwarf2_loclist_funcs);
21674
21675 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21676 &dwarf2_block_frame_base_locexpr_funcs);
21677 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21678 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21679}
This page took 4.35654 seconds and 4 git commands to generate.