tui: don't overwrite a secondary prompt that was given no input
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
32d0add0 3 Copyright (C) 1994-2015 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"
53ce3c39 58#include <sys/stat.h>
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5 61#include "c-lang.h"
a766d390 62#include "go-lang.h"
98bfdba5 63#include "valprint.h"
3019eac3 64#include "gdbcore.h" /* for gnutarget */
156942c7 65#include "gdb/gdb-index.h"
60d5a603 66#include <ctype.h>
cbb099e8 67#include "gdb_bfd.h"
4357ac6c 68#include "f-lang.h"
05cba821 69#include "source.h"
614c279d 70#include "filestuff.h"
dc294be5 71#include "build-id.h"
22cee43f 72#include "namespace.h"
4c2df51b 73
c906108c 74#include <fcntl.h>
c906108c 75#include <sys/types.h>
d8151005 76
34eaf542
TT
77typedef struct symbol *symbolp;
78DEF_VEC_P (symbolp);
79
73be47f5
DE
80/* When == 1, print basic high level tracing messages.
81 When > 1, be more verbose.
b4f54984
DE
82 This is in contrast to the low level DIE reading of dwarf_die_debug. */
83static unsigned int dwarf_read_debug = 0;
45cfd468 84
d97bc12b 85/* When non-zero, dump DIEs after they are read in. */
b4f54984 86static unsigned int dwarf_die_debug = 0;
d97bc12b 87
27e0867f
DE
88/* When non-zero, dump line number entries as they are read in. */
89static unsigned int dwarf_line_debug = 0;
90
900e11f9
JK
91/* When non-zero, cross-check physname against demangler. */
92static int check_physname = 0;
93
481860b3 94/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 95static int use_deprecated_index_sections = 0;
481860b3 96
6502dd73
DJ
97static const struct objfile_data *dwarf2_objfile_data_key;
98
f1e6e072
TT
99/* The "aclass" indices for various kinds of computed DWARF symbols. */
100
101static int dwarf2_locexpr_index;
102static int dwarf2_loclist_index;
103static int dwarf2_locexpr_block_index;
104static int dwarf2_loclist_block_index;
105
73869dc2
DE
106/* A descriptor for dwarf sections.
107
108 S.ASECTION, SIZE are typically initialized when the objfile is first
109 scanned. BUFFER, READIN are filled in later when the section is read.
110 If the section contained compressed data then SIZE is updated to record
111 the uncompressed size of the section.
112
113 DWP file format V2 introduces a wrinkle that is easiest to handle by
114 creating the concept of virtual sections contained within a real section.
115 In DWP V2 the sections of the input DWO files are concatenated together
116 into one section, but section offsets are kept relative to the original
117 input section.
118 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
119 the real section this "virtual" section is contained in, and BUFFER,SIZE
120 describe the virtual section. */
121
dce234bc
PP
122struct dwarf2_section_info
123{
73869dc2
DE
124 union
125 {
e5aa3347 126 /* If this is a real section, the bfd section. */
73869dc2
DE
127 asection *asection;
128 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 129 section. */
73869dc2
DE
130 struct dwarf2_section_info *containing_section;
131 } s;
19ac8c2e 132 /* Pointer to section data, only valid if readin. */
d521ce57 133 const gdb_byte *buffer;
73869dc2 134 /* The size of the section, real or virtual. */
dce234bc 135 bfd_size_type size;
73869dc2
DE
136 /* If this is a virtual section, the offset in the real section.
137 Only valid if is_virtual. */
138 bfd_size_type virtual_offset;
be391dca 139 /* True if we have tried to read this section. */
73869dc2
DE
140 char readin;
141 /* True if this is a virtual section, False otherwise.
142 This specifies which of s.asection and s.containing_section to use. */
143 char is_virtual;
dce234bc
PP
144};
145
8b70b953
TT
146typedef struct dwarf2_section_info dwarf2_section_info_def;
147DEF_VEC_O (dwarf2_section_info_def);
148
9291a0cd
TT
149/* All offsets in the index are of this type. It must be
150 architecture-independent. */
151typedef uint32_t offset_type;
152
153DEF_VEC_I (offset_type);
154
156942c7
DE
155/* Ensure only legit values are used. */
156#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
157 do { \
158 gdb_assert ((unsigned int) (value) <= 1); \
159 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
160 } while (0)
161
162/* Ensure only legit values are used. */
163#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
164 do { \
165 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
166 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
167 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
168 } while (0)
169
170/* Ensure we don't use more than the alloted nuber of bits for the CU. */
171#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
172 do { \
173 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
174 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
175 } while (0)
176
9291a0cd
TT
177/* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
179struct mapped_index
180{
559a7a62
JK
181 /* Index data format version. */
182 int version;
183
9291a0cd
TT
184 /* The total length of the buffer. */
185 off_t total_size;
b11b1f88 186
9291a0cd
TT
187 /* A pointer to the address table data. */
188 const gdb_byte *address_table;
b11b1f88 189
9291a0cd
TT
190 /* Size of the address table data in bytes. */
191 offset_type address_table_size;
b11b1f88 192
3876f04e
DE
193 /* The symbol table, implemented as a hash table. */
194 const offset_type *symbol_table;
b11b1f88 195
9291a0cd 196 /* Size in slots, each slot is 2 offset_types. */
3876f04e 197 offset_type symbol_table_slots;
b11b1f88 198
9291a0cd
TT
199 /* A pointer to the constant pool. */
200 const char *constant_pool;
201};
202
95554aad
TT
203typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
204DEF_VEC_P (dwarf2_per_cu_ptr);
205
52059ffd
TT
206struct tu_stats
207{
208 int nr_uniq_abbrev_tables;
209 int nr_symtabs;
210 int nr_symtab_sharers;
211 int nr_stmt_less_type_units;
212 int nr_all_type_units_reallocs;
213};
214
9cdd5dbd
DE
215/* Collection of data recorded per objfile.
216 This hangs off of dwarf2_objfile_data_key. */
217
6502dd73
DJ
218struct dwarf2_per_objfile
219{
dce234bc
PP
220 struct dwarf2_section_info info;
221 struct dwarf2_section_info abbrev;
222 struct dwarf2_section_info line;
dce234bc
PP
223 struct dwarf2_section_info loc;
224 struct dwarf2_section_info macinfo;
cf2c3c16 225 struct dwarf2_section_info macro;
dce234bc
PP
226 struct dwarf2_section_info str;
227 struct dwarf2_section_info ranges;
3019eac3 228 struct dwarf2_section_info addr;
dce234bc
PP
229 struct dwarf2_section_info frame;
230 struct dwarf2_section_info eh_frame;
9291a0cd 231 struct dwarf2_section_info gdb_index;
ae038cb0 232
8b70b953
TT
233 VEC (dwarf2_section_info_def) *types;
234
be391dca
TT
235 /* Back link. */
236 struct objfile *objfile;
237
d467dd73 238 /* Table of all the compilation units. This is used to locate
10b3939b 239 the target compilation unit of a particular reference. */
ae038cb0
DJ
240 struct dwarf2_per_cu_data **all_comp_units;
241
242 /* The number of compilation units in ALL_COMP_UNITS. */
243 int n_comp_units;
244
1fd400ff 245 /* The number of .debug_types-related CUs. */
d467dd73 246 int n_type_units;
1fd400ff 247
6aa5f3a6
DE
248 /* The number of elements allocated in all_type_units.
249 If there are skeleton-less TUs, we add them to all_type_units lazily. */
250 int n_allocated_type_units;
251
a2ce51a0
DE
252 /* The .debug_types-related CUs (TUs).
253 This is stored in malloc space because we may realloc it. */
b4dd5633 254 struct signatured_type **all_type_units;
1fd400ff 255
f4dc4d17
DE
256 /* Table of struct type_unit_group objects.
257 The hash key is the DW_AT_stmt_list value. */
258 htab_t type_unit_groups;
72dca2f5 259
348e048f
DE
260 /* A table mapping .debug_types signatures to its signatured_type entry.
261 This is NULL if the .debug_types section hasn't been read in yet. */
262 htab_t signatured_types;
263
f4dc4d17
DE
264 /* Type unit statistics, to see how well the scaling improvements
265 are doing. */
52059ffd 266 struct tu_stats tu_stats;
f4dc4d17
DE
267
268 /* A chain of compilation units that are currently read in, so that
269 they can be freed later. */
270 struct dwarf2_per_cu_data *read_in_chain;
271
3019eac3
DE
272 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
273 This is NULL if the table hasn't been allocated yet. */
274 htab_t dwo_files;
275
80626a55
DE
276 /* Non-zero if we've check for whether there is a DWP file. */
277 int dwp_checked;
278
279 /* The DWP file if there is one, or NULL. */
280 struct dwp_file *dwp_file;
281
36586728
TT
282 /* The shared '.dwz' file, if one exists. This is used when the
283 original data was compressed using 'dwz -m'. */
284 struct dwz_file *dwz_file;
285
72dca2f5
FR
286 /* A flag indicating wether this objfile has a section loaded at a
287 VMA of 0. */
288 int has_section_at_zero;
9291a0cd 289
ae2de4f8
DE
290 /* True if we are using the mapped index,
291 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
292 unsigned char using_index;
293
ae2de4f8 294 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 295 struct mapped_index *index_table;
98bfdba5 296
7b9f3c50 297 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
298 TUs typically share line table entries with a CU, so we maintain a
299 separate table of all line table entries to support the sharing.
300 Note that while there can be way more TUs than CUs, we've already
301 sorted all the TUs into "type unit groups", grouped by their
302 DW_AT_stmt_list value. Therefore the only sharing done here is with a
303 CU and its associated TU group if there is one. */
7b9f3c50
DE
304 htab_t quick_file_names_table;
305
98bfdba5
PA
306 /* Set during partial symbol reading, to prevent queueing of full
307 symbols. */
308 int reading_partial_symbols;
673bfd45 309
dee91e82 310 /* Table mapping type DIEs to their struct type *.
673bfd45 311 This is NULL if not allocated yet.
02142a6c 312 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 313 htab_t die_type_hash;
95554aad
TT
314
315 /* The CUs we recently read. */
316 VEC (dwarf2_per_cu_ptr) *just_read_cus;
527f3840
JK
317
318 /* Table containing line_header indexed by offset and offset_in_dwz. */
319 htab_t line_header_hash;
6502dd73
DJ
320};
321
322static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 323
251d32d9 324/* Default names of the debugging sections. */
c906108c 325
233a11ab
CS
326/* Note that if the debugging section has been compressed, it might
327 have a name like .zdebug_info. */
328
9cdd5dbd
DE
329static const struct dwarf2_debug_sections dwarf2_elf_names =
330{
251d32d9
TG
331 { ".debug_info", ".zdebug_info" },
332 { ".debug_abbrev", ".zdebug_abbrev" },
333 { ".debug_line", ".zdebug_line" },
334 { ".debug_loc", ".zdebug_loc" },
335 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 336 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
337 { ".debug_str", ".zdebug_str" },
338 { ".debug_ranges", ".zdebug_ranges" },
339 { ".debug_types", ".zdebug_types" },
3019eac3 340 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
341 { ".debug_frame", ".zdebug_frame" },
342 { ".eh_frame", NULL },
24d3216f
TT
343 { ".gdb_index", ".zgdb_index" },
344 23
251d32d9 345};
c906108c 346
80626a55 347/* List of DWO/DWP sections. */
3019eac3 348
80626a55 349static const struct dwop_section_names
3019eac3
DE
350{
351 struct dwarf2_section_names abbrev_dwo;
352 struct dwarf2_section_names info_dwo;
353 struct dwarf2_section_names line_dwo;
354 struct dwarf2_section_names loc_dwo;
09262596
DE
355 struct dwarf2_section_names macinfo_dwo;
356 struct dwarf2_section_names macro_dwo;
3019eac3
DE
357 struct dwarf2_section_names str_dwo;
358 struct dwarf2_section_names str_offsets_dwo;
359 struct dwarf2_section_names types_dwo;
80626a55
DE
360 struct dwarf2_section_names cu_index;
361 struct dwarf2_section_names tu_index;
3019eac3 362}
80626a55 363dwop_section_names =
3019eac3
DE
364{
365 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
366 { ".debug_info.dwo", ".zdebug_info.dwo" },
367 { ".debug_line.dwo", ".zdebug_line.dwo" },
368 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
369 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
370 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
371 { ".debug_str.dwo", ".zdebug_str.dwo" },
372 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
373 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
374 { ".debug_cu_index", ".zdebug_cu_index" },
375 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
376};
377
c906108c
SS
378/* local data types */
379
107d2387
AC
380/* The data in a compilation unit header, after target2host
381 translation, looks like this. */
c906108c 382struct comp_unit_head
a738430d 383{
c764a876 384 unsigned int length;
a738430d 385 short version;
a738430d
MK
386 unsigned char addr_size;
387 unsigned char signed_addr_p;
b64f50a1 388 sect_offset abbrev_offset;
57349743 389
a738430d
MK
390 /* Size of file offsets; either 4 or 8. */
391 unsigned int offset_size;
57349743 392
a738430d
MK
393 /* Size of the length field; either 4 or 12. */
394 unsigned int initial_length_size;
57349743 395
a738430d
MK
396 /* Offset to the first byte of this compilation unit header in the
397 .debug_info section, for resolving relative reference dies. */
b64f50a1 398 sect_offset offset;
57349743 399
d00adf39
DE
400 /* Offset to first die in this cu from the start of the cu.
401 This will be the first byte following the compilation unit header. */
b64f50a1 402 cu_offset first_die_offset;
a738430d 403};
c906108c 404
3da10d80
KS
405/* Type used for delaying computation of method physnames.
406 See comments for compute_delayed_physnames. */
407struct delayed_method_info
408{
409 /* The type to which the method is attached, i.e., its parent class. */
410 struct type *type;
411
412 /* The index of the method in the type's function fieldlists. */
413 int fnfield_index;
414
415 /* The index of the method in the fieldlist. */
416 int index;
417
418 /* The name of the DIE. */
419 const char *name;
420
421 /* The DIE associated with this method. */
422 struct die_info *die;
423};
424
425typedef struct delayed_method_info delayed_method_info;
426DEF_VEC_O (delayed_method_info);
427
e7c27a73
DJ
428/* Internal state when decoding a particular compilation unit. */
429struct dwarf2_cu
430{
431 /* The objfile containing this compilation unit. */
432 struct objfile *objfile;
433
d00adf39 434 /* The header of the compilation unit. */
e7c27a73 435 struct comp_unit_head header;
e142c38c 436
d00adf39
DE
437 /* Base address of this compilation unit. */
438 CORE_ADDR base_address;
439
440 /* Non-zero if base_address has been set. */
441 int base_known;
442
e142c38c
DJ
443 /* The language we are debugging. */
444 enum language language;
445 const struct language_defn *language_defn;
446
b0f35d58
DL
447 const char *producer;
448
e142c38c
DJ
449 /* The generic symbol table building routines have separate lists for
450 file scope symbols and all all other scopes (local scopes). So
451 we need to select the right one to pass to add_symbol_to_list().
452 We do it by keeping a pointer to the correct list in list_in_scope.
453
454 FIXME: The original dwarf code just treated the file scope as the
455 first local scope, and all other local scopes as nested local
456 scopes, and worked fine. Check to see if we really need to
457 distinguish these in buildsym.c. */
458 struct pending **list_in_scope;
459
433df2d4
DE
460 /* The abbrev table for this CU.
461 Normally this points to the abbrev table in the objfile.
462 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
463 struct abbrev_table *abbrev_table;
72bf9492 464
b64f50a1
JK
465 /* Hash table holding all the loaded partial DIEs
466 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
467 htab_t partial_dies;
468
469 /* Storage for things with the same lifetime as this read-in compilation
470 unit, including partial DIEs. */
471 struct obstack comp_unit_obstack;
472
ae038cb0
DJ
473 /* When multiple dwarf2_cu structures are living in memory, this field
474 chains them all together, so that they can be released efficiently.
475 We will probably also want a generation counter so that most-recently-used
476 compilation units are cached... */
477 struct dwarf2_per_cu_data *read_in_chain;
478
69d751e3 479 /* Backlink to our per_cu entry. */
ae038cb0
DJ
480 struct dwarf2_per_cu_data *per_cu;
481
482 /* How many compilation units ago was this CU last referenced? */
483 int last_used;
484
b64f50a1
JK
485 /* A hash table of DIE cu_offset for following references with
486 die_info->offset.sect_off as hash. */
51545339 487 htab_t die_hash;
10b3939b
DJ
488
489 /* Full DIEs if read in. */
490 struct die_info *dies;
491
492 /* A set of pointers to dwarf2_per_cu_data objects for compilation
493 units referenced by this one. Only set during full symbol processing;
494 partial symbol tables do not have dependencies. */
495 htab_t dependencies;
496
cb1df416
DJ
497 /* Header data from the line table, during full symbol processing. */
498 struct line_header *line_header;
499
3da10d80
KS
500 /* A list of methods which need to have physnames computed
501 after all type information has been read. */
502 VEC (delayed_method_info) *method_list;
503
96408a79
SA
504 /* To be copied to symtab->call_site_htab. */
505 htab_t call_site_htab;
506
034e5797
DE
507 /* Non-NULL if this CU came from a DWO file.
508 There is an invariant here that is important to remember:
509 Except for attributes copied from the top level DIE in the "main"
510 (or "stub") file in preparation for reading the DWO file
511 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
512 Either there isn't a DWO file (in which case this is NULL and the point
513 is moot), or there is and either we're not going to read it (in which
514 case this is NULL) or there is and we are reading it (in which case this
515 is non-NULL). */
3019eac3
DE
516 struct dwo_unit *dwo_unit;
517
518 /* The DW_AT_addr_base attribute if present, zero otherwise
519 (zero is a valid value though).
1dbab08b 520 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
521 ULONGEST addr_base;
522
2e3cf129
DE
523 /* The DW_AT_ranges_base attribute if present, zero otherwise
524 (zero is a valid value though).
1dbab08b 525 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 526 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
527 be used without needing to know whether DWO files are in use or not.
528 N.B. This does not apply to DW_AT_ranges appearing in
529 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
530 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
531 DW_AT_ranges_base *would* have to be applied, and we'd have to care
532 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
533 ULONGEST ranges_base;
534
ae038cb0
DJ
535 /* Mark used when releasing cached dies. */
536 unsigned int mark : 1;
537
8be455d7
JK
538 /* This CU references .debug_loc. See the symtab->locations_valid field.
539 This test is imperfect as there may exist optimized debug code not using
540 any location list and still facing inlining issues if handled as
541 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 542 unsigned int has_loclist : 1;
ba919b58 543
1b80a9fa
JK
544 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
545 if all the producer_is_* fields are valid. This information is cached
546 because profiling CU expansion showed excessive time spent in
547 producer_is_gxx_lt_4_6. */
ba919b58
TT
548 unsigned int checked_producer : 1;
549 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 550 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 551 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
552
553 /* When set, the file that we're processing is known to have
554 debugging info for C++ namespaces. GCC 3.3.x did not produce
555 this information, but later versions do. */
556
557 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
558};
559
10b3939b
DJ
560/* Persistent data held for a compilation unit, even when not
561 processing it. We put a pointer to this structure in the
28dee7f5 562 read_symtab_private field of the psymtab. */
10b3939b 563
ae038cb0
DJ
564struct dwarf2_per_cu_data
565{
36586728 566 /* The start offset and length of this compilation unit.
45452591 567 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
568 initial_length_size.
569 If the DIE refers to a DWO file, this is always of the original die,
570 not the DWO file. */
b64f50a1 571 sect_offset offset;
36586728 572 unsigned int length;
ae038cb0
DJ
573
574 /* Flag indicating this compilation unit will be read in before
575 any of the current compilation units are processed. */
c764a876 576 unsigned int queued : 1;
ae038cb0 577
0d99eb77
DE
578 /* This flag will be set when reading partial DIEs if we need to load
579 absolutely all DIEs for this compilation unit, instead of just the ones
580 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
581 hash table and don't find it. */
582 unsigned int load_all_dies : 1;
583
0186c6a7
DE
584 /* Non-zero if this CU is from .debug_types.
585 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
586 this is non-zero. */
3019eac3
DE
587 unsigned int is_debug_types : 1;
588
36586728
TT
589 /* Non-zero if this CU is from the .dwz file. */
590 unsigned int is_dwz : 1;
591
a2ce51a0
DE
592 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
593 This flag is only valid if is_debug_types is true.
594 We can't read a CU directly from a DWO file: There are required
595 attributes in the stub. */
596 unsigned int reading_dwo_directly : 1;
597
7ee85ab1
DE
598 /* Non-zero if the TU has been read.
599 This is used to assist the "Stay in DWO Optimization" for Fission:
600 When reading a DWO, it's faster to read TUs from the DWO instead of
601 fetching them from random other DWOs (due to comdat folding).
602 If the TU has already been read, the optimization is unnecessary
603 (and unwise - we don't want to change where gdb thinks the TU lives
604 "midflight").
605 This flag is only valid if is_debug_types is true. */
606 unsigned int tu_read : 1;
607
3019eac3
DE
608 /* The section this CU/TU lives in.
609 If the DIE refers to a DWO file, this is always the original die,
610 not the DWO file. */
8a0459fd 611 struct dwarf2_section_info *section;
348e048f 612
17ea53c3 613 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
614 of the CU cache it gets reset to NULL again. This is left as NULL for
615 dummy CUs (a CU header, but nothing else). */
ae038cb0 616 struct dwarf2_cu *cu;
1c379e20 617
9cdd5dbd
DE
618 /* The corresponding objfile.
619 Normally we can get the objfile from dwarf2_per_objfile.
620 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
621 struct objfile *objfile;
622
fffbe6a8
YQ
623 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
624 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
625 union
626 {
627 /* The partial symbol table associated with this compilation unit,
95554aad 628 or NULL for unread partial units. */
9291a0cd
TT
629 struct partial_symtab *psymtab;
630
631 /* Data needed by the "quick" functions. */
632 struct dwarf2_per_cu_quick_data *quick;
633 } v;
95554aad 634
796a7ff8
DE
635 /* The CUs we import using DW_TAG_imported_unit. This is filled in
636 while reading psymtabs, used to compute the psymtab dependencies,
637 and then cleared. Then it is filled in again while reading full
638 symbols, and only deleted when the objfile is destroyed.
639
640 This is also used to work around a difference between the way gold
641 generates .gdb_index version <=7 and the way gdb does. Arguably this
642 is a gold bug. For symbols coming from TUs, gold records in the index
643 the CU that includes the TU instead of the TU itself. This breaks
644 dw2_lookup_symbol: It assumes that if the index says symbol X lives
645 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
646 will find X. Alas TUs live in their own symtab, so after expanding CU Y
647 we need to look in TU Z to find X. Fortunately, this is akin to
648 DW_TAG_imported_unit, so we just use the same mechanism: For
649 .gdb_index version <=7 this also records the TUs that the CU referred
650 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
651 indices so we only pay a price for gold generated indices.
652 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 653 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
654};
655
348e048f
DE
656/* Entry in the signatured_types hash table. */
657
658struct signatured_type
659{
42e7ad6c 660 /* The "per_cu" object of this type.
ac9ec31b 661 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
662 N.B.: This is the first member so that it's easy to convert pointers
663 between them. */
664 struct dwarf2_per_cu_data per_cu;
665
3019eac3 666 /* The type's signature. */
348e048f
DE
667 ULONGEST signature;
668
3019eac3 669 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
670 If this TU is a DWO stub and the definition lives in a DWO file
671 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
672 cu_offset type_offset_in_tu;
673
674 /* Offset in the section of the type's DIE.
675 If the definition lives in a DWO file, this is the offset in the
676 .debug_types.dwo section.
677 The value is zero until the actual value is known.
678 Zero is otherwise not a valid section offset. */
679 sect_offset type_offset_in_section;
0186c6a7
DE
680
681 /* Type units are grouped by their DW_AT_stmt_list entry so that they
682 can share them. This points to the containing symtab. */
683 struct type_unit_group *type_unit_group;
ac9ec31b
DE
684
685 /* The type.
686 The first time we encounter this type we fully read it in and install it
687 in the symbol tables. Subsequent times we only need the type. */
688 struct type *type;
a2ce51a0
DE
689
690 /* Containing DWO unit.
691 This field is valid iff per_cu.reading_dwo_directly. */
692 struct dwo_unit *dwo_unit;
348e048f
DE
693};
694
0186c6a7
DE
695typedef struct signatured_type *sig_type_ptr;
696DEF_VEC_P (sig_type_ptr);
697
094b34ac
DE
698/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
699 This includes type_unit_group and quick_file_names. */
700
701struct stmt_list_hash
702{
703 /* The DWO unit this table is from or NULL if there is none. */
704 struct dwo_unit *dwo_unit;
705
706 /* Offset in .debug_line or .debug_line.dwo. */
707 sect_offset line_offset;
708};
709
f4dc4d17
DE
710/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
711 an object of this type. */
712
713struct type_unit_group
714{
0186c6a7 715 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
716 To simplify things we create an artificial CU that "includes" all the
717 type units using this stmt_list so that the rest of the code still has
718 a "per_cu" handle on the symtab.
719 This PER_CU is recognized by having no section. */
8a0459fd 720#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
721 struct dwarf2_per_cu_data per_cu;
722
0186c6a7
DE
723 /* The TUs that share this DW_AT_stmt_list entry.
724 This is added to while parsing type units to build partial symtabs,
725 and is deleted afterwards and not used again. */
726 VEC (sig_type_ptr) *tus;
f4dc4d17 727
43f3e411 728 /* The compunit symtab.
094b34ac 729 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
730 so we create an essentially anonymous symtab as the compunit symtab. */
731 struct compunit_symtab *compunit_symtab;
f4dc4d17 732
094b34ac
DE
733 /* The data used to construct the hash key. */
734 struct stmt_list_hash hash;
f4dc4d17
DE
735
736 /* The number of symtabs from the line header.
737 The value here must match line_header.num_file_names. */
738 unsigned int num_symtabs;
739
740 /* The symbol tables for this TU (obtained from the files listed in
741 DW_AT_stmt_list).
742 WARNING: The order of entries here must match the order of entries
743 in the line header. After the first TU using this type_unit_group, the
744 line header for the subsequent TUs is recreated from this. This is done
745 because we need to use the same symtabs for each TU using the same
746 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
747 there's no guarantee the line header doesn't have duplicate entries. */
748 struct symtab **symtabs;
749};
750
73869dc2 751/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
752
753struct dwo_sections
754{
755 struct dwarf2_section_info abbrev;
3019eac3
DE
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
09262596
DE
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
3019eac3
DE
760 struct dwarf2_section_info str;
761 struct dwarf2_section_info str_offsets;
80626a55
DE
762 /* In the case of a virtual DWO file, these two are unused. */
763 struct dwarf2_section_info info;
3019eac3
DE
764 VEC (dwarf2_section_info_def) *types;
765};
766
c88ee1f0 767/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
768
769struct dwo_unit
770{
771 /* Backlink to the containing struct dwo_file. */
772 struct dwo_file *dwo_file;
773
774 /* The "id" that distinguishes this CU/TU.
775 .debug_info calls this "dwo_id", .debug_types calls this "signature".
776 Since signatures came first, we stick with it for consistency. */
777 ULONGEST signature;
778
779 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 780 struct dwarf2_section_info *section;
3019eac3 781
19ac8c2e 782 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
3019eac3
DE
783 sect_offset offset;
784 unsigned int length;
785
786 /* For types, offset in the type's DIE of the type defined by this TU. */
787 cu_offset type_offset_in_tu;
788};
789
73869dc2
DE
790/* include/dwarf2.h defines the DWP section codes.
791 It defines a max value but it doesn't define a min value, which we
792 use for error checking, so provide one. */
793
794enum dwp_v2_section_ids
795{
796 DW_SECT_MIN = 1
797};
798
80626a55 799/* Data for one DWO file.
57d63ce2
DE
800
801 This includes virtual DWO files (a virtual DWO file is a DWO file as it
802 appears in a DWP file). DWP files don't really have DWO files per se -
803 comdat folding of types "loses" the DWO file they came from, and from
804 a high level view DWP files appear to contain a mass of random types.
805 However, to maintain consistency with the non-DWP case we pretend DWP
806 files contain virtual DWO files, and we assign each TU with one virtual
807 DWO file (generally based on the line and abbrev section offsets -
808 a heuristic that seems to work in practice). */
3019eac3
DE
809
810struct dwo_file
811{
0ac5b59e 812 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
813 For virtual DWO files the name is constructed from the section offsets
814 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
815 from related CU+TUs. */
0ac5b59e
DE
816 const char *dwo_name;
817
818 /* The DW_AT_comp_dir attribute. */
819 const char *comp_dir;
3019eac3 820
80626a55
DE
821 /* The bfd, when the file is open. Otherwise this is NULL.
822 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
823 bfd *dbfd;
3019eac3 824
73869dc2
DE
825 /* The sections that make up this DWO file.
826 Remember that for virtual DWO files in DWP V2, these are virtual
827 sections (for lack of a better name). */
3019eac3
DE
828 struct dwo_sections sections;
829
19c3d4c9
DE
830 /* The CU in the file.
831 We only support one because having more than one requires hacking the
832 dwo_name of each to match, which is highly unlikely to happen.
833 Doing this means all TUs can share comp_dir: We also assume that
834 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
835 struct dwo_unit *cu;
3019eac3
DE
836
837 /* Table of TUs in the file.
838 Each element is a struct dwo_unit. */
839 htab_t tus;
840};
841
80626a55
DE
842/* These sections are what may appear in a DWP file. */
843
844struct dwp_sections
845{
73869dc2 846 /* These are used by both DWP version 1 and 2. */
80626a55
DE
847 struct dwarf2_section_info str;
848 struct dwarf2_section_info cu_index;
849 struct dwarf2_section_info tu_index;
73869dc2
DE
850
851 /* These are only used by DWP version 2 files.
852 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
853 sections are referenced by section number, and are not recorded here.
854 In DWP version 2 there is at most one copy of all these sections, each
855 section being (effectively) comprised of the concatenation of all of the
856 individual sections that exist in the version 1 format.
857 To keep the code simple we treat each of these concatenated pieces as a
858 section itself (a virtual section?). */
859 struct dwarf2_section_info abbrev;
860 struct dwarf2_section_info info;
861 struct dwarf2_section_info line;
862 struct dwarf2_section_info loc;
863 struct dwarf2_section_info macinfo;
864 struct dwarf2_section_info macro;
865 struct dwarf2_section_info str_offsets;
866 struct dwarf2_section_info types;
80626a55
DE
867};
868
73869dc2
DE
869/* These sections are what may appear in a virtual DWO file in DWP version 1.
870 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 871
73869dc2 872struct virtual_v1_dwo_sections
80626a55
DE
873{
874 struct dwarf2_section_info abbrev;
875 struct dwarf2_section_info line;
876 struct dwarf2_section_info loc;
877 struct dwarf2_section_info macinfo;
878 struct dwarf2_section_info macro;
879 struct dwarf2_section_info str_offsets;
880 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 881 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
882 struct dwarf2_section_info info_or_types;
883};
884
73869dc2
DE
885/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
886 In version 2, the sections of the DWO files are concatenated together
887 and stored in one section of that name. Thus each ELF section contains
888 several "virtual" sections. */
889
890struct virtual_v2_dwo_sections
891{
892 bfd_size_type abbrev_offset;
893 bfd_size_type abbrev_size;
894
895 bfd_size_type line_offset;
896 bfd_size_type line_size;
897
898 bfd_size_type loc_offset;
899 bfd_size_type loc_size;
900
901 bfd_size_type macinfo_offset;
902 bfd_size_type macinfo_size;
903
904 bfd_size_type macro_offset;
905 bfd_size_type macro_size;
906
907 bfd_size_type str_offsets_offset;
908 bfd_size_type str_offsets_size;
909
910 /* Each DWP hash table entry records one CU or one TU.
911 That is recorded here, and copied to dwo_unit.section. */
912 bfd_size_type info_or_types_offset;
913 bfd_size_type info_or_types_size;
914};
915
80626a55
DE
916/* Contents of DWP hash tables. */
917
918struct dwp_hash_table
919{
73869dc2 920 uint32_t version, nr_columns;
80626a55 921 uint32_t nr_units, nr_slots;
73869dc2
DE
922 const gdb_byte *hash_table, *unit_table;
923 union
924 {
925 struct
926 {
927 const gdb_byte *indices;
928 } v1;
929 struct
930 {
931 /* This is indexed by column number and gives the id of the section
932 in that column. */
933#define MAX_NR_V2_DWO_SECTIONS \
934 (1 /* .debug_info or .debug_types */ \
935 + 1 /* .debug_abbrev */ \
936 + 1 /* .debug_line */ \
937 + 1 /* .debug_loc */ \
938 + 1 /* .debug_str_offsets */ \
939 + 1 /* .debug_macro or .debug_macinfo */)
940 int section_ids[MAX_NR_V2_DWO_SECTIONS];
941 const gdb_byte *offsets;
942 const gdb_byte *sizes;
943 } v2;
944 } section_pool;
80626a55
DE
945};
946
947/* Data for one DWP file. */
948
949struct dwp_file
950{
951 /* Name of the file. */
952 const char *name;
953
73869dc2
DE
954 /* File format version. */
955 int version;
956
93417882 957 /* The bfd. */
80626a55
DE
958 bfd *dbfd;
959
960 /* Section info for this file. */
961 struct dwp_sections sections;
962
57d63ce2 963 /* Table of CUs in the file. */
80626a55
DE
964 const struct dwp_hash_table *cus;
965
966 /* Table of TUs in the file. */
967 const struct dwp_hash_table *tus;
968
19ac8c2e
DE
969 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
970 htab_t loaded_cus;
971 htab_t loaded_tus;
80626a55 972
73869dc2
DE
973 /* Table to map ELF section numbers to their sections.
974 This is only needed for the DWP V1 file format. */
80626a55
DE
975 unsigned int num_sections;
976 asection **elf_sections;
977};
978
36586728
TT
979/* This represents a '.dwz' file. */
980
981struct dwz_file
982{
983 /* A dwz file can only contain a few sections. */
984 struct dwarf2_section_info abbrev;
985 struct dwarf2_section_info info;
986 struct dwarf2_section_info str;
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info macro;
2ec9a5e0 989 struct dwarf2_section_info gdb_index;
36586728
TT
990
991 /* The dwz's BFD. */
992 bfd *dwz_bfd;
993};
994
0963b4bd
MS
995/* Struct used to pass misc. parameters to read_die_and_children, et
996 al. which are used for both .debug_info and .debug_types dies.
997 All parameters here are unchanging for the life of the call. This
dee91e82 998 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
999
1000struct die_reader_specs
1001{
a32a8923 1002 /* The bfd of die_section. */
93311388
DE
1003 bfd* abfd;
1004
1005 /* The CU of the DIE we are parsing. */
1006 struct dwarf2_cu *cu;
1007
80626a55 1008 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1009 struct dwo_file *dwo_file;
1010
dee91e82 1011 /* The section the die comes from.
3019eac3 1012 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1013 struct dwarf2_section_info *die_section;
1014
1015 /* die_section->buffer. */
d521ce57 1016 const gdb_byte *buffer;
f664829e
DE
1017
1018 /* The end of the buffer. */
1019 const gdb_byte *buffer_end;
a2ce51a0
DE
1020
1021 /* The value of the DW_AT_comp_dir attribute. */
1022 const char *comp_dir;
93311388
DE
1023};
1024
fd820528 1025/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1026typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1027 const gdb_byte *info_ptr,
dee91e82
DE
1028 struct die_info *comp_unit_die,
1029 int has_children,
1030 void *data);
1031
52059ffd
TT
1032struct file_entry
1033{
1034 const char *name;
1035 unsigned int dir_index;
1036 unsigned int mod_time;
1037 unsigned int length;
83769d0b
DE
1038 /* Non-zero if referenced by the Line Number Program. */
1039 int included_p;
1040 /* The associated symbol table, if any. */
1041 struct symtab *symtab;
52059ffd
TT
1042};
1043
debd256d
JB
1044/* The line number information for a compilation unit (found in the
1045 .debug_line section) begins with a "statement program header",
1046 which contains the following information. */
1047struct line_header
1048{
527f3840
JK
1049 /* Offset of line number information in .debug_line section. */
1050 sect_offset offset;
1051
1052 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1053 unsigned offset_in_dwz : 1;
1054
debd256d
JB
1055 unsigned int total_length;
1056 unsigned short version;
1057 unsigned int header_length;
1058 unsigned char minimum_instruction_length;
2dc7f7b3 1059 unsigned char maximum_ops_per_instruction;
debd256d
JB
1060 unsigned char default_is_stmt;
1061 int line_base;
1062 unsigned char line_range;
1063 unsigned char opcode_base;
1064
1065 /* standard_opcode_lengths[i] is the number of operands for the
1066 standard opcode whose value is i. This means that
1067 standard_opcode_lengths[0] is unused, and the last meaningful
1068 element is standard_opcode_lengths[opcode_base - 1]. */
1069 unsigned char *standard_opcode_lengths;
1070
1071 /* The include_directories table. NOTE! These strings are not
1072 allocated with xmalloc; instead, they are pointers into
1073 debug_line_buffer. If you try to free them, `free' will get
1074 indigestion. */
1075 unsigned int num_include_dirs, include_dirs_size;
d521ce57 1076 const char **include_dirs;
debd256d
JB
1077
1078 /* The file_names table. NOTE! These strings are not allocated
1079 with xmalloc; instead, they are pointers into debug_line_buffer.
1080 Don't try to free them directly. */
1081 unsigned int num_file_names, file_names_size;
52059ffd 1082 struct file_entry *file_names;
debd256d
JB
1083
1084 /* The start and end of the statement program following this
6502dd73 1085 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 1086 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 1087};
c906108c
SS
1088
1089/* When we construct a partial symbol table entry we only
0963b4bd 1090 need this much information. */
c906108c
SS
1091struct partial_die_info
1092 {
72bf9492 1093 /* Offset of this DIE. */
b64f50a1 1094 sect_offset offset;
72bf9492
DJ
1095
1096 /* DWARF-2 tag for this DIE. */
1097 ENUM_BITFIELD(dwarf_tag) tag : 16;
1098
72bf9492
DJ
1099 /* Assorted flags describing the data found in this DIE. */
1100 unsigned int has_children : 1;
1101 unsigned int is_external : 1;
1102 unsigned int is_declaration : 1;
1103 unsigned int has_type : 1;
1104 unsigned int has_specification : 1;
1105 unsigned int has_pc_info : 1;
481860b3 1106 unsigned int may_be_inlined : 1;
72bf9492
DJ
1107
1108 /* Flag set if the SCOPE field of this structure has been
1109 computed. */
1110 unsigned int scope_set : 1;
1111
fa4028e9
JB
1112 /* Flag set if the DIE has a byte_size attribute. */
1113 unsigned int has_byte_size : 1;
1114
ff908ebf
AW
1115 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1116 unsigned int has_const_value : 1;
1117
98bfdba5
PA
1118 /* Flag set if any of the DIE's children are template arguments. */
1119 unsigned int has_template_arguments : 1;
1120
abc72ce4
DE
1121 /* Flag set if fixup_partial_die has been called on this die. */
1122 unsigned int fixup_called : 1;
1123
36586728
TT
1124 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1125 unsigned int is_dwz : 1;
1126
1127 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1128 unsigned int spec_is_dwz : 1;
1129
72bf9492 1130 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1131 sometimes a default name for unnamed DIEs. */
15d034d0 1132 const char *name;
72bf9492 1133
abc72ce4
DE
1134 /* The linkage name, if present. */
1135 const char *linkage_name;
1136
72bf9492
DJ
1137 /* The scope to prepend to our children. This is generally
1138 allocated on the comp_unit_obstack, so will disappear
1139 when this compilation unit leaves the cache. */
15d034d0 1140 const char *scope;
72bf9492 1141
95554aad
TT
1142 /* Some data associated with the partial DIE. The tag determines
1143 which field is live. */
1144 union
1145 {
1146 /* The location description associated with this DIE, if any. */
1147 struct dwarf_block *locdesc;
1148 /* The offset of an import, for DW_TAG_imported_unit. */
1149 sect_offset offset;
1150 } d;
72bf9492
DJ
1151
1152 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1153 CORE_ADDR lowpc;
1154 CORE_ADDR highpc;
72bf9492 1155
93311388 1156 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1157 DW_AT_sibling, if any. */
abc72ce4
DE
1158 /* NOTE: This member isn't strictly necessary, read_partial_die could
1159 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1160 const gdb_byte *sibling;
72bf9492
DJ
1161
1162 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1163 DW_AT_specification (or DW_AT_abstract_origin or
1164 DW_AT_extension). */
b64f50a1 1165 sect_offset spec_offset;
72bf9492
DJ
1166
1167 /* Pointers to this DIE's parent, first child, and next sibling,
1168 if any. */
1169 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1170 };
1171
0963b4bd 1172/* This data structure holds the information of an abbrev. */
c906108c
SS
1173struct abbrev_info
1174 {
1175 unsigned int number; /* number identifying abbrev */
1176 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1177 unsigned short has_children; /* boolean */
1178 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1179 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1180 struct abbrev_info *next; /* next in chain */
1181 };
1182
1183struct attr_abbrev
1184 {
9d25dd43
DE
1185 ENUM_BITFIELD(dwarf_attribute) name : 16;
1186 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1187 };
1188
433df2d4
DE
1189/* Size of abbrev_table.abbrev_hash_table. */
1190#define ABBREV_HASH_SIZE 121
1191
1192/* Top level data structure to contain an abbreviation table. */
1193
1194struct abbrev_table
1195{
f4dc4d17
DE
1196 /* Where the abbrev table came from.
1197 This is used as a sanity check when the table is used. */
433df2d4
DE
1198 sect_offset offset;
1199
1200 /* Storage for the abbrev table. */
1201 struct obstack abbrev_obstack;
1202
1203 /* Hash table of abbrevs.
1204 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1205 It could be statically allocated, but the previous code didn't so we
1206 don't either. */
1207 struct abbrev_info **abbrevs;
1208};
1209
0963b4bd 1210/* Attributes have a name and a value. */
b60c80d6
DJ
1211struct attribute
1212 {
9d25dd43 1213 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1214 ENUM_BITFIELD(dwarf_form) form : 15;
1215
1216 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1217 field should be in u.str (existing only for DW_STRING) but it is kept
1218 here for better struct attribute alignment. */
1219 unsigned int string_is_canonical : 1;
1220
b60c80d6
DJ
1221 union
1222 {
15d034d0 1223 const char *str;
b60c80d6 1224 struct dwarf_block *blk;
43bbcdc2
PH
1225 ULONGEST unsnd;
1226 LONGEST snd;
b60c80d6 1227 CORE_ADDR addr;
ac9ec31b 1228 ULONGEST signature;
b60c80d6
DJ
1229 }
1230 u;
1231 };
1232
0963b4bd 1233/* This data structure holds a complete die structure. */
c906108c
SS
1234struct die_info
1235 {
76815b17
DE
1236 /* DWARF-2 tag for this DIE. */
1237 ENUM_BITFIELD(dwarf_tag) tag : 16;
1238
1239 /* Number of attributes */
98bfdba5
PA
1240 unsigned char num_attrs;
1241
1242 /* True if we're presently building the full type name for the
1243 type derived from this DIE. */
1244 unsigned char building_fullname : 1;
76815b17 1245
adde2bff
DE
1246 /* True if this die is in process. PR 16581. */
1247 unsigned char in_process : 1;
1248
76815b17
DE
1249 /* Abbrev number */
1250 unsigned int abbrev;
1251
93311388 1252 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1253 sect_offset offset;
78ba4af6
JB
1254
1255 /* The dies in a compilation unit form an n-ary tree. PARENT
1256 points to this die's parent; CHILD points to the first child of
1257 this node; and all the children of a given node are chained
4950bc1c 1258 together via their SIBLING fields. */
639d11d3
DC
1259 struct die_info *child; /* Its first child, if any. */
1260 struct die_info *sibling; /* Its next sibling, if any. */
1261 struct die_info *parent; /* Its parent, if any. */
c906108c 1262
b60c80d6
DJ
1263 /* An array of attributes, with NUM_ATTRS elements. There may be
1264 zero, but it's not common and zero-sized arrays are not
1265 sufficiently portable C. */
1266 struct attribute attrs[1];
c906108c
SS
1267 };
1268
0963b4bd 1269/* Get at parts of an attribute structure. */
c906108c
SS
1270
1271#define DW_STRING(attr) ((attr)->u.str)
8285870a 1272#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1273#define DW_UNSND(attr) ((attr)->u.unsnd)
1274#define DW_BLOCK(attr) ((attr)->u.blk)
1275#define DW_SND(attr) ((attr)->u.snd)
1276#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1277#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1278
0963b4bd 1279/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1280struct dwarf_block
1281 {
56eb65bd 1282 size_t size;
1d6edc3c
JK
1283
1284 /* Valid only if SIZE is not zero. */
d521ce57 1285 const gdb_byte *data;
c906108c
SS
1286 };
1287
c906108c
SS
1288#ifndef ATTR_ALLOC_CHUNK
1289#define ATTR_ALLOC_CHUNK 4
1290#endif
1291
c906108c
SS
1292/* Allocate fields for structs, unions and enums in this size. */
1293#ifndef DW_FIELD_ALLOC_CHUNK
1294#define DW_FIELD_ALLOC_CHUNK 4
1295#endif
1296
c906108c
SS
1297/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1298 but this would require a corresponding change in unpack_field_as_long
1299 and friends. */
1300static int bits_per_byte = 8;
1301
52059ffd
TT
1302struct nextfield
1303{
1304 struct nextfield *next;
1305 int accessibility;
1306 int virtuality;
1307 struct field field;
1308};
1309
1310struct nextfnfield
1311{
1312 struct nextfnfield *next;
1313 struct fn_field fnfield;
1314};
1315
1316struct fnfieldlist
1317{
1318 const char *name;
1319 int length;
1320 struct nextfnfield *head;
1321};
1322
1323struct typedef_field_list
1324{
1325 struct typedef_field field;
1326 struct typedef_field_list *next;
1327};
1328
c906108c
SS
1329/* The routines that read and process dies for a C struct or C++ class
1330 pass lists of data member fields and lists of member function fields
1331 in an instance of a field_info structure, as defined below. */
1332struct field_info
c5aa993b 1333 {
0963b4bd 1334 /* List of data member and baseclasses fields. */
52059ffd 1335 struct nextfield *fields, *baseclasses;
c906108c 1336
7d0ccb61 1337 /* Number of fields (including baseclasses). */
c5aa993b 1338 int nfields;
c906108c 1339
c5aa993b
JM
1340 /* Number of baseclasses. */
1341 int nbaseclasses;
c906108c 1342
c5aa993b
JM
1343 /* Set if the accesibility of one of the fields is not public. */
1344 int non_public_fields;
c906108c 1345
c5aa993b
JM
1346 /* Member function fields array, entries are allocated in the order they
1347 are encountered in the object file. */
52059ffd 1348 struct nextfnfield *fnfields;
c906108c 1349
c5aa993b
JM
1350 /* Member function fieldlist array, contains name of possibly overloaded
1351 member function, number of overloaded member functions and a pointer
1352 to the head of the member function field chain. */
52059ffd 1353 struct fnfieldlist *fnfieldlists;
c906108c 1354
c5aa993b
JM
1355 /* Number of entries in the fnfieldlists array. */
1356 int nfnfields;
98751a41
JK
1357
1358 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1359 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1360 struct typedef_field_list *typedef_field_list;
98751a41 1361 unsigned typedef_field_list_count;
c5aa993b 1362 };
c906108c 1363
10b3939b
DJ
1364/* One item on the queue of compilation units to read in full symbols
1365 for. */
1366struct dwarf2_queue_item
1367{
1368 struct dwarf2_per_cu_data *per_cu;
95554aad 1369 enum language pretend_language;
10b3939b
DJ
1370 struct dwarf2_queue_item *next;
1371};
1372
1373/* The current queue. */
1374static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1375
ae038cb0
DJ
1376/* Loaded secondary compilation units are kept in memory until they
1377 have not been referenced for the processing of this many
1378 compilation units. Set this to zero to disable caching. Cache
1379 sizes of up to at least twenty will improve startup time for
1380 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1381static int dwarf_max_cache_age = 5;
920d2a44 1382static void
b4f54984
DE
1383show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1384 struct cmd_list_element *c, const char *value)
920d2a44 1385{
3e43a32a 1386 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1387 "DWARF compilation units is %s.\n"),
920d2a44
AC
1388 value);
1389}
4390d890 1390\f
c906108c
SS
1391/* local function prototypes */
1392
a32a8923
DE
1393static const char *get_section_name (const struct dwarf2_section_info *);
1394
1395static const char *get_section_file_name (const struct dwarf2_section_info *);
1396
4efb68b1 1397static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1398
918dd910
JK
1399static void dwarf2_find_base_address (struct die_info *die,
1400 struct dwarf2_cu *cu);
1401
0018ea6f
DE
1402static struct partial_symtab *create_partial_symtab
1403 (struct dwarf2_per_cu_data *per_cu, const char *name);
1404
c67a9c90 1405static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1406
72bf9492
DJ
1407static void scan_partial_symbols (struct partial_die_info *,
1408 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1409 int, struct dwarf2_cu *);
c906108c 1410
72bf9492
DJ
1411static void add_partial_symbol (struct partial_die_info *,
1412 struct dwarf2_cu *);
63d06c5c 1413
72bf9492
DJ
1414static void add_partial_namespace (struct partial_die_info *pdi,
1415 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1416 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1417
5d7cb8df 1418static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1419 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1420 struct dwarf2_cu *cu);
1421
72bf9492
DJ
1422static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1423 struct dwarf2_cu *cu);
91c24f0a 1424
bc30ff58
JB
1425static void add_partial_subprogram (struct partial_die_info *pdi,
1426 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1427 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1428
257e7a09
YQ
1429static void dwarf2_read_symtab (struct partial_symtab *,
1430 struct objfile *);
c906108c 1431
a14ed312 1432static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1433
433df2d4
DE
1434static struct abbrev_info *abbrev_table_lookup_abbrev
1435 (const struct abbrev_table *, unsigned int);
1436
1437static struct abbrev_table *abbrev_table_read_table
1438 (struct dwarf2_section_info *, sect_offset);
1439
1440static void abbrev_table_free (struct abbrev_table *);
1441
f4dc4d17
DE
1442static void abbrev_table_free_cleanup (void *);
1443
dee91e82
DE
1444static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1445 struct dwarf2_section_info *);
c906108c 1446
f3dd6933 1447static void dwarf2_free_abbrev_table (void *);
c906108c 1448
d521ce57 1449static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1450
dee91e82 1451static struct partial_die_info *load_partial_dies
d521ce57 1452 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1453
d521ce57
TT
1454static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1455 struct partial_die_info *,
1456 struct abbrev_info *,
1457 unsigned int,
1458 const gdb_byte *);
c906108c 1459
36586728 1460static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1461 struct dwarf2_cu *);
72bf9492
DJ
1462
1463static void fixup_partial_die (struct partial_die_info *,
1464 struct dwarf2_cu *);
1465
d521ce57
TT
1466static const gdb_byte *read_attribute (const struct die_reader_specs *,
1467 struct attribute *, struct attr_abbrev *,
1468 const gdb_byte *);
a8329558 1469
a1855c1d 1470static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1471
a1855c1d 1472static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1473
a1855c1d 1474static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1475
a1855c1d 1476static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1477
a1855c1d 1478static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1479
d521ce57 1480static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1481 unsigned int *);
c906108c 1482
d521ce57 1483static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1484
1485static LONGEST read_checked_initial_length_and_offset
d521ce57 1486 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1487 unsigned int *, unsigned int *);
613e1657 1488
d521ce57
TT
1489static LONGEST read_offset (bfd *, const gdb_byte *,
1490 const struct comp_unit_head *,
c764a876
DE
1491 unsigned int *);
1492
d521ce57 1493static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1494
f4dc4d17
DE
1495static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1496 sect_offset);
1497
d521ce57 1498static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1499
d521ce57 1500static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1501
d521ce57
TT
1502static const char *read_indirect_string (bfd *, const gdb_byte *,
1503 const struct comp_unit_head *,
1504 unsigned int *);
4bdf3d34 1505
d521ce57 1506static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1507
d521ce57 1508static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1509
d521ce57 1510static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1511
d521ce57
TT
1512static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1513 const gdb_byte *,
3019eac3
DE
1514 unsigned int *);
1515
d521ce57 1516static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1517 ULONGEST str_index);
3019eac3 1518
e142c38c 1519static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1520
e142c38c
DJ
1521static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1522 struct dwarf2_cu *);
c906108c 1523
348e048f 1524static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1525 unsigned int);
348e048f 1526
7d45c7c3
KB
1527static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1528 struct dwarf2_cu *cu);
1529
05cf31d1
JB
1530static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1531 struct dwarf2_cu *cu);
1532
e142c38c 1533static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1534
e142c38c 1535static struct die_info *die_specification (struct die_info *die,
f2f0e013 1536 struct dwarf2_cu **);
63d06c5c 1537
debd256d
JB
1538static void free_line_header (struct line_header *lh);
1539
3019eac3
DE
1540static struct line_header *dwarf_decode_line_header (unsigned int offset,
1541 struct dwarf2_cu *cu);
debd256d 1542
f3f5162e 1543static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1544 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1545 CORE_ADDR, int decode_mapping);
c906108c 1546
4d663531 1547static void dwarf2_start_subfile (const char *, const char *);
c906108c 1548
43f3e411
DE
1549static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1550 const char *, const char *,
1551 CORE_ADDR);
f4dc4d17 1552
a14ed312 1553static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1554 struct dwarf2_cu *);
c906108c 1555
34eaf542
TT
1556static struct symbol *new_symbol_full (struct die_info *, struct type *,
1557 struct dwarf2_cu *, struct symbol *);
1558
ff39bb5e 1559static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1560 struct dwarf2_cu *);
c906108c 1561
ff39bb5e 1562static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1563 struct type *type,
1564 const char *name,
1565 struct obstack *obstack,
12df843f 1566 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1567 const gdb_byte **bytes,
98bfdba5 1568 struct dwarf2_locexpr_baton **baton);
2df3850c 1569
e7c27a73 1570static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1571
b4ba55a1
JB
1572static int need_gnat_info (struct dwarf2_cu *);
1573
3e43a32a
MS
1574static struct type *die_descriptive_type (struct die_info *,
1575 struct dwarf2_cu *);
b4ba55a1
JB
1576
1577static void set_descriptive_type (struct type *, struct die_info *,
1578 struct dwarf2_cu *);
1579
e7c27a73
DJ
1580static struct type *die_containing_type (struct die_info *,
1581 struct dwarf2_cu *);
c906108c 1582
ff39bb5e 1583static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1584 struct dwarf2_cu *);
c906108c 1585
f792889a 1586static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1587
673bfd45
DE
1588static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1589
0d5cff50 1590static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1591
6e70227d 1592static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1593 const char *suffix, int physname,
1594 struct dwarf2_cu *cu);
63d06c5c 1595
e7c27a73 1596static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1597
348e048f
DE
1598static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1599
e7c27a73 1600static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1601
e7c27a73 1602static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1603
96408a79
SA
1604static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1605
ff013f42
JK
1606static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1607 struct dwarf2_cu *, struct partial_symtab *);
1608
a14ed312 1609static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1610 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1611 struct partial_symtab *);
c906108c 1612
fae299cd
DC
1613static void get_scope_pc_bounds (struct die_info *,
1614 CORE_ADDR *, CORE_ADDR *,
1615 struct dwarf2_cu *);
1616
801e3a5b
JB
1617static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1618 CORE_ADDR, struct dwarf2_cu *);
1619
a14ed312 1620static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1621 struct dwarf2_cu *);
c906108c 1622
a14ed312 1623static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1624 struct type *, struct dwarf2_cu *);
c906108c 1625
a14ed312 1626static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1627 struct die_info *, struct type *,
e7c27a73 1628 struct dwarf2_cu *);
c906108c 1629
a14ed312 1630static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1631 struct type *,
1632 struct dwarf2_cu *);
c906108c 1633
134d01f1 1634static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1635
e7c27a73 1636static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1637
e7c27a73 1638static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1639
5d7cb8df
JK
1640static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1641
22cee43f
PMR
1642static struct using_direct **using_directives (enum language);
1643
27aa8d6a
SW
1644static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1645
74921315
KS
1646static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1647
f55ee35c
JK
1648static struct type *read_module_type (struct die_info *die,
1649 struct dwarf2_cu *cu);
1650
38d518c9 1651static const char *namespace_name (struct die_info *die,
e142c38c 1652 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1653
134d01f1 1654static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1655
e7c27a73 1656static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1657
6e70227d 1658static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1659 struct dwarf2_cu *);
1660
bf6af496 1661static struct die_info *read_die_and_siblings_1
d521ce57 1662 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1663 struct die_info *);
639d11d3 1664
dee91e82 1665static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1666 const gdb_byte *info_ptr,
1667 const gdb_byte **new_info_ptr,
639d11d3
DC
1668 struct die_info *parent);
1669
d521ce57
TT
1670static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1671 struct die_info **, const gdb_byte *,
1672 int *, int);
3019eac3 1673
d521ce57
TT
1674static const gdb_byte *read_full_die (const struct die_reader_specs *,
1675 struct die_info **, const gdb_byte *,
1676 int *);
93311388 1677
e7c27a73 1678static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1679
15d034d0
TT
1680static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1681 struct obstack *);
71c25dea 1682
15d034d0 1683static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1684
15d034d0 1685static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1686 struct die_info *die,
1687 struct dwarf2_cu *cu);
1688
ca69b9e6
DE
1689static const char *dwarf2_physname (const char *name, struct die_info *die,
1690 struct dwarf2_cu *cu);
1691
e142c38c 1692static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1693 struct dwarf2_cu **);
9219021c 1694
f39c6ffd 1695static const char *dwarf_tag_name (unsigned int);
c906108c 1696
f39c6ffd 1697static const char *dwarf_attr_name (unsigned int);
c906108c 1698
f39c6ffd 1699static const char *dwarf_form_name (unsigned int);
c906108c 1700
a14ed312 1701static char *dwarf_bool_name (unsigned int);
c906108c 1702
f39c6ffd 1703static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1704
f9aca02d 1705static struct die_info *sibling_die (struct die_info *);
c906108c 1706
d97bc12b
DE
1707static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1708
1709static void dump_die_for_error (struct die_info *);
1710
1711static void dump_die_1 (struct ui_file *, int level, int max_level,
1712 struct die_info *);
c906108c 1713
d97bc12b 1714/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1715
51545339 1716static void store_in_ref_table (struct die_info *,
10b3939b 1717 struct dwarf2_cu *);
c906108c 1718
ff39bb5e 1719static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1720
ff39bb5e 1721static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1722
348e048f 1723static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1724 const struct attribute *,
348e048f
DE
1725 struct dwarf2_cu **);
1726
10b3939b 1727static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1728 const struct attribute *,
f2f0e013 1729 struct dwarf2_cu **);
c906108c 1730
348e048f 1731static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1732 const struct attribute *,
348e048f
DE
1733 struct dwarf2_cu **);
1734
ac9ec31b
DE
1735static struct type *get_signatured_type (struct die_info *, ULONGEST,
1736 struct dwarf2_cu *);
1737
1738static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1739 const struct attribute *,
ac9ec31b
DE
1740 struct dwarf2_cu *);
1741
e5fe5e75 1742static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1743
52dc124a 1744static void read_signatured_type (struct signatured_type *);
348e048f 1745
c906108c
SS
1746/* memory allocation interface */
1747
7b5a2f43 1748static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1749
b60c80d6 1750static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1751
43f3e411 1752static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1753
6e5a29e1 1754static int attr_form_is_block (const struct attribute *);
8e19ed76 1755
6e5a29e1 1756static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1757
6e5a29e1 1758static int attr_form_is_constant (const struct attribute *);
3690dd37 1759
6e5a29e1 1760static int attr_form_is_ref (const struct attribute *);
7771576e 1761
8cf6f0b1
TT
1762static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1763 struct dwarf2_loclist_baton *baton,
ff39bb5e 1764 const struct attribute *attr);
8cf6f0b1 1765
ff39bb5e 1766static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1767 struct symbol *sym,
f1e6e072
TT
1768 struct dwarf2_cu *cu,
1769 int is_block);
4c2df51b 1770
d521ce57
TT
1771static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1772 const gdb_byte *info_ptr,
1773 struct abbrev_info *abbrev);
4bb7a0a7 1774
72bf9492
DJ
1775static void free_stack_comp_unit (void *);
1776
72bf9492
DJ
1777static hashval_t partial_die_hash (const void *item);
1778
1779static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1780
ae038cb0 1781static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1782 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1783
9816fde3 1784static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1785 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1786
1787static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1788 struct die_info *comp_unit_die,
1789 enum language pretend_language);
93311388 1790
68dc6402 1791static void free_heap_comp_unit (void *);
ae038cb0
DJ
1792
1793static void free_cached_comp_units (void *);
1794
1795static void age_cached_comp_units (void);
1796
dee91e82 1797static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1798
f792889a
DJ
1799static struct type *set_die_type (struct die_info *, struct type *,
1800 struct dwarf2_cu *);
1c379e20 1801
ae038cb0
DJ
1802static void create_all_comp_units (struct objfile *);
1803
0e50663e 1804static int create_all_type_units (struct objfile *);
1fd400ff 1805
95554aad
TT
1806static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1807 enum language);
10b3939b 1808
95554aad
TT
1809static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1810 enum language);
10b3939b 1811
f4dc4d17
DE
1812static void process_full_type_unit (struct dwarf2_per_cu_data *,
1813 enum language);
1814
10b3939b
DJ
1815static void dwarf2_add_dependence (struct dwarf2_cu *,
1816 struct dwarf2_per_cu_data *);
1817
ae038cb0
DJ
1818static void dwarf2_mark (struct dwarf2_cu *);
1819
1820static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1821
b64f50a1 1822static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1823 struct dwarf2_per_cu_data *);
673bfd45 1824
f792889a 1825static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1826
9291a0cd
TT
1827static void dwarf2_release_queue (void *dummy);
1828
95554aad
TT
1829static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1830 enum language pretend_language);
1831
a0f42c21 1832static void process_queue (void);
9291a0cd
TT
1833
1834static void find_file_and_directory (struct die_info *die,
1835 struct dwarf2_cu *cu,
15d034d0 1836 const char **name, const char **comp_dir);
9291a0cd
TT
1837
1838static char *file_full_name (int file, struct line_header *lh,
1839 const char *comp_dir);
1840
d521ce57 1841static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1842 (struct comp_unit_head *header,
1843 struct dwarf2_section_info *section,
d521ce57 1844 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1845 int is_debug_types_section);
1846
fd820528 1847static void init_cutu_and_read_dies
f4dc4d17
DE
1848 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1849 int use_existing_cu, int keep,
3019eac3
DE
1850 die_reader_func_ftype *die_reader_func, void *data);
1851
dee91e82
DE
1852static void init_cutu_and_read_dies_simple
1853 (struct dwarf2_per_cu_data *this_cu,
1854 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1855
673bfd45 1856static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1857
3019eac3
DE
1858static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1859
57d63ce2
DE
1860static struct dwo_unit *lookup_dwo_unit_in_dwp
1861 (struct dwp_file *dwp_file, const char *comp_dir,
1862 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
1863
1864static struct dwp_file *get_dwp_file (void);
1865
3019eac3 1866static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1867 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1868
1869static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1870 (struct signatured_type *, const char *, const char *);
3019eac3 1871
89e63ee4
DE
1872static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1873
3019eac3
DE
1874static void free_dwo_file_cleanup (void *);
1875
95554aad
TT
1876static void process_cu_includes (void);
1877
1b80a9fa 1878static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1879
1880static void free_line_header_voidp (void *arg);
4390d890
DE
1881\f
1882/* Various complaints about symbol reading that don't abort the process. */
1883
1884static void
1885dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1886{
1887 complaint (&symfile_complaints,
1888 _("statement list doesn't fit in .debug_line section"));
1889}
1890
1891static void
1892dwarf2_debug_line_missing_file_complaint (void)
1893{
1894 complaint (&symfile_complaints,
1895 _(".debug_line section has line data without a file"));
1896}
1897
1898static void
1899dwarf2_debug_line_missing_end_sequence_complaint (void)
1900{
1901 complaint (&symfile_complaints,
1902 _(".debug_line section has line "
1903 "program sequence without an end"));
1904}
1905
1906static void
1907dwarf2_complex_location_expr_complaint (void)
1908{
1909 complaint (&symfile_complaints, _("location expression too complex"));
1910}
1911
1912static void
1913dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1914 int arg3)
1915{
1916 complaint (&symfile_complaints,
1917 _("const value length mismatch for '%s', got %d, expected %d"),
1918 arg1, arg2, arg3);
1919}
1920
1921static void
1922dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1923{
1924 complaint (&symfile_complaints,
1925 _("debug info runs off end of %s section"
1926 " [in module %s]"),
a32a8923
DE
1927 get_section_name (section),
1928 get_section_file_name (section));
4390d890 1929}
1b80a9fa 1930
4390d890
DE
1931static void
1932dwarf2_macro_malformed_definition_complaint (const char *arg1)
1933{
1934 complaint (&symfile_complaints,
1935 _("macro debug info contains a "
1936 "malformed macro definition:\n`%s'"),
1937 arg1);
1938}
1939
1940static void
1941dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1942{
1943 complaint (&symfile_complaints,
1944 _("invalid attribute class or form for '%s' in '%s'"),
1945 arg1, arg2);
1946}
527f3840
JK
1947
1948/* Hash function for line_header_hash. */
1949
1950static hashval_t
1951line_header_hash (const struct line_header *ofs)
1952{
1953 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
1954}
1955
1956/* Hash function for htab_create_alloc_ex for line_header_hash. */
1957
1958static hashval_t
1959line_header_hash_voidp (const void *item)
1960{
1961 const struct line_header *ofs = item;
1962
1963 return line_header_hash (ofs);
1964}
1965
1966/* Equality function for line_header_hash. */
1967
1968static int
1969line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1970{
1971 const struct line_header *ofs_lhs = item_lhs;
1972 const struct line_header *ofs_rhs = item_rhs;
1973
1974 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
1975 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1976}
1977
4390d890 1978\f
9291a0cd
TT
1979#if WORDS_BIGENDIAN
1980
1981/* Convert VALUE between big- and little-endian. */
1982static offset_type
1983byte_swap (offset_type value)
1984{
1985 offset_type result;
1986
1987 result = (value & 0xff) << 24;
1988 result |= (value & 0xff00) << 8;
1989 result |= (value & 0xff0000) >> 8;
1990 result |= (value & 0xff000000) >> 24;
1991 return result;
1992}
1993
1994#define MAYBE_SWAP(V) byte_swap (V)
1995
1996#else
1997#define MAYBE_SWAP(V) (V)
1998#endif /* WORDS_BIGENDIAN */
1999
31aa7e4e
JB
2000/* Read the given attribute value as an address, taking the attribute's
2001 form into account. */
2002
2003static CORE_ADDR
2004attr_value_as_address (struct attribute *attr)
2005{
2006 CORE_ADDR addr;
2007
2008 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2009 {
2010 /* Aside from a few clearly defined exceptions, attributes that
2011 contain an address must always be in DW_FORM_addr form.
2012 Unfortunately, some compilers happen to be violating this
2013 requirement by encoding addresses using other forms, such
2014 as DW_FORM_data4 for example. For those broken compilers,
2015 we try to do our best, without any guarantee of success,
2016 to interpret the address correctly. It would also be nice
2017 to generate a complaint, but that would require us to maintain
2018 a list of legitimate cases where a non-address form is allowed,
2019 as well as update callers to pass in at least the CU's DWARF
2020 version. This is more overhead than what we're willing to
2021 expand for a pretty rare case. */
2022 addr = DW_UNSND (attr);
2023 }
2024 else
2025 addr = DW_ADDR (attr);
2026
2027 return addr;
2028}
2029
9291a0cd
TT
2030/* The suffix for an index file. */
2031#define INDEX_SUFFIX ".gdb-index"
2032
c906108c 2033/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2034 information and return true if we have enough to do something.
2035 NAMES points to the dwarf2 section names, or is NULL if the standard
2036 ELF names are used. */
c906108c
SS
2037
2038int
251d32d9
TG
2039dwarf2_has_info (struct objfile *objfile,
2040 const struct dwarf2_debug_sections *names)
c906108c 2041{
be391dca
TT
2042 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2043 if (!dwarf2_per_objfile)
2044 {
2045 /* Initialize per-objfile state. */
2046 struct dwarf2_per_objfile *data
2047 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 2048
be391dca
TT
2049 memset (data, 0, sizeof (*data));
2050 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2051 dwarf2_per_objfile = data;
6502dd73 2052
251d32d9
TG
2053 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2054 (void *) names);
be391dca
TT
2055 dwarf2_per_objfile->objfile = objfile;
2056 }
73869dc2
DE
2057 return (!dwarf2_per_objfile->info.is_virtual
2058 && dwarf2_per_objfile->info.s.asection != NULL
2059 && !dwarf2_per_objfile->abbrev.is_virtual
2060 && dwarf2_per_objfile->abbrev.s.asection != NULL);
2061}
2062
2063/* Return the containing section of virtual section SECTION. */
2064
2065static struct dwarf2_section_info *
2066get_containing_section (const struct dwarf2_section_info *section)
2067{
2068 gdb_assert (section->is_virtual);
2069 return section->s.containing_section;
c906108c
SS
2070}
2071
a32a8923
DE
2072/* Return the bfd owner of SECTION. */
2073
2074static struct bfd *
2075get_section_bfd_owner (const struct dwarf2_section_info *section)
2076{
73869dc2
DE
2077 if (section->is_virtual)
2078 {
2079 section = get_containing_section (section);
2080 gdb_assert (!section->is_virtual);
2081 }
2082 return section->s.asection->owner;
a32a8923
DE
2083}
2084
2085/* Return the bfd section of SECTION.
2086 Returns NULL if the section is not present. */
2087
2088static asection *
2089get_section_bfd_section (const struct dwarf2_section_info *section)
2090{
73869dc2
DE
2091 if (section->is_virtual)
2092 {
2093 section = get_containing_section (section);
2094 gdb_assert (!section->is_virtual);
2095 }
2096 return section->s.asection;
a32a8923
DE
2097}
2098
2099/* Return the name of SECTION. */
2100
2101static const char *
2102get_section_name (const struct dwarf2_section_info *section)
2103{
2104 asection *sectp = get_section_bfd_section (section);
2105
2106 gdb_assert (sectp != NULL);
2107 return bfd_section_name (get_section_bfd_owner (section), sectp);
2108}
2109
2110/* Return the name of the file SECTION is in. */
2111
2112static const char *
2113get_section_file_name (const struct dwarf2_section_info *section)
2114{
2115 bfd *abfd = get_section_bfd_owner (section);
2116
2117 return bfd_get_filename (abfd);
2118}
2119
2120/* Return the id of SECTION.
2121 Returns 0 if SECTION doesn't exist. */
2122
2123static int
2124get_section_id (const struct dwarf2_section_info *section)
2125{
2126 asection *sectp = get_section_bfd_section (section);
2127
2128 if (sectp == NULL)
2129 return 0;
2130 return sectp->id;
2131}
2132
2133/* Return the flags of SECTION.
73869dc2 2134 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2135
2136static int
2137get_section_flags (const struct dwarf2_section_info *section)
2138{
2139 asection *sectp = get_section_bfd_section (section);
2140
2141 gdb_assert (sectp != NULL);
2142 return bfd_get_section_flags (sectp->owner, sectp);
2143}
2144
251d32d9
TG
2145/* When loading sections, we look either for uncompressed section or for
2146 compressed section names. */
233a11ab
CS
2147
2148static int
251d32d9
TG
2149section_is_p (const char *section_name,
2150 const struct dwarf2_section_names *names)
233a11ab 2151{
251d32d9
TG
2152 if (names->normal != NULL
2153 && strcmp (section_name, names->normal) == 0)
2154 return 1;
2155 if (names->compressed != NULL
2156 && strcmp (section_name, names->compressed) == 0)
2157 return 1;
2158 return 0;
233a11ab
CS
2159}
2160
c906108c
SS
2161/* This function is mapped across the sections and remembers the
2162 offset and size of each of the debugging sections we are interested
2163 in. */
2164
2165static void
251d32d9 2166dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 2167{
251d32d9 2168 const struct dwarf2_debug_sections *names;
dc7650b8 2169 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
2170
2171 if (vnames == NULL)
2172 names = &dwarf2_elf_names;
2173 else
2174 names = (const struct dwarf2_debug_sections *) vnames;
2175
dc7650b8
JK
2176 if ((aflag & SEC_HAS_CONTENTS) == 0)
2177 {
2178 }
2179 else if (section_is_p (sectp->name, &names->info))
c906108c 2180 {
73869dc2 2181 dwarf2_per_objfile->info.s.asection = sectp;
dce234bc 2182 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 2183 }
251d32d9 2184 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 2185 {
73869dc2 2186 dwarf2_per_objfile->abbrev.s.asection = sectp;
dce234bc 2187 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 2188 }
251d32d9 2189 else if (section_is_p (sectp->name, &names->line))
c906108c 2190 {
73869dc2 2191 dwarf2_per_objfile->line.s.asection = sectp;
dce234bc 2192 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 2193 }
251d32d9 2194 else if (section_is_p (sectp->name, &names->loc))
c906108c 2195 {
73869dc2 2196 dwarf2_per_objfile->loc.s.asection = sectp;
dce234bc 2197 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 2198 }
251d32d9 2199 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 2200 {
73869dc2 2201 dwarf2_per_objfile->macinfo.s.asection = sectp;
dce234bc 2202 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 2203 }
cf2c3c16
TT
2204 else if (section_is_p (sectp->name, &names->macro))
2205 {
73869dc2 2206 dwarf2_per_objfile->macro.s.asection = sectp;
cf2c3c16
TT
2207 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2208 }
251d32d9 2209 else if (section_is_p (sectp->name, &names->str))
c906108c 2210 {
73869dc2 2211 dwarf2_per_objfile->str.s.asection = sectp;
dce234bc 2212 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 2213 }
3019eac3
DE
2214 else if (section_is_p (sectp->name, &names->addr))
2215 {
73869dc2 2216 dwarf2_per_objfile->addr.s.asection = sectp;
3019eac3
DE
2217 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2218 }
251d32d9 2219 else if (section_is_p (sectp->name, &names->frame))
b6af0555 2220 {
73869dc2 2221 dwarf2_per_objfile->frame.s.asection = sectp;
dce234bc 2222 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 2223 }
251d32d9 2224 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 2225 {
73869dc2 2226 dwarf2_per_objfile->eh_frame.s.asection = sectp;
dc7650b8 2227 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2228 }
251d32d9 2229 else if (section_is_p (sectp->name, &names->ranges))
af34e669 2230 {
73869dc2 2231 dwarf2_per_objfile->ranges.s.asection = sectp;
dce234bc 2232 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2233 }
251d32d9 2234 else if (section_is_p (sectp->name, &names->types))
348e048f 2235 {
8b70b953
TT
2236 struct dwarf2_section_info type_section;
2237
2238 memset (&type_section, 0, sizeof (type_section));
73869dc2 2239 type_section.s.asection = sectp;
8b70b953
TT
2240 type_section.size = bfd_get_section_size (sectp);
2241
2242 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2243 &type_section);
348e048f 2244 }
251d32d9 2245 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd 2246 {
73869dc2 2247 dwarf2_per_objfile->gdb_index.s.asection = sectp;
9291a0cd
TT
2248 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2249 }
dce234bc 2250
b4e1fd61 2251 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5
FR
2252 && bfd_section_vma (abfd, sectp) == 0)
2253 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2254}
2255
fceca515
DE
2256/* A helper function that decides whether a section is empty,
2257 or not present. */
9e0ac564
TT
2258
2259static int
19ac8c2e 2260dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2261{
73869dc2
DE
2262 if (section->is_virtual)
2263 return section->size == 0;
2264 return section->s.asection == NULL || section->size == 0;
9e0ac564
TT
2265}
2266
3019eac3
DE
2267/* Read the contents of the section INFO.
2268 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2269 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2270 of the DWO file.
dce234bc 2271 If the section is compressed, uncompress it before returning. */
c906108c 2272
dce234bc
PP
2273static void
2274dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2275{
a32a8923 2276 asection *sectp;
3019eac3 2277 bfd *abfd;
dce234bc 2278 gdb_byte *buf, *retbuf;
c906108c 2279
be391dca
TT
2280 if (info->readin)
2281 return;
dce234bc 2282 info->buffer = NULL;
be391dca 2283 info->readin = 1;
188dd5d6 2284
9e0ac564 2285 if (dwarf2_section_empty_p (info))
dce234bc 2286 return;
c906108c 2287
a32a8923 2288 sectp = get_section_bfd_section (info);
3019eac3 2289
73869dc2
DE
2290 /* If this is a virtual section we need to read in the real one first. */
2291 if (info->is_virtual)
2292 {
2293 struct dwarf2_section_info *containing_section =
2294 get_containing_section (info);
2295
2296 gdb_assert (sectp != NULL);
2297 if ((sectp->flags & SEC_RELOC) != 0)
2298 {
2299 error (_("Dwarf Error: DWP format V2 with relocations is not"
2300 " supported in section %s [in module %s]"),
2301 get_section_name (info), get_section_file_name (info));
2302 }
2303 dwarf2_read_section (objfile, containing_section);
2304 /* Other code should have already caught virtual sections that don't
2305 fit. */
2306 gdb_assert (info->virtual_offset + info->size
2307 <= containing_section->size);
2308 /* If the real section is empty or there was a problem reading the
2309 section we shouldn't get here. */
2310 gdb_assert (containing_section->buffer != NULL);
2311 info->buffer = containing_section->buffer + info->virtual_offset;
2312 return;
2313 }
2314
4bf44c1c
TT
2315 /* If the section has relocations, we must read it ourselves.
2316 Otherwise we attach it to the BFD. */
2317 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2318 {
d521ce57 2319 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2320 return;
dce234bc 2321 }
dce234bc 2322
4bf44c1c
TT
2323 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
2324 info->buffer = buf;
dce234bc
PP
2325
2326 /* When debugging .o files, we may need to apply relocations; see
2327 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2328 We never compress sections in .o files, so we only need to
2329 try this when the section is not compressed. */
ac8035ab 2330 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2331 if (retbuf != NULL)
2332 {
2333 info->buffer = retbuf;
2334 return;
2335 }
2336
a32a8923
DE
2337 abfd = get_section_bfd_owner (info);
2338 gdb_assert (abfd != NULL);
2339
dce234bc
PP
2340 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2341 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2342 {
2343 error (_("Dwarf Error: Can't read DWARF data"
2344 " in section %s [in module %s]"),
2345 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2346 }
dce234bc
PP
2347}
2348
9e0ac564
TT
2349/* A helper function that returns the size of a section in a safe way.
2350 If you are positive that the section has been read before using the
2351 size, then it is safe to refer to the dwarf2_section_info object's
2352 "size" field directly. In other cases, you must call this
2353 function, because for compressed sections the size field is not set
2354 correctly until the section has been read. */
2355
2356static bfd_size_type
2357dwarf2_section_size (struct objfile *objfile,
2358 struct dwarf2_section_info *info)
2359{
2360 if (!info->readin)
2361 dwarf2_read_section (objfile, info);
2362 return info->size;
2363}
2364
dce234bc 2365/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2366 SECTION_NAME. */
af34e669 2367
dce234bc 2368void
3017a003
TG
2369dwarf2_get_section_info (struct objfile *objfile,
2370 enum dwarf2_section_enum sect,
d521ce57 2371 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2372 bfd_size_type *sizep)
2373{
2374 struct dwarf2_per_objfile *data
2375 = objfile_data (objfile, dwarf2_objfile_data_key);
2376 struct dwarf2_section_info *info;
a3b2a86b
TT
2377
2378 /* We may see an objfile without any DWARF, in which case we just
2379 return nothing. */
2380 if (data == NULL)
2381 {
2382 *sectp = NULL;
2383 *bufp = NULL;
2384 *sizep = 0;
2385 return;
2386 }
3017a003
TG
2387 switch (sect)
2388 {
2389 case DWARF2_DEBUG_FRAME:
2390 info = &data->frame;
2391 break;
2392 case DWARF2_EH_FRAME:
2393 info = &data->eh_frame;
2394 break;
2395 default:
2396 gdb_assert_not_reached ("unexpected section");
2397 }
dce234bc 2398
9e0ac564 2399 dwarf2_read_section (objfile, info);
dce234bc 2400
a32a8923 2401 *sectp = get_section_bfd_section (info);
dce234bc
PP
2402 *bufp = info->buffer;
2403 *sizep = info->size;
2404}
2405
36586728
TT
2406/* A helper function to find the sections for a .dwz file. */
2407
2408static void
2409locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2410{
2411 struct dwz_file *dwz_file = arg;
2412
2413 /* Note that we only support the standard ELF names, because .dwz
2414 is ELF-only (at the time of writing). */
2415 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2416 {
73869dc2 2417 dwz_file->abbrev.s.asection = sectp;
36586728
TT
2418 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2419 }
2420 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2421 {
73869dc2 2422 dwz_file->info.s.asection = sectp;
36586728
TT
2423 dwz_file->info.size = bfd_get_section_size (sectp);
2424 }
2425 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2426 {
73869dc2 2427 dwz_file->str.s.asection = sectp;
36586728
TT
2428 dwz_file->str.size = bfd_get_section_size (sectp);
2429 }
2430 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2431 {
73869dc2 2432 dwz_file->line.s.asection = sectp;
36586728
TT
2433 dwz_file->line.size = bfd_get_section_size (sectp);
2434 }
2435 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2436 {
73869dc2 2437 dwz_file->macro.s.asection = sectp;
36586728
TT
2438 dwz_file->macro.size = bfd_get_section_size (sectp);
2439 }
2ec9a5e0
TT
2440 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2441 {
73869dc2 2442 dwz_file->gdb_index.s.asection = sectp;
2ec9a5e0
TT
2443 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2444 }
36586728
TT
2445}
2446
4db1a1dc
TT
2447/* Open the separate '.dwz' debug file, if needed. Return NULL if
2448 there is no .gnu_debugaltlink section in the file. Error if there
2449 is such a section but the file cannot be found. */
36586728
TT
2450
2451static struct dwz_file *
2452dwarf2_get_dwz_file (void)
2453{
4db1a1dc
TT
2454 bfd *dwz_bfd;
2455 char *data;
36586728
TT
2456 struct cleanup *cleanup;
2457 const char *filename;
2458 struct dwz_file *result;
acd13123 2459 bfd_size_type buildid_len_arg;
dc294be5
TT
2460 size_t buildid_len;
2461 bfd_byte *buildid;
36586728
TT
2462
2463 if (dwarf2_per_objfile->dwz_file != NULL)
2464 return dwarf2_per_objfile->dwz_file;
2465
4db1a1dc
TT
2466 bfd_set_error (bfd_error_no_error);
2467 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2468 &buildid_len_arg, &buildid);
4db1a1dc
TT
2469 if (data == NULL)
2470 {
2471 if (bfd_get_error () == bfd_error_no_error)
2472 return NULL;
2473 error (_("could not read '.gnu_debugaltlink' section: %s"),
2474 bfd_errmsg (bfd_get_error ()));
2475 }
36586728 2476 cleanup = make_cleanup (xfree, data);
dc294be5 2477 make_cleanup (xfree, buildid);
36586728 2478
acd13123
TT
2479 buildid_len = (size_t) buildid_len_arg;
2480
f9d83a0b 2481 filename = (const char *) data;
36586728
TT
2482 if (!IS_ABSOLUTE_PATH (filename))
2483 {
4262abfb 2484 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2485 char *rel;
2486
2487 make_cleanup (xfree, abs);
2488 abs = ldirname (abs);
2489 make_cleanup (xfree, abs);
2490
2491 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2492 make_cleanup (xfree, rel);
2493 filename = rel;
2494 }
2495
dc294be5
TT
2496 /* First try the file name given in the section. If that doesn't
2497 work, try to use the build-id instead. */
36586728 2498 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
dc294be5 2499 if (dwz_bfd != NULL)
36586728 2500 {
dc294be5
TT
2501 if (!build_id_verify (dwz_bfd, buildid_len, buildid))
2502 {
2503 gdb_bfd_unref (dwz_bfd);
2504 dwz_bfd = NULL;
2505 }
36586728
TT
2506 }
2507
dc294be5
TT
2508 if (dwz_bfd == NULL)
2509 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2510
2511 if (dwz_bfd == NULL)
2512 error (_("could not find '.gnu_debugaltlink' file for %s"),
2513 objfile_name (dwarf2_per_objfile->objfile));
2514
36586728
TT
2515 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2516 struct dwz_file);
2517 result->dwz_bfd = dwz_bfd;
2518
2519 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2520
2521 do_cleanups (cleanup);
2522
13aaf454 2523 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, dwz_bfd);
8d2cc612 2524 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2525 return result;
2526}
9291a0cd 2527\f
7b9f3c50
DE
2528/* DWARF quick_symbols_functions support. */
2529
2530/* TUs can share .debug_line entries, and there can be a lot more TUs than
2531 unique line tables, so we maintain a separate table of all .debug_line
2532 derived entries to support the sharing.
2533 All the quick functions need is the list of file names. We discard the
2534 line_header when we're done and don't need to record it here. */
2535struct quick_file_names
2536{
094b34ac
DE
2537 /* The data used to construct the hash key. */
2538 struct stmt_list_hash hash;
7b9f3c50
DE
2539
2540 /* The number of entries in file_names, real_names. */
2541 unsigned int num_file_names;
2542
2543 /* The file names from the line table, after being run through
2544 file_full_name. */
2545 const char **file_names;
2546
2547 /* The file names from the line table after being run through
2548 gdb_realpath. These are computed lazily. */
2549 const char **real_names;
2550};
2551
2552/* When using the index (and thus not using psymtabs), each CU has an
2553 object of this type. This is used to hold information needed by
2554 the various "quick" methods. */
2555struct dwarf2_per_cu_quick_data
2556{
2557 /* The file table. This can be NULL if there was no file table
2558 or it's currently not read in.
2559 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2560 struct quick_file_names *file_names;
2561
2562 /* The corresponding symbol table. This is NULL if symbols for this
2563 CU have not yet been read. */
43f3e411 2564 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2565
2566 /* A temporary mark bit used when iterating over all CUs in
2567 expand_symtabs_matching. */
2568 unsigned int mark : 1;
2569
2570 /* True if we've tried to read the file table and found there isn't one.
2571 There will be no point in trying to read it again next time. */
2572 unsigned int no_file_data : 1;
2573};
2574
094b34ac
DE
2575/* Utility hash function for a stmt_list_hash. */
2576
2577static hashval_t
2578hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2579{
2580 hashval_t v = 0;
2581
2582 if (stmt_list_hash->dwo_unit != NULL)
2583 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2584 v += stmt_list_hash->line_offset.sect_off;
2585 return v;
2586}
2587
2588/* Utility equality function for a stmt_list_hash. */
2589
2590static int
2591eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2592 const struct stmt_list_hash *rhs)
2593{
2594 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2595 return 0;
2596 if (lhs->dwo_unit != NULL
2597 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2598 return 0;
2599
2600 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2601}
2602
7b9f3c50
DE
2603/* Hash function for a quick_file_names. */
2604
2605static hashval_t
2606hash_file_name_entry (const void *e)
2607{
2608 const struct quick_file_names *file_data = e;
2609
094b34ac 2610 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2611}
2612
2613/* Equality function for a quick_file_names. */
2614
2615static int
2616eq_file_name_entry (const void *a, const void *b)
2617{
2618 const struct quick_file_names *ea = a;
2619 const struct quick_file_names *eb = b;
2620
094b34ac 2621 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2622}
2623
2624/* Delete function for a quick_file_names. */
2625
2626static void
2627delete_file_name_entry (void *e)
2628{
2629 struct quick_file_names *file_data = e;
2630 int i;
2631
2632 for (i = 0; i < file_data->num_file_names; ++i)
2633 {
2634 xfree ((void*) file_data->file_names[i]);
2635 if (file_data->real_names)
2636 xfree ((void*) file_data->real_names[i]);
2637 }
2638
2639 /* The space for the struct itself lives on objfile_obstack,
2640 so we don't free it here. */
2641}
2642
2643/* Create a quick_file_names hash table. */
2644
2645static htab_t
2646create_quick_file_names_table (unsigned int nr_initial_entries)
2647{
2648 return htab_create_alloc (nr_initial_entries,
2649 hash_file_name_entry, eq_file_name_entry,
2650 delete_file_name_entry, xcalloc, xfree);
2651}
9291a0cd 2652
918dd910
JK
2653/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2654 have to be created afterwards. You should call age_cached_comp_units after
2655 processing PER_CU->CU. dw2_setup must have been already called. */
2656
2657static void
2658load_cu (struct dwarf2_per_cu_data *per_cu)
2659{
3019eac3 2660 if (per_cu->is_debug_types)
e5fe5e75 2661 load_full_type_unit (per_cu);
918dd910 2662 else
95554aad 2663 load_full_comp_unit (per_cu, language_minimal);
918dd910 2664
cc12ce38
DE
2665 if (per_cu->cu == NULL)
2666 return; /* Dummy CU. */
2dc860c0
DE
2667
2668 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2669}
2670
a0f42c21 2671/* Read in the symbols for PER_CU. */
2fdf6df6 2672
9291a0cd 2673static void
a0f42c21 2674dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2675{
2676 struct cleanup *back_to;
2677
f4dc4d17
DE
2678 /* Skip type_unit_groups, reading the type units they contain
2679 is handled elsewhere. */
2680 if (IS_TYPE_UNIT_GROUP (per_cu))
2681 return;
2682
9291a0cd
TT
2683 back_to = make_cleanup (dwarf2_release_queue, NULL);
2684
95554aad 2685 if (dwarf2_per_objfile->using_index
43f3e411 2686 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2687 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2688 {
2689 queue_comp_unit (per_cu, language_minimal);
2690 load_cu (per_cu);
89e63ee4
DE
2691
2692 /* If we just loaded a CU from a DWO, and we're working with an index
2693 that may badly handle TUs, load all the TUs in that DWO as well.
2694 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2695 if (!per_cu->is_debug_types
cc12ce38 2696 && per_cu->cu != NULL
89e63ee4
DE
2697 && per_cu->cu->dwo_unit != NULL
2698 && dwarf2_per_objfile->index_table != NULL
2699 && dwarf2_per_objfile->index_table->version <= 7
2700 /* DWP files aren't supported yet. */
2701 && get_dwp_file () == NULL)
2702 queue_and_load_all_dwo_tus (per_cu);
95554aad 2703 }
9291a0cd 2704
a0f42c21 2705 process_queue ();
9291a0cd
TT
2706
2707 /* Age the cache, releasing compilation units that have not
2708 been used recently. */
2709 age_cached_comp_units ();
2710
2711 do_cleanups (back_to);
2712}
2713
2714/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2715 the objfile from which this CU came. Returns the resulting symbol
2716 table. */
2fdf6df6 2717
43f3e411 2718static struct compunit_symtab *
a0f42c21 2719dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2720{
95554aad 2721 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2722 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2723 {
2724 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2725 increment_reading_symtab ();
a0f42c21 2726 dw2_do_instantiate_symtab (per_cu);
95554aad 2727 process_cu_includes ();
9291a0cd
TT
2728 do_cleanups (back_to);
2729 }
f194fefb 2730
43f3e411 2731 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2732}
2733
8832e7e3 2734/* Return the CU/TU given its index.
f4dc4d17
DE
2735
2736 This is intended for loops like:
2737
2738 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2739 + dwarf2_per_objfile->n_type_units); ++i)
2740 {
8832e7e3 2741 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2742
2743 ...;
2744 }
2745*/
2fdf6df6 2746
1fd400ff 2747static struct dwarf2_per_cu_data *
8832e7e3 2748dw2_get_cutu (int index)
1fd400ff
TT
2749{
2750 if (index >= dwarf2_per_objfile->n_comp_units)
2751 {
f4dc4d17 2752 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2753 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2754 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2755 }
2756
2757 return dwarf2_per_objfile->all_comp_units[index];
2758}
2759
8832e7e3
DE
2760/* Return the CU given its index.
2761 This differs from dw2_get_cutu in that it's for when you know INDEX
2762 refers to a CU. */
f4dc4d17
DE
2763
2764static struct dwarf2_per_cu_data *
8832e7e3 2765dw2_get_cu (int index)
f4dc4d17 2766{
8832e7e3 2767 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2768
1fd400ff
TT
2769 return dwarf2_per_objfile->all_comp_units[index];
2770}
2771
2ec9a5e0
TT
2772/* A helper for create_cus_from_index that handles a given list of
2773 CUs. */
2fdf6df6 2774
74a0d9f6 2775static void
2ec9a5e0
TT
2776create_cus_from_index_list (struct objfile *objfile,
2777 const gdb_byte *cu_list, offset_type n_elements,
2778 struct dwarf2_section_info *section,
2779 int is_dwz,
2780 int base_offset)
9291a0cd
TT
2781{
2782 offset_type i;
9291a0cd 2783
2ec9a5e0 2784 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2785 {
2786 struct dwarf2_per_cu_data *the_cu;
2787 ULONGEST offset, length;
2788
74a0d9f6
JK
2789 gdb_static_assert (sizeof (ULONGEST) >= 8);
2790 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2791 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2792 cu_list += 2 * 8;
2793
2794 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2795 struct dwarf2_per_cu_data);
b64f50a1 2796 the_cu->offset.sect_off = offset;
9291a0cd
TT
2797 the_cu->length = length;
2798 the_cu->objfile = objfile;
8a0459fd 2799 the_cu->section = section;
9291a0cd
TT
2800 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2801 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2802 the_cu->is_dwz = is_dwz;
2803 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2804 }
9291a0cd
TT
2805}
2806
2ec9a5e0 2807/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2808 the CU objects for this objfile. */
2ec9a5e0 2809
74a0d9f6 2810static void
2ec9a5e0
TT
2811create_cus_from_index (struct objfile *objfile,
2812 const gdb_byte *cu_list, offset_type cu_list_elements,
2813 const gdb_byte *dwz_list, offset_type dwz_elements)
2814{
2815 struct dwz_file *dwz;
2816
2817 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2818 dwarf2_per_objfile->all_comp_units
2819 = obstack_alloc (&objfile->objfile_obstack,
2820 dwarf2_per_objfile->n_comp_units
2821 * sizeof (struct dwarf2_per_cu_data *));
2822
74a0d9f6
JK
2823 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2824 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2825
2826 if (dwz_elements == 0)
74a0d9f6 2827 return;
2ec9a5e0
TT
2828
2829 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2830 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2831 cu_list_elements / 2);
2ec9a5e0
TT
2832}
2833
1fd400ff 2834/* Create the signatured type hash table from the index. */
673bfd45 2835
74a0d9f6 2836static void
673bfd45 2837create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2838 struct dwarf2_section_info *section,
673bfd45
DE
2839 const gdb_byte *bytes,
2840 offset_type elements)
1fd400ff
TT
2841{
2842 offset_type i;
673bfd45 2843 htab_t sig_types_hash;
1fd400ff 2844
6aa5f3a6
DE
2845 dwarf2_per_objfile->n_type_units
2846 = dwarf2_per_objfile->n_allocated_type_units
2847 = elements / 3;
d467dd73 2848 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2849 = xmalloc (dwarf2_per_objfile->n_type_units
2850 * sizeof (struct signatured_type *));
1fd400ff 2851
673bfd45 2852 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2853
2854 for (i = 0; i < elements; i += 3)
2855 {
52dc124a
DE
2856 struct signatured_type *sig_type;
2857 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2858 void **slot;
2859
74a0d9f6
JK
2860 gdb_static_assert (sizeof (ULONGEST) >= 8);
2861 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2862 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2863 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2864 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2865 bytes += 3 * 8;
2866
52dc124a 2867 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2868 struct signatured_type);
52dc124a 2869 sig_type->signature = signature;
3019eac3
DE
2870 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2871 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2872 sig_type->per_cu.section = section;
52dc124a
DE
2873 sig_type->per_cu.offset.sect_off = offset;
2874 sig_type->per_cu.objfile = objfile;
2875 sig_type->per_cu.v.quick
1fd400ff
TT
2876 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2877 struct dwarf2_per_cu_quick_data);
2878
52dc124a
DE
2879 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2880 *slot = sig_type;
1fd400ff 2881
b4dd5633 2882 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2883 }
2884
673bfd45 2885 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2886}
2887
9291a0cd
TT
2888/* Read the address map data from the mapped index, and use it to
2889 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2890
9291a0cd
TT
2891static void
2892create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2893{
3e29f34a 2894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd
TT
2895 const gdb_byte *iter, *end;
2896 struct obstack temp_obstack;
2897 struct addrmap *mutable_map;
2898 struct cleanup *cleanup;
2899 CORE_ADDR baseaddr;
2900
2901 obstack_init (&temp_obstack);
2902 cleanup = make_cleanup_obstack_free (&temp_obstack);
2903 mutable_map = addrmap_create_mutable (&temp_obstack);
2904
2905 iter = index->address_table;
2906 end = iter + index->address_table_size;
2907
2908 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2909
2910 while (iter < end)
2911 {
2912 ULONGEST hi, lo, cu_index;
2913 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2914 iter += 8;
2915 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2916 iter += 8;
2917 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2918 iter += 4;
f652bce2 2919
24a55014 2920 if (lo > hi)
f652bce2 2921 {
24a55014
DE
2922 complaint (&symfile_complaints,
2923 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2924 hex_string (lo), hex_string (hi));
24a55014 2925 continue;
f652bce2 2926 }
24a55014
DE
2927
2928 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2929 {
2930 complaint (&symfile_complaints,
2931 _(".gdb_index address table has invalid CU number %u"),
2932 (unsigned) cu_index);
24a55014 2933 continue;
f652bce2 2934 }
24a55014 2935
3e29f34a
MR
2936 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
2937 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
2938 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
2939 }
2940
2941 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2942 &objfile->objfile_obstack);
2943 do_cleanups (cleanup);
2944}
2945
59d7bcaf
JK
2946/* The hash function for strings in the mapped index. This is the same as
2947 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2948 implementation. This is necessary because the hash function is tied to the
2949 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2950 SYMBOL_HASH_NEXT.
2951
2952 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2953
9291a0cd 2954static hashval_t
559a7a62 2955mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2956{
2957 const unsigned char *str = (const unsigned char *) p;
2958 hashval_t r = 0;
2959 unsigned char c;
2960
2961 while ((c = *str++) != 0)
559a7a62
JK
2962 {
2963 if (index_version >= 5)
2964 c = tolower (c);
2965 r = r * 67 + c - 113;
2966 }
9291a0cd
TT
2967
2968 return r;
2969}
2970
2971/* Find a slot in the mapped index INDEX for the object named NAME.
2972 If NAME is found, set *VEC_OUT to point to the CU vector in the
2973 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2974
9291a0cd
TT
2975static int
2976find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2977 offset_type **vec_out)
2978{
0cf03b49
JK
2979 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2980 offset_type hash;
9291a0cd 2981 offset_type slot, step;
559a7a62 2982 int (*cmp) (const char *, const char *);
9291a0cd 2983
0cf03b49
JK
2984 if (current_language->la_language == language_cplus
2985 || current_language->la_language == language_java
45280282
IB
2986 || current_language->la_language == language_fortran
2987 || current_language->la_language == language_d)
0cf03b49
JK
2988 {
2989 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2990 not contain any. */
a8719064 2991
72998fb3 2992 if (strchr (name, '(') != NULL)
0cf03b49 2993 {
72998fb3 2994 char *without_params = cp_remove_params (name);
0cf03b49 2995
72998fb3
DE
2996 if (without_params != NULL)
2997 {
2998 make_cleanup (xfree, without_params);
2999 name = without_params;
3000 }
0cf03b49
JK
3001 }
3002 }
3003
559a7a62 3004 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3005 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3006 simulate our NAME being searched is also lowercased. */
3007 hash = mapped_index_string_hash ((index->version == 4
3008 && case_sensitivity == case_sensitive_off
3009 ? 5 : index->version),
3010 name);
3011
3876f04e
DE
3012 slot = hash & (index->symbol_table_slots - 1);
3013 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3014 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3015
3016 for (;;)
3017 {
3018 /* Convert a slot number to an offset into the table. */
3019 offset_type i = 2 * slot;
3020 const char *str;
3876f04e 3021 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3022 {
3023 do_cleanups (back_to);
3024 return 0;
3025 }
9291a0cd 3026
3876f04e 3027 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3028 if (!cmp (name, str))
9291a0cd
TT
3029 {
3030 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3031 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3032 do_cleanups (back_to);
9291a0cd
TT
3033 return 1;
3034 }
3035
3876f04e 3036 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3037 }
3038}
3039
2ec9a5e0
TT
3040/* A helper function that reads the .gdb_index from SECTION and fills
3041 in MAP. FILENAME is the name of the file containing the section;
3042 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3043 ok to use deprecated sections.
3044
3045 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3046 out parameters that are filled in with information about the CU and
3047 TU lists in the section.
3048
3049 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3050
9291a0cd 3051static int
2ec9a5e0
TT
3052read_index_from_section (struct objfile *objfile,
3053 const char *filename,
3054 int deprecated_ok,
3055 struct dwarf2_section_info *section,
3056 struct mapped_index *map,
3057 const gdb_byte **cu_list,
3058 offset_type *cu_list_elements,
3059 const gdb_byte **types_list,
3060 offset_type *types_list_elements)
9291a0cd 3061{
948f8e3d 3062 const gdb_byte *addr;
2ec9a5e0 3063 offset_type version;
b3b272e1 3064 offset_type *metadata;
1fd400ff 3065 int i;
9291a0cd 3066
2ec9a5e0 3067 if (dwarf2_section_empty_p (section))
9291a0cd 3068 return 0;
82430852
JK
3069
3070 /* Older elfutils strip versions could keep the section in the main
3071 executable while splitting it for the separate debug info file. */
a32a8923 3072 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3073 return 0;
3074
2ec9a5e0 3075 dwarf2_read_section (objfile, section);
9291a0cd 3076
2ec9a5e0 3077 addr = section->buffer;
9291a0cd 3078 /* Version check. */
1fd400ff 3079 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3080 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3081 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3082 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3083 indices. */
831adc1f 3084 if (version < 4)
481860b3
GB
3085 {
3086 static int warning_printed = 0;
3087 if (!warning_printed)
3088 {
3089 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3090 filename);
481860b3
GB
3091 warning_printed = 1;
3092 }
3093 return 0;
3094 }
3095 /* Index version 4 uses a different hash function than index version
3096 5 and later.
3097
3098 Versions earlier than 6 did not emit psymbols for inlined
3099 functions. Using these files will cause GDB not to be able to
3100 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3101 indices unless the user has done
3102 "set use-deprecated-index-sections on". */
2ec9a5e0 3103 if (version < 6 && !deprecated_ok)
481860b3
GB
3104 {
3105 static int warning_printed = 0;
3106 if (!warning_printed)
3107 {
e615022a
DE
3108 warning (_("\
3109Skipping deprecated .gdb_index section in %s.\n\
3110Do \"set use-deprecated-index-sections on\" before the file is read\n\
3111to use the section anyway."),
2ec9a5e0 3112 filename);
481860b3
GB
3113 warning_printed = 1;
3114 }
3115 return 0;
3116 }
796a7ff8 3117 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3118 of the TU (for symbols coming from TUs),
3119 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3120 Plus gold-generated indices can have duplicate entries for global symbols,
3121 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3122 These are just performance bugs, and we can't distinguish gdb-generated
3123 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3124
481860b3 3125 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3126 longer backward compatible. */
796a7ff8 3127 if (version > 8)
594e8718 3128 return 0;
9291a0cd 3129
559a7a62 3130 map->version = version;
2ec9a5e0 3131 map->total_size = section->size;
9291a0cd
TT
3132
3133 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3134
3135 i = 0;
2ec9a5e0
TT
3136 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3137 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3138 / 8);
1fd400ff
TT
3139 ++i;
3140
2ec9a5e0
TT
3141 *types_list = addr + MAYBE_SWAP (metadata[i]);
3142 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3143 - MAYBE_SWAP (metadata[i]))
3144 / 8);
987d643c 3145 ++i;
1fd400ff
TT
3146
3147 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3148 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3149 - MAYBE_SWAP (metadata[i]));
3150 ++i;
3151
3876f04e
DE
3152 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3153 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3154 - MAYBE_SWAP (metadata[i]))
3155 / (2 * sizeof (offset_type)));
1fd400ff 3156 ++i;
9291a0cd 3157
f9d83a0b 3158 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3159
2ec9a5e0
TT
3160 return 1;
3161}
3162
3163
3164/* Read the index file. If everything went ok, initialize the "quick"
3165 elements of all the CUs and return 1. Otherwise, return 0. */
3166
3167static int
3168dwarf2_read_index (struct objfile *objfile)
3169{
3170 struct mapped_index local_map, *map;
3171 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3172 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3173 struct dwz_file *dwz;
2ec9a5e0 3174
4262abfb 3175 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3176 use_deprecated_index_sections,
3177 &dwarf2_per_objfile->gdb_index, &local_map,
3178 &cu_list, &cu_list_elements,
3179 &types_list, &types_list_elements))
3180 return 0;
3181
0fefef59 3182 /* Don't use the index if it's empty. */
2ec9a5e0 3183 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3184 return 0;
3185
2ec9a5e0
TT
3186 /* If there is a .dwz file, read it so we can get its CU list as
3187 well. */
4db1a1dc
TT
3188 dwz = dwarf2_get_dwz_file ();
3189 if (dwz != NULL)
2ec9a5e0 3190 {
2ec9a5e0
TT
3191 struct mapped_index dwz_map;
3192 const gdb_byte *dwz_types_ignore;
3193 offset_type dwz_types_elements_ignore;
3194
3195 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3196 1,
3197 &dwz->gdb_index, &dwz_map,
3198 &dwz_list, &dwz_list_elements,
3199 &dwz_types_ignore,
3200 &dwz_types_elements_ignore))
3201 {
3202 warning (_("could not read '.gdb_index' section from %s; skipping"),
3203 bfd_get_filename (dwz->dwz_bfd));
3204 return 0;
3205 }
3206 }
3207
74a0d9f6
JK
3208 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3209 dwz_list_elements);
1fd400ff 3210
8b70b953
TT
3211 if (types_list_elements)
3212 {
3213 struct dwarf2_section_info *section;
3214
3215 /* We can only handle a single .debug_types when we have an
3216 index. */
3217 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3218 return 0;
3219
3220 section = VEC_index (dwarf2_section_info_def,
3221 dwarf2_per_objfile->types, 0);
3222
74a0d9f6
JK
3223 create_signatured_type_table_from_index (objfile, section, types_list,
3224 types_list_elements);
8b70b953 3225 }
9291a0cd 3226
2ec9a5e0
TT
3227 create_addrmap_from_index (objfile, &local_map);
3228
3229 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
3230 *map = local_map;
9291a0cd
TT
3231
3232 dwarf2_per_objfile->index_table = map;
3233 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3234 dwarf2_per_objfile->quick_file_names_table =
3235 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3236
3237 return 1;
3238}
3239
3240/* A helper for the "quick" functions which sets the global
3241 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3242
9291a0cd
TT
3243static void
3244dw2_setup (struct objfile *objfile)
3245{
3246 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
3247 gdb_assert (dwarf2_per_objfile);
3248}
3249
dee91e82 3250/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3251
dee91e82
DE
3252static void
3253dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3254 const gdb_byte *info_ptr,
dee91e82
DE
3255 struct die_info *comp_unit_die,
3256 int has_children,
3257 void *data)
9291a0cd 3258{
dee91e82
DE
3259 struct dwarf2_cu *cu = reader->cu;
3260 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3261 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3262 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 3263 struct line_header *lh;
9291a0cd 3264 struct attribute *attr;
dee91e82 3265 int i;
15d034d0 3266 const char *name, *comp_dir;
7b9f3c50
DE
3267 void **slot;
3268 struct quick_file_names *qfn;
3269 unsigned int line_offset;
9291a0cd 3270
0186c6a7
DE
3271 gdb_assert (! this_cu->is_debug_types);
3272
07261596
TT
3273 /* Our callers never want to match partial units -- instead they
3274 will match the enclosing full CU. */
3275 if (comp_unit_die->tag == DW_TAG_partial_unit)
3276 {
3277 this_cu->v.quick->no_file_data = 1;
3278 return;
3279 }
3280
0186c6a7 3281 lh_cu = this_cu;
7b9f3c50
DE
3282 lh = NULL;
3283 slot = NULL;
3284 line_offset = 0;
dee91e82
DE
3285
3286 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3287 if (attr)
3288 {
7b9f3c50
DE
3289 struct quick_file_names find_entry;
3290
3291 line_offset = DW_UNSND (attr);
3292
3293 /* We may have already read in this line header (TU line header sharing).
3294 If we have we're done. */
094b34ac
DE
3295 find_entry.hash.dwo_unit = cu->dwo_unit;
3296 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3297 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3298 &find_entry, INSERT);
3299 if (*slot != NULL)
3300 {
094b34ac 3301 lh_cu->v.quick->file_names = *slot;
dee91e82 3302 return;
7b9f3c50
DE
3303 }
3304
3019eac3 3305 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3306 }
3307 if (lh == NULL)
3308 {
094b34ac 3309 lh_cu->v.quick->no_file_data = 1;
dee91e82 3310 return;
9291a0cd
TT
3311 }
3312
7b9f3c50 3313 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
3314 qfn->hash.dwo_unit = cu->dwo_unit;
3315 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3316 gdb_assert (slot != NULL);
3317 *slot = qfn;
9291a0cd 3318
dee91e82 3319 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 3320
7b9f3c50
DE
3321 qfn->num_file_names = lh->num_file_names;
3322 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
3323 lh->num_file_names * sizeof (char *));
9291a0cd 3324 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
3325 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3326 qfn->real_names = NULL;
9291a0cd 3327
7b9f3c50 3328 free_line_header (lh);
7b9f3c50 3329
094b34ac 3330 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3331}
3332
3333/* A helper for the "quick" functions which attempts to read the line
3334 table for THIS_CU. */
3335
3336static struct quick_file_names *
e4a48d9d 3337dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3338{
0186c6a7
DE
3339 /* This should never be called for TUs. */
3340 gdb_assert (! this_cu->is_debug_types);
3341 /* Nor type unit groups. */
3342 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3343
dee91e82
DE
3344 if (this_cu->v.quick->file_names != NULL)
3345 return this_cu->v.quick->file_names;
3346 /* If we know there is no line data, no point in looking again. */
3347 if (this_cu->v.quick->no_file_data)
3348 return NULL;
3349
0186c6a7 3350 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3351
3352 if (this_cu->v.quick->no_file_data)
3353 return NULL;
3354 return this_cu->v.quick->file_names;
9291a0cd
TT
3355}
3356
3357/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3358 real path for a given file name from the line table. */
2fdf6df6 3359
9291a0cd 3360static const char *
7b9f3c50
DE
3361dw2_get_real_path (struct objfile *objfile,
3362 struct quick_file_names *qfn, int index)
9291a0cd 3363{
7b9f3c50
DE
3364 if (qfn->real_names == NULL)
3365 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3366 qfn->num_file_names, const char *);
9291a0cd 3367
7b9f3c50
DE
3368 if (qfn->real_names[index] == NULL)
3369 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3370
7b9f3c50 3371 return qfn->real_names[index];
9291a0cd
TT
3372}
3373
3374static struct symtab *
3375dw2_find_last_source_symtab (struct objfile *objfile)
3376{
43f3e411 3377 struct compunit_symtab *cust;
9291a0cd 3378 int index;
ae2de4f8 3379
9291a0cd
TT
3380 dw2_setup (objfile);
3381 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3382 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3383 if (cust == NULL)
3384 return NULL;
3385 return compunit_primary_filetab (cust);
9291a0cd
TT
3386}
3387
7b9f3c50
DE
3388/* Traversal function for dw2_forget_cached_source_info. */
3389
3390static int
3391dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3392{
7b9f3c50 3393 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3394
7b9f3c50 3395 if (file_data->real_names)
9291a0cd 3396 {
7b9f3c50 3397 int i;
9291a0cd 3398
7b9f3c50 3399 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3400 {
7b9f3c50
DE
3401 xfree ((void*) file_data->real_names[i]);
3402 file_data->real_names[i] = NULL;
9291a0cd
TT
3403 }
3404 }
7b9f3c50
DE
3405
3406 return 1;
3407}
3408
3409static void
3410dw2_forget_cached_source_info (struct objfile *objfile)
3411{
3412 dw2_setup (objfile);
3413
3414 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3415 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3416}
3417
f8eba3c6
TT
3418/* Helper function for dw2_map_symtabs_matching_filename that expands
3419 the symtabs and calls the iterator. */
3420
3421static int
3422dw2_map_expand_apply (struct objfile *objfile,
3423 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3424 const char *name, const char *real_path,
f8eba3c6
TT
3425 int (*callback) (struct symtab *, void *),
3426 void *data)
3427{
43f3e411 3428 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3429
3430 /* Don't visit already-expanded CUs. */
43f3e411 3431 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3432 return 0;
3433
3434 /* This may expand more than one symtab, and we want to iterate over
3435 all of them. */
a0f42c21 3436 dw2_instantiate_symtab (per_cu);
f8eba3c6 3437
f5b95b50 3438 return iterate_over_some_symtabs (name, real_path, callback, data,
43f3e411 3439 objfile->compunit_symtabs, last_made);
f8eba3c6
TT
3440}
3441
3442/* Implementation of the map_symtabs_matching_filename method. */
3443
9291a0cd 3444static int
f8eba3c6 3445dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3446 const char *real_path,
f8eba3c6
TT
3447 int (*callback) (struct symtab *, void *),
3448 void *data)
9291a0cd
TT
3449{
3450 int i;
c011a4f4 3451 const char *name_basename = lbasename (name);
9291a0cd
TT
3452
3453 dw2_setup (objfile);
ae2de4f8 3454
848e3e78
DE
3455 /* The rule is CUs specify all the files, including those used by
3456 any TU, so there's no need to scan TUs here. */
f4dc4d17 3457
848e3e78 3458 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3459 {
3460 int j;
8832e7e3 3461 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3462 struct quick_file_names *file_data;
9291a0cd 3463
3d7bb9d9 3464 /* We only need to look at symtabs not already expanded. */
43f3e411 3465 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3466 continue;
3467
e4a48d9d 3468 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3469 if (file_data == NULL)
9291a0cd
TT
3470 continue;
3471
7b9f3c50 3472 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3473 {
7b9f3c50 3474 const char *this_name = file_data->file_names[j];
da235a7c 3475 const char *this_real_name;
9291a0cd 3476
af529f8f 3477 if (compare_filenames_for_search (this_name, name))
9291a0cd 3478 {
f5b95b50 3479 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3480 callback, data))
3481 return 1;
288e77a7 3482 continue;
4aac40c8 3483 }
9291a0cd 3484
c011a4f4
DE
3485 /* Before we invoke realpath, which can get expensive when many
3486 files are involved, do a quick comparison of the basenames. */
3487 if (! basenames_may_differ
3488 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3489 continue;
3490
da235a7c
JK
3491 this_real_name = dw2_get_real_path (objfile, file_data, j);
3492 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3493 {
da235a7c
JK
3494 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3495 callback, data))
3496 return 1;
288e77a7 3497 continue;
da235a7c 3498 }
9291a0cd 3499
da235a7c
JK
3500 if (real_path != NULL)
3501 {
af529f8f
JK
3502 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3503 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3504 if (this_real_name != NULL
af529f8f 3505 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3506 {
f5b95b50 3507 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3508 callback, data))
3509 return 1;
288e77a7 3510 continue;
9291a0cd
TT
3511 }
3512 }
3513 }
3514 }
3515
9291a0cd
TT
3516 return 0;
3517}
3518
da51c347
DE
3519/* Struct used to manage iterating over all CUs looking for a symbol. */
3520
3521struct dw2_symtab_iterator
9291a0cd 3522{
da51c347
DE
3523 /* The internalized form of .gdb_index. */
3524 struct mapped_index *index;
3525 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3526 int want_specific_block;
3527 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3528 Unused if !WANT_SPECIFIC_BLOCK. */
3529 int block_index;
3530 /* The kind of symbol we're looking for. */
3531 domain_enum domain;
3532 /* The list of CUs from the index entry of the symbol,
3533 or NULL if not found. */
3534 offset_type *vec;
3535 /* The next element in VEC to look at. */
3536 int next;
3537 /* The number of elements in VEC, or zero if there is no match. */
3538 int length;
8943b874
DE
3539 /* Have we seen a global version of the symbol?
3540 If so we can ignore all further global instances.
3541 This is to work around gold/15646, inefficient gold-generated
3542 indices. */
3543 int global_seen;
da51c347 3544};
9291a0cd 3545
da51c347
DE
3546/* Initialize the index symtab iterator ITER.
3547 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3548 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3549
9291a0cd 3550static void
da51c347
DE
3551dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3552 struct mapped_index *index,
3553 int want_specific_block,
3554 int block_index,
3555 domain_enum domain,
3556 const char *name)
3557{
3558 iter->index = index;
3559 iter->want_specific_block = want_specific_block;
3560 iter->block_index = block_index;
3561 iter->domain = domain;
3562 iter->next = 0;
8943b874 3563 iter->global_seen = 0;
da51c347
DE
3564
3565 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3566 iter->length = MAYBE_SWAP (*iter->vec);
3567 else
3568 {
3569 iter->vec = NULL;
3570 iter->length = 0;
3571 }
3572}
3573
3574/* Return the next matching CU or NULL if there are no more. */
3575
3576static struct dwarf2_per_cu_data *
3577dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3578{
3579 for ( ; iter->next < iter->length; ++iter->next)
3580 {
3581 offset_type cu_index_and_attrs =
3582 MAYBE_SWAP (iter->vec[iter->next + 1]);
3583 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3584 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3585 int want_static = iter->block_index != GLOBAL_BLOCK;
3586 /* This value is only valid for index versions >= 7. */
3587 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3588 gdb_index_symbol_kind symbol_kind =
3589 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3590 /* Only check the symbol attributes if they're present.
3591 Indices prior to version 7 don't record them,
3592 and indices >= 7 may elide them for certain symbols
3593 (gold does this). */
3594 int attrs_valid =
3595 (iter->index->version >= 7
3596 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3597
3190f0c6
DE
3598 /* Don't crash on bad data. */
3599 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3600 + dwarf2_per_objfile->n_type_units))
3601 {
3602 complaint (&symfile_complaints,
3603 _(".gdb_index entry has bad CU index"
4262abfb
JK
3604 " [in module %s]"),
3605 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3606 continue;
3607 }
3608
8832e7e3 3609 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3610
da51c347 3611 /* Skip if already read in. */
43f3e411 3612 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3613 continue;
3614
8943b874
DE
3615 /* Check static vs global. */
3616 if (attrs_valid)
3617 {
3618 if (iter->want_specific_block
3619 && want_static != is_static)
3620 continue;
3621 /* Work around gold/15646. */
3622 if (!is_static && iter->global_seen)
3623 continue;
3624 if (!is_static)
3625 iter->global_seen = 1;
3626 }
da51c347
DE
3627
3628 /* Only check the symbol's kind if it has one. */
3629 if (attrs_valid)
3630 {
3631 switch (iter->domain)
3632 {
3633 case VAR_DOMAIN:
3634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3635 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3636 /* Some types are also in VAR_DOMAIN. */
3637 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3638 continue;
3639 break;
3640 case STRUCT_DOMAIN:
3641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3642 continue;
3643 break;
3644 case LABEL_DOMAIN:
3645 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3646 continue;
3647 break;
3648 default:
3649 break;
3650 }
3651 }
3652
3653 ++iter->next;
3654 return per_cu;
3655 }
3656
3657 return NULL;
3658}
3659
43f3e411 3660static struct compunit_symtab *
da51c347
DE
3661dw2_lookup_symbol (struct objfile *objfile, int block_index,
3662 const char *name, domain_enum domain)
9291a0cd 3663{
43f3e411 3664 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3665 struct mapped_index *index;
3666
9291a0cd
TT
3667 dw2_setup (objfile);
3668
156942c7
DE
3669 index = dwarf2_per_objfile->index_table;
3670
da51c347 3671 /* index is NULL if OBJF_READNOW. */
156942c7 3672 if (index)
9291a0cd 3673 {
da51c347
DE
3674 struct dw2_symtab_iterator iter;
3675 struct dwarf2_per_cu_data *per_cu;
3676
3677 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3678
da51c347 3679 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3680 {
b2e2f908 3681 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3682 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3683 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3684 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3685
b2e2f908
DE
3686 sym = block_find_symbol (block, name, domain,
3687 block_find_non_opaque_type_preferred,
3688 &with_opaque);
3689
da51c347
DE
3690 /* Some caution must be observed with overloaded functions
3691 and methods, since the index will not contain any overload
3692 information (but NAME might contain it). */
da51c347 3693
b2e2f908
DE
3694 if (sym != NULL
3695 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3696 return stab;
3697 if (with_opaque != NULL
3698 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3699 stab_best = stab;
da51c347
DE
3700
3701 /* Keep looking through other CUs. */
9291a0cd
TT
3702 }
3703 }
9291a0cd 3704
da51c347 3705 return stab_best;
9291a0cd
TT
3706}
3707
3708static void
3709dw2_print_stats (struct objfile *objfile)
3710{
e4a48d9d 3711 int i, total, count;
9291a0cd
TT
3712
3713 dw2_setup (objfile);
e4a48d9d 3714 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3715 count = 0;
e4a48d9d 3716 for (i = 0; i < total; ++i)
9291a0cd 3717 {
8832e7e3 3718 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3719
43f3e411 3720 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3721 ++count;
3722 }
e4a48d9d 3723 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3724 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3725}
3726
779bd270
DE
3727/* This dumps minimal information about the index.
3728 It is called via "mt print objfiles".
3729 One use is to verify .gdb_index has been loaded by the
3730 gdb.dwarf2/gdb-index.exp testcase. */
3731
9291a0cd
TT
3732static void
3733dw2_dump (struct objfile *objfile)
3734{
779bd270
DE
3735 dw2_setup (objfile);
3736 gdb_assert (dwarf2_per_objfile->using_index);
3737 printf_filtered (".gdb_index:");
3738 if (dwarf2_per_objfile->index_table != NULL)
3739 {
3740 printf_filtered (" version %d\n",
3741 dwarf2_per_objfile->index_table->version);
3742 }
3743 else
3744 printf_filtered (" faked for \"readnow\"\n");
3745 printf_filtered ("\n");
9291a0cd
TT
3746}
3747
3748static void
3189cb12
DE
3749dw2_relocate (struct objfile *objfile,
3750 const struct section_offsets *new_offsets,
3751 const struct section_offsets *delta)
9291a0cd
TT
3752{
3753 /* There's nothing to relocate here. */
3754}
3755
3756static void
3757dw2_expand_symtabs_for_function (struct objfile *objfile,
3758 const char *func_name)
3759{
da51c347
DE
3760 struct mapped_index *index;
3761
3762 dw2_setup (objfile);
3763
3764 index = dwarf2_per_objfile->index_table;
3765
3766 /* index is NULL if OBJF_READNOW. */
3767 if (index)
3768 {
3769 struct dw2_symtab_iterator iter;
3770 struct dwarf2_per_cu_data *per_cu;
3771
3772 /* Note: It doesn't matter what we pass for block_index here. */
3773 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3774 func_name);
3775
3776 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3777 dw2_instantiate_symtab (per_cu);
3778 }
9291a0cd
TT
3779}
3780
3781static void
3782dw2_expand_all_symtabs (struct objfile *objfile)
3783{
3784 int i;
3785
3786 dw2_setup (objfile);
1fd400ff
TT
3787
3788 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3789 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3790 {
8832e7e3 3791 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3792
a0f42c21 3793 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3794 }
3795}
3796
3797static void
652a8996
JK
3798dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3799 const char *fullname)
9291a0cd
TT
3800{
3801 int i;
3802
3803 dw2_setup (objfile);
d4637a04
DE
3804
3805 /* We don't need to consider type units here.
3806 This is only called for examining code, e.g. expand_line_sal.
3807 There can be an order of magnitude (or more) more type units
3808 than comp units, and we avoid them if we can. */
3809
3810 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3811 {
3812 int j;
8832e7e3 3813 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 3814 struct quick_file_names *file_data;
9291a0cd 3815
3d7bb9d9 3816 /* We only need to look at symtabs not already expanded. */
43f3e411 3817 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3818 continue;
3819
e4a48d9d 3820 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3821 if (file_data == NULL)
9291a0cd
TT
3822 continue;
3823
7b9f3c50 3824 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3825 {
652a8996
JK
3826 const char *this_fullname = file_data->file_names[j];
3827
3828 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3829 {
a0f42c21 3830 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3831 break;
3832 }
3833 }
3834 }
3835}
3836
9291a0cd 3837static void
ade7ed9e 3838dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 3839 const char * name, domain_enum domain,
ade7ed9e 3840 int global,
40658b94
PH
3841 int (*callback) (struct block *,
3842 struct symbol *, void *),
2edb89d3
JK
3843 void *data, symbol_compare_ftype *match,
3844 symbol_compare_ftype *ordered_compare)
9291a0cd 3845{
40658b94 3846 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3847 current language is Ada for a non-Ada objfile using GNU index. As Ada
3848 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3849}
3850
3851static void
f8eba3c6
TT
3852dw2_expand_symtabs_matching
3853 (struct objfile *objfile,
206f2a57
DE
3854 expand_symtabs_file_matcher_ftype *file_matcher,
3855 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
276d885b 3856 expand_symtabs_exp_notify_ftype *expansion_notify,
f8eba3c6
TT
3857 enum search_domain kind,
3858 void *data)
9291a0cd
TT
3859{
3860 int i;
3861 offset_type iter;
4b5246aa 3862 struct mapped_index *index;
9291a0cd
TT
3863
3864 dw2_setup (objfile);
ae2de4f8
DE
3865
3866 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3867 if (!dwarf2_per_objfile->index_table)
3868 return;
4b5246aa 3869 index = dwarf2_per_objfile->index_table;
9291a0cd 3870
7b08b9eb 3871 if (file_matcher != NULL)
24c79950
TT
3872 {
3873 struct cleanup *cleanup;
3874 htab_t visited_found, visited_not_found;
3875
3876 visited_found = htab_create_alloc (10,
3877 htab_hash_pointer, htab_eq_pointer,
3878 NULL, xcalloc, xfree);
3879 cleanup = make_cleanup_htab_delete (visited_found);
3880 visited_not_found = htab_create_alloc (10,
3881 htab_hash_pointer, htab_eq_pointer,
3882 NULL, xcalloc, xfree);
3883 make_cleanup_htab_delete (visited_not_found);
3884
848e3e78
DE
3885 /* The rule is CUs specify all the files, including those used by
3886 any TU, so there's no need to scan TUs here. */
3887
3888 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3889 {
3890 int j;
8832e7e3 3891 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
3892 struct quick_file_names *file_data;
3893 void **slot;
7b08b9eb 3894
61d96d7e
DE
3895 QUIT;
3896
24c79950 3897 per_cu->v.quick->mark = 0;
3d7bb9d9 3898
24c79950 3899 /* We only need to look at symtabs not already expanded. */
43f3e411 3900 if (per_cu->v.quick->compunit_symtab)
24c79950 3901 continue;
7b08b9eb 3902
e4a48d9d 3903 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3904 if (file_data == NULL)
3905 continue;
7b08b9eb 3906
24c79950
TT
3907 if (htab_find (visited_not_found, file_data) != NULL)
3908 continue;
3909 else if (htab_find (visited_found, file_data) != NULL)
3910 {
3911 per_cu->v.quick->mark = 1;
3912 continue;
3913 }
3914
3915 for (j = 0; j < file_data->num_file_names; ++j)
3916 {
da235a7c
JK
3917 const char *this_real_name;
3918
fbd9ab74 3919 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3920 {
3921 per_cu->v.quick->mark = 1;
3922 break;
3923 }
da235a7c
JK
3924
3925 /* Before we invoke realpath, which can get expensive when many
3926 files are involved, do a quick comparison of the basenames. */
3927 if (!basenames_may_differ
3928 && !file_matcher (lbasename (file_data->file_names[j]),
3929 data, 1))
3930 continue;
3931
3932 this_real_name = dw2_get_real_path (objfile, file_data, j);
3933 if (file_matcher (this_real_name, data, 0))
3934 {
3935 per_cu->v.quick->mark = 1;
3936 break;
3937 }
24c79950
TT
3938 }
3939
3940 slot = htab_find_slot (per_cu->v.quick->mark
3941 ? visited_found
3942 : visited_not_found,
3943 file_data, INSERT);
3944 *slot = file_data;
3945 }
3946
3947 do_cleanups (cleanup);
3948 }
9291a0cd 3949
3876f04e 3950 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3951 {
3952 offset_type idx = 2 * iter;
3953 const char *name;
3954 offset_type *vec, vec_len, vec_idx;
8943b874 3955 int global_seen = 0;
9291a0cd 3956
61d96d7e
DE
3957 QUIT;
3958
3876f04e 3959 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3960 continue;
3961
3876f04e 3962 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3963
206f2a57 3964 if (! (*symbol_matcher) (name, data))
9291a0cd
TT
3965 continue;
3966
3967 /* The name was matched, now expand corresponding CUs that were
3968 marked. */
4b5246aa 3969 vec = (offset_type *) (index->constant_pool
3876f04e 3970 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3971 vec_len = MAYBE_SWAP (vec[0]);
3972 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3973 {
e254ef6a 3974 struct dwarf2_per_cu_data *per_cu;
156942c7 3975 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
3976 /* This value is only valid for index versions >= 7. */
3977 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
3978 gdb_index_symbol_kind symbol_kind =
3979 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3980 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3981 /* Only check the symbol attributes if they're present.
3982 Indices prior to version 7 don't record them,
3983 and indices >= 7 may elide them for certain symbols
3984 (gold does this). */
3985 int attrs_valid =
3986 (index->version >= 7
3987 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3988
8943b874
DE
3989 /* Work around gold/15646. */
3990 if (attrs_valid)
3991 {
3992 if (!is_static && global_seen)
3993 continue;
3994 if (!is_static)
3995 global_seen = 1;
3996 }
3997
3190f0c6
DE
3998 /* Only check the symbol's kind if it has one. */
3999 if (attrs_valid)
156942c7
DE
4000 {
4001 switch (kind)
4002 {
4003 case VARIABLES_DOMAIN:
4004 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4005 continue;
4006 break;
4007 case FUNCTIONS_DOMAIN:
4008 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4009 continue;
4010 break;
4011 case TYPES_DOMAIN:
4012 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4013 continue;
4014 break;
4015 default:
4016 break;
4017 }
4018 }
4019
3190f0c6
DE
4020 /* Don't crash on bad data. */
4021 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4022 + dwarf2_per_objfile->n_type_units))
4023 {
4024 complaint (&symfile_complaints,
4025 _(".gdb_index entry has bad CU index"
4262abfb 4026 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4027 continue;
4028 }
4029
8832e7e3 4030 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4031 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4032 {
4033 int symtab_was_null =
4034 (per_cu->v.quick->compunit_symtab == NULL);
4035
4036 dw2_instantiate_symtab (per_cu);
4037
4038 if (expansion_notify != NULL
4039 && symtab_was_null
4040 && per_cu->v.quick->compunit_symtab != NULL)
4041 {
4042 expansion_notify (per_cu->v.quick->compunit_symtab,
4043 data);
4044 }
4045 }
9291a0cd
TT
4046 }
4047 }
4048}
4049
43f3e411 4050/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4051 symtab. */
4052
43f3e411
DE
4053static struct compunit_symtab *
4054recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4055 CORE_ADDR pc)
9703b513
TT
4056{
4057 int i;
4058
43f3e411
DE
4059 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4060 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4061 return cust;
9703b513 4062
43f3e411 4063 if (cust->includes == NULL)
a3ec0bb1
DE
4064 return NULL;
4065
43f3e411 4066 for (i = 0; cust->includes[i]; ++i)
9703b513 4067 {
43f3e411 4068 struct compunit_symtab *s = cust->includes[i];
9703b513 4069
43f3e411 4070 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4071 if (s != NULL)
4072 return s;
4073 }
4074
4075 return NULL;
4076}
4077
43f3e411
DE
4078static struct compunit_symtab *
4079dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4080 struct bound_minimal_symbol msymbol,
4081 CORE_ADDR pc,
4082 struct obj_section *section,
4083 int warn_if_readin)
9291a0cd
TT
4084{
4085 struct dwarf2_per_cu_data *data;
43f3e411 4086 struct compunit_symtab *result;
9291a0cd
TT
4087
4088 dw2_setup (objfile);
4089
4090 if (!objfile->psymtabs_addrmap)
4091 return NULL;
4092
4093 data = addrmap_find (objfile->psymtabs_addrmap, pc);
4094 if (!data)
4095 return NULL;
4096
43f3e411 4097 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4098 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4099 paddress (get_objfile_arch (objfile), pc));
4100
43f3e411
DE
4101 result
4102 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4103 pc);
9703b513
TT
4104 gdb_assert (result != NULL);
4105 return result;
9291a0cd
TT
4106}
4107
9291a0cd 4108static void
44b13c5a 4109dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4110 void *data, int need_fullname)
9291a0cd
TT
4111{
4112 int i;
24c79950
TT
4113 struct cleanup *cleanup;
4114 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4115 NULL, xcalloc, xfree);
9291a0cd 4116
24c79950 4117 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 4118 dw2_setup (objfile);
ae2de4f8 4119
848e3e78
DE
4120 /* The rule is CUs specify all the files, including those used by
4121 any TU, so there's no need to scan TUs here.
4122 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 4123
848e3e78 4124 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950 4125 {
8832e7e3 4126 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
24c79950 4127
43f3e411 4128 if (per_cu->v.quick->compunit_symtab)
24c79950
TT
4129 {
4130 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
4131 INSERT);
4132
4133 *slot = per_cu->v.quick->file_names;
4134 }
4135 }
4136
848e3e78 4137 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4138 {
4139 int j;
8832e7e3 4140 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4141 struct quick_file_names *file_data;
24c79950 4142 void **slot;
9291a0cd 4143
3d7bb9d9 4144 /* We only need to look at symtabs not already expanded. */
43f3e411 4145 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4146 continue;
4147
e4a48d9d 4148 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4149 if (file_data == NULL)
9291a0cd
TT
4150 continue;
4151
24c79950
TT
4152 slot = htab_find_slot (visited, file_data, INSERT);
4153 if (*slot)
4154 {
4155 /* Already visited. */
4156 continue;
4157 }
4158 *slot = file_data;
4159
7b9f3c50 4160 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4161 {
74e2f255
DE
4162 const char *this_real_name;
4163
4164 if (need_fullname)
4165 this_real_name = dw2_get_real_path (objfile, file_data, j);
4166 else
4167 this_real_name = NULL;
7b9f3c50 4168 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
4169 }
4170 }
24c79950
TT
4171
4172 do_cleanups (cleanup);
9291a0cd
TT
4173}
4174
4175static int
4176dw2_has_symbols (struct objfile *objfile)
4177{
4178 return 1;
4179}
4180
4181const struct quick_symbol_functions dwarf2_gdb_index_functions =
4182{
4183 dw2_has_symbols,
4184 dw2_find_last_source_symtab,
4185 dw2_forget_cached_source_info,
f8eba3c6 4186 dw2_map_symtabs_matching_filename,
9291a0cd 4187 dw2_lookup_symbol,
9291a0cd
TT
4188 dw2_print_stats,
4189 dw2_dump,
4190 dw2_relocate,
4191 dw2_expand_symtabs_for_function,
4192 dw2_expand_all_symtabs,
652a8996 4193 dw2_expand_symtabs_with_fullname,
40658b94 4194 dw2_map_matching_symbols,
9291a0cd 4195 dw2_expand_symtabs_matching,
43f3e411 4196 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4197 dw2_map_symbol_filenames
4198};
4199
4200/* Initialize for reading DWARF for this objfile. Return 0 if this
4201 file will use psymtabs, or 1 if using the GNU index. */
4202
4203int
4204dwarf2_initialize_objfile (struct objfile *objfile)
4205{
4206 /* If we're about to read full symbols, don't bother with the
4207 indices. In this case we also don't care if some other debug
4208 format is making psymtabs, because they are all about to be
4209 expanded anyway. */
4210 if ((objfile->flags & OBJF_READNOW))
4211 {
4212 int i;
4213
4214 dwarf2_per_objfile->using_index = 1;
4215 create_all_comp_units (objfile);
0e50663e 4216 create_all_type_units (objfile);
7b9f3c50
DE
4217 dwarf2_per_objfile->quick_file_names_table =
4218 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4219
1fd400ff 4220 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4221 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4222 {
8832e7e3 4223 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4224
e254ef6a
DE
4225 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4226 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4227 }
4228
4229 /* Return 1 so that gdb sees the "quick" functions. However,
4230 these functions will be no-ops because we will have expanded
4231 all symtabs. */
4232 return 1;
4233 }
4234
4235 if (dwarf2_read_index (objfile))
4236 return 1;
4237
9291a0cd
TT
4238 return 0;
4239}
4240
4241\f
4242
dce234bc
PP
4243/* Build a partial symbol table. */
4244
4245void
f29dff0a 4246dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4247{
c9bf0622 4248
f29dff0a 4249 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4250 {
4251 init_psymbol_list (objfile, 1024);
4252 }
4253
492d29ea 4254 TRY
c9bf0622
TT
4255 {
4256 /* This isn't really ideal: all the data we allocate on the
4257 objfile's obstack is still uselessly kept around. However,
4258 freeing it seems unsafe. */
4259 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
4260
4261 dwarf2_build_psymtabs_hard (objfile);
4262 discard_cleanups (cleanups);
4263 }
492d29ea
PA
4264 CATCH (except, RETURN_MASK_ERROR)
4265 {
4266 exception_print (gdb_stderr, except);
4267 }
4268 END_CATCH
c906108c 4269}
c906108c 4270
1ce1cefd
DE
4271/* Return the total length of the CU described by HEADER. */
4272
4273static unsigned int
4274get_cu_length (const struct comp_unit_head *header)
4275{
4276 return header->initial_length_size + header->length;
4277}
4278
45452591
DE
4279/* Return TRUE if OFFSET is within CU_HEADER. */
4280
4281static inline int
b64f50a1 4282offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 4283{
b64f50a1 4284 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 4285 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 4286
b64f50a1 4287 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
4288}
4289
3b80fe9b
DE
4290/* Find the base address of the compilation unit for range lists and
4291 location lists. It will normally be specified by DW_AT_low_pc.
4292 In DWARF-3 draft 4, the base address could be overridden by
4293 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4294 compilation units with discontinuous ranges. */
4295
4296static void
4297dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4298{
4299 struct attribute *attr;
4300
4301 cu->base_known = 0;
4302 cu->base_address = 0;
4303
4304 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4305 if (attr)
4306 {
31aa7e4e 4307 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4308 cu->base_known = 1;
4309 }
4310 else
4311 {
4312 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4313 if (attr)
4314 {
31aa7e4e 4315 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4316 cu->base_known = 1;
4317 }
4318 }
4319}
4320
93311388
DE
4321/* Read in the comp unit header information from the debug_info at info_ptr.
4322 NOTE: This leaves members offset, first_die_offset to be filled in
4323 by the caller. */
107d2387 4324
d521ce57 4325static const gdb_byte *
107d2387 4326read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 4327 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
4328{
4329 int signed_addr;
891d2f0b 4330 unsigned int bytes_read;
c764a876
DE
4331
4332 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4333 cu_header->initial_length_size = bytes_read;
4334 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4335 info_ptr += bytes_read;
107d2387
AC
4336 cu_header->version = read_2_bytes (abfd, info_ptr);
4337 info_ptr += 2;
b64f50a1
JK
4338 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4339 &bytes_read);
613e1657 4340 info_ptr += bytes_read;
107d2387
AC
4341 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4342 info_ptr += 1;
4343 signed_addr = bfd_get_sign_extend_vma (abfd);
4344 if (signed_addr < 0)
8e65ff28 4345 internal_error (__FILE__, __LINE__,
e2e0b3e5 4346 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4347 cu_header->signed_addr_p = signed_addr;
c764a876 4348
107d2387
AC
4349 return info_ptr;
4350}
4351
36586728
TT
4352/* Helper function that returns the proper abbrev section for
4353 THIS_CU. */
4354
4355static struct dwarf2_section_info *
4356get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4357{
4358 struct dwarf2_section_info *abbrev;
4359
4360 if (this_cu->is_dwz)
4361 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4362 else
4363 abbrev = &dwarf2_per_objfile->abbrev;
4364
4365 return abbrev;
4366}
4367
9ff913ba
DE
4368/* Subroutine of read_and_check_comp_unit_head and
4369 read_and_check_type_unit_head to simplify them.
4370 Perform various error checking on the header. */
4371
4372static void
4373error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4374 struct dwarf2_section_info *section,
4375 struct dwarf2_section_info *abbrev_section)
9ff913ba 4376{
a32a8923
DE
4377 bfd *abfd = get_section_bfd_owner (section);
4378 const char *filename = get_section_file_name (section);
9ff913ba
DE
4379
4380 if (header->version != 2 && header->version != 3 && header->version != 4)
4381 error (_("Dwarf Error: wrong version in compilation unit header "
4382 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4383 filename);
4384
b64f50a1 4385 if (header->abbrev_offset.sect_off
36586728 4386 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4387 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4388 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4389 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4390 filename);
4391
4392 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4393 avoid potential 32-bit overflow. */
1ce1cefd 4394 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4395 > section->size)
4396 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4397 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4398 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4399 filename);
4400}
4401
4402/* Read in a CU/TU header and perform some basic error checking.
4403 The contents of the header are stored in HEADER.
4404 The result is a pointer to the start of the first DIE. */
adabb602 4405
d521ce57 4406static const gdb_byte *
9ff913ba
DE
4407read_and_check_comp_unit_head (struct comp_unit_head *header,
4408 struct dwarf2_section_info *section,
4bdcc0c1 4409 struct dwarf2_section_info *abbrev_section,
d521ce57 4410 const gdb_byte *info_ptr,
9ff913ba 4411 int is_debug_types_section)
72bf9492 4412{
d521ce57 4413 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4414 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4415
b64f50a1 4416 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4417
72bf9492
DJ
4418 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4419
460c1c54
CC
4420 /* If we're reading a type unit, skip over the signature and
4421 type_offset fields. */
b0df02fd 4422 if (is_debug_types_section)
460c1c54
CC
4423 info_ptr += 8 /*signature*/ + header->offset_size;
4424
b64f50a1 4425 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4426
4bdcc0c1 4427 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4428
4429 return info_ptr;
4430}
4431
348e048f
DE
4432/* Read in the types comp unit header information from .debug_types entry at
4433 types_ptr. The result is a pointer to one past the end of the header. */
4434
d521ce57 4435static const gdb_byte *
9ff913ba
DE
4436read_and_check_type_unit_head (struct comp_unit_head *header,
4437 struct dwarf2_section_info *section,
4bdcc0c1 4438 struct dwarf2_section_info *abbrev_section,
d521ce57 4439 const gdb_byte *info_ptr,
dee91e82
DE
4440 ULONGEST *signature,
4441 cu_offset *type_offset_in_tu)
348e048f 4442{
d521ce57 4443 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4444 bfd *abfd = get_section_bfd_owner (section);
348e048f 4445
b64f50a1 4446 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4447
9ff913ba 4448 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4449
9ff913ba
DE
4450 /* If we're reading a type unit, skip over the signature and
4451 type_offset fields. */
4452 if (signature != NULL)
4453 *signature = read_8_bytes (abfd, info_ptr);
4454 info_ptr += 8;
dee91e82
DE
4455 if (type_offset_in_tu != NULL)
4456 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4457 header->offset_size);
9ff913ba
DE
4458 info_ptr += header->offset_size;
4459
b64f50a1 4460 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4461
4bdcc0c1 4462 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4463
4464 return info_ptr;
348e048f
DE
4465}
4466
f4dc4d17
DE
4467/* Fetch the abbreviation table offset from a comp or type unit header. */
4468
4469static sect_offset
4470read_abbrev_offset (struct dwarf2_section_info *section,
4471 sect_offset offset)
4472{
a32a8923 4473 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4474 const gdb_byte *info_ptr;
f4dc4d17
DE
4475 unsigned int length, initial_length_size, offset_size;
4476 sect_offset abbrev_offset;
4477
4478 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4479 info_ptr = section->buffer + offset.sect_off;
4480 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4481 offset_size = initial_length_size == 4 ? 4 : 8;
4482 info_ptr += initial_length_size + 2 /*version*/;
4483 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4484 return abbrev_offset;
4485}
4486
aaa75496
JB
4487/* Allocate a new partial symtab for file named NAME and mark this new
4488 partial symtab as being an include of PST. */
4489
4490static void
d521ce57 4491dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4492 struct objfile *objfile)
4493{
4494 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4495
fbd9ab74
JK
4496 if (!IS_ABSOLUTE_PATH (subpst->filename))
4497 {
4498 /* It shares objfile->objfile_obstack. */
4499 subpst->dirname = pst->dirname;
4500 }
4501
aaa75496
JB
4502 subpst->textlow = 0;
4503 subpst->texthigh = 0;
4504
4505 subpst->dependencies = (struct partial_symtab **)
4506 obstack_alloc (&objfile->objfile_obstack,
4507 sizeof (struct partial_symtab *));
4508 subpst->dependencies[0] = pst;
4509 subpst->number_of_dependencies = 1;
4510
4511 subpst->globals_offset = 0;
4512 subpst->n_global_syms = 0;
4513 subpst->statics_offset = 0;
4514 subpst->n_static_syms = 0;
43f3e411 4515 subpst->compunit_symtab = NULL;
aaa75496
JB
4516 subpst->read_symtab = pst->read_symtab;
4517 subpst->readin = 0;
4518
4519 /* No private part is necessary for include psymtabs. This property
4520 can be used to differentiate between such include psymtabs and
10b3939b 4521 the regular ones. */
58a9656e 4522 subpst->read_symtab_private = NULL;
aaa75496
JB
4523}
4524
4525/* Read the Line Number Program data and extract the list of files
4526 included by the source file represented by PST. Build an include
d85a05f0 4527 partial symtab for each of these included files. */
aaa75496
JB
4528
4529static void
4530dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4531 struct die_info *die,
4532 struct partial_symtab *pst)
aaa75496 4533{
d85a05f0
DJ
4534 struct line_header *lh = NULL;
4535 struct attribute *attr;
aaa75496 4536
d85a05f0
DJ
4537 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4538 if (attr)
3019eac3 4539 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4540 if (lh == NULL)
4541 return; /* No linetable, so no includes. */
4542
c6da4cef 4543 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
527f3840 4544 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4545
4546 free_line_header (lh);
4547}
4548
348e048f 4549static hashval_t
52dc124a 4550hash_signatured_type (const void *item)
348e048f 4551{
52dc124a 4552 const struct signatured_type *sig_type = item;
9a619af0 4553
348e048f 4554 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4555 return sig_type->signature;
348e048f
DE
4556}
4557
4558static int
52dc124a 4559eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4560{
4561 const struct signatured_type *lhs = item_lhs;
4562 const struct signatured_type *rhs = item_rhs;
9a619af0 4563
348e048f
DE
4564 return lhs->signature == rhs->signature;
4565}
4566
1fd400ff
TT
4567/* Allocate a hash table for signatured types. */
4568
4569static htab_t
673bfd45 4570allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4571{
4572 return htab_create_alloc_ex (41,
52dc124a
DE
4573 hash_signatured_type,
4574 eq_signatured_type,
1fd400ff
TT
4575 NULL,
4576 &objfile->objfile_obstack,
4577 hashtab_obstack_allocate,
4578 dummy_obstack_deallocate);
4579}
4580
d467dd73 4581/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4582
4583static int
d467dd73 4584add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4585{
4586 struct signatured_type *sigt = *slot;
b4dd5633 4587 struct signatured_type ***datap = datum;
1fd400ff 4588
b4dd5633 4589 **datap = sigt;
1fd400ff
TT
4590 ++*datap;
4591
4592 return 1;
4593}
4594
c88ee1f0
DE
4595/* Create the hash table of all entries in the .debug_types
4596 (or .debug_types.dwo) section(s).
4597 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4598 otherwise it is NULL.
4599
4600 The result is a pointer to the hash table or NULL if there are no types.
4601
4602 Note: This function processes DWO files only, not DWP files. */
348e048f 4603
3019eac3
DE
4604static htab_t
4605create_debug_types_hash_table (struct dwo_file *dwo_file,
4606 VEC (dwarf2_section_info_def) *types)
348e048f 4607{
3019eac3 4608 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4609 htab_t types_htab = NULL;
8b70b953
TT
4610 int ix;
4611 struct dwarf2_section_info *section;
4bdcc0c1 4612 struct dwarf2_section_info *abbrev_section;
348e048f 4613
3019eac3
DE
4614 if (VEC_empty (dwarf2_section_info_def, types))
4615 return NULL;
348e048f 4616
4bdcc0c1
DE
4617 abbrev_section = (dwo_file != NULL
4618 ? &dwo_file->sections.abbrev
4619 : &dwarf2_per_objfile->abbrev);
4620
b4f54984 4621 if (dwarf_read_debug)
09406207
DE
4622 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4623 dwo_file ? ".dwo" : "",
a32a8923 4624 get_section_file_name (abbrev_section));
09406207 4625
8b70b953 4626 for (ix = 0;
3019eac3 4627 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4628 ++ix)
4629 {
3019eac3 4630 bfd *abfd;
d521ce57 4631 const gdb_byte *info_ptr, *end_ptr;
348e048f 4632
8b70b953
TT
4633 dwarf2_read_section (objfile, section);
4634 info_ptr = section->buffer;
348e048f 4635
8b70b953
TT
4636 if (info_ptr == NULL)
4637 continue;
348e048f 4638
3019eac3 4639 /* We can't set abfd until now because the section may be empty or
a32a8923
DE
4640 not present, in which case the bfd is unknown. */
4641 abfd = get_section_bfd_owner (section);
3019eac3 4642
dee91e82
DE
4643 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4644 because we don't need to read any dies: the signature is in the
4645 header. */
8b70b953
TT
4646
4647 end_ptr = info_ptr + section->size;
4648 while (info_ptr < end_ptr)
4649 {
b64f50a1 4650 sect_offset offset;
3019eac3 4651 cu_offset type_offset_in_tu;
8b70b953 4652 ULONGEST signature;
52dc124a 4653 struct signatured_type *sig_type;
3019eac3 4654 struct dwo_unit *dwo_tu;
8b70b953 4655 void **slot;
d521ce57 4656 const gdb_byte *ptr = info_ptr;
9ff913ba 4657 struct comp_unit_head header;
dee91e82 4658 unsigned int length;
348e048f 4659
b64f50a1 4660 offset.sect_off = ptr - section->buffer;
348e048f 4661
8b70b953 4662 /* We need to read the type's signature in order to build the hash
9ff913ba 4663 table, but we don't need anything else just yet. */
348e048f 4664
4bdcc0c1
DE
4665 ptr = read_and_check_type_unit_head (&header, section,
4666 abbrev_section, ptr,
3019eac3 4667 &signature, &type_offset_in_tu);
6caca83c 4668
1ce1cefd 4669 length = get_cu_length (&header);
dee91e82 4670
6caca83c 4671 /* Skip dummy type units. */
dee91e82
DE
4672 if (ptr >= info_ptr + length
4673 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4674 {
1ce1cefd 4675 info_ptr += length;
6caca83c
CC
4676 continue;
4677 }
8b70b953 4678
0349ea22
DE
4679 if (types_htab == NULL)
4680 {
4681 if (dwo_file)
4682 types_htab = allocate_dwo_unit_table (objfile);
4683 else
4684 types_htab = allocate_signatured_type_table (objfile);
4685 }
4686
3019eac3
DE
4687 if (dwo_file)
4688 {
4689 sig_type = NULL;
4690 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4691 struct dwo_unit);
4692 dwo_tu->dwo_file = dwo_file;
4693 dwo_tu->signature = signature;
4694 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4695 dwo_tu->section = section;
3019eac3
DE
4696 dwo_tu->offset = offset;
4697 dwo_tu->length = length;
4698 }
4699 else
4700 {
4701 /* N.B.: type_offset is not usable if this type uses a DWO file.
4702 The real type_offset is in the DWO file. */
4703 dwo_tu = NULL;
4704 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4705 struct signatured_type);
4706 sig_type->signature = signature;
4707 sig_type->type_offset_in_tu = type_offset_in_tu;
4708 sig_type->per_cu.objfile = objfile;
4709 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4710 sig_type->per_cu.section = section;
3019eac3
DE
4711 sig_type->per_cu.offset = offset;
4712 sig_type->per_cu.length = length;
4713 }
8b70b953 4714
3019eac3
DE
4715 slot = htab_find_slot (types_htab,
4716 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4717 INSERT);
8b70b953
TT
4718 gdb_assert (slot != NULL);
4719 if (*slot != NULL)
4720 {
3019eac3
DE
4721 sect_offset dup_offset;
4722
4723 if (dwo_file)
4724 {
4725 const struct dwo_unit *dup_tu = *slot;
4726
4727 dup_offset = dup_tu->offset;
4728 }
4729 else
4730 {
4731 const struct signatured_type *dup_tu = *slot;
4732
4733 dup_offset = dup_tu->per_cu.offset;
4734 }
b3c8eb43 4735
8b70b953 4736 complaint (&symfile_complaints,
c88ee1f0 4737 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4738 " the entry at offset 0x%x, signature %s"),
3019eac3 4739 offset.sect_off, dup_offset.sect_off,
4031ecc5 4740 hex_string (signature));
8b70b953 4741 }
3019eac3 4742 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4743
b4f54984 4744 if (dwarf_read_debug > 1)
4031ecc5 4745 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4746 offset.sect_off,
4031ecc5 4747 hex_string (signature));
348e048f 4748
dee91e82 4749 info_ptr += length;
8b70b953 4750 }
348e048f
DE
4751 }
4752
3019eac3
DE
4753 return types_htab;
4754}
4755
4756/* Create the hash table of all entries in the .debug_types section,
4757 and initialize all_type_units.
4758 The result is zero if there is an error (e.g. missing .debug_types section),
4759 otherwise non-zero. */
4760
4761static int
4762create_all_type_units (struct objfile *objfile)
4763{
4764 htab_t types_htab;
b4dd5633 4765 struct signatured_type **iter;
3019eac3
DE
4766
4767 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4768 if (types_htab == NULL)
4769 {
4770 dwarf2_per_objfile->signatured_types = NULL;
4771 return 0;
4772 }
4773
348e048f
DE
4774 dwarf2_per_objfile->signatured_types = types_htab;
4775
6aa5f3a6
DE
4776 dwarf2_per_objfile->n_type_units
4777 = dwarf2_per_objfile->n_allocated_type_units
4778 = htab_elements (types_htab);
d467dd73 4779 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4780 = xmalloc (dwarf2_per_objfile->n_type_units
4781 * sizeof (struct signatured_type *));
d467dd73
DE
4782 iter = &dwarf2_per_objfile->all_type_units[0];
4783 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4784 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4785 == dwarf2_per_objfile->n_type_units);
1fd400ff 4786
348e048f
DE
4787 return 1;
4788}
4789
6aa5f3a6
DE
4790/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4791 If SLOT is non-NULL, it is the entry to use in the hash table.
4792 Otherwise we find one. */
4793
4794static struct signatured_type *
4795add_type_unit (ULONGEST sig, void **slot)
4796{
4797 struct objfile *objfile = dwarf2_per_objfile->objfile;
4798 int n_type_units = dwarf2_per_objfile->n_type_units;
4799 struct signatured_type *sig_type;
4800
4801 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4802 ++n_type_units;
4803 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4804 {
4805 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4806 dwarf2_per_objfile->n_allocated_type_units = 1;
4807 dwarf2_per_objfile->n_allocated_type_units *= 2;
4808 dwarf2_per_objfile->all_type_units
4809 = xrealloc (dwarf2_per_objfile->all_type_units,
4810 dwarf2_per_objfile->n_allocated_type_units
4811 * sizeof (struct signatured_type *));
4812 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4813 }
4814 dwarf2_per_objfile->n_type_units = n_type_units;
4815
4816 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4817 struct signatured_type);
4818 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4819 sig_type->signature = sig;
4820 sig_type->per_cu.is_debug_types = 1;
4821 if (dwarf2_per_objfile->using_index)
4822 {
4823 sig_type->per_cu.v.quick =
4824 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4825 struct dwarf2_per_cu_quick_data);
4826 }
4827
4828 if (slot == NULL)
4829 {
4830 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4831 sig_type, INSERT);
4832 }
4833 gdb_assert (*slot == NULL);
4834 *slot = sig_type;
4835 /* The rest of sig_type must be filled in by the caller. */
4836 return sig_type;
4837}
4838
a2ce51a0
DE
4839/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4840 Fill in SIG_ENTRY with DWO_ENTRY. */
4841
4842static void
4843fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4844 struct signatured_type *sig_entry,
4845 struct dwo_unit *dwo_entry)
4846{
7ee85ab1 4847 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4848 gdb_assert (! sig_entry->per_cu.queued);
4849 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
4850 if (dwarf2_per_objfile->using_index)
4851 {
4852 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 4853 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
4854 }
4855 else
4856 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0
DE
4857 gdb_assert (sig_entry->signature == dwo_entry->signature);
4858 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4859 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4860 gdb_assert (sig_entry->dwo_unit == NULL);
4861
4862 sig_entry->per_cu.section = dwo_entry->section;
4863 sig_entry->per_cu.offset = dwo_entry->offset;
4864 sig_entry->per_cu.length = dwo_entry->length;
4865 sig_entry->per_cu.reading_dwo_directly = 1;
4866 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4867 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4868 sig_entry->dwo_unit = dwo_entry;
4869}
4870
4871/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4872 If we haven't read the TU yet, create the signatured_type data structure
4873 for a TU to be read in directly from a DWO file, bypassing the stub.
4874 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4875 using .gdb_index, then when reading a CU we want to stay in the DWO file
4876 containing that CU. Otherwise we could end up reading several other DWO
4877 files (due to comdat folding) to process the transitive closure of all the
4878 mentioned TUs, and that can be slow. The current DWO file will have every
4879 type signature that it needs.
a2ce51a0
DE
4880 We only do this for .gdb_index because in the psymtab case we already have
4881 to read all the DWOs to build the type unit groups. */
4882
4883static struct signatured_type *
4884lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4885{
4886 struct objfile *objfile = dwarf2_per_objfile->objfile;
4887 struct dwo_file *dwo_file;
4888 struct dwo_unit find_dwo_entry, *dwo_entry;
4889 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 4890 void **slot;
a2ce51a0
DE
4891
4892 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4893
6aa5f3a6
DE
4894 /* If TU skeletons have been removed then we may not have read in any
4895 TUs yet. */
4896 if (dwarf2_per_objfile->signatured_types == NULL)
4897 {
4898 dwarf2_per_objfile->signatured_types
4899 = allocate_signatured_type_table (objfile);
4900 }
a2ce51a0
DE
4901
4902 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
4903 Use the global signatured_types array to do our own comdat-folding
4904 of types. If this is the first time we're reading this TU, and
4905 the TU has an entry in .gdb_index, replace the recorded data from
4906 .gdb_index with this TU. */
a2ce51a0 4907
a2ce51a0 4908 find_sig_entry.signature = sig;
6aa5f3a6
DE
4909 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4910 &find_sig_entry, INSERT);
4911 sig_entry = *slot;
7ee85ab1
DE
4912
4913 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
4914 read. Don't reassign the global entry to point to this DWO if that's
4915 the case. Also note that if the TU is already being read, it may not
4916 have come from a DWO, the program may be a mix of Fission-compiled
4917 code and non-Fission-compiled code. */
4918
4919 /* Have we already tried to read this TU?
4920 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
4921 needn't exist in the global table yet). */
4922 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
4923 return sig_entry;
4924
6aa5f3a6
DE
4925 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4926 dwo_unit of the TU itself. */
4927 dwo_file = cu->dwo_unit->dwo_file;
4928
a2ce51a0
DE
4929 /* Ok, this is the first time we're reading this TU. */
4930 if (dwo_file->tus == NULL)
4931 return NULL;
4932 find_dwo_entry.signature = sig;
4933 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4934 if (dwo_entry == NULL)
4935 return NULL;
4936
6aa5f3a6
DE
4937 /* If the global table doesn't have an entry for this TU, add one. */
4938 if (sig_entry == NULL)
4939 sig_entry = add_type_unit (sig, slot);
4940
a2ce51a0 4941 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 4942 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
4943 return sig_entry;
4944}
4945
a2ce51a0
DE
4946/* Subroutine of lookup_signatured_type.
4947 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
4948 then try the DWP file. If the TU stub (skeleton) has been removed then
4949 it won't be in .gdb_index. */
a2ce51a0
DE
4950
4951static struct signatured_type *
4952lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4953{
4954 struct objfile *objfile = dwarf2_per_objfile->objfile;
4955 struct dwp_file *dwp_file = get_dwp_file ();
4956 struct dwo_unit *dwo_entry;
4957 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 4958 void **slot;
a2ce51a0
DE
4959
4960 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4961 gdb_assert (dwp_file != NULL);
4962
6aa5f3a6
DE
4963 /* If TU skeletons have been removed then we may not have read in any
4964 TUs yet. */
4965 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 4966 {
6aa5f3a6
DE
4967 dwarf2_per_objfile->signatured_types
4968 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
4969 }
4970
6aa5f3a6
DE
4971 find_sig_entry.signature = sig;
4972 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4973 &find_sig_entry, INSERT);
4974 sig_entry = *slot;
4975
4976 /* Have we already tried to read this TU?
4977 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
4978 needn't exist in the global table yet). */
4979 if (sig_entry != NULL)
4980 return sig_entry;
4981
a2ce51a0
DE
4982 if (dwp_file->tus == NULL)
4983 return NULL;
57d63ce2
DE
4984 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
4985 sig, 1 /* is_debug_types */);
a2ce51a0
DE
4986 if (dwo_entry == NULL)
4987 return NULL;
4988
6aa5f3a6 4989 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
4990 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4991
a2ce51a0
DE
4992 return sig_entry;
4993}
4994
380bca97 4995/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4996 Returns NULL if signature SIG is not present in the table.
4997 It is up to the caller to complain about this. */
348e048f
DE
4998
4999static struct signatured_type *
a2ce51a0 5000lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5001{
a2ce51a0
DE
5002 if (cu->dwo_unit
5003 && dwarf2_per_objfile->using_index)
5004 {
5005 /* We're in a DWO/DWP file, and we're using .gdb_index.
5006 These cases require special processing. */
5007 if (get_dwp_file () == NULL)
5008 return lookup_dwo_signatured_type (cu, sig);
5009 else
5010 return lookup_dwp_signatured_type (cu, sig);
5011 }
5012 else
5013 {
5014 struct signatured_type find_entry, *entry;
348e048f 5015
a2ce51a0
DE
5016 if (dwarf2_per_objfile->signatured_types == NULL)
5017 return NULL;
5018 find_entry.signature = sig;
5019 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
5020 return entry;
5021 }
348e048f 5022}
42e7ad6c
DE
5023\f
5024/* Low level DIE reading support. */
348e048f 5025
d85a05f0
DJ
5026/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5027
5028static void
5029init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5030 struct dwarf2_cu *cu,
3019eac3
DE
5031 struct dwarf2_section_info *section,
5032 struct dwo_file *dwo_file)
d85a05f0 5033{
fceca515 5034 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5035 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5036 reader->cu = cu;
3019eac3 5037 reader->dwo_file = dwo_file;
dee91e82
DE
5038 reader->die_section = section;
5039 reader->buffer = section->buffer;
f664829e 5040 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5041 reader->comp_dir = NULL;
d85a05f0
DJ
5042}
5043
b0c7bfa9
DE
5044/* Subroutine of init_cutu_and_read_dies to simplify it.
5045 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5046 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5047 already.
5048
5049 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5050 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5051 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5052 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5053 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5054 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5055 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5056 are filled in with the info of the DIE from the DWO file.
5057 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5058 provided an abbrev table to use.
5059 The result is non-zero if a valid (non-dummy) DIE was found. */
5060
5061static int
5062read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5063 struct dwo_unit *dwo_unit,
5064 int abbrev_table_provided,
5065 struct die_info *stub_comp_unit_die,
a2ce51a0 5066 const char *stub_comp_dir,
b0c7bfa9 5067 struct die_reader_specs *result_reader,
d521ce57 5068 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5069 struct die_info **result_comp_unit_die,
5070 int *result_has_children)
5071{
5072 struct objfile *objfile = dwarf2_per_objfile->objfile;
5073 struct dwarf2_cu *cu = this_cu->cu;
5074 struct dwarf2_section_info *section;
5075 bfd *abfd;
d521ce57 5076 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5077 ULONGEST signature; /* Or dwo_id. */
5078 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5079 int i,num_extra_attrs;
5080 struct dwarf2_section_info *dwo_abbrev_section;
5081 struct attribute *attr;
5082 struct die_info *comp_unit_die;
5083
b0aeadb3
DE
5084 /* At most one of these may be provided. */
5085 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5086
b0c7bfa9
DE
5087 /* These attributes aren't processed until later:
5088 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5089 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5090 referenced later. However, these attributes are found in the stub
5091 which we won't have later. In order to not impose this complication
5092 on the rest of the code, we read them here and copy them to the
5093 DWO CU/TU die. */
b0c7bfa9
DE
5094
5095 stmt_list = NULL;
5096 low_pc = NULL;
5097 high_pc = NULL;
5098 ranges = NULL;
5099 comp_dir = NULL;
5100
5101 if (stub_comp_unit_die != NULL)
5102 {
5103 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5104 DWO file. */
5105 if (! this_cu->is_debug_types)
5106 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5107 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5108 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5109 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5110 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5111
5112 /* There should be a DW_AT_addr_base attribute here (if needed).
5113 We need the value before we can process DW_FORM_GNU_addr_index. */
5114 cu->addr_base = 0;
5115 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5116 if (attr)
5117 cu->addr_base = DW_UNSND (attr);
5118
5119 /* There should be a DW_AT_ranges_base attribute here (if needed).
5120 We need the value before we can process DW_AT_ranges. */
5121 cu->ranges_base = 0;
5122 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5123 if (attr)
5124 cu->ranges_base = DW_UNSND (attr);
5125 }
a2ce51a0
DE
5126 else if (stub_comp_dir != NULL)
5127 {
5128 /* Reconstruct the comp_dir attribute to simplify the code below. */
5129 comp_dir = (struct attribute *)
5130 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
5131 comp_dir->name = DW_AT_comp_dir;
5132 comp_dir->form = DW_FORM_string;
5133 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5134 DW_STRING (comp_dir) = stub_comp_dir;
5135 }
b0c7bfa9
DE
5136
5137 /* Set up for reading the DWO CU/TU. */
5138 cu->dwo_unit = dwo_unit;
5139 section = dwo_unit->section;
5140 dwarf2_read_section (objfile, section);
a32a8923 5141 abfd = get_section_bfd_owner (section);
b0c7bfa9
DE
5142 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5143 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5144 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5145
5146 if (this_cu->is_debug_types)
5147 {
5148 ULONGEST header_signature;
5149 cu_offset type_offset_in_tu;
5150 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5151
5152 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5153 dwo_abbrev_section,
5154 info_ptr,
5155 &header_signature,
5156 &type_offset_in_tu);
a2ce51a0
DE
5157 /* This is not an assert because it can be caused by bad debug info. */
5158 if (sig_type->signature != header_signature)
5159 {
5160 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5161 " TU at offset 0x%x [in module %s]"),
5162 hex_string (sig_type->signature),
5163 hex_string (header_signature),
5164 dwo_unit->offset.sect_off,
5165 bfd_get_filename (abfd));
5166 }
b0c7bfa9
DE
5167 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5168 /* For DWOs coming from DWP files, we don't know the CU length
5169 nor the type's offset in the TU until now. */
5170 dwo_unit->length = get_cu_length (&cu->header);
5171 dwo_unit->type_offset_in_tu = type_offset_in_tu;
5172
5173 /* Establish the type offset that can be used to lookup the type.
5174 For DWO files, we don't know it until now. */
5175 sig_type->type_offset_in_section.sect_off =
5176 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5177 }
5178 else
5179 {
5180 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5181 dwo_abbrev_section,
5182 info_ptr, 0);
5183 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5184 /* For DWOs coming from DWP files, we don't know the CU length
5185 until now. */
5186 dwo_unit->length = get_cu_length (&cu->header);
5187 }
5188
02142a6c
DE
5189 /* Replace the CU's original abbrev table with the DWO's.
5190 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5191 if (abbrev_table_provided)
5192 {
5193 /* Don't free the provided abbrev table, the caller of
5194 init_cutu_and_read_dies owns it. */
5195 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5196 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5197 make_cleanup (dwarf2_free_abbrev_table, cu);
5198 }
5199 else
5200 {
5201 dwarf2_free_abbrev_table (cu);
5202 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5203 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5204 }
5205
5206 /* Read in the die, but leave space to copy over the attributes
5207 from the stub. This has the benefit of simplifying the rest of
5208 the code - all the work to maintain the illusion of a single
5209 DW_TAG_{compile,type}_unit DIE is done here. */
5210 num_extra_attrs = ((stmt_list != NULL)
5211 + (low_pc != NULL)
5212 + (high_pc != NULL)
5213 + (ranges != NULL)
5214 + (comp_dir != NULL));
5215 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5216 result_has_children, num_extra_attrs);
5217
5218 /* Copy over the attributes from the stub to the DIE we just read in. */
5219 comp_unit_die = *result_comp_unit_die;
5220 i = comp_unit_die->num_attrs;
5221 if (stmt_list != NULL)
5222 comp_unit_die->attrs[i++] = *stmt_list;
5223 if (low_pc != NULL)
5224 comp_unit_die->attrs[i++] = *low_pc;
5225 if (high_pc != NULL)
5226 comp_unit_die->attrs[i++] = *high_pc;
5227 if (ranges != NULL)
5228 comp_unit_die->attrs[i++] = *ranges;
5229 if (comp_dir != NULL)
5230 comp_unit_die->attrs[i++] = *comp_dir;
5231 comp_unit_die->num_attrs += num_extra_attrs;
5232
b4f54984 5233 if (dwarf_die_debug)
bf6af496
DE
5234 {
5235 fprintf_unfiltered (gdb_stdlog,
5236 "Read die from %s@0x%x of %s:\n",
a32a8923 5237 get_section_name (section),
bf6af496
DE
5238 (unsigned) (begin_info_ptr - section->buffer),
5239 bfd_get_filename (abfd));
b4f54984 5240 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5241 }
5242
a2ce51a0
DE
5243 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5244 TUs by skipping the stub and going directly to the entry in the DWO file.
5245 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5246 to get it via circuitous means. Blech. */
5247 if (comp_dir != NULL)
5248 result_reader->comp_dir = DW_STRING (comp_dir);
5249
b0c7bfa9
DE
5250 /* Skip dummy compilation units. */
5251 if (info_ptr >= begin_info_ptr + dwo_unit->length
5252 || peek_abbrev_code (abfd, info_ptr) == 0)
5253 return 0;
5254
5255 *result_info_ptr = info_ptr;
5256 return 1;
5257}
5258
5259/* Subroutine of init_cutu_and_read_dies to simplify it.
5260 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5261 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5262
5263static struct dwo_unit *
5264lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5265 struct die_info *comp_unit_die)
5266{
5267 struct dwarf2_cu *cu = this_cu->cu;
5268 struct attribute *attr;
5269 ULONGEST signature;
5270 struct dwo_unit *dwo_unit;
5271 const char *comp_dir, *dwo_name;
5272
a2ce51a0
DE
5273 gdb_assert (cu != NULL);
5274
b0c7bfa9 5275 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5276 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5277 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5278
5279 if (this_cu->is_debug_types)
5280 {
5281 struct signatured_type *sig_type;
5282
5283 /* Since this_cu is the first member of struct signatured_type,
5284 we can go from a pointer to one to a pointer to the other. */
5285 sig_type = (struct signatured_type *) this_cu;
5286 signature = sig_type->signature;
5287 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5288 }
5289 else
5290 {
5291 struct attribute *attr;
5292
5293 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5294 if (! attr)
5295 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5296 " [in module %s]"),
4262abfb 5297 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5298 signature = DW_UNSND (attr);
5299 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5300 signature);
5301 }
5302
b0c7bfa9
DE
5303 return dwo_unit;
5304}
5305
a2ce51a0 5306/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5307 See it for a description of the parameters.
5308 Read a TU directly from a DWO file, bypassing the stub.
5309
5310 Note: This function could be a little bit simpler if we shared cleanups
5311 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5312 to do, so we keep this function self-contained. Or we could move this
5313 into our caller, but it's complex enough already. */
a2ce51a0
DE
5314
5315static void
6aa5f3a6
DE
5316init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5317 int use_existing_cu, int keep,
a2ce51a0
DE
5318 die_reader_func_ftype *die_reader_func,
5319 void *data)
5320{
5321 struct dwarf2_cu *cu;
5322 struct signatured_type *sig_type;
6aa5f3a6 5323 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5324 struct die_reader_specs reader;
5325 const gdb_byte *info_ptr;
5326 struct die_info *comp_unit_die;
5327 int has_children;
5328
5329 /* Verify we can do the following downcast, and that we have the
5330 data we need. */
5331 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5332 sig_type = (struct signatured_type *) this_cu;
5333 gdb_assert (sig_type->dwo_unit != NULL);
5334
5335 cleanups = make_cleanup (null_cleanup, NULL);
5336
6aa5f3a6
DE
5337 if (use_existing_cu && this_cu->cu != NULL)
5338 {
5339 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5340 cu = this_cu->cu;
5341 /* There's no need to do the rereading_dwo_cu handling that
5342 init_cutu_and_read_dies does since we don't read the stub. */
5343 }
5344 else
5345 {
5346 /* If !use_existing_cu, this_cu->cu must be NULL. */
5347 gdb_assert (this_cu->cu == NULL);
5348 cu = xmalloc (sizeof (*cu));
5349 init_one_comp_unit (cu, this_cu);
5350 /* If an error occurs while loading, release our storage. */
5351 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5352 }
5353
5354 /* A future optimization, if needed, would be to use an existing
5355 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5356 could share abbrev tables. */
a2ce51a0
DE
5357
5358 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5359 0 /* abbrev_table_provided */,
5360 NULL /* stub_comp_unit_die */,
5361 sig_type->dwo_unit->dwo_file->comp_dir,
5362 &reader, &info_ptr,
5363 &comp_unit_die, &has_children) == 0)
5364 {
5365 /* Dummy die. */
5366 do_cleanups (cleanups);
5367 return;
5368 }
5369
5370 /* All the "real" work is done here. */
5371 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5372
6aa5f3a6 5373 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5374 but the alternative is making the latter more complex.
5375 This function is only for the special case of using DWO files directly:
5376 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5377 if (free_cu_cleanup != NULL)
a2ce51a0 5378 {
6aa5f3a6
DE
5379 if (keep)
5380 {
5381 /* We've successfully allocated this compilation unit. Let our
5382 caller clean it up when finished with it. */
5383 discard_cleanups (free_cu_cleanup);
a2ce51a0 5384
6aa5f3a6
DE
5385 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5386 So we have to manually free the abbrev table. */
5387 dwarf2_free_abbrev_table (cu);
a2ce51a0 5388
6aa5f3a6
DE
5389 /* Link this CU into read_in_chain. */
5390 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5391 dwarf2_per_objfile->read_in_chain = this_cu;
5392 }
5393 else
5394 do_cleanups (free_cu_cleanup);
a2ce51a0 5395 }
a2ce51a0
DE
5396
5397 do_cleanups (cleanups);
5398}
5399
fd820528 5400/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5401 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5402
f4dc4d17
DE
5403 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5404 Otherwise the table specified in the comp unit header is read in and used.
5405 This is an optimization for when we already have the abbrev table.
5406
dee91e82
DE
5407 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5408 Otherwise, a new CU is allocated with xmalloc.
5409
5410 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5411 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5412
5413 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5414 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5415
70221824 5416static void
fd820528 5417init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5418 struct abbrev_table *abbrev_table,
fd820528
DE
5419 int use_existing_cu, int keep,
5420 die_reader_func_ftype *die_reader_func,
5421 void *data)
c906108c 5422{
dee91e82 5423 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5424 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5425 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5426 struct dwarf2_cu *cu;
d521ce57 5427 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5428 struct die_reader_specs reader;
d85a05f0 5429 struct die_info *comp_unit_die;
dee91e82 5430 int has_children;
d85a05f0 5431 struct attribute *attr;
365156ad 5432 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5433 struct signatured_type *sig_type = NULL;
4bdcc0c1 5434 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5435 /* Non-zero if CU currently points to a DWO file and we need to
5436 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5437 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5438 int rereading_dwo_cu = 0;
c906108c 5439
b4f54984 5440 if (dwarf_die_debug)
09406207
DE
5441 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5442 this_cu->is_debug_types ? "type" : "comp",
5443 this_cu->offset.sect_off);
5444
dee91e82
DE
5445 if (use_existing_cu)
5446 gdb_assert (keep);
23745b47 5447
a2ce51a0
DE
5448 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5449 file (instead of going through the stub), short-circuit all of this. */
5450 if (this_cu->reading_dwo_directly)
5451 {
5452 /* Narrow down the scope of possibilities to have to understand. */
5453 gdb_assert (this_cu->is_debug_types);
5454 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5455 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5456 die_reader_func, data);
a2ce51a0
DE
5457 return;
5458 }
5459
dee91e82
DE
5460 cleanups = make_cleanup (null_cleanup, NULL);
5461
5462 /* This is cheap if the section is already read in. */
5463 dwarf2_read_section (objfile, section);
5464
5465 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5466
5467 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5468
5469 if (use_existing_cu && this_cu->cu != NULL)
5470 {
5471 cu = this_cu->cu;
42e7ad6c
DE
5472 /* If this CU is from a DWO file we need to start over, we need to
5473 refetch the attributes from the skeleton CU.
5474 This could be optimized by retrieving those attributes from when we
5475 were here the first time: the previous comp_unit_die was stored in
5476 comp_unit_obstack. But there's no data yet that we need this
5477 optimization. */
5478 if (cu->dwo_unit != NULL)
5479 rereading_dwo_cu = 1;
dee91e82
DE
5480 }
5481 else
5482 {
5483 /* If !use_existing_cu, this_cu->cu must be NULL. */
5484 gdb_assert (this_cu->cu == NULL);
dee91e82
DE
5485 cu = xmalloc (sizeof (*cu));
5486 init_one_comp_unit (cu, this_cu);
dee91e82 5487 /* If an error occurs while loading, release our storage. */
365156ad 5488 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5489 }
dee91e82 5490
b0c7bfa9 5491 /* Get the header. */
42e7ad6c
DE
5492 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5493 {
5494 /* We already have the header, there's no need to read it in again. */
5495 info_ptr += cu->header.first_die_offset.cu_off;
5496 }
5497 else
5498 {
3019eac3 5499 if (this_cu->is_debug_types)
dee91e82
DE
5500 {
5501 ULONGEST signature;
42e7ad6c 5502 cu_offset type_offset_in_tu;
dee91e82 5503
4bdcc0c1
DE
5504 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5505 abbrev_section, info_ptr,
42e7ad6c
DE
5506 &signature,
5507 &type_offset_in_tu);
dee91e82 5508
42e7ad6c
DE
5509 /* Since per_cu is the first member of struct signatured_type,
5510 we can go from a pointer to one to a pointer to the other. */
5511 sig_type = (struct signatured_type *) this_cu;
5512 gdb_assert (sig_type->signature == signature);
5513 gdb_assert (sig_type->type_offset_in_tu.cu_off
5514 == type_offset_in_tu.cu_off);
dee91e82
DE
5515 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5516
42e7ad6c
DE
5517 /* LENGTH has not been set yet for type units if we're
5518 using .gdb_index. */
1ce1cefd 5519 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5520
5521 /* Establish the type offset that can be used to lookup the type. */
5522 sig_type->type_offset_in_section.sect_off =
5523 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5524 }
5525 else
5526 {
4bdcc0c1
DE
5527 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5528 abbrev_section,
5529 info_ptr, 0);
dee91e82
DE
5530
5531 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5532 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5533 }
5534 }
10b3939b 5535
6caca83c 5536 /* Skip dummy compilation units. */
dee91e82 5537 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5538 || peek_abbrev_code (abfd, info_ptr) == 0)
5539 {
dee91e82 5540 do_cleanups (cleanups);
21b2bd31 5541 return;
6caca83c
CC
5542 }
5543
433df2d4
DE
5544 /* If we don't have them yet, read the abbrevs for this compilation unit.
5545 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5546 done. Note that it's important that if the CU had an abbrev table
5547 on entry we don't free it when we're done: Somewhere up the call stack
5548 it may be in use. */
f4dc4d17
DE
5549 if (abbrev_table != NULL)
5550 {
5551 gdb_assert (cu->abbrev_table == NULL);
5552 gdb_assert (cu->header.abbrev_offset.sect_off
5553 == abbrev_table->offset.sect_off);
5554 cu->abbrev_table = abbrev_table;
5555 }
5556 else if (cu->abbrev_table == NULL)
dee91e82 5557 {
4bdcc0c1 5558 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5559 make_cleanup (dwarf2_free_abbrev_table, cu);
5560 }
42e7ad6c
DE
5561 else if (rereading_dwo_cu)
5562 {
5563 dwarf2_free_abbrev_table (cu);
5564 dwarf2_read_abbrevs (cu, abbrev_section);
5565 }
af703f96 5566
dee91e82 5567 /* Read the top level CU/TU die. */
3019eac3 5568 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5569 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5570
b0c7bfa9
DE
5571 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5572 from the DWO file.
5573 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5574 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5575 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5576 if (attr)
5577 {
3019eac3 5578 struct dwo_unit *dwo_unit;
b0c7bfa9 5579 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5580
5581 if (has_children)
6a506a2d
DE
5582 {
5583 complaint (&symfile_complaints,
5584 _("compilation unit with DW_AT_GNU_dwo_name"
5585 " has children (offset 0x%x) [in module %s]"),
5586 this_cu->offset.sect_off, bfd_get_filename (abfd));
5587 }
b0c7bfa9 5588 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5589 if (dwo_unit != NULL)
3019eac3 5590 {
6a506a2d
DE
5591 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5592 abbrev_table != NULL,
a2ce51a0 5593 comp_unit_die, NULL,
6a506a2d
DE
5594 &reader, &info_ptr,
5595 &dwo_comp_unit_die, &has_children) == 0)
5596 {
5597 /* Dummy die. */
5598 do_cleanups (cleanups);
5599 return;
5600 }
5601 comp_unit_die = dwo_comp_unit_die;
5602 }
5603 else
5604 {
5605 /* Yikes, we couldn't find the rest of the DIE, we only have
5606 the stub. A complaint has already been logged. There's
5607 not much more we can do except pass on the stub DIE to
5608 die_reader_func. We don't want to throw an error on bad
5609 debug info. */
3019eac3
DE
5610 }
5611 }
5612
b0c7bfa9 5613 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5614 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5615
b0c7bfa9 5616 /* Done, clean up. */
365156ad 5617 if (free_cu_cleanup != NULL)
348e048f 5618 {
365156ad
TT
5619 if (keep)
5620 {
5621 /* We've successfully allocated this compilation unit. Let our
5622 caller clean it up when finished with it. */
5623 discard_cleanups (free_cu_cleanup);
dee91e82 5624
365156ad
TT
5625 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5626 So we have to manually free the abbrev table. */
5627 dwarf2_free_abbrev_table (cu);
dee91e82 5628
365156ad
TT
5629 /* Link this CU into read_in_chain. */
5630 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5631 dwarf2_per_objfile->read_in_chain = this_cu;
5632 }
5633 else
5634 do_cleanups (free_cu_cleanup);
348e048f 5635 }
365156ad
TT
5636
5637 do_cleanups (cleanups);
dee91e82
DE
5638}
5639
33e80786
DE
5640/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5641 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5642 to have already done the lookup to find the DWO file).
dee91e82
DE
5643
5644 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5645 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5646
5647 We fill in THIS_CU->length.
5648
5649 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5650 linker) then DIE_READER_FUNC will not get called.
5651
5652 THIS_CU->cu is always freed when done.
3019eac3
DE
5653 This is done in order to not leave THIS_CU->cu in a state where we have
5654 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5655
5656static void
5657init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5658 struct dwo_file *dwo_file,
dee91e82
DE
5659 die_reader_func_ftype *die_reader_func,
5660 void *data)
5661{
5662 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5663 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5664 bfd *abfd = get_section_bfd_owner (section);
33e80786 5665 struct dwarf2_section_info *abbrev_section;
dee91e82 5666 struct dwarf2_cu cu;
d521ce57 5667 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5668 struct die_reader_specs reader;
5669 struct cleanup *cleanups;
5670 struct die_info *comp_unit_die;
5671 int has_children;
5672
b4f54984 5673 if (dwarf_die_debug)
09406207
DE
5674 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5675 this_cu->is_debug_types ? "type" : "comp",
5676 this_cu->offset.sect_off);
5677
dee91e82
DE
5678 gdb_assert (this_cu->cu == NULL);
5679
33e80786
DE
5680 abbrev_section = (dwo_file != NULL
5681 ? &dwo_file->sections.abbrev
5682 : get_abbrev_section_for_cu (this_cu));
5683
dee91e82
DE
5684 /* This is cheap if the section is already read in. */
5685 dwarf2_read_section (objfile, section);
5686
5687 init_one_comp_unit (&cu, this_cu);
5688
5689 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5690
5691 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5692 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5693 abbrev_section, info_ptr,
3019eac3 5694 this_cu->is_debug_types);
dee91e82 5695
1ce1cefd 5696 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5697
5698 /* Skip dummy compilation units. */
5699 if (info_ptr >= begin_info_ptr + this_cu->length
5700 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5701 {
dee91e82 5702 do_cleanups (cleanups);
21b2bd31 5703 return;
93311388 5704 }
72bf9492 5705
dee91e82
DE
5706 dwarf2_read_abbrevs (&cu, abbrev_section);
5707 make_cleanup (dwarf2_free_abbrev_table, &cu);
5708
3019eac3 5709 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5710 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5711
5712 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5713
5714 do_cleanups (cleanups);
5715}
5716
3019eac3
DE
5717/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5718 does not lookup the specified DWO file.
5719 This cannot be used to read DWO files.
dee91e82
DE
5720
5721 THIS_CU->cu is always freed when done.
3019eac3
DE
5722 This is done in order to not leave THIS_CU->cu in a state where we have
5723 to care whether it refers to the "main" CU or the DWO CU.
5724 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5725
5726static void
5727init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5728 die_reader_func_ftype *die_reader_func,
5729 void *data)
5730{
33e80786 5731 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5732}
0018ea6f
DE
5733\f
5734/* Type Unit Groups.
dee91e82 5735
0018ea6f
DE
5736 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5737 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5738 so that all types coming from the same compilation (.o file) are grouped
5739 together. A future step could be to put the types in the same symtab as
5740 the CU the types ultimately came from. */
ff013f42 5741
f4dc4d17
DE
5742static hashval_t
5743hash_type_unit_group (const void *item)
5744{
094b34ac 5745 const struct type_unit_group *tu_group = item;
f4dc4d17 5746
094b34ac 5747 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5748}
348e048f
DE
5749
5750static int
f4dc4d17 5751eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5752{
f4dc4d17
DE
5753 const struct type_unit_group *lhs = item_lhs;
5754 const struct type_unit_group *rhs = item_rhs;
348e048f 5755
094b34ac 5756 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5757}
348e048f 5758
f4dc4d17
DE
5759/* Allocate a hash table for type unit groups. */
5760
5761static htab_t
5762allocate_type_unit_groups_table (void)
5763{
5764 return htab_create_alloc_ex (3,
5765 hash_type_unit_group,
5766 eq_type_unit_group,
5767 NULL,
5768 &dwarf2_per_objfile->objfile->objfile_obstack,
5769 hashtab_obstack_allocate,
5770 dummy_obstack_deallocate);
5771}
dee91e82 5772
f4dc4d17
DE
5773/* Type units that don't have DW_AT_stmt_list are grouped into their own
5774 partial symtabs. We combine several TUs per psymtab to not let the size
5775 of any one psymtab grow too big. */
5776#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5777#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5778
094b34ac 5779/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5780 Create the type_unit_group object used to hold one or more TUs. */
5781
5782static struct type_unit_group *
094b34ac 5783create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5784{
5785 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5786 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5787 struct type_unit_group *tu_group;
f4dc4d17
DE
5788
5789 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5790 struct type_unit_group);
094b34ac 5791 per_cu = &tu_group->per_cu;
f4dc4d17 5792 per_cu->objfile = objfile;
f4dc4d17 5793
094b34ac
DE
5794 if (dwarf2_per_objfile->using_index)
5795 {
5796 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5797 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5798 }
5799 else
5800 {
5801 unsigned int line_offset = line_offset_struct.sect_off;
5802 struct partial_symtab *pst;
5803 char *name;
5804
5805 /* Give the symtab a useful name for debug purposes. */
5806 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5807 name = xstrprintf ("<type_units_%d>",
5808 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5809 else
5810 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5811
5812 pst = create_partial_symtab (per_cu, name);
5813 pst->anonymous = 1;
f4dc4d17 5814
094b34ac
DE
5815 xfree (name);
5816 }
f4dc4d17 5817
094b34ac
DE
5818 tu_group->hash.dwo_unit = cu->dwo_unit;
5819 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5820
5821 return tu_group;
5822}
5823
094b34ac
DE
5824/* Look up the type_unit_group for type unit CU, and create it if necessary.
5825 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5826
5827static struct type_unit_group *
ff39bb5e 5828get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5829{
5830 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5831 struct type_unit_group *tu_group;
5832 void **slot;
5833 unsigned int line_offset;
5834 struct type_unit_group type_unit_group_for_lookup;
5835
5836 if (dwarf2_per_objfile->type_unit_groups == NULL)
5837 {
5838 dwarf2_per_objfile->type_unit_groups =
5839 allocate_type_unit_groups_table ();
5840 }
5841
5842 /* Do we need to create a new group, or can we use an existing one? */
5843
5844 if (stmt_list)
5845 {
5846 line_offset = DW_UNSND (stmt_list);
5847 ++tu_stats->nr_symtab_sharers;
5848 }
5849 else
5850 {
5851 /* Ugh, no stmt_list. Rare, but we have to handle it.
5852 We can do various things here like create one group per TU or
5853 spread them over multiple groups to split up the expansion work.
5854 To avoid worst case scenarios (too many groups or too large groups)
5855 we, umm, group them in bunches. */
5856 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5857 | (tu_stats->nr_stmt_less_type_units
5858 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5859 ++tu_stats->nr_stmt_less_type_units;
5860 }
5861
094b34ac
DE
5862 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5863 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5864 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5865 &type_unit_group_for_lookup, INSERT);
5866 if (*slot != NULL)
5867 {
5868 tu_group = *slot;
5869 gdb_assert (tu_group != NULL);
5870 }
5871 else
5872 {
5873 sect_offset line_offset_struct;
5874
5875 line_offset_struct.sect_off = line_offset;
094b34ac 5876 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5877 *slot = tu_group;
5878 ++tu_stats->nr_symtabs;
5879 }
5880
5881 return tu_group;
5882}
0018ea6f
DE
5883\f
5884/* Partial symbol tables. */
5885
5886/* Create a psymtab named NAME and assign it to PER_CU.
5887
5888 The caller must fill in the following details:
5889 dirname, textlow, texthigh. */
5890
5891static struct partial_symtab *
5892create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5893{
5894 struct objfile *objfile = per_cu->objfile;
5895 struct partial_symtab *pst;
5896
18a94d75 5897 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
5898 objfile->global_psymbols.next,
5899 objfile->static_psymbols.next);
5900
5901 pst->psymtabs_addrmap_supported = 1;
5902
5903 /* This is the glue that links PST into GDB's symbol API. */
5904 pst->read_symtab_private = per_cu;
5905 pst->read_symtab = dwarf2_read_symtab;
5906 per_cu->v.psymtab = pst;
5907
5908 return pst;
5909}
5910
b93601f3
TT
5911/* The DATA object passed to process_psymtab_comp_unit_reader has this
5912 type. */
5913
5914struct process_psymtab_comp_unit_data
5915{
5916 /* True if we are reading a DW_TAG_partial_unit. */
5917
5918 int want_partial_unit;
5919
5920 /* The "pretend" language that is used if the CU doesn't declare a
5921 language. */
5922
5923 enum language pretend_language;
5924};
5925
0018ea6f
DE
5926/* die_reader_func for process_psymtab_comp_unit. */
5927
5928static void
5929process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5930 const gdb_byte *info_ptr,
0018ea6f
DE
5931 struct die_info *comp_unit_die,
5932 int has_children,
5933 void *data)
5934{
5935 struct dwarf2_cu *cu = reader->cu;
5936 struct objfile *objfile = cu->objfile;
3e29f34a 5937 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 5938 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
5939 CORE_ADDR baseaddr;
5940 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5941 struct partial_symtab *pst;
5942 int has_pc_info;
5943 const char *filename;
b93601f3 5944 struct process_psymtab_comp_unit_data *info = data;
0018ea6f 5945
b93601f3 5946 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
5947 return;
5948
5949 gdb_assert (! per_cu->is_debug_types);
5950
b93601f3 5951 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
5952
5953 cu->list_in_scope = &file_symbols;
5954
5955 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
5956 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
5957 if (filename == NULL)
0018ea6f 5958 filename = "";
0018ea6f
DE
5959
5960 pst = create_partial_symtab (per_cu, filename);
5961
5962 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 5963 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
5964
5965 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5966
5967 dwarf2_find_base_address (comp_unit_die, cu);
5968
5969 /* Possibly set the default values of LOWPC and HIGHPC from
5970 `DW_AT_ranges'. */
5971 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5972 &best_highpc, cu, pst);
5973 if (has_pc_info == 1 && best_lowpc < best_highpc)
5974 /* Store the contiguous range if it is not empty; it can be empty for
5975 CUs with no code. */
5976 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
5977 gdbarch_adjust_dwarf2_addr (gdbarch,
5978 best_lowpc + baseaddr),
5979 gdbarch_adjust_dwarf2_addr (gdbarch,
5980 best_highpc + baseaddr) - 1,
5981 pst);
0018ea6f
DE
5982
5983 /* Check if comp unit has_children.
5984 If so, read the rest of the partial symbols from this comp unit.
5985 If not, there's no more debug_info for this comp unit. */
5986 if (has_children)
5987 {
5988 struct partial_die_info *first_die;
5989 CORE_ADDR lowpc, highpc;
5990
5991 lowpc = ((CORE_ADDR) -1);
5992 highpc = ((CORE_ADDR) 0);
5993
5994 first_die = load_partial_dies (reader, info_ptr, 1);
5995
5996 scan_partial_symbols (first_die, &lowpc, &highpc,
5997 ! has_pc_info, cu);
5998
5999 /* If we didn't find a lowpc, set it to highpc to avoid
6000 complaints from `maint check'. */
6001 if (lowpc == ((CORE_ADDR) -1))
6002 lowpc = highpc;
6003
6004 /* If the compilation unit didn't have an explicit address range,
6005 then use the information extracted from its child dies. */
6006 if (! has_pc_info)
6007 {
6008 best_lowpc = lowpc;
6009 best_highpc = highpc;
6010 }
6011 }
3e29f34a
MR
6012 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6013 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6014
8763cede 6015 end_psymtab_common (objfile, pst);
0018ea6f
DE
6016
6017 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6018 {
6019 int i;
6020 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6021 struct dwarf2_per_cu_data *iter;
6022
6023 /* Fill in 'dependencies' here; we fill in 'users' in a
6024 post-pass. */
6025 pst->number_of_dependencies = len;
6026 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
6027 len * sizeof (struct symtab *));
6028 for (i = 0;
6029 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6030 i, iter);
6031 ++i)
6032 pst->dependencies[i] = iter->v.psymtab;
6033
6034 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6035 }
6036
6037 /* Get the list of files included in the current compilation unit,
6038 and build a psymtab for each of them. */
6039 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6040
b4f54984 6041 if (dwarf_read_debug)
0018ea6f
DE
6042 {
6043 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6044
6045 fprintf_unfiltered (gdb_stdlog,
6046 "Psymtab for %s unit @0x%x: %s - %s"
6047 ", %d global, %d static syms\n",
6048 per_cu->is_debug_types ? "type" : "comp",
6049 per_cu->offset.sect_off,
6050 paddress (gdbarch, pst->textlow),
6051 paddress (gdbarch, pst->texthigh),
6052 pst->n_global_syms, pst->n_static_syms);
6053 }
6054}
6055
6056/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6057 Process compilation unit THIS_CU for a psymtab. */
6058
6059static void
6060process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6061 int want_partial_unit,
6062 enum language pretend_language)
0018ea6f 6063{
b93601f3
TT
6064 struct process_psymtab_comp_unit_data info;
6065
0018ea6f
DE
6066 /* If this compilation unit was already read in, free the
6067 cached copy in order to read it in again. This is
6068 necessary because we skipped some symbols when we first
6069 read in the compilation unit (see load_partial_dies).
6070 This problem could be avoided, but the benefit is unclear. */
6071 if (this_cu->cu != NULL)
6072 free_one_cached_comp_unit (this_cu);
6073
6074 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
6075 info.want_partial_unit = want_partial_unit;
6076 info.pretend_language = pretend_language;
0018ea6f
DE
6077 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6078 process_psymtab_comp_unit_reader,
b93601f3 6079 &info);
0018ea6f
DE
6080
6081 /* Age out any secondary CUs. */
6082 age_cached_comp_units ();
6083}
f4dc4d17
DE
6084
6085/* Reader function for build_type_psymtabs. */
6086
6087static void
6088build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6089 const gdb_byte *info_ptr,
f4dc4d17
DE
6090 struct die_info *type_unit_die,
6091 int has_children,
6092 void *data)
6093{
6094 struct objfile *objfile = dwarf2_per_objfile->objfile;
6095 struct dwarf2_cu *cu = reader->cu;
6096 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6097 struct signatured_type *sig_type;
f4dc4d17
DE
6098 struct type_unit_group *tu_group;
6099 struct attribute *attr;
6100 struct partial_die_info *first_die;
6101 CORE_ADDR lowpc, highpc;
6102 struct partial_symtab *pst;
6103
6104 gdb_assert (data == NULL);
0186c6a7
DE
6105 gdb_assert (per_cu->is_debug_types);
6106 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6107
6108 if (! has_children)
6109 return;
6110
6111 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6112 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6113
0186c6a7 6114 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6115
6116 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6117 cu->list_in_scope = &file_symbols;
6118 pst = create_partial_symtab (per_cu, "");
6119 pst->anonymous = 1;
6120
6121 first_die = load_partial_dies (reader, info_ptr, 1);
6122
6123 lowpc = (CORE_ADDR) -1;
6124 highpc = (CORE_ADDR) 0;
6125 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6126
8763cede 6127 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6128}
6129
73051182
DE
6130/* Struct used to sort TUs by their abbreviation table offset. */
6131
6132struct tu_abbrev_offset
6133{
6134 struct signatured_type *sig_type;
6135 sect_offset abbrev_offset;
6136};
6137
6138/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6139
6140static int
6141sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6142{
6143 const struct tu_abbrev_offset * const *a = ap;
6144 const struct tu_abbrev_offset * const *b = bp;
6145 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6146 unsigned int boff = (*b)->abbrev_offset.sect_off;
6147
6148 return (aoff > boff) - (aoff < boff);
6149}
6150
6151/* Efficiently read all the type units.
6152 This does the bulk of the work for build_type_psymtabs.
6153
6154 The efficiency is because we sort TUs by the abbrev table they use and
6155 only read each abbrev table once. In one program there are 200K TUs
6156 sharing 8K abbrev tables.
6157
6158 The main purpose of this function is to support building the
6159 dwarf2_per_objfile->type_unit_groups table.
6160 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6161 can collapse the search space by grouping them by stmt_list.
6162 The savings can be significant, in the same program from above the 200K TUs
6163 share 8K stmt_list tables.
6164
6165 FUNC is expected to call get_type_unit_group, which will create the
6166 struct type_unit_group if necessary and add it to
6167 dwarf2_per_objfile->type_unit_groups. */
6168
6169static void
6170build_type_psymtabs_1 (void)
6171{
6172 struct objfile *objfile = dwarf2_per_objfile->objfile;
6173 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6174 struct cleanup *cleanups;
6175 struct abbrev_table *abbrev_table;
6176 sect_offset abbrev_offset;
6177 struct tu_abbrev_offset *sorted_by_abbrev;
6178 struct type_unit_group **iter;
6179 int i;
6180
6181 /* It's up to the caller to not call us multiple times. */
6182 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6183
6184 if (dwarf2_per_objfile->n_type_units == 0)
6185 return;
6186
6187 /* TUs typically share abbrev tables, and there can be way more TUs than
6188 abbrev tables. Sort by abbrev table to reduce the number of times we
6189 read each abbrev table in.
6190 Alternatives are to punt or to maintain a cache of abbrev tables.
6191 This is simpler and efficient enough for now.
6192
6193 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6194 symtab to use). Typically TUs with the same abbrev offset have the same
6195 stmt_list value too so in practice this should work well.
6196
6197 The basic algorithm here is:
6198
6199 sort TUs by abbrev table
6200 for each TU with same abbrev table:
6201 read abbrev table if first user
6202 read TU top level DIE
6203 [IWBN if DWO skeletons had DW_AT_stmt_list]
6204 call FUNC */
6205
b4f54984 6206 if (dwarf_read_debug)
73051182
DE
6207 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6208
6209 /* Sort in a separate table to maintain the order of all_type_units
6210 for .gdb_index: TU indices directly index all_type_units. */
6211 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6212 dwarf2_per_objfile->n_type_units);
6213 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6214 {
6215 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6216
6217 sorted_by_abbrev[i].sig_type = sig_type;
6218 sorted_by_abbrev[i].abbrev_offset =
6219 read_abbrev_offset (sig_type->per_cu.section,
6220 sig_type->per_cu.offset);
6221 }
6222 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6223 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6224 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6225
6226 abbrev_offset.sect_off = ~(unsigned) 0;
6227 abbrev_table = NULL;
6228 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6229
6230 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6231 {
6232 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6233
6234 /* Switch to the next abbrev table if necessary. */
6235 if (abbrev_table == NULL
6236 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6237 {
6238 if (abbrev_table != NULL)
6239 {
6240 abbrev_table_free (abbrev_table);
6241 /* Reset to NULL in case abbrev_table_read_table throws
6242 an error: abbrev_table_free_cleanup will get called. */
6243 abbrev_table = NULL;
6244 }
6245 abbrev_offset = tu->abbrev_offset;
6246 abbrev_table =
6247 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6248 abbrev_offset);
6249 ++tu_stats->nr_uniq_abbrev_tables;
6250 }
6251
6252 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6253 build_type_psymtabs_reader, NULL);
6254 }
6255
73051182 6256 do_cleanups (cleanups);
6aa5f3a6 6257}
73051182 6258
6aa5f3a6
DE
6259/* Print collected type unit statistics. */
6260
6261static void
6262print_tu_stats (void)
6263{
6264 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6265
6266 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6267 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6268 dwarf2_per_objfile->n_type_units);
6269 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6270 tu_stats->nr_uniq_abbrev_tables);
6271 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6272 tu_stats->nr_symtabs);
6273 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6274 tu_stats->nr_symtab_sharers);
6275 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6276 tu_stats->nr_stmt_less_type_units);
6277 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6278 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6279}
6280
f4dc4d17
DE
6281/* Traversal function for build_type_psymtabs. */
6282
6283static int
6284build_type_psymtab_dependencies (void **slot, void *info)
6285{
6286 struct objfile *objfile = dwarf2_per_objfile->objfile;
6287 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6288 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6289 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6290 int len = VEC_length (sig_type_ptr, tu_group->tus);
6291 struct signatured_type *iter;
f4dc4d17
DE
6292 int i;
6293
6294 gdb_assert (len > 0);
0186c6a7 6295 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6296
6297 pst->number_of_dependencies = len;
6298 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
6299 len * sizeof (struct psymtab *));
6300 for (i = 0;
0186c6a7 6301 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6302 ++i)
6303 {
0186c6a7
DE
6304 gdb_assert (iter->per_cu.is_debug_types);
6305 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6306 iter->type_unit_group = tu_group;
f4dc4d17
DE
6307 }
6308
0186c6a7 6309 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6310
6311 return 1;
6312}
6313
6314/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6315 Build partial symbol tables for the .debug_types comp-units. */
6316
6317static void
6318build_type_psymtabs (struct objfile *objfile)
6319{
0e50663e 6320 if (! create_all_type_units (objfile))
348e048f
DE
6321 return;
6322
73051182 6323 build_type_psymtabs_1 ();
6aa5f3a6 6324}
f4dc4d17 6325
6aa5f3a6
DE
6326/* Traversal function for process_skeletonless_type_unit.
6327 Read a TU in a DWO file and build partial symbols for it. */
6328
6329static int
6330process_skeletonless_type_unit (void **slot, void *info)
6331{
6332 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6333 struct objfile *objfile = info;
6334 struct signatured_type find_entry, *entry;
6335
6336 /* If this TU doesn't exist in the global table, add it and read it in. */
6337
6338 if (dwarf2_per_objfile->signatured_types == NULL)
6339 {
6340 dwarf2_per_objfile->signatured_types
6341 = allocate_signatured_type_table (objfile);
6342 }
6343
6344 find_entry.signature = dwo_unit->signature;
6345 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6346 INSERT);
6347 /* If we've already seen this type there's nothing to do. What's happening
6348 is we're doing our own version of comdat-folding here. */
6349 if (*slot != NULL)
6350 return 1;
6351
6352 /* This does the job that create_all_type_units would have done for
6353 this TU. */
6354 entry = add_type_unit (dwo_unit->signature, slot);
6355 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6356 *slot = entry;
6357
6358 /* This does the job that build_type_psymtabs_1 would have done. */
6359 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6360 build_type_psymtabs_reader, NULL);
6361
6362 return 1;
6363}
6364
6365/* Traversal function for process_skeletonless_type_units. */
6366
6367static int
6368process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6369{
6370 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6371
6372 if (dwo_file->tus != NULL)
6373 {
6374 htab_traverse_noresize (dwo_file->tus,
6375 process_skeletonless_type_unit, info);
6376 }
6377
6378 return 1;
6379}
6380
6381/* Scan all TUs of DWO files, verifying we've processed them.
6382 This is needed in case a TU was emitted without its skeleton.
6383 Note: This can't be done until we know what all the DWO files are. */
6384
6385static void
6386process_skeletonless_type_units (struct objfile *objfile)
6387{
6388 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6389 if (get_dwp_file () == NULL
6390 && dwarf2_per_objfile->dwo_files != NULL)
6391 {
6392 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6393 process_dwo_file_for_skeletonless_type_units,
6394 objfile);
6395 }
348e048f
DE
6396}
6397
60606b2c
TT
6398/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6399
6400static void
6401psymtabs_addrmap_cleanup (void *o)
6402{
6403 struct objfile *objfile = o;
ec61707d 6404
60606b2c
TT
6405 objfile->psymtabs_addrmap = NULL;
6406}
6407
95554aad
TT
6408/* Compute the 'user' field for each psymtab in OBJFILE. */
6409
6410static void
6411set_partial_user (struct objfile *objfile)
6412{
6413 int i;
6414
6415 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6416 {
8832e7e3 6417 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6418 struct partial_symtab *pst = per_cu->v.psymtab;
6419 int j;
6420
36586728
TT
6421 if (pst == NULL)
6422 continue;
6423
95554aad
TT
6424 for (j = 0; j < pst->number_of_dependencies; ++j)
6425 {
6426 /* Set the 'user' field only if it is not already set. */
6427 if (pst->dependencies[j]->user == NULL)
6428 pst->dependencies[j]->user = pst;
6429 }
6430 }
6431}
6432
93311388
DE
6433/* Build the partial symbol table by doing a quick pass through the
6434 .debug_info and .debug_abbrev sections. */
72bf9492 6435
93311388 6436static void
c67a9c90 6437dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6438{
60606b2c
TT
6439 struct cleanup *back_to, *addrmap_cleanup;
6440 struct obstack temp_obstack;
21b2bd31 6441 int i;
93311388 6442
b4f54984 6443 if (dwarf_read_debug)
45cfd468
DE
6444 {
6445 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6446 objfile_name (objfile));
45cfd468
DE
6447 }
6448
98bfdba5
PA
6449 dwarf2_per_objfile->reading_partial_symbols = 1;
6450
be391dca 6451 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6452
93311388
DE
6453 /* Any cached compilation units will be linked by the per-objfile
6454 read_in_chain. Make sure to free them when we're done. */
6455 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6456
348e048f
DE
6457 build_type_psymtabs (objfile);
6458
93311388 6459 create_all_comp_units (objfile);
c906108c 6460
60606b2c
TT
6461 /* Create a temporary address map on a temporary obstack. We later
6462 copy this to the final obstack. */
6463 obstack_init (&temp_obstack);
6464 make_cleanup_obstack_free (&temp_obstack);
6465 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6466 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6467
21b2bd31 6468 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6469 {
8832e7e3 6470 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6471
b93601f3 6472 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6473 }
ff013f42 6474
6aa5f3a6
DE
6475 /* This has to wait until we read the CUs, we need the list of DWOs. */
6476 process_skeletonless_type_units (objfile);
6477
6478 /* Now that all TUs have been processed we can fill in the dependencies. */
6479 if (dwarf2_per_objfile->type_unit_groups != NULL)
6480 {
6481 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6482 build_type_psymtab_dependencies, NULL);
6483 }
6484
b4f54984 6485 if (dwarf_read_debug)
6aa5f3a6
DE
6486 print_tu_stats ();
6487
95554aad
TT
6488 set_partial_user (objfile);
6489
ff013f42
JK
6490 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6491 &objfile->objfile_obstack);
60606b2c 6492 discard_cleanups (addrmap_cleanup);
ff013f42 6493
ae038cb0 6494 do_cleanups (back_to);
45cfd468 6495
b4f54984 6496 if (dwarf_read_debug)
45cfd468 6497 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6498 objfile_name (objfile));
ae038cb0
DJ
6499}
6500
3019eac3 6501/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6502
6503static void
dee91e82 6504load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6505 const gdb_byte *info_ptr,
dee91e82
DE
6506 struct die_info *comp_unit_die,
6507 int has_children,
6508 void *data)
ae038cb0 6509{
dee91e82 6510 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6511
95554aad 6512 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6513
ae038cb0
DJ
6514 /* Check if comp unit has_children.
6515 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6516 If not, there's no more debug_info for this comp unit. */
d85a05f0 6517 if (has_children)
dee91e82
DE
6518 load_partial_dies (reader, info_ptr, 0);
6519}
98bfdba5 6520
dee91e82
DE
6521/* Load the partial DIEs for a secondary CU into memory.
6522 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6523
dee91e82
DE
6524static void
6525load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6526{
f4dc4d17
DE
6527 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6528 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6529}
6530
ae038cb0 6531static void
36586728
TT
6532read_comp_units_from_section (struct objfile *objfile,
6533 struct dwarf2_section_info *section,
6534 unsigned int is_dwz,
6535 int *n_allocated,
6536 int *n_comp_units,
6537 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6538{
d521ce57 6539 const gdb_byte *info_ptr;
a32a8923 6540 bfd *abfd = get_section_bfd_owner (section);
be391dca 6541
b4f54984 6542 if (dwarf_read_debug)
bf6af496 6543 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6544 get_section_name (section),
6545 get_section_file_name (section));
bf6af496 6546
36586728 6547 dwarf2_read_section (objfile, section);
ae038cb0 6548
36586728 6549 info_ptr = section->buffer;
6e70227d 6550
36586728 6551 while (info_ptr < section->buffer + section->size)
ae038cb0 6552 {
c764a876 6553 unsigned int length, initial_length_size;
ae038cb0 6554 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6555 sect_offset offset;
ae038cb0 6556
36586728 6557 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6558
6559 /* Read just enough information to find out where the next
6560 compilation unit is. */
36586728 6561 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6562
6563 /* Save the compilation unit for later lookup. */
6564 this_cu = obstack_alloc (&objfile->objfile_obstack,
6565 sizeof (struct dwarf2_per_cu_data));
6566 memset (this_cu, 0, sizeof (*this_cu));
6567 this_cu->offset = offset;
c764a876 6568 this_cu->length = length + initial_length_size;
36586728 6569 this_cu->is_dwz = is_dwz;
9291a0cd 6570 this_cu->objfile = objfile;
8a0459fd 6571 this_cu->section = section;
ae038cb0 6572
36586728 6573 if (*n_comp_units == *n_allocated)
ae038cb0 6574 {
36586728
TT
6575 *n_allocated *= 2;
6576 *all_comp_units = xrealloc (*all_comp_units,
6577 *n_allocated
6578 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6579 }
36586728
TT
6580 (*all_comp_units)[*n_comp_units] = this_cu;
6581 ++*n_comp_units;
ae038cb0
DJ
6582
6583 info_ptr = info_ptr + this_cu->length;
6584 }
36586728
TT
6585}
6586
6587/* Create a list of all compilation units in OBJFILE.
6588 This is only done for -readnow and building partial symtabs. */
6589
6590static void
6591create_all_comp_units (struct objfile *objfile)
6592{
6593 int n_allocated;
6594 int n_comp_units;
6595 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6596 struct dwz_file *dwz;
36586728
TT
6597
6598 n_comp_units = 0;
6599 n_allocated = 10;
6600 all_comp_units = xmalloc (n_allocated
6601 * sizeof (struct dwarf2_per_cu_data *));
6602
6603 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6604 &n_allocated, &n_comp_units, &all_comp_units);
6605
4db1a1dc
TT
6606 dwz = dwarf2_get_dwz_file ();
6607 if (dwz != NULL)
6608 read_comp_units_from_section (objfile, &dwz->info, 1,
6609 &n_allocated, &n_comp_units,
6610 &all_comp_units);
ae038cb0
DJ
6611
6612 dwarf2_per_objfile->all_comp_units
6613 = obstack_alloc (&objfile->objfile_obstack,
6614 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6615 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6616 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6617 xfree (all_comp_units);
6618 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6619}
6620
5734ee8b 6621/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6622 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6623 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6624 DW_AT_ranges). See the comments of add_partial_subprogram on how
6625 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6626
72bf9492
DJ
6627static void
6628scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6629 CORE_ADDR *highpc, int set_addrmap,
6630 struct dwarf2_cu *cu)
c906108c 6631{
72bf9492 6632 struct partial_die_info *pdi;
c906108c 6633
91c24f0a
DC
6634 /* Now, march along the PDI's, descending into ones which have
6635 interesting children but skipping the children of the other ones,
6636 until we reach the end of the compilation unit. */
c906108c 6637
72bf9492 6638 pdi = first_die;
91c24f0a 6639
72bf9492
DJ
6640 while (pdi != NULL)
6641 {
6642 fixup_partial_die (pdi, cu);
c906108c 6643
f55ee35c 6644 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6645 children, so we need to look at them. Ditto for anonymous
6646 enums. */
933c6fe4 6647
72bf9492 6648 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6649 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6650 || pdi->tag == DW_TAG_imported_unit)
c906108c 6651 {
72bf9492 6652 switch (pdi->tag)
c906108c
SS
6653 {
6654 case DW_TAG_subprogram:
cdc07690 6655 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6656 break;
72929c62 6657 case DW_TAG_constant:
c906108c
SS
6658 case DW_TAG_variable:
6659 case DW_TAG_typedef:
91c24f0a 6660 case DW_TAG_union_type:
72bf9492 6661 if (!pdi->is_declaration)
63d06c5c 6662 {
72bf9492 6663 add_partial_symbol (pdi, cu);
63d06c5c
DC
6664 }
6665 break;
c906108c 6666 case DW_TAG_class_type:
680b30c7 6667 case DW_TAG_interface_type:
c906108c 6668 case DW_TAG_structure_type:
72bf9492 6669 if (!pdi->is_declaration)
c906108c 6670 {
72bf9492 6671 add_partial_symbol (pdi, cu);
c906108c
SS
6672 }
6673 break;
91c24f0a 6674 case DW_TAG_enumeration_type:
72bf9492
DJ
6675 if (!pdi->is_declaration)
6676 add_partial_enumeration (pdi, cu);
c906108c
SS
6677 break;
6678 case DW_TAG_base_type:
a02abb62 6679 case DW_TAG_subrange_type:
c906108c 6680 /* File scope base type definitions are added to the partial
c5aa993b 6681 symbol table. */
72bf9492 6682 add_partial_symbol (pdi, cu);
c906108c 6683 break;
d9fa45fe 6684 case DW_TAG_namespace:
cdc07690 6685 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6686 break;
5d7cb8df 6687 case DW_TAG_module:
cdc07690 6688 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6689 break;
95554aad
TT
6690 case DW_TAG_imported_unit:
6691 {
6692 struct dwarf2_per_cu_data *per_cu;
6693
f4dc4d17
DE
6694 /* For now we don't handle imported units in type units. */
6695 if (cu->per_cu->is_debug_types)
6696 {
6697 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6698 " supported in type units [in module %s]"),
4262abfb 6699 objfile_name (cu->objfile));
f4dc4d17
DE
6700 }
6701
95554aad 6702 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6703 pdi->is_dwz,
95554aad
TT
6704 cu->objfile);
6705
6706 /* Go read the partial unit, if needed. */
6707 if (per_cu->v.psymtab == NULL)
b93601f3 6708 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6709
f4dc4d17 6710 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6711 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6712 }
6713 break;
74921315
KS
6714 case DW_TAG_imported_declaration:
6715 add_partial_symbol (pdi, cu);
6716 break;
c906108c
SS
6717 default:
6718 break;
6719 }
6720 }
6721
72bf9492
DJ
6722 /* If the die has a sibling, skip to the sibling. */
6723
6724 pdi = pdi->die_sibling;
6725 }
6726}
6727
6728/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6729
72bf9492 6730 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6731 name is concatenated with "::" and the partial DIE's name. For
6732 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6733 Enumerators are an exception; they use the scope of their parent
6734 enumeration type, i.e. the name of the enumeration type is not
6735 prepended to the enumerator.
91c24f0a 6736
72bf9492
DJ
6737 There are two complexities. One is DW_AT_specification; in this
6738 case "parent" means the parent of the target of the specification,
6739 instead of the direct parent of the DIE. The other is compilers
6740 which do not emit DW_TAG_namespace; in this case we try to guess
6741 the fully qualified name of structure types from their members'
6742 linkage names. This must be done using the DIE's children rather
6743 than the children of any DW_AT_specification target. We only need
6744 to do this for structures at the top level, i.e. if the target of
6745 any DW_AT_specification (if any; otherwise the DIE itself) does not
6746 have a parent. */
6747
6748/* Compute the scope prefix associated with PDI's parent, in
6749 compilation unit CU. The result will be allocated on CU's
6750 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6751 field. NULL is returned if no prefix is necessary. */
15d034d0 6752static const char *
72bf9492
DJ
6753partial_die_parent_scope (struct partial_die_info *pdi,
6754 struct dwarf2_cu *cu)
6755{
15d034d0 6756 const char *grandparent_scope;
72bf9492 6757 struct partial_die_info *parent, *real_pdi;
91c24f0a 6758
72bf9492
DJ
6759 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6760 then this means the parent of the specification DIE. */
6761
6762 real_pdi = pdi;
72bf9492 6763 while (real_pdi->has_specification)
36586728
TT
6764 real_pdi = find_partial_die (real_pdi->spec_offset,
6765 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6766
6767 parent = real_pdi->die_parent;
6768 if (parent == NULL)
6769 return NULL;
6770
6771 if (parent->scope_set)
6772 return parent->scope;
6773
6774 fixup_partial_die (parent, cu);
6775
10b3939b 6776 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6777
acebe513
UW
6778 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6779 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6780 Work around this problem here. */
6781 if (cu->language == language_cplus
6e70227d 6782 && parent->tag == DW_TAG_namespace
acebe513
UW
6783 && strcmp (parent->name, "::") == 0
6784 && grandparent_scope == NULL)
6785 {
6786 parent->scope = NULL;
6787 parent->scope_set = 1;
6788 return NULL;
6789 }
6790
9c6c53f7
SA
6791 if (pdi->tag == DW_TAG_enumerator)
6792 /* Enumerators should not get the name of the enumeration as a prefix. */
6793 parent->scope = grandparent_scope;
6794 else if (parent->tag == DW_TAG_namespace
f55ee35c 6795 || parent->tag == DW_TAG_module
72bf9492
DJ
6796 || parent->tag == DW_TAG_structure_type
6797 || parent->tag == DW_TAG_class_type
680b30c7 6798 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6799 || parent->tag == DW_TAG_union_type
6800 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6801 {
6802 if (grandparent_scope == NULL)
6803 parent->scope = parent->name;
6804 else
3e43a32a
MS
6805 parent->scope = typename_concat (&cu->comp_unit_obstack,
6806 grandparent_scope,
f55ee35c 6807 parent->name, 0, cu);
72bf9492 6808 }
72bf9492
DJ
6809 else
6810 {
6811 /* FIXME drow/2004-04-01: What should we be doing with
6812 function-local names? For partial symbols, we should probably be
6813 ignoring them. */
6814 complaint (&symfile_complaints,
e2e0b3e5 6815 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6816 parent->tag, pdi->offset.sect_off);
72bf9492 6817 parent->scope = grandparent_scope;
c906108c
SS
6818 }
6819
72bf9492
DJ
6820 parent->scope_set = 1;
6821 return parent->scope;
6822}
6823
6824/* Return the fully scoped name associated with PDI, from compilation unit
6825 CU. The result will be allocated with malloc. */
4568ecf9 6826
72bf9492
DJ
6827static char *
6828partial_die_full_name (struct partial_die_info *pdi,
6829 struct dwarf2_cu *cu)
6830{
15d034d0 6831 const char *parent_scope;
72bf9492 6832
98bfdba5
PA
6833 /* If this is a template instantiation, we can not work out the
6834 template arguments from partial DIEs. So, unfortunately, we have
6835 to go through the full DIEs. At least any work we do building
6836 types here will be reused if full symbols are loaded later. */
6837 if (pdi->has_template_arguments)
6838 {
6839 fixup_partial_die (pdi, cu);
6840
6841 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6842 {
6843 struct die_info *die;
6844 struct attribute attr;
6845 struct dwarf2_cu *ref_cu = cu;
6846
b64f50a1 6847 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6848 attr.name = 0;
6849 attr.form = DW_FORM_ref_addr;
4568ecf9 6850 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6851 die = follow_die_ref (NULL, &attr, &ref_cu);
6852
6853 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6854 }
6855 }
6856
72bf9492
DJ
6857 parent_scope = partial_die_parent_scope (pdi, cu);
6858 if (parent_scope == NULL)
6859 return NULL;
6860 else
f55ee35c 6861 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6862}
6863
6864static void
72bf9492 6865add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6866{
e7c27a73 6867 struct objfile *objfile = cu->objfile;
3e29f34a 6868 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 6869 CORE_ADDR addr = 0;
15d034d0 6870 const char *actual_name = NULL;
e142c38c 6871 CORE_ADDR baseaddr;
15d034d0 6872 char *built_actual_name;
e142c38c
DJ
6873
6874 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6875
15d034d0
TT
6876 built_actual_name = partial_die_full_name (pdi, cu);
6877 if (built_actual_name != NULL)
6878 actual_name = built_actual_name;
63d06c5c 6879
72bf9492
DJ
6880 if (actual_name == NULL)
6881 actual_name = pdi->name;
6882
c906108c
SS
6883 switch (pdi->tag)
6884 {
6885 case DW_TAG_subprogram:
3e29f34a 6886 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 6887 if (pdi->is_external || cu->language == language_ada)
c906108c 6888 {
2cfa0c8d
JB
6889 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6890 of the global scope. But in Ada, we want to be able to access
6891 nested procedures globally. So all Ada subprograms are stored
6892 in the global scope. */
3e29f34a
MR
6893 /* prim_record_minimal_symbol (actual_name, addr, mst_text,
6894 objfile); */
f47fb265 6895 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6896 built_actual_name != NULL,
f47fb265
MS
6897 VAR_DOMAIN, LOC_BLOCK,
6898 &objfile->global_psymbols,
1762568f 6899 addr, cu->language, objfile);
c906108c
SS
6900 }
6901 else
6902 {
3e29f34a
MR
6903 /* prim_record_minimal_symbol (actual_name, addr, mst_file_text,
6904 objfile); */
f47fb265 6905 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6906 built_actual_name != NULL,
f47fb265
MS
6907 VAR_DOMAIN, LOC_BLOCK,
6908 &objfile->static_psymbols,
1762568f 6909 addr, cu->language, objfile);
c906108c
SS
6910 }
6911 break;
72929c62
JB
6912 case DW_TAG_constant:
6913 {
6914 struct psymbol_allocation_list *list;
6915
6916 if (pdi->is_external)
6917 list = &objfile->global_psymbols;
6918 else
6919 list = &objfile->static_psymbols;
f47fb265 6920 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6921 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 6922 list, 0, cu->language, objfile);
72929c62
JB
6923 }
6924 break;
c906108c 6925 case DW_TAG_variable:
95554aad
TT
6926 if (pdi->d.locdesc)
6927 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6928
95554aad 6929 if (pdi->d.locdesc
caac4577
JG
6930 && addr == 0
6931 && !dwarf2_per_objfile->has_section_at_zero)
6932 {
6933 /* A global or static variable may also have been stripped
6934 out by the linker if unused, in which case its address
6935 will be nullified; do not add such variables into partial
6936 symbol table then. */
6937 }
6938 else if (pdi->is_external)
c906108c
SS
6939 {
6940 /* Global Variable.
6941 Don't enter into the minimal symbol tables as there is
6942 a minimal symbol table entry from the ELF symbols already.
6943 Enter into partial symbol table if it has a location
6944 descriptor or a type.
6945 If the location descriptor is missing, new_symbol will create
6946 a LOC_UNRESOLVED symbol, the address of the variable will then
6947 be determined from the minimal symbol table whenever the variable
6948 is referenced.
6949 The address for the partial symbol table entry is not
6950 used by GDB, but it comes in handy for debugging partial symbol
6951 table building. */
6952
95554aad 6953 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6954 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6955 built_actual_name != NULL,
f47fb265
MS
6956 VAR_DOMAIN, LOC_STATIC,
6957 &objfile->global_psymbols,
1762568f 6958 addr + baseaddr,
f47fb265 6959 cu->language, objfile);
c906108c
SS
6960 }
6961 else
6962 {
ff908ebf
AW
6963 int has_loc = pdi->d.locdesc != NULL;
6964
6965 /* Static Variable. Skip symbols whose value we cannot know (those
6966 without location descriptors or constant values). */
6967 if (!has_loc && !pdi->has_const_value)
decbce07 6968 {
15d034d0 6969 xfree (built_actual_name);
decbce07
MS
6970 return;
6971 }
ff908ebf 6972
f47fb265 6973 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6974 mst_file_data, objfile); */
f47fb265 6975 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6976 built_actual_name != NULL,
f47fb265
MS
6977 VAR_DOMAIN, LOC_STATIC,
6978 &objfile->static_psymbols,
ff908ebf 6979 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 6980 cu->language, objfile);
c906108c
SS
6981 }
6982 break;
6983 case DW_TAG_typedef:
6984 case DW_TAG_base_type:
a02abb62 6985 case DW_TAG_subrange_type:
38d518c9 6986 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6987 built_actual_name != NULL,
176620f1 6988 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6989 &objfile->static_psymbols,
1762568f 6990 0, cu->language, objfile);
c906108c 6991 break;
74921315 6992 case DW_TAG_imported_declaration:
72bf9492
DJ
6993 case DW_TAG_namespace:
6994 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6995 built_actual_name != NULL,
72bf9492
DJ
6996 VAR_DOMAIN, LOC_TYPEDEF,
6997 &objfile->global_psymbols,
1762568f 6998 0, cu->language, objfile);
72bf9492 6999 break;
530e8392
KB
7000 case DW_TAG_module:
7001 add_psymbol_to_list (actual_name, strlen (actual_name),
7002 built_actual_name != NULL,
7003 MODULE_DOMAIN, LOC_TYPEDEF,
7004 &objfile->global_psymbols,
1762568f 7005 0, cu->language, objfile);
530e8392 7006 break;
c906108c 7007 case DW_TAG_class_type:
680b30c7 7008 case DW_TAG_interface_type:
c906108c
SS
7009 case DW_TAG_structure_type:
7010 case DW_TAG_union_type:
7011 case DW_TAG_enumeration_type:
fa4028e9
JB
7012 /* Skip external references. The DWARF standard says in the section
7013 about "Structure, Union, and Class Type Entries": "An incomplete
7014 structure, union or class type is represented by a structure,
7015 union or class entry that does not have a byte size attribute
7016 and that has a DW_AT_declaration attribute." */
7017 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7018 {
15d034d0 7019 xfree (built_actual_name);
decbce07
MS
7020 return;
7021 }
fa4028e9 7022
63d06c5c
DC
7023 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7024 static vs. global. */
38d518c9 7025 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7026 built_actual_name != NULL,
176620f1 7027 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
7028 (cu->language == language_cplus
7029 || cu->language == language_java)
63d06c5c
DC
7030 ? &objfile->global_psymbols
7031 : &objfile->static_psymbols,
1762568f 7032 0, cu->language, objfile);
c906108c 7033
c906108c
SS
7034 break;
7035 case DW_TAG_enumerator:
38d518c9 7036 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7037 built_actual_name != NULL,
176620f1 7038 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
7039 (cu->language == language_cplus
7040 || cu->language == language_java)
f6fe98ef
DJ
7041 ? &objfile->global_psymbols
7042 : &objfile->static_psymbols,
1762568f 7043 0, cu->language, objfile);
c906108c
SS
7044 break;
7045 default:
7046 break;
7047 }
5c4e30ca 7048
15d034d0 7049 xfree (built_actual_name);
c906108c
SS
7050}
7051
5c4e30ca
DC
7052/* Read a partial die corresponding to a namespace; also, add a symbol
7053 corresponding to that namespace to the symbol table. NAMESPACE is
7054 the name of the enclosing namespace. */
91c24f0a 7055
72bf9492
DJ
7056static void
7057add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7058 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7059 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7060{
72bf9492 7061 /* Add a symbol for the namespace. */
e7c27a73 7062
72bf9492 7063 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7064
7065 /* Now scan partial symbols in that namespace. */
7066
91c24f0a 7067 if (pdi->has_children)
cdc07690 7068 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7069}
7070
5d7cb8df
JK
7071/* Read a partial die corresponding to a Fortran module. */
7072
7073static void
7074add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7075 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7076{
530e8392
KB
7077 /* Add a symbol for the namespace. */
7078
7079 add_partial_symbol (pdi, cu);
7080
f55ee35c 7081 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7082
7083 if (pdi->has_children)
cdc07690 7084 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7085}
7086
bc30ff58
JB
7087/* Read a partial die corresponding to a subprogram and create a partial
7088 symbol for that subprogram. When the CU language allows it, this
7089 routine also defines a partial symbol for each nested subprogram
cdc07690 7090 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7091 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7092 and highest PC values found in PDI.
6e70227d 7093
cdc07690
YQ
7094 PDI may also be a lexical block, in which case we simply search
7095 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7096 Again, this is only performed when the CU language allows this
7097 type of definitions. */
7098
7099static void
7100add_partial_subprogram (struct partial_die_info *pdi,
7101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7102 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7103{
7104 if (pdi->tag == DW_TAG_subprogram)
7105 {
7106 if (pdi->has_pc_info)
7107 {
7108 if (pdi->lowpc < *lowpc)
7109 *lowpc = pdi->lowpc;
7110 if (pdi->highpc > *highpc)
7111 *highpc = pdi->highpc;
cdc07690 7112 if (set_addrmap)
5734ee8b 7113 {
5734ee8b 7114 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7115 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7116 CORE_ADDR baseaddr;
7117 CORE_ADDR highpc;
7118 CORE_ADDR lowpc;
5734ee8b
DJ
7119
7120 baseaddr = ANOFFSET (objfile->section_offsets,
7121 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7122 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7123 pdi->lowpc + baseaddr);
7124 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7125 pdi->highpc + baseaddr);
7126 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7127 cu->per_cu->v.psymtab);
5734ee8b 7128 }
481860b3
GB
7129 }
7130
7131 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7132 {
bc30ff58 7133 if (!pdi->is_declaration)
e8d05480
JB
7134 /* Ignore subprogram DIEs that do not have a name, they are
7135 illegal. Do not emit a complaint at this point, we will
7136 do so when we convert this psymtab into a symtab. */
7137 if (pdi->name)
7138 add_partial_symbol (pdi, cu);
bc30ff58
JB
7139 }
7140 }
6e70227d 7141
bc30ff58
JB
7142 if (! pdi->has_children)
7143 return;
7144
7145 if (cu->language == language_ada)
7146 {
7147 pdi = pdi->die_child;
7148 while (pdi != NULL)
7149 {
7150 fixup_partial_die (pdi, cu);
7151 if (pdi->tag == DW_TAG_subprogram
7152 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7153 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7154 pdi = pdi->die_sibling;
7155 }
7156 }
7157}
7158
91c24f0a
DC
7159/* Read a partial die corresponding to an enumeration type. */
7160
72bf9492
DJ
7161static void
7162add_partial_enumeration (struct partial_die_info *enum_pdi,
7163 struct dwarf2_cu *cu)
91c24f0a 7164{
72bf9492 7165 struct partial_die_info *pdi;
91c24f0a
DC
7166
7167 if (enum_pdi->name != NULL)
72bf9492
DJ
7168 add_partial_symbol (enum_pdi, cu);
7169
7170 pdi = enum_pdi->die_child;
7171 while (pdi)
91c24f0a 7172 {
72bf9492 7173 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7174 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7175 else
72bf9492
DJ
7176 add_partial_symbol (pdi, cu);
7177 pdi = pdi->die_sibling;
91c24f0a 7178 }
91c24f0a
DC
7179}
7180
6caca83c
CC
7181/* Return the initial uleb128 in the die at INFO_PTR. */
7182
7183static unsigned int
d521ce57 7184peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7185{
7186 unsigned int bytes_read;
7187
7188 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7189}
7190
4bb7a0a7
DJ
7191/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7192 Return the corresponding abbrev, or NULL if the number is zero (indicating
7193 an empty DIE). In either case *BYTES_READ will be set to the length of
7194 the initial number. */
7195
7196static struct abbrev_info *
d521ce57 7197peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7198 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7199{
7200 bfd *abfd = cu->objfile->obfd;
7201 unsigned int abbrev_number;
7202 struct abbrev_info *abbrev;
7203
7204 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7205
7206 if (abbrev_number == 0)
7207 return NULL;
7208
433df2d4 7209 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7210 if (!abbrev)
7211 {
422b9917
DE
7212 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7213 " at offset 0x%x [in module %s]"),
7214 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7215 cu->header.offset.sect_off, bfd_get_filename (abfd));
4bb7a0a7
DJ
7216 }
7217
7218 return abbrev;
7219}
7220
93311388
DE
7221/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7222 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7223 DIE. Any children of the skipped DIEs will also be skipped. */
7224
d521ce57
TT
7225static const gdb_byte *
7226skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7227{
dee91e82 7228 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7229 struct abbrev_info *abbrev;
7230 unsigned int bytes_read;
7231
7232 while (1)
7233 {
7234 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7235 if (abbrev == NULL)
7236 return info_ptr + bytes_read;
7237 else
dee91e82 7238 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7239 }
7240}
7241
93311388
DE
7242/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7243 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7244 abbrev corresponding to that skipped uleb128 should be passed in
7245 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7246 children. */
7247
d521ce57
TT
7248static const gdb_byte *
7249skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7250 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7251{
7252 unsigned int bytes_read;
7253 struct attribute attr;
dee91e82
DE
7254 bfd *abfd = reader->abfd;
7255 struct dwarf2_cu *cu = reader->cu;
d521ce57 7256 const gdb_byte *buffer = reader->buffer;
f664829e 7257 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 7258 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
7259 unsigned int form, i;
7260
7261 for (i = 0; i < abbrev->num_attrs; i++)
7262 {
7263 /* The only abbrev we care about is DW_AT_sibling. */
7264 if (abbrev->attrs[i].name == DW_AT_sibling)
7265 {
dee91e82 7266 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7267 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7268 complaint (&symfile_complaints,
7269 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7270 else
b9502d3f
WN
7271 {
7272 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7273 const gdb_byte *sibling_ptr = buffer + off;
7274
7275 if (sibling_ptr < info_ptr)
7276 complaint (&symfile_complaints,
7277 _("DW_AT_sibling points backwards"));
22869d73
KS
7278 else if (sibling_ptr > reader->buffer_end)
7279 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7280 else
7281 return sibling_ptr;
7282 }
4bb7a0a7
DJ
7283 }
7284
7285 /* If it isn't DW_AT_sibling, skip this attribute. */
7286 form = abbrev->attrs[i].form;
7287 skip_attribute:
7288 switch (form)
7289 {
4bb7a0a7 7290 case DW_FORM_ref_addr:
ae411497
TT
7291 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7292 and later it is offset sized. */
7293 if (cu->header.version == 2)
7294 info_ptr += cu->header.addr_size;
7295 else
7296 info_ptr += cu->header.offset_size;
7297 break;
36586728
TT
7298 case DW_FORM_GNU_ref_alt:
7299 info_ptr += cu->header.offset_size;
7300 break;
ae411497 7301 case DW_FORM_addr:
4bb7a0a7
DJ
7302 info_ptr += cu->header.addr_size;
7303 break;
7304 case DW_FORM_data1:
7305 case DW_FORM_ref1:
7306 case DW_FORM_flag:
7307 info_ptr += 1;
7308 break;
2dc7f7b3
TT
7309 case DW_FORM_flag_present:
7310 break;
4bb7a0a7
DJ
7311 case DW_FORM_data2:
7312 case DW_FORM_ref2:
7313 info_ptr += 2;
7314 break;
7315 case DW_FORM_data4:
7316 case DW_FORM_ref4:
7317 info_ptr += 4;
7318 break;
7319 case DW_FORM_data8:
7320 case DW_FORM_ref8:
55f1336d 7321 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7322 info_ptr += 8;
7323 break;
7324 case DW_FORM_string:
9b1c24c8 7325 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7326 info_ptr += bytes_read;
7327 break;
2dc7f7b3 7328 case DW_FORM_sec_offset:
4bb7a0a7 7329 case DW_FORM_strp:
36586728 7330 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7331 info_ptr += cu->header.offset_size;
7332 break;
2dc7f7b3 7333 case DW_FORM_exprloc:
4bb7a0a7
DJ
7334 case DW_FORM_block:
7335 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7336 info_ptr += bytes_read;
7337 break;
7338 case DW_FORM_block1:
7339 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7340 break;
7341 case DW_FORM_block2:
7342 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7343 break;
7344 case DW_FORM_block4:
7345 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7346 break;
7347 case DW_FORM_sdata:
7348 case DW_FORM_udata:
7349 case DW_FORM_ref_udata:
3019eac3
DE
7350 case DW_FORM_GNU_addr_index:
7351 case DW_FORM_GNU_str_index:
d521ce57 7352 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7353 break;
7354 case DW_FORM_indirect:
7355 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7356 info_ptr += bytes_read;
7357 /* We need to continue parsing from here, so just go back to
7358 the top. */
7359 goto skip_attribute;
7360
7361 default:
3e43a32a
MS
7362 error (_("Dwarf Error: Cannot handle %s "
7363 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7364 dwarf_form_name (form),
7365 bfd_get_filename (abfd));
7366 }
7367 }
7368
7369 if (abbrev->has_children)
dee91e82 7370 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7371 else
7372 return info_ptr;
7373}
7374
93311388 7375/* Locate ORIG_PDI's sibling.
dee91e82 7376 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7377
d521ce57 7378static const gdb_byte *
dee91e82
DE
7379locate_pdi_sibling (const struct die_reader_specs *reader,
7380 struct partial_die_info *orig_pdi,
d521ce57 7381 const gdb_byte *info_ptr)
91c24f0a
DC
7382{
7383 /* Do we know the sibling already? */
72bf9492 7384
91c24f0a
DC
7385 if (orig_pdi->sibling)
7386 return orig_pdi->sibling;
7387
7388 /* Are there any children to deal with? */
7389
7390 if (!orig_pdi->has_children)
7391 return info_ptr;
7392
4bb7a0a7 7393 /* Skip the children the long way. */
91c24f0a 7394
dee91e82 7395 return skip_children (reader, info_ptr);
91c24f0a
DC
7396}
7397
257e7a09 7398/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7399 not NULL. */
c906108c
SS
7400
7401static void
257e7a09
YQ
7402dwarf2_read_symtab (struct partial_symtab *self,
7403 struct objfile *objfile)
c906108c 7404{
257e7a09 7405 if (self->readin)
c906108c 7406 {
442e4d9c 7407 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7408 self->filename);
442e4d9c
YQ
7409 }
7410 else
7411 {
7412 if (info_verbose)
c906108c 7413 {
442e4d9c 7414 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7415 self->filename);
442e4d9c 7416 gdb_flush (gdb_stdout);
c906108c 7417 }
c906108c 7418
442e4d9c
YQ
7419 /* Restore our global data. */
7420 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 7421
442e4d9c
YQ
7422 /* If this psymtab is constructed from a debug-only objfile, the
7423 has_section_at_zero flag will not necessarily be correct. We
7424 can get the correct value for this flag by looking at the data
7425 associated with the (presumably stripped) associated objfile. */
7426 if (objfile->separate_debug_objfile_backlink)
7427 {
7428 struct dwarf2_per_objfile *dpo_backlink
7429 = objfile_data (objfile->separate_debug_objfile_backlink,
7430 dwarf2_objfile_data_key);
9a619af0 7431
442e4d9c
YQ
7432 dwarf2_per_objfile->has_section_at_zero
7433 = dpo_backlink->has_section_at_zero;
7434 }
b2ab525c 7435
442e4d9c 7436 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7437
257e7a09 7438 psymtab_to_symtab_1 (self);
c906108c 7439
442e4d9c
YQ
7440 /* Finish up the debug error message. */
7441 if (info_verbose)
7442 printf_filtered (_("done.\n"));
c906108c 7443 }
95554aad
TT
7444
7445 process_cu_includes ();
c906108c 7446}
9cdd5dbd
DE
7447\f
7448/* Reading in full CUs. */
c906108c 7449
10b3939b
DJ
7450/* Add PER_CU to the queue. */
7451
7452static void
95554aad
TT
7453queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7454 enum language pretend_language)
10b3939b
DJ
7455{
7456 struct dwarf2_queue_item *item;
7457
7458 per_cu->queued = 1;
7459 item = xmalloc (sizeof (*item));
7460 item->per_cu = per_cu;
95554aad 7461 item->pretend_language = pretend_language;
10b3939b
DJ
7462 item->next = NULL;
7463
7464 if (dwarf2_queue == NULL)
7465 dwarf2_queue = item;
7466 else
7467 dwarf2_queue_tail->next = item;
7468
7469 dwarf2_queue_tail = item;
7470}
7471
89e63ee4
DE
7472/* If PER_CU is not yet queued, add it to the queue.
7473 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7474 dependency.
0907af0c 7475 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7476 meaning either PER_CU is already queued or it is already loaded.
7477
7478 N.B. There is an invariant here that if a CU is queued then it is loaded.
7479 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7480
7481static int
89e63ee4 7482maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7483 struct dwarf2_per_cu_data *per_cu,
7484 enum language pretend_language)
7485{
7486 /* We may arrive here during partial symbol reading, if we need full
7487 DIEs to process an unusual case (e.g. template arguments). Do
7488 not queue PER_CU, just tell our caller to load its DIEs. */
7489 if (dwarf2_per_objfile->reading_partial_symbols)
7490 {
7491 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7492 return 1;
7493 return 0;
7494 }
7495
7496 /* Mark the dependence relation so that we don't flush PER_CU
7497 too early. */
89e63ee4
DE
7498 if (dependent_cu != NULL)
7499 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7500
7501 /* If it's already on the queue, we have nothing to do. */
7502 if (per_cu->queued)
7503 return 0;
7504
7505 /* If the compilation unit is already loaded, just mark it as
7506 used. */
7507 if (per_cu->cu != NULL)
7508 {
7509 per_cu->cu->last_used = 0;
7510 return 0;
7511 }
7512
7513 /* Add it to the queue. */
7514 queue_comp_unit (per_cu, pretend_language);
7515
7516 return 1;
7517}
7518
10b3939b
DJ
7519/* Process the queue. */
7520
7521static void
a0f42c21 7522process_queue (void)
10b3939b
DJ
7523{
7524 struct dwarf2_queue_item *item, *next_item;
7525
b4f54984 7526 if (dwarf_read_debug)
45cfd468
DE
7527 {
7528 fprintf_unfiltered (gdb_stdlog,
7529 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7530 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7531 }
7532
03dd20cc
DJ
7533 /* The queue starts out with one item, but following a DIE reference
7534 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7535 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7536 {
cc12ce38
DE
7537 if ((dwarf2_per_objfile->using_index
7538 ? !item->per_cu->v.quick->compunit_symtab
7539 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7540 /* Skip dummy CUs. */
7541 && item->per_cu->cu != NULL)
f4dc4d17
DE
7542 {
7543 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7544 unsigned int debug_print_threshold;
247f5c4f 7545 char buf[100];
f4dc4d17 7546
247f5c4f 7547 if (per_cu->is_debug_types)
f4dc4d17 7548 {
247f5c4f
DE
7549 struct signatured_type *sig_type =
7550 (struct signatured_type *) per_cu;
7551
7552 sprintf (buf, "TU %s at offset 0x%x",
73be47f5
DE
7553 hex_string (sig_type->signature),
7554 per_cu->offset.sect_off);
7555 /* There can be 100s of TUs.
7556 Only print them in verbose mode. */
7557 debug_print_threshold = 2;
f4dc4d17 7558 }
247f5c4f 7559 else
73be47f5
DE
7560 {
7561 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7562 debug_print_threshold = 1;
7563 }
247f5c4f 7564
b4f54984 7565 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7566 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7567
7568 if (per_cu->is_debug_types)
7569 process_full_type_unit (per_cu, item->pretend_language);
7570 else
7571 process_full_comp_unit (per_cu, item->pretend_language);
7572
b4f54984 7573 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7574 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7575 }
10b3939b
DJ
7576
7577 item->per_cu->queued = 0;
7578 next_item = item->next;
7579 xfree (item);
7580 }
7581
7582 dwarf2_queue_tail = NULL;
45cfd468 7583
b4f54984 7584 if (dwarf_read_debug)
45cfd468
DE
7585 {
7586 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7587 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7588 }
10b3939b
DJ
7589}
7590
7591/* Free all allocated queue entries. This function only releases anything if
7592 an error was thrown; if the queue was processed then it would have been
7593 freed as we went along. */
7594
7595static void
7596dwarf2_release_queue (void *dummy)
7597{
7598 struct dwarf2_queue_item *item, *last;
7599
7600 item = dwarf2_queue;
7601 while (item)
7602 {
7603 /* Anything still marked queued is likely to be in an
7604 inconsistent state, so discard it. */
7605 if (item->per_cu->queued)
7606 {
7607 if (item->per_cu->cu != NULL)
dee91e82 7608 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7609 item->per_cu->queued = 0;
7610 }
7611
7612 last = item;
7613 item = item->next;
7614 xfree (last);
7615 }
7616
7617 dwarf2_queue = dwarf2_queue_tail = NULL;
7618}
7619
7620/* Read in full symbols for PST, and anything it depends on. */
7621
c906108c 7622static void
fba45db2 7623psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7624{
10b3939b 7625 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7626 int i;
7627
95554aad
TT
7628 if (pst->readin)
7629 return;
7630
aaa75496 7631 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7632 if (!pst->dependencies[i]->readin
7633 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7634 {
7635 /* Inform about additional files that need to be read in. */
7636 if (info_verbose)
7637 {
a3f17187 7638 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7639 fputs_filtered (" ", gdb_stdout);
7640 wrap_here ("");
7641 fputs_filtered ("and ", gdb_stdout);
7642 wrap_here ("");
7643 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7644 wrap_here (""); /* Flush output. */
aaa75496
JB
7645 gdb_flush (gdb_stdout);
7646 }
7647 psymtab_to_symtab_1 (pst->dependencies[i]);
7648 }
7649
e38df1d0 7650 per_cu = pst->read_symtab_private;
10b3939b
DJ
7651
7652 if (per_cu == NULL)
aaa75496
JB
7653 {
7654 /* It's an include file, no symbols to read for it.
7655 Everything is in the parent symtab. */
7656 pst->readin = 1;
7657 return;
7658 }
c906108c 7659
a0f42c21 7660 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7661}
7662
dee91e82
DE
7663/* Trivial hash function for die_info: the hash value of a DIE
7664 is its offset in .debug_info for this objfile. */
10b3939b 7665
dee91e82
DE
7666static hashval_t
7667die_hash (const void *item)
10b3939b 7668{
dee91e82 7669 const struct die_info *die = item;
6502dd73 7670
dee91e82
DE
7671 return die->offset.sect_off;
7672}
63d06c5c 7673
dee91e82
DE
7674/* Trivial comparison function for die_info structures: two DIEs
7675 are equal if they have the same offset. */
98bfdba5 7676
dee91e82
DE
7677static int
7678die_eq (const void *item_lhs, const void *item_rhs)
7679{
7680 const struct die_info *die_lhs = item_lhs;
7681 const struct die_info *die_rhs = item_rhs;
c906108c 7682
dee91e82
DE
7683 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7684}
c906108c 7685
dee91e82
DE
7686/* die_reader_func for load_full_comp_unit.
7687 This is identical to read_signatured_type_reader,
7688 but is kept separate for now. */
c906108c 7689
dee91e82
DE
7690static void
7691load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7692 const gdb_byte *info_ptr,
dee91e82
DE
7693 struct die_info *comp_unit_die,
7694 int has_children,
7695 void *data)
7696{
7697 struct dwarf2_cu *cu = reader->cu;
95554aad 7698 enum language *language_ptr = data;
6caca83c 7699
dee91e82
DE
7700 gdb_assert (cu->die_hash == NULL);
7701 cu->die_hash =
7702 htab_create_alloc_ex (cu->header.length / 12,
7703 die_hash,
7704 die_eq,
7705 NULL,
7706 &cu->comp_unit_obstack,
7707 hashtab_obstack_allocate,
7708 dummy_obstack_deallocate);
e142c38c 7709
dee91e82
DE
7710 if (has_children)
7711 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7712 &info_ptr, comp_unit_die);
7713 cu->dies = comp_unit_die;
7714 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7715
7716 /* We try not to read any attributes in this function, because not
9cdd5dbd 7717 all CUs needed for references have been loaded yet, and symbol
10b3939b 7718 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7719 or we won't be able to build types correctly.
7720 Similarly, if we do not read the producer, we can not apply
7721 producer-specific interpretation. */
95554aad 7722 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7723}
10b3939b 7724
dee91e82 7725/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7726
dee91e82 7727static void
95554aad
TT
7728load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7729 enum language pretend_language)
dee91e82 7730{
3019eac3 7731 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7732
f4dc4d17
DE
7733 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7734 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7735}
7736
3da10d80
KS
7737/* Add a DIE to the delayed physname list. */
7738
7739static void
7740add_to_method_list (struct type *type, int fnfield_index, int index,
7741 const char *name, struct die_info *die,
7742 struct dwarf2_cu *cu)
7743{
7744 struct delayed_method_info mi;
7745 mi.type = type;
7746 mi.fnfield_index = fnfield_index;
7747 mi.index = index;
7748 mi.name = name;
7749 mi.die = die;
7750 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7751}
7752
7753/* A cleanup for freeing the delayed method list. */
7754
7755static void
7756free_delayed_list (void *ptr)
7757{
7758 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7759 if (cu->method_list != NULL)
7760 {
7761 VEC_free (delayed_method_info, cu->method_list);
7762 cu->method_list = NULL;
7763 }
7764}
7765
7766/* Compute the physnames of any methods on the CU's method list.
7767
7768 The computation of method physnames is delayed in order to avoid the
7769 (bad) condition that one of the method's formal parameters is of an as yet
7770 incomplete type. */
7771
7772static void
7773compute_delayed_physnames (struct dwarf2_cu *cu)
7774{
7775 int i;
7776 struct delayed_method_info *mi;
7777 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7778 {
1d06ead6 7779 const char *physname;
3da10d80
KS
7780 struct fn_fieldlist *fn_flp
7781 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7782 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
7783 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7784 = physname ? physname : "";
3da10d80
KS
7785 }
7786}
7787
a766d390
DE
7788/* Go objects should be embedded in a DW_TAG_module DIE,
7789 and it's not clear if/how imported objects will appear.
7790 To keep Go support simple until that's worked out,
7791 go back through what we've read and create something usable.
7792 We could do this while processing each DIE, and feels kinda cleaner,
7793 but that way is more invasive.
7794 This is to, for example, allow the user to type "p var" or "b main"
7795 without having to specify the package name, and allow lookups
7796 of module.object to work in contexts that use the expression
7797 parser. */
7798
7799static void
7800fixup_go_packaging (struct dwarf2_cu *cu)
7801{
7802 char *package_name = NULL;
7803 struct pending *list;
7804 int i;
7805
7806 for (list = global_symbols; list != NULL; list = list->next)
7807 {
7808 for (i = 0; i < list->nsyms; ++i)
7809 {
7810 struct symbol *sym = list->symbol[i];
7811
7812 if (SYMBOL_LANGUAGE (sym) == language_go
7813 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7814 {
7815 char *this_package_name = go_symbol_package_name (sym);
7816
7817 if (this_package_name == NULL)
7818 continue;
7819 if (package_name == NULL)
7820 package_name = this_package_name;
7821 else
7822 {
7823 if (strcmp (package_name, this_package_name) != 0)
7824 complaint (&symfile_complaints,
7825 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
7826 (symbol_symtab (sym) != NULL
7827 ? symtab_to_filename_for_display
7828 (symbol_symtab (sym))
4262abfb 7829 : objfile_name (cu->objfile)),
a766d390
DE
7830 this_package_name, package_name);
7831 xfree (this_package_name);
7832 }
7833 }
7834 }
7835 }
7836
7837 if (package_name != NULL)
7838 {
7839 struct objfile *objfile = cu->objfile;
34a68019
TT
7840 const char *saved_package_name
7841 = obstack_copy0 (&objfile->per_bfd->storage_obstack,
7842 package_name,
7843 strlen (package_name));
a766d390 7844 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7845 saved_package_name, objfile);
a766d390
DE
7846 struct symbol *sym;
7847
7848 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7849
e623cf5d 7850 sym = allocate_symbol (objfile);
f85f34ed 7851 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7852 SYMBOL_SET_NAMES (sym, saved_package_name,
7853 strlen (saved_package_name), 0, objfile);
a766d390
DE
7854 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7855 e.g., "main" finds the "main" module and not C's main(). */
7856 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7857 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7858 SYMBOL_TYPE (sym) = type;
7859
7860 add_symbol_to_list (sym, &global_symbols);
7861
7862 xfree (package_name);
7863 }
7864}
7865
95554aad
TT
7866/* Return the symtab for PER_CU. This works properly regardless of
7867 whether we're using the index or psymtabs. */
7868
43f3e411
DE
7869static struct compunit_symtab *
7870get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
7871{
7872 return (dwarf2_per_objfile->using_index
43f3e411
DE
7873 ? per_cu->v.quick->compunit_symtab
7874 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
7875}
7876
7877/* A helper function for computing the list of all symbol tables
7878 included by PER_CU. */
7879
7880static void
43f3e411 7881recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 7882 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 7883 struct dwarf2_per_cu_data *per_cu,
43f3e411 7884 struct compunit_symtab *immediate_parent)
95554aad
TT
7885{
7886 void **slot;
7887 int ix;
43f3e411 7888 struct compunit_symtab *cust;
95554aad
TT
7889 struct dwarf2_per_cu_data *iter;
7890
7891 slot = htab_find_slot (all_children, per_cu, INSERT);
7892 if (*slot != NULL)
7893 {
7894 /* This inclusion and its children have been processed. */
7895 return;
7896 }
7897
7898 *slot = per_cu;
7899 /* Only add a CU if it has a symbol table. */
43f3e411
DE
7900 cust = get_compunit_symtab (per_cu);
7901 if (cust != NULL)
ec94af83
DE
7902 {
7903 /* If this is a type unit only add its symbol table if we haven't
7904 seen it yet (type unit per_cu's can share symtabs). */
7905 if (per_cu->is_debug_types)
7906 {
43f3e411 7907 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
7908 if (*slot == NULL)
7909 {
43f3e411
DE
7910 *slot = cust;
7911 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7912 if (cust->user == NULL)
7913 cust->user = immediate_parent;
ec94af83
DE
7914 }
7915 }
7916 else
f9125b6c 7917 {
43f3e411
DE
7918 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7919 if (cust->user == NULL)
7920 cust->user = immediate_parent;
f9125b6c 7921 }
ec94af83 7922 }
95554aad
TT
7923
7924 for (ix = 0;
796a7ff8 7925 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7926 ++ix)
ec94af83
DE
7927 {
7928 recursively_compute_inclusions (result, all_children,
43f3e411 7929 all_type_symtabs, iter, cust);
ec94af83 7930 }
95554aad
TT
7931}
7932
43f3e411 7933/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
7934 PER_CU. */
7935
7936static void
43f3e411 7937compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 7938{
f4dc4d17
DE
7939 gdb_assert (! per_cu->is_debug_types);
7940
796a7ff8 7941 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7942 {
7943 int ix, len;
ec94af83 7944 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
7945 struct compunit_symtab *compunit_symtab_iter;
7946 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 7947 htab_t all_children, all_type_symtabs;
43f3e411 7948 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
7949
7950 /* If we don't have a symtab, we can just skip this case. */
43f3e411 7951 if (cust == NULL)
95554aad
TT
7952 return;
7953
7954 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7955 NULL, xcalloc, xfree);
ec94af83
DE
7956 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7957 NULL, xcalloc, xfree);
95554aad
TT
7958
7959 for (ix = 0;
796a7ff8 7960 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7961 ix, per_cu_iter);
95554aad 7962 ++ix)
ec94af83
DE
7963 {
7964 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 7965 all_type_symtabs, per_cu_iter,
43f3e411 7966 cust);
ec94af83 7967 }
95554aad 7968
ec94af83 7969 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
7970 len = VEC_length (compunit_symtab_ptr, result_symtabs);
7971 cust->includes
95554aad 7972 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
666fcf91 7973 (len + 1) * sizeof (struct compunit_symtab *));
95554aad 7974 for (ix = 0;
43f3e411
DE
7975 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
7976 compunit_symtab_iter);
95554aad 7977 ++ix)
43f3e411
DE
7978 cust->includes[ix] = compunit_symtab_iter;
7979 cust->includes[len] = NULL;
95554aad 7980
43f3e411 7981 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 7982 htab_delete (all_children);
ec94af83 7983 htab_delete (all_type_symtabs);
95554aad
TT
7984 }
7985}
7986
7987/* Compute the 'includes' field for the symtabs of all the CUs we just
7988 read. */
7989
7990static void
7991process_cu_includes (void)
7992{
7993 int ix;
7994 struct dwarf2_per_cu_data *iter;
7995
7996 for (ix = 0;
7997 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7998 ix, iter);
7999 ++ix)
f4dc4d17
DE
8000 {
8001 if (! iter->is_debug_types)
43f3e411 8002 compute_compunit_symtab_includes (iter);
f4dc4d17 8003 }
95554aad
TT
8004
8005 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8006}
8007
9cdd5dbd 8008/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8009 already been loaded into memory. */
8010
8011static void
95554aad
TT
8012process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8013 enum language pretend_language)
10b3939b 8014{
10b3939b 8015 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8016 struct objfile *objfile = per_cu->objfile;
3e29f34a 8017 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8018 CORE_ADDR lowpc, highpc;
43f3e411 8019 struct compunit_symtab *cust;
3da10d80 8020 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8021 CORE_ADDR baseaddr;
4359dff1 8022 struct block *static_block;
3e29f34a 8023 CORE_ADDR addr;
10b3939b
DJ
8024
8025 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8026
10b3939b
DJ
8027 buildsym_init ();
8028 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8029 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8030
8031 cu->list_in_scope = &file_symbols;
c906108c 8032
95554aad
TT
8033 cu->language = pretend_language;
8034 cu->language_defn = language_def (cu->language);
8035
c906108c 8036 /* Do line number decoding in read_file_scope () */
10b3939b 8037 process_die (cu->dies, cu);
c906108c 8038
a766d390
DE
8039 /* For now fudge the Go package. */
8040 if (cu->language == language_go)
8041 fixup_go_packaging (cu);
8042
3da10d80
KS
8043 /* Now that we have processed all the DIEs in the CU, all the types
8044 should be complete, and it should now be safe to compute all of the
8045 physnames. */
8046 compute_delayed_physnames (cu);
8047 do_cleanups (delayed_list_cleanup);
8048
fae299cd
DC
8049 /* Some compilers don't define a DW_AT_high_pc attribute for the
8050 compilation unit. If the DW_AT_high_pc is missing, synthesize
8051 it, by scanning the DIE's below the compilation unit. */
10b3939b 8052 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8053
3e29f34a
MR
8054 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8055 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8056
8057 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8058 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8059 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8060 addrmap to help ensure it has an accurate map of pc values belonging to
8061 this comp unit. */
8062 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8063
43f3e411
DE
8064 cust = end_symtab_from_static_block (static_block,
8065 SECT_OFF_TEXT (objfile), 0);
c906108c 8066
43f3e411 8067 if (cust != NULL)
c906108c 8068 {
df15bd07 8069 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8070
8be455d7
JK
8071 /* Set symtab language to language from DW_AT_language. If the
8072 compilation is from a C file generated by language preprocessors, do
8073 not set the language if it was already deduced by start_subfile. */
43f3e411 8074 if (!(cu->language == language_c
40e3ad0e 8075 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8076 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8077
8078 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8079 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8080 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8081 there were bugs in prologue debug info, fixed later in GCC-4.5
8082 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8083
8084 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8085 needed, it would be wrong due to missing DW_AT_producer there.
8086
8087 Still one can confuse GDB by using non-standard GCC compilation
8088 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8089 */
ab260dad 8090 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8091 cust->locations_valid = 1;
e0d00bc7
JK
8092
8093 if (gcc_4_minor >= 5)
43f3e411 8094 cust->epilogue_unwind_valid = 1;
96408a79 8095
43f3e411 8096 cust->call_site_htab = cu->call_site_htab;
c906108c 8097 }
9291a0cd
TT
8098
8099 if (dwarf2_per_objfile->using_index)
43f3e411 8100 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8101 else
8102 {
8103 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8104 pst->compunit_symtab = cust;
9291a0cd
TT
8105 pst->readin = 1;
8106 }
c906108c 8107
95554aad
TT
8108 /* Push it for inclusion processing later. */
8109 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8110
c906108c 8111 do_cleanups (back_to);
f4dc4d17 8112}
45cfd468 8113
f4dc4d17
DE
8114/* Generate full symbol information for type unit PER_CU, whose DIEs have
8115 already been loaded into memory. */
8116
8117static void
8118process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8119 enum language pretend_language)
8120{
8121 struct dwarf2_cu *cu = per_cu->cu;
8122 struct objfile *objfile = per_cu->objfile;
43f3e411 8123 struct compunit_symtab *cust;
f4dc4d17 8124 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8125 struct signatured_type *sig_type;
8126
8127 gdb_assert (per_cu->is_debug_types);
8128 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8129
8130 buildsym_init ();
8131 back_to = make_cleanup (really_free_pendings, NULL);
8132 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8133
8134 cu->list_in_scope = &file_symbols;
8135
8136 cu->language = pretend_language;
8137 cu->language_defn = language_def (cu->language);
8138
8139 /* The symbol tables are set up in read_type_unit_scope. */
8140 process_die (cu->dies, cu);
8141
8142 /* For now fudge the Go package. */
8143 if (cu->language == language_go)
8144 fixup_go_packaging (cu);
8145
8146 /* Now that we have processed all the DIEs in the CU, all the types
8147 should be complete, and it should now be safe to compute all of the
8148 physnames. */
8149 compute_delayed_physnames (cu);
8150 do_cleanups (delayed_list_cleanup);
8151
8152 /* TUs share symbol tables.
8153 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8154 of it with end_expandable_symtab. Otherwise, complete the addition of
8155 this TU's symbols to the existing symtab. */
43f3e411 8156 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8157 {
43f3e411
DE
8158 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8159 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8160
43f3e411 8161 if (cust != NULL)
f4dc4d17
DE
8162 {
8163 /* Set symtab language to language from DW_AT_language. If the
8164 compilation is from a C file generated by language preprocessors,
8165 do not set the language if it was already deduced by
8166 start_subfile. */
43f3e411
DE
8167 if (!(cu->language == language_c
8168 && COMPUNIT_FILETABS (cust)->language != language_c))
8169 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8170 }
8171 }
8172 else
8173 {
0ab9ce85 8174 augment_type_symtab ();
43f3e411 8175 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8176 }
8177
8178 if (dwarf2_per_objfile->using_index)
43f3e411 8179 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8180 else
8181 {
8182 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8183 pst->compunit_symtab = cust;
f4dc4d17 8184 pst->readin = 1;
45cfd468 8185 }
f4dc4d17
DE
8186
8187 do_cleanups (back_to);
c906108c
SS
8188}
8189
95554aad
TT
8190/* Process an imported unit DIE. */
8191
8192static void
8193process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8194{
8195 struct attribute *attr;
8196
f4dc4d17
DE
8197 /* For now we don't handle imported units in type units. */
8198 if (cu->per_cu->is_debug_types)
8199 {
8200 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8201 " supported in type units [in module %s]"),
4262abfb 8202 objfile_name (cu->objfile));
f4dc4d17
DE
8203 }
8204
95554aad
TT
8205 attr = dwarf2_attr (die, DW_AT_import, cu);
8206 if (attr != NULL)
8207 {
8208 struct dwarf2_per_cu_data *per_cu;
8209 struct symtab *imported_symtab;
8210 sect_offset offset;
36586728 8211 int is_dwz;
95554aad
TT
8212
8213 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
8214 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8215 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 8216
69d751e3 8217 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8218 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8219 load_full_comp_unit (per_cu, cu->language);
8220
796a7ff8 8221 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8222 per_cu);
8223 }
8224}
8225
adde2bff
DE
8226/* Reset the in_process bit of a die. */
8227
8228static void
8229reset_die_in_process (void *arg)
8230{
8231 struct die_info *die = arg;
8c3cb9fa 8232
adde2bff
DE
8233 die->in_process = 0;
8234}
8235
c906108c
SS
8236/* Process a die and its children. */
8237
8238static void
e7c27a73 8239process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8240{
adde2bff
DE
8241 struct cleanup *in_process;
8242
8243 /* We should only be processing those not already in process. */
8244 gdb_assert (!die->in_process);
8245
8246 die->in_process = 1;
8247 in_process = make_cleanup (reset_die_in_process,die);
8248
c906108c
SS
8249 switch (die->tag)
8250 {
8251 case DW_TAG_padding:
8252 break;
8253 case DW_TAG_compile_unit:
95554aad 8254 case DW_TAG_partial_unit:
e7c27a73 8255 read_file_scope (die, cu);
c906108c 8256 break;
348e048f
DE
8257 case DW_TAG_type_unit:
8258 read_type_unit_scope (die, cu);
8259 break;
c906108c 8260 case DW_TAG_subprogram:
c906108c 8261 case DW_TAG_inlined_subroutine:
edb3359d 8262 read_func_scope (die, cu);
c906108c
SS
8263 break;
8264 case DW_TAG_lexical_block:
14898363
L
8265 case DW_TAG_try_block:
8266 case DW_TAG_catch_block:
e7c27a73 8267 read_lexical_block_scope (die, cu);
c906108c 8268 break;
96408a79
SA
8269 case DW_TAG_GNU_call_site:
8270 read_call_site_scope (die, cu);
8271 break;
c906108c 8272 case DW_TAG_class_type:
680b30c7 8273 case DW_TAG_interface_type:
c906108c
SS
8274 case DW_TAG_structure_type:
8275 case DW_TAG_union_type:
134d01f1 8276 process_structure_scope (die, cu);
c906108c
SS
8277 break;
8278 case DW_TAG_enumeration_type:
134d01f1 8279 process_enumeration_scope (die, cu);
c906108c 8280 break;
134d01f1 8281
f792889a
DJ
8282 /* These dies have a type, but processing them does not create
8283 a symbol or recurse to process the children. Therefore we can
8284 read them on-demand through read_type_die. */
c906108c 8285 case DW_TAG_subroutine_type:
72019c9c 8286 case DW_TAG_set_type:
c906108c 8287 case DW_TAG_array_type:
c906108c 8288 case DW_TAG_pointer_type:
c906108c 8289 case DW_TAG_ptr_to_member_type:
c906108c 8290 case DW_TAG_reference_type:
c906108c 8291 case DW_TAG_string_type:
c906108c 8292 break;
134d01f1 8293
c906108c 8294 case DW_TAG_base_type:
a02abb62 8295 case DW_TAG_subrange_type:
cb249c71 8296 case DW_TAG_typedef:
134d01f1
DJ
8297 /* Add a typedef symbol for the type definition, if it has a
8298 DW_AT_name. */
f792889a 8299 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8300 break;
c906108c 8301 case DW_TAG_common_block:
e7c27a73 8302 read_common_block (die, cu);
c906108c
SS
8303 break;
8304 case DW_TAG_common_inclusion:
8305 break;
d9fa45fe 8306 case DW_TAG_namespace:
4d4ec4e5 8307 cu->processing_has_namespace_info = 1;
e7c27a73 8308 read_namespace (die, cu);
d9fa45fe 8309 break;
5d7cb8df 8310 case DW_TAG_module:
4d4ec4e5 8311 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8312 read_module (die, cu);
8313 break;
d9fa45fe 8314 case DW_TAG_imported_declaration:
74921315
KS
8315 cu->processing_has_namespace_info = 1;
8316 if (read_namespace_alias (die, cu))
8317 break;
8318 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8319 case DW_TAG_imported_module:
4d4ec4e5 8320 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8321 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8322 || cu->language != language_fortran))
8323 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8324 dwarf_tag_name (die->tag));
8325 read_import_statement (die, cu);
d9fa45fe 8326 break;
95554aad
TT
8327
8328 case DW_TAG_imported_unit:
8329 process_imported_unit_die (die, cu);
8330 break;
8331
c906108c 8332 default:
e7c27a73 8333 new_symbol (die, NULL, cu);
c906108c
SS
8334 break;
8335 }
adde2bff
DE
8336
8337 do_cleanups (in_process);
c906108c 8338}
ca69b9e6
DE
8339\f
8340/* DWARF name computation. */
c906108c 8341
94af9270
KS
8342/* A helper function for dwarf2_compute_name which determines whether DIE
8343 needs to have the name of the scope prepended to the name listed in the
8344 die. */
8345
8346static int
8347die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8348{
1c809c68
TT
8349 struct attribute *attr;
8350
94af9270
KS
8351 switch (die->tag)
8352 {
8353 case DW_TAG_namespace:
8354 case DW_TAG_typedef:
8355 case DW_TAG_class_type:
8356 case DW_TAG_interface_type:
8357 case DW_TAG_structure_type:
8358 case DW_TAG_union_type:
8359 case DW_TAG_enumeration_type:
8360 case DW_TAG_enumerator:
8361 case DW_TAG_subprogram:
08a76f8a 8362 case DW_TAG_inlined_subroutine:
94af9270 8363 case DW_TAG_member:
74921315 8364 case DW_TAG_imported_declaration:
94af9270
KS
8365 return 1;
8366
8367 case DW_TAG_variable:
c2b0a229 8368 case DW_TAG_constant:
94af9270
KS
8369 /* We only need to prefix "globally" visible variables. These include
8370 any variable marked with DW_AT_external or any variable that
8371 lives in a namespace. [Variables in anonymous namespaces
8372 require prefixing, but they are not DW_AT_external.] */
8373
8374 if (dwarf2_attr (die, DW_AT_specification, cu))
8375 {
8376 struct dwarf2_cu *spec_cu = cu;
9a619af0 8377
94af9270
KS
8378 return die_needs_namespace (die_specification (die, &spec_cu),
8379 spec_cu);
8380 }
8381
1c809c68 8382 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8383 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8384 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8385 return 0;
8386 /* A variable in a lexical block of some kind does not need a
8387 namespace, even though in C++ such variables may be external
8388 and have a mangled name. */
8389 if (die->parent->tag == DW_TAG_lexical_block
8390 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8391 || die->parent->tag == DW_TAG_catch_block
8392 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8393 return 0;
8394 return 1;
94af9270
KS
8395
8396 default:
8397 return 0;
8398 }
8399}
8400
98bfdba5
PA
8401/* Retrieve the last character from a mem_file. */
8402
8403static void
8404do_ui_file_peek_last (void *object, const char *buffer, long length)
8405{
8406 char *last_char_p = (char *) object;
8407
8408 if (length > 0)
8409 *last_char_p = buffer[length - 1];
8410}
8411
94af9270 8412/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
8413 compute the physname for the object, which include a method's:
8414 - formal parameters (C++/Java),
8415 - receiver type (Go),
8416 - return type (Java).
8417
8418 The term "physname" is a bit confusing.
8419 For C++, for example, it is the demangled name.
8420 For Go, for example, it's the mangled name.
94af9270 8421
af6b7be1
JB
8422 For Ada, return the DIE's linkage name rather than the fully qualified
8423 name. PHYSNAME is ignored..
8424
94af9270
KS
8425 The result is allocated on the objfile_obstack and canonicalized. */
8426
8427static const char *
15d034d0
TT
8428dwarf2_compute_name (const char *name,
8429 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8430 int physname)
8431{
bb5ed363
DE
8432 struct objfile *objfile = cu->objfile;
8433
94af9270
KS
8434 if (name == NULL)
8435 name = dwarf2_name (die, cu);
8436
f55ee35c
JK
8437 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
8438 compute it by typename_concat inside GDB. */
8439 if (cu->language == language_ada
8440 || (cu->language == language_fortran && physname))
8441 {
8442 /* For Ada unit, we prefer the linkage name over the name, as
8443 the former contains the exported name, which the user expects
8444 to be able to reference. Ideally, we want the user to be able
8445 to reference this entity using either natural or linkage name,
8446 but we haven't started looking at this enhancement yet. */
7d45c7c3 8447 const char *name;
f55ee35c 8448
7d45c7c3
KB
8449 name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8450 if (name == NULL)
8451 name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8452 if (name != NULL)
8453 return name;
f55ee35c
JK
8454 }
8455
94af9270
KS
8456 /* These are the only languages we know how to qualify names in. */
8457 if (name != NULL
f55ee35c 8458 && (cu->language == language_cplus || cu->language == language_java
45280282 8459 || cu->language == language_fortran || cu->language == language_d))
94af9270
KS
8460 {
8461 if (die_needs_namespace (die, cu))
8462 {
8463 long length;
0d5cff50 8464 const char *prefix;
94af9270 8465 struct ui_file *buf;
34a68019
TT
8466 char *intermediate_name;
8467 const char *canonical_name = NULL;
94af9270
KS
8468
8469 prefix = determine_prefix (die, cu);
8470 buf = mem_fileopen ();
8471 if (*prefix != '\0')
8472 {
f55ee35c
JK
8473 char *prefixed_name = typename_concat (NULL, prefix, name,
8474 physname, cu);
9a619af0 8475
94af9270
KS
8476 fputs_unfiltered (prefixed_name, buf);
8477 xfree (prefixed_name);
8478 }
8479 else
62d5b8da 8480 fputs_unfiltered (name, buf);
94af9270 8481
98bfdba5
PA
8482 /* Template parameters may be specified in the DIE's DW_AT_name, or
8483 as children with DW_TAG_template_type_param or
8484 DW_TAG_value_type_param. If the latter, add them to the name
8485 here. If the name already has template parameters, then
8486 skip this step; some versions of GCC emit both, and
8487 it is more efficient to use the pre-computed name.
8488
8489 Something to keep in mind about this process: it is very
8490 unlikely, or in some cases downright impossible, to produce
8491 something that will match the mangled name of a function.
8492 If the definition of the function has the same debug info,
8493 we should be able to match up with it anyway. But fallbacks
8494 using the minimal symbol, for instance to find a method
8495 implemented in a stripped copy of libstdc++, will not work.
8496 If we do not have debug info for the definition, we will have to
8497 match them up some other way.
8498
8499 When we do name matching there is a related problem with function
8500 templates; two instantiated function templates are allowed to
8501 differ only by their return types, which we do not add here. */
8502
8503 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8504 {
8505 struct attribute *attr;
8506 struct die_info *child;
8507 int first = 1;
8508
8509 die->building_fullname = 1;
8510
8511 for (child = die->child; child != NULL; child = child->sibling)
8512 {
8513 struct type *type;
12df843f 8514 LONGEST value;
d521ce57 8515 const gdb_byte *bytes;
98bfdba5
PA
8516 struct dwarf2_locexpr_baton *baton;
8517 struct value *v;
8518
8519 if (child->tag != DW_TAG_template_type_param
8520 && child->tag != DW_TAG_template_value_param)
8521 continue;
8522
8523 if (first)
8524 {
8525 fputs_unfiltered ("<", buf);
8526 first = 0;
8527 }
8528 else
8529 fputs_unfiltered (", ", buf);
8530
8531 attr = dwarf2_attr (child, DW_AT_type, cu);
8532 if (attr == NULL)
8533 {
8534 complaint (&symfile_complaints,
8535 _("template parameter missing DW_AT_type"));
8536 fputs_unfiltered ("UNKNOWN_TYPE", buf);
8537 continue;
8538 }
8539 type = die_type (child, cu);
8540
8541 if (child->tag == DW_TAG_template_type_param)
8542 {
79d43c61 8543 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8544 continue;
8545 }
8546
8547 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8548 if (attr == NULL)
8549 {
8550 complaint (&symfile_complaints,
3e43a32a
MS
8551 _("template parameter missing "
8552 "DW_AT_const_value"));
98bfdba5
PA
8553 fputs_unfiltered ("UNKNOWN_VALUE", buf);
8554 continue;
8555 }
8556
8557 dwarf2_const_value_attr (attr, type, name,
8558 &cu->comp_unit_obstack, cu,
8559 &value, &bytes, &baton);
8560
8561 if (TYPE_NOSIGN (type))
8562 /* GDB prints characters as NUMBER 'CHAR'. If that's
8563 changed, this can use value_print instead. */
8564 c_printchar (value, type, buf);
8565 else
8566 {
8567 struct value_print_options opts;
8568
8569 if (baton != NULL)
8570 v = dwarf2_evaluate_loc_desc (type, NULL,
8571 baton->data,
8572 baton->size,
8573 baton->per_cu);
8574 else if (bytes != NULL)
8575 {
8576 v = allocate_value (type);
8577 memcpy (value_contents_writeable (v), bytes,
8578 TYPE_LENGTH (type));
8579 }
8580 else
8581 v = value_from_longest (type, value);
8582
3e43a32a
MS
8583 /* Specify decimal so that we do not depend on
8584 the radix. */
98bfdba5
PA
8585 get_formatted_print_options (&opts, 'd');
8586 opts.raw = 1;
8587 value_print (v, buf, &opts);
8588 release_value (v);
8589 value_free (v);
8590 }
8591 }
8592
8593 die->building_fullname = 0;
8594
8595 if (!first)
8596 {
8597 /* Close the argument list, with a space if necessary
8598 (nested templates). */
8599 char last_char = '\0';
8600 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8601 if (last_char == '>')
8602 fputs_unfiltered (" >", buf);
8603 else
8604 fputs_unfiltered (">", buf);
8605 }
8606 }
8607
94af9270
KS
8608 /* For Java and C++ methods, append formal parameter type
8609 information, if PHYSNAME. */
6e70227d 8610
94af9270
KS
8611 if (physname && die->tag == DW_TAG_subprogram
8612 && (cu->language == language_cplus
8613 || cu->language == language_java))
8614 {
8615 struct type *type = read_type_die (die, cu);
8616
79d43c61
TT
8617 c_type_print_args (type, buf, 1, cu->language,
8618 &type_print_raw_options);
94af9270
KS
8619
8620 if (cu->language == language_java)
8621 {
8622 /* For java, we must append the return type to method
0963b4bd 8623 names. */
94af9270
KS
8624 if (die->tag == DW_TAG_subprogram)
8625 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8626 0, 0, &type_print_raw_options);
94af9270
KS
8627 }
8628 else if (cu->language == language_cplus)
8629 {
60430eff
DJ
8630 /* Assume that an artificial first parameter is
8631 "this", but do not crash if it is not. RealView
8632 marks unnamed (and thus unused) parameters as
8633 artificial; there is no way to differentiate
8634 the two cases. */
94af9270
KS
8635 if (TYPE_NFIELDS (type) > 0
8636 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8637 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8638 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8639 0))))
94af9270
KS
8640 fputs_unfiltered (" const", buf);
8641 }
8642 }
8643
34a68019 8644 intermediate_name = ui_file_xstrdup (buf, &length);
94af9270
KS
8645 ui_file_delete (buf);
8646
8647 if (cu->language == language_cplus)
34a68019
TT
8648 canonical_name
8649 = dwarf2_canonicalize_name (intermediate_name, cu,
8650 &objfile->per_bfd->storage_obstack);
8651
8652 /* If we only computed INTERMEDIATE_NAME, or if
8653 INTERMEDIATE_NAME is already canonical, then we need to
8654 copy it to the appropriate obstack. */
8655 if (canonical_name == NULL || canonical_name == intermediate_name)
8656 name = obstack_copy0 (&objfile->per_bfd->storage_obstack,
8657 intermediate_name,
8658 strlen (intermediate_name));
8659 else
8660 name = canonical_name;
9a619af0 8661
34a68019 8662 xfree (intermediate_name);
94af9270
KS
8663 }
8664 }
8665
8666 return name;
8667}
8668
0114d602
DJ
8669/* Return the fully qualified name of DIE, based on its DW_AT_name.
8670 If scope qualifiers are appropriate they will be added. The result
34a68019 8671 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8672 not have a name. NAME may either be from a previous call to
8673 dwarf2_name or NULL.
8674
0963b4bd 8675 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8676
8677static const char *
15d034d0 8678dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8679{
94af9270
KS
8680 return dwarf2_compute_name (name, die, cu, 0);
8681}
0114d602 8682
94af9270
KS
8683/* Construct a physname for the given DIE in CU. NAME may either be
8684 from a previous call to dwarf2_name or NULL. The result will be
8685 allocated on the objfile_objstack or NULL if the DIE does not have a
8686 name.
0114d602 8687
94af9270 8688 The output string will be canonicalized (if C++/Java). */
0114d602 8689
94af9270 8690static const char *
15d034d0 8691dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8692{
bb5ed363 8693 struct objfile *objfile = cu->objfile;
900e11f9
JK
8694 struct attribute *attr;
8695 const char *retval, *mangled = NULL, *canon = NULL;
8696 struct cleanup *back_to;
8697 int need_copy = 1;
8698
8699 /* In this case dwarf2_compute_name is just a shortcut not building anything
8700 on its own. */
8701 if (!die_needs_namespace (die, cu))
8702 return dwarf2_compute_name (name, die, cu, 1);
8703
8704 back_to = make_cleanup (null_cleanup, NULL);
8705
7d45c7c3
KB
8706 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8707 if (mangled == NULL)
8708 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
900e11f9
JK
8709
8710 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8711 has computed. */
7d45c7c3 8712 if (mangled != NULL)
900e11f9
JK
8713 {
8714 char *demangled;
8715
900e11f9
JK
8716 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8717 type. It is easier for GDB users to search for such functions as
8718 `name(params)' than `long name(params)'. In such case the minimal
8719 symbol names do not match the full symbol names but for template
8720 functions there is never a need to look up their definition from their
8721 declaration so the only disadvantage remains the minimal symbol
8722 variant `long name(params)' does not have the proper inferior type.
8723 */
8724
a766d390
DE
8725 if (cu->language == language_go)
8726 {
8727 /* This is a lie, but we already lie to the caller new_symbol_full.
8728 new_symbol_full assumes we return the mangled name.
8729 This just undoes that lie until things are cleaned up. */
8730 demangled = NULL;
8731 }
8732 else
8733 {
8de20a37
TT
8734 demangled = gdb_demangle (mangled,
8735 (DMGL_PARAMS | DMGL_ANSI
8736 | (cu->language == language_java
8737 ? DMGL_JAVA | DMGL_RET_POSTFIX
8738 : DMGL_RET_DROP)));
a766d390 8739 }
900e11f9
JK
8740 if (demangled)
8741 {
8742 make_cleanup (xfree, demangled);
8743 canon = demangled;
8744 }
8745 else
8746 {
8747 canon = mangled;
8748 need_copy = 0;
8749 }
8750 }
8751
8752 if (canon == NULL || check_physname)
8753 {
8754 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8755
8756 if (canon != NULL && strcmp (physname, canon) != 0)
8757 {
8758 /* It may not mean a bug in GDB. The compiler could also
8759 compute DW_AT_linkage_name incorrectly. But in such case
8760 GDB would need to be bug-to-bug compatible. */
8761
8762 complaint (&symfile_complaints,
8763 _("Computed physname <%s> does not match demangled <%s> "
8764 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
4262abfb
JK
8765 physname, canon, mangled, die->offset.sect_off,
8766 objfile_name (objfile));
900e11f9
JK
8767
8768 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8769 is available here - over computed PHYSNAME. It is safer
8770 against both buggy GDB and buggy compilers. */
8771
8772 retval = canon;
8773 }
8774 else
8775 {
8776 retval = physname;
8777 need_copy = 0;
8778 }
8779 }
8780 else
8781 retval = canon;
8782
8783 if (need_copy)
34a68019
TT
8784 retval = obstack_copy0 (&objfile->per_bfd->storage_obstack,
8785 retval, strlen (retval));
900e11f9
JK
8786
8787 do_cleanups (back_to);
8788 return retval;
0114d602
DJ
8789}
8790
74921315
KS
8791/* Inspect DIE in CU for a namespace alias. If one exists, record
8792 a new symbol for it.
8793
8794 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8795
8796static int
8797read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8798{
8799 struct attribute *attr;
8800
8801 /* If the die does not have a name, this is not a namespace
8802 alias. */
8803 attr = dwarf2_attr (die, DW_AT_name, cu);
8804 if (attr != NULL)
8805 {
8806 int num;
8807 struct die_info *d = die;
8808 struct dwarf2_cu *imported_cu = cu;
8809
8810 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8811 keep inspecting DIEs until we hit the underlying import. */
8812#define MAX_NESTED_IMPORTED_DECLARATIONS 100
8813 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8814 {
8815 attr = dwarf2_attr (d, DW_AT_import, cu);
8816 if (attr == NULL)
8817 break;
8818
8819 d = follow_die_ref (d, attr, &imported_cu);
8820 if (d->tag != DW_TAG_imported_declaration)
8821 break;
8822 }
8823
8824 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8825 {
8826 complaint (&symfile_complaints,
8827 _("DIE at 0x%x has too many recursively imported "
8828 "declarations"), d->offset.sect_off);
8829 return 0;
8830 }
8831
8832 if (attr != NULL)
8833 {
8834 struct type *type;
8835 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8836
8837 type = get_die_type_at_offset (offset, cu->per_cu);
8838 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8839 {
8840 /* This declaration is a global namespace alias. Add
8841 a symbol for it whose type is the aliased namespace. */
8842 new_symbol (die, type, cu);
8843 return 1;
8844 }
8845 }
8846 }
8847
8848 return 0;
8849}
8850
22cee43f
PMR
8851/* Return the using directives repository (global or local?) to use in the
8852 current context for LANGUAGE.
8853
8854 For Ada, imported declarations can materialize renamings, which *may* be
8855 global. However it is impossible (for now?) in DWARF to distinguish
8856 "external" imported declarations and "static" ones. As all imported
8857 declarations seem to be static in all other languages, make them all CU-wide
8858 global only in Ada. */
8859
8860static struct using_direct **
8861using_directives (enum language language)
8862{
8863 if (language == language_ada && context_stack_depth == 0)
8864 return &global_using_directives;
8865 else
8866 return &local_using_directives;
8867}
8868
27aa8d6a
SW
8869/* Read the import statement specified by the given die and record it. */
8870
8871static void
8872read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8873{
bb5ed363 8874 struct objfile *objfile = cu->objfile;
27aa8d6a 8875 struct attribute *import_attr;
32019081 8876 struct die_info *imported_die, *child_die;
de4affc9 8877 struct dwarf2_cu *imported_cu;
27aa8d6a 8878 const char *imported_name;
794684b6 8879 const char *imported_name_prefix;
13387711
SW
8880 const char *canonical_name;
8881 const char *import_alias;
8882 const char *imported_declaration = NULL;
794684b6 8883 const char *import_prefix;
32019081
JK
8884 VEC (const_char_ptr) *excludes = NULL;
8885 struct cleanup *cleanups;
13387711 8886
27aa8d6a
SW
8887 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8888 if (import_attr == NULL)
8889 {
8890 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8891 dwarf_tag_name (die->tag));
8892 return;
8893 }
8894
de4affc9
CC
8895 imported_cu = cu;
8896 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8897 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8898 if (imported_name == NULL)
8899 {
8900 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8901
8902 The import in the following code:
8903 namespace A
8904 {
8905 typedef int B;
8906 }
8907
8908 int main ()
8909 {
8910 using A::B;
8911 B b;
8912 return b;
8913 }
8914
8915 ...
8916 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8917 <52> DW_AT_decl_file : 1
8918 <53> DW_AT_decl_line : 6
8919 <54> DW_AT_import : <0x75>
8920 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8921 <59> DW_AT_name : B
8922 <5b> DW_AT_decl_file : 1
8923 <5c> DW_AT_decl_line : 2
8924 <5d> DW_AT_type : <0x6e>
8925 ...
8926 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8927 <76> DW_AT_byte_size : 4
8928 <77> DW_AT_encoding : 5 (signed)
8929
8930 imports the wrong die ( 0x75 instead of 0x58 ).
8931 This case will be ignored until the gcc bug is fixed. */
8932 return;
8933 }
8934
82856980
SW
8935 /* Figure out the local name after import. */
8936 import_alias = dwarf2_name (die, cu);
27aa8d6a 8937
794684b6
SW
8938 /* Figure out where the statement is being imported to. */
8939 import_prefix = determine_prefix (die, cu);
8940
8941 /* Figure out what the scope of the imported die is and prepend it
8942 to the name of the imported die. */
de4affc9 8943 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8944
f55ee35c
JK
8945 if (imported_die->tag != DW_TAG_namespace
8946 && imported_die->tag != DW_TAG_module)
794684b6 8947 {
13387711
SW
8948 imported_declaration = imported_name;
8949 canonical_name = imported_name_prefix;
794684b6 8950 }
13387711 8951 else if (strlen (imported_name_prefix) > 0)
12aaed36 8952 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
8953 imported_name_prefix,
8954 (cu->language == language_d ? "." : "::"),
8955 imported_name, (char *) NULL);
13387711
SW
8956 else
8957 canonical_name = imported_name;
794684b6 8958
32019081
JK
8959 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8960
8961 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8962 for (child_die = die->child; child_die && child_die->tag;
8963 child_die = sibling_die (child_die))
8964 {
8965 /* DWARF-4: A Fortran use statement with a “rename list” may be
8966 represented by an imported module entry with an import attribute
8967 referring to the module and owned entries corresponding to those
8968 entities that are renamed as part of being imported. */
8969
8970 if (child_die->tag != DW_TAG_imported_declaration)
8971 {
8972 complaint (&symfile_complaints,
8973 _("child DW_TAG_imported_declaration expected "
8974 "- DIE at 0x%x [in module %s]"),
4262abfb 8975 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
8976 continue;
8977 }
8978
8979 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8980 if (import_attr == NULL)
8981 {
8982 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8983 dwarf_tag_name (child_die->tag));
8984 continue;
8985 }
8986
8987 imported_cu = cu;
8988 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8989 &imported_cu);
8990 imported_name = dwarf2_name (imported_die, imported_cu);
8991 if (imported_name == NULL)
8992 {
8993 complaint (&symfile_complaints,
8994 _("child DW_TAG_imported_declaration has unknown "
8995 "imported name - DIE at 0x%x [in module %s]"),
4262abfb 8996 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
8997 continue;
8998 }
8999
9000 VEC_safe_push (const_char_ptr, excludes, imported_name);
9001
9002 process_die (child_die, cu);
9003 }
9004
22cee43f
PMR
9005 add_using_directive (using_directives (cu->language),
9006 import_prefix,
9007 canonical_name,
9008 import_alias,
9009 imported_declaration,
9010 excludes,
9011 0,
9012 &objfile->objfile_obstack);
32019081
JK
9013
9014 do_cleanups (cleanups);
27aa8d6a
SW
9015}
9016
f4dc4d17 9017/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 9018
cb1df416
DJ
9019static void
9020free_cu_line_header (void *arg)
9021{
9022 struct dwarf2_cu *cu = arg;
9023
9024 free_line_header (cu->line_header);
9025 cu->line_header = NULL;
9026}
9027
1b80a9fa
JK
9028/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9029 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9030 this, it was first present in GCC release 4.3.0. */
9031
9032static int
9033producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9034{
9035 if (!cu->checked_producer)
9036 check_producer (cu);
9037
9038 return cu->producer_is_gcc_lt_4_3;
9039}
9040
9291a0cd
TT
9041static void
9042find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 9043 const char **name, const char **comp_dir)
9291a0cd 9044{
9291a0cd
TT
9045 /* Find the filename. Do not use dwarf2_name here, since the filename
9046 is not a source language identifier. */
7d45c7c3
KB
9047 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9048 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9049
7d45c7c3
KB
9050 if (*comp_dir == NULL
9051 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9052 && IS_ABSOLUTE_PATH (*name))
9291a0cd 9053 {
15d034d0
TT
9054 char *d = ldirname (*name);
9055
9056 *comp_dir = d;
9057 if (d != NULL)
9058 make_cleanup (xfree, d);
9291a0cd
TT
9059 }
9060 if (*comp_dir != NULL)
9061 {
9062 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9063 directory, get rid of it. */
9064 char *cp = strchr (*comp_dir, ':');
9065
9066 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9067 *comp_dir = cp + 1;
9068 }
9069
9070 if (*name == NULL)
9071 *name = "<unknown>";
9072}
9073
f4dc4d17
DE
9074/* Handle DW_AT_stmt_list for a compilation unit.
9075 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9076 COMP_DIR is the compilation directory. LOWPC is passed to
9077 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9078
9079static void
9080handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9081 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9082{
527f3840 9083 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9084 struct attribute *attr;
527f3840
JK
9085 unsigned int line_offset;
9086 struct line_header line_header_local;
9087 hashval_t line_header_local_hash;
9088 unsigned u;
9089 void **slot;
9090 int decode_mapping;
2ab95328 9091
f4dc4d17
DE
9092 gdb_assert (! cu->per_cu->is_debug_types);
9093
2ab95328 9094 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9095 if (attr == NULL)
9096 return;
9097
9098 line_offset = DW_UNSND (attr);
9099
9100 /* The line header hash table is only created if needed (it exists to
9101 prevent redundant reading of the line table for partial_units).
9102 If we're given a partial_unit, we'll need it. If we're given a
9103 compile_unit, then use the line header hash table if it's already
9104 created, but don't create one just yet. */
9105
9106 if (dwarf2_per_objfile->line_header_hash == NULL
9107 && die->tag == DW_TAG_partial_unit)
2ab95328 9108 {
527f3840
JK
9109 dwarf2_per_objfile->line_header_hash
9110 = htab_create_alloc_ex (127, line_header_hash_voidp,
9111 line_header_eq_voidp,
9112 free_line_header_voidp,
9113 &objfile->objfile_obstack,
9114 hashtab_obstack_allocate,
9115 dummy_obstack_deallocate);
9116 }
2ab95328 9117
527f3840
JK
9118 line_header_local.offset.sect_off = line_offset;
9119 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9120 line_header_local_hash = line_header_hash (&line_header_local);
9121 if (dwarf2_per_objfile->line_header_hash != NULL)
9122 {
9123 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9124 &line_header_local,
9125 line_header_local_hash, NO_INSERT);
9126
9127 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9128 is not present in *SLOT (since if there is something in *SLOT then
9129 it will be for a partial_unit). */
9130 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9131 {
527f3840
JK
9132 gdb_assert (*slot != NULL);
9133 cu->line_header = *slot;
9134 return;
dee91e82 9135 }
2ab95328 9136 }
527f3840
JK
9137
9138 /* dwarf_decode_line_header does not yet provide sufficient information.
9139 We always have to call also dwarf_decode_lines for it. */
9140 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9141 if (cu->line_header == NULL)
9142 return;
9143
9144 if (dwarf2_per_objfile->line_header_hash == NULL)
9145 slot = NULL;
9146 else
9147 {
9148 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9149 &line_header_local,
9150 line_header_local_hash, INSERT);
9151 gdb_assert (slot != NULL);
9152 }
9153 if (slot != NULL && *slot == NULL)
9154 {
9155 /* This newly decoded line number information unit will be owned
9156 by line_header_hash hash table. */
9157 *slot = cu->line_header;
9158 }
9159 else
9160 {
9161 /* We cannot free any current entry in (*slot) as that struct line_header
9162 may be already used by multiple CUs. Create only temporary decoded
9163 line_header for this CU - it may happen at most once for each line
9164 number information unit. And if we're not using line_header_hash
9165 then this is what we want as well. */
9166 gdb_assert (die->tag != DW_TAG_partial_unit);
9167 make_cleanup (free_cu_line_header, cu);
9168 }
9169 decode_mapping = (die->tag != DW_TAG_partial_unit);
9170 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9171 decode_mapping);
2ab95328
TT
9172}
9173
95554aad 9174/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9175
c906108c 9176static void
e7c27a73 9177read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9178{
dee91e82 9179 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9180 struct gdbarch *gdbarch = get_objfile_arch (objfile);
debd256d 9181 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 9182 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9183 CORE_ADDR highpc = ((CORE_ADDR) 0);
9184 struct attribute *attr;
15d034d0
TT
9185 const char *name = NULL;
9186 const char *comp_dir = NULL;
c906108c
SS
9187 struct die_info *child_die;
9188 bfd *abfd = objfile->obfd;
e142c38c 9189 CORE_ADDR baseaddr;
6e70227d 9190
e142c38c 9191 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9192
fae299cd 9193 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9194
9195 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9196 from finish_block. */
2acceee2 9197 if (lowpc == ((CORE_ADDR) -1))
c906108c 9198 lowpc = highpc;
3e29f34a 9199 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9200
9291a0cd 9201 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 9202
95554aad 9203 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9204
f4b8a18d
KW
9205 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9206 standardised yet. As a workaround for the language detection we fall
9207 back to the DW_AT_producer string. */
9208 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9209 cu->language = language_opencl;
9210
3019eac3
DE
9211 /* Similar hack for Go. */
9212 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9213 set_cu_language (DW_LANG_Go, cu);
9214
f4dc4d17 9215 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
9216
9217 /* Decode line number information if present. We do this before
9218 processing child DIEs, so that the line header table is available
9219 for DW_AT_decl_file. */
c3b7b696 9220 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
3019eac3
DE
9221
9222 /* Process all dies in compilation unit. */
9223 if (die->child != NULL)
9224 {
9225 child_die = die->child;
9226 while (child_die && child_die->tag)
9227 {
9228 process_die (child_die, cu);
9229 child_die = sibling_die (child_die);
9230 }
9231 }
9232
9233 /* Decode macro information, if present. Dwarf 2 macro information
9234 refers to information in the line number info statement program
9235 header, so we can only read it if we've read the header
9236 successfully. */
9237 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9238 if (attr && cu->line_header)
9239 {
9240 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9241 complaint (&symfile_complaints,
9242 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
9243
43f3e411 9244 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9245 }
9246 else
9247 {
9248 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9249 if (attr && cu->line_header)
9250 {
9251 unsigned int macro_offset = DW_UNSND (attr);
9252
43f3e411 9253 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9254 }
9255 }
9256
9257 do_cleanups (back_to);
9258}
9259
f4dc4d17
DE
9260/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9261 Create the set of symtabs used by this TU, or if this TU is sharing
9262 symtabs with another TU and the symtabs have already been created
9263 then restore those symtabs in the line header.
9264 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9265
9266static void
f4dc4d17 9267setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9268{
f4dc4d17
DE
9269 struct objfile *objfile = dwarf2_per_objfile->objfile;
9270 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9271 struct type_unit_group *tu_group;
9272 int first_time;
9273 struct line_header *lh;
3019eac3 9274 struct attribute *attr;
f4dc4d17 9275 unsigned int i, line_offset;
0186c6a7 9276 struct signatured_type *sig_type;
3019eac3 9277
f4dc4d17 9278 gdb_assert (per_cu->is_debug_types);
0186c6a7 9279 sig_type = (struct signatured_type *) per_cu;
3019eac3 9280
f4dc4d17 9281 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9282
f4dc4d17 9283 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9284 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9285 if (sig_type->type_unit_group == NULL)
9286 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9287 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9288
9289 /* If we've already processed this stmt_list there's no real need to
9290 do it again, we could fake it and just recreate the part we need
9291 (file name,index -> symtab mapping). If data shows this optimization
9292 is useful we can do it then. */
43f3e411 9293 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9294
9295 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9296 debug info. */
9297 lh = NULL;
9298 if (attr != NULL)
3019eac3 9299 {
f4dc4d17
DE
9300 line_offset = DW_UNSND (attr);
9301 lh = dwarf_decode_line_header (line_offset, cu);
9302 }
9303 if (lh == NULL)
9304 {
9305 if (first_time)
9306 dwarf2_start_symtab (cu, "", NULL, 0);
9307 else
9308 {
9309 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9310 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9311 }
f4dc4d17 9312 return;
3019eac3
DE
9313 }
9314
f4dc4d17
DE
9315 cu->line_header = lh;
9316 make_cleanup (free_cu_line_header, cu);
3019eac3 9317
f4dc4d17
DE
9318 if (first_time)
9319 {
43f3e411 9320 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9321
f4dc4d17
DE
9322 tu_group->num_symtabs = lh->num_file_names;
9323 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 9324
f4dc4d17
DE
9325 for (i = 0; i < lh->num_file_names; ++i)
9326 {
d521ce57 9327 const char *dir = NULL;
f4dc4d17 9328 struct file_entry *fe = &lh->file_names[i];
3019eac3 9329
afa6c9ab 9330 if (fe->dir_index && lh->include_dirs != NULL)
f4dc4d17 9331 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 9332 dwarf2_start_subfile (fe->name, dir);
3019eac3 9333
f4dc4d17
DE
9334 if (current_subfile->symtab == NULL)
9335 {
9336 /* NOTE: start_subfile will recognize when it's been passed
9337 a file it has already seen. So we can't assume there's a
43f3e411 9338 simple mapping from lh->file_names to subfiles, plus
f4dc4d17 9339 lh->file_names may contain dups. */
43f3e411
DE
9340 current_subfile->symtab
9341 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9342 }
9343
9344 fe->symtab = current_subfile->symtab;
9345 tu_group->symtabs[i] = fe->symtab;
9346 }
9347 }
9348 else
3019eac3 9349 {
0ab9ce85 9350 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17
DE
9351
9352 for (i = 0; i < lh->num_file_names; ++i)
9353 {
9354 struct file_entry *fe = &lh->file_names[i];
9355
9356 fe->symtab = tu_group->symtabs[i];
9357 }
3019eac3
DE
9358 }
9359
f4dc4d17
DE
9360 /* The main symtab is allocated last. Type units don't have DW_AT_name
9361 so they don't have a "real" (so to speak) symtab anyway.
9362 There is later code that will assign the main symtab to all symbols
9363 that don't have one. We need to handle the case of a symbol with a
9364 missing symtab (DW_AT_decl_file) anyway. */
9365}
3019eac3 9366
f4dc4d17
DE
9367/* Process DW_TAG_type_unit.
9368 For TUs we want to skip the first top level sibling if it's not the
9369 actual type being defined by this TU. In this case the first top
9370 level sibling is there to provide context only. */
3019eac3 9371
f4dc4d17
DE
9372static void
9373read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9374{
9375 struct die_info *child_die;
3019eac3 9376
f4dc4d17
DE
9377 prepare_one_comp_unit (cu, die, language_minimal);
9378
9379 /* Initialize (or reinitialize) the machinery for building symtabs.
9380 We do this before processing child DIEs, so that the line header table
9381 is available for DW_AT_decl_file. */
9382 setup_type_unit_groups (die, cu);
9383
9384 if (die->child != NULL)
9385 {
9386 child_die = die->child;
9387 while (child_die && child_die->tag)
9388 {
9389 process_die (child_die, cu);
9390 child_die = sibling_die (child_die);
9391 }
9392 }
3019eac3
DE
9393}
9394\f
80626a55
DE
9395/* DWO/DWP files.
9396
9397 http://gcc.gnu.org/wiki/DebugFission
9398 http://gcc.gnu.org/wiki/DebugFissionDWP
9399
9400 To simplify handling of both DWO files ("object" files with the DWARF info)
9401 and DWP files (a file with the DWOs packaged up into one file), we treat
9402 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9403
9404static hashval_t
9405hash_dwo_file (const void *item)
9406{
9407 const struct dwo_file *dwo_file = item;
a2ce51a0 9408 hashval_t hash;
3019eac3 9409
a2ce51a0
DE
9410 hash = htab_hash_string (dwo_file->dwo_name);
9411 if (dwo_file->comp_dir != NULL)
9412 hash += htab_hash_string (dwo_file->comp_dir);
9413 return hash;
3019eac3
DE
9414}
9415
9416static int
9417eq_dwo_file (const void *item_lhs, const void *item_rhs)
9418{
9419 const struct dwo_file *lhs = item_lhs;
9420 const struct dwo_file *rhs = item_rhs;
9421
a2ce51a0
DE
9422 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9423 return 0;
9424 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9425 return lhs->comp_dir == rhs->comp_dir;
9426 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9427}
9428
9429/* Allocate a hash table for DWO files. */
9430
9431static htab_t
9432allocate_dwo_file_hash_table (void)
9433{
9434 struct objfile *objfile = dwarf2_per_objfile->objfile;
9435
9436 return htab_create_alloc_ex (41,
9437 hash_dwo_file,
9438 eq_dwo_file,
9439 NULL,
9440 &objfile->objfile_obstack,
9441 hashtab_obstack_allocate,
9442 dummy_obstack_deallocate);
9443}
9444
80626a55
DE
9445/* Lookup DWO file DWO_NAME. */
9446
9447static void **
0ac5b59e 9448lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9449{
9450 struct dwo_file find_entry;
9451 void **slot;
9452
9453 if (dwarf2_per_objfile->dwo_files == NULL)
9454 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9455
9456 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9457 find_entry.dwo_name = dwo_name;
9458 find_entry.comp_dir = comp_dir;
80626a55
DE
9459 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9460
9461 return slot;
9462}
9463
3019eac3
DE
9464static hashval_t
9465hash_dwo_unit (const void *item)
9466{
9467 const struct dwo_unit *dwo_unit = item;
9468
9469 /* This drops the top 32 bits of the id, but is ok for a hash. */
9470 return dwo_unit->signature;
9471}
9472
9473static int
9474eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9475{
9476 const struct dwo_unit *lhs = item_lhs;
9477 const struct dwo_unit *rhs = item_rhs;
9478
9479 /* The signature is assumed to be unique within the DWO file.
9480 So while object file CU dwo_id's always have the value zero,
9481 that's OK, assuming each object file DWO file has only one CU,
9482 and that's the rule for now. */
9483 return lhs->signature == rhs->signature;
9484}
9485
9486/* Allocate a hash table for DWO CUs,TUs.
9487 There is one of these tables for each of CUs,TUs for each DWO file. */
9488
9489static htab_t
9490allocate_dwo_unit_table (struct objfile *objfile)
9491{
9492 /* Start out with a pretty small number.
9493 Generally DWO files contain only one CU and maybe some TUs. */
9494 return htab_create_alloc_ex (3,
9495 hash_dwo_unit,
9496 eq_dwo_unit,
9497 NULL,
9498 &objfile->objfile_obstack,
9499 hashtab_obstack_allocate,
9500 dummy_obstack_deallocate);
9501}
9502
80626a55 9503/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9504
19c3d4c9 9505struct create_dwo_cu_data
3019eac3
DE
9506{
9507 struct dwo_file *dwo_file;
19c3d4c9 9508 struct dwo_unit dwo_unit;
3019eac3
DE
9509};
9510
19c3d4c9 9511/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9512
9513static void
19c3d4c9
DE
9514create_dwo_cu_reader (const struct die_reader_specs *reader,
9515 const gdb_byte *info_ptr,
9516 struct die_info *comp_unit_die,
9517 int has_children,
9518 void *datap)
3019eac3
DE
9519{
9520 struct dwarf2_cu *cu = reader->cu;
9521 struct objfile *objfile = dwarf2_per_objfile->objfile;
9522 sect_offset offset = cu->per_cu->offset;
8a0459fd 9523 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 9524 struct create_dwo_cu_data *data = datap;
3019eac3 9525 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9526 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9527 struct attribute *attr;
3019eac3
DE
9528
9529 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9530 if (attr == NULL)
9531 {
19c3d4c9
DE
9532 complaint (&symfile_complaints,
9533 _("Dwarf Error: debug entry at offset 0x%x is missing"
9534 " its dwo_id [in module %s]"),
9535 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
9536 return;
9537 }
9538
3019eac3
DE
9539 dwo_unit->dwo_file = dwo_file;
9540 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9541 dwo_unit->section = section;
3019eac3
DE
9542 dwo_unit->offset = offset;
9543 dwo_unit->length = cu->per_cu->length;
9544
b4f54984 9545 if (dwarf_read_debug)
4031ecc5
DE
9546 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9547 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
9548}
9549
19c3d4c9
DE
9550/* Create the dwo_unit for the lone CU in DWO_FILE.
9551 Note: This function processes DWO files only, not DWP files. */
3019eac3 9552
19c3d4c9
DE
9553static struct dwo_unit *
9554create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9555{
9556 struct objfile *objfile = dwarf2_per_objfile->objfile;
9557 struct dwarf2_section_info *section = &dwo_file->sections.info;
9558 bfd *abfd;
9559 htab_t cu_htab;
d521ce57 9560 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9561 struct create_dwo_cu_data create_dwo_cu_data;
9562 struct dwo_unit *dwo_unit;
3019eac3
DE
9563
9564 dwarf2_read_section (objfile, section);
9565 info_ptr = section->buffer;
9566
9567 if (info_ptr == NULL)
9568 return NULL;
9569
9570 /* We can't set abfd until now because the section may be empty or
9571 not present, in which case section->asection will be NULL. */
a32a8923 9572 abfd = get_section_bfd_owner (section);
3019eac3 9573
b4f54984 9574 if (dwarf_read_debug)
19c3d4c9
DE
9575 {
9576 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9577 get_section_name (section),
9578 get_section_file_name (section));
19c3d4c9 9579 }
3019eac3 9580
19c3d4c9
DE
9581 create_dwo_cu_data.dwo_file = dwo_file;
9582 dwo_unit = NULL;
3019eac3
DE
9583
9584 end_ptr = info_ptr + section->size;
9585 while (info_ptr < end_ptr)
9586 {
9587 struct dwarf2_per_cu_data per_cu;
9588
19c3d4c9
DE
9589 memset (&create_dwo_cu_data.dwo_unit, 0,
9590 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9591 memset (&per_cu, 0, sizeof (per_cu));
9592 per_cu.objfile = objfile;
9593 per_cu.is_debug_types = 0;
9594 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 9595 per_cu.section = section;
3019eac3 9596
33e80786 9597 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
19c3d4c9
DE
9598 create_dwo_cu_reader,
9599 &create_dwo_cu_data);
9600
9601 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9602 {
9603 /* If we've already found one, complain. We only support one
9604 because having more than one requires hacking the dwo_name of
9605 each to match, which is highly unlikely to happen. */
9606 if (dwo_unit != NULL)
9607 {
9608 complaint (&symfile_complaints,
9609 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 9610 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
9611 break;
9612 }
9613
9614 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9615 *dwo_unit = create_dwo_cu_data.dwo_unit;
9616 }
3019eac3
DE
9617
9618 info_ptr += per_cu.length;
9619 }
9620
19c3d4c9 9621 return dwo_unit;
3019eac3
DE
9622}
9623
80626a55
DE
9624/* DWP file .debug_{cu,tu}_index section format:
9625 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9626
d2415c6c
DE
9627 DWP Version 1:
9628
80626a55
DE
9629 Both index sections have the same format, and serve to map a 64-bit
9630 signature to a set of section numbers. Each section begins with a header,
9631 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9632 indexes, and a pool of 32-bit section numbers. The index sections will be
9633 aligned at 8-byte boundaries in the file.
9634
d2415c6c
DE
9635 The index section header consists of:
9636
9637 V, 32 bit version number
9638 -, 32 bits unused
9639 N, 32 bit number of compilation units or type units in the index
9640 M, 32 bit number of slots in the hash table
80626a55 9641
d2415c6c 9642 Numbers are recorded using the byte order of the application binary.
80626a55 9643
d2415c6c
DE
9644 The hash table begins at offset 16 in the section, and consists of an array
9645 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9646 order of the application binary). Unused slots in the hash table are 0.
9647 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9648
d2415c6c
DE
9649 The parallel table begins immediately after the hash table
9650 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9651 array of 32-bit indexes (using the byte order of the application binary),
9652 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9653 table contains a 32-bit index into the pool of section numbers. For unused
9654 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9655
73869dc2
DE
9656 The pool of section numbers begins immediately following the hash table
9657 (at offset 16 + 12 * M from the beginning of the section). The pool of
9658 section numbers consists of an array of 32-bit words (using the byte order
9659 of the application binary). Each item in the array is indexed starting
9660 from 0. The hash table entry provides the index of the first section
9661 number in the set. Additional section numbers in the set follow, and the
9662 set is terminated by a 0 entry (section number 0 is not used in ELF).
9663
9664 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9665 section must be the first entry in the set, and the .debug_abbrev.dwo must
9666 be the second entry. Other members of the set may follow in any order.
9667
9668 ---
9669
9670 DWP Version 2:
9671
9672 DWP Version 2 combines all the .debug_info, etc. sections into one,
9673 and the entries in the index tables are now offsets into these sections.
9674 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9675 section.
9676
9677 Index Section Contents:
9678 Header
9679 Hash Table of Signatures dwp_hash_table.hash_table
9680 Parallel Table of Indices dwp_hash_table.unit_table
9681 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9682 Table of Section Sizes dwp_hash_table.v2.sizes
9683
9684 The index section header consists of:
9685
9686 V, 32 bit version number
9687 L, 32 bit number of columns in the table of section offsets
9688 N, 32 bit number of compilation units or type units in the index
9689 M, 32 bit number of slots in the hash table
9690
9691 Numbers are recorded using the byte order of the application binary.
9692
9693 The hash table has the same format as version 1.
9694 The parallel table of indices has the same format as version 1,
9695 except that the entries are origin-1 indices into the table of sections
9696 offsets and the table of section sizes.
9697
9698 The table of offsets begins immediately following the parallel table
9699 (at offset 16 + 12 * M from the beginning of the section). The table is
9700 a two-dimensional array of 32-bit words (using the byte order of the
9701 application binary), with L columns and N+1 rows, in row-major order.
9702 Each row in the array is indexed starting from 0. The first row provides
9703 a key to the remaining rows: each column in this row provides an identifier
9704 for a debug section, and the offsets in the same column of subsequent rows
9705 refer to that section. The section identifiers are:
9706
9707 DW_SECT_INFO 1 .debug_info.dwo
9708 DW_SECT_TYPES 2 .debug_types.dwo
9709 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9710 DW_SECT_LINE 4 .debug_line.dwo
9711 DW_SECT_LOC 5 .debug_loc.dwo
9712 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9713 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9714 DW_SECT_MACRO 8 .debug_macro.dwo
9715
9716 The offsets provided by the CU and TU index sections are the base offsets
9717 for the contributions made by each CU or TU to the corresponding section
9718 in the package file. Each CU and TU header contains an abbrev_offset
9719 field, used to find the abbreviations table for that CU or TU within the
9720 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9721 be interpreted as relative to the base offset given in the index section.
9722 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9723 should be interpreted as relative to the base offset for .debug_line.dwo,
9724 and offsets into other debug sections obtained from DWARF attributes should
9725 also be interpreted as relative to the corresponding base offset.
9726
9727 The table of sizes begins immediately following the table of offsets.
9728 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9729 with L columns and N rows, in row-major order. Each row in the array is
9730 indexed starting from 1 (row 0 is shared by the two tables).
9731
9732 ---
9733
9734 Hash table lookup is handled the same in version 1 and 2:
9735
9736 We assume that N and M will not exceed 2^32 - 1.
9737 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9738
d2415c6c
DE
9739 Given a 64-bit compilation unit signature or a type signature S, an entry
9740 in the hash table is located as follows:
80626a55 9741
d2415c6c
DE
9742 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9743 the low-order k bits all set to 1.
80626a55 9744
d2415c6c 9745 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9746
d2415c6c
DE
9747 3) If the hash table entry at index H matches the signature, use that
9748 entry. If the hash table entry at index H is unused (all zeroes),
9749 terminate the search: the signature is not present in the table.
80626a55 9750
d2415c6c 9751 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9752
d2415c6c 9753 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9754 to stop at an unused slot or find the match. */
80626a55
DE
9755
9756/* Create a hash table to map DWO IDs to their CU/TU entry in
9757 .debug_{info,types}.dwo in DWP_FILE.
9758 Returns NULL if there isn't one.
9759 Note: This function processes DWP files only, not DWO files. */
9760
9761static struct dwp_hash_table *
9762create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9763{
9764 struct objfile *objfile = dwarf2_per_objfile->objfile;
9765 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9766 const gdb_byte *index_ptr, *index_end;
80626a55 9767 struct dwarf2_section_info *index;
73869dc2 9768 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
9769 struct dwp_hash_table *htab;
9770
9771 if (is_debug_types)
9772 index = &dwp_file->sections.tu_index;
9773 else
9774 index = &dwp_file->sections.cu_index;
9775
9776 if (dwarf2_section_empty_p (index))
9777 return NULL;
9778 dwarf2_read_section (objfile, index);
9779
9780 index_ptr = index->buffer;
9781 index_end = index_ptr + index->size;
9782
9783 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
9784 index_ptr += 4;
9785 if (version == 2)
9786 nr_columns = read_4_bytes (dbfd, index_ptr);
9787 else
9788 nr_columns = 0;
9789 index_ptr += 4;
80626a55
DE
9790 nr_units = read_4_bytes (dbfd, index_ptr);
9791 index_ptr += 4;
9792 nr_slots = read_4_bytes (dbfd, index_ptr);
9793 index_ptr += 4;
9794
73869dc2 9795 if (version != 1 && version != 2)
80626a55 9796 {
21aa081e 9797 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9798 " [in module %s]"),
21aa081e 9799 pulongest (version), dwp_file->name);
80626a55
DE
9800 }
9801 if (nr_slots != (nr_slots & -nr_slots))
9802 {
21aa081e 9803 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9804 " is not power of 2 [in module %s]"),
21aa081e 9805 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9806 }
9807
9808 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9809 htab->version = version;
9810 htab->nr_columns = nr_columns;
80626a55
DE
9811 htab->nr_units = nr_units;
9812 htab->nr_slots = nr_slots;
9813 htab->hash_table = index_ptr;
9814 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
9815
9816 /* Exit early if the table is empty. */
9817 if (nr_slots == 0 || nr_units == 0
9818 || (version == 2 && nr_columns == 0))
9819 {
9820 /* All must be zero. */
9821 if (nr_slots != 0 || nr_units != 0
9822 || (version == 2 && nr_columns != 0))
9823 {
9824 complaint (&symfile_complaints,
9825 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9826 " all zero [in modules %s]"),
9827 dwp_file->name);
9828 }
9829 return htab;
9830 }
9831
9832 if (version == 1)
9833 {
9834 htab->section_pool.v1.indices =
9835 htab->unit_table + sizeof (uint32_t) * nr_slots;
9836 /* It's harder to decide whether the section is too small in v1.
9837 V1 is deprecated anyway so we punt. */
9838 }
9839 else
9840 {
9841 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9842 int *ids = htab->section_pool.v2.section_ids;
9843 /* Reverse map for error checking. */
9844 int ids_seen[DW_SECT_MAX + 1];
9845 int i;
9846
9847 if (nr_columns < 2)
9848 {
9849 error (_("Dwarf Error: bad DWP hash table, too few columns"
9850 " in section table [in module %s]"),
9851 dwp_file->name);
9852 }
9853 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9854 {
9855 error (_("Dwarf Error: bad DWP hash table, too many columns"
9856 " in section table [in module %s]"),
9857 dwp_file->name);
9858 }
9859 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9860 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9861 for (i = 0; i < nr_columns; ++i)
9862 {
9863 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9864
9865 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9866 {
9867 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9868 " in section table [in module %s]"),
9869 id, dwp_file->name);
9870 }
9871 if (ids_seen[id] != -1)
9872 {
9873 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9874 " id %d in section table [in module %s]"),
9875 id, dwp_file->name);
9876 }
9877 ids_seen[id] = i;
9878 ids[i] = id;
9879 }
9880 /* Must have exactly one info or types section. */
9881 if (((ids_seen[DW_SECT_INFO] != -1)
9882 + (ids_seen[DW_SECT_TYPES] != -1))
9883 != 1)
9884 {
9885 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9886 " DWO info/types section [in module %s]"),
9887 dwp_file->name);
9888 }
9889 /* Must have an abbrev section. */
9890 if (ids_seen[DW_SECT_ABBREV] == -1)
9891 {
9892 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9893 " section [in module %s]"),
9894 dwp_file->name);
9895 }
9896 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9897 htab->section_pool.v2.sizes =
9898 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9899 * nr_units * nr_columns);
9900 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9901 * nr_units * nr_columns))
9902 > index_end)
9903 {
9904 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9905 " [in module %s]"),
9906 dwp_file->name);
9907 }
9908 }
80626a55
DE
9909
9910 return htab;
9911}
9912
9913/* Update SECTIONS with the data from SECTP.
9914
9915 This function is like the other "locate" section routines that are
9916 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 9917 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
9918
9919 The result is non-zero for success, or zero if an error was found. */
9920
9921static int
73869dc2
DE
9922locate_v1_virtual_dwo_sections (asection *sectp,
9923 struct virtual_v1_dwo_sections *sections)
80626a55
DE
9924{
9925 const struct dwop_section_names *names = &dwop_section_names;
9926
9927 if (section_is_p (sectp->name, &names->abbrev_dwo))
9928 {
9929 /* There can be only one. */
73869dc2 9930 if (sections->abbrev.s.asection != NULL)
80626a55 9931 return 0;
73869dc2 9932 sections->abbrev.s.asection = sectp;
80626a55
DE
9933 sections->abbrev.size = bfd_get_section_size (sectp);
9934 }
9935 else if (section_is_p (sectp->name, &names->info_dwo)
9936 || section_is_p (sectp->name, &names->types_dwo))
9937 {
9938 /* There can be only one. */
73869dc2 9939 if (sections->info_or_types.s.asection != NULL)
80626a55 9940 return 0;
73869dc2 9941 sections->info_or_types.s.asection = sectp;
80626a55
DE
9942 sections->info_or_types.size = bfd_get_section_size (sectp);
9943 }
9944 else if (section_is_p (sectp->name, &names->line_dwo))
9945 {
9946 /* There can be only one. */
73869dc2 9947 if (sections->line.s.asection != NULL)
80626a55 9948 return 0;
73869dc2 9949 sections->line.s.asection = sectp;
80626a55
DE
9950 sections->line.size = bfd_get_section_size (sectp);
9951 }
9952 else if (section_is_p (sectp->name, &names->loc_dwo))
9953 {
9954 /* There can be only one. */
73869dc2 9955 if (sections->loc.s.asection != NULL)
80626a55 9956 return 0;
73869dc2 9957 sections->loc.s.asection = sectp;
80626a55
DE
9958 sections->loc.size = bfd_get_section_size (sectp);
9959 }
9960 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9961 {
9962 /* There can be only one. */
73869dc2 9963 if (sections->macinfo.s.asection != NULL)
80626a55 9964 return 0;
73869dc2 9965 sections->macinfo.s.asection = sectp;
80626a55
DE
9966 sections->macinfo.size = bfd_get_section_size (sectp);
9967 }
9968 else if (section_is_p (sectp->name, &names->macro_dwo))
9969 {
9970 /* There can be only one. */
73869dc2 9971 if (sections->macro.s.asection != NULL)
80626a55 9972 return 0;
73869dc2 9973 sections->macro.s.asection = sectp;
80626a55
DE
9974 sections->macro.size = bfd_get_section_size (sectp);
9975 }
9976 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9977 {
9978 /* There can be only one. */
73869dc2 9979 if (sections->str_offsets.s.asection != NULL)
80626a55 9980 return 0;
73869dc2 9981 sections->str_offsets.s.asection = sectp;
80626a55
DE
9982 sections->str_offsets.size = bfd_get_section_size (sectp);
9983 }
9984 else
9985 {
9986 /* No other kind of section is valid. */
9987 return 0;
9988 }
9989
9990 return 1;
9991}
9992
73869dc2
DE
9993/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
9994 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
9995 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
9996 This is for DWP version 1 files. */
80626a55
DE
9997
9998static struct dwo_unit *
73869dc2
DE
9999create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10000 uint32_t unit_index,
10001 const char *comp_dir,
10002 ULONGEST signature, int is_debug_types)
80626a55
DE
10003{
10004 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10005 const struct dwp_hash_table *dwp_htab =
10006 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10007 bfd *dbfd = dwp_file->dbfd;
10008 const char *kind = is_debug_types ? "TU" : "CU";
10009 struct dwo_file *dwo_file;
10010 struct dwo_unit *dwo_unit;
73869dc2 10011 struct virtual_v1_dwo_sections sections;
80626a55
DE
10012 void **dwo_file_slot;
10013 char *virtual_dwo_name;
10014 struct dwarf2_section_info *cutu;
10015 struct cleanup *cleanups;
10016 int i;
10017
73869dc2
DE
10018 gdb_assert (dwp_file->version == 1);
10019
b4f54984 10020 if (dwarf_read_debug)
80626a55 10021 {
73869dc2 10022 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10023 kind,
73869dc2 10024 pulongest (unit_index), hex_string (signature),
80626a55
DE
10025 dwp_file->name);
10026 }
10027
19ac8c2e 10028 /* Fetch the sections of this DWO unit.
80626a55
DE
10029 Put a limit on the number of sections we look for so that bad data
10030 doesn't cause us to loop forever. */
10031
73869dc2 10032#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10033 (1 /* .debug_info or .debug_types */ \
10034 + 1 /* .debug_abbrev */ \
10035 + 1 /* .debug_line */ \
10036 + 1 /* .debug_loc */ \
10037 + 1 /* .debug_str_offsets */ \
19ac8c2e 10038 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10039 + 1 /* trailing zero */)
10040
10041 memset (&sections, 0, sizeof (sections));
10042 cleanups = make_cleanup (null_cleanup, 0);
10043
73869dc2 10044 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10045 {
10046 asection *sectp;
10047 uint32_t section_nr =
10048 read_4_bytes (dbfd,
73869dc2
DE
10049 dwp_htab->section_pool.v1.indices
10050 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10051
10052 if (section_nr == 0)
10053 break;
10054 if (section_nr >= dwp_file->num_sections)
10055 {
10056 error (_("Dwarf Error: bad DWP hash table, section number too large"
10057 " [in module %s]"),
10058 dwp_file->name);
10059 }
10060
10061 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10062 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10063 {
10064 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10065 " [in module %s]"),
10066 dwp_file->name);
10067 }
10068 }
10069
10070 if (i < 2
a32a8923
DE
10071 || dwarf2_section_empty_p (&sections.info_or_types)
10072 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10073 {
10074 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10075 " [in module %s]"),
10076 dwp_file->name);
10077 }
73869dc2 10078 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10079 {
10080 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10081 " [in module %s]"),
10082 dwp_file->name);
10083 }
10084
10085 /* It's easier for the rest of the code if we fake a struct dwo_file and
10086 have dwo_unit "live" in that. At least for now.
10087
10088 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10089 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10090 file, we can combine them back into a virtual DWO file to save space
10091 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10092 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10093
2792b94d
PM
10094 virtual_dwo_name =
10095 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10096 get_section_id (&sections.abbrev),
10097 get_section_id (&sections.line),
10098 get_section_id (&sections.loc),
10099 get_section_id (&sections.str_offsets));
80626a55
DE
10100 make_cleanup (xfree, virtual_dwo_name);
10101 /* Can we use an existing virtual DWO file? */
0ac5b59e 10102 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10103 /* Create one if necessary. */
10104 if (*dwo_file_slot == NULL)
10105 {
b4f54984 10106 if (dwarf_read_debug)
80626a55
DE
10107 {
10108 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10109 virtual_dwo_name);
10110 }
10111 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10112 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
10113 virtual_dwo_name,
10114 strlen (virtual_dwo_name));
10115 dwo_file->comp_dir = comp_dir;
80626a55
DE
10116 dwo_file->sections.abbrev = sections.abbrev;
10117 dwo_file->sections.line = sections.line;
10118 dwo_file->sections.loc = sections.loc;
10119 dwo_file->sections.macinfo = sections.macinfo;
10120 dwo_file->sections.macro = sections.macro;
10121 dwo_file->sections.str_offsets = sections.str_offsets;
10122 /* The "str" section is global to the entire DWP file. */
10123 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10124 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10125 there's no need to record it in dwo_file.
10126 Also, we can't simply record type sections in dwo_file because
10127 we record a pointer into the vector in dwo_unit. As we collect more
10128 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10129 for it, invalidating all copies of pointers into the previous
10130 contents. */
80626a55
DE
10131 *dwo_file_slot = dwo_file;
10132 }
10133 else
10134 {
b4f54984 10135 if (dwarf_read_debug)
80626a55
DE
10136 {
10137 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10138 virtual_dwo_name);
10139 }
10140 dwo_file = *dwo_file_slot;
10141 }
10142 do_cleanups (cleanups);
10143
10144 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10145 dwo_unit->dwo_file = dwo_file;
10146 dwo_unit->signature = signature;
8a0459fd
DE
10147 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
10148 sizeof (struct dwarf2_section_info));
10149 *dwo_unit->section = sections.info_or_types;
57d63ce2 10150 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10151
10152 return dwo_unit;
10153}
10154
73869dc2
DE
10155/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10156 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10157 piece within that section used by a TU/CU, return a virtual section
10158 of just that piece. */
10159
10160static struct dwarf2_section_info
10161create_dwp_v2_section (struct dwarf2_section_info *section,
10162 bfd_size_type offset, bfd_size_type size)
10163{
10164 struct dwarf2_section_info result;
10165 asection *sectp;
10166
10167 gdb_assert (section != NULL);
10168 gdb_assert (!section->is_virtual);
10169
10170 memset (&result, 0, sizeof (result));
10171 result.s.containing_section = section;
10172 result.is_virtual = 1;
10173
10174 if (size == 0)
10175 return result;
10176
10177 sectp = get_section_bfd_section (section);
10178
10179 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10180 bounds of the real section. This is a pretty-rare event, so just
10181 flag an error (easier) instead of a warning and trying to cope. */
10182 if (sectp == NULL
10183 || offset + size > bfd_get_section_size (sectp))
10184 {
10185 bfd *abfd = sectp->owner;
10186
10187 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10188 " in section %s [in module %s]"),
10189 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10190 objfile_name (dwarf2_per_objfile->objfile));
10191 }
10192
10193 result.virtual_offset = offset;
10194 result.size = size;
10195 return result;
10196}
10197
10198/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10199 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10200 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10201 This is for DWP version 2 files. */
10202
10203static struct dwo_unit *
10204create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10205 uint32_t unit_index,
10206 const char *comp_dir,
10207 ULONGEST signature, int is_debug_types)
10208{
10209 struct objfile *objfile = dwarf2_per_objfile->objfile;
10210 const struct dwp_hash_table *dwp_htab =
10211 is_debug_types ? dwp_file->tus : dwp_file->cus;
10212 bfd *dbfd = dwp_file->dbfd;
10213 const char *kind = is_debug_types ? "TU" : "CU";
10214 struct dwo_file *dwo_file;
10215 struct dwo_unit *dwo_unit;
10216 struct virtual_v2_dwo_sections sections;
10217 void **dwo_file_slot;
10218 char *virtual_dwo_name;
10219 struct dwarf2_section_info *cutu;
10220 struct cleanup *cleanups;
10221 int i;
10222
10223 gdb_assert (dwp_file->version == 2);
10224
b4f54984 10225 if (dwarf_read_debug)
73869dc2
DE
10226 {
10227 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10228 kind,
10229 pulongest (unit_index), hex_string (signature),
10230 dwp_file->name);
10231 }
10232
10233 /* Fetch the section offsets of this DWO unit. */
10234
10235 memset (&sections, 0, sizeof (sections));
10236 cleanups = make_cleanup (null_cleanup, 0);
10237
10238 for (i = 0; i < dwp_htab->nr_columns; ++i)
10239 {
10240 uint32_t offset = read_4_bytes (dbfd,
10241 dwp_htab->section_pool.v2.offsets
10242 + (((unit_index - 1) * dwp_htab->nr_columns
10243 + i)
10244 * sizeof (uint32_t)));
10245 uint32_t size = read_4_bytes (dbfd,
10246 dwp_htab->section_pool.v2.sizes
10247 + (((unit_index - 1) * dwp_htab->nr_columns
10248 + i)
10249 * sizeof (uint32_t)));
10250
10251 switch (dwp_htab->section_pool.v2.section_ids[i])
10252 {
10253 case DW_SECT_INFO:
10254 case DW_SECT_TYPES:
10255 sections.info_or_types_offset = offset;
10256 sections.info_or_types_size = size;
10257 break;
10258 case DW_SECT_ABBREV:
10259 sections.abbrev_offset = offset;
10260 sections.abbrev_size = size;
10261 break;
10262 case DW_SECT_LINE:
10263 sections.line_offset = offset;
10264 sections.line_size = size;
10265 break;
10266 case DW_SECT_LOC:
10267 sections.loc_offset = offset;
10268 sections.loc_size = size;
10269 break;
10270 case DW_SECT_STR_OFFSETS:
10271 sections.str_offsets_offset = offset;
10272 sections.str_offsets_size = size;
10273 break;
10274 case DW_SECT_MACINFO:
10275 sections.macinfo_offset = offset;
10276 sections.macinfo_size = size;
10277 break;
10278 case DW_SECT_MACRO:
10279 sections.macro_offset = offset;
10280 sections.macro_size = size;
10281 break;
10282 }
10283 }
10284
10285 /* It's easier for the rest of the code if we fake a struct dwo_file and
10286 have dwo_unit "live" in that. At least for now.
10287
10288 The DWP file can be made up of a random collection of CUs and TUs.
10289 However, for each CU + set of TUs that came from the same original DWO
10290 file, we can combine them back into a virtual DWO file to save space
10291 (fewer struct dwo_file objects to allocate). Remember that for really
10292 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10293
10294 virtual_dwo_name =
10295 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10296 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10297 (long) (sections.line_size ? sections.line_offset : 0),
10298 (long) (sections.loc_size ? sections.loc_offset : 0),
10299 (long) (sections.str_offsets_size
10300 ? sections.str_offsets_offset : 0));
10301 make_cleanup (xfree, virtual_dwo_name);
10302 /* Can we use an existing virtual DWO file? */
10303 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10304 /* Create one if necessary. */
10305 if (*dwo_file_slot == NULL)
10306 {
b4f54984 10307 if (dwarf_read_debug)
73869dc2
DE
10308 {
10309 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10310 virtual_dwo_name);
10311 }
10312 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10313 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
10314 virtual_dwo_name,
10315 strlen (virtual_dwo_name));
10316 dwo_file->comp_dir = comp_dir;
10317 dwo_file->sections.abbrev =
10318 create_dwp_v2_section (&dwp_file->sections.abbrev,
10319 sections.abbrev_offset, sections.abbrev_size);
10320 dwo_file->sections.line =
10321 create_dwp_v2_section (&dwp_file->sections.line,
10322 sections.line_offset, sections.line_size);
10323 dwo_file->sections.loc =
10324 create_dwp_v2_section (&dwp_file->sections.loc,
10325 sections.loc_offset, sections.loc_size);
10326 dwo_file->sections.macinfo =
10327 create_dwp_v2_section (&dwp_file->sections.macinfo,
10328 sections.macinfo_offset, sections.macinfo_size);
10329 dwo_file->sections.macro =
10330 create_dwp_v2_section (&dwp_file->sections.macro,
10331 sections.macro_offset, sections.macro_size);
10332 dwo_file->sections.str_offsets =
10333 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10334 sections.str_offsets_offset,
10335 sections.str_offsets_size);
10336 /* The "str" section is global to the entire DWP file. */
10337 dwo_file->sections.str = dwp_file->sections.str;
10338 /* The info or types section is assigned below to dwo_unit,
10339 there's no need to record it in dwo_file.
10340 Also, we can't simply record type sections in dwo_file because
10341 we record a pointer into the vector in dwo_unit. As we collect more
10342 types we'll grow the vector and eventually have to reallocate space
10343 for it, invalidating all copies of pointers into the previous
10344 contents. */
10345 *dwo_file_slot = dwo_file;
10346 }
10347 else
10348 {
b4f54984 10349 if (dwarf_read_debug)
73869dc2
DE
10350 {
10351 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10352 virtual_dwo_name);
10353 }
10354 dwo_file = *dwo_file_slot;
10355 }
10356 do_cleanups (cleanups);
10357
10358 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10359 dwo_unit->dwo_file = dwo_file;
10360 dwo_unit->signature = signature;
10361 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
10362 sizeof (struct dwarf2_section_info));
10363 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10364 ? &dwp_file->sections.types
10365 : &dwp_file->sections.info,
10366 sections.info_or_types_offset,
10367 sections.info_or_types_size);
10368 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10369
10370 return dwo_unit;
10371}
10372
57d63ce2
DE
10373/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10374 Returns NULL if the signature isn't found. */
80626a55
DE
10375
10376static struct dwo_unit *
57d63ce2
DE
10377lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10378 ULONGEST signature, int is_debug_types)
80626a55 10379{
57d63ce2
DE
10380 const struct dwp_hash_table *dwp_htab =
10381 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10382 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10383 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10384 uint32_t hash = signature & mask;
10385 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10386 unsigned int i;
10387 void **slot;
10388 struct dwo_unit find_dwo_cu, *dwo_cu;
10389
10390 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10391 find_dwo_cu.signature = signature;
19ac8c2e
DE
10392 slot = htab_find_slot (is_debug_types
10393 ? dwp_file->loaded_tus
10394 : dwp_file->loaded_cus,
10395 &find_dwo_cu, INSERT);
80626a55
DE
10396
10397 if (*slot != NULL)
10398 return *slot;
10399
10400 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10401 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10402 {
10403 ULONGEST signature_in_table;
10404
10405 signature_in_table =
57d63ce2 10406 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10407 if (signature_in_table == signature)
10408 {
57d63ce2
DE
10409 uint32_t unit_index =
10410 read_4_bytes (dbfd,
10411 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10412
73869dc2
DE
10413 if (dwp_file->version == 1)
10414 {
10415 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10416 comp_dir, signature,
10417 is_debug_types);
10418 }
10419 else
10420 {
10421 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10422 comp_dir, signature,
10423 is_debug_types);
10424 }
80626a55
DE
10425 return *slot;
10426 }
10427 if (signature_in_table == 0)
10428 return NULL;
10429 hash = (hash + hash2) & mask;
10430 }
10431
10432 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10433 " [in module %s]"),
10434 dwp_file->name);
10435}
10436
ab5088bf 10437/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10438 Open the file specified by FILE_NAME and hand it off to BFD for
10439 preliminary analysis. Return a newly initialized bfd *, which
10440 includes a canonicalized copy of FILE_NAME.
80626a55 10441 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10442 SEARCH_CWD is true if the current directory is to be searched.
10443 It will be searched before debug-file-directory.
13aaf454
DE
10444 If successful, the file is added to the bfd include table of the
10445 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10446 If unable to find/open the file, return NULL.
3019eac3
DE
10447 NOTE: This function is derived from symfile_bfd_open. */
10448
10449static bfd *
6ac97d4c 10450try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
10451{
10452 bfd *sym_bfd;
80626a55 10453 int desc, flags;
3019eac3 10454 char *absolute_name;
9c02c129
DE
10455 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10456 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10457 to debug_file_directory. */
10458 char *search_path;
10459 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10460
6ac97d4c
DE
10461 if (search_cwd)
10462 {
10463 if (*debug_file_directory != '\0')
10464 search_path = concat (".", dirname_separator_string,
10465 debug_file_directory, NULL);
10466 else
10467 search_path = xstrdup (".");
10468 }
9c02c129 10469 else
6ac97d4c 10470 search_path = xstrdup (debug_file_directory);
3019eac3 10471
492c0ab7 10472 flags = OPF_RETURN_REALPATH;
80626a55
DE
10473 if (is_dwp)
10474 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10475 desc = openp (search_path, flags, file_name,
3019eac3 10476 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10477 xfree (search_path);
3019eac3
DE
10478 if (desc < 0)
10479 return NULL;
10480
bb397797 10481 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 10482 xfree (absolute_name);
9c02c129
DE
10483 if (sym_bfd == NULL)
10484 return NULL;
3019eac3
DE
10485 bfd_set_cacheable (sym_bfd, 1);
10486
10487 if (!bfd_check_format (sym_bfd, bfd_object))
10488 {
cbb099e8 10489 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
10490 return NULL;
10491 }
10492
13aaf454
DE
10493 /* Success. Record the bfd as having been included by the objfile's bfd.
10494 This is important because things like demangled_names_hash lives in the
10495 objfile's per_bfd space and may have references to things like symbol
10496 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10497 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd);
10498
3019eac3
DE
10499 return sym_bfd;
10500}
10501
ab5088bf 10502/* Try to open DWO file FILE_NAME.
3019eac3
DE
10503 COMP_DIR is the DW_AT_comp_dir attribute.
10504 The result is the bfd handle of the file.
10505 If there is a problem finding or opening the file, return NULL.
10506 Upon success, the canonicalized path of the file is stored in the bfd,
10507 same as symfile_bfd_open. */
10508
10509static bfd *
ab5088bf 10510open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
10511{
10512 bfd *abfd;
3019eac3 10513
80626a55 10514 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10515 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10516
10517 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10518
10519 if (comp_dir != NULL)
10520 {
80626a55 10521 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
10522
10523 /* NOTE: If comp_dir is a relative path, this will also try the
10524 search path, which seems useful. */
6ac97d4c 10525 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10526 xfree (path_to_try);
10527 if (abfd != NULL)
10528 return abfd;
10529 }
10530
10531 /* That didn't work, try debug-file-directory, which, despite its name,
10532 is a list of paths. */
10533
10534 if (*debug_file_directory == '\0')
10535 return NULL;
10536
6ac97d4c 10537 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10538}
10539
80626a55
DE
10540/* This function is mapped across the sections and remembers the offset and
10541 size of each of the DWO debugging sections we are interested in. */
10542
10543static void
10544dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10545{
10546 struct dwo_sections *dwo_sections = dwo_sections_ptr;
10547 const struct dwop_section_names *names = &dwop_section_names;
10548
10549 if (section_is_p (sectp->name, &names->abbrev_dwo))
10550 {
73869dc2 10551 dwo_sections->abbrev.s.asection = sectp;
80626a55
DE
10552 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10553 }
10554 else if (section_is_p (sectp->name, &names->info_dwo))
10555 {
73869dc2 10556 dwo_sections->info.s.asection = sectp;
80626a55
DE
10557 dwo_sections->info.size = bfd_get_section_size (sectp);
10558 }
10559 else if (section_is_p (sectp->name, &names->line_dwo))
10560 {
73869dc2 10561 dwo_sections->line.s.asection = sectp;
80626a55
DE
10562 dwo_sections->line.size = bfd_get_section_size (sectp);
10563 }
10564 else if (section_is_p (sectp->name, &names->loc_dwo))
10565 {
73869dc2 10566 dwo_sections->loc.s.asection = sectp;
80626a55
DE
10567 dwo_sections->loc.size = bfd_get_section_size (sectp);
10568 }
10569 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10570 {
73869dc2 10571 dwo_sections->macinfo.s.asection = sectp;
80626a55
DE
10572 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10573 }
10574 else if (section_is_p (sectp->name, &names->macro_dwo))
10575 {
73869dc2 10576 dwo_sections->macro.s.asection = sectp;
80626a55
DE
10577 dwo_sections->macro.size = bfd_get_section_size (sectp);
10578 }
10579 else if (section_is_p (sectp->name, &names->str_dwo))
10580 {
73869dc2 10581 dwo_sections->str.s.asection = sectp;
80626a55
DE
10582 dwo_sections->str.size = bfd_get_section_size (sectp);
10583 }
10584 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10585 {
73869dc2 10586 dwo_sections->str_offsets.s.asection = sectp;
80626a55
DE
10587 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10588 }
10589 else if (section_is_p (sectp->name, &names->types_dwo))
10590 {
10591 struct dwarf2_section_info type_section;
10592
10593 memset (&type_section, 0, sizeof (type_section));
73869dc2 10594 type_section.s.asection = sectp;
80626a55
DE
10595 type_section.size = bfd_get_section_size (sectp);
10596 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10597 &type_section);
10598 }
10599}
10600
ab5088bf 10601/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10602 by PER_CU. This is for the non-DWP case.
80626a55 10603 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10604
10605static struct dwo_file *
0ac5b59e
DE
10606open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10607 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10608{
10609 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
10610 struct dwo_file *dwo_file;
10611 bfd *dbfd;
3019eac3
DE
10612 struct cleanup *cleanups;
10613
ab5088bf 10614 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
10615 if (dbfd == NULL)
10616 {
b4f54984 10617 if (dwarf_read_debug)
80626a55
DE
10618 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10619 return NULL;
10620 }
10621 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10622 dwo_file->dwo_name = dwo_name;
10623 dwo_file->comp_dir = comp_dir;
80626a55 10624 dwo_file->dbfd = dbfd;
3019eac3
DE
10625
10626 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10627
80626a55 10628 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 10629
19c3d4c9 10630 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
10631
10632 dwo_file->tus = create_debug_types_hash_table (dwo_file,
10633 dwo_file->sections.types);
10634
10635 discard_cleanups (cleanups);
10636
b4f54984 10637 if (dwarf_read_debug)
80626a55
DE
10638 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10639
3019eac3
DE
10640 return dwo_file;
10641}
10642
80626a55 10643/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10644 size of each of the DWP debugging sections common to version 1 and 2 that
10645 we are interested in. */
3019eac3 10646
80626a55 10647static void
73869dc2
DE
10648dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10649 void *dwp_file_ptr)
3019eac3 10650{
80626a55
DE
10651 struct dwp_file *dwp_file = dwp_file_ptr;
10652 const struct dwop_section_names *names = &dwop_section_names;
10653 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10654
80626a55 10655 /* Record the ELF section number for later lookup: this is what the
73869dc2 10656 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10657 gdb_assert (elf_section_nr < dwp_file->num_sections);
10658 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10659
80626a55
DE
10660 /* Look for specific sections that we need. */
10661 if (section_is_p (sectp->name, &names->str_dwo))
10662 {
73869dc2 10663 dwp_file->sections.str.s.asection = sectp;
80626a55
DE
10664 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10665 }
10666 else if (section_is_p (sectp->name, &names->cu_index))
10667 {
73869dc2 10668 dwp_file->sections.cu_index.s.asection = sectp;
80626a55
DE
10669 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10670 }
10671 else if (section_is_p (sectp->name, &names->tu_index))
10672 {
73869dc2 10673 dwp_file->sections.tu_index.s.asection = sectp;
80626a55
DE
10674 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10675 }
10676}
3019eac3 10677
73869dc2
DE
10678/* This function is mapped across the sections and remembers the offset and
10679 size of each of the DWP version 2 debugging sections that we are interested
10680 in. This is split into a separate function because we don't know if we
10681 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10682
10683static void
10684dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10685{
10686 struct dwp_file *dwp_file = dwp_file_ptr;
10687 const struct dwop_section_names *names = &dwop_section_names;
10688 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10689
10690 /* Record the ELF section number for later lookup: this is what the
10691 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10692 gdb_assert (elf_section_nr < dwp_file->num_sections);
10693 dwp_file->elf_sections[elf_section_nr] = sectp;
10694
10695 /* Look for specific sections that we need. */
10696 if (section_is_p (sectp->name, &names->abbrev_dwo))
10697 {
10698 dwp_file->sections.abbrev.s.asection = sectp;
10699 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10700 }
10701 else if (section_is_p (sectp->name, &names->info_dwo))
10702 {
10703 dwp_file->sections.info.s.asection = sectp;
10704 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10705 }
10706 else if (section_is_p (sectp->name, &names->line_dwo))
10707 {
10708 dwp_file->sections.line.s.asection = sectp;
10709 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10710 }
10711 else if (section_is_p (sectp->name, &names->loc_dwo))
10712 {
10713 dwp_file->sections.loc.s.asection = sectp;
10714 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10715 }
10716 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10717 {
10718 dwp_file->sections.macinfo.s.asection = sectp;
10719 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10720 }
10721 else if (section_is_p (sectp->name, &names->macro_dwo))
10722 {
10723 dwp_file->sections.macro.s.asection = sectp;
10724 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10725 }
10726 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10727 {
10728 dwp_file->sections.str_offsets.s.asection = sectp;
10729 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10730 }
10731 else if (section_is_p (sectp->name, &names->types_dwo))
10732 {
10733 dwp_file->sections.types.s.asection = sectp;
10734 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10735 }
10736}
10737
80626a55 10738/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10739
80626a55
DE
10740static hashval_t
10741hash_dwp_loaded_cutus (const void *item)
10742{
10743 const struct dwo_unit *dwo_unit = item;
3019eac3 10744
80626a55
DE
10745 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10746 return dwo_unit->signature;
3019eac3
DE
10747}
10748
80626a55 10749/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10750
80626a55
DE
10751static int
10752eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10753{
80626a55
DE
10754 const struct dwo_unit *dua = a;
10755 const struct dwo_unit *dub = b;
3019eac3 10756
80626a55
DE
10757 return dua->signature == dub->signature;
10758}
3019eac3 10759
80626a55 10760/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10761
80626a55
DE
10762static htab_t
10763allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10764{
10765 return htab_create_alloc_ex (3,
10766 hash_dwp_loaded_cutus,
10767 eq_dwp_loaded_cutus,
10768 NULL,
10769 &objfile->objfile_obstack,
10770 hashtab_obstack_allocate,
10771 dummy_obstack_deallocate);
10772}
3019eac3 10773
ab5088bf
DE
10774/* Try to open DWP file FILE_NAME.
10775 The result is the bfd handle of the file.
10776 If there is a problem finding or opening the file, return NULL.
10777 Upon success, the canonicalized path of the file is stored in the bfd,
10778 same as symfile_bfd_open. */
10779
10780static bfd *
10781open_dwp_file (const char *file_name)
10782{
6ac97d4c
DE
10783 bfd *abfd;
10784
10785 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
10786 if (abfd != NULL)
10787 return abfd;
10788
10789 /* Work around upstream bug 15652.
10790 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10791 [Whether that's a "bug" is debatable, but it is getting in our way.]
10792 We have no real idea where the dwp file is, because gdb's realpath-ing
10793 of the executable's path may have discarded the needed info.
10794 [IWBN if the dwp file name was recorded in the executable, akin to
10795 .gnu_debuglink, but that doesn't exist yet.]
10796 Strip the directory from FILE_NAME and search again. */
10797 if (*debug_file_directory != '\0')
10798 {
10799 /* Don't implicitly search the current directory here.
10800 If the user wants to search "." to handle this case,
10801 it must be added to debug-file-directory. */
10802 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10803 0 /*search_cwd*/);
10804 }
10805
10806 return NULL;
ab5088bf
DE
10807}
10808
80626a55
DE
10809/* Initialize the use of the DWP file for the current objfile.
10810 By convention the name of the DWP file is ${objfile}.dwp.
10811 The result is NULL if it can't be found. */
a766d390 10812
80626a55 10813static struct dwp_file *
ab5088bf 10814open_and_init_dwp_file (void)
80626a55
DE
10815{
10816 struct objfile *objfile = dwarf2_per_objfile->objfile;
10817 struct dwp_file *dwp_file;
10818 char *dwp_name;
10819 bfd *dbfd;
10820 struct cleanup *cleanups;
10821
82bf32bc
JK
10822 /* Try to find first .dwp for the binary file before any symbolic links
10823 resolving. */
10824 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
80626a55
DE
10825 cleanups = make_cleanup (xfree, dwp_name);
10826
ab5088bf 10827 dbfd = open_dwp_file (dwp_name);
82bf32bc
JK
10828 if (dbfd == NULL
10829 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10830 {
10831 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10832 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10833 make_cleanup (xfree, dwp_name);
10834 dbfd = open_dwp_file (dwp_name);
10835 }
10836
80626a55
DE
10837 if (dbfd == NULL)
10838 {
b4f54984 10839 if (dwarf_read_debug)
80626a55
DE
10840 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10841 do_cleanups (cleanups);
10842 return NULL;
3019eac3 10843 }
80626a55 10844 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 10845 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
10846 dwp_file->dbfd = dbfd;
10847 do_cleanups (cleanups);
c906108c 10848
80626a55
DE
10849 /* +1: section 0 is unused */
10850 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
10851 dwp_file->elf_sections =
10852 OBSTACK_CALLOC (&objfile->objfile_obstack,
10853 dwp_file->num_sections, asection *);
10854
73869dc2 10855 bfd_map_over_sections (dbfd, dwarf2_locate_common_dwp_sections, dwp_file);
80626a55
DE
10856
10857 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10858
10859 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10860
73869dc2
DE
10861 /* The DWP file version is stored in the hash table. Oh well. */
10862 if (dwp_file->cus->version != dwp_file->tus->version)
10863 {
10864 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 10865 pretty bizarre. We use pulongest here because that's the established
4d65956b 10866 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
10867 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10868 " TU version %s [in DWP file %s]"),
10869 pulongest (dwp_file->cus->version),
10870 pulongest (dwp_file->tus->version), dwp_name);
73869dc2
DE
10871 }
10872 dwp_file->version = dwp_file->cus->version;
10873
10874 if (dwp_file->version == 2)
10875 bfd_map_over_sections (dbfd, dwarf2_locate_v2_dwp_sections, dwp_file);
10876
19ac8c2e
DE
10877 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10878 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 10879
b4f54984 10880 if (dwarf_read_debug)
80626a55
DE
10881 {
10882 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10883 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
10884 " %s CUs, %s TUs\n",
10885 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10886 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
10887 }
10888
10889 return dwp_file;
3019eac3 10890}
c906108c 10891
ab5088bf
DE
10892/* Wrapper around open_and_init_dwp_file, only open it once. */
10893
10894static struct dwp_file *
10895get_dwp_file (void)
10896{
10897 if (! dwarf2_per_objfile->dwp_checked)
10898 {
10899 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10900 dwarf2_per_objfile->dwp_checked = 1;
10901 }
10902 return dwarf2_per_objfile->dwp_file;
10903}
10904
80626a55
DE
10905/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10906 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10907 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 10908 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
10909 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10910
10911 This is called, for example, when wanting to read a variable with a
10912 complex location. Therefore we don't want to do file i/o for every call.
10913 Therefore we don't want to look for a DWO file on every call.
10914 Therefore we first see if we've already seen SIGNATURE in a DWP file,
10915 then we check if we've already seen DWO_NAME, and only THEN do we check
10916 for a DWO file.
10917
1c658ad5 10918 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 10919 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 10920
3019eac3 10921static struct dwo_unit *
80626a55
DE
10922lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
10923 const char *dwo_name, const char *comp_dir,
10924 ULONGEST signature, int is_debug_types)
3019eac3
DE
10925{
10926 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
10927 const char *kind = is_debug_types ? "TU" : "CU";
10928 void **dwo_file_slot;
3019eac3 10929 struct dwo_file *dwo_file;
80626a55 10930 struct dwp_file *dwp_file;
cb1df416 10931
6a506a2d
DE
10932 /* First see if there's a DWP file.
10933 If we have a DWP file but didn't find the DWO inside it, don't
10934 look for the original DWO file. It makes gdb behave differently
10935 depending on whether one is debugging in the build tree. */
cf2c3c16 10936
ab5088bf 10937 dwp_file = get_dwp_file ();
80626a55 10938 if (dwp_file != NULL)
cf2c3c16 10939 {
80626a55
DE
10940 const struct dwp_hash_table *dwp_htab =
10941 is_debug_types ? dwp_file->tus : dwp_file->cus;
10942
10943 if (dwp_htab != NULL)
10944 {
10945 struct dwo_unit *dwo_cutu =
57d63ce2
DE
10946 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
10947 signature, is_debug_types);
80626a55
DE
10948
10949 if (dwo_cutu != NULL)
10950 {
b4f54984 10951 if (dwarf_read_debug)
80626a55
DE
10952 {
10953 fprintf_unfiltered (gdb_stdlog,
10954 "Virtual DWO %s %s found: @%s\n",
10955 kind, hex_string (signature),
10956 host_address_to_string (dwo_cutu));
10957 }
10958 return dwo_cutu;
10959 }
10960 }
10961 }
6a506a2d 10962 else
80626a55 10963 {
6a506a2d 10964 /* No DWP file, look for the DWO file. */
80626a55 10965
6a506a2d
DE
10966 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
10967 if (*dwo_file_slot == NULL)
80626a55 10968 {
6a506a2d
DE
10969 /* Read in the file and build a table of the CUs/TUs it contains. */
10970 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 10971 }
6a506a2d
DE
10972 /* NOTE: This will be NULL if unable to open the file. */
10973 dwo_file = *dwo_file_slot;
3019eac3 10974
6a506a2d 10975 if (dwo_file != NULL)
19c3d4c9 10976 {
6a506a2d
DE
10977 struct dwo_unit *dwo_cutu = NULL;
10978
10979 if (is_debug_types && dwo_file->tus)
10980 {
10981 struct dwo_unit find_dwo_cutu;
10982
10983 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
10984 find_dwo_cutu.signature = signature;
10985 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
10986 }
10987 else if (!is_debug_types && dwo_file->cu)
80626a55 10988 {
6a506a2d
DE
10989 if (signature == dwo_file->cu->signature)
10990 dwo_cutu = dwo_file->cu;
10991 }
10992
10993 if (dwo_cutu != NULL)
10994 {
b4f54984 10995 if (dwarf_read_debug)
6a506a2d
DE
10996 {
10997 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
10998 kind, dwo_name, hex_string (signature),
10999 host_address_to_string (dwo_cutu));
11000 }
11001 return dwo_cutu;
80626a55
DE
11002 }
11003 }
2e276125 11004 }
9cdd5dbd 11005
80626a55
DE
11006 /* We didn't find it. This could mean a dwo_id mismatch, or
11007 someone deleted the DWO/DWP file, or the search path isn't set up
11008 correctly to find the file. */
11009
b4f54984 11010 if (dwarf_read_debug)
80626a55
DE
11011 {
11012 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11013 kind, dwo_name, hex_string (signature));
11014 }
3019eac3 11015
6656a72d
DE
11016 /* This is a warning and not a complaint because it can be caused by
11017 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11018 {
11019 /* Print the name of the DWP file if we looked there, helps the user
11020 better diagnose the problem. */
11021 char *dwp_text = NULL;
11022 struct cleanup *cleanups;
11023
11024 if (dwp_file != NULL)
11025 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11026 cleanups = make_cleanup (xfree, dwp_text);
11027
11028 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11029 " [in module %s]"),
11030 kind, dwo_name, hex_string (signature),
11031 dwp_text != NULL ? dwp_text : "",
11032 this_unit->is_debug_types ? "TU" : "CU",
11033 this_unit->offset.sect_off, objfile_name (objfile));
11034
11035 do_cleanups (cleanups);
11036 }
3019eac3 11037 return NULL;
5fb290d7
DJ
11038}
11039
80626a55
DE
11040/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11041 See lookup_dwo_cutu_unit for details. */
11042
11043static struct dwo_unit *
11044lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11045 const char *dwo_name, const char *comp_dir,
11046 ULONGEST signature)
11047{
11048 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11049}
11050
11051/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11052 See lookup_dwo_cutu_unit for details. */
11053
11054static struct dwo_unit *
11055lookup_dwo_type_unit (struct signatured_type *this_tu,
11056 const char *dwo_name, const char *comp_dir)
11057{
11058 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11059}
11060
89e63ee4
DE
11061/* Traversal function for queue_and_load_all_dwo_tus. */
11062
11063static int
11064queue_and_load_dwo_tu (void **slot, void *info)
11065{
11066 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11067 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11068 ULONGEST signature = dwo_unit->signature;
11069 struct signatured_type *sig_type =
11070 lookup_dwo_signatured_type (per_cu->cu, signature);
11071
11072 if (sig_type != NULL)
11073 {
11074 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11075
11076 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11077 a real dependency of PER_CU on SIG_TYPE. That is detected later
11078 while processing PER_CU. */
11079 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11080 load_full_type_unit (sig_cu);
11081 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11082 }
11083
11084 return 1;
11085}
11086
11087/* Queue all TUs contained in the DWO of PER_CU to be read in.
11088 The DWO may have the only definition of the type, though it may not be
11089 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11090 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11091
11092static void
11093queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11094{
11095 struct dwo_unit *dwo_unit;
11096 struct dwo_file *dwo_file;
11097
11098 gdb_assert (!per_cu->is_debug_types);
11099 gdb_assert (get_dwp_file () == NULL);
11100 gdb_assert (per_cu->cu != NULL);
11101
11102 dwo_unit = per_cu->cu->dwo_unit;
11103 gdb_assert (dwo_unit != NULL);
11104
11105 dwo_file = dwo_unit->dwo_file;
11106 if (dwo_file->tus != NULL)
11107 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11108}
11109
3019eac3
DE
11110/* Free all resources associated with DWO_FILE.
11111 Close the DWO file and munmap the sections.
11112 All memory should be on the objfile obstack. */
348e048f
DE
11113
11114static void
3019eac3 11115free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11116{
3019eac3
DE
11117 int ix;
11118 struct dwarf2_section_info *section;
348e048f 11119
5c6fa7ab 11120 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11121 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11122
3019eac3
DE
11123 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11124}
348e048f 11125
3019eac3 11126/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11127
3019eac3
DE
11128static void
11129free_dwo_file_cleanup (void *arg)
11130{
11131 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11132 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11133
3019eac3
DE
11134 free_dwo_file (dwo_file, objfile);
11135}
348e048f 11136
3019eac3 11137/* Traversal function for free_dwo_files. */
2ab95328 11138
3019eac3
DE
11139static int
11140free_dwo_file_from_slot (void **slot, void *info)
11141{
11142 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11143 struct objfile *objfile = (struct objfile *) info;
348e048f 11144
3019eac3 11145 free_dwo_file (dwo_file, objfile);
348e048f 11146
3019eac3
DE
11147 return 1;
11148}
348e048f 11149
3019eac3 11150/* Free all resources associated with DWO_FILES. */
348e048f 11151
3019eac3
DE
11152static void
11153free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11154{
11155 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11156}
3019eac3
DE
11157\f
11158/* Read in various DIEs. */
348e048f 11159
d389af10
JK
11160/* qsort helper for inherit_abstract_dies. */
11161
11162static int
11163unsigned_int_compar (const void *ap, const void *bp)
11164{
11165 unsigned int a = *(unsigned int *) ap;
11166 unsigned int b = *(unsigned int *) bp;
11167
11168 return (a > b) - (b > a);
11169}
11170
11171/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11172 Inherit only the children of the DW_AT_abstract_origin DIE not being
11173 already referenced by DW_AT_abstract_origin from the children of the
11174 current DIE. */
d389af10
JK
11175
11176static void
11177inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11178{
11179 struct die_info *child_die;
11180 unsigned die_children_count;
11181 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11182 sect_offset *offsets;
11183 sect_offset *offsets_end, *offsetp;
d389af10
JK
11184 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11185 struct die_info *origin_die;
11186 /* Iterator of the ORIGIN_DIE children. */
11187 struct die_info *origin_child_die;
11188 struct cleanup *cleanups;
11189 struct attribute *attr;
cd02d79d
PA
11190 struct dwarf2_cu *origin_cu;
11191 struct pending **origin_previous_list_in_scope;
d389af10
JK
11192
11193 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11194 if (!attr)
11195 return;
11196
cd02d79d
PA
11197 /* Note that following die references may follow to a die in a
11198 different cu. */
11199
11200 origin_cu = cu;
11201 origin_die = follow_die_ref (die, attr, &origin_cu);
11202
11203 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11204 symbols in. */
11205 origin_previous_list_in_scope = origin_cu->list_in_scope;
11206 origin_cu->list_in_scope = cu->list_in_scope;
11207
edb3359d
DJ
11208 if (die->tag != origin_die->tag
11209 && !(die->tag == DW_TAG_inlined_subroutine
11210 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11211 complaint (&symfile_complaints,
11212 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 11213 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
11214
11215 child_die = die->child;
11216 die_children_count = 0;
11217 while (child_die && child_die->tag)
11218 {
11219 child_die = sibling_die (child_die);
11220 die_children_count++;
11221 }
11222 offsets = xmalloc (sizeof (*offsets) * die_children_count);
11223 cleanups = make_cleanup (xfree, offsets);
11224
11225 offsets_end = offsets;
3ea89b92
PMR
11226 for (child_die = die->child;
11227 child_die && child_die->tag;
11228 child_die = sibling_die (child_die))
11229 {
11230 struct die_info *child_origin_die;
11231 struct dwarf2_cu *child_origin_cu;
11232
11233 /* We are trying to process concrete instance entries:
11234 DW_TAG_GNU_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11235 it's not relevant to our analysis here. i.e. detecting DIEs that are
11236 present in the abstract instance but not referenced in the concrete
11237 one. */
11238 if (child_die->tag == DW_TAG_GNU_call_site)
11239 continue;
11240
c38f313d
DJ
11241 /* For each CHILD_DIE, find the corresponding child of
11242 ORIGIN_DIE. If there is more than one layer of
11243 DW_AT_abstract_origin, follow them all; there shouldn't be,
11244 but GCC versions at least through 4.4 generate this (GCC PR
11245 40573). */
3ea89b92
PMR
11246 child_origin_die = child_die;
11247 child_origin_cu = cu;
c38f313d
DJ
11248 while (1)
11249 {
cd02d79d
PA
11250 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11251 child_origin_cu);
c38f313d
DJ
11252 if (attr == NULL)
11253 break;
cd02d79d
PA
11254 child_origin_die = follow_die_ref (child_origin_die, attr,
11255 &child_origin_cu);
c38f313d
DJ
11256 }
11257
d389af10
JK
11258 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11259 counterpart may exist. */
c38f313d 11260 if (child_origin_die != child_die)
d389af10 11261 {
edb3359d
DJ
11262 if (child_die->tag != child_origin_die->tag
11263 && !(child_die->tag == DW_TAG_inlined_subroutine
11264 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11265 complaint (&symfile_complaints,
11266 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11267 "different tags"), child_die->offset.sect_off,
11268 child_origin_die->offset.sect_off);
c38f313d
DJ
11269 if (child_origin_die->parent != origin_die)
11270 complaint (&symfile_complaints,
11271 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11272 "different parents"), child_die->offset.sect_off,
11273 child_origin_die->offset.sect_off);
c38f313d
DJ
11274 else
11275 *offsets_end++ = child_origin_die->offset;
d389af10 11276 }
d389af10
JK
11277 }
11278 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11279 unsigned_int_compar);
11280 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 11281 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
11282 complaint (&symfile_complaints,
11283 _("Multiple children of DIE 0x%x refer "
11284 "to DIE 0x%x as their abstract origin"),
b64f50a1 11285 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
11286
11287 offsetp = offsets;
11288 origin_child_die = origin_die->child;
11289 while (origin_child_die && origin_child_die->tag)
11290 {
11291 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
11292 while (offsetp < offsets_end
11293 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 11294 offsetp++;
b64f50a1
JK
11295 if (offsetp >= offsets_end
11296 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10 11297 {
adde2bff
DE
11298 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11299 Check whether we're already processing ORIGIN_CHILD_DIE.
11300 This can happen with mutually referenced abstract_origins.
11301 PR 16581. */
11302 if (!origin_child_die->in_process)
11303 process_die (origin_child_die, origin_cu);
d389af10
JK
11304 }
11305 origin_child_die = sibling_die (origin_child_die);
11306 }
cd02d79d 11307 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11308
11309 do_cleanups (cleanups);
11310}
11311
c906108c 11312static void
e7c27a73 11313read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11314{
e7c27a73 11315 struct objfile *objfile = cu->objfile;
3e29f34a 11316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11317 struct context_stack *newobj;
c906108c
SS
11318 CORE_ADDR lowpc;
11319 CORE_ADDR highpc;
11320 struct die_info *child_die;
edb3359d 11321 struct attribute *attr, *call_line, *call_file;
15d034d0 11322 const char *name;
e142c38c 11323 CORE_ADDR baseaddr;
801e3a5b 11324 struct block *block;
edb3359d 11325 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11326 VEC (symbolp) *template_args = NULL;
11327 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11328
11329 if (inlined_func)
11330 {
11331 /* If we do not have call site information, we can't show the
11332 caller of this inlined function. That's too confusing, so
11333 only use the scope for local variables. */
11334 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11335 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11336 if (call_line == NULL || call_file == NULL)
11337 {
11338 read_lexical_block_scope (die, cu);
11339 return;
11340 }
11341 }
c906108c 11342
e142c38c
DJ
11343 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11344
94af9270 11345 name = dwarf2_name (die, cu);
c906108c 11346
e8d05480
JB
11347 /* Ignore functions with missing or empty names. These are actually
11348 illegal according to the DWARF standard. */
11349 if (name == NULL)
11350 {
11351 complaint (&symfile_complaints,
b64f50a1
JK
11352 _("missing name for subprogram DIE at %d"),
11353 die->offset.sect_off);
e8d05480
JB
11354 return;
11355 }
11356
11357 /* Ignore functions with missing or invalid low and high pc attributes. */
11358 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11359 {
ae4d0c03
PM
11360 attr = dwarf2_attr (die, DW_AT_external, cu);
11361 if (!attr || !DW_UNSND (attr))
11362 complaint (&symfile_complaints,
3e43a32a
MS
11363 _("cannot get low and high bounds "
11364 "for subprogram DIE at %d"),
b64f50a1 11365 die->offset.sect_off);
e8d05480
JB
11366 return;
11367 }
c906108c 11368
3e29f34a
MR
11369 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11370 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11371
34eaf542
TT
11372 /* If we have any template arguments, then we must allocate a
11373 different sort of symbol. */
11374 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11375 {
11376 if (child_die->tag == DW_TAG_template_type_param
11377 || child_die->tag == DW_TAG_template_value_param)
11378 {
e623cf5d 11379 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11380 templ_func->base.is_cplus_template_function = 1;
11381 break;
11382 }
11383 }
11384
fe978cb0
PA
11385 newobj = push_context (0, lowpc);
11386 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11387 (struct symbol *) templ_func);
4c2df51b 11388
4cecd739
DJ
11389 /* If there is a location expression for DW_AT_frame_base, record
11390 it. */
e142c38c 11391 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11392 if (attr)
fe978cb0 11393 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11394
e142c38c 11395 cu->list_in_scope = &local_symbols;
c906108c 11396
639d11d3 11397 if (die->child != NULL)
c906108c 11398 {
639d11d3 11399 child_die = die->child;
c906108c
SS
11400 while (child_die && child_die->tag)
11401 {
34eaf542
TT
11402 if (child_die->tag == DW_TAG_template_type_param
11403 || child_die->tag == DW_TAG_template_value_param)
11404 {
11405 struct symbol *arg = new_symbol (child_die, NULL, cu);
11406
f1078f66
DJ
11407 if (arg != NULL)
11408 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11409 }
11410 else
11411 process_die (child_die, cu);
c906108c
SS
11412 child_die = sibling_die (child_die);
11413 }
11414 }
11415
d389af10
JK
11416 inherit_abstract_dies (die, cu);
11417
4a811a97
UW
11418 /* If we have a DW_AT_specification, we might need to import using
11419 directives from the context of the specification DIE. See the
11420 comment in determine_prefix. */
11421 if (cu->language == language_cplus
11422 && dwarf2_attr (die, DW_AT_specification, cu))
11423 {
11424 struct dwarf2_cu *spec_cu = cu;
11425 struct die_info *spec_die = die_specification (die, &spec_cu);
11426
11427 while (spec_die)
11428 {
11429 child_die = spec_die->child;
11430 while (child_die && child_die->tag)
11431 {
11432 if (child_die->tag == DW_TAG_imported_module)
11433 process_die (child_die, spec_cu);
11434 child_die = sibling_die (child_die);
11435 }
11436
11437 /* In some cases, GCC generates specification DIEs that
11438 themselves contain DW_AT_specification attributes. */
11439 spec_die = die_specification (spec_die, &spec_cu);
11440 }
11441 }
11442
fe978cb0 11443 newobj = pop_context ();
c906108c 11444 /* Make a block for the local symbols within. */
fe978cb0 11445 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
4d663531 11446 lowpc, highpc);
801e3a5b 11447
df8a16a1 11448 /* For C++, set the block's scope. */
45280282
IB
11449 if ((cu->language == language_cplus
11450 || cu->language == language_fortran
11451 || cu->language == language_d)
4d4ec4e5 11452 && cu->processing_has_namespace_info)
195a3f6c
TT
11453 block_set_scope (block, determine_prefix (die, cu),
11454 &objfile->objfile_obstack);
df8a16a1 11455
801e3a5b
JB
11456 /* If we have address ranges, record them. */
11457 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11458
fe978cb0 11459 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11460
34eaf542
TT
11461 /* Attach template arguments to function. */
11462 if (! VEC_empty (symbolp, template_args))
11463 {
11464 gdb_assert (templ_func != NULL);
11465
11466 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11467 templ_func->template_arguments
11468 = obstack_alloc (&objfile->objfile_obstack,
11469 (templ_func->n_template_arguments
11470 * sizeof (struct symbol *)));
11471 memcpy (templ_func->template_arguments,
11472 VEC_address (symbolp, template_args),
11473 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11474 VEC_free (symbolp, template_args);
11475 }
11476
208d8187
JB
11477 /* In C++, we can have functions nested inside functions (e.g., when
11478 a function declares a class that has methods). This means that
11479 when we finish processing a function scope, we may need to go
11480 back to building a containing block's symbol lists. */
fe978cb0 11481 local_symbols = newobj->locals;
22cee43f 11482 local_using_directives = newobj->local_using_directives;
208d8187 11483
921e78cf
JB
11484 /* If we've finished processing a top-level function, subsequent
11485 symbols go in the file symbol list. */
11486 if (outermost_context_p ())
e142c38c 11487 cu->list_in_scope = &file_symbols;
c906108c
SS
11488}
11489
11490/* Process all the DIES contained within a lexical block scope. Start
11491 a new scope, process the dies, and then close the scope. */
11492
11493static void
e7c27a73 11494read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11495{
e7c27a73 11496 struct objfile *objfile = cu->objfile;
3e29f34a 11497 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11498 struct context_stack *newobj;
c906108c
SS
11499 CORE_ADDR lowpc, highpc;
11500 struct die_info *child_die;
e142c38c
DJ
11501 CORE_ADDR baseaddr;
11502
11503 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11504
11505 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11506 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11507 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11508 be nasty. Might be easier to properly extend generic blocks to
af34e669 11509 describe ranges. */
d85a05f0 11510 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c 11511 return;
3e29f34a
MR
11512 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11513 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11514
11515 push_context (0, lowpc);
639d11d3 11516 if (die->child != NULL)
c906108c 11517 {
639d11d3 11518 child_die = die->child;
c906108c
SS
11519 while (child_die && child_die->tag)
11520 {
e7c27a73 11521 process_die (child_die, cu);
c906108c
SS
11522 child_die = sibling_die (child_die);
11523 }
11524 }
3ea89b92 11525 inherit_abstract_dies (die, cu);
fe978cb0 11526 newobj = pop_context ();
c906108c 11527
22cee43f 11528 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11529 {
801e3a5b 11530 struct block *block
fe978cb0
PA
11531 = finish_block (0, &local_symbols, newobj->old_blocks,
11532 newobj->start_addr, highpc);
801e3a5b
JB
11533
11534 /* Note that recording ranges after traversing children, as we
11535 do here, means that recording a parent's ranges entails
11536 walking across all its children's ranges as they appear in
11537 the address map, which is quadratic behavior.
11538
11539 It would be nicer to record the parent's ranges before
11540 traversing its children, simply overriding whatever you find
11541 there. But since we don't even decide whether to create a
11542 block until after we've traversed its children, that's hard
11543 to do. */
11544 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11545 }
fe978cb0 11546 local_symbols = newobj->locals;
22cee43f 11547 local_using_directives = newobj->local_using_directives;
c906108c
SS
11548}
11549
96408a79
SA
11550/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
11551
11552static void
11553read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11554{
11555 struct objfile *objfile = cu->objfile;
11556 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11557 CORE_ADDR pc, baseaddr;
11558 struct attribute *attr;
11559 struct call_site *call_site, call_site_local;
11560 void **slot;
11561 int nparams;
11562 struct die_info *child_die;
11563
11564 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11565
11566 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11567 if (!attr)
11568 {
11569 complaint (&symfile_complaints,
11570 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
11571 "DIE 0x%x [in module %s]"),
4262abfb 11572 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11573 return;
11574 }
31aa7e4e 11575 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11576 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11577
11578 if (cu->call_site_htab == NULL)
11579 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11580 NULL, &objfile->objfile_obstack,
11581 hashtab_obstack_allocate, NULL);
11582 call_site_local.pc = pc;
11583 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11584 if (*slot != NULL)
11585 {
11586 complaint (&symfile_complaints,
11587 _("Duplicate PC %s for DW_TAG_GNU_call_site "
11588 "DIE 0x%x [in module %s]"),
4262abfb
JK
11589 paddress (gdbarch, pc), die->offset.sect_off,
11590 objfile_name (objfile));
96408a79
SA
11591 return;
11592 }
11593
11594 /* Count parameters at the caller. */
11595
11596 nparams = 0;
11597 for (child_die = die->child; child_die && child_die->tag;
11598 child_die = sibling_die (child_die))
11599 {
11600 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11601 {
11602 complaint (&symfile_complaints,
11603 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
11604 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb
JK
11605 child_die->tag, child_die->offset.sect_off,
11606 objfile_name (objfile));
96408a79
SA
11607 continue;
11608 }
11609
11610 nparams++;
11611 }
11612
11613 call_site = obstack_alloc (&objfile->objfile_obstack,
11614 (sizeof (*call_site)
11615 + (sizeof (*call_site->parameter)
11616 * (nparams - 1))));
11617 *slot = call_site;
11618 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11619 call_site->pc = pc;
11620
11621 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11622 {
11623 struct die_info *func_die;
11624
11625 /* Skip also over DW_TAG_inlined_subroutine. */
11626 for (func_die = die->parent;
11627 func_die && func_die->tag != DW_TAG_subprogram
11628 && func_die->tag != DW_TAG_subroutine_type;
11629 func_die = func_die->parent);
11630
11631 /* DW_AT_GNU_all_call_sites is a superset
11632 of DW_AT_GNU_all_tail_call_sites. */
11633 if (func_die
11634 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11635 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11636 {
11637 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11638 not complete. But keep CALL_SITE for look ups via call_site_htab,
11639 both the initial caller containing the real return address PC and
11640 the final callee containing the current PC of a chain of tail
11641 calls do not need to have the tail call list complete. But any
11642 function candidate for a virtual tail call frame searched via
11643 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11644 determined unambiguously. */
11645 }
11646 else
11647 {
11648 struct type *func_type = NULL;
11649
11650 if (func_die)
11651 func_type = get_die_type (func_die, cu);
11652 if (func_type != NULL)
11653 {
11654 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11655
11656 /* Enlist this call site to the function. */
11657 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11658 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11659 }
11660 else
11661 complaint (&symfile_complaints,
11662 _("Cannot find function owning DW_TAG_GNU_call_site "
11663 "DIE 0x%x [in module %s]"),
4262abfb 11664 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11665 }
11666 }
11667
11668 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11669 if (attr == NULL)
11670 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11671 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11672 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11673 /* Keep NULL DWARF_BLOCK. */;
11674 else if (attr_form_is_block (attr))
11675 {
11676 struct dwarf2_locexpr_baton *dlbaton;
11677
11678 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
11679 dlbaton->data = DW_BLOCK (attr)->data;
11680 dlbaton->size = DW_BLOCK (attr)->size;
11681 dlbaton->per_cu = cu->per_cu;
11682
11683 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11684 }
7771576e 11685 else if (attr_form_is_ref (attr))
96408a79 11686 {
96408a79
SA
11687 struct dwarf2_cu *target_cu = cu;
11688 struct die_info *target_die;
11689
ac9ec31b 11690 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11691 gdb_assert (target_cu->objfile == objfile);
11692 if (die_is_declaration (target_die, target_cu))
11693 {
7d45c7c3 11694 const char *target_physname;
9112db09
JK
11695
11696 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
11697 target_physname = dwarf2_string_attr (target_die,
11698 DW_AT_linkage_name,
11699 target_cu);
11700 if (target_physname == NULL)
11701 target_physname = dwarf2_string_attr (target_die,
11702 DW_AT_MIPS_linkage_name,
11703 target_cu);
11704 if (target_physname == NULL)
9112db09 11705 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
11706 if (target_physname == NULL)
11707 complaint (&symfile_complaints,
11708 _("DW_AT_GNU_call_site_target target DIE has invalid "
11709 "physname, for referencing DIE 0x%x [in module %s]"),
4262abfb 11710 die->offset.sect_off, objfile_name (objfile));
96408a79 11711 else
7d455152 11712 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11713 }
11714 else
11715 {
11716 CORE_ADDR lowpc;
11717
11718 /* DW_AT_entry_pc should be preferred. */
11719 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
11720 complaint (&symfile_complaints,
11721 _("DW_AT_GNU_call_site_target target DIE has invalid "
11722 "low pc, for referencing DIE 0x%x [in module %s]"),
4262abfb 11723 die->offset.sect_off, objfile_name (objfile));
96408a79 11724 else
3e29f34a
MR
11725 {
11726 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11727 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11728 }
96408a79
SA
11729 }
11730 }
11731 else
11732 complaint (&symfile_complaints,
11733 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
11734 "block nor reference, for DIE 0x%x [in module %s]"),
4262abfb 11735 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11736
11737 call_site->per_cu = cu->per_cu;
11738
11739 for (child_die = die->child;
11740 child_die && child_die->tag;
11741 child_die = sibling_die (child_die))
11742 {
96408a79 11743 struct call_site_parameter *parameter;
1788b2d3 11744 struct attribute *loc, *origin;
96408a79
SA
11745
11746 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11747 {
11748 /* Already printed the complaint above. */
11749 continue;
11750 }
11751
11752 gdb_assert (call_site->parameter_count < nparams);
11753 parameter = &call_site->parameter[call_site->parameter_count];
11754
1788b2d3
JK
11755 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11756 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11757 register is contained in DW_AT_GNU_call_site_value. */
96408a79 11758
24c5c679 11759 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 11760 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 11761 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
11762 {
11763 sect_offset offset;
11764
11765 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11766 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
11767 if (!offset_in_cu_p (&cu->header, offset))
11768 {
11769 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11770 binding can be done only inside one CU. Such referenced DIE
11771 therefore cannot be even moved to DW_TAG_partial_unit. */
11772 complaint (&symfile_complaints,
11773 _("DW_AT_abstract_origin offset is not in CU for "
11774 "DW_TAG_GNU_call_site child DIE 0x%x "
11775 "[in module %s]"),
4262abfb 11776 child_die->offset.sect_off, objfile_name (objfile));
d76b7dbc
JK
11777 continue;
11778 }
1788b2d3
JK
11779 parameter->u.param_offset.cu_off = (offset.sect_off
11780 - cu->header.offset.sect_off);
11781 }
11782 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
11783 {
11784 complaint (&symfile_complaints,
11785 _("No DW_FORM_block* DW_AT_location for "
11786 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 11787 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11788 continue;
11789 }
24c5c679 11790 else
96408a79 11791 {
24c5c679
JK
11792 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11793 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11794 if (parameter->u.dwarf_reg != -1)
11795 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11796 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11797 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11798 &parameter->u.fb_offset))
11799 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11800 else
11801 {
11802 complaint (&symfile_complaints,
11803 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11804 "for DW_FORM_block* DW_AT_location is supported for "
11805 "DW_TAG_GNU_call_site child DIE 0x%x "
11806 "[in module %s]"),
4262abfb 11807 child_die->offset.sect_off, objfile_name (objfile));
24c5c679
JK
11808 continue;
11809 }
96408a79
SA
11810 }
11811
11812 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11813 if (!attr_form_is_block (attr))
11814 {
11815 complaint (&symfile_complaints,
11816 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
11817 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 11818 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11819 continue;
11820 }
11821 parameter->value = DW_BLOCK (attr)->data;
11822 parameter->value_size = DW_BLOCK (attr)->size;
11823
11824 /* Parameters are not pre-cleared by memset above. */
11825 parameter->data_value = NULL;
11826 parameter->data_value_size = 0;
11827 call_site->parameter_count++;
11828
11829 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11830 if (attr)
11831 {
11832 if (!attr_form_is_block (attr))
11833 complaint (&symfile_complaints,
11834 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
11835 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 11836 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11837 else
11838 {
11839 parameter->data_value = DW_BLOCK (attr)->data;
11840 parameter->data_value_size = DW_BLOCK (attr)->size;
11841 }
11842 }
11843 }
11844}
11845
43039443 11846/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
11847 Return 1 if the attributes are present and valid, otherwise, return 0.
11848 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
11849
11850static int
11851dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
11852 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11853 struct partial_symtab *ranges_pst)
43039443
JK
11854{
11855 struct objfile *objfile = cu->objfile;
3e29f34a 11856 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
11857 struct comp_unit_head *cu_header = &cu->header;
11858 bfd *obfd = objfile->obfd;
11859 unsigned int addr_size = cu_header->addr_size;
11860 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11861 /* Base address selection entry. */
11862 CORE_ADDR base;
11863 int found_base;
11864 unsigned int dummy;
d521ce57 11865 const gdb_byte *buffer;
43039443
JK
11866 CORE_ADDR marker;
11867 int low_set;
11868 CORE_ADDR low = 0;
11869 CORE_ADDR high = 0;
ff013f42 11870 CORE_ADDR baseaddr;
43039443 11871
d00adf39
DE
11872 found_base = cu->base_known;
11873 base = cu->base_address;
43039443 11874
be391dca 11875 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 11876 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
11877 {
11878 complaint (&symfile_complaints,
11879 _("Offset %d out of bounds for DW_AT_ranges attribute"),
11880 offset);
11881 return 0;
11882 }
dce234bc 11883 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
11884
11885 /* Read in the largest possible address. */
11886 marker = read_address (obfd, buffer, cu, &dummy);
11887 if ((marker & mask) == mask)
11888 {
11889 /* If we found the largest possible address, then
11890 read the base address. */
11891 base = read_address (obfd, buffer + addr_size, cu, &dummy);
11892 buffer += 2 * addr_size;
11893 offset += 2 * addr_size;
11894 found_base = 1;
11895 }
11896
11897 low_set = 0;
11898
e7030f15 11899 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 11900
43039443
JK
11901 while (1)
11902 {
11903 CORE_ADDR range_beginning, range_end;
11904
11905 range_beginning = read_address (obfd, buffer, cu, &dummy);
11906 buffer += addr_size;
11907 range_end = read_address (obfd, buffer, cu, &dummy);
11908 buffer += addr_size;
11909 offset += 2 * addr_size;
11910
11911 /* An end of list marker is a pair of zero addresses. */
11912 if (range_beginning == 0 && range_end == 0)
11913 /* Found the end of list entry. */
11914 break;
11915
11916 /* Each base address selection entry is a pair of 2 values.
11917 The first is the largest possible address, the second is
11918 the base address. Check for a base address here. */
11919 if ((range_beginning & mask) == mask)
11920 {
11921 /* If we found the largest possible address, then
11922 read the base address. */
11923 base = read_address (obfd, buffer + addr_size, cu, &dummy);
11924 found_base = 1;
11925 continue;
11926 }
11927
11928 if (!found_base)
11929 {
11930 /* We have no valid base address for the ranges
11931 data. */
11932 complaint (&symfile_complaints,
11933 _("Invalid .debug_ranges data (no base address)"));
11934 return 0;
11935 }
11936
9277c30c
UW
11937 if (range_beginning > range_end)
11938 {
11939 /* Inverted range entries are invalid. */
11940 complaint (&symfile_complaints,
11941 _("Invalid .debug_ranges data (inverted range)"));
11942 return 0;
11943 }
11944
11945 /* Empty range entries have no effect. */
11946 if (range_beginning == range_end)
11947 continue;
11948
43039443
JK
11949 range_beginning += base;
11950 range_end += base;
11951
01093045
DE
11952 /* A not-uncommon case of bad debug info.
11953 Don't pollute the addrmap with bad data. */
11954 if (range_beginning + baseaddr == 0
11955 && !dwarf2_per_objfile->has_section_at_zero)
11956 {
11957 complaint (&symfile_complaints,
11958 _(".debug_ranges entry has start address of zero"
4262abfb 11959 " [in module %s]"), objfile_name (objfile));
01093045
DE
11960 continue;
11961 }
11962
9277c30c 11963 if (ranges_pst != NULL)
3e29f34a
MR
11964 {
11965 CORE_ADDR lowpc;
11966 CORE_ADDR highpc;
11967
11968 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
11969 range_beginning + baseaddr);
11970 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
11971 range_end + baseaddr);
11972 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
11973 ranges_pst);
11974 }
ff013f42 11975
43039443
JK
11976 /* FIXME: This is recording everything as a low-high
11977 segment of consecutive addresses. We should have a
11978 data structure for discontiguous block ranges
11979 instead. */
11980 if (! low_set)
11981 {
11982 low = range_beginning;
11983 high = range_end;
11984 low_set = 1;
11985 }
11986 else
11987 {
11988 if (range_beginning < low)
11989 low = range_beginning;
11990 if (range_end > high)
11991 high = range_end;
11992 }
11993 }
11994
11995 if (! low_set)
11996 /* If the first entry is an end-of-list marker, the range
11997 describes an empty scope, i.e. no instructions. */
11998 return 0;
11999
12000 if (low_return)
12001 *low_return = low;
12002 if (high_return)
12003 *high_return = high;
12004 return 1;
12005}
12006
af34e669
DJ
12007/* Get low and high pc attributes from a die. Return 1 if the attributes
12008 are present and valid, otherwise, return 0. Return -1 if the range is
12009 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 12010
c906108c 12011static int
af34e669 12012dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12013 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12014 struct partial_symtab *pst)
c906108c
SS
12015{
12016 struct attribute *attr;
91da1414 12017 struct attribute *attr_high;
af34e669
DJ
12018 CORE_ADDR low = 0;
12019 CORE_ADDR high = 0;
12020 int ret = 0;
c906108c 12021
91da1414
MW
12022 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12023 if (attr_high)
af34e669 12024 {
e142c38c 12025 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12026 if (attr)
91da1414 12027 {
31aa7e4e
JB
12028 low = attr_value_as_address (attr);
12029 high = attr_value_as_address (attr_high);
12030 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12031 high += low;
91da1414 12032 }
af34e669
DJ
12033 else
12034 /* Found high w/o low attribute. */
12035 return 0;
12036
12037 /* Found consecutive range of addresses. */
12038 ret = 1;
12039 }
c906108c 12040 else
af34e669 12041 {
e142c38c 12042 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12043 if (attr != NULL)
12044 {
ab435259
DE
12045 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12046 We take advantage of the fact that DW_AT_ranges does not appear
12047 in DW_TAG_compile_unit of DWO files. */
12048 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12049 unsigned int ranges_offset = (DW_UNSND (attr)
12050 + (need_ranges_base
12051 ? cu->ranges_base
12052 : 0));
2e3cf129 12053
af34e669 12054 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12055 .debug_ranges section. */
2e3cf129 12056 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 12057 return 0;
43039443 12058 /* Found discontinuous range of addresses. */
af34e669
DJ
12059 ret = -1;
12060 }
12061 }
c906108c 12062
9373cf26
JK
12063 /* read_partial_die has also the strict LOW < HIGH requirement. */
12064 if (high <= low)
c906108c
SS
12065 return 0;
12066
12067 /* When using the GNU linker, .gnu.linkonce. sections are used to
12068 eliminate duplicate copies of functions and vtables and such.
12069 The linker will arbitrarily choose one and discard the others.
12070 The AT_*_pc values for such functions refer to local labels in
12071 these sections. If the section from that file was discarded, the
12072 labels are not in the output, so the relocs get a value of 0.
12073 If this is a discarded function, mark the pc bounds as invalid,
12074 so that GDB will ignore it. */
72dca2f5 12075 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
12076 return 0;
12077
12078 *lowpc = low;
96408a79
SA
12079 if (highpc)
12080 *highpc = high;
af34e669 12081 return ret;
c906108c
SS
12082}
12083
b084d499
JB
12084/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12085 its low and high PC addresses. Do nothing if these addresses could not
12086 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12087 and HIGHPC to the high address if greater than HIGHPC. */
12088
12089static void
12090dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12091 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12092 struct dwarf2_cu *cu)
12093{
12094 CORE_ADDR low, high;
12095 struct die_info *child = die->child;
12096
d85a05f0 12097 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
12098 {
12099 *lowpc = min (*lowpc, low);
12100 *highpc = max (*highpc, high);
12101 }
12102
12103 /* If the language does not allow nested subprograms (either inside
12104 subprograms or lexical blocks), we're done. */
12105 if (cu->language != language_ada)
12106 return;
6e70227d 12107
b084d499
JB
12108 /* Check all the children of the given DIE. If it contains nested
12109 subprograms, then check their pc bounds. Likewise, we need to
12110 check lexical blocks as well, as they may also contain subprogram
12111 definitions. */
12112 while (child && child->tag)
12113 {
12114 if (child->tag == DW_TAG_subprogram
12115 || child->tag == DW_TAG_lexical_block)
12116 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12117 child = sibling_die (child);
12118 }
12119}
12120
fae299cd
DC
12121/* Get the low and high pc's represented by the scope DIE, and store
12122 them in *LOWPC and *HIGHPC. If the correct values can't be
12123 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12124
12125static void
12126get_scope_pc_bounds (struct die_info *die,
12127 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12128 struct dwarf2_cu *cu)
12129{
12130 CORE_ADDR best_low = (CORE_ADDR) -1;
12131 CORE_ADDR best_high = (CORE_ADDR) 0;
12132 CORE_ADDR current_low, current_high;
12133
d85a05f0 12134 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
12135 {
12136 best_low = current_low;
12137 best_high = current_high;
12138 }
12139 else
12140 {
12141 struct die_info *child = die->child;
12142
12143 while (child && child->tag)
12144 {
12145 switch (child->tag) {
12146 case DW_TAG_subprogram:
b084d499 12147 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12148 break;
12149 case DW_TAG_namespace:
f55ee35c 12150 case DW_TAG_module:
fae299cd
DC
12151 /* FIXME: carlton/2004-01-16: Should we do this for
12152 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12153 that current GCC's always emit the DIEs corresponding
12154 to definitions of methods of classes as children of a
12155 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12156 the DIEs giving the declarations, which could be
12157 anywhere). But I don't see any reason why the
12158 standards says that they have to be there. */
12159 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12160
12161 if (current_low != ((CORE_ADDR) -1))
12162 {
12163 best_low = min (best_low, current_low);
12164 best_high = max (best_high, current_high);
12165 }
12166 break;
12167 default:
0963b4bd 12168 /* Ignore. */
fae299cd
DC
12169 break;
12170 }
12171
12172 child = sibling_die (child);
12173 }
12174 }
12175
12176 *lowpc = best_low;
12177 *highpc = best_high;
12178}
12179
801e3a5b
JB
12180/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12181 in DIE. */
380bca97 12182
801e3a5b
JB
12183static void
12184dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12185 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12186{
bb5ed363 12187 struct objfile *objfile = cu->objfile;
3e29f34a 12188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12189 struct attribute *attr;
91da1414 12190 struct attribute *attr_high;
801e3a5b 12191
91da1414
MW
12192 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12193 if (attr_high)
801e3a5b 12194 {
801e3a5b
JB
12195 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12196 if (attr)
12197 {
31aa7e4e
JB
12198 CORE_ADDR low = attr_value_as_address (attr);
12199 CORE_ADDR high = attr_value_as_address (attr_high);
12200
12201 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12202 high += low;
9a619af0 12203
3e29f34a
MR
12204 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12205 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12206 record_block_range (block, low, high - 1);
801e3a5b
JB
12207 }
12208 }
12209
12210 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12211 if (attr)
12212 {
bb5ed363 12213 bfd *obfd = objfile->obfd;
ab435259
DE
12214 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12215 We take advantage of the fact that DW_AT_ranges does not appear
12216 in DW_TAG_compile_unit of DWO files. */
12217 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12218
12219 /* The value of the DW_AT_ranges attribute is the offset of the
12220 address range list in the .debug_ranges section. */
ab435259
DE
12221 unsigned long offset = (DW_UNSND (attr)
12222 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12223 const gdb_byte *buffer;
801e3a5b
JB
12224
12225 /* For some target architectures, but not others, the
12226 read_address function sign-extends the addresses it returns.
12227 To recognize base address selection entries, we need a
12228 mask. */
12229 unsigned int addr_size = cu->header.addr_size;
12230 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12231
12232 /* The base address, to which the next pair is relative. Note
12233 that this 'base' is a DWARF concept: most entries in a range
12234 list are relative, to reduce the number of relocs against the
12235 debugging information. This is separate from this function's
12236 'baseaddr' argument, which GDB uses to relocate debugging
12237 information from a shared library based on the address at
12238 which the library was loaded. */
d00adf39
DE
12239 CORE_ADDR base = cu->base_address;
12240 int base_known = cu->base_known;
801e3a5b 12241
d62bfeaf 12242 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12243 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
12244 {
12245 complaint (&symfile_complaints,
12246 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
12247 offset);
12248 return;
12249 }
d62bfeaf 12250 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
12251
12252 for (;;)
12253 {
12254 unsigned int bytes_read;
12255 CORE_ADDR start, end;
12256
12257 start = read_address (obfd, buffer, cu, &bytes_read);
12258 buffer += bytes_read;
12259 end = read_address (obfd, buffer, cu, &bytes_read);
12260 buffer += bytes_read;
12261
12262 /* Did we find the end of the range list? */
12263 if (start == 0 && end == 0)
12264 break;
12265
12266 /* Did we find a base address selection entry? */
12267 else if ((start & base_select_mask) == base_select_mask)
12268 {
12269 base = end;
12270 base_known = 1;
12271 }
12272
12273 /* We found an ordinary address range. */
12274 else
12275 {
12276 if (!base_known)
12277 {
12278 complaint (&symfile_complaints,
3e43a32a
MS
12279 _("Invalid .debug_ranges data "
12280 "(no base address)"));
801e3a5b
JB
12281 return;
12282 }
12283
9277c30c
UW
12284 if (start > end)
12285 {
12286 /* Inverted range entries are invalid. */
12287 complaint (&symfile_complaints,
12288 _("Invalid .debug_ranges data "
12289 "(inverted range)"));
12290 return;
12291 }
12292
12293 /* Empty range entries have no effect. */
12294 if (start == end)
12295 continue;
12296
01093045
DE
12297 start += base + baseaddr;
12298 end += base + baseaddr;
12299
12300 /* A not-uncommon case of bad debug info.
12301 Don't pollute the addrmap with bad data. */
12302 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
12303 {
12304 complaint (&symfile_complaints,
12305 _(".debug_ranges entry has start address of zero"
4262abfb 12306 " [in module %s]"), objfile_name (objfile));
01093045
DE
12307 continue;
12308 }
12309
3e29f34a
MR
12310 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12311 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
01093045 12312 record_block_range (block, start, end - 1);
801e3a5b
JB
12313 }
12314 }
12315 }
12316}
12317
685b1105
JK
12318/* Check whether the producer field indicates either of GCC < 4.6, or the
12319 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12320
685b1105
JK
12321static void
12322check_producer (struct dwarf2_cu *cu)
60d5a603
JK
12323{
12324 const char *cs;
38360086 12325 int major, minor;
60d5a603
JK
12326
12327 if (cu->producer == NULL)
12328 {
12329 /* For unknown compilers expect their behavior is DWARF version
12330 compliant.
12331
12332 GCC started to support .debug_types sections by -gdwarf-4 since
12333 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12334 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12335 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12336 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12337 }
b1ffba5a 12338 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12339 {
38360086
MW
12340 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12341 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12342 }
61012eef 12343 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12344 cu->producer_is_icc = 1;
12345 else
12346 {
12347 /* For other non-GCC compilers, expect their behavior is DWARF version
12348 compliant. */
60d5a603
JK
12349 }
12350
ba919b58 12351 cu->checked_producer = 1;
685b1105 12352}
ba919b58 12353
685b1105
JK
12354/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12355 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12356 during 4.6.0 experimental. */
12357
12358static int
12359producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12360{
12361 if (!cu->checked_producer)
12362 check_producer (cu);
12363
12364 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12365}
12366
12367/* Return the default accessibility type if it is not overriden by
12368 DW_AT_accessibility. */
12369
12370static enum dwarf_access_attribute
12371dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12372{
12373 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12374 {
12375 /* The default DWARF 2 accessibility for members is public, the default
12376 accessibility for inheritance is private. */
12377
12378 if (die->tag != DW_TAG_inheritance)
12379 return DW_ACCESS_public;
12380 else
12381 return DW_ACCESS_private;
12382 }
12383 else
12384 {
12385 /* DWARF 3+ defines the default accessibility a different way. The same
12386 rules apply now for DW_TAG_inheritance as for the members and it only
12387 depends on the container kind. */
12388
12389 if (die->parent->tag == DW_TAG_class_type)
12390 return DW_ACCESS_private;
12391 else
12392 return DW_ACCESS_public;
12393 }
12394}
12395
74ac6d43
TT
12396/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12397 offset. If the attribute was not found return 0, otherwise return
12398 1. If it was found but could not properly be handled, set *OFFSET
12399 to 0. */
12400
12401static int
12402handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12403 LONGEST *offset)
12404{
12405 struct attribute *attr;
12406
12407 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12408 if (attr != NULL)
12409 {
12410 *offset = 0;
12411
12412 /* Note that we do not check for a section offset first here.
12413 This is because DW_AT_data_member_location is new in DWARF 4,
12414 so if we see it, we can assume that a constant form is really
12415 a constant and not a section offset. */
12416 if (attr_form_is_constant (attr))
12417 *offset = dwarf2_get_attr_constant_value (attr, 0);
12418 else if (attr_form_is_section_offset (attr))
12419 dwarf2_complex_location_expr_complaint ();
12420 else if (attr_form_is_block (attr))
12421 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12422 else
12423 dwarf2_complex_location_expr_complaint ();
12424
12425 return 1;
12426 }
12427
12428 return 0;
12429}
12430
c906108c
SS
12431/* Add an aggregate field to the field list. */
12432
12433static void
107d2387 12434dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12435 struct dwarf2_cu *cu)
6e70227d 12436{
e7c27a73 12437 struct objfile *objfile = cu->objfile;
5e2b427d 12438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12439 struct nextfield *new_field;
12440 struct attribute *attr;
12441 struct field *fp;
15d034d0 12442 const char *fieldname = "";
c906108c
SS
12443
12444 /* Allocate a new field list entry and link it in. */
12445 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 12446 make_cleanup (xfree, new_field);
c906108c 12447 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12448
12449 if (die->tag == DW_TAG_inheritance)
12450 {
12451 new_field->next = fip->baseclasses;
12452 fip->baseclasses = new_field;
12453 }
12454 else
12455 {
12456 new_field->next = fip->fields;
12457 fip->fields = new_field;
12458 }
c906108c
SS
12459 fip->nfields++;
12460
e142c38c 12461 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12462 if (attr)
12463 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12464 else
12465 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12466 if (new_field->accessibility != DW_ACCESS_public)
12467 fip->non_public_fields = 1;
60d5a603 12468
e142c38c 12469 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12470 if (attr)
12471 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12472 else
12473 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12474
12475 fp = &new_field->field;
a9a9bd0f 12476
e142c38c 12477 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12478 {
74ac6d43
TT
12479 LONGEST offset;
12480
a9a9bd0f 12481 /* Data member other than a C++ static data member. */
6e70227d 12482
c906108c 12483 /* Get type of field. */
e7c27a73 12484 fp->type = die_type (die, cu);
c906108c 12485
d6a843b5 12486 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12487
c906108c 12488 /* Get bit size of field (zero if none). */
e142c38c 12489 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12490 if (attr)
12491 {
12492 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12493 }
12494 else
12495 {
12496 FIELD_BITSIZE (*fp) = 0;
12497 }
12498
12499 /* Get bit offset of field. */
74ac6d43
TT
12500 if (handle_data_member_location (die, cu, &offset))
12501 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12502 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12503 if (attr)
12504 {
5e2b427d 12505 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12506 {
12507 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12508 additional bit offset from the MSB of the containing
12509 anonymous object to the MSB of the field. We don't
12510 have to do anything special since we don't need to
12511 know the size of the anonymous object. */
f41f5e61 12512 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12513 }
12514 else
12515 {
12516 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12517 MSB of the anonymous object, subtract off the number of
12518 bits from the MSB of the field to the MSB of the
12519 object, and then subtract off the number of bits of
12520 the field itself. The result is the bit offset of
12521 the LSB of the field. */
c906108c
SS
12522 int anonymous_size;
12523 int bit_offset = DW_UNSND (attr);
12524
e142c38c 12525 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12526 if (attr)
12527 {
12528 /* The size of the anonymous object containing
12529 the bit field is explicit, so use the
12530 indicated size (in bytes). */
12531 anonymous_size = DW_UNSND (attr);
12532 }
12533 else
12534 {
12535 /* The size of the anonymous object containing
12536 the bit field must be inferred from the type
12537 attribute of the data member containing the
12538 bit field. */
12539 anonymous_size = TYPE_LENGTH (fp->type);
12540 }
f41f5e61
PA
12541 SET_FIELD_BITPOS (*fp,
12542 (FIELD_BITPOS (*fp)
12543 + anonymous_size * bits_per_byte
12544 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12545 }
12546 }
12547
12548 /* Get name of field. */
39cbfefa
DJ
12549 fieldname = dwarf2_name (die, cu);
12550 if (fieldname == NULL)
12551 fieldname = "";
d8151005
DJ
12552
12553 /* The name is already allocated along with this objfile, so we don't
12554 need to duplicate it for the type. */
12555 fp->name = fieldname;
c906108c
SS
12556
12557 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12558 pointer or virtual base class pointer) to private. */
e142c38c 12559 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12560 {
d48cc9dd 12561 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12562 new_field->accessibility = DW_ACCESS_private;
12563 fip->non_public_fields = 1;
12564 }
12565 }
a9a9bd0f 12566 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12567 {
a9a9bd0f
DC
12568 /* C++ static member. */
12569
12570 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12571 is a declaration, but all versions of G++ as of this writing
12572 (so through at least 3.2.1) incorrectly generate
12573 DW_TAG_variable tags. */
6e70227d 12574
ff355380 12575 const char *physname;
c906108c 12576
a9a9bd0f 12577 /* Get name of field. */
39cbfefa
DJ
12578 fieldname = dwarf2_name (die, cu);
12579 if (fieldname == NULL)
c906108c
SS
12580 return;
12581
254e6b9e 12582 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
12583 if (attr
12584 /* Only create a symbol if this is an external value.
12585 new_symbol checks this and puts the value in the global symbol
12586 table, which we want. If it is not external, new_symbol
12587 will try to put the value in cu->list_in_scope which is wrong. */
12588 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
12589 {
12590 /* A static const member, not much different than an enum as far as
12591 we're concerned, except that we can support more types. */
12592 new_symbol (die, NULL, cu);
12593 }
12594
2df3850c 12595 /* Get physical name. */
ff355380 12596 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12597
d8151005
DJ
12598 /* The name is already allocated along with this objfile, so we don't
12599 need to duplicate it for the type. */
12600 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 12601 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12602 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12603 }
12604 else if (die->tag == DW_TAG_inheritance)
12605 {
74ac6d43 12606 LONGEST offset;
d4b96c9a 12607
74ac6d43
TT
12608 /* C++ base class field. */
12609 if (handle_data_member_location (die, cu, &offset))
12610 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12611 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12612 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12613 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12614 fip->nbaseclasses++;
12615 }
12616}
12617
98751a41
JK
12618/* Add a typedef defined in the scope of the FIP's class. */
12619
12620static void
12621dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12622 struct dwarf2_cu *cu)
6e70227d 12623{
98751a41 12624 struct objfile *objfile = cu->objfile;
98751a41
JK
12625 struct typedef_field_list *new_field;
12626 struct attribute *attr;
12627 struct typedef_field *fp;
12628 char *fieldname = "";
12629
12630 /* Allocate a new field list entry and link it in. */
12631 new_field = xzalloc (sizeof (*new_field));
12632 make_cleanup (xfree, new_field);
12633
12634 gdb_assert (die->tag == DW_TAG_typedef);
12635
12636 fp = &new_field->field;
12637
12638 /* Get name of field. */
12639 fp->name = dwarf2_name (die, cu);
12640 if (fp->name == NULL)
12641 return;
12642
12643 fp->type = read_type_die (die, cu);
12644
12645 new_field->next = fip->typedef_field_list;
12646 fip->typedef_field_list = new_field;
12647 fip->typedef_field_list_count++;
12648}
12649
c906108c
SS
12650/* Create the vector of fields, and attach it to the type. */
12651
12652static void
fba45db2 12653dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12654 struct dwarf2_cu *cu)
c906108c
SS
12655{
12656 int nfields = fip->nfields;
12657
12658 /* Record the field count, allocate space for the array of fields,
12659 and create blank accessibility bitfields if necessary. */
12660 TYPE_NFIELDS (type) = nfields;
12661 TYPE_FIELDS (type) = (struct field *)
12662 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12663 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12664
b4ba55a1 12665 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
12666 {
12667 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12668
12669 TYPE_FIELD_PRIVATE_BITS (type) =
12670 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12671 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12672
12673 TYPE_FIELD_PROTECTED_BITS (type) =
12674 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12675 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12676
774b6a14
TT
12677 TYPE_FIELD_IGNORE_BITS (type) =
12678 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12679 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
12680 }
12681
12682 /* If the type has baseclasses, allocate and clear a bit vector for
12683 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 12684 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
12685 {
12686 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 12687 unsigned char *pointer;
c906108c
SS
12688
12689 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
12690 pointer = TYPE_ALLOC (type, num_bytes);
12691 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
12692 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12693 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12694 }
12695
3e43a32a
MS
12696 /* Copy the saved-up fields into the field vector. Start from the head of
12697 the list, adding to the tail of the field array, so that they end up in
12698 the same order in the array in which they were added to the list. */
c906108c
SS
12699 while (nfields-- > 0)
12700 {
7d0ccb61
DJ
12701 struct nextfield *fieldp;
12702
12703 if (fip->fields)
12704 {
12705 fieldp = fip->fields;
12706 fip->fields = fieldp->next;
12707 }
12708 else
12709 {
12710 fieldp = fip->baseclasses;
12711 fip->baseclasses = fieldp->next;
12712 }
12713
12714 TYPE_FIELD (type, nfields) = fieldp->field;
12715 switch (fieldp->accessibility)
c906108c 12716 {
c5aa993b 12717 case DW_ACCESS_private:
b4ba55a1
JB
12718 if (cu->language != language_ada)
12719 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 12720 break;
c906108c 12721
c5aa993b 12722 case DW_ACCESS_protected:
b4ba55a1
JB
12723 if (cu->language != language_ada)
12724 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 12725 break;
c906108c 12726
c5aa993b
JM
12727 case DW_ACCESS_public:
12728 break;
c906108c 12729
c5aa993b
JM
12730 default:
12731 /* Unknown accessibility. Complain and treat it as public. */
12732 {
e2e0b3e5 12733 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 12734 fieldp->accessibility);
c5aa993b
JM
12735 }
12736 break;
c906108c
SS
12737 }
12738 if (nfields < fip->nbaseclasses)
12739 {
7d0ccb61 12740 switch (fieldp->virtuality)
c906108c 12741 {
c5aa993b
JM
12742 case DW_VIRTUALITY_virtual:
12743 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 12744 if (cu->language == language_ada)
a73c6dcd 12745 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
12746 SET_TYPE_FIELD_VIRTUAL (type, nfields);
12747 break;
c906108c
SS
12748 }
12749 }
c906108c
SS
12750 }
12751}
12752
7d27a96d
TT
12753/* Return true if this member function is a constructor, false
12754 otherwise. */
12755
12756static int
12757dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12758{
12759 const char *fieldname;
fe978cb0 12760 const char *type_name;
7d27a96d
TT
12761 int len;
12762
12763 if (die->parent == NULL)
12764 return 0;
12765
12766 if (die->parent->tag != DW_TAG_structure_type
12767 && die->parent->tag != DW_TAG_union_type
12768 && die->parent->tag != DW_TAG_class_type)
12769 return 0;
12770
12771 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
12772 type_name = dwarf2_name (die->parent, cu);
12773 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
12774 return 0;
12775
12776 len = strlen (fieldname);
fe978cb0
PA
12777 return (strncmp (fieldname, type_name, len) == 0
12778 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
12779}
12780
c906108c
SS
12781/* Add a member function to the proper fieldlist. */
12782
12783static void
107d2387 12784dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 12785 struct type *type, struct dwarf2_cu *cu)
c906108c 12786{
e7c27a73 12787 struct objfile *objfile = cu->objfile;
c906108c
SS
12788 struct attribute *attr;
12789 struct fnfieldlist *flp;
12790 int i;
12791 struct fn_field *fnp;
15d034d0 12792 const char *fieldname;
c906108c 12793 struct nextfnfield *new_fnfield;
f792889a 12794 struct type *this_type;
60d5a603 12795 enum dwarf_access_attribute accessibility;
c906108c 12796
b4ba55a1 12797 if (cu->language == language_ada)
a73c6dcd 12798 error (_("unexpected member function in Ada type"));
b4ba55a1 12799
2df3850c 12800 /* Get name of member function. */
39cbfefa
DJ
12801 fieldname = dwarf2_name (die, cu);
12802 if (fieldname == NULL)
2df3850c 12803 return;
c906108c 12804
c906108c
SS
12805 /* Look up member function name in fieldlist. */
12806 for (i = 0; i < fip->nfnfields; i++)
12807 {
27bfe10e 12808 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
12809 break;
12810 }
12811
12812 /* Create new list element if necessary. */
12813 if (i < fip->nfnfields)
12814 flp = &fip->fnfieldlists[i];
12815 else
12816 {
12817 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
12818 {
12819 fip->fnfieldlists = (struct fnfieldlist *)
12820 xrealloc (fip->fnfieldlists,
12821 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12822 * sizeof (struct fnfieldlist));
c906108c 12823 if (fip->nfnfields == 0)
c13c43fd 12824 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
12825 }
12826 flp = &fip->fnfieldlists[fip->nfnfields];
12827 flp->name = fieldname;
12828 flp->length = 0;
12829 flp->head = NULL;
3da10d80 12830 i = fip->nfnfields++;
c906108c
SS
12831 }
12832
12833 /* Create a new member function field and chain it to the field list
0963b4bd 12834 entry. */
c906108c 12835 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 12836 make_cleanup (xfree, new_fnfield);
c906108c
SS
12837 memset (new_fnfield, 0, sizeof (struct nextfnfield));
12838 new_fnfield->next = flp->head;
12839 flp->head = new_fnfield;
12840 flp->length++;
12841
12842 /* Fill in the member function field info. */
12843 fnp = &new_fnfield->fnfield;
3da10d80
KS
12844
12845 /* Delay processing of the physname until later. */
12846 if (cu->language == language_cplus || cu->language == language_java)
12847 {
12848 add_to_method_list (type, i, flp->length - 1, fieldname,
12849 die, cu);
12850 }
12851 else
12852 {
1d06ead6 12853 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
12854 fnp->physname = physname ? physname : "";
12855 }
12856
c906108c 12857 fnp->type = alloc_type (objfile);
f792889a
DJ
12858 this_type = read_type_die (die, cu);
12859 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 12860 {
f792889a 12861 int nparams = TYPE_NFIELDS (this_type);
c906108c 12862
f792889a 12863 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
12864 of the method itself (TYPE_CODE_METHOD). */
12865 smash_to_method_type (fnp->type, type,
f792889a
DJ
12866 TYPE_TARGET_TYPE (this_type),
12867 TYPE_FIELDS (this_type),
12868 TYPE_NFIELDS (this_type),
12869 TYPE_VARARGS (this_type));
c906108c
SS
12870
12871 /* Handle static member functions.
c5aa993b 12872 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
12873 member functions. G++ helps GDB by marking the first
12874 parameter for non-static member functions (which is the this
12875 pointer) as artificial. We obtain this information from
12876 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 12877 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
12878 fnp->voffset = VOFFSET_STATIC;
12879 }
12880 else
e2e0b3e5 12881 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 12882 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
12883
12884 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 12885 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 12886 fnp->fcontext = die_containing_type (die, cu);
c906108c 12887
3e43a32a
MS
12888 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12889 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
12890
12891 /* Get accessibility. */
e142c38c 12892 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 12893 if (attr)
aead7601 12894 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
12895 else
12896 accessibility = dwarf2_default_access_attribute (die, cu);
12897 switch (accessibility)
c906108c 12898 {
60d5a603
JK
12899 case DW_ACCESS_private:
12900 fnp->is_private = 1;
12901 break;
12902 case DW_ACCESS_protected:
12903 fnp->is_protected = 1;
12904 break;
c906108c
SS
12905 }
12906
b02dede2 12907 /* Check for artificial methods. */
e142c38c 12908 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
12909 if (attr && DW_UNSND (attr) != 0)
12910 fnp->is_artificial = 1;
12911
7d27a96d
TT
12912 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12913
0d564a31 12914 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
12915 function. For older versions of GCC, this is an offset in the
12916 appropriate virtual table, as specified by DW_AT_containing_type.
12917 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
12918 to the object address. */
12919
e142c38c 12920 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 12921 if (attr)
8e19ed76 12922 {
aec5aa8b 12923 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 12924 {
aec5aa8b
TT
12925 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
12926 {
12927 /* Old-style GCC. */
12928 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
12929 }
12930 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
12931 || (DW_BLOCK (attr)->size > 1
12932 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
12933 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
12934 {
12935 struct dwarf_block blk;
12936 int offset;
12937
12938 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
12939 ? 1 : 2);
12940 blk.size = DW_BLOCK (attr)->size - offset;
12941 blk.data = DW_BLOCK (attr)->data + offset;
12942 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
12943 if ((fnp->voffset % cu->header.addr_size) != 0)
12944 dwarf2_complex_location_expr_complaint ();
12945 else
12946 fnp->voffset /= cu->header.addr_size;
12947 fnp->voffset += 2;
12948 }
12949 else
12950 dwarf2_complex_location_expr_complaint ();
12951
12952 if (!fnp->fcontext)
7e993ebf
KS
12953 {
12954 /* If there is no `this' field and no DW_AT_containing_type,
12955 we cannot actually find a base class context for the
12956 vtable! */
12957 if (TYPE_NFIELDS (this_type) == 0
12958 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
12959 {
12960 complaint (&symfile_complaints,
12961 _("cannot determine context for virtual member "
12962 "function \"%s\" (offset %d)"),
12963 fieldname, die->offset.sect_off);
12964 }
12965 else
12966 {
12967 fnp->fcontext
12968 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
12969 }
12970 }
aec5aa8b 12971 }
3690dd37 12972 else if (attr_form_is_section_offset (attr))
8e19ed76 12973 {
4d3c2250 12974 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
12975 }
12976 else
12977 {
4d3c2250
KB
12978 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12979 fieldname);
8e19ed76 12980 }
0d564a31 12981 }
d48cc9dd
DJ
12982 else
12983 {
12984 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12985 if (attr && DW_UNSND (attr))
12986 {
12987 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12988 complaint (&symfile_complaints,
3e43a32a
MS
12989 _("Member function \"%s\" (offset %d) is virtual "
12990 "but the vtable offset is not specified"),
b64f50a1 12991 fieldname, die->offset.sect_off);
9655fd1a 12992 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
12993 TYPE_CPLUS_DYNAMIC (type) = 1;
12994 }
12995 }
c906108c
SS
12996}
12997
12998/* Create the vector of member function fields, and attach it to the type. */
12999
13000static void
fba45db2 13001dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13002 struct dwarf2_cu *cu)
c906108c
SS
13003{
13004 struct fnfieldlist *flp;
c906108c
SS
13005 int i;
13006
b4ba55a1 13007 if (cu->language == language_ada)
a73c6dcd 13008 error (_("unexpected member functions in Ada type"));
b4ba55a1 13009
c906108c
SS
13010 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13011 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13012 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13013
13014 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13015 {
13016 struct nextfnfield *nfp = flp->head;
13017 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13018 int k;
13019
13020 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13021 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13022 fn_flp->fn_fields = (struct fn_field *)
13023 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13024 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13025 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13026 }
13027
13028 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13029}
13030
1168df01
JB
13031/* Returns non-zero if NAME is the name of a vtable member in CU's
13032 language, zero otherwise. */
13033static int
13034is_vtable_name (const char *name, struct dwarf2_cu *cu)
13035{
13036 static const char vptr[] = "_vptr";
987504bb 13037 static const char vtable[] = "vtable";
1168df01 13038
987504bb
JJ
13039 /* Look for the C++ and Java forms of the vtable. */
13040 if ((cu->language == language_java
61012eef
GB
13041 && startswith (name, vtable))
13042 || (startswith (name, vptr)
987504bb 13043 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
13044 return 1;
13045
13046 return 0;
13047}
13048
c0dd20ea 13049/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13050 functions, with the ABI-specified layout. If TYPE describes
13051 such a structure, smash it into a member function type.
61049d3b
DJ
13052
13053 GCC shouldn't do this; it should just output pointer to member DIEs.
13054 This is GCC PR debug/28767. */
c0dd20ea 13055
0b92b5bb
TT
13056static void
13057quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13058{
09e2d7c7 13059 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13060
13061 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13062 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13063 return;
c0dd20ea
DJ
13064
13065 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13066 if (TYPE_FIELD_NAME (type, 0) == NULL
13067 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13068 || TYPE_FIELD_NAME (type, 1) == NULL
13069 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13070 return;
c0dd20ea
DJ
13071
13072 /* Find the type of the method. */
0b92b5bb 13073 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13074 if (pfn_type == NULL
13075 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13076 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13077 return;
c0dd20ea
DJ
13078
13079 /* Look for the "this" argument. */
13080 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13081 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13082 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13083 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13084 return;
c0dd20ea 13085
09e2d7c7 13086 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13087 new_type = alloc_type (objfile);
09e2d7c7 13088 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13089 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13090 TYPE_VARARGS (pfn_type));
0b92b5bb 13091 smash_to_methodptr_type (type, new_type);
c0dd20ea 13092}
1168df01 13093
685b1105
JK
13094/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13095 (icc). */
13096
13097static int
13098producer_is_icc (struct dwarf2_cu *cu)
13099{
13100 if (!cu->checked_producer)
13101 check_producer (cu);
13102
13103 return cu->producer_is_icc;
13104}
13105
c906108c 13106/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13107 (definition) to create a type for the structure or union. Fill in
13108 the type's name and general properties; the members will not be
83655187
DE
13109 processed until process_structure_scope. A symbol table entry for
13110 the type will also not be done until process_structure_scope (assuming
13111 the type has a name).
c906108c 13112
c767944b
DJ
13113 NOTE: we need to call these functions regardless of whether or not the
13114 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13115 structure or union. This gets the type entered into our set of
83655187 13116 user defined types. */
c906108c 13117
f792889a 13118static struct type *
134d01f1 13119read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13120{
e7c27a73 13121 struct objfile *objfile = cu->objfile;
c906108c
SS
13122 struct type *type;
13123 struct attribute *attr;
15d034d0 13124 const char *name;
c906108c 13125
348e048f
DE
13126 /* If the definition of this type lives in .debug_types, read that type.
13127 Don't follow DW_AT_specification though, that will take us back up
13128 the chain and we want to go down. */
45e58e77 13129 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13130 if (attr)
13131 {
ac9ec31b 13132 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13133
ac9ec31b 13134 /* The type's CU may not be the same as CU.
02142a6c 13135 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13136 return set_die_type (die, type, cu);
13137 }
13138
c0dd20ea 13139 type = alloc_type (objfile);
c906108c 13140 INIT_CPLUS_SPECIFIC (type);
93311388 13141
39cbfefa
DJ
13142 name = dwarf2_name (die, cu);
13143 if (name != NULL)
c906108c 13144 {
987504bb 13145 if (cu->language == language_cplus
45280282
IB
13146 || cu->language == language_java
13147 || cu->language == language_d)
63d06c5c 13148 {
15d034d0 13149 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13150
13151 /* dwarf2_full_name might have already finished building the DIE's
13152 type. If so, there is no need to continue. */
13153 if (get_die_type (die, cu) != NULL)
13154 return get_die_type (die, cu);
13155
13156 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13157 if (die->tag == DW_TAG_structure_type
13158 || die->tag == DW_TAG_class_type)
13159 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13160 }
13161 else
13162 {
d8151005
DJ
13163 /* The name is already allocated along with this objfile, so
13164 we don't need to duplicate it for the type. */
7d455152 13165 TYPE_TAG_NAME (type) = name;
94af9270
KS
13166 if (die->tag == DW_TAG_class_type)
13167 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13168 }
c906108c
SS
13169 }
13170
13171 if (die->tag == DW_TAG_structure_type)
13172 {
13173 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13174 }
13175 else if (die->tag == DW_TAG_union_type)
13176 {
13177 TYPE_CODE (type) = TYPE_CODE_UNION;
13178 }
13179 else
13180 {
4753d33b 13181 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13182 }
13183
0cc2414c
TT
13184 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13185 TYPE_DECLARED_CLASS (type) = 1;
13186
e142c38c 13187 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13188 if (attr)
13189 {
13190 TYPE_LENGTH (type) = DW_UNSND (attr);
13191 }
13192 else
13193 {
13194 TYPE_LENGTH (type) = 0;
13195 }
13196
422b1cb0 13197 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13198 {
13199 /* ICC does not output the required DW_AT_declaration
13200 on incomplete types, but gives them a size of zero. */
422b1cb0 13201 TYPE_STUB (type) = 1;
685b1105
JK
13202 }
13203 else
13204 TYPE_STUB_SUPPORTED (type) = 1;
13205
dc718098 13206 if (die_is_declaration (die, cu))
876cecd0 13207 TYPE_STUB (type) = 1;
a6c727b2
DJ
13208 else if (attr == NULL && die->child == NULL
13209 && producer_is_realview (cu->producer))
13210 /* RealView does not output the required DW_AT_declaration
13211 on incomplete types. */
13212 TYPE_STUB (type) = 1;
dc718098 13213
c906108c
SS
13214 /* We need to add the type field to the die immediately so we don't
13215 infinitely recurse when dealing with pointers to the structure
0963b4bd 13216 type within the structure itself. */
1c379e20 13217 set_die_type (die, type, cu);
c906108c 13218
7e314c57
JK
13219 /* set_die_type should be already done. */
13220 set_descriptive_type (type, die, cu);
13221
c767944b
DJ
13222 return type;
13223}
13224
13225/* Finish creating a structure or union type, including filling in
13226 its members and creating a symbol for it. */
13227
13228static void
13229process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13230{
13231 struct objfile *objfile = cu->objfile;
ca040673 13232 struct die_info *child_die;
c767944b
DJ
13233 struct type *type;
13234
13235 type = get_die_type (die, cu);
13236 if (type == NULL)
13237 type = read_structure_type (die, cu);
13238
e142c38c 13239 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13240 {
13241 struct field_info fi;
34eaf542 13242 VEC (symbolp) *template_args = NULL;
c767944b 13243 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13244
13245 memset (&fi, 0, sizeof (struct field_info));
13246
639d11d3 13247 child_die = die->child;
c906108c
SS
13248
13249 while (child_die && child_die->tag)
13250 {
a9a9bd0f
DC
13251 if (child_die->tag == DW_TAG_member
13252 || child_die->tag == DW_TAG_variable)
c906108c 13253 {
a9a9bd0f
DC
13254 /* NOTE: carlton/2002-11-05: A C++ static data member
13255 should be a DW_TAG_member that is a declaration, but
13256 all versions of G++ as of this writing (so through at
13257 least 3.2.1) incorrectly generate DW_TAG_variable
13258 tags for them instead. */
e7c27a73 13259 dwarf2_add_field (&fi, child_die, cu);
c906108c 13260 }
8713b1b1 13261 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13262 {
0963b4bd 13263 /* C++ member function. */
e7c27a73 13264 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
13265 }
13266 else if (child_die->tag == DW_TAG_inheritance)
13267 {
13268 /* C++ base class field. */
e7c27a73 13269 dwarf2_add_field (&fi, child_die, cu);
c906108c 13270 }
98751a41
JK
13271 else if (child_die->tag == DW_TAG_typedef)
13272 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13273 else if (child_die->tag == DW_TAG_template_type_param
13274 || child_die->tag == DW_TAG_template_value_param)
13275 {
13276 struct symbol *arg = new_symbol (child_die, NULL, cu);
13277
f1078f66
DJ
13278 if (arg != NULL)
13279 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13280 }
13281
c906108c
SS
13282 child_die = sibling_die (child_die);
13283 }
13284
34eaf542
TT
13285 /* Attach template arguments to type. */
13286 if (! VEC_empty (symbolp, template_args))
13287 {
13288 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13289 TYPE_N_TEMPLATE_ARGUMENTS (type)
13290 = VEC_length (symbolp, template_args);
13291 TYPE_TEMPLATE_ARGUMENTS (type)
13292 = obstack_alloc (&objfile->objfile_obstack,
13293 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13294 * sizeof (struct symbol *)));
13295 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13296 VEC_address (symbolp, template_args),
13297 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13298 * sizeof (struct symbol *)));
13299 VEC_free (symbolp, template_args);
13300 }
13301
c906108c
SS
13302 /* Attach fields and member functions to the type. */
13303 if (fi.nfields)
e7c27a73 13304 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13305 if (fi.nfnfields)
13306 {
e7c27a73 13307 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13308
c5aa993b 13309 /* Get the type which refers to the base class (possibly this
c906108c 13310 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13311 class from the DW_AT_containing_type attribute. This use of
13312 DW_AT_containing_type is a GNU extension. */
c906108c 13313
e142c38c 13314 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13315 {
e7c27a73 13316 struct type *t = die_containing_type (die, cu);
c906108c 13317
ae6ae975 13318 set_type_vptr_basetype (type, t);
c906108c
SS
13319 if (type == t)
13320 {
c906108c
SS
13321 int i;
13322
13323 /* Our own class provides vtbl ptr. */
13324 for (i = TYPE_NFIELDS (t) - 1;
13325 i >= TYPE_N_BASECLASSES (t);
13326 --i)
13327 {
0d5cff50 13328 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13329
1168df01 13330 if (is_vtable_name (fieldname, cu))
c906108c 13331 {
ae6ae975 13332 set_type_vptr_fieldno (type, i);
c906108c
SS
13333 break;
13334 }
13335 }
13336
13337 /* Complain if virtual function table field not found. */
13338 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13339 complaint (&symfile_complaints,
3e43a32a
MS
13340 _("virtual function table pointer "
13341 "not found when defining class '%s'"),
4d3c2250
KB
13342 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13343 "");
c906108c
SS
13344 }
13345 else
13346 {
ae6ae975 13347 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13348 }
13349 }
f6235d4c 13350 else if (cu->producer
61012eef 13351 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13352 {
13353 /* The IBM XLC compiler does not provide direct indication
13354 of the containing type, but the vtable pointer is
13355 always named __vfp. */
13356
13357 int i;
13358
13359 for (i = TYPE_NFIELDS (type) - 1;
13360 i >= TYPE_N_BASECLASSES (type);
13361 --i)
13362 {
13363 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13364 {
ae6ae975
DE
13365 set_type_vptr_fieldno (type, i);
13366 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13367 break;
13368 }
13369 }
13370 }
c906108c 13371 }
98751a41
JK
13372
13373 /* Copy fi.typedef_field_list linked list elements content into the
13374 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13375 if (fi.typedef_field_list)
13376 {
13377 int i = fi.typedef_field_list_count;
13378
a0d7a4ff 13379 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
13380 TYPE_TYPEDEF_FIELD_ARRAY (type)
13381 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
13382 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13383
13384 /* Reverse the list order to keep the debug info elements order. */
13385 while (--i >= 0)
13386 {
13387 struct typedef_field *dest, *src;
6e70227d 13388
98751a41
JK
13389 dest = &TYPE_TYPEDEF_FIELD (type, i);
13390 src = &fi.typedef_field_list->field;
13391 fi.typedef_field_list = fi.typedef_field_list->next;
13392 *dest = *src;
13393 }
13394 }
c767944b
DJ
13395
13396 do_cleanups (back_to);
eb2a6f42
TT
13397
13398 if (HAVE_CPLUS_STRUCT (type))
13399 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 13400 }
63d06c5c 13401
bb5ed363 13402 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13403
90aeadfc
DC
13404 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13405 snapshots) has been known to create a die giving a declaration
13406 for a class that has, as a child, a die giving a definition for a
13407 nested class. So we have to process our children even if the
13408 current die is a declaration. Normally, of course, a declaration
13409 won't have any children at all. */
134d01f1 13410
ca040673
DE
13411 child_die = die->child;
13412
90aeadfc
DC
13413 while (child_die != NULL && child_die->tag)
13414 {
13415 if (child_die->tag == DW_TAG_member
13416 || child_die->tag == DW_TAG_variable
34eaf542
TT
13417 || child_die->tag == DW_TAG_inheritance
13418 || child_die->tag == DW_TAG_template_value_param
13419 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13420 {
90aeadfc 13421 /* Do nothing. */
134d01f1 13422 }
90aeadfc
DC
13423 else
13424 process_die (child_die, cu);
134d01f1 13425
90aeadfc 13426 child_die = sibling_die (child_die);
134d01f1
DJ
13427 }
13428
fa4028e9
JB
13429 /* Do not consider external references. According to the DWARF standard,
13430 these DIEs are identified by the fact that they have no byte_size
13431 attribute, and a declaration attribute. */
13432 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13433 || !die_is_declaration (die, cu))
c767944b 13434 new_symbol (die, type, cu);
134d01f1
DJ
13435}
13436
55426c9d
JB
13437/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13438 update TYPE using some information only available in DIE's children. */
13439
13440static void
13441update_enumeration_type_from_children (struct die_info *die,
13442 struct type *type,
13443 struct dwarf2_cu *cu)
13444{
13445 struct obstack obstack;
60f7655a 13446 struct die_info *child_die;
55426c9d
JB
13447 int unsigned_enum = 1;
13448 int flag_enum = 1;
13449 ULONGEST mask = 0;
13450 struct cleanup *old_chain;
13451
13452 obstack_init (&obstack);
13453 old_chain = make_cleanup_obstack_free (&obstack);
13454
60f7655a
DE
13455 for (child_die = die->child;
13456 child_die != NULL && child_die->tag;
13457 child_die = sibling_die (child_die))
55426c9d
JB
13458 {
13459 struct attribute *attr;
13460 LONGEST value;
13461 const gdb_byte *bytes;
13462 struct dwarf2_locexpr_baton *baton;
13463 const char *name;
60f7655a 13464
55426c9d
JB
13465 if (child_die->tag != DW_TAG_enumerator)
13466 continue;
13467
13468 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13469 if (attr == NULL)
13470 continue;
13471
13472 name = dwarf2_name (child_die, cu);
13473 if (name == NULL)
13474 name = "<anonymous enumerator>";
13475
13476 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13477 &value, &bytes, &baton);
13478 if (value < 0)
13479 {
13480 unsigned_enum = 0;
13481 flag_enum = 0;
13482 }
13483 else if ((mask & value) != 0)
13484 flag_enum = 0;
13485 else
13486 mask |= value;
13487
13488 /* If we already know that the enum type is neither unsigned, nor
13489 a flag type, no need to look at the rest of the enumerates. */
13490 if (!unsigned_enum && !flag_enum)
13491 break;
55426c9d
JB
13492 }
13493
13494 if (unsigned_enum)
13495 TYPE_UNSIGNED (type) = 1;
13496 if (flag_enum)
13497 TYPE_FLAG_ENUM (type) = 1;
13498
13499 do_cleanups (old_chain);
13500}
13501
134d01f1
DJ
13502/* Given a DW_AT_enumeration_type die, set its type. We do not
13503 complete the type's fields yet, or create any symbols. */
c906108c 13504
f792889a 13505static struct type *
134d01f1 13506read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13507{
e7c27a73 13508 struct objfile *objfile = cu->objfile;
c906108c 13509 struct type *type;
c906108c 13510 struct attribute *attr;
0114d602 13511 const char *name;
134d01f1 13512
348e048f
DE
13513 /* If the definition of this type lives in .debug_types, read that type.
13514 Don't follow DW_AT_specification though, that will take us back up
13515 the chain and we want to go down. */
45e58e77 13516 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13517 if (attr)
13518 {
ac9ec31b 13519 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13520
ac9ec31b 13521 /* The type's CU may not be the same as CU.
02142a6c 13522 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13523 return set_die_type (die, type, cu);
13524 }
13525
c906108c
SS
13526 type = alloc_type (objfile);
13527
13528 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 13529 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 13530 if (name != NULL)
7d455152 13531 TYPE_TAG_NAME (type) = name;
c906108c 13532
0626fc76
TT
13533 attr = dwarf2_attr (die, DW_AT_type, cu);
13534 if (attr != NULL)
13535 {
13536 struct type *underlying_type = die_type (die, cu);
13537
13538 TYPE_TARGET_TYPE (type) = underlying_type;
13539 }
13540
e142c38c 13541 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13542 if (attr)
13543 {
13544 TYPE_LENGTH (type) = DW_UNSND (attr);
13545 }
13546 else
13547 {
13548 TYPE_LENGTH (type) = 0;
13549 }
13550
137033e9
JB
13551 /* The enumeration DIE can be incomplete. In Ada, any type can be
13552 declared as private in the package spec, and then defined only
13553 inside the package body. Such types are known as Taft Amendment
13554 Types. When another package uses such a type, an incomplete DIE
13555 may be generated by the compiler. */
02eb380e 13556 if (die_is_declaration (die, cu))
876cecd0 13557 TYPE_STUB (type) = 1;
02eb380e 13558
0626fc76
TT
13559 /* Finish the creation of this type by using the enum's children.
13560 We must call this even when the underlying type has been provided
13561 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
13562 update_enumeration_type_from_children (die, type, cu);
13563
0626fc76
TT
13564 /* If this type has an underlying type that is not a stub, then we
13565 may use its attributes. We always use the "unsigned" attribute
13566 in this situation, because ordinarily we guess whether the type
13567 is unsigned -- but the guess can be wrong and the underlying type
13568 can tell us the reality. However, we defer to a local size
13569 attribute if one exists, because this lets the compiler override
13570 the underlying type if needed. */
13571 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13572 {
13573 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13574 if (TYPE_LENGTH (type) == 0)
13575 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13576 }
13577
3d567982
TT
13578 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13579
f792889a 13580 return set_die_type (die, type, cu);
134d01f1
DJ
13581}
13582
13583/* Given a pointer to a die which begins an enumeration, process all
13584 the dies that define the members of the enumeration, and create the
13585 symbol for the enumeration type.
13586
13587 NOTE: We reverse the order of the element list. */
13588
13589static void
13590process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13591{
f792889a 13592 struct type *this_type;
134d01f1 13593
f792889a
DJ
13594 this_type = get_die_type (die, cu);
13595 if (this_type == NULL)
13596 this_type = read_enumeration_type (die, cu);
9dc481d3 13597
639d11d3 13598 if (die->child != NULL)
c906108c 13599 {
9dc481d3
DE
13600 struct die_info *child_die;
13601 struct symbol *sym;
13602 struct field *fields = NULL;
13603 int num_fields = 0;
15d034d0 13604 const char *name;
9dc481d3 13605
639d11d3 13606 child_die = die->child;
c906108c
SS
13607 while (child_die && child_die->tag)
13608 {
13609 if (child_die->tag != DW_TAG_enumerator)
13610 {
e7c27a73 13611 process_die (child_die, cu);
c906108c
SS
13612 }
13613 else
13614 {
39cbfefa
DJ
13615 name = dwarf2_name (child_die, cu);
13616 if (name)
c906108c 13617 {
f792889a 13618 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
13619
13620 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13621 {
13622 fields = (struct field *)
13623 xrealloc (fields,
13624 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13625 * sizeof (struct field));
c906108c
SS
13626 }
13627
3567439c 13628 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13629 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13630 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
13631 FIELD_BITSIZE (fields[num_fields]) = 0;
13632
13633 num_fields++;
13634 }
13635 }
13636
13637 child_die = sibling_die (child_die);
13638 }
13639
13640 if (num_fields)
13641 {
f792889a
DJ
13642 TYPE_NFIELDS (this_type) = num_fields;
13643 TYPE_FIELDS (this_type) = (struct field *)
13644 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13645 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 13646 sizeof (struct field) * num_fields);
b8c9b27d 13647 xfree (fields);
c906108c 13648 }
c906108c 13649 }
134d01f1 13650
6c83ed52
TT
13651 /* If we are reading an enum from a .debug_types unit, and the enum
13652 is a declaration, and the enum is not the signatured type in the
13653 unit, then we do not want to add a symbol for it. Adding a
13654 symbol would in some cases obscure the true definition of the
13655 enum, giving users an incomplete type when the definition is
13656 actually available. Note that we do not want to do this for all
13657 enums which are just declarations, because C++0x allows forward
13658 enum declarations. */
3019eac3 13659 if (cu->per_cu->is_debug_types
6c83ed52
TT
13660 && die_is_declaration (die, cu))
13661 {
52dc124a 13662 struct signatured_type *sig_type;
6c83ed52 13663
c0f78cd4 13664 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
13665 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13666 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
13667 return;
13668 }
13669
f792889a 13670 new_symbol (die, this_type, cu);
c906108c
SS
13671}
13672
13673/* Extract all information from a DW_TAG_array_type DIE and put it in
13674 the DIE's type field. For now, this only handles one dimensional
13675 arrays. */
13676
f792889a 13677static struct type *
e7c27a73 13678read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13679{
e7c27a73 13680 struct objfile *objfile = cu->objfile;
c906108c 13681 struct die_info *child_die;
7e314c57 13682 struct type *type;
c906108c
SS
13683 struct type *element_type, *range_type, *index_type;
13684 struct type **range_types = NULL;
13685 struct attribute *attr;
13686 int ndim = 0;
13687 struct cleanup *back_to;
15d034d0 13688 const char *name;
dc53a7ad 13689 unsigned int bit_stride = 0;
c906108c 13690
e7c27a73 13691 element_type = die_type (die, cu);
c906108c 13692
7e314c57
JK
13693 /* The die_type call above may have already set the type for this DIE. */
13694 type = get_die_type (die, cu);
13695 if (type)
13696 return type;
13697
dc53a7ad
JB
13698 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13699 if (attr != NULL)
13700 bit_stride = DW_UNSND (attr) * 8;
13701
13702 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13703 if (attr != NULL)
13704 bit_stride = DW_UNSND (attr);
13705
c906108c
SS
13706 /* Irix 6.2 native cc creates array types without children for
13707 arrays with unspecified length. */
639d11d3 13708 if (die->child == NULL)
c906108c 13709 {
46bf5051 13710 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 13711 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
13712 type = create_array_type_with_stride (NULL, element_type, range_type,
13713 bit_stride);
f792889a 13714 return set_die_type (die, type, cu);
c906108c
SS
13715 }
13716
13717 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 13718 child_die = die->child;
c906108c
SS
13719 while (child_die && child_die->tag)
13720 {
13721 if (child_die->tag == DW_TAG_subrange_type)
13722 {
f792889a 13723 struct type *child_type = read_type_die (child_die, cu);
9a619af0 13724
f792889a 13725 if (child_type != NULL)
a02abb62 13726 {
0963b4bd
MS
13727 /* The range type was succesfully read. Save it for the
13728 array type creation. */
a02abb62
JB
13729 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
13730 {
13731 range_types = (struct type **)
13732 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
13733 * sizeof (struct type *));
13734 if (ndim == 0)
13735 make_cleanup (free_current_contents, &range_types);
13736 }
f792889a 13737 range_types[ndim++] = child_type;
a02abb62 13738 }
c906108c
SS
13739 }
13740 child_die = sibling_die (child_die);
13741 }
13742
13743 /* Dwarf2 dimensions are output from left to right, create the
13744 necessary array types in backwards order. */
7ca2d3a3 13745
c906108c 13746 type = element_type;
7ca2d3a3
DL
13747
13748 if (read_array_order (die, cu) == DW_ORD_col_major)
13749 {
13750 int i = 0;
9a619af0 13751
7ca2d3a3 13752 while (i < ndim)
dc53a7ad
JB
13753 type = create_array_type_with_stride (NULL, type, range_types[i++],
13754 bit_stride);
7ca2d3a3
DL
13755 }
13756 else
13757 {
13758 while (ndim-- > 0)
dc53a7ad
JB
13759 type = create_array_type_with_stride (NULL, type, range_types[ndim],
13760 bit_stride);
7ca2d3a3 13761 }
c906108c 13762
f5f8a009
EZ
13763 /* Understand Dwarf2 support for vector types (like they occur on
13764 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13765 array type. This is not part of the Dwarf2/3 standard yet, but a
13766 custom vendor extension. The main difference between a regular
13767 array and the vector variant is that vectors are passed by value
13768 to functions. */
e142c38c 13769 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 13770 if (attr)
ea37ba09 13771 make_vector_type (type);
f5f8a009 13772
dbc98a8b
KW
13773 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13774 implementation may choose to implement triple vectors using this
13775 attribute. */
13776 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13777 if (attr)
13778 {
13779 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
13780 TYPE_LENGTH (type) = DW_UNSND (attr);
13781 else
3e43a32a
MS
13782 complaint (&symfile_complaints,
13783 _("DW_AT_byte_size for array type smaller "
13784 "than the total size of elements"));
dbc98a8b
KW
13785 }
13786
39cbfefa
DJ
13787 name = dwarf2_name (die, cu);
13788 if (name)
13789 TYPE_NAME (type) = name;
6e70227d 13790
0963b4bd 13791 /* Install the type in the die. */
7e314c57
JK
13792 set_die_type (die, type, cu);
13793
13794 /* set_die_type should be already done. */
b4ba55a1
JB
13795 set_descriptive_type (type, die, cu);
13796
c906108c
SS
13797 do_cleanups (back_to);
13798
7e314c57 13799 return type;
c906108c
SS
13800}
13801
7ca2d3a3 13802static enum dwarf_array_dim_ordering
6e70227d 13803read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
13804{
13805 struct attribute *attr;
13806
13807 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13808
aead7601
SM
13809 if (attr)
13810 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 13811
0963b4bd
MS
13812 /* GNU F77 is a special case, as at 08/2004 array type info is the
13813 opposite order to the dwarf2 specification, but data is still
13814 laid out as per normal fortran.
7ca2d3a3 13815
0963b4bd
MS
13816 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13817 version checking. */
7ca2d3a3 13818
905e0470
PM
13819 if (cu->language == language_fortran
13820 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
13821 {
13822 return DW_ORD_row_major;
13823 }
13824
6e70227d 13825 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
13826 {
13827 case array_column_major:
13828 return DW_ORD_col_major;
13829 case array_row_major:
13830 default:
13831 return DW_ORD_row_major;
13832 };
13833}
13834
72019c9c 13835/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 13836 the DIE's type field. */
72019c9c 13837
f792889a 13838static struct type *
72019c9c
GM
13839read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13840{
7e314c57
JK
13841 struct type *domain_type, *set_type;
13842 struct attribute *attr;
f792889a 13843
7e314c57
JK
13844 domain_type = die_type (die, cu);
13845
13846 /* The die_type call above may have already set the type for this DIE. */
13847 set_type = get_die_type (die, cu);
13848 if (set_type)
13849 return set_type;
13850
13851 set_type = create_set_type (NULL, domain_type);
13852
13853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
13854 if (attr)
13855 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 13856
f792889a 13857 return set_die_type (die, set_type, cu);
72019c9c 13858}
7ca2d3a3 13859
0971de02
TT
13860/* A helper for read_common_block that creates a locexpr baton.
13861 SYM is the symbol which we are marking as computed.
13862 COMMON_DIE is the DIE for the common block.
13863 COMMON_LOC is the location expression attribute for the common
13864 block itself.
13865 MEMBER_LOC is the location expression attribute for the particular
13866 member of the common block that we are processing.
13867 CU is the CU from which the above come. */
13868
13869static void
13870mark_common_block_symbol_computed (struct symbol *sym,
13871 struct die_info *common_die,
13872 struct attribute *common_loc,
13873 struct attribute *member_loc,
13874 struct dwarf2_cu *cu)
13875{
13876 struct objfile *objfile = dwarf2_per_objfile->objfile;
13877 struct dwarf2_locexpr_baton *baton;
13878 gdb_byte *ptr;
13879 unsigned int cu_off;
13880 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
13881 LONGEST offset = 0;
13882
13883 gdb_assert (common_loc && member_loc);
13884 gdb_assert (attr_form_is_block (common_loc));
13885 gdb_assert (attr_form_is_block (member_loc)
13886 || attr_form_is_constant (member_loc));
13887
13888 baton = obstack_alloc (&objfile->objfile_obstack,
13889 sizeof (struct dwarf2_locexpr_baton));
13890 baton->per_cu = cu->per_cu;
13891 gdb_assert (baton->per_cu);
13892
13893 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13894
13895 if (attr_form_is_constant (member_loc))
13896 {
13897 offset = dwarf2_get_attr_constant_value (member_loc, 0);
13898 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13899 }
13900 else
13901 baton->size += DW_BLOCK (member_loc)->size;
13902
13903 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
13904 baton->data = ptr;
13905
13906 *ptr++ = DW_OP_call4;
13907 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
13908 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13909 ptr += 4;
13910
13911 if (attr_form_is_constant (member_loc))
13912 {
13913 *ptr++ = DW_OP_addr;
13914 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13915 ptr += cu->header.addr_size;
13916 }
13917 else
13918 {
13919 /* We have to copy the data here, because DW_OP_call4 will only
13920 use a DW_AT_location attribute. */
13921 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
13922 ptr += DW_BLOCK (member_loc)->size;
13923 }
13924
13925 *ptr++ = DW_OP_plus;
13926 gdb_assert (ptr - baton->data == baton->size);
13927
0971de02 13928 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 13929 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
13930}
13931
4357ac6c
TT
13932/* Create appropriate locally-scoped variables for all the
13933 DW_TAG_common_block entries. Also create a struct common_block
13934 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13935 is used to sepate the common blocks name namespace from regular
13936 variable names. */
c906108c
SS
13937
13938static void
e7c27a73 13939read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13940{
0971de02
TT
13941 struct attribute *attr;
13942
13943 attr = dwarf2_attr (die, DW_AT_location, cu);
13944 if (attr)
13945 {
13946 /* Support the .debug_loc offsets. */
13947 if (attr_form_is_block (attr))
13948 {
13949 /* Ok. */
13950 }
13951 else if (attr_form_is_section_offset (attr))
13952 {
13953 dwarf2_complex_location_expr_complaint ();
13954 attr = NULL;
13955 }
13956 else
13957 {
13958 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13959 "common block member");
13960 attr = NULL;
13961 }
13962 }
13963
639d11d3 13964 if (die->child != NULL)
c906108c 13965 {
4357ac6c
TT
13966 struct objfile *objfile = cu->objfile;
13967 struct die_info *child_die;
13968 size_t n_entries = 0, size;
13969 struct common_block *common_block;
13970 struct symbol *sym;
74ac6d43 13971
4357ac6c
TT
13972 for (child_die = die->child;
13973 child_die && child_die->tag;
13974 child_die = sibling_die (child_die))
13975 ++n_entries;
13976
13977 size = (sizeof (struct common_block)
13978 + (n_entries - 1) * sizeof (struct symbol *));
13979 common_block = obstack_alloc (&objfile->objfile_obstack, size);
13980 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13981 common_block->n_entries = 0;
13982
13983 for (child_die = die->child;
13984 child_die && child_die->tag;
13985 child_die = sibling_die (child_die))
13986 {
13987 /* Create the symbol in the DW_TAG_common_block block in the current
13988 symbol scope. */
e7c27a73 13989 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
13990 if (sym != NULL)
13991 {
13992 struct attribute *member_loc;
13993
13994 common_block->contents[common_block->n_entries++] = sym;
13995
13996 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13997 cu);
13998 if (member_loc)
13999 {
14000 /* GDB has handled this for a long time, but it is
14001 not specified by DWARF. It seems to have been
14002 emitted by gfortran at least as recently as:
14003 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14004 complaint (&symfile_complaints,
14005 _("Variable in common block has "
14006 "DW_AT_data_member_location "
14007 "- DIE at 0x%x [in module %s]"),
4262abfb
JK
14008 child_die->offset.sect_off,
14009 objfile_name (cu->objfile));
0971de02
TT
14010
14011 if (attr_form_is_section_offset (member_loc))
14012 dwarf2_complex_location_expr_complaint ();
14013 else if (attr_form_is_constant (member_loc)
14014 || attr_form_is_block (member_loc))
14015 {
14016 if (attr)
14017 mark_common_block_symbol_computed (sym, die, attr,
14018 member_loc, cu);
14019 }
14020 else
14021 dwarf2_complex_location_expr_complaint ();
14022 }
14023 }
c906108c 14024 }
4357ac6c
TT
14025
14026 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14027 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14028 }
14029}
14030
0114d602 14031/* Create a type for a C++ namespace. */
d9fa45fe 14032
0114d602
DJ
14033static struct type *
14034read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14035{
e7c27a73 14036 struct objfile *objfile = cu->objfile;
0114d602 14037 const char *previous_prefix, *name;
9219021c 14038 int is_anonymous;
0114d602
DJ
14039 struct type *type;
14040
14041 /* For extensions, reuse the type of the original namespace. */
14042 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14043 {
14044 struct die_info *ext_die;
14045 struct dwarf2_cu *ext_cu = cu;
9a619af0 14046
0114d602
DJ
14047 ext_die = dwarf2_extension (die, &ext_cu);
14048 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14049
14050 /* EXT_CU may not be the same as CU.
02142a6c 14051 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14052 return set_die_type (die, type, cu);
14053 }
9219021c 14054
e142c38c 14055 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14056
14057 /* Now build the name of the current namespace. */
14058
0114d602
DJ
14059 previous_prefix = determine_prefix (die, cu);
14060 if (previous_prefix[0] != '\0')
14061 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14062 previous_prefix, name, 0, cu);
0114d602
DJ
14063
14064 /* Create the type. */
14065 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
14066 objfile);
abee88f2 14067 TYPE_NAME (type) = name;
0114d602
DJ
14068 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14069
60531b24 14070 return set_die_type (die, type, cu);
0114d602
DJ
14071}
14072
22cee43f 14073/* Read a namespace scope. */
0114d602
DJ
14074
14075static void
14076read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14077{
14078 struct objfile *objfile = cu->objfile;
0114d602 14079 int is_anonymous;
9219021c 14080
5c4e30ca
DC
14081 /* Add a symbol associated to this if we haven't seen the namespace
14082 before. Also, add a using directive if it's an anonymous
14083 namespace. */
9219021c 14084
f2f0e013 14085 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14086 {
14087 struct type *type;
14088
0114d602 14089 type = read_type_die (die, cu);
e7c27a73 14090 new_symbol (die, type, cu);
5c4e30ca 14091
e8e80198 14092 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14093 if (is_anonymous)
0114d602
DJ
14094 {
14095 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14096
22cee43f
PMR
14097 add_using_directive (using_directives (cu->language),
14098 previous_prefix, TYPE_NAME (type), NULL,
14099 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14100 }
5c4e30ca 14101 }
9219021c 14102
639d11d3 14103 if (die->child != NULL)
d9fa45fe 14104 {
639d11d3 14105 struct die_info *child_die = die->child;
6e70227d 14106
d9fa45fe
DC
14107 while (child_die && child_die->tag)
14108 {
e7c27a73 14109 process_die (child_die, cu);
d9fa45fe
DC
14110 child_die = sibling_die (child_die);
14111 }
14112 }
38d518c9
EZ
14113}
14114
f55ee35c
JK
14115/* Read a Fortran module as type. This DIE can be only a declaration used for
14116 imported module. Still we need that type as local Fortran "use ... only"
14117 declaration imports depend on the created type in determine_prefix. */
14118
14119static struct type *
14120read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14121{
14122 struct objfile *objfile = cu->objfile;
15d034d0 14123 const char *module_name;
f55ee35c
JK
14124 struct type *type;
14125
14126 module_name = dwarf2_name (die, cu);
14127 if (!module_name)
3e43a32a
MS
14128 complaint (&symfile_complaints,
14129 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 14130 die->offset.sect_off);
f55ee35c
JK
14131 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
14132
14133 /* determine_prefix uses TYPE_TAG_NAME. */
14134 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14135
14136 return set_die_type (die, type, cu);
14137}
14138
5d7cb8df
JK
14139/* Read a Fortran module. */
14140
14141static void
14142read_module (struct die_info *die, struct dwarf2_cu *cu)
14143{
14144 struct die_info *child_die = die->child;
530e8392
KB
14145 struct type *type;
14146
14147 type = read_type_die (die, cu);
14148 new_symbol (die, type, cu);
5d7cb8df 14149
5d7cb8df
JK
14150 while (child_die && child_die->tag)
14151 {
14152 process_die (child_die, cu);
14153 child_die = sibling_die (child_die);
14154 }
14155}
14156
38d518c9
EZ
14157/* Return the name of the namespace represented by DIE. Set
14158 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14159 namespace. */
14160
14161static const char *
e142c38c 14162namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14163{
14164 struct die_info *current_die;
14165 const char *name = NULL;
14166
14167 /* Loop through the extensions until we find a name. */
14168
14169 for (current_die = die;
14170 current_die != NULL;
f2f0e013 14171 current_die = dwarf2_extension (die, &cu))
38d518c9 14172 {
96553a0c
DE
14173 /* We don't use dwarf2_name here so that we can detect the absence
14174 of a name -> anonymous namespace. */
7d45c7c3 14175 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14176
38d518c9
EZ
14177 if (name != NULL)
14178 break;
14179 }
14180
14181 /* Is it an anonymous namespace? */
14182
14183 *is_anonymous = (name == NULL);
14184 if (*is_anonymous)
2b1dbab0 14185 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14186
14187 return name;
d9fa45fe
DC
14188}
14189
c906108c
SS
14190/* Extract all information from a DW_TAG_pointer_type DIE and add to
14191 the user defined type vector. */
14192
f792889a 14193static struct type *
e7c27a73 14194read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14195{
5e2b427d 14196 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14197 struct comp_unit_head *cu_header = &cu->header;
c906108c 14198 struct type *type;
8b2dbe47
KB
14199 struct attribute *attr_byte_size;
14200 struct attribute *attr_address_class;
14201 int byte_size, addr_class;
7e314c57
JK
14202 struct type *target_type;
14203
14204 target_type = die_type (die, cu);
c906108c 14205
7e314c57
JK
14206 /* The die_type call above may have already set the type for this DIE. */
14207 type = get_die_type (die, cu);
14208 if (type)
14209 return type;
14210
14211 type = lookup_pointer_type (target_type);
8b2dbe47 14212
e142c38c 14213 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14214 if (attr_byte_size)
14215 byte_size = DW_UNSND (attr_byte_size);
c906108c 14216 else
8b2dbe47
KB
14217 byte_size = cu_header->addr_size;
14218
e142c38c 14219 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14220 if (attr_address_class)
14221 addr_class = DW_UNSND (attr_address_class);
14222 else
14223 addr_class = DW_ADDR_none;
14224
14225 /* If the pointer size or address class is different than the
14226 default, create a type variant marked as such and set the
14227 length accordingly. */
14228 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14229 {
5e2b427d 14230 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14231 {
14232 int type_flags;
14233
849957d9 14234 type_flags = gdbarch_address_class_type_flags
5e2b427d 14235 (gdbarch, byte_size, addr_class);
876cecd0
TT
14236 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14237 == 0);
8b2dbe47
KB
14238 type = make_type_with_address_space (type, type_flags);
14239 }
14240 else if (TYPE_LENGTH (type) != byte_size)
14241 {
3e43a32a
MS
14242 complaint (&symfile_complaints,
14243 _("invalid pointer size %d"), byte_size);
8b2dbe47 14244 }
6e70227d 14245 else
9a619af0
MS
14246 {
14247 /* Should we also complain about unhandled address classes? */
14248 }
c906108c 14249 }
8b2dbe47
KB
14250
14251 TYPE_LENGTH (type) = byte_size;
f792889a 14252 return set_die_type (die, type, cu);
c906108c
SS
14253}
14254
14255/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14256 the user defined type vector. */
14257
f792889a 14258static struct type *
e7c27a73 14259read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14260{
14261 struct type *type;
14262 struct type *to_type;
14263 struct type *domain;
14264
e7c27a73
DJ
14265 to_type = die_type (die, cu);
14266 domain = die_containing_type (die, cu);
0d5de010 14267
7e314c57
JK
14268 /* The calls above may have already set the type for this DIE. */
14269 type = get_die_type (die, cu);
14270 if (type)
14271 return type;
14272
0d5de010
DJ
14273 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14274 type = lookup_methodptr_type (to_type);
7078baeb
TT
14275 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14276 {
14277 struct type *new_type = alloc_type (cu->objfile);
14278
14279 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14280 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14281 TYPE_VARARGS (to_type));
14282 type = lookup_methodptr_type (new_type);
14283 }
0d5de010
DJ
14284 else
14285 type = lookup_memberptr_type (to_type, domain);
c906108c 14286
f792889a 14287 return set_die_type (die, type, cu);
c906108c
SS
14288}
14289
14290/* Extract all information from a DW_TAG_reference_type DIE and add to
14291 the user defined type vector. */
14292
f792889a 14293static struct type *
e7c27a73 14294read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14295{
e7c27a73 14296 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14297 struct type *type, *target_type;
c906108c
SS
14298 struct attribute *attr;
14299
7e314c57
JK
14300 target_type = die_type (die, cu);
14301
14302 /* The die_type call above may have already set the type for this DIE. */
14303 type = get_die_type (die, cu);
14304 if (type)
14305 return type;
14306
14307 type = lookup_reference_type (target_type);
e142c38c 14308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14309 if (attr)
14310 {
14311 TYPE_LENGTH (type) = DW_UNSND (attr);
14312 }
14313 else
14314 {
107d2387 14315 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14316 }
f792889a 14317 return set_die_type (die, type, cu);
c906108c
SS
14318}
14319
cf363f18
MW
14320/* Add the given cv-qualifiers to the element type of the array. GCC
14321 outputs DWARF type qualifiers that apply to an array, not the
14322 element type. But GDB relies on the array element type to carry
14323 the cv-qualifiers. This mimics section 6.7.3 of the C99
14324 specification. */
14325
14326static struct type *
14327add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14328 struct type *base_type, int cnst, int voltl)
14329{
14330 struct type *el_type, *inner_array;
14331
14332 base_type = copy_type (base_type);
14333 inner_array = base_type;
14334
14335 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14336 {
14337 TYPE_TARGET_TYPE (inner_array) =
14338 copy_type (TYPE_TARGET_TYPE (inner_array));
14339 inner_array = TYPE_TARGET_TYPE (inner_array);
14340 }
14341
14342 el_type = TYPE_TARGET_TYPE (inner_array);
14343 cnst |= TYPE_CONST (el_type);
14344 voltl |= TYPE_VOLATILE (el_type);
14345 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14346
14347 return set_die_type (die, base_type, cu);
14348}
14349
f792889a 14350static struct type *
e7c27a73 14351read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14352{
f792889a 14353 struct type *base_type, *cv_type;
c906108c 14354
e7c27a73 14355 base_type = die_type (die, cu);
7e314c57
JK
14356
14357 /* The die_type call above may have already set the type for this DIE. */
14358 cv_type = get_die_type (die, cu);
14359 if (cv_type)
14360 return cv_type;
14361
2f608a3a
KW
14362 /* In case the const qualifier is applied to an array type, the element type
14363 is so qualified, not the array type (section 6.7.3 of C99). */
14364 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14365 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14366
f792889a
DJ
14367 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14368 return set_die_type (die, cv_type, cu);
c906108c
SS
14369}
14370
f792889a 14371static struct type *
e7c27a73 14372read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14373{
f792889a 14374 struct type *base_type, *cv_type;
c906108c 14375
e7c27a73 14376 base_type = die_type (die, cu);
7e314c57
JK
14377
14378 /* The die_type call above may have already set the type for this DIE. */
14379 cv_type = get_die_type (die, cu);
14380 if (cv_type)
14381 return cv_type;
14382
cf363f18
MW
14383 /* In case the volatile qualifier is applied to an array type, the
14384 element type is so qualified, not the array type (section 6.7.3
14385 of C99). */
14386 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14387 return add_array_cv_type (die, cu, base_type, 0, 1);
14388
f792889a
DJ
14389 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14390 return set_die_type (die, cv_type, cu);
c906108c
SS
14391}
14392
06d66ee9
TT
14393/* Handle DW_TAG_restrict_type. */
14394
14395static struct type *
14396read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14397{
14398 struct type *base_type, *cv_type;
14399
14400 base_type = die_type (die, cu);
14401
14402 /* The die_type call above may have already set the type for this DIE. */
14403 cv_type = get_die_type (die, cu);
14404 if (cv_type)
14405 return cv_type;
14406
14407 cv_type = make_restrict_type (base_type);
14408 return set_die_type (die, cv_type, cu);
14409}
14410
a2c2acaf
MW
14411/* Handle DW_TAG_atomic_type. */
14412
14413static struct type *
14414read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14415{
14416 struct type *base_type, *cv_type;
14417
14418 base_type = die_type (die, cu);
14419
14420 /* The die_type call above may have already set the type for this DIE. */
14421 cv_type = get_die_type (die, cu);
14422 if (cv_type)
14423 return cv_type;
14424
14425 cv_type = make_atomic_type (base_type);
14426 return set_die_type (die, cv_type, cu);
14427}
14428
c906108c
SS
14429/* Extract all information from a DW_TAG_string_type DIE and add to
14430 the user defined type vector. It isn't really a user defined type,
14431 but it behaves like one, with other DIE's using an AT_user_def_type
14432 attribute to reference it. */
14433
f792889a 14434static struct type *
e7c27a73 14435read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14436{
e7c27a73 14437 struct objfile *objfile = cu->objfile;
3b7538c0 14438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14439 struct type *type, *range_type, *index_type, *char_type;
14440 struct attribute *attr;
14441 unsigned int length;
14442
e142c38c 14443 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14444 if (attr)
14445 {
14446 length = DW_UNSND (attr);
14447 }
14448 else
14449 {
0963b4bd 14450 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14451 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14452 if (attr)
14453 {
14454 length = DW_UNSND (attr);
14455 }
14456 else
14457 {
14458 length = 1;
14459 }
c906108c 14460 }
6ccb9162 14461
46bf5051 14462 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14463 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14464 char_type = language_string_char_type (cu->language_defn, gdbarch);
14465 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14466
f792889a 14467 return set_die_type (die, type, cu);
c906108c
SS
14468}
14469
4d804846
JB
14470/* Assuming that DIE corresponds to a function, returns nonzero
14471 if the function is prototyped. */
14472
14473static int
14474prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14475{
14476 struct attribute *attr;
14477
14478 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14479 if (attr && (DW_UNSND (attr) != 0))
14480 return 1;
14481
14482 /* The DWARF standard implies that the DW_AT_prototyped attribute
14483 is only meaninful for C, but the concept also extends to other
14484 languages that allow unprototyped functions (Eg: Objective C).
14485 For all other languages, assume that functions are always
14486 prototyped. */
14487 if (cu->language != language_c
14488 && cu->language != language_objc
14489 && cu->language != language_opencl)
14490 return 1;
14491
14492 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14493 prototyped and unprototyped functions; default to prototyped,
14494 since that is more common in modern code (and RealView warns
14495 about unprototyped functions). */
14496 if (producer_is_realview (cu->producer))
14497 return 1;
14498
14499 return 0;
14500}
14501
c906108c
SS
14502/* Handle DIES due to C code like:
14503
14504 struct foo
c5aa993b
JM
14505 {
14506 int (*funcp)(int a, long l);
14507 int b;
14508 };
c906108c 14509
0963b4bd 14510 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14511
f792889a 14512static struct type *
e7c27a73 14513read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14514{
bb5ed363 14515 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14516 struct type *type; /* Type that this function returns. */
14517 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14518 struct attribute *attr;
14519
e7c27a73 14520 type = die_type (die, cu);
7e314c57
JK
14521
14522 /* The die_type call above may have already set the type for this DIE. */
14523 ftype = get_die_type (die, cu);
14524 if (ftype)
14525 return ftype;
14526
0c8b41f1 14527 ftype = lookup_function_type (type);
c906108c 14528
4d804846 14529 if (prototyped_function_p (die, cu))
a6c727b2 14530 TYPE_PROTOTYPED (ftype) = 1;
c906108c 14531
c055b101
CV
14532 /* Store the calling convention in the type if it's available in
14533 the subroutine die. Otherwise set the calling convention to
14534 the default value DW_CC_normal. */
14535 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
14536 if (attr)
14537 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14538 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14539 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14540 else
14541 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 14542
743649fd
MW
14543 /* Record whether the function returns normally to its caller or not
14544 if the DWARF producer set that information. */
14545 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14546 if (attr && (DW_UNSND (attr) != 0))
14547 TYPE_NO_RETURN (ftype) = 1;
14548
76c10ea2
GM
14549 /* We need to add the subroutine type to the die immediately so
14550 we don't infinitely recurse when dealing with parameters
0963b4bd 14551 declared as the same subroutine type. */
76c10ea2 14552 set_die_type (die, ftype, cu);
6e70227d 14553
639d11d3 14554 if (die->child != NULL)
c906108c 14555 {
bb5ed363 14556 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 14557 struct die_info *child_die;
8072405b 14558 int nparams, iparams;
c906108c
SS
14559
14560 /* Count the number of parameters.
14561 FIXME: GDB currently ignores vararg functions, but knows about
14562 vararg member functions. */
8072405b 14563 nparams = 0;
639d11d3 14564 child_die = die->child;
c906108c
SS
14565 while (child_die && child_die->tag)
14566 {
14567 if (child_die->tag == DW_TAG_formal_parameter)
14568 nparams++;
14569 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 14570 TYPE_VARARGS (ftype) = 1;
c906108c
SS
14571 child_die = sibling_die (child_die);
14572 }
14573
14574 /* Allocate storage for parameters and fill them in. */
14575 TYPE_NFIELDS (ftype) = nparams;
14576 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 14577 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 14578
8072405b
JK
14579 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14580 even if we error out during the parameters reading below. */
14581 for (iparams = 0; iparams < nparams; iparams++)
14582 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14583
14584 iparams = 0;
639d11d3 14585 child_die = die->child;
c906108c
SS
14586 while (child_die && child_die->tag)
14587 {
14588 if (child_die->tag == DW_TAG_formal_parameter)
14589 {
3ce3b1ba
PA
14590 struct type *arg_type;
14591
14592 /* DWARF version 2 has no clean way to discern C++
14593 static and non-static member functions. G++ helps
14594 GDB by marking the first parameter for non-static
14595 member functions (which is the this pointer) as
14596 artificial. We pass this information to
14597 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14598
14599 DWARF version 3 added DW_AT_object_pointer, which GCC
14600 4.5 does not yet generate. */
e142c38c 14601 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
14602 if (attr)
14603 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14604 else
418835cc
KS
14605 {
14606 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14607
14608 /* GCC/43521: In java, the formal parameter
14609 "this" is sometimes not marked with DW_AT_artificial. */
14610 if (cu->language == language_java)
14611 {
14612 const char *name = dwarf2_name (child_die, cu);
9a619af0 14613
418835cc
KS
14614 if (name && !strcmp (name, "this"))
14615 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
14616 }
14617 }
3ce3b1ba
PA
14618 arg_type = die_type (child_die, cu);
14619
14620 /* RealView does not mark THIS as const, which the testsuite
14621 expects. GCC marks THIS as const in method definitions,
14622 but not in the class specifications (GCC PR 43053). */
14623 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14624 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14625 {
14626 int is_this = 0;
14627 struct dwarf2_cu *arg_cu = cu;
14628 const char *name = dwarf2_name (child_die, cu);
14629
14630 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14631 if (attr)
14632 {
14633 /* If the compiler emits this, use it. */
14634 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14635 is_this = 1;
14636 }
14637 else if (name && strcmp (name, "this") == 0)
14638 /* Function definitions will have the argument names. */
14639 is_this = 1;
14640 else if (name == NULL && iparams == 0)
14641 /* Declarations may not have the names, so like
14642 elsewhere in GDB, assume an artificial first
14643 argument is "this". */
14644 is_this = 1;
14645
14646 if (is_this)
14647 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14648 arg_type, 0);
14649 }
14650
14651 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
14652 iparams++;
14653 }
14654 child_die = sibling_die (child_die);
14655 }
14656 }
14657
76c10ea2 14658 return ftype;
c906108c
SS
14659}
14660
f792889a 14661static struct type *
e7c27a73 14662read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14663{
e7c27a73 14664 struct objfile *objfile = cu->objfile;
0114d602 14665 const char *name = NULL;
3c8e0968 14666 struct type *this_type, *target_type;
c906108c 14667
94af9270 14668 name = dwarf2_full_name (NULL, die, cu);
f792889a 14669 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 14670 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 14671 TYPE_NAME (this_type) = name;
f792889a 14672 set_die_type (die, this_type, cu);
3c8e0968
DE
14673 target_type = die_type (die, cu);
14674 if (target_type != this_type)
14675 TYPE_TARGET_TYPE (this_type) = target_type;
14676 else
14677 {
14678 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14679 spec and cause infinite loops in GDB. */
14680 complaint (&symfile_complaints,
14681 _("Self-referential DW_TAG_typedef "
14682 "- DIE at 0x%x [in module %s]"),
4262abfb 14683 die->offset.sect_off, objfile_name (objfile));
3c8e0968
DE
14684 TYPE_TARGET_TYPE (this_type) = NULL;
14685 }
f792889a 14686 return this_type;
c906108c
SS
14687}
14688
14689/* Find a representation of a given base type and install
14690 it in the TYPE field of the die. */
14691
f792889a 14692static struct type *
e7c27a73 14693read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14694{
e7c27a73 14695 struct objfile *objfile = cu->objfile;
c906108c
SS
14696 struct type *type;
14697 struct attribute *attr;
14698 int encoding = 0, size = 0;
15d034d0 14699 const char *name;
6ccb9162
UW
14700 enum type_code code = TYPE_CODE_INT;
14701 int type_flags = 0;
14702 struct type *target_type = NULL;
c906108c 14703
e142c38c 14704 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
14705 if (attr)
14706 {
14707 encoding = DW_UNSND (attr);
14708 }
e142c38c 14709 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14710 if (attr)
14711 {
14712 size = DW_UNSND (attr);
14713 }
39cbfefa 14714 name = dwarf2_name (die, cu);
6ccb9162 14715 if (!name)
c906108c 14716 {
6ccb9162
UW
14717 complaint (&symfile_complaints,
14718 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 14719 }
6ccb9162
UW
14720
14721 switch (encoding)
c906108c 14722 {
6ccb9162
UW
14723 case DW_ATE_address:
14724 /* Turn DW_ATE_address into a void * pointer. */
14725 code = TYPE_CODE_PTR;
14726 type_flags |= TYPE_FLAG_UNSIGNED;
14727 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
14728 break;
14729 case DW_ATE_boolean:
14730 code = TYPE_CODE_BOOL;
14731 type_flags |= TYPE_FLAG_UNSIGNED;
14732 break;
14733 case DW_ATE_complex_float:
14734 code = TYPE_CODE_COMPLEX;
14735 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
14736 break;
14737 case DW_ATE_decimal_float:
14738 code = TYPE_CODE_DECFLOAT;
14739 break;
14740 case DW_ATE_float:
14741 code = TYPE_CODE_FLT;
14742 break;
14743 case DW_ATE_signed:
14744 break;
14745 case DW_ATE_unsigned:
14746 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
14747 if (cu->language == language_fortran
14748 && name
61012eef 14749 && startswith (name, "character("))
3b2b8fea 14750 code = TYPE_CODE_CHAR;
6ccb9162
UW
14751 break;
14752 case DW_ATE_signed_char:
6e70227d 14753 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
14754 || cu->language == language_pascal
14755 || cu->language == language_fortran)
6ccb9162
UW
14756 code = TYPE_CODE_CHAR;
14757 break;
14758 case DW_ATE_unsigned_char:
868a0084 14759 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
14760 || cu->language == language_pascal
14761 || cu->language == language_fortran)
6ccb9162
UW
14762 code = TYPE_CODE_CHAR;
14763 type_flags |= TYPE_FLAG_UNSIGNED;
14764 break;
75079b2b
TT
14765 case DW_ATE_UTF:
14766 /* We just treat this as an integer and then recognize the
14767 type by name elsewhere. */
14768 break;
14769
6ccb9162
UW
14770 default:
14771 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
14772 dwarf_type_encoding_name (encoding));
14773 break;
c906108c 14774 }
6ccb9162 14775
0114d602
DJ
14776 type = init_type (code, size, type_flags, NULL, objfile);
14777 TYPE_NAME (type) = name;
6ccb9162
UW
14778 TYPE_TARGET_TYPE (type) = target_type;
14779
0114d602 14780 if (name && strcmp (name, "char") == 0)
876cecd0 14781 TYPE_NOSIGN (type) = 1;
0114d602 14782
f792889a 14783 return set_die_type (die, type, cu);
c906108c
SS
14784}
14785
80180f79
SA
14786/* Parse dwarf attribute if it's a block, reference or constant and put the
14787 resulting value of the attribute into struct bound_prop.
14788 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
14789
14790static int
14791attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
14792 struct dwarf2_cu *cu, struct dynamic_prop *prop)
14793{
14794 struct dwarf2_property_baton *baton;
14795 struct obstack *obstack = &cu->objfile->objfile_obstack;
14796
14797 if (attr == NULL || prop == NULL)
14798 return 0;
14799
14800 if (attr_form_is_block (attr))
14801 {
14802 baton = obstack_alloc (obstack, sizeof (*baton));
14803 baton->referenced_type = NULL;
14804 baton->locexpr.per_cu = cu->per_cu;
14805 baton->locexpr.size = DW_BLOCK (attr)->size;
14806 baton->locexpr.data = DW_BLOCK (attr)->data;
14807 prop->data.baton = baton;
14808 prop->kind = PROP_LOCEXPR;
14809 gdb_assert (prop->data.baton != NULL);
14810 }
14811 else if (attr_form_is_ref (attr))
14812 {
14813 struct dwarf2_cu *target_cu = cu;
14814 struct die_info *target_die;
14815 struct attribute *target_attr;
14816
14817 target_die = follow_die_ref (die, attr, &target_cu);
14818 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
14819 if (target_attr == NULL)
14820 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
14821 target_cu);
80180f79
SA
14822 if (target_attr == NULL)
14823 return 0;
14824
df25ebbd 14825 switch (target_attr->name)
80180f79 14826 {
df25ebbd
JB
14827 case DW_AT_location:
14828 if (attr_form_is_section_offset (target_attr))
14829 {
14830 baton = obstack_alloc (obstack, sizeof (*baton));
14831 baton->referenced_type = die_type (target_die, target_cu);
14832 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
14833 prop->data.baton = baton;
14834 prop->kind = PROP_LOCLIST;
14835 gdb_assert (prop->data.baton != NULL);
14836 }
14837 else if (attr_form_is_block (target_attr))
14838 {
14839 baton = obstack_alloc (obstack, sizeof (*baton));
14840 baton->referenced_type = die_type (target_die, target_cu);
14841 baton->locexpr.per_cu = cu->per_cu;
14842 baton->locexpr.size = DW_BLOCK (target_attr)->size;
14843 baton->locexpr.data = DW_BLOCK (target_attr)->data;
14844 prop->data.baton = baton;
14845 prop->kind = PROP_LOCEXPR;
14846 gdb_assert (prop->data.baton != NULL);
14847 }
14848 else
14849 {
14850 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14851 "dynamic property");
14852 return 0;
14853 }
14854 break;
14855 case DW_AT_data_member_location:
14856 {
14857 LONGEST offset;
14858
14859 if (!handle_data_member_location (target_die, target_cu,
14860 &offset))
14861 return 0;
14862
14863 baton = obstack_alloc (obstack, sizeof (*baton));
6ad395a7
JB
14864 baton->referenced_type = read_type_die (target_die->parent,
14865 target_cu);
df25ebbd
JB
14866 baton->offset_info.offset = offset;
14867 baton->offset_info.type = die_type (target_die, target_cu);
14868 prop->data.baton = baton;
14869 prop->kind = PROP_ADDR_OFFSET;
14870 break;
14871 }
80180f79
SA
14872 }
14873 }
14874 else if (attr_form_is_constant (attr))
14875 {
14876 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
14877 prop->kind = PROP_CONST;
14878 }
14879 else
14880 {
14881 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
14882 dwarf2_name (die, cu));
14883 return 0;
14884 }
14885
14886 return 1;
14887}
14888
a02abb62
JB
14889/* Read the given DW_AT_subrange DIE. */
14890
f792889a 14891static struct type *
a02abb62
JB
14892read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
14893{
4c9ad8c2 14894 struct type *base_type, *orig_base_type;
a02abb62
JB
14895 struct type *range_type;
14896 struct attribute *attr;
729efb13 14897 struct dynamic_prop low, high;
4fae6e18 14898 int low_default_is_valid;
c451ebe5 14899 int high_bound_is_count = 0;
15d034d0 14900 const char *name;
43bbcdc2 14901 LONGEST negative_mask;
e77813c8 14902
4c9ad8c2
TT
14903 orig_base_type = die_type (die, cu);
14904 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
14905 whereas the real type might be. So, we use ORIG_BASE_TYPE when
14906 creating the range type, but we use the result of check_typedef
14907 when examining properties of the type. */
14908 base_type = check_typedef (orig_base_type);
a02abb62 14909
7e314c57
JK
14910 /* The die_type call above may have already set the type for this DIE. */
14911 range_type = get_die_type (die, cu);
14912 if (range_type)
14913 return range_type;
14914
729efb13
SA
14915 low.kind = PROP_CONST;
14916 high.kind = PROP_CONST;
14917 high.data.const_val = 0;
14918
4fae6e18
JK
14919 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
14920 omitting DW_AT_lower_bound. */
14921 switch (cu->language)
6e70227d 14922 {
4fae6e18
JK
14923 case language_c:
14924 case language_cplus:
729efb13 14925 low.data.const_val = 0;
4fae6e18
JK
14926 low_default_is_valid = 1;
14927 break;
14928 case language_fortran:
729efb13 14929 low.data.const_val = 1;
4fae6e18
JK
14930 low_default_is_valid = 1;
14931 break;
14932 case language_d:
14933 case language_java:
14934 case language_objc:
729efb13 14935 low.data.const_val = 0;
4fae6e18
JK
14936 low_default_is_valid = (cu->header.version >= 4);
14937 break;
14938 case language_ada:
14939 case language_m2:
14940 case language_pascal:
729efb13 14941 low.data.const_val = 1;
4fae6e18
JK
14942 low_default_is_valid = (cu->header.version >= 4);
14943 break;
14944 default:
729efb13 14945 low.data.const_val = 0;
4fae6e18
JK
14946 low_default_is_valid = 0;
14947 break;
a02abb62
JB
14948 }
14949
e142c38c 14950 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 14951 if (attr)
11c1ba78 14952 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
14953 else if (!low_default_is_valid)
14954 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
14955 "- DIE at 0x%x [in module %s]"),
4262abfb 14956 die->offset.sect_off, objfile_name (cu->objfile));
a02abb62 14957
e142c38c 14958 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 14959 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
14960 {
14961 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 14962 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 14963 {
c451ebe5
SA
14964 /* If bounds are constant do the final calculation here. */
14965 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
14966 high.data.const_val = low.data.const_val + high.data.const_val - 1;
14967 else
14968 high_bound_is_count = 1;
c2ff108b 14969 }
e77813c8
PM
14970 }
14971
14972 /* Dwarf-2 specifications explicitly allows to create subrange types
14973 without specifying a base type.
14974 In that case, the base type must be set to the type of
14975 the lower bound, upper bound or count, in that order, if any of these
14976 three attributes references an object that has a type.
14977 If no base type is found, the Dwarf-2 specifications say that
14978 a signed integer type of size equal to the size of an address should
14979 be used.
14980 For the following C code: `extern char gdb_int [];'
14981 GCC produces an empty range DIE.
14982 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 14983 high bound or count are not yet handled by this code. */
e77813c8
PM
14984 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
14985 {
14986 struct objfile *objfile = cu->objfile;
14987 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14988 int addr_size = gdbarch_addr_bit (gdbarch) /8;
14989 struct type *int_type = objfile_type (objfile)->builtin_int;
14990
14991 /* Test "int", "long int", and "long long int" objfile types,
14992 and select the first one having a size above or equal to the
14993 architecture address size. */
14994 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14995 base_type = int_type;
14996 else
14997 {
14998 int_type = objfile_type (objfile)->builtin_long;
14999 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15000 base_type = int_type;
15001 else
15002 {
15003 int_type = objfile_type (objfile)->builtin_long_long;
15004 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15005 base_type = int_type;
15006 }
15007 }
15008 }
a02abb62 15009
dbb9c2b1
JB
15010 /* Normally, the DWARF producers are expected to use a signed
15011 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15012 But this is unfortunately not always the case, as witnessed
15013 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15014 is used instead. To work around that ambiguity, we treat
15015 the bounds as signed, and thus sign-extend their values, when
15016 the base type is signed. */
6e70227d 15017 negative_mask =
43bbcdc2 15018 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
729efb13
SA
15019 if (low.kind == PROP_CONST
15020 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15021 low.data.const_val |= negative_mask;
15022 if (high.kind == PROP_CONST
15023 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15024 high.data.const_val |= negative_mask;
43bbcdc2 15025
729efb13 15026 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15027
c451ebe5
SA
15028 if (high_bound_is_count)
15029 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15030
c2ff108b
JK
15031 /* Ada expects an empty array on no boundary attributes. */
15032 if (attr == NULL && cu->language != language_ada)
729efb13 15033 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15034
39cbfefa
DJ
15035 name = dwarf2_name (die, cu);
15036 if (name)
15037 TYPE_NAME (range_type) = name;
6e70227d 15038
e142c38c 15039 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15040 if (attr)
15041 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15042
7e314c57
JK
15043 set_die_type (die, range_type, cu);
15044
15045 /* set_die_type should be already done. */
b4ba55a1
JB
15046 set_descriptive_type (range_type, die, cu);
15047
7e314c57 15048 return range_type;
a02abb62 15049}
6e70227d 15050
f792889a 15051static struct type *
81a17f79
JB
15052read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15053{
15054 struct type *type;
81a17f79 15055
81a17f79
JB
15056 /* For now, we only support the C meaning of an unspecified type: void. */
15057
0114d602
DJ
15058 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
15059 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15060
f792889a 15061 return set_die_type (die, type, cu);
81a17f79 15062}
a02abb62 15063
639d11d3
DC
15064/* Read a single die and all its descendents. Set the die's sibling
15065 field to NULL; set other fields in the die correctly, and set all
15066 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15067 location of the info_ptr after reading all of those dies. PARENT
15068 is the parent of the die in question. */
15069
15070static struct die_info *
dee91e82 15071read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15072 const gdb_byte *info_ptr,
15073 const gdb_byte **new_info_ptr,
dee91e82 15074 struct die_info *parent)
639d11d3
DC
15075{
15076 struct die_info *die;
d521ce57 15077 const gdb_byte *cur_ptr;
639d11d3
DC
15078 int has_children;
15079
bf6af496 15080 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15081 if (die == NULL)
15082 {
15083 *new_info_ptr = cur_ptr;
15084 return NULL;
15085 }
93311388 15086 store_in_ref_table (die, reader->cu);
639d11d3
DC
15087
15088 if (has_children)
bf6af496 15089 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15090 else
15091 {
15092 die->child = NULL;
15093 *new_info_ptr = cur_ptr;
15094 }
15095
15096 die->sibling = NULL;
15097 die->parent = parent;
15098 return die;
15099}
15100
15101/* Read a die, all of its descendents, and all of its siblings; set
15102 all of the fields of all of the dies correctly. Arguments are as
15103 in read_die_and_children. */
15104
15105static struct die_info *
bf6af496 15106read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15107 const gdb_byte *info_ptr,
15108 const gdb_byte **new_info_ptr,
bf6af496 15109 struct die_info *parent)
639d11d3
DC
15110{
15111 struct die_info *first_die, *last_sibling;
d521ce57 15112 const gdb_byte *cur_ptr;
639d11d3 15113
c906108c 15114 cur_ptr = info_ptr;
639d11d3
DC
15115 first_die = last_sibling = NULL;
15116
15117 while (1)
c906108c 15118 {
639d11d3 15119 struct die_info *die
dee91e82 15120 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15121
1d325ec1 15122 if (die == NULL)
c906108c 15123 {
639d11d3
DC
15124 *new_info_ptr = cur_ptr;
15125 return first_die;
c906108c 15126 }
1d325ec1
DJ
15127
15128 if (!first_die)
15129 first_die = die;
c906108c 15130 else
1d325ec1
DJ
15131 last_sibling->sibling = die;
15132
15133 last_sibling = die;
c906108c 15134 }
c906108c
SS
15135}
15136
bf6af496
DE
15137/* Read a die, all of its descendents, and all of its siblings; set
15138 all of the fields of all of the dies correctly. Arguments are as
15139 in read_die_and_children.
15140 This the main entry point for reading a DIE and all its children. */
15141
15142static struct die_info *
15143read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15144 const gdb_byte *info_ptr,
15145 const gdb_byte **new_info_ptr,
bf6af496
DE
15146 struct die_info *parent)
15147{
15148 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15149 new_info_ptr, parent);
15150
b4f54984 15151 if (dwarf_die_debug)
bf6af496
DE
15152 {
15153 fprintf_unfiltered (gdb_stdlog,
15154 "Read die from %s@0x%x of %s:\n",
a32a8923 15155 get_section_name (reader->die_section),
bf6af496
DE
15156 (unsigned) (info_ptr - reader->die_section->buffer),
15157 bfd_get_filename (reader->abfd));
b4f54984 15158 dump_die (die, dwarf_die_debug);
bf6af496
DE
15159 }
15160
15161 return die;
15162}
15163
3019eac3
DE
15164/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15165 attributes.
15166 The caller is responsible for filling in the extra attributes
15167 and updating (*DIEP)->num_attrs.
15168 Set DIEP to point to a newly allocated die with its information,
15169 except for its child, sibling, and parent fields.
15170 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15171
d521ce57 15172static const gdb_byte *
3019eac3 15173read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15174 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15175 int *has_children, int num_extra_attrs)
93311388 15176{
b64f50a1
JK
15177 unsigned int abbrev_number, bytes_read, i;
15178 sect_offset offset;
93311388
DE
15179 struct abbrev_info *abbrev;
15180 struct die_info *die;
15181 struct dwarf2_cu *cu = reader->cu;
15182 bfd *abfd = reader->abfd;
15183
b64f50a1 15184 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
15185 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15186 info_ptr += bytes_read;
15187 if (!abbrev_number)
15188 {
15189 *diep = NULL;
15190 *has_children = 0;
15191 return info_ptr;
15192 }
15193
433df2d4 15194 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15195 if (!abbrev)
348e048f
DE
15196 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15197 abbrev_number,
15198 bfd_get_filename (abfd));
15199
3019eac3 15200 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
15201 die->offset = offset;
15202 die->tag = abbrev->tag;
15203 die->abbrev = abbrev_number;
15204
3019eac3
DE
15205 /* Make the result usable.
15206 The caller needs to update num_attrs after adding the extra
15207 attributes. */
93311388
DE
15208 die->num_attrs = abbrev->num_attrs;
15209
15210 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15211 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15212 info_ptr);
93311388
DE
15213
15214 *diep = die;
15215 *has_children = abbrev->has_children;
15216 return info_ptr;
15217}
15218
3019eac3
DE
15219/* Read a die and all its attributes.
15220 Set DIEP to point to a newly allocated die with its information,
15221 except for its child, sibling, and parent fields.
15222 Set HAS_CHILDREN to tell whether the die has children or not. */
15223
d521ce57 15224static const gdb_byte *
3019eac3 15225read_full_die (const struct die_reader_specs *reader,
d521ce57 15226 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15227 int *has_children)
15228{
d521ce57 15229 const gdb_byte *result;
bf6af496
DE
15230
15231 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15232
b4f54984 15233 if (dwarf_die_debug)
bf6af496
DE
15234 {
15235 fprintf_unfiltered (gdb_stdlog,
15236 "Read die from %s@0x%x of %s:\n",
a32a8923 15237 get_section_name (reader->die_section),
bf6af496
DE
15238 (unsigned) (info_ptr - reader->die_section->buffer),
15239 bfd_get_filename (reader->abfd));
b4f54984 15240 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15241 }
15242
15243 return result;
3019eac3 15244}
433df2d4
DE
15245\f
15246/* Abbreviation tables.
3019eac3 15247
433df2d4 15248 In DWARF version 2, the description of the debugging information is
c906108c
SS
15249 stored in a separate .debug_abbrev section. Before we read any
15250 dies from a section we read in all abbreviations and install them
433df2d4
DE
15251 in a hash table. */
15252
15253/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15254
15255static struct abbrev_info *
15256abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15257{
15258 struct abbrev_info *abbrev;
15259
15260 abbrev = (struct abbrev_info *)
15261 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
15262 memset (abbrev, 0, sizeof (struct abbrev_info));
15263 return abbrev;
15264}
15265
15266/* Add an abbreviation to the table. */
c906108c
SS
15267
15268static void
433df2d4
DE
15269abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15270 unsigned int abbrev_number,
15271 struct abbrev_info *abbrev)
15272{
15273 unsigned int hash_number;
15274
15275 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15276 abbrev->next = abbrev_table->abbrevs[hash_number];
15277 abbrev_table->abbrevs[hash_number] = abbrev;
15278}
dee91e82 15279
433df2d4
DE
15280/* Look up an abbrev in the table.
15281 Returns NULL if the abbrev is not found. */
15282
15283static struct abbrev_info *
15284abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15285 unsigned int abbrev_number)
c906108c 15286{
433df2d4
DE
15287 unsigned int hash_number;
15288 struct abbrev_info *abbrev;
15289
15290 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15291 abbrev = abbrev_table->abbrevs[hash_number];
15292
15293 while (abbrev)
15294 {
15295 if (abbrev->number == abbrev_number)
15296 return abbrev;
15297 abbrev = abbrev->next;
15298 }
15299 return NULL;
15300}
15301
15302/* Read in an abbrev table. */
15303
15304static struct abbrev_table *
15305abbrev_table_read_table (struct dwarf2_section_info *section,
15306 sect_offset offset)
15307{
15308 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15309 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15310 struct abbrev_table *abbrev_table;
d521ce57 15311 const gdb_byte *abbrev_ptr;
c906108c
SS
15312 struct abbrev_info *cur_abbrev;
15313 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15314 unsigned int abbrev_form;
f3dd6933
DJ
15315 struct attr_abbrev *cur_attrs;
15316 unsigned int allocated_attrs;
c906108c 15317
70ba0933 15318 abbrev_table = XNEW (struct abbrev_table);
f4dc4d17 15319 abbrev_table->offset = offset;
433df2d4
DE
15320 obstack_init (&abbrev_table->abbrev_obstack);
15321 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
15322 (ABBREV_HASH_SIZE
15323 * sizeof (struct abbrev_info *)));
15324 memset (abbrev_table->abbrevs, 0,
15325 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15326
433df2d4
DE
15327 dwarf2_read_section (objfile, section);
15328 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
15329 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15330 abbrev_ptr += bytes_read;
15331
f3dd6933
DJ
15332 allocated_attrs = ATTR_ALLOC_CHUNK;
15333 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 15334
0963b4bd 15335 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15336 while (abbrev_number)
15337 {
433df2d4 15338 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15339
15340 /* read in abbrev header */
15341 cur_abbrev->number = abbrev_number;
aead7601
SM
15342 cur_abbrev->tag
15343 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15344 abbrev_ptr += bytes_read;
15345 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15346 abbrev_ptr += 1;
15347
15348 /* now read in declarations */
15349 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15350 abbrev_ptr += bytes_read;
15351 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15352 abbrev_ptr += bytes_read;
15353 while (abbrev_name)
15354 {
f3dd6933 15355 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15356 {
f3dd6933
DJ
15357 allocated_attrs += ATTR_ALLOC_CHUNK;
15358 cur_attrs
15359 = xrealloc (cur_attrs, (allocated_attrs
15360 * sizeof (struct attr_abbrev)));
c906108c 15361 }
ae038cb0 15362
aead7601
SM
15363 cur_attrs[cur_abbrev->num_attrs].name
15364 = (enum dwarf_attribute) abbrev_name;
15365 cur_attrs[cur_abbrev->num_attrs++].form
15366 = (enum dwarf_form) abbrev_form;
c906108c
SS
15367 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15368 abbrev_ptr += bytes_read;
15369 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15370 abbrev_ptr += bytes_read;
15371 }
15372
433df2d4 15373 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
15374 (cur_abbrev->num_attrs
15375 * sizeof (struct attr_abbrev)));
15376 memcpy (cur_abbrev->attrs, cur_attrs,
15377 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15378
433df2d4 15379 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15380
15381 /* Get next abbreviation.
15382 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15383 always properly terminated with an abbrev number of 0.
15384 Exit loop if we encounter an abbreviation which we have
15385 already read (which means we are about to read the abbreviations
15386 for the next compile unit) or if the end of the abbreviation
15387 table is reached. */
433df2d4 15388 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15389 break;
15390 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15391 abbrev_ptr += bytes_read;
433df2d4 15392 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15393 break;
15394 }
f3dd6933
DJ
15395
15396 xfree (cur_attrs);
433df2d4 15397 return abbrev_table;
c906108c
SS
15398}
15399
433df2d4 15400/* Free the resources held by ABBREV_TABLE. */
c906108c 15401
c906108c 15402static void
433df2d4 15403abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15404{
433df2d4
DE
15405 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15406 xfree (abbrev_table);
c906108c
SS
15407}
15408
f4dc4d17
DE
15409/* Same as abbrev_table_free but as a cleanup.
15410 We pass in a pointer to the pointer to the table so that we can
15411 set the pointer to NULL when we're done. It also simplifies
73051182 15412 build_type_psymtabs_1. */
f4dc4d17
DE
15413
15414static void
15415abbrev_table_free_cleanup (void *table_ptr)
15416{
15417 struct abbrev_table **abbrev_table_ptr = table_ptr;
15418
15419 if (*abbrev_table_ptr != NULL)
15420 abbrev_table_free (*abbrev_table_ptr);
15421 *abbrev_table_ptr = NULL;
15422}
15423
433df2d4
DE
15424/* Read the abbrev table for CU from ABBREV_SECTION. */
15425
15426static void
15427dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15428 struct dwarf2_section_info *abbrev_section)
c906108c 15429{
433df2d4
DE
15430 cu->abbrev_table =
15431 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15432}
c906108c 15433
433df2d4 15434/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15435
433df2d4
DE
15436static void
15437dwarf2_free_abbrev_table (void *ptr_to_cu)
15438{
15439 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 15440
a2ce51a0
DE
15441 if (cu->abbrev_table != NULL)
15442 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15443 /* Set this to NULL so that we SEGV if we try to read it later,
15444 and also because free_comp_unit verifies this is NULL. */
15445 cu->abbrev_table = NULL;
15446}
15447\f
72bf9492
DJ
15448/* Returns nonzero if TAG represents a type that we might generate a partial
15449 symbol for. */
15450
15451static int
15452is_type_tag_for_partial (int tag)
15453{
15454 switch (tag)
15455 {
15456#if 0
15457 /* Some types that would be reasonable to generate partial symbols for,
15458 that we don't at present. */
15459 case DW_TAG_array_type:
15460 case DW_TAG_file_type:
15461 case DW_TAG_ptr_to_member_type:
15462 case DW_TAG_set_type:
15463 case DW_TAG_string_type:
15464 case DW_TAG_subroutine_type:
15465#endif
15466 case DW_TAG_base_type:
15467 case DW_TAG_class_type:
680b30c7 15468 case DW_TAG_interface_type:
72bf9492
DJ
15469 case DW_TAG_enumeration_type:
15470 case DW_TAG_structure_type:
15471 case DW_TAG_subrange_type:
15472 case DW_TAG_typedef:
15473 case DW_TAG_union_type:
15474 return 1;
15475 default:
15476 return 0;
15477 }
15478}
15479
15480/* Load all DIEs that are interesting for partial symbols into memory. */
15481
15482static struct partial_die_info *
dee91e82 15483load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15484 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15485{
dee91e82 15486 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15487 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15488 struct partial_die_info *part_die;
15489 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15490 struct abbrev_info *abbrev;
15491 unsigned int bytes_read;
5afb4e99 15492 unsigned int load_all = 0;
72bf9492
DJ
15493 int nesting_level = 1;
15494
15495 parent_die = NULL;
15496 last_die = NULL;
15497
7adf1e79
DE
15498 gdb_assert (cu->per_cu != NULL);
15499 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15500 load_all = 1;
15501
72bf9492
DJ
15502 cu->partial_dies
15503 = htab_create_alloc_ex (cu->header.length / 12,
15504 partial_die_hash,
15505 partial_die_eq,
15506 NULL,
15507 &cu->comp_unit_obstack,
15508 hashtab_obstack_allocate,
15509 dummy_obstack_deallocate);
15510
15511 part_die = obstack_alloc (&cu->comp_unit_obstack,
15512 sizeof (struct partial_die_info));
15513
15514 while (1)
15515 {
15516 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15517
15518 /* A NULL abbrev means the end of a series of children. */
15519 if (abbrev == NULL)
15520 {
15521 if (--nesting_level == 0)
15522 {
15523 /* PART_DIE was probably the last thing allocated on the
15524 comp_unit_obstack, so we could call obstack_free
15525 here. We don't do that because the waste is small,
15526 and will be cleaned up when we're done with this
15527 compilation unit. This way, we're also more robust
15528 against other users of the comp_unit_obstack. */
15529 return first_die;
15530 }
15531 info_ptr += bytes_read;
15532 last_die = parent_die;
15533 parent_die = parent_die->die_parent;
15534 continue;
15535 }
15536
98bfdba5
PA
15537 /* Check for template arguments. We never save these; if
15538 they're seen, we just mark the parent, and go on our way. */
15539 if (parent_die != NULL
15540 && cu->language == language_cplus
15541 && (abbrev->tag == DW_TAG_template_type_param
15542 || abbrev->tag == DW_TAG_template_value_param))
15543 {
15544 parent_die->has_template_arguments = 1;
15545
15546 if (!load_all)
15547 {
15548 /* We don't need a partial DIE for the template argument. */
dee91e82 15549 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15550 continue;
15551 }
15552 }
15553
0d99eb77 15554 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
15555 Skip their other children. */
15556 if (!load_all
15557 && cu->language == language_cplus
15558 && parent_die != NULL
15559 && parent_die->tag == DW_TAG_subprogram)
15560 {
dee91e82 15561 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15562 continue;
15563 }
15564
5afb4e99
DJ
15565 /* Check whether this DIE is interesting enough to save. Normally
15566 we would not be interested in members here, but there may be
15567 later variables referencing them via DW_AT_specification (for
15568 static members). */
15569 if (!load_all
15570 && !is_type_tag_for_partial (abbrev->tag)
72929c62 15571 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15572 && abbrev->tag != DW_TAG_enumerator
15573 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15574 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15575 && abbrev->tag != DW_TAG_variable
5afb4e99 15576 && abbrev->tag != DW_TAG_namespace
f55ee35c 15577 && abbrev->tag != DW_TAG_module
95554aad 15578 && abbrev->tag != DW_TAG_member
74921315
KS
15579 && abbrev->tag != DW_TAG_imported_unit
15580 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15581 {
15582 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15583 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15584 continue;
15585 }
15586
dee91e82
DE
15587 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15588 info_ptr);
72bf9492
DJ
15589
15590 /* This two-pass algorithm for processing partial symbols has a
15591 high cost in cache pressure. Thus, handle some simple cases
15592 here which cover the majority of C partial symbols. DIEs
15593 which neither have specification tags in them, nor could have
15594 specification tags elsewhere pointing at them, can simply be
15595 processed and discarded.
15596
15597 This segment is also optional; scan_partial_symbols and
15598 add_partial_symbol will handle these DIEs if we chain
15599 them in normally. When compilers which do not emit large
15600 quantities of duplicate debug information are more common,
15601 this code can probably be removed. */
15602
15603 /* Any complete simple types at the top level (pretty much all
15604 of them, for a language without namespaces), can be processed
15605 directly. */
15606 if (parent_die == NULL
15607 && part_die->has_specification == 0
15608 && part_die->is_declaration == 0
d8228535 15609 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
15610 || part_die->tag == DW_TAG_base_type
15611 || part_die->tag == DW_TAG_subrange_type))
15612 {
15613 if (building_psymtab && part_die->name != NULL)
04a679b8 15614 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15615 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 15616 &objfile->static_psymbols,
1762568f 15617 0, cu->language, objfile);
dee91e82 15618 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15619 continue;
15620 }
15621
d8228535
JK
15622 /* The exception for DW_TAG_typedef with has_children above is
15623 a workaround of GCC PR debug/47510. In the case of this complaint
15624 type_name_no_tag_or_error will error on such types later.
15625
15626 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15627 it could not find the child DIEs referenced later, this is checked
15628 above. In correct DWARF DW_TAG_typedef should have no children. */
15629
15630 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15631 complaint (&symfile_complaints,
15632 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15633 "- DIE at 0x%x [in module %s]"),
4262abfb 15634 part_die->offset.sect_off, objfile_name (objfile));
d8228535 15635
72bf9492
DJ
15636 /* If we're at the second level, and we're an enumerator, and
15637 our parent has no specification (meaning possibly lives in a
15638 namespace elsewhere), then we can add the partial symbol now
15639 instead of queueing it. */
15640 if (part_die->tag == DW_TAG_enumerator
15641 && parent_die != NULL
15642 && parent_die->die_parent == NULL
15643 && parent_die->tag == DW_TAG_enumeration_type
15644 && parent_die->has_specification == 0)
15645 {
15646 if (part_die->name == NULL)
3e43a32a
MS
15647 complaint (&symfile_complaints,
15648 _("malformed enumerator DIE ignored"));
72bf9492 15649 else if (building_psymtab)
04a679b8 15650 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15651 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
15652 (cu->language == language_cplus
15653 || cu->language == language_java)
bb5ed363
DE
15654 ? &objfile->global_psymbols
15655 : &objfile->static_psymbols,
1762568f 15656 0, cu->language, objfile);
72bf9492 15657
dee91e82 15658 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15659 continue;
15660 }
15661
15662 /* We'll save this DIE so link it in. */
15663 part_die->die_parent = parent_die;
15664 part_die->die_sibling = NULL;
15665 part_die->die_child = NULL;
15666
15667 if (last_die && last_die == parent_die)
15668 last_die->die_child = part_die;
15669 else if (last_die)
15670 last_die->die_sibling = part_die;
15671
15672 last_die = part_die;
15673
15674 if (first_die == NULL)
15675 first_die = part_die;
15676
15677 /* Maybe add the DIE to the hash table. Not all DIEs that we
15678 find interesting need to be in the hash table, because we
15679 also have the parent/sibling/child chains; only those that we
15680 might refer to by offset later during partial symbol reading.
15681
15682 For now this means things that might have be the target of a
15683 DW_AT_specification, DW_AT_abstract_origin, or
15684 DW_AT_extension. DW_AT_extension will refer only to
15685 namespaces; DW_AT_abstract_origin refers to functions (and
15686 many things under the function DIE, but we do not recurse
15687 into function DIEs during partial symbol reading) and
15688 possibly variables as well; DW_AT_specification refers to
15689 declarations. Declarations ought to have the DW_AT_declaration
15690 flag. It happens that GCC forgets to put it in sometimes, but
15691 only for functions, not for types.
15692
15693 Adding more things than necessary to the hash table is harmless
15694 except for the performance cost. Adding too few will result in
5afb4e99
DJ
15695 wasted time in find_partial_die, when we reread the compilation
15696 unit with load_all_dies set. */
72bf9492 15697
5afb4e99 15698 if (load_all
72929c62 15699 || abbrev->tag == DW_TAG_constant
5afb4e99 15700 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
15701 || abbrev->tag == DW_TAG_variable
15702 || abbrev->tag == DW_TAG_namespace
15703 || part_die->is_declaration)
15704 {
15705 void **slot;
15706
15707 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 15708 part_die->offset.sect_off, INSERT);
72bf9492
DJ
15709 *slot = part_die;
15710 }
15711
15712 part_die = obstack_alloc (&cu->comp_unit_obstack,
15713 sizeof (struct partial_die_info));
15714
15715 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 15716 we have no reason to follow the children of structures; for other
98bfdba5
PA
15717 languages we have to, so that we can get at method physnames
15718 to infer fully qualified class names, for DW_AT_specification,
15719 and for C++ template arguments. For C++, we also look one level
15720 inside functions to find template arguments (if the name of the
15721 function does not already contain the template arguments).
bc30ff58
JB
15722
15723 For Ada, we need to scan the children of subprograms and lexical
15724 blocks as well because Ada allows the definition of nested
15725 entities that could be interesting for the debugger, such as
15726 nested subprograms for instance. */
72bf9492 15727 if (last_die->has_children
5afb4e99
DJ
15728 && (load_all
15729 || last_die->tag == DW_TAG_namespace
f55ee35c 15730 || last_die->tag == DW_TAG_module
72bf9492 15731 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
15732 || (cu->language == language_cplus
15733 && last_die->tag == DW_TAG_subprogram
15734 && (last_die->name == NULL
15735 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
15736 || (cu->language != language_c
15737 && (last_die->tag == DW_TAG_class_type
680b30c7 15738 || last_die->tag == DW_TAG_interface_type
72bf9492 15739 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
15740 || last_die->tag == DW_TAG_union_type))
15741 || (cu->language == language_ada
15742 && (last_die->tag == DW_TAG_subprogram
15743 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
15744 {
15745 nesting_level++;
15746 parent_die = last_die;
15747 continue;
15748 }
15749
15750 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15751 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
15752
15753 /* Back to the top, do it again. */
15754 }
15755}
15756
c906108c
SS
15757/* Read a minimal amount of information into the minimal die structure. */
15758
d521ce57 15759static const gdb_byte *
dee91e82
DE
15760read_partial_die (const struct die_reader_specs *reader,
15761 struct partial_die_info *part_die,
15762 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 15763 const gdb_byte *info_ptr)
c906108c 15764{
dee91e82 15765 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15766 struct objfile *objfile = cu->objfile;
d521ce57 15767 const gdb_byte *buffer = reader->buffer;
fa238c03 15768 unsigned int i;
c906108c 15769 struct attribute attr;
c5aa993b 15770 int has_low_pc_attr = 0;
c906108c 15771 int has_high_pc_attr = 0;
91da1414 15772 int high_pc_relative = 0;
c906108c 15773
72bf9492 15774 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 15775
b64f50a1 15776 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
15777
15778 info_ptr += abbrev_len;
15779
15780 if (abbrev == NULL)
15781 return info_ptr;
15782
c906108c
SS
15783 part_die->tag = abbrev->tag;
15784 part_die->has_children = abbrev->has_children;
c906108c
SS
15785
15786 for (i = 0; i < abbrev->num_attrs; ++i)
15787 {
dee91e82 15788 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
15789
15790 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 15791 partial symbol table. */
c906108c
SS
15792 switch (attr.name)
15793 {
15794 case DW_AT_name:
71c25dea
TT
15795 switch (part_die->tag)
15796 {
15797 case DW_TAG_compile_unit:
95554aad 15798 case DW_TAG_partial_unit:
348e048f 15799 case DW_TAG_type_unit:
71c25dea
TT
15800 /* Compilation units have a DW_AT_name that is a filename, not
15801 a source language identifier. */
15802 case DW_TAG_enumeration_type:
15803 case DW_TAG_enumerator:
15804 /* These tags always have simple identifiers already; no need
15805 to canonicalize them. */
15806 part_die->name = DW_STRING (&attr);
15807 break;
15808 default:
15809 part_die->name
15810 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 15811 &objfile->per_bfd->storage_obstack);
71c25dea
TT
15812 break;
15813 }
c906108c 15814 break;
31ef98ae 15815 case DW_AT_linkage_name:
c906108c 15816 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
15817 /* Note that both forms of linkage name might appear. We
15818 assume they will be the same, and we only store the last
15819 one we see. */
94af9270
KS
15820 if (cu->language == language_ada)
15821 part_die->name = DW_STRING (&attr);
abc72ce4 15822 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
15823 break;
15824 case DW_AT_low_pc:
15825 has_low_pc_attr = 1;
31aa7e4e 15826 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
15827 break;
15828 case DW_AT_high_pc:
15829 has_high_pc_attr = 1;
31aa7e4e
JB
15830 part_die->highpc = attr_value_as_address (&attr);
15831 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
15832 high_pc_relative = 1;
c906108c
SS
15833 break;
15834 case DW_AT_location:
0963b4bd 15835 /* Support the .debug_loc offsets. */
8e19ed76
PS
15836 if (attr_form_is_block (&attr))
15837 {
95554aad 15838 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 15839 }
3690dd37 15840 else if (attr_form_is_section_offset (&attr))
8e19ed76 15841 {
4d3c2250 15842 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15843 }
15844 else
15845 {
4d3c2250
KB
15846 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15847 "partial symbol information");
8e19ed76 15848 }
c906108c 15849 break;
c906108c
SS
15850 case DW_AT_external:
15851 part_die->is_external = DW_UNSND (&attr);
15852 break;
15853 case DW_AT_declaration:
15854 part_die->is_declaration = DW_UNSND (&attr);
15855 break;
15856 case DW_AT_type:
15857 part_die->has_type = 1;
15858 break;
15859 case DW_AT_abstract_origin:
15860 case DW_AT_specification:
72bf9492
DJ
15861 case DW_AT_extension:
15862 part_die->has_specification = 1;
c764a876 15863 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
15864 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15865 || cu->per_cu->is_dwz);
c906108c
SS
15866 break;
15867 case DW_AT_sibling:
15868 /* Ignore absolute siblings, they might point outside of
15869 the current compile unit. */
15870 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
15871 complaint (&symfile_complaints,
15872 _("ignoring absolute DW_AT_sibling"));
c906108c 15873 else
b9502d3f
WN
15874 {
15875 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
15876 const gdb_byte *sibling_ptr = buffer + off;
15877
15878 if (sibling_ptr < info_ptr)
15879 complaint (&symfile_complaints,
15880 _("DW_AT_sibling points backwards"));
22869d73
KS
15881 else if (sibling_ptr > reader->buffer_end)
15882 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
15883 else
15884 part_die->sibling = sibling_ptr;
15885 }
c906108c 15886 break;
fa4028e9
JB
15887 case DW_AT_byte_size:
15888 part_die->has_byte_size = 1;
15889 break;
ff908ebf
AW
15890 case DW_AT_const_value:
15891 part_die->has_const_value = 1;
15892 break;
68511cec
CES
15893 case DW_AT_calling_convention:
15894 /* DWARF doesn't provide a way to identify a program's source-level
15895 entry point. DW_AT_calling_convention attributes are only meant
15896 to describe functions' calling conventions.
15897
15898 However, because it's a necessary piece of information in
15899 Fortran, and because DW_CC_program is the only piece of debugging
15900 information whose definition refers to a 'main program' at all,
15901 several compilers have begun marking Fortran main programs with
15902 DW_CC_program --- even when those functions use the standard
15903 calling conventions.
15904
15905 So until DWARF specifies a way to provide this information and
15906 compilers pick up the new representation, we'll support this
15907 practice. */
15908 if (DW_UNSND (&attr) == DW_CC_program
15909 && cu->language == language_fortran)
3d548a53 15910 set_objfile_main_name (objfile, part_die->name, language_fortran);
68511cec 15911 break;
481860b3
GB
15912 case DW_AT_inline:
15913 if (DW_UNSND (&attr) == DW_INL_inlined
15914 || DW_UNSND (&attr) == DW_INL_declared_inlined)
15915 part_die->may_be_inlined = 1;
15916 break;
95554aad
TT
15917
15918 case DW_AT_import:
15919 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
15920 {
15921 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
15922 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15923 || cu->per_cu->is_dwz);
15924 }
95554aad
TT
15925 break;
15926
c906108c
SS
15927 default:
15928 break;
15929 }
15930 }
15931
91da1414
MW
15932 if (high_pc_relative)
15933 part_die->highpc += part_die->lowpc;
15934
9373cf26
JK
15935 if (has_low_pc_attr && has_high_pc_attr)
15936 {
15937 /* When using the GNU linker, .gnu.linkonce. sections are used to
15938 eliminate duplicate copies of functions and vtables and such.
15939 The linker will arbitrarily choose one and discard the others.
15940 The AT_*_pc values for such functions refer to local labels in
15941 these sections. If the section from that file was discarded, the
15942 labels are not in the output, so the relocs get a value of 0.
15943 If this is a discarded function, mark the pc bounds as invalid,
15944 so that GDB will ignore it. */
15945 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
15946 {
bb5ed363 15947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
15948
15949 complaint (&symfile_complaints,
15950 _("DW_AT_low_pc %s is zero "
15951 "for DIE at 0x%x [in module %s]"),
15952 paddress (gdbarch, part_die->lowpc),
4262abfb 15953 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
15954 }
15955 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
15956 else if (part_die->lowpc >= part_die->highpc)
15957 {
bb5ed363 15958 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
15959
15960 complaint (&symfile_complaints,
15961 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
15962 "for DIE at 0x%x [in module %s]"),
15963 paddress (gdbarch, part_die->lowpc),
15964 paddress (gdbarch, part_die->highpc),
4262abfb 15965 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
15966 }
15967 else
15968 part_die->has_pc_info = 1;
15969 }
85cbf3d3 15970
c906108c
SS
15971 return info_ptr;
15972}
15973
72bf9492
DJ
15974/* Find a cached partial DIE at OFFSET in CU. */
15975
15976static struct partial_die_info *
b64f50a1 15977find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
15978{
15979 struct partial_die_info *lookup_die = NULL;
15980 struct partial_die_info part_die;
15981
15982 part_die.offset = offset;
b64f50a1
JK
15983 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
15984 offset.sect_off);
72bf9492 15985
72bf9492
DJ
15986 return lookup_die;
15987}
15988
348e048f
DE
15989/* Find a partial DIE at OFFSET, which may or may not be in CU,
15990 except in the case of .debug_types DIEs which do not reference
15991 outside their CU (they do however referencing other types via
55f1336d 15992 DW_FORM_ref_sig8). */
72bf9492
DJ
15993
15994static struct partial_die_info *
36586728 15995find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 15996{
bb5ed363 15997 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
15998 struct dwarf2_per_cu_data *per_cu = NULL;
15999 struct partial_die_info *pd = NULL;
72bf9492 16000
36586728
TT
16001 if (offset_in_dwz == cu->per_cu->is_dwz
16002 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
16003 {
16004 pd = find_partial_die_in_comp_unit (offset, cu);
16005 if (pd != NULL)
16006 return pd;
0d99eb77
DE
16007 /* We missed recording what we needed.
16008 Load all dies and try again. */
16009 per_cu = cu->per_cu;
5afb4e99 16010 }
0d99eb77
DE
16011 else
16012 {
16013 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16014 if (cu->per_cu->is_debug_types)
0d99eb77
DE
16015 {
16016 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16017 " external reference to offset 0x%lx [in module %s].\n"),
16018 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16019 bfd_get_filename (objfile->obfd));
16020 }
36586728
TT
16021 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16022 objfile);
72bf9492 16023
0d99eb77
DE
16024 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16025 load_partial_comp_unit (per_cu);
ae038cb0 16026
0d99eb77
DE
16027 per_cu->cu->last_used = 0;
16028 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16029 }
5afb4e99 16030
dee91e82
DE
16031 /* If we didn't find it, and not all dies have been loaded,
16032 load them all and try again. */
16033
5afb4e99
DJ
16034 if (pd == NULL && per_cu->load_all_dies == 0)
16035 {
5afb4e99 16036 per_cu->load_all_dies = 1;
fd820528
DE
16037
16038 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16039 THIS_CU->cu may already be in use. So we can't just free it and
16040 replace its DIEs with the ones we read in. Instead, we leave those
16041 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16042 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16043 set. */
dee91e82 16044 load_partial_comp_unit (per_cu);
5afb4e99
DJ
16045
16046 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16047 }
16048
16049 if (pd == NULL)
16050 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16051 _("could not find partial DIE 0x%x "
16052 "in cache [from module %s]\n"),
b64f50a1 16053 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 16054 return pd;
72bf9492
DJ
16055}
16056
abc72ce4
DE
16057/* See if we can figure out if the class lives in a namespace. We do
16058 this by looking for a member function; its demangled name will
16059 contain namespace info, if there is any. */
16060
16061static void
16062guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16063 struct dwarf2_cu *cu)
16064{
16065 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16066 what template types look like, because the demangler
16067 frequently doesn't give the same name as the debug info. We
16068 could fix this by only using the demangled name to get the
16069 prefix (but see comment in read_structure_type). */
16070
16071 struct partial_die_info *real_pdi;
16072 struct partial_die_info *child_pdi;
16073
16074 /* If this DIE (this DIE's specification, if any) has a parent, then
16075 we should not do this. We'll prepend the parent's fully qualified
16076 name when we create the partial symbol. */
16077
16078 real_pdi = struct_pdi;
16079 while (real_pdi->has_specification)
36586728
TT
16080 real_pdi = find_partial_die (real_pdi->spec_offset,
16081 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16082
16083 if (real_pdi->die_parent != NULL)
16084 return;
16085
16086 for (child_pdi = struct_pdi->die_child;
16087 child_pdi != NULL;
16088 child_pdi = child_pdi->die_sibling)
16089 {
16090 if (child_pdi->tag == DW_TAG_subprogram
16091 && child_pdi->linkage_name != NULL)
16092 {
16093 char *actual_class_name
16094 = language_class_name_from_physname (cu->language_defn,
16095 child_pdi->linkage_name);
16096 if (actual_class_name != NULL)
16097 {
16098 struct_pdi->name
34a68019 16099 = obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
10f0c4bb
TT
16100 actual_class_name,
16101 strlen (actual_class_name));
abc72ce4
DE
16102 xfree (actual_class_name);
16103 }
16104 break;
16105 }
16106 }
16107}
16108
72bf9492
DJ
16109/* Adjust PART_DIE before generating a symbol for it. This function
16110 may set the is_external flag or change the DIE's name. */
16111
16112static void
16113fixup_partial_die (struct partial_die_info *part_die,
16114 struct dwarf2_cu *cu)
16115{
abc72ce4
DE
16116 /* Once we've fixed up a die, there's no point in doing so again.
16117 This also avoids a memory leak if we were to call
16118 guess_partial_die_structure_name multiple times. */
16119 if (part_die->fixup_called)
16120 return;
16121
72bf9492
DJ
16122 /* If we found a reference attribute and the DIE has no name, try
16123 to find a name in the referred to DIE. */
16124
16125 if (part_die->name == NULL && part_die->has_specification)
16126 {
16127 struct partial_die_info *spec_die;
72bf9492 16128
36586728
TT
16129 spec_die = find_partial_die (part_die->spec_offset,
16130 part_die->spec_is_dwz, cu);
72bf9492 16131
10b3939b 16132 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16133
16134 if (spec_die->name)
16135 {
16136 part_die->name = spec_die->name;
16137
16138 /* Copy DW_AT_external attribute if it is set. */
16139 if (spec_die->is_external)
16140 part_die->is_external = spec_die->is_external;
16141 }
16142 }
16143
16144 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16145
16146 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16147 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16148
abc72ce4
DE
16149 /* If there is no parent die to provide a namespace, and there are
16150 children, see if we can determine the namespace from their linkage
122d1940 16151 name. */
abc72ce4 16152 if (cu->language == language_cplus
8b70b953 16153 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16154 && part_die->die_parent == NULL
16155 && part_die->has_children
16156 && (part_die->tag == DW_TAG_class_type
16157 || part_die->tag == DW_TAG_structure_type
16158 || part_die->tag == DW_TAG_union_type))
16159 guess_partial_die_structure_name (part_die, cu);
16160
53832f31
TT
16161 /* GCC might emit a nameless struct or union that has a linkage
16162 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16163 if (part_die->name == NULL
96408a79
SA
16164 && (part_die->tag == DW_TAG_class_type
16165 || part_die->tag == DW_TAG_interface_type
16166 || part_die->tag == DW_TAG_structure_type
16167 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16168 && part_die->linkage_name != NULL)
16169 {
16170 char *demangled;
16171
8de20a37 16172 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16173 if (demangled)
16174 {
96408a79
SA
16175 const char *base;
16176
16177 /* Strip any leading namespaces/classes, keep only the base name.
16178 DW_AT_name for named DIEs does not contain the prefixes. */
16179 base = strrchr (demangled, ':');
16180 if (base && base > demangled && base[-1] == ':')
16181 base++;
16182 else
16183 base = demangled;
16184
34a68019
TT
16185 part_die->name
16186 = obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16187 base, strlen (base));
53832f31
TT
16188 xfree (demangled);
16189 }
16190 }
16191
abc72ce4 16192 part_die->fixup_called = 1;
72bf9492
DJ
16193}
16194
a8329558 16195/* Read an attribute value described by an attribute form. */
c906108c 16196
d521ce57 16197static const gdb_byte *
dee91e82
DE
16198read_attribute_value (const struct die_reader_specs *reader,
16199 struct attribute *attr, unsigned form,
d521ce57 16200 const gdb_byte *info_ptr)
c906108c 16201{
dee91e82 16202 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16203 struct objfile *objfile = cu->objfile;
16204 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16205 bfd *abfd = reader->abfd;
e7c27a73 16206 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16207 unsigned int bytes_read;
16208 struct dwarf_block *blk;
16209
aead7601 16210 attr->form = (enum dwarf_form) form;
a8329558 16211 switch (form)
c906108c 16212 {
c906108c 16213 case DW_FORM_ref_addr:
ae411497 16214 if (cu->header.version == 2)
4568ecf9 16215 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16216 else
4568ecf9
DE
16217 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16218 &cu->header, &bytes_read);
ae411497
TT
16219 info_ptr += bytes_read;
16220 break;
36586728
TT
16221 case DW_FORM_GNU_ref_alt:
16222 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16223 info_ptr += bytes_read;
16224 break;
ae411497 16225 case DW_FORM_addr:
e7c27a73 16226 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16227 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16228 info_ptr += bytes_read;
c906108c
SS
16229 break;
16230 case DW_FORM_block2:
7b5a2f43 16231 blk = dwarf_alloc_block (cu);
c906108c
SS
16232 blk->size = read_2_bytes (abfd, info_ptr);
16233 info_ptr += 2;
16234 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16235 info_ptr += blk->size;
16236 DW_BLOCK (attr) = blk;
16237 break;
16238 case DW_FORM_block4:
7b5a2f43 16239 blk = dwarf_alloc_block (cu);
c906108c
SS
16240 blk->size = read_4_bytes (abfd, info_ptr);
16241 info_ptr += 4;
16242 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16243 info_ptr += blk->size;
16244 DW_BLOCK (attr) = blk;
16245 break;
16246 case DW_FORM_data2:
16247 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16248 info_ptr += 2;
16249 break;
16250 case DW_FORM_data4:
16251 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16252 info_ptr += 4;
16253 break;
16254 case DW_FORM_data8:
16255 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16256 info_ptr += 8;
16257 break;
2dc7f7b3
TT
16258 case DW_FORM_sec_offset:
16259 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16260 info_ptr += bytes_read;
16261 break;
c906108c 16262 case DW_FORM_string:
9b1c24c8 16263 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16264 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16265 info_ptr += bytes_read;
16266 break;
4bdf3d34 16267 case DW_FORM_strp:
36586728
TT
16268 if (!cu->per_cu->is_dwz)
16269 {
16270 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16271 &bytes_read);
16272 DW_STRING_IS_CANONICAL (attr) = 0;
16273 info_ptr += bytes_read;
16274 break;
16275 }
16276 /* FALLTHROUGH */
16277 case DW_FORM_GNU_strp_alt:
16278 {
16279 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16280 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16281 &bytes_read);
16282
16283 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16284 DW_STRING_IS_CANONICAL (attr) = 0;
16285 info_ptr += bytes_read;
16286 }
4bdf3d34 16287 break;
2dc7f7b3 16288 case DW_FORM_exprloc:
c906108c 16289 case DW_FORM_block:
7b5a2f43 16290 blk = dwarf_alloc_block (cu);
c906108c
SS
16291 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16292 info_ptr += bytes_read;
16293 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16294 info_ptr += blk->size;
16295 DW_BLOCK (attr) = blk;
16296 break;
16297 case DW_FORM_block1:
7b5a2f43 16298 blk = dwarf_alloc_block (cu);
c906108c
SS
16299 blk->size = read_1_byte (abfd, info_ptr);
16300 info_ptr += 1;
16301 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16302 info_ptr += blk->size;
16303 DW_BLOCK (attr) = blk;
16304 break;
16305 case DW_FORM_data1:
16306 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16307 info_ptr += 1;
16308 break;
16309 case DW_FORM_flag:
16310 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16311 info_ptr += 1;
16312 break;
2dc7f7b3
TT
16313 case DW_FORM_flag_present:
16314 DW_UNSND (attr) = 1;
16315 break;
c906108c
SS
16316 case DW_FORM_sdata:
16317 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16318 info_ptr += bytes_read;
16319 break;
16320 case DW_FORM_udata:
16321 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16322 info_ptr += bytes_read;
16323 break;
16324 case DW_FORM_ref1:
4568ecf9
DE
16325 DW_UNSND (attr) = (cu->header.offset.sect_off
16326 + read_1_byte (abfd, info_ptr));
c906108c
SS
16327 info_ptr += 1;
16328 break;
16329 case DW_FORM_ref2:
4568ecf9
DE
16330 DW_UNSND (attr) = (cu->header.offset.sect_off
16331 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16332 info_ptr += 2;
16333 break;
16334 case DW_FORM_ref4:
4568ecf9
DE
16335 DW_UNSND (attr) = (cu->header.offset.sect_off
16336 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16337 info_ptr += 4;
16338 break;
613e1657 16339 case DW_FORM_ref8:
4568ecf9
DE
16340 DW_UNSND (attr) = (cu->header.offset.sect_off
16341 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16342 info_ptr += 8;
16343 break;
55f1336d 16344 case DW_FORM_ref_sig8:
ac9ec31b 16345 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16346 info_ptr += 8;
16347 break;
c906108c 16348 case DW_FORM_ref_udata:
4568ecf9
DE
16349 DW_UNSND (attr) = (cu->header.offset.sect_off
16350 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16351 info_ptr += bytes_read;
16352 break;
c906108c 16353 case DW_FORM_indirect:
a8329558
KW
16354 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16355 info_ptr += bytes_read;
dee91e82 16356 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 16357 break;
3019eac3
DE
16358 case DW_FORM_GNU_addr_index:
16359 if (reader->dwo_file == NULL)
16360 {
16361 /* For now flag a hard error.
16362 Later we can turn this into a complaint. */
16363 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16364 dwarf_form_name (form),
16365 bfd_get_filename (abfd));
16366 }
16367 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16368 info_ptr += bytes_read;
16369 break;
16370 case DW_FORM_GNU_str_index:
16371 if (reader->dwo_file == NULL)
16372 {
16373 /* For now flag a hard error.
16374 Later we can turn this into a complaint if warranted. */
16375 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16376 dwarf_form_name (form),
16377 bfd_get_filename (abfd));
16378 }
16379 {
16380 ULONGEST str_index =
16381 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16382
342587c4 16383 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16384 DW_STRING_IS_CANONICAL (attr) = 0;
16385 info_ptr += bytes_read;
16386 }
16387 break;
c906108c 16388 default:
8a3fe4f8 16389 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16390 dwarf_form_name (form),
16391 bfd_get_filename (abfd));
c906108c 16392 }
28e94949 16393
36586728 16394 /* Super hack. */
7771576e 16395 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16396 attr->form = DW_FORM_GNU_ref_alt;
16397
28e94949
JB
16398 /* We have seen instances where the compiler tried to emit a byte
16399 size attribute of -1 which ended up being encoded as an unsigned
16400 0xffffffff. Although 0xffffffff is technically a valid size value,
16401 an object of this size seems pretty unlikely so we can relatively
16402 safely treat these cases as if the size attribute was invalid and
16403 treat them as zero by default. */
16404 if (attr->name == DW_AT_byte_size
16405 && form == DW_FORM_data4
16406 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16407 {
16408 complaint
16409 (&symfile_complaints,
43bbcdc2
PH
16410 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16411 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16412 DW_UNSND (attr) = 0;
16413 }
28e94949 16414
c906108c
SS
16415 return info_ptr;
16416}
16417
a8329558
KW
16418/* Read an attribute described by an abbreviated attribute. */
16419
d521ce57 16420static const gdb_byte *
dee91e82
DE
16421read_attribute (const struct die_reader_specs *reader,
16422 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16423 const gdb_byte *info_ptr)
a8329558
KW
16424{
16425 attr->name = abbrev->name;
dee91e82 16426 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
16427}
16428
0963b4bd 16429/* Read dwarf information from a buffer. */
c906108c
SS
16430
16431static unsigned int
a1855c1d 16432read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16433{
fe1b8b76 16434 return bfd_get_8 (abfd, buf);
c906108c
SS
16435}
16436
16437static int
a1855c1d 16438read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16439{
fe1b8b76 16440 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16441}
16442
16443static unsigned int
a1855c1d 16444read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16445{
fe1b8b76 16446 return bfd_get_16 (abfd, buf);
c906108c
SS
16447}
16448
21ae7a4d 16449static int
a1855c1d 16450read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16451{
16452 return bfd_get_signed_16 (abfd, buf);
16453}
16454
c906108c 16455static unsigned int
a1855c1d 16456read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16457{
fe1b8b76 16458 return bfd_get_32 (abfd, buf);
c906108c
SS
16459}
16460
21ae7a4d 16461static int
a1855c1d 16462read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16463{
16464 return bfd_get_signed_32 (abfd, buf);
16465}
16466
93311388 16467static ULONGEST
a1855c1d 16468read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16469{
fe1b8b76 16470 return bfd_get_64 (abfd, buf);
c906108c
SS
16471}
16472
16473static CORE_ADDR
d521ce57 16474read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16475 unsigned int *bytes_read)
c906108c 16476{
e7c27a73 16477 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16478 CORE_ADDR retval = 0;
16479
107d2387 16480 if (cu_header->signed_addr_p)
c906108c 16481 {
107d2387
AC
16482 switch (cu_header->addr_size)
16483 {
16484 case 2:
fe1b8b76 16485 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16486 break;
16487 case 4:
fe1b8b76 16488 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16489 break;
16490 case 8:
fe1b8b76 16491 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16492 break;
16493 default:
8e65ff28 16494 internal_error (__FILE__, __LINE__,
e2e0b3e5 16495 _("read_address: bad switch, signed [in module %s]"),
659b0389 16496 bfd_get_filename (abfd));
107d2387
AC
16497 }
16498 }
16499 else
16500 {
16501 switch (cu_header->addr_size)
16502 {
16503 case 2:
fe1b8b76 16504 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16505 break;
16506 case 4:
fe1b8b76 16507 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16508 break;
16509 case 8:
fe1b8b76 16510 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16511 break;
16512 default:
8e65ff28 16513 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16514 _("read_address: bad switch, "
16515 "unsigned [in module %s]"),
659b0389 16516 bfd_get_filename (abfd));
107d2387 16517 }
c906108c 16518 }
64367e0a 16519
107d2387
AC
16520 *bytes_read = cu_header->addr_size;
16521 return retval;
c906108c
SS
16522}
16523
f7ef9339 16524/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
16525 specification allows the initial length to take up either 4 bytes
16526 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16527 bytes describe the length and all offsets will be 8 bytes in length
16528 instead of 4.
16529
f7ef9339
KB
16530 An older, non-standard 64-bit format is also handled by this
16531 function. The older format in question stores the initial length
16532 as an 8-byte quantity without an escape value. Lengths greater
16533 than 2^32 aren't very common which means that the initial 4 bytes
16534 is almost always zero. Since a length value of zero doesn't make
16535 sense for the 32-bit format, this initial zero can be considered to
16536 be an escape value which indicates the presence of the older 64-bit
16537 format. As written, the code can't detect (old format) lengths
917c78fc
MK
16538 greater than 4GB. If it becomes necessary to handle lengths
16539 somewhat larger than 4GB, we could allow other small values (such
16540 as the non-sensical values of 1, 2, and 3) to also be used as
16541 escape values indicating the presence of the old format.
f7ef9339 16542
917c78fc
MK
16543 The value returned via bytes_read should be used to increment the
16544 relevant pointer after calling read_initial_length().
c764a876 16545
613e1657
KB
16546 [ Note: read_initial_length() and read_offset() are based on the
16547 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16548 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16549 from:
16550
f7ef9339 16551 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16552
613e1657
KB
16553 This document is only a draft and is subject to change. (So beware.)
16554
f7ef9339 16555 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16556 determined empirically by examining 64-bit ELF files produced by
16557 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16558
16559 - Kevin, July 16, 2002
613e1657
KB
16560 ] */
16561
16562static LONGEST
d521ce57 16563read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16564{
fe1b8b76 16565 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16566
dd373385 16567 if (length == 0xffffffff)
613e1657 16568 {
fe1b8b76 16569 length = bfd_get_64 (abfd, buf + 4);
613e1657 16570 *bytes_read = 12;
613e1657 16571 }
dd373385 16572 else if (length == 0)
f7ef9339 16573 {
dd373385 16574 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16575 length = bfd_get_64 (abfd, buf);
f7ef9339 16576 *bytes_read = 8;
f7ef9339 16577 }
613e1657
KB
16578 else
16579 {
16580 *bytes_read = 4;
613e1657
KB
16581 }
16582
c764a876
DE
16583 return length;
16584}
dd373385 16585
c764a876
DE
16586/* Cover function for read_initial_length.
16587 Returns the length of the object at BUF, and stores the size of the
16588 initial length in *BYTES_READ and stores the size that offsets will be in
16589 *OFFSET_SIZE.
16590 If the initial length size is not equivalent to that specified in
16591 CU_HEADER then issue a complaint.
16592 This is useful when reading non-comp-unit headers. */
dd373385 16593
c764a876 16594static LONGEST
d521ce57 16595read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
16596 const struct comp_unit_head *cu_header,
16597 unsigned int *bytes_read,
16598 unsigned int *offset_size)
16599{
16600 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16601
16602 gdb_assert (cu_header->initial_length_size == 4
16603 || cu_header->initial_length_size == 8
16604 || cu_header->initial_length_size == 12);
16605
16606 if (cu_header->initial_length_size != *bytes_read)
16607 complaint (&symfile_complaints,
16608 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 16609
c764a876 16610 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 16611 return length;
613e1657
KB
16612}
16613
16614/* Read an offset from the data stream. The size of the offset is
917c78fc 16615 given by cu_header->offset_size. */
613e1657
KB
16616
16617static LONGEST
d521ce57
TT
16618read_offset (bfd *abfd, const gdb_byte *buf,
16619 const struct comp_unit_head *cu_header,
891d2f0b 16620 unsigned int *bytes_read)
c764a876
DE
16621{
16622 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 16623
c764a876
DE
16624 *bytes_read = cu_header->offset_size;
16625 return offset;
16626}
16627
16628/* Read an offset from the data stream. */
16629
16630static LONGEST
d521ce57 16631read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
16632{
16633 LONGEST retval = 0;
16634
c764a876 16635 switch (offset_size)
613e1657
KB
16636 {
16637 case 4:
fe1b8b76 16638 retval = bfd_get_32 (abfd, buf);
613e1657
KB
16639 break;
16640 case 8:
fe1b8b76 16641 retval = bfd_get_64 (abfd, buf);
613e1657
KB
16642 break;
16643 default:
8e65ff28 16644 internal_error (__FILE__, __LINE__,
c764a876 16645 _("read_offset_1: bad switch [in module %s]"),
659b0389 16646 bfd_get_filename (abfd));
613e1657
KB
16647 }
16648
917c78fc 16649 return retval;
613e1657
KB
16650}
16651
d521ce57
TT
16652static const gdb_byte *
16653read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
16654{
16655 /* If the size of a host char is 8 bits, we can return a pointer
16656 to the buffer, otherwise we have to copy the data to a buffer
16657 allocated on the temporary obstack. */
4bdf3d34 16658 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 16659 return buf;
c906108c
SS
16660}
16661
d521ce57
TT
16662static const char *
16663read_direct_string (bfd *abfd, const gdb_byte *buf,
16664 unsigned int *bytes_read_ptr)
c906108c
SS
16665{
16666 /* If the size of a host char is 8 bits, we can return a pointer
16667 to the string, otherwise we have to copy the string to a buffer
16668 allocated on the temporary obstack. */
4bdf3d34 16669 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
16670 if (*buf == '\0')
16671 {
16672 *bytes_read_ptr = 1;
16673 return NULL;
16674 }
d521ce57
TT
16675 *bytes_read_ptr = strlen ((const char *) buf) + 1;
16676 return (const char *) buf;
4bdf3d34
JJ
16677}
16678
d521ce57 16679static const char *
cf2c3c16 16680read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 16681{
be391dca 16682 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 16683 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
16684 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
16685 bfd_get_filename (abfd));
dce234bc 16686 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
16687 error (_("DW_FORM_strp pointing outside of "
16688 ".debug_str section [in module %s]"),
16689 bfd_get_filename (abfd));
4bdf3d34 16690 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 16691 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 16692 return NULL;
d521ce57 16693 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
16694}
16695
36586728
TT
16696/* Read a string at offset STR_OFFSET in the .debug_str section from
16697 the .dwz file DWZ. Throw an error if the offset is too large. If
16698 the string consists of a single NUL byte, return NULL; otherwise
16699 return a pointer to the string. */
16700
d521ce57 16701static const char *
36586728
TT
16702read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
16703{
16704 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
16705
16706 if (dwz->str.buffer == NULL)
16707 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
16708 "section [in module %s]"),
16709 bfd_get_filename (dwz->dwz_bfd));
16710 if (str_offset >= dwz->str.size)
16711 error (_("DW_FORM_GNU_strp_alt pointing outside of "
16712 ".debug_str section [in module %s]"),
16713 bfd_get_filename (dwz->dwz_bfd));
16714 gdb_assert (HOST_CHAR_BIT == 8);
16715 if (dwz->str.buffer[str_offset] == '\0')
16716 return NULL;
d521ce57 16717 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
16718}
16719
d521ce57
TT
16720static const char *
16721read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
16722 const struct comp_unit_head *cu_header,
16723 unsigned int *bytes_read_ptr)
16724{
16725 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
16726
16727 return read_indirect_string_at_offset (abfd, str_offset);
16728}
16729
12df843f 16730static ULONGEST
d521ce57
TT
16731read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
16732 unsigned int *bytes_read_ptr)
c906108c 16733{
12df843f 16734 ULONGEST result;
ce5d95e1 16735 unsigned int num_read;
c906108c
SS
16736 int i, shift;
16737 unsigned char byte;
16738
16739 result = 0;
16740 shift = 0;
16741 num_read = 0;
16742 i = 0;
16743 while (1)
16744 {
fe1b8b76 16745 byte = bfd_get_8 (abfd, buf);
c906108c
SS
16746 buf++;
16747 num_read++;
12df843f 16748 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
16749 if ((byte & 128) == 0)
16750 {
16751 break;
16752 }
16753 shift += 7;
16754 }
16755 *bytes_read_ptr = num_read;
16756 return result;
16757}
16758
12df843f 16759static LONGEST
d521ce57
TT
16760read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
16761 unsigned int *bytes_read_ptr)
c906108c 16762{
12df843f 16763 LONGEST result;
77e0b926 16764 int i, shift, num_read;
c906108c
SS
16765 unsigned char byte;
16766
16767 result = 0;
16768 shift = 0;
c906108c
SS
16769 num_read = 0;
16770 i = 0;
16771 while (1)
16772 {
fe1b8b76 16773 byte = bfd_get_8 (abfd, buf);
c906108c
SS
16774 buf++;
16775 num_read++;
12df843f 16776 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
16777 shift += 7;
16778 if ((byte & 128) == 0)
16779 {
16780 break;
16781 }
16782 }
77e0b926 16783 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 16784 result |= -(((LONGEST) 1) << shift);
c906108c
SS
16785 *bytes_read_ptr = num_read;
16786 return result;
16787}
16788
3019eac3
DE
16789/* Given index ADDR_INDEX in .debug_addr, fetch the value.
16790 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
16791 ADDR_SIZE is the size of addresses from the CU header. */
16792
16793static CORE_ADDR
16794read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
16795{
16796 struct objfile *objfile = dwarf2_per_objfile->objfile;
16797 bfd *abfd = objfile->obfd;
16798 const gdb_byte *info_ptr;
16799
16800 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
16801 if (dwarf2_per_objfile->addr.buffer == NULL)
16802 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 16803 objfile_name (objfile));
3019eac3
DE
16804 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
16805 error (_("DW_FORM_addr_index pointing outside of "
16806 ".debug_addr section [in module %s]"),
4262abfb 16807 objfile_name (objfile));
3019eac3
DE
16808 info_ptr = (dwarf2_per_objfile->addr.buffer
16809 + addr_base + addr_index * addr_size);
16810 if (addr_size == 4)
16811 return bfd_get_32 (abfd, info_ptr);
16812 else
16813 return bfd_get_64 (abfd, info_ptr);
16814}
16815
16816/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
16817
16818static CORE_ADDR
16819read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
16820{
16821 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
16822}
16823
16824/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
16825
16826static CORE_ADDR
d521ce57 16827read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
16828 unsigned int *bytes_read)
16829{
16830 bfd *abfd = cu->objfile->obfd;
16831 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
16832
16833 return read_addr_index (cu, addr_index);
16834}
16835
16836/* Data structure to pass results from dwarf2_read_addr_index_reader
16837 back to dwarf2_read_addr_index. */
16838
16839struct dwarf2_read_addr_index_data
16840{
16841 ULONGEST addr_base;
16842 int addr_size;
16843};
16844
16845/* die_reader_func for dwarf2_read_addr_index. */
16846
16847static void
16848dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 16849 const gdb_byte *info_ptr,
3019eac3
DE
16850 struct die_info *comp_unit_die,
16851 int has_children,
16852 void *data)
16853{
16854 struct dwarf2_cu *cu = reader->cu;
16855 struct dwarf2_read_addr_index_data *aidata =
16856 (struct dwarf2_read_addr_index_data *) data;
16857
16858 aidata->addr_base = cu->addr_base;
16859 aidata->addr_size = cu->header.addr_size;
16860}
16861
16862/* Given an index in .debug_addr, fetch the value.
16863 NOTE: This can be called during dwarf expression evaluation,
16864 long after the debug information has been read, and thus per_cu->cu
16865 may no longer exist. */
16866
16867CORE_ADDR
16868dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
16869 unsigned int addr_index)
16870{
16871 struct objfile *objfile = per_cu->objfile;
16872 struct dwarf2_cu *cu = per_cu->cu;
16873 ULONGEST addr_base;
16874 int addr_size;
16875
16876 /* This is intended to be called from outside this file. */
16877 dw2_setup (objfile);
16878
16879 /* We need addr_base and addr_size.
16880 If we don't have PER_CU->cu, we have to get it.
16881 Nasty, but the alternative is storing the needed info in PER_CU,
16882 which at this point doesn't seem justified: it's not clear how frequently
16883 it would get used and it would increase the size of every PER_CU.
16884 Entry points like dwarf2_per_cu_addr_size do a similar thing
16885 so we're not in uncharted territory here.
16886 Alas we need to be a bit more complicated as addr_base is contained
16887 in the DIE.
16888
16889 We don't need to read the entire CU(/TU).
16890 We just need the header and top level die.
a1b64ce1 16891
3019eac3 16892 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 16893 For now we skip this optimization. */
3019eac3
DE
16894
16895 if (cu != NULL)
16896 {
16897 addr_base = cu->addr_base;
16898 addr_size = cu->header.addr_size;
16899 }
16900 else
16901 {
16902 struct dwarf2_read_addr_index_data aidata;
16903
a1b64ce1
DE
16904 /* Note: We can't use init_cutu_and_read_dies_simple here,
16905 we need addr_base. */
16906 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
16907 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
16908 addr_base = aidata.addr_base;
16909 addr_size = aidata.addr_size;
16910 }
16911
16912 return read_addr_index_1 (addr_index, addr_base, addr_size);
16913}
16914
57d63ce2
DE
16915/* Given a DW_FORM_GNU_str_index, fetch the string.
16916 This is only used by the Fission support. */
3019eac3 16917
d521ce57 16918static const char *
342587c4 16919read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
16920{
16921 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 16922 const char *objf_name = objfile_name (objfile);
3019eac3 16923 bfd *abfd = objfile->obfd;
342587c4 16924 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
16925 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
16926 struct dwarf2_section_info *str_offsets_section =
16927 &reader->dwo_file->sections.str_offsets;
d521ce57 16928 const gdb_byte *info_ptr;
3019eac3 16929 ULONGEST str_offset;
57d63ce2 16930 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 16931
73869dc2
DE
16932 dwarf2_read_section (objfile, str_section);
16933 dwarf2_read_section (objfile, str_offsets_section);
16934 if (str_section->buffer == NULL)
57d63ce2 16935 error (_("%s used without .debug_str.dwo section"
3019eac3 16936 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 16937 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 16938 if (str_offsets_section->buffer == NULL)
57d63ce2 16939 error (_("%s used without .debug_str_offsets.dwo section"
3019eac3 16940 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 16941 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 16942 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 16943 error (_("%s pointing outside of .debug_str_offsets.dwo"
3019eac3 16944 " section in CU at offset 0x%lx [in module %s]"),
c5164cbc 16945 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 16946 info_ptr = (str_offsets_section->buffer
3019eac3
DE
16947 + str_index * cu->header.offset_size);
16948 if (cu->header.offset_size == 4)
16949 str_offset = bfd_get_32 (abfd, info_ptr);
16950 else
16951 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 16952 if (str_offset >= str_section->size)
57d63ce2 16953 error (_("Offset from %s pointing outside of"
3019eac3 16954 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
c5164cbc 16955 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 16956 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
16957}
16958
3019eac3
DE
16959/* Return the length of an LEB128 number in BUF. */
16960
16961static int
16962leb128_size (const gdb_byte *buf)
16963{
16964 const gdb_byte *begin = buf;
16965 gdb_byte byte;
16966
16967 while (1)
16968 {
16969 byte = *buf++;
16970 if ((byte & 128) == 0)
16971 return buf - begin;
16972 }
16973}
16974
c906108c 16975static void
e142c38c 16976set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
16977{
16978 switch (lang)
16979 {
16980 case DW_LANG_C89:
76bee0cc 16981 case DW_LANG_C99:
0cfd832f 16982 case DW_LANG_C11:
c906108c 16983 case DW_LANG_C:
d1be3247 16984 case DW_LANG_UPC:
e142c38c 16985 cu->language = language_c;
c906108c
SS
16986 break;
16987 case DW_LANG_C_plus_plus:
0cfd832f
MW
16988 case DW_LANG_C_plus_plus_11:
16989 case DW_LANG_C_plus_plus_14:
e142c38c 16990 cu->language = language_cplus;
c906108c 16991 break;
6aecb9c2
JB
16992 case DW_LANG_D:
16993 cu->language = language_d;
16994 break;
c906108c
SS
16995 case DW_LANG_Fortran77:
16996 case DW_LANG_Fortran90:
b21b22e0 16997 case DW_LANG_Fortran95:
f7de9aab
MW
16998 case DW_LANG_Fortran03:
16999 case DW_LANG_Fortran08:
e142c38c 17000 cu->language = language_fortran;
c906108c 17001 break;
a766d390
DE
17002 case DW_LANG_Go:
17003 cu->language = language_go;
17004 break;
c906108c 17005 case DW_LANG_Mips_Assembler:
e142c38c 17006 cu->language = language_asm;
c906108c 17007 break;
bebd888e 17008 case DW_LANG_Java:
e142c38c 17009 cu->language = language_java;
bebd888e 17010 break;
c906108c 17011 case DW_LANG_Ada83:
8aaf0b47 17012 case DW_LANG_Ada95:
bc5f45f8
JB
17013 cu->language = language_ada;
17014 break;
72019c9c
GM
17015 case DW_LANG_Modula2:
17016 cu->language = language_m2;
17017 break;
fe8e67fd
PM
17018 case DW_LANG_Pascal83:
17019 cu->language = language_pascal;
17020 break;
22566fbd
DJ
17021 case DW_LANG_ObjC:
17022 cu->language = language_objc;
17023 break;
c906108c
SS
17024 case DW_LANG_Cobol74:
17025 case DW_LANG_Cobol85:
c906108c 17026 default:
e142c38c 17027 cu->language = language_minimal;
c906108c
SS
17028 break;
17029 }
e142c38c 17030 cu->language_defn = language_def (cu->language);
c906108c
SS
17031}
17032
17033/* Return the named attribute or NULL if not there. */
17034
17035static struct attribute *
e142c38c 17036dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17037{
a48e046c 17038 for (;;)
c906108c 17039 {
a48e046c
TT
17040 unsigned int i;
17041 struct attribute *spec = NULL;
17042
17043 for (i = 0; i < die->num_attrs; ++i)
17044 {
17045 if (die->attrs[i].name == name)
17046 return &die->attrs[i];
17047 if (die->attrs[i].name == DW_AT_specification
17048 || die->attrs[i].name == DW_AT_abstract_origin)
17049 spec = &die->attrs[i];
17050 }
17051
17052 if (!spec)
17053 break;
c906108c 17054
f2f0e013 17055 die = follow_die_ref (die, spec, &cu);
f2f0e013 17056 }
c5aa993b 17057
c906108c
SS
17058 return NULL;
17059}
17060
348e048f
DE
17061/* Return the named attribute or NULL if not there,
17062 but do not follow DW_AT_specification, etc.
17063 This is for use in contexts where we're reading .debug_types dies.
17064 Following DW_AT_specification, DW_AT_abstract_origin will take us
17065 back up the chain, and we want to go down. */
17066
17067static struct attribute *
45e58e77 17068dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17069{
17070 unsigned int i;
17071
17072 for (i = 0; i < die->num_attrs; ++i)
17073 if (die->attrs[i].name == name)
17074 return &die->attrs[i];
17075
17076 return NULL;
17077}
17078
7d45c7c3
KB
17079/* Return the string associated with a string-typed attribute, or NULL if it
17080 is either not found or is of an incorrect type. */
17081
17082static const char *
17083dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17084{
17085 struct attribute *attr;
17086 const char *str = NULL;
17087
17088 attr = dwarf2_attr (die, name, cu);
17089
17090 if (attr != NULL)
17091 {
17092 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_string
17093 || attr->form == DW_FORM_GNU_strp_alt)
17094 str = DW_STRING (attr);
17095 else
17096 complaint (&symfile_complaints,
17097 _("string type expected for attribute %s for "
17098 "DIE at 0x%x in module %s"),
17099 dwarf_attr_name (name), die->offset.sect_off,
17100 objfile_name (cu->objfile));
17101 }
17102
17103 return str;
17104}
17105
05cf31d1
JB
17106/* Return non-zero iff the attribute NAME is defined for the given DIE,
17107 and holds a non-zero value. This function should only be used for
2dc7f7b3 17108 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17109
17110static int
17111dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17112{
17113 struct attribute *attr = dwarf2_attr (die, name, cu);
17114
17115 return (attr && DW_UNSND (attr));
17116}
17117
3ca72b44 17118static int
e142c38c 17119die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17120{
05cf31d1
JB
17121 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17122 which value is non-zero. However, we have to be careful with
17123 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17124 (via dwarf2_flag_true_p) follows this attribute. So we may
17125 end up accidently finding a declaration attribute that belongs
17126 to a different DIE referenced by the specification attribute,
17127 even though the given DIE does not have a declaration attribute. */
17128 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17129 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17130}
17131
63d06c5c 17132/* Return the die giving the specification for DIE, if there is
f2f0e013 17133 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17134 containing the return value on output. If there is no
17135 specification, but there is an abstract origin, that is
17136 returned. */
63d06c5c
DC
17137
17138static struct die_info *
f2f0e013 17139die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17140{
f2f0e013
DJ
17141 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17142 *spec_cu);
63d06c5c 17143
edb3359d
DJ
17144 if (spec_attr == NULL)
17145 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17146
63d06c5c
DC
17147 if (spec_attr == NULL)
17148 return NULL;
17149 else
f2f0e013 17150 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17151}
c906108c 17152
debd256d 17153/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
17154 refers to.
17155 NOTE: This is also used as a "cleanup" function. */
17156
debd256d
JB
17157static void
17158free_line_header (struct line_header *lh)
17159{
17160 if (lh->standard_opcode_lengths)
a8bc7b56 17161 xfree (lh->standard_opcode_lengths);
debd256d
JB
17162
17163 /* Remember that all the lh->file_names[i].name pointers are
17164 pointers into debug_line_buffer, and don't need to be freed. */
17165 if (lh->file_names)
a8bc7b56 17166 xfree (lh->file_names);
debd256d
JB
17167
17168 /* Similarly for the include directory names. */
17169 if (lh->include_dirs)
a8bc7b56 17170 xfree (lh->include_dirs);
debd256d 17171
a8bc7b56 17172 xfree (lh);
debd256d
JB
17173}
17174
527f3840
JK
17175/* Stub for free_line_header to match void * callback types. */
17176
17177static void
17178free_line_header_voidp (void *arg)
17179{
17180 struct line_header *lh = arg;
17181
17182 free_line_header (lh);
17183}
17184
debd256d 17185/* Add an entry to LH's include directory table. */
ae2de4f8 17186
debd256d 17187static void
d521ce57 17188add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 17189{
27e0867f
DE
17190 if (dwarf_line_debug >= 2)
17191 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17192 lh->num_include_dirs + 1, include_dir);
17193
debd256d
JB
17194 /* Grow the array if necessary. */
17195 if (lh->include_dirs_size == 0)
c5aa993b 17196 {
debd256d
JB
17197 lh->include_dirs_size = 1; /* for testing */
17198 lh->include_dirs = xmalloc (lh->include_dirs_size
17199 * sizeof (*lh->include_dirs));
17200 }
17201 else if (lh->num_include_dirs >= lh->include_dirs_size)
17202 {
17203 lh->include_dirs_size *= 2;
17204 lh->include_dirs = xrealloc (lh->include_dirs,
17205 (lh->include_dirs_size
17206 * sizeof (*lh->include_dirs)));
c5aa993b 17207 }
c906108c 17208
debd256d
JB
17209 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17210}
6e70227d 17211
debd256d 17212/* Add an entry to LH's file name table. */
ae2de4f8 17213
debd256d
JB
17214static void
17215add_file_name (struct line_header *lh,
d521ce57 17216 const char *name,
debd256d
JB
17217 unsigned int dir_index,
17218 unsigned int mod_time,
17219 unsigned int length)
17220{
17221 struct file_entry *fe;
17222
27e0867f
DE
17223 if (dwarf_line_debug >= 2)
17224 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17225 lh->num_file_names + 1, name);
17226
debd256d
JB
17227 /* Grow the array if necessary. */
17228 if (lh->file_names_size == 0)
17229 {
17230 lh->file_names_size = 1; /* for testing */
17231 lh->file_names = xmalloc (lh->file_names_size
17232 * sizeof (*lh->file_names));
17233 }
17234 else if (lh->num_file_names >= lh->file_names_size)
17235 {
17236 lh->file_names_size *= 2;
17237 lh->file_names = xrealloc (lh->file_names,
17238 (lh->file_names_size
17239 * sizeof (*lh->file_names)));
17240 }
17241
17242 fe = &lh->file_names[lh->num_file_names++];
17243 fe->name = name;
17244 fe->dir_index = dir_index;
17245 fe->mod_time = mod_time;
17246 fe->length = length;
aaa75496 17247 fe->included_p = 0;
cb1df416 17248 fe->symtab = NULL;
debd256d 17249}
6e70227d 17250
83769d0b 17251/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17252
17253static struct dwarf2_section_info *
17254get_debug_line_section (struct dwarf2_cu *cu)
17255{
17256 struct dwarf2_section_info *section;
17257
17258 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17259 DWO file. */
17260 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17261 section = &cu->dwo_unit->dwo_file->sections.line;
17262 else if (cu->per_cu->is_dwz)
17263 {
17264 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17265
17266 section = &dwz->line;
17267 }
17268 else
17269 section = &dwarf2_per_objfile->line;
17270
17271 return section;
17272}
17273
debd256d 17274/* Read the statement program header starting at OFFSET in
3019eac3 17275 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17276 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17277 Returns NULL if there is a problem reading the header, e.g., if it
17278 has a version we don't understand.
debd256d
JB
17279
17280 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17281 the returned object point into the dwarf line section buffer,
17282 and must not be freed. */
ae2de4f8 17283
debd256d 17284static struct line_header *
3019eac3 17285dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
17286{
17287 struct cleanup *back_to;
17288 struct line_header *lh;
d521ce57 17289 const gdb_byte *line_ptr;
c764a876 17290 unsigned int bytes_read, offset_size;
debd256d 17291 int i;
d521ce57 17292 const char *cur_dir, *cur_file;
3019eac3
DE
17293 struct dwarf2_section_info *section;
17294 bfd *abfd;
17295
36586728 17296 section = get_debug_line_section (cu);
3019eac3
DE
17297 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17298 if (section->buffer == NULL)
debd256d 17299 {
3019eac3
DE
17300 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17301 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17302 else
17303 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17304 return 0;
17305 }
17306
fceca515
DE
17307 /* We can't do this until we know the section is non-empty.
17308 Only then do we know we have such a section. */
a32a8923 17309 abfd = get_section_bfd_owner (section);
fceca515 17310
a738430d
MK
17311 /* Make sure that at least there's room for the total_length field.
17312 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 17313 if (offset + 4 >= section->size)
debd256d 17314 {
4d3c2250 17315 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17316 return 0;
17317 }
17318
17319 lh = xmalloc (sizeof (*lh));
17320 memset (lh, 0, sizeof (*lh));
17321 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17322 (void *) lh);
17323
527f3840
JK
17324 lh->offset.sect_off = offset;
17325 lh->offset_in_dwz = cu->per_cu->is_dwz;
17326
3019eac3 17327 line_ptr = section->buffer + offset;
debd256d 17328
a738430d 17329 /* Read in the header. */
6e70227d 17330 lh->total_length =
c764a876
DE
17331 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17332 &bytes_read, &offset_size);
debd256d 17333 line_ptr += bytes_read;
3019eac3 17334 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17335 {
4d3c2250 17336 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 17337 do_cleanups (back_to);
debd256d
JB
17338 return 0;
17339 }
17340 lh->statement_program_end = line_ptr + lh->total_length;
17341 lh->version = read_2_bytes (abfd, line_ptr);
17342 line_ptr += 2;
cd366ee8
DE
17343 if (lh->version > 4)
17344 {
17345 /* This is a version we don't understand. The format could have
17346 changed in ways we don't handle properly so just punt. */
17347 complaint (&symfile_complaints,
17348 _("unsupported version in .debug_line section"));
17349 return NULL;
17350 }
c764a876
DE
17351 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17352 line_ptr += offset_size;
debd256d
JB
17353 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17354 line_ptr += 1;
2dc7f7b3
TT
17355 if (lh->version >= 4)
17356 {
17357 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17358 line_ptr += 1;
17359 }
17360 else
17361 lh->maximum_ops_per_instruction = 1;
17362
17363 if (lh->maximum_ops_per_instruction == 0)
17364 {
17365 lh->maximum_ops_per_instruction = 1;
17366 complaint (&symfile_complaints,
3e43a32a
MS
17367 _("invalid maximum_ops_per_instruction "
17368 "in `.debug_line' section"));
2dc7f7b3
TT
17369 }
17370
debd256d
JB
17371 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17372 line_ptr += 1;
17373 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17374 line_ptr += 1;
17375 lh->line_range = read_1_byte (abfd, line_ptr);
17376 line_ptr += 1;
17377 lh->opcode_base = read_1_byte (abfd, line_ptr);
17378 line_ptr += 1;
17379 lh->standard_opcode_lengths
fe1b8b76 17380 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
17381
17382 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17383 for (i = 1; i < lh->opcode_base; ++i)
17384 {
17385 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17386 line_ptr += 1;
17387 }
17388
a738430d 17389 /* Read directory table. */
9b1c24c8 17390 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
17391 {
17392 line_ptr += bytes_read;
17393 add_include_dir (lh, cur_dir);
17394 }
17395 line_ptr += bytes_read;
17396
a738430d 17397 /* Read file name table. */
9b1c24c8 17398 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
17399 {
17400 unsigned int dir_index, mod_time, length;
17401
17402 line_ptr += bytes_read;
17403 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17404 line_ptr += bytes_read;
17405 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17406 line_ptr += bytes_read;
17407 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17408 line_ptr += bytes_read;
17409
17410 add_file_name (lh, cur_file, dir_index, mod_time, length);
17411 }
17412 line_ptr += bytes_read;
6e70227d 17413 lh->statement_program_start = line_ptr;
debd256d 17414
3019eac3 17415 if (line_ptr > (section->buffer + section->size))
4d3c2250 17416 complaint (&symfile_complaints,
3e43a32a
MS
17417 _("line number info header doesn't "
17418 "fit in `.debug_line' section"));
debd256d
JB
17419
17420 discard_cleanups (back_to);
17421 return lh;
17422}
c906108c 17423
c6da4cef
DE
17424/* Subroutine of dwarf_decode_lines to simplify it.
17425 Return the file name of the psymtab for included file FILE_INDEX
17426 in line header LH of PST.
17427 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17428 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
17429 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17430
17431 The function creates dangling cleanup registration. */
c6da4cef 17432
d521ce57 17433static const char *
c6da4cef
DE
17434psymtab_include_file_name (const struct line_header *lh, int file_index,
17435 const struct partial_symtab *pst,
17436 const char *comp_dir)
17437{
17438 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
17439 const char *include_name = fe.name;
17440 const char *include_name_to_compare = include_name;
17441 const char *dir_name = NULL;
72b9f47f
TT
17442 const char *pst_filename;
17443 char *copied_name = NULL;
c6da4cef
DE
17444 int file_is_pst;
17445
afa6c9ab 17446 if (fe.dir_index && lh->include_dirs != NULL)
c6da4cef
DE
17447 dir_name = lh->include_dirs[fe.dir_index - 1];
17448
17449 if (!IS_ABSOLUTE_PATH (include_name)
17450 && (dir_name != NULL || comp_dir != NULL))
17451 {
17452 /* Avoid creating a duplicate psymtab for PST.
17453 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17454 Before we do the comparison, however, we need to account
17455 for DIR_NAME and COMP_DIR.
17456 First prepend dir_name (if non-NULL). If we still don't
17457 have an absolute path prepend comp_dir (if non-NULL).
17458 However, the directory we record in the include-file's
17459 psymtab does not contain COMP_DIR (to match the
17460 corresponding symtab(s)).
17461
17462 Example:
17463
17464 bash$ cd /tmp
17465 bash$ gcc -g ./hello.c
17466 include_name = "hello.c"
17467 dir_name = "."
17468 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
17469 DW_AT_name = "./hello.c"
17470
17471 */
c6da4cef
DE
17472
17473 if (dir_name != NULL)
17474 {
d521ce57
TT
17475 char *tem = concat (dir_name, SLASH_STRING,
17476 include_name, (char *)NULL);
17477
17478 make_cleanup (xfree, tem);
17479 include_name = tem;
c6da4cef 17480 include_name_to_compare = include_name;
c6da4cef
DE
17481 }
17482 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
17483 {
d521ce57
TT
17484 char *tem = concat (comp_dir, SLASH_STRING,
17485 include_name, (char *)NULL);
17486
17487 make_cleanup (xfree, tem);
17488 include_name_to_compare = tem;
c6da4cef
DE
17489 }
17490 }
17491
17492 pst_filename = pst->filename;
17493 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
17494 {
72b9f47f
TT
17495 copied_name = concat (pst->dirname, SLASH_STRING,
17496 pst_filename, (char *)NULL);
17497 pst_filename = copied_name;
c6da4cef
DE
17498 }
17499
1e3fad37 17500 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 17501
72b9f47f
TT
17502 if (copied_name != NULL)
17503 xfree (copied_name);
c6da4cef
DE
17504
17505 if (file_is_pst)
17506 return NULL;
17507 return include_name;
17508}
17509
d9b3de22
DE
17510/* State machine to track the state of the line number program. */
17511
17512typedef struct
17513{
17514 /* These are part of the standard DWARF line number state machine. */
17515
17516 unsigned char op_index;
17517 unsigned int file;
17518 unsigned int line;
17519 CORE_ADDR address;
17520 int is_stmt;
17521 unsigned int discriminator;
17522
17523 /* Additional bits of state we need to track. */
17524
17525 /* The last file that we called dwarf2_start_subfile for.
17526 This is only used for TLLs. */
17527 unsigned int last_file;
17528 /* The last file a line number was recorded for. */
17529 struct subfile *last_subfile;
17530
17531 /* The function to call to record a line. */
17532 record_line_ftype *record_line;
17533
17534 /* The last line number that was recorded, used to coalesce
17535 consecutive entries for the same line. This can happen, for
17536 example, when discriminators are present. PR 17276. */
17537 unsigned int last_line;
17538 int line_has_non_zero_discriminator;
17539} lnp_state_machine;
17540
17541/* There's a lot of static state to pass to dwarf_record_line.
17542 This keeps it all together. */
17543
17544typedef struct
17545{
17546 /* The gdbarch. */
17547 struct gdbarch *gdbarch;
17548
17549 /* The line number header. */
17550 struct line_header *line_header;
17551
17552 /* Non-zero if we're recording lines.
17553 Otherwise we're building partial symtabs and are just interested in
17554 finding include files mentioned by the line number program. */
17555 int record_lines_p;
17556} lnp_reader_state;
17557
c91513d8
PP
17558/* Ignore this record_line request. */
17559
17560static void
17561noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
17562{
17563 return;
17564}
17565
a05a36a5
DE
17566/* Return non-zero if we should add LINE to the line number table.
17567 LINE is the line to add, LAST_LINE is the last line that was added,
17568 LAST_SUBFILE is the subfile for LAST_LINE.
17569 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
17570 had a non-zero discriminator.
17571
17572 We have to be careful in the presence of discriminators.
17573 E.g., for this line:
17574
17575 for (i = 0; i < 100000; i++);
17576
17577 clang can emit four line number entries for that one line,
17578 each with a different discriminator.
17579 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
17580
17581 However, we want gdb to coalesce all four entries into one.
17582 Otherwise the user could stepi into the middle of the line and
17583 gdb would get confused about whether the pc really was in the
17584 middle of the line.
17585
17586 Things are further complicated by the fact that two consecutive
17587 line number entries for the same line is a heuristic used by gcc
17588 to denote the end of the prologue. So we can't just discard duplicate
17589 entries, we have to be selective about it. The heuristic we use is
17590 that we only collapse consecutive entries for the same line if at least
17591 one of those entries has a non-zero discriminator. PR 17276.
17592
17593 Note: Addresses in the line number state machine can never go backwards
17594 within one sequence, thus this coalescing is ok. */
17595
17596static int
17597dwarf_record_line_p (unsigned int line, unsigned int last_line,
17598 int line_has_non_zero_discriminator,
17599 struct subfile *last_subfile)
17600{
17601 if (current_subfile != last_subfile)
17602 return 1;
17603 if (line != last_line)
17604 return 1;
17605 /* Same line for the same file that we've seen already.
17606 As a last check, for pr 17276, only record the line if the line
17607 has never had a non-zero discriminator. */
17608 if (!line_has_non_zero_discriminator)
17609 return 1;
17610 return 0;
17611}
17612
252a6764
DE
17613/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
17614 in the line table of subfile SUBFILE. */
17615
17616static void
d9b3de22
DE
17617dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
17618 unsigned int line, CORE_ADDR address,
17619 record_line_ftype p_record_line)
252a6764
DE
17620{
17621 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
17622
27e0867f
DE
17623 if (dwarf_line_debug)
17624 {
17625 fprintf_unfiltered (gdb_stdlog,
17626 "Recording line %u, file %s, address %s\n",
17627 line, lbasename (subfile->name),
17628 paddress (gdbarch, address));
17629 }
17630
d5962de5 17631 (*p_record_line) (subfile, line, addr);
252a6764
DE
17632}
17633
17634/* Subroutine of dwarf_decode_lines_1 to simplify it.
17635 Mark the end of a set of line number records.
d9b3de22 17636 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
17637 If SUBFILE is NULL the request is ignored. */
17638
17639static void
17640dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
17641 CORE_ADDR address, record_line_ftype p_record_line)
17642{
27e0867f
DE
17643 if (subfile == NULL)
17644 return;
17645
17646 if (dwarf_line_debug)
17647 {
17648 fprintf_unfiltered (gdb_stdlog,
17649 "Finishing current line, file %s, address %s\n",
17650 lbasename (subfile->name),
17651 paddress (gdbarch, address));
17652 }
17653
d9b3de22
DE
17654 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
17655}
17656
17657/* Record the line in STATE.
17658 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
17659
17660static void
17661dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
17662 int end_sequence)
17663{
17664 const struct line_header *lh = reader->line_header;
17665 unsigned int file, line, discriminator;
17666 int is_stmt;
17667
17668 file = state->file;
17669 line = state->line;
17670 is_stmt = state->is_stmt;
17671 discriminator = state->discriminator;
17672
17673 if (dwarf_line_debug)
17674 {
17675 fprintf_unfiltered (gdb_stdlog,
17676 "Processing actual line %u: file %u,"
17677 " address %s, is_stmt %u, discrim %u\n",
17678 line, file,
17679 paddress (reader->gdbarch, state->address),
17680 is_stmt, discriminator);
17681 }
17682
17683 if (file == 0 || file - 1 >= lh->num_file_names)
17684 dwarf2_debug_line_missing_file_complaint ();
17685 /* For now we ignore lines not starting on an instruction boundary.
17686 But not when processing end_sequence for compatibility with the
17687 previous version of the code. */
17688 else if (state->op_index == 0 || end_sequence)
17689 {
17690 lh->file_names[file - 1].included_p = 1;
17691 if (reader->record_lines_p && is_stmt)
17692 {
e815d2d2 17693 if (state->last_subfile != current_subfile || end_sequence)
d9b3de22
DE
17694 {
17695 dwarf_finish_line (reader->gdbarch, state->last_subfile,
17696 state->address, state->record_line);
17697 }
17698
17699 if (!end_sequence)
17700 {
17701 if (dwarf_record_line_p (line, state->last_line,
17702 state->line_has_non_zero_discriminator,
17703 state->last_subfile))
17704 {
17705 dwarf_record_line_1 (reader->gdbarch, current_subfile,
17706 line, state->address,
17707 state->record_line);
17708 }
17709 state->last_subfile = current_subfile;
17710 state->last_line = line;
17711 }
17712 }
17713 }
17714}
17715
17716/* Initialize STATE for the start of a line number program. */
17717
17718static void
17719init_lnp_state_machine (lnp_state_machine *state,
17720 const lnp_reader_state *reader)
17721{
17722 memset (state, 0, sizeof (*state));
17723
17724 /* Just starting, there is no "last file". */
17725 state->last_file = 0;
17726 state->last_subfile = NULL;
17727
17728 state->record_line = record_line;
17729
17730 state->last_line = 0;
17731 state->line_has_non_zero_discriminator = 0;
17732
17733 /* Initialize these according to the DWARF spec. */
17734 state->op_index = 0;
17735 state->file = 1;
17736 state->line = 1;
17737 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
17738 was a line entry for it so that the backend has a chance to adjust it
17739 and also record it in case it needs it. This is currently used by MIPS
17740 code, cf. `mips_adjust_dwarf2_line'. */
17741 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
17742 state->is_stmt = reader->line_header->default_is_stmt;
17743 state->discriminator = 0;
252a6764
DE
17744}
17745
924c2928
DE
17746/* Check address and if invalid nop-out the rest of the lines in this
17747 sequence. */
17748
17749static void
d9b3de22 17750check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
924c2928
DE
17751 const gdb_byte *line_ptr,
17752 CORE_ADDR lowpc, CORE_ADDR address)
17753{
17754 /* If address < lowpc then it's not a usable value, it's outside the
17755 pc range of the CU. However, we restrict the test to only address
17756 values of zero to preserve GDB's previous behaviour which is to
17757 handle the specific case of a function being GC'd by the linker. */
17758
17759 if (address == 0 && address < lowpc)
17760 {
17761 /* This line table is for a function which has been
17762 GCd by the linker. Ignore it. PR gdb/12528 */
17763
17764 struct objfile *objfile = cu->objfile;
17765 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
17766
17767 complaint (&symfile_complaints,
17768 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
17769 line_offset, objfile_name (objfile));
d9b3de22
DE
17770 state->record_line = noop_record_line;
17771 /* Note: sm.record_line is left as noop_record_line
924c2928
DE
17772 until we see DW_LNE_end_sequence. */
17773 }
17774}
17775
f3f5162e 17776/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
17777 Process the line number information in LH.
17778 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
17779 program in order to set included_p for every referenced header. */
debd256d 17780
c906108c 17781static void
43f3e411
DE
17782dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
17783 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 17784{
d521ce57
TT
17785 const gdb_byte *line_ptr, *extended_end;
17786 const gdb_byte *line_end;
a8c50c1f 17787 unsigned int bytes_read, extended_len;
699ca60a 17788 unsigned char op_code, extended_op;
e142c38c
DJ
17789 CORE_ADDR baseaddr;
17790 struct objfile *objfile = cu->objfile;
f3f5162e 17791 bfd *abfd = objfile->obfd;
fbf65064 17792 struct gdbarch *gdbarch = get_objfile_arch (objfile);
d9b3de22
DE
17793 /* Non-zero if we're recording line info (as opposed to building partial
17794 symtabs). */
17795 int record_lines_p = !decode_for_pst_p;
17796 /* A collection of things we need to pass to dwarf_record_line. */
17797 lnp_reader_state reader_state;
e142c38c
DJ
17798
17799 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 17800
debd256d
JB
17801 line_ptr = lh->statement_program_start;
17802 line_end = lh->statement_program_end;
c906108c 17803
d9b3de22
DE
17804 reader_state.gdbarch = gdbarch;
17805 reader_state.line_header = lh;
17806 reader_state.record_lines_p = record_lines_p;
17807
c906108c
SS
17808 /* Read the statement sequences until there's nothing left. */
17809 while (line_ptr < line_end)
17810 {
d9b3de22
DE
17811 /* The DWARF line number program state machine. */
17812 lnp_state_machine state_machine;
c906108c 17813 int end_sequence = 0;
d9b3de22
DE
17814
17815 /* Reset the state machine at the start of each sequence. */
17816 init_lnp_state_machine (&state_machine, &reader_state);
17817
17818 if (record_lines_p && lh->num_file_names >= state_machine.file)
c906108c 17819 {
aaa75496 17820 /* Start a subfile for the current file of the state machine. */
debd256d
JB
17821 /* lh->include_dirs and lh->file_names are 0-based, but the
17822 directory and file name numbers in the statement program
17823 are 1-based. */
d9b3de22 17824 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
d521ce57 17825 const char *dir = NULL;
a738430d 17826
afa6c9ab 17827 if (fe->dir_index && lh->include_dirs != NULL)
debd256d 17828 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb 17829
4d663531 17830 dwarf2_start_subfile (fe->name, dir);
c906108c
SS
17831 }
17832
a738430d 17833 /* Decode the table. */
d9b3de22 17834 while (line_ptr < line_end && !end_sequence)
c906108c
SS
17835 {
17836 op_code = read_1_byte (abfd, line_ptr);
17837 line_ptr += 1;
9aa1fe7e 17838
debd256d 17839 if (op_code >= lh->opcode_base)
6e70227d 17840 {
8e07a239 17841 /* Special opcode. */
699ca60a 17842 unsigned char adj_opcode;
3e29f34a 17843 CORE_ADDR addr_adj;
a05a36a5 17844 int line_delta;
8e07a239 17845
debd256d 17846 adj_opcode = op_code - lh->opcode_base;
d9b3de22
DE
17847 addr_adj = (((state_machine.op_index
17848 + (adj_opcode / lh->line_range))
2dc7f7b3
TT
17849 / lh->maximum_ops_per_instruction)
17850 * lh->minimum_instruction_length);
d9b3de22
DE
17851 state_machine.address
17852 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
17853 state_machine.op_index = ((state_machine.op_index
17854 + (adj_opcode / lh->line_range))
17855 % lh->maximum_ops_per_instruction);
a05a36a5 17856 line_delta = lh->line_base + (adj_opcode % lh->line_range);
d9b3de22 17857 state_machine.line += line_delta;
a05a36a5 17858 if (line_delta != 0)
d9b3de22
DE
17859 state_machine.line_has_non_zero_discriminator
17860 = state_machine.discriminator != 0;
17861
17862 dwarf_record_line (&reader_state, &state_machine, 0);
17863 state_machine.discriminator = 0;
9aa1fe7e
GK
17864 }
17865 else switch (op_code)
c906108c
SS
17866 {
17867 case DW_LNS_extended_op:
3e43a32a
MS
17868 extended_len = read_unsigned_leb128 (abfd, line_ptr,
17869 &bytes_read);
473b7be6 17870 line_ptr += bytes_read;
a8c50c1f 17871 extended_end = line_ptr + extended_len;
c906108c
SS
17872 extended_op = read_1_byte (abfd, line_ptr);
17873 line_ptr += 1;
17874 switch (extended_op)
17875 {
17876 case DW_LNE_end_sequence:
d9b3de22 17877 state_machine.record_line = record_line;
c906108c 17878 end_sequence = 1;
c906108c
SS
17879 break;
17880 case DW_LNE_set_address:
d9b3de22
DE
17881 {
17882 CORE_ADDR address
17883 = read_address (abfd, line_ptr, cu, &bytes_read);
17884
17885 line_ptr += bytes_read;
17886 check_line_address (cu, &state_machine, line_ptr,
17887 lowpc, address);
17888 state_machine.op_index = 0;
17889 address += baseaddr;
17890 state_machine.address
17891 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
17892 }
c906108c
SS
17893 break;
17894 case DW_LNE_define_file:
debd256d 17895 {
d521ce57 17896 const char *cur_file;
debd256d 17897 unsigned int dir_index, mod_time, length;
6e70227d 17898
3e43a32a
MS
17899 cur_file = read_direct_string (abfd, line_ptr,
17900 &bytes_read);
debd256d
JB
17901 line_ptr += bytes_read;
17902 dir_index =
17903 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17904 line_ptr += bytes_read;
17905 mod_time =
17906 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17907 line_ptr += bytes_read;
17908 length =
17909 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17910 line_ptr += bytes_read;
17911 add_file_name (lh, cur_file, dir_index, mod_time, length);
17912 }
c906108c 17913 break;
d0c6ba3d
CC
17914 case DW_LNE_set_discriminator:
17915 /* The discriminator is not interesting to the debugger;
a05a36a5
DE
17916 just ignore it. We still need to check its value though:
17917 if there are consecutive entries for the same
17918 (non-prologue) line we want to coalesce them.
17919 PR 17276. */
d9b3de22
DE
17920 state_machine.discriminator
17921 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17922 state_machine.line_has_non_zero_discriminator
17923 |= state_machine.discriminator != 0;
a05a36a5 17924 line_ptr += bytes_read;
d0c6ba3d 17925 break;
c906108c 17926 default:
4d3c2250 17927 complaint (&symfile_complaints,
e2e0b3e5 17928 _("mangled .debug_line section"));
debd256d 17929 return;
c906108c 17930 }
a8c50c1f
DJ
17931 /* Make sure that we parsed the extended op correctly. If e.g.
17932 we expected a different address size than the producer used,
17933 we may have read the wrong number of bytes. */
17934 if (line_ptr != extended_end)
17935 {
17936 complaint (&symfile_complaints,
17937 _("mangled .debug_line section"));
17938 return;
17939 }
c906108c
SS
17940 break;
17941 case DW_LNS_copy:
d9b3de22
DE
17942 dwarf_record_line (&reader_state, &state_machine, 0);
17943 state_machine.discriminator = 0;
c906108c
SS
17944 break;
17945 case DW_LNS_advance_pc:
2dc7f7b3
TT
17946 {
17947 CORE_ADDR adjust
17948 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3e29f34a 17949 CORE_ADDR addr_adj;
2dc7f7b3 17950
d9b3de22 17951 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
17952 / lh->maximum_ops_per_instruction)
17953 * lh->minimum_instruction_length);
d9b3de22
DE
17954 state_machine.address
17955 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
17956 state_machine.op_index = ((state_machine.op_index + adjust)
17957 % lh->maximum_ops_per_instruction);
2dc7f7b3
TT
17958 line_ptr += bytes_read;
17959 }
c906108c
SS
17960 break;
17961 case DW_LNS_advance_line:
a05a36a5
DE
17962 {
17963 int line_delta
17964 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
17965
d9b3de22 17966 state_machine.line += line_delta;
a05a36a5 17967 if (line_delta != 0)
d9b3de22
DE
17968 state_machine.line_has_non_zero_discriminator
17969 = state_machine.discriminator != 0;
a05a36a5
DE
17970 line_ptr += bytes_read;
17971 }
c906108c
SS
17972 break;
17973 case DW_LNS_set_file:
d9b3de22
DE
17974 {
17975 /* The arrays lh->include_dirs and lh->file_names are
17976 0-based, but the directory and file name numbers in
17977 the statement program are 1-based. */
17978 struct file_entry *fe;
17979 const char *dir = NULL;
17980
17981 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
17982 &bytes_read);
17983 line_ptr += bytes_read;
17984 if (state_machine.file == 0
17985 || state_machine.file - 1 >= lh->num_file_names)
17986 dwarf2_debug_line_missing_file_complaint ();
17987 else
17988 {
17989 fe = &lh->file_names[state_machine.file - 1];
17990 if (fe->dir_index && lh->include_dirs != NULL)
17991 dir = lh->include_dirs[fe->dir_index - 1];
17992 if (record_lines_p)
17993 {
17994 state_machine.last_subfile = current_subfile;
17995 state_machine.line_has_non_zero_discriminator
17996 = state_machine.discriminator != 0;
17997 dwarf2_start_subfile (fe->name, dir);
17998 }
17999 }
18000 }
c906108c
SS
18001 break;
18002 case DW_LNS_set_column:
0ad93d4f 18003 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18004 line_ptr += bytes_read;
18005 break;
18006 case DW_LNS_negate_stmt:
d9b3de22 18007 state_machine.is_stmt = (!state_machine.is_stmt);
c906108c
SS
18008 break;
18009 case DW_LNS_set_basic_block:
c906108c 18010 break;
c2c6d25f
JM
18011 /* Add to the address register of the state machine the
18012 address increment value corresponding to special opcode
a738430d
MK
18013 255. I.e., this value is scaled by the minimum
18014 instruction length since special opcode 255 would have
b021a221 18015 scaled the increment. */
c906108c 18016 case DW_LNS_const_add_pc:
2dc7f7b3
TT
18017 {
18018 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
3e29f34a 18019 CORE_ADDR addr_adj;
2dc7f7b3 18020
d9b3de22 18021 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18022 / lh->maximum_ops_per_instruction)
18023 * lh->minimum_instruction_length);
d9b3de22
DE
18024 state_machine.address
18025 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18026 state_machine.op_index = ((state_machine.op_index + adjust)
18027 % lh->maximum_ops_per_instruction);
2dc7f7b3 18028 }
c906108c
SS
18029 break;
18030 case DW_LNS_fixed_advance_pc:
3e29f34a
MR
18031 {
18032 CORE_ADDR addr_adj;
18033
18034 addr_adj = read_2_bytes (abfd, line_ptr);
d9b3de22
DE
18035 state_machine.address
18036 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18037 state_machine.op_index = 0;
3e29f34a
MR
18038 line_ptr += 2;
18039 }
c906108c 18040 break;
9aa1fe7e 18041 default:
a738430d
MK
18042 {
18043 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18044 int i;
a738430d 18045
debd256d 18046 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18047 {
18048 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18049 line_ptr += bytes_read;
18050 }
18051 }
c906108c
SS
18052 }
18053 }
d9b3de22
DE
18054
18055 if (!end_sequence)
18056 dwarf2_debug_line_missing_end_sequence_complaint ();
18057
18058 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18059 in which case we still finish recording the last line). */
18060 dwarf_record_line (&reader_state, &state_machine, 1);
c906108c 18061 }
f3f5162e
DE
18062}
18063
18064/* Decode the Line Number Program (LNP) for the given line_header
18065 structure and CU. The actual information extracted and the type
18066 of structures created from the LNP depends on the value of PST.
18067
18068 1. If PST is NULL, then this procedure uses the data from the program
18069 to create all necessary symbol tables, and their linetables.
18070
18071 2. If PST is not NULL, this procedure reads the program to determine
18072 the list of files included by the unit represented by PST, and
18073 builds all the associated partial symbol tables.
18074
18075 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18076 It is used for relative paths in the line table.
18077 NOTE: When processing partial symtabs (pst != NULL),
18078 comp_dir == pst->dirname.
18079
18080 NOTE: It is important that psymtabs have the same file name (via strcmp)
18081 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18082 symtab we don't use it in the name of the psymtabs we create.
18083 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18084 A good testcase for this is mb-inline.exp.
18085
527f3840
JK
18086 LOWPC is the lowest address in CU (or 0 if not known).
18087
18088 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18089 for its PC<->lines mapping information. Otherwise only the filename
18090 table is read in. */
f3f5162e
DE
18091
18092static void
18093dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18094 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18095 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18096{
18097 struct objfile *objfile = cu->objfile;
18098 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18099
527f3840
JK
18100 if (decode_mapping)
18101 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18102
18103 if (decode_for_pst_p)
18104 {
18105 int file_index;
18106
18107 /* Now that we're done scanning the Line Header Program, we can
18108 create the psymtab of each included file. */
18109 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18110 if (lh->file_names[file_index].included_p == 1)
18111 {
d521ce57 18112 const char *include_name =
c6da4cef
DE
18113 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18114 if (include_name != NULL)
aaa75496
JB
18115 dwarf2_create_include_psymtab (include_name, pst, objfile);
18116 }
18117 }
cb1df416
DJ
18118 else
18119 {
18120 /* Make sure a symtab is created for every file, even files
18121 which contain only variables (i.e. no code with associated
18122 line numbers). */
43f3e411 18123 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18124 int i;
cb1df416
DJ
18125
18126 for (i = 0; i < lh->num_file_names; i++)
18127 {
d521ce57 18128 const char *dir = NULL;
f3f5162e 18129 struct file_entry *fe;
9a619af0 18130
cb1df416 18131 fe = &lh->file_names[i];
afa6c9ab 18132 if (fe->dir_index && lh->include_dirs != NULL)
cb1df416 18133 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 18134 dwarf2_start_subfile (fe->name, dir);
cb1df416 18135
cb1df416 18136 if (current_subfile->symtab == NULL)
43f3e411
DE
18137 {
18138 current_subfile->symtab
18139 = allocate_symtab (cust, current_subfile->name);
18140 }
cb1df416
DJ
18141 fe->symtab = current_subfile->symtab;
18142 }
18143 }
c906108c
SS
18144}
18145
18146/* Start a subfile for DWARF. FILENAME is the name of the file and
18147 DIRNAME the name of the source directory which contains FILENAME
4d663531 18148 or NULL if not known.
c906108c
SS
18149 This routine tries to keep line numbers from identical absolute and
18150 relative file names in a common subfile.
18151
18152 Using the `list' example from the GDB testsuite, which resides in
18153 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18154 of /srcdir/list0.c yields the following debugging information for list0.c:
18155
c5aa993b 18156 DW_AT_name: /srcdir/list0.c
4d663531 18157 DW_AT_comp_dir: /compdir
357e46e7 18158 files.files[0].name: list0.h
c5aa993b 18159 files.files[0].dir: /srcdir
357e46e7 18160 files.files[1].name: list0.c
c5aa993b 18161 files.files[1].dir: /srcdir
c906108c
SS
18162
18163 The line number information for list0.c has to end up in a single
4f1520fb
FR
18164 subfile, so that `break /srcdir/list0.c:1' works as expected.
18165 start_subfile will ensure that this happens provided that we pass the
18166 concatenation of files.files[1].dir and files.files[1].name as the
18167 subfile's name. */
c906108c
SS
18168
18169static void
4d663531 18170dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18171{
d521ce57 18172 char *copy = NULL;
4f1520fb 18173
4d663531 18174 /* In order not to lose the line information directory,
4f1520fb
FR
18175 we concatenate it to the filename when it makes sense.
18176 Note that the Dwarf3 standard says (speaking of filenames in line
18177 information): ``The directory index is ignored for file names
18178 that represent full path names''. Thus ignoring dirname in the
18179 `else' branch below isn't an issue. */
c906108c 18180
d5166ae1 18181 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18182 {
18183 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18184 filename = copy;
18185 }
c906108c 18186
4d663531 18187 start_subfile (filename);
4f1520fb 18188
d521ce57
TT
18189 if (copy != NULL)
18190 xfree (copy);
c906108c
SS
18191}
18192
f4dc4d17
DE
18193/* Start a symtab for DWARF.
18194 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18195
43f3e411 18196static struct compunit_symtab *
f4dc4d17 18197dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18198 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18199{
43f3e411
DE
18200 struct compunit_symtab *cust
18201 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18202
f4dc4d17
DE
18203 record_debugformat ("DWARF 2");
18204 record_producer (cu->producer);
18205
18206 /* We assume that we're processing GCC output. */
18207 processing_gcc_compilation = 2;
18208
4d4ec4e5 18209 cu->processing_has_namespace_info = 0;
43f3e411
DE
18210
18211 return cust;
f4dc4d17
DE
18212}
18213
4c2df51b
DJ
18214static void
18215var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18216 struct dwarf2_cu *cu)
4c2df51b 18217{
e7c27a73
DJ
18218 struct objfile *objfile = cu->objfile;
18219 struct comp_unit_head *cu_header = &cu->header;
18220
4c2df51b
DJ
18221 /* NOTE drow/2003-01-30: There used to be a comment and some special
18222 code here to turn a symbol with DW_AT_external and a
18223 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18224 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18225 with some versions of binutils) where shared libraries could have
18226 relocations against symbols in their debug information - the
18227 minimal symbol would have the right address, but the debug info
18228 would not. It's no longer necessary, because we will explicitly
18229 apply relocations when we read in the debug information now. */
18230
18231 /* A DW_AT_location attribute with no contents indicates that a
18232 variable has been optimized away. */
18233 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18234 {
f1e6e072 18235 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18236 return;
18237 }
18238
18239 /* Handle one degenerate form of location expression specially, to
18240 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18241 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18242 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18243
18244 if (attr_form_is_block (attr)
3019eac3
DE
18245 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18246 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18247 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18248 && (DW_BLOCK (attr)->size
18249 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18250 {
891d2f0b 18251 unsigned int dummy;
4c2df51b 18252
3019eac3
DE
18253 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18254 SYMBOL_VALUE_ADDRESS (sym) =
18255 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18256 else
18257 SYMBOL_VALUE_ADDRESS (sym) =
18258 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18259 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18260 fixup_symbol_section (sym, objfile);
18261 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18262 SYMBOL_SECTION (sym));
4c2df51b
DJ
18263 return;
18264 }
18265
18266 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18267 expression evaluator, and use LOC_COMPUTED only when necessary
18268 (i.e. when the value of a register or memory location is
18269 referenced, or a thread-local block, etc.). Then again, it might
18270 not be worthwhile. I'm assuming that it isn't unless performance
18271 or memory numbers show me otherwise. */
18272
f1e6e072 18273 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18274
f1e6e072 18275 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18276 cu->has_loclist = 1;
4c2df51b
DJ
18277}
18278
c906108c
SS
18279/* Given a pointer to a DWARF information entry, figure out if we need
18280 to make a symbol table entry for it, and if so, create a new entry
18281 and return a pointer to it.
18282 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
18283 used the passed type.
18284 If SPACE is not NULL, use it to hold the new symbol. If it is
18285 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
18286
18287static struct symbol *
34eaf542
TT
18288new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18289 struct symbol *space)
c906108c 18290{
e7c27a73 18291 struct objfile *objfile = cu->objfile;
3e29f34a 18292 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18293 struct symbol *sym = NULL;
15d034d0 18294 const char *name;
c906108c
SS
18295 struct attribute *attr = NULL;
18296 struct attribute *attr2 = NULL;
e142c38c 18297 CORE_ADDR baseaddr;
e37fd15a
SW
18298 struct pending **list_to_add = NULL;
18299
edb3359d 18300 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18301
18302 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18303
94af9270 18304 name = dwarf2_name (die, cu);
c906108c
SS
18305 if (name)
18306 {
94af9270 18307 const char *linkagename;
34eaf542 18308 int suppress_add = 0;
94af9270 18309
34eaf542
TT
18310 if (space)
18311 sym = space;
18312 else
e623cf5d 18313 sym = allocate_symbol (objfile);
c906108c 18314 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18315
18316 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18317 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18318 linkagename = dwarf2_physname (name, die, cu);
18319 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18320
f55ee35c
JK
18321 /* Fortran does not have mangling standard and the mangling does differ
18322 between gfortran, iFort etc. */
18323 if (cu->language == language_fortran
b250c185 18324 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18325 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18326 dwarf2_full_name (name, die, cu),
29df156d 18327 NULL);
f55ee35c 18328
c906108c 18329 /* Default assumptions.
c5aa993b 18330 Use the passed type or decode it from the die. */
176620f1 18331 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18332 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18333 if (type != NULL)
18334 SYMBOL_TYPE (sym) = type;
18335 else
e7c27a73 18336 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18337 attr = dwarf2_attr (die,
18338 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18339 cu);
c906108c
SS
18340 if (attr)
18341 {
18342 SYMBOL_LINE (sym) = DW_UNSND (attr);
18343 }
cb1df416 18344
edb3359d
DJ
18345 attr = dwarf2_attr (die,
18346 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18347 cu);
cb1df416
DJ
18348 if (attr)
18349 {
18350 int file_index = DW_UNSND (attr);
9a619af0 18351
cb1df416
DJ
18352 if (cu->line_header == NULL
18353 || file_index > cu->line_header->num_file_names)
18354 complaint (&symfile_complaints,
18355 _("file index out of range"));
1c3d648d 18356 else if (file_index > 0)
cb1df416
DJ
18357 {
18358 struct file_entry *fe;
9a619af0 18359
cb1df416 18360 fe = &cu->line_header->file_names[file_index - 1];
08be3fe3 18361 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18362 }
18363 }
18364
c906108c
SS
18365 switch (die->tag)
18366 {
18367 case DW_TAG_label:
e142c38c 18368 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18369 if (attr)
3e29f34a
MR
18370 {
18371 CORE_ADDR addr;
18372
18373 addr = attr_value_as_address (attr);
18374 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18375 SYMBOL_VALUE_ADDRESS (sym) = addr;
18376 }
0f5238ed
TT
18377 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18378 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18379 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18380 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18381 break;
18382 case DW_TAG_subprogram:
18383 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18384 finish_block. */
f1e6e072 18385 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18386 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18387 if ((attr2 && (DW_UNSND (attr2) != 0))
18388 || cu->language == language_ada)
c906108c 18389 {
2cfa0c8d
JB
18390 /* Subprograms marked external are stored as a global symbol.
18391 Ada subprograms, whether marked external or not, are always
18392 stored as a global symbol, because we want to be able to
18393 access them globally. For instance, we want to be able
18394 to break on a nested subprogram without having to
18395 specify the context. */
e37fd15a 18396 list_to_add = &global_symbols;
c906108c
SS
18397 }
18398 else
18399 {
e37fd15a 18400 list_to_add = cu->list_in_scope;
c906108c
SS
18401 }
18402 break;
edb3359d
DJ
18403 case DW_TAG_inlined_subroutine:
18404 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18405 finish_block. */
f1e6e072 18406 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 18407 SYMBOL_INLINED (sym) = 1;
481860b3 18408 list_to_add = cu->list_in_scope;
edb3359d 18409 break;
34eaf542
TT
18410 case DW_TAG_template_value_param:
18411 suppress_add = 1;
18412 /* Fall through. */
72929c62 18413 case DW_TAG_constant:
c906108c 18414 case DW_TAG_variable:
254e6b9e 18415 case DW_TAG_member:
0963b4bd
MS
18416 /* Compilation with minimal debug info may result in
18417 variables with missing type entries. Change the
18418 misleading `void' type to something sensible. */
c906108c 18419 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 18420 SYMBOL_TYPE (sym)
46bf5051 18421 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 18422
e142c38c 18423 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
18424 /* In the case of DW_TAG_member, we should only be called for
18425 static const members. */
18426 if (die->tag == DW_TAG_member)
18427 {
3863f96c
DE
18428 /* dwarf2_add_field uses die_is_declaration,
18429 so we do the same. */
254e6b9e
DE
18430 gdb_assert (die_is_declaration (die, cu));
18431 gdb_assert (attr);
18432 }
c906108c
SS
18433 if (attr)
18434 {
e7c27a73 18435 dwarf2_const_value (attr, sym, cu);
e142c38c 18436 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 18437 if (!suppress_add)
34eaf542
TT
18438 {
18439 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 18440 list_to_add = &global_symbols;
34eaf542 18441 else
e37fd15a 18442 list_to_add = cu->list_in_scope;
34eaf542 18443 }
c906108c
SS
18444 break;
18445 }
e142c38c 18446 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
18447 if (attr)
18448 {
e7c27a73 18449 var_decode_location (attr, sym, cu);
e142c38c 18450 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
18451
18452 /* Fortran explicitly imports any global symbols to the local
18453 scope by DW_TAG_common_block. */
18454 if (cu->language == language_fortran && die->parent
18455 && die->parent->tag == DW_TAG_common_block)
18456 attr2 = NULL;
18457
caac4577
JG
18458 if (SYMBOL_CLASS (sym) == LOC_STATIC
18459 && SYMBOL_VALUE_ADDRESS (sym) == 0
18460 && !dwarf2_per_objfile->has_section_at_zero)
18461 {
18462 /* When a static variable is eliminated by the linker,
18463 the corresponding debug information is not stripped
18464 out, but the variable address is set to null;
18465 do not add such variables into symbol table. */
18466 }
18467 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 18468 {
f55ee35c
JK
18469 /* Workaround gfortran PR debug/40040 - it uses
18470 DW_AT_location for variables in -fPIC libraries which may
18471 get overriden by other libraries/executable and get
18472 a different address. Resolve it by the minimal symbol
18473 which may come from inferior's executable using copy
18474 relocation. Make this workaround only for gfortran as for
18475 other compilers GDB cannot guess the minimal symbol
18476 Fortran mangling kind. */
18477 if (cu->language == language_fortran && die->parent
18478 && die->parent->tag == DW_TAG_module
18479 && cu->producer
61012eef 18480 && startswith (cu->producer, "GNU Fortran "))
f1e6e072 18481 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 18482
1c809c68
TT
18483 /* A variable with DW_AT_external is never static,
18484 but it may be block-scoped. */
18485 list_to_add = (cu->list_in_scope == &file_symbols
18486 ? &global_symbols : cu->list_in_scope);
1c809c68 18487 }
c906108c 18488 else
e37fd15a 18489 list_to_add = cu->list_in_scope;
c906108c
SS
18490 }
18491 else
18492 {
18493 /* We do not know the address of this symbol.
c5aa993b
JM
18494 If it is an external symbol and we have type information
18495 for it, enter the symbol as a LOC_UNRESOLVED symbol.
18496 The address of the variable will then be determined from
18497 the minimal symbol table whenever the variable is
18498 referenced. */
e142c38c 18499 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
18500
18501 /* Fortran explicitly imports any global symbols to the local
18502 scope by DW_TAG_common_block. */
18503 if (cu->language == language_fortran && die->parent
18504 && die->parent->tag == DW_TAG_common_block)
18505 {
18506 /* SYMBOL_CLASS doesn't matter here because
18507 read_common_block is going to reset it. */
18508 if (!suppress_add)
18509 list_to_add = cu->list_in_scope;
18510 }
18511 else if (attr2 && (DW_UNSND (attr2) != 0)
18512 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 18513 {
0fe7935b
DJ
18514 /* A variable with DW_AT_external is never static, but it
18515 may be block-scoped. */
18516 list_to_add = (cu->list_in_scope == &file_symbols
18517 ? &global_symbols : cu->list_in_scope);
18518
f1e6e072 18519 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 18520 }
442ddf59
JK
18521 else if (!die_is_declaration (die, cu))
18522 {
18523 /* Use the default LOC_OPTIMIZED_OUT class. */
18524 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
18525 if (!suppress_add)
18526 list_to_add = cu->list_in_scope;
442ddf59 18527 }
c906108c
SS
18528 }
18529 break;
18530 case DW_TAG_formal_parameter:
edb3359d
DJ
18531 /* If we are inside a function, mark this as an argument. If
18532 not, we might be looking at an argument to an inlined function
18533 when we do not have enough information to show inlined frames;
18534 pretend it's a local variable in that case so that the user can
18535 still see it. */
18536 if (context_stack_depth > 0
18537 && context_stack[context_stack_depth - 1].name != NULL)
18538 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 18539 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
18540 if (attr)
18541 {
e7c27a73 18542 var_decode_location (attr, sym, cu);
c906108c 18543 }
e142c38c 18544 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
18545 if (attr)
18546 {
e7c27a73 18547 dwarf2_const_value (attr, sym, cu);
c906108c 18548 }
f346a30d 18549
e37fd15a 18550 list_to_add = cu->list_in_scope;
c906108c
SS
18551 break;
18552 case DW_TAG_unspecified_parameters:
18553 /* From varargs functions; gdb doesn't seem to have any
18554 interest in this information, so just ignore it for now.
18555 (FIXME?) */
18556 break;
34eaf542
TT
18557 case DW_TAG_template_type_param:
18558 suppress_add = 1;
18559 /* Fall through. */
c906108c 18560 case DW_TAG_class_type:
680b30c7 18561 case DW_TAG_interface_type:
c906108c
SS
18562 case DW_TAG_structure_type:
18563 case DW_TAG_union_type:
72019c9c 18564 case DW_TAG_set_type:
c906108c 18565 case DW_TAG_enumeration_type:
f1e6e072 18566 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 18567 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 18568
63d06c5c 18569 {
987504bb 18570 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
18571 really ever be static objects: otherwise, if you try
18572 to, say, break of a class's method and you're in a file
18573 which doesn't mention that class, it won't work unless
18574 the check for all static symbols in lookup_symbol_aux
18575 saves you. See the OtherFileClass tests in
18576 gdb.c++/namespace.exp. */
18577
e37fd15a 18578 if (!suppress_add)
34eaf542 18579 {
34eaf542
TT
18580 list_to_add = (cu->list_in_scope == &file_symbols
18581 && (cu->language == language_cplus
18582 || cu->language == language_java)
18583 ? &global_symbols : cu->list_in_scope);
63d06c5c 18584
64382290
TT
18585 /* The semantics of C++ state that "struct foo {
18586 ... }" also defines a typedef for "foo". A Java
18587 class declaration also defines a typedef for the
18588 class. */
18589 if (cu->language == language_cplus
18590 || cu->language == language_java
45280282
IB
18591 || cu->language == language_ada
18592 || cu->language == language_d)
64382290
TT
18593 {
18594 /* The symbol's name is already allocated along
18595 with this objfile, so we don't need to
18596 duplicate it for the type. */
18597 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
18598 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
18599 }
63d06c5c
DC
18600 }
18601 }
c906108c
SS
18602 break;
18603 case DW_TAG_typedef:
f1e6e072 18604 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 18605 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 18606 list_to_add = cu->list_in_scope;
63d06c5c 18607 break;
c906108c 18608 case DW_TAG_base_type:
a02abb62 18609 case DW_TAG_subrange_type:
f1e6e072 18610 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 18611 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 18612 list_to_add = cu->list_in_scope;
c906108c
SS
18613 break;
18614 case DW_TAG_enumerator:
e142c38c 18615 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
18616 if (attr)
18617 {
e7c27a73 18618 dwarf2_const_value (attr, sym, cu);
c906108c 18619 }
63d06c5c
DC
18620 {
18621 /* NOTE: carlton/2003-11-10: See comment above in the
18622 DW_TAG_class_type, etc. block. */
18623
e142c38c 18624 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
18625 && (cu->language == language_cplus
18626 || cu->language == language_java)
e142c38c 18627 ? &global_symbols : cu->list_in_scope);
63d06c5c 18628 }
c906108c 18629 break;
74921315 18630 case DW_TAG_imported_declaration:
5c4e30ca 18631 case DW_TAG_namespace:
f1e6e072 18632 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 18633 list_to_add = &global_symbols;
5c4e30ca 18634 break;
530e8392
KB
18635 case DW_TAG_module:
18636 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18637 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
18638 list_to_add = &global_symbols;
18639 break;
4357ac6c 18640 case DW_TAG_common_block:
f1e6e072 18641 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
18642 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
18643 add_symbol_to_list (sym, cu->list_in_scope);
18644 break;
c906108c
SS
18645 default:
18646 /* Not a tag we recognize. Hopefully we aren't processing
18647 trash data, but since we must specifically ignore things
18648 we don't recognize, there is nothing else we should do at
0963b4bd 18649 this point. */
e2e0b3e5 18650 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 18651 dwarf_tag_name (die->tag));
c906108c
SS
18652 break;
18653 }
df8a16a1 18654
e37fd15a
SW
18655 if (suppress_add)
18656 {
18657 sym->hash_next = objfile->template_symbols;
18658 objfile->template_symbols = sym;
18659 list_to_add = NULL;
18660 }
18661
18662 if (list_to_add != NULL)
18663 add_symbol_to_list (sym, list_to_add);
18664
df8a16a1
DJ
18665 /* For the benefit of old versions of GCC, check for anonymous
18666 namespaces based on the demangled name. */
4d4ec4e5 18667 if (!cu->processing_has_namespace_info
94af9270 18668 && cu->language == language_cplus)
a10964d1 18669 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
18670 }
18671 return (sym);
18672}
18673
34eaf542
TT
18674/* A wrapper for new_symbol_full that always allocates a new symbol. */
18675
18676static struct symbol *
18677new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18678{
18679 return new_symbol_full (die, type, cu, NULL);
18680}
18681
98bfdba5
PA
18682/* Given an attr with a DW_FORM_dataN value in host byte order,
18683 zero-extend it as appropriate for the symbol's type. The DWARF
18684 standard (v4) is not entirely clear about the meaning of using
18685 DW_FORM_dataN for a constant with a signed type, where the type is
18686 wider than the data. The conclusion of a discussion on the DWARF
18687 list was that this is unspecified. We choose to always zero-extend
18688 because that is the interpretation long in use by GCC. */
c906108c 18689
98bfdba5 18690static gdb_byte *
ff39bb5e 18691dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 18692 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 18693{
e7c27a73 18694 struct objfile *objfile = cu->objfile;
e17a4113
UW
18695 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
18696 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
18697 LONGEST l = DW_UNSND (attr);
18698
18699 if (bits < sizeof (*value) * 8)
18700 {
18701 l &= ((LONGEST) 1 << bits) - 1;
18702 *value = l;
18703 }
18704 else if (bits == sizeof (*value) * 8)
18705 *value = l;
18706 else
18707 {
18708 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
18709 store_unsigned_integer (bytes, bits / 8, byte_order, l);
18710 return bytes;
18711 }
18712
18713 return NULL;
18714}
18715
18716/* Read a constant value from an attribute. Either set *VALUE, or if
18717 the value does not fit in *VALUE, set *BYTES - either already
18718 allocated on the objfile obstack, or newly allocated on OBSTACK,
18719 or, set *BATON, if we translated the constant to a location
18720 expression. */
18721
18722static void
ff39bb5e 18723dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
18724 const char *name, struct obstack *obstack,
18725 struct dwarf2_cu *cu,
d521ce57 18726 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
18727 struct dwarf2_locexpr_baton **baton)
18728{
18729 struct objfile *objfile = cu->objfile;
18730 struct comp_unit_head *cu_header = &cu->header;
c906108c 18731 struct dwarf_block *blk;
98bfdba5
PA
18732 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
18733 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18734
18735 *value = 0;
18736 *bytes = NULL;
18737 *baton = NULL;
c906108c
SS
18738
18739 switch (attr->form)
18740 {
18741 case DW_FORM_addr:
3019eac3 18742 case DW_FORM_GNU_addr_index:
ac56253d 18743 {
ac56253d
TT
18744 gdb_byte *data;
18745
98bfdba5
PA
18746 if (TYPE_LENGTH (type) != cu_header->addr_size)
18747 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 18748 cu_header->addr_size,
98bfdba5 18749 TYPE_LENGTH (type));
ac56253d
TT
18750 /* Symbols of this form are reasonably rare, so we just
18751 piggyback on the existing location code rather than writing
18752 a new implementation of symbol_computed_ops. */
7919a973 18753 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
18754 (*baton)->per_cu = cu->per_cu;
18755 gdb_assert ((*baton)->per_cu);
ac56253d 18756
98bfdba5 18757 (*baton)->size = 2 + cu_header->addr_size;
7919a973 18758 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 18759 (*baton)->data = data;
ac56253d
TT
18760
18761 data[0] = DW_OP_addr;
18762 store_unsigned_integer (&data[1], cu_header->addr_size,
18763 byte_order, DW_ADDR (attr));
18764 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 18765 }
c906108c 18766 break;
4ac36638 18767 case DW_FORM_string:
93b5768b 18768 case DW_FORM_strp:
3019eac3 18769 case DW_FORM_GNU_str_index:
36586728 18770 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
18771 /* DW_STRING is already allocated on the objfile obstack, point
18772 directly to it. */
d521ce57 18773 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 18774 break;
c906108c
SS
18775 case DW_FORM_block1:
18776 case DW_FORM_block2:
18777 case DW_FORM_block4:
18778 case DW_FORM_block:
2dc7f7b3 18779 case DW_FORM_exprloc:
c906108c 18780 blk = DW_BLOCK (attr);
98bfdba5
PA
18781 if (TYPE_LENGTH (type) != blk->size)
18782 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
18783 TYPE_LENGTH (type));
18784 *bytes = blk->data;
c906108c 18785 break;
2df3850c
JM
18786
18787 /* The DW_AT_const_value attributes are supposed to carry the
18788 symbol's value "represented as it would be on the target
18789 architecture." By the time we get here, it's already been
18790 converted to host endianness, so we just need to sign- or
18791 zero-extend it as appropriate. */
18792 case DW_FORM_data1:
3aef2284 18793 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 18794 break;
c906108c 18795 case DW_FORM_data2:
3aef2284 18796 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 18797 break;
c906108c 18798 case DW_FORM_data4:
3aef2284 18799 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 18800 break;
c906108c 18801 case DW_FORM_data8:
3aef2284 18802 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
18803 break;
18804
c906108c 18805 case DW_FORM_sdata:
98bfdba5 18806 *value = DW_SND (attr);
2df3850c
JM
18807 break;
18808
c906108c 18809 case DW_FORM_udata:
98bfdba5 18810 *value = DW_UNSND (attr);
c906108c 18811 break;
2df3850c 18812
c906108c 18813 default:
4d3c2250 18814 complaint (&symfile_complaints,
e2e0b3e5 18815 _("unsupported const value attribute form: '%s'"),
4d3c2250 18816 dwarf_form_name (attr->form));
98bfdba5 18817 *value = 0;
c906108c
SS
18818 break;
18819 }
18820}
18821
2df3850c 18822
98bfdba5
PA
18823/* Copy constant value from an attribute to a symbol. */
18824
2df3850c 18825static void
ff39bb5e 18826dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 18827 struct dwarf2_cu *cu)
2df3850c 18828{
98bfdba5
PA
18829 struct objfile *objfile = cu->objfile;
18830 struct comp_unit_head *cu_header = &cu->header;
12df843f 18831 LONGEST value;
d521ce57 18832 const gdb_byte *bytes;
98bfdba5 18833 struct dwarf2_locexpr_baton *baton;
2df3850c 18834
98bfdba5
PA
18835 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
18836 SYMBOL_PRINT_NAME (sym),
18837 &objfile->objfile_obstack, cu,
18838 &value, &bytes, &baton);
2df3850c 18839
98bfdba5
PA
18840 if (baton != NULL)
18841 {
98bfdba5 18842 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 18843 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
18844 }
18845 else if (bytes != NULL)
18846 {
18847 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 18848 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
18849 }
18850 else
18851 {
18852 SYMBOL_VALUE (sym) = value;
f1e6e072 18853 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 18854 }
2df3850c
JM
18855}
18856
c906108c
SS
18857/* Return the type of the die in question using its DW_AT_type attribute. */
18858
18859static struct type *
e7c27a73 18860die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18861{
c906108c 18862 struct attribute *type_attr;
c906108c 18863
e142c38c 18864 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
18865 if (!type_attr)
18866 {
18867 /* A missing DW_AT_type represents a void type. */
46bf5051 18868 return objfile_type (cu->objfile)->builtin_void;
c906108c 18869 }
348e048f 18870
673bfd45 18871 return lookup_die_type (die, type_attr, cu);
c906108c
SS
18872}
18873
b4ba55a1
JB
18874/* True iff CU's producer generates GNAT Ada auxiliary information
18875 that allows to find parallel types through that information instead
18876 of having to do expensive parallel lookups by type name. */
18877
18878static int
18879need_gnat_info (struct dwarf2_cu *cu)
18880{
18881 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
18882 of GNAT produces this auxiliary information, without any indication
18883 that it is produced. Part of enhancing the FSF version of GNAT
18884 to produce that information will be to put in place an indicator
18885 that we can use in order to determine whether the descriptive type
18886 info is available or not. One suggestion that has been made is
18887 to use a new attribute, attached to the CU die. For now, assume
18888 that the descriptive type info is not available. */
18889 return 0;
18890}
18891
b4ba55a1
JB
18892/* Return the auxiliary type of the die in question using its
18893 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
18894 attribute is not present. */
18895
18896static struct type *
18897die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
18898{
b4ba55a1 18899 struct attribute *type_attr;
b4ba55a1
JB
18900
18901 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
18902 if (!type_attr)
18903 return NULL;
18904
673bfd45 18905 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
18906}
18907
18908/* If DIE has a descriptive_type attribute, then set the TYPE's
18909 descriptive type accordingly. */
18910
18911static void
18912set_descriptive_type (struct type *type, struct die_info *die,
18913 struct dwarf2_cu *cu)
18914{
18915 struct type *descriptive_type = die_descriptive_type (die, cu);
18916
18917 if (descriptive_type)
18918 {
18919 ALLOCATE_GNAT_AUX_TYPE (type);
18920 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
18921 }
18922}
18923
c906108c
SS
18924/* Return the containing type of the die in question using its
18925 DW_AT_containing_type attribute. */
18926
18927static struct type *
e7c27a73 18928die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18929{
c906108c 18930 struct attribute *type_attr;
c906108c 18931
e142c38c 18932 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
18933 if (!type_attr)
18934 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 18935 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 18936
673bfd45 18937 return lookup_die_type (die, type_attr, cu);
c906108c
SS
18938}
18939
ac9ec31b
DE
18940/* Return an error marker type to use for the ill formed type in DIE/CU. */
18941
18942static struct type *
18943build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
18944{
18945 struct objfile *objfile = dwarf2_per_objfile->objfile;
18946 char *message, *saved;
18947
18948 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 18949 objfile_name (objfile),
ac9ec31b
DE
18950 cu->header.offset.sect_off,
18951 die->offset.sect_off);
18952 saved = obstack_copy0 (&objfile->objfile_obstack,
18953 message, strlen (message));
18954 xfree (message);
18955
18956 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
18957}
18958
673bfd45 18959/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
18960 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
18961 DW_AT_containing_type.
673bfd45
DE
18962 If there is no type substitute an error marker. */
18963
c906108c 18964static struct type *
ff39bb5e 18965lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 18966 struct dwarf2_cu *cu)
c906108c 18967{
bb5ed363 18968 struct objfile *objfile = cu->objfile;
f792889a
DJ
18969 struct type *this_type;
18970
ac9ec31b
DE
18971 gdb_assert (attr->name == DW_AT_type
18972 || attr->name == DW_AT_GNAT_descriptive_type
18973 || attr->name == DW_AT_containing_type);
18974
673bfd45
DE
18975 /* First see if we have it cached. */
18976
36586728
TT
18977 if (attr->form == DW_FORM_GNU_ref_alt)
18978 {
18979 struct dwarf2_per_cu_data *per_cu;
18980 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18981
18982 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
18983 this_type = get_die_type_at_offset (offset, per_cu);
18984 }
7771576e 18985 else if (attr_form_is_ref (attr))
673bfd45 18986 {
b64f50a1 18987 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
18988
18989 this_type = get_die_type_at_offset (offset, cu->per_cu);
18990 }
55f1336d 18991 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 18992 {
ac9ec31b 18993 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 18994
ac9ec31b 18995 return get_signatured_type (die, signature, cu);
673bfd45
DE
18996 }
18997 else
18998 {
ac9ec31b
DE
18999 complaint (&symfile_complaints,
19000 _("Dwarf Error: Bad type attribute %s in DIE"
19001 " at 0x%x [in module %s]"),
19002 dwarf_attr_name (attr->name), die->offset.sect_off,
4262abfb 19003 objfile_name (objfile));
ac9ec31b 19004 return build_error_marker_type (cu, die);
673bfd45
DE
19005 }
19006
19007 /* If not cached we need to read it in. */
19008
19009 if (this_type == NULL)
19010 {
ac9ec31b 19011 struct die_info *type_die = NULL;
673bfd45
DE
19012 struct dwarf2_cu *type_cu = cu;
19013
7771576e 19014 if (attr_form_is_ref (attr))
ac9ec31b
DE
19015 type_die = follow_die_ref (die, attr, &type_cu);
19016 if (type_die == NULL)
19017 return build_error_marker_type (cu, die);
19018 /* If we find the type now, it's probably because the type came
3019eac3
DE
19019 from an inter-CU reference and the type's CU got expanded before
19020 ours. */
ac9ec31b 19021 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19022 }
19023
19024 /* If we still don't have a type use an error marker. */
19025
19026 if (this_type == NULL)
ac9ec31b 19027 return build_error_marker_type (cu, die);
673bfd45 19028
f792889a 19029 return this_type;
c906108c
SS
19030}
19031
673bfd45
DE
19032/* Return the type in DIE, CU.
19033 Returns NULL for invalid types.
19034
02142a6c 19035 This first does a lookup in die_type_hash,
673bfd45
DE
19036 and only reads the die in if necessary.
19037
19038 NOTE: This can be called when reading in partial or full symbols. */
19039
f792889a 19040static struct type *
e7c27a73 19041read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19042{
f792889a
DJ
19043 struct type *this_type;
19044
19045 this_type = get_die_type (die, cu);
19046 if (this_type)
19047 return this_type;
19048
673bfd45
DE
19049 return read_type_die_1 (die, cu);
19050}
19051
19052/* Read the type in DIE, CU.
19053 Returns NULL for invalid types. */
19054
19055static struct type *
19056read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19057{
19058 struct type *this_type = NULL;
19059
c906108c
SS
19060 switch (die->tag)
19061 {
19062 case DW_TAG_class_type:
680b30c7 19063 case DW_TAG_interface_type:
c906108c
SS
19064 case DW_TAG_structure_type:
19065 case DW_TAG_union_type:
f792889a 19066 this_type = read_structure_type (die, cu);
c906108c
SS
19067 break;
19068 case DW_TAG_enumeration_type:
f792889a 19069 this_type = read_enumeration_type (die, cu);
c906108c
SS
19070 break;
19071 case DW_TAG_subprogram:
19072 case DW_TAG_subroutine_type:
edb3359d 19073 case DW_TAG_inlined_subroutine:
f792889a 19074 this_type = read_subroutine_type (die, cu);
c906108c
SS
19075 break;
19076 case DW_TAG_array_type:
f792889a 19077 this_type = read_array_type (die, cu);
c906108c 19078 break;
72019c9c 19079 case DW_TAG_set_type:
f792889a 19080 this_type = read_set_type (die, cu);
72019c9c 19081 break;
c906108c 19082 case DW_TAG_pointer_type:
f792889a 19083 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19084 break;
19085 case DW_TAG_ptr_to_member_type:
f792889a 19086 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19087 break;
19088 case DW_TAG_reference_type:
f792889a 19089 this_type = read_tag_reference_type (die, cu);
c906108c
SS
19090 break;
19091 case DW_TAG_const_type:
f792889a 19092 this_type = read_tag_const_type (die, cu);
c906108c
SS
19093 break;
19094 case DW_TAG_volatile_type:
f792889a 19095 this_type = read_tag_volatile_type (die, cu);
c906108c 19096 break;
06d66ee9
TT
19097 case DW_TAG_restrict_type:
19098 this_type = read_tag_restrict_type (die, cu);
19099 break;
c906108c 19100 case DW_TAG_string_type:
f792889a 19101 this_type = read_tag_string_type (die, cu);
c906108c
SS
19102 break;
19103 case DW_TAG_typedef:
f792889a 19104 this_type = read_typedef (die, cu);
c906108c 19105 break;
a02abb62 19106 case DW_TAG_subrange_type:
f792889a 19107 this_type = read_subrange_type (die, cu);
a02abb62 19108 break;
c906108c 19109 case DW_TAG_base_type:
f792889a 19110 this_type = read_base_type (die, cu);
c906108c 19111 break;
81a17f79 19112 case DW_TAG_unspecified_type:
f792889a 19113 this_type = read_unspecified_type (die, cu);
81a17f79 19114 break;
0114d602
DJ
19115 case DW_TAG_namespace:
19116 this_type = read_namespace_type (die, cu);
19117 break;
f55ee35c
JK
19118 case DW_TAG_module:
19119 this_type = read_module_type (die, cu);
19120 break;
a2c2acaf
MW
19121 case DW_TAG_atomic_type:
19122 this_type = read_tag_atomic_type (die, cu);
19123 break;
c906108c 19124 default:
3e43a32a
MS
19125 complaint (&symfile_complaints,
19126 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19127 dwarf_tag_name (die->tag));
c906108c
SS
19128 break;
19129 }
63d06c5c 19130
f792889a 19131 return this_type;
63d06c5c
DC
19132}
19133
abc72ce4
DE
19134/* See if we can figure out if the class lives in a namespace. We do
19135 this by looking for a member function; its demangled name will
19136 contain namespace info, if there is any.
19137 Return the computed name or NULL.
19138 Space for the result is allocated on the objfile's obstack.
19139 This is the full-die version of guess_partial_die_structure_name.
19140 In this case we know DIE has no useful parent. */
19141
19142static char *
19143guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19144{
19145 struct die_info *spec_die;
19146 struct dwarf2_cu *spec_cu;
19147 struct die_info *child;
19148
19149 spec_cu = cu;
19150 spec_die = die_specification (die, &spec_cu);
19151 if (spec_die != NULL)
19152 {
19153 die = spec_die;
19154 cu = spec_cu;
19155 }
19156
19157 for (child = die->child;
19158 child != NULL;
19159 child = child->sibling)
19160 {
19161 if (child->tag == DW_TAG_subprogram)
19162 {
7d45c7c3 19163 const char *linkage_name;
abc72ce4 19164
7d45c7c3
KB
19165 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19166 if (linkage_name == NULL)
19167 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19168 cu);
19169 if (linkage_name != NULL)
abc72ce4
DE
19170 {
19171 char *actual_name
19172 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19173 linkage_name);
abc72ce4
DE
19174 char *name = NULL;
19175
19176 if (actual_name != NULL)
19177 {
15d034d0 19178 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19179
19180 if (die_name != NULL
19181 && strcmp (die_name, actual_name) != 0)
19182 {
19183 /* Strip off the class name from the full name.
19184 We want the prefix. */
19185 int die_name_len = strlen (die_name);
19186 int actual_name_len = strlen (actual_name);
19187
19188 /* Test for '::' as a sanity check. */
19189 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19190 && actual_name[actual_name_len
19191 - die_name_len - 1] == ':')
abc72ce4 19192 name =
34a68019 19193 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
10f0c4bb
TT
19194 actual_name,
19195 actual_name_len - die_name_len - 2);
abc72ce4
DE
19196 }
19197 }
19198 xfree (actual_name);
19199 return name;
19200 }
19201 }
19202 }
19203
19204 return NULL;
19205}
19206
96408a79
SA
19207/* GCC might emit a nameless typedef that has a linkage name. Determine the
19208 prefix part in such case. See
19209 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19210
19211static char *
19212anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19213{
19214 struct attribute *attr;
19215 char *base;
19216
19217 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19218 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19219 return NULL;
19220
7d45c7c3 19221 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19222 return NULL;
19223
19224 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19225 if (attr == NULL)
19226 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19227 if (attr == NULL || DW_STRING (attr) == NULL)
19228 return NULL;
19229
19230 /* dwarf2_name had to be already called. */
19231 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19232
19233 /* Strip the base name, keep any leading namespaces/classes. */
19234 base = strrchr (DW_STRING (attr), ':');
19235 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19236 return "";
19237
34a68019 19238 return obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
10f0c4bb 19239 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
19240}
19241
fdde2d81 19242/* Return the name of the namespace/class that DIE is defined within,
0114d602 19243 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19244
0114d602
DJ
19245 For example, if we're within the method foo() in the following
19246 code:
19247
19248 namespace N {
19249 class C {
19250 void foo () {
19251 }
19252 };
19253 }
19254
19255 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19256
0d5cff50 19257static const char *
e142c38c 19258determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19259{
0114d602
DJ
19260 struct die_info *parent, *spec_die;
19261 struct dwarf2_cu *spec_cu;
19262 struct type *parent_type;
96408a79 19263 char *retval;
63d06c5c 19264
f55ee35c 19265 if (cu->language != language_cplus && cu->language != language_java
45280282 19266 && cu->language != language_fortran && cu->language != language_d)
0114d602
DJ
19267 return "";
19268
96408a79
SA
19269 retval = anonymous_struct_prefix (die, cu);
19270 if (retval)
19271 return retval;
19272
0114d602
DJ
19273 /* We have to be careful in the presence of DW_AT_specification.
19274 For example, with GCC 3.4, given the code
19275
19276 namespace N {
19277 void foo() {
19278 // Definition of N::foo.
19279 }
19280 }
19281
19282 then we'll have a tree of DIEs like this:
19283
19284 1: DW_TAG_compile_unit
19285 2: DW_TAG_namespace // N
19286 3: DW_TAG_subprogram // declaration of N::foo
19287 4: DW_TAG_subprogram // definition of N::foo
19288 DW_AT_specification // refers to die #3
19289
19290 Thus, when processing die #4, we have to pretend that we're in
19291 the context of its DW_AT_specification, namely the contex of die
19292 #3. */
19293 spec_cu = cu;
19294 spec_die = die_specification (die, &spec_cu);
19295 if (spec_die == NULL)
19296 parent = die->parent;
19297 else
63d06c5c 19298 {
0114d602
DJ
19299 parent = spec_die->parent;
19300 cu = spec_cu;
63d06c5c 19301 }
0114d602
DJ
19302
19303 if (parent == NULL)
19304 return "";
98bfdba5
PA
19305 else if (parent->building_fullname)
19306 {
19307 const char *name;
19308 const char *parent_name;
19309
19310 /* It has been seen on RealView 2.2 built binaries,
19311 DW_TAG_template_type_param types actually _defined_ as
19312 children of the parent class:
19313
19314 enum E {};
19315 template class <class Enum> Class{};
19316 Class<enum E> class_e;
19317
19318 1: DW_TAG_class_type (Class)
19319 2: DW_TAG_enumeration_type (E)
19320 3: DW_TAG_enumerator (enum1:0)
19321 3: DW_TAG_enumerator (enum2:1)
19322 ...
19323 2: DW_TAG_template_type_param
19324 DW_AT_type DW_FORM_ref_udata (E)
19325
19326 Besides being broken debug info, it can put GDB into an
19327 infinite loop. Consider:
19328
19329 When we're building the full name for Class<E>, we'll start
19330 at Class, and go look over its template type parameters,
19331 finding E. We'll then try to build the full name of E, and
19332 reach here. We're now trying to build the full name of E,
19333 and look over the parent DIE for containing scope. In the
19334 broken case, if we followed the parent DIE of E, we'd again
19335 find Class, and once again go look at its template type
19336 arguments, etc., etc. Simply don't consider such parent die
19337 as source-level parent of this die (it can't be, the language
19338 doesn't allow it), and break the loop here. */
19339 name = dwarf2_name (die, cu);
19340 parent_name = dwarf2_name (parent, cu);
19341 complaint (&symfile_complaints,
19342 _("template param type '%s' defined within parent '%s'"),
19343 name ? name : "<unknown>",
19344 parent_name ? parent_name : "<unknown>");
19345 return "";
19346 }
63d06c5c 19347 else
0114d602
DJ
19348 switch (parent->tag)
19349 {
63d06c5c 19350 case DW_TAG_namespace:
0114d602 19351 parent_type = read_type_die (parent, cu);
acebe513
UW
19352 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19353 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19354 Work around this problem here. */
19355 if (cu->language == language_cplus
19356 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19357 return "";
0114d602
DJ
19358 /* We give a name to even anonymous namespaces. */
19359 return TYPE_TAG_NAME (parent_type);
63d06c5c 19360 case DW_TAG_class_type:
680b30c7 19361 case DW_TAG_interface_type:
63d06c5c 19362 case DW_TAG_structure_type:
0114d602 19363 case DW_TAG_union_type:
f55ee35c 19364 case DW_TAG_module:
0114d602
DJ
19365 parent_type = read_type_die (parent, cu);
19366 if (TYPE_TAG_NAME (parent_type) != NULL)
19367 return TYPE_TAG_NAME (parent_type);
19368 else
19369 /* An anonymous structure is only allowed non-static data
19370 members; no typedefs, no member functions, et cetera.
19371 So it does not need a prefix. */
19372 return "";
abc72ce4 19373 case DW_TAG_compile_unit:
95554aad 19374 case DW_TAG_partial_unit:
abc72ce4
DE
19375 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19376 if (cu->language == language_cplus
8b70b953 19377 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
19378 && die->child != NULL
19379 && (die->tag == DW_TAG_class_type
19380 || die->tag == DW_TAG_structure_type
19381 || die->tag == DW_TAG_union_type))
19382 {
19383 char *name = guess_full_die_structure_name (die, cu);
19384 if (name != NULL)
19385 return name;
19386 }
19387 return "";
3d567982
TT
19388 case DW_TAG_enumeration_type:
19389 parent_type = read_type_die (parent, cu);
19390 if (TYPE_DECLARED_CLASS (parent_type))
19391 {
19392 if (TYPE_TAG_NAME (parent_type) != NULL)
19393 return TYPE_TAG_NAME (parent_type);
19394 return "";
19395 }
19396 /* Fall through. */
63d06c5c 19397 default:
8176b9b8 19398 return determine_prefix (parent, cu);
63d06c5c 19399 }
63d06c5c
DC
19400}
19401
3e43a32a
MS
19402/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19403 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19404 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19405 an obconcat, otherwise allocate storage for the result. The CU argument is
19406 used to determine the language and hence, the appropriate separator. */
987504bb 19407
f55ee35c 19408#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
19409
19410static char *
f55ee35c
JK
19411typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19412 int physname, struct dwarf2_cu *cu)
63d06c5c 19413{
f55ee35c 19414 const char *lead = "";
5c315b68 19415 const char *sep;
63d06c5c 19416
3e43a32a
MS
19417 if (suffix == NULL || suffix[0] == '\0'
19418 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
19419 sep = "";
19420 else if (cu->language == language_java)
19421 sep = ".";
45280282
IB
19422 else if (cu->language == language_d)
19423 {
19424 /* For D, the 'main' function could be defined in any module, but it
19425 should never be prefixed. */
19426 if (strcmp (suffix, "D main") == 0)
19427 {
19428 prefix = "";
19429 sep = "";
19430 }
19431 else
19432 sep = ".";
19433 }
f55ee35c
JK
19434 else if (cu->language == language_fortran && physname)
19435 {
19436 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19437 DW_AT_MIPS_linkage_name is preferred and used instead. */
19438
19439 lead = "__";
19440 sep = "_MOD_";
19441 }
987504bb
JJ
19442 else
19443 sep = "::";
63d06c5c 19444
6dd47d34
DE
19445 if (prefix == NULL)
19446 prefix = "";
19447 if (suffix == NULL)
19448 suffix = "";
19449
987504bb
JJ
19450 if (obs == NULL)
19451 {
3e43a32a
MS
19452 char *retval
19453 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 19454
f55ee35c
JK
19455 strcpy (retval, lead);
19456 strcat (retval, prefix);
6dd47d34
DE
19457 strcat (retval, sep);
19458 strcat (retval, suffix);
63d06c5c
DC
19459 return retval;
19460 }
987504bb
JJ
19461 else
19462 {
19463 /* We have an obstack. */
f55ee35c 19464 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 19465 }
63d06c5c
DC
19466}
19467
c906108c
SS
19468/* Return sibling of die, NULL if no sibling. */
19469
f9aca02d 19470static struct die_info *
fba45db2 19471sibling_die (struct die_info *die)
c906108c 19472{
639d11d3 19473 return die->sibling;
c906108c
SS
19474}
19475
71c25dea
TT
19476/* Get name of a die, return NULL if not found. */
19477
15d034d0
TT
19478static const char *
19479dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
19480 struct obstack *obstack)
19481{
19482 if (name && cu->language == language_cplus)
19483 {
19484 char *canon_name = cp_canonicalize_string (name);
19485
19486 if (canon_name != NULL)
19487 {
19488 if (strcmp (canon_name, name) != 0)
10f0c4bb 19489 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
19490 xfree (canon_name);
19491 }
19492 }
19493
19494 return name;
c906108c
SS
19495}
19496
96553a0c
DE
19497/* Get name of a die, return NULL if not found.
19498 Anonymous namespaces are converted to their magic string. */
9219021c 19499
15d034d0 19500static const char *
e142c38c 19501dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
19502{
19503 struct attribute *attr;
19504
e142c38c 19505 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 19506 if ((!attr || !DW_STRING (attr))
96553a0c 19507 && die->tag != DW_TAG_namespace
53832f31
TT
19508 && die->tag != DW_TAG_class_type
19509 && die->tag != DW_TAG_interface_type
19510 && die->tag != DW_TAG_structure_type
19511 && die->tag != DW_TAG_union_type)
71c25dea
TT
19512 return NULL;
19513
19514 switch (die->tag)
19515 {
19516 case DW_TAG_compile_unit:
95554aad 19517 case DW_TAG_partial_unit:
71c25dea
TT
19518 /* Compilation units have a DW_AT_name that is a filename, not
19519 a source language identifier. */
19520 case DW_TAG_enumeration_type:
19521 case DW_TAG_enumerator:
19522 /* These tags always have simple identifiers already; no need
19523 to canonicalize them. */
19524 return DW_STRING (attr);
907af001 19525
96553a0c
DE
19526 case DW_TAG_namespace:
19527 if (attr != NULL && DW_STRING (attr) != NULL)
19528 return DW_STRING (attr);
19529 return CP_ANONYMOUS_NAMESPACE_STR;
19530
418835cc
KS
19531 case DW_TAG_subprogram:
19532 /* Java constructors will all be named "<init>", so return
19533 the class name when we see this special case. */
19534 if (cu->language == language_java
19535 && DW_STRING (attr) != NULL
19536 && strcmp (DW_STRING (attr), "<init>") == 0)
19537 {
19538 struct dwarf2_cu *spec_cu = cu;
19539 struct die_info *spec_die;
19540
19541 /* GCJ will output '<init>' for Java constructor names.
19542 For this special case, return the name of the parent class. */
19543
cdc07690 19544 /* GCJ may output subprogram DIEs with AT_specification set.
418835cc
KS
19545 If so, use the name of the specified DIE. */
19546 spec_die = die_specification (die, &spec_cu);
19547 if (spec_die != NULL)
19548 return dwarf2_name (spec_die, spec_cu);
19549
19550 do
19551 {
19552 die = die->parent;
19553 if (die->tag == DW_TAG_class_type)
19554 return dwarf2_name (die, cu);
19555 }
95554aad
TT
19556 while (die->tag != DW_TAG_compile_unit
19557 && die->tag != DW_TAG_partial_unit);
418835cc 19558 }
907af001
UW
19559 break;
19560
19561 case DW_TAG_class_type:
19562 case DW_TAG_interface_type:
19563 case DW_TAG_structure_type:
19564 case DW_TAG_union_type:
19565 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
19566 structures or unions. These were of the form "._%d" in GCC 4.1,
19567 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
19568 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 19569 if (attr && DW_STRING (attr)
61012eef
GB
19570 && (startswith (DW_STRING (attr), "._")
19571 || startswith (DW_STRING (attr), "<anonymous")))
907af001 19572 return NULL;
53832f31
TT
19573
19574 /* GCC might emit a nameless typedef that has a linkage name. See
19575 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19576 if (!attr || DW_STRING (attr) == NULL)
19577 {
df5c6c50 19578 char *demangled = NULL;
53832f31
TT
19579
19580 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19581 if (attr == NULL)
19582 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19583
19584 if (attr == NULL || DW_STRING (attr) == NULL)
19585 return NULL;
19586
df5c6c50
JK
19587 /* Avoid demangling DW_STRING (attr) the second time on a second
19588 call for the same DIE. */
19589 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 19590 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
19591
19592 if (demangled)
19593 {
96408a79
SA
19594 char *base;
19595
53832f31 19596 /* FIXME: we already did this for the partial symbol... */
34a68019
TT
19597 DW_STRING (attr)
19598 = obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19599 demangled, strlen (demangled));
53832f31
TT
19600 DW_STRING_IS_CANONICAL (attr) = 1;
19601 xfree (demangled);
96408a79
SA
19602
19603 /* Strip any leading namespaces/classes, keep only the base name.
19604 DW_AT_name for named DIEs does not contain the prefixes. */
19605 base = strrchr (DW_STRING (attr), ':');
19606 if (base && base > DW_STRING (attr) && base[-1] == ':')
19607 return &base[1];
19608 else
19609 return DW_STRING (attr);
53832f31
TT
19610 }
19611 }
907af001
UW
19612 break;
19613
71c25dea 19614 default:
907af001
UW
19615 break;
19616 }
19617
19618 if (!DW_STRING_IS_CANONICAL (attr))
19619 {
19620 DW_STRING (attr)
19621 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 19622 &cu->objfile->per_bfd->storage_obstack);
907af001 19623 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 19624 }
907af001 19625 return DW_STRING (attr);
9219021c
DC
19626}
19627
19628/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
19629 is none. *EXT_CU is the CU containing DIE on input, and the CU
19630 containing the return value on output. */
9219021c
DC
19631
19632static struct die_info *
f2f0e013 19633dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
19634{
19635 struct attribute *attr;
9219021c 19636
f2f0e013 19637 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
19638 if (attr == NULL)
19639 return NULL;
19640
f2f0e013 19641 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
19642}
19643
c906108c
SS
19644/* Convert a DIE tag into its string name. */
19645
f39c6ffd 19646static const char *
aa1ee363 19647dwarf_tag_name (unsigned tag)
c906108c 19648{
f39c6ffd
TT
19649 const char *name = get_DW_TAG_name (tag);
19650
19651 if (name == NULL)
19652 return "DW_TAG_<unknown>";
19653
19654 return name;
c906108c
SS
19655}
19656
19657/* Convert a DWARF attribute code into its string name. */
19658
f39c6ffd 19659static const char *
aa1ee363 19660dwarf_attr_name (unsigned attr)
c906108c 19661{
f39c6ffd
TT
19662 const char *name;
19663
c764a876 19664#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
19665 if (attr == DW_AT_MIPS_fde)
19666 return "DW_AT_MIPS_fde";
19667#else
19668 if (attr == DW_AT_HP_block_index)
19669 return "DW_AT_HP_block_index";
c764a876 19670#endif
f39c6ffd
TT
19671
19672 name = get_DW_AT_name (attr);
19673
19674 if (name == NULL)
19675 return "DW_AT_<unknown>";
19676
19677 return name;
c906108c
SS
19678}
19679
19680/* Convert a DWARF value form code into its string name. */
19681
f39c6ffd 19682static const char *
aa1ee363 19683dwarf_form_name (unsigned form)
c906108c 19684{
f39c6ffd
TT
19685 const char *name = get_DW_FORM_name (form);
19686
19687 if (name == NULL)
19688 return "DW_FORM_<unknown>";
19689
19690 return name;
c906108c
SS
19691}
19692
19693static char *
fba45db2 19694dwarf_bool_name (unsigned mybool)
c906108c
SS
19695{
19696 if (mybool)
19697 return "TRUE";
19698 else
19699 return "FALSE";
19700}
19701
19702/* Convert a DWARF type code into its string name. */
19703
f39c6ffd 19704static const char *
aa1ee363 19705dwarf_type_encoding_name (unsigned enc)
c906108c 19706{
f39c6ffd 19707 const char *name = get_DW_ATE_name (enc);
c906108c 19708
f39c6ffd
TT
19709 if (name == NULL)
19710 return "DW_ATE_<unknown>";
c906108c 19711
f39c6ffd 19712 return name;
c906108c 19713}
c906108c 19714
f9aca02d 19715static void
d97bc12b 19716dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
19717{
19718 unsigned int i;
19719
d97bc12b
DE
19720 print_spaces (indent, f);
19721 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 19722 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
19723
19724 if (die->parent != NULL)
19725 {
19726 print_spaces (indent, f);
19727 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 19728 die->parent->offset.sect_off);
d97bc12b
DE
19729 }
19730
19731 print_spaces (indent, f);
19732 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 19733 dwarf_bool_name (die->child != NULL));
c906108c 19734
d97bc12b
DE
19735 print_spaces (indent, f);
19736 fprintf_unfiltered (f, " attributes:\n");
19737
c906108c
SS
19738 for (i = 0; i < die->num_attrs; ++i)
19739 {
d97bc12b
DE
19740 print_spaces (indent, f);
19741 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
19742 dwarf_attr_name (die->attrs[i].name),
19743 dwarf_form_name (die->attrs[i].form));
d97bc12b 19744
c906108c
SS
19745 switch (die->attrs[i].form)
19746 {
c906108c 19747 case DW_FORM_addr:
3019eac3 19748 case DW_FORM_GNU_addr_index:
d97bc12b 19749 fprintf_unfiltered (f, "address: ");
5af949e3 19750 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
19751 break;
19752 case DW_FORM_block2:
19753 case DW_FORM_block4:
19754 case DW_FORM_block:
19755 case DW_FORM_block1:
56eb65bd
SP
19756 fprintf_unfiltered (f, "block: size %s",
19757 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 19758 break;
2dc7f7b3 19759 case DW_FORM_exprloc:
56eb65bd
SP
19760 fprintf_unfiltered (f, "expression: size %s",
19761 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 19762 break;
4568ecf9
DE
19763 case DW_FORM_ref_addr:
19764 fprintf_unfiltered (f, "ref address: ");
19765 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19766 break;
36586728
TT
19767 case DW_FORM_GNU_ref_alt:
19768 fprintf_unfiltered (f, "alt ref address: ");
19769 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19770 break;
10b3939b
DJ
19771 case DW_FORM_ref1:
19772 case DW_FORM_ref2:
19773 case DW_FORM_ref4:
4568ecf9
DE
19774 case DW_FORM_ref8:
19775 case DW_FORM_ref_udata:
d97bc12b 19776 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 19777 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 19778 break;
c906108c
SS
19779 case DW_FORM_data1:
19780 case DW_FORM_data2:
19781 case DW_FORM_data4:
ce5d95e1 19782 case DW_FORM_data8:
c906108c
SS
19783 case DW_FORM_udata:
19784 case DW_FORM_sdata:
43bbcdc2
PH
19785 fprintf_unfiltered (f, "constant: %s",
19786 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 19787 break;
2dc7f7b3
TT
19788 case DW_FORM_sec_offset:
19789 fprintf_unfiltered (f, "section offset: %s",
19790 pulongest (DW_UNSND (&die->attrs[i])));
19791 break;
55f1336d 19792 case DW_FORM_ref_sig8:
ac9ec31b
DE
19793 fprintf_unfiltered (f, "signature: %s",
19794 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 19795 break;
c906108c 19796 case DW_FORM_string:
4bdf3d34 19797 case DW_FORM_strp:
3019eac3 19798 case DW_FORM_GNU_str_index:
36586728 19799 case DW_FORM_GNU_strp_alt:
8285870a 19800 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 19801 DW_STRING (&die->attrs[i])
8285870a
JK
19802 ? DW_STRING (&die->attrs[i]) : "",
19803 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
19804 break;
19805 case DW_FORM_flag:
19806 if (DW_UNSND (&die->attrs[i]))
d97bc12b 19807 fprintf_unfiltered (f, "flag: TRUE");
c906108c 19808 else
d97bc12b 19809 fprintf_unfiltered (f, "flag: FALSE");
c906108c 19810 break;
2dc7f7b3
TT
19811 case DW_FORM_flag_present:
19812 fprintf_unfiltered (f, "flag: TRUE");
19813 break;
a8329558 19814 case DW_FORM_indirect:
0963b4bd
MS
19815 /* The reader will have reduced the indirect form to
19816 the "base form" so this form should not occur. */
3e43a32a
MS
19817 fprintf_unfiltered (f,
19818 "unexpected attribute form: DW_FORM_indirect");
a8329558 19819 break;
c906108c 19820 default:
d97bc12b 19821 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 19822 die->attrs[i].form);
d97bc12b 19823 break;
c906108c 19824 }
d97bc12b 19825 fprintf_unfiltered (f, "\n");
c906108c
SS
19826 }
19827}
19828
f9aca02d 19829static void
d97bc12b 19830dump_die_for_error (struct die_info *die)
c906108c 19831{
d97bc12b
DE
19832 dump_die_shallow (gdb_stderr, 0, die);
19833}
19834
19835static void
19836dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
19837{
19838 int indent = level * 4;
19839
19840 gdb_assert (die != NULL);
19841
19842 if (level >= max_level)
19843 return;
19844
19845 dump_die_shallow (f, indent, die);
19846
19847 if (die->child != NULL)
c906108c 19848 {
d97bc12b
DE
19849 print_spaces (indent, f);
19850 fprintf_unfiltered (f, " Children:");
19851 if (level + 1 < max_level)
19852 {
19853 fprintf_unfiltered (f, "\n");
19854 dump_die_1 (f, level + 1, max_level, die->child);
19855 }
19856 else
19857 {
3e43a32a
MS
19858 fprintf_unfiltered (f,
19859 " [not printed, max nesting level reached]\n");
d97bc12b
DE
19860 }
19861 }
19862
19863 if (die->sibling != NULL && level > 0)
19864 {
19865 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
19866 }
19867}
19868
d97bc12b
DE
19869/* This is called from the pdie macro in gdbinit.in.
19870 It's not static so gcc will keep a copy callable from gdb. */
19871
19872void
19873dump_die (struct die_info *die, int max_level)
19874{
19875 dump_die_1 (gdb_stdlog, 0, max_level, die);
19876}
19877
f9aca02d 19878static void
51545339 19879store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19880{
51545339 19881 void **slot;
c906108c 19882
b64f50a1
JK
19883 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
19884 INSERT);
51545339
DJ
19885
19886 *slot = die;
c906108c
SS
19887}
19888
b64f50a1
JK
19889/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
19890 required kind. */
19891
19892static sect_offset
ff39bb5e 19893dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 19894{
4568ecf9 19895 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 19896
7771576e 19897 if (attr_form_is_ref (attr))
b64f50a1 19898 return retval;
93311388 19899
b64f50a1 19900 retval.sect_off = 0;
93311388
DE
19901 complaint (&symfile_complaints,
19902 _("unsupported die ref attribute form: '%s'"),
19903 dwarf_form_name (attr->form));
b64f50a1 19904 return retval;
c906108c
SS
19905}
19906
43bbcdc2
PH
19907/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
19908 * the value held by the attribute is not constant. */
a02abb62 19909
43bbcdc2 19910static LONGEST
ff39bb5e 19911dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
19912{
19913 if (attr->form == DW_FORM_sdata)
19914 return DW_SND (attr);
19915 else if (attr->form == DW_FORM_udata
19916 || attr->form == DW_FORM_data1
19917 || attr->form == DW_FORM_data2
19918 || attr->form == DW_FORM_data4
19919 || attr->form == DW_FORM_data8)
19920 return DW_UNSND (attr);
19921 else
19922 {
3e43a32a
MS
19923 complaint (&symfile_complaints,
19924 _("Attribute value is not a constant (%s)"),
a02abb62
JB
19925 dwarf_form_name (attr->form));
19926 return default_value;
19927 }
19928}
19929
348e048f
DE
19930/* Follow reference or signature attribute ATTR of SRC_DIE.
19931 On entry *REF_CU is the CU of SRC_DIE.
19932 On exit *REF_CU is the CU of the result. */
19933
19934static struct die_info *
ff39bb5e 19935follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
19936 struct dwarf2_cu **ref_cu)
19937{
19938 struct die_info *die;
19939
7771576e 19940 if (attr_form_is_ref (attr))
348e048f 19941 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 19942 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
19943 die = follow_die_sig (src_die, attr, ref_cu);
19944 else
19945 {
19946 dump_die_for_error (src_die);
19947 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 19948 objfile_name ((*ref_cu)->objfile));
348e048f
DE
19949 }
19950
19951 return die;
03dd20cc
DJ
19952}
19953
5c631832 19954/* Follow reference OFFSET.
673bfd45
DE
19955 On entry *REF_CU is the CU of the source die referencing OFFSET.
19956 On exit *REF_CU is the CU of the result.
19957 Returns NULL if OFFSET is invalid. */
f504f079 19958
f9aca02d 19959static struct die_info *
36586728
TT
19960follow_die_offset (sect_offset offset, int offset_in_dwz,
19961 struct dwarf2_cu **ref_cu)
c906108c 19962{
10b3939b 19963 struct die_info temp_die;
f2f0e013 19964 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 19965
348e048f
DE
19966 gdb_assert (cu->per_cu != NULL);
19967
98bfdba5
PA
19968 target_cu = cu;
19969
3019eac3 19970 if (cu->per_cu->is_debug_types)
348e048f
DE
19971 {
19972 /* .debug_types CUs cannot reference anything outside their CU.
19973 If they need to, they have to reference a signatured type via
55f1336d 19974 DW_FORM_ref_sig8. */
348e048f 19975 if (! offset_in_cu_p (&cu->header, offset))
5c631832 19976 return NULL;
348e048f 19977 }
36586728
TT
19978 else if (offset_in_dwz != cu->per_cu->is_dwz
19979 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
19980 {
19981 struct dwarf2_per_cu_data *per_cu;
9a619af0 19982
36586728
TT
19983 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
19984 cu->objfile);
03dd20cc
DJ
19985
19986 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
19987 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
19988 load_full_comp_unit (per_cu, cu->language);
03dd20cc 19989
10b3939b
DJ
19990 target_cu = per_cu->cu;
19991 }
98bfdba5
PA
19992 else if (cu->dies == NULL)
19993 {
19994 /* We're loading full DIEs during partial symbol reading. */
19995 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 19996 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 19997 }
c906108c 19998
f2f0e013 19999 *ref_cu = target_cu;
51545339 20000 temp_die.offset = offset;
b64f50a1 20001 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 20002}
10b3939b 20003
5c631832
JK
20004/* Follow reference attribute ATTR of SRC_DIE.
20005 On entry *REF_CU is the CU of SRC_DIE.
20006 On exit *REF_CU is the CU of the result. */
20007
20008static struct die_info *
ff39bb5e 20009follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20010 struct dwarf2_cu **ref_cu)
20011{
b64f50a1 20012 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20013 struct dwarf2_cu *cu = *ref_cu;
20014 struct die_info *die;
20015
36586728
TT
20016 die = follow_die_offset (offset,
20017 (attr->form == DW_FORM_GNU_ref_alt
20018 || cu->per_cu->is_dwz),
20019 ref_cu);
5c631832
JK
20020 if (!die)
20021 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20022 "at 0x%x [in module %s]"),
4262abfb
JK
20023 offset.sect_off, src_die->offset.sect_off,
20024 objfile_name (cu->objfile));
348e048f 20025
5c631832
JK
20026 return die;
20027}
20028
d83e736b
JK
20029/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20030 Returned value is intended for DW_OP_call*. Returned
20031 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20032
20033struct dwarf2_locexpr_baton
8b9737bf
TT
20034dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20035 struct dwarf2_per_cu_data *per_cu,
20036 CORE_ADDR (*get_frame_pc) (void *baton),
20037 void *baton)
5c631832 20038{
918dd910 20039 struct dwarf2_cu *cu;
5c631832
JK
20040 struct die_info *die;
20041 struct attribute *attr;
20042 struct dwarf2_locexpr_baton retval;
20043
8cf6f0b1
TT
20044 dw2_setup (per_cu->objfile);
20045
918dd910
JK
20046 if (per_cu->cu == NULL)
20047 load_cu (per_cu);
20048 cu = per_cu->cu;
cc12ce38
DE
20049 if (cu == NULL)
20050 {
20051 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20052 Instead just throw an error, not much else we can do. */
20053 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20054 offset.sect_off, objfile_name (per_cu->objfile));
20055 }
918dd910 20056
36586728 20057 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
20058 if (!die)
20059 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20060 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20061
20062 attr = dwarf2_attr (die, DW_AT_location, cu);
20063 if (!attr)
20064 {
e103e986
JK
20065 /* DWARF: "If there is no such attribute, then there is no effect.".
20066 DATA is ignored if SIZE is 0. */
5c631832 20067
e103e986 20068 retval.data = NULL;
5c631832
JK
20069 retval.size = 0;
20070 }
8cf6f0b1
TT
20071 else if (attr_form_is_section_offset (attr))
20072 {
20073 struct dwarf2_loclist_baton loclist_baton;
20074 CORE_ADDR pc = (*get_frame_pc) (baton);
20075 size_t size;
20076
20077 fill_in_loclist_baton (cu, &loclist_baton, attr);
20078
20079 retval.data = dwarf2_find_location_expression (&loclist_baton,
20080 &size, pc);
20081 retval.size = size;
20082 }
5c631832
JK
20083 else
20084 {
20085 if (!attr_form_is_block (attr))
20086 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20087 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
4262abfb 20088 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20089
20090 retval.data = DW_BLOCK (attr)->data;
20091 retval.size = DW_BLOCK (attr)->size;
20092 }
20093 retval.per_cu = cu->per_cu;
918dd910 20094
918dd910
JK
20095 age_cached_comp_units ();
20096
5c631832 20097 return retval;
348e048f
DE
20098}
20099
8b9737bf
TT
20100/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20101 offset. */
20102
20103struct dwarf2_locexpr_baton
20104dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20105 struct dwarf2_per_cu_data *per_cu,
20106 CORE_ADDR (*get_frame_pc) (void *baton),
20107 void *baton)
20108{
20109 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20110
20111 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20112}
20113
b6807d98
TT
20114/* Write a constant of a given type as target-ordered bytes into
20115 OBSTACK. */
20116
20117static const gdb_byte *
20118write_constant_as_bytes (struct obstack *obstack,
20119 enum bfd_endian byte_order,
20120 struct type *type,
20121 ULONGEST value,
20122 LONGEST *len)
20123{
20124 gdb_byte *result;
20125
20126 *len = TYPE_LENGTH (type);
20127 result = obstack_alloc (obstack, *len);
20128 store_unsigned_integer (result, *len, byte_order, value);
20129
20130 return result;
20131}
20132
20133/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20134 pointer to the constant bytes and set LEN to the length of the
20135 data. If memory is needed, allocate it on OBSTACK. If the DIE
20136 does not have a DW_AT_const_value, return NULL. */
20137
20138const gdb_byte *
20139dwarf2_fetch_constant_bytes (sect_offset offset,
20140 struct dwarf2_per_cu_data *per_cu,
20141 struct obstack *obstack,
20142 LONGEST *len)
20143{
20144 struct dwarf2_cu *cu;
20145 struct die_info *die;
20146 struct attribute *attr;
20147 const gdb_byte *result = NULL;
20148 struct type *type;
20149 LONGEST value;
20150 enum bfd_endian byte_order;
20151
20152 dw2_setup (per_cu->objfile);
20153
20154 if (per_cu->cu == NULL)
20155 load_cu (per_cu);
20156 cu = per_cu->cu;
cc12ce38
DE
20157 if (cu == NULL)
20158 {
20159 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20160 Instead just throw an error, not much else we can do. */
20161 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20162 offset.sect_off, objfile_name (per_cu->objfile));
20163 }
b6807d98
TT
20164
20165 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20166 if (!die)
20167 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20168 offset.sect_off, objfile_name (per_cu->objfile));
b6807d98
TT
20169
20170
20171 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20172 if (attr == NULL)
20173 return NULL;
20174
20175 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20176 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20177
20178 switch (attr->form)
20179 {
20180 case DW_FORM_addr:
20181 case DW_FORM_GNU_addr_index:
20182 {
20183 gdb_byte *tem;
20184
20185 *len = cu->header.addr_size;
20186 tem = obstack_alloc (obstack, *len);
20187 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20188 result = tem;
20189 }
20190 break;
20191 case DW_FORM_string:
20192 case DW_FORM_strp:
20193 case DW_FORM_GNU_str_index:
20194 case DW_FORM_GNU_strp_alt:
20195 /* DW_STRING is already allocated on the objfile obstack, point
20196 directly to it. */
20197 result = (const gdb_byte *) DW_STRING (attr);
20198 *len = strlen (DW_STRING (attr));
20199 break;
20200 case DW_FORM_block1:
20201 case DW_FORM_block2:
20202 case DW_FORM_block4:
20203 case DW_FORM_block:
20204 case DW_FORM_exprloc:
20205 result = DW_BLOCK (attr)->data;
20206 *len = DW_BLOCK (attr)->size;
20207 break;
20208
20209 /* The DW_AT_const_value attributes are supposed to carry the
20210 symbol's value "represented as it would be on the target
20211 architecture." By the time we get here, it's already been
20212 converted to host endianness, so we just need to sign- or
20213 zero-extend it as appropriate. */
20214 case DW_FORM_data1:
20215 type = die_type (die, cu);
20216 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20217 if (result == NULL)
20218 result = write_constant_as_bytes (obstack, byte_order,
20219 type, value, len);
20220 break;
20221 case DW_FORM_data2:
20222 type = die_type (die, cu);
20223 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20224 if (result == NULL)
20225 result = write_constant_as_bytes (obstack, byte_order,
20226 type, value, len);
20227 break;
20228 case DW_FORM_data4:
20229 type = die_type (die, cu);
20230 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20231 if (result == NULL)
20232 result = write_constant_as_bytes (obstack, byte_order,
20233 type, value, len);
20234 break;
20235 case DW_FORM_data8:
20236 type = die_type (die, cu);
20237 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20238 if (result == NULL)
20239 result = write_constant_as_bytes (obstack, byte_order,
20240 type, value, len);
20241 break;
20242
20243 case DW_FORM_sdata:
20244 type = die_type (die, cu);
20245 result = write_constant_as_bytes (obstack, byte_order,
20246 type, DW_SND (attr), len);
20247 break;
20248
20249 case DW_FORM_udata:
20250 type = die_type (die, cu);
20251 result = write_constant_as_bytes (obstack, byte_order,
20252 type, DW_UNSND (attr), len);
20253 break;
20254
20255 default:
20256 complaint (&symfile_complaints,
20257 _("unsupported const value attribute form: '%s'"),
20258 dwarf_form_name (attr->form));
20259 break;
20260 }
20261
20262 return result;
20263}
20264
8a9b8146
TT
20265/* Return the type of the DIE at DIE_OFFSET in the CU named by
20266 PER_CU. */
20267
20268struct type *
b64f50a1 20269dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20270 struct dwarf2_per_cu_data *per_cu)
20271{
b64f50a1
JK
20272 sect_offset die_offset_sect;
20273
8a9b8146 20274 dw2_setup (per_cu->objfile);
b64f50a1
JK
20275
20276 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20277 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
20278}
20279
ac9ec31b 20280/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20281 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20282 On exit *REF_CU is the CU of the result.
20283 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20284
20285static struct die_info *
ac9ec31b
DE
20286follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20287 struct dwarf2_cu **ref_cu)
348e048f
DE
20288{
20289 struct objfile *objfile = (*ref_cu)->objfile;
20290 struct die_info temp_die;
348e048f
DE
20291 struct dwarf2_cu *sig_cu;
20292 struct die_info *die;
20293
ac9ec31b
DE
20294 /* While it might be nice to assert sig_type->type == NULL here,
20295 we can get here for DW_AT_imported_declaration where we need
20296 the DIE not the type. */
348e048f
DE
20297
20298 /* If necessary, add it to the queue and load its DIEs. */
20299
95554aad 20300 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20301 read_signatured_type (sig_type);
348e048f 20302
348e048f 20303 sig_cu = sig_type->per_cu.cu;
69d751e3 20304 gdb_assert (sig_cu != NULL);
3019eac3
DE
20305 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20306 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
20307 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
20308 temp_die.offset.sect_off);
348e048f
DE
20309 if (die)
20310 {
796a7ff8
DE
20311 /* For .gdb_index version 7 keep track of included TUs.
20312 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20313 if (dwarf2_per_objfile->index_table != NULL
20314 && dwarf2_per_objfile->index_table->version <= 7)
20315 {
20316 VEC_safe_push (dwarf2_per_cu_ptr,
20317 (*ref_cu)->per_cu->imported_symtabs,
20318 sig_cu->per_cu);
20319 }
20320
348e048f
DE
20321 *ref_cu = sig_cu;
20322 return die;
20323 }
20324
ac9ec31b
DE
20325 return NULL;
20326}
20327
20328/* Follow signatured type referenced by ATTR in SRC_DIE.
20329 On entry *REF_CU is the CU of SRC_DIE.
20330 On exit *REF_CU is the CU of the result.
20331 The result is the DIE of the type.
20332 If the referenced type cannot be found an error is thrown. */
20333
20334static struct die_info *
ff39bb5e 20335follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
20336 struct dwarf2_cu **ref_cu)
20337{
20338 ULONGEST signature = DW_SIGNATURE (attr);
20339 struct signatured_type *sig_type;
20340 struct die_info *die;
20341
20342 gdb_assert (attr->form == DW_FORM_ref_sig8);
20343
a2ce51a0 20344 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
20345 /* sig_type will be NULL if the signatured type is missing from
20346 the debug info. */
20347 if (sig_type == NULL)
20348 {
20349 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20350 " from DIE at 0x%x [in module %s]"),
20351 hex_string (signature), src_die->offset.sect_off,
4262abfb 20352 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20353 }
20354
20355 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20356 if (die == NULL)
20357 {
20358 dump_die_for_error (src_die);
20359 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20360 " from DIE at 0x%x [in module %s]"),
20361 hex_string (signature), src_die->offset.sect_off,
4262abfb 20362 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20363 }
20364
20365 return die;
20366}
20367
20368/* Get the type specified by SIGNATURE referenced in DIE/CU,
20369 reading in and processing the type unit if necessary. */
20370
20371static struct type *
20372get_signatured_type (struct die_info *die, ULONGEST signature,
20373 struct dwarf2_cu *cu)
20374{
20375 struct signatured_type *sig_type;
20376 struct dwarf2_cu *type_cu;
20377 struct die_info *type_die;
20378 struct type *type;
20379
a2ce51a0 20380 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
20381 /* sig_type will be NULL if the signatured type is missing from
20382 the debug info. */
20383 if (sig_type == NULL)
20384 {
20385 complaint (&symfile_complaints,
20386 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20387 " from DIE at 0x%x [in module %s]"),
20388 hex_string (signature), die->offset.sect_off,
4262abfb 20389 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20390 return build_error_marker_type (cu, die);
20391 }
20392
20393 /* If we already know the type we're done. */
20394 if (sig_type->type != NULL)
20395 return sig_type->type;
20396
20397 type_cu = cu;
20398 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20399 if (type_die != NULL)
20400 {
20401 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20402 is created. This is important, for example, because for c++ classes
20403 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20404 type = read_type_die (type_die, type_cu);
20405 if (type == NULL)
20406 {
20407 complaint (&symfile_complaints,
20408 _("Dwarf Error: Cannot build signatured type %s"
20409 " referenced from DIE at 0x%x [in module %s]"),
20410 hex_string (signature), die->offset.sect_off,
4262abfb 20411 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20412 type = build_error_marker_type (cu, die);
20413 }
20414 }
20415 else
20416 {
20417 complaint (&symfile_complaints,
20418 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20419 " from DIE at 0x%x [in module %s]"),
20420 hex_string (signature), die->offset.sect_off,
4262abfb 20421 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20422 type = build_error_marker_type (cu, die);
20423 }
20424 sig_type->type = type;
20425
20426 return type;
20427}
20428
20429/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20430 reading in and processing the type unit if necessary. */
20431
20432static struct type *
ff39bb5e 20433get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 20434 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
20435{
20436 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 20437 if (attr_form_is_ref (attr))
ac9ec31b
DE
20438 {
20439 struct dwarf2_cu *type_cu = cu;
20440 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20441
20442 return read_type_die (type_die, type_cu);
20443 }
20444 else if (attr->form == DW_FORM_ref_sig8)
20445 {
20446 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20447 }
20448 else
20449 {
20450 complaint (&symfile_complaints,
20451 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20452 " at 0x%x [in module %s]"),
20453 dwarf_form_name (attr->form), die->offset.sect_off,
4262abfb 20454 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20455 return build_error_marker_type (cu, die);
20456 }
348e048f
DE
20457}
20458
e5fe5e75 20459/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
20460
20461static void
e5fe5e75 20462load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 20463{
52dc124a 20464 struct signatured_type *sig_type;
348e048f 20465
f4dc4d17
DE
20466 /* Caller is responsible for ensuring type_unit_groups don't get here. */
20467 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
20468
6721b2ec
DE
20469 /* We have the per_cu, but we need the signatured_type.
20470 Fortunately this is an easy translation. */
20471 gdb_assert (per_cu->is_debug_types);
20472 sig_type = (struct signatured_type *) per_cu;
348e048f 20473
6721b2ec 20474 gdb_assert (per_cu->cu == NULL);
348e048f 20475
52dc124a 20476 read_signatured_type (sig_type);
348e048f 20477
6721b2ec 20478 gdb_assert (per_cu->cu != NULL);
348e048f
DE
20479}
20480
dee91e82
DE
20481/* die_reader_func for read_signatured_type.
20482 This is identical to load_full_comp_unit_reader,
20483 but is kept separate for now. */
348e048f
DE
20484
20485static void
dee91e82 20486read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 20487 const gdb_byte *info_ptr,
dee91e82
DE
20488 struct die_info *comp_unit_die,
20489 int has_children,
20490 void *data)
348e048f 20491{
dee91e82 20492 struct dwarf2_cu *cu = reader->cu;
348e048f 20493
dee91e82
DE
20494 gdb_assert (cu->die_hash == NULL);
20495 cu->die_hash =
20496 htab_create_alloc_ex (cu->header.length / 12,
20497 die_hash,
20498 die_eq,
20499 NULL,
20500 &cu->comp_unit_obstack,
20501 hashtab_obstack_allocate,
20502 dummy_obstack_deallocate);
348e048f 20503
dee91e82
DE
20504 if (has_children)
20505 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
20506 &info_ptr, comp_unit_die);
20507 cu->dies = comp_unit_die;
20508 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
20509
20510 /* We try not to read any attributes in this function, because not
9cdd5dbd 20511 all CUs needed for references have been loaded yet, and symbol
348e048f 20512 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
20513 or we won't be able to build types correctly.
20514 Similarly, if we do not read the producer, we can not apply
20515 producer-specific interpretation. */
95554aad 20516 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 20517}
348e048f 20518
3019eac3
DE
20519/* Read in a signatured type and build its CU and DIEs.
20520 If the type is a stub for the real type in a DWO file,
20521 read in the real type from the DWO file as well. */
dee91e82
DE
20522
20523static void
20524read_signatured_type (struct signatured_type *sig_type)
20525{
20526 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 20527
3019eac3 20528 gdb_assert (per_cu->is_debug_types);
dee91e82 20529 gdb_assert (per_cu->cu == NULL);
348e048f 20530
f4dc4d17
DE
20531 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
20532 read_signatured_type_reader, NULL);
7ee85ab1 20533 sig_type->per_cu.tu_read = 1;
c906108c
SS
20534}
20535
c906108c
SS
20536/* Decode simple location descriptions.
20537 Given a pointer to a dwarf block that defines a location, compute
20538 the location and return the value.
20539
4cecd739
DJ
20540 NOTE drow/2003-11-18: This function is called in two situations
20541 now: for the address of static or global variables (partial symbols
20542 only) and for offsets into structures which are expected to be
20543 (more or less) constant. The partial symbol case should go away,
20544 and only the constant case should remain. That will let this
20545 function complain more accurately. A few special modes are allowed
20546 without complaint for global variables (for instance, global
20547 register values and thread-local values).
c906108c
SS
20548
20549 A location description containing no operations indicates that the
4cecd739 20550 object is optimized out. The return value is 0 for that case.
6b992462
DJ
20551 FIXME drow/2003-11-16: No callers check for this case any more; soon all
20552 callers will only want a very basic result and this can become a
21ae7a4d
JK
20553 complaint.
20554
20555 Note that stack[0] is unused except as a default error return. */
c906108c
SS
20556
20557static CORE_ADDR
e7c27a73 20558decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 20559{
e7c27a73 20560 struct objfile *objfile = cu->objfile;
56eb65bd
SP
20561 size_t i;
20562 size_t size = blk->size;
d521ce57 20563 const gdb_byte *data = blk->data;
21ae7a4d
JK
20564 CORE_ADDR stack[64];
20565 int stacki;
20566 unsigned int bytes_read, unsnd;
20567 gdb_byte op;
c906108c 20568
21ae7a4d
JK
20569 i = 0;
20570 stacki = 0;
20571 stack[stacki] = 0;
20572 stack[++stacki] = 0;
20573
20574 while (i < size)
20575 {
20576 op = data[i++];
20577 switch (op)
20578 {
20579 case DW_OP_lit0:
20580 case DW_OP_lit1:
20581 case DW_OP_lit2:
20582 case DW_OP_lit3:
20583 case DW_OP_lit4:
20584 case DW_OP_lit5:
20585 case DW_OP_lit6:
20586 case DW_OP_lit7:
20587 case DW_OP_lit8:
20588 case DW_OP_lit9:
20589 case DW_OP_lit10:
20590 case DW_OP_lit11:
20591 case DW_OP_lit12:
20592 case DW_OP_lit13:
20593 case DW_OP_lit14:
20594 case DW_OP_lit15:
20595 case DW_OP_lit16:
20596 case DW_OP_lit17:
20597 case DW_OP_lit18:
20598 case DW_OP_lit19:
20599 case DW_OP_lit20:
20600 case DW_OP_lit21:
20601 case DW_OP_lit22:
20602 case DW_OP_lit23:
20603 case DW_OP_lit24:
20604 case DW_OP_lit25:
20605 case DW_OP_lit26:
20606 case DW_OP_lit27:
20607 case DW_OP_lit28:
20608 case DW_OP_lit29:
20609 case DW_OP_lit30:
20610 case DW_OP_lit31:
20611 stack[++stacki] = op - DW_OP_lit0;
20612 break;
f1bea926 20613
21ae7a4d
JK
20614 case DW_OP_reg0:
20615 case DW_OP_reg1:
20616 case DW_OP_reg2:
20617 case DW_OP_reg3:
20618 case DW_OP_reg4:
20619 case DW_OP_reg5:
20620 case DW_OP_reg6:
20621 case DW_OP_reg7:
20622 case DW_OP_reg8:
20623 case DW_OP_reg9:
20624 case DW_OP_reg10:
20625 case DW_OP_reg11:
20626 case DW_OP_reg12:
20627 case DW_OP_reg13:
20628 case DW_OP_reg14:
20629 case DW_OP_reg15:
20630 case DW_OP_reg16:
20631 case DW_OP_reg17:
20632 case DW_OP_reg18:
20633 case DW_OP_reg19:
20634 case DW_OP_reg20:
20635 case DW_OP_reg21:
20636 case DW_OP_reg22:
20637 case DW_OP_reg23:
20638 case DW_OP_reg24:
20639 case DW_OP_reg25:
20640 case DW_OP_reg26:
20641 case DW_OP_reg27:
20642 case DW_OP_reg28:
20643 case DW_OP_reg29:
20644 case DW_OP_reg30:
20645 case DW_OP_reg31:
20646 stack[++stacki] = op - DW_OP_reg0;
20647 if (i < size)
20648 dwarf2_complex_location_expr_complaint ();
20649 break;
c906108c 20650
21ae7a4d
JK
20651 case DW_OP_regx:
20652 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
20653 i += bytes_read;
20654 stack[++stacki] = unsnd;
20655 if (i < size)
20656 dwarf2_complex_location_expr_complaint ();
20657 break;
c906108c 20658
21ae7a4d
JK
20659 case DW_OP_addr:
20660 stack[++stacki] = read_address (objfile->obfd, &data[i],
20661 cu, &bytes_read);
20662 i += bytes_read;
20663 break;
d53d4ac5 20664
21ae7a4d
JK
20665 case DW_OP_const1u:
20666 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
20667 i += 1;
20668 break;
20669
20670 case DW_OP_const1s:
20671 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
20672 i += 1;
20673 break;
20674
20675 case DW_OP_const2u:
20676 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
20677 i += 2;
20678 break;
20679
20680 case DW_OP_const2s:
20681 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
20682 i += 2;
20683 break;
d53d4ac5 20684
21ae7a4d
JK
20685 case DW_OP_const4u:
20686 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
20687 i += 4;
20688 break;
20689
20690 case DW_OP_const4s:
20691 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
20692 i += 4;
20693 break;
20694
585861ea
JK
20695 case DW_OP_const8u:
20696 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
20697 i += 8;
20698 break;
20699
21ae7a4d
JK
20700 case DW_OP_constu:
20701 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
20702 &bytes_read);
20703 i += bytes_read;
20704 break;
20705
20706 case DW_OP_consts:
20707 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
20708 i += bytes_read;
20709 break;
20710
20711 case DW_OP_dup:
20712 stack[stacki + 1] = stack[stacki];
20713 stacki++;
20714 break;
20715
20716 case DW_OP_plus:
20717 stack[stacki - 1] += stack[stacki];
20718 stacki--;
20719 break;
20720
20721 case DW_OP_plus_uconst:
20722 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
20723 &bytes_read);
20724 i += bytes_read;
20725 break;
20726
20727 case DW_OP_minus:
20728 stack[stacki - 1] -= stack[stacki];
20729 stacki--;
20730 break;
20731
20732 case DW_OP_deref:
20733 /* If we're not the last op, then we definitely can't encode
20734 this using GDB's address_class enum. This is valid for partial
20735 global symbols, although the variable's address will be bogus
20736 in the psymtab. */
20737 if (i < size)
20738 dwarf2_complex_location_expr_complaint ();
20739 break;
20740
20741 case DW_OP_GNU_push_tls_address:
20742 /* The top of the stack has the offset from the beginning
20743 of the thread control block at which the variable is located. */
20744 /* Nothing should follow this operator, so the top of stack would
20745 be returned. */
20746 /* This is valid for partial global symbols, but the variable's
585861ea
JK
20747 address will be bogus in the psymtab. Make it always at least
20748 non-zero to not look as a variable garbage collected by linker
20749 which have DW_OP_addr 0. */
21ae7a4d
JK
20750 if (i < size)
20751 dwarf2_complex_location_expr_complaint ();
585861ea 20752 stack[stacki]++;
21ae7a4d
JK
20753 break;
20754
20755 case DW_OP_GNU_uninit:
20756 break;
20757
3019eac3 20758 case DW_OP_GNU_addr_index:
49f6c839 20759 case DW_OP_GNU_const_index:
3019eac3
DE
20760 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
20761 &bytes_read);
20762 i += bytes_read;
20763 break;
20764
21ae7a4d
JK
20765 default:
20766 {
f39c6ffd 20767 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
20768
20769 if (name)
20770 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
20771 name);
20772 else
20773 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
20774 op);
20775 }
20776
20777 return (stack[stacki]);
d53d4ac5 20778 }
3c6e0cb3 20779
21ae7a4d
JK
20780 /* Enforce maximum stack depth of SIZE-1 to avoid writing
20781 outside of the allocated space. Also enforce minimum>0. */
20782 if (stacki >= ARRAY_SIZE (stack) - 1)
20783 {
20784 complaint (&symfile_complaints,
20785 _("location description stack overflow"));
20786 return 0;
20787 }
20788
20789 if (stacki <= 0)
20790 {
20791 complaint (&symfile_complaints,
20792 _("location description stack underflow"));
20793 return 0;
20794 }
20795 }
20796 return (stack[stacki]);
c906108c
SS
20797}
20798
20799/* memory allocation interface */
20800
c906108c 20801static struct dwarf_block *
7b5a2f43 20802dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
20803{
20804 struct dwarf_block *blk;
20805
20806 blk = (struct dwarf_block *)
7b5a2f43 20807 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
20808 return (blk);
20809}
20810
c906108c 20811static struct die_info *
b60c80d6 20812dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
20813{
20814 struct die_info *die;
b60c80d6
DJ
20815 size_t size = sizeof (struct die_info);
20816
20817 if (num_attrs > 1)
20818 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 20819
b60c80d6 20820 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
20821 memset (die, 0, sizeof (struct die_info));
20822 return (die);
20823}
2e276125
JB
20824
20825\f
20826/* Macro support. */
20827
233d95b5
JK
20828/* Return file name relative to the compilation directory of file number I in
20829 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 20830 responsible for freeing it. */
233d95b5 20831
2e276125 20832static char *
233d95b5 20833file_file_name (int file, struct line_header *lh)
2e276125 20834{
6a83a1e6
EZ
20835 /* Is the file number a valid index into the line header's file name
20836 table? Remember that file numbers start with one, not zero. */
20837 if (1 <= file && file <= lh->num_file_names)
20838 {
20839 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 20840
afa6c9ab
SL
20841 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
20842 || lh->include_dirs == NULL)
6a83a1e6 20843 return xstrdup (fe->name);
233d95b5
JK
20844 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
20845 fe->name, NULL);
6a83a1e6 20846 }
2e276125
JB
20847 else
20848 {
6a83a1e6
EZ
20849 /* The compiler produced a bogus file number. We can at least
20850 record the macro definitions made in the file, even if we
20851 won't be able to find the file by name. */
20852 char fake_name[80];
9a619af0 20853
8c042590
PM
20854 xsnprintf (fake_name, sizeof (fake_name),
20855 "<bad macro file number %d>", file);
2e276125 20856
6e70227d 20857 complaint (&symfile_complaints,
6a83a1e6
EZ
20858 _("bad file number in macro information (%d)"),
20859 file);
2e276125 20860
6a83a1e6 20861 return xstrdup (fake_name);
2e276125
JB
20862 }
20863}
20864
233d95b5
JK
20865/* Return the full name of file number I in *LH's file name table.
20866 Use COMP_DIR as the name of the current directory of the
20867 compilation. The result is allocated using xmalloc; the caller is
20868 responsible for freeing it. */
20869static char *
20870file_full_name (int file, struct line_header *lh, const char *comp_dir)
20871{
20872 /* Is the file number a valid index into the line header's file name
20873 table? Remember that file numbers start with one, not zero. */
20874 if (1 <= file && file <= lh->num_file_names)
20875 {
20876 char *relative = file_file_name (file, lh);
20877
20878 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
20879 return relative;
20880 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
20881 }
20882 else
20883 return file_file_name (file, lh);
20884}
20885
2e276125
JB
20886
20887static struct macro_source_file *
20888macro_start_file (int file, int line,
20889 struct macro_source_file *current_file,
43f3e411 20890 struct line_header *lh)
2e276125 20891{
233d95b5
JK
20892 /* File name relative to the compilation directory of this source file. */
20893 char *file_name = file_file_name (file, lh);
2e276125 20894
2e276125 20895 if (! current_file)
abc9d0dc 20896 {
fc474241
DE
20897 /* Note: We don't create a macro table for this compilation unit
20898 at all until we actually get a filename. */
43f3e411 20899 struct macro_table *macro_table = get_macro_table ();
fc474241 20900
abc9d0dc
TT
20901 /* If we have no current file, then this must be the start_file
20902 directive for the compilation unit's main source file. */
fc474241
DE
20903 current_file = macro_set_main (macro_table, file_name);
20904 macro_define_special (macro_table);
abc9d0dc 20905 }
2e276125 20906 else
233d95b5 20907 current_file = macro_include (current_file, line, file_name);
2e276125 20908
233d95b5 20909 xfree (file_name);
6e70227d 20910
2e276125
JB
20911 return current_file;
20912}
20913
20914
20915/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
20916 followed by a null byte. */
20917static char *
20918copy_string (const char *buf, int len)
20919{
20920 char *s = xmalloc (len + 1);
9a619af0 20921
2e276125
JB
20922 memcpy (s, buf, len);
20923 s[len] = '\0';
2e276125
JB
20924 return s;
20925}
20926
20927
20928static const char *
20929consume_improper_spaces (const char *p, const char *body)
20930{
20931 if (*p == ' ')
20932 {
4d3c2250 20933 complaint (&symfile_complaints,
3e43a32a
MS
20934 _("macro definition contains spaces "
20935 "in formal argument list:\n`%s'"),
4d3c2250 20936 body);
2e276125
JB
20937
20938 while (*p == ' ')
20939 p++;
20940 }
20941
20942 return p;
20943}
20944
20945
20946static void
20947parse_macro_definition (struct macro_source_file *file, int line,
20948 const char *body)
20949{
20950 const char *p;
20951
20952 /* The body string takes one of two forms. For object-like macro
20953 definitions, it should be:
20954
20955 <macro name> " " <definition>
20956
20957 For function-like macro definitions, it should be:
20958
20959 <macro name> "() " <definition>
20960 or
20961 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
20962
20963 Spaces may appear only where explicitly indicated, and in the
20964 <definition>.
20965
20966 The Dwarf 2 spec says that an object-like macro's name is always
20967 followed by a space, but versions of GCC around March 2002 omit
6e70227d 20968 the space when the macro's definition is the empty string.
2e276125
JB
20969
20970 The Dwarf 2 spec says that there should be no spaces between the
20971 formal arguments in a function-like macro's formal argument list,
20972 but versions of GCC around March 2002 include spaces after the
20973 commas. */
20974
20975
20976 /* Find the extent of the macro name. The macro name is terminated
20977 by either a space or null character (for an object-like macro) or
20978 an opening paren (for a function-like macro). */
20979 for (p = body; *p; p++)
20980 if (*p == ' ' || *p == '(')
20981 break;
20982
20983 if (*p == ' ' || *p == '\0')
20984 {
20985 /* It's an object-like macro. */
20986 int name_len = p - body;
20987 char *name = copy_string (body, name_len);
20988 const char *replacement;
20989
20990 if (*p == ' ')
20991 replacement = body + name_len + 1;
20992 else
20993 {
4d3c2250 20994 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
20995 replacement = body + name_len;
20996 }
6e70227d 20997
2e276125
JB
20998 macro_define_object (file, line, name, replacement);
20999
21000 xfree (name);
21001 }
21002 else if (*p == '(')
21003 {
21004 /* It's a function-like macro. */
21005 char *name = copy_string (body, p - body);
21006 int argc = 0;
21007 int argv_size = 1;
21008 char **argv = xmalloc (argv_size * sizeof (*argv));
21009
21010 p++;
21011
21012 p = consume_improper_spaces (p, body);
21013
21014 /* Parse the formal argument list. */
21015 while (*p && *p != ')')
21016 {
21017 /* Find the extent of the current argument name. */
21018 const char *arg_start = p;
21019
21020 while (*p && *p != ',' && *p != ')' && *p != ' ')
21021 p++;
21022
21023 if (! *p || p == arg_start)
4d3c2250 21024 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21025 else
21026 {
21027 /* Make sure argv has room for the new argument. */
21028 if (argc >= argv_size)
21029 {
21030 argv_size *= 2;
21031 argv = xrealloc (argv, argv_size * sizeof (*argv));
21032 }
21033
21034 argv[argc++] = copy_string (arg_start, p - arg_start);
21035 }
21036
21037 p = consume_improper_spaces (p, body);
21038
21039 /* Consume the comma, if present. */
21040 if (*p == ',')
21041 {
21042 p++;
21043
21044 p = consume_improper_spaces (p, body);
21045 }
21046 }
21047
21048 if (*p == ')')
21049 {
21050 p++;
21051
21052 if (*p == ' ')
21053 /* Perfectly formed definition, no complaints. */
21054 macro_define_function (file, line, name,
6e70227d 21055 argc, (const char **) argv,
2e276125
JB
21056 p + 1);
21057 else if (*p == '\0')
21058 {
21059 /* Complain, but do define it. */
4d3c2250 21060 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21061 macro_define_function (file, line, name,
6e70227d 21062 argc, (const char **) argv,
2e276125
JB
21063 p);
21064 }
21065 else
21066 /* Just complain. */
4d3c2250 21067 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21068 }
21069 else
21070 /* Just complain. */
4d3c2250 21071 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21072
21073 xfree (name);
21074 {
21075 int i;
21076
21077 for (i = 0; i < argc; i++)
21078 xfree (argv[i]);
21079 }
21080 xfree (argv);
21081 }
21082 else
4d3c2250 21083 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21084}
21085
cf2c3c16
TT
21086/* Skip some bytes from BYTES according to the form given in FORM.
21087 Returns the new pointer. */
2e276125 21088
d521ce57
TT
21089static const gdb_byte *
21090skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21091 enum dwarf_form form,
21092 unsigned int offset_size,
21093 struct dwarf2_section_info *section)
2e276125 21094{
cf2c3c16 21095 unsigned int bytes_read;
2e276125 21096
cf2c3c16 21097 switch (form)
2e276125 21098 {
cf2c3c16
TT
21099 case DW_FORM_data1:
21100 case DW_FORM_flag:
21101 ++bytes;
21102 break;
21103
21104 case DW_FORM_data2:
21105 bytes += 2;
21106 break;
21107
21108 case DW_FORM_data4:
21109 bytes += 4;
21110 break;
21111
21112 case DW_FORM_data8:
21113 bytes += 8;
21114 break;
21115
21116 case DW_FORM_string:
21117 read_direct_string (abfd, bytes, &bytes_read);
21118 bytes += bytes_read;
21119 break;
21120
21121 case DW_FORM_sec_offset:
21122 case DW_FORM_strp:
36586728 21123 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21124 bytes += offset_size;
21125 break;
21126
21127 case DW_FORM_block:
21128 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21129 bytes += bytes_read;
21130 break;
21131
21132 case DW_FORM_block1:
21133 bytes += 1 + read_1_byte (abfd, bytes);
21134 break;
21135 case DW_FORM_block2:
21136 bytes += 2 + read_2_bytes (abfd, bytes);
21137 break;
21138 case DW_FORM_block4:
21139 bytes += 4 + read_4_bytes (abfd, bytes);
21140 break;
21141
21142 case DW_FORM_sdata:
21143 case DW_FORM_udata:
3019eac3
DE
21144 case DW_FORM_GNU_addr_index:
21145 case DW_FORM_GNU_str_index:
d521ce57 21146 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21147 if (bytes == NULL)
21148 {
21149 dwarf2_section_buffer_overflow_complaint (section);
21150 return NULL;
21151 }
cf2c3c16
TT
21152 break;
21153
21154 default:
21155 {
21156 complain:
21157 complaint (&symfile_complaints,
21158 _("invalid form 0x%x in `%s'"),
a32a8923 21159 form, get_section_name (section));
cf2c3c16
TT
21160 return NULL;
21161 }
2e276125
JB
21162 }
21163
cf2c3c16
TT
21164 return bytes;
21165}
757a13d0 21166
cf2c3c16
TT
21167/* A helper for dwarf_decode_macros that handles skipping an unknown
21168 opcode. Returns an updated pointer to the macro data buffer; or,
21169 on error, issues a complaint and returns NULL. */
757a13d0 21170
d521ce57 21171static const gdb_byte *
cf2c3c16 21172skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21173 const gdb_byte **opcode_definitions,
21174 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21175 bfd *abfd,
21176 unsigned int offset_size,
21177 struct dwarf2_section_info *section)
21178{
21179 unsigned int bytes_read, i;
21180 unsigned long arg;
d521ce57 21181 const gdb_byte *defn;
2e276125 21182
cf2c3c16 21183 if (opcode_definitions[opcode] == NULL)
2e276125 21184 {
cf2c3c16
TT
21185 complaint (&symfile_complaints,
21186 _("unrecognized DW_MACFINO opcode 0x%x"),
21187 opcode);
21188 return NULL;
21189 }
2e276125 21190
cf2c3c16
TT
21191 defn = opcode_definitions[opcode];
21192 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21193 defn += bytes_read;
2e276125 21194
cf2c3c16
TT
21195 for (i = 0; i < arg; ++i)
21196 {
aead7601
SM
21197 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21198 (enum dwarf_form) defn[i], offset_size,
f664829e 21199 section);
cf2c3c16
TT
21200 if (mac_ptr == NULL)
21201 {
21202 /* skip_form_bytes already issued the complaint. */
21203 return NULL;
21204 }
21205 }
757a13d0 21206
cf2c3c16
TT
21207 return mac_ptr;
21208}
757a13d0 21209
cf2c3c16
TT
21210/* A helper function which parses the header of a macro section.
21211 If the macro section is the extended (for now called "GNU") type,
21212 then this updates *OFFSET_SIZE. Returns a pointer to just after
21213 the header, or issues a complaint and returns NULL on error. */
757a13d0 21214
d521ce57
TT
21215static const gdb_byte *
21216dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21217 bfd *abfd,
d521ce57 21218 const gdb_byte *mac_ptr,
cf2c3c16
TT
21219 unsigned int *offset_size,
21220 int section_is_gnu)
21221{
21222 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21223
cf2c3c16
TT
21224 if (section_is_gnu)
21225 {
21226 unsigned int version, flags;
757a13d0 21227
cf2c3c16
TT
21228 version = read_2_bytes (abfd, mac_ptr);
21229 if (version != 4)
21230 {
21231 complaint (&symfile_complaints,
21232 _("unrecognized version `%d' in .debug_macro section"),
21233 version);
21234 return NULL;
21235 }
21236 mac_ptr += 2;
757a13d0 21237
cf2c3c16
TT
21238 flags = read_1_byte (abfd, mac_ptr);
21239 ++mac_ptr;
21240 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21241
cf2c3c16
TT
21242 if ((flags & 2) != 0)
21243 /* We don't need the line table offset. */
21244 mac_ptr += *offset_size;
757a13d0 21245
cf2c3c16
TT
21246 /* Vendor opcode descriptions. */
21247 if ((flags & 4) != 0)
21248 {
21249 unsigned int i, count;
757a13d0 21250
cf2c3c16
TT
21251 count = read_1_byte (abfd, mac_ptr);
21252 ++mac_ptr;
21253 for (i = 0; i < count; ++i)
21254 {
21255 unsigned int opcode, bytes_read;
21256 unsigned long arg;
21257
21258 opcode = read_1_byte (abfd, mac_ptr);
21259 ++mac_ptr;
21260 opcode_definitions[opcode] = mac_ptr;
21261 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21262 mac_ptr += bytes_read;
21263 mac_ptr += arg;
21264 }
757a13d0 21265 }
cf2c3c16 21266 }
757a13d0 21267
cf2c3c16
TT
21268 return mac_ptr;
21269}
757a13d0 21270
cf2c3c16 21271/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 21272 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
21273
21274static void
d521ce57
TT
21275dwarf_decode_macro_bytes (bfd *abfd,
21276 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21277 struct macro_source_file *current_file,
43f3e411 21278 struct line_header *lh,
cf2c3c16 21279 struct dwarf2_section_info *section,
36586728 21280 int section_is_gnu, int section_is_dwz,
cf2c3c16 21281 unsigned int offset_size,
8fc3fc34 21282 htab_t include_hash)
cf2c3c16 21283{
4d663531 21284 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21285 enum dwarf_macro_record_type macinfo_type;
21286 int at_commandline;
d521ce57 21287 const gdb_byte *opcode_definitions[256];
757a13d0 21288
cf2c3c16
TT
21289 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21290 &offset_size, section_is_gnu);
21291 if (mac_ptr == NULL)
21292 {
21293 /* We already issued a complaint. */
21294 return;
21295 }
757a13d0
JK
21296
21297 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21298 GDB is still reading the definitions from command line. First
21299 DW_MACINFO_start_file will need to be ignored as it was already executed
21300 to create CURRENT_FILE for the main source holding also the command line
21301 definitions. On first met DW_MACINFO_start_file this flag is reset to
21302 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21303
21304 at_commandline = 1;
21305
21306 do
21307 {
21308 /* Do we at least have room for a macinfo type byte? */
21309 if (mac_ptr >= mac_end)
21310 {
f664829e 21311 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21312 break;
21313 }
21314
aead7601 21315 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21316 mac_ptr++;
21317
cf2c3c16
TT
21318 /* Note that we rely on the fact that the corresponding GNU and
21319 DWARF constants are the same. */
757a13d0
JK
21320 switch (macinfo_type)
21321 {
21322 /* A zero macinfo type indicates the end of the macro
21323 information. */
21324 case 0:
21325 break;
2e276125 21326
cf2c3c16
TT
21327 case DW_MACRO_GNU_define:
21328 case DW_MACRO_GNU_undef:
21329 case DW_MACRO_GNU_define_indirect:
21330 case DW_MACRO_GNU_undef_indirect:
36586728
TT
21331 case DW_MACRO_GNU_define_indirect_alt:
21332 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 21333 {
891d2f0b 21334 unsigned int bytes_read;
2e276125 21335 int line;
d521ce57 21336 const char *body;
cf2c3c16 21337 int is_define;
2e276125 21338
cf2c3c16
TT
21339 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21340 mac_ptr += bytes_read;
21341
21342 if (macinfo_type == DW_MACRO_GNU_define
21343 || macinfo_type == DW_MACRO_GNU_undef)
21344 {
21345 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21346 mac_ptr += bytes_read;
21347 }
21348 else
21349 {
21350 LONGEST str_offset;
21351
21352 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21353 mac_ptr += offset_size;
2e276125 21354
36586728 21355 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
21356 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
21357 || section_is_dwz)
36586728
TT
21358 {
21359 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21360
21361 body = read_indirect_string_from_dwz (dwz, str_offset);
21362 }
21363 else
21364 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
21365 }
21366
21367 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
21368 || macinfo_type == DW_MACRO_GNU_define_indirect
21369 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 21370 if (! current_file)
757a13d0
JK
21371 {
21372 /* DWARF violation as no main source is present. */
21373 complaint (&symfile_complaints,
21374 _("debug info with no main source gives macro %s "
21375 "on line %d: %s"),
cf2c3c16
TT
21376 is_define ? _("definition") : _("undefinition"),
21377 line, body);
757a13d0
JK
21378 break;
21379 }
3e43a32a
MS
21380 if ((line == 0 && !at_commandline)
21381 || (line != 0 && at_commandline))
4d3c2250 21382 complaint (&symfile_complaints,
757a13d0
JK
21383 _("debug info gives %s macro %s with %s line %d: %s"),
21384 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21385 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
21386 line == 0 ? _("zero") : _("non-zero"), line, body);
21387
cf2c3c16 21388 if (is_define)
757a13d0 21389 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
21390 else
21391 {
21392 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
21393 || macinfo_type == DW_MACRO_GNU_undef_indirect
21394 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
21395 macro_undef (current_file, line, body);
21396 }
2e276125
JB
21397 }
21398 break;
21399
cf2c3c16 21400 case DW_MACRO_GNU_start_file:
2e276125 21401 {
891d2f0b 21402 unsigned int bytes_read;
2e276125
JB
21403 int line, file;
21404
21405 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21406 mac_ptr += bytes_read;
21407 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21408 mac_ptr += bytes_read;
21409
3e43a32a
MS
21410 if ((line == 0 && !at_commandline)
21411 || (line != 0 && at_commandline))
757a13d0
JK
21412 complaint (&symfile_complaints,
21413 _("debug info gives source %d included "
21414 "from %s at %s line %d"),
21415 file, at_commandline ? _("command-line") : _("file"),
21416 line == 0 ? _("zero") : _("non-zero"), line);
21417
21418 if (at_commandline)
21419 {
cf2c3c16
TT
21420 /* This DW_MACRO_GNU_start_file was executed in the
21421 pass one. */
757a13d0
JK
21422 at_commandline = 0;
21423 }
21424 else
43f3e411 21425 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
21426 }
21427 break;
21428
cf2c3c16 21429 case DW_MACRO_GNU_end_file:
2e276125 21430 if (! current_file)
4d3c2250 21431 complaint (&symfile_complaints,
3e43a32a
MS
21432 _("macro debug info has an unmatched "
21433 "`close_file' directive"));
2e276125
JB
21434 else
21435 {
21436 current_file = current_file->included_by;
21437 if (! current_file)
21438 {
cf2c3c16 21439 enum dwarf_macro_record_type next_type;
2e276125
JB
21440
21441 /* GCC circa March 2002 doesn't produce the zero
21442 type byte marking the end of the compilation
21443 unit. Complain if it's not there, but exit no
21444 matter what. */
21445
21446 /* Do we at least have room for a macinfo type byte? */
21447 if (mac_ptr >= mac_end)
21448 {
f664829e 21449 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
21450 return;
21451 }
21452
21453 /* We don't increment mac_ptr here, so this is just
21454 a look-ahead. */
aead7601
SM
21455 next_type
21456 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21457 mac_ptr);
2e276125 21458 if (next_type != 0)
4d3c2250 21459 complaint (&symfile_complaints,
3e43a32a
MS
21460 _("no terminating 0-type entry for "
21461 "macros in `.debug_macinfo' section"));
2e276125
JB
21462
21463 return;
21464 }
21465 }
21466 break;
21467
cf2c3c16 21468 case DW_MACRO_GNU_transparent_include:
36586728 21469 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
21470 {
21471 LONGEST offset;
8fc3fc34 21472 void **slot;
a036ba48
TT
21473 bfd *include_bfd = abfd;
21474 struct dwarf2_section_info *include_section = section;
21475 struct dwarf2_section_info alt_section;
d521ce57 21476 const gdb_byte *include_mac_end = mac_end;
a036ba48 21477 int is_dwz = section_is_dwz;
d521ce57 21478 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
21479
21480 offset = read_offset_1 (abfd, mac_ptr, offset_size);
21481 mac_ptr += offset_size;
21482
a036ba48
TT
21483 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
21484 {
21485 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21486
4d663531 21487 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 21488
a036ba48 21489 include_section = &dwz->macro;
a32a8923 21490 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
21491 include_mac_end = dwz->macro.buffer + dwz->macro.size;
21492 is_dwz = 1;
21493 }
21494
21495 new_mac_ptr = include_section->buffer + offset;
21496 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
21497
8fc3fc34
TT
21498 if (*slot != NULL)
21499 {
21500 /* This has actually happened; see
21501 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
21502 complaint (&symfile_complaints,
21503 _("recursive DW_MACRO_GNU_transparent_include in "
21504 ".debug_macro section"));
21505 }
21506 else
21507 {
d521ce57 21508 *slot = (void *) new_mac_ptr;
36586728 21509
a036ba48 21510 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 21511 include_mac_end, current_file, lh,
36586728 21512 section, section_is_gnu, is_dwz,
4d663531 21513 offset_size, include_hash);
8fc3fc34 21514
d521ce57 21515 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 21516 }
cf2c3c16
TT
21517 }
21518 break;
21519
2e276125 21520 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
21521 if (!section_is_gnu)
21522 {
21523 unsigned int bytes_read;
21524 int constant;
2e276125 21525
cf2c3c16
TT
21526 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21527 mac_ptr += bytes_read;
21528 read_direct_string (abfd, mac_ptr, &bytes_read);
21529 mac_ptr += bytes_read;
2e276125 21530
cf2c3c16
TT
21531 /* We don't recognize any vendor extensions. */
21532 break;
21533 }
21534 /* FALLTHROUGH */
21535
21536 default:
21537 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 21538 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
21539 section);
21540 if (mac_ptr == NULL)
21541 return;
21542 break;
2e276125 21543 }
757a13d0 21544 } while (macinfo_type != 0);
2e276125 21545}
8e19ed76 21546
cf2c3c16 21547static void
09262596 21548dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 21549 int section_is_gnu)
cf2c3c16 21550{
bb5ed363 21551 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
21552 struct line_header *lh = cu->line_header;
21553 bfd *abfd;
d521ce57 21554 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
21555 struct macro_source_file *current_file = 0;
21556 enum dwarf_macro_record_type macinfo_type;
21557 unsigned int offset_size = cu->header.offset_size;
d521ce57 21558 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
21559 struct cleanup *cleanup;
21560 htab_t include_hash;
21561 void **slot;
09262596
DE
21562 struct dwarf2_section_info *section;
21563 const char *section_name;
21564
21565 if (cu->dwo_unit != NULL)
21566 {
21567 if (section_is_gnu)
21568 {
21569 section = &cu->dwo_unit->dwo_file->sections.macro;
21570 section_name = ".debug_macro.dwo";
21571 }
21572 else
21573 {
21574 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21575 section_name = ".debug_macinfo.dwo";
21576 }
21577 }
21578 else
21579 {
21580 if (section_is_gnu)
21581 {
21582 section = &dwarf2_per_objfile->macro;
21583 section_name = ".debug_macro";
21584 }
21585 else
21586 {
21587 section = &dwarf2_per_objfile->macinfo;
21588 section_name = ".debug_macinfo";
21589 }
21590 }
cf2c3c16 21591
bb5ed363 21592 dwarf2_read_section (objfile, section);
cf2c3c16
TT
21593 if (section->buffer == NULL)
21594 {
fceca515 21595 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
21596 return;
21597 }
a32a8923 21598 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
21599
21600 /* First pass: Find the name of the base filename.
21601 This filename is needed in order to process all macros whose definition
21602 (or undefinition) comes from the command line. These macros are defined
21603 before the first DW_MACINFO_start_file entry, and yet still need to be
21604 associated to the base file.
21605
21606 To determine the base file name, we scan the macro definitions until we
21607 reach the first DW_MACINFO_start_file entry. We then initialize
21608 CURRENT_FILE accordingly so that any macro definition found before the
21609 first DW_MACINFO_start_file can still be associated to the base file. */
21610
21611 mac_ptr = section->buffer + offset;
21612 mac_end = section->buffer + section->size;
21613
21614 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21615 &offset_size, section_is_gnu);
21616 if (mac_ptr == NULL)
21617 {
21618 /* We already issued a complaint. */
21619 return;
21620 }
21621
21622 do
21623 {
21624 /* Do we at least have room for a macinfo type byte? */
21625 if (mac_ptr >= mac_end)
21626 {
21627 /* Complaint is printed during the second pass as GDB will probably
21628 stop the first pass earlier upon finding
21629 DW_MACINFO_start_file. */
21630 break;
21631 }
21632
aead7601 21633 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
21634 mac_ptr++;
21635
21636 /* Note that we rely on the fact that the corresponding GNU and
21637 DWARF constants are the same. */
21638 switch (macinfo_type)
21639 {
21640 /* A zero macinfo type indicates the end of the macro
21641 information. */
21642 case 0:
21643 break;
21644
21645 case DW_MACRO_GNU_define:
21646 case DW_MACRO_GNU_undef:
21647 /* Only skip the data by MAC_PTR. */
21648 {
21649 unsigned int bytes_read;
21650
21651 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21652 mac_ptr += bytes_read;
21653 read_direct_string (abfd, mac_ptr, &bytes_read);
21654 mac_ptr += bytes_read;
21655 }
21656 break;
21657
21658 case DW_MACRO_GNU_start_file:
21659 {
21660 unsigned int bytes_read;
21661 int line, file;
21662
21663 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21664 mac_ptr += bytes_read;
21665 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21666 mac_ptr += bytes_read;
21667
43f3e411 21668 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
21669 }
21670 break;
21671
21672 case DW_MACRO_GNU_end_file:
21673 /* No data to skip by MAC_PTR. */
21674 break;
21675
21676 case DW_MACRO_GNU_define_indirect:
21677 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
21678 case DW_MACRO_GNU_define_indirect_alt:
21679 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
21680 {
21681 unsigned int bytes_read;
21682
21683 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21684 mac_ptr += bytes_read;
21685 mac_ptr += offset_size;
21686 }
21687 break;
21688
21689 case DW_MACRO_GNU_transparent_include:
f7a35f02 21690 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
21691 /* Note that, according to the spec, a transparent include
21692 chain cannot call DW_MACRO_GNU_start_file. So, we can just
21693 skip this opcode. */
21694 mac_ptr += offset_size;
21695 break;
21696
21697 case DW_MACINFO_vendor_ext:
21698 /* Only skip the data by MAC_PTR. */
21699 if (!section_is_gnu)
21700 {
21701 unsigned int bytes_read;
21702
21703 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21704 mac_ptr += bytes_read;
21705 read_direct_string (abfd, mac_ptr, &bytes_read);
21706 mac_ptr += bytes_read;
21707 }
21708 /* FALLTHROUGH */
21709
21710 default:
21711 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 21712 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
21713 section);
21714 if (mac_ptr == NULL)
21715 return;
21716 break;
21717 }
21718 } while (macinfo_type != 0 && current_file == NULL);
21719
21720 /* Second pass: Process all entries.
21721
21722 Use the AT_COMMAND_LINE flag to determine whether we are still processing
21723 command-line macro definitions/undefinitions. This flag is unset when we
21724 reach the first DW_MACINFO_start_file entry. */
21725
8fc3fc34
TT
21726 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
21727 NULL, xcalloc, xfree);
21728 cleanup = make_cleanup_htab_delete (include_hash);
21729 mac_ptr = section->buffer + offset;
21730 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 21731 *slot = (void *) mac_ptr;
8fc3fc34 21732 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 21733 current_file, lh, section,
4d663531 21734 section_is_gnu, 0, offset_size, include_hash);
8fc3fc34 21735 do_cleanups (cleanup);
cf2c3c16
TT
21736}
21737
8e19ed76 21738/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 21739 if so return true else false. */
380bca97 21740
8e19ed76 21741static int
6e5a29e1 21742attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
21743{
21744 return (attr == NULL ? 0 :
21745 attr->form == DW_FORM_block1
21746 || attr->form == DW_FORM_block2
21747 || attr->form == DW_FORM_block4
2dc7f7b3
TT
21748 || attr->form == DW_FORM_block
21749 || attr->form == DW_FORM_exprloc);
8e19ed76 21750}
4c2df51b 21751
c6a0999f
JB
21752/* Return non-zero if ATTR's value is a section offset --- classes
21753 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
21754 You may use DW_UNSND (attr) to retrieve such offsets.
21755
21756 Section 7.5.4, "Attribute Encodings", explains that no attribute
21757 may have a value that belongs to more than one of these classes; it
21758 would be ambiguous if we did, because we use the same forms for all
21759 of them. */
380bca97 21760
3690dd37 21761static int
6e5a29e1 21762attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
21763{
21764 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
21765 || attr->form == DW_FORM_data8
21766 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
21767}
21768
3690dd37
JB
21769/* Return non-zero if ATTR's value falls in the 'constant' class, or
21770 zero otherwise. When this function returns true, you can apply
21771 dwarf2_get_attr_constant_value to it.
21772
21773 However, note that for some attributes you must check
21774 attr_form_is_section_offset before using this test. DW_FORM_data4
21775 and DW_FORM_data8 are members of both the constant class, and of
21776 the classes that contain offsets into other debug sections
21777 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
21778 that, if an attribute's can be either a constant or one of the
21779 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
21780 taken as section offsets, not constants. */
380bca97 21781
3690dd37 21782static int
6e5a29e1 21783attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
21784{
21785 switch (attr->form)
21786 {
21787 case DW_FORM_sdata:
21788 case DW_FORM_udata:
21789 case DW_FORM_data1:
21790 case DW_FORM_data2:
21791 case DW_FORM_data4:
21792 case DW_FORM_data8:
21793 return 1;
21794 default:
21795 return 0;
21796 }
21797}
21798
7771576e
SA
21799
21800/* DW_ADDR is always stored already as sect_offset; despite for the forms
21801 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
21802
21803static int
6e5a29e1 21804attr_form_is_ref (const struct attribute *attr)
7771576e
SA
21805{
21806 switch (attr->form)
21807 {
21808 case DW_FORM_ref_addr:
21809 case DW_FORM_ref1:
21810 case DW_FORM_ref2:
21811 case DW_FORM_ref4:
21812 case DW_FORM_ref8:
21813 case DW_FORM_ref_udata:
21814 case DW_FORM_GNU_ref_alt:
21815 return 1;
21816 default:
21817 return 0;
21818 }
21819}
21820
3019eac3
DE
21821/* Return the .debug_loc section to use for CU.
21822 For DWO files use .debug_loc.dwo. */
21823
21824static struct dwarf2_section_info *
21825cu_debug_loc_section (struct dwarf2_cu *cu)
21826{
21827 if (cu->dwo_unit)
21828 return &cu->dwo_unit->dwo_file->sections.loc;
21829 return &dwarf2_per_objfile->loc;
21830}
21831
8cf6f0b1
TT
21832/* A helper function that fills in a dwarf2_loclist_baton. */
21833
21834static void
21835fill_in_loclist_baton (struct dwarf2_cu *cu,
21836 struct dwarf2_loclist_baton *baton,
ff39bb5e 21837 const struct attribute *attr)
8cf6f0b1 21838{
3019eac3
DE
21839 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21840
21841 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
21842
21843 baton->per_cu = cu->per_cu;
21844 gdb_assert (baton->per_cu);
21845 /* We don't know how long the location list is, but make sure we
21846 don't run off the edge of the section. */
3019eac3
DE
21847 baton->size = section->size - DW_UNSND (attr);
21848 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 21849 baton->base_address = cu->base_address;
f664829e 21850 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
21851}
21852
4c2df51b 21853static void
ff39bb5e 21854dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 21855 struct dwarf2_cu *cu, int is_block)
4c2df51b 21856{
bb5ed363 21857 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 21858 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 21859
3690dd37 21860 if (attr_form_is_section_offset (attr)
3019eac3 21861 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
21862 the section. If so, fall through to the complaint in the
21863 other branch. */
3019eac3 21864 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 21865 {
0d53c4c4 21866 struct dwarf2_loclist_baton *baton;
4c2df51b 21867
bb5ed363 21868 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 21869 sizeof (struct dwarf2_loclist_baton));
4c2df51b 21870
8cf6f0b1 21871 fill_in_loclist_baton (cu, baton, attr);
be391dca 21872
d00adf39 21873 if (cu->base_known == 0)
0d53c4c4 21874 complaint (&symfile_complaints,
3e43a32a
MS
21875 _("Location list used without "
21876 "specifying the CU base address."));
4c2df51b 21877
f1e6e072
TT
21878 SYMBOL_ACLASS_INDEX (sym) = (is_block
21879 ? dwarf2_loclist_block_index
21880 : dwarf2_loclist_index);
0d53c4c4
DJ
21881 SYMBOL_LOCATION_BATON (sym) = baton;
21882 }
21883 else
21884 {
21885 struct dwarf2_locexpr_baton *baton;
21886
bb5ed363 21887 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 21888 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
21889 baton->per_cu = cu->per_cu;
21890 gdb_assert (baton->per_cu);
0d53c4c4
DJ
21891
21892 if (attr_form_is_block (attr))
21893 {
21894 /* Note that we're just copying the block's data pointer
21895 here, not the actual data. We're still pointing into the
6502dd73
DJ
21896 info_buffer for SYM's objfile; right now we never release
21897 that buffer, but when we do clean up properly this may
21898 need to change. */
0d53c4c4
DJ
21899 baton->size = DW_BLOCK (attr)->size;
21900 baton->data = DW_BLOCK (attr)->data;
21901 }
21902 else
21903 {
21904 dwarf2_invalid_attrib_class_complaint ("location description",
21905 SYMBOL_NATURAL_NAME (sym));
21906 baton->size = 0;
0d53c4c4 21907 }
6e70227d 21908
f1e6e072
TT
21909 SYMBOL_ACLASS_INDEX (sym) = (is_block
21910 ? dwarf2_locexpr_block_index
21911 : dwarf2_locexpr_index);
0d53c4c4
DJ
21912 SYMBOL_LOCATION_BATON (sym) = baton;
21913 }
4c2df51b 21914}
6502dd73 21915
9aa1f1e3
TT
21916/* Return the OBJFILE associated with the compilation unit CU. If CU
21917 came from a separate debuginfo file, then the master objfile is
21918 returned. */
ae0d2f24
UW
21919
21920struct objfile *
21921dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
21922{
9291a0cd 21923 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
21924
21925 /* Return the master objfile, so that we can report and look up the
21926 correct file containing this variable. */
21927 if (objfile->separate_debug_objfile_backlink)
21928 objfile = objfile->separate_debug_objfile_backlink;
21929
21930 return objfile;
21931}
21932
96408a79
SA
21933/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
21934 (CU_HEADERP is unused in such case) or prepare a temporary copy at
21935 CU_HEADERP first. */
21936
21937static const struct comp_unit_head *
21938per_cu_header_read_in (struct comp_unit_head *cu_headerp,
21939 struct dwarf2_per_cu_data *per_cu)
21940{
d521ce57 21941 const gdb_byte *info_ptr;
96408a79
SA
21942
21943 if (per_cu->cu)
21944 return &per_cu->cu->header;
21945
8a0459fd 21946 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
21947
21948 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 21949 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
21950
21951 return cu_headerp;
21952}
21953
ae0d2f24
UW
21954/* Return the address size given in the compilation unit header for CU. */
21955
98714339 21956int
ae0d2f24
UW
21957dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
21958{
96408a79
SA
21959 struct comp_unit_head cu_header_local;
21960 const struct comp_unit_head *cu_headerp;
c471e790 21961
96408a79
SA
21962 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21963
21964 return cu_headerp->addr_size;
ae0d2f24
UW
21965}
21966
9eae7c52
TT
21967/* Return the offset size given in the compilation unit header for CU. */
21968
21969int
21970dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
21971{
96408a79
SA
21972 struct comp_unit_head cu_header_local;
21973 const struct comp_unit_head *cu_headerp;
9c6c53f7 21974
96408a79
SA
21975 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21976
21977 return cu_headerp->offset_size;
21978}
21979
21980/* See its dwarf2loc.h declaration. */
21981
21982int
21983dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
21984{
21985 struct comp_unit_head cu_header_local;
21986 const struct comp_unit_head *cu_headerp;
21987
21988 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21989
21990 if (cu_headerp->version == 2)
21991 return cu_headerp->addr_size;
21992 else
21993 return cu_headerp->offset_size;
181cebd4
JK
21994}
21995
9aa1f1e3
TT
21996/* Return the text offset of the CU. The returned offset comes from
21997 this CU's objfile. If this objfile came from a separate debuginfo
21998 file, then the offset may be different from the corresponding
21999 offset in the parent objfile. */
22000
22001CORE_ADDR
22002dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22003{
bb3fa9d0 22004 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22005
22006 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22007}
22008
348e048f
DE
22009/* Locate the .debug_info compilation unit from CU's objfile which contains
22010 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22011
22012static struct dwarf2_per_cu_data *
b64f50a1 22013dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 22014 unsigned int offset_in_dwz,
ae038cb0
DJ
22015 struct objfile *objfile)
22016{
22017 struct dwarf2_per_cu_data *this_cu;
22018 int low, high;
36586728 22019 const sect_offset *cu_off;
ae038cb0 22020
ae038cb0
DJ
22021 low = 0;
22022 high = dwarf2_per_objfile->n_comp_units - 1;
22023 while (high > low)
22024 {
36586728 22025 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22026 int mid = low + (high - low) / 2;
9a619af0 22027
36586728
TT
22028 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22029 cu_off = &mid_cu->offset;
22030 if (mid_cu->is_dwz > offset_in_dwz
22031 || (mid_cu->is_dwz == offset_in_dwz
22032 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
22033 high = mid;
22034 else
22035 low = mid + 1;
22036 }
22037 gdb_assert (low == high);
36586728
TT
22038 this_cu = dwarf2_per_objfile->all_comp_units[low];
22039 cu_off = &this_cu->offset;
22040 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 22041 {
36586728 22042 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
22043 error (_("Dwarf Error: could not find partial DIE containing "
22044 "offset 0x%lx [in module %s]"),
b64f50a1 22045 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 22046
b64f50a1
JK
22047 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22048 <= offset.sect_off);
ae038cb0
DJ
22049 return dwarf2_per_objfile->all_comp_units[low-1];
22050 }
22051 else
22052 {
22053 this_cu = dwarf2_per_objfile->all_comp_units[low];
22054 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
22055 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22056 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22057 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
22058 return this_cu;
22059 }
22060}
22061
23745b47 22062/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22063
9816fde3 22064static void
23745b47 22065init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22066{
9816fde3 22067 memset (cu, 0, sizeof (*cu));
23745b47
DE
22068 per_cu->cu = cu;
22069 cu->per_cu = per_cu;
22070 cu->objfile = per_cu->objfile;
93311388 22071 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22072}
22073
22074/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22075
22076static void
95554aad
TT
22077prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22078 enum language pretend_language)
9816fde3
JK
22079{
22080 struct attribute *attr;
22081
22082 /* Set the language we're debugging. */
22083 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22084 if (attr)
22085 set_cu_language (DW_UNSND (attr), cu);
22086 else
9cded63f 22087 {
95554aad 22088 cu->language = pretend_language;
9cded63f
TT
22089 cu->language_defn = language_def (cu->language);
22090 }
dee91e82 22091
7d45c7c3 22092 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22093}
22094
ae038cb0
DJ
22095/* Release one cached compilation unit, CU. We unlink it from the tree
22096 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22097 the caller is responsible for that.
22098 NOTE: DATA is a void * because this function is also used as a
22099 cleanup routine. */
ae038cb0
DJ
22100
22101static void
68dc6402 22102free_heap_comp_unit (void *data)
ae038cb0
DJ
22103{
22104 struct dwarf2_cu *cu = data;
22105
23745b47
DE
22106 gdb_assert (cu->per_cu != NULL);
22107 cu->per_cu->cu = NULL;
ae038cb0
DJ
22108 cu->per_cu = NULL;
22109
22110 obstack_free (&cu->comp_unit_obstack, NULL);
22111
22112 xfree (cu);
22113}
22114
72bf9492 22115/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22116 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22117 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22118
22119static void
22120free_stack_comp_unit (void *data)
22121{
22122 struct dwarf2_cu *cu = data;
22123
23745b47
DE
22124 gdb_assert (cu->per_cu != NULL);
22125 cu->per_cu->cu = NULL;
22126 cu->per_cu = NULL;
22127
72bf9492
DJ
22128 obstack_free (&cu->comp_unit_obstack, NULL);
22129 cu->partial_dies = NULL;
ae038cb0
DJ
22130}
22131
22132/* Free all cached compilation units. */
22133
22134static void
22135free_cached_comp_units (void *data)
22136{
22137 struct dwarf2_per_cu_data *per_cu, **last_chain;
22138
22139 per_cu = dwarf2_per_objfile->read_in_chain;
22140 last_chain = &dwarf2_per_objfile->read_in_chain;
22141 while (per_cu != NULL)
22142 {
22143 struct dwarf2_per_cu_data *next_cu;
22144
22145 next_cu = per_cu->cu->read_in_chain;
22146
68dc6402 22147 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22148 *last_chain = next_cu;
22149
22150 per_cu = next_cu;
22151 }
22152}
22153
22154/* Increase the age counter on each cached compilation unit, and free
22155 any that are too old. */
22156
22157static void
22158age_cached_comp_units (void)
22159{
22160 struct dwarf2_per_cu_data *per_cu, **last_chain;
22161
22162 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22163 per_cu = dwarf2_per_objfile->read_in_chain;
22164 while (per_cu != NULL)
22165 {
22166 per_cu->cu->last_used ++;
b4f54984 22167 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22168 dwarf2_mark (per_cu->cu);
22169 per_cu = per_cu->cu->read_in_chain;
22170 }
22171
22172 per_cu = dwarf2_per_objfile->read_in_chain;
22173 last_chain = &dwarf2_per_objfile->read_in_chain;
22174 while (per_cu != NULL)
22175 {
22176 struct dwarf2_per_cu_data *next_cu;
22177
22178 next_cu = per_cu->cu->read_in_chain;
22179
22180 if (!per_cu->cu->mark)
22181 {
68dc6402 22182 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22183 *last_chain = next_cu;
22184 }
22185 else
22186 last_chain = &per_cu->cu->read_in_chain;
22187
22188 per_cu = next_cu;
22189 }
22190}
22191
22192/* Remove a single compilation unit from the cache. */
22193
22194static void
dee91e82 22195free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22196{
22197 struct dwarf2_per_cu_data *per_cu, **last_chain;
22198
22199 per_cu = dwarf2_per_objfile->read_in_chain;
22200 last_chain = &dwarf2_per_objfile->read_in_chain;
22201 while (per_cu != NULL)
22202 {
22203 struct dwarf2_per_cu_data *next_cu;
22204
22205 next_cu = per_cu->cu->read_in_chain;
22206
dee91e82 22207 if (per_cu == target_per_cu)
ae038cb0 22208 {
68dc6402 22209 free_heap_comp_unit (per_cu->cu);
dee91e82 22210 per_cu->cu = NULL;
ae038cb0
DJ
22211 *last_chain = next_cu;
22212 break;
22213 }
22214 else
22215 last_chain = &per_cu->cu->read_in_chain;
22216
22217 per_cu = next_cu;
22218 }
22219}
22220
fe3e1990
DJ
22221/* Release all extra memory associated with OBJFILE. */
22222
22223void
22224dwarf2_free_objfile (struct objfile *objfile)
22225{
22226 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
22227
22228 if (dwarf2_per_objfile == NULL)
22229 return;
22230
22231 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22232 free_cached_comp_units (NULL);
22233
7b9f3c50
DE
22234 if (dwarf2_per_objfile->quick_file_names_table)
22235 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22236
527f3840
JK
22237 if (dwarf2_per_objfile->line_header_hash)
22238 htab_delete (dwarf2_per_objfile->line_header_hash);
22239
fe3e1990
DJ
22240 /* Everything else should be on the objfile obstack. */
22241}
22242
dee91e82
DE
22243/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22244 We store these in a hash table separate from the DIEs, and preserve them
22245 when the DIEs are flushed out of cache.
22246
22247 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22248 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22249 or the type may come from a DWO file. Furthermore, while it's more logical
22250 to use per_cu->section+offset, with Fission the section with the data is in
22251 the DWO file but we don't know that section at the point we need it.
22252 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22253 because we can enter the lookup routine, get_die_type_at_offset, from
22254 outside this file, and thus won't necessarily have PER_CU->cu.
22255 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22256
dee91e82 22257struct dwarf2_per_cu_offset_and_type
1c379e20 22258{
dee91e82 22259 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 22260 sect_offset offset;
1c379e20
DJ
22261 struct type *type;
22262};
22263
dee91e82 22264/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22265
22266static hashval_t
dee91e82 22267per_cu_offset_and_type_hash (const void *item)
1c379e20 22268{
dee91e82 22269 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 22270
dee91e82 22271 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
22272}
22273
dee91e82 22274/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22275
22276static int
dee91e82 22277per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22278{
dee91e82
DE
22279 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
22280 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 22281
dee91e82
DE
22282 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22283 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
22284}
22285
22286/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
22287 table if necessary. For convenience, return TYPE.
22288
22289 The DIEs reading must have careful ordering to:
22290 * Not cause infite loops trying to read in DIEs as a prerequisite for
22291 reading current DIE.
22292 * Not trying to dereference contents of still incompletely read in types
22293 while reading in other DIEs.
22294 * Enable referencing still incompletely read in types just by a pointer to
22295 the type without accessing its fields.
22296
22297 Therefore caller should follow these rules:
22298 * Try to fetch any prerequisite types we may need to build this DIE type
22299 before building the type and calling set_die_type.
e71ec853 22300 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22301 possible before fetching more types to complete the current type.
22302 * Make the type as complete as possible before fetching more types. */
1c379e20 22303
f792889a 22304static struct type *
1c379e20
DJ
22305set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22306{
dee91e82 22307 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22308 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22309 struct attribute *attr;
22310 struct dynamic_prop prop;
1c379e20 22311
b4ba55a1
JB
22312 /* For Ada types, make sure that the gnat-specific data is always
22313 initialized (if not already set). There are a few types where
22314 we should not be doing so, because the type-specific area is
22315 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22316 where the type-specific area is used to store the floatformat).
22317 But this is not a problem, because the gnat-specific information
22318 is actually not needed for these types. */
22319 if (need_gnat_info (cu)
22320 && TYPE_CODE (type) != TYPE_CODE_FUNC
22321 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
22322 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22323 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22324 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22325 && !HAVE_GNAT_AUX_INFO (type))
22326 INIT_GNAT_SPECIFIC (type);
22327
3cdcd0ce
JB
22328 /* Read DW_AT_data_location and set in type. */
22329 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22330 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 22331 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22332
dee91e82 22333 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22334 {
dee91e82
DE
22335 dwarf2_per_objfile->die_type_hash =
22336 htab_create_alloc_ex (127,
22337 per_cu_offset_and_type_hash,
22338 per_cu_offset_and_type_eq,
22339 NULL,
22340 &objfile->objfile_obstack,
22341 hashtab_obstack_allocate,
22342 dummy_obstack_deallocate);
f792889a 22343 }
1c379e20 22344
dee91e82 22345 ofs.per_cu = cu->per_cu;
1c379e20
DJ
22346 ofs.offset = die->offset;
22347 ofs.type = type;
dee91e82
DE
22348 slot = (struct dwarf2_per_cu_offset_and_type **)
22349 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
22350 if (*slot)
22351 complaint (&symfile_complaints,
22352 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 22353 die->offset.sect_off);
673bfd45 22354 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 22355 **slot = ofs;
f792889a 22356 return type;
1c379e20
DJ
22357}
22358
02142a6c
DE
22359/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22360 or return NULL if the die does not have a saved type. */
1c379e20
DJ
22361
22362static struct type *
b64f50a1 22363get_die_type_at_offset (sect_offset offset,
673bfd45 22364 struct dwarf2_per_cu_data *per_cu)
1c379e20 22365{
dee91e82 22366 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 22367
dee91e82 22368 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22369 return NULL;
1c379e20 22370
dee91e82 22371 ofs.per_cu = per_cu;
673bfd45 22372 ofs.offset = offset;
dee91e82 22373 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
22374 if (slot)
22375 return slot->type;
22376 else
22377 return NULL;
22378}
22379
02142a6c 22380/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
22381 or return NULL if DIE does not have a saved type. */
22382
22383static struct type *
22384get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22385{
22386 return get_die_type_at_offset (die->offset, cu->per_cu);
22387}
22388
10b3939b
DJ
22389/* Add a dependence relationship from CU to REF_PER_CU. */
22390
22391static void
22392dwarf2_add_dependence (struct dwarf2_cu *cu,
22393 struct dwarf2_per_cu_data *ref_per_cu)
22394{
22395 void **slot;
22396
22397 if (cu->dependencies == NULL)
22398 cu->dependencies
22399 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22400 NULL, &cu->comp_unit_obstack,
22401 hashtab_obstack_allocate,
22402 dummy_obstack_deallocate);
22403
22404 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22405 if (*slot == NULL)
22406 *slot = ref_per_cu;
22407}
1c379e20 22408
f504f079
DE
22409/* Subroutine of dwarf2_mark to pass to htab_traverse.
22410 Set the mark field in every compilation unit in the
ae038cb0
DJ
22411 cache that we must keep because we are keeping CU. */
22412
10b3939b
DJ
22413static int
22414dwarf2_mark_helper (void **slot, void *data)
22415{
22416 struct dwarf2_per_cu_data *per_cu;
22417
22418 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
22419
22420 /* cu->dependencies references may not yet have been ever read if QUIT aborts
22421 reading of the chain. As such dependencies remain valid it is not much
22422 useful to track and undo them during QUIT cleanups. */
22423 if (per_cu->cu == NULL)
22424 return 1;
22425
10b3939b
DJ
22426 if (per_cu->cu->mark)
22427 return 1;
22428 per_cu->cu->mark = 1;
22429
22430 if (per_cu->cu->dependencies != NULL)
22431 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
22432
22433 return 1;
22434}
22435
f504f079
DE
22436/* Set the mark field in CU and in every other compilation unit in the
22437 cache that we must keep because we are keeping CU. */
22438
ae038cb0
DJ
22439static void
22440dwarf2_mark (struct dwarf2_cu *cu)
22441{
22442 if (cu->mark)
22443 return;
22444 cu->mark = 1;
10b3939b
DJ
22445 if (cu->dependencies != NULL)
22446 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
22447}
22448
22449static void
22450dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
22451{
22452 while (per_cu)
22453 {
22454 per_cu->cu->mark = 0;
22455 per_cu = per_cu->cu->read_in_chain;
22456 }
72bf9492
DJ
22457}
22458
72bf9492
DJ
22459/* Trivial hash function for partial_die_info: the hash value of a DIE
22460 is its offset in .debug_info for this objfile. */
22461
22462static hashval_t
22463partial_die_hash (const void *item)
22464{
22465 const struct partial_die_info *part_die = item;
9a619af0 22466
b64f50a1 22467 return part_die->offset.sect_off;
72bf9492
DJ
22468}
22469
22470/* Trivial comparison function for partial_die_info structures: two DIEs
22471 are equal if they have the same offset. */
22472
22473static int
22474partial_die_eq (const void *item_lhs, const void *item_rhs)
22475{
22476 const struct partial_die_info *part_die_lhs = item_lhs;
22477 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 22478
b64f50a1 22479 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
22480}
22481
b4f54984
DE
22482static struct cmd_list_element *set_dwarf_cmdlist;
22483static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
22484
22485static void
b4f54984 22486set_dwarf_cmd (char *args, int from_tty)
ae038cb0 22487{
b4f54984 22488 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 22489 gdb_stdout);
ae038cb0
DJ
22490}
22491
22492static void
b4f54984 22493show_dwarf_cmd (char *args, int from_tty)
6e70227d 22494{
b4f54984 22495 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
22496}
22497
4bf44c1c 22498/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
22499
22500static void
c1bd65d0 22501dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
22502{
22503 struct dwarf2_per_objfile *data = d;
8b70b953 22504 int ix;
8b70b953 22505
626f2d1c
TT
22506 /* Make sure we don't accidentally use dwarf2_per_objfile while
22507 cleaning up. */
22508 dwarf2_per_objfile = NULL;
22509
59b0c7c1
JB
22510 for (ix = 0; ix < data->n_comp_units; ++ix)
22511 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 22512
59b0c7c1 22513 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 22514 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
22515 data->all_type_units[ix]->per_cu.imported_symtabs);
22516 xfree (data->all_type_units);
95554aad 22517
8b70b953 22518 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
22519
22520 if (data->dwo_files)
22521 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
22522 if (data->dwp_file)
22523 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
22524
22525 if (data->dwz_file && data->dwz_file->dwz_bfd)
22526 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
22527}
22528
22529\f
ae2de4f8 22530/* The "save gdb-index" command. */
9291a0cd
TT
22531
22532/* The contents of the hash table we create when building the string
22533 table. */
22534struct strtab_entry
22535{
22536 offset_type offset;
22537 const char *str;
22538};
22539
559a7a62
JK
22540/* Hash function for a strtab_entry.
22541
22542 Function is used only during write_hash_table so no index format backward
22543 compatibility is needed. */
b89be57b 22544
9291a0cd
TT
22545static hashval_t
22546hash_strtab_entry (const void *e)
22547{
22548 const struct strtab_entry *entry = e;
559a7a62 22549 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
22550}
22551
22552/* Equality function for a strtab_entry. */
b89be57b 22553
9291a0cd
TT
22554static int
22555eq_strtab_entry (const void *a, const void *b)
22556{
22557 const struct strtab_entry *ea = a;
22558 const struct strtab_entry *eb = b;
22559 return !strcmp (ea->str, eb->str);
22560}
22561
22562/* Create a strtab_entry hash table. */
b89be57b 22563
9291a0cd
TT
22564static htab_t
22565create_strtab (void)
22566{
22567 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
22568 xfree, xcalloc, xfree);
22569}
22570
22571/* Add a string to the constant pool. Return the string's offset in
22572 host order. */
b89be57b 22573
9291a0cd
TT
22574static offset_type
22575add_string (htab_t table, struct obstack *cpool, const char *str)
22576{
22577 void **slot;
22578 struct strtab_entry entry;
22579 struct strtab_entry *result;
22580
22581 entry.str = str;
22582 slot = htab_find_slot (table, &entry, INSERT);
22583 if (*slot)
22584 result = *slot;
22585 else
22586 {
22587 result = XNEW (struct strtab_entry);
22588 result->offset = obstack_object_size (cpool);
22589 result->str = str;
22590 obstack_grow_str0 (cpool, str);
22591 *slot = result;
22592 }
22593 return result->offset;
22594}
22595
22596/* An entry in the symbol table. */
22597struct symtab_index_entry
22598{
22599 /* The name of the symbol. */
22600 const char *name;
22601 /* The offset of the name in the constant pool. */
22602 offset_type index_offset;
22603 /* A sorted vector of the indices of all the CUs that hold an object
22604 of this name. */
22605 VEC (offset_type) *cu_indices;
22606};
22607
22608/* The symbol table. This is a power-of-2-sized hash table. */
22609struct mapped_symtab
22610{
22611 offset_type n_elements;
22612 offset_type size;
22613 struct symtab_index_entry **data;
22614};
22615
22616/* Hash function for a symtab_index_entry. */
b89be57b 22617
9291a0cd
TT
22618static hashval_t
22619hash_symtab_entry (const void *e)
22620{
22621 const struct symtab_index_entry *entry = e;
22622 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
22623 sizeof (offset_type) * VEC_length (offset_type,
22624 entry->cu_indices),
22625 0);
22626}
22627
22628/* Equality function for a symtab_index_entry. */
b89be57b 22629
9291a0cd
TT
22630static int
22631eq_symtab_entry (const void *a, const void *b)
22632{
22633 const struct symtab_index_entry *ea = a;
22634 const struct symtab_index_entry *eb = b;
22635 int len = VEC_length (offset_type, ea->cu_indices);
22636 if (len != VEC_length (offset_type, eb->cu_indices))
22637 return 0;
22638 return !memcmp (VEC_address (offset_type, ea->cu_indices),
22639 VEC_address (offset_type, eb->cu_indices),
22640 sizeof (offset_type) * len);
22641}
22642
22643/* Destroy a symtab_index_entry. */
b89be57b 22644
9291a0cd
TT
22645static void
22646delete_symtab_entry (void *p)
22647{
22648 struct symtab_index_entry *entry = p;
22649 VEC_free (offset_type, entry->cu_indices);
22650 xfree (entry);
22651}
22652
22653/* Create a hash table holding symtab_index_entry objects. */
b89be57b 22654
9291a0cd 22655static htab_t
3876f04e 22656create_symbol_hash_table (void)
9291a0cd
TT
22657{
22658 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
22659 delete_symtab_entry, xcalloc, xfree);
22660}
22661
22662/* Create a new mapped symtab object. */
b89be57b 22663
9291a0cd
TT
22664static struct mapped_symtab *
22665create_mapped_symtab (void)
22666{
22667 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
22668 symtab->n_elements = 0;
22669 symtab->size = 1024;
22670 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22671 return symtab;
22672}
22673
22674/* Destroy a mapped_symtab. */
b89be57b 22675
9291a0cd
TT
22676static void
22677cleanup_mapped_symtab (void *p)
22678{
22679 struct mapped_symtab *symtab = p;
22680 /* The contents of the array are freed when the other hash table is
22681 destroyed. */
22682 xfree (symtab->data);
22683 xfree (symtab);
22684}
22685
22686/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
22687 the slot.
22688
22689 Function is used only during write_hash_table so no index format backward
22690 compatibility is needed. */
b89be57b 22691
9291a0cd
TT
22692static struct symtab_index_entry **
22693find_slot (struct mapped_symtab *symtab, const char *name)
22694{
559a7a62 22695 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
22696
22697 index = hash & (symtab->size - 1);
22698 step = ((hash * 17) & (symtab->size - 1)) | 1;
22699
22700 for (;;)
22701 {
22702 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
22703 return &symtab->data[index];
22704 index = (index + step) & (symtab->size - 1);
22705 }
22706}
22707
22708/* Expand SYMTAB's hash table. */
b89be57b 22709
9291a0cd
TT
22710static void
22711hash_expand (struct mapped_symtab *symtab)
22712{
22713 offset_type old_size = symtab->size;
22714 offset_type i;
22715 struct symtab_index_entry **old_entries = symtab->data;
22716
22717 symtab->size *= 2;
22718 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22719
22720 for (i = 0; i < old_size; ++i)
22721 {
22722 if (old_entries[i])
22723 {
22724 struct symtab_index_entry **slot = find_slot (symtab,
22725 old_entries[i]->name);
22726 *slot = old_entries[i];
22727 }
22728 }
22729
22730 xfree (old_entries);
22731}
22732
156942c7
DE
22733/* Add an entry to SYMTAB. NAME is the name of the symbol.
22734 CU_INDEX is the index of the CU in which the symbol appears.
22735 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 22736
9291a0cd
TT
22737static void
22738add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 22739 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
22740 offset_type cu_index)
22741{
22742 struct symtab_index_entry **slot;
156942c7 22743 offset_type cu_index_and_attrs;
9291a0cd
TT
22744
22745 ++symtab->n_elements;
22746 if (4 * symtab->n_elements / 3 >= symtab->size)
22747 hash_expand (symtab);
22748
22749 slot = find_slot (symtab, name);
22750 if (!*slot)
22751 {
22752 *slot = XNEW (struct symtab_index_entry);
22753 (*slot)->name = name;
156942c7 22754 /* index_offset is set later. */
9291a0cd
TT
22755 (*slot)->cu_indices = NULL;
22756 }
156942c7
DE
22757
22758 cu_index_and_attrs = 0;
22759 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
22760 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
22761 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
22762
22763 /* We don't want to record an index value twice as we want to avoid the
22764 duplication.
22765 We process all global symbols and then all static symbols
22766 (which would allow us to avoid the duplication by only having to check
22767 the last entry pushed), but a symbol could have multiple kinds in one CU.
22768 To keep things simple we don't worry about the duplication here and
22769 sort and uniqufy the list after we've processed all symbols. */
22770 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
22771}
22772
22773/* qsort helper routine for uniquify_cu_indices. */
22774
22775static int
22776offset_type_compare (const void *ap, const void *bp)
22777{
22778 offset_type a = *(offset_type *) ap;
22779 offset_type b = *(offset_type *) bp;
22780
22781 return (a > b) - (b > a);
22782}
22783
22784/* Sort and remove duplicates of all symbols' cu_indices lists. */
22785
22786static void
22787uniquify_cu_indices (struct mapped_symtab *symtab)
22788{
22789 int i;
22790
22791 for (i = 0; i < symtab->size; ++i)
22792 {
22793 struct symtab_index_entry *entry = symtab->data[i];
22794
22795 if (entry
22796 && entry->cu_indices != NULL)
22797 {
22798 unsigned int next_to_insert, next_to_check;
22799 offset_type last_value;
22800
22801 qsort (VEC_address (offset_type, entry->cu_indices),
22802 VEC_length (offset_type, entry->cu_indices),
22803 sizeof (offset_type), offset_type_compare);
22804
22805 last_value = VEC_index (offset_type, entry->cu_indices, 0);
22806 next_to_insert = 1;
22807 for (next_to_check = 1;
22808 next_to_check < VEC_length (offset_type, entry->cu_indices);
22809 ++next_to_check)
22810 {
22811 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
22812 != last_value)
22813 {
22814 last_value = VEC_index (offset_type, entry->cu_indices,
22815 next_to_check);
22816 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
22817 last_value);
22818 ++next_to_insert;
22819 }
22820 }
22821 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
22822 }
22823 }
9291a0cd
TT
22824}
22825
22826/* Add a vector of indices to the constant pool. */
b89be57b 22827
9291a0cd 22828static offset_type
3876f04e 22829add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
22830 struct symtab_index_entry *entry)
22831{
22832 void **slot;
22833
3876f04e 22834 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
22835 if (!*slot)
22836 {
22837 offset_type len = VEC_length (offset_type, entry->cu_indices);
22838 offset_type val = MAYBE_SWAP (len);
22839 offset_type iter;
22840 int i;
22841
22842 *slot = entry;
22843 entry->index_offset = obstack_object_size (cpool);
22844
22845 obstack_grow (cpool, &val, sizeof (val));
22846 for (i = 0;
22847 VEC_iterate (offset_type, entry->cu_indices, i, iter);
22848 ++i)
22849 {
22850 val = MAYBE_SWAP (iter);
22851 obstack_grow (cpool, &val, sizeof (val));
22852 }
22853 }
22854 else
22855 {
22856 struct symtab_index_entry *old_entry = *slot;
22857 entry->index_offset = old_entry->index_offset;
22858 entry = old_entry;
22859 }
22860 return entry->index_offset;
22861}
22862
22863/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
22864 constant pool entries going into the obstack CPOOL. */
b89be57b 22865
9291a0cd
TT
22866static void
22867write_hash_table (struct mapped_symtab *symtab,
22868 struct obstack *output, struct obstack *cpool)
22869{
22870 offset_type i;
3876f04e 22871 htab_t symbol_hash_table;
9291a0cd
TT
22872 htab_t str_table;
22873
3876f04e 22874 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 22875 str_table = create_strtab ();
3876f04e 22876
9291a0cd
TT
22877 /* We add all the index vectors to the constant pool first, to
22878 ensure alignment is ok. */
22879 for (i = 0; i < symtab->size; ++i)
22880 {
22881 if (symtab->data[i])
3876f04e 22882 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
22883 }
22884
22885 /* Now write out the hash table. */
22886 for (i = 0; i < symtab->size; ++i)
22887 {
22888 offset_type str_off, vec_off;
22889
22890 if (symtab->data[i])
22891 {
22892 str_off = add_string (str_table, cpool, symtab->data[i]->name);
22893 vec_off = symtab->data[i]->index_offset;
22894 }
22895 else
22896 {
22897 /* While 0 is a valid constant pool index, it is not valid
22898 to have 0 for both offsets. */
22899 str_off = 0;
22900 vec_off = 0;
22901 }
22902
22903 str_off = MAYBE_SWAP (str_off);
22904 vec_off = MAYBE_SWAP (vec_off);
22905
22906 obstack_grow (output, &str_off, sizeof (str_off));
22907 obstack_grow (output, &vec_off, sizeof (vec_off));
22908 }
22909
22910 htab_delete (str_table);
3876f04e 22911 htab_delete (symbol_hash_table);
9291a0cd
TT
22912}
22913
0a5429f6
DE
22914/* Struct to map psymtab to CU index in the index file. */
22915struct psymtab_cu_index_map
22916{
22917 struct partial_symtab *psymtab;
22918 unsigned int cu_index;
22919};
22920
22921static hashval_t
22922hash_psymtab_cu_index (const void *item)
22923{
22924 const struct psymtab_cu_index_map *map = item;
22925
22926 return htab_hash_pointer (map->psymtab);
22927}
22928
22929static int
22930eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
22931{
22932 const struct psymtab_cu_index_map *lhs = item_lhs;
22933 const struct psymtab_cu_index_map *rhs = item_rhs;
22934
22935 return lhs->psymtab == rhs->psymtab;
22936}
22937
22938/* Helper struct for building the address table. */
22939struct addrmap_index_data
22940{
22941 struct objfile *objfile;
22942 struct obstack *addr_obstack;
22943 htab_t cu_index_htab;
22944
22945 /* Non-zero if the previous_* fields are valid.
22946 We can't write an entry until we see the next entry (since it is only then
22947 that we know the end of the entry). */
22948 int previous_valid;
22949 /* Index of the CU in the table of all CUs in the index file. */
22950 unsigned int previous_cu_index;
0963b4bd 22951 /* Start address of the CU. */
0a5429f6
DE
22952 CORE_ADDR previous_cu_start;
22953};
22954
22955/* Write an address entry to OBSTACK. */
b89be57b 22956
9291a0cd 22957static void
0a5429f6
DE
22958add_address_entry (struct objfile *objfile, struct obstack *obstack,
22959 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 22960{
0a5429f6 22961 offset_type cu_index_to_write;
948f8e3d 22962 gdb_byte addr[8];
9291a0cd
TT
22963 CORE_ADDR baseaddr;
22964
22965 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22966
0a5429f6
DE
22967 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
22968 obstack_grow (obstack, addr, 8);
22969 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
22970 obstack_grow (obstack, addr, 8);
22971 cu_index_to_write = MAYBE_SWAP (cu_index);
22972 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
22973}
22974
22975/* Worker function for traversing an addrmap to build the address table. */
22976
22977static int
22978add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
22979{
22980 struct addrmap_index_data *data = datap;
22981 struct partial_symtab *pst = obj;
0a5429f6
DE
22982
22983 if (data->previous_valid)
22984 add_address_entry (data->objfile, data->addr_obstack,
22985 data->previous_cu_start, start_addr,
22986 data->previous_cu_index);
22987
22988 data->previous_cu_start = start_addr;
22989 if (pst != NULL)
22990 {
22991 struct psymtab_cu_index_map find_map, *map;
22992 find_map.psymtab = pst;
22993 map = htab_find (data->cu_index_htab, &find_map);
22994 gdb_assert (map != NULL);
22995 data->previous_cu_index = map->cu_index;
22996 data->previous_valid = 1;
22997 }
22998 else
22999 data->previous_valid = 0;
23000
23001 return 0;
23002}
23003
23004/* Write OBJFILE's address map to OBSTACK.
23005 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23006 in the index file. */
23007
23008static void
23009write_address_map (struct objfile *objfile, struct obstack *obstack,
23010 htab_t cu_index_htab)
23011{
23012 struct addrmap_index_data addrmap_index_data;
23013
23014 /* When writing the address table, we have to cope with the fact that
23015 the addrmap iterator only provides the start of a region; we have to
23016 wait until the next invocation to get the start of the next region. */
23017
23018 addrmap_index_data.objfile = objfile;
23019 addrmap_index_data.addr_obstack = obstack;
23020 addrmap_index_data.cu_index_htab = cu_index_htab;
23021 addrmap_index_data.previous_valid = 0;
23022
23023 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23024 &addrmap_index_data);
23025
23026 /* It's highly unlikely the last entry (end address = 0xff...ff)
23027 is valid, but we should still handle it.
23028 The end address is recorded as the start of the next region, but that
23029 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23030 anyway. */
23031 if (addrmap_index_data.previous_valid)
23032 add_address_entry (objfile, obstack,
23033 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23034 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23035}
23036
156942c7
DE
23037/* Return the symbol kind of PSYM. */
23038
23039static gdb_index_symbol_kind
23040symbol_kind (struct partial_symbol *psym)
23041{
23042 domain_enum domain = PSYMBOL_DOMAIN (psym);
23043 enum address_class aclass = PSYMBOL_CLASS (psym);
23044
23045 switch (domain)
23046 {
23047 case VAR_DOMAIN:
23048 switch (aclass)
23049 {
23050 case LOC_BLOCK:
23051 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23052 case LOC_TYPEDEF:
23053 return GDB_INDEX_SYMBOL_KIND_TYPE;
23054 case LOC_COMPUTED:
23055 case LOC_CONST_BYTES:
23056 case LOC_OPTIMIZED_OUT:
23057 case LOC_STATIC:
23058 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23059 case LOC_CONST:
23060 /* Note: It's currently impossible to recognize psyms as enum values
23061 short of reading the type info. For now punt. */
23062 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23063 default:
23064 /* There are other LOC_FOO values that one might want to classify
23065 as variables, but dwarf2read.c doesn't currently use them. */
23066 return GDB_INDEX_SYMBOL_KIND_OTHER;
23067 }
23068 case STRUCT_DOMAIN:
23069 return GDB_INDEX_SYMBOL_KIND_TYPE;
23070 default:
23071 return GDB_INDEX_SYMBOL_KIND_OTHER;
23072 }
23073}
23074
9291a0cd 23075/* Add a list of partial symbols to SYMTAB. */
b89be57b 23076
9291a0cd
TT
23077static void
23078write_psymbols (struct mapped_symtab *symtab,
987d643c 23079 htab_t psyms_seen,
9291a0cd
TT
23080 struct partial_symbol **psymp,
23081 int count,
987d643c
TT
23082 offset_type cu_index,
23083 int is_static)
9291a0cd
TT
23084{
23085 for (; count-- > 0; ++psymp)
23086 {
156942c7
DE
23087 struct partial_symbol *psym = *psymp;
23088 void **slot;
987d643c 23089
156942c7 23090 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23091 error (_("Ada is not currently supported by the index"));
987d643c 23092
987d643c 23093 /* Only add a given psymbol once. */
156942c7 23094 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
23095 if (!*slot)
23096 {
156942c7
DE
23097 gdb_index_symbol_kind kind = symbol_kind (psym);
23098
23099 *slot = psym;
23100 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23101 is_static, kind, cu_index);
987d643c 23102 }
9291a0cd
TT
23103 }
23104}
23105
23106/* Write the contents of an ("unfinished") obstack to FILE. Throw an
23107 exception if there is an error. */
b89be57b 23108
9291a0cd
TT
23109static void
23110write_obstack (FILE *file, struct obstack *obstack)
23111{
23112 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23113 file)
23114 != obstack_object_size (obstack))
23115 error (_("couldn't data write to file"));
23116}
23117
23118/* Unlink a file if the argument is not NULL. */
b89be57b 23119
9291a0cd
TT
23120static void
23121unlink_if_set (void *p)
23122{
23123 char **filename = p;
23124 if (*filename)
23125 unlink (*filename);
23126}
23127
1fd400ff
TT
23128/* A helper struct used when iterating over debug_types. */
23129struct signatured_type_index_data
23130{
23131 struct objfile *objfile;
23132 struct mapped_symtab *symtab;
23133 struct obstack *types_list;
987d643c 23134 htab_t psyms_seen;
1fd400ff
TT
23135 int cu_index;
23136};
23137
23138/* A helper function that writes a single signatured_type to an
23139 obstack. */
b89be57b 23140
1fd400ff
TT
23141static int
23142write_one_signatured_type (void **slot, void *d)
23143{
23144 struct signatured_type_index_data *info = d;
23145 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23146 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23147 gdb_byte val[8];
23148
23149 write_psymbols (info->symtab,
987d643c 23150 info->psyms_seen,
3e43a32a
MS
23151 info->objfile->global_psymbols.list
23152 + psymtab->globals_offset,
987d643c
TT
23153 psymtab->n_global_syms, info->cu_index,
23154 0);
1fd400ff 23155 write_psymbols (info->symtab,
987d643c 23156 info->psyms_seen,
3e43a32a
MS
23157 info->objfile->static_psymbols.list
23158 + psymtab->statics_offset,
987d643c
TT
23159 psymtab->n_static_syms, info->cu_index,
23160 1);
1fd400ff 23161
b64f50a1
JK
23162 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23163 entry->per_cu.offset.sect_off);
1fd400ff 23164 obstack_grow (info->types_list, val, 8);
3019eac3
DE
23165 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23166 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
23167 obstack_grow (info->types_list, val, 8);
23168 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23169 obstack_grow (info->types_list, val, 8);
23170
23171 ++info->cu_index;
23172
23173 return 1;
23174}
23175
95554aad
TT
23176/* Recurse into all "included" dependencies and write their symbols as
23177 if they appeared in this psymtab. */
23178
23179static void
23180recursively_write_psymbols (struct objfile *objfile,
23181 struct partial_symtab *psymtab,
23182 struct mapped_symtab *symtab,
23183 htab_t psyms_seen,
23184 offset_type cu_index)
23185{
23186 int i;
23187
23188 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23189 if (psymtab->dependencies[i]->user != NULL)
23190 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23191 symtab, psyms_seen, cu_index);
23192
23193 write_psymbols (symtab,
23194 psyms_seen,
23195 objfile->global_psymbols.list + psymtab->globals_offset,
23196 psymtab->n_global_syms, cu_index,
23197 0);
23198 write_psymbols (symtab,
23199 psyms_seen,
23200 objfile->static_psymbols.list + psymtab->statics_offset,
23201 psymtab->n_static_syms, cu_index,
23202 1);
23203}
23204
9291a0cd 23205/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23206
9291a0cd
TT
23207static void
23208write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23209{
23210 struct cleanup *cleanup;
23211 char *filename, *cleanup_filename;
1fd400ff
TT
23212 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23213 struct obstack cu_list, types_cu_list;
9291a0cd
TT
23214 int i;
23215 FILE *out_file;
23216 struct mapped_symtab *symtab;
23217 offset_type val, size_of_contents, total_len;
23218 struct stat st;
987d643c 23219 htab_t psyms_seen;
0a5429f6
DE
23220 htab_t cu_index_htab;
23221 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 23222
9291a0cd
TT
23223 if (dwarf2_per_objfile->using_index)
23224 error (_("Cannot use an index to create the index"));
23225
8b70b953
TT
23226 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23227 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23228
260b681b
DE
23229 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23230 return;
23231
4262abfb
JK
23232 if (stat (objfile_name (objfile), &st) < 0)
23233 perror_with_name (objfile_name (objfile));
9291a0cd 23234
4262abfb 23235 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
23236 INDEX_SUFFIX, (char *) NULL);
23237 cleanup = make_cleanup (xfree, filename);
23238
614c279d 23239 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
23240 if (!out_file)
23241 error (_("Can't open `%s' for writing"), filename);
23242
23243 cleanup_filename = filename;
23244 make_cleanup (unlink_if_set, &cleanup_filename);
23245
23246 symtab = create_mapped_symtab ();
23247 make_cleanup (cleanup_mapped_symtab, symtab);
23248
23249 obstack_init (&addr_obstack);
23250 make_cleanup_obstack_free (&addr_obstack);
23251
23252 obstack_init (&cu_list);
23253 make_cleanup_obstack_free (&cu_list);
23254
1fd400ff
TT
23255 obstack_init (&types_cu_list);
23256 make_cleanup_obstack_free (&types_cu_list);
23257
987d643c
TT
23258 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
23259 NULL, xcalloc, xfree);
96408a79 23260 make_cleanup_htab_delete (psyms_seen);
987d643c 23261
0a5429f6
DE
23262 /* While we're scanning CU's create a table that maps a psymtab pointer
23263 (which is what addrmap records) to its index (which is what is recorded
23264 in the index file). This will later be needed to write the address
23265 table. */
23266 cu_index_htab = htab_create_alloc (100,
23267 hash_psymtab_cu_index,
23268 eq_psymtab_cu_index,
23269 NULL, xcalloc, xfree);
96408a79 23270 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
23271 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
23272 xmalloc (sizeof (struct psymtab_cu_index_map)
23273 * dwarf2_per_objfile->n_comp_units);
23274 make_cleanup (xfree, psymtab_cu_index_map);
23275
23276 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23277 work here. Also, the debug_types entries do not appear in
23278 all_comp_units, but only in their own hash table. */
9291a0cd
TT
23279 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23280 {
3e43a32a
MS
23281 struct dwarf2_per_cu_data *per_cu
23282 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23283 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23284 gdb_byte val[8];
0a5429f6
DE
23285 struct psymtab_cu_index_map *map;
23286 void **slot;
9291a0cd 23287
92fac807
JK
23288 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23289 It may be referenced from a local scope but in such case it does not
23290 need to be present in .gdb_index. */
23291 if (psymtab == NULL)
23292 continue;
23293
95554aad
TT
23294 if (psymtab->user == NULL)
23295 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 23296
0a5429f6
DE
23297 map = &psymtab_cu_index_map[i];
23298 map->psymtab = psymtab;
23299 map->cu_index = i;
23300 slot = htab_find_slot (cu_index_htab, map, INSERT);
23301 gdb_assert (slot != NULL);
23302 gdb_assert (*slot == NULL);
23303 *slot = map;
9291a0cd 23304
b64f50a1
JK
23305 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23306 per_cu->offset.sect_off);
9291a0cd 23307 obstack_grow (&cu_list, val, 8);
e254ef6a 23308 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23309 obstack_grow (&cu_list, val, 8);
23310 }
23311
0a5429f6
DE
23312 /* Dump the address map. */
23313 write_address_map (objfile, &addr_obstack, cu_index_htab);
23314
1fd400ff
TT
23315 /* Write out the .debug_type entries, if any. */
23316 if (dwarf2_per_objfile->signatured_types)
23317 {
23318 struct signatured_type_index_data sig_data;
23319
23320 sig_data.objfile = objfile;
23321 sig_data.symtab = symtab;
23322 sig_data.types_list = &types_cu_list;
987d643c 23323 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
23324 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23325 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23326 write_one_signatured_type, &sig_data);
23327 }
23328
156942c7
DE
23329 /* Now that we've processed all symbols we can shrink their cu_indices
23330 lists. */
23331 uniquify_cu_indices (symtab);
23332
9291a0cd
TT
23333 obstack_init (&constant_pool);
23334 make_cleanup_obstack_free (&constant_pool);
23335 obstack_init (&symtab_obstack);
23336 make_cleanup_obstack_free (&symtab_obstack);
23337 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23338
23339 obstack_init (&contents);
23340 make_cleanup_obstack_free (&contents);
1fd400ff 23341 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
23342 total_len = size_of_contents;
23343
23344 /* The version number. */
796a7ff8 23345 val = MAYBE_SWAP (8);
9291a0cd
TT
23346 obstack_grow (&contents, &val, sizeof (val));
23347
23348 /* The offset of the CU list from the start of the file. */
23349 val = MAYBE_SWAP (total_len);
23350 obstack_grow (&contents, &val, sizeof (val));
23351 total_len += obstack_object_size (&cu_list);
23352
1fd400ff
TT
23353 /* The offset of the types CU list from the start of the file. */
23354 val = MAYBE_SWAP (total_len);
23355 obstack_grow (&contents, &val, sizeof (val));
23356 total_len += obstack_object_size (&types_cu_list);
23357
9291a0cd
TT
23358 /* The offset of the address table from the start of the file. */
23359 val = MAYBE_SWAP (total_len);
23360 obstack_grow (&contents, &val, sizeof (val));
23361 total_len += obstack_object_size (&addr_obstack);
23362
23363 /* The offset of the symbol table from the start of the file. */
23364 val = MAYBE_SWAP (total_len);
23365 obstack_grow (&contents, &val, sizeof (val));
23366 total_len += obstack_object_size (&symtab_obstack);
23367
23368 /* The offset of the constant pool from the start of the file. */
23369 val = MAYBE_SWAP (total_len);
23370 obstack_grow (&contents, &val, sizeof (val));
23371 total_len += obstack_object_size (&constant_pool);
23372
23373 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23374
23375 write_obstack (out_file, &contents);
23376 write_obstack (out_file, &cu_list);
1fd400ff 23377 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
23378 write_obstack (out_file, &addr_obstack);
23379 write_obstack (out_file, &symtab_obstack);
23380 write_obstack (out_file, &constant_pool);
23381
23382 fclose (out_file);
23383
23384 /* We want to keep the file, so we set cleanup_filename to NULL
23385 here. See unlink_if_set. */
23386 cleanup_filename = NULL;
23387
23388 do_cleanups (cleanup);
23389}
23390
90476074
TT
23391/* Implementation of the `save gdb-index' command.
23392
23393 Note that the file format used by this command is documented in the
23394 GDB manual. Any changes here must be documented there. */
11570e71 23395
9291a0cd
TT
23396static void
23397save_gdb_index_command (char *arg, int from_tty)
23398{
23399 struct objfile *objfile;
23400
23401 if (!arg || !*arg)
96d19272 23402 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
23403
23404 ALL_OBJFILES (objfile)
23405 {
23406 struct stat st;
23407
23408 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 23409 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
23410 continue;
23411
23412 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
23413 if (dwarf2_per_objfile)
23414 {
9291a0cd 23415
492d29ea 23416 TRY
9291a0cd
TT
23417 {
23418 write_psymtabs_to_index (objfile, arg);
23419 }
492d29ea
PA
23420 CATCH (except, RETURN_MASK_ERROR)
23421 {
23422 exception_fprintf (gdb_stderr, except,
23423 _("Error while writing index for `%s': "),
23424 objfile_name (objfile));
23425 }
23426 END_CATCH
9291a0cd
TT
23427 }
23428 }
dce234bc
PP
23429}
23430
9291a0cd
TT
23431\f
23432
b4f54984 23433int dwarf_always_disassemble;
9eae7c52
TT
23434
23435static void
b4f54984
DE
23436show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23437 struct cmd_list_element *c, const char *value)
9eae7c52 23438{
3e43a32a
MS
23439 fprintf_filtered (file,
23440 _("Whether to always disassemble "
23441 "DWARF expressions is %s.\n"),
9eae7c52
TT
23442 value);
23443}
23444
900e11f9
JK
23445static void
23446show_check_physname (struct ui_file *file, int from_tty,
23447 struct cmd_list_element *c, const char *value)
23448{
23449 fprintf_filtered (file,
23450 _("Whether to check \"physname\" is %s.\n"),
23451 value);
23452}
23453
6502dd73
DJ
23454void _initialize_dwarf2_read (void);
23455
23456void
23457_initialize_dwarf2_read (void)
23458{
96d19272
JK
23459 struct cmd_list_element *c;
23460
dce234bc 23461 dwarf2_objfile_data_key
c1bd65d0 23462 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 23463
b4f54984
DE
23464 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23465Set DWARF specific variables.\n\
23466Configure DWARF variables such as the cache size"),
23467 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
23468 0/*allow-unknown*/, &maintenance_set_cmdlist);
23469
b4f54984
DE
23470 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23471Show DWARF specific variables\n\
23472Show DWARF variables such as the cache size"),
23473 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
23474 0/*allow-unknown*/, &maintenance_show_cmdlist);
23475
23476 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
23477 &dwarf_max_cache_age, _("\
23478Set the upper bound on the age of cached DWARF compilation units."), _("\
23479Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
23480A higher limit means that cached compilation units will be stored\n\
23481in memory longer, and more total memory will be used. Zero disables\n\
23482caching, which can slow down startup."),
2c5b56ce 23483 NULL,
b4f54984
DE
23484 show_dwarf_max_cache_age,
23485 &set_dwarf_cmdlist,
23486 &show_dwarf_cmdlist);
d97bc12b 23487
9eae7c52 23488 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 23489 &dwarf_always_disassemble, _("\
9eae7c52
TT
23490Set whether `info address' always disassembles DWARF expressions."), _("\
23491Show whether `info address' always disassembles DWARF expressions."), _("\
23492When enabled, DWARF expressions are always printed in an assembly-like\n\
23493syntax. When disabled, expressions will be printed in a more\n\
23494conversational style, when possible."),
23495 NULL,
b4f54984
DE
23496 show_dwarf_always_disassemble,
23497 &set_dwarf_cmdlist,
23498 &show_dwarf_cmdlist);
23499
23500 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23501Set debugging of the DWARF reader."), _("\
23502Show debugging of the DWARF reader."), _("\
23503When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
23504reading and symtab expansion. A value of 1 (one) provides basic\n\
23505information. A value greater than 1 provides more verbose information."),
45cfd468
DE
23506 NULL,
23507 NULL,
23508 &setdebuglist, &showdebuglist);
23509
b4f54984
DE
23510 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23511Set debugging of the DWARF DIE reader."), _("\
23512Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
23513When enabled (non-zero), DIEs are dumped after they are read in.\n\
23514The value is the maximum depth to print."),
ccce17b0
YQ
23515 NULL,
23516 NULL,
23517 &setdebuglist, &showdebuglist);
9291a0cd 23518
27e0867f
DE
23519 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23520Set debugging of the dwarf line reader."), _("\
23521Show debugging of the dwarf line reader."), _("\
23522When enabled (non-zero), line number entries are dumped as they are read in.\n\
23523A value of 1 (one) provides basic information.\n\
23524A value greater than 1 provides more verbose information."),
23525 NULL,
23526 NULL,
23527 &setdebuglist, &showdebuglist);
23528
900e11f9
JK
23529 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23530Set cross-checking of \"physname\" code against demangler."), _("\
23531Show cross-checking of \"physname\" code against demangler."), _("\
23532When enabled, GDB's internal \"physname\" code is checked against\n\
23533the demangler."),
23534 NULL, show_check_physname,
23535 &setdebuglist, &showdebuglist);
23536
e615022a
DE
23537 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23538 no_class, &use_deprecated_index_sections, _("\
23539Set whether to use deprecated gdb_index sections."), _("\
23540Show whether to use deprecated gdb_index sections."), _("\
23541When enabled, deprecated .gdb_index sections are used anyway.\n\
23542Normally they are ignored either because of a missing feature or\n\
23543performance issue.\n\
23544Warning: This option must be enabled before gdb reads the file."),
23545 NULL,
23546 NULL,
23547 &setlist, &showlist);
23548
96d19272 23549 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 23550 _("\
fc1a9d6e 23551Save a gdb-index file.\n\
11570e71 23552Usage: save gdb-index DIRECTORY"),
96d19272
JK
23553 &save_cmdlist);
23554 set_cmd_completer (c, filename_completer);
f1e6e072
TT
23555
23556 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23557 &dwarf2_locexpr_funcs);
23558 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23559 &dwarf2_loclist_funcs);
23560
23561 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23562 &dwarf2_block_frame_base_locexpr_funcs);
23563 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23564 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 23565}
This page took 3.605645 seconds and 4 git commands to generate.